fascicle 0.13.1 → 0.15.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.
Files changed (45) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +20 -1
  3. package/dist/acp.d.ts +233 -0
  4. package/dist/acp.js +1919 -0
  5. package/dist/acp.js.map +1 -0
  6. package/dist/adapters.js +1 -2
  7. package/dist/agents.d.ts +1 -1
  8. package/dist/agents.js +1 -1
  9. package/dist/{core-DK7g1Z40.js → core-CDrz0NM1.js} +1 -12
  10. package/dist/core-CDrz0NM1.js.map +1 -0
  11. package/dist/diagram_bin.js +1 -1
  12. package/dist/{engine-BOGuc0X2.js → engine-ha9xmgwE.js} +528 -783
  13. package/dist/engine-ha9xmgwE.js.map +1 -0
  14. package/dist/index.d.ts +468 -622
  15. package/dist/index.js +20 -12
  16. package/dist/index.js.map +1 -1
  17. package/dist/{invoke-Beef3R3X.js → invoke-px31fffH.js} +97 -26
  18. package/dist/invoke-px31fffH.js.map +1 -0
  19. package/dist/json_schema_to_standard-BTWD0J6p.js +270 -0
  20. package/dist/json_schema_to_standard-BTWD0J6p.js.map +1 -0
  21. package/dist/leading_system-DfJLqpGi.js +584 -0
  22. package/dist/leading_system-DfJLqpGi.js.map +1 -0
  23. package/dist/mcp.d.ts +20 -19
  24. package/dist/mcp.js +5 -268
  25. package/dist/mcp.js.map +1 -1
  26. package/dist/records-1HU7mWXf.js +22 -0
  27. package/dist/records-1HU7mWXf.js.map +1 -0
  28. package/dist/search-CA_OqFeW.js +579 -0
  29. package/dist/search-CA_OqFeW.js.map +1 -0
  30. package/dist/search_worker.d.ts +1 -0
  31. package/dist/search_worker.js +19 -0
  32. package/dist/search_worker.js.map +1 -0
  33. package/dist/stdio.js +1 -1
  34. package/dist/testing.d.ts +1 -1
  35. package/dist/testing.js +3 -2
  36. package/dist/testing.js.map +1 -1
  37. package/dist/{types-CZ83Q5nm.d.ts → types-De-TVQxS.d.ts} +49 -4
  38. package/dist/viewer-app/assets/index-Fnh5HKSU.js +2 -0
  39. package/dist/viewer-app/index.html +1 -1
  40. package/dist/viewer.js +1 -1
  41. package/package.json +30 -19
  42. package/dist/core-DK7g1Z40.js.map +0 -1
  43. package/dist/engine-BOGuc0X2.js.map +0 -1
  44. package/dist/invoke-Beef3R3X.js.map +0 -1
  45. package/dist/viewer-app/assets/index-DXsB0byD.js +0 -2
@@ -1,4 +1,6 @@
1
- import { h as wait_with_abort, k as aborted_error, m as compute_backoff, o as format_schema_issues, s as validate_schema } from "./core-DK7g1Z40.js";
1
+ import { h as wait_with_abort, k as aborted_error, m as compute_backoff, o as format_schema_issues, s as validate_schema } from "./core-CDrz0NM1.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-DfJLqpGi.js";
2
4
  import { join } from "node:path";
3
5
  import { mkdtempSync, writeFileSync } from "node:fs";
4
6
  import { tmpdir } from "node:os";
