Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 152 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ clap = { version = "4", features = ["derive"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
toml = "1.1"
ureq = { version = "2", features = ["json"] }
ureq = { version = "3", features = ["json"] }
regex = "1"
walkdir = "2"
34 changes: 20 additions & 14 deletions src/llm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,25 +367,31 @@ fn call_openrouter(
"messages": messages,
});

// ureq 3.x: Error::StatusCode(u16) no longer carries the response body, so
// http_status_as_error is disabled and the status is checked manually to
// preserve the old behavior of surfacing the error response body.
let result = ureq::post(OPENROUTER_URL)
.set("Authorization", &format!("Bearer {api_key}"))
.set("Content-Type", "application/json")
.config()
.http_status_as_error(false)
.build()
.header("Authorization", &format!("Bearer {api_key}"))
.header("Content-Type", "application/json")
.send_json(body);

let resp = match result {
Ok(r) => r,
Err(ureq::Error::Status(code, r)) => {
let body = r.into_string().unwrap_or_default();
return Err(anyhow!(
"openrouter response code {code}: {}",
truncate(&body, 400)
));
}
Err(e) => return Err(anyhow!("openrouter call failed: {e}")),
};
let mut resp = result.map_err(|e| anyhow!("openrouter call failed: {e}"))?;

if !resp.status().is_success() {
let code = resp.status().as_u16();
let body = resp.body_mut().read_to_string().unwrap_or_default();
return Err(anyhow!(
"openrouter response code {code}: {}",
truncate(&body, 400)
));
}

let v: serde_json::Value = resp
.into_json()
.body_mut()
.read_json()
.context("failed to parse openrouter response JSON")?;
let content = v
.get("choices")
Expand Down