fascicle 0.11.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { A as aborted_error, C as map, D as describe, E as sequence, F as flow_schema_default, M as resume_validation_error, N as suspended_error, O as step, P as timeout_error, S as pipe, T as parallel, _ as timeout, a as parse_trajectory_event, b as compute_backoff, c as scope, d as suspend, f as format_schema_issues, g as loop, h as compose, i as is_span_start_event, j as describe_cycle_error, k as run, l as stash, m as checkpoint, n as is_emit_event, o as STEP_KINDS, p as validate_schema, r as is_span_end_event, s as is_step_kind, t as is_custom_trajectory_event, u as use, v as fallback, w as branch, x as wait_with_abort, y as retry } from "./core-Ww6An4CS.js";
2
- import { a as http_logger, n as tee_logger, o as filesystem_logger } from "./adapters-CJnu1687.js";
3
- import { _ as tool_approval_denied_error, a as claude_cli_error, c as incomplete_generation_error, d as provider_auth_error, f as provider_capability_error, g as schema_validation_error, h as rate_limit_error, i as resolve_transport, l as model_required_error, m as provider_not_configured_error, n as load_optional_peer, o as engine_config_error, p as provider_error, r as missing_peer_error, s as engine_disposed_error, t as default_normalize_usage, u as on_chunk_error, v as tool_error, y as turn_timeout_error } from "./types-NnSvmpjg.js";
1
+ import { A as aborted_error, C as map, D as describe, E as sequence, F as flow_schema_default, M as resume_validation_error, N as suspended_error, O as step, P as timeout_error, S as pipe, T as parallel, _ as timeout, a as parse_trajectory_event, b as compute_backoff, c as scope, d as suspend, f as format_schema_issues, g as loop, h as compose, i as is_span_start_event, j as describe_cycle_error, k as run, l as stash, m as checkpoint, n as is_emit_event, o as STEP_KINDS, p as validate_schema, r as is_span_end_event, s as is_step_kind, t as is_custom_trajectory_event, u as use, v as fallback, w as branch, x as wait_with_abort, y as retry } from "./core-DgH4mDbK.js";
2
+ import { a as http_logger, n as tee_logger, o as filesystem_logger } from "./adapters-hmnJOOgf.js";
3
+ import { _ as rate_limit_error, a as normalize_local_usage, b as tool_error, c as engine_config_error, d as model_required_error, f as on_chunk_error, g as provider_not_configured_error, h as provider_error, i as missing_peer_error, l as engine_disposed_error, m as provider_capability_error, n as default_normalize_usage, o as resolve_transport, p as provider_auth_error, r as load_optional_peer, s as claude_cli_error, t as split_leading_system_run, u as incomplete_generation_error, v as schema_validation_error, x as turn_timeout_error, y as tool_approval_denied_error } from "./leading_system-CpkjHoCQ.js";
4
4
  import { createHash } from "node:crypto";
5
5
  import { mkdir, open, readFile, readdir, stat, writeFile } from "node:fs/promises";
6
6
  import { dirname, join, resolve } from "node:path";
@@ -806,40 +806,55 @@ function tournament(config) {
806
806
  let round = 0;
807
807
  while (current.length > 1) {
808
808
  round += 1;
809
- const next_round = [];
810
- for (let i = 0; i < current.length; i += 2) {
811
- const a_id = current[i];
812
- const b_id = current[i + 1];
813
- if (a_id === void 0) continue;
814
- if (b_id === void 0) {
815
- next_round.push(a_id);
816
- continue;
817
- }
818
- const a_val = results[a_id];
819
- const b_val = results[b_id];
820
- if (a_val === void 0 || b_val === void 0) throw new Error("tournament: missing result for match");
821
- const winner_id = await compare_fn(a_val, b_val) === "a" ? a_id : b_id;
822
- bracket.push({
823
- round,
824
- a_id,
825
- b_id,
826
- winner_id
827
- });
828
- next_round.push(winner_id);
829
- }
830
- current = next_round;
809
+ current = await play_round(current, results, compare_fn, round, bracket);
831
810
  }
832
- const final_id = current[0];
833
- if (final_id === void 0) throw new Error("tournament: no winner");
834
- const winner = results[final_id];
835
- if (winner === void 0) throw new Error("tournament: winner missing from results");
836
811
  return {
837
- winner,
812
+ winner: winner_of(current, results),
838
813
  bracket
839
814
  };
840
815
  })]);
841
816
  return compose(config.name ?? "tournament", inner);
842
817
  }
818
+ /**
819
+ * Plays one bracket round: pairs survivors two at a time, comparing each pair
820
+ * and recording a `BracketRecord`, and returns the ids advancing. An unpaired
821
+ * trailing survivor takes a bye. Every paired id must have a settled result.
822
+ */
823
+ async function play_round(current, results, compare, round, bracket) {
824
+ const next_round = [];
825
+ for (let i = 0; i < current.length; i += 2) {
826
+ const a_id = current[i];
827
+ const b_id = current[i + 1];
828
+ if (a_id === void 0) continue;
829
+ if (b_id === void 0) {
830
+ next_round.push(a_id);
831
+ continue;
832
+ }
833
+ const a_val = results[a_id];
834
+ const b_val = results[b_id];
835
+ if (a_val === void 0 || b_val === void 0) throw new Error("tournament: missing result for match");
836
+ const winner_id = await compare(a_val, b_val) === "a" ? a_id : b_id;
837
+ bracket.push({
838
+ round,
839
+ a_id,
840
+ b_id,
841
+ winner_id
842
+ });
843
+ next_round.push(winner_id);
844
+ }
845
+ return next_round;
846
+ }
847
+ /**
848
+ * Extracts the tournament winner from the single surviving id, throwing when
849
+ * no survivor remains or its result is missing.
850
+ */
851
+ function winner_of(current, results) {
852
+ const final_id = current[0];
853
+ if (final_id === void 0) throw new Error("tournament: no winner");
854
+ const winner = results[final_id];
855
+ if (winner === void 0) throw new Error("tournament: winner missing from results");
856
+ return winner;
857
+ }
843
858
  //#endregion
844
859
  //#region src/composites/errors.ts
845
860
  /**
@@ -905,12 +920,12 @@ async function bench(flow, cases, judges, options = {}) {
905
920
  if (options.trajectory_dir !== void 0) await mkdir(options.trajectory_dir, { recursive: true });
906
921
  const judge_entries = Object.entries(judges);
907
922
  const run_one = async (bc) => {
908
- const trajectory_path = options.trajectory_dir === void 0 ? void 0 : join(options.trajectory_dir, `${bc.id}.jsonl`);
909
- const sinks = [];
910
- if (trajectory_path !== void 0) sinks.push(filesystem_logger({ output_path: trajectory_path }));
911
- if (options.live_url !== void 0) sinks.push(http_logger({ url: options.live_url }));
912
- const cost_tracker = create_cost_tracker();
913
- const case_logger = sinks.length === 0 ? cost_tracker.logger : tee_logger(...sinks, cost_tracker.logger);
923
+ const { trajectory_path, case_logger, cost_tracker } = build_case_logging(options, bc.id);
924
+ const ctx = {
925
+ case_logger,
926
+ install_signal_handlers,
927
+ run_options
928
+ };
914
929
  const start = Date.now();
915
930
  let output;
916
931
  let error;
@@ -927,33 +942,7 @@ async function bench(flow, cases, judges, options = {}) {
927
942
  error = err instanceof Error ? err.message : String(err);
928
943
  ok = false;
929
944
  }
930
- const scores = {};
931
- if (ok && output !== void 0) {
932
- const judge_input = bc.meta === void 0 ? {
933
- input: bc.input,
934
- output
935
- } : {
936
- input: bc.input,
937
- output,
938
- meta: bc.meta
939
- };
940
- for (const [name, judge] of judge_entries) {
941
- let raw;
942
- try {
943
- raw = await run(judge, judge_input, {
944
- trajectory: case_logger,
945
- install_signal_handlers,
946
- ...run_options
947
- });
948
- } catch (err) {
949
- rethrow_control_flow(err, bc.id);
950
- continue;
951
- }
952
- const normalized = normalize_score(raw);
953
- if (normalized === void 0) continue;
954
- scores[name] = normalized;
955
- }
956
- }
945
+ const scores = ok && output !== void 0 ? await run_judges(judge_entries, build_judge_input(bc, output), ctx, bc.id) : {};
957
946
  const duration_ms = Date.now() - start;
958
947
  const result = {
959
948
  case_id: bc.id,
@@ -978,6 +967,65 @@ async function bench(flow, cases, judges, options = {}) {
978
967
  };
979
968
  }
980
969
  /**
970
+ * Builds the per-case trajectory pipeline: an optional filesystem sink when
971
+ * `trajectory_dir` is set, an optional HTTP sink when `live_url` is set, and
972
+ * always an in-process cost tracker, all tee'd together. Returns the resolved
973
+ * trajectory path (undefined when no dir is set) alongside the logger and
974
+ * tracker.
975
+ */
976
+ function build_case_logging(options, case_id) {
977
+ const trajectory_path = options.trajectory_dir === void 0 ? void 0 : join(options.trajectory_dir, `${case_id}.jsonl`);
978
+ const sinks = [];
979
+ if (trajectory_path !== void 0) sinks.push(filesystem_logger({ output_path: trajectory_path }));
980
+ if (options.live_url !== void 0) sinks.push(http_logger({ url: options.live_url }));
981
+ const cost_tracker = create_cost_tracker();
982
+ return {
983
+ trajectory_path,
984
+ case_logger: sinks.length === 0 ? cost_tracker.logger : tee_logger(...sinks, cost_tracker.logger),
985
+ cost_tracker
986
+ };
987
+ }
988
+ /**
989
+ * Assembles the judge input triple, including `meta` only when the case
990
+ * carries it so the optional field stays absent rather than `undefined`.
991
+ */
992
+ function build_judge_input(bc, output) {
993
+ return bc.meta === void 0 ? {
994
+ input: bc.input,
995
+ output
996
+ } : {
997
+ input: bc.input,
998
+ output,
999
+ meta: bc.meta
1000
+ };
1001
+ }
1002
+ /**
1003
+ * Runs each judge as a Step over the case's input/output, collecting the
1004
+ * normalized scores. A judge that throws a control-flow signal rethrows via
1005
+ * `rethrow_control_flow`; any other throw, or an abstaining `undefined`,
1006
+ * omits that judge from the scores rather than failing the case.
1007
+ */
1008
+ async function run_judges(judge_entries, judge_input, ctx, case_id) {
1009
+ const scores = {};
1010
+ for (const [name, judge] of judge_entries) {
1011
+ let raw;
1012
+ try {
1013
+ raw = await run(judge, judge_input, {
1014
+ trajectory: ctx.case_logger,
1015
+ install_signal_handlers: ctx.install_signal_handlers,
1016
+ ...ctx.run_options
1017
+ });
1018
+ } catch (err) {
1019
+ rethrow_control_flow(err, case_id);
1020
+ continue;
1021
+ }
1022
+ const normalized = normalize_score(raw);
1023
+ if (normalized === void 0) continue;
1024
+ scores[name] = normalized;
1025
+ }
1026
+ return scores;
1027
+ }
1028
+ /**
981
1029
  * Rethrows the control-flow signals that must never become a `CaseResult`.
982
1030
  *
983
1031
  * An `aborted_error` cancels the whole bench: a report claiming every
@@ -1181,13 +1229,25 @@ function is_record$1(v) {
1181
1229
  */
