Read-only mirror of github.com/ondeinference/ed.
Mirrored from https://github.com/ondeinference/ed.git
Synced Nov 23, 01:00 UTC
| 1 | //! Claude's [`Profile`]: who the agent is, its prompt, and the workspace tools. |
| 2 | |
| 3 | use std::sync::Arc; |
| 4 | |
| 5 | use async_trait::async_trait; |
| 6 | use ed_acp::{ |
| 7 | AgentInfo, AuthProvider, DescribeCtx, LlmEnv, Profile, PromptCtx, ToolCtx, ToolOutcome, |
| 8 | Toolset, WorkspaceTools, |
| 9 | }; |
| 10 | use serde_json::Value; |
| 11 | |
| 12 | use crate::auth::{ClaudeAuth, DEFAULT_MODEL}; |
| 13 | |
| 14 | pub const INFO: AgentInfo = AgentInfo { |
| 15 | name: "claude", |
| 16 | display_name: "Claude", |
| 17 | version: env!("CARGO_PKG_VERSION"), |
| 18 | env: LlmEnv { |
| 19 | prefix: "ED_CLAUDE", |
| 20 | dir_name: "edclaude", |
| 21 | default_model: DEFAULT_MODEL, |
| 22 | }, |
| 23 | }; |
| 24 | |
| 25 | const NO_SHELL_NOTE: &str = "\n\nThis host cannot run commands: there is no run_command tool, \ |
| 26 | and no git or shell. When something needs a command, say which one and let the user run it."; |
| 27 | |
| 28 | /// Claude as an `ed-acp` agent that uses the user's Anthropic API key. |
| 29 | #[derive(Debug, Clone)] |
| 30 | pub struct Claude { |
| 31 | shell: bool, |
| 32 | auth: ClaudeAuth, |
| 33 | } |
| 34 | |
| 35 | impl Claude { |
| 36 | /// With the workspace tools, `run_command` included. |
| 37 | pub fn new() -> Self { |
| 38 | Self { |
| 39 | shell: true, |
| 40 | auth: ClaudeAuth::new(), |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /// For hosts that cannot start processes (an iOS app, a sandboxed Mac app): the workspace |
| 45 | /// tools without `run_command`, and a prompt that says so. |
| 46 | pub fn without_shell() -> Self { |
| 47 | Self { |
| 48 | shell: false, |
| 49 | auth: ClaudeAuth::new(), |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /// Use `auth` instead of the default, e.g. one pointed at another endpoint. |
| 54 | pub fn with_auth(mut self, auth: ClaudeAuth) -> Self { |
| 55 | self.auth = auth; |
| 56 | self |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl Default for Claude { |
| 61 | fn default() -> Self { |
| 62 | Self::new() |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /// `WorkspaceTools` minus `run_command`. |
| 67 | struct FileTools; |
| 68 | |
| 69 | #[async_trait] |
| 70 | impl Toolset for FileTools { |
| 71 | fn definitions(&self) -> Vec<Value> { |
| 72 | WorkspaceTools |
| 73 | .definitions() |
| 74 | .into_iter() |
| 75 | .filter(|d| d["function"]["name"] != "run_command") |
| 76 | .collect() |
| 77 | } |
| 78 | |
| 79 | fn handles(&self, name: &str) -> bool { |
| 80 | name != "run_command" && WorkspaceTools.handles(name) |
| 81 | } |
| 82 | |
| 83 | fn describe( |
| 84 | &self, |
| 85 | name: &str, |
| 86 | args: &Value, |
| 87 | ctx: &DescribeCtx<'_>, |
| 88 | ) -> ( |
| 89 | String, |
| 90 | agent_client_protocol::schema::v1::ToolKind, |
| 91 | Vec<agent_client_protocol::schema::v1::ToolCallLocation>, |
| 92 | ) { |
| 93 | WorkspaceTools.describe(name, args, ctx) |
| 94 | } |
| 95 | |
| 96 | async fn execute( |
| 97 | &self, |
| 98 | ctx: &ToolCtx, |
| 99 | tool_call_id: &str, |
| 100 | name: &str, |
| 101 | args: Value, |
| 102 | ) -> anyhow::Result<ToolOutcome> { |
| 103 | if name == "run_command" { |
| 104 | anyhow::bail!("This host cannot run commands."); |
| 105 | } |
| 106 | WorkspaceTools.execute(ctx, tool_call_id, name, args).await |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #[async_trait] |
| 111 | impl Profile for Claude { |
| 112 | fn info(&self) -> AgentInfo { |
| 113 | INFO |
| 114 | } |
| 115 | |
| 116 | fn system_prompt(&self, ctx: &PromptCtx<'_>) -> String { |
| 117 | let tools = if self.shell { |
| 118 | "read_file, write_file, edit_file, list_directory and run_command" |
| 119 | } else { |
| 120 | "read_file, write_file, edit_file and list_directory" |
| 121 | }; |
| 122 | let mut prompt = format!( |
| 123 | "You are Claude, an autonomous coding agent working inside the user's editor.\n\ |
| 124 | Use your tools to inspect and change the project: read files before editing them, \ |
| 125 | prefer edit_file for small changes, and use write_file to create files. Your tools \ |
| 126 | are {tools}, plus those of any connected MCP servers. Keep answers concise and use \ |
| 127 | Markdown." |
| 128 | ); |
| 129 | if !self.shell { |
| 130 | prompt.push_str(NO_SHELL_NOTE); |
| 131 | } |
| 132 | prompt.push_str(&format!("\n\nWorking directory: {}", ctx.cwd.display())); |
| 133 | if ctx.roots.len() > 1 { |
| 134 | let roots: Vec<String> = ctx.roots.iter().map(|r| r.display().to_string()).collect(); |
| 135 | prompt.push_str(&format!("\nProject roots: {}", roots.join(", "))); |
| 136 | } |
| 137 | prompt |
| 138 | } |
| 139 | |
| 140 | fn auth(&self) -> Option<Arc<dyn AuthProvider>> { |
| 141 | Some(Arc::new(self.auth.clone())) |
| 142 | } |
| 143 | |
| 144 | fn toolsets(&self) -> Vec<Arc<dyn Toolset>> { |
| 145 | if self.shell { |
| 146 | vec![Arc::new(WorkspaceTools)] |
| 147 | } else { |
| 148 | vec![Arc::new(FileTools)] |
| 149 | } |
| 150 | } |
| 151 | } |