From 80fd2fbbf14e6115490fddbd023e3cee9a079b31 Mon Sep 17 00:00:00 2001 From: Jackson Oursland Date: Fri, 23 Oct 2026 13:43:41 +0000 Subject: [PATCH] Assistant: add Anthropic-compatible API agents (#5015) (#5017) * claim: #5015 Co-Authored-By: Claude Code * Assistant: add Anthropic-compatible API agents (#5015) The Add agent wizard now offers Anthropic-compatible endpoints beside OpenAI-compatible ones. The API backend speaks the Anthropic Messages protocol with the same MCP bridge, tool gate, limits, undo and keychain rules: x-api-key and anthropic-version, a top-level system note, input_schema tools, content-block answers and one user tool_result message per round. Test sends a short message because compatible endpoints (Qwen's) need not list models. Co-Authored-By: Claude Code --------- Co-authored-by: Claude Code --- assets/locales/en.json | 6 + assets/locales/ja.json | 6 + assets/locales/zh.json | 6 + crates/ui-egui/src/agent_chat/agents.rs | 94 ++- crates/ui-egui/src/agent_chat/api_client.rs | 630 +++++++++++++++++--- crates/ui-egui/src/agent_chat/mod.rs | 49 +- crates/ui-egui/src/agent_chat/settings.rs | 103 +++- crates/ui-egui/src/lib.rs | 2 +- docs/agent-chat.md | 25 +- docs/control-protocol.md | 2 +- docs/design/in-app-agent-chat.md | 20 +- 11 files changed, 791 insertions(+), 152 deletions(-) diff --git a/assets/locales/en.json b/assets/locales/en.json index 22bbb85e8..9cc32df7c 100644 --- a/assets/locales/en.json +++ b/assets/locales/en.json @@ -1553,8 +1553,10 @@ "ui.ai_add": "Add", "ui.ai_add_agent": "Add agent…", "ui.ai_add_agent_title": "Add agent", + "ui.ai_add_anthropic": "Anthropic-compatible API", "ui.ai_add_api": "OpenAI-compatible API", "ui.ai_add_custom": "Custom command", + "ui.ai_add_endpoint": "API endpoint", "ui.ai_add_installed": "Installed agent", "ui.ai_add_installed_note": "An entry of your own for an installed agent, for example with other arguments. Installed agents are listed by themselves.", "ui.ai_added": "added", @@ -1583,6 +1585,7 @@ "ui.ai_key_needs_https": "A key is only sent over HTTPS, or to this computer.", "ui.ai_key_note": "Stored in the system keychain, never in SolidCraft's settings. Leave it empty for a local server.", "ui.ai_keychain_error": "The system keychain refused: {error}", + "ui.ai_kind": "Kind", "ui.ai_mode_all": "Allow everything", "ui.ai_mode_all_note": "Nothing asks first. Crash reports stay off limits and the folder limits still apply.", "ui.ai_mode_all_warning": "The agent can save, overwrite, export and replace your design without asking. Use this only with an agent you trust, on designs you have saved.", @@ -1604,6 +1607,7 @@ "ui.ai_panel": "Panel", "ui.ai_permissions": "Permissions", "ui.ai_preset": "Preset", + "ui.ai_protocol": "Protocol", "ui.ai_remove": "Remove", "ui.ai_request_limit": "stopped after 24 requests for one message; send another message to go on", "ui.ai_reset": "Reset", @@ -1614,6 +1618,8 @@ "ui.ai_test": "Test", "ui.ai_test_acp_ok": "Answers: {agent}", "ui.ai_test_acp_tip": "Starts the agent briefly and asks it for a session; ends it again", + "ui.ai_test_answered": "Reached: {model} answered", + "ui.ai_test_anthropic_tip": "Sends a short message to the endpoint", "ui.ai_test_api_tip": "Asks the endpoint for its models", "ui.ai_test_no_model": "Reached: {count} models, but not {model}", "ui.ai_test_ok": "Reached: {count} models", diff --git a/assets/locales/ja.json b/assets/locales/ja.json index 62fcea403..ca0da91b7 100644 --- a/assets/locales/ja.json +++ b/assets/locales/ja.json @@ -1553,8 +1553,10 @@ "ui.ai_add": "追加", "ui.ai_add_agent": "エージェントを追加…", "ui.ai_add_agent_title": "エージェントを追加", + "ui.ai_add_anthropic": "Anthropic 互換 API", "ui.ai_add_api": "OpenAI 互換 API", "ui.ai_add_custom": "カスタムコマンド", + "ui.ai_add_endpoint": "API エンドポイント", "ui.ai_add_installed": "インストール済みエージェント", "ui.ai_add_installed_note": "インストール済みエージェントの独自エントリ(例: 別の引数で起動)。インストール済みのエージェントは自動で一覧に表示されます。", "ui.ai_added": "追加済み", @@ -1583,6 +1585,7 @@ "ui.ai_key_needs_https": "キーは HTTPS 経由、またはこのコンピューター宛てにのみ送信されます。", "ui.ai_key_note": "システムのキーチェーンに保存され、SolidCraft の設定には保存されません。ローカルサーバーの場合は空欄のままにします。", "ui.ai_keychain_error": "システムのキーチェーンが拒否しました: {error}", + "ui.ai_kind": "種類", "ui.ai_mode_all": "すべて許可", "ui.ai_mode_all_note": "確認なしで実行します。クラッシュレポートは引き続き対象外で、フォルダー制限も有効です。", "ui.ai_mode_all_warning": "エージェントは確認なしでデザインの保存・上書き・エクスポート・置き換えができます。信頼できるエージェントで、保存済みのデザインに対してのみ使ってください。", @@ -1604,6 +1607,7 @@ "ui.ai_panel": "パネル", "ui.ai_permissions": "権限", "ui.ai_preset": "プリセット", + "ui.ai_protocol": "プロトコル", "ui.ai_remove": "削除", "ui.ai_request_limit": "1メッセージあたり24リクエストで停止しました。続けるには、もう一度メッセージを送ってください", "ui.ai_reset": "リセット", @@ -1614,6 +1618,8 @@ "ui.ai_test": "テスト", "ui.ai_test_acp_ok": "応答あり: {agent}", "ui.ai_test_acp_tip": "エージェントを短時間起動してセッションを要求し、終了します", + "ui.ai_test_answered": "接続成功: {model} が応答しました", + "ui.ai_test_anthropic_tip": "エンドポイントに短いメッセージを送信します", "ui.ai_test_api_tip": "エンドポイントにモデル一覧を問い合わせます", "ui.ai_test_no_model": "接続成功: {count} 個のモデル({model} はありません)", "ui.ai_test_ok": "接続成功: {count} 個のモデル", diff --git a/assets/locales/zh.json b/assets/locales/zh.json index 9e134d106..678d56deb 100644 --- a/assets/locales/zh.json +++ b/assets/locales/zh.json @@ -1553,8 +1553,10 @@ "ui.ai_add": "添加", "ui.ai_add_agent": "添加智能体…", "ui.ai_add_agent_title": "添加智能体", + "ui.ai_add_anthropic": "Anthropic 兼容 API", "ui.ai_add_api": "OpenAI 兼容 API", "ui.ai_add_custom": "自定义命令", + "ui.ai_add_endpoint": "API 端点", "ui.ai_add_installed": "已安装的智能体", "ui.ai_add_installed_note": "为已安装的智能体添加一个自己的条目,例如使用其他参数。已安装的智能体会自动列出。", "ui.ai_added": "已添加", @@ -1583,6 +1585,7 @@ "ui.ai_key_needs_https": "密钥只会通过 HTTPS 发送,或发送到本机。", "ui.ai_key_note": "保存在系统钥匙串中,绝不保存在 SolidCraft 的设置里。本地服务器可留空。", "ui.ai_keychain_error": "系统钥匙串拒绝:{error}", + "ui.ai_kind": "类型", "ui.ai_mode_all": "全部允许", "ui.ai_mode_all_note": "不再询问。崩溃报告仍不可访问,文件夹限制仍然有效。", "ui.ai_mode_all_warning": "智能体可以不经询问保存、覆盖、导出和替换你的设计。只在你信任的智能体和已保存的设计上使用。", @@ -1604,6 +1607,7 @@ "ui.ai_panel": "面板", "ui.ai_permissions": "权限", "ui.ai_preset": "预设", + "ui.ai_protocol": "协议", "ui.ai_remove": "移除", "ui.ai_request_limit": "每条消息 24 次请求后已停止;再发送一条消息即可继续", "ui.ai_reset": "重置", @@ -1614,6 +1618,8 @@ "ui.ai_test": "测试", "ui.ai_test_acp_ok": "已响应:{agent}", "ui.ai_test_acp_tip": "短暂启动智能体并请求一个会话,然后结束它", + "ui.ai_test_answered": "已连接:{model} 已响应", + "ui.ai_test_anthropic_tip": "向端点发送一条简短消息", "ui.ai_test_api_tip": "向端点查询其模型列表", "ui.ai_test_no_model": "已连接:{count} 个模型,但没有 {model}", "ui.ai_test_ok": "已连接:{count} 个模型", diff --git a/crates/ui-egui/src/agent_chat/agents.rs b/crates/ui-egui/src/agent_chat/agents.rs index d2a65d198..1bcb01954 100644 --- a/crates/ui-egui/src/agent_chat/agents.rs +++ b/crates/ui-egui/src/agent_chat/agents.rs @@ -1,7 +1,8 @@ //! The agents the chat can use: the installed ones it finds by itself (Claude Code, Codex, //! Antigravity, OpenCode, …) and the ones the user adds (another ACP command, or an -//! OpenAI-compatible endpoint such as Ollama, LM Studio or OpenAI). Also the permission modes. -//! Preferences only: no design data, no keys (those are in the system keychain). +//! OpenAI-compatible endpoint such as Ollama, LM Studio or OpenAI, or an Anthropic-compatible +//! one). Also the permission modes. Preferences only: no design data, no keys (those are in the +//! system keychain). use serde::{Deserialize, Serialize}; @@ -47,6 +48,50 @@ pub enum Kind { Acp, /// An OpenAI-compatible chat completions endpoint. Api, + /// An Anthropic-compatible Messages endpoint. + Anthropic, +} + +impl Kind { + /// The id written in preferences and on the control channel. + pub fn id(self) -> &'static str { + match self { + Kind::Acp => "acp", + Kind::Api => "api", + Kind::Anthropic => "anthropic", + } + } + /// Whether the agent is an endpoint SolidCraft talks to itself (either protocol). + pub fn is_api(self) -> bool { + matches!(self, Kind::Api | Kind::Anthropic) + } + /// The wire protocol of an added endpoint; `None` for an ACP agent. + pub fn protocol(self) -> Option { + match self { + Kind::Acp => None, + Kind::Api => Some(Protocol::OpenAi), + Kind::Anthropic => Some(Protocol::Anthropic), + } + } +} + +/// The wire protocol of an endpoint the user added. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Protocol { + /// `{base}/chat/completions`, as Ollama, LM Studio, OpenAI and their kin speak. + #[default] + OpenAi, + /// `{base}/v1/messages`, as the Anthropic Messages API and compatible endpoints speak. + Anthropic, +} + +impl Protocol { + pub fn kind(self) -> Kind { + match self { + Protocol::OpenAi => Kind::Api, + Protocol::Anthropic => Kind::Anthropic, + } + } } /// An agent the user added. @@ -101,7 +146,7 @@ impl Entry { /// A short line under the name: the version, the model, or the command. pub fn detail(&self) -> String { match self.kind { - Kind::Api => format!("{} · {}", self.model, self.base_url), + Kind::Api | Kind::Anthropic => format!("{} · {}", self.model, self.base_url), Kind::Acp => match &self.version { Some(v) => v.clone(), None => super::panel::short_command(&self.argv.join(" ")), @@ -162,13 +207,31 @@ pub fn new_id(p: &crate::prefs::Prefs, prefix: &str) -> String { (1..).map(|n| format!("{prefix}-{n}")).find(|id| !p.agents.iter().any(|a| &a.id == id)).unwrap_or_else(|| prefix.to_string()) } -/// OpenAI-compatible endpoints people use: name, base URL, whether it needs a key. -pub const API_PRESETS: &[(&str, &str, bool)] = &[ - ("Ollama", "http://localhost:11434/v1", false), - ("LM Studio", "http://localhost:1234/v1", false), - ("OpenAI", "https://api.openai.com/v1", true), +/// An endpoint people commonly add, with the base URL to start from. +pub struct ApiPreset { + pub name: &'static str, + pub base_url: &'static str, +} + +/// OpenAI-compatible endpoints people use. +pub const OPENAI_PRESETS: &[ApiPreset] = &[ + ApiPreset { name: "Ollama", base_url: "http://localhost:11434/v1" }, + ApiPreset { name: "LM Studio", base_url: "http://localhost:1234/v1" }, + ApiPreset { name: "OpenAI", base_url: "https://api.openai.com/v1" }, ]; +/// Anthropic-compatible endpoints people use. The base URL is the one their documentation calls +/// `ANTHROPIC_BASE_URL`: the `/v1` is added where needed. +pub const ANTHROPIC_PRESETS: &[ApiPreset] = &[ApiPreset { name: "Anthropic", base_url: "https://api.anthropic.com/v1" }]; + +/// The presets offered for an endpoint protocol. +pub fn presets(protocol: Protocol) -> &'static [ApiPreset] { + match protocol { + Protocol::OpenAi => OPENAI_PRESETS, + Protocol::Anthropic => ANTHROPIC_PRESETS, + } +} + /// Whether a key may be sent to `url`: only over HTTPS, or to this computer. pub fn key_safe_for(url: &str) -> bool { let u = url.trim().to_ascii_lowercase(); @@ -231,4 +294,19 @@ mod tests { assert_eq!(normalize_url(" http://localhost:11434/v1/ ").as_deref(), Ok("http://localhost:11434/v1")); assert!(normalize_url("localhost:11434").is_err()); } + + #[test] + fn endpoint_kinds_keep_their_protocol_and_presets() { + assert_eq!(serde_json::to_string(&Kind::Api).unwrap(), "\"api\""); + assert_eq!(serde_json::to_string(&Kind::Anthropic).unwrap(), "\"anthropic\""); + assert_eq!(serde_json::from_str::("\"anthropic\"").unwrap(), Kind::Anthropic); + assert!(Kind::Anthropic.is_api() && !Kind::Acp.is_api()); + assert!(Kind::Acp.protocol().is_none()); + assert_eq!(Kind::Anthropic.protocol(), Some(Protocol::Anthropic)); + assert_eq!(Protocol::Anthropic.kind(), Kind::Anthropic); + assert_eq!(Kind::Api.id(), "api"); + assert_eq!(Kind::Anthropic.id(), "anthropic"); + assert!(presets(Protocol::OpenAi).iter().all(|p| !p.name.is_empty() && p.base_url.starts_with("http"))); + assert!(presets(Protocol::Anthropic).iter().any(|p| p.base_url == "https://api.anthropic.com/v1")); + } } diff --git a/crates/ui-egui/src/agent_chat/api_client.rs b/crates/ui-egui/src/agent_chat/api_client.rs index 3fb584c5e..c3d074027 100644 --- a/crates/ui-egui/src/agent_chat/api_client.rs +++ b/crates/ui-egui/src/agent_chat/api_client.rs @@ -1,9 +1,10 @@ -//! The API backend (desktop only): a model behind an OpenAI-compatible chat completions endpoint -//! (Ollama, LM Studio, OpenAI, any URL the user gives). SolidCraft runs the tool loop itself, with -//! the same tools the ACP agents get: it starts the same MCP server (`solidcraft-cli mcp -//! --connect` to the chat's own endpoint) and is its client, so every tool call passes the same -//! gate, limits and folder checks. The key, when there is one, comes from the system keychain for -//! this connection only. +//! The API backend (desktop only): a model behind an endpoint SolidCraft talks to itself, either +//! OpenAI-compatible (Ollama, LM Studio, OpenAI, any URL the user gives) or Anthropic-compatible +//! (the Anthropic Messages API, or an endpoint that speaks it). SolidCraft runs the tool loop +//! itself, with the same tools the ACP agents get: it starts the same MCP server (`solidcraft-cli +//! mcp --connect` to the chat's own endpoint) and is its client, so every tool call passes the +//! same gate, limits and folder checks. The key, when there is one, comes from the system keychain +//! for this connection only. //! //! The loop runs on a thread of its own: a model request can take a while. @@ -15,6 +16,7 @@ use std::time::Duration; use serde_json::{Value, json}; use super::acp::ModelInfo; +use super::agents::Protocol; use super::backend::{ChatBackend, Event, HttpFn, HttpRequest, PromptPart, StartContext, ToolStatus}; use super::process::{AgentProcess, FromAgent, ProcessHandle}; @@ -30,12 +32,21 @@ const REQUEST_TIMEOUT_S: u64 = 300; const TOOL_TIMEOUT: Duration = Duration::from_secs(330); /// How often a waiting request or tool call looks at the cancel flag. const CANCEL_POLL: Duration = Duration::from_millis(40); +/// The Anthropic Messages API requires `max_tokens`; it caps one answer, which is plenty for a +/// chat reply and for the tool calls in it. +const ANTHROPIC_MAX_TOKENS: u64 = 8192; +/// The API version sent to Anthropic-compatible endpoints. It is the version every such endpoint +/// knows: DeepSeek ignores it, Qwen does not require it, and the Claude API still accepts it. +const ANTHROPIC_VERSION: &str = "2023-06-01"; +/// The connection test's own timeout: it asks for a token or two, not a whole answer. +const TEST_TIMEOUT_S: u64 = 60; /// Tools the model is not offered. const HIDDEN_TOOLS: &[&str] = &["crash_reports"]; pub struct ApiConfig { pub name: String, + pub protocol: Protocol, pub base_url: String, pub model: String, pub key: Option, @@ -80,6 +91,7 @@ impl ApiBackend { mcp, next: 0, tools: Vec::new(), + tool_names: Vec::new(), history: Vec::new(), events: ev_tx, wake, @@ -141,8 +153,11 @@ struct Worker { cfg: ApiConfig, mcp: Option, next: u64, - /// The MCP tools as OpenAI function tools. + /// The MCP tools in the endpoint's own shape (function tools, or `input_schema` tools). tools: Vec, + /// The tools' names, whichever shape `tools` has. + tool_names: Vec, + /// The conversation in the endpoint's shape, without the system note (sent separately). history: Vec, events: Sender, wake: Arc, @@ -161,10 +176,9 @@ impl Worker { if let Err(e) = self.start_mcp() { self.emit(Event::Error(e)); } - self.history.push(json!({"role": "system", "content": super::acp::SYSTEM_NOTE})); self.emit(Event::Ready { agent: format!("{} · {}", self.cfg.name, self.cfg.model), images: true }); // The endpoint's models for the picker; an endpoint that does not list them shows none. - if let Ok(list) = model_list(&self.cfg.http, &self.cfg.base_url, self.cfg.key.as_deref()) { + if let Ok(list) = self.model_list() { let mut available = list; if !self.cfg.model.is_empty() && !available.iter().any(|m| m.id == self.cfg.model) { available.insert(0, ModelInfo { id: self.cfg.model.clone(), name: self.cfg.model.clone(), description: String::new() }); @@ -178,7 +192,7 @@ impl Worker { Cmd::Prompt(parts) => self.turn(parts), Cmd::SetModel(m) => self.cfg.model = m, Cmd::NewChat => { - self.history.truncate(1); + self.history.clear(); self.cancel.store(false, Ordering::Relaxed); self.emit(Event::Ready { agent: format!("{} · {}", self.cfg.name, self.cfg.model), images: true }); } @@ -189,6 +203,14 @@ impl Worker { } } + /// The endpoint's models, in the shape its protocol lists them. + fn model_list(&self) -> Result, String> { + match self.cfg.protocol { + Protocol::OpenAi => model_list(&self.cfg.http, &self.cfg.base_url, self.cfg.key.as_deref()), + Protocol::Anthropic => anthropic_model_list(&self.cfg.http, &self.cfg.base_url, self.cfg.key.as_deref()), + } + } + /// One MCP request to the server; its `result`. fn mcp_call(&mut self, method: &str, params: Value) -> Result { let Some(m) = self.mcp.as_ref() else { return Err("no SolidCraft tools in this session".into()) }; @@ -233,19 +255,17 @@ impl Worker { m.send(&json!({"jsonrpc": "2.0", "method": "notifications/initialized"})); } let list = self.mcp_call("tools/list", json!({}))?; - self.tools = openai_tools(&list); + self.tools = match self.cfg.protocol { + Protocol::OpenAi => openai_tools(&list), + Protocol::Anthropic => anthropic_tools(&list), + }; + self.tool_names = tool_names(&list); Ok(()) } fn turn(&mut self, parts: Vec) { - self.history.push(user_message(&parts)); - // Keep the system note; drop the oldest messages, never leaving a reply first. - while self.history.len() > MAX_HISTORY { - self.history.remove(1); - } - while self.history.len() > 2 && self.history.get(1).is_some_and(|m| m["role"] != "user") { - self.history.remove(1); - } + self.history.push(user_message(self.cfg.protocol, &parts)); + self.trim_history(); for _ in 0..MAX_REQUESTS_PER_TURN { // The history as of here is complete (every tool call answered): a cancelled request // goes back to it, so no half-finished round is left to confuse the next message. @@ -272,27 +292,21 @@ impl Worker { return; } }; - let msg = reply.pointer("/choices/0/message").cloned().unwrap_or(Value::Null); - if let Some(text) = msg.get("content").and_then(Value::as_str).filter(|t| !t.is_empty()) { - self.emit(Event::Text { thought: false, text: text.to_string() }); + for (thought, text) in reply.text { + self.emit(Event::Text { thought, text }); } - let calls: Vec = msg.get("tool_calls").and_then(Value::as_array).cloned().unwrap_or_default(); - let mut kept = json!({"role": "assistant", "content": msg.get("content").cloned().unwrap_or(Value::Null)}); - if !calls.is_empty() { - kept["tool_calls"] = Value::Array(calls.clone()); - } - self.history.push(kept); - if calls.is_empty() { + self.history.push(reply.assistant); + if reply.calls.is_empty() { self.emit(Event::TurnEnded { reason: String::new() }); return; } - for (i, c) in calls.iter().enumerate().take(32) { - let id = c.get("id").and_then(Value::as_str).map_or_else(|| format!("call-{i}"), str::to_string); - let name = c.pointer("/function/name").and_then(Value::as_str).unwrap_or("").to_string(); - let parsed = tool_arguments(c.pointer("/function/arguments")); - let args = parsed.clone().unwrap_or_else(|_| json!({})); - let title = tool_title(&name, &args); - let tool_id = format!("{id}-{}", self.next); + // Anthropic takes every result of a round in one user message; OpenAI takes one + // `tool` message per call. + let mut results = Vec::new(); + for c in reply.calls.iter().take(32) { + let args = c.arguments.clone().unwrap_or_else(|_| json!({})); + let title = tool_title(&c.name, &args); + let tool_id = format!("{}-{}", c.id, self.next); self.emit(Event::Tool { id: tool_id.clone(), title: Some(title), @@ -302,15 +316,13 @@ impl Worker { }); let (text, failed) = if self.cancel.load(Ordering::Relaxed) { ("cancelled by the user".to_string(), true) - } else if let Err(e) = &parsed { + } else if let Err(e) = &c.arguments { // A call whose arguments do not parse is answered, never run with `{}`. (format!("Not run: {e}"), true) - } else if HIDDEN_TOOLS.contains(&name.as_str()) - || !self.tools.iter().any(|t| t.pointer("/function/name").and_then(Value::as_str) == Some(name.as_str())) - { - (format!("unknown tool `{name}`"), true) + } else if !self.tool_names.iter().any(|n| n == &c.name) { + (format!("unknown tool `{}`", c.name), true) } else { - match self.mcp_call("tools/call", json!({"name": name, "arguments": args})) { + match self.mcp_call("tools/call", json!({"name": c.name, "arguments": args})) { Ok(r) => tool_text(&r), Err(e) => (e, true), } @@ -323,50 +335,103 @@ impl Worker { status: Some(if failed { ToolStatus::Failed } else { ToolStatus::Completed }), output: Some(shown), }); - self.history.push(json!({"role": "tool", "tool_call_id": id, "content": text})); + match self.cfg.protocol { + Protocol::OpenAi => self.history.push(json!({"role": "tool", "tool_call_id": c.id, "content": text})), + Protocol::Anthropic => { + results.push(json!({"type": "tool_result", "tool_use_id": c.id, "content": text, "is_error": failed})); + } + } + } + if self.cfg.protocol == Protocol::Anthropic && !results.is_empty() { + self.history.push(json!({"role": "user", "content": results})); } } self.emit(Event::Text { thought: false, text: format!("\n({})", crate::i18n::tr("ui.ai_request_limit")) }); self.emit(Event::TurnEnded { reason: "max_turn_requests".into() }); } - fn request(&self) -> Result { - let mut body = json!({"model": self.cfg.model, "messages": self.history}); - if !self.tools.is_empty() { - body["tools"] = Value::Array(self.tools.clone()); - if self.no_reasoning.get() { - body["reasoning_effort"] = json!("none"); - } + /// Keep the conversation bounded and starting at a user message, so neither protocol sees a + /// reply or a tool result without the message it answers. + fn trim_history(&mut self) { + while self.history.len() > MAX_HISTORY { + self.history.remove(0); } + while self.history.first().is_some_and(|m| !starts_a_turn(m)) { + self.history.remove(0); + } + } + + fn request(&self) -> Result { + let mut body = self.body(); let (status, v) = self.post(&body)?; // Some OpenAI models refuse function tools on this endpoint unless the reasoning effort is // "none" (HTTP 400 naming `reasoning_effort`): ask again once with it, and keep it for the - // next requests. - let refused = status == 400 && !self.tools.is_empty() && !self.no_reasoning.get() && http_error(status, &v).contains("reasoning_effort"); + // next requests. Anthropic has no such parameter. + let refused = self.cfg.protocol == Protocol::OpenAi + && status == 400 + && !self.tools.is_empty() + && !self.no_reasoning.get() + && http_error(status, &v).contains("reasoning_effort"); if refused { self.no_reasoning.set(true); body["reasoning_effort"] = json!("none"); let (status, v) = self.post(&body)?; - return if status == 200 { Ok(v) } else { Err(http_error(status, &v)) }; + return if status == 200 { Ok(reply(self.cfg.protocol, &v)) } else { Err(http_error(status, &v)) }; } if status != 200 { return Err(http_error(status, &v)); } - Ok(v) + Ok(reply(self.cfg.protocol, &v)) + } + + /// The request body in the endpoint's shape: the system note is a message for OpenAI and a + /// top-level field for Anthropic. + fn body(&self) -> Value { + match self.cfg.protocol { + Protocol::OpenAi => { + let mut messages = Vec::with_capacity(self.history.len() + 1); + messages.push(json!({"role": "system", "content": super::acp::SYSTEM_NOTE})); + messages.extend(self.history.iter().cloned()); + let mut body = json!({"model": self.cfg.model, "messages": messages}); + if !self.tools.is_empty() { + body["tools"] = Value::Array(self.tools.clone()); + if self.no_reasoning.get() { + body["reasoning_effort"] = json!("none"); + } + } + body + } + Protocol::Anthropic => { + let mut body = json!({ + "model": self.cfg.model, + "max_tokens": ANTHROPIC_MAX_TOKENS, + "system": super::acp::SYSTEM_NOTE, + "messages": &self.history, + }); + if !self.tools.is_empty() { + body["tools"] = Value::Array(self.tools.clone()); + } + body + } + } } fn post(&self, body: &Value) -> Result<(u16, Value), String> { let mut headers = vec![("Content-Type".to_string(), "application/json".to_string())]; - if let Some(k) = &self.cfg.key { - headers.push(("Authorization".into(), format!("Bearer {k}"))); - } - let req = HttpRequest { - method: "POST", - url: format!("{}/chat/completions", self.cfg.base_url), - headers, - body: Some(body.to_string().into_bytes()), - timeout_s: REQUEST_TIMEOUT_S, + let url = match self.cfg.protocol { + Protocol::OpenAi => format!("{}/chat/completions", self.cfg.base_url), + Protocol::Anthropic => { + headers.push(("anthropic-version".to_string(), ANTHROPIC_VERSION.to_string())); + endpoint(&self.cfg.base_url, "messages") + } }; + if let Some(k) = &self.cfg.key { + match self.cfg.protocol { + Protocol::OpenAi => headers.push(("Authorization".into(), format!("Bearer {k}"))), + Protocol::Anthropic => headers.push(("x-api-key".into(), k.clone())), + } + } + let req = HttpRequest { method: "POST", url, headers, body: Some(body.to_string().into_bytes()), timeout_s: REQUEST_TIMEOUT_S }; // The request runs on a thread of its own so that Stop does not wait for the model: a // cancelled one is abandoned (the host's own timeout ends it) and its answer dropped. let (tx, rx) = channel(); @@ -424,8 +489,8 @@ fn tool_arguments(raw: Option<&Value>) -> Result { } } -/// The message for what the user typed (and the view, as a data URL). -fn user_message(parts: &[PromptPart]) -> Value { +/// The message for what the user typed (and the view, as the endpoint's own content blocks). +fn user_message(protocol: Protocol, parts: &[PromptPart]) -> Value { let images = parts.iter().any(|p| matches!(p, PromptPart::Image { .. })); if !images { let text: Vec<&str> = parts.iter().filter_map(|p| if let PromptPart::Text(t) = p { Some(t.as_str()) } else { None }).collect(); @@ -433,16 +498,129 @@ fn user_message(parts: &[PromptPart]) -> Value { } let content: Vec = parts .iter() - .filter_map(|p| match p { - PromptPart::Text(t) => Some(json!({"type": "text", "text": t})), - PromptPart::Image { mime, base64 } => Some(json!({"type": "image_url", "image_url": {"url": format!("data:{mime};base64,{base64}")}})), - // This endpoint takes no documents (the chat doesn't send any); the message names the file. - PromptPart::Document { .. } => None, + .filter_map(|p| match (protocol, p) { + (_, PromptPart::Text(t)) => Some(json!({"type": "text", "text": t})), + (Protocol::OpenAi, PromptPart::Image { mime, base64 }) => { + Some(json!({"type": "image_url", "image_url": {"url": format!("data:{mime};base64,{base64}")}})) + } + (Protocol::Anthropic, PromptPart::Image { mime, base64 }) => { + Some(json!({"type": "image", "source": {"type": "base64", "media_type": mime, "data": base64}})) + } + // These endpoints take no documents (the chat doesn't send any); the message names the file. + (_, PromptPart::Document { .. }) => None, }) .collect(); json!({"role": "user", "content": content}) } +/// Whether a history message starts a turn: a user message that is not only tool results. +fn starts_a_turn(message: &Value) -> bool { + message["role"] == "user" && !message["content"].as_array().is_some_and(|blocks| blocks.iter().any(|b| b["type"] == "tool_result")) +} + +/// One tool call the endpoint asked for. +#[derive(Debug)] +struct Call { + id: String, + name: String, + /// The arguments as an object, or why they cannot be used. + arguments: Result, +} + +/// An answer from the endpoint, in the protocol's shape. +#[derive(Debug)] +struct Reply { + /// Text to show: `(thought, text)` in the order it came. + text: Vec<(bool, String)>, + calls: Vec, + /// The assistant message to keep in the history, in the endpoint's own shape. Tool calls are + /// capped to the ones the loop answers, so every call kept here gets a result. + assistant: Value, +} + +/// The answer in the endpoint's shape. +fn reply(protocol: Protocol, body: &Value) -> Reply { + match protocol { + Protocol::OpenAi => openai_reply(body), + Protocol::Anthropic => anthropic_reply(body), + } +} + +fn openai_reply(body: &Value) -> Reply { + let message = body.pointer("/choices/0/message").cloned().unwrap_or(Value::Null); + let text = message.get("content").and_then(Value::as_str).filter(|t| !t.is_empty()).map(|t| vec![(false, t.to_string())]).unwrap_or_default(); + // No more than the loop answers: every call kept here must get a `tool` message. + let raw: Vec = message.get("tool_calls").and_then(Value::as_array).map(|a| a.iter().take(32).cloned().collect()).unwrap_or_default(); + let calls = raw + .iter() + .enumerate() + .map(|(i, c)| Call { + id: c.get("id").and_then(Value::as_str).map_or_else(|| format!("call-{i}"), str::to_string), + name: c.pointer("/function/name").and_then(Value::as_str).unwrap_or("").to_string(), + arguments: tool_arguments(c.pointer("/function/arguments")), + }) + .collect(); + let mut assistant = json!({"role": "assistant", "content": message.get("content").cloned().unwrap_or(Value::Null)}); + if !raw.is_empty() { + assistant["tool_calls"] = Value::Array(raw); + } + Reply { text, calls, assistant } +} + +fn anthropic_reply(body: &Value) -> Reply { + let content = body.get("content").cloned().unwrap_or(Value::Null); + let mut text = Vec::new(); + let mut calls = Vec::new(); + let assistant = match &content { + Value::Array(blocks) => { + // Keep the answer as it came (thinking blocks and signatures included), but leave out + // tool calls beyond the loop's cap so every `tool_use` kept gets its `tool_result`. + let mut kept = Vec::new(); + for (i, block) in blocks.iter().take(128).enumerate() { + match block.get("type").and_then(Value::as_str) { + Some("text") => { + if let Some(t) = block.get("text").and_then(Value::as_str).filter(|t| !t.trim().is_empty()) { + text.push((false, t.to_string())); + } + kept.push(block.clone()); + } + Some("thinking") => { + let thought = block.get("thinking").or_else(|| block.get("text")); + if let Some(t) = thought.and_then(Value::as_str).filter(|t| !t.trim().is_empty()) { + text.push((true, t.to_string())); + } + kept.push(block.clone()); + } + Some("tool_use") if calls.len() < 32 => { + calls.push(Call { + id: block.get("id").and_then(Value::as_str).map_or_else(|| format!("toolu-{i}"), str::to_string), + name: block.get("name").and_then(Value::as_str).unwrap_or("").to_string(), + arguments: match block.get("input") { + None | Some(Value::Null) => Ok(json!({})), + Some(v @ Value::Object(_)) => Ok(v.clone()), + Some(_) => Err("the tool arguments must be a JSON object".into()), + }, + }); + kept.push(block.clone()); + } + Some("tool_use") => {} + _ => kept.push(block.clone()), + } + } + json!({"role": "assistant", "content": kept}) + } + // An endpoint that answers with plain text instead of content blocks. + Value::String(s) => { + if !s.trim().is_empty() { + text.push((false, s.clone())); + } + json!({"role": "assistant", "content": content.clone()}) + } + _ => json!({"role": "assistant", "content": content.clone()}), + }; + Reply { text, calls, assistant } +} + /// MCP `tools/list` as OpenAI function tools (the hidden ones left out). pub fn openai_tools(list: &Value) -> Vec { list.get("tools") @@ -467,6 +645,116 @@ pub fn openai_tools(list: &Value) -> Vec { .unwrap_or_default() } +/// MCP `tools/list` as Anthropic tools (the hidden ones left out). +pub fn anthropic_tools(list: &Value) -> Vec { + list.get("tools") + .and_then(Value::as_array) + .map(|a| { + a.iter() + .take(128) + .filter_map(|t| { + let name = t.get("name")?.as_str()?; + if HIDDEN_TOOLS.contains(&name) { + return None; + } + let schema = t.get("inputSchema").cloned().filter(Value::is_object).unwrap_or(json!({"type": "object", "properties": {}})); + Some(json!({ + "name": name, + "description": t.get("description").and_then(Value::as_str).unwrap_or(""), + "input_schema": schema, + })) + }) + .collect() + }) + .unwrap_or_default() +} + +/// The offered tools' names, whichever shape the endpoint takes. +fn tool_names(list: &Value) -> Vec { + list.get("tools") + .and_then(Value::as_array) + .map(|a| { + a.iter() + .take(128) + .filter_map(|t| { + let name = t.get("name")?.as_str()?; + (!HIDDEN_TOOLS.contains(&name)).then(|| name.to_string()) + }) + .collect() + }) + .unwrap_or_default() +} + +/// `{base}/v1/{path}`, whether the user's base URL ends in `/v1` or in the endpoint root the +/// provider's documentation gives (Anthropic's `https://api.anthropic.com`, DeepSeek's +/// `https://api.deepseek.com/anthropic`). +fn endpoint(base_url: &str, path: &str) -> String { + let base = base_url.trim_end_matches('/'); + if base.to_ascii_lowercase().ends_with("/v1") { format!("{base}/{path}") } else { format!("{base}/v1/{path}") } +} + +/// Headers for an Anthropic-compatible request: the version, and the key when there is one (which +/// only goes over HTTPS or to this computer). +fn anthropic_headers(base_url: &str, key: Option<&str>) -> Result, String> { + let mut headers = vec![("anthropic-version".to_string(), ANTHROPIC_VERSION.to_string())]; + if let Some(k) = key { + if !super::agents::key_safe_for(base_url) { + return Err(crate::i18n::tr("ui.ai_key_needs_https").to_string()); + } + headers.push(("x-api-key".to_string(), k.to_string())); + } + Ok(headers) +} + +/// The endpoint's models for the picker. The Messages API has no models endpoint at every +/// compatible provider (Qwen's has none), so a failure here simply leaves the picker empty. +pub fn anthropic_model_list(http: &HttpFn, base_url: &str, key: Option<&str>) -> Result, String> { + let req = HttpRequest { method: "GET", url: endpoint(base_url, "models"), headers: anthropic_headers(base_url, key)?, body: None, timeout_s: 15 }; + let (status, bytes) = http(&req)?; + let v: Value = serde_json::from_slice(&bytes).unwrap_or(Value::Null); + if status != 200 { + return Err(http_error(status, &v)); + } + Ok(v.get("data") + .and_then(Value::as_array) + .map(|a| { + a.iter() + .take(500) + .filter_map(|m| { + let id = m.get("id").and_then(Value::as_str)?.to_string(); + let name = m.get("display_name").and_then(Value::as_str).map(str::to_string).unwrap_or_else(|| id.clone()); + Some(ModelInfo { id, name, description: String::new() }) + }) + .collect() + }) + .unwrap_or_default()) +} + +/// Test an Anthropic-compatible endpoint: send the smallest message there is, so the check covers +/// the path, the key and the model even where there is no models endpoint to ask. +pub fn test_anthropic_endpoint(http: &HttpFn, base_url: &str, key: Option<&str>, model: &str) -> Result { + let model = model.trim(); + if model.is_empty() { + return Err(crate::i18n::tr("ui.ai_need_model").to_string()); + } + let mut headers = anthropic_headers(base_url, key)?; + headers.push(("Content-Type".to_string(), "application/json".to_string())); + let body = json!({"model": model, "max_tokens": 16, "messages": [{"role": "user", "content": "Hello"}]}); + let req = HttpRequest { + method: "POST", + url: endpoint(base_url, "messages"), + headers, + body: Some(body.to_string().into_bytes()), + timeout_s: TEST_TIMEOUT_S, + }; + let (status, bytes) = http(&req)?; + let v: Value = serde_json::from_slice(&bytes).unwrap_or(Value::Null); + if status != 200 { + return Err(http_error(status, &v)); + } + Ok(crate::i18n::format("ui.ai_test_answered", &[("model", model)])) +} + /// A tool call's result as text for the model, and whether it failed. fn tool_text(r: &Value) -> (String, bool) { let failed = r.get("isError").and_then(Value::as_bool).unwrap_or(false); @@ -572,9 +860,17 @@ mod tests { #[test] fn pictures_go_as_data_urls_and_text_alone_as_a_string() { - assert_eq!(user_message(&[PromptPart::Text("hi".into())])["content"], "hi"); - let m = user_message(&[PromptPart::Text("hi".into()), PromptPart::Image { mime: "image/png".into(), base64: "AA==".into() }]); + assert_eq!(user_message(Protocol::OpenAi, &[PromptPart::Text("hi".into())])["content"], "hi"); + let m = + user_message(Protocol::OpenAi, &[PromptPart::Text("hi".into()), PromptPart::Image { mime: "image/png".into(), base64: "AA==".into() }]); assert_eq!(m["content"][1]["image_url"]["url"], "data:image/png;base64,AA=="); + let a = user_message( + Protocol::Anthropic, + &[PromptPart::Text("hi".into()), PromptPart::Image { mime: "image/png".into(), base64: "AA==".into() }], + ); + assert_eq!(a["content"][1]["type"], "image"); + assert_eq!(a["content"][1]["source"]["media_type"], "image/png"); + assert_eq!(a["content"][1]["source"]["data"], "AA=="); } /// A scripted endpoint: one answer with a tool call, then a plain answer. The loop runs the @@ -595,7 +891,14 @@ mod tests { let a = answers.lock().unwrap().pop().unwrap_or(json!({})); Ok((200, a.to_string().into_bytes())) }); - let cfg = ApiConfig { name: "Test".into(), base_url: "http://localhost:1/v1".into(), model: "m".into(), key: None, http }; + let cfg = ApiConfig { + name: "Test".into(), + protocol: Protocol::OpenAi, + base_url: "http://localhost:1/v1".into(), + model: "m".into(), + key: None, + http, + }; let ctx = StartContext { cwd: std::env::temp_dir(), bridge: None, tools: None, wake: Box::new(|| {}) }; let mut b = ApiBackend::start(cfg, ctx).unwrap(); b.prompt(&[PromptPart::Text("measure it".into())]); @@ -641,7 +944,14 @@ mod tests { } Ok((200, json!({"choices": [{"message": {"role": "assistant", "content": "Steered."}}]}).to_string().into_bytes())) }); - let cfg = ApiConfig { name: "Test".into(), base_url: "http://localhost:1/v1".into(), model: "m".into(), key: None, http }; + let cfg = ApiConfig { + name: "Test".into(), + protocol: Protocol::OpenAi, + base_url: "http://localhost:1/v1".into(), + model: "m".into(), + key: None, + http, + }; let ctx = StartContext { cwd: std::env::temp_dir(), bridge: None, tools: None, wake: Box::new(|| {}) }; let mut b = ApiBackend::start(cfg, ctx).unwrap(); let wait = |b: &mut ApiBackend, events: &mut Vec| { @@ -688,7 +998,14 @@ mod tests { log.lock().unwrap().push(serde_json::from_slice(r.body.as_deref().unwrap_or(b"null")).unwrap()); Ok((200, json!({"choices": [{"message": {"role": "assistant", "content": "ok"}}]}).to_string().into_bytes())) }); - let cfg = ApiConfig { name: "Test".into(), base_url: "http://localhost:1/v1".into(), model: "m".into(), key: None, http }; + let cfg = ApiConfig { + name: "Test".into(), + protocol: Protocol::OpenAi, + base_url: "http://localhost:1/v1".into(), + model: "m".into(), + key: None, + http, + }; let ctx = StartContext { cwd: std::env::temp_dir(), bridge: None, tools: None, wake: Box::new(|| {}) }; let mut b = ApiBackend::start(cfg, ctx).unwrap(); let run = |b: &mut ApiBackend, f: &dyn Fn(&mut ApiBackend), want: usize| { @@ -726,7 +1043,14 @@ mod tests { log.lock().unwrap().push(serde_json::from_slice(r.body.as_deref().unwrap_or(b"null")).unwrap()); Ok((200, json!({"choices": [{"message": {"role": "assistant", "content": "ok"}}]}).to_string().into_bytes())) }); - let cfg = ApiConfig { name: "Test".into(), base_url: "http://localhost:1/v1".into(), model: "local".into(), key: None, http }; + let cfg = ApiConfig { + name: "Test".into(), + protocol: Protocol::OpenAi, + base_url: "http://localhost:1/v1".into(), + model: "local".into(), + key: None, + http, + }; let ctx = StartContext { cwd: std::env::temp_dir(), bridge: None, tools: None, wake: Box::new(|| {}) }; let mut b = ApiBackend::start(cfg, ctx).unwrap(); let mut events = Vec::new(); @@ -775,7 +1099,14 @@ mod tests { log.lock().unwrap().push(serde_json::from_slice(r.body.as_deref().unwrap_or(b"null")).unwrap()); Ok((200, answers.lock().unwrap().pop().unwrap_or(json!({})).to_string().into_bytes())) }); - let cfg = ApiConfig { name: "Test".into(), base_url: "http://localhost:1/v1".into(), model: "m".into(), key: None, http }; + let cfg = ApiConfig { + name: "Test".into(), + protocol: Protocol::OpenAi, + base_url: "http://localhost:1/v1".into(), + model: "m".into(), + key: None, + http, + }; let ctx = StartContext { cwd: std::env::temp_dir(), bridge: None, tools: None, wake: Box::new(|| {}) }; let mut b = ApiBackend::start(cfg, ctx).unwrap(); b.prompt(&[PromptPart::Text("go".into())]); @@ -801,7 +1132,14 @@ mod tests { fn keys_never_go_over_plain_http_to_another_computer() { let http: HttpFn = Arc::new(|_: &HttpRequest| Ok((200, b"{}".to_vec()))); assert!(test_endpoint(&http, "http://10.0.0.2:11434/v1", Some("k"), "m").is_err()); - let cfg = ApiConfig { name: "x".into(), base_url: "http://10.0.0.2/v1".into(), model: "m".into(), key: Some("k".into()), http }; + let cfg = ApiConfig { + name: "x".into(), + protocol: Protocol::OpenAi, + base_url: "http://10.0.0.2/v1".into(), + model: "m".into(), + key: Some("k".into()), + http, + }; let ctx = StartContext { cwd: std::env::temp_dir(), bridge: None, tools: None, wake: Box::new(|| {}) }; assert!(ApiBackend::start(cfg, ctx).is_err()); } @@ -817,13 +1155,21 @@ mod tests { let (status, v) = answers.lock().unwrap().pop().expect("no request beyond the script"); Ok((status, v.to_string().into_bytes())) }); - let cfg = ApiConfig { name: "Test".into(), base_url: "http://localhost:1/v1".into(), model: "m".into(), key: Some("sk-secret".into()), http }; + let cfg = ApiConfig { + name: "Test".into(), + protocol: Protocol::OpenAi, + base_url: "http://localhost:1/v1".into(), + model: "m".into(), + key: Some("sk-secret".into()), + http, + }; let (events, _rx) = channel(); let w = Worker { cfg, mcp: None, next: 0, tools: vec![json!({"type": "function", "function": {"name": "measure"}})], + tool_names: vec!["measure".into()], history: vec![json!({"role": "user", "content": "hi"})], events, wake: Arc::new(|| {}), @@ -843,8 +1189,8 @@ mod tests { fn a_refusal_of_tools_with_reasoning_effort_is_replayed_once_with_none() { let ok = json!({"choices": [{"message": {"role": "assistant", "content": "Done."}}]}); let (w, seen) = scripted_worker(vec![refusal(), (200, ok.clone()), (200, ok.clone())]); - assert_eq!(w.request().unwrap(), ok); - assert_eq!(w.request().unwrap(), ok); + assert_eq!(w.request().unwrap().assistant, ok["choices"][0]["message"]); + assert_eq!(w.request().unwrap().assistant, ok["choices"][0]["message"]); let sent = seen.lock().unwrap(); assert_eq!(sent.len(), 3); assert!(sent[0].get("reasoning_effort").is_none()); @@ -864,4 +1210,126 @@ mod tests { assert!(w.request().unwrap_err().contains("context length")); assert_eq!(seen.lock().unwrap().len(), 1); } + + #[test] + fn mcp_tools_become_anthropic_tools_without_crash_reports() { + let list = json!({"tools": [ + {"name": "batch", "description": "Run commands", "inputSchema": {"type": "object", "properties": {"commands": {"type": "array"}}}}, + {"name": "crash_reports", "description": "x", "inputSchema": {"type": "object"}}, + {"name": "measure"}, + ]}); + let t = anthropic_tools(&list); + assert_eq!(t.len(), 2); + assert_eq!(t[0]["name"], "batch"); + assert_eq!(t[0]["input_schema"]["properties"]["commands"]["type"], "array"); + assert_eq!(t[1]["input_schema"]["type"], "object"); + assert_eq!(tool_names(&list), ["batch", "measure"]); + } + + #[test] + fn anthropic_answers_read_text_thought_and_tool_calls() { + let r = anthropic_reply(&json!({"content": [ + {"type": "thinking", "thinking": "checking", "signature": "s"}, + {"type": "text", "text": "Measuring."}, + {"type": "tool_use", "id": "t1", "name": "measure", "input": {"body": "b"}}, + ]})); + assert_eq!(r.text, [(true, "checking".to_string()), (false, "Measuring.".to_string())]); + assert_eq!(r.calls.len(), 1); + assert_eq!(r.calls[0].id, "t1"); + assert_eq!(r.calls[0].name, "measure"); + assert_eq!(r.calls[0].arguments, Ok(json!({"body": "b"}))); + assert_eq!(r.assistant["role"], "assistant"); + assert_eq!(r.assistant["content"][2]["type"], "tool_use"); + let broken = anthropic_reply(&json!({"content": [{"type": "tool_use", "name": "measure", "input": "no"}]})); + assert!(broken.calls[0].arguments.is_err()); + // Calls beyond the loop's cap are left out of the answer too: each kept one is answered. + let many: Vec = (0..40).map(|i| json!({"type": "tool_use", "id": format!("t{i}"), "name": "measure", "input": {}})).collect(); + let capped = anthropic_reply(&json!({"content": many})); + assert_eq!(capped.calls.len(), 32); + assert_eq!(capped.assistant["content"].as_array().unwrap().len(), 32); + } + + #[test] + fn anthropic_urls_take_the_base_with_or_without_v1() { + assert_eq!(endpoint("https://api.anthropic.com", "messages"), "https://api.anthropic.com/v1/messages"); + assert_eq!(endpoint("https://api.anthropic.com/v1", "messages"), "https://api.anthropic.com/v1/messages"); + assert_eq!(endpoint("https://api.anthropic.com/v1/", "messages"), "https://api.anthropic.com/v1/messages"); + assert_eq!(endpoint("https://api.deepseek.com/anthropic/", "models"), "https://api.deepseek.com/anthropic/v1/models"); + assert_eq!( + endpoint("https://dashscope.aliyuncs.com/apps/anthropic", "messages"), + "https://dashscope.aliyuncs.com/apps/anthropic/v1/messages" + ); + } + + /// A scripted Anthropic endpoint: one answer with a tool call, then a plain answer. The loop + /// runs the tool (here without SolidCraft tools: an unknown tool fails) and reports the end. + #[test] + fn the_anthropic_tool_loop_runs_calls_and_ends_the_turn() { + let answers = std::sync::Mutex::new(vec![ + json!({"content": [{"type": "text", "text": "Done."}]}), + json!({"content": [ + {"type": "text", "text": "Measuring."}, + {"type": "tool_use", "id": "t1", "name": "measure", "input": {}}, + ]}), + ]); + let seen = Arc::new(std::sync::Mutex::new(Vec::::new())); + let log = seen.clone(); + let http: HttpFn = Arc::new(move |r: &HttpRequest| { + if r.method == "GET" { + return Ok((200, b"{\"data\":[]}".to_vec())); + } + log.lock().unwrap().push(serde_json::from_slice(r.body.as_deref().unwrap_or(b"null")).unwrap()); + Ok((200, answers.lock().unwrap().pop().unwrap_or(json!({})).to_string().into_bytes())) + }); + let cfg = ApiConfig { + name: "Test".into(), + protocol: Protocol::Anthropic, + base_url: "http://localhost:1".into(), + model: "m".into(), + key: Some("sk-secret".into()), + http, + }; + let ctx = StartContext { cwd: std::env::temp_dir(), bridge: None, tools: None, wake: Box::new(|| {}) }; + let mut b = ApiBackend::start(cfg, ctx).unwrap(); + b.prompt(&[PromptPart::Text("measure it".into())]); + let mut events = Vec::new(); + let start = std::time::Instant::now(); + while !events.iter().any(|e| matches!(e, Event::TurnEnded { .. })) && start.elapsed() < Duration::from_secs(10) { + events.extend(b.poll()); + std::thread::sleep(Duration::from_millis(10)); + } + assert!(matches!(events.first(), Some(Event::Ready { .. })), "{events:?}"); + assert!(events.iter().any(|e| matches!(e, Event::Tool { status: Some(ToolStatus::Failed), .. })), "{events:?}"); + assert!(events.iter().any(|e| matches!(e, Event::Text { text, .. } if text == "Done.")), "{events:?}"); + assert!(matches!(events.last(), Some(Event::TurnEnded { reason }) if reason.is_empty()), "{events:?}"); + let sent = seen.lock().unwrap(); + assert_eq!(sent.len(), 2); + assert_eq!(sent[0]["system"], super::super::acp::SYSTEM_NOTE); + assert_eq!(sent[0]["max_tokens"], ANTHROPIC_MAX_TOKENS); + assert_eq!(sent[1]["messages"].as_array().unwrap().last().unwrap()["content"][0]["type"], "tool_result"); + assert_eq!(sent[1]["messages"].as_array().unwrap().last().unwrap()["content"][0]["tool_use_id"], "t1"); + b.shutdown(true); + } + + #[test] + fn the_anthropic_test_sends_one_small_message_and_checks_the_key_rule() { + let seen = Arc::new(std::sync::Mutex::new(Vec::::new())); + let log = seen.clone(); + let http: HttpFn = Arc::new(move |r: &HttpRequest| { + log.lock().unwrap().push(r.clone()); + Ok((200, json!({"content": [{"type": "text", "text": "Hi"}]}).to_string().into_bytes())) + }); + let message = test_anthropic_endpoint(&http, "https://api.anthropic.com/v1", Some("sk-k"), "claude-opus-5-5").unwrap(); + assert!(message.contains("claude-opus-5-5"), "{message}"); + let sent = seen.lock().unwrap(); + assert_eq!(sent[0].url, "https://api.anthropic.com/v1/messages"); + assert!(sent[0].headers.iter().any(|(k, v)| k == "anthropic-version" && v == ANTHROPIC_VERSION)); + assert!(sent[0].headers.iter().any(|(k, v)| k == "x-api-key" && v == "sk-k")); + let body: Value = serde_json::from_slice(sent[0].body.as_deref().unwrap()).unwrap(); + assert_eq!(body["max_tokens"], 16); + assert_eq!(body["messages"][0]["content"], "Hello"); + drop(sent); + assert!(test_anthropic_endpoint(&http, "http://10.0.0.2/anthropic", Some("k"), "m").is_err()); + assert!(test_anthropic_endpoint(&http, "https://api.anthropic.com", None, "").is_err()); + } } diff --git a/crates/ui-egui/src/agent_chat/mod.rs b/crates/ui-egui/src/agent_chat/mod.rs index e73f401fa..9b0d6a738 100644 --- a/crates/ui-egui/src/agent_chat/mod.rs +++ b/crates/ui-egui/src/agent_chat/mod.rs @@ -742,7 +742,7 @@ pub fn connect(app: &mut SolveApp, ctx: &egui::Context) { return; }; // An API agent's key comes from the keychain for this connection only. - let key = if entry.kind == agents::Kind::Api && entry.key { + let key = if entry.kind.is_api() && entry.key { match app.services.secrets.as_ref().map(|s| s.get(&entry.id)) { Some(Ok(k)) => k, Some(Err(e)) => { @@ -778,12 +778,22 @@ pub fn connect(app: &mut SolveApp, ctx: &egui::Context) { let started: Result, String> = match entry.kind { agents::Kind::Acp if entry.argv.is_empty() => Err(tr("ui.agent_chat_none_found").to_string()), agents::Kind::Acp => acp_client::AcpBackend::start(&entry.argv, start).map(|b| Box::new(b) as Box), - agents::Kind::Api => match http { - Some(http) => api_client::ApiBackend::start( - api_client::ApiConfig { name: entry.name.clone(), base_url: entry.base_url.clone(), model: entry.model.clone(), key, http }, - start, - ) - .map(|b| Box::new(b) as Box), + kind @ (agents::Kind::Api | agents::Kind::Anthropic) => match http { + Some(http) => { + let protocol = if kind == agents::Kind::Anthropic { agents::Protocol::Anthropic } else { agents::Protocol::OpenAi }; + api_client::ApiBackend::start( + api_client::ApiConfig { + name: entry.name.clone(), + protocol, + base_url: entry.base_url.clone(), + model: entry.model.clone(), + key, + http, + }, + start, + ) + .map(|b| Box::new(b) as Box) + } None => Err(tr("ui.ai_no_http").to_string()), }, }; @@ -796,7 +806,7 @@ pub fn connect(app: &mut SolveApp, ctx: &egui::Context) { chat.mode.clear(); chat.models.clear(); chat.model.clear(); - chat.running_endpoint = if entry.kind == agents::Kind::Api { entry.base_url.clone() } else { String::new() }; + chat.running_endpoint = if entry.kind.is_api() { entry.base_url.clone() } else { String::new() }; // An endpoint's models from the last time, until the new list arrives. if let Some(list) = chat.models_cache.get(&chat.running_endpoint) { chat.models = list.clone(); @@ -1487,7 +1497,7 @@ pub fn set_model(app: &mut SolveApp, model: &str) { } app.agent_chat.model = model.to_string(); let agent = app.agent_chat.running_id.clone(); - if let Some(a) = app.preferences.agents.iter_mut().find(|a| a.id == agent && a.kind == agents::Kind::Api) { + if let Some(a) = app.preferences.agents.iter_mut().find(|a| a.id == agent && a.kind.is_api()) { a.model = model.to_string(); } app.preferences.agent_models.insert(agent, model.to_string()); @@ -1537,7 +1547,7 @@ pub fn control(app: &mut SolveApp, ctx: &egui::Context, p: &Value) -> Result Result { - let kind = match k { - "installed" => settings::AddKind::Installed, - "custom" => settings::AddKind::Custom, - "api" => settings::AddKind::Api, - _ => return Err("add_dialog must be installed, custom, api or false".into()), + let (kind, protocol) = match k { + "installed" => (settings::AddKind::Installed, None), + "custom" => (settings::AddKind::Custom, None), + "api" => (settings::AddKind::Api, Some(agents::Protocol::OpenAi)), + "anthropic" => (settings::AddKind::Api, Some(agents::Protocol::Anthropic)), + _ => return Err("add_dialog must be installed, custom, api, anthropic or false".into()), }; settings::open_add(app); if let Some(a) = app.agent_chat.settings.add.as_mut() { @@ -1568,6 +1580,9 @@ pub fn control(app: &mut SolveApp, ctx: &egui::Context, p: &Value) -> Result app.agent_chat.settings.add = None, @@ -1700,7 +1715,7 @@ pub fn status(app: &SolveApp) -> Value { "features": app.preferences.ai_features, "toolbarButton": app.preferences.ai_toolbar_button, "permissions": app.preferences.ai_permissions.id(), - "agents": agents::entries(app).iter().map(|e| json!({"id": e.id, "name": e.name, "kind": if e.kind == agents::Kind::Api { "api" } else { "acp" }, "found": e.found, "version": e.version, "login": e.login})).collect::>(), + "agents": agents::entries(app).iter().map(|e| json!({"id": e.id, "name": e.name, "kind": e.kind.id(), "found": e.found, "version": e.version, "login": e.login})).collect::>(), "currentAgent": agents::current(app).map(|e| e.id), "models": c.models.iter().map(|m| json!({"id": m.id, "name": m.name, "description": m.description})).collect::>(), "model": c.model, diff --git a/crates/ui-egui/src/agent_chat/settings.rs b/crates/ui-egui/src/agent_chat/settings.rs index e997f7eeb..27cafaa0b 100644 --- a/crates/ui-egui/src/agent_chat/settings.rs +++ b/crates/ui-egui/src/agent_chat/settings.rs @@ -1,9 +1,9 @@ //! Settings → AI: the master switch, the agents (found and added, each with Test, Default and, //! for added ones, Remove), permissions (global and per agent, and the "always allow" answers), //! and the panel. Plus the Add agent dialog: an installed ACP agent, a custom command, or an -//! OpenAI-compatible endpoint (whose key goes to the system keychain). +//! endpoint (OpenAI-compatible or Anthropic-compatible, whose key goes to the system keychain). -use super::agents::{self, AgentConfig, Kind, Mode}; +use super::agents::{self, AgentConfig, Kind, Mode, Protocol}; use super::*; use crate::ui_kit; @@ -23,6 +23,7 @@ pub struct AddState { pub command: String, pub installed: usize, pub preset: usize, + pub protocol: Protocol, pub base_url: String, pub model: String, pub key: String, @@ -55,14 +56,28 @@ pub struct State { /// Open the Add agent dialog. pub fn open_add(app: &mut SolveApp) { - let preset = agents::API_PRESETS.first(); + let preset = agents::OPENAI_PRESETS.first(); app.agent_chat.settings.add = Some(AddState { - base_url: preset.map(|p| p.1.to_string()).unwrap_or_default(), - name: preset.map(|p| p.0.to_string()).unwrap_or_default(), + base_url: preset.map(|p| p.base_url.to_string()).unwrap_or_default(), + name: preset.map(|p| p.name.to_string()).unwrap_or_default(), ..Default::default() }); } +/// Take `index` from the protocol's presets, or make the URL empty for a custom endpoint. +pub(super) fn choose_preset(s: &mut AddState, protocol: Protocol, index: usize) { + s.protocol = protocol; + s.preset = index; + if let Some(preset) = agents::presets(protocol).get(index) { + s.base_url = preset.base_url.to_string(); + s.name = preset.name.to_string(); + } else { + s.base_url.clear(); + } + s.error = None; + s.test = None; +} + /// Run a connection test in the background (it starts the agent briefly, or asks the endpoint /// for its models). #[cfg(not(target_arch = "wasm32"))] @@ -82,6 +97,13 @@ fn start_test(app: &SolveApp, e: &agents::Entry, key: Option) -> Test { let _ = tx.send(super::api_client::test_endpoint(&http, &url, key.as_deref(), &model)); }); } + Kind::Anthropic => { + let Some(http) = app.services.agent_http.clone() else { return Test::Done(Err(tr("ui.ai_no_http").to_string())) }; + let (url, model) = (e.base_url.clone(), e.model.clone()); + std::thread::spawn(move || { + let _ = tx.send(super::api_client::test_anthropic_endpoint(&http, &url, key.as_deref(), &model)); + }); + } } Test::Running(rx) } @@ -282,8 +304,12 @@ fn agents_section(app: &mut SolveApp, ui: &mut egui::Ui) { } let test_button = ui_kit::button(ui, tr("ui.ai_test"), ui_kit::Button::Small); crate::scenario::publish_handle(&format!("prefs:ai_test:{}", entry.id), test_button.rect.center()); - if test_button.on_hover_text(tr(if entry.kind == Kind::Acp { "ui.ai_test_acp_tip" } else { "ui.ai_test_api_tip" })).clicked() - { + let tip = match entry.kind { + Kind::Acp => "ui.ai_test_acp_tip", + Kind::Api => "ui.ai_test_api_tip", + Kind::Anthropic => "ui.ai_test_anthropic_tip", + }; + if test_button.on_hover_text(tr(tip)).clicked() { test = Some(entry.clone()); } if !entry.found && ui_kit::button(ui, tr("ui.ai_remove"), ui_kit::Button::Ghost).clicked() { @@ -388,8 +414,8 @@ pub fn dialog(app: &mut SolveApp, ctx: &egui::Context) { if let Some(test) = s.test.as_mut() { test.poll(); } - let kinds = [(AddKind::Installed, "ui.ai_add_installed"), (AddKind::Custom, "ui.ai_add_custom"), (AddKind::Api, "ui.ai_add_api")]; - ui_kit::row(ui, tr("ui.ai_preset"), |ui| { + let kinds = [(AddKind::Installed, "ui.ai_add_installed"), (AddKind::Custom, "ui.ai_add_custom"), (AddKind::Api, "ui.ai_add_endpoint")]; + ui_kit::row(ui, tr("ui.ai_kind"), |ui| { let mut selected = kinds.iter().position(|(k, _)| *k == s.kind).unwrap_or(0); let labels = kinds.map(|(_, key)| tr(key)); if ui_kit::segmented(ui, &mut selected, &labels, ui_kit::Segments { compact: true, ..Default::default() }) @@ -400,7 +426,7 @@ pub fn dialog(app: &mut SolveApp, ctx: &egui::Context) { s.test = None; } for (_, key) in kinds { - if let Some(at) = crate::scenario::handle_at(&format!("dialog:{}:{}", tr("ui.ai_preset"), tr(key))) { + if let Some(at) = crate::scenario::handle_at(&format!("dialog:{}:{}", tr("ui.ai_kind"), tr(key))) { crate::scenario::publish_handle(&format!("ai_add:{key}"), at); } } @@ -447,21 +473,33 @@ pub fn dialog(app: &mut SolveApp, ctx: &egui::Context) { ui_kit::text(ui, tr("ui.agent_chat_custom_hint"), ui_kit::Text::Note("custom")); } AddKind::Api => { - let presets: Vec<_> = agents::API_PRESETS.iter().map(|p| p.0).chain(std::iter::once(tr("ui.ai_custom_url"))).collect(); + ui_kit::row(ui, tr("ui.ai_protocol"), |ui| { + let mut selected = if s.protocol == Protocol::Anthropic { 1 } else { 0 }; + let labels = [tr("ui.ai_add_api"), tr("ui.ai_add_anthropic")]; + if ui_kit::segmented(ui, &mut selected, &labels, ui_kit::Segments { compact: true, ..Default::default() }) + && let Some(protocol) = [Protocol::OpenAi, Protocol::Anthropic].get(selected) + { + choose_preset(s, *protocol, 0); + } + for (name, key) in [("openai", "ui.ai_add_api"), ("anthropic", "ui.ai_add_anthropic")] { + if let Some(at) = crate::scenario::handle_at(&format!("dialog:{}:{}", tr("ui.ai_protocol"), tr(key))) { + crate::scenario::publish_handle(&format!("ai_add:protocol:{name}"), at); + } + } + }); + let choices: Vec<&str> = + agents::presets(s.protocol).iter().map(|p| p.name).chain(std::iter::once(tr("ui.ai_custom_url"))).collect(); ui_kit::row(ui, tr("ui.ai_preset"), |ui| { - ui_kit::dropdown_menu(ui, "sc_ai_add_preset", presets.get(s.preset).copied().unwrap_or_default(), |ui| { - for (index, preset) in presets.iter().enumerate() { - if ui_kit::list_picker(ui, preset, preset, s.preset == index).clicked() { - s.preset = index; - if let Some((name, url, _)) = agents::API_PRESETS.get(index) { - s.base_url = url.to_string(); - s.name = name.to_string(); - } else { - s.base_url.clear(); - } + let picker = ui_kit::dropdown_menu(ui, "sc_ai_add_preset", choices.get(s.preset).copied().unwrap_or_default(), |ui| { + for (index, preset) in choices.iter().enumerate() { + let response = ui_kit::list_picker(ui, preset, preset, s.preset == index); + crate::scenario::publish_handle(&format!("ai_add:preset:{preset}"), response.rect.center()); + if response.clicked() { + choose_preset(s, s.protocol, index); } } }); + crate::scenario::publish_handle("ai_add:preset", picker.rect.center()); }); ui_kit::row(ui, tr("ui.ai_name"), |ui| { ui_kit::text_field(ui, &mut s.name, ""); @@ -501,7 +539,7 @@ pub fn dialog(app: &mut SolveApp, ctx: &egui::Context) { let e = agents::Entry { id: String::new(), name: s.name.clone(), - kind: Kind::Api, + kind: s.protocol.kind(), argv: Vec::new(), version: None, login: "", @@ -555,7 +593,7 @@ fn add_agent(app: &mut SolveApp) -> Result<(), String> { c.id = agents::new_id(&app.preferences, "custom"); } AddKind::Api => { - c.kind = Kind::Api; + c.kind = s.protocol.kind(); c.base_url = agents::normalize_url(&s.base_url)?; c.model = s.model.trim().to_string(); if c.model.is_empty() { @@ -565,7 +603,7 @@ fn add_agent(app: &mut SolveApp) -> Result<(), String> { if key.is_some() && !agents::key_safe_for(&c.base_url) { return Err(tr("ui.ai_key_needs_https").to_string()); } - c.id = agents::new_id(&app.preferences, "api"); + c.id = agents::new_id(&app.preferences, c.kind.id()); } } if c.name.is_empty() { @@ -607,10 +645,23 @@ mod card_tests { assert!(h.app.preferences.agents.iter().any(|e| e.name == "Local fixture")); open_add(&mut h.app); h.frames(3); - h.step(&json!({"click":{"handle":"ai_add:ui.ai_add_api"}})).unwrap(); + h.step(&json!({"click":{"handle":"ai_add:ui.ai_add_endpoint"}})).unwrap(); assert!(scenario::handle_at("ai_add:key").is_some()); assert!(h.app.services.secrets.is_none(), "this fixture has no keychain"); - h.step(&json!({"key":"Escape"})).unwrap(); + h.step(&json!({"click":{"handle":"ai_add:protocol:anthropic"}})).unwrap(); + { + let add = h.app.agent_chat.settings.add.as_ref().unwrap(); + assert_eq!(add.protocol, Protocol::Anthropic); + assert_eq!(add.base_url, "https://api.anthropic.com/v1"); + assert_eq!(add.name, "Anthropic"); + } + h.app.agent_chat.settings.add.as_mut().unwrap().model = "claude-opus-5-5".into(); + h.frames(3); + h.step(&json!({"click":{"handle":"ai_add:add"}})).unwrap(); assert!(h.app.agent_chat.settings.add.is_none()); + let saved = h.app.preferences.agents.last().unwrap(); + assert_eq!(saved.kind, Kind::Anthropic); + assert_eq!(saved.base_url, "https://api.anthropic.com/v1"); + assert_eq!(saved.model, "claude-opus-5-5"); } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 6d2b46833..0c4cf320a 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -353,7 +353,7 @@ pub struct Services { pub restore_file: Option>, /// Show the designs the browser keeps for itself; None: the host has no such store. pub browser_files: Option>, - /// HTTP for the agent chat's API agents (OpenAI-compatible endpoints); None: no API agents. + /// HTTP for the agent chat's API agents (OpenAI-compatible and Anthropic-compatible endpoints); None: no API agents. pub agent_http: Option, /// The system keychain, for API agents' keys; None: keys can't be stored. pub secrets: Option>, diff --git a/docs/agent-chat.md b/docs/agent-chat.md index d8565a424..e17831886 100644 --- a/docs/agent-chat.md +++ b/docs/agent-chat.md @@ -1,11 +1,12 @@ # Agent chat Agent chat is a side panel where you talk to a coding agent you already use (Claude Code, -Codex, Google Antigravity, OpenCode, Grok Build and others), or to a model behind an OpenAI-compatible endpoint you -choose (Ollama or LM Studio on your computer, OpenAI, any other URL), and have it work on the -design that's open. A coding agent runs on your computer under **your own account** with that -agent: SolidCraft doesn't sell or proxy a model and needs no key for it. An endpoint's key, if -it needs one, goes into your system keychain, never into SolidCraft's settings. +Codex, Google Antigravity, OpenCode, Grok Build and others), or to a model behind an +OpenAI-compatible or Anthropic-compatible endpoint you choose (Ollama or LM Studio on your +computer, OpenAI, Anthropic, any other URL), and have it work on the design that's open. A +coding agent runs on your computer under **your own account** with that agent: SolidCraft +doesn't sell or proxy a model and needs no key for it. An endpoint's key, if it needs one, goes +into your system keychain, never into SolidCraft's settings. Desktop app only. @@ -199,11 +200,15 @@ Grok Build also supports `XAI_API_KEY` for users who choose API authentication. `my-agent --acp`. SolidCraft starts it directly, not through a shell. - **OpenAI-compatible API:** a preset (Ollama `http://localhost:11434/v1`, LM Studio `http://localhost:1234/v1`, OpenAI) or a custom URL, the model name, and an optional key. - The key goes into the system keychain (Keychain on macOS, Credential Manager on Windows, the - Secret Service on Linux) and is only ever sent over HTTPS or to this computer. **Test** asks the - endpoint for its models. For these agents SolidCraft runs the conversation itself, with the - same tools, limits and questions as for a coding agent, and at most 24 model requests per - message. +- **Anthropic-compatible API:** a preset (Anthropic) or a custom URL, for an endpoint that + speaks the Anthropic Messages API (`{base}/v1/messages`). This covers Anthropic itself and + providers that offer an Anthropic-compatible endpoint, such as DeepSeek and Qwen. +- The key goes into the system keychain (Keychain on macOS, Credential Manager on Windows, the + Secret Service on Linux) and is only ever sent over HTTPS or to this computer. **Test** asks an + OpenAI-compatible endpoint for its models; an Anthropic-compatible one gets a short message, + because not every such endpoint lists models (Qwen's has no model list). For these agents + SolidCraft runs the conversation itself, with the same tools, limits and questions as for a + coding agent, and at most 24 model requests per message. ## Signing in diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 8abeddc38..d36ee69ca 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -211,7 +211,7 @@ labels; `prefs:measure_axis_distances` identifies the checkbox. | `ui.agent_cursor` | `show` (bool), `speed`: a number from 1 to 5 (times the normal speed) or `"instant"` (no animation: the command runs at once and the pointer jumps to where it acted), `follow_camera` (bool); all optional | the settings. While the cursor is shown, `engine.execute` first glides to the command's toolbar button and to each pick, then runs and replies; later requests wait their turn. It also follows the pointer input sent with `ui.move`, `ui.click`, `ui.drag` and `ui.scroll`, as each event reaches the app, and a press leaves a ripple. `"animate": false` on a call skips it | | `engine.execute` → `view.layout` | `mode`: `floating` or `compact` | Apply and save the workspace layout without opening a dialog, changing the design, resetting the camera or cancelling its active tool. General preferences expose the same choice; `ui.prefs {"layout":"compact"}` also saves it. | | `engine.execute` → `view.history_layout` | `width?`, `dock?`: `bottom`, `left`, `under_browser`, `collapsed?` | Resize/dock the history without opening a dialog or changing the design. Contents respond to measured width; browser-stacked history shares its width and splits height equally. One folded header gives the expanded card the rest; two folded headers stay at the top. | -| `ui.agent_chat` | `features?`, `toolbar_button?`, `permissions?` (`ask`, `edits`, `all`), `add_agent?` ({`name`, `kind`: `acp` or `api`, `command` or `base_url` + `model`}; no keys), `default_agent?`, `agent?` (the conversation's agent, by id), `mode?` (the agent's own mode), `model?` (one of its models), `open?`, `side?` (`left`, `right`), `minimized?`, `popped?` (in its own window), `width?`, `picker?`, `refresh?`, `connect?`, `stop?`, `attach?`: a file's path (a picture, a text file or a CAD file, for the next message; CAD files are imported at once), `remove_file?` (index of a file on the next message), `send?`: text (files alone make a message too), `attach_view?`, `permission?`: an option id or `"cancel"` (the newest question of the agent's own), `approve?`: true, false or `"always"` (the oldest request held for Allow/Deny), `cancel?` (Stop: the queue stays), `send_now?` (stop the running step, then send the queued messages), `remove_pending?` (index), `new_chat?` (clear the chat, new session), `undo?` (the last message's changes) | the chat's state: `features`, `agents` (found and added), `currentAgent`, `state` (off, starting, ready, busy, auth, exited), `popped`, `window` (its place and size), `agent`, `modes`, `mode`, `models`, `model`, `authMethods`, `toolsPort`, `pid`, `waiting`, `turn`, `pending` (queued messages, with their `files`), `attachments` (the files on the next message, by name), `loading` (pictures still being read for it, by name), `stopped`, `detection` and the conversation (`items`). With `features` off only the settings apply. Desktop only; see [Agent chat](agent-chat.md) | +| `ui.agent_chat` | `features?`, `toolbar_button?`, `permissions?` (`ask`, `edits`, `all`), `add_agent?` ({`name`, `kind`: `acp`, `api` or `anthropic` (`api` is OpenAI-compatible, `anthropic` Anthropic-compatible), `command` or `base_url` + `model`}; no keys), `add_dialog?` (`installed`, `custom`, `api`, `anthropic`, or `false` to close), `default_agent?`, `agent?` (the conversation's agent, by id), `mode?` (the agent's own mode), `model?` (one of its models), `open?`, `side?` (`left`, `right`), `minimized?`, `popped?` (in its own window), `width?`, `picker?`, `refresh?`, `connect?`, `stop?`, `attach?`: a file's path (a picture, a text file or a CAD file, for the next message; CAD files are imported at once), `remove_file?` (index of a file on the next message), `send?`: text (files alone make a message too), `attach_view?`, `permission?`: an option id or `"cancel"` (the newest question of the agent's own), `approve?`: true, false or `"always"` (the oldest request held for Allow/Deny), `cancel?` (Stop: the queue stays), `send_now?` (stop the running step, then send the queued messages), `remove_pending?` (index), `new_chat?` (clear the chat, new session), `undo?` (the last message's changes) | the chat's state: `features`, `agents` (found and added), `currentAgent`, `state` (off, starting, ready, busy, auth, exited), `popped`, `window` (its place and size), `agent`, `modes`, `mode`, `models`, `model`, `authMethods`, `toolsPort`, `pid`, `waiting`, `turn`, `pending` (queued messages, with their `files`), `attachments` (the files on the next message, by name), `loading` (pictures still being read for it, by name), `stopped`, `detection` and the conversation (`items`). With `features` off only the settings apply. Desktop only; see [Agent chat](agent-chat.md) | | `ui.resize` | `width`, `height` | | | `app.quit` | `force?` (quit without asking about unsaved designs) | `quitting` (the window closes), `asking` (a "save changes?" prompt is open) | diff --git a/docs/design/in-app-agent-chat.md b/docs/design/in-app-agent-chat.md index 8a0739543..c529f29c8 100644 --- a/docs/design/in-app-agent-chat.md +++ b/docs/design/in-app-agent-chat.md @@ -92,7 +92,7 @@ Code: `crates/ui-egui/src/agent_chat/`. |---|---| | `backend.rs` | The `ChatBackend` trait and its `Event`s: the only thing the panel talks to. | | `acp.rs`, `acp_client.rs` | The protocol messages, and `AcpBackend`, the first backend. | -| `api_client.rs` | `ApiBackend`: an OpenAI-compatible endpoint, with SolidCraft running the tool loop. | +| `api_client.rs` | `ApiBackend`: an OpenAI-compatible or Anthropic-compatible endpoint, with SolidCraft running the tool loop. | | `agents.rs` | The agents (found and added), permission modes, "always allow" answers. | | `settings.rs` | Preferences → AI and the Add agent dialog. | | `process.rs` | The agent as a child process in a process group of its own; stopping it ends the group. | @@ -112,13 +112,17 @@ requests, for a backend that runs tools in process). Two backends exist: - `AcpBackend` drives a coding agent the user already has (the default, and the point of the feature: the user's own account, no keys). - `ApiBackend` talks to an OpenAI-compatible chat completions endpoint (Ollama, LM Studio, - OpenAI, a custom URL) and runs the tool loop itself: it starts the same `solidcraft-cli mcp - --connect` server an ACP agent would get and is its MCP client (`tools/list` becomes the - function tools, `crash_reports` left out), so its calls pass the same gate, limits, questions - and undo as an ACP agent's. At most 24 model requests per message. The HTTP request is the - desktop host's (`Services::agent_http`, ureq with no redirects); the key comes from the system - keychain (`Services::secrets`, the keyring crate) for the connection only and is refused over - plain HTTP to another computer. + OpenAI, a custom URL) or an Anthropic-compatible Messages endpoint (Anthropic, or a provider + that speaks the Messages API), and runs the tool loop itself: it starts the same + `solidcraft-cli mcp --connect` server an ACP agent would get and is its MCP client + (`tools/list` becomes the endpoint's tools, `crash_reports` left out), so its calls pass the + same gate, limits, questions and undo as an ACP agent's. At most 24 model requests per + message. The HTTP request is the desktop host's (`Services::agent_http`, ureq with no + redirects); the key comes from the system keychain (`Services::secrets`, the keyring crate) + for the connection only and is refused over plain HTTP to another computer. The two protocols + differ only in their wire shape: OpenAI sends the system note as a message, function tools + and one `tool` message per result; Anthropic sends it as `system`, `input_schema` tools and + all of a round's `tool_result` blocks in one user message. Another kind of backend slots in the same way without reshaping the panel.