fascicle 0.10.3 → 0.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/dist/{adapters-CJnu1687.js → adapters-hmnJOOgf.js} +30 -64
- package/dist/adapters-hmnJOOgf.js.map +1 -0
- package/dist/adapters.js +1 -1
- package/dist/agents.js +51 -29
- package/dist/agents.js.map +1 -1
- package/dist/{core-DkuJqLwd.js → core-DgH4mDbK.js} +205 -114
- package/dist/core-DgH4mDbK.js.map +1 -0
- package/dist/index.d.ts +17 -23
- package/dist/index.js +2352 -1641
- package/dist/index.js.map +1 -1
- package/dist/{invoke-D9pxbZcu.js → invoke-BjrbCBSV.js} +101 -66
- package/dist/invoke-BjrbCBSV.js.map +1 -0
- package/dist/{types-kO1JoGNZ.js → leading_system-CpkjHoCQ.js} +57 -3
- package/dist/leading_system-CpkjHoCQ.js.map +1 -0
- package/dist/mcp.js +1 -1
- package/dist/stdio.js +2 -2
- package/package.json +1 -1
- package/dist/adapters-CJnu1687.js.map +0 -1
- package/dist/core-DkuJqLwd.js.map +0 -1
- package/dist/invoke-D9pxbZcu.js.map +0 -1
- package/dist/types-kO1JoGNZ.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { A as aborted_error, C as map, D as describe, E as sequence, F as flow_schema_default, M as resume_validation_error, N as suspended_error, O as step, P as timeout_error, S as pipe, T as parallel, _ as timeout, a as parse_trajectory_event, b as compute_backoff, c as scope, d as suspend, f as format_schema_issues, g as loop, h as compose, i as is_span_start_event, j as describe_cycle_error, k as run, l as stash, m as checkpoint, n as is_emit_event, o as STEP_KINDS, p as validate_schema, r as is_span_end_event, s as is_step_kind, t as is_custom_trajectory_event, u as use, v as fallback, w as branch, x as wait_with_abort, y as retry } from "./core-
|
|
2
|
-
import { a as http_logger, n as tee_logger, o as filesystem_logger } from "./adapters-
|
|
3
|
-
import { _ as
|
|
1
|
+
import { A as aborted_error, C as map, D as describe, E as sequence, F as flow_schema_default, M as resume_validation_error, N as suspended_error, O as step, P as timeout_error, S as pipe, T as parallel, _ as timeout, a as parse_trajectory_event, b as compute_backoff, c as scope, d as suspend, f as format_schema_issues, g as loop, h as compose, i as is_span_start_event, j as describe_cycle_error, k as run, l as stash, m as checkpoint, n as is_emit_event, o as STEP_KINDS, p as validate_schema, r as is_span_end_event, s as is_step_kind, t as is_custom_trajectory_event, u as use, v as fallback, w as branch, x as wait_with_abort, y as retry } from "./core-DgH4mDbK.js";
|
|
2
|
+
import { a as http_logger, n as tee_logger, o as filesystem_logger } from "./adapters-hmnJOOgf.js";
|
|
3
|
+
import { _ as rate_limit_error, a as normalize_local_usage, b as tool_error, c as engine_config_error, d as model_required_error, f as on_chunk_error, g as provider_not_configured_error, h as provider_error, i as missing_peer_error, l as engine_disposed_error, m as provider_capability_error, n as default_normalize_usage, o as resolve_transport, p as provider_auth_error, r as load_optional_peer, s as claude_cli_error, t as split_leading_system_run, u as incomplete_generation_error, v as schema_validation_error, x as turn_timeout_error, y as tool_approval_denied_error } from "./leading_system-CpkjHoCQ.js";
|
|
4
4
|
import { createHash } from "node:crypto";
|
|
5
5
|
import { mkdir, open, readFile, readdir, stat, writeFile } from "node:fs/promises";
|
|
6
6
|
import { dirname, join, resolve } from "node:path";
|
|
@@ -31,13 +31,18 @@ var schema_not_convertible_error = class extends Error {
|
|
|
31
31
|
/**
|
|
32
32
|
* JSON Schema emission for schemas that reach a model.
|
|
33
33
|
*
|
|
34
|
-
* Standard JSON Schema exposes two directions, `input` and `output`.
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
34
|
+
* Standard JSON Schema exposes two directions, `input` and `output`. A tool
|
|
35
|
+
* definition describes what the model should *produce*, so fascicle emits the
|
|
36
|
+
* input direction: a field with a default is one the model may omit, and
|
|
37
|
+
* saying otherwise asks it to supply a value the schema already supplies.
|
|
38
|
+
*
|
|
39
|
+
* The input direction alone is too loose, though. A vendor describes what it
|
|
40
|
+
* will accept, and accepting unknown keys is the common default (zod's
|
|
41
|
+
* `z.object` strips them, ArkType ignores them), so nothing in the emitted
|
|
42
|
+
* schema stops a model inventing one. `close_objects` supplies that guard,
|
|
43
|
+
* which is also what the strict and constrained-decode modes require. The AI
|
|
44
|
+
* SDK composes the same two halves for every `kind: 'ai_sdk'` provider, so
|
|
45
|
+
* this is what the majority of fascicle's transports already send.
|
|
41
46
|
*/
|
|
42
47
|
const DEFAULT_TARGET = "draft-2020-12";
|
|
43
48
|
/**
|
|
@@ -51,10 +56,86 @@ const DEFAULT_TARGET = "draft-2020-12";
|
|
|
51
56
|
*/
|
|
52
57
|
function to_json_schema(schema, options = {}) {
|
|
53
58
|
const props = schema["~standard"];
|
|
54
|
-
if (typeof props.jsonSchema?.
|
|
55
|
-
const json = props.jsonSchema.
|
|
59
|
+
if (typeof props.jsonSchema?.input !== "function") throw new schema_not_convertible_error(props.vendor);
|
|
60
|
+
const json = close_objects(props.jsonSchema.input({ target: options.target ?? DEFAULT_TARGET }));
|
|
56
61
|
return options.strip_meta === true ? strip_meta_keys(json) : json;
|
|
57
62
|
}
|
|
63
|
+
/** Keys whose value is a bag of nested schemas, one per name. */
|
|
64
|
+
const SCHEMA_BAGS = [
|
|
65
|
+
"properties",
|
|
66
|
+
"patternProperties",
|
|
67
|
+
"$defs",
|
|
68
|
+
"definitions"
|
|
69
|
+
];
|
|
70
|
+
/** Keys whose value is a nested schema, or a list of them. */
|
|
71
|
+
const SCHEMA_SLOTS = [
|
|
72
|
+
"items",
|
|
73
|
+
"additionalItems",
|
|
74
|
+
"additionalProperties",
|
|
75
|
+
"propertyNames",
|
|
76
|
+
"not",
|
|
77
|
+
"anyOf",
|
|
78
|
+
"allOf",
|
|
79
|
+
"oneOf",
|
|
80
|
+
"prefixItems"
|
|
81
|
+
];
|
|
82
|
+
/**
|
|
83
|
+
* Add `additionalProperties: false` to every object node that does not already
|
|
84
|
+
* constrain it, so the model is told which keys exist rather than left to
|
|
85
|
+
* invent them.
|
|
86
|
+
*
|
|
87
|
+
* Two restraints keep this from asserting more than the vendor did. An
|
|
88
|
+
* existing `additionalProperties` is never overwritten, so a deliberately open
|
|
89
|
+
* schema (zod's `looseObject`, a `record`'s value schema, a server that
|
|
90
|
+
* published `true`) stays open. And a node is only closed if it declares
|
|
91
|
+
* `properties`: a bare `{ type: 'object' }` is how a freeform payload is
|
|
92
|
+
* described, most visibly by an MCP server, and closing it would forbid every
|
|
93
|
+
* key rather than constrain the listed ones. Every zod object emits
|
|
94
|
+
* `properties`, `z.object({})` included, so the guard costs nothing there.
|
|
95
|
+
*
|
|
96
|
+
* The tree is rebuilt rather than mutated. The spec does not promise a fresh
|
|
97
|
+
* object per call, and a vendor handing back a cached one would be corrupted
|
|
98
|
+
* by an in-place stamp. Recursion terminates on any schema: a self-referential
|
|
99
|
+
* schema emits `$ref`, which is a string, not a nested node.
|
|
100
|
+
*/
|
|
101
|
+
function close_objects(node) {
|
|
102
|
+
const closed = close_nested(node);
|
|
103
|
+
return needs_closing(closed) ? {
|
|
104
|
+
...closed,
|
|
105
|
+
additionalProperties: false
|
|
106
|
+
} : closed;
|
|
107
|
+
}
|
|
108
|
+
/** Rebuild `node` with every schema it holds closed, leaving its own keys alone. */
|
|
109
|
+
function close_nested(node) {
|
|
110
|
+
const closed = { ...node };
|
|
111
|
+
for (const key of SCHEMA_BAGS) {
|
|
112
|
+
const bag = closed[key];
|
|
113
|
+
if (is_node(bag)) closed[key] = close_bag(bag);
|
|
114
|
+
}
|
|
115
|
+
for (const key of SCHEMA_SLOTS) if (key in closed) closed[key] = close_value(closed[key]);
|
|
116
|
+
return closed;
|
|
117
|
+
}
|
|
118
|
+
/** Rebuild a name-to-schema bag with each of its schemas closed. */
|
|
119
|
+
function close_bag(bag) {
|
|
120
|
+
const closed = {};
|
|
121
|
+
for (const [name, value] of Object.entries(bag)) closed[name] = close_value(value);
|
|
122
|
+
return closed;
|
|
123
|
+
}
|
|
124
|
+
/** Recurse into a schema or a list of them, leaving anything else alone. */
|
|
125
|
+
function close_value(value) {
|
|
126
|
+
if (Array.isArray(value)) return value.map(close_value);
|
|
127
|
+
return is_node(value) ? close_objects(value) : value;
|
|
128
|
+
}
|
|
129
|
+
/** True for an object node listing its keys but not saying whether more are allowed. */
|
|
130
|
+
function needs_closing(node) {
|
|
131
|
+
if ("additionalProperties" in node || !("properties" in node)) return false;
|
|
132
|
+
const type = node["type"];
|
|
133
|
+
return type === "object" || Array.isArray(type) && type.includes("object");
|
|
134
|
+
}
|
|
135
|
+
/** A JSON Schema node, as opposed to a boolean schema, a list, or a scalar. */
|
|
136
|
+
function is_node(value) {
|
|
137
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
138
|
+
}
|
|
58
139
|
/**
|
|
59
140
|
* Drop the top-level `$schema` and `$id` keys.
|
|
60
141
|
*
|
|
@@ -108,7 +189,7 @@ function build_input_from_state(s) {
|
|
|
108
189
|
*/
|
|
109
190
|
function adversarial(config) {
|
|
110
191
|
const { build, critique, accept, max_rounds } = config;
|
|
111
|
-
const inner =
|
|
192
|
+
const inner = loop({
|
|
112
193
|
init: (input) => ({ input }),
|
|
113
194
|
body: scope([
|
|
114
195
|
stash("state", step("snapshot", (s) => s)),
|
|
@@ -140,16 +221,16 @@ function adversarial(config) {
|
|
|
140
221
|
};
|
|
141
222
|
})
|
|
142
223
|
]),
|
|
143
|
-
finish: (s) => {
|
|
224
|
+
finish: (s, { converged, rounds }) => {
|
|
144
225
|
if (s.candidate === void 0) throw new Error("adversarial: finished without a candidate");
|
|
145
|
-
return
|
|
226
|
+
return {
|
|
227
|
+
candidate: s.candidate,
|
|
228
|
+
converged,
|
|
229
|
+
rounds
|
|
230
|
+
};
|
|
146
231
|
},
|
|
147
232
|
max_rounds
|
|
148
|
-
})
|
|
149
|
-
candidate: result.value,
|
|
150
|
-
converged: result.converged,
|
|
151
|
-
rounds: result.rounds
|
|
152
|
-
}));
|
|
233
|
+
});
|
|
153
234
|
return compose(config.name ?? "adversarial", inner);
|
|
154
235
|
}
|
|
155
236
|
//#endregion
|
|
@@ -172,7 +253,7 @@ function adversarial(config) {
|
|
|
172
253
|
function consensus(config) {
|
|
173
254
|
const { members, agree, max_rounds } = config;
|
|
174
255
|
const round_step = parallel(members);
|
|
175
|
-
const inner =
|
|
256
|
+
const inner = loop({
|
|
176
257
|
init: (input) => ({
|
|
177
258
|
input,
|
|
178
259
|
results: {}
|
|
@@ -192,12 +273,12 @@ function consensus(config) {
|
|
|
192
273
|
stop: agree(s.results),
|
|
193
274
|
state: s
|
|
194
275
|
})),
|
|
195
|
-
finish: (s) =>
|
|
276
|
+
finish: (s, { converged }) => ({
|
|
277
|
+
result: s.results,
|
|
278
|
+
converged
|
|
279
|
+
}),
|
|
196
280
|
max_rounds
|
|
197
|
-
})
|
|
198
|
-
result: result.value,
|
|
199
|
-
converged: result.converged
|
|
200
|
-
}));
|
|
281
|
+
});
|
|
201
282
|
return compose(config.name ?? "consensus", inner);
|
|
202
283
|
}
|
|
203
284
|
//#endregion
|
|
@@ -508,14 +589,14 @@ function improve(config) {
|
|
|
508
589
|
}),
|
|
509
590
|
max_rounds
|
|
510
591
|
}),
|
|
511
|
-
step("
|
|
592
|
+
step("improve_stop", (result, ctx) => {
|
|
512
593
|
ctx.trajectory.record({
|
|
513
594
|
kind: "improve.stop",
|
|
514
|
-
stopped_by: result.
|
|
515
|
-
rounds_used: result.
|
|
516
|
-
final_score: result.
|
|
595
|
+
stopped_by: result.stopped_by,
|
|
596
|
+
rounds_used: result.rounds_used,
|
|
597
|
+
final_score: result.best.score
|
|
517
598
|
});
|
|
518
|
-
return result
|
|
599
|
+
return result;
|
|
519
600
|
})
|
|
520
601
|
]);
|
|
521
602
|
return compose(config.name ?? "improve", inner);
|
|
@@ -725,40 +806,55 @@ function tournament(config) {
|
|
|
725
806
|
let round = 0;
|
|
726
807
|
while (current.length > 1) {
|
|
727
808
|
round += 1;
|
|
728
|
-
|
|
729
|
-
for (let i = 0; i < current.length; i += 2) {
|
|
730
|
-
const a_id = current[i];
|
|
731
|
-
const b_id = current[i + 1];
|
|
732
|
-
if (a_id === void 0) continue;
|
|
733
|
-
if (b_id === void 0) {
|
|
734
|
-
next_round.push(a_id);
|
|
735
|
-
continue;
|
|
736
|
-
}
|
|
737
|
-
const a_val = results[a_id];
|
|
738
|
-
const b_val = results[b_id];
|
|
739
|
-
if (a_val === void 0 || b_val === void 0) throw new Error("tournament: missing result for match");
|
|
740
|
-
const winner_id = await compare_fn(a_val, b_val) === "a" ? a_id : b_id;
|
|
741
|
-
bracket.push({
|
|
742
|
-
round,
|
|
743
|
-
a_id,
|
|
744
|
-
b_id,
|
|
745
|
-
winner_id
|
|
746
|
-
});
|
|
747
|
-
next_round.push(winner_id);
|
|
748
|
-
}
|
|
749
|
-
current = next_round;
|
|
809
|
+
current = await play_round(current, results, compare_fn, round, bracket);
|
|
750
810
|
}
|
|
751
|
-
const final_id = current[0];
|
|
752
|
-
if (final_id === void 0) throw new Error("tournament: no winner");
|
|
753
|
-
const winner = results[final_id];
|
|
754
|
-
if (winner === void 0) throw new Error("tournament: winner missing from results");
|
|
755
811
|
return {
|
|
756
|
-
winner,
|
|
812
|
+
winner: winner_of(current, results),
|
|
757
813
|
bracket
|
|
758
814
|
};
|
|
759
815
|
})]);
|
|
760
816
|
return compose(config.name ?? "tournament", inner);
|
|
761
817
|
}
|
|
818
|
+
/**
|
|
819
|
+
* Plays one bracket round: pairs survivors two at a time, comparing each pair
|
|
820
|
+
* and recording a `BracketRecord`, and returns the ids advancing. An unpaired
|
|
821
|
+
* trailing survivor takes a bye. Every paired id must have a settled result.
|
|
822
|
+
*/
|
|
823
|
+
async function play_round(current, results, compare, round, bracket) {
|
|
824
|
+
const next_round = [];
|
|
825
|
+
for (let i = 0; i < current.length; i += 2) {
|
|
826
|
+
const a_id = current[i];
|
|
827
|
+
const b_id = current[i + 1];
|
|
828
|
+
if (a_id === void 0) continue;
|
|
829
|
+
if (b_id === void 0) {
|
|
830
|
+
next_round.push(a_id);
|
|
831
|
+
continue;
|
|
832
|
+
}
|
|
833
|
+
const a_val = results[a_id];
|
|
834
|
+
const b_val = results[b_id];
|
|
835
|
+
if (a_val === void 0 || b_val === void 0) throw new Error("tournament: missing result for match");
|
|
836
|
+
const winner_id = await compare(a_val, b_val) === "a" ? a_id : b_id;
|
|
837
|
+
bracket.push({
|
|
838
|
+
round,
|
|
839
|
+
a_id,
|
|
840
|
+
b_id,
|
|
841
|
+
winner_id
|
|
842
|
+
});
|
|
843
|
+
next_round.push(winner_id);
|
|
844
|
+
}
|
|
845
|
+
return next_round;
|
|
846
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* Extracts the tournament winner from the single surviving id, throwing when
|
|
849
|
+
* no survivor remains or its result is missing.
|
|
850
|
+
*/
|
|
851
|
+
function winner_of(current, results) {
|
|
852
|
+
const final_id = current[0];
|
|
853
|
+
if (final_id === void 0) throw new Error("tournament: no winner");
|
|
854
|
+
const winner = results[final_id];
|
|
855
|
+
if (winner === void 0) throw new Error("tournament: winner missing from results");
|
|
856
|
+
return winner;
|
|
857
|
+
}
|
|
762
858
|
//#endregion
|
|
763
859
|
//#region src/composites/errors.ts
|
|
764
860
|
/**
|
|
@@ -824,12 +920,12 @@ async function bench(flow, cases, judges, options = {}) {
|
|
|
824
920
|
if (options.trajectory_dir !== void 0) await mkdir(options.trajectory_dir, { recursive: true });
|
|
825
921
|
const judge_entries = Object.entries(judges);
|
|
826
922
|
const run_one = async (bc) => {
|
|
827
|
-
const trajectory_path
|
|
828
|
-
const
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
923
|
+
const { trajectory_path, case_logger, cost_tracker } = build_case_logging(options, bc.id);
|
|
924
|
+
const ctx = {
|
|
925
|
+
case_logger,
|
|
926
|
+
install_signal_handlers,
|
|
927
|
+
run_options
|
|
928
|
+
};
|
|
833
929
|
const start = Date.now();
|
|
834
930
|
let output;
|
|
835
931
|
let error;
|
|
@@ -846,33 +942,7 @@ async function bench(flow, cases, judges, options = {}) {
|
|
|
846
942
|
error = err instanceof Error ? err.message : String(err);
|
|
847
943
|
ok = false;
|
|
848
944
|
}
|
|
849
|
-
const scores = {};
|
|
850
|
-
if (ok && output !== void 0) {
|
|
851
|
-
const judge_input = bc.meta === void 0 ? {
|
|
852
|
-
input: bc.input,
|
|
853
|
-
output
|
|
854
|
-
} : {
|
|
855
|
-
input: bc.input,
|
|
856
|
-
output,
|
|
857
|
-
meta: bc.meta
|
|
858
|
-
};
|
|
859
|
-
for (const [name, judge] of judge_entries) {
|
|
860
|
-
let raw;
|
|
861
|
-
try {
|
|
862
|
-
raw = await run(judge, judge_input, {
|
|
863
|
-
trajectory: case_logger,
|
|
864
|
-
install_signal_handlers,
|
|
865
|
-
...run_options
|
|
866
|
-
});
|
|
867
|
-
} catch (err) {
|
|
868
|
-
rethrow_control_flow(err, bc.id);
|
|
869
|
-
continue;
|
|
870
|
-
}
|
|
871
|
-
const normalized = normalize_score(raw);
|
|
872
|
-
if (normalized === void 0) continue;
|
|
873
|
-
scores[name] = normalized;
|
|
874
|
-
}
|
|
875
|
-
}
|
|
945
|
+
const scores = ok && output !== void 0 ? await run_judges(judge_entries, build_judge_input(bc, output), ctx, bc.id) : {};
|
|
876
946
|
const duration_ms = Date.now() - start;
|
|
877
947
|
const result = {
|
|
878
948
|
case_id: bc.id,
|
|
@@ -897,6 +967,65 @@ async function bench(flow, cases, judges, options = {}) {
|
|
|
897
967
|
};
|
|
898
968
|
}
|
|
899
969
|
/**
|
|
970
|
+
* Builds the per-case trajectory pipeline: an optional filesystem sink when
|
|
971
|
+
* `trajectory_dir` is set, an optional HTTP sink when `live_url` is set, and
|
|
972
|
+
* always an in-process cost tracker, all tee'd together. Returns the resolved
|
|
973
|
+
* trajectory path (undefined when no dir is set) alongside the logger and
|
|
974
|
+
* tracker.
|
|
975
|
+
*/
|
|
976
|
+
function build_case_logging(options, case_id) {
|
|
977
|
+
const trajectory_path = options.trajectory_dir === void 0 ? void 0 : join(options.trajectory_dir, `${case_id}.jsonl`);
|
|
978
|
+
const sinks = [];
|
|
979
|
+
if (trajectory_path !== void 0) sinks.push(filesystem_logger({ output_path: trajectory_path }));
|
|
980
|
+
if (options.live_url !== void 0) sinks.push(http_logger({ url: options.live_url }));
|
|
981
|
+
const cost_tracker = create_cost_tracker();
|
|
982
|
+
return {
|
|
983
|
+
trajectory_path,
|
|
984
|
+
case_logger: sinks.length === 0 ? cost_tracker.logger : tee_logger(...sinks, cost_tracker.logger),
|
|
985
|
+
cost_tracker
|
|
986
|
+
};
|
|
987
|
+
}
|
|
988
|
+
/**
|
|
989
|
+
* Assembles the judge input triple, including `meta` only when the case
|
|
990
|
+
* carries it so the optional field stays absent rather than `undefined`.
|
|
991
|
+
*/
|
|
992
|
+
function build_judge_input(bc, output) {
|
|
993
|
+
return bc.meta === void 0 ? {
|
|
994
|
+
input: bc.input,
|
|
995
|
+
output
|
|
996
|
+
} : {
|
|
997
|
+
input: bc.input,
|
|
998
|
+
output,
|
|
999
|
+
meta: bc.meta
|
|
1000
|
+
};
|
|
1001
|
+
}
|
|
1002
|
+
/**
|
|
1003
|
+
* Runs each judge as a Step over the case's input/output, collecting the
|
|
1004
|
+
* normalized scores. A judge that throws a control-flow signal rethrows via
|
|
1005
|
+
* `rethrow_control_flow`; any other throw, or an abstaining `undefined`,
|
|
1006
|
+
* omits that judge from the scores rather than failing the case.
|
|
1007
|
+
*/
|
|
1008
|
+
async function run_judges(judge_entries, judge_input, ctx, case_id) {
|
|
1009
|
+
const scores = {};
|
|
1010
|
+
for (const [name, judge] of judge_entries) {
|
|
1011
|
+
let raw;
|
|
1012
|
+
try {
|
|
1013
|
+
raw = await run(judge, judge_input, {
|
|
1014
|
+
trajectory: ctx.case_logger,
|
|
1015
|
+
install_signal_handlers: ctx.install_signal_handlers,
|
|
1016
|
+
...ctx.run_options
|
|
1017
|
+
});
|
|
1018
|
+
} catch (err) {
|
|
1019
|
+
rethrow_control_flow(err, case_id);
|
|
1020
|
+
continue;
|
|
1021
|
+
}
|
|
1022
|
+
const normalized = normalize_score(raw);
|
|
1023
|
+
if (normalized === void 0) continue;
|
|
1024
|
+
scores[name] = normalized;
|
|
1025
|
+
}
|
|
1026
|
+
return scores;
|
|
1027
|
+
}
|
|
1028
|
+
/**
|
|
900
1029
|
* Rethrows the control-flow signals that must never become a `CaseResult`.
|
|
901
1030
|
*
|
|
902
1031
|
* An `aborted_error` cancels the whole bench: a report claiming every
|
|
@@ -1100,13 +1229,25 @@ function is_record$1(v) {
|
|
|
1100
1229
|
*/
|
|
1101
1230
|
function deep_equal(a, b) {
|
|
1102
1231
|
if (a === b) return true;
|
|
1103
|
-
if (Array.isArray(a) || Array.isArray(b))
|
|
1104
|
-
if (!Array.isArray(a) || !Array.isArray(b)) return false;
|
|
1105
|
-
if (a.length !== b.length) return false;
|
|
1106
|
-
for (let i = 0; i < a.length; i += 1) if (!deep_equal(a[i], b[i])) return false;
|
|
1107
|
-
return true;
|
|
1108
|
-
}
|
|
1232
|
+
if (Array.isArray(a) || Array.isArray(b)) return arrays_equal(a, b);
|
|
1109
1233
|
if (!is_record$1(a) || !is_record$1(b)) return false;
|
|
1234
|
+
return records_equal(a, b);
|
|
1235
|
+
}
|
|
1236
|
+
/**
|
|
1237
|
+
* Element-wise deep equality once at least one side is an array: both must be
|
|
1238
|
+
* arrays of the same length with every index equal.
|
|
1239
|
+
*/
|
|
1240
|
+
function arrays_equal(a, b) {
|
|
1241
|
+
if (!Array.isArray(a) || !Array.isArray(b)) return false;
|
|
1242
|
+
if (a.length !== b.length) return false;
|
|
1243
|
+
for (let i = 0; i < a.length; i += 1) if (!deep_equal(a[i], b[i])) return false;
|
|
1244
|
+
return true;
|
|
1245
|
+
}
|
|
1246
|
+
/**
|
|
1247
|
+
* Key-by-key deep equality over two records: same sorted key sets, and each
|
|
1248
|
+
* value deep-equal under the shared key.
|
|
1249
|
+
*/
|
|
1250
|
+
function records_equal(a, b) {
|
|
1110
1251
|
const ak = Object.keys(a).toSorted();
|
|
1111
1252
|
const bk = Object.keys(b).toSorted();
|
|
1112
1253
|
if (ak.length !== bk.length) return false;
|
|
@@ -1312,16 +1453,8 @@ function compute_per_case(current, baseline, score_threshold, cost_threshold) {
|
|
|
1312
1453
|
for (const id of all_ids) {
|
|
1313
1454
|
const c = by_id_current.get(id);
|
|
1314
1455
|
const b = by_id_baseline.get(id);
|
|
1315
|
-
const score_deltas =
|
|
1316
|
-
|
|
1317
|
-
const judge_names = /* @__PURE__ */ new Set([...Object.keys(c?.scores ?? {}), ...Object.keys(b?.scores ?? {})]);
|
|
1318
|
-
for (const name of judge_names) {
|
|
1319
|
-
const delta = score_value(c?.scores[name]) - score_value(b?.scores[name]);
|
|
1320
|
-
score_deltas[name] = delta;
|
|
1321
|
-
if (delta < -score_threshold) any_score_regression = true;
|
|
1322
|
-
}
|
|
1323
|
-
const cost_delta = (c?.cost_usd ?? 0) - (b?.cost_usd ?? 0);
|
|
1324
|
-
const cost_ratio_regression = b?.cost_usd !== void 0 && b.cost_usd > 0 ? cost_delta / b.cost_usd > cost_threshold : (c?.cost_usd ?? 0) > 0 && (b?.cost_usd ?? 0) === 0 && cost_threshold < Infinity;
|
|
1456
|
+
const { score_deltas, any_regression } = compute_score_deltas(c, b, score_threshold);
|
|
1457
|
+
const { cost_delta, is_regression: cost_regression } = compute_cost_regression(c, b, cost_threshold);
|
|
1325
1458
|
const ok_regression = b?.ok === true && c?.ok !== true;
|
|
1326
1459
|
out.push({
|
|
1327
1460
|
case_id: id,
|
|
@@ -1329,12 +1462,46 @@ function compute_per_case(current, baseline, score_threshold, cost_threshold) {
|
|
|
1329
1462
|
...c !== void 0 ? { current: c } : {},
|
|
1330
1463
|
score_deltas,
|
|
1331
1464
|
cost_delta,
|
|
1332
|
-
is_regression:
|
|
1465
|
+
is_regression: any_regression || cost_regression || ok_regression
|
|
1333
1466
|
});
|
|
1334
1467
|
}
|
|
1335
1468
|
return out;
|
|
1336
1469
|
}
|
|
1337
1470
|
/**
|
|
1471
|
+
* Compute per-judge score deltas for one case pair across the union of judge
|
|
1472
|
+
* names on both sides (a missing side scores 0), flagging a regression when
|
|
1473
|
+
* any judge's score drops beyond the threshold.
|
|
1474
|
+
*/
|
|
1475
|
+
function compute_score_deltas(c, b, score_threshold) {
|
|
1476
|
+
const score_deltas = {};
|
|
1477
|
+
let any_regression = false;
|
|
1478
|
+
const judge_names = /* @__PURE__ */ new Set([...Object.keys(c?.scores ?? {}), ...Object.keys(b?.scores ?? {})]);
|
|
1479
|
+
for (const name of judge_names) {
|
|
1480
|
+
const delta = score_value(c?.scores[name]) - score_value(b?.scores[name]);
|
|
1481
|
+
score_deltas[name] = delta;
|
|
1482
|
+
if (delta < -score_threshold) any_regression = true;
|
|
1483
|
+
}
|
|
1484
|
+
return {
|
|
1485
|
+
score_deltas,
|
|
1486
|
+
any_regression
|
|
1487
|
+
};
|
|
1488
|
+
}
|
|
1489
|
+
/**
|
|
1490
|
+
* Compute the cost delta for one case pair and whether it regressed. With a
|
|
1491
|
+
* positive baseline cost, a relative rise beyond `cost_threshold` regresses;
|
|
1492
|
+
* with a zero baseline, any positive current cost regresses (unless the
|
|
1493
|
+
* threshold is Infinity). A missing side counts as 0.
|
|
1494
|
+
*/
|
|
1495
|
+
function compute_cost_regression(c, b, cost_threshold) {
|
|
1496
|
+
const baseline_cost = b?.cost_usd ?? 0;
|
|
1497
|
+
const current_cost = c?.cost_usd ?? 0;
|
|
1498
|
+
const cost_delta = current_cost - baseline_cost;
|
|
1499
|
+
return {
|
|
1500
|
+
cost_delta,
|
|
1501
|
+
is_regression: baseline_cost > 0 ? cost_delta / baseline_cost > cost_threshold : current_cost > 0 && baseline_cost === 0 && cost_threshold < Infinity
|
|
1502
|
+
};
|
|
1503
|
+
}
|
|
1504
|
+
/**
|
|
1338
1505
|
* Extract a finite numeric score from a raw score entry.
|
|
1339
1506
|
*
|
|
1340
1507
|
* Accepts a bare number or an object with a numeric `score` field; anything
|
|
@@ -1460,6 +1627,48 @@ function round6$1(value) {
|
|
|
1460
1627
|
return Math.round(value * 1e6) / 1e6;
|
|
1461
1628
|
}
|
|
1462
1629
|
/**
|
|
1630
|
+
* The breakdown for a turn whose model has no pricing entry.
|
|
1631
|
+
*
|
|
1632
|
+
* Free providers (ollama, lmstudio) still report an all-zero, estimated
|
|
1633
|
+
* breakdown; a priced provider missing a table entry returns undefined so the
|
|
1634
|
+
* caller can surface a `pricing_missing` event instead of a fake zero.
|
|
1635
|
+
*/
|
|
1636
|
+
function missing_pricing_breakdown(provider) {
|
|
1637
|
+
if (!FREE_PROVIDERS.has(provider)) return void 0;
|
|
1638
|
+
return {
|
|
1639
|
+
total_usd: 0,
|
|
1640
|
+
input_usd: 0,
|
|
1641
|
+
output_usd: 0,
|
|
1642
|
+
currency: "USD",
|
|
1643
|
+
is_estimate: true
|
|
1644
|
+
};
|
|
1645
|
+
}
|
|
1646
|
+
/**
|
|
1647
|
+
* Split reasoning tokens between a surfaced `reasoning_usd` field and the
|
|
1648
|
+
* output total.
|
|
1649
|
+
*
|
|
1650
|
+
* With a dedicated `reasoning_per_million` rate the reasoning cost is surfaced
|
|
1651
|
+
* separately and left out of output; without one it rolls into output at the
|
|
1652
|
+
* output rate. Absent reasoning tokens leave output untouched and surface
|
|
1653
|
+
* nothing.
|
|
1654
|
+
*/
|
|
1655
|
+
function apply_reasoning_cost(plain_output_usd_raw, reasoning, has_reasoning_rate, reasoning_usd_raw) {
|
|
1656
|
+
if (reasoning > 0) {
|
|
1657
|
+
if (has_reasoning_rate) return {
|
|
1658
|
+
output_usd_raw: plain_output_usd_raw,
|
|
1659
|
+
surface_reasoning_usd: round6$1(reasoning_usd_raw)
|
|
1660
|
+
};
|
|
1661
|
+
return {
|
|
1662
|
+
output_usd_raw: plain_output_usd_raw + reasoning_usd_raw,
|
|
1663
|
+
surface_reasoning_usd: void 0
|
|
1664
|
+
};
|
|
1665
|
+
}
|
|
1666
|
+
return {
|
|
1667
|
+
output_usd_raw: plain_output_usd_raw,
|
|
1668
|
+
surface_reasoning_usd: void 0
|
|
1669
|
+
};
|
|
1670
|
+
}
|
|
1671
|
+
/**
|
|
1463
1672
|
* Compute a CostBreakdown for a single turn's usage.
|
|
1464
1673
|
*
|
|
1465
1674
|
* Returns undefined when pricing is absent AND the provider is not free. Free
|
|
@@ -1471,50 +1680,28 @@ function round6$1(value) {
|
|
|
1471
1680
|
* input call.
|
|
1472
1681
|
*/
|
|
1473
1682
|
function compute_cost(usage, pricing, provider) {
|
|
1474
|
-
if (pricing === void 0)
|
|
1475
|
-
if (!FREE_PROVIDERS.has(provider)) return void 0;
|
|
1476
|
-
return {
|
|
1477
|
-
total_usd: 0,
|
|
1478
|
-
input_usd: 0,
|
|
1479
|
-
output_usd: 0,
|
|
1480
|
-
currency: "USD",
|
|
1481
|
-
is_estimate: true
|
|
1482
|
-
};
|
|
1483
|
-
}
|
|
1484
|
-
const input_tokens = usage.input_tokens;
|
|
1485
|
-
const output_tokens = usage.output_tokens;
|
|
1683
|
+
if (pricing === void 0) return missing_pricing_breakdown(provider);
|
|
1486
1684
|
const cached = usage.cached_input_tokens ?? 0;
|
|
1487
1685
|
const cache_write = usage.cache_write_tokens ?? 0;
|
|
1488
1686
|
const reasoning = usage.reasoning_tokens ?? 0;
|
|
1489
|
-
const fresh_input = Math.max(0, input_tokens - cached - cache_write);
|
|
1687
|
+
const fresh_input = Math.max(0, usage.input_tokens - cached - cache_write);
|
|
1490
1688
|
const cached_rate = pricing.cached_input_per_million ?? pricing.input_per_million;
|
|
1491
1689
|
const cache_write_rate = pricing.cache_write_per_million ?? pricing.input_per_million;
|
|
1492
|
-
const has_reasoning_rate = pricing.reasoning_per_million !== void 0;
|
|
1493
1690
|
const reasoning_rate = pricing.reasoning_per_million ?? pricing.output_per_million;
|
|
1494
1691
|
const input_usd_raw = fresh_input * pricing.input_per_million / 1e6;
|
|
1495
1692
|
const cached_usd_raw = cached * cached_rate / 1e6;
|
|
1496
1693
|
const cache_write_usd_raw = cache_write * cache_write_rate / 1e6;
|
|
1497
1694
|
const reasoning_usd_raw = reasoning * reasoning_rate / 1e6;
|
|
1498
|
-
const
|
|
1499
|
-
const input_usd = round6$1(input_usd_raw);
|
|
1500
|
-
let output_usd_raw = plain_output_usd_raw;
|
|
1501
|
-
let surface_reasoning_usd;
|
|
1502
|
-
if (reasoning > 0) if (has_reasoning_rate) surface_reasoning_usd = round6$1(reasoning_usd_raw);
|
|
1503
|
-
else output_usd_raw = plain_output_usd_raw + reasoning_usd_raw;
|
|
1504
|
-
const output_usd = round6$1(output_usd_raw);
|
|
1505
|
-
let cached_input_usd;
|
|
1506
|
-
let cache_write_usd;
|
|
1507
|
-
if (cached > 0) cached_input_usd = round6$1(cached_usd_raw);
|
|
1508
|
-
if (cache_write > 0) cache_write_usd = round6$1(cache_write_usd_raw);
|
|
1695
|
+
const { output_usd_raw, surface_reasoning_usd } = apply_reasoning_cost(Math.max(0, usage.output_tokens - reasoning) * pricing.output_per_million / 1e6, reasoning, pricing.reasoning_per_million !== void 0, reasoning_usd_raw);
|
|
1509
1696
|
const breakdown = {
|
|
1510
1697
|
total_usd: round6$1(input_usd_raw + cached_usd_raw + cache_write_usd_raw + (surface_reasoning_usd !== void 0 ? reasoning_usd_raw : 0) + output_usd_raw),
|
|
1511
|
-
input_usd,
|
|
1512
|
-
output_usd,
|
|
1698
|
+
input_usd: round6$1(input_usd_raw),
|
|
1699
|
+
output_usd: round6$1(output_usd_raw),
|
|
1513
1700
|
currency: "USD",
|
|
1514
1701
|
is_estimate: true
|
|
1515
1702
|
};
|
|
1516
|
-
if (
|
|
1517
|
-
if (
|
|
1703
|
+
if (cached > 0) breakdown.cached_input_usd = round6$1(cached_usd_raw);
|
|
1704
|
+
if (cache_write > 0) breakdown.cache_write_usd = round6$1(cache_write_usd_raw);
|
|
1518
1705
|
if (surface_reasoning_usd !== void 0) breakdown.reasoning_usd = surface_reasoning_usd;
|
|
1519
1706
|
return breakdown;
|
|
1520
1707
|
}
|
|
@@ -1554,6 +1741,70 @@ function is_retryable(kind, policy) {
|
|
|
1554
1741
|
return policy.retry_on.includes(kind);
|
|
1555
1742
|
}
|
|
1556
1743
|
/**
|
|
1744
|
+
* Map an abort reason onto the error this layer rejects with, always wrapping
|
|
1745
|
+
* so an abort can only ever surface as `aborted_error` with the caller's
|
|
1746
|
+
* reason attached.
|
|
1747
|
+
*
|
|
1748
|
+
* The engine's convention, and the opposite of core's propagate-verbatim rule.
|
|
1749
|
+
* Two things depend on the wrap. A bare `controller.abort()` sets
|
|
1750
|
+
* `signal.reason` to a `DOMException`, which is an `Error` on Node, so
|
|
1751
|
+
* propagating would leak a non-fascicle error out of a boundary documented as
|
|
1752
|
+
* throwing `aborted_error`. And the engine's other abort sites (`generate`,
|
|
1753
|
+
* `tool_loop`, the adapters) wrap in order to attach `step_index` and
|
|
1754
|
+
* `tool_call_in_flight`, which only the construction site knows; propagating
|
|
1755
|
+
* would discard them. Wrapping a reason that is already an `aborted_error`
|
|
1756
|
+
* nests one inside the other, which is the accepted cost of that contract.
|
|
1757
|
+
*/
|
|
1758
|
+
function to_abort_error(reason) {
|
|
1759
|
+
return new aborted_error("aborted", { reason });
|
|
1760
|
+
}
|
|
1761
|
+
/**
|
|
1762
|
+
* Build the `rate_limit_error` thrown when 429 retries exhaust. Prefers the
|
|
1763
|
+
* failing attempt's own `Retry-After`, falling back to the most recent one a
|
|
1764
|
+
* prior attempt carried (`last_rate_limit_after`).
|
|
1765
|
+
*/
|
|
1766
|
+
function build_rate_limit_error(retryable, attempt, last_rate_limit_after) {
|
|
1767
|
+
const metadata = { attempts: attempt };
|
|
1768
|
+
if (retryable.retry_after_ms !== void 0) metadata.retry_after_ms = retryable.retry_after_ms;
|
|
1769
|
+
else if (last_rate_limit_after !== void 0) metadata.retry_after_ms = last_rate_limit_after;
|
|
1770
|
+
if (retryable.status !== void 0) metadata.status = retryable.status;
|
|
1771
|
+
return new rate_limit_error(retryable.message ?? `rate limited after ${attempt} attempts`, metadata);
|
|
1772
|
+
}
|
|
1773
|
+
/**
|
|
1774
|
+
* Build the `provider_error` thrown when 5xx/network/timeout retries exhaust.
|
|
1775
|
+
* Timeout collapses onto the `network` cause; only 5xx carries status/body.
|
|
1776
|
+
*/
|
|
1777
|
+
function build_provider_error(retryable, attempt) {
|
|
1778
|
+
const metadata = { cause_kind: retryable.kind === "provider_5xx" ? "provider_5xx" : "network" };
|
|
1779
|
+
if (retryable.kind === "provider_5xx") {
|
|
1780
|
+
if (retryable.status !== void 0) metadata.status = retryable.status;
|
|
1781
|
+
if (retryable.body !== void 0) metadata.body = retryable.body;
|
|
1782
|
+
}
|
|
1783
|
+
return new provider_error(retryable.message ?? `${retryable.kind} after ${attempt} attempts`, metadata);
|
|
1784
|
+
}
|
|
1785
|
+
/**
|
|
1786
|
+
* Map an exhausted retryable failure onto the error this layer rejects with:
|
|
1787
|
+
* `rate_limit_error` for 429s, `provider_error` for everything else.
|
|
1788
|
+
*/
|
|
1789
|
+
function build_exhaustion_error(retryable, attempt, last_rate_limit_after) {
|
|
1790
|
+
if (retryable.kind === "rate_limit") return build_rate_limit_error(retryable, attempt, last_rate_limit_after);
|
|
1791
|
+
return build_provider_error(retryable, attempt);
|
|
1792
|
+
}
|
|
1793
|
+
/**
|
|
1794
|
+
* Compute the pre-retry backoff for `attempt`. A rate-limit `Retry-After`
|
|
1795
|
+
* outranks the local cap (`Math.max`), so a server instruction longer than
|
|
1796
|
+
* `max_delay_ms` is honored; the honored value is returned so the caller can
|
|
1797
|
+
* carry it onto a later failure that omits its own.
|
|
1798
|
+
*/
|
|
1799
|
+
function next_backoff(retryable, backoff_policy, attempt) {
|
|
1800
|
+
const delay = compute_backoff(backoff_policy, attempt - 1);
|
|
1801
|
+
if (retryable.kind === "rate_limit" && retryable.retry_after_ms !== void 0) return {
|
|
1802
|
+
delay: Math.max(delay, retryable.retry_after_ms),
|
|
1803
|
+
retry_after_ms: retryable.retry_after_ms
|
|
1804
|
+
};
|
|
1805
|
+
return { delay };
|
|
1806
|
+
}
|
|
1807
|
+
/**
|
|
1557
1808
|
* Retry `fn` under `policy`. `fn` must throw a RetryableError-shaped object to
|
|
1558
1809
|
* trigger retry; any other thrown value short-circuits as a permanent failure.
|
|
1559
1810
|
*
|
|
@@ -1572,35 +1823,17 @@ async function retry_with_policy(fn, policy = DEFAULT_RETRY, abort) {
|
|
|
1572
1823
|
let attempt = 0;
|
|
1573
1824
|
let last_rate_limit_after;
|
|
1574
1825
|
while (true) {
|
|
1575
|
-
if (abort?.aborted === true) throw
|
|
1826
|
+
if (abort?.aborted === true) throw to_abort_error(abort.reason);
|
|
1576
1827
|
try {
|
|
1577
1828
|
return await fn(attempt);
|
|
1578
1829
|
} catch (err) {
|
|
1579
1830
|
const retryable = classify_retryable(err);
|
|
1580
|
-
if (retryable === void 0) throw err;
|
|
1581
|
-
if (!is_retryable(retryable.kind, policy)) throw err;
|
|
1831
|
+
if (retryable === void 0 || !is_retryable(retryable.kind, policy)) throw err;
|
|
1582
1832
|
attempt += 1;
|
|
1583
|
-
if (attempt >= policy.max_attempts)
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
else if (last_rate_limit_after !== void 0) metadata.retry_after_ms = last_rate_limit_after;
|
|
1588
|
-
if (retryable.status !== void 0) metadata.status = retryable.status;
|
|
1589
|
-
throw new rate_limit_error(retryable.message ?? `rate limited after ${attempt} attempts`, metadata);
|
|
1590
|
-
}
|
|
1591
|
-
const metadata = { cause_kind: retryable.kind === "provider_5xx" ? "provider_5xx" : "network" };
|
|
1592
|
-
if (retryable.kind === "provider_5xx") {
|
|
1593
|
-
if (retryable.status !== void 0) metadata.status = retryable.status;
|
|
1594
|
-
if (retryable.body !== void 0) metadata.body = retryable.body;
|
|
1595
|
-
}
|
|
1596
|
-
throw new provider_error(retryable.message ?? `${retryable.kind} after ${attempt} attempts`, metadata);
|
|
1597
|
-
}
|
|
1598
|
-
let delay = compute_backoff(backoff_policy, attempt - 1);
|
|
1599
|
-
if (retryable.kind === "rate_limit" && retryable.retry_after_ms !== void 0) {
|
|
1600
|
-
delay = Math.max(delay, retryable.retry_after_ms);
|
|
1601
|
-
last_rate_limit_after = retryable.retry_after_ms;
|
|
1602
|
-
}
|
|
1603
|
-
await wait_with_abort(delay, signal, (reason) => new aborted_error("aborted", { reason }));
|
|
1833
|
+
if (attempt >= policy.max_attempts) throw build_exhaustion_error(retryable, attempt, last_rate_limit_after);
|
|
1834
|
+
const backoff = next_backoff(retryable, backoff_policy, attempt);
|
|
1835
|
+
last_rate_limit_after = backoff.retry_after_ms ?? last_rate_limit_after;
|
|
1836
|
+
await wait_with_abort(backoff.delay, signal, to_abort_error);
|
|
1604
1837
|
}
|
|
1605
1838
|
}
|
|
1606
1839
|
}
|
|
@@ -1619,6 +1852,44 @@ function read_number$1(err, key) {
|
|
|
1619
1852
|
return typeof value === "number" ? value : void 0;
|
|
1620
1853
|
}
|
|
1621
1854
|
/**
|
|
1855
|
+
* Assemble the `rate_limit` retryable shape, copying the optional
|
|
1856
|
+
* `retry_after_ms`, `status`, and `message` fields when each is present.
|
|
1857
|
+
*/
|
|
1858
|
+
function build_retryable_rate_limit(err) {
|
|
1859
|
+
const base = { kind: "rate_limit" };
|
|
1860
|
+
const retry_after = read_number$1(err, "retry_after_ms");
|
|
1861
|
+
if (retry_after !== void 0) base.retry_after_ms = retry_after;
|
|
1862
|
+
const status = read_number$1(err, "status");
|
|
1863
|
+
if (status !== void 0) base.status = status;
|
|
1864
|
+
const message = read_string$1(err, "message");
|
|
1865
|
+
if (message !== void 0) base.message = message;
|
|
1866
|
+
return base;
|
|
1867
|
+
}
|
|
1868
|
+
/**
|
|
1869
|
+
* Assemble the `provider_5xx` retryable shape, copying the optional `status`,
|
|
1870
|
+
* `body`, and `message` fields when each is present.
|
|
1871
|
+
*/
|
|
1872
|
+
function build_retryable_5xx(err) {
|
|
1873
|
+
const base = { kind: "provider_5xx" };
|
|
1874
|
+
const status = read_number$1(err, "status");
|
|
1875
|
+
if (status !== void 0) base.status = status;
|
|
1876
|
+
const body = read_string$1(err, "body");
|
|
1877
|
+
if (body !== void 0) base.body = body;
|
|
1878
|
+
const message = read_string$1(err, "message");
|
|
1879
|
+
if (message !== void 0) base.message = message;
|
|
1880
|
+
return base;
|
|
1881
|
+
}
|
|
1882
|
+
/**
|
|
1883
|
+
* Assemble a `network` or `timeout` retryable shape, copying the optional
|
|
1884
|
+
* `message` when present.
|
|
1885
|
+
*/
|
|
1886
|
+
function build_retryable_signal(err, kind) {
|
|
1887
|
+
const base = { kind };
|
|
1888
|
+
const message = read_string$1(err, "message");
|
|
1889
|
+
if (message !== void 0) base.message = message;
|
|
1890
|
+
return base;
|
|
1891
|
+
}
|
|
1892
|
+
/**
|
|
1622
1893
|
* Classify a thrown value into a `RetryableError`, or `undefined` if it
|
|
1623
1894
|
* does not carry one of the recognized retryable `kind`s.
|
|
1624
1895
|
*
|
|
@@ -1629,64 +1900,147 @@ function read_number$1(err, key) {
|
|
|
1629
1900
|
function classify_retryable(err) {
|
|
1630
1901
|
if (err === null || typeof err !== "object") return void 0;
|
|
1631
1902
|
const kind = read_string$1(err, "kind");
|
|
1632
|
-
if (kind === "rate_limit")
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1903
|
+
if (kind === "rate_limit") return build_retryable_rate_limit(err);
|
|
1904
|
+
if (kind === "provider_5xx") return build_retryable_5xx(err);
|
|
1905
|
+
if (kind === "network" || kind === "timeout") return build_retryable_signal(err, kind);
|
|
1906
|
+
}
|
|
1907
|
+
//#endregion
|
|
1908
|
+
//#region src/engine/providers/native_shared.ts
|
|
1909
|
+
/**
|
|
1910
|
+
* The message under a JSON error envelope's `error` key, or undefined when
|
|
1911
|
+
* the shape carries none worth surfacing.
|
|
1912
|
+
*/
|
|
1913
|
+
function error_field_message(parsed) {
|
|
1914
|
+
if (parsed === null || typeof parsed !== "object") return void 0;
|
|
1915
|
+
const error = Reflect.get(parsed, "error");
|
|
1916
|
+
if (typeof error === "string" && error.length > 0) return error;
|
|
1917
|
+
if (error === null || typeof error !== "object") return void 0;
|
|
1918
|
+
const message = Reflect.get(error, "message");
|
|
1919
|
+
return typeof message === "string" && message.length > 0 ? message : void 0;
|
|
1920
|
+
}
|
|
1921
|
+
/**
|
|
1922
|
+
* Pull a human-readable message out of a non-2xx body. The native wires share
|
|
1923
|
+
* one envelope family: Anthropic sends `{ type: 'error', error: { message } }`,
|
|
1924
|
+
* OpenAI-style wires send `{ error: { message } }`, and Ollama sends
|
|
1925
|
+
* `{ error: "message" }` (with the nested shape appearing behind proxies), so
|
|
1926
|
+
* one parser accepts a string or a `{ message }` object under `error` and
|
|
1927
|
+
* falls back to the raw body, truncated to 300 chars.
|
|
1928
|
+
*/
|
|
1929
|
+
function extract_error_message(body) {
|
|
1930
|
+
if (body.length === 0) return "(empty body)";
|
|
1931
|
+
let message;
|
|
1932
|
+
try {
|
|
1933
|
+
message = error_field_message(JSON.parse(body));
|
|
1934
|
+
} catch {}
|
|
1935
|
+
if (message !== void 0) return message;
|
|
1936
|
+
return body.length > 300 ? `${body.slice(0, 300)}...` : body;
|
|
1937
|
+
}
|
|
1938
|
+
/**
|
|
1939
|
+
* Map a non-2xx response to what the engine's retry stack expects: 401
|
|
1940
|
+
* becomes a typed provider_auth_error, 429/5xx keep `status` +
|
|
1941
|
+
* `responseHeaders` so classify_provider_error marks them retryable
|
|
1942
|
+
* (rate_limit honoring retry-after, provider_5xx), and any other 4xx is a
|
|
1943
|
+
* permanent provider_error that surfaces as-is.
|
|
1944
|
+
*/
|
|
1945
|
+
async function response_error(response, provider) {
|
|
1946
|
+
let body = "";
|
|
1947
|
+
try {
|
|
1948
|
+
body = await response.text();
|
|
1949
|
+
} catch {}
|
|
1950
|
+
const detail = extract_error_message(body);
|
|
1951
|
+
const status = response.status;
|
|
1952
|
+
if (status === 401) return new provider_auth_error(provider, `${provider} authentication failed (401): ${detail}`);
|
|
1953
|
+
if (status === 429 || status >= 500) {
|
|
1954
|
+
const retry_after = response.headers.get("retry-after");
|
|
1955
|
+
return Object.assign(/* @__PURE__ */ new Error(`${provider} API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
|
|
1641
1956
|
}
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1957
|
+
return new provider_error(`${provider} API error ${status}: ${detail}`, {
|
|
1958
|
+
status,
|
|
1959
|
+
body
|
|
1960
|
+
});
|
|
1961
|
+
}
|
|
1962
|
+
/**
|
|
1963
|
+
* A user abort surfaces as the fetch/reader AbortError; retry_turn converts
|
|
1964
|
+
* it via its own signal check, so it is rethrown untouched. Everything else
|
|
1965
|
+
* is a transport failure wrapped in the `kind: 'network'` shape the shared
|
|
1966
|
+
* classify_provider_error marks retryable.
|
|
1967
|
+
*/
|
|
1968
|
+
function rethrow_network_failure(err, abort, provider) {
|
|
1969
|
+
if (abort.aborted) throw err;
|
|
1970
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
1971
|
+
throw Object.assign(/* @__PURE__ */ new Error(`${provider} native: network failure: ${detail}`), { kind: "network" });
|
|
1972
|
+
}
|
|
1973
|
+
/**
|
|
1974
|
+
* Run one native turn over HTTP: POST the dialect's body, map a non-2xx to
|
|
1975
|
+
* the retry stack's shapes, and hand the response to the dialect's stream or
|
|
1976
|
+
* JSON consumer. `build_body` is called inside the network try/catch on
|
|
1977
|
+
* purpose: that is where each adapter historically built its body, so a
|
|
1978
|
+
* mapper throw keeps surfacing through the same path it always did.
|
|
1979
|
+
*/
|
|
1980
|
+
async function invoke_http_turn(req, provider, request, consume) {
|
|
1981
|
+
let response;
|
|
1982
|
+
try {
|
|
1983
|
+
response = await fetch(request.url, {
|
|
1984
|
+
method: "POST",
|
|
1985
|
+
headers: request.headers,
|
|
1986
|
+
body: JSON.stringify(request.build_body()),
|
|
1987
|
+
signal: req.abort
|
|
1988
|
+
});
|
|
1989
|
+
} catch (err) {
|
|
1990
|
+
rethrow_network_failure(err, req.abort, provider);
|
|
1651
1991
|
}
|
|
1652
|
-
if (
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1992
|
+
if (!response.ok) throw await response_error(response, provider);
|
|
1993
|
+
if (req.stream) return consume.stream(response);
|
|
1994
|
+
const payload = await response.json();
|
|
1995
|
+
return consume.parse(payload);
|
|
1996
|
+
}
|
|
1997
|
+
/**
|
|
1998
|
+
* Drain a streaming response: decode bytes, reassemble framed payloads (SSE
|
|
1999
|
+
* events or NDJSON lines), and feed them to the dialect's aggregator, which
|
|
2000
|
+
* dispatches chunks through req.dispatch_chunk as they arrive. Transport
|
|
2001
|
+
* failures mid-read wrap as network errors; aggregator throws (malformed
|
|
2002
|
+
* frames, mid-stream error events, a rejecting on_chunk) pass through
|
|
2003
|
+
* untouched, with the reader cancelled so the connection is released.
|
|
2004
|
+
*/
|
|
2005
|
+
async function consume_framed_stream(response, req, provider, framing, create_aggregator) {
|
|
2006
|
+
const body = response.body;
|
|
2007
|
+
if (body === null) throw new provider_error(`${provider} native: streaming response has no body`);
|
|
2008
|
+
const aggregator = create_aggregator(req.dispatch_chunk ?? (async () => {}));
|
|
2009
|
+
const reader = body.getReader();
|
|
2010
|
+
const text_decoder = new TextDecoder();
|
|
2011
|
+
const next_bytes = async () => {
|
|
2012
|
+
let step;
|
|
2013
|
+
try {
|
|
2014
|
+
step = await reader.read();
|
|
2015
|
+
} catch (err) {
|
|
2016
|
+
rethrow_network_failure(err, req.abort, provider);
|
|
2017
|
+
}
|
|
2018
|
+
return step.done ? void 0 : step.value;
|
|
2019
|
+
};
|
|
2020
|
+
try {
|
|
2021
|
+
while (true) {
|
|
2022
|
+
const bytes = await next_bytes();
|
|
2023
|
+
if (bytes === void 0) break;
|
|
2024
|
+
for (const data of framing.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle(data);
|
|
2025
|
+
}
|
|
2026
|
+
} finally {
|
|
2027
|
+
reader.cancel().catch(() => {});
|
|
1657
2028
|
}
|
|
2029
|
+
const tail = [...framing.push(text_decoder.decode()), ...framing.flush()];
|
|
2030
|
+
for (const data of tail) await aggregator.handle(data);
|
|
2031
|
+
return aggregator.complete();
|
|
1658
2032
|
}
|
|
1659
|
-
//#endregion
|
|
1660
|
-
//#region src/engine/providers/sse_native.ts
|
|
1661
2033
|
/**
|
|
1662
|
-
* Incremental
|
|
1663
|
-
*
|
|
1664
|
-
*
|
|
1665
|
-
*
|
|
1666
|
-
*
|
|
1667
|
-
* when the stream ends without a trailing blank line. Only `data:` fields
|
|
1668
|
-
* matter to the streams fascicle consumes (the Messages API repeats the
|
|
1669
|
-
* event type inside the JSON payload and the Chat Completions stream sends
|
|
1670
|
-
* nothing but data frames), so `event:`/`id:`/`retry:` fields and `:`
|
|
1671
|
-
* comments are dropped. Multi-line data joins with '\n' per the SSE spec.
|
|
2034
|
+
* Incremental newline framing shared by the SSE and NDJSON decoders: buffer
|
|
2035
|
+
* decoded text across arbitrary chunk boundaries, hand each completed line
|
|
2036
|
+
* (CR trimmed) to `handle_line`, and on flush hand over an unterminated tail
|
|
2037
|
+
* line, then `on_flush` for state a decoder holds across lines (an SSE event
|
|
2038
|
+
* never closed by a blank line).
|
|
1672
2039
|
*/
|
|
1673
|
-
function
|
|
2040
|
+
function create_line_splitter(handle_line, on_flush) {
|
|
1674
2041
|
let buffer = "";
|
|
1675
|
-
let data_lines = [];
|
|
1676
2042
|
const take_line = (raw, out) => {
|
|
1677
|
-
|
|
1678
|
-
if (line.length === 0) {
|
|
1679
|
-
if (data_lines.length > 0) {
|
|
1680
|
-
out.push(data_lines.join("\n"));
|
|
1681
|
-
data_lines = [];
|
|
1682
|
-
}
|
|
1683
|
-
return;
|
|
1684
|
-
}
|
|
1685
|
-
if (line.startsWith(":")) return;
|
|
1686
|
-
if (line.startsWith("data:")) {
|
|
1687
|
-
const value = line.slice(5);
|
|
1688
|
-
data_lines.push(value.startsWith(" ") ? value.slice(1) : value);
|
|
1689
|
-
}
|
|
2043
|
+
handle_line(raw.endsWith("\r") ? raw.slice(0, -1) : raw, out);
|
|
1690
2044
|
};
|
|
1691
2045
|
return {
|
|
1692
2046
|
push(text) {
|
|
@@ -1704,34 +2058,130 @@ function create_sse_decoder() {
|
|
|
1704
2058
|
const out = [];
|
|
1705
2059
|
if (buffer.length > 0) take_line(buffer, out);
|
|
1706
2060
|
buffer = "";
|
|
1707
|
-
|
|
1708
|
-
out.push(data_lines.join("\n"));
|
|
1709
|
-
data_lines = [];
|
|
1710
|
-
}
|
|
2061
|
+
on_flush?.(out);
|
|
1711
2062
|
return out;
|
|
1712
2063
|
}
|
|
1713
2064
|
};
|
|
1714
2065
|
}
|
|
1715
|
-
//#endregion
|
|
1716
|
-
//#region src/engine/providers/anthropic_native.ts
|
|
1717
2066
|
/**
|
|
1718
|
-
*
|
|
1719
|
-
*
|
|
1720
|
-
*
|
|
1721
|
-
*
|
|
1722
|
-
*
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
*
|
|
1734
|
-
*
|
|
2067
|
+
* Decode a tool call's wire `arguments` string into the engine's input value:
|
|
2068
|
+
* non-empty JSON parses (bad JSON is a provider_error naming the tool), and
|
|
2069
|
+
* an absent or empty value means `{}`, the shape some servers send for
|
|
2070
|
+
* no-argument calls. The Ollama wire's structured-object arguments are its
|
|
2071
|
+
* caller's business before this runs.
|
|
2072
|
+
*/
|
|
2073
|
+
function parse_tool_call_arguments(raw_args, provider, tool_name) {
|
|
2074
|
+
if (typeof raw_args === "string" && raw_args.length > 0) try {
|
|
2075
|
+
return JSON.parse(raw_args);
|
|
2076
|
+
} catch {
|
|
2077
|
+
throw new provider_error(`${provider} native: tool_calls arguments for ${tool_name} is not valid JSON`);
|
|
2078
|
+
}
|
|
2079
|
+
return {};
|
|
2080
|
+
}
|
|
2081
|
+
/**
|
|
2082
|
+
* Fold engine messages into a wire's message array by role. Serves the wires
|
|
2083
|
+
* whose messages map 1:1 in place (chat/completions, Ollama's /api/chat); the
|
|
2084
|
+
* Messages API's system-hoisting and role-merging rules live in its own
|
|
2085
|
+
* mapper instead.
|
|
2086
|
+
*/
|
|
2087
|
+
function map_messages_by_role(messages, wire) {
|
|
2088
|
+
const out = [];
|
|
2089
|
+
for (const message of messages) switch (message.role) {
|
|
2090
|
+
case "system":
|
|
2091
|
+
out.push(wire.system(message.content));
|
|
2092
|
+
break;
|
|
2093
|
+
case "user":
|
|
2094
|
+
out.push(wire.user(message.content));
|
|
2095
|
+
break;
|
|
2096
|
+
case "assistant":
|
|
2097
|
+
out.push(wire.assistant(message.content));
|
|
2098
|
+
break;
|
|
2099
|
+
case "tool":
|
|
2100
|
+
out.push(wire.tool(message));
|
|
2101
|
+
break;
|
|
2102
|
+
}
|
|
2103
|
+
return out;
|
|
2104
|
+
}
|
|
2105
|
+
/**
|
|
2106
|
+
* Partition assistant content parts into the text runs and tool calls the
|
|
2107
|
+
* wire mappers serialize separately.
|
|
2108
|
+
*/
|
|
2109
|
+
function split_assistant_parts(parts) {
|
|
2110
|
+
const texts = [];
|
|
2111
|
+
const tool_parts = [];
|
|
2112
|
+
for (const part of parts) if (part.type === "text") texts.push(part.text);
|
|
2113
|
+
else tool_parts.push(part);
|
|
2114
|
+
return {
|
|
2115
|
+
texts,
|
|
2116
|
+
tool_parts
|
|
2117
|
+
};
|
|
2118
|
+
}
|
|
2119
|
+
/**
|
|
2120
|
+
* Read the api_key and base_url every OpenAI-compatible dialect starts from:
|
|
2121
|
+
* a missing or non-string api_key becomes '' (the core's empty-key guard owns
|
|
2122
|
+
* the throw), and an absent base_url falls back to the dialect's default.
|
|
2123
|
+
*/
|
|
2124
|
+
function read_dialect_init(init, default_base_url) {
|
|
2125
|
+
return {
|
|
2126
|
+
api_key: typeof init.api_key === "string" ? init.api_key : "",
|
|
2127
|
+
base_url: typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : default_base_url
|
|
2128
|
+
};
|
|
2129
|
+
}
|
|
2130
|
+
//#endregion
|
|
2131
|
+
//#region src/engine/providers/sse_native.ts
|
|
2132
|
+
/**
|
|
2133
|
+
* Incremental SSE decoder shared by the native (raw-HTTP) provider adapters.
|
|
2134
|
+
*/
|
|
2135
|
+
/**
|
|
2136
|
+
* Wire-neutral framing only: push() takes decoded text as it arrives off the
|
|
2137
|
+
* wire (any chunk boundary, including mid-line) and returns the data payloads
|
|
2138
|
+
* of every event completed by that chunk; flush() drains an event left open
|
|
2139
|
+
* when the stream ends without a trailing blank line. Only `data:` fields
|
|
2140
|
+
* matter to the streams fascicle consumes (the Messages API repeats the
|
|
2141
|
+
* event type inside the JSON payload and the Chat Completions stream sends
|
|
2142
|
+
* nothing but data frames), so `event:`/`id:`/`retry:` fields and `:`
|
|
2143
|
+
* comments are dropped. Multi-line data joins with '\n' per the SSE spec.
|
|
2144
|
+
*/
|
|
2145
|
+
function create_sse_decoder() {
|
|
2146
|
+
let data_lines = [];
|
|
2147
|
+
const close_event = (out) => {
|
|
2148
|
+
if (data_lines.length > 0) {
|
|
2149
|
+
out.push(data_lines.join("\n"));
|
|
2150
|
+
data_lines = [];
|
|
2151
|
+
}
|
|
2152
|
+
};
|
|
2153
|
+
return create_line_splitter((line, out) => {
|
|
2154
|
+
if (line.length === 0) {
|
|
2155
|
+
close_event(out);
|
|
2156
|
+
return;
|
|
2157
|
+
}
|
|
2158
|
+
if (line.startsWith(":")) return;
|
|
2159
|
+
if (line.startsWith("data:")) {
|
|
2160
|
+
const value = line.slice(5);
|
|
2161
|
+
data_lines.push(value.startsWith(" ") ? value.slice(1) : value);
|
|
2162
|
+
}
|
|
2163
|
+
}, close_event);
|
|
2164
|
+
}
|
|
2165
|
+
//#endregion
|
|
2166
|
+
//#region src/engine/providers/anthropic_native.ts
|
|
2167
|
+
/**
|
|
2168
|
+
* Native Anthropic provider adapter (raw HTTP).
|
|
2169
|
+
*
|
|
2170
|
+
* Talks to the Messages API directly over global fetch with zero `ai` /
|
|
2171
|
+
* `@ai-sdk/*` in its module graph. It owns request/response mapping only:
|
|
2172
|
+
* generate.ts wraps invoke_turn in retry + classification + abort, so
|
|
2173
|
+
* failures are thrown in shapes the shared classify_provider_error already
|
|
2174
|
+
* understands (`status` + `responseHeaders` for HTTP transients,
|
|
2175
|
+
* `kind: 'network'` for transport failures) instead of being re-classified
|
|
2176
|
+
* or retried here.
|
|
2177
|
+
*
|
|
2178
|
+
* Selected via `transport: 'native'` on the anthropic provider init. The
|
|
2179
|
+
* adapter name stays 'anthropic' so DEFAULT_PRICING keys and UsageTotals
|
|
2180
|
+
* fields keep working across transports. Schema requests ride the engine's
|
|
2181
|
+
* prompt + parse + repair loop, so `TurnRequest.schema` is intentionally
|
|
2182
|
+
* unread. Streaming hand-rolls the SSE parse; the aggregator rebuilds the
|
|
2183
|
+
* non-stream payload shape and feeds it through the same
|
|
2184
|
+
* parse_messages_response, so streamed and non-streamed results are equal by
|
|
1735
2185
|
* construction rather than by parallel code paths.
|
|
1736
2186
|
*/
|
|
1737
2187
|
/**
|
|
@@ -1762,22 +2212,26 @@ const DEFAULT_BASE_URL$2 = "https://api.anthropic.com/v1";
|
|
|
1762
2212
|
*/
|
|
1763
2213
|
const DEFAULT_MAX_TOKENS = 4096;
|
|
1764
2214
|
/**
|
|
2215
|
+
* Wrap non-blank text in a single text block; blank text maps to no blocks,
|
|
2216
|
+
* since the API rejects empty text blocks.
|
|
2217
|
+
*/
|
|
2218
|
+
function text_blocks(text) {
|
|
2219
|
+
return text.trim().length > 0 ? [{
|
|
2220
|
+
type: "text",
|
|
2221
|
+
text
|
|
2222
|
+
}] : [];
|
|
2223
|
+
}
|
|
2224
|
+
/**
|
|
1765
2225
|
* Map user message content to Messages-API text blocks, dropping empty text
|
|
1766
2226
|
* (the API rejects empty blocks). Image parts throw a capability error: they
|
|
1767
2227
|
* are not mapped on this transport.
|
|
1768
2228
|
*/
|
|
1769
2229
|
function to_user_blocks(content) {
|
|
1770
|
-
if (typeof content === "string") return content
|
|
1771
|
-
type: "text",
|
|
1772
|
-
text: content
|
|
1773
|
-
}] : [];
|
|
2230
|
+
if (typeof content === "string") return text_blocks(content);
|
|
1774
2231
|
const blocks = [];
|
|
1775
2232
|
for (const part of content) {
|
|
1776
2233
|
if (part.type === "image") throw new provider_capability_error("anthropic", "image_input", "image parts are not mapped on the native transport; use transport: 'ai_sdk'");
|
|
1777
|
-
|
|
1778
|
-
type: "text",
|
|
1779
|
-
text: part.text
|
|
1780
|
-
});
|
|
2234
|
+
blocks.push(...text_blocks(part.text));
|
|
1781
2235
|
}
|
|
1782
2236
|
return blocks;
|
|
1783
2237
|
}
|
|
@@ -1786,17 +2240,10 @@ function to_user_blocks(content) {
|
|
|
1786
2240
|
* text blocks (empties dropped), tool-call parts become tool_use blocks.
|
|
1787
2241
|
*/
|
|
1788
2242
|
function to_assistant_blocks(content) {
|
|
1789
|
-
if (typeof content === "string") return content
|
|
1790
|
-
type: "text",
|
|
1791
|
-
text: content
|
|
1792
|
-
}] : [];
|
|
2243
|
+
if (typeof content === "string") return text_blocks(content);
|
|
1793
2244
|
const blocks = [];
|
|
1794
|
-
for (const part of content) if (part.type === "text")
|
|
1795
|
-
|
|
1796
|
-
type: "text",
|
|
1797
|
-
text: part.text
|
|
1798
|
-
});
|
|
1799
|
-
} else blocks.push({
|
|
2245
|
+
for (const part of content) if (part.type === "text") blocks.push(...text_blocks(part.text));
|
|
2246
|
+
else blocks.push({
|
|
1800
2247
|
type: "tool_use",
|
|
1801
2248
|
id: part.id,
|
|
1802
2249
|
name: part.name,
|
|
@@ -1933,32 +2380,63 @@ function map_anthropic_usage(raw) {
|
|
|
1933
2380
|
return totals;
|
|
1934
2381
|
}
|
|
1935
2382
|
/**
|
|
2383
|
+
* Read the id and name a tool_use block must carry, throwing a
|
|
2384
|
+
* provider_error naming `where` the malformed block appeared.
|
|
2385
|
+
*/
|
|
2386
|
+
function read_tool_use_identity(block, where) {
|
|
2387
|
+
const id = Reflect.get(block, "id");
|
|
2388
|
+
const name = Reflect.get(block, "name");
|
|
2389
|
+
if (typeof id !== "string" || typeof name !== "string") throw new provider_error(`anthropic native: malformed tool_use block in ${where}`);
|
|
2390
|
+
return {
|
|
2391
|
+
id,
|
|
2392
|
+
name
|
|
2393
|
+
};
|
|
2394
|
+
}
|
|
2395
|
+
/**
|
|
2396
|
+
* Fold one response `content` block into the text/tool_call accumulators.
|
|
2397
|
+
* Text blocks append their string; tool_use blocks contribute a call whose id
|
|
2398
|
+
* and name are validated by read_tool_use_identity; thinking /
|
|
2399
|
+
* redacted_thinking blocks have no TurnResult field and are skipped.
|
|
2400
|
+
*/
|
|
2401
|
+
function push_content_block(block, text_parts, tool_calls) {
|
|
2402
|
+
if (block === null || typeof block !== "object") return;
|
|
2403
|
+
const type = Reflect.get(block, "type");
|
|
2404
|
+
if (type === "text") {
|
|
2405
|
+
const text = Reflect.get(block, "text");
|
|
2406
|
+
if (typeof text === "string") text_parts.push(text);
|
|
2407
|
+
return;
|
|
2408
|
+
}
|
|
2409
|
+
if (type === "tool_use") {
|
|
2410
|
+
const { id, name } = read_tool_use_identity(block, "response content");
|
|
2411
|
+
tool_calls.push({
|
|
2412
|
+
id,
|
|
2413
|
+
name,
|
|
2414
|
+
input: Reflect.get(block, "input")
|
|
2415
|
+
});
|
|
2416
|
+
}
|
|
2417
|
+
}
|
|
2418
|
+
/**
|
|
2419
|
+
* Walk a response payload's `content` array into the flat text and tool_call
|
|
2420
|
+
* lists a TurnResult carries. A non-array `content` yields empty lists,
|
|
2421
|
+
* matching a payload that carried no content blocks.
|
|
2422
|
+
*/
|
|
2423
|
+
function collect_content_blocks(content) {
|
|
2424
|
+
const text_parts = [];
|
|
2425
|
+
const tool_calls = [];
|
|
2426
|
+
if (Array.isArray(content)) for (const block of content) push_content_block(block, text_parts, tool_calls);
|
|
2427
|
+
return {
|
|
2428
|
+
text_parts,
|
|
2429
|
+
tool_calls
|
|
2430
|
+
};
|
|
2431
|
+
}
|
|
2432
|
+
/**
|
|
1936
2433
|
* Parse a non-stream Messages-API response payload into a TurnResult. The
|
|
1937
2434
|
* stream aggregator rebuilds this same payload shape and feeds it here too,
|
|
1938
2435
|
* so both paths share one parser.
|
|
1939
2436
|
*/
|
|
1940
2437
|
function parse_messages_response(payload) {
|
|
1941
2438
|
if (payload === null || typeof payload !== "object") throw new provider_error("anthropic native: response payload is not a JSON object");
|
|
1942
|
-
const
|
|
1943
|
-
const text_parts = [];
|
|
1944
|
-
const tool_calls = [];
|
|
1945
|
-
if (Array.isArray(content)) for (const block of content) {
|
|
1946
|
-
if (block === null || typeof block !== "object") continue;
|
|
1947
|
-
const type = Reflect.get(block, "type");
|
|
1948
|
-
if (type === "text") {
|
|
1949
|
-
const text = Reflect.get(block, "text");
|
|
1950
|
-
if (typeof text === "string") text_parts.push(text);
|
|
1951
|
-
} else if (type === "tool_use") {
|
|
1952
|
-
const id = Reflect.get(block, "id");
|
|
1953
|
-
const name = Reflect.get(block, "name");
|
|
1954
|
-
if (typeof id !== "string" || typeof name !== "string") throw new provider_error("anthropic native: malformed tool_use block in response content");
|
|
1955
|
-
tool_calls.push({
|
|
1956
|
-
id,
|
|
1957
|
-
name,
|
|
1958
|
-
input: Reflect.get(block, "input")
|
|
1959
|
-
});
|
|
1960
|
-
}
|
|
1961
|
-
}
|
|
2439
|
+
const { text_parts, tool_calls } = collect_content_blocks(Reflect.get(payload, "content"));
|
|
1962
2440
|
return {
|
|
1963
2441
|
text: text_parts.join(""),
|
|
1964
2442
|
tool_calls,
|
|
@@ -2039,9 +2517,7 @@ function create_stream_aggregator(step_index, dispatch) {
|
|
|
2039
2517
|
return;
|
|
2040
2518
|
}
|
|
2041
2519
|
if (type === "tool_use") {
|
|
2042
|
-
const id =
|
|
2043
|
-
const name = Reflect.get(block, "name");
|
|
2044
|
-
if (typeof id !== "string" || typeof name !== "string") throw new provider_error("anthropic native: malformed tool_use block in stream");
|
|
2520
|
+
const { id, name } = read_tool_use_identity(block, "stream");
|
|
2045
2521
|
const synthetic = {
|
|
2046
2522
|
type: "tool_use",
|
|
2047
2523
|
id,
|
|
@@ -2119,7 +2595,7 @@ function create_stream_aggregator(step_index, dispatch) {
|
|
|
2119
2595
|
});
|
|
2120
2596
|
};
|
|
2121
2597
|
return {
|
|
2122
|
-
async
|
|
2598
|
+
async handle(data) {
|
|
2123
2599
|
let event;
|
|
2124
2600
|
try {
|
|
2125
2601
|
event = JSON.parse(data);
|
|
@@ -2174,93 +2650,13 @@ function create_stream_aggregator(step_index, dispatch) {
|
|
|
2174
2650
|
}
|
|
2175
2651
|
};
|
|
2176
2652
|
}
|
|
2177
|
-
/** Anthropic error bodies are `{ type: 'error', error: { message } }`; fall back to the raw body. */
|
|
2178
|
-
function extract_error_message$2(body) {
|
|
2179
|
-
if (body.length === 0) return "(empty body)";
|
|
2180
|
-
try {
|
|
2181
|
-
const parsed = JSON.parse(body);
|
|
2182
|
-
if (parsed !== null && typeof parsed === "object") {
|
|
2183
|
-
const error = Reflect.get(parsed, "error");
|
|
2184
|
-
if (error !== null && typeof error === "object") {
|
|
2185
|
-
const message = Reflect.get(error, "message");
|
|
2186
|
-
if (typeof message === "string" && message.length > 0) return message;
|
|
2187
|
-
}
|
|
2188
|
-
}
|
|
2189
|
-
} catch {}
|
|
2190
|
-
return body.length > 300 ? `${body.slice(0, 300)}...` : body;
|
|
2191
|
-
}
|
|
2192
|
-
/**
|
|
2193
|
-
* Map a non-2xx response to what the engine's retry stack expects: 401
|
|
2194
|
-
* becomes a typed provider_auth_error, 429/5xx keep `status` +
|
|
2195
|
-
* `responseHeaders` so classify_provider_error marks them retryable
|
|
2196
|
-
* (rate_limit honoring retry-after, provider_5xx), and any other 4xx is a
|
|
2197
|
-
* permanent provider_error that surfaces as-is.
|
|
2198
|
-
*/
|
|
2199
|
-
async function response_error$2(response) {
|
|
2200
|
-
let body = "";
|
|
2201
|
-
try {
|
|
2202
|
-
body = await response.text();
|
|
2203
|
-
} catch {}
|
|
2204
|
-
const detail = extract_error_message$2(body);
|
|
2205
|
-
const status = response.status;
|
|
2206
|
-
if (status === 401) return new provider_auth_error("anthropic", `anthropic authentication failed (401): ${detail}`);
|
|
2207
|
-
if (status === 429 || status >= 500) {
|
|
2208
|
-
const retry_after = response.headers.get("retry-after");
|
|
2209
|
-
return Object.assign(/* @__PURE__ */ new Error(`anthropic API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
|
|
2210
|
-
}
|
|
2211
|
-
return new provider_error(`anthropic API error ${status}: ${detail}`, {
|
|
2212
|
-
status,
|
|
2213
|
-
body
|
|
2214
|
-
});
|
|
2215
|
-
}
|
|
2216
|
-
/**
|
|
2217
|
-
* A user abort surfaces as the fetch/reader AbortError; retry_turn converts
|
|
2218
|
-
* it via its own signal check, so it is rethrown untouched. Everything else
|
|
2219
|
-
* is a transport failure wrapped in the `kind: 'network'` shape the shared
|
|
2220
|
-
* classify_provider_error marks retryable.
|
|
2221
|
-
*/
|
|
2222
|
-
function rethrow_network_failure$2(err, abort) {
|
|
2223
|
-
if (abort.aborted) throw err;
|
|
2224
|
-
const detail = err instanceof Error ? err.message : String(err);
|
|
2225
|
-
throw Object.assign(/* @__PURE__ */ new Error(`anthropic native: network failure: ${detail}`), { kind: "network" });
|
|
2226
|
-
}
|
|
2227
2653
|
/**
|
|
2228
|
-
* Drain a streaming Messages response
|
|
2229
|
-
*
|
|
2230
|
-
* req.dispatch_chunk as they arrive.
|
|
2231
|
-
* network errors; aggregator throws (malformed events, mid-stream error
|
|
2232
|
-
* events, a rejecting on_chunk) pass through untouched, with the reader
|
|
2233
|
-
* cancelled so the connection is released.
|
|
2654
|
+
* Drain a streaming Messages response through the shared framed-stream
|
|
2655
|
+
* consumer: SSE framing feeding the aggregator, which dispatches chunks
|
|
2656
|
+
* through req.dispatch_chunk as they arrive.
|
|
2234
2657
|
*/
|
|
2235
2658
|
async function consume_sse_response$1(response, req) {
|
|
2236
|
-
|
|
2237
|
-
if (body === null) throw new provider_error("anthropic native: streaming response has no body");
|
|
2238
|
-
const dispatch = req.dispatch_chunk ?? (async () => {});
|
|
2239
|
-
const aggregator = create_stream_aggregator(req.step_index, dispatch);
|
|
2240
|
-
const reader = body.getReader();
|
|
2241
|
-
const text_decoder = new TextDecoder();
|
|
2242
|
-
const sse = create_sse_decoder();
|
|
2243
|
-
const next_bytes = async () => {
|
|
2244
|
-
let step;
|
|
2245
|
-
try {
|
|
2246
|
-
step = await reader.read();
|
|
2247
|
-
} catch (err) {
|
|
2248
|
-
rethrow_network_failure$2(err, req.abort);
|
|
2249
|
-
}
|
|
2250
|
-
return step.done ? void 0 : step.value;
|
|
2251
|
-
};
|
|
2252
|
-
try {
|
|
2253
|
-
while (true) {
|
|
2254
|
-
const bytes = await next_bytes();
|
|
2255
|
-
if (bytes === void 0) break;
|
|
2256
|
-
for (const data of sse.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle_event(data);
|
|
2257
|
-
}
|
|
2258
|
-
} finally {
|
|
2259
|
-
reader.cancel().catch(() => {});
|
|
2260
|
-
}
|
|
2261
|
-
const tail = [...sse.push(text_decoder.decode()), ...sse.flush()];
|
|
2262
|
-
for (const data of tail) await aggregator.handle_event(data);
|
|
2263
|
-
return aggregator.complete();
|
|
2659
|
+
return consume_framed_stream(response, req, "anthropic", create_sse_decoder(), (dispatch) => create_stream_aggregator(req.step_index, dispatch));
|
|
2264
2660
|
}
|
|
2265
2661
|
const SUPPORTED$10 = /* @__PURE__ */ new Set([
|
|
2266
2662
|
"text",
|
|
@@ -2282,24 +2678,18 @@ const create_anthropic_native_adapter = (init) => {
|
|
|
2282
2678
|
kind: "native",
|
|
2283
2679
|
name: "anthropic",
|
|
2284
2680
|
async invoke_turn(req) {
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
} catch (err) {
|
|
2298
|
-
rethrow_network_failure$2(err, req.abort);
|
|
2299
|
-
}
|
|
2300
|
-
if (!response.ok) throw await response_error$2(response);
|
|
2301
|
-
if (req.stream) return consume_sse_response$1(response, req);
|
|
2302
|
-
return parse_messages_response(await response.json());
|
|
2681
|
+
return invoke_http_turn(req, "anthropic", {
|
|
2682
|
+
url: `${base_url}/messages`,
|
|
2683
|
+
headers: {
|
|
2684
|
+
"content-type": "application/json",
|
|
2685
|
+
"x-api-key": api_key,
|
|
2686
|
+
"anthropic-version": ANTHROPIC_VERSION
|
|
2687
|
+
},
|
|
2688
|
+
build_body: () => build_messages_body(req)
|
|
2689
|
+
}, {
|
|
2690
|
+
stream: (response) => consume_sse_response$1(response, req),
|
|
2691
|
+
parse: parse_messages_response
|
|
2692
|
+
});
|
|
2303
2693
|
},
|
|
2304
2694
|
supports: (capability) => SUPPORTED$10.has(capability)
|
|
2305
2695
|
};
|
|
@@ -2309,12 +2699,13 @@ const create_anthropic_native_adapter = (init) => {
|
|
|
2309
2699
|
/**
|
|
2310
2700
|
* Map an effort level to Anthropic's extended-thinking provider options.
|
|
2311
2701
|
*
|
|
2312
|
-
* `none`
|
|
2313
|
-
* explicit "disabled" value, so non-reasoning models see a plain
|
|
2702
|
+
* A zero budget (which is what `none` maps to) sends no thinking config at all
|
|
2703
|
+
* rather than an explicit "disabled" value, so non-reasoning models see a plain
|
|
2704
|
+
* request.
|
|
2314
2705
|
*/
|
|
2315
2706
|
function translate_anthropic_effort(effort) {
|
|
2316
2707
|
const budget = ANTHROPIC_THINKING_BUDGETS[effort];
|
|
2317
|
-
if (
|
|
2708
|
+
if (budget === 0) return {
|
|
2318
2709
|
provider_options: {},
|
|
2319
2710
|
effort_ignored: false
|
|
2320
2711
|
};
|
|
@@ -2437,18 +2828,16 @@ function to_assistant_message$1(content) {
|
|
|
2437
2828
|
role: "assistant",
|
|
2438
2829
|
content
|
|
2439
2830
|
};
|
|
2440
|
-
const
|
|
2441
|
-
const tool_calls =
|
|
2442
|
-
for (const part of content) if (part.type === "text") text_parts.push(part.text);
|
|
2443
|
-
else tool_calls.push({
|
|
2831
|
+
const { texts, tool_parts } = split_assistant_parts(content);
|
|
2832
|
+
const tool_calls = tool_parts.map((part) => ({
|
|
2444
2833
|
id: part.id,
|
|
2445
2834
|
type: "function",
|
|
2446
2835
|
function: {
|
|
2447
2836
|
name: part.name,
|
|
2448
2837
|
arguments: JSON.stringify(part.input)
|
|
2449
2838
|
}
|
|
2450
|
-
});
|
|
2451
|
-
const text =
|
|
2839
|
+
}));
|
|
2840
|
+
const text = texts.join("");
|
|
2452
2841
|
const message = {
|
|
2453
2842
|
role: "assistant",
|
|
2454
2843
|
content: text.length > 0 || tool_calls.length === 0 ? text : null
|
|
@@ -2463,32 +2852,22 @@ function to_assistant_message$1(content) {
|
|
|
2463
2852
|
* `tool` role messages keyed by tool_call_id.
|
|
2464
2853
|
*/
|
|
2465
2854
|
function to_chat_messages(messages, provider) {
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
break;
|
|
2483
|
-
case "tool":
|
|
2484
|
-
out.push({
|
|
2485
|
-
role: "tool",
|
|
2486
|
-
tool_call_id: message.tool_call_id,
|
|
2487
|
-
content: message.content
|
|
2488
|
-
});
|
|
2489
|
-
break;
|
|
2490
|
-
}
|
|
2491
|
-
return out;
|
|
2855
|
+
return map_messages_by_role(messages, {
|
|
2856
|
+
system: (content) => ({
|
|
2857
|
+
role: "system",
|
|
2858
|
+
content
|
|
2859
|
+
}),
|
|
2860
|
+
user: (content) => ({
|
|
2861
|
+
role: "user",
|
|
2862
|
+
content: to_user_content$1(content, provider)
|
|
2863
|
+
}),
|
|
2864
|
+
assistant: to_assistant_message$1,
|
|
2865
|
+
tool: (message) => ({
|
|
2866
|
+
role: "tool",
|
|
2867
|
+
tool_call_id: message.tool_call_id,
|
|
2868
|
+
content: message.content
|
|
2869
|
+
})
|
|
2870
|
+
});
|
|
2492
2871
|
}
|
|
2493
2872
|
/**
|
|
2494
2873
|
* Map fascicle Tool[] to the Chat Completions function-tool shape,
|
|
@@ -2599,17 +2978,10 @@ function parse_tool_call(raw, provider) {
|
|
|
2599
2978
|
if (typeof id !== "string" || fn === null || typeof fn !== "object") throw new provider_error(`${provider} native: malformed tool_calls entry in response`);
|
|
2600
2979
|
const name = Reflect.get(fn, "name");
|
|
2601
2980
|
if (typeof name !== "string") throw new provider_error(`${provider} native: malformed tool_calls entry in response`);
|
|
2602
|
-
const raw_args = Reflect.get(fn, "arguments");
|
|
2603
|
-
let input = {};
|
|
2604
|
-
if (typeof raw_args === "string" && raw_args.length > 0) try {
|
|
2605
|
-
input = JSON.parse(raw_args);
|
|
2606
|
-
} catch {
|
|
2607
|
-
throw new provider_error(`${provider} native: tool_calls arguments for ${name} is not valid JSON`);
|
|
2608
|
-
}
|
|
2609
2981
|
return {
|
|
2610
2982
|
id,
|
|
2611
2983
|
name,
|
|
2612
|
-
input
|
|
2984
|
+
input: parse_tool_call_arguments(Reflect.get(fn, "arguments"), provider, name)
|
|
2613
2985
|
};
|
|
2614
2986
|
}
|
|
2615
2987
|
/**
|
|
@@ -2788,101 +3160,29 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
|
|
|
2788
3160
|
}
|
|
2789
3161
|
};
|
|
2790
3162
|
}
|
|
2791
|
-
/** OpenAI-style error bodies are `{ error: { message } }`; fall back to the raw body. */
|
|
2792
|
-
function extract_error_message$1(body) {
|
|
2793
|
-
if (body.length === 0) return "(empty body)";
|
|
2794
|
-
try {
|
|
2795
|
-
const parsed = JSON.parse(body);
|
|
2796
|
-
if (parsed !== null && typeof parsed === "object") {
|
|
2797
|
-
const error = Reflect.get(parsed, "error");
|
|
2798
|
-
if (error !== null && typeof error === "object") {
|
|
2799
|
-
const message = Reflect.get(error, "message");
|
|
2800
|
-
if (typeof message === "string" && message.length > 0) return message;
|
|
2801
|
-
}
|
|
2802
|
-
}
|
|
2803
|
-
} catch {}
|
|
2804
|
-
return body.length > 300 ? `${body.slice(0, 300)}...` : body;
|
|
2805
|
-
}
|
|
2806
3163
|
/**
|
|
2807
|
-
*
|
|
2808
|
-
*
|
|
2809
|
-
*
|
|
2810
|
-
*
|
|
2811
|
-
*
|
|
3164
|
+
* Drain a streaming chat/completions response through the shared
|
|
3165
|
+
* framed-stream consumer: SSE framing feeding the aggregator, which
|
|
3166
|
+
* dispatches chunks through req.dispatch_chunk as they arrive. The
|
|
3167
|
+
* aggregator's `handle_data` name is part of its exported API, so it is
|
|
3168
|
+
* adapted to the consumer's `handle` shape here rather than renamed.
|
|
2812
3169
|
*/
|
|
2813
|
-
async function
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
if (status === 401) return new provider_auth_error(provider, `${provider} authentication failed (401): ${detail}`);
|
|
2821
|
-
if (status === 429 || status >= 500) {
|
|
2822
|
-
const retry_after = response.headers.get("retry-after");
|
|
2823
|
-
return Object.assign(/* @__PURE__ */ new Error(`${provider} API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
|
|
2824
|
-
}
|
|
2825
|
-
return new provider_error(`${provider} API error ${status}: ${detail}`, {
|
|
2826
|
-
status,
|
|
2827
|
-
body
|
|
3170
|
+
async function consume_sse_response(response, req, dialect) {
|
|
3171
|
+
return consume_framed_stream(response, req, dialect.name, create_sse_decoder(), (dispatch) => {
|
|
3172
|
+
const aggregator = create_chat_stream_aggregator(dialect, req.step_index, dispatch);
|
|
3173
|
+
return {
|
|
3174
|
+
handle: aggregator.handle_data,
|
|
3175
|
+
complete: aggregator.complete
|
|
3176
|
+
};
|
|
2828
3177
|
});
|
|
2829
3178
|
}
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
if (abort.aborted) throw err;
|
|
2838
|
-
const detail = err instanceof Error ? err.message : String(err);
|
|
2839
|
-
throw Object.assign(/* @__PURE__ */ new Error(`${provider} native: network failure: ${detail}`), { kind: "network" });
|
|
2840
|
-
}
|
|
2841
|
-
/**
|
|
2842
|
-
* Drain a streaming chat/completions response: decode bytes, reassemble SSE
|
|
2843
|
-
* events, and feed each data payload to the aggregator, which dispatches
|
|
2844
|
-
* chunks through req.dispatch_chunk as they arrive. Transport failures
|
|
2845
|
-
* mid-read wrap as network errors; aggregator throws (malformed frames, a
|
|
2846
|
-
* rejecting on_chunk) pass through untouched, with the reader cancelled so
|
|
2847
|
-
* the connection is released.
|
|
2848
|
-
*/
|
|
2849
|
-
async function consume_sse_response(response, req, dialect) {
|
|
2850
|
-
const body = response.body;
|
|
2851
|
-
if (body === null) throw new provider_error(`${dialect.name} native: streaming response has no body`);
|
|
2852
|
-
const dispatch = req.dispatch_chunk ?? (async () => {});
|
|
2853
|
-
const aggregator = create_chat_stream_aggregator(dialect, req.step_index, dispatch);
|
|
2854
|
-
const reader = body.getReader();
|
|
2855
|
-
const text_decoder = new TextDecoder();
|
|
2856
|
-
const sse = create_sse_decoder();
|
|
2857
|
-
const next_bytes = async () => {
|
|
2858
|
-
let step;
|
|
2859
|
-
try {
|
|
2860
|
-
step = await reader.read();
|
|
2861
|
-
} catch (err) {
|
|
2862
|
-
rethrow_network_failure$1(err, req.abort, dialect.name);
|
|
2863
|
-
}
|
|
2864
|
-
return step.done ? void 0 : step.value;
|
|
2865
|
-
};
|
|
2866
|
-
try {
|
|
2867
|
-
while (true) {
|
|
2868
|
-
const bytes = await next_bytes();
|
|
2869
|
-
if (bytes === void 0) break;
|
|
2870
|
-
for (const data of sse.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle_data(data);
|
|
2871
|
-
}
|
|
2872
|
-
} finally {
|
|
2873
|
-
reader.cancel().catch(() => {});
|
|
2874
|
-
}
|
|
2875
|
-
const tail = [...sse.push(text_decoder.decode()), ...sse.flush()];
|
|
2876
|
-
for (const data of tail) await aggregator.handle_data(data);
|
|
2877
|
-
return aggregator.complete();
|
|
2878
|
-
}
|
|
2879
|
-
const SUPPORTED$8 = /* @__PURE__ */ new Set([
|
|
2880
|
-
"text",
|
|
2881
|
-
"tools",
|
|
2882
|
-
"schema",
|
|
2883
|
-
"streaming",
|
|
2884
|
-
"reasoning"
|
|
2885
|
-
]);
|
|
3179
|
+
const SUPPORTED$8 = /* @__PURE__ */ new Set([
|
|
3180
|
+
"text",
|
|
3181
|
+
"tools",
|
|
3182
|
+
"schema",
|
|
3183
|
+
"streaming",
|
|
3184
|
+
"reasoning"
|
|
3185
|
+
]);
|
|
2886
3186
|
/**
|
|
2887
3187
|
* Build a native OpenAI-compatible adapter from a dialect: validates the
|
|
2888
3188
|
* dialect's api_key when auth is `bearer`, normalizes base_url, and wires
|
|
@@ -2901,20 +3201,14 @@ const create_openai_compatible_adapter = (dialect) => {
|
|
|
2901
3201
|
kind: "native",
|
|
2902
3202
|
name: dialect.name,
|
|
2903
3203
|
async invoke_turn(req) {
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
} catch (err) {
|
|
2913
|
-
rethrow_network_failure$1(err, req.abort, dialect.name);
|
|
2914
|
-
}
|
|
2915
|
-
if (!response.ok) throw await response_error$1(response, dialect.name);
|
|
2916
|
-
if (req.stream) return consume_sse_response(response, req, dialect);
|
|
2917
|
-
return parse_chat_completion(await response.json(), dialect);
|
|
3204
|
+
return invoke_http_turn(req, dialect.name, {
|
|
3205
|
+
url: `${base_url}/chat/completions`,
|
|
3206
|
+
headers,
|
|
3207
|
+
build_body: () => build_chat_completions_body(req, dialect)
|
|
3208
|
+
}, {
|
|
3209
|
+
stream: (response) => consume_sse_response(response, req, dialect),
|
|
3210
|
+
parse: (payload) => parse_chat_completion(payload, dialect)
|
|
3211
|
+
});
|
|
2918
3212
|
},
|
|
2919
3213
|
supports: (capability) => SUPPORTED$8.has(capability)
|
|
2920
3214
|
};
|
|
@@ -2978,8 +3272,7 @@ const create_openai_adapter = (init) => {
|
|
|
2978
3272
|
* `openai provider requires a non-empty api_key` engine_config_error.
|
|
2979
3273
|
*/
|
|
2980
3274
|
const create_openai_native_adapter = (init) => {
|
|
2981
|
-
const
|
|
2982
|
-
const base_url = typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : DEFAULT_BASE_URL$1;
|
|
3275
|
+
const { api_key, base_url } = read_dialect_init(init, DEFAULT_BASE_URL$1);
|
|
2983
3276
|
const organization = typeof init["organization"] === "string" ? init["organization"] : void 0;
|
|
2984
3277
|
return create_openai_compatible_adapter({
|
|
2985
3278
|
name: "openai",
|
|
@@ -3103,18 +3396,15 @@ function to_assistant_message(content) {
|
|
|
3103
3396
|
role: "assistant",
|
|
3104
3397
|
content
|
|
3105
3398
|
};
|
|
3106
|
-
const
|
|
3107
|
-
const tool_calls = [];
|
|
3108
|
-
for (const part of content) if (part.type === "text") text_parts.push(part.text);
|
|
3109
|
-
else tool_calls.push({ function: {
|
|
3110
|
-
name: part.name,
|
|
3111
|
-
arguments: part.input
|
|
3112
|
-
} });
|
|
3399
|
+
const { texts, tool_parts } = split_assistant_parts(content);
|
|
3113
3400
|
const message = {
|
|
3114
3401
|
role: "assistant",
|
|
3115
|
-
content:
|
|
3402
|
+
content: texts.join("")
|
|
3116
3403
|
};
|
|
3117
|
-
if (
|
|
3404
|
+
if (tool_parts.length > 0) message.tool_calls = tool_parts.map((part) => ({ function: {
|
|
3405
|
+
name: part.name,
|
|
3406
|
+
arguments: part.input
|
|
3407
|
+
} }));
|
|
3118
3408
|
return message;
|
|
3119
3409
|
}
|
|
3120
3410
|
/**
|
|
@@ -3123,32 +3413,22 @@ function to_assistant_message(content) {
|
|
|
3123
3413
|
* `tool_name`, since this wire has no tool_call_id to round-trip.
|
|
3124
3414
|
*/
|
|
3125
3415
|
function to_ollama_messages(messages) {
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
break;
|
|
3143
|
-
case "tool":
|
|
3144
|
-
out.push({
|
|
3145
|
-
role: "tool",
|
|
3146
|
-
tool_name: message.name,
|
|
3147
|
-
content: message.content
|
|
3148
|
-
});
|
|
3149
|
-
break;
|
|
3150
|
-
}
|
|
3151
|
-
return out;
|
|
3416
|
+
return map_messages_by_role(messages, {
|
|
3417
|
+
system: (content) => ({
|
|
3418
|
+
role: "system",
|
|
3419
|
+
content
|
|
3420
|
+
}),
|
|
3421
|
+
user: (content) => ({
|
|
3422
|
+
role: "user",
|
|
3423
|
+
content: to_user_content(content)
|
|
3424
|
+
}),
|
|
3425
|
+
assistant: to_assistant_message,
|
|
3426
|
+
tool: (message) => ({
|
|
3427
|
+
role: "tool",
|
|
3428
|
+
tool_name: message.name,
|
|
3429
|
+
content: message.content
|
|
3430
|
+
})
|
|
3431
|
+
});
|
|
3152
3432
|
}
|
|
3153
3433
|
/**
|
|
3154
3434
|
* Build the /api/chat request body for one turn: mapped messages and tools,
|
|
@@ -3225,17 +3505,10 @@ function parse_ollama_tool_call(raw, ordinal, step_index) {
|
|
|
3225
3505
|
const wire_id = Reflect.get(raw, "id");
|
|
3226
3506
|
const id = typeof wire_id === "string" && wire_id.length > 0 ? wire_id : `ollama_call_${step_index}_${ordinal}`;
|
|
3227
3507
|
const raw_args = Reflect.get(fn, "arguments");
|
|
3228
|
-
let input = {};
|
|
3229
|
-
if (typeof raw_args === "string" && raw_args.length > 0) try {
|
|
3230
|
-
input = JSON.parse(raw_args);
|
|
3231
|
-
} catch {
|
|
3232
|
-
throw new provider_error(`ollama native: tool_calls arguments for ${name} is not valid JSON`);
|
|
3233
|
-
}
|
|
3234
|
-
else if (raw_args !== null && typeof raw_args === "object") input = raw_args;
|
|
3235
3508
|
return {
|
|
3236
3509
|
id,
|
|
3237
3510
|
name,
|
|
3238
|
-
input
|
|
3511
|
+
input: raw_args !== null && typeof raw_args === "object" ? raw_args : parse_tool_call_arguments(raw_args, "ollama", name)
|
|
3239
3512
|
};
|
|
3240
3513
|
}
|
|
3241
3514
|
/**
|
|
@@ -3258,40 +3531,15 @@ function parse_ollama_chat(payload, step_index) {
|
|
|
3258
3531
|
};
|
|
3259
3532
|
}
|
|
3260
3533
|
/**
|
|
3261
|
-
* Append one decoded line to `out`, trimming a trailing `\r` and dropping
|
|
3262
|
-
* blank lines; JSON parsing is the aggregator's job.
|
|
3263
|
-
*/
|
|
3264
|
-
function take_line(raw, out) {
|
|
3265
|
-
const line = raw.endsWith("\r") ? raw.slice(0, -1) : raw;
|
|
3266
|
-
if (line.trim().length > 0) out.push(line);
|
|
3267
|
-
}
|
|
3268
|
-
/**
|
|
3269
3534
|
* Incremental NDJSON framing: push() takes decoded text at any chunk
|
|
3270
|
-
* boundary (including mid-line) and returns the complete lines it finished
|
|
3271
|
-
*
|
|
3272
|
-
* trailing newline.
|
|
3535
|
+
* boundary (including mid-line) and returns the complete lines it finished
|
|
3536
|
+
* (blank lines dropped; JSON parsing is the aggregator's job); flush() drains
|
|
3537
|
+
* a final line left open when the stream ends without a trailing newline.
|
|
3273
3538
|
*/
|
|
3274
3539
|
function create_ndjson_decoder() {
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
buffer += text;
|
|
3279
|
-
const out = [];
|
|
3280
|
-
let newline = buffer.indexOf("\n");
|
|
3281
|
-
while (newline >= 0) {
|
|
3282
|
-
take_line(buffer.slice(0, newline), out);
|
|
3283
|
-
buffer = buffer.slice(newline + 1);
|
|
3284
|
-
newline = buffer.indexOf("\n");
|
|
3285
|
-
}
|
|
3286
|
-
return out;
|
|
3287
|
-
},
|
|
3288
|
-
flush() {
|
|
3289
|
-
const out = [];
|
|
3290
|
-
if (buffer.length > 0) take_line(buffer, out);
|
|
3291
|
-
buffer = "";
|
|
3292
|
-
return out;
|
|
3293
|
-
}
|
|
3294
|
-
};
|
|
3540
|
+
return create_line_splitter((line, out) => {
|
|
3541
|
+
if (line.trim().length > 0) out.push(line);
|
|
3542
|
+
});
|
|
3295
3543
|
}
|
|
3296
3544
|
/**
|
|
3297
3545
|
* Consume /api/chat NDJSON frames, dispatching StreamChunks as they arrive
|
|
@@ -3345,7 +3593,7 @@ function create_ollama_stream_aggregator(step_index, dispatch) {
|
|
|
3345
3593
|
}
|
|
3346
3594
|
};
|
|
3347
3595
|
return {
|
|
3348
|
-
async
|
|
3596
|
+
async handle(line) {
|
|
3349
3597
|
if (done) return;
|
|
3350
3598
|
let frame;
|
|
3351
3599
|
try {
|
|
@@ -3391,94 +3639,13 @@ function create_ollama_stream_aggregator(step_index, dispatch) {
|
|
|
3391
3639
|
}
|
|
3392
3640
|
};
|
|
3393
3641
|
}
|
|
3394
|
-
/** Ollama error bodies are `{ error: "message" }`; tolerate the nested OpenAI shape too. */
|
|
3395
|
-
function extract_error_message(body) {
|
|
3396
|
-
if (body.length === 0) return "(empty body)";
|
|
3397
|
-
try {
|
|
3398
|
-
const parsed = JSON.parse(body);
|
|
3399
|
-
if (parsed !== null && typeof parsed === "object") {
|
|
3400
|
-
const error = Reflect.get(parsed, "error");
|
|
3401
|
-
if (typeof error === "string" && error.length > 0) return error;
|
|
3402
|
-
if (error !== null && typeof error === "object") {
|
|
3403
|
-
const message = Reflect.get(error, "message");
|
|
3404
|
-
if (typeof message === "string" && message.length > 0) return message;
|
|
3405
|
-
}
|
|
3406
|
-
}
|
|
3407
|
-
} catch {}
|
|
3408
|
-
return body.length > 300 ? `${body.slice(0, 300)}...` : body;
|
|
3409
|
-
}
|
|
3410
|
-
/**
|
|
3411
|
-
* Map a non-2xx response to what the engine's retry stack expects: 401
|
|
3412
|
-
* becomes a typed provider_auth_error (rare locally, but base_url may point
|
|
3413
|
-
* at a fronted daemon), 429/5xx keep `status` + `responseHeaders` so
|
|
3414
|
-
* classify_provider_error marks them retryable, and any other 4xx (a missing
|
|
3415
|
-
* model, a malformed request) is a permanent provider_error.
|
|
3416
|
-
*/
|
|
3417
|
-
async function response_error(response) {
|
|
3418
|
-
let body = "";
|
|
3419
|
-
try {
|
|
3420
|
-
body = await response.text();
|
|
3421
|
-
} catch {}
|
|
3422
|
-
const detail = extract_error_message(body);
|
|
3423
|
-
const status = response.status;
|
|
3424
|
-
if (status === 401) return new provider_auth_error("ollama", `ollama authentication failed (401): ${detail}`);
|
|
3425
|
-
if (status === 429 || status >= 500) {
|
|
3426
|
-
const retry_after = response.headers.get("retry-after");
|
|
3427
|
-
return Object.assign(/* @__PURE__ */ new Error(`ollama API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
|
|
3428
|
-
}
|
|
3429
|
-
return new provider_error(`ollama API error ${status}: ${detail}`, {
|
|
3430
|
-
status,
|
|
3431
|
-
body
|
|
3432
|
-
});
|
|
3433
|
-
}
|
|
3434
|
-
/**
|
|
3435
|
-
* A user abort surfaces as the fetch AbortError; retry_turn converts it via
|
|
3436
|
-
* its own signal check, so it is rethrown untouched. Everything else is a
|
|
3437
|
-
* transport failure wrapped in the `kind: 'network'` shape the shared
|
|
3438
|
-
* classify_provider_error marks retryable.
|
|
3439
|
-
*/
|
|
3440
|
-
function rethrow_network_failure(err, abort) {
|
|
3441
|
-
if (abort.aborted) throw err;
|
|
3442
|
-
const detail = err instanceof Error ? err.message : String(err);
|
|
3443
|
-
throw Object.assign(/* @__PURE__ */ new Error(`ollama native: network failure: ${detail}`), { kind: "network" });
|
|
3444
|
-
}
|
|
3445
3642
|
/**
|
|
3446
|
-
* Drain a streaming /api/chat response
|
|
3447
|
-
*
|
|
3448
|
-
* req.dispatch_chunk as they arrive.
|
|
3449
|
-
* network errors; aggregator throws (malformed frames, a rejecting on_chunk)
|
|
3450
|
-
* pass through untouched, with the reader cancelled so the connection is
|
|
3451
|
-
* released.
|
|
3643
|
+
* Drain a streaming /api/chat response through the shared framed-stream
|
|
3644
|
+
* consumer: NDJSON framing feeding the aggregator, which dispatches chunks
|
|
3645
|
+
* through req.dispatch_chunk as they arrive.
|
|
3452
3646
|
*/
|
|
3453
3647
|
async function consume_ndjson_response(response, req) {
|
|
3454
|
-
|
|
3455
|
-
if (body === null) throw new provider_error("ollama native: streaming response has no body");
|
|
3456
|
-
const dispatch = req.dispatch_chunk ?? (async () => {});
|
|
3457
|
-
const aggregator = create_ollama_stream_aggregator(req.step_index, dispatch);
|
|
3458
|
-
const reader = body.getReader();
|
|
3459
|
-
const text_decoder = new TextDecoder();
|
|
3460
|
-
const ndjson = create_ndjson_decoder();
|
|
3461
|
-
const next_bytes = async () => {
|
|
3462
|
-
let step;
|
|
3463
|
-
try {
|
|
3464
|
-
step = await reader.read();
|
|
3465
|
-
} catch (err) {
|
|
3466
|
-
rethrow_network_failure(err, req.abort);
|
|
3467
|
-
}
|
|
3468
|
-
return step.done ? void 0 : step.value;
|
|
3469
|
-
};
|
|
3470
|
-
try {
|
|
3471
|
-
while (true) {
|
|
3472
|
-
const bytes = await next_bytes();
|
|
3473
|
-
if (bytes === void 0) break;
|
|
3474
|
-
for (const line of ndjson.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle_line(line);
|
|
3475
|
-
}
|
|
3476
|
-
} finally {
|
|
3477
|
-
reader.cancel().catch(() => {});
|
|
3478
|
-
}
|
|
3479
|
-
const tail = [...ndjson.push(text_decoder.decode()), ...ndjson.flush()];
|
|
3480
|
-
for (const line of tail) await aggregator.handle_line(line);
|
|
3481
|
-
return aggregator.complete();
|
|
3648
|
+
return consume_framed_stream(response, req, "ollama", create_ndjson_decoder(), (dispatch) => create_ollama_stream_aggregator(req.step_index, dispatch));
|
|
3482
3649
|
}
|
|
3483
3650
|
const SUPPORTED$5 = /* @__PURE__ */ new Set([
|
|
3484
3651
|
"text",
|
|
@@ -3499,20 +3666,14 @@ const create_ollama_native_adapter = (init) => {
|
|
|
3499
3666
|
kind: "native",
|
|
3500
3667
|
name: "ollama",
|
|
3501
3668
|
async invoke_turn(req) {
|
|
3502
|
-
|
|
3503
|
-
|
|
3504
|
-
|
|
3505
|
-
|
|
3506
|
-
|
|
3507
|
-
|
|
3508
|
-
|
|
3509
|
-
|
|
3510
|
-
} catch (err) {
|
|
3511
|
-
rethrow_network_failure(err, req.abort);
|
|
3512
|
-
}
|
|
3513
|
-
if (!response.ok) throw await response_error(response);
|
|
3514
|
-
if (req.stream) return consume_ndjson_response(response, req);
|
|
3515
|
-
return parse_ollama_chat(await response.json(), req.step_index);
|
|
3669
|
+
return invoke_http_turn(req, "ollama", {
|
|
3670
|
+
url: `${base_url}/api/chat`,
|
|
3671
|
+
headers: { "content-type": "application/json" },
|
|
3672
|
+
build_body: () => build_ollama_chat_body(req)
|
|
3673
|
+
}, {
|
|
3674
|
+
stream: (response) => consume_ndjson_response(response, req),
|
|
3675
|
+
parse: (payload) => parse_ollama_chat(payload, req.step_index)
|
|
3676
|
+
});
|
|
3516
3677
|
},
|
|
3517
3678
|
supports: (capability) => SUPPORTED$5.has(capability)
|
|
3518
3679
|
};
|
|
@@ -3530,15 +3691,11 @@ function translate_ollama_effort(effort) {
|
|
|
3530
3691
|
};
|
|
3531
3692
|
}
|
|
3532
3693
|
/**
|
|
3533
|
-
* Normalize Ollama's raw usage payload into UsageTotals
|
|
3694
|
+
* Normalize Ollama's raw usage payload into UsageTotals: the shared
|
|
3695
|
+
* local-backend mapper, since Ollama never reports cache or reasoning
|
|
3696
|
+
* tokens.
|
|
3534
3697
|
*/
|
|
3535
|
-
|
|
3536
|
-
const base = default_normalize_usage(raw);
|
|
3537
|
-
delete base.cached_input_tokens;
|
|
3538
|
-
delete base.cache_write_tokens;
|
|
3539
|
-
delete base.reasoning_tokens;
|
|
3540
|
-
return base;
|
|
3541
|
-
}
|
|
3698
|
+
const normalize_ollama_usage = normalize_local_usage;
|
|
3542
3699
|
const SUPPORTED$4 = /* @__PURE__ */ new Set([
|
|
3543
3700
|
"text",
|
|
3544
3701
|
"tools",
|
|
@@ -3585,15 +3742,11 @@ function translate_lmstudio_effort(effort) {
|
|
|
3585
3742
|
};
|
|
3586
3743
|
}
|
|
3587
3744
|
/**
|
|
3588
|
-
* Normalize LM Studio's raw usage payload into UsageTotals
|
|
3745
|
+
* Normalize LM Studio's raw usage payload into UsageTotals: the shared
|
|
3746
|
+
* local-backend mapper, since LM Studio never reports cache or reasoning
|
|
3747
|
+
* tokens.
|
|
3589
3748
|
*/
|
|
3590
|
-
|
|
3591
|
-
const base = default_normalize_usage(raw);
|
|
3592
|
-
delete base.cached_input_tokens;
|
|
3593
|
-
delete base.cache_write_tokens;
|
|
3594
|
-
delete base.reasoning_tokens;
|
|
3595
|
-
return base;
|
|
3596
|
-
}
|
|
3749
|
+
const normalize_lmstudio_usage = normalize_local_usage;
|
|
3597
3750
|
const SUPPORTED$3 = /* @__PURE__ */ new Set([
|
|
3598
3751
|
"text",
|
|
3599
3752
|
"tools",
|
|
@@ -3705,8 +3858,7 @@ const create_openrouter_adapter = (init) => {
|
|
|
3705
3858
|
* `openrouter provider requires a non-empty api_key` engine_config_error.
|
|
3706
3859
|
*/
|
|
3707
3860
|
const create_openrouter_native_adapter = (init) => {
|
|
3708
|
-
const
|
|
3709
|
-
const base_url = typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : DEFAULT_BASE_URL;
|
|
3861
|
+
const { api_key, base_url } = read_dialect_init(init, DEFAULT_BASE_URL);
|
|
3710
3862
|
const http_referer = typeof init["http_referer"] === "string" ? init["http_referer"] : void 0;
|
|
3711
3863
|
const x_title = typeof init["x_title"] === "string" ? init["x_title"] : void 0;
|
|
3712
3864
|
const extra_headers = {};
|
|
@@ -3941,6 +4093,51 @@ function validate_auth_config(config) {
|
|
|
3941
4093
|
}
|
|
3942
4094
|
}
|
|
3943
4095
|
/**
|
|
4096
|
+
* Copy the string-valued entries of `source` onto `target`, dropping any
|
|
4097
|
+
* `undefined` slot (the shape `process.env` and caller overrides both carry).
|
|
4098
|
+
*/
|
|
4099
|
+
function copy_string_entries(target, source) {
|
|
4100
|
+
for (const [key, value] of Object.entries(source)) if (typeof value === "string") target[key] = value;
|
|
4101
|
+
}
|
|
4102
|
+
/**
|
|
4103
|
+
* Seed the base subprocess env before caller overrides and the auth scrub.
|
|
4104
|
+
*
|
|
4105
|
+
* `inherit_env: false` starts from an empty env. Otherwise `'oauth'` inherits
|
|
4106
|
+
* the full `process.env` (the CLI needs HOME plus session-specific vars to
|
|
4107
|
+
* reach the logged-in session); every other mode inherits only the standard
|
|
4108
|
+
* keys needed to resolve the sandbox wrapper and the claude binary on PATH.
|
|
4109
|
+
*/
|
|
4110
|
+
function seed_base_env(config, auth_mode) {
|
|
4111
|
+
const env = {};
|
|
4112
|
+
if (config.inherit_env === false) return env;
|
|
4113
|
+
if (auth_mode === "oauth") {
|
|
4114
|
+
copy_string_entries(env, process.env);
|
|
4115
|
+
return env;
|
|
4116
|
+
}
|
|
4117
|
+
for (const key of STANDARD_ENV_KEYS) {
|
|
4118
|
+
const value = process.env[key];
|
|
4119
|
+
if (typeof value === "string") env[key] = value;
|
|
4120
|
+
}
|
|
4121
|
+
return env;
|
|
4122
|
+
}
|
|
4123
|
+
/**
|
|
4124
|
+
* Apply the auth-mode scrub last, after base and caller env are merged, so
|
|
4125
|
+
* nothing can re-introduce `ANTHROPIC_API_KEY` under `'oauth'`. `'api_key'`
|
|
4126
|
+
* forces the configured key (empty string when absent); `'auto'` forwards a
|
|
4127
|
+
* non-empty configured key and otherwise leaves the inherited env untouched.
|
|
4128
|
+
*/
|
|
4129
|
+
function apply_auth_key(env, config, auth_mode) {
|
|
4130
|
+
if (auth_mode === "oauth") {
|
|
4131
|
+
delete env["ANTHROPIC_API_KEY"];
|
|
4132
|
+
return;
|
|
4133
|
+
}
|
|
4134
|
+
if (auth_mode === "api_key") {
|
|
4135
|
+
env["ANTHROPIC_API_KEY"] = typeof config.api_key === "string" ? config.api_key : "";
|
|
4136
|
+
return;
|
|
4137
|
+
}
|
|
4138
|
+
if (typeof config.api_key === "string" && config.api_key.length > 0) env["ANTHROPIC_API_KEY"] = config.api_key;
|
|
4139
|
+
}
|
|
4140
|
+
/**
|
|
3944
4141
|
* Compose the subprocess environment for one claude CLI invocation.
|
|
3945
4142
|
*
|
|
3946
4143
|
* Regardless of `auth_mode`, seeds the standard process-env keys (PATH,
|
|
@@ -3956,25 +4153,9 @@ function validate_auth_config(config) {
|
|
|
3956
4153
|
* in, so nothing can re-introduce `ANTHROPIC_API_KEY` under `'oauth'`.
|
|
3957
4154
|
*/
|
|
3958
4155
|
function build_env(config, caller_env, auth_mode) {
|
|
3959
|
-
const env =
|
|
3960
|
-
if (
|
|
3961
|
-
|
|
3962
|
-
} else for (const key of STANDARD_ENV_KEYS) {
|
|
3963
|
-
const v = process.env[key];
|
|
3964
|
-
if (typeof v === "string") env[key] = v;
|
|
3965
|
-
}
|
|
3966
|
-
if (caller_env !== void 0) {
|
|
3967
|
-
for (const [k, v] of Object.entries(caller_env)) if (typeof v === "string") env[k] = v;
|
|
3968
|
-
}
|
|
3969
|
-
if (auth_mode === "oauth") {
|
|
3970
|
-
delete env["ANTHROPIC_API_KEY"];
|
|
3971
|
-
return env;
|
|
3972
|
-
}
|
|
3973
|
-
if (auth_mode === "api_key") {
|
|
3974
|
-
env["ANTHROPIC_API_KEY"] = typeof config.api_key === "string" ? config.api_key : "";
|
|
3975
|
-
return env;
|
|
3976
|
-
}
|
|
3977
|
-
if (typeof config.api_key === "string" && config.api_key.length > 0) env["ANTHROPIC_API_KEY"] = config.api_key;
|
|
4156
|
+
const env = seed_base_env(config, auth_mode);
|
|
4157
|
+
if (caller_env !== void 0) copy_string_entries(env, caller_env);
|
|
4158
|
+
apply_auth_key(env, config, auth_mode);
|
|
3978
4159
|
return env;
|
|
3979
4160
|
}
|
|
3980
4161
|
/**
|
|
@@ -4969,6 +5150,21 @@ function decompose_total_cost(total_cost_usd, usage) {
|
|
|
4969
5150
|
return breakdown;
|
|
4970
5151
|
}
|
|
4971
5152
|
/**
|
|
5153
|
+
* Split `total_cost_usd` into a per-turn total for each turn, proportional to
|
|
5154
|
+
* its output tokens, falling back to an equal split when every turn reports
|
|
5155
|
+
* zero output (the proportional weights would otherwise all be zero). The last
|
|
5156
|
+
* turn absorbs the floating-point rounding remainder so the shares sum exactly
|
|
5157
|
+
* to `total_cost_usd`. Caller guarantees a non-empty `turns`.
|
|
5158
|
+
*/
|
|
5159
|
+
function split_total_across_turns(total_cost_usd, turns) {
|
|
5160
|
+
const total_output = turns.reduce((sum, t) => sum + Math.max(0, t.output_tokens), 0);
|
|
5161
|
+
const per_turn = turns.map((t) => total_output === 0 ? total_cost_usd / turns.length : total_cost_usd * (Math.max(0, t.output_tokens) / total_output));
|
|
5162
|
+
const assigned = per_turn.reduce((a, b) => a + b, 0);
|
|
5163
|
+
const last_index = turns.length - 1;
|
|
5164
|
+
per_turn[last_index] = (per_turn[last_index] ?? 0) + (total_cost_usd - assigned);
|
|
5165
|
+
return per_turn;
|
|
5166
|
+
}
|
|
5167
|
+
/**
|
|
4972
5168
|
* Split a call's total cost across its turns, proportional to each turn's
|
|
4973
5169
|
* output tokens, then decompose each turn's share into a `CostBreakdown`.
|
|
4974
5170
|
*
|
|
@@ -4979,29 +5175,8 @@ function decompose_total_cost(total_cost_usd, usage) {
|
|
|
4979
5175
|
*/
|
|
4980
5176
|
function allocate_cost_across_turns(total_cost_usd, turns) {
|
|
4981
5177
|
if (turns.length === 0) return [];
|
|
4982
|
-
const
|
|
4983
|
-
|
|
4984
|
-
if (total_output === 0) {
|
|
4985
|
-
const equal_share = total_cost_usd / turns.length;
|
|
4986
|
-
for (let i = 0; i < turns.length; i += 1) per_turn_totals[i] = equal_share;
|
|
4987
|
-
} else for (let i = 0; i < turns.length; i += 1) {
|
|
4988
|
-
const t = turns[i];
|
|
4989
|
-
if (t === void 0) continue;
|
|
4990
|
-
per_turn_totals[i] = total_cost_usd * (Math.max(0, t.output_tokens) / total_output);
|
|
4991
|
-
}
|
|
4992
|
-
const remainder = total_cost_usd - per_turn_totals.reduce((a, b) => a + b, 0);
|
|
4993
|
-
if (turns.length > 0) {
|
|
4994
|
-
const last_index = turns.length - 1;
|
|
4995
|
-
per_turn_totals[last_index] = (per_turn_totals[last_index] ?? 0) + remainder;
|
|
4996
|
-
}
|
|
4997
|
-
const out = [];
|
|
4998
|
-
for (let i = 0; i < turns.length; i += 1) {
|
|
4999
|
-
const t = turns[i];
|
|
5000
|
-
if (t === void 0) continue;
|
|
5001
|
-
const total = per_turn_totals[i] ?? 0;
|
|
5002
|
-
out.push(decompose_total_cost(total, t.usage));
|
|
5003
|
-
}
|
|
5004
|
-
return out;
|
|
5178
|
+
const per_turn = split_total_across_turns(total_cost_usd, turns);
|
|
5179
|
+
return turns.map((t, i) => decompose_total_cost(per_turn[i] ?? 0, t.usage));
|
|
5005
5180
|
}
|
|
5006
5181
|
//#endregion
|
|
5007
5182
|
//#region src/engine/providers/claude_cli/stream_result.ts
|
|
@@ -5697,94 +5872,282 @@ async function run_cli(spawn_runtime, args) {
|
|
|
5697
5872
|
};
|
|
5698
5873
|
}
|
|
5699
5874
|
/**
|
|
5700
|
-
*
|
|
5701
|
-
*
|
|
5702
|
-
*
|
|
5703
|
-
* a spawn runtime plus an in-flight abort-controller registry that
|
|
5704
|
-
* `dispose()` uses to cancel every call still running.
|
|
5875
|
+
* Emit `option_ignored` events for the loop-control options this provider
|
|
5876
|
+
* cannot honor: the CLI runs its own tool loop on the far side of the
|
|
5877
|
+
* subprocess boundary.
|
|
5705
5878
|
*/
|
|
5706
|
-
function
|
|
5707
|
-
const
|
|
5708
|
-
|
|
5709
|
-
|
|
5710
|
-
|
|
5711
|
-
|
|
5712
|
-
|
|
5713
|
-
|
|
5714
|
-
|
|
5715
|
-
|
|
5879
|
+
function emit_ignored_options(opts, trajectory) {
|
|
5880
|
+
const option_ignored = create_option_ignored_dedup(trajectory);
|
|
5881
|
+
if (opts.max_steps !== void 0) option_ignored.emit("max_steps", PROVIDER_NAME);
|
|
5882
|
+
if (opts.tool_error_policy !== void 0) option_ignored.emit("tool_error_policy", PROVIDER_NAME);
|
|
5883
|
+
if (opts.on_tool_approval !== void 0) option_ignored.emit("on_tool_approval", PROVIDER_NAME);
|
|
5884
|
+
if (opts.tool_call_repair_attempts !== void 0) option_ignored.emit("tool_call_repair_attempts", PROVIDER_NAME);
|
|
5885
|
+
if (opts.max_tool_calls_per_step !== void 0) option_ignored.emit("max_tool_calls_per_step", PROVIDER_NAME);
|
|
5886
|
+
}
|
|
5887
|
+
/**
|
|
5888
|
+
* Reject requests the CLI cannot express: multi-turn user history (one user
|
|
5889
|
+
* turn per invocation; continuation goes through `session_id`), and
|
|
5890
|
+
* executable tools under `tool_bridge: 'forbid'`.
|
|
5891
|
+
*/
|
|
5892
|
+
function validate_request(prompt, tools_list, tool_bridge) {
|
|
5893
|
+
if (count_user_messages(prompt) >= 2) throw new provider_capability_error(PROVIDER_NAME, "multi_turn_history", "use provider_options.claude_cli.session_id instead");
|
|
5894
|
+
if (tools_list.length > 0 && tool_bridge === "forbid") {
|
|
5895
|
+
if (tools_list.some((t) => typeof t.execute === "function")) throw new provider_capability_error(PROVIDER_NAME, "tool_execute", "tool_bridge is forbid; tools with execute closures cannot run under claude_cli");
|
|
5896
|
+
}
|
|
5897
|
+
}
|
|
5898
|
+
/**
|
|
5899
|
+
* Record which executable tools the allowlist-only bridge is dropping:
|
|
5900
|
+
* their names still pass through `--allowedTools`, but their execute
|
|
5901
|
+
* closures cannot run on this side of the subprocess boundary.
|
|
5902
|
+
*/
|
|
5903
|
+
function record_allowlist_only_drop(tools_list, tool_bridge, trajectory) {
|
|
5904
|
+
if (tools_list.length === 0 || tool_bridge !== "allowlist_only" || trajectory === void 0) return;
|
|
5905
|
+
const dropped = tools_list.filter((t) => typeof t.execute === "function").map((t) => t.name);
|
|
5906
|
+
trajectory.record({
|
|
5907
|
+
kind: "cli_tool_bridge_allowlist_only",
|
|
5908
|
+
dropped
|
|
5909
|
+
});
|
|
5910
|
+
}
|
|
5911
|
+
/**
|
|
5912
|
+
* Choose the JSON Schema string for `--json-schema`: a typed `opts.schema`
|
|
5913
|
+
* compiles and wins; otherwise a raw `output_json_schema` string passes
|
|
5914
|
+
* through verbatim.
|
|
5915
|
+
*/
|
|
5916
|
+
function resolve_compiled_schema(opts, call_opts) {
|
|
5917
|
+
if (opts.schema !== void 0) return compile_schema(opts.schema);
|
|
5918
|
+
if (typeof call_opts.output_json_schema === "string" && call_opts.output_json_schema.length > 0) return call_opts.output_json_schema;
|
|
5919
|
+
}
|
|
5920
|
+
/**
|
|
5921
|
+
* Wrap `opts.on_chunk` so sync and async callbacks dispatch the same way: a
|
|
5922
|
+
* returned thenable is awaited, any other return is fire-and-done. Returns
|
|
5923
|
+
* `undefined` when the caller isn't streaming, which downstream code uses
|
|
5924
|
+
* as the streaming flag.
|
|
5925
|
+
*/
|
|
5926
|
+
function make_dispatch_chunk(opts) {
|
|
5927
|
+
if (opts.on_chunk === void 0) return void 0;
|
|
5928
|
+
return async (chunk) => {
|
|
5929
|
+
const maybe = opts.on_chunk?.(chunk);
|
|
5930
|
+
if (maybe !== void 0 && typeof maybe.then === "function") await maybe;
|
|
5931
|
+
};
|
|
5932
|
+
}
|
|
5933
|
+
/**
|
|
5934
|
+
* Create this call's AbortController, register it in the adapter's
|
|
5935
|
+
* in-flight set (`dispose()` aborts everything still registered), and
|
|
5936
|
+
* forward the caller's abort signal into it, including one already aborted.
|
|
5937
|
+
* `unlink` undoes both and must run however generate settles, or the
|
|
5938
|
+
* registry would pin settled calls alive.
|
|
5939
|
+
*/
|
|
5940
|
+
function link_caller_abort(in_flight, caller_abort) {
|
|
5941
|
+
const controller = new AbortController();
|
|
5942
|
+
in_flight.add(controller);
|
|
5943
|
+
const on_caller_abort = () => {
|
|
5944
|
+
controller.abort(caller_abort?.reason);
|
|
5945
|
+
};
|
|
5946
|
+
if (caller_abort !== void 0) if (caller_abort.aborted) controller.abort(caller_abort.reason);
|
|
5947
|
+
else caller_abort.addEventListener("abort", on_caller_abort, { once: true });
|
|
5716
5948
|
return {
|
|
5717
|
-
|
|
5718
|
-
|
|
5719
|
-
|
|
5720
|
-
|
|
5721
|
-
|
|
5722
|
-
|
|
5723
|
-
|
|
5724
|
-
|
|
5725
|
-
|
|
5726
|
-
|
|
5727
|
-
|
|
5728
|
-
|
|
5729
|
-
|
|
5730
|
-
|
|
5731
|
-
|
|
5732
|
-
|
|
5733
|
-
|
|
5734
|
-
|
|
5735
|
-
|
|
5736
|
-
|
|
5737
|
-
|
|
5738
|
-
|
|
5739
|
-
|
|
5740
|
-
|
|
5741
|
-
|
|
5742
|
-
|
|
5743
|
-
|
|
5744
|
-
|
|
5949
|
+
controller,
|
|
5950
|
+
unlink: () => {
|
|
5951
|
+
if (caller_abort !== void 0) caller_abort.removeEventListener("abort", on_caller_abort);
|
|
5952
|
+
in_flight.delete(controller);
|
|
5953
|
+
}
|
|
5954
|
+
};
|
|
5955
|
+
}
|
|
5956
|
+
/**
|
|
5957
|
+
* Open the generate span and the first step span before the CLI spawns, so
|
|
5958
|
+
* their timing brackets the whole subprocess run.
|
|
5959
|
+
*/
|
|
5960
|
+
function open_spans(trajectory, opts, resolved, has_tools, streaming) {
|
|
5961
|
+
return {
|
|
5962
|
+
generate: start_generate_span(trajectory, {
|
|
5963
|
+
model: opts.model ?? resolved.model_id,
|
|
5964
|
+
provider: PROVIDER_NAME,
|
|
5965
|
+
model_id: resolved.model_id,
|
|
5966
|
+
has_tools,
|
|
5967
|
+
has_schema: opts.schema !== void 0,
|
|
5968
|
+
streaming
|
|
5969
|
+
}),
|
|
5970
|
+
first_step: start_step_span(trajectory, 0)
|
|
5971
|
+
};
|
|
5972
|
+
}
|
|
5973
|
+
/**
|
|
5974
|
+
* Failure message for schema validation that exhausted its repair budget.
|
|
5975
|
+
* Zero names the opt-out explicitly so "repair disabled" reads differently
|
|
5976
|
+
* from "repairs tried and failed".
|
|
5977
|
+
*/
|
|
5978
|
+
function repair_exhausted_message(max_repairs) {
|
|
5979
|
+
if (max_repairs === 0) return "schema validation failed and repair is disabled (schema_repair_attempts: 0)";
|
|
5980
|
+
return `schema validation failed after ${String(max_repairs)} repair attempt${max_repairs === 1 ? "" : "s"}`;
|
|
5981
|
+
}
|
|
5982
|
+
/**
|
|
5983
|
+
* Validate the CLI's final text against `schema`, re-invoking the CLI with
|
|
5984
|
+
* a repair prompt on failure, up to `max_repairs` times.
|
|
5985
|
+
*
|
|
5986
|
+
* Each repair resumes the previous attempt's `session_id`, so a snapshot
|
|
5987
|
+
* without one makes validation failure terminal immediately. The returned
|
|
5988
|
+
* snapshot is the one the repair loop settled on; the result must be built
|
|
5989
|
+
* from it, not from the first attempt's.
|
|
5990
|
+
*/
|
|
5991
|
+
async function parse_schema_with_repair(spawn_runtime, base_args, schema, max_repairs, first_parsed, trajectory) {
|
|
5992
|
+
let parsed = first_parsed;
|
|
5993
|
+
let repairs_done = 0;
|
|
5994
|
+
for (;;) {
|
|
5995
|
+
const attempt = await parse_with_schema(schema, parsed.final_text);
|
|
5996
|
+
if (attempt.ok) return {
|
|
5997
|
+
parsed,
|
|
5998
|
+
parsed_content: attempt.value
|
|
5999
|
+
};
|
|
6000
|
+
record_schema_validation_failed(trajectory, {
|
|
6001
|
+
attempt: repairs_done === 0 ? "initial" : "repair",
|
|
6002
|
+
schema_issues: format_schema_issues(attempt.issues),
|
|
6003
|
+
raw_text: parsed.final_text
|
|
6004
|
+
});
|
|
6005
|
+
if (repairs_done >= max_repairs) throw new schema_validation_error(repair_exhausted_message(max_repairs), attempt.issues, parsed.final_text);
|
|
6006
|
+
const repair_session_id = parsed.session_id;
|
|
6007
|
+
if (repair_session_id === void 0) throw new schema_validation_error("schema validation failed and no session_id available for repair", attempt.issues, parsed.final_text);
|
|
6008
|
+
parsed = (await run_cli(spawn_runtime, {
|
|
6009
|
+
...base_args,
|
|
6010
|
+
stdin_text: build_repair_prompt_text(attempt.issues),
|
|
6011
|
+
call_opts: {
|
|
6012
|
+
...base_args.call_opts,
|
|
6013
|
+
session_id: repair_session_id
|
|
5745
6014
|
}
|
|
5746
|
-
|
|
5747
|
-
|
|
5748
|
-
|
|
5749
|
-
|
|
5750
|
-
|
|
5751
|
-
|
|
6015
|
+
})).parsed;
|
|
6016
|
+
repairs_done += 1;
|
|
6017
|
+
}
|
|
6018
|
+
}
|
|
6019
|
+
/**
|
|
6020
|
+
* Resolve the call's final parse snapshot and validated content. Without a
|
|
6021
|
+
* schema the first snapshot is final and content stays undefined; with one,
|
|
6022
|
+
* the repair loop takes over.
|
|
6023
|
+
*/
|
|
6024
|
+
async function resolve_content(spawn_runtime, base_args, opts, first_parsed) {
|
|
6025
|
+
if (opts.schema === void 0) return {
|
|
6026
|
+
parsed: first_parsed,
|
|
6027
|
+
parsed_content: void 0
|
|
6028
|
+
};
|
|
6029
|
+
return parse_schema_with_repair(spawn_runtime, base_args, opts.schema, opts.schema_repair_attempts ?? 1, first_parsed, base_args.trajectory);
|
|
6030
|
+
}
|
|
6031
|
+
/**
|
|
6032
|
+
* Assemble the `build_generate_result` input, attaching the schema and its
|
|
6033
|
+
* validated value only when structured output was requested.
|
|
6034
|
+
*/
|
|
6035
|
+
function assemble_result$1(parsed, resolved, schema, parsed_content) {
|
|
6036
|
+
return build_generate_result({
|
|
6037
|
+
parsed,
|
|
6038
|
+
resolved,
|
|
6039
|
+
...schema !== void 0 ? { schema } : {},
|
|
6040
|
+
...parsed_content !== void 0 ? { parsed_content } : {}
|
|
6041
|
+
});
|
|
6042
|
+
}
|
|
6043
|
+
/**
|
|
6044
|
+
* Forward the CLI's per-turn provider-reported costs into the trajectory.
|
|
6045
|
+
*/
|
|
6046
|
+
function record_provider_costs(trajectory, steps) {
|
|
6047
|
+
for (const step of steps) if (step.cost !== void 0) record_cost(trajectory, step.index, step.cost, "provider_reported");
|
|
6048
|
+
}
|
|
6049
|
+
/**
|
|
6050
|
+
* Close the eagerly-opened first step span against the steps the CLI
|
|
6051
|
+
* reported, then emit paired open/close spans for steps 1..n; those arrive
|
|
6052
|
+
* all at once on process close, so their spans carry usage, not timing.
|
|
6053
|
+
* Clears `spans.first_step` so the error path cannot close it twice.
|
|
6054
|
+
*/
|
|
6055
|
+
function close_step_spans(trajectory, spans, steps) {
|
|
6056
|
+
if (steps.length === 0) {
|
|
6057
|
+
end_step_span(trajectory, spans.first_step, {});
|
|
6058
|
+
spans.first_step = void 0;
|
|
6059
|
+
return;
|
|
6060
|
+
}
|
|
6061
|
+
const head = steps[0];
|
|
6062
|
+
if (head !== void 0) end_step_span(trajectory, spans.first_step, {
|
|
6063
|
+
usage: head.usage,
|
|
6064
|
+
finish_reason: head.finish_reason
|
|
6065
|
+
});
|
|
6066
|
+
spans.first_step = void 0;
|
|
6067
|
+
for (let i = 1; i < steps.length; i += 1) {
|
|
6068
|
+
const s = steps[i];
|
|
6069
|
+
if (s === void 0) continue;
|
|
6070
|
+
end_step_span(trajectory, start_step_span(trajectory, i), {
|
|
6071
|
+
usage: s.usage,
|
|
6072
|
+
finish_reason: s.finish_reason
|
|
6073
|
+
});
|
|
6074
|
+
}
|
|
6075
|
+
}
|
|
6076
|
+
/**
|
|
6077
|
+
* Emit the terminal `finish` chunk streaming callers expect; the CLI stream
|
|
6078
|
+
* has no equivalent event, so it is synthesized from the final result.
|
|
6079
|
+
*/
|
|
6080
|
+
async function dispatch_finish(dispatch_chunk, result) {
|
|
6081
|
+
if (dispatch_chunk === void 0) return;
|
|
6082
|
+
await dispatch_chunk({
|
|
6083
|
+
kind: "finish",
|
|
6084
|
+
finish_reason: result.finish_reason,
|
|
6085
|
+
usage: result.usage
|
|
6086
|
+
});
|
|
6087
|
+
}
|
|
6088
|
+
/**
|
|
6089
|
+
* Close whatever spans are still open with the failure message. A failure
|
|
6090
|
+
* after the step spans already closed (e.g. in the finish-chunk dispatch)
|
|
6091
|
+
* closes only the generate span.
|
|
6092
|
+
*/
|
|
6093
|
+
function close_spans_on_error(trajectory, spans, err) {
|
|
6094
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
6095
|
+
if (spans.first_step !== void 0) {
|
|
6096
|
+
end_step_span(trajectory, spans.first_step, { error: message });
|
|
6097
|
+
spans.first_step = void 0;
|
|
6098
|
+
}
|
|
6099
|
+
end_generate_span(trajectory, spans.generate, { error: message });
|
|
6100
|
+
}
|
|
6101
|
+
/**
|
|
6102
|
+
* Build a claude_cli `ExternalAgentAdapter` bound to one config.
|
|
6103
|
+
*
|
|
6104
|
+
* Validates the auth config eagerly, before any call is made, and wires up
|
|
6105
|
+
* a spawn runtime plus an in-flight abort-controller registry that
|
|
6106
|
+
* `dispose()` uses to cancel every call still running.
|
|
6107
|
+
*/
|
|
6108
|
+
function create_claude_cli_adapter(init) {
|
|
6109
|
+
const config = init;
|
|
6110
|
+
validate_auth_config(config);
|
|
6111
|
+
const spawn_runtime = create_spawn_runtime();
|
|
6112
|
+
let disposed = false;
|
|
6113
|
+
const in_flight = /* @__PURE__ */ new Set();
|
|
6114
|
+
const resolved_binary = typeof config.binary === "string" && config.binary.length > 0 ? config.binary : CLI_BINARY_DEFAULT;
|
|
6115
|
+
const resolved_auth_mode = config.auth_mode ?? "auto";
|
|
6116
|
+
const resolved_startup_timeout = config.startup_timeout_ms ?? 12e4;
|
|
6117
|
+
const resolved_stall_timeout = config.stall_timeout_ms ?? 3e5;
|
|
6118
|
+
return {
|
|
6119
|
+
kind: "external",
|
|
6120
|
+
name: PROVIDER_NAME,
|
|
6121
|
+
supports: (capability) => SUPPORTED.has(capability),
|
|
6122
|
+
async generate(opts, resolved) {
|
|
6123
|
+
if (disposed) throw new engine_disposed_error();
|
|
6124
|
+
if (opts.abort?.aborted === true) throw new aborted_error("aborted", { reason: opts.abort.reason });
|
|
6125
|
+
const trajectory = with_timestamps(opts.trajectory);
|
|
6126
|
+
const call_opts = extract_call_opts(opts);
|
|
6127
|
+
emit_ignored_options(opts, trajectory);
|
|
6128
|
+
const tools_list = opts.tools ?? [];
|
|
6129
|
+
const tool_bridge = call_opts.tool_bridge ?? "allowlist_only";
|
|
6130
|
+
validate_request(opts.prompt, tools_list, tool_bridge);
|
|
6131
|
+
const merged_allowed_tools = merge_allowed_tools(call_opts.allowed_tools, tools_list.map((t) => t.name));
|
|
6132
|
+
record_allowlist_only_drop(tools_list, tool_bridge, trajectory);
|
|
6133
|
+
const merged_system = merge_system(opts.system ?? extract_system_text(opts.prompt), call_opts.append_system_prompt);
|
|
5752
6134
|
const env = build_env(config, call_opts.env, resolved_auth_mode);
|
|
5753
6135
|
Object.assign(env, effort_env_for_claude_cli(opts.effort));
|
|
5754
6136
|
const sandbox_plan = build_sandbox_plan(resolved_binary, config.sandbox);
|
|
5755
|
-
const
|
|
5756
|
-
const
|
|
5757
|
-
|
|
5758
|
-
if (maybe !== void 0 && typeof maybe.then === "function") await maybe;
|
|
5759
|
-
} : void 0;
|
|
5760
|
-
const controller = new AbortController();
|
|
5761
|
-
in_flight.add(controller);
|
|
5762
|
-
const caller_abort = opts.abort;
|
|
5763
|
-
const on_caller_abort = () => {
|
|
5764
|
-
controller.abort(caller_abort?.reason);
|
|
5765
|
-
};
|
|
5766
|
-
if (caller_abort !== void 0) if (caller_abort.aborted) controller.abort(caller_abort.reason);
|
|
5767
|
-
else caller_abort.addEventListener("abort", on_caller_abort, { once: true });
|
|
5768
|
-
const generate_span = start_generate_span(trajectory, {
|
|
5769
|
-
model: opts.model ?? resolved.model_id,
|
|
5770
|
-
provider: PROVIDER_NAME,
|
|
5771
|
-
model_id: resolved.model_id,
|
|
5772
|
-
has_tools: tools_list.length > 0,
|
|
5773
|
-
has_schema: opts.schema !== void 0,
|
|
5774
|
-
streaming: dispatch_chunk !== void 0
|
|
5775
|
-
});
|
|
5776
|
-
let first_step_span = start_step_span(trajectory, 0);
|
|
6137
|
+
const dispatch_chunk = make_dispatch_chunk(opts);
|
|
6138
|
+
const { controller, unlink } = link_caller_abort(in_flight, opts.abort);
|
|
6139
|
+
const spans = open_spans(trajectory, opts, resolved, tools_list.length > 0, dispatch_chunk !== void 0);
|
|
5777
6140
|
const base_args = {
|
|
5778
6141
|
model_id: resolved.model_id,
|
|
5779
|
-
stdin_text,
|
|
6142
|
+
stdin_text: extract_prompt_text(opts.prompt),
|
|
5780
6143
|
merged_system,
|
|
5781
6144
|
merged_allowed_tools,
|
|
5782
6145
|
call_opts,
|
|
5783
|
-
compiled_schema,
|
|
6146
|
+
compiled_schema: resolve_compiled_schema(opts, call_opts),
|
|
5784
6147
|
env,
|
|
5785
6148
|
spawn_cmd: sandbox_plan.spawn_cmd,
|
|
5786
6149
|
prefix_args: sandbox_plan.prefix_args,
|
|
5787
|
-
cwd,
|
|
6150
|
+
cwd: config.default_cwd,
|
|
5788
6151
|
startup_timeout_ms: resolved_startup_timeout,
|
|
5789
6152
|
stall_timeout_ms: resolved_stall_timeout,
|
|
5790
6153
|
abort: controller.signal,
|
|
@@ -5792,86 +6155,23 @@ function create_claude_cli_adapter(init) {
|
|
|
5792
6155
|
trajectory
|
|
5793
6156
|
};
|
|
5794
6157
|
try {
|
|
5795
|
-
|
|
5796
|
-
|
|
5797
|
-
|
|
5798
|
-
|
|
5799
|
-
|
|
5800
|
-
|
|
5801
|
-
const attempt = await parse_with_schema(opts.schema, parsed.final_text);
|
|
5802
|
-
if (attempt.ok) {
|
|
5803
|
-
parsed_content = attempt.value;
|
|
5804
|
-
break;
|
|
5805
|
-
}
|
|
5806
|
-
record_schema_validation_failed(trajectory, {
|
|
5807
|
-
attempt: repairs_done === 0 ? "initial" : "repair",
|
|
5808
|
-
schema_issues: format_schema_issues(attempt.issues),
|
|
5809
|
-
raw_text: parsed.final_text
|
|
5810
|
-
});
|
|
5811
|
-
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);
|
|
5812
|
-
const repair_session_id = parsed.session_id;
|
|
5813
|
-
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);
|
|
5814
|
-
const repair_call_opts = {
|
|
5815
|
-
...call_opts,
|
|
5816
|
-
session_id: repair_session_id
|
|
5817
|
-
};
|
|
5818
|
-
parsed = (await run_cli(spawn_runtime, {
|
|
5819
|
-
...base_args,
|
|
5820
|
-
stdin_text: build_repair_prompt_text(attempt.issues),
|
|
5821
|
-
call_opts: repair_call_opts
|
|
5822
|
-
})).parsed;
|
|
5823
|
-
repairs_done += 1;
|
|
5824
|
-
}
|
|
5825
|
-
}
|
|
5826
|
-
const result_input = {
|
|
5827
|
-
parsed,
|
|
5828
|
-
resolved
|
|
5829
|
-
};
|
|
5830
|
-
if (opts.schema !== void 0) result_input.schema = opts.schema;
|
|
5831
|
-
if (parsed_content !== void 0) result_input.parsed_content = parsed_content;
|
|
5832
|
-
const result = build_generate_result(result_input);
|
|
5833
|
-
for (const step of result.steps) if (step.cost !== void 0) record_cost(trajectory, step.index, step.cost, "provider_reported");
|
|
5834
|
-
if (result.steps.length > 0) {
|
|
5835
|
-
const head = result.steps[0];
|
|
5836
|
-
if (head !== void 0) end_step_span(trajectory, first_step_span, {
|
|
5837
|
-
usage: head.usage,
|
|
5838
|
-
finish_reason: head.finish_reason
|
|
5839
|
-
});
|
|
5840
|
-
first_step_span = void 0;
|
|
5841
|
-
for (let i = 1; i < result.steps.length; i += 1) {
|
|
5842
|
-
const s = result.steps[i];
|
|
5843
|
-
if (s === void 0) continue;
|
|
5844
|
-
end_step_span(trajectory, start_step_span(trajectory, i), {
|
|
5845
|
-
usage: s.usage,
|
|
5846
|
-
finish_reason: s.finish_reason
|
|
5847
|
-
});
|
|
5848
|
-
}
|
|
5849
|
-
} else {
|
|
5850
|
-
end_step_span(trajectory, first_step_span, {});
|
|
5851
|
-
first_step_span = void 0;
|
|
5852
|
-
}
|
|
5853
|
-
end_generate_span(trajectory, generate_span, {
|
|
6158
|
+
const first_outcome = await run_cli(spawn_runtime, base_args);
|
|
6159
|
+
const { parsed, parsed_content } = await resolve_content(spawn_runtime, base_args, opts, first_outcome.parsed);
|
|
6160
|
+
const result = assemble_result$1(parsed, resolved, opts.schema, parsed_content);
|
|
6161
|
+
record_provider_costs(trajectory, result.steps);
|
|
6162
|
+
close_step_spans(trajectory, spans, result.steps);
|
|
6163
|
+
end_generate_span(trajectory, spans.generate, {
|
|
5854
6164
|
usage: result.usage,
|
|
5855
6165
|
finish_reason: result.finish_reason,
|
|
5856
6166
|
model_resolved: result.model_resolved
|
|
5857
6167
|
});
|
|
5858
|
-
|
|
5859
|
-
kind: "finish",
|
|
5860
|
-
finish_reason: result.finish_reason,
|
|
5861
|
-
usage: result.usage
|
|
5862
|
-
});
|
|
6168
|
+
await dispatch_finish(dispatch_chunk, result);
|
|
5863
6169
|
return result;
|
|
5864
6170
|
} catch (err) {
|
|
5865
|
-
|
|
5866
|
-
if (first_step_span !== void 0) {
|
|
5867
|
-
end_step_span(trajectory, first_step_span, { error: message });
|
|
5868
|
-
first_step_span = void 0;
|
|
5869
|
-
}
|
|
5870
|
-
end_generate_span(trajectory, generate_span, { error: message });
|
|
6171
|
+
close_spans_on_error(trajectory, spans, err);
|
|
5871
6172
|
throw err;
|
|
5872
6173
|
} finally {
|
|
5873
|
-
|
|
5874
|
-
in_flight.delete(controller);
|
|
6174
|
+
unlink();
|
|
5875
6175
|
}
|
|
5876
6176
|
},
|
|
5877
6177
|
async dispose() {
|
|
@@ -6070,6 +6370,36 @@ const PARAMETER_RE = /<parameter=([^>\s]+)>([\s\S]*?)<\/parameter>/g;
|
|
|
6070
6370
|
const FENCE_RE = /```([^\n]*)\n([\s\S]*?)```/g;
|
|
6071
6371
|
const EMPTY_BLOCK_RE = /<tool_call>\s*<\/tool_call>/g;
|
|
6072
6372
|
/**
|
|
6373
|
+
* From `open` at an opening quote, return the index of the matching closing
|
|
6374
|
+
* quote, honoring backslash escapes. Returns `text.length` for an unterminated
|
|
6375
|
+
* string, so the caller's scan runs off the end and yields no object.
|
|
6376
|
+
*/
|
|
6377
|
+
function skip_json_string(text, open) {
|
|
6378
|
+
let escaped = false;
|
|
6379
|
+
for (let i = open + 1; i < text.length; i += 1) {
|
|
6380
|
+
const ch = text[i];
|
|
6381
|
+
if (escaped) escaped = false;
|
|
6382
|
+
else if (ch === "\\") escaped = true;
|
|
6383
|
+
else if (ch === "\"") return i;
|
|
6384
|
+
}
|
|
6385
|
+
return text.length;
|
|
6386
|
+
}
|
|
6387
|
+
/**
|
|
6388
|
+
* Parse `text.slice(start, end)` as JSON, pairing the value with `end`, or
|
|
6389
|
+
* `undefined` when the slice is not valid JSON. JSON.parse is the sole
|
|
6390
|
+
* semantic authority; the scanner only fixed the extent.
|
|
6391
|
+
*/
|
|
6392
|
+
function parse_json_extent(text, start, end) {
|
|
6393
|
+
try {
|
|
6394
|
+
return {
|
|
6395
|
+
value: JSON.parse(text.slice(start, end)),
|
|
6396
|
+
end
|
|
6397
|
+
};
|
|
6398
|
+
} catch {
|
|
6399
|
+
return;
|
|
6400
|
+
}
|
|
6401
|
+
}
|
|
6402
|
+
/**
|
|
6073
6403
|
* Find the extent of one balanced JSON object starting at `start_index`
|
|
6074
6404
|
* (which must point at `{`), respecting strings and escapes so braces inside
|
|
6075
6405
|
* string values do not affect the depth count. The scanner only finds the
|
|
@@ -6078,31 +6408,13 @@ const EMPTY_BLOCK_RE = /<tool_call>\s*<\/tool_call>/g;
|
|
|
6078
6408
|
function scan_balanced_json(text, start_index) {
|
|
6079
6409
|
if (text[start_index] !== "{") return void 0;
|
|
6080
6410
|
let depth = 0;
|
|
6081
|
-
let in_string = false;
|
|
6082
|
-
let escaped = false;
|
|
6083
6411
|
for (let i = start_index; i < text.length; i += 1) {
|
|
6084
6412
|
const ch = text[i];
|
|
6085
|
-
if (
|
|
6086
|
-
if (escaped) escaped = false;
|
|
6087
|
-
else if (ch === "\\") escaped = true;
|
|
6088
|
-
else if (ch === "\"") in_string = false;
|
|
6089
|
-
continue;
|
|
6090
|
-
}
|
|
6091
|
-
if (ch === "\"") in_string = true;
|
|
6413
|
+
if (ch === "\"") i = skip_json_string(text, i);
|
|
6092
6414
|
else if (ch === "{") depth += 1;
|
|
6093
6415
|
else if (ch === "}") {
|
|
6094
6416
|
depth -= 1;
|
|
6095
|
-
if (depth === 0)
|
|
6096
|
-
const end = i + 1;
|
|
6097
|
-
try {
|
|
6098
|
-
return {
|
|
6099
|
-
value: JSON.parse(text.slice(start_index, end)),
|
|
6100
|
-
end
|
|
6101
|
-
};
|
|
6102
|
-
} catch {
|
|
6103
|
-
return;
|
|
6104
|
-
}
|
|
6105
|
-
}
|
|
6417
|
+
if (depth === 0) return parse_json_extent(text, start_index, i + 1);
|
|
6106
6418
|
}
|
|
6107
6419
|
}
|
|
6108
6420
|
}
|
|
@@ -6181,6 +6493,38 @@ function scan_qwen_functions(inner_start, inner) {
|
|
|
6181
6493
|
return out;
|
|
6182
6494
|
}
|
|
6183
6495
|
/**
|
|
6496
|
+
* Parse a hermes `<tool_call>{...}</tool_call>` payload into a candidate. The
|
|
6497
|
+
* block's whole extent (`span`) is used so a later strip removes the wrapper
|
|
6498
|
+
* too. Returns `undefined` when the inner object is not call-shaped.
|
|
6499
|
+
*/
|
|
6500
|
+
function scan_hermes_block(text, inner_start, span) {
|
|
6501
|
+
const brace = text.indexOf("{", inner_start);
|
|
6502
|
+
const scanned = brace === -1 ? void 0 : scan_balanced_json(text, brace);
|
|
6503
|
+
const shaped = scanned === void 0 ? void 0 : call_shape(scanned.value);
|
|
6504
|
+
if (shaped === void 0) return void 0;
|
|
6505
|
+
return {
|
|
6506
|
+
name: shaped.name,
|
|
6507
|
+
args: shaped.args,
|
|
6508
|
+
format: "hermes",
|
|
6509
|
+
span
|
|
6510
|
+
};
|
|
6511
|
+
}
|
|
6512
|
+
/**
|
|
6513
|
+
* Parse one located `<tool_call>` block's inner content into candidates,
|
|
6514
|
+
* dispatching on whether it opens with a qwen `<function=` tag or a hermes
|
|
6515
|
+
* `{` object.
|
|
6516
|
+
*/
|
|
6517
|
+
function scan_block_candidates(text, inner_start, close, span) {
|
|
6518
|
+
const inner = text.slice(inner_start, close);
|
|
6519
|
+
const trimmed = inner.trim();
|
|
6520
|
+
if (trimmed.startsWith("<function=")) return scan_qwen_functions(inner_start, inner);
|
|
6521
|
+
if (trimmed.startsWith("{")) {
|
|
6522
|
+
const candidate = scan_hermes_block(text, inner_start, span);
|
|
6523
|
+
return candidate === void 0 ? [] : [candidate];
|
|
6524
|
+
}
|
|
6525
|
+
return [];
|
|
6526
|
+
}
|
|
6527
|
+
/**
|
|
6184
6528
|
* Scan `<tool_call>` blocks for hermes and qwen_xml candidates.
|
|
6185
6529
|
*
|
|
6186
6530
|
* Every block extent (accepted, rejected, or unterminated) is returned as a
|
|
@@ -6202,28 +6546,13 @@ function scan_tool_call_blocks(text) {
|
|
|
6202
6546
|
break;
|
|
6203
6547
|
}
|
|
6204
6548
|
const block_end = close + 12;
|
|
6205
|
-
|
|
6549
|
+
const span = {
|
|
6206
6550
|
start: open,
|
|
6207
6551
|
end: block_end
|
|
6208
|
-
}
|
|
6552
|
+
};
|
|
6553
|
+
masks.push(span);
|
|
6209
6554
|
const inner_start = open + 11;
|
|
6210
|
-
|
|
6211
|
-
const trimmed = inner.trim();
|
|
6212
|
-
if (trimmed.startsWith("<function=")) candidates.push(...scan_qwen_functions(inner_start, inner));
|
|
6213
|
-
else if (trimmed.startsWith("{")) {
|
|
6214
|
-
const brace = text.indexOf("{", inner_start);
|
|
6215
|
-
const scanned = brace === -1 ? void 0 : scan_balanced_json(text, brace);
|
|
6216
|
-
const shaped = scanned === void 0 ? void 0 : call_shape(scanned.value);
|
|
6217
|
-
if (shaped !== void 0) candidates.push({
|
|
6218
|
-
name: shaped.name,
|
|
6219
|
-
args: shaped.args,
|
|
6220
|
-
format: "hermes",
|
|
6221
|
-
span: {
|
|
6222
|
-
start: open,
|
|
6223
|
-
end: block_end
|
|
6224
|
-
}
|
|
6225
|
-
});
|
|
6226
|
-
}
|
|
6555
|
+
candidates.push(...scan_block_candidates(text, inner_start, close, span));
|
|
6227
6556
|
pos = block_end;
|
|
6228
6557
|
}
|
|
6229
6558
|
return {
|
|
@@ -6653,6 +6982,482 @@ async function apply_prepare_step(config, step_index) {
|
|
|
6653
6982
|
return replacement;
|
|
6654
6983
|
}
|
|
6655
6984
|
/**
|
|
6985
|
+
* Assemble the loop's return value from the accumulated steps and records.
|
|
6986
|
+
*/
|
|
6987
|
+
function loop_result(acc, text, finish_reason, max_steps_reached) {
|
|
6988
|
+
return {
|
|
6989
|
+
text,
|
|
6990
|
+
steps: acc.steps,
|
|
6991
|
+
tool_calls: acc.tool_calls,
|
|
6992
|
+
finish_reason,
|
|
6993
|
+
max_steps_reached
|
|
6994
|
+
};
|
|
6995
|
+
}
|
|
6996
|
+
/**
|
|
6997
|
+
* Invoke the transport seam once for one turn, bracketed by request_sent /
|
|
6998
|
+
* response_received events. A thrown invoke (or prepare_step) closes the
|
|
6999
|
+
* step span with the error before rethrowing.
|
|
7000
|
+
*/
|
|
7001
|
+
async function invoke_turn(config, step_index, step_span) {
|
|
7002
|
+
try {
|
|
7003
|
+
const request_messages = await apply_prepare_step(config, step_index);
|
|
7004
|
+
config.trajectory?.record({
|
|
7005
|
+
kind: "request_sent",
|
|
7006
|
+
step_index
|
|
7007
|
+
});
|
|
7008
|
+
const turn = await config.invoke_once({
|
|
7009
|
+
step_index,
|
|
7010
|
+
messages: request_messages,
|
|
7011
|
+
tools: config.tools,
|
|
7012
|
+
abort: config.abort,
|
|
7013
|
+
stream: config.stream
|
|
7014
|
+
});
|
|
7015
|
+
config.trajectory?.record({
|
|
7016
|
+
kind: "response_received",
|
|
7017
|
+
step_index,
|
|
7018
|
+
output_tokens: turn.usage.output_tokens,
|
|
7019
|
+
finish_reason: turn.finish_reason
|
|
7020
|
+
});
|
|
7021
|
+
return turn;
|
|
7022
|
+
} catch (err) {
|
|
7023
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
7024
|
+
end_step_span(config.trajectory, step_span, { error: message });
|
|
7025
|
+
throw err;
|
|
7026
|
+
}
|
|
7027
|
+
}
|
|
7028
|
+
/**
|
|
7029
|
+
* Salvage text-embedded tool calls from an eligible turn.
|
|
7030
|
+
*
|
|
7031
|
+
* A turn that "stopped" with plain text may hold a call the runtime failed
|
|
7032
|
+
* to parse into tool_calls; salvage before deciding the step ends the loop.
|
|
7033
|
+
* History gets the stripped text + structured parts (raw markup in history
|
|
7034
|
+
* would teach the model the text format works, and a tool result without a
|
|
7035
|
+
* matching call part is rejected by OpenAI-compatible APIs);
|
|
7036
|
+
* StepRecord.text keeps the raw text for debugging. Returns the native view
|
|
7037
|
+
* unchanged when the turn is ineligible or nothing salvages.
|
|
7038
|
+
*/
|
|
7039
|
+
async function salvage_turn(config, turn, tool_map, step_index) {
|
|
7040
|
+
const native = {
|
|
7041
|
+
calls: turn.tool_calls,
|
|
7042
|
+
history_text: turn.text,
|
|
7043
|
+
formats: /* @__PURE__ */ new Map(),
|
|
7044
|
+
outcome: void 0
|
|
7045
|
+
};
|
|
7046
|
+
const budget = config.salvage_budget;
|
|
7047
|
+
if (!(turn.tool_calls.length === 0 && (turn.finish_reason === "stop" || turn.finish_reason === "length") && config.tools.length > 0 && budget !== void 0 && budget.remaining > 0)) return native;
|
|
7048
|
+
const outcome = await salvage_tool_calls(turn.text, tool_map);
|
|
7049
|
+
if (outcome === void 0) return native;
|
|
7050
|
+
budget.remaining -= 1;
|
|
7051
|
+
const formats = /* @__PURE__ */ new Map();
|
|
7052
|
+
const calls = outcome.calls.map((c, n) => {
|
|
7053
|
+
const id = `salvaged_${step_index}_${n}`;
|
|
7054
|
+
formats.set(id, c.format);
|
|
7055
|
+
return {
|
|
7056
|
+
id,
|
|
7057
|
+
name: c.name,
|
|
7058
|
+
input: c.input
|
|
7059
|
+
};
|
|
7060
|
+
});
|
|
7061
|
+
record_tool_call_salvaged(config.trajectory, {
|
|
7062
|
+
step_index,
|
|
7063
|
+
calls: calls.map((c) => ({
|
|
7064
|
+
tool_call_id: c.id,
|
|
7065
|
+
name: c.name,
|
|
7066
|
+
format: formats.get(c.id) ?? "json"
|
|
7067
|
+
})),
|
|
7068
|
+
raw_text: turn.text
|
|
7069
|
+
});
|
|
7070
|
+
await dispatch_salvaged_call_chunks(config, calls, step_index);
|
|
7071
|
+
return {
|
|
7072
|
+
calls,
|
|
7073
|
+
history_text: outcome.stripped_text,
|
|
7074
|
+
formats,
|
|
7075
|
+
outcome
|
|
7076
|
+
};
|
|
7077
|
+
}
|
|
7078
|
+
/**
|
|
7079
|
+
* Mirror the native stream for salvaged calls, which emits start/end for
|
|
7080
|
+
* every call the model attempted, including ones the per-step clamp later
|
|
7081
|
+
* drops.
|
|
7082
|
+
*/
|
|
7083
|
+
async function dispatch_salvaged_call_chunks(config, calls, step_index) {
|
|
7084
|
+
if (config.dispatch_chunk === void 0) return;
|
|
7085
|
+
for (const c of calls) {
|
|
7086
|
+
await config.dispatch_chunk({
|
|
7087
|
+
kind: "tool_call_start",
|
|
7088
|
+
id: c.id,
|
|
7089
|
+
name: c.name,
|
|
7090
|
+
step_index
|
|
7091
|
+
});
|
|
7092
|
+
await config.dispatch_chunk({
|
|
7093
|
+
kind: "tool_call_end",
|
|
7094
|
+
id: c.id,
|
|
7095
|
+
input: c.input,
|
|
7096
|
+
step_index
|
|
7097
|
+
});
|
|
7098
|
+
}
|
|
7099
|
+
}
|
|
7100
|
+
/**
|
|
7101
|
+
* Apply `max_tool_calls_per_step` to a turn's calls, native and salvaged
|
|
7102
|
+
* alike. Dropped calls never reach history; the model re-issues them on a
|
|
7103
|
+
* later turn if it still wants them.
|
|
7104
|
+
*/
|
|
7105
|
+
function clamp_calls(config, calls, step_index) {
|
|
7106
|
+
const cap = config.max_tool_calls_per_step;
|
|
7107
|
+
if (cap === void 0 || calls.length <= cap) return {
|
|
7108
|
+
kept: calls,
|
|
7109
|
+
dropped: []
|
|
7110
|
+
};
|
|
7111
|
+
const kept = calls.slice(0, cap);
|
|
7112
|
+
const dropped = calls.slice(cap);
|
|
7113
|
+
record_tool_calls_dropped(config.trajectory, {
|
|
7114
|
+
step_index,
|
|
7115
|
+
max_tool_calls_per_step: cap,
|
|
7116
|
+
kept: kept.length,
|
|
7117
|
+
dropped: dropped.map((d) => ({
|
|
7118
|
+
tool_call_id: d.id,
|
|
7119
|
+
name: d.name
|
|
7120
|
+
}))
|
|
7121
|
+
});
|
|
7122
|
+
return {
|
|
7123
|
+
kept,
|
|
7124
|
+
dropped
|
|
7125
|
+
};
|
|
7126
|
+
}
|
|
7127
|
+
/**
|
|
7128
|
+
* Record a call that resolved to an error on the feed-back path: the error
|
|
7129
|
+
* record, the fed-back error tool_result message, the tool_call trajectory
|
|
7130
|
+
* event, and the stream chunk, in that order. Throw-policy paths raise
|
|
7131
|
+
* before reaching here.
|
|
7132
|
+
*/
|
|
7133
|
+
async function record_failed_call(ctx, call, message, timing) {
|
|
7134
|
+
const record = {
|
|
7135
|
+
id: call.id,
|
|
7136
|
+
name: call.name,
|
|
7137
|
+
input: call.input,
|
|
7138
|
+
error: { message },
|
|
7139
|
+
duration_ms: timing.duration_ms,
|
|
7140
|
+
started_at: timing.started_at
|
|
7141
|
+
};
|
|
7142
|
+
ctx.records.push(record);
|
|
7143
|
+
ctx.all_records.push(record);
|
|
7144
|
+
ctx.feed.push(build_tool_result_message(call.id, call.name, { error: message }));
|
|
7145
|
+
record_tool_call(ctx.config.trajectory, {
|
|
7146
|
+
step_index: ctx.step_index,
|
|
7147
|
+
name: call.name,
|
|
7148
|
+
tool_call_id: call.id,
|
|
7149
|
+
input: call.input,
|
|
7150
|
+
duration_ms: timing.duration_ms,
|
|
7151
|
+
error: { message }
|
|
7152
|
+
});
|
|
7153
|
+
await dispatch_tool_result_chunk(ctx.config.dispatch_chunk, ctx.step_index, call.id, void 0, { message });
|
|
7154
|
+
}
|
|
7155
|
+
/**
|
|
7156
|
+
* Record a call that never reached execution (the final-turn max_steps skip,
|
|
7157
|
+
* the per-step-cap drop): an error record and a tool_result stream chunk,
|
|
7158
|
+
* but no tool_call trajectory event, no execution, and no fed-back tool
|
|
7159
|
+
* message.
|
|
7160
|
+
*/
|
|
7161
|
+
async function record_unexecuted_call(ctx, call, message) {
|
|
7162
|
+
const record = {
|
|
7163
|
+
id: call.id,
|
|
7164
|
+
name: call.name,
|
|
7165
|
+
input: call.input,
|
|
7166
|
+
error: { message },
|
|
7167
|
+
duration_ms: 0,
|
|
7168
|
+
started_at: Date.now()
|
|
7169
|
+
};
|
|
7170
|
+
ctx.records.push(record);
|
|
7171
|
+
ctx.all_records.push(record);
|
|
7172
|
+
await dispatch_tool_result_chunk(ctx.config.dispatch_chunk, ctx.step_index, call.id, void 0, { message });
|
|
7173
|
+
}
|
|
7174
|
+
/**
|
|
7175
|
+
* Look up the named tool and validate the raw input against its schema.
|
|
7176
|
+
* Returns undefined when the failure was recorded on the feed-back path;
|
|
7177
|
+
* throws under 'throw' policy for an unknown tool.
|
|
7178
|
+
*/
|
|
7179
|
+
async function resolve_tool_and_input(ctx, raw_call) {
|
|
7180
|
+
const tool = ctx.tool_map.get(raw_call.name);
|
|
7181
|
+
if (tool === void 0) {
|
|
7182
|
+
const err_message = `unknown tool '${raw_call.name}'`;
|
|
7183
|
+
if (ctx.config.tool_error_policy === "throw") {
|
|
7184
|
+
const thrown = new tool_error(err_message, {
|
|
7185
|
+
tool_name: raw_call.name,
|
|
7186
|
+
tool_call_id: raw_call.id,
|
|
7187
|
+
cause: new Error(err_message)
|
|
7188
|
+
});
|
|
7189
|
+
end_step_span(ctx.config.trajectory, ctx.step_span, { error: err_message });
|
|
7190
|
+
throw thrown;
|
|
7191
|
+
}
|
|
7192
|
+
await record_failed_call(ctx, raw_call, err_message, {
|
|
7193
|
+
duration_ms: 0,
|
|
7194
|
+
started_at: Date.now()
|
|
7195
|
+
});
|
|
7196
|
+
return;
|
|
7197
|
+
}
|
|
7198
|
+
const validation = await validate_tool_input(tool, raw_call.input);
|
|
7199
|
+
if (!validation.ok) {
|
|
7200
|
+
await record_failed_call(ctx, raw_call, validation.message, {
|
|
7201
|
+
duration_ms: 0,
|
|
7202
|
+
started_at: Date.now()
|
|
7203
|
+
});
|
|
7204
|
+
return;
|
|
7205
|
+
}
|
|
7206
|
+
return {
|
|
7207
|
+
tool,
|
|
7208
|
+
input: validation.value
|
|
7209
|
+
};
|
|
7210
|
+
}
|
|
7211
|
+
/**
|
|
7212
|
+
* Resolve whether the call may execute (see request_approval). A denial
|
|
7213
|
+
* either throws ('throw' policy) or records the denied call and returns
|
|
7214
|
+
* false; an abort or handler rejection closes the step span and rethrows.
|
|
7215
|
+
*/
|
|
7216
|
+
async function approve_call(ctx, tool, input, raw_call) {
|
|
7217
|
+
let approved;
|
|
7218
|
+
try {
|
|
7219
|
+
approved = await request_approval(tool, input, ctx.step_index, raw_call.id, ctx.config.abort, ctx.config.on_tool_approval, ctx.config.trajectory);
|
|
7220
|
+
} catch (err) {
|
|
7221
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
7222
|
+
end_step_span(ctx.config.trajectory, ctx.step_span, { error: message });
|
|
7223
|
+
throw err;
|
|
7224
|
+
}
|
|
7225
|
+
if (approved) return true;
|
|
7226
|
+
const denied_message = "tool_approval_denied";
|
|
7227
|
+
if (ctx.config.tool_error_policy === "throw") {
|
|
7228
|
+
const thrown = new tool_approval_denied_error(`tool '${tool.name}' approval denied`, {
|
|
7229
|
+
tool_name: tool.name,
|
|
7230
|
+
step_index: ctx.step_index,
|
|
7231
|
+
tool_call_id: raw_call.id
|
|
7232
|
+
});
|
|
7233
|
+
end_step_span(ctx.config.trajectory, ctx.step_span, { error: denied_message });
|
|
7234
|
+
throw thrown;
|
|
7235
|
+
}
|
|
7236
|
+
await record_failed_call(ctx, {
|
|
7237
|
+
id: raw_call.id,
|
|
7238
|
+
name: tool.name,
|
|
7239
|
+
input
|
|
7240
|
+
}, denied_message, {
|
|
7241
|
+
duration_ms: 0,
|
|
7242
|
+
started_at: Date.now()
|
|
7243
|
+
});
|
|
7244
|
+
return false;
|
|
7245
|
+
}
|
|
7246
|
+
/**
|
|
7247
|
+
* Execute an approved call and record the outcome: a success record with
|
|
7248
|
+
* output (flipping terminal_fired for an ends_turn tool), or the error path
|
|
7249
|
+
* via record_execute_failure.
|
|
7250
|
+
*/
|
|
7251
|
+
async function execute_approved_call(ctx, tool, input, raw_call) {
|
|
7252
|
+
const started_at = Date.now();
|
|
7253
|
+
const tool_ctx = {
|
|
7254
|
+
abort: ctx.config.abort,
|
|
7255
|
+
tool_call_id: raw_call.id,
|
|
7256
|
+
step_index: ctx.step_index,
|
|
7257
|
+
...ctx.config.trajectory !== void 0 ? { trajectory: ctx.config.trajectory } : {}
|
|
7258
|
+
};
|
|
7259
|
+
let output;
|
|
7260
|
+
let err_message;
|
|
7261
|
+
let thrown;
|
|
7262
|
+
try {
|
|
7263
|
+
const execute = tool.execute;
|
|
7264
|
+
const maybe = execute(input, tool_ctx);
|
|
7265
|
+
output = maybe instanceof Promise ? await maybe : maybe;
|
|
7266
|
+
} catch (err) {
|
|
7267
|
+
thrown = err;
|
|
7268
|
+
err_message = serialize_error(err);
|
|
7269
|
+
}
|
|
7270
|
+
const duration_ms = Date.now() - started_at;
|
|
7271
|
+
if (thrown !== void 0) {
|
|
7272
|
+
await record_execute_failure(ctx, tool, raw_call, input, {
|
|
7273
|
+
thrown,
|
|
7274
|
+
err_message,
|
|
7275
|
+
duration_ms,
|
|
7276
|
+
started_at
|
|
7277
|
+
});
|
|
7278
|
+
return;
|
|
7279
|
+
}
|
|
7280
|
+
const record = {
|
|
7281
|
+
id: raw_call.id,
|
|
7282
|
+
name: tool.name,
|
|
7283
|
+
input,
|
|
7284
|
+
output,
|
|
7285
|
+
duration_ms,
|
|
7286
|
+
started_at
|
|
7287
|
+
};
|
|
7288
|
+
ctx.records.push(record);
|
|
7289
|
+
ctx.all_records.push(record);
|
|
7290
|
+
if (tool.ends_turn === true) ctx.terminal_fired = true;
|
|
7291
|
+
ctx.feed.push(build_tool_result_message(raw_call.id, tool.name, output ?? ""));
|
|
7292
|
+
record_tool_call(ctx.config.trajectory, {
|
|
7293
|
+
step_index: ctx.step_index,
|
|
7294
|
+
name: tool.name,
|
|
7295
|
+
tool_call_id: raw_call.id,
|
|
7296
|
+
input,
|
|
7297
|
+
duration_ms
|
|
7298
|
+
});
|
|
7299
|
+
await dispatch_tool_result_chunk(ctx.config.dispatch_chunk, ctx.step_index, raw_call.id, output);
|
|
7300
|
+
}
|
|
7301
|
+
/**
|
|
7302
|
+
* Handle a throwing execute: an abort in flight or 'throw' policy closes
|
|
7303
|
+
* the step span and throws; feed-back policy records the failure with the
|
|
7304
|
+
* measured timing.
|
|
7305
|
+
*/
|
|
7306
|
+
async function record_execute_failure(ctx, tool, raw_call, input, failure) {
|
|
7307
|
+
if (ctx.config.abort.aborted) {
|
|
7308
|
+
const abort_err = new aborted_error("aborted", {
|
|
7309
|
+
reason: ctx.config.abort.reason,
|
|
7310
|
+
step_index: ctx.step_index,
|
|
7311
|
+
tool_call_in_flight: {
|
|
7312
|
+
id: raw_call.id,
|
|
7313
|
+
name: tool.name
|
|
7314
|
+
}
|
|
7315
|
+
});
|
|
7316
|
+
end_step_span(ctx.config.trajectory, ctx.step_span, { error: "aborted" });
|
|
7317
|
+
throw abort_err;
|
|
7318
|
+
}
|
|
7319
|
+
if (ctx.config.tool_error_policy === "throw") {
|
|
7320
|
+
const wrapped = new tool_error(`tool '${tool.name}' failed: ${failure.err_message ?? "unknown"}`, {
|
|
7321
|
+
tool_name: tool.name,
|
|
7322
|
+
tool_call_id: raw_call.id,
|
|
7323
|
+
cause: failure.thrown
|
|
7324
|
+
});
|
|
7325
|
+
end_step_span(ctx.config.trajectory, ctx.step_span, { error: failure.err_message ?? "tool error" });
|
|
7326
|
+
throw wrapped;
|
|
7327
|
+
}
|
|
7328
|
+
await record_failed_call(ctx, {
|
|
7329
|
+
id: raw_call.id,
|
|
7330
|
+
name: tool.name,
|
|
7331
|
+
input
|
|
7332
|
+
}, failure.err_message ?? "unknown", {
|
|
7333
|
+
duration_ms: failure.duration_ms,
|
|
7334
|
+
started_at: failure.started_at
|
|
7335
|
+
});
|
|
7336
|
+
}
|
|
7337
|
+
/**
|
|
7338
|
+
* Run one raw tool call through the guard chain — final-turn skip, tool
|
|
7339
|
+
* lookup, input validation, approval — then execute it. Each guard either
|
|
7340
|
+
* records the failure (feed-back policy) and stops, or throws (throw
|
|
7341
|
+
* policy / abort).
|
|
7342
|
+
*/
|
|
7343
|
+
async function process_call(ctx, raw_call) {
|
|
7344
|
+
throw_if_aborted_in_flight(ctx.config.abort, ctx.step_index, {
|
|
7345
|
+
id: raw_call.id,
|
|
7346
|
+
name: raw_call.name
|
|
7347
|
+
});
|
|
7348
|
+
if (ctx.would_exceed_after && ctx.tool_map.get(raw_call.name)?.ends_turn !== true) {
|
|
7349
|
+
await record_unexecuted_call(ctx, raw_call, "max_steps_exceeded_before_execution");
|
|
7350
|
+
return;
|
|
7351
|
+
}
|
|
7352
|
+
const resolved = await resolve_tool_and_input(ctx, raw_call);
|
|
7353
|
+
if (resolved === void 0) return;
|
|
7354
|
+
if (!await approve_call(ctx, resolved.tool, resolved.input, raw_call)) return;
|
|
7355
|
+
throw_if_aborted_in_flight(ctx.config.abort, ctx.step_index, {
|
|
7356
|
+
id: raw_call.id,
|
|
7357
|
+
name: resolved.tool.name
|
|
7358
|
+
});
|
|
7359
|
+
await execute_approved_call(ctx, resolved.tool, resolved.input, raw_call);
|
|
7360
|
+
}
|
|
7361
|
+
/**
|
|
7362
|
+
* Stamp salvage provenance onto the records of calls that came from text
|
|
7363
|
+
* salvage rather than structured output.
|
|
7364
|
+
*/
|
|
7365
|
+
function mark_salvaged(records, formats) {
|
|
7366
|
+
for (const r of records) {
|
|
7367
|
+
const format = formats.get(r.id);
|
|
7368
|
+
if (format !== void 0) {
|
|
7369
|
+
r.salvaged = true;
|
|
7370
|
+
r.salvaged_format = format;
|
|
7371
|
+
}
|
|
7372
|
+
}
|
|
7373
|
+
}
|
|
7374
|
+
/**
|
|
7375
|
+
* Emit a tool_result for every resolved call in this step (success carries
|
|
7376
|
+
* output, feed-back failures carry error). Throw-policy and aborted calls
|
|
7377
|
+
* exit before here and surface loudly as a thrown error instead.
|
|
7378
|
+
*/
|
|
7379
|
+
function emit_tool_results(config, step_index, records) {
|
|
7380
|
+
for (const r of records) record_tool_result(config.trajectory, {
|
|
7381
|
+
step_index,
|
|
7382
|
+
name: r.name,
|
|
7383
|
+
tool_call_id: r.id,
|
|
7384
|
+
duration_ms: r.duration_ms,
|
|
7385
|
+
...r.error !== void 0 ? { error: r.error } : { output: r.output }
|
|
7386
|
+
});
|
|
7387
|
+
}
|
|
7388
|
+
/**
|
|
7389
|
+
* The finish reason a tool-call step reports. A salvaged step reports
|
|
7390
|
+
* 'tool_calls': downstream consumers see the same shape a native tool turn
|
|
7391
|
+
* produces; the salvaged flags carry provenance. A terminal step reports
|
|
7392
|
+
* 'tool_calls' (it genuinely made calls); the loop-level finish_reason
|
|
7393
|
+
* 'stop' is the separate signal that generation ended.
|
|
7394
|
+
*/
|
|
7395
|
+
function resolve_turn_finish_reason(ctx, salvaged, turn_finish) {
|
|
7396
|
+
if (ctx.terminal_fired) return "tool_calls";
|
|
7397
|
+
if (ctx.would_exceed_after) return "max_steps";
|
|
7398
|
+
if (salvaged) return "tool_calls";
|
|
7399
|
+
return turn_finish;
|
|
7400
|
+
}
|
|
7401
|
+
/**
|
|
7402
|
+
* Build one StepRecord — cost derived and recorded, provider_reported
|
|
7403
|
+
* passed through — push it, notify on_finish_step, and close the step span.
|
|
7404
|
+
*/
|
|
7405
|
+
function push_step_record(config, acc, step_index, turn, tool_calls, finish_reason, step_span) {
|
|
7406
|
+
const record = {
|
|
7407
|
+
index: step_index,
|
|
7408
|
+
text: turn.text,
|
|
7409
|
+
tool_calls,
|
|
7410
|
+
usage: turn.usage,
|
|
7411
|
+
finish_reason
|
|
7412
|
+
};
|
|
7413
|
+
const breakdown = compute_and_record_cost(config, step_index, turn.usage);
|
|
7414
|
+
if (breakdown !== void 0) record.cost = breakdown;
|
|
7415
|
+
if (turn.provider_reported !== void 0) record.provider_reported = turn.provider_reported;
|
|
7416
|
+
acc.steps.push(record);
|
|
7417
|
+
if (config.on_finish_step !== void 0) config.on_finish_step(record);
|
|
7418
|
+
end_step_span(config.trajectory, step_span, {
|
|
7419
|
+
usage: turn.usage,
|
|
7420
|
+
finish_reason
|
|
7421
|
+
});
|
|
7422
|
+
}
|
|
7423
|
+
/**
|
|
7424
|
+
* Run one loop iteration: invoke the turn, salvage and clamp its calls,
|
|
7425
|
+
* execute them sequentially, and record the step. Returns the finished
|
|
7426
|
+
* ToolLoopResult when this step ends the loop (no calls, terminal tool,
|
|
7427
|
+
* max_steps), undefined when the loop should continue.
|
|
7428
|
+
*/
|
|
7429
|
+
async function run_loop_step(config, tool_map, acc, step_index) {
|
|
7430
|
+
const step_span = start_step_span(config.trajectory, step_index);
|
|
7431
|
+
const turn = await invoke_turn(config, step_index, step_span);
|
|
7432
|
+
const view = await salvage_turn(config, turn, tool_map, step_index);
|
|
7433
|
+
const { kept, dropped } = clamp_calls(config, view.calls, step_index);
|
|
7434
|
+
config.messages.push(build_assistant_message(view.history_text, kept));
|
|
7435
|
+
if (kept.length === 0) {
|
|
7436
|
+
push_step_record(config, acc, step_index, turn, [], turn.finish_reason, step_span);
|
|
7437
|
+
return loop_result(acc, turn.text, turn.finish_reason, false);
|
|
7438
|
+
}
|
|
7439
|
+
const ctx = {
|
|
7440
|
+
config,
|
|
7441
|
+
tool_map,
|
|
7442
|
+
step_index,
|
|
7443
|
+
step_span,
|
|
7444
|
+
would_exceed_after: step_index + 1 >= config.max_steps,
|
|
7445
|
+
records: [],
|
|
7446
|
+
all_records: acc.tool_calls,
|
|
7447
|
+
feed: [],
|
|
7448
|
+
terminal_fired: false
|
|
7449
|
+
};
|
|
7450
|
+
for (const raw_call of kept) await process_call(ctx, raw_call);
|
|
7451
|
+
for (const d of dropped) await record_unexecuted_call(ctx, d, "dropped_max_tool_calls_per_step");
|
|
7452
|
+
mark_salvaged(ctx.records, view.formats);
|
|
7453
|
+
emit_tool_results(config, step_index, ctx.records);
|
|
7454
|
+
for (const m of ctx.feed) config.messages.push(m);
|
|
7455
|
+
const turn_finish_reason = resolve_turn_finish_reason(ctx, view.outcome !== void 0, turn.finish_reason);
|
|
7456
|
+
push_step_record(config, acc, step_index, turn, ctx.records, turn_finish_reason, step_span);
|
|
7457
|
+
if (ctx.terminal_fired) return loop_result(acc, turn.text, "stop", false);
|
|
7458
|
+
if (ctx.would_exceed_after) return loop_result(acc, turn.text, "max_steps", true);
|
|
7459
|
+
}
|
|
7460
|
+
/**
|
|
6656
7461
|
* Run the model-turn / tool-execution loop to completion.
|
|
6657
7462
|
*
|
|
6658
7463
|
* Each iteration invokes the transport seam once, salvages text-embedded
|
|
@@ -6661,403 +7466,20 @@ async function apply_prepare_step(config, step_index) {
|
|
|
6661
7466
|
* until the model stops, a terminal tool fires, or `max_steps` is reached.
|
|
6662
7467
|
*/
|
|
6663
7468
|
async function run_tool_loop(config) {
|
|
6664
|
-
const
|
|
6665
|
-
|
|
6666
|
-
|
|
6667
|
-
|
|
6668
|
-
let step_index = config.step_index_start;
|
|
6669
|
-
let max_steps_reached = false;
|
|
7469
|
+
const acc = {
|
|
7470
|
+
steps: [],
|
|
7471
|
+
tool_calls: []
|
|
7472
|
+
};
|
|
6670
7473
|
const tool_map = /* @__PURE__ */ new Map();
|
|
6671
7474
|
for (const t of config.tools) tool_map.set(t.name, t);
|
|
7475
|
+
let step_index = config.step_index_start;
|
|
6672
7476
|
while (true) {
|
|
6673
7477
|
throw_if_aborted(config.abort, step_index);
|
|
6674
|
-
if (step_index >= config.max_steps)
|
|
6675
|
-
|
|
6676
|
-
|
|
6677
|
-
break;
|
|
6678
|
-
}
|
|
6679
|
-
const step_span = start_step_span(config.trajectory, step_index);
|
|
6680
|
-
let turn;
|
|
6681
|
-
try {
|
|
6682
|
-
const request_messages = await apply_prepare_step(config, step_index);
|
|
6683
|
-
config.trajectory?.record({
|
|
6684
|
-
kind: "request_sent",
|
|
6685
|
-
step_index
|
|
6686
|
-
});
|
|
6687
|
-
turn = await config.invoke_once({
|
|
6688
|
-
step_index,
|
|
6689
|
-
messages: request_messages,
|
|
6690
|
-
tools: config.tools,
|
|
6691
|
-
abort: config.abort,
|
|
6692
|
-
stream: config.stream
|
|
6693
|
-
});
|
|
6694
|
-
config.trajectory?.record({
|
|
6695
|
-
kind: "response_received",
|
|
6696
|
-
step_index,
|
|
6697
|
-
output_tokens: turn.usage.output_tokens,
|
|
6698
|
-
finish_reason: turn.finish_reason
|
|
6699
|
-
});
|
|
6700
|
-
} catch (err) {
|
|
6701
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
6702
|
-
end_step_span(config.trajectory, step_span, { error: message });
|
|
6703
|
-
throw err;
|
|
6704
|
-
}
|
|
6705
|
-
let effective_calls = turn.tool_calls;
|
|
6706
|
-
let history_text = turn.text;
|
|
6707
|
-
let salvage;
|
|
6708
|
-
const salvaged_formats = /* @__PURE__ */ new Map();
|
|
6709
|
-
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) {
|
|
6710
|
-
salvage = await salvage_tool_calls(turn.text, tool_map);
|
|
6711
|
-
if (salvage !== void 0) {
|
|
6712
|
-
config.salvage_budget.remaining -= 1;
|
|
6713
|
-
history_text = salvage.stripped_text;
|
|
6714
|
-
effective_calls = salvage.calls.map((c, n) => {
|
|
6715
|
-
const id = `salvaged_${step_index}_${n}`;
|
|
6716
|
-
salvaged_formats.set(id, c.format);
|
|
6717
|
-
return {
|
|
6718
|
-
id,
|
|
6719
|
-
name: c.name,
|
|
6720
|
-
input: c.input
|
|
6721
|
-
};
|
|
6722
|
-
});
|
|
6723
|
-
record_tool_call_salvaged(config.trajectory, {
|
|
6724
|
-
step_index,
|
|
6725
|
-
calls: effective_calls.map((c) => ({
|
|
6726
|
-
tool_call_id: c.id,
|
|
6727
|
-
name: c.name,
|
|
6728
|
-
format: salvaged_formats.get(c.id) ?? "json"
|
|
6729
|
-
})),
|
|
6730
|
-
raw_text: turn.text
|
|
6731
|
-
});
|
|
6732
|
-
if (config.dispatch_chunk !== void 0) for (const c of effective_calls) {
|
|
6733
|
-
await config.dispatch_chunk({
|
|
6734
|
-
kind: "tool_call_start",
|
|
6735
|
-
id: c.id,
|
|
6736
|
-
name: c.name,
|
|
6737
|
-
step_index
|
|
6738
|
-
});
|
|
6739
|
-
await config.dispatch_chunk({
|
|
6740
|
-
kind: "tool_call_end",
|
|
6741
|
-
id: c.id,
|
|
6742
|
-
input: c.input,
|
|
6743
|
-
step_index
|
|
6744
|
-
});
|
|
6745
|
-
}
|
|
6746
|
-
}
|
|
6747
|
-
}
|
|
6748
|
-
let dropped_calls = [];
|
|
6749
|
-
const per_step_cap = config.max_tool_calls_per_step;
|
|
6750
|
-
if (per_step_cap !== void 0 && effective_calls.length > per_step_cap) {
|
|
6751
|
-
dropped_calls = effective_calls.slice(per_step_cap);
|
|
6752
|
-
effective_calls = effective_calls.slice(0, per_step_cap);
|
|
6753
|
-
record_tool_calls_dropped(config.trajectory, {
|
|
6754
|
-
step_index,
|
|
6755
|
-
max_tool_calls_per_step: per_step_cap,
|
|
6756
|
-
kept: effective_calls.length,
|
|
6757
|
-
dropped: dropped_calls.map((d) => ({
|
|
6758
|
-
tool_call_id: d.id,
|
|
6759
|
-
name: d.name
|
|
6760
|
-
}))
|
|
6761
|
-
});
|
|
6762
|
-
}
|
|
6763
|
-
const step_tool_records = [];
|
|
6764
|
-
const assistant_message = build_assistant_message(history_text, effective_calls);
|
|
6765
|
-
config.messages.push(assistant_message);
|
|
6766
|
-
if (effective_calls.length === 0) {
|
|
6767
|
-
text = turn.text;
|
|
6768
|
-
finish_reason = turn.finish_reason;
|
|
6769
|
-
const record_step = {
|
|
6770
|
-
index: step_index,
|
|
6771
|
-
text: turn.text,
|
|
6772
|
-
tool_calls: [],
|
|
6773
|
-
usage: turn.usage,
|
|
6774
|
-
finish_reason: turn.finish_reason
|
|
6775
|
-
};
|
|
6776
|
-
const breakdown = compute_and_record_cost(config, step_index, turn.usage);
|
|
6777
|
-
if (breakdown !== void 0) record_step.cost = breakdown;
|
|
6778
|
-
if (turn.provider_reported !== void 0) record_step.provider_reported = turn.provider_reported;
|
|
6779
|
-
steps.push(record_step);
|
|
6780
|
-
if (config.on_finish_step !== void 0) config.on_finish_step(record_step);
|
|
6781
|
-
end_step_span(config.trajectory, step_span, {
|
|
6782
|
-
usage: turn.usage,
|
|
6783
|
-
finish_reason: turn.finish_reason
|
|
6784
|
-
});
|
|
6785
|
-
break;
|
|
6786
|
-
}
|
|
6787
|
-
const would_exceed_after = step_index + 1 >= config.max_steps;
|
|
6788
|
-
const tool_results_to_feed = [];
|
|
6789
|
-
let terminal_fired = false;
|
|
6790
|
-
for (const raw_call of effective_calls) {
|
|
6791
|
-
throw_if_aborted_in_flight(config.abort, step_index, {
|
|
6792
|
-
id: raw_call.id,
|
|
6793
|
-
name: raw_call.name
|
|
6794
|
-
});
|
|
6795
|
-
if (would_exceed_after && tool_map.get(raw_call.name)?.ends_turn !== true) {
|
|
6796
|
-
const record = {
|
|
6797
|
-
id: raw_call.id,
|
|
6798
|
-
name: raw_call.name,
|
|
6799
|
-
input: raw_call.input,
|
|
6800
|
-
error: { message: "max_steps_exceeded_before_execution" },
|
|
6801
|
-
duration_ms: 0,
|
|
6802
|
-
started_at: Date.now()
|
|
6803
|
-
};
|
|
6804
|
-
step_tool_records.push(record);
|
|
6805
|
-
tool_calls_all.push(record);
|
|
6806
|
-
await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, void 0, { message: "max_steps_exceeded_before_execution" });
|
|
6807
|
-
continue;
|
|
6808
|
-
}
|
|
6809
|
-
const tool = tool_map.get(raw_call.name);
|
|
6810
|
-
if (tool === void 0) {
|
|
6811
|
-
const err_message = `unknown tool '${raw_call.name}'`;
|
|
6812
|
-
if (config.tool_error_policy === "throw") {
|
|
6813
|
-
const thrown = new tool_error(err_message, {
|
|
6814
|
-
tool_name: raw_call.name,
|
|
6815
|
-
tool_call_id: raw_call.id,
|
|
6816
|
-
cause: new Error(err_message)
|
|
6817
|
-
});
|
|
6818
|
-
end_step_span(config.trajectory, step_span, { error: err_message });
|
|
6819
|
-
throw thrown;
|
|
6820
|
-
}
|
|
6821
|
-
const record = {
|
|
6822
|
-
id: raw_call.id,
|
|
6823
|
-
name: raw_call.name,
|
|
6824
|
-
input: raw_call.input,
|
|
6825
|
-
error: { message: err_message },
|
|
6826
|
-
duration_ms: 0,
|
|
6827
|
-
started_at: Date.now()
|
|
6828
|
-
};
|
|
6829
|
-
step_tool_records.push(record);
|
|
6830
|
-
tool_calls_all.push(record);
|
|
6831
|
-
tool_results_to_feed.push(build_tool_result_message(raw_call.id, raw_call.name, { error: err_message }));
|
|
6832
|
-
record_tool_call(config.trajectory, {
|
|
6833
|
-
step_index,
|
|
6834
|
-
name: raw_call.name,
|
|
6835
|
-
tool_call_id: raw_call.id,
|
|
6836
|
-
input: raw_call.input,
|
|
6837
|
-
duration_ms: 0,
|
|
6838
|
-
error: { message: err_message }
|
|
6839
|
-
});
|
|
6840
|
-
await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, void 0, { message: err_message });
|
|
6841
|
-
continue;
|
|
6842
|
-
}
|
|
6843
|
-
const validation = await validate_tool_input(tool, raw_call.input);
|
|
6844
|
-
if (!validation.ok) {
|
|
6845
|
-
const record = {
|
|
6846
|
-
id: raw_call.id,
|
|
6847
|
-
name: raw_call.name,
|
|
6848
|
-
input: raw_call.input,
|
|
6849
|
-
error: { message: validation.message },
|
|
6850
|
-
duration_ms: 0,
|
|
6851
|
-
started_at: Date.now()
|
|
6852
|
-
};
|
|
6853
|
-
step_tool_records.push(record);
|
|
6854
|
-
tool_calls_all.push(record);
|
|
6855
|
-
tool_results_to_feed.push(build_tool_result_message(raw_call.id, raw_call.name, { error: validation.message }));
|
|
6856
|
-
record_tool_call(config.trajectory, {
|
|
6857
|
-
step_index,
|
|
6858
|
-
name: raw_call.name,
|
|
6859
|
-
tool_call_id: raw_call.id,
|
|
6860
|
-
input: raw_call.input,
|
|
6861
|
-
duration_ms: 0,
|
|
6862
|
-
error: { message: validation.message }
|
|
6863
|
-
});
|
|
6864
|
-
await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, void 0, { message: validation.message });
|
|
6865
|
-
continue;
|
|
6866
|
-
}
|
|
6867
|
-
let approved;
|
|
6868
|
-
try {
|
|
6869
|
-
approved = await request_approval(tool, validation.value, step_index, raw_call.id, config.abort, config.on_tool_approval, config.trajectory);
|
|
6870
|
-
} catch (err) {
|
|
6871
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
6872
|
-
end_step_span(config.trajectory, step_span, { error: message });
|
|
6873
|
-
throw err;
|
|
6874
|
-
}
|
|
6875
|
-
if (!approved) {
|
|
6876
|
-
const denied_message = "tool_approval_denied";
|
|
6877
|
-
if (config.tool_error_policy === "throw") {
|
|
6878
|
-
const thrown = new tool_approval_denied_error(`tool '${tool.name}' approval denied`, {
|
|
6879
|
-
tool_name: tool.name,
|
|
6880
|
-
step_index,
|
|
6881
|
-
tool_call_id: raw_call.id
|
|
6882
|
-
});
|
|
6883
|
-
end_step_span(config.trajectory, step_span, { error: denied_message });
|
|
6884
|
-
throw thrown;
|
|
6885
|
-
}
|
|
6886
|
-
const record = {
|
|
6887
|
-
id: raw_call.id,
|
|
6888
|
-
name: tool.name,
|
|
6889
|
-
input: validation.value,
|
|
6890
|
-
error: { message: denied_message },
|
|
6891
|
-
duration_ms: 0,
|
|
6892
|
-
started_at: Date.now()
|
|
6893
|
-
};
|
|
6894
|
-
step_tool_records.push(record);
|
|
6895
|
-
tool_calls_all.push(record);
|
|
6896
|
-
tool_results_to_feed.push(build_tool_result_message(raw_call.id, tool.name, { error: denied_message }));
|
|
6897
|
-
record_tool_call(config.trajectory, {
|
|
6898
|
-
step_index,
|
|
6899
|
-
name: tool.name,
|
|
6900
|
-
tool_call_id: raw_call.id,
|
|
6901
|
-
input: validation.value,
|
|
6902
|
-
duration_ms: 0,
|
|
6903
|
-
error: { message: denied_message }
|
|
6904
|
-
});
|
|
6905
|
-
await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, void 0, { message: denied_message });
|
|
6906
|
-
continue;
|
|
6907
|
-
}
|
|
6908
|
-
throw_if_aborted_in_flight(config.abort, step_index, {
|
|
6909
|
-
id: raw_call.id,
|
|
6910
|
-
name: tool.name
|
|
6911
|
-
});
|
|
6912
|
-
const started_at = Date.now();
|
|
6913
|
-
const tool_ctx = {
|
|
6914
|
-
abort: config.abort,
|
|
6915
|
-
tool_call_id: raw_call.id,
|
|
6916
|
-
step_index,
|
|
6917
|
-
...config.trajectory !== void 0 ? { trajectory: config.trajectory } : {}
|
|
6918
|
-
};
|
|
6919
|
-
let output;
|
|
6920
|
-
let err_message;
|
|
6921
|
-
let thrown;
|
|
6922
|
-
try {
|
|
6923
|
-
const execute = tool.execute;
|
|
6924
|
-
const maybe = execute(validation.value, tool_ctx);
|
|
6925
|
-
output = maybe instanceof Promise ? await maybe : maybe;
|
|
6926
|
-
} catch (err) {
|
|
6927
|
-
thrown = err;
|
|
6928
|
-
err_message = serialize_error(err);
|
|
6929
|
-
}
|
|
6930
|
-
const duration_ms = Date.now() - started_at;
|
|
6931
|
-
if (thrown !== void 0) {
|
|
6932
|
-
if (config.abort.aborted) {
|
|
6933
|
-
const abort_err = new aborted_error("aborted", {
|
|
6934
|
-
reason: config.abort.reason,
|
|
6935
|
-
step_index,
|
|
6936
|
-
tool_call_in_flight: {
|
|
6937
|
-
id: raw_call.id,
|
|
6938
|
-
name: tool.name
|
|
6939
|
-
}
|
|
6940
|
-
});
|
|
6941
|
-
end_step_span(config.trajectory, step_span, { error: "aborted" });
|
|
6942
|
-
throw abort_err;
|
|
6943
|
-
}
|
|
6944
|
-
if (config.tool_error_policy === "throw") {
|
|
6945
|
-
const wrapped = new tool_error(`tool '${tool.name}' failed: ${err_message ?? "unknown"}`, {
|
|
6946
|
-
tool_name: tool.name,
|
|
6947
|
-
tool_call_id: raw_call.id,
|
|
6948
|
-
cause: thrown
|
|
6949
|
-
});
|
|
6950
|
-
end_step_span(config.trajectory, step_span, { error: err_message ?? "tool error" });
|
|
6951
|
-
throw wrapped;
|
|
6952
|
-
}
|
|
6953
|
-
const record = {
|
|
6954
|
-
id: raw_call.id,
|
|
6955
|
-
name: tool.name,
|
|
6956
|
-
input: validation.value,
|
|
6957
|
-
error: { message: err_message ?? "unknown" },
|
|
6958
|
-
duration_ms,
|
|
6959
|
-
started_at
|
|
6960
|
-
};
|
|
6961
|
-
step_tool_records.push(record);
|
|
6962
|
-
tool_calls_all.push(record);
|
|
6963
|
-
tool_results_to_feed.push(build_tool_result_message(raw_call.id, tool.name, { error: err_message ?? "unknown" }));
|
|
6964
|
-
record_tool_call(config.trajectory, {
|
|
6965
|
-
step_index,
|
|
6966
|
-
name: tool.name,
|
|
6967
|
-
tool_call_id: raw_call.id,
|
|
6968
|
-
input: validation.value,
|
|
6969
|
-
duration_ms,
|
|
6970
|
-
error: { message: err_message ?? "unknown" }
|
|
6971
|
-
});
|
|
6972
|
-
await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, void 0, { message: err_message ?? "unknown" });
|
|
6973
|
-
continue;
|
|
6974
|
-
}
|
|
6975
|
-
const record = {
|
|
6976
|
-
id: raw_call.id,
|
|
6977
|
-
name: tool.name,
|
|
6978
|
-
input: validation.value,
|
|
6979
|
-
output,
|
|
6980
|
-
duration_ms,
|
|
6981
|
-
started_at
|
|
6982
|
-
};
|
|
6983
|
-
step_tool_records.push(record);
|
|
6984
|
-
tool_calls_all.push(record);
|
|
6985
|
-
if (tool.ends_turn === true) terminal_fired = true;
|
|
6986
|
-
tool_results_to_feed.push(build_tool_result_message(raw_call.id, tool.name, output ?? ""));
|
|
6987
|
-
record_tool_call(config.trajectory, {
|
|
6988
|
-
step_index,
|
|
6989
|
-
name: tool.name,
|
|
6990
|
-
tool_call_id: raw_call.id,
|
|
6991
|
-
input: validation.value,
|
|
6992
|
-
duration_ms
|
|
6993
|
-
});
|
|
6994
|
-
await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, output);
|
|
6995
|
-
}
|
|
6996
|
-
for (const d of dropped_calls) {
|
|
6997
|
-
const record = {
|
|
6998
|
-
id: d.id,
|
|
6999
|
-
name: d.name,
|
|
7000
|
-
input: d.input,
|
|
7001
|
-
error: { message: "dropped_max_tool_calls_per_step" },
|
|
7002
|
-
duration_ms: 0,
|
|
7003
|
-
started_at: Date.now()
|
|
7004
|
-
};
|
|
7005
|
-
step_tool_records.push(record);
|
|
7006
|
-
tool_calls_all.push(record);
|
|
7007
|
-
await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, d.id, void 0, { message: "dropped_max_tool_calls_per_step" });
|
|
7008
|
-
}
|
|
7009
|
-
for (const r of step_tool_records) {
|
|
7010
|
-
const format = salvaged_formats.get(r.id);
|
|
7011
|
-
if (format !== void 0) {
|
|
7012
|
-
r.salvaged = true;
|
|
7013
|
-
r.salvaged_format = format;
|
|
7014
|
-
}
|
|
7015
|
-
}
|
|
7016
|
-
for (const r of step_tool_records) record_tool_result(config.trajectory, {
|
|
7017
|
-
step_index,
|
|
7018
|
-
name: r.name,
|
|
7019
|
-
tool_call_id: r.id,
|
|
7020
|
-
duration_ms: r.duration_ms,
|
|
7021
|
-
...r.error !== void 0 ? { error: r.error } : { output: r.output }
|
|
7022
|
-
});
|
|
7023
|
-
for (const m of tool_results_to_feed) config.messages.push(m);
|
|
7024
|
-
const turn_finish_reason = terminal_fired ? "tool_calls" : would_exceed_after ? "max_steps" : salvage !== void 0 ? "tool_calls" : turn.finish_reason;
|
|
7025
|
-
const step_record = {
|
|
7026
|
-
index: step_index,
|
|
7027
|
-
text: turn.text,
|
|
7028
|
-
tool_calls: step_tool_records,
|
|
7029
|
-
usage: turn.usage,
|
|
7030
|
-
finish_reason: turn_finish_reason
|
|
7031
|
-
};
|
|
7032
|
-
const breakdown = compute_and_record_cost(config, step_index, turn.usage);
|
|
7033
|
-
if (breakdown !== void 0) step_record.cost = breakdown;
|
|
7034
|
-
if (turn.provider_reported !== void 0) step_record.provider_reported = turn.provider_reported;
|
|
7035
|
-
steps.push(step_record);
|
|
7036
|
-
if (config.on_finish_step !== void 0) config.on_finish_step(step_record);
|
|
7037
|
-
end_step_span(config.trajectory, step_span, {
|
|
7038
|
-
usage: turn.usage,
|
|
7039
|
-
finish_reason: turn_finish_reason
|
|
7040
|
-
});
|
|
7041
|
-
if (terminal_fired) {
|
|
7042
|
-
finish_reason = "stop";
|
|
7043
|
-
text = turn.text;
|
|
7044
|
-
break;
|
|
7045
|
-
}
|
|
7046
|
-
if (would_exceed_after) {
|
|
7047
|
-
max_steps_reached = true;
|
|
7048
|
-
finish_reason = "max_steps";
|
|
7049
|
-
text = turn.text;
|
|
7050
|
-
break;
|
|
7051
|
-
}
|
|
7478
|
+
if (step_index >= config.max_steps) return loop_result(acc, "", "max_steps", true);
|
|
7479
|
+
const finished = await run_loop_step(config, tool_map, acc, step_index);
|
|
7480
|
+
if (finished !== void 0) return finished;
|
|
7052
7481
|
step_index += 1;
|
|
7053
7482
|
}
|
|
7054
|
-
return {
|
|
7055
|
-
text,
|
|
7056
|
-
steps,
|
|
7057
|
-
tool_calls: tool_calls_all,
|
|
7058
|
-
finish_reason,
|
|
7059
|
-
max_steps_reached
|
|
7060
|
-
};
|
|
7061
7483
|
}
|
|
7062
7484
|
//#endregion
|
|
7063
7485
|
//#region src/engine/generate.ts
|
|
@@ -7095,20 +7517,76 @@ function build_initial_messages(opts) {
|
|
|
7095
7517
|
* empty (provider APIs reject an empty messages array).
|
|
7096
7518
|
*/
|
|
7097
7519
|
function split_leading_system_messages(messages) {
|
|
7098
|
-
|
|
7099
|
-
|
|
7100
|
-
|
|
7101
|
-
|
|
7102
|
-
|
|
7103
|
-
|
|
7104
|
-
|
|
7105
|
-
|
|
7106
|
-
|
|
7107
|
-
|
|
7108
|
-
|
|
7109
|
-
|
|
7110
|
-
|
|
7111
|
-
|
|
7520
|
+
return split_leading_system_run(messages, (m) => m.content);
|
|
7521
|
+
}
|
|
7522
|
+
const RETRY_CLASSIFIED_KINDS = /* @__PURE__ */ new Set([
|
|
7523
|
+
"rate_limit",
|
|
7524
|
+
"provider_5xx",
|
|
7525
|
+
"network",
|
|
7526
|
+
"timeout"
|
|
7527
|
+
]);
|
|
7528
|
+
const NETWORK_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
7529
|
+
"ECONNRESET",
|
|
7530
|
+
"ETIMEDOUT",
|
|
7531
|
+
"ENOTFOUND",
|
|
7532
|
+
"ECONNREFUSED"
|
|
7533
|
+
]);
|
|
7534
|
+
/**
|
|
7535
|
+
* True when `err` already carries a `kind` this layer treats as retryable, so
|
|
7536
|
+
* the classifier passes it through untouched.
|
|
7537
|
+
*/
|
|
7538
|
+
function has_retry_kind(err) {
|
|
7539
|
+
const kind = Reflect.get(err, "kind");
|
|
7540
|
+
return typeof kind === "string" && RETRY_CLASSIFIED_KINDS.has(kind);
|
|
7541
|
+
}
|
|
7542
|
+
/**
|
|
7543
|
+
* Read the HTTP status off either `statusCode` or `status`, in that order.
|
|
7544
|
+
*/
|
|
7545
|
+
function resolve_error_status(err) {
|
|
7546
|
+
const status = Reflect.get(err, "statusCode");
|
|
7547
|
+
if (typeof status === "number") return status;
|
|
7548
|
+
const status_alt = Reflect.get(err, "status");
|
|
7549
|
+
return typeof status_alt === "number" ? status_alt : void 0;
|
|
7550
|
+
}
|
|
7551
|
+
/**
|
|
7552
|
+
* Copy the error's `message` onto a classification record when it is a string,
|
|
7553
|
+
* returning the same record for chaining.
|
|
7554
|
+
*/
|
|
7555
|
+
function with_error_message(err, out) {
|
|
7556
|
+
const message = Reflect.get(err, "message");
|
|
7557
|
+
if (typeof message === "string") out["message"] = message;
|
|
7558
|
+
return out;
|
|
7559
|
+
}
|
|
7560
|
+
/**
|
|
7561
|
+
* Parse a `Retry-After` value off the error's `responseHeaders`, if present.
|
|
7562
|
+
*/
|
|
7563
|
+
function retry_after_from_error(err) {
|
|
7564
|
+
const headers = Reflect.get(err, "responseHeaders");
|
|
7565
|
+
if (headers === null || typeof headers !== "object") return void 0;
|
|
7566
|
+
const hv = Reflect.get(headers, "retry-after");
|
|
7567
|
+
return typeof hv === "string" ? parse_retry_after(hv) : void 0;
|
|
7568
|
+
}
|
|
7569
|
+
/**
|
|
7570
|
+
* Build the `rate_limit` classification for a 429, attaching the message and a
|
|
7571
|
+
* parsed `Retry-After` when either is present.
|
|
7572
|
+
*/
|
|
7573
|
+
function classify_rate_limit(err) {
|
|
7574
|
+
const out = with_error_message(err, {
|
|
7575
|
+
kind: "rate_limit",
|
|
7576
|
+
status: 429
|
|
7577
|
+
});
|
|
7578
|
+
const retry_after_ms = retry_after_from_error(err);
|
|
7579
|
+
if (retry_after_ms !== void 0) out["retry_after_ms"] = retry_after_ms;
|
|
7580
|
+
return out;
|
|
7581
|
+
}
|
|
7582
|
+
/**
|
|
7583
|
+
* Build the `network` classification when `err.code` is a known network error
|
|
7584
|
+
* code, or `undefined` when it is not.
|
|
7585
|
+
*/
|
|
7586
|
+
function classify_network_error(err) {
|
|
7587
|
+
const code = Reflect.get(err, "code");
|
|
7588
|
+
if (typeof code !== "string" || !NETWORK_ERROR_CODES.has(code)) return void 0;
|
|
7589
|
+
return with_error_message(err, { kind: "network" });
|
|
7112
7590
|
}
|
|
7113
7591
|
/**
|
|
7114
7592
|
* Classify a thrown provider error into the shape `retry_with_policy`
|
|
@@ -7118,49 +7596,19 @@ function split_leading_system_messages(messages) {
|
|
|
7118
7596
|
* Otherwise inspects `statusCode`/`status`: 429 becomes `rate_limit`
|
|
7119
7597
|
* (reading `Retry-After` off `responseHeaders` when present), 5xx becomes
|
|
7120
7598
|
* `provider_5xx`, and a known `ECONNRESET`/`ETIMEDOUT`/`ENOTFOUND`/
|
|
7121
|
-
* `ECONNREFUSED` error code becomes `network`. Anything else passes through
|
|
7122
|
-
* unclassified, which `retry_with_policy` treats as non-retryable.
|
|
7123
|
-
*/
|
|
7124
|
-
function classify_provider_error(err) {
|
|
7125
|
-
if (err === null || typeof err !== "object") return err;
|
|
7126
|
-
|
|
7127
|
-
|
|
7128
|
-
|
|
7129
|
-
|
|
7130
|
-
|
|
7131
|
-
|
|
7132
|
-
|
|
7133
|
-
|
|
7134
|
-
if (headers !== null && typeof headers === "object") {
|
|
7135
|
-
const hv = Reflect.get(headers, "retry-after");
|
|
7136
|
-
if (typeof hv === "string") retry_after_ms = parse_retry_after(hv);
|
|
7137
|
-
}
|
|
7138
|
-
const message = Reflect.get(err, "message");
|
|
7139
|
-
const out = {
|
|
7140
|
-
kind: "rate_limit",
|
|
7141
|
-
status: 429
|
|
7142
|
-
};
|
|
7143
|
-
if (typeof message === "string") out["message"] = message;
|
|
7144
|
-
if (retry_after_ms !== void 0) out["retry_after_ms"] = retry_after_ms;
|
|
7145
|
-
return out;
|
|
7146
|
-
}
|
|
7147
|
-
if (resolved_status !== void 0 && resolved_status >= 500 && resolved_status < 600) {
|
|
7148
|
-
const message = Reflect.get(err, "message");
|
|
7149
|
-
const out = {
|
|
7150
|
-
kind: "provider_5xx",
|
|
7151
|
-
status: resolved_status
|
|
7152
|
-
};
|
|
7153
|
-
if (typeof message === "string") out["message"] = message;
|
|
7154
|
-
return out;
|
|
7155
|
-
}
|
|
7156
|
-
const code = Reflect.get(err, "code");
|
|
7157
|
-
if (code === "ECONNRESET" || code === "ETIMEDOUT" || code === "ENOTFOUND" || code === "ECONNREFUSED") {
|
|
7158
|
-
const message = Reflect.get(err, "message");
|
|
7159
|
-
const out = { kind: "network" };
|
|
7160
|
-
if (typeof message === "string") out["message"] = message;
|
|
7161
|
-
return out;
|
|
7162
|
-
}
|
|
7163
|
-
return err;
|
|
7599
|
+
* `ECONNREFUSED` error code becomes `network`. Anything else passes through
|
|
7600
|
+
* unclassified, which `retry_with_policy` treats as non-retryable.
|
|
7601
|
+
*/
|
|
7602
|
+
function classify_provider_error(err) {
|
|
7603
|
+
if (err === null || typeof err !== "object") return err;
|
|
7604
|
+
if (has_retry_kind(err)) return err;
|
|
7605
|
+
const status = resolve_error_status(err);
|
|
7606
|
+
if (status === 429) return classify_rate_limit(err);
|
|
7607
|
+
if (status !== void 0 && status >= 500 && status < 600) return with_error_message(err, {
|
|
7608
|
+
kind: "provider_5xx",
|
|
7609
|
+
status
|
|
7610
|
+
});
|
|
7611
|
+
return classify_network_error(err) ?? err;
|
|
7164
7612
|
}
|
|
7165
7613
|
/**
|
|
7166
7614
|
* Compose the per-attempt turn signal: the user's abort OR'd with a fresh
|
|
@@ -7306,31 +7754,26 @@ function build_native_invoke(cfg) {
|
|
|
7306
7754
|
};
|
|
7307
7755
|
}
|
|
7308
7756
|
/**
|
|
7309
|
-
* Resolve
|
|
7310
|
-
*
|
|
7757
|
+
* Resolve the provider name: per-call, then the engine default, then the sole
|
|
7758
|
+
* configured adapter, then 'anthropic'.
|
|
7759
|
+
*/
|
|
7760
|
+
function resolve_provider(opts_in, engine) {
|
|
7761
|
+
const sole_provider = engine.adapters.size === 1 ? [...engine.adapters.keys()][0] : void 0;
|
|
7762
|
+
return opts_in.provider ?? engine.default_provider ?? sole_provider ?? "anthropic";
|
|
7763
|
+
}
|
|
7764
|
+
/**
|
|
7765
|
+
* Resolve the model/provider pair for one call and freeze the resolved
|
|
7766
|
+
* options view.
|
|
7311
7767
|
*
|
|
7312
|
-
*
|
|
7313
|
-
*
|
|
7314
|
-
*
|
|
7315
|
-
*
|
|
7316
|
-
* defaults, checks the adapter supports every capability the call actually
|
|
7317
|
-
* uses (schema, tools, streaming), builds the `InvokeOnce` closure for the
|
|
7318
|
-
* resolved transport, and drives it through `run_tool_loop`. When a schema
|
|
7319
|
-
* is set and the model's output fails validation, it appends a repair
|
|
7320
|
-
* message and re-invokes the loop, until the response parses, repair
|
|
7321
|
-
* attempts run out, or `max_steps` is reached. When a schema is set and the
|
|
7322
|
-
* loop finishes for any reason other than `'stop'` (a content filter, the
|
|
7323
|
-
* token limit, the step cap), no validated value can exist, so it throws
|
|
7324
|
-
* `incomplete_generation_error` carrying the finish reason, the raw text,
|
|
7325
|
-
* and the last `provider_reported` payload rather than returning unchecked
|
|
7326
|
-
* text. Without a schema those finish reasons return normally.
|
|
7768
|
+
* The model must resolve (per-call, then engine default) or the call cannot
|
|
7769
|
+
* proceed. Engine-default provider_options merge under per-call ones here so
|
|
7770
|
+
* every transport downstream sees a single already-merged view instead of
|
|
7771
|
+
* re-merging per branch.
|
|
7327
7772
|
*/
|
|
7328
|
-
|
|
7329
|
-
if (opts_in.abort?.aborted === true) throw new aborted_error("aborted", { reason: opts_in.abort.reason });
|
|
7773
|
+
function resolve_target(opts_in, engine) {
|
|
7330
7774
|
const resolved_model = opts_in.model ?? engine.default_model;
|
|
7331
7775
|
if (resolved_model === void 0) throw new model_required_error();
|
|
7332
|
-
const
|
|
7333
|
-
const resolved_provider = opts_in.provider ?? engine.default_provider ?? sole_provider ?? "anthropic";
|
|
7776
|
+
const resolved_provider = resolve_provider(opts_in, engine);
|
|
7334
7777
|
const merged_provider_options = merge_provider_options(engine.default_provider_options, opts_in.provider_options);
|
|
7335
7778
|
const opts = {
|
|
7336
7779
|
...opts_in,
|
|
@@ -7339,74 +7782,111 @@ async function generate(opts_in, engine) {
|
|
|
7339
7782
|
};
|
|
7340
7783
|
if (opts_in.system === void 0 && engine.default_system !== void 0) opts.system = engine.default_system;
|
|
7341
7784
|
if (merged_provider_options !== void 0) opts.provider_options = merged_provider_options;
|
|
7342
|
-
|
|
7343
|
-
|
|
7344
|
-
|
|
7785
|
+
return {
|
|
7786
|
+
opts,
|
|
7787
|
+
target: {
|
|
7788
|
+
provider: resolved_provider,
|
|
7789
|
+
model_id: resolved_model
|
|
7790
|
+
}
|
|
7345
7791
|
};
|
|
7346
|
-
|
|
7347
|
-
|
|
7348
|
-
|
|
7349
|
-
|
|
7350
|
-
|
|
7351
|
-
|
|
7352
|
-
|
|
7353
|
-
if (
|
|
7354
|
-
if (
|
|
7792
|
+
}
|
|
7793
|
+
/**
|
|
7794
|
+
* Reject a call that uses a capability the resolved adapter does not
|
|
7795
|
+
* implement. Checked up front, before any transport work, so the failure is a
|
|
7796
|
+
* typed capability error rather than a mid-call provider rejection.
|
|
7797
|
+
*/
|
|
7798
|
+
function assert_capabilities(adapter, provider, opts, tools_list, on_chunk_provided) {
|
|
7799
|
+
if (opts.schema !== void 0 && !adapter.supports("schema")) throw new provider_capability_error(provider, "schema");
|
|
7800
|
+
if (tools_list.length > 0 && !adapter.supports("tools")) throw new provider_capability_error(provider, "tools");
|
|
7801
|
+
if (on_chunk_provided && !adapter.supports("streaming")) throw new provider_capability_error(provider, "streaming");
|
|
7802
|
+
}
|
|
7803
|
+
/**
|
|
7804
|
+
* Resolve the per-turn knobs (effort, retry policy, turn timeout) against
|
|
7805
|
+
* engine defaults. Validated here, before any transport is built, so a bad
|
|
7806
|
+
* budget fails as a config error, never as a spurious timeout.
|
|
7807
|
+
*/
|
|
7808
|
+
function resolve_turn_config(opts, engine) {
|
|
7355
7809
|
const effort = opts.effort ?? engine.default_effort;
|
|
7356
7810
|
const retry_policy = opts.retry ?? engine.default_retry;
|
|
7357
7811
|
const turn_timeout_ms = opts.turn_timeout_ms ?? engine.default_turn_timeout_ms;
|
|
7358
7812
|
if (turn_timeout_ms !== void 0 && turn_timeout_ms <= 0) throw new engine_config_error("turn_timeout_ms must be > 0");
|
|
7359
|
-
|
|
7360
|
-
let invoke_once;
|
|
7361
|
-
if (adapter.kind === "ai_sdk") {
|
|
7362
|
-
const effort_translation = adapter.translate_effort(effort);
|
|
7363
|
-
if (effort !== "none" && effort_translation.effort_ignored) record_effort_ignored(trajectory, target.model_id);
|
|
7364
|
-
const combined_provider_options = merge_provider_options(effort_translation.provider_options, opts.provider_options);
|
|
7365
|
-
const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
|
|
7366
|
-
let ai_sdk_invoke_mod;
|
|
7367
|
-
try {
|
|
7368
|
-
ai_sdk_invoke_mod = await import("./invoke-D9pxbZcu.js");
|
|
7369
|
-
} catch (err) {
|
|
7370
|
-
throw missing_peer_error("ai", err);
|
|
7371
|
-
}
|
|
7372
|
-
const { create_ai_sdk_turn } = ai_sdk_invoke_mod;
|
|
7373
|
-
invoke_once = build_ai_sdk_invoke({
|
|
7374
|
-
invoke_turn: create_ai_sdk_turn({
|
|
7375
|
-
adapter,
|
|
7376
|
-
model_id: target.model_id,
|
|
7377
|
-
dispatcher,
|
|
7378
|
-
tools: tools_list,
|
|
7379
|
-
schema: opts.schema,
|
|
7380
|
-
provider_options,
|
|
7381
|
-
temperature: opts.temperature,
|
|
7382
|
-
max_tokens: opts.max_tokens,
|
|
7383
|
-
top_p: opts.top_p,
|
|
7384
|
-
telemetry: engine.default_ai_sdk_telemetry
|
|
7385
|
-
}),
|
|
7386
|
-
retry_policy,
|
|
7387
|
-
turn_timeout_ms
|
|
7388
|
-
});
|
|
7389
|
-
} else invoke_once = build_native_invoke({
|
|
7390
|
-
adapter,
|
|
7391
|
-
model_id: target.model_id,
|
|
7392
|
-
retry_policy,
|
|
7393
|
-
turn_timeout_ms,
|
|
7394
|
-
dispatcher,
|
|
7813
|
+
return {
|
|
7395
7814
|
effort,
|
|
7396
|
-
|
|
7397
|
-
|
|
7398
|
-
|
|
7399
|
-
|
|
7400
|
-
|
|
7401
|
-
|
|
7402
|
-
|
|
7815
|
+
retry_policy,
|
|
7816
|
+
turn_timeout_ms
|
|
7817
|
+
};
|
|
7818
|
+
}
|
|
7819
|
+
/**
|
|
7820
|
+
* Resolve the tool-call salvage budget. Returned as a holder rather than a
|
|
7821
|
+
* count: one holder per generate call so schema-repair re-invocations of the
|
|
7822
|
+
* loop cannot refill the salvage budget. undefined disables salvage.
|
|
7823
|
+
*/
|
|
7824
|
+
function resolve_salvage_budget(opts, engine) {
|
|
7825
|
+
const tool_call_repair_attempts = opts.tool_call_repair_attempts ?? engine.default_tool_call_repair_attempts ?? 0;
|
|
7826
|
+
if (tool_call_repair_attempts < 0) throw new engine_config_error("tool_call_repair_attempts must be >= 0");
|
|
7827
|
+
return tool_call_repair_attempts > 0 ? { remaining: tool_call_repair_attempts } : void 0;
|
|
7828
|
+
}
|
|
7829
|
+
/**
|
|
7830
|
+
* Resolve the loop-level limits and policies against engine defaults,
|
|
7831
|
+
* rejecting configurations the tool loop cannot honor.
|
|
7832
|
+
*/
|
|
7833
|
+
function resolve_loop_limits(opts, engine) {
|
|
7403
7834
|
const max_steps = opts.max_steps ?? engine.default_max_steps;
|
|
7404
7835
|
const tool_error_policy = opts.tool_error_policy ?? engine.default_tool_error_policy ?? "feed_back";
|
|
7405
7836
|
const schema_repair_attempts = opts.schema_repair_attempts ?? engine.default_schema_repair_attempts ?? 1;
|
|
7406
|
-
const
|
|
7407
|
-
if (tool_call_repair_attempts < 0) throw new engine_config_error("tool_call_repair_attempts must be >= 0");
|
|
7837
|
+
const salvage_budget = resolve_salvage_budget(opts, engine);
|
|
7408
7838
|
const max_tool_calls_per_step = opts.max_tool_calls_per_step ?? engine.default_max_tool_calls_per_step;
|
|
7409
7839
|
if (max_tool_calls_per_step !== void 0 && max_tool_calls_per_step < 1) throw new engine_config_error("max_tool_calls_per_step must be >= 1");
|
|
7840
|
+
return {
|
|
7841
|
+
max_steps,
|
|
7842
|
+
tool_error_policy,
|
|
7843
|
+
schema_repair_attempts,
|
|
7844
|
+
salvage_budget,
|
|
7845
|
+
max_tool_calls_per_step
|
|
7846
|
+
};
|
|
7847
|
+
}
|
|
7848
|
+
/**
|
|
7849
|
+
* Build the ai_sdk-transport InvokeOnce for one call: translate effort, merge
|
|
7850
|
+
* provider options, load the SDK seam, and wrap it in the engine-owned retry
|
|
7851
|
+
* via build_ai_sdk_invoke.
|
|
7852
|
+
*/
|
|
7853
|
+
async function build_ai_sdk_transport(cfg) {
|
|
7854
|
+
const { adapter, opts, effort } = cfg;
|
|
7855
|
+
const effort_translation = adapter.translate_effort(effort);
|
|
7856
|
+
if (effort !== "none" && effort_translation.effort_ignored) record_effort_ignored(cfg.trajectory, cfg.target.model_id);
|
|
7857
|
+
const combined_provider_options = merge_provider_options(effort_translation.provider_options, opts.provider_options);
|
|
7858
|
+
const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
|
|
7859
|
+
let ai_sdk_invoke_mod;
|
|
7860
|
+
try {
|
|
7861
|
+
ai_sdk_invoke_mod = await import("./invoke-BjrbCBSV.js");
|
|
7862
|
+
} catch (err) {
|
|
7863
|
+
throw missing_peer_error("ai", err);
|
|
7864
|
+
}
|
|
7865
|
+
const { create_ai_sdk_turn } = ai_sdk_invoke_mod;
|
|
7866
|
+
return build_ai_sdk_invoke({
|
|
7867
|
+
invoke_turn: create_ai_sdk_turn({
|
|
7868
|
+
adapter,
|
|
7869
|
+
model_id: cfg.target.model_id,
|
|
7870
|
+
dispatcher: cfg.dispatcher,
|
|
7871
|
+
tools: cfg.tools_list,
|
|
7872
|
+
schema: opts.schema,
|
|
7873
|
+
provider_options,
|
|
7874
|
+
temperature: opts.temperature,
|
|
7875
|
+
max_tokens: opts.max_tokens,
|
|
7876
|
+
top_p: opts.top_p,
|
|
7877
|
+
telemetry: cfg.telemetry
|
|
7878
|
+
}),
|
|
7879
|
+
retry_policy: cfg.retry_policy,
|
|
7880
|
+
turn_timeout_ms: cfg.turn_timeout_ms
|
|
7881
|
+
});
|
|
7882
|
+
}
|
|
7883
|
+
/**
|
|
7884
|
+
* Build the message list one generate call starts from. When a schema is set,
|
|
7885
|
+
* the JSON-only instruction folds into the existing system message (or is
|
|
7886
|
+
* prepended as one) so the model sees it exactly once, wherever the system
|
|
7887
|
+
* prompt came from.
|
|
7888
|
+
*/
|
|
7889
|
+
function build_prompt_messages(opts) {
|
|
7410
7890
|
const schema_prefix = opts.schema !== void 0 ? "You must respond with a single JSON value that conforms to the expected schema. Return ONLY the JSON value, with no markdown or commentary." : void 0;
|
|
7411
7891
|
const initial_messages = build_initial_messages(opts);
|
|
7412
7892
|
if (schema_prefix !== void 0) {
|
|
@@ -7422,100 +7902,240 @@ async function generate(opts_in, engine) {
|
|
|
7422
7902
|
content: schema_prefix
|
|
7423
7903
|
});
|
|
7424
7904
|
}
|
|
7425
|
-
|
|
7426
|
-
|
|
7427
|
-
|
|
7428
|
-
|
|
7429
|
-
|
|
7430
|
-
|
|
7431
|
-
|
|
7432
|
-
|
|
7433
|
-
|
|
7905
|
+
return initial_messages;
|
|
7906
|
+
}
|
|
7907
|
+
/**
|
|
7908
|
+
* Assemble the ToolLoopConfig base shared by every loop invocation of one
|
|
7909
|
+
* generate call. Everything here is per-call state: the mutable transcript,
|
|
7910
|
+
* the pricing lookup, and the pricing_missing dedup all live for exactly one
|
|
7911
|
+
* call, which is what lets schema-repair re-invocations share them. The
|
|
7912
|
+
* conditional spreads keep optional fields absent rather than undefined for
|
|
7913
|
+
* exactOptionalPropertyTypes.
|
|
7914
|
+
*/
|
|
7915
|
+
function build_loop_config(cfg) {
|
|
7916
|
+
const { opts, target, limits } = cfg;
|
|
7917
|
+
const abort = opts.abort ?? new AbortController().signal;
|
|
7434
7918
|
const resolve_pricing = () => {
|
|
7435
|
-
return
|
|
7919
|
+
return cfg.pricing[pricing_key(target.provider, target.model_id)];
|
|
7436
7920
|
};
|
|
7437
|
-
const dispatch_chunk = on_chunk_provided ? async (chunk) => {
|
|
7438
|
-
await dispatcher.dispatch(chunk);
|
|
7921
|
+
const dispatch_chunk = cfg.on_chunk_provided ? async (chunk) => {
|
|
7922
|
+
await cfg.dispatcher.dispatch(chunk);
|
|
7439
7923
|
} : void 0;
|
|
7440
|
-
|
|
7441
|
-
|
|
7924
|
+
return {
|
|
7925
|
+
invoke_once: cfg.invoke_once,
|
|
7926
|
+
messages: cfg.messages,
|
|
7927
|
+
tools: cfg.tools_list,
|
|
7928
|
+
max_steps: limits.max_steps,
|
|
7929
|
+
tool_error_policy: limits.tool_error_policy,
|
|
7930
|
+
abort,
|
|
7931
|
+
on_tool_approval: opts.on_tool_approval,
|
|
7932
|
+
trajectory: cfg.trajectory,
|
|
7933
|
+
stream: cfg.on_chunk_provided,
|
|
7934
|
+
dispatch_chunk,
|
|
7935
|
+
provider: target.provider,
|
|
7936
|
+
model_id: target.model_id,
|
|
7937
|
+
resolve_pricing,
|
|
7938
|
+
pricing_dedup: create_pricing_missing_dedup(cfg.trajectory),
|
|
7939
|
+
...limits.salvage_budget !== void 0 ? { salvage_budget: limits.salvage_budget } : {},
|
|
7940
|
+
...limits.max_tool_calls_per_step !== void 0 ? { max_tool_calls_per_step: limits.max_tool_calls_per_step } : {},
|
|
7941
|
+
...opts.prepare_step !== void 0 ? { prepare_step: opts.prepare_step } : {}
|
|
7942
|
+
};
|
|
7943
|
+
}
|
|
7944
|
+
/**
|
|
7945
|
+
* One schema-validation pass over the loop's final text. A successful parse
|
|
7946
|
+
* returns the value in a holder (a schema can legitimately validate to
|
|
7947
|
+
* undefined, which must stay distinguishable from "nothing parsed"). A failed
|
|
7948
|
+
* parse records the trajectory event first, then either throws (`can_repair`
|
|
7949
|
+
* false: the repair or step budget is spent) or hands back the repair message
|
|
7950
|
+
* to append before re-invoking the loop.
|
|
7951
|
+
*/
|
|
7952
|
+
async function parse_schema_step(cfg) {
|
|
7953
|
+
const parse = await parse_with_schema(cfg.schema, cfg.text);
|
|
7954
|
+
if (parse.ok) return { parsed: { value: parse.value } };
|
|
7955
|
+
record_schema_validation_failed(cfg.trajectory, {
|
|
7956
|
+
attempt: cfg.attempt,
|
|
7957
|
+
schema_issues: format_schema_issues(parse.issues),
|
|
7958
|
+
raw_text: cfg.text
|
|
7959
|
+
});
|
|
7960
|
+
if (!cfg.can_repair) throw_schema_validation(parse.issues, cfg.text);
|
|
7961
|
+
return { repair_message: build_repair_message(parse.issues) };
|
|
7962
|
+
}
|
|
7963
|
+
/**
|
|
7964
|
+
* Drive run_tool_loop to a final answer, re-invoking it for schema repair.
|
|
7965
|
+
*
|
|
7966
|
+
* Without a schema this is a single loop invocation. With one, the final text
|
|
7967
|
+
* must parse: a non-'stop' finish reason means no validated value can exist
|
|
7968
|
+
* (a content filter, the token limit, the step cap), so this throws
|
|
7969
|
+
* incomplete_generation_error rather than returning unchecked text; a failed
|
|
7970
|
+
* parse appends the repair message to the shared transcript and re-enters the
|
|
7971
|
+
* loop until the response parses, repair attempts run out, or max_steps is
|
|
7972
|
+
* reached. step_index_start advances by the accumulated step count so step
|
|
7973
|
+
* indices stay call-global across re-invocations.
|
|
7974
|
+
*/
|
|
7975
|
+
async function run_generate_loop(cfg) {
|
|
7976
|
+
const { base, schema } = cfg;
|
|
7442
7977
|
const steps_accum = [];
|
|
7443
7978
|
const tool_calls_accum = [];
|
|
7444
7979
|
let text = "";
|
|
7445
7980
|
let finish_reason = "stop";
|
|
7446
|
-
let repair_remaining = schema_repair_attempts;
|
|
7981
|
+
let repair_remaining = cfg.schema_repair_attempts;
|
|
7447
7982
|
let content_parsed;
|
|
7448
|
-
|
|
7449
|
-
|
|
7450
|
-
|
|
7451
|
-
|
|
7452
|
-
|
|
7453
|
-
|
|
7454
|
-
|
|
7455
|
-
|
|
7456
|
-
|
|
7457
|
-
|
|
7458
|
-
|
|
7459
|
-
|
|
7460
|
-
|
|
7461
|
-
|
|
7462
|
-
|
|
7463
|
-
|
|
7464
|
-
|
|
7465
|
-
|
|
7466
|
-
|
|
7467
|
-
|
|
7468
|
-
|
|
7469
|
-
...opts.prepare_step !== void 0 ? { prepare_step: opts.prepare_step } : {}
|
|
7470
|
-
});
|
|
7471
|
-
for (const s of loop_result.steps) steps_accum.push(s);
|
|
7472
|
-
for (const tc of loop_result.tool_calls) tool_calls_accum.push(tc);
|
|
7473
|
-
total_steps = steps_accum.length;
|
|
7474
|
-
text = loop_result.text;
|
|
7475
|
-
finish_reason = loop_result.finish_reason;
|
|
7476
|
-
if (opts.schema === void 0) break;
|
|
7477
|
-
if (finish_reason !== "stop") throw new incomplete_generation_error(finish_reason, text, last_provider_reported(steps_accum));
|
|
7478
|
-
const parse = await parse_with_schema(opts.schema, text);
|
|
7479
|
-
if (parse.ok) {
|
|
7480
|
-
content_parsed = { value: parse.value };
|
|
7481
|
-
break;
|
|
7482
|
-
}
|
|
7483
|
-
record_schema_validation_failed(trajectory, {
|
|
7484
|
-
attempt: repair_remaining === schema_repair_attempts ? "initial" : "repair",
|
|
7485
|
-
schema_issues: format_schema_issues(parse.issues),
|
|
7486
|
-
raw_text: text
|
|
7487
|
-
});
|
|
7488
|
-
if (repair_remaining <= 0 || total_steps >= max_steps) throw_schema_validation(parse.issues, text);
|
|
7489
|
-
repair_remaining -= 1;
|
|
7490
|
-
messages_mutable.push(build_repair_message(parse.issues));
|
|
7983
|
+
while (true) {
|
|
7984
|
+
const loop_result = await run_tool_loop({
|
|
7985
|
+
...base,
|
|
7986
|
+
step_index_start: steps_accum.length
|
|
7987
|
+
});
|
|
7988
|
+
for (const s of loop_result.steps) steps_accum.push(s);
|
|
7989
|
+
for (const tc of loop_result.tool_calls) tool_calls_accum.push(tc);
|
|
7990
|
+
text = loop_result.text;
|
|
7991
|
+
finish_reason = loop_result.finish_reason;
|
|
7992
|
+
if (schema === void 0) break;
|
|
7993
|
+
if (finish_reason !== "stop") throw new incomplete_generation_error(finish_reason, text, last_provider_reported(steps_accum));
|
|
7994
|
+
const schema_step = await parse_schema_step({
|
|
7995
|
+
schema,
|
|
7996
|
+
text,
|
|
7997
|
+
trajectory: base.trajectory,
|
|
7998
|
+
attempt: repair_remaining === cfg.schema_repair_attempts ? "initial" : "repair",
|
|
7999
|
+
can_repair: repair_remaining > 0 && steps_accum.length < base.max_steps
|
|
8000
|
+
});
|
|
8001
|
+
if ("parsed" in schema_step) {
|
|
8002
|
+
content_parsed = schema_step.parsed;
|
|
8003
|
+
break;
|
|
7491
8004
|
}
|
|
7492
|
-
|
|
7493
|
-
|
|
7494
|
-
|
|
7495
|
-
|
|
7496
|
-
|
|
7497
|
-
|
|
7498
|
-
|
|
7499
|
-
|
|
7500
|
-
|
|
7501
|
-
|
|
7502
|
-
|
|
7503
|
-
|
|
7504
|
-
|
|
7505
|
-
|
|
7506
|
-
|
|
7507
|
-
|
|
7508
|
-
|
|
7509
|
-
|
|
7510
|
-
|
|
8005
|
+
repair_remaining -= 1;
|
|
8006
|
+
base.messages.push(schema_step.repair_message);
|
|
8007
|
+
}
|
|
8008
|
+
return {
|
|
8009
|
+
steps: steps_accum,
|
|
8010
|
+
tool_calls: tool_calls_accum,
|
|
8011
|
+
text,
|
|
8012
|
+
finish_reason,
|
|
8013
|
+
content_parsed
|
|
8014
|
+
};
|
|
8015
|
+
}
|
|
8016
|
+
/**
|
|
8017
|
+
* Fold the loop outcome into the caller-facing GenerateResult: usage and cost
|
|
8018
|
+
* aggregate across every step (schema-repair re-invocations included), the
|
|
8019
|
+
* content is the parsed holder's value when a schema ran, and cost /
|
|
8020
|
+
* provider_reported attach only when a step actually reported them.
|
|
8021
|
+
*/
|
|
8022
|
+
function assemble_result(outcome, target) {
|
|
8023
|
+
const aggregated_usage = sum_usage(outcome.steps);
|
|
8024
|
+
const aggregated_cost = aggregate_cost(outcome.steps, target.provider);
|
|
8025
|
+
let final_content;
|
|
8026
|
+
if (outcome.content_parsed !== void 0) final_content = outcome.content_parsed.value;
|
|
8027
|
+
else final_content = outcome.text;
|
|
8028
|
+
const result = {
|
|
8029
|
+
content: final_content,
|
|
8030
|
+
tool_calls: outcome.tool_calls,
|
|
8031
|
+
steps: outcome.steps,
|
|
8032
|
+
usage: aggregated_usage,
|
|
8033
|
+
finish_reason: outcome.finish_reason,
|
|
8034
|
+
model_resolved: {
|
|
8035
|
+
provider: target.provider,
|
|
8036
|
+
model_id: target.model_id
|
|
8037
|
+
}
|
|
8038
|
+
};
|
|
8039
|
+
if (aggregated_cost !== void 0) result.cost = aggregated_cost;
|
|
8040
|
+
const aggregated_reported = last_provider_reported(outcome.steps);
|
|
8041
|
+
if (aggregated_reported !== void 0) result.provider_reported = aggregated_reported;
|
|
8042
|
+
return result;
|
|
8043
|
+
}
|
|
8044
|
+
/**
|
|
8045
|
+
* Resolve a `generate` call's options against engine defaults, run the tool
|
|
8046
|
+
* loop, and return the aggregated result.
|
|
8047
|
+
*
|
|
8048
|
+
* A provider whose adapter is `external` (`adapter.kind === 'external'`)
|
|
8049
|
+
* delegates the whole call to `adapter.generate` before any of the logic
|
|
8050
|
+
* below runs. For `ai_sdk` and native adapters, this function resolves
|
|
8051
|
+
* model, provider, system prompt, and `provider_options` against engine
|
|
8052
|
+
* defaults, checks the adapter supports every capability the call actually
|
|
8053
|
+
* uses (schema, tools, streaming), builds the `InvokeOnce` closure for the
|
|
8054
|
+
* resolved transport, and drives it through `run_tool_loop`. When a schema
|
|
8055
|
+
* is set and the model's output fails validation, it appends a repair
|
|
8056
|
+
* message and re-invokes the loop, until the response parses, repair
|
|
8057
|
+
* attempts run out, or `max_steps` is reached. When a schema is set and the
|
|
8058
|
+
* loop finishes for any reason other than `'stop'` (a content filter, the
|
|
8059
|
+
* token limit, the step cap), no validated value can exist, so it throws
|
|
8060
|
+
* `incomplete_generation_error` carrying the finish reason, the raw text,
|
|
8061
|
+
* and the last `provider_reported` payload rather than returning unchecked
|
|
8062
|
+
* text. Without a schema those finish reasons return normally.
|
|
8063
|
+
*/
|
|
8064
|
+
async function generate(opts_in, engine) {
|
|
8065
|
+
if (opts_in.abort?.aborted === true) throw new aborted_error("aborted", { reason: opts_in.abort.reason });
|
|
8066
|
+
const { opts, target } = resolve_target(opts_in, engine);
|
|
8067
|
+
const adapter = engine.adapters.get(target.provider);
|
|
8068
|
+
if (adapter === void 0) throw new provider_not_configured_error(target.provider);
|
|
8069
|
+
if (adapter.kind === "external") return adapter.generate(opts, target);
|
|
8070
|
+
const trajectory = with_timestamps(opts.trajectory);
|
|
8071
|
+
const on_chunk_provided = opts.on_chunk !== void 0;
|
|
8072
|
+
const tools_list = opts.tools ?? [];
|
|
8073
|
+
assert_capabilities(adapter, target.provider, opts, tools_list, on_chunk_provided);
|
|
8074
|
+
const turn = resolve_turn_config(opts, engine);
|
|
8075
|
+
const dispatcher = create_chunk_dispatcher(opts.on_chunk);
|
|
8076
|
+
let invoke_once;
|
|
8077
|
+
if (adapter.kind === "ai_sdk") invoke_once = await build_ai_sdk_transport({
|
|
8078
|
+
adapter,
|
|
8079
|
+
target,
|
|
8080
|
+
opts,
|
|
8081
|
+
effort: turn.effort,
|
|
8082
|
+
retry_policy: turn.retry_policy,
|
|
8083
|
+
turn_timeout_ms: turn.turn_timeout_ms,
|
|
8084
|
+
dispatcher,
|
|
8085
|
+
tools_list,
|
|
8086
|
+
trajectory,
|
|
8087
|
+
telemetry: engine.default_ai_sdk_telemetry
|
|
8088
|
+
});
|
|
8089
|
+
else invoke_once = build_native_invoke({
|
|
8090
|
+
adapter,
|
|
8091
|
+
model_id: target.model_id,
|
|
8092
|
+
retry_policy: turn.retry_policy,
|
|
8093
|
+
turn_timeout_ms: turn.turn_timeout_ms,
|
|
8094
|
+
dispatcher,
|
|
8095
|
+
effort: turn.effort,
|
|
8096
|
+
schema: opts.schema,
|
|
8097
|
+
provider_options: opts.provider_options,
|
|
8098
|
+
temperature: opts.temperature,
|
|
8099
|
+
max_tokens: opts.max_tokens,
|
|
8100
|
+
top_p: opts.top_p
|
|
8101
|
+
});
|
|
8102
|
+
const limits = resolve_loop_limits(opts, engine);
|
|
8103
|
+
const initial_messages = build_prompt_messages(opts);
|
|
8104
|
+
const generate_span = start_generate_span(trajectory, {
|
|
8105
|
+
model: target.model_id,
|
|
8106
|
+
provider: target.provider,
|
|
8107
|
+
model_id: target.model_id,
|
|
8108
|
+
has_tools: tools_list.length > 0,
|
|
8109
|
+
has_schema: opts.schema !== void 0,
|
|
8110
|
+
streaming: on_chunk_provided
|
|
8111
|
+
});
|
|
8112
|
+
const base = build_loop_config({
|
|
8113
|
+
opts,
|
|
8114
|
+
target,
|
|
8115
|
+
pricing: engine.pricing,
|
|
8116
|
+
invoke_once,
|
|
8117
|
+
messages: [...initial_messages],
|
|
8118
|
+
tools_list,
|
|
8119
|
+
trajectory,
|
|
8120
|
+
dispatcher,
|
|
8121
|
+
on_chunk_provided,
|
|
8122
|
+
limits
|
|
8123
|
+
});
|
|
8124
|
+
try {
|
|
8125
|
+
const outcome = await run_generate_loop({
|
|
8126
|
+
base,
|
|
8127
|
+
schema: opts.schema,
|
|
8128
|
+
schema_repair_attempts: limits.schema_repair_attempts
|
|
8129
|
+
});
|
|
8130
|
+
const result = assemble_result(outcome, target);
|
|
7511
8131
|
if (on_chunk_provided) await dispatcher.dispatch({
|
|
7512
8132
|
kind: "finish",
|
|
7513
|
-
finish_reason,
|
|
7514
|
-
usage:
|
|
8133
|
+
finish_reason: outcome.finish_reason,
|
|
8134
|
+
usage: result.usage
|
|
7515
8135
|
});
|
|
7516
8136
|
end_generate_span(trajectory, generate_span, {
|
|
7517
|
-
usage:
|
|
7518
|
-
finish_reason,
|
|
8137
|
+
usage: result.usage,
|
|
8138
|
+
finish_reason: outcome.finish_reason,
|
|
7519
8139
|
model_resolved: result.model_resolved
|
|
7520
8140
|
});
|
|
7521
8141
|
return result;
|
|
@@ -7544,46 +8164,57 @@ function last_provider_reported(steps) {
|
|
|
7544
8164
|
return steps.findLast((s) => s.provider_reported !== void 0)?.provider_reported;
|
|
7545
8165
|
}
|
|
7546
8166
|
/**
|
|
7547
|
-
*
|
|
7548
|
-
*
|
|
7549
|
-
*
|
|
7550
|
-
* Returns `undefined` if no step reports a cost, unless `provider` is free
|
|
7551
|
-
* (`FREE_PROVIDERS`) and at least one step ran; in that case it returns an
|
|
7552
|
-
* explicit all-zero breakdown instead of omitting cost entirely. The
|
|
7553
|
-
* optional fields (`cached_input_usd`, `cache_write_usd`, `reasoning_usd`)
|
|
7554
|
-
* are included only when at least one step reported them.
|
|
8167
|
+
* Fold every step's `cost` into a single {@link CostAccumulator}, skipping steps
|
|
8168
|
+
* that reported none. The required fields always sum; each optional field sums
|
|
8169
|
+
* and flips its `_present` flag only on the steps that carried it.
|
|
7555
8170
|
*/
|
|
7556
|
-
function
|
|
7557
|
-
|
|
7558
|
-
|
|
7559
|
-
|
|
7560
|
-
|
|
7561
|
-
|
|
7562
|
-
|
|
7563
|
-
|
|
7564
|
-
|
|
7565
|
-
|
|
7566
|
-
|
|
8171
|
+
function sum_step_costs(steps) {
|
|
8172
|
+
const acc = {
|
|
8173
|
+
any_present: false,
|
|
8174
|
+
total: 0,
|
|
8175
|
+
input: 0,
|
|
8176
|
+
output: 0,
|
|
8177
|
+
cached_present: false,
|
|
8178
|
+
cached: 0,
|
|
8179
|
+
cache_write_present: false,
|
|
8180
|
+
cache_write: 0,
|
|
8181
|
+
reasoning_present: false,
|
|
8182
|
+
reasoning: 0
|
|
8183
|
+
};
|
|
7567
8184
|
for (const s of steps) {
|
|
7568
8185
|
if (s.cost === void 0) continue;
|
|
7569
|
-
any_present = true;
|
|
7570
|
-
total += s.cost.total_usd;
|
|
7571
|
-
input += s.cost.input_usd;
|
|
7572
|
-
output += s.cost.output_usd;
|
|
8186
|
+
acc.any_present = true;
|
|
8187
|
+
acc.total += s.cost.total_usd;
|
|
8188
|
+
acc.input += s.cost.input_usd;
|
|
8189
|
+
acc.output += s.cost.output_usd;
|
|
7573
8190
|
if (s.cost.cached_input_usd !== void 0) {
|
|
7574
|
-
cached_present = true;
|
|
7575
|
-
cached += s.cost.cached_input_usd;
|
|
8191
|
+
acc.cached_present = true;
|
|
8192
|
+
acc.cached += s.cost.cached_input_usd;
|
|
7576
8193
|
}
|
|
7577
8194
|
if (s.cost.cache_write_usd !== void 0) {
|
|
7578
|
-
cache_write_present = true;
|
|
7579
|
-
cache_write += s.cost.cache_write_usd;
|
|
8195
|
+
acc.cache_write_present = true;
|
|
8196
|
+
acc.cache_write += s.cost.cache_write_usd;
|
|
7580
8197
|
}
|
|
7581
8198
|
if (s.cost.reasoning_usd !== void 0) {
|
|
7582
|
-
reasoning_present = true;
|
|
7583
|
-
reasoning += s.cost.reasoning_usd;
|
|
8199
|
+
acc.reasoning_present = true;
|
|
8200
|
+
acc.reasoning += s.cost.reasoning_usd;
|
|
7584
8201
|
}
|
|
7585
8202
|
}
|
|
7586
|
-
|
|
8203
|
+
return acc;
|
|
8204
|
+
}
|
|
8205
|
+
/**
|
|
8206
|
+
* Sum per-step cost breakdowns into a single `CostBreakdown` for the whole
|
|
8207
|
+
* generate call.
|
|
8208
|
+
*
|
|
8209
|
+
* Returns `undefined` if no step reports a cost, unless `provider` is free
|
|
8210
|
+
* (`FREE_PROVIDERS`) and at least one step ran; in that case it returns an
|
|
8211
|
+
* explicit all-zero breakdown instead of omitting cost entirely. The
|
|
8212
|
+
* optional fields (`cached_input_usd`, `cache_write_usd`, `reasoning_usd`)
|
|
8213
|
+
* are included only when at least one step reported them.
|
|
8214
|
+
*/
|
|
8215
|
+
function aggregate_cost(steps, provider) {
|
|
8216
|
+
const acc = sum_step_costs(steps);
|
|
8217
|
+
if (!acc.any_present) {
|
|
7587
8218
|
if (FREE_PROVIDERS.has(provider) && steps.length > 0) return {
|
|
7588
8219
|
total_usd: 0,
|
|
7589
8220
|
input_usd: 0,
|
|
@@ -7594,15 +8225,15 @@ function aggregate_cost(steps, provider) {
|
|
|
7594
8225
|
return;
|
|
7595
8226
|
}
|
|
7596
8227
|
const out = {
|
|
7597
|
-
total_usd: round6(total),
|
|
7598
|
-
input_usd: round6(input),
|
|
7599
|
-
output_usd: round6(output),
|
|
8228
|
+
total_usd: round6(acc.total),
|
|
8229
|
+
input_usd: round6(acc.input),
|
|
8230
|
+
output_usd: round6(acc.output),
|
|
7600
8231
|
currency: "USD",
|
|
7601
8232
|
is_estimate: true
|
|
7602
8233
|
};
|
|
7603
|
-
if (cached_present) out.cached_input_usd = round6(cached);
|
|
7604
|
-
if (cache_write_present) out.cache_write_usd = round6(cache_write);
|
|
7605
|
-
if (reasoning_present) out.reasoning_usd = round6(reasoning);
|
|
8234
|
+
if (acc.cached_present) out.cached_input_usd = round6(acc.cached);
|
|
8235
|
+
if (acc.cache_write_present) out.cache_write_usd = round6(acc.cache_write);
|
|
8236
|
+
if (acc.reasoning_present) out.reasoning_usd = round6(acc.reasoning);
|
|
7606
8237
|
return out;
|
|
7607
8238
|
}
|
|
7608
8239
|
//#endregion
|
|
@@ -7631,6 +8262,83 @@ function build_provider_adapters(providers, custom_providers) {
|
|
|
7631
8262
|
return adapters;
|
|
7632
8263
|
}
|
|
7633
8264
|
/**
|
|
8265
|
+
* Seed a fresh, mutable pricing table from DEFAULT_PRICING and layer any
|
|
8266
|
+
* per-engine `config.pricing` overrides on top.
|
|
8267
|
+
*
|
|
8268
|
+
* The result is the engine's own table: register_price mutates it in place,
|
|
8269
|
+
* and DEFAULT_PRICING (frozen at module load) is never touched.
|
|
8270
|
+
*/
|
|
8271
|
+
function merge_pricing(overrides) {
|
|
8272
|
+
const pricing = { ...DEFAULT_PRICING };
|
|
8273
|
+
if (overrides !== void 0) for (const [key, value] of Object.entries(overrides)) pricing[key] = value;
|
|
8274
|
+
return pricing;
|
|
8275
|
+
}
|
|
8276
|
+
/**
|
|
8277
|
+
* Resolve the three always-present scalar defaults.
|
|
8278
|
+
*
|
|
8279
|
+
* `config.defaults.*` is the current nested layer; `config.default_retry`,
|
|
8280
|
+
* `config.default_effort`, and `config.default_max_steps` are older top-level
|
|
8281
|
+
* fields kept for compatibility. The nested value wins where both are set, and
|
|
8282
|
+
* the built-in constant is the last resort.
|
|
8283
|
+
*/
|
|
8284
|
+
function resolve_default_scalars(config) {
|
|
8285
|
+
const defaults = config.defaults ?? {};
|
|
8286
|
+
return {
|
|
8287
|
+
default_retry: defaults.retry_policy ?? config.default_retry ?? DEFAULT_RETRY,
|
|
8288
|
+
default_effort: defaults.effort ?? config.default_effort ?? "none",
|
|
8289
|
+
default_max_steps: defaults.max_steps ?? config.default_max_steps ?? 10
|
|
8290
|
+
};
|
|
8291
|
+
}
|
|
8292
|
+
/**
|
|
8293
|
+
* Validate the numeric defaults once, at construction: a bad bound should fail
|
|
8294
|
+
* fast here rather than surfacing deep inside the first generate call that ends
|
|
8295
|
+
* up using the default.
|
|
8296
|
+
*/
|
|
8297
|
+
function validate_numeric_defaults(defaults) {
|
|
8298
|
+
if (defaults === void 0) return;
|
|
8299
|
+
const { tool_call_repair_attempts, max_tool_calls_per_step, turn_timeout_ms } = defaults;
|
|
8300
|
+
if (tool_call_repair_attempts !== void 0 && tool_call_repair_attempts < 0) throw new engine_config_error("defaults.tool_call_repair_attempts must be >= 0");
|
|
8301
|
+
if (max_tool_calls_per_step !== void 0 && max_tool_calls_per_step < 1) throw new engine_config_error("defaults.max_tool_calls_per_step must be >= 1");
|
|
8302
|
+
if (turn_timeout_ms !== void 0 && turn_timeout_ms <= 0) throw new engine_config_error("defaults.turn_timeout_ms must be > 0");
|
|
8303
|
+
}
|
|
8304
|
+
/**
|
|
8305
|
+
* Copy `value` onto `target[key]` only when it is defined, so an absent optional
|
|
8306
|
+
* default leaves the key off the object rather than present-as-undefined.
|
|
8307
|
+
*/
|
|
8308
|
+
function assign_if_present$1(target, key, value) {
|
|
8309
|
+
if (value !== void 0) target[key] = value;
|
|
8310
|
+
}
|
|
8311
|
+
/**
|
|
8312
|
+
* Assemble the per-instance EngineInternals from the resolved required fields
|
|
8313
|
+
* plus whichever optional `defaults.*` knobs were supplied.
|
|
8314
|
+
*
|
|
8315
|
+
* The optional defaults form a fixed source-to-target table (`defaults.model` ->
|
|
8316
|
+
* `default_model`, and so on); each is folded in only when present, matching the
|
|
8317
|
+
* conditional-spread shape this replaced.
|
|
8318
|
+
*/
|
|
8319
|
+
function build_internals(base, defaults) {
|
|
8320
|
+
const internals = {
|
|
8321
|
+
pricing: base.pricing,
|
|
8322
|
+
adapters: base.adapters,
|
|
8323
|
+
default_retry: base.default_retry,
|
|
8324
|
+
default_effort: base.default_effort,
|
|
8325
|
+
default_max_steps: base.default_max_steps
|
|
8326
|
+
};
|
|
8327
|
+
if (defaults !== void 0) {
|
|
8328
|
+
assign_if_present$1(internals, "default_model", defaults.model);
|
|
8329
|
+
assign_if_present$1(internals, "default_provider", defaults.provider);
|
|
8330
|
+
assign_if_present$1(internals, "default_system", defaults.system);
|
|
8331
|
+
assign_if_present$1(internals, "default_tool_error_policy", defaults.tool_error_policy);
|
|
8332
|
+
assign_if_present$1(internals, "default_schema_repair_attempts", defaults.schema_repair_attempts);
|
|
8333
|
+
assign_if_present$1(internals, "default_tool_call_repair_attempts", defaults.tool_call_repair_attempts);
|
|
8334
|
+
assign_if_present$1(internals, "default_max_tool_calls_per_step", defaults.max_tool_calls_per_step);
|
|
8335
|
+
assign_if_present$1(internals, "default_turn_timeout_ms", defaults.turn_timeout_ms);
|
|
8336
|
+
assign_if_present$1(internals, "default_ai_sdk_telemetry", defaults.ai_sdk_telemetry);
|
|
8337
|
+
assign_if_present$1(internals, "default_provider_options", defaults.provider_options);
|
|
8338
|
+
}
|
|
8339
|
+
return internals;
|
|
8340
|
+
}
|
|
8341
|
+
/**
|
|
7634
8342
|
* Validate `config` and construct an `Engine`.
|
|
7635
8343
|
*
|
|
7636
8344
|
* Provider entries are validated synchronously via `build_provider_adapters`.
|
|
@@ -7640,32 +8348,16 @@ function build_provider_adapters(providers, custom_providers) {
|
|
|
7640
8348
|
function create_engine(config) {
|
|
7641
8349
|
if (config.providers === null || typeof config.providers !== "object") throw new engine_config_error("EngineConfig.providers is required");
|
|
7642
8350
|
const adapters = build_provider_adapters(config.providers, config.custom_providers);
|
|
7643
|
-
const pricing =
|
|
7644
|
-
|
|
7645
|
-
|
|
7646
|
-
const
|
|
7647
|
-
const default_effort = defaults?.effort ?? config.default_effort ?? "none";
|
|
7648
|
-
const default_max_steps = defaults?.max_steps ?? config.default_max_steps ?? 10;
|
|
7649
|
-
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");
|
|
7650
|
-
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");
|
|
7651
|
-
if (defaults?.turn_timeout_ms !== void 0 && defaults.turn_timeout_ms <= 0) throw new engine_config_error("defaults.turn_timeout_ms must be > 0");
|
|
7652
|
-
const get_internals = () => ({
|
|
8351
|
+
const pricing = merge_pricing(config.pricing);
|
|
8352
|
+
const { default_retry, default_effort, default_max_steps } = resolve_default_scalars(config);
|
|
8353
|
+
validate_numeric_defaults(config.defaults);
|
|
8354
|
+
const get_internals = () => build_internals({
|
|
7653
8355
|
pricing,
|
|
7654
8356
|
adapters,
|
|
7655
8357
|
default_retry,
|
|
7656
8358
|
default_effort,
|
|
7657
|
-
default_max_steps
|
|
7658
|
-
|
|
7659
|
-
...defaults?.provider !== void 0 ? { default_provider: defaults.provider } : {},
|
|
7660
|
-
...defaults?.system !== void 0 ? { default_system: defaults.system } : {},
|
|
7661
|
-
...defaults?.tool_error_policy !== void 0 ? { default_tool_error_policy: defaults.tool_error_policy } : {},
|
|
7662
|
-
...defaults?.schema_repair_attempts !== void 0 ? { default_schema_repair_attempts: defaults.schema_repair_attempts } : {},
|
|
7663
|
-
...defaults?.tool_call_repair_attempts !== void 0 ? { default_tool_call_repair_attempts: defaults.tool_call_repair_attempts } : {},
|
|
7664
|
-
...defaults?.max_tool_calls_per_step !== void 0 ? { default_max_tool_calls_per_step: defaults.max_tool_calls_per_step } : {},
|
|
7665
|
-
...defaults?.turn_timeout_ms !== void 0 ? { default_turn_timeout_ms: defaults.turn_timeout_ms } : {},
|
|
7666
|
-
...defaults?.ai_sdk_telemetry !== void 0 ? { default_ai_sdk_telemetry: defaults.ai_sdk_telemetry } : {},
|
|
7667
|
-
...defaults?.provider_options !== void 0 ? { default_provider_options: defaults.provider_options } : {}
|
|
7668
|
-
});
|
|
8359
|
+
default_max_steps
|
|
8360
|
+
}, config.defaults);
|
|
7669
8361
|
let disposed = false;
|
|
7670
8362
|
let dispose_promise;
|
|
7671
8363
|
return {
|
|
@@ -7771,6 +8463,41 @@ function stable_signature(input) {
|
|
|
7771
8463
|
return createHash("sha256").update(payload).digest("hex").slice(0, 8);
|
|
7772
8464
|
}
|
|
7773
8465
|
/**
|
|
8466
|
+
* Copy `value` onto `target[key]` only when it is defined, so an absent optional
|
|
8467
|
+
* config field leaves the key off `GenerateOptions` rather than present-as-undefined.
|
|
8468
|
+
*/
|
|
8469
|
+
function assign_if_present(target, key, value) {
|
|
8470
|
+
if (value !== void 0) target[key] = value;
|
|
8471
|
+
}
|
|
8472
|
+
/**
|
|
8473
|
+
* Translate `cfg` into a `GenerateOptions`, folding each supplied optional field
|
|
8474
|
+
* in via `assign_if_present`. `tools` (spread to a fresh array) and
|
|
8475
|
+
* `retry_policy` (renamed to `retry`) are the two fields that do not copy
|
|
8476
|
+
* straight across and stay explicit; the rest map name-for-name.
|
|
8477
|
+
*/
|
|
8478
|
+
function build_generate_options(cfg, prompt, ctx) {
|
|
8479
|
+
const opts = {
|
|
8480
|
+
prompt,
|
|
8481
|
+
abort: ctx.abort,
|
|
8482
|
+
trajectory: engine_trajectory(ctx.trajectory, ctx.parent_span_id)
|
|
8483
|
+
};
|
|
8484
|
+
assign_if_present(opts, "model", cfg.model);
|
|
8485
|
+
assign_if_present(opts, "provider", cfg.provider);
|
|
8486
|
+
assign_if_present(opts, "system", cfg.system);
|
|
8487
|
+
if (cfg.tools !== void 0) opts.tools = [...cfg.tools];
|
|
8488
|
+
assign_if_present(opts, "schema", cfg.schema);
|
|
8489
|
+
assign_if_present(opts, "effort", cfg.effort);
|
|
8490
|
+
assign_if_present(opts, "max_steps", cfg.max_steps);
|
|
8491
|
+
assign_if_present(opts, "provider_options", cfg.provider_options);
|
|
8492
|
+
if (cfg.retry_policy !== void 0) opts.retry = cfg.retry_policy;
|
|
8493
|
+
assign_if_present(opts, "tool_error_policy", cfg.tool_error_policy);
|
|
8494
|
+
assign_if_present(opts, "schema_repair_attempts", cfg.schema_repair_attempts);
|
|
8495
|
+
assign_if_present(opts, "tool_call_repair_attempts", cfg.tool_call_repair_attempts);
|
|
8496
|
+
assign_if_present(opts, "max_tool_calls_per_step", cfg.max_tool_calls_per_step);
|
|
8497
|
+
assign_if_present(opts, "on_tool_approval", cfg.on_tool_approval);
|
|
8498
|
+
return opts;
|
|
8499
|
+
}
|
|
8500
|
+
/**
|
|
7774
8501
|
* Build a `Step` that calls `cfg.engine.generate` with `cfg` translated into
|
|
7775
8502
|
* `GenerateOptions`.
|
|
7776
8503
|
*
|
|
@@ -7801,31 +8528,13 @@ function model_call(cfg) {
|
|
|
7801
8528
|
reason: { signal: "abort" },
|
|
7802
8529
|
step_index: 0
|
|
7803
8530
|
});
|
|
7804
|
-
const opts = {
|
|
7805
|
-
|
|
7806
|
-
|
|
7807
|
-
|
|
7808
|
-
|
|
7809
|
-
|
|
7810
|
-
|
|
7811
|
-
}] : [...input],
|
|
7812
|
-
abort: ctx.abort,
|
|
7813
|
-
trajectory: engine_trajectory(ctx.trajectory, ctx.parent_span_id)
|
|
7814
|
-
};
|
|
7815
|
-
if (cfg.model !== void 0) opts.model = cfg.model;
|
|
7816
|
-
if (cfg.provider !== void 0) opts.provider = cfg.provider;
|
|
7817
|
-
if (cfg.system !== void 0) opts.system = cfg.system;
|
|
7818
|
-
if (cfg.tools !== void 0) opts.tools = [...cfg.tools];
|
|
7819
|
-
if (cfg.schema !== void 0) opts.schema = cfg.schema;
|
|
7820
|
-
if (cfg.effort !== void 0) opts.effort = cfg.effort;
|
|
7821
|
-
if (cfg.max_steps !== void 0) opts.max_steps = cfg.max_steps;
|
|
7822
|
-
if (cfg.provider_options !== void 0) opts.provider_options = cfg.provider_options;
|
|
7823
|
-
if (cfg.retry_policy !== void 0) opts.retry = cfg.retry_policy;
|
|
7824
|
-
if (cfg.tool_error_policy !== void 0) opts.tool_error_policy = cfg.tool_error_policy;
|
|
7825
|
-
if (cfg.schema_repair_attempts !== void 0) opts.schema_repair_attempts = cfg.schema_repair_attempts;
|
|
7826
|
-
if (cfg.tool_call_repair_attempts !== void 0) opts.tool_call_repair_attempts = cfg.tool_call_repair_attempts;
|
|
7827
|
-
if (cfg.max_tool_calls_per_step !== void 0) opts.max_tool_calls_per_step = cfg.max_tool_calls_per_step;
|
|
7828
|
-
if (cfg.on_tool_approval !== void 0) opts.on_tool_approval = cfg.on_tool_approval;
|
|
8531
|
+
const opts = build_generate_options(cfg, typeof input === "string" ? [{
|
|
8532
|
+
role: "user",
|
|
8533
|
+
content: [{
|
|
8534
|
+
type: "text",
|
|
8535
|
+
text: input
|
|
8536
|
+
}]
|
|
8537
|
+
}] : [...input], ctx);
|
|
7829
8538
|
if (ctx.streaming) opts.on_chunk = (chunk) => {
|
|
7830
8539
|
ctx.trajectory.record({
|
|
7831
8540
|
kind: "model_chunk",
|
|
@@ -8323,19 +9032,21 @@ async function start_viewer(options = {}) {
|
|
|
8323
9032
|
...options.on_parse_error ? { on_parse_error: options.on_parse_error } : {},
|
|
8324
9033
|
...options.on_io_error ? { on_io_error: options.on_io_error } : {}
|
|
8325
9034
|
});
|
|
8326
|
-
return wrap_handle(server, tail, host
|
|
9035
|
+
return wrap_handle(server, tail, host);
|
|
8327
9036
|
}
|
|
8328
9037
|
/**
|
|
8329
9038
|
* Combines the server and optional tail into a single `ViewerHandle`.
|
|
8330
9039
|
*
|
|
8331
9040
|
* `close` stops the tail first, then closes the server, mirroring the
|
|
8332
|
-
* startup order (server first, tail second) in reverse.
|
|
9041
|
+
* startup order (server first, tail second) in reverse. The port is read
|
|
9042
|
+
* back from `server.url` rather than the requested `port` option, since
|
|
9043
|
+
* a caller that passes `0` gets whatever port the OS actually bound.
|
|
8333
9044
|
*/
|
|
8334
|
-
function wrap_handle(server, tail, host
|
|
9045
|
+
function wrap_handle(server, tail, host) {
|
|
8335
9046
|
return {
|
|
8336
9047
|
url: server.url,
|
|
8337
9048
|
host,
|
|
8338
|
-
port,
|
|
9049
|
+
port: Number(new URL(server.url).port),
|
|
8339
9050
|
close: async () => {
|
|
8340
9051
|
if (tail) tail.stop();
|
|
8341
9052
|
await server.close();
|