Files
sausage/agent/src/llm.rs
T

207 lines
8.2 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 ("low", "medium", ...).
#[serde(default)]
pub reasoning_effort: Option<String>,
#[serde(default)]
pub max_tokens: Option<u32>,
}
#[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).
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> {
match cfg.api.as_str() {
"openai-responses" => openai(cfg, system, tools, history).await,
"anthropic-messages" => anthropic(cfg, system, tools, history).await,
other => Err(format!("unknown API shape {other:?}")),
}
}
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]) -> 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) = &cfg.reasoning_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]) -> 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}))
}
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 body = json!({
"model": cfg.model,
"system": system,
"messages": messages,
"tools": tools,
"max_tokens": cfg.max_tokens.unwrap_or(32000),
});
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(),
}),
_ => {}
}
}
Ok(out)
}