fascicle 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +43 -0
- package/README.md +26 -27
- package/dist/{adapters-CJnu1687.js → adapters-hmnJOOgf.js} +30 -64
- package/dist/adapters-hmnJOOgf.js.map +1 -0
- package/dist/adapters.d.ts +1 -1
- package/dist/adapters.js +1 -1
- package/dist/agents.d.ts +2 -2
- package/dist/agents.js +51 -29
- package/dist/agents.js.map +1 -1
- package/dist/{core-Ww6An4CS.js → core-BkB2l1-e.js} +438 -120
- package/dist/core-BkB2l1-e.js.map +1 -0
- package/dist/index.d.ts +258 -99
- package/dist/index.js +2375 -1720
- package/dist/index.js.map +1 -1
- package/dist/{invoke-CWjm1Y_R.js → invoke-JkzNf7-Y.js} +101 -66
- package/dist/invoke-JkzNf7-Y.js.map +1 -0
- package/dist/{types-NnSvmpjg.js → leading_system-BVtGMweY.js} +57 -3
- package/dist/leading_system-BVtGMweY.js.map +1 -0
- package/dist/mcp.d.ts +2 -2
- package/dist/mcp.js +1 -1
- package/dist/otel.d.ts +1 -1
- package/dist/stdio.d.ts +1 -1
- package/dist/stdio.js +2 -2
- package/dist/testing.d.ts +39 -0
- package/dist/testing.js +90 -0
- package/dist/testing.js.map +1 -0
- package/dist/{types-B3HIsBBD.d.ts → types-4l54cexm.d.ts} +2 -2
- package/dist/{types-BDSbSUAO.d.ts → types-CVdTBXE_.d.ts} +13 -4
- package/dist/ui.d.ts +1 -1
- package/package.json +7 -2
- package/dist/adapters-CJnu1687.js.map +0 -1
- package/dist/core-Ww6An4CS.js.map +0 -1
- package/dist/invoke-CWjm1Y_R.js.map +0 -1
- package/dist/types-NnSvmpjg.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { A as
|
|
2
|
-
import { a as http_logger, n as tee_logger, o as filesystem_logger } from "./adapters-
|
|
3
|
-
import { _ as
|
|
1
|
+
import { A as run, C as pipe, D as sequence, E as parallel, F as timeout_error, I as flow_schema_default, M as describe_cycle_error, N as resume_validation_error, O as describe, P as suspended_error, S as wait_with_abort, T as branch, _ as loop, a as parse_trajectory_event, b as retry, c as chain, d as use, f as suspend, g as compose, h as checkpoint, i as is_span_start_event, j as aborted_error, k as step, l as scope, m as validate_schema, n as is_emit_event, o as STEP_KINDS, p as format_schema_issues, r as is_span_end_event, s as is_step_kind, t as is_custom_trajectory_event, u as stash, v as timeout, w as map, x as compute_backoff, y as fallback } from "./core-BkB2l1-e.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-BVtGMweY.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";
|
|
@@ -180,7 +180,9 @@ function build_input_from_state(s) {
|
|
|
180
180
|
* Each round builds a candidate (receiving the prior candidate and critique
|
|
181
181
|
* notes when available), critiques it, then checks `accept(critique_result)`.
|
|
182
182
|
* Returns `{ candidate, converged, rounds }`. Does not throw on
|
|
183
|
-
* non-convergence; `converged: false` reports it instead.
|
|
183
|
+
* non-convergence; `converged: false` reports it instead. `project` maps
|
|
184
|
+
* that envelope into the step's output (e.g. `(r) => r.candidate`); omitted,
|
|
185
|
+
* the envelope itself is the output.
|
|
184
186
|
*
|
|
185
187
|
* Implemented as a `compose`d `loop` whose body builds a new candidate and
|
|
186
188
|
* whose guard runs the critique step and checks `accept`. State is threaded
|
|
@@ -189,6 +191,7 @@ function build_input_from_state(s) {
|
|
|
189
191
|
*/
|
|
190
192
|
function adversarial(config) {
|
|
191
193
|
const { build, critique, accept, max_rounds } = config;
|
|
194
|
+
const project = config.project ?? ((r) => r);
|
|
192
195
|
const inner = loop({
|
|
193
196
|
init: (input) => ({ input }),
|
|
194
197
|
body: scope([
|
|
@@ -223,11 +226,11 @@ function adversarial(config) {
|
|
|
223
226
|
]),
|
|
224
227
|
finish: (s, { converged, rounds }) => {
|
|
225
228
|
if (s.candidate === void 0) throw new Error("adversarial: finished without a candidate");
|
|
226
|
-
return {
|
|
229
|
+
return project({
|
|
227
230
|
candidate: s.candidate,
|
|
228
231
|
converged,
|
|
229
232
|
rounds
|
|
230
|
-
};
|
|
233
|
+
});
|
|
231
234
|
},
|
|
232
235
|
max_rounds
|
|
233
236
|
});
|
|
@@ -245,6 +248,8 @@ function adversarial(config) {
|
|
|
245
248
|
* On agreement, returns the round's results with `converged: true`.
|
|
246
249
|
* Otherwise re-runs all members up to `max_rounds` times and returns the
|
|
247
250
|
* last results with `converged: false` if no agreement is reached.
|
|
251
|
+
* `project` maps the `{ result, converged }` envelope into the step's output
|
|
252
|
+
* (e.g. `(r) => r.converged`); omitted, the envelope itself is the output.
|
|
248
253
|
*
|
|
249
254
|
* Implemented as a `compose`d `loop` whose body runs `parallel(members)` and
|
|
250
255
|
* whose guard evaluates `agree`. State carries the original input alongside
|
|
@@ -252,6 +257,7 @@ function adversarial(config) {
|
|
|
252
257
|
*/
|
|
253
258
|
function consensus(config) {
|
|
254
259
|
const { members, agree, max_rounds } = config;
|
|
260
|
+
const project = config.project ?? ((r) => r);
|
|
255
261
|
const round_step = parallel(members);
|
|
256
262
|
const inner = loop({
|
|
257
263
|
init: (input) => ({
|
|
@@ -273,7 +279,7 @@ function consensus(config) {
|
|
|
273
279
|
stop: agree(s.results),
|
|
274
280
|
state: s
|
|
275
281
|
})),
|
|
276
|
-
finish: (s, { converged }) => ({
|
|
282
|
+
finish: (s, { converged }) => project({
|
|
277
283
|
result: s.results,
|
|
278
284
|
converged
|
|
279
285
|
}),
|
|
@@ -292,7 +298,9 @@ function consensus(config) {
|
|
|
292
298
|
*
|
|
293
299
|
* `select` picks the highest (`'max'`, the default) or lowest (`'min'`)
|
|
294
300
|
* score. Tie-breaking is "any tied result is acceptable": the first member
|
|
295
|
-
* (in `members` key order) holding the best score wins.
|
|
301
|
+
* (in `members` key order) holding the best score wins. `project` maps the
|
|
302
|
+
* `{ winner, scores }` envelope into the step's output (e.g. `(r) =>
|
|
303
|
+
* r.winner`); omitted, the envelope itself is the output.
|
|
296
304
|
*
|
|
297
305
|
* Implemented as a `compose`d `sequence` of `parallel(members)` followed by
|
|
298
306
|
* a single picking step. Cancellation, fan-out, and abort propagation come
|
|
@@ -302,6 +310,7 @@ function ensemble(config) {
|
|
|
302
310
|
const select = config.select ?? "max";
|
|
303
311
|
const score_fn = config.score;
|
|
304
312
|
const keys = Object.keys(config.members);
|
|
313
|
+
const project = config.project ?? ((r) => r);
|
|
305
314
|
const inner = sequence([parallel(config.members), step("pick_winner", async (results) => {
|
|
306
315
|
const scores = {};
|
|
307
316
|
for (const k of keys) {
|
|
@@ -327,10 +336,10 @@ function ensemble(config) {
|
|
|
327
336
|
if (winner_key === void 0) throw new Error("ensemble: no members produced a result");
|
|
328
337
|
const winner = results[winner_key];
|
|
329
338
|
if (winner === void 0) throw new Error("ensemble: winner missing from results");
|
|
330
|
-
return {
|
|
339
|
+
return project({
|
|
331
340
|
winner,
|
|
332
341
|
scores
|
|
333
|
-
};
|
|
342
|
+
});
|
|
334
343
|
})]);
|
|
335
344
|
return compose(config.name ?? "ensemble", inner);
|
|
336
345
|
}
|
|
@@ -353,7 +362,9 @@ const ITEM_KEY = "__ensemble_step_item";
|
|
|
353
362
|
*
|
|
354
363
|
* The result carries `winner_scored`, the structured output from the round's
|
|
355
364
|
* scoring run preserved verbatim, so callers never pay to re-score the
|
|
356
|
-
* winner. `scored` holds the structured score for every member.
|
|
365
|
+
* winner. `scored` holds the structured score for every member. `project`
|
|
366
|
+
* maps that envelope into the step's output (e.g. `(r) => r.winner`);
|
|
367
|
+
* omitted, the envelope itself is the output.
|
|
357
368
|
*
|
|
358
369
|
* Implemented as a `compose`d `scope` over (`stash(parallel(members))` ->
|
|
359
370
|
* `to_pairs` -> `map(score per result, threading id)` -> `use` to pick the
|
|
@@ -364,6 +375,7 @@ function ensemble_step(config) {
|
|
|
364
375
|
const { members, score, rank_by } = config;
|
|
365
376
|
const select = config.select ?? "max";
|
|
366
377
|
const keys = Object.keys(members);
|
|
378
|
+
const project = config.project ?? ((r) => r);
|
|
367
379
|
if (keys.length === 0) throw new Error("ensemble_step: at least one member required");
|
|
368
380
|
const fan_out = parallel(members);
|
|
369
381
|
const score_one = scope([
|
|
@@ -407,12 +419,12 @@ function ensemble_step(config) {
|
|
|
407
419
|
if (winner_id === void 0 || winner_scored === void 0) throw new Error("ensemble_step: no members produced a result");
|
|
408
420
|
const winner = results[winner_id];
|
|
409
421
|
if (winner === void 0) throw new Error("ensemble_step: winner missing from results");
|
|
410
|
-
return {
|
|
422
|
+
return project({
|
|
411
423
|
winner_id,
|
|
412
424
|
winner,
|
|
413
425
|
winner_scored,
|
|
414
426
|
scored
|
|
415
|
-
};
|
|
427
|
+
});
|
|
416
428
|
});
|
|
417
429
|
const inner = scope([
|
|
418
430
|
stash(RESULTS_KEY, fan_out),
|
|
@@ -444,7 +456,9 @@ const PRIOR_KEY = "__improve_prior";
|
|
|
444
456
|
* Rejected rounds with a `reason` feed a bounded lessons list back into the
|
|
445
457
|
* next round's input. Plateau detection stops the loop early when no
|
|
446
458
|
* progress has been made for `patience` rounds; an optional wall-clock
|
|
447
|
-
* budget caps total runtime.
|
|
459
|
+
* budget caps total runtime. `project` maps the result envelope into the
|
|
460
|
+
* step's output (e.g. `(r) => r.best.content`); omitted, the envelope
|
|
461
|
+
* itself is the output.
|
|
448
462
|
*
|
|
449
463
|
* Implemented as a `compose`d `scope` of (seed) -> (init state) -> (loop) ->
|
|
450
464
|
* (unwrap). The loop body is itself a small `scope` that snapshots prior
|
|
@@ -454,6 +468,7 @@ const PRIOR_KEY = "__improve_prior";
|
|
|
454
468
|
*/
|
|
455
469
|
function improve(config) {
|
|
456
470
|
const { seed, propose, score, budget } = config;
|
|
471
|
+
const project = config.project ?? ((r) => r);
|
|
457
472
|
const epsilon = config.epsilon ?? 0;
|
|
458
473
|
const proposers_per_round = Math.max(1, config.proposers_per_round ?? 1);
|
|
459
474
|
const lessons_capacity = Math.max(0, config.lessons_capacity ?? 5);
|
|
@@ -596,7 +611,7 @@ function improve(config) {
|
|
|
596
611
|
rounds_used: result.rounds_used,
|
|
597
612
|
final_score: result.best.score
|
|
598
613
|
});
|
|
599
|
-
return result;
|
|
614
|
+
return project(result);
|
|
600
615
|
})
|
|
601
616
|
]);
|
|
602
617
|
return compose(config.name ?? "improve", inner);
|
|
@@ -794,11 +809,14 @@ function learn(config) {
|
|
|
794
809
|
*
|
|
795
810
|
* All members run concurrently via `parallel`; the bracket step then reduces
|
|
796
811
|
* survivors round by round with `compare`, recording every match. Requires at
|
|
797
|
-
* least one member so a winner always exists.
|
|
812
|
+
* least one member so a winner always exists. `project` maps the `{ winner,
|
|
813
|
+
* bracket }` envelope into the step's output (e.g. `(r) => r.winner`);
|
|
814
|
+
* omitted, the envelope itself is the output.
|
|
798
815
|
*/
|
|
799
816
|
function tournament(config) {
|
|
800
817
|
const compare_fn = config.compare;
|
|
801
818
|
const keys = Object.keys(config.members);
|
|
819
|
+
const project = config.project ?? ((r) => r);
|
|
802
820
|
if (keys.length === 0) throw new Error("tournament: at least one member required");
|
|
803
821
|
const inner = sequence([parallel(config.members), step("bracket", async (results) => {
|
|
804
822
|
let current = [...keys];
|
|
@@ -806,40 +824,55 @@ function tournament(config) {
|
|
|
806
824
|
let round = 0;
|
|
807
825
|
while (current.length > 1) {
|
|
808
826
|
round += 1;
|
|
809
|
-
|
|
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;
|
|
827
|
+
current = await play_round(current, results, compare_fn, round, bracket);
|
|
831
828
|
}
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
const winner = results[final_id];
|
|
835
|
-
if (winner === void 0) throw new Error("tournament: winner missing from results");
|
|
836
|
-
return {
|
|
837
|
-
winner,
|
|
829
|
+
return project({
|
|
830
|
+
winner: winner_of(current, results),
|
|
838
831
|
bracket
|
|
839
|
-
};
|
|
832
|
+
});
|
|
840
833
|
})]);
|
|
841
834
|
return compose(config.name ?? "tournament", inner);
|
|
842
835
|
}
|
|
836
|
+
/**
|
|
837
|
+
* Plays one bracket round: pairs survivors two at a time, comparing each pair
|
|
838
|
+
* and recording a `BracketRecord`, and returns the ids advancing. An unpaired
|
|
839
|
+
* trailing survivor takes a bye. Every paired id must have a settled result.
|
|
840
|
+
*/
|
|
841
|
+
async function play_round(current, results, compare, round, bracket) {
|
|
842
|
+
const next_round = [];
|
|
843
|
+
for (let i = 0; i < current.length; i += 2) {
|
|
844
|
+
const a_id = current[i];
|
|
845
|
+
const b_id = current[i + 1];
|
|
846
|
+
if (a_id === void 0) continue;
|
|
847
|
+
if (b_id === void 0) {
|
|
848
|
+
next_round.push(a_id);
|
|
849
|
+
continue;
|
|
850
|
+
}
|
|
851
|
+
const a_val = results[a_id];
|
|
852
|
+
const b_val = results[b_id];
|
|
853
|
+
if (a_val === void 0 || b_val === void 0) throw new Error("tournament: missing result for match");
|
|
854
|
+
const winner_id = await compare(a_val, b_val) === "a" ? a_id : b_id;
|
|
855
|
+
bracket.push({
|
|
856
|
+
round,
|
|
857
|
+
a_id,
|
|
858
|
+
b_id,
|
|
859
|
+
winner_id
|
|
860
|
+
});
|
|
861
|
+
next_round.push(winner_id);
|
|
862
|
+
}
|
|
863
|
+
return next_round;
|
|
864
|
+
}
|
|
865
|
+
/**
|
|
866
|
+
* Extracts the tournament winner from the single surviving id, throwing when
|
|
867
|
+
* no survivor remains or its result is missing.
|
|
868
|
+
*/
|
|
869
|
+
function winner_of(current, results) {
|
|
870
|
+
const final_id = current[0];
|
|
871
|
+
if (final_id === void 0) throw new Error("tournament: no winner");
|
|
872
|
+
const winner = results[final_id];
|
|
873
|
+
if (winner === void 0) throw new Error("tournament: winner missing from results");
|
|
874
|
+
return winner;
|
|
875
|
+
}
|
|
843
876
|
//#endregion
|
|
844
877
|
//#region src/composites/errors.ts
|
|
845
878
|
/**
|
|
@@ -905,12 +938,12 @@ async function bench(flow, cases, judges, options = {}) {
|
|
|
905
938
|
if (options.trajectory_dir !== void 0) await mkdir(options.trajectory_dir, { recursive: true });
|
|
906
939
|
const judge_entries = Object.entries(judges);
|
|
907
940
|
const run_one = async (bc) => {
|
|
908
|
-
const trajectory_path
|
|
909
|
-
const
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
941
|
+
const { trajectory_path, case_logger, cost_tracker } = build_case_logging(options, bc.id);
|
|
942
|
+
const ctx = {
|
|
943
|
+
case_logger,
|
|
944
|
+
install_signal_handlers,
|
|
945
|
+
run_options
|
|
946
|
+
};
|
|
914
947
|
const start = Date.now();
|
|
915
948
|
let output;
|
|
916
949
|
let error;
|
|
@@ -927,33 +960,7 @@ async function bench(flow, cases, judges, options = {}) {
|
|
|
927
960
|
error = err instanceof Error ? err.message : String(err);
|
|
928
961
|
ok = false;
|
|
929
962
|
}
|
|
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
|
-
}
|
|
963
|
+
const scores = ok && output !== void 0 ? await run_judges(judge_entries, build_judge_input(bc, output), ctx, bc.id) : {};
|
|
957
964
|
const duration_ms = Date.now() - start;
|
|
958
965
|
const result = {
|
|
959
966
|
case_id: bc.id,
|
|
@@ -978,6 +985,65 @@ async function bench(flow, cases, judges, options = {}) {
|
|
|
978
985
|
};
|
|
979
986
|
}
|
|
980
987
|
/**
|
|
988
|
+
* Builds the per-case trajectory pipeline: an optional filesystem sink when
|
|
989
|
+
* `trajectory_dir` is set, an optional HTTP sink when `live_url` is set, and
|
|
990
|
+
* always an in-process cost tracker, all tee'd together. Returns the resolved
|
|
991
|
+
* trajectory path (undefined when no dir is set) alongside the logger and
|
|
992
|
+
* tracker.
|
|
993
|
+
*/
|
|
994
|
+
function build_case_logging(options, case_id) {
|
|
995
|
+
const trajectory_path = options.trajectory_dir === void 0 ? void 0 : join(options.trajectory_dir, `${case_id}.jsonl`);
|
|
996
|
+
const sinks = [];
|
|
997
|
+
if (trajectory_path !== void 0) sinks.push(filesystem_logger({ output_path: trajectory_path }));
|
|
998
|
+
if (options.live_url !== void 0) sinks.push(http_logger({ url: options.live_url }));
|
|
999
|
+
const cost_tracker = create_cost_tracker();
|
|
1000
|
+
return {
|
|
1001
|
+
trajectory_path,
|
|
1002
|
+
case_logger: sinks.length === 0 ? cost_tracker.logger : tee_logger(...sinks, cost_tracker.logger),
|
|
1003
|
+
cost_tracker
|
|
1004
|
+
};
|
|
1005
|
+
}
|
|
1006
|
+
/**
|
|
1007
|
+
* Assembles the judge input triple, including `meta` only when the case
|
|
1008
|
+
* carries it so the optional field stays absent rather than `undefined`.
|
|
1009
|
+
*/
|
|
1010
|
+
function build_judge_input(bc, output) {
|
|
1011
|
+
return bc.meta === void 0 ? {
|
|
1012
|
+
input: bc.input,
|
|
1013
|
+
output
|
|
1014
|
+
} : {
|
|
1015
|
+
input: bc.input,
|
|
1016
|
+
output,
|
|
1017
|
+
meta: bc.meta
|
|
1018
|
+
};
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* Runs each judge as a Step over the case's input/output, collecting the
|
|
1022
|
+
* normalized scores. A judge that throws a control-flow signal rethrows via
|
|
1023
|
+
* `rethrow_control_flow`; any other throw, or an abstaining `undefined`,
|
|
1024
|
+
* omits that judge from the scores rather than failing the case.
|
|
1025
|
+
*/
|
|
1026
|
+
async function run_judges(judge_entries, judge_input, ctx, case_id) {
|
|
1027
|
+
const scores = {};
|
|
1028
|
+
for (const [name, judge] of judge_entries) {
|
|
1029
|
+
let raw;
|
|
1030
|
+
try {
|
|
1031
|
+
raw = await run(judge, judge_input, {
|
|
1032
|
+
trajectory: ctx.case_logger,
|
|
1033
|
+
install_signal_handlers: ctx.install_signal_handlers,
|
|
1034
|
+
...ctx.run_options
|
|
1035
|
+
});
|
|
1036
|
+
} catch (err) {
|
|
1037
|
+
rethrow_control_flow(err, case_id);
|
|
1038
|
+
continue;
|
|
1039
|
+
}
|
|
1040
|
+
const normalized = normalize_score(raw);
|
|
1041
|
+
if (normalized === void 0) continue;
|
|
1042
|
+
scores[name] = normalized;
|
|
1043
|
+
}
|
|
1044
|
+
return scores;
|
|
1045
|
+
}
|
|
1046
|
+
/**
|
|
981
1047
|
* Rethrows the control-flow signals that must never become a `CaseResult`.
|
|
982
1048
|
*
|
|
983
1049
|
* An `aborted_error` cancels the whole bench: a report claiming every
|
|
@@ -1181,13 +1247,25 @@ function is_record$1(v) {
|
|
|
1181
1247
|
*/
|
|
1182
1248
|
function deep_equal(a, b) {
|
|
1183
1249
|
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
|
-
}
|
|
1250
|
+
if (Array.isArray(a) || Array.isArray(b)) return arrays_equal(a, b);
|
|
1190
1251
|
if (!is_record$1(a) || !is_record$1(b)) return false;
|
|
1252
|
+
return records_equal(a, b);
|
|
1253
|
+
}
|
|
1254
|
+
/**
|
|
1255
|
+
* Element-wise deep equality once at least one side is an array: both must be
|
|
1256
|
+
* arrays of the same length with every index equal.
|
|
1257
|
+
*/
|
|
1258
|
+
function arrays_equal(a, b) {
|
|
1259
|
+
if (!Array.isArray(a) || !Array.isArray(b)) return false;
|
|
1260
|
+
if (a.length !== b.length) return false;
|
|
1261
|
+
for (let i = 0; i < a.length; i += 1) if (!deep_equal(a[i], b[i])) return false;
|
|
1262
|
+
return true;
|
|
1263
|
+
}
|
|
1264
|
+
/**
|
|
1265
|
+
* Key-by-key deep equality over two records: same sorted key sets, and each
|
|
1266
|
+
* value deep-equal under the shared key.
|
|
1267
|
+
*/
|
|
1268
|
+
function records_equal(a, b) {
|
|
1191
1269
|
const ak = Object.keys(a).toSorted();
|
|
1192
1270
|
const bk = Object.keys(b).toSorted();
|
|
1193
1271
|
if (ak.length !== bk.length) return false;
|
|
@@ -1393,16 +1471,8 @@ function compute_per_case(current, baseline, score_threshold, cost_threshold) {
|
|
|
1393
1471
|
for (const id of all_ids) {
|
|
1394
1472
|
const c = by_id_current.get(id);
|
|
1395
1473
|
const b = by_id_baseline.get(id);
|
|
1396
|
-
const score_deltas =
|
|
1397
|
-
|
|
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;
|
|
1474
|
+
const { score_deltas, any_regression } = compute_score_deltas(c, b, score_threshold);
|
|
1475
|
+
const { cost_delta, is_regression: cost_regression } = compute_cost_regression(c, b, cost_threshold);
|
|
1406
1476
|
const ok_regression = b?.ok === true && c?.ok !== true;
|
|
1407
1477
|
out.push({
|
|
1408
1478
|
case_id: id,
|
|
@@ -1410,12 +1480,46 @@ function compute_per_case(current, baseline, score_threshold, cost_threshold) {
|
|
|
1410
1480
|
...c !== void 0 ? { current: c } : {},
|
|
1411
1481
|
score_deltas,
|
|
1412
1482
|
cost_delta,
|
|
1413
|
-
is_regression:
|
|
1483
|
+
is_regression: any_regression || cost_regression || ok_regression
|
|
1414
1484
|
});
|
|
1415
1485
|
}
|
|
1416
1486
|
return out;
|
|
1417
1487
|
}
|
|
1418
1488
|
/**
|
|
1489
|
+
* Compute per-judge score deltas for one case pair across the union of judge
|
|
1490
|
+
* names on both sides (a missing side scores 0), flagging a regression when
|
|
1491
|
+
* any judge's score drops beyond the threshold.
|
|
1492
|
+
*/
|
|
1493
|
+
function compute_score_deltas(c, b, score_threshold) {
|
|
1494
|
+
const score_deltas = {};
|
|
1495
|
+
let any_regression = false;
|
|
1496
|
+
const judge_names = /* @__PURE__ */ new Set([...Object.keys(c?.scores ?? {}), ...Object.keys(b?.scores ?? {})]);
|
|
1497
|
+
for (const name of judge_names) {
|
|
1498
|
+
const delta = score_value(c?.scores[name]) - score_value(b?.scores[name]);
|
|
1499
|
+
score_deltas[name] = delta;
|
|
1500
|
+
if (delta < -score_threshold) any_regression = true;
|
|
1501
|
+
}
|
|
1502
|
+
return {
|
|
1503
|
+
score_deltas,
|
|
1504
|
+
any_regression
|
|
1505
|
+
};
|
|
1506
|
+
}
|
|
1507
|
+
/**
|
|
1508
|
+
* Compute the cost delta for one case pair and whether it regressed. With a
|
|
1509
|
+
* positive baseline cost, a relative rise beyond `cost_threshold` regresses;
|
|
1510
|
+
* with a zero baseline, any positive current cost regresses (unless the
|
|
1511
|
+
* threshold is Infinity). A missing side counts as 0.
|
|
1512
|
+
*/
|
|
1513
|
+
function compute_cost_regression(c, b, cost_threshold) {
|
|
1514
|
+
const baseline_cost = b?.cost_usd ?? 0;
|
|
1515
|
+
const current_cost = c?.cost_usd ?? 0;
|
|
1516
|
+
const cost_delta = current_cost - baseline_cost;
|
|
1517
|
+
return {
|
|
1518
|
+
cost_delta,
|
|
1519
|
+
is_regression: baseline_cost > 0 ? cost_delta / baseline_cost > cost_threshold : current_cost > 0 && baseline_cost === 0 && cost_threshold < Infinity
|
|
1520
|
+
};
|
|
1521
|
+
}
|
|
1522
|
+
/**
|
|
1419
1523
|
* Extract a finite numeric score from a raw score entry.
|
|
1420
1524
|
*
|
|
1421
1525
|
* Accepts a bare number or an object with a numeric `score` field; anything
|
|
@@ -1541,6 +1645,48 @@ function round6$1(value) {
|
|
|
1541
1645
|
return Math.round(value * 1e6) / 1e6;
|
|
1542
1646
|
}
|
|
1543
1647
|
/**
|
|
1648
|
+
* The breakdown for a turn whose model has no pricing entry.
|
|
1649
|
+
*
|
|
1650
|
+
* Free providers (ollama, lmstudio) still report an all-zero, estimated
|
|
1651
|
+
* breakdown; a priced provider missing a table entry returns undefined so the
|
|
1652
|
+
* caller can surface a `pricing_missing` event instead of a fake zero.
|
|
1653
|
+
*/
|
|
1654
|
+
function missing_pricing_breakdown(provider) {
|
|
1655
|
+
if (!FREE_PROVIDERS.has(provider)) return void 0;
|
|
1656
|
+
return {
|
|
1657
|
+
total_usd: 0,
|
|
1658
|
+
input_usd: 0,
|
|
1659
|
+
output_usd: 0,
|
|
1660
|
+
currency: "USD",
|
|
1661
|
+
is_estimate: true
|
|
1662
|
+
};
|
|
1663
|
+
}
|
|
1664
|
+
/**
|
|
1665
|
+
* Split reasoning tokens between a surfaced `reasoning_usd` field and the
|
|
1666
|
+
* output total.
|
|
1667
|
+
*
|
|
1668
|
+
* With a dedicated `reasoning_per_million` rate the reasoning cost is surfaced
|
|
1669
|
+
* separately and left out of output; without one it rolls into output at the
|
|
1670
|
+
* output rate. Absent reasoning tokens leave output untouched and surface
|
|
1671
|
+
* nothing.
|
|
1672
|
+
*/
|
|
1673
|
+
function apply_reasoning_cost(plain_output_usd_raw, reasoning, has_reasoning_rate, reasoning_usd_raw) {
|
|
1674
|
+
if (reasoning > 0) {
|
|
1675
|
+
if (has_reasoning_rate) return {
|
|
1676
|
+
output_usd_raw: plain_output_usd_raw,
|
|
1677
|
+
surface_reasoning_usd: round6$1(reasoning_usd_raw)
|
|
1678
|
+
};
|
|
1679
|
+
return {
|
|
1680
|
+
output_usd_raw: plain_output_usd_raw + reasoning_usd_raw,
|
|
1681
|
+
surface_reasoning_usd: void 0
|
|
1682
|
+
};
|
|
1683
|
+
}
|
|
1684
|
+
return {
|
|
1685
|
+
output_usd_raw: plain_output_usd_raw,
|
|
1686
|
+
surface_reasoning_usd: void 0
|
|
1687
|
+
};
|
|
1688
|
+
}
|
|
1689
|
+
/**
|
|
1544
1690
|
* Compute a CostBreakdown for a single turn's usage.
|
|
1545
1691
|
*
|
|
1546
1692
|
* Returns undefined when pricing is absent AND the provider is not free. Free
|
|
@@ -1552,50 +1698,28 @@ function round6$1(value) {
|
|
|
1552
1698
|
* input call.
|
|
1553
1699
|
*/
|
|
1554
1700
|
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;
|
|
1701
|
+
if (pricing === void 0) return missing_pricing_breakdown(provider);
|
|
1567
1702
|
const cached = usage.cached_input_tokens ?? 0;
|
|
1568
1703
|
const cache_write = usage.cache_write_tokens ?? 0;
|
|
1569
1704
|
const reasoning = usage.reasoning_tokens ?? 0;
|
|
1570
|
-
const fresh_input = Math.max(0, input_tokens - cached - cache_write);
|
|
1705
|
+
const fresh_input = Math.max(0, usage.input_tokens - cached - cache_write);
|
|
1571
1706
|
const cached_rate = pricing.cached_input_per_million ?? pricing.input_per_million;
|
|
1572
1707
|
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
1708
|
const reasoning_rate = pricing.reasoning_per_million ?? pricing.output_per_million;
|
|
1575
1709
|
const input_usd_raw = fresh_input * pricing.input_per_million / 1e6;
|
|
1576
1710
|
const cached_usd_raw = cached * cached_rate / 1e6;
|
|
1577
1711
|
const cache_write_usd_raw = cache_write * cache_write_rate / 1e6;
|
|
1578
1712
|
const reasoning_usd_raw = reasoning * reasoning_rate / 1e6;
|
|
1579
|
-
const
|
|
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);
|
|
1713
|
+
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
1714
|
const breakdown = {
|
|
1591
1715
|
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,
|
|
1716
|
+
input_usd: round6$1(input_usd_raw),
|
|
1717
|
+
output_usd: round6$1(output_usd_raw),
|
|
1594
1718
|
currency: "USD",
|
|
1595
1719
|
is_estimate: true
|
|
1596
1720
|
};
|
|
1597
|
-
if (
|
|
1598
|
-
if (
|
|
1721
|
+
if (cached > 0) breakdown.cached_input_usd = round6$1(cached_usd_raw);
|
|
1722
|
+
if (cache_write > 0) breakdown.cache_write_usd = round6$1(cache_write_usd_raw);
|
|
1599
1723
|
if (surface_reasoning_usd !== void 0) breakdown.reasoning_usd = surface_reasoning_usd;
|
|
1600
1724
|
return breakdown;
|
|
1601
1725
|
}
|
|
@@ -1653,6 +1777,52 @@ function to_abort_error(reason) {
|
|
|
1653
1777
|
return new aborted_error("aborted", { reason });
|
|
1654
1778
|
}
|
|
1655
1779
|
/**
|
|
1780
|
+
* Build the `rate_limit_error` thrown when 429 retries exhaust. Prefers the
|
|
1781
|
+
* failing attempt's own `Retry-After`, falling back to the most recent one a
|
|
1782
|
+
* prior attempt carried (`last_rate_limit_after`).
|
|
1783
|
+
*/
|
|
1784
|
+
function build_rate_limit_error(retryable, attempt, last_rate_limit_after) {
|
|
1785
|
+
const metadata = { attempts: attempt };
|
|
1786
|
+
if (retryable.retry_after_ms !== void 0) metadata.retry_after_ms = retryable.retry_after_ms;
|
|
1787
|
+
else if (last_rate_limit_after !== void 0) metadata.retry_after_ms = last_rate_limit_after;
|
|
1788
|
+
if (retryable.status !== void 0) metadata.status = retryable.status;
|
|
1789
|
+
return new rate_limit_error(retryable.message ?? `rate limited after ${attempt} attempts`, metadata);
|
|
1790
|
+
}
|
|
1791
|
+
/**
|
|
1792
|
+
* Build the `provider_error` thrown when 5xx/network/timeout retries exhaust.
|
|
1793
|
+
* Timeout collapses onto the `network` cause; only 5xx carries status/body.
|
|
1794
|
+
*/
|
|
1795
|
+
function build_provider_error(retryable, attempt) {
|
|
1796
|
+
const metadata = { cause_kind: retryable.kind === "provider_5xx" ? "provider_5xx" : "network" };
|
|
1797
|
+
if (retryable.kind === "provider_5xx") {
|
|
1798
|
+
if (retryable.status !== void 0) metadata.status = retryable.status;
|
|
1799
|
+
if (retryable.body !== void 0) metadata.body = retryable.body;
|
|
1800
|
+
}
|
|
1801
|
+
return new provider_error(retryable.message ?? `${retryable.kind} after ${attempt} attempts`, metadata);
|
|
1802
|
+
}
|
|
1803
|
+
/**
|
|
1804
|
+
* Map an exhausted retryable failure onto the error this layer rejects with:
|
|
1805
|
+
* `rate_limit_error` for 429s, `provider_error` for everything else.
|
|
1806
|
+
*/
|
|
1807
|
+
function build_exhaustion_error(retryable, attempt, last_rate_limit_after) {
|
|
1808
|
+
if (retryable.kind === "rate_limit") return build_rate_limit_error(retryable, attempt, last_rate_limit_after);
|
|
1809
|
+
return build_provider_error(retryable, attempt);
|
|
1810
|
+
}
|
|
1811
|
+
/**
|
|
1812
|
+
* Compute the pre-retry backoff for `attempt`. A rate-limit `Retry-After`
|
|
1813
|
+
* outranks the local cap (`Math.max`), so a server instruction longer than
|
|
1814
|
+
* `max_delay_ms` is honored; the honored value is returned so the caller can
|
|
1815
|
+
* carry it onto a later failure that omits its own.
|
|
1816
|
+
*/
|
|
1817
|
+
function next_backoff(retryable, backoff_policy, attempt) {
|
|
1818
|
+
const delay = compute_backoff(backoff_policy, attempt - 1);
|
|
1819
|
+
if (retryable.kind === "rate_limit" && retryable.retry_after_ms !== void 0) return {
|
|
1820
|
+
delay: Math.max(delay, retryable.retry_after_ms),
|
|
1821
|
+
retry_after_ms: retryable.retry_after_ms
|
|
1822
|
+
};
|
|
1823
|
+
return { delay };
|
|
1824
|
+
}
|
|
1825
|
+
/**
|
|
1656
1826
|
* Retry `fn` under `policy`. `fn` must throw a RetryableError-shaped object to
|
|
1657
1827
|
* trigger retry; any other thrown value short-circuits as a permanent failure.
|
|
1658
1828
|
*
|
|
@@ -1676,30 +1846,12 @@ async function retry_with_policy(fn, policy = DEFAULT_RETRY, abort) {
|
|
|
1676
1846
|
return await fn(attempt);
|
|
1677
1847
|
} catch (err) {
|
|
1678
1848
|
const retryable = classify_retryable(err);
|
|
1679
|
-
if (retryable === void 0) throw err;
|
|
1680
|
-
if (!is_retryable(retryable.kind, policy)) throw err;
|
|
1849
|
+
if (retryable === void 0 || !is_retryable(retryable.kind, policy)) throw err;
|
|
1681
1850
|
attempt += 1;
|
|
1682
|
-
if (attempt >= policy.max_attempts)
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
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);
|
|
1851
|
+
if (attempt >= policy.max_attempts) throw build_exhaustion_error(retryable, attempt, last_rate_limit_after);
|
|
1852
|
+
const backoff = next_backoff(retryable, backoff_policy, attempt);
|
|
1853
|
+
last_rate_limit_after = backoff.retry_after_ms ?? last_rate_limit_after;
|
|
1854
|
+
await wait_with_abort(backoff.delay, signal, to_abort_error);
|
|
1703
1855
|
}
|
|
1704
1856
|
}
|
|
1705
1857
|
}
|
|
@@ -1718,6 +1870,44 @@ function read_number$1(err, key) {
|
|
|
1718
1870
|
return typeof value === "number" ? value : void 0;
|
|
1719
1871
|
}
|
|
1720
1872
|
/**
|
|
1873
|
+
* Assemble the `rate_limit` retryable shape, copying the optional
|
|
1874
|
+
* `retry_after_ms`, `status`, and `message` fields when each is present.
|
|
1875
|
+
*/
|
|
1876
|
+
function build_retryable_rate_limit(err) {
|
|
1877
|
+
const base = { kind: "rate_limit" };
|
|
1878
|
+
const retry_after = read_number$1(err, "retry_after_ms");
|
|
1879
|
+
if (retry_after !== void 0) base.retry_after_ms = retry_after;
|
|
1880
|
+
const status = read_number$1(err, "status");
|
|
1881
|
+
if (status !== void 0) base.status = status;
|
|
1882
|
+
const message = read_string$1(err, "message");
|
|
1883
|
+
if (message !== void 0) base.message = message;
|
|
1884
|
+
return base;
|
|
1885
|
+
}
|
|
1886
|
+
/**
|
|
1887
|
+
* Assemble the `provider_5xx` retryable shape, copying the optional `status`,
|
|
1888
|
+
* `body`, and `message` fields when each is present.
|
|
1889
|
+
*/
|
|
1890
|
+
function build_retryable_5xx(err) {
|
|
1891
|
+
const base = { kind: "provider_5xx" };
|
|
1892
|
+
const status = read_number$1(err, "status");
|
|
1893
|
+
if (status !== void 0) base.status = status;
|
|
1894
|
+
const body = read_string$1(err, "body");
|
|
1895
|
+
if (body !== void 0) base.body = body;
|
|
1896
|
+
const message = read_string$1(err, "message");
|
|
1897
|
+
if (message !== void 0) base.message = message;
|
|
1898
|
+
return base;
|
|
1899
|
+
}
|
|
1900
|
+
/**
|
|
1901
|
+
* Assemble a `network` or `timeout` retryable shape, copying the optional
|
|
1902
|
+
* `message` when present.
|
|
1903
|
+
*/
|
|
1904
|
+
function build_retryable_signal(err, kind) {
|
|
1905
|
+
const base = { kind };
|
|
1906
|
+
const message = read_string$1(err, "message");
|
|
1907
|
+
if (message !== void 0) base.message = message;
|
|
1908
|
+
return base;
|
|
1909
|
+
}
|
|
1910
|
+
/**
|
|
1721
1911
|
* Classify a thrown value into a `RetryableError`, or `undefined` if it
|
|
1722
1912
|
* does not carry one of the recognized retryable `kind`s.
|
|
1723
1913
|
*
|
|
@@ -1728,64 +1918,147 @@ function read_number$1(err, key) {
|
|
|
1728
1918
|
function classify_retryable(err) {
|
|
1729
1919
|
if (err === null || typeof err !== "object") return void 0;
|
|
1730
1920
|
const kind = read_string$1(err, "kind");
|
|
1731
|
-
if (kind === "rate_limit")
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1921
|
+
if (kind === "rate_limit") return build_retryable_rate_limit(err);
|
|
1922
|
+
if (kind === "provider_5xx") return build_retryable_5xx(err);
|
|
1923
|
+
if (kind === "network" || kind === "timeout") return build_retryable_signal(err, kind);
|
|
1924
|
+
}
|
|
1925
|
+
//#endregion
|
|
1926
|
+
//#region src/engine/providers/native_shared.ts
|
|
1927
|
+
/**
|
|
1928
|
+
* The message under a JSON error envelope's `error` key, or undefined when
|
|
1929
|
+
* the shape carries none worth surfacing.
|
|
1930
|
+
*/
|
|
1931
|
+
function error_field_message(parsed) {
|
|
1932
|
+
if (parsed === null || typeof parsed !== "object") return void 0;
|
|
1933
|
+
const error = Reflect.get(parsed, "error");
|
|
1934
|
+
if (typeof error === "string" && error.length > 0) return error;
|
|
1935
|
+
if (error === null || typeof error !== "object") return void 0;
|
|
1936
|
+
const message = Reflect.get(error, "message");
|
|
1937
|
+
return typeof message === "string" && message.length > 0 ? message : void 0;
|
|
1938
|
+
}
|
|
1939
|
+
/**
|
|
1940
|
+
* Pull a human-readable message out of a non-2xx body. The native wires share
|
|
1941
|
+
* one envelope family: Anthropic sends `{ type: 'error', error: { message } }`,
|
|
1942
|
+
* OpenAI-style wires send `{ error: { message } }`, and Ollama sends
|
|
1943
|
+
* `{ error: "message" }` (with the nested shape appearing behind proxies), so
|
|
1944
|
+
* one parser accepts a string or a `{ message }` object under `error` and
|
|
1945
|
+
* falls back to the raw body, truncated to 300 chars.
|
|
1946
|
+
*/
|
|
1947
|
+
function extract_error_message(body) {
|
|
1948
|
+
if (body.length === 0) return "(empty body)";
|
|
1949
|
+
let message;
|
|
1950
|
+
try {
|
|
1951
|
+
message = error_field_message(JSON.parse(body));
|
|
1952
|
+
} catch {}
|
|
1953
|
+
if (message !== void 0) return message;
|
|
1954
|
+
return body.length > 300 ? `${body.slice(0, 300)}...` : body;
|
|
1955
|
+
}
|
|
1956
|
+
/**
|
|
1957
|
+
* Map a non-2xx response to what the engine's retry stack expects: 401
|
|
1958
|
+
* becomes a typed provider_auth_error, 429/5xx keep `status` +
|
|
1959
|
+
* `responseHeaders` so classify_provider_error marks them retryable
|
|
1960
|
+
* (rate_limit honoring retry-after, provider_5xx), and any other 4xx is a
|
|
1961
|
+
* permanent provider_error that surfaces as-is.
|
|
1962
|
+
*/
|
|
1963
|
+
async function response_error(response, provider) {
|
|
1964
|
+
let body = "";
|
|
1965
|
+
try {
|
|
1966
|
+
body = await response.text();
|
|
1967
|
+
} catch {}
|
|
1968
|
+
const detail = extract_error_message(body);
|
|
1969
|
+
const status = response.status;
|
|
1970
|
+
if (status === 401) return new provider_auth_error(provider, `${provider} authentication failed (401): ${detail}`);
|
|
1971
|
+
if (status === 429 || status >= 500) {
|
|
1972
|
+
const retry_after = response.headers.get("retry-after");
|
|
1973
|
+
return Object.assign(/* @__PURE__ */ new Error(`${provider} API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
|
|
1740
1974
|
}
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1975
|
+
return new provider_error(`${provider} API error ${status}: ${detail}`, {
|
|
1976
|
+
status,
|
|
1977
|
+
body
|
|
1978
|
+
});
|
|
1979
|
+
}
|
|
1980
|
+
/**
|
|
1981
|
+
* A user abort surfaces as the fetch/reader AbortError; retry_turn converts
|
|
1982
|
+
* it via its own signal check, so it is rethrown untouched. Everything else
|
|
1983
|
+
* is a transport failure wrapped in the `kind: 'network'` shape the shared
|
|
1984
|
+
* classify_provider_error marks retryable.
|
|
1985
|
+
*/
|
|
1986
|
+
function rethrow_network_failure(err, abort, provider) {
|
|
1987
|
+
if (abort.aborted) throw err;
|
|
1988
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
1989
|
+
throw Object.assign(/* @__PURE__ */ new Error(`${provider} native: network failure: ${detail}`), { kind: "network" });
|
|
1990
|
+
}
|
|
1991
|
+
/**
|
|
1992
|
+
* Run one native turn over HTTP: POST the dialect's body, map a non-2xx to
|
|
1993
|
+
* the retry stack's shapes, and hand the response to the dialect's stream or
|
|
1994
|
+
* JSON consumer. `build_body` is called inside the network try/catch on
|
|
1995
|
+
* purpose: that is where each adapter historically built its body, so a
|
|
1996
|
+
* mapper throw keeps surfacing through the same path it always did.
|
|
1997
|
+
*/
|
|
1998
|
+
async function invoke_http_turn(req, provider, request, consume) {
|
|
1999
|
+
let response;
|
|
2000
|
+
try {
|
|
2001
|
+
response = await fetch(request.url, {
|
|
2002
|
+
method: "POST",
|
|
2003
|
+
headers: request.headers,
|
|
2004
|
+
body: JSON.stringify(request.build_body()),
|
|
2005
|
+
signal: req.abort
|
|
2006
|
+
});
|
|
2007
|
+
} catch (err) {
|
|
2008
|
+
rethrow_network_failure(err, req.abort, provider);
|
|
1750
2009
|
}
|
|
1751
|
-
if (
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
2010
|
+
if (!response.ok) throw await response_error(response, provider);
|
|
2011
|
+
if (req.stream) return consume.stream(response);
|
|
2012
|
+
const payload = await response.json();
|
|
2013
|
+
return consume.parse(payload);
|
|
2014
|
+
}
|
|
2015
|
+
/**
|
|
2016
|
+
* Drain a streaming response: decode bytes, reassemble framed payloads (SSE
|
|
2017
|
+
* events or NDJSON lines), and feed them to the dialect's aggregator, which
|
|
2018
|
+
* dispatches chunks through req.dispatch_chunk as they arrive. Transport
|
|
2019
|
+
* failures mid-read wrap as network errors; aggregator throws (malformed
|
|
2020
|
+
* frames, mid-stream error events, a rejecting on_chunk) pass through
|
|
2021
|
+
* untouched, with the reader cancelled so the connection is released.
|
|
2022
|
+
*/
|
|
2023
|
+
async function consume_framed_stream(response, req, provider, framing, create_aggregator) {
|
|
2024
|
+
const body = response.body;
|
|
2025
|
+
if (body === null) throw new provider_error(`${provider} native: streaming response has no body`);
|
|
2026
|
+
const aggregator = create_aggregator(req.dispatch_chunk ?? (async () => {}));
|
|
2027
|
+
const reader = body.getReader();
|
|
2028
|
+
const text_decoder = new TextDecoder();
|
|
2029
|
+
const next_bytes = async () => {
|
|
2030
|
+
let step;
|
|
2031
|
+
try {
|
|
2032
|
+
step = await reader.read();
|
|
2033
|
+
} catch (err) {
|
|
2034
|
+
rethrow_network_failure(err, req.abort, provider);
|
|
2035
|
+
}
|
|
2036
|
+
return step.done ? void 0 : step.value;
|
|
2037
|
+
};
|
|
2038
|
+
try {
|
|
2039
|
+
while (true) {
|
|
2040
|
+
const bytes = await next_bytes();
|
|
2041
|
+
if (bytes === void 0) break;
|
|
2042
|
+
for (const data of framing.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle(data);
|
|
2043
|
+
}
|
|
2044
|
+
} finally {
|
|
2045
|
+
reader.cancel().catch(() => {});
|
|
1756
2046
|
}
|
|
2047
|
+
const tail = [...framing.push(text_decoder.decode()), ...framing.flush()];
|
|
2048
|
+
for (const data of tail) await aggregator.handle(data);
|
|
2049
|
+
return aggregator.complete();
|
|
1757
2050
|
}
|
|
1758
|
-
//#endregion
|
|
1759
|
-
//#region src/engine/providers/sse_native.ts
|
|
1760
2051
|
/**
|
|
1761
|
-
* Incremental
|
|
1762
|
-
*
|
|
1763
|
-
*
|
|
1764
|
-
*
|
|
1765
|
-
*
|
|
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.
|
|
2052
|
+
* Incremental newline framing shared by the SSE and NDJSON decoders: buffer
|
|
2053
|
+
* decoded text across arbitrary chunk boundaries, hand each completed line
|
|
2054
|
+
* (CR trimmed) to `handle_line`, and on flush hand over an unterminated tail
|
|
2055
|
+
* line, then `on_flush` for state a decoder holds across lines (an SSE event
|
|
2056
|
+
* never closed by a blank line).
|
|
1771
2057
|
*/
|
|
1772
|
-
function
|
|
2058
|
+
function create_line_splitter(handle_line, on_flush) {
|
|
1773
2059
|
let buffer = "";
|
|
1774
|
-
let data_lines = [];
|
|
1775
2060
|
const take_line = (raw, out) => {
|
|
1776
|
-
|
|
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
|
-
}
|
|
2061
|
+
handle_line(raw.endsWith("\r") ? raw.slice(0, -1) : raw, out);
|
|
1789
2062
|
};
|
|
1790
2063
|
return {
|
|
1791
2064
|
push(text) {
|
|
@@ -1803,38 +2076,134 @@ function create_sse_decoder() {
|
|
|
1803
2076
|
const out = [];
|
|
1804
2077
|
if (buffer.length > 0) take_line(buffer, out);
|
|
1805
2078
|
buffer = "";
|
|
1806
|
-
|
|
1807
|
-
out.push(data_lines.join("\n"));
|
|
1808
|
-
data_lines = [];
|
|
1809
|
-
}
|
|
2079
|
+
on_flush?.(out);
|
|
1810
2080
|
return out;
|
|
1811
2081
|
}
|
|
1812
2082
|
};
|
|
1813
2083
|
}
|
|
1814
|
-
//#endregion
|
|
1815
|
-
//#region src/engine/providers/anthropic_native.ts
|
|
1816
2084
|
/**
|
|
1817
|
-
*
|
|
1818
|
-
*
|
|
1819
|
-
*
|
|
1820
|
-
*
|
|
1821
|
-
*
|
|
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.
|
|
2085
|
+
* Decode a tool call's wire `arguments` string into the engine's input value:
|
|
2086
|
+
* non-empty JSON parses (bad JSON is a provider_error naming the tool), and
|
|
2087
|
+
* an absent or empty value means `{}`, the shape some servers send for
|
|
2088
|
+
* no-argument calls. The Ollama wire's structured-object arguments are its
|
|
2089
|
+
* caller's business before this runs.
|
|
1835
2090
|
*/
|
|
2091
|
+
function parse_tool_call_arguments(raw_args, provider, tool_name) {
|
|
2092
|
+
if (typeof raw_args === "string" && raw_args.length > 0) try {
|
|
2093
|
+
return JSON.parse(raw_args);
|
|
2094
|
+
} catch {
|
|
2095
|
+
throw new provider_error(`${provider} native: tool_calls arguments for ${tool_name} is not valid JSON`);
|
|
2096
|
+
}
|
|
2097
|
+
return {};
|
|
2098
|
+
}
|
|
1836
2099
|
/**
|
|
1837
|
-
*
|
|
2100
|
+
* Fold engine messages into a wire's message array by role. Serves the wires
|
|
2101
|
+
* whose messages map 1:1 in place (chat/completions, Ollama's /api/chat); the
|
|
2102
|
+
* Messages API's system-hoisting and role-merging rules live in its own
|
|
2103
|
+
* mapper instead.
|
|
2104
|
+
*/
|
|
2105
|
+
function map_messages_by_role(messages, wire) {
|
|
2106
|
+
const out = [];
|
|
2107
|
+
for (const message of messages) switch (message.role) {
|
|
2108
|
+
case "system":
|
|
2109
|
+
out.push(wire.system(message.content));
|
|
2110
|
+
break;
|
|
2111
|
+
case "user":
|
|
2112
|
+
out.push(wire.user(message.content));
|
|
2113
|
+
break;
|
|
2114
|
+
case "assistant":
|
|
2115
|
+
out.push(wire.assistant(message.content));
|
|
2116
|
+
break;
|
|
2117
|
+
case "tool":
|
|
2118
|
+
out.push(wire.tool(message));
|
|
2119
|
+
break;
|
|
2120
|
+
}
|
|
2121
|
+
return out;
|
|
2122
|
+
}
|
|
2123
|
+
/**
|
|
2124
|
+
* Partition assistant content parts into the text runs and tool calls the
|
|
2125
|
+
* wire mappers serialize separately.
|
|
2126
|
+
*/
|
|
2127
|
+
function split_assistant_parts(parts) {
|
|
2128
|
+
const texts = [];
|
|
2129
|
+
const tool_parts = [];
|
|
2130
|
+
for (const part of parts) if (part.type === "text") texts.push(part.text);
|
|
2131
|
+
else tool_parts.push(part);
|
|
2132
|
+
return {
|
|
2133
|
+
texts,
|
|
2134
|
+
tool_parts
|
|
2135
|
+
};
|
|
2136
|
+
}
|
|
2137
|
+
/**
|
|
2138
|
+
* Read the api_key and base_url every OpenAI-compatible dialect starts from:
|
|
2139
|
+
* a missing or non-string api_key becomes '' (the core's empty-key guard owns
|
|
2140
|
+
* the throw), and an absent base_url falls back to the dialect's default.
|
|
2141
|
+
*/
|
|
2142
|
+
function read_dialect_init(init, default_base_url) {
|
|
2143
|
+
return {
|
|
2144
|
+
api_key: typeof init.api_key === "string" ? init.api_key : "",
|
|
2145
|
+
base_url: typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : default_base_url
|
|
2146
|
+
};
|
|
2147
|
+
}
|
|
2148
|
+
//#endregion
|
|
2149
|
+
//#region src/engine/providers/sse_native.ts
|
|
2150
|
+
/**
|
|
2151
|
+
* Incremental SSE decoder shared by the native (raw-HTTP) provider adapters.
|
|
2152
|
+
*/
|
|
2153
|
+
/**
|
|
2154
|
+
* Wire-neutral framing only: push() takes decoded text as it arrives off the
|
|
2155
|
+
* wire (any chunk boundary, including mid-line) and returns the data payloads
|
|
2156
|
+
* of every event completed by that chunk; flush() drains an event left open
|
|
2157
|
+
* when the stream ends without a trailing blank line. Only `data:` fields
|
|
2158
|
+
* matter to the streams fascicle consumes (the Messages API repeats the
|
|
2159
|
+
* event type inside the JSON payload and the Chat Completions stream sends
|
|
2160
|
+
* nothing but data frames), so `event:`/`id:`/`retry:` fields and `:`
|
|
2161
|
+
* comments are dropped. Multi-line data joins with '\n' per the SSE spec.
|
|
2162
|
+
*/
|
|
2163
|
+
function create_sse_decoder() {
|
|
2164
|
+
let data_lines = [];
|
|
2165
|
+
const close_event = (out) => {
|
|
2166
|
+
if (data_lines.length > 0) {
|
|
2167
|
+
out.push(data_lines.join("\n"));
|
|
2168
|
+
data_lines = [];
|
|
2169
|
+
}
|
|
2170
|
+
};
|
|
2171
|
+
return create_line_splitter((line, out) => {
|
|
2172
|
+
if (line.length === 0) {
|
|
2173
|
+
close_event(out);
|
|
2174
|
+
return;
|
|
2175
|
+
}
|
|
2176
|
+
if (line.startsWith(":")) return;
|
|
2177
|
+
if (line.startsWith("data:")) {
|
|
2178
|
+
const value = line.slice(5);
|
|
2179
|
+
data_lines.push(value.startsWith(" ") ? value.slice(1) : value);
|
|
2180
|
+
}
|
|
2181
|
+
}, close_event);
|
|
2182
|
+
}
|
|
2183
|
+
//#endregion
|
|
2184
|
+
//#region src/engine/providers/anthropic_native.ts
|
|
2185
|
+
/**
|
|
2186
|
+
* Native Anthropic provider adapter (raw HTTP).
|
|
2187
|
+
*
|
|
2188
|
+
* Talks to the Messages API directly over global fetch with zero `ai` /
|
|
2189
|
+
* `@ai-sdk/*` in its module graph. It owns request/response mapping only:
|
|
2190
|
+
* generate.ts wraps invoke_turn in retry + classification + abort, so
|
|
2191
|
+
* failures are thrown in shapes the shared classify_provider_error already
|
|
2192
|
+
* understands (`status` + `responseHeaders` for HTTP transients,
|
|
2193
|
+
* `kind: 'network'` for transport failures) instead of being re-classified
|
|
2194
|
+
* or retried here.
|
|
2195
|
+
*
|
|
2196
|
+
* Selected via `transport: 'native'` on the anthropic provider init. The
|
|
2197
|
+
* adapter name stays 'anthropic' so DEFAULT_PRICING keys and UsageTotals
|
|
2198
|
+
* fields keep working across transports. Schema requests ride the engine's
|
|
2199
|
+
* prompt + parse + repair loop, so `TurnRequest.schema` is intentionally
|
|
2200
|
+
* unread. Streaming hand-rolls the SSE parse; the aggregator rebuilds the
|
|
2201
|
+
* non-stream payload shape and feeds it through the same
|
|
2202
|
+
* parse_messages_response, so streamed and non-streamed results are equal by
|
|
2203
|
+
* construction rather than by parallel code paths.
|
|
2204
|
+
*/
|
|
2205
|
+
/**
|
|
2206
|
+
* Effort level to extended-thinking budget tokens. Lives here (not in
|
|
1838
2207
|
* anthropic.ts) because both transports map effort from this table and the
|
|
1839
2208
|
* ai_sdk factory already imports this module for transport dispatch; the
|
|
1840
2209
|
* reverse import would be a cycle.
|
|
@@ -1861,22 +2230,26 @@ const DEFAULT_BASE_URL$2 = "https://api.anthropic.com/v1";
|
|
|
1861
2230
|
*/
|
|
1862
2231
|
const DEFAULT_MAX_TOKENS = 4096;
|
|
1863
2232
|
/**
|
|
2233
|
+
* Wrap non-blank text in a single text block; blank text maps to no blocks,
|
|
2234
|
+
* since the API rejects empty text blocks.
|
|
2235
|
+
*/
|
|
2236
|
+
function text_blocks(text) {
|
|
2237
|
+
return text.trim().length > 0 ? [{
|
|
2238
|
+
type: "text",
|
|
2239
|
+
text
|
|
2240
|
+
}] : [];
|
|
2241
|
+
}
|
|
2242
|
+
/**
|
|
1864
2243
|
* Map user message content to Messages-API text blocks, dropping empty text
|
|
1865
2244
|
* (the API rejects empty blocks). Image parts throw a capability error: they
|
|
1866
2245
|
* are not mapped on this transport.
|
|
1867
2246
|
*/
|
|
1868
2247
|
function to_user_blocks(content) {
|
|
1869
|
-
if (typeof content === "string") return content
|
|
1870
|
-
type: "text",
|
|
1871
|
-
text: content
|
|
1872
|
-
}] : [];
|
|
2248
|
+
if (typeof content === "string") return text_blocks(content);
|
|
1873
2249
|
const blocks = [];
|
|
1874
2250
|
for (const part of content) {
|
|
1875
2251
|
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
|
-
|
|
1877
|
-
type: "text",
|
|
1878
|
-
text: part.text
|
|
1879
|
-
});
|
|
2252
|
+
blocks.push(...text_blocks(part.text));
|
|
1880
2253
|
}
|
|
1881
2254
|
return blocks;
|
|
1882
2255
|
}
|
|
@@ -1885,17 +2258,10 @@ function to_user_blocks(content) {
|
|
|
1885
2258
|
* text blocks (empties dropped), tool-call parts become tool_use blocks.
|
|
1886
2259
|
*/
|
|
1887
2260
|
function to_assistant_blocks(content) {
|
|
1888
|
-
if (typeof content === "string") return content
|
|
1889
|
-
type: "text",
|
|
1890
|
-
text: content
|
|
1891
|
-
}] : [];
|
|
2261
|
+
if (typeof content === "string") return text_blocks(content);
|
|
1892
2262
|
const blocks = [];
|
|
1893
|
-
for (const part of content) if (part.type === "text")
|
|
1894
|
-
|
|
1895
|
-
type: "text",
|
|
1896
|
-
text: part.text
|
|
1897
|
-
});
|
|
1898
|
-
} else blocks.push({
|
|
2263
|
+
for (const part of content) if (part.type === "text") blocks.push(...text_blocks(part.text));
|
|
2264
|
+
else blocks.push({
|
|
1899
2265
|
type: "tool_use",
|
|
1900
2266
|
id: part.id,
|
|
1901
2267
|
name: part.name,
|
|
@@ -2032,32 +2398,63 @@ function map_anthropic_usage(raw) {
|
|
|
2032
2398
|
return totals;
|
|
2033
2399
|
}
|
|
2034
2400
|
/**
|
|
2401
|
+
* Read the id and name a tool_use block must carry, throwing a
|
|
2402
|
+
* provider_error naming `where` the malformed block appeared.
|
|
2403
|
+
*/
|
|
2404
|
+
function read_tool_use_identity(block, where) {
|
|
2405
|
+
const id = Reflect.get(block, "id");
|
|
2406
|
+
const name = Reflect.get(block, "name");
|
|
2407
|
+
if (typeof id !== "string" || typeof name !== "string") throw new provider_error(`anthropic native: malformed tool_use block in ${where}`);
|
|
2408
|
+
return {
|
|
2409
|
+
id,
|
|
2410
|
+
name
|
|
2411
|
+
};
|
|
2412
|
+
}
|
|
2413
|
+
/**
|
|
2414
|
+
* Fold one response `content` block into the text/tool_call accumulators.
|
|
2415
|
+
* Text blocks append their string; tool_use blocks contribute a call whose id
|
|
2416
|
+
* and name are validated by read_tool_use_identity; thinking /
|
|
2417
|
+
* redacted_thinking blocks have no TurnResult field and are skipped.
|
|
2418
|
+
*/
|
|
2419
|
+
function push_content_block(block, text_parts, tool_calls) {
|
|
2420
|
+
if (block === null || typeof block !== "object") return;
|
|
2421
|
+
const type = Reflect.get(block, "type");
|
|
2422
|
+
if (type === "text") {
|
|
2423
|
+
const text = Reflect.get(block, "text");
|
|
2424
|
+
if (typeof text === "string") text_parts.push(text);
|
|
2425
|
+
return;
|
|
2426
|
+
}
|
|
2427
|
+
if (type === "tool_use") {
|
|
2428
|
+
const { id, name } = read_tool_use_identity(block, "response content");
|
|
2429
|
+
tool_calls.push({
|
|
2430
|
+
id,
|
|
2431
|
+
name,
|
|
2432
|
+
input: Reflect.get(block, "input")
|
|
2433
|
+
});
|
|
2434
|
+
}
|
|
2435
|
+
}
|
|
2436
|
+
/**
|
|
2437
|
+
* Walk a response payload's `content` array into the flat text and tool_call
|
|
2438
|
+
* lists a TurnResult carries. A non-array `content` yields empty lists,
|
|
2439
|
+
* matching a payload that carried no content blocks.
|
|
2440
|
+
*/
|
|
2441
|
+
function collect_content_blocks(content) {
|
|
2442
|
+
const text_parts = [];
|
|
2443
|
+
const tool_calls = [];
|
|
2444
|
+
if (Array.isArray(content)) for (const block of content) push_content_block(block, text_parts, tool_calls);
|
|
2445
|
+
return {
|
|
2446
|
+
text_parts,
|
|
2447
|
+
tool_calls
|
|
2448
|
+
};
|
|
2449
|
+
}
|
|
2450
|
+
/**
|
|
2035
2451
|
* Parse a non-stream Messages-API response payload into a TurnResult. The
|
|
2036
2452
|
* stream aggregator rebuilds this same payload shape and feeds it here too,
|
|
2037
2453
|
* so both paths share one parser.
|
|
2038
2454
|
*/
|
|
2039
2455
|
function parse_messages_response(payload) {
|
|
2040
2456
|
if (payload === null || typeof payload !== "object") throw new provider_error("anthropic native: response payload is not a JSON object");
|
|
2041
|
-
const
|
|
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
|
-
}
|
|
2457
|
+
const { text_parts, tool_calls } = collect_content_blocks(Reflect.get(payload, "content"));
|
|
2061
2458
|
return {
|
|
2062
2459
|
text: text_parts.join(""),
|
|
2063
2460
|
tool_calls,
|
|
@@ -2138,9 +2535,7 @@ function create_stream_aggregator(step_index, dispatch) {
|
|
|
2138
2535
|
return;
|
|
2139
2536
|
}
|
|
2140
2537
|
if (type === "tool_use") {
|
|
2141
|
-
const 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");
|
|
2538
|
+
const { id, name } = read_tool_use_identity(block, "stream");
|
|
2144
2539
|
const synthetic = {
|
|
2145
2540
|
type: "tool_use",
|
|
2146
2541
|
id,
|
|
@@ -2218,7 +2613,7 @@ function create_stream_aggregator(step_index, dispatch) {
|
|
|
2218
2613
|
});
|
|
2219
2614
|
};
|
|
2220
2615
|
return {
|
|
2221
|
-
async
|
|
2616
|
+
async handle(data) {
|
|
2222
2617
|
let event;
|
|
2223
2618
|
try {
|
|
2224
2619
|
event = JSON.parse(data);
|
|
@@ -2273,93 +2668,13 @@ function create_stream_aggregator(step_index, dispatch) {
|
|
|
2273
2668
|
}
|
|
2274
2669
|
};
|
|
2275
2670
|
}
|
|
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
2671
|
/**
|
|
2327
|
-
* Drain a streaming Messages response
|
|
2328
|
-
*
|
|
2329
|
-
* req.dispatch_chunk as they arrive.
|
|
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.
|
|
2672
|
+
* Drain a streaming Messages response through the shared framed-stream
|
|
2673
|
+
* consumer: SSE framing feeding the aggregator, which dispatches chunks
|
|
2674
|
+
* through req.dispatch_chunk as they arrive.
|
|
2333
2675
|
*/
|
|
2334
2676
|
async function consume_sse_response$1(response, req) {
|
|
2335
|
-
|
|
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();
|
|
2677
|
+
return consume_framed_stream(response, req, "anthropic", create_sse_decoder(), (dispatch) => create_stream_aggregator(req.step_index, dispatch));
|
|
2363
2678
|
}
|
|
2364
2679
|
const SUPPORTED$10 = /* @__PURE__ */ new Set([
|
|
2365
2680
|
"text",
|
|
@@ -2381,24 +2696,18 @@ const create_anthropic_native_adapter = (init) => {
|
|
|
2381
2696
|
kind: "native",
|
|
2382
2697
|
name: "anthropic",
|
|
2383
2698
|
async invoke_turn(req) {
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
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());
|
|
2699
|
+
return invoke_http_turn(req, "anthropic", {
|
|
2700
|
+
url: `${base_url}/messages`,
|
|
2701
|
+
headers: {
|
|
2702
|
+
"content-type": "application/json",
|
|
2703
|
+
"x-api-key": api_key,
|
|
2704
|
+
"anthropic-version": ANTHROPIC_VERSION
|
|
2705
|
+
},
|
|
2706
|
+
build_body: () => build_messages_body(req)
|
|
2707
|
+
}, {
|
|
2708
|
+
stream: (response) => consume_sse_response$1(response, req),
|
|
2709
|
+
parse: parse_messages_response
|
|
2710
|
+
});
|
|
2402
2711
|
},
|
|
2403
2712
|
supports: (capability) => SUPPORTED$10.has(capability)
|
|
2404
2713
|
};
|
|
@@ -2537,18 +2846,16 @@ function to_assistant_message$1(content) {
|
|
|
2537
2846
|
role: "assistant",
|
|
2538
2847
|
content
|
|
2539
2848
|
};
|
|
2540
|
-
const
|
|
2541
|
-
const tool_calls =
|
|
2542
|
-
for (const part of content) if (part.type === "text") text_parts.push(part.text);
|
|
2543
|
-
else tool_calls.push({
|
|
2849
|
+
const { texts, tool_parts } = split_assistant_parts(content);
|
|
2850
|
+
const tool_calls = tool_parts.map((part) => ({
|
|
2544
2851
|
id: part.id,
|
|
2545
2852
|
type: "function",
|
|
2546
2853
|
function: {
|
|
2547
2854
|
name: part.name,
|
|
2548
2855
|
arguments: JSON.stringify(part.input)
|
|
2549
2856
|
}
|
|
2550
|
-
});
|
|
2551
|
-
const text =
|
|
2857
|
+
}));
|
|
2858
|
+
const text = texts.join("");
|
|
2552
2859
|
const message = {
|
|
2553
2860
|
role: "assistant",
|
|
2554
2861
|
content: text.length > 0 || tool_calls.length === 0 ? text : null
|
|
@@ -2563,32 +2870,22 @@ function to_assistant_message$1(content) {
|
|
|
2563
2870
|
* `tool` role messages keyed by tool_call_id.
|
|
2564
2871
|
*/
|
|
2565
2872
|
function to_chat_messages(messages, provider) {
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
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;
|
|
2873
|
+
return map_messages_by_role(messages, {
|
|
2874
|
+
system: (content) => ({
|
|
2875
|
+
role: "system",
|
|
2876
|
+
content
|
|
2877
|
+
}),
|
|
2878
|
+
user: (content) => ({
|
|
2879
|
+
role: "user",
|
|
2880
|
+
content: to_user_content$1(content, provider)
|
|
2881
|
+
}),
|
|
2882
|
+
assistant: to_assistant_message$1,
|
|
2883
|
+
tool: (message) => ({
|
|
2884
|
+
role: "tool",
|
|
2885
|
+
tool_call_id: message.tool_call_id,
|
|
2886
|
+
content: message.content
|
|
2887
|
+
})
|
|
2888
|
+
});
|
|
2592
2889
|
}
|
|
2593
2890
|
/**
|
|
2594
2891
|
* Map fascicle Tool[] to the Chat Completions function-tool shape,
|
|
@@ -2699,17 +2996,10 @@ function parse_tool_call(raw, provider) {
|
|
|
2699
2996
|
if (typeof id !== "string" || fn === null || typeof fn !== "object") throw new provider_error(`${provider} native: malformed tool_calls entry in response`);
|
|
2700
2997
|
const name = Reflect.get(fn, "name");
|
|
2701
2998
|
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
2999
|
return {
|
|
2710
3000
|
id,
|
|
2711
3001
|
name,
|
|
2712
|
-
input
|
|
3002
|
+
input: parse_tool_call_arguments(Reflect.get(fn, "arguments"), provider, name)
|
|
2713
3003
|
};
|
|
2714
3004
|
}
|
|
2715
3005
|
/**
|
|
@@ -2888,101 +3178,29 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
|
|
|
2888
3178
|
}
|
|
2889
3179
|
};
|
|
2890
3180
|
}
|
|
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
3181
|
/**
|
|
2907
|
-
*
|
|
2908
|
-
*
|
|
2909
|
-
*
|
|
2910
|
-
*
|
|
2911
|
-
*
|
|
3182
|
+
* Drain a streaming chat/completions response through the shared
|
|
3183
|
+
* framed-stream consumer: SSE framing feeding the aggregator, which
|
|
3184
|
+
* dispatches chunks through req.dispatch_chunk as they arrive. The
|
|
3185
|
+
* aggregator's `handle_data` name is part of its exported API, so it is
|
|
3186
|
+
* adapted to the consumer's `handle` shape here rather than renamed.
|
|
2912
3187
|
*/
|
|
2913
|
-
async function
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
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
|
|
3188
|
+
async function consume_sse_response(response, req, dialect) {
|
|
3189
|
+
return consume_framed_stream(response, req, dialect.name, create_sse_decoder(), (dispatch) => {
|
|
3190
|
+
const aggregator = create_chat_stream_aggregator(dialect, req.step_index, dispatch);
|
|
3191
|
+
return {
|
|
3192
|
+
handle: aggregator.handle_data,
|
|
3193
|
+
complete: aggregator.complete
|
|
3194
|
+
};
|
|
2928
3195
|
});
|
|
2929
3196
|
}
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
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.
|
|
2948
|
-
*/
|
|
2949
|
-
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();
|
|
2978
|
-
}
|
|
2979
|
-
const SUPPORTED$8 = /* @__PURE__ */ new Set([
|
|
2980
|
-
"text",
|
|
2981
|
-
"tools",
|
|
2982
|
-
"schema",
|
|
2983
|
-
"streaming",
|
|
2984
|
-
"reasoning"
|
|
2985
|
-
]);
|
|
3197
|
+
const SUPPORTED$8 = /* @__PURE__ */ new Set([
|
|
3198
|
+
"text",
|
|
3199
|
+
"tools",
|
|
3200
|
+
"schema",
|
|
3201
|
+
"streaming",
|
|
3202
|
+
"reasoning"
|
|
3203
|
+
]);
|
|
2986
3204
|
/**
|
|
2987
3205
|
* Build a native OpenAI-compatible adapter from a dialect: validates the
|
|
2988
3206
|
* dialect's api_key when auth is `bearer`, normalizes base_url, and wires
|
|
@@ -3001,20 +3219,14 @@ const create_openai_compatible_adapter = (dialect) => {
|
|
|
3001
3219
|
kind: "native",
|
|
3002
3220
|
name: dialect.name,
|
|
3003
3221
|
async invoke_turn(req) {
|
|
3004
|
-
|
|
3005
|
-
|
|
3006
|
-
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
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);
|
|
3222
|
+
return invoke_http_turn(req, dialect.name, {
|
|
3223
|
+
url: `${base_url}/chat/completions`,
|
|
3224
|
+
headers,
|
|
3225
|
+
build_body: () => build_chat_completions_body(req, dialect)
|
|
3226
|
+
}, {
|
|
3227
|
+
stream: (response) => consume_sse_response(response, req, dialect),
|
|
3228
|
+
parse: (payload) => parse_chat_completion(payload, dialect)
|
|
3229
|
+
});
|
|
3018
3230
|
},
|
|
3019
3231
|
supports: (capability) => SUPPORTED$8.has(capability)
|
|
3020
3232
|
};
|
|
@@ -3078,8 +3290,7 @@ const create_openai_adapter = (init) => {
|
|
|
3078
3290
|
* `openai provider requires a non-empty api_key` engine_config_error.
|
|
3079
3291
|
*/
|
|
3080
3292
|
const create_openai_native_adapter = (init) => {
|
|
3081
|
-
const
|
|
3082
|
-
const base_url = typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : DEFAULT_BASE_URL$1;
|
|
3293
|
+
const { api_key, base_url } = read_dialect_init(init, DEFAULT_BASE_URL$1);
|
|
3083
3294
|
const organization = typeof init["organization"] === "string" ? init["organization"] : void 0;
|
|
3084
3295
|
return create_openai_compatible_adapter({
|
|
3085
3296
|
name: "openai",
|
|
@@ -3203,18 +3414,15 @@ function to_assistant_message(content) {
|
|
|
3203
3414
|
role: "assistant",
|
|
3204
3415
|
content
|
|
3205
3416
|
};
|
|
3206
|
-
const
|
|
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
|
-
} });
|
|
3417
|
+
const { texts, tool_parts } = split_assistant_parts(content);
|
|
3213
3418
|
const message = {
|
|
3214
3419
|
role: "assistant",
|
|
3215
|
-
content:
|
|
3420
|
+
content: texts.join("")
|
|
3216
3421
|
};
|
|
3217
|
-
if (
|
|
3422
|
+
if (tool_parts.length > 0) message.tool_calls = tool_parts.map((part) => ({ function: {
|
|
3423
|
+
name: part.name,
|
|
3424
|
+
arguments: part.input
|
|
3425
|
+
} }));
|
|
3218
3426
|
return message;
|
|
3219
3427
|
}
|
|
3220
3428
|
/**
|
|
@@ -3223,32 +3431,22 @@ function to_assistant_message(content) {
|
|
|
3223
3431
|
* `tool_name`, since this wire has no tool_call_id to round-trip.
|
|
3224
3432
|
*/
|
|
3225
3433
|
function to_ollama_messages(messages) {
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
|
|
3237
|
-
|
|
3238
|
-
|
|
3239
|
-
|
|
3240
|
-
|
|
3241
|
-
|
|
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;
|
|
3434
|
+
return map_messages_by_role(messages, {
|
|
3435
|
+
system: (content) => ({
|
|
3436
|
+
role: "system",
|
|
3437
|
+
content
|
|
3438
|
+
}),
|
|
3439
|
+
user: (content) => ({
|
|
3440
|
+
role: "user",
|
|
3441
|
+
content: to_user_content(content)
|
|
3442
|
+
}),
|
|
3443
|
+
assistant: to_assistant_message,
|
|
3444
|
+
tool: (message) => ({
|
|
3445
|
+
role: "tool",
|
|
3446
|
+
tool_name: message.name,
|
|
3447
|
+
content: message.content
|
|
3448
|
+
})
|
|
3449
|
+
});
|
|
3252
3450
|
}
|
|
3253
3451
|
/**
|
|
3254
3452
|
* Build the /api/chat request body for one turn: mapped messages and tools,
|
|
@@ -3325,17 +3523,10 @@ function parse_ollama_tool_call(raw, ordinal, step_index) {
|
|
|
3325
3523
|
const wire_id = Reflect.get(raw, "id");
|
|
3326
3524
|
const id = typeof wire_id === "string" && wire_id.length > 0 ? wire_id : `ollama_call_${step_index}_${ordinal}`;
|
|
3327
3525
|
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
3526
|
return {
|
|
3336
3527
|
id,
|
|
3337
3528
|
name,
|
|
3338
|
-
input
|
|
3529
|
+
input: raw_args !== null && typeof raw_args === "object" ? raw_args : parse_tool_call_arguments(raw_args, "ollama", name)
|
|
3339
3530
|
};
|
|
3340
3531
|
}
|
|
3341
3532
|
/**
|
|
@@ -3358,40 +3549,15 @@ function parse_ollama_chat(payload, step_index) {
|
|
|
3358
3549
|
};
|
|
3359
3550
|
}
|
|
3360
3551
|
/**
|
|
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
3552
|
* Incremental NDJSON framing: push() takes decoded text at any chunk
|
|
3370
|
-
* boundary (including mid-line) and returns the complete lines it finished
|
|
3371
|
-
*
|
|
3372
|
-
* trailing newline.
|
|
3553
|
+
* boundary (including mid-line) and returns the complete lines it finished
|
|
3554
|
+
* (blank lines dropped; JSON parsing is the aggregator's job); flush() drains
|
|
3555
|
+
* a final line left open when the stream ends without a trailing newline.
|
|
3373
3556
|
*/
|
|
3374
3557
|
function create_ndjson_decoder() {
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
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
|
-
};
|
|
3558
|
+
return create_line_splitter((line, out) => {
|
|
3559
|
+
if (line.trim().length > 0) out.push(line);
|
|
3560
|
+
});
|
|
3395
3561
|
}
|
|
3396
3562
|
/**
|
|
3397
3563
|
* Consume /api/chat NDJSON frames, dispatching StreamChunks as they arrive
|
|
@@ -3445,7 +3611,7 @@ function create_ollama_stream_aggregator(step_index, dispatch) {
|
|
|
3445
3611
|
}
|
|
3446
3612
|
};
|
|
3447
3613
|
return {
|
|
3448
|
-
async
|
|
3614
|
+
async handle(line) {
|
|
3449
3615
|
if (done) return;
|
|
3450
3616
|
let frame;
|
|
3451
3617
|
try {
|
|
@@ -3491,94 +3657,13 @@ function create_ollama_stream_aggregator(step_index, dispatch) {
|
|
|
3491
3657
|
}
|
|
3492
3658
|
};
|
|
3493
3659
|
}
|
|
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
3660
|
/**
|
|
3546
|
-
* Drain a streaming /api/chat response
|
|
3547
|
-
*
|
|
3548
|
-
* req.dispatch_chunk as they arrive.
|
|
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.
|
|
3661
|
+
* Drain a streaming /api/chat response through the shared framed-stream
|
|
3662
|
+
* consumer: NDJSON framing feeding the aggregator, which dispatches chunks
|
|
3663
|
+
* through req.dispatch_chunk as they arrive.
|
|
3552
3664
|
*/
|
|
3553
3665
|
async function consume_ndjson_response(response, req) {
|
|
3554
|
-
|
|
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();
|
|
3666
|
+
return consume_framed_stream(response, req, "ollama", create_ndjson_decoder(), (dispatch) => create_ollama_stream_aggregator(req.step_index, dispatch));
|
|
3582
3667
|
}
|
|
3583
3668
|
const SUPPORTED$5 = /* @__PURE__ */ new Set([
|
|
3584
3669
|
"text",
|
|
@@ -3599,20 +3684,14 @@ const create_ollama_native_adapter = (init) => {
|
|
|
3599
3684
|
kind: "native",
|
|
3600
3685
|
name: "ollama",
|
|
3601
3686
|
async invoke_turn(req) {
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
|
|
3605
|
-
|
|
3606
|
-
|
|
3607
|
-
|
|
3608
|
-
|
|
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);
|
|
3687
|
+
return invoke_http_turn(req, "ollama", {
|
|
3688
|
+
url: `${base_url}/api/chat`,
|
|
3689
|
+
headers: { "content-type": "application/json" },
|
|
3690
|
+
build_body: () => build_ollama_chat_body(req)
|
|
3691
|
+
}, {
|
|
3692
|
+
stream: (response) => consume_ndjson_response(response, req),
|
|
3693
|
+
parse: (payload) => parse_ollama_chat(payload, req.step_index)
|
|
3694
|
+
});
|
|
3616
3695
|
},
|
|
3617
3696
|
supports: (capability) => SUPPORTED$5.has(capability)
|
|
3618
3697
|
};
|
|
@@ -3630,15 +3709,11 @@ function translate_ollama_effort(effort) {
|
|
|
3630
3709
|
};
|
|
3631
3710
|
}
|
|
3632
3711
|
/**
|
|
3633
|
-
* Normalize Ollama's raw usage payload into UsageTotals
|
|
3712
|
+
* Normalize Ollama's raw usage payload into UsageTotals: the shared
|
|
3713
|
+
* local-backend mapper, since Ollama never reports cache or reasoning
|
|
3714
|
+
* tokens.
|
|
3634
3715
|
*/
|
|
3635
|
-
|
|
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
|
-
}
|
|
3716
|
+
const normalize_ollama_usage = normalize_local_usage;
|
|
3642
3717
|
const SUPPORTED$4 = /* @__PURE__ */ new Set([
|
|
3643
3718
|
"text",
|
|
3644
3719
|
"tools",
|
|
@@ -3685,15 +3760,11 @@ function translate_lmstudio_effort(effort) {
|
|
|
3685
3760
|
};
|
|
3686
3761
|
}
|
|
3687
3762
|
/**
|
|
3688
|
-
* Normalize LM Studio's raw usage payload into UsageTotals
|
|
3763
|
+
* Normalize LM Studio's raw usage payload into UsageTotals: the shared
|
|
3764
|
+
* local-backend mapper, since LM Studio never reports cache or reasoning
|
|
3765
|
+
* tokens.
|
|
3689
3766
|
*/
|
|
3690
|
-
|
|
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
|
-
}
|
|
3767
|
+
const normalize_lmstudio_usage = normalize_local_usage;
|
|
3697
3768
|
const SUPPORTED$3 = /* @__PURE__ */ new Set([
|
|
3698
3769
|
"text",
|
|
3699
3770
|
"tools",
|
|
@@ -3805,8 +3876,7 @@ const create_openrouter_adapter = (init) => {
|
|
|
3805
3876
|
* `openrouter provider requires a non-empty api_key` engine_config_error.
|
|
3806
3877
|
*/
|
|
3807
3878
|
const create_openrouter_native_adapter = (init) => {
|
|
3808
|
-
const
|
|
3809
|
-
const base_url = typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : DEFAULT_BASE_URL;
|
|
3879
|
+
const { api_key, base_url } = read_dialect_init(init, DEFAULT_BASE_URL);
|
|
3810
3880
|
const http_referer = typeof init["http_referer"] === "string" ? init["http_referer"] : void 0;
|
|
3811
3881
|
const x_title = typeof init["x_title"] === "string" ? init["x_title"] : void 0;
|
|
3812
3882
|
const extra_headers = {};
|
|
@@ -4041,6 +4111,51 @@ function validate_auth_config(config) {
|
|
|
4041
4111
|
}
|
|
4042
4112
|
}
|
|
4043
4113
|
/**
|
|
4114
|
+
* Copy the string-valued entries of `source` onto `target`, dropping any
|
|
4115
|
+
* `undefined` slot (the shape `process.env` and caller overrides both carry).
|
|
4116
|
+
*/
|
|
4117
|
+
function copy_string_entries(target, source) {
|
|
4118
|
+
for (const [key, value] of Object.entries(source)) if (typeof value === "string") target[key] = value;
|
|
4119
|
+
}
|
|
4120
|
+
/**
|
|
4121
|
+
* Seed the base subprocess env before caller overrides and the auth scrub.
|
|
4122
|
+
*
|
|
4123
|
+
* `inherit_env: false` starts from an empty env. Otherwise `'oauth'` inherits
|
|
4124
|
+
* the full `process.env` (the CLI needs HOME plus session-specific vars to
|
|
4125
|
+
* reach the logged-in session); every other mode inherits only the standard
|
|
4126
|
+
* keys needed to resolve the sandbox wrapper and the claude binary on PATH.
|
|
4127
|
+
*/
|
|
4128
|
+
function seed_base_env(config, auth_mode) {
|
|
4129
|
+
const env = {};
|
|
4130
|
+
if (config.inherit_env === false) return env;
|
|
4131
|
+
if (auth_mode === "oauth") {
|
|
4132
|
+
copy_string_entries(env, process.env);
|
|
4133
|
+
return env;
|
|
4134
|
+
}
|
|
4135
|
+
for (const key of STANDARD_ENV_KEYS) {
|
|
4136
|
+
const value = process.env[key];
|
|
4137
|
+
if (typeof value === "string") env[key] = value;
|
|
4138
|
+
}
|
|
4139
|
+
return env;
|
|
4140
|
+
}
|
|
4141
|
+
/**
|
|
4142
|
+
* Apply the auth-mode scrub last, after base and caller env are merged, so
|
|
4143
|
+
* nothing can re-introduce `ANTHROPIC_API_KEY` under `'oauth'`. `'api_key'`
|
|
4144
|
+
* forces the configured key (empty string when absent); `'auto'` forwards a
|
|
4145
|
+
* non-empty configured key and otherwise leaves the inherited env untouched.
|
|
4146
|
+
*/
|
|
4147
|
+
function apply_auth_key(env, config, auth_mode) {
|
|
4148
|
+
if (auth_mode === "oauth") {
|
|
4149
|
+
delete env["ANTHROPIC_API_KEY"];
|
|
4150
|
+
return;
|
|
4151
|
+
}
|
|
4152
|
+
if (auth_mode === "api_key") {
|
|
4153
|
+
env["ANTHROPIC_API_KEY"] = typeof config.api_key === "string" ? config.api_key : "";
|
|
4154
|
+
return;
|
|
4155
|
+
}
|
|
4156
|
+
if (typeof config.api_key === "string" && config.api_key.length > 0) env["ANTHROPIC_API_KEY"] = config.api_key;
|
|
4157
|
+
}
|
|
4158
|
+
/**
|
|
4044
4159
|
* Compose the subprocess environment for one claude CLI invocation.
|
|
4045
4160
|
*
|
|
4046
4161
|
* Regardless of `auth_mode`, seeds the standard process-env keys (PATH,
|
|
@@ -4056,25 +4171,9 @@ function validate_auth_config(config) {
|
|
|
4056
4171
|
* in, so nothing can re-introduce `ANTHROPIC_API_KEY` under `'oauth'`.
|
|
4057
4172
|
*/
|
|
4058
4173
|
function build_env(config, caller_env, auth_mode) {
|
|
4059
|
-
const env =
|
|
4060
|
-
if (
|
|
4061
|
-
|
|
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;
|
|
4174
|
+
const env = seed_base_env(config, auth_mode);
|
|
4175
|
+
if (caller_env !== void 0) copy_string_entries(env, caller_env);
|
|
4176
|
+
apply_auth_key(env, config, auth_mode);
|
|
4078
4177
|
return env;
|
|
4079
4178
|
}
|
|
4080
4179
|
/**
|
|
@@ -5069,6 +5168,21 @@ function decompose_total_cost(total_cost_usd, usage) {
|
|
|
5069
5168
|
return breakdown;
|
|
5070
5169
|
}
|
|
5071
5170
|
/**
|
|
5171
|
+
* Split `total_cost_usd` into a per-turn total for each turn, proportional to
|
|
5172
|
+
* its output tokens, falling back to an equal split when every turn reports
|
|
5173
|
+
* zero output (the proportional weights would otherwise all be zero). The last
|
|
5174
|
+
* turn absorbs the floating-point rounding remainder so the shares sum exactly
|
|
5175
|
+
* to `total_cost_usd`. Caller guarantees a non-empty `turns`.
|
|
5176
|
+
*/
|
|
5177
|
+
function split_total_across_turns(total_cost_usd, turns) {
|
|
5178
|
+
const total_output = turns.reduce((sum, t) => sum + Math.max(0, t.output_tokens), 0);
|
|
5179
|
+
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));
|
|
5180
|
+
const assigned = per_turn.reduce((a, b) => a + b, 0);
|
|
5181
|
+
const last_index = turns.length - 1;
|
|
5182
|
+
per_turn[last_index] = (per_turn[last_index] ?? 0) + (total_cost_usd - assigned);
|
|
5183
|
+
return per_turn;
|
|
5184
|
+
}
|
|
5185
|
+
/**
|
|
5072
5186
|
* Split a call's total cost across its turns, proportional to each turn's
|
|
5073
5187
|
* output tokens, then decompose each turn's share into a `CostBreakdown`.
|
|
5074
5188
|
*
|
|
@@ -5079,29 +5193,8 @@ function decompose_total_cost(total_cost_usd, usage) {
|
|
|
5079
5193
|
*/
|
|
5080
5194
|
function allocate_cost_across_turns(total_cost_usd, turns) {
|
|
5081
5195
|
if (turns.length === 0) return [];
|
|
5082
|
-
const
|
|
5083
|
-
|
|
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;
|
|
5196
|
+
const per_turn = split_total_across_turns(total_cost_usd, turns);
|
|
5197
|
+
return turns.map((t, i) => decompose_total_cost(per_turn[i] ?? 0, t.usage));
|
|
5105
5198
|
}
|
|
5106
5199
|
//#endregion
|
|
5107
5200
|
//#region src/engine/providers/claude_cli/stream_result.ts
|
|
@@ -5797,94 +5890,282 @@ async function run_cli(spawn_runtime, args) {
|
|
|
5797
5890
|
};
|
|
5798
5891
|
}
|
|
5799
5892
|
/**
|
|
5800
|
-
*
|
|
5801
|
-
*
|
|
5802
|
-
*
|
|
5803
|
-
* a spawn runtime plus an in-flight abort-controller registry that
|
|
5804
|
-
* `dispose()` uses to cancel every call still running.
|
|
5893
|
+
* Emit `option_ignored` events for the loop-control options this provider
|
|
5894
|
+
* cannot honor: the CLI runs its own tool loop on the far side of the
|
|
5895
|
+
* subprocess boundary.
|
|
5805
5896
|
*/
|
|
5806
|
-
function
|
|
5807
|
-
const
|
|
5808
|
-
|
|
5809
|
-
|
|
5810
|
-
|
|
5811
|
-
|
|
5812
|
-
|
|
5813
|
-
|
|
5814
|
-
|
|
5815
|
-
|
|
5897
|
+
function emit_ignored_options(opts, trajectory) {
|
|
5898
|
+
const option_ignored = create_option_ignored_dedup(trajectory);
|
|
5899
|
+
if (opts.max_steps !== void 0) option_ignored.emit("max_steps", PROVIDER_NAME);
|
|
5900
|
+
if (opts.tool_error_policy !== void 0) option_ignored.emit("tool_error_policy", PROVIDER_NAME);
|
|
5901
|
+
if (opts.on_tool_approval !== void 0) option_ignored.emit("on_tool_approval", PROVIDER_NAME);
|
|
5902
|
+
if (opts.tool_call_repair_attempts !== void 0) option_ignored.emit("tool_call_repair_attempts", PROVIDER_NAME);
|
|
5903
|
+
if (opts.max_tool_calls_per_step !== void 0) option_ignored.emit("max_tool_calls_per_step", PROVIDER_NAME);
|
|
5904
|
+
}
|
|
5905
|
+
/**
|
|
5906
|
+
* Reject requests the CLI cannot express: multi-turn user history (one user
|
|
5907
|
+
* turn per invocation; continuation goes through `session_id`), and
|
|
5908
|
+
* executable tools under `tool_bridge: 'forbid'`.
|
|
5909
|
+
*/
|
|
5910
|
+
function validate_request(prompt, tools_list, tool_bridge) {
|
|
5911
|
+
if (count_user_messages(prompt) >= 2) throw new provider_capability_error(PROVIDER_NAME, "multi_turn_history", "use provider_options.claude_cli.session_id instead");
|
|
5912
|
+
if (tools_list.length > 0 && tool_bridge === "forbid") {
|
|
5913
|
+
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");
|
|
5914
|
+
}
|
|
5915
|
+
}
|
|
5916
|
+
/**
|
|
5917
|
+
* Record which executable tools the allowlist-only bridge is dropping:
|
|
5918
|
+
* their names still pass through `--allowedTools`, but their execute
|
|
5919
|
+
* closures cannot run on this side of the subprocess boundary.
|
|
5920
|
+
*/
|
|
5921
|
+
function record_allowlist_only_drop(tools_list, tool_bridge, trajectory) {
|
|
5922
|
+
if (tools_list.length === 0 || tool_bridge !== "allowlist_only" || trajectory === void 0) return;
|
|
5923
|
+
const dropped = tools_list.filter((t) => typeof t.execute === "function").map((t) => t.name);
|
|
5924
|
+
trajectory.record({
|
|
5925
|
+
kind: "cli_tool_bridge_allowlist_only",
|
|
5926
|
+
dropped
|
|
5927
|
+
});
|
|
5928
|
+
}
|
|
5929
|
+
/**
|
|
5930
|
+
* Choose the JSON Schema string for `--json-schema`: a typed `opts.schema`
|
|
5931
|
+
* compiles and wins; otherwise a raw `output_json_schema` string passes
|
|
5932
|
+
* through verbatim.
|
|
5933
|
+
*/
|
|
5934
|
+
function resolve_compiled_schema(opts, call_opts) {
|
|
5935
|
+
if (opts.schema !== void 0) return compile_schema(opts.schema);
|
|
5936
|
+
if (typeof call_opts.output_json_schema === "string" && call_opts.output_json_schema.length > 0) return call_opts.output_json_schema;
|
|
5937
|
+
}
|
|
5938
|
+
/**
|
|
5939
|
+
* Wrap `opts.on_chunk` so sync and async callbacks dispatch the same way: a
|
|
5940
|
+
* returned thenable is awaited, any other return is fire-and-done. Returns
|
|
5941
|
+
* `undefined` when the caller isn't streaming, which downstream code uses
|
|
5942
|
+
* as the streaming flag.
|
|
5943
|
+
*/
|
|
5944
|
+
function make_dispatch_chunk(opts) {
|
|
5945
|
+
if (opts.on_chunk === void 0) return void 0;
|
|
5946
|
+
return async (chunk) => {
|
|
5947
|
+
const maybe = opts.on_chunk?.(chunk);
|
|
5948
|
+
if (maybe !== void 0 && typeof maybe.then === "function") await maybe;
|
|
5949
|
+
};
|
|
5950
|
+
}
|
|
5951
|
+
/**
|
|
5952
|
+
* Create this call's AbortController, register it in the adapter's
|
|
5953
|
+
* in-flight set (`dispose()` aborts everything still registered), and
|
|
5954
|
+
* forward the caller's abort signal into it, including one already aborted.
|
|
5955
|
+
* `unlink` undoes both and must run however generate settles, or the
|
|
5956
|
+
* registry would pin settled calls alive.
|
|
5957
|
+
*/
|
|
5958
|
+
function link_caller_abort(in_flight, caller_abort) {
|
|
5959
|
+
const controller = new AbortController();
|
|
5960
|
+
in_flight.add(controller);
|
|
5961
|
+
const on_caller_abort = () => {
|
|
5962
|
+
controller.abort(caller_abort?.reason);
|
|
5963
|
+
};
|
|
5964
|
+
if (caller_abort !== void 0) if (caller_abort.aborted) controller.abort(caller_abort.reason);
|
|
5965
|
+
else caller_abort.addEventListener("abort", on_caller_abort, { once: true });
|
|
5816
5966
|
return {
|
|
5817
|
-
|
|
5818
|
-
|
|
5819
|
-
|
|
5820
|
-
|
|
5821
|
-
|
|
5822
|
-
|
|
5823
|
-
|
|
5824
|
-
|
|
5825
|
-
|
|
5826
|
-
|
|
5827
|
-
|
|
5828
|
-
|
|
5829
|
-
|
|
5830
|
-
|
|
5831
|
-
|
|
5832
|
-
|
|
5833
|
-
|
|
5834
|
-
|
|
5835
|
-
|
|
5836
|
-
|
|
5837
|
-
|
|
5838
|
-
|
|
5839
|
-
|
|
5840
|
-
|
|
5841
|
-
|
|
5842
|
-
|
|
5843
|
-
|
|
5844
|
-
|
|
5845
|
-
|
|
5846
|
-
|
|
5847
|
-
|
|
5848
|
-
|
|
5849
|
-
|
|
5850
|
-
|
|
5851
|
-
|
|
5852
|
-
|
|
5967
|
+
controller,
|
|
5968
|
+
unlink: () => {
|
|
5969
|
+
if (caller_abort !== void 0) caller_abort.removeEventListener("abort", on_caller_abort);
|
|
5970
|
+
in_flight.delete(controller);
|
|
5971
|
+
}
|
|
5972
|
+
};
|
|
5973
|
+
}
|
|
5974
|
+
/**
|
|
5975
|
+
* Open the generate span and the first step span before the CLI spawns, so
|
|
5976
|
+
* their timing brackets the whole subprocess run.
|
|
5977
|
+
*/
|
|
5978
|
+
function open_spans(trajectory, opts, resolved, has_tools, streaming) {
|
|
5979
|
+
return {
|
|
5980
|
+
generate: start_generate_span(trajectory, {
|
|
5981
|
+
model: opts.model ?? resolved.model_id,
|
|
5982
|
+
provider: PROVIDER_NAME,
|
|
5983
|
+
model_id: resolved.model_id,
|
|
5984
|
+
has_tools,
|
|
5985
|
+
has_schema: opts.schema !== void 0,
|
|
5986
|
+
streaming
|
|
5987
|
+
}),
|
|
5988
|
+
first_step: start_step_span(trajectory, 0)
|
|
5989
|
+
};
|
|
5990
|
+
}
|
|
5991
|
+
/**
|
|
5992
|
+
* Failure message for schema validation that exhausted its repair budget.
|
|
5993
|
+
* Zero names the opt-out explicitly so "repair disabled" reads differently
|
|
5994
|
+
* from "repairs tried and failed".
|
|
5995
|
+
*/
|
|
5996
|
+
function repair_exhausted_message(max_repairs) {
|
|
5997
|
+
if (max_repairs === 0) return "schema validation failed and repair is disabled (schema_repair_attempts: 0)";
|
|
5998
|
+
return `schema validation failed after ${String(max_repairs)} repair attempt${max_repairs === 1 ? "" : "s"}`;
|
|
5999
|
+
}
|
|
6000
|
+
/**
|
|
6001
|
+
* Validate the CLI's final text against `schema`, re-invoking the CLI with
|
|
6002
|
+
* a repair prompt on failure, up to `max_repairs` times.
|
|
6003
|
+
*
|
|
6004
|
+
* Each repair resumes the previous attempt's `session_id`, so a snapshot
|
|
6005
|
+
* without one makes validation failure terminal immediately. The returned
|
|
6006
|
+
* snapshot is the one the repair loop settled on; the result must be built
|
|
6007
|
+
* from it, not from the first attempt's.
|
|
6008
|
+
*/
|
|
6009
|
+
async function parse_schema_with_repair(spawn_runtime, base_args, schema, max_repairs, first_parsed, trajectory) {
|
|
6010
|
+
let parsed = first_parsed;
|
|
6011
|
+
let repairs_done = 0;
|
|
6012
|
+
for (;;) {
|
|
6013
|
+
const attempt = await parse_with_schema(schema, parsed.final_text);
|
|
6014
|
+
if (attempt.ok) return {
|
|
6015
|
+
parsed,
|
|
6016
|
+
parsed_content: attempt.value
|
|
6017
|
+
};
|
|
6018
|
+
record_schema_validation_failed(trajectory, {
|
|
6019
|
+
attempt: repairs_done === 0 ? "initial" : "repair",
|
|
6020
|
+
schema_issues: format_schema_issues(attempt.issues),
|
|
6021
|
+
raw_text: parsed.final_text
|
|
6022
|
+
});
|
|
6023
|
+
if (repairs_done >= max_repairs) throw new schema_validation_error(repair_exhausted_message(max_repairs), attempt.issues, parsed.final_text);
|
|
6024
|
+
const repair_session_id = parsed.session_id;
|
|
6025
|
+
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);
|
|
6026
|
+
parsed = (await run_cli(spawn_runtime, {
|
|
6027
|
+
...base_args,
|
|
6028
|
+
stdin_text: build_repair_prompt_text(attempt.issues),
|
|
6029
|
+
call_opts: {
|
|
6030
|
+
...base_args.call_opts,
|
|
6031
|
+
session_id: repair_session_id
|
|
6032
|
+
}
|
|
6033
|
+
})).parsed;
|
|
6034
|
+
repairs_done += 1;
|
|
6035
|
+
}
|
|
6036
|
+
}
|
|
6037
|
+
/**
|
|
6038
|
+
* Resolve the call's final parse snapshot and validated content. Without a
|
|
6039
|
+
* schema the first snapshot is final and content stays undefined; with one,
|
|
6040
|
+
* the repair loop takes over.
|
|
6041
|
+
*/
|
|
6042
|
+
async function resolve_content(spawn_runtime, base_args, opts, first_parsed) {
|
|
6043
|
+
if (opts.schema === void 0) return {
|
|
6044
|
+
parsed: first_parsed,
|
|
6045
|
+
parsed_content: void 0
|
|
6046
|
+
};
|
|
6047
|
+
return parse_schema_with_repair(spawn_runtime, base_args, opts.schema, opts.schema_repair_attempts ?? 1, first_parsed, base_args.trajectory);
|
|
6048
|
+
}
|
|
6049
|
+
/**
|
|
6050
|
+
* Assemble the `build_generate_result` input, attaching the schema and its
|
|
6051
|
+
* validated value only when structured output was requested.
|
|
6052
|
+
*/
|
|
6053
|
+
function assemble_result$1(parsed, resolved, schema, parsed_content) {
|
|
6054
|
+
return build_generate_result({
|
|
6055
|
+
parsed,
|
|
6056
|
+
resolved,
|
|
6057
|
+
...schema !== void 0 ? { schema } : {},
|
|
6058
|
+
...parsed_content !== void 0 ? { parsed_content } : {}
|
|
6059
|
+
});
|
|
6060
|
+
}
|
|
6061
|
+
/**
|
|
6062
|
+
* Forward the CLI's per-turn provider-reported costs into the trajectory.
|
|
6063
|
+
*/
|
|
6064
|
+
function record_provider_costs(trajectory, steps) {
|
|
6065
|
+
for (const step of steps) if (step.cost !== void 0) record_cost(trajectory, step.index, step.cost, "provider_reported");
|
|
6066
|
+
}
|
|
6067
|
+
/**
|
|
6068
|
+
* Close the eagerly-opened first step span against the steps the CLI
|
|
6069
|
+
* reported, then emit paired open/close spans for steps 1..n; those arrive
|
|
6070
|
+
* all at once on process close, so their spans carry usage, not timing.
|
|
6071
|
+
* Clears `spans.first_step` so the error path cannot close it twice.
|
|
6072
|
+
*/
|
|
6073
|
+
function close_step_spans(trajectory, spans, steps) {
|
|
6074
|
+
if (steps.length === 0) {
|
|
6075
|
+
end_step_span(trajectory, spans.first_step, {});
|
|
6076
|
+
spans.first_step = void 0;
|
|
6077
|
+
return;
|
|
6078
|
+
}
|
|
6079
|
+
const head = steps[0];
|
|
6080
|
+
if (head !== void 0) end_step_span(trajectory, spans.first_step, {
|
|
6081
|
+
usage: head.usage,
|
|
6082
|
+
finish_reason: head.finish_reason
|
|
6083
|
+
});
|
|
6084
|
+
spans.first_step = void 0;
|
|
6085
|
+
for (let i = 1; i < steps.length; i += 1) {
|
|
6086
|
+
const s = steps[i];
|
|
6087
|
+
if (s === void 0) continue;
|
|
6088
|
+
end_step_span(trajectory, start_step_span(trajectory, i), {
|
|
6089
|
+
usage: s.usage,
|
|
6090
|
+
finish_reason: s.finish_reason
|
|
6091
|
+
});
|
|
6092
|
+
}
|
|
6093
|
+
}
|
|
6094
|
+
/**
|
|
6095
|
+
* Emit the terminal `finish` chunk streaming callers expect; the CLI stream
|
|
6096
|
+
* has no equivalent event, so it is synthesized from the final result.
|
|
6097
|
+
*/
|
|
6098
|
+
async function dispatch_finish(dispatch_chunk, result) {
|
|
6099
|
+
if (dispatch_chunk === void 0) return;
|
|
6100
|
+
await dispatch_chunk({
|
|
6101
|
+
kind: "finish",
|
|
6102
|
+
finish_reason: result.finish_reason,
|
|
6103
|
+
usage: result.usage
|
|
6104
|
+
});
|
|
6105
|
+
}
|
|
6106
|
+
/**
|
|
6107
|
+
* Close whatever spans are still open with the failure message. A failure
|
|
6108
|
+
* after the step spans already closed (e.g. in the finish-chunk dispatch)
|
|
6109
|
+
* closes only the generate span.
|
|
6110
|
+
*/
|
|
6111
|
+
function close_spans_on_error(trajectory, spans, err) {
|
|
6112
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
6113
|
+
if (spans.first_step !== void 0) {
|
|
6114
|
+
end_step_span(trajectory, spans.first_step, { error: message });
|
|
6115
|
+
spans.first_step = void 0;
|
|
6116
|
+
}
|
|
6117
|
+
end_generate_span(trajectory, spans.generate, { error: message });
|
|
6118
|
+
}
|
|
6119
|
+
/**
|
|
6120
|
+
* Build a claude_cli `ExternalAgentAdapter` bound to one config.
|
|
6121
|
+
*
|
|
6122
|
+
* Validates the auth config eagerly, before any call is made, and wires up
|
|
6123
|
+
* a spawn runtime plus an in-flight abort-controller registry that
|
|
6124
|
+
* `dispose()` uses to cancel every call still running.
|
|
6125
|
+
*/
|
|
6126
|
+
function create_claude_cli_adapter(init) {
|
|
6127
|
+
const config = init;
|
|
6128
|
+
validate_auth_config(config);
|
|
6129
|
+
const spawn_runtime = create_spawn_runtime();
|
|
6130
|
+
let disposed = false;
|
|
6131
|
+
const in_flight = /* @__PURE__ */ new Set();
|
|
6132
|
+
const resolved_binary = typeof config.binary === "string" && config.binary.length > 0 ? config.binary : CLI_BINARY_DEFAULT;
|
|
6133
|
+
const resolved_auth_mode = config.auth_mode ?? "auto";
|
|
6134
|
+
const resolved_startup_timeout = config.startup_timeout_ms ?? 12e4;
|
|
6135
|
+
const resolved_stall_timeout = config.stall_timeout_ms ?? 3e5;
|
|
6136
|
+
return {
|
|
6137
|
+
kind: "external",
|
|
6138
|
+
name: PROVIDER_NAME,
|
|
6139
|
+
supports: (capability) => SUPPORTED.has(capability),
|
|
6140
|
+
async generate(opts, resolved) {
|
|
6141
|
+
if (disposed) throw new engine_disposed_error();
|
|
6142
|
+
if (opts.abort?.aborted === true) throw new aborted_error("aborted", { reason: opts.abort.reason });
|
|
6143
|
+
const trajectory = with_timestamps(opts.trajectory);
|
|
6144
|
+
const call_opts = extract_call_opts(opts);
|
|
6145
|
+
emit_ignored_options(opts, trajectory);
|
|
6146
|
+
const tools_list = opts.tools ?? [];
|
|
6147
|
+
const tool_bridge = call_opts.tool_bridge ?? "allowlist_only";
|
|
6148
|
+
validate_request(opts.prompt, tools_list, tool_bridge);
|
|
6149
|
+
const merged_allowed_tools = merge_allowed_tools(call_opts.allowed_tools, tools_list.map((t) => t.name));
|
|
6150
|
+
record_allowlist_only_drop(tools_list, tool_bridge, trajectory);
|
|
6151
|
+
const merged_system = merge_system(opts.system ?? extract_system_text(opts.prompt), call_opts.append_system_prompt);
|
|
6152
|
+
const env = build_env(config, call_opts.env, resolved_auth_mode);
|
|
5853
6153
|
Object.assign(env, effort_env_for_claude_cli(opts.effort));
|
|
5854
6154
|
const sandbox_plan = build_sandbox_plan(resolved_binary, config.sandbox);
|
|
5855
|
-
const
|
|
5856
|
-
const
|
|
5857
|
-
|
|
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);
|
|
6155
|
+
const dispatch_chunk = make_dispatch_chunk(opts);
|
|
6156
|
+
const { controller, unlink } = link_caller_abort(in_flight, opts.abort);
|
|
6157
|
+
const spans = open_spans(trajectory, opts, resolved, tools_list.length > 0, dispatch_chunk !== void 0);
|
|
5877
6158
|
const base_args = {
|
|
5878
6159
|
model_id: resolved.model_id,
|
|
5879
|
-
stdin_text,
|
|
6160
|
+
stdin_text: extract_prompt_text(opts.prompt),
|
|
5880
6161
|
merged_system,
|
|
5881
6162
|
merged_allowed_tools,
|
|
5882
6163
|
call_opts,
|
|
5883
|
-
compiled_schema,
|
|
6164
|
+
compiled_schema: resolve_compiled_schema(opts, call_opts),
|
|
5884
6165
|
env,
|
|
5885
6166
|
spawn_cmd: sandbox_plan.spawn_cmd,
|
|
5886
6167
|
prefix_args: sandbox_plan.prefix_args,
|
|
5887
|
-
cwd,
|
|
6168
|
+
cwd: config.default_cwd,
|
|
5888
6169
|
startup_timeout_ms: resolved_startup_timeout,
|
|
5889
6170
|
stall_timeout_ms: resolved_stall_timeout,
|
|
5890
6171
|
abort: controller.signal,
|
|
@@ -5892,86 +6173,23 @@ function create_claude_cli_adapter(init) {
|
|
|
5892
6173
|
trajectory
|
|
5893
6174
|
};
|
|
5894
6175
|
try {
|
|
5895
|
-
|
|
5896
|
-
|
|
5897
|
-
|
|
5898
|
-
|
|
5899
|
-
|
|
5900
|
-
|
|
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, {
|
|
6176
|
+
const first_outcome = await run_cli(spawn_runtime, base_args);
|
|
6177
|
+
const { parsed, parsed_content } = await resolve_content(spawn_runtime, base_args, opts, first_outcome.parsed);
|
|
6178
|
+
const result = assemble_result$1(parsed, resolved, opts.schema, parsed_content);
|
|
6179
|
+
record_provider_costs(trajectory, result.steps);
|
|
6180
|
+
close_step_spans(trajectory, spans, result.steps);
|
|
6181
|
+
end_generate_span(trajectory, spans.generate, {
|
|
5954
6182
|
usage: result.usage,
|
|
5955
6183
|
finish_reason: result.finish_reason,
|
|
5956
6184
|
model_resolved: result.model_resolved
|
|
5957
6185
|
});
|
|
5958
|
-
|
|
5959
|
-
kind: "finish",
|
|
5960
|
-
finish_reason: result.finish_reason,
|
|
5961
|
-
usage: result.usage
|
|
5962
|
-
});
|
|
6186
|
+
await dispatch_finish(dispatch_chunk, result);
|
|
5963
6187
|
return result;
|
|
5964
6188
|
} catch (err) {
|
|
5965
|
-
|
|
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 });
|
|
6189
|
+
close_spans_on_error(trajectory, spans, err);
|
|
5971
6190
|
throw err;
|
|
5972
6191
|
} finally {
|
|
5973
|
-
|
|
5974
|
-
in_flight.delete(controller);
|
|
6192
|
+
unlink();
|
|
5975
6193
|
}
|
|
5976
6194
|
},
|
|
5977
6195
|
async dispose() {
|
|
@@ -6170,6 +6388,36 @@ const PARAMETER_RE = /<parameter=([^>\s]+)>([\s\S]*?)<\/parameter>/g;
|
|
|
6170
6388
|
const FENCE_RE = /```([^\n]*)\n([\s\S]*?)```/g;
|
|
6171
6389
|
const EMPTY_BLOCK_RE = /<tool_call>\s*<\/tool_call>/g;
|
|
6172
6390
|
/**
|
|
6391
|
+
* From `open` at an opening quote, return the index of the matching closing
|
|
6392
|
+
* quote, honoring backslash escapes. Returns `text.length` for an unterminated
|
|
6393
|
+
* string, so the caller's scan runs off the end and yields no object.
|
|
6394
|
+
*/
|
|
6395
|
+
function skip_json_string(text, open) {
|
|
6396
|
+
let escaped = false;
|
|
6397
|
+
for (let i = open + 1; i < text.length; i += 1) {
|
|
6398
|
+
const ch = text[i];
|
|
6399
|
+
if (escaped) escaped = false;
|
|
6400
|
+
else if (ch === "\\") escaped = true;
|
|
6401
|
+
else if (ch === "\"") return i;
|
|
6402
|
+
}
|
|
6403
|
+
return text.length;
|
|
6404
|
+
}
|
|
6405
|
+
/**
|
|
6406
|
+
* Parse `text.slice(start, end)` as JSON, pairing the value with `end`, or
|
|
6407
|
+
* `undefined` when the slice is not valid JSON. JSON.parse is the sole
|
|
6408
|
+
* semantic authority; the scanner only fixed the extent.
|
|
6409
|
+
*/
|
|
6410
|
+
function parse_json_extent(text, start, end) {
|
|
6411
|
+
try {
|
|
6412
|
+
return {
|
|
6413
|
+
value: JSON.parse(text.slice(start, end)),
|
|
6414
|
+
end
|
|
6415
|
+
};
|
|
6416
|
+
} catch {
|
|
6417
|
+
return;
|
|
6418
|
+
}
|
|
6419
|
+
}
|
|
6420
|
+
/**
|
|
6173
6421
|
* Find the extent of one balanced JSON object starting at `start_index`
|
|
6174
6422
|
* (which must point at `{`), respecting strings and escapes so braces inside
|
|
6175
6423
|
* string values do not affect the depth count. The scanner only finds the
|
|
@@ -6178,31 +6426,13 @@ const EMPTY_BLOCK_RE = /<tool_call>\s*<\/tool_call>/g;
|
|
|
6178
6426
|
function scan_balanced_json(text, start_index) {
|
|
6179
6427
|
if (text[start_index] !== "{") return void 0;
|
|
6180
6428
|
let depth = 0;
|
|
6181
|
-
let in_string = false;
|
|
6182
|
-
let escaped = false;
|
|
6183
6429
|
for (let i = start_index; i < text.length; i += 1) {
|
|
6184
6430
|
const ch = text[i];
|
|
6185
|
-
if (
|
|
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;
|
|
6431
|
+
if (ch === "\"") i = skip_json_string(text, i);
|
|
6192
6432
|
else if (ch === "{") depth += 1;
|
|
6193
6433
|
else if (ch === "}") {
|
|
6194
6434
|
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
|
-
}
|
|
6435
|
+
if (depth === 0) return parse_json_extent(text, start_index, i + 1);
|
|
6206
6436
|
}
|
|
6207
6437
|
}
|
|
6208
6438
|
}
|
|
@@ -6281,6 +6511,38 @@ function scan_qwen_functions(inner_start, inner) {
|
|
|
6281
6511
|
return out;
|
|
6282
6512
|
}
|
|
6283
6513
|
/**
|
|
6514
|
+
* Parse a hermes `<tool_call>{...}</tool_call>` payload into a candidate. The
|
|
6515
|
+
* block's whole extent (`span`) is used so a later strip removes the wrapper
|
|
6516
|
+
* too. Returns `undefined` when the inner object is not call-shaped.
|
|
6517
|
+
*/
|
|
6518
|
+
function scan_hermes_block(text, inner_start, span) {
|
|
6519
|
+
const brace = text.indexOf("{", inner_start);
|
|
6520
|
+
const scanned = brace === -1 ? void 0 : scan_balanced_json(text, brace);
|
|
6521
|
+
const shaped = scanned === void 0 ? void 0 : call_shape(scanned.value);
|
|
6522
|
+
if (shaped === void 0) return void 0;
|
|
6523
|
+
return {
|
|
6524
|
+
name: shaped.name,
|
|
6525
|
+
args: shaped.args,
|
|
6526
|
+
format: "hermes",
|
|
6527
|
+
span
|
|
6528
|
+
};
|
|
6529
|
+
}
|
|
6530
|
+
/**
|
|
6531
|
+
* Parse one located `<tool_call>` block's inner content into candidates,
|
|
6532
|
+
* dispatching on whether it opens with a qwen `<function=` tag or a hermes
|
|
6533
|
+
* `{` object.
|
|
6534
|
+
*/
|
|
6535
|
+
function scan_block_candidates(text, inner_start, close, span) {
|
|
6536
|
+
const inner = text.slice(inner_start, close);
|
|
6537
|
+
const trimmed = inner.trim();
|
|
6538
|
+
if (trimmed.startsWith("<function=")) return scan_qwen_functions(inner_start, inner);
|
|
6539
|
+
if (trimmed.startsWith("{")) {
|
|
6540
|
+
const candidate = scan_hermes_block(text, inner_start, span);
|
|
6541
|
+
return candidate === void 0 ? [] : [candidate];
|
|
6542
|
+
}
|
|
6543
|
+
return [];
|
|
6544
|
+
}
|
|
6545
|
+
/**
|
|
6284
6546
|
* Scan `<tool_call>` blocks for hermes and qwen_xml candidates.
|
|
6285
6547
|
*
|
|
6286
6548
|
* Every block extent (accepted, rejected, or unterminated) is returned as a
|
|
@@ -6302,28 +6564,13 @@ function scan_tool_call_blocks(text) {
|
|
|
6302
6564
|
break;
|
|
6303
6565
|
}
|
|
6304
6566
|
const block_end = close + 12;
|
|
6305
|
-
|
|
6567
|
+
const span = {
|
|
6306
6568
|
start: open,
|
|
6307
6569
|
end: block_end
|
|
6308
|
-
}
|
|
6570
|
+
};
|
|
6571
|
+
masks.push(span);
|
|
6309
6572
|
const inner_start = open + 11;
|
|
6310
|
-
|
|
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
|
-
}
|
|
6573
|
+
candidates.push(...scan_block_candidates(text, inner_start, close, span));
|
|
6327
6574
|
pos = block_end;
|
|
6328
6575
|
}
|
|
6329
6576
|
return {
|
|
@@ -6753,431 +7000,524 @@ async function apply_prepare_step(config, step_index) {
|
|
|
6753
7000
|
return replacement;
|
|
6754
7001
|
}
|
|
6755
7002
|
/**
|
|
6756
|
-
*
|
|
6757
|
-
*
|
|
6758
|
-
* Each iteration invokes the transport seam once, salvages text-embedded
|
|
6759
|
-
* tool calls when eligible, clamps to `max_tool_calls_per_step`, executes
|
|
6760
|
-
* calls sequentially under the header invariants, and feeds results back
|
|
6761
|
-
* until the model stops, a terminal tool fires, or `max_steps` is reached.
|
|
7003
|
+
* Assemble the loop's return value from the accumulated steps and records.
|
|
6762
7004
|
*/
|
|
6763
|
-
|
|
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;
|
|
6770
|
-
const tool_map = /* @__PURE__ */ new Map();
|
|
6771
|
-
for (const t of config.tools) tool_map.set(t.name, t);
|
|
6772
|
-
while (true) {
|
|
6773
|
-
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
|
-
}
|
|
7152
|
-
step_index += 1;
|
|
7153
|
-
}
|
|
7005
|
+
function loop_result(acc, text, finish_reason, max_steps_reached) {
|
|
7154
7006
|
return {
|
|
7155
7007
|
text,
|
|
7156
|
-
steps,
|
|
7157
|
-
tool_calls:
|
|
7008
|
+
steps: acc.steps,
|
|
7009
|
+
tool_calls: acc.tool_calls,
|
|
7158
7010
|
finish_reason,
|
|
7159
7011
|
max_steps_reached
|
|
7160
7012
|
};
|
|
7161
7013
|
}
|
|
7162
|
-
//#endregion
|
|
7163
|
-
//#region src/engine/generate.ts
|
|
7164
7014
|
/**
|
|
7165
|
-
*
|
|
7166
|
-
*
|
|
7167
|
-
*
|
|
7168
|
-
* A non-empty string `system` is prepended as a system message. A string
|
|
7169
|
-
* `prompt` becomes a single user message; a `Message[]` prompt is copied
|
|
7170
|
-
* through element-by-element so the returned array can be mutated without
|
|
7171
|
-
* affecting the caller's.
|
|
7015
|
+
* Invoke the transport seam once for one turn, bracketed by request_sent /
|
|
7016
|
+
* response_received events. A thrown invoke (or prepare_step) closes the
|
|
7017
|
+
* step span with the error before rethrowing.
|
|
7172
7018
|
*/
|
|
7173
|
-
function
|
|
7174
|
-
|
|
7175
|
-
|
|
7176
|
-
|
|
7177
|
-
|
|
7178
|
-
|
|
7179
|
-
|
|
7180
|
-
|
|
7019
|
+
async function invoke_turn(config, step_index, step_span) {
|
|
7020
|
+
try {
|
|
7021
|
+
const request_messages = await apply_prepare_step(config, step_index);
|
|
7022
|
+
config.trajectory?.record({
|
|
7023
|
+
kind: "request_sent",
|
|
7024
|
+
step_index
|
|
7025
|
+
});
|
|
7026
|
+
const turn = await config.invoke_once({
|
|
7027
|
+
step_index,
|
|
7028
|
+
messages: request_messages,
|
|
7029
|
+
tools: config.tools,
|
|
7030
|
+
abort: config.abort,
|
|
7031
|
+
stream: config.stream
|
|
7032
|
+
});
|
|
7033
|
+
config.trajectory?.record({
|
|
7034
|
+
kind: "response_received",
|
|
7035
|
+
step_index,
|
|
7036
|
+
output_tokens: turn.usage.output_tokens,
|
|
7037
|
+
finish_reason: turn.finish_reason
|
|
7038
|
+
});
|
|
7039
|
+
return turn;
|
|
7040
|
+
} catch (err) {
|
|
7041
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
7042
|
+
end_step_span(config.trajectory, step_span, { error: message });
|
|
7043
|
+
throw err;
|
|
7044
|
+
}
|
|
7045
|
+
}
|
|
7046
|
+
/**
|
|
7047
|
+
* Salvage text-embedded tool calls from an eligible turn.
|
|
7048
|
+
*
|
|
7049
|
+
* A turn that "stopped" with plain text may hold a call the runtime failed
|
|
7050
|
+
* to parse into tool_calls; salvage before deciding the step ends the loop.
|
|
7051
|
+
* History gets the stripped text + structured parts (raw markup in history
|
|
7052
|
+
* would teach the model the text format works, and a tool result without a
|
|
7053
|
+
* matching call part is rejected by OpenAI-compatible APIs);
|
|
7054
|
+
* StepRecord.text keeps the raw text for debugging. Returns the native view
|
|
7055
|
+
* unchanged when the turn is ineligible or nothing salvages.
|
|
7056
|
+
*/
|
|
7057
|
+
async function salvage_turn(config, turn, tool_map, step_index) {
|
|
7058
|
+
const native = {
|
|
7059
|
+
calls: turn.tool_calls,
|
|
7060
|
+
history_text: turn.text,
|
|
7061
|
+
formats: /* @__PURE__ */ new Map(),
|
|
7062
|
+
outcome: void 0
|
|
7063
|
+
};
|
|
7064
|
+
const budget = config.salvage_budget;
|
|
7065
|
+
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;
|
|
7066
|
+
const outcome = await salvage_tool_calls(turn.text, tool_map);
|
|
7067
|
+
if (outcome === void 0) return native;
|
|
7068
|
+
budget.remaining -= 1;
|
|
7069
|
+
const formats = /* @__PURE__ */ new Map();
|
|
7070
|
+
const calls = outcome.calls.map((c, n) => {
|
|
7071
|
+
const id = `salvaged_${step_index}_${n}`;
|
|
7072
|
+
formats.set(id, c.format);
|
|
7073
|
+
return {
|
|
7074
|
+
id,
|
|
7075
|
+
name: c.name,
|
|
7076
|
+
input: c.input
|
|
7077
|
+
};
|
|
7078
|
+
});
|
|
7079
|
+
record_tool_call_salvaged(config.trajectory, {
|
|
7080
|
+
step_index,
|
|
7081
|
+
calls: calls.map((c) => ({
|
|
7082
|
+
tool_call_id: c.id,
|
|
7083
|
+
name: c.name,
|
|
7084
|
+
format: formats.get(c.id) ?? "json"
|
|
7085
|
+
})),
|
|
7086
|
+
raw_text: turn.text
|
|
7087
|
+
});
|
|
7088
|
+
await dispatch_salvaged_call_chunks(config, calls, step_index);
|
|
7089
|
+
return {
|
|
7090
|
+
calls,
|
|
7091
|
+
history_text: outcome.stripped_text,
|
|
7092
|
+
formats,
|
|
7093
|
+
outcome
|
|
7094
|
+
};
|
|
7095
|
+
}
|
|
7096
|
+
/**
|
|
7097
|
+
* Mirror the native stream for salvaged calls, which emits start/end for
|
|
7098
|
+
* every call the model attempted, including ones the per-step clamp later
|
|
7099
|
+
* drops.
|
|
7100
|
+
*/
|
|
7101
|
+
async function dispatch_salvaged_call_chunks(config, calls, step_index) {
|
|
7102
|
+
if (config.dispatch_chunk === void 0) return;
|
|
7103
|
+
for (const c of calls) {
|
|
7104
|
+
await config.dispatch_chunk({
|
|
7105
|
+
kind: "tool_call_start",
|
|
7106
|
+
id: c.id,
|
|
7107
|
+
name: c.name,
|
|
7108
|
+
step_index
|
|
7109
|
+
});
|
|
7110
|
+
await config.dispatch_chunk({
|
|
7111
|
+
kind: "tool_call_end",
|
|
7112
|
+
id: c.id,
|
|
7113
|
+
input: c.input,
|
|
7114
|
+
step_index
|
|
7115
|
+
});
|
|
7116
|
+
}
|
|
7117
|
+
}
|
|
7118
|
+
/**
|
|
7119
|
+
* Apply `max_tool_calls_per_step` to a turn's calls, native and salvaged
|
|
7120
|
+
* alike. Dropped calls never reach history; the model re-issues them on a
|
|
7121
|
+
* later turn if it still wants them.
|
|
7122
|
+
*/
|
|
7123
|
+
function clamp_calls(config, calls, step_index) {
|
|
7124
|
+
const cap = config.max_tool_calls_per_step;
|
|
7125
|
+
if (cap === void 0 || calls.length <= cap) return {
|
|
7126
|
+
kept: calls,
|
|
7127
|
+
dropped: []
|
|
7128
|
+
};
|
|
7129
|
+
const kept = calls.slice(0, cap);
|
|
7130
|
+
const dropped = calls.slice(cap);
|
|
7131
|
+
record_tool_calls_dropped(config.trajectory, {
|
|
7132
|
+
step_index,
|
|
7133
|
+
max_tool_calls_per_step: cap,
|
|
7134
|
+
kept: kept.length,
|
|
7135
|
+
dropped: dropped.map((d) => ({
|
|
7136
|
+
tool_call_id: d.id,
|
|
7137
|
+
name: d.name
|
|
7138
|
+
}))
|
|
7139
|
+
});
|
|
7140
|
+
return {
|
|
7141
|
+
kept,
|
|
7142
|
+
dropped
|
|
7143
|
+
};
|
|
7144
|
+
}
|
|
7145
|
+
/**
|
|
7146
|
+
* Record a call that resolved to an error on the feed-back path: the error
|
|
7147
|
+
* record, the fed-back error tool_result message, the tool_call trajectory
|
|
7148
|
+
* event, and the stream chunk, in that order. Throw-policy paths raise
|
|
7149
|
+
* before reaching here.
|
|
7150
|
+
*/
|
|
7151
|
+
async function record_failed_call(ctx, call, message, timing) {
|
|
7152
|
+
const record = {
|
|
7153
|
+
id: call.id,
|
|
7154
|
+
name: call.name,
|
|
7155
|
+
input: call.input,
|
|
7156
|
+
error: { message },
|
|
7157
|
+
duration_ms: timing.duration_ms,
|
|
7158
|
+
started_at: timing.started_at
|
|
7159
|
+
};
|
|
7160
|
+
ctx.records.push(record);
|
|
7161
|
+
ctx.all_records.push(record);
|
|
7162
|
+
ctx.feed.push(build_tool_result_message(call.id, call.name, { error: message }));
|
|
7163
|
+
record_tool_call(ctx.config.trajectory, {
|
|
7164
|
+
step_index: ctx.step_index,
|
|
7165
|
+
name: call.name,
|
|
7166
|
+
tool_call_id: call.id,
|
|
7167
|
+
input: call.input,
|
|
7168
|
+
duration_ms: timing.duration_ms,
|
|
7169
|
+
error: { message }
|
|
7170
|
+
});
|
|
7171
|
+
await dispatch_tool_result_chunk(ctx.config.dispatch_chunk, ctx.step_index, call.id, void 0, { message });
|
|
7172
|
+
}
|
|
7173
|
+
/**
|
|
7174
|
+
* Record a call that never reached execution (the final-turn max_steps skip,
|
|
7175
|
+
* the per-step-cap drop): an error record and a tool_result stream chunk,
|
|
7176
|
+
* but no tool_call trajectory event, no execution, and no fed-back tool
|
|
7177
|
+
* message.
|
|
7178
|
+
*/
|
|
7179
|
+
async function record_unexecuted_call(ctx, call, message) {
|
|
7180
|
+
const record = {
|
|
7181
|
+
id: call.id,
|
|
7182
|
+
name: call.name,
|
|
7183
|
+
input: call.input,
|
|
7184
|
+
error: { message },
|
|
7185
|
+
duration_ms: 0,
|
|
7186
|
+
started_at: Date.now()
|
|
7187
|
+
};
|
|
7188
|
+
ctx.records.push(record);
|
|
7189
|
+
ctx.all_records.push(record);
|
|
7190
|
+
await dispatch_tool_result_chunk(ctx.config.dispatch_chunk, ctx.step_index, call.id, void 0, { message });
|
|
7191
|
+
}
|
|
7192
|
+
/**
|
|
7193
|
+
* Look up the named tool and validate the raw input against its schema.
|
|
7194
|
+
* Returns undefined when the failure was recorded on the feed-back path;
|
|
7195
|
+
* throws under 'throw' policy for an unknown tool.
|
|
7196
|
+
*/
|
|
7197
|
+
async function resolve_tool_and_input(ctx, raw_call) {
|
|
7198
|
+
const tool = ctx.tool_map.get(raw_call.name);
|
|
7199
|
+
if (tool === void 0) {
|
|
7200
|
+
const err_message = `unknown tool '${raw_call.name}'`;
|
|
7201
|
+
if (ctx.config.tool_error_policy === "throw") {
|
|
7202
|
+
const thrown = new tool_error(err_message, {
|
|
7203
|
+
tool_name: raw_call.name,
|
|
7204
|
+
tool_call_id: raw_call.id,
|
|
7205
|
+
cause: new Error(err_message)
|
|
7206
|
+
});
|
|
7207
|
+
end_step_span(ctx.config.trajectory, ctx.step_span, { error: err_message });
|
|
7208
|
+
throw thrown;
|
|
7209
|
+
}
|
|
7210
|
+
await record_failed_call(ctx, raw_call, err_message, {
|
|
7211
|
+
duration_ms: 0,
|
|
7212
|
+
started_at: Date.now()
|
|
7213
|
+
});
|
|
7214
|
+
return;
|
|
7215
|
+
}
|
|
7216
|
+
const validation = await validate_tool_input(tool, raw_call.input);
|
|
7217
|
+
if (!validation.ok) {
|
|
7218
|
+
await record_failed_call(ctx, raw_call, validation.message, {
|
|
7219
|
+
duration_ms: 0,
|
|
7220
|
+
started_at: Date.now()
|
|
7221
|
+
});
|
|
7222
|
+
return;
|
|
7223
|
+
}
|
|
7224
|
+
return {
|
|
7225
|
+
tool,
|
|
7226
|
+
input: validation.value
|
|
7227
|
+
};
|
|
7228
|
+
}
|
|
7229
|
+
/**
|
|
7230
|
+
* Resolve whether the call may execute (see request_approval). A denial
|
|
7231
|
+
* either throws ('throw' policy) or records the denied call and returns
|
|
7232
|
+
* false; an abort or handler rejection closes the step span and rethrows.
|
|
7233
|
+
*/
|
|
7234
|
+
async function approve_call(ctx, tool, input, raw_call) {
|
|
7235
|
+
let approved;
|
|
7236
|
+
try {
|
|
7237
|
+
approved = await request_approval(tool, input, ctx.step_index, raw_call.id, ctx.config.abort, ctx.config.on_tool_approval, ctx.config.trajectory);
|
|
7238
|
+
} catch (err) {
|
|
7239
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
7240
|
+
end_step_span(ctx.config.trajectory, ctx.step_span, { error: message });
|
|
7241
|
+
throw err;
|
|
7242
|
+
}
|
|
7243
|
+
if (approved) return true;
|
|
7244
|
+
const denied_message = "tool_approval_denied";
|
|
7245
|
+
if (ctx.config.tool_error_policy === "throw") {
|
|
7246
|
+
const thrown = new tool_approval_denied_error(`tool '${tool.name}' approval denied`, {
|
|
7247
|
+
tool_name: tool.name,
|
|
7248
|
+
step_index: ctx.step_index,
|
|
7249
|
+
tool_call_id: raw_call.id
|
|
7250
|
+
});
|
|
7251
|
+
end_step_span(ctx.config.trajectory, ctx.step_span, { error: denied_message });
|
|
7252
|
+
throw thrown;
|
|
7253
|
+
}
|
|
7254
|
+
await record_failed_call(ctx, {
|
|
7255
|
+
id: raw_call.id,
|
|
7256
|
+
name: tool.name,
|
|
7257
|
+
input
|
|
7258
|
+
}, denied_message, {
|
|
7259
|
+
duration_ms: 0,
|
|
7260
|
+
started_at: Date.now()
|
|
7261
|
+
});
|
|
7262
|
+
return false;
|
|
7263
|
+
}
|
|
7264
|
+
/**
|
|
7265
|
+
* Execute an approved call and record the outcome: a success record with
|
|
7266
|
+
* output (flipping terminal_fired for an ends_turn tool), or the error path
|
|
7267
|
+
* via record_execute_failure.
|
|
7268
|
+
*/
|
|
7269
|
+
async function execute_approved_call(ctx, tool, input, raw_call) {
|
|
7270
|
+
const started_at = Date.now();
|
|
7271
|
+
const tool_ctx = {
|
|
7272
|
+
abort: ctx.config.abort,
|
|
7273
|
+
tool_call_id: raw_call.id,
|
|
7274
|
+
step_index: ctx.step_index,
|
|
7275
|
+
...ctx.config.trajectory !== void 0 ? { trajectory: ctx.config.trajectory } : {}
|
|
7276
|
+
};
|
|
7277
|
+
let output;
|
|
7278
|
+
let err_message;
|
|
7279
|
+
let thrown;
|
|
7280
|
+
try {
|
|
7281
|
+
const execute = tool.execute;
|
|
7282
|
+
const maybe = execute(input, tool_ctx);
|
|
7283
|
+
output = maybe instanceof Promise ? await maybe : maybe;
|
|
7284
|
+
} catch (err) {
|
|
7285
|
+
thrown = err;
|
|
7286
|
+
err_message = serialize_error(err);
|
|
7287
|
+
}
|
|
7288
|
+
const duration_ms = Date.now() - started_at;
|
|
7289
|
+
if (thrown !== void 0) {
|
|
7290
|
+
await record_execute_failure(ctx, tool, raw_call, input, {
|
|
7291
|
+
thrown,
|
|
7292
|
+
err_message,
|
|
7293
|
+
duration_ms,
|
|
7294
|
+
started_at
|
|
7295
|
+
});
|
|
7296
|
+
return;
|
|
7297
|
+
}
|
|
7298
|
+
const record = {
|
|
7299
|
+
id: raw_call.id,
|
|
7300
|
+
name: tool.name,
|
|
7301
|
+
input,
|
|
7302
|
+
output,
|
|
7303
|
+
duration_ms,
|
|
7304
|
+
started_at
|
|
7305
|
+
};
|
|
7306
|
+
ctx.records.push(record);
|
|
7307
|
+
ctx.all_records.push(record);
|
|
7308
|
+
if (tool.ends_turn === true) ctx.terminal_fired = true;
|
|
7309
|
+
ctx.feed.push(build_tool_result_message(raw_call.id, tool.name, output ?? ""));
|
|
7310
|
+
record_tool_call(ctx.config.trajectory, {
|
|
7311
|
+
step_index: ctx.step_index,
|
|
7312
|
+
name: tool.name,
|
|
7313
|
+
tool_call_id: raw_call.id,
|
|
7314
|
+
input,
|
|
7315
|
+
duration_ms
|
|
7316
|
+
});
|
|
7317
|
+
await dispatch_tool_result_chunk(ctx.config.dispatch_chunk, ctx.step_index, raw_call.id, output);
|
|
7318
|
+
}
|
|
7319
|
+
/**
|
|
7320
|
+
* Handle a throwing execute: an abort in flight or 'throw' policy closes
|
|
7321
|
+
* the step span and throws; feed-back policy records the failure with the
|
|
7322
|
+
* measured timing.
|
|
7323
|
+
*/
|
|
7324
|
+
async function record_execute_failure(ctx, tool, raw_call, input, failure) {
|
|
7325
|
+
if (ctx.config.abort.aborted) {
|
|
7326
|
+
const abort_err = new aborted_error("aborted", {
|
|
7327
|
+
reason: ctx.config.abort.reason,
|
|
7328
|
+
step_index: ctx.step_index,
|
|
7329
|
+
tool_call_in_flight: {
|
|
7330
|
+
id: raw_call.id,
|
|
7331
|
+
name: tool.name
|
|
7332
|
+
}
|
|
7333
|
+
});
|
|
7334
|
+
end_step_span(ctx.config.trajectory, ctx.step_span, { error: "aborted" });
|
|
7335
|
+
throw abort_err;
|
|
7336
|
+
}
|
|
7337
|
+
if (ctx.config.tool_error_policy === "throw") {
|
|
7338
|
+
const wrapped = new tool_error(`tool '${tool.name}' failed: ${failure.err_message ?? "unknown"}`, {
|
|
7339
|
+
tool_name: tool.name,
|
|
7340
|
+
tool_call_id: raw_call.id,
|
|
7341
|
+
cause: failure.thrown
|
|
7342
|
+
});
|
|
7343
|
+
end_step_span(ctx.config.trajectory, ctx.step_span, { error: failure.err_message ?? "tool error" });
|
|
7344
|
+
throw wrapped;
|
|
7345
|
+
}
|
|
7346
|
+
await record_failed_call(ctx, {
|
|
7347
|
+
id: raw_call.id,
|
|
7348
|
+
name: tool.name,
|
|
7349
|
+
input
|
|
7350
|
+
}, failure.err_message ?? "unknown", {
|
|
7351
|
+
duration_ms: failure.duration_ms,
|
|
7352
|
+
started_at: failure.started_at
|
|
7353
|
+
});
|
|
7354
|
+
}
|
|
7355
|
+
/**
|
|
7356
|
+
* Run one raw tool call through the guard chain — final-turn skip, tool
|
|
7357
|
+
* lookup, input validation, approval — then execute it. Each guard either
|
|
7358
|
+
* records the failure (feed-back policy) and stops, or throws (throw
|
|
7359
|
+
* policy / abort).
|
|
7360
|
+
*/
|
|
7361
|
+
async function process_call(ctx, raw_call) {
|
|
7362
|
+
throw_if_aborted_in_flight(ctx.config.abort, ctx.step_index, {
|
|
7363
|
+
id: raw_call.id,
|
|
7364
|
+
name: raw_call.name
|
|
7365
|
+
});
|
|
7366
|
+
if (ctx.would_exceed_after && ctx.tool_map.get(raw_call.name)?.ends_turn !== true) {
|
|
7367
|
+
await record_unexecuted_call(ctx, raw_call, "max_steps_exceeded_before_execution");
|
|
7368
|
+
return;
|
|
7369
|
+
}
|
|
7370
|
+
const resolved = await resolve_tool_and_input(ctx, raw_call);
|
|
7371
|
+
if (resolved === void 0) return;
|
|
7372
|
+
if (!await approve_call(ctx, resolved.tool, resolved.input, raw_call)) return;
|
|
7373
|
+
throw_if_aborted_in_flight(ctx.config.abort, ctx.step_index, {
|
|
7374
|
+
id: raw_call.id,
|
|
7375
|
+
name: resolved.tool.name
|
|
7376
|
+
});
|
|
7377
|
+
await execute_approved_call(ctx, resolved.tool, resolved.input, raw_call);
|
|
7378
|
+
}
|
|
7379
|
+
/**
|
|
7380
|
+
* Stamp salvage provenance onto the records of calls that came from text
|
|
7381
|
+
* salvage rather than structured output.
|
|
7382
|
+
*/
|
|
7383
|
+
function mark_salvaged(records, formats) {
|
|
7384
|
+
for (const r of records) {
|
|
7385
|
+
const format = formats.get(r.id);
|
|
7386
|
+
if (format !== void 0) {
|
|
7387
|
+
r.salvaged = true;
|
|
7388
|
+
r.salvaged_format = format;
|
|
7389
|
+
}
|
|
7390
|
+
}
|
|
7391
|
+
}
|
|
7392
|
+
/**
|
|
7393
|
+
* Emit a tool_result for every resolved call in this step (success carries
|
|
7394
|
+
* output, feed-back failures carry error). Throw-policy and aborted calls
|
|
7395
|
+
* exit before here and surface loudly as a thrown error instead.
|
|
7396
|
+
*/
|
|
7397
|
+
function emit_tool_results(config, step_index, records) {
|
|
7398
|
+
for (const r of records) record_tool_result(config.trajectory, {
|
|
7399
|
+
step_index,
|
|
7400
|
+
name: r.name,
|
|
7401
|
+
tool_call_id: r.id,
|
|
7402
|
+
duration_ms: r.duration_ms,
|
|
7403
|
+
...r.error !== void 0 ? { error: r.error } : { output: r.output }
|
|
7404
|
+
});
|
|
7405
|
+
}
|
|
7406
|
+
/**
|
|
7407
|
+
* The finish reason a tool-call step reports. A salvaged step reports
|
|
7408
|
+
* 'tool_calls': downstream consumers see the same shape a native tool turn
|
|
7409
|
+
* produces; the salvaged flags carry provenance. A terminal step reports
|
|
7410
|
+
* 'tool_calls' (it genuinely made calls); the loop-level finish_reason
|
|
7411
|
+
* 'stop' is the separate signal that generation ended.
|
|
7412
|
+
*/
|
|
7413
|
+
function resolve_turn_finish_reason(ctx, salvaged, turn_finish) {
|
|
7414
|
+
if (ctx.terminal_fired) return "tool_calls";
|
|
7415
|
+
if (ctx.would_exceed_after) return "max_steps";
|
|
7416
|
+
if (salvaged) return "tool_calls";
|
|
7417
|
+
return turn_finish;
|
|
7418
|
+
}
|
|
7419
|
+
/**
|
|
7420
|
+
* Build one StepRecord — cost derived and recorded, provider_reported
|
|
7421
|
+
* passed through — push it, notify on_finish_step, and close the step span.
|
|
7422
|
+
*/
|
|
7423
|
+
function push_step_record(config, acc, step_index, turn, tool_calls, finish_reason, step_span) {
|
|
7424
|
+
const record = {
|
|
7425
|
+
index: step_index,
|
|
7426
|
+
text: turn.text,
|
|
7427
|
+
tool_calls,
|
|
7428
|
+
usage: turn.usage,
|
|
7429
|
+
finish_reason
|
|
7430
|
+
};
|
|
7431
|
+
const breakdown = compute_and_record_cost(config, step_index, turn.usage);
|
|
7432
|
+
if (breakdown !== void 0) record.cost = breakdown;
|
|
7433
|
+
if (turn.provider_reported !== void 0) record.provider_reported = turn.provider_reported;
|
|
7434
|
+
acc.steps.push(record);
|
|
7435
|
+
if (config.on_finish_step !== void 0) config.on_finish_step(record);
|
|
7436
|
+
end_step_span(config.trajectory, step_span, {
|
|
7437
|
+
usage: turn.usage,
|
|
7438
|
+
finish_reason
|
|
7439
|
+
});
|
|
7440
|
+
}
|
|
7441
|
+
/**
|
|
7442
|
+
* Run one loop iteration: invoke the turn, salvage and clamp its calls,
|
|
7443
|
+
* execute them sequentially, and record the step. Returns the finished
|
|
7444
|
+
* ToolLoopResult when this step ends the loop (no calls, terminal tool,
|
|
7445
|
+
* max_steps), undefined when the loop should continue.
|
|
7446
|
+
*/
|
|
7447
|
+
async function run_loop_step(config, tool_map, acc, step_index) {
|
|
7448
|
+
const step_span = start_step_span(config.trajectory, step_index);
|
|
7449
|
+
const turn = await invoke_turn(config, step_index, step_span);
|
|
7450
|
+
const view = await salvage_turn(config, turn, tool_map, step_index);
|
|
7451
|
+
const { kept, dropped } = clamp_calls(config, view.calls, step_index);
|
|
7452
|
+
config.messages.push(build_assistant_message(view.history_text, kept));
|
|
7453
|
+
if (kept.length === 0) {
|
|
7454
|
+
push_step_record(config, acc, step_index, turn, [], turn.finish_reason, step_span);
|
|
7455
|
+
return loop_result(acc, turn.text, turn.finish_reason, false);
|
|
7456
|
+
}
|
|
7457
|
+
const ctx = {
|
|
7458
|
+
config,
|
|
7459
|
+
tool_map,
|
|
7460
|
+
step_index,
|
|
7461
|
+
step_span,
|
|
7462
|
+
would_exceed_after: step_index + 1 >= config.max_steps,
|
|
7463
|
+
records: [],
|
|
7464
|
+
all_records: acc.tool_calls,
|
|
7465
|
+
feed: [],
|
|
7466
|
+
terminal_fired: false
|
|
7467
|
+
};
|
|
7468
|
+
for (const raw_call of kept) await process_call(ctx, raw_call);
|
|
7469
|
+
for (const d of dropped) await record_unexecuted_call(ctx, d, "dropped_max_tool_calls_per_step");
|
|
7470
|
+
mark_salvaged(ctx.records, view.formats);
|
|
7471
|
+
emit_tool_results(config, step_index, ctx.records);
|
|
7472
|
+
for (const m of ctx.feed) config.messages.push(m);
|
|
7473
|
+
const turn_finish_reason = resolve_turn_finish_reason(ctx, view.outcome !== void 0, turn.finish_reason);
|
|
7474
|
+
push_step_record(config, acc, step_index, turn, ctx.records, turn_finish_reason, step_span);
|
|
7475
|
+
if (ctx.terminal_fired) return loop_result(acc, turn.text, "stop", false);
|
|
7476
|
+
if (ctx.would_exceed_after) return loop_result(acc, turn.text, "max_steps", true);
|
|
7477
|
+
}
|
|
7478
|
+
/**
|
|
7479
|
+
* Run the model-turn / tool-execution loop to completion.
|
|
7480
|
+
*
|
|
7481
|
+
* Each iteration invokes the transport seam once, salvages text-embedded
|
|
7482
|
+
* tool calls when eligible, clamps to `max_tool_calls_per_step`, executes
|
|
7483
|
+
* calls sequentially under the header invariants, and feeds results back
|
|
7484
|
+
* until the model stops, a terminal tool fires, or `max_steps` is reached.
|
|
7485
|
+
*/
|
|
7486
|
+
async function run_tool_loop(config) {
|
|
7487
|
+
const acc = {
|
|
7488
|
+
steps: [],
|
|
7489
|
+
tool_calls: []
|
|
7490
|
+
};
|
|
7491
|
+
const tool_map = /* @__PURE__ */ new Map();
|
|
7492
|
+
for (const t of config.tools) tool_map.set(t.name, t);
|
|
7493
|
+
let step_index = config.step_index_start;
|
|
7494
|
+
while (true) {
|
|
7495
|
+
throw_if_aborted(config.abort, step_index);
|
|
7496
|
+
if (step_index >= config.max_steps) return loop_result(acc, "", "max_steps", true);
|
|
7497
|
+
const finished = await run_loop_step(config, tool_map, acc, step_index);
|
|
7498
|
+
if (finished !== void 0) return finished;
|
|
7499
|
+
step_index += 1;
|
|
7500
|
+
}
|
|
7501
|
+
}
|
|
7502
|
+
//#endregion
|
|
7503
|
+
//#region src/engine/generate.ts
|
|
7504
|
+
/**
|
|
7505
|
+
* Build the initial message list for a generate call from `opts.system` and
|
|
7506
|
+
* `opts.prompt`.
|
|
7507
|
+
*
|
|
7508
|
+
* A non-empty string `system` is prepended as a system message. A string
|
|
7509
|
+
* `prompt` becomes a single user message; a `Message[]` prompt is copied
|
|
7510
|
+
* through element-by-element so the returned array can be mutated without
|
|
7511
|
+
* affecting the caller's.
|
|
7512
|
+
*/
|
|
7513
|
+
function build_initial_messages(opts) {
|
|
7514
|
+
const messages = [];
|
|
7515
|
+
if (typeof opts.system === "string" && opts.system.length > 0) messages.push({
|
|
7516
|
+
role: "system",
|
|
7517
|
+
content: opts.system
|
|
7518
|
+
});
|
|
7519
|
+
if (typeof opts.prompt === "string") {
|
|
7520
|
+
messages.push({
|
|
7181
7521
|
role: "user",
|
|
7182
7522
|
content: opts.prompt
|
|
7183
7523
|
});
|
|
@@ -7195,20 +7535,76 @@ function build_initial_messages(opts) {
|
|
|
7195
7535
|
* empty (provider APIs reject an empty messages array).
|
|
7196
7536
|
*/
|
|
7197
7537
|
function split_leading_system_messages(messages) {
|
|
7198
|
-
|
|
7199
|
-
|
|
7200
|
-
|
|
7201
|
-
|
|
7202
|
-
|
|
7203
|
-
|
|
7204
|
-
|
|
7205
|
-
|
|
7206
|
-
|
|
7207
|
-
|
|
7208
|
-
|
|
7209
|
-
|
|
7210
|
-
|
|
7211
|
-
|
|
7538
|
+
return split_leading_system_run(messages, (m) => m.content);
|
|
7539
|
+
}
|
|
7540
|
+
const RETRY_CLASSIFIED_KINDS = /* @__PURE__ */ new Set([
|
|
7541
|
+
"rate_limit",
|
|
7542
|
+
"provider_5xx",
|
|
7543
|
+
"network",
|
|
7544
|
+
"timeout"
|
|
7545
|
+
]);
|
|
7546
|
+
const NETWORK_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
7547
|
+
"ECONNRESET",
|
|
7548
|
+
"ETIMEDOUT",
|
|
7549
|
+
"ENOTFOUND",
|
|
7550
|
+
"ECONNREFUSED"
|
|
7551
|
+
]);
|
|
7552
|
+
/**
|
|
7553
|
+
* True when `err` already carries a `kind` this layer treats as retryable, so
|
|
7554
|
+
* the classifier passes it through untouched.
|
|
7555
|
+
*/
|
|
7556
|
+
function has_retry_kind(err) {
|
|
7557
|
+
const kind = Reflect.get(err, "kind");
|
|
7558
|
+
return typeof kind === "string" && RETRY_CLASSIFIED_KINDS.has(kind);
|
|
7559
|
+
}
|
|
7560
|
+
/**
|
|
7561
|
+
* Read the HTTP status off either `statusCode` or `status`, in that order.
|
|
7562
|
+
*/
|
|
7563
|
+
function resolve_error_status(err) {
|
|
7564
|
+
const status = Reflect.get(err, "statusCode");
|
|
7565
|
+
if (typeof status === "number") return status;
|
|
7566
|
+
const status_alt = Reflect.get(err, "status");
|
|
7567
|
+
return typeof status_alt === "number" ? status_alt : void 0;
|
|
7568
|
+
}
|
|
7569
|
+
/**
|
|
7570
|
+
* Copy the error's `message` onto a classification record when it is a string,
|
|
7571
|
+
* returning the same record for chaining.
|
|
7572
|
+
*/
|
|
7573
|
+
function with_error_message(err, out) {
|
|
7574
|
+
const message = Reflect.get(err, "message");
|
|
7575
|
+
if (typeof message === "string") out["message"] = message;
|
|
7576
|
+
return out;
|
|
7577
|
+
}
|
|
7578
|
+
/**
|
|
7579
|
+
* Parse a `Retry-After` value off the error's `responseHeaders`, if present.
|
|
7580
|
+
*/
|
|
7581
|
+
function retry_after_from_error(err) {
|
|
7582
|
+
const headers = Reflect.get(err, "responseHeaders");
|
|
7583
|
+
if (headers === null || typeof headers !== "object") return void 0;
|
|
7584
|
+
const hv = Reflect.get(headers, "retry-after");
|
|
7585
|
+
return typeof hv === "string" ? parse_retry_after(hv) : void 0;
|
|
7586
|
+
}
|
|
7587
|
+
/**
|
|
7588
|
+
* Build the `rate_limit` classification for a 429, attaching the message and a
|
|
7589
|
+
* parsed `Retry-After` when either is present.
|
|
7590
|
+
*/
|
|
7591
|
+
function classify_rate_limit(err) {
|
|
7592
|
+
const out = with_error_message(err, {
|
|
7593
|
+
kind: "rate_limit",
|
|
7594
|
+
status: 429
|
|
7595
|
+
});
|
|
7596
|
+
const retry_after_ms = retry_after_from_error(err);
|
|
7597
|
+
if (retry_after_ms !== void 0) out["retry_after_ms"] = retry_after_ms;
|
|
7598
|
+
return out;
|
|
7599
|
+
}
|
|
7600
|
+
/**
|
|
7601
|
+
* Build the `network` classification when `err.code` is a known network error
|
|
7602
|
+
* code, or `undefined` when it is not.
|
|
7603
|
+
*/
|
|
7604
|
+
function classify_network_error(err) {
|
|
7605
|
+
const code = Reflect.get(err, "code");
|
|
7606
|
+
if (typeof code !== "string" || !NETWORK_ERROR_CODES.has(code)) return void 0;
|
|
7607
|
+
return with_error_message(err, { kind: "network" });
|
|
7212
7608
|
}
|
|
7213
7609
|
/**
|
|
7214
7610
|
* Classify a thrown provider error into the shape `retry_with_policy`
|
|
@@ -7223,44 +7619,14 @@ function split_leading_system_messages(messages) {
|
|
|
7223
7619
|
*/
|
|
7224
7620
|
function classify_provider_error(err) {
|
|
7225
7621
|
if (err === null || typeof err !== "object") return err;
|
|
7226
|
-
|
|
7227
|
-
|
|
7228
|
-
|
|
7229
|
-
|
|
7230
|
-
|
|
7231
|
-
|
|
7232
|
-
|
|
7233
|
-
|
|
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;
|
|
7622
|
+
if (has_retry_kind(err)) return err;
|
|
7623
|
+
const status = resolve_error_status(err);
|
|
7624
|
+
if (status === 429) return classify_rate_limit(err);
|
|
7625
|
+
if (status !== void 0 && status >= 500 && status < 600) return with_error_message(err, {
|
|
7626
|
+
kind: "provider_5xx",
|
|
7627
|
+
status
|
|
7628
|
+
});
|
|
7629
|
+
return classify_network_error(err) ?? err;
|
|
7264
7630
|
}
|
|
7265
7631
|
/**
|
|
7266
7632
|
* Compose the per-attempt turn signal: the user's abort OR'd with a fresh
|
|
@@ -7330,80 +7696,368 @@ function retry_turn(call_once, args, has_streamed, retry_policy, classify, turn_
|
|
|
7330
7696
|
}, retry_policy, args.abort);
|
|
7331
7697
|
}
|
|
7332
7698
|
/**
|
|
7333
|
-
* Build the ai_sdk-transport InvokeOnce: the SDK turn built by
|
|
7334
|
-
* providers/ai_sdk/invoke.ts behind the same engine-owned retry_turn wrapper
|
|
7335
|
-
* the native path uses. The SDK call body lives behind the seam; this builder
|
|
7336
|
-
* owns only retry and the streamed-output tracking that makes a mid-stream
|
|
7337
|
-
* failure a non-retryable interruption.
|
|
7699
|
+
* Build the ai_sdk-transport InvokeOnce: the SDK turn built by
|
|
7700
|
+
* providers/ai_sdk/invoke.ts behind the same engine-owned retry_turn wrapper
|
|
7701
|
+
* the native path uses. The SDK call body lives behind the seam; this builder
|
|
7702
|
+
* owns only retry and the streamed-output tracking that makes a mid-stream
|
|
7703
|
+
* failure a non-retryable interruption.
|
|
7704
|
+
*/
|
|
7705
|
+
function build_ai_sdk_invoke(cfg) {
|
|
7706
|
+
return async (args) => {
|
|
7707
|
+
let chunks_started = false;
|
|
7708
|
+
const call_once = (turn_abort) => cfg.invoke_turn({
|
|
7709
|
+
step_index: args.step_index,
|
|
7710
|
+
messages: args.messages,
|
|
7711
|
+
abort: turn_abort,
|
|
7712
|
+
stream: args.stream,
|
|
7713
|
+
on_first_chunk: () => {
|
|
7714
|
+
chunks_started = true;
|
|
7715
|
+
}
|
|
7716
|
+
});
|
|
7717
|
+
return await retry_turn(call_once, args, () => chunks_started, cfg.retry_policy, classify_provider_error, cfg.turn_timeout_ms);
|
|
7718
|
+
};
|
|
7719
|
+
}
|
|
7720
|
+
/**
|
|
7721
|
+
* Build the native-transport InvokeOnce: maps the loop's InvokeOnceArgs to a
|
|
7722
|
+
* TurnRequest and calls adapter.invoke_turn inside the same retry_turn
|
|
7723
|
+
* wrapper as the ai_sdk path. The adapter sees a child abort signal so a
|
|
7724
|
+
* throwing on_chunk consumer cancels the in-flight request, and its chunk
|
|
7725
|
+
* emissions flow through the shared dispatcher, which is what lets
|
|
7726
|
+
* run_tool_loop treat both transports identically.
|
|
7727
|
+
*/
|
|
7728
|
+
function build_native_invoke(cfg) {
|
|
7729
|
+
const classify = cfg.adapter.classify_error ?? classify_provider_error;
|
|
7730
|
+
return async (args) => {
|
|
7731
|
+
let chunks_started = false;
|
|
7732
|
+
const call_once = async (turn_abort) => {
|
|
7733
|
+
const internal_controller = new AbortController();
|
|
7734
|
+
const cancel_on_turn_abort = () => {
|
|
7735
|
+
internal_controller.abort(turn_abort.reason);
|
|
7736
|
+
};
|
|
7737
|
+
if (turn_abort.aborted) internal_controller.abort(turn_abort.reason);
|
|
7738
|
+
else turn_abort.addEventListener("abort", cancel_on_turn_abort, { once: true });
|
|
7739
|
+
const dispatch_chunk = async (chunk) => {
|
|
7740
|
+
chunks_started = true;
|
|
7741
|
+
try {
|
|
7742
|
+
await cfg.dispatcher.dispatch(chunk);
|
|
7743
|
+
} catch (err) {
|
|
7744
|
+
internal_controller.abort();
|
|
7745
|
+
throw err;
|
|
7746
|
+
}
|
|
7747
|
+
};
|
|
7748
|
+
const { system, messages } = split_leading_system_messages(args.messages);
|
|
7749
|
+
const req = {
|
|
7750
|
+
step_index: args.step_index,
|
|
7751
|
+
messages,
|
|
7752
|
+
tools: args.tools,
|
|
7753
|
+
abort: internal_controller.signal,
|
|
7754
|
+
stream: args.stream,
|
|
7755
|
+
model_id: cfg.model_id,
|
|
7756
|
+
effort: cfg.effort,
|
|
7757
|
+
...system !== void 0 ? { system } : {},
|
|
7758
|
+
...cfg.schema !== void 0 ? { schema: cfg.schema } : {},
|
|
7759
|
+
...cfg.provider_options !== void 0 ? { provider_options: cfg.provider_options } : {},
|
|
7760
|
+
...cfg.temperature !== void 0 ? { temperature: cfg.temperature } : {},
|
|
7761
|
+
...cfg.max_tokens !== void 0 ? { max_tokens: cfg.max_tokens } : {},
|
|
7762
|
+
...cfg.top_p !== void 0 ? { top_p: cfg.top_p } : {},
|
|
7763
|
+
...args.stream ? { dispatch_chunk } : {}
|
|
7764
|
+
};
|
|
7765
|
+
try {
|
|
7766
|
+
return await cfg.adapter.invoke_turn(req);
|
|
7767
|
+
} finally {
|
|
7768
|
+
turn_abort.removeEventListener("abort", cancel_on_turn_abort);
|
|
7769
|
+
}
|
|
7770
|
+
};
|
|
7771
|
+
return await retry_turn(call_once, args, () => chunks_started, cfg.retry_policy, classify, cfg.turn_timeout_ms);
|
|
7772
|
+
};
|
|
7773
|
+
}
|
|
7774
|
+
/**
|
|
7775
|
+
* Resolve the provider name: per-call, then the engine default, then the sole
|
|
7776
|
+
* configured adapter, then 'anthropic'.
|
|
7777
|
+
*/
|
|
7778
|
+
function resolve_provider(opts_in, engine) {
|
|
7779
|
+
const sole_provider = engine.adapters.size === 1 ? [...engine.adapters.keys()][0] : void 0;
|
|
7780
|
+
return opts_in.provider ?? engine.default_provider ?? sole_provider ?? "anthropic";
|
|
7781
|
+
}
|
|
7782
|
+
/**
|
|
7783
|
+
* Resolve the model/provider pair for one call and freeze the resolved
|
|
7784
|
+
* options view.
|
|
7785
|
+
*
|
|
7786
|
+
* The model must resolve (per-call, then engine default) or the call cannot
|
|
7787
|
+
* proceed. Engine-default provider_options merge under per-call ones here so
|
|
7788
|
+
* every transport downstream sees a single already-merged view instead of
|
|
7789
|
+
* re-merging per branch.
|
|
7790
|
+
*/
|
|
7791
|
+
function resolve_target(opts_in, engine) {
|
|
7792
|
+
const resolved_model = opts_in.model ?? engine.default_model;
|
|
7793
|
+
if (resolved_model === void 0) throw new model_required_error();
|
|
7794
|
+
const resolved_provider = resolve_provider(opts_in, engine);
|
|
7795
|
+
const merged_provider_options = merge_provider_options(engine.default_provider_options, opts_in.provider_options);
|
|
7796
|
+
const opts = {
|
|
7797
|
+
...opts_in,
|
|
7798
|
+
model: resolved_model,
|
|
7799
|
+
provider: resolved_provider
|
|
7800
|
+
};
|
|
7801
|
+
if (opts_in.system === void 0 && engine.default_system !== void 0) opts.system = engine.default_system;
|
|
7802
|
+
if (merged_provider_options !== void 0) opts.provider_options = merged_provider_options;
|
|
7803
|
+
return {
|
|
7804
|
+
opts,
|
|
7805
|
+
target: {
|
|
7806
|
+
provider: resolved_provider,
|
|
7807
|
+
model_id: resolved_model
|
|
7808
|
+
}
|
|
7809
|
+
};
|
|
7810
|
+
}
|
|
7811
|
+
/**
|
|
7812
|
+
* Reject a call that uses a capability the resolved adapter does not
|
|
7813
|
+
* implement. Checked up front, before any transport work, so the failure is a
|
|
7814
|
+
* typed capability error rather than a mid-call provider rejection.
|
|
7815
|
+
*/
|
|
7816
|
+
function assert_capabilities(adapter, provider, opts, tools_list, on_chunk_provided) {
|
|
7817
|
+
if (opts.schema !== void 0 && !adapter.supports("schema")) throw new provider_capability_error(provider, "schema");
|
|
7818
|
+
if (tools_list.length > 0 && !adapter.supports("tools")) throw new provider_capability_error(provider, "tools");
|
|
7819
|
+
if (on_chunk_provided && !adapter.supports("streaming")) throw new provider_capability_error(provider, "streaming");
|
|
7820
|
+
}
|
|
7821
|
+
/**
|
|
7822
|
+
* Resolve the per-turn knobs (effort, retry policy, turn timeout) against
|
|
7823
|
+
* engine defaults. Validated here, before any transport is built, so a bad
|
|
7824
|
+
* budget fails as a config error, never as a spurious timeout.
|
|
7825
|
+
*/
|
|
7826
|
+
function resolve_turn_config(opts, engine) {
|
|
7827
|
+
const effort = opts.effort ?? engine.default_effort;
|
|
7828
|
+
const retry_policy = opts.retry ?? engine.default_retry;
|
|
7829
|
+
const turn_timeout_ms = opts.turn_timeout_ms ?? engine.default_turn_timeout_ms;
|
|
7830
|
+
if (turn_timeout_ms !== void 0 && turn_timeout_ms <= 0) throw new engine_config_error("turn_timeout_ms must be > 0");
|
|
7831
|
+
return {
|
|
7832
|
+
effort,
|
|
7833
|
+
retry_policy,
|
|
7834
|
+
turn_timeout_ms
|
|
7835
|
+
};
|
|
7836
|
+
}
|
|
7837
|
+
/**
|
|
7838
|
+
* Resolve the tool-call salvage budget. Returned as a holder rather than a
|
|
7839
|
+
* count: one holder per generate call so schema-repair re-invocations of the
|
|
7840
|
+
* loop cannot refill the salvage budget. undefined disables salvage.
|
|
7841
|
+
*/
|
|
7842
|
+
function resolve_salvage_budget(opts, engine) {
|
|
7843
|
+
const tool_call_repair_attempts = opts.tool_call_repair_attempts ?? engine.default_tool_call_repair_attempts ?? 0;
|
|
7844
|
+
if (tool_call_repair_attempts < 0) throw new engine_config_error("tool_call_repair_attempts must be >= 0");
|
|
7845
|
+
return tool_call_repair_attempts > 0 ? { remaining: tool_call_repair_attempts } : void 0;
|
|
7846
|
+
}
|
|
7847
|
+
/**
|
|
7848
|
+
* Resolve the loop-level limits and policies against engine defaults,
|
|
7849
|
+
* rejecting configurations the tool loop cannot honor.
|
|
7850
|
+
*/
|
|
7851
|
+
function resolve_loop_limits(opts, engine) {
|
|
7852
|
+
const max_steps = opts.max_steps ?? engine.default_max_steps;
|
|
7853
|
+
const tool_error_policy = opts.tool_error_policy ?? engine.default_tool_error_policy ?? "feed_back";
|
|
7854
|
+
const schema_repair_attempts = opts.schema_repair_attempts ?? engine.default_schema_repair_attempts ?? 1;
|
|
7855
|
+
const salvage_budget = resolve_salvage_budget(opts, engine);
|
|
7856
|
+
const max_tool_calls_per_step = opts.max_tool_calls_per_step ?? engine.default_max_tool_calls_per_step;
|
|
7857
|
+
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");
|
|
7858
|
+
return {
|
|
7859
|
+
max_steps,
|
|
7860
|
+
tool_error_policy,
|
|
7861
|
+
schema_repair_attempts,
|
|
7862
|
+
salvage_budget,
|
|
7863
|
+
max_tool_calls_per_step
|
|
7864
|
+
};
|
|
7865
|
+
}
|
|
7866
|
+
/**
|
|
7867
|
+
* Build the ai_sdk-transport InvokeOnce for one call: translate effort, merge
|
|
7868
|
+
* provider options, load the SDK seam, and wrap it in the engine-owned retry
|
|
7869
|
+
* via build_ai_sdk_invoke.
|
|
7870
|
+
*/
|
|
7871
|
+
async function build_ai_sdk_transport(cfg) {
|
|
7872
|
+
const { adapter, opts, effort } = cfg;
|
|
7873
|
+
const effort_translation = adapter.translate_effort(effort);
|
|
7874
|
+
if (effort !== "none" && effort_translation.effort_ignored) record_effort_ignored(cfg.trajectory, cfg.target.model_id);
|
|
7875
|
+
const combined_provider_options = merge_provider_options(effort_translation.provider_options, opts.provider_options);
|
|
7876
|
+
const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
|
|
7877
|
+
let ai_sdk_invoke_mod;
|
|
7878
|
+
try {
|
|
7879
|
+
ai_sdk_invoke_mod = await import("./invoke-JkzNf7-Y.js");
|
|
7880
|
+
} catch (err) {
|
|
7881
|
+
throw missing_peer_error("ai", err);
|
|
7882
|
+
}
|
|
7883
|
+
const { create_ai_sdk_turn } = ai_sdk_invoke_mod;
|
|
7884
|
+
return build_ai_sdk_invoke({
|
|
7885
|
+
invoke_turn: create_ai_sdk_turn({
|
|
7886
|
+
adapter,
|
|
7887
|
+
model_id: cfg.target.model_id,
|
|
7888
|
+
dispatcher: cfg.dispatcher,
|
|
7889
|
+
tools: cfg.tools_list,
|
|
7890
|
+
schema: opts.schema,
|
|
7891
|
+
provider_options,
|
|
7892
|
+
temperature: opts.temperature,
|
|
7893
|
+
max_tokens: opts.max_tokens,
|
|
7894
|
+
top_p: opts.top_p,
|
|
7895
|
+
telemetry: cfg.telemetry
|
|
7896
|
+
}),
|
|
7897
|
+
retry_policy: cfg.retry_policy,
|
|
7898
|
+
turn_timeout_ms: cfg.turn_timeout_ms
|
|
7899
|
+
});
|
|
7900
|
+
}
|
|
7901
|
+
/**
|
|
7902
|
+
* Build the message list one generate call starts from. When a schema is set,
|
|
7903
|
+
* the JSON-only instruction folds into the existing system message (or is
|
|
7904
|
+
* prepended as one) so the model sees it exactly once, wherever the system
|
|
7905
|
+
* prompt came from.
|
|
7906
|
+
*/
|
|
7907
|
+
function build_prompt_messages(opts) {
|
|
7908
|
+
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;
|
|
7909
|
+
const initial_messages = build_initial_messages(opts);
|
|
7910
|
+
if (schema_prefix !== void 0) {
|
|
7911
|
+
const idx = initial_messages.findIndex((m) => m.role === "system");
|
|
7912
|
+
if (idx >= 0) {
|
|
7913
|
+
const sys = initial_messages[idx];
|
|
7914
|
+
if (sys?.role === "system") initial_messages[idx] = {
|
|
7915
|
+
role: "system",
|
|
7916
|
+
content: `${sys.content}\n\n${schema_prefix}`
|
|
7917
|
+
};
|
|
7918
|
+
} else initial_messages.unshift({
|
|
7919
|
+
role: "system",
|
|
7920
|
+
content: schema_prefix
|
|
7921
|
+
});
|
|
7922
|
+
}
|
|
7923
|
+
return initial_messages;
|
|
7924
|
+
}
|
|
7925
|
+
/**
|
|
7926
|
+
* Assemble the ToolLoopConfig base shared by every loop invocation of one
|
|
7927
|
+
* generate call. Everything here is per-call state: the mutable transcript,
|
|
7928
|
+
* the pricing lookup, and the pricing_missing dedup all live for exactly one
|
|
7929
|
+
* call, which is what lets schema-repair re-invocations share them. The
|
|
7930
|
+
* conditional spreads keep optional fields absent rather than undefined for
|
|
7931
|
+
* exactOptionalPropertyTypes.
|
|
7338
7932
|
*/
|
|
7339
|
-
function
|
|
7340
|
-
|
|
7341
|
-
|
|
7342
|
-
|
|
7343
|
-
|
|
7344
|
-
|
|
7345
|
-
|
|
7346
|
-
|
|
7347
|
-
|
|
7348
|
-
|
|
7349
|
-
|
|
7933
|
+
function build_loop_config(cfg) {
|
|
7934
|
+
const { opts, target, limits } = cfg;
|
|
7935
|
+
const abort = opts.abort ?? new AbortController().signal;
|
|
7936
|
+
const resolve_pricing = () => {
|
|
7937
|
+
return cfg.pricing[pricing_key(target.provider, target.model_id)];
|
|
7938
|
+
};
|
|
7939
|
+
const dispatch_chunk = cfg.on_chunk_provided ? async (chunk) => {
|
|
7940
|
+
await cfg.dispatcher.dispatch(chunk);
|
|
7941
|
+
} : void 0;
|
|
7942
|
+
return {
|
|
7943
|
+
invoke_once: cfg.invoke_once,
|
|
7944
|
+
messages: cfg.messages,
|
|
7945
|
+
tools: cfg.tools_list,
|
|
7946
|
+
max_steps: limits.max_steps,
|
|
7947
|
+
tool_error_policy: limits.tool_error_policy,
|
|
7948
|
+
abort,
|
|
7949
|
+
on_tool_approval: opts.on_tool_approval,
|
|
7950
|
+
trajectory: cfg.trajectory,
|
|
7951
|
+
stream: cfg.on_chunk_provided,
|
|
7952
|
+
dispatch_chunk,
|
|
7953
|
+
provider: target.provider,
|
|
7954
|
+
model_id: target.model_id,
|
|
7955
|
+
resolve_pricing,
|
|
7956
|
+
pricing_dedup: create_pricing_missing_dedup(cfg.trajectory),
|
|
7957
|
+
...limits.salvage_budget !== void 0 ? { salvage_budget: limits.salvage_budget } : {},
|
|
7958
|
+
...limits.max_tool_calls_per_step !== void 0 ? { max_tool_calls_per_step: limits.max_tool_calls_per_step } : {},
|
|
7959
|
+
...opts.prepare_step !== void 0 ? { prepare_step: opts.prepare_step } : {}
|
|
7960
|
+
};
|
|
7961
|
+
}
|
|
7962
|
+
/**
|
|
7963
|
+
* One schema-validation pass over the loop's final text. A successful parse
|
|
7964
|
+
* returns the value in a holder (a schema can legitimately validate to
|
|
7965
|
+
* undefined, which must stay distinguishable from "nothing parsed"). A failed
|
|
7966
|
+
* parse records the trajectory event first, then either throws (`can_repair`
|
|
7967
|
+
* false: the repair or step budget is spent) or hands back the repair message
|
|
7968
|
+
* to append before re-invoking the loop.
|
|
7969
|
+
*/
|
|
7970
|
+
async function parse_schema_step(cfg) {
|
|
7971
|
+
const parse = await parse_with_schema(cfg.schema, cfg.text);
|
|
7972
|
+
if (parse.ok) return { parsed: { value: parse.value } };
|
|
7973
|
+
record_schema_validation_failed(cfg.trajectory, {
|
|
7974
|
+
attempt: cfg.attempt,
|
|
7975
|
+
schema_issues: format_schema_issues(parse.issues),
|
|
7976
|
+
raw_text: cfg.text
|
|
7977
|
+
});
|
|
7978
|
+
if (!cfg.can_repair) throw_schema_validation(parse.issues, cfg.text);
|
|
7979
|
+
return { repair_message: build_repair_message(parse.issues) };
|
|
7980
|
+
}
|
|
7981
|
+
/**
|
|
7982
|
+
* Drive run_tool_loop to a final answer, re-invoking it for schema repair.
|
|
7983
|
+
*
|
|
7984
|
+
* Without a schema this is a single loop invocation. With one, the final text
|
|
7985
|
+
* must parse: a non-'stop' finish reason means no validated value can exist
|
|
7986
|
+
* (a content filter, the token limit, the step cap), so this throws
|
|
7987
|
+
* incomplete_generation_error rather than returning unchecked text; a failed
|
|
7988
|
+
* parse appends the repair message to the shared transcript and re-enters the
|
|
7989
|
+
* loop until the response parses, repair attempts run out, or max_steps is
|
|
7990
|
+
* reached. step_index_start advances by the accumulated step count so step
|
|
7991
|
+
* indices stay call-global across re-invocations.
|
|
7992
|
+
*/
|
|
7993
|
+
async function run_generate_loop(cfg) {
|
|
7994
|
+
const { base, schema } = cfg;
|
|
7995
|
+
const steps_accum = [];
|
|
7996
|
+
const tool_calls_accum = [];
|
|
7997
|
+
let text = "";
|
|
7998
|
+
let finish_reason = "stop";
|
|
7999
|
+
let repair_remaining = cfg.schema_repair_attempts;
|
|
8000
|
+
let content_parsed;
|
|
8001
|
+
while (true) {
|
|
8002
|
+
const loop_result = await run_tool_loop({
|
|
8003
|
+
...base,
|
|
8004
|
+
step_index_start: steps_accum.length
|
|
7350
8005
|
});
|
|
7351
|
-
|
|
8006
|
+
for (const s of loop_result.steps) steps_accum.push(s);
|
|
8007
|
+
for (const tc of loop_result.tool_calls) tool_calls_accum.push(tc);
|
|
8008
|
+
text = loop_result.text;
|
|
8009
|
+
finish_reason = loop_result.finish_reason;
|
|
8010
|
+
if (schema === void 0) break;
|
|
8011
|
+
if (finish_reason !== "stop") throw new incomplete_generation_error(finish_reason, text, last_provider_reported(steps_accum));
|
|
8012
|
+
const schema_step = await parse_schema_step({
|
|
8013
|
+
schema,
|
|
8014
|
+
text,
|
|
8015
|
+
trajectory: base.trajectory,
|
|
8016
|
+
attempt: repair_remaining === cfg.schema_repair_attempts ? "initial" : "repair",
|
|
8017
|
+
can_repair: repair_remaining > 0 && steps_accum.length < base.max_steps
|
|
8018
|
+
});
|
|
8019
|
+
if ("parsed" in schema_step) {
|
|
8020
|
+
content_parsed = schema_step.parsed;
|
|
8021
|
+
break;
|
|
8022
|
+
}
|
|
8023
|
+
repair_remaining -= 1;
|
|
8024
|
+
base.messages.push(schema_step.repair_message);
|
|
8025
|
+
}
|
|
8026
|
+
return {
|
|
8027
|
+
steps: steps_accum,
|
|
8028
|
+
tool_calls: tool_calls_accum,
|
|
8029
|
+
text,
|
|
8030
|
+
finish_reason,
|
|
8031
|
+
content_parsed
|
|
7352
8032
|
};
|
|
7353
8033
|
}
|
|
7354
8034
|
/**
|
|
7355
|
-
*
|
|
7356
|
-
*
|
|
7357
|
-
*
|
|
7358
|
-
*
|
|
7359
|
-
* emissions flow through the shared dispatcher, which is what lets
|
|
7360
|
-
* run_tool_loop treat both transports identically.
|
|
8035
|
+
* Fold the loop outcome into the caller-facing GenerateResult: usage and cost
|
|
8036
|
+
* aggregate across every step (schema-repair re-invocations included), the
|
|
8037
|
+
* content is the parsed holder's value when a schema ran, and cost /
|
|
8038
|
+
* provider_reported attach only when a step actually reported them.
|
|
7361
8039
|
*/
|
|
7362
|
-
function
|
|
7363
|
-
const
|
|
7364
|
-
|
|
7365
|
-
|
|
7366
|
-
|
|
7367
|
-
|
|
7368
|
-
|
|
7369
|
-
|
|
7370
|
-
|
|
7371
|
-
|
|
7372
|
-
|
|
7373
|
-
|
|
7374
|
-
|
|
7375
|
-
|
|
7376
|
-
|
|
7377
|
-
|
|
7378
|
-
internal_controller.abort();
|
|
7379
|
-
throw err;
|
|
7380
|
-
}
|
|
7381
|
-
};
|
|
7382
|
-
const { system, messages } = split_leading_system_messages(args.messages);
|
|
7383
|
-
const req = {
|
|
7384
|
-
step_index: args.step_index,
|
|
7385
|
-
messages,
|
|
7386
|
-
tools: args.tools,
|
|
7387
|
-
abort: internal_controller.signal,
|
|
7388
|
-
stream: args.stream,
|
|
7389
|
-
model_id: cfg.model_id,
|
|
7390
|
-
effort: cfg.effort,
|
|
7391
|
-
...system !== void 0 ? { system } : {},
|
|
7392
|
-
...cfg.schema !== void 0 ? { schema: cfg.schema } : {},
|
|
7393
|
-
...cfg.provider_options !== void 0 ? { provider_options: cfg.provider_options } : {},
|
|
7394
|
-
...cfg.temperature !== void 0 ? { temperature: cfg.temperature } : {},
|
|
7395
|
-
...cfg.max_tokens !== void 0 ? { max_tokens: cfg.max_tokens } : {},
|
|
7396
|
-
...cfg.top_p !== void 0 ? { top_p: cfg.top_p } : {},
|
|
7397
|
-
...args.stream ? { dispatch_chunk } : {}
|
|
7398
|
-
};
|
|
7399
|
-
try {
|
|
7400
|
-
return await cfg.adapter.invoke_turn(req);
|
|
7401
|
-
} finally {
|
|
7402
|
-
turn_abort.removeEventListener("abort", cancel_on_turn_abort);
|
|
7403
|
-
}
|
|
7404
|
-
};
|
|
7405
|
-
return await retry_turn(call_once, args, () => chunks_started, cfg.retry_policy, classify, cfg.turn_timeout_ms);
|
|
8040
|
+
function assemble_result(outcome, target) {
|
|
8041
|
+
const aggregated_usage = sum_usage(outcome.steps);
|
|
8042
|
+
const aggregated_cost = aggregate_cost(outcome.steps, target.provider);
|
|
8043
|
+
let final_content;
|
|
8044
|
+
if (outcome.content_parsed !== void 0) final_content = outcome.content_parsed.value;
|
|
8045
|
+
else final_content = outcome.text;
|
|
8046
|
+
const result = {
|
|
8047
|
+
content: final_content,
|
|
8048
|
+
tool_calls: outcome.tool_calls,
|
|
8049
|
+
steps: outcome.steps,
|
|
8050
|
+
usage: aggregated_usage,
|
|
8051
|
+
finish_reason: outcome.finish_reason,
|
|
8052
|
+
model_resolved: {
|
|
8053
|
+
provider: target.provider,
|
|
8054
|
+
model_id: target.model_id
|
|
8055
|
+
}
|
|
7406
8056
|
};
|
|
8057
|
+
if (aggregated_cost !== void 0) result.cost = aggregated_cost;
|
|
8058
|
+
const aggregated_reported = last_provider_reported(outcome.steps);
|
|
8059
|
+
if (aggregated_reported !== void 0) result.provider_reported = aggregated_reported;
|
|
8060
|
+
return result;
|
|
7407
8061
|
}
|
|
7408
8062
|
/**
|
|
7409
8063
|
* Resolve a `generate` call's options against engine defaults, run the tool
|
|
@@ -7427,195 +8081,79 @@ function build_native_invoke(cfg) {
|
|
|
7427
8081
|
*/
|
|
7428
8082
|
async function generate(opts_in, engine) {
|
|
7429
8083
|
if (opts_in.abort?.aborted === true) throw new aborted_error("aborted", { reason: opts_in.abort.reason });
|
|
7430
|
-
const
|
|
7431
|
-
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";
|
|
7434
|
-
const merged_provider_options = merge_provider_options(engine.default_provider_options, opts_in.provider_options);
|
|
7435
|
-
const opts = {
|
|
7436
|
-
...opts_in,
|
|
7437
|
-
model: resolved_model,
|
|
7438
|
-
provider: resolved_provider
|
|
7439
|
-
};
|
|
7440
|
-
if (opts_in.system === void 0 && engine.default_system !== void 0) opts.system = engine.default_system;
|
|
7441
|
-
if (merged_provider_options !== void 0) opts.provider_options = merged_provider_options;
|
|
7442
|
-
const target = {
|
|
7443
|
-
provider: resolved_provider,
|
|
7444
|
-
model_id: resolved_model
|
|
7445
|
-
};
|
|
8084
|
+
const { opts, target } = resolve_target(opts_in, engine);
|
|
7446
8085
|
const adapter = engine.adapters.get(target.provider);
|
|
7447
8086
|
if (adapter === void 0) throw new provider_not_configured_error(target.provider);
|
|
7448
8087
|
if (adapter.kind === "external") return adapter.generate(opts, target);
|
|
7449
8088
|
const trajectory = with_timestamps(opts.trajectory);
|
|
7450
8089
|
const on_chunk_provided = opts.on_chunk !== void 0;
|
|
7451
8090
|
const tools_list = opts.tools ?? [];
|
|
7452
|
-
|
|
7453
|
-
|
|
7454
|
-
if (on_chunk_provided && !adapter.supports("streaming")) throw new provider_capability_error(target.provider, "streaming");
|
|
7455
|
-
const effort = opts.effort ?? engine.default_effort;
|
|
7456
|
-
const retry_policy = opts.retry ?? engine.default_retry;
|
|
7457
|
-
const turn_timeout_ms = opts.turn_timeout_ms ?? engine.default_turn_timeout_ms;
|
|
7458
|
-
if (turn_timeout_ms !== void 0 && turn_timeout_ms <= 0) throw new engine_config_error("turn_timeout_ms must be > 0");
|
|
8091
|
+
assert_capabilities(adapter, target.provider, opts, tools_list, on_chunk_provided);
|
|
8092
|
+
const turn = resolve_turn_config(opts, engine);
|
|
7459
8093
|
const dispatcher = create_chunk_dispatcher(opts.on_chunk);
|
|
7460
8094
|
let invoke_once;
|
|
7461
|
-
if (adapter.kind === "ai_sdk") {
|
|
7462
|
-
|
|
7463
|
-
|
|
7464
|
-
|
|
7465
|
-
|
|
7466
|
-
|
|
7467
|
-
|
|
7468
|
-
|
|
7469
|
-
|
|
7470
|
-
|
|
7471
|
-
|
|
7472
|
-
|
|
7473
|
-
|
|
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({
|
|
8095
|
+
if (adapter.kind === "ai_sdk") invoke_once = await build_ai_sdk_transport({
|
|
8096
|
+
adapter,
|
|
8097
|
+
target,
|
|
8098
|
+
opts,
|
|
8099
|
+
effort: turn.effort,
|
|
8100
|
+
retry_policy: turn.retry_policy,
|
|
8101
|
+
turn_timeout_ms: turn.turn_timeout_ms,
|
|
8102
|
+
dispatcher,
|
|
8103
|
+
tools_list,
|
|
8104
|
+
trajectory,
|
|
8105
|
+
telemetry: engine.default_ai_sdk_telemetry
|
|
8106
|
+
});
|
|
8107
|
+
else invoke_once = build_native_invoke({
|
|
7490
8108
|
adapter,
|
|
7491
8109
|
model_id: target.model_id,
|
|
7492
|
-
retry_policy,
|
|
7493
|
-
turn_timeout_ms,
|
|
8110
|
+
retry_policy: turn.retry_policy,
|
|
8111
|
+
turn_timeout_ms: turn.turn_timeout_ms,
|
|
7494
8112
|
dispatcher,
|
|
7495
|
-
effort,
|
|
8113
|
+
effort: turn.effort,
|
|
7496
8114
|
schema: opts.schema,
|
|
7497
8115
|
provider_options: opts.provider_options,
|
|
7498
8116
|
temperature: opts.temperature,
|
|
7499
8117
|
max_tokens: opts.max_tokens,
|
|
7500
8118
|
top_p: opts.top_p
|
|
7501
8119
|
});
|
|
7502
|
-
const
|
|
7503
|
-
const
|
|
7504
|
-
const tool_error_policy = opts.tool_error_policy ?? engine.default_tool_error_policy ?? "feed_back";
|
|
7505
|
-
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");
|
|
7508
|
-
const max_tool_calls_per_step = opts.max_tool_calls_per_step ?? engine.default_max_tool_calls_per_step;
|
|
7509
|
-
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");
|
|
7510
|
-
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
|
-
const initial_messages = build_initial_messages(opts);
|
|
7512
|
-
if (schema_prefix !== void 0) {
|
|
7513
|
-
const idx = initial_messages.findIndex((m) => m.role === "system");
|
|
7514
|
-
if (idx >= 0) {
|
|
7515
|
-
const sys = initial_messages[idx];
|
|
7516
|
-
if (sys?.role === "system") initial_messages[idx] = {
|
|
7517
|
-
role: "system",
|
|
7518
|
-
content: `${sys.content}\n\n${schema_prefix}`
|
|
7519
|
-
};
|
|
7520
|
-
} else initial_messages.unshift({
|
|
7521
|
-
role: "system",
|
|
7522
|
-
content: schema_prefix
|
|
7523
|
-
});
|
|
7524
|
-
}
|
|
8120
|
+
const limits = resolve_loop_limits(opts, engine);
|
|
8121
|
+
const initial_messages = build_prompt_messages(opts);
|
|
7525
8122
|
const generate_span = start_generate_span(trajectory, {
|
|
7526
|
-
model:
|
|
8123
|
+
model: target.model_id,
|
|
7527
8124
|
provider: target.provider,
|
|
7528
8125
|
model_id: target.model_id,
|
|
7529
8126
|
has_tools: tools_list.length > 0,
|
|
7530
8127
|
has_schema: opts.schema !== void 0,
|
|
7531
8128
|
streaming: on_chunk_provided
|
|
7532
8129
|
});
|
|
7533
|
-
const
|
|
7534
|
-
|
|
7535
|
-
|
|
7536
|
-
|
|
7537
|
-
|
|
7538
|
-
|
|
7539
|
-
|
|
7540
|
-
|
|
7541
|
-
|
|
7542
|
-
|
|
7543
|
-
|
|
7544
|
-
|
|
7545
|
-
let finish_reason = "stop";
|
|
7546
|
-
let repair_remaining = schema_repair_attempts;
|
|
7547
|
-
let content_parsed;
|
|
7548
|
-
const salvage_budget = tool_call_repair_attempts > 0 ? { remaining: tool_call_repair_attempts } : void 0;
|
|
8130
|
+
const base = build_loop_config({
|
|
8131
|
+
opts,
|
|
8132
|
+
target,
|
|
8133
|
+
pricing: engine.pricing,
|
|
8134
|
+
invoke_once,
|
|
8135
|
+
messages: [...initial_messages],
|
|
8136
|
+
tools_list,
|
|
8137
|
+
trajectory,
|
|
8138
|
+
dispatcher,
|
|
8139
|
+
on_chunk_provided,
|
|
8140
|
+
limits
|
|
8141
|
+
});
|
|
7549
8142
|
try {
|
|
7550
|
-
|
|
7551
|
-
|
|
7552
|
-
|
|
7553
|
-
|
|
7554
|
-
|
|
7555
|
-
|
|
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));
|
|
7591
|
-
}
|
|
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;
|
|
8143
|
+
const outcome = await run_generate_loop({
|
|
8144
|
+
base,
|
|
8145
|
+
schema: opts.schema,
|
|
8146
|
+
schema_repair_attempts: limits.schema_repair_attempts
|
|
8147
|
+
});
|
|
8148
|
+
const result = assemble_result(outcome, target);
|
|
7611
8149
|
if (on_chunk_provided) await dispatcher.dispatch({
|
|
7612
8150
|
kind: "finish",
|
|
7613
|
-
finish_reason,
|
|
7614
|
-
usage:
|
|
8151
|
+
finish_reason: outcome.finish_reason,
|
|
8152
|
+
usage: result.usage
|
|
7615
8153
|
});
|
|
7616
8154
|
end_generate_span(trajectory, generate_span, {
|
|
7617
|
-
usage:
|
|
7618
|
-
finish_reason,
|
|
8155
|
+
usage: result.usage,
|
|
8156
|
+
finish_reason: outcome.finish_reason,
|
|
7619
8157
|
model_resolved: result.model_resolved
|
|
7620
8158
|
});
|
|
7621
8159
|
return result;
|
|
@@ -7644,46 +8182,57 @@ function last_provider_reported(steps) {
|
|
|
7644
8182
|
return steps.findLast((s) => s.provider_reported !== void 0)?.provider_reported;
|
|
7645
8183
|
}
|
|
7646
8184
|
/**
|
|
7647
|
-
*
|
|
7648
|
-
*
|
|
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.
|
|
8185
|
+
* Fold every step's `cost` into a single {@link CostAccumulator}, skipping steps
|
|
8186
|
+
* that reported none. The required fields always sum; each optional field sums
|
|
8187
|
+
* and flips its `_present` flag only on the steps that carried it.
|
|
7655
8188
|
*/
|
|
7656
|
-
function
|
|
7657
|
-
|
|
7658
|
-
|
|
7659
|
-
|
|
7660
|
-
|
|
7661
|
-
|
|
7662
|
-
|
|
7663
|
-
|
|
7664
|
-
|
|
7665
|
-
|
|
7666
|
-
|
|
8189
|
+
function sum_step_costs(steps) {
|
|
8190
|
+
const acc = {
|
|
8191
|
+
any_present: false,
|
|
8192
|
+
total: 0,
|
|
8193
|
+
input: 0,
|
|
8194
|
+
output: 0,
|
|
8195
|
+
cached_present: false,
|
|
8196
|
+
cached: 0,
|
|
8197
|
+
cache_write_present: false,
|
|
8198
|
+
cache_write: 0,
|
|
8199
|
+
reasoning_present: false,
|
|
8200
|
+
reasoning: 0
|
|
8201
|
+
};
|
|
7667
8202
|
for (const s of steps) {
|
|
7668
8203
|
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;
|
|
8204
|
+
acc.any_present = true;
|
|
8205
|
+
acc.total += s.cost.total_usd;
|
|
8206
|
+
acc.input += s.cost.input_usd;
|
|
8207
|
+
acc.output += s.cost.output_usd;
|
|
7673
8208
|
if (s.cost.cached_input_usd !== void 0) {
|
|
7674
|
-
cached_present = true;
|
|
7675
|
-
cached += s.cost.cached_input_usd;
|
|
8209
|
+
acc.cached_present = true;
|
|
8210
|
+
acc.cached += s.cost.cached_input_usd;
|
|
7676
8211
|
}
|
|
7677
8212
|
if (s.cost.cache_write_usd !== void 0) {
|
|
7678
|
-
cache_write_present = true;
|
|
7679
|
-
cache_write += s.cost.cache_write_usd;
|
|
8213
|
+
acc.cache_write_present = true;
|
|
8214
|
+
acc.cache_write += s.cost.cache_write_usd;
|
|
7680
8215
|
}
|
|
7681
8216
|
if (s.cost.reasoning_usd !== void 0) {
|
|
7682
|
-
reasoning_present = true;
|
|
7683
|
-
reasoning += s.cost.reasoning_usd;
|
|
8217
|
+
acc.reasoning_present = true;
|
|
8218
|
+
acc.reasoning += s.cost.reasoning_usd;
|
|
7684
8219
|
}
|
|
7685
8220
|
}
|
|
7686
|
-
|
|
8221
|
+
return acc;
|
|
8222
|
+
}
|
|
8223
|
+
/**
|
|
8224
|
+
* Sum per-step cost breakdowns into a single `CostBreakdown` for the whole
|
|
8225
|
+
* generate call.
|
|
8226
|
+
*
|
|
8227
|
+
* Returns `undefined` if no step reports a cost, unless `provider` is free
|
|
8228
|
+
* (`FREE_PROVIDERS`) and at least one step ran; in that case it returns an
|
|
8229
|
+
* explicit all-zero breakdown instead of omitting cost entirely. The
|
|
8230
|
+
* optional fields (`cached_input_usd`, `cache_write_usd`, `reasoning_usd`)
|
|
8231
|
+
* are included only when at least one step reported them.
|
|
8232
|
+
*/
|
|
8233
|
+
function aggregate_cost(steps, provider) {
|
|
8234
|
+
const acc = sum_step_costs(steps);
|
|
8235
|
+
if (!acc.any_present) {
|
|
7687
8236
|
if (FREE_PROVIDERS.has(provider) && steps.length > 0) return {
|
|
7688
8237
|
total_usd: 0,
|
|
7689
8238
|
input_usd: 0,
|
|
@@ -7694,15 +8243,15 @@ function aggregate_cost(steps, provider) {
|
|
|
7694
8243
|
return;
|
|
7695
8244
|
}
|
|
7696
8245
|
const out = {
|
|
7697
|
-
total_usd: round6(total),
|
|
7698
|
-
input_usd: round6(input),
|
|
7699
|
-
output_usd: round6(output),
|
|
8246
|
+
total_usd: round6(acc.total),
|
|
8247
|
+
input_usd: round6(acc.input),
|
|
8248
|
+
output_usd: round6(acc.output),
|
|
7700
8249
|
currency: "USD",
|
|
7701
8250
|
is_estimate: true
|
|
7702
8251
|
};
|
|
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);
|
|
8252
|
+
if (acc.cached_present) out.cached_input_usd = round6(acc.cached);
|
|
8253
|
+
if (acc.cache_write_present) out.cache_write_usd = round6(acc.cache_write);
|
|
8254
|
+
if (acc.reasoning_present) out.reasoning_usd = round6(acc.reasoning);
|
|
7706
8255
|
return out;
|
|
7707
8256
|
}
|
|
7708
8257
|
//#endregion
|
|
@@ -7731,6 +8280,83 @@ function build_provider_adapters(providers, custom_providers) {
|
|
|
7731
8280
|
return adapters;
|
|
7732
8281
|
}
|
|
7733
8282
|
/**
|
|
8283
|
+
* Seed a fresh, mutable pricing table from DEFAULT_PRICING and layer any
|
|
8284
|
+
* per-engine `config.pricing` overrides on top.
|
|
8285
|
+
*
|
|
8286
|
+
* The result is the engine's own table: register_price mutates it in place,
|
|
8287
|
+
* and DEFAULT_PRICING (frozen at module load) is never touched.
|
|
8288
|
+
*/
|
|
8289
|
+
function merge_pricing(overrides) {
|
|
8290
|
+
const pricing = { ...DEFAULT_PRICING };
|
|
8291
|
+
if (overrides !== void 0) for (const [key, value] of Object.entries(overrides)) pricing[key] = value;
|
|
8292
|
+
return pricing;
|
|
8293
|
+
}
|
|
8294
|
+
/**
|
|
8295
|
+
* Resolve the three always-present scalar defaults.
|
|
8296
|
+
*
|
|
8297
|
+
* `config.defaults.*` is the current nested layer; `config.default_retry`,
|
|
8298
|
+
* `config.default_effort`, and `config.default_max_steps` are older top-level
|
|
8299
|
+
* fields kept for compatibility. The nested value wins where both are set, and
|
|
8300
|
+
* the built-in constant is the last resort.
|
|
8301
|
+
*/
|
|
8302
|
+
function resolve_default_scalars(config) {
|
|
8303
|
+
const defaults = config.defaults ?? {};
|
|
8304
|
+
return {
|
|
8305
|
+
default_retry: defaults.retry_policy ?? config.default_retry ?? DEFAULT_RETRY,
|
|
8306
|
+
default_effort: defaults.effort ?? config.default_effort ?? "none",
|
|
8307
|
+
default_max_steps: defaults.max_steps ?? config.default_max_steps ?? 10
|
|
8308
|
+
};
|
|
8309
|
+
}
|
|
8310
|
+
/**
|
|
8311
|
+
* Validate the numeric defaults once, at construction: a bad bound should fail
|
|
8312
|
+
* fast here rather than surfacing deep inside the first generate call that ends
|
|
8313
|
+
* up using the default.
|
|
8314
|
+
*/
|
|
8315
|
+
function validate_numeric_defaults(defaults) {
|
|
8316
|
+
if (defaults === void 0) return;
|
|
8317
|
+
const { tool_call_repair_attempts, max_tool_calls_per_step, turn_timeout_ms } = defaults;
|
|
8318
|
+
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");
|
|
8319
|
+
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");
|
|
8320
|
+
if (turn_timeout_ms !== void 0 && turn_timeout_ms <= 0) throw new engine_config_error("defaults.turn_timeout_ms must be > 0");
|
|
8321
|
+
}
|
|
8322
|
+
/**
|
|
8323
|
+
* Copy `value` onto `target[key]` only when it is defined, so an absent optional
|
|
8324
|
+
* default leaves the key off the object rather than present-as-undefined.
|
|
8325
|
+
*/
|
|
8326
|
+
function assign_if_present$1(target, key, value) {
|
|
8327
|
+
if (value !== void 0) target[key] = value;
|
|
8328
|
+
}
|
|
8329
|
+
/**
|
|
8330
|
+
* Assemble the per-instance EngineInternals from the resolved required fields
|
|
8331
|
+
* plus whichever optional `defaults.*` knobs were supplied.
|
|
8332
|
+
*
|
|
8333
|
+
* The optional defaults form a fixed source-to-target table (`defaults.model` ->
|
|
8334
|
+
* `default_model`, and so on); each is folded in only when present, matching the
|
|
8335
|
+
* conditional-spread shape this replaced.
|
|
8336
|
+
*/
|
|
8337
|
+
function build_internals(base, defaults) {
|
|
8338
|
+
const internals = {
|
|
8339
|
+
pricing: base.pricing,
|
|
8340
|
+
adapters: base.adapters,
|
|
8341
|
+
default_retry: base.default_retry,
|
|
8342
|
+
default_effort: base.default_effort,
|
|
8343
|
+
default_max_steps: base.default_max_steps
|
|
8344
|
+
};
|
|
8345
|
+
if (defaults !== void 0) {
|
|
8346
|
+
assign_if_present$1(internals, "default_model", defaults.model);
|
|
8347
|
+
assign_if_present$1(internals, "default_provider", defaults.provider);
|
|
8348
|
+
assign_if_present$1(internals, "default_system", defaults.system);
|
|
8349
|
+
assign_if_present$1(internals, "default_tool_error_policy", defaults.tool_error_policy);
|
|
8350
|
+
assign_if_present$1(internals, "default_schema_repair_attempts", defaults.schema_repair_attempts);
|
|
8351
|
+
assign_if_present$1(internals, "default_tool_call_repair_attempts", defaults.tool_call_repair_attempts);
|
|
8352
|
+
assign_if_present$1(internals, "default_max_tool_calls_per_step", defaults.max_tool_calls_per_step);
|
|
8353
|
+
assign_if_present$1(internals, "default_turn_timeout_ms", defaults.turn_timeout_ms);
|
|
8354
|
+
assign_if_present$1(internals, "default_ai_sdk_telemetry", defaults.ai_sdk_telemetry);
|
|
8355
|
+
assign_if_present$1(internals, "default_provider_options", defaults.provider_options);
|
|
8356
|
+
}
|
|
8357
|
+
return internals;
|
|
8358
|
+
}
|
|
8359
|
+
/**
|
|
7734
8360
|
* Validate `config` and construct an `Engine`.
|
|
7735
8361
|
*
|
|
7736
8362
|
* Provider entries are validated synchronously via `build_provider_adapters`.
|
|
@@ -7740,32 +8366,16 @@ function build_provider_adapters(providers, custom_providers) {
|
|
|
7740
8366
|
function create_engine(config) {
|
|
7741
8367
|
if (config.providers === null || typeof config.providers !== "object") throw new engine_config_error("EngineConfig.providers is required");
|
|
7742
8368
|
const adapters = build_provider_adapters(config.providers, config.custom_providers);
|
|
7743
|
-
const pricing =
|
|
7744
|
-
|
|
7745
|
-
|
|
7746
|
-
const
|
|
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 = () => ({
|
|
8369
|
+
const pricing = merge_pricing(config.pricing);
|
|
8370
|
+
const { default_retry, default_effort, default_max_steps } = resolve_default_scalars(config);
|
|
8371
|
+
validate_numeric_defaults(config.defaults);
|
|
8372
|
+
const get_internals = () => build_internals({
|
|
7753
8373
|
pricing,
|
|
7754
8374
|
adapters,
|
|
7755
8375
|
default_retry,
|
|
7756
8376
|
default_effort,
|
|
7757
|
-
default_max_steps
|
|
7758
|
-
|
|
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
|
-
});
|
|
8377
|
+
default_max_steps
|
|
8378
|
+
}, config.defaults);
|
|
7769
8379
|
let disposed = false;
|
|
7770
8380
|
let dispose_promise;
|
|
7771
8381
|
return {
|
|
@@ -7871,16 +8481,54 @@ function stable_signature(input) {
|
|
|
7871
8481
|
return createHash("sha256").update(payload).digest("hex").slice(0, 8);
|
|
7872
8482
|
}
|
|
7873
8483
|
/**
|
|
8484
|
+
* Copy `value` onto `target[key]` only when it is defined, so an absent optional
|
|
8485
|
+
* config field leaves the key off `GenerateOptions` rather than present-as-undefined.
|
|
8486
|
+
*/
|
|
8487
|
+
function assign_if_present(target, key, value) {
|
|
8488
|
+
if (value !== void 0) target[key] = value;
|
|
8489
|
+
}
|
|
8490
|
+
/**
|
|
8491
|
+
* Translate `cfg` into a `GenerateOptions`, folding each supplied optional field
|
|
8492
|
+
* in via `assign_if_present`. `tools` (spread to a fresh array) and
|
|
8493
|
+
* `retry_policy` (renamed to `retry`) are the two fields that do not copy
|
|
8494
|
+
* straight across and stay explicit; the rest map name-for-name.
|
|
8495
|
+
*/
|
|
8496
|
+
function build_generate_options(cfg, prompt, ctx) {
|
|
8497
|
+
const opts = {
|
|
8498
|
+
prompt,
|
|
8499
|
+
abort: ctx.abort,
|
|
8500
|
+
trajectory: engine_trajectory(ctx.trajectory, ctx.parent_span_id)
|
|
8501
|
+
};
|
|
8502
|
+
assign_if_present(opts, "model", cfg.model);
|
|
8503
|
+
assign_if_present(opts, "provider", cfg.provider);
|
|
8504
|
+
assign_if_present(opts, "system", cfg.system);
|
|
8505
|
+
if (cfg.tools !== void 0) opts.tools = [...cfg.tools];
|
|
8506
|
+
assign_if_present(opts, "schema", cfg.schema);
|
|
8507
|
+
assign_if_present(opts, "effort", cfg.effort);
|
|
8508
|
+
assign_if_present(opts, "max_steps", cfg.max_steps);
|
|
8509
|
+
assign_if_present(opts, "provider_options", cfg.provider_options);
|
|
8510
|
+
if (cfg.retry_policy !== void 0) opts.retry = cfg.retry_policy;
|
|
8511
|
+
assign_if_present(opts, "tool_error_policy", cfg.tool_error_policy);
|
|
8512
|
+
assign_if_present(opts, "schema_repair_attempts", cfg.schema_repair_attempts);
|
|
8513
|
+
assign_if_present(opts, "tool_call_repair_attempts", cfg.tool_call_repair_attempts);
|
|
8514
|
+
assign_if_present(opts, "max_tool_calls_per_step", cfg.max_tool_calls_per_step);
|
|
8515
|
+
assign_if_present(opts, "on_tool_approval", cfg.on_tool_approval);
|
|
8516
|
+
return opts;
|
|
8517
|
+
}
|
|
8518
|
+
/**
|
|
7874
8519
|
* Build a `Step` that calls `cfg.engine.generate` with `cfg` translated into
|
|
7875
8520
|
* `GenerateOptions`.
|
|
7876
8521
|
*
|
|
7877
8522
|
* Threads `ctx.abort` and `ctx.trajectory` (nested under the model_call
|
|
7878
8523
|
* step's own span) into every call, and, when `run.stream` is driving,
|
|
7879
|
-
* records each chunk as a `model_chunk` trajectory event.
|
|
8524
|
+
* records each chunk as a `model_chunk` trajectory event. `cfg.project`
|
|
8525
|
+
* maps the `GenerateResult` envelope into the step's output inside the same
|
|
8526
|
+
* step; omitted, the envelope is the output.
|
|
7880
8527
|
*/
|
|
7881
8528
|
function model_call(cfg) {
|
|
7882
8529
|
const has_tools = Boolean(cfg.tools && cfg.tools.length > 0);
|
|
7883
8530
|
const has_schema = cfg.schema !== void 0;
|
|
8531
|
+
const project = cfg.project ?? ((r) => r);
|
|
7884
8532
|
const step_id = cfg.id ?? `model_call:${stable_signature({
|
|
7885
8533
|
model: cfg.model,
|
|
7886
8534
|
provider: cfg.provider,
|
|
@@ -7890,7 +8538,8 @@ function model_call(cfg) {
|
|
|
7890
8538
|
})}`;
|
|
7891
8539
|
const describe_config = {
|
|
7892
8540
|
has_tools,
|
|
7893
|
-
has_schema
|
|
8541
|
+
has_schema,
|
|
8542
|
+
has_project: cfg.project !== void 0
|
|
7894
8543
|
};
|
|
7895
8544
|
if (cfg.model !== void 0) describe_config.model = cfg.model;
|
|
7896
8545
|
if (cfg.provider !== void 0) describe_config.provider = cfg.provider;
|
|
@@ -7901,31 +8550,13 @@ function model_call(cfg) {
|
|
|
7901
8550
|
reason: { signal: "abort" },
|
|
7902
8551
|
step_index: 0
|
|
7903
8552
|
});
|
|
7904
|
-
const opts = {
|
|
7905
|
-
|
|
7906
|
-
|
|
7907
|
-
|
|
7908
|
-
|
|
7909
|
-
|
|
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;
|
|
8553
|
+
const opts = build_generate_options(cfg, typeof input === "string" ? [{
|
|
8554
|
+
role: "user",
|
|
8555
|
+
content: [{
|
|
8556
|
+
type: "text",
|
|
8557
|
+
text: input
|
|
8558
|
+
}]
|
|
8559
|
+
}] : [...input], ctx);
|
|
7929
8560
|
if (ctx.streaming) opts.on_chunk = (chunk) => {
|
|
7930
8561
|
ctx.trajectory.record({
|
|
7931
8562
|
kind: "model_chunk",
|
|
@@ -7933,7 +8564,7 @@ function model_call(cfg) {
|
|
|
7933
8564
|
chunk
|
|
7934
8565
|
});
|
|
7935
8566
|
};
|
|
7936
|
-
return cfg.engine.generate(opts);
|
|
8567
|
+
return project(await cfg.engine.generate(opts));
|
|
7937
8568
|
});
|
|
7938
8569
|
return {
|
|
7939
8570
|
id: inner.id,
|
|
@@ -7942,6 +8573,28 @@ function model_call(cfg) {
|
|
|
7942
8573
|
config: Object.freeze({ ...describe_config })
|
|
7943
8574
|
};
|
|
7944
8575
|
}
|
|
8576
|
+
/**
|
|
8577
|
+
* model_step: `model_call` projected to its content.
|
|
8578
|
+
*
|
|
8579
|
+
* Returns a Step whose output is the model's final content (a `string`, or
|
|
8580
|
+
* the schema-validated value when `cfg.schema` is set) instead of the full
|
|
8581
|
+
* `GenerateResult` envelope. Use `model_step` when the flow only wants the
|
|
8582
|
+
* answer, keeping compositions at the `step, step, model_step, step` cadence;
|
|
8583
|
+
* drop to `model_call` when the caller needs usage, cost, tool calls, or
|
|
8584
|
+
* finish reason (or its `project` option, when the flow wants a slice of the
|
|
8585
|
+
* envelope mapped at the source).
|
|
8586
|
+
*
|
|
8587
|
+
* Implemented as `model_call` with `project` preset to `(r) => r.content`,
|
|
8588
|
+
* so the leaf is a single node in `describe` and the trajectory. Wrapping a
|
|
8589
|
+
* primitive into an app-shaped helper of your own is still the extension
|
|
8590
|
+
* pattern; this is its smallest instance.
|
|
8591
|
+
*/
|
|
8592
|
+
function model_step(cfg) {
|
|
8593
|
+
return model_call({
|
|
8594
|
+
...cfg,
|
|
8595
|
+
project: (r) => r.content
|
|
8596
|
+
});
|
|
8597
|
+
}
|
|
7945
8598
|
//#endregion
|
|
7946
8599
|
//#region src/forward_standard_env.ts
|
|
7947
8600
|
/**
|
|
@@ -8423,19 +9076,21 @@ async function start_viewer(options = {}) {
|
|
|
8423
9076
|
...options.on_parse_error ? { on_parse_error: options.on_parse_error } : {},
|
|
8424
9077
|
...options.on_io_error ? { on_io_error: options.on_io_error } : {}
|
|
8425
9078
|
});
|
|
8426
|
-
return wrap_handle(server, tail, host
|
|
9079
|
+
return wrap_handle(server, tail, host);
|
|
8427
9080
|
}
|
|
8428
9081
|
/**
|
|
8429
9082
|
* Combines the server and optional tail into a single `ViewerHandle`.
|
|
8430
9083
|
*
|
|
8431
9084
|
* `close` stops the tail first, then closes the server, mirroring the
|
|
8432
|
-
* startup order (server first, tail second) in reverse.
|
|
9085
|
+
* startup order (server first, tail second) in reverse. The port is read
|
|
9086
|
+
* back from `server.url` rather than the requested `port` option, since
|
|
9087
|
+
* a caller that passes `0` gets whatever port the OS actually bound.
|
|
8433
9088
|
*/
|
|
8434
|
-
function wrap_handle(server, tail, host
|
|
9089
|
+
function wrap_handle(server, tail, host) {
|
|
8435
9090
|
return {
|
|
8436
9091
|
url: server.url,
|
|
8437
9092
|
host,
|
|
8438
|
-
port,
|
|
9093
|
+
port: Number(new URL(server.url).port),
|
|
8439
9094
|
close: async () => {
|
|
8440
9095
|
if (tail) tail.stop();
|
|
8441
9096
|
await server.close();
|
|
@@ -8626,6 +9281,6 @@ if (process.argv[1] !== void 0 && process.argv[1] === fileURLToPath(import.meta.
|
|
|
8626
9281
|
process.exit(1);
|
|
8627
9282
|
});
|
|
8628
9283
|
//#endregion
|
|
8629
|
-
export { STEP_KINDS, aborted_error, adversarial, bench, bench_suspend_error, branch, checkpoint, claude_cli_error, compose, consensus, create_engine, default_normalize_usage, describe, describe_cycle_error, engine_config_error, engine_disposed_error, ensemble, ensemble_step, fallback, flow_schema_default as flow_schema, forward_standard_env, improve, incomplete_generation_error, is_custom_trajectory_event, is_emit_event, is_span_end_event, is_span_start_event, is_step_kind, judge_equals, judge_llm, judge_with, learn, loop, map, model_call, model_required_error, normalize_score, on_chunk_error, parallel, parse_trajectory_event, pipe, provider_auth_error, provider_capability_error, provider_error, provider_not_configured_error, rate_limit_error, read_baseline, regression_compare, resume_validation_error, retry, run, run_viewer_cli, schema_validation_error, scope, sequence, start_viewer, stash, step, suspend, suspended_error, timeout, timeout_error, tool_approval_denied_error, tool_error, tournament, use, write_baseline };
|
|
9284
|
+
export { STEP_KINDS, aborted_error, adversarial, bench, bench_suspend_error, branch, chain, checkpoint, claude_cli_error, compose, consensus, create_engine, default_normalize_usage, describe, describe_cycle_error, engine_config_error, engine_disposed_error, ensemble, ensemble_step, fallback, flow_schema_default as flow_schema, forward_standard_env, improve, incomplete_generation_error, is_custom_trajectory_event, is_emit_event, is_span_end_event, is_span_start_event, is_step_kind, judge_equals, judge_llm, judge_with, learn, loop, map, model_call, model_required_error, model_step, normalize_score, on_chunk_error, parallel, parse_trajectory_event, pipe, provider_auth_error, provider_capability_error, provider_error, provider_not_configured_error, rate_limit_error, read_baseline, regression_compare, resume_validation_error, retry, run, run_viewer_cli, schema_validation_error, scope, sequence, start_viewer, stash, step, suspend, suspended_error, timeout, timeout_error, tool_approval_denied_error, tool_error, tournament, use, write_baseline };
|
|
8630
9285
|
|
|
8631
9286
|
//# sourceMappingURL=index.js.map
|