外部编程工具接入模式
CountBot 支持两种接入外部编程工具的模式:AI 模式(智能体编排)与直通模式(消息直接转发)。
两种模式对比
| 维度 | AI 模式 | 直通模式 |
|---|---|---|
| 消息处理 | 经 AgentLoop,模型可选择调用工具、读取文件、执行 Shell 命令 | 直接转发给外部编程工具 |
| 能力范围 | 完整:可读写文件、执行命令、调用技能、搜索记忆 | 受限:仅能调用已启用的编程工具 |
| 适用场景 | 需要模型自主决策的复杂任务 | 简单任务,或用户明确指定工具 |
AI 模式
工作流程
用户消息 → ChannelManager → AgentLoop → 模型推理 → 工具调用 → exec → 外部编程工具
在 AI 模式下:
- 消息进入消息队列,由 AgentLoop 处理
- 模型通过系统提示词了解编程工具的能力
- 模型自主决定何时调用
exec工具 exec工具启动外部编程工具的 CLI 进程- 执行结果返回给模型,模型继续推理
编程工具在 AI 模式下的角色
编程工具在 AI 模式下是 AgentLoop 可调用的工具之一。模型可以:
- 决定调用哪个编程工具
- 构造调用参数
- 根据输出结果调整策略
直通模式
工作流程
用户消息 → ChannelManager → 检测到直通模式 → 构建 ExternalAgentRequest → CLI 进程 → 返回输出
在直通模式下:
- 消息绕过 AgentLoop
- 系统直接构建
ExternalAgentRequest - 调用
CliExternalAgentAdapter.run() - 启动 CLI 进程并等待完成
- 将 stdout/stderr 返回给用户
启用直通模式的条件
- 渠道账号的路由模式设置为
direct - 该账号配置了
external_coding_profile - 消息到达时,系统检测到路由模式为
direct,直接执行编程工具
会话模式(直通模式特有)
直通模式下,系统需要决定携带多少历史消息给外部编程工具。conversation.py 中的会话模式逻辑:
def resolve_effective_session_mode(profile: ExternalAgentProfile) -> str:
"""返回真正生效的会话模式。"""
mode = normalize_session_mode(profile.session_mode)
if mode == "native" and not profile_supports_native_session(profile):
return "history"
return mode
def profile_supports_native_session(profile: ExternalAgentProfile) -> bool:
"""判断当前 profile 是否可稳定使用原生会话。"""
command_name = Path(profile.command or "").name.lower()
profile_name = profile.name.strip().lower()
return command_name == "claude" or profile_name == "claude"
三种会话模式:
| 模式 | 行为 | 源码实现 |
|---|---|---|
stateless | 只发当前任务,不携带历史 | build_history_prompt() 直接返回当前任务 |
history | 携带最近 N 条历史消息 | build_history_prompt() 拼接历史到 prompt |
native | 使用工具自身的会话能力 | cli.py:_build_argv() 为 claude 注入 --session-id |
native 模式的实际行为:
只有当 profile 的 command 或 name 为 claude 时,native 模式才会真正生效:
- 源码
cli.py:_build_argv()检查command_name == "claude"或profile.name == "claude" - 如果是 claude 且模式为
native,CLI 参数中自动注入--session-id <session_id> - 如果不是 claude,即使配置为
native,也会在resolve_effective_session_mode()中回退到history
默认工具配置:
| Profile | 默认 session_mode | 说明 |
|---|---|---|
claude | native | 支持 Claude Code 的内置会话 |
codex | history | 回退到历史模式 |
opencode | history | 回退到历史模式 |
配置外部编程工具
步骤
-
在设置页新增 profile
- 名称:如
claude、codex、opencode - 类型:当前仅支持
cli - 命令:如
claude、codex - 参数:支持
{prompt}、{working_dir}等占位符
- 名称:如
-
配置会话模式
stateless:不携带历史history:携带最近 N 条消息(默认 10,范围 1-50)native:仅 claude 支持
-
配置环境变量
inherit_env:从父进程继承的变量名,如ANTHROPIC_API_KEYenv:自定义环境变量
-
启用或禁用
- 默认新建的 profile 为禁用状态
- 需要在设置页启用