ci / check (push) Successful in 5s
New config field `thinking` (OpenAI: reasoning_effort still wins): Anthropic adaptive thinking + output_config.effort (thinking blocks kept and sent back), OpenAI reasoning.effort. A 400 that rejects the effort is retried with the nearest accepted level (from the error's list, lower on a tie), logged, never dropped to off.
281 lines
12 KiB
Rust
281 lines
12 KiB
Rust
//! Inference through two API shapes: OpenAI Responses and Anthropic Messages.
|
|
//!
|
|
//! The conversation is stored in a neutral format, so the agent can switch
|
|
//! between the two shapes.
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use serde_json::{Value, json};
|
|
use wstd::http::{Body, Client, Request};
|
|
|
|
#[derive(Deserialize, Clone)]
|
|
pub struct Config {
|
|
/// `openai-responses` or `anthropic-messages`.
|
|
pub api: String,
|
|
pub base_url: String,
|
|
pub model: String,
|
|
pub token: String,
|
|
/// Optional reasoning effort for OpenAI models only (wins over `thinking` there).
|
|
#[serde(default)]
|
|
pub reasoning_effort: Option<String>,
|
|
#[serde(default)]
|
|
pub max_tokens: Option<u32>,
|
|
/// Thinking effort for both APIs: "low", "medium", "high", "xhigh", "max" (`LEVELS`), or "off"/unset for none.
|
|
/// Anthropic: adaptive thinking + output_config.effort; OpenAI: reasoning.effort. A level the model rejects is
|
|
/// replaced by the nearest accepted one (logged), never by "off".
|
|
#[serde(default)]
|
|
pub thinking: Option<String>,
|
|
}
|
|
|
|
/// Thinking levels, lowest first.
|
|
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 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))
|
|
.min_by_key(|l| {
|
|
let i = LEVELS.iter().position(|x| x == l).unwrap_or(1) as i32;
|
|
((i - at).abs(), i > at)
|
|
})
|
|
.map(str::to_string)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::fallback_effort;
|
|
|
|
#[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 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"));
|
|
// 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"));
|
|
}
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
#[serde(tag = "kind", rename_all = "snake_case")]
|
|
pub enum Entry {
|
|
User { text: String },
|
|
Assistant {
|
|
text: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
phase: Option<String>,
|
|
},
|
|
ToolCall { id: String, name: String, arguments: String },
|
|
ToolResult { id: String, output: String },
|
|
/// A provider item that only this API shape understands (OpenAI reasoning, Anthropic thinking).
|
|
Opaque { api: String, item: Value },
|
|
}
|
|
|
|
pub struct Tool {
|
|
pub name: &'static str,
|
|
pub description: &'static str,
|
|
pub parameters: Value,
|
|
}
|
|
|
|
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()),
|
|
_ => cfg.thinking.clone(),
|
|
}
|
|
.filter(|t| t != "off");
|
|
let mut effort = want.clone();
|
|
let mut tried = Vec::new();
|
|
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,
|
|
other => return Err(format!("unknown API shape {other:?}")),
|
|
};
|
|
match (result, effort.clone()) {
|
|
// The model rejected the effort level: retry with the nearest accepted one (thinking stays on).
|
|
(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);
|
|
}
|
|
(result, _) => return result,
|
|
}
|
|
}
|
|
}
|
|
|
|
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 client = Client::new();
|
|
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();
|
|
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> {
|
|
let mut input = Vec::new();
|
|
for e in history {
|
|
match e {
|
|
Entry::User { text } => input.push(json!({"role": "user", "content": text})),
|
|
Entry::Assistant { text, phase } => {
|
|
let mut item = json!({"role": "assistant", "content": text});
|
|
if let Some(phase) = phase {
|
|
item["phase"] = json!(phase);
|
|
}
|
|
input.push(item);
|
|
}
|
|
Entry::ToolCall { id, name, arguments } => {
|
|
input.push(json!({"type": "function_call", "call_id": id, "name": name, "arguments": arguments}))
|
|
}
|
|
Entry::ToolResult { id, output } => {
|
|
input.push(json!({"type": "function_call_output", "call_id": id, "output": output}))
|
|
}
|
|
Entry::Opaque { api, item } if api == "openai-responses" => input.push(item.clone()),
|
|
Entry::Opaque { .. } => {}
|
|
}
|
|
}
|
|
let tools: Vec<Value> = tools
|
|
.iter()
|
|
.map(|t| json!({"type": "function", "name": t.name, "description": t.description, "parameters": t.parameters}))
|
|
.collect();
|
|
let mut body = json!({
|
|
"model": cfg.model,
|
|
"instructions": system,
|
|
"input": input,
|
|
"tools": tools,
|
|
"store": false,
|
|
"include": ["reasoning.encrypted_content"],
|
|
});
|
|
if let Some(max_tokens) = cfg.max_tokens {
|
|
body["max_output_tokens"] = json!(max_tokens);
|
|
}
|
|
if let Some(effort) = effort {
|
|
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 mut out = Vec::new();
|
|
for item in resp["output"].as_array().cloned().unwrap_or_default() {
|
|
match item["type"].as_str() {
|
|
Some("message") => {
|
|
let text: String = item["content"]
|
|
.as_array()
|
|
.map(|parts| {
|
|
parts
|
|
.iter()
|
|
.filter(|p| p["type"] == "output_text")
|
|
.filter_map(|p| p["text"].as_str())
|
|
.collect()
|
|
})
|
|
.unwrap_or_default();
|
|
let phase = item["phase"].as_str().map(str::to_string);
|
|
if !text.is_empty() || phase.is_some() {
|
|
out.push(Entry::Assistant { text, phase });
|
|
}
|
|
}
|
|
Some("function_call") => out.push(Entry::ToolCall {
|
|
id: item["call_id"].as_str().unwrap_or_default().to_string(),
|
|
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 }),
|
|
_ => {}
|
|
}
|
|
}
|
|
if resp["status"] == "incomplete" && out.is_empty() {
|
|
return Err(format!("incomplete response: {}", resp["incomplete_details"]));
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
async fn anthropic(
|
|
cfg: &Config,
|
|
system: &str,
|
|
tools: &[Tool],
|
|
history: &[Entry],
|
|
thinking: bool,
|
|
effort: Option<&str>,
|
|
) -> Result<Vec<Entry>, String> {
|
|
// Merge consecutive blocks of the same role into one message.
|
|
let mut messages: Vec<(String, Vec<Value>)> = Vec::new();
|
|
let mut push = |role: &str, block: Value| match messages.last_mut() {
|
|
Some((r, blocks)) if r == role => blocks.push(block),
|
|
_ => messages.push((role.to_string(), vec![block])),
|
|
};
|
|
for e in history {
|
|
match e {
|
|
Entry::User { text } => push("user", json!({"type": "text", "text": text})),
|
|
Entry::Assistant { text, .. } => {
|
|
push("assistant", json!({"type": "text", "text": text}))
|
|
}
|
|
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}))
|
|
}
|
|
// 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 { .. } => {}
|
|
}
|
|
}
|
|
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}))
|
|
.collect();
|
|
let mut body = json!({
|
|
"model": cfg.model,
|
|
"system": system,
|
|
"messages": messages,
|
|
"tools": tools,
|
|
"max_tokens": cfg.max_tokens.unwrap_or(32000),
|
|
});
|
|
if thinking {
|
|
body["thinking"] = json!({"type": "adaptive"});
|
|
if let Some(effort) = effort {
|
|
body["output_config"] = json!({"effort": effort});
|
|
}
|
|
}
|
|
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())],
|
|
&body,
|
|
)
|
|
.await?;
|
|
let mut out = Vec::new();
|
|
for block in resp["content"].as_array().cloned().unwrap_or_default() {
|
|
match block["type"].as_str() {
|
|
Some("text") => {
|
|
let text = block["text"].as_str().unwrap_or_default().to_string();
|
|
if !text.is_empty() {
|
|
out.push(Entry::Assistant { text, phase: None });
|
|
}
|
|
}
|
|
Some("tool_use") => out.push(Entry::ToolCall {
|
|
id: block["id"].as_str().unwrap_or_default().to_string(),
|
|
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 })
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
Ok(out)
|
|
}
|