@qilin-agent/llm-replay 0.0.0-stage → 3.1.3
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/LICENSE +21 -0
- package/README.i18n.yaml +61 -0
- package/README.md +155 -2
- package/README.zh.md +156 -0
- package/lib/index.js +800 -0
- package/lib/types/index.d.ts +283 -0
- package/package.json +52 -3
package/lib/index.js
ADDED
|
@@ -0,0 +1,800 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { delimiter } from "node:path";
|
|
3
|
+
import { SESSION_FORMAT_VERSION, SessionLogOffset } from "@qilin-agent/session";
|
|
4
|
+
import { SessionFormatUnsupportedMigrationError, createSessionFormatCatalogWithChildren, sessionFormatCatalog } from "@qilin-agent/session-format-catalog";
|
|
5
|
+
import { LlmAdapter, LlmError, ReasoningEffortId, expandAssistantStream, offloadedImageText, requestImageHandleText, resolveRetryPolicy } from "@qilin-agent/llm";
|
|
6
|
+
import { assertNever } from "@qilin-agent/util-values";
|
|
7
|
+
//#region lib/types/index.js
|
|
8
|
+
/**
|
|
9
|
+
* Keyless snapshot-test LLM replay. It derives one model-call script per
|
|
10
|
+
* recorded session from v3 embedded Assistant streams and explicitly marked local
|
|
11
|
+
* compaction calls, then binds fresh live sessions to parent/child scripts by
|
|
12
|
+
* first-call order. Throw and hang cases require an explicit override because
|
|
13
|
+
* a session log cannot reconstruct them alone.
|
|
14
|
+
* @module @qilin-agent/llm-replay
|
|
15
|
+
*/
|
|
16
|
+
const PACKED_CHUNK_ROW_TYPES = new Set([
|
|
17
|
+
"text-chunks",
|
|
18
|
+
"reasoning-chunks",
|
|
19
|
+
"tool-call-chunks"
|
|
20
|
+
]);
|
|
21
|
+
if (sessionFormatCatalog.currentVersion !== SESSION_FORMAT_VERSION) throw new Error(`llm-replay: format catalog v${sessionFormatCatalog.currentVersion} does not match Session v${SESSION_FORMAT_VERSION}`);
|
|
22
|
+
/**
|
|
23
|
+
* Parse a projected session `.jsonl` buffer into current events. The first
|
|
24
|
+
* non-empty line is the physical header. Body rows either all carry complete
|
|
25
|
+
* persistence envelopes or all omit them; projected rows receive deterministic
|
|
26
|
+
* dense sequences and zero timestamps. The build-static format catalog then
|
|
27
|
+
* decodes and migrates the complete artifact before this function returns.
|
|
28
|
+
* @param text - the raw `.jsonl` file contents.
|
|
29
|
+
* @returns every migrated current event, in log order.
|
|
30
|
+
*/
|
|
31
|
+
function parseSessionLog(text) {
|
|
32
|
+
return parseSessionFixture(text).events;
|
|
33
|
+
}
|
|
34
|
+
/** Parse, complete, decode, and migrate one projected snapshot artifact without writing its source. */
|
|
35
|
+
function parseSessionFixture(text) {
|
|
36
|
+
let headerLineNumber;
|
|
37
|
+
let sourceHeader;
|
|
38
|
+
let restore;
|
|
39
|
+
const rowLines = [];
|
|
40
|
+
const eventLines = [];
|
|
41
|
+
let bodyKind;
|
|
42
|
+
let nextSeq = 0;
|
|
43
|
+
for (const [index, line] of text.split(/\r?\n/).entries()) {
|
|
44
|
+
if (line.trim().length === 0) continue;
|
|
45
|
+
let value;
|
|
46
|
+
try {
|
|
47
|
+
value = JSON.parse(line);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
throw new Error(`session snapshot line ${index + 1} contains invalid JSON`, { cause: error });
|
|
50
|
+
}
|
|
51
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) throw new Error(`session snapshot line ${index + 1} must be a JSON object`);
|
|
52
|
+
const lineNumber = index + 1;
|
|
53
|
+
const recordValue = value;
|
|
54
|
+
if (restore === void 0) {
|
|
55
|
+
headerLineNumber = lineNumber;
|
|
56
|
+
sourceHeader = recordValue;
|
|
57
|
+
try {
|
|
58
|
+
restore = createSessionFormatCatalogWithChildren([]).createRestore(normalizeProjectedHeader(recordValue), {
|
|
59
|
+
recovery: "strict",
|
|
60
|
+
validation: "current"
|
|
61
|
+
});
|
|
62
|
+
} catch (error) {
|
|
63
|
+
throw fixtureFormatError(error, lineNumber, [], []);
|
|
64
|
+
}
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const record = normalizeProjectedRow(recordValue);
|
|
68
|
+
const packed = PACKED_CHUNK_ROW_TYPES.has(record.type);
|
|
69
|
+
const seqKey = packed ? "seq0" : "seq";
|
|
70
|
+
const timeKey = packed ? "time0" : "time";
|
|
71
|
+
const hasSeq = Object.hasOwn(record, seqKey);
|
|
72
|
+
if (hasSeq !== Object.hasOwn(record, timeKey)) throw new Error(`session snapshot line ${lineNumber} must contain both ${seqKey} and ${timeKey}, or neither`);
|
|
73
|
+
const currentKind = hasSeq ? "complete" : "projected";
|
|
74
|
+
if (bodyKind !== void 0 && currentKind !== bodyKind) throw new Error(`session snapshot line ${lineNumber} cannot mix projected and complete body rows`);
|
|
75
|
+
bodyKind = currentKind;
|
|
76
|
+
if (currentKind === "projected") {
|
|
77
|
+
record[seqKey] = nextSeq;
|
|
78
|
+
record[timeKey] = 0;
|
|
79
|
+
}
|
|
80
|
+
const cardinality = physicalRowCardinality(record);
|
|
81
|
+
rowLines.push(lineNumber);
|
|
82
|
+
eventLines.push(...Array.from({ length: cardinality }, () => lineNumber));
|
|
83
|
+
nextSeq += cardinality;
|
|
84
|
+
try {
|
|
85
|
+
restore.decodeRow(record);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
throw fixtureFormatError(error, headerLineNumber, rowLines, eventLines, rowLines.length - 1);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (restore === void 0 || sourceHeader === void 0 || headerLineNumber === void 0) throw new Error("session snapshot must start with a session header");
|
|
91
|
+
try {
|
|
92
|
+
return parsedSessionFixture(restore.finish(), sourceHeader);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
throw fixtureFormatError(error, headerLineNumber, rowLines, eventLines);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/** Materialize the common replay view from a migrated artifact. */
|
|
98
|
+
function parsedSessionFixture(artifact, sourceHeader) {
|
|
99
|
+
return {
|
|
100
|
+
id: artifact.header.id,
|
|
101
|
+
createdAt: artifact.header.createdAt,
|
|
102
|
+
inheritedEventCount: SessionLogOffset(artifact.inheritedEventCount),
|
|
103
|
+
events: [...artifact.events],
|
|
104
|
+
artifact,
|
|
105
|
+
sourceHeader
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Convert one persisted or projected snapshot fixture to the current physical format in memory for expected-output comparison.
|
|
110
|
+
* Projected cwd and request-tool tokens remain tokens for comparison with a fresh run.
|
|
111
|
+
* @param text - one complete Session fixture.
|
|
112
|
+
* @returns current-format JSONL with complete event envelopes; the input string and source file remain unchanged.
|
|
113
|
+
*/
|
|
114
|
+
function prepareSessionSnapshotFixtureForComparison(text) {
|
|
115
|
+
return encodeCurrentSessionSnapshotFixture(text, parseSessionFixture(text));
|
|
116
|
+
}
|
|
117
|
+
/** Restore fixture tokens materialized only to satisfy released-format validation. */
|
|
118
|
+
function restoreProjectedRequestHeader(target, source) {
|
|
119
|
+
const targetData = target["data"];
|
|
120
|
+
const sourceData = source["data"];
|
|
121
|
+
const targetHeader = targetData["header"];
|
|
122
|
+
const sourceTools = sourceData["header"]["tools"];
|
|
123
|
+
if (sourceTools !== "{{tools}}") return target;
|
|
124
|
+
return {
|
|
125
|
+
...target,
|
|
126
|
+
data: {
|
|
127
|
+
...targetData,
|
|
128
|
+
header: {
|
|
129
|
+
...targetHeader,
|
|
130
|
+
tools: sourceTools
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/** Encode one migrated fixture while retaining projected cwd and request-tool tokens. */
|
|
136
|
+
function encodeCurrentSessionSnapshotFixture(text, parsed) {
|
|
137
|
+
const header = { ...sessionFormatCatalog.encodeCurrentHeader(parsed.artifact.header, parsed.artifact.inheritedEventCount) };
|
|
138
|
+
const sourceCwd = parsed.sourceHeader["cwd"];
|
|
139
|
+
if (typeof sourceCwd === "string" && /^\{\{cwd\}\}(?:\/|$)/.test(sourceCwd)) header["cwd"] = sourceCwd;
|
|
140
|
+
const sourceRequests = text.split(/\r?\n/).filter((line) => line.trim().length > 0).slice(1).map((line) => JSON.parse(line)).filter((row) => row["type"] === "request/header");
|
|
141
|
+
let requestIndex = 0;
|
|
142
|
+
const output = [JSON.stringify(header), ...parsed.artifact.events.map((event) => {
|
|
143
|
+
const encoded = sessionFormatCatalog.encodeCurrentEvent(event);
|
|
144
|
+
if (event.type !== "request/header") return JSON.stringify(encoded);
|
|
145
|
+
const source = sourceRequests[requestIndex++];
|
|
146
|
+
return JSON.stringify(restoreProjectedRequestHeader(encoded, source));
|
|
147
|
+
})].join("\n");
|
|
148
|
+
return text.endsWith("\n") ? `${output}\n` : output;
|
|
149
|
+
}
|
|
150
|
+
/** Omit exact request-tool sidecar tokens and materialize projected tool names for validation. */
|
|
151
|
+
function normalizeProjectedRow(source) {
|
|
152
|
+
const record = { ...source };
|
|
153
|
+
if (record["type"] !== "request/header") return record;
|
|
154
|
+
const data = record["data"];
|
|
155
|
+
if (data === null || typeof data !== "object" || Array.isArray(data)) return record;
|
|
156
|
+
const header = data["header"];
|
|
157
|
+
if (header === null || typeof header !== "object" || Array.isArray(header)) return record;
|
|
158
|
+
const tools = header["tools"];
|
|
159
|
+
const normalizedHeader = { ...header };
|
|
160
|
+
if (tools === "{{tools}}") delete normalizedHeader["tools"];
|
|
161
|
+
else if (Array.isArray(tools) && tools.length > 0 && tools.every((tool) => typeof tool === "string" && tool.length > 0)) normalizedHeader["tools"] = tools.map((name) => ({
|
|
162
|
+
name,
|
|
163
|
+
description: "",
|
|
164
|
+
parameters: {}
|
|
165
|
+
}));
|
|
166
|
+
else return record;
|
|
167
|
+
record["data"] = {
|
|
168
|
+
...data,
|
|
169
|
+
header: normalizedHeader
|
|
170
|
+
};
|
|
171
|
+
return record;
|
|
172
|
+
}
|
|
173
|
+
/** Materialize fixture-only header omissions and tokens before physical validation. */
|
|
174
|
+
function normalizeProjectedHeader(header) {
|
|
175
|
+
const normalized = { ...header };
|
|
176
|
+
if (normalized["version"] === 0 && !Object.hasOwn(header, "delegationDepth")) normalized["delegationDepth"] = 0;
|
|
177
|
+
if (typeof header["cwd"] === "string" && /^\{\{cwd\}\}(?:\/|$)/.test(header["cwd"])) normalized["cwd"] = header["cwd"].replace("{{cwd}}", "/qilin-snapshot-cwd");
|
|
178
|
+
return normalized;
|
|
179
|
+
}
|
|
180
|
+
/** Return how many logical events one physical row contributes for deterministic seq completion. */
|
|
181
|
+
function physicalRowCardinality(row) {
|
|
182
|
+
if (!PACKED_CHUNK_ROW_TYPES.has(row["type"])) return 1;
|
|
183
|
+
const data = row["data"];
|
|
184
|
+
if (data === null || typeof data !== "object" || Array.isArray(data)) return 1;
|
|
185
|
+
const payload = data[row["type"] === "tool-call-chunks" ? "args" : "texts"];
|
|
186
|
+
return Array.isArray(payload) && payload.length > 0 ? payload.length : 1;
|
|
187
|
+
}
|
|
188
|
+
/** Attach the nearest physical source line while preserving unsupported-migration classification. */
|
|
189
|
+
function fixtureFormatError(error, headerLine, rowLines, eventLines, physicalRow) {
|
|
190
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
191
|
+
const locationDetail = error instanceof Error && error.cause instanceof Error ? error.cause.message : detail;
|
|
192
|
+
const storedRow = /^released Session row (\d+)/.exec(locationDetail);
|
|
193
|
+
const event = /Session event (\d+)/.exec(locationDetail) ?? / at seq (\d+)/.exec(locationDetail) ?? /inherited Session cut (\d+)/.exec(locationDetail);
|
|
194
|
+
let line;
|
|
195
|
+
if (physicalRow !== void 0) line = rowLines[physicalRow];
|
|
196
|
+
else if (storedRow !== null) line = rowLines[Number(storedRow[1])] ?? headerLine;
|
|
197
|
+
else if (event === null) line = headerLine;
|
|
198
|
+
else line = eventLines[Number(event[1])] ?? headerLine;
|
|
199
|
+
const message = `session snapshot line ${line}: ${detail}`;
|
|
200
|
+
if (error instanceof SessionFormatUnsupportedMigrationError) return new SessionFormatUnsupportedMigrationError(message, { cause: error });
|
|
201
|
+
return new Error(message, { cause: error });
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Read replay identity, ordering, and fork-seed facts from the JSONL header.
|
|
205
|
+
*
|
|
206
|
+
* @param text - the raw `.jsonl` file contents; the complete artifact is validated and migrated.
|
|
207
|
+
* @returns the migrated header's `id`, `createdAt`, and exact inherited-event count.
|
|
208
|
+
*/
|
|
209
|
+
function parseSessionHeader(text) {
|
|
210
|
+
const parsed = parseSessionFixture(text);
|
|
211
|
+
return {
|
|
212
|
+
id: parsed.id,
|
|
213
|
+
createdAt: parsed.createdAt,
|
|
214
|
+
inheritedEventCount: parsed.inheritedEventCount
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Reconstruct the per-`stream()` replay script from a recorded session log.
|
|
219
|
+
*
|
|
220
|
+
* Reads one embedded stream from each Assistant settlement. A `compaction/summary` explicitly marked
|
|
221
|
+
* as one local LLM-stream call becomes a canonical successful stream from its
|
|
222
|
+
* complete `rawOutput` at the summary's log position. A
|
|
223
|
+
* missing assistant terminator means the live stream threw, so derivation
|
|
224
|
+
* rejects and the scenario must provide an explicit override. Multiple calls
|
|
225
|
+
* may share one turn and step when the loop retries.
|
|
226
|
+
* @param events - the recorded session's events.
|
|
227
|
+
* @returns one `chunks` entry per recorded model call, in call order.
|
|
228
|
+
*/
|
|
229
|
+
function deriveReplayScript(events) {
|
|
230
|
+
const script = [];
|
|
231
|
+
const close = (key, chunks) => {
|
|
232
|
+
if (chunks.length === 0) return;
|
|
233
|
+
if (chunks[chunks.length - 1]?.type !== "finish") throw new Error(`llm-replay: model call ${key} ended without a finish chunk (a thrown stream); this scenario needs a replay.override.json sidecar`);
|
|
234
|
+
script.push({
|
|
235
|
+
kind: "chunks",
|
|
236
|
+
chunks
|
|
237
|
+
});
|
|
238
|
+
};
|
|
239
|
+
for (const event of events) {
|
|
240
|
+
if (event.type === "compaction/summary") {
|
|
241
|
+
const persisted = event.data;
|
|
242
|
+
if (persisted.llmStreamCall === true) {
|
|
243
|
+
if (persisted.rawOutput === void 0) throw new Error("llm-replay: compaction/summary marks an LLM stream call without rawOutput");
|
|
244
|
+
const chunks = [];
|
|
245
|
+
for (const [index, block] of persisted.rawOutput.entries()) {
|
|
246
|
+
chunks.push({
|
|
247
|
+
type: "block-start",
|
|
248
|
+
index,
|
|
249
|
+
blockType: block.type
|
|
250
|
+
});
|
|
251
|
+
chunks.push({
|
|
252
|
+
type: "block-end",
|
|
253
|
+
index,
|
|
254
|
+
block
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
if (persisted.usage !== void 0) chunks.push({
|
|
258
|
+
type: "usage",
|
|
259
|
+
usage: persisted.usage
|
|
260
|
+
});
|
|
261
|
+
chunks.push({
|
|
262
|
+
type: "finish",
|
|
263
|
+
reason: { kind: "stop" }
|
|
264
|
+
});
|
|
265
|
+
script.push({
|
|
266
|
+
kind: "chunks",
|
|
267
|
+
chunks
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
if (event.type !== "assistant/message" && event.type !== "assistant/attempt") continue;
|
|
273
|
+
const chunks = expandAssistantStream(event.data.stream).map((member) => member.chunk);
|
|
274
|
+
close(`${String(event.data.turn)}/${String(event.data.step)}`, chunks);
|
|
275
|
+
}
|
|
276
|
+
return script;
|
|
277
|
+
}
|
|
278
|
+
const REPLAY_CHUNK_TYPES = new Set([
|
|
279
|
+
"block-start",
|
|
280
|
+
"text-delta",
|
|
281
|
+
"reasoning-delta",
|
|
282
|
+
"tool-call-delta",
|
|
283
|
+
"block-end",
|
|
284
|
+
"usage",
|
|
285
|
+
"finish"
|
|
286
|
+
]);
|
|
287
|
+
const FROM_REQUEST_OPEN = "{{fromRequest:";
|
|
288
|
+
const FROM_REQUEST_CLOSE = "}}";
|
|
289
|
+
/** Collect every string leaf of one JSON-compatible value, in traversal order. */
|
|
290
|
+
function collectStrings(value, out) {
|
|
291
|
+
if (typeof value === "string") {
|
|
292
|
+
out.push(value);
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (Array.isArray(value)) {
|
|
296
|
+
for (const item of value) collectStrings(item, out);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
if (value !== null && typeof value === "object") for (const item of Object.values(value)) collectStrings(item, out);
|
|
300
|
+
}
|
|
301
|
+
/** Resolve one placeholder pattern against the request corpus; the LAST match wins. */
|
|
302
|
+
function resolveFromRequest(pattern, corpus) {
|
|
303
|
+
let regex;
|
|
304
|
+
try {
|
|
305
|
+
regex = new RegExp(pattern, "g");
|
|
306
|
+
} catch (error) {
|
|
307
|
+
throw new Error(`llm-replay: fromRequest has an invalid pattern ${JSON.stringify(pattern)}: ${String(error)}`);
|
|
308
|
+
}
|
|
309
|
+
let last;
|
|
310
|
+
for (const match of corpus.matchAll(regex)) last = match;
|
|
311
|
+
if (last === void 0) throw new Error(`llm-replay: fromRequest pattern ${JSON.stringify(pattern)} matched nothing in the request`);
|
|
312
|
+
return last[1] ?? last[0];
|
|
313
|
+
}
|
|
314
|
+
/** Replace every `{{fromRequest:<pattern>}}` occurrence in one scripted string. */
|
|
315
|
+
function substituteString(text, corpus) {
|
|
316
|
+
let result = "";
|
|
317
|
+
let cursor = 0;
|
|
318
|
+
while (true) {
|
|
319
|
+
const open = text.indexOf(FROM_REQUEST_OPEN, cursor);
|
|
320
|
+
if (open === -1) return result + text.slice(cursor);
|
|
321
|
+
let close = text.indexOf(FROM_REQUEST_CLOSE, open + 14);
|
|
322
|
+
if (close === -1) throw new Error(`llm-replay: fromRequest placeholder is unterminated in ${JSON.stringify(text)}`);
|
|
323
|
+
while (text[close + 2] === "}") close += 1;
|
|
324
|
+
const pattern = text.slice(open + 14, close);
|
|
325
|
+
result += text.slice(cursor, open) + resolveFromRequest(pattern, corpus);
|
|
326
|
+
cursor = close + 2;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
/** Deep-copy one JSON-compatible value with scripted placeholders resolved. */
|
|
330
|
+
function substituteValue(value, corpus) {
|
|
331
|
+
if (typeof value === "string") return value.includes(FROM_REQUEST_OPEN) ? substituteString(value, corpus) : value;
|
|
332
|
+
if (Array.isArray(value)) return value.map((item) => substituteValue(item, corpus));
|
|
333
|
+
if (value !== null && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, substituteValue(item, corpus)]));
|
|
334
|
+
return value;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Resolve every `{{fromRequest:<regex>}}` placeholder in one scripted entry
|
|
338
|
+
* against the live request. The corpus is every string leaf of the request
|
|
339
|
+
* messages joined by newlines; the pattern's LAST corpus match wins and its
|
|
340
|
+
* first capture group (or, without one, the whole match) substitutes in place.
|
|
341
|
+
* Scenario sidecars use this to script arguments no static file can know,
|
|
342
|
+
* such as a randomly minted goal id the model must echo back. A pattern that
|
|
343
|
+
* matches nothing, an invalid pattern, and an unterminated placeholder each
|
|
344
|
+
* fail loud. The last two braces of a consecutive `}` run terminate the
|
|
345
|
+
* placeholder, so a pattern may end with a brace quantifier but cannot
|
|
346
|
+
* contain `}}` followed by further pattern content. Derived entries pass
|
|
347
|
+
* through the same resolution as sidecar entries.
|
|
348
|
+
* @param entry - the scripted entry about to replay.
|
|
349
|
+
* @param messages - the live request messages searched by the placeholders.
|
|
350
|
+
* @returns the entry itself when no placeholder appears, else a resolved deep copy.
|
|
351
|
+
*/
|
|
352
|
+
function resolveScriptedEntry(entry, messages) {
|
|
353
|
+
if (!JSON.stringify(entry).includes(FROM_REQUEST_OPEN)) return entry;
|
|
354
|
+
const leaves = [];
|
|
355
|
+
collectStrings(messages, leaves);
|
|
356
|
+
return substituteValue(entry, leaves.join("\n"));
|
|
357
|
+
}
|
|
358
|
+
/** Replace typed recorded-session tokens with the live sessions bound at the same corpus indexes. */
|
|
359
|
+
function materializeSessionTokens(entry, liveSessionIds) {
|
|
360
|
+
if (!JSON.stringify(entry).includes("{{session:")) return entry;
|
|
361
|
+
const replace = (value) => {
|
|
362
|
+
if (typeof value === "string") return value.replace(/\{\{session:([1-9]\d*)\}\}/g, (_token, ordinal) => {
|
|
363
|
+
const live = liveSessionIds[Number(ordinal) - 1];
|
|
364
|
+
if (live === void 0) throw new Error(`llm-replay: session token {{session:${ordinal}}} was used before that recorded session bound`);
|
|
365
|
+
return live;
|
|
366
|
+
});
|
|
367
|
+
if (Array.isArray(value)) return value.map(replace);
|
|
368
|
+
if (value !== null && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, replace(item)]));
|
|
369
|
+
return value;
|
|
370
|
+
};
|
|
371
|
+
return replace(entry);
|
|
372
|
+
}
|
|
373
|
+
/** Learn a background child id from the stable tool-result text before that child reaches its first model call. */
|
|
374
|
+
function inferStartedSubagents(messages, liveSessionIds) {
|
|
375
|
+
const leaves = [];
|
|
376
|
+
collectStrings(messages, leaves);
|
|
377
|
+
for (const leaf of leaves) for (const match of leaf.matchAll(/started subagent ([^\s"'<>]+)/g)) {
|
|
378
|
+
const id = match[1];
|
|
379
|
+
/* v8 ignore next -- the fixed regular expression always has capture group 1. */
|
|
380
|
+
if (id === void 0 || liveSessionIds.includes(id)) continue;
|
|
381
|
+
const index = liveSessionIds.findIndex((value, candidate) => candidate > 0 && value === void 0);
|
|
382
|
+
if (index < 0) return;
|
|
383
|
+
liveSessionIds[index] = id;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
function isRecord(value) {
|
|
387
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
388
|
+
}
|
|
389
|
+
function hasExactKeys(value, keys) {
|
|
390
|
+
return Object.keys(value).length === keys.length && keys.every((key) => Object.hasOwn(value, key));
|
|
391
|
+
}
|
|
392
|
+
function invalidOverride(file, location, detail) {
|
|
393
|
+
throw new Error(`llm-replay: invalid override ${file}: ${location} ${detail}`);
|
|
394
|
+
}
|
|
395
|
+
function readChunks(value, file, location) {
|
|
396
|
+
if (!Array.isArray(value)) invalidOverride(file, location, "chunks must be an array");
|
|
397
|
+
for (const [index, chunk] of value.entries()) if (!isRecord(chunk) || typeof chunk["type"] !== "string" || !REPLAY_CHUNK_TYPES.has(chunk["type"])) invalidOverride(file, `${location}.chunks[${index}]`, "must have a known StreamChunk type");
|
|
398
|
+
return value;
|
|
399
|
+
}
|
|
400
|
+
function readReplayEntry(value, file, location) {
|
|
401
|
+
if (!isRecord(value)) invalidOverride(file, location, "must be an object");
|
|
402
|
+
switch (value["kind"]) {
|
|
403
|
+
case "chunks":
|
|
404
|
+
if (!hasExactKeys(value, ["kind", "chunks"])) invalidOverride(file, location, "has invalid chunks-entry fields");
|
|
405
|
+
return {
|
|
406
|
+
kind: "chunks",
|
|
407
|
+
chunks: readChunks(value["chunks"], file, location)
|
|
408
|
+
};
|
|
409
|
+
case "throw": {
|
|
410
|
+
const accepted = value["accepted"];
|
|
411
|
+
if (!hasExactKeys(value, accepted === void 0 ? [
|
|
412
|
+
"kind",
|
|
413
|
+
"chunks",
|
|
414
|
+
"message",
|
|
415
|
+
"code"
|
|
416
|
+
] : [
|
|
417
|
+
"kind",
|
|
418
|
+
"chunks",
|
|
419
|
+
"message",
|
|
420
|
+
"code",
|
|
421
|
+
"accepted"
|
|
422
|
+
])) invalidOverride(file, location, "has invalid throw-entry fields");
|
|
423
|
+
if (typeof value["message"] !== "string" || value["message"].length === 0) invalidOverride(file, location, "message must be a non-empty string");
|
|
424
|
+
if (typeof value["code"] !== "string" || value["code"].length === 0) invalidOverride(file, location, "code must be a non-empty string");
|
|
425
|
+
if (accepted !== void 0 && typeof accepted !== "boolean") invalidOverride(file, location, "accepted must be a boolean");
|
|
426
|
+
return {
|
|
427
|
+
kind: "throw",
|
|
428
|
+
chunks: readChunks(value["chunks"], file, location),
|
|
429
|
+
message: value["message"],
|
|
430
|
+
code: value["code"],
|
|
431
|
+
...accepted === void 0 ? {} : { accepted }
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
case "hang": {
|
|
435
|
+
const readyFile = value["readyFile"];
|
|
436
|
+
if (!hasExactKeys(value, readyFile === void 0 ? ["kind"] : ["kind", "readyFile"])) invalidOverride(file, location, "has invalid hang-entry fields");
|
|
437
|
+
if (readyFile !== void 0 && (typeof readyFile !== "string" || readyFile.length === 0)) invalidOverride(file, location, "readyFile must be a non-empty string");
|
|
438
|
+
return {
|
|
439
|
+
kind: "hang",
|
|
440
|
+
...readyFile === void 0 ? {} : { readyFile }
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
default: return invalidOverride(file, location, `has unknown kind ${JSON.stringify(value["kind"])}`);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
function readOverrideDoc(value, file) {
|
|
447
|
+
if (Array.isArray(value)) return value.map((entry, index) => readReplayEntry(entry, file, `entry ${index}`));
|
|
448
|
+
if (!isRecord(value) || !hasExactKeys(value, ["patches"]) || !Array.isArray(value["patches"])) return invalidOverride(file, "document", "must be a ReplayEntry[] or { patches: [...] }");
|
|
449
|
+
return { patches: value["patches"].map((value, index) => {
|
|
450
|
+
const location = `patch ${index}`;
|
|
451
|
+
if (!isRecord(value) || !hasExactKeys(value, ["at", "entry"])) return invalidOverride(file, location, "must contain exactly at and entry");
|
|
452
|
+
const at = value["at"];
|
|
453
|
+
if (typeof at !== "number" || !Number.isSafeInteger(at) || at < 0) return invalidOverride(file, location, "at must be a non-negative safe integer");
|
|
454
|
+
return {
|
|
455
|
+
at,
|
|
456
|
+
entry: readReplayEntry(value["entry"], file, `${location}.entry`)
|
|
457
|
+
};
|
|
458
|
+
}) };
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Load the PRIMARY session's replay script: the sidecar override when present
|
|
462
|
+
* (whole-script replacement or `{ patches }` augmentation over the derived
|
|
463
|
+
* script), else the script derived from the session JSONL (fail-loud when the
|
|
464
|
+
* fixture is missing).
|
|
465
|
+
* @param config - the fixture paths; only `file` and `overrideFile` are consulted.
|
|
466
|
+
* @returns the resolved primary-session script.
|
|
467
|
+
*/
|
|
468
|
+
function loadReplayScript(config) {
|
|
469
|
+
return resolveReplayScript(config, readPrimaryFixture(config));
|
|
470
|
+
}
|
|
471
|
+
/** Read a primary JSONL unless a whole-script sidecar intentionally occupies the same path. */
|
|
472
|
+
function readPrimaryFixture(config) {
|
|
473
|
+
if (!existsSync(config.file) || config.file === config.overrideFile) return void 0;
|
|
474
|
+
return parseSessionFixture(readFileSync(config.file, "utf8"));
|
|
475
|
+
}
|
|
476
|
+
/** Resolve an override or derive from one already validated and migrated fixture. */
|
|
477
|
+
function resolveReplayScript(config, fixture) {
|
|
478
|
+
if (config.overrideFile !== void 0 && existsSync(config.overrideFile)) {
|
|
479
|
+
const doc = readOverrideDoc(JSON.parse(readFileSync(config.overrideFile, "utf8")), config.overrideFile);
|
|
480
|
+
if (Array.isArray(doc)) return doc;
|
|
481
|
+
const script = deriveScriptFromFixture(config.file, fixture);
|
|
482
|
+
const derivedLength = script.length;
|
|
483
|
+
const seenIndexes = /* @__PURE__ */ new Set();
|
|
484
|
+
for (const patch of doc.patches) {
|
|
485
|
+
if (patch.at > derivedLength) throw new Error(`llm-replay: override patch index ${String(patch.at)} out of range (derived script has ${derivedLength} call(s); == length appends): ${config.overrideFile}`);
|
|
486
|
+
if (seenIndexes.has(patch.at)) throw new Error(`llm-replay: duplicate override patch index ${patch.at}: ${config.overrideFile}`);
|
|
487
|
+
seenIndexes.add(patch.at);
|
|
488
|
+
script[patch.at] = patch.entry;
|
|
489
|
+
}
|
|
490
|
+
return script;
|
|
491
|
+
}
|
|
492
|
+
return deriveScriptFromFixture(config.file, fixture);
|
|
493
|
+
}
|
|
494
|
+
/** Derive a script from an already migrated fixture, failing loud when it is absent. */
|
|
495
|
+
function deriveScriptFromFixture(file, fixture) {
|
|
496
|
+
if (fixture === void 0) throw new Error(`llm-replay: fixture not found: ${file} — run \`pnpm run test:snapshot:record\` first`);
|
|
497
|
+
return deriveReplayScript(fixture.events);
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Load the primary and child scripts in bind order. Child derivation begins at
|
|
501
|
+
* the v0 header's inherited-event cut so parent chunks are never replayed as child calls.
|
|
502
|
+
*
|
|
503
|
+
* @param config - the fixture paths: the primary log plus any recorded child logs.
|
|
504
|
+
* @returns the primary script first, then the child scripts in bind order.
|
|
505
|
+
*/
|
|
506
|
+
function loadSessionScripts(config) {
|
|
507
|
+
const primaryFixture = readPrimaryFixture(config);
|
|
508
|
+
const primaryEntries = resolveReplayScript(config, primaryFixture);
|
|
509
|
+
const primaryHeader = primaryFixture ?? {
|
|
510
|
+
id: "",
|
|
511
|
+
createdAt: 0
|
|
512
|
+
};
|
|
513
|
+
const primary = {
|
|
514
|
+
recordedId: primaryHeader.id,
|
|
515
|
+
createdAt: primaryHeader.createdAt,
|
|
516
|
+
entries: primaryEntries,
|
|
517
|
+
primary: true
|
|
518
|
+
};
|
|
519
|
+
const children = [];
|
|
520
|
+
for (const childFile of config.childFiles ?? []) {
|
|
521
|
+
if (!existsSync(childFile)) throw new Error(`llm-replay: child fixture not found: ${childFile} — re-record the scenario`);
|
|
522
|
+
const fixture = parseSessionFixture(readFileSync(childFile, "utf8"));
|
|
523
|
+
const ownEvents = fixture.events.slice(fixture.inheritedEventCount);
|
|
524
|
+
children.push({
|
|
525
|
+
recordedId: fixture.id,
|
|
526
|
+
createdAt: fixture.createdAt,
|
|
527
|
+
entries: deriveReplayScript(ownEvents),
|
|
528
|
+
primary: false
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
children.sort((a, b) => a.createdAt - b.createdAt || a.recordedId.localeCompare(b.recordedId));
|
|
532
|
+
return [primary, ...children];
|
|
533
|
+
}
|
|
534
|
+
/** Replay adapter that makes a configured provider catalog discoverable without provider I/O. */
|
|
535
|
+
var ReplayAdapter = class extends LlmAdapter {
|
|
536
|
+
replay;
|
|
537
|
+
providers;
|
|
538
|
+
constructor(providers, replay) {
|
|
539
|
+
super();
|
|
540
|
+
this.replay = replay;
|
|
541
|
+
this.providers = new Map(providers.map((provider) => [provider.id, provider]));
|
|
542
|
+
}
|
|
543
|
+
providerInfo(provider) {
|
|
544
|
+
const configured = this.providers.get(provider);
|
|
545
|
+
/* v8 ignore next -- LlmRuntime only asks about routes registered from this same map. */
|
|
546
|
+
if (configured === void 0) return super.providerInfo(provider);
|
|
547
|
+
return {
|
|
548
|
+
id: provider,
|
|
549
|
+
name: configured.name ?? provider
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
providerRetryPolicy(provider) {
|
|
553
|
+
const configured = this.providers.get(provider);
|
|
554
|
+
/* v8 ignore next -- LlmRuntime only asks about routes registered from this same map. */
|
|
555
|
+
if (configured === void 0) return super.providerRetryPolicy(provider);
|
|
556
|
+
return configured.retryPolicy === void 0 ? void 0 : resolveRetryPolicy(configured.retryPolicy, `llm-replay: provider "${provider}" retryPolicy`);
|
|
557
|
+
}
|
|
558
|
+
imageRequestPricing(provider, model) {
|
|
559
|
+
const visualTokens = this.providers.get(provider)?.models?.find((candidate) => candidate.id === model)?.imageRequestTokens;
|
|
560
|
+
if (visualTokens === void 0) return void 0;
|
|
561
|
+
return { priceImages: (images) => images.map(({ attachment: ref, offloaded }) => offloaded === true ? {
|
|
562
|
+
visualTokens: 0,
|
|
563
|
+
text: offloadedImageText(ref)
|
|
564
|
+
} : {
|
|
565
|
+
visualTokens,
|
|
566
|
+
text: requestImageHandleText(ref, {
|
|
567
|
+
width: ref.width,
|
|
568
|
+
height: ref.height
|
|
569
|
+
})
|
|
570
|
+
}) };
|
|
571
|
+
}
|
|
572
|
+
listModels(provider) {
|
|
573
|
+
const configured = this.providers.get(provider);
|
|
574
|
+
/* v8 ignore next -- LlmRuntime only asks about routes registered from this same map. */
|
|
575
|
+
if (configured === void 0) return Promise.resolve([]);
|
|
576
|
+
return Promise.resolve((configured.models ?? []).map((model) => ({
|
|
577
|
+
provider,
|
|
578
|
+
id: model.id,
|
|
579
|
+
name: model.name ?? model.id,
|
|
580
|
+
...model.description === void 0 ? {} : { description: model.description },
|
|
581
|
+
...model.inputModalities === void 0 ? {} : { inputModalities: [...model.inputModalities] }
|
|
582
|
+
})));
|
|
583
|
+
}
|
|
584
|
+
resolveModel(provider, model) {
|
|
585
|
+
const configured = this.providers.get(provider);
|
|
586
|
+
/* v8 ignore next -- LlmRuntime only asks about routes registered from this same map. */
|
|
587
|
+
if (configured === void 0) return Promise.resolve({
|
|
588
|
+
provider,
|
|
589
|
+
id: model,
|
|
590
|
+
name: model
|
|
591
|
+
});
|
|
592
|
+
const configuredModel = configured.models?.find((candidate) => candidate.id === model);
|
|
593
|
+
return Promise.resolve({
|
|
594
|
+
provider,
|
|
595
|
+
id: model,
|
|
596
|
+
name: configuredModel?.name ?? model,
|
|
597
|
+
...configuredModel?.description === void 0 ? {} : { description: configuredModel.description },
|
|
598
|
+
...configuredModel?.inputModalities === void 0 ? {} : { inputModalities: [...configuredModel.inputModalities] },
|
|
599
|
+
...configuredModel?.contextWindow === void 0 ? {} : { context: { contextWindow: configuredModel.contextWindow } },
|
|
600
|
+
...configuredModel?.defaultMaxTokens === void 0 ? {} : { defaultMaxTokens: configuredModel.defaultMaxTokens },
|
|
601
|
+
...configuredModel?.systemPromptUpdate === void 0 ? {} : { systemPromptUpdate: configuredModel.systemPromptUpdate },
|
|
602
|
+
...configuredModel?.toolUpdate === void 0 ? {} : { toolUpdate: configuredModel.toolUpdate },
|
|
603
|
+
...configuredModel?.reasoningEfforts === void 0 ? {} : { reasoning: {
|
|
604
|
+
efforts: configuredModel.reasoningEfforts.map((id) => ({
|
|
605
|
+
id: ReasoningEffortId(id),
|
|
606
|
+
name: id
|
|
607
|
+
})),
|
|
608
|
+
...configuredModel.defaultReasoningEffort === void 0 ? {} : { defaultEffort: ReasoningEffortId(configuredModel.defaultReasoningEffort) }
|
|
609
|
+
} }
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
stream(options) {
|
|
613
|
+
return this.replay(options);
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
/**
|
|
617
|
+
* Wait `paceMs` between chunk yields, aborting the wait (and the stream) the
|
|
618
|
+
* moment the signal fires — a paced replay must cancel as promptly as a burst
|
|
619
|
+
* one.
|
|
620
|
+
*/
|
|
621
|
+
function paceDelay(paceMs, signal) {
|
|
622
|
+
return new Promise((resolve, reject) => {
|
|
623
|
+
const timer = setTimeout(() => {
|
|
624
|
+
signal?.removeEventListener("abort", onAbort);
|
|
625
|
+
resolve();
|
|
626
|
+
}, paceMs);
|
|
627
|
+
const onAbort = () => {
|
|
628
|
+
clearTimeout(timer);
|
|
629
|
+
reject(/* @__PURE__ */ new Error("aborted"));
|
|
630
|
+
};
|
|
631
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
/** Yield a recorded stream back, honoring abort like a real adapter. */
|
|
635
|
+
async function* replayEntry(entry, signal, paceMs) {
|
|
636
|
+
switch (entry.kind) {
|
|
637
|
+
case "chunks":
|
|
638
|
+
for (const chunk of entry.chunks) {
|
|
639
|
+
if (signal?.aborted) throw new Error("aborted");
|
|
640
|
+
if (paceMs > 0) await paceDelay(paceMs, signal);
|
|
641
|
+
yield chunk;
|
|
642
|
+
}
|
|
643
|
+
return;
|
|
644
|
+
case "throw":
|
|
645
|
+
for (const chunk of entry.chunks) {
|
|
646
|
+
if (signal?.aborted) throw new Error("aborted");
|
|
647
|
+
if (paceMs > 0) await paceDelay(paceMs, signal);
|
|
648
|
+
yield chunk;
|
|
649
|
+
}
|
|
650
|
+
throw new LlmError(entry.message, entry.code);
|
|
651
|
+
case "hang":
|
|
652
|
+
yield {
|
|
653
|
+
type: "block-start",
|
|
654
|
+
index: 0,
|
|
655
|
+
blockType: "text"
|
|
656
|
+
};
|
|
657
|
+
yield {
|
|
658
|
+
type: "text-delta",
|
|
659
|
+
index: 0,
|
|
660
|
+
text: "partial"
|
|
661
|
+
};
|
|
662
|
+
if (entry.readyFile !== void 0) writeFileSync(entry.readyFile, "");
|
|
663
|
+
await new Promise((_resolve, reject) => {
|
|
664
|
+
if (signal?.aborted) {
|
|
665
|
+
reject(/* @__PURE__ */ new Error("aborted"));
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
signal?.addEventListener("abort", () => {
|
|
669
|
+
reject(/* @__PURE__ */ new Error("aborted"));
|
|
670
|
+
}, { once: true });
|
|
671
|
+
});
|
|
672
|
+
/* v8 ignore next -- unreachable: the hang promise only ever rejects (on abort), never resolves; control never reaches here */
|
|
673
|
+
return;
|
|
674
|
+
/* v8 ignore next -- sidecar entries are validated before they reach the closed local union. */
|
|
675
|
+
default: return assertNever(entry, "llm-replay replay entry");
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
/** Whether the scripted provider call reached the live adapter's post-2xx commit point. */
|
|
679
|
+
function providerAccepted(entry) {
|
|
680
|
+
switch (entry.kind) {
|
|
681
|
+
case "chunks":
|
|
682
|
+
case "hang": return true;
|
|
683
|
+
case "throw": return entry.accepted ?? entry.chunks.length > 0;
|
|
684
|
+
/* v8 ignore next -- override parsing and derived entries close the local union before replay. */
|
|
685
|
+
default: return assertNever(entry, "llm-replay acceptance entry");
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Install per-session positional replay. A newly seen live session takes the
|
|
690
|
+
* next ordered recorded script, then advances its own cursor synchronously at
|
|
691
|
+
* invocation time; calls without `sessionId` share one anonymous session. A
|
|
692
|
+
* non-empty provider catalog registers a routed replay adapter; otherwise a
|
|
693
|
+
* catch-all waterfall intercepts requests.
|
|
694
|
+
*
|
|
695
|
+
* @param ctx - the context whose LLM service receives the replay route or waterfall.
|
|
696
|
+
* @param config - the resolved fixture paths (env-var defaulting is `apply`'s job).
|
|
697
|
+
* @returns the {@link ReplayHandle} carrying the disposer and the teardown consumption check.
|
|
698
|
+
*/
|
|
699
|
+
function installLlmReplay(ctx, config) {
|
|
700
|
+
const paceMs = config.paceMs ?? 0;
|
|
701
|
+
if (!Number.isInteger(paceMs) || paceMs < 0) throw new Error(`llm-replay: paceMs must be a non-negative integer, got ${String(config.paceMs)}`);
|
|
702
|
+
const scripts = loadSessionScripts(config);
|
|
703
|
+
const bound = /* @__PURE__ */ new Map();
|
|
704
|
+
const liveSessionIds = Array.from({ length: scripts.length });
|
|
705
|
+
let nextScript = 0;
|
|
706
|
+
const ANON = "\0anon\0";
|
|
707
|
+
const replay = (options) => {
|
|
708
|
+
const key = options.sessionId ?? ANON;
|
|
709
|
+
let state = bound.get(key);
|
|
710
|
+
let unrecorded = false;
|
|
711
|
+
if (state === void 0) {
|
|
712
|
+
const script = scripts[nextScript];
|
|
713
|
+
if (script === void 0) {
|
|
714
|
+
unrecorded = true;
|
|
715
|
+
state = {
|
|
716
|
+
entries: [],
|
|
717
|
+
cursor: 0
|
|
718
|
+
};
|
|
719
|
+
} else {
|
|
720
|
+
const scriptIndex = nextScript;
|
|
721
|
+
nextScript++;
|
|
722
|
+
state = {
|
|
723
|
+
entries: script.entries,
|
|
724
|
+
cursor: 0
|
|
725
|
+
};
|
|
726
|
+
bound.set(key, state);
|
|
727
|
+
if (key !== ANON) liveSessionIds[scriptIndex] = key;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
const boundState = state;
|
|
731
|
+
const seenSessions = nextScript;
|
|
732
|
+
const totalScripts = scripts.length;
|
|
733
|
+
const index = boundState.cursor++;
|
|
734
|
+
const entry = boundState.entries[index];
|
|
735
|
+
return (async function* () {
|
|
736
|
+
if (unrecorded) throw new Error(`llm-replay: a model call arrived from an unrecorded session (#${seenSessions + 1}); the scenario recorded only ${totalScripts} session(s) — re-record it`);
|
|
737
|
+
if (entry === void 0) throw new Error(`llm-replay: script exhausted — session requested model call #${index + 1} but its script has only ${boundState.entries.length}; re-record the scenario`);
|
|
738
|
+
inferStartedSubagents(options.messages, liveSessionIds);
|
|
739
|
+
const resolved = resolveScriptedEntry(materializeSessionTokens(entry, liveSessionIds), options.messages);
|
|
740
|
+
if (options.provider === "deepseek-official" && providerAccepted(resolved)) {
|
|
741
|
+
const extensions = ctx.get("deepseekLlmApiExtensions");
|
|
742
|
+
if (extensions !== void 0) {
|
|
743
|
+
const signal = options.signal ?? new AbortController().signal;
|
|
744
|
+
await (await extensions.prepare({
|
|
745
|
+
body: { messages: [] },
|
|
746
|
+
signal,
|
|
747
|
+
...options.sessionId === void 0 ? {} : { sessionId: String(options.sessionId) },
|
|
748
|
+
...options.purpose === void 0 ? {} : { purpose: options.purpose }
|
|
749
|
+
})).accept();
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
yield* replayEntry(resolved, options.signal, paceMs);
|
|
753
|
+
})();
|
|
754
|
+
};
|
|
755
|
+
const providers = config.providers ?? [];
|
|
756
|
+
return {
|
|
757
|
+
dispose: providers.length > 0 ? ctx.llm.registerAdapter(providers.map((provider) => provider.id), new ReplayAdapter(providers, replay)) : ctx.on("llm/stream", (options, _next) => replay(options)),
|
|
758
|
+
assertConsumed() {
|
|
759
|
+
const problems = [];
|
|
760
|
+
if (nextScript < scripts.length) problems.push(`${scripts.length - nextScript} recorded script(s) never bound to a live session`);
|
|
761
|
+
for (const [key, state] of bound) if (state.cursor < state.entries.length) {
|
|
762
|
+
const who = key === ANON ? "the anonymous session" : `session ${key}`;
|
|
763
|
+
problems.push(`${who} consumed ${state.cursor}/${state.entries.length} recorded call(s)`);
|
|
764
|
+
}
|
|
765
|
+
if (problems.length > 0) throw new Error(`llm-replay: fixture not fully consumed — ${problems.join("; ")}; the scenario drove fewer model calls than recorded`);
|
|
766
|
+
}
|
|
767
|
+
};
|
|
768
|
+
}
|
|
769
|
+
const name = "llm-replay";
|
|
770
|
+
const inject = ["llm"];
|
|
771
|
+
function validateConfiguredModels(providers) {
|
|
772
|
+
for (const provider of providers ?? []) for (const model of provider.models ?? []) {
|
|
773
|
+
const modalities = model.inputModalities;
|
|
774
|
+
if (modalities !== void 0 && (!Array.isArray(modalities) || !modalities.every((modality) => modality === "text" || modality === "image"))) throw new Error(`llm-replay: provider "${provider.id}" model "${model.id}" inputModalities must be an array containing only "text" and "image"`);
|
|
775
|
+
const imageRequestTokens = model.imageRequestTokens;
|
|
776
|
+
if (imageRequestTokens !== void 0 && (!Number.isSafeInteger(imageRequestTokens) || imageRequestTokens <= 0)) throw new Error(`llm-replay: provider "${provider.id}" model "${model.id}" imageRequestTokens must be a positive safe integer`);
|
|
777
|
+
if (imageRequestTokens !== void 0 && model.inputModalities?.includes("image") !== true) throw new Error(`llm-replay: provider "${provider.id}" model "${model.id}" imageRequestTokens requires inputModalities to include "image"`);
|
|
778
|
+
const systemPromptUpdate = model.systemPromptUpdate;
|
|
779
|
+
if (systemPromptUpdate !== void 0 && systemPromptUpdate !== "in-history") throw new Error(`llm-replay: provider "${provider.id}" model "${model.id}" systemPromptUpdate must be "in-history" when present`);
|
|
780
|
+
const toolUpdate = model.toolUpdate;
|
|
781
|
+
if (toolUpdate !== void 0 && toolUpdate !== "in-history" && toolUpdate !== "addition-only") throw new Error(`llm-replay: provider "${provider.id}" model "${model.id}" toolUpdate must be "in-history" or "addition-only" when present`);
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
function apply(ctx, config = {}) {
|
|
785
|
+
const file = config.file ?? process.env.QILIN_SNAPSHOT_FILE;
|
|
786
|
+
if (file === void 0 || file.length === 0) throw new Error("llm-replay: a fixture path is required (Config.file or $QILIN_SNAPSHOT_FILE)");
|
|
787
|
+
validateConfiguredModels(config.providers);
|
|
788
|
+
const overrideFile = config.overrideFile ?? process.env.QILIN_SNAPSHOT_OVERRIDE;
|
|
789
|
+
const childEnv = process.env.QILIN_SNAPSHOT_CHILD_FILES;
|
|
790
|
+
const childFiles = config.childFiles ?? (childEnv !== void 0 && childEnv.length > 0 ? childEnv.split(delimiter) : []);
|
|
791
|
+
installLlmReplay(ctx, {
|
|
792
|
+
file,
|
|
793
|
+
...overrideFile !== void 0 && overrideFile.length > 0 ? { overrideFile } : {},
|
|
794
|
+
...childFiles.length > 0 ? { childFiles } : {},
|
|
795
|
+
...config.providers !== void 0 ? { providers: config.providers } : {},
|
|
796
|
+
...config.paceMs !== void 0 ? { paceMs: config.paceMs } : {}
|
|
797
|
+
});
|
|
798
|
+
}
|
|
799
|
+
//#endregion
|
|
800
|
+
export { apply, deriveReplayScript, inject, installLlmReplay, loadReplayScript, loadSessionScripts, name, parseSessionHeader, parseSessionLog, prepareSessionSnapshotFixtureForComparison, resolveScriptedEntry };
|