fascicle 0.10.2 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +52 -0
- package/README.md +9 -9
- package/dist/adapters.d.ts +1 -1
- package/dist/agents.d.ts +4 -4
- package/dist/agents.js +1 -1
- package/dist/agents.js.map +1 -1
- package/dist/{core-Dit3Rv9J.js → core-Ww6An4CS.js} +192 -96
- package/dist/core-Ww6An4CS.js.map +1 -0
- package/dist/index.d.ts +1124 -5
- package/dist/index.js +589 -961
- package/dist/index.js.map +1 -1
- package/dist/invoke-CWjm1Y_R.js +451 -0
- package/dist/invoke-CWjm1Y_R.js.map +1 -0
- package/dist/mcp.d.ts +25 -13
- package/dist/mcp.js +203 -121
- package/dist/mcp.js.map +1 -1
- package/dist/otel.d.ts +1 -1
- package/dist/stdio.d.ts +4 -4
- package/dist/stdio.js +30 -12
- package/dist/stdio.js.map +1 -1
- package/dist/{index-CHtfOfEF.d.ts → types-B3HIsBBD.d.ts} +7 -133
- package/dist/types-BCtKsxrX.d.ts +149 -0
- package/dist/types-BDSbSUAO.d.ts +63 -0
- package/dist/types-NnSvmpjg.js +253 -0
- package/dist/types-NnSvmpjg.js.map +1 -0
- package/dist/ui.d.ts +2 -2
- package/package.json +14 -3
- package/dist/core-Dit3Rv9J.js.map +0 -1
- package/dist/index-C2ayBpzS.d.ts +0 -993
package/dist/index.js
CHANGED
|
@@ -1,16 +1,152 @@
|
|
|
1
|
-
import { A 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-Ww6An4CS.js";
|
|
2
2
|
import { a as http_logger, n as tee_logger, o as filesystem_logger } from "./adapters-CJnu1687.js";
|
|
3
|
+
import { _ as tool_approval_denied_error, a as claude_cli_error, c as incomplete_generation_error, d as provider_auth_error, f as provider_capability_error, g as schema_validation_error, h as rate_limit_error, i as resolve_transport, l as model_required_error, m as provider_not_configured_error, n as load_optional_peer, o as engine_config_error, p as provider_error, r as missing_peer_error, s as engine_disposed_error, t as default_normalize_usage, u as on_chunk_error, v as tool_error, y as turn_timeout_error } from "./types-NnSvmpjg.js";
|
|
3
4
|
import { createHash } from "node:crypto";
|
|
4
|
-
import { z } from "zod";
|
|
5
5
|
import { mkdir, open, readFile, readdir, stat, writeFile } from "node:fs/promises";
|
|
6
6
|
import { dirname, join, resolve } from "node:path";
|
|
7
7
|
import { createReadStream, existsSync, mkdtempSync, statSync, watch, writeFileSync } from "node:fs";
|
|
8
8
|
import { tmpdir } from "node:os";
|
|
9
9
|
import { spawn } from "node:child_process";
|
|
10
|
-
import { NoObjectGeneratedError, Output, generateText, isStepCount, streamText, tool } from "ai";
|
|
11
10
|
import { createServer } from "node:http";
|
|
12
11
|
import { fileURLToPath } from "node:url";
|
|
13
12
|
import { parseArgs } from "node:util";
|
|
13
|
+
//#region src/schema/errors.ts
|
|
14
|
+
/**
|
|
15
|
+
* The one error this zone raises.
|
|
16
|
+
*
|
|
17
|
+
* It lives here rather than in core's taxonomy because core imports this zone,
|
|
18
|
+
* and reaching back for an error class would close a cycle.
|
|
19
|
+
*/
|
|
20
|
+
var schema_not_convertible_error = class extends Error {
|
|
21
|
+
kind = "schema_not_convertible_error";
|
|
22
|
+
vendor;
|
|
23
|
+
constructor(vendor, message) {
|
|
24
|
+
super(message ?? `schema vendor '${vendor}' does not implement Standard JSON Schema, so it cannot be sent to a model as JSON Schema`);
|
|
25
|
+
this.name = "schema_not_convertible_error";
|
|
26
|
+
this.vendor = vendor;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/schema/json_schema.ts
|
|
31
|
+
/**
|
|
32
|
+
* JSON Schema emission for schemas that reach a model.
|
|
33
|
+
*
|
|
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.
|
|
46
|
+
*/
|
|
47
|
+
const DEFAULT_TARGET = "draft-2020-12";
|
|
48
|
+
/**
|
|
49
|
+
* Emit JSON Schema for `schema`, targeting draft-2020-12 unless told otherwise.
|
|
50
|
+
*
|
|
51
|
+
* `target` is passed explicitly on every call because the spec makes it a
|
|
52
|
+
* required argument of the converter, even where a vendor tolerates its
|
|
53
|
+
* omission. A vendor that implements Standard Schema but not Standard JSON
|
|
54
|
+
* Schema raises `schema_not_convertible_error` naming itself, so the message
|
|
55
|
+
* says which library to swap rather than just that something failed.
|
|
56
|
+
*/
|
|
57
|
+
function to_json_schema(schema, options = {}) {
|
|
58
|
+
const props = schema["~standard"];
|
|
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 }));
|
|
61
|
+
return options.strip_meta === true ? strip_meta_keys(json) : json;
|
|
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
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Drop the top-level `$schema` and `$id` keys.
|
|
141
|
+
*
|
|
142
|
+
* `claude --json-schema` rejects both, and the field constraints alone drive
|
|
143
|
+
* constrained decode, so the CLI adapter asks for a stripped emission.
|
|
144
|
+
*/
|
|
145
|
+
function strip_meta_keys(json) {
|
|
146
|
+
const { $schema: _schema, $id: _id, ...rest } = json;
|
|
147
|
+
return rest;
|
|
148
|
+
}
|
|
149
|
+
//#endregion
|
|
14
150
|
//#region src/composites/adversarial.ts
|
|
15
151
|
/**
|
|
16
152
|
* adversarial: build-and-critique loop.
|
|
@@ -53,7 +189,7 @@ function build_input_from_state(s) {
|
|
|
53
189
|
*/
|
|
54
190
|
function adversarial(config) {
|
|
55
191
|
const { build, critique, accept, max_rounds } = config;
|
|
56
|
-
const inner =
|
|
192
|
+
const inner = loop({
|
|
57
193
|
init: (input) => ({ input }),
|
|
58
194
|
body: scope([
|
|
59
195
|
stash("state", step("snapshot", (s) => s)),
|
|
@@ -85,16 +221,16 @@ function adversarial(config) {
|
|
|
85
221
|
};
|
|
86
222
|
})
|
|
87
223
|
]),
|
|
88
|
-
finish: (s) => {
|
|
224
|
+
finish: (s, { converged, rounds }) => {
|
|
89
225
|
if (s.candidate === void 0) throw new Error("adversarial: finished without a candidate");
|
|
90
|
-
return
|
|
226
|
+
return {
|
|
227
|
+
candidate: s.candidate,
|
|
228
|
+
converged,
|
|
229
|
+
rounds
|
|
230
|
+
};
|
|
91
231
|
},
|
|
92
232
|
max_rounds
|
|
93
|
-
})
|
|
94
|
-
candidate: result.value,
|
|
95
|
-
converged: result.converged,
|
|
96
|
-
rounds: result.rounds
|
|
97
|
-
}));
|
|
233
|
+
});
|
|
98
234
|
return compose(config.name ?? "adversarial", inner);
|
|
99
235
|
}
|
|
100
236
|
//#endregion
|
|
@@ -117,7 +253,7 @@ function adversarial(config) {
|
|
|
117
253
|
function consensus(config) {
|
|
118
254
|
const { members, agree, max_rounds } = config;
|
|
119
255
|
const round_step = parallel(members);
|
|
120
|
-
const inner =
|
|
256
|
+
const inner = loop({
|
|
121
257
|
init: (input) => ({
|
|
122
258
|
input,
|
|
123
259
|
results: {}
|
|
@@ -137,12 +273,12 @@ function consensus(config) {
|
|
|
137
273
|
stop: agree(s.results),
|
|
138
274
|
state: s
|
|
139
275
|
})),
|
|
140
|
-
finish: (s) =>
|
|
276
|
+
finish: (s, { converged }) => ({
|
|
277
|
+
result: s.results,
|
|
278
|
+
converged
|
|
279
|
+
}),
|
|
141
280
|
max_rounds
|
|
142
|
-
})
|
|
143
|
-
result: result.value,
|
|
144
|
-
converged: result.converged
|
|
145
|
-
}));
|
|
281
|
+
});
|
|
146
282
|
return compose(config.name ?? "consensus", inner);
|
|
147
283
|
}
|
|
148
284
|
//#endregion
|
|
@@ -453,14 +589,14 @@ function improve(config) {
|
|
|
453
589
|
}),
|
|
454
590
|
max_rounds
|
|
455
591
|
}),
|
|
456
|
-
step("
|
|
592
|
+
step("improve_stop", (result, ctx) => {
|
|
457
593
|
ctx.trajectory.record({
|
|
458
594
|
kind: "improve.stop",
|
|
459
|
-
stopped_by: result.
|
|
460
|
-
rounds_used: result.
|
|
461
|
-
final_score: result.
|
|
595
|
+
stopped_by: result.stopped_by,
|
|
596
|
+
rounds_used: result.rounds_used,
|
|
597
|
+
final_score: result.best.score
|
|
462
598
|
});
|
|
463
|
-
return result
|
|
599
|
+
return result;
|
|
464
600
|
})
|
|
465
601
|
]);
|
|
466
602
|
return compose(config.name ?? "improve", inner);
|
|
@@ -530,7 +666,7 @@ async function read_jsonl(file_path, ctx) {
|
|
|
530
666
|
});
|
|
531
667
|
continue;
|
|
532
668
|
}
|
|
533
|
-
const result =
|
|
669
|
+
const result = parse_trajectory_event(parsed);
|
|
534
670
|
if (result.success) events.push(result.data);
|
|
535
671
|
else ctx.trajectory.record({
|
|
536
672
|
kind: "learn.parse_error",
|
|
@@ -544,7 +680,7 @@ async function read_jsonl(file_path, ctx) {
|
|
|
544
680
|
* Throw the abort reason (wrapped in `aborted_error` if needed) when the run
|
|
545
681
|
* has been cancelled.
|
|
546
682
|
*/
|
|
547
|
-
function throw_if_aborted$
|
|
683
|
+
function throw_if_aborted$2(ctx) {
|
|
548
684
|
if (!ctx.abort.aborted) return;
|
|
549
685
|
const reason = ctx.abort.reason;
|
|
550
686
|
if (reason instanceof aborted_error) throw reason;
|
|
@@ -562,7 +698,7 @@ async function resolve_events(source, ctx) {
|
|
|
562
698
|
const file_paths = source.kind === "paths" ? [...source.paths] : (await walk_jsonl(source.dir)).toSorted();
|
|
563
699
|
const all = [];
|
|
564
700
|
for (const p of file_paths) {
|
|
565
|
-
throw_if_aborted$
|
|
701
|
+
throw_if_aborted$2(ctx);
|
|
566
702
|
const events = await read_jsonl(p, ctx);
|
|
567
703
|
all.push(...events);
|
|
568
704
|
}
|
|
@@ -705,6 +841,26 @@ function tournament(config) {
|
|
|
705
841
|
return compose(config.name ?? "tournament", inner);
|
|
706
842
|
}
|
|
707
843
|
//#endregion
|
|
844
|
+
//#region src/composites/errors.ts
|
|
845
|
+
/**
|
|
846
|
+
* Typed errors for the composites layer.
|
|
847
|
+
*
|
|
848
|
+
* Listed alongside src/core/errors.ts, src/engine/errors.ts, and
|
|
849
|
+
* src/mcp/errors.ts in the `no-class` rule ignores: `instanceof` branching is
|
|
850
|
+
* how a caller tells a benched flow's usage error apart from a case failure.
|
|
851
|
+
*/
|
|
852
|
+
var bench_suspend_error = class extends Error {
|
|
853
|
+
kind = "bench_suspend_error";
|
|
854
|
+
case_id;
|
|
855
|
+
suspend_id;
|
|
856
|
+
constructor(case_id, suspend_id, message) {
|
|
857
|
+
super(message ?? `bench: case ${case_id} suspended at ${suspend_id}; bench cannot resume a suspended flow`);
|
|
858
|
+
this.name = "bench_suspend_error";
|
|
859
|
+
this.case_id = case_id;
|
|
860
|
+
this.suspend_id = suspend_id;
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
//#endregion
|
|
708
864
|
//#region src/composites/bench.ts
|
|
709
865
|
/**
|
|
710
866
|
* bench: run a flow against a fixture set, score each result, return a report.
|
|
@@ -731,12 +887,21 @@ function tournament(config) {
|
|
|
731
887
|
* judge spans land in the case's trajectory file (or push). A judge that
|
|
732
888
|
* throws or returns undefined abstains: the entry is omitted from
|
|
733
889
|
* `case.scores` and the case does not contribute to that judge's mean.
|
|
890
|
+
*
|
|
891
|
+
* Control-flow signals are not case failures and never reach a `CaseResult`.
|
|
892
|
+
* `abort` forwards to every per-case `run` and is re-checked before each case
|
|
893
|
+
* is claimed, so cancelling rejects the whole bench rather than returning a
|
|
894
|
+
* report that silently covers fewer cases than it was given. A
|
|
895
|
+
* `suspended_error` from an approval gate rejects with `bench_suspend_error`:
|
|
896
|
+
* `bench` has no resume path, so a suspend is a usage error, not a zero score.
|
|
734
897
|
*/
|
|
735
898
|
async function bench(flow, cases, judges, options = {}) {
|
|
736
899
|
const run_id = options.run_id ?? `bench-${String(Date.now())}-${random_suffix()}`;
|
|
737
900
|
const flow_name = describe_flow_name(flow);
|
|
738
901
|
const concurrency = resolve_concurrency(options.concurrency, cases.length);
|
|
739
902
|
const install_signal_handlers = options.install_signal_handlers ?? false;
|
|
903
|
+
const run_options = options.abort === void 0 ? {} : { abort: options.abort };
|
|
904
|
+
throw_if_aborted$1(options.abort);
|
|
740
905
|
if (options.trajectory_dir !== void 0) await mkdir(options.trajectory_dir, { recursive: true });
|
|
741
906
|
const judge_entries = Object.entries(judges);
|
|
742
907
|
const run_one = async (bc) => {
|
|
@@ -753,10 +918,12 @@ async function bench(flow, cases, judges, options = {}) {
|
|
|
753
918
|
try {
|
|
754
919
|
output = await run(flow, bc.input, {
|
|
755
920
|
trajectory: case_logger,
|
|
756
|
-
install_signal_handlers
|
|
921
|
+
install_signal_handlers,
|
|
922
|
+
...run_options
|
|
757
923
|
});
|
|
758
924
|
ok = true;
|
|
759
925
|
} catch (err) {
|
|
926
|
+
rethrow_control_flow(err, bc.id);
|
|
760
927
|
error = err instanceof Error ? err.message : String(err);
|
|
761
928
|
ok = false;
|
|
762
929
|
}
|
|
@@ -775,9 +942,11 @@ async function bench(flow, cases, judges, options = {}) {
|
|
|
775
942
|
try {
|
|
776
943
|
raw = await run(judge, judge_input, {
|
|
777
944
|
trajectory: case_logger,
|
|
778
|
-
install_signal_handlers
|
|
945
|
+
install_signal_handlers,
|
|
946
|
+
...run_options
|
|
779
947
|
});
|
|
780
|
-
} catch {
|
|
948
|
+
} catch (err) {
|
|
949
|
+
rethrow_control_flow(err, bc.id);
|
|
781
950
|
continue;
|
|
782
951
|
}
|
|
783
952
|
const normalized = normalize_score(raw);
|
|
@@ -799,7 +968,8 @@ async function bench(flow, cases, judges, options = {}) {
|
|
|
799
968
|
if (options.on_case) options.on_case(result);
|
|
800
969
|
return result;
|
|
801
970
|
};
|
|
802
|
-
const cases_out = await run_with_concurrency(cases, concurrency, run_one);
|
|
971
|
+
const cases_out = await run_with_concurrency(cases, concurrency, run_one, options.abort);
|
|
972
|
+
throw_if_aborted$1(options.abort);
|
|
803
973
|
return {
|
|
804
974
|
flow_name,
|
|
805
975
|
run_id,
|
|
@@ -808,6 +978,32 @@ async function bench(flow, cases, judges, options = {}) {
|
|
|
808
978
|
};
|
|
809
979
|
}
|
|
810
980
|
/**
|
|
981
|
+
* Rethrows the control-flow signals that must never become a `CaseResult`.
|
|
982
|
+
*
|
|
983
|
+
* An `aborted_error` cancels the whole bench: a report claiming every
|
|
984
|
+
* remaining case failed is worse than no report. A `suspended_error` becomes
|
|
985
|
+
* `bench_suspend_error` naming the case, because `bench` cannot resume an
|
|
986
|
+
* approval gate and scoring the paused case as a failure corrupts the
|
|
987
|
+
* benchmark. Anything else returns, and the caller records it as a failure.
|
|
988
|
+
*/
|
|
989
|
+
function rethrow_control_flow(err, case_id) {
|
|
990
|
+
if (err instanceof aborted_error) throw err;
|
|
991
|
+
if (err instanceof suspended_error) throw new bench_suspend_error(case_id, err.suspend_id);
|
|
992
|
+
}
|
|
993
|
+
/**
|
|
994
|
+
* Rejects once `signal` has fired, so an abort stops the bench claiming
|
|
995
|
+
* further cases instead of running the queue to completion.
|
|
996
|
+
*
|
|
997
|
+
* Mirrors core's abort shape: an `Error` reason propagates verbatim, anything
|
|
998
|
+
* else is wrapped, so a caller aborting with its own `aborted_error` sees that
|
|
999
|
+
* instance rather than a copy.
|
|
1000
|
+
*/
|
|
1001
|
+
function throw_if_aborted$1(signal) {
|
|
1002
|
+
if (signal?.aborted !== true) return;
|
|
1003
|
+
const reason = signal.reason;
|
|
1004
|
+
throw reason instanceof Error ? reason : new aborted_error("aborted", { reason });
|
|
1005
|
+
}
|
|
1006
|
+
/**
|
|
811
1007
|
* Aggregates per-case results into the report summary.
|
|
812
1008
|
*
|
|
813
1009
|
* A judge's mean only averages the cases where that judge produced a score;
|
|
@@ -900,14 +1096,20 @@ function create_cost_tracker() {
|
|
|
900
1096
|
* Uses a shared-index worker pool: each of the `limit` workers claims the
|
|
901
1097
|
* next unclaimed index and writes its result into that slot, so completion
|
|
902
1098
|
* order never reorders the output.
|
|
1099
|
+
*
|
|
1100
|
+
* `abort` is re-checked before each claim, so a signal that fires mid-run
|
|
1101
|
+
* halts scheduling at the queue rather than only cancelling work already in
|
|
1102
|
+
* flight. The full fan-out path has no queue to halt: every item is already
|
|
1103
|
+
* running, so the caller re-checks the signal before reading the results.
|
|
903
1104
|
*/
|
|
904
|
-
async function run_with_concurrency(items, limit, fn) {
|
|
1105
|
+
async function run_with_concurrency(items, limit, fn, abort) {
|
|
905
1106
|
if (items.length === 0) return [];
|
|
906
1107
|
if (limit >= items.length) return Promise.all(items.map((it) => fn(it)));
|
|
907
1108
|
const results = Array.from({ length: items.length });
|
|
908
1109
|
let next = 0;
|
|
909
1110
|
const worker = async () => {
|
|
910
1111
|
while (true) {
|
|
1112
|
+
throw_if_aborted$1(abort);
|
|
911
1113
|
const idx = next;
|
|
912
1114
|
next += 1;
|
|
913
1115
|
if (idx >= items.length) return;
|
|
@@ -969,7 +1171,7 @@ function random_suffix() {
|
|
|
969
1171
|
* Narrows an unknown value to a plain string-keyed record (non-null object,
|
|
970
1172
|
* not an array).
|
|
971
1173
|
*/
|
|
972
|
-
function is_record(v) {
|
|
1174
|
+
function is_record$1(v) {
|
|
973
1175
|
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
974
1176
|
}
|
|
975
1177
|
/**
|
|
@@ -985,7 +1187,7 @@ function deep_equal(a, b) {
|
|
|
985
1187
|
for (let i = 0; i < a.length; i += 1) if (!deep_equal(a[i], b[i])) return false;
|
|
986
1188
|
return true;
|
|
987
1189
|
}
|
|
988
|
-
if (!is_record(a) || !is_record(b)) return false;
|
|
1190
|
+
if (!is_record$1(a) || !is_record$1(b)) return false;
|
|
989
1191
|
const ak = Object.keys(a).toSorted();
|
|
990
1192
|
const bk = Object.keys(b).toSorted();
|
|
991
1193
|
if (ak.length !== bk.length) return false;
|
|
@@ -1081,7 +1283,7 @@ function parse_score(reply, scale) {
|
|
|
1081
1283
|
} catch {
|
|
1082
1284
|
return;
|
|
1083
1285
|
}
|
|
1084
|
-
if (!is_record(parsed)) return void 0;
|
|
1286
|
+
if (!is_record$1(parsed)) return void 0;
|
|
1085
1287
|
const raw_score = parsed["score"];
|
|
1086
1288
|
if (typeof raw_score !== "number" || !Number.isFinite(raw_score)) return void 0;
|
|
1087
1289
|
const clamped = Math.max(scale.min, Math.min(scale.max, raw_score));
|
|
@@ -1398,191 +1600,6 @@ function compute_cost(usage, pricing, provider) {
|
|
|
1398
1600
|
return breakdown;
|
|
1399
1601
|
}
|
|
1400
1602
|
//#endregion
|
|
1401
|
-
//#region src/engine/errors.ts
|
|
1402
|
-
var rate_limit_error = class extends Error {
|
|
1403
|
-
kind = "rate_limit_error";
|
|
1404
|
-
retry_after_ms;
|
|
1405
|
-
attempts;
|
|
1406
|
-
status;
|
|
1407
|
-
constructor(message, metadata = {}) {
|
|
1408
|
-
super(message);
|
|
1409
|
-
this.name = "rate_limit_error";
|
|
1410
|
-
this.retry_after_ms = metadata.retry_after_ms;
|
|
1411
|
-
this.attempts = metadata.attempts ?? 0;
|
|
1412
|
-
this.status = metadata.status;
|
|
1413
|
-
}
|
|
1414
|
-
};
|
|
1415
|
-
var provider_error = class extends Error {
|
|
1416
|
-
kind = "provider_error";
|
|
1417
|
-
status;
|
|
1418
|
-
body;
|
|
1419
|
-
cause_kind;
|
|
1420
|
-
constructor(message, metadata = {}) {
|
|
1421
|
-
super(message);
|
|
1422
|
-
this.name = "provider_error";
|
|
1423
|
-
this.status = metadata.status;
|
|
1424
|
-
this.body = metadata.body;
|
|
1425
|
-
this.cause_kind = metadata.cause_kind;
|
|
1426
|
-
}
|
|
1427
|
-
};
|
|
1428
|
-
/**
|
|
1429
|
-
* Turn-timeout expiry. Thrown by the engine-owned retry_turn wrapper when a
|
|
1430
|
-
* single invoke_turn exceeds `turn_timeout_ms` before any chunk streamed.
|
|
1431
|
-
* Its `kind` is the RetryFailureKind wire value `'timeout'` (not the class
|
|
1432
|
-
* name) on purpose: that is the discriminant classify_retryable /
|
|
1433
|
-
* classify_provider_error already key on, so the shared classifier treats an
|
|
1434
|
-
* expiry as retryable without any timeout-specific branch. A mid-stream
|
|
1435
|
-
* expiry never reaches here; retry_turn converts it to a non-retryable
|
|
1436
|
-
* stream interruption first.
|
|
1437
|
-
*/
|
|
1438
|
-
var turn_timeout_error = class extends Error {
|
|
1439
|
-
kind = "timeout";
|
|
1440
|
-
timeout_ms;
|
|
1441
|
-
step_index;
|
|
1442
|
-
constructor(timeout_ms, step_index) {
|
|
1443
|
-
super(`turn exceeded turn_timeout_ms budget of ${timeout_ms}ms`);
|
|
1444
|
-
this.name = "turn_timeout_error";
|
|
1445
|
-
this.timeout_ms = timeout_ms;
|
|
1446
|
-
this.step_index = step_index;
|
|
1447
|
-
}
|
|
1448
|
-
};
|
|
1449
|
-
var schema_validation_error = class extends Error {
|
|
1450
|
-
kind = "schema_validation_error";
|
|
1451
|
-
zod_error;
|
|
1452
|
-
raw_text;
|
|
1453
|
-
constructor(message, zod_error, raw_text) {
|
|
1454
|
-
super(message);
|
|
1455
|
-
this.name = "schema_validation_error";
|
|
1456
|
-
this.zod_error = zod_error;
|
|
1457
|
-
this.raw_text = raw_text;
|
|
1458
|
-
}
|
|
1459
|
-
};
|
|
1460
|
-
var tool_error = class extends Error {
|
|
1461
|
-
kind = "tool_error";
|
|
1462
|
-
tool_name;
|
|
1463
|
-
tool_call_id;
|
|
1464
|
-
cause;
|
|
1465
|
-
constructor(message, metadata) {
|
|
1466
|
-
super(message);
|
|
1467
|
-
this.name = "tool_error";
|
|
1468
|
-
this.tool_name = metadata.tool_name;
|
|
1469
|
-
this.tool_call_id = metadata.tool_call_id;
|
|
1470
|
-
this.cause = metadata.cause;
|
|
1471
|
-
}
|
|
1472
|
-
};
|
|
1473
|
-
var tool_approval_denied_error = class extends Error {
|
|
1474
|
-
kind = "tool_approval_denied_error";
|
|
1475
|
-
tool_name;
|
|
1476
|
-
step_index;
|
|
1477
|
-
tool_call_id;
|
|
1478
|
-
constructor(message, metadata) {
|
|
1479
|
-
super(message);
|
|
1480
|
-
this.name = "tool_approval_denied_error";
|
|
1481
|
-
this.tool_name = metadata.tool_name;
|
|
1482
|
-
this.step_index = metadata.step_index;
|
|
1483
|
-
this.tool_call_id = metadata.tool_call_id;
|
|
1484
|
-
}
|
|
1485
|
-
};
|
|
1486
|
-
var model_required_error = class extends Error {
|
|
1487
|
-
kind = "model_required_error";
|
|
1488
|
-
constructor(message = "no model specified: pass `model` to generate() or set `defaults.model` on the engine") {
|
|
1489
|
-
super(message);
|
|
1490
|
-
this.name = "model_required_error";
|
|
1491
|
-
}
|
|
1492
|
-
};
|
|
1493
|
-
var provider_not_configured_error = class extends Error {
|
|
1494
|
-
kind = "provider_not_configured_error";
|
|
1495
|
-
provider;
|
|
1496
|
-
constructor(provider) {
|
|
1497
|
-
super(`provider '${provider}' is not configured on this engine`);
|
|
1498
|
-
this.name = "provider_not_configured_error";
|
|
1499
|
-
this.provider = provider;
|
|
1500
|
-
}
|
|
1501
|
-
};
|
|
1502
|
-
var engine_config_error = class extends Error {
|
|
1503
|
-
kind = "engine_config_error";
|
|
1504
|
-
provider;
|
|
1505
|
-
constructor(message, provider) {
|
|
1506
|
-
super(message);
|
|
1507
|
-
this.name = "engine_config_error";
|
|
1508
|
-
this.provider = provider;
|
|
1509
|
-
}
|
|
1510
|
-
};
|
|
1511
|
-
var on_chunk_error = class extends Error {
|
|
1512
|
-
kind = "on_chunk_error";
|
|
1513
|
-
cause;
|
|
1514
|
-
constructor(message, cause) {
|
|
1515
|
-
super(message);
|
|
1516
|
-
this.name = "on_chunk_error";
|
|
1517
|
-
this.cause = cause;
|
|
1518
|
-
}
|
|
1519
|
-
};
|
|
1520
|
-
var provider_capability_error = class extends Error {
|
|
1521
|
-
kind = "provider_capability_error";
|
|
1522
|
-
provider;
|
|
1523
|
-
capability;
|
|
1524
|
-
constructor(provider, capability, detail) {
|
|
1525
|
-
const suffix = detail !== void 0 ? `: ${detail}` : "";
|
|
1526
|
-
super(`provider '${provider}' does not support '${capability}'${suffix}`);
|
|
1527
|
-
this.name = "provider_capability_error";
|
|
1528
|
-
this.provider = provider;
|
|
1529
|
-
this.capability = capability;
|
|
1530
|
-
}
|
|
1531
|
-
};
|
|
1532
|
-
var engine_disposed_error = class extends Error {
|
|
1533
|
-
kind = "engine_disposed_error";
|
|
1534
|
-
constructor(message = "engine has been disposed; further calls are not permitted") {
|
|
1535
|
-
super(message);
|
|
1536
|
-
this.name = "engine_disposed_error";
|
|
1537
|
-
}
|
|
1538
|
-
};
|
|
1539
|
-
var claude_cli_error = class extends Error {
|
|
1540
|
-
kind = "claude_cli_error";
|
|
1541
|
-
reason;
|
|
1542
|
-
status;
|
|
1543
|
-
stderr_snippet;
|
|
1544
|
-
constructor(reason, message, metadata = {}) {
|
|
1545
|
-
super(message);
|
|
1546
|
-
this.name = "claude_cli_error";
|
|
1547
|
-
this.reason = reason;
|
|
1548
|
-
this.status = metadata.status;
|
|
1549
|
-
this.stderr_snippet = metadata.stderr_snippet;
|
|
1550
|
-
}
|
|
1551
|
-
};
|
|
1552
|
-
var provider_auth_error = class extends Error {
|
|
1553
|
-
kind = "provider_auth_error";
|
|
1554
|
-
provider;
|
|
1555
|
-
refresh_command;
|
|
1556
|
-
constructor(provider, message, metadata = {}) {
|
|
1557
|
-
super(message);
|
|
1558
|
-
this.name = "provider_auth_error";
|
|
1559
|
-
this.provider = provider;
|
|
1560
|
-
this.refresh_command = metadata.refresh_command;
|
|
1561
|
-
}
|
|
1562
|
-
};
|
|
1563
|
-
/**
|
|
1564
|
-
* A generate call that set a schema finished for a reason other than 'stop'
|
|
1565
|
-
* (content_filter, length, max_steps, tool_calls), so no schema-validated
|
|
1566
|
-
* value exists and validation was never attempted. Carries the raw model text
|
|
1567
|
-
* and the last provider-reported payload, so a guardrail trace or other
|
|
1568
|
-
* block detail stays reachable from the catch. Distinct from
|
|
1569
|
-
* schema_validation_error, which covers a response that completed normally
|
|
1570
|
-
* and then failed validation after its repair attempts ran out.
|
|
1571
|
-
*/
|
|
1572
|
-
var incomplete_generation_error = class extends Error {
|
|
1573
|
-
kind = "incomplete_generation_error";
|
|
1574
|
-
finish_reason;
|
|
1575
|
-
raw_text;
|
|
1576
|
-
provider_reported;
|
|
1577
|
-
constructor(finish_reason, raw_text, provider_reported) {
|
|
1578
|
-
super(`generation finished with '${finish_reason}' before a schema-validated response was produced`);
|
|
1579
|
-
this.name = "incomplete_generation_error";
|
|
1580
|
-
this.finish_reason = finish_reason;
|
|
1581
|
-
this.raw_text = raw_text;
|
|
1582
|
-
this.provider_reported = provider_reported;
|
|
1583
|
-
}
|
|
1584
|
-
};
|
|
1585
|
-
//#endregion
|
|
1586
1603
|
//#region src/engine/retry.ts
|
|
1587
1604
|
const DEFAULT_RETRY = {
|
|
1588
1605
|
max_attempts: 3,
|
|
@@ -1612,50 +1629,28 @@ function parse_retry_after(value) {
|
|
|
1612
1629
|
return Math.max(0, parsed - Date.now());
|
|
1613
1630
|
}
|
|
1614
1631
|
/**
|
|
1615
|
-
*
|
|
1616
|
-
*
|
|
1617
|
-
* Exponential backoff (`initial_delay_ms * 2^attempt`) plus up to one
|
|
1618
|
-
* `initial_delay_ms` of random jitter, capped at `max_delay_ms`. The jitter
|
|
1619
|
-
* spreads retries from concurrent callers apart so they don't all retry in
|
|
1620
|
-
* lockstep.
|
|
1632
|
+
* Check whether `policy` allows retrying a failure of the given `kind`.
|
|
1621
1633
|
*/
|
|
1622
|
-
function
|
|
1623
|
-
|
|
1624
|
-
const jitter = Math.random() * policy.initial_delay_ms;
|
|
1625
|
-
return Math.min(base + jitter, policy.max_delay_ms);
|
|
1634
|
+
function is_retryable(kind, policy) {
|
|
1635
|
+
return policy.retry_on.includes(kind);
|
|
1626
1636
|
}
|
|
1627
1637
|
/**
|
|
1628
|
-
*
|
|
1629
|
-
*
|
|
1638
|
+
* Map an abort reason onto the error this layer rejects with, always wrapping
|
|
1639
|
+
* so an abort can only ever surface as `aborted_error` with the caller's
|
|
1640
|
+
* reason attached.
|
|
1630
1641
|
*
|
|
1631
|
-
*
|
|
1642
|
+
* The engine's convention, and the opposite of core's propagate-verbatim rule.
|
|
1643
|
+
* Two things depend on the wrap. A bare `controller.abort()` sets
|
|
1644
|
+
* `signal.reason` to a `DOMException`, which is an `Error` on Node, so
|
|
1645
|
+
* propagating would leak a non-fascicle error out of a boundary documented as
|
|
1646
|
+
* throwing `aborted_error`. And the engine's other abort sites (`generate`,
|
|
1647
|
+
* `tool_loop`, the adapters) wrap in order to attach `step_index` and
|
|
1648
|
+
* `tool_call_in_flight`, which only the construction site knows; propagating
|
|
1649
|
+
* would discard them. Wrapping a reason that is already an `aborted_error`
|
|
1650
|
+
* nests one inside the other, which is the accepted cost of that contract.
|
|
1632
1651
|
*/
|
|
1633
|
-
function
|
|
1634
|
-
return new
|
|
1635
|
-
if (ms <= 0) {
|
|
1636
|
-
resolve();
|
|
1637
|
-
return;
|
|
1638
|
-
}
|
|
1639
|
-
if (abort?.aborted === true) {
|
|
1640
|
-
reject(new aborted_error("aborted", { reason: abort.reason }));
|
|
1641
|
-
return;
|
|
1642
|
-
}
|
|
1643
|
-
const timer = setTimeout(() => {
|
|
1644
|
-
if (abort !== void 0) abort.removeEventListener("abort", on_abort);
|
|
1645
|
-
resolve();
|
|
1646
|
-
}, ms);
|
|
1647
|
-
const on_abort = () => {
|
|
1648
|
-
clearTimeout(timer);
|
|
1649
|
-
reject(new aborted_error("aborted", { reason: abort?.reason }));
|
|
1650
|
-
};
|
|
1651
|
-
abort?.addEventListener("abort", on_abort, { once: true });
|
|
1652
|
-
});
|
|
1653
|
-
}
|
|
1654
|
-
/**
|
|
1655
|
-
* Check whether `policy` allows retrying a failure of the given `kind`.
|
|
1656
|
-
*/
|
|
1657
|
-
function is_retryable(kind, policy) {
|
|
1658
|
-
return policy.retry_on.includes(kind);
|
|
1652
|
+
function to_abort_error(reason) {
|
|
1653
|
+
return new aborted_error("aborted", { reason });
|
|
1659
1654
|
}
|
|
1660
1655
|
/**
|
|
1661
1656
|
* Retry `fn` under `policy`. `fn` must throw a RetryableError-shaped object to
|
|
@@ -1667,10 +1662,16 @@ function is_retryable(kind, policy) {
|
|
|
1667
1662
|
* 5xx/network/timeout).
|
|
1668
1663
|
*/
|
|
1669
1664
|
async function retry_with_policy(fn, policy = DEFAULT_RETRY, abort) {
|
|
1665
|
+
const backoff_policy = {
|
|
1666
|
+
initial_delay_ms: policy.initial_delay_ms,
|
|
1667
|
+
max_delay_ms: policy.max_delay_ms,
|
|
1668
|
+
jitter: true
|
|
1669
|
+
};
|
|
1670
|
+
const signal = abort ?? new AbortController().signal;
|
|
1670
1671
|
let attempt = 0;
|
|
1671
1672
|
let last_rate_limit_after;
|
|
1672
1673
|
while (true) {
|
|
1673
|
-
if (abort?.aborted === true) throw
|
|
1674
|
+
if (abort?.aborted === true) throw to_abort_error(abort.reason);
|
|
1674
1675
|
try {
|
|
1675
1676
|
return await fn(attempt);
|
|
1676
1677
|
} catch (err) {
|
|
@@ -1693,12 +1694,12 @@ async function retry_with_policy(fn, policy = DEFAULT_RETRY, abort) {
|
|
|
1693
1694
|
}
|
|
1694
1695
|
throw new provider_error(retryable.message ?? `${retryable.kind} after ${attempt} attempts`, metadata);
|
|
1695
1696
|
}
|
|
1696
|
-
let delay = compute_backoff(
|
|
1697
|
+
let delay = compute_backoff(backoff_policy, attempt - 1);
|
|
1697
1698
|
if (retryable.kind === "rate_limit" && retryable.retry_after_ms !== void 0) {
|
|
1698
1699
|
delay = Math.max(delay, retryable.retry_after_ms);
|
|
1699
1700
|
last_rate_limit_after = retryable.retry_after_ms;
|
|
1700
1701
|
}
|
|
1701
|
-
await
|
|
1702
|
+
await wait_with_abort(delay, signal, to_abort_error);
|
|
1702
1703
|
}
|
|
1703
1704
|
}
|
|
1704
1705
|
}
|
|
@@ -1755,58 +1756,6 @@ function classify_retryable(err) {
|
|
|
1755
1756
|
}
|
|
1756
1757
|
}
|
|
1757
1758
|
//#endregion
|
|
1758
|
-
//#region src/engine/providers/types.ts
|
|
1759
|
-
/**
|
|
1760
|
-
* Read the `transport` selector off a provider init. Defaults to 'ai_sdk';
|
|
1761
|
-
* anything but the two known backends throws at construction so a typo
|
|
1762
|
-
* fails loud instead of silently running the default.
|
|
1763
|
-
*/
|
|
1764
|
-
function resolve_transport(init, provider) {
|
|
1765
|
-
const raw = init["transport"];
|
|
1766
|
-
if (raw === void 0 || raw === "ai_sdk") return "ai_sdk";
|
|
1767
|
-
if (raw === "native") return "native";
|
|
1768
|
-
throw new engine_config_error(`${provider} provider: transport must be 'ai_sdk' or 'native', got ${JSON.stringify(raw)}`, provider);
|
|
1769
|
-
}
|
|
1770
|
-
/**
|
|
1771
|
-
* Map raw provider usage to UsageTotals, the mapper most adapters share.
|
|
1772
|
-
*
|
|
1773
|
-
* Missing usage zeroes the totals. Optional fields (reasoning and cache
|
|
1774
|
-
* tokens) are set only when the raw payload carries them, reading the
|
|
1775
|
-
* flattened field first and the AI SDK's *_details container second.
|
|
1776
|
-
*/
|
|
1777
|
-
function default_normalize_usage(raw) {
|
|
1778
|
-
if (raw === void 0) return {
|
|
1779
|
-
input_tokens: 0,
|
|
1780
|
-
output_tokens: 0
|
|
1781
|
-
};
|
|
1782
|
-
const totals = {
|
|
1783
|
-
input_tokens: typeof raw.input_tokens === "number" ? raw.input_tokens : 0,
|
|
1784
|
-
output_tokens: typeof raw.output_tokens === "number" ? raw.output_tokens : 0
|
|
1785
|
-
};
|
|
1786
|
-
if (typeof raw.reasoning_tokens === "number") totals.reasoning_tokens = raw.reasoning_tokens;
|
|
1787
|
-
else if (typeof raw.output_token_details?.reasoning_tokens === "number") totals.reasoning_tokens = raw.output_token_details.reasoning_tokens;
|
|
1788
|
-
if (typeof raw.cached_input_tokens === "number") totals.cached_input_tokens = raw.cached_input_tokens;
|
|
1789
|
-
else if (typeof raw.input_token_details?.cached_tokens === "number") totals.cached_input_tokens = raw.input_token_details.cached_tokens;
|
|
1790
|
-
if (typeof raw.cache_write_tokens === "number") totals.cache_write_tokens = raw.cache_write_tokens;
|
|
1791
|
-
else if (typeof raw.input_token_details?.cache_creation_input_tokens === "number") totals.cache_write_tokens = raw.input_token_details.cache_creation_input_tokens;
|
|
1792
|
-
return totals;
|
|
1793
|
-
}
|
|
1794
|
-
/**
|
|
1795
|
-
* Dynamically import an optional peer dependency, rethrowing an import
|
|
1796
|
-
* failure as an error that names the package and the install command so a
|
|
1797
|
-
* missing peer is diagnosable from the message alone.
|
|
1798
|
-
*/
|
|
1799
|
-
async function load_optional_peer(specifier) {
|
|
1800
|
-
let mod;
|
|
1801
|
-
try {
|
|
1802
|
-
mod = await import(specifier);
|
|
1803
|
-
} catch (err) {
|
|
1804
|
-
const message = `missing peer dependency '${specifier}'. Install it with: pnpm add ${specifier}. Cause: ${err instanceof Error ? err.message : JSON.stringify(err)}`;
|
|
1805
|
-
throw new Error(message, { cause: err });
|
|
1806
|
-
}
|
|
1807
|
-
return mod;
|
|
1808
|
-
}
|
|
1809
|
-
//#endregion
|
|
1810
1759
|
//#region src/engine/providers/sse_native.ts
|
|
1811
1760
|
/**
|
|
1812
1761
|
* Incremental SSE decoder shared by the native (raw-HTTP) provider adapters.
|
|
@@ -2002,13 +1951,13 @@ function to_anthropic_messages(messages) {
|
|
|
2002
1951
|
}
|
|
2003
1952
|
/**
|
|
2004
1953
|
* Map fascicle tool definitions to the Messages-API tool shape, converting
|
|
2005
|
-
* each
|
|
1954
|
+
* each input schema to JSON Schema.
|
|
2006
1955
|
*/
|
|
2007
1956
|
function to_anthropic_tools(tools) {
|
|
2008
1957
|
return tools.map((tool) => ({
|
|
2009
1958
|
name: tool.name,
|
|
2010
1959
|
description: tool.description,
|
|
2011
|
-
input_schema:
|
|
1960
|
+
input_schema: to_json_schema(tool.input_schema)
|
|
2012
1961
|
}));
|
|
2013
1962
|
}
|
|
2014
1963
|
/**
|
|
@@ -2459,12 +2408,13 @@ const create_anthropic_native_adapter = (init) => {
|
|
|
2459
2408
|
/**
|
|
2460
2409
|
* Map an effort level to Anthropic's extended-thinking provider options.
|
|
2461
2410
|
*
|
|
2462
|
-
* `none`
|
|
2463
|
-
* explicit "disabled" value, so non-reasoning models see a plain
|
|
2411
|
+
* A zero budget (which is what `none` maps to) sends no thinking config at all
|
|
2412
|
+
* rather than an explicit "disabled" value, so non-reasoning models see a plain
|
|
2413
|
+
* request.
|
|
2464
2414
|
*/
|
|
2465
2415
|
function translate_anthropic_effort(effort) {
|
|
2466
2416
|
const budget = ANTHROPIC_THINKING_BUDGETS[effort];
|
|
2467
|
-
if (
|
|
2417
|
+
if (budget === 0) return {
|
|
2468
2418
|
provider_options: {},
|
|
2469
2419
|
effort_ignored: false
|
|
2470
2420
|
};
|
|
@@ -2642,7 +2592,7 @@ function to_chat_messages(messages, provider) {
|
|
|
2642
2592
|
}
|
|
2643
2593
|
/**
|
|
2644
2594
|
* Map fascicle Tool[] to the Chat Completions function-tool shape,
|
|
2645
|
-
* converting each
|
|
2595
|
+
* converting each input schema to JSON Schema.
|
|
2646
2596
|
*/
|
|
2647
2597
|
function to_chat_tools(tools) {
|
|
2648
2598
|
return tools.map((tool) => ({
|
|
@@ -2650,7 +2600,7 @@ function to_chat_tools(tools) {
|
|
|
2650
2600
|
function: {
|
|
2651
2601
|
name: tool.name,
|
|
2652
2602
|
description: tool.description,
|
|
2653
|
-
parameters:
|
|
2603
|
+
parameters: to_json_schema(tool.input_schema)
|
|
2654
2604
|
}
|
|
2655
2605
|
}));
|
|
2656
2606
|
}
|
|
@@ -4583,114 +4533,172 @@ async function* build_stdout_lines(child, on_data) {
|
|
|
4583
4533
|
}
|
|
4584
4534
|
//#endregion
|
|
4585
4535
|
//#region src/engine/providers/claude_cli/stream_parse.ts
|
|
4536
|
+
/** A non-null, non-array object: the shape every CLI event and sub-object takes. */
|
|
4537
|
+
function is_record(value) {
|
|
4538
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4539
|
+
}
|
|
4540
|
+
/** `key` is absent (or `undefined`), or holds a string. */
|
|
4541
|
+
function opt_string(rec, key) {
|
|
4542
|
+
const value = rec[key];
|
|
4543
|
+
return value === void 0 || typeof value === "string";
|
|
4544
|
+
}
|
|
4545
|
+
/** `key` is absent (or `undefined`), or holds a number. */
|
|
4546
|
+
function opt_number(rec, key) {
|
|
4547
|
+
const value = rec[key];
|
|
4548
|
+
return value === void 0 || typeof value === "number";
|
|
4549
|
+
}
|
|
4550
|
+
/** `key` is absent (or `undefined`), or holds a boolean. */
|
|
4551
|
+
function opt_boolean(rec, key) {
|
|
4552
|
+
const value = rec[key];
|
|
4553
|
+
return value === void 0 || typeof value === "boolean";
|
|
4554
|
+
}
|
|
4586
4555
|
/**
|
|
4587
|
-
*
|
|
4588
|
-
*
|
|
4589
|
-
*
|
|
4590
|
-
|
|
4591
|
-
|
|
4592
|
-
|
|
4593
|
-
|
|
4594
|
-
|
|
4595
|
-
|
|
4596
|
-
|
|
4597
|
-
*
|
|
4598
|
-
*
|
|
4599
|
-
*
|
|
4600
|
-
|
|
4601
|
-
|
|
4602
|
-
|
|
4603
|
-
|
|
4604
|
-
|
|
4605
|
-
|
|
4606
|
-
|
|
4607
|
-
*
|
|
4608
|
-
*
|
|
4609
|
-
*
|
|
4610
|
-
|
|
4611
|
-
|
|
4612
|
-
|
|
4613
|
-
|
|
4614
|
-
|
|
4615
|
-
|
|
4616
|
-
|
|
4617
|
-
const
|
|
4618
|
-
|
|
4619
|
-
|
|
4620
|
-
|
|
4621
|
-
|
|
4622
|
-
|
|
4623
|
-
|
|
4624
|
-
|
|
4625
|
-
|
|
4626
|
-
|
|
4627
|
-
|
|
4628
|
-
|
|
4629
|
-
|
|
4630
|
-
|
|
4631
|
-
|
|
4632
|
-
|
|
4633
|
-
}
|
|
4634
|
-
const assistant_content_array_schema = z.array(z.unknown()).transform((arr) => {
|
|
4635
|
-
const out = [];
|
|
4636
|
-
for (const entry of arr) {
|
|
4637
|
-
const result = assistant_content_part_schema.safeParse(entry);
|
|
4638
|
-
if (result.success) out.push(result.data);
|
|
4556
|
+
* The optional `usage` sub-object of a `result` event. Absent is fine; a present
|
|
4557
|
+
* value must be an object whose known token counts are numbers when given. A
|
|
4558
|
+
* `null`, an array, or a mistyped count rejects the event.
|
|
4559
|
+
*/
|
|
4560
|
+
function is_valid_usage(value) {
|
|
4561
|
+
if (value === void 0) return true;
|
|
4562
|
+
if (!is_record(value)) return false;
|
|
4563
|
+
return opt_number(value, "input_tokens") && opt_number(value, "output_tokens") && opt_number(value, "cache_read_input_tokens") && opt_number(value, "cache_creation_input_tokens");
|
|
4564
|
+
}
|
|
4565
|
+
/**
|
|
4566
|
+
* The optional `rate_limit_info` sub-object of a `rate_limit_event`, under the
|
|
4567
|
+
* same rule as usage: absent is fine, a present value is validated field by
|
|
4568
|
+
* field or the event is rejected.
|
|
4569
|
+
*/
|
|
4570
|
+
function is_valid_rate_limit_info(value) {
|
|
4571
|
+
if (value === void 0) return true;
|
|
4572
|
+
if (!is_record(value)) return false;
|
|
4573
|
+
return opt_string(value, "status") && opt_number(value, "resetsAt") && opt_string(value, "rateLimitType") && opt_string(value, "overageStatus") && opt_number(value, "overageResetsAt") && opt_boolean(value, "isUsingOverage");
|
|
4574
|
+
}
|
|
4575
|
+
/**
|
|
4576
|
+
* Recognize one `assistant` content entry, or `undefined` for anything
|
|
4577
|
+
* unrecognized. `text` needs a string `text`; `tool_use` needs string `id` and
|
|
4578
|
+
* `name` and carries its opaque `input` through (absent `input` becomes
|
|
4579
|
+
* `undefined`, one deliberate tolerance the old `z.unknown()` did not grant).
|
|
4580
|
+
* Callers drop the `undefined` results, so an unknown entry type never rejects
|
|
4581
|
+
* the surrounding event.
|
|
4582
|
+
*/
|
|
4583
|
+
function as_assistant_part(value) {
|
|
4584
|
+
if (!is_record(value)) return void 0;
|
|
4585
|
+
if (value["type"] === "text") {
|
|
4586
|
+
const text = value["text"];
|
|
4587
|
+
return typeof text === "string" ? {
|
|
4588
|
+
type: "text",
|
|
4589
|
+
text
|
|
4590
|
+
} : void 0;
|
|
4591
|
+
}
|
|
4592
|
+
if (value["type"] === "tool_use") {
|
|
4593
|
+
const id = value["id"];
|
|
4594
|
+
const name = value["name"];
|
|
4595
|
+
if (typeof id !== "string") return void 0;
|
|
4596
|
+
if (typeof name !== "string") return void 0;
|
|
4597
|
+
return {
|
|
4598
|
+
type: "tool_use",
|
|
4599
|
+
id,
|
|
4600
|
+
name,
|
|
4601
|
+
input: value["input"]
|
|
4602
|
+
};
|
|
4639
4603
|
}
|
|
4640
|
-
|
|
4641
|
-
|
|
4642
|
-
|
|
4643
|
-
|
|
4644
|
-
|
|
4645
|
-
|
|
4646
|
-
|
|
4604
|
+
}
|
|
4605
|
+
/**
|
|
4606
|
+
* Recognize one `user` content entry as a tool result, or `undefined`. Requires
|
|
4607
|
+
* a string `tool_use_id`; `is_error` must be boolean when present, and a present
|
|
4608
|
+
* `content` of any shape is carried through. Callers drop the `undefined`
|
|
4609
|
+
* results.
|
|
4610
|
+
*/
|
|
4611
|
+
function as_user_part(value) {
|
|
4612
|
+
if (!is_record(value)) return void 0;
|
|
4613
|
+
if (value["type"] !== "tool_result") return void 0;
|
|
4614
|
+
const tool_use_id = value["tool_use_id"];
|
|
4615
|
+
if (typeof tool_use_id !== "string") return void 0;
|
|
4616
|
+
if (!opt_boolean(value, "is_error")) return void 0;
|
|
4617
|
+
const part = {
|
|
4618
|
+
type: "tool_result",
|
|
4619
|
+
tool_use_id
|
|
4620
|
+
};
|
|
4621
|
+
if ("content" in value) part.content = value["content"];
|
|
4622
|
+
const is_error = value["is_error"];
|
|
4623
|
+
if (typeof is_error === "boolean") part.is_error = is_error;
|
|
4624
|
+
return part;
|
|
4625
|
+
}
|
|
4626
|
+
/**
|
|
4627
|
+
* Collect the valid parts of an `assistant`/`user` content array. Returns
|
|
4628
|
+
* `undefined` (rejecting the event) when `value` is not an array; otherwise maps
|
|
4629
|
+
* each entry through `recognize` and keeps the ones it accepts.
|
|
4630
|
+
*/
|
|
4631
|
+
function collect_parts(value, recognize) {
|
|
4632
|
+
if (!Array.isArray(value)) return void 0;
|
|
4633
|
+
const parts = [];
|
|
4634
|
+
for (const entry of value) {
|
|
4635
|
+
const part = recognize(entry);
|
|
4636
|
+
if (part !== void 0) parts.push(part);
|
|
4647
4637
|
}
|
|
4648
|
-
return
|
|
4649
|
-
}
|
|
4650
|
-
|
|
4651
|
-
|
|
4652
|
-
subtype
|
|
4653
|
-
|
|
4654
|
-
|
|
4655
|
-
|
|
4656
|
-
|
|
4657
|
-
|
|
4658
|
-
|
|
4659
|
-
|
|
4660
|
-
|
|
4661
|
-
|
|
4662
|
-
|
|
4663
|
-
|
|
4664
|
-
|
|
4665
|
-
|
|
4666
|
-
|
|
4667
|
-
|
|
4668
|
-
|
|
4669
|
-
|
|
4670
|
-
|
|
4671
|
-
|
|
4672
|
-
|
|
4673
|
-
|
|
4674
|
-
|
|
4675
|
-
|
|
4676
|
-
|
|
4677
|
-
|
|
4678
|
-
|
|
4679
|
-
|
|
4680
|
-
|
|
4681
|
-
}
|
|
4682
|
-
|
|
4683
|
-
|
|
4684
|
-
|
|
4685
|
-
|
|
4686
|
-
|
|
4687
|
-
const
|
|
4688
|
-
|
|
4689
|
-
|
|
4690
|
-
|
|
4691
|
-
|
|
4692
|
-
|
|
4693
|
-
|
|
4638
|
+
return parts;
|
|
4639
|
+
}
|
|
4640
|
+
/** A `system` event whose optional metadata fields type-check. */
|
|
4641
|
+
function is_system_event(raw) {
|
|
4642
|
+
return opt_string(raw, "subtype") && opt_string(raw, "session_id") && opt_string(raw, "model");
|
|
4643
|
+
}
|
|
4644
|
+
/** A `result` event whose optional fields and `usage` sub-object type-check. */
|
|
4645
|
+
function is_result_event(raw) {
|
|
4646
|
+
return opt_string(raw, "subtype") && opt_string(raw, "session_id") && opt_number(raw, "total_cost_usd") && opt_number(raw, "duration_ms") && opt_boolean(raw, "is_error") && is_valid_usage(raw["usage"]) && opt_string(raw, "result");
|
|
4647
|
+
}
|
|
4648
|
+
/** A `rate_limit_event` whose `rate_limit_info` sub-object and `session_id` type-check. */
|
|
4649
|
+
function is_rate_limit_event(raw) {
|
|
4650
|
+
return is_valid_rate_limit_info(raw["rate_limit_info"]) && opt_string(raw, "session_id");
|
|
4651
|
+
}
|
|
4652
|
+
/**
|
|
4653
|
+
* Recognize a pass-through event in place: hand the original object back (extra
|
|
4654
|
+
* keys and all, since nothing downstream reads them) when `recognize` accepts
|
|
4655
|
+
* it, or `undefined` to reject. Narrowing keeps this cast-free.
|
|
4656
|
+
*/
|
|
4657
|
+
function gate(raw, recognize) {
|
|
4658
|
+
return recognize(raw) ? raw : void 0;
|
|
4659
|
+
}
|
|
4660
|
+
/**
|
|
4661
|
+
* Normalize an `assistant` event: its `message` must be an object and its
|
|
4662
|
+
* `content` an array, whose recognized parts are kept and the rest dropped.
|
|
4663
|
+
*/
|
|
4664
|
+
function as_assistant_event(raw) {
|
|
4665
|
+
const message = raw["message"];
|
|
4666
|
+
if (!is_record(message)) return void 0;
|
|
4667
|
+
const content = collect_parts(message["content"], as_assistant_part);
|
|
4668
|
+
return content === void 0 ? void 0 : {
|
|
4669
|
+
type: "assistant",
|
|
4670
|
+
message: { content }
|
|
4671
|
+
};
|
|
4672
|
+
}
|
|
4673
|
+
/** Normalize a `user` event, under the same rule as `as_assistant_event`. */
|
|
4674
|
+
function as_user_event(raw) {
|
|
4675
|
+
const message = raw["message"];
|
|
4676
|
+
if (!is_record(message)) return void 0;
|
|
4677
|
+
const content = collect_parts(message["content"], as_user_part);
|
|
4678
|
+
return content === void 0 ? void 0 : {
|
|
4679
|
+
type: "user",
|
|
4680
|
+
message: { content }
|
|
4681
|
+
};
|
|
4682
|
+
}
|
|
4683
|
+
/**
|
|
4684
|
+
* The wire gate: validate a parsed JSON value against the recognized CLI event
|
|
4685
|
+
* shapes, returning the typed event or `undefined` on any mismatch.
|
|
4686
|
+
*
|
|
4687
|
+
* `assistant` and `user` are normalized (their content arrays are filtered down
|
|
4688
|
+
* to the recognized parts); the pass-through events are recognized in place, so
|
|
4689
|
+
* unknown keys survive but are never read.
|
|
4690
|
+
*/
|
|
4691
|
+
function as_event(raw) {
|
|
4692
|
+
if (!is_record(raw)) return void 0;
|
|
4693
|
+
switch (raw["type"]) {
|
|
4694
|
+
case "system": return gate(raw, is_system_event);
|
|
4695
|
+
case "assistant": return as_assistant_event(raw);
|
|
4696
|
+
case "user": return as_user_event(raw);
|
|
4697
|
+
case "result": return gate(raw, is_result_event);
|
|
4698
|
+
case "rate_limit_event": return gate(raw, is_rate_limit_event);
|
|
4699
|
+
default: return;
|
|
4700
|
+
}
|
|
4701
|
+
}
|
|
4694
4702
|
/**
|
|
4695
4703
|
* Build a fresh, empty `ParserState` for one CLI invocation.
|
|
4696
4704
|
*/
|
|
@@ -4801,14 +4809,6 @@ function flush_turn(state) {
|
|
|
4801
4809
|
};
|
|
4802
4810
|
}
|
|
4803
4811
|
/**
|
|
4804
|
-
* Validate a parsed JSON value against `cli_event_schema`, returning
|
|
4805
|
-
* `undefined` instead of throwing on a mismatch.
|
|
4806
|
-
*/
|
|
4807
|
-
function as_event(raw) {
|
|
4808
|
-
const result = cli_event_schema.safeParse(raw);
|
|
4809
|
-
return result.success ? result.data : void 0;
|
|
4810
|
-
}
|
|
4811
|
-
/**
|
|
4812
4812
|
* Append a `StreamChunk` to the output array and, if the caller passed an
|
|
4813
4813
|
* `on_chunk` dispatcher, await its delivery too.
|
|
4814
4814
|
*/
|
|
@@ -5278,6 +5278,14 @@ function build_generate_result(input) {
|
|
|
5278
5278
|
//#endregion
|
|
5279
5279
|
//#region src/engine/schema.ts
|
|
5280
5280
|
/**
|
|
5281
|
+
* Schema parse and repair helpers.
|
|
5282
|
+
*
|
|
5283
|
+
* The tool-loop / generate orchestrator owns the repair loop itself (counting
|
|
5284
|
+
* attempts toward `max_steps`, re-dispatching through the provider). This
|
|
5285
|
+
* module exposes only the parse primitive and the canonical repair-prompt
|
|
5286
|
+
* shape.
|
|
5287
|
+
*/
|
|
5288
|
+
/**
|
|
5281
5289
|
* Attempt to parse `text` as JSON and validate it with `schema`. Returns a
|
|
5282
5290
|
* tagged union; the caller decides whether to escalate to repair, throw, or
|
|
5283
5291
|
* surface the failure.
|
|
@@ -5288,35 +5296,39 @@ function build_generate_result(input) {
|
|
|
5288
5296
|
* block in the text, and the outermost {…} / […] slice. The first candidate
|
|
5289
5297
|
* that both parses as JSON and matches the schema wins.
|
|
5290
5298
|
*
|
|
5291
|
-
* When every candidate fails, we prefer the schema-validation
|
|
5292
|
-
* FIRST candidate that parsed as JSON:
|
|
5293
|
-
* primary output and
|
|
5294
|
-
* candidates (e.g. the bracket-slice fallback) often produce noisy
|
|
5299
|
+
* When every candidate fails, we prefer the schema-validation issues from the
|
|
5300
|
+
* FIRST candidate that parsed as JSON: those issues reflect the model's
|
|
5301
|
+
* primary output and are what a repair prompt should feed back. Later
|
|
5302
|
+
* candidates (e.g. the bracket-slice fallback) often produce noisy issues
|
|
5295
5303
|
* like "expected object, received array" that misdirect the model. Only when
|
|
5296
5304
|
* NO candidate parses as JSON do we surface the JSON parse error.
|
|
5305
|
+
*
|
|
5306
|
+
* Async because Standard Schema permits a vendor's `validate` to return a
|
|
5307
|
+
* promise; the ripple stops at generate.ts and the claude_cli adapter, both of
|
|
5308
|
+
* which already await their surrounding work.
|
|
5297
5309
|
*/
|
|
5298
|
-
function parse_with_schema(schema, text) {
|
|
5310
|
+
async function parse_with_schema(schema, text) {
|
|
5299
5311
|
const candidates = json_candidates(text);
|
|
5300
|
-
let
|
|
5301
|
-
let
|
|
5312
|
+
let json_parse_issues = [{ message: "No JSON-parseable content found in model output" }];
|
|
5313
|
+
let first_schema_issues;
|
|
5302
5314
|
for (const candidate of candidates) {
|
|
5303
5315
|
let parsed;
|
|
5304
5316
|
try {
|
|
5305
5317
|
parsed = JSON.parse(candidate);
|
|
5306
5318
|
} catch (err) {
|
|
5307
|
-
|
|
5319
|
+
json_parse_issues = [{ message: err instanceof Error ? err.message : String(err) }];
|
|
5308
5320
|
continue;
|
|
5309
5321
|
}
|
|
5310
|
-
const result = schema
|
|
5311
|
-
if (result.
|
|
5322
|
+
const result = await validate_schema(schema, parsed);
|
|
5323
|
+
if (result.ok) return {
|
|
5312
5324
|
ok: true,
|
|
5313
|
-
value: result.
|
|
5325
|
+
value: result.value
|
|
5314
5326
|
};
|
|
5315
|
-
|
|
5327
|
+
first_schema_issues ??= result.issues;
|
|
5316
5328
|
}
|
|
5317
5329
|
return {
|
|
5318
5330
|
ok: false,
|
|
5319
|
-
|
|
5331
|
+
issues: first_schema_issues ?? json_parse_issues
|
|
5320
5332
|
};
|
|
5321
5333
|
}
|
|
5322
5334
|
const FENCE_BLOCK = /```(?:[\w-]*)\s*\n?([\s\S]*?)\n?```/g;
|
|
@@ -5360,10 +5372,10 @@ function slice_outermost(text, open, close) {
|
|
|
5360
5372
|
* The wording is fixed rather than adapter-specific, so the repair prompt
|
|
5361
5373
|
* stays predictable and is easy to read back from trajectory output.
|
|
5362
5374
|
*/
|
|
5363
|
-
function build_repair_message(
|
|
5375
|
+
function build_repair_message(issues) {
|
|
5364
5376
|
return {
|
|
5365
5377
|
role: "user",
|
|
5366
|
-
content: build_repair_prompt_text(
|
|
5378
|
+
content: build_repair_prompt_text(issues)
|
|
5367
5379
|
};
|
|
5368
5380
|
}
|
|
5369
5381
|
/**
|
|
@@ -5371,36 +5383,15 @@ function build_repair_message(zod_error) {
|
|
|
5371
5383
|
* a single stdin string rather than a Message. Same wording as
|
|
5372
5384
|
* build_repair_message so the on-wire instruction is identical across providers.
|
|
5373
5385
|
*/
|
|
5374
|
-
function build_repair_prompt_text(
|
|
5375
|
-
return `Your previous response did not match the expected schema. Error: ${
|
|
5386
|
+
function build_repair_prompt_text(issues) {
|
|
5387
|
+
return `Your previous response did not match the expected schema. Error: ${format_schema_issues(issues)}. Please provide a corrected response that strictly conforms to the schema. Return ONLY the JSON value: no markdown code fences, no surrounding prose, no commentary.`;
|
|
5376
5388
|
}
|
|
5377
5389
|
/**
|
|
5378
|
-
* Throw schema_validation_error carrying the
|
|
5390
|
+
* Throw schema_validation_error carrying the schema issues and the raw model text.
|
|
5379
5391
|
* Called by the orchestrator when all repair attempts are exhausted.
|
|
5380
5392
|
*/
|
|
5381
|
-
function throw_schema_validation(
|
|
5382
|
-
throw new schema_validation_error(`schema validation failed: ${
|
|
5383
|
-
}
|
|
5384
|
-
/**
|
|
5385
|
-
* Render an unknown error value as a display string.
|
|
5386
|
-
*
|
|
5387
|
-
* Handles `Error` instances, plain strings, and objects with a string
|
|
5388
|
-
* `message` directly; falls back to `JSON.stringify`, and finally to a fixed
|
|
5389
|
-
* placeholder if even that throws.
|
|
5390
|
-
*/
|
|
5391
|
-
function format_zod_error(err) {
|
|
5392
|
-
if (err === null || err === void 0) return "unknown error";
|
|
5393
|
-
if (typeof err === "string") return err;
|
|
5394
|
-
if (err instanceof Error) return err.message;
|
|
5395
|
-
if (typeof err === "object" && "message" in err) {
|
|
5396
|
-
const msg = err.message;
|
|
5397
|
-
if (typeof msg === "string") return msg;
|
|
5398
|
-
}
|
|
5399
|
-
try {
|
|
5400
|
-
return JSON.stringify(err) ?? "unknown error";
|
|
5401
|
-
} catch {
|
|
5402
|
-
return "unknown error";
|
|
5403
|
-
}
|
|
5393
|
+
function throw_schema_validation(issues, raw_text) {
|
|
5394
|
+
throw new schema_validation_error(`schema validation failed: ${format_schema_issues(issues)}`, issues, raw_text);
|
|
5404
5395
|
}
|
|
5405
5396
|
//#endregion
|
|
5406
5397
|
//#region src/engine/trajectory.ts
|
|
@@ -5558,16 +5549,16 @@ function record_effort_ignored(trajectory, model_id) {
|
|
|
5558
5549
|
}
|
|
5559
5550
|
/**
|
|
5560
5551
|
* Emit a structured event when a schema-driven generate call returns text
|
|
5561
|
-
* that fails parse +
|
|
5562
|
-
* formatted
|
|
5563
|
-
* without needing stdout.
|
|
5552
|
+
* that fails parse + schema validation. Persists the raw model output and a
|
|
5553
|
+
* formatted schema issues summary to the trajectory so the failure is
|
|
5554
|
+
* debuggable without needing stdout.
|
|
5564
5555
|
*/
|
|
5565
5556
|
function record_schema_validation_failed(trajectory, meta) {
|
|
5566
5557
|
if (trajectory === void 0) return;
|
|
5567
5558
|
trajectory.record({
|
|
5568
5559
|
kind: "schema_validation_failed",
|
|
5569
5560
|
attempt: meta.attempt,
|
|
5570
|
-
|
|
5561
|
+
schema_issues: meta.schema_issues,
|
|
5571
5562
|
raw_text: meta.raw_text
|
|
5572
5563
|
});
|
|
5573
5564
|
}
|
|
@@ -5723,12 +5714,14 @@ function extract_system_text(prompt) {
|
|
|
5723
5714
|
for (const m of prompt) if (m.role === "system") return m.content;
|
|
5724
5715
|
}
|
|
5725
5716
|
/**
|
|
5726
|
-
* Serialize a
|
|
5727
|
-
*
|
|
5717
|
+
* Serialize a schema to the JSON Schema string the CLI's `--json-schema`
|
|
5718
|
+
* flag expects.
|
|
5719
|
+
*
|
|
5720
|
+
* `strip_meta` is what makes this CLI-specific: `claude --json-schema`
|
|
5721
|
+
* rejects a top-level `$schema`/`$id`, which every other provider tolerates.
|
|
5728
5722
|
*/
|
|
5729
5723
|
function compile_schema(schema) {
|
|
5730
|
-
|
|
5731
|
-
return JSON.stringify(json);
|
|
5724
|
+
return JSON.stringify(to_json_schema(schema, { strip_meta: true }));
|
|
5732
5725
|
}
|
|
5733
5726
|
/**
|
|
5734
5727
|
* Turn a closed CLI subprocess's exit code, signal, and stderr into an
|
|
@@ -5905,26 +5898,26 @@ function create_claude_cli_adapter(init) {
|
|
|
5905
5898
|
const max_repairs = opts.schema_repair_attempts ?? 1;
|
|
5906
5899
|
let repairs_done = 0;
|
|
5907
5900
|
for (;;) {
|
|
5908
|
-
const attempt = parse_with_schema(opts.schema, parsed.final_text);
|
|
5901
|
+
const attempt = await parse_with_schema(opts.schema, parsed.final_text);
|
|
5909
5902
|
if (attempt.ok) {
|
|
5910
5903
|
parsed_content = attempt.value;
|
|
5911
5904
|
break;
|
|
5912
5905
|
}
|
|
5913
5906
|
record_schema_validation_failed(trajectory, {
|
|
5914
5907
|
attempt: repairs_done === 0 ? "initial" : "repair",
|
|
5915
|
-
|
|
5908
|
+
schema_issues: format_schema_issues(attempt.issues),
|
|
5916
5909
|
raw_text: parsed.final_text
|
|
5917
5910
|
});
|
|
5918
|
-
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.
|
|
5911
|
+
if (repairs_done >= max_repairs) throw new schema_validation_error(max_repairs === 0 ? "schema validation failed and repair is disabled (schema_repair_attempts: 0)" : `schema validation failed after ${String(max_repairs)} repair attempt${max_repairs === 1 ? "" : "s"}`, attempt.issues, parsed.final_text);
|
|
5919
5912
|
const repair_session_id = parsed.session_id;
|
|
5920
|
-
if (repair_session_id === void 0) throw new schema_validation_error("schema validation failed and no session_id available for repair", attempt.
|
|
5913
|
+
if (repair_session_id === void 0) throw new schema_validation_error("schema validation failed and no session_id available for repair", attempt.issues, parsed.final_text);
|
|
5921
5914
|
const repair_call_opts = {
|
|
5922
5915
|
...call_opts,
|
|
5923
5916
|
session_id: repair_session_id
|
|
5924
5917
|
};
|
|
5925
5918
|
parsed = (await run_cli(spawn_runtime, {
|
|
5926
5919
|
...base_args,
|
|
5927
|
-
stdin_text: build_repair_prompt_text(attempt.
|
|
5920
|
+
stdin_text: build_repair_prompt_text(attempt.issues),
|
|
5928
5921
|
call_opts: repair_call_opts
|
|
5929
5922
|
})).parsed;
|
|
5930
5923
|
repairs_done += 1;
|
|
@@ -6140,6 +6133,36 @@ function sum_usage(steps) {
|
|
|
6140
6133
|
}
|
|
6141
6134
|
//#endregion
|
|
6142
6135
|
//#region src/engine/tool_call_salvage.ts
|
|
6136
|
+
/**
|
|
6137
|
+
* Salvage tool calls that a model emitted as assistant TEXT instead of a
|
|
6138
|
+
* structured tool_calls array. Local runtimes (Ollama native API, LM Studio
|
|
6139
|
+
* /v1) frequently mis-serialize tool definitions in the chat template, so the
|
|
6140
|
+
* model writes its call into the content in one of three well-known formats:
|
|
6141
|
+
*
|
|
6142
|
+
* hermes <tool_call>{"name":"x","arguments":{...}}</tool_call>
|
|
6143
|
+
* json {"name":"x","arguments":{...}} bare or ```json-fenced
|
|
6144
|
+
* qwen_xml <tool_call><function=x><parameter=k>v</parameter></function></tool_call>
|
|
6145
|
+
*
|
|
6146
|
+
* A candidate is accepted ONLY when its name resolves in the call's tool
|
|
6147
|
+
* registry AND its arguments validate against that tool's input_schema.
|
|
6148
|
+
* That double gate is the guard against a false positive on an ordinary
|
|
6149
|
+
* answer that merely contains JSON.
|
|
6150
|
+
*
|
|
6151
|
+
* Parsing rules that matter for correctness:
|
|
6152
|
+
* - Every <tool_call> block extent, accepted or rejected, is masked so the
|
|
6153
|
+
* bare-JSON pass never re-matches a payload inside it (a hermes block
|
|
6154
|
+
* contains a shape-valid bare object; double-matching would double-strip).
|
|
6155
|
+
* - All code fences are masked from the bare pass for the same reason;
|
|
6156
|
+
* only fences with no info string or `json` are parsed as candidates.
|
|
6157
|
+
* - A complete bare object that is not a call is skipped whole: descending
|
|
6158
|
+
* into it could false-positive on substructures of ordinary JSON output.
|
|
6159
|
+
* - An unterminated <tool_call> is ignored (finish_reason 'length'
|
|
6160
|
+
* truncation); its text is left intact.
|
|
6161
|
+
* - Qwen XML parameter values that contain a literal </parameter> mis-parse
|
|
6162
|
+
* under the non-greedy matcher; accepted limitation.
|
|
6163
|
+
*
|
|
6164
|
+
* Never throws; returns undefined when zero candidates survive validation.
|
|
6165
|
+
*/
|
|
6143
6166
|
const TOOL_CALL_OPEN = "<tool_call>";
|
|
6144
6167
|
const TOOL_CALL_CLOSE = "</tool_call>";
|
|
6145
6168
|
const FUNCTION_RE = /<function=([^>\s]+)>([\s\S]*?)<\/function>/g;
|
|
@@ -6394,14 +6417,14 @@ function scan_bare_json(text, masks) {
|
|
|
6394
6417
|
* qwen_xml candidates get one retry with per-parameter JSON coercion, since
|
|
6395
6418
|
* XML parameter values always arrive as strings.
|
|
6396
6419
|
*/
|
|
6397
|
-
function validate_candidate(candidate, tool_map) {
|
|
6420
|
+
async function validate_candidate(candidate, tool_map) {
|
|
6398
6421
|
const tool = tool_map.get(candidate.name);
|
|
6399
6422
|
if (tool === void 0) return void 0;
|
|
6400
6423
|
const schema = tool.input_schema;
|
|
6401
|
-
const raw = schema
|
|
6402
|
-
if (raw.
|
|
6424
|
+
const raw = await validate_schema(schema, candidate.args);
|
|
6425
|
+
if (raw.ok) return {
|
|
6403
6426
|
name: candidate.name,
|
|
6404
|
-
input: raw.
|
|
6427
|
+
input: raw.value,
|
|
6405
6428
|
format: candidate.format,
|
|
6406
6429
|
span: candidate.span
|
|
6407
6430
|
};
|
|
@@ -6418,11 +6441,11 @@ function validate_candidate(candidate, tool_map) {
|
|
|
6418
6441
|
coerced[k] = v;
|
|
6419
6442
|
}
|
|
6420
6443
|
}
|
|
6421
|
-
const retried = schema
|
|
6422
|
-
if (!retried.
|
|
6444
|
+
const retried = await validate_schema(schema, coerced);
|
|
6445
|
+
if (!retried.ok) return void 0;
|
|
6423
6446
|
return {
|
|
6424
6447
|
name: candidate.name,
|
|
6425
|
-
input: retried.
|
|
6448
|
+
input: retried.value,
|
|
6426
6449
|
format: candidate.format,
|
|
6427
6450
|
span: candidate.span
|
|
6428
6451
|
};
|
|
@@ -6445,7 +6468,7 @@ function strip_spans(text, spans) {
|
|
|
6445
6468
|
* surviving calls plus the text with their spans stripped. Returns undefined
|
|
6446
6469
|
* when nothing survives.
|
|
6447
6470
|
*/
|
|
6448
|
-
function salvage_tool_calls(text, tool_map) {
|
|
6471
|
+
async function salvage_tool_calls(text, tool_map) {
|
|
6449
6472
|
if (text.length === 0) return void 0;
|
|
6450
6473
|
const blocks = scan_tool_call_blocks(text);
|
|
6451
6474
|
const fences = scan_fences(text, blocks.masks);
|
|
@@ -6455,11 +6478,7 @@ function salvage_tool_calls(text, tool_map) {
|
|
|
6455
6478
|
...fences.candidates,
|
|
6456
6479
|
...scan_bare_json(text, all_masks)
|
|
6457
6480
|
].toSorted((a, b) => a.span.start - b.span.start);
|
|
6458
|
-
const calls =
|
|
6459
|
-
for (const c of candidates) {
|
|
6460
|
-
const validated = validate_candidate(c, tool_map);
|
|
6461
|
-
if (validated !== void 0) calls.push(validated);
|
|
6462
|
-
}
|
|
6481
|
+
const calls = (await Promise.all(candidates.map((c) => validate_candidate(c, tool_map)))).filter((c) => c !== void 0);
|
|
6463
6482
|
if (calls.length === 0) return void 0;
|
|
6464
6483
|
return {
|
|
6465
6484
|
calls,
|
|
@@ -6469,6 +6488,53 @@ function salvage_tool_calls(text, tool_map) {
|
|
|
6469
6488
|
//#endregion
|
|
6470
6489
|
//#region src/engine/tool_loop.ts
|
|
6471
6490
|
/**
|
|
6491
|
+
* Tool-call loop orchestration.
|
|
6492
|
+
*
|
|
6493
|
+
* Invariants:
|
|
6494
|
+
* - Tools execute SEQUENTIALLY within a turn (no parallel dispatch).
|
|
6495
|
+
* - Abort is checked at the top of each loop iteration AND before each tool
|
|
6496
|
+
* call within a turn.
|
|
6497
|
+
* - Tool input is validated against tool.input_schema BEFORE execute is
|
|
6498
|
+
* invoked. Invalid input is fed back as a tool result with error: true and
|
|
6499
|
+
* consumes a step; execute is not called.
|
|
6500
|
+
* - tool_error_policy:
|
|
6501
|
+
* 'feed_back' (default) serializes the thrown error into a tool result
|
|
6502
|
+
* with { error: <message> }. Loop continues.
|
|
6503
|
+
* 'throw' wraps the error in tool_error and ends the call.
|
|
6504
|
+
* - needs_approval (boolean or predicate) gates execute. Abort fired during
|
|
6505
|
+
* the await rejects with aborted_error.
|
|
6506
|
+
* - Absent on_tool_approval with needs_approval truthy FAILS CLOSED
|
|
6507
|
+
* (tool_approval_denied_error thrown before execute).
|
|
6508
|
+
* - max_steps cap RESOLVES with finish_reason: 'max_steps' (does not throw).
|
|
6509
|
+
* Attempted-but-unexecuted tool calls from the final turn land in
|
|
6510
|
+
* tool_calls with error: { message: 'max_steps_exceeded_before_execution' }.
|
|
6511
|
+
* - salvage_budget (from tool_call_repair_attempts): a turn with NO
|
|
6512
|
+
* structured calls, finish_reason 'stop'|'length', and tools present is
|
|
6513
|
+
* scanned for calls the model emitted as text (a local-runtime failure).
|
|
6514
|
+
* Validated matches run the normal execute path and produce the same
|
|
6515
|
+
* records/events/chunks as native calls, marked salvaged. The budget is
|
|
6516
|
+
* a shared mutable holder so it spans schema-repair re-invocations.
|
|
6517
|
+
* - max_tool_calls_per_step: calls beyond the cap are dropped for the step
|
|
6518
|
+
* (the model can re-issue next turn), recorded with
|
|
6519
|
+
* error: { message: 'dropped_max_tool_calls_per_step' }, and excluded
|
|
6520
|
+
* from the assistant history message so providers that require a result
|
|
6521
|
+
* per emitted call do not reject the next request.
|
|
6522
|
+
* - ends_turn (Tool.ends_turn): a tool flagged terminal ends the loop when a
|
|
6523
|
+
* call to it executes SUCCESSFULLY. The call runs the normal execute path
|
|
6524
|
+
* (record, fed tool_result message, trajectory events + chunk), then the
|
|
6525
|
+
* loop breaks with finish_reason 'stop' instead of running another model
|
|
6526
|
+
* turn. Salvaged terminal calls behave identically. A denied, invalid,
|
|
6527
|
+
* dropped, or throwing terminal call does NOT end the loop. A successful
|
|
6528
|
+
* terminal call is exempt from the max_steps would_exceed_after skip (it
|
|
6529
|
+
* needs no follow-up turn), so a terminal finish wins over a coincident
|
|
6530
|
+
* max_steps cap (finish_reason 'stop', max_steps_reached false).
|
|
6531
|
+
*
|
|
6532
|
+
* The loop does not itself call the AI SDK. It invokes a supplied `invoke_once`
|
|
6533
|
+
* seam that returns a neutral TurnResult. generate.ts builds the real seam
|
|
6534
|
+
* (the ai_sdk transport in providers/ai_sdk/, or a native adapter's
|
|
6535
|
+
* invoke_turn); tests inject a mock seam directly.
|
|
6536
|
+
*/
|
|
6537
|
+
/**
|
|
6472
6538
|
* Throw `aborted_error` (tagged with the step index) if the signal has fired.
|
|
6473
6539
|
*/
|
|
6474
6540
|
function throw_if_aborted(abort, step_index) {
|
|
@@ -6648,18 +6714,18 @@ function compute_and_record_cost(config, step_index, usage) {
|
|
|
6648
6714
|
return breakdown;
|
|
6649
6715
|
}
|
|
6650
6716
|
/**
|
|
6651
|
-
* Validate a raw tool-call input against the tool's
|
|
6652
|
-
*
|
|
6717
|
+
* Validate a raw tool-call input against the tool's schema before execution,
|
|
6718
|
+
* returning the parsed value or a feedback-ready error message.
|
|
6653
6719
|
*/
|
|
6654
|
-
function validate_tool_input(tool, input) {
|
|
6655
|
-
const parsed = tool.input_schema
|
|
6656
|
-
if (parsed.
|
|
6720
|
+
async function validate_tool_input(tool, input) {
|
|
6721
|
+
const parsed = await validate_schema(tool.input_schema, input);
|
|
6722
|
+
if (parsed.ok) return {
|
|
6657
6723
|
ok: true,
|
|
6658
|
-
value: parsed.
|
|
6724
|
+
value: parsed.value
|
|
6659
6725
|
};
|
|
6660
6726
|
return {
|
|
6661
6727
|
ok: false,
|
|
6662
|
-
message: `invalid tool input: ${
|
|
6728
|
+
message: `invalid tool input: ${format_schema_issues(parsed.issues)}`
|
|
6663
6729
|
};
|
|
6664
6730
|
}
|
|
6665
6731
|
/**
|
|
@@ -6741,7 +6807,7 @@ async function run_tool_loop(config) {
|
|
|
6741
6807
|
let salvage;
|
|
6742
6808
|
const salvaged_formats = /* @__PURE__ */ new Map();
|
|
6743
6809
|
if (turn.tool_calls.length === 0 && (turn.finish_reason === "stop" || turn.finish_reason === "length") && config.tools.length > 0 && config.salvage_budget !== void 0 && config.salvage_budget.remaining > 0) {
|
|
6744
|
-
salvage = salvage_tool_calls(turn.text, tool_map);
|
|
6810
|
+
salvage = await salvage_tool_calls(turn.text, tool_map);
|
|
6745
6811
|
if (salvage !== void 0) {
|
|
6746
6812
|
config.salvage_budget.remaining -= 1;
|
|
6747
6813
|
history_text = salvage.stripped_text;
|
|
@@ -6874,7 +6940,7 @@ async function run_tool_loop(config) {
|
|
|
6874
6940
|
await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, void 0, { message: err_message });
|
|
6875
6941
|
continue;
|
|
6876
6942
|
}
|
|
6877
|
-
const validation = validate_tool_input(tool, raw_call.input);
|
|
6943
|
+
const validation = await validate_tool_input(tool, raw_call.input);
|
|
6878
6944
|
if (!validation.ok) {
|
|
6879
6945
|
const record = {
|
|
6880
6946
|
id: raw_call.id,
|
|
@@ -7094,451 +7160,6 @@ async function run_tool_loop(config) {
|
|
|
7094
7160
|
};
|
|
7095
7161
|
}
|
|
7096
7162
|
//#endregion
|
|
7097
|
-
//#region src/engine/providers/ai_sdk/telemetry.ts
|
|
7098
|
-
/**
|
|
7099
|
-
* Build the AI SDK's `experimental_telemetry` option from engine settings,
|
|
7100
|
-
* loading the `@ai-sdk/otel` peer only when telemetry is enabled.
|
|
7101
|
-
*/
|
|
7102
|
-
async function build_ai_sdk_telemetry(settings) {
|
|
7103
|
-
if (!settings?.enabled) return void 0;
|
|
7104
|
-
const out = {
|
|
7105
|
-
isEnabled: true,
|
|
7106
|
-
integrations: [new (await (load_optional_peer("@ai-sdk/otel"))).OpenTelemetry(settings.tracer !== void 0 ? { tracer: settings.tracer } : {})]
|
|
7107
|
-
};
|
|
7108
|
-
if (settings.function_id !== void 0) out.functionId = settings.function_id;
|
|
7109
|
-
if (settings.record_inputs !== void 0) out.recordInputs = settings.record_inputs;
|
|
7110
|
-
if (settings.record_outputs !== void 0) out.recordOutputs = settings.record_outputs;
|
|
7111
|
-
if (settings.metadata !== void 0) out.metadata = settings.metadata;
|
|
7112
|
-
return out;
|
|
7113
|
-
}
|
|
7114
|
-
//#endregion
|
|
7115
|
-
//#region src/engine/providers/ai_sdk/invoke.ts
|
|
7116
|
-
/**
|
|
7117
|
-
* The ai_sdk transport: the engine's only Vercel AI SDK call site.
|
|
7118
|
-
*
|
|
7119
|
-
* Only this module imports from `ai` or invokes generateText / streamText.
|
|
7120
|
-
* generate.ts drives this transport through create_ai_sdk_turn and stays
|
|
7121
|
-
* SDK-agnostic; run_tool_loop sees the same neutral TurnResult a native
|
|
7122
|
-
* adapter produces. Enforced by rules/no-ai-import-outside-ai-sdk-provider.yml.
|
|
7123
|
-
*
|
|
7124
|
-
* Retry stays out of this module on purpose: generate.ts wraps the turn
|
|
7125
|
-
* returned here in the engine-owned retry_turn, exactly as it wraps a native
|
|
7126
|
-
* adapter's invoke_turn. This module owns request/response mapping only.
|
|
7127
|
-
*/
|
|
7128
|
-
/**
|
|
7129
|
-
* Map the AI SDK's finish reason string to the engine's FinishReason.
|
|
7130
|
-
*/
|
|
7131
|
-
function map_finish_reason(raw) {
|
|
7132
|
-
switch (raw) {
|
|
7133
|
-
case "stop": return "stop";
|
|
7134
|
-
case "length": return "length";
|
|
7135
|
-
case "content-filter":
|
|
7136
|
-
case "content_filter": return "content_filter";
|
|
7137
|
-
case "tool-calls":
|
|
7138
|
-
case "tool_calls": return "tool_calls";
|
|
7139
|
-
default: return "stop";
|
|
7140
|
-
}
|
|
7141
|
-
}
|
|
7142
|
-
/**
|
|
7143
|
-
* Extract the usage fields `default_normalize_usage` understands from an AI
|
|
7144
|
-
* SDK usage object, tolerating both the SDK's camelCase shape and the
|
|
7145
|
-
* flattened snake_case fields some provider mocks return directly.
|
|
7146
|
-
*/
|
|
7147
|
-
function to_raw_provider_usage(usage) {
|
|
7148
|
-
if (usage === null || typeof usage !== "object") return {};
|
|
7149
|
-
const raw = {};
|
|
7150
|
-
const input_tokens = Reflect.get(usage, "inputTokens");
|
|
7151
|
-
if (typeof input_tokens === "number") raw.input_tokens = input_tokens;
|
|
7152
|
-
const output_tokens = Reflect.get(usage, "outputTokens");
|
|
7153
|
-
if (typeof output_tokens === "number") raw.output_tokens = output_tokens;
|
|
7154
|
-
const input_details = Reflect.get(usage, "inputTokenDetails");
|
|
7155
|
-
if (input_details !== null && typeof input_details === "object") {
|
|
7156
|
-
const cache_read = Reflect.get(input_details, "cacheReadTokens");
|
|
7157
|
-
const cache_write = Reflect.get(input_details, "cacheWriteTokens");
|
|
7158
|
-
raw.input_token_details = {};
|
|
7159
|
-
if (typeof cache_read === "number") raw.input_token_details.cached_tokens = cache_read;
|
|
7160
|
-
if (typeof cache_write === "number") raw.input_token_details.cache_creation_input_tokens = cache_write;
|
|
7161
|
-
}
|
|
7162
|
-
const output_details = Reflect.get(usage, "outputTokenDetails");
|
|
7163
|
-
if (output_details !== null && typeof output_details === "object") {
|
|
7164
|
-
const reasoning = Reflect.get(output_details, "reasoningTokens");
|
|
7165
|
-
raw.output_token_details = {};
|
|
7166
|
-
if (typeof reasoning === "number") raw.output_token_details.reasoning_tokens = reasoning;
|
|
7167
|
-
}
|
|
7168
|
-
const cached_flat = Reflect.get(usage, "cached_input_tokens");
|
|
7169
|
-
if (typeof cached_flat === "number") raw.cached_input_tokens = cached_flat;
|
|
7170
|
-
const cache_write_flat = Reflect.get(usage, "cache_write_tokens");
|
|
7171
|
-
if (typeof cache_write_flat === "number") raw.cache_write_tokens = cache_write_flat;
|
|
7172
|
-
const reasoning_flat = Reflect.get(usage, "reasoning_tokens");
|
|
7173
|
-
if (typeof reasoning_flat === "number") raw.reasoning_tokens = reasoning_flat;
|
|
7174
|
-
const input_flat = Reflect.get(usage, "input_tokens");
|
|
7175
|
-
if (raw.input_tokens === void 0 && typeof input_flat === "number") raw.input_tokens = input_flat;
|
|
7176
|
-
const output_flat = Reflect.get(usage, "output_tokens");
|
|
7177
|
-
if (raw.output_tokens === void 0 && typeof output_flat === "number") raw.output_tokens = output_flat;
|
|
7178
|
-
return raw;
|
|
7179
|
-
}
|
|
7180
|
-
/**
|
|
7181
|
-
* Normalize an AI SDK `providerMetadata` value into the neutral
|
|
7182
|
-
* TurnResult.provider_reported shape.
|
|
7183
|
-
*
|
|
7184
|
-
* The SDK's metadata is already keyed by provider name, which is the shape
|
|
7185
|
-
* provider_reported documents, so the keys ride through untranslated and every
|
|
7186
|
-
* ai_sdk provider is served by this one seam: bedrock's guardrail `trace`,
|
|
7187
|
-
* anthropic's cache detail, openai's own fields. The payloads stay opaque
|
|
7188
|
-
* (callers narrow them at the use site), so this copies the top level and
|
|
7189
|
-
* inspects nothing below it. An absent or empty map returns undefined, keeping
|
|
7190
|
-
* "the provider reported nothing" spelled one way.
|
|
7191
|
-
*/
|
|
7192
|
-
function to_provider_reported(metadata) {
|
|
7193
|
-
if (metadata === null || typeof metadata !== "object") return void 0;
|
|
7194
|
-
const keys = Object.keys(metadata);
|
|
7195
|
-
if (keys.length === 0) return void 0;
|
|
7196
|
-
const reported = {};
|
|
7197
|
-
for (const key of keys) reported[key] = Reflect.get(metadata, key);
|
|
7198
|
-
return reported;
|
|
7199
|
-
}
|
|
7200
|
-
/**
|
|
7201
|
-
* Map fascicle's Message[] to the AI SDK's ModelMessage[] shape.
|
|
7202
|
-
*/
|
|
7203
|
-
function to_sdk_messages(messages) {
|
|
7204
|
-
const out = [];
|
|
7205
|
-
for (const m of messages) {
|
|
7206
|
-
if (m.role === "system") {
|
|
7207
|
-
out.push({
|
|
7208
|
-
role: "system",
|
|
7209
|
-
content: m.content
|
|
7210
|
-
});
|
|
7211
|
-
continue;
|
|
7212
|
-
}
|
|
7213
|
-
if (m.role === "user") {
|
|
7214
|
-
if (typeof m.content === "string") out.push({
|
|
7215
|
-
role: "user",
|
|
7216
|
-
content: m.content
|
|
7217
|
-
});
|
|
7218
|
-
else {
|
|
7219
|
-
const parts = m.content.map((p) => {
|
|
7220
|
-
if (p.type === "text") return {
|
|
7221
|
-
type: "text",
|
|
7222
|
-
text: p.text
|
|
7223
|
-
};
|
|
7224
|
-
const image_part = {
|
|
7225
|
-
type: "image",
|
|
7226
|
-
image: p.image
|
|
7227
|
-
};
|
|
7228
|
-
if (p.media_type !== void 0) image_part.mediaType = p.media_type;
|
|
7229
|
-
return image_part;
|
|
7230
|
-
});
|
|
7231
|
-
out.push({
|
|
7232
|
-
role: "user",
|
|
7233
|
-
content: parts
|
|
7234
|
-
});
|
|
7235
|
-
}
|
|
7236
|
-
continue;
|
|
7237
|
-
}
|
|
7238
|
-
if (m.role === "tool") {
|
|
7239
|
-
out.push({
|
|
7240
|
-
role: "tool",
|
|
7241
|
-
content: [{
|
|
7242
|
-
type: "tool-result",
|
|
7243
|
-
toolCallId: m.tool_call_id,
|
|
7244
|
-
toolName: m.name,
|
|
7245
|
-
output: {
|
|
7246
|
-
type: "text",
|
|
7247
|
-
value: m.content
|
|
7248
|
-
}
|
|
7249
|
-
}]
|
|
7250
|
-
});
|
|
7251
|
-
continue;
|
|
7252
|
-
}
|
|
7253
|
-
if (typeof m.content === "string") {
|
|
7254
|
-
out.push({
|
|
7255
|
-
role: "assistant",
|
|
7256
|
-
content: m.content
|
|
7257
|
-
});
|
|
7258
|
-
continue;
|
|
7259
|
-
}
|
|
7260
|
-
const parts = m.content.map((p) => {
|
|
7261
|
-
if (p.type === "text") return {
|
|
7262
|
-
type: "text",
|
|
7263
|
-
text: p.text
|
|
7264
|
-
};
|
|
7265
|
-
return {
|
|
7266
|
-
type: "tool-call",
|
|
7267
|
-
toolCallId: p.id,
|
|
7268
|
-
toolName: p.name,
|
|
7269
|
-
input: p.input
|
|
7270
|
-
};
|
|
7271
|
-
});
|
|
7272
|
-
out.push({
|
|
7273
|
-
role: "assistant",
|
|
7274
|
-
content: parts
|
|
7275
|
-
});
|
|
7276
|
-
}
|
|
7277
|
-
return out;
|
|
7278
|
-
}
|
|
7279
|
-
/**
|
|
7280
|
-
* Hoist a leading run of system messages out of the SDK message list into the
|
|
7281
|
-
* AI SDK's top-level `system` option.
|
|
7282
|
-
*
|
|
7283
|
-
* Passing a `role: 'system'` entry inside `messages` makes the AI SDK treat it
|
|
7284
|
-
* as potentially attacker-controlled: it warns on every call (and newer `ai`
|
|
7285
|
-
* versions throw unless `allowSystemInMessages` is set). fascicle's system
|
|
7286
|
-
* content is the developer's own prompt, and the SDK's own recommendation is to
|
|
7287
|
-
* deliver it through the top-level `instructions` option (v7's rename of the
|
|
7288
|
-
* former `system` option), which removes the warning at its source rather than
|
|
7289
|
-
* suppressing it.
|
|
7290
|
-
*
|
|
7291
|
-
* Only the *leading* run is hoisted (fascicle's only shape: one or more system
|
|
7292
|
-
* messages, then the conversation). A system message that appears after a
|
|
7293
|
-
* non-system message keeps its position rather than being silently reordered to
|
|
7294
|
-
* the top, and the original list is returned untouched when hoisting would leave
|
|
7295
|
-
* `messages` empty, since the SDK rejects an empty messages array.
|
|
7296
|
-
*/
|
|
7297
|
-
function split_leading_system(messages) {
|
|
7298
|
-
let run_end = 0;
|
|
7299
|
-
const system_parts = [];
|
|
7300
|
-
while (run_end < messages.length) {
|
|
7301
|
-
const m = messages[run_end];
|
|
7302
|
-
if (m?.role !== "system") break;
|
|
7303
|
-
system_parts.push(m.content);
|
|
7304
|
-
run_end += 1;
|
|
7305
|
-
}
|
|
7306
|
-
const rest = messages.slice(run_end);
|
|
7307
|
-
if (system_parts.length === 0 || rest.length === 0) return { messages: [...messages] };
|
|
7308
|
-
return {
|
|
7309
|
-
system: system_parts.join("\n\n"),
|
|
7310
|
-
messages: rest
|
|
7311
|
-
};
|
|
7312
|
-
}
|
|
7313
|
-
/**
|
|
7314
|
-
* Map fascicle's Tool[] to the AI SDK's ToolSet.
|
|
7315
|
-
*
|
|
7316
|
-
* Returns undefined for an empty tool list so the caller can omit `tools`
|
|
7317
|
-
* from the call params entirely instead of passing an empty object.
|
|
7318
|
-
*/
|
|
7319
|
-
function to_sdk_tools(tools) {
|
|
7320
|
-
if (tools.length === 0) return void 0;
|
|
7321
|
-
const entries = {};
|
|
7322
|
-
for (const t of tools) entries[t.name] = tool({
|
|
7323
|
-
description: t.description,
|
|
7324
|
-
inputSchema: t.input_schema
|
|
7325
|
-
});
|
|
7326
|
-
return entries;
|
|
7327
|
-
}
|
|
7328
|
-
/**
|
|
7329
|
-
* Map one AI SDK stream part to the engine's StreamChunk shape, or undefined
|
|
7330
|
-
* for a part kind the engine does not model.
|
|
7331
|
-
*/
|
|
7332
|
-
function map_stream_part_to_chunk(part, step_index) {
|
|
7333
|
-
switch (part.type) {
|
|
7334
|
-
case "text-delta": return {
|
|
7335
|
-
kind: "text",
|
|
7336
|
-
text: part.text,
|
|
7337
|
-
step_index
|
|
7338
|
-
};
|
|
7339
|
-
case "reasoning-delta": return {
|
|
7340
|
-
kind: "reasoning",
|
|
7341
|
-
text: part.text,
|
|
7342
|
-
step_index
|
|
7343
|
-
};
|
|
7344
|
-
case "tool-input-start": return {
|
|
7345
|
-
kind: "tool_call_start",
|
|
7346
|
-
id: part.id,
|
|
7347
|
-
name: part.toolName,
|
|
7348
|
-
step_index
|
|
7349
|
-
};
|
|
7350
|
-
case "tool-input-delta": return {
|
|
7351
|
-
kind: "tool_call_input_delta",
|
|
7352
|
-
id: part.id,
|
|
7353
|
-
delta: part.delta,
|
|
7354
|
-
step_index
|
|
7355
|
-
};
|
|
7356
|
-
case "tool-call": return {
|
|
7357
|
-
kind: "tool_call_end",
|
|
7358
|
-
id: part.toolCallId,
|
|
7359
|
-
input: part.input,
|
|
7360
|
-
step_index
|
|
7361
|
-
};
|
|
7362
|
-
case "finish-step": return {
|
|
7363
|
-
kind: "step_finish",
|
|
7364
|
-
step_index,
|
|
7365
|
-
finish_reason: map_finish_reason(part.finishReason),
|
|
7366
|
-
usage: default_usage_from_sdk(part.usage)
|
|
7367
|
-
};
|
|
7368
|
-
default: return;
|
|
7369
|
-
}
|
|
7370
|
-
}
|
|
7371
|
-
/**
|
|
7372
|
-
* Normalize a raw AI SDK usage object into UsageTotals for a streamed
|
|
7373
|
-
* step_finish chunk.
|
|
7374
|
-
*/
|
|
7375
|
-
function default_usage_from_sdk(usage) {
|
|
7376
|
-
return default_normalize_usage(to_raw_provider_usage(usage));
|
|
7377
|
-
}
|
|
7378
|
-
/**
|
|
7379
|
-
* Run one streamText call, dispatching StreamChunks as parts arrive and
|
|
7380
|
-
* accumulating them into a TurnResult once the stream completes.
|
|
7381
|
-
*/
|
|
7382
|
-
async function collect_stream(params, step_index, dispatcher, on_first_chunk, internal_controller, adapter) {
|
|
7383
|
-
const stream_result = streamText(params);
|
|
7384
|
-
let text = "";
|
|
7385
|
-
const tool_calls = [];
|
|
7386
|
-
let finish_reason = "stop";
|
|
7387
|
-
let raw_usage = {};
|
|
7388
|
-
let provider_reported;
|
|
7389
|
-
let first = true;
|
|
7390
|
-
for await (const part of stream_result.stream) {
|
|
7391
|
-
if (first) {
|
|
7392
|
-
first = false;
|
|
7393
|
-
on_first_chunk();
|
|
7394
|
-
}
|
|
7395
|
-
if (part.type === "text-delta") text += part.text;
|
|
7396
|
-
if (part.type === "tool-call") tool_calls.push({
|
|
7397
|
-
id: part.toolCallId,
|
|
7398
|
-
name: part.toolName,
|
|
7399
|
-
input: part.input
|
|
7400
|
-
});
|
|
7401
|
-
if (part.type === "finish-step") {
|
|
7402
|
-
finish_reason = map_finish_reason(part.finishReason);
|
|
7403
|
-
raw_usage = to_raw_provider_usage(part.usage);
|
|
7404
|
-
provider_reported = to_provider_reported(part.providerMetadata);
|
|
7405
|
-
}
|
|
7406
|
-
if (part.type === "error") throw part.error;
|
|
7407
|
-
if (part.type === "abort") throw new aborted_error("aborted", { step_index });
|
|
7408
|
-
const chunk = map_stream_part_to_chunk(part, step_index);
|
|
7409
|
-
if (chunk === void 0) continue;
|
|
7410
|
-
try {
|
|
7411
|
-
await dispatcher.dispatch(chunk);
|
|
7412
|
-
} catch (err) {
|
|
7413
|
-
internal_controller.abort();
|
|
7414
|
-
throw err;
|
|
7415
|
-
}
|
|
7416
|
-
}
|
|
7417
|
-
return {
|
|
7418
|
-
text,
|
|
7419
|
-
tool_calls,
|
|
7420
|
-
finish_reason,
|
|
7421
|
-
usage: adapter.normalize_usage(raw_usage),
|
|
7422
|
-
...provider_reported !== void 0 ? { provider_reported } : {}
|
|
7423
|
-
};
|
|
7424
|
-
}
|
|
7425
|
-
/**
|
|
7426
|
-
* Run one generateText call and map its result into a TurnResult, recovering
|
|
7427
|
-
* a schema-validation failure from the SDK via recover_no_object_generated
|
|
7428
|
-
* instead of letting it throw.
|
|
7429
|
-
*/
|
|
7430
|
-
async function collect_non_stream(params, adapter) {
|
|
7431
|
-
let result;
|
|
7432
|
-
try {
|
|
7433
|
-
result = await generateText(params);
|
|
7434
|
-
} catch (err) {
|
|
7435
|
-
const recovered = recover_no_object_generated(err, adapter);
|
|
7436
|
-
if (recovered !== void 0) return recovered;
|
|
7437
|
-
throw err;
|
|
7438
|
-
}
|
|
7439
|
-
const tool_calls = [];
|
|
7440
|
-
for (const tc of result.toolCalls) tool_calls.push({
|
|
7441
|
-
id: tc.toolCallId,
|
|
7442
|
-
name: tc.toolName,
|
|
7443
|
-
input: tc.input
|
|
7444
|
-
});
|
|
7445
|
-
const raw_usage = to_raw_provider_usage(result.usage);
|
|
7446
|
-
const provider_reported = to_provider_reported(result.providerMetadata);
|
|
7447
|
-
return {
|
|
7448
|
-
text: result.text,
|
|
7449
|
-
tool_calls,
|
|
7450
|
-
finish_reason: map_finish_reason(result.finishReason),
|
|
7451
|
-
usage: adapter.normalize_usage(raw_usage),
|
|
7452
|
-
...provider_reported !== void 0 ? { provider_reported } : {}
|
|
7453
|
-
};
|
|
7454
|
-
}
|
|
7455
|
-
/**
|
|
7456
|
-
* When `output` (structured output) is in play, `generateText` eagerly parses the
|
|
7457
|
-
* model's text against the schema and throws `NoObjectGeneratedError` if it
|
|
7458
|
-
* does not conform. That parse is the SDK's, not fascicle's: turning it into a
|
|
7459
|
-
* thrown error would skip the engine's own schema-parse + repair loop (and the
|
|
7460
|
-
* error is not a retryable transient, so it would surface raw). Instead we
|
|
7461
|
-
* recover the model's raw text from the error and hand it back as a normal
|
|
7462
|
-
* turn result, so `parse_with_schema` re-validates it and the repair loop
|
|
7463
|
-
* engages exactly as it does for the prompt-based path. Returns undefined for
|
|
7464
|
-
* any other error so the caller rethrows.
|
|
7465
|
-
*
|
|
7466
|
-
* The recovered turn carries no provider_reported: the SDK's error exposes
|
|
7467
|
-
* text, usage, finishReason and response metadata, but not providerMetadata,
|
|
7468
|
-
* so there is nothing to read. The repair turn that follows reports normally.
|
|
7469
|
-
*/
|
|
7470
|
-
function recover_no_object_generated(err, adapter) {
|
|
7471
|
-
if (!NoObjectGeneratedError.isInstance(err)) return void 0;
|
|
7472
|
-
const text_field = Reflect.get(err, "text");
|
|
7473
|
-
const finish_field = Reflect.get(err, "finishReason");
|
|
7474
|
-
const raw_usage = to_raw_provider_usage(Reflect.get(err, "usage"));
|
|
7475
|
-
return {
|
|
7476
|
-
text: typeof text_field === "string" ? text_field : "",
|
|
7477
|
-
tool_calls: [],
|
|
7478
|
-
finish_reason: map_finish_reason(typeof finish_field === "string" ? finish_field : void 0),
|
|
7479
|
-
usage: adapter.normalize_usage(raw_usage)
|
|
7480
|
-
};
|
|
7481
|
-
}
|
|
7482
|
-
/**
|
|
7483
|
-
* Build the single-attempt SDK turn for one generate call: the
|
|
7484
|
-
* generateText/streamText call body behind the same neutral seam a native
|
|
7485
|
-
* adapter's invoke_turn implements. The built model is memoized across steps
|
|
7486
|
-
* of one generate call. No retry here: generate.ts wraps the returned turn in
|
|
7487
|
-
* the engine-owned retry_turn.
|
|
7488
|
-
*/
|
|
7489
|
-
function create_ai_sdk_turn(cfg) {
|
|
7490
|
-
let model_instance;
|
|
7491
|
-
const get_model = async () => {
|
|
7492
|
-
if (model_instance === void 0) model_instance = await cfg.adapter.build_model(cfg.model_id);
|
|
7493
|
-
return model_instance;
|
|
7494
|
-
};
|
|
7495
|
-
let telemetry_options;
|
|
7496
|
-
let telemetry_resolved = false;
|
|
7497
|
-
const get_telemetry = async () => {
|
|
7498
|
-
if (!telemetry_resolved) {
|
|
7499
|
-
telemetry_options = await build_ai_sdk_telemetry(cfg.telemetry);
|
|
7500
|
-
telemetry_resolved = true;
|
|
7501
|
-
}
|
|
7502
|
-
return telemetry_options;
|
|
7503
|
-
};
|
|
7504
|
-
const sdk_tools = to_sdk_tools(cfg.tools);
|
|
7505
|
-
let output_spec;
|
|
7506
|
-
if (cfg.schema !== void 0 && cfg.tools.length === 0 && cfg.adapter.supports("structured_output")) output_spec = Output.object({ schema: cfg.schema });
|
|
7507
|
-
const provider_options = cfg.provider_options;
|
|
7508
|
-
return async (args) => {
|
|
7509
|
-
const model = await get_model();
|
|
7510
|
-
const telemetry = await get_telemetry();
|
|
7511
|
-
const internal_controller = new AbortController();
|
|
7512
|
-
const cancel_on_user_abort = () => {
|
|
7513
|
-
internal_controller.abort(args.abort.reason);
|
|
7514
|
-
};
|
|
7515
|
-
if (args.abort.aborted) internal_controller.abort(args.abort.reason);
|
|
7516
|
-
else args.abort.addEventListener("abort", cancel_on_user_abort, { once: true });
|
|
7517
|
-
const { system: hoisted_system, messages: sdk_messages } = split_leading_system(to_sdk_messages(args.messages));
|
|
7518
|
-
const base_params = {
|
|
7519
|
-
model,
|
|
7520
|
-
messages: sdk_messages,
|
|
7521
|
-
abortSignal: internal_controller.signal,
|
|
7522
|
-
stopWhen: isStepCount(1),
|
|
7523
|
-
maxRetries: 0
|
|
7524
|
-
};
|
|
7525
|
-
if (hoisted_system !== void 0) base_params.instructions = hoisted_system;
|
|
7526
|
-
if (sdk_tools !== void 0) base_params.tools = sdk_tools;
|
|
7527
|
-
if (output_spec !== void 0) base_params.output = output_spec;
|
|
7528
|
-
if (provider_options !== void 0) base_params.providerOptions = provider_options;
|
|
7529
|
-
if (cfg.temperature !== void 0) base_params.temperature = cfg.temperature;
|
|
7530
|
-
if (cfg.max_tokens !== void 0) base_params.maxOutputTokens = cfg.max_tokens;
|
|
7531
|
-
if (cfg.top_p !== void 0) base_params.topP = cfg.top_p;
|
|
7532
|
-
if (telemetry !== void 0) base_params.experimental_telemetry = telemetry;
|
|
7533
|
-
try {
|
|
7534
|
-
if (args.stream) return await collect_stream(base_params, args.step_index, cfg.dispatcher, args.on_first_chunk, internal_controller, cfg.adapter);
|
|
7535
|
-
return await collect_non_stream(base_params, cfg.adapter);
|
|
7536
|
-
} finally {
|
|
7537
|
-
args.abort.removeEventListener("abort", cancel_on_user_abort);
|
|
7538
|
-
}
|
|
7539
|
-
};
|
|
7540
|
-
}
|
|
7541
|
-
//#endregion
|
|
7542
7163
|
//#region src/engine/generate.ts
|
|
7543
7164
|
/**
|
|
7544
7165
|
* Build the initial message list for a generate call from `opts.system` and
|
|
@@ -7842,6 +7463,13 @@ async function generate(opts_in, engine) {
|
|
|
7842
7463
|
if (effort !== "none" && effort_translation.effort_ignored) record_effort_ignored(trajectory, target.model_id);
|
|
7843
7464
|
const combined_provider_options = merge_provider_options(effort_translation.provider_options, opts.provider_options);
|
|
7844
7465
|
const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
|
|
7466
|
+
let ai_sdk_invoke_mod;
|
|
7467
|
+
try {
|
|
7468
|
+
ai_sdk_invoke_mod = await import("./invoke-CWjm1Y_R.js");
|
|
7469
|
+
} catch (err) {
|
|
7470
|
+
throw missing_peer_error("ai", err);
|
|
7471
|
+
}
|
|
7472
|
+
const { create_ai_sdk_turn } = ai_sdk_invoke_mod;
|
|
7845
7473
|
invoke_once = build_ai_sdk_invoke({
|
|
7846
7474
|
invoke_turn: create_ai_sdk_turn({
|
|
7847
7475
|
adapter,
|
|
@@ -7947,19 +7575,19 @@ async function generate(opts_in, engine) {
|
|
|
7947
7575
|
finish_reason = loop_result.finish_reason;
|
|
7948
7576
|
if (opts.schema === void 0) break;
|
|
7949
7577
|
if (finish_reason !== "stop") throw new incomplete_generation_error(finish_reason, text, last_provider_reported(steps_accum));
|
|
7950
|
-
const parse = parse_with_schema(opts.schema, text);
|
|
7578
|
+
const parse = await parse_with_schema(opts.schema, text);
|
|
7951
7579
|
if (parse.ok) {
|
|
7952
7580
|
content_parsed = { value: parse.value };
|
|
7953
7581
|
break;
|
|
7954
7582
|
}
|
|
7955
7583
|
record_schema_validation_failed(trajectory, {
|
|
7956
7584
|
attempt: repair_remaining === schema_repair_attempts ? "initial" : "repair",
|
|
7957
|
-
|
|
7585
|
+
schema_issues: format_schema_issues(parse.issues),
|
|
7958
7586
|
raw_text: text
|
|
7959
7587
|
});
|
|
7960
|
-
if (repair_remaining <= 0 || total_steps >= max_steps) throw_schema_validation(parse.
|
|
7588
|
+
if (repair_remaining <= 0 || total_steps >= max_steps) throw_schema_validation(parse.issues, text);
|
|
7961
7589
|
repair_remaining -= 1;
|
|
7962
|
-
messages_mutable.push(build_repair_message(parse.
|
|
7590
|
+
messages_mutable.push(build_repair_message(parse.issues));
|
|
7963
7591
|
}
|
|
7964
7592
|
const aggregated_usage = sum_usage(steps_accum);
|
|
7965
7593
|
const aggregated_cost = aggregate_cost(steps_accum, target.provider);
|
|
@@ -8591,7 +8219,7 @@ function handle_ingest(req, res, broadcaster, on_parse_error) {
|
|
|
8591
8219
|
if (on_parse_error) on_parse_error(err, line);
|
|
8592
8220
|
return;
|
|
8593
8221
|
}
|
|
8594
|
-
const result =
|
|
8222
|
+
const result = parse_trajectory_event(parsed);
|
|
8595
8223
|
if (!result.success) {
|
|
8596
8224
|
rejected++;
|
|
8597
8225
|
if (on_parse_error) on_parse_error(result.error, line);
|
|
@@ -8634,7 +8262,7 @@ function handle_ingest(req, res, broadcaster, on_parse_error) {
|
|
|
8634
8262
|
* current offset means the file shrunk. Reset offset to 0 and re-stream.
|
|
8635
8263
|
* - Partial last line: if the trailing chunk has no `\n`, buffer it and
|
|
8636
8264
|
* wait. Never parse a half-written line.
|
|
8637
|
-
* - Schema failures: a line that fails `
|
|
8265
|
+
* - Schema failures: a line that fails `parse_trajectory_event` is
|
|
8638
8266
|
* dropped and surfaced to `on_parse_error`. The stream keeps going.
|
|
8639
8267
|
*
|
|
8640
8268
|
* The watcher is fs.watch-based; reads are serialized and change events
|
|
@@ -8671,7 +8299,7 @@ function start_tail(options) {
|
|
|
8671
8299
|
if (on_parse_error) on_parse_error(err, line);
|
|
8672
8300
|
return;
|
|
8673
8301
|
}
|
|
8674
|
-
const result =
|
|
8302
|
+
const result = parse_trajectory_event(parsed);
|
|
8675
8303
|
if (!result.success) {
|
|
8676
8304
|
if (on_parse_error) on_parse_error(result.error, line);
|
|
8677
8305
|
return;
|
|
@@ -8998,6 +8626,6 @@ if (process.argv[1] !== void 0 && process.argv[1] === fileURLToPath(import.meta.
|
|
|
8998
8626
|
process.exit(1);
|
|
8999
8627
|
});
|
|
9000
8628
|
//#endregion
|
|
9001
|
-
export { STEP_KINDS, aborted_error, adversarial, bench, branch, checkpoint, claude_cli_error, compose, consensus, create_engine,
|
|
8629
|
+
export { STEP_KINDS, aborted_error, adversarial, bench, bench_suspend_error, branch, checkpoint, claude_cli_error, compose, consensus, create_engine, default_normalize_usage, describe, describe_cycle_error, engine_config_error, engine_disposed_error, ensemble, ensemble_step, fallback, flow_schema_default as flow_schema, forward_standard_env, improve, incomplete_generation_error, is_custom_trajectory_event, is_emit_event, is_span_end_event, is_span_start_event, is_step_kind, judge_equals, judge_llm, judge_with, learn, loop, map, model_call, model_required_error, normalize_score, on_chunk_error, parallel, parse_trajectory_event, pipe, provider_auth_error, provider_capability_error, provider_error, provider_not_configured_error, rate_limit_error, read_baseline, regression_compare, resume_validation_error, retry, run, run_viewer_cli, schema_validation_error, scope, sequence, start_viewer, stash, step, suspend, suspended_error, timeout, timeout_error, tool_approval_denied_error, tool_error, tournament, use, write_baseline };
|
|
9002
8630
|
|
|
9003
8631
|
//# sourceMappingURL=index.js.map
|