fascicle 0.14.0 → 0.16.0
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.
- package/CHANGELOG.md +150 -0
- package/README.md +8 -8
- package/dist/acp.d.ts +16 -2
- package/dist/acp.js +1406 -1307
- package/dist/acp.js.map +1 -1
- package/dist/{adapters-DNnzeLxr.js → adapters-CDUzZlby.js} +35 -13
- package/dist/adapters-CDUzZlby.js.map +1 -0
- package/dist/adapters.d.ts +8 -1
- package/dist/adapters.js +1 -1
- package/dist/agents.d.ts +1 -1
- package/dist/agents.js +15 -8
- package/dist/agents.js.map +1 -1
- package/dist/bin/fascicle-viewer.js +4 -1
- package/dist/console-CdB4pcFO.js +37 -0
- package/dist/console-CdB4pcFO.js.map +1 -0
- package/dist/{core-CDrz0NM1.js → core-CErsxGF6.js} +246 -147
- package/dist/core-CErsxGF6.js.map +1 -0
- package/dist/diagram_bin.js +2 -2
- package/dist/diagram_bin.js.map +1 -1
- package/dist/{engine-BDWB2jdn.js → engine-CR8q2IqV.js} +683 -511
- package/dist/engine-CR8q2IqV.js.map +1 -0
- package/dist/index.d.ts +529 -73
- package/dist/index.js +440 -137
- package/dist/index.js.map +1 -1
- package/dist/{invoke-tjTjZLHj.js → invoke-COaeZ-vy.js} +98 -26
- package/dist/invoke-COaeZ-vy.js.map +1 -0
- package/dist/{json_schema_to_standard-r21kW2LI.js → json_schema_to_standard-C4F4lXHE.js} +11 -32
- package/dist/json_schema_to_standard-C4F4lXHE.js.map +1 -0
- package/dist/{leading_system-BQe_405V.js → leading_system-PoHzNHuV.js} +277 -3
- package/dist/leading_system-PoHzNHuV.js.map +1 -0
- package/dist/mcp.d.ts +6 -5
- package/dist/mcp.js +11 -6
- package/dist/mcp.js.map +1 -1
- package/dist/otel.js +10 -10
- package/dist/otel.js.map +1 -1
- package/dist/records-1HU7mWXf.js +22 -0
- package/dist/records-1HU7mWXf.js.map +1 -0
- package/dist/{search-CqH5hmuF.js → search-CpCJYMUm.js} +73 -10
- package/dist/search-CpCJYMUm.js.map +1 -0
- package/dist/search_worker.js +1 -1
- package/dist/stdio.js +7 -3
- package/dist/stdio.js.map +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +3 -3
- package/dist/testing.js.map +1 -1
- package/dist/{trajectory-Bm-mOtqw.d.ts → trajectory-BKGNxi7u.d.ts} +10 -9
- package/dist/{trajectory-BI-mrVFT.js → trajectory-DeURWIhj.js} +6 -5
- package/dist/trajectory-DeURWIhj.js.map +1 -0
- package/dist/{types-C_Rmcgck.d.ts → types-Sw0oHW-_.d.ts} +47 -9
- package/dist/ui.d.ts +2 -1
- package/dist/ui.js +8 -1
- package/dist/ui.js.map +1 -1
- package/dist/viewer-app/assets/index-BYF5Dp7d.js +2 -0
- package/dist/viewer-app/index.html +2 -1
- package/dist/viewer.d.ts +26 -12
- package/dist/viewer.js +282 -63
- package/dist/viewer.js.map +1 -1
- package/package.json +4 -4
- package/dist/adapters-DNnzeLxr.js.map +0 -1
- package/dist/core-CDrz0NM1.js.map +0 -1
- package/dist/engine-BDWB2jdn.js.map +0 -1
- package/dist/invoke-tjTjZLHj.js.map +0 -1
- package/dist/json_schema_to_standard-r21kW2LI.js.map +0 -1
- package/dist/leading_system-BQe_405V.js.map +0 -1
- package/dist/search-CqH5hmuF.js.map +0 -1
- package/dist/trajectory-BI-mrVFT.js.map +0 -1
- package/dist/viewer-app/assets/index-Fnh5HKSU.js +0 -2
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { T as validate_schema, f as compute_backoff, k as aborted_error, p as wait_with_abort, w as format_schema_issues } from "./core-CErsxGF6.js";
|
|
2
|
+
import { n as is_record$1 } from "./records-1HU7mWXf.js";
|
|
3
|
+
import { A as tool_error, C as provider_capability_error, D as rate_limit_error, E as provider_required_error, O as schema_validation_error, S as provider_auth_error, T as provider_not_configured_error, _ as engine_config_error, b as model_required_error, c as read_dialect_init, d as load_optional_peer, f as missing_peer_error, g as claude_cli_error, h as translate_effort_to_sdk_reasoning, i as invoke_http_turn, j as turn_timeout_error, k as tool_approval_denied_error, l as split_assistant_parts, m as resolve_transport, n as consume_framed_stream, o as map_messages_by_role, p as normalize_local_usage, r as create_line_splitter, s as parse_tool_call_arguments, t as split_leading_system_run, u as default_normalize_usage, v as engine_disposed_error, w as provider_error, x as on_chunk_error, y as incomplete_generation_error } from "./leading_system-PoHzNHuV.js";
|
|
3
4
|
import { join } from "node:path";
|
|
4
5
|
import { mkdtempSync, writeFileSync } from "node:fs";
|
|
5
6
|
import { tmpdir } from "node:os";
|
|
@@ -104,7 +105,7 @@ function close_nested(node) {
|
|
|
104
105
|
const closed = { ...node };
|
|
105
106
|
for (const key of SCHEMA_BAGS) {
|
|
106
107
|
const bag = closed[key];
|
|
107
|
-
if (
|
|
108
|
+
if (is_record$1(bag)) closed[key] = close_bag(bag);
|
|
108
109
|
}
|
|
109
110
|
for (const key of SCHEMA_SLOTS) if (key in closed) closed[key] = close_value(closed[key]);
|
|
110
111
|
return closed;
|
|
@@ -118,7 +119,7 @@ function close_bag(bag) {
|
|
|
118
119
|
/** Recurse into a schema or a list of them, leaving anything else alone. */
|
|
119
120
|
function close_value(value) {
|
|
120
121
|
if (Array.isArray(value)) return value.map(close_value);
|
|
121
|
-
return
|
|
122
|
+
return is_record$1(value) ? close_objects(value) : value;
|
|
122
123
|
}
|
|
123
124
|
/** True for an object node listing its keys but not saying whether more are allowed. */
|
|
124
125
|
function needs_closing(node) {
|
|
@@ -126,10 +127,6 @@ function needs_closing(node) {
|
|
|
126
127
|
const type = node["type"];
|
|
127
128
|
return type === "object" || Array.isArray(type) && type.includes("object");
|
|
128
129
|
}
|
|
129
|
-
/** A JSON Schema node, as opposed to a boolean schema, a list, or a scalar. */
|
|
130
|
-
function is_node(value) {
|
|
131
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
132
|
-
}
|
|
133
130
|
/**
|
|
134
131
|
* Drop the top-level `$schema` and `$id` keys.
|
|
135
132
|
*
|
|
@@ -143,7 +140,33 @@ function strip_meta_keys(json) {
|
|
|
143
140
|
//#endregion
|
|
144
141
|
//#region src/engine/pricing.ts
|
|
145
142
|
const FREE_PROVIDERS = /* @__PURE__ */ new Set(["ollama", "lmstudio"]);
|
|
143
|
+
const CACHE_READ_MULTIPLIER = .1;
|
|
144
|
+
const CACHE_WRITE_MULTIPLIER = 1.25;
|
|
146
145
|
const DEFAULT_PRICING = Object.freeze({
|
|
146
|
+
"anthropic:claude-fable-5-1": {
|
|
147
|
+
input_per_million: 10,
|
|
148
|
+
output_per_million: 50,
|
|
149
|
+
cached_input_per_million: .25,
|
|
150
|
+
cache_write_per_million: 12.5
|
|
151
|
+
},
|
|
152
|
+
"anthropic:claude-fable-5": {
|
|
153
|
+
input_per_million: 10,
|
|
154
|
+
output_per_million: 50,
|
|
155
|
+
cached_input_per_million: 1,
|
|
156
|
+
cache_write_per_million: 12.5
|
|
157
|
+
},
|
|
158
|
+
"anthropic:claude-opus-5-5": {
|
|
159
|
+
input_per_million: 4,
|
|
160
|
+
output_per_million: 20,
|
|
161
|
+
cached_input_per_million: .2,
|
|
162
|
+
cache_write_per_million: 5
|
|
163
|
+
},
|
|
164
|
+
"anthropic:claude-opus-5": {
|
|
165
|
+
input_per_million: 5,
|
|
166
|
+
output_per_million: 25,
|
|
167
|
+
cached_input_per_million: .5,
|
|
168
|
+
cache_write_per_million: 6.25
|
|
169
|
+
},
|
|
147
170
|
"anthropic:claude-opus-4-8": {
|
|
148
171
|
input_per_million: 5,
|
|
149
172
|
output_per_million: 25,
|
|
@@ -162,18 +185,54 @@ const DEFAULT_PRICING = Object.freeze({
|
|
|
162
185
|
cached_input_per_million: .5,
|
|
163
186
|
cache_write_per_million: 6.25
|
|
164
187
|
},
|
|
188
|
+
"anthropic:claude-sonnet-5-5": {
|
|
189
|
+
input_per_million: 2,
|
|
190
|
+
output_per_million: 10,
|
|
191
|
+
cached_input_per_million: .1,
|
|
192
|
+
cache_write_per_million: 2.5
|
|
193
|
+
},
|
|
194
|
+
"anthropic:claude-sonnet-5": {
|
|
195
|
+
input_per_million: 2,
|
|
196
|
+
output_per_million: 10,
|
|
197
|
+
cached_input_per_million: .2,
|
|
198
|
+
cache_write_per_million: 2.5
|
|
199
|
+
},
|
|
165
200
|
"anthropic:claude-sonnet-4-6": {
|
|
166
201
|
input_per_million: 3,
|
|
167
202
|
output_per_million: 15,
|
|
168
203
|
cached_input_per_million: .3,
|
|
169
204
|
cache_write_per_million: 3.75
|
|
170
205
|
},
|
|
206
|
+
"anthropic:claude-haiku-5-5": {
|
|
207
|
+
input_per_million: .1,
|
|
208
|
+
output_per_million: .5,
|
|
209
|
+
cached_input_per_million: .01,
|
|
210
|
+
cache_write_per_million: .125
|
|
211
|
+
},
|
|
171
212
|
"anthropic:claude-haiku-4-5": {
|
|
172
213
|
input_per_million: 1,
|
|
173
214
|
output_per_million: 5,
|
|
174
215
|
cached_input_per_million: .1,
|
|
175
216
|
cache_write_per_million: 1.25
|
|
176
217
|
},
|
|
218
|
+
"openai:gpt-6-astra": {
|
|
219
|
+
input_per_million: 10,
|
|
220
|
+
output_per_million: 50,
|
|
221
|
+
cached_input_per_million: 1,
|
|
222
|
+
cache_write_per_million: 12.5
|
|
223
|
+
},
|
|
224
|
+
"openai:gpt-6.1-sol": {
|
|
225
|
+
input_per_million: 2,
|
|
226
|
+
output_per_million: 10,
|
|
227
|
+
cached_input_per_million: .1,
|
|
228
|
+
cache_write_per_million: 2.5
|
|
229
|
+
},
|
|
230
|
+
"openai:gpt-6-luna": {
|
|
231
|
+
input_per_million: .1,
|
|
232
|
+
output_per_million: .5,
|
|
233
|
+
cached_input_per_million: .01,
|
|
234
|
+
cache_write_per_million: .125
|
|
235
|
+
},
|
|
177
236
|
"openai:gpt-4o": {
|
|
178
237
|
input_per_million: 2.5,
|
|
179
238
|
output_per_million: 10,
|
|
@@ -184,13 +243,30 @@ const DEFAULT_PRICING = Object.freeze({
|
|
|
184
243
|
output_per_million: .6,
|
|
185
244
|
cached_input_per_million: .075
|
|
186
245
|
},
|
|
246
|
+
"google:gemini-4-argon": {
|
|
247
|
+
input_per_million: 2,
|
|
248
|
+
output_per_million: 10,
|
|
249
|
+
cached_input_per_million: .1
|
|
250
|
+
},
|
|
251
|
+
"google:gemini-3.8-flash": {
|
|
252
|
+
input_per_million: .75,
|
|
253
|
+
output_per_million: 3.75,
|
|
254
|
+
cached_input_per_million: .075
|
|
255
|
+
},
|
|
256
|
+
"google:gemini-3.5-flash-lite": {
|
|
257
|
+
input_per_million: .3,
|
|
258
|
+
output_per_million: 2.5,
|
|
259
|
+
cached_input_per_million: .03
|
|
260
|
+
},
|
|
187
261
|
"google:gemini-2.5-pro": {
|
|
188
262
|
input_per_million: 1.25,
|
|
189
|
-
output_per_million:
|
|
263
|
+
output_per_million: 10,
|
|
264
|
+
cached_input_per_million: .125
|
|
190
265
|
},
|
|
191
266
|
"google:gemini-2.5-flash": {
|
|
192
|
-
input_per_million: .
|
|
193
|
-
output_per_million: .
|
|
267
|
+
input_per_million: .3,
|
|
268
|
+
output_per_million: 2.5,
|
|
269
|
+
cached_input_per_million: .03
|
|
194
270
|
}
|
|
195
271
|
});
|
|
196
272
|
/**
|
|
@@ -205,7 +281,7 @@ function pricing_key(provider, model_id) {
|
|
|
205
281
|
* Per-token rates produce long floating-point tails; 6 places keeps
|
|
206
282
|
* sub-cent precision without leaking binary rounding noise into reports.
|
|
207
283
|
*/
|
|
208
|
-
function round6
|
|
284
|
+
function round6(value) {
|
|
209
285
|
return Math.round(value * 1e6) / 1e6;
|
|
210
286
|
}
|
|
211
287
|
/**
|
|
@@ -238,7 +314,7 @@ function apply_reasoning_cost(plain_output_usd_raw, reasoning, has_reasoning_rat
|
|
|
238
314
|
if (reasoning > 0) {
|
|
239
315
|
if (has_reasoning_rate) return {
|
|
240
316
|
output_usd_raw: plain_output_usd_raw,
|
|
241
|
-
surface_reasoning_usd: round6
|
|
317
|
+
surface_reasoning_usd: round6(reasoning_usd_raw)
|
|
242
318
|
};
|
|
243
319
|
return {
|
|
244
320
|
output_usd_raw: plain_output_usd_raw + reasoning_usd_raw,
|
|
@@ -276,17 +352,61 @@ function compute_cost(usage, pricing, provider) {
|
|
|
276
352
|
const reasoning_usd_raw = reasoning * reasoning_rate / 1e6;
|
|
277
353
|
const { output_usd_raw, surface_reasoning_usd } = apply_reasoning_cost(Math.max(0, usage.output_tokens - reasoning) * pricing.output_per_million / 1e6, reasoning, pricing.reasoning_per_million !== void 0, reasoning_usd_raw);
|
|
278
354
|
const breakdown = {
|
|
279
|
-
total_usd: round6
|
|
280
|
-
input_usd: round6
|
|
281
|
-
output_usd: round6
|
|
355
|
+
total_usd: round6(input_usd_raw + cached_usd_raw + cache_write_usd_raw + (surface_reasoning_usd !== void 0 ? reasoning_usd_raw : 0) + output_usd_raw),
|
|
356
|
+
input_usd: round6(input_usd_raw),
|
|
357
|
+
output_usd: round6(output_usd_raw),
|
|
282
358
|
currency: "USD",
|
|
283
359
|
is_estimate: true
|
|
284
360
|
};
|
|
285
|
-
if (cached > 0) breakdown.cached_input_usd = round6
|
|
286
|
-
if (cache_write > 0) breakdown.cache_write_usd = round6
|
|
361
|
+
if (cached > 0) breakdown.cached_input_usd = round6(cached_usd_raw);
|
|
362
|
+
if (cache_write > 0) breakdown.cache_write_usd = round6(cache_write_usd_raw);
|
|
287
363
|
if (surface_reasoning_usd !== void 0) breakdown.reasoning_usd = surface_reasoning_usd;
|
|
288
364
|
return breakdown;
|
|
289
365
|
}
|
|
366
|
+
/**
|
|
367
|
+
* Split a reported total cost into an input/output cost breakdown, weighted
|
|
368
|
+
* by token counts.
|
|
369
|
+
*
|
|
370
|
+
* Cache-read and cache-write tokens are cheaper and pricier respectively
|
|
371
|
+
* than base input tokens, so they're weighted by `CACHE_READ_MULTIPLIER`
|
|
372
|
+
* and `CACHE_WRITE_MULTIPLIER` before the total is distributed
|
|
373
|
+
* proportionally across all weighted components. The total is what the
|
|
374
|
+
* provider charged and the split is estimated, so the breakdown keeps
|
|
375
|
+
* `is_estimate: true`. Returns an all-zero estimate when there's no usage or
|
|
376
|
+
* no cost to distribute.
|
|
377
|
+
*/
|
|
378
|
+
function decompose_total_cost(total_cost_usd, usage) {
|
|
379
|
+
const input = Math.max(0, usage.input_tokens);
|
|
380
|
+
const output = Math.max(0, usage.output_tokens);
|
|
381
|
+
const cached = Math.max(0, usage.cached_input_tokens ?? 0);
|
|
382
|
+
const cache_write = Math.max(0, usage.cache_write_tokens ?? 0);
|
|
383
|
+
const input_weight = Math.max(0, input - cached - cache_write);
|
|
384
|
+
const output_weight = output;
|
|
385
|
+
const cached_weight = cached * CACHE_READ_MULTIPLIER;
|
|
386
|
+
const cache_write_weight = cache_write * CACHE_WRITE_MULTIPLIER;
|
|
387
|
+
const total_weight = input_weight + output_weight + cached_weight + cache_write_weight;
|
|
388
|
+
if (total_weight === 0 || total_cost_usd === 0) return {
|
|
389
|
+
total_usd: total_cost_usd,
|
|
390
|
+
input_usd: 0,
|
|
391
|
+
output_usd: 0,
|
|
392
|
+
currency: "USD",
|
|
393
|
+
is_estimate: true
|
|
394
|
+
};
|
|
395
|
+
const input_usd = total_cost_usd * input_weight / total_weight;
|
|
396
|
+
const output_usd = total_cost_usd * output_weight / total_weight;
|
|
397
|
+
const cached_usd_raw = total_cost_usd * cached_weight / total_weight;
|
|
398
|
+
const cache_write_usd_raw = total_cost_usd * cache_write_weight / total_weight;
|
|
399
|
+
const breakdown = {
|
|
400
|
+
total_usd: total_cost_usd,
|
|
401
|
+
input_usd,
|
|
402
|
+
output_usd,
|
|
403
|
+
currency: "USD",
|
|
404
|
+
is_estimate: true
|
|
405
|
+
};
|
|
406
|
+
if (cached > 0) breakdown.cached_input_usd = cached_usd_raw;
|
|
407
|
+
if (cache_write > 0) breakdown.cache_write_usd = cache_write_usd_raw;
|
|
408
|
+
return breakdown;
|
|
409
|
+
}
|
|
290
410
|
//#endregion
|
|
291
411
|
//#region src/engine/retry.ts
|
|
292
412
|
const DEFAULT_RETRY = {
|
|
@@ -509,227 +629,6 @@ function classify_retryable(err) {
|
|
|
509
629
|
if (kind === "network" || kind === "timeout") return build_retryable_signal(err, kind);
|
|
510
630
|
}
|
|
511
631
|
//#endregion
|
|
512
|
-
//#region src/engine/providers/native_shared.ts
|
|
513
|
-
/**
|
|
514
|
-
* The message under a JSON error envelope's `error` key, or undefined when
|
|
515
|
-
* the shape carries none worth surfacing.
|
|
516
|
-
*/
|
|
517
|
-
function error_field_message(parsed) {
|
|
518
|
-
if (parsed === null || typeof parsed !== "object") return void 0;
|
|
519
|
-
const error = Reflect.get(parsed, "error");
|
|
520
|
-
if (typeof error === "string" && error.length > 0) return error;
|
|
521
|
-
if (error === null || typeof error !== "object") return void 0;
|
|
522
|
-
const message = Reflect.get(error, "message");
|
|
523
|
-
return typeof message === "string" && message.length > 0 ? message : void 0;
|
|
524
|
-
}
|
|
525
|
-
/**
|
|
526
|
-
* Pull a human-readable message out of a non-2xx body. The native wires share
|
|
527
|
-
* one envelope family: Anthropic sends `{ type: 'error', error: { message } }`,
|
|
528
|
-
* OpenAI-style wires send `{ error: { message } }`, and Ollama sends
|
|
529
|
-
* `{ error: "message" }` (with the nested shape appearing behind proxies), so
|
|
530
|
-
* one parser accepts a string or a `{ message }` object under `error` and
|
|
531
|
-
* falls back to the raw body, truncated to 300 chars.
|
|
532
|
-
*/
|
|
533
|
-
function extract_error_message(body) {
|
|
534
|
-
if (body.length === 0) return "(empty body)";
|
|
535
|
-
let message;
|
|
536
|
-
try {
|
|
537
|
-
message = error_field_message(JSON.parse(body));
|
|
538
|
-
} catch {}
|
|
539
|
-
if (message !== void 0) return message;
|
|
540
|
-
return body.length > 300 ? `${body.slice(0, 300)}...` : body;
|
|
541
|
-
}
|
|
542
|
-
/**
|
|
543
|
-
* Map a non-2xx response to what the engine's retry stack expects: 401
|
|
544
|
-
* becomes a typed provider_auth_error, 429/5xx keep `status` +
|
|
545
|
-
* `responseHeaders` so classify_provider_error marks them retryable
|
|
546
|
-
* (rate_limit honoring retry-after, provider_5xx), and any other 4xx is a
|
|
547
|
-
* permanent provider_error that surfaces as-is.
|
|
548
|
-
*/
|
|
549
|
-
async function response_error(response, provider) {
|
|
550
|
-
let body = "";
|
|
551
|
-
try {
|
|
552
|
-
body = await response.text();
|
|
553
|
-
} catch {}
|
|
554
|
-
const detail = extract_error_message(body);
|
|
555
|
-
const status = response.status;
|
|
556
|
-
if (status === 401) return new provider_auth_error(provider, `${provider} authentication failed (401): ${detail}`);
|
|
557
|
-
if (status === 429 || status >= 500) {
|
|
558
|
-
const retry_after = response.headers.get("retry-after");
|
|
559
|
-
return Object.assign(/* @__PURE__ */ new Error(`${provider} API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
|
|
560
|
-
}
|
|
561
|
-
return new provider_error(`${provider} API error ${status}: ${detail}`, {
|
|
562
|
-
status,
|
|
563
|
-
body
|
|
564
|
-
});
|
|
565
|
-
}
|
|
566
|
-
/**
|
|
567
|
-
* A user abort surfaces as the fetch/reader AbortError; retry_turn converts
|
|
568
|
-
* it via its own signal check, so it is rethrown untouched. Everything else
|
|
569
|
-
* is a transport failure wrapped in the `kind: 'network'` shape the shared
|
|
570
|
-
* classify_provider_error marks retryable.
|
|
571
|
-
*/
|
|
572
|
-
function rethrow_network_failure(err, abort, provider) {
|
|
573
|
-
if (abort.aborted) throw err;
|
|
574
|
-
const detail = err instanceof Error ? err.message : String(err);
|
|
575
|
-
throw Object.assign(/* @__PURE__ */ new Error(`${provider} native: network failure: ${detail}`), { kind: "network" });
|
|
576
|
-
}
|
|
577
|
-
/**
|
|
578
|
-
* Run one native turn over HTTP: POST the dialect's body, map a non-2xx to
|
|
579
|
-
* the retry stack's shapes, and hand the response to the dialect's stream or
|
|
580
|
-
* JSON consumer. `build_body` is called inside the network try/catch on
|
|
581
|
-
* purpose, so a mapper that throws surfaces as a retryable network failure
|
|
582
|
-
* rather than escaping the turn unclassified.
|
|
583
|
-
*/
|
|
584
|
-
async function invoke_http_turn(req, provider, request, consume) {
|
|
585
|
-
let response;
|
|
586
|
-
try {
|
|
587
|
-
response = await fetch(request.url, {
|
|
588
|
-
method: "POST",
|
|
589
|
-
headers: request.headers,
|
|
590
|
-
body: JSON.stringify(request.build_body()),
|
|
591
|
-
signal: req.abort
|
|
592
|
-
});
|
|
593
|
-
} catch (err) {
|
|
594
|
-
rethrow_network_failure(err, req.abort, provider);
|
|
595
|
-
}
|
|
596
|
-
if (!response.ok) throw await response_error(response, provider);
|
|
597
|
-
if (req.stream) return consume.stream(response);
|
|
598
|
-
const payload = await response.json();
|
|
599
|
-
return consume.parse(payload);
|
|
600
|
-
}
|
|
601
|
-
/**
|
|
602
|
-
* Drain a streaming response: decode bytes, reassemble framed payloads (SSE
|
|
603
|
-
* events or NDJSON lines), and feed them to the dialect's aggregator, which
|
|
604
|
-
* dispatches chunks through req.dispatch_chunk as they arrive. Transport
|
|
605
|
-
* failures mid-read wrap as network errors; aggregator throws (malformed
|
|
606
|
-
* frames, mid-stream error events, a rejecting on_chunk) pass through
|
|
607
|
-
* untouched, with the reader cancelled so the connection is released.
|
|
608
|
-
*/
|
|
609
|
-
async function consume_framed_stream(response, req, provider, framing, create_aggregator) {
|
|
610
|
-
const body = response.body;
|
|
611
|
-
if (body === null) throw new provider_error(`${provider} native: streaming response has no body`);
|
|
612
|
-
const aggregator = create_aggregator(req.dispatch_chunk ?? (async () => {}));
|
|
613
|
-
const reader = body.getReader();
|
|
614
|
-
const text_decoder = new TextDecoder();
|
|
615
|
-
const next_bytes = async () => {
|
|
616
|
-
let step;
|
|
617
|
-
try {
|
|
618
|
-
step = await reader.read();
|
|
619
|
-
} catch (err) {
|
|
620
|
-
rethrow_network_failure(err, req.abort, provider);
|
|
621
|
-
}
|
|
622
|
-
return step.done ? void 0 : step.value;
|
|
623
|
-
};
|
|
624
|
-
try {
|
|
625
|
-
while (true) {
|
|
626
|
-
const bytes = await next_bytes();
|
|
627
|
-
if (bytes === void 0) break;
|
|
628
|
-
for (const data of framing.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle(data);
|
|
629
|
-
}
|
|
630
|
-
} finally {
|
|
631
|
-
reader.cancel().catch(() => {});
|
|
632
|
-
}
|
|
633
|
-
const tail = [...framing.push(text_decoder.decode()), ...framing.flush()];
|
|
634
|
-
for (const data of tail) await aggregator.handle(data);
|
|
635
|
-
return aggregator.complete();
|
|
636
|
-
}
|
|
637
|
-
/**
|
|
638
|
-
* Incremental newline framing shared by the SSE and NDJSON decoders: buffer
|
|
639
|
-
* decoded text across arbitrary chunk boundaries, hand each completed line
|
|
640
|
-
* (CR trimmed) to `handle_line`, and on flush hand over an unterminated tail
|
|
641
|
-
* line, then `on_flush` for state a decoder holds across lines (an SSE event
|
|
642
|
-
* never closed by a blank line).
|
|
643
|
-
*/
|
|
644
|
-
function create_line_splitter(handle_line, on_flush) {
|
|
645
|
-
let buffer = "";
|
|
646
|
-
const take_line = (raw, out) => {
|
|
647
|
-
handle_line(raw.endsWith("\r") ? raw.slice(0, -1) : raw, out);
|
|
648
|
-
};
|
|
649
|
-
return {
|
|
650
|
-
push(text) {
|
|
651
|
-
buffer += text;
|
|
652
|
-
const out = [];
|
|
653
|
-
let newline = buffer.indexOf("\n");
|
|
654
|
-
while (newline >= 0) {
|
|
655
|
-
take_line(buffer.slice(0, newline), out);
|
|
656
|
-
buffer = buffer.slice(newline + 1);
|
|
657
|
-
newline = buffer.indexOf("\n");
|
|
658
|
-
}
|
|
659
|
-
return out;
|
|
660
|
-
},
|
|
661
|
-
flush() {
|
|
662
|
-
const out = [];
|
|
663
|
-
if (buffer.length > 0) take_line(buffer, out);
|
|
664
|
-
buffer = "";
|
|
665
|
-
on_flush?.(out);
|
|
666
|
-
return out;
|
|
667
|
-
}
|
|
668
|
-
};
|
|
669
|
-
}
|
|
670
|
-
/**
|
|
671
|
-
* Decode a tool call's wire `arguments` string into the engine's input value:
|
|
672
|
-
* non-empty JSON parses (bad JSON is a provider_error naming the tool), and
|
|
673
|
-
* an absent or empty value means `{}`, the shape some servers send for
|
|
674
|
-
* no-argument calls. The Ollama wire's structured-object arguments are its
|
|
675
|
-
* caller's business before this runs.
|
|
676
|
-
*/
|
|
677
|
-
function parse_tool_call_arguments(raw_args, provider, tool_name) {
|
|
678
|
-
if (typeof raw_args === "string" && raw_args.length > 0) try {
|
|
679
|
-
return JSON.parse(raw_args);
|
|
680
|
-
} catch {
|
|
681
|
-
throw new provider_error(`${provider} native: tool_calls arguments for ${tool_name} is not valid JSON`);
|
|
682
|
-
}
|
|
683
|
-
return {};
|
|
684
|
-
}
|
|
685
|
-
/**
|
|
686
|
-
* Fold engine messages into a wire's message array by role. Serves the wires
|
|
687
|
-
* whose messages map 1:1 in place (chat/completions, Ollama's /api/chat); the
|
|
688
|
-
* Messages API's system-hoisting and role-merging rules live in its own
|
|
689
|
-
* mapper instead.
|
|
690
|
-
*/
|
|
691
|
-
function map_messages_by_role(messages, wire) {
|
|
692
|
-
const out = [];
|
|
693
|
-
for (const message of messages) switch (message.role) {
|
|
694
|
-
case "system":
|
|
695
|
-
out.push(wire.system(message.content));
|
|
696
|
-
break;
|
|
697
|
-
case "user":
|
|
698
|
-
out.push(wire.user(message.content));
|
|
699
|
-
break;
|
|
700
|
-
case "assistant":
|
|
701
|
-
out.push(wire.assistant(message.content));
|
|
702
|
-
break;
|
|
703
|
-
case "tool": out.push(wire.tool(message));
|
|
704
|
-
}
|
|
705
|
-
return out;
|
|
706
|
-
}
|
|
707
|
-
/**
|
|
708
|
-
* Partition assistant content parts into the text runs and tool calls the
|
|
709
|
-
* wire mappers serialize separately.
|
|
710
|
-
*/
|
|
711
|
-
function split_assistant_parts(parts) {
|
|
712
|
-
const texts = [];
|
|
713
|
-
const tool_parts = [];
|
|
714
|
-
for (const part of parts) if (part.type === "text") texts.push(part.text);
|
|
715
|
-
else tool_parts.push(part);
|
|
716
|
-
return {
|
|
717
|
-
texts,
|
|
718
|
-
tool_parts
|
|
719
|
-
};
|
|
720
|
-
}
|
|
721
|
-
/**
|
|
722
|
-
* Read the api_key and base_url every OpenAI-compatible dialect starts from:
|
|
723
|
-
* a missing or non-string api_key becomes '' (the core's empty-key guard owns
|
|
724
|
-
* the throw), and an absent base_url falls back to the dialect's default.
|
|
725
|
-
*/
|
|
726
|
-
function read_dialect_init(init, default_base_url) {
|
|
727
|
-
return {
|
|
728
|
-
api_key: typeof init.api_key === "string" ? init.api_key : "",
|
|
729
|
-
base_url: typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : default_base_url
|
|
730
|
-
};
|
|
731
|
-
}
|
|
732
|
-
//#endregion
|
|
733
632
|
//#region src/engine/providers/sse_native.ts
|
|
734
633
|
/**
|
|
735
634
|
* Incremental SSE decoder shared by the native (raw-HTTP) provider adapters.
|
|
@@ -787,10 +686,10 @@ function create_sse_decoder() {
|
|
|
787
686
|
* construction rather than by parallel code paths.
|
|
788
687
|
*/
|
|
789
688
|
/**
|
|
790
|
-
* Effort level to
|
|
791
|
-
*
|
|
792
|
-
*
|
|
793
|
-
*
|
|
689
|
+
* Effort level to the room native adds to its max_tokens default for
|
|
690
|
+
* adaptive thinking. Neither transport sends these as a budget: native
|
|
691
|
+
* sends `output_config.effort`, and the ai_sdk transport hands the level to
|
|
692
|
+
* the SDK's `reasoning` option.
|
|
794
693
|
*/
|
|
795
694
|
const ANTHROPIC_THINKING_BUDGETS = {
|
|
796
695
|
none: 0,
|
|
@@ -808,9 +707,9 @@ const ANTHROPIC_VERSION = "2023-06-01";
|
|
|
808
707
|
*/
|
|
809
708
|
const DEFAULT_BASE_URL$2 = "https://api.anthropic.com/v1";
|
|
810
709
|
/**
|
|
811
|
-
* The Messages API requires max_tokens on every request.
|
|
812
|
-
*
|
|
813
|
-
*
|
|
710
|
+
* The Messages API requires max_tokens on every request. Thinking spends
|
|
711
|
+
* from max_tokens, so with effort set the default rides on top of that
|
|
712
|
+
* effort's budget-table value rather than being swallowed by it.
|
|
814
713
|
*/
|
|
815
714
|
const DEFAULT_MAX_TOKENS = 4096;
|
|
816
715
|
/**
|
|
@@ -838,19 +737,44 @@ function to_user_blocks(content) {
|
|
|
838
737
|
return blocks;
|
|
839
738
|
}
|
|
840
739
|
/**
|
|
841
|
-
* Map
|
|
842
|
-
*
|
|
740
|
+
* Map a reasoning part back to the block it came from: a `signature` under
|
|
741
|
+
* `anthropic` is a thinking block, its text as it was (empty included), and
|
|
742
|
+
* `redactedData` is a redacted_thinking block. A part with neither (keyed for
|
|
743
|
+
* another provider, or carrying no data) maps to no block, since Anthropic
|
|
744
|
+
* refuses thinking it didn't sign.
|
|
745
|
+
*/
|
|
746
|
+
function reasoning_blocks(part) {
|
|
747
|
+
const own = part.provider_options?.["anthropic"];
|
|
748
|
+
const signature = own?.["signature"];
|
|
749
|
+
if (typeof signature === "string") return [{
|
|
750
|
+
type: "thinking",
|
|
751
|
+
thinking: part.text,
|
|
752
|
+
signature
|
|
753
|
+
}];
|
|
754
|
+
const data = own?.["redactedData"];
|
|
755
|
+
return typeof data === "string" ? [{
|
|
756
|
+
type: "redacted_thinking",
|
|
757
|
+
data
|
|
758
|
+
}] : [];
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* Map assistant message content to Messages-API blocks in part order: text
|
|
762
|
+
* parts become text blocks (empties dropped), tool-call parts become tool_use
|
|
763
|
+
* blocks, and Anthropic's own reasoning parts become the thinking and
|
|
764
|
+
* redacted_thinking blocks they came from. Order is kept because Anthropic
|
|
765
|
+
* refuses a tool-use turn whose thinking blocks moved.
|
|
843
766
|
*/
|
|
844
767
|
function to_assistant_blocks(content) {
|
|
845
768
|
if (typeof content === "string") return text_blocks(content);
|
|
846
769
|
const blocks = [];
|
|
847
770
|
for (const part of content) if (part.type === "text") blocks.push(...text_blocks(part.text));
|
|
848
|
-
else blocks.push({
|
|
771
|
+
else if (part.type === "tool_call") blocks.push({
|
|
849
772
|
type: "tool_use",
|
|
850
773
|
id: part.id,
|
|
851
774
|
name: part.name,
|
|
852
775
|
input: part.input
|
|
853
776
|
});
|
|
777
|
+
else blocks.push(...reasoning_blocks(part));
|
|
854
778
|
return blocks;
|
|
855
779
|
}
|
|
856
780
|
/**
|
|
@@ -910,29 +834,30 @@ function to_anthropic_tools(tools) {
|
|
|
910
834
|
}
|
|
911
835
|
/**
|
|
912
836
|
* Build the Messages-API request body for one turn: mapped messages and
|
|
913
|
-
* tools,
|
|
914
|
-
* and the `provider_options.anthropic` wire
|
|
837
|
+
* tools, effort as adaptive thinking plus `output_config.effort`, the
|
|
838
|
+
* required max_tokens default, and the `provider_options.anthropic` wire
|
|
839
|
+
* passthrough merged last.
|
|
915
840
|
*/
|
|
916
841
|
function build_messages_body(req) {
|
|
917
842
|
const { system: hoisted_system, messages } = to_anthropic_messages(req.messages);
|
|
918
843
|
const system_parts = [req.system, hoisted_system].filter((s) => typeof s === "string" && s.length > 0);
|
|
919
|
-
const budget = ANTHROPIC_THINKING_BUDGETS[req.effort];
|
|
920
|
-
const thinking_enabled = budget > 0;
|
|
921
844
|
const body = {
|
|
922
845
|
model: req.model_id,
|
|
923
846
|
messages,
|
|
924
|
-
max_tokens: req.max_tokens ??
|
|
847
|
+
max_tokens: req.max_tokens ?? ANTHROPIC_THINKING_BUDGETS[req.effort] + DEFAULT_MAX_TOKENS
|
|
925
848
|
};
|
|
926
849
|
if (system_parts.length > 0) body["system"] = system_parts.join("\n\n");
|
|
927
850
|
if (req.tools.length > 0) body["tools"] = to_anthropic_tools(req.tools);
|
|
928
851
|
if (req.stream) body["stream"] = true;
|
|
929
|
-
if (
|
|
930
|
-
type: "enabled",
|
|
931
|
-
budget_tokens: budget
|
|
932
|
-
};
|
|
933
|
-
else {
|
|
852
|
+
if (req.effort === "none") {
|
|
934
853
|
if (req.temperature !== void 0) body["temperature"] = req.temperature;
|
|
935
854
|
if (req.top_p !== void 0) body["top_p"] = req.top_p;
|
|
855
|
+
} else {
|
|
856
|
+
body["thinking"] = {
|
|
857
|
+
type: "adaptive",
|
|
858
|
+
display: "summarized"
|
|
859
|
+
};
|
|
860
|
+
body["output_config"] = { effort: req.effort };
|
|
936
861
|
}
|
|
937
862
|
const passthrough = req.provider_options?.["anthropic"];
|
|
938
863
|
return passthrough === void 0 ? body : {
|
|
@@ -993,56 +918,103 @@ function read_tool_use_identity(block, where) {
|
|
|
993
918
|
};
|
|
994
919
|
}
|
|
995
920
|
/**
|
|
996
|
-
*
|
|
997
|
-
*
|
|
998
|
-
*
|
|
999
|
-
*
|
|
921
|
+
* Read a thinking or redacted_thinking block as a reasoning part under
|
|
922
|
+
* `@ai-sdk/anthropic`'s own keys, so a transcript moves between the two
|
|
923
|
+
* transports: a thinking block keeps its text (empty when the display was
|
|
924
|
+
* omitted) and its `signature`, and a redacted one keeps its `data` as
|
|
925
|
+
* `redactedData`. A block missing its signature or data becomes a part with
|
|
926
|
+
* no data, which no adapter sends back. Any other block type is undefined.
|
|
927
|
+
*/
|
|
928
|
+
function read_reasoning_block(block, type) {
|
|
929
|
+
if (type === "thinking") {
|
|
930
|
+
const thinking = Reflect.get(block, "thinking");
|
|
931
|
+
const signature = Reflect.get(block, "signature");
|
|
932
|
+
return {
|
|
933
|
+
type: "reasoning",
|
|
934
|
+
text: typeof thinking === "string" ? thinking : "",
|
|
935
|
+
...typeof signature === "string" ? { provider_options: { anthropic: { signature } } } : {}
|
|
936
|
+
};
|
|
937
|
+
}
|
|
938
|
+
if (type !== "redacted_thinking") return void 0;
|
|
939
|
+
const data = Reflect.get(block, "data");
|
|
940
|
+
return {
|
|
941
|
+
type: "reasoning",
|
|
942
|
+
text: "",
|
|
943
|
+
...typeof data === "string" ? { provider_options: { anthropic: { redactedData: data } } } : {}
|
|
944
|
+
};
|
|
945
|
+
}
|
|
946
|
+
/**
|
|
947
|
+
* Fold one response `content` block into the accumulators. Text blocks append
|
|
948
|
+
* their string; tool_use blocks contribute a call whose id and name are
|
|
949
|
+
* validated by read_tool_use_identity; thinking and redacted_thinking blocks
|
|
950
|
+
* become reasoning parts. Every kept block also lands in `parts`, in block
|
|
951
|
+
* order, for TurnResult.content.
|
|
1000
952
|
*/
|
|
1001
|
-
function push_content_block(block,
|
|
953
|
+
function push_content_block(block, acc) {
|
|
1002
954
|
if (block === null || typeof block !== "object") return;
|
|
1003
955
|
const type = Reflect.get(block, "type");
|
|
1004
956
|
if (type === "text") {
|
|
1005
957
|
const text = Reflect.get(block, "text");
|
|
1006
|
-
if (typeof text === "string")
|
|
958
|
+
if (typeof text === "string") {
|
|
959
|
+
acc.text_parts.push(text);
|
|
960
|
+
acc.parts.push({
|
|
961
|
+
type: "text",
|
|
962
|
+
text
|
|
963
|
+
});
|
|
964
|
+
}
|
|
1007
965
|
return;
|
|
1008
966
|
}
|
|
1009
967
|
if (type === "tool_use") {
|
|
1010
968
|
const { id, name } = read_tool_use_identity(block, "response content");
|
|
1011
|
-
|
|
969
|
+
const call = {
|
|
1012
970
|
id,
|
|
1013
971
|
name,
|
|
1014
972
|
input: Reflect.get(block, "input")
|
|
973
|
+
};
|
|
974
|
+
acc.tool_calls.push(call);
|
|
975
|
+
acc.parts.push({
|
|
976
|
+
type: "tool_call",
|
|
977
|
+
...call
|
|
1015
978
|
});
|
|
979
|
+
return;
|
|
1016
980
|
}
|
|
981
|
+
const reasoning = read_reasoning_block(block, type);
|
|
982
|
+
if (reasoning !== void 0) acc.parts.push(reasoning);
|
|
1017
983
|
}
|
|
1018
984
|
/**
|
|
1019
985
|
* Walk a response payload's `content` array into the flat text and tool_call
|
|
1020
|
-
* lists a TurnResult carries
|
|
1021
|
-
* matching a payload that carried no
|
|
986
|
+
* lists a TurnResult carries, plus every block as a part in order. A
|
|
987
|
+
* non-array `content` yields empty lists, matching a payload that carried no
|
|
988
|
+
* content blocks.
|
|
1022
989
|
*/
|
|
1023
990
|
function collect_content_blocks(content) {
|
|
1024
|
-
const
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
text_parts,
|
|
1029
|
-
tool_calls
|
|
991
|
+
const acc = {
|
|
992
|
+
text_parts: [],
|
|
993
|
+
tool_calls: [],
|
|
994
|
+
parts: []
|
|
1030
995
|
};
|
|
996
|
+
if (Array.isArray(content)) for (const block of content) push_content_block(block, acc);
|
|
997
|
+
return acc;
|
|
1031
998
|
}
|
|
1032
999
|
/**
|
|
1033
1000
|
* Parse a non-stream Messages-API response payload into a TurnResult. The
|
|
1034
1001
|
* stream aggregator rebuilds this same payload shape and feeds it here too,
|
|
1035
|
-
* so both paths share one parser.
|
|
1002
|
+
* so both paths share one parser. A turn with a thinking block gets
|
|
1003
|
+
* `content`, every block in order, and any other turn stays as it was.
|
|
1036
1004
|
*/
|
|
1037
1005
|
function parse_messages_response(payload) {
|
|
1038
1006
|
if (payload === null || typeof payload !== "object") throw new provider_error("anthropic native: response payload is not a JSON object");
|
|
1039
|
-
const { text_parts, tool_calls } = collect_content_blocks(Reflect.get(payload, "content"));
|
|
1040
|
-
|
|
1007
|
+
const { text_parts, tool_calls, parts } = collect_content_blocks(Reflect.get(payload, "content"));
|
|
1008
|
+
const result = {
|
|
1041
1009
|
text: text_parts.join(""),
|
|
1042
1010
|
tool_calls,
|
|
1043
1011
|
finish_reason: map_anthropic_stop_reason(Reflect.get(payload, "stop_reason")),
|
|
1044
1012
|
usage: map_anthropic_usage(Reflect.get(payload, "usage"))
|
|
1045
1013
|
};
|
|
1014
|
+
return parts.some((part) => part.type === "reasoning") ? {
|
|
1015
|
+
...result,
|
|
1016
|
+
content: parts
|
|
1017
|
+
} : result;
|
|
1046
1018
|
}
|
|
1047
1019
|
/**
|
|
1048
1020
|
* Mid-stream `error` events reuse the HTTP status classification:
|
|
@@ -1080,6 +1052,21 @@ function read_block_index(event) {
|
|
|
1080
1052
|
return index;
|
|
1081
1053
|
}
|
|
1082
1054
|
/**
|
|
1055
|
+
* Parse one Messages-stream data line. OpenRouter's Anthropic-compatible
|
|
1056
|
+
* endpoint ends every stream with a chat-completions `[DONE]` line after
|
|
1057
|
+
* message_stop; it carries no event, so it reads as null, which the
|
|
1058
|
+
* aggregator drops like a JSON null. Any other data that isn't JSON is a
|
|
1059
|
+
* broken stream.
|
|
1060
|
+
*/
|
|
1061
|
+
function parse_stream_event(data) {
|
|
1062
|
+
if (data === "[DONE]") return null;
|
|
1063
|
+
try {
|
|
1064
|
+
return JSON.parse(data);
|
|
1065
|
+
} catch {
|
|
1066
|
+
throw new provider_error("anthropic native: stream event is not valid JSON");
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
/**
|
|
1083
1070
|
* Consume parsed Messages-stream events, dispatching StreamChunks as they
|
|
1084
1071
|
* arrive and rebuilding the non-stream response payload as it goes: text
|
|
1085
1072
|
* deltas accumulate into synthetic text blocks, tool_use input arrives as raw
|
|
@@ -1093,6 +1080,7 @@ function read_block_index(event) {
|
|
|
1093
1080
|
function create_stream_aggregator(step_index, dispatch) {
|
|
1094
1081
|
const content = [];
|
|
1095
1082
|
const open_text = /* @__PURE__ */ new Map();
|
|
1083
|
+
const open_thinking = /* @__PURE__ */ new Map();
|
|
1096
1084
|
const open_tools = /* @__PURE__ */ new Map();
|
|
1097
1085
|
const usage = {};
|
|
1098
1086
|
let stop_reason;
|
|
@@ -1101,6 +1089,21 @@ function create_stream_aggregator(step_index, dispatch) {
|
|
|
1101
1089
|
if (raw === null || typeof raw !== "object") return;
|
|
1102
1090
|
for (const [key, value] of Object.entries(raw)) if (typeof value === "number") usage[key] = value;
|
|
1103
1091
|
};
|
|
1092
|
+
const start_reasoning_block = (index, block, type) => {
|
|
1093
|
+
if (type === "thinking") {
|
|
1094
|
+
const initial = Reflect.get(block, "thinking");
|
|
1095
|
+
const synthetic = {
|
|
1096
|
+
type: "thinking",
|
|
1097
|
+
thinking: typeof initial === "string" ? initial : "",
|
|
1098
|
+
signature: Reflect.get(block, "signature")
|
|
1099
|
+
};
|
|
1100
|
+
content.push(synthetic);
|
|
1101
|
+
open_thinking.set(index, synthetic);
|
|
1102
|
+
} else if (type === "redacted_thinking") content.push({
|
|
1103
|
+
type: "redacted_thinking",
|
|
1104
|
+
data: Reflect.get(block, "data")
|
|
1105
|
+
});
|
|
1106
|
+
};
|
|
1104
1107
|
const on_block_start = async (event) => {
|
|
1105
1108
|
const index = read_block_index(event);
|
|
1106
1109
|
const block = Reflect.get(event, "content_block");
|
|
@@ -1135,7 +1138,25 @@ function create_stream_aggregator(step_index, dispatch) {
|
|
|
1135
1138
|
name,
|
|
1136
1139
|
step_index
|
|
1137
1140
|
});
|
|
1141
|
+
return;
|
|
1138
1142
|
}
|
|
1143
|
+
start_reasoning_block(index, block, type);
|
|
1144
|
+
};
|
|
1145
|
+
const on_thinking_delta = async (index, delta, type) => {
|
|
1146
|
+
const open = open_thinking.get(index);
|
|
1147
|
+
if (type === "signature_delta") {
|
|
1148
|
+
const signature = Reflect.get(delta, "signature");
|
|
1149
|
+
if (typeof signature === "string" && open !== void 0) open.signature = signature;
|
|
1150
|
+
return;
|
|
1151
|
+
}
|
|
1152
|
+
const thinking = Reflect.get(delta, "thinking");
|
|
1153
|
+
if (typeof thinking !== "string") return;
|
|
1154
|
+
if (open !== void 0) open.thinking += thinking;
|
|
1155
|
+
await dispatch({
|
|
1156
|
+
kind: "reasoning",
|
|
1157
|
+
text: thinking,
|
|
1158
|
+
step_index
|
|
1159
|
+
});
|
|
1139
1160
|
};
|
|
1140
1161
|
const on_block_delta = async (event) => {
|
|
1141
1162
|
const index = read_block_index(event);
|
|
@@ -1154,13 +1175,8 @@ function create_stream_aggregator(step_index, dispatch) {
|
|
|
1154
1175
|
});
|
|
1155
1176
|
return;
|
|
1156
1177
|
}
|
|
1157
|
-
if (type === "thinking_delta") {
|
|
1158
|
-
|
|
1159
|
-
if (typeof thinking === "string") await dispatch({
|
|
1160
|
-
kind: "reasoning",
|
|
1161
|
-
text: thinking,
|
|
1162
|
-
step_index
|
|
1163
|
-
});
|
|
1178
|
+
if (type === "thinking_delta" || type === "signature_delta") {
|
|
1179
|
+
await on_thinking_delta(index, delta, type);
|
|
1164
1180
|
return;
|
|
1165
1181
|
}
|
|
1166
1182
|
if (type === "input_json_delta") {
|
|
@@ -1180,6 +1196,7 @@ function create_stream_aggregator(step_index, dispatch) {
|
|
|
1180
1196
|
const index = read_block_index(event);
|
|
1181
1197
|
const open = open_tools.get(index);
|
|
1182
1198
|
open_text.delete(index);
|
|
1199
|
+
open_thinking.delete(index);
|
|
1183
1200
|
if (open === void 0) return;
|
|
1184
1201
|
open_tools.delete(index);
|
|
1185
1202
|
if (open.json.length > 0) try {
|
|
@@ -1196,12 +1213,7 @@ function create_stream_aggregator(step_index, dispatch) {
|
|
|
1196
1213
|
};
|
|
1197
1214
|
return {
|
|
1198
1215
|
async handle(data) {
|
|
1199
|
-
|
|
1200
|
-
try {
|
|
1201
|
-
event = JSON.parse(data);
|
|
1202
|
-
} catch {
|
|
1203
|
-
throw new provider_error("anthropic native: stream event is not valid JSON");
|
|
1204
|
-
}
|
|
1216
|
+
const event = parse_stream_event(data);
|
|
1205
1217
|
if (event === null || typeof event !== "object") return;
|
|
1206
1218
|
switch (Reflect.get(event, "type")) {
|
|
1207
1219
|
case "message_start": {
|
|
@@ -1296,27 +1308,6 @@ const create_anthropic_native_adapter = (init) => {
|
|
|
1296
1308
|
//#endregion
|
|
1297
1309
|
//#region src/engine/providers/anthropic.ts
|
|
1298
1310
|
/**
|
|
1299
|
-
* Map an effort level to Anthropic's extended-thinking provider options.
|
|
1300
|
-
*
|
|
1301
|
-
* A zero budget (which is what `none` maps to) sends no thinking config at all
|
|
1302
|
-
* rather than an explicit "disabled" value, so non-reasoning models see a plain
|
|
1303
|
-
* request.
|
|
1304
|
-
*/
|
|
1305
|
-
function translate_anthropic_effort(effort) {
|
|
1306
|
-
const budget = ANTHROPIC_THINKING_BUDGETS[effort];
|
|
1307
|
-
if (budget === 0) return {
|
|
1308
|
-
provider_options: {},
|
|
1309
|
-
effort_ignored: false
|
|
1310
|
-
};
|
|
1311
|
-
return {
|
|
1312
|
-
provider_options: { anthropic: { thinking: {
|
|
1313
|
-
type: "enabled",
|
|
1314
|
-
budgetTokens: budget
|
|
1315
|
-
} } },
|
|
1316
|
-
effort_ignored: false
|
|
1317
|
-
};
|
|
1318
|
-
}
|
|
1319
|
-
/**
|
|
1320
1311
|
* Normalize Anthropic usage via the shared default mapper; the AI SDK already
|
|
1321
1312
|
* reports cache and reasoning tokens in the fields the default reads.
|
|
1322
1313
|
*/
|
|
@@ -1357,7 +1348,7 @@ const create_anthropic_ai_sdk_adapter = (init) => {
|
|
|
1357
1348
|
if (base_url !== void 0) config.baseURL = base_url;
|
|
1358
1349
|
return sdk.createAnthropic(config)(model_id);
|
|
1359
1350
|
},
|
|
1360
|
-
translate_effort:
|
|
1351
|
+
translate_effort: translate_effort_to_sdk_reasoning,
|
|
1361
1352
|
normalize_usage: normalize_anthropic_usage,
|
|
1362
1353
|
supports: (capability) => SUPPORTED$9.has(capability)
|
|
1363
1354
|
};
|
|
@@ -1388,12 +1379,13 @@ const create_anthropic_ai_sdk_adapter = (init) => {
|
|
|
1388
1379
|
* parallel code paths.
|
|
1389
1380
|
*/
|
|
1390
1381
|
/**
|
|
1391
|
-
* The
|
|
1392
|
-
* `
|
|
1393
|
-
*
|
|
1394
|
-
* server
|
|
1382
|
+
* The levels a `clamp_effort` server takes on the flat field: `low | medium |
|
|
1383
|
+
* high` only, so `xhigh` and `max` clamp to `high`. OpenAI's own enum takes
|
|
1384
|
+
* every level, so the openai dialect skips this table, as the nested shape
|
|
1385
|
+
* does. Which levels a model honors is the server's call; the adapter does
|
|
1386
|
+
* not model-sniff.
|
|
1395
1387
|
*/
|
|
1396
|
-
const
|
|
1388
|
+
const CLAMPED_REASONING_EFFORT = {
|
|
1397
1389
|
low: "low",
|
|
1398
1390
|
medium: "medium",
|
|
1399
1391
|
high: "high",
|
|
@@ -1401,6 +1393,21 @@ const OPENAI_COMPATIBLE_REASONING_EFFORT = {
|
|
|
1401
1393
|
max: "high"
|
|
1402
1394
|
};
|
|
1403
1395
|
/**
|
|
1396
|
+
* The field each joinable reasoning_details entry type carries its visible
|
|
1397
|
+
* text in. A Map rather than an object literal, since the lookup key is a
|
|
1398
|
+
* `type` read off the wire.
|
|
1399
|
+
*/
|
|
1400
|
+
const REASONING_TEXT_FIELD = /* @__PURE__ */ new Map([["reasoning.text", "text"], ["reasoning.summary", "summary"]]);
|
|
1401
|
+
/**
|
|
1402
|
+
* Read a reasoning_details entry's visible text: a text entry's `text`, a
|
|
1403
|
+
* summary's `summary`, and '' for any other entry or a missing value.
|
|
1404
|
+
*/
|
|
1405
|
+
function reasoning_detail_text(entry) {
|
|
1406
|
+
const field = REASONING_TEXT_FIELD.get(entry["type"]);
|
|
1407
|
+
const value = field === void 0 ? void 0 : entry[field];
|
|
1408
|
+
return typeof value === "string" ? value : "";
|
|
1409
|
+
}
|
|
1410
|
+
/**
|
|
1404
1411
|
* Map user message content to this wire's shape (a string, or an array of
|
|
1405
1412
|
* text parts). Image parts throw a capability error: they are not mapped on
|
|
1406
1413
|
* the native transport.
|
|
@@ -1419,15 +1426,16 @@ function to_user_content$1(content, provider) {
|
|
|
1419
1426
|
}
|
|
1420
1427
|
/**
|
|
1421
1428
|
* Map assistant message content to Chat Completions shape: text parts join
|
|
1422
|
-
* into `content`,
|
|
1423
|
-
* JSON-stringified arguments
|
|
1429
|
+
* into `content`, tool-call parts become `tool_calls` entries with
|
|
1430
|
+
* JSON-stringified arguments, and the provider's own reasoning parts give
|
|
1431
|
+
* their entries back as `reasoning_details`.
|
|
1424
1432
|
*/
|
|
1425
|
-
function to_assistant_message$1(content) {
|
|
1433
|
+
function to_assistant_message$1(content, provider) {
|
|
1426
1434
|
if (typeof content === "string") return {
|
|
1427
1435
|
role: "assistant",
|
|
1428
1436
|
content
|
|
1429
1437
|
};
|
|
1430
|
-
const { texts, tool_parts } = split_assistant_parts(content);
|
|
1438
|
+
const { texts, tool_parts, reasoning } = split_assistant_parts(content, provider);
|
|
1431
1439
|
const tool_calls = tool_parts.map((part) => ({
|
|
1432
1440
|
id: part.id,
|
|
1433
1441
|
type: "function",
|
|
@@ -1442,9 +1450,20 @@ function to_assistant_message$1(content) {
|
|
|
1442
1450
|
content: text.length > 0 || tool_calls.length === 0 ? text : null
|
|
1443
1451
|
};
|
|
1444
1452
|
if (tool_calls.length > 0) message.tool_calls = tool_calls;
|
|
1453
|
+
const reasoning_details = reasoning.flatMap((part) => own_reasoning_detail(part, provider));
|
|
1454
|
+
if (reasoning_details.length > 0) message.reasoning_details = reasoning_details;
|
|
1445
1455
|
return message;
|
|
1446
1456
|
}
|
|
1447
1457
|
/**
|
|
1458
|
+
* The reasoning_details entry a reasoning part keeps for `provider`, as a
|
|
1459
|
+
* one-element list, or an empty one when the part carries no entry (a part
|
|
1460
|
+
* another transport keyed for this provider, say).
|
|
1461
|
+
*/
|
|
1462
|
+
function own_reasoning_detail(part, provider) {
|
|
1463
|
+
const detail = part.provider_options?.[provider]?.["detail"];
|
|
1464
|
+
return is_record$1(detail) ? [detail] : [];
|
|
1465
|
+
}
|
|
1466
|
+
/**
|
|
1448
1467
|
* Map Fascicle Message[] to Chat Completions shape. Unlike the Messages API
|
|
1449
1468
|
* there is no top-level system field or role-alternation constraint: system
|
|
1450
1469
|
* messages map in place at any position, and tool results are first-class
|
|
@@ -1460,7 +1479,7 @@ function to_chat_messages(messages, provider) {
|
|
|
1460
1479
|
role: "user",
|
|
1461
1480
|
content: to_user_content$1(content, provider)
|
|
1462
1481
|
}),
|
|
1463
|
-
assistant: to_assistant_message$1,
|
|
1482
|
+
assistant: (content) => to_assistant_message$1(content, provider),
|
|
1464
1483
|
tool: (message) => ({
|
|
1465
1484
|
role: "tool",
|
|
1466
1485
|
tool_call_id: message.tool_call_id,
|
|
@@ -1483,10 +1502,19 @@ function to_chat_tools(tools) {
|
|
|
1483
1502
|
}));
|
|
1484
1503
|
}
|
|
1485
1504
|
/**
|
|
1505
|
+
* Write effort into the body in the dialect's shape. `none` writes nothing,
|
|
1506
|
+
* on either shape.
|
|
1507
|
+
*/
|
|
1508
|
+
function apply_effort(body, effort, dialect) {
|
|
1509
|
+
if (effort === "none") return;
|
|
1510
|
+
if (dialect.effort_shape === "nested") body["reasoning"] = { effort };
|
|
1511
|
+
else body["reasoning_effort"] = dialect.clamp_effort === true ? CLAMPED_REASONING_EFFORT[effort] : effort;
|
|
1512
|
+
}
|
|
1513
|
+
/**
|
|
1486
1514
|
* Build the Chat Completions request body for one turn: mapped messages and
|
|
1487
|
-
* tools, the token-limit field named per dialect, sampling params,
|
|
1488
|
-
*
|
|
1489
|
-
*
|
|
1515
|
+
* tools, the token-limit field named per dialect, sampling params, effort in
|
|
1516
|
+
* the dialect's shape, and the dialect's `provider_options.<name>` wire
|
|
1517
|
+
* passthrough merged last.
|
|
1490
1518
|
*/
|
|
1491
1519
|
function build_chat_completions_body(req, dialect) {
|
|
1492
1520
|
const messages = [];
|
|
@@ -1503,7 +1531,7 @@ function build_chat_completions_body(req, dialect) {
|
|
|
1503
1531
|
if (req.max_tokens !== void 0) body[dialect.token_limit_field] = req.max_tokens;
|
|
1504
1532
|
if (req.temperature !== void 0) body["temperature"] = req.temperature;
|
|
1505
1533
|
if (req.top_p !== void 0) body["top_p"] = req.top_p;
|
|
1506
|
-
|
|
1534
|
+
apply_effort(body, req.effort, dialect);
|
|
1507
1535
|
if (req.stream) {
|
|
1508
1536
|
body["stream"] = true;
|
|
1509
1537
|
if (dialect.stream_include_usage) body["stream_options"] = { include_usage: true };
|
|
@@ -1534,12 +1562,31 @@ function read_number(source, key) {
|
|
|
1534
1562
|
return typeof value === "number" ? value : void 0;
|
|
1535
1563
|
}
|
|
1536
1564
|
/**
|
|
1565
|
+
* Copy the cache counts in a usage object's prompt_tokens_details onto
|
|
1566
|
+
* totals, `cached_tokens` as reads and `cache_write_tokens` as writes,
|
|
1567
|
+
* leaving off any count that isn't a number.
|
|
1568
|
+
*/
|
|
1569
|
+
function read_prompt_details(raw, totals) {
|
|
1570
|
+
const details = Reflect.get(raw, "prompt_tokens_details");
|
|
1571
|
+
if (details === null || typeof details !== "object") return;
|
|
1572
|
+
const cached = read_number(details, "cached_tokens");
|
|
1573
|
+
if (cached !== void 0) totals.cached_input_tokens = cached;
|
|
1574
|
+
const cache_write = read_number(details, "cache_write_tokens");
|
|
1575
|
+
if (cache_write !== void 0) totals.cache_write_tokens = cache_write;
|
|
1576
|
+
}
|
|
1577
|
+
/**
|
|
1537
1578
|
* Map the Chat Completions usage object to UsageTotals. prompt_tokens is
|
|
1538
1579
|
* already INCLUSIVE of cached tokens on this API, unlike Anthropic's
|
|
1539
1580
|
* exclusive accounting, so a straight copy is correct; compute_cost
|
|
1540
|
-
* subtracts the cached portion back out.
|
|
1541
|
-
*
|
|
1542
|
-
*
|
|
1581
|
+
* subtracts the cached portion back out. OpenRouter adds
|
|
1582
|
+
* `prompt_tokens_details.cache_write_tokens` for a model with explicit
|
|
1583
|
+
* caching, and prompt_tokens counts those too: a live request to
|
|
1584
|
+
* anthropic/claude-sonnet-5.5 through Google Vertex on 2026-10-19 reported
|
|
1585
|
+
* prompt_tokens 16,358 with 16,299 cache writes, then 16,358 with 16,299
|
|
1586
|
+
* cache reads on the same request repeated, and its charge matched pricing
|
|
1587
|
+
* that subtracts both from prompt_tokens. A missing usage object throws
|
|
1588
|
+
* under a strict dialect (a hosted API omitting usage is a broken response)
|
|
1589
|
+
* and zeroes under a tolerant one.
|
|
1543
1590
|
*/
|
|
1544
1591
|
function map_chat_usage(raw, dialect) {
|
|
1545
1592
|
if (raw === null || typeof raw !== "object") {
|
|
@@ -1553,11 +1600,7 @@ function map_chat_usage(raw, dialect) {
|
|
|
1553
1600
|
input_tokens: read_number(raw, "prompt_tokens") ?? 0,
|
|
1554
1601
|
output_tokens: read_number(raw, "completion_tokens") ?? 0
|
|
1555
1602
|
};
|
|
1556
|
-
|
|
1557
|
-
if (prompt_details !== null && typeof prompt_details === "object") {
|
|
1558
|
-
const cached = read_number(prompt_details, "cached_tokens");
|
|
1559
|
-
if (cached !== void 0) totals.cached_input_tokens = cached;
|
|
1560
|
-
}
|
|
1603
|
+
read_prompt_details(raw, totals);
|
|
1561
1604
|
const completion_details = Reflect.get(raw, "completion_tokens_details");
|
|
1562
1605
|
if (completion_details !== null && typeof completion_details === "object") {
|
|
1563
1606
|
const reasoning = read_number(completion_details, "reasoning_tokens");
|
|
@@ -1566,6 +1609,32 @@ function map_chat_usage(raw, dialect) {
|
|
|
1566
1609
|
return totals;
|
|
1567
1610
|
}
|
|
1568
1611
|
/**
|
|
1612
|
+
* Give a turn the charge a cost-reporting dialect puts on its usage object,
|
|
1613
|
+
* and leave every other turn as it was. `usage.cost` is the request's cost in
|
|
1614
|
+
* US dollars (OpenRouter's credits are dollars). On a bring-your-own-key
|
|
1615
|
+
* request it is only OpenRouter's fee, so the upstream provider's own charge
|
|
1616
|
+
* (`cost_details.upstream_inference_cost`) is added, a null one counting as
|
|
1617
|
+
* nothing. The raw fields ride `provider_reported` under the dialect's name
|
|
1618
|
+
* as sent. A usage object without a numeric `cost` reports nothing, so the
|
|
1619
|
+
* loop falls back to pricing.
|
|
1620
|
+
*/
|
|
1621
|
+
function with_reported_cost(result, raw_usage, dialect) {
|
|
1622
|
+
if (!dialect.reports_cost || !is_record$1(raw_usage)) return result;
|
|
1623
|
+
const cost = raw_usage["cost"];
|
|
1624
|
+
if (typeof cost !== "number") return result;
|
|
1625
|
+
const details = raw_usage["cost_details"];
|
|
1626
|
+
const upstream = is_record$1(details) ? details["upstream_inference_cost"] : void 0;
|
|
1627
|
+
const byok_upstream = raw_usage["is_byok"] === true && typeof upstream === "number" ? upstream : 0;
|
|
1628
|
+
const reported = { cost };
|
|
1629
|
+
if (raw_usage["is_byok"] !== void 0) reported["is_byok"] = raw_usage["is_byok"];
|
|
1630
|
+
if (details !== void 0) reported["cost_details"] = details;
|
|
1631
|
+
return {
|
|
1632
|
+
...result,
|
|
1633
|
+
reported_cost_usd: cost + byok_upstream,
|
|
1634
|
+
provider_reported: { [dialect.name]: reported }
|
|
1635
|
+
};
|
|
1636
|
+
}
|
|
1637
|
+
/**
|
|
1569
1638
|
* Parse one Chat Completions tool_calls entry into the engine's tool-call
|
|
1570
1639
|
* shape, throwing a provider_error if the entry is missing id, function
|
|
1571
1640
|
* name, or valid JSON arguments.
|
|
@@ -1585,8 +1654,8 @@ function parse_tool_call(raw, provider) {
|
|
|
1585
1654
|
}
|
|
1586
1655
|
/**
|
|
1587
1656
|
* The one response parser every path feeds: non-stream parses the payload
|
|
1588
|
-
* directly, and the streaming aggregator (
|
|
1589
|
-
* payload shape from deltas before calling it.
|
|
1657
|
+
* directly, and the streaming aggregator (create_chat_stream_aggregator)
|
|
1658
|
+
* rebuilds this same payload shape from deltas before calling it.
|
|
1590
1659
|
*/
|
|
1591
1660
|
function parse_chat_completion(payload, dialect) {
|
|
1592
1661
|
if (payload === null || typeof payload !== "object") throw new provider_error(`${dialect.name} native: response payload is not a JSON object`);
|
|
@@ -1598,23 +1667,123 @@ function parse_chat_completion(payload, dialect) {
|
|
|
1598
1667
|
const content = Reflect.get(message, "content");
|
|
1599
1668
|
const raw_tool_calls = Reflect.get(message, "tool_calls");
|
|
1600
1669
|
const tool_calls = Array.isArray(raw_tool_calls) ? raw_tool_calls.map((entry) => parse_tool_call(entry, dialect.name)) : [];
|
|
1601
|
-
|
|
1670
|
+
const raw_usage = Reflect.get(payload, "usage");
|
|
1671
|
+
return with_reported_cost(with_reasoning_content({
|
|
1602
1672
|
text: typeof content === "string" ? content : "",
|
|
1603
1673
|
tool_calls,
|
|
1604
1674
|
finish_reason: map_chat_finish_reason(Reflect.get(choice, "finish_reason")),
|
|
1605
|
-
usage: map_chat_usage(
|
|
1675
|
+
usage: map_chat_usage(raw_usage, dialect)
|
|
1676
|
+
}, read_reasoning_parts(message, dialect)), raw_usage, dialect);
|
|
1677
|
+
}
|
|
1678
|
+
/**
|
|
1679
|
+
* Read a response message's reasoning_details into reasoning parts, one per
|
|
1680
|
+
* entry, in order, each keeping its entry under
|
|
1681
|
+
* `provider_options.<name>.detail` for the trip back. Empty unless the
|
|
1682
|
+
* dialect reads reasoning_details.
|
|
1683
|
+
*/
|
|
1684
|
+
function read_reasoning_parts(message, dialect) {
|
|
1685
|
+
if (dialect.reasoning !== "reasoning_details") return [];
|
|
1686
|
+
const entries = Reflect.get(message, "reasoning_details");
|
|
1687
|
+
if (!Array.isArray(entries)) return [];
|
|
1688
|
+
return entries.filter(is_record$1).map((detail) => ({
|
|
1689
|
+
type: "reasoning",
|
|
1690
|
+
text: reasoning_detail_text(detail),
|
|
1691
|
+
provider_options: { [dialect.name]: { detail } }
|
|
1692
|
+
}));
|
|
1693
|
+
}
|
|
1694
|
+
/**
|
|
1695
|
+
* Give a turn its `content` when it carries reasoning, and leave every other
|
|
1696
|
+
* turn exactly as it was. This wire keeps reasoning, text, and tool calls in
|
|
1697
|
+
* separate fields, so the parts go in that order.
|
|
1698
|
+
*/
|
|
1699
|
+
function with_reasoning_content(result, reasoning) {
|
|
1700
|
+
if (reasoning.length === 0) return result;
|
|
1701
|
+
const content = [...reasoning];
|
|
1702
|
+
if (result.text.length > 0) content.push({
|
|
1703
|
+
type: "text",
|
|
1704
|
+
text: result.text
|
|
1705
|
+
});
|
|
1706
|
+
for (const call of result.tool_calls) content.push({
|
|
1707
|
+
type: "tool_call",
|
|
1708
|
+
...call
|
|
1709
|
+
});
|
|
1710
|
+
return {
|
|
1711
|
+
...result,
|
|
1712
|
+
content
|
|
1713
|
+
};
|
|
1714
|
+
}
|
|
1715
|
+
/**
|
|
1716
|
+
* The error a compatible server reports after its stream began under HTTP
|
|
1717
|
+
* 200: a frame whose top-level `error` holds the server's message and, on
|
|
1718
|
+
* OpenRouter, the HTTP status the failure would have had as a numeric
|
|
1719
|
+
* `code`. Carrying that code as `status` lets the engine's retry ladder treat
|
|
1720
|
+
* it as it treats the same error at the start of a response, so a 429 or a
|
|
1721
|
+
* 5xx before any chunk retries, and any error after a chunk fails the turn as
|
|
1722
|
+
* a stream interruption that names the message.
|
|
1723
|
+
*/
|
|
1724
|
+
function stream_error_frame(error, provider) {
|
|
1725
|
+
const message = error["message"];
|
|
1726
|
+
const code = error["code"];
|
|
1727
|
+
const detail = typeof message === "string" && message.length > 0 ? message : "(no message)";
|
|
1728
|
+
const label = code === void 0 ? "" : ` (code ${JSON.stringify(code)})`;
|
|
1729
|
+
return new provider_error(`${provider} native: the server reported an error mid-stream${label}: ${detail}`, typeof code === "number" ? { status: code } : {});
|
|
1730
|
+
}
|
|
1731
|
+
/**
|
|
1732
|
+
* Join streamed reasoning_details fragments into whole entries, the way
|
|
1733
|
+
* OpenRouter's own AI SDK provider does. Consecutive `reasoning.text` or
|
|
1734
|
+
* `reasoning.summary` fragments of one type grow one entry until the type
|
|
1735
|
+
* changes or a signature closes a text entry, and any other entry (encrypted,
|
|
1736
|
+
* a server tool call) stands alone. `index` is ignored, since providers
|
|
1737
|
+
* repeat it. A later fragment fills only the fields its entry still lacks, so
|
|
1738
|
+
* the late signature lands and the first format stays. add() returns the
|
|
1739
|
+
* fragment's visible text, '' when it has none.
|
|
1740
|
+
*/
|
|
1741
|
+
function create_reasoning_joiner() {
|
|
1742
|
+
const entries = [];
|
|
1743
|
+
let open;
|
|
1744
|
+
return {
|
|
1745
|
+
entries,
|
|
1746
|
+
add(fragment) {
|
|
1747
|
+
if (!is_record$1(fragment)) return "";
|
|
1748
|
+
const field = REASONING_TEXT_FIELD.get(fragment["type"]);
|
|
1749
|
+
if (field === void 0) {
|
|
1750
|
+
entries.push(fragment);
|
|
1751
|
+
open = void 0;
|
|
1752
|
+
return "";
|
|
1753
|
+
}
|
|
1754
|
+
const text = reasoning_detail_text(fragment);
|
|
1755
|
+
const joinable = open?.["type"] === fragment["type"] ? open : void 0;
|
|
1756
|
+
let entry;
|
|
1757
|
+
if (joinable === void 0) {
|
|
1758
|
+
entry = { ...fragment };
|
|
1759
|
+
entries.push(entry);
|
|
1760
|
+
} else {
|
|
1761
|
+
entry = joinable;
|
|
1762
|
+
const joined = reasoning_detail_text(entry) + text;
|
|
1763
|
+
for (const [key, value] of Object.entries(fragment)) entry[key] ??= value;
|
|
1764
|
+
entry[field] = joined;
|
|
1765
|
+
}
|
|
1766
|
+
const signature = entry["signature"];
|
|
1767
|
+
open = typeof signature === "string" && signature.length > 0 ? void 0 : entry;
|
|
1768
|
+
return text;
|
|
1769
|
+
}
|
|
1606
1770
|
};
|
|
1607
1771
|
}
|
|
1608
1772
|
/**
|
|
1609
1773
|
* Consume Chat Completions stream frames (the JSON payload of each SSE
|
|
1610
1774
|
* `data:` line, plus the literal `[DONE]` terminator), dispatching
|
|
1611
1775
|
* StreamChunks as they arrive and rebuilding the non-stream payload as it
|
|
1612
|
-
* goes: `delta.content` accumulates into the message text,
|
|
1776
|
+
* goes: `delta.content` accumulates into the message text,
|
|
1613
1777
|
* `delta.tool_calls[]` entries accumulate `function.arguments` string deltas
|
|
1614
|
-
* keyed by their `index` field
|
|
1778
|
+
* keyed by their `index` field, and under a reasoning_details dialect each
|
|
1779
|
+
* `delta.reasoning_details` fragment dispatches a reasoning chunk and joins
|
|
1780
|
+
* into the message's entries. A tool call closes (parsed input dispatched)
|
|
1615
1781
|
* when the choice's finish_reason frame arrives, or at `[DONE]` for servers
|
|
1616
|
-
* that never send one.
|
|
1617
|
-
*
|
|
1782
|
+
* that never send one. A frame with a top-level `error` throws before its
|
|
1783
|
+
* choice is read, on any dialect. Usage rides the final pre-DONE frame
|
|
1784
|
+
* (stream_options.include_usage), and OpenRouter's reported cost rides
|
|
1785
|
+
* inside it, so the streamed turn reads the same charge as the whole
|
|
1786
|
+
* response; a stream that ends without `[DONE]` is
|
|
1618
1787
|
* truncated output, so complete() fails loud instead of returning a partial
|
|
1619
1788
|
* turn. complete() feeds the synthetic payload through parse_chat_completion,
|
|
1620
1789
|
* which is what makes the streamed TurnResult equal the non-streamed one by
|
|
@@ -1624,6 +1793,7 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
|
|
|
1624
1793
|
let text = "";
|
|
1625
1794
|
const tool_calls = [];
|
|
1626
1795
|
const by_index = /* @__PURE__ */ new Map();
|
|
1796
|
+
const reasoning = create_reasoning_joiner();
|
|
1627
1797
|
let finish_reason;
|
|
1628
1798
|
let usage;
|
|
1629
1799
|
let done = false;
|
|
@@ -1685,9 +1855,31 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
|
|
|
1685
1855
|
});
|
|
1686
1856
|
}
|
|
1687
1857
|
};
|
|
1858
|
+
const on_reasoning_delta = async (delta) => {
|
|
1859
|
+
if (dialect.reasoning !== "reasoning_details") return;
|
|
1860
|
+
const fragments = Reflect.get(delta, "reasoning_details");
|
|
1861
|
+
if (Array.isArray(fragments) && fragments.length > 0) {
|
|
1862
|
+
for (const fragment of fragments) {
|
|
1863
|
+
const shown = reasoning.add(fragment);
|
|
1864
|
+
if (shown.length > 0) await dispatch({
|
|
1865
|
+
kind: "reasoning",
|
|
1866
|
+
text: shown,
|
|
1867
|
+
step_index
|
|
1868
|
+
});
|
|
1869
|
+
}
|
|
1870
|
+
return;
|
|
1871
|
+
}
|
|
1872
|
+
const bare = Reflect.get(delta, "reasoning");
|
|
1873
|
+
if (typeof bare === "string" && bare.length > 0) await dispatch({
|
|
1874
|
+
kind: "reasoning",
|
|
1875
|
+
text: bare,
|
|
1876
|
+
step_index
|
|
1877
|
+
});
|
|
1878
|
+
};
|
|
1688
1879
|
const on_choice = async (choice) => {
|
|
1689
1880
|
const delta = Reflect.get(choice, "delta");
|
|
1690
1881
|
if (delta !== null && typeof delta === "object") {
|
|
1882
|
+
await on_reasoning_delta(delta);
|
|
1691
1883
|
const content = Reflect.get(delta, "content");
|
|
1692
1884
|
if (typeof content === "string" && content.length > 0) {
|
|
1693
1885
|
text += content;
|
|
@@ -1727,6 +1919,8 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
|
|
|
1727
1919
|
throw new provider_error(`${dialect.name} native: stream frame is not valid JSON`);
|
|
1728
1920
|
}
|
|
1729
1921
|
if (frame === null || typeof frame !== "object") return;
|
|
1922
|
+
const error = Reflect.get(frame, "error");
|
|
1923
|
+
if (is_record$1(error)) throw stream_error_frame(error, dialect.name);
|
|
1730
1924
|
const raw_usage = Reflect.get(frame, "usage");
|
|
1731
1925
|
if (raw_usage !== null && typeof raw_usage === "object") usage = raw_usage;
|
|
1732
1926
|
const choices = Reflect.get(frame, "choices");
|
|
@@ -1738,7 +1932,8 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
|
|
|
1738
1932
|
const ordered = in_index_order();
|
|
1739
1933
|
const message = {
|
|
1740
1934
|
role: "assistant",
|
|
1741
|
-
content: text.length > 0 || ordered.length === 0 ? text : null
|
|
1935
|
+
content: text.length > 0 || ordered.length === 0 ? text : null,
|
|
1936
|
+
reasoning_details: reasoning.entries
|
|
1742
1937
|
};
|
|
1743
1938
|
if (ordered.length > 0) message["tool_calls"] = ordered.map((call) => ({
|
|
1744
1939
|
id: call.id,
|
|
@@ -1814,26 +2009,6 @@ const create_openai_compatible_adapter = (dialect) => {
|
|
|
1814
2009
|
};
|
|
1815
2010
|
//#endregion
|
|
1816
2011
|
//#region src/engine/providers/openai.ts
|
|
1817
|
-
const OPENAI_REASONING_EFFORT = {
|
|
1818
|
-
low: "low",
|
|
1819
|
-
medium: "medium",
|
|
1820
|
-
high: "high",
|
|
1821
|
-
xhigh: "high",
|
|
1822
|
-
max: "high"
|
|
1823
|
-
};
|
|
1824
|
-
/**
|
|
1825
|
-
* Map an EffortLevel to OpenAI's `reasoningEffort` provider option.
|
|
1826
|
-
*/
|
|
1827
|
-
function translate_openai_effort(effort) {
|
|
1828
|
-
if (effort === "none") return {
|
|
1829
|
-
provider_options: {},
|
|
1830
|
-
effort_ignored: false
|
|
1831
|
-
};
|
|
1832
|
-
return {
|
|
1833
|
-
provider_options: { openai: { reasoningEffort: OPENAI_REASONING_EFFORT[effort] } },
|
|
1834
|
-
effort_ignored: false
|
|
1835
|
-
};
|
|
1836
|
-
}
|
|
1837
2012
|
/**
|
|
1838
2013
|
* Normalize OpenAI's raw usage payload into UsageTotals.
|
|
1839
2014
|
*/
|
|
@@ -1883,6 +2058,7 @@ const create_openai_native_adapter = (init) => {
|
|
|
1883
2058
|
token_limit_field: "max_completion_tokens",
|
|
1884
2059
|
stream_include_usage: true,
|
|
1885
2060
|
tolerant_usage: false,
|
|
2061
|
+
reports_cost: false,
|
|
1886
2062
|
...organization !== void 0 ? { extra_headers: { "OpenAI-Organization": organization } } : {}
|
|
1887
2063
|
};
|
|
1888
2064
|
return create_openai_compatible_adapter(dialect);
|
|
@@ -1906,34 +2082,13 @@ const create_openai_ai_sdk_adapter = (init) => {
|
|
|
1906
2082
|
if (organization !== void 0) config.organization = organization;
|
|
1907
2083
|
return sdk.createOpenAI(config)(model_id);
|
|
1908
2084
|
},
|
|
1909
|
-
translate_effort:
|
|
2085
|
+
translate_effort: translate_effort_to_sdk_reasoning,
|
|
1910
2086
|
normalize_usage: normalize_openai_usage,
|
|
1911
2087
|
supports: (capability) => SUPPORTED$7.has(capability)
|
|
1912
2088
|
};
|
|
1913
2089
|
};
|
|
1914
2090
|
//#endregion
|
|
1915
2091
|
//#region src/engine/providers/google.ts
|
|
1916
|
-
const GOOGLE_THINKING_BUDGET = {
|
|
1917
|
-
low: 1024,
|
|
1918
|
-
medium: 8192,
|
|
1919
|
-
high: 24576,
|
|
1920
|
-
xhigh: 32768,
|
|
1921
|
-
max: 32768
|
|
1922
|
-
};
|
|
1923
|
-
/**
|
|
1924
|
-
* Map an EffortLevel to Google's `thinkingConfig.thinkingBudget` provider
|
|
1925
|
-
* option.
|
|
1926
|
-
*/
|
|
1927
|
-
function translate_google_effort(effort) {
|
|
1928
|
-
if (effort === "none") return {
|
|
1929
|
-
provider_options: {},
|
|
1930
|
-
effort_ignored: false
|
|
1931
|
-
};
|
|
1932
|
-
return {
|
|
1933
|
-
provider_options: { google: { thinkingConfig: { thinkingBudget: GOOGLE_THINKING_BUDGET[effort] } } },
|
|
1934
|
-
effort_ignored: false
|
|
1935
|
-
};
|
|
1936
|
-
}
|
|
1937
2092
|
/**
|
|
1938
2093
|
* Normalize Google's raw usage payload into UsageTotals.
|
|
1939
2094
|
*/
|
|
@@ -1965,7 +2120,7 @@ const create_google_adapter = (init) => {
|
|
|
1965
2120
|
if (base_url !== void 0) config.baseURL = base_url;
|
|
1966
2121
|
return sdk.createGoogleGenerativeAI(config)(model_id);
|
|
1967
2122
|
},
|
|
1968
|
-
translate_effort:
|
|
2123
|
+
translate_effort: translate_effort_to_sdk_reasoning,
|
|
1969
2124
|
normalize_usage: normalize_google_usage,
|
|
1970
2125
|
supports: (capability) => SUPPORTED$6.has(capability)
|
|
1971
2126
|
};
|
|
@@ -1989,14 +2144,15 @@ function to_user_content(content) {
|
|
|
1989
2144
|
/**
|
|
1990
2145
|
* Map assistant message content to this wire's message shape: text parts
|
|
1991
2146
|
* join into `content`, and tool-call parts become `tool_calls` entries with
|
|
1992
|
-
* structured (not JSON-string) arguments.
|
|
2147
|
+
* structured (not JSON-string) arguments. Reasoning parts are never sent:
|
|
2148
|
+
* this adapter maps no `thinking` field in either direction.
|
|
1993
2149
|
*/
|
|
1994
2150
|
function to_assistant_message(content) {
|
|
1995
2151
|
if (typeof content === "string") return {
|
|
1996
2152
|
role: "assistant",
|
|
1997
2153
|
content
|
|
1998
2154
|
};
|
|
1999
|
-
const { texts, tool_parts } = split_assistant_parts(content);
|
|
2155
|
+
const { texts, tool_parts } = split_assistant_parts(content, "ollama");
|
|
2000
2156
|
const message = {
|
|
2001
2157
|
role: "assistant",
|
|
2002
2158
|
content: texts.join("")
|
|
@@ -2173,10 +2329,12 @@ function create_ndjson_decoder() {
|
|
|
2173
2329
|
* each call dispatches tool_call_start then tool_call_end with no input
|
|
2174
2330
|
* deltas between. The `done: true` frame carries done_reason and the eval
|
|
2175
2331
|
* counts and closes the turn (step_finish); frames after it are ignored. A
|
|
2176
|
-
*
|
|
2177
|
-
*
|
|
2178
|
-
*
|
|
2179
|
-
*
|
|
2332
|
+
* frame whose top-level `error` is a string is the daemon failing mid-stream
|
|
2333
|
+
* and throws with its message. A stream that ends without a done frame is
|
|
2334
|
+
* truncated output, so complete() fails loud instead of returning a partial
|
|
2335
|
+
* turn. complete() feeds the synthetic payload through parse_ollama_chat,
|
|
2336
|
+
* which is what makes the streamed TurnResult equal the non-streamed one by
|
|
2337
|
+
* construction.
|
|
2180
2338
|
*/
|
|
2181
2339
|
function create_ollama_stream_aggregator(step_index, dispatch) {
|
|
2182
2340
|
let text = "";
|
|
@@ -2227,6 +2385,8 @@ function create_ollama_stream_aggregator(step_index, dispatch) {
|
|
|
2227
2385
|
throw new provider_error("ollama native: stream frame is not valid JSON");
|
|
2228
2386
|
}
|
|
2229
2387
|
if (frame === null || typeof frame !== "object") return;
|
|
2388
|
+
const error = Reflect.get(frame, "error");
|
|
2389
|
+
if (typeof error === "string") throw new provider_error(`ollama native: the daemon reported an error mid-stream: ${error}`);
|
|
2230
2390
|
const message = Reflect.get(frame, "message");
|
|
2231
2391
|
if (message !== null && message !== void 0 && typeof message === "object") await on_message(message);
|
|
2232
2392
|
if (Reflect.get(frame, "done") === true) {
|
|
@@ -2404,7 +2564,9 @@ const create_lmstudio_native_adapter = (init) => {
|
|
|
2404
2564
|
auth: { kind: "none" },
|
|
2405
2565
|
token_limit_field: "max_tokens",
|
|
2406
2566
|
stream_include_usage: true,
|
|
2407
|
-
tolerant_usage: true
|
|
2567
|
+
tolerant_usage: true,
|
|
2568
|
+
reports_cost: false,
|
|
2569
|
+
clamp_effort: true
|
|
2408
2570
|
});
|
|
2409
2571
|
};
|
|
2410
2572
|
/**
|
|
@@ -2480,8 +2642,10 @@ const create_openrouter_adapter = (init) => {
|
|
|
2480
2642
|
/**
|
|
2481
2643
|
* Build the openrouter dialect and hand it to the shared OpenAI-compatible
|
|
2482
2644
|
* core: Bearer auth, the optional `HTTP-Referer`/`X-Title` attribution
|
|
2483
|
-
* headers, the `max_tokens` token-limit field,
|
|
2484
|
-
*
|
|
2645
|
+
* headers, the `max_tokens` token-limit field, the nested effort shape,
|
|
2646
|
+
* reasoning read from and sent back as `reasoning_details`, strict usage,
|
|
2647
|
+
* and the cost OpenRouter reports on every response. The empty-api_key guard
|
|
2648
|
+
* rides on the core, which throws the same
|
|
2485
2649
|
* `openrouter provider requires a non-empty api_key` engine_config_error.
|
|
2486
2650
|
*/
|
|
2487
2651
|
const create_openrouter_native_adapter = (init) => {
|
|
@@ -2501,6 +2665,9 @@ const create_openrouter_native_adapter = (init) => {
|
|
|
2501
2665
|
token_limit_field: "max_tokens",
|
|
2502
2666
|
stream_include_usage: true,
|
|
2503
2667
|
tolerant_usage: false,
|
|
2668
|
+
reports_cost: true,
|
|
2669
|
+
effort_shape: "nested",
|
|
2670
|
+
reasoning: "reasoning_details",
|
|
2504
2671
|
...Object.keys(extra_headers).length > 0 ? { extra_headers } : {}
|
|
2505
2672
|
};
|
|
2506
2673
|
return create_openai_compatible_adapter(dialect);
|
|
@@ -2535,31 +2702,6 @@ const create_openrouter_ai_sdk_adapter = (init) => {
|
|
|
2535
2702
|
};
|
|
2536
2703
|
//#endregion
|
|
2537
2704
|
//#region src/engine/providers/bedrock.ts
|
|
2538
|
-
const BEDROCK_THINKING_BUDGETS = {
|
|
2539
|
-
none: 0,
|
|
2540
|
-
low: 1024,
|
|
2541
|
-
medium: 5e3,
|
|
2542
|
-
high: 2e4,
|
|
2543
|
-
xhigh: 32e3,
|
|
2544
|
-
max: 64e3
|
|
2545
|
-
};
|
|
2546
|
-
/**
|
|
2547
|
-
* Map an EffortLevel to the Bedrock `reasoningConfig` provider option.
|
|
2548
|
-
*/
|
|
2549
|
-
function translate_bedrock_effort(effort) {
|
|
2550
|
-
const budget = BEDROCK_THINKING_BUDGETS[effort];
|
|
2551
|
-
if (budget === 0) return {
|
|
2552
|
-
provider_options: {},
|
|
2553
|
-
effort_ignored: false
|
|
2554
|
-
};
|
|
2555
|
-
return {
|
|
2556
|
-
provider_options: { bedrock: { reasoningConfig: {
|
|
2557
|
-
type: "enabled",
|
|
2558
|
-
budgetTokens: budget
|
|
2559
|
-
} } },
|
|
2560
|
-
effort_ignored: false
|
|
2561
|
-
};
|
|
2562
|
-
}
|
|
2563
2705
|
/**
|
|
2564
2706
|
* Normalize Bedrock's raw usage payload into UsageTotals.
|
|
2565
2707
|
*/
|
|
@@ -2654,7 +2796,7 @@ const create_bedrock_adapter = (init) => {
|
|
|
2654
2796
|
async build_model(model_id) {
|
|
2655
2797
|
return (await load_optional_peer("@ai-sdk/amazon-bedrock")).createAmazonBedrock(to_sdk_config(statics, await resolve_credential_provider()))(model_id);
|
|
2656
2798
|
},
|
|
2657
|
-
translate_effort:
|
|
2799
|
+
translate_effort: translate_effort_to_sdk_reasoning,
|
|
2658
2800
|
normalize_usage: normalize_bedrock_usage,
|
|
2659
2801
|
supports: (capability) => SUPPORTED$1.has(capability)
|
|
2660
2802
|
};
|
|
@@ -2663,16 +2805,13 @@ const create_bedrock_adapter = (init) => {
|
|
|
2663
2805
|
//#region src/engine/providers/claude_cli/constants.ts
|
|
2664
2806
|
/**
|
|
2665
2807
|
* Frozen constants for the claude_cli provider: startup and idle defaults,
|
|
2666
|
-
* the escalation window,
|
|
2667
|
-
* failure pattern list.
|
|
2808
|
+
* the escalation window, and the auth failure pattern list.
|
|
2668
2809
|
*
|
|
2669
2810
|
* Arrays are Object.freeze-d at module load so downstream code cannot mutate
|
|
2670
2811
|
* them.
|
|
2671
2812
|
*/
|
|
2672
2813
|
const CLI_BINARY_DEFAULT = "claude";
|
|
2673
2814
|
const SIGKILL_ESCALATION_MS = 2e3;
|
|
2674
|
-
const CACHE_READ_MULTIPLIER = .1;
|
|
2675
|
-
const CACHE_WRITE_MULTIPLIER = 1.25;
|
|
2676
2815
|
const CLI_AUTH_ERROR_PATTERNS = Object.freeze([
|
|
2677
2816
|
"authentication",
|
|
2678
2817
|
"unauthorized",
|
|
@@ -3037,6 +3176,7 @@ function create_spawn_runtime() {
|
|
|
3037
3176
|
install_exit_handler_once();
|
|
3038
3177
|
const spawn_cli = async (args) => {
|
|
3039
3178
|
if (args.abort?.aborted === true) throw new aborted_error("aborted", { reason: args.abort.reason });
|
|
3179
|
+
const spawn_failure_reason = args.spawn_failure_reason ?? "binary_not_found";
|
|
3040
3180
|
let child;
|
|
3041
3181
|
try {
|
|
3042
3182
|
const spawn_opts = {
|
|
@@ -3053,7 +3193,7 @@ function create_spawn_runtime() {
|
|
|
3053
3193
|
child = spawn(args.cmd, [...args.argv], spawn_opts);
|
|
3054
3194
|
} catch (err) {
|
|
3055
3195
|
const message = err instanceof Error ? err.message : String(err);
|
|
3056
|
-
throw new claude_cli_error(
|
|
3196
|
+
throw new claude_cli_error(spawn_failure_reason, `failed to spawn '${args.cmd}': ${message}`);
|
|
3057
3197
|
}
|
|
3058
3198
|
live_children.add(child);
|
|
3059
3199
|
let spawn_error;
|
|
@@ -3148,7 +3288,7 @@ function create_spawn_runtime() {
|
|
|
3148
3288
|
const outcome = await close_promise;
|
|
3149
3289
|
if (spawn_error !== void 0) {
|
|
3150
3290
|
const message = spawn_error.message;
|
|
3151
|
-
throw new claude_cli_error(
|
|
3291
|
+
throw new claude_cli_error(spawn_failure_reason, message);
|
|
3152
3292
|
}
|
|
3153
3293
|
if (startup_error !== void 0) throw startup_error;
|
|
3154
3294
|
if (stall_error !== void 0) throw stall_error;
|
|
@@ -3445,18 +3585,26 @@ function create_parser_state() {
|
|
|
3445
3585
|
* `cache_read_input_tokens` maps to `cached_input_tokens` and
|
|
3446
3586
|
* `cache_creation_input_tokens` maps to `cache_write_tokens`; both are
|
|
3447
3587
|
* omitted when the CLI didn't report them.
|
|
3588
|
+
*
|
|
3589
|
+
* The CLI reports `input_tokens` exclusive of its cache reads and writes, as
|
|
3590
|
+
* the Messages API does, while `UsageTotals.input_tokens` is inclusive and
|
|
3591
|
+
* cost decomposition subtracts both back out to find the fresh input. So the
|
|
3592
|
+
* inclusive total is the sum of all three; a straight copy would leave fresh
|
|
3593
|
+
* input with too small a share of a reported cost.
|
|
3448
3594
|
*/
|
|
3449
3595
|
function map_usage(raw) {
|
|
3450
3596
|
if (raw === void 0) return {
|
|
3451
3597
|
input_tokens: 0,
|
|
3452
3598
|
output_tokens: 0
|
|
3453
3599
|
};
|
|
3600
|
+
const cache_read = raw.cache_read_input_tokens;
|
|
3601
|
+
const cache_write = raw.cache_creation_input_tokens;
|
|
3454
3602
|
const out = {
|
|
3455
|
-
input_tokens:
|
|
3456
|
-
output_tokens:
|
|
3603
|
+
input_tokens: (raw.input_tokens ?? 0) + (cache_read ?? 0) + (cache_write ?? 0),
|
|
3604
|
+
output_tokens: raw.output_tokens ?? 0
|
|
3457
3605
|
};
|
|
3458
|
-
if (
|
|
3459
|
-
if (
|
|
3606
|
+
if (cache_read !== void 0) out.cached_input_tokens = cache_read;
|
|
3607
|
+
if (cache_write !== void 0) out.cache_write_tokens = cache_write;
|
|
3460
3608
|
return out;
|
|
3461
3609
|
}
|
|
3462
3610
|
/**
|
|
@@ -3744,47 +3892,15 @@ function snapshot(state) {
|
|
|
3744
3892
|
//#endregion
|
|
3745
3893
|
//#region src/engine/providers/claude_cli/cost.ts
|
|
3746
3894
|
/**
|
|
3747
|
-
*
|
|
3748
|
-
* breakdown, weighted by token counts.
|
|
3895
|
+
* Cost allocation from the CLI's reported `total_cost_usd`.
|
|
3749
3896
|
*
|
|
3750
|
-
*
|
|
3751
|
-
*
|
|
3752
|
-
*
|
|
3753
|
-
*
|
|
3754
|
-
*
|
|
3897
|
+
* The CLI reports a single total_cost_usd and per-turn usage; it does not
|
|
3898
|
+
* break cost down by component or by turn. This module splits the total
|
|
3899
|
+
* across turns proportional to each turn's output tokens, then hands each
|
|
3900
|
+
* turn's share to the engine's `decompose_total_cost` for the component
|
|
3901
|
+
* split. Sum equality is preserved exactly by giving any floating-point
|
|
3902
|
+
* rounding remainder to the last turn.
|
|
3755
3903
|
*/
|
|
3756
|
-
function decompose_total_cost(total_cost_usd, usage) {
|
|
3757
|
-
const input = Math.max(0, usage.input_tokens);
|
|
3758
|
-
const output = Math.max(0, usage.output_tokens);
|
|
3759
|
-
const cached = Math.max(0, usage.cached_input_tokens ?? 0);
|
|
3760
|
-
const cache_write = Math.max(0, usage.cache_write_tokens ?? 0);
|
|
3761
|
-
const input_weight = Math.max(0, input - cached - cache_write);
|
|
3762
|
-
const output_weight = output;
|
|
3763
|
-
const cached_weight = cached * CACHE_READ_MULTIPLIER;
|
|
3764
|
-
const cache_write_weight = cache_write * CACHE_WRITE_MULTIPLIER;
|
|
3765
|
-
const total_weight = input_weight + output_weight + cached_weight + cache_write_weight;
|
|
3766
|
-
if (total_weight === 0 || total_cost_usd === 0) return {
|
|
3767
|
-
total_usd: total_cost_usd,
|
|
3768
|
-
input_usd: 0,
|
|
3769
|
-
output_usd: 0,
|
|
3770
|
-
currency: "USD",
|
|
3771
|
-
is_estimate: true
|
|
3772
|
-
};
|
|
3773
|
-
const input_usd = total_cost_usd * input_weight / total_weight;
|
|
3774
|
-
const output_usd = total_cost_usd * output_weight / total_weight;
|
|
3775
|
-
const cached_usd_raw = total_cost_usd * cached_weight / total_weight;
|
|
3776
|
-
const cache_write_usd_raw = total_cost_usd * cache_write_weight / total_weight;
|
|
3777
|
-
const breakdown = {
|
|
3778
|
-
total_usd: total_cost_usd,
|
|
3779
|
-
input_usd,
|
|
3780
|
-
output_usd,
|
|
3781
|
-
currency: "USD",
|
|
3782
|
-
is_estimate: true
|
|
3783
|
-
};
|
|
3784
|
-
if (cached > 0) breakdown.cached_input_usd = cached_usd_raw;
|
|
3785
|
-
if (cache_write > 0) breakdown.cache_write_usd = cache_write_usd_raw;
|
|
3786
|
-
return breakdown;
|
|
3787
|
-
}
|
|
3788
3904
|
/**
|
|
3789
3905
|
* Split a run-level `total` into a per-turn share for each turn, proportional
|
|
3790
3906
|
* to its output tokens, falling back to an equal split when every turn reports
|
|
@@ -3913,7 +4029,7 @@ function normalize_turns(parsed) {
|
|
|
3913
4029
|
* `duration_ms` is always 0: the CLI's stream-json events don't report
|
|
3914
4030
|
* per-tool-call timing, so there's nothing to measure it against.
|
|
3915
4031
|
*/
|
|
3916
|
-
function tool_call_record(
|
|
4032
|
+
function tool_call_record(call, results, now) {
|
|
3917
4033
|
const rec = {
|
|
3918
4034
|
id: call.id,
|
|
3919
4035
|
name: call.name,
|
|
@@ -3983,7 +4099,7 @@ function build_generate_result(input) {
|
|
|
3983
4099
|
const steps = [];
|
|
3984
4100
|
const tool_calls_accum = [];
|
|
3985
4101
|
turns.forEach((turn, i) => {
|
|
3986
|
-
const step_tool_calls = turn.tool_calls.map((call) => tool_call_record(
|
|
4102
|
+
const step_tool_calls = turn.tool_calls.map((call) => tool_call_record(call, turn.tool_results, now));
|
|
3987
4103
|
for (const tc of step_tool_calls) tool_calls_accum.push(tc);
|
|
3988
4104
|
const record = {
|
|
3989
4105
|
index: turn.step_index,
|
|
@@ -4009,7 +4125,7 @@ function build_generate_result(input) {
|
|
|
4009
4125
|
...parsed.duration_api_ms !== void 0 ? { duration_api_ms: parsed.duration_api_ms } : {}
|
|
4010
4126
|
};
|
|
4011
4127
|
const result = {
|
|
4012
|
-
content: input.
|
|
4128
|
+
content: input.schema === void 0 ? parsed.final_text : input.parsed_content,
|
|
4013
4129
|
tool_calls: tool_calls_accum,
|
|
4014
4130
|
steps,
|
|
4015
4131
|
usage: total_usage,
|
|
@@ -4463,6 +4579,7 @@ const SUPPORTED = /* @__PURE__ */ new Set([
|
|
|
4463
4579
|
"reasoning"
|
|
4464
4580
|
]);
|
|
4465
4581
|
const PROVIDER_NAME = "claude_cli";
|
|
4582
|
+
const BWRAP_EXEC_MISSING = /^bwrap: execvp (.+): No such file or directory$/m;
|
|
4466
4583
|
const CLAUDE_CLI_EFFORT_VALUES = {
|
|
4467
4584
|
low: "low",
|
|
4468
4585
|
medium: "medium",
|
|
@@ -4545,13 +4662,20 @@ function compile_schema(schema) {
|
|
|
4545
4662
|
* Turn a closed CLI subprocess's exit code, signal, and stderr into an
|
|
4546
4663
|
* `Error`.
|
|
4547
4664
|
*
|
|
4548
|
-
* Checks stderr for an auth failure first, then for a
|
|
4549
|
-
*
|
|
4665
|
+
* Checks stderr for an auth failure first, then for a claude binary that
|
|
4666
|
+
* bwrap started but couldn't find inside its sandbox, then for a missing
|
|
4667
|
+
* sandbox binary, then falls back to a generic exit-code error. The stderr
|
|
4550
4668
|
* snippet attached to each error is truncated to 512 bytes.
|
|
4669
|
+
*
|
|
4670
|
+
* The bwrap check has to come before the sandbox check: bwrap's own
|
|
4671
|
+
* "execvp <binary>: No such file or directory" names bwrap and says the file
|
|
4672
|
+
* is missing, so the sandbox check would blame bwrap for the claude binary.
|
|
4551
4673
|
*/
|
|
4552
4674
|
function classify_close_error(code, signal, stderr) {
|
|
4553
4675
|
const snippet = stderr.slice(0, 512);
|
|
4554
4676
|
if (stderr_is_auth_failure(stderr)) return new provider_auth_error(PROVIDER_NAME, `claude CLI reported an auth failure: ${snippet}`, { refresh_command: "claude login" });
|
|
4677
|
+
const sandboxed_binary = BWRAP_EXEC_MISSING.exec(stderr)?.[1];
|
|
4678
|
+
if (sandboxed_binary !== void 0) return new claude_cli_error("binary_not_found", `bwrap ran but found no claude binary at '${sandboxed_binary}' inside its sandbox, which binds only /usr, /bin, /lib, /lib64, and additional_write_paths: ${snippet}`, { stderr_snippet: snippet });
|
|
4555
4679
|
if (/bwrap|greywall|sandbox/i.test(stderr) && /not found|no such file|enoent/i.test(stderr)) return new claude_cli_error("sandbox_unavailable", `sandbox binary unavailable: ${snippet}`, { stderr_snippet: snippet });
|
|
4556
4680
|
const status = typeof code === "number" ? code : void 0;
|
|
4557
4681
|
const signal_part = signal !== null ? ` (signal ${signal})` : "";
|
|
@@ -4573,24 +4697,24 @@ function classify_close_error(code, signal, stderr) {
|
|
|
4573
4697
|
async function run_cli(spawn_runtime, args) {
|
|
4574
4698
|
const argv = build_cli_argv({
|
|
4575
4699
|
model_id: args.model_id,
|
|
4576
|
-
provider_config:
|
|
4700
|
+
provider_config: args.provider_config,
|
|
4577
4701
|
call_opts: args.call_opts,
|
|
4578
4702
|
merged_allowed_tools: args.merged_allowed_tools,
|
|
4579
4703
|
merged_system: args.merged_system,
|
|
4580
4704
|
...args.compiled_schema !== void 0 ? { compiled_schema: args.compiled_schema } : {}
|
|
4581
4705
|
});
|
|
4582
4706
|
const full_argv = [...args.prefix_args, ...argv];
|
|
4583
|
-
const
|
|
4707
|
+
const session = await spawn_runtime.spawn_cli({
|
|
4584
4708
|
cmd: args.spawn_cmd,
|
|
4585
4709
|
argv: full_argv,
|
|
4586
4710
|
env: args.env,
|
|
4587
4711
|
stdin: args.stdin_text,
|
|
4588
4712
|
startup_timeout_ms: args.startup_timeout_ms,
|
|
4589
|
-
stall_timeout_ms: args.stall_timeout_ms
|
|
4590
|
-
|
|
4591
|
-
|
|
4592
|
-
|
|
4593
|
-
|
|
4713
|
+
stall_timeout_ms: args.stall_timeout_ms,
|
|
4714
|
+
...args.cwd !== void 0 ? { cwd: args.cwd } : {},
|
|
4715
|
+
...args.abort !== void 0 ? { abort: args.abort } : {},
|
|
4716
|
+
...args.provider_config.sandbox !== void 0 ? { spawn_failure_reason: "sandbox_unavailable" } : {}
|
|
4717
|
+
});
|
|
4594
4718
|
const state = create_parser_state();
|
|
4595
4719
|
const chunks = [];
|
|
4596
4720
|
const consume = (async () => {
|
|
@@ -4884,6 +5008,7 @@ function create_claude_cli_adapter(init) {
|
|
|
4884
5008
|
const spans = open_spans(trajectory, opts, resolved, tools_list.length > 0, dispatch_chunk !== void 0);
|
|
4885
5009
|
const base_args = {
|
|
4886
5010
|
model_id: resolved.model_id,
|
|
5011
|
+
provider_config: config,
|
|
4887
5012
|
stdin_text: extract_prompt_text(opts.prompt),
|
|
4888
5013
|
merged_system,
|
|
4889
5014
|
merged_allowed_tools,
|
|
@@ -5651,43 +5776,95 @@ function safe_json_stringify(value) {
|
|
|
5651
5776
|
}
|
|
5652
5777
|
}
|
|
5653
5778
|
/**
|
|
5654
|
-
* Build the assistant history message for a turn
|
|
5655
|
-
*
|
|
5779
|
+
* Build the assistant history message for a turn. A turn with `content`
|
|
5780
|
+
* keeps the provider's parts in its order, data included, so reasoning goes
|
|
5781
|
+
* back beside the text and calls it came with. Any other turn is plain text
|
|
5782
|
+
* when there are no tool calls, otherwise text plus tool_call parts.
|
|
5656
5783
|
*/
|
|
5657
|
-
function build_assistant_message(
|
|
5658
|
-
if (
|
|
5784
|
+
function build_assistant_message(turn, view, kept) {
|
|
5785
|
+
if (turn.content !== void 0) return {
|
|
5786
|
+
role: "assistant",
|
|
5787
|
+
content: history_parts(turn.content, view, kept)
|
|
5788
|
+
};
|
|
5789
|
+
if (kept.length === 0) return {
|
|
5659
5790
|
role: "assistant",
|
|
5660
|
-
content:
|
|
5791
|
+
content: view.history_text
|
|
5661
5792
|
};
|
|
5662
5793
|
const parts = [];
|
|
5663
|
-
if (
|
|
5794
|
+
if (view.history_text.length > 0) parts.push({
|
|
5664
5795
|
type: "text",
|
|
5665
|
-
text
|
|
5796
|
+
text: view.history_text
|
|
5666
5797
|
});
|
|
5667
|
-
for (const tc of
|
|
5798
|
+
for (const tc of kept) parts.push(tool_call_part(tc));
|
|
5799
|
+
return {
|
|
5800
|
+
role: "assistant",
|
|
5801
|
+
content: parts
|
|
5802
|
+
};
|
|
5803
|
+
}
|
|
5804
|
+
/**
|
|
5805
|
+
* A raw call as a history part, carrying no provider data.
|
|
5806
|
+
*/
|
|
5807
|
+
function tool_call_part(tc) {
|
|
5808
|
+
return {
|
|
5668
5809
|
type: "tool_call",
|
|
5669
5810
|
id: tc.id,
|
|
5670
5811
|
name: tc.name,
|
|
5671
5812
|
input: tc.input
|
|
5672
|
-
});
|
|
5673
|
-
return {
|
|
5674
|
-
role: "assistant",
|
|
5675
|
-
content: parts
|
|
5676
5813
|
};
|
|
5677
5814
|
}
|
|
5678
5815
|
/**
|
|
5679
|
-
*
|
|
5680
|
-
*
|
|
5681
|
-
* Missing pricing for a paid provider emits `pricing_missing` (deduped per
|
|
5682
|
-
* call) and yields no breakdown; free providers skip the emit entirely.
|
|
5816
|
+
* A turn's own parts for history. A call the per-step clamp dropped leaves,
|
|
5817
|
+
* as it leaves the plain message, while everything around it stays.
|
|
5683
5818
|
*/
|
|
5684
|
-
function
|
|
5819
|
+
function history_parts(content, view, kept) {
|
|
5820
|
+
if (view.outcome !== void 0) return salvaged_parts(content, view.history_text, kept);
|
|
5821
|
+
const kept_ids = new Set(kept.map((tc) => tc.id));
|
|
5822
|
+
return content.filter((part) => part.type !== "tool_call" || kept_ids.has(part.id));
|
|
5823
|
+
}
|
|
5824
|
+
/**
|
|
5825
|
+
* A salvaged turn's parts for history: the stripped text replaces the text
|
|
5826
|
+
* parts at the first one's place, and the salvaged calls follow the last
|
|
5827
|
+
* part. Salvage runs only on a turn with no native calls, so the provider
|
|
5828
|
+
* gave its calls no place of their own. The replacement carries no provider
|
|
5829
|
+
* data, since whatever the provider attached belonged to the text salvage
|
|
5830
|
+
* rewrote.
|
|
5831
|
+
*/
|
|
5832
|
+
function salvaged_parts(content, stripped_text, kept) {
|
|
5833
|
+
const parts = [];
|
|
5834
|
+
let text_placed = false;
|
|
5835
|
+
for (const part of content) if (part.type !== "text") parts.push(part);
|
|
5836
|
+
else if (!text_placed) {
|
|
5837
|
+
text_placed = true;
|
|
5838
|
+
if (stripped_text.length > 0) parts.push({
|
|
5839
|
+
type: "text",
|
|
5840
|
+
text: stripped_text
|
|
5841
|
+
});
|
|
5842
|
+
}
|
|
5843
|
+
for (const tc of kept) parts.push(tool_call_part(tc));
|
|
5844
|
+
return parts;
|
|
5845
|
+
}
|
|
5846
|
+
/**
|
|
5847
|
+
* Derive the step's cost and record it.
|
|
5848
|
+
*
|
|
5849
|
+
* A cost the provider reported wins over any pricing row, since it is what
|
|
5850
|
+
* was charged: it is split by token weight and recorded as
|
|
5851
|
+
* `provider_reported`, and the pricing lookup never runs, so no
|
|
5852
|
+
* `pricing_missing` either. Otherwise the cost comes from the pricing tables,
|
|
5853
|
+
* where missing pricing for a paid provider emits `pricing_missing` (deduped
|
|
5854
|
+
* per call) and yields no breakdown; free providers skip the emit entirely.
|
|
5855
|
+
*/
|
|
5856
|
+
function compute_and_record_cost(config, step_index, turn, step_span) {
|
|
5857
|
+
if (turn.reported_cost_usd !== void 0) {
|
|
5858
|
+
const reported = decompose_total_cost(turn.reported_cost_usd, turn.usage);
|
|
5859
|
+
record_cost(config.trajectory, step_index, reported, "provider_reported", step_span);
|
|
5860
|
+
return reported;
|
|
5861
|
+
}
|
|
5685
5862
|
const pricing = config.resolve_pricing();
|
|
5686
5863
|
if (pricing === void 0 && !FREE_PROVIDERS.has(config.provider)) {
|
|
5687
5864
|
config.pricing_dedup.emit(config.provider, config.model_id);
|
|
5688
5865
|
return;
|
|
5689
5866
|
}
|
|
5690
|
-
const breakdown = compute_cost(usage, pricing, config.provider);
|
|
5867
|
+
const breakdown = compute_cost(turn.usage, pricing, config.provider);
|
|
5691
5868
|
if (breakdown !== void 0) record_cost(config.trajectory, step_index, breakdown, "engine_derived", step_span);
|
|
5692
5869
|
return breakdown;
|
|
5693
5870
|
}
|
|
@@ -6013,7 +6190,7 @@ async function execute_approved_call(ctx, tool, input, raw_call) {
|
|
|
6013
6190
|
err_message = serialize_error(err);
|
|
6014
6191
|
}
|
|
6015
6192
|
const duration_ms = Date.now() - started_at;
|
|
6016
|
-
if (
|
|
6193
|
+
if (err_message !== void 0) {
|
|
6017
6194
|
await record_execute_failure(ctx, tool, raw_call, input, {
|
|
6018
6195
|
thrown,
|
|
6019
6196
|
err_message,
|
|
@@ -6156,7 +6333,7 @@ function push_step_record(config, acc, step_index, turn, tool_calls, finish_reas
|
|
|
6156
6333
|
usage: turn.usage,
|
|
6157
6334
|
finish_reason
|
|
6158
6335
|
};
|
|
6159
|
-
const breakdown = compute_and_record_cost(config, step_index, turn
|
|
6336
|
+
const breakdown = compute_and_record_cost(config, step_index, turn, step_span);
|
|
6160
6337
|
if (breakdown !== void 0) record.cost = breakdown;
|
|
6161
6338
|
if (turn.timing !== void 0) record.timing = turn.timing;
|
|
6162
6339
|
if (turn.provider_reported !== void 0) record.provider_reported = turn.provider_reported;
|
|
@@ -6178,7 +6355,7 @@ async function run_loop_step(config, tool_map, acc, step_index) {
|
|
|
6178
6355
|
const turn = await invoke_turn(config, step_index, step_span);
|
|
6179
6356
|
const view = await salvage_turn(config, turn, tool_map, step_index);
|
|
6180
6357
|
const { kept, dropped } = clamp_calls(config, view.calls, step_index);
|
|
6181
|
-
config.messages.push(build_assistant_message(view
|
|
6358
|
+
config.messages.push(build_assistant_message(turn, view, kept));
|
|
6182
6359
|
if (kept.length === 0) {
|
|
6183
6360
|
push_step_record(config, acc, step_index, turn, [], turn.finish_reason, step_span);
|
|
6184
6361
|
return loop_result(acc, turn.text, turn.finish_reason, false);
|
|
@@ -6643,7 +6820,7 @@ async function build_ai_sdk_transport(cfg) {
|
|
|
6643
6820
|
const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
|
|
6644
6821
|
let ai_sdk_invoke_mod;
|
|
6645
6822
|
try {
|
|
6646
|
-
ai_sdk_invoke_mod = await import("./invoke-
|
|
6823
|
+
ai_sdk_invoke_mod = await import("./invoke-COaeZ-vy.js");
|
|
6647
6824
|
} catch (err) {
|
|
6648
6825
|
throw missing_peer_error("ai", err);
|
|
6649
6826
|
}
|
|
@@ -6659,6 +6836,7 @@ async function build_ai_sdk_transport(cfg) {
|
|
|
6659
6836
|
temperature: opts.temperature,
|
|
6660
6837
|
max_tokens: opts.max_tokens,
|
|
6661
6838
|
top_p: opts.top_p,
|
|
6839
|
+
reasoning: effort_translation.reasoning,
|
|
6662
6840
|
telemetry: cfg.telemetry
|
|
6663
6841
|
}),
|
|
6664
6842
|
retry_policy: cfg.retry_policy,
|
|
@@ -6950,12 +7128,6 @@ function call_timing(started_at) {
|
|
|
6950
7128
|
};
|
|
6951
7129
|
}
|
|
6952
7130
|
/**
|
|
6953
|
-
* Round `v` to 6 decimal places, the resolution used for the USD cost fields.
|
|
6954
|
-
*/
|
|
6955
|
-
function round6(v) {
|
|
6956
|
-
return Math.round(v * 1e6) / 1e6;
|
|
6957
|
-
}
|
|
6958
|
-
/**
|
|
6959
7131
|
* Pick the call-level `provider_reported` from the step records: the payload of
|
|
6960
7132
|
* the last step that reported one.
|
|
6961
7133
|
*
|
|
@@ -7209,4 +7381,4 @@ function create_engine(config) {
|
|
|
7209
7381
|
//#endregion
|
|
7210
7382
|
export { create_engine as t };
|
|
7211
7383
|
|
|
7212
|
-
//# sourceMappingURL=engine-
|
|
7384
|
+
//# sourceMappingURL=engine-CR8q2IqV.js.map
|