claw 输入流程

用户输入获取流程

两条入口路径

                   ┌─────────────────┐
                   │   main() / run()│
                   └────────┬────────┘
                            │ parse_args()
              ┌─────────────┴─────────────┐
              │                           │
     CliAction::Prompt             CliAction::Repl
     (一次性命令行模式)             (交互 REPL 模式)
              │                           │
              ▼                           ▼
     read_piped_stdin()           run_repl()  [line 4612]
              │                           │
              ▼                           ▼
   merge_prompt_with_stdin()    LineEditor::read_line()
              │                    每轮循环读取一行
              ▼                           │
     LiveCli::run_turn()    ◄─────────────┘
              │
              ▼
   ConversationRuntime::run_turn()

三层结构

第 1 层 — 物理输入

文件: input.rs

LineEditor::read_line() (line 140) 判断是否为 TTY:

  • TTY 模式:使用 rustyline 库提供完整的 readline 体验(行编辑、Tab 补全、语法高亮、Ctrl-C 取消 / Ctrl-D 退出)
  • 非 TTY 模式(管道):read_line_fallback() 直接用 io::stdin().read_line() 读取原始文本(line 182-197)

返回 ReadOutcome::Submit(String) / Cancel / Exit

管道 stdin(一次性模式):read_piped_stdin() (line 348) 检测到 stdin 不是 TTY 时,一次性 read_to_string 全部内容。

第 2 层 — CLI 命令分发

文件: main.rs

run_repl() (line 4612):交互循环

  1. editor.read_line() 获取一行输入
  2. 空行跳过;/exit / /quit 退出
  3. SlashCommand::parse() 解析内置斜杠命令(如 /help, /model, /compact
  4. try_resolve_bare_skill_prompt() 检测裸 skill 名称并自动转换为 /skills <input>
  5. 否则调用 cli.run_turn(&input) 发送给 LLM

merge_prompt_with_stdin() (line 368):将命令行 prompt 参数和管道 stdin 合并,用 \n\n 分隔。

第 3 层 — 会话引擎

文件: conversation.rs

ConversationRuntime::run_turn() (line 318):

  1. session.push_user_text(user_input) → 将用户输入写入会话消息列表
  2. 进入 agent 循环:
    • 构建 ApiRequest { system_prompt, messages } → 调用 api_client.stream()
    • 解析 assistant 消息 → 执行 tool use → 将结果写回 session
    • 循环直到没有 tool use
  3. 返回 TurnSummary

关键文件

文件 作用
input.rs 物理输入:rustyline readline / stdin 回退
main.rs:4612 REPL 循环:读输入 → 解析命令 → 调用 run_turn
main.rs:348 管道 stdin:一次性读取 + 合并 prompt
conversation.rs:318 Agent 循环:用户消息 → API 调用 → 工具执行
posted @ 2026-05-28 16:52  青山見我  阅读(45)  评论(0)    收藏  举报