fascicle 0.16.5 → 0.17.1

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 (41) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +3 -2
  3. package/dist/AGENTS.md +69 -18
  4. package/dist/acp.d.ts +1 -1
  5. package/dist/acp.js +7 -2
  6. package/dist/acp.js.map +1 -1
  7. package/dist/agents.d.ts +1 -1
  8. package/dist/{engine-B6rIIM4J.js → engine-Bde18P7H.js} +1163 -236
  9. package/dist/engine-Bde18P7H.js.map +1 -0
  10. package/dist/index.d.ts +114 -16
  11. package/dist/index.js +226 -16
  12. package/dist/index.js.map +1 -1
  13. package/dist/{invoke-LbzZLZFw.js → invoke-BbJLQcLl.js} +62 -106
  14. package/dist/invoke-BbJLQcLl.js.map +1 -0
  15. package/dist/{leading_system-WhW76tRn.js → leading_system-DmNxhvzL.js} +45 -16
  16. package/dist/leading_system-DmNxhvzL.js.map +1 -0
  17. package/dist/mcp.d.ts +2 -1
  18. package/dist/mcp.js +6 -3
  19. package/dist/mcp.js.map +1 -1
  20. package/dist/otel.js +48 -3
  21. package/dist/otel.js.map +1 -1
  22. package/dist/{search-CpCJYMUm.js → search-DjhdMsJ8.js} +4 -1
  23. package/dist/search-DjhdMsJ8.js.map +1 -0
  24. package/dist/search_worker.js +1 -1
  25. package/dist/testing.d.ts +77 -2
  26. package/dist/testing.js +202 -19
  27. package/dist/testing.js.map +1 -1
  28. package/dist/{types-Sw0oHW-_.d.ts → types-C7pO0jLq.d.ts} +218 -21
  29. package/dist/viewer-app/assets/index-9eVv6Mar.css +1 -0
  30. package/dist/viewer-app/assets/index-BkS2ZQu0.js +4 -0
  31. package/dist/viewer-app/index.html +2 -2
  32. package/dist/viewer.d.ts +64 -10
  33. package/dist/viewer.js +265 -64
  34. package/dist/viewer.js.map +1 -1
  35. package/package.json +1 -1
  36. package/dist/engine-B6rIIM4J.js.map +0 -1
  37. package/dist/invoke-LbzZLZFw.js.map +0 -1
  38. package/dist/leading_system-WhW76tRn.js.map +0 -1
  39. package/dist/search-CpCJYMUm.js.map +0 -1
  40. package/dist/viewer-app/assets/index-Cev83tcH.css +0 -1
  41. package/dist/viewer-app/assets/index-D8UmFVfR.js +0 -2
@@ -1,6 +1,6 @@
1
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-BOFcG3tp.js";
2
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-WhW76tRn.js";
3
+ import { A as tool_approval_denied_error, C as provider_auth_error, D as provider_required_error, E as provider_not_configured_error, M as turn_timeout_error, O as rate_limit_error, S as on_chunk_error, T as provider_error, _ as claude_cli_error, b as incomplete_generation_error, c as parse_tool_call_arguments, d as default_normalize_usage, f as load_optional_peer, g as translate_effort_to_sdk_reasoning, h as resolve_transport, i as invoke_http_turn, j as tool_error, k as schema_validation_error, l as read_dialect_init, m as normalize_local_usage, n as consume_framed_stream, o as map_messages_by_role, p as missing_peer_error, r as create_line_splitter, s as network_failure, t as split_leading_system_run, u as split_assistant_parts, v as engine_config_error, w as provider_capability_error, x as model_required_error, y as engine_disposed_error } from "./leading_system-DmNxhvzL.js";
4
4
  import { join } from "node:path";
5
5
  import { mkdtempSync, writeFileSync } from "node:fs";
6
6
  import { tmpdir } from "node:os";
@@ -341,14 +341,16 @@ function compute_cost(usage, pricing, provider) {
341
341
  if (pricing === void 0) return missing_pricing_breakdown(provider);
342
342
  const cached = usage.cached_input_tokens ?? 0;
343
343
  const cache_write = usage.cache_write_tokens ?? 0;
344
+ const write_1h = Math.min(cache_write, usage.cache_write_1h_tokens ?? 0);
344
345
  const reasoning = usage.reasoning_tokens ?? 0;
345
346
  const fresh_input = Math.max(0, usage.input_tokens - cached - cache_write);
346
347
  const cached_rate = pricing.cached_input_per_million ?? pricing.input_per_million;
347
348
  const cache_write_rate = pricing.cache_write_per_million ?? pricing.input_per_million;
349
+ const cache_write_1h_rate = pricing.cache_write_1h_per_million ?? pricing.input_per_million * 2;
348
350
  const reasoning_rate = pricing.reasoning_per_million ?? pricing.output_per_million;
349
351
  const input_usd_raw = fresh_input * pricing.input_per_million / 1e6;
350
352
  const cached_usd_raw = cached * cached_rate / 1e6;
351
- const cache_write_usd_raw = cache_write * cache_write_rate / 1e6;
353
+ const cache_write_usd_raw = ((cache_write - write_1h) * cache_write_rate + write_1h * cache_write_1h_rate) / 1e6;
352
354
  const reasoning_usd_raw = reasoning * reasoning_rate / 1e6;
353
355
  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);
