fascicle 0.16.5 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/CHANGELOG.md +49 -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 +6 -1
  6. package/dist/acp.js.map +1 -1
  7. package/dist/agents.d.ts +1 -1
  8. package/dist/{engine-B6rIIM4J.js → engine-BMdRqWL2.js} +988 -218
  9. package/dist/engine-BMdRqWL2.js.map +1 -0
  10. package/dist/index.d.ts +108 -16
  11. package/dist/index.js +225 -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/testing.d.ts +77 -2
  23. package/dist/testing.js +202 -19
  24. package/dist/testing.js.map +1 -1
  25. package/dist/{types-Sw0oHW-_.d.ts → types-BNgAUQP1.d.ts} +170 -17
  26. package/dist/viewer-app/assets/index-9eVv6Mar.css +1 -0
  27. package/dist/viewer-app/assets/index-BkS2ZQu0.js +4 -0
  28. package/dist/viewer-app/index.html +2 -2
  29. package/dist/viewer.d.ts +37 -10
  30. package/dist/viewer.js +101 -46
  31. package/dist/viewer.js.map +1 -1
  32. package/package.json +1 -1
  33. package/dist/engine-B6rIIM4J.js.map +0 -1
  34. package/dist/invoke-LbzZLZFw.js.map +0 -1
  35. package/dist/leading_system-WhW76tRn.js.map +0 -1
  36. package/dist/viewer-app/assets/index-Cev83tcH.css +0 -1
  37. 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,50 @@ 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
+ /**
4483
4772
  * Record that a requested reasoning-effort setting was ignored because the
4484
4773
  * model does not support it.
4485
4774
  */
@@ -4550,18 +4839,20 @@ function record_tool_approval(trajectory, kind, meta) {
4550
4839
  });
4551
4840
  }
4552
4841
  /**
4553
- * Build the per-call dedup that emits `option_ignored` once per option key.
4842
+ * Build the per-call dedup that emits `option_ignored` once per option key,
4843
+ * with a `reason` field only when the emit passes one.
4554
4844
  */
4555
4845
  function create_option_ignored_dedup(trajectory) {
4556
4846
  const seen = /* @__PURE__ */ new Set();
4557
- return { emit(option, provider) {
4847
+ return { emit(option, provider, reason) {
4558
4848
  if (trajectory === void 0) return;
4559
4849
  if (seen.has(option)) return;
4560
4850
  seen.add(option);
4561
4851
  trajectory.record({
4562
4852
  kind: "option_ignored",
4563
4853
  option,
4564
- provider
4854
+ provider,
4855
+ ...reason !== void 0 ? { reason } : {}
4565
4856
  });
4566
4857
  } };
4567
4858
  }
@@ -4758,7 +5049,9 @@ async function run_cli(spawn_runtime, args) {
4758
5049
  /**
4759
5050
  * Emit `option_ignored` events for the loop-control options this provider
4760
5051
  * cannot honor: the CLI runs its own tool loop on the far side of the
4761
- * subprocess boundary.
5052
+ * subprocess boundary, and Claude Code caches its own prompts and builds its
5053
+ * own requests, so a per-call `cache` or `capture`, `'off'` included, is
5054
+ * ignored too.
4762
5055
  */
4763
5056
  function emit_ignored_options(opts, trajectory) {
4764
5057
  const option_ignored = create_option_ignored_dedup(trajectory);
@@ -4767,6 +5060,8 @@ function emit_ignored_options(opts, trajectory) {
4767
5060
  if (opts.on_tool_approval !== void 0) option_ignored.emit("on_tool_approval", PROVIDER_NAME);
4768
5061
  if (opts.tool_call_repair_attempts !== void 0) option_ignored.emit("tool_call_repair_attempts", PROVIDER_NAME);
4769
5062
  if (opts.max_tool_calls_per_step !== void 0) option_ignored.emit("max_tool_calls_per_step", PROVIDER_NAME);
5063
+ if (opts.cache !== void 0) option_ignored.emit("cache", PROVIDER_NAME, "the provider caches on its own");
5064
+ if (opts.capture !== void 0) option_ignored.emit("capture", PROVIDER_NAME, "Claude Code builds its own requests");
4770
5065
  }
4771
5066
  /**
4772
5067
  * Reject requests the CLI cannot express: multi-turn user history (one user
@@ -5108,6 +5403,42 @@ function get_provider_factory(name) {
5108
5403
  return factory;
5109
5404
  }
5110
5405
  //#endregion
5406
+ //#region src/engine/first_chunk_stamp.ts
5407
+ /**
5408
+ * Creates the stamp one turn's attempts share, cleared by `reset` as each
5409
+ * attempt starts, so a failed attempt's chunks never count for the one that
5410
+ * returns.
5411
+ */
5412
+ function create_first_chunk_stamp() {
5413
+ let at;
5414
+ let open_call;
5415
+ let deltas = 0;
5416
+ let whole = false;
5417
+ const observe_first = (chunk) => {
5418
+ at = Date.now();
5419
+ if (chunk.kind === "tool_call_end") whole = true;
5420
+ if (chunk.kind === "tool_call_start") open_call = chunk.id;
5421
+ };
5422
+ return {
5423
+ reset: () => {
5424
+ at = void 0;
5425
+ open_call = void 0;
5426
+ deltas = 0;
5427
+ whole = false;
5428
+ },
5429
+ observe: (chunk) => {
5430
+ if (at === void 0) {
5431
+ observe_first(chunk);
5432
+ return;
5433
+ }
5434
+ if (chunk.kind === "tool_call_input_delta" && chunk.id === open_call) deltas += 1;
5435
+ if (chunk.kind === "tool_call_end" && chunk.id === open_call) whole = deltas <= 1;
5436
+ },
5437
+ at: () => at,
5438
+ whole_tool_call: () => whole
5439
+ };
5440
+ }
5441
+ //#endregion
5111
5442
  //#region src/engine/merge_defaults.ts
5112
5443
  /**
5113
5444
  * Default-merging helpers for engine-level `EngineDefaults`.
@@ -5179,13 +5510,15 @@ function create_chunk_dispatcher(on_chunk) {
5179
5510
  const OPTIONAL_FIELDS = [
5180
5511
  "reasoning_tokens",
5181
5512
  "cached_input_tokens",
5182
- "cache_write_tokens"
5513
+ "cache_write_tokens",
5514
+ "cache_write_1h_tokens"
5183
5515
  ];
5184
5516
  /**
5185
5517
  * Sum token usage across every step of a generate call.
5186
5518
  *
5187
5519
  * `input_tokens` and `output_tokens` always total. The optional fields
5188
- * (`reasoning_tokens`, `cached_input_tokens`, `cache_write_tokens`) stay
5520
+ * (`reasoning_tokens`, `cached_input_tokens`, `cache_write_tokens`,
5521
+ * `cache_write_1h_tokens`) stay
5189
5522
  * absent on the result unless at least one step reported them; once
5190
5523
  * present, every step contributes to the sum, with steps that omit the
5191
5524
  * field counting as 0.
@@ -5196,12 +5529,14 @@ function sum_usage(steps) {
5196
5529
  const present = {
5197
5530
  reasoning_tokens: false,
5198
5531
  cached_input_tokens: false,
5199
- cache_write_tokens: false
5532
+ cache_write_tokens: false,
5533
+ cache_write_1h_tokens: false
5200
5534
  };
5201
5535
  const sums = {
5202
5536
  reasoning_tokens: 0,
5203
5537
  cached_input_tokens: 0,
5204
- cache_write_tokens: 0
5538
+ cache_write_tokens: 0,
5539
+ cache_write_1h_tokens: 0
5205
5540
  };
5206
5541
  for (const step of steps) {
5207
5542
  input_tokens += step.usage.input_tokens;
@@ -5771,14 +6106,16 @@ async function dispatch_tool_result_chunk(dispatch_chunk, step_index, id, output
5771
6106
  }
5772
6107
  /**
5773
6108
  * Build the `role: 'tool'` message that feeds a tool's result back to the
5774
- * model, JSON-serializing non-string content.
6109
+ * model, JSON-serializing non-string content. A failed call's message carries
6110
+ * `is_error: true`; a success's carries no such key.
5775
6111
  */
5776
- function build_tool_result_message(tool_call_id, tool_name, content) {
6112
+ function build_tool_result_message(tool_call_id, tool_name, content, is_error = false) {
5777
6113
  return {
5778
6114
  role: "tool",
5779
6115
  tool_call_id,
5780
6116
  name: tool_name,
5781
- content: typeof content === "string" ? content : safe_json_stringify(content)
6117
+ content: typeof content === "string" ? content : safe_json_stringify(content),
6118
+ ...is_error ? { is_error: true } : {}
5782
6119
  };
5783
6120
  }
5784
6121
  /**
@@ -5906,8 +6243,8 @@ async function validate_tool_input(tool, input) {
5906
6243
  * config.messages unchanged. The replacement is ephemeral; it is fed ONLY to
5907
6244
  * invoke_once for this turn, never pushed onto config.messages, so the loop's
5908
6245
  * 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.
6246
+ * transcript. A step_prepared event makes the mid-loop mutation legible
6247
+ * with the before/after message counts.
5911
6248
  */
5912
6249
  async function apply_prepare_step(config, step_index) {
5913
6250
  if (config.prepare_step === void 0) return config.messages;
@@ -5926,10 +6263,12 @@ async function apply_prepare_step(config, step_index) {
5926
6263
  }
5927
6264
  /**
5928
6265
  * Assemble the loop's return value from the accumulated steps and records.
6266
+ * The text is the final turn's, after the texts of any paused turns that it
6267
+ * continued.
5929
6268
  */
5930
6269
  function loop_result(acc, text, finish_reason, max_steps_reached) {
5931
6270
  return {
5932
- text,
6271
+ text: [...acc.paused_texts, text].join(""),
5933
6272
  steps: acc.steps,
5934
6273
  tool_calls: acc.tool_calls,
5935
6274
  finish_reason,
@@ -5937,16 +6276,248 @@ function loop_result(acc, text, finish_reason, max_steps_reached) {
5937
6276
  };
5938
6277
  }
5939
6278
  /**
6279
+ * Continue a turn the provider paused without calls: send its assistant
6280
+ * message back as it is, with nothing after it, which is how a paused
6281
+ * response resumes, and ask for another turn, which counts toward
6282
+ * `max_steps`. A pause on the last allowed step ends the loop with
6283
+ * `'max_steps'` and the paused turn's text. Salvage and the clamp never see a
6284
+ * paused turn.
6285
+ */
6286
+ function continue_paused_turn(config, acc, step_index, step_span, turn) {
6287
+ const view = {
6288
+ calls: [],
6289
+ history_text: turn.text,
6290
+ formats: /* @__PURE__ */ new Map(),
6291
+ outcome: void 0
6292
+ };
6293
+ config.messages.push(build_assistant_message(turn, view, []));
6294
+ const at_cap = step_index + 1 >= config.max_steps;
6295
+ push_step_record(config, acc, step_index, turn, [], at_cap ? "max_steps" : turn.finish_reason, step_span);
6296
+ if (at_cap) return loop_result(acc, turn.text, "max_steps", true);
6297
+ acc.paused_texts.push(turn.text);
6298
+ }
6299
+ /**
6300
+ * Record a `cache_miss` when the previous turn read or wrote cache tokens
6301
+ * and this one read none, then remember this turn's reads plus writes for
6302
+ * the next. The rule needs no table of model minimums and no knowledge of
6303
+ * the provider.
6304
+ */
6305
+ function note_cache_traffic(config, memory, usage, turn) {
6306
+ const reads = usage.cached_input_tokens ?? 0;
6307
+ if (memory.previous_cache_traffic > 0 && reads === 0) record_cache_miss(config.trajectory, {
6308
+ ...turn,
6309
+ previous_cached_tokens: memory.previous_cache_traffic,
6310
+ input_tokens: usage.input_tokens
6311
+ });
6312
+ memory.previous_cache_traffic = reads + (usage.cache_write_tokens ?? 0);
6313
+ }
6314
+ /**
6315
+ * The call's capture when it records its turns' requests and replies, and
6316
+ * undefined when it records neither.
6317
+ */
6318
+ function active_capture(config) {
6319
+ const capture = config.capture;
6320
+ return capture !== void 0 && capture.level !== "off" ? capture : void 0;
6321
+ }
6322
+ /**
6323
+ * JSON.stringify's replacer for a captured value: a `Uint8Array`, a Node
6324
+ * `Buffer` included, becomes its base64 text, read off the holder because a
6325
+ * Buffer's own toJSON has already run by the time the replacer sees it.
6326
+ */
6327
+ function capture_replacer(key, value) {
6328
+ const raw = this[key];
6329
+ return raw instanceof Uint8Array ? Buffer.from(raw).toString("base64") : value;
6330
+ }
6331
+ /**
6332
+ * The JSON text a captured value is recorded as. Throws on a value that JSON
6333
+ * can't hold, a BigInt or a cycle.
6334
+ */
6335
+ function capture_text(value) {
6336
+ return JSON.stringify(value, capture_replacer);
6337
+ }
6338
+ /**
6339
+ * A fresh, JSON-safe copy of a value, as capture records it.
6340
+ */
6341
+ function capture_copy(value) {
6342
+ return JSON.parse(capture_text(value));
6343
+ }
6344
+ /**
6345
+ * A schema as JSON Schema with `to_json_schema`'s defaults, or undefined when
6346
+ * it can't be written as one.
6347
+ */
6348
+ function json_schema_or_undefined(schema) {
6349
+ let json;
6350
+ try {
6351
+ json = to_json_schema(schema);
6352
+ } catch {}
6353
+ return json;
6354
+ }
6355
+ /**
6356
+ * A tool as capture records it: its name, its description, and its input
6357
+ * schema as JSON Schema. A schema that can't be written as one stays
6358
+ * undefined, which the JSON copy leaves out.
6359
+ */
6360
+ function captured_tool(tool) {
6361
+ return {
6362
+ name: tool.name,
6363
+ description: tool.description,
6364
+ input_schema: json_schema_or_undefined(tool.input_schema)
6365
+ };
6366
+ }
6367
+ /**
6368
+ * A later turn's request: the messages it appended when it starts with
6369
+ * every message the previous captured turn held, and its whole array marked
6370
+ * `reshaped` otherwise, or when the previous turn's payload was dropped.
6371
+ */
6372
+ function later_request(previous, texts, copies) {
6373
+ if (previous?.every((text, n) => texts[n] === text) === true) return { appended: copies.slice(previous.length) };
6374
+ return {
6375
+ messages: copies,
6376
+ reshaped: true
6377
+ };
6378
+ }
6379
+ /**
6380
+ * The `request` a turn's request_sent carries under capture, built from
6381
+ * fresh JSON-safe copies of what the turn sends, so the loop's later pushes
6382
+ * never reach it. A call's first turn records its settings, its tools, and
6383
+ * every message; a later one records what changed (see later_request). The
6384
+ * messages' JSON text is remembered for the next turn. A value JSON can't
6385
+ * hold drops the payload, and the call goes on.
6386
+ */
6387
+ function capture_request(config, capture, memory, step_index, messages) {
6388
+ const previous = memory.captured_messages;
6389
+ memory.captured_messages = void 0;
6390
+ let request;
6391
+ try {
6392
+ const texts = messages.map(capture_text);
6393
+ const copies = texts.map((text) => JSON.parse(text));
6394
+ request = step_index === 0 ? {
6395
+ settings: capture_copy(capture.settings()),
6396
+ tools: capture_copy(config.tools.map(captured_tool)),
6397
+ messages: copies
6398
+ } : later_request(previous, texts, copies);
6399
+ memory.captured_messages = texts;
6400
+ } catch {}
6401
+ return request;
6402
+ }
6403
+ /**
6404
+ * The parts a turn returned, as response_received records them under
6405
+ * capture: the turn's own parts when its adapter set them, and otherwise its
6406
+ * text, when it has any, followed by its calls. A value JSON can't hold
6407
+ * drops them.
6408
+ */
6409
+ function capture_response(turn) {
6410
+ const parts = turn.content ?? [...turn.text.length > 0 ? [{
6411
+ type: "text",
6412
+ text: turn.text
6413
+ }] : [], ...turn.tool_calls.map(tool_call_part)];
6414
+ let content;
6415
+ try {
6416
+ content = capture_copy(parts);
6417
+ } catch {}
6418
+ return content;
6419
+ }
6420
+ const REDACTION_FAILED = Symbol("redaction_failed");
6421
+ /**
6422
+ * True for a promise, or any other object with a `then` method.
6423
+ */
6424
+ function is_thenable(value) {
6425
+ return typeof value === "object" && value !== null && typeof Reflect.get(value, "then") === "function";
6426
+ }
6427
+ /**
6428
+ * Hand a payload to the redactor, and return what it returned, or
6429
+ * REDACTION_FAILED when it threw or returned a thenable. The engine records
6430
+ * synchronously, so it can't wait for a thenable, and a rejection it leaves
6431
+ * behind is swallowed rather than left unhandled.
6432
+ */
6433
+ function apply_redactor(redact, payload) {
6434
+ try {
6435
+ const recorded = redact(payload);
6436
+ if (!is_thenable(recorded)) return recorded;
6437
+ Promise.resolve(recorded).catch(() => void 0);
6438
+ } catch {}
6439
+ return REDACTION_FAILED;
6440
+ }
6441
+ /**
6442
+ * What the trajectory records for a captured payload: the payload itself,
6443
+ * or what the engine's redactor returned in its place, `undefined` recording
6444
+ * nothing. A payload that couldn't be built (`data` undefined), and a
6445
+ * redactor that failed, record nothing and say so with
6446
+ * `capture_redaction_failed`, so nothing the redactor would have scrubbed is
6447
+ * written.
6448
+ */
6449
+ function redact_payload(config, capture, payload, span_id) {
6450
+ const recorded = payload.data === void 0 ? REDACTION_FAILED : capture.redact === void 0 ? payload.data : apply_redactor(capture.redact, payload);
6451
+ if (recorded !== REDACTION_FAILED) return recorded;
6452
+ record_capture_redaction_failed(config.trajectory, {
6453
+ step_index: payload.step_index,
6454
+ span_id,
6455
+ payload_kind: payload.kind
6456
+ });
6457
+ }
6458
+ /**
6459
+ * Record the exact body a native adapter is about to send, from a fresh copy
6460
+ * of `{ url, body }` that goes through the redactor, so nothing the redactor
6461
+ * does reaches the wire. The event carries the `url` and `body` of the object
6462
+ * the redactor returns, leaving out either one it dropped. A redactor that
6463
+ * returns anything other than an object fails closed.
6464
+ */
6465
+ function record_wire(config, capture, turn, wire) {
6466
+ let data;
6467
+ try {
6468
+ data = capture_copy({
6469
+ url: wire.url,
6470
+ body: wire.body
6471
+ });
6472
+ } catch {}
6473
+ const recorded = redact_payload(config, capture, {
6474
+ kind: "wire",
6475
+ step_index: turn.step_index,
6476
+ data
6477
+ }, turn.span_id);
6478
+ if (recorded === void 0) return;
6479
+ if (!is_record$1(recorded)) {
6480
+ record_capture_redaction_failed(config.trajectory, {
6481
+ ...turn,
6482
+ payload_kind: "wire"
6483
+ });
6484
+ return;
6485
+ }
6486
+ record_request_wire(config.trajectory, {
6487
+ ...turn,
6488
+ provider: config.provider,
6489
+ recorded
6490
+ });
6491
+ }
6492
+ /**
6493
+ * The `on_wire` a turn hands its adapter under capture at `'wire'`.
6494
+ */
6495
+ function wire_recorder(config, capture, turn) {
6496
+ return (wire) => {
6497
+ record_wire(config, capture, turn, wire);
6498
+ };
6499
+ }
6500
+ /**
5940
6501
  * 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.
6502
+ * response_received events, which carry the request and the reply under
6503
+ * capture, and note its cache traffic in `memory`. A thrown invoke (or
6504
+ * prepare_step) closes the step span with the error before rethrowing.
5943
6505
  */
5944
- async function invoke_turn(config, step_index, step_span) {
6506
+ async function invoke_turn(config, memory, step_index, step_span) {
5945
6507
  try {
5946
6508
  const request_messages = await apply_prepare_step(config, step_index);
5947
- config.trajectory?.record({
5948
- kind: "request_sent",
5949
- step_index
6509
+ const capture = active_capture(config);
6510
+ const request = capture !== void 0 ? redact_payload(config, capture, {
6511
+ kind: "request",
6512
+ step_index,
6513
+ data: capture_request(config, capture, memory, step_index, request_messages)
6514
+ }, step_span) : void 0;
6515
+ if (request === void 0) memory.captured_messages = void 0;
6516
+ record_request_sent(config.trajectory, {
6517
+ step_index,
6518
+ span_id: step_span,
6519
+ call_span_id: config.capture?.call_span_id,
6520
+ request
5950
6521
  });
5951
6522
  const turn = await config.invoke_once({
5952
6523
  step_index,
@@ -5954,9 +6525,22 @@ async function invoke_turn(config, step_index, step_span) {
5954
6525
  tools: config.tools,
5955
6526
  abort: config.abort,
5956
6527
  stream: config.stream,
5957
- step_span
6528
+ step_span,
6529
+ ...capture?.level === "wire" ? { on_wire: wire_recorder(config, capture, {
6530
+ step_index,
6531
+ span_id: step_span
6532
+ }) } : {}
6533
+ });
6534
+ record_response_received(config.trajectory, step_index, turn, step_span, capture !== void 0 ? redact_payload(config, capture, {
6535
+ kind: "response",
6536
+ step_index,
6537
+ data: capture_response(turn)
6538
+ }, step_span) : void 0);
6539
+ note_cache_traffic(config, memory, turn.usage, {
6540
+ step_index,
6541
+ span_id: step_span,
6542
+ after_prepare_step: request_messages !== config.messages
5958
6543
  });
5959
- record_response_received(config.trajectory, step_index, turn, step_span);
5960
6544
  return turn;
5961
6545
  } catch (err) {
5962
6546
  const message = err instanceof Error ? err.message : String(err);
@@ -6080,7 +6664,7 @@ async function record_failed_call(ctx, call, message, timing) {
6080
6664
  };
6081
6665
  ctx.records.push(record);
6082
6666
  ctx.all_records.push(record);
6083
- ctx.feed.push(build_tool_result_message(call.id, call.name, { error: message }));
6667
+ ctx.feed.push(build_tool_result_message(call.id, call.name, { error: message }, true));
6084
6668
  record_tool_call(ctx.config.trajectory, {
6085
6669
  step_index: ctx.step_index,
6086
6670
  name: call.name,
@@ -6369,7 +6953,11 @@ function push_step_record(config, acc, step_index, turn, tool_calls, finish_reas
6369
6953
  */
6370
6954
  async function run_loop_step(config, tool_map, acc, step_index) {
6371
6955
  const step_span = start_step_span(config.trajectory, step_index);
6372
- const turn = await invoke_turn(config, step_index, step_span);
6956
+ const turn = await invoke_turn(config, acc.memory, step_index, step_span);
6957
+ if (turn.paused === true) {
6958
+ record_turn_paused(config.trajectory, step_index, step_span);
6959
+ if (turn.tool_calls.length === 0) return continue_paused_turn(config, acc, step_index, step_span, turn);
6960
+ }
6373
6961
  const view = await salvage_turn(config, turn, tool_map, step_index);
6374
6962
  const { kept, dropped } = clamp_calls(config, view.calls, step_index);
6375
6963
  config.messages.push(build_assistant_message(turn, view, kept));
@@ -6397,6 +6985,7 @@ async function run_loop_step(config, tool_map, acc, step_index) {
6397
6985
  push_step_record(config, acc, step_index, turn, ctx.records, turn_finish_reason, step_span);
6398
6986
  if (ctx.terminal_fired) return loop_result(acc, turn.text, "stop", false);
6399
6987
  if (ctx.would_exceed_after) return loop_result(acc, turn.text, "max_steps", true);
6988
+ acc.paused_texts.length = 0;
6400
6989
  }
6401
6990
  /**
6402
6991
  * Run the model-turn / tool-execution loop to completion.
@@ -6409,7 +6998,9 @@ async function run_loop_step(config, tool_map, acc, step_index) {
6409
6998
  async function run_tool_loop(config) {
6410
6999
  const acc = {
6411
7000
  steps: [],
6412
- tool_calls: []
7001
+ tool_calls: [],
7002
+ paused_texts: [],
7003
+ memory: config.call_memory ?? { previous_cache_traffic: 0 }
6413
7004
  };
6414
7005
  const tool_map = /* @__PURE__ */ new Map();
6415
7006
  for (const t of config.tools) tool_map.set(t.name, t);
@@ -6470,7 +7061,10 @@ const NETWORK_ERROR_CODES = /* @__PURE__ */ new Set([
6470
7061
  "ECONNRESET",
6471
7062
  "ETIMEDOUT",
6472
7063
  "ENOTFOUND",
6473
- "ECONNREFUSED"
7064
+ "ECONNREFUSED",
7065
+ "UND_ERR_HEADERS_TIMEOUT",
7066
+ "UND_ERR_BODY_TIMEOUT",
7067
+ "UND_ERR_SOCKET"
6474
7068
  ]);
6475
7069
  /**
6476
7070
  * True when `err` already carries a `kind` this layer treats as retryable, so
@@ -6508,25 +7102,35 @@ function retry_after_from_error(err) {
6508
7102
  return typeof hv === "string" ? parse_retry_after(hv) : void 0;
6509
7103
  }
6510
7104
  /**
6511
- * Build the `rate_limit` classification for a 429, attaching the message and a
6512
- * parsed `Retry-After` when either is present.
7105
+ * Build the classification for a retryable status (`rate_limit` for a 429,
7106
+ * `provider_5xx` for a 5xx), attaching the message and a parsed `Retry-After`
7107
+ * when either is present. HTTP defines the header for a 503 as well as a 429,
7108
+ * so a server's wait counts on both.
6513
7109
  */
6514
- function classify_rate_limit(err) {
7110
+ function classify_status(err, kind, status) {
6515
7111
  const out = with_error_message(err, {
6516
- kind: "rate_limit",
6517
- status: 429
7112
+ kind,
7113
+ status
6518
7114
  });
6519
7115
  const retry_after_ms = retry_after_from_error(err);
6520
7116
  if (retry_after_ms !== void 0) out["retry_after_ms"] = retry_after_ms;
6521
7117
  return out;
6522
7118
  }
6523
7119
  /**
6524
- * Build the `network` classification when `err.code` is a known network error
6525
- * code, or `undefined` when it is not.
7120
+ * True when `value` carries a `code` that names a known network failure.
7121
+ */
7122
+ function has_network_code(value) {
7123
+ if (value === null || typeof value !== "object") return false;
7124
+ const code = Reflect.get(value, "code");
7125
+ return typeof code === "string" && NETWORK_ERROR_CODES.has(code);
7126
+ }
7127
+ /**
7128
+ * Build the `network` classification when the error or its `cause` carries a
7129
+ * known network error code, or `undefined` when neither does. The AI SDK
7130
+ * wraps a failed fetch in an `APICallError` whose `cause` holds undici's code.
6526
7131
  */
6527
7132
  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;
7133
+ if (!has_network_code(err) && !has_network_code(Reflect.get(err, "cause"))) return void 0;
6530
7134
  return with_error_message(err, { kind: "network" });
6531
7135
  }
6532
7136
  /**
@@ -6534,21 +7138,20 @@ function classify_network_error(err) {
6534
7138
  * understands.
6535
7139
  *
6536
7140
  * 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.
7141
+ * Otherwise inspects `statusCode`/`status`: 429 becomes `rate_limit` and 5xx
7142
+ * becomes `provider_5xx`, each reading `Retry-After` off `responseHeaders`
7143
+ * when present, and a known network error code (`ECONNRESET`, `ETIMEDOUT`,
7144
+ * `ENOTFOUND`, `ECONNREFUSED`, or undici's `UND_ERR_HEADERS_TIMEOUT`,
7145
+ * `UND_ERR_BODY_TIMEOUT`, and `UND_ERR_SOCKET`), on the error or on its
7146
+ * `cause`, becomes `network`. Anything else passes through unclassified,
7147
+ * which `retry_with_policy` treats as non-retryable.
6542
7148
  */
6543
7149
  function classify_provider_error(err) {
6544
7150
  if (err === null || typeof err !== "object") return err;
6545
7151
  if (has_retry_kind(err)) return err;
6546
7152
  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
- });
7153
+ if (status === 429) return classify_status(err, "rate_limit", status);
7154
+ if (status !== void 0 && status >= 500 && status < 600) return classify_status(err, "provider_5xx", status);
6552
7155
  return classify_network_error(err) ?? err;
6553
7156
  }
6554
7157
  /**
@@ -6586,34 +7189,36 @@ function arm_turn_timeout(user_abort, turn_timeout_ms) {
6586
7189
  * Engine-owned wrapper shared by both transports: one turn attempt inside
6587
7190
  * retry_with_policy. The catch ladder classifies by CAUSE, not
6588
7191
  * 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.
7192
+ * then a genuine user abort wins, then any error once a chunk has reached a
7193
+ * consumer is a non-retryable stream interruption, then a `turn_timeout_ms`
7194
+ * expiry is a retryable typed timeout, and only a below-loop aborted_error
7195
+ * unrelated to either passes through. With no consumer attached, nothing was
7196
+ * delivered that a retry would repeat, so a mid-stream failure, a deadline
7197
+ * expiry included, is classified as one before any chunk is. Cause-first
7198
+ * ordering is what keeps the two transports in parity: the ai_sdk transport
7199
+ * reports a mid-stream abort as an aborted_error and the native one as a raw
7200
+ * AbortError, so both must be read as the same interruption rather than the
7201
+ * ai_sdk timeout masquerading as a user cancel. call_once receives the
7202
+ * composed abort+timeout signal so the deadline actually cancels the
7203
+ * in-flight request. Adapters may swap `classify`, but never the ladder
7204
+ * itself: the engine owns retry, so a hidden adapter-level retry would be
7205
+ * invisible to anything relying on this ladder.
6600
7206
  *
6601
7207
  * The wrapper also stamps StepTiming here, and only here, because this is the
6602
7208
  * one place that can see attempt boundaries: started_at re-stamps at every
6603
7209
  * 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.
7210
+ * duration_ms. The builder's first-chunk stamp belongs to the returning
7211
+ * attempt, since the builder clears it as each attempt starts.
6607
7212
  */
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;
7213
+ function retry_turn(call_once, args, stamp, retry_policy, classify, turn_timeout_ms, trajectory, on_chunk_provided) {
7214
+ const has_streamed = () => on_chunk_provided && stamp.at() !== void 0;
6610
7215
  return retry_with_policy(async () => {
6611
7216
  const started_at = Date.now();
6612
7217
  const deadline = arm_turn_timeout(args.abort, turn_timeout_ms);
6613
7218
  try {
6614
7219
  return {
6615
7220
  ...await call_once(deadline.signal),
6616
- timing: build_step_timing(started_at, first_chunk_at())
7221
+ timing: build_step_timing(started_at, stamp)
6617
7222
  };
6618
7223
  } catch (err) {
6619
7224
  if (err instanceof on_chunk_error) throw err;
@@ -6637,52 +7242,61 @@ function retry_turn(call_once, args, first_chunk_at, retry_policy, classify, tur
6637
7242
  }
6638
7243
  /**
6639
7244
  * Assemble one turn's StepTiming from the attempt's start stamp and the
6640
- * builder's first-chunk stamp (undefined on non-streamed turns).
7245
+ * builder's first-chunk stamp, which holds nothing on a non-streamed turn.
6641
7246
  */
6642
- function build_step_timing(started_at, first_chunk) {
7247
+ function build_step_timing(started_at, stamp) {
6643
7248
  const timing = {
6644
7249
  started_at,
6645
7250
  duration_ms: Date.now() - started_at
6646
7251
  };
7252
+ const first_chunk = stamp.at();
6647
7253
  if (first_chunk !== void 0) timing.first_chunk_ms = first_chunk - started_at;
7254
+ if (stamp.whole_tool_call()) timing.whole_tool_call = true;
6648
7255
  return timing;
6649
7256
  }
6650
7257
  /**
6651
7258
  * Build the ai_sdk-transport InvokeOnce: the SDK turn built by
6652
7259
  * 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).
7260
+ * the native path uses. The SDK call body lives behind the seam, and every
7261
+ * turn streams; this builder owns only retry and the first-chunk stamp, which
7262
+ * it clears as each attempt starts, and which both makes a failure after a
7263
+ * chunk reached a consumer a non-retryable interruption and doubles as
7264
+ * StepTiming.first_chunk_ms.
6657
7265
  */
6658
7266
  function build_ai_sdk_invoke(cfg) {
6659
7267
  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);
7268
+ const stamp = create_first_chunk_stamp();
7269
+ const call_once = (turn_abort) => {
7270
+ stamp.reset();
7271
+ return cfg.invoke_turn({
7272
+ step_index: args.step_index,
7273
+ messages: args.messages,
7274
+ abort: turn_abort,
7275
+ observe_chunk: stamp.observe
7276
+ });
7277
+ };
7278
+ 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
7279
  };
6672
7280
  }
6673
7281
  /**
6674
7282
  * Build the native-transport InvokeOnce: maps the loop's InvokeOnceArgs to a
6675
7283
  * 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.
7284
+ * wrapper as the ai_sdk path. Every turn asks for a stream when the adapter
7285
+ * supports streaming, whether or not a consumer is attached, so a long turn's
7286
+ * headers arrive before fetch's 300-second limit, and the adapter gets a
7287
+ * `dispatch_chunk` that stamps the first chunk of each attempt and hands
7288
+ * every chunk to the shared dispatcher, which passes it on only to a
7289
+ * consumer. The adapter sees a child abort signal so a throwing on_chunk
7290
+ * consumer cancels the in-flight request, which is what lets run_tool_loop
7291
+ * treat both transports identically.
6680
7292
  */
6681
7293
  function build_native_invoke(cfg) {
6682
7294
  const classify = cfg.adapter.classify_error ?? classify_provider_error;
7295
+ const streams = cfg.adapter.supports("streaming");
6683
7296
  return async (args) => {
6684
- let first_chunk_at;
7297
+ const stamp = create_first_chunk_stamp();
6685
7298
  const call_once = async (turn_abort) => {
7299
+ stamp.reset();
6686
7300
  const internal_controller = new AbortController();
6687
7301
  const cancel_on_turn_abort = () => {
6688
7302
  internal_controller.abort(turn_abort.reason);
@@ -6690,7 +7304,7 @@ function build_native_invoke(cfg) {
6690
7304
  if (turn_abort.aborted) internal_controller.abort(turn_abort.reason);
6691
7305
  else turn_abort.addEventListener("abort", cancel_on_turn_abort, { once: true });
6692
7306
  const dispatch_chunk = async (chunk) => {
6693
- first_chunk_at ??= Date.now();
7307
+ stamp.observe(chunk);
6694
7308
  try {
6695
7309
  await cfg.dispatcher.dispatch(chunk);
6696
7310
  } catch (err) {
@@ -6704,16 +7318,18 @@ function build_native_invoke(cfg) {
6704
7318
  messages,
6705
7319
  tools: args.tools,
6706
7320
  abort: internal_controller.signal,
6707
- stream: args.stream,
7321
+ stream: streams,
6708
7322
  model_id: cfg.model_id,
6709
7323
  effort: cfg.effort,
7324
+ ...cfg.cache !== "off" ? { cache: cfg.cache } : {},
6710
7325
  ...system !== void 0 ? { system } : {},
6711
7326
  ...cfg.schema !== void 0 ? { schema: cfg.schema } : {},
6712
7327
  ...cfg.provider_options !== void 0 ? { provider_options: cfg.provider_options } : {},
6713
7328
  ...cfg.temperature !== void 0 ? { temperature: cfg.temperature } : {},
6714
7329
  ...cfg.max_tokens !== void 0 ? { max_tokens: cfg.max_tokens } : {},
6715
7330
  ...cfg.top_p !== void 0 ? { top_p: cfg.top_p } : {},
6716
- ...args.stream ? { dispatch_chunk } : {}
7331
+ ...streams ? { dispatch_chunk } : {},
7332
+ ...args.on_wire !== void 0 ? { on_wire: args.on_wire } : {}
6717
7333
  };
6718
7334
  try {
6719
7335
  return await cfg.adapter.invoke_turn(req);
@@ -6721,10 +7337,60 @@ function build_native_invoke(cfg) {
6721
7337
  turn_abort.removeEventListener("abort", cancel_on_turn_abort);
6722
7338
  }
6723
7339
  };
6724
- return await retry_turn(call_once, args, () => first_chunk_at, cfg.retry_policy, classify, cfg.turn_timeout_ms, cfg.trajectory);
7340
+ return await retry_turn(call_once, args, stamp, cfg.retry_policy, classify, cfg.turn_timeout_ms, cfg.trajectory, cfg.on_chunk_provided);
6725
7341
  };
6726
7342
  }
6727
7343
  /**
7344
+ * Throw `engine_config_error` before any request when two or more of a call's
7345
+ * tools share a name, naming each duplicate with its count. The loop and
7346
+ * every transport key tools by name, so a duplicate would silently replace
7347
+ * the tool before it.
7348
+ */
7349
+ function assert_unique_tool_names(tools) {
7350
+ if (tools === void 0) return;
7351
+ const counts = /* @__PURE__ */ new Map();
7352
+ for (const tool of tools) counts.set(tool.name, (counts.get(tool.name) ?? 0) + 1);
7353
+ const duplicates = [...counts].filter(([, count]) => count > 1);
7354
+ if (duplicates.length === 0) return;
7355
+ const listed = duplicates.map(([name, count]) => `${name} (${String(count)})`).join(", ");
7356
+ 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`);
7357
+ }
7358
+ const CACHE_SETTINGS = {
7359
+ off: true,
7360
+ "5m": true,
7361
+ "1h": true
7362
+ };
7363
+ const CAPTURE_LEVELS = {
7364
+ off: true,
7365
+ turns: true,
7366
+ wire: true
7367
+ };
7368
+ /**
7369
+ * Throw `engine_config_error` when `value` is set to anything but a key of
7370
+ * `allowed`, naming the field, the value, and the values it takes.
7371
+ */
7372
+ function assert_allowed(allowed, value, field) {
7373
+ if (value === void 0 || Object.hasOwn(allowed, value)) return;
7374
+ const listed = Object.keys(allowed).map((key) => `'${key}'`).join(", ");
7375
+ throw new engine_config_error(`${field} must be one of ${listed}, got ${JSON.stringify(value)}`);
7376
+ }
7377
+ /**
7378
+ * Throw `engine_config_error` for a cache setting that isn't a
7379
+ * `CacheSetting`. `create_engine` checks `defaults.cache` with it, and
7380
+ * `generate` a call's `cache`.
7381
+ */
7382
+ function assert_cache_setting(value, field) {
7383
+ assert_allowed(CACHE_SETTINGS, value, field);
7384
+ }
7385
+ /**
7386
+ * Throw `engine_config_error` for a capture level that isn't a
7387
+ * `CaptureLevel`. `create_engine` checks `defaults.capture` with it, and
7388
+ * `generate` a call's `capture`.
7389
+ */
7390
+ function assert_capture_level(value, field) {
7391
+ assert_allowed(CAPTURE_LEVELS, value, field);
7392
+ }
7393
+ /**
6728
7394
  * Resolve the provider name: per-call, then the engine default, then the sole
6729
7395
  * configured adapter. With several providers configured and neither a per-call
6730
7396
  * provider nor a default, there is no sane guess, so this throws
@@ -6780,17 +7446,58 @@ function assert_capabilities(adapter, provider, opts, tools_list, on_chunk_provi
6780
7446
  if (on_chunk_provided && !adapter.supports("streaming")) throw new provider_capability_error(provider, "streaming");
6781
7447
  }
6782
7448
  /**
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.
7449
+ * Read the caching an adapter declares. An `ai_sdk` adapter that declares
7450
+ * `'with_tools'` or `'opt_in'` but has no `translate_cache` has no way to put
7451
+ * the setting on the call, so it counts as declaring nothing.
7452
+ */
7453
+ function declared_caching(adapter) {
7454
+ if (adapter.kind === "ai_sdk" && adapter.prompt_cache !== "automatic" && adapter.translate_cache === void 0) return;
7455
+ return adapter.prompt_cache;
7456
+ }
7457
+ /**
7458
+ * Resolve a call's cache setting from the caching its adapter declares and
7459
+ * the setting the call or the engine asked for. `'with_tools'` caches for
7460
+ * five minutes by default on a call with tools, `'opt_in'` only when asked,
7461
+ * and an `'automatic'` or undeclared adapter never, with a reason for
7462
+ * ignoring an explicit `'5m'` or `'1h'`.
7463
+ */
7464
+ function resolve_cache(declared, explicit, has_tools) {
7465
+ if (declared === "with_tools") return { cache: explicit ?? (has_tools ? "5m" : "off") };
7466
+ if (declared === "opt_in") return { cache: explicit ?? "off" };
7467
+ if (explicit === void 0 || explicit === "off") return { cache: "off" };
7468
+ return {
7469
+ cache: "off",
7470
+ ignored_reason: declared === "automatic" ? "the provider caches on its own" : "this adapter doesn't request prompt caching"
7471
+ };
7472
+ }
7473
+ /**
7474
+ * Resolve the capture level a call's loop runs at: the call's, else the
7475
+ * engine default, else `'off'`. On the ai_sdk transport the SDK builds the
7476
+ * body, so `'wire'` records `option_ignored` and runs as `'turns'`.
6786
7477
  */
6787
- function resolve_turn_config(opts, engine) {
7478
+ function resolve_capture(opts, engine, call) {
7479
+ const level = opts.capture ?? engine.default_capture ?? "off";
7480
+ if (level !== "wire" || call.adapter.kind !== "ai_sdk") return level;
7481
+ create_option_ignored_dedup(call.trajectory).emit("capture", call.adapter.name, "wire capture needs a native transport");
7482
+ return "turns";
7483
+ }
7484
+ /**
7485
+ * Resolve the per-turn knobs (effort, cache, capture, retry policy, turn
7486
+ * timeout) against engine defaults. Validated here, before any transport is built, so
7487
+ * a bad budget fails as a config error, never as a spurious timeout. A cache
7488
+ * setting the adapter can't honor records `option_ignored` with its reason.
7489
+ */
7490
+ function resolve_turn_config(opts, engine, call) {
6788
7491
  const effort = opts.effort ?? engine.default_effort;
6789
7492
  const retry_policy = opts.retry ?? engine.default_retry;
6790
7493
  const turn_timeout_ms = opts.turn_timeout_ms ?? engine.default_turn_timeout_ms;
6791
7494
  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)}`);
7495
+ const { cache, ignored_reason } = resolve_cache(declared_caching(call.adapter), opts.cache ?? engine.default_cache, call.tools.length > 0);
7496
+ if (ignored_reason !== void 0) create_option_ignored_dedup(call.trajectory).emit("cache", call.adapter.name, ignored_reason);
6792
7497
  return {
6793
7498
  effort,
7499
+ cache,
7500
+ capture: resolve_capture(opts, engine, call),
6794
7501
  retry_policy,
6795
7502
  turn_timeout_ms
6796
7503
  };
@@ -6825,19 +7532,32 @@ function resolve_loop_limits(opts, engine) {
6825
7532
  };
6826
7533
  }
6827
7534
  /**
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.
7535
+ * Merge two of an adapter's translations into call-level provider options,
7536
+ * key by key under each provider, with `over` winning where both set a key.
7537
+ */
7538
+ function merge_translations(base, over) {
7539
+ const out = { ...base };
7540
+ for (const [provider, inner] of Object.entries(over)) out[provider] = {
7541
+ ...base[provider],
7542
+ ...inner
7543
+ };
7544
+ return out;
7545
+ }
7546
+ /**
7547
+ * Build the ai_sdk-transport InvokeOnce for one call: translate effort and
7548
+ * the cache setting, merge provider options, load the SDK seam, and wrap it
7549
+ * in the engine-owned retry via build_ai_sdk_invoke.
6831
7550
  */
6832
7551
  async function build_ai_sdk_transport(cfg) {
6833
7552
  const { adapter, opts, effort } = cfg;
6834
7553
  const effort_translation = adapter.translate_effort(effort);
6835
7554
  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);
7555
+ const cache_options = adapter.translate_cache?.(cfg.cache, opts.provider_options) ?? {};
7556
+ const combined_provider_options = merge_provider_options(merge_translations(effort_translation.provider_options, cache_options), opts.provider_options);
6837
7557
  const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
6838
7558
  let ai_sdk_invoke_mod;
6839
7559
  try {
6840
- ai_sdk_invoke_mod = await import("./invoke-LbzZLZFw.js");
7560
+ ai_sdk_invoke_mod = await import("./invoke-BbJLQcLl.js");
6841
7561
  } catch (err) {
6842
7562
  throw missing_peer_error("ai", err);
6843
7563
  }
@@ -6858,7 +7578,8 @@ async function build_ai_sdk_transport(cfg) {
6858
7578
  }),
6859
7579
  retry_policy: cfg.retry_policy,
6860
7580
  turn_timeout_ms: cfg.turn_timeout_ms,
6861
- trajectory: cfg.trajectory
7581
+ trajectory: cfg.trajectory,
7582
+ on_chunk_provided: cfg.on_chunk_provided
6862
7583
  });
6863
7584
  }
6864
7585
  /**
@@ -6910,8 +7631,10 @@ function schema_instruction(schema) {
6910
7631
  /**
6911
7632
  * Assemble the ToolLoopConfig base shared by every loop invocation of one
6912
7633
  * 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
7634
+ * the pricing lookup, the pricing_missing dedup, and the call memory that
7635
+ * carries the previous turn's cache traffic and captured messages all live
7636
+ * for exactly one call,
7637
+ * which is what lets schema-repair re-invocations share them. The
6915
7638
  * conditional spreads keep optional fields absent rather than undefined for
6916
7639
  * exactOptionalPropertyTypes.
6917
7640
  */
@@ -6939,6 +7662,8 @@ function build_loop_config(cfg) {
6939
7662
  model_id: target.model_id,
6940
7663
  resolve_pricing,
6941
7664
  pricing_dedup: create_pricing_missing_dedup(cfg.trajectory),
7665
+ call_memory: { previous_cache_traffic: 0 },
7666
+ capture: cfg.capture,
6942
7667
  ...limits.salvage_budget !== void 0 ? { salvage_budget: limits.salvage_budget } : {},
6943
7668
  ...limits.max_tool_calls_per_step !== void 0 ? { max_tool_calls_per_step: limits.max_tool_calls_per_step } : {},
6944
7669
  ...opts.prepare_step !== void 0 ? { prepare_step: opts.prepare_step } : {}
@@ -7045,6 +7770,27 @@ function assemble_result(outcome, target) {
7045
7770
  return result;
7046
7771
  }
7047
7772
  /**
7773
+ * The settings a call's first captured turn records: the provider and the
7774
+ * model, the resolved effort and cache (left out when off), the sampling
7775
+ * knobs it set, its provider options as the engine defaults and the call
7776
+ * merged them, before an adapter's translation of effort or cache adds to
7777
+ * them, and its schema as JSON Schema, left out when it can't be written as
7778
+ * one. An unset field stays undefined, which the loop's JSON copy leaves out.
7779
+ */
7780
+ function capture_settings(opts, target, turn) {
7781
+ return {
7782
+ provider: target.provider,
7783
+ model_id: target.model_id,
7784
+ effort: turn.effort,
7785
+ cache: turn.cache === "off" ? void 0 : turn.cache,
7786
+ temperature: opts.temperature,
7787
+ max_tokens: opts.max_tokens,
7788
+ top_p: opts.top_p,
7789
+ provider_options: opts.provider_options,
7790
+ schema: opts.schema === void 0 ? void 0 : json_schema_or_undefined(opts.schema)
7791
+ };
7792
+ }
7793
+ /**
7048
7794
  * Resolve a `generate` call's options against engine defaults, run the tool
7049
7795
  * loop, and return the aggregated result.
7050
7796
  *
@@ -7072,6 +7818,9 @@ async function generate(opts_in, engine) {
7072
7818
  const { opts, target } = resolve_target(opts_in, engine);
7073
7819
  const adapter = engine.adapters.get(target.provider);
7074
7820
  if (adapter === void 0) throw new provider_not_configured_error(target.provider, [...engine.adapters.keys()]);
7821
+ assert_unique_tool_names(opts.tools);
7822
+ assert_cache_setting(opts.cache, "cache");
7823
+ assert_capture_level(opts.capture, "capture");
7075
7824
  if (adapter.kind === "external") return {
7076
7825
  ...await adapter.generate(opts, target),
7077
7826
  timing: call_timing(started_at)
@@ -7080,7 +7829,11 @@ async function generate(opts_in, engine) {
7080
7829
  const on_chunk_provided = opts.on_chunk !== void 0;
7081
7830
  const tools_list = opts.tools ?? [];
7082
7831
  assert_capabilities(adapter, target.provider, opts, tools_list, on_chunk_provided);
7083
- const turn = resolve_turn_config(opts, engine);
7832
+ const turn = resolve_turn_config(opts, engine, {
7833
+ adapter,
7834
+ tools: tools_list,
7835
+ trajectory
7836
+ });
7084
7837
  const dispatcher = create_chunk_dispatcher(opts.on_chunk);
7085
7838
  let invoke_once;
7086
7839
  if (adapter.kind === "ai_sdk") invoke_once = await build_ai_sdk_transport({
@@ -7088,12 +7841,14 @@ async function generate(opts_in, engine) {
7088
7841
  target,
7089
7842
  opts,
7090
7843
  effort: turn.effort,
7844
+ cache: turn.cache,
7091
7845
  retry_policy: turn.retry_policy,
7092
7846
  turn_timeout_ms: turn.turn_timeout_ms,
7093
7847
  dispatcher,
7094
7848
  tools_list,
7095
7849
  trajectory,
7096
- telemetry: engine.default_ai_sdk_telemetry
7850
+ telemetry: engine.default_ai_sdk_telemetry,
7851
+ on_chunk_provided
7097
7852
  });
7098
7853
  else invoke_once = build_native_invoke({
7099
7854
  adapter,
@@ -7103,11 +7858,13 @@ async function generate(opts_in, engine) {
7103
7858
  trajectory,
7104
7859
  dispatcher,
7105
7860
  effort: turn.effort,
7861
+ cache: turn.cache,
7106
7862
  schema: opts.schema,
7107
7863
  provider_options: opts.provider_options,
7108
7864
  temperature: opts.temperature,
7109
7865
  max_tokens: opts.max_tokens,
7110
- top_p: opts.top_p
7866
+ top_p: opts.top_p,
7867
+ on_chunk_provided
7111
7868
  });
7112
7869
  const limits = resolve_loop_limits(opts, engine);
7113
7870
  const initial_messages = build_prompt_messages(opts);
@@ -7129,7 +7886,13 @@ async function generate(opts_in, engine) {
7129
7886
  trajectory,
7130
7887
  dispatcher,
7131
7888
  on_chunk_provided,
7132
- limits
7889
+ limits,
7890
+ capture: {
7891
+ level: turn.capture,
7892
+ call_span_id: generate_span,
7893
+ settings: () => capture_settings(opts, target, turn),
7894
+ redact: engine.redact_capture
7895
+ }
7133
7896
  });
7134
7897
  try {
7135
7898
  const outcome = await run_generate_loop({
@@ -7344,6 +8107,8 @@ function build_internals(base, defaults) {
7344
8107
  assign_if_present(internals, "default_model", defaults.model);
7345
8108
  assign_if_present(internals, "default_provider", defaults.provider);
7346
8109
  assign_if_present(internals, "default_system", defaults.system);
8110
+ assign_if_present(internals, "default_cache", defaults.cache);
8111
+ assign_if_present(internals, "default_capture", defaults.capture);
7347
8112
  assign_if_present(internals, "default_temperature", defaults.temperature);
7348
8113
  assign_if_present(internals, "default_max_tokens", defaults.max_tokens);
7349
8114
  assign_if_present(internals, "default_top_p", defaults.top_p);
@@ -7370,13 +8135,18 @@ function create_engine(config) {
7370
8135
  const pricing = merge_pricing(config.pricing);
7371
8136
  const { default_retry, default_effort, default_max_steps } = resolve_default_scalars(config);
7372
8137
  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);
8138
+ assert_cache_setting(config.defaults?.cache, "defaults.cache");
8139
+ assert_capture_level(config.defaults?.capture, "defaults.capture");
8140
+ const get_internals = () => ({
8141
+ ...build_internals({
8142
+ pricing,
8143
+ adapters,
8144
+ default_retry,
8145
+ default_effort,
8146
+ default_max_steps
8147
+ }, config.defaults),
8148
+ redact_capture: config.redact_capture
8149
+ });
7380
8150
  let disposed = false;
7381
8151
  let dispose_promise;
7382
8152
  return {
@@ -7418,6 +8188,6 @@ function create_engine(config) {
7418
8188
  };
7419
8189
  }
7420
8190
  //#endregion
7421
- export { create_engine as t };
8191
+ export { to_json_schema as n, create_engine as t };
7422
8192
 
7423
- //# sourceMappingURL=engine-B6rIIM4J.js.map
8193
+ //# sourceMappingURL=engine-BMdRqWL2.js.map