agent: a plugin on the new SDK (agent.chat, tools call/read_file/write_file/list_dir/copy_dir)

Holds the history lock for a turn, 600 s grace period, interim texts as
core.notify. Tests with a fake LLM in the real core. The SDK tests of
/proc, /self, /run, and detached notifications now run.
This commit is contained in:
2026-10-23 01:42:20 +01:00
parent 8d058666dc
commit 53b90a8ecf
11 changed files with 754 additions and 387 deletions
+168 -34
View File
@@ -5,18 +5,24 @@
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
#[cfg(target_os = "wasi")]
use wstd::http::{Body, Client, Request};
/// Inference configuration from `/config/agent.json`.
#[derive(Deserialize, Clone)]
pub struct Config {
/// `openai-responses` or `anthropic-messages`.
pub api: String,
/// The API origin, without its versioned endpoint path.
pub base_url: String,
/// The provider's model identifier.
pub model: String,
/// The API credential sent in the provider's authentication header.
pub token: String,
/// Optional reasoning effort for OpenAI models only (wins over `thinking` there).
#[serde(default)]
pub reasoning_effort: Option<String>,
/// An optional cap on generated tokens.
#[serde(default)]
pub max_tokens: Option<u32>,
/// Thinking effort for both APIs: "low", "medium", "high", "xhigh", "max" (`LEVELS`), or "off"/unset for none.
@@ -32,8 +38,17 @@ pub const LEVELS: [&str; 5] = ["low", "medium", "high", "xhigh", "max"];
/// The level nearest to `want` (lower on a tie) among those a 400 error lists in quotes (else all levels), not
/// yet `tried`. None: nothing left, so the request goes out without an effort field (model default).
fn fallback_effort(want: &str, error: &str, tried: &[String]) -> Option<String> {
let listed: Vec<&str> = error.split('\'').skip(1).step_by(2).filter(|w| LEVELS.contains(w)).collect();
let pool = if listed.iter().any(|l| *l != want) { listed } else { LEVELS.to_vec() };
let listed: Vec<&str> = error
.split('\'')
.skip(1)
.step_by(2)
.filter(|w| LEVELS.contains(w))
.collect();
let pool = if listed.iter().any(|l| *l != want) {
listed
} else {
LEVELS.to_vec()
};
let at = LEVELS.iter().position(|l| *l == want).unwrap_or(1) as i32;
pool.into_iter()
.filter(|l| *l != want && !tried.iter().any(|t| t == l))
@@ -50,39 +65,90 @@ mod tests {
#[test]
fn nearest_accepted_level() {
let anthropic = "HTTP 400: output_config.effort: Input should be 'low', 'medium', 'high' or 'max'";
assert_eq!(fallback_effort("xhigh", anthropic, &["xhigh".into()]).as_deref(), Some("high"));
let anthropic =
"HTTP 400: output_config.effort: Input should be 'low', 'medium', 'high' or 'max'";
assert_eq!(
fallback_effort("xhigh", anthropic, &["xhigh".into()]).as_deref(),
Some("high")
);
let openai = "HTTP 400: Invalid value: 'ultra'. Supported values are: 'none', 'minimal', 'low', 'medium', 'high', 'xhigh', and 'max'.";
assert_eq!(fallback_effort("ultra", openai, &["ultra".into()]).as_deref(), Some("medium"));
assert_eq!(
fallback_effort("ultra", openai, &["ultra".into()]).as_deref(),
Some("medium")
);
// No list in the error: step to the neighbour, never to off.
assert_eq!(fallback_effort("max", "HTTP 400: effort max not supported", &["max".into()]).as_deref(), Some("xhigh"));
assert_eq!(
fallback_effort("max", "HTTP 400: effort max not supported", &["max".into()])
.as_deref(),
Some("xhigh")
);
}
}
/// One append-only entry in the provider-neutral conversation.
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Entry {
User { text: String },
Assistant {
/// A person's contribution starts a new turn.
User {
/// The complete contribution.
text: String,
},
/// Text produced by the model.
Assistant {
/// The generated text.
text: String,
/// The optional provider phase, preserved for subsequent requests.
#[serde(default, skip_serializing_if = "Option::is_none")]
phase: Option<String>,
},
ToolCall { id: String, name: String, arguments: String },
ToolResult { id: String, output: String },
/// A requested tool operation.
ToolCall {
/// The provider's correlation identifier.
id: String,
/// The tool name.
name: String,
/// The JSON parameter object as text.
arguments: String,
},
/// The outcome of a tool operation, including tool errors.
ToolResult {
/// The correlation identifier of the tool call.
id: String,
/// The JSON outcome as text.
output: String,
},
/// A provider item that only this API shape understands (OpenAI reasoning, Anthropic thinking).
Opaque { api: String, item: Value },
Opaque {
/// The API shape that owns this entry.
api: String,
/// The original provider object.
item: Value,
},
}
/// A tool contract shared by both API shapes.
pub struct Tool {
/// The function identifier exposed to the model.
pub name: &'static str,
/// The explanation of the tool's behavior.
pub description: &'static str,
/// The JSON Schema for named parameters.
pub parameters: Value,
}
pub async fn complete(cfg: &Config, system: &str, tools: &[Tool], history: &[Entry]) -> Result<Vec<Entry>, String> {
/// Request the next model step, with provider-specific reasoning items intact.
pub async fn complete(
cfg: &Config,
system: &str,
tools: &[Tool],
history: &[Entry],
) -> Result<Vec<Entry>, String> {
let want = match cfg.api.as_str() {
"openai-responses" => cfg.reasoning_effort.clone().or_else(|| cfg.thinking.clone()),
"openai-responses" => cfg
.reasoning_effort
.clone()
.or_else(|| cfg.thinking.clone()),
_ => cfg.thinking.clone(),
}
.filter(|t| t != "off");
@@ -91,7 +157,17 @@ pub async fn complete(cfg: &Config, system: &str, tools: &[Tool], history: &[Ent
loop {
let result = match cfg.api.as_str() {
"openai-responses" => openai(cfg, system, tools, history, effort.as_deref()).await,
"anthropic-messages" => anthropic(cfg, system, tools, history, want.is_some(), effort.as_deref()).await,
"anthropic-messages" => {
anthropic(
cfg,
system,
tools,
history,
want.is_some(),
effort.as_deref(),
)
.await
}
other => return Err(format!("unknown API shape {other:?}")),
};
match (result, effort.clone()) {
@@ -99,31 +175,58 @@ pub async fn complete(cfg: &Config, system: &str, tools: &[Tool], history: &[Ent
(Err(e), Some(current)) if e.starts_with("HTTP 400") && e.contains("effort") => {
tried.push(current.clone());
effort = fallback_effort(want.as_deref().unwrap_or(""), &e, &tried);
eprintln!("agent: {} rejected thinking effort {current:?}; retrying with {effort:?}: {e}", cfg.model);
eprintln!(
"agent: {} rejected thinking effort {current:?}; retrying with {effort:?}: {e}",
cfg.model
);
}
(result, _) => return result,
}
}
}
/// Native unit tests exercise history logic without an HTTP runtime.
#[cfg(not(target_os = "wasi"))]
async fn post(_url: &str, _headers: &[(&str, String)], _body: &Value) -> Result<Value, String> {
Err("inference requires the WASI HTTP runtime".into())
}
/// Send JSON through wasi:http and retain error bodies for context-size detection.
#[cfg(target_os = "wasi")]
async fn post(url: &str, headers: &[(&str, String)], body: &Value) -> Result<Value, String> {
let mut req = Request::post(url).header("content-type", "application/json");
for (k, v) in headers {
req = req.header(*k, v.as_str());
}
let req = req.body(Body::from(body.to_string())).map_err(|e| format!("request: {e}"))?;
let req = req
.body(Body::from(body.to_string()))
.map_err(|e| format!("request: {e}"))?;
let client = Client::new();
let resp = client.send(req).await.map_err(|e| format!("HTTP request failed: {e:?}"))?;
let resp = client
.send(req)
.await
.map_err(|e| format!("HTTP request failed: {e:?}"))?;
let status = resp.status();
let mut body = resp.into_body();
let text = body.str_contents().await.map_err(|e| format!("reading the response failed: {e:?}"))?.to_string();
let text = body
.str_contents()
.await
.map_err(|e| format!("reading the response failed: {e:?}"))?
.to_string();
if !status.is_success() {
return Err(format!("HTTP {status}: {text}"));
}
serde_json::from_str(&text).map_err(|e| format!("invalid JSON in response: {e}"))
}
async fn openai(cfg: &Config, system: &str, tools: &[Tool], history: &[Entry], effort: Option<&str>) -> Result<Vec<Entry>, String> {
/// Translate neutral history to OpenAI Responses and preserve returned reasoning items.
async fn openai(
cfg: &Config,
system: &str,
tools: &[Tool],
history: &[Entry],
effort: Option<&str>,
) -> Result<Vec<Entry>, String> {
let mut input = Vec::new();
for e in history {
match e {
@@ -164,7 +267,12 @@ async fn openai(cfg: &Config, system: &str, tools: &[Tool], history: &[Entry], e
body["reasoning"] = json!({"effort": effort});
}
let url = format!("{}/v1/responses", cfg.base_url);
let resp = post(&url, &[("authorization", format!("Bearer {}", cfg.token))], &body).await?;
let resp = post(
&url,
&[("authorization", format!("Bearer {}", cfg.token))],
&body,
)
.await?;
let mut out = Vec::new();
for item in resp["output"].as_array().cloned().unwrap_or_default() {
match item["type"].as_str() {
@@ -189,16 +297,23 @@ async fn openai(cfg: &Config, system: &str, tools: &[Tool], history: &[Entry], e
name: item["name"].as_str().unwrap_or_default().to_string(),
arguments: item["arguments"].as_str().unwrap_or("{}").to_string(),
}),
Some("reasoning") => out.push(Entry::Opaque { api: "openai-responses".into(), item }),
Some("reasoning") => out.push(Entry::Opaque {
api: "openai-responses".into(),
item,
}),
_ => {}
}
}
if resp["status"] == "incomplete" && out.is_empty() {
return Err(format!("incomplete response: {}", resp["incomplete_details"]));
return Err(format!(
"incomplete response: {}",
resp["incomplete_details"]
));
}
Ok(out)
}
/// Translate neutral history to Anthropic Messages and merge consecutive roles.
async fn anthropic(
cfg: &Config,
system: &str,
@@ -219,22 +334,37 @@ async fn anthropic(
Entry::Assistant { text, .. } => {
push("assistant", json!({"type": "text", "text": text}))
}
Entry::ToolCall { id, name, arguments } => {
Entry::ToolCall {
id,
name,
arguments,
} => {
let input: Value = serde_json::from_str(arguments).unwrap_or(json!({}));
push("assistant", json!({"type": "tool_use", "id": id, "name": name, "input": input}))
}
Entry::ToolResult { id, output } => {
push("user", json!({"type": "tool_result", "tool_use_id": id, "content": output}))
push(
"assistant",
json!({"type": "tool_use", "id": id, "name": name, "input": input}),
)
}
Entry::ToolResult { id, output } => push(
"user",
json!({"type": "tool_result", "tool_use_id": id, "content": output}),
),
// Thinking blocks go back unchanged (with their signature): required next to tool use.
Entry::Opaque { api, item } if api == "anthropic-messages" && thinking => push("assistant", item.clone()),
Entry::Opaque { api, item } if api == "anthropic-messages" && thinking => {
push("assistant", item.clone())
}
Entry::Opaque { .. } => {}
}
}
let messages: Vec<Value> = messages.into_iter().map(|(role, content)| json!({"role": role, "content": content})).collect();
let messages: Vec<Value> = messages
.into_iter()
.map(|(role, content)| json!({"role": role, "content": content}))
.collect();
let tools: Vec<Value> = tools
.iter()
.map(|t| json!({"name": t.name, "description": t.description, "input_schema": t.parameters}))
.map(
|t| json!({"name": t.name, "description": t.description, "input_schema": t.parameters}),
)
.collect();
let mut body = json!({
"model": cfg.model,
@@ -252,7 +382,10 @@ async fn anthropic(
let url = format!("{}/v1/messages", cfg.base_url);
let resp = post(
&url,
&[("x-api-key", cfg.token.clone()), ("anthropic-version", "2023-06-01".to_string())],
&[
("x-api-key", cfg.token.clone()),
("anthropic-version", "2023-06-01".to_string()),
],
&body,
)
.await?;
@@ -270,9 +403,10 @@ async fn anthropic(
name: block["name"].as_str().unwrap_or_default().to_string(),
arguments: block["input"].to_string(),
}),
Some("thinking") | Some("redacted_thinking") => {
out.push(Entry::Opaque { api: "anthropic-messages".into(), item: block })
}
Some("thinking") | Some("redacted_thinking") => out.push(Entry::Opaque {
api: "anthropic-messages".into(),
item: block,
}),
_ => {}
}
}