mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-15 12:55:51 +00:00
* docs: 修复文档巡检发现的 4 处错误 - daemon.md: 反映实际实现状态(supervisor/worker 已实现而非 stub) - bridge-mode.md: API 操作数量从 7 修正为 9 - web-search-tool.md: 文件路径从 src/tools/ 修正为 packages/builtin-tools/src/tools/ - remote-control-self-hosting.md: 补充缺失的 RCS_WS_IDLE_TIMEOUT 和 RCS_WS_KEEPALIVE_INTERVAL 配置项 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 Safety 和 Context 文档中的代码引用和类型错误 - permission-model: 修正规则来源从"五层"到八层,优先级顺序对齐代码 - permission-model: PermissionUpdate 类型改为实际的 addRules/replaceRules 等 - permission-model: 补充 acceptEdits 和 dontAsk 两种权限模式 - permission-model: DENIAL_LIMITS 字段名对齐实际代码 - plan-mode: 工具路径从 src/tools/ 改为 packages/builtin-tools/src/tools/ - compaction: 修正 COMPACTABLE_TOOLS 和 POST_COMPACT_* 的行号 - project-memory: 修正 ENTRYPOINT_NAME 常量的行号 - system-prompt: 修正 SystemPrompt 类型定义文件路径和多个行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复 introduction 文档中的错误路径和行号引用 - why-this-whitepaper.mdx: BashTool 路径从 src/tools/ 修正为 packages/builtin-tools/src/tools/ - what-is-claude-code.mdx: 移除不存在的 Azure provider,改为实际的 7 种 provider - architecture-overview.mdx: State 类型行号从 204 修正为 207 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复 conversation/features 文档中的错误 - streaming.mdx: queryStreamRaw → queryModelWithStreaming 函数名修正 - streaming.mdx: Azure 提供商不存在,替换为实际 7 个提供商 - debug-mode.mdx: --inspect-wait 描述错误,实际使用 BUN_INSPECT 环境变量 - buddy.mdx: 补充缺失的 companionReact.ts、CompanionCard.tsx、index.ts Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复文档巡检中的源码引用错误 - feature-flags.mdx: 修正 feature() 兜底描述,实际从 bun:bundle 导入而非 cli.tsx:3 内联 - feature-flags.mdx: 修正工具 require 路径为 @claude-code-best/builtin-tools 包路径 - ant-only-world.mdx: 修正 tools.ts 中 require 路径为包路径 - ant-only-world.mdx: 修正 INTERNAL_ONLY_COMMANDS 行号 (267-295) 和数量 (24+) - skills.mdx: 修正 COMMANDS memoize 行号 258 → 299 - mcp-protocol.mdx: 修正 fetchToolsForClient LRU 缓存上限 20 → 100 - streaming.mdx: 修正流式事件引用 - file-operations.mdx: 修正工具路径引用 - search-and-navigation.mdx: 修正搜索工具引用 - shell-execution.mdx: 修正 shell 工具引用 - buddy.mdx: 补充缺失的 frontmatter 字段 - debug-mode.mdx: 修正调试模式描述 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 tools/agent 文档中的文件路径和行号引用 - 修正 TodoWriteTool、AgentTool、ToolSearchTool 等工具路径 src/tools/ → packages/builtin-tools/src/tools/ - 更新 Tool.ts、tools.ts、BashTool.tsx 中过时的行号引用 - 修正 WebSearchTool/WebFetchTool/EnterWorktreeTool/ExitWorktreeTool 路径 - 修正 AgentTool.tsx 中多行行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature 文档中的文件路径和行号引用 - ultraplan.md: 更新文件行数(525/349/127) - fork-subagent.md: 路径迁移 src/tools/ → packages/builtin-tools/ - mcp-skills.md: 修正 getMcpSkillCommands 行号 547→604,client.ts 行号 117→129 - kairos.md: 修正 getBriefSection/getProactiveSection 行号 - proactive.md: 修正 getProactiveSection 行号 860→864 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正顶层文档中的路径迁移和行号引用 - auto-updater.md: config.ts 行号 1735→1737,标注未接入启动流程的函数 - external-dependencies.md: WebSearchTool/WebFetchTool 路径迁移到 builtin-tools 包,Vertex 行号修正 - lsp-integration.md: LSPTool 路径从 src/tools/ 迁移到 packages/builtin-tools/ - stub-recovery-design-1-4.md: 修正 Windows 绝对路径链接为标准代码引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 task 文档中的文件扩展名和路径引用 - task-004: AssistantSessionChooser.ts → .tsx, assistant.ts → .tsx - task-003: cli.tsx 行号 249→272, markdownConfigLoader.ts 行号 29→35 - lan-pipes: SendMessageTool 路径迁移到 packages/builtin-tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 补充 computer-use-tools-reference 缺失的 Windows 工具 添加遗漏的 open_terminal 和 activate_window 两个 Windows 专属工具, 修正工具总数 37→39,Windows 工具数 10→12。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 audit/bash-classifier/token-budget/tree-sitter 文档 - feature-flags-audit: ScheduleCronTool 路径迁移、DAEMON 状态更新为 COMPLETE、assistant 文件标记已补全、UDS 标记已实现 - bash-classifier: BashPermissionRequest 文件路径修正、withRetry 行号移除 - token-budget: attachments.ts 行号范围修正 - tree-sitter-bash: bashPermissions.ts 路径迁移到 packages/builtin-tools Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 langfuse-monitoring AgentTool 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 bridgeApi 行号和 Tool.ts 行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 Safety/Extensibility 文档中的工具路径迁移和行号引用 - sandbox.mdx: shouldUseSandbox.ts 和 bashPermissions.ts 路径迁移至 packages/builtin-tools - why-safety-matters.mdx: bashPermissions.ts 路径迁移(3 处) - plan-mode.mdx: EnterPlanModeTool/prompt.ts 路径迁移 - auto-mode.mdx: Auto mode 指令行号 3464→3481 - hooks.mdx: AgentTool/runAgent.ts 路径迁移 - skills.mdx: SkillTool.ts 路径迁移 - custom-agents.mdx: Agent built-in 目录和 exploreAgent.ts 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 internals 文档引用计数和路径 - ant-only-world: USER_TYPE 引用计数 465→410+,工具路径迁移到 builtin-tools - growthbook-ab-testing: growthbook.ts 行数 1156→1258 - hidden-features: 语音模式状态更新(audio-napi 已恢复) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正工具文档中的行号引用 - sub-agents: AgentTool.call 入口行号 340→387 - shell-execution: ShellCommand onTimeout 行号 129→144 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature 文档中的状态、路径和计数 - all-features-guide: 修正 feature flag 启用范围(dev only vs dev+build) - tier3-stubs: 大量状态修正(stub→已实现),缩减过时条目 - workflow-scripts: 路径迁移到 builtin-tools,状态更新 - web-browser-tool: 工具状态缺失→已实现,路径迁移 - context-collapse: CtxInspectTool 状态缺失→已实现 - computer-use: 行号引用更新,平台分发描述修正 - computer-use-tools-reference: 工具数 39→38 - voice-mode: voiceModeEnabled 行数 55→54 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 更新 the-loop 查询循环行号引用 query.ts 代码变更后终止原因行号整体偏移约 40 行 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 补充 feature-flags-audit 完整 build 默认 feature 列表 添加 ULTRATHINK/LODESTONE/ACP/DAEMON 等 19 个缺失的 build 默认 feature, 修正 dev-only 特征标注(UDS_INBOX/LAN_PIPES/BG_SESSIONS/TEMPLATES) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit ConfigTool 路径迁移 ConfigTool 路径从 src/tools/ 迁移到 packages/builtin-tools/src/tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit BashTool 路径迁移 BashTool 路径从 src/tools/ 迁移到 packages/builtin-tools/src/tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit SkillTool 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 更新 feature-flags-audit WorkflowTool 状态为已实现 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
7.5 KiB
7.5 KiB
KAIROS — 常驻助手模式
Feature Flag:
FEATURE_KAIROS=1(及子 Feature) 实现状态:核心框架完整,部分子模块为 stub;proactive/sleep 节奏控制已可用 引用数:154(全库最大)
一、功能概述
KAIROS 将 Claude Code CLI 从"问答工具"转变为"常驻助手"。开启后,CLI 持续运行在后台,支持:
- 持久化 bridge 会话:跨终端重启复用 session,通过 Anthropic OAuth 连接 claude.ai
- 后台执行任务:用户离开终端时继续工作(配合 PROACTIVE feature)
- 推送通知到移动端:任务完成或需要输入时推送(配合
KAIROS_PUSH_NOTIFICATION) - 每日记忆日志:自动记录和回顾工作内容(配合
KAIROS_DREAM) - 外部频道消息接入:Slack/Discord/Telegram 消息转发到 CLI(配合
KAIROS_CHANNELS) - 结构化 Brief 输出:通过 BriefTool 输出结构化消息(配合
KAIROS_BRIEF)
子 Feature 依赖关系
KAIROS (主开关)
├── KAIROS_BRIEF (BriefTool, 结构化输出)
├── KAIROS_CHANNELS (外部频道消息)
├── KAIROS_PUSH_NOTIFICATION (移动端推送)
├── KAIROS_GITHUB_WEBHOOKS (GitHub PR webhook)
└── KAIROS_DREAM (记忆蒸馏)
注意:PROACTIVE 与 KAIROS 强绑定。所有代码检查都是 feature('PROACTIVE') || feature('KAIROS'),即 KAIROS 开启时自动获得 proactive 能力。
二、系统提示
KAIROS 在系统提示中注入两大段落:
2.1 Brief 段落 (getBriefSection)
文件:src/constants/prompts.ts:847-858
当 feature('KAIROS') || feature('KAIROS_BRIEF') 时注入。Brief 工具(SendUserMessage)的结构化消息输出指令。/brief toggle 和 --brief flag 只控制显示过滤,不影响模型行为。
2.2 Proactive/Autonomous Work 段落 (getProactiveSection)
文件:src/constants/prompts.ts:864-918
当 feature('PROACTIVE') || feature('KAIROS') 且 isProactiveActive() 时注入。核心行为指令:
- Tick 驱动:通过
<tick_tag>prompt 保持存活,每个 tick 包含用户当前本地时间 - 节奏控制:使用
SleepTool控制等待间隔(prompt cache 5 分钟过期) - 空操作时必须 Sleep:禁止输出 "still waiting" 类文本(浪费 turn 和 token)
- 偏向行动:读文件、搜索代码、修改文件、commit — 都不需询问
- 终端焦点感知:
terminalFocus字段指示用户是否在看终端- Unfocused → 高度自主行动
- Focused → 更协作,展示选择
三、实现架构
3.1 核心模块
| 模块 | 文件 | 状态 | 职责 |
|---|---|---|---|
| Assistant 入口 | src/assistant/index.ts |
Stub | isAssistantMode()、initializeAssistantTeam() |
| Session 发现 | src/assistant/sessionDiscovery.ts |
Stub | 发现可用 bridge session |
| Session 历史 | src/assistant/sessionHistory.ts |
Stub | 持久化 session 历史 |
| Gate 控制 | src/assistant/gate.ts |
Stub | GrowthBook 门控检查 |
| Session 选择器 | src/assistant/AssistantSessionChooser.ts |
Stub | UI 选择 session |
| BriefTool | src/tools/BriefTool/ |
Stub | 结构化消息输出工具 |
| Channel Notification | src/services/mcp/channelNotification.ts |
Stub | 外部频道消息接入 |
| Dream Task | src/components/tasks/src/tasks/DreamTask/ |
Stub | 记忆蒸馏任务 |
| Memory Directory | src/memdir/memdir.ts |
Stub | 记忆目录管理 |
3.2 SleepTool(与 Proactive 共享)
文件:src/tools/SleepTool/prompt.ts
SleepTool 是 KAIROS/Proactive 的节奏控制核心。工具描述让模型理解"休眠"概念:
- 工具名:
Sleep - 功能:等待指定时间后响应 tick prompt;若队列出现新工作或 proactive 被关闭,会提前唤醒
- 与
<tick_tag>配合实现心跳式自主工作 - 远程控制 surfaces 可通过
automation_state看到standby/sleeping两种状态
3.3 Bridge 集成
KAIROS 通过 Bridge Mode(src/bridge/)连接到 claude.ai 服务器:
claude.ai web/app
│
▼ (HTTPS long-poll)
┌──────────────────────┐
│ Bridge API Client │ src/bridge/bridgeApi.ts
│ (register/poll/ │
│ acknowledge) │
└──────────┬───────────┘
│
▼
┌──────────────────────┐
│ Session Runner │ src/bridge/sessionRunner.ts
│ (创建/恢复 REPL) │
└──────────┬───────────┘
│
▼
┌──────────────────────┐
│ REPL + Proactive │ Tick 驱动自主工作
│ Tick Loop │
└──────────────────────┘
3.4 数据流
用户从 claude.ai 发送消息
│
▼
Bridge pollForWork() 收到 WorkResponse
│
▼
acknowledgeWork() 确认接收
│
▼
sessionRunner 创建/恢复 REPL session
│
▼
用户消息注入到 REPL 对话
│
▼
模型处理 → 工具调用 → BriefTool 结构化输出
│
▼
结果通过 Bridge API 回传到 claude.ai
四、关键设计决策
- Tick 驱动而非事件驱动:模型通过 SleepTool 自行控制唤醒频率,而非外部事件推送。简化架构但增加 API 调用开销
- KAIROS ⊃ PROACTIVE:所有 proactive 检查都包含 KAIROS,无需同时开启两个 flag
- Brief 显示/行为分离:
/brieftoggle 只控制 UI 过滤,模型始终可以使用 BriefTool - Terminal Focus 感知:模型根据用户是否在看终端自动调节自主程度
- GrowthBook 门控:部分功能(如推送通知)即使 feature flag 开启还需要服务端 GrowthBook 开关
五、使用方式
# 最小启用(常驻助手 + Brief)
FEATURE_KAIROS=1 FEATURE_KAIROS_BRIEF=1 bun run dev
# 全功能启用
FEATURE_KAIROS=1 \
FEATURE_KAIROS_BRIEF=1 \
FEATURE_KAIROS_CHANNELS=1 \
FEATURE_KAIROS_PUSH_NOTIFICATION=1 \
FEATURE_KAIROS_GITHUB_WEBHOOKS=1 \
FEATURE_PROACTIVE=1 \
bun run dev
# 配合 Token Budget 使用
FEATURE_KAIROS=1 FEATURE_TOKEN_BUDGET=1 bun run dev
六、外部依赖
- Anthropic OAuth:必须使用 claude.ai 订阅登录(非 API key)
- GrowthBook:服务端特性门控(
tengu_ccr_bridge等) - Bridge API:
/v1/environments/bridge系列端点
七、文件索引
| 文件 | 行数 | 职责 |
|---|---|---|
src/assistant/index.ts |
9 | Assistant 模块入口(stub) |
src/assistant/gate.ts |
— | GrowthBook 门控(stub) |
src/assistant/sessionDiscovery.ts |
— | Session 发现(stub) |
src/assistant/sessionHistory.ts |
— | Session 历史(stub) |
src/assistant/AssistantSessionChooser.ts |
— | Session 选择 UI(stub) |
src/tools/BriefTool/ |
— | BriefTool 实现(stub) |
src/tools/SleepTool/prompt.ts |
~30 | SleepTool 工具提示 |
src/tools/SleepTool/SleepTool.ts |
~200 | 休眠/唤醒与 automation metadata |
src/services/mcp/channelNotification.ts |
5 | 频道消息接入(stub) |
src/memdir/memdir.ts |
— | 记忆目录管理(stub) |
src/constants/prompts.ts:557,847-918 |
72 | 系统提示注入 |
src/components/tasks/src/tasks/DreamTask/ |
3 | Dream 任务(stub) |
src/proactive/index.ts |
— | Proactive 核心(KAIROS 共享) |
src/utils/sessionState.ts |
— | 向 bridge/CCR 暴露 automation 状态 |