|
| 1 | +//! Claude Code as an agent (spec 7.4, 9.2): `claude -p` with `stream-json` in |
| 2 | +//! both directions. |
| 3 | +
|
| 4 | +use std::ffi::OsString; |
| 5 | + |
| 6 | +use botloft_core::protocol::AgentKind; |
| 7 | +use bytes::Bytes; |
| 8 | +use serde_json::json; |
| 9 | + |
| 10 | +use super::{Agent, LaunchPlan, Turn}; |
| 11 | + |
| 12 | +/// Tools the bot uses without asking: its crew tools (spec 7.4). |
| 13 | +const ALLOWED_TOOLS: &str = "mcp__botloft"; |
| 14 | +/// Claude Code's own schedulers: they die with the process and Botloft never |
| 15 | +/// sees them. Work at set times is a routine (spec 7.4, 20). |
| 16 | +const DISALLOWED_TOOLS: &str = "CronCreate,CronDelete,CronList,ScheduleWakeup,RemoteTrigger"; |
| 17 | +/// Makes Claude Code load `CLAUDE.md` from `--add-dir` folders (spec 5). |
| 18 | +const ADDITIONAL_MEMORY: &str = "CLAUDE_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD"; |
| 19 | +/// Where permission requests go (spec 10.1). |
| 20 | +const PERMISSION_TOOL: &str = "mcp__botloft__permission_prompt"; |
| 21 | + |
| 22 | +pub struct ClaudeAgent; |
| 23 | + |
| 24 | +impl Agent for ClaudeAgent { |
| 25 | + fn kind(&self) -> AgentKind { |
| 26 | + AgentKind::Claude |
| 27 | + } |
| 28 | + |
| 29 | + fn args(&self, plan: &LaunchPlan<'_>) -> Vec<OsString> { |
| 30 | + let mut args: Vec<OsString> = [ |
| 31 | + "-p", |
| 32 | + "--input-format", |
| 33 | + "stream-json", |
| 34 | + "--output-format", |
| 35 | + "stream-json", |
| 36 | + "--verbose", |
| 37 | + "--include-partial-messages", |
| 38 | + "--replay-user-messages", |
| 39 | + ] |
| 40 | + .into_iter() |
| 41 | + .map(OsString::from) |
| 42 | + .collect(); |
| 43 | + args.push( |
| 44 | + if plan.resumed { |
| 45 | + "--resume" |
| 46 | + } else { |
| 47 | + "--session-id" |
| 48 | + } |
| 49 | + .into(), |
| 50 | + ); |
| 51 | + args.push(plan.session.into()); |
| 52 | + for arg in [ |
| 53 | + "--setting-sources", |
| 54 | + "project,local", |
| 55 | + "--strict-mcp-config", |
| 56 | + "--permission-mode", |
| 57 | + plan.permission_mode.cli_value(), |
| 58 | + "--permission-prompt-tool", |
| 59 | + PERMISSION_TOOL, |
| 60 | + "--allowedTools", |
| 61 | + ALLOWED_TOOLS, |
| 62 | + "--disallowedTools", |
| 63 | + DISALLOWED_TOOLS, |
| 64 | + "--mcp-config", |
| 65 | + ] { |
| 66 | + args.push(arg.into()); |
| 67 | + } |
| 68 | + args.push(plan.mcp_config.as_os_str().to_owned()); |
| 69 | + args.push("--add-dir".into()); |
| 70 | + args.push(plan.work_folder.as_os_str().to_owned()); |
| 71 | + // Without the flag, Claude Code uses the default of the owner's plan. |
| 72 | + if let Some(model) = plan.model.cli_value() { |
| 73 | + args.push("--model".into()); |
| 74 | + args.push(model.into()); |
| 75 | + } |
| 76 | + // Without the flag, Claude Code uses the level it sets for the model. |
| 77 | + if let Some(effort) = plan.effort.cli_value() { |
| 78 | + args.push("--effort".into()); |
| 79 | + args.push(effort.into()); |
| 80 | + } |
| 81 | + args |
| 82 | + } |
| 83 | + |
| 84 | + fn extra_env(&self) -> Vec<(OsString, OsString)> { |
| 85 | + // Loads the work folder's CLAUDE.md with the bot's memory. |
| 86 | + vec![(OsString::from(ADDITIONAL_MEMORY), OsString::from("1"))] |
| 87 | + } |
| 88 | + |
| 89 | + fn encode_turn(&self, uuid: &str, turn: &Turn) -> Bytes { |
| 90 | + let mut content = vec![json!({ "type": "text", "text": turn.text })]; |
| 91 | + content.extend(turn.images.iter().map(|image| { |
| 92 | + json!({ |
| 93 | + "type": "image", |
| 94 | + "source": { |
| 95 | + "type": "base64", |
| 96 | + "media_type": image.media_type, |
| 97 | + "data": image.data, |
| 98 | + }, |
| 99 | + }) |
| 100 | + })); |
| 101 | + let event = json!({ |
| 102 | + "type": "user", |
| 103 | + "uuid": uuid, |
| 104 | + "message": { "role": "user", "content": content }, |
| 105 | + }); |
| 106 | + let mut line = event.to_string().into_bytes(); |
| 107 | + line.push(b'\n'); |
| 108 | + Bytes::from(line) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +#[cfg(test)] |
| 113 | +mod tests { |
| 114 | + use std::path::Path; |
| 115 | + |
| 116 | + use botloft_core::protocol::{BotEffort, BotModel, PermissionMode}; |
| 117 | + |
| 118 | + use super::*; |
| 119 | + use crate::agent::TurnImage; |
| 120 | + |
| 121 | + fn plan(resumed: bool, model: BotModel, effort: BotEffort) -> LaunchPlan<'static> { |
| 122 | + LaunchPlan { |
| 123 | + session: "abc", |
| 124 | + resumed, |
| 125 | + mcp_config: Path::new("w/.botloft/mcp.json"), |
| 126 | + work_folder: Path::new("w/shared"), |
| 127 | + permission_mode: PermissionMode::Default, |
| 128 | + model, |
| 129 | + effort, |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + fn strings(args: Vec<OsString>) -> Vec<String> { |
| 134 | + args.into_iter() |
| 135 | + .map(|arg| arg.to_string_lossy().into_owned()) |
| 136 | + .collect() |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn a_new_bot_gets_a_session_id_and_the_defaults_leave_model_and_effort_out() { |
| 141 | + let args = strings(ClaudeAgent.args(&plan(false, BotModel::Default, BotEffort::Default))); |
| 142 | + let at = args.iter().position(|a| a == "--session-id").expect("flag"); |
| 143 | + assert_eq!(args[at + 1], "abc"); |
| 144 | + assert!(!args.contains(&"--resume".to_owned())); |
| 145 | + assert!(!args.contains(&"--model".to_owned())); |
| 146 | + assert!(!args.contains(&"--effort".to_owned())); |
| 147 | + assert!(args.contains(&"--permission-prompt-tool".to_owned())); |
| 148 | + } |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn a_resumed_bot_with_a_model_and_effort_passes_them() { |
| 152 | + let args = strings(ClaudeAgent.args(&plan(true, BotModel::Sonnet, BotEffort::High))); |
| 153 | + assert!(args.contains(&"--resume".to_owned())); |
| 154 | + let at = args.iter().position(|a| a == "--model").expect("model"); |
| 155 | + assert_eq!(args[at + 1], "sonnet"); |
| 156 | + let at = args.iter().position(|a| a == "--effort").expect("effort"); |
| 157 | + assert_eq!(args[at + 1], "high"); |
| 158 | + } |
| 159 | + |
| 160 | + #[test] |
| 161 | + fn a_turn_is_one_user_line_with_its_images() { |
| 162 | + let turn = Turn { |
| 163 | + text: "hi".into(), |
| 164 | + images: vec![TurnImage { |
| 165 | + media_type: "image/png".into(), |
| 166 | + data: "AAAA".into(), |
| 167 | + }], |
| 168 | + }; |
| 169 | + let line = ClaudeAgent.encode_turn("u-1", &turn); |
| 170 | + assert!(line.ends_with(b"\n")); |
| 171 | + let json: serde_json::Value = serde_json::from_slice(&line).expect("json"); |
| 172 | + assert_eq!(json["type"], "user"); |
| 173 | + assert_eq!(json["uuid"], "u-1"); |
| 174 | + assert_eq!(json["message"]["content"][0]["text"], "hi"); |
| 175 | + assert_eq!(json["message"]["content"][1]["source"]["data"], "AAAA"); |
| 176 | + } |
| 177 | +} |
0 commit comments