web-cortex-framework 2.5.2__tar.gz → 2.6.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/Cargo.lock +6 -6
  2. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/Cargo.toml +1 -1
  3. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/PKG-INFO +2 -2
  4. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/README.md +1 -1
  5. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/agent/provider.rs +313 -90
  6. web_cortex_framework-2.6.0/crates/webcortex-core/src/agent/session.rs +230 -0
  7. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/agent.rs +255 -14
  8. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/app.rs +40 -23
  9. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/auth.rs +109 -8
  10. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/bridge.rs +44 -0
  11. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/db.rs +7 -4
  12. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/devices.rs +227 -27
  13. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/errors.rs +9 -3
  14. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/files.rs +65 -11
  15. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/flow.rs +82 -3
  16. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/http.rs +113 -3
  17. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/ledger.rs +49 -4
  18. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/manifest.rs +63 -2
  19. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/mcp.rs +208 -8
  20. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/middleware.rs +71 -7
  21. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/openapi.rs +78 -9
  22. web_cortex_framework-2.6.0/crates/webcortex-core/src/router.rs +224 -0
  23. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/server.rs +489 -46
  24. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/status.rs +45 -0
  25. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/templates.rs +30 -1
  26. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/typegen.rs +207 -23
  27. web_cortex_framework-2.6.0/crates/webcortex-core/src/ws.rs +652 -0
  28. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-py/src/behaviour.rs +106 -21
  29. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-py/src/lib.rs +77 -3
  30. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/pyproject.toml +1 -1
  31. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/python/webcortex/__init__.py +1 -1
  32. web_cortex_framework-2.6.0/python/webcortex/_bridge.py +666 -0
  33. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/python/webcortex/app.py +173 -26
  34. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/python/webcortex/cli.py +73 -21
  35. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/python/webcortex/client.py +157 -22
  36. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/python/webcortex/contextpack.py +21 -8
  37. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/python/webcortex/diagnostics.py +27 -5
  38. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/python/webcortex/errors.py +1 -1
  39. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/python/webcortex/media.py +51 -23
  40. web_cortex_framework-2.6.0/python/webcortex/schema.py +544 -0
  41. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/python/webcortex/starters.py +35 -15
  42. web_cortex_framework-2.5.2/crates/webcortex-core/src/agent/session.rs +0 -126
  43. web_cortex_framework-2.5.2/crates/webcortex-core/src/router.rs +0 -114
  44. web_cortex_framework-2.5.2/crates/webcortex-core/src/ws.rs +0 -381
  45. web_cortex_framework-2.5.2/python/webcortex/_bridge.py +0 -384
  46. web_cortex_framework-2.5.2/python/webcortex/schema.py +0 -210
  47. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/LICENSE +0 -0
  48. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/Cargo.toml +0 -0
  49. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/agent/registry.rs +0 -0
  50. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/agent/vision.rs +0 -0
  51. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/audit.rs +0 -0
  52. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/context.rs +0 -0
  53. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/src/lib.rs +0 -0
  54. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-core/tests/diagnostics.rs +0 -0
  55. {web_cortex_framework-2.5.2 → web_cortex_framework-2.6.0}/crates/webcortex-py/Cargo.toml +0 -0
