Skip to content

feat(cli): add memory hook adapter#452

Open
soongao wants to merge 1 commit into
TencentCloud:mainfrom
soongao:feat/cli-memory-hooks
Open

feat(cli): add memory hook adapter#452
soongao wants to merge 1 commit into
TencentCloud:mainfrom
soongao:feat/cli-memory-hooks

Conversation

@soongao

@soongao soongao commented Jul 9, 2026

Copy link
Copy Markdown

概述

本 PR 新增一个独立的 Python CLI adapter 包:packages/tdai-memory-cli

它提供一组通用命令,让不同 agent runtime 可以通过命令行调用 TencentDB Agent Memory Gateway。CLI 本身不启动、不关闭、不 watch Gateway,只连接一个已经运行中的 TDAI Gateway,并通过 Gateway HTTP API 完成 recall、capture 和 session flush。

关联 Issue

Part of #235

提供的 CLI 能力

命令 Gateway API 能力
tdai-memory session-start GET /health 检查 Gateway 连通性
tdai-memory prefetch POST /recall 在用户输入进入模型前预取记忆上下文
tdai-memory sync-turn POST /capture 在一轮对话完成后捕获 user/assistant turn
tdai-memory end-session POST /session/end 结束 session 并触发 flush

同时提供一个可选的 stdin 事件适配入口,方便支持 command hook 的 agent runtime 将 hook event 转成 CLI 调用:

命令 能力
tdai-memory-hook session-start 从 stdin 读取 agent hook event,并调用 tdai-memory session-start
tdai-memory-hook prefetch 从 stdin 读取 agent hook event,并调用 tdai-memory prefetch
tdai-memory-hook sync-turn 从 stdin 读取 agent hook event,并调用 tdai-memory sync-turn
tdai-memory-hook end-session 从 stdin 读取 agent hook event,并调用 tdai-memory end-session

实现范围

本 PR 包含:

  • Python CLI package:tdai-memory-cli
  • Gateway HTTP client helper
  • stdin hook event parser
  • Codex / Claude Code hook JSON 示例,展示不同 agent runtime 如何注册命令
  • CLI 使用示例 skill 目录:examples/skills/tdai-memory/
  • session key 生成和配置读取逻辑
  • 单元测试和可选 e2e 测试入口

设计说明

CLI 默认假设 Gateway 已经由用户或部署环境启动。

整体调用链路是:

Agent runtime
  -> command registration / hook registration
  -> tdai-memory or tdai-memory-hook
  -> tdai-memory
  -> TDAI Gateway HTTP API
  -> TencentDB Agent Memory

不同 agent runtime 的 hook 注册方式并不相同,因此本 PR 只提供通用 CLI 命令和可选的 stdin 事件适配入口。Codex / Claude Code 的 hook JSON 放在 examples/ 下,仅作为注册方式示例。

这样可以让 Codex、Claude Code 或其他支持命令调用的 agent runtime 通过 CLI 接入 TencentDB Agent Memory,同时保持 Gateway 生命周期管理和 CLI adapter 解耦。

examples/skills/tdai-memory/ 是 CLI 的使用示例 skill 目录,入口文件是 SKILL.md,用于说明 agent 在什么场景下应该调用 tdai-memory / tdai-memory-hook。它不负责安装,也不修改 AGENTS.mdCLAUDE.md

Codex / Claude Code hooks 示例

本 PR 在 examples/ 下提供 Codex 和 Claude Code 的 hook JSON 示例,用于展示不同 agent runtime 如何把自身 hook event 接到通用 CLI 命令上。

示例文件 Runtime 说明
examples/codex/hooks.json Codex 展示 Codex hook 事件如何调用 tdai-memory-hook
examples/claude-code/hooks.json Claude Code 展示 Claude Code hook 事件如何调用 tdai-memory-hook
examples/hooks.json 通用示例 展示最小 command hook 配置形态

Codex 示例中的事件映射:

Codex hook CLI 调用 用途
SessionStart tdai-memory-hook session-start 会话开始、恢复或压缩后检查 Gateway 连通性
UserPromptSubmit tdai-memory-hook prefetch 用户输入进入模型前预取记忆上下文
Stop tdai-memory-hook sync-turn assistant 输出完成后捕获本轮对话

Claude Code 示例中的事件映射:

Claude Code hook CLI 调用 用途
SessionStart tdai-memory-hook session-start 会话开始时检查 Gateway 连通性
UserPromptSubmit tdai-memory-hook prefetch 用户输入进入模型前预取记忆上下文
Stop tdai-memory-hook sync-turn assistant 输出完成后捕获本轮对话

这些 JSON 只作为注册示例存在。不同 agent runtime 可以用自己的 hook 注册方式调用同一组 tdai-memory / tdai-memory-hook 命令。

自测结果

检查项 命令 结果
CLI unit tests cd packages/tdai-memory-cli && python3 -m pytest tests/unit -q 16 passed
Python compile check cd packages/tdai-memory-cli && python3 -m py_compile tdai_memory_cli/*.py passed

可选 e2e 测试需要本地已经运行 TDAI Gateway,因此不作为默认自测要求。

@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! 🚀

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.

2 participants