feat: 添加 context_rewrite 工具并清理工具限制器死配置

- 新增 context_rewrite 工具:允许 AI 在单轮内压缩工具调用上下文
- 清理 persona_query_max 和 task_query_max 死配置(commit 9be60ba 引入)
- 同步全栈:后端/前端/文档/测试 18 个文件
- 测试:70/70 通过
This commit is contained in:
root
2026-04-16 14:33:21 +08:00
parent 535fca933a
commit 2aa6e9240f
19 changed files with 431 additions and 129 deletions

View File

@ -472,12 +472,11 @@ asyncio.run(main())
| Category | Operation | Per-Turn Limit |
|----------|-----------|---------------|
| Persona graph | Query | 1 time |
| Persona graph | Modify | 1 time |
| Working memory chain | Query | 4 times |
| Working memory chain | Modify | 2 times |
| Working memory chain | Modify | 5 times |
| General memory | Query | 20 times |
| General memory | Modify | 10 times |
| Context compression | Query | Counted as general memory query |
### Reset Mechanism

View File

@ -32,7 +32,7 @@ TrulyMEM-TrueHumanMEM/
│ ├── services/ # Service layer (config only)
│ ├── handlers/ # Event handlers
│ └── styles/ # Style files
└── tests/ # Tests (42 tests)
└── tests/ # Tests (54 tests)
```
## Architecture Diagram
@ -146,13 +146,14 @@ def main():
## Tool System
### Memory Tools (6)
### Memory Tools (7)
- `memory_recall` - Retrieve memory
- `memory_commit` - Write memory
- `memory_purge` - Delete memory
- `memory_introspect` - View status
- `memory_archive` - Archive memory
- `memory_cleanup` - Clean data
- `context_rewrite` - Compress single-turn tool call context (experimental)
### Persona Tools (2)
- `persona_update` - Update persona
@ -166,6 +167,19 @@ def main():
---
## Tool Call Limits
| Category | Operation | Per-Turn Limit |
|----------|-----------|---------------|
| Persona graph | Modify | 1 time |
| Working memory chain | Modify | 5 times |
| General memory | Query | 20 times |
| General memory | Modify | 10 times |
> Note: `memory_recall` is uniformly counted as general memory query, no longer distinguished by persona/working memory queries.
---
## Error Handling Principle
All APIs **do not throw exceptions**, errors are passed via return dictionary:

View File

@ -25,6 +25,14 @@ All memory must be written to the graph database:
All memory must be read from:
- `memory_recall` - Retrieve memory
### Working Memory Management (Experimental)
`context_rewrite` allows AI to proactively compress tool call context within a single turn:
- Distills verbose JSON tool results into concise natural language summaries
- Summary must include which tools were called and how many calls are summarized
- After system validates the format, replaces `messages_history` with `[user message, summary]`
- Ensures LLM retains meta-cognition (knows "I called tools") while reducing JSON noise
---
## Mandatory Execution Flow (Per Turn)

View File

@ -478,12 +478,11 @@ asyncio.run(main())
| 类别 | 操作 | 每轮上限 |
|------|------|---------|
| 人设图 | 查询 | 1 次 |
| 人设图 | 修改 | 1 次 |
| 工作记忆链 | 查询 | 4 次 |
| 工作记忆链 | 修改 | 2 次 |
| 工作记忆链 | 修改 | 5 次 |
| 一般记忆 | 查询 | 20 次 |
| 一般记忆 | 修改 | 10 次 |
| 上下文压缩 | 查询 | 计入一般记忆查询 |
### 重置机制

View File

@ -32,7 +32,7 @@ TrulyMEM-TrueHumanMEM/
│ ├── services/ # 服务层(仅配置管理)
│ ├── handlers/ # 事件处理
│ └── styles/ # 样式文件
└── tests/ # 测试 (42 tests)
└── tests/ # 测试 (54 tests)
```
## 架构图
@ -146,13 +146,14 @@ def main():
## 工具系统
### 记忆工具 (6个)
### 记忆工具 (7个)
- `memory_recall` - 检索记忆
- `memory_commit` - 写入记忆
- `memory_purge` - 删除记忆
- `memory_introspect` - 查看状态
- `memory_archive` - 归档记忆
- `memory_cleanup` - 清理数据
- `context_rewrite` - 压缩单轮工具调用上下文(实验性)
### 人设工具 (2个)
- `persona_update` - 更新人设
@ -166,6 +167,19 @@ def main():
---
## 工具调用限制
| 类别 | 操作 | 每轮上限 |
|------|------|---------|
| 人设图 | 修改 | 1 次 |
| 工作记忆链 | 修改 | 5 次 |
| 一般记忆 | 查询 | 20 次 |
| 一般记忆 | 修改 | 10 次 |
> 注:`memory_recall` 统一计入一般记忆查询,不再区分人设/工作记忆查询。
---
## 错误处理原则
所有 API **不抛出异常**,错误通过返回字典传递:

View File

@ -25,6 +25,14 @@ TrulyMEM 的解决思路:
所有记忆必须通过以下方式读取:
- `memory_recall` - 检索记忆
### 工作记忆管理(实验性)
`context_rewrite` 允许 AI 在单轮对话内主动压缩工具调用的临时上下文:
- 将冗长的 JSON 工具结果提炼为简洁的自然语言摘要
- 摘要必须包含调用了哪些工具、对几次调用的总结
- 系统验证格式后,替换 `messages_history``[用户消息, 摘要]`
- 确保 LLM 保留元认知(知道"我调用过工具"),同时减少 JSON 噪音
---
## 强制执行流程(每轮对话)