@@ -1729,9 +1729,9 @@ dependencies = [
1729
1729
 
1730
1730
  [[package]]
1731
1731
  name = "rustls-webpki"
1732
- version = "0.103.15"
1732
+ version = "0.103.16"
1733
1733
  source = "registry+https://github.com/rust-lang/crates.io-index"
1734
- checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2"
1734
+ checksum = "4443b1b6d39d7c663139942cc4558262562d2c84123bb6282ef177009ca8b281"
1735
1735
  dependencies = [
1736
1736
  "ring",
1737
1737
  "rustls-pki-types",
@@ -2662,7 +2662,7 @@ dependencies = [
2662
2662
 
2663
2663
  [[package]]
2664
2664
  name = "webcortex-core"
2665
- version = "2.5.2"
2665
+ version = "2.6.0"
2666
2666
  dependencies = [
2667
2667
  "base64 0.23.1",
2668
2668
  "bytes",
@@ -2690,7 +2690,7 @@ dependencies = [
2690
2690
 
2691
2691
  [[package]]
2692
2692
  name = "webcortex-py"
2693
- version = "2.5.2"
2693
+ version = "2.6.0"
2694
2694
  dependencies = [
2695
2695
  "bytes",
2696
2696
  "futures",
@@ -2704,9 +2704,9 @@ dependencies = [
2704
2704
 
2705
2705
  [[package]]
2706
2706
  name = "webpki-roots"
2707
- version = "1.0.9"
2707
+ version = "1.0.10"
2708
2708
  source = "registry+https://github.com/rust-lang/crates.io-index"
2709
- checksum = "7dcd9d09a39985f5344844e66b0c530a33843579125f23e21e9f0f220850f22a"
2709
+ checksum = "1c1c8e20c8a38fc7d65abfe74eb7280e4aa667e8eec8bc84996d2874810748d9"
2710
2710
  dependencies = [
2711
2711
  "rustls-pki-types",
2712
2712
  ]
@@ -3,7 +3,7 @@ members = ["crates/webcortex-core", "crates/webcortex-py"]
3
3
  resolver = "3"
4
4
 
5
5
  [workspace.package]
6
- version = "2.5.2"
6
+ version = "2.6.0"
7
7
  edition = "2024"
8
8
  license = "Apache-2.0"
9
9
  rust-version = "1.88"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: web-cortex-framework
3
- Version: 2.5.2
3
+ Version: 2.6.0
4
4
  Classifier: Development Status :: 3 - Alpha
5
5
  Classifier: Intended Audience :: Developers
6
6
  Classifier: License :: OSI Approved :: Apache Software License
@@ -527,7 +527,7 @@ tested, the known limits, and what v2 added to the surface.
527
527
 
528
528
  ## Status
529
529
 
530
- v2.5.2. Working and tested: the manifest IR, router, native ops (static /
530
+ v2.6.0. Working and tested: the manifest IR, router, native ops (static /
531
531
  query / proxy / page / files / flow), the free-threaded Python bridge,
532
532
  authentication and scopes, rate limiting, CORS, security headers, graceful
533
533
  shutdown, Behaviours with concurrent leaves, the agent runtime with handoffs,
@@ -499,7 +499,7 @@ tested, the known limits, and what v2 added to the surface.
499
499
 
500
500
  ## Status
501
501
 
502
- v2.5.2. Working and tested: the manifest IR, router, native ops (static /
502
+ v2.6.0. Working and tested: the manifest IR, router, native ops (static /
503
503
  query / proxy / page / files / flow), the free-threaded Python bridge,
504
504
  authentication and scopes, rate limiting, CORS, security headers, graceful
505
505
  shutdown, Behaviours with concurrent leaves, the agent runtime with handoffs,
@@ -101,6 +101,159 @@ fn full_system(req: &CompletionRequest<'_>) -> String {
101
101
  s
102
102
  }
103
103
 
104
+ // ---------------------------------------------------------------------------
105
+ // The wire, shared by both formats
106
+ // ---------------------------------------------------------------------------
107
+
108
+ /// Extra attempts after a transient failure: a refused connection, or one of
109
+ /// [`RETRY_STATUSES`]. The official SDKs retry the same set twice. A timeout
110
+ /// is not retried — the client's timeout is ten minutes, and a third of an
111
+ /// hour spent re-asking would outlast any request that is waiting on it.
112
+ pub const PROVIDER_RETRIES: u32 = 2;
113
+
114
+ /// Rate limited, overloaded (529 is Anthropic's), or a gateway that lost its
115
+ /// upstream: an answer that is likely to be different a moment later.
116
+ pub const RETRY_STATUSES: [u16; 8] = [408, 409, 429, 500, 502, 503, 504, 529];
117
+
118
+ /// Longest `retry-after` honoured. A longer one is the provider saying "not
119
+ /// soon", and the run is better told so now than after a minute of silence.
120
+ const MAX_RETRY_AFTER: std::time::Duration = std::time::Duration::from_secs(10);
121
+
122
+ /// How long to wait before attempt `attempt + 1`.
123
+ fn retry_delay(attempt: u32, retry_after: Option<std::time::Duration>) -> std::time::Duration {
124
+ retry_after
125
+ .filter(|d| *d <= MAX_RETRY_AFTER)
126
+ .unwrap_or_else(|| std::time::Duration::from_millis(250u64 << attempt.min(6)))
127
+ }
128
+
129
+ /// Send one completion request and return its JSON body, retrying transient
130
+ /// failures. Errors keep the shapes the error classifier reads:
131
+ /// `"<label> request failed: …"` (WC303), `"<label> <status>: <detail>"`
132
+ /// (WC304) and `"<label> returned an unreadable body: …"` (WC305).
133
+ ///
134
+ /// The body is read as bytes before it is parsed, so an error status survives
135
+ /// a body that is not JSON: a 429 or 502 from a proxy answers in text or HTML,
136
+ /// and parsing first reported it as an unreadable body, losing the status.
137
+ async fn send_with_retries(label: &str, request: reqwest::RequestBuilder) -> Result<Value, String> {
138
+ let mut attempt = 0u32;
139
+ loop {
140
+ let Some(this) = request.try_clone() else {
141
+ return Err(format!("{label} request failed: the request body could not be replayed"));
142
+ };
143
+ let res = match this.send().await {
144
+ Ok(res) => res,
145
+ Err(e) if e.is_connect() && attempt < PROVIDER_RETRIES => {
146
+ tokio::time::sleep(retry_delay(attempt, None)).await;
147
+ attempt += 1;
148
+ continue;
149
+ }
150
+ Err(e) => return Err(format!("{label} request failed: {e}")),
151
+ };
152
+ let status = res.status();
153
+ let retry_after = res
154
+ .headers()
155
+ .get(reqwest::header::RETRY_AFTER)
156
+ .and_then(|v| v.to_str().ok())
157
+ .and_then(|v| v.trim().parse::<f64>().ok())
158
+ .filter(|s| s.is_finite() && *s >= 0.0)
159
+ .map(std::time::Duration::from_secs_f64);
160
+ let body = res
161
+ .bytes()
162
+ .await
163
+ .map_err(|e| format!("{label} returned an unreadable body: {e}"))?;
164
+ if !status.is_success() {
165
+ if RETRY_STATUSES.contains(&status.as_u16()) && attempt < PROVIDER_RETRIES {
166
+ tracing::debug!(provider = label, %status, attempt, "retrying a transient provider failure");
167
+ tokio::time::sleep(retry_delay(attempt, retry_after)).await;
168
+ attempt += 1;
169
+ continue;
170
+ }
171
+ return Err(format!("{label} {status}: {}", error_detail(&body)));
172
+ }
173
+ return serde_json::from_slice(&body).map_err(|e| {
174
+ format!("{label} returned an unreadable body: {e} (it began {:?})", snippet(&body))
175
+ });
176
+ }
177
+ }
178
+
179
+ /// What an error response says went wrong: `error.message` (both formats),
180
+ /// a bare `error` string, or the start of a body that is not JSON at all.
181
+ fn error_detail(body: &[u8]) -> String {
182
+ match serde_json::from_slice::<Value>(body) {
183
+ Ok(payload) => match payload.get("error") {
184
+ Some(e) => e
185
+ .get("message")
186
+ .and_then(|m| m.as_str())
187
+ .map(str::to_string)
188
+ .or_else(|| e.as_str().map(str::to_string))
189
+ .unwrap_or_else(|| e.to_string()),
190
+ None => payload
191
+ .get("message")
192
+ .and_then(|m| m.as_str())
193
+ .map(str::to_string)
194
+ .unwrap_or_else(|| "no detail".into()),
195
+ },
196
+ Err(_) => {
197
+ let text = snippet(body);
198
+ if text.is_empty() { "no detail".into() } else { text }
199
+ }
200
+ }
201
+ }
202
+
203
+ /// The first couple of hundred characters of a body, whitespace collapsed.
204
+ fn snippet(body: &[u8]) -> String {
205
+ let text = String::from_utf8_lossy(body);
206
+ let mut out: String = text.split_whitespace().collect::<Vec<_>>().join(" ");
207
+ if out.chars().count() > 200 {
208
+ out = out.chars().take(200).collect::<String>() + "…";
209
+ }
210
+ out
211
+ }
212
+
213
+ /// A token count from a `usage` field. Some OpenAI-compatible servers report
214
+ /// them as floats (`12.0`); reading only integers charged those calls nothing.
215
+ fn token_count(v: Option<&Value>) -> u64 {
216
+ match v {
217
+ Some(Value::Number(n)) => n.as_u64().unwrap_or_else(|| match n.as_f64() {
218
+ Some(f) if f.is_finite() && f > 0.0 => f.round().min(u64::MAX as f64) as u64,
219
+ _ => 0,
220
+ }),
221
+ _ => 0,
222
+ }
223
+ }
224
+
225
+ /// A tool-call id the rest of the turn can rely on: present, non-empty and
226
+ /// unique within the turn. A server that omits ids, sends `""`, or repeats one
227
+ /// left `tool_result`s that could not be matched to their calls.
228
+ fn call_id(given: Option<&str>, seen: &mut std::collections::HashSet<String>) -> String {
229
+ let id = match given.map(str::trim) {
230
+ Some(id) if !id.is_empty() && !seen.contains(id) => id.to_string(),
231
+ _ => format!("call_{}", uuid::Uuid::new_v4().simple()),
232
+ };
233
+ seen.insert(id.clone());
234
+ id
235
+ }
236
+
237
+ /// What a tool call's arguments are taken to be. Anything that is not a JSON
238
+ /// object is kept as it came — the runtime refuses such a call (WC317) rather
239
+ /// than run the tool with no arguments, which is what a truncated or garbled
240
+ /// argument string used to do.
241
+ fn call_arguments(raw: Option<&Value>) -> Value {
242
+ match raw {
243
+ None | Some(Value::Null) => json!({}),
244
+ Some(Value::String(s)) if s.trim().is_empty() => json!({}),
245
+ Some(Value::String(s)) => serde_json::from_str::<Value>(s).unwrap_or_else(|_| Value::String(s.clone())),
246
+ Some(v) => v.clone(),
247
+ }
248
+ }
249
+
250
+ /// The `input` a canonical `tool_use` block replays with. Anthropic accepts
251
+ /// only an object there; a malformed call is replayed as `{}` and answered
252
+ /// with an error result.
253
+ fn replay_input(arguments: &Value) -> Value {
254
+ if arguments.is_object() { arguments.clone() } else { json!({}) }
255
+ }
256
+
104
257
  // ---------------------------------------------------------------------------
105
258
  // Anthropic
106
259
  // ---------------------------------------------------------------------------
@@ -152,32 +305,14 @@ impl ModelProvider for AnthropicProvider {
152
305
  let agent = req.agent;
153
306
  let body = anthropic_body(req);
154
307
 
155
- let res = self
308
+ let request = self
156
309
  .client
157
310
  .post(format!("{}/v1/messages", self.base_url.trim_end_matches('/')))
158
311
  .header("x-api-key", &self.api_key)
159
312
  .header("anthropic-version", &self.version)
160
313
  .header("content-type", "application/json")
161
- .json(&body)
162
- .send()
163
- .await
164
- .map_err(|e| format!("anthropic request failed: {e}"))?;
165
-
166
- let status = res.status();
167
- let payload: Value = res
168
- .json()
169
- .await
170
- .map_err(|e| format!("anthropic returned an unreadable body: {e}"))?;
171
-
172
- if !status.is_success() {
173
- let detail = payload
174
- .get("error")
175
- .and_then(|e| e.get("message"))
176
- .and_then(|m| m.as_str())
177
- .unwrap_or("no detail");
178
- return Err(format!("anthropic {status}: {detail}"));
179
- }
180
-
314
+ .json(&body);
315
+ let payload = send_with_retries("anthropic", request).await?;
181
316
  parse_anthropic(&payload, &agent.model)
182
317
  })
183
318
  }
@@ -245,10 +380,12 @@ pub fn parse_anthropic(payload: &Value, model: &str) -> Result<ProviderResponse,
245
380
  let content = payload
246
381
  .get("content")
247
382
  .and_then(|c| c.as_array())
248
- .ok_or("anthropic response had no content array")?;
383
+ .ok_or("anthropic returned an unreadable body: the response had no content array")?;
249
384
 
250
385
  let mut text = String::new();
251
386
  let mut tool_calls = Vec::new();
387
+ let mut raw = Vec::with_capacity(content.len());
388
+ let mut seen = std::collections::HashSet::new();
252
389
 
253
390
  for block in content {
254
391
  match block.get("type").and_then(|t| t.as_str()) {
@@ -256,21 +393,21 @@ pub fn parse_anthropic(payload: &Value, model: &str) -> Result<ProviderResponse,
256
393
  if let Some(t) = block.get("text").and_then(|t| t.as_str()) {
257
394
  text.push_str(t);
258
395
  }
396
+ raw.push(block.clone());
259
397
  }
260
- Some("tool_use") => tool_calls.push(ToolCall {
261
- id: block
262
- .get("id")
263
- .and_then(|i| i.as_str())
264
- .unwrap_or_default()
265
- .to_string(),
266
- name: block
267
- .get("name")
268
- .and_then(|n| n.as_str())
269
- .unwrap_or_default()
270
- .to_string(),
271
- arguments: block.get("input").cloned().unwrap_or_else(|| json!({})),
272
- }),
273
- _ => {}
398
+ Some("tool_use") => {
399
+ let id = call_id(block.get("id").and_then(|i| i.as_str()), &mut seen);
400
+ let name = block.get("name").and_then(|n| n.as_str()).unwrap_or_default().to_string();
401
+ let arguments = call_arguments(block.get("input"));
402
+ // Replayed with the id and input the rest of the turn uses, so
403
+ // a repaired id still pairs with its result.
404
+ let mut replay = block.clone();
405
+ replay["id"] = json!(id);
406
+ replay["input"] = replay_input(&arguments);
407
+ raw.push(replay);
408
+ tool_calls.push(ToolCall { id, name, arguments });
409
+ }
410
+ _ => raw.push(block.clone()),
274
411
  }
275
412
  }
276
413
 
@@ -282,12 +419,7 @@ pub fn parse_anthropic(payload: &Value, model: &str) -> Result<ProviderResponse,
282
419
  };
283
420
 
284
421
  let usage = payload.get("usage");
285
- let get = |k: &str| {
286
- usage
287
- .and_then(|u| u.get(k))
288
- .and_then(|v| v.as_u64())
289
- .unwrap_or(0)
290
- };
422
+ let get = |k: &str| token_count(usage.and_then(|u| u.get(k)));
291
423
  Ok(ProviderResponse {
292
424
  text,
293
425
  tool_calls,
@@ -301,7 +433,7 @@ pub fn parse_anthropic(payload: &Value, model: &str) -> Result<ProviderResponse,
301
433
  .and_then(|m| m.as_str())
302
434
  .unwrap_or(model)
303
435
  .to_string(),
304
- raw_content: Value::Array(content.clone()),
436
+ raw_content: Value::Array(raw),
305
437
  })
306
438
  }
307
439
 
@@ -354,27 +486,7 @@ impl ModelProvider for OpenAiCompatProvider {
354
486
  if let Some(key) = &self.api_key {
355
487
  builder = builder.bearer_auth(key);
356
488
  }
357
- let res = builder
358
- .json(&body)
359
- .send()
360
- .await
361
- .map_err(|e| format!("{} request failed: {e}", self.label))?;
362
-
363
- let status = res.status();
364
- let payload: Value = res
365
- .json()
366
- .await
367
- .map_err(|e| format!("{} returned an unreadable body: {e}", self.label))?;
368
-
369
- if !status.is_success() {
370
- let detail = payload
371
- .get("error")
372
- .and_then(|e| e.get("message").or(Some(e)))
373
- .map(|m| m.as_str().map(str::to_string).unwrap_or_else(|| m.to_string()))
374
- .unwrap_or_else(|| "no detail".into());
375
- return Err(format!("{} {status}: {detail}", self.label));
376
- }
377
-
489
+ let payload = send_with_retries(self.label, builder.json(&body)).await?;
378
490
  parse_openai(&payload, &agent.model)
379
491
  })
380
492
  }
@@ -519,12 +631,21 @@ pub fn to_openai_messages(role: &str, content: &Value) -> Vec<Value> {
519
631
  }
520
632
 
521
633
  pub fn parse_openai(payload: &Value, model: &str) -> Result<ProviderResponse, String> {
634
+ // OpenRouter and some proxies report a failure in a 200 body.
635
+ if payload.get("choices").is_none()
636
+ && let Some(e) = payload.get("error")
637
+ {
638
+ return Err(format!("the provider returned an error: {}", error_detail(json!({"error": e}).to_string().as_bytes())));
639
+ }
522
640
  let choice = payload
523
641
  .get("choices")
524
642
  .and_then(|c| c.as_array())
525
643
  .and_then(|c| c.first())
526
- .ok_or("chat completion had no choices")?;
527
- let message = choice.get("message").ok_or("chat completion choice had no message")?;
644
+ .ok_or("the provider returned an unreadable body: the chat completion had no choices")?;
645
+ let message = choice
646
+ .get("message")
647
+ .filter(|m| m.is_object())
648
+ .ok_or("the provider returned an unreadable body: the chat completion choice had no message")?;
528
649
 
529
650
  let text = match message.get("content") {
530
651
  Some(Value::String(s)) => s.clone(),
@@ -542,54 +663,43 @@ pub fn parse_openai(payload: &Value, model: &str) -> Result<ProviderResponse, St
542
663
  raw.push(json!({"type": "text", "text": text}));
543
664
  }
544
665
  if let Some(calls) = message.get("tool_calls").and_then(|c| c.as_array()) {
545
- for (i, c) in calls.iter().enumerate() {
546
- let id = c
547
- .get("id")
548
- .and_then(|i| i.as_str())
549
- .map(str::to_string)
550
- .unwrap_or_else(|| format!("call_{i}"));
666
+ let mut seen = std::collections::HashSet::new();
667
+ for c in calls {
668
+ let id = call_id(c.get("id").and_then(|i| i.as_str()), &mut seen);
551
669
  let f = c.get("function").cloned().unwrap_or(Value::Null);
552
670
  let name = f
553
671
  .get("name")
554
672
  .and_then(|n| n.as_str())
555
673
  .unwrap_or_default()
556
674
  .to_string();
557
- let arguments = match f.get("arguments") {
558
- Some(Value::String(s)) => serde_json::from_str(s).unwrap_or_else(|_| json!({})),
559
- Some(v) => v.clone(),
560
- None => json!({}),
561
- };
562
- raw.push(json!({"type": "tool_use", "id": id, "name": name, "input": arguments}));
675
+ let arguments = call_arguments(f.get("arguments"));
676
+ raw.push(json!({"type": "tool_use", "id": id, "name": name, "input": replay_input(&arguments)}));
563
677
  tool_calls.push(ToolCall { id, name, arguments });
564
678
  }
565
679
  }
566
680
 
567
681
  let stop_reason = match choice.get("finish_reason").and_then(|s| s.as_str()) {
568
682
  Some("tool_calls") | Some("function_call") => StopReason::ToolUse,
569
- Some("stop") => StopReason::EndTurn,
570
683
  Some("length") => StopReason::MaxTokens,
684
+ // Ollama, vLLM and others answer `stop` for a turn that holds tool
685
+ // calls. Read as the end of the turn, the calls were never run and the
686
+ // run "completed" with no output.
571
687
  _ if !tool_calls.is_empty() => StopReason::ToolUse,
688
+ Some("stop") => StopReason::EndTurn,
572
689
  _ => StopReason::Other,
573
690
  };
574
691
 
575
692
  let usage = payload.get("usage");
576
- let get = |k: &str| {
577
- usage
578
- .and_then(|u| u.get(k))
579
- .and_then(|v| v.as_u64())
580
- .unwrap_or(0)
581
- };
693
+ let get = |k: &str| token_count(usage.and_then(|u| u.get(k)));
582
694
  Ok(ProviderResponse {
583
695
  text,
584
696
  tool_calls,
585
697
  stop_reason,
586
698
  input_tokens: get("prompt_tokens"),
587
699
  output_tokens: get("completion_tokens"),
588
- cache_read_tokens: usage
589
- .and_then(|u| u.get("prompt_tokens_details"))
590
- .and_then(|d| d.get("cached_tokens"))
591
- .and_then(|v| v.as_u64())
592
- .unwrap_or(0),
700
+ cache_read_tokens: token_count(
701
+ usage.and_then(|u| u.get("prompt_tokens_details")).and_then(|d| d.get("cached_tokens")),
702
+ ),
593
703
  cache_write_tokens: 0,
594
704
  model: payload
595
705
  .get("model")
@@ -687,6 +797,8 @@ enum Turn {
687
797
  Error(String),
688
798
  /// Text cut off at `max_tokens`.
689
799
  Truncated(String),
800
+ /// A response exactly as given, for wire shapes the others cannot express.
801
+ Response(Box<ProviderResponse>),
690
802
  }
691
803
 
692
804
  impl ScriptedProvider {
@@ -747,6 +859,11 @@ impl ScriptedProvider {
747
859
  )
748
860
  }
749
861
 
862
+ /// Replay these responses verbatim, in order.
863
+ pub fn responses(responses: Vec<ProviderResponse>) -> Self {
864
+ Self::new(responses.into_iter().map(|r| Turn::Response(Box::new(r))).collect(), false)
865
+ }
866
+
750
867
  pub fn always_tool(tool: &str, arguments: Value) -> Self {
751
868
  Self::new(vec![Turn::Tool { name: tool.into(), arguments }], true)
752
869
  }
@@ -804,6 +921,7 @@ impl ModelProvider for ScriptedProvider {
804
921
  let model = req.agent.model.clone();
805
922
  match turn {
806
923
  Turn::Error(e) => Err(e),
924
+ Turn::Response(r) => Ok(*r),
807
925
  Turn::Truncated(text) => Ok(ProviderResponse {
808
926
  raw_content: json!([{"type": "text", "text": text}]),
809
927
  text,
@@ -1223,6 +1341,111 @@ mod tests {
1223
1341
  assert_eq!(out[0]["tool_calls"][0]["function"]["arguments"], "{\"q\":1}");
1224
1342
  }
1225
1343
 
1344
+ #[test]
1345
+ fn openai_tool_calls_under_a_stop_finish_are_still_tool_use() {
1346
+ // Ollama and vLLM answer `finish_reason: "stop"` for a turn of calls.
1347
+ let payload = json!({"choices": [{"finish_reason": "stop", "message": {"content": null,
1348
+ "tool_calls": [{"id": "c1", "type": "function", "function": {"name": "f", "arguments": "{}"}}]}}]});
1349
+ assert_eq!(parse_openai(&payload, "m").unwrap().stop_reason, StopReason::ToolUse);
1350
+ let cut = json!({"choices": [{"finish_reason": "length", "message": {
1351
+ "tool_calls": [{"id": "c1", "function": {"name": "f", "arguments": "{\"a\":"}}]}}]});
1352
+ assert_eq!(parse_openai(&cut, "m").unwrap().stop_reason, StopReason::MaxTokens, "a cut-off turn is never run");
1353
+ }
1354
+
1355
+ #[test]
1356
+ fn tool_call_ids_are_present_and_unique_within_a_turn() {
1357
+ let payload = json!({"choices": [{"finish_reason": "tool_calls", "message": {"tool_calls": [
1358
+ {"function": {"name": "f", "arguments": "{}"}},
1359
+ {"id": "", "function": {"name": "f", "arguments": "{}"}},
1360
+ {"id": "dup", "function": {"name": "f", "arguments": "{}"}},
1361
+ {"id": "dup", "function": {"name": "f", "arguments": "{}"}}
1362
+ ]}}]});
1363
+ let r = parse_openai(&payload, "m").unwrap();
1364
+ let ids: std::collections::HashSet<_> = r.tool_calls.iter().map(|c| c.id.clone()).collect();
1365
+ assert_eq!(ids.len(), 4, "{:?}", r.tool_calls);
1366
+ assert!(ids.iter().all(|i| !i.is_empty()));
1367
+ assert_eq!(r.tool_calls[2].id, "dup", "a usable id is kept");
1368
+ // The replayed turn carries the same ids, so results pair with calls.
1369
+ let replayed: Vec<_> = r.raw_content.as_array().unwrap().iter().map(|b| b["id"].as_str().unwrap().to_string()).collect();
1370
+ assert_eq!(replayed, r.tool_calls.iter().map(|c| c.id.clone()).collect::<Vec<_>>());
1371
+
1372
+ let anthropic = json!({"stop_reason": "tool_use", "content": [
1373
+ {"type": "tool_use", "name": "f", "input": {}},
1374
+ {"type": "tool_use", "id": "", "name": "f", "input": {}}]});
1375
+ let r = parse_anthropic(&anthropic, "m").unwrap();
1376
+ assert_ne!(r.tool_calls[0].id, r.tool_calls[1].id);
1377
+ assert_eq!(r.raw_content[1]["id"], json!(r.tool_calls[1].id));
1378
+ }
1379
+
1380
+ #[test]
1381
+ fn garbled_arguments_are_kept_as_they_came_and_replayed_as_an_object() {
1382
+ let payload = json!({"choices": [{"finish_reason": "tool_calls", "message": {"tool_calls": [
1383
+ {"id": "a", "function": {"name": "f", "arguments": "{\"id\": 5, \"conf"}},
1384
+ {"id": "b", "function": {"name": "f", "arguments": "[1,2]"}},
1385
+ {"id": "c", "function": {"name": "f", "arguments": ""}},
1386
+ {"id": "d", "function": {"name": "f", "arguments": {"x": 1}}}
1387
+ ]}}]});
1388
+ let r = parse_openai(&payload, "m").unwrap();
1389
+ assert_eq!(r.tool_calls[0].arguments, json!("{\"id\": 5, \"conf"), "not silently {{}}");
1390
+ assert_eq!(r.tool_calls[1].arguments, json!([1, 2]));
1391
+ assert_eq!(r.tool_calls[2].arguments, json!({}), "no arguments at all is an empty object");
1392
+ assert_eq!(r.tool_calls[3].arguments, json!({"x": 1}));
1393
+ assert_eq!(r.raw_content[0]["input"], json!({}), "Anthropic replays only an object");
1394
+ let anthropic = json!({"stop_reason": "tool_use", "content": [{"type": "tool_use", "id": "a", "name": "f", "input": "x"}]});
1395
+ let r = parse_anthropic(&anthropic, "m").unwrap();
1396
+ assert_eq!(r.tool_calls[0].arguments, json!("x"));
1397
+ assert_eq!(r.raw_content[0]["input"], json!({}));
1398
+ }
1399
+
1400
+ #[test]
1401
+ fn usage_reported_as_floats_is_still_counted() {
1402
+ let payload = json!({"choices": [{"finish_reason": "stop", "message": {"content": "x"}}],
1403
+ "usage": {"prompt_tokens": 12.0, "completion_tokens": 3.4,
1404
+ "prompt_tokens_details": {"cached_tokens": -1}}});
1405
+ let r = parse_openai(&payload, "m").unwrap();
1406
+ assert_eq!((r.input_tokens, r.output_tokens, r.cache_read_tokens), (12, 3, 0));
1407
+ let r = parse_anthropic(&json!({"content": [], "usage": {"input_tokens": 1e30, "output_tokens": "9"}}), "m").unwrap();
1408
+ assert_eq!((r.input_tokens, r.output_tokens), (u64::MAX, 0), "huge saturates; a string is not a count");
1409
+ }
1410
+
1411
+ #[test]
1412
+ fn an_error_body_that_is_not_json_still_says_what_happened() {
1413
+ assert_eq!(error_detail(br#"{"error": {"message": "overloaded"}}"#), "overloaded");
1414
+ assert_eq!(error_detail(br#"{"error": "bad model"}"#), "bad model");
1415
+ assert_eq!(error_detail(br#"{"error": {"code": 7}}"#), r#"{"code":7}"#);
1416
+ assert_eq!(error_detail(b"<html>\n <h1>502 Bad Gateway</h1>\n</html>"), "<html> <h1>502 Bad Gateway</h1> </html>");
1417
+ assert_eq!(error_detail(b""), "no detail");
1418
+ assert!(error_detail(&vec![b'x'; 5000]).chars().count() <= 201);
1419
+ // The message keeps the shape the classifier reads as WC304.
1420
+ let msg = format!("openai 429 Too Many Requests: {}", error_detail(b"slow down"));
1421
+ assert_eq!(crate::errors::classify_run_error(&msg), "WC304");
1422
+ }
1423
+
1424
+ #[test]
1425
+ fn malformed_success_bodies_are_unreadable_responses_with_their_detail() {
1426
+ use crate::errors::classify_run_error as code;
1427
+ let e = parse_anthropic(&json!({"type": "message"}), "m").unwrap_err();
1428
+ assert_eq!(code(&e), "WC305", "{e}");
1429
+ let e = parse_openai(&json!({"choices": []}), "m").unwrap_err();
1430
+ assert_eq!(code(&e), "WC305", "{e}");
1431
+ let e = parse_openai(&json!({"choices": [{"message": null}]}), "m").unwrap_err();
1432
+ assert_eq!(code(&e), "WC305", "{e}");
1433
+ let e = parse_openai(&json!({"error": {"message": "credit exhausted"}}), "m").unwrap_err();
1434
+ assert!(e.contains("credit exhausted") && code(&e) == "WC304", "{e}");
1435
+ let e = parse_openai(&json!({"error": "nope"}), "m").unwrap_err();
1436
+ assert!(e.contains("nope"), "{e}");
1437
+ }
1438
+
1439
+ #[test]
1440
+ fn retries_back_off_and_honour_a_short_retry_after() {
1441
+ use std::time::Duration;
1442
+ assert_eq!(retry_delay(0, None), Duration::from_millis(250));
1443
+ assert_eq!(retry_delay(1, None), Duration::from_millis(500));
1444
+ assert_eq!(retry_delay(0, Some(Duration::from_secs(2))), Duration::from_secs(2));
1445
+ assert_eq!(retry_delay(0, Some(Duration::from_secs(3600))), Duration::from_millis(250), "a long wait is not honoured");
1446
+ assert_eq!(retry_delay(u32::MAX, None), Duration::from_millis(250 << 6), "no shift overflow");
1447
+ }
1448
+
1226
1449
  #[test]
1227
1450
  fn json_is_recovered_from_prose_and_fences() {
1228
1451
  assert_eq!(recover_json(r#"{"a": 1}"#), Some(json!({"a": 1})));