fascicle 0.8.12 → 0.8.16

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
@@ -3098,6 +3098,37 @@ function record_schema_validation_failed(trajectory, meta) {
3098
3098
  raw_text: meta.raw_text
3099
3099
  });
3100
3100
  }
3101
+ /**
3102
+ * Emitted once per step whose tool calls were recovered from assistant text
3103
+ * rather than returned structurally (tool_call_repair_attempts). The per-call
3104
+ * tool_call/tool_result events still fire for each salvaged call; this event
3105
+ * answers why the step has calls the provider never emitted, and persists the
3106
+ * raw text for debugging (precedent: schema_validation_failed).
3107
+ */
3108
+ function record_tool_call_salvaged(trajectory, meta) {
3109
+ if (trajectory === void 0) return;
3110
+ trajectory.record({
3111
+ kind: "tool_call_salvaged",
3112
+ step_index: meta.step_index,
3113
+ calls: meta.calls,
3114
+ raw_text: meta.raw_text
3115
+ });
3116
+ }
3117
+ /**
3118
+ * Emitted when max_tool_calls_per_step drops calls beyond the cap. Dropped
3119
+ * calls also get ToolCallRecords with an error, but only this event carries
3120
+ * the configured cap alongside what was kept.
3121
+ */
3122
+ function record_tool_calls_dropped(trajectory, meta) {
3123
+ if (trajectory === void 0) return;
3124
+ trajectory.record({
3125
+ kind: "tool_calls_dropped",
3126
+ step_index: meta.step_index,
3127
+ max_tool_calls_per_step: meta.max_tool_calls_per_step,
3128
+ kept: meta.kept,
3129
+ dropped: meta.dropped
3130
+ });
3131
+ }
3101
3132
  function record_tool_approval(trajectory, kind, meta) {
3102
3133
  if (trajectory === void 0) return;
3103
3134
  trajectory.record({
@@ -3262,6 +3293,8 @@ function create_claude_cli_adapter(init) {
3262
3293
  if (opts.max_steps !== void 0) option_ignored.emit("max_steps", PROVIDER_NAME);
3263
3294
  if (opts.tool_error_policy !== void 0) option_ignored.emit("tool_error_policy", PROVIDER_NAME);
3264
3295
  if (opts.on_tool_approval !== void 0) option_ignored.emit("on_tool_approval", PROVIDER_NAME);
3296
+ if (opts.tool_call_repair_attempts !== void 0) option_ignored.emit("tool_call_repair_attempts", PROVIDER_NAME);
3297
+ if (opts.max_tool_calls_per_step !== void 0) option_ignored.emit("max_tool_calls_per_step", PROVIDER_NAME);
3265
3298
  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");
3266
3299
  const tools_list = opts.tools ?? [];
3267
3300
  const tool_bridge = call_opts.tool_bridge ?? "allowlist_only";
@@ -3464,7 +3497,7 @@ function merge_provider_options(defaults, call) {
3464
3497
  for (const [provider, inner] of Object.entries(defaults)) out[provider] = { ...inner };
3465
3498
  for (const [provider, inner] of Object.entries(call)) {
3466
3499
  const existing = out[provider];
3467
- if (is_plain_object(existing) && is_plain_object(inner)) out[provider] = {
3500
+ if (is_plain_object$1(existing) && is_plain_object$1(inner)) out[provider] = {
3468
3501
  ...existing,
3469
3502
  ...inner
3470
3503
  };
@@ -3472,7 +3505,7 @@ function merge_provider_options(defaults, call) {
3472
3505
  }
3473
3506
  return out;
3474
3507
  }
3475
- function is_plain_object(value) {
3508
+ function is_plain_object$1(value) {
3476
3509
  return value !== null && typeof value === "object" && !Array.isArray(value);
3477
3510
  }
3478
3511
  //#endregion
@@ -3539,6 +3572,274 @@ function sum_usage(steps) {
3539
3572
  return total;
3540
3573
  }
3541
3574
  //#endregion
3575
+ //#region src/engine/tool_call_salvage.ts
3576
+ const TOOL_CALL_OPEN = "<tool_call>";
3577
+ const TOOL_CALL_CLOSE = "</tool_call>";
3578
+ const FUNCTION_RE = /<function=([^>\s]+)>([\s\S]*?)<\/function>/g;
3579
+ const PARAMETER_RE = /<parameter=([^>\s]+)>([\s\S]*?)<\/parameter>/g;
3580
+ const FENCE_RE = /```([^\n]*)\n([\s\S]*?)```/g;
3581
+ const EMPTY_BLOCK_RE = /<tool_call>\s*<\/tool_call>/g;
3582
+ /**
3583
+ * Find the extent of one balanced JSON object starting at `start_index`
3584
+ * (which must point at `{`), respecting strings and escapes so braces inside
3585
+ * string values do not affect the depth count. The scanner only finds the
3586
+ * extent; JSON.parse remains the sole semantic authority.
3587
+ */
3588
+ function scan_balanced_json(text, start_index) {
3589
+ if (text[start_index] !== "{") return void 0;
3590
+ let depth = 0;
3591
+ let in_string = false;
3592
+ let escaped = false;
3593
+ for (let i = start_index; i < text.length; i += 1) {
3594
+ const ch = text[i];
3595
+ if (in_string) {
3596
+ if (escaped) escaped = false;
3597
+ else if (ch === "\\") escaped = true;
3598
+ else if (ch === "\"") in_string = false;
3599
+ continue;
3600
+ }
3601
+ if (ch === "\"") in_string = true;
3602
+ else if (ch === "{") depth += 1;
3603
+ else if (ch === "}") {
3604
+ depth -= 1;
3605
+ if (depth === 0) {
3606
+ const end = i + 1;
3607
+ try {
3608
+ return {
3609
+ value: JSON.parse(text.slice(start_index, end)),
3610
+ end
3611
+ };
3612
+ } catch {
3613
+ return;
3614
+ }
3615
+ }
3616
+ }
3617
+ }
3618
+ }
3619
+ function is_plain_object(value) {
3620
+ return typeof value === "object" && value !== null && !Array.isArray(value);
3621
+ }
3622
+ /**
3623
+ * Exactly {name, arguments} with a string name and object arguments. This is
3624
+ * the shape firewall against prose that happens to contain JSON.
3625
+ */
3626
+ function call_shape(value) {
3627
+ if (!is_plain_object(value)) return void 0;
3628
+ if (Object.keys(value).length !== 2) return void 0;
3629
+ const name = value["name"];
3630
+ const args = value["arguments"];
3631
+ if (typeof name !== "string" || !is_plain_object(args)) return void 0;
3632
+ return {
3633
+ name,
3634
+ args
3635
+ };
3636
+ }
3637
+ function overlaps_any(span, masks) {
3638
+ return masks.some((m) => span.start < m.end && m.start < span.end);
3639
+ }
3640
+ function mask_containing(masks, index) {
3641
+ return masks.find((m) => index >= m.start && index < m.end);
3642
+ }
3643
+ function trim_one_newline(value) {
3644
+ let out = value;
3645
+ if (out.startsWith("\n")) out = out.slice(1);
3646
+ if (out.endsWith("\n")) out = out.slice(0, -1);
3647
+ return out;
3648
+ }
3649
+ function scan_qwen_functions(inner_start, inner) {
3650
+ const out = [];
3651
+ for (const fn of inner.matchAll(FUNCTION_RE)) {
3652
+ const body = fn[2] ?? "";
3653
+ const params = {};
3654
+ let leftover = body;
3655
+ for (const pm of body.matchAll(PARAMETER_RE)) {
3656
+ params[pm[1] ?? ""] = trim_one_newline(pm[2] ?? "");
3657
+ leftover = leftover.replace(pm[0], "");
3658
+ }
3659
+ if (leftover.trim().length !== 0) continue;
3660
+ out.push({
3661
+ name: fn[1] ?? "",
3662
+ args: params,
3663
+ format: "qwen_xml",
3664
+ span: {
3665
+ start: inner_start + fn.index,
3666
+ end: inner_start + fn.index + fn[0].length
3667
+ }
3668
+ });
3669
+ }
3670
+ return out;
3671
+ }
3672
+ function scan_tool_call_blocks(text) {
3673
+ const candidates = [];
3674
+ const masks = [];
3675
+ let pos = 0;
3676
+ while (true) {
3677
+ const open = text.indexOf(TOOL_CALL_OPEN, pos);
3678
+ if (open === -1) break;
3679
+ const close = text.indexOf(TOOL_CALL_CLOSE, open + 11);
3680
+ if (close === -1) {
3681
+ masks.push({
3682
+ start: open,
3683
+ end: text.length
3684
+ });
3685
+ break;
3686
+ }
3687
+ const block_end = close + 12;
3688
+ masks.push({
3689
+ start: open,
3690
+ end: block_end
3691
+ });
3692
+ const inner_start = open + 11;
3693
+ const inner = text.slice(inner_start, close);
3694
+ const trimmed = inner.trim();
3695
+ if (trimmed.startsWith("<function=")) candidates.push(...scan_qwen_functions(inner_start, inner));
3696
+ else if (trimmed.startsWith("{")) {
3697
+ const brace = text.indexOf("{", inner_start);
3698
+ const scanned = brace === -1 ? void 0 : scan_balanced_json(text, brace);
3699
+ const shaped = scanned === void 0 ? void 0 : call_shape(scanned.value);
3700
+ if (shaped !== void 0) candidates.push({
3701
+ name: shaped.name,
3702
+ args: shaped.args,
3703
+ format: "hermes",
3704
+ span: {
3705
+ start: open,
3706
+ end: block_end
3707
+ }
3708
+ });
3709
+ }
3710
+ pos = block_end;
3711
+ }
3712
+ return {
3713
+ candidates,
3714
+ masks
3715
+ };
3716
+ }
3717
+ function scan_fences(text, block_masks) {
3718
+ const candidates = [];
3719
+ const masks = [];
3720
+ for (const m of text.matchAll(FENCE_RE)) {
3721
+ const span = {
3722
+ start: m.index,
3723
+ end: m.index + m[0].length
3724
+ };
3725
+ if (overlaps_any(span, block_masks)) continue;
3726
+ masks.push(span);
3727
+ const info = (m[1] ?? "").trim();
3728
+ if (info !== "" && info !== "json") continue;
3729
+ let value;
3730
+ try {
3731
+ value = JSON.parse((m[2] ?? "").trim());
3732
+ } catch {
3733
+ continue;
3734
+ }
3735
+ const shaped = call_shape(value);
3736
+ if (shaped === void 0) continue;
3737
+ candidates.push({
3738
+ name: shaped.name,
3739
+ args: shaped.args,
3740
+ format: "json",
3741
+ span
3742
+ });
3743
+ }
3744
+ return {
3745
+ candidates,
3746
+ masks
3747
+ };
3748
+ }
3749
+ function scan_bare_json(text, masks) {
3750
+ const out = [];
3751
+ let i = 0;
3752
+ while (i < text.length) {
3753
+ const masked = mask_containing(masks, i);
3754
+ if (masked !== void 0) {
3755
+ i = masked.end;
3756
+ continue;
3757
+ }
3758
+ if (text[i] !== "{") {
3759
+ i += 1;
3760
+ continue;
3761
+ }
3762
+ const scanned = scan_balanced_json(text, i);
3763
+ if (scanned === void 0) {
3764
+ i += 1;
3765
+ continue;
3766
+ }
3767
+ const span = {
3768
+ start: i,
3769
+ end: scanned.end
3770
+ };
3771
+ const shaped = overlaps_any(span, masks) ? void 0 : call_shape(scanned.value);
3772
+ if (shaped !== void 0) out.push({
3773
+ name: shaped.name,
3774
+ args: shaped.args,
3775
+ format: "json",
3776
+ span
3777
+ });
3778
+ i = scanned.end;
3779
+ }
3780
+ return out;
3781
+ }
3782
+ function validate_candidate(candidate, tool_map) {
3783
+ const tool = tool_map.get(candidate.name);
3784
+ if (tool === void 0) return void 0;
3785
+ const schema = tool.input_schema;
3786
+ const raw = schema.safeParse(candidate.args);
3787
+ if (raw.success) return {
3788
+ name: candidate.name,
3789
+ input: raw.data,
3790
+ format: candidate.format,
3791
+ span: candidate.span
3792
+ };
3793
+ if (candidate.format !== "qwen_xml") return void 0;
3794
+ const coerced = {};
3795
+ for (const [k, v] of Object.entries(candidate.args)) {
3796
+ if (typeof v !== "string") {
3797
+ coerced[k] = v;
3798
+ continue;
3799
+ }
3800
+ try {
3801
+ coerced[k] = JSON.parse(v);
3802
+ } catch {
3803
+ coerced[k] = v;
3804
+ }
3805
+ }
3806
+ const retried = schema.safeParse(coerced);
3807
+ if (!retried.success) return void 0;
3808
+ return {
3809
+ name: candidate.name,
3810
+ input: retried.data,
3811
+ format: candidate.format,
3812
+ span: candidate.span
3813
+ };
3814
+ }
3815
+ function strip_spans(text, spans) {
3816
+ let out = text;
3817
+ const descending = spans.toSorted((a, b) => b.start - a.start);
3818
+ for (const s of descending) out = out.slice(0, s.start) + out.slice(s.end);
3819
+ return out.replace(EMPTY_BLOCK_RE, "").trim();
3820
+ }
3821
+ function salvage_tool_calls(text, tool_map) {
3822
+ if (text.length === 0) return void 0;
3823
+ const blocks = scan_tool_call_blocks(text);
3824
+ const fences = scan_fences(text, blocks.masks);
3825
+ const all_masks = [...blocks.masks, ...fences.masks];
3826
+ const candidates = [
3827
+ ...blocks.candidates,
3828
+ ...fences.candidates,
3829
+ ...scan_bare_json(text, all_masks)
3830
+ ].toSorted((a, b) => a.span.start - b.span.start);
3831
+ const calls = [];
3832
+ for (const c of candidates) {
3833
+ const validated = validate_candidate(c, tool_map);
3834
+ if (validated !== void 0) calls.push(validated);
3835
+ }
3836
+ if (calls.length === 0) return void 0;
3837
+ return {
3838
+ calls,
3839
+ stripped_text: strip_spans(text, calls.map((c) => c.span))
3840
+ };
3841
+ }
3842
+ //#endregion
3542
3843
  //#region src/engine/tool_loop.ts
3543
3844
  function throw_if_aborted(abort, step_index) {
3544
3845
  if (!abort.aborted) return;
@@ -3732,10 +4033,68 @@ async function run_tool_loop(config) {
3732
4033
  end_step_span(config.trajectory, step_span, { error: message });
3733
4034
  throw err;
3734
4035
  }
4036
+ let effective_calls = turn.tool_calls;
4037
+ let history_text = turn.text;
4038
+ let salvage;
4039
+ const salvaged_formats = /* @__PURE__ */ new Map();
4040
+ 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) {
4041
+ salvage = salvage_tool_calls(turn.text, tool_map);
4042
+ if (salvage !== void 0) {
4043
+ config.salvage_budget.remaining -= 1;
4044
+ history_text = salvage.stripped_text;
4045
+ effective_calls = salvage.calls.map((c, n) => {
4046
+ const id = `salvaged_${step_index}_${n}`;
4047
+ salvaged_formats.set(id, c.format);
4048
+ return {
4049
+ id,
4050
+ name: c.name,
4051
+ input: c.input
4052
+ };
4053
+ });
4054
+ record_tool_call_salvaged(config.trajectory, {
4055
+ step_index,
4056
+ calls: effective_calls.map((c) => ({
4057
+ tool_call_id: c.id,
4058
+ name: c.name,
4059
+ format: salvaged_formats.get(c.id) ?? "json"
4060
+ })),
4061
+ raw_text: turn.text
4062
+ });
4063
+ if (config.dispatch_chunk !== void 0) for (const c of effective_calls) {
4064
+ await config.dispatch_chunk({
4065
+ kind: "tool_call_start",
4066
+ id: c.id,
4067
+ name: c.name,
4068
+ step_index
4069
+ });
4070
+ await config.dispatch_chunk({
4071
+ kind: "tool_call_end",
4072
+ id: c.id,
4073
+ input: c.input,
4074
+ step_index
4075
+ });
4076
+ }
4077
+ }
4078
+ }
4079
+ let dropped_calls = [];
4080
+ const per_step_cap = config.max_tool_calls_per_step;
4081
+ if (per_step_cap !== void 0 && effective_calls.length > per_step_cap) {
4082
+ dropped_calls = effective_calls.slice(per_step_cap);
4083
+ effective_calls = effective_calls.slice(0, per_step_cap);
4084
+ record_tool_calls_dropped(config.trajectory, {
4085
+ step_index,
4086
+ max_tool_calls_per_step: per_step_cap,
4087
+ kept: effective_calls.length,
4088
+ dropped: dropped_calls.map((d) => ({
4089
+ tool_call_id: d.id,
4090
+ name: d.name
4091
+ }))
4092
+ });
4093
+ }
3735
4094
  const step_tool_records = [];
3736
- const assistant_message = build_assistant_message(turn.text, turn.tool_calls);
4095
+ const assistant_message = build_assistant_message(history_text, effective_calls);
3737
4096
  config.messages.push(assistant_message);
3738
- if (turn.tool_calls.length === 0) {
4097
+ if (effective_calls.length === 0) {
3739
4098
  text = turn.text;
3740
4099
  finish_reason = turn.finish_reason;
3741
4100
  const record_step = {
@@ -3757,12 +4116,13 @@ async function run_tool_loop(config) {
3757
4116
  }
3758
4117
  const would_exceed_after = step_index + 1 >= config.max_steps;
3759
4118
  const tool_results_to_feed = [];
3760
- for (const raw_call of turn.tool_calls) {
4119
+ let terminal_fired = false;
4120
+ for (const raw_call of effective_calls) {
3761
4121
  throw_if_aborted_in_flight(config.abort, step_index, {
3762
4122
  id: raw_call.id,
3763
4123
  name: raw_call.name
3764
4124
  });
3765
- if (would_exceed_after) {
4125
+ if (would_exceed_after && tool_map.get(raw_call.name)?.ends_turn !== true) {
3766
4126
  const record = {
3767
4127
  id: raw_call.id,
3768
4128
  name: raw_call.name,
@@ -3952,6 +4312,7 @@ async function run_tool_loop(config) {
3952
4312
  };
3953
4313
  step_tool_records.push(record);
3954
4314
  tool_calls_all.push(record);
4315
+ if (tool.ends_turn === true) terminal_fired = true;
3955
4316
  tool_results_to_feed.push(build_tool_result_message(raw_call.id, tool.name, output ?? ""));
3956
4317
  record_tool_call(config.trajectory, {
3957
4318
  step_index,
@@ -3962,6 +4323,26 @@ async function run_tool_loop(config) {
3962
4323
  });
3963
4324
  await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, output);
3964
4325
  }
4326
+ for (const d of dropped_calls) {
4327
+ const record = {
4328
+ id: d.id,
4329
+ name: d.name,
4330
+ input: d.input,
4331
+ error: { message: "dropped_max_tool_calls_per_step" },
4332
+ duration_ms: 0,
4333
+ started_at: Date.now()
4334
+ };
4335
+ step_tool_records.push(record);
4336
+ tool_calls_all.push(record);
4337
+ await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, d.id, void 0, { message: "dropped_max_tool_calls_per_step" });
4338
+ }
4339
+ for (const r of step_tool_records) {
4340
+ const format = salvaged_formats.get(r.id);
4341
+ if (format !== void 0) {
4342
+ r.salvaged = true;
4343
+ r.salvaged_format = format;
4344
+ }
4345
+ }
3965
4346
  for (const r of step_tool_records) record_tool_result(config.trajectory, {
3966
4347
  step_index,
3967
4348
  name: r.name,
@@ -3970,7 +4351,7 @@ async function run_tool_loop(config) {
3970
4351
  ...r.error !== void 0 ? { error: r.error } : { output: r.output }
3971
4352
  });
3972
4353
  for (const m of tool_results_to_feed) config.messages.push(m);
3973
- const turn_finish_reason = would_exceed_after ? "max_steps" : turn.finish_reason;
4354
+ const turn_finish_reason = terminal_fired ? "tool_calls" : would_exceed_after ? "max_steps" : salvage !== void 0 ? "tool_calls" : turn.finish_reason;
3974
4355
  const step_record = {
3975
4356
  index: step_index,
3976
4357
  text: turn.text,
@@ -3986,6 +4367,11 @@ async function run_tool_loop(config) {
3986
4367
  usage: turn.usage,
3987
4368
  finish_reason: turn_finish_reason
3988
4369
  });
4370
+ if (terminal_fired) {
4371
+ finish_reason = "stop";
4372
+ text = turn.text;
4373
+ break;
4374
+ }
3989
4375
  if (would_exceed_after) {
3990
4376
  max_steps_reached = true;
3991
4377
  finish_reason = "max_steps";
@@ -4399,6 +4785,10 @@ async function generate(opts_in, engine) {
4399
4785
  const max_steps = opts.max_steps ?? engine.default_max_steps;
4400
4786
  const tool_error_policy = opts.tool_error_policy ?? engine.default_tool_error_policy ?? "feed_back";
4401
4787
  const schema_repair_attempts = opts.schema_repair_attempts ?? engine.default_schema_repair_attempts ?? 1;
4788
+ const tool_call_repair_attempts = opts.tool_call_repair_attempts ?? engine.default_tool_call_repair_attempts ?? 0;
4789
+ if (tool_call_repair_attempts < 0) throw new engine_config_error("tool_call_repair_attempts must be >= 0");
4790
+ const max_tool_calls_per_step = opts.max_tool_calls_per_step ?? engine.default_max_tool_calls_per_step;
4791
+ 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");
4402
4792
  const retry_policy = opts.retry ?? engine.default_retry;
4403
4793
  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;
4404
4794
  const initial_messages = build_initial_messages(opts);
@@ -4450,7 +4840,8 @@ async function generate(opts_in, engine) {
4450
4840
  model,
4451
4841
  messages: sdk_messages,
4452
4842
  abortSignal: internal_controller.signal,
4453
- stopWhen: stepCountIs(1)
4843
+ stopWhen: stepCountIs(1),
4844
+ maxRetries: 0
4454
4845
  };
4455
4846
  if (hoisted_system !== void 0) base_params.system = hoisted_system;
4456
4847
  if (sdk_tools !== void 0) base_params.tools = sdk_tools;
@@ -4495,6 +4886,7 @@ async function generate(opts_in, engine) {
4495
4886
  let repair_remaining = schema_repair_attempts;
4496
4887
  let content_parsed;
4497
4888
  let schema_satisfied = opts.schema === void 0;
4889
+ const salvage_budget = tool_call_repair_attempts > 0 ? { remaining: tool_call_repair_attempts } : void 0;
4498
4890
  try {
4499
4891
  while (true) {
4500
4892
  const loop_result = await run_tool_loop({
@@ -4512,7 +4904,9 @@ async function generate(opts_in, engine) {
4512
4904
  provider: target.provider,
4513
4905
  model_id: target.model_id,
4514
4906
  resolve_pricing,
4515
- pricing_dedup
4907
+ pricing_dedup,
4908
+ ...salvage_budget !== void 0 ? { salvage_budget } : {},
4909
+ ...max_tool_calls_per_step !== void 0 ? { max_tool_calls_per_step } : {}
4516
4910
  });
4517
4911
  for (const s of loop_result.steps) steps_accum.push(s);
4518
4912
  for (const tc of loop_result.tool_calls) tool_calls_accum.push(tc);
@@ -4645,6 +5039,8 @@ function create_engine(config) {
4645
5039
  const default_retry = defaults?.retry_policy ?? config.default_retry ?? DEFAULT_RETRY;
4646
5040
  const default_effort = defaults?.effort ?? config.default_effort ?? "none";
4647
5041
  const default_max_steps = defaults?.max_steps ?? config.default_max_steps ?? 10;
5042
+ 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");
5043
+ 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");
4648
5044
  const get_internals = () => ({
4649
5045
  pricing,
4650
5046
  adapters,
@@ -4656,6 +5052,8 @@ function create_engine(config) {
4656
5052
  ...defaults?.system !== void 0 ? { default_system: defaults.system } : {},
4657
5053
  ...defaults?.tool_error_policy !== void 0 ? { default_tool_error_policy: defaults.tool_error_policy } : {},
4658
5054
  ...defaults?.schema_repair_attempts !== void 0 ? { default_schema_repair_attempts: defaults.schema_repair_attempts } : {},
5055
+ ...defaults?.tool_call_repair_attempts !== void 0 ? { default_tool_call_repair_attempts: defaults.tool_call_repair_attempts } : {},
5056
+ ...defaults?.max_tool_calls_per_step !== void 0 ? { default_max_tool_calls_per_step: defaults.max_tool_calls_per_step } : {},
4659
5057
  ...defaults?.provider_options !== void 0 ? { default_provider_options: defaults.provider_options } : {}
4660
5058
  });
4661
5059
  let disposed = false;
@@ -4787,9 +5185,11 @@ function model_call(cfg) {
4787
5185
  if (cfg.retry_policy !== void 0) opts.retry = cfg.retry_policy;
4788
5186
  if (cfg.tool_error_policy !== void 0) opts.tool_error_policy = cfg.tool_error_policy;
4789
5187
  if (cfg.schema_repair_attempts !== void 0) opts.schema_repair_attempts = cfg.schema_repair_attempts;
5188
+ if (cfg.tool_call_repair_attempts !== void 0) opts.tool_call_repair_attempts = cfg.tool_call_repair_attempts;
5189
+ if (cfg.max_tool_calls_per_step !== void 0) opts.max_tool_calls_per_step = cfg.max_tool_calls_per_step;
4790
5190
  if (cfg.on_tool_approval !== void 0) opts.on_tool_approval = cfg.on_tool_approval;
4791
5191
  if (ctx.streaming) opts.on_chunk = (chunk) => {
4792
- ctx.emit({
5192
+ ctx.trajectory.record({
4793
5193
  kind: "model_chunk",
4794
5194
  step_id,
4795
5195
  chunk