354
356
  const breakdown = {
@@ -369,7 +371,8 @@ function compute_cost(usage, pricing, provider) {
369
371
  *
370
372
  * Cache-read and cache-write tokens are cheaper and pricier respectively
371
373
  * than base input tokens, so they're weighted by `CACHE_READ_MULTIPLIER`
372
- * and `CACHE_WRITE_MULTIPLIER` before the total is distributed
374
+ * and `CACHE_WRITE_MULTIPLIER` (`CACHE_WRITE_1H_MULTIPLIER` for the
375
+ * one-hour part of the writes) before the total is distributed
373
376
  * proportionally across all weighted components. The total is what the
374
377
  * provider charged and the split is estimated, so the breakdown keeps
375
378
  * `is_estimate: true`. Returns an all-zero estimate when there's no usage or
@@ -380,10 +383,11 @@ function decompose_total_cost(total_cost_usd, usage) {
380
383
  const output = Math.max(0, usage.output_tokens);
381
384
  const cached = Math.max(0, usage.cached_input_tokens ?? 0);
382
385
  const cache_write = Math.max(0, usage.cache_write_tokens ?? 0);
386
+ const write_1h = Math.min(cache_write, Math.max(0, usage.cache_write_1h_tokens ?? 0));
383
387
  const input_weight = Math.max(0, input - cached - cache_write);
384
388
  const output_weight = output;
385
389
  const cached_weight = cached * CACHE_READ_MULTIPLIER;
386
- const cache_write_weight = cache_write * CACHE_WRITE_MULTIPLIER;
390
+ const cache_write_weight = (cache_write - write_1h) * CACHE_WRITE_MULTIPLIER + write_1h * 2;
387
391
  const total_weight = input_weight + output_weight + cached_weight + cache_write_weight;
388
392
  if (total_weight === 0 || total_cost_usd === 0) return {
389
393
  total_usd: total_cost_usd,
@@ -493,20 +497,36 @@ function build_exhaustion_error(retryable, attempt, last_rate_limit_after) {
493
497
  return build_provider_error(retryable, attempt);
494
498
  }
495
499
  /**
496
- * Compute the pre-retry backoff for `attempt`. A rate-limit `Retry-After`
497
- * outranks the local cap (`Math.max`), so a server instruction longer than
498
- * `max_delay_ms` is honored; the honored value is returned so the caller can
499
- * carry it onto a later failure that omits its own.
500
+ * The server's `Retry-After`, in milliseconds, that a 429 or a 5xx carried,
501
+ * or `undefined` for a failure that carries none.
502
+ */
503
+ function server_retry_after(retryable) {
504
+ return retryable.kind === "rate_limit" || retryable.kind === "provider_5xx" ? retryable.retry_after_ms : void 0;
505
+ }
506
+ /**
507
+ * Compute the pre-retry backoff for `attempt`. A server's `Retry-After`, on a
508
+ * 429 or a 5xx, outranks the local cap (`Math.max`), so a server instruction
509
+ * longer than `max_delay_ms` is honored; the honored value is returned so the
510
+ * caller can carry a rate limit's onto a later failure that omits its own.
500
511
  */
501
512
  function next_backoff(retryable, backoff_policy, attempt) {
502
513
  const delay = compute_backoff(backoff_policy, attempt - 1);
503
- if (retryable.kind === "rate_limit" && retryable.retry_after_ms !== void 0) return {
504
- delay: Math.max(delay, retryable.retry_after_ms),
505
- retry_after_ms: retryable.retry_after_ms
514
+ const retry_after_ms = server_retry_after(retryable);
515
+ if (retry_after_ms !== void 0) return {
516
+ delay: Math.max(delay, retry_after_ms),
517
+ retry_after_ms
506
518
  };
507
519
  return { delay };
508
520
  }
509
521
  /**
522
+ * The `Retry-After` that a rate limit's exhaustion error falls back on: this
523
+ * failure's own when it is a rate limit that carried one, and otherwise the
524
+ * last one a rate limit carried. A 5xx's value never carries onto it.
525
+ */
526
+ function carry_rate_limit_after(retryable, backoff, last) {
527
+ return retryable.kind === "rate_limit" ? backoff.retry_after_ms ?? last : last;
528
+ }
529
+ /**
510
530
  * Assemble the `on_retry` payload from the classified failure and the backoff
511
531
  * the retry will actually wait.
512
532
  */
@@ -519,7 +539,8 @@ function build_attempt_info(retryable, attempt, delay_ms) {
519
539
  if (retryable.kind === "rate_limit" || retryable.kind === "provider_5xx") {
520
540
  if (retryable.status !== void 0) info.status = retryable.status;
521
541
  }
522
- if (retryable.kind === "rate_limit" && retryable.retry_after_ms !== void 0) info.retry_after_ms = retryable.retry_after_ms;
542
+ const retry_after_ms = server_retry_after(retryable);
543
+ if (retry_after_ms !== void 0) info.retry_after_ms = retry_after_ms;
523
544
  return info;
524
545
  }
525
546
  /**
@@ -555,7 +576,7 @@ async function retry_with_policy(fn, policy = DEFAULT_RETRY, abort, on_retry) {
555
576
  attempt += 1;
556
577
  if (attempt >= policy.max_attempts) throw build_exhaustion_error(retryable, attempt, last_rate_limit_after);
557
578
  const backoff = next_backoff(retryable, backoff_policy, attempt);
558
- last_rate_limit_after = backoff.retry_after_ms ?? last_rate_limit_after;
579
+ last_rate_limit_after = carry_rate_limit_after(retryable, backoff, last_rate_limit_after);
559
580
  on_retry?.(build_attempt_info(retryable, attempt, backoff.delay));
560
581
  await wait_with_abort(backoff.delay, signal, to_abort_error);
561
582
  }
@@ -576,31 +597,41 @@ function read_number$1(err, key) {
576
597
  return typeof value === "number" ? value : void 0;
577
598
  }
578
599
  /**
579
- * Assemble the `rate_limit` retryable shape, copying the optional
580
- * `retry_after_ms`, `status`, and `message` fields when each is present.
600
+ * Read the fields that a retryable status carries (`retry_after_ms`,
601
+ * `status`, and `message`), keeping each only when it is present.
581
602
  */
582
- function build_retryable_rate_limit(err) {
583
- const base = { kind: "rate_limit" };
603
+ function read_status_fields(err) {
604
+ const fields = {};
584
605
  const retry_after = read_number$1(err, "retry_after_ms");
585
- if (retry_after !== void 0) base.retry_after_ms = retry_after;
606
+ if (retry_after !== void 0) fields.retry_after_ms = retry_after;
586
607
  const status = read_number$1(err, "status");
587
- if (status !== void 0) base.status = status;
608
+ if (status !== void 0) fields.status = status;
588
609
  const message = read_string$1(err, "message");
589
- if (message !== void 0) base.message = message;
590
- return base;
610
+ if (message !== void 0) fields.message = message;
611
+ return fields;
612
+ }
613
+ /**
614
+ * Assemble the `rate_limit` retryable shape, copying the optional
615
+ * `retry_after_ms`, `status`, and `message` fields when each is present.
616
+ */
617
+ function build_retryable_rate_limit(err) {
618
+ return {
619
+ kind: "rate_limit",
620
+ ...read_status_fields(err)
621
+ };
591
622
  }
592
623
  /**
593
- * Assemble the `provider_5xx` retryable shape, copying the optional `status`,
594
- * `body`, and `message` fields when each is present.
624
+ * Assemble the `provider_5xx` retryable shape, copying the optional
625
+ * `retry_after_ms`, `status`, `body`, and `message` fields when each is
626
+ * present.
595
627
  */
596
628
  function build_retryable_5xx(err) {
597
- const base = { kind: "provider_5xx" };
598
- const status = read_number$1(err, "status");
599
- if (status !== void 0) base.status = status;
629
+ const base = {
630
+ kind: "provider_5xx",
631
+ ...read_status_fields(err)
632
+ };
600
633
  const body = read_string$1(err, "body");
601
634
  if (body !== void 0) base.body = body;
602
- const message = read_string$1(err, "message");
603
- if (message !== void 0) base.message = message;
604
635
  return base;
605
636
  }
606
637
  /**
@@ -758,22 +789,48 @@ function reasoning_blocks(part) {
758
789
  }] : [];
759
790
  }
760
791
  /**
792
+ * Map a text part back to its block, writing the citations it came with (under
793
+ * `anthropic`) back onto it, since a multi-turn web search has to send each
794
+ * citation's `encrypted_index` back. A blank text maps to no block.
795
+ */
796
+ function text_part_blocks(part) {
797
+ const citations = part.provider_options?.["anthropic"]?.["citations"];
798
+ const blocks = text_blocks(part.text);
799
+ return citations === void 0 ? blocks : blocks.map((block) => ({
800
+ ...block,
801
+ citations
802
+ }));
803
+ }
804
+ /**
805
+ * Map an opaque part back to the block it came from, when it carries one
806
+ * under `anthropic`; another provider's opaque part maps to no block.
807
+ */
808
+ function opaque_blocks(part) {
809
+ const block = part.provider_options["anthropic"]?.["block"];
810
+ return is_record$1(block) && typeof block["type"] === "string" ? [{
811
+ ...block,
812
+ type: block["type"]
813
+ }] : [];
814
+ }
815
+ /**
761
816
  * 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.
817
+ * parts become text blocks (empties dropped, citations kept), tool-call parts
818
+ * become tool_use blocks, Anthropic's own reasoning parts become the thinking
819
+ * and redacted_thinking blocks they came from, and its opaque parts become
820
+ * the blocks they carry. Order is kept because Anthropic refuses a tool-use
821
+ * turn whose thinking blocks moved.
766
822
  */
767
823
  function to_assistant_blocks(content) {
768
824
  if (typeof content === "string") return text_blocks(content);
769
825
  const blocks = [];
770
- for (const part of content) if (part.type === "text") blocks.push(...text_blocks(part.text));
826
+ for (const part of content) if (part.type === "text") blocks.push(...text_part_blocks(part));
771
827
  else if (part.type === "tool_call") blocks.push({
772
828
  type: "tool_use",
773
829
  id: part.id,
774
830
  name: part.name,
775
831
  input: part.input
776
832
  });
833
+ else if (part.type === "opaque") blocks.push(...opaque_blocks(part));
777
834
  else blocks.push(...reasoning_blocks(part));
778
835
  return blocks;
779
836
  }
@@ -813,7 +870,8 @@ function to_anthropic_messages(messages) {
813
870
  case "tool": push_blocks("user", [{
814
871
  type: "tool_result",
815
872
  tool_use_id: message.tool_call_id,
816
- content: message.content
873
+ content: message.content,
874
+ ...message.is_error === true ? { is_error: true } : {}
817
875
  }]);
818
876
  }
819
877
  return {
@@ -859,6 +917,10 @@ function build_messages_body(req) {
859
917
  };
860
918
  body["output_config"] = { effort: req.effort };
861
919
  }
920
+ if (req.cache !== void 0) body["cache_control"] = req.cache === "1h" ? {
921
+ type: "ephemeral",
922
+ ttl: "1h"
923
+ } : { type: "ephemeral" };
862
924
  const passthrough = req.provider_options?.["anthropic"];
863
925
  return passthrough === void 0 ? body : {
864
926
  ...body,
@@ -902,9 +964,21 @@ function map_anthropic_usage(raw) {
902
964
  };
903
965
  if (cache_read !== void 0) totals.cached_input_tokens = cache_read;
904
966
  if (cache_write !== void 0) totals.cache_write_tokens = cache_write;
967
+ const cache_write_1h = read_one_hour_writes(Reflect.get(raw, "cache_creation"));
968
+ if (cache_write_1h !== void 0) totals.cache_write_1h_tokens = cache_write_1h;
905
969
  return totals;
906
970
  }
907
971
  /**
972
+ * Read the one-hour part of a cache write from Anthropic's `cache_creation`
973
+ * split (`ephemeral_1h_input_tokens`), or undefined when the usage carries
974
+ * no split.
975
+ */
976
+ function read_one_hour_writes(cache_creation) {
977
+ if (cache_creation === null || typeof cache_creation !== "object") return void 0;
978
+ const value = Reflect.get(cache_creation, "ephemeral_1h_input_tokens");
979
+ return typeof value === "number" ? value : void 0;
980
+ }
981
+ /**
908
982
  * Read the id and name a tool_use block must carry, throwing a
909
983
  * provider_error naming `where` the malformed block appeared.
910
984
  */
@@ -945,21 +1019,25 @@ function read_reasoning_block(block, type) {
945
1019
  }
946
1020
  /**
947
1021
  * 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
1022
+ * their string, keeping any citations under `anthropic`; tool_use blocks
1023
+ * contribute a call whose id and name are validated by read_tool_use_identity;
1024
+ * thinking and redacted_thinking blocks become reasoning parts; and every
1025
+ * other block type becomes an opaque part that carries the block as it came,
1026
+ * keyed `anthropic.block`. Every kept block also lands in `parts`, in block
951
1027
  * order, for TurnResult.content.
952
1028
  */
953
1029
  function push_content_block(block, acc) {
954
- if (block === null || typeof block !== "object") return;
955
- const type = Reflect.get(block, "type");
1030
+ if (!is_record$1(block)) return;
1031
+ const type = block["type"];
956
1032
  if (type === "text") {
957
- const text = Reflect.get(block, "text");
1033
+ const text = block["text"];
1034
+ const citations = block["citations"];
958
1035
  if (typeof text === "string") {
959
1036
  acc.text_parts.push(text);
960
1037
  acc.parts.push({
961
1038
  type: "text",
962
- text
1039
+ text,
1040
+ ...Array.isArray(citations) ? { provider_options: { anthropic: { citations } } } : {}
963
1041
  });
964
1042
  }
965
1043
  return;
@@ -980,6 +1058,10 @@ function push_content_block(block, acc) {
980
1058
  }
981
1059
  const reasoning = read_reasoning_block(block, type);
982
1060
  if (reasoning !== void 0) acc.parts.push(reasoning);
1061
+ else if (typeof type === "string") acc.parts.push({
1062
+ type: "opaque",
1063
+ provider_options: { anthropic: { block } }
1064
+ });
983
1065
  }
984
1066
  /**
985
1067
  * Walk a response payload's `content` array into the flat text and tool_call
@@ -997,21 +1079,32 @@ function collect_content_blocks(content) {
997
1079
  return acc;
998
1080
  }
999
1081
  /**
1082
+ * True when a turn's parts carry something its provider wants back: a
1083
+ * reasoning part, an opaque block, or a text part with citations.
1084
+ */
1085
+ function carries_provider_data(parts) {
1086
+ return parts.some((part) => part.type === "reasoning" || part.type === "opaque" || part.type === "text" && part.provider_options !== void 0);
1087
+ }
1088
+ /**
1000
1089
  * Parse a non-stream Messages-API response payload into a TurnResult. The
1001
1090
  * stream aggregator rebuilds this same payload shape and feeds it here too,
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.
1091
+ * so both paths share one parser. A turn that carries provider data (see
1092
+ * carries_provider_data) gets `content`, every block in order, and any other
1093
+ * turn stays as it was. A `pause_turn` stop sets `paused` beside its
1094
+ * `'stop'` finish, so the loop continues the turn.
1004
1095
  */
1005
1096
  function parse_messages_response(payload) {
1006
1097
  if (payload === null || typeof payload !== "object") throw new provider_error("anthropic native: response payload is not a JSON object");
1007
1098
  const { text_parts, tool_calls, parts } = collect_content_blocks(Reflect.get(payload, "content"));
1099
+ const stop_reason = Reflect.get(payload, "stop_reason");
1008
1100
  const result = {
1009
1101
  text: text_parts.join(""),
1010
1102
  tool_calls,
1011
- finish_reason: map_anthropic_stop_reason(Reflect.get(payload, "stop_reason")),
1012
- usage: map_anthropic_usage(Reflect.get(payload, "usage"))
1103
+ finish_reason: map_anthropic_stop_reason(stop_reason),
1104
+ usage: map_anthropic_usage(Reflect.get(payload, "usage")),
1105
+ ...stop_reason === "pause_turn" ? { paused: true } : {}
1013
1106
  };
1014
- return parts.some((part) => part.type === "reasoning") ? {
1107
+ return carries_provider_data(parts) ? {
1015
1108
  ...result,
1016
1109
  content: parts
1017
1110
  } : result;
@@ -1021,8 +1114,8 @@ function parse_messages_response(payload) {
1021
1114
  * overloaded_error is the SSE analog of a 529, api_error of a 500, and
1022
1115
  * rate_limit_error of a 429, so classify_provider_error sees the transients
1023
1116
  * it already retries. Anything else is a permanent provider_error. Whether a
1024
- * retry actually happens stays retry_turn's call: once chunks have streamed
1025
- * it refuses, exactly as on the ai_sdk path.
1117
+ * retry actually happens stays retry_turn's call: once chunks have reached an
1118
+ * on_chunk consumer it refuses, exactly as on the ai_sdk path.
1026
1119
  */
1027
1120
  function stream_event_error(event) {
1028
1121
  const error = Reflect.get(event, "error");
@@ -1069,9 +1162,12 @@ function parse_stream_event(data) {
1069
1162
  /**
1070
1163
  * Consume parsed Messages-stream events, dispatching StreamChunks as they
1071
1164
  * arrive and rebuilding the non-stream response payload as it goes: text
1072
- * deltas accumulate into synthetic text blocks, tool_use input arrives as raw
1165
+ * deltas accumulate into synthetic text blocks (with any citations, from the
1166
+ * start payload and each citations_delta), tool_use input arrives as raw
1073
1167
  * JSON text (input_json_delta) held per open block and parsed at
1074
- * content_block_stop. complete() feeds the synthetic payload through
1168
+ * content_block_stop, and a block of any other type starts from its start
1169
+ * payload, folds its own input_json_delta into `input` the same way, and
1170
+ * dispatches no chunk. complete() feeds the synthetic payload through
1075
1171
  * parse_messages_response, which is what makes the streamed TurnResult equal
1076
1172
  * the non-streamed one by construction. A stream that ends without
1077
1173
  * message_stop is truncated output, so complete() fails loud instead of
@@ -1080,6 +1176,7 @@ function parse_stream_event(data) {
1080
1176
  function create_stream_aggregator(step_index, dispatch) {
1081
1177
  const content = [];
1082
1178
  const open_text = /* @__PURE__ */ new Map();
1179
+ const open_opaque = /* @__PURE__ */ new Map();
1083
1180
  const open_thinking = /* @__PURE__ */ new Map();
1084
1181
  const open_tools = /* @__PURE__ */ new Map();
1085
1182
  const usage = {};
@@ -1088,8 +1185,10 @@ function create_stream_aggregator(step_index, dispatch) {
1088
1185
  const merge_usage = (raw) => {
1089
1186
  if (raw === null || typeof raw !== "object") return;
1090
1187
  for (const [key, value] of Object.entries(raw)) if (typeof value === "number") usage[key] = value;
1188
+ const split = Reflect.get(raw, "cache_creation");
1189
+ if (read_one_hour_writes(split) !== void 0) usage["cache_creation"] = split;
1091
1190
  };
1092
- const start_reasoning_block = (index, block, type) => {
1191
+ const start_other_block = (index, block, type) => {
1093
1192
  if (type === "thinking") {
1094
1193
  const initial = Reflect.get(block, "thinking");
1095
1194
  const synthetic = {
@@ -1103,17 +1202,27 @@ function create_stream_aggregator(step_index, dispatch) {
1103
1202
  type: "redacted_thinking",
1104
1203
  data: Reflect.get(block, "data")
1105
1204
  });
1205
+ else if (typeof type === "string") {
1206
+ const opaque = { ...block };
1207
+ content.push(opaque);
1208
+ open_opaque.set(index, {
1209
+ block: opaque,
1210
+ json: ""
1211
+ });
1212
+ }
1106
1213
  };
1107
1214
  const on_block_start = async (event) => {
1108
1215
  const index = read_block_index(event);
1109
1216
  const block = Reflect.get(event, "content_block");
1110
- if (block === null || typeof block !== "object") return;
1111
- const type = Reflect.get(block, "type");
1217
+ if (!is_record$1(block)) return;
1218
+ const type = block["type"];
1112
1219
  if (type === "text") {
1113
- const initial = Reflect.get(block, "text");
1220
+ const initial = block["text"];
1221
+ const citations = block["citations"];
1114
1222
  const synthetic = {
1115
1223
  type: "text",
1116
- text: typeof initial === "string" ? initial : ""
1224
+ text: typeof initial === "string" ? initial : "",
1225
+ ...Array.isArray(citations) ? { citations: [...citations] } : {}
1117
1226
  };
1118
1227
  content.push(synthetic);
1119
1228
  open_text.set(index, synthetic);
@@ -1140,7 +1249,7 @@ function create_stream_aggregator(step_index, dispatch) {
1140
1249
  });
1141
1250
  return;
1142
1251
  }
1143
- start_reasoning_block(index, block, type);
1252
+ start_other_block(index, block, type);
1144
1253
  };
1145
1254
  const on_thinking_delta = async (index, delta, type) => {
1146
1255
  const open = open_thinking.get(index);
@@ -1158,38 +1267,55 @@ function create_stream_aggregator(step_index, dispatch) {
1158
1267
  step_index
1159
1268
  });
1160
1269
  };
1270
+ const on_text_delta = async (index, delta, type) => {
1271
+ const open = open_text.get(index);
1272
+ if (open === void 0) return;
1273
+ if (type === "citations_delta") {
1274
+ open.citations = [...open.citations ?? [], Reflect.get(delta, "citation")];
1275
+ return;
1276
+ }
1277
+ const text = Reflect.get(delta, "text");
1278
+ if (typeof text !== "string") return;
1279
+ open.text += text;
1280
+ await dispatch({
1281
+ kind: "text",
1282
+ text,
1283
+ step_index
1284
+ });
1285
+ };
1286
+ const on_input_json_delta = async (index, delta) => {
1287
+ const partial = Reflect.get(delta, "partial_json");
1288
+ if (typeof partial !== "string") return;
1289
+ const opaque = open_opaque.get(index);
1290
+ if (opaque !== void 0) opaque.json += partial;
1291
+ const open = open_tools.get(index);
1292
+ if (open === void 0) return;
1293
+ open.json += partial;
1294
+ await dispatch({
1295
+ kind: "tool_call_input_delta",
1296
+ id: open.block.id,
1297
+ delta: partial,
1298
+ step_index
1299
+ });
1300
+ };
1161
1301
  const on_block_delta = async (event) => {
1162
1302
  const index = read_block_index(event);
1163
1303
  const delta = Reflect.get(event, "delta");
1164
1304
  if (delta === null || typeof delta !== "object") return;
1165
1305
  const type = Reflect.get(delta, "type");
1166
- if (type === "text_delta") {
1167
- const text = Reflect.get(delta, "text");
1168
- const open = open_text.get(index);
1169
- if (typeof text !== "string" || open === void 0) return;
1170
- open.text += text;
1171
- await dispatch({
1172
- kind: "text",
1173
- text,
1174
- step_index
1175
- });
1176
- return;
1177
- }
1178
- if (type === "thinking_delta" || type === "signature_delta") {
1179
- await on_thinking_delta(index, delta, type);
1180
- return;
1181
- }
1182
- if (type === "input_json_delta") {
1183
- const partial = Reflect.get(delta, "partial_json");
1184
- const open = open_tools.get(index);
1185
- if (typeof partial !== "string" || open === void 0) return;
1186
- open.json += partial;
1187
- await dispatch({
1188
- kind: "tool_call_input_delta",
1189
- id: open.block.id,
1190
- delta: partial,
1191
- step_index
1192
- });
1306
+ if (type === "text_delta" || type === "citations_delta") await on_text_delta(index, delta, type);
1307
+ else if (type === "thinking_delta" || type === "signature_delta") await on_thinking_delta(index, delta, type);
1308
+ else if (type === "input_json_delta") await on_input_json_delta(index, delta);
1309
+ };
1310
+ const stop_opaque_block = (index) => {
1311
+ const opaque = open_opaque.get(index);
1312
+ if (opaque === void 0) return;
1313
+ open_opaque.delete(index);
1314
+ if (opaque.json.length === 0) return;
1315
+ try {
1316
+ opaque.block["input"] = JSON.parse(opaque.json);
1317
+ } catch {
1318
+ throw new provider_error(`anthropic native: ${String(opaque.block["type"])} input is not valid JSON`);
1193
1319
  }
1194
1320
  };
1195
1321
  const on_block_stop = async (event) => {
@@ -1197,6 +1323,7 @@ function create_stream_aggregator(step_index, dispatch) {
1197
1323
  const open = open_tools.get(index);
1198
1324
  open_text.delete(index);
1199
1325
  open_thinking.delete(index);
1326
+ stop_opaque_block(index);
1200
1327
  if (open === void 0) return;
1201
1328
  open_tools.delete(index);
1202
1329
  if (open.json.length > 0) try {
@@ -1252,7 +1379,7 @@ function create_stream_aggregator(step_index, dispatch) {
1252
1379
  }
1253
1380
  },
1254
1381
  complete() {
1255
- if (!stopped) throw new provider_error("anthropic native: stream ended before message_stop; the result would be truncated");
1382
+ if (!stopped) throw network_failure("anthropic native: stream ended before message_stop; the result would be truncated");
1256
1383
  return parse_messages_response({
1257
1384
  content,
1258
1385
  stop_reason,
@@ -1288,6 +1415,7 @@ const create_anthropic_native_adapter = (init) => {
1288
1415
  return {
1289
1416
  kind: "native",
1290
1417
  name: "anthropic",
1418
+ prompt_cache: "with_tools",
1291
1419
  async invoke_turn(req) {
1292
1420
  return invoke_http_turn(req, "anthropic", {
1293
1421
  url: `${base_url}/messages`,
@@ -1299,7 +1427,8 @@ const create_anthropic_native_adapter = (init) => {
1299
1427
  build_body: () => build_messages_body(req)
1300
1428
  }, {
1301
1429
  stream: (response) => consume_sse_response$1(response, req),
1302
- parse: parse_messages_response
1430
+ parse: parse_messages_response,
1431
+ stream_media_type: "text/event-stream"
1303
1432
  });
1304
1433
  },
1305
1434
  supports: (capability) => SUPPORTED$10.has(capability)
@@ -1308,11 +1437,50 @@ const create_anthropic_native_adapter = (init) => {
1308
1437
  //#endregion
1309
1438
  //#region src/engine/providers/anthropic.ts
1310
1439
  /**
1311
- * Normalize Anthropic usage via the shared default mapper; the AI SDK already
1312
- * reports cache and reasoning tokens in the fields the default reads.
1313
- */
1314
- function normalize_anthropic_usage(raw) {
1315
- return default_normalize_usage(raw);
1440
+ * Normalize Anthropic usage via the shared default mapper, since the AI SDK
1441
+ * already reports cache and reasoning tokens in the fields the default reads.
1442
+ * The one-hour part of a cache write comes from the raw usage that
1443
+ * @ai-sdk/anthropic keeps in `providerMetadata.anthropic.usage`, and stays
1444
+ * out when the metadata carries no split.
1445
+ */
1446
+ function normalize_anthropic_usage(raw, provider_metadata) {
1447
+ const totals = default_normalize_usage(raw);
1448
+ const cache_write_1h = read_one_hour_writes(read_path(provider_metadata, [
1449
+ "anthropic",
1450
+ "usage",
1451
+ "cache_creation"
1452
+ ]));
1453
+ if (cache_write_1h !== void 0) totals.cache_write_1h_tokens = cache_write_1h;
1454
+ return totals;
1455
+ }
1456
+ /** Follow `keys` down through nested objects, or undefined where one is missing. */
1457
+ function read_path(value, keys) {
1458
+ let current = value;
1459
+ for (const key of keys) {
1460
+ if (current === null || typeof current !== "object") return void 0;
1461
+ current = Reflect.get(current, key);
1462
+ }
1463
+ return current;
1464
+ }
1465
+ /**
1466
+ * Turn a cache setting into the call-level `cacheControl` that
1467
+ * @ai-sdk/anthropic sends as the top-level `cache_control`. A caller's own
1468
+ * `cacheControl` under `anthropic` wins, so nothing is derived beside it. A
1469
+ * caller's `cache_control` there, which the package drops from the call
1470
+ * options, comes back as `cacheControl` instead, so that spelling still
1471
+ * caches. With neither, `'off'` adds nothing.
1472
+ */
1473
+ function translate_anthropic_cache(cache, provider_options) {
1474
+ const own = provider_options?.["anthropic"];
1475
+ const read = (key) => typeof own === "object" && own !== null ? Reflect.get(own, key) : void 0;
1476
+ if (read("cacheControl") !== void 0) return {};
1477
+ const raw = read("cache_control");
1478
+ if (raw !== void 0) return { anthropic: { cacheControl: raw } };
1479
+ if (cache === "off") return {};
1480
+ return { anthropic: { cacheControl: cache === "1h" ? {
1481
+ type: "ephemeral",
1482
+ ttl: "1h"
1483
+ } : { type: "ephemeral" } } };
1316
1484
  }
1317
1485
  const SUPPORTED$9 = /* @__PURE__ */ new Set([
1318
1486
  "text",
@@ -1350,7 +1518,9 @@ const create_anthropic_ai_sdk_adapter = (init) => {
1350
1518
  },
1351
1519
  translate_effort: translate_effort_to_sdk_reasoning,
1352
1520
  normalize_usage: normalize_anthropic_usage,
1353
- supports: (capability) => SUPPORTED$9.has(capability)
1521
+ supports: (capability) => SUPPORTED$9.has(capability),
1522
+ prompt_cache: "with_tools",
1523
+ translate_cache: translate_anthropic_cache
1354
1524
  };
1355
1525
  };
1356
1526
  //#endregion
@@ -1536,6 +1706,10 @@ function build_chat_completions_body(req, dialect) {
1536
1706
  body["stream"] = true;
1537
1707
  if (dialect.stream_include_usage) body["stream_options"] = { include_usage: true };
1538
1708
  }
1709
+ if (req.cache !== void 0) body["cache_control"] = req.cache === "1h" ? {
1710
+ type: "ephemeral",
1711
+ ttl: "1h"
1712
+ } : { type: "ephemeral" };
1539
1713
  const passthrough = req.provider_options?.[dialect.name];
1540
1714
  return passthrough === void 0 ? body : {
1541
1715
  ...body,
@@ -1718,8 +1892,9 @@ function with_reasoning_content(result, reasoning) {
1718
1892
  * OpenRouter, the HTTP status the failure would have had as a numeric
1719
1893
  * `code`. Carrying that code as `status` lets the engine's retry ladder treat
1720
1894
  * 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.
1895
+ * 5xx retries unless a chunk has reached an on_chunk consumer, and any error
1896
+ * after one has fails the turn as a stream interruption that names the
1897
+ * message.
1723
1898
  */
1724
1899
  function stream_error_frame(error, provider) {
1725
1900
  const message = error["message"];
@@ -1928,7 +2103,7 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
1928
2103
  if (choice !== null && choice !== void 0 && typeof choice === "object") await on_choice(choice);
1929
2104
  },
1930
2105
  complete() {
1931
- if (!done) throw new provider_error(`${dialect.name} native: stream ended before [DONE]; the result would be truncated`);
2106
+ if (!done) throw network_failure(`${dialect.name} native: stream ended before [DONE]; the result would be truncated`);
1932
2107
  const ordered = in_index_order();
1933
2108
  const message = {
1934
2109
  role: "assistant",
@@ -1994,6 +2169,7 @@ const create_openai_compatible_adapter = (dialect) => {
1994
2169
  return {
1995
2170
  kind: "native",
1996
2171
  name: dialect.name,
2172
+ ...dialect.prompt_cache !== void 0 ? { prompt_cache: dialect.prompt_cache } : {},
1997
2173
  async invoke_turn(req) {
1998
2174
  return invoke_http_turn(req, dialect.name, {
1999
2175
  url: `${base_url}/chat/completions`,
@@ -2001,7 +2177,8 @@ const create_openai_compatible_adapter = (dialect) => {
2001
2177
  build_body: () => build_chat_completions_body(req, dialect)
2002
2178
  }, {
2003
2179
  stream: (response) => consume_sse_response(response, req, dialect),
2004
- parse: (payload) => parse_chat_completion(payload, dialect)
2180
+ parse: (payload) => parse_chat_completion(payload, dialect),
2181
+ stream_media_type: "text/event-stream"
2005
2182
  });
2006
2183
  },
2007
2184
  supports: (capability) => SUPPORTED$8.has(capability)
@@ -2059,6 +2236,7 @@ const create_openai_native_adapter = (init) => {
2059
2236
  stream_include_usage: true,
2060
2237
  tolerant_usage: false,
2061
2238
  reports_cost: false,
2239
+ prompt_cache: "automatic",
2062
2240
  ...organization !== void 0 ? { extra_headers: { "OpenAI-Organization": organization } } : {}
2063
2241
  };
2064
2242
  return create_openai_compatible_adapter(dialect);
@@ -2084,7 +2262,8 @@ const create_openai_ai_sdk_adapter = (init) => {
2084
2262
  },
2085
2263
  translate_effort: translate_effort_to_sdk_reasoning,
2086
2264
  normalize_usage: normalize_openai_usage,
2087
- supports: (capability) => SUPPORTED$7.has(capability)
2265
+ supports: (capability) => SUPPORTED$7.has(capability),
2266
+ prompt_cache: "automatic"
2088
2267
  };
2089
2268
  };
2090
2269
  //#endregion
@@ -2122,7 +2301,8 @@ const create_google_adapter = (init) => {
2122
2301
  },
2123
2302
  translate_effort: translate_effort_to_sdk_reasoning,
2124
2303
  normalize_usage: normalize_google_usage,
2125
- supports: (capability) => SUPPORTED$6.has(capability)
2304
+ supports: (capability) => SUPPORTED$6.has(capability),
2305
+ prompt_cache: "automatic"
2126
2306
  };
2127
2307
  };
2128
2308
  //#endregion
@@ -2145,7 +2325,8 @@ function to_user_content(content) {
2145
2325
  * Map assistant message content to this wire's message shape: text parts
2146
2326
  * join into `content`, and tool-call parts become `tool_calls` entries with
2147
2327
  * structured (not JSON-string) arguments. Reasoning parts are never sent:
2148
- * this adapter maps no `thinking` field in either direction.
2328
+ * this adapter maps no `thinking` field onto a request, and the thinking it
2329
+ * reads off a response only streams.
2149
2330
  */
2150
2331
  function to_assistant_message(content) {
2151
2332
  if (typeof content === "string") return {
@@ -2327,7 +2508,9 @@ function create_ndjson_decoder() {
2327
2508
  * accumulates into the turn text, and `message.tool_calls` entries arrive
2328
2509
  * whole per frame, since arguments are complete objects on this wire, so
2329
2510
  * each call dispatches tool_call_start then tool_call_end with no input
2330
- * deltas between. The `done: true` frame carries done_reason and the eval
2511
+ * deltas between. `message.thinking` dispatches as reasoning ahead of the
2512
+ * frame's text and rebuilds into nothing, the way the non-stream parse reads
2513
+ * none of it. The `done: true` frame carries done_reason and the eval
2331
2514
  * counts and closes the turn (step_finish); frames after it are ignored. A
2332
2515
  * frame whose top-level `error` is a string is the daemon failing mid-stream
2333
2516
  * and throws with its message. A stream that ends without a done frame is
@@ -2347,6 +2530,12 @@ function create_ollama_stream_aggregator(step_index, dispatch) {
2347
2530
  };
2348
2531
  let reported;
2349
2532
  const on_message = async (message) => {
2533
+ const thinking = Reflect.get(message, "thinking");
2534
+ if (typeof thinking === "string" && thinking.length > 0) await dispatch({
2535
+ kind: "reasoning",
2536
+ text: thinking,
2537
+ step_index
2538
+ });
2350
2539
  const content = Reflect.get(message, "content");
2351
2540
  if (typeof content === "string" && content.length > 0) {
2352
2541
  text += content;
@@ -2403,7 +2592,7 @@ function create_ollama_stream_aggregator(step_index, dispatch) {
2403
2592
  }
2404
2593
  },
2405
2594
  complete() {
2406
- if (!done) throw new provider_error("ollama native: stream ended before its done frame; the result would be truncated");
2595
+ if (!done) throw network_failure("ollama native: stream ended before its done frame; the result would be truncated");
2407
2596
  const message = {
2408
2597
  role: "assistant",
2409
2598
  content: text
@@ -2459,7 +2648,8 @@ const create_ollama_native_adapter = (init) => {
2459
2648
  build_body: () => build_ollama_chat_body(req)
2460
2649
  }, {
2461
2650
  stream: (response) => consume_ndjson_response(response, req),
2462
- parse: (payload) => parse_ollama_chat(payload, req.step_index)
2651
+ parse: (payload) => parse_ollama_chat(payload, req.step_index),
2652
+ stream_media_type: "application/x-ndjson"
2463
2653
  });
2464
2654
  },
2465
2655
  supports: (capability) => SUPPORTED$5.has(capability)
@@ -2644,8 +2834,9 @@ const create_openrouter_adapter = (init) => {
2644
2834
  * core: Bearer auth, the optional `HTTP-Referer`/`X-Title` attribution
2645
2835
  * headers, the `max_tokens` token-limit field, the nested effort shape,
2646
2836
  * 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
2837
+ * the cost OpenRouter reports on every response, and caching only when the
2838
+ * call or the engine asks for it, since only Claude models take the field.
2839
+ * The empty-api_key guard rides on the core, which throws the same
2649
2840
  * `openrouter provider requires a non-empty api_key` engine_config_error.
2650
2841
  */
2651
2842
  const create_openrouter_native_adapter = (init) => {
@@ -2668,6 +2859,7 @@ const create_openrouter_native_adapter = (init) => {
2668
2859
  reports_cost: true,
2669
2860
  effort_shape: "nested",
2670
2861
  reasoning: "reasoning_details",
2862
+ prompt_cache: "opt_in",
2671
2863
  ...Object.keys(extra_headers).length > 0 ? { extra_headers } : {}
2672
2864
  };
2673
2865
  return create_openai_compatible_adapter(dialect);
@@ -4301,6 +4493,22 @@ function end_step_span(trajectory, span_id, meta) {
4301
4493
  trajectory.end_span(span_id, { ...meta });
4302
4494
  }
4303
4495
  /**
4496
+ * Record that a turn's request went out: its step index, the turn's
4497
+ * `engine.generate.step` span (`span_id`), the call's `engine.generate` span
4498
+ * (`call_span_id`), and, under capture, the `request` itself, which is the
4499
+ * whole request on a call's first turn and what changed on a later one.
4500
+ */
4501
+ function record_request_sent(trajectory, meta) {
4502
+ if (trajectory === void 0) return;
4503
+ trajectory.record({
4504
+ kind: "request_sent",
4505
+ step_index: meta.step_index,
4506
+ ...meta.span_id !== void 0 ? { span_id: meta.span_id } : {},
4507
+ ...meta.call_span_id !== void 0 ? { call_span_id: meta.call_span_id } : {},
4508
+ ...meta.request !== void 0 ? { request: meta.request } : {}
4509
+ });
4510
+ }
4511
+ /**
4304
4512
  * Record a provider response: finish reason, flat token counts, and the
4305
4513
  * engine-measured turn timing when the turn carried one.
4306
4514
  *
@@ -4309,9 +4517,10 @@ function end_step_span(trajectory, span_id, meta) {
4309
4517
  * `first_chunk_ms` directly, where a nested usage object would arrive as an
4310
4518
  * opaque JSON string. This is the one per-turn event a metrics dashboard
4311
4519
  * needs: tokens by direction, the cache and reasoning splits when the
4312
- * provider reports them, and the model-only latency window.
4520
+ * provider reports them, and the model-only latency window. Under capture it
4521
+ * also carries `content`, the parts the provider returned.
4313
4522
  */
4314
- function record_response_received(trajectory, step_index, turn, span_id) {
4523
+ function record_response_received(trajectory, step_index, turn, span_id, content) {
4315
4524
  if (trajectory === void 0) return;
4316
4525
  const { usage, timing } = turn;
4317
4526
  const event = {
@@ -4325,17 +4534,53 @@ function record_response_received(trajectory, step_index, turn, span_id) {
4325
4534
  if (usage.reasoning_tokens !== void 0) event["reasoning_tokens"] = usage.reasoning_tokens;
4326
4535
  if (usage.cached_input_tokens !== void 0) event["cached_input_tokens"] = usage.cached_input_tokens;
4327
4536
  if (usage.cache_write_tokens !== void 0) event["cache_write_tokens"] = usage.cache_write_tokens;
4537
+ if (usage.cache_write_1h_tokens !== void 0) event["cache_write_1h_tokens"] = usage.cache_write_1h_tokens;
4538
+ const ratio = cache_hit_ratio(usage);
4539
+ if (ratio !== void 0) event["cache_hit_ratio"] = ratio;
4328
4540
  if (timing !== void 0) {
4329
4541
  event["started_at"] = timing.started_at;
4330
4542
  event["duration_ms"] = timing.duration_ms;
4331
4543
  if (timing.first_chunk_ms !== void 0) event["first_chunk_ms"] = timing.first_chunk_ms;
4332
4544
  }
4545
+ if (content !== void 0) event["content"] = content;
4333
4546
  trajectory.record({
4334
4547
  kind: "response_received",
4335
4548
  ...event
4336
4549
  });
4337
4550
  }
4338
4551
  /**
4552
+ * The share of a turn's input that it read from the cache,
4553
+ * `cached_input_tokens / input_tokens` rounded to three decimals, since
4554
+ * `input_tokens` counts the cache reads and writes too. Undefined on a turn
4555
+ * with no cache traffic (reads plus writes of zero), because the providers
4556
+ * that cache report zero counts on a turn that didn't, and undefined when
4557
+ * the turn reported no input.
4558
+ */
4559
+ function cache_hit_ratio(usage) {
4560
+ const reads = usage.cached_input_tokens ?? 0;
4561
+ if (reads + (usage.cache_write_tokens ?? 0) <= 0 || usage.input_tokens <= 0) return void 0;
4562
+ return Math.round(reads / usage.input_tokens * 1e3) / 1e3;
4563
+ }
4564
+ /**
4565
+ * Record a `cache_miss`: a turn that read nothing from the cache right after
4566
+ * a turn of the same generate call that read or wrote some. It names how
4567
+ * many tokens the previous turn cached (`previous_cached_tokens`, its reads
4568
+ * plus its writes), this turn's input, and whether `prepare_step` replaced
4569
+ * this turn's messages, which is the commonest way to change the top of the
4570
+ * array.
4571
+ */
4572
+ function record_cache_miss(trajectory, meta) {
4573
+ if (trajectory === void 0) return;
4574
+ trajectory.record({
4575
+ kind: "cache_miss",
4576
+ step_index: meta.step_index,
4577
+ ...meta.span_id !== void 0 ? { span_id: meta.span_id } : {},
4578
+ previous_cached_tokens: meta.previous_cached_tokens,
4579
+ input_tokens: meta.input_tokens,
4580
+ after_prepare_step: meta.after_prepare_step
4581
+ });
4582
+ }
4583
+ /**
4339
4584
  * Record one failed-then-retried provider attempt as a `turn_retry` event:
4340
4585
  * the 1-based attempt count, the classified failure kind, the backoff this
4341
4586
  * retry waits, and the HTTP status / `Retry-After` when the failure carried
@@ -4480,6 +4725,66 @@ function create_pricing_missing_dedup(trajectory) {
4480
4725
  } };
4481
4726
  }
4482
4727
  /**
4728
+ * Record that a captured payload went unrecorded because it couldn't be
4729
+ * built, or because the engine's redactor threw or returned a thenable:
4730
+ * the turn's step index, its step span, and the payload's kind.
4731
+ */
4732
+ function record_capture_redaction_failed(trajectory, meta) {
4733
+ if (trajectory === void 0) return;
4734
+ trajectory.record({
4735
+ kind: "capture_redaction_failed",
4736
+ step_index: meta.step_index,
4737
+ ...meta.span_id !== void 0 ? { span_id: meta.span_id } : {},
4738
+ payload_kind: meta.payload_kind
4739
+ });
4740
+ }
4741
+ /**
4742
+ * Record the exact body a native adapter sent on one attempt, under capture
4743
+ * at `'wire'`: the turn's step index and step span, the provider, and the
4744
+ * `url` and `body` of what the redactor returned, each left out when it
4745
+ * dropped them. Headers are never recorded, since they carry the key.
4746
+ */
4747
+ function record_request_wire(trajectory, meta) {
4748
+ if (trajectory === void 0) return;
4749
+ const { recorded } = meta;
4750
+ trajectory.record({
4751
+ kind: "request_wire",
4752
+ step_index: meta.step_index,
4753
+ ...meta.span_id !== void 0 ? { span_id: meta.span_id } : {},
4754
+ provider: meta.provider,
4755
+ ..."url" in recorded ? { url: recorded["url"] } : {},
4756
+ ..."body" in recorded ? { body: recorded["body"] } : {}
4757
+ });
4758
+ }
4759
+ /**
4760
+ * Record that the provider paused a turn partway through its response
4761
+ * (Anthropic's `pause_turn`), which the loop continues with another turn.
4762
+ */
4763
+ function record_turn_paused(trajectory, step_index, span_id) {
4764
+ if (trajectory === void 0) return;
4765
+ trajectory.record({
4766
+ kind: "turn_paused",
4767
+ step_index,
4768
+ ...span_id !== void 0 ? { span_id } : {}
4769
+ });
4770
+ }
4771
+ /**
4772
+ * Record that the loop went on past a turn that hit its output limit before
4773
+ * it made a tool call (`truncated_turn_policy: 'continue'`). `output_tokens`
4774
+ * is what the turn generated, and `max_tokens` is the limit, when the call
4775
+ * set one.
4776
+ */
4777
+ function record_turn_truncated(trajectory, meta) {
4778
+ if (trajectory === void 0) return;
4779
+ trajectory.record({
4780
+ kind: "turn_truncated",
4781
+ step_index: meta.step_index,
4782
+ output_tokens: meta.output_tokens,
4783
+ ...meta.max_tokens !== void 0 ? { max_tokens: meta.max_tokens } : {},
4784
+ ...meta.span_id !== void 0 ? { span_id: meta.span_id } : {}
4785
+ });
4786
+ }
4787
+ /**
4483
4788
  * Record that a requested reasoning-effort setting was ignored because the
4484
4789
  * model does not support it.
4485
4790
  */
@@ -4550,18 +4855,20 @@ function record_tool_approval(trajectory, kind, meta) {
4550
4855
  });
4551
4856
  }
4552
4857
  /**
4553
- * Build the per-call dedup that emits `option_ignored` once per option key.
4858
+ * Build the per-call dedup that emits `option_ignored` once per option key,
4859
+ * with a `reason` field only when the emit passes one.
4554
4860
  */
4555
4861
  function create_option_ignored_dedup(trajectory) {
4556
4862
  const seen = /* @__PURE__ */ new Set();
4557
- return { emit(option, provider) {
4863
+ return { emit(option, provider, reason) {
4558
4864
  if (trajectory === void 0) return;
4559
4865
  if (seen.has(option)) return;
4560
4866
  seen.add(option);
4561
4867
  trajectory.record({
4562
4868
  kind: "option_ignored",
4563
4869
  option,
4564
- provider
4870
+ provider,
4871
+ ...reason !== void 0 ? { reason } : {}
4565
4872
  });
4566
4873
  } };
4567
4874
  }
@@ -4758,7 +5065,9 @@ async function run_cli(spawn_runtime, args) {
4758
5065
  /**
4759
5066
  * Emit `option_ignored` events for the loop-control options this provider
4760
5067
  * cannot honor: the CLI runs its own tool loop on the far side of the
4761
- * subprocess boundary.
5068
+ * subprocess boundary, and Claude Code caches its own prompts and builds its
5069
+ * own requests, so a per-call `cache` or `capture`, `'off'` included, is
5070
+ * ignored too.
4762
5071
  */
4763
5072
  function emit_ignored_options(opts, trajectory) {
4764
5073
  const option_ignored = create_option_ignored_dedup(trajectory);
@@ -4767,6 +5076,9 @@ function emit_ignored_options(opts, trajectory) {
4767
5076
  if (opts.on_tool_approval !== void 0) option_ignored.emit("on_tool_approval", PROVIDER_NAME);
4768
5077
  if (opts.tool_call_repair_attempts !== void 0) option_ignored.emit("tool_call_repair_attempts", PROVIDER_NAME);
4769
5078
  if (opts.max_tool_calls_per_step !== void 0) option_ignored.emit("max_tool_calls_per_step", PROVIDER_NAME);
5079
+ if (opts.truncated_turn_policy !== void 0) option_ignored.emit("truncated_turn_policy", PROVIDER_NAME);
5080
+ if (opts.cache !== void 0) option_ignored.emit("cache", PROVIDER_NAME, "the provider caches on its own");
5081
+ if (opts.capture !== void 0) option_ignored.emit("capture", PROVIDER_NAME, "Claude Code builds its own requests");
4770
5082
  }
4771
5083
  /**
4772
5084
  * Reject requests the CLI cannot express: multi-turn user history (one user
@@ -5108,6 +5420,42 @@ function get_provider_factory(name) {
5108
5420
  return factory;
5109
5421
  }
5110
5422
  //#endregion
5423
+ //#region src/engine/first_chunk_stamp.ts
5424
+ /**
5425
+ * Creates the stamp one turn's attempts share, cleared by `reset` as each
5426
+ * attempt starts, so a failed attempt's chunks never count for the one that
5427
+ * returns.
5428
+ */
5429
+ function create_first_chunk_stamp() {
5430
+ let at;
5431
+ let open_call;
5432
+ let deltas = 0;
5433
+ let whole = false;
5434
+ const observe_first = (chunk) => {
5435
+ at = Date.now();
5436
+ if (chunk.kind === "tool_call_end") whole = true;
5437
+ if (chunk.kind === "tool_call_start") open_call = chunk.id;
5438
+ };
5439
+ return {
5440
+ reset: () => {
5441
+ at = void 0;
5442
+ open_call = void 0;
5443
+ deltas = 0;
5444
+ whole = false;
5445
+ },
5446
+ observe: (chunk) => {
5447
+ if (at === void 0) {
5448
+ observe_first(chunk);
5449
+ return;
5450
+ }
5451
+ if (chunk.kind === "tool_call_input_delta" && chunk.id === open_call) deltas += 1;
5452
+ if (chunk.kind === "tool_call_end" && chunk.id === open_call) whole = deltas <= 1;
5453
+ },
5454
+ at: () => at,
5455
+ whole_tool_call: () => whole
5456
+ };
5457
+ }
5458
+ //#endregion
5111
5459
  //#region src/engine/merge_defaults.ts
5112
5460
  /**
5113
5461
  * Default-merging helpers for engine-level `EngineDefaults`.
@@ -5179,13 +5527,15 @@ function create_chunk_dispatcher(on_chunk) {
5179
5527
  const OPTIONAL_FIELDS = [
5180
5528
  "reasoning_tokens",
5181
5529
  "cached_input_tokens",
5182
- "cache_write_tokens"
5530
+ "cache_write_tokens",
5531
+ "cache_write_1h_tokens"
5183
5532
  ];
5184
5533
  /**
5185
5534
  * Sum token usage across every step of a generate call.
5186
5535
  *
5187
5536
  * `input_tokens` and `output_tokens` always total. The optional fields
5188
- * (`reasoning_tokens`, `cached_input_tokens`, `cache_write_tokens`) stay
5537
+ * (`reasoning_tokens`, `cached_input_tokens`, `cache_write_tokens`,
5538
+ * `cache_write_1h_tokens`) stay
5189
5539
  * absent on the result unless at least one step reported them; once
5190
5540
  * present, every step contributes to the sum, with steps that omit the
5191
5541
  * field counting as 0.
@@ -5196,12 +5546,14 @@ function sum_usage(steps) {
5196
5546
  const present = {
5197
5547
  reasoning_tokens: false,
5198
5548
  cached_input_tokens: false,
5199
- cache_write_tokens: false
5549
+ cache_write_tokens: false,
5550
+ cache_write_1h_tokens: false
5200
5551
  };
5201
5552
  const sums = {
5202
5553
  reasoning_tokens: 0,
5203
5554
  cached_input_tokens: 0,
5204
- cache_write_tokens: 0
5555
+ cache_write_tokens: 0,
5556
+ cache_write_1h_tokens: 0
5205
5557
  };
5206
5558
  for (const step of steps) {
5207
5559
  input_tokens += step.usage.input_tokens;
@@ -5633,11 +5985,15 @@ async function salvage_tool_calls(text, tool_map) {
5633
5985
  * Validated matches run the normal execute path and produce the same
5634
5986
  * records/events/chunks as native calls, marked salvaged. The budget is
5635
5987
  * a shared mutable holder so it spans schema-repair re-invocations.
5636
- * - max_tool_calls_per_step: calls beyond the cap are dropped for the step
5637
- * (the model can re-issue next turn), recorded with
5638
- * error: { message: 'dropped_max_tool_calls_per_step' }, and excluded
5639
- * from the assistant history message so providers that require a result
5640
- * per emitted call do not reject the next request.
5988
+ * - max_tool_calls_per_step: a call to a tool flagged read_only doesn't
5989
+ * count toward the cap. The first counted call past it, and every call
5990
+ * after that one, are dropped for the step, so what runs is a prefix of
5991
+ * what the model asked for. Dropped calls are recorded with
5992
+ * error: { message: 'dropped_max_tool_calls_per_step' } and excluded from
5993
+ * the assistant history message, so providers that require a result per
5994
+ * emitted call do not reject the next request. When the loop goes on,
5995
+ * one user message after the step's tool results names them, so the
5996
+ * model learns they didn't run and can make them again.
5641
5997
  * - ends_turn (Tool.ends_turn): a tool flagged terminal ends the loop when a
5642
5998
  * call to it executes SUCCESSFULLY. The call runs the normal execute path
5643
5999
  * (record, fed tool_result message, trajectory events + chunk), then the
@@ -5647,6 +6003,14 @@ async function salvage_tool_calls(text, tool_map) {
5647
6003
  * terminal call is exempt from the max_steps would_exceed_after skip (it
5648
6004
  * needs no follow-up turn), so a terminal finish wins over a coincident
5649
6005
  * max_steps cap (finish_reason 'stop', max_steps_reached false).
6006
+ * - truncated_turns (truncated_turn_policy 'continue'): a turn that
6007
+ * finishes 'length' with no calls, salvaged ones included, on a call
6008
+ * with tools and with a step left, doesn't end the loop. Its visible
6009
+ * text goes back as a plain assistant message (none when it showed no
6010
+ * text, and never its reasoning), then a user message that says it was
6011
+ * cut off. The turn counts toward max_steps, records turn_truncated, and
6012
+ * its StepRecord says truncated: true. Without the holder, or on the last
6013
+ * step, such a turn ends the loop with 'length' as before.
5650
6014
  *
5651
6015
  * The loop does not itself call the AI SDK. It invokes a supplied `invoke_once`
5652
6016
  * seam that returns a neutral TurnResult. generate.ts builds the real seam
@@ -5771,14 +6135,16 @@ async function dispatch_tool_result_chunk(dispatch_chunk, step_index, id, output
5771
6135
  }
5772
6136
  /**
5773
6137
  * Build the `role: 'tool'` message that feeds a tool's result back to the
5774
- * model, JSON-serializing non-string content.
6138
+ * model, JSON-serializing non-string content. A failed call's message carries
6139
+ * `is_error: true`; a success's carries no such key.
5775
6140
  */
5776
- function build_tool_result_message(tool_call_id, tool_name, content) {
6141
+ function build_tool_result_message(tool_call_id, tool_name, content, is_error = false) {
5777
6142
  return {
5778
6143
  role: "tool",
5779
6144
  tool_call_id,
5780
6145
  name: tool_name,
5781
- content: typeof content === "string" ? content : safe_json_stringify(content)
6146
+ content: typeof content === "string" ? content : safe_json_stringify(content),
6147
+ ...is_error ? { is_error: true } : {}
5782
6148
  };
5783
6149
  }
5784
6150
  /**
@@ -5906,8 +6272,8 @@ async function validate_tool_input(tool, input) {
5906
6272
  * config.messages unchanged. The replacement is ephemeral; it is fed ONLY to
5907
6273
  * invoke_once for this turn, never pushed onto config.messages, so the loop's
5908
6274
  * salvage/approval/ends_turn/schema-repair machinery keeps reading the real
5909
- * transcript. A step_prepared event (recorded inline, as request_sent is)
5910
- * makes the mid-loop mutation legible with the before/after message counts.
6275
+ * transcript. A step_prepared event makes the mid-loop mutation legible
6276
+ * with the before/after message counts.
5911
6277
  */
5912
6278
  async function apply_prepare_step(config, step_index) {
5913
6279
  if (config.prepare_step === void 0) return config.messages;
@@ -5926,10 +6292,12 @@ async function apply_prepare_step(config, step_index) {
5926
6292
  }
5927
6293
  /**
5928
6294
  * Assemble the loop's return value from the accumulated steps and records.
6295
+ * The text is the final turn's, after the texts of any paused turns that it
6296
+ * continued.
5929
6297
  */
5930
6298
  function loop_result(acc, text, finish_reason, max_steps_reached) {
5931
6299
  return {
5932
- text,
6300
+ text: [...acc.paused_texts, text].join(""),
5933
6301
  steps: acc.steps,
5934
6302
  tool_calls: acc.tool_calls,
5935
6303
  finish_reason,
@@ -5937,16 +6305,295 @@ function loop_result(acc, text, finish_reason, max_steps_reached) {
5937
6305
  };
5938
6306
  }
5939
6307
  /**
6308
+ * Continue a turn the provider paused without calls: send its assistant
6309
+ * message back as it is, with nothing after it, which is how a paused
6310
+ * response resumes, and ask for another turn, which counts toward
6311
+ * `max_steps`. A pause on the last allowed step ends the loop with
6312
+ * `'max_steps'` and the paused turn's text. Salvage and the clamp never see a
6313
+ * paused turn.
6314
+ */
6315
+ function continue_paused_turn(config, acc, step_index, step_span, turn) {
6316
+ const view = {
6317
+ calls: [],
6318
+ history_text: turn.text,
6319
+ formats: /* @__PURE__ */ new Map(),
6320
+ outcome: void 0
6321
+ };
6322
+ config.messages.push(build_assistant_message(turn, view, []));
6323
+ const at_cap = step_index + 1 >= config.max_steps;
6324
+ push_step_record(config, acc, step_index, turn, [], at_cap ? "max_steps" : turn.finish_reason, step_span);
6325
+ if (at_cap) return loop_result(acc, turn.text, "max_steps", true);
6326
+ acc.paused_texts.push(turn.text);
6327
+ }
6328
+ /**
6329
+ * The truncated-turn holder when the loop goes on past this turn, which made
6330
+ * no call, salvaged ones included, and undefined when the turn ends the loop
6331
+ * as any other turn without calls does. The loop goes on only past a turn
6332
+ * that hit its output limit, on a call that asked it to and has tools, so the
6333
+ * turn could have made one, and with a step left for another turn.
6334
+ */
6335
+ function truncation_holder(config, turn, step_index) {
6336
+ return turn.finish_reason === "length" && config.tools.length > 0 && step_index + 1 < config.max_steps ? config.truncated_turns : void 0;
6337
+ }
6338
+ /**
6339
+ * The user message that follows a turn the loop went on past. It says that
6340
+ * the turn was cut off, naming the limit when the call set `max_tokens`, and
6341
+ * asks for a shorter one: the next tool call or the final answer, since a
6342
+ * turn can be cut off partway through an answer as well as its reasoning.
6343
+ */
6344
+ function truncation_notice(max_tokens) {
6345
+ return {
6346
+ role: "user",
6347
+ content: `Your last turn reached ${max_tokens === void 0 ? "the output limit" : `the ${String(max_tokens)}-token output limit`} before it finished, so it was cut off. Keep this turn short: make your next tool call, or give your final answer.`
6348
+ };
6349
+ }
6350
+ /**
6351
+ * Go on past a turn that hit its output limit before it made a call. Its
6352
+ * visible text goes back as a plain assistant message, and nothing goes back
6353
+ * when it showed only whitespace, which some transports would send as an
6354
+ * empty block that the API refuses. Its reasoning never goes back: a thinking
6355
+ * block cut off before its signature can't be sent again, and the abandoned
6356
+ * reasoning is what the limit was set to stop paying for. The notice follows
6357
+ * it. The turn counts toward max_steps, its record says truncated, and it
6358
+ * ends whatever response the paused turns before it began.
6359
+ */
6360
+ function continue_truncated_turn(config, acc, at, turn, max_tokens) {
6361
+ record_turn_truncated(config.trajectory, {
6362
+ step_index: at.step_index,
6363
+ span_id: at.step_span,
6364
+ output_tokens: turn.usage.output_tokens,
6365
+ max_tokens
6366
+ });
6367
+ if (turn.text.trim().length > 0) config.messages.push({
6368
+ role: "assistant",
6369
+ content: turn.text
6370
+ });
6371
+ config.messages.push(truncation_notice(max_tokens));
6372
+ push_step_record(config, acc, at.step_index, turn, [], "length", at.step_span, true);
6373
+ acc.paused_texts.length = 0;
6374
+ }
6375
+ /**
6376
+ * Record a `cache_miss` when the previous turn read or wrote cache tokens
6377
+ * and this one read none, then remember this turn's reads plus writes for
6378
+ * the next. The rule needs no table of model minimums and no knowledge of
6379
+ * the provider.
6380
+ */
6381
+ function note_cache_traffic(config, memory, usage, turn) {
6382
+ const reads = usage.cached_input_tokens ?? 0;
6383
+ if (memory.previous_cache_traffic > 0 && reads === 0) record_cache_miss(config.trajectory, {
6384
+ ...turn,
6385
+ previous_cached_tokens: memory.previous_cache_traffic,
6386
+ input_tokens: usage.input_tokens
6387
+ });
6388
+ memory.previous_cache_traffic = reads + (usage.cache_write_tokens ?? 0);
6389
+ }
6390
+ /**
6391
+ * The call's capture when it records its turns' requests and replies, and
6392
+ * undefined when it records neither.
6393
+ */
6394
+ function active_capture(config) {
6395
+ const capture = config.capture;
6396
+ return capture !== void 0 && capture.level !== "off" ? capture : void 0;
6397
+ }
6398
+ /**
6399
+ * JSON.stringify's replacer for a captured value: a `Uint8Array`, a Node
6400
+ * `Buffer` included, becomes its base64 text, read off the holder because a
6401
+ * Buffer's own toJSON has already run by the time the replacer sees it.
6402
+ */
6403
+ function capture_replacer(key, value) {
6404
+ const raw = this[key];
6405
+ return raw instanceof Uint8Array ? Buffer.from(raw).toString("base64") : value;
6406
+ }
6407
+ /**
6408
+ * The JSON text a captured value is recorded as. Throws on a value that JSON
6409
+ * can't hold, a BigInt or a cycle.
6410
+ */
6411
+ function capture_text(value) {
6412
+ return JSON.stringify(value, capture_replacer);
6413
+ }
6414
+ /**
6415
+ * A fresh, JSON-safe copy of a value, as capture records it.
6416
+ */
6417
+ function capture_copy(value) {
6418
+ return JSON.parse(capture_text(value));
6419
+ }
6420
+ /**
6421
+ * A schema as JSON Schema with `to_json_schema`'s defaults, or undefined when
6422
+ * it can't be written as one.
6423
+ */
6424
+ function json_schema_or_undefined(schema) {
6425
+ let json;
6426
+ try {
6427
+ json = to_json_schema(schema);
6428
+ } catch {}
6429
+ return json;
6430
+ }
6431
+ /**
6432
+ * A tool as capture records it: its name, its description, and its input
6433
+ * schema as JSON Schema. A schema that can't be written as one stays
6434
+ * undefined, which the JSON copy leaves out.
6435
+ */
6436
+ function captured_tool(tool) {
6437
+ return {
6438
+ name: tool.name,
6439
+ description: tool.description,
6440
+ input_schema: json_schema_or_undefined(tool.input_schema)
6441
+ };
6442
+ }
6443
+ /**
6444
+ * A later turn's request: the messages it appended when it starts with
6445
+ * every message the previous captured turn held, and its whole array marked
6446
+ * `reshaped` otherwise, or when the previous turn's payload was dropped.
6447
+ */
6448
+ function later_request(previous, texts, copies) {
6449
+ if (previous?.every((text, n) => texts[n] === text) === true) return { appended: copies.slice(previous.length) };
6450
+ return {
6451
+ messages: copies,
6452
+ reshaped: true
6453
+ };
6454
+ }
6455
+ /**
6456
+ * The `request` a turn's request_sent carries under capture, built from
6457
+ * fresh JSON-safe copies of what the turn sends, so the loop's later pushes
6458
+ * never reach it. A call's first turn records its settings, its tools, and
6459
+ * every message; a later one records what changed (see later_request). The
6460
+ * messages' JSON text is remembered for the next turn. A value JSON can't
6461
+ * hold drops the payload, and the call goes on.
6462
+ */
6463
+ function capture_request(config, capture, memory, step_index, messages) {
6464
+ const previous = memory.captured_messages;
6465
+ memory.captured_messages = void 0;
6466
+ let request;
6467
+ try {
6468
+ const texts = messages.map(capture_text);
6469
+ const copies = texts.map((text) => JSON.parse(text));
6470
+ request = step_index === 0 ? {
6471
+ settings: capture_copy(capture.settings()),
6472
+ tools: capture_copy(config.tools.map(captured_tool)),
6473
+ messages: copies
6474
+ } : later_request(previous, texts, copies);
6475
+ memory.captured_messages = texts;
6476
+ } catch {}
6477
+ return request;
6478
+ }
6479
+ /**
6480
+ * The parts a turn returned, as response_received records them under
6481
+ * capture: the turn's own parts when its adapter set them, and otherwise its
6482
+ * text, when it has any, followed by its calls. A value JSON can't hold
6483
+ * drops them.
6484
+ */
6485
+ function capture_response(turn) {
6486
+ const parts = turn.content ?? [...turn.text.length > 0 ? [{
6487
+ type: "text",
6488
+ text: turn.text
6489
+ }] : [], ...turn.tool_calls.map(tool_call_part)];
6490
+ let content;
6491
+ try {
6492
+ content = capture_copy(parts);
6493
+ } catch {}
6494
+ return content;
6495
+ }
6496
+ const REDACTION_FAILED = Symbol("redaction_failed");
6497
+ /**
6498
+ * True for a promise, or any other object with a `then` method.
6499
+ */
6500
+ function is_thenable(value) {
6501
+ return typeof value === "object" && value !== null && typeof Reflect.get(value, "then") === "function";
6502
+ }
6503
+ /**
6504
+ * Hand a payload to the redactor, and return what it returned, or
6505
+ * REDACTION_FAILED when it threw or returned a thenable. The engine records
6506
+ * synchronously, so it can't wait for a thenable, and a rejection it leaves
6507
+ * behind is swallowed rather than left unhandled.
6508
+ */
6509
+ function apply_redactor(redact, payload) {
6510
+ try {
6511
+ const recorded = redact(payload);
6512
+ if (!is_thenable(recorded)) return recorded;
6513
+ Promise.resolve(recorded).catch(() => void 0);
6514
+ } catch {}
6515
+ return REDACTION_FAILED;
6516
+ }
6517
+ /**
6518
+ * What the trajectory records for a captured payload: the payload itself,
6519
+ * or what the engine's redactor returned in its place, `undefined` recording
6520
+ * nothing. A payload that couldn't be built (`data` undefined), and a
6521
+ * redactor that failed, record nothing and say so with
6522
+ * `capture_redaction_failed`, so nothing the redactor would have scrubbed is
6523
+ * written.
6524
+ */
6525
+ function redact_payload(config, capture, payload, span_id) {
6526
+ const recorded = payload.data === void 0 ? REDACTION_FAILED : capture.redact === void 0 ? payload.data : apply_redactor(capture.redact, payload);
6527
+ if (recorded !== REDACTION_FAILED) return recorded;
6528
+ record_capture_redaction_failed(config.trajectory, {
6529
+ step_index: payload.step_index,
6530
+ span_id,
6531
+ payload_kind: payload.kind
6532
+ });
6533
+ }
6534
+ /**
6535
+ * Record the exact body a native adapter is about to send, from a fresh copy
6536
+ * of `{ url, body }` that goes through the redactor, so nothing the redactor
6537
+ * does reaches the wire. The event carries the `url` and `body` of the object
6538
+ * the redactor returns, leaving out either one it dropped. A redactor that
6539
+ * returns anything other than an object fails closed.
6540
+ */
6541
+ function record_wire(config, capture, turn, wire) {
6542
+ let data;
6543
+ try {
6544
+ data = capture_copy({
6545
+ url: wire.url,
6546
+ body: wire.body
6547
+ });
6548
+ } catch {}
6549
+ const recorded = redact_payload(config, capture, {
6550
+ kind: "wire",
6551
+ step_index: turn.step_index,
6552
+ data
6553
+ }, turn.span_id);
6554
+ if (recorded === void 0) return;
6555
+ if (!is_record$1(recorded)) {
6556
+ record_capture_redaction_failed(config.trajectory, {
6557
+ ...turn,
6558
+ payload_kind: "wire"
6559
+ });
6560
+ return;
6561
+ }
6562
+ record_request_wire(config.trajectory, {
6563
+ ...turn,
6564
+ provider: config.provider,
6565
+ recorded
6566
+ });
6567
+ }
6568
+ /**
6569
+ * The `on_wire` a turn hands its adapter under capture at `'wire'`.
6570
+ */
6571
+ function wire_recorder(config, capture, turn) {
6572
+ return (wire) => {
6573
+ record_wire(config, capture, turn, wire);
6574
+ };
6575
+ }
6576
+ /**
5940
6577
  * Invoke the transport seam once for one turn, bracketed by request_sent /
5941
- * response_received events. A thrown invoke (or prepare_step) closes the
5942
- * step span with the error before rethrowing.
6578
+ * response_received events, which carry the request and the reply under
6579
+ * capture, and note its cache traffic in `memory`. A thrown invoke (or
6580
+ * prepare_step) closes the step span with the error before rethrowing.
5943
6581
  */
5944
- async function invoke_turn(config, step_index, step_span) {
6582
+ async function invoke_turn(config, memory, step_index, step_span) {
5945
6583
  try {
5946
6584
  const request_messages = await apply_prepare_step(config, step_index);
5947
- config.trajectory?.record({
5948
- kind: "request_sent",
5949
- step_index
6585
+ const capture = active_capture(config);
6586
+ const request = capture !== void 0 ? redact_payload(config, capture, {
6587
+ kind: "request",
6588
+ step_index,
6589
+ data: capture_request(config, capture, memory, step_index, request_messages)
6590
+ }, step_span) : void 0;
6591
+ if (request === void 0) memory.captured_messages = void 0;
6592
+ record_request_sent(config.trajectory, {
6593
+ step_index,
6594
+ span_id: step_span,
6595
+ call_span_id: config.capture?.call_span_id,
6596
+ request
5950
6597
  });
5951
6598
  const turn = await config.invoke_once({
5952
6599
  step_index,
@@ -5954,9 +6601,22 @@ async function invoke_turn(config, step_index, step_span) {
5954
6601
  tools: config.tools,
5955
6602
  abort: config.abort,
5956
6603
  stream: config.stream,
5957
- step_span
6604
+ step_span,
6605
+ ...capture?.level === "wire" ? { on_wire: wire_recorder(config, capture, {
6606
+ step_index,
6607
+ span_id: step_span
6608
+ }) } : {}
6609
+ });
6610
+ record_response_received(config.trajectory, step_index, turn, step_span, capture !== void 0 ? redact_payload(config, capture, {
6611
+ kind: "response",
6612
+ step_index,
6613
+ data: capture_response(turn)
6614
+ }, step_span) : void 0);
6615
+ note_cache_traffic(config, memory, turn.usage, {
6616
+ step_index,
6617
+ span_id: step_span,
6618
+ after_prepare_step: request_messages !== config.messages
5958
6619
  });
5959
- record_response_received(config.trajectory, step_index, turn, step_span);
5960
6620
  return turn;
5961
6621
  } catch (err) {
5962
6622
  const message = err instanceof Error ? err.message : String(err);
@@ -6037,18 +6697,70 @@ async function dispatch_salvaged_call_chunks(config, calls, step_index) {
6037
6697
  }
6038
6698
  }
6039
6699
  /**
6700
+ * The most characters of a dropped call's input that the notice quotes.
6701
+ */
6702
+ const NOTICE_INPUT_LIMIT = 120;
6703
+ /**
6704
+ * `text` cut to at most `limit` characters, counted by code point so that no
6705
+ * surrogate pair is split, with an ellipsis standing in for what was cut.
6706
+ */
6707
+ function clip(text, limit) {
6708
+ const points = Array.from(text);
6709
+ return points.length <= limit ? text : `${points.slice(0, limit - 1).join("")}…`;
6710
+ }
6711
+ /**
6712
+ * The user message that names the calls the per-step cap dropped. It's fed
6713
+ * back after the step's tool results, since the dropped calls themselves never
6714
+ * reach history, so without it the model would never learn they didn't run.
6715
+ * Each call reads as `name(input)`, with the input as JSON clipped to
6716
+ * NOTICE_INPUT_LIMIT characters. When the call has read-only tools, the
6717
+ * notice says that they don't count, or a turn that made more calls than the
6718
+ * cap would read as a contradiction.
6719
+ */
6720
+ function dropped_notice(cap, dropped, reads_exempt) {
6721
+ const one = dropped.length === 1;
6722
+ const counted = one ? "1 call" : `${String(dropped.length)} calls`;
6723
+ const exempt = reads_exempt ? ", not counting read-only tools" : "";
6724
+ const listed = dropped.map((call) => `${call.name}(${clip(safe_json_stringify(call.input), NOTICE_INPUT_LIMIT)})`).join(", ");
6725
+ return {
6726
+ role: "user",
6727
+ content: `${counted} didn't run, because max_tool_calls_per_step allows ${String(cap)} per turn${exempt}: ${listed}. Make ${one ? "it" : "them"} again, no more than ${String(cap)} per turn.`
6728
+ };
6729
+ }
6730
+ /**
6731
+ * The index of the first call past `cap`, counting only calls to tools that
6732
+ * aren't flagged read_only (an unknown name counts), or -1 when every call
6733
+ * fits.
6734
+ */
6735
+ function first_over_cap(calls, tool_map, cap) {
6736
+ let counted = 0;
6737
+ for (const [n, call] of calls.entries()) {
6738
+ if (tool_map.get(call.name)?.read_only === true) continue;
6739
+ counted += 1;
6740
+ if (counted > cap) return n;
6741
+ }
6742
+ return -1;
6743
+ }
6744
+ /**
6040
6745
  * Apply `max_tool_calls_per_step` to a turn's calls, native and salvaged
6041
- * alike. Dropped calls never reach history; the model re-issues them on a
6042
- * later turn if it still wants them.
6746
+ * alike. A call to a read-only tool doesn't count. The first counted call past
6747
+ * the cap is dropped, and so is every call after it, so what runs is always a
6748
+ * prefix of what the model asked for, and nothing runs after a call that
6749
+ * didn't. Dropped calls never reach history; the notice names them instead.
6043
6750
  */
6044
- function clamp_calls(config, calls, step_index) {
6751
+ function clamp_calls(config, tool_map, calls, step_index) {
6045
6752
  const cap = config.max_tool_calls_per_step;
6046
- if (cap === void 0 || calls.length <= cap) return {
6753
+ if (cap === void 0) return {
6754
+ kept: calls,
6755
+ dropped: []
6756
+ };
6757
+ const cut = first_over_cap(calls, tool_map, cap);
6758
+ if (cut === -1) return {
6047
6759
  kept: calls,
6048
6760
  dropped: []
6049
6761
  };
6050
- const kept = calls.slice(0, cap);
6051
- const dropped = calls.slice(cap);
6762
+ const kept = calls.slice(0, cut);
6763
+ const dropped = calls.slice(cut);
6052
6764
  record_tool_calls_dropped(config.trajectory, {
6053
6765
  step_index,
6054
6766
  max_tool_calls_per_step: cap,
@@ -6060,7 +6772,8 @@ function clamp_calls(config, calls, step_index) {
6060
6772
  });
6061
6773
  return {
6062
6774
  kept,
6063
- dropped
6775
+ dropped,
6776
+ notice: dropped_notice(cap, dropped, config.tools.some((tool) => tool.read_only === true))
6064
6777
  };
6065
6778
  }
6066
6779
  /**
@@ -6080,7 +6793,7 @@ async function record_failed_call(ctx, call, message, timing) {
6080
6793
  };
6081
6794
  ctx.records.push(record);
6082
6795
  ctx.all_records.push(record);
6083
- ctx.feed.push(build_tool_result_message(call.id, call.name, { error: message }));
6796
+ ctx.feed.push(build_tool_result_message(call.id, call.name, { error: message }, true));
6084
6797
  record_tool_call(ctx.config.trajectory, {
6085
6798
  step_index: ctx.step_index,
6086
6799
  name: call.name,
@@ -6339,16 +7052,17 @@ function resolve_turn_finish_reason(ctx, salvaged, turn_finish) {
6339
7052
  }
6340
7053
  /**
6341
7054
  * Build one StepRecord (cost derived and recorded, timing and
6342
- * provider_reported passed through), push it, notify on_finish_step, and
6343
- * close the step span.
7055
+ * provider_reported passed through, `truncated` set for a turn the loop went
7056
+ * on past), push it, notify on_finish_step, and close the step span.
6344
7057
  */
6345
- function push_step_record(config, acc, step_index, turn, tool_calls, finish_reason, step_span) {
7058
+ function push_step_record(config, acc, step_index, turn, tool_calls, finish_reason, step_span, truncated = false) {
6346
7059
  const record = {
6347
7060
  index: step_index,
6348
7061
  text: turn.text,
6349
7062
  tool_calls,
6350
7063
  usage: turn.usage,
6351
- finish_reason
7064
+ finish_reason,
7065
+ ...truncated ? { truncated: true } : {}
6352
7066
  };
6353
7067
  const breakdown = compute_and_record_cost(config, step_index, turn, step_span);
6354
7068
  if (breakdown !== void 0) record.cost = breakdown;
@@ -6369,9 +7083,21 @@ function push_step_record(config, acc, step_index, turn, tool_calls, finish_reas
6369
7083
  */
6370
7084
  async function run_loop_step(config, tool_map, acc, step_index) {
6371
7085
  const step_span = start_step_span(config.trajectory, step_index);
6372
- const turn = await invoke_turn(config, step_index, step_span);
7086
+ const turn = await invoke_turn(config, acc.memory, step_index, step_span);
7087
+ if (turn.paused === true) {
7088
+ record_turn_paused(config.trajectory, step_index, step_span);
7089
+ if (turn.tool_calls.length === 0) return continue_paused_turn(config, acc, step_index, step_span, turn);
7090
+ }
6373
7091
  const view = await salvage_turn(config, turn, tool_map, step_index);
6374
- const { kept, dropped } = clamp_calls(config, view.calls, step_index);
7092
+ const truncated = view.calls.length === 0 ? truncation_holder(config, turn, step_index) : void 0;
7093
+ if (truncated !== void 0) {
7094
+ continue_truncated_turn(config, acc, {
7095
+ step_index,
7096
+ step_span
7097
+ }, turn, truncated.max_tokens);
7098
+ return;
7099
+ }
7100
+ const { kept, dropped, notice } = clamp_calls(config, tool_map, view.calls, step_index);
6375
7101
  config.messages.push(build_assistant_message(turn, view, kept));
6376
7102
  if (kept.length === 0) {
6377
7103
  push_step_record(config, acc, step_index, turn, [], turn.finish_reason, step_span);
@@ -6397,6 +7123,8 @@ async function run_loop_step(config, tool_map, acc, step_index) {
6397
7123
  push_step_record(config, acc, step_index, turn, ctx.records, turn_finish_reason, step_span);
6398
7124
  if (ctx.terminal_fired) return loop_result(acc, turn.text, "stop", false);
6399
7125
  if (ctx.would_exceed_after) return loop_result(acc, turn.text, "max_steps", true);
7126
+ if (notice !== void 0) config.messages.push(notice);
7127
+ acc.paused_texts.length = 0;
6400
7128
  }
6401
7129
  /**
6402
7130
  * Run the model-turn / tool-execution loop to completion.
@@ -6409,7 +7137,9 @@ async function run_loop_step(config, tool_map, acc, step_index) {
6409
7137
  async function run_tool_loop(config) {
6410
7138
  const acc = {
6411
7139
  steps: [],
6412
- tool_calls: []
7140
+ tool_calls: [],
7141
+ paused_texts: [],
7142
+ memory: config.call_memory ?? { previous_cache_traffic: 0 }
6413
7143
  };
6414
7144
  const tool_map = /* @__PURE__ */ new Map();
6415
7145
  for (const t of config.tools) tool_map.set(t.name, t);
@@ -6470,7 +7200,10 @@ const NETWORK_ERROR_CODES = /* @__PURE__ */ new Set([
6470
7200
  "ECONNRESET",
6471
7201
  "ETIMEDOUT",
6472
7202
  "ENOTFOUND",
6473
- "ECONNREFUSED"
7203
+ "ECONNREFUSED",
7204
+ "UND_ERR_HEADERS_TIMEOUT",
7205
+ "UND_ERR_BODY_TIMEOUT",
7206
+ "UND_ERR_SOCKET"
6474
7207
  ]);
6475
7208
  /**
6476
7209
  * True when `err` already carries a `kind` this layer treats as retryable, so
@@ -6508,25 +7241,35 @@ function retry_after_from_error(err) {
6508
7241
  return typeof hv === "string" ? parse_retry_after(hv) : void 0;
6509
7242
  }
6510
7243
  /**
6511
- * Build the `rate_limit` classification for a 429, attaching the message and a
6512
- * parsed `Retry-After` when either is present.
7244
+ * Build the classification for a retryable status (`rate_limit` for a 429,
7245
+ * `provider_5xx` for a 5xx), attaching the message and a parsed `Retry-After`
7246
+ * when either is present. HTTP defines the header for a 503 as well as a 429,
7247
+ * so a server's wait counts on both.
6513
7248
  */
6514
- function classify_rate_limit(err) {
7249
+ function classify_status(err, kind, status) {
6515
7250
  const out = with_error_message(err, {
6516
- kind: "rate_limit",
6517
- status: 429
7251
+ kind,
7252
+ status
6518
7253
  });
6519
7254
  const retry_after_ms = retry_after_from_error(err);
6520
7255
  if (retry_after_ms !== void 0) out["retry_after_ms"] = retry_after_ms;
6521
7256
  return out;
6522
7257
  }
6523
7258
  /**
6524
- * Build the `network` classification when `err.code` is a known network error
6525
- * code, or `undefined` when it is not.
7259
+ * True when `value` carries a `code` that names a known network failure.
7260
+ */
7261
+ function has_network_code(value) {
7262
+ if (value === null || typeof value !== "object") return false;
7263
+ const code = Reflect.get(value, "code");
7264
+ return typeof code === "string" && NETWORK_ERROR_CODES.has(code);
7265
+ }
7266
+ /**
7267
+ * Build the `network` classification when the error or its `cause` carries a
7268
+ * known network error code, or `undefined` when neither does. The AI SDK
7269
+ * wraps a failed fetch in an `APICallError` whose `cause` holds undici's code.
6526
7270
  */
6527
7271
  function classify_network_error(err) {
6528
- const code = Reflect.get(err, "code");
6529
- if (typeof code !== "string" || !NETWORK_ERROR_CODES.has(code)) return void 0;
7272
+ if (!has_network_code(err) && !has_network_code(Reflect.get(err, "cause"))) return void 0;
6530
7273
  return with_error_message(err, { kind: "network" });
6531
7274
  }
6532
7275
  /**
@@ -6534,21 +7277,20 @@ function classify_network_error(err) {
6534
7277
  * understands.
6535
7278
  *
6536
7279
  * Passes through anything that already carries a recognized `kind`.
6537
- * Otherwise inspects `statusCode`/`status`: 429 becomes `rate_limit`
6538
- * (reading `Retry-After` off `responseHeaders` when present), 5xx becomes
6539
- * `provider_5xx`, and a known `ECONNRESET`/`ETIMEDOUT`/`ENOTFOUND`/
6540
- * `ECONNREFUSED` error code becomes `network`. Anything else passes through
6541
- * unclassified, which `retry_with_policy` treats as non-retryable.
7280
+ * Otherwise inspects `statusCode`/`status`: 429 becomes `rate_limit` and 5xx
7281
+ * becomes `provider_5xx`, each reading `Retry-After` off `responseHeaders`
7282
+ * when present, and a known network error code (`ECONNRESET`, `ETIMEDOUT`,
7283
+ * `ENOTFOUND`, `ECONNREFUSED`, or undici's `UND_ERR_HEADERS_TIMEOUT`,
7284
+ * `UND_ERR_BODY_TIMEOUT`, and `UND_ERR_SOCKET`), on the error or on its
7285
+ * `cause`, becomes `network`. Anything else passes through unclassified,
7286
+ * which `retry_with_policy` treats as non-retryable.
6542
7287
  */
6543
7288
  function classify_provider_error(err) {
6544
7289
  if (err === null || typeof err !== "object") return err;
6545
7290
  if (has_retry_kind(err)) return err;
6546
7291
  const status = resolve_error_status(err);
6547
- if (status === 429) return classify_rate_limit(err);
6548
- if (status !== void 0 && status >= 500 && status < 600) return with_error_message(err, {
6549
- kind: "provider_5xx",
6550
- status
6551
- });
7292
+ if (status === 429) return classify_status(err, "rate_limit", status);
7293
+ if (status !== void 0 && status >= 500 && status < 600) return classify_status(err, "provider_5xx", status);
6552
7294
  return classify_network_error(err) ?? err;
6553
7295
  }
6554
7296
  /**
@@ -6586,34 +7328,36 @@ function arm_turn_timeout(user_abort, turn_timeout_ms) {
6586
7328
  * Engine-owned wrapper shared by both transports: one turn attempt inside
6587
7329
  * retry_with_policy. The catch ladder classifies by CAUSE, not
6588
7330
  * by the shape the transport happened to throw: on_chunk_error passes through,
6589
- * then a genuine user abort wins, then any error once a chunk has flowed is a
6590
- * non-retryable stream interruption, then a pre-chunk `turn_timeout_ms` expiry
6591
- * is a retryable typed timeout, and only a below-loop aborted_error unrelated
6592
- * to either passes through. Cause-first ordering is what keeps the two
6593
- * transports in parity: the ai_sdk transport reports a mid-stream abort as an
6594
- * aborted_error and the native one as a raw AbortError, so both must be read as
6595
- * the same interruption rather than the ai_sdk timeout masquerading as a user
6596
- * cancel. call_once receives the composed abort+timeout signal so the deadline
6597
- * actually cancels the in-flight request. Adapters may swap `classify`, but
6598
- * never the ladder itself: the engine owns retry, so a hidden adapter-level
6599
- * retry would be invisible to anything relying on this ladder.
7331
+ * then a genuine user abort wins, then any error once a chunk has reached a
7332
+ * consumer is a non-retryable stream interruption, then a `turn_timeout_ms`
7333
+ * expiry is a retryable typed timeout, and only a below-loop aborted_error
7334
+ * unrelated to either passes through. With no consumer attached, nothing was
7335
+ * delivered that a retry would repeat, so a mid-stream failure, a deadline
7336
+ * expiry included, is classified as one before any chunk is. Cause-first
7337
+ * ordering is what keeps the two transports in parity: the ai_sdk transport
7338
+ * reports a mid-stream abort as an aborted_error and the native one as a raw
7339
+ * AbortError, so both must be read as the same interruption rather than the
7340
+ * ai_sdk timeout masquerading as a user cancel. call_once receives the
7341
+ * composed abort+timeout signal so the deadline actually cancels the
7342
+ * in-flight request. Adapters may swap `classify`, but never the ladder
7343
+ * itself: the engine owns retry, so a hidden adapter-level retry would be
7344
+ * invisible to anything relying on this ladder.
6600
7345
  *
6601
7346
  * The wrapper also stamps StepTiming here, and only here, because this is the
6602
7347
  * one place that can see attempt boundaries: started_at re-stamps at every
6603
7348
  * attempt entry, so failed attempts and backoff waits never inflate
6604
- * duration_ms. `first_chunk_at` (the builder's Date.now() from its first
6605
- * dispatched chunk) can only belong to the returning attempt, since any
6606
- * failure after a chunk has flowed is non-retryable by the ladder below.
7349
+ * duration_ms. The builder's first-chunk stamp belongs to the returning
7350
+ * attempt, since the builder clears it as each attempt starts.
6607
7351
  */
6608
- function retry_turn(call_once, args, first_chunk_at, retry_policy, classify, turn_timeout_ms, trajectory) {
6609
- const has_streamed = () => first_chunk_at() !== void 0;
7352
+ function retry_turn(call_once, args, stamp, retry_policy, classify, turn_timeout_ms, trajectory, on_chunk_provided) {
7353
+ const has_streamed = () => on_chunk_provided && stamp.at() !== void 0;
6610
7354
  return retry_with_policy(async () => {
6611
7355
  const started_at = Date.now();
6612
7356
  const deadline = arm_turn_timeout(args.abort, turn_timeout_ms);
6613
7357
  try {
6614
7358
  return {
6615
7359
  ...await call_once(deadline.signal),
6616
- timing: build_step_timing(started_at, first_chunk_at())
7360
+ timing: build_step_timing(started_at, stamp)
6617
7361
  };
6618
7362
  } catch (err) {
6619
7363
  if (err instanceof on_chunk_error) throw err;
@@ -6637,52 +7381,61 @@ function retry_turn(call_once, args, first_chunk_at, retry_policy, classify, tur
6637
7381
  }
6638
7382
  /**
6639
7383
  * Assemble one turn's StepTiming from the attempt's start stamp and the
6640
- * builder's first-chunk stamp (undefined on non-streamed turns).
7384
+ * builder's first-chunk stamp, which holds nothing on a non-streamed turn.
6641
7385
  */
6642
- function build_step_timing(started_at, first_chunk) {
7386
+ function build_step_timing(started_at, stamp) {
6643
7387
  const timing = {
6644
7388
  started_at,
6645
7389
  duration_ms: Date.now() - started_at
6646
7390
  };
7391
+ const first_chunk = stamp.at();
6647
7392
  if (first_chunk !== void 0) timing.first_chunk_ms = first_chunk - started_at;
7393
+ if (stamp.whole_tool_call()) timing.whole_tool_call = true;
6648
7394
  return timing;
6649
7395
  }
6650
7396
  /**
6651
7397
  * Build the ai_sdk-transport InvokeOnce: the SDK turn built by
6652
7398
  * providers/ai_sdk/invoke.ts behind the same engine-owned retry_turn wrapper
6653
- * the native path uses. The SDK call body lives behind the seam; this builder
6654
- * owns only retry and the streamed-output tracking that makes a mid-stream
6655
- * failure a non-retryable interruption (the same first-chunk stamp doubles as
6656
- * StepTiming.first_chunk_ms).
7399
+ * the native path uses. The SDK call body lives behind the seam, and every
7400
+ * turn streams; this builder owns only retry and the first-chunk stamp, which
7401
+ * it clears as each attempt starts, and which both makes a failure after a
7402
+ * chunk reached a consumer a non-retryable interruption and doubles as
7403
+ * StepTiming.first_chunk_ms.
6657
7404
  */
6658
7405
  function build_ai_sdk_invoke(cfg) {
6659
7406
  return async (args) => {
6660
- let first_chunk_at;
6661
- const call_once = (turn_abort) => cfg.invoke_turn({
6662
- step_index: args.step_index,
6663
- messages: args.messages,
6664
- abort: turn_abort,
6665
- stream: args.stream,
6666
- on_first_chunk: () => {
6667
- first_chunk_at ??= Date.now();
6668
- }
6669
- });
6670
- return await retry_turn(call_once, args, () => first_chunk_at, cfg.retry_policy, classify_provider_error, cfg.turn_timeout_ms, cfg.trajectory);
7407
+ const stamp = create_first_chunk_stamp();
7408
+ const call_once = (turn_abort) => {
7409
+ stamp.reset();
7410
+ return cfg.invoke_turn({
7411
+ step_index: args.step_index,
7412
+ messages: args.messages,
7413
+ abort: turn_abort,
7414
+ observe_chunk: stamp.observe
7415
+ });
7416
+ };
7417
+ return await retry_turn(call_once, args, stamp, cfg.retry_policy, classify_provider_error, cfg.turn_timeout_ms, cfg.trajectory, cfg.on_chunk_provided);
6671
7418
  };
6672
7419
  }
6673
7420
  /**
6674
7421
  * Build the native-transport InvokeOnce: maps the loop's InvokeOnceArgs to a
6675
7422
  * TurnRequest and calls adapter.invoke_turn inside the same retry_turn
6676
- * wrapper as the ai_sdk path. The adapter sees a child abort signal so a
6677
- * throwing on_chunk consumer cancels the in-flight request, and its chunk
6678
- * emissions flow through the shared dispatcher, which is what lets
6679
- * run_tool_loop treat both transports identically.
7423
+ * wrapper as the ai_sdk path. Every turn asks for a stream when the adapter
7424
+ * supports streaming, whether or not a consumer is attached, so a long turn's
7425
+ * headers arrive before fetch's 300-second limit, and the adapter gets a
7426
+ * `dispatch_chunk` that stamps the first chunk of each attempt and hands
7427
+ * every chunk to the shared dispatcher, which passes it on only to a
7428
+ * consumer. The adapter sees a child abort signal so a throwing on_chunk
7429
+ * consumer cancels the in-flight request, which is what lets run_tool_loop
7430
+ * treat both transports identically.
6680
7431
  */
6681
7432
  function build_native_invoke(cfg) {
6682
7433
  const classify = cfg.adapter.classify_error ?? classify_provider_error;
7434
+ const streams = cfg.adapter.supports("streaming");
6683
7435
  return async (args) => {
6684
- let first_chunk_at;
7436
+ const stamp = create_first_chunk_stamp();
6685
7437
  const call_once = async (turn_abort) => {
7438
+ stamp.reset();
6686
7439
  const internal_controller = new AbortController();
6687
7440
  const cancel_on_turn_abort = () => {
6688
7441
  internal_controller.abort(turn_abort.reason);
@@ -6690,7 +7443,7 @@ function build_native_invoke(cfg) {
6690
7443
  if (turn_abort.aborted) internal_controller.abort(turn_abort.reason);
6691
7444
  else turn_abort.addEventListener("abort", cancel_on_turn_abort, { once: true });
6692
7445
  const dispatch_chunk = async (chunk) => {
6693
- first_chunk_at ??= Date.now();
7446
+ stamp.observe(chunk);
6694
7447
  try {
6695
7448
  await cfg.dispatcher.dispatch(chunk);
6696
7449
  } catch (err) {
@@ -6704,16 +7457,18 @@ function build_native_invoke(cfg) {
6704
7457
  messages,
6705
7458
  tools: args.tools,
6706
7459
  abort: internal_controller.signal,
6707
- stream: args.stream,
7460
+ stream: streams,
6708
7461
  model_id: cfg.model_id,
6709
7462
  effort: cfg.effort,
7463
+ ...cfg.cache !== "off" ? { cache: cfg.cache } : {},
6710
7464
  ...system !== void 0 ? { system } : {},
6711
7465
  ...cfg.schema !== void 0 ? { schema: cfg.schema } : {},
6712
7466
  ...cfg.provider_options !== void 0 ? { provider_options: cfg.provider_options } : {},
6713
7467
  ...cfg.temperature !== void 0 ? { temperature: cfg.temperature } : {},
6714
7468
  ...cfg.max_tokens !== void 0 ? { max_tokens: cfg.max_tokens } : {},
6715
7469
  ...cfg.top_p !== void 0 ? { top_p: cfg.top_p } : {},
6716
- ...args.stream ? { dispatch_chunk } : {}
7470
+ ...streams ? { dispatch_chunk } : {},
7471
+ ...args.on_wire !== void 0 ? { on_wire: args.on_wire } : {}
6717
7472
  };
6718
7473
  try {
6719
7474
  return await cfg.adapter.invoke_turn(req);
@@ -6721,10 +7476,73 @@ function build_native_invoke(cfg) {
6721
7476
  turn_abort.removeEventListener("abort", cancel_on_turn_abort);
6722
7477
  }
6723
7478
  };
6724
- return await retry_turn(call_once, args, () => first_chunk_at, cfg.retry_policy, classify, cfg.turn_timeout_ms, cfg.trajectory);
7479
+ return await retry_turn(call_once, args, stamp, cfg.retry_policy, classify, cfg.turn_timeout_ms, cfg.trajectory, cfg.on_chunk_provided);
6725
7480
  };
6726
7481
  }
6727
7482
  /**
7483
+ * Throw `engine_config_error` before any request when two or more of a call's
7484
+ * tools share a name, naming each duplicate with its count. The loop and
7485
+ * every transport key tools by name, so a duplicate would silently replace
7486
+ * the tool before it.
7487
+ */
7488
+ function assert_unique_tool_names(tools) {
7489
+ if (tools === void 0) return;
7490
+ const counts = /* @__PURE__ */ new Map();
7491
+ for (const tool of tools) counts.set(tool.name, (counts.get(tool.name) ?? 0) + 1);
7492
+ const duplicates = [...counts].filter(([, count]) => count > 1);
7493
+ if (duplicates.length === 0) return;
7494
+ const listed = duplicates.map(([name, count]) => `${name} (${String(count)})`).join(", ");
7495
+ throw new engine_config_error(`duplicate tool names: ${listed}; give each tool its own name, or pass \`name_prefix\` to mcp_client for one server's tools`);
7496
+ }
7497
+ const CACHE_SETTINGS = {
7498
+ off: true,
7499
+ "5m": true,
7500
+ "1h": true
7501
+ };
7502
+ const CAPTURE_LEVELS = {
7503
+ off: true,
7504
+ turns: true,
7505
+ wire: true
7506
+ };
7507
+ const TRUNCATED_TURN_POLICIES = {
7508
+ stop: true,
7509
+ continue: true
7510
+ };
7511
+ /**
7512
+ * Throw `engine_config_error` when `value` is set to anything but a key of
7513
+ * `allowed`, naming the field, the value, and the values it takes.
7514
+ */
7515
+ function assert_allowed(allowed, value, field) {
7516
+ if (value === void 0 || Object.hasOwn(allowed, value)) return;
7517
+ const listed = Object.keys(allowed).map((key) => `'${key}'`).join(", ");
7518
+ throw new engine_config_error(`${field} must be one of ${listed}, got ${JSON.stringify(value)}`);
7519
+ }
7520
+ /**
7521
+ * Throw `engine_config_error` for a cache setting that isn't a
7522
+ * `CacheSetting`. `create_engine` checks `defaults.cache` with it, and
7523
+ * `generate` a call's `cache`.
7524
+ */
7525
+ function assert_cache_setting(value, field) {
7526
+ assert_allowed(CACHE_SETTINGS, value, field);
7527
+ }
7528
+ /**
7529
+ * Throw `engine_config_error` for a capture level that isn't a
7530
+ * `CaptureLevel`. `create_engine` checks `defaults.capture` with it, and
7531
+ * `generate` a call's `capture`.
7532
+ */
7533
+ function assert_capture_level(value, field) {
7534
+ assert_allowed(CAPTURE_LEVELS, value, field);
7535
+ }
7536
+ /**
7537
+ * Throw `engine_config_error` for a truncated-turn policy that isn't a
7538
+ * `TruncatedTurnPolicy`. `create_engine` checks
7539
+ * `defaults.truncated_turn_policy` with it, and `generate` a call's
7540
+ * `truncated_turn_policy`.
7541
+ */
7542
+ function assert_truncated_turn_policy(value, field) {
7543
+ assert_allowed(TRUNCATED_TURN_POLICIES, value, field);
7544
+ }
7545
+ /**
6728
7546
  * Resolve the provider name: per-call, then the engine default, then the sole
6729
7547
  * configured adapter. With several providers configured and neither a per-call
6730
7548
  * provider nor a default, there is no sane guess, so this throws
@@ -6780,17 +7598,58 @@ function assert_capabilities(adapter, provider, opts, tools_list, on_chunk_provi
6780
7598
  if (on_chunk_provided && !adapter.supports("streaming")) throw new provider_capability_error(provider, "streaming");
6781
7599
  }
6782
7600
  /**
6783
- * Resolve the per-turn knobs (effort, retry policy, turn timeout) against
6784
- * engine defaults. Validated here, before any transport is built, so a bad
6785
- * budget fails as a config error, never as a spurious timeout.
7601
+ * Read the caching an adapter declares. An `ai_sdk` adapter that declares
7602
+ * `'with_tools'` or `'opt_in'` but has no `translate_cache` has no way to put
7603
+ * the setting on the call, so it counts as declaring nothing.
7604
+ */
7605
+ function declared_caching(adapter) {
7606
+ if (adapter.kind === "ai_sdk" && adapter.prompt_cache !== "automatic" && adapter.translate_cache === void 0) return;
7607
+ return adapter.prompt_cache;
7608
+ }
7609
+ /**
7610
+ * Resolve a call's cache setting from the caching its adapter declares and
7611
+ * the setting the call or the engine asked for. `'with_tools'` caches for
7612
+ * five minutes by default on a call with tools, `'opt_in'` only when asked,
7613
+ * and an `'automatic'` or undeclared adapter never, with a reason for
7614
+ * ignoring an explicit `'5m'` or `'1h'`.
7615
+ */
7616
+ function resolve_cache(declared, explicit, has_tools) {
7617
+ if (declared === "with_tools") return { cache: explicit ?? (has_tools ? "5m" : "off") };
7618
+ if (declared === "opt_in") return { cache: explicit ?? "off" };
7619
+ if (explicit === void 0 || explicit === "off") return { cache: "off" };
7620
+ return {
7621
+ cache: "off",
7622
+ ignored_reason: declared === "automatic" ? "the provider caches on its own" : "this adapter doesn't request prompt caching"
7623
+ };
7624
+ }
7625
+ /**
7626
+ * Resolve the capture level a call's loop runs at: the call's, else the
7627
+ * engine default, else `'off'`. On the ai_sdk transport the SDK builds the
7628
+ * body, so `'wire'` records `option_ignored` and runs as `'turns'`.
6786
7629
  */
6787
- function resolve_turn_config(opts, engine) {
7630
+ function resolve_capture(opts, engine, call) {
7631
+ const level = opts.capture ?? engine.default_capture ?? "off";
7632
+ if (level !== "wire" || call.adapter.kind !== "ai_sdk") return level;
7633
+ create_option_ignored_dedup(call.trajectory).emit("capture", call.adapter.name, "wire capture needs a native transport");
7634
+ return "turns";
7635
+ }
7636
+ /**
7637
+ * Resolve the per-turn knobs (effort, cache, capture, retry policy, turn
7638
+ * timeout) against engine defaults. Validated here, before any transport is built, so
7639
+ * a bad budget fails as a config error, never as a spurious timeout. A cache
7640
+ * setting the adapter can't honor records `option_ignored` with its reason.
7641
+ */
7642
+ function resolve_turn_config(opts, engine, call) {
6788
7643
  const effort = opts.effort ?? engine.default_effort;
6789
7644
  const retry_policy = opts.retry ?? engine.default_retry;
6790
7645
  const turn_timeout_ms = opts.turn_timeout_ms ?? engine.default_turn_timeout_ms;
6791
7646
  if (turn_timeout_ms !== void 0 && turn_timeout_ms <= 0) throw new engine_config_error(`turn_timeout_ms must be > 0, got ${String(turn_timeout_ms)}`);
7647
+ const { cache, ignored_reason } = resolve_cache(declared_caching(call.adapter), opts.cache ?? engine.default_cache, call.tools.length > 0);
7648
+ if (ignored_reason !== void 0) create_option_ignored_dedup(call.trajectory).emit("cache", call.adapter.name, ignored_reason);
6792
7649
  return {
6793
7650
  effort,
7651
+ cache,
7652
+ capture: resolve_capture(opts, engine, call),
6794
7653
  retry_policy,
6795
7654
  turn_timeout_ms
6796
7655
  };
@@ -6821,23 +7680,37 @@ function resolve_loop_limits(opts, engine) {
6821
7680
  tool_error_policy,
6822
7681
  schema_repair_attempts,
6823
7682
  salvage_budget,
6824
- max_tool_calls_per_step
7683
+ max_tool_calls_per_step,
7684
+ truncated_turns: (opts.truncated_turn_policy ?? engine.default_truncated_turn_policy) === "continue" ? { max_tokens: opts.max_tokens } : void 0
7685
+ };
7686
+ }
7687
+ /**
7688
+ * Merge two of an adapter's translations into call-level provider options,
7689
+ * key by key under each provider, with `over` winning where both set a key.
7690
+ */
7691
+ function merge_translations(base, over) {
7692
+ const out = { ...base };
7693
+ for (const [provider, inner] of Object.entries(over)) out[provider] = {
7694
+ ...base[provider],
7695
+ ...inner
6825
7696
  };
7697
+ return out;
6826
7698
  }
6827
7699
  /**
6828
- * Build the ai_sdk-transport InvokeOnce for one call: translate effort, merge
6829
- * provider options, load the SDK seam, and wrap it in the engine-owned retry
6830
- * via build_ai_sdk_invoke.
7700
+ * Build the ai_sdk-transport InvokeOnce for one call: translate effort and
7701
+ * the cache setting, merge provider options, load the SDK seam, and wrap it
7702
+ * in the engine-owned retry via build_ai_sdk_invoke.
6831
7703
  */
6832
7704
  async function build_ai_sdk_transport(cfg) {
6833
7705
  const { adapter, opts, effort } = cfg;
6834
7706
  const effort_translation = adapter.translate_effort(effort);
6835
7707
  if (effort !== "none" && effort_translation.effort_ignored) record_effort_ignored(cfg.trajectory, cfg.target.model_id);
6836
- const combined_provider_options = merge_provider_options(effort_translation.provider_options, opts.provider_options);
7708
+ const cache_options = adapter.translate_cache?.(cfg.cache, opts.provider_options) ?? {};
7709
+ const combined_provider_options = merge_provider_options(merge_translations(effort_translation.provider_options, cache_options), opts.provider_options);
6837
7710
  const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
6838
7711
  let ai_sdk_invoke_mod;
6839
7712
  try {
6840
- ai_sdk_invoke_mod = await import("./invoke-LbzZLZFw.js");
7713
+ ai_sdk_invoke_mod = await import("./invoke-BbJLQcLl.js");
6841
7714
  } catch (err) {
6842
7715
  throw missing_peer_error("ai", err);
6843
7716
  }
@@ -6858,7 +7731,8 @@ async function build_ai_sdk_transport(cfg) {
6858
7731
  }),
6859
7732
  retry_policy: cfg.retry_policy,
6860
7733
  turn_timeout_ms: cfg.turn_timeout_ms,
6861
- trajectory: cfg.trajectory
7734
+ trajectory: cfg.trajectory,
7735
+ on_chunk_provided: cfg.on_chunk_provided
6862
7736
  });
6863
7737
  }
6864
7738
  /**
@@ -6910,8 +7784,10 @@ function schema_instruction(schema) {
6910
7784
  /**
6911
7785
  * Assemble the ToolLoopConfig base shared by every loop invocation of one
6912
7786
  * generate call. Everything here is per-call state: the mutable transcript,
6913
- * the pricing lookup, and the pricing_missing dedup all live for exactly one
6914
- * call, which is what lets schema-repair re-invocations share them. The
7787
+ * the pricing lookup, the pricing_missing dedup, and the call memory that
7788
+ * carries the previous turn's cache traffic and captured messages all live
7789
+ * for exactly one call,
7790
+ * which is what lets schema-repair re-invocations share them. The
6915
7791
  * conditional spreads keep optional fields absent rather than undefined for
6916
7792
  * exactOptionalPropertyTypes.
6917
7793
  */
@@ -6939,8 +7815,11 @@ function build_loop_config(cfg) {
6939
7815
  model_id: target.model_id,
6940
7816
  resolve_pricing,
6941
7817
  pricing_dedup: create_pricing_missing_dedup(cfg.trajectory),
7818
+ call_memory: { previous_cache_traffic: 0 },
7819
+ capture: cfg.capture,
6942
7820
  ...limits.salvage_budget !== void 0 ? { salvage_budget: limits.salvage_budget } : {},
6943
7821
  ...limits.max_tool_calls_per_step !== void 0 ? { max_tool_calls_per_step: limits.max_tool_calls_per_step } : {},
7822
+ truncated_turns: limits.truncated_turns,
6944
7823
  ...opts.prepare_step !== void 0 ? { prepare_step: opts.prepare_step } : {}
6945
7824
  };
6946
7825
  }
@@ -7045,6 +7924,27 @@ function assemble_result(outcome, target) {
7045
7924
  return result;
7046
7925
  }
7047
7926
  /**
7927
+ * The settings a call's first captured turn records: the provider and the
7928
+ * model, the resolved effort and cache (left out when off), the sampling
7929
+ * knobs it set, its provider options as the engine defaults and the call
7930
+ * merged them, before an adapter's translation of effort or cache adds to
7931
+ * them, and its schema as JSON Schema, left out when it can't be written as
7932
+ * one. An unset field stays undefined, which the loop's JSON copy leaves out.
7933
+ */
7934
+ function capture_settings(opts, target, turn) {
7935
+ return {
7936
+ provider: target.provider,
7937
+ model_id: target.model_id,
7938
+ effort: turn.effort,
7939
+ cache: turn.cache === "off" ? void 0 : turn.cache,
7940
+ temperature: opts.temperature,
7941
+ max_tokens: opts.max_tokens,
7942
+ top_p: opts.top_p,
7943
+ provider_options: opts.provider_options,
7944
+ schema: opts.schema === void 0 ? void 0 : json_schema_or_undefined(opts.schema)
7945
+ };
7946
+ }
7947
+ /**
7048
7948
  * Resolve a `generate` call's options against engine defaults, run the tool
7049
7949
  * loop, and return the aggregated result.
7050
7950
  *
@@ -7072,6 +7972,10 @@ async function generate(opts_in, engine) {
7072
7972
  const { opts, target } = resolve_target(opts_in, engine);
7073
7973
  const adapter = engine.adapters.get(target.provider);
7074
7974
  if (adapter === void 0) throw new provider_not_configured_error(target.provider, [...engine.adapters.keys()]);
7975
+ assert_unique_tool_names(opts.tools);
7976
+ assert_cache_setting(opts.cache, "cache");
7977
+ assert_capture_level(opts.capture, "capture");
7978
+ assert_truncated_turn_policy(opts.truncated_turn_policy, "truncated_turn_policy");
7075
7979
  if (adapter.kind === "external") return {
7076
7980
  ...await adapter.generate(opts, target),
7077
7981
  timing: call_timing(started_at)
@@ -7080,7 +7984,11 @@ async function generate(opts_in, engine) {
7080
7984
  const on_chunk_provided = opts.on_chunk !== void 0;
7081
7985
  const tools_list = opts.tools ?? [];
7082
7986
  assert_capabilities(adapter, target.provider, opts, tools_list, on_chunk_provided);
7083
- const turn = resolve_turn_config(opts, engine);
7987
+ const turn = resolve_turn_config(opts, engine, {
7988
+ adapter,
7989
+ tools: tools_list,
7990
+ trajectory
7991
+ });
7084
7992
  const dispatcher = create_chunk_dispatcher(opts.on_chunk);
7085
7993
  let invoke_once;
7086
7994
  if (adapter.kind === "ai_sdk") invoke_once = await build_ai_sdk_transport({
@@ -7088,12 +7996,14 @@ async function generate(opts_in, engine) {
7088
7996
  target,
7089
7997
  opts,
7090
7998
  effort: turn.effort,
7999
+ cache: turn.cache,
7091
8000
  retry_policy: turn.retry_policy,
7092
8001
  turn_timeout_ms: turn.turn_timeout_ms,
7093
8002
  dispatcher,
7094
8003
  tools_list,
7095
8004
  trajectory,
7096
- telemetry: engine.default_ai_sdk_telemetry
8005
+ telemetry: engine.default_ai_sdk_telemetry,
8006
+ on_chunk_provided
7097
8007
  });
7098
8008
  else invoke_once = build_native_invoke({
7099
8009
  adapter,
@@ -7103,11 +8013,13 @@ async function generate(opts_in, engine) {
7103
8013
  trajectory,
7104
8014
  dispatcher,
7105
8015
  effort: turn.effort,
8016
+ cache: turn.cache,
7106
8017
  schema: opts.schema,
7107
8018
  provider_options: opts.provider_options,
7108
8019
  temperature: opts.temperature,
7109
8020
  max_tokens: opts.max_tokens,
7110
- top_p: opts.top_p
8021
+ top_p: opts.top_p,
8022
+ on_chunk_provided
7111
8023
  });
7112
8024
  const limits = resolve_loop_limits(opts, engine);
7113
8025
  const initial_messages = build_prompt_messages(opts);
@@ -7129,7 +8041,13 @@ async function generate(opts_in, engine) {
7129
8041
  trajectory,
7130
8042
  dispatcher,
7131
8043
  on_chunk_provided,
7132
- limits
8044
+ limits,
8045
+ capture: {
8046
+ level: turn.capture,
8047
+ call_span_id: generate_span,
8048
+ settings: () => capture_settings(opts, target, turn),
8049
+ redact: engine.redact_capture
8050
+ }
7133
8051
  });
7134
8052
  try {
7135
8053
  const outcome = await run_generate_loop({
@@ -7344,6 +8262,8 @@ function build_internals(base, defaults) {
7344
8262
  assign_if_present(internals, "default_model", defaults.model);
7345
8263
  assign_if_present(internals, "default_provider", defaults.provider);
7346
8264
  assign_if_present(internals, "default_system", defaults.system);
8265
+ assign_if_present(internals, "default_cache", defaults.cache);
8266
+ assign_if_present(internals, "default_capture", defaults.capture);
7347
8267
  assign_if_present(internals, "default_temperature", defaults.temperature);
7348
8268
  assign_if_present(internals, "default_max_tokens", defaults.max_tokens);
7349
8269
  assign_if_present(internals, "default_top_p", defaults.top_p);
@@ -7351,6 +8271,7 @@ function build_internals(base, defaults) {
7351
8271
  assign_if_present(internals, "default_schema_repair_attempts", defaults.schema_repair_attempts);
7352
8272
  assign_if_present(internals, "default_tool_call_repair_attempts", defaults.tool_call_repair_attempts);
7353
8273
  assign_if_present(internals, "default_max_tool_calls_per_step", defaults.max_tool_calls_per_step);
8274
+ assign_if_present(internals, "default_truncated_turn_policy", defaults.truncated_turn_policy);
7354
8275
  assign_if_present(internals, "default_turn_timeout_ms", defaults.turn_timeout_ms);
7355
8276
  assign_if_present(internals, "default_ai_sdk_telemetry", defaults.ai_sdk_telemetry);
7356
8277
  assign_if_present(internals, "default_provider_options", defaults.provider_options);
@@ -7370,13 +8291,19 @@ function create_engine(config) {
7370
8291
  const pricing = merge_pricing(config.pricing);
7371
8292
  const { default_retry, default_effort, default_max_steps } = resolve_default_scalars(config);
7372
8293
  validate_numeric_defaults(config.defaults);
7373
- const get_internals = () => build_internals({
7374
- pricing,
7375
- adapters,
7376
- default_retry,
7377
- default_effort,
7378
- default_max_steps
7379
- }, config.defaults);
8294
+ assert_cache_setting(config.defaults?.cache, "defaults.cache");
8295
+ assert_capture_level(config.defaults?.capture, "defaults.capture");
8296
+ assert_truncated_turn_policy(config.defaults?.truncated_turn_policy, "defaults.truncated_turn_policy");
8297
+ const get_internals = () => ({
8298
+ ...build_internals({
8299
+ pricing,
8300
+ adapters,
8301
+ default_retry,
8302
+ default_effort,
8303
+ default_max_steps
8304
+ }, config.defaults),
8305
+ redact_capture: config.redact_capture
8306
+ });
7380
8307
  let disposed = false;
7381
8308
  let dispose_promise;
7382
8309
  return {
@@ -7418,6 +8345,6 @@ function create_engine(config) {
7418
8345
  };
7419
8346
  }
7420
8347
  //#endregion
7421
- export { create_engine as t };
8348
+ export { to_json_schema as n, create_engine as t };
7422
8349
 
7423
- //# sourceMappingURL=engine-B6rIIM4J.js.map
8350
+ //# sourceMappingURL=engine-Bde18P7H.js.map