mirror of
https://gitcode.com/JianFeeeee/TrulyMEM-TrueHumanMEM.git
synced 2026-10-15 12:15:32 +00:00
feat: Add config persistence
- Create ConfigManager for save/load - Save config to config.json - Load config on startup - Persist API key, model, base_url - Fix config not saved issue
This commit is contained in:
@ -17,6 +17,7 @@ from .widgets.message_history import MessageHistory
|
|||||||
from .models.message import Message, ToolCall, ToolResult
|
from .models.message import Message, ToolCall, ToolResult
|
||||||
from .models.config import AppConfig
|
from .models.config import AppConfig
|
||||||
from .models.log_entry import LogEntry
|
from .models.log_entry import LogEntry
|
||||||
|
from .services.config_manager import ConfigManager
|
||||||
from .core.imports import (
|
from .core.imports import (
|
||||||
Neo4jGraph,
|
Neo4jGraph,
|
||||||
GraphMemoryClient,
|
GraphMemoryClient,
|
||||||
@ -48,7 +49,15 @@ class GraphMemoryApp(App[None]):
|
|||||||
|
|
||||||
def __init__(self, config: AppConfig | None = None, **kwargs):
|
def __init__(self, config: AppConfig | None = None, **kwargs):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
self._config = config or AppConfig.from_env()
|
# 配置管理器
|
||||||
|
self._config_manager = ConfigManager()
|
||||||
|
# 优先使用传入的配置,其次加载持久化配置,最后使用环境变量
|
||||||
|
if config:
|
||||||
|
self._config = config
|
||||||
|
elif self._config_manager.exists():
|
||||||
|
self._config = self._config_manager.load()
|
||||||
|
else:
|
||||||
|
self._config = AppConfig.from_env()
|
||||||
|
|
||||||
# 核心组件
|
# 核心组件
|
||||||
self._graph: Neo4jGraph | None = None
|
self._graph: Neo4jGraph | None = None
|
||||||
@ -350,10 +359,13 @@ API Key 未配置!
|
|||||||
# 更新配置
|
# 更新配置
|
||||||
self._config = event.config
|
self._config = event.config
|
||||||
|
|
||||||
|
# 持久化保存配置
|
||||||
|
self._config_manager.save(self._config)
|
||||||
|
|
||||||
# 重新初始化客户端
|
# 重新初始化客户端
|
||||||
self._init_client()
|
self._init_client()
|
||||||
|
|
||||||
if self._client:
|
if self._client:
|
||||||
self.notify("✅ 配置已更新并应用", title="配置")
|
self.notify("✅ 配置已保存并应用", title="配置")
|
||||||
else:
|
else:
|
||||||
self.notify("⚠️ 配置已保存,但API Key无效", title="警告")
|
self.notify("⚠️ 配置已保存,但API Key无效", title="警告")
|
||||||
|
|||||||
46
graph_memory_tui/services/config_manager.py
Normal file
46
graph_memory_tui/services/config_manager.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
"""
|
||||||
|
配置管理 - 支持持久化
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
from .config import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigManager:
|
||||||
|
"""配置管理器 - 支持持久化"""
|
||||||
|
|
||||||
|
def __init__(self, config_file: str = "config.json"):
|
||||||
|
self.config_file = Path(config_file)
|
||||||
|
|
||||||
|
def save(self, config: AppConfig) -> None:
|
||||||
|
"""保存配置到文件"""
|
||||||
|
data = {
|
||||||
|
"api_key": config.api_key,
|
||||||
|
"model": config.model,
|
||||||
|
"base_url": config.base_url
|
||||||
|
}
|
||||||
|
|
||||||
|
with open(self.config_file, 'w', encoding='utf-8') as f:
|
||||||
|
json.dump(data, f, indent=2)
|
||||||
|
|
||||||
|
def load(self) -> AppConfig:
|
||||||
|
"""从文件加载配置"""
|
||||||
|
if not self.config_file.exists():
|
||||||
|
return AppConfig()
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(self.config_file, 'r', encoding='utf-8') as f:
|
||||||
|
data = json.load(f)
|
||||||
|
|
||||||
|
return AppConfig(
|
||||||
|
api_key=data.get("api_key", ""),
|
||||||
|
model=data.get("model", "deepseek-chat"),
|
||||||
|
base_url=data.get("base_url", "https://api.deepseek.com")
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
return AppConfig()
|
||||||
|
|
||||||
|
def exists(self) -> bool:
|
||||||
|
"""检查配置文件是否存在"""
|
||||||
|
return self.config_file.exists()
|
||||||
Reference in New Issue
Block a user