chat-history-db
chat-history 对话历史库
结构化对话历史存储 + 强召回,以 MCP 工具形式提供给 Agent。
存储:LanceDB(本地嵌入式,vector + 全文 + 元数据)
嵌入:bge-m3(ONNX,本地推理)
重排:bge-reranker-v2-m3(cross-encoder)
召回:混合检索(BM25 精确词 + 向量语义)→ RRF 融合 → 跨编码器重排
功能(MCP 工具)
工具 | 说明 |
| 存一条消息(会话/标题/时间/轮次自动,入库自动算向量) |
| 语义召回(混合检索 + 重排,可按时间/会话/角色过滤) |
| 最近消息,时间倒序 |
| 列出会话及条数( |
| 会话管理: |
recall / recent / list_sessions 的 source 支持:messages(默认,活跃表)、archive(归档表)。
recall / recent 的 session 支持:空=不限、current=当前会话、sess_xxx=指定、其它=按标题匹配(匹配不到返回 E_SESSIONNOTFOUND,重名返回 E_AMBIGUOUSTITLE)。
安装
pip install -r requirements.txt模型文件默认可放在 ./models/ 下,或用环境变量覆盖:
CHAT_HISTORY_MODEL_DIR(默认models/bge-m3-onnx)CHAT_HISTORY_RERANK_DIR(默认models/bge-reranker-v2-m3-onnx)
运行(MCP stdio)
python mcp_server.py作为 MCP server 通过 stdio 接入;同时在 127.0.0.1:17891 开一个本地 HTTP 端点供 hooks 复用本进程模型:
POST /remember:入库(复用 MCP 进程内已加载的模型)GET /health:存活探测
端口随 MCP 进程退出而关闭。可用 CHAT_HISTORY_PORT 改端口。
会话标题
title_dispatcher / title_cache / title_validate 读取宿主(ZCode)的会话标题,用于按标题解析会话与回显。
运维
启动自动清理:MCP 启动后按
chat.db/messages.lance/_versions的垃圾体积阈值(默认 10 MiB)自动回收旧版本、合并碎片并更新 FTS 索引。日志见~/.agent/hooks/chat_maintenance.log;CHAT_HISTORY_GC_MB调阈值,CHAT_HISTORY_GC=0关闭。生产库不在本目录:本仓库的
chat.db/是空库骨架(只有空的messages.lance),运行中的 MCP 用的是安装版目录C:\Users\Think\.agent\tools\chat-history\chat.db。tools/backup.py按当前工作目录解析库路径,在项目版目录直接跑会操作这个空库——治理/备份生产库请在安装版目录执行,或先设CHAT_HISTORY_DB。备份 / 恢复 / 手动治理:
python tools/backup.py {backup,list,restore,verify,vacuum,reindex},细节见docs/ops/BACKUP.md。restore与vacuum一样会拒绝在 MCP 运行中/队列有积压时执行(--force跳过)。测试:
python -m unittest discover -s tests。
文档
位置 | 内容 |
唯一权威的未决项 / 已完成清单——先看这个 | |
各项改造的设计与实施记录: | |
项目版 → 安装版的迁移计划与执行记录 | |
运维手册( | |
已归档的历史规格( |
说明
模型为外部依赖(体积大、含许可),源码另行分发;运行时通过环境变量指定模型目录。
数据文件
chat.db为 LanceDB 目录,首次写入自动创建。
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/ThinkofRain1213/chat-history-db'
If you have feedback or need assistance with the MCP directory API, please join our Discord server