@@ -142,6 +144,8 @@ function strip_meta_keys(json) {
142
144
  //#endregion
143
145
  //#region src/engine/pricing.ts
144
146
  const FREE_PROVIDERS = /* @__PURE__ */ new Set(["ollama", "lmstudio"]);
147
+ const CACHE_READ_MULTIPLIER = .1;
148
+ const CACHE_WRITE_MULTIPLIER = 1.25;
145
149
  const DEFAULT_PRICING = Object.freeze({
146
150
  "anthropic:claude-opus-4-8": {
147
151
  input_per_million: 5,
@@ -286,214 +290,50 @@ function compute_cost(usage, pricing, provider) {
286
290
  if (surface_reasoning_usd !== void 0) breakdown.reasoning_usd = surface_reasoning_usd;
287
291
  return breakdown;
288
292
  }
289
- //#endregion
290
- //#region src/engine/errors.ts
291
- var rate_limit_error = class extends Error {
292
- kind = "rate_limit_error";
293
- retry_after_ms;
294
- attempts;
295
- status;
296
- constructor(message, metadata = {}) {
297
- super(message);
298
- this.name = "rate_limit_error";
299
- this.retry_after_ms = metadata.retry_after_ms;
300
- this.attempts = metadata.attempts ?? 0;
301
- this.status = metadata.status;
302
- }
303
- };
304
- var provider_error = class extends Error {
305
- kind = "provider_error";
306
- status;
307
- body;
308
- cause_kind;
309
- constructor(message, metadata = {}) {
310
- super(message);
311
- this.name = "provider_error";
312
- this.status = metadata.status;
313
- this.body = metadata.body;
314
- this.cause_kind = metadata.cause_kind;
315
- }
316
- };
317
- /**
318
- * Turn-timeout expiry. Thrown by the engine-owned retry_turn wrapper when a
319
- * single invoke_turn exceeds `turn_timeout_ms` before any chunk streamed.
320
- * Its `kind` is the RetryFailureKind wire value `'timeout'` (not the class
321
- * name) on purpose: that is the discriminant classify_retryable /
322
- * classify_provider_error already key on, so the shared classifier treats an
323
- * expiry as retryable without any timeout-specific branch. A mid-stream
324
- * expiry never reaches here; retry_turn converts it to a non-retryable
325
- * stream interruption first.
326
- */
327
- var turn_timeout_error = class extends Error {
328
- kind = "timeout";
329
- timeout_ms;
330
- step_index;
331
- constructor(timeout_ms, step_index) {
332
- super(`turn exceeded turn_timeout_ms budget of ${timeout_ms}ms`);
333
- this.name = "turn_timeout_error";
334
- this.timeout_ms = timeout_ms;
335
- this.step_index = step_index;
336
- }
337
- };
338
- var schema_validation_error = class extends Error {
339
- kind = "schema_validation_error";
340
- schema_issues;
341
- raw_text;
342
- constructor(message, schema_issues, raw_text) {
343
- super(message);
344
- this.name = "schema_validation_error";
345
- this.schema_issues = schema_issues;
346
- this.raw_text = raw_text;
347
- }
348
- };
349
- var tool_error = class extends Error {
350
- kind = "tool_error";
351
- tool_name;
352
- tool_call_id;
353
- cause;
354
- constructor(message, metadata) {
355
- super(message);
356
- this.name = "tool_error";
357
- this.tool_name = metadata.tool_name;
358
- this.tool_call_id = metadata.tool_call_id;
359
- this.cause = metadata.cause;
360
- }
361
- };
362
- var tool_approval_denied_error = class extends Error {
363
- kind = "tool_approval_denied_error";
364
- tool_name;
365
- step_index;
366
- tool_call_id;
367
- constructor(message, metadata) {
368
- super(message);
369
- this.name = "tool_approval_denied_error";
370
- this.tool_name = metadata.tool_name;
371
- this.step_index = metadata.step_index;
372
- this.tool_call_id = metadata.tool_call_id;
373
- }
374
- };
375
- var model_required_error = class extends Error {
376
- kind = "model_required_error";
377
- constructor(message = "no model specified: pass `model` to generate() or set `defaults.model` on the engine") {
378
- super(message);
379
- this.name = "model_required_error";
380
- }
381
- };
382
- /**
383
- * A generate call named a provider the engine has no adapter for. Carries the
384
- * configured names, and the message repeats them, so a typo is visible from
385
- * the message alone rather than needing the engine config alongside it.
386
- */
387
- var provider_not_configured_error = class extends Error {
388
- kind = "provider_not_configured_error";
389
- provider;
390
- configured;
391
- constructor(provider, configured = []) {
392
- const state = configured.length > 0 ? configured.join(", ") : "none";
393
- super(`provider '${provider}' is not configured on this engine: pass it to create_engine({ providers }) (configured: ${state})`);
394
- this.name = "provider_not_configured_error";
395
- this.provider = provider;
396
- this.configured = configured;
397
- }
398
- };
399
- /**
400
- * No provider could be resolved for a generate call: several providers are
401
- * configured, the call named none, and no `defaults.provider` is set. Carries
402
- * the configured provider names so the message tells the caller exactly what
403
- * the engine can route to.
404
- */
405
- var provider_required_error = class extends Error {
406
- kind = "provider_required_error";
407
- configured;
408
- constructor(configured) {
409
- super(`no provider specified: pass \`provider\` to generate() or set \`defaults.provider\` (configured: ${configured.join(", ")})`);
410
- this.name = "provider_required_error";
411
- this.configured = configured;
412
- }
413
- };
414
- var engine_config_error = class extends Error {
415
- kind = "engine_config_error";
416
- provider;
417
- constructor(message, provider) {
418
- super(message);
419
- this.name = "engine_config_error";
420
- this.provider = provider;
421
- }
422
- };
423
- var on_chunk_error = class extends Error {
424
- kind = "on_chunk_error";
425
- cause;
426
- constructor(message, cause) {
427
- super(message);
428
- this.name = "on_chunk_error";
429
- this.cause = cause;
430
- }
431
- };
432
- var provider_capability_error = class extends Error {
433
- kind = "provider_capability_error";
434
- provider;
435
- capability;
436
- constructor(provider, capability, detail) {
437
- const suffix = detail !== void 0 ? `: ${detail}` : "";
438
- super(`provider '${provider}' does not support '${capability}'${suffix}`);
439
- this.name = "provider_capability_error";
440
- this.provider = provider;
441
- this.capability = capability;
442
- }
443
- };
444
- var engine_disposed_error = class extends Error {
445
- kind = "engine_disposed_error";
446
- constructor(message = "engine has been disposed: build a new one with create_engine()") {
447
- super(message);
448
- this.name = "engine_disposed_error";
449
- }
450
- };
451
- var claude_cli_error = class extends Error {
452
- kind = "claude_cli_error";
453
- reason;
454
- status;
455
- stderr_snippet;
456
- constructor(reason, message, metadata = {}) {
457
- super(message);
458
- this.name = "claude_cli_error";
459
- this.reason = reason;
460
- this.status = metadata.status;
461
- this.stderr_snippet = metadata.stderr_snippet;
462
- }
463
- };
464
- var provider_auth_error = class extends Error {
465
- kind = "provider_auth_error";
466
- provider;
467
- refresh_command;
468
- constructor(provider, message, metadata = {}) {
469
- super(message);
470
- this.name = "provider_auth_error";
471
- this.provider = provider;
472
- this.refresh_command = metadata.refresh_command;
473
- }
474
- };
475
293
  /**
476
- * A generate call that set a schema finished for a reason other than 'stop'
477
- * (content_filter, length, max_steps, tool_calls), so no schema-validated
478
- * value exists and validation was never attempted. Carries the raw model text
479
- * and the last provider-reported payload, so a guardrail trace or other
480
- * block detail stays reachable from the catch. Distinct from
481
- * schema_validation_error, which covers a response that completed normally
482
- * and then failed validation after its repair attempts ran out.
483
- */
484
- var incomplete_generation_error = class extends Error {
485
- kind = "incomplete_generation_error";
486
- finish_reason;
487
- raw_text;
488
- provider_reported;
489
- constructor(finish_reason, raw_text, provider_reported) {
490
- super(`generation finished with '${finish_reason}' before a schema-validated response was produced`);
491
- this.name = "incomplete_generation_error";
492
- this.finish_reason = finish_reason;
493
- this.raw_text = raw_text;
494
- this.provider_reported = provider_reported;
495
- }
496
- };
294
+ * Split a reported total cost into an input/output cost breakdown, weighted
295
+ * by token counts.
296
+ *
297
+ * Cache-read and cache-write tokens are cheaper and pricier respectively
298
+ * than base input tokens, so they're weighted by `CACHE_READ_MULTIPLIER`
299
+ * and `CACHE_WRITE_MULTIPLIER` before the total is distributed
300
+ * proportionally across all weighted components. The total is what the
301
+ * provider charged and the split is estimated, so the breakdown keeps
302
+ * `is_estimate: true`. Returns an all-zero estimate when there's no usage or
303
+ * no cost to distribute.
304
+ */
305
+ function decompose_total_cost(total_cost_usd, usage) {
306
+ const input = Math.max(0, usage.input_tokens);
307
+ const output = Math.max(0, usage.output_tokens);
308
+ const cached = Math.max(0, usage.cached_input_tokens ?? 0);
309
+ const cache_write = Math.max(0, usage.cache_write_tokens ?? 0);
310
+ const input_weight = Math.max(0, input - cached - cache_write);
311
+ const output_weight = output;
312
+ const cached_weight = cached * CACHE_READ_MULTIPLIER;
313
+ const cache_write_weight = cache_write * CACHE_WRITE_MULTIPLIER;
314
+ const total_weight = input_weight + output_weight + cached_weight + cache_write_weight;
315
+ if (total_weight === 0 || total_cost_usd === 0) return {
316
+ total_usd: total_cost_usd,
317
+ input_usd: 0,
318
+ output_usd: 0,
319
+ currency: "USD",
320
+ is_estimate: true
321
+ };
322
+ const input_usd = total_cost_usd * input_weight / total_weight;
323
+ const output_usd = total_cost_usd * output_weight / total_weight;
324
+ const cached_usd_raw = total_cost_usd * cached_weight / total_weight;
325
+ const cache_write_usd_raw = total_cost_usd * cache_write_weight / total_weight;
326
+ const breakdown = {
327
+ total_usd: total_cost_usd,
328
+ input_usd,
329
+ output_usd,
330
+ currency: "USD",
331
+ is_estimate: true
332
+ };
333
+ if (cached > 0) breakdown.cached_input_usd = cached_usd_raw;
334
+ if (cache_write > 0) breakdown.cache_write_usd = cache_write_usd_raw;
335
+ return breakdown;
336
+ }
497
337
  //#endregion
498
338
  //#region src/engine/retry.ts
499
339
  const DEFAULT_RETRY = {
@@ -716,303 +556,6 @@ function classify_retryable(err) {
716
556
  if (kind === "network" || kind === "timeout") return build_retryable_signal(err, kind);
717
557
  }
718
558
  //#endregion
719
- //#region src/engine/providers/types.ts
720
- /**
721
- * Read the `transport` selector off a provider init. Defaults to 'ai_sdk';
722
- * anything but the two known backends throws at construction so a typo
723
- * fails loud instead of silently running the default.
724
- */
725
- function resolve_transport(init, provider) {
726
- const raw = init["transport"];
727
- if (raw === void 0 || raw === "ai_sdk") return "ai_sdk";
728
- if (raw === "native") return "native";
729
- throw new engine_config_error(`${provider} provider: transport must be 'ai_sdk' or 'native', got ${JSON.stringify(raw)}`, provider);
730
- }
731
- /**
732
- * Map raw provider usage to UsageTotals, the mapper most adapters share.
733
- *
734
- * Missing usage zeroes the totals. Optional fields (reasoning and cache
735
- * tokens) are set only when the raw payload carries them, reading the
736
- * flattened field first and the AI SDK's *_details container second.
737
- */
738
- function default_normalize_usage(raw) {
739
- if (raw === void 0) return {
740
- input_tokens: 0,
741
- output_tokens: 0
742
- };
743
- const totals = {
744
- input_tokens: typeof raw.input_tokens === "number" ? raw.input_tokens : 0,
745
- output_tokens: typeof raw.output_tokens === "number" ? raw.output_tokens : 0
746
- };
747
- if (typeof raw.reasoning_tokens === "number") totals.reasoning_tokens = raw.reasoning_tokens;
748
- else if (typeof raw.output_token_details?.reasoning_tokens === "number") totals.reasoning_tokens = raw.output_token_details.reasoning_tokens;
749
- if (typeof raw.cached_input_tokens === "number") totals.cached_input_tokens = raw.cached_input_tokens;
750
- else if (typeof raw.input_token_details?.cached_tokens === "number") totals.cached_input_tokens = raw.input_token_details.cached_tokens;
751
- if (typeof raw.cache_write_tokens === "number") totals.cache_write_tokens = raw.cache_write_tokens;
752
- else if (typeof raw.input_token_details?.cache_creation_input_tokens === "number") totals.cache_write_tokens = raw.input_token_details.cache_creation_input_tokens;
753
- return totals;
754
- }
755
- /**
756
- * Usage mapper for local backends (Ollama, LM Studio) whose servers never
757
- * report cache or reasoning tokens: default-normalize, then strip those
758
- * fields so a stray *_details container cannot invent them.
759
- */
760
- function normalize_local_usage(raw) {
761
- const base = default_normalize_usage(raw);
762
- delete base.cached_input_tokens;
763
- delete base.cache_write_tokens;
764
- delete base.reasoning_tokens;
765
- return base;
766
- }
767
- /**
768
- * Build the standard missing-peer error: names the package and the install
769
- * command so a missing peer is diagnosable from the message alone. Shared so
770
- * every optional-peer failure reads identically, whether the failing
771
- * `import()` names the peer directly (load_optional_peer below) or a local
772
- * module that reaches the peer through its own static import (the ai_sdk
773
- * turn seam in generate.ts, whose dynamic import must stay written in
774
- * generate.ts for its relative specifier to resolve correctly).
775
- */
776
- function missing_peer_error(peer_name, cause) {
777
- const message = `missing peer dependency '${peer_name}'. Install it with your package manager, e.g., \`pnpm add ${peer_name}\` or \`npm install ${peer_name}\`. Cause: ${cause instanceof Error ? cause.message : JSON.stringify(cause)}`;
778
- return new Error(message, { cause });
779
- }
780
- /**
781
- * Dynamically import an optional peer dependency, rethrowing an import
782
- * failure via missing_peer_error so a missing peer is diagnosable from the
783
- * message alone.
784
- */
785
- async function load_optional_peer(specifier) {
786
- let mod;
787
- try {
788
- mod = await import(specifier);
789
- } catch (err) {
790
- throw missing_peer_error(specifier, err);
791
- }
792
- return mod;
793
- }
794
- //#endregion
795
- //#region src/engine/providers/native_shared.ts
796
- /**
797
- * The message under a JSON error envelope's `error` key, or undefined when
798
- * the shape carries none worth surfacing.
799
- */
800
- function error_field_message(parsed) {
801
- if (parsed === null || typeof parsed !== "object") return void 0;
802
- const error = Reflect.get(parsed, "error");
803
- if (typeof error === "string" && error.length > 0) return error;
804
- if (error === null || typeof error !== "object") return void 0;
805
- const message = Reflect.get(error, "message");
806
- return typeof message === "string" && message.length > 0 ? message : void 0;
807
- }
808
- /**
809
- * Pull a human-readable message out of a non-2xx body. The native wires share
810
- * one envelope family: Anthropic sends `{ type: 'error', error: { message } }`,
811
- * OpenAI-style wires send `{ error: { message } }`, and Ollama sends
812
- * `{ error: "message" }` (with the nested shape appearing behind proxies), so
813
- * one parser accepts a string or a `{ message }` object under `error` and
814
- * falls back to the raw body, truncated to 300 chars.
815
- */
816
- function extract_error_message(body) {
817
- if (body.length === 0) return "(empty body)";
818
- let message;
819
- try {
820
- message = error_field_message(JSON.parse(body));
821
- } catch {}
822
- if (message !== void 0) return message;
823
- return body.length > 300 ? `${body.slice(0, 300)}...` : body;
824
- }
825
- /**
826
- * Map a non-2xx response to what the engine's retry stack expects: 401
827
- * becomes a typed provider_auth_error, 429/5xx keep `status` +
828
- * `responseHeaders` so classify_provider_error marks them retryable
829
- * (rate_limit honoring retry-after, provider_5xx), and any other 4xx is a
830
- * permanent provider_error that surfaces as-is.
831
- */
832
- async function response_error(response, provider) {
833
- let body = "";
834
- try {
835
- body = await response.text();
836
- } catch {}
837
- const detail = extract_error_message(body);
838
- const status = response.status;
839
- if (status === 401) return new provider_auth_error(provider, `${provider} authentication failed (401): ${detail}`);
840
- if (status === 429 || status >= 500) {
841
- const retry_after = response.headers.get("retry-after");
842
- return Object.assign(/* @__PURE__ */ new Error(`${provider} API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
843
- }
844
- return new provider_error(`${provider} API error ${status}: ${detail}`, {
845
- status,
846
- body
847
- });
848
- }
849
- /**
850
- * A user abort surfaces as the fetch/reader AbortError; retry_turn converts
851
- * it via its own signal check, so it is rethrown untouched. Everything else
852
- * is a transport failure wrapped in the `kind: 'network'` shape the shared
853
- * classify_provider_error marks retryable.
854
- */
855
- function rethrow_network_failure(err, abort, provider) {
856
- if (abort.aborted) throw err;
857
- const detail = err instanceof Error ? err.message : String(err);
858
- throw Object.assign(/* @__PURE__ */ new Error(`${provider} native: network failure: ${detail}`), { kind: "network" });
859
- }
860
- /**
861
- * Run one native turn over HTTP: POST the dialect's body, map a non-2xx to
862
- * the retry stack's shapes, and hand the response to the dialect's stream or
863
- * JSON consumer. `build_body` is called inside the network try/catch on
864
- * purpose, so a mapper that throws surfaces as a retryable network failure
865
- * rather than escaping the turn unclassified.
866
- */
867
- async function invoke_http_turn(req, provider, request, consume) {
868
- let response;
869
- try {
870
- response = await fetch(request.url, {
871
- method: "POST",
872
- headers: request.headers,
873
- body: JSON.stringify(request.build_body()),
874
- signal: req.abort
875
- });
876
- } catch (err) {
877
- rethrow_network_failure(err, req.abort, provider);
878
- }
879
- if (!response.ok) throw await response_error(response, provider);
880
- if (req.stream) return consume.stream(response);
881
- const payload = await response.json();
882
- return consume.parse(payload);
883
- }
884
- /**
885
- * Drain a streaming response: decode bytes, reassemble framed payloads (SSE
886
- * events or NDJSON lines), and feed them to the dialect's aggregator, which
887
- * dispatches chunks through req.dispatch_chunk as they arrive. Transport
888
- * failures mid-read wrap as network errors; aggregator throws (malformed
889
- * frames, mid-stream error events, a rejecting on_chunk) pass through
890
- * untouched, with the reader cancelled so the connection is released.
891
- */
892
- async function consume_framed_stream(response, req, provider, framing, create_aggregator) {
893
- const body = response.body;
894
- if (body === null) throw new provider_error(`${provider} native: streaming response has no body`);
895
- const aggregator = create_aggregator(req.dispatch_chunk ?? (async () => {}));
896
- const reader = body.getReader();
897
- const text_decoder = new TextDecoder();
898
- const next_bytes = async () => {
899
- let step;
900
- try {
901
- step = await reader.read();
902
- } catch (err) {
903
- rethrow_network_failure(err, req.abort, provider);
904
- }
905
- return step.done ? void 0 : step.value;
906
- };
907
- try {
908
- while (true) {
909
- const bytes = await next_bytes();
910
- if (bytes === void 0) break;
911
- for (const data of framing.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle(data);
912
- }
913
- } finally {
914
- reader.cancel().catch(() => {});
915
- }
916
- const tail = [...framing.push(text_decoder.decode()), ...framing.flush()];
917
- for (const data of tail) await aggregator.handle(data);
918
- return aggregator.complete();
919
- }
920
- /**
921
- * Incremental newline framing shared by the SSE and NDJSON decoders: buffer
922
- * decoded text across arbitrary chunk boundaries, hand each completed line
923
- * (CR trimmed) to `handle_line`, and on flush hand over an unterminated tail
924
- * line, then `on_flush` for state a decoder holds across lines (an SSE event
925
- * never closed by a blank line).
926
- */
927
- function create_line_splitter(handle_line, on_flush) {
928
- let buffer = "";
929
- const take_line = (raw, out) => {
930
- handle_line(raw.endsWith("\r") ? raw.slice(0, -1) : raw, out);
931
- };
932
- return {
933
- push(text) {
934
- buffer += text;
935
- const out = [];
936
- let newline = buffer.indexOf("\n");
937
- while (newline >= 0) {
938
- take_line(buffer.slice(0, newline), out);
939
- buffer = buffer.slice(newline + 1);
940
- newline = buffer.indexOf("\n");
941
- }
942
- return out;
943
- },
944
- flush() {
945
- const out = [];
946
- if (buffer.length > 0) take_line(buffer, out);
947
- buffer = "";
948
- on_flush?.(out);
949
- return out;
950
- }
951
- };
952
- }
953
- /**
954
- * Decode a tool call's wire `arguments` string into the engine's input value:
955
- * non-empty JSON parses (bad JSON is a provider_error naming the tool), and
956
- * an absent or empty value means `{}`, the shape some servers send for
957
- * no-argument calls. The Ollama wire's structured-object arguments are its
958
- * caller's business before this runs.
959
- */
960
- function parse_tool_call_arguments(raw_args, provider, tool_name) {
961
- if (typeof raw_args === "string" && raw_args.length > 0) try {
962
- return JSON.parse(raw_args);
963
- } catch {
964
- throw new provider_error(`${provider} native: tool_calls arguments for ${tool_name} is not valid JSON`);
965
- }
966
- return {};
967
- }
968
- /**
969
- * Fold engine messages into a wire's message array by role. Serves the wires
970
- * whose messages map 1:1 in place (chat/completions, Ollama's /api/chat); the
971
- * Messages API's system-hoisting and role-merging rules live in its own
972
- * mapper instead.
973
- */
974
- function map_messages_by_role(messages, wire) {
975
- const out = [];
976
- for (const message of messages) switch (message.role) {
977
- case "system":
978
- out.push(wire.system(message.content));
979
- break;
980
- case "user":
981
- out.push(wire.user(message.content));
982
- break;
983
- case "assistant":
984
- out.push(wire.assistant(message.content));
985
- break;
986
- case "tool": out.push(wire.tool(message));
987
- }
988
- return out;
989
- }
990
- /**
991
- * Partition assistant content parts into the text runs and tool calls the
992
- * wire mappers serialize separately.
993
- */
994
- function split_assistant_parts(parts) {
995
- const texts = [];
996
- const tool_parts = [];
997
- for (const part of parts) if (part.type === "text") texts.push(part.text);
998
- else tool_parts.push(part);
999
- return {
1000
- texts,
1001
- tool_parts
1002
- };
1003
- }
1004
- /**
1005
- * Read the api_key and base_url every OpenAI-compatible dialect starts from:
1006
- * a missing or non-string api_key becomes '' (the core's empty-key guard owns
1007
- * the throw), and an absent base_url falls back to the dialect's default.
1008
- */
1009
- function read_dialect_init(init, default_base_url) {
1010
- return {
1011
- api_key: typeof init.api_key === "string" ? init.api_key : "",
1012
- base_url: typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : default_base_url
1013
- };
1014
- }
1015
- //#endregion
1016
559
  //#region src/engine/providers/sse_native.ts
1017
560
  /**
1018
561
  * Incremental SSE decoder shared by the native (raw-HTTP) provider adapters.
@@ -1070,10 +613,10 @@ function create_sse_decoder() {
1070
613
  * construction rather than by parallel code paths.
1071
614
  */
1072
615
  /**
1073
- * Effort level to extended-thinking budget tokens. Lives here (not in
1074
- * anthropic.ts) because both transports map effort from this table and the
1075
- * ai_sdk factory already imports this module for transport dispatch; the
1076
- * reverse import would be a cycle.
616
+ * Effort level to the room native adds to its max_tokens default for
617
+ * adaptive thinking. No transport sends these as a budget any more: native
618
+ * sends `output_config.effort`, and the ai_sdk transport hands the level to
619
+ * the SDK's `reasoning` option.
1077
620
  */
1078
621
  const ANTHROPIC_THINKING_BUDGETS = {
1079
622
  none: 0,
@@ -1091,9 +634,9 @@ const ANTHROPIC_VERSION = "2023-06-01";
1091
634
  */
1092
635
  const DEFAULT_BASE_URL$2 = "https://api.anthropic.com/v1";
1093
636
  /**
1094
- * The Messages API requires max_tokens on every request. When extended
1095
- * thinking is enabled the budget counts against max_tokens, so the default
1096
- * rides on top of the thinking budget rather than being swallowed by it.
637
+ * The Messages API requires max_tokens on every request. Thinking spends
638
+ * from max_tokens, so with effort set the default rides on top of that
639
+ * effort's budget-table value rather than being swallowed by it.
1097
640
  */
1098
641
  const DEFAULT_MAX_TOKENS = 4096;
1099
642
  /**
@@ -1121,19 +664,44 @@ function to_user_blocks(content) {
1121
664
  return blocks;
1122
665
  }
1123
666
  /**
1124
- * Map assistant message content to Messages-API blocks: text parts become
1125
- * text blocks (empties dropped), tool-call parts become tool_use blocks.
667
+ * Map a reasoning part back to the block it came from: a `signature` under
668
+ * `anthropic` is a thinking block, its text as it was (empty included), and
669
+ * `redactedData` is a redacted_thinking block. A part with neither (keyed for
670
+ * another provider, or carrying no data) maps to no block, since Anthropic
671
+ * refuses thinking it didn't sign.
672
+ */
673
+ function reasoning_blocks(part) {
674
+ const own = part.provider_options?.["anthropic"];
675
+ const signature = own?.["signature"];
676
+ if (typeof signature === "string") return [{
677
+ type: "thinking",
678
+ thinking: part.text,
679
+ signature
680
+ }];
681
+ const data = own?.["redactedData"];
682
+ return typeof data === "string" ? [{
683
+ type: "redacted_thinking",
684
+ data
685
+ }] : [];
686
+ }
687
+ /**
688
+ * Map assistant message content to Messages-API blocks in part order: text
689
+ * parts become text blocks (empties dropped), tool-call parts become tool_use
690
+ * blocks, and Anthropic's own reasoning parts become the thinking and
691
+ * redacted_thinking blocks they came from. Order is kept because Anthropic
692
+ * refuses a tool-use turn whose thinking blocks moved.
1126
693
  */
1127
694
  function to_assistant_blocks(content) {
1128
695
  if (typeof content === "string") return text_blocks(content);
1129
696
  const blocks = [];
1130
697
  for (const part of content) if (part.type === "text") blocks.push(...text_blocks(part.text));
1131
- else blocks.push({
698
+ else if (part.type === "tool_call") blocks.push({
1132
699
  type: "tool_use",
1133
700
  id: part.id,
1134
701
  name: part.name,
1135
702
  input: part.input
1136
703
  });
704
+ else blocks.push(...reasoning_blocks(part));
1137
705
  return blocks;
1138
706
  }
1139
707
  /**
@@ -1193,29 +761,30 @@ function to_anthropic_tools(tools) {
1193
761
  }
1194
762
  /**
1195
763
  * Build the Messages-API request body for one turn: mapped messages and
1196
- * tools, the effort-derived thinking block, the required max_tokens default,
1197
- * and the `provider_options.anthropic` wire passthrough merged last.
764
+ * tools, effort as adaptive thinking plus `output_config.effort`, the
765
+ * required max_tokens default, and the `provider_options.anthropic` wire
766
+ * passthrough merged last.
1198
767
  */
1199
768
  function build_messages_body(req) {
1200
769
  const { system: hoisted_system, messages } = to_anthropic_messages(req.messages);
1201
770
  const system_parts = [req.system, hoisted_system].filter((s) => typeof s === "string" && s.length > 0);
1202
- const budget = ANTHROPIC_THINKING_BUDGETS[req.effort];
1203
- const thinking_enabled = budget > 0;
1204
771
  const body = {
1205
772
  model: req.model_id,
1206
773
  messages,
1207
- max_tokens: req.max_tokens ?? (thinking_enabled ? budget + DEFAULT_MAX_TOKENS : DEFAULT_MAX_TOKENS)
774
+ max_tokens: req.max_tokens ?? ANTHROPIC_THINKING_BUDGETS[req.effort] + DEFAULT_MAX_TOKENS
1208
775
  };
1209
776
  if (system_parts.length > 0) body["system"] = system_parts.join("\n\n");
1210
777
  if (req.tools.length > 0) body["tools"] = to_anthropic_tools(req.tools);
1211
778
  if (req.stream) body["stream"] = true;
1212
- if (thinking_enabled) body["thinking"] = {
1213
- type: "enabled",
1214
- budget_tokens: budget
1215
- };
1216
- else {
779
+ if (req.effort === "none") {
1217
780
  if (req.temperature !== void 0) body["temperature"] = req.temperature;
1218
781
  if (req.top_p !== void 0) body["top_p"] = req.top_p;
782
+ } else {
783
+ body["thinking"] = {
784
+ type: "adaptive",
785
+ display: "summarized"
786
+ };
787
+ body["output_config"] = { effort: req.effort };
1219
788
  }
1220
789
  const passthrough = req.provider_options?.["anthropic"];
1221
790
  return passthrough === void 0 ? body : {
@@ -1276,56 +845,103 @@ function read_tool_use_identity(block, where) {
1276
845
  };
1277
846
  }
1278
847
  /**
1279
- * Fold one response `content` block into the text/tool_call accumulators.
1280
- * Text blocks append their string; tool_use blocks contribute a call whose id
1281
- * and name are validated by read_tool_use_identity; thinking /
1282
- * redacted_thinking blocks have no TurnResult field and are skipped.
848
+ * Read a thinking or redacted_thinking block as a reasoning part under
849
+ * `@ai-sdk/anthropic`'s own keys, so a transcript moves between the two
850
+ * transports: a thinking block keeps its text (empty when the display was
851
+ * omitted) and its `signature`, and a redacted one keeps its `data` as
852
+ * `redactedData`. A block missing its signature or data becomes a part with
853
+ * no data, which no adapter sends back. Any other block type is undefined.
1283
854
  */
1284
- function push_content_block(block, text_parts, tool_calls) {
855
+ function read_reasoning_block(block, type) {
856
+ if (type === "thinking") {
857
+ const thinking = Reflect.get(block, "thinking");
858
+ const signature = Reflect.get(block, "signature");
859
+ return {
860
+ type: "reasoning",
861
+ text: typeof thinking === "string" ? thinking : "",
862
+ ...typeof signature === "string" ? { provider_options: { anthropic: { signature } } } : {}
863
+ };
864
+ }
865
+ if (type !== "redacted_thinking") return void 0;
866
+ const data = Reflect.get(block, "data");
867
+ return {
868
+ type: "reasoning",
869
+ text: "",
870
+ ...typeof data === "string" ? { provider_options: { anthropic: { redactedData: data } } } : {}
871
+ };
872
+ }
873
+ /**
874
+ * Fold one response `content` block into the accumulators. Text blocks append
875
+ * their string; tool_use blocks contribute a call whose id and name are
876
+ * validated by read_tool_use_identity; thinking and redacted_thinking blocks
877
+ * become reasoning parts. Every kept block also lands in `parts`, in block
878
+ * order, for TurnResult.content.
879
+ */
880
+ function push_content_block(block, acc) {
1285
881
  if (block === null || typeof block !== "object") return;
1286
882
  const type = Reflect.get(block, "type");
1287
883
  if (type === "text") {
1288
884
  const text = Reflect.get(block, "text");
1289
- if (typeof text === "string") text_parts.push(text);
885
+ if (typeof text === "string") {
886
+ acc.text_parts.push(text);
887
+ acc.parts.push({
888
+ type: "text",
889
+ text
890
+ });
891
+ }
1290
892
  return;
1291
893
  }
1292
894
  if (type === "tool_use") {
1293
895
  const { id, name } = read_tool_use_identity(block, "response content");
1294
- tool_calls.push({
896
+ const call = {
1295
897
  id,
1296
898
  name,
1297
899
  input: Reflect.get(block, "input")
900
+ };
901
+ acc.tool_calls.push(call);
902
+ acc.parts.push({
903
+ type: "tool_call",
904
+ ...call
1298
905
  });
906
+ return;
1299
907
  }
908
+ const reasoning = read_reasoning_block(block, type);
909
+ if (reasoning !== void 0) acc.parts.push(reasoning);
1300
910
  }
1301
911
  /**
1302
912
  * Walk a response payload's `content` array into the flat text and tool_call
1303
- * lists a TurnResult carries. A non-array `content` yields empty lists,
1304
- * matching a payload that carried no content blocks.
913
+ * lists a TurnResult carries, plus every block as a part in order. A
914
+ * non-array `content` yields empty lists, matching a payload that carried no
915
+ * content blocks.
1305
916
  */
1306
917
  function collect_content_blocks(content) {
1307
- const text_parts = [];
1308
- const tool_calls = [];
1309
- if (Array.isArray(content)) for (const block of content) push_content_block(block, text_parts, tool_calls);
1310
- return {
1311
- text_parts,
1312
- tool_calls
918
+ const acc = {
919
+ text_parts: [],
920
+ tool_calls: [],
921
+ parts: []
1313
922
  };
923
+ if (Array.isArray(content)) for (const block of content) push_content_block(block, acc);
924
+ return acc;
1314
925
  }
1315
926
  /**
1316
927
  * Parse a non-stream Messages-API response payload into a TurnResult. The
1317
928
  * stream aggregator rebuilds this same payload shape and feeds it here too,
1318
- * so both paths share one parser.
929
+ * so both paths share one parser. A turn with a thinking block gets
930
+ * `content`, every block in order, and any other turn stays as it was.
1319
931
  */
1320
932
  function parse_messages_response(payload) {
1321
933
  if (payload === null || typeof payload !== "object") throw new provider_error("anthropic native: response payload is not a JSON object");
1322
- const { text_parts, tool_calls } = collect_content_blocks(Reflect.get(payload, "content"));
1323
- return {
934
+ const { text_parts, tool_calls, parts } = collect_content_blocks(Reflect.get(payload, "content"));
935
+ const result = {
1324
936
  text: text_parts.join(""),
1325
937
  tool_calls,
1326
938
  finish_reason: map_anthropic_stop_reason(Reflect.get(payload, "stop_reason")),
1327
939
  usage: map_anthropic_usage(Reflect.get(payload, "usage"))
1328
940
  };
941
+ return parts.some((part) => part.type === "reasoning") ? {
942
+ ...result,
943
+ content: parts
944
+ } : result;
1329
945
  }
1330
946
  /**
1331
947
  * Mid-stream `error` events reuse the HTTP status classification:
@@ -1363,6 +979,21 @@ function read_block_index(event) {
1363
979
  return index;
1364
980
  }
1365
981
  /**
982
+ * Parse one Messages-stream data line. OpenRouter's Anthropic-compatible
983
+ * endpoint ends every stream with a chat-completions `[DONE]` line after
984
+ * message_stop; it carries no event, so it reads as null, which the
985
+ * aggregator drops like a JSON null. Any other data that isn't JSON is a
986
+ * broken stream.
987
+ */
988
+ function parse_stream_event(data) {
989
+ if (data === "[DONE]") return null;
990
+ try {
991
+ return JSON.parse(data);
992
+ } catch {
993
+ throw new provider_error("anthropic native: stream event is not valid JSON");
994
+ }
995
+ }
996
+ /**
1366
997
  * Consume parsed Messages-stream events, dispatching StreamChunks as they
1367
998
  * arrive and rebuilding the non-stream response payload as it goes: text
1368
999
  * deltas accumulate into synthetic text blocks, tool_use input arrives as raw
@@ -1376,6 +1007,7 @@ function read_block_index(event) {
1376
1007
  function create_stream_aggregator(step_index, dispatch) {
1377
1008
  const content = [];
1378
1009
  const open_text = /* @__PURE__ */ new Map();
1010
+ const open_thinking = /* @__PURE__ */ new Map();
1379
1011
  const open_tools = /* @__PURE__ */ new Map();
1380
1012
  const usage = {};
1381
1013
  let stop_reason;
@@ -1384,6 +1016,21 @@ function create_stream_aggregator(step_index, dispatch) {
1384
1016
  if (raw === null || typeof raw !== "object") return;
1385
1017
  for (const [key, value] of Object.entries(raw)) if (typeof value === "number") usage[key] = value;
1386
1018
  };
1019
+ const start_reasoning_block = (index, block, type) => {
1020
+ if (type === "thinking") {
1021
+ const initial = Reflect.get(block, "thinking");
1022
+ const synthetic = {
1023
+ type: "thinking",
1024
+ thinking: typeof initial === "string" ? initial : "",
1025
+ signature: Reflect.get(block, "signature")
1026
+ };
1027
+ content.push(synthetic);
1028
+ open_thinking.set(index, synthetic);
1029
+ } else if (type === "redacted_thinking") content.push({
1030
+ type: "redacted_thinking",
1031
+ data: Reflect.get(block, "data")
1032
+ });
1033
+ };
1387
1034
  const on_block_start = async (event) => {
1388
1035
  const index = read_block_index(event);
1389
1036
  const block = Reflect.get(event, "content_block");
@@ -1418,7 +1065,25 @@ function create_stream_aggregator(step_index, dispatch) {
1418
1065
  name,
1419
1066
  step_index
1420
1067
  });
1068
+ return;
1421
1069
  }
1070
+ start_reasoning_block(index, block, type);
1071
+ };
1072
+ const on_thinking_delta = async (index, delta, type) => {
1073
+ const open = open_thinking.get(index);
1074
+ if (type === "signature_delta") {
1075
+ const signature = Reflect.get(delta, "signature");
1076
+ if (typeof signature === "string" && open !== void 0) open.signature = signature;
1077
+ return;
1078
+ }
1079
+ const thinking = Reflect.get(delta, "thinking");
1080
+ if (typeof thinking !== "string") return;
1081
+ if (open !== void 0) open.thinking += thinking;
1082
+ await dispatch({
1083
+ kind: "reasoning",
1084
+ text: thinking,
1085
+ step_index
1086
+ });
1422
1087
  };
1423
1088
  const on_block_delta = async (event) => {
1424
1089
  const index = read_block_index(event);
@@ -1437,13 +1102,8 @@ function create_stream_aggregator(step_index, dispatch) {
1437
1102
  });
1438
1103
  return;
1439
1104
  }
1440
- if (type === "thinking_delta") {
1441
- const thinking = Reflect.get(delta, "thinking");
1442
- if (typeof thinking === "string") await dispatch({
1443
- kind: "reasoning",
1444
- text: thinking,
1445
- step_index
1446
- });
1105
+ if (type === "thinking_delta" || type === "signature_delta") {
1106
+ await on_thinking_delta(index, delta, type);
1447
1107
  return;
1448
1108
  }
1449
1109
  if (type === "input_json_delta") {
@@ -1463,6 +1123,7 @@ function create_stream_aggregator(step_index, dispatch) {
1463
1123
  const index = read_block_index(event);
1464
1124
  const open = open_tools.get(index);
1465
1125
  open_text.delete(index);
1126
+ open_thinking.delete(index);
1466
1127
  if (open === void 0) return;
1467
1128
  open_tools.delete(index);
1468
1129
  if (open.json.length > 0) try {
@@ -1479,12 +1140,7 @@ function create_stream_aggregator(step_index, dispatch) {
1479
1140
  };
1480
1141
  return {
1481
1142
  async handle(data) {
1482
- let event;
1483
- try {
1484
- event = JSON.parse(data);
1485
- } catch {
1486
- throw new provider_error("anthropic native: stream event is not valid JSON");
1487
- }
1143
+ const event = parse_stream_event(data);
1488
1144
  if (event === null || typeof event !== "object") return;
1489
1145
  switch (Reflect.get(event, "type")) {
1490
1146
  case "message_start": {
@@ -1579,27 +1235,6 @@ const create_anthropic_native_adapter = (init) => {
1579
1235
  //#endregion
1580
1236
  //#region src/engine/providers/anthropic.ts
1581
1237
  /**
1582
- * Map an effort level to Anthropic's extended-thinking provider options.
1583
- *
1584
- * A zero budget (which is what `none` maps to) sends no thinking config at all
1585
- * rather than an explicit "disabled" value, so non-reasoning models see a plain
1586
- * request.
1587
- */
1588
- function translate_anthropic_effort(effort) {
1589
- const budget = ANTHROPIC_THINKING_BUDGETS[effort];
1590
- if (budget === 0) return {
1591
- provider_options: {},
1592
- effort_ignored: false
1593
- };
1594
- return {
1595
- provider_options: { anthropic: { thinking: {
1596
- type: "enabled",
1597
- budgetTokens: budget
1598
- } } },
1599
- effort_ignored: false
1600
- };
1601
- }
1602
- /**
1603
1238
  * Normalize Anthropic usage via the shared default mapper; the AI SDK already
1604
1239
  * reports cache and reasoning tokens in the fields the default reads.
1605
1240
  */
@@ -1640,7 +1275,7 @@ const create_anthropic_ai_sdk_adapter = (init) => {
1640
1275
  if (base_url !== void 0) config.baseURL = base_url;
1641
1276
  return sdk.createAnthropic(config)(model_id);
1642
1277
  },
1643
- translate_effort: translate_anthropic_effort,
1278
+ translate_effort: translate_effort_to_sdk_reasoning,
1644
1279
  normalize_usage: normalize_anthropic_usage,
1645
1280
  supports: (capability) => SUPPORTED$9.has(capability)
1646
1281
  };
@@ -1671,10 +1306,10 @@ const create_anthropic_ai_sdk_adapter = (init) => {
1671
1306
  * parallel code paths.
1672
1307
  */
1673
1308
  /**
1674
- * The wire enum is `low | medium | high` only, so `xhigh` and `max` clamp to
1675
- * `high`, the same clamp as the ai_sdk transport's `reasoningEffort`. `none`
1676
- * omits the field entirely. Non-reasoning models ignore the field
1677
- * server-side; the adapter does not model-sniff.
1309
+ * The flat field's wire enum is `low | medium | high` only, so `xhigh` and
1310
+ * `max` clamp to `high`. The nested shape takes every level, so it skips this
1311
+ * table. Non-reasoning models ignore either field server-side; the adapter
1312
+ * does not model-sniff.
1678
1313
  */
1679
1314
  const OPENAI_COMPATIBLE_REASONING_EFFORT = {
1680
1315
  low: "low",
@@ -1684,6 +1319,21 @@ const OPENAI_COMPATIBLE_REASONING_EFFORT = {
1684
1319
  max: "high"
1685
1320
  };
1686
1321
  /**
1322
+ * The field each joinable reasoning_details entry type carries its visible
1323
+ * text in. A Map rather than an object literal, since the lookup key is a
1324
+ * `type` read off the wire.
1325
+ */
1326
+ const REASONING_TEXT_FIELD = /* @__PURE__ */ new Map([["reasoning.text", "text"], ["reasoning.summary", "summary"]]);
1327
+ /**
1328
+ * Read a reasoning_details entry's visible text: a text entry's `text`, a
1329
+ * summary's `summary`, and '' for any other entry or a missing value.
1330
+ */
1331
+ function reasoning_detail_text(entry) {
1332
+ const field = REASONING_TEXT_FIELD.get(entry["type"]);
1333
+ const value = field === void 0 ? void 0 : entry[field];
1334
+ return typeof value === "string" ? value : "";
1335
+ }
1336
+ /**
1687
1337
  * Map user message content to this wire's shape (a string, or an array of
1688
1338
  * text parts). Image parts throw a capability error: they are not mapped on
1689
1339
  * the native transport.
@@ -1702,15 +1352,16 @@ function to_user_content$1(content, provider) {
1702
1352
  }
1703
1353
  /**
1704
1354
  * Map assistant message content to Chat Completions shape: text parts join
1705
- * into `content`, and tool-call parts become `tool_calls` entries with
1706
- * JSON-stringified arguments.
1355
+ * into `content`, tool-call parts become `tool_calls` entries with
1356
+ * JSON-stringified arguments, and the provider's own reasoning parts give
1357
+ * their entries back as `reasoning_details`.
1707
1358
  */
1708
- function to_assistant_message$1(content) {
1359
+ function to_assistant_message$1(content, provider) {
1709
1360
  if (typeof content === "string") return {
1710
1361
  role: "assistant",
1711
1362
  content
1712
1363
  };
1713
- const { texts, tool_parts } = split_assistant_parts(content);
1364
+ const { texts, tool_parts, reasoning } = split_assistant_parts(content, provider);
1714
1365
  const tool_calls = tool_parts.map((part) => ({
1715
1366
  id: part.id,
1716
1367
  type: "function",
@@ -1725,9 +1376,20 @@ function to_assistant_message$1(content) {
1725
1376
  content: text.length > 0 || tool_calls.length === 0 ? text : null
1726
1377
  };
1727
1378
  if (tool_calls.length > 0) message.tool_calls = tool_calls;
1379
+ const reasoning_details = reasoning.flatMap((part) => own_reasoning_detail(part, provider));
1380
+ if (reasoning_details.length > 0) message.reasoning_details = reasoning_details;
1728
1381
  return message;
1729
1382
  }
1730
1383
  /**
1384
+ * The reasoning_details entry a reasoning part keeps for `provider`, as a
1385
+ * one-element list, or an empty one when the part carries no entry (a part
1386
+ * another transport keyed for this provider, say).
1387
+ */
1388
+ function own_reasoning_detail(part, provider) {
1389
+ const detail = part.provider_options?.[provider]?.["detail"];
1390
+ return is_record$1(detail) ? [detail] : [];
1391
+ }
1392
+ /**
1731
1393
  * Map Fascicle Message[] to Chat Completions shape. Unlike the Messages API
1732
1394
  * there is no top-level system field or role-alternation constraint: system
1733
1395
  * messages map in place at any position, and tool results are first-class
@@ -1743,7 +1405,7 @@ function to_chat_messages(messages, provider) {
1743
1405
  role: "user",
1744
1406
  content: to_user_content$1(content, provider)
1745
1407
  }),
1746
- assistant: to_assistant_message$1,
1408
+ assistant: (content) => to_assistant_message$1(content, provider),
1747
1409
  tool: (message) => ({
1748
1410
  role: "tool",
1749
1411
  tool_call_id: message.tool_call_id,
@@ -1766,10 +1428,19 @@ function to_chat_tools(tools) {
1766
1428
  }));
1767
1429
  }
1768
1430
  /**
1431
+ * Write effort into the body in the dialect's shape. `none` writes nothing,
1432
+ * on either shape.
1433
+ */
1434
+ function apply_effort(body, effort, dialect) {
1435
+ if (effort === "none") return;
1436
+ if (dialect.effort_shape === "nested") body["reasoning"] = { effort };
1437
+ else body["reasoning_effort"] = OPENAI_COMPATIBLE_REASONING_EFFORT[effort];
1438
+ }
1439
+ /**
1769
1440
  * Build the Chat Completions request body for one turn: mapped messages and
1770
- * tools, the token-limit field named per dialect, sampling params, the
1771
- * effort-derived reasoning_effort, and the dialect's
1772
- * `provider_options.<name>` wire passthrough merged last.
1441
+ * tools, the token-limit field named per dialect, sampling params, effort in
1442
+ * the dialect's shape, and the dialect's `provider_options.<name>` wire
1443
+ * passthrough merged last.
1773
1444
  */
1774
1445
  function build_chat_completions_body(req, dialect) {
1775
1446
  const messages = [];
@@ -1786,7 +1457,7 @@ function build_chat_completions_body(req, dialect) {
1786
1457
  if (req.max_tokens !== void 0) body[dialect.token_limit_field] = req.max_tokens;
1787
1458
  if (req.temperature !== void 0) body["temperature"] = req.temperature;
1788
1459
  if (req.top_p !== void 0) body["top_p"] = req.top_p;
1789
- if (req.effort !== "none") body["reasoning_effort"] = OPENAI_COMPATIBLE_REASONING_EFFORT[req.effort];
1460
+ apply_effort(body, req.effort, dialect);
1790
1461
  if (req.stream) {
1791
1462
  body["stream"] = true;
1792
1463
  if (dialect.stream_include_usage) body["stream_options"] = { include_usage: true };
@@ -1849,6 +1520,32 @@ function map_chat_usage(raw, dialect) {
1849
1520
  return totals;
1850
1521
  }
1851
1522
  /**
1523
+ * Give a turn the charge a cost-reporting dialect puts on its usage object,
1524
+ * and leave every other turn as it was. `usage.cost` is the request's cost in
1525
+ * US dollars (OpenRouter's credits are dollars). On a bring-your-own-key
1526
+ * request it is only OpenRouter's fee, so the upstream provider's own charge
1527
+ * (`cost_details.upstream_inference_cost`) is added, a null one counting as
1528
+ * nothing. The raw fields ride `provider_reported` under the dialect's name
1529
+ * as sent. A usage object without a numeric `cost` reports nothing, so the
1530
+ * loop falls back to pricing.
1531
+ */
1532
+ function with_reported_cost(result, raw_usage, dialect) {
1533
+ if (!dialect.reports_cost || !is_record$1(raw_usage)) return result;
1534
+ const cost = raw_usage["cost"];
1535
+ if (typeof cost !== "number") return result;
1536
+ const details = raw_usage["cost_details"];
1537
+ const upstream = is_record$1(details) ? details["upstream_inference_cost"] : void 0;
1538
+ const byok_upstream = raw_usage["is_byok"] === true && typeof upstream === "number" ? upstream : 0;
1539
+ const reported = { cost };
1540
+ if (raw_usage["is_byok"] !== void 0) reported["is_byok"] = raw_usage["is_byok"];
1541
+ if (details !== void 0) reported["cost_details"] = details;
1542
+ return {
1543
+ ...result,
1544
+ reported_cost_usd: cost + byok_upstream,
1545
+ provider_reported: { [dialect.name]: reported }
1546
+ };
1547
+ }
1548
+ /**
1852
1549
  * Parse one Chat Completions tool_calls entry into the engine's tool-call
1853
1550
  * shape, throwing a provider_error if the entry is missing id, function
1854
1551
  * name, or valid JSON arguments.
@@ -1881,23 +1578,123 @@ function parse_chat_completion(payload, dialect) {
1881
1578
  const content = Reflect.get(message, "content");
1882
1579
  const raw_tool_calls = Reflect.get(message, "tool_calls");
1883
1580
  const tool_calls = Array.isArray(raw_tool_calls) ? raw_tool_calls.map((entry) => parse_tool_call(entry, dialect.name)) : [];
1884
- return {
1581
+ const raw_usage = Reflect.get(payload, "usage");
1582
+ return with_reported_cost(with_reasoning_content({
1885
1583
  text: typeof content === "string" ? content : "",
1886
1584
  tool_calls,
1887
1585
  finish_reason: map_chat_finish_reason(Reflect.get(choice, "finish_reason")),
1888
- usage: map_chat_usage(Reflect.get(payload, "usage"), dialect)
1586
+ usage: map_chat_usage(raw_usage, dialect)
1587
+ }, read_reasoning_parts(message, dialect)), raw_usage, dialect);
1588
+ }
1589
+ /**
1590
+ * Read a response message's reasoning_details into reasoning parts, one per
1591
+ * entry, in order, each keeping its entry under
1592
+ * `provider_options.<name>.detail` for the trip back. Empty unless the
1593
+ * dialect reads reasoning_details.
1594
+ */
1595
+ function read_reasoning_parts(message, dialect) {
1596
+ if (dialect.reasoning !== "reasoning_details") return [];
1597
+ const entries = Reflect.get(message, "reasoning_details");
1598
+ if (!Array.isArray(entries)) return [];
1599
+ return entries.filter(is_record$1).map((detail) => ({
1600
+ type: "reasoning",
1601
+ text: reasoning_detail_text(detail),
1602
+ provider_options: { [dialect.name]: { detail } }
1603
+ }));
1604
+ }
1605
+ /**
1606
+ * Give a turn its `content` when it carries reasoning, and leave every other
1607
+ * turn exactly as it was. This wire keeps reasoning, text, and tool calls in
1608
+ * separate fields, so the parts go in that order.
1609
+ */
1610
+ function with_reasoning_content(result, reasoning) {
1611
+ if (reasoning.length === 0) return result;
1612
+ const content = [...reasoning];
1613
+ if (result.text.length > 0) content.push({
1614
+ type: "text",
1615
+ text: result.text
1616
+ });
1617
+ for (const call of result.tool_calls) content.push({
1618
+ type: "tool_call",
1619
+ ...call
1620
+ });
1621
+ return {
1622
+ ...result,
1623
+ content
1624
+ };
1625
+ }
1626
+ /**
1627
+ * The error a compatible server reports after its stream began under HTTP
1628
+ * 200: a frame whose top-level `error` holds the server's message and, on
1629
+ * OpenRouter, the HTTP status the failure would have had as a numeric
1630
+ * `code`. Carrying that code as `status` lets the engine's retry ladder treat
1631
+ * it as it treats the same error at the start of a response, so a 429 or a
1632
+ * 5xx before any chunk retries, and any error after a chunk fails the turn as
1633
+ * a stream interruption that names the message.
1634
+ */
1635
+ function stream_error_frame(error, provider) {
1636
+ const message = error["message"];
1637
+ const code = error["code"];
1638
+ const detail = typeof message === "string" && message.length > 0 ? message : "(no message)";
1639
+ const label = code === void 0 ? "" : ` (code ${JSON.stringify(code)})`;
1640
+ return new provider_error(`${provider} native: the server reported an error mid-stream${label}: ${detail}`, typeof code === "number" ? { status: code } : {});
1641
+ }
1642
+ /**
1643
+ * Join streamed reasoning_details fragments into whole entries, the way
1644
+ * OpenRouter's own AI SDK provider does. Consecutive `reasoning.text` or
1645
+ * `reasoning.summary` fragments of one type grow one entry until the type
1646
+ * changes or a signature closes a text entry, and any other entry (encrypted,
1647
+ * a server tool call) stands alone. `index` is ignored, since providers
1648
+ * repeat it. A later fragment fills only the fields its entry still lacks, so
1649
+ * the late signature lands and the first format stays. add() returns the
1650
+ * fragment's visible text, '' when it has none.
1651
+ */
1652
+ function create_reasoning_joiner() {
1653
+ const entries = [];
1654
+ let open;
1655
+ return {
1656
+ entries,
1657
+ add(fragment) {
1658
+ if (!is_record$1(fragment)) return "";
1659
+ const field = REASONING_TEXT_FIELD.get(fragment["type"]);
1660
+ if (field === void 0) {
1661
+ entries.push(fragment);
1662
+ open = void 0;
1663
+ return "";
1664
+ }
1665
+ const text = reasoning_detail_text(fragment);
1666
+ const joinable = open?.["type"] === fragment["type"] ? open : void 0;
1667
+ let entry;
1668
+ if (joinable === void 0) {
1669
+ entry = { ...fragment };
1670
+ entries.push(entry);
1671
+ } else {
1672
+ entry = joinable;
1673
+ const joined = reasoning_detail_text(entry) + text;
1674
+ for (const [key, value] of Object.entries(fragment)) entry[key] ??= value;
1675
+ entry[field] = joined;
1676
+ }
1677
+ const signature = entry["signature"];
1678
+ open = typeof signature === "string" && signature.length > 0 ? void 0 : entry;
1679
+ return text;
1680
+ }
1889
1681
  };
1890
1682
  }
1891
1683
  /**
1892
1684
  * Consume Chat Completions stream frames (the JSON payload of each SSE
1893
1685
  * `data:` line, plus the literal `[DONE]` terminator), dispatching
1894
1686
  * StreamChunks as they arrive and rebuilding the non-stream payload as it
1895
- * goes: `delta.content` accumulates into the message text, and
1687
+ * goes: `delta.content` accumulates into the message text,
1896
1688
  * `delta.tool_calls[]` entries accumulate `function.arguments` string deltas
1897
- * keyed by their `index` field. A tool call closes (parsed input dispatched)
1689
+ * keyed by their `index` field, and under a reasoning_details dialect each
1690
+ * `delta.reasoning_details` fragment dispatches a reasoning chunk and joins
1691
+ * into the message's entries. A tool call closes (parsed input dispatched)
1898
1692
  * when the choice's finish_reason frame arrives, or at `[DONE]` for servers
1899
- * that never send one. Usage rides the final pre-DONE frame
1900
- * (stream_options.include_usage); a stream that ends without `[DONE]` is
1693
+ * that never send one. A frame with a top-level `error` throws before its
1694
+ * choice is read, on any dialect. Usage rides the final pre-DONE frame
1695
+ * (stream_options.include_usage), and OpenRouter's reported cost rides
1696
+ * inside it, so the streamed turn reads the same charge as the whole
1697
+ * response; a stream that ends without `[DONE]` is
1901
1698
  * truncated output, so complete() fails loud instead of returning a partial
1902
1699
  * turn. complete() feeds the synthetic payload through parse_chat_completion,
1903
1700
  * which is what makes the streamed TurnResult equal the non-streamed one by
@@ -1907,6 +1704,7 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
1907
1704
  let text = "";
1908
1705
  const tool_calls = [];
1909
1706
  const by_index = /* @__PURE__ */ new Map();
1707
+ const reasoning = create_reasoning_joiner();
1910
1708
  let finish_reason;
1911
1709
  let usage;
1912
1710
  let done = false;
@@ -1968,9 +1766,31 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
1968
1766
  });
1969
1767
  }
1970
1768
  };
1769
+ const on_reasoning_delta = async (delta) => {
1770
+ if (dialect.reasoning !== "reasoning_details") return;
1771
+ const fragments = Reflect.get(delta, "reasoning_details");
1772
+ if (Array.isArray(fragments) && fragments.length > 0) {
1773
+ for (const fragment of fragments) {
1774
+ const shown = reasoning.add(fragment);
1775
+ if (shown.length > 0) await dispatch({
1776
+ kind: "reasoning",
1777
+ text: shown,
1778
+ step_index
1779
+ });
1780
+ }
1781
+ return;
1782
+ }
1783
+ const bare = Reflect.get(delta, "reasoning");
1784
+ if (typeof bare === "string" && bare.length > 0) await dispatch({
1785
+ kind: "reasoning",
1786
+ text: bare,
1787
+ step_index
1788
+ });
1789
+ };
1971
1790
  const on_choice = async (choice) => {
1972
1791
  const delta = Reflect.get(choice, "delta");
1973
1792
  if (delta !== null && typeof delta === "object") {
1793
+ await on_reasoning_delta(delta);
1974
1794
  const content = Reflect.get(delta, "content");
1975
1795
  if (typeof content === "string" && content.length > 0) {
1976
1796
  text += content;
@@ -2010,6 +1830,8 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
2010
1830
  throw new provider_error(`${dialect.name} native: stream frame is not valid JSON`);
2011
1831
  }
2012
1832
  if (frame === null || typeof frame !== "object") return;
1833
+ const error = Reflect.get(frame, "error");
1834
+ if (is_record$1(error)) throw stream_error_frame(error, dialect.name);
2013
1835
  const raw_usage = Reflect.get(frame, "usage");
2014
1836
  if (raw_usage !== null && typeof raw_usage === "object") usage = raw_usage;
2015
1837
  const choices = Reflect.get(frame, "choices");
@@ -2021,7 +1843,8 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
2021
1843
  const ordered = in_index_order();
2022
1844
  const message = {
2023
1845
  role: "assistant",
2024
- content: text.length > 0 || ordered.length === 0 ? text : null
1846
+ content: text.length > 0 || ordered.length === 0 ? text : null,
1847
+ reasoning_details: reasoning.entries
2025
1848
  };
2026
1849
  if (ordered.length > 0) message["tool_calls"] = ordered.map((call) => ({
2027
1850
  id: call.id,
@@ -2097,26 +1920,6 @@ const create_openai_compatible_adapter = (dialect) => {
2097
1920
  };
2098
1921
  //#endregion
2099
1922
  //#region src/engine/providers/openai.ts
2100
- const OPENAI_REASONING_EFFORT = {
2101
- low: "low",
2102
- medium: "medium",
2103
- high: "high",
2104
- xhigh: "high",
2105
- max: "high"
2106
- };
2107
- /**
2108
- * Map an EffortLevel to OpenAI's `reasoningEffort` provider option.
2109
- */
2110
- function translate_openai_effort(effort) {
2111
- if (effort === "none") return {
2112
- provider_options: {},
2113
- effort_ignored: false
2114
- };
2115
- return {
2116
- provider_options: { openai: { reasoningEffort: OPENAI_REASONING_EFFORT[effort] } },
2117
- effort_ignored: false
2118
- };
2119
- }
2120
1923
  /**
2121
1924
  * Normalize OpenAI's raw usage payload into UsageTotals.
2122
1925
  */
@@ -2166,6 +1969,7 @@ const create_openai_native_adapter = (init) => {
2166
1969
  token_limit_field: "max_completion_tokens",
2167
1970
  stream_include_usage: true,
2168
1971
  tolerant_usage: false,
1972
+ reports_cost: false,
2169
1973
  ...organization !== void 0 ? { extra_headers: { "OpenAI-Organization": organization } } : {}
2170
1974
  };
2171
1975
  return create_openai_compatible_adapter(dialect);
@@ -2189,34 +1993,13 @@ const create_openai_ai_sdk_adapter = (init) => {
2189
1993
  if (organization !== void 0) config.organization = organization;
2190
1994
  return sdk.createOpenAI(config)(model_id);
2191
1995
  },
2192
- translate_effort: translate_openai_effort,
1996
+ translate_effort: translate_effort_to_sdk_reasoning,
2193
1997
  normalize_usage: normalize_openai_usage,
2194
1998
  supports: (capability) => SUPPORTED$7.has(capability)
2195
1999
  };
2196
2000
  };
2197
2001
  //#endregion
2198
2002
  //#region src/engine/providers/google.ts
2199
- const GOOGLE_THINKING_BUDGET = {
2200
- low: 1024,
2201
- medium: 8192,
2202
- high: 24576,
2203
- xhigh: 32768,
2204
- max: 32768
2205
- };
2206
- /**
2207
- * Map an EffortLevel to Google's `thinkingConfig.thinkingBudget` provider
2208
- * option.
2209
- */
2210
- function translate_google_effort(effort) {
2211
- if (effort === "none") return {
2212
- provider_options: {},
2213
- effort_ignored: false
2214
- };
2215
- return {
2216
- provider_options: { google: { thinkingConfig: { thinkingBudget: GOOGLE_THINKING_BUDGET[effort] } } },
2217
- effort_ignored: false
2218
- };
2219
- }
2220
2003
  /**
2221
2004
  * Normalize Google's raw usage payload into UsageTotals.
2222
2005
  */
@@ -2248,7 +2031,7 @@ const create_google_adapter = (init) => {
2248
2031
  if (base_url !== void 0) config.baseURL = base_url;
2249
2032
  return sdk.createGoogleGenerativeAI(config)(model_id);
2250
2033
  },
2251
- translate_effort: translate_google_effort,
2034
+ translate_effort: translate_effort_to_sdk_reasoning,
2252
2035
  normalize_usage: normalize_google_usage,
2253
2036
  supports: (capability) => SUPPORTED$6.has(capability)
2254
2037
  };
@@ -2272,14 +2055,15 @@ function to_user_content(content) {
2272
2055
  /**
2273
2056
  * Map assistant message content to this wire's message shape: text parts
2274
2057
  * join into `content`, and tool-call parts become `tool_calls` entries with
2275
- * structured (not JSON-string) arguments.
2058
+ * structured (not JSON-string) arguments. Reasoning parts are never sent:
2059
+ * this adapter maps no `thinking` field in either direction.
2276
2060
  */
2277
2061
  function to_assistant_message(content) {
2278
2062
  if (typeof content === "string") return {
2279
2063
  role: "assistant",
2280
2064
  content
2281
2065
  };
2282
- const { texts, tool_parts } = split_assistant_parts(content);
2066
+ const { texts, tool_parts } = split_assistant_parts(content, "ollama");
2283
2067
  const message = {
2284
2068
  role: "assistant",
2285
2069
  content: texts.join("")
@@ -2687,7 +2471,8 @@ const create_lmstudio_native_adapter = (init) => {
2687
2471
  auth: { kind: "none" },
2688
2472
  token_limit_field: "max_tokens",
2689
2473
  stream_include_usage: true,
2690
- tolerant_usage: true
2474
+ tolerant_usage: true,
2475
+ reports_cost: false
2691
2476
  });
2692
2477
  };
2693
2478
  /**
@@ -2763,8 +2548,10 @@ const create_openrouter_adapter = (init) => {
2763
2548
  /**
2764
2549
  * Build the openrouter dialect and hand it to the shared OpenAI-compatible
2765
2550
  * core: Bearer auth, the optional `HTTP-Referer`/`X-Title` attribution
2766
- * headers, the `max_tokens` token-limit field, and strict usage. The
2767
- * empty-api_key guard rides on the core, which throws the same
2551
+ * headers, the `max_tokens` token-limit field, the nested effort shape,
2552
+ * reasoning read from and sent back as `reasoning_details`, strict usage,
2553
+ * and the cost OpenRouter reports on every response. The empty-api_key guard
2554
+ * rides on the core, which throws the same
2768
2555
  * `openrouter provider requires a non-empty api_key` engine_config_error.
2769
2556
  */
2770
2557
  const create_openrouter_native_adapter = (init) => {
@@ -2784,6 +2571,9 @@ const create_openrouter_native_adapter = (init) => {
2784
2571
  token_limit_field: "max_tokens",
2785
2572
  stream_include_usage: true,
2786
2573
  tolerant_usage: false,
2574
+ reports_cost: true,
2575
+ effort_shape: "nested",
2576
+ reasoning: "reasoning_details",
2787
2577
  ...Object.keys(extra_headers).length > 0 ? { extra_headers } : {}
2788
2578
  };
2789
2579
  return create_openai_compatible_adapter(dialect);
@@ -2818,31 +2608,6 @@ const create_openrouter_ai_sdk_adapter = (init) => {
2818
2608
  };
2819
2609
  //#endregion
2820
2610
  //#region src/engine/providers/bedrock.ts
2821
- const BEDROCK_THINKING_BUDGETS = {
2822
- none: 0,
2823
- low: 1024,
2824
- medium: 5e3,
2825
- high: 2e4,
2826
- xhigh: 32e3,
2827
- max: 64e3
2828
- };
2829
- /**
2830
- * Map an EffortLevel to the Bedrock `reasoningConfig` provider option.
2831
- */
2832
- function translate_bedrock_effort(effort) {
2833
- const budget = BEDROCK_THINKING_BUDGETS[effort];
2834
- if (budget === 0) return {
2835
- provider_options: {},
2836
- effort_ignored: false
2837
- };
2838
- return {
2839
- provider_options: { bedrock: { reasoningConfig: {
2840
- type: "enabled",
2841
- budgetTokens: budget
2842
- } } },
2843
- effort_ignored: false
2844
- };
2845
- }
2846
2611
  /**
2847
2612
  * Normalize Bedrock's raw usage payload into UsageTotals.
2848
2613
  */
@@ -2937,7 +2702,7 @@ const create_bedrock_adapter = (init) => {
2937
2702
  async build_model(model_id) {
2938
2703
  return (await load_optional_peer("@ai-sdk/amazon-bedrock")).createAmazonBedrock(to_sdk_config(statics, await resolve_credential_provider()))(model_id);
2939
2704
  },
2940
- translate_effort: translate_bedrock_effort,
2705
+ translate_effort: translate_effort_to_sdk_reasoning,
2941
2706
  normalize_usage: normalize_bedrock_usage,
2942
2707
  supports: (capability) => SUPPORTED$1.has(capability)
2943
2708
  };
@@ -2946,16 +2711,13 @@ const create_bedrock_adapter = (init) => {
2946
2711
  //#region src/engine/providers/claude_cli/constants.ts
2947
2712
  /**
2948
2713
  * Frozen constants for the claude_cli provider: startup and idle defaults,
2949
- * the escalation window, cost decomposition multipliers, and the auth
2950
- * failure pattern list.
2714
+ * the escalation window, and the auth failure pattern list.
2951
2715
  *
2952
2716
  * Arrays are Object.freeze-d at module load so downstream code cannot mutate
2953
2717
  * them.
2954
2718
  */
2955
2719
  const CLI_BINARY_DEFAULT = "claude";
2956
2720
  const SIGKILL_ESCALATION_MS = 2e3;
2957
- const CACHE_READ_MULTIPLIER = .1;
2958
- const CACHE_WRITE_MULTIPLIER = 1.25;
2959
2721
  const CLI_AUTH_ERROR_PATTERNS = Object.freeze([
2960
2722
  "authentication",
2961
2723
  "unauthorized",
@@ -4027,47 +3789,15 @@ function snapshot(state) {
4027
3789
  //#endregion
4028
3790
  //#region src/engine/providers/claude_cli/cost.ts
4029
3791
  /**
4030
- * Split a single `total_cost_usd` figure into an input/output cost
4031
- * breakdown, weighted by token counts.
3792
+ * Cost allocation from the CLI's reported `total_cost_usd`.
4032
3793
  *
4033
- * Cache-read and cache-write tokens are cheaper and pricier respectively
4034
- * than base input tokens, so they're weighted by `CACHE_READ_MULTIPLIER`
4035
- * and `CACHE_WRITE_MULTIPLIER` before the total is distributed
4036
- * proportionally across all weighted components. Returns an all-zero
4037
- * estimate when there's no usage or no cost to distribute.
3794
+ * The CLI reports a single total_cost_usd and per-turn usage; it does not
3795
+ * break cost down by component or by turn. This module splits the total
3796
+ * across turns proportional to each turn's output tokens, then hands each
3797
+ * turn's share to the engine's `decompose_total_cost` for the component
3798
+ * split. Sum equality is preserved exactly by giving any floating-point
3799
+ * rounding remainder to the last turn.
4038
3800
  */
4039
- function decompose_total_cost(total_cost_usd, usage) {
4040
- const input = Math.max(0, usage.input_tokens);
4041
- const output = Math.max(0, usage.output_tokens);
4042
- const cached = Math.max(0, usage.cached_input_tokens ?? 0);
4043
- const cache_write = Math.max(0, usage.cache_write_tokens ?? 0);
4044
- const input_weight = Math.max(0, input - cached - cache_write);
4045
- const output_weight = output;
4046
- const cached_weight = cached * CACHE_READ_MULTIPLIER;
4047
- const cache_write_weight = cache_write * CACHE_WRITE_MULTIPLIER;
4048
- const total_weight = input_weight + output_weight + cached_weight + cache_write_weight;
4049
- if (total_weight === 0 || total_cost_usd === 0) return {
4050
- total_usd: total_cost_usd,
4051
- input_usd: 0,
4052
- output_usd: 0,
4053
- currency: "USD",
4054
- is_estimate: true
4055
- };
4056
- const input_usd = total_cost_usd * input_weight / total_weight;
4057
- const output_usd = total_cost_usd * output_weight / total_weight;
4058
- const cached_usd_raw = total_cost_usd * cached_weight / total_weight;
4059
- const cache_write_usd_raw = total_cost_usd * cache_write_weight / total_weight;
4060
- const breakdown = {
4061
- total_usd: total_cost_usd,
4062
- input_usd,
4063
- output_usd,
4064
- currency: "USD",
4065
- is_estimate: true
4066
- };
4067
- if (cached > 0) breakdown.cached_input_usd = cached_usd_raw;
4068
- if (cache_write > 0) breakdown.cache_write_usd = cache_write_usd_raw;
4069
- return breakdown;
4070
- }
4071
3801
  /**
4072
3802
  * Split a run-level `total` into a per-turn share for each turn, proportional
4073
3803
  * to its output tokens, falling back to an equal split when every turn reports
@@ -5249,48 +4979,6 @@ function get_provider_factory(name) {
5249
4979
  return factory;
5250
4980
  }
5251
4981
  //#endregion
5252
- //#region src/engine/leading_system.ts
5253
- /**
5254
- * Collapse a leading run of system messages into a single joined string,
5255
- * returning the remaining messages.
5256
- *
5257
- * Both message paths hoist a leading system run out of their list the same way:
5258
- * the engine-Message side (generate.ts split_leading_system_messages) hands the
5259
- * run to a native adapter's TurnRequest.system, and the AI SDK side
5260
- * (providers/ai_sdk/invoke.ts split_leading_system) hands it to the SDK's
5261
- * top-level `instructions` option. Same guard, same '\n\n' join, and the same
5262
- * "return the list untouched when hoisting would leave `messages` empty" rule,
5263
- * since provider APIs reject an empty messages array. They differ only in the
5264
- * element type and how a system message's text is read, so each caller supplies
5265
- * `system_text`; the traversal and its guards live here once.
5266
- */
5267
- function split_leading_system_run(messages, system_text) {
5268
- let run_end = 0;
5269
- const system_parts = [];
5270
- while (run_end < messages.length) {
5271
- const m = messages[run_end];
5272
- if (!is_leading_system(m)) break;
5273
- system_parts.push(system_text(m));
5274
- run_end += 1;
5275
- }
5276
- const rest = messages.slice(run_end);
5277
- if (system_parts.length === 0 || rest.length === 0) return { messages: [...messages] };
5278
- return {
5279
- system: system_parts.join("\n\n"),
5280
- messages: rest
5281
- };
5282
- }
5283
- /**
5284
- * Narrow a possibly undefined message to the system member of its union.
5285
- *
5286
- * A cast-free type guard so `split_leading_system_run` can hand the narrowed
5287
- * message straight to `system_text` without a `T`-eroding assertion; the
5288
- * optional chain covers the out-of-bounds `undefined` the index access yields.
5289
- */
5290
- function is_leading_system(m) {
5291
- return m?.role === "system";
5292
- }
5293
- //#endregion
5294
4982
  //#region src/engine/merge_defaults.ts
5295
4983
  /**
5296
4984
  * Default-merging helpers for engine-level `EngineDefaults`.
@@ -5905,6 +5593,7 @@ async function request_approval(tool, input, step_index, tool_call_id, abort, on
5905
5593
  tool_name: tool.name,
5906
5594
  input,
5907
5595
  step_index,
5596
+ tool_call_id,
5908
5597
  abort
5909
5598
  }));
5910
5599
  const approved = await new Promise((resolve, reject) => {
@@ -5975,43 +5664,95 @@ function safe_json_stringify(value) {
5975
5664
  }
5976
5665
  }
5977
5666
  /**
5978
- * Build the assistant history message for a turn: plain text when there are
5979
- * no tool calls, otherwise structured text plus tool_call parts.
5667
+ * Build the assistant history message for a turn. A turn with `content`
5668
+ * keeps the provider's parts in its order, data included, so reasoning goes
5669
+ * back beside the text and calls it came with. Any other turn is plain text
5670
+ * when there are no tool calls, otherwise text plus tool_call parts.
5980
5671
  */
5981
- function build_assistant_message(text, tool_calls) {
5982
- if (tool_calls.length === 0) return {
5672
+ function build_assistant_message(turn, view, kept) {
5673
+ if (turn.content !== void 0) return {
5674
+ role: "assistant",
5675
+ content: history_parts(turn.content, view, kept)
5676
+ };
5677
+ if (kept.length === 0) return {
5983
5678
  role: "assistant",
5984
- content: text
5679
+ content: view.history_text
5985
5680
  };
5986
5681
  const parts = [];
5987
- if (text.length > 0) parts.push({
5682
+ if (view.history_text.length > 0) parts.push({
5988
5683
  type: "text",
5989
- text
5684
+ text: view.history_text
5990
5685
  });
5991
- for (const tc of tool_calls) parts.push({
5686
+ for (const tc of kept) parts.push(tool_call_part(tc));
5687
+ return {
5688
+ role: "assistant",
5689
+ content: parts
5690
+ };
5691
+ }
5692
+ /**
5693
+ * A raw call as a history part, carrying no provider data.
5694
+ */
5695
+ function tool_call_part(tc) {
5696
+ return {
5992
5697
  type: "tool_call",
5993
5698
  id: tc.id,
5994
5699
  name: tc.name,
5995
5700
  input: tc.input
5996
- });
5997
- return {
5998
- role: "assistant",
5999
- content: parts
6000
5701
  };
6001
5702
  }
6002
5703
  /**
6003
- * Derive the step's cost from pricing tables and record it.
6004
- *
6005
- * Missing pricing for a paid provider emits `pricing_missing` (deduped per
6006
- * call) and yields no breakdown; free providers skip the emit entirely.
5704
+ * A turn's own parts for history. A call the per-step clamp dropped leaves,
5705
+ * as it leaves the plain message, while everything around it stays.
6007
5706
  */
6008
- function compute_and_record_cost(config, step_index, usage, step_span) {
5707
+ function history_parts(content, view, kept) {
5708
+ if (view.outcome !== void 0) return salvaged_parts(content, view.history_text, kept);
5709
+ const kept_ids = new Set(kept.map((tc) => tc.id));
5710
+ return content.filter((part) => part.type !== "tool_call" || kept_ids.has(part.id));
5711
+ }
5712
+ /**
5713
+ * A salvaged turn's parts for history: the stripped text replaces the text
5714
+ * parts at the first one's place, and the salvaged calls follow the last
5715
+ * part. Salvage runs only on a turn with no native calls, so the provider
5716
+ * gave its calls no place of their own. The replacement carries no provider
5717
+ * data, since whatever the provider attached belonged to the text salvage
5718
+ * rewrote.
5719
+ */
5720
+ function salvaged_parts(content, stripped_text, kept) {
5721
+ const parts = [];
5722
+ let text_placed = false;
5723
+ for (const part of content) if (part.type !== "text") parts.push(part);
5724
+ else if (!text_placed) {
5725
+ text_placed = true;
5726
+ if (stripped_text.length > 0) parts.push({
5727
+ type: "text",
5728
+ text: stripped_text
5729
+ });
5730
+ }
5731
+ for (const tc of kept) parts.push(tool_call_part(tc));
5732
+ return parts;
5733
+ }
5734
+ /**
5735
+ * Derive the step's cost and record it.
5736
+ *
5737
+ * A cost the provider reported wins over any pricing row, since it is what
5738
+ * was charged: it is split by token weight and recorded as
5739
+ * `provider_reported`, and the pricing lookup never runs, so no
5740
+ * `pricing_missing` either. Otherwise the cost comes from the pricing tables,
5741
+ * where missing pricing for a paid provider emits `pricing_missing` (deduped
5742
+ * per call) and yields no breakdown; free providers skip the emit entirely.
5743
+ */
5744
+ function compute_and_record_cost(config, step_index, turn, step_span) {
5745
+ if (turn.reported_cost_usd !== void 0) {
5746
+ const reported = decompose_total_cost(turn.reported_cost_usd, turn.usage);
5747
+ record_cost(config.trajectory, step_index, reported, "provider_reported", step_span);
5748
+ return reported;
5749
+ }
6009
5750
  const pricing = config.resolve_pricing();
6010
5751
  if (pricing === void 0 && !FREE_PROVIDERS.has(config.provider)) {
6011
5752
  config.pricing_dedup.emit(config.provider, config.model_id);
6012
5753
  return;
6013
5754
  }
6014
- const breakdown = compute_cost(usage, pricing, config.provider);
5755
+ const breakdown = compute_cost(turn.usage, pricing, config.provider);
6015
5756
  if (breakdown !== void 0) record_cost(config.trajectory, step_index, breakdown, "engine_derived", step_span);
6016
5757
  return breakdown;
6017
5758
  }
@@ -6480,7 +6221,7 @@ function push_step_record(config, acc, step_index, turn, tool_calls, finish_reas
6480
6221
  usage: turn.usage,
6481
6222
  finish_reason
6482
6223
  };
6483
- const breakdown = compute_and_record_cost(config, step_index, turn.usage, step_span);
6224
+ const breakdown = compute_and_record_cost(config, step_index, turn, step_span);
6484
6225
  if (breakdown !== void 0) record.cost = breakdown;
6485
6226
  if (turn.timing !== void 0) record.timing = turn.timing;
6486
6227
  if (turn.provider_reported !== void 0) record.provider_reported = turn.provider_reported;
@@ -6502,7 +6243,7 @@ async function run_loop_step(config, tool_map, acc, step_index) {
6502
6243
  const turn = await invoke_turn(config, step_index, step_span);
6503
6244
  const view = await salvage_turn(config, turn, tool_map, step_index);
6504
6245
  const { kept, dropped } = clamp_calls(config, view.calls, step_index);
6505
- config.messages.push(build_assistant_message(view.history_text, kept));
6246
+ config.messages.push(build_assistant_message(turn, view, kept));
6506
6247
  if (kept.length === 0) {
6507
6248
  push_step_record(config, acc, step_index, turn, [], turn.finish_reason, step_span);
6508
6249
  return loop_result(acc, turn.text, turn.finish_reason, false);
@@ -6751,7 +6492,10 @@ function retry_turn(call_once, args, first_chunk_at, retry_policy, classify, tur
6751
6492
  reason: args.abort.reason,
6752
6493
  step_index: args.step_index
6753
6494
  });
6754
- if (has_streamed()) throw new provider_error(`stream interrupted: ${err instanceof Error ? err.message : String(err)}`, { cause_kind: "unknown" });
6495
+ if (has_streamed()) {
6496
+ const message = err instanceof Error ? err.message : String(err);
6497
+ throw new provider_error(`stream interrupted: ${message}`, { cause_kind: "unknown" });
6498
+ }
6755
6499
  if (deadline.timed_out()) throw new turn_timeout_error(turn_timeout_ms ?? 0, args.step_index);
6756
6500
  if (err instanceof aborted_error) throw err;
6757
6501
  throw classify(err);
@@ -6964,7 +6708,7 @@ async function build_ai_sdk_transport(cfg) {
6964
6708
  const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
6965
6709
  let ai_sdk_invoke_mod;
6966
6710
  try {
6967
- ai_sdk_invoke_mod = await import("./invoke-Beef3R3X.js");
6711
+ ai_sdk_invoke_mod = await import("./invoke-px31fffH.js");
6968
6712
  } catch (err) {
6969
6713
  throw missing_peer_error("ai", err);
6970
6714
  }
@@ -6980,6 +6724,7 @@ async function build_ai_sdk_transport(cfg) {
6980
6724
  temperature: opts.temperature,
6981
6725
  max_tokens: opts.max_tokens,
6982
6726
  top_p: opts.top_p,
6727
+ reasoning: effort_translation.reasoning,
6983
6728
  telemetry: cfg.telemetry
6984
6729
  }),
6985
6730
  retry_policy: cfg.retry_policy,
@@ -7025,9 +6770,9 @@ function build_loop_config(cfg) {
7025
6770
  const resolve_pricing = () => {
7026
6771
  return cfg.pricing[pricing_key(target.provider, target.model_id)];
7027
6772
  };
7028
- const dispatch_chunk = cfg.on_chunk_provided ? async (chunk) => {
6773
+ const dispatch_chunk = cfg.on_chunk_provided ? (async (chunk) => {
7029
6774
  await cfg.dispatcher.dispatch(chunk);
7030
- } : void 0;
6775
+ }) : void 0;
7031
6776
  return {
7032
6777
  invoke_once: cfg.invoke_once,
7033
6778
  messages: cfg.messages,
@@ -7528,6 +7273,6 @@ function create_engine(config) {
7528
7273
  };
7529
7274
  }
7530
7275
  //#endregion
7531
- export { schema_validation_error as _, claude_cli_error as a, incomplete_generation_error as c, provider_auth_error as d, provider_capability_error as f, rate_limit_error as g, provider_required_error as h, load_optional_peer as i, model_required_error as l, provider_not_configured_error as m, split_leading_system_run as n, engine_config_error as o, provider_error as p, default_normalize_usage as r, engine_disposed_error as s, create_engine as t, on_chunk_error as u, tool_approval_denied_error as v, tool_error as y };
7276
+ export { create_engine as t };
7532
7277
 
7533
- //# sourceMappingURL=engine-BOGuc0X2.js.map
7278
+ //# sourceMappingURL=engine-ha9xmgwE.js.map