@orkestrel/agent 0.0.25 → 0.0.27
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/dist/src/core/index.cjs +735 -21
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +640 -0
- package/dist/src/core/index.d.ts +640 -0
- package/dist/src/core/index.js +719 -24
- package/dist/src/core/index.js.map +1 -1
- package/package.json +16 -16
package/dist/src/core/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { arrayOf, arrayShape, attempt, booleanShape, cloneJSONValue, createContract, isArray, isBoolean, isFiniteNumber, isInstance, isObject, isRecord, isString, jsonShape, literalShape, numberShape, objectShape, optionalShape, parseJSONAs, parseJSONValue, rawShape, recordShape, stringShape, unionShape } from "@orkestrel/contract";
|
|
1
|
+
import { arrayOf, arrayShape, attempt, booleanShape, boundsOf, cloneJSONValue, createContract, isArray, isBoolean, isFiniteNumber, isInstance, isJSONValue, isNumber, isObject, isRecord, isString, jsonShape, literalShape, nullableOf, numberShape, objectShape, optionalOf, optionalShape, parseJSON, parseJSONAs, parseJSONValue, rawShape, recordShape, stringShape, unionShape } from "@orkestrel/contract";
|
|
2
2
|
import { createDatabase, createMemoryDriver } from "@orkestrel/database";
|
|
3
3
|
import { createQueue } from "@orkestrel/queue";
|
|
4
4
|
import { createRunner, errorToMessage } from "@orkestrel/workflow";
|
|
@@ -7,7 +7,7 @@ import { Timeout, createTimeout } from "@orkestrel/timeout";
|
|
|
7
7
|
import { Emitter } from "@orkestrel/emitter";
|
|
8
8
|
import { ToolManager, isToolCall } from "@orkestrel/tool";
|
|
9
9
|
import { WorkspaceManager, isBinary, isText } from "@orkestrel/workspace";
|
|
10
|
-
import { createTokenBudget } from "@orkestrel/budget";
|
|
10
|
+
import { createTokenBudget, isTokenUsage } from "@orkestrel/budget";
|
|
11
11
|
//#region src/core/constants.ts
|
|
12
12
|
/**
|
|
13
13
|
* Caps an {@link AgentInterface} turn's tool iterations by default — `10` context → provider →
|
|
@@ -145,6 +145,8 @@ var INVALID_RELAY_STATUS = 400;
|
|
|
145
145
|
* carried with no body and answered for an aborted inbound read as well.
|
|
146
146
|
*/
|
|
147
147
|
var OVERSIZED_RELAY_STATUS = 413;
|
|
148
|
+
/** Names the System One decision endpoint shared by compatible servers. */
|
|
149
|
+
var SYSTEM_ONE_PATH = "/v1/systemone";
|
|
148
150
|
//#endregion
|
|
149
151
|
//#region src/core/errors.ts
|
|
150
152
|
/**
|
|
@@ -360,6 +362,75 @@ var ProviderError = class extends Error {
|
|
|
360
362
|
function isProviderError(value) {
|
|
361
363
|
return isInstance(value, ProviderError);
|
|
362
364
|
}
|
|
365
|
+
/**
|
|
366
|
+
* Reports a judge call cancelled by the caller's signal or its deadline, carrying the
|
|
367
|
+
* {@link JudgeResult} merged from the calls that completed before the cancel and the
|
|
368
|
+
* machine-readable `code` `'ABORT'`.
|
|
369
|
+
*
|
|
370
|
+
* @remarks
|
|
371
|
+
* `partial` keeps the answers and the usage of every completed call, so usage reported by
|
|
372
|
+
* completed calls is retained; a cancel before the first call carries an empty partial. `cause` holds the failure the
|
|
373
|
+
* cancel superseded when a throw raced the abort, and is undefined when the cancel was the only
|
|
374
|
+
* failure.
|
|
375
|
+
*/
|
|
376
|
+
var JudgeAbortError = class extends Error {
|
|
377
|
+
/** Names the machine-readable condition — `'ABORT'`: a judge call cancelled mid-flight. */
|
|
378
|
+
code = "ABORT";
|
|
379
|
+
partial;
|
|
380
|
+
constructor(partial, options) {
|
|
381
|
+
super("judge call aborted", options);
|
|
382
|
+
this.name = "JudgeAbortError";
|
|
383
|
+
this.partial = partial;
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
/**
|
|
387
|
+
* Narrows a caught value to a {@link JudgeAbortError} through `instanceof`, so a `catch` can
|
|
388
|
+
* recover its `partial` result.
|
|
389
|
+
*
|
|
390
|
+
* @param value - The caught value
|
|
391
|
+
* @returns True if the value is a {@link JudgeAbortError}; false otherwise
|
|
392
|
+
* @example
|
|
393
|
+
* ```ts
|
|
394
|
+
* isJudgeAbortError(new JudgeAbortError({ model: 'tev1:0.8b', answers: {} })) // true
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
function isJudgeAbortError(value) {
|
|
398
|
+
return isInstance(value, JudgeAbortError);
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Reports a coded judge failure with its HTTP status and underlying cause when available.
|
|
402
|
+
*
|
|
403
|
+
* @remarks
|
|
404
|
+
* The judge engine throws this error for a non-OK HTTP response, a missing or unparsable
|
|
405
|
+
* response body, and a request refused before inference. Concrete wire decoders also use it for
|
|
406
|
+
* a response they cannot read and for a wire limit. `status` is present only for an `HTTP`
|
|
407
|
+
* failure; other codes leave it undefined.
|
|
408
|
+
*/
|
|
409
|
+
var JudgeError = class extends Error {
|
|
410
|
+
/** Names the machine-readable condition — `'HTTP'`: a non-OK response; `'PROTOCOL'`: a missing, unparsable, or unreadable response body; `'QUESTION'`: a request refused before inference. */
|
|
411
|
+
code;
|
|
412
|
+
/** Holds the response status for an HTTP failure, or undefined for other codes. */
|
|
413
|
+
status;
|
|
414
|
+
constructor(code, message, options) {
|
|
415
|
+
super(message, options);
|
|
416
|
+
this.name = "JudgeError";
|
|
417
|
+
this.code = code;
|
|
418
|
+
this.status = options?.status;
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
/**
|
|
422
|
+
* Narrows a caught value to the judge failure class through `instanceof`.
|
|
423
|
+
*
|
|
424
|
+
* @param value - The caught value
|
|
425
|
+
* @returns True if the value is a {@link JudgeError}; false otherwise
|
|
426
|
+
* @example
|
|
427
|
+
* ```ts
|
|
428
|
+
* isJudgeError(new JudgeError('HTTP', 'judge error: 429', { status: 429 })) // true
|
|
429
|
+
* ```
|
|
430
|
+
*/
|
|
431
|
+
function isJudgeError(value) {
|
|
432
|
+
return isInstance(value, JudgeError);
|
|
433
|
+
}
|
|
363
434
|
//#endregion
|
|
364
435
|
//#region src/core/shapers.ts
|
|
365
436
|
/**
|
|
@@ -1259,6 +1330,273 @@ async function* readChunks(body, signal) {
|
|
|
1259
1330
|
}
|
|
1260
1331
|
}
|
|
1261
1332
|
}
|
|
1333
|
+
/**
|
|
1334
|
+
* Derives the winner, its probability, the published confidence, and a score answer's expected
|
|
1335
|
+
* level from a judge answer's distribution.
|
|
1336
|
+
*
|
|
1337
|
+
* @remarks
|
|
1338
|
+
* The winner is the first strictly greatest candidate in enumeration order: an option name in
|
|
1339
|
+
* criteria order, a level index, or `'false'` then `'true'` for a noul, so a noul of exactly 0.5
|
|
1340
|
+
* names `'false'`. Confidence follows the TypeSafe formulas, whatever confidence a server sent: a
|
|
1341
|
+
* choice reads `(max(p) - 1/n) / (1 - 1/n)`, a noul reads `|2p - 1|` (the choice formula at
|
|
1342
|
+
* n = 2), and a score reads `max(0, 1 - spread / even_spread)`, where `spread` sums each level's
|
|
1343
|
+
* probability times its distance from the winning level and `even_spread` is the mean distance
|
|
1344
|
+
* from the middle level. `score` is the expected level `sum(i * p_i)` and is set only for a score
|
|
1345
|
+
* answer.
|
|
1346
|
+
*
|
|
1347
|
+
* @param answer - The answer whose distribution is read
|
|
1348
|
+
* @returns The derived measures
|
|
1349
|
+
* @throws JudgeError Thrown with code `PROTOCOL` when a choice or score answer has fewer than 2
|
|
1350
|
+
* candidates, because the confidence formulas divide by the candidate count
|
|
1351
|
+
* @example
|
|
1352
|
+
* ```ts
|
|
1353
|
+
* computeReading({ form: 'choice', probabilities: { billing: 0.88, technical: 0.12, sales: 0 } })
|
|
1354
|
+
* // { winner: 'billing', probability: 0.88, confidence: 0.82 } to two decimals
|
|
1355
|
+
* computeReading({ form: 'score', probabilities: [0, 0.57, 0.43] })
|
|
1356
|
+
* // { winner: '1', probability: 0.57, confidence: 0.355, score: 1.43 } to three decimals
|
|
1357
|
+
* computeReading({ form: 'noul', noul: 0.5 }) // { winner: 'false', probability: 0.5, confidence: 0 }
|
|
1358
|
+
* ```
|
|
1359
|
+
*/
|
|
1360
|
+
function computeReading(answer) {
|
|
1361
|
+
if (answer.form === "noul") {
|
|
1362
|
+
const yes = answer.noul > .5;
|
|
1363
|
+
return {
|
|
1364
|
+
winner: yes ? "true" : "false",
|
|
1365
|
+
probability: yes ? answer.noul : 1 - answer.noul,
|
|
1366
|
+
confidence: Math.abs(2 * answer.noul - 1)
|
|
1367
|
+
};
|
|
1368
|
+
}
|
|
1369
|
+
const candidates = answer.form === "choice" ? Object.entries(answer.probabilities) : answer.probabilities.map((probability, level) => [String(level), probability]);
|
|
1370
|
+
const count = candidates.length;
|
|
1371
|
+
if (count < 2) throw new JudgeError("PROTOCOL", "judge error: an answer needs at least 2 candidates");
|
|
1372
|
+
let winner = "";
|
|
1373
|
+
let index = 0;
|
|
1374
|
+
let probability = -Infinity;
|
|
1375
|
+
for (const [position, [name, candidate]] of candidates.entries()) if (candidate > probability) {
|
|
1376
|
+
winner = name;
|
|
1377
|
+
index = position;
|
|
1378
|
+
probability = candidate;
|
|
1379
|
+
}
|
|
1380
|
+
if (answer.form === "choice") return {
|
|
1381
|
+
winner,
|
|
1382
|
+
probability,
|
|
1383
|
+
confidence: (probability - 1 / count) / (1 - 1 / count)
|
|
1384
|
+
};
|
|
1385
|
+
let spread = 0;
|
|
1386
|
+
let even = 0;
|
|
1387
|
+
let score = 0;
|
|
1388
|
+
for (const [level, candidate] of answer.probabilities.entries()) {
|
|
1389
|
+
spread += candidate * Math.abs(level - index);
|
|
1390
|
+
even += Math.abs(level - (count - 1) / 2);
|
|
1391
|
+
score += level * candidate;
|
|
1392
|
+
}
|
|
1393
|
+
return {
|
|
1394
|
+
winner,
|
|
1395
|
+
probability,
|
|
1396
|
+
confidence: Math.max(0, 1 - spread / (even / count)),
|
|
1397
|
+
score
|
|
1398
|
+
};
|
|
1399
|
+
}
|
|
1400
|
+
/**
|
|
1401
|
+
* Merges the results of a judge request's calls into one result.
|
|
1402
|
+
*
|
|
1403
|
+
* @remarks
|
|
1404
|
+
* Pure and total. Answers and refusals are joined by question id; `refusals` is omitted when no
|
|
1405
|
+
* call refused a question. Each call's usage passes through {@link sanitizeUsage} and the totals
|
|
1406
|
+
* add through {@link sumUsage}; `usage` is omitted when no call reported one. The model is the
|
|
1407
|
+
* first call's, or the given model when the list is empty, as the empty partial of a cancel
|
|
1408
|
+
* before the first call requires.
|
|
1409
|
+
*
|
|
1410
|
+
* @param model - The judge's configured model, reported when no call completed
|
|
1411
|
+
* @param results - The completed calls' results in call order
|
|
1412
|
+
* @returns The merged result
|
|
1413
|
+
* @example
|
|
1414
|
+
* ```ts
|
|
1415
|
+
* buildJudgeResult('tev1:0.8b', []) // { model: 'tev1:0.8b', answers: {} }
|
|
1416
|
+
* buildJudgeResult('jev-latest', [
|
|
1417
|
+
* { model: 'jev-1.13.0', answers: { urgent: { form: 'noul', noul: 0.95 } } },
|
|
1418
|
+
* { model: 'jev-1.13.0', answers: {}, refusals: { team: { missing: ['sales'] } } },
|
|
1419
|
+
* ])
|
|
1420
|
+
* // { model: 'jev-1.13.0', answers: { urgent: ... }, refusals: { team: { missing: ['sales'] } } }
|
|
1421
|
+
* ```
|
|
1422
|
+
*/
|
|
1423
|
+
function buildJudgeResult(model, results) {
|
|
1424
|
+
let answers = {};
|
|
1425
|
+
let refusals = {};
|
|
1426
|
+
let usage;
|
|
1427
|
+
for (const result of results) {
|
|
1428
|
+
answers = {
|
|
1429
|
+
...answers,
|
|
1430
|
+
...result.answers
|
|
1431
|
+
};
|
|
1432
|
+
refusals = {
|
|
1433
|
+
...refusals,
|
|
1434
|
+
...result.refusals
|
|
1435
|
+
};
|
|
1436
|
+
if (result.usage !== void 0) usage = sumUsage(usage, sanitizeUsage(result.usage));
|
|
1437
|
+
}
|
|
1438
|
+
return {
|
|
1439
|
+
model: results[0]?.model ?? model,
|
|
1440
|
+
answers,
|
|
1441
|
+
...Object.keys(refusals).length === 0 ? {} : { refusals },
|
|
1442
|
+
...usage === void 0 ? {} : { usage }
|
|
1443
|
+
};
|
|
1444
|
+
}
|
|
1445
|
+
/**
|
|
1446
|
+
* Builds a request's JSON headers, awaiting the caller's header hook inside the call's
|
|
1447
|
+
* cancellation bound.
|
|
1448
|
+
*
|
|
1449
|
+
* @remarks
|
|
1450
|
+
* The headers start with `Content-Type: application/json`; each entry the hook returns is set
|
|
1451
|
+
* over them, so the hook overrides the content type only when it returns that header. The hook
|
|
1452
|
+
* receives `signal` and races its abort, and the abort listener is removed on every exit. Both
|
|
1453
|
+
* HTTP engines, the provider and the judge, read their headers here.
|
|
1454
|
+
*
|
|
1455
|
+
* @param hook - The caller's header hook, or undefined for the JSON content type alone
|
|
1456
|
+
* @param signal - The call's combined caller and deadline signal
|
|
1457
|
+
* @returns The request headers
|
|
1458
|
+
* @throws Thrown when the signal aborts before the hook settles, with the signal's reason, and
|
|
1459
|
+
* when the hook throws or rejects, with the hook's own failure
|
|
1460
|
+
* @example
|
|
1461
|
+
* ```ts
|
|
1462
|
+
* const signal = new AbortController().signal
|
|
1463
|
+
* const headers = await readHeaders(() => ({ authorization: 'Bearer KEY' }), signal)
|
|
1464
|
+
* headers.get('authorization') // 'Bearer KEY'
|
|
1465
|
+
* headers.get('content-type') // 'application/json'
|
|
1466
|
+
* ```
|
|
1467
|
+
*/
|
|
1468
|
+
async function readHeaders(hook, signal) {
|
|
1469
|
+
const headers = new Headers({ "Content-Type": "application/json" });
|
|
1470
|
+
if (hook === void 0) return headers;
|
|
1471
|
+
const cleanup = new AbortController();
|
|
1472
|
+
const aborted = Promise.withResolvers();
|
|
1473
|
+
signal.addEventListener("abort", () => aborted.reject(signal.reason), {
|
|
1474
|
+
once: true,
|
|
1475
|
+
signal: cleanup.signal
|
|
1476
|
+
});
|
|
1477
|
+
try {
|
|
1478
|
+
signal.throwIfAborted();
|
|
1479
|
+
const entries = await Promise.race([Promise.resolve().then(() => hook(signal)), aborted.promise]);
|
|
1480
|
+
for (const [key, value] of Object.entries(entries)) headers.set(key, value);
|
|
1481
|
+
return headers;
|
|
1482
|
+
} finally {
|
|
1483
|
+
cleanup.abort();
|
|
1484
|
+
}
|
|
1485
|
+
}
|
|
1486
|
+
/**
|
|
1487
|
+
* Owns a value by serializing it to JSON and parsing the text, so the copy shares nothing with its source.
|
|
1488
|
+
*
|
|
1489
|
+
* @remarks
|
|
1490
|
+
* A value that JSON cannot carry, such as a cycle or a bigint, and a value that serializes to
|
|
1491
|
+
* nothing, such as a function, both return undefined, so a guard over the result refuses them.
|
|
1492
|
+
* A proxied value serializes through its traps, where a structured clone refuses it.
|
|
1493
|
+
*
|
|
1494
|
+
* @param value - The value to own
|
|
1495
|
+
* @returns The owned JSON copy, or undefined when the value is not JSON
|
|
1496
|
+
* @example
|
|
1497
|
+
* ```ts
|
|
1498
|
+
* copyJSON({ state: 'A ticket.' }) // { state: 'A ticket.' }
|
|
1499
|
+
* copyJSON(() => 1) // undefined
|
|
1500
|
+
* ```
|
|
1501
|
+
*/
|
|
1502
|
+
function copyJSON(value) {
|
|
1503
|
+
const text = attempt(() => JSON.stringify(value));
|
|
1504
|
+
if (!text.success || typeof text.value !== "string") return void 0;
|
|
1505
|
+
return parseJSON(text.value);
|
|
1506
|
+
}
|
|
1507
|
+
/**
|
|
1508
|
+
* Projects a judge question onto the System One wire while preserving omitted members.
|
|
1509
|
+
*
|
|
1510
|
+
* @param question - The domain question to send
|
|
1511
|
+
* @returns The wire question with `type` replacing `form`
|
|
1512
|
+
* @example
|
|
1513
|
+
* ```ts
|
|
1514
|
+
* questionToSystemOne({ form: 'noul' }) // { type: 'noul' }
|
|
1515
|
+
* ```
|
|
1516
|
+
*/
|
|
1517
|
+
function questionToSystemOne(question) {
|
|
1518
|
+
return {
|
|
1519
|
+
type: question.form,
|
|
1520
|
+
...question.instructions === void 0 ? {} : { instructions: question.instructions },
|
|
1521
|
+
...question.criteria === void 0 ? {} : { criteria: question.criteria }
|
|
1522
|
+
};
|
|
1523
|
+
}
|
|
1524
|
+
/**
|
|
1525
|
+
* Extracts a System One distribution in question criteria order and drops server measures.
|
|
1526
|
+
*
|
|
1527
|
+
* @remarks
|
|
1528
|
+
* Every requested candidate must have a finite probability in [0, 1]; the helper checks each
|
|
1529
|
+
* requested candidate itself and reads no other member. Score maps and arrays project onto the
|
|
1530
|
+
* requested levels. Additional candidates are ignored, sums are unconstrained, and probabilities
|
|
1531
|
+
* are preserved without normalization.
|
|
1532
|
+
*
|
|
1533
|
+
* @param answer - The wire answer to decode
|
|
1534
|
+
* @param question - The question defining the form and candidate order
|
|
1535
|
+
* @returns The domain answer, or undefined for a mismatched form or a requested candidate whose
|
|
1536
|
+
* probability is missing, not finite, or outside [0, 1]
|
|
1537
|
+
* @example
|
|
1538
|
+
* ```ts
|
|
1539
|
+
* extractSystemOneAnswer({ type: 'noul', noul: 0.9 }, { form: 'noul' })
|
|
1540
|
+
* // { form: 'noul', noul: 0.9 }
|
|
1541
|
+
* ```
|
|
1542
|
+
*/
|
|
1543
|
+
function extractSystemOneAnswer(answer, question) {
|
|
1544
|
+
const bounded = boundsOf(0, 1);
|
|
1545
|
+
if (answer.type === "noul" && question.form === "noul") return bounded(answer.noul) ? {
|
|
1546
|
+
form: "noul",
|
|
1547
|
+
noul: answer.noul
|
|
1548
|
+
} : void 0;
|
|
1549
|
+
if (answer.type === "choice" && question.form === "choice") {
|
|
1550
|
+
const entries = [];
|
|
1551
|
+
for (const label of Object.keys(question.criteria)) {
|
|
1552
|
+
if (!Object.hasOwn(answer.probabilities, label)) return void 0;
|
|
1553
|
+
const probability = answer.probabilities[label];
|
|
1554
|
+
if (!bounded(probability)) return void 0;
|
|
1555
|
+
entries.push([label, probability]);
|
|
1556
|
+
}
|
|
1557
|
+
return {
|
|
1558
|
+
form: "choice",
|
|
1559
|
+
probabilities: Object.fromEntries(entries)
|
|
1560
|
+
};
|
|
1561
|
+
}
|
|
1562
|
+
if (answer.type !== "score" || question.form !== "score") return void 0;
|
|
1563
|
+
const probabilities = [];
|
|
1564
|
+
for (let level = 0; level < question.criteria.length; level++) {
|
|
1565
|
+
if (!Object.hasOwn(answer.probabilities, level)) return void 0;
|
|
1566
|
+
const probability = isArray(answer.probabilities) ? answer.probabilities[level] : answer.probabilities[String(level)];
|
|
1567
|
+
if (!bounded(probability)) return void 0;
|
|
1568
|
+
probabilities.push(probability);
|
|
1569
|
+
}
|
|
1570
|
+
return {
|
|
1571
|
+
form: "score",
|
|
1572
|
+
probabilities
|
|
1573
|
+
};
|
|
1574
|
+
}
|
|
1575
|
+
/**
|
|
1576
|
+
* Maps complete System One token counts onto validated token usage.
|
|
1577
|
+
*
|
|
1578
|
+
* @param usage - The optional wire token counts
|
|
1579
|
+
* @returns Token usage, or undefined for missing, null, negative, or non-finite counts
|
|
1580
|
+
* @example
|
|
1581
|
+
* ```ts
|
|
1582
|
+
* extractSystemOneUsage({ input_tokens: 975, output_tokens: 4 })
|
|
1583
|
+
* // { prompt: 975, completion: 4, total: 979 }
|
|
1584
|
+
* ```
|
|
1585
|
+
*/
|
|
1586
|
+
function extractSystemOneUsage(usage) {
|
|
1587
|
+
const extracted = attempt(() => {
|
|
1588
|
+
if (usage === void 0) return void 0;
|
|
1589
|
+
const { input_tokens: prompt, output_tokens: completion } = usage;
|
|
1590
|
+
if (!isNumber(prompt) || !isNumber(completion)) return void 0;
|
|
1591
|
+
const result = {
|
|
1592
|
+
prompt,
|
|
1593
|
+
completion,
|
|
1594
|
+
total: prompt + completion
|
|
1595
|
+
};
|
|
1596
|
+
return isTokenUsage(result) ? result : void 0;
|
|
1597
|
+
});
|
|
1598
|
+
return extracted.success ? extracted.value : void 0;
|
|
1599
|
+
}
|
|
1262
1600
|
//#endregion
|
|
1263
1601
|
//#region src/core/conversations/Conversation.ts
|
|
1264
1602
|
/**
|
|
@@ -2189,7 +2527,7 @@ var Agent = class {
|
|
|
2189
2527
|
};
|
|
2190
2528
|
const toolMessage = this.#context.messages.add({
|
|
2191
2529
|
role: "tool",
|
|
2192
|
-
content: outcomeResult.success ? JSON.stringify(outcomeResult.value) : outcomeResult.error
|
|
2530
|
+
content: outcomeResult.success ? typeof outcomeResult.value === "string" ? outcomeResult.value : JSON.stringify(outcomeResult.value) : outcomeResult.error
|
|
2193
2531
|
});
|
|
2194
2532
|
messages.push(toolMessage);
|
|
2195
2533
|
}
|
|
@@ -2549,6 +2887,117 @@ function isConversationSnapshot(value) {
|
|
|
2549
2887
|
if (!isArray(value.sections) || !value.sections.every(isSection)) return false;
|
|
2550
2888
|
return isArray(value.messages) && value.messages.every(isMessage);
|
|
2551
2889
|
}
|
|
2890
|
+
/**
|
|
2891
|
+
* Checks whether a value is a judge entry: a string, a JSON record, or a JSON array.
|
|
2892
|
+
*
|
|
2893
|
+
* @remarks
|
|
2894
|
+
* Total: a cycle, a non-JSON member, a class instance, and a hostile input return false. `null`
|
|
2895
|
+
* is not an entry; a criteria guard admits it where the protocol keeps it.
|
|
2896
|
+
*
|
|
2897
|
+
* @param value - The unknown entry candidate
|
|
2898
|
+
* @returns True if the value is a string or JSON structure the model can read; false otherwise
|
|
2899
|
+
* @example
|
|
2900
|
+
* ```ts
|
|
2901
|
+
* isJudgeEntry('Customer asks for a refund') // true
|
|
2902
|
+
* isJudgeEntry({ ticket: 4182, tags: ['billing'] }) // true
|
|
2903
|
+
* isJudgeEntry(null) // false
|
|
2904
|
+
* isJudgeEntry({ opened: new Date() }) // false
|
|
2905
|
+
* ```
|
|
2906
|
+
*/
|
|
2907
|
+
function isJudgeEntry(value) {
|
|
2908
|
+
if (isString(value)) return true;
|
|
2909
|
+
return (isRecord(value) || isArray(value)) && isJSONValue(value);
|
|
2910
|
+
}
|
|
2911
|
+
/**
|
|
2912
|
+
* Checks whether a value is a well-formed judge question of the choice, score, or noul form.
|
|
2913
|
+
*
|
|
2914
|
+
* @remarks
|
|
2915
|
+
* Total: a hostile input returns false. A choice needs at least 2 options and a score at least 2
|
|
2916
|
+
* levels, because the confidence formula divides by the candidate count; a description or a level
|
|
2917
|
+
* can be `null`. `instructions` and noul `criteria` are omitted when absent and never `null`.
|
|
2918
|
+
* Server limits on option, level, and question counts are left to the server.
|
|
2919
|
+
*
|
|
2920
|
+
* @param value - The unknown question candidate
|
|
2921
|
+
* @returns True if the value is a question the judge engine can send; false otherwise
|
|
2922
|
+
* @example
|
|
2923
|
+
* ```ts
|
|
2924
|
+
* isJudgeQuestion({ form: 'choice', criteria: { billing: null, bug: 'Software defect' } }) // true
|
|
2925
|
+
* isJudgeQuestion({ form: 'score', criteria: ['Cosmetic', null, 'Blocking'] }) // true
|
|
2926
|
+
* isJudgeQuestion({ form: 'noul', instructions: 'Is a refund owed?' }) // true
|
|
2927
|
+
* isJudgeQuestion({ form: 'choice', criteria: { billing: null } }) // false
|
|
2928
|
+
* ```
|
|
2929
|
+
*/
|
|
2930
|
+
function isJudgeQuestion(value) {
|
|
2931
|
+
const checked = attempt(() => {
|
|
2932
|
+
if (!isRecord(value)) return false;
|
|
2933
|
+
const { form, instructions, criteria } = value;
|
|
2934
|
+
if (instructions !== void 0 && !isJudgeEntry(instructions)) return false;
|
|
2935
|
+
if (form === "choice") return isRecord(criteria) && Object.keys(criteria).length >= 2 && Object.values(criteria).every(nullableOf(isJudgeEntry));
|
|
2936
|
+
if (form === "score") return arrayOf(nullableOf(isJudgeEntry))(criteria) && criteria.length >= 2;
|
|
2937
|
+
if (form !== "noul") return false;
|
|
2938
|
+
if (criteria === void 0) return true;
|
|
2939
|
+
if (!isRecord(criteria)) return false;
|
|
2940
|
+
return optionalOf(isJudgeEntry)(criteria.true) && optionalOf(isJudgeEntry)(criteria.false);
|
|
2941
|
+
});
|
|
2942
|
+
return checked.success && checked.value;
|
|
2943
|
+
}
|
|
2944
|
+
/**
|
|
2945
|
+
* Checks whether a value is a System One response envelope with optional model and usage.
|
|
2946
|
+
*
|
|
2947
|
+
* @remarks
|
|
2948
|
+
* Answers remain unknown until checked against their questions. Missing and null usage counts
|
|
2949
|
+
* are accepted. Extra members are ignored, and unreadable fields return false.
|
|
2950
|
+
*
|
|
2951
|
+
* @param value - The unknown response candidate
|
|
2952
|
+
* @returns True if the envelope fields have their wire types; false otherwise
|
|
2953
|
+
* @example
|
|
2954
|
+
* ```ts
|
|
2955
|
+
* isSystemOneResponse({ answers: {}, usage: { input_tokens: null } }) // true
|
|
2956
|
+
* ```
|
|
2957
|
+
*/
|
|
2958
|
+
function isSystemOneResponse(value) {
|
|
2959
|
+
const checked = attempt(() => {
|
|
2960
|
+
if (!isRecord(value)) return false;
|
|
2961
|
+
const { model, answers, usage } = value;
|
|
2962
|
+
if (!optionalOf(isString)(model) || !isRecord(answers)) return false;
|
|
2963
|
+
if (usage === void 0) return true;
|
|
2964
|
+
if (!isRecord(usage)) return false;
|
|
2965
|
+
return optionalOf(nullableOf(isNumber))(usage.input_tokens) && optionalOf(nullableOf(isNumber))(usage.output_tokens);
|
|
2966
|
+
});
|
|
2967
|
+
return checked.success && checked.value;
|
|
2968
|
+
}
|
|
2969
|
+
/**
|
|
2970
|
+
* Checks whether a value is a System One answer whose type and distribution the wire can read.
|
|
2971
|
+
*
|
|
2972
|
+
* @remarks
|
|
2973
|
+
* The guard checks `type` and the distribution the wire dereferences: the `noul` number for a
|
|
2974
|
+
* noul, the `probabilities` map for a choice, and the `probabilities` map or dense array for a
|
|
2975
|
+
* score, each probability a finite number in [0, 1]. The `choice`, `score`, `confidence`, and
|
|
2976
|
+
* `legend` members are carried unchecked, because the wire never reads them, so a value this
|
|
2977
|
+
* guard accepts can hold any value in those members. Distribution sums are not constrained;
|
|
2978
|
+
* values are never normalized. Hostile reads return false.
|
|
2979
|
+
*
|
|
2980
|
+
* @param value - The unknown answer candidate
|
|
2981
|
+
* @returns True if the type and the distribution satisfy the wire contract; false otherwise
|
|
2982
|
+
* @example
|
|
2983
|
+
* ```ts
|
|
2984
|
+
* isSystemOneAnswer({ type: 'noul', noul: 0.9 }) // true
|
|
2985
|
+
* isSystemOneAnswer({ type: 'score', probabilities: [0.2, 0.8] }) // true
|
|
2986
|
+
* isSystemOneAnswer({ type: 'noul', noul: 1.1 }) // false
|
|
2987
|
+
* ```
|
|
2988
|
+
*/
|
|
2989
|
+
function isSystemOneAnswer(value) {
|
|
2990
|
+
const checked = attempt(() => {
|
|
2991
|
+
if (!isRecord(value)) return false;
|
|
2992
|
+
const { type } = value;
|
|
2993
|
+
if (type === "noul") return boundsOf(0, 1)(value.noul);
|
|
2994
|
+
if (type !== "choice" && type !== "score") return false;
|
|
2995
|
+
const { probabilities } = value;
|
|
2996
|
+
if (type === "score" && isArray(probabilities)) return arrayOf(boundsOf(0, 1))(probabilities);
|
|
2997
|
+
return isRecord(probabilities) && Object.values(probabilities).every(boundsOf(0, 1));
|
|
2998
|
+
});
|
|
2999
|
+
return checked.success && checked.value;
|
|
3000
|
+
}
|
|
2552
3001
|
//#endregion
|
|
2553
3002
|
//#region src/core/conversations/stores/DatabaseConversationStore.ts
|
|
2554
3003
|
/**
|
|
@@ -2900,7 +3349,7 @@ var AgentProvider = class {
|
|
|
2900
3349
|
};
|
|
2901
3350
|
}
|
|
2902
3351
|
async #request(request, signal) {
|
|
2903
|
-
const headers = await this.#
|
|
3352
|
+
const headers = await readHeaders(this.#headers, signal);
|
|
2904
3353
|
signal.throwIfAborted();
|
|
2905
3354
|
const response = await this.#transport(this.#url + this.#path, {
|
|
2906
3355
|
method: "POST",
|
|
@@ -2923,25 +3372,6 @@ var AgentProvider = class {
|
|
|
2923
3372
|
}
|
|
2924
3373
|
return response;
|
|
2925
3374
|
}
|
|
2926
|
-
async #requestHeaders(signal) {
|
|
2927
|
-
const headers = new Headers({ "Content-Type": "application/json" });
|
|
2928
|
-
if (this.#headers === void 0) return headers;
|
|
2929
|
-
const cleanup = new AbortController();
|
|
2930
|
-
const aborted = Promise.withResolvers();
|
|
2931
|
-
signal.addEventListener("abort", () => aborted.reject(signal.reason), {
|
|
2932
|
-
once: true,
|
|
2933
|
-
signal: cleanup.signal
|
|
2934
|
-
});
|
|
2935
|
-
try {
|
|
2936
|
-
signal.throwIfAborted();
|
|
2937
|
-
const hook = this.#headers;
|
|
2938
|
-
const entries = await Promise.race([Promise.resolve().then(() => hook(signal)), aborted.promise]);
|
|
2939
|
-
for (const [key, value] of Object.entries(entries)) headers.set(key, value);
|
|
2940
|
-
return headers;
|
|
2941
|
-
} finally {
|
|
2942
|
-
cleanup.abort();
|
|
2943
|
-
}
|
|
2944
|
-
}
|
|
2945
3375
|
};
|
|
2946
3376
|
//#endregion
|
|
2947
3377
|
//#region src/core/providers/RelayProvider.ts
|
|
@@ -3088,6 +3518,258 @@ var RelayProvider = class extends AgentProvider {
|
|
|
3088
3518
|
}
|
|
3089
3519
|
};
|
|
3090
3520
|
//#endregion
|
|
3521
|
+
//#region src/core/AgentJudge.ts
|
|
3522
|
+
/**
|
|
3523
|
+
* Implements the bounded HTTP calls, validation, and result merging of a judge behind concrete
|
|
3524
|
+
* wire seams.
|
|
3525
|
+
*
|
|
3526
|
+
* @remarks
|
|
3527
|
+
* A subclass fills `name`, `body`, and `read`; the constructor takes the `batch` switch that
|
|
3528
|
+
* decides whether one call carries every question or each question gets its own call in key
|
|
3529
|
+
* order. `ask` validates the request before any call, builds every body before the first call so
|
|
3530
|
+
* a wire limit refuses the request before inference, and runs the calls one after another, each
|
|
3531
|
+
* under its own deadline folded with the caller's signal. A cancel throws a `JudgeAbortError`
|
|
3532
|
+
* whose `partial` merges the calls that completed. A transport error reaches the caller unchanged.
|
|
3533
|
+
*
|
|
3534
|
+
* @example Writing a judge wire
|
|
3535
|
+
* ```ts
|
|
3536
|
+
* import type { JudgeRequest, JudgeResult } from '@orkestrel/agent'
|
|
3537
|
+
* import { AgentJudge, JudgeError } from '@orkestrel/agent'
|
|
3538
|
+
* import { isFiniteNumber, isRecord } from '@orkestrel/contract'
|
|
3539
|
+
*
|
|
3540
|
+
* // A wire whose server answers one yes/no question per call as { "yes": 0.93 }.
|
|
3541
|
+
* class YesJudge extends AgentJudge {
|
|
3542
|
+
* readonly name = 'yes'
|
|
3543
|
+
* body(request: JudgeRequest): object {
|
|
3544
|
+
* return { model: this.model, state: request.state, questions: request.questions }
|
|
3545
|
+
* }
|
|
3546
|
+
* read(value: unknown, request: JudgeRequest): JudgeResult {
|
|
3547
|
+
* const [id] = Object.keys(request.questions)
|
|
3548
|
+
* if (id === undefined || !isRecord(value) || !isFiniteNumber(value.yes)) {
|
|
3549
|
+
* throw new JudgeError('PROTOCOL', 'judge error: unreadable answer')
|
|
3550
|
+
* }
|
|
3551
|
+
* return { model: this.model, answers: { [id]: { form: 'noul', noul: value.yes } } }
|
|
3552
|
+
* }
|
|
3553
|
+
* }
|
|
3554
|
+
*
|
|
3555
|
+
* const judge = new YesJudge({
|
|
3556
|
+
* url: 'http://localhost:8010',
|
|
3557
|
+
* path: '/v1/yes',
|
|
3558
|
+
* model: 'yes-1',
|
|
3559
|
+
* batch: false,
|
|
3560
|
+
* })
|
|
3561
|
+
* ```
|
|
3562
|
+
*/
|
|
3563
|
+
var AgentJudge = class {
|
|
3564
|
+
#id;
|
|
3565
|
+
#url;
|
|
3566
|
+
#path;
|
|
3567
|
+
#model;
|
|
3568
|
+
#timeout;
|
|
3569
|
+
#transport;
|
|
3570
|
+
#headers;
|
|
3571
|
+
#batch;
|
|
3572
|
+
constructor(input) {
|
|
3573
|
+
this.#id = crypto.randomUUID();
|
|
3574
|
+
this.#url = input.url;
|
|
3575
|
+
this.#path = input.path ?? "";
|
|
3576
|
+
this.#model = input.model;
|
|
3577
|
+
this.#timeout = input.timeout ?? 12e4;
|
|
3578
|
+
this.#transport = input.fetch ?? globalThis.fetch.bind(globalThis);
|
|
3579
|
+
this.#headers = input.headers;
|
|
3580
|
+
this.#batch = input.batch ?? true;
|
|
3581
|
+
}
|
|
3582
|
+
/** Exposes the instance's minted UUID. */
|
|
3583
|
+
get id() {
|
|
3584
|
+
return this.#id;
|
|
3585
|
+
}
|
|
3586
|
+
/** Exposes the configured model identity. */
|
|
3587
|
+
get model() {
|
|
3588
|
+
return this.#model;
|
|
3589
|
+
}
|
|
3590
|
+
/**
|
|
3591
|
+
* Asks every question of the request about its state and merges the answers of every call.
|
|
3592
|
+
*
|
|
3593
|
+
* @param request - The state and the questions keyed by caller id
|
|
3594
|
+
* @param signal - The caller's cancellation bound
|
|
3595
|
+
* @returns The merged answers, refusals, and usage of every call
|
|
3596
|
+
* @throws JudgeAbortError Thrown when the caller's signal or a call's deadline fires, carrying
|
|
3597
|
+
* the merged result of the completed calls
|
|
3598
|
+
* @throws JudgeError Thrown with code `QUESTION` for an empty question map, a malformed question
|
|
3599
|
+
* or state, or a wire refusal before any call; with code `HTTP` for a non-OK response; and with
|
|
3600
|
+
* code `PROTOCOL` for a missing or unparsable response body
|
|
3601
|
+
*/
|
|
3602
|
+
async ask(request, signal) {
|
|
3603
|
+
if (signal.aborted) throw new JudgeAbortError(buildJudgeResult(this.#model, []));
|
|
3604
|
+
const state = copyJSON(request.state);
|
|
3605
|
+
if (!isJudgeEntry(state)) throw new JudgeError("QUESTION", "judge error: state is not a judge entry");
|
|
3606
|
+
const questions = isRecord(request.questions) ? Object.entries(request.questions) : [];
|
|
3607
|
+
if (questions.length === 0) throw new JudgeError("QUESTION", "judge error: no questions");
|
|
3608
|
+
const entries = [];
|
|
3609
|
+
for (const [id, question] of questions) {
|
|
3610
|
+
const copy = copyJSON(question);
|
|
3611
|
+
if (!isJudgeQuestion(copy)) throw new JudgeError("QUESTION", `judge error: question ${id} is malformed`);
|
|
3612
|
+
entries.push([id, copy]);
|
|
3613
|
+
}
|
|
3614
|
+
const owned = {
|
|
3615
|
+
state,
|
|
3616
|
+
questions: Object.fromEntries(entries)
|
|
3617
|
+
};
|
|
3618
|
+
const calls = (this.#batch ? [owned] : entries.map(([id, question]) => ({
|
|
3619
|
+
state,
|
|
3620
|
+
questions: { [id]: question }
|
|
3621
|
+
}))).map((part) => ({
|
|
3622
|
+
request: part,
|
|
3623
|
+
body: JSON.stringify(this.body(part))
|
|
3624
|
+
}));
|
|
3625
|
+
const results = [];
|
|
3626
|
+
for (const call of calls) results.push(await this.#call(call.request, call.body, signal, results));
|
|
3627
|
+
return buildJudgeResult(this.#model, results);
|
|
3628
|
+
}
|
|
3629
|
+
async #call(request, body, signal, completed) {
|
|
3630
|
+
const timeout = new Timeout({ ms: this.#timeout });
|
|
3631
|
+
timeout.start();
|
|
3632
|
+
const combined = AbortSignal.any([timeout.signal, signal]);
|
|
3633
|
+
try {
|
|
3634
|
+
const headers = await readHeaders(this.#headers, combined);
|
|
3635
|
+
combined.throwIfAborted();
|
|
3636
|
+
const response = await this.#transport(this.#url + this.#path, {
|
|
3637
|
+
method: "POST",
|
|
3638
|
+
headers,
|
|
3639
|
+
body,
|
|
3640
|
+
signal: combined
|
|
3641
|
+
});
|
|
3642
|
+
if (!response.ok) {
|
|
3643
|
+
let detail;
|
|
3644
|
+
try {
|
|
3645
|
+
detail = response.body === null ? "" : (await readText(response.body, MAX_ERROR_BODY_LENGTH, combined)).text;
|
|
3646
|
+
} catch (cause) {
|
|
3647
|
+
throw new JudgeError("HTTP", `judge error: ${response.status} - (error body unavailable)`, {
|
|
3648
|
+
status: response.status,
|
|
3649
|
+
cause
|
|
3650
|
+
});
|
|
3651
|
+
}
|
|
3652
|
+
combined.throwIfAborted();
|
|
3653
|
+
throw new JudgeError("HTTP", `judge error: ${response.status}${detail === "" ? "" : ` - ${detail}`}`, { status: response.status });
|
|
3654
|
+
}
|
|
3655
|
+
if (response.body === null) throw new JudgeError("PROTOCOL", "judge error: no response body");
|
|
3656
|
+
const text = await readText(response.body, void 0, combined);
|
|
3657
|
+
combined.throwIfAborted();
|
|
3658
|
+
const value = parseJSON(text.text);
|
|
3659
|
+
if (value === void 0) throw new JudgeError("PROTOCOL", "judge error: invalid JSON body");
|
|
3660
|
+
const result = this.read(value, request);
|
|
3661
|
+
combined.throwIfAborted();
|
|
3662
|
+
return result;
|
|
3663
|
+
} catch (error) {
|
|
3664
|
+
if (combined.aborted) throw new JudgeAbortError(buildJudgeResult(this.#model, completed), error === combined.reason ? void 0 : { cause: error });
|
|
3665
|
+
throw error;
|
|
3666
|
+
} finally {
|
|
3667
|
+
timeout.clear();
|
|
3668
|
+
}
|
|
3669
|
+
}
|
|
3670
|
+
};
|
|
3671
|
+
//#endregion
|
|
3672
|
+
//#region src/core/providers/SystemOneJudge.ts
|
|
3673
|
+
/**
|
|
3674
|
+
* Carries judge questions over the System One protocol and derives answers from server distributions.
|
|
3675
|
+
*
|
|
3676
|
+
* @remarks
|
|
3677
|
+
* The caller supplies the server origin and model. Every question travels in one request.
|
|
3678
|
+
* Server measures are ignored; the response model is preserved.
|
|
3679
|
+
* `headers` supplies authentication through the shared judge engine.
|
|
3680
|
+
*
|
|
3681
|
+
* @example Asking a System One server a choice, a noul, and a score
|
|
3682
|
+
* ```ts
|
|
3683
|
+
* import { computeReading, createSystemOneJudge } from '@orkestrel/agent'
|
|
3684
|
+
*
|
|
3685
|
+
* const judge = createSystemOneJudge({ url: 'http://localhost:11434', model: 'tev1:0.8b' })
|
|
3686
|
+
* const result = await judge.ask(
|
|
3687
|
+
* {
|
|
3688
|
+
* state: 'Our checkout has returned 500 errors since 9am. I want a refund for today.',
|
|
3689
|
+
* questions: {
|
|
3690
|
+
* label: {
|
|
3691
|
+
* form: 'choice',
|
|
3692
|
+
* instructions: 'Which label fits this ticket?',
|
|
3693
|
+
* criteria: { billing: 'Payments and refunds', bug: 'Software errors', account: null },
|
|
3694
|
+
* },
|
|
3695
|
+
* refund: {
|
|
3696
|
+
* form: 'noul',
|
|
3697
|
+
* instructions: 'Does the customer ask for money back?',
|
|
3698
|
+
* criteria: {
|
|
3699
|
+
* true: 'The customer asks for a refund or for money back.',
|
|
3700
|
+
* false: 'The customer does not ask for money back.',
|
|
3701
|
+
* },
|
|
3702
|
+
* },
|
|
3703
|
+
* severity: {
|
|
3704
|
+
* form: 'score',
|
|
3705
|
+
* instructions: 'How severe is the reported issue?',
|
|
3706
|
+
* criteria: ['Cosmetic; no impact', 'Degraded, workaround exists', 'Blocking; no workaround'],
|
|
3707
|
+
* },
|
|
3708
|
+
* },
|
|
3709
|
+
* },
|
|
3710
|
+
* AbortSignal.timeout(30_000),
|
|
3711
|
+
* )
|
|
3712
|
+
* const readings = Object.fromEntries(
|
|
3713
|
+
* Object.entries(result.answers).map(([id, answer]) => [id, computeReading(answer)]),
|
|
3714
|
+
* )
|
|
3715
|
+
* result.model // 'tev1:0.8b' — the model the server named
|
|
3716
|
+
* result.usage // { prompt: 975, completion: 4, total: 979 }
|
|
3717
|
+
* readings.label // { winner: 'bug', probability: 0.9691, confidence: 0.9536 } to four decimals
|
|
3718
|
+
* readings.refund // { winner: 'true', probability: 0.9979, confidence: 0.9958 } to four decimals
|
|
3719
|
+
* readings.severity // { winner: '1', probability: 0.9494, confidence: 0.9241, score: 0.9919 } to four decimals
|
|
3720
|
+
* ```
|
|
3721
|
+
*/
|
|
3722
|
+
var SystemOneJudge = class extends AgentJudge {
|
|
3723
|
+
constructor(options) {
|
|
3724
|
+
super({
|
|
3725
|
+
...options,
|
|
3726
|
+
path: SYSTEM_ONE_PATH,
|
|
3727
|
+
batch: true
|
|
3728
|
+
});
|
|
3729
|
+
}
|
|
3730
|
+
/** Identifies the System One protocol. */
|
|
3731
|
+
name = "systemone";
|
|
3732
|
+
/**
|
|
3733
|
+
* Projects the state and questions onto the System One request with the configured model.
|
|
3734
|
+
*
|
|
3735
|
+
* @param request - The state and questions keyed by caller id
|
|
3736
|
+
* @returns The System One request body
|
|
3737
|
+
*/
|
|
3738
|
+
body(request) {
|
|
3739
|
+
return {
|
|
3740
|
+
state: request.state,
|
|
3741
|
+
model: this.model,
|
|
3742
|
+
questions: Object.fromEntries(Object.entries(request.questions).map(([id, question]) => [id, questionToSystemOne(question)]))
|
|
3743
|
+
};
|
|
3744
|
+
}
|
|
3745
|
+
/**
|
|
3746
|
+
* Decodes requested System One answers and reports the server model and available usage.
|
|
3747
|
+
*
|
|
3748
|
+
* @param value - The parsed response body
|
|
3749
|
+
* @param request - The questions defining the expected answers
|
|
3750
|
+
* @returns The decoded distributions, model, and available usage
|
|
3751
|
+
* @throws JudgeError Thrown with code `PROTOCOL` for a malformed envelope or an invalid answer naming its question id
|
|
3752
|
+
*/
|
|
3753
|
+
read(value, request) {
|
|
3754
|
+
if (!isSystemOneResponse(value)) throw new JudgeError("PROTOCOL", "judge error: invalid System One response envelope");
|
|
3755
|
+
const entries = [];
|
|
3756
|
+
for (const [id, question] of Object.entries(request.questions)) {
|
|
3757
|
+
if (!Object.hasOwn(value.answers, id)) throw new JudgeError("PROTOCOL", `judge error: question ${id} has no System One answer`);
|
|
3758
|
+
const wire = value.answers[id];
|
|
3759
|
+
if (!isSystemOneAnswer(wire)) throw new JudgeError("PROTOCOL", `judge error: question ${id} has an invalid System One answer`);
|
|
3760
|
+
const answer = extractSystemOneAnswer(wire, question);
|
|
3761
|
+
if (answer === void 0) throw new JudgeError("PROTOCOL", `judge error: question ${id} has a mismatched or incomplete System One answer`);
|
|
3762
|
+
entries.push([id, answer]);
|
|
3763
|
+
}
|
|
3764
|
+
const usage = extractSystemOneUsage(value.usage);
|
|
3765
|
+
return {
|
|
3766
|
+
model: value.model ?? this.model,
|
|
3767
|
+
answers: Object.fromEntries(entries),
|
|
3768
|
+
...usage === void 0 ? {} : { usage }
|
|
3769
|
+
};
|
|
3770
|
+
}
|
|
3771
|
+
};
|
|
3772
|
+
//#endregion
|
|
3091
3773
|
//#region src/core/RelayStream.ts
|
|
3092
3774
|
/**
|
|
3093
3775
|
* Streams a provider call as validated NDJSON frames under response backpressure.
|
|
@@ -4125,7 +4807,20 @@ function createAgentRunner(options) {
|
|
|
4125
4807
|
handler: handleAgentRunnerJob.bind(void 0, registry, partial)
|
|
4126
4808
|
});
|
|
4127
4809
|
}
|
|
4810
|
+
/**
|
|
4811
|
+
* Creates a judge that sends every question through the configured System One server.
|
|
4812
|
+
*
|
|
4813
|
+
* @param options - The server origin, model, and optional transport, headers, and timeout
|
|
4814
|
+
* @returns The configured judge behind its shared interface
|
|
4815
|
+
* @example
|
|
4816
|
+
* ```ts
|
|
4817
|
+
* const judge = createSystemOneJudge({ url: 'http://localhost:11434', model: 'tev1:0.8b' })
|
|
4818
|
+
* ```
|
|
4819
|
+
*/
|
|
4820
|
+
function createSystemOneJudge(options) {
|
|
4821
|
+
return new SystemOneJudge(options);
|
|
4822
|
+
}
|
|
4128
4823
|
//#endregion
|
|
4129
|
-
export { Agent, AgentContext, AgentError, AgentJobError, AgentProvider, AgentRegistry, Authority, CONVERSATION_RECAP_PREFIX, Channel, Conversation, ConversationError, ConversationManager, DEFAULT_AGENT_LIMIT, DEFAULT_AUTHORITY_ZONE, DEFAULT_CONVERSATION_KEEP, DEFAULT_PROVIDER_TIMEOUT, DEFAULT_RELAY_LIMIT, DatabaseConversationStore, IMAGE_TOKEN_ESTIMATE, INVALID_RELAY_STATUS, Instruction, InstructionManager, MAX_ERROR_BODY_LENGTH, MESSAGE_TOKEN_OVERHEAD, MemoryConversationStore, OVERSIZED_RELAY_STATUS, ProviderAbortError, ProviderError, RELAY_CONTENT_TYPE, RELAY_PROVIDER_MESSAGE, RelayProvider, RelayStream, Scope, ScopeManager, THINK_CLOSE, THINK_OPEN, ThinkSplitter, UNAUTHORIZED_RELAY_STATUS, UPSTREAM_RELAY_STATUS, WORKSPACE_SECTION_HEADER, agentResultToJSON, assembleResult, attachImages, attachUserImages, buildProviderResult, buildRecapMessage, buildSummaryMessage, collectImageData, createAgent, createAgentContext, createAgentQueue, createAgentRegistry, createAgentRunner, createAuthority, createChannel, createConversation, createConversationManager, createDatabaseConversationStore, createInstruction, createInstructionManager, createMemoryConversationStore, createRelay, createRelayProvider, createScope, createScopeManager, createThinkSplitter, denyCall, estimateMessages, estimateTokens, filterAllowList, handleAgentQueueJob, handleAgentRunnerJob, intersectKeys, isAgentError, isAgentJobError, isConversationError, isConversationSnapshot, isMessage, isProviderAbortError, isProviderError, isSection, joinThinking, messageContract, messageShape, providerRequestContract, providerRequestShape, providerResultContract, providerResultShape, readChunks, readText, relayFrameContract, relayFrameShape, renderFencedFile, renderSection, resolveClose, resolveItem, resolveOpen, sanitizeToken, sanitizeUsage, settleAgentJob, sumUsage, toolCallShape };
|
|
4824
|
+
export { Agent, AgentContext, AgentError, AgentJobError, AgentJudge, AgentProvider, AgentRegistry, Authority, CONVERSATION_RECAP_PREFIX, Channel, Conversation, ConversationError, ConversationManager, DEFAULT_AGENT_LIMIT, DEFAULT_AUTHORITY_ZONE, DEFAULT_CONVERSATION_KEEP, DEFAULT_PROVIDER_TIMEOUT, DEFAULT_RELAY_LIMIT, DatabaseConversationStore, IMAGE_TOKEN_ESTIMATE, INVALID_RELAY_STATUS, Instruction, InstructionManager, JudgeAbortError, JudgeError, MAX_ERROR_BODY_LENGTH, MESSAGE_TOKEN_OVERHEAD, MemoryConversationStore, OVERSIZED_RELAY_STATUS, ProviderAbortError, ProviderError, RELAY_CONTENT_TYPE, RELAY_PROVIDER_MESSAGE, RelayProvider, RelayStream, SYSTEM_ONE_PATH, Scope, ScopeManager, SystemOneJudge, THINK_CLOSE, THINK_OPEN, ThinkSplitter, UNAUTHORIZED_RELAY_STATUS, UPSTREAM_RELAY_STATUS, WORKSPACE_SECTION_HEADER, agentResultToJSON, assembleResult, attachImages, attachUserImages, buildJudgeResult, buildProviderResult, buildRecapMessage, buildSummaryMessage, collectImageData, computeReading, copyJSON, createAgent, createAgentContext, createAgentQueue, createAgentRegistry, createAgentRunner, createAuthority, createChannel, createConversation, createConversationManager, createDatabaseConversationStore, createInstruction, createInstructionManager, createMemoryConversationStore, createRelay, createRelayProvider, createScope, createScopeManager, createSystemOneJudge, createThinkSplitter, denyCall, estimateMessages, estimateTokens, extractSystemOneAnswer, extractSystemOneUsage, filterAllowList, handleAgentQueueJob, handleAgentRunnerJob, intersectKeys, isAgentError, isAgentJobError, isConversationError, isConversationSnapshot, isJudgeAbortError, isJudgeEntry, isJudgeError, isJudgeQuestion, isMessage, isProviderAbortError, isProviderError, isSection, isSystemOneAnswer, isSystemOneResponse, joinThinking, messageContract, messageShape, providerRequestContract, providerRequestShape, providerResultContract, providerResultShape, questionToSystemOne, readChunks, readHeaders, readText, relayFrameContract, relayFrameShape, renderFencedFile, renderSection, resolveClose, resolveItem, resolveOpen, sanitizeToken, sanitizeUsage, settleAgentJob, sumUsage, toolCallShape };
|
|
4130
4825
|
|
|
4131
4826
|
//# sourceMappingURL=index.js.map
|