fascicle 0.8.11 → 0.8.13
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/CHANGELOG.md +15 -0
- package/dist/{core-dRSAN1Q2.js → core-BhmclJzj.js} +20 -7
- package/dist/core-BhmclJzj.js.map +1 -0
- package/dist/{index-Cy7EUeiP.d.ts → index-C3NcdaSY.d.ts} +25 -2
- package/dist/index.d.ts +3 -1
- package/dist/index.js +400 -8
- package/dist/index.js.map +1 -1
- package/dist/mcp.d.ts +1 -1
- package/dist/mcp.js +1 -1
- package/dist/stdio.js +1 -1
- package/package.json +1 -1
- package/dist/core-dRSAN1Q2.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { A as timeout_error, C as describe, D as describe_cycle_error, E as aborted_error, O as resume_validation_error, S as sequence, T as run, _ as retry, a as trajectory_event_schema, b as branch, c as scope, d as suspend, f as checkpoint, g as fallback, h as timeout, i as span_start_event_schema, j as flow_schema_default, k as suspended_error, l as stash, m as loop, n as emit_event_schema, o as STEP_KINDS, p as compose, r as span_end_event_schema, s as is_step_kind, t as custom_event_schema, u as use, v as pipe, w as step, x as parallel, y as map } from "./core-
|
|
1
|
+
import { A as timeout_error, C as describe, D as describe_cycle_error, E as aborted_error, O as resume_validation_error, S as sequence, T as run, _ as retry, a as trajectory_event_schema, b as branch, c as scope, d as suspend, f as checkpoint, g as fallback, h as timeout, i as span_start_event_schema, j as flow_schema_default, k as suspended_error, l as stash, m as loop, n as emit_event_schema, o as STEP_KINDS, p as compose, r as span_end_event_schema, s as is_step_kind, t as custom_event_schema, u as use, v as pipe, w as step, x as parallel, y as map } from "./core-BhmclJzj.js";
|
|
2
2
|
import { a as http_logger, n as tee_logger, o as filesystem_logger } from "./adapters-DdUx-_b3.js";
|
|
3
3
|
import { createHash } from "node:crypto";
|
|
4
4
|
import { z } from "zod";
|
|
@@ -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(
|
|
4095
|
+
const assistant_message = build_assistant_message(history_text, effective_calls);
|
|
3737
4096
|
config.messages.push(assistant_message);
|
|
3738
|
-
if (
|
|
4097
|
+
if (effective_calls.length === 0) {
|
|
3739
4098
|
text = turn.text;
|
|
3740
4099
|
finish_reason = turn.finish_reason;
|
|
3741
4100
|
const record_step = {
|
|
@@ -3757,7 +4116,7 @@ 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
|
|
4119
|
+
for (const raw_call of effective_calls) {
|
|
3761
4120
|
throw_if_aborted_in_flight(config.abort, step_index, {
|
|
3762
4121
|
id: raw_call.id,
|
|
3763
4122
|
name: raw_call.name
|
|
@@ -3962,6 +4321,26 @@ async function run_tool_loop(config) {
|
|
|
3962
4321
|
});
|
|
3963
4322
|
await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, output);
|
|
3964
4323
|
}
|
|
4324
|
+
for (const d of dropped_calls) {
|
|
4325
|
+
const record = {
|
|
4326
|
+
id: d.id,
|
|
4327
|
+
name: d.name,
|
|
4328
|
+
input: d.input,
|
|
4329
|
+
error: { message: "dropped_max_tool_calls_per_step" },
|
|
4330
|
+
duration_ms: 0,
|
|
4331
|
+
started_at: Date.now()
|
|
4332
|
+
};
|
|
4333
|
+
step_tool_records.push(record);
|
|
4334
|
+
tool_calls_all.push(record);
|
|
4335
|
+
await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, d.id, void 0, { message: "dropped_max_tool_calls_per_step" });
|
|
4336
|
+
}
|
|
4337
|
+
for (const r of step_tool_records) {
|
|
4338
|
+
const format = salvaged_formats.get(r.id);
|
|
4339
|
+
if (format !== void 0) {
|
|
4340
|
+
r.salvaged = true;
|
|
4341
|
+
r.salvaged_format = format;
|
|
4342
|
+
}
|
|
4343
|
+
}
|
|
3965
4344
|
for (const r of step_tool_records) record_tool_result(config.trajectory, {
|
|
3966
4345
|
step_index,
|
|
3967
4346
|
name: r.name,
|
|
@@ -3970,7 +4349,7 @@ async function run_tool_loop(config) {
|
|
|
3970
4349
|
...r.error !== void 0 ? { error: r.error } : { output: r.output }
|
|
3971
4350
|
});
|
|
3972
4351
|
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;
|
|
4352
|
+
const turn_finish_reason = would_exceed_after ? "max_steps" : salvage !== void 0 ? "tool_calls" : turn.finish_reason;
|
|
3974
4353
|
const step_record = {
|
|
3975
4354
|
index: step_index,
|
|
3976
4355
|
text: turn.text,
|
|
@@ -4399,6 +4778,10 @@ async function generate(opts_in, engine) {
|
|
|
4399
4778
|
const max_steps = opts.max_steps ?? engine.default_max_steps;
|
|
4400
4779
|
const tool_error_policy = opts.tool_error_policy ?? engine.default_tool_error_policy ?? "feed_back";
|
|
4401
4780
|
const schema_repair_attempts = opts.schema_repair_attempts ?? engine.default_schema_repair_attempts ?? 1;
|
|
4781
|
+
const tool_call_repair_attempts = opts.tool_call_repair_attempts ?? engine.default_tool_call_repair_attempts ?? 0;
|
|
4782
|
+
if (tool_call_repair_attempts < 0) throw new engine_config_error("tool_call_repair_attempts must be >= 0");
|
|
4783
|
+
const max_tool_calls_per_step = opts.max_tool_calls_per_step ?? engine.default_max_tool_calls_per_step;
|
|
4784
|
+
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
4785
|
const retry_policy = opts.retry ?? engine.default_retry;
|
|
4403
4786
|
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
4787
|
const initial_messages = build_initial_messages(opts);
|
|
@@ -4495,6 +4878,7 @@ async function generate(opts_in, engine) {
|
|
|
4495
4878
|
let repair_remaining = schema_repair_attempts;
|
|
4496
4879
|
let content_parsed;
|
|
4497
4880
|
let schema_satisfied = opts.schema === void 0;
|
|
4881
|
+
const salvage_budget = tool_call_repair_attempts > 0 ? { remaining: tool_call_repair_attempts } : void 0;
|
|
4498
4882
|
try {
|
|
4499
4883
|
while (true) {
|
|
4500
4884
|
const loop_result = await run_tool_loop({
|
|
@@ -4512,7 +4896,9 @@ async function generate(opts_in, engine) {
|
|
|
4512
4896
|
provider: target.provider,
|
|
4513
4897
|
model_id: target.model_id,
|
|
4514
4898
|
resolve_pricing,
|
|
4515
|
-
pricing_dedup
|
|
4899
|
+
pricing_dedup,
|
|
4900
|
+
...salvage_budget !== void 0 ? { salvage_budget } : {},
|
|
4901
|
+
...max_tool_calls_per_step !== void 0 ? { max_tool_calls_per_step } : {}
|
|
4516
4902
|
});
|
|
4517
4903
|
for (const s of loop_result.steps) steps_accum.push(s);
|
|
4518
4904
|
for (const tc of loop_result.tool_calls) tool_calls_accum.push(tc);
|
|
@@ -4645,6 +5031,8 @@ function create_engine(config) {
|
|
|
4645
5031
|
const default_retry = defaults?.retry_policy ?? config.default_retry ?? DEFAULT_RETRY;
|
|
4646
5032
|
const default_effort = defaults?.effort ?? config.default_effort ?? "none";
|
|
4647
5033
|
const default_max_steps = defaults?.max_steps ?? config.default_max_steps ?? 10;
|
|
5034
|
+
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");
|
|
5035
|
+
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
5036
|
const get_internals = () => ({
|
|
4649
5037
|
pricing,
|
|
4650
5038
|
adapters,
|
|
@@ -4656,6 +5044,8 @@ function create_engine(config) {
|
|
|
4656
5044
|
...defaults?.system !== void 0 ? { default_system: defaults.system } : {},
|
|
4657
5045
|
...defaults?.tool_error_policy !== void 0 ? { default_tool_error_policy: defaults.tool_error_policy } : {},
|
|
4658
5046
|
...defaults?.schema_repair_attempts !== void 0 ? { default_schema_repair_attempts: defaults.schema_repair_attempts } : {},
|
|
5047
|
+
...defaults?.tool_call_repair_attempts !== void 0 ? { default_tool_call_repair_attempts: defaults.tool_call_repair_attempts } : {},
|
|
5048
|
+
...defaults?.max_tool_calls_per_step !== void 0 ? { default_max_tool_calls_per_step: defaults.max_tool_calls_per_step } : {},
|
|
4659
5049
|
...defaults?.provider_options !== void 0 ? { default_provider_options: defaults.provider_options } : {}
|
|
4660
5050
|
});
|
|
4661
5051
|
let disposed = false;
|
|
@@ -4787,6 +5177,8 @@ function model_call(cfg) {
|
|
|
4787
5177
|
if (cfg.retry_policy !== void 0) opts.retry = cfg.retry_policy;
|
|
4788
5178
|
if (cfg.tool_error_policy !== void 0) opts.tool_error_policy = cfg.tool_error_policy;
|
|
4789
5179
|
if (cfg.schema_repair_attempts !== void 0) opts.schema_repair_attempts = cfg.schema_repair_attempts;
|
|
5180
|
+
if (cfg.tool_call_repair_attempts !== void 0) opts.tool_call_repair_attempts = cfg.tool_call_repair_attempts;
|
|
5181
|
+
if (cfg.max_tool_calls_per_step !== void 0) opts.max_tool_calls_per_step = cfg.max_tool_calls_per_step;
|
|
4790
5182
|
if (cfg.on_tool_approval !== void 0) opts.on_tool_approval = cfg.on_tool_approval;
|
|
4791
5183
|
if (ctx.streaming) opts.on_chunk = (chunk) => {
|
|
4792
5184
|
ctx.emit({
|