1182
1230
  function deep_equal(a, b) {
1183
1231
  if (a === b) return true;
1184
- if (Array.isArray(a) || Array.isArray(b)) {
1185
- if (!Array.isArray(a) || !Array.isArray(b)) return false;
1186
- if (a.length !== b.length) return false;
1187
- for (let i = 0; i < a.length; i += 1) if (!deep_equal(a[i], b[i])) return false;
1188
- return true;
1189
- }
1232
+ if (Array.isArray(a) || Array.isArray(b)) return arrays_equal(a, b);
1190
1233
  if (!is_record$1(a) || !is_record$1(b)) return false;
1234
+ return records_equal(a, b);
1235
+ }
1236
+ /**
1237
+ * Element-wise deep equality once at least one side is an array: both must be
1238
+ * arrays of the same length with every index equal.
1239
+ */
1240
+ function arrays_equal(a, b) {
1241
+ if (!Array.isArray(a) || !Array.isArray(b)) return false;
1242
+ if (a.length !== b.length) return false;
1243
+ for (let i = 0; i < a.length; i += 1) if (!deep_equal(a[i], b[i])) return false;
1244
+ return true;
1245
+ }
1246
+ /**
1247
+ * Key-by-key deep equality over two records: same sorted key sets, and each
1248
+ * value deep-equal under the shared key.
1249
+ */
1250
+ function records_equal(a, b) {
1191
1251
  const ak = Object.keys(a).toSorted();
1192
1252
  const bk = Object.keys(b).toSorted();
1193
1253
  if (ak.length !== bk.length) return false;
@@ -1393,16 +1453,8 @@ function compute_per_case(current, baseline, score_threshold, cost_threshold) {
1393
1453
  for (const id of all_ids) {
1394
1454
  const c = by_id_current.get(id);
1395
1455
  const b = by_id_baseline.get(id);
1396
- const score_deltas = {};
1397
- let any_score_regression = false;
1398
- const judge_names = /* @__PURE__ */ new Set([...Object.keys(c?.scores ?? {}), ...Object.keys(b?.scores ?? {})]);
1399
- for (const name of judge_names) {
1400
- const delta = score_value(c?.scores[name]) - score_value(b?.scores[name]);
1401
- score_deltas[name] = delta;
1402
- if (delta < -score_threshold) any_score_regression = true;
1403
- }
1404
- const cost_delta = (c?.cost_usd ?? 0) - (b?.cost_usd ?? 0);
1405
- const cost_ratio_regression = b?.cost_usd !== void 0 && b.cost_usd > 0 ? cost_delta / b.cost_usd > cost_threshold : (c?.cost_usd ?? 0) > 0 && (b?.cost_usd ?? 0) === 0 && cost_threshold < Infinity;
1456
+ const { score_deltas, any_regression } = compute_score_deltas(c, b, score_threshold);
1457
+ const { cost_delta, is_regression: cost_regression } = compute_cost_regression(c, b, cost_threshold);
1406
1458
  const ok_regression = b?.ok === true && c?.ok !== true;
1407
1459
  out.push({
1408
1460
  case_id: id,
@@ -1410,12 +1462,46 @@ function compute_per_case(current, baseline, score_threshold, cost_threshold) {
1410
1462
  ...c !== void 0 ? { current: c } : {},
1411
1463
  score_deltas,
1412
1464
  cost_delta,
1413
- is_regression: any_score_regression || cost_ratio_regression || ok_regression
1465
+ is_regression: any_regression || cost_regression || ok_regression
1414
1466
  });
1415
1467
  }
1416
1468
  return out;
1417
1469
  }
1418
1470
  /**
1471
+ * Compute per-judge score deltas for one case pair across the union of judge
1472
+ * names on both sides (a missing side scores 0), flagging a regression when
1473
+ * any judge's score drops beyond the threshold.
1474
+ */
1475
+ function compute_score_deltas(c, b, score_threshold) {
1476
+ const score_deltas = {};
1477
+ let any_regression = false;
1478
+ const judge_names = /* @__PURE__ */ new Set([...Object.keys(c?.scores ?? {}), ...Object.keys(b?.scores ?? {})]);
1479
+ for (const name of judge_names) {
1480
+ const delta = score_value(c?.scores[name]) - score_value(b?.scores[name]);
1481
+ score_deltas[name] = delta;
1482
+ if (delta < -score_threshold) any_regression = true;
1483
+ }
1484
+ return {
1485
+ score_deltas,
1486
+ any_regression
1487
+ };
1488
+ }
1489
+ /**
1490
+ * Compute the cost delta for one case pair and whether it regressed. With a
1491
+ * positive baseline cost, a relative rise beyond `cost_threshold` regresses;
1492
+ * with a zero baseline, any positive current cost regresses (unless the
1493
+ * threshold is Infinity). A missing side counts as 0.
1494
+ */
1495
+ function compute_cost_regression(c, b, cost_threshold) {
1496
+ const baseline_cost = b?.cost_usd ?? 0;
1497
+ const current_cost = c?.cost_usd ?? 0;
1498
+ const cost_delta = current_cost - baseline_cost;
1499
+ return {
1500
+ cost_delta,
1501
+ is_regression: baseline_cost > 0 ? cost_delta / baseline_cost > cost_threshold : current_cost > 0 && baseline_cost === 0 && cost_threshold < Infinity
1502
+ };
1503
+ }
1504
+ /**
1419
1505
  * Extract a finite numeric score from a raw score entry.
1420
1506
  *
1421
1507
  * Accepts a bare number or an object with a numeric `score` field; anything
@@ -1541,6 +1627,48 @@ function round6$1(value) {
1541
1627
  return Math.round(value * 1e6) / 1e6;
1542
1628
  }
1543
1629
  /**
1630
+ * The breakdown for a turn whose model has no pricing entry.
1631
+ *
1632
+ * Free providers (ollama, lmstudio) still report an all-zero, estimated
1633
+ * breakdown; a priced provider missing a table entry returns undefined so the
1634
+ * caller can surface a `pricing_missing` event instead of a fake zero.
1635
+ */
1636
+ function missing_pricing_breakdown(provider) {
1637
+ if (!FREE_PROVIDERS.has(provider)) return void 0;
1638
+ return {
1639
+ total_usd: 0,
1640
+ input_usd: 0,
1641
+ output_usd: 0,
1642
+ currency: "USD",
1643
+ is_estimate: true
1644
+ };
1645
+ }
1646
+ /**
1647
+ * Split reasoning tokens between a surfaced `reasoning_usd` field and the
1648
+ * output total.
1649
+ *
1650
+ * With a dedicated `reasoning_per_million` rate the reasoning cost is surfaced
1651
+ * separately and left out of output; without one it rolls into output at the
1652
+ * output rate. Absent reasoning tokens leave output untouched and surface
1653
+ * nothing.
1654
+ */
1655
+ function apply_reasoning_cost(plain_output_usd_raw, reasoning, has_reasoning_rate, reasoning_usd_raw) {
1656
+ if (reasoning > 0) {
1657
+ if (has_reasoning_rate) return {
1658
+ output_usd_raw: plain_output_usd_raw,
1659
+ surface_reasoning_usd: round6$1(reasoning_usd_raw)
1660
+ };
1661
+ return {
1662
+ output_usd_raw: plain_output_usd_raw + reasoning_usd_raw,
1663
+ surface_reasoning_usd: void 0
1664
+ };
1665
+ }
1666
+ return {
1667
+ output_usd_raw: plain_output_usd_raw,
1668
+ surface_reasoning_usd: void 0
1669
+ };
1670
+ }
1671
+ /**
1544
1672
  * Compute a CostBreakdown for a single turn's usage.
1545
1673
  *
1546
1674
  * Returns undefined when pricing is absent AND the provider is not free. Free
@@ -1552,50 +1680,28 @@ function round6$1(value) {
1552
1680
  * input call.
1553
1681
  */
1554
1682
  function compute_cost(usage, pricing, provider) {
1555
- if (pricing === void 0) {
1556
- if (!FREE_PROVIDERS.has(provider)) return void 0;
1557
- return {
1558
- total_usd: 0,
1559
- input_usd: 0,
1560
- output_usd: 0,
1561
- currency: "USD",
1562
- is_estimate: true
1563
- };
1564
- }
1565
- const input_tokens = usage.input_tokens;
1566
- const output_tokens = usage.output_tokens;
1683
+ if (pricing === void 0) return missing_pricing_breakdown(provider);
1567
1684
  const cached = usage.cached_input_tokens ?? 0;
1568
1685
  const cache_write = usage.cache_write_tokens ?? 0;
1569
1686
  const reasoning = usage.reasoning_tokens ?? 0;
1570
- const fresh_input = Math.max(0, input_tokens - cached - cache_write);
1687
+ const fresh_input = Math.max(0, usage.input_tokens - cached - cache_write);
1571
1688
  const cached_rate = pricing.cached_input_per_million ?? pricing.input_per_million;
1572
1689
  const cache_write_rate = pricing.cache_write_per_million ?? pricing.input_per_million;
1573
- const has_reasoning_rate = pricing.reasoning_per_million !== void 0;
1574
1690
  const reasoning_rate = pricing.reasoning_per_million ?? pricing.output_per_million;
1575
1691
  const input_usd_raw = fresh_input * pricing.input_per_million / 1e6;
1576
1692
  const cached_usd_raw = cached * cached_rate / 1e6;
1577
1693
  const cache_write_usd_raw = cache_write * cache_write_rate / 1e6;
1578
1694
  const reasoning_usd_raw = reasoning * reasoning_rate / 1e6;
1579
- const plain_output_usd_raw = Math.max(0, output_tokens - reasoning) * pricing.output_per_million / 1e6;
1580
- const input_usd = round6$1(input_usd_raw);
1581
- let output_usd_raw = plain_output_usd_raw;
1582
- let surface_reasoning_usd;
1583
- if (reasoning > 0) if (has_reasoning_rate) surface_reasoning_usd = round6$1(reasoning_usd_raw);
1584
- else output_usd_raw = plain_output_usd_raw + reasoning_usd_raw;
1585
- const output_usd = round6$1(output_usd_raw);
1586
- let cached_input_usd;
1587
- let cache_write_usd;
1588
- if (cached > 0) cached_input_usd = round6$1(cached_usd_raw);
1589
- if (cache_write > 0) cache_write_usd = round6$1(cache_write_usd_raw);
1695
+ 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);
1590
1696
  const breakdown = {
1591
1697
  total_usd: round6$1(input_usd_raw + cached_usd_raw + cache_write_usd_raw + (surface_reasoning_usd !== void 0 ? reasoning_usd_raw : 0) + output_usd_raw),
1592
- input_usd,
1593
- output_usd,
1698
+ input_usd: round6$1(input_usd_raw),
1699
+ output_usd: round6$1(output_usd_raw),
1594
1700
  currency: "USD",
1595
1701
  is_estimate: true
1596
1702
  };
1597
- if (cached_input_usd !== void 0) breakdown.cached_input_usd = cached_input_usd;
1598
- if (cache_write_usd !== void 0) breakdown.cache_write_usd = cache_write_usd;
1703
+ if (cached > 0) breakdown.cached_input_usd = round6$1(cached_usd_raw);
1704
+ if (cache_write > 0) breakdown.cache_write_usd = round6$1(cache_write_usd_raw);
1599
1705
  if (surface_reasoning_usd !== void 0) breakdown.reasoning_usd = surface_reasoning_usd;
1600
1706
  return breakdown;
1601
1707
  }
@@ -1653,6 +1759,52 @@ function to_abort_error(reason) {
1653
1759
  return new aborted_error("aborted", { reason });
1654
1760
  }
1655
1761
  /**
1762
+ * Build the `rate_limit_error` thrown when 429 retries exhaust. Prefers the
1763
+ * failing attempt's own `Retry-After`, falling back to the most recent one a
1764
+ * prior attempt carried (`last_rate_limit_after`).
1765
+ */
1766
+ function build_rate_limit_error(retryable, attempt, last_rate_limit_after) {
1767
+ const metadata = { attempts: attempt };
1768
+ if (retryable.retry_after_ms !== void 0) metadata.retry_after_ms = retryable.retry_after_ms;
1769
+ else if (last_rate_limit_after !== void 0) metadata.retry_after_ms = last_rate_limit_after;
1770
+ if (retryable.status !== void 0) metadata.status = retryable.status;
1771
+ return new rate_limit_error(retryable.message ?? `rate limited after ${attempt} attempts`, metadata);
1772
+ }
1773
+ /**
1774
+ * Build the `provider_error` thrown when 5xx/network/timeout retries exhaust.
1775
+ * Timeout collapses onto the `network` cause; only 5xx carries status/body.
1776
+ */
1777
+ function build_provider_error(retryable, attempt) {
1778
+ const metadata = { cause_kind: retryable.kind === "provider_5xx" ? "provider_5xx" : "network" };
1779
+ if (retryable.kind === "provider_5xx") {
1780
+ if (retryable.status !== void 0) metadata.status = retryable.status;
1781
+ if (retryable.body !== void 0) metadata.body = retryable.body;
1782
+ }
1783
+ return new provider_error(retryable.message ?? `${retryable.kind} after ${attempt} attempts`, metadata);
1784
+ }
1785
+ /**
1786
+ * Map an exhausted retryable failure onto the error this layer rejects with:
1787
+ * `rate_limit_error` for 429s, `provider_error` for everything else.
1788
+ */
1789
+ function build_exhaustion_error(retryable, attempt, last_rate_limit_after) {
1790
+ if (retryable.kind === "rate_limit") return build_rate_limit_error(retryable, attempt, last_rate_limit_after);
1791
+ return build_provider_error(retryable, attempt);
1792
+ }
1793
+ /**
1794
+ * Compute the pre-retry backoff for `attempt`. A rate-limit `Retry-After`
1795
+ * outranks the local cap (`Math.max`), so a server instruction longer than
1796
+ * `max_delay_ms` is honored; the honored value is returned so the caller can
1797
+ * carry it onto a later failure that omits its own.
1798
+ */
1799
+ function next_backoff(retryable, backoff_policy, attempt) {
1800
+ const delay = compute_backoff(backoff_policy, attempt - 1);
1801
+ if (retryable.kind === "rate_limit" && retryable.retry_after_ms !== void 0) return {
1802
+ delay: Math.max(delay, retryable.retry_after_ms),
1803
+ retry_after_ms: retryable.retry_after_ms
1804
+ };
1805
+ return { delay };
1806
+ }
1807
+ /**
1656
1808
  * Retry `fn` under `policy`. `fn` must throw a RetryableError-shaped object to
1657
1809
  * trigger retry; any other thrown value short-circuits as a permanent failure.
1658
1810
  *
@@ -1676,30 +1828,12 @@ async function retry_with_policy(fn, policy = DEFAULT_RETRY, abort) {
1676
1828
  return await fn(attempt);
1677
1829
  } catch (err) {
1678
1830
  const retryable = classify_retryable(err);
1679
- if (retryable === void 0) throw err;
1680
- if (!is_retryable(retryable.kind, policy)) throw err;
1831
+ if (retryable === void 0 || !is_retryable(retryable.kind, policy)) throw err;
1681
1832
  attempt += 1;
1682
- if (attempt >= policy.max_attempts) {
1683
- if (retryable.kind === "rate_limit") {
1684
- const metadata = { attempts: attempt };
1685
- if (retryable.retry_after_ms !== void 0) metadata.retry_after_ms = retryable.retry_after_ms;
1686
- else if (last_rate_limit_after !== void 0) metadata.retry_after_ms = last_rate_limit_after;
1687
- if (retryable.status !== void 0) metadata.status = retryable.status;
1688
- throw new rate_limit_error(retryable.message ?? `rate limited after ${attempt} attempts`, metadata);
1689
- }
1690
- const metadata = { cause_kind: retryable.kind === "provider_5xx" ? "provider_5xx" : "network" };
1691
- if (retryable.kind === "provider_5xx") {
1692
- if (retryable.status !== void 0) metadata.status = retryable.status;
1693
- if (retryable.body !== void 0) metadata.body = retryable.body;
1694
- }
1695
- throw new provider_error(retryable.message ?? `${retryable.kind} after ${attempt} attempts`, metadata);
1696
- }
1697
- let delay = compute_backoff(backoff_policy, attempt - 1);
1698
- if (retryable.kind === "rate_limit" && retryable.retry_after_ms !== void 0) {
1699
- delay = Math.max(delay, retryable.retry_after_ms);
1700
- last_rate_limit_after = retryable.retry_after_ms;
1701
- }
1702
- await wait_with_abort(delay, signal, to_abort_error);
1833
+ if (attempt >= policy.max_attempts) throw build_exhaustion_error(retryable, attempt, last_rate_limit_after);
1834
+ const backoff = next_backoff(retryable, backoff_policy, attempt);
1835
+ last_rate_limit_after = backoff.retry_after_ms ?? last_rate_limit_after;
1836
+ await wait_with_abort(backoff.delay, signal, to_abort_error);
1703
1837
  }
1704
1838
  }
1705
1839
  }
@@ -1718,6 +1852,44 @@ function read_number$1(err, key) {
1718
1852
  return typeof value === "number" ? value : void 0;
1719
1853
  }
1720
1854
  /**
1855
+ * Assemble the `rate_limit` retryable shape, copying the optional
1856
+ * `retry_after_ms`, `status`, and `message` fields when each is present.
1857
+ */
1858
+ function build_retryable_rate_limit(err) {
1859
+ const base = { kind: "rate_limit" };
1860
+ const retry_after = read_number$1(err, "retry_after_ms");
1861
+ if (retry_after !== void 0) base.retry_after_ms = retry_after;
1862
+ const status = read_number$1(err, "status");
1863
+ if (status !== void 0) base.status = status;
1864
+ const message = read_string$1(err, "message");
1865
+ if (message !== void 0) base.message = message;
1866
+ return base;
1867
+ }
1868
+ /**
1869
+ * Assemble the `provider_5xx` retryable shape, copying the optional `status`,
1870
+ * `body`, and `message` fields when each is present.
1871
+ */
1872
+ function build_retryable_5xx(err) {
1873
+ const base = { kind: "provider_5xx" };
1874
+ const status = read_number$1(err, "status");
1875
+ if (status !== void 0) base.status = status;
1876
+ const body = read_string$1(err, "body");
1877
+ if (body !== void 0) base.body = body;
1878
+ const message = read_string$1(err, "message");
1879
+ if (message !== void 0) base.message = message;
1880
+ return base;
1881
+ }
1882
+ /**
1883
+ * Assemble a `network` or `timeout` retryable shape, copying the optional
1884
+ * `message` when present.
1885
+ */
1886
+ function build_retryable_signal(err, kind) {
1887
+ const base = { kind };
1888
+ const message = read_string$1(err, "message");
1889
+ if (message !== void 0) base.message = message;
1890
+ return base;
1891
+ }
1892
+ /**
1721
1893
  * Classify a thrown value into a `RetryableError`, or `undefined` if it
1722
1894
  * does not carry one of the recognized retryable `kind`s.
1723
1895
  *
@@ -1728,64 +1900,147 @@ function read_number$1(err, key) {
1728
1900
  function classify_retryable(err) {
1729
1901
  if (err === null || typeof err !== "object") return void 0;
1730
1902
  const kind = read_string$1(err, "kind");
1731
- if (kind === "rate_limit") {
1732
- const base = { kind: "rate_limit" };
1733
- const retry_after = read_number$1(err, "retry_after_ms");
1734
- if (retry_after !== void 0) base.retry_after_ms = retry_after;
1735
- const status = read_number$1(err, "status");
1736
- if (status !== void 0) base.status = status;
1737
- const message = read_string$1(err, "message");
1738
- if (message !== void 0) base.message = message;
1739
- return base;
1903
+ if (kind === "rate_limit") return build_retryable_rate_limit(err);
1904
+ if (kind === "provider_5xx") return build_retryable_5xx(err);
1905
+ if (kind === "network" || kind === "timeout") return build_retryable_signal(err, kind);
1906
+ }
1907
+ //#endregion
1908
+ //#region src/engine/providers/native_shared.ts
1909
+ /**
1910
+ * The message under a JSON error envelope's `error` key, or undefined when
1911
+ * the shape carries none worth surfacing.
1912
+ */
1913
+ function error_field_message(parsed) {
1914
+ if (parsed === null || typeof parsed !== "object") return void 0;
1915
+ const error = Reflect.get(parsed, "error");
1916
+ if (typeof error === "string" && error.length > 0) return error;
1917
+ if (error === null || typeof error !== "object") return void 0;
1918
+ const message = Reflect.get(error, "message");
1919
+ return typeof message === "string" && message.length > 0 ? message : void 0;
1920
+ }
1921
+ /**
1922
+ * Pull a human-readable message out of a non-2xx body. The native wires share
1923
+ * one envelope family: Anthropic sends `{ type: 'error', error: { message } }`,
1924
+ * OpenAI-style wires send `{ error: { message } }`, and Ollama sends
1925
+ * `{ error: "message" }` (with the nested shape appearing behind proxies), so
1926
+ * one parser accepts a string or a `{ message }` object under `error` and
1927
+ * falls back to the raw body, truncated to 300 chars.
1928
+ */
1929
+ function extract_error_message(body) {
1930
+ if (body.length === 0) return "(empty body)";
1931
+ let message;
1932
+ try {
1933
+ message = error_field_message(JSON.parse(body));
1934
+ } catch {}
1935
+ if (message !== void 0) return message;
1936
+ return body.length > 300 ? `${body.slice(0, 300)}...` : body;
1937
+ }
1938
+ /**
1939
+ * Map a non-2xx response to what the engine's retry stack expects: 401
1940
+ * becomes a typed provider_auth_error, 429/5xx keep `status` +
1941
+ * `responseHeaders` so classify_provider_error marks them retryable
1942
+ * (rate_limit honoring retry-after, provider_5xx), and any other 4xx is a
1943
+ * permanent provider_error that surfaces as-is.
1944
+ */
1945
+ async function response_error(response, provider) {
1946
+ let body = "";
1947
+ try {
1948
+ body = await response.text();
1949
+ } catch {}
1950
+ const detail = extract_error_message(body);
1951
+ const status = response.status;
1952
+ if (status === 401) return new provider_auth_error(provider, `${provider} authentication failed (401): ${detail}`);
1953
+ if (status === 429 || status >= 500) {
1954
+ const retry_after = response.headers.get("retry-after");
1955
+ return Object.assign(/* @__PURE__ */ new Error(`${provider} API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
1740
1956
  }
1741
- if (kind === "provider_5xx") {
1742
- const base = { kind: "provider_5xx" };
1743
- const status = read_number$1(err, "status");
1744
- if (status !== void 0) base.status = status;
1745
- const body = read_string$1(err, "body");
1746
- if (body !== void 0) base.body = body;
1747
- const message = read_string$1(err, "message");
1748
- if (message !== void 0) base.message = message;
1749
- return base;
1957
+ return new provider_error(`${provider} API error ${status}: ${detail}`, {
1958
+ status,
1959
+ body
1960
+ });
1961
+ }
1962
+ /**
1963
+ * A user abort surfaces as the fetch/reader AbortError; retry_turn converts
1964
+ * it via its own signal check, so it is rethrown untouched. Everything else
1965
+ * is a transport failure wrapped in the `kind: 'network'` shape the shared
1966
+ * classify_provider_error marks retryable.
1967
+ */
1968
+ function rethrow_network_failure(err, abort, provider) {
1969
+ if (abort.aborted) throw err;
1970
+ const detail = err instanceof Error ? err.message : String(err);
1971
+ throw Object.assign(/* @__PURE__ */ new Error(`${provider} native: network failure: ${detail}`), { kind: "network" });
1972
+ }
1973
+ /**
1974
+ * Run one native turn over HTTP: POST the dialect's body, map a non-2xx to
1975
+ * the retry stack's shapes, and hand the response to the dialect's stream or
1976
+ * JSON consumer. `build_body` is called inside the network try/catch on
1977
+ * purpose: that is where each adapter historically built its body, so a
1978
+ * mapper throw keeps surfacing through the same path it always did.
1979
+ */
1980
+ async function invoke_http_turn(req, provider, request, consume) {
1981
+ let response;
1982
+ try {
1983
+ response = await fetch(request.url, {
1984
+ method: "POST",
1985
+ headers: request.headers,
1986
+ body: JSON.stringify(request.build_body()),
1987
+ signal: req.abort
1988
+ });
1989
+ } catch (err) {
1990
+ rethrow_network_failure(err, req.abort, provider);
1750
1991
  }
1751
- if (kind === "network" || kind === "timeout") {
1752
- const base = { kind };
1753
- const message = read_string$1(err, "message");
1754
- if (message !== void 0) base.message = message;
1755
- return base;
1992
+ if (!response.ok) throw await response_error(response, provider);
1993
+ if (req.stream) return consume.stream(response);
1994
+ const payload = await response.json();
1995
+ return consume.parse(payload);
1996
+ }
1997
+ /**
1998
+ * Drain a streaming response: decode bytes, reassemble framed payloads (SSE
1999
+ * events or NDJSON lines), and feed them to the dialect's aggregator, which
2000
+ * dispatches chunks through req.dispatch_chunk as they arrive. Transport
2001
+ * failures mid-read wrap as network errors; aggregator throws (malformed
2002
+ * frames, mid-stream error events, a rejecting on_chunk) pass through
2003
+ * untouched, with the reader cancelled so the connection is released.
2004
+ */
2005
+ async function consume_framed_stream(response, req, provider, framing, create_aggregator) {
2006
+ const body = response.body;
2007
+ if (body === null) throw new provider_error(`${provider} native: streaming response has no body`);
2008
+ const aggregator = create_aggregator(req.dispatch_chunk ?? (async () => {}));
2009
+ const reader = body.getReader();
2010
+ const text_decoder = new TextDecoder();
2011
+ const next_bytes = async () => {
2012
+ let step;
2013
+ try {
2014
+ step = await reader.read();
2015
+ } catch (err) {
2016
+ rethrow_network_failure(err, req.abort, provider);
2017
+ }
2018
+ return step.done ? void 0 : step.value;
2019
+ };
2020
+ try {
2021
+ while (true) {
2022
+ const bytes = await next_bytes();
2023
+ if (bytes === void 0) break;
2024
+ for (const data of framing.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle(data);
2025
+ }
2026
+ } finally {
2027
+ reader.cancel().catch(() => {});
1756
2028
  }
2029
+ const tail = [...framing.push(text_decoder.decode()), ...framing.flush()];
2030
+ for (const data of tail) await aggregator.handle(data);
2031
+ return aggregator.complete();
1757
2032
  }
1758
- //#endregion
1759
- //#region src/engine/providers/sse_native.ts
1760
2033
  /**
1761
- * Incremental SSE decoder shared by the native (raw-HTTP) provider adapters.
1762
- *
1763
- * Wire-neutral framing only: push() takes decoded text as it arrives off the
1764
- * wire (any chunk boundary, including mid-line) and returns the data payloads
1765
- * of every event completed by that chunk; flush() drains an event left open
1766
- * when the stream ends without a trailing blank line. Only `data:` fields
1767
- * matter to the streams fascicle consumes (the Messages API repeats the
1768
- * event type inside the JSON payload and the Chat Completions stream sends
1769
- * nothing but data frames), so `event:`/`id:`/`retry:` fields and `:`
1770
- * comments are dropped. Multi-line data joins with '\n' per the SSE spec.
2034
+ * Incremental newline framing shared by the SSE and NDJSON decoders: buffer
2035
+ * decoded text across arbitrary chunk boundaries, hand each completed line
2036
+ * (CR trimmed) to `handle_line`, and on flush hand over an unterminated tail
2037
+ * line, then `on_flush` for state a decoder holds across lines (an SSE event
2038
+ * never closed by a blank line).
1771
2039
  */
1772
- function create_sse_decoder() {
2040
+ function create_line_splitter(handle_line, on_flush) {
1773
2041
  let buffer = "";
1774
- let data_lines = [];
1775
2042
  const take_line = (raw, out) => {
1776
- const line = raw.endsWith("\r") ? raw.slice(0, -1) : raw;
1777
- if (line.length === 0) {
1778
- if (data_lines.length > 0) {
1779
- out.push(data_lines.join("\n"));
1780
- data_lines = [];
1781
- }
1782
- return;
1783
- }
1784
- if (line.startsWith(":")) return;
1785
- if (line.startsWith("data:")) {
1786
- const value = line.slice(5);
1787
- data_lines.push(value.startsWith(" ") ? value.slice(1) : value);
1788
- }
2043
+ handle_line(raw.endsWith("\r") ? raw.slice(0, -1) : raw, out);
1789
2044
  };
1790
2045
  return {
1791
2046
  push(text) {
@@ -1803,51 +2058,147 @@ function create_sse_decoder() {
1803
2058
  const out = [];
1804
2059
  if (buffer.length > 0) take_line(buffer, out);
1805
2060
  buffer = "";
1806
- if (data_lines.length > 0) {
1807
- out.push(data_lines.join("\n"));
1808
- data_lines = [];
1809
- }
2061
+ on_flush?.(out);
1810
2062
  return out;
1811
2063
  }
1812
2064
  };
1813
2065
  }
1814
- //#endregion
1815
- //#region src/engine/providers/anthropic_native.ts
1816
2066
  /**
1817
- * Native Anthropic provider adapter (raw HTTP).
1818
- *
1819
- * Talks to the Messages API directly over global fetch with zero `ai` /
1820
- * `@ai-sdk/*` in its module graph. It owns request/response mapping only:
1821
- * generate.ts wraps invoke_turn in retry + classification + abort, so
1822
- * failures are thrown in shapes the shared classify_provider_error already
1823
- * understands (`status` + `responseHeaders` for HTTP transients,
1824
- * `kind: 'network'` for transport failures) instead of being re-classified
1825
- * or retried here.
1826
- *
1827
- * Selected via `transport: 'native'` on the anthropic provider init. The
1828
- * adapter name stays 'anthropic' so DEFAULT_PRICING keys and UsageTotals
1829
- * fields keep working across transports. Schema requests ride the engine's
1830
- * prompt + parse + repair loop, so `TurnRequest.schema` is intentionally
1831
- * unread. Streaming hand-rolls the SSE parse; the aggregator rebuilds the
1832
- * non-stream payload shape and feeds it through the same
1833
- * parse_messages_response, so streamed and non-streamed results are equal by
1834
- * construction rather than by parallel code paths.
2067
+ * Decode a tool call's wire `arguments` string into the engine's input value:
2068
+ * non-empty JSON parses (bad JSON is a provider_error naming the tool), and
2069
+ * an absent or empty value means `{}`, the shape some servers send for
2070
+ * no-argument calls. The Ollama wire's structured-object arguments are its
2071
+ * caller's business before this runs.
1835
2072
  */
2073
+ function parse_tool_call_arguments(raw_args, provider, tool_name) {
2074
+ if (typeof raw_args === "string" && raw_args.length > 0) try {
2075
+ return JSON.parse(raw_args);
2076
+ } catch {
2077
+ throw new provider_error(`${provider} native: tool_calls arguments for ${tool_name} is not valid JSON`);
2078
+ }
2079
+ return {};
2080
+ }
1836
2081
  /**
1837
- * Effort level to extended-thinking budget tokens. Lives here (not in
1838
- * anthropic.ts) because both transports map effort from this table and the
1839
- * ai_sdk factory already imports this module for transport dispatch; the
1840
- * reverse import would be a cycle.
2082
+ * Fold engine messages into a wire's message array by role. Serves the wires
2083
+ * whose messages map 1:1 in place (chat/completions, Ollama's /api/chat); the
2084
+ * Messages API's system-hoisting and role-merging rules live in its own
2085
+ * mapper instead.
1841
2086
  */
1842
- const ANTHROPIC_THINKING_BUDGETS = {
1843
- none: 0,
1844
- low: 1024,
1845
- medium: 5e3,
1846
- high: 2e4,
1847
- xhigh: 32e3,
1848
- max: 64e3
1849
- };
1850
- const ANTHROPIC_VERSION = "2023-06-01";
2087
+ function map_messages_by_role(messages, wire) {
2088
+ const out = [];
2089
+ for (const message of messages) switch (message.role) {
2090
+ case "system":
2091
+ out.push(wire.system(message.content));
2092
+ break;
2093
+ case "user":
2094
+ out.push(wire.user(message.content));
2095
+ break;
2096
+ case "assistant":
2097
+ out.push(wire.assistant(message.content));
2098
+ break;
2099
+ case "tool":
2100
+ out.push(wire.tool(message));
2101
+ break;
2102
+ }
2103
+ return out;
2104
+ }
2105
+ /**
2106
+ * Partition assistant content parts into the text runs and tool calls the
2107
+ * wire mappers serialize separately.
2108
+ */
2109
+ function split_assistant_parts(parts) {
2110
+ const texts = [];
2111
+ const tool_parts = [];
2112
+ for (const part of parts) if (part.type === "text") texts.push(part.text);
2113
+ else tool_parts.push(part);
2114
+ return {
2115
+ texts,
2116
+ tool_parts
2117
+ };
2118
+ }
2119
+ /**
2120
+ * Read the api_key and base_url every OpenAI-compatible dialect starts from:
2121
+ * a missing or non-string api_key becomes '' (the core's empty-key guard owns
2122
+ * the throw), and an absent base_url falls back to the dialect's default.
2123
+ */
2124
+ function read_dialect_init(init, default_base_url) {
2125
+ return {
2126
+ api_key: typeof init.api_key === "string" ? init.api_key : "",
2127
+ base_url: typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : default_base_url
2128
+ };
2129
+ }
2130
+ //#endregion
2131
+ //#region src/engine/providers/sse_native.ts
2132
+ /**
2133
+ * Incremental SSE decoder shared by the native (raw-HTTP) provider adapters.
2134
+ */
2135
+ /**
2136
+ * Wire-neutral framing only: push() takes decoded text as it arrives off the
2137
+ * wire (any chunk boundary, including mid-line) and returns the data payloads
2138
+ * of every event completed by that chunk; flush() drains an event left open
2139
+ * when the stream ends without a trailing blank line. Only `data:` fields
2140
+ * matter to the streams fascicle consumes (the Messages API repeats the
2141
+ * event type inside the JSON payload and the Chat Completions stream sends
2142
+ * nothing but data frames), so `event:`/`id:`/`retry:` fields and `:`
2143
+ * comments are dropped. Multi-line data joins with '\n' per the SSE spec.
2144
+ */
2145
+ function create_sse_decoder() {
2146
+ let data_lines = [];
2147
+ const close_event = (out) => {
2148
+ if (data_lines.length > 0) {
2149
+ out.push(data_lines.join("\n"));
2150
+ data_lines = [];
2151
+ }
2152
+ };
2153
+ return create_line_splitter((line, out) => {
2154
+ if (line.length === 0) {
2155
+ close_event(out);
2156
+ return;
2157
+ }
2158
+ if (line.startsWith(":")) return;
2159
+ if (line.startsWith("data:")) {
2160
+ const value = line.slice(5);
2161
+ data_lines.push(value.startsWith(" ") ? value.slice(1) : value);
2162
+ }
2163
+ }, close_event);
2164
+ }
2165
+ //#endregion
2166
+ //#region src/engine/providers/anthropic_native.ts
2167
+ /**
2168
+ * Native Anthropic provider adapter (raw HTTP).
2169
+ *
2170
+ * Talks to the Messages API directly over global fetch with zero `ai` /
2171
+ * `@ai-sdk/*` in its module graph. It owns request/response mapping only:
2172
+ * generate.ts wraps invoke_turn in retry + classification + abort, so
2173
+ * failures are thrown in shapes the shared classify_provider_error already
2174
+ * understands (`status` + `responseHeaders` for HTTP transients,
2175
+ * `kind: 'network'` for transport failures) instead of being re-classified
2176
+ * or retried here.
2177
+ *
2178
+ * Selected via `transport: 'native'` on the anthropic provider init. The
2179
+ * adapter name stays 'anthropic' so DEFAULT_PRICING keys and UsageTotals
2180
+ * fields keep working across transports. Schema requests ride the engine's
2181
+ * prompt + parse + repair loop, so `TurnRequest.schema` is intentionally
2182
+ * unread. Streaming hand-rolls the SSE parse; the aggregator rebuilds the
2183
+ * non-stream payload shape and feeds it through the same
2184
+ * parse_messages_response, so streamed and non-streamed results are equal by
2185
+ * construction rather than by parallel code paths.
2186
+ */
2187
+ /**
2188
+ * Effort level to extended-thinking budget tokens. Lives here (not in
2189
+ * anthropic.ts) because both transports map effort from this table and the
2190
+ * ai_sdk factory already imports this module for transport dispatch; the
2191
+ * reverse import would be a cycle.
2192
+ */
2193
+ const ANTHROPIC_THINKING_BUDGETS = {
2194
+ none: 0,
2195
+ low: 1024,
2196
+ medium: 5e3,
2197
+ high: 2e4,
2198
+ xhigh: 32e3,
2199
+ max: 64e3
2200
+ };
2201
+ const ANTHROPIC_VERSION = "2023-06-01";
1851
2202
  /**
1852
2203
  * Matches the @ai-sdk/anthropic baseURL convention (origin + /v1) so a
1853
2204
  * base_url configured for the ai_sdk transport keeps pointing at the same
@@ -1861,22 +2212,26 @@ const DEFAULT_BASE_URL$2 = "https://api.anthropic.com/v1";
1861
2212
  */
1862
2213
  const DEFAULT_MAX_TOKENS = 4096;
1863
2214
  /**
2215
+ * Wrap non-blank text in a single text block; blank text maps to no blocks,
2216
+ * since the API rejects empty text blocks.
2217
+ */
2218
+ function text_blocks(text) {
2219
+ return text.trim().length > 0 ? [{
2220
+ type: "text",
2221
+ text
2222
+ }] : [];
2223
+ }
2224
+ /**
1864
2225
  * Map user message content to Messages-API text blocks, dropping empty text
1865
2226
  * (the API rejects empty blocks). Image parts throw a capability error: they
1866
2227
  * are not mapped on this transport.
1867
2228
  */
1868
2229
  function to_user_blocks(content) {
1869
- if (typeof content === "string") return content.trim().length > 0 ? [{
1870
- type: "text",
1871
- text: content
1872
- }] : [];
2230
+ if (typeof content === "string") return text_blocks(content);
1873
2231
  const blocks = [];
1874
2232
  for (const part of content) {
1875
2233
  if (part.type === "image") throw new provider_capability_error("anthropic", "image_input", "image parts are not mapped on the native transport; use transport: 'ai_sdk'");
1876
- if (part.text.trim().length > 0) blocks.push({
1877
- type: "text",
1878
- text: part.text
1879
- });
2234
+ blocks.push(...text_blocks(part.text));
1880
2235
  }
1881
2236
  return blocks;
1882
2237
  }
@@ -1885,17 +2240,10 @@ function to_user_blocks(content) {
1885
2240
  * text blocks (empties dropped), tool-call parts become tool_use blocks.
1886
2241
  */
1887
2242
  function to_assistant_blocks(content) {
1888
- if (typeof content === "string") return content.trim().length > 0 ? [{
1889
- type: "text",
1890
- text: content
1891
- }] : [];
2243
+ if (typeof content === "string") return text_blocks(content);
1892
2244
  const blocks = [];
1893
- for (const part of content) if (part.type === "text") {
1894
- if (part.text.trim().length > 0) blocks.push({
1895
- type: "text",
1896
- text: part.text
1897
- });
1898
- } else blocks.push({
2245
+ for (const part of content) if (part.type === "text") blocks.push(...text_blocks(part.text));
2246
+ else blocks.push({
1899
2247
  type: "tool_use",
1900
2248
  id: part.id,
1901
2249
  name: part.name,
@@ -2032,32 +2380,63 @@ function map_anthropic_usage(raw) {
2032
2380
  return totals;
2033
2381
  }
2034
2382
  /**
2383
+ * Read the id and name a tool_use block must carry, throwing a
2384
+ * provider_error naming `where` the malformed block appeared.
2385
+ */
2386
+ function read_tool_use_identity(block, where) {
2387
+ const id = Reflect.get(block, "id");
2388
+ const name = Reflect.get(block, "name");
2389
+ if (typeof id !== "string" || typeof name !== "string") throw new provider_error(`anthropic native: malformed tool_use block in ${where}`);
2390
+ return {
2391
+ id,
2392
+ name
2393
+ };
2394
+ }
2395
+ /**
2396
+ * Fold one response `content` block into the text/tool_call accumulators.
2397
+ * Text blocks append their string; tool_use blocks contribute a call whose id
2398
+ * and name are validated by read_tool_use_identity; thinking /
2399
+ * redacted_thinking blocks have no TurnResult field and are skipped.
2400
+ */
2401
+ function push_content_block(block, text_parts, tool_calls) {
2402
+ if (block === null || typeof block !== "object") return;
2403
+ const type = Reflect.get(block, "type");
2404
+ if (type === "text") {
2405
+ const text = Reflect.get(block, "text");
2406
+ if (typeof text === "string") text_parts.push(text);
2407
+ return;
2408
+ }
2409
+ if (type === "tool_use") {
2410
+ const { id, name } = read_tool_use_identity(block, "response content");
2411
+ tool_calls.push({
2412
+ id,
2413
+ name,
2414
+ input: Reflect.get(block, "input")
2415
+ });
2416
+ }
2417
+ }
2418
+ /**
2419
+ * Walk a response payload's `content` array into the flat text and tool_call
2420
+ * lists a TurnResult carries. A non-array `content` yields empty lists,
2421
+ * matching a payload that carried no content blocks.
2422
+ */
2423
+ function collect_content_blocks(content) {
2424
+ const text_parts = [];
2425
+ const tool_calls = [];
2426
+ if (Array.isArray(content)) for (const block of content) push_content_block(block, text_parts, tool_calls);
2427
+ return {
2428
+ text_parts,
2429
+ tool_calls
2430
+ };
2431
+ }
2432
+ /**
2035
2433
  * Parse a non-stream Messages-API response payload into a TurnResult. The
2036
2434
  * stream aggregator rebuilds this same payload shape and feeds it here too,
2037
2435
  * so both paths share one parser.
2038
2436
  */
2039
2437
  function parse_messages_response(payload) {
2040
2438
  if (payload === null || typeof payload !== "object") throw new provider_error("anthropic native: response payload is not a JSON object");
2041
- const content = Reflect.get(payload, "content");
2042
- const text_parts = [];
2043
- const tool_calls = [];
2044
- if (Array.isArray(content)) for (const block of content) {
2045
- if (block === null || typeof block !== "object") continue;
2046
- const type = Reflect.get(block, "type");
2047
- if (type === "text") {
2048
- const text = Reflect.get(block, "text");
2049
- if (typeof text === "string") text_parts.push(text);
2050
- } else if (type === "tool_use") {
2051
- const id = Reflect.get(block, "id");
2052
- const name = Reflect.get(block, "name");
2053
- if (typeof id !== "string" || typeof name !== "string") throw new provider_error("anthropic native: malformed tool_use block in response content");
2054
- tool_calls.push({
2055
- id,
2056
- name,
2057
- input: Reflect.get(block, "input")
2058
- });
2059
- }
2060
- }
2439
+ const { text_parts, tool_calls } = collect_content_blocks(Reflect.get(payload, "content"));
2061
2440
  return {
2062
2441
  text: text_parts.join(""),
2063
2442
  tool_calls,
@@ -2138,9 +2517,7 @@ function create_stream_aggregator(step_index, dispatch) {
2138
2517
  return;
2139
2518
  }
2140
2519
  if (type === "tool_use") {
2141
- const id = Reflect.get(block, "id");
2142
- const name = Reflect.get(block, "name");
2143
- if (typeof id !== "string" || typeof name !== "string") throw new provider_error("anthropic native: malformed tool_use block in stream");
2520
+ const { id, name } = read_tool_use_identity(block, "stream");
2144
2521
  const synthetic = {
2145
2522
  type: "tool_use",
2146
2523
  id,
@@ -2218,7 +2595,7 @@ function create_stream_aggregator(step_index, dispatch) {
2218
2595
  });
2219
2596
  };
2220
2597
  return {
2221
- async handle_event(data) {
2598
+ async handle(data) {
2222
2599
  let event;
2223
2600
  try {
2224
2601
  event = JSON.parse(data);
@@ -2273,93 +2650,13 @@ function create_stream_aggregator(step_index, dispatch) {
2273
2650
  }
2274
2651
  };
2275
2652
  }
2276
- /** Anthropic error bodies are `{ type: 'error', error: { message } }`; fall back to the raw body. */
2277
- function extract_error_message$2(body) {
2278
- if (body.length === 0) return "(empty body)";
2279
- try {
2280
- const parsed = JSON.parse(body);
2281
- if (parsed !== null && typeof parsed === "object") {
2282
- const error = Reflect.get(parsed, "error");
2283
- if (error !== null && typeof error === "object") {
2284
- const message = Reflect.get(error, "message");
2285
- if (typeof message === "string" && message.length > 0) return message;
2286
- }
2287
- }
2288
- } catch {}
2289
- return body.length > 300 ? `${body.slice(0, 300)}...` : body;
2290
- }
2291
- /**
2292
- * Map a non-2xx response to what the engine's retry stack expects: 401
2293
- * becomes a typed provider_auth_error, 429/5xx keep `status` +
2294
- * `responseHeaders` so classify_provider_error marks them retryable
2295
- * (rate_limit honoring retry-after, provider_5xx), and any other 4xx is a
2296
- * permanent provider_error that surfaces as-is.
2297
- */
2298
- async function response_error$2(response) {
2299
- let body = "";
2300
- try {
2301
- body = await response.text();
2302
- } catch {}
2303
- const detail = extract_error_message$2(body);
2304
- const status = response.status;
2305
- if (status === 401) return new provider_auth_error("anthropic", `anthropic authentication failed (401): ${detail}`);
2306
- if (status === 429 || status >= 500) {
2307
- const retry_after = response.headers.get("retry-after");
2308
- return Object.assign(/* @__PURE__ */ new Error(`anthropic API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
2309
- }
2310
- return new provider_error(`anthropic API error ${status}: ${detail}`, {
2311
- status,
2312
- body
2313
- });
2314
- }
2315
- /**
2316
- * A user abort surfaces as the fetch/reader AbortError; retry_turn converts
2317
- * it via its own signal check, so it is rethrown untouched. Everything else
2318
- * is a transport failure wrapped in the `kind: 'network'` shape the shared
2319
- * classify_provider_error marks retryable.
2320
- */
2321
- function rethrow_network_failure$2(err, abort) {
2322
- if (abort.aborted) throw err;
2323
- const detail = err instanceof Error ? err.message : String(err);
2324
- throw Object.assign(/* @__PURE__ */ new Error(`anthropic native: network failure: ${detail}`), { kind: "network" });
2325
- }
2326
2653
  /**
2327
- * Drain a streaming Messages response: decode bytes, reassemble SSE events,
2328
- * and feed them to the aggregator, which dispatches chunks through
2329
- * req.dispatch_chunk as they arrive. Transport failures mid-read wrap as
2330
- * network errors; aggregator throws (malformed events, mid-stream error
2331
- * events, a rejecting on_chunk) pass through untouched, with the reader
2332
- * cancelled so the connection is released.
2654
+ * Drain a streaming Messages response through the shared framed-stream
2655
+ * consumer: SSE framing feeding the aggregator, which dispatches chunks
2656
+ * through req.dispatch_chunk as they arrive.
2333
2657
  */
2334
2658
  async function consume_sse_response$1(response, req) {
2335
- const body = response.body;
2336
- if (body === null) throw new provider_error("anthropic native: streaming response has no body");
2337
- const dispatch = req.dispatch_chunk ?? (async () => {});
2338
- const aggregator = create_stream_aggregator(req.step_index, dispatch);
2339
- const reader = body.getReader();
2340
- const text_decoder = new TextDecoder();
2341
- const sse = create_sse_decoder();
2342
- const next_bytes = async () => {
2343
- let step;
2344
- try {
2345
- step = await reader.read();
2346
- } catch (err) {
2347
- rethrow_network_failure$2(err, req.abort);
2348
- }
2349
- return step.done ? void 0 : step.value;
2350
- };
2351
- try {
2352
- while (true) {
2353
- const bytes = await next_bytes();
2354
- if (bytes === void 0) break;
2355
- for (const data of sse.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle_event(data);
2356
- }
2357
- } finally {
2358
- reader.cancel().catch(() => {});
2359
- }
2360
- const tail = [...sse.push(text_decoder.decode()), ...sse.flush()];
2361
- for (const data of tail) await aggregator.handle_event(data);
2362
- return aggregator.complete();
2659
+ return consume_framed_stream(response, req, "anthropic", create_sse_decoder(), (dispatch) => create_stream_aggregator(req.step_index, dispatch));
2363
2660
  }
2364
2661
  const SUPPORTED$10 = /* @__PURE__ */ new Set([
2365
2662
  "text",
@@ -2381,24 +2678,18 @@ const create_anthropic_native_adapter = (init) => {
2381
2678
  kind: "native",
2382
2679
  name: "anthropic",
2383
2680
  async invoke_turn(req) {
2384
- let response;
2385
- try {
2386
- response = await fetch(`${base_url}/messages`, {
2387
- method: "POST",
2388
- headers: {
2389
- "content-type": "application/json",
2390
- "x-api-key": api_key,
2391
- "anthropic-version": ANTHROPIC_VERSION
2392
- },
2393
- body: JSON.stringify(build_messages_body(req)),
2394
- signal: req.abort
2395
- });
2396
- } catch (err) {
2397
- rethrow_network_failure$2(err, req.abort);
2398
- }
2399
- if (!response.ok) throw await response_error$2(response);
2400
- if (req.stream) return consume_sse_response$1(response, req);
2401
- return parse_messages_response(await response.json());
2681
+ return invoke_http_turn(req, "anthropic", {
2682
+ url: `${base_url}/messages`,
2683
+ headers: {
2684
+ "content-type": "application/json",
2685
+ "x-api-key": api_key,
2686
+ "anthropic-version": ANTHROPIC_VERSION
2687
+ },
2688
+ build_body: () => build_messages_body(req)
2689
+ }, {
2690
+ stream: (response) => consume_sse_response$1(response, req),
2691
+ parse: parse_messages_response
2692
+ });
2402
2693
  },
2403
2694
  supports: (capability) => SUPPORTED$10.has(capability)
2404
2695
  };
@@ -2537,18 +2828,16 @@ function to_assistant_message$1(content) {
2537
2828
  role: "assistant",
2538
2829
  content
2539
2830
  };
2540
- const text_parts = [];
2541
- const tool_calls = [];
2542
- for (const part of content) if (part.type === "text") text_parts.push(part.text);
2543
- else tool_calls.push({
2831
+ const { texts, tool_parts } = split_assistant_parts(content);
2832
+ const tool_calls = tool_parts.map((part) => ({
2544
2833
  id: part.id,
2545
2834
  type: "function",
2546
2835
  function: {
2547
2836
  name: part.name,
2548
2837
  arguments: JSON.stringify(part.input)
2549
2838
  }
2550
- });
2551
- const text = text_parts.join("");
2839
+ }));
2840
+ const text = texts.join("");
2552
2841
  const message = {
2553
2842
  role: "assistant",
2554
2843
  content: text.length > 0 || tool_calls.length === 0 ? text : null
@@ -2563,32 +2852,22 @@ function to_assistant_message$1(content) {
2563
2852
  * `tool` role messages keyed by tool_call_id.
2564
2853
  */
2565
2854
  function to_chat_messages(messages, provider) {
2566
- const out = [];
2567
- for (const message of messages) switch (message.role) {
2568
- case "system":
2569
- out.push({
2570
- role: "system",
2571
- content: message.content
2572
- });
2573
- break;
2574
- case "user":
2575
- out.push({
2576
- role: "user",
2577
- content: to_user_content$1(message.content, provider)
2578
- });
2579
- break;
2580
- case "assistant":
2581
- out.push(to_assistant_message$1(message.content));
2582
- break;
2583
- case "tool":
2584
- out.push({
2585
- role: "tool",
2586
- tool_call_id: message.tool_call_id,
2587
- content: message.content
2588
- });
2589
- break;
2590
- }
2591
- return out;
2855
+ return map_messages_by_role(messages, {
2856
+ system: (content) => ({
2857
+ role: "system",
2858
+ content
2859
+ }),
2860
+ user: (content) => ({
2861
+ role: "user",
2862
+ content: to_user_content$1(content, provider)
2863
+ }),
2864
+ assistant: to_assistant_message$1,
2865
+ tool: (message) => ({
2866
+ role: "tool",
2867
+ tool_call_id: message.tool_call_id,
2868
+ content: message.content
2869
+ })
2870
+ });
2592
2871
  }
2593
2872
  /**
2594
2873
  * Map fascicle Tool[] to the Chat Completions function-tool shape,
@@ -2699,17 +2978,10 @@ function parse_tool_call(raw, provider) {
2699
2978
  if (typeof id !== "string" || fn === null || typeof fn !== "object") throw new provider_error(`${provider} native: malformed tool_calls entry in response`);
2700
2979
  const name = Reflect.get(fn, "name");
2701
2980
  if (typeof name !== "string") throw new provider_error(`${provider} native: malformed tool_calls entry in response`);
2702
- const raw_args = Reflect.get(fn, "arguments");
2703
- let input = {};
2704
- if (typeof raw_args === "string" && raw_args.length > 0) try {
2705
- input = JSON.parse(raw_args);
2706
- } catch {
2707
- throw new provider_error(`${provider} native: tool_calls arguments for ${name} is not valid JSON`);
2708
- }
2709
2981
  return {
2710
2982
  id,
2711
2983
  name,
2712
- input
2984
+ input: parse_tool_call_arguments(Reflect.get(fn, "arguments"), provider, name)
2713
2985
  };
2714
2986
  }
2715
2987
  /**
@@ -2888,93 +3160,21 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
2888
3160
  }
2889
3161
  };
2890
3162
  }
2891
- /** OpenAI-style error bodies are `{ error: { message } }`; fall back to the raw body. */
2892
- function extract_error_message$1(body) {
2893
- if (body.length === 0) return "(empty body)";
2894
- try {
2895
- const parsed = JSON.parse(body);
2896
- if (parsed !== null && typeof parsed === "object") {
2897
- const error = Reflect.get(parsed, "error");
2898
- if (error !== null && typeof error === "object") {
2899
- const message = Reflect.get(error, "message");
2900
- if (typeof message === "string" && message.length > 0) return message;
2901
- }
2902
- }
2903
- } catch {}
2904
- return body.length > 300 ? `${body.slice(0, 300)}...` : body;
2905
- }
2906
3163
  /**
2907
- * Map a non-2xx response to what the engine's retry stack expects: 401
2908
- * becomes a typed provider_auth_error, 429/5xx keep `status` +
2909
- * `responseHeaders` so classify_provider_error marks them retryable
2910
- * (rate_limit honoring retry-after, provider_5xx), and any other 4xx is a
2911
- * permanent provider_error that surfaces as-is.
2912
- */
2913
- async function response_error$1(response, provider) {
2914
- let body = "";
2915
- try {
2916
- body = await response.text();
2917
- } catch {}
2918
- const detail = extract_error_message$1(body);
2919
- const status = response.status;
2920
- if (status === 401) return new provider_auth_error(provider, `${provider} authentication failed (401): ${detail}`);
2921
- if (status === 429 || status >= 500) {
2922
- const retry_after = response.headers.get("retry-after");
2923
- return Object.assign(/* @__PURE__ */ new Error(`${provider} API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
2924
- }
2925
- return new provider_error(`${provider} API error ${status}: ${detail}`, {
2926
- status,
2927
- body
2928
- });
2929
- }
2930
- /**
2931
- * A user abort surfaces as the fetch AbortError; retry_turn converts it via
2932
- * its own signal check, so it is rethrown untouched. Everything else is a
2933
- * transport failure wrapped in the `kind: 'network'` shape the shared
2934
- * classify_provider_error marks retryable.
2935
- */
2936
- function rethrow_network_failure$1(err, abort, provider) {
2937
- if (abort.aborted) throw err;
2938
- const detail = err instanceof Error ? err.message : String(err);
2939
- throw Object.assign(/* @__PURE__ */ new Error(`${provider} native: network failure: ${detail}`), { kind: "network" });
2940
- }
2941
- /**
2942
- * Drain a streaming chat/completions response: decode bytes, reassemble SSE
2943
- * events, and feed each data payload to the aggregator, which dispatches
2944
- * chunks through req.dispatch_chunk as they arrive. Transport failures
2945
- * mid-read wrap as network errors; aggregator throws (malformed frames, a
2946
- * rejecting on_chunk) pass through untouched, with the reader cancelled so
2947
- * the connection is released.
3164
+ * Drain a streaming chat/completions response through the shared
3165
+ * framed-stream consumer: SSE framing feeding the aggregator, which
3166
+ * dispatches chunks through req.dispatch_chunk as they arrive. The
3167
+ * aggregator's `handle_data` name is part of its exported API, so it is
3168
+ * adapted to the consumer's `handle` shape here rather than renamed.
2948
3169
  */
2949
3170
  async function consume_sse_response(response, req, dialect) {
2950
- const body = response.body;
2951
- if (body === null) throw new provider_error(`${dialect.name} native: streaming response has no body`);
2952
- const dispatch = req.dispatch_chunk ?? (async () => {});
2953
- const aggregator = create_chat_stream_aggregator(dialect, req.step_index, dispatch);
2954
- const reader = body.getReader();
2955
- const text_decoder = new TextDecoder();
2956
- const sse = create_sse_decoder();
2957
- const next_bytes = async () => {
2958
- let step;
2959
- try {
2960
- step = await reader.read();
2961
- } catch (err) {
2962
- rethrow_network_failure$1(err, req.abort, dialect.name);
2963
- }
2964
- return step.done ? void 0 : step.value;
2965
- };
2966
- try {
2967
- while (true) {
2968
- const bytes = await next_bytes();
2969
- if (bytes === void 0) break;
2970
- for (const data of sse.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle_data(data);
2971
- }
2972
- } finally {
2973
- reader.cancel().catch(() => {});
2974
- }
2975
- const tail = [...sse.push(text_decoder.decode()), ...sse.flush()];
2976
- for (const data of tail) await aggregator.handle_data(data);
2977
- return aggregator.complete();
3171
+ return consume_framed_stream(response, req, dialect.name, create_sse_decoder(), (dispatch) => {
3172
+ const aggregator = create_chat_stream_aggregator(dialect, req.step_index, dispatch);
3173
+ return {
3174
+ handle: aggregator.handle_data,
3175
+ complete: aggregator.complete
3176
+ };
3177
+ });
2978
3178
  }
2979
3179
  const SUPPORTED$8 = /* @__PURE__ */ new Set([
2980
3180
  "text",
@@ -3001,20 +3201,14 @@ const create_openai_compatible_adapter = (dialect) => {
3001
3201
  kind: "native",
3002
3202
  name: dialect.name,
3003
3203
  async invoke_turn(req) {
3004
- let response;
3005
- try {
3006
- response = await fetch(`${base_url}/chat/completions`, {
3007
- method: "POST",
3008
- headers,
3009
- body: JSON.stringify(build_chat_completions_body(req, dialect)),
3010
- signal: req.abort
3011
- });
3012
- } catch (err) {
3013
- rethrow_network_failure$1(err, req.abort, dialect.name);
3014
- }
3015
- if (!response.ok) throw await response_error$1(response, dialect.name);
3016
- if (req.stream) return consume_sse_response(response, req, dialect);
3017
- return parse_chat_completion(await response.json(), dialect);
3204
+ return invoke_http_turn(req, dialect.name, {
3205
+ url: `${base_url}/chat/completions`,
3206
+ headers,
3207
+ build_body: () => build_chat_completions_body(req, dialect)
3208
+ }, {
3209
+ stream: (response) => consume_sse_response(response, req, dialect),
3210
+ parse: (payload) => parse_chat_completion(payload, dialect)
3211
+ });
3018
3212
  },
3019
3213
  supports: (capability) => SUPPORTED$8.has(capability)
3020
3214
  };
@@ -3078,8 +3272,7 @@ const create_openai_adapter = (init) => {
3078
3272
  * `openai provider requires a non-empty api_key` engine_config_error.
3079
3273
  */
3080
3274
  const create_openai_native_adapter = (init) => {
3081
- const api_key = typeof init.api_key === "string" ? init.api_key : "";
3082
- const base_url = typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : DEFAULT_BASE_URL$1;
3275
+ const { api_key, base_url } = read_dialect_init(init, DEFAULT_BASE_URL$1);
3083
3276
  const organization = typeof init["organization"] === "string" ? init["organization"] : void 0;
3084
3277
  return create_openai_compatible_adapter({
3085
3278
  name: "openai",
@@ -3203,18 +3396,15 @@ function to_assistant_message(content) {
3203
3396
  role: "assistant",
3204
3397
  content
3205
3398
  };
3206
- const text_parts = [];
3207
- const tool_calls = [];
3208
- for (const part of content) if (part.type === "text") text_parts.push(part.text);
3209
- else tool_calls.push({ function: {
3210
- name: part.name,
3211
- arguments: part.input
3212
- } });
3399
+ const { texts, tool_parts } = split_assistant_parts(content);
3213
3400
  const message = {
3214
3401
  role: "assistant",
3215
- content: text_parts.join("")
3402
+ content: texts.join("")
3216
3403
  };
3217
- if (tool_calls.length > 0) message.tool_calls = tool_calls;
3404
+ if (tool_parts.length > 0) message.tool_calls = tool_parts.map((part) => ({ function: {
3405
+ name: part.name,
3406
+ arguments: part.input
3407
+ } }));
3218
3408
  return message;
3219
3409
  }
3220
3410
  /**
@@ -3223,32 +3413,22 @@ function to_assistant_message(content) {
3223
3413
  * `tool_name`, since this wire has no tool_call_id to round-trip.
3224
3414
  */
3225
3415
  function to_ollama_messages(messages) {
3226
- const out = [];
3227
- for (const message of messages) switch (message.role) {
3228
- case "system":
3229
- out.push({
3230
- role: "system",
3231
- content: message.content
3232
- });
3233
- break;
3234
- case "user":
3235
- out.push({
3236
- role: "user",
3237
- content: to_user_content(message.content)
3238
- });
3239
- break;
3240
- case "assistant":
3241
- out.push(to_assistant_message(message.content));
3242
- break;
3243
- case "tool":
3244
- out.push({
3245
- role: "tool",
3246
- tool_name: message.name,
3247
- content: message.content
3248
- });
3249
- break;
3250
- }
3251
- return out;
3416
+ return map_messages_by_role(messages, {
3417
+ system: (content) => ({
3418
+ role: "system",
3419
+ content
3420
+ }),
3421
+ user: (content) => ({
3422
+ role: "user",
3423
+ content: to_user_content(content)
3424
+ }),
3425
+ assistant: to_assistant_message,
3426
+ tool: (message) => ({
3427
+ role: "tool",
3428
+ tool_name: message.name,
3429
+ content: message.content
3430
+ })
3431
+ });
3252
3432
  }
3253
3433
  /**
3254
3434
  * Build the /api/chat request body for one turn: mapped messages and tools,
@@ -3325,17 +3505,10 @@ function parse_ollama_tool_call(raw, ordinal, step_index) {
3325
3505
  const wire_id = Reflect.get(raw, "id");
3326
3506
  const id = typeof wire_id === "string" && wire_id.length > 0 ? wire_id : `ollama_call_${step_index}_${ordinal}`;
3327
3507
  const raw_args = Reflect.get(fn, "arguments");
3328
- let input = {};
3329
- if (typeof raw_args === "string" && raw_args.length > 0) try {
3330
- input = JSON.parse(raw_args);
3331
- } catch {
3332
- throw new provider_error(`ollama native: tool_calls arguments for ${name} is not valid JSON`);
3333
- }
3334
- else if (raw_args !== null && typeof raw_args === "object") input = raw_args;
3335
3508
  return {
3336
3509
  id,
3337
3510
  name,
3338
- input
3511
+ input: raw_args !== null && typeof raw_args === "object" ? raw_args : parse_tool_call_arguments(raw_args, "ollama", name)
3339
3512
  };
3340
3513
  }
3341
3514
  /**
@@ -3358,40 +3531,15 @@ function parse_ollama_chat(payload, step_index) {
3358
3531
  };
3359
3532
  }
3360
3533
  /**
3361
- * Append one decoded line to `out`, trimming a trailing `\r` and dropping
3362
- * blank lines; JSON parsing is the aggregator's job.
3363
- */
3364
- function take_line(raw, out) {
3365
- const line = raw.endsWith("\r") ? raw.slice(0, -1) : raw;
3366
- if (line.trim().length > 0) out.push(line);
3367
- }
3368
- /**
3369
3534
  * Incremental NDJSON framing: push() takes decoded text at any chunk
3370
- * boundary (including mid-line) and returns the complete lines it finished;
3371
- * flush() drains a final line left open when the stream ends without a
3372
- * trailing newline.
3535
+ * boundary (including mid-line) and returns the complete lines it finished
3536
+ * (blank lines dropped; JSON parsing is the aggregator's job); flush() drains
3537
+ * a final line left open when the stream ends without a trailing newline.
3373
3538
  */
3374
3539
  function create_ndjson_decoder() {
3375
- let buffer = "";
3376
- return {
3377
- push(text) {
3378
- buffer += text;
3379
- const out = [];
3380
- let newline = buffer.indexOf("\n");
3381
- while (newline >= 0) {
3382
- take_line(buffer.slice(0, newline), out);
3383
- buffer = buffer.slice(newline + 1);
3384
- newline = buffer.indexOf("\n");
3385
- }
3386
- return out;
3387
- },
3388
- flush() {
3389
- const out = [];
3390
- if (buffer.length > 0) take_line(buffer, out);
3391
- buffer = "";
3392
- return out;
3393
- }
3394
- };
3540
+ return create_line_splitter((line, out) => {
3541
+ if (line.trim().length > 0) out.push(line);
3542
+ });
3395
3543
  }
3396
3544
  /**
3397
3545
  * Consume /api/chat NDJSON frames, dispatching StreamChunks as they arrive
@@ -3445,7 +3593,7 @@ function create_ollama_stream_aggregator(step_index, dispatch) {
3445
3593
  }
3446
3594
  };
3447
3595
  return {
3448
- async handle_line(line) {
3596
+ async handle(line) {
3449
3597
  if (done) return;
3450
3598
  let frame;
3451
3599
  try {
@@ -3491,94 +3639,13 @@ function create_ollama_stream_aggregator(step_index, dispatch) {
3491
3639
  }
3492
3640
  };
3493
3641
  }
3494
- /** Ollama error bodies are `{ error: "message" }`; tolerate the nested OpenAI shape too. */
3495
- function extract_error_message(body) {
3496
- if (body.length === 0) return "(empty body)";
3497
- try {
3498
- const parsed = JSON.parse(body);
3499
- if (parsed !== null && typeof parsed === "object") {
3500
- const error = Reflect.get(parsed, "error");
3501
- if (typeof error === "string" && error.length > 0) return error;
3502
- if (error !== null && typeof error === "object") {
3503
- const message = Reflect.get(error, "message");
3504
- if (typeof message === "string" && message.length > 0) return message;
3505
- }
3506
- }
3507
- } catch {}
3508
- return body.length > 300 ? `${body.slice(0, 300)}...` : body;
3509
- }
3510
- /**
3511
- * Map a non-2xx response to what the engine's retry stack expects: 401
3512
- * becomes a typed provider_auth_error (rare locally, but base_url may point
3513
- * at a fronted daemon), 429/5xx keep `status` + `responseHeaders` so
3514
- * classify_provider_error marks them retryable, and any other 4xx (a missing
3515
- * model, a malformed request) is a permanent provider_error.
3516
- */
3517
- async function response_error(response) {
3518
- let body = "";
3519
- try {
3520
- body = await response.text();
3521
- } catch {}
3522
- const detail = extract_error_message(body);
3523
- const status = response.status;
3524
- if (status === 401) return new provider_auth_error("ollama", `ollama authentication failed (401): ${detail}`);
3525
- if (status === 429 || status >= 500) {
3526
- const retry_after = response.headers.get("retry-after");
3527
- return Object.assign(/* @__PURE__ */ new Error(`ollama API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
3528
- }
3529
- return new provider_error(`ollama API error ${status}: ${detail}`, {
3530
- status,
3531
- body
3532
- });
3533
- }
3534
- /**
3535
- * A user abort surfaces as the fetch AbortError; retry_turn converts it via
3536
- * its own signal check, so it is rethrown untouched. Everything else is a
3537
- * transport failure wrapped in the `kind: 'network'` shape the shared
3538
- * classify_provider_error marks retryable.
3539
- */
3540
- function rethrow_network_failure(err, abort) {
3541
- if (abort.aborted) throw err;
3542
- const detail = err instanceof Error ? err.message : String(err);
3543
- throw Object.assign(/* @__PURE__ */ new Error(`ollama native: network failure: ${detail}`), { kind: "network" });
3544
- }
3545
3642
  /**
3546
- * Drain a streaming /api/chat response: decode bytes, reassemble NDJSON
3547
- * lines, and feed each to the aggregator, which dispatches chunks through
3548
- * req.dispatch_chunk as they arrive. Transport failures mid-read wrap as
3549
- * network errors; aggregator throws (malformed frames, a rejecting on_chunk)
3550
- * pass through untouched, with the reader cancelled so the connection is
3551
- * released.
3643
+ * Drain a streaming /api/chat response through the shared framed-stream
3644
+ * consumer: NDJSON framing feeding the aggregator, which dispatches chunks
3645
+ * through req.dispatch_chunk as they arrive.
3552
3646
  */
3553
3647
  async function consume_ndjson_response(response, req) {
3554
- const body = response.body;
3555
- if (body === null) throw new provider_error("ollama native: streaming response has no body");
3556
- const dispatch = req.dispatch_chunk ?? (async () => {});
3557
- const aggregator = create_ollama_stream_aggregator(req.step_index, dispatch);
3558
- const reader = body.getReader();
3559
- const text_decoder = new TextDecoder();
3560
- const ndjson = create_ndjson_decoder();
3561
- const next_bytes = async () => {
3562
- let step;
3563
- try {
3564
- step = await reader.read();
3565
- } catch (err) {
3566
- rethrow_network_failure(err, req.abort);
3567
- }
3568
- return step.done ? void 0 : step.value;
3569
- };
3570
- try {
3571
- while (true) {
3572
- const bytes = await next_bytes();
3573
- if (bytes === void 0) break;
3574
- for (const line of ndjson.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle_line(line);
3575
- }
3576
- } finally {
3577
- reader.cancel().catch(() => {});
3578
- }
3579
- const tail = [...ndjson.push(text_decoder.decode()), ...ndjson.flush()];
3580
- for (const line of tail) await aggregator.handle_line(line);
3581
- return aggregator.complete();
3648
+ return consume_framed_stream(response, req, "ollama", create_ndjson_decoder(), (dispatch) => create_ollama_stream_aggregator(req.step_index, dispatch));
3582
3649
  }
3583
3650
  const SUPPORTED$5 = /* @__PURE__ */ new Set([
3584
3651
  "text",
@@ -3599,20 +3666,14 @@ const create_ollama_native_adapter = (init) => {
3599
3666
  kind: "native",
3600
3667
  name: "ollama",
3601
3668
  async invoke_turn(req) {
3602
- let response;
3603
- try {
3604
- response = await fetch(`${base_url}/api/chat`, {
3605
- method: "POST",
3606
- headers: { "content-type": "application/json" },
3607
- body: JSON.stringify(build_ollama_chat_body(req)),
3608
- signal: req.abort
3609
- });
3610
- } catch (err) {
3611
- rethrow_network_failure(err, req.abort);
3612
- }
3613
- if (!response.ok) throw await response_error(response);
3614
- if (req.stream) return consume_ndjson_response(response, req);
3615
- return parse_ollama_chat(await response.json(), req.step_index);
3669
+ return invoke_http_turn(req, "ollama", {
3670
+ url: `${base_url}/api/chat`,
3671
+ headers: { "content-type": "application/json" },
3672
+ build_body: () => build_ollama_chat_body(req)
3673
+ }, {
3674
+ stream: (response) => consume_ndjson_response(response, req),
3675
+ parse: (payload) => parse_ollama_chat(payload, req.step_index)
3676
+ });
3616
3677
  },
3617
3678
  supports: (capability) => SUPPORTED$5.has(capability)
3618
3679
  };
@@ -3630,15 +3691,11 @@ function translate_ollama_effort(effort) {
3630
3691
  };
3631
3692
  }
3632
3693
  /**
3633
- * Normalize Ollama's raw usage payload into UsageTotals.
3694
+ * Normalize Ollama's raw usage payload into UsageTotals: the shared
3695
+ * local-backend mapper, since Ollama never reports cache or reasoning
3696
+ * tokens.
3634
3697
  */
3635
- function normalize_ollama_usage(raw) {
3636
- const base = default_normalize_usage(raw);
3637
- delete base.cached_input_tokens;
3638
- delete base.cache_write_tokens;
3639
- delete base.reasoning_tokens;
3640
- return base;
3641
- }
3698
+ const normalize_ollama_usage = normalize_local_usage;
3642
3699
  const SUPPORTED$4 = /* @__PURE__ */ new Set([
3643
3700
  "text",
3644
3701
  "tools",
@@ -3685,15 +3742,11 @@ function translate_lmstudio_effort(effort) {
3685
3742
  };
3686
3743
  }
3687
3744
  /**
3688
- * Normalize LM Studio's raw usage payload into UsageTotals.
3745
+ * Normalize LM Studio's raw usage payload into UsageTotals: the shared
3746
+ * local-backend mapper, since LM Studio never reports cache or reasoning
3747
+ * tokens.
3689
3748
  */
3690
- function normalize_lmstudio_usage(raw) {
3691
- const base = default_normalize_usage(raw);
3692
- delete base.cached_input_tokens;
3693
- delete base.cache_write_tokens;
3694
- delete base.reasoning_tokens;
3695
- return base;
3696
- }
3749
+ const normalize_lmstudio_usage = normalize_local_usage;
3697
3750
  const SUPPORTED$3 = /* @__PURE__ */ new Set([
3698
3751
  "text",
3699
3752
  "tools",
@@ -3805,8 +3858,7 @@ const create_openrouter_adapter = (init) => {
3805
3858
  * `openrouter provider requires a non-empty api_key` engine_config_error.
3806
3859
  */
3807
3860
  const create_openrouter_native_adapter = (init) => {
3808
- const api_key = typeof init.api_key === "string" ? init.api_key : "";
3809
- const base_url = typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : DEFAULT_BASE_URL;
3861
+ const { api_key, base_url } = read_dialect_init(init, DEFAULT_BASE_URL);
3810
3862
  const http_referer = typeof init["http_referer"] === "string" ? init["http_referer"] : void 0;
3811
3863
  const x_title = typeof init["x_title"] === "string" ? init["x_title"] : void 0;
3812
3864
  const extra_headers = {};
@@ -4041,6 +4093,51 @@ function validate_auth_config(config) {
4041
4093
  }
4042
4094
  }
4043
4095
  /**
4096
+ * Copy the string-valued entries of `source` onto `target`, dropping any
4097
+ * `undefined` slot (the shape `process.env` and caller overrides both carry).
4098
+ */
4099
+ function copy_string_entries(target, source) {
4100
+ for (const [key, value] of Object.entries(source)) if (typeof value === "string") target[key] = value;
4101
+ }
4102
+ /**
4103
+ * Seed the base subprocess env before caller overrides and the auth scrub.
4104
+ *
4105
+ * `inherit_env: false` starts from an empty env. Otherwise `'oauth'` inherits
4106
+ * the full `process.env` (the CLI needs HOME plus session-specific vars to
4107
+ * reach the logged-in session); every other mode inherits only the standard
4108
+ * keys needed to resolve the sandbox wrapper and the claude binary on PATH.
4109
+ */
4110
+ function seed_base_env(config, auth_mode) {
4111
+ const env = {};
4112
+ if (config.inherit_env === false) return env;
4113
+ if (auth_mode === "oauth") {
4114
+ copy_string_entries(env, process.env);
4115
+ return env;
4116
+ }
4117
+ for (const key of STANDARD_ENV_KEYS) {
4118
+ const value = process.env[key];
4119
+ if (typeof value === "string") env[key] = value;
4120
+ }
4121
+ return env;
4122
+ }
4123
+ /**
4124
+ * Apply the auth-mode scrub last, after base and caller env are merged, so
4125
+ * nothing can re-introduce `ANTHROPIC_API_KEY` under `'oauth'`. `'api_key'`
4126
+ * forces the configured key (empty string when absent); `'auto'` forwards a
4127
+ * non-empty configured key and otherwise leaves the inherited env untouched.
4128
+ */
4129
+ function apply_auth_key(env, config, auth_mode) {
4130
+ if (auth_mode === "oauth") {
4131
+ delete env["ANTHROPIC_API_KEY"];
4132
+ return;
4133
+ }
4134
+ if (auth_mode === "api_key") {
4135
+ env["ANTHROPIC_API_KEY"] = typeof config.api_key === "string" ? config.api_key : "";
4136
+ return;
4137
+ }
4138
+ if (typeof config.api_key === "string" && config.api_key.length > 0) env["ANTHROPIC_API_KEY"] = config.api_key;
4139
+ }
4140
+ /**
4044
4141
  * Compose the subprocess environment for one claude CLI invocation.
4045
4142
  *
4046
4143
  * Regardless of `auth_mode`, seeds the standard process-env keys (PATH,
@@ -4056,25 +4153,9 @@ function validate_auth_config(config) {
4056
4153
  * in, so nothing can re-introduce `ANTHROPIC_API_KEY` under `'oauth'`.
4057
4154
  */
4058
4155
  function build_env(config, caller_env, auth_mode) {
4059
- const env = {};
4060
- if (config.inherit_env !== false) if (auth_mode === "oauth") {
4061
- for (const [k, v] of Object.entries(process.env)) if (typeof v === "string") env[k] = v;
4062
- } else for (const key of STANDARD_ENV_KEYS) {
4063
- const v = process.env[key];
4064
- if (typeof v === "string") env[key] = v;
4065
- }
4066
- if (caller_env !== void 0) {
4067
- for (const [k, v] of Object.entries(caller_env)) if (typeof v === "string") env[k] = v;
4068
- }
4069
- if (auth_mode === "oauth") {
4070
- delete env["ANTHROPIC_API_KEY"];
4071
- return env;
4072
- }
4073
- if (auth_mode === "api_key") {
4074
- env["ANTHROPIC_API_KEY"] = typeof config.api_key === "string" ? config.api_key : "";
4075
- return env;
4076
- }
4077
- if (typeof config.api_key === "string" && config.api_key.length > 0) env["ANTHROPIC_API_KEY"] = config.api_key;
4156
+ const env = seed_base_env(config, auth_mode);
4157
+ if (caller_env !== void 0) copy_string_entries(env, caller_env);
4158
+ apply_auth_key(env, config, auth_mode);
4078
4159
  return env;
4079
4160
  }
4080
4161
  /**
@@ -5069,6 +5150,21 @@ function decompose_total_cost(total_cost_usd, usage) {
5069
5150
  return breakdown;
5070
5151
  }
5071
5152
  /**
5153
+ * Split `total_cost_usd` into a per-turn total for each turn, proportional to
5154
+ * its output tokens, falling back to an equal split when every turn reports
5155
+ * zero output (the proportional weights would otherwise all be zero). The last
5156
+ * turn absorbs the floating-point rounding remainder so the shares sum exactly
5157
+ * to `total_cost_usd`. Caller guarantees a non-empty `turns`.
5158
+ */
5159
+ function split_total_across_turns(total_cost_usd, turns) {
5160
+ const total_output = turns.reduce((sum, t) => sum + Math.max(0, t.output_tokens), 0);
5161
+ const per_turn = turns.map((t) => total_output === 0 ? total_cost_usd / turns.length : total_cost_usd * (Math.max(0, t.output_tokens) / total_output));
5162
+ const assigned = per_turn.reduce((a, b) => a + b, 0);
5163
+ const last_index = turns.length - 1;
5164
+ per_turn[last_index] = (per_turn[last_index] ?? 0) + (total_cost_usd - assigned);
5165
+ return per_turn;
5166
+ }
5167
+ /**
5072
5168
  * Split a call's total cost across its turns, proportional to each turn's
5073
5169
  * output tokens, then decompose each turn's share into a `CostBreakdown`.
5074
5170
  *
@@ -5079,29 +5175,8 @@ function decompose_total_cost(total_cost_usd, usage) {
5079
5175
  */
5080
5176
  function allocate_cost_across_turns(total_cost_usd, turns) {
5081
5177
  if (turns.length === 0) return [];
5082
- const total_output = turns.reduce((sum, t) => sum + Math.max(0, t.output_tokens), 0);
5083
- const per_turn_totals = Array.from({ length: turns.length }, () => 0);
5084
- if (total_output === 0) {
5085
- const equal_share = total_cost_usd / turns.length;
5086
- for (let i = 0; i < turns.length; i += 1) per_turn_totals[i] = equal_share;
5087
- } else for (let i = 0; i < turns.length; i += 1) {
5088
- const t = turns[i];
5089
- if (t === void 0) continue;
5090
- per_turn_totals[i] = total_cost_usd * (Math.max(0, t.output_tokens) / total_output);
5091
- }
5092
- const remainder = total_cost_usd - per_turn_totals.reduce((a, b) => a + b, 0);
5093
- if (turns.length > 0) {
5094
- const last_index = turns.length - 1;
5095
- per_turn_totals[last_index] = (per_turn_totals[last_index] ?? 0) + remainder;
5096
- }
5097
- const out = [];
5098
- for (let i = 0; i < turns.length; i += 1) {
5099
- const t = turns[i];
5100
- if (t === void 0) continue;
5101
- const total = per_turn_totals[i] ?? 0;
5102
- out.push(decompose_total_cost(total, t.usage));
5103
- }
5104
- return out;
5178
+ const per_turn = split_total_across_turns(total_cost_usd, turns);
5179
+ return turns.map((t, i) => decompose_total_cost(per_turn[i] ?? 0, t.usage));
5105
5180
  }
5106
5181
  //#endregion
5107
5182
  //#region src/engine/providers/claude_cli/stream_result.ts
@@ -5797,94 +5872,282 @@ async function run_cli(spawn_runtime, args) {
5797
5872
  };
5798
5873
  }
5799
5874
  /**
5800
- * Build a claude_cli `ExternalAgentAdapter` bound to one config.
5801
- *
5802
- * Validates the auth config eagerly, before any call is made, and wires up
5803
- * a spawn runtime plus an in-flight abort-controller registry that
5804
- * `dispose()` uses to cancel every call still running.
5875
+ * Emit `option_ignored` events for the loop-control options this provider
5876
+ * cannot honor: the CLI runs its own tool loop on the far side of the
5877
+ * subprocess boundary.
5805
5878
  */
5806
- function create_claude_cli_adapter(init) {
5807
- const config = init;
5808
- validate_auth_config(config);
5809
- const spawn_runtime = create_spawn_runtime();
5810
- let disposed = false;
5811
- const in_flight = /* @__PURE__ */ new Set();
5812
- const resolved_binary = typeof config.binary === "string" && config.binary.length > 0 ? config.binary : CLI_BINARY_DEFAULT;
5813
- const resolved_auth_mode = config.auth_mode ?? "auto";
5814
- const resolved_startup_timeout = config.startup_timeout_ms ?? 12e4;
5815
- const resolved_stall_timeout = config.stall_timeout_ms ?? 3e5;
5879
+ function emit_ignored_options(opts, trajectory) {
5880
+ const option_ignored = create_option_ignored_dedup(trajectory);
5881
+ if (opts.max_steps !== void 0) option_ignored.emit("max_steps", PROVIDER_NAME);
5882
+ if (opts.tool_error_policy !== void 0) option_ignored.emit("tool_error_policy", PROVIDER_NAME);
5883
+ if (opts.on_tool_approval !== void 0) option_ignored.emit("on_tool_approval", PROVIDER_NAME);
5884
+ if (opts.tool_call_repair_attempts !== void 0) option_ignored.emit("tool_call_repair_attempts", PROVIDER_NAME);
5885
+ if (opts.max_tool_calls_per_step !== void 0) option_ignored.emit("max_tool_calls_per_step", PROVIDER_NAME);
5886
+ }
5887
+ /**
5888
+ * Reject requests the CLI cannot express: multi-turn user history (one user
5889
+ * turn per invocation; continuation goes through `session_id`), and
5890
+ * executable tools under `tool_bridge: 'forbid'`.
5891
+ */
5892
+ function validate_request(prompt, tools_list, tool_bridge) {
5893
+ if (count_user_messages(prompt) >= 2) throw new provider_capability_error(PROVIDER_NAME, "multi_turn_history", "use provider_options.claude_cli.session_id instead");
5894
+ if (tools_list.length > 0 && tool_bridge === "forbid") {
5895
+ if (tools_list.some((t) => typeof t.execute === "function")) throw new provider_capability_error(PROVIDER_NAME, "tool_execute", "tool_bridge is forbid; tools with execute closures cannot run under claude_cli");
5896
+ }
5897
+ }
5898
+ /**
5899
+ * Record which executable tools the allowlist-only bridge is dropping:
5900
+ * their names still pass through `--allowedTools`, but their execute
5901
+ * closures cannot run on this side of the subprocess boundary.
5902
+ */
5903
+ function record_allowlist_only_drop(tools_list, tool_bridge, trajectory) {
5904
+ if (tools_list.length === 0 || tool_bridge !== "allowlist_only" || trajectory === void 0) return;
5905
+ const dropped = tools_list.filter((t) => typeof t.execute === "function").map((t) => t.name);
5906
+ trajectory.record({
5907
+ kind: "cli_tool_bridge_allowlist_only",
5908
+ dropped
5909
+ });
5910
+ }
5911
+ /**
5912
+ * Choose the JSON Schema string for `--json-schema`: a typed `opts.schema`
5913
+ * compiles and wins; otherwise a raw `output_json_schema` string passes
5914
+ * through verbatim.
5915
+ */
5916
+ function resolve_compiled_schema(opts, call_opts) {
5917
+ if (opts.schema !== void 0) return compile_schema(opts.schema);
5918
+ if (typeof call_opts.output_json_schema === "string" && call_opts.output_json_schema.length > 0) return call_opts.output_json_schema;
5919
+ }
5920
+ /**
5921
+ * Wrap `opts.on_chunk` so sync and async callbacks dispatch the same way: a
5922
+ * returned thenable is awaited, any other return is fire-and-done. Returns
5923
+ * `undefined` when the caller isn't streaming, which downstream code uses
5924
+ * as the streaming flag.
5925
+ */
5926
+ function make_dispatch_chunk(opts) {
5927
+ if (opts.on_chunk === void 0) return void 0;
5928
+ return async (chunk) => {
5929
+ const maybe = opts.on_chunk?.(chunk);
5930
+ if (maybe !== void 0 && typeof maybe.then === "function") await maybe;
5931
+ };
5932
+ }
5933
+ /**
5934
+ * Create this call's AbortController, register it in the adapter's
5935
+ * in-flight set (`dispose()` aborts everything still registered), and
5936
+ * forward the caller's abort signal into it, including one already aborted.
5937
+ * `unlink` undoes both and must run however generate settles, or the
5938
+ * registry would pin settled calls alive.
5939
+ */
5940
+ function link_caller_abort(in_flight, caller_abort) {
5941
+ const controller = new AbortController();
5942
+ in_flight.add(controller);
5943
+ const on_caller_abort = () => {
5944
+ controller.abort(caller_abort?.reason);
5945
+ };
5946
+ if (caller_abort !== void 0) if (caller_abort.aborted) controller.abort(caller_abort.reason);
5947
+ else caller_abort.addEventListener("abort", on_caller_abort, { once: true });
5816
5948
  return {
5817
- kind: "external",
5818
- name: PROVIDER_NAME,
5819
- supports: (capability) => SUPPORTED.has(capability),
5820
- async generate(opts, resolved) {
5821
- if (disposed) throw new engine_disposed_error();
5822
- if (opts.abort?.aborted === true) throw new aborted_error("aborted", { reason: opts.abort.reason });
5823
- const trajectory = with_timestamps(opts.trajectory);
5824
- const call_opts = extract_call_opts(opts);
5825
- const option_ignored = create_option_ignored_dedup(trajectory);
5826
- if (opts.max_steps !== void 0) option_ignored.emit("max_steps", PROVIDER_NAME);
5827
- if (opts.tool_error_policy !== void 0) option_ignored.emit("tool_error_policy", PROVIDER_NAME);
5828
- if (opts.on_tool_approval !== void 0) option_ignored.emit("on_tool_approval", PROVIDER_NAME);
5829
- if (opts.tool_call_repair_attempts !== void 0) option_ignored.emit("tool_call_repair_attempts", PROVIDER_NAME);
5830
- if (opts.max_tool_calls_per_step !== void 0) option_ignored.emit("max_tool_calls_per_step", PROVIDER_NAME);
5831
- if (count_user_messages(opts.prompt) >= 2) throw new provider_capability_error(PROVIDER_NAME, "multi_turn_history", "use provider_options.claude_cli.session_id instead");
5832
- const tools_list = opts.tools ?? [];
5833
- const tool_bridge = call_opts.tool_bridge ?? "allowlist_only";
5834
- if (tools_list.length > 0 && tool_bridge === "forbid") {
5835
- if (tools_list.some((t) => typeof t.execute === "function")) throw new provider_capability_error(PROVIDER_NAME, "tool_execute", "tool_bridge is forbid; tools with execute closures cannot run under claude_cli");
5836
- }
5837
- const tool_names = tools_list.map((t) => t.name);
5838
- const merged_allowed_tools = merge_allowed_tools(call_opts.allowed_tools, tool_names);
5839
- if (tools_list.length > 0 && tool_bridge === "allowlist_only" && trajectory !== void 0) {
5840
- const dropped = tools_list.filter((t) => typeof t.execute === "function").map((t) => t.name);
5841
- trajectory.record({
5842
- kind: "cli_tool_bridge_allowlist_only",
5843
- dropped
5844
- });
5845
- }
5846
- let compiled_schema;
5847
- if (opts.schema !== void 0) compiled_schema = compile_schema(opts.schema);
5848
- else if (typeof call_opts.output_json_schema === "string" && call_opts.output_json_schema.length > 0) compiled_schema = call_opts.output_json_schema;
5849
- const system_from_prompt = extract_system_text(opts.prompt);
5850
- const merged_system = merge_system(opts.system ?? system_from_prompt, call_opts.append_system_prompt);
5851
- const stdin_text = extract_prompt_text(opts.prompt);
5852
- const env = build_env(config, call_opts.env, resolved_auth_mode);
5853
- Object.assign(env, effort_env_for_claude_cli(opts.effort));
5854
- const sandbox_plan = build_sandbox_plan(resolved_binary, config.sandbox);
5855
- const cwd = config.default_cwd;
5856
- const dispatch_chunk = opts.on_chunk !== void 0 ? async (chunk) => {
5857
- const maybe = opts.on_chunk?.(chunk);
5858
- if (maybe !== void 0 && typeof maybe.then === "function") await maybe;
5859
- } : void 0;
5860
- const controller = new AbortController();
5861
- in_flight.add(controller);
5862
- const caller_abort = opts.abort;
5863
- const on_caller_abort = () => {
5864
- controller.abort(caller_abort?.reason);
5865
- };
5866
- if (caller_abort !== void 0) if (caller_abort.aborted) controller.abort(caller_abort.reason);
5867
- else caller_abort.addEventListener("abort", on_caller_abort, { once: true });
5868
- const generate_span = start_generate_span(trajectory, {
5869
- model: opts.model ?? resolved.model_id,
5870
- provider: PROVIDER_NAME,
5871
- model_id: resolved.model_id,
5872
- has_tools: tools_list.length > 0,
5873
- has_schema: opts.schema !== void 0,
5874
- streaming: dispatch_chunk !== void 0
5875
- });
5876
- let first_step_span = start_step_span(trajectory, 0);
5949
+ controller,
5950
+ unlink: () => {
5951
+ if (caller_abort !== void 0) caller_abort.removeEventListener("abort", on_caller_abort);
5952
+ in_flight.delete(controller);
5953
+ }
5954
+ };
5955
+ }
5956
+ /**
5957
+ * Open the generate span and the first step span before the CLI spawns, so
5958
+ * their timing brackets the whole subprocess run.
5959
+ */
5960
+ function open_spans(trajectory, opts, resolved, has_tools, streaming) {
5961
+ return {
5962
+ generate: start_generate_span(trajectory, {
5963
+ model: opts.model ?? resolved.model_id,
5964
+ provider: PROVIDER_NAME,
5965
+ model_id: resolved.model_id,
5966
+ has_tools,
5967
+ has_schema: opts.schema !== void 0,
5968
+ streaming
5969
+ }),
5970
+ first_step: start_step_span(trajectory, 0)
5971
+ };
5972
+ }
5973
+ /**
5974
+ * Failure message for schema validation that exhausted its repair budget.
5975
+ * Zero names the opt-out explicitly so "repair disabled" reads differently
5976
+ * from "repairs tried and failed".
5977
+ */
5978
+ function repair_exhausted_message(max_repairs) {
5979
+ if (max_repairs === 0) return "schema validation failed and repair is disabled (schema_repair_attempts: 0)";
5980
+ return `schema validation failed after ${String(max_repairs)} repair attempt${max_repairs === 1 ? "" : "s"}`;
5981
+ }
5982
+ /**
5983
+ * Validate the CLI's final text against `schema`, re-invoking the CLI with
5984
+ * a repair prompt on failure, up to `max_repairs` times.
5985
+ *
5986
+ * Each repair resumes the previous attempt's `session_id`, so a snapshot
5987
+ * without one makes validation failure terminal immediately. The returned
5988
+ * snapshot is the one the repair loop settled on; the result must be built
5989
+ * from it, not from the first attempt's.
5990
+ */
5991
+ async function parse_schema_with_repair(spawn_runtime, base_args, schema, max_repairs, first_parsed, trajectory) {
5992
+ let parsed = first_parsed;
5993
+ let repairs_done = 0;
5994
+ for (;;) {
5995
+ const attempt = await parse_with_schema(schema, parsed.final_text);
5996
+ if (attempt.ok) return {
5997
+ parsed,
5998
+ parsed_content: attempt.value
5999
+ };
6000
+ record_schema_validation_failed(trajectory, {
6001
+ attempt: repairs_done === 0 ? "initial" : "repair",
6002
+ schema_issues: format_schema_issues(attempt.issues),
6003
+ raw_text: parsed.final_text
6004
+ });
6005
+ if (repairs_done >= max_repairs) throw new schema_validation_error(repair_exhausted_message(max_repairs), attempt.issues, parsed.final_text);
6006
+ const repair_session_id = parsed.session_id;
6007
+ if (repair_session_id === void 0) throw new schema_validation_error("schema validation failed and no session_id available for repair", attempt.issues, parsed.final_text);
6008
+ parsed = (await run_cli(spawn_runtime, {
6009
+ ...base_args,
6010
+ stdin_text: build_repair_prompt_text(attempt.issues),
6011
+ call_opts: {
6012
+ ...base_args.call_opts,
6013
+ session_id: repair_session_id
6014
+ }
6015
+ })).parsed;
6016
+ repairs_done += 1;
6017
+ }
6018
+ }
6019
+ /**
6020
+ * Resolve the call's final parse snapshot and validated content. Without a
6021
+ * schema the first snapshot is final and content stays undefined; with one,
6022
+ * the repair loop takes over.
6023
+ */
6024
+ async function resolve_content(spawn_runtime, base_args, opts, first_parsed) {
6025
+ if (opts.schema === void 0) return {
6026
+ parsed: first_parsed,
6027
+ parsed_content: void 0
6028
+ };
6029
+ return parse_schema_with_repair(spawn_runtime, base_args, opts.schema, opts.schema_repair_attempts ?? 1, first_parsed, base_args.trajectory);
6030
+ }
6031
+ /**
6032
+ * Assemble the `build_generate_result` input, attaching the schema and its
6033
+ * validated value only when structured output was requested.
6034
+ */
6035
+ function assemble_result$1(parsed, resolved, schema, parsed_content) {
6036
+ return build_generate_result({
6037
+ parsed,
6038
+ resolved,
6039
+ ...schema !== void 0 ? { schema } : {},
6040
+ ...parsed_content !== void 0 ? { parsed_content } : {}
6041
+ });
6042
+ }
6043
+ /**
6044
+ * Forward the CLI's per-turn provider-reported costs into the trajectory.
6045
+ */
6046
+ function record_provider_costs(trajectory, steps) {
6047
+ for (const step of steps) if (step.cost !== void 0) record_cost(trajectory, step.index, step.cost, "provider_reported");
6048
+ }
6049
+ /**
6050
+ * Close the eagerly-opened first step span against the steps the CLI
6051
+ * reported, then emit paired open/close spans for steps 1..n; those arrive
6052
+ * all at once on process close, so their spans carry usage, not timing.
6053
+ * Clears `spans.first_step` so the error path cannot close it twice.
6054
+ */
6055
+ function close_step_spans(trajectory, spans, steps) {
6056
+ if (steps.length === 0) {
6057
+ end_step_span(trajectory, spans.first_step, {});
6058
+ spans.first_step = void 0;
6059
+ return;
6060
+ }
6061
+ const head = steps[0];
6062
+ if (head !== void 0) end_step_span(trajectory, spans.first_step, {
6063
+ usage: head.usage,
6064
+ finish_reason: head.finish_reason
6065
+ });
6066
+ spans.first_step = void 0;
6067
+ for (let i = 1; i < steps.length; i += 1) {
6068
+ const s = steps[i];
6069
+ if (s === void 0) continue;
6070
+ end_step_span(trajectory, start_step_span(trajectory, i), {
6071
+ usage: s.usage,
6072
+ finish_reason: s.finish_reason
6073
+ });
6074
+ }
6075
+ }
6076
+ /**
6077
+ * Emit the terminal `finish` chunk streaming callers expect; the CLI stream
6078
+ * has no equivalent event, so it is synthesized from the final result.
6079
+ */
6080
+ async function dispatch_finish(dispatch_chunk, result) {
6081
+ if (dispatch_chunk === void 0) return;
6082
+ await dispatch_chunk({
6083
+ kind: "finish",
6084
+ finish_reason: result.finish_reason,
6085
+ usage: result.usage
6086
+ });
6087
+ }
6088
+ /**
6089
+ * Close whatever spans are still open with the failure message. A failure
6090
+ * after the step spans already closed (e.g. in the finish-chunk dispatch)
6091
+ * closes only the generate span.
6092
+ */
6093
+ function close_spans_on_error(trajectory, spans, err) {
6094
+ const message = err instanceof Error ? err.message : String(err);
6095
+ if (spans.first_step !== void 0) {
6096
+ end_step_span(trajectory, spans.first_step, { error: message });
6097
+ spans.first_step = void 0;
6098
+ }
6099
+ end_generate_span(trajectory, spans.generate, { error: message });
6100
+ }
6101
+ /**
6102
+ * Build a claude_cli `ExternalAgentAdapter` bound to one config.
6103
+ *
6104
+ * Validates the auth config eagerly, before any call is made, and wires up
6105
+ * a spawn runtime plus an in-flight abort-controller registry that
6106
+ * `dispose()` uses to cancel every call still running.
6107
+ */
6108
+ function create_claude_cli_adapter(init) {
6109
+ const config = init;
6110
+ validate_auth_config(config);
6111
+ const spawn_runtime = create_spawn_runtime();
6112
+ let disposed = false;
6113
+ const in_flight = /* @__PURE__ */ new Set();
6114
+ const resolved_binary = typeof config.binary === "string" && config.binary.length > 0 ? config.binary : CLI_BINARY_DEFAULT;
6115
+ const resolved_auth_mode = config.auth_mode ?? "auto";
6116
+ const resolved_startup_timeout = config.startup_timeout_ms ?? 12e4;
6117
+ const resolved_stall_timeout = config.stall_timeout_ms ?? 3e5;
6118
+ return {
6119
+ kind: "external",
6120
+ name: PROVIDER_NAME,
6121
+ supports: (capability) => SUPPORTED.has(capability),
6122
+ async generate(opts, resolved) {
6123
+ if (disposed) throw new engine_disposed_error();
6124
+ if (opts.abort?.aborted === true) throw new aborted_error("aborted", { reason: opts.abort.reason });
6125
+ const trajectory = with_timestamps(opts.trajectory);
6126
+ const call_opts = extract_call_opts(opts);
6127
+ emit_ignored_options(opts, trajectory);
6128
+ const tools_list = opts.tools ?? [];
6129
+ const tool_bridge = call_opts.tool_bridge ?? "allowlist_only";
6130
+ validate_request(opts.prompt, tools_list, tool_bridge);
6131
+ const merged_allowed_tools = merge_allowed_tools(call_opts.allowed_tools, tools_list.map((t) => t.name));
6132
+ record_allowlist_only_drop(tools_list, tool_bridge, trajectory);
6133
+ const merged_system = merge_system(opts.system ?? extract_system_text(opts.prompt), call_opts.append_system_prompt);
6134
+ const env = build_env(config, call_opts.env, resolved_auth_mode);
6135
+ Object.assign(env, effort_env_for_claude_cli(opts.effort));
6136
+ const sandbox_plan = build_sandbox_plan(resolved_binary, config.sandbox);
6137
+ const dispatch_chunk = make_dispatch_chunk(opts);
6138
+ const { controller, unlink } = link_caller_abort(in_flight, opts.abort);
6139
+ const spans = open_spans(trajectory, opts, resolved, tools_list.length > 0, dispatch_chunk !== void 0);
5877
6140
  const base_args = {
5878
6141
  model_id: resolved.model_id,
5879
- stdin_text,
6142
+ stdin_text: extract_prompt_text(opts.prompt),
5880
6143
  merged_system,
5881
6144
  merged_allowed_tools,
5882
6145
  call_opts,
5883
- compiled_schema,
6146
+ compiled_schema: resolve_compiled_schema(opts, call_opts),
5884
6147
  env,
5885
6148
  spawn_cmd: sandbox_plan.spawn_cmd,
5886
6149
  prefix_args: sandbox_plan.prefix_args,
5887
- cwd,
6150
+ cwd: config.default_cwd,
5888
6151
  startup_timeout_ms: resolved_startup_timeout,
5889
6152
  stall_timeout_ms: resolved_stall_timeout,
5890
6153
  abort: controller.signal,
@@ -5892,86 +6155,23 @@ function create_claude_cli_adapter(init) {
5892
6155
  trajectory
5893
6156
  };
5894
6157
  try {
5895
- let parsed = (await run_cli(spawn_runtime, base_args)).parsed;
5896
- let parsed_content;
5897
- if (opts.schema !== void 0) {
5898
- const max_repairs = opts.schema_repair_attempts ?? 1;
5899
- let repairs_done = 0;
5900
- for (;;) {
5901
- const attempt = await parse_with_schema(opts.schema, parsed.final_text);
5902
- if (attempt.ok) {
5903
- parsed_content = attempt.value;
5904
- break;
5905
- }
5906
- record_schema_validation_failed(trajectory, {
5907
- attempt: repairs_done === 0 ? "initial" : "repair",
5908
- schema_issues: format_schema_issues(attempt.issues),
5909
- raw_text: parsed.final_text
5910
- });
5911
- if (repairs_done >= max_repairs) throw new schema_validation_error(max_repairs === 0 ? "schema validation failed and repair is disabled (schema_repair_attempts: 0)" : `schema validation failed after ${String(max_repairs)} repair attempt${max_repairs === 1 ? "" : "s"}`, attempt.issues, parsed.final_text);
5912
- const repair_session_id = parsed.session_id;
5913
- if (repair_session_id === void 0) throw new schema_validation_error("schema validation failed and no session_id available for repair", attempt.issues, parsed.final_text);
5914
- const repair_call_opts = {
5915
- ...call_opts,
5916
- session_id: repair_session_id
5917
- };
5918
- parsed = (await run_cli(spawn_runtime, {
5919
- ...base_args,
5920
- stdin_text: build_repair_prompt_text(attempt.issues),
5921
- call_opts: repair_call_opts
5922
- })).parsed;
5923
- repairs_done += 1;
5924
- }
5925
- }
5926
- const result_input = {
5927
- parsed,
5928
- resolved
5929
- };
5930
- if (opts.schema !== void 0) result_input.schema = opts.schema;
5931
- if (parsed_content !== void 0) result_input.parsed_content = parsed_content;
5932
- const result = build_generate_result(result_input);
5933
- for (const step of result.steps) if (step.cost !== void 0) record_cost(trajectory, step.index, step.cost, "provider_reported");
5934
- if (result.steps.length > 0) {
5935
- const head = result.steps[0];
5936
- if (head !== void 0) end_step_span(trajectory, first_step_span, {
5937
- usage: head.usage,
5938
- finish_reason: head.finish_reason
5939
- });
5940
- first_step_span = void 0;
5941
- for (let i = 1; i < result.steps.length; i += 1) {
5942
- const s = result.steps[i];
5943
- if (s === void 0) continue;
5944
- end_step_span(trajectory, start_step_span(trajectory, i), {
5945
- usage: s.usage,
5946
- finish_reason: s.finish_reason
5947
- });
5948
- }
5949
- } else {
5950
- end_step_span(trajectory, first_step_span, {});
5951
- first_step_span = void 0;
5952
- }
5953
- end_generate_span(trajectory, generate_span, {
6158
+ const first_outcome = await run_cli(spawn_runtime, base_args);
6159
+ const { parsed, parsed_content } = await resolve_content(spawn_runtime, base_args, opts, first_outcome.parsed);
6160
+ const result = assemble_result$1(parsed, resolved, opts.schema, parsed_content);
6161
+ record_provider_costs(trajectory, result.steps);
6162
+ close_step_spans(trajectory, spans, result.steps);
6163
+ end_generate_span(trajectory, spans.generate, {
5954
6164
  usage: result.usage,
5955
6165
  finish_reason: result.finish_reason,
5956
6166
  model_resolved: result.model_resolved
5957
6167
  });
5958
- if (dispatch_chunk !== void 0) await dispatch_chunk({
5959
- kind: "finish",
5960
- finish_reason: result.finish_reason,
5961
- usage: result.usage
5962
- });
6168
+ await dispatch_finish(dispatch_chunk, result);
5963
6169
  return result;
5964
6170
  } catch (err) {
5965
- const message = err instanceof Error ? err.message : String(err);
5966
- if (first_step_span !== void 0) {
5967
- end_step_span(trajectory, first_step_span, { error: message });
5968
- first_step_span = void 0;
5969
- }
5970
- end_generate_span(trajectory, generate_span, { error: message });
6171
+ close_spans_on_error(trajectory, spans, err);
5971
6172
  throw err;
5972
6173
  } finally {
5973
- if (caller_abort !== void 0) caller_abort.removeEventListener("abort", on_caller_abort);
5974
- in_flight.delete(controller);
6174
+ unlink();
5975
6175
  }
5976
6176
  },
5977
6177
  async dispose() {
@@ -6170,6 +6370,36 @@ const PARAMETER_RE = /<parameter=([^>\s]+)>([\s\S]*?)<\/parameter>/g;
6170
6370
  const FENCE_RE = /```([^\n]*)\n([\s\S]*?)```/g;
6171
6371
  const EMPTY_BLOCK_RE = /<tool_call>\s*<\/tool_call>/g;
6172
6372
  /**
6373
+ * From `open` at an opening quote, return the index of the matching closing
6374
+ * quote, honoring backslash escapes. Returns `text.length` for an unterminated
6375
+ * string, so the caller's scan runs off the end and yields no object.
6376
+ */
6377
+ function skip_json_string(text, open) {
6378
+ let escaped = false;
6379
+ for (let i = open + 1; i < text.length; i += 1) {
6380
+ const ch = text[i];
6381
+ if (escaped) escaped = false;
6382
+ else if (ch === "\\") escaped = true;
6383
+ else if (ch === "\"") return i;
6384
+ }
6385
+ return text.length;
6386
+ }
6387
+ /**
6388
+ * Parse `text.slice(start, end)` as JSON, pairing the value with `end`, or
6389
+ * `undefined` when the slice is not valid JSON. JSON.parse is the sole
6390
+ * semantic authority; the scanner only fixed the extent.
6391
+ */
6392
+ function parse_json_extent(text, start, end) {
6393
+ try {
6394
+ return {
6395
+ value: JSON.parse(text.slice(start, end)),
6396
+ end
6397
+ };
6398
+ } catch {
6399
+ return;
6400
+ }
6401
+ }
6402
+ /**
6173
6403
  * Find the extent of one balanced JSON object starting at `start_index`
6174
6404
  * (which must point at `{`), respecting strings and escapes so braces inside
6175
6405
  * string values do not affect the depth count. The scanner only finds the
@@ -6178,31 +6408,13 @@ const EMPTY_BLOCK_RE = /<tool_call>\s*<\/tool_call>/g;
6178
6408
  function scan_balanced_json(text, start_index) {
6179
6409
  if (text[start_index] !== "{") return void 0;
6180
6410
  let depth = 0;
6181
- let in_string = false;
6182
- let escaped = false;
6183
6411
  for (let i = start_index; i < text.length; i += 1) {
6184
6412
  const ch = text[i];
6185
- if (in_string) {
6186
- if (escaped) escaped = false;
6187
- else if (ch === "\\") escaped = true;
6188
- else if (ch === "\"") in_string = false;
6189
- continue;
6190
- }
6191
- if (ch === "\"") in_string = true;
6413
+ if (ch === "\"") i = skip_json_string(text, i);
6192
6414
  else if (ch === "{") depth += 1;
6193
6415
  else if (ch === "}") {
6194
6416
  depth -= 1;
6195
- if (depth === 0) {
6196
- const end = i + 1;
6197
- try {
6198
- return {
6199
- value: JSON.parse(text.slice(start_index, end)),
6200
- end
6201
- };
6202
- } catch {
6203
- return;
6204
- }
6205
- }
6417
+ if (depth === 0) return parse_json_extent(text, start_index, i + 1);
6206
6418
  }
6207
6419
  }
6208
6420
  }
@@ -6281,6 +6493,38 @@ function scan_qwen_functions(inner_start, inner) {
6281
6493
  return out;
6282
6494
  }
6283
6495
  /**
6496
+ * Parse a hermes `<tool_call>{...}</tool_call>` payload into a candidate. The
6497
+ * block's whole extent (`span`) is used so a later strip removes the wrapper
6498
+ * too. Returns `undefined` when the inner object is not call-shaped.
6499
+ */
6500
+ function scan_hermes_block(text, inner_start, span) {
6501
+ const brace = text.indexOf("{", inner_start);
6502
+ const scanned = brace === -1 ? void 0 : scan_balanced_json(text, brace);
6503
+ const shaped = scanned === void 0 ? void 0 : call_shape(scanned.value);
6504
+ if (shaped === void 0) return void 0;
6505
+ return {
6506
+ name: shaped.name,
6507
+ args: shaped.args,
6508
+ format: "hermes",
6509
+ span
6510
+ };
6511
+ }
6512
+ /**
6513
+ * Parse one located `<tool_call>` block's inner content into candidates,
6514
+ * dispatching on whether it opens with a qwen `<function=` tag or a hermes
6515
+ * `{` object.
6516
+ */
6517
+ function scan_block_candidates(text, inner_start, close, span) {
6518
+ const inner = text.slice(inner_start, close);
6519
+ const trimmed = inner.trim();
6520
+ if (trimmed.startsWith("<function=")) return scan_qwen_functions(inner_start, inner);
6521
+ if (trimmed.startsWith("{")) {
6522
+ const candidate = scan_hermes_block(text, inner_start, span);
6523
+ return candidate === void 0 ? [] : [candidate];
6524
+ }
6525
+ return [];
6526
+ }
6527
+ /**
6284
6528
  * Scan `<tool_call>` blocks for hermes and qwen_xml candidates.
6285
6529
  *
6286
6530
  * Every block extent (accepted, rejected, or unterminated) is returned as a
@@ -6302,28 +6546,13 @@ function scan_tool_call_blocks(text) {
6302
6546
  break;
6303
6547
  }
6304
6548
  const block_end = close + 12;
6305
- masks.push({
6549
+ const span = {
6306
6550
  start: open,
6307
6551
  end: block_end
6308
- });
6552
+ };
6553
+ masks.push(span);
6309
6554
  const inner_start = open + 11;
6310
- const inner = text.slice(inner_start, close);
6311
- const trimmed = inner.trim();
6312
- if (trimmed.startsWith("<function=")) candidates.push(...scan_qwen_functions(inner_start, inner));
6313
- else if (trimmed.startsWith("{")) {
6314
- const brace = text.indexOf("{", inner_start);
6315
- const scanned = brace === -1 ? void 0 : scan_balanced_json(text, brace);
6316
- const shaped = scanned === void 0 ? void 0 : call_shape(scanned.value);
6317
- if (shaped !== void 0) candidates.push({
6318
- name: shaped.name,
6319
- args: shaped.args,
6320
- format: "hermes",
6321
- span: {
6322
- start: open,
6323
- end: block_end
6324
- }
6325
- });
6326
- }
6555
+ candidates.push(...scan_block_candidates(text, inner_start, close, span));
6327
6556
  pos = block_end;
6328
6557
  }
6329
6558
  return {
@@ -6753,6 +6982,482 @@ async function apply_prepare_step(config, step_index) {
6753
6982
  return replacement;
6754
6983
  }
6755
6984
  /**
6985
+ * Assemble the loop's return value from the accumulated steps and records.
6986
+ */
6987
+ function loop_result(acc, text, finish_reason, max_steps_reached) {
6988
+ return {
6989
+ text,
6990
+ steps: acc.steps,
6991
+ tool_calls: acc.tool_calls,
6992
+ finish_reason,
6993
+ max_steps_reached
6994
+ };
6995
+ }
6996
+ /**
6997
+ * Invoke the transport seam once for one turn, bracketed by request_sent /
6998
+ * response_received events. A thrown invoke (or prepare_step) closes the
6999
+ * step span with the error before rethrowing.
7000
+ */
7001
+ async function invoke_turn(config, step_index, step_span) {
7002
+ try {
7003
+ const request_messages = await apply_prepare_step(config, step_index);
7004
+ config.trajectory?.record({
7005
+ kind: "request_sent",
7006
+ step_index
7007
+ });
7008
+ const turn = await config.invoke_once({
7009
+ step_index,
7010
+ messages: request_messages,
7011
+ tools: config.tools,
7012
+ abort: config.abort,
7013
+ stream: config.stream
7014
+ });
7015
+ config.trajectory?.record({
7016
+ kind: "response_received",
7017
+ step_index,
7018
+ output_tokens: turn.usage.output_tokens,
7019
+ finish_reason: turn.finish_reason
7020
+ });
7021
+ return turn;
7022
+ } catch (err) {
7023
+ const message = err instanceof Error ? err.message : String(err);
7024
+ end_step_span(config.trajectory, step_span, { error: message });
7025
+ throw err;
7026
+ }
7027
+ }
7028
+ /**
7029
+ * Salvage text-embedded tool calls from an eligible turn.
7030
+ *
7031
+ * A turn that "stopped" with plain text may hold a call the runtime failed
7032
+ * to parse into tool_calls; salvage before deciding the step ends the loop.
7033
+ * History gets the stripped text + structured parts (raw markup in history
7034
+ * would teach the model the text format works, and a tool result without a
7035
+ * matching call part is rejected by OpenAI-compatible APIs);
7036
+ * StepRecord.text keeps the raw text for debugging. Returns the native view
7037
+ * unchanged when the turn is ineligible or nothing salvages.
7038
+ */
7039
+ async function salvage_turn(config, turn, tool_map, step_index) {
7040
+ const native = {
7041
+ calls: turn.tool_calls,
7042
+ history_text: turn.text,
7043
+ formats: /* @__PURE__ */ new Map(),
7044
+ outcome: void 0
7045
+ };
7046
+ const budget = config.salvage_budget;
7047
+ if (!(turn.tool_calls.length === 0 && (turn.finish_reason === "stop" || turn.finish_reason === "length") && config.tools.length > 0 && budget !== void 0 && budget.remaining > 0)) return native;
7048
+ const outcome = await salvage_tool_calls(turn.text, tool_map);
7049
+ if (outcome === void 0) return native;
7050
+ budget.remaining -= 1;
7051
+ const formats = /* @__PURE__ */ new Map();
7052
+ const calls = outcome.calls.map((c, n) => {
7053
+ const id = `salvaged_${step_index}_${n}`;
7054
+ formats.set(id, c.format);
7055
+ return {
7056
+ id,
7057
+ name: c.name,
7058
+ input: c.input
7059
+ };
7060
+ });
7061
+ record_tool_call_salvaged(config.trajectory, {
7062
+ step_index,
7063
+ calls: calls.map((c) => ({
7064
+ tool_call_id: c.id,
7065
+ name: c.name,
7066
+ format: formats.get(c.id) ?? "json"
7067
+ })),
7068
+ raw_text: turn.text
7069
+ });
7070
+ await dispatch_salvaged_call_chunks(config, calls, step_index);
7071
+ return {
7072
+ calls,
7073
+ history_text: outcome.stripped_text,
7074
+ formats,
7075
+ outcome
7076
+ };
7077
+ }
7078
+ /**
7079
+ * Mirror the native stream for salvaged calls, which emits start/end for
7080
+ * every call the model attempted, including ones the per-step clamp later
7081
+ * drops.
7082
+ */
7083
+ async function dispatch_salvaged_call_chunks(config, calls, step_index) {
7084
+ if (config.dispatch_chunk === void 0) return;
7085
+ for (const c of calls) {
7086
+ await config.dispatch_chunk({
7087
+ kind: "tool_call_start",
7088
+ id: c.id,
7089
+ name: c.name,
7090
+ step_index
7091
+ });
7092
+ await config.dispatch_chunk({
7093
+ kind: "tool_call_end",
7094
+ id: c.id,
7095
+ input: c.input,
7096
+ step_index
7097
+ });
7098
+ }
7099
+ }
7100
+ /**
7101
+ * Apply `max_tool_calls_per_step` to a turn's calls, native and salvaged
7102
+ * alike. Dropped calls never reach history; the model re-issues them on a
7103
+ * later turn if it still wants them.
7104
+ */
7105
+ function clamp_calls(config, calls, step_index) {
7106
+ const cap = config.max_tool_calls_per_step;
7107
+ if (cap === void 0 || calls.length <= cap) return {
7108
+ kept: calls,
7109
+ dropped: []
7110
+ };
7111
+ const kept = calls.slice(0, cap);
7112
+ const dropped = calls.slice(cap);
7113
+ record_tool_calls_dropped(config.trajectory, {
7114
+ step_index,
7115
+ max_tool_calls_per_step: cap,
7116
+ kept: kept.length,
7117
+ dropped: dropped.map((d) => ({
7118
+ tool_call_id: d.id,
7119
+ name: d.name
7120
+ }))
7121
+ });
7122
+ return {
7123
+ kept,
7124
+ dropped
7125
+ };
7126
+ }
7127
+ /**
7128
+ * Record a call that resolved to an error on the feed-back path: the error
7129
+ * record, the fed-back error tool_result message, the tool_call trajectory
7130
+ * event, and the stream chunk, in that order. Throw-policy paths raise
7131
+ * before reaching here.
7132
+ */
7133
+ async function record_failed_call(ctx, call, message, timing) {
7134
+ const record = {
7135
+ id: call.id,
7136
+ name: call.name,
7137
+ input: call.input,
7138
+ error: { message },
7139
+ duration_ms: timing.duration_ms,
7140
+ started_at: timing.started_at
7141
+ };
7142
+ ctx.records.push(record);
7143
+ ctx.all_records.push(record);
7144
+ ctx.feed.push(build_tool_result_message(call.id, call.name, { error: message }));
7145
+ record_tool_call(ctx.config.trajectory, {
7146
+ step_index: ctx.step_index,
7147
+ name: call.name,
7148
+ tool_call_id: call.id,
7149
+ input: call.input,
7150
+ duration_ms: timing.duration_ms,
7151
+ error: { message }
7152
+ });
7153
+ await dispatch_tool_result_chunk(ctx.config.dispatch_chunk, ctx.step_index, call.id, void 0, { message });
7154
+ }
7155
+ /**
7156
+ * Record a call that never reached execution (the final-turn max_steps skip,
7157
+ * the per-step-cap drop): an error record and a tool_result stream chunk,
7158
+ * but no tool_call trajectory event, no execution, and no fed-back tool
7159
+ * message.
7160
+ */
7161
+ async function record_unexecuted_call(ctx, call, message) {
7162
+ const record = {
7163
+ id: call.id,
7164
+ name: call.name,
7165
+ input: call.input,
7166
+ error: { message },
7167
+ duration_ms: 0,
7168
+ started_at: Date.now()
7169
+ };
7170
+ ctx.records.push(record);
7171
+ ctx.all_records.push(record);
7172
+ await dispatch_tool_result_chunk(ctx.config.dispatch_chunk, ctx.step_index, call.id, void 0, { message });
7173
+ }
7174
+ /**
7175
+ * Look up the named tool and validate the raw input against its schema.
7176
+ * Returns undefined when the failure was recorded on the feed-back path;
7177
+ * throws under 'throw' policy for an unknown tool.
7178
+ */
7179
+ async function resolve_tool_and_input(ctx, raw_call) {
7180
+ const tool = ctx.tool_map.get(raw_call.name);
7181
+ if (tool === void 0) {
7182
+ const err_message = `unknown tool '${raw_call.name}'`;
7183
+ if (ctx.config.tool_error_policy === "throw") {
7184
+ const thrown = new tool_error(err_message, {
7185
+ tool_name: raw_call.name,
7186
+ tool_call_id: raw_call.id,
7187
+ cause: new Error(err_message)
7188
+ });
7189
+ end_step_span(ctx.config.trajectory, ctx.step_span, { error: err_message });
7190
+ throw thrown;
7191
+ }
7192
+ await record_failed_call(ctx, raw_call, err_message, {
7193
+ duration_ms: 0,
7194
+ started_at: Date.now()
7195
+ });
7196
+ return;
7197
+ }
7198
+ const validation = await validate_tool_input(tool, raw_call.input);
7199
+ if (!validation.ok) {
7200
+ await record_failed_call(ctx, raw_call, validation.message, {
7201
+ duration_ms: 0,
7202
+ started_at: Date.now()
7203
+ });
7204
+ return;
7205
+ }
7206
+ return {
7207
+ tool,
7208
+ input: validation.value
7209
+ };
7210
+ }
7211
+ /**
7212
+ * Resolve whether the call may execute (see request_approval). A denial
7213
+ * either throws ('throw' policy) or records the denied call and returns
7214
+ * false; an abort or handler rejection closes the step span and rethrows.
7215
+ */
7216
+ async function approve_call(ctx, tool, input, raw_call) {
7217
+ let approved;
7218
+ try {
7219
+ approved = await request_approval(tool, input, ctx.step_index, raw_call.id, ctx.config.abort, ctx.config.on_tool_approval, ctx.config.trajectory);
7220
+ } catch (err) {
7221
+ const message = err instanceof Error ? err.message : String(err);
7222
+ end_step_span(ctx.config.trajectory, ctx.step_span, { error: message });
7223
+ throw err;
7224
+ }
7225
+ if (approved) return true;
7226
+ const denied_message = "tool_approval_denied";
7227
+ if (ctx.config.tool_error_policy === "throw") {
7228
+ const thrown = new tool_approval_denied_error(`tool '${tool.name}' approval denied`, {
7229
+ tool_name: tool.name,
7230
+ step_index: ctx.step_index,
7231
+ tool_call_id: raw_call.id
7232
+ });
7233
+ end_step_span(ctx.config.trajectory, ctx.step_span, { error: denied_message });
7234
+ throw thrown;
7235
+ }
7236
+ await record_failed_call(ctx, {
7237
+ id: raw_call.id,
7238
+ name: tool.name,
7239
+ input
7240
+ }, denied_message, {
7241
+ duration_ms: 0,
7242
+ started_at: Date.now()
7243
+ });
7244
+ return false;
7245
+ }
7246
+ /**
7247
+ * Execute an approved call and record the outcome: a success record with
7248
+ * output (flipping terminal_fired for an ends_turn tool), or the error path
7249
+ * via record_execute_failure.
7250
+ */
7251
+ async function execute_approved_call(ctx, tool, input, raw_call) {
7252
+ const started_at = Date.now();
7253
+ const tool_ctx = {
7254
+ abort: ctx.config.abort,
7255
+ tool_call_id: raw_call.id,
7256
+ step_index: ctx.step_index,
7257
+ ...ctx.config.trajectory !== void 0 ? { trajectory: ctx.config.trajectory } : {}
7258
+ };
7259
+ let output;
7260
+ let err_message;
7261
+ let thrown;
7262
+ try {
7263
+ const execute = tool.execute;
7264
+ const maybe = execute(input, tool_ctx);
7265
+ output = maybe instanceof Promise ? await maybe : maybe;
7266
+ } catch (err) {
7267
+ thrown = err;
7268
+ err_message = serialize_error(err);
7269
+ }
7270
+ const duration_ms = Date.now() - started_at;
7271
+ if (thrown !== void 0) {
7272
+ await record_execute_failure(ctx, tool, raw_call, input, {
7273
+ thrown,
7274
+ err_message,
7275
+ duration_ms,
7276
+ started_at
7277
+ });
7278
+ return;
7279
+ }
7280
+ const record = {
7281
+ id: raw_call.id,
7282
+ name: tool.name,
7283
+ input,
7284
+ output,
7285
+ duration_ms,
7286
+ started_at
7287
+ };
7288
+ ctx.records.push(record);
7289
+ ctx.all_records.push(record);
7290
+ if (tool.ends_turn === true) ctx.terminal_fired = true;
7291
+ ctx.feed.push(build_tool_result_message(raw_call.id, tool.name, output ?? ""));
7292
+ record_tool_call(ctx.config.trajectory, {
7293
+ step_index: ctx.step_index,
7294
+ name: tool.name,
7295
+ tool_call_id: raw_call.id,
7296
+ input,
7297
+ duration_ms
7298
+ });
7299
+ await dispatch_tool_result_chunk(ctx.config.dispatch_chunk, ctx.step_index, raw_call.id, output);
7300
+ }
7301
+ /**
7302
+ * Handle a throwing execute: an abort in flight or 'throw' policy closes
7303
+ * the step span and throws; feed-back policy records the failure with the
7304
+ * measured timing.
7305
+ */
7306
+ async function record_execute_failure(ctx, tool, raw_call, input, failure) {
7307
+ if (ctx.config.abort.aborted) {
7308
+ const abort_err = new aborted_error("aborted", {
7309
+ reason: ctx.config.abort.reason,
7310
+ step_index: ctx.step_index,
7311
+ tool_call_in_flight: {
7312
+ id: raw_call.id,
7313
+ name: tool.name
7314
+ }
7315
+ });
7316
+ end_step_span(ctx.config.trajectory, ctx.step_span, { error: "aborted" });
7317
+ throw abort_err;
7318
+ }
7319
+ if (ctx.config.tool_error_policy === "throw") {
7320
+ const wrapped = new tool_error(`tool '${tool.name}' failed: ${failure.err_message ?? "unknown"}`, {
7321
+ tool_name: tool.name,
7322
+ tool_call_id: raw_call.id,
7323
+ cause: failure.thrown
7324
+ });
7325
+ end_step_span(ctx.config.trajectory, ctx.step_span, { error: failure.err_message ?? "tool error" });
7326
+ throw wrapped;
7327
+ }
7328
+ await record_failed_call(ctx, {
7329
+ id: raw_call.id,
7330
+ name: tool.name,
7331
+ input
7332
+ }, failure.err_message ?? "unknown", {
7333
+ duration_ms: failure.duration_ms,
7334
+ started_at: failure.started_at
7335
+ });
7336
+ }
7337
+ /**
7338
+ * Run one raw tool call through the guard chain — final-turn skip, tool
7339
+ * lookup, input validation, approval — then execute it. Each guard either
7340
+ * records the failure (feed-back policy) and stops, or throws (throw
7341
+ * policy / abort).
7342
+ */
7343
+ async function process_call(ctx, raw_call) {
7344
+ throw_if_aborted_in_flight(ctx.config.abort, ctx.step_index, {
7345
+ id: raw_call.id,
7346
+ name: raw_call.name
7347
+ });
7348
+ if (ctx.would_exceed_after && ctx.tool_map.get(raw_call.name)?.ends_turn !== true) {
7349
+ await record_unexecuted_call(ctx, raw_call, "max_steps_exceeded_before_execution");
7350
+ return;
7351
+ }
7352
+ const resolved = await resolve_tool_and_input(ctx, raw_call);
7353
+ if (resolved === void 0) return;
7354
+ if (!await approve_call(ctx, resolved.tool, resolved.input, raw_call)) return;
7355
+ throw_if_aborted_in_flight(ctx.config.abort, ctx.step_index, {
7356
+ id: raw_call.id,
7357
+ name: resolved.tool.name
7358
+ });
7359
+ await execute_approved_call(ctx, resolved.tool, resolved.input, raw_call);
7360
+ }
7361
+ /**
7362
+ * Stamp salvage provenance onto the records of calls that came from text
7363
+ * salvage rather than structured output.
7364
+ */
7365
+ function mark_salvaged(records, formats) {
7366
+ for (const r of records) {
7367
+ const format = formats.get(r.id);
7368
+ if (format !== void 0) {
7369
+ r.salvaged = true;
7370
+ r.salvaged_format = format;
7371
+ }
7372
+ }
7373
+ }
7374
+ /**
7375
+ * Emit a tool_result for every resolved call in this step (success carries
7376
+ * output, feed-back failures carry error). Throw-policy and aborted calls
7377
+ * exit before here and surface loudly as a thrown error instead.
7378
+ */
7379
+ function emit_tool_results(config, step_index, records) {
7380
+ for (const r of records) record_tool_result(config.trajectory, {
7381
+ step_index,
7382
+ name: r.name,
7383
+ tool_call_id: r.id,
7384
+ duration_ms: r.duration_ms,
7385
+ ...r.error !== void 0 ? { error: r.error } : { output: r.output }
7386
+ });
7387
+ }
7388
+ /**
7389
+ * The finish reason a tool-call step reports. A salvaged step reports
7390
+ * 'tool_calls': downstream consumers see the same shape a native tool turn
7391
+ * produces; the salvaged flags carry provenance. A terminal step reports
7392
+ * 'tool_calls' (it genuinely made calls); the loop-level finish_reason
7393
+ * 'stop' is the separate signal that generation ended.
7394
+ */
7395
+ function resolve_turn_finish_reason(ctx, salvaged, turn_finish) {
7396
+ if (ctx.terminal_fired) return "tool_calls";
7397
+ if (ctx.would_exceed_after) return "max_steps";
7398
+ if (salvaged) return "tool_calls";
7399
+ return turn_finish;
7400
+ }
7401
+ /**
7402
+ * Build one StepRecord — cost derived and recorded, provider_reported
7403
+ * passed through — push it, notify on_finish_step, and close the step span.
7404
+ */
7405
+ function push_step_record(config, acc, step_index, turn, tool_calls, finish_reason, step_span) {
7406
+ const record = {
7407
+ index: step_index,
7408
+ text: turn.text,
7409
+ tool_calls,
7410
+ usage: turn.usage,
7411
+ finish_reason
7412
+ };
7413
+ const breakdown = compute_and_record_cost(config, step_index, turn.usage);
7414
+ if (breakdown !== void 0) record.cost = breakdown;
7415
+ if (turn.provider_reported !== void 0) record.provider_reported = turn.provider_reported;
7416
+ acc.steps.push(record);
7417
+ if (config.on_finish_step !== void 0) config.on_finish_step(record);
7418
+ end_step_span(config.trajectory, step_span, {
7419
+ usage: turn.usage,
7420
+ finish_reason
7421
+ });
7422
+ }
7423
+ /**
7424
+ * Run one loop iteration: invoke the turn, salvage and clamp its calls,
7425
+ * execute them sequentially, and record the step. Returns the finished
7426
+ * ToolLoopResult when this step ends the loop (no calls, terminal tool,
7427
+ * max_steps), undefined when the loop should continue.
7428
+ */
7429
+ async function run_loop_step(config, tool_map, acc, step_index) {
7430
+ const step_span = start_step_span(config.trajectory, step_index);
7431
+ const turn = await invoke_turn(config, step_index, step_span);
7432
+ const view = await salvage_turn(config, turn, tool_map, step_index);
7433
+ const { kept, dropped } = clamp_calls(config, view.calls, step_index);
7434
+ config.messages.push(build_assistant_message(view.history_text, kept));
7435
+ if (kept.length === 0) {
7436
+ push_step_record(config, acc, step_index, turn, [], turn.finish_reason, step_span);
7437
+ return loop_result(acc, turn.text, turn.finish_reason, false);
7438
+ }
7439
+ const ctx = {
7440
+ config,
7441
+ tool_map,
7442
+ step_index,
7443
+ step_span,
7444
+ would_exceed_after: step_index + 1 >= config.max_steps,
7445
+ records: [],
7446
+ all_records: acc.tool_calls,
7447
+ feed: [],
7448
+ terminal_fired: false
7449
+ };
7450
+ for (const raw_call of kept) await process_call(ctx, raw_call);
7451
+ for (const d of dropped) await record_unexecuted_call(ctx, d, "dropped_max_tool_calls_per_step");
7452
+ mark_salvaged(ctx.records, view.formats);
7453
+ emit_tool_results(config, step_index, ctx.records);
7454
+ for (const m of ctx.feed) config.messages.push(m);
7455
+ const turn_finish_reason = resolve_turn_finish_reason(ctx, view.outcome !== void 0, turn.finish_reason);
7456
+ push_step_record(config, acc, step_index, turn, ctx.records, turn_finish_reason, step_span);
7457
+ if (ctx.terminal_fired) return loop_result(acc, turn.text, "stop", false);
7458
+ if (ctx.would_exceed_after) return loop_result(acc, turn.text, "max_steps", true);
7459
+ }
7460
+ /**
6756
7461
  * Run the model-turn / tool-execution loop to completion.
6757
7462
  *
6758
7463
  * Each iteration invokes the transport seam once, salvages text-embedded
@@ -6761,403 +7466,20 @@ async function apply_prepare_step(config, step_index) {
6761
7466
  * until the model stops, a terminal tool fires, or `max_steps` is reached.
6762
7467
  */
6763
7468
  async function run_tool_loop(config) {
6764
- const steps = [];
6765
- const tool_calls_all = [];
6766
- let text = "";
6767
- let finish_reason = "stop";
6768
- let step_index = config.step_index_start;
6769
- let max_steps_reached = false;
7469
+ const acc = {
7470
+ steps: [],
7471
+ tool_calls: []
7472
+ };
6770
7473
  const tool_map = /* @__PURE__ */ new Map();
6771
7474
  for (const t of config.tools) tool_map.set(t.name, t);
7475
+ let step_index = config.step_index_start;
6772
7476
  while (true) {
6773
7477
  throw_if_aborted(config.abort, step_index);
6774
- if (step_index >= config.max_steps) {
6775
- max_steps_reached = true;
6776
- finish_reason = "max_steps";
6777
- break;
6778
- }
6779
- const step_span = start_step_span(config.trajectory, step_index);
6780
- let turn;
6781
- try {
6782
- const request_messages = await apply_prepare_step(config, step_index);
6783
- config.trajectory?.record({
6784
- kind: "request_sent",
6785
- step_index
6786
- });
6787
- turn = await config.invoke_once({
6788
- step_index,
6789
- messages: request_messages,
6790
- tools: config.tools,
6791
- abort: config.abort,
6792
- stream: config.stream
6793
- });
6794
- config.trajectory?.record({
6795
- kind: "response_received",
6796
- step_index,
6797
- output_tokens: turn.usage.output_tokens,
6798
- finish_reason: turn.finish_reason
6799
- });
6800
- } catch (err) {
6801
- const message = err instanceof Error ? err.message : String(err);
6802
- end_step_span(config.trajectory, step_span, { error: message });
6803
- throw err;
6804
- }
6805
- let effective_calls = turn.tool_calls;
6806
- let history_text = turn.text;
6807
- let salvage;
6808
- const salvaged_formats = /* @__PURE__ */ new Map();
6809
- if (turn.tool_calls.length === 0 && (turn.finish_reason === "stop" || turn.finish_reason === "length") && config.tools.length > 0 && config.salvage_budget !== void 0 && config.salvage_budget.remaining > 0) {
6810
- salvage = await salvage_tool_calls(turn.text, tool_map);
6811
- if (salvage !== void 0) {
6812
- config.salvage_budget.remaining -= 1;
6813
- history_text = salvage.stripped_text;
6814
- effective_calls = salvage.calls.map((c, n) => {
6815
- const id = `salvaged_${step_index}_${n}`;
6816
- salvaged_formats.set(id, c.format);
6817
- return {
6818
- id,
6819
- name: c.name,
6820
- input: c.input
6821
- };
6822
- });
6823
- record_tool_call_salvaged(config.trajectory, {
6824
- step_index,
6825
- calls: effective_calls.map((c) => ({
6826
- tool_call_id: c.id,
6827
- name: c.name,
6828
- format: salvaged_formats.get(c.id) ?? "json"
6829
- })),
6830
- raw_text: turn.text
6831
- });
6832
- if (config.dispatch_chunk !== void 0) for (const c of effective_calls) {
6833
- await config.dispatch_chunk({
6834
- kind: "tool_call_start",
6835
- id: c.id,
6836
- name: c.name,
6837
- step_index
6838
- });
6839
- await config.dispatch_chunk({
6840
- kind: "tool_call_end",
6841
- id: c.id,
6842
- input: c.input,
6843
- step_index
6844
- });
6845
- }
6846
- }
6847
- }
6848
- let dropped_calls = [];
6849
- const per_step_cap = config.max_tool_calls_per_step;
6850
- if (per_step_cap !== void 0 && effective_calls.length > per_step_cap) {
6851
- dropped_calls = effective_calls.slice(per_step_cap);
6852
- effective_calls = effective_calls.slice(0, per_step_cap);
6853
- record_tool_calls_dropped(config.trajectory, {
6854
- step_index,
6855
- max_tool_calls_per_step: per_step_cap,
6856
- kept: effective_calls.length,
6857
- dropped: dropped_calls.map((d) => ({
6858
- tool_call_id: d.id,
6859
- name: d.name
6860
- }))
6861
- });
6862
- }
6863
- const step_tool_records = [];
6864
- const assistant_message = build_assistant_message(history_text, effective_calls);
6865
- config.messages.push(assistant_message);
6866
- if (effective_calls.length === 0) {
6867
- text = turn.text;
6868
- finish_reason = turn.finish_reason;
6869
- const record_step = {
6870
- index: step_index,
6871
- text: turn.text,
6872
- tool_calls: [],
6873
- usage: turn.usage,
6874
- finish_reason: turn.finish_reason
6875
- };
6876
- const breakdown = compute_and_record_cost(config, step_index, turn.usage);
6877
- if (breakdown !== void 0) record_step.cost = breakdown;
6878
- if (turn.provider_reported !== void 0) record_step.provider_reported = turn.provider_reported;
6879
- steps.push(record_step);
6880
- if (config.on_finish_step !== void 0) config.on_finish_step(record_step);
6881
- end_step_span(config.trajectory, step_span, {
6882
- usage: turn.usage,
6883
- finish_reason: turn.finish_reason
6884
- });
6885
- break;
6886
- }
6887
- const would_exceed_after = step_index + 1 >= config.max_steps;
6888
- const tool_results_to_feed = [];
6889
- let terminal_fired = false;
6890
- for (const raw_call of effective_calls) {
6891
- throw_if_aborted_in_flight(config.abort, step_index, {
6892
- id: raw_call.id,
6893
- name: raw_call.name
6894
- });
6895
- if (would_exceed_after && tool_map.get(raw_call.name)?.ends_turn !== true) {
6896
- const record = {
6897
- id: raw_call.id,
6898
- name: raw_call.name,
6899
- input: raw_call.input,
6900
- error: { message: "max_steps_exceeded_before_execution" },
6901
- duration_ms: 0,
6902
- started_at: Date.now()
6903
- };
6904
- step_tool_records.push(record);
6905
- tool_calls_all.push(record);
6906
- await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, void 0, { message: "max_steps_exceeded_before_execution" });
6907
- continue;
6908
- }
6909
- const tool = tool_map.get(raw_call.name);
6910
- if (tool === void 0) {
6911
- const err_message = `unknown tool '${raw_call.name}'`;
6912
- if (config.tool_error_policy === "throw") {
6913
- const thrown = new tool_error(err_message, {
6914
- tool_name: raw_call.name,
6915
- tool_call_id: raw_call.id,
6916
- cause: new Error(err_message)
6917
- });
6918
- end_step_span(config.trajectory, step_span, { error: err_message });
6919
- throw thrown;
6920
- }
6921
- const record = {
6922
- id: raw_call.id,
6923
- name: raw_call.name,
6924
- input: raw_call.input,
6925
- error: { message: err_message },
6926
- duration_ms: 0,
6927
- started_at: Date.now()
6928
- };
6929
- step_tool_records.push(record);
6930
- tool_calls_all.push(record);
6931
- tool_results_to_feed.push(build_tool_result_message(raw_call.id, raw_call.name, { error: err_message }));
6932
- record_tool_call(config.trajectory, {
6933
- step_index,
6934
- name: raw_call.name,
6935
- tool_call_id: raw_call.id,
6936
- input: raw_call.input,
6937
- duration_ms: 0,
6938
- error: { message: err_message }
6939
- });
6940
- await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, void 0, { message: err_message });
6941
- continue;
6942
- }
6943
- const validation = await validate_tool_input(tool, raw_call.input);
6944
- if (!validation.ok) {
6945
- const record = {
6946
- id: raw_call.id,
6947
- name: raw_call.name,
6948
- input: raw_call.input,
6949
- error: { message: validation.message },
6950
- duration_ms: 0,
6951
- started_at: Date.now()
6952
- };
6953
- step_tool_records.push(record);
6954
- tool_calls_all.push(record);
6955
- tool_results_to_feed.push(build_tool_result_message(raw_call.id, raw_call.name, { error: validation.message }));
6956
- record_tool_call(config.trajectory, {
6957
- step_index,
6958
- name: raw_call.name,
6959
- tool_call_id: raw_call.id,
6960
- input: raw_call.input,
6961
- duration_ms: 0,
6962
- error: { message: validation.message }
6963
- });
6964
- await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, void 0, { message: validation.message });
6965
- continue;
6966
- }
6967
- let approved;
6968
- try {
6969
- approved = await request_approval(tool, validation.value, step_index, raw_call.id, config.abort, config.on_tool_approval, config.trajectory);
6970
- } catch (err) {
6971
- const message = err instanceof Error ? err.message : String(err);
6972
- end_step_span(config.trajectory, step_span, { error: message });
6973
- throw err;
6974
- }
6975
- if (!approved) {
6976
- const denied_message = "tool_approval_denied";
6977
- if (config.tool_error_policy === "throw") {
6978
- const thrown = new tool_approval_denied_error(`tool '${tool.name}' approval denied`, {
6979
- tool_name: tool.name,
6980
- step_index,
6981
- tool_call_id: raw_call.id
6982
- });
6983
- end_step_span(config.trajectory, step_span, { error: denied_message });
6984
- throw thrown;
6985
- }
6986
- const record = {
6987
- id: raw_call.id,
6988
- name: tool.name,
6989
- input: validation.value,
6990
- error: { message: denied_message },
6991
- duration_ms: 0,
6992
- started_at: Date.now()
6993
- };
6994
- step_tool_records.push(record);
6995
- tool_calls_all.push(record);
6996
- tool_results_to_feed.push(build_tool_result_message(raw_call.id, tool.name, { error: denied_message }));
6997
- record_tool_call(config.trajectory, {
6998
- step_index,
6999
- name: tool.name,
7000
- tool_call_id: raw_call.id,
7001
- input: validation.value,
7002
- duration_ms: 0,
7003
- error: { message: denied_message }
7004
- });
7005
- await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, void 0, { message: denied_message });
7006
- continue;
7007
- }
7008
- throw_if_aborted_in_flight(config.abort, step_index, {
7009
- id: raw_call.id,
7010
- name: tool.name
7011
- });
7012
- const started_at = Date.now();
7013
- const tool_ctx = {
7014
- abort: config.abort,
7015
- tool_call_id: raw_call.id,
7016
- step_index,
7017
- ...config.trajectory !== void 0 ? { trajectory: config.trajectory } : {}
7018
- };
7019
- let output;
7020
- let err_message;
7021
- let thrown;
7022
- try {
7023
- const execute = tool.execute;
7024
- const maybe = execute(validation.value, tool_ctx);
7025
- output = maybe instanceof Promise ? await maybe : maybe;
7026
- } catch (err) {
7027
- thrown = err;
7028
- err_message = serialize_error(err);
7029
- }
7030
- const duration_ms = Date.now() - started_at;
7031
- if (thrown !== void 0) {
7032
- if (config.abort.aborted) {
7033
- const abort_err = new aborted_error("aborted", {
7034
- reason: config.abort.reason,
7035
- step_index,
7036
- tool_call_in_flight: {
7037
- id: raw_call.id,
7038
- name: tool.name
7039
- }
7040
- });
7041
- end_step_span(config.trajectory, step_span, { error: "aborted" });
7042
- throw abort_err;
7043
- }
7044
- if (config.tool_error_policy === "throw") {
7045
- const wrapped = new tool_error(`tool '${tool.name}' failed: ${err_message ?? "unknown"}`, {
7046
- tool_name: tool.name,
7047
- tool_call_id: raw_call.id,
7048
- cause: thrown
7049
- });
7050
- end_step_span(config.trajectory, step_span, { error: err_message ?? "tool error" });
7051
- throw wrapped;
7052
- }
7053
- const record = {
7054
- id: raw_call.id,
7055
- name: tool.name,
7056
- input: validation.value,
7057
- error: { message: err_message ?? "unknown" },
7058
- duration_ms,
7059
- started_at
7060
- };
7061
- step_tool_records.push(record);
7062
- tool_calls_all.push(record);
7063
- tool_results_to_feed.push(build_tool_result_message(raw_call.id, tool.name, { error: err_message ?? "unknown" }));
7064
- record_tool_call(config.trajectory, {
7065
- step_index,
7066
- name: tool.name,
7067
- tool_call_id: raw_call.id,
7068
- input: validation.value,
7069
- duration_ms,
7070
- error: { message: err_message ?? "unknown" }
7071
- });
7072
- await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, void 0, { message: err_message ?? "unknown" });
7073
- continue;
7074
- }
7075
- const record = {
7076
- id: raw_call.id,
7077
- name: tool.name,
7078
- input: validation.value,
7079
- output,
7080
- duration_ms,
7081
- started_at
7082
- };
7083
- step_tool_records.push(record);
7084
- tool_calls_all.push(record);
7085
- if (tool.ends_turn === true) terminal_fired = true;
7086
- tool_results_to_feed.push(build_tool_result_message(raw_call.id, tool.name, output ?? ""));
7087
- record_tool_call(config.trajectory, {
7088
- step_index,
7089
- name: tool.name,
7090
- tool_call_id: raw_call.id,
7091
- input: validation.value,
7092
- duration_ms
7093
- });
7094
- await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, output);
7095
- }
7096
- for (const d of dropped_calls) {
7097
- const record = {
7098
- id: d.id,
7099
- name: d.name,
7100
- input: d.input,
7101
- error: { message: "dropped_max_tool_calls_per_step" },
7102
- duration_ms: 0,
7103
- started_at: Date.now()
7104
- };
7105
- step_tool_records.push(record);
7106
- tool_calls_all.push(record);
7107
- await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, d.id, void 0, { message: "dropped_max_tool_calls_per_step" });
7108
- }
7109
- for (const r of step_tool_records) {
7110
- const format = salvaged_formats.get(r.id);
7111
- if (format !== void 0) {
7112
- r.salvaged = true;
7113
- r.salvaged_format = format;
7114
- }
7115
- }
7116
- for (const r of step_tool_records) record_tool_result(config.trajectory, {
7117
- step_index,
7118
- name: r.name,
7119
- tool_call_id: r.id,
7120
- duration_ms: r.duration_ms,
7121
- ...r.error !== void 0 ? { error: r.error } : { output: r.output }
7122
- });
7123
- for (const m of tool_results_to_feed) config.messages.push(m);
7124
- const turn_finish_reason = terminal_fired ? "tool_calls" : would_exceed_after ? "max_steps" : salvage !== void 0 ? "tool_calls" : turn.finish_reason;
7125
- const step_record = {
7126
- index: step_index,
7127
- text: turn.text,
7128
- tool_calls: step_tool_records,
7129
- usage: turn.usage,
7130
- finish_reason: turn_finish_reason
7131
- };
7132
- const breakdown = compute_and_record_cost(config, step_index, turn.usage);
7133
- if (breakdown !== void 0) step_record.cost = breakdown;
7134
- if (turn.provider_reported !== void 0) step_record.provider_reported = turn.provider_reported;
7135
- steps.push(step_record);
7136
- if (config.on_finish_step !== void 0) config.on_finish_step(step_record);
7137
- end_step_span(config.trajectory, step_span, {
7138
- usage: turn.usage,
7139
- finish_reason: turn_finish_reason
7140
- });
7141
- if (terminal_fired) {
7142
- finish_reason = "stop";
7143
- text = turn.text;
7144
- break;
7145
- }
7146
- if (would_exceed_after) {
7147
- max_steps_reached = true;
7148
- finish_reason = "max_steps";
7149
- text = turn.text;
7150
- break;
7151
- }
7478
+ if (step_index >= config.max_steps) return loop_result(acc, "", "max_steps", true);
7479
+ const finished = await run_loop_step(config, tool_map, acc, step_index);
7480
+ if (finished !== void 0) return finished;
7152
7481
  step_index += 1;
7153
7482
  }
7154
- return {
7155
- text,
7156
- steps,
7157
- tool_calls: tool_calls_all,
7158
- finish_reason,
7159
- max_steps_reached
7160
- };
7161
7483
  }
7162
7484
  //#endregion
7163
7485
  //#region src/engine/generate.ts
@@ -7195,20 +7517,76 @@ function build_initial_messages(opts) {
7195
7517
  * empty (provider APIs reject an empty messages array).
7196
7518
  */
7197
7519
  function split_leading_system_messages(messages) {
7198
- let run_end = 0;
7199
- const system_parts = [];
7200
- while (run_end < messages.length) {
7201
- const m = messages[run_end];
7202
- if (m?.role !== "system") break;
7203
- system_parts.push(m.content);
7204
- run_end += 1;
7205
- }
7206
- const rest = messages.slice(run_end);
7207
- if (system_parts.length === 0 || rest.length === 0) return { messages: [...messages] };
7208
- return {
7209
- system: system_parts.join("\n\n"),
7210
- messages: rest
7211
- };
7520
+ return split_leading_system_run(messages, (m) => m.content);
7521
+ }
7522
+ const RETRY_CLASSIFIED_KINDS = /* @__PURE__ */ new Set([
7523
+ "rate_limit",
7524
+ "provider_5xx",
7525
+ "network",
7526
+ "timeout"
7527
+ ]);
7528
+ const NETWORK_ERROR_CODES = /* @__PURE__ */ new Set([
7529
+ "ECONNRESET",
7530
+ "ETIMEDOUT",
7531
+ "ENOTFOUND",
7532
+ "ECONNREFUSED"
7533
+ ]);
7534
+ /**
7535
+ * True when `err` already carries a `kind` this layer treats as retryable, so
7536
+ * the classifier passes it through untouched.
7537
+ */
7538
+ function has_retry_kind(err) {
7539
+ const kind = Reflect.get(err, "kind");
7540
+ return typeof kind === "string" && RETRY_CLASSIFIED_KINDS.has(kind);
7541
+ }
7542
+ /**
7543
+ * Read the HTTP status off either `statusCode` or `status`, in that order.
7544
+ */
7545
+ function resolve_error_status(err) {
7546
+ const status = Reflect.get(err, "statusCode");
7547
+ if (typeof status === "number") return status;
7548
+ const status_alt = Reflect.get(err, "status");
7549
+ return typeof status_alt === "number" ? status_alt : void 0;
7550
+ }
7551
+ /**
7552
+ * Copy the error's `message` onto a classification record when it is a string,
7553
+ * returning the same record for chaining.
7554
+ */
7555
+ function with_error_message(err, out) {
7556
+ const message = Reflect.get(err, "message");
7557
+ if (typeof message === "string") out["message"] = message;
7558
+ return out;
7559
+ }
7560
+ /**
7561
+ * Parse a `Retry-After` value off the error's `responseHeaders`, if present.
7562
+ */
7563
+ function retry_after_from_error(err) {
7564
+ const headers = Reflect.get(err, "responseHeaders");
7565
+ if (headers === null || typeof headers !== "object") return void 0;
7566
+ const hv = Reflect.get(headers, "retry-after");
7567
+ return typeof hv === "string" ? parse_retry_after(hv) : void 0;
7568
+ }
7569
+ /**
7570
+ * Build the `rate_limit` classification for a 429, attaching the message and a
7571
+ * parsed `Retry-After` when either is present.
7572
+ */
7573
+ function classify_rate_limit(err) {
7574
+ const out = with_error_message(err, {
7575
+ kind: "rate_limit",
7576
+ status: 429
7577
+ });
7578
+ const retry_after_ms = retry_after_from_error(err);
7579
+ if (retry_after_ms !== void 0) out["retry_after_ms"] = retry_after_ms;
7580
+ return out;
7581
+ }
7582
+ /**
7583
+ * Build the `network` classification when `err.code` is a known network error
7584
+ * code, or `undefined` when it is not.
7585
+ */
7586
+ function classify_network_error(err) {
7587
+ const code = Reflect.get(err, "code");
7588
+ if (typeof code !== "string" || !NETWORK_ERROR_CODES.has(code)) return void 0;
7589
+ return with_error_message(err, { kind: "network" });
7212
7590
  }
7213
7591
  /**
7214
7592
  * Classify a thrown provider error into the shape `retry_with_policy`
@@ -7218,49 +7596,19 @@ function split_leading_system_messages(messages) {
7218
7596
  * Otherwise inspects `statusCode`/`status`: 429 becomes `rate_limit`
7219
7597
  * (reading `Retry-After` off `responseHeaders` when present), 5xx becomes
7220
7598
  * `provider_5xx`, and a known `ECONNRESET`/`ETIMEDOUT`/`ENOTFOUND`/
7221
- * `ECONNREFUSED` error code becomes `network`. Anything else passes through
7222
- * unclassified, which `retry_with_policy` treats as non-retryable.
7223
- */
7224
- function classify_provider_error(err) {
7225
- if (err === null || typeof err !== "object") return err;
7226
- const existing_kind = Reflect.get(err, "kind");
7227
- if (typeof existing_kind === "string" && (existing_kind === "rate_limit" || existing_kind === "provider_5xx" || existing_kind === "network" || existing_kind === "timeout")) return err;
7228
- const status = Reflect.get(err, "statusCode");
7229
- const status_alt = Reflect.get(err, "status");
7230
- const resolved_status = typeof status === "number" ? status : typeof status_alt === "number" ? status_alt : void 0;
7231
- if (resolved_status === 429) {
7232
- const headers = Reflect.get(err, "responseHeaders");
7233
- let retry_after_ms;
7234
- if (headers !== null && typeof headers === "object") {
7235
- const hv = Reflect.get(headers, "retry-after");
7236
- if (typeof hv === "string") retry_after_ms = parse_retry_after(hv);
7237
- }
7238
- const message = Reflect.get(err, "message");
7239
- const out = {
7240
- kind: "rate_limit",
7241
- status: 429
7242
- };
7243
- if (typeof message === "string") out["message"] = message;
7244
- if (retry_after_ms !== void 0) out["retry_after_ms"] = retry_after_ms;
7245
- return out;
7246
- }
7247
- if (resolved_status !== void 0 && resolved_status >= 500 && resolved_status < 600) {
7248
- const message = Reflect.get(err, "message");
7249
- const out = {
7250
- kind: "provider_5xx",
7251
- status: resolved_status
7252
- };
7253
- if (typeof message === "string") out["message"] = message;
7254
- return out;
7255
- }
7256
- const code = Reflect.get(err, "code");
7257
- if (code === "ECONNRESET" || code === "ETIMEDOUT" || code === "ENOTFOUND" || code === "ECONNREFUSED") {
7258
- const message = Reflect.get(err, "message");
7259
- const out = { kind: "network" };
7260
- if (typeof message === "string") out["message"] = message;
7261
- return out;
7262
- }
7263
- return err;
7599
+ * `ECONNREFUSED` error code becomes `network`. Anything else passes through
7600
+ * unclassified, which `retry_with_policy` treats as non-retryable.
7601
+ */
7602
+ function classify_provider_error(err) {
7603
+ if (err === null || typeof err !== "object") return err;
7604
+ if (has_retry_kind(err)) return err;
7605
+ const status = resolve_error_status(err);
7606
+ if (status === 429) return classify_rate_limit(err);
7607
+ if (status !== void 0 && status >= 500 && status < 600) return with_error_message(err, {
7608
+ kind: "provider_5xx",
7609
+ status
7610
+ });
7611
+ return classify_network_error(err) ?? err;
7264
7612
  }
7265
7613
  /**
7266
7614
  * Compose the per-attempt turn signal: the user's abort OR'd with a fresh
@@ -7406,31 +7754,26 @@ function build_native_invoke(cfg) {
7406
7754
  };
7407
7755
  }
7408
7756
  /**
7409
- * Resolve a `generate` call's options against engine defaults, run the tool
7410
- * loop, and return the aggregated result.
7757
+ * Resolve the provider name: per-call, then the engine default, then the sole
7758
+ * configured adapter, then 'anthropic'.
7759
+ */
7760
+ function resolve_provider(opts_in, engine) {
7761
+ const sole_provider = engine.adapters.size === 1 ? [...engine.adapters.keys()][0] : void 0;
7762
+ return opts_in.provider ?? engine.default_provider ?? sole_provider ?? "anthropic";
7763
+ }
7764
+ /**
7765
+ * Resolve the model/provider pair for one call and freeze the resolved
7766
+ * options view.
7411
7767
  *
7412
- * A provider whose adapter is `external` (`adapter.kind === 'external'`)
7413
- * delegates the whole call to `adapter.generate` before any of the logic
7414
- * below runs. For `ai_sdk` and native adapters, this function resolves
7415
- * model, provider, system prompt, and `provider_options` against engine
7416
- * defaults, checks the adapter supports every capability the call actually
7417
- * uses (schema, tools, streaming), builds the `InvokeOnce` closure for the
7418
- * resolved transport, and drives it through `run_tool_loop`. When a schema
7419
- * is set and the model's output fails validation, it appends a repair
7420
- * message and re-invokes the loop, until the response parses, repair
7421
- * attempts run out, or `max_steps` is reached. When a schema is set and the
7422
- * loop finishes for any reason other than `'stop'` (a content filter, the
7423
- * token limit, the step cap), no validated value can exist, so it throws
7424
- * `incomplete_generation_error` carrying the finish reason, the raw text,
7425
- * and the last `provider_reported` payload rather than returning unchecked
7426
- * text. Without a schema those finish reasons return normally.
7768
+ * The model must resolve (per-call, then engine default) or the call cannot
7769
+ * proceed. Engine-default provider_options merge under per-call ones here so
7770
+ * every transport downstream sees a single already-merged view instead of
7771
+ * re-merging per branch.
7427
7772
  */
7428
- async function generate(opts_in, engine) {
7429
- if (opts_in.abort?.aborted === true) throw new aborted_error("aborted", { reason: opts_in.abort.reason });
7773
+ function resolve_target(opts_in, engine) {
7430
7774
  const resolved_model = opts_in.model ?? engine.default_model;
7431
7775
  if (resolved_model === void 0) throw new model_required_error();
7432
- const sole_provider = engine.adapters.size === 1 ? [...engine.adapters.keys()][0] : void 0;
7433
- const resolved_provider = opts_in.provider ?? engine.default_provider ?? sole_provider ?? "anthropic";
7776
+ const resolved_provider = resolve_provider(opts_in, engine);
7434
7777
  const merged_provider_options = merge_provider_options(engine.default_provider_options, opts_in.provider_options);
7435
7778
  const opts = {
7436
7779
  ...opts_in,
@@ -7439,74 +7782,111 @@ async function generate(opts_in, engine) {
7439
7782
  };
7440
7783
  if (opts_in.system === void 0 && engine.default_system !== void 0) opts.system = engine.default_system;
7441
7784
  if (merged_provider_options !== void 0) opts.provider_options = merged_provider_options;
7442
- const target = {
7443
- provider: resolved_provider,
7444
- model_id: resolved_model
7785
+ return {
7786
+ opts,
7787
+ target: {
7788
+ provider: resolved_provider,
7789
+ model_id: resolved_model
7790
+ }
7445
7791
  };
7446
- const adapter = engine.adapters.get(target.provider);
7447
- if (adapter === void 0) throw new provider_not_configured_error(target.provider);
7448
- if (adapter.kind === "external") return adapter.generate(opts, target);
7449
- const trajectory = with_timestamps(opts.trajectory);
7450
- const on_chunk_provided = opts.on_chunk !== void 0;
7451
- const tools_list = opts.tools ?? [];
7452
- if (opts.schema !== void 0 && !adapter.supports("schema")) throw new provider_capability_error(target.provider, "schema");
7453
- if (tools_list.length > 0 && !adapter.supports("tools")) throw new provider_capability_error(target.provider, "tools");
7454
- if (on_chunk_provided && !adapter.supports("streaming")) throw new provider_capability_error(target.provider, "streaming");
7792
+ }
7793
+ /**
7794
+ * Reject a call that uses a capability the resolved adapter does not
7795
+ * implement. Checked up front, before any transport work, so the failure is a
7796
+ * typed capability error rather than a mid-call provider rejection.
7797
+ */
7798
+ function assert_capabilities(adapter, provider, opts, tools_list, on_chunk_provided) {
7799
+ if (opts.schema !== void 0 && !adapter.supports("schema")) throw new provider_capability_error(provider, "schema");
7800
+ if (tools_list.length > 0 && !adapter.supports("tools")) throw new provider_capability_error(provider, "tools");
7801
+ if (on_chunk_provided && !adapter.supports("streaming")) throw new provider_capability_error(provider, "streaming");
7802
+ }
7803
+ /**
7804
+ * Resolve the per-turn knobs (effort, retry policy, turn timeout) against
7805
+ * engine defaults. Validated here, before any transport is built, so a bad
7806
+ * budget fails as a config error, never as a spurious timeout.
7807
+ */
7808
+ function resolve_turn_config(opts, engine) {
7455
7809
  const effort = opts.effort ?? engine.default_effort;
7456
7810
  const retry_policy = opts.retry ?? engine.default_retry;
7457
7811
  const turn_timeout_ms = opts.turn_timeout_ms ?? engine.default_turn_timeout_ms;
7458
7812
  if (turn_timeout_ms !== void 0 && turn_timeout_ms <= 0) throw new engine_config_error("turn_timeout_ms must be > 0");
7459
- const dispatcher = create_chunk_dispatcher(opts.on_chunk);
7460
- let invoke_once;
7461
- if (adapter.kind === "ai_sdk") {
7462
- const effort_translation = adapter.translate_effort(effort);
7463
- if (effort !== "none" && effort_translation.effort_ignored) record_effort_ignored(trajectory, target.model_id);
7464
- const combined_provider_options = merge_provider_options(effort_translation.provider_options, opts.provider_options);
7465
- const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
7466
- let ai_sdk_invoke_mod;
7467
- try {
7468
- ai_sdk_invoke_mod = await import("./invoke-CWjm1Y_R.js");
7469
- } catch (err) {
7470
- throw missing_peer_error("ai", err);
7471
- }
7472
- const { create_ai_sdk_turn } = ai_sdk_invoke_mod;
7473
- invoke_once = build_ai_sdk_invoke({
7474
- invoke_turn: create_ai_sdk_turn({
7475
- adapter,
7476
- model_id: target.model_id,
7477
- dispatcher,
7478
- tools: tools_list,
7479
- schema: opts.schema,
7480
- provider_options,
7481
- temperature: opts.temperature,
7482
- max_tokens: opts.max_tokens,
7483
- top_p: opts.top_p,
7484
- telemetry: engine.default_ai_sdk_telemetry
7485
- }),
7486
- retry_policy,
7487
- turn_timeout_ms
7488
- });
7489
- } else invoke_once = build_native_invoke({
7490
- adapter,
7491
- model_id: target.model_id,
7492
- retry_policy,
7493
- turn_timeout_ms,
7494
- dispatcher,
7813
+ return {
7495
7814
  effort,
7496
- schema: opts.schema,
7497
- provider_options: opts.provider_options,
7498
- temperature: opts.temperature,
7499
- max_tokens: opts.max_tokens,
7500
- top_p: opts.top_p
7501
- });
7502
- const abort = opts.abort ?? new AbortController().signal;
7815
+ retry_policy,
7816
+ turn_timeout_ms
7817
+ };
7818
+ }
7819
+ /**
7820
+ * Resolve the tool-call salvage budget. Returned as a holder rather than a
7821
+ * count: one holder per generate call so schema-repair re-invocations of the
7822
+ * loop cannot refill the salvage budget. undefined disables salvage.
7823
+ */
7824
+ function resolve_salvage_budget(opts, engine) {
7825
+ const tool_call_repair_attempts = opts.tool_call_repair_attempts ?? engine.default_tool_call_repair_attempts ?? 0;
7826
+ if (tool_call_repair_attempts < 0) throw new engine_config_error("tool_call_repair_attempts must be >= 0");
7827
+ return tool_call_repair_attempts > 0 ? { remaining: tool_call_repair_attempts } : void 0;
7828
+ }
7829
+ /**
7830
+ * Resolve the loop-level limits and policies against engine defaults,
7831
+ * rejecting configurations the tool loop cannot honor.
7832
+ */
7833
+ function resolve_loop_limits(opts, engine) {
7503
7834
  const max_steps = opts.max_steps ?? engine.default_max_steps;
7504
7835
  const tool_error_policy = opts.tool_error_policy ?? engine.default_tool_error_policy ?? "feed_back";
7505
7836
  const schema_repair_attempts = opts.schema_repair_attempts ?? engine.default_schema_repair_attempts ?? 1;
7506
- const tool_call_repair_attempts = opts.tool_call_repair_attempts ?? engine.default_tool_call_repair_attempts ?? 0;
7507
- if (tool_call_repair_attempts < 0) throw new engine_config_error("tool_call_repair_attempts must be >= 0");
7837
+ const salvage_budget = resolve_salvage_budget(opts, engine);
7508
7838
  const max_tool_calls_per_step = opts.max_tool_calls_per_step ?? engine.default_max_tool_calls_per_step;
7509
7839
  if (max_tool_calls_per_step !== void 0 && max_tool_calls_per_step < 1) throw new engine_config_error("max_tool_calls_per_step must be >= 1");
7840
+ return {
7841
+ max_steps,
7842
+ tool_error_policy,
7843
+ schema_repair_attempts,
7844
+ salvage_budget,
7845
+ max_tool_calls_per_step
7846
+ };
7847
+ }
7848
+ /**
7849
+ * Build the ai_sdk-transport InvokeOnce for one call: translate effort, merge
7850
+ * provider options, load the SDK seam, and wrap it in the engine-owned retry
7851
+ * via build_ai_sdk_invoke.
7852
+ */
7853
+ async function build_ai_sdk_transport(cfg) {
7854
+ const { adapter, opts, effort } = cfg;
7855
+ const effort_translation = adapter.translate_effort(effort);
7856
+ if (effort !== "none" && effort_translation.effort_ignored) record_effort_ignored(cfg.trajectory, cfg.target.model_id);
7857
+ const combined_provider_options = merge_provider_options(effort_translation.provider_options, opts.provider_options);
7858
+ const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
7859
+ let ai_sdk_invoke_mod;
7860
+ try {
7861
+ ai_sdk_invoke_mod = await import("./invoke-BjrbCBSV.js");
7862
+ } catch (err) {
7863
+ throw missing_peer_error("ai", err);
7864
+ }
7865
+ const { create_ai_sdk_turn } = ai_sdk_invoke_mod;
7866
+ return build_ai_sdk_invoke({
7867
+ invoke_turn: create_ai_sdk_turn({
7868
+ adapter,
7869
+ model_id: cfg.target.model_id,
7870
+ dispatcher: cfg.dispatcher,
7871
+ tools: cfg.tools_list,
7872
+ schema: opts.schema,
7873
+ provider_options,
7874
+ temperature: opts.temperature,
7875
+ max_tokens: opts.max_tokens,
7876
+ top_p: opts.top_p,
7877
+ telemetry: cfg.telemetry
7878
+ }),
7879
+ retry_policy: cfg.retry_policy,
7880
+ turn_timeout_ms: cfg.turn_timeout_ms
7881
+ });
7882
+ }
7883
+ /**
7884
+ * Build the message list one generate call starts from. When a schema is set,
7885
+ * the JSON-only instruction folds into the existing system message (or is
7886
+ * prepended as one) so the model sees it exactly once, wherever the system
7887
+ * prompt came from.
7888
+ */
7889
+ function build_prompt_messages(opts) {
7510
7890
  const schema_prefix = opts.schema !== void 0 ? "You must respond with a single JSON value that conforms to the expected schema. Return ONLY the JSON value, with no markdown or commentary." : void 0;
7511
7891
  const initial_messages = build_initial_messages(opts);
7512
7892
  if (schema_prefix !== void 0) {
@@ -7522,100 +7902,240 @@ async function generate(opts_in, engine) {
7522
7902
  content: schema_prefix
7523
7903
  });
7524
7904
  }
7525
- const generate_span = start_generate_span(trajectory, {
7526
- model: resolved_model,
7527
- provider: target.provider,
7528
- model_id: target.model_id,
7529
- has_tools: tools_list.length > 0,
7530
- has_schema: opts.schema !== void 0,
7531
- streaming: on_chunk_provided
7532
- });
7533
- const pricing_dedup = create_pricing_missing_dedup(trajectory);
7905
+ return initial_messages;
7906
+ }
7907
+ /**
7908
+ * Assemble the ToolLoopConfig base shared by every loop invocation of one
7909
+ * generate call. Everything here is per-call state: the mutable transcript,
7910
+ * the pricing lookup, and the pricing_missing dedup all live for exactly one
7911
+ * call, which is what lets schema-repair re-invocations share them. The
7912
+ * conditional spreads keep optional fields absent rather than undefined for
7913
+ * exactOptionalPropertyTypes.
7914
+ */
7915
+ function build_loop_config(cfg) {
7916
+ const { opts, target, limits } = cfg;
7917
+ const abort = opts.abort ?? new AbortController().signal;
7534
7918
  const resolve_pricing = () => {
7535
- return engine.pricing[pricing_key(target.provider, target.model_id)];
7919
+ return cfg.pricing[pricing_key(target.provider, target.model_id)];
7536
7920
  };
7537
- const dispatch_chunk = on_chunk_provided ? async (chunk) => {
7538
- await dispatcher.dispatch(chunk);
7921
+ const dispatch_chunk = cfg.on_chunk_provided ? async (chunk) => {
7922
+ await cfg.dispatcher.dispatch(chunk);
7539
7923
  } : void 0;
7540
- const messages_mutable = [...initial_messages];
7541
- let total_steps = 0;
7924
+ return {
7925
+ invoke_once: cfg.invoke_once,
7926
+ messages: cfg.messages,
7927
+ tools: cfg.tools_list,
7928
+ max_steps: limits.max_steps,
7929
+ tool_error_policy: limits.tool_error_policy,
7930
+ abort,
7931
+ on_tool_approval: opts.on_tool_approval,
7932
+ trajectory: cfg.trajectory,
7933
+ stream: cfg.on_chunk_provided,
7934
+ dispatch_chunk,
7935
+ provider: target.provider,
7936
+ model_id: target.model_id,
7937
+ resolve_pricing,
7938
+ pricing_dedup: create_pricing_missing_dedup(cfg.trajectory),
7939
+ ...limits.salvage_budget !== void 0 ? { salvage_budget: limits.salvage_budget } : {},
7940
+ ...limits.max_tool_calls_per_step !== void 0 ? { max_tool_calls_per_step: limits.max_tool_calls_per_step } : {},
7941
+ ...opts.prepare_step !== void 0 ? { prepare_step: opts.prepare_step } : {}
7942
+ };
7943
+ }
7944
+ /**
7945
+ * One schema-validation pass over the loop's final text. A successful parse
7946
+ * returns the value in a holder (a schema can legitimately validate to
7947
+ * undefined, which must stay distinguishable from "nothing parsed"). A failed
7948
+ * parse records the trajectory event first, then either throws (`can_repair`
7949
+ * false: the repair or step budget is spent) or hands back the repair message
7950
+ * to append before re-invoking the loop.
7951
+ */
7952
+ async function parse_schema_step(cfg) {
7953
+ const parse = await parse_with_schema(cfg.schema, cfg.text);
7954
+ if (parse.ok) return { parsed: { value: parse.value } };
7955
+ record_schema_validation_failed(cfg.trajectory, {
7956
+ attempt: cfg.attempt,
7957
+ schema_issues: format_schema_issues(parse.issues),
7958
+ raw_text: cfg.text
7959
+ });
7960
+ if (!cfg.can_repair) throw_schema_validation(parse.issues, cfg.text);
7961
+ return { repair_message: build_repair_message(parse.issues) };
7962
+ }
7963
+ /**
7964
+ * Drive run_tool_loop to a final answer, re-invoking it for schema repair.
7965
+ *
7966
+ * Without a schema this is a single loop invocation. With one, the final text
7967
+ * must parse: a non-'stop' finish reason means no validated value can exist
7968
+ * (a content filter, the token limit, the step cap), so this throws
7969
+ * incomplete_generation_error rather than returning unchecked text; a failed
7970
+ * parse appends the repair message to the shared transcript and re-enters the
7971
+ * loop until the response parses, repair attempts run out, or max_steps is
7972
+ * reached. step_index_start advances by the accumulated step count so step
7973
+ * indices stay call-global across re-invocations.
7974
+ */
7975
+ async function run_generate_loop(cfg) {
7976
+ const { base, schema } = cfg;
7542
7977
  const steps_accum = [];
7543
7978
  const tool_calls_accum = [];
7544
7979
  let text = "";
7545
7980
  let finish_reason = "stop";
7546
- let repair_remaining = schema_repair_attempts;
7981
+ let repair_remaining = cfg.schema_repair_attempts;
7547
7982
  let content_parsed;
7548
- const salvage_budget = tool_call_repair_attempts > 0 ? { remaining: tool_call_repair_attempts } : void 0;
7549
- try {
7550
- while (true) {
7551
- const loop_result = await run_tool_loop({
7552
- invoke_once,
7553
- messages: messages_mutable,
7554
- tools: tools_list,
7555
- max_steps,
7556
- step_index_start: total_steps,
7557
- tool_error_policy,
7558
- abort,
7559
- on_tool_approval: opts.on_tool_approval,
7560
- trajectory,
7561
- stream: on_chunk_provided,
7562
- dispatch_chunk,
7563
- provider: target.provider,
7564
- model_id: target.model_id,
7565
- resolve_pricing,
7566
- pricing_dedup,
7567
- ...salvage_budget !== void 0 ? { salvage_budget } : {},
7568
- ...max_tool_calls_per_step !== void 0 ? { max_tool_calls_per_step } : {},
7569
- ...opts.prepare_step !== void 0 ? { prepare_step: opts.prepare_step } : {}
7570
- });
7571
- for (const s of loop_result.steps) steps_accum.push(s);
7572
- for (const tc of loop_result.tool_calls) tool_calls_accum.push(tc);
7573
- total_steps = steps_accum.length;
7574
- text = loop_result.text;
7575
- finish_reason = loop_result.finish_reason;
7576
- if (opts.schema === void 0) break;
7577
- if (finish_reason !== "stop") throw new incomplete_generation_error(finish_reason, text, last_provider_reported(steps_accum));
7578
- const parse = await parse_with_schema(opts.schema, text);
7579
- if (parse.ok) {
7580
- content_parsed = { value: parse.value };
7581
- break;
7582
- }
7583
- record_schema_validation_failed(trajectory, {
7584
- attempt: repair_remaining === schema_repair_attempts ? "initial" : "repair",
7585
- schema_issues: format_schema_issues(parse.issues),
7586
- raw_text: text
7587
- });
7588
- if (repair_remaining <= 0 || total_steps >= max_steps) throw_schema_validation(parse.issues, text);
7589
- repair_remaining -= 1;
7590
- messages_mutable.push(build_repair_message(parse.issues));
7983
+ while (true) {
7984
+ const loop_result = await run_tool_loop({
7985
+ ...base,
7986
+ step_index_start: steps_accum.length
7987
+ });
7988
+ for (const s of loop_result.steps) steps_accum.push(s);
7989
+ for (const tc of loop_result.tool_calls) tool_calls_accum.push(tc);
7990
+ text = loop_result.text;
7991
+ finish_reason = loop_result.finish_reason;
7992
+ if (schema === void 0) break;
7993
+ if (finish_reason !== "stop") throw new incomplete_generation_error(finish_reason, text, last_provider_reported(steps_accum));
7994
+ const schema_step = await parse_schema_step({
7995
+ schema,
7996
+ text,
7997
+ trajectory: base.trajectory,
7998
+ attempt: repair_remaining === cfg.schema_repair_attempts ? "initial" : "repair",
7999
+ can_repair: repair_remaining > 0 && steps_accum.length < base.max_steps
8000
+ });
8001
+ if ("parsed" in schema_step) {
8002
+ content_parsed = schema_step.parsed;
8003
+ break;
7591
8004
  }
7592
- const aggregated_usage = sum_usage(steps_accum);
7593
- const aggregated_cost = aggregate_cost(steps_accum, target.provider);
7594
- let final_content;
7595
- if (content_parsed !== void 0) final_content = content_parsed.value;
7596
- else final_content = text;
7597
- const result = {
7598
- content: final_content,
7599
- tool_calls: tool_calls_accum,
7600
- steps: steps_accum,
7601
- usage: aggregated_usage,
7602
- finish_reason,
7603
- model_resolved: {
7604
- provider: target.provider,
7605
- model_id: target.model_id
7606
- }
7607
- };
7608
- if (aggregated_cost !== void 0) result.cost = aggregated_cost;
7609
- const aggregated_reported = last_provider_reported(steps_accum);
7610
- if (aggregated_reported !== void 0) result.provider_reported = aggregated_reported;
8005
+ repair_remaining -= 1;
8006
+ base.messages.push(schema_step.repair_message);
8007
+ }
8008
+ return {
8009
+ steps: steps_accum,
8010
+ tool_calls: tool_calls_accum,
8011
+ text,
8012
+ finish_reason,
8013
+ content_parsed
8014
+ };
8015
+ }
8016
+ /**
8017
+ * Fold the loop outcome into the caller-facing GenerateResult: usage and cost
8018
+ * aggregate across every step (schema-repair re-invocations included), the
8019
+ * content is the parsed holder's value when a schema ran, and cost /
8020
+ * provider_reported attach only when a step actually reported them.
8021
+ */
8022
+ function assemble_result(outcome, target) {
8023
+ const aggregated_usage = sum_usage(outcome.steps);
8024
+ const aggregated_cost = aggregate_cost(outcome.steps, target.provider);
8025
+ let final_content;
8026
+ if (outcome.content_parsed !== void 0) final_content = outcome.content_parsed.value;
8027
+ else final_content = outcome.text;
8028
+ const result = {
8029
+ content: final_content,
8030
+ tool_calls: outcome.tool_calls,
8031
+ steps: outcome.steps,
8032
+ usage: aggregated_usage,
8033
+ finish_reason: outcome.finish_reason,
8034
+ model_resolved: {
8035
+ provider: target.provider,
8036
+ model_id: target.model_id
8037
+ }
8038
+ };
8039
+ if (aggregated_cost !== void 0) result.cost = aggregated_cost;
8040
+ const aggregated_reported = last_provider_reported(outcome.steps);
8041
+ if (aggregated_reported !== void 0) result.provider_reported = aggregated_reported;
8042
+ return result;
8043
+ }
8044
+ /**
8045
+ * Resolve a `generate` call's options against engine defaults, run the tool
8046
+ * loop, and return the aggregated result.
8047
+ *
8048
+ * A provider whose adapter is `external` (`adapter.kind === 'external'`)
8049
+ * delegates the whole call to `adapter.generate` before any of the logic
8050
+ * below runs. For `ai_sdk` and native adapters, this function resolves
8051
+ * model, provider, system prompt, and `provider_options` against engine
8052
+ * defaults, checks the adapter supports every capability the call actually
8053
+ * uses (schema, tools, streaming), builds the `InvokeOnce` closure for the
8054
+ * resolved transport, and drives it through `run_tool_loop`. When a schema
8055
+ * is set and the model's output fails validation, it appends a repair
8056
+ * message and re-invokes the loop, until the response parses, repair
8057
+ * attempts run out, or `max_steps` is reached. When a schema is set and the
8058
+ * loop finishes for any reason other than `'stop'` (a content filter, the
8059
+ * token limit, the step cap), no validated value can exist, so it throws
8060
+ * `incomplete_generation_error` carrying the finish reason, the raw text,
8061
+ * and the last `provider_reported` payload rather than returning unchecked
8062
+ * text. Without a schema those finish reasons return normally.
8063
+ */
8064
+ async function generate(opts_in, engine) {
8065
+ if (opts_in.abort?.aborted === true) throw new aborted_error("aborted", { reason: opts_in.abort.reason });
8066
+ const { opts, target } = resolve_target(opts_in, engine);
8067
+ const adapter = engine.adapters.get(target.provider);
8068
+ if (adapter === void 0) throw new provider_not_configured_error(target.provider);
8069
+ if (adapter.kind === "external") return adapter.generate(opts, target);
8070
+ const trajectory = with_timestamps(opts.trajectory);
8071
+ const on_chunk_provided = opts.on_chunk !== void 0;
8072
+ const tools_list = opts.tools ?? [];
8073
+ assert_capabilities(adapter, target.provider, opts, tools_list, on_chunk_provided);
8074
+ const turn = resolve_turn_config(opts, engine);
8075
+ const dispatcher = create_chunk_dispatcher(opts.on_chunk);
8076
+ let invoke_once;
8077
+ if (adapter.kind === "ai_sdk") invoke_once = await build_ai_sdk_transport({
8078
+ adapter,
8079
+ target,
8080
+ opts,
8081
+ effort: turn.effort,
8082
+ retry_policy: turn.retry_policy,
8083
+ turn_timeout_ms: turn.turn_timeout_ms,
8084
+ dispatcher,
8085
+ tools_list,
8086
+ trajectory,
8087
+ telemetry: engine.default_ai_sdk_telemetry
8088
+ });
8089
+ else invoke_once = build_native_invoke({
8090
+ adapter,
8091
+ model_id: target.model_id,
8092
+ retry_policy: turn.retry_policy,
8093
+ turn_timeout_ms: turn.turn_timeout_ms,
8094
+ dispatcher,
8095
+ effort: turn.effort,
8096
+ schema: opts.schema,
8097
+ provider_options: opts.provider_options,
8098
+ temperature: opts.temperature,
8099
+ max_tokens: opts.max_tokens,
8100
+ top_p: opts.top_p
8101
+ });
8102
+ const limits = resolve_loop_limits(opts, engine);
8103
+ const initial_messages = build_prompt_messages(opts);
8104
+ const generate_span = start_generate_span(trajectory, {
8105
+ model: target.model_id,
8106
+ provider: target.provider,
8107
+ model_id: target.model_id,
8108
+ has_tools: tools_list.length > 0,
8109
+ has_schema: opts.schema !== void 0,
8110
+ streaming: on_chunk_provided
8111
+ });
8112
+ const base = build_loop_config({
8113
+ opts,
8114
+ target,
8115
+ pricing: engine.pricing,
8116
+ invoke_once,
8117
+ messages: [...initial_messages],
8118
+ tools_list,
8119
+ trajectory,
8120
+ dispatcher,
8121
+ on_chunk_provided,
8122
+ limits
8123
+ });
8124
+ try {
8125
+ const outcome = await run_generate_loop({
8126
+ base,
8127
+ schema: opts.schema,
8128
+ schema_repair_attempts: limits.schema_repair_attempts
8129
+ });
8130
+ const result = assemble_result(outcome, target);
7611
8131
  if (on_chunk_provided) await dispatcher.dispatch({
7612
8132
  kind: "finish",
7613
- finish_reason,
7614
- usage: aggregated_usage
8133
+ finish_reason: outcome.finish_reason,
8134
+ usage: result.usage
7615
8135
  });
7616
8136
  end_generate_span(trajectory, generate_span, {
7617
- usage: aggregated_usage,
7618
- finish_reason,
8137
+ usage: result.usage,
8138
+ finish_reason: outcome.finish_reason,
7619
8139
  model_resolved: result.model_resolved
7620
8140
  });
7621
8141
  return result;
@@ -7644,46 +8164,57 @@ function last_provider_reported(steps) {
7644
8164
  return steps.findLast((s) => s.provider_reported !== void 0)?.provider_reported;
7645
8165
  }
7646
8166
  /**
7647
- * Sum per-step cost breakdowns into a single `CostBreakdown` for the whole
7648
- * generate call.
7649
- *
7650
- * Returns `undefined` if no step reports a cost, unless `provider` is free
7651
- * (`FREE_PROVIDERS`) and at least one step ran; in that case it returns an
7652
- * explicit all-zero breakdown instead of omitting cost entirely. The
7653
- * optional fields (`cached_input_usd`, `cache_write_usd`, `reasoning_usd`)
7654
- * are included only when at least one step reported them.
8167
+ * Fold every step's `cost` into a single {@link CostAccumulator}, skipping steps
8168
+ * that reported none. The required fields always sum; each optional field sums
8169
+ * and flips its `_present` flag only on the steps that carried it.
7655
8170
  */
7656
- function aggregate_cost(steps, provider) {
7657
- let any_present = false;
7658
- let total = 0;
7659
- let input = 0;
7660
- let output = 0;
7661
- let cached_present = false;
7662
- let cached = 0;
7663
- let cache_write_present = false;
7664
- let cache_write = 0;
7665
- let reasoning_present = false;
7666
- let reasoning = 0;
8171
+ function sum_step_costs(steps) {
8172
+ const acc = {
8173
+ any_present: false,
8174
+ total: 0,
8175
+ input: 0,
8176
+ output: 0,
8177
+ cached_present: false,
8178
+ cached: 0,
8179
+ cache_write_present: false,
8180
+ cache_write: 0,
8181
+ reasoning_present: false,
8182
+ reasoning: 0
8183
+ };
7667
8184
  for (const s of steps) {
7668
8185
  if (s.cost === void 0) continue;
7669
- any_present = true;
7670
- total += s.cost.total_usd;
7671
- input += s.cost.input_usd;
7672
- output += s.cost.output_usd;
8186
+ acc.any_present = true;
8187
+ acc.total += s.cost.total_usd;
8188
+ acc.input += s.cost.input_usd;
8189
+ acc.output += s.cost.output_usd;
7673
8190
  if (s.cost.cached_input_usd !== void 0) {
7674
- cached_present = true;
7675
- cached += s.cost.cached_input_usd;
8191
+ acc.cached_present = true;
8192
+ acc.cached += s.cost.cached_input_usd;
7676
8193
  }
7677
8194
  if (s.cost.cache_write_usd !== void 0) {
7678
- cache_write_present = true;
7679
- cache_write += s.cost.cache_write_usd;
8195
+ acc.cache_write_present = true;
8196
+ acc.cache_write += s.cost.cache_write_usd;
7680
8197
  }
7681
8198
  if (s.cost.reasoning_usd !== void 0) {
7682
- reasoning_present = true;
7683
- reasoning += s.cost.reasoning_usd;
8199
+ acc.reasoning_present = true;
8200
+ acc.reasoning += s.cost.reasoning_usd;
7684
8201
  }
7685
8202
  }
7686
- if (!any_present) {
8203
+ return acc;
8204
+ }
8205
+ /**
8206
+ * Sum per-step cost breakdowns into a single `CostBreakdown` for the whole
8207
+ * generate call.
8208
+ *
8209
+ * Returns `undefined` if no step reports a cost, unless `provider` is free
8210
+ * (`FREE_PROVIDERS`) and at least one step ran; in that case it returns an
8211
+ * explicit all-zero breakdown instead of omitting cost entirely. The
8212
+ * optional fields (`cached_input_usd`, `cache_write_usd`, `reasoning_usd`)
8213
+ * are included only when at least one step reported them.
8214
+ */
8215
+ function aggregate_cost(steps, provider) {
8216
+ const acc = sum_step_costs(steps);
8217
+ if (!acc.any_present) {
7687
8218
  if (FREE_PROVIDERS.has(provider) && steps.length > 0) return {
7688
8219
  total_usd: 0,
7689
8220
  input_usd: 0,
@@ -7694,15 +8225,15 @@ function aggregate_cost(steps, provider) {
7694
8225
  return;
7695
8226
  }
7696
8227
  const out = {
7697
- total_usd: round6(total),
7698
- input_usd: round6(input),
7699
- output_usd: round6(output),
8228
+ total_usd: round6(acc.total),
8229
+ input_usd: round6(acc.input),
8230
+ output_usd: round6(acc.output),
7700
8231
  currency: "USD",
7701
8232
  is_estimate: true
7702
8233
  };
7703
- if (cached_present) out.cached_input_usd = round6(cached);
7704
- if (cache_write_present) out.cache_write_usd = round6(cache_write);
7705
- if (reasoning_present) out.reasoning_usd = round6(reasoning);
8234
+ if (acc.cached_present) out.cached_input_usd = round6(acc.cached);
8235
+ if (acc.cache_write_present) out.cache_write_usd = round6(acc.cache_write);
8236
+ if (acc.reasoning_present) out.reasoning_usd = round6(acc.reasoning);
7706
8237
  return out;
7707
8238
  }
7708
8239
  //#endregion
@@ -7731,6 +8262,83 @@ function build_provider_adapters(providers, custom_providers) {
7731
8262
  return adapters;
7732
8263
  }
7733
8264
  /**
8265
+ * Seed a fresh, mutable pricing table from DEFAULT_PRICING and layer any
8266
+ * per-engine `config.pricing` overrides on top.
8267
+ *
8268
+ * The result is the engine's own table: register_price mutates it in place,
8269
+ * and DEFAULT_PRICING (frozen at module load) is never touched.
8270
+ */
8271
+ function merge_pricing(overrides) {
8272
+ const pricing = { ...DEFAULT_PRICING };
8273
+ if (overrides !== void 0) for (const [key, value] of Object.entries(overrides)) pricing[key] = value;
8274
+ return pricing;
8275
+ }
8276
+ /**
8277
+ * Resolve the three always-present scalar defaults.
8278
+ *
8279
+ * `config.defaults.*` is the current nested layer; `config.default_retry`,
8280
+ * `config.default_effort`, and `config.default_max_steps` are older top-level
8281
+ * fields kept for compatibility. The nested value wins where both are set, and
8282
+ * the built-in constant is the last resort.
8283
+ */
8284
+ function resolve_default_scalars(config) {
8285
+ const defaults = config.defaults ?? {};
8286
+ return {
8287
+ default_retry: defaults.retry_policy ?? config.default_retry ?? DEFAULT_RETRY,
8288
+ default_effort: defaults.effort ?? config.default_effort ?? "none",
8289
+ default_max_steps: defaults.max_steps ?? config.default_max_steps ?? 10
8290
+ };
8291
+ }
8292
+ /**
8293
+ * Validate the numeric defaults once, at construction: a bad bound should fail
8294
+ * fast here rather than surfacing deep inside the first generate call that ends
8295
+ * up using the default.
8296
+ */
8297
+ function validate_numeric_defaults(defaults) {
8298
+ if (defaults === void 0) return;
8299
+ const { tool_call_repair_attempts, max_tool_calls_per_step, turn_timeout_ms } = defaults;
8300
+ if (tool_call_repair_attempts !== void 0 && tool_call_repair_attempts < 0) throw new engine_config_error("defaults.tool_call_repair_attempts must be >= 0");
8301
+ if (max_tool_calls_per_step !== void 0 && max_tool_calls_per_step < 1) throw new engine_config_error("defaults.max_tool_calls_per_step must be >= 1");
8302
+ if (turn_timeout_ms !== void 0 && turn_timeout_ms <= 0) throw new engine_config_error("defaults.turn_timeout_ms must be > 0");
8303
+ }
8304
+ /**
8305
+ * Copy `value` onto `target[key]` only when it is defined, so an absent optional
8306
+ * default leaves the key off the object rather than present-as-undefined.
8307
+ */
8308
+ function assign_if_present$1(target, key, value) {
8309
+ if (value !== void 0) target[key] = value;
8310
+ }
8311
+ /**
8312
+ * Assemble the per-instance EngineInternals from the resolved required fields
8313
+ * plus whichever optional `defaults.*` knobs were supplied.
8314
+ *
8315
+ * The optional defaults form a fixed source-to-target table (`defaults.model` ->
8316
+ * `default_model`, and so on); each is folded in only when present, matching the
8317
+ * conditional-spread shape this replaced.
8318
+ */
8319
+ function build_internals(base, defaults) {
8320
+ const internals = {
8321
+ pricing: base.pricing,
8322
+ adapters: base.adapters,
8323
+ default_retry: base.default_retry,
8324
+ default_effort: base.default_effort,
8325
+ default_max_steps: base.default_max_steps
8326
+ };
8327
+ if (defaults !== void 0) {
8328
+ assign_if_present$1(internals, "default_model", defaults.model);
8329
+ assign_if_present$1(internals, "default_provider", defaults.provider);
8330
+ assign_if_present$1(internals, "default_system", defaults.system);
8331
+ assign_if_present$1(internals, "default_tool_error_policy", defaults.tool_error_policy);
8332
+ assign_if_present$1(internals, "default_schema_repair_attempts", defaults.schema_repair_attempts);
8333
+ assign_if_present$1(internals, "default_tool_call_repair_attempts", defaults.tool_call_repair_attempts);
8334
+ assign_if_present$1(internals, "default_max_tool_calls_per_step", defaults.max_tool_calls_per_step);
8335
+ assign_if_present$1(internals, "default_turn_timeout_ms", defaults.turn_timeout_ms);
8336
+ assign_if_present$1(internals, "default_ai_sdk_telemetry", defaults.ai_sdk_telemetry);
8337
+ assign_if_present$1(internals, "default_provider_options", defaults.provider_options);
8338
+ }
8339
+ return internals;
8340
+ }
8341
+ /**
7734
8342
  * Validate `config` and construct an `Engine`.
7735
8343
  *
7736
8344
  * Provider entries are validated synchronously via `build_provider_adapters`.
@@ -7740,32 +8348,16 @@ function build_provider_adapters(providers, custom_providers) {
7740
8348
  function create_engine(config) {
7741
8349
  if (config.providers === null || typeof config.providers !== "object") throw new engine_config_error("EngineConfig.providers is required");
7742
8350
  const adapters = build_provider_adapters(config.providers, config.custom_providers);
7743
- const pricing = { ...DEFAULT_PRICING };
7744
- if (config.pricing !== void 0) for (const [key, value] of Object.entries(config.pricing)) pricing[key] = value;
7745
- const defaults = config.defaults;
7746
- const default_retry = defaults?.retry_policy ?? config.default_retry ?? DEFAULT_RETRY;
7747
- const default_effort = defaults?.effort ?? config.default_effort ?? "none";
7748
- const default_max_steps = defaults?.max_steps ?? config.default_max_steps ?? 10;
7749
- if (defaults?.tool_call_repair_attempts !== void 0 && defaults.tool_call_repair_attempts < 0) throw new engine_config_error("defaults.tool_call_repair_attempts must be >= 0");
7750
- if (defaults?.max_tool_calls_per_step !== void 0 && defaults.max_tool_calls_per_step < 1) throw new engine_config_error("defaults.max_tool_calls_per_step must be >= 1");
7751
- if (defaults?.turn_timeout_ms !== void 0 && defaults.turn_timeout_ms <= 0) throw new engine_config_error("defaults.turn_timeout_ms must be > 0");
7752
- const get_internals = () => ({
8351
+ const pricing = merge_pricing(config.pricing);
8352
+ const { default_retry, default_effort, default_max_steps } = resolve_default_scalars(config);
8353
+ validate_numeric_defaults(config.defaults);
8354
+ const get_internals = () => build_internals({
7753
8355
  pricing,
7754
8356
  adapters,
7755
8357
  default_retry,
7756
8358
  default_effort,
7757
- default_max_steps,
7758
- ...defaults?.model !== void 0 ? { default_model: defaults.model } : {},
7759
- ...defaults?.provider !== void 0 ? { default_provider: defaults.provider } : {},
7760
- ...defaults?.system !== void 0 ? { default_system: defaults.system } : {},
7761
- ...defaults?.tool_error_policy !== void 0 ? { default_tool_error_policy: defaults.tool_error_policy } : {},
7762
- ...defaults?.schema_repair_attempts !== void 0 ? { default_schema_repair_attempts: defaults.schema_repair_attempts } : {},
7763
- ...defaults?.tool_call_repair_attempts !== void 0 ? { default_tool_call_repair_attempts: defaults.tool_call_repair_attempts } : {},
7764
- ...defaults?.max_tool_calls_per_step !== void 0 ? { default_max_tool_calls_per_step: defaults.max_tool_calls_per_step } : {},
7765
- ...defaults?.turn_timeout_ms !== void 0 ? { default_turn_timeout_ms: defaults.turn_timeout_ms } : {},
7766
- ...defaults?.ai_sdk_telemetry !== void 0 ? { default_ai_sdk_telemetry: defaults.ai_sdk_telemetry } : {},
7767
- ...defaults?.provider_options !== void 0 ? { default_provider_options: defaults.provider_options } : {}
7768
- });
8359
+ default_max_steps
8360
+ }, config.defaults);
7769
8361
  let disposed = false;
7770
8362
  let dispose_promise;
7771
8363
  return {
@@ -7871,6 +8463,41 @@ function stable_signature(input) {
7871
8463
  return createHash("sha256").update(payload).digest("hex").slice(0, 8);
7872
8464
  }
7873
8465
  /**
8466
+ * Copy `value` onto `target[key]` only when it is defined, so an absent optional
8467
+ * config field leaves the key off `GenerateOptions` rather than present-as-undefined.
8468
+ */
8469
+ function assign_if_present(target, key, value) {
8470
+ if (value !== void 0) target[key] = value;
8471
+ }
8472
+ /**
8473
+ * Translate `cfg` into a `GenerateOptions`, folding each supplied optional field
8474
+ * in via `assign_if_present`. `tools` (spread to a fresh array) and
8475
+ * `retry_policy` (renamed to `retry`) are the two fields that do not copy
8476
+ * straight across and stay explicit; the rest map name-for-name.
8477
+ */
8478
+ function build_generate_options(cfg, prompt, ctx) {
8479
+ const opts = {
8480
+ prompt,
8481
+ abort: ctx.abort,
8482
+ trajectory: engine_trajectory(ctx.trajectory, ctx.parent_span_id)
8483
+ };
8484
+ assign_if_present(opts, "model", cfg.model);
8485
+ assign_if_present(opts, "provider", cfg.provider);
8486
+ assign_if_present(opts, "system", cfg.system);
8487
+ if (cfg.tools !== void 0) opts.tools = [...cfg.tools];
8488
+ assign_if_present(opts, "schema", cfg.schema);
8489
+ assign_if_present(opts, "effort", cfg.effort);
8490
+ assign_if_present(opts, "max_steps", cfg.max_steps);
8491
+ assign_if_present(opts, "provider_options", cfg.provider_options);
8492
+ if (cfg.retry_policy !== void 0) opts.retry = cfg.retry_policy;
8493
+ assign_if_present(opts, "tool_error_policy", cfg.tool_error_policy);
8494
+ assign_if_present(opts, "schema_repair_attempts", cfg.schema_repair_attempts);
8495
+ assign_if_present(opts, "tool_call_repair_attempts", cfg.tool_call_repair_attempts);
8496
+ assign_if_present(opts, "max_tool_calls_per_step", cfg.max_tool_calls_per_step);
8497
+ assign_if_present(opts, "on_tool_approval", cfg.on_tool_approval);
8498
+ return opts;
8499
+ }
8500
+ /**
7874
8501
  * Build a `Step` that calls `cfg.engine.generate` with `cfg` translated into
7875
8502
  * `GenerateOptions`.
7876
8503
  *
@@ -7901,31 +8528,13 @@ function model_call(cfg) {
7901
8528
  reason: { signal: "abort" },
7902
8529
  step_index: 0
7903
8530
  });
7904
- const opts = {
7905
- prompt: typeof input === "string" ? [{
7906
- role: "user",
7907
- content: [{
7908
- type: "text",
7909
- text: input
7910
- }]
7911
- }] : [...input],
7912
- abort: ctx.abort,
7913
- trajectory: engine_trajectory(ctx.trajectory, ctx.parent_span_id)
7914
- };
7915
- if (cfg.model !== void 0) opts.model = cfg.model;
7916
- if (cfg.provider !== void 0) opts.provider = cfg.provider;
7917
- if (cfg.system !== void 0) opts.system = cfg.system;
7918
- if (cfg.tools !== void 0) opts.tools = [...cfg.tools];
7919
- if (cfg.schema !== void 0) opts.schema = cfg.schema;
7920
- if (cfg.effort !== void 0) opts.effort = cfg.effort;
7921
- if (cfg.max_steps !== void 0) opts.max_steps = cfg.max_steps;
7922
- if (cfg.provider_options !== void 0) opts.provider_options = cfg.provider_options;
7923
- if (cfg.retry_policy !== void 0) opts.retry = cfg.retry_policy;
7924
- if (cfg.tool_error_policy !== void 0) opts.tool_error_policy = cfg.tool_error_policy;
7925
- if (cfg.schema_repair_attempts !== void 0) opts.schema_repair_attempts = cfg.schema_repair_attempts;
7926
- if (cfg.tool_call_repair_attempts !== void 0) opts.tool_call_repair_attempts = cfg.tool_call_repair_attempts;
7927
- if (cfg.max_tool_calls_per_step !== void 0) opts.max_tool_calls_per_step = cfg.max_tool_calls_per_step;
7928
- if (cfg.on_tool_approval !== void 0) opts.on_tool_approval = cfg.on_tool_approval;
8531
+ const opts = build_generate_options(cfg, typeof input === "string" ? [{
8532
+ role: "user",
8533
+ content: [{
8534
+ type: "text",
8535
+ text: input
8536
+ }]
8537
+ }] : [...input], ctx);
7929
8538
  if (ctx.streaming) opts.on_chunk = (chunk) => {
7930
8539
  ctx.trajectory.record({
7931
8540
  kind: "model_chunk",
@@ -8423,19 +9032,21 @@ async function start_viewer(options = {}) {
8423
9032
  ...options.on_parse_error ? { on_parse_error: options.on_parse_error } : {},
8424
9033
  ...options.on_io_error ? { on_io_error: options.on_io_error } : {}
8425
9034
  });
8426
- return wrap_handle(server, tail, host, port);
9035
+ return wrap_handle(server, tail, host);
8427
9036
  }
8428
9037
  /**
8429
9038
  * Combines the server and optional tail into a single `ViewerHandle`.
8430
9039
  *
8431
9040
  * `close` stops the tail first, then closes the server, mirroring the
8432
- * startup order (server first, tail second) in reverse.
9041
+ * startup order (server first, tail second) in reverse. The port is read
9042
+ * back from `server.url` rather than the requested `port` option, since
9043
+ * a caller that passes `0` gets whatever port the OS actually bound.
8433
9044
  */
8434
- function wrap_handle(server, tail, host, port) {
9045
+ function wrap_handle(server, tail, host) {
8435
9046
  return {
8436
9047
  url: server.url,
8437
9048
  host,
8438
- port,
9049
+ port: Number(new URL(server.url).port),
8439
9050
  close: async () => {
8440
9051
  if (tail) tail.stop();
8441
9052
  await server.close();