Skip to content

feat(adapter): 新增 Claude Code 记忆适配#477

Open
shsaihdsaiudh wants to merge 2 commits into
TencentCloud:mainfrom
shsaihdsaiudh:codex/issue-235-coding-agent-adapter
Open

feat(adapter): 新增 Claude Code 记忆适配#477
shsaihdsaiudh wants to merge 2 commits into
TencentCloud:mainfrom
shsaihdsaiudh:codex/issue-235-coding-agent-adapter

Conversation

@shsaihdsaiudh

@shsaihdsaiudh shsaihdsaiudh commented Jul 12, 2026

Copy link
Copy Markdown

当前状态

本 PR 是 issue #235 的 Claude Code 平台适配增量,复用现有 TDAI Gateway 和 TdaiCore,不新增第二套记忆引擎。

当前实现包含通用 Gateway 客户端、Claude Code Hook、可执行命令、完整 recall 响应以及 Hermes 配置修正。PR #316 已被 maintainer 作为共享 Gateway Client 候选基线,PR #372 是 MCP 与 recall response 候选基线;如果它们先合并,本 PR 可以在其上 rebase,并移除重叠的通用层代码,只保留 Claude Code 特有增量。

Refs #235#316#372

要解决的问题

当前 main 已支持 OpenClaw 和 Hermes,但 Claude Code 还没有可直接安装的生命周期适配层,主要缺口包括:

  • UserPromptSubmit 无法在模型调用前执行记忆召回。
  • Stop 只读取异步落盘的 transcript 时,可能漏掉当前回复,或者把 tool_result 错当成用户输入。
  • SessionEnd 缺少可靠的 session flush,重复执行 capture 还会写入两次最后一轮。
  • Gateway 的 legacy context 只包含稳定上下文,L1 动态召回结果没有通过 HTTP 完整暴露。
  • Hook 没有独立构建和 npm bin,用户只能从源码目录通过 tsx 启动。
  • Hermes Supervisor 使用的 MEMORY_TENCENTDB_* 与 Node Gateway 实际读取的 TDAI_* 环境变量没有完整对齐。

本 PR 的方案

新增基于 TDAI Gateway 的 Claude Code Hook 适配器,并保持平台层尽可能薄:

  • UserPromptSubmit 调用 /recall,把 L1 动态记忆与 Persona/Scene 稳定上下文注入 additionalContext
  • Stop 使用 Claude Code 提供的 prompt_idlast_assistant_message 精确配对当前回合。
  • transcript 解析过滤 tool_result、meta 和 sidechain 行,避免把工具输出写成用户记忆。
  • SessionEnd 只调用 /session/end,不重复 capture。
  • 透传 transcript 原始时间戳和 started_at,复用核心原子 checkpoint 处理 Hook 重试。
  • 所有 Gateway 失败均 fail-open,只写 stderr,不阻塞 Claude Code。

具体改动

  • src/adapters/coding-agent/:补充 Gateway HTTP 客户端、超时、鉴权、搜索和 session 生命周期映射。
  • src/adapters/claude-code/:新增 Hook handler、stdio CLI 和回归测试。
  • src/gateway/recall-response.ts:分别返回 prepend_contextappend_system_context,同时保留 legacy context
  • package.json / tsdown.config.ts:发布 memory-tencentdb-claude-hook 可执行命令,并把适配文档纳入 npm 包。
  • hermes-plugin/memory/memory_tencentdb/:对齐 Gateway host、port、LLM 环境变量,并消费完整 recall 上下文。
  • docs/README.mdREADME_CN.md:新增 Claude Code、Hermes、通用 Coding Agent 和跨平台对比文档入口。

与现有 PR 的关系

  • PR feat(adapters): add gateway client adapter kit #316:覆盖共享 Gateway Client / 生命周期 helper。本 PR 中的通用客户端与其存在重叠,后续应优先复用最终合并的基线。
  • PR feat(adapter): add Codex MCP gateway bridge #372:覆盖 MCP stdio 和完整 recall response。本 PR 对齐其动态/稳定上下文字段,但 Claude Code Hook 本身不是 MCP Server。
  • 本 PR 的独特增量是 Claude Code Hook 生命周期映射、transcript 安全解析、可执行打包、SessionEnd flush 和对应文档。

非目标

  • 不在本 PR 中实现 Dify 或其他平台插件。
  • 不复制 TdaiCore 的存储、排序、抽取或 L0-L3 pipeline。
  • 不声称 Hook 模式已经向 Claude Code 注册主动搜索工具;自动 recall/capture 与主动 MCP 搜索保持独立。

验证

npm test
npm run build
npm pack --dry-run --json
git diff --check

本地验证结果:

  • TypeScript:7 个测试文件、85 项测试全部通过。
  • Hermes recovery suite:隔离 pytest 环境中 20 项测试通过。
  • Claude Hook stdio、Bearer 鉴权和 recall 冒烟测试通过。
  • npm pack 包含可执行 Hook 和 4 份适配文档,且不包含 Python bytecode。
  • 完整构建和 git diff --check 通过。

当前 PR 没有 GitHub status check。

Assisted-by: Codex:GPT-5

@shsaihdsaiudh shsaihdsaiudh changed the title feat(adapter): add Claude Code memory integration feat(adapter): 新增 Claude Code 记忆适配 Jul 12, 2026
@Maxwell-Code07

Copy link
Copy Markdown
Collaborator

Thank you for submitting this PR and participating in Tencent Rhino-bird Open-source Training Program!
We have successfully received your submission. The program is currently in full swing, and we will complete the Code Review for you as soon as possible. Please keep an eye on the status notifications for this PR so you can follow up promptly once the review feedback is provided.
Thanks again for your contribution and open-source spirit! 🚀

@YOMXXX

YOMXXX commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Scope cleanup triage for #235:

This PR has useful Claude Code hook material, but it currently bundles several layers that already have canonical or narrower PRs:

As written, this PR carries a new src/adapters/coding-agent Gateway client layer, recall-response changes, Hermes configuration changes, docs, packaging, and the Claude Code hook adapter in one branch. That makes it a competing #235 integration stack rather than a focused Claude Code follow-up.

Recommendation: do not review/merge this as-is. Rebase after #316/#372 are resolved and split the remaining value into a narrow Claude Code adapter PR: lifecycle hook mapping, transcript parsing/fallback, executable hook packaging, and focused Claude Code tests. Keep shared Gateway client and recall-response behavior on the approved base PRs, and keep Hermes-specific config recovery as a separate follow-up unless it is strictly required for the Claude Code hook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants