mirror of
https://gitcode.com/JianFeeeee/TrulyMEM-TrueHumanMEM.git
synced 2026-09-21 17:38:18 +00:00
fix: 配置持久化 - 启动时加载并保存到本地文件
- 修复配置无法持久化的问题,之前配置只保存在内存中 - 启动时从应用根目录加载 config.json - 配置变更时自动保存到本地文件 - 配置文件位置:源码运行时在项目根目录,打包后exe同级目录 - 修复 model 字段在保存时丢失的问题
This commit is contained in:
25
ui/app.py
25
ui/app.py
@ -5,6 +5,7 @@ from textual.binding import Binding
|
||||
|
||||
from core import BackendServer, BackendClient
|
||||
from .models.message import Message
|
||||
from .services.config_service import ConfigService
|
||||
|
||||
|
||||
class GraphMemoryApp(App):
|
||||
@ -22,18 +23,24 @@ class GraphMemoryApp(App):
|
||||
Binding("f6", "quit", "退出"),
|
||||
]
|
||||
|
||||
def __init__(self, backend_server: BackendServer = None, **kwargs):
|
||||
def __init__(self, backend_server: BackendServer = None, config_service: ConfigService = None, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self._backend_server = backend_server
|
||||
self._backend_client = BackendClient(backend_server) if backend_server else None
|
||||
self._config_service = config_service
|
||||
self._api_configured = False
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
from .widgets.left_panel import LeftPanel
|
||||
from .widgets.right_panel import RightPanel
|
||||
from .widgets.status_bar import StatusBar
|
||||
from .models.config import AppConfig
|
||||
|
||||
# 获取初始配置
|
||||
initial_config = self._config_service.get_config() if self._config_service else AppConfig()
|
||||
|
||||
yield LeftPanel()
|
||||
yield RightPanel()
|
||||
yield RightPanel(config=initial_config)
|
||||
yield StatusBar()
|
||||
|
||||
def on_mount(self) -> None:
|
||||
@ -137,17 +144,18 @@ class GraphMemoryApp(App):
|
||||
return
|
||||
|
||||
config = event.config
|
||||
api_key = config.api_key
|
||||
base_url = config.base_url
|
||||
|
||||
# 异步更新配置,避免阻塞UI
|
||||
asyncio.create_task(self._update_config_async(api_key, base_url))
|
||||
asyncio.create_task(self._update_config_async(config))
|
||||
|
||||
async def _update_config_async(self, api_key: str, base_url: str) -> None:
|
||||
async def _update_config_async(self, config) -> None:
|
||||
"""异步更新配置"""
|
||||
from .widgets.status_bar import StatusBar
|
||||
status_bar = self.query_one(StatusBar)
|
||||
|
||||
api_key = config.api_key
|
||||
base_url = config.base_url
|
||||
|
||||
try:
|
||||
result = await asyncio.get_event_loop().run_in_executor(
|
||||
None,
|
||||
@ -157,6 +165,11 @@ class GraphMemoryApp(App):
|
||||
if result.get("success"):
|
||||
self._api_configured = bool(api_key)
|
||||
status_bar.set_api_status(self._api_configured)
|
||||
|
||||
# 保存配置到文件(使用完整的config对象,保留model字段)
|
||||
if self._config_service:
|
||||
self._config_service.set_config(config)
|
||||
|
||||
self.notify("✅ 配置已保存并生效", title="配置成功", severity="information")
|
||||
else:
|
||||
error = result.get("error", "未知错误")
|
||||
|
||||
Reference in New Issue
Block a user