YAML configuration, init without a wizard, and openai-chat-completions
ci / check (push) Successful in 15m52s
ci / check (push) Successful in 15m52s
- /config/agent.yaml and /config/boot.yaml replace the JSON files, read with serde_yaml_ng (also in the agent, inside WASM). - sausage init asks nothing and reads no SAUSAGE_* variables. It writes commented templates with an example for each API, and prints the path to edit. Until the agent is configured, agent.chat names the missing field. allow_no_token: true permits a local model without a key. - The agent speaks openai-chat-completions, the format of OpenRouter, Ollama, and most providers. base_url follows each SDK: with /v1 for the OpenAI shapes, without it for Anthropic. - Checked against the proxy with glm-5.3-flash: the agent wrote, built, and activated a counter plugin in 55 s.
This commit is contained in:
@@ -8,6 +8,7 @@ description = "The Sausage agent plugin."
|
||||
sausage-plugin = { version = "0.1", path = "../sdk" }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
serde_yaml_ng = "0.10"
|
||||
|
||||
[target.'cfg(target_os = "wasi")'.dependencies]
|
||||
wstd = "0.6"
|
||||
|
||||
+210
-26
@@ -1,37 +1,86 @@
|
||||
//! Inference through two API shapes: OpenAI Responses and Anthropic Messages.
|
||||
//! Inference through OpenAI Responses, Chat Completions, and Anthropic Messages.
|
||||
//!
|
||||
//! The conversation is stored in a neutral format, so the agent can switch
|
||||
//! between the two shapes.
|
||||
//! between the three shapes.
|
||||
|
||||
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)]
|
||||
/// Inference configuration from `/config/agent.yaml`.
|
||||
#[derive(Deserialize, Clone, Default)]
|
||||
#[serde(default)]
|
||||
pub struct Config {
|
||||
/// `openai-responses` or `anthropic-messages`.
|
||||
/// `openai-responses`, `openai-chat-completions`, or `anthropic-messages`.
|
||||
pub api: String,
|
||||
/// The API origin, without its versioned endpoint path.
|
||||
/// The SDK base URL: versioned for OpenAI, unversioned for Anthropic.
|
||||
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,
|
||||
/// Permit an empty token and omit authentication headers.
|
||||
pub allow_no_token: bool,
|
||||
/// 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.
|
||||
/// Thinking effort for all 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>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load() -> Result<Self, String> {
|
||||
let bytes = std::fs::read("/config/agent.yaml")
|
||||
.map_err(|e| format!("/config/agent.yaml: missing configuration file: {e}"))?;
|
||||
Self::parse(&bytes)
|
||||
}
|
||||
|
||||
fn parse(bytes: &[u8]) -> Result<Self, String> {
|
||||
let cfg = serde_yaml_ng::from_slice::<Option<Self>>(bytes)
|
||||
.map_err(|e| format!("/config/agent.yaml: {e}"))?
|
||||
.unwrap_or_default();
|
||||
let invalid = if ![
|
||||
"openai-responses",
|
||||
"openai-chat-completions",
|
||||
"anthropic-messages",
|
||||
]
|
||||
.contains(&cfg.api.as_str())
|
||||
{
|
||||
Some("api (missing or unknown API shape)")
|
||||
} else if cfg.base_url.trim().is_empty() {
|
||||
Some("base_url")
|
||||
} else if cfg.model.trim().is_empty() {
|
||||
Some("model")
|
||||
} else if (cfg.token.trim().is_empty() && !cfg.allow_no_token)
|
||||
|| cfg.token == "CHANGE_ME"
|
||||
|| cfg.token.starts_with("YOUR_")
|
||||
{
|
||||
Some("token (set a key, or set token: \"\" and allow_no_token: true)")
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some(field) = invalid {
|
||||
return Err(format!("/config/agent.yaml: configure field {field}"));
|
||||
}
|
||||
Ok(cfg)
|
||||
}
|
||||
|
||||
fn bearer(&self) -> Vec<(&'static str, String)> {
|
||||
if self.token.is_empty() {
|
||||
vec![]
|
||||
} else {
|
||||
vec![("authorization", format!("Bearer {}", self.token))]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Thinking levels, lowest first.
|
||||
pub const LEVELS: [&str; 5] = ["low", "medium", "high", "xhigh", "max"];
|
||||
|
||||
@@ -63,6 +112,64 @@ fn fallback_effort(want: &str, error: &str, tried: &[String]) -> Option<String>
|
||||
mod tests {
|
||||
use super::fallback_effort;
|
||||
|
||||
#[test]
|
||||
fn yaml_configuration_and_credentials() {
|
||||
use super::Config;
|
||||
for text in [
|
||||
"",
|
||||
"api: unknown",
|
||||
"api: openai-responses\nbase_url: url\nmodel: model\ntoken: CHANGE_ME",
|
||||
"api: openai-responses\nbase_url: url\nmodel: model\ntoken: ''",
|
||||
] {
|
||||
assert!(
|
||||
Config::parse(text.as_bytes())
|
||||
.err()
|
||||
.unwrap()
|
||||
.contains("/config/agent.yaml")
|
||||
);
|
||||
}
|
||||
let cfg = Config::parse(b"api: openai-chat-completions\nbase_url: http://localhost/v1\nmodel: local\ntoken: ''\nallow_no_token: true").unwrap();
|
||||
assert!(cfg.bearer().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chat_request_and_response_mapping() {
|
||||
use super::*;
|
||||
let cfg = Config {
|
||||
model: "test".into(),
|
||||
max_tokens: Some(100),
|
||||
..Config::default()
|
||||
};
|
||||
let response = json!({"choices":[{"message":{"content":"interim","tool_calls":[{"id":"one","function":{"name":"read_file","arguments":"{}"}},{"id":"two","function":{"name":"read_file","arguments":"{}"}}]}}]});
|
||||
let mut history = chat_entries(&response);
|
||||
assert_eq!(history.len(), 3);
|
||||
history.push(Entry::ToolResult {
|
||||
id: "one".into(),
|
||||
output: "error".into(),
|
||||
});
|
||||
let tools = [Tool {
|
||||
name: "read_file",
|
||||
description: "read",
|
||||
parameters: json!({"type":"object"}),
|
||||
}];
|
||||
let body = chat_body(&cfg, "system", &tools, &history, Some("high"));
|
||||
assert_eq!(body["messages"][1]["content"], "interim");
|
||||
assert_eq!(
|
||||
body["messages"][1]["tool_calls"].as_array().unwrap().len(),
|
||||
2
|
||||
);
|
||||
assert_eq!(
|
||||
body["messages"][2],
|
||||
json!({"role":"tool","tool_call_id":"one","content":"error"})
|
||||
);
|
||||
assert_eq!(body["tools"][0]["function"]["name"], "read_file");
|
||||
assert_eq!(body["reasoning_effort"], "high");
|
||||
assert_eq!(body["max_completion_tokens"], 100);
|
||||
assert!(
|
||||
matches!(&chat_entries(&json!({"choices":[{"message":{"content":"final"}}]}))[0], Entry::Assistant {text,..} if text == "final")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nearest_accepted_level() {
|
||||
let anthropic =
|
||||
@@ -130,7 +237,7 @@ pub enum Entry {
|
||||
},
|
||||
}
|
||||
|
||||
/// A tool contract shared by both API shapes.
|
||||
/// A tool contract shared by all API shapes.
|
||||
pub struct Tool {
|
||||
/// The function identifier exposed to the model.
|
||||
pub name: &'static str,
|
||||
@@ -148,7 +255,7 @@ pub async fn complete(
|
||||
history: &[Entry],
|
||||
) -> Result<Vec<Entry>, String> {
|
||||
let want = match cfg.api.as_str() {
|
||||
"openai-responses" => cfg
|
||||
"openai-responses" | "openai-chat-completions" => cfg
|
||||
.reasoning_effort
|
||||
.clone()
|
||||
.or_else(|| cfg.thinking.clone()),
|
||||
@@ -160,6 +267,7 @@ pub async fn complete(
|
||||
loop {
|
||||
let result = match cfg.api.as_str() {
|
||||
"openai-responses" => openai(cfg, system, tools, history, effort.as_deref()).await,
|
||||
"openai-chat-completions" => chat(cfg, system, tools, history, effort.as_deref()).await,
|
||||
"anthropic-messages" => {
|
||||
anthropic(
|
||||
cfg,
|
||||
@@ -269,13 +377,8 @@ async fn openai(
|
||||
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 url = format!("{}/responses", cfg.base_url.trim_end_matches('/'));
|
||||
let resp = post(&url, &cfg.bearer(), &body).await?;
|
||||
let mut out = Vec::new();
|
||||
for item in resp["output"].as_array().cloned().unwrap_or_default() {
|
||||
match item["type"].as_str() {
|
||||
@@ -389,16 +492,12 @@ async fn anthropic(
|
||||
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 url = format!("{}/v1/messages", cfg.base_url.trim_end_matches('/'));
|
||||
let mut headers = vec![("anthropic-version", "2023-06-01".to_string())];
|
||||
if !cfg.token.is_empty() {
|
||||
headers.push(("x-api-key", cfg.token.clone()));
|
||||
}
|
||||
let resp = post(&url, &headers, &body).await?;
|
||||
let mut out = Vec::new();
|
||||
for block in resp["content"].as_array().cloned().unwrap_or_default() {
|
||||
match block["type"].as_str() {
|
||||
@@ -423,3 +522,88 @@ async fn anthropic(
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Translate neutral entries into the Chat Completions message format.
|
||||
fn chat_body(
|
||||
cfg: &Config,
|
||||
system: &str,
|
||||
tools: &[Tool],
|
||||
history: &[Entry],
|
||||
effort: Option<&str>,
|
||||
) -> Value {
|
||||
let mut messages = vec![json!({"role":"system", "content":system})];
|
||||
for entry in history {
|
||||
match entry {
|
||||
Entry::User { text } => messages.push(json!({"role":"user","content":text})),
|
||||
Entry::Assistant { text, .. } => {
|
||||
messages.push(json!({"role":"assistant","content":text}))
|
||||
}
|
||||
Entry::ToolCall {
|
||||
id,
|
||||
name,
|
||||
arguments,
|
||||
} => {
|
||||
if messages.last().unwrap()["role"] != "assistant" {
|
||||
messages.push(json!({"role":"assistant","content":null}));
|
||||
}
|
||||
let last = messages.last_mut().unwrap();
|
||||
if !last["tool_calls"].is_array() {
|
||||
last["tool_calls"] = json!([]);
|
||||
}
|
||||
last["tool_calls"].as_array_mut().unwrap().push(json!({"id":id,"type":"function","function":{"name":name,"arguments":arguments}}));
|
||||
}
|
||||
Entry::ToolResult { id, output } => {
|
||||
messages.push(json!({"role":"tool","tool_call_id":id,"content":output}))
|
||||
}
|
||||
Entry::Opaque { .. } => {}
|
||||
}
|
||||
}
|
||||
let tools: Vec<Value> = tools.iter().map(|t| json!({"type":"function","function":{"name":t.name,"description":t.description,"parameters":t.parameters}})).collect();
|
||||
let mut body = json!({"model":cfg.model,"messages":messages,"tools":tools});
|
||||
if let Some(max) = cfg.max_tokens {
|
||||
body["max_completion_tokens"] = json!(max);
|
||||
}
|
||||
if let Some(effort) = effort {
|
||||
body["reasoning_effort"] = json!(effort);
|
||||
}
|
||||
body
|
||||
}
|
||||
|
||||
/// Map text and tool calls back into the shared history.
|
||||
fn chat_entries(response: &Value) -> Vec<Entry> {
|
||||
let message = &response["choices"][0]["message"];
|
||||
let mut entries = Vec::new();
|
||||
if let Some(text) = message["content"].as_str().filter(|s| !s.is_empty()) {
|
||||
entries.push(Entry::Assistant {
|
||||
text: text.into(),
|
||||
phase: None,
|
||||
});
|
||||
}
|
||||
for call in message["tool_calls"].as_array().into_iter().flatten() {
|
||||
entries.push(Entry::ToolCall {
|
||||
id: call["id"].as_str().unwrap_or_default().into(),
|
||||
name: call["function"]["name"].as_str().unwrap_or_default().into(),
|
||||
arguments: call["function"]["arguments"]
|
||||
.as_str()
|
||||
.unwrap_or("{}")
|
||||
.into(),
|
||||
});
|
||||
}
|
||||
entries
|
||||
}
|
||||
|
||||
async fn chat(
|
||||
cfg: &Config,
|
||||
system: &str,
|
||||
tools: &[Tool],
|
||||
history: &[Entry],
|
||||
effort: Option<&str>,
|
||||
) -> Result<Vec<Entry>, String> {
|
||||
let response = post(
|
||||
&format!("{}/chat/completions", cfg.base_url.trim_end_matches('/')),
|
||||
&cfg.bearer(),
|
||||
&chat_body(cfg, system, tools, history, effort),
|
||||
)
|
||||
.await?;
|
||||
Ok(chat_entries(&response))
|
||||
}
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ impl Agent {
|
||||
async fn chat(&self, ctx: &Ctx, p: ChatParams) -> Result<String> {
|
||||
let _guard = lock(HISTORY).await?;
|
||||
std::fs::create_dir_all("/state/agent")?;
|
||||
let cfg: Config = serde_json::from_slice(&std::fs::read("/config/agent.json")?)?;
|
||||
let cfg = Config::load().map_err(|error| Error::new(-32603, error))?;
|
||||
let mut entries = load_history(HISTORY)?;
|
||||
repair_tools(&mut entries)?;
|
||||
let current = entries.len();
|
||||
|
||||
Reference in New Issue
Block a user