Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3bc462ffd2 | ||
|
|
f008d0cc60 | ||
|
|
81daddfd70 |
+84
-10
@@ -14,11 +14,49 @@ pub struct Config {
|
||||
pub base_url: String,
|
||||
pub model: String,
|
||||
pub token: String,
|
||||
/// Optional reasoning effort for OpenAI models ("low", "medium", ...).
|
||||
/// 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)]
|
||||
@@ -32,7 +70,7 @@ pub enum Entry {
|
||||
},
|
||||
ToolCall { id: String, name: String, arguments: String },
|
||||
ToolResult { id: String, output: String },
|
||||
/// A provider item that only this API shape understands (OpenAI reasoning).
|
||||
/// A provider item that only this API shape understands (OpenAI reasoning, Anthropic thinking).
|
||||
Opaque { api: String, item: Value },
|
||||
}
|
||||
|
||||
@@ -43,10 +81,28 @@ pub struct Tool {
|
||||
}
|
||||
|
||||
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:?}")),
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,7 +123,7 @@ async fn post(url: &str, headers: &[(&str, String)], body: &Value) -> Result<Val
|
||||
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> {
|
||||
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 {
|
||||
@@ -104,7 +160,7 @@ async fn openai(cfg: &Config, system: &str, tools: &[Tool], history: &[Entry]) -
|
||||
if let Some(max_tokens) = cfg.max_tokens {
|
||||
body["max_output_tokens"] = json!(max_tokens);
|
||||
}
|
||||
if let Some(effort) = &cfg.reasoning_effort {
|
||||
if let Some(effort) = effort {
|
||||
body["reasoning"] = json!({"effort": effort});
|
||||
}
|
||||
let url = format!("{}/v1/responses", cfg.base_url);
|
||||
@@ -143,7 +199,14 @@ async fn openai(cfg: &Config, system: &str, tools: &[Tool], history: &[Entry]) -
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
async fn anthropic(cfg: &Config, system: &str, tools: &[Tool], history: &[Entry]) -> Result<Vec<Entry>, String> {
|
||||
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() {
|
||||
@@ -163,6 +226,8 @@ async fn anthropic(cfg: &Config, system: &str, tools: &[Tool], history: &[Entry]
|
||||
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 { .. } => {}
|
||||
}
|
||||
}
|
||||
@@ -171,13 +236,19 @@ async fn anthropic(cfg: &Config, system: &str, tools: &[Tool], history: &[Entry]
|
||||
.iter()
|
||||
.map(|t| json!({"name": t.name, "description": t.description, "input_schema": t.parameters}))
|
||||
.collect();
|
||||
let body = json!({
|
||||
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,
|
||||
@@ -199,6 +270,9 @@ async fn anthropic(cfg: &Config, system: &str, tools: &[Tool], history: &[Entry]
|
||||
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 })
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+16
@@ -1,5 +1,20 @@
|
||||
{
|
||||
"nodes": {
|
||||
"crane": {
|
||||
"locked": {
|
||||
"lastModified": 1791754810,
|
||||
"narHash": "sha256-cN0Mgk+sQtCJcGWs9H4M0CnLRAuC4I8aCwiBTcBJs6E=",
|
||||
"owner": "ipetkov",
|
||||
"repo": "crane",
|
||||
"rev": "bd63bf1d0a0f6dcacd7549c714a671ee63b0db6a",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "ipetkov",
|
||||
"repo": "crane",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"flake-utils": {
|
||||
"inputs": {
|
||||
"systems": "systems"
|
||||
@@ -36,6 +51,7 @@
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"crane": "crane",
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs",
|
||||
"rust-overlay": "rust-overlay"
|
||||
|
||||
@@ -6,9 +6,10 @@
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
rust-overlay.url = "github:oxalica/rust-overlay";
|
||||
rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
|
||||
crane.url = "github:ipetkov/crane";
|
||||
};
|
||||
|
||||
outputs = { nixpkgs, flake-utils, rust-overlay, ... }:
|
||||
outputs = { nixpkgs, flake-utils, rust-overlay, crane, ... }:
|
||||
flake-utils.lib.eachSystem flake-utils.lib.allSystems (system:
|
||||
let
|
||||
pkgs = import nixpkgs {
|
||||
@@ -20,13 +21,28 @@
|
||||
targets = [ "wasm32-wasip2" ];
|
||||
extensions = [ "rust-src" ];
|
||||
};
|
||||
rustPlatform = pkgs.makeRustPlatform { cargo = rust; rustc = rust; };
|
||||
sausage = rustPlatform.buildRustPackage {
|
||||
# Crane builds the dependencies once (`cargoArtifacts`, keyed only on
|
||||
# Cargo.toml/Cargo.lock), so a change to our own crates rebuilds
|
||||
# just them.
|
||||
craneLib = (crane.mkLib pkgs).overrideToolchain rust;
|
||||
common = {
|
||||
pname = "sausage";
|
||||
version = "0.1.0";
|
||||
src = ./.;
|
||||
cargoLock.lockFile = ./Cargo.lock;
|
||||
cargoBuildFlags = [ "-p" "sausage" ];
|
||||
};
|
||||
# The test packages for `checks.tests`.
|
||||
testPackages = "-p sausage -p sausage-actor -p agent";
|
||||
cargoArtifacts = craneLib.buildDepsOnly (common // {
|
||||
doCheck = false;
|
||||
# The dependencies of the host binary and of the tests.
|
||||
buildPhaseCargoCommand = ''
|
||||
cargoWithProfile build --locked -p sausage
|
||||
cargoWithProfile test --locked --no-run ${testPackages}
|
||||
'';
|
||||
});
|
||||
sausage = craneLib.buildPackage (common // {
|
||||
inherit cargoArtifacts;
|
||||
cargoExtraArgs = "--locked -p sausage";
|
||||
doCheck = false;
|
||||
nativeBuildInputs = [ pkgs.makeWrapper ];
|
||||
# The `cargo` actor runs cargo at runtime. Build scripts need a C linker.
|
||||
@@ -34,7 +50,7 @@
|
||||
wrapProgram $out/bin/sausage --prefix PATH : ${pkgs.lib.makeBinPath [ rust pkgs.stdenv.cc ]}
|
||||
'';
|
||||
meta.mainProgram = "sausage";
|
||||
};
|
||||
});
|
||||
# `nix run .#loop -- <world>` runs the newest `sausage run <world>`
|
||||
# (without a folder: bare `sausage`, i.e. ./world, created if needed)
|
||||
# from the public repo, again and again: after a crash or a broker
|
||||
@@ -62,10 +78,14 @@
|
||||
# `nix flake check` runs every test of the host, the SDK, and the
|
||||
# agent. The tests build WASM actors with cargo, offline, from the
|
||||
# vendored crates of Cargo.lock.
|
||||
checks.tests = sausage.overrideAttrs (old: {
|
||||
pname = "sausage-tests";
|
||||
doCheck = true;
|
||||
cargoTestFlags = [ "-p" "sausage" "-p" "sausage-actor" "-p" "agent" ];
|
||||
# The binary the host loop runs (`nix run`), built from the same
|
||||
# cached dependencies as the tests.
|
||||
checks.package = sausage;
|
||||
checks.tests = craneLib.cargoTest (common // {
|
||||
inherit cargoArtifacts;
|
||||
cargoExtraArgs = "--locked ${testPackages}";
|
||||
# Nothing builds on the test run's target folder: don't store it.
|
||||
doInstallCargoArtifacts = false;
|
||||
preCheck = ''
|
||||
# Its own lock file pins crates that the vendored set may lack.
|
||||
rm -f cli/tests/actors/toolbox/Cargo.lock
|
||||
|
||||
Reference in New Issue
Block a user