refactor: restructure to core/ + ui/ with multi-threaded backend

This commit is contained in:
root
2026-04-12 18:15:55 +08:00
parent c13d3f671c
commit 83906a6985
64 changed files with 5269 additions and 571 deletions

View File

@ -17,7 +17,7 @@ TrulyMEM (TrueHumanMEM) 是一个让 AI 拥有长期记忆能力的图记忆系
- **长期记忆存储**: 基于 SQLite 内嵌图数据库,开箱即用
- **人设图机制**: 支持角色扮演和性格设定
- **工作记忆链**: 维持对话连贯性的任务跟踪机制
- **流式消息显示**: 实时显示 AI 响应
- **TUI 与后端分离**: 多线程 Queue 通信
- **键盘驱动 TUI**: 无需鼠标,全键盘操作
- **跨平台支持**: Windows / Linux / macOS
- **独立部署**: 支持打包为可执行文件

View File

@ -5,68 +5,137 @@
- 键盘驱动,零鼠标依赖
- 极简视觉,信息密度优先
- 工具痕迹默认隐藏,需要时可展开
- TUI 与后端分离,多线程通信
## 项目结构
```
TrulyMEM-TrueHumanMEM/
├── trulymem_entry.py # 打包入口
├── graph_memory_tui/ # 核心应用包 (38 个 Python 文件)
│ ├── app.py # TUI 主应用 (GraphMemoryApp)
│ ├── main.py # 模块入口
│ ├── __init__.py
│ ├── core/ # 核心逻辑
│ │ ├── __init__.py
│ │ ├── imports.py # 动态导入(内嵌DB vs Neo4j)
│ │ ├── embedded_db.py # SQLite 图数据库实现
│ │ ├── graph_client.py # Neo4j 客户端(可选,未使用)
│ │ ├── optimized_operations.py
│ │ ├── prompts/ # 提示词管理
│ │ │ ├── __init__.py
│ │ │ ├── prompt_manager.py
│ │ │ └── templates/
│ │ │ └── system_prompt.md
│ │ └── tools/ # 工具定义与执行
│ │ ├── __init__.py
│ │ ├── memory_tools.py # 工具定义
│ │ ├── tool_executor.py # 工具执行器
│ │ └── tool_limiter.py # 调用限制器
│ ├── models/ # 数据模型
│ │ ├── __init__.py
│ │ ├── message.py # Message, ToolCall, ToolResult
│ │ ├── config.py # AppConfig
│ │ └── log_entry.py # LogEntry
│ ├── services/ # 服务层
│ │ ├── __init__.py
│ │ ├── config_manager.py # 配置持久化
│ │ ├── config_service.py
│ │ ├── chat_service.py
│ │ └── tool_service.py
│ ├── handlers/ # 事件处理
├── trulymem_entry.py # 入口:先启动 core → 再启动 ui
├── core/ # 后端/业务逻辑
│ ├── __init__.py # 导出 BackendServer, BackendClient, EmbeddedGraphDB
│ ├── server.py # BackendServer (多线程队列通信)
│ ├── client.py # BackendClient
│ ├── embedded_db.py # SQLite 图数据库实现
│ ├── graph_client.py
│ ├── tool_executor.py # 工具执行器
│ ├── tool_limiter.py # 工具调用限制器
│ ├── memory_tools.py # 工具定义
│ ├── prompts/ # 提示词管理
│ │ ├── __init__.py
│ │ ├── prompt_manager.py
│ │ └── templates/
│ │ └── system_prompt.md
│ └── tools/ # 工具模块
│ ├── __init__.py
│ ├── memory_tools.py
│ ├── tool_executor.py
│ └── tool_limiter.py
├── ui/ # TUI 显示层
│ ├── __init__.py # 导出 GraphMemoryApp, AppConfig
│ ├── app.py # GraphMemoryApp (纯显示)
│ ├── widgets/ # TUI 组件
│ │ ├── left_panel.py
│ │ ├── right_panel.py
│ │ ├── message_history.py
│ │ ├── message_widget.py
│ │ ├── input_box.py
│ │ ├── config_section.py
│ │ ├── operation_log.py
│ │ ├── cypher_query_box.py
│ │ └── status_bar.py
│ ├── handlers/ # 事件处理
│ │ ├── focus_handler.py
│ │ ├── key_handler.py
│ │ └── message_handler.py
│ ├── widgets/ # TUI 组件
│ │ ├── __init__.py
│ │ ├── left_panel.py # 左侧主对话区
│ │ ├── right_panel.py # 右侧边栏
│ │ ├── message_history.py # 消息历史列表
│ │ ├── message_widget.py # 单条消息组件
│ │ ├── input_box.py # 底部输入框
│ │ ├── config_section.py # 配置区
│ │ ├── operation_log.py # 图操作日志
│ │ ├── cypher_query_box.py # 查询框(注:无实际 Cypher 支持)
│ │ └── status_bar.py # 状态栏
│ └── styles/ # 样式文件
│ ├── __init__.py
│ ├── models/ # 数据模型
│ │ ├── message.py
│ │ ├── config.py
│ │ └── log_entry.py
│ ├── services/ # 服务层
│ │ ├── config_manager.py
│ │ ├── config_service.py
│ │ ├── chat_service.py
│ │ └── tool_service.py
│ └── styles/ # 样式文件
│ ├── app.css
│ ├── components.css
│ └── messages.css
├── tests/ # 测试(pytest)
├── docs/ # 文档
├── requirements.txt # 依赖
└── build_*.{bat,sh} # 打包脚本
└── tests/ # 测试 (38 tests)
```
## 架构图
```
trulymem_entry.py
│
├─ BackendServer.start() → 独立线程运行
│ ├─ 处理 PROCESS_MESSAGE 请求
│ ├─ 处理 EXECUTE_TOOL 请求
│ └─ 管理 GraphMemoryClient, EmbeddedGraphDB
│
└─ GraphMemoryApp(backend_server=server)
│
└─ BackendClient ← queue.Queue → BackendServer
```
## 组件职责
### core/ (后端)
| 组件 | 职责 |
|------|------|
| `server.py` | 多线程队列通信,处理消息和工具调用 |
| `client.py` | TUI 端的通信客户端 |
| `embedded_db.py` | SQLite 图数据库 CRUD |
| `graph_client.py` | OpenAI/DeepSeek API 客户端 |
| `tool_executor.py` | 工具执行逻辑 |
| `tool_limiter.py` | 工具调用频率限制 |
### ui/ (显示层)
| 组件 | 职责 |
|------|------|
| `app.py` | Textual 应用主类 |
| `widgets/` | TUI 组件(面板、输入框等) |
| `handlers/` | 事件处理(键盘、焦点) |
| `models/` | 数据模型(消息、配置) |
| `services/` | 配置管理、服务层 |
## 数据流
```
用户输入 → InputBox → on_input_box_send_message
↓
BackendClient.process_message(user_input)
↓
queue.Queue → BackendServer (独立线程)
↓
GraphMemoryClient.send_message_with_history()
↓
OpenAI API / DeepSeek API
↓
execute_tool() → EmbeddedGraphDB
↓
循环调用 API 直到无 tool_calls
↓
queue.Queue → 返回结果
↓
MessageHistory 显示
```
## 启动流程
```python
# trulymem_entry.py
def main():
backend_server = BackendServer(db_path="graph_memory.db")
backend_server.start(api_key=config.api_key)
app = GraphMemoryApp(backend_server=backend_server)
app.run()
backend_server.shutdown()
```
## 布局结构
@ -118,34 +187,6 @@ TrulyMEM-TrueHumanMEM/
| F5 | 清屏 |
| F6 | 退出 |
## 组件职责
### 左侧区域
- **MessageHistory**: 消息历史容器
- **InputBox**: 底部输入框
### 右侧区域
- **RightPanel**: 侧边栏容器(宽度 70)
- **ConfigSection**: 配置区(API Key、模型选择、Base URL)
- **OperationLog**: 操作日志
- **CypherQueryBox**: 查询框(注:目前仅作展示,无 Cypher 查询功能)
## 数据流
```
用户输入 → InputBox → app.on_input_box_send_message
↓
GraphMemoryClient.send_message_with_history()
↓
OpenAI API / DeepSeek API
↓
检查 tool_calls → execute_tool() → EmbeddedGraphDB
↓
循环调用 API 直到无 tool_calls
↓
最终回复 → MessageHistory + OperationLog
```
## 技术栈
| 技术 | 用途 |
@ -154,17 +195,18 @@ OpenAI API / DeepSeek API
| Textual 0.47+ | TUI 框架 |
| SQLite | 图数据库(默认内嵌) |
| OpenAI SDK | API 调用(兼容 DeepSeek) |
| Neo4j | 可选数据库(需 Docker) |
| threading.Queue | 多线程通信 |
| PyInstaller | 打包 |
## 数据库模式
### 默认:SQLite 内嵌
### SQLite 内嵌(默认)
```python
# core/imports.py
if USE_EMBEDDED_DB:
from .embedded_db import EmbeddedGraphDB as Neo4jGraph
# core/embedded_db.py
class EmbeddedGraphDB:
def __init__(self, db_path="graph_memory.db"):
self.conn = sqlite3.connect(db_path, check_same_thread=False)
```
### 可选:Neo4j
@ -192,4 +234,4 @@ docker run -d --name neo4j -p 7474:7474 -p 7687:7687 neo4j:latest
- `task_create` - 创建任务
- `task_set_state` - 设置状态
- `task_delete` - 删除任务
- `task_link_info` - 关联信息
- `task_link_info` - 关联信息