
Hindsight 集成 GitHub Copilot为 VS Code Agent 接入持久化长期记忆【免费下载链接】hindsightHindsight: Agent Memory That Learns项目地址: https://gitcode.com/GitHub_Trending/hindsight2/hindsight导读本文围绕 Hindsight 仓库中hindsight-integrations/github-copilot这一官方集成完整讲解如何让 GitHub CopilotVS Code的 Agent 模式通过 Hindsight MCP 服务器获得recall/retain/reflect记忆工具从安装初始化、mcp.json与copilot-instructions.md的自动接线原理到配置优先级、命令参数、自托管与 Cloud 两种接入方式再到源码级实现与测试验证。读完本文你可以在一分钟内让 Copilot 在任务开始时自动回忆相关项目记忆、并在工作中持久化沉淀事实。Hindsight 为 AI Agent 提供会学习的长期记忆层。hindsight-copilot是它的官方 GitHub Copilot 集成包一个纯配置型configuration-only的接线工具不介入任何运行时推理只负责把 Hindsight MCP 服务器写进 VS Code 的.vscode/mcp.json并在.github/copilot-instructions.md中写入一条先回忆、再沉淀的规则让 Copilot 的 Agent 模式自动使用 Hindsight 的记忆能力。完整源码位于 hindsight-integrations/github-copilot对应实现见 hindsight_copilot/。工作原理两个 VS Code Copilot 原生机制该集成依赖 VS Code Copilot 已支持的两项能力因此不需要任何桥接进程或代理MCP 服务器HTTP 类型VS Code Copilot 的 Agent 模式从.vscode/mcp.json的servers键读取 MCP 服务器配置且原生支持type: http的远程 HTTP MCP 端点并允许携带请求头。Hindsight MCP 端点因此可以直接连接例如{ servers: { hindsight: { type: http, url: https://api.hindsight.vectorize.io/mcp/my-project/, headers: { Authorization: Bearer hsk_... } } } }.github/copilot-instructions.mdCopilot 会把该文件作为工作区级别的项目说明应用到每一次聊天请求中。集成把回忆/沉淀规则写在这里从而让 Copilot 在 Agent 模式下自动使用记忆工具而无需用户每次手动调用。从源码看MCP 端点的拼装逻辑集中在 mcp_config.py 的mcp_endpoint_urldef mcp_endpoint_url(api_url: str, bank_id: str) - str: The Hindsight MCP endpoint for a bank (bank is the last path segment). return f{api_url.rstrip(/)}/mcp/{bank_id}/即端点为api_url/mcp/bank_id/bank_id 是端点 URL 的最后一个路径段这决定了 MCP 服务器只会操作你所指定的那个记忆银行memory bank。相应的服务器条目由build_http_server生成有 token 时输出headersAuthorization: Bearer token无 token开放的自托管服务器时则完全省略headers字段。安装与初始化pip install hindsight-copilot cd your-project hindsight-copilot init --api-token YOUR_HINDSIGHT_API_KEY --bank-id my-projectinit命令做两件事实现见 cli.py 的build_install把servers.hindsight条目合并进./.vscode/mcp.json文件不存在则创建把 recall/retain 规则写入./.github/copilot-instructions.md。之后只需重载 VS Code在 Copilot Chat 中切到Agent 模式从聊天的工具菜单中启动hindsightMCP 服务器即可。build_install是本集成的可测试核心test_cli.py 验证了它会同时写出 MCP 条目URL 与 Bearer 头正确以及带HINDSIGHT:BEGIN标记的规则文件。选择 Cloud 还是自托管Hindsight Cloud使用https://hindsight.vectorize.io生成的 API Key必须通过--api-token或环境变量 / 配置文件提供默认 API 地址为https://api.hindsight.vectorize.io自托管服务器追加--api-url http://localhost:8888指向本地开放服务此时无需 tokenbuild_http_server 会省略认证头。关于mcp.json含注释JSONC的特别处理VS Code 的.vscode/mcp.json允许 JSONC 注释而 Python 标准库 JSON 解析器无法保留注释往返。因此集成采取了绝不冒险动用户文件的策略见 mcp_config.py 的apply_to_mcp文件是严格 JSON → 原地合并created/merged/unchanged文件含注释无法按严格 JSON 解析→ 返回manual不改动原文件仅在终端打印一段可直接粘贴的servers片段。对应测试 test_mcp_config.py 明确断言JSONC 场景下文件内容保持原样且snippet中包含hindsight条目。任何时刻你也可以用hindsight-copilot init --print-only只打印配置片段而不写入任何文件。命令一览命令说明hindsight-copilot init写入 MCP 服务器条目 recall/retain 规则hindsight-copilot status显示 MCP 服务器与规则是否已配置hindsight-copilot uninstall移除 MCP 服务器条目与规则init的完整参数根据 cli.py 的参数定义init支持参数说明默认值--api-urlHindsight API 地址云地址https://api.hindsight.vectorize.io--api-tokenAPI tokenCloud 必填无--bank-idMCP 服务器对应的记忆银行copilot--print-only仅打印待粘贴的配置不写任何文件关闭--mcp-path.vscode/mcp.json路径./.vscode/mcp.json--instructions-pathcopilot-instructions.md路径./.github/copilot-instructions.md--version打印hindsight-copilot版本—status与uninstall同样支持--mcp-path/--instructions-path覆盖默认路径便于在非标准目录的仓库或测试场景使用。test_cli.py 的test_init_status_uninstall演示了init → status → uninstall的完整生命周期init 后服务器已安装status 输出installeduninstall 后服务器条目与规则文件均被移除。uninstall的清理行为remove_from_mcp只删除hindsight一个条目保留其他服务器与文件中的其余键如inputs若删除后servers为空则会一并移除空的servers键。规则清理见 instructions.py 的clear_rule仅剥离 Hindsight 的标记块保留用户自写内容若文件只剩标记块则整文件删除对应测试 test_instructions.py。JSONC 场景下同样走manual提示由用户手动移除。配置体系文件、环境变量与 CLI 的优先级集成支持三层配置解析逻辑在 config.py 的load_config中实现层级为内置默认值 →~/.hindsight/copilot.json→ 环境变量 → CLI 参数后者覆盖前者。配置项环境变量用户配置文件键默认值API 地址HINDSIGHT_API_URLhindsightApiUrlhttps://api.hindsight.vectorize.ioAPI tokenHINDSIGHT_API_TOKENhindsightApiToken无Cloud 必填Bank idHINDSIGHT_COPILOT_BANK_IDbankIdcopilot用户配置文件位于~/.hindsight/copilot.json常量USER_CONFIG_FILE见 config.pyinit首次运行时还会用当前解析结果自动脚手架出该文件见 cli.py 的_scaffold_user_config环境变量优先于文件load_config先读文件、再读环境变量逐项覆盖CLI 参数最优先_resolve_config在load_config之后用--api-url/--api-token/--bank-id再次覆盖见 cli.py容错配置文件损坏JSON 解析失败时静默回退到默认值不阻断安装test_config.py。写入的规则长什么样init写入.github/copilot-instructions.md的规则原文定义于 instructions.py 的RULE_TEXTYou have persistent long-term memory through the Hindsight MCP server (recall, retain, and reflect tools). - At the start of each task, call recall with the users request to load relevant decisions, preferences, and project context before you answer. Use whats relevant and ignore the rest. - When you learn a durable fact — an architectural decision, a user preference, a convention, or anything worth remembering across sessions — call retain to store it. - Do not mention these memory operations unless the user asks about them.三条规则对应三种行为任务开始先recall加载相关决策、偏好与项目上下文、遇到持久事实就retain架构决策、用户偏好、约定等跨会话值得记住的内容、除非用户询问否则不提及记忆操作。标记块不打扰用户内容的写入策略规则被包裹在!-- HINDSIGHT:BEGIN --…!-- HINDSIGHT:END --的 HTML 注释块中常量见 instructions.py这样集成可以精确替换或移除自己的内容而不干扰用户原有说明write_rule保留文件原有内容仅重写标记块并把块置于文件顶部让记忆规则引领说明instructions.py重复init不会产生重复块count(BEGIN_MARKER) 1见 test_instructions.py规则文本中recall、retain、reflect三个工具名均有测试断言覆盖test_instructions.py。端到端验证MCP 端点确实暴露记忆工具除确定性单元测试外集成还提供了门控的端到端测试 test_e2e.py用requires_real_llm标记隔离uv run pytest tests -v -m not requires_real_llm # 确定性测试套件 uv run pytest tests -v -m requires_real_llm # 门控的 MCP 端点检查e2e 测试会先探测api_url/health是否可达默认http://localhost:8888也可用HINDSIGHT_API_URL/HINDSIGHT_API_TOKEN覆盖然后通过urllib发起标准 MCP JSON-RPC 握手initialize→notifications/initialized→tools/list断言返回的tools/list中确实包含recall与retain——这从协议层验证了Copilot 在 Agent 模式下能拿到 Hindsight 记忆工具这一核心链路。小结hindsight-copilot的接入成本极低一条pip install、一条initCopilot 的 Agent 模式便自动获得跨会话的持久记忆能力。其实现哲学是纯配置接线——运行时记忆操作全部经由 Hindsight MCP 服务器完成本地工具只负责安全地生成配置与规则并通过 JSONC 探测、标记块、三层配置优先级等设计保证了不破坏用户既有配置。相关实现与测试均可直接在仓库中查阅包入口与 CLIhindsight_copilot/cli.py配置解析hindsight_copilot/config.pyMCP 配置读写hindsight_copilot/mcp_config.py规则写入hindsight_copilot/instructions.py测试tests/打包与入口点声明pyproject.toml【免费下载链接】hindsightHindsight: Agent Memory That Learns项目地址: https://gitcode.com/GitHub_Trending/hindsight2/hindsight创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考