feat: 贯通 context_window 配置并按 token 预算裁剪对话历史

This commit is contained in:
2026-09-20 14:22:53 +08:00
parent 8251a57010
commit 4114b0b4fb
21 changed files with 307 additions and 42 deletions
+10 -1
View File
@@ -137,7 +137,15 @@ apps/agent/
索引:`(company_id, user_id, thread_id, seq)`
**产品真相源 = 本表**;打开会话只加载最近 N 条(或按 token 窗口),更早依赖 `sessions.summary`。
**产品真相源 = 本表**;组装模型上下文时:Mongo 多拉若干条 → **按 token 预算从旧到新裁剪**(非固定 40 条);更早内容待 `sessions.summary`(未落地)。
上下文窗口来源(多数 OpenAI Compatible **不会**在 API 返回 context 大小):
1. 租户配置 `context_window`(设置页可填)
2. 否则按 `model` 名启发式(deepseek/gpt-4o/claude 等)
3. 再否则默认 `128000`
预算:system + 本轮 user + 预留输出/tool 后,剩余给历史原文。
#### `agent_user_memory`
@@ -172,6 +180,7 @@ apps/agent/
|------|------|
| `company_id` | 唯一 |
| `base_url` / `model` | OpenAI Compatible |
| `context_window` | 可选;模型上下文 token 上限,空则 Agent 按模型名估计 |
| `api_key_encrypted` / `api_key_suffix` | 密文 + 掩码后缀 |
| `extra_headers_json` | 可选 |