Files
makeCAD/apps/solidcraft-web/src/agent.rs
T
likang 2cdf535fd1
CI / Select checks (push) Has been cancelled
CI / Format and repository checks (push) Has been cancelled
CI / Compile RISC-V (manual) (push) Has been cancelled
Docker / image (push) Has been cancelled
Generated files / Regenerate and commit (push) Has been cancelled
Lists / Design format changes merge cleanly (push) Has been cancelled
Lists / Lists are sorted one item per line (push) Has been cancelled
Packaging lint / lint (push) Has been cancelled
Pages / build (push) Has been cancelled
CI / Dependency advisories (push) Has been cancelled
CI / Quick compile (push) Has been cancelled
CI / Native Linux Clippy (push) Has been cancelled
CI / Build tests (push) Has been cancelled
CI / Native Linux tests (push) Has been cancelled
CI / GPU path tests (1/4) (push) Has been cancelled
CI / GPU path tests (2/4) (push) Has been cancelled
CI / GPU path tests (3/4) (push) Has been cancelled
CI / GPU path tests (4/4) (push) Has been cancelled
CI / Action matrix (1/16) (push) Has been cancelled
CI / Action matrix (10/16) (push) Has been cancelled
CI / Action matrix (11/16) (push) Has been cancelled
CI / Action matrix (12/16) (push) Has been cancelled
CI / Action matrix (13/16) (push) Has been cancelled
CI / Action matrix (14/16) (push) Has been cancelled
CI / Action matrix (15/16) (push) Has been cancelled
CI / Action matrix (16/16) (push) Has been cancelled
CI / Action matrix (2/16) (push) Has been cancelled
CI / Action matrix (3/16) (push) Has been cancelled
CI / Action matrix (4/16) (push) Has been cancelled
CI / Action matrix (5/16) (push) Has been cancelled
CI / Action matrix (6/16) (push) Has been cancelled
CI / Action matrix (7/16) (push) Has been cancelled
CI / Action matrix (8/16) (push) Has been cancelled
CI / Action matrix (9/16) (push) Has been cancelled
CI / Action matrix determinism (push) Has been cancelled
CI / Property tests (many cases) (push) Has been cancelled
CI / Doc tests (push) Has been cancelled
CI / Reference tests (push) Has been cancelled
CI / Windows D3D12 FXC shaders (push) Has been cancelled
CI / Windows app startup (push) Has been cancelled
CI / ci (push) Has been cancelled
CI / Compile WebAssembly (push) Has been cancelled
CI / Nightly alert (push) Has been cancelled
Pages / deploy (push) Has been cancelled
修改ai助手
2026-11-09 18:53:36 +08:00

1791 lines
81 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Browser-only transport for the original assistant. No process, server, or shared key.
use serde_json::{Value, json};
use solidcraft_ui_egui::{
ControlRequest,
agent_chat::{
acp::{self, ModelInfo},
agents::{self, Entry, Protocol},
api_wire, attach,
backend::{BrowserServices, ChatBackend, Event, PlatformAgentContext, PromptPart, TestError, ToolStatus},
},
};
use std::{
cell::{Cell, RefCell},
collections::VecDeque,
rc::Rc,
sync::mpsc::{self, Receiver, Sender},
};
use wasm_bindgen::{JsCast, JsValue, closure::Closure};
use wasm_bindgen_futures::{JsFuture, spawn_local};
const TOOLS: &[&str] = &[
"list_commands",
"describe_command",
"execute",
"inspect_design",
"measure",
"body_topology",
"set_parameter",
"batch",
"undo",
"redo",
"skill_reference",
];
const PLATFORM_TOOLS: &[&str] = &[
"list_commands",
"describe_command",
"execute",
"inspect_design",
"measure",
"body_topology",
"set_parameter",
"check_parameters",
"batch",
"screenshot",
"undo",
"redo",
"skill_reference",
];
const MAX_RESPONSE: usize = 16 * 1024 * 1024;
const MAX_SSE_EVENT: usize = 2 * 1024 * 1024;
thread_local! {
static NETWORK_GENERATION: Cell<u64> = const { Cell::new(0) };
static REQUESTS: RefCell<Vec<web_sys::AbortController>> = const { RefCell::new(Vec::new()) };
}
/// Account and document lifecycle also cancels connection tests and model-list requests.
pub fn cancel_all_requests() {
NETWORK_GENERATION.with(|generation| generation.set(generation.get().wrapping_add(1)));
REQUESTS.with(|requests| {
for request in requests.borrow_mut().drain(..) {
request.abort();
}
});
}
fn tr(key: &str) -> String {
solidcraft_ui_egui::i18n::tr(key).into()
}
fn failure(value: JsValue) -> String {
value.as_string().unwrap_or_else(|| tr("vm.agent.network_error"))
}
pub async fn delay(ms: i32) {
let promise = js_sys::Promise::new(&mut |resolve, _| {
if let Some(window) = web_sys::window() {
let _ = window.set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, ms);
}
});
let _ = JsFuture::from(promise).await;
}
/// A browser request has its own abort signal, no application credentials, no redirects.
async fn request(entry: &Entry, key: Option<&str>, path: &str, body: Option<&Value>, abort: &web_sys::AbortController) -> Result<Value, String> {
if entry.base_url.len() > 4096 || entry.model.len() > 1024 || key.is_some_and(|key| key.len() > 16_384) {
return Err(tr("vm.agent.bad_response"));
}
let base = agents::normalize_url(&entry.base_url)?;
if !agents::key_safe_for(&base) {
return Err(tr("ui.ai_key_needs_https"));
}
let parsed = url::Url::parse(&base).map_err(|_| tr("ui.ai_bad_url"))?;
if !parsed.username().is_empty() || parsed.password().is_some() || parsed.query().is_some() || parsed.fragment().is_some() {
return Err(tr("ui.ai_bad_url"));
}
let protocol = entry.kind.protocol().ok_or_else(|| tr("vm.agent.api_only"))?;
let url = format!("{}/{}", base.trim_end_matches('/'), path);
let init = web_sys::RequestInit::new();
init.set_method(if body.is_some() { "POST" } else { "GET" });
init.set_mode(web_sys::RequestMode::Cors);
init.set_credentials(web_sys::RequestCredentials::Omit);
init.set_redirect(web_sys::RequestRedirect::Error);
init.set_signal(Some(&abort.signal()));
if let Some(body) = body {
init.set_body(&JsValue::from_str(&body.to_string()));
}
let req = web_sys::Request::new_with_str_and_init(&url, &init).map_err(failure)?;
req.headers().set("Content-Type", "application/json").map_err(failure)?;
if protocol == Protocol::Anthropic {
req.headers().set("anthropic-version", "2023-06-01").map_err(failure)?;
req.headers().set("anthropic-dangerous-direct-browser-access", "true").map_err(failure)?;
}
if let Some(key) = key.filter(|key| !key.is_empty()) {
req.headers()
.set(
if protocol == Protocol::Anthropic { "x-api-key" } else { "Authorization" },
&if protocol == Protocol::Anthropic { key.into() } else { format!("Bearer {key}") },
)
.map_err(failure)?;
}
let window = web_sys::window().ok_or_else(|| tr("vm.agent.network_error"))?;
let timeout_abort = abort.clone();
let timed_out = Rc::new(Cell::new(false));
let timed = timed_out.clone();
let timer = Closure::<dyn FnMut()>::new(move || {
timed.set(true);
timeout_abort.abort();
});
let timeout = window
.set_timeout_with_callback_and_timeout_and_arguments_0(
timer.as_ref().unchecked_ref(),
(entry.request_timeout_s.clamp(30, 3600) * 1000) as i32,
)
.map_err(failure)?;
let scope = NETWORK_GENERATION.with(Cell::get);
REQUESTS.with(|requests| requests.borrow_mut().push(abort.clone()));
let result = async {
let response: web_sys::Response = JsFuture::from(window.fetch_with_request(&req)).await.map_err(failure)?.dyn_into().map_err(failure)?;
let status = response.status();
if status == 401 || status == 403 {
return Err(tr("vm.agent.auth_error"));
}
if status == 429 {
return Err(tr("vm.agent.rate_limit"));
}
if !response.ok() {
return Err(format!("{} (HTTP {status})", tr("vm.agent.service_error")));
}
// Bound the actual stream, including responses without Content-Length.
let stream = response.body().ok_or_else(|| tr("vm.agent.bad_response"))?;
let reader: web_sys::ReadableStreamDefaultReader = stream.get_reader().dyn_into().map_err(|value| failure(value.into()))?;
let mut bytes = Vec::new();
loop {
let chunk = JsFuture::from(reader.read()).await.map_err(failure)?;
if js_sys::Reflect::get(&chunk, &"done".into()).map_err(failure)?.as_bool() == Some(true) {
break;
}
let chunk = js_sys::Uint8Array::new(&js_sys::Reflect::get(&chunk, &"value".into()).map_err(failure)?);
if bytes.len().saturating_add(chunk.length() as usize) > MAX_RESPONSE {
abort.abort();
return Err(tr("vm.agent.bad_response"));
}
bytes.extend(chunk.to_vec());
}
let value: Value = serde_json::from_slice(&bytes).map_err(|_| tr("vm.agent.bad_response"))?;
let valid = if body.is_none() {
value.get("data").is_some()
} else {
match protocol {
Protocol::OpenAi => value.pointer("/choices/0/message").is_some(),
Protocol::Anthropic => value.get("content").is_some(),
}
};
if !valid {
return Err(tr("vm.agent.bad_response"));
}
Ok(value)
}
.await;
window.clear_timeout_with_handle(timeout);
REQUESTS.with(|requests| requests.borrow_mut().retain(|request| request != abort));
if scope != NETWORK_GENERATION.with(Cell::get) {
return Err("cancelled".into());
}
if timed_out.get() { Err(tr("vm.agent.timeout")) } else { result }
}
fn path(protocol: Protocol) -> &'static str {
if protocol == Protocol::Anthropic { "messages" } else { "chat/completions" }
}
fn catalog() -> Value {
json!({"tools":solidcraft_mcp::tool_definitions().into_iter().filter(|tool| tool["name"].as_str().is_some_and(|name| TOOLS.contains(&name))).collect::<Vec<_>>()})
}
fn protocol_tools(protocol: Protocol, catalog: &Value) -> Vec<Value> {
match protocol {
Protocol::OpenAi => api_wire::openai_tools(catalog),
Protocol::Anthropic => api_wire::anthropic_tools(catalog),
}
}
/// The same listing feeds the connection switcher and the settings model picker.
async fn model_list(entry: &Entry, key: Option<&str>, abort: &web_sys::AbortController) -> Result<Vec<ModelInfo>, String> {
let body = request(entry, key, "models", None, abort).await?;
let rows = body["data"].as_array().ok_or_else(|| tr("vm.agent.bad_response"))?;
let mut models: Vec<ModelInfo> = rows
.iter()
.take(512)
.filter_map(|model| {
let id: String = model["id"].as_str()?.chars().take(256).collect();
if id.trim().is_empty() {
return None;
}
let name = model["display_name"].as_str().or_else(|| model["name"].as_str()).filter(|name| !name.trim().is_empty()).unwrap_or(&id);
Some(ModelInfo { name: name.chars().take(256).collect(), id, description: String::new() })
})
.collect();
models.sort_by(|a, b| a.id.cmp(&b.id));
models.dedup_by(|a, b| a.id == b.id);
Ok(models)
}
use crate::agent_tools::{ControlReply, Replay};
async fn tool_call(
name: &str,
args: &Value,
tools: &Sender<ControlRequest>,
generation: &Cell<u64>,
turn: u64,
ctx: &egui::Context,
) -> Result<Value, String> {
platform_tool_call_allowed(name, args, tools, generation, turn, ctx, TOOLS).await
}
async fn platform_tool_call_allowed(
name: &str,
args: &Value,
tools: &Sender<ControlRequest>,
generation: &Cell<u64>,
turn: u64,
ctx: &egui::Context,
allowed: &[&str],
) -> Result<Value, String> {
if allowed == TOOLS && args.to_string().len() > 256 * 1024 {
return Err(tr("vm.agent.bad_response"));
}
if !allowed.contains(&name) {
return Err(tr("vm.agent.tool_unavailable"));
}
let mut results = Vec::new();
for _ in 0..4096 {
if generation.get() != turn {
return Err("cancelled".into());
}
let mut replay = Replay { results: &results, at: 0, next: None };
let result = solidcraft_mcp::call_tool(&mut replay, name, args);
let Some((method, params)) = replay.next else {
return Ok(result);
};
let sent_method = method.clone();
let sent_params = params.clone();
let (req, rx) = ControlRequest::new(method, params);
tools.send(req).map_err(|_| "document released".to_string())?;
ctx.request_repaint();
let deadline = js_sys::Date::now() + 330_000.0;
loop {
if generation.get() != turn {
return Err("cancelled".into());
}
match rx.try_recv() {
Ok(value) => {
results.push(ControlReply {
method: sent_method.clone(),
params: sent_params.clone(),
result: if value["ok"] == true {
Ok(value["result"].clone())
} else {
Err(value["error"].as_str().unwrap_or("tool failed").into())
},
});
break;
}
Err(mpsc::TryRecvError::Disconnected) => return Err("document released".into()),
Err(mpsc::TryRecvError::Empty) => {}
}
if js_sys::Date::now() > deadline {
return Err(tr("vm.agent.timeout"));
}
delay(20).await;
}
}
Err(tr("vm.agent.tool_unavailable"))
}
fn validate_schema(schema: &Value, value: &Value, path: &str, depth: usize) -> Result<(), String> {
if depth > 16 {
return Err(format!("{path}: schema nesting is too deep"));
}
let matches = |kind: &str| match kind {
"object" => value.is_object(),
"array" => value.is_array(),
"string" => value.is_string(),
"number" => value.is_number(),
"integer" => value.as_i64().is_some() || value.as_u64().is_some(),
"boolean" => value.is_boolean(),
"null" => value.is_null(),
_ => true,
};
match schema.get("type") {
Some(Value::String(kind)) if !matches(kind) => return Err(format!("{path}: must be {kind}")),
Some(Value::Array(kinds)) if !kinds.iter().filter_map(Value::as_str).any(matches) => {
return Err(format!("{path}: has an unsupported type"));
}
_ => {}
}
if let Some(options) = schema.get("enum").and_then(Value::as_array)
&& !options.contains(value)
{
return Err(format!("{path}: value is not allowed"));
}
if let Some(object) = value.as_object() {
let properties = schema.get("properties").and_then(Value::as_object);
for required in schema.get("required").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str) {
if !object.contains_key(required) || object.get(required).is_some_and(Value::is_null) {
return Err(format!("{path}.{required}: required"));
}
}
for (name, child) in object {
let child_schema = properties.and_then(|entries| entries.get(name));
if let Some(child_schema) = child_schema {
validate_schema(child_schema, child, &format!("{path}.{name}"), depth + 1)?;
} else if schema.get("additionalProperties") == Some(&Value::Bool(false)) {
return Err(format!("{path}.{name}: unknown property"));
} else if let Some(additional) = schema.get("additionalProperties").filter(|v| v.is_object()) {
validate_schema(additional, child, &format!("{path}.{name}"), depth + 1)?;
}
}
if schema.get("minProperties").and_then(Value::as_u64).is_some_and(|min| object.len() < min as usize) {
return Err(format!("{path}: too few properties"));
}
if schema.get("maxProperties").and_then(Value::as_u64).is_some_and(|max| object.len() > max as usize) {
return Err(format!("{path}: too many properties"));
}
}
if let Some(array) = value.as_array() {
if schema.get("minItems").and_then(Value::as_u64).is_some_and(|min| array.len() < min as usize)
|| schema.get("maxItems").and_then(Value::as_u64).is_some_and(|max| array.len() > max as usize)
{
return Err(format!("{path}: array length is outside the allowed range"));
}
if let Some(item_schema) = schema.get("items") {
for (index, item) in array.iter().enumerate() {
validate_schema(item_schema, item, &format!("{path}[{index}]"), depth + 1)?;
}
}
}
if let Some(text) = value.as_str()
&& (schema.get("minLength").and_then(Value::as_u64).is_some_and(|min| text.len() < min as usize)
|| schema.get("maxLength").and_then(Value::as_u64).is_some_and(|max| text.len() > max as usize))
{
return Err(format!("{path}: string length is outside the allowed range"));
}
if let Some(number) = value.as_f64()
&& (!number.is_finite()
|| schema.get("minimum").and_then(Value::as_f64).is_some_and(|min| number < min)
|| schema.get("maximum").and_then(Value::as_f64).is_some_and(|max| number > max))
{
return Err(format!("{path}: number is outside the allowed range"));
}
Ok(())
}
#[derive(Clone)]
struct PlatformCall {
code: String,
name: String,
arguments: Value,
}
struct PlatformCompletion {
finish_reason: String,
output: Vec<Value>,
}
fn sse_boundary(bytes: &[u8]) -> Option<(usize, usize)> {
let mut index = 0;
while index < bytes.len() {
if bytes.get(index..index + 2) == Some(b"\n\n") {
return Some((index, 2));
}
if bytes.get(index..index + 4) == Some(b"\r\n\r\n") {
return Some((index, 4));
}
index += 1;
}
None
}
fn sse_event(block: &[u8]) -> Result<Option<(String, Value)>, String> {
let text = std::str::from_utf8(block).map_err(|_| "CAD AI returned invalid UTF-8".to_string())?;
let mut event = "message".to_string();
let mut data = Vec::new();
for line in text.split('\n') {
let line = line.strip_suffix('\r').unwrap_or(line);
if line.starts_with(':') {
continue;
}
if let Some(value) = line.strip_prefix("event:") {
event = value.trim_start().to_string();
} else if let Some(value) = line.strip_prefix("data:") {
data.push(value.strip_prefix(' ').unwrap_or(value));
}
}
if data.is_empty() {
return Ok(None);
}
let envelope = serde_json::from_str(&data.join("\n")).map_err(|_| "CAD AI returned an invalid SSE event".to_string())?;
Ok(Some((event, envelope)))
}
fn protocol_code() -> &'static str {
"cad-agent-v1"
}
fn string_field<'a>(value: &'a Value, key: &str) -> Option<&'a str> {
value.get(key).and_then(Value::as_str)
}
fn valid_code(code: &str) -> bool {
!code.is_empty() && code.len() <= 64 && code.chars().all(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '-')
}
fn content_text(content: &[Value]) -> String {
content.iter().filter_map(|part| string_field(part, "text")).collect::<Vec<_>>().join("\n")
}
// Only positive limits advertised by the backend impose a quota. Missing/zero
// values do not acquire a second, hard-coded frontend ceiling.
fn platform_limit(limits: &Value, name: &str) -> Option<u64> {
limits.get(name).and_then(Value::as_u64).filter(|limit| *limit > 0)
}
fn exceeds_platform_limit(limits: &Value, name: &str, used: usize) -> bool {
platform_limit(limits, name).is_some_and(|limit| used as u64 > limit)
}
fn base64_size(data: &str) -> usize {
(data.len().saturating_mul(3) / 4).saturating_sub(data.bytes().rev().take_while(|byte| *byte == b'=').count().min(2))
}
fn platform_image(mime: &str, data: &str, limits: &Value) -> Result<(Value, u32, u32), String> {
if !matches!(mime, "image/png" | "image/jpeg" | "image/gif" | "image/webp") {
return Err("CAD AI images must use PNG or JPEG".into());
}
let binary = web_sys::window().ok_or_else(|| tr("vm.agent.network_error"))?.atob(data).map_err(failure)?;
let bytes: Vec<u8> = binary.chars().map(|byte| byte as u8).collect();
let mut reader = image::ImageReader::new(std::io::Cursor::new(&bytes)).with_guessed_format().map_err(|error| error.to_string())?;
let mut decode_limits = image::Limits::default();
decode_limits.max_image_width = Some(8192);
decode_limits.max_image_height = Some(8192);
decode_limits.max_alloc = Some(128 << 20);
reader.limits(decode_limits);
let mut picture = reader.decode().map_err(|error| error.to_string())?;
let max_edge = platform_limit(limits, "imageMaxEdge").unwrap_or(8192).min(8192) as u32;
if matches!(mime, "image/png" | "image/jpeg")
&& picture.width().max(picture.height()) <= max_edge
&& !exceeds_platform_limit(limits, "imageBytes", bytes.len())
{
return Ok((json!({"type":"image","mimeType":mime,"data":data}), picture.width(), picture.height()));
}
if picture.width().max(picture.height()) > max_edge {
picture = picture.resize(max_edge, max_edge, image::imageops::FilterType::Triangle);
}
loop {
let mut encoded = Vec::new();
picture.write_to(&mut std::io::Cursor::new(&mut encoded), image::ImageFormat::Png).map_err(|error| error.to_string())?;
if !exceeds_platform_limit(limits, "imageBytes", encoded.len()) {
return Ok((
json!({"type":"image","mimeType":"image/png",
"data":solidcraft_ui_egui::agent_chat::backend::base64(&encoded)}),
picture.width(),
picture.height(),
));
}
if picture.width().max(picture.height()) <= 1 {
return Err("CAD AI screenshot cannot fit backend imageBytes even at one pixel; no image was sent".into());
}
picture = picture.resize((picture.width() / 2).max(1), (picture.height() / 2).max(1), image::imageops::FilterType::Triangle);
}
}
fn validate_content(content: &Value, allow_image: bool, limits: &Value) -> Result<(), String> {
let blocks = content.as_array().ok_or_else(|| "CAD AI content must be an array".to_string())?;
if blocks.is_empty() || blocks.len() > 16 {
return Err("CAD AI content block limit exceeded".into());
}
for block in blocks {
match string_field(block, "type") {
Some("text") => {
let text = string_field(block, "text").ok_or_else(|| "CAD AI text block is invalid".to_string())?;
if text.is_empty() || exceeds_platform_limit(limits, "textBytes", text.len()) {
return Err("CAD AI text limit exceeded".into());
}
}
Some("image") if allow_image => {
let mime = string_field(block, "mimeType").ok_or_else(|| "CAD AI image block is invalid".to_string())?;
let data = string_field(block, "data").ok_or_else(|| "CAD AI image block is invalid".to_string())?;
let decoded = base64_size(data);
if !matches!(mime, "image/png" | "image/jpeg") || exceeds_platform_limit(limits, "imageBytes", decoded) || data.starts_with("data:") {
return Err("CAD AI image limit or format is invalid".into());
}
}
_ => return Err("CAD AI content block type is not supported".into()),
}
}
Ok(())
}
fn trim_platform_history(history: &mut Vec<Value>, max_items: usize) {
while history.len() > max_items {
let next_user =
history.iter().enumerate().skip(1).find(|(_, item)| item["type"] == "message" && item["role"] == "user").map(|(index, _)| index);
let Some(end) = next_user else { break };
history.drain(..end);
}
}
struct PlatformBackend {
context: PlatformAgentContext,
limits: Value,
tools: std::collections::BTreeMap<String, Value>,
sender: Sender<ControlRequest>,
ctx: egui::Context,
events: Rc<RefCell<VecDeque<Event>>>,
history: Rc<RefCell<Vec<Value>>>,
generation: Rc<Cell<u64>>,
abort: Rc<RefCell<Option<web_sys::AbortController>>>,
}
fn random_code() -> Result<String, String> {
let window = web_sys::window().ok_or_else(|| tr("vm.agent.network_error"))?;
let crypto = js_sys::Reflect::get(&window, &"crypto".into()).map_err(failure)?;
let random_uuid: js_sys::Function = js_sys::Reflect::get(&crypto, &"randomUUID".into()).map_err(failure)?.dyn_into().map_err(failure)?;
random_uuid.call0(&crypto).map_err(failure)?.as_string().ok_or_else(|| tr("vm.agent.network_error"))
}
fn safe_platform_tools(context: &PlatformAgentContext) -> std::collections::BTreeMap<String, Value> {
context.capabilities["tools"]
.as_array()
.into_iter()
.flatten()
.filter_map(|tool| {
let name = string_field(tool, "name")?;
if !PLATFORM_TOOLS.contains(&name) {
return None;
}
let parameters = tool.get("parameters")?.as_object()?;
if parameters.get("type").and_then(Value::as_str) != Some("object") {
return None;
}
Some((name.to_string(), tool.clone()))
})
.collect()
}
fn item_call(item: &Value) -> Result<PlatformCall, String> {
if item["type"] != "toolCall" {
return Err("CAD AI completed output contains a non-tool item".into());
}
let code = string_field(item, "callCode").ok_or_else(|| "CAD AI tool call is missing callCode".to_string())?;
let name = string_field(item, "name").ok_or_else(|| "CAD AI tool call is missing its name".to_string())?;
let arguments =
item.get("arguments").filter(|value| value.is_object()).cloned().ok_or_else(|| "CAD AI tool arguments must be an object".to_string())?;
if !valid_code(code) || name.len() > 64 {
return Err("CAD AI tool call identifier or name is invalid".into());
}
Ok(PlatformCall { code: code.to_string(), name: name.to_string(), arguments })
}
fn validate_output(output: &Value, finish: &str, limits: &Value) -> Result<Vec<PlatformCall>, String> {
let items = output.as_array().ok_or_else(|| "CAD AI completed output must be an array".to_string())?;
if items.is_empty() {
return Err("CAD AI completed output is empty".into());
}
let mut calls = Vec::new();
for item in items {
match string_field(item, "type") {
Some("message") => {
if string_field(item, "role") != Some("assistant") {
return Err("CAD AI output may only contain assistant messages".into());
}
validate_content(&item["content"], false, limits)?;
}
Some("toolCall") if finish == "tool_calls" => calls.push(item_call(item)?),
_ => return Err("CAD AI completed output has an invalid item".into()),
}
}
if (finish == "tool_calls") != !calls.is_empty() {
return Err("CAD AI finishReason does not match its output".into());
}
let mut seen = std::collections::BTreeSet::new();
if calls.iter().any(|call| !seen.insert(call.code.as_str())) {
return Err("CAD AI output repeats a callCode".into());
}
Ok(calls)
}
fn history_call_codes(history: &[Value]) -> std::collections::BTreeSet<String> {
history.iter().filter(|item| item["type"] == "toolCall").filter_map(|item| string_field(item, "callCode").map(str::to_string)).collect()
}
fn history_usage(history: &[Value]) -> (usize, usize) {
let mut text_bytes = 0usize;
let mut images = 0usize;
for item in history {
if let Some(content) = item.get("content").and_then(Value::as_array) {
for part in content {
if part["type"] == "text" {
text_bytes = text_bytes.saturating_add(string_field(part, "text").map_or(0, str::len));
} else if part["type"] == "image" {
images += 1;
}
}
}
if item["type"] == "toolCall" {
text_bytes = text_bytes.saturating_add(item["arguments"].to_string().len());
}
}
(text_bytes, images)
}
fn reserve_platform_image_slot(history: &mut Vec<Value>, limits: &Value) {
let Some(limit) = platform_limit(limits, "imagesPerRequest") else { return };
let Some(current) = history.iter().rposition(|item| item["type"] == "message" && item["role"] == "user") else { return };
// A new user message may need another screenshot. Free an old image only by
// removing its entire completed round, keeping all current call/result pairs.
if history_usage(&history[current..]).1 as u64 >= limit {
return;
}
while history_usage(history).1 as u64 >= limit {
let before = history.len();
trim_platform_history(history, before.saturating_sub(1));
if history.len() == before {
break;
}
}
}
fn platform_request(history: &[Value], context: &PlatformAgentContext, turn: &str) -> Value {
// The host replaces this placeholder with the current revision's SHA-256.
// Account for its real wire length when measuring the request, too.
json!({"protocolVersion":protocol_code(), "documentSessionCode":context.document_session_code,
"turnCode":turn, "cadProjectCode":context.cad_project_code, "history":history,
"context":{"documentRevision":"0".repeat(64)}})
}
fn platform_request_limits(request: &Value, limits: &Value) -> Vec<String> {
let history = request["history"].as_array().map(Vec::as_slice).unwrap_or_default();
let (text, images) = history_usage(history);
let text = text.saturating_add(request["context"].to_string().len());
[("requestBytes", request.to_string().len()), ("textBytes", text), ("imagesPerRequest", images), ("historyItems", history.len())]
.into_iter()
.filter_map(|(name, used)| platform_limit(limits, name).filter(|limit| used as u64 > *limit).map(|limit| format!("{name}={used}/{limit}")))
.collect()
}
fn platform_model_request(
history: &[Value],
context: &PlatformAgentContext,
turn: &str,
limits: &Value,
tools: &std::collections::BTreeMap<String, Value>,
) -> Result<Value, String> {
let mut request = platform_request(history, context, turn);
let (text, images) = history_usage(history);
let note = json!({"cadWebRuntime":{
"backendLimits":limits,
"historyUsageBeforeThisNote":{"requestBytes":request.to_string().len(),"textBytes":text,"images":images,"historyItems":history.len()},
"remainingImages":platform_limit(limits,"imagesPerRequest").map(|limit| limit.saturating_sub(images as u64)),
"availableTools":tools.keys().collect::<Vec<_>>(),
"rules":"Respect these backend limits on every request, including all retained history and tool results. Missing or zero limits impose no frontend quota. There is no frontend per-turn model-call or change quota. Do not request screenshots when remainingImages is zero; inspect_design, measure and body_topology can verify the model without images. Screenshot source must be model; never request window, path, local files, network access or unavailable tools. Give view or camera, not both. The CAD renderer accepts dimensions from 16 to 8192 pixels, further bounded by imageMaxEdge when advertised. Keep tool queries and batch output small enough for toolResultTextBytes. Leave room for the next tool calls/results and this runtime note within textBytes, requestBytes and historyItems; finish with a concise progress report and ask the user to continue in a new message before exhausting them. Never retry a forbidden operation unchanged. Request pacing is automatic; do not waste tools to wait."
}}).to_string();
// Put runtime context in an existing user content block, never an unrecognised
// request field or a client-supplied system prompt. Only the wire copy changes:
// updated budgets replace the note on each request instead of accumulating.
let content = request["history"]
.as_array_mut()
.and_then(|items| items.iter_mut().find(|item| item["type"] == "message" && item["role"] == "user"))
.and_then(|item| item.get_mut("content"))
.and_then(Value::as_array_mut)
.ok_or_else(|| "CAD AI history must start with a user message".to_string())?;
if let Some(block) = content.iter_mut().find(|block| string_field(block, "type") == Some("text")) {
let original = string_field(block, "text").unwrap_or_default();
block["text"] = json!(format!("{original}\n\n[CAD Web runtime context]\n{note}"));
} else if content.len() < 16 {
content.push(json!({"type":"text","text":note}));
} else {
return Err("CAD AI user message has no room for runtime context".into());
}
Ok(request)
}
fn platform_tool_budget(name: &str, history: &[Value], limits: &Value) -> Option<Value> {
if name != "screenshot" {
return None;
}
let (_, images) = history_usage(history);
let limit = platform_limit(limits, "imagesPerRequest")?;
if (images as u64) < limit {
return None;
}
Some(json!({"isError":true,"content":[{"type":"text","text":format!(
"Screenshot not executed: backend imagesPerRequest={limit}, retained images={images}, remainingImages=0. The view and model were not changed. Do not call screenshot again in this turn. Use inspect_design, measure or body_topology, or ask the user to continue in a new message if another image is essential."
)}]}))
}
fn prompt_history(parts: &[PromptPart], limits: &Value) -> Result<Value, String> {
let mut content = Vec::new();
for part in parts {
match part {
PromptPart::Text(text) if !text.is_empty() => content.push(json!({"type":"text","text":text})),
PromptPart::Text(_) => {}
PromptPart::Image { mime, base64 } => {
content.push(platform_image(mime, base64, limits)?.0);
}
PromptPart::Document { .. } => return Err("CAD AI does not accept PDF attachments yet".into()),
}
}
validate_content(&Value::Array(content.clone()), true, limits)?;
Ok(json!({"type":"message","role":"user","content":content}))
}
async fn platform_control(
method: &str,
params: Value,
sender: &Sender<ControlRequest>,
generation: &Cell<u64>,
turn: u64,
ctx: &egui::Context,
) -> Result<Value, String> {
if generation.get() != turn {
return Err("cancelled".into());
}
let (request, reply) = ControlRequest::new(method, params);
sender.send(request).map_err(|_| "document released".to_string())?;
ctx.request_repaint();
let deadline = js_sys::Date::now() + 330_000.0;
loop {
match reply.try_recv() {
Ok(result) => {
if result["ok"] == true {
return Ok(result["result"].clone());
}
return Err(result["error"].as_str().unwrap_or("CAD command failed").to_string());
}
Err(mpsc::TryRecvError::Disconnected) => return Err("document released".into()),
Err(mpsc::TryRecvError::Empty) => {}
}
if js_sys::Date::now() >= deadline {
return Err(tr("vm.agent.timeout"));
}
delay(20).await;
}
}
async fn platform_screenshot(
args: &Value,
sender: &Sender<ControlRequest>,
generation: &Cell<u64>,
turn: u64,
ctx: &egui::Context,
limits: &Value,
) -> Result<Value, String> {
let max_edge = platform_limit(limits, "imageMaxEdge").unwrap_or(8192).min(8192);
if max_edge < 16 {
return Err("CAD AI screenshot imageMaxEdge is smaller than the renderer's minimum size (16 pixels)".into());
}
let width = args["width"].as_u64().unwrap_or(960.min(max_edge));
let height = args["height"].as_u64().unwrap_or(600.min(max_edge));
if !(16..=max_edge).contains(&width) || !(16..=max_edge).contains(&height) {
return Err(format!("CAD AI screenshot dimensions must be between 16 and {max_edge} pixels; rendering was not started"));
}
if args.get("path").is_some_and(|value| !value.is_null()) || string_field(args, "source").is_some_and(|source| source != "model") {
return Err("CAD AI screenshot only supports the in-memory model view".into());
}
if args.get("view").is_some_and(|value| !value.is_null()) && args.get("camera").is_some_and(|value| !value.is_null()) {
return Err("CAD AI screenshot accepts either view or camera".into());
}
if let Some(camera) = args.get("camera").filter(|value| !value.is_null()) {
platform_control("engine.execute", json!({"command":"view.apply","params":{"camera":camera}}), sender, generation, turn, ctx).await?;
} else if let Some(view) = string_field(args, "view") {
if ["iso", "home", "front", "back", "top", "bottom", "left", "right", "fit"].contains(&view) {
platform_control("ui.view", json!({"view":view}), sender, generation, turn, ctx).await?;
if !matches!(view, "fit" | "home") {
platform_control("ui.view", json!({"view":"fit"}), sender, generation, turn, ctx).await?;
}
} else {
// As in the native screenshot tool, resolve saved views and standard-view
// aliases (for example isometric) through the engine's view command.
platform_control("engine.execute", json!({"command":"view.apply","params":{"view":view}}), sender, generation, turn, ctx).await?;
}
}
let rendered = platform_control(
"ui.render",
json!({"width":width,"height":height,"grid":args["grid"],"axes":args["axes"],"inline":true}),
sender,
generation,
turn,
ctx,
)
.await?;
let data = string_field(&rendered, "pngBase64").ok_or_else(|| "CAD screenshot renderer returned no image".to_string())?;
let (image, width, height) = platform_image("image/png", data, limits)?;
Ok(json!({"content":[image,
{"type":"text","text":format!("CAD model view screenshot, {width} × {height} pixels") }]}))
}
async fn platform_tool(
name: &str,
arguments: &Value,
schema: &Value,
sender: &Sender<ControlRequest>,
generation: &Cell<u64>,
turn: u64,
ctx: &egui::Context,
limits: &Value,
) -> Result<Value, String> {
let params = schema.get("parameters").ok_or_else(|| "CAD AI tool schema is missing".to_string())?;
validate_schema(params, arguments, name, 0)?;
if name == "screenshot" {
return match platform_screenshot(arguments, sender, generation, turn, ctx, limits).await {
Err(error)
if error.starts_with("invalid parameters for `view.apply`:")
|| error.starts_with("unknown view `")
|| error.starts_with("CAD AI screenshot ") =>
{
Ok(json!({"isError":true,"content":[{"type":"text","text":error},
{"type":"text","text":"Correct the screenshot arguments. Use iso, home, front, back, top, bottom, left, right, fit, or an existing saved view name. Omit view and camera to capture the current view. Supply either view or camera, not both; source must be model."}]}))
}
result => result,
};
}
platform_tool_call_allowed(name, arguments, sender, generation, turn, ctx, PLATFORM_TOOLS).await
}
async fn platform_request_step(
request: Value,
context: &PlatformAgentContext,
schemas: &std::collections::BTreeMap<String, Value>,
abort: &web_sys::AbortController,
on_start: &js_sys::Function,
generation: &Cell<u64>,
turn: u64,
events: &Rc<RefCell<VecDeque<Event>>>,
ctx: &egui::Context,
) -> Result<(PlatformCompletion, Vec<PlatformCall>), String> {
if generation.get() != turn {
return Err("cancelled".into());
}
let window = web_sys::window().ok_or_else(|| tr("vm.agent.network_error"))?;
let host = js_sys::Reflect::get(&window, &"__VIBEMAKE_CAD__".into()).map_err(failure)?;
let callback: js_sys::Function = js_sys::Reflect::get(&host, &"agentChat".into()).map_err(failure)?.dyn_into().map_err(failure)?;
let request_js = js_sys::JSON::parse(&request.to_string()).map_err(failure)?;
let promise: js_sys::Promise = callback.call3(&host, &request_js, &abort.signal(), on_start).map_err(failure)?.dyn_into().map_err(failure)?;
let response: web_sys::Response = JsFuture::from(promise).await.map_err(failure)?.dyn_into().map_err(failure)?;
if generation.get() != turn {
return Err("cancelled".into());
}
if !response.ok() {
let status = response.status();
let message = JsFuture::from(response.json().map_err(failure)?)
.await
.ok()
.and_then(|value| js_sys::Reflect::get(&value, &"message".into()).ok()?.as_string());
return Err(message.unwrap_or_else(|| format!("CAD AI request failed (HTTP {status})")));
}
if !response.headers().get("content-type").map_err(failure)?.unwrap_or_default().to_ascii_lowercase().contains("text/event-stream") {
return Err("CAD AI did not return an SSE stream".into());
}
let stream = response.body().ok_or_else(|| "CAD AI stream is empty".to_string())?;
let reader: web_sys::ReadableStreamDefaultReader = stream.get_reader().dyn_into().map_err(|value| failure(value.into()))?;
let mut buffer = Vec::<u8>::new();
let mut response_bytes = 0usize;
let mut request_id: Option<String> = None;
let mut announced = Vec::<PlatformCall>::new();
let mut streamed_text = String::new();
let mut completed = None;
let result = async {
loop {
if generation.get() != turn {
return Err("cancelled".into());
}
let chunk = JsFuture::from(reader.read()).await.map_err(failure)?;
if js_sys::Reflect::get(&chunk, &"done".into()).map_err(failure)?.as_bool() == Some(true) {
break;
}
let chunk = js_sys::Uint8Array::new(&js_sys::Reflect::get(&chunk, &"value".into()).map_err(failure)?);
response_bytes = response_bytes.saturating_add(chunk.length() as usize);
if response_bytes > MAX_RESPONSE {
return Err("CAD AI response exceeds its size limit".into());
}
buffer.extend(chunk.to_vec());
loop {
let Some((at, delimiter)) = sse_boundary(&buffer) else { break };
if at > MAX_SSE_EVENT {
return Err("CAD AI SSE event exceeds its size limit".into());
}
let block = buffer.drain(..at).collect::<Vec<_>>();
buffer.drain(..delimiter);
let Some((event, envelope)) = sse_event(&block)? else { continue };
if string_field(&envelope, "code") != Some("OK") {
return Err(string_field(&envelope, "message").unwrap_or("CAD AI returned an error").to_string());
}
let id = string_field(&envelope, "requestId")
.filter(|id| !id.is_empty() && id.len() <= 128)
.ok_or_else(|| "CAD AI SSE requestId is missing".to_string())?;
if request_id.as_deref().is_some_and(|previous| previous != id) {
return Err("CAD AI SSE requestId changed during the response".into());
}
request_id = Some(id.to_string());
let data = envelope.get("data").ok_or_else(|| "CAD AI SSE data is missing".to_string())?;
if string_field(data, "documentSessionCode") != Some(&context.document_session_code)
|| string_field(data, "turnCode") != string_field(&request, "turnCode")
{
return Err("CAD AI SSE event belongs to another document or turn".into());
}
match event.as_str() {
"text_delta" => {
let text =
string_field(data, "text").filter(|text| !text.is_empty()).ok_or_else(|| "CAD AI text delta is invalid".to_string())?;
streamed_text.push_str(text);
if exceeds_platform_limit(&context.capabilities["limits"], "textBytes", streamed_text.len()) {
return Err("CAD AI text limit exceeded".into());
}
events.borrow_mut().push_back(Event::Text { thought: false, text: text.to_string() });
ctx.request_repaint();
}
"tool_call" => {
let call = PlatformCall {
code: string_field(data, "callCode")
.filter(|code| valid_code(code))
.ok_or_else(|| "CAD AI callCode is invalid".to_string())?
.to_string(),
name: string_field(data, "name").ok_or_else(|| "CAD AI tool name is missing".to_string())?.to_string(),
arguments: data
.get("arguments")
.filter(|args| args.is_object())
.cloned()
.ok_or_else(|| "CAD AI tool arguments are invalid".to_string())?,
};
if !schemas.contains_key(&call.name) || announced.iter().any(|old| old.code == call.code) {
return Err("CAD AI announced an unknown or duplicate tool call".into());
}
if let Some(schema) = schemas.get(&call.name) {
validate_schema(&schema["parameters"], &call.arguments, &call.name, 0)?;
}
let id = format!("{}:{}", string_field(&request, "turnCode").unwrap_or("turn"), call.code);
events.borrow_mut().push_back(Event::Tool {
id,
title: Some(call.name.clone()),
kind: None,
status: Some(ToolStatus::Pending),
input: Some(call.arguments.clone()),
output: None,
});
ctx.request_repaint();
announced.push(call);
}
"completed" => {
let finish = string_field(data, "finishReason").ok_or_else(|| "CAD AI completed event has no finishReason".to_string())?;
if !matches!(finish, "stop" | "tool_calls") {
return Err("CAD AI completed event has an unsupported finishReason".into());
}
completed = Some(PlatformCompletion {
finish_reason: finish.to_string(),
output: data
.get("output")
.and_then(Value::as_array)
.cloned()
.ok_or_else(|| "CAD AI completed output is missing".to_string())?,
});
return Ok(());
}
"error" => return Err(string_field(&envelope, "message").unwrap_or("CAD AI request failed").to_string()),
_ => return Err(format!("CAD AI sent unsupported SSE event `{event}`")),
}
}
if buffer.len() > MAX_SSE_EVENT {
return Err("CAD AI SSE event exceeds its size limit".into());
}
}
Err("CAD AI stream ended before completed".into())
}
.await;
let _ = reader.cancel();
reader.release_lock();
result?;
let completion = completed.ok_or_else(|| "CAD AI stream ended before completed".to_string())?;
let output_text = completion
.output
.iter()
.filter(|item| item["type"] == "message")
.flat_map(|item| item["content"].as_array().into_iter().flatten())
.filter_map(|part| string_field(part, "text"))
.collect::<String>();
if !streamed_text.is_empty() && streamed_text != output_text {
return Err("CAD AI completed output does not match its streamed text".into());
}
if streamed_text.is_empty() && !output_text.is_empty() {
events.borrow_mut().push_back(Event::Text { thought: false, text: output_text });
ctx.request_repaint();
}
let calls = validate_output(&Value::Array(completion.output.clone()), &completion.finish_reason, &context.capabilities["limits"])?;
if calls.len() != announced.len()
|| calls
.iter()
.zip(&announced)
.any(|(output, announced)| output.code != announced.code || output.name != announced.name || output.arguments != announced.arguments)
{
return Err("CAD AI completed output does not match its tool_call events".into());
}
Ok((completion, calls))
}
fn tool_result_content(result: &Value, limits: &Value) -> Result<(Vec<Value>, bool), String> {
let mut is_error = result["isError"] == true;
let mut content =
result.get("content").and_then(Value::as_array).cloned().unwrap_or_else(|| vec![json!({"type":"text","text":result.to_string()})]);
let mut text_total = 0usize;
for block in &content {
match string_field(block, "type") {
Some("text") => text_total = text_total.saturating_add(string_field(block, "text").map_or(0, str::len)),
Some("image") => {
let data = string_field(block, "data").ok_or_else(|| "CAD tool returned invalid image data".to_string())?;
if !matches!(string_field(block, "mimeType"), Some("image/png" | "image/jpeg"))
|| exceeds_platform_limit(limits, "imageBytes", base64_size(data))
{
return Err("CAD tool image exceeds the platform limit".into());
}
}
_ => return Err("CAD tool returned an unsupported result block".into()),
}
}
if let Some(limit) = platform_limit(limits, "toolResultTextBytes").filter(|limit| text_total as u64 > *limit) {
// The command has already run. Report a delivery failure without sending
// partial JSON or inviting a replay of an already applied modification.
let feedback = json!({
"error":"tool_result_too_large", "textBytes":text_total, "toolResultTextBytes":limit,
"toolReportedError":is_error,
"recovery":"The tool ran, but its full result was withheld because it exceeds the backend text limit. No partial JSON was sent. Do not infer success or failure of individual changes from this delivery error and never replay modifications. Inspect the current design with a smaller query or read-only measurement before continuing."
}).to_string();
if feedback.len() as u64 > limit {
return Err(format!(
"CAD AI tool result textBytes={text_total} exceeds backend toolResultTextBytes={limit}; the full result was withheld. Inspect with a smaller query; do not replay modifications."
));
}
content = vec![json!({"type":"text","text":feedback})];
is_error = true;
}
Ok((content, is_error))
}
async fn platform_turn(
parts: Vec<PromptPart>,
context: PlatformAgentContext,
tools: std::collections::BTreeMap<String, Value>,
limits: Value,
sender: Sender<ControlRequest>,
ctx: egui::Context,
events: Rc<RefCell<VecDeque<Event>>>,
history: Rc<RefCell<Vec<Value>>>,
generation: Rc<Cell<u64>>,
abort_slot: Rc<RefCell<Option<web_sys::AbortController>>>,
turn: u64,
) {
let result = async {
let mut next = history.borrow().clone();
let user = prompt_history(&parts, &limits)?;
next.push(user);
let turn_code = random_code()?;
// As in the native assistant, a turn runs until completion, Stop or an error.
// Per-request resource and protocol limits still apply to each iteration.
loop {
if generation.get() != turn {
return Ok(());
}
if let Some(max_items) = platform_limit(&limits, "historyItems") {
trim_platform_history(&mut next, usize::try_from(max_items).unwrap_or(usize::MAX));
}
reserve_platform_image_slot(&mut next, &limits);
let request = loop {
let request = platform_model_request(&next, &context, &turn_code, &limits, &tools)?;
let exceeded = platform_request_limits(&request, &limits);
if exceeded.is_empty() {
break request;
}
let before = next.len();
let keep = next.len().saturating_sub(1);
trim_platform_history(&mut next, keep);
if next.len() == before {
return Err(solidcraft_ui_egui::i18n::format("vm.agent.request_limits", &[("limits", &exceeded.join(", "))]));
}
};
let controller = web_sys::AbortController::new().map_err(failure)?;
*abort_slot.borrow_mut() = Some(controller.clone());
let window = web_sys::window().ok_or_else(|| tr("vm.agent.network_error"))?;
let timeout_abort = controller.clone();
let timed_out = Rc::new(Cell::new(false));
let timeout_state = timed_out.clone();
let timer = Closure::<dyn FnMut()>::new(move || {
timeout_state.set(true);
timeout_abort.abort();
});
let timeout_ms = platform_limit(&limits, "requestTimeoutSeconds").map(|seconds| seconds.saturating_mul(1000).min(i32::MAX as u64) as i32);
// The host queues requests by account. Start the network/stream timeout only
// when fetch begins, so waiting for the next minute is cancellable, not a timeout.
let timeout = Rc::new(Cell::new(None));
let start_timeout = timeout.clone();
let start_window = window.clone();
let start_timer = timer.as_ref().clone();
let on_start = Closure::<dyn FnMut() -> Result<(), JsValue>>::new(move || {
if let Some(timeout_ms) = timeout_ms.filter(|_| start_timeout.get().is_none()) {
let id = start_window.set_timeout_with_callback_and_timeout_and_arguments_0(start_timer.unchecked_ref(), timeout_ms)?;
start_timeout.set(Some(id));
}
Ok(())
});
let step = platform_request_step(request, &context, &tools, &controller, on_start.as_ref().unchecked_ref(), &generation, turn, &events, &ctx).await;
if let Some(timeout) = timeout.get() {
window.clear_timeout_with_handle(timeout);
}
*abort_slot.borrow_mut() = None;
if timed_out.get() {
return Err(tr("vm.agent.timeout"));
}
if generation.get() != turn {
return Ok(());
}
let (completion, calls) = step?;
let used_codes = history_call_codes(&next);
if calls.iter().any(|call| used_codes.contains(&call.code)) {
return Err("CAD AI reused a callCode from this conversation".into());
}
next.extend(completion.output.clone());
if completion.finish_reason == "stop" {
*history.borrow_mut() = next;
return Ok(());
}
for call in calls {
if generation.get() != turn {
return Ok(());
}
let schema = tools.get(&call.name).ok_or_else(|| "CAD AI requested a tool that is not available".to_string())?;
let id = format!("{turn_code}:{}", call.code);
let result = match platform_tool_budget(&call.name, &next, &limits) {
Some(blocked) => Ok(blocked),
None => match validate_schema(&schema["parameters"], &call.arguments, &call.name, 0) {
Ok(()) => platform_tool(&call.name, &call.arguments, schema, &sender, &generation, turn, &ctx, &limits).await,
Err(error) => Ok(json!({"isError":true,"content":[{"type":"text","text":error}]})),
},
};
let (mut content, failed) = match result {
Ok(value) => tool_result_content(&value, &limits)?,
Err(error) if error == "cancelled" => return Ok(()),
Err(error) => return Err(error),
};
// This is execution feedback for the next model call, paired with the original
// error. The model can inspect and correct arguments without replaying completed
// steps. Refusals and stale contexts still stop recovery.
if failed {
let feedback = json!({"recovery":"Analyse the tool error. Inspect the current design or command schema before correcting the failed step. Respect applied/not_run/rolled_back; never repeat already applied steps. Retry with corrected arguments. Do not retry a user denial, a stale context or a server-enforced limit."}).to_string();
let size = content.iter().filter_map(|part| string_field(part, "text")).map(str::len).sum::<usize>();
if !exceeds_platform_limit(&limits, "toolResultTextBytes", size.saturating_add(feedback.len())) {
content.push(json!({"type":"text","text":feedback}));
}
}
let result_value = json!({"content":content,"isError":failed});
let output = content_text(&content).chars().take(20_000).collect::<String>();
events.borrow_mut().push_back(Event::Tool {
id,
title: Some(call.name.clone()),
kind: None,
status: Some(if failed { ToolStatus::Failed } else { ToolStatus::Completed }),
input: None,
output: Some(if output.is_empty() { "CAD screenshot captured".into() } else { output }),
});
ctx.request_repaint();
next.push(json!({"type":"toolResult","callCode":call.code,"isError":failed,"content":result_value["content"]}));
}
*history.borrow_mut() = next.clone();
}
}
.await;
if generation.get() != turn {
return;
}
match result {
Ok(()) => events.borrow_mut().push_back(Event::TurnEnded { reason: String::new() }),
Err(error) => {
events.borrow_mut().push_back(Event::Error(error));
events.borrow_mut().push_back(Event::TurnEnded { reason: "error".into() });
}
}
abort_slot.borrow_mut().take();
ctx.request_repaint();
}
impl PlatformBackend {
fn emit(&self, event: Event) {
self.events.borrow_mut().push_back(event);
self.ctx.request_repaint();
}
fn abort_pending(&self) {
self.generation.set(self.generation.get().wrapping_add(1));
if let Some(controller) = self.abort.borrow_mut().take() {
controller.abort();
}
}
}
impl ChatBackend for PlatformBackend {
fn poll(&mut self) -> Vec<Event> {
self.events.borrow_mut().drain(..).collect()
}
fn prompt(&mut self, parts: &[PromptPart]) {
let generation = self.generation.clone();
let turn = generation.get().wrapping_add(1);
generation.set(turn);
let context = self.context.clone();
let tools = self.tools.clone();
let limits = self.limits.clone();
let sender = self.sender.clone();
let ctx = self.ctx.clone();
let events = self.events.clone();
let history = self.history.clone();
let abort = self.abort.clone();
spawn_local(platform_turn(parts.to_vec(), context, tools, limits, sender, ctx, events, history, generation, abort, turn));
}
fn cancel(&mut self) {
self.abort_pending();
self.emit(Event::TurnEnded { reason: "cancelled".into() });
}
fn new_session(&mut self) {
self.abort_pending();
self.history.borrow_mut().clear();
let model = string_field(&self.context.capabilities, "model").unwrap_or("VibeMake CAD Agent").to_string();
self.emit(Event::Ready { agent: model, images: true });
}
fn answer_permission(&mut self, _key: u64, _option: Option<&str>) {}
fn authenticate(&mut self, _method: &str) {}
fn shutdown(&mut self, _wait: bool) {
self.abort_pending();
}
}
impl Drop for PlatformBackend {
fn drop(&mut self) {
self.shutdown(false);
}
}
fn platform_limits(input: &Value) -> Value {
[
"requestBytes",
"imageBytes",
"imageMaxEdge",
"imagesPerRequest",
"textBytes",
"toolResultTextBytes",
"historyItems",
"requestTimeoutSeconds",
"requestsPerMinute",
]
.into_iter()
.filter_map(|name| platform_limit(input, name).map(|limit| (name.to_string(), json!(limit))))
.collect::<serde_json::Map<_, _>>()
.into()
}
/// A harmless model request proves the actual tool-call shape, not just /models access.
async fn probe(entry: &Entry, key: Option<&str>, abort: &web_sys::AbortController) -> Result<bool, String> {
let protocol = entry.kind.protocol().ok_or_else(|| tr("vm.agent.api_only"))?;
if entry.model.trim().is_empty() {
return Err(tr("ui.ai_need_model"));
}
let list = json!({"tools":[{"name":"cad_connection_test","description":"Return ok=true. Connection test only; no model changes.","inputSchema":{"type":"object","properties":{"ok":{"type":"boolean"}},"required":["ok"]}}]});
let history = vec![json!({"role":"user","content":"Call cad_connection_test with ok=true. This only checks API compatibility."})];
let mut body =
api_wire::request_body(protocol, &entry.model, &history, &protocol_tools(protocol, &list), &entry.reasoning_effort, false, acp::SYSTEM_NOTE);
body["tool_choice"] = match protocol {
Protocol::OpenAi => json!({"type":"function","function":{"name":"cad_connection_test"}}),
Protocol::Anthropic => json!({"type":"tool","name":"cad_connection_test"}),
};
match request(entry, key, path(protocol), Some(&body), abort).await {
Ok(value) => Ok(api_wire::reply(protocol, &value)
.calls
.iter()
.any(|call| call.name == "cad_connection_test" && call.arguments.as_ref().is_ok_and(|args| args["ok"] == true))),
Err(error) if error.contains("HTTP 400") || error.contains("HTTP 422") => {
let history = vec![json!({"role":"user","content":"Reply OK. API connection test."})];
let body = api_wire::request_body(protocol, &entry.model, &history, &[], "", false, acp::SYSTEM_NOTE);
request(entry, key, path(protocol), Some(&body), abort).await?;
Ok(false)
}
Err(error) => Err(error),
}
}
struct BrowserBackend {
note: String,
entry: Rc<RefCell<Entry>>,
key: Option<String>,
tools: Sender<ControlRequest>,
ctx: egui::Context,
events: Rc<RefCell<VecDeque<Event>>>,
history: Rc<RefCell<Vec<Value>>>,
generation: Rc<Cell<u64>>,
abort: Rc<RefCell<Option<web_sys::AbortController>>>,
tools_enabled: Rc<Cell<Option<bool>>>,
}
impl BrowserBackend {
fn emit(&self, event: Event) {
self.events.borrow_mut().push_back(event);
self.ctx.request_repaint();
}
fn abort_pending(&self) {
self.generation.set(self.generation.get().wrapping_add(1));
if let Some(abort) = self.abort.borrow_mut().take() {
abort.abort();
}
}
fn start_probe(&self) {
let entry = self.entry.borrow().clone();
let key = self.key.clone();
let events = self.events.clone();
let ctx = self.ctx.clone();
let generation = self.generation.clone();
let turn = generation.get();
let abort = self.abort.clone();
let supported = self.tools_enabled.clone();
spawn_local(async move {
let Ok(controller) = web_sys::AbortController::new() else {
return;
};
*abort.borrow_mut() = Some(controller.clone());
let result = probe(&entry, key.as_deref(), &controller).await;
if generation.get() != turn {
return;
}
match result {
Ok(tools) => {
let listing = model_list(&entry, key.as_deref(), &controller).await;
if generation.get() != turn {
return;
}
let configured = ModelInfo { id: entry.model.clone(), name: entry.model.clone(), description: String::new() };
let (mut available, note) = match listing {
Ok(models) => (models, None),
Err(error) => (Vec::new(), Some(error)),
};
if !available.iter().any(|model| model.id == entry.model) {
available.insert(0, configured);
}
supported.set(Some(tools));
let mut events = events.borrow_mut();
events.push_back(Event::ToolsAvailable(tools));
events.push_back(Event::ModelsNote(note));
events.push_back(Event::Models { current: entry.model.clone(), available });
events.push_back(Event::Ready { agent: entry.name, images: true });
}
Err(error) => {
let mut events = events.borrow_mut();
events.push_back(Event::Error(error.clone()));
events.push_back(Event::Exited { tail: vec![error] });
}
}
abort.borrow_mut().take();
ctx.request_repaint();
});
}
}
impl ChatBackend for BrowserBackend {
fn poll(&mut self) -> Vec<Event> {
self.events.borrow_mut().drain(..).collect()
}
fn prompt(&mut self, parts: &[PromptPart]) {
let generation = self.generation.clone();
let turn = generation.get().wrapping_add(1);
generation.set(turn);
let entry = self.entry.clone();
let key = self.key.clone();
let note = self.note.clone();
let tools = self.tools.clone();
let ctx = self.ctx.clone();
let supported = self.tools_enabled.clone();
let events = self.events.clone();
let history = self.history.clone();
let abort = self.abort.clone();
let parts = parts.to_vec();
spawn_local(async move {
let emit = |event| {
if generation.get() == turn {
events.borrow_mut().push_back(event);
ctx.request_repaint();
}
};
let protocol = entry.borrow().kind.protocol().unwrap_or(Protocol::OpenAi);
let mut messages = history.borrow().clone();
messages.push(api_wire::user_message(protocol, &parts));
api_wire::drop_old_views(&mut messages, 2);
if messages.len() > 240 {
let start = messages
.iter()
.enumerate()
.skip(messages.len() - 240)
.find(|(_, msg)| api_wire::starts_a_turn(msg))
.map(|(index, _)| index)
.unwrap_or(0);
messages.drain(..start);
}
if protocol == Protocol::Anthropic {
api_wire::drop_thinking(&mut messages);
}
let outcome: Result<(), String> = async {
if supported.get().is_none() {
let configured = entry.borrow().clone();
let controller = web_sys::AbortController::new().map_err(failure)?;
*abort.borrow_mut() = Some(controller.clone());
let tools = probe(&configured, key.as_deref(), &controller).await?;
if generation.get() != turn {
return Ok(());
}
supported.set(Some(tools));
emit(Event::ToolsAvailable(tools));
}
let offered = if supported.get() == Some(true) { protocol_tools(protocol, &catalog()) } else { Vec::new() };
let mut next_tool = 0u64;
loop {
if generation.get() != turn {
return Ok(());
}
let configured = entry.borrow().clone();
let controller = web_sys::AbortController::new().map_err(failure)?;
*abort.borrow_mut() = Some(controller.clone());
let body = api_wire::request_body(protocol, &configured.model, &messages, &offered, &configured.reasoning_effort, false, &note);
let value = request(&configured, key.as_deref(), path(protocol), Some(&body), &controller).await?;
if generation.get() != turn {
return Ok(());
}
let reply = api_wire::reply(protocol, &value);
if supported.get() != Some(true) && !reply.calls.is_empty() {
return Err(tr("vm.agent.chat_only"));
}
for (thought, text) in reply.text {
emit(Event::Text { thought, text });
}
messages.push(reply.assistant);
if reply.calls.is_empty() {
*history.borrow_mut() = messages;
emit(Event::TurnEnded { reason: reply.stop.into() });
return Ok(());
}
let mut outputs = Vec::new();
for call in reply.calls {
if generation.get() != turn {
return Ok(());
}
let tool_id = format!("{}-{turn}-{next_tool}", call.id);
next_tool = next_tool.wrapping_add(1);
emit(Event::Tool {
id: tool_id.clone(),
title: Some(call.name.clone()),
kind: None,
status: Some(ToolStatus::InProgress),
input: call.arguments.as_ref().ok().cloned(),
output: None,
});
let result = match &call.arguments {
Ok(args) => tool_call(&call.name, args, &tools, &generation, turn, &ctx).await,
Err(error) => Err(format!("Not run: {error}")),
};
if generation.get() != turn { return Ok(()); }
let (text, failed) = match result {
Ok(result) => (result.to_string(), result["isError"] == true),
Err(error) => (error, true),
};
emit(Event::Tool {
id: tool_id,
title: Some(call.name),
kind: None,
status: Some(if failed { ToolStatus::Failed } else { ToolStatus::Completed }),
input: None,
output: Some(text.chars().take(20_000).collect()),
});
if protocol == Protocol::OpenAi {
messages.push(json!({"role":"tool","tool_call_id":call.id,"content":text.chars().take(20_000).collect::<String>()}));
} else {
outputs.push(json!({"type":"tool_result","tool_use_id":call.id,"content":text.chars().take(20_000).collect::<String>(),"is_error":failed}));
}
}
if !outputs.is_empty() {
messages.push(json!({"role":"user","content":outputs}));
}
*history.borrow_mut() = messages.clone();
}
}
.await;
if let Err(error) = outcome {
emit(Event::Error(error));
emit(Event::TurnEnded { reason: "error".into() });
}
if generation.get() == turn {
abort.borrow_mut().take();
}
});
}
fn cancel(&mut self) {
self.abort_pending();
self.emit(Event::TurnEnded { reason: "cancelled".into() });
}
fn new_session(&mut self) {
// The panel already ended the old turn. A fresh chat must not start with a cancelled notice.
self.abort_pending();
self.history.borrow_mut().clear();
self.emit(Event::Ready { agent: self.entry.borrow().name.clone(), images: true });
}
fn set_instructions(&mut self, text: &str) {
self.note = acp::system_note(text);
}
fn answer_permission(&mut self, _key: u64, _option: Option<&str>) {}
fn authenticate(&mut self, _method: &str) {}
fn set_model(&mut self, model: &str) {
if self.entry.borrow().model == model {
return;
}
self.cancel();
self.entry.borrow_mut().model = model.into();
self.tools_enabled.set(None);
}
fn set_request_timeout(&mut self, seconds: u64) {
self.entry.borrow_mut().request_timeout_s = seconds;
}
fn set_reasoning_effort(&mut self, effort: &str) {
self.entry.borrow_mut().reasoning_effort = agents::reasoning_effort(effort);
}
fn shutdown(&mut self, _wait: bool) {
self.cancel();
self.key = None;
}
}
impl Drop for BrowserBackend {
fn drop(&mut self) {
self.shutdown(false);
}
}
pub fn services() -> BrowserServices {
BrowserServices {
connect: Box::new(|entry, key, tools, ctx| {
let backend = BrowserBackend {
note: acp::SYSTEM_NOTE.into(),
entry: Rc::new(RefCell::new(entry)),
key,
tools,
ctx,
events: Rc::default(),
history: Rc::default(),
generation: Rc::default(),
abort: Rc::default(),
tools_enabled: Rc::new(Cell::new(None)),
};
backend.start_probe();
Ok(Box::new(backend))
}),
platform_connect: Some(Box::new(|mut context, sender, ctx| {
let model = string_field(&context.capabilities, "model").unwrap_or("VibeMake CAD Agent").to_string();
let compatible = context.capabilities["protocolVersion"] == protocol_code() && context.capabilities["enabled"] == true;
let limits = platform_limits(&context.capabilities["limits"]);
context.capabilities["limits"] = limits.clone();
let backend = PlatformBackend {
limits,
tools: safe_platform_tools(&context),
context,
sender,
ctx,
events: Rc::default(),
history: Rc::default(),
generation: Rc::default(),
abort: Rc::default(),
};
if !compatible {
backend.emit(Event::Error(tr("vm.agent.platform_unavailable")));
backend.emit(Event::Exited { tail: Vec::new() });
} else {
let has_tools = !backend.tools.is_empty();
backend.emit(Event::Documents(false));
backend.emit(Event::ToolsAvailable(has_tools));
backend.emit(Event::Ready { agent: model, images: true });
}
Ok(Box::new(backend) as Box<dyn ChatBackend>)
})),
list: Box::new(|protocol, url, key, ctx| {
let (tx, rx) = mpsc::channel();
spawn_local(async move {
let entry =
Entry::from_config(&agents::AgentConfig { kind: protocol.kind(), base_url: url, request_timeout_s: 30, ..Default::default() });
let result = async {
let abort = web_sys::AbortController::new().map_err(failure)?;
model_list(&entry, key.as_deref(), &abort).await
}
.await;
let _ = tx.send(result);
ctx.request_repaint();
});
rx
}),
test: Box::new(|entry, key, ctx| {
let (tx, rx) = mpsc::channel();
spawn_local(async move {
let result = match web_sys::AbortController::new() {
Ok(abort) => probe(&entry, key.as_deref(), &abort)
.await
.and_then(|tools| if tools { Ok(tr("vm.agent.test_ok")) } else { Err(tr("vm.agent.chat_only")) }),
Err(error) => Err(failure(error)),
}
.map_err(TestError::Other);
let _ = tx.send(result);
ctx.request_repaint();
});
rx
}),
attach: Box::new(choose_attachment),
paste_picture: Box::new(paste_picture),
}
}
fn paste_picture(ctx: egui::Context) -> Receiver<Result<attach::Attachment, String>> {
let (tx, rx) = mpsc::channel();
// Begin the browser permission request in the button's user gesture.
let request = web_sys::window().and_then(|window| {
let clipboard = window.navigator().clipboard();
js_sys::Reflect::get(&clipboard, &"read".into()).ok().filter(JsValue::is_function).map(|_| clipboard.read())
});
spawn_local(async move {
let result = async {
let request = request.ok_or_else(|| tr("vm.agent.clipboard_error"))?;
let items = JsFuture::from(request).await.map_err(|_| tr("vm.agent.clipboard_error"))?;
for value in js_sys::Array::from(&items).iter().take(attach::MAX_FILES) {
let item: web_sys::ClipboardItem = value.dyn_into().map_err(failure)?;
for mime in item.types().iter().filter_map(|value| value.as_string()) {
let extension = match mime.as_str() {
"image/png" => "png",
"image/jpeg" => "jpg",
"image/webp" => "webp",
"image/gif" => "gif",
_ => continue,
};
let blob: web_sys::Blob =
JsFuture::from(item.get_type(&mime)).await.map_err(|_| tr("vm.agent.clipboard_error"))?.dyn_into().map_err(failure)?;
if blob.size() > 40.0 * 1024.0 * 1024.0 {
return Err(tr("vm.agent.file_error"));
}
let bytes = js_sys::Uint8Array::new(&JsFuture::from(blob.array_buffer()).await.map_err(failure)?).to_vec();
return attach::picture(format!("pasted picture.{}", extension), &bytes);
}
}
Err(tr("ui.agent_chat_no_picture"))
}
.await;
let _ = tx.send(result);
ctx.request_repaint();
});
rx
}
fn choose_attachment(ctx: egui::Context) -> Receiver<Result<attach::Attachment, String>> {
let (tx, rx) = mpsc::channel();
spawn_local(async move {
let result = pick_attachment().await;
let _ = tx.send(result);
ctx.request_repaint();
});
rx
}
fn attachment_bytes(name: String, bytes: &[u8]) -> Result<attach::Attachment, String> {
match attach::kind(std::path::Path::new(&name)) {
attach::Kind::Picture if bytes.len() <= 40 * 1024 * 1024 => attach::picture(name, bytes),
attach::Kind::Text if bytes.len() <= 256 * 1024 => Ok(attach::Attachment {
id: 0,
name,
content: attach::Content::Text(std::sync::Arc::new(String::from_utf8(bytes.to_vec()).map_err(|_| tr("vm.agent.file_error"))?)),
thumb: None,
}),
_ => Err(tr("vm.agent.file_error")),
}
}
/// Consume assistant drops before the general file importer and the embedded host discard them.
pub fn take_attachment_drops(app: &mut solidcraft_ui_egui::SolveApp, ctx: &egui::Context, raw: &mut egui::RawInput) {
let over_chat = ctx.pointer_hover_pos().is_some_and(|point| app.agent_chat.panel_rect.is_some_and(|rect| rect.contains(point)));
if raw.dropped_files.is_empty()
|| app.platform.is_locked()
|| !solidcraft_ui_egui::agent_chat::shown(app)
|| !app.agent_chat.open
|| app.preferences.agent_chat_minimized
|| !over_chat
{
return;
}
let dropped = std::mem::take(&mut raw.dropped_files);
let (tx, rx) = mpsc::channel();
let remaining = solidcraft_ui_egui::agent_chat::receive_browser_attachments(app, dropped.len(), rx);
if remaining == 0 {
return;
}
let ctx = ctx.clone();
spawn_local(async move {
for file in dropped.into_iter().take(remaining) {
let name = file.path().file_name().map(|name| name.to_string_lossy().into_owned()).unwrap_or_else(|| "dropped".into());
let limit = match attach::kind(std::path::Path::new(&name)) {
attach::Kind::Picture => 40.0 * 1024.0 * 1024.0,
attach::Kind::Text => 256.0 * 1024.0,
_ => 0.0,
};
let size = file.web_file().map(|file| file.size()).unwrap_or(f64::INFINITY);
let result = if limit == 0.0 || !size.is_finite() || size < 0.0 || size > limit {
Err(tr("vm.agent.file_error"))
} else {
file.bytes_async().await.map_err(|_| tr("vm.agent.file_error")).and_then(|bytes| attachment_bytes(name, &bytes))
};
if tx.send(result).is_err() {
break;
}
ctx.request_repaint();
}
});
}
async fn pick_attachment() -> Result<attach::Attachment, String> {
let document = web_sys::window().and_then(|window| window.document()).ok_or_else(|| tr("vm.agent.file_error"))?;
let input: web_sys::HtmlInputElement =
document.create_element("input").map_err(failure)?.dyn_into().map_err(|value: web_sys::Element| failure(value.into()))?;
input.set_type("file");
let extensions: Vec<_> = attach::extensions()
.into_iter()
.filter(|extension| matches!(attach::kind(std::path::Path::new(&format!("file.{extension}"))), attach::Kind::Picture | attach::Kind::Text))
.map(|extension| format!(".{extension}"))
.collect();
input.set_accept(&extensions.join(","));
let mut change = None;
let mut cancel = None;
let choice = js_sys::Promise::new(&mut |resolve, _| {
let selected = input.clone();
let changed = resolve.clone();
let on_change = Closure::<dyn FnMut()>::new(move || {
let value = selected.files().and_then(|files| files.get(0)).map(JsValue::from).unwrap_or(JsValue::NULL);
let _ = changed.call1(&JsValue::NULL, &value);
});
let on_cancel = Closure::<dyn FnMut()>::new(move || {
let _ = resolve.call1(&JsValue::NULL, &JsValue::NULL);
});
input.set_onchange(Some(on_change.as_ref().unchecked_ref()));
let _ = input.add_event_listener_with_callback("cancel", on_cancel.as_ref().unchecked_ref());
change = Some(on_change);
cancel = Some(on_cancel);
});
input.click();
let selected = JsFuture::from(choice).await.map_err(failure)?;
input.set_onchange(None);
if let Some(cancel) = &cancel {
let _ = input.remove_event_listener_with_callback("cancel", cancel.as_ref().unchecked_ref());
}
drop((change, cancel));
if selected.is_null() {
return Err(String::new());
}
let file: web_sys::File = selected.dyn_into().map_err(failure)?;
if file.size() > 40.0 * 1024.0 * 1024.0 {
return Err(tr("vm.agent.file_error"));
}
let name = file.name();
let bytes = js_sys::Uint8Array::new(&JsFuture::from(file.array_buffer()).await.map_err(failure)?).to_vec();
attachment_bytes(name, &bytes)
}