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:
@ -13,13 +13,21 @@ os.chdir(application_path)
|
|||||||
|
|
||||||
from core import BackendServer
|
from core import BackendServer
|
||||||
from ui import GraphMemoryApp
|
from ui import GraphMemoryApp
|
||||||
|
from ui.services.config_service import ConfigService
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
backend_server = BackendServer(db_path="graph_memory.db", use_embedded_db=True)
|
# 配置文件保存在应用根目录(exe同级目录或源码根目录)
|
||||||
backend_server.start(api_key="", base_url="https://api.deepseek.com")
|
config_file = application_path / "config.json"
|
||||||
|
|
||||||
app = GraphMemoryApp(backend_server=backend_server)
|
# 加载配置
|
||||||
|
config_service = ConfigService(config_file=config_file)
|
||||||
|
config = config_service.get_config()
|
||||||
|
|
||||||
|
backend_server = BackendServer(db_path="graph_memory.db", use_embedded_db=True)
|
||||||
|
backend_server.start(api_key=config.api_key, base_url=config.base_url)
|
||||||
|
|
||||||
|
app = GraphMemoryApp(backend_server=backend_server, config_service=config_service)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
app.run()
|
app.run()
|
||||||
|
|||||||
25
ui/app.py
25
ui/app.py
@ -5,6 +5,7 @@ from textual.binding import Binding
|
|||||||
|
|
||||||
from core import BackendServer, BackendClient
|
from core import BackendServer, BackendClient
|
||||||
from .models.message import Message
|
from .models.message import Message
|
||||||
|
from .services.config_service import ConfigService
|
||||||
|
|
||||||
|
|
||||||
class GraphMemoryApp(App):
|
class GraphMemoryApp(App):
|
||||||
@ -22,18 +23,24 @@ class GraphMemoryApp(App):
|
|||||||
Binding("f6", "quit", "退出"),
|
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)
|
super().__init__(**kwargs)
|
||||||
self._backend_server = backend_server
|
self._backend_server = backend_server
|
||||||
self._backend_client = BackendClient(backend_server) if backend_server else None
|
self._backend_client = BackendClient(backend_server) if backend_server else None
|
||||||
|
self._config_service = config_service
|
||||||
self._api_configured = False
|
self._api_configured = False
|
||||||
|
|
||||||
def compose(self) -> ComposeResult:
|
def compose(self) -> ComposeResult:
|
||||||
from .widgets.left_panel import LeftPanel
|
from .widgets.left_panel import LeftPanel
|
||||||
from .widgets.right_panel import RightPanel
|
from .widgets.right_panel import RightPanel
|
||||||
from .widgets.status_bar import StatusBar
|
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 LeftPanel()
|
||||||
yield RightPanel()
|
yield RightPanel(config=initial_config)
|
||||||
yield StatusBar()
|
yield StatusBar()
|
||||||
|
|
||||||
def on_mount(self) -> None:
|
def on_mount(self) -> None:
|
||||||
@ -137,17 +144,18 @@ class GraphMemoryApp(App):
|
|||||||
return
|
return
|
||||||
|
|
||||||
config = event.config
|
config = event.config
|
||||||
api_key = config.api_key
|
|
||||||
base_url = config.base_url
|
|
||||||
|
|
||||||
# 异步更新配置,避免阻塞UI
|
# 异步更新配置,避免阻塞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
|
from .widgets.status_bar import StatusBar
|
||||||
status_bar = self.query_one(StatusBar)
|
status_bar = self.query_one(StatusBar)
|
||||||
|
|
||||||
|
api_key = config.api_key
|
||||||
|
base_url = config.base_url
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = await asyncio.get_event_loop().run_in_executor(
|
result = await asyncio.get_event_loop().run_in_executor(
|
||||||
None,
|
None,
|
||||||
@ -157,6 +165,11 @@ class GraphMemoryApp(App):
|
|||||||
if result.get("success"):
|
if result.get("success"):
|
||||||
self._api_configured = bool(api_key)
|
self._api_configured = bool(api_key)
|
||||||
status_bar.set_api_status(self._api_configured)
|
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")
|
self.notify("✅ 配置已保存并生效", title="配置成功", severity="information")
|
||||||
else:
|
else:
|
||||||
error = result.get("error", "未知错误")
|
error = result.get("error", "未知错误")
|
||||||
|
|||||||
Reference in New Issue
Block a user