fascicle 0.16.4 → 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 +59 -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-D1QMcyIG.js → engine-BMdRqWL2.js} +1018 -231
  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-D1QMcyIG.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;
591
612
  }
592
613
  /**
593
- * Assemble the `provider_5xx` retryable shape, copying the optional `status`,
594
- * `body`, and `message` fields when each is present.
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
+ };
622
+ }
623
+ /**
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);
@@ -3514,13 +3706,20 @@ function gate(raw, recognize) {
3514
3706
  }
3515
3707
  /**
3516
3708
  * Normalize an `assistant` event: its `message` must be an object and its
3517
- * `content` an array, whose recognized parts are kept and the rest dropped.
3709
+ * `content` an array, whose recognized parts are kept and the rest dropped. An
3710
+ * `error` code, when present, must be a string.
3518
3711
  */
3519
3712
  function as_assistant_event(raw) {
3520
3713
  const message = raw["message"];
3521
- if (!is_record(message)) return void 0;
3714
+ if (!is_record(message) || !opt_string(raw, "error")) return void 0;
3522
3715
  const content = collect_parts(message["content"], as_assistant_part);
3523
- return content === void 0 ? void 0 : {
3716
+ if (content === void 0) return void 0;
3717
+ const error = raw["error"];
3718
+ return typeof error === "string" ? {
3719
+ type: "assistant",
3720
+ message: { content },
3721
+ error
3722
+ } : {
3524
3723
  type: "assistant",
3525
3724
  message: { content }
3526
3725
  };
@@ -3701,6 +3900,7 @@ async function handle_assistant(state, event, chunks, dispatch) {
3701
3900
  flush_turn(state);
3702
3901
  state.current_step_index += 1;
3703
3902
  }
3903
+ if (event.error !== void 0) state.api_error = event.error;
3704
3904
  for (const part of event.message.content) {
3705
3905
  if (part.type === "text") {
3706
3906
  state.turn_text += part.text;
@@ -3887,6 +4087,7 @@ function snapshot(state) {
3887
4087
  if (state.total_cost_usd !== void 0) base.total_cost_usd = state.total_cost_usd;
3888
4088
  if (state.duration_ms !== void 0) base.duration_ms = state.duration_ms;
3889
4089
  if (state.duration_api_ms !== void 0) base.duration_api_ms = state.duration_api_ms;
4090
+ if (state.api_error !== void 0) base.api_error = state.api_error;
3890
4091
  return base;
3891
4092
  }
3892
4093
  //#endregion
@@ -4292,6 +4493,22 @@ function end_step_span(trajectory, span_id, meta) {
4292
4493
  trajectory.end_span(span_id, { ...meta });
4293
4494
  }
4294
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
+ /**
4295
4512
  * Record a provider response: finish reason, flat token counts, and the
4296
4513
  * engine-measured turn timing when the turn carried one.
4297
4514
  *
@@ -4300,9 +4517,10 @@ function end_step_span(trajectory, span_id, meta) {
4300
4517
  * `first_chunk_ms` directly, where a nested usage object would arrive as an
4301
4518
  * opaque JSON string. This is the one per-turn event a metrics dashboard
4302
4519
  * needs: tokens by direction, the cache and reasoning splits when the
4303
- * 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.
4304
4522
  */
4305
- function record_response_received(trajectory, step_index, turn, span_id) {
4523
+ function record_response_received(trajectory, step_index, turn, span_id, content) {
4306
4524
  if (trajectory === void 0) return;
4307
4525
  const { usage, timing } = turn;
4308
4526
  const event = {
@@ -4316,17 +4534,53 @@ function record_response_received(trajectory, step_index, turn, span_id) {
4316
4534
  if (usage.reasoning_tokens !== void 0) event["reasoning_tokens"] = usage.reasoning_tokens;
4317
4535
  if (usage.cached_input_tokens !== void 0) event["cached_input_tokens"] = usage.cached_input_tokens;
4318
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;
4319
4540
  if (timing !== void 0) {
4320
4541
  event["started_at"] = timing.started_at;
4321
4542
  event["duration_ms"] = timing.duration_ms;
4322
4543
  if (timing.first_chunk_ms !== void 0) event["first_chunk_ms"] = timing.first_chunk_ms;
4323
4544
  }
4545
+ if (content !== void 0) event["content"] = content;
4324
4546
  trajectory.record({
4325
4547
  kind: "response_received",
4326
4548
  ...event
4327
4549
  });
4328
4550
  }
4329
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
+ /**
4330
4584
  * Record one failed-then-retried provider attempt as a `turn_retry` event:
4331
4585
  * the 1-based attempt count, the classified failure kind, the backoff this
4332
4586
  * retry waits, and the HTTP status / `Retry-After` when the failure carried
@@ -4471,6 +4725,50 @@ function create_pricing_missing_dedup(trajectory) {
4471
4725
  } };
4472
4726
  }
4473
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
+ /**
4474
4772
  * Record that a requested reasoning-effort setting was ignored because the
4475
4773
  * model does not support it.
4476
4774
  */
@@ -4541,18 +4839,20 @@ function record_tool_approval(trajectory, kind, meta) {
4541
4839
  });
4542
4840
  }
4543
4841
  /**
4544
- * 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.
4545
4844
  */
4546
4845
  function create_option_ignored_dedup(trajectory) {
4547
4846
  const seen = /* @__PURE__ */ new Set();
4548
- return { emit(option, provider) {
4847
+ return { emit(option, provider, reason) {
4549
4848
  if (trajectory === void 0) return;
4550
4849
  if (seen.has(option)) return;
4551
4850
  seen.add(option);
4552
4851
  trajectory.record({
4553
4852
  kind: "option_ignored",
4554
4853
  option,
4555
- provider
4854
+ provider,
4855
+ ...reason !== void 0 ? { reason } : {}
4556
4856
  });
4557
4857
  } };
4558
4858
  }
@@ -4659,27 +4959,35 @@ function compile_schema(schema) {
4659
4959
  return JSON.stringify(to_json_schema(schema, { strip_meta: true }));
4660
4960
  }
4661
4961
  /**
4662
- * Turn a closed CLI subprocess's exit code, signal, and stderr into an
4663
- * `Error`.
4962
+ * Turn a closed CLI subprocess's exit code, signal, stderr, and what its
4963
+ * stream reported into an `Error`.
4964
+ *
4965
+ * Checks for an auth failure first, then for a claude binary that bwrap
4966
+ * started but couldn't find inside its sandbox, then for a missing sandbox
4967
+ * binary, then falls back to a generic exit-code error. The stderr snippet
4968
+ * attached to each error is truncated to 512 bytes.
4664
4969
  *
4665
- * Checks stderr for an auth failure first, then for a claude binary that
4666
- * bwrap started but couldn't find inside its sandbox, then for a missing
4667
- * sandbox binary, then falls back to a generic exit-code error. The stderr
4668
- * snippet attached to each error is truncated to 512 bytes.
4970
+ * A logged-out CLI says so on stdout and leaves stderr empty: it writes an
4971
+ * assistant message with the `authentication_failed` error code and an error
4972
+ * result that reads "Not logged in · Please run /login". So the auth check
4973
+ * reads the stream's error code as well as stderr, and when stderr is empty,
4974
+ * an error result's text stands in for it in the message.
4669
4975
  *
4670
4976
  * The bwrap check has to come before the sandbox check: bwrap's own
4671
4977
  * "execvp <binary>: No such file or directory" names bwrap and says the file
4672
4978
  * is missing, so the sandbox check would blame bwrap for the claude binary.
4673
4979
  */
4674
- function classify_close_error(code, signal, stderr) {
4980
+ function classify_close_error(code, signal, stderr, parsed) {
4675
4981
  const snippet = stderr.slice(0, 512);
4676
- if (stderr_is_auth_failure(stderr)) return new provider_auth_error(PROVIDER_NAME, `claude CLI reported an auth failure: ${snippet}`, { refresh_command: "claude login" });
4982
+ const reported = parsed?.is_error === true ? parsed.final_text.slice(0, 512) : "";
4983
+ const detail = snippet.length > 0 ? snippet : reported;
4984
+ if (stderr_is_auth_failure(stderr) || parsed?.api_error === "authentication_failed") return new provider_auth_error(PROVIDER_NAME, `claude CLI reported an auth failure: ${detail}`, { refresh_command: "claude auth login" });
4677
4985
  const sandboxed_binary = BWRAP_EXEC_MISSING.exec(stderr)?.[1];
4678
4986
  if (sandboxed_binary !== void 0) return new claude_cli_error("binary_not_found", `bwrap ran but found no claude binary at '${sandboxed_binary}' inside its sandbox, which binds only /usr, /bin, /lib, /lib64, and additional_write_paths: ${snippet}`, { stderr_snippet: snippet });
4679
4987
  if (/bwrap|greywall|sandbox/i.test(stderr) && /not found|no such file|enoent/i.test(stderr)) return new claude_cli_error("sandbox_unavailable", `sandbox binary unavailable: ${snippet}`, { stderr_snippet: snippet });
4680
4988
  const status = typeof code === "number" ? code : void 0;
4681
4989
  const signal_part = signal !== null ? ` (signal ${signal})` : "";
4682
- const message = `claude CLI exited with code ${String(code)}${signal_part}: ${snippet}`;
4990
+ const message = `claude CLI exited with code ${String(code)}${signal_part}: ${detail}`;
4683
4991
  const metadata = { stderr_snippet: snippet };
4684
4992
  if (status !== void 0) metadata.status = status;
4685
4993
  return new claude_cli_error("subprocess_exit", message, metadata);
@@ -4731,7 +5039,7 @@ async function run_cli(spawn_runtime, args) {
4731
5039
  throw err;
4732
5040
  }
4733
5041
  const parsed = snapshot(state);
4734
- if (close_outcome.code !== 0) throw classify_close_error(close_outcome.code, close_outcome.signal, close_outcome.stderr);
5042
+ if (close_outcome.code !== 0) throw classify_close_error(close_outcome.code, close_outcome.signal, close_outcome.stderr, parsed);
4735
5043
  if (!parsed.received_result) throw new claude_cli_error("no_result_event", "claude CLI closed without emitting a terminal result event", { stderr_snippet: close_outcome.stderr.slice(0, 512) });
4736
5044
  return {
4737
5045
  parsed,
@@ -4741,7 +5049,9 @@ async function run_cli(spawn_runtime, args) {
4741
5049
  /**
4742
5050
  * Emit `option_ignored` events for the loop-control options this provider
4743
5051
  * cannot honor: the CLI runs its own tool loop on the far side of the
4744
- * 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.
4745
5055
  */
4746
5056
  function emit_ignored_options(opts, trajectory) {
4747
5057
  const option_ignored = create_option_ignored_dedup(trajectory);
@@ -4750,6 +5060,8 @@ function emit_ignored_options(opts, trajectory) {
4750
5060
  if (opts.on_tool_approval !== void 0) option_ignored.emit("on_tool_approval", PROVIDER_NAME);
4751
5061
  if (opts.tool_call_repair_attempts !== void 0) option_ignored.emit("tool_call_repair_attempts", PROVIDER_NAME);
4752
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");
4753
5065
  }
4754
5066
  /**
4755
5067
  * Reject requests the CLI cannot express: multi-turn user history (one user
@@ -5091,6 +5403,42 @@ function get_provider_factory(name) {
5091
5403
  return factory;
5092
5404
  }
5093
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
5094
5442
  //#region src/engine/merge_defaults.ts
5095
5443
  /**
5096
5444
  * Default-merging helpers for engine-level `EngineDefaults`.
@@ -5162,13 +5510,15 @@ function create_chunk_dispatcher(on_chunk) {
5162
5510
  const OPTIONAL_FIELDS = [
5163
5511
  "reasoning_tokens",
5164
5512
  "cached_input_tokens",
5165
- "cache_write_tokens"
5513
+ "cache_write_tokens",
5514
+ "cache_write_1h_tokens"
5166
5515
  ];
5167
5516
  /**
5168
5517
  * Sum token usage across every step of a generate call.
5169
5518
  *
5170
5519
  * `input_tokens` and `output_tokens` always total. The optional fields
5171
- * (`reasoning_tokens`, `cached_input_tokens`, `cache_write_tokens`) stay
5520
+ * (`reasoning_tokens`, `cached_input_tokens`, `cache_write_tokens`,
5521
+ * `cache_write_1h_tokens`) stay
5172
5522
  * absent on the result unless at least one step reported them; once
5173
5523
  * present, every step contributes to the sum, with steps that omit the
5174
5524
  * field counting as 0.
@@ -5179,12 +5529,14 @@ function sum_usage(steps) {
5179
5529
  const present = {
5180
5530
  reasoning_tokens: false,
5181
5531
  cached_input_tokens: false,
5182
- cache_write_tokens: false
5532
+ cache_write_tokens: false,
5533
+ cache_write_1h_tokens: false
5183
5534
  };
5184
5535
  const sums = {
5185
5536
  reasoning_tokens: 0,
5186
5537
  cached_input_tokens: 0,
5187
- cache_write_tokens: 0
5538
+ cache_write_tokens: 0,
5539
+ cache_write_1h_tokens: 0
5188
5540
  };
5189
5541
  for (const step of steps) {
5190
5542
  input_tokens += step.usage.input_tokens;
@@ -5754,14 +6106,16 @@ async function dispatch_tool_result_chunk(dispatch_chunk, step_index, id, output
5754
6106
  }
5755
6107
  /**
5756
6108
  * Build the `role: 'tool'` message that feeds a tool's result back to the
5757
- * 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.
5758
6111
  */
5759
- 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) {
5760
6113
  return {
5761
6114
  role: "tool",
5762
6115
  tool_call_id,
5763
6116
  name: tool_name,
5764
- 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 } : {}
5765
6119
  };
5766
6120
  }
5767
6121
  /**
@@ -5889,8 +6243,8 @@ async function validate_tool_input(tool, input) {
5889
6243
  * config.messages unchanged. The replacement is ephemeral; it is fed ONLY to
5890
6244
  * invoke_once for this turn, never pushed onto config.messages, so the loop's
5891
6245
  * salvage/approval/ends_turn/schema-repair machinery keeps reading the real
5892
- * transcript. A step_prepared event (recorded inline, as request_sent is)
5893
- * 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.
5894
6248
  */
5895
6249
  async function apply_prepare_step(config, step_index) {
5896
6250
  if (config.prepare_step === void 0) return config.messages;
@@ -5909,10 +6263,12 @@ async function apply_prepare_step(config, step_index) {
5909
6263
  }
5910
6264
  /**
5911
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.
5912
6268
  */
5913
6269
  function loop_result(acc, text, finish_reason, max_steps_reached) {
5914
6270
  return {
5915
- text,
6271
+ text: [...acc.paused_texts, text].join(""),
5916
6272
  steps: acc.steps,
5917
6273
  tool_calls: acc.tool_calls,
5918
6274
  finish_reason,
@@ -5920,16 +6276,248 @@ function loop_result(acc, text, finish_reason, max_steps_reached) {
5920
6276
  };
5921
6277
  }
5922
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
+ /**
5923
6501
  * Invoke the transport seam once for one turn, bracketed by request_sent /
5924
- * response_received events. A thrown invoke (or prepare_step) closes the
5925
- * 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.
5926
6505
  */
5927
- async function invoke_turn(config, step_index, step_span) {
6506
+ async function invoke_turn(config, memory, step_index, step_span) {
5928
6507
  try {
5929
6508
  const request_messages = await apply_prepare_step(config, step_index);
5930
- config.trajectory?.record({
5931
- kind: "request_sent",
5932
- 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
5933
6521
  });
5934
6522
  const turn = await config.invoke_once({
5935
6523
  step_index,
@@ -5937,9 +6525,22 @@ async function invoke_turn(config, step_index, step_span) {
5937
6525
  tools: config.tools,
5938
6526
  abort: config.abort,
5939
6527
  stream: config.stream,
5940
- 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
5941
6543
  });
5942
- record_response_received(config.trajectory, step_index, turn, step_span);
5943
6544
  return turn;
5944
6545
  } catch (err) {
5945
6546
  const message = err instanceof Error ? err.message : String(err);
@@ -6063,7 +6664,7 @@ async function record_failed_call(ctx, call, message, timing) {
6063
6664
  };
6064
6665
  ctx.records.push(record);
6065
6666
  ctx.all_records.push(record);
6066
- 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));
6067
6668
  record_tool_call(ctx.config.trajectory, {
6068
6669
  step_index: ctx.step_index,
6069
6670
  name: call.name,
@@ -6352,7 +6953,11 @@ function push_step_record(config, acc, step_index, turn, tool_calls, finish_reas
6352
6953
  */
6353
6954
  async function run_loop_step(config, tool_map, acc, step_index) {
6354
6955
  const step_span = start_step_span(config.trajectory, step_index);
6355
- 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
+ }
6356
6961
  const view = await salvage_turn(config, turn, tool_map, step_index);
6357
6962
  const { kept, dropped } = clamp_calls(config, view.calls, step_index);
6358
6963
  config.messages.push(build_assistant_message(turn, view, kept));
@@ -6380,6 +6985,7 @@ async function run_loop_step(config, tool_map, acc, step_index) {
6380
6985
  push_step_record(config, acc, step_index, turn, ctx.records, turn_finish_reason, step_span);
6381
6986
  if (ctx.terminal_fired) return loop_result(acc, turn.text, "stop", false);
6382
6987
  if (ctx.would_exceed_after) return loop_result(acc, turn.text, "max_steps", true);
6988
+ acc.paused_texts.length = 0;
6383
6989
  }
6384
6990
  /**
6385
6991
  * Run the model-turn / tool-execution loop to completion.
@@ -6392,7 +6998,9 @@ async function run_loop_step(config, tool_map, acc, step_index) {
6392
6998
  async function run_tool_loop(config) {
6393
6999
  const acc = {
6394
7000
  steps: [],
6395
- tool_calls: []
7001
+ tool_calls: [],
7002
+ paused_texts: [],
7003
+ memory: config.call_memory ?? { previous_cache_traffic: 0 }
6396
7004
  };
6397
7005
  const tool_map = /* @__PURE__ */ new Map();
6398
7006
  for (const t of config.tools) tool_map.set(t.name, t);
@@ -6453,7 +7061,10 @@ const NETWORK_ERROR_CODES = /* @__PURE__ */ new Set([
6453
7061
  "ECONNRESET",
6454
7062
  "ETIMEDOUT",
6455
7063
  "ENOTFOUND",
6456
- "ECONNREFUSED"
7064
+ "ECONNREFUSED",
7065
+ "UND_ERR_HEADERS_TIMEOUT",
7066
+ "UND_ERR_BODY_TIMEOUT",
7067
+ "UND_ERR_SOCKET"
6457
7068
  ]);
6458
7069
  /**
6459
7070
  * True when `err` already carries a `kind` this layer treats as retryable, so
@@ -6491,25 +7102,35 @@ function retry_after_from_error(err) {
6491
7102
  return typeof hv === "string" ? parse_retry_after(hv) : void 0;
6492
7103
  }
6493
7104
  /**
6494
- * Build the `rate_limit` classification for a 429, attaching the message and a
6495
- * 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.
6496
7109
  */
6497
- function classify_rate_limit(err) {
7110
+ function classify_status(err, kind, status) {
6498
7111
  const out = with_error_message(err, {
6499
- kind: "rate_limit",
6500
- status: 429
7112
+ kind,
7113
+ status
6501
7114
  });
6502
7115
  const retry_after_ms = retry_after_from_error(err);
6503
7116
  if (retry_after_ms !== void 0) out["retry_after_ms"] = retry_after_ms;
6504
7117
  return out;
6505
7118
  }
6506
7119
  /**
6507
- * Build the `network` classification when `err.code` is a known network error
6508
- * 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.
6509
7131
  */
6510
7132
  function classify_network_error(err) {
6511
- const code = Reflect.get(err, "code");
6512
- 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;
6513
7134
  return with_error_message(err, { kind: "network" });
6514
7135
  }
6515
7136
  /**
@@ -6517,21 +7138,20 @@ function classify_network_error(err) {
6517
7138
  * understands.
6518
7139
  *
6519
7140
  * Passes through anything that already carries a recognized `kind`.
6520
- * Otherwise inspects `statusCode`/`status`: 429 becomes `rate_limit`
6521
- * (reading `Retry-After` off `responseHeaders` when present), 5xx becomes
6522
- * `provider_5xx`, and a known `ECONNRESET`/`ETIMEDOUT`/`ENOTFOUND`/
6523
- * `ECONNREFUSED` error code becomes `network`. Anything else passes through
6524
- * 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.
6525
7148
  */
6526
7149
  function classify_provider_error(err) {
6527
7150
  if (err === null || typeof err !== "object") return err;
6528
7151
  if (has_retry_kind(err)) return err;
6529
7152
  const status = resolve_error_status(err);
6530
- if (status === 429) return classify_rate_limit(err);
6531
- if (status !== void 0 && status >= 500 && status < 600) return with_error_message(err, {
6532
- kind: "provider_5xx",
6533
- status
6534
- });
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);
6535
7155
  return classify_network_error(err) ?? err;
6536
7156
  }
6537
7157
  /**
@@ -6569,34 +7189,36 @@ function arm_turn_timeout(user_abort, turn_timeout_ms) {
6569
7189
  * Engine-owned wrapper shared by both transports: one turn attempt inside
6570
7190
  * retry_with_policy. The catch ladder classifies by CAUSE, not
6571
7191
  * by the shape the transport happened to throw: on_chunk_error passes through,
6572
- * then a genuine user abort wins, then any error once a chunk has flowed is a
6573
- * non-retryable stream interruption, then a pre-chunk `turn_timeout_ms` expiry
6574
- * is a retryable typed timeout, and only a below-loop aborted_error unrelated
6575
- * to either passes through. Cause-first ordering is what keeps the two
6576
- * transports in parity: the ai_sdk transport reports a mid-stream abort as an
6577
- * aborted_error and the native one as a raw AbortError, so both must be read as
6578
- * the same interruption rather than the ai_sdk timeout masquerading as a user
6579
- * cancel. call_once receives the composed abort+timeout signal so the deadline
6580
- * actually cancels the in-flight request. Adapters may swap `classify`, but
6581
- * never the ladder itself: the engine owns retry, so a hidden adapter-level
6582
- * 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.
6583
7206
  *
6584
7207
  * The wrapper also stamps StepTiming here, and only here, because this is the
6585
7208
  * one place that can see attempt boundaries: started_at re-stamps at every
6586
7209
  * attempt entry, so failed attempts and backoff waits never inflate
6587
- * duration_ms. `first_chunk_at` (the builder's Date.now() from its first
6588
- * dispatched chunk) can only belong to the returning attempt, since any
6589
- * 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.
6590
7212
  */
6591
- function retry_turn(call_once, args, first_chunk_at, retry_policy, classify, turn_timeout_ms, trajectory) {
6592
- 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;
6593
7215
  return retry_with_policy(async () => {
6594
7216
  const started_at = Date.now();
6595
7217
  const deadline = arm_turn_timeout(args.abort, turn_timeout_ms);
6596
7218
  try {
6597
7219
  return {
6598
7220
  ...await call_once(deadline.signal),
6599
- timing: build_step_timing(started_at, first_chunk_at())
7221
+ timing: build_step_timing(started_at, stamp)
6600
7222
  };
6601
7223
  } catch (err) {
6602
7224
  if (err instanceof on_chunk_error) throw err;
@@ -6620,52 +7242,61 @@ function retry_turn(call_once, args, first_chunk_at, retry_policy, classify, tur
6620
7242
  }
6621
7243
  /**
6622
7244
  * Assemble one turn's StepTiming from the attempt's start stamp and the
6623
- * builder's first-chunk stamp (undefined on non-streamed turns).
7245
+ * builder's first-chunk stamp, which holds nothing on a non-streamed turn.
6624
7246
  */
6625
- function build_step_timing(started_at, first_chunk) {
7247
+ function build_step_timing(started_at, stamp) {
6626
7248
  const timing = {
6627
7249
  started_at,
6628
7250
  duration_ms: Date.now() - started_at
6629
7251
  };
7252
+ const first_chunk = stamp.at();
6630
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;
6631
7255
  return timing;
6632
7256
  }
6633
7257
  /**
6634
7258
  * Build the ai_sdk-transport InvokeOnce: the SDK turn built by
6635
7259
  * providers/ai_sdk/invoke.ts behind the same engine-owned retry_turn wrapper
6636
- * the native path uses. The SDK call body lives behind the seam; this builder
6637
- * owns only retry and the streamed-output tracking that makes a mid-stream
6638
- * failure a non-retryable interruption (the same first-chunk stamp doubles as
6639
- * 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.
6640
7265
  */
6641
7266
  function build_ai_sdk_invoke(cfg) {
6642
7267
  return async (args) => {
6643
- let first_chunk_at;
6644
- const call_once = (turn_abort) => cfg.invoke_turn({
6645
- step_index: args.step_index,
6646
- messages: args.messages,
6647
- abort: turn_abort,
6648
- stream: args.stream,
6649
- on_first_chunk: () => {
6650
- first_chunk_at ??= Date.now();
6651
- }
6652
- });
6653
- 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);
6654
7279
  };
6655
7280
  }
6656
7281
  /**
6657
7282
  * Build the native-transport InvokeOnce: maps the loop's InvokeOnceArgs to a
6658
7283
  * TurnRequest and calls adapter.invoke_turn inside the same retry_turn
6659
- * wrapper as the ai_sdk path. The adapter sees a child abort signal so a
6660
- * throwing on_chunk consumer cancels the in-flight request, and its chunk
6661
- * emissions flow through the shared dispatcher, which is what lets
6662
- * 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.
6663
7292
  */
6664
7293
  function build_native_invoke(cfg) {
6665
7294
  const classify = cfg.adapter.classify_error ?? classify_provider_error;
7295
+ const streams = cfg.adapter.supports("streaming");
6666
7296
  return async (args) => {
6667
- let first_chunk_at;
7297
+ const stamp = create_first_chunk_stamp();
6668
7298
  const call_once = async (turn_abort) => {
7299
+ stamp.reset();
6669
7300
  const internal_controller = new AbortController();
6670
7301
  const cancel_on_turn_abort = () => {
6671
7302
  internal_controller.abort(turn_abort.reason);
@@ -6673,7 +7304,7 @@ function build_native_invoke(cfg) {
6673
7304
  if (turn_abort.aborted) internal_controller.abort(turn_abort.reason);
6674
7305
  else turn_abort.addEventListener("abort", cancel_on_turn_abort, { once: true });
6675
7306
  const dispatch_chunk = async (chunk) => {
6676
- first_chunk_at ??= Date.now();
7307
+ stamp.observe(chunk);
6677
7308
  try {
6678
7309
  await cfg.dispatcher.dispatch(chunk);
6679
7310
  } catch (err) {
@@ -6687,16 +7318,18 @@ function build_native_invoke(cfg) {
6687
7318
  messages,
6688
7319
  tools: args.tools,
6689
7320
  abort: internal_controller.signal,
6690
- stream: args.stream,
7321
+ stream: streams,
6691
7322
  model_id: cfg.model_id,
6692
7323
  effort: cfg.effort,
7324
+ ...cfg.cache !== "off" ? { cache: cfg.cache } : {},
6693
7325
  ...system !== void 0 ? { system } : {},
6694
7326
  ...cfg.schema !== void 0 ? { schema: cfg.schema } : {},
6695
7327
  ...cfg.provider_options !== void 0 ? { provider_options: cfg.provider_options } : {},
6696
7328
  ...cfg.temperature !== void 0 ? { temperature: cfg.temperature } : {},
6697
7329
  ...cfg.max_tokens !== void 0 ? { max_tokens: cfg.max_tokens } : {},
6698
7330
  ...cfg.top_p !== void 0 ? { top_p: cfg.top_p } : {},
6699
- ...args.stream ? { dispatch_chunk } : {}
7331
+ ...streams ? { dispatch_chunk } : {},
7332
+ ...args.on_wire !== void 0 ? { on_wire: args.on_wire } : {}
6700
7333
  };
6701
7334
  try {
6702
7335
  return await cfg.adapter.invoke_turn(req);
@@ -6704,10 +7337,60 @@ function build_native_invoke(cfg) {
6704
7337
  turn_abort.removeEventListener("abort", cancel_on_turn_abort);
6705
7338
  }
6706
7339
  };
6707
- 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);
6708
7341
  };
6709
7342
  }
6710
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
+ /**
6711
7394
  * Resolve the provider name: per-call, then the engine default, then the sole
6712
7395
  * configured adapter. With several providers configured and neither a per-call
6713
7396
  * provider nor a default, there is no sane guess, so this throws
@@ -6763,17 +7446,58 @@ function assert_capabilities(adapter, provider, opts, tools_list, on_chunk_provi
6763
7446
  if (on_chunk_provided && !adapter.supports("streaming")) throw new provider_capability_error(provider, "streaming");
6764
7447
  }
6765
7448
  /**
6766
- * Resolve the per-turn knobs (effort, retry policy, turn timeout) against
6767
- * engine defaults. Validated here, before any transport is built, so a bad
6768
- * 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'`.
6769
7463
  */
6770
- function resolve_turn_config(opts, engine) {
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'`.
7477
+ */
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) {
6771
7491
  const effort = opts.effort ?? engine.default_effort;
6772
7492
  const retry_policy = opts.retry ?? engine.default_retry;
6773
7493
  const turn_timeout_ms = opts.turn_timeout_ms ?? engine.default_turn_timeout_ms;
6774
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);
6775
7497
  return {
6776
7498
  effort,
7499
+ cache,
7500
+ capture: resolve_capture(opts, engine, call),
6777
7501
  retry_policy,
6778
7502
  turn_timeout_ms
6779
7503
  };
@@ -6808,19 +7532,32 @@ function resolve_loop_limits(opts, engine) {
6808
7532
  };
6809
7533
  }
6810
7534
  /**
6811
- * Build the ai_sdk-transport InvokeOnce for one call: translate effort, merge
6812
- * provider options, load the SDK seam, and wrap it in the engine-owned retry
6813
- * 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.
6814
7550
  */
6815
7551
  async function build_ai_sdk_transport(cfg) {
6816
7552
  const { adapter, opts, effort } = cfg;
6817
7553
  const effort_translation = adapter.translate_effort(effort);
6818
7554
  if (effort !== "none" && effort_translation.effort_ignored) record_effort_ignored(cfg.trajectory, cfg.target.model_id);
6819
- 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);
6820
7557
  const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
6821
7558
  let ai_sdk_invoke_mod;
6822
7559
  try {
6823
- ai_sdk_invoke_mod = await import("./invoke-LbzZLZFw.js");
7560
+ ai_sdk_invoke_mod = await import("./invoke-BbJLQcLl.js");
6824
7561
  } catch (err) {
6825
7562
  throw missing_peer_error("ai", err);
6826
7563
  }
@@ -6841,7 +7578,8 @@ async function build_ai_sdk_transport(cfg) {
6841
7578
  }),
6842
7579
  retry_policy: cfg.retry_policy,
6843
7580
  turn_timeout_ms: cfg.turn_timeout_ms,
6844
- trajectory: cfg.trajectory
7581
+ trajectory: cfg.trajectory,
7582
+ on_chunk_provided: cfg.on_chunk_provided
6845
7583
  });
6846
7584
  }
6847
7585
  /**
@@ -6893,8 +7631,10 @@ function schema_instruction(schema) {
6893
7631
  /**
6894
7632
  * Assemble the ToolLoopConfig base shared by every loop invocation of one
6895
7633
  * generate call. Everything here is per-call state: the mutable transcript,
6896
- * the pricing lookup, and the pricing_missing dedup all live for exactly one
6897
- * 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
6898
7638
  * conditional spreads keep optional fields absent rather than undefined for
6899
7639
  * exactOptionalPropertyTypes.
6900
7640
  */
@@ -6922,6 +7662,8 @@ function build_loop_config(cfg) {
6922
7662
  model_id: target.model_id,
6923
7663
  resolve_pricing,
6924
7664
  pricing_dedup: create_pricing_missing_dedup(cfg.trajectory),
7665
+ call_memory: { previous_cache_traffic: 0 },
7666
+ capture: cfg.capture,
6925
7667
  ...limits.salvage_budget !== void 0 ? { salvage_budget: limits.salvage_budget } : {},
6926
7668
  ...limits.max_tool_calls_per_step !== void 0 ? { max_tool_calls_per_step: limits.max_tool_calls_per_step } : {},
6927
7669
  ...opts.prepare_step !== void 0 ? { prepare_step: opts.prepare_step } : {}
@@ -7028,6 +7770,27 @@ function assemble_result(outcome, target) {
7028
7770
  return result;
7029
7771
  }
7030
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
+ /**
7031
7794
  * Resolve a `generate` call's options against engine defaults, run the tool
7032
7795
  * loop, and return the aggregated result.
7033
7796
  *
@@ -7055,6 +7818,9 @@ async function generate(opts_in, engine) {
7055
7818
  const { opts, target } = resolve_target(opts_in, engine);
7056
7819
  const adapter = engine.adapters.get(target.provider);
7057
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");
7058
7824
  if (adapter.kind === "external") return {
7059
7825
  ...await adapter.generate(opts, target),
7060
7826
  timing: call_timing(started_at)
@@ -7063,7 +7829,11 @@ async function generate(opts_in, engine) {
7063
7829
  const on_chunk_provided = opts.on_chunk !== void 0;
7064
7830
  const tools_list = opts.tools ?? [];
7065
7831
  assert_capabilities(adapter, target.provider, opts, tools_list, on_chunk_provided);
7066
- const turn = resolve_turn_config(opts, engine);
7832
+ const turn = resolve_turn_config(opts, engine, {
7833
+ adapter,
7834
+ tools: tools_list,
7835
+ trajectory
7836
+ });
7067
7837
  const dispatcher = create_chunk_dispatcher(opts.on_chunk);
7068
7838
  let invoke_once;
7069
7839
  if (adapter.kind === "ai_sdk") invoke_once = await build_ai_sdk_transport({
@@ -7071,12 +7841,14 @@ async function generate(opts_in, engine) {
7071
7841
  target,
7072
7842
  opts,
7073
7843
  effort: turn.effort,
7844
+ cache: turn.cache,
7074
7845
  retry_policy: turn.retry_policy,
7075
7846
  turn_timeout_ms: turn.turn_timeout_ms,
7076
7847
  dispatcher,
7077
7848
  tools_list,
7078
7849
  trajectory,
7079
- telemetry: engine.default_ai_sdk_telemetry
7850
+ telemetry: engine.default_ai_sdk_telemetry,
7851
+ on_chunk_provided
7080
7852
  });
7081
7853
  else invoke_once = build_native_invoke({
7082
7854
  adapter,
@@ -7086,11 +7858,13 @@ async function generate(opts_in, engine) {
7086
7858
  trajectory,
7087
7859
  dispatcher,
7088
7860
  effort: turn.effort,
7861
+ cache: turn.cache,
7089
7862
  schema: opts.schema,
7090
7863
  provider_options: opts.provider_options,
7091
7864
  temperature: opts.temperature,
7092
7865
  max_tokens: opts.max_tokens,
7093
- top_p: opts.top_p
7866
+ top_p: opts.top_p,
7867
+ on_chunk_provided
7094
7868
  });
7095
7869
  const limits = resolve_loop_limits(opts, engine);
7096
7870
  const initial_messages = build_prompt_messages(opts);
@@ -7112,7 +7886,13 @@ async function generate(opts_in, engine) {
7112
7886
  trajectory,
7113
7887
  dispatcher,
7114
7888
  on_chunk_provided,
7115
- 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
+ }
7116
7896
  });
7117
7897
  try {
7118
7898
  const outcome = await run_generate_loop({
@@ -7327,6 +8107,8 @@ function build_internals(base, defaults) {
7327
8107
  assign_if_present(internals, "default_model", defaults.model);
7328
8108
  assign_if_present(internals, "default_provider", defaults.provider);
7329
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);
7330
8112
  assign_if_present(internals, "default_temperature", defaults.temperature);
7331
8113
  assign_if_present(internals, "default_max_tokens", defaults.max_tokens);
7332
8114
  assign_if_present(internals, "default_top_p", defaults.top_p);
@@ -7353,13 +8135,18 @@ function create_engine(config) {
7353
8135
  const pricing = merge_pricing(config.pricing);
7354
8136
  const { default_retry, default_effort, default_max_steps } = resolve_default_scalars(config);
7355
8137
  validate_numeric_defaults(config.defaults);
7356
- const get_internals = () => build_internals({
7357
- pricing,
7358
- adapters,
7359
- default_retry,
7360
- default_effort,
7361
- default_max_steps
7362
- }, 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
+ });
7363
8150
  let disposed = false;
7364
8151
  let dispose_promise;
7365
8152
  return {
@@ -7401,6 +8188,6 @@ function create_engine(config) {
7401
8188
  };
7402
8189
  }
7403
8190
  //#endregion
7404
- export { create_engine as t };
8191
+ export { to_json_schema as n, create_engine as t };
7405
8192
 
7406
- //# sourceMappingURL=engine-D1QMcyIG.js.map
8193
+ //# sourceMappingURL=engine-BMdRqWL2.js.map