@tangle-network/agent-eval 0.20.11 → 0.21.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 +76 -0
- package/README.md +137 -170
- package/dist/benchmarks/index.d.ts +2 -1
- package/dist/{chunk-JAOLXRIA.js → chunk-3GN6U53I.js} +205 -4
- package/dist/chunk-3GN6U53I.js.map +1 -0
- package/dist/chunk-3IX6QTB7.js +1349 -0
- package/dist/chunk-3IX6QTB7.js.map +1 -0
- package/dist/chunk-5IIQKMD5.js +236 -0
- package/dist/chunk-5IIQKMD5.js.map +1 -0
- package/dist/chunk-ARZ6BEV6.js +1310 -0
- package/dist/chunk-ARZ6BEV6.js.map +1 -0
- package/dist/chunk-HRZELXCR.js +1354 -0
- package/dist/chunk-HRZELXCR.js.map +1 -0
- package/dist/chunk-KRR4VMH7.js +423 -0
- package/dist/chunk-KRR4VMH7.js.map +1 -0
- package/dist/chunk-SNUHRBDL.js +154 -0
- package/dist/chunk-SNUHRBDL.js.map +1 -0
- package/dist/chunk-WOK2RTWG.js +1920 -0
- package/dist/chunk-WOK2RTWG.js.map +1 -0
- package/dist/{chunk-LSR4IAYN.js → chunk-WOPGKVN4.js} +2 -2
- package/dist/chunk-YUFXO3TU.js +148 -0
- package/dist/chunk-YUFXO3TU.js.map +1 -0
- package/dist/cli.js +3 -2
- package/dist/cli.js.map +1 -1
- package/dist/control-cxwMOAsy.d.ts +259 -0
- package/dist/control.d.ts +6 -0
- package/dist/control.js +30 -0
- package/dist/control.js.map +1 -0
- package/dist/dataset-B9qvlm_o.d.ts +112 -0
- package/dist/emitter-B2XqDKFU.d.ts +121 -0
- package/dist/feedback-trajectory-CB0A32o3.d.ts +346 -0
- package/dist/{index-1PZOtZFr.d.ts → index-c5saLbKD.d.ts} +2 -133
- package/dist/index.d.ts +178 -2945
- package/dist/index.js +1066 -6185
- package/dist/index.js.map +1 -1
- package/dist/multi-shot-optimization-Bvtz294B.d.ts +598 -0
- package/dist/openapi.json +1 -1
- package/dist/optimization.d.ts +146 -0
- package/dist/optimization.js +60 -0
- package/dist/optimization.js.map +1 -0
- package/dist/reporting-Da2ihlcM.d.ts +672 -0
- package/dist/reporting.d.ts +5 -0
- package/dist/reporting.js +36 -0
- package/dist/reporting.js.map +1 -0
- package/dist/run-record-CX_jcAyr.d.ts +134 -0
- package/dist/store-u47QaJ9G.d.ts +297 -0
- package/dist/traces.d.ts +914 -0
- package/dist/traces.js +120 -0
- package/dist/traces.js.map +1 -0
- package/dist/wire/index.js +3 -2
- package/docs/concepts.md +16 -11
- package/docs/feature-guide.md +10 -17
- package/docs/integration-launch-gates.md +77 -0
- package/docs/product-eval-adoption.md +27 -0
- package/docs/research-report-methodology.md +155 -0
- package/docs/trace-analysis.md +75 -0
- package/package.json +30 -12
- package/dist/chunk-JAOLXRIA.js.map +0 -1
- /package/dist/{chunk-LSR4IAYN.js.map → chunk-WOPGKVN4.js.map} +0 -0
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defaultProviderRedactor,
|
|
3
|
+
providerFromBaseUrl
|
|
4
|
+
} from "./chunk-SNUHRBDL.js";
|
|
5
|
+
|
|
1
6
|
// src/llm-client.ts
|
|
2
7
|
var LlmCallError = class extends Error {
|
|
3
8
|
constructor(message, status, body, model) {
|
|
@@ -57,7 +62,10 @@ function buildBody(req, forceJsonObject) {
|
|
|
57
62
|
messages: req.messages,
|
|
58
63
|
temperature: req.temperature ?? 0
|
|
59
64
|
};
|
|
60
|
-
if (req.maxTokens != null)
|
|
65
|
+
if (req.maxTokens != null) {
|
|
66
|
+
if (usesMaxCompletionTokens(req.model)) body.max_completion_tokens = req.maxTokens;
|
|
67
|
+
else body.max_tokens = req.maxTokens;
|
|
68
|
+
}
|
|
61
69
|
if (req.jsonSchema && !forceJsonObject) {
|
|
62
70
|
body.response_format = {
|
|
63
71
|
type: "json_schema",
|
|
@@ -68,6 +76,9 @@ function buildBody(req, forceJsonObject) {
|
|
|
68
76
|
}
|
|
69
77
|
return body;
|
|
70
78
|
}
|
|
79
|
+
function usesMaxCompletionTokens(model) {
|
|
80
|
+
return /^gpt-5(?:[.\-]|$)/i.test(model);
|
|
81
|
+
}
|
|
71
82
|
async function sleep(ms) {
|
|
72
83
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
73
84
|
}
|
|
@@ -129,25 +140,71 @@ function extractBalancedJson(input, start) {
|
|
|
129
140
|
async function callLlm(req, opts = {}) {
|
|
130
141
|
const baseUrl = (opts.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
131
142
|
const url = `${baseUrl}/chat/completions`;
|
|
143
|
+
const endpoint = "/chat/completions";
|
|
132
144
|
const timeoutMs = req.timeoutMs ?? opts.defaultTimeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
133
145
|
const maxRetries = opts.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
134
146
|
const fetchFn = opts.fetch ?? globalThis.fetch;
|
|
135
147
|
const headers = buildHeaders(opts);
|
|
148
|
+
const provider = opts.provider ?? providerFromBaseUrl(baseUrl);
|
|
149
|
+
const sink = opts.rawSink;
|
|
150
|
+
const redactor = opts.redactor ?? defaultProviderRedactor;
|
|
151
|
+
const traceContext = opts.traceContext;
|
|
136
152
|
let lastErr;
|
|
137
153
|
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
138
154
|
const controller = new AbortController();
|
|
139
155
|
const timeoutHandle = setTimeout(() => controller.abort(), timeoutMs);
|
|
140
156
|
const started = Date.now();
|
|
157
|
+
const requestBody = buildBody(req, false);
|
|
158
|
+
let attemptErrorRecorded = false;
|
|
159
|
+
if (sink) {
|
|
160
|
+
await recordRaw(sink, redactor, {
|
|
161
|
+
eventId: cryptoEventId(),
|
|
162
|
+
runId: traceContext?.runId,
|
|
163
|
+
spanId: traceContext?.spanId,
|
|
164
|
+
provider,
|
|
165
|
+
model: req.model,
|
|
166
|
+
endpoint,
|
|
167
|
+
baseUrl,
|
|
168
|
+
attemptIndex: attempt,
|
|
169
|
+
direction: "request",
|
|
170
|
+
timestamp: started,
|
|
171
|
+
requestHeaders: headers,
|
|
172
|
+
requestBody,
|
|
173
|
+
redactedFields: []
|
|
174
|
+
});
|
|
175
|
+
}
|
|
141
176
|
try {
|
|
142
177
|
const res = await fetchFn(url, {
|
|
143
178
|
method: "POST",
|
|
144
179
|
headers,
|
|
145
|
-
body: JSON.stringify(
|
|
180
|
+
body: JSON.stringify(requestBody),
|
|
146
181
|
signal: controller.signal
|
|
147
182
|
});
|
|
148
183
|
clearTimeout(timeoutHandle);
|
|
184
|
+
const responseHeaders = sink ? headersToObject(res.headers) : void 0;
|
|
149
185
|
if (!res.ok) {
|
|
150
186
|
const body = await res.text();
|
|
187
|
+
if (sink) {
|
|
188
|
+
await recordRaw(sink, redactor, {
|
|
189
|
+
eventId: cryptoEventId(),
|
|
190
|
+
runId: traceContext?.runId,
|
|
191
|
+
spanId: traceContext?.spanId,
|
|
192
|
+
provider,
|
|
193
|
+
model: req.model,
|
|
194
|
+
endpoint,
|
|
195
|
+
baseUrl,
|
|
196
|
+
attemptIndex: attempt,
|
|
197
|
+
direction: "error",
|
|
198
|
+
timestamp: Date.now(),
|
|
199
|
+
durationMs: Date.now() - started,
|
|
200
|
+
statusCode: res.status,
|
|
201
|
+
responseHeaders,
|
|
202
|
+
responseBody: body,
|
|
203
|
+
errorMessage: `HTTP ${res.status}`,
|
|
204
|
+
redactedFields: []
|
|
205
|
+
});
|
|
206
|
+
attemptErrorRecorded = true;
|
|
207
|
+
}
|
|
151
208
|
const err = new LlmCallError(
|
|
152
209
|
`LLM call ${res.status}: ${body.slice(0, 300)}`,
|
|
153
210
|
res.status,
|
|
@@ -162,7 +219,53 @@ async function callLlm(req, opts = {}) {
|
|
|
162
219
|
}
|
|
163
220
|
throw err;
|
|
164
221
|
}
|
|
165
|
-
const
|
|
222
|
+
const text = await res.text();
|
|
223
|
+
let json;
|
|
224
|
+
try {
|
|
225
|
+
json = JSON.parse(text);
|
|
226
|
+
} catch (parseErr) {
|
|
227
|
+
if (sink) {
|
|
228
|
+
await recordRaw(sink, redactor, {
|
|
229
|
+
eventId: cryptoEventId(),
|
|
230
|
+
runId: traceContext?.runId,
|
|
231
|
+
spanId: traceContext?.spanId,
|
|
232
|
+
provider,
|
|
233
|
+
model: req.model,
|
|
234
|
+
endpoint,
|
|
235
|
+
baseUrl,
|
|
236
|
+
attemptIndex: attempt,
|
|
237
|
+
direction: "error",
|
|
238
|
+
timestamp: Date.now(),
|
|
239
|
+
durationMs: Date.now() - started,
|
|
240
|
+
statusCode: res.status,
|
|
241
|
+
responseHeaders,
|
|
242
|
+
responseBody: text,
|
|
243
|
+
errorMessage: `non-JSON response: ${parseErr instanceof Error ? parseErr.message : String(parseErr)}`,
|
|
244
|
+
redactedFields: []
|
|
245
|
+
});
|
|
246
|
+
attemptErrorRecorded = true;
|
|
247
|
+
}
|
|
248
|
+
throw parseErr;
|
|
249
|
+
}
|
|
250
|
+
if (sink) {
|
|
251
|
+
await recordRaw(sink, redactor, {
|
|
252
|
+
eventId: cryptoEventId(),
|
|
253
|
+
runId: traceContext?.runId,
|
|
254
|
+
spanId: traceContext?.spanId,
|
|
255
|
+
provider,
|
|
256
|
+
model: req.model,
|
|
257
|
+
endpoint,
|
|
258
|
+
baseUrl,
|
|
259
|
+
attemptIndex: attempt,
|
|
260
|
+
direction: "response",
|
|
261
|
+
timestamp: Date.now(),
|
|
262
|
+
durationMs: Date.now() - started,
|
|
263
|
+
statusCode: res.status,
|
|
264
|
+
responseHeaders,
|
|
265
|
+
responseBody: json,
|
|
266
|
+
redactedFields: []
|
|
267
|
+
});
|
|
268
|
+
}
|
|
166
269
|
const choice = json.choices?.[0];
|
|
167
270
|
const usageRaw = json.usage ?? {};
|
|
168
271
|
const costFromProxy = json._response_cost ?? json.cost_usd;
|
|
@@ -184,6 +287,23 @@ async function callLlm(req, opts = {}) {
|
|
|
184
287
|
} catch (err) {
|
|
185
288
|
clearTimeout(timeoutHandle);
|
|
186
289
|
lastErr = err;
|
|
290
|
+
if (sink && !attemptErrorRecorded) {
|
|
291
|
+
await recordRaw(sink, redactor, {
|
|
292
|
+
eventId: cryptoEventId(),
|
|
293
|
+
runId: traceContext?.runId,
|
|
294
|
+
spanId: traceContext?.spanId,
|
|
295
|
+
provider,
|
|
296
|
+
model: req.model,
|
|
297
|
+
endpoint,
|
|
298
|
+
baseUrl,
|
|
299
|
+
attemptIndex: attempt,
|
|
300
|
+
direction: "error",
|
|
301
|
+
timestamp: Date.now(),
|
|
302
|
+
durationMs: Date.now() - started,
|
|
303
|
+
errorMessage: err instanceof Error ? err.message : String(err),
|
|
304
|
+
redactedFields: []
|
|
305
|
+
});
|
|
306
|
+
}
|
|
187
307
|
if (attempt < maxRetries - 1 && isRetryableError(err)) {
|
|
188
308
|
await sleep(backoffMs(attempt));
|
|
189
309
|
continue;
|
|
@@ -193,6 +313,23 @@ async function callLlm(req, opts = {}) {
|
|
|
193
313
|
}
|
|
194
314
|
throw lastErr instanceof Error ? lastErr : new Error(String(lastErr));
|
|
195
315
|
}
|
|
316
|
+
async function recordRaw(sink, redactor, event) {
|
|
317
|
+
try {
|
|
318
|
+
await sink.record(redactor(event));
|
|
319
|
+
} catch {
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function headersToObject(h) {
|
|
323
|
+
const out = {};
|
|
324
|
+
h.forEach((value, key) => {
|
|
325
|
+
out[key] = value;
|
|
326
|
+
});
|
|
327
|
+
return out;
|
|
328
|
+
}
|
|
329
|
+
function cryptoEventId() {
|
|
330
|
+
if (typeof globalThis.crypto?.randomUUID === "function") return globalThis.crypto.randomUUID();
|
|
331
|
+
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
|
|
332
|
+
}
|
|
196
333
|
async function callLlmJson(req, opts = {}) {
|
|
197
334
|
try {
|
|
198
335
|
const result = await callLlm({ ...req, jsonMode: req.jsonMode ?? !req.jsonSchema }, opts);
|
|
@@ -220,6 +357,68 @@ ${content.slice(0, 800)}`
|
|
|
220
357
|
);
|
|
221
358
|
}
|
|
222
359
|
}
|
|
360
|
+
var LlmRouteAssertionError = class extends Error {
|
|
361
|
+
constructor(message, code, baseUrl) {
|
|
362
|
+
super(message);
|
|
363
|
+
this.code = code;
|
|
364
|
+
this.baseUrl = baseUrl;
|
|
365
|
+
this.name = "LlmRouteAssertionError";
|
|
366
|
+
}
|
|
367
|
+
code;
|
|
368
|
+
baseUrl;
|
|
369
|
+
};
|
|
370
|
+
function assertLlmRoute(opts, req = {}) {
|
|
371
|
+
const baseUrlExplicit = opts.baseUrl !== void 0;
|
|
372
|
+
const baseUrl = (opts.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
373
|
+
if (req.requireExplicitBaseUrl && !baseUrlExplicit) {
|
|
374
|
+
throw new LlmRouteAssertionError(
|
|
375
|
+
`assertLlmRoute: requireExplicitBaseUrl set but opts.baseUrl is undefined; would fall back to ${DEFAULT_BASE_URL}.`,
|
|
376
|
+
"no_explicit_base_url",
|
|
377
|
+
baseUrl
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
if (req.blockedBaseUrls?.some((p) => matchUrl(baseUrl, p))) {
|
|
381
|
+
throw new LlmRouteAssertionError(
|
|
382
|
+
`assertLlmRoute: baseUrl ${baseUrl} matches a blocked pattern.`,
|
|
383
|
+
"base_url_blocked",
|
|
384
|
+
baseUrl
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
if (req.allowedBaseUrls && req.allowedBaseUrls.length > 0) {
|
|
388
|
+
const ok = req.allowedBaseUrls.some((p) => matchUrl(baseUrl, p));
|
|
389
|
+
if (!ok) {
|
|
390
|
+
throw new LlmRouteAssertionError(
|
|
391
|
+
`assertLlmRoute: baseUrl ${baseUrl} is not in the allowed list (${req.allowedBaseUrls.map(describePattern).join(", ")}).`,
|
|
392
|
+
"base_url_not_allowed",
|
|
393
|
+
baseUrl
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
if (req.requireAuth && !opts.apiKey && !opts.bearer && !opts.authHeader) {
|
|
398
|
+
throw new LlmRouteAssertionError(
|
|
399
|
+
`assertLlmRoute: requireAuth set but no apiKey, bearer, or authHeader was supplied.`,
|
|
400
|
+
"no_auth",
|
|
401
|
+
baseUrl
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
if (req.expectedProvider) {
|
|
405
|
+
const actual = opts.provider ?? providerFromBaseUrl(baseUrl);
|
|
406
|
+
if (actual !== req.expectedProvider) {
|
|
407
|
+
throw new LlmRouteAssertionError(
|
|
408
|
+
`assertLlmRoute: expected provider ${req.expectedProvider} but baseUrl ${baseUrl} resolves to ${actual}.`,
|
|
409
|
+
"wrong_provider",
|
|
410
|
+
baseUrl
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
function matchUrl(url, pattern) {
|
|
416
|
+
if (pattern instanceof RegExp) return pattern.test(url);
|
|
417
|
+
return url.toLowerCase().startsWith(pattern.toLowerCase());
|
|
418
|
+
}
|
|
419
|
+
function describePattern(p) {
|
|
420
|
+
return p instanceof RegExp ? p.source : p;
|
|
421
|
+
}
|
|
223
422
|
async function probeLlm(model, opts = {}) {
|
|
224
423
|
const start = Date.now();
|
|
225
424
|
try {
|
|
@@ -259,7 +458,9 @@ export {
|
|
|
259
458
|
stripFencedJson,
|
|
260
459
|
callLlm,
|
|
261
460
|
callLlmJson,
|
|
461
|
+
LlmRouteAssertionError,
|
|
462
|
+
assertLlmRoute,
|
|
262
463
|
probeLlm,
|
|
263
464
|
LlmClient
|
|
264
465
|
};
|
|
265
|
-
//# sourceMappingURL=chunk-
|
|
466
|
+
//# sourceMappingURL=chunk-3GN6U53I.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/llm-client.ts"],"sourcesContent":["/**\n * LLM client with graceful degrade.\n *\n * OpenAI-compatible `/v1/chat/completions` client with:\n * - Exponential-backoff retry on 429 + 5xx gateway errors (502/503/504).\n * - Retry on transient network errors (fetch failed, AbortError, ECONNRESET).\n * - Graceful json_schema → json_object degrade on 400 with schema-reject body.\n * - Fenced-JSON stripping (```json ... ```) for models that wrap structured output.\n * - Configurable base URL + api key / bearer, works with LiteLLM proxies, OpenAI\n * directly, cli-bridge subscriptions, and any router that speaks the spec.\n *\n * Usage:\n * const { value, result } = await callLlmJson<MyType>(\n * { model: 'gpt-4o', messages: [...], jsonSchema: { name: 'x', schema: {...} } },\n * { baseUrl: 'https://router.tangle.tools/v1', apiKey: process.env.KEY },\n * )\n *\n * This is THE llm-calling seam for agent-eval primitives that need structured\n * output (semantic concept judge, reviewer directives, critic scores). Primitives\n * that need free-form text use `callLlm` and parse output themselves.\n */\n\nimport {\n defaultProviderRedactor,\n providerFromBaseUrl,\n type ProviderRedactor,\n type RawProviderEvent,\n type RawProviderSink,\n} from './trace/raw-provider-sink'\n\n// ─── Types ──────────────────────────────────────────────────────────────\n\nexport interface LlmMessage {\n role: 'system' | 'user' | 'assistant'\n /**\n * Either a plain text content string OR a multimodal content array\n * (text + image_url parts) for vision-capable models.\n */\n content:\n | string\n | Array<\n | { type: 'text'; text: string }\n | { type: 'image_url'; image_url: { url: string; detail?: 'auto' | 'low' | 'high' } }\n >\n}\n\nexport interface LlmCallRequest {\n model: string\n messages: LlmMessage[]\n /** Optional JSON-mode response format (response_format: json_object). */\n jsonMode?: boolean\n /** Optional structured output via JSON Schema. Falls back to json_object on 400. */\n jsonSchema?: { name: string; schema: Record<string, unknown> }\n temperature?: number\n maxTokens?: number\n /** Per-call timeout, default 60s. */\n timeoutMs?: number\n}\n\nexport interface LlmUsage {\n promptTokens: number\n completionTokens: number\n totalTokens: number\n /** Proxies populate this when prompt caching is on. */\n cachedPromptTokens?: number\n}\n\nexport interface LlmCallResult {\n /** The text content of the first choice. Empty string if none. */\n content: string\n usage: LlmUsage\n /**\n * Cost in USD. Pulled from proxy's `_response_cost` field when present;\n * `null` when neither the proxy nor the caller can derive it.\n */\n costUsd: number | null\n /** Model name actually used (echoed from response). */\n model: string\n /** Wall-clock duration of the HTTP call (last attempt, if retried). */\n durationMs: number\n /** Raw response body. */\n raw: Record<string, unknown>\n}\n\nexport class LlmCallError extends Error {\n constructor(\n message: string,\n public readonly status: number,\n public readonly body: string,\n public readonly model: string,\n ) {\n super(message)\n this.name = 'LlmCallError'\n }\n}\n\nexport interface LlmClientOptions {\n /** Base URL (without trailing slash). Must end at the `/v1` prefix. */\n baseUrl?: string\n /** Bearer token — either `apiKey` or `bearer` populates `Authorization: Bearer ...`. */\n apiKey?: string\n bearer?: string\n /** Override for the `Authorization` header (e.g. `X-Auth: ...`). Takes precedence over apiKey/bearer. */\n authHeader?: { name: string; value: string }\n /** Default timeout in ms. Per-call can override. */\n defaultTimeoutMs?: number\n /** Max retry attempts on retriable errors. Default 3 (1 initial + 2 retries). */\n maxRetries?: number\n /** Fetch implementation — defaults to global `fetch`. Override for custom transport (e.g. tests). */\n fetch?: typeof fetch\n /**\n * Optional raw HTTP capture sink. When provided, every request, response,\n * and error (across all retry attempts) is recorded to the sink, with auth\n * headers and credential-shaped body fields redacted by default. This is\n * the layer-1 forensics primitive: structured `LlmSpan`s record intent,\n * raw events record what actually crossed the wire.\n */\n rawSink?: RawProviderSink\n /**\n * Logical provider id attached to raw events. When omitted, derived from\n * `baseUrl` via `providerFromBaseUrl`.\n */\n provider?: string\n /** Trace context attached to raw events; populated by emitter-aware callers. */\n traceContext?: { runId?: string; spanId?: string }\n /** Override the redaction strategy for this call. Defaults to `defaultProviderRedactor`. */\n redactor?: ProviderRedactor\n}\n\n// ─── Internals ──────────────────────────────────────────────────────────\n\nconst DEFAULT_BASE_URL = 'https://router.tangle.tools/v1'\nconst DEFAULT_TIMEOUT_MS = 60_000\nconst DEFAULT_MAX_RETRIES = 3\n\nconst RETRYABLE_STATUS = new Set([429, 502, 503, 504])\n\nfunction isRetryableError(err: unknown): boolean {\n if (err instanceof LlmCallError) return RETRYABLE_STATUS.has(err.status)\n if (err instanceof Error) {\n return (\n err.name === 'AbortError' ||\n err.name === 'TimeoutError' ||\n /fetch failed|ECONNRESET|ETIMEDOUT|EAI_AGAIN/i.test(err.message)\n )\n }\n return false\n}\n\nfunction parseRetryAfter(headers: Headers): number | null {\n const h = headers.get('retry-after')\n if (!h) return null\n const asNumber = Number(h)\n if (Number.isFinite(asNumber) && asNumber > 0) return asNumber * 1000\n const asDate = Date.parse(h)\n if (Number.isFinite(asDate)) return Math.max(0, asDate - Date.now())\n return null\n}\n\nfunction backoffMs(attempt: number): number {\n // 500ms, 1s, 2s, 4s, ...\n return Math.min(500 * Math.pow(2, attempt), 16_000)\n}\n\nfunction buildHeaders(opts: LlmClientOptions): Record<string, string> {\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n }\n if (opts.authHeader) {\n headers[opts.authHeader.name] = opts.authHeader.value\n } else if (opts.bearer || opts.apiKey) {\n headers.Authorization = `Bearer ${opts.bearer ?? opts.apiKey}`\n }\n return headers\n}\n\nfunction isSchemaRejection(status: number, body: string): boolean {\n if (status !== 400) return false\n const lower = body.toLowerCase()\n return (\n lower.includes('response_format') ||\n lower.includes('json_schema') ||\n lower.includes('is unavailable') ||\n lower.includes('not supported')\n )\n}\n\nfunction buildBody(req: LlmCallRequest, forceJsonObject: boolean): Record<string, unknown> {\n const body: Record<string, unknown> = {\n model: req.model,\n messages: req.messages,\n temperature: req.temperature ?? 0,\n }\n if (req.maxTokens != null) {\n if (usesMaxCompletionTokens(req.model)) body.max_completion_tokens = req.maxTokens\n else body.max_tokens = req.maxTokens\n }\n\n if (req.jsonSchema && !forceJsonObject) {\n body.response_format = {\n type: 'json_schema',\n json_schema: { name: req.jsonSchema.name, schema: req.jsonSchema.schema, strict: true },\n }\n } else if (req.jsonMode || req.jsonSchema) {\n body.response_format = { type: 'json_object' }\n }\n\n return body\n}\n\nfunction usesMaxCompletionTokens(model: string): boolean {\n return /^gpt-5(?:[.\\-]|$)/i.test(model)\n}\n\nasync function sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms))\n}\n\n// ─── Public API ─────────────────────────────────────────────────────────\n\n/**\n * Strip a ```json / ``` code fence if the model emitted one.\n * Idempotent for naked JSON. Some models (claude-code via router, certain\n * deepseek models) wrap output even under json_object.\n */\nexport function stripFencedJson(raw: string): string {\n const trimmed = raw.trim()\n const m = trimmed.match(/^```(?:json)?\\s*\\n?([\\s\\S]*?)\\n?```\\s*$/)\n return m ? m[1]!.trim() : trimmed\n}\n\nexport function extractJsonPayload(raw: string): string {\n const stripped = stripFencedJson(raw)\n try {\n JSON.parse(stripped)\n return stripped\n } catch {\n // Continue with balanced extraction below.\n }\n\n const starts = [...stripped.matchAll(/[\\[{]/g)].map((match) => match.index).filter((index) => index != null)\n for (const start of starts) {\n const candidate = extractBalancedJson(stripped, start)\n if (!candidate) continue\n try {\n JSON.parse(candidate)\n return candidate\n } catch {\n // Keep scanning; earlier braces may belong to prose.\n }\n }\n\n return stripped\n}\n\nfunction extractBalancedJson(input: string, start: number): string | null {\n const opener = input[start]\n const closer = opener === '{' ? '}' : opener === '[' ? ']' : null\n if (!closer) return null\n\n const stack: string[] = [closer]\n let isInString = false\n let isEscaped = false\n\n for (let i = start + 1; i < input.length; i++) {\n const char = input[i]!\n if (isEscaped) {\n isEscaped = false\n continue\n }\n if (char === '\\\\') {\n isEscaped = isInString\n continue\n }\n if (char === '\"') {\n isInString = !isInString\n continue\n }\n if (isInString) continue\n\n if (char === '{') stack.push('}')\n else if (char === '[') stack.push(']')\n else if (char === stack[stack.length - 1]) {\n stack.pop()\n if (stack.length === 0) return input.slice(start, i + 1)\n }\n }\n\n return null\n}\n\n/**\n * Low-level call. Returns raw content + usage + cost. Retries on transient\n * failures; does NOT degrade schema here — callers that want graceful\n * degrade use `callLlmJson`.\n */\nexport async function callLlm(\n req: LlmCallRequest,\n opts: LlmClientOptions = {},\n): Promise<LlmCallResult> {\n const baseUrl = (opts.baseUrl ?? DEFAULT_BASE_URL).replace(/\\/+$/, '')\n const url = `${baseUrl}/chat/completions`\n const endpoint = '/chat/completions'\n const timeoutMs = req.timeoutMs ?? opts.defaultTimeoutMs ?? DEFAULT_TIMEOUT_MS\n const maxRetries = opts.maxRetries ?? DEFAULT_MAX_RETRIES\n const fetchFn = opts.fetch ?? globalThis.fetch\n const headers = buildHeaders(opts)\n const provider = opts.provider ?? providerFromBaseUrl(baseUrl)\n const sink = opts.rawSink\n const redactor = opts.redactor ?? defaultProviderRedactor\n const traceContext = opts.traceContext\n\n let lastErr: unknown\n for (let attempt = 0; attempt < maxRetries; attempt++) {\n const controller = new AbortController()\n const timeoutHandle = setTimeout(() => controller.abort(), timeoutMs)\n const started = Date.now()\n const requestBody = buildBody(req, false)\n let attemptErrorRecorded = false\n if (sink) {\n await recordRaw(sink, redactor, {\n eventId: cryptoEventId(),\n runId: traceContext?.runId,\n spanId: traceContext?.spanId,\n provider,\n model: req.model,\n endpoint,\n baseUrl,\n attemptIndex: attempt,\n direction: 'request',\n timestamp: started,\n requestHeaders: headers,\n requestBody,\n redactedFields: [],\n })\n }\n\n try {\n const res = await fetchFn(url, {\n method: 'POST',\n headers,\n body: JSON.stringify(requestBody),\n signal: controller.signal,\n })\n clearTimeout(timeoutHandle)\n const responseHeaders = sink ? headersToObject(res.headers) : undefined\n\n if (!res.ok) {\n const body = await res.text()\n if (sink) {\n await recordRaw(sink, redactor, {\n eventId: cryptoEventId(),\n runId: traceContext?.runId,\n spanId: traceContext?.spanId,\n provider,\n model: req.model,\n endpoint,\n baseUrl,\n attemptIndex: attempt,\n direction: 'error',\n timestamp: Date.now(),\n durationMs: Date.now() - started,\n statusCode: res.status,\n responseHeaders,\n responseBody: body,\n errorMessage: `HTTP ${res.status}`,\n redactedFields: [],\n })\n attemptErrorRecorded = true\n }\n const err = new LlmCallError(\n `LLM call ${res.status}: ${body.slice(0, 300)}`,\n res.status,\n body,\n req.model,\n )\n if (RETRYABLE_STATUS.has(res.status) && attempt < maxRetries - 1) {\n lastErr = err\n const retryAfter = parseRetryAfter(res.headers)\n await sleep(retryAfter ?? backoffMs(attempt))\n continue\n }\n throw err\n }\n\n const text = await res.text()\n let json: Record<string, unknown>\n try {\n json = JSON.parse(text) as Record<string, unknown>\n } catch (parseErr) {\n if (sink) {\n await recordRaw(sink, redactor, {\n eventId: cryptoEventId(),\n runId: traceContext?.runId,\n spanId: traceContext?.spanId,\n provider,\n model: req.model,\n endpoint,\n baseUrl,\n attemptIndex: attempt,\n direction: 'error',\n timestamp: Date.now(),\n durationMs: Date.now() - started,\n statusCode: res.status,\n responseHeaders,\n responseBody: text,\n errorMessage: `non-JSON response: ${parseErr instanceof Error ? parseErr.message : String(parseErr)}`,\n redactedFields: [],\n })\n attemptErrorRecorded = true\n }\n throw parseErr\n }\n if (sink) {\n await recordRaw(sink, redactor, {\n eventId: cryptoEventId(),\n runId: traceContext?.runId,\n spanId: traceContext?.spanId,\n provider,\n model: req.model,\n endpoint,\n baseUrl,\n attemptIndex: attempt,\n direction: 'response',\n timestamp: Date.now(),\n durationMs: Date.now() - started,\n statusCode: res.status,\n responseHeaders,\n responseBody: json,\n redactedFields: [],\n })\n }\n const choice = (json.choices as Array<{ message?: { content?: string } }> | undefined)?.[0]\n const usageRaw = (json.usage as Record<string, unknown> | undefined) ?? {}\n const costFromProxy = (json._response_cost ?? json.cost_usd) as number | undefined\n\n return {\n content: choice?.message?.content ?? '',\n usage: {\n promptTokens: Number(usageRaw.prompt_tokens ?? 0),\n completionTokens: Number(usageRaw.completion_tokens ?? 0),\n totalTokens: Number(usageRaw.total_tokens ?? 0),\n cachedPromptTokens:\n usageRaw.prompt_tokens_details &&\n typeof usageRaw.prompt_tokens_details === 'object'\n ? Number(\n (usageRaw.prompt_tokens_details as Record<string, unknown>).cached_tokens ?? 0,\n )\n : undefined,\n },\n costUsd: typeof costFromProxy === 'number' ? costFromProxy : null,\n model: (json.model as string) ?? req.model,\n durationMs: Date.now() - started,\n raw: json,\n }\n } catch (err) {\n clearTimeout(timeoutHandle)\n lastErr = err\n if (sink && !attemptErrorRecorded) {\n // Record only if neither the !res.ok branch nor the JSON.parse catch\n // already produced an error event for this attempt. Covers network\n // failures, timeouts, and aborts.\n await recordRaw(sink, redactor, {\n eventId: cryptoEventId(),\n runId: traceContext?.runId,\n spanId: traceContext?.spanId,\n provider,\n model: req.model,\n endpoint,\n baseUrl,\n attemptIndex: attempt,\n direction: 'error',\n timestamp: Date.now(),\n durationMs: Date.now() - started,\n errorMessage: err instanceof Error ? err.message : String(err),\n redactedFields: [],\n })\n }\n if (attempt < maxRetries - 1 && isRetryableError(err)) {\n await sleep(backoffMs(attempt))\n continue\n }\n throw err\n }\n }\n throw lastErr instanceof Error ? lastErr : new Error(String(lastErr))\n}\n\nasync function recordRaw(\n sink: RawProviderSink,\n redactor: ProviderRedactor,\n event: RawProviderEvent,\n): Promise<void> {\n // Errors from sinks must not crash the LLM call. Forensic capture is\n // best-effort; the structured trace is the system of record.\n try {\n await sink.record(redactor(event))\n } catch {\n // Intentionally swallowed.\n }\n}\n\nfunction headersToObject(h: Headers): Record<string, string> {\n const out: Record<string, string> = {}\n h.forEach((value, key) => {\n out[key] = value\n })\n return out\n}\n\nfunction cryptoEventId(): string {\n if (typeof globalThis.crypto?.randomUUID === 'function') return globalThis.crypto.randomUUID()\n return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`\n}\n\n/**\n * Structured-output call. Returns parsed JSON plus the raw result envelope.\n * Degrades `jsonSchema` → `jsonMode` on a 400 that names the schema param —\n * critical for deepseek-v3/v4, kimi-k2.6, and other models that don't accept\n * the `response_format.json_schema` shape but DO accept `json_object`.\n */\nexport async function callLlmJson<T = unknown>(\n req: LlmCallRequest,\n opts: LlmClientOptions = {},\n): Promise<{ value: T; result: LlmCallResult }> {\n try {\n const result = await callLlm({ ...req, jsonMode: req.jsonMode ?? !req.jsonSchema }, opts)\n const value = parseJsonSafely<T>(result.content, result.model)\n return { value, result }\n } catch (err) {\n if (err instanceof LlmCallError && isSchemaRejection(err.status, err.body) && req.jsonSchema) {\n // Degrade to json_object + retry.\n const degradedReq: LlmCallRequest = { ...req, jsonMode: true, jsonSchema: undefined }\n const result = await callLlm(degradedReq, opts)\n const value = parseJsonSafely<T>(result.content, result.model)\n return { value, result }\n }\n throw err\n }\n}\n\nfunction parseJsonSafely<T>(content: string, model: string): T {\n const stripped = extractJsonPayload(content)\n try {\n return JSON.parse(stripped) as T\n } catch (err) {\n throw new Error(\n `LLM returned non-JSON content (model=${model}): ${\n err instanceof Error ? err.message : String(err)\n }\\n--- raw content ---\\n${content.slice(0, 800)}`,\n )\n }\n}\n\n// ─── Route assertion ────────────────────────────────────────────────────\n\nexport class LlmRouteAssertionError extends Error {\n constructor(\n message: string,\n public readonly code:\n | 'no_explicit_base_url'\n | 'base_url_blocked'\n | 'base_url_not_allowed'\n | 'no_auth'\n | 'wrong_provider',\n public readonly baseUrl: string,\n ) {\n super(message)\n this.name = 'LlmRouteAssertionError'\n }\n}\n\nexport interface LlmRouteRequirements {\n /**\n * Throw if `opts.baseUrl` is undefined, i.e. the call would fall back to\n * `DEFAULT_BASE_URL`. Set this for evaluation runs where silently using\n * the public/free-tier router is a defect — the launch reviewer needs to\n * know exactly which provider answered.\n */\n requireExplicitBaseUrl?: boolean\n /**\n * Allowlist of acceptable base URLs. Strings match by prefix\n * (case-insensitive); RegExps test against the full base URL.\n */\n allowedBaseUrls?: Array<string | RegExp>\n /** Blocklist that takes precedence over `allowedBaseUrls`. */\n blockedBaseUrls?: Array<string | RegExp>\n /** Throw if no auth header / api key is configured. */\n requireAuth?: boolean\n /**\n * Logical provider id the configured `baseUrl` is expected to match (via\n * `providerFromBaseUrl`). Mainly useful when paired with `requireExplicitBaseUrl`.\n */\n expectedProvider?: string\n}\n\n/**\n * Fail-loud assertion that the configured LLM client points at the route\n * the caller intends. Designed for the matrix-runner preflight: invoke\n * once before any LLM call to catch misconfiguration before a sweep burns\n * dollars on the wrong provider.\n *\n * Throws `LlmRouteAssertionError`. Pure — no I/O — so it's safe to call\n * from constructors and CI gates.\n */\nexport function assertLlmRoute(opts: LlmClientOptions, req: LlmRouteRequirements = {}): void {\n const baseUrlExplicit = opts.baseUrl !== undefined\n const baseUrl = (opts.baseUrl ?? DEFAULT_BASE_URL).replace(/\\/+$/, '')\n\n if (req.requireExplicitBaseUrl && !baseUrlExplicit) {\n throw new LlmRouteAssertionError(\n `assertLlmRoute: requireExplicitBaseUrl set but opts.baseUrl is undefined; would fall back to ${DEFAULT_BASE_URL}.`,\n 'no_explicit_base_url',\n baseUrl,\n )\n }\n\n if (req.blockedBaseUrls?.some((p) => matchUrl(baseUrl, p))) {\n throw new LlmRouteAssertionError(\n `assertLlmRoute: baseUrl ${baseUrl} matches a blocked pattern.`,\n 'base_url_blocked',\n baseUrl,\n )\n }\n\n if (req.allowedBaseUrls && req.allowedBaseUrls.length > 0) {\n const ok = req.allowedBaseUrls.some((p) => matchUrl(baseUrl, p))\n if (!ok) {\n throw new LlmRouteAssertionError(\n `assertLlmRoute: baseUrl ${baseUrl} is not in the allowed list (${req.allowedBaseUrls.map(describePattern).join(', ')}).`,\n 'base_url_not_allowed',\n baseUrl,\n )\n }\n }\n\n if (req.requireAuth && !opts.apiKey && !opts.bearer && !opts.authHeader) {\n throw new LlmRouteAssertionError(\n `assertLlmRoute: requireAuth set but no apiKey, bearer, or authHeader was supplied.`,\n 'no_auth',\n baseUrl,\n )\n }\n\n if (req.expectedProvider) {\n const actual = opts.provider ?? providerFromBaseUrl(baseUrl)\n if (actual !== req.expectedProvider) {\n throw new LlmRouteAssertionError(\n `assertLlmRoute: expected provider ${req.expectedProvider} but baseUrl ${baseUrl} resolves to ${actual}.`,\n 'wrong_provider',\n baseUrl,\n )\n }\n }\n}\n\nfunction matchUrl(url: string, pattern: string | RegExp): boolean {\n if (pattern instanceof RegExp) return pattern.test(url)\n return url.toLowerCase().startsWith(pattern.toLowerCase())\n}\n\nfunction describePattern(p: string | RegExp): string {\n return p instanceof RegExp ? p.source : p\n}\n\n/**\n * Probe whether a model is reachable. Returns latency + null error on\n * success; `ok=false` + error message on any failure (HTTP, timeout,\n * network, parse). Designed for sweep preflights — fail loud at the\n * boundary before burning a 30-leaf run on a misconfigured router.\n *\n * Sends a tiny `ping` message with `maxTokens=64`. Reasoning models\n * (glm-5.1, deepseek-v4) can burn the entire budget on internal reasoning\n * for short prompts, so don't tighten this further. We don't validate\n * content; HTTP 200 means reachable.\n */\nexport async function probeLlm(\n model: string,\n opts: LlmClientOptions & { timeoutMs?: number } = {},\n): Promise<{ ok: boolean; latencyMs: number; error: string | null }> {\n const start = Date.now()\n try {\n await callLlm(\n {\n model,\n messages: [{ role: 'user', content: 'ping' }],\n maxTokens: 64,\n timeoutMs: opts.timeoutMs ?? 30_000,\n },\n opts,\n )\n return { ok: true, latencyMs: Date.now() - start, error: null }\n } catch (err) {\n return {\n ok: false,\n latencyMs: Date.now() - start,\n error: err instanceof Error ? err.message : String(err),\n }\n }\n}\n\n/**\n * Stateful client — construct once with defaults, call many times.\n * Thin wrapper around the free functions; exists for callers that want\n * to inject a single configured instance into multiple primitives.\n */\nexport class LlmClient {\n constructor(private readonly opts: LlmClientOptions = {}) {}\n\n call(req: LlmCallRequest, per?: LlmClientOptions): Promise<LlmCallResult> {\n return callLlm(req, { ...this.opts, ...per })\n }\n\n callJson<T = unknown>(\n req: LlmCallRequest,\n per?: LlmClientOptions,\n ): Promise<{ value: T; result: LlmCallResult }> {\n return callLlmJson<T>(req, { ...this.opts, ...per })\n }\n}\n"],"mappings":";;;;;;AAoFO,IAAM,eAAN,cAA2B,MAAM;AAAA,EACtC,YACE,SACgB,QACA,MACA,OAChB;AACA,UAAM,OAAO;AAJG;AACA;AACA;AAGhB,SAAK,OAAO;AAAA,EACd;AAAA,EANkB;AAAA,EACA;AAAA,EACA;AAKpB;AAqCA,IAAM,mBAAmB;AACzB,IAAM,qBAAqB;AAC3B,IAAM,sBAAsB;AAE5B,IAAM,mBAA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