@orkestrel/agent 0.0.26 → 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.
@@ -9,6 +9,7 @@ import type { EmitterErrorHandler } from '@orkestrel/emitter';
9
9
  import type { EmitterHooks } from '@orkestrel/emitter';
10
10
  import type { EmitterInterface } from '@orkestrel/emitter';
11
11
  import type { FileInterface } from '@orkestrel/workspace';
12
+ import type { JSONRecord } from '@orkestrel/contract';
12
13
  import { JSONShape } from '@orkestrel/contract';
13
14
  import { JSONValue } from '@orkestrel/contract';
14
15
  import { LiteralShape } from '@orkestrel/contract';
@@ -624,6 +625,99 @@ export declare interface AgentJobInput {
624
625
  readonly children?: readonly AgentJobInput[];
625
626
  }
626
627
 
628
+ /**
629
+ * Implements the bounded HTTP calls, validation, and result merging of a judge behind concrete
630
+ * wire seams.
631
+ *
632
+ * @remarks
633
+ * A subclass fills `name`, `body`, and `read`; the constructor takes the `batch` switch that
634
+ * decides whether one call carries every question or each question gets its own call in key
635
+ * order. `ask` validates the request before any call, builds every body before the first call so
636
+ * a wire limit refuses the request before inference, and runs the calls one after another, each
637
+ * under its own deadline folded with the caller's signal. A cancel throws a `JudgeAbortError`
638
+ * whose `partial` merges the calls that completed. A transport error reaches the caller unchanged.
639
+ *
640
+ * @example Writing a judge wire
641
+ * ```ts
642
+ * import type { JudgeRequest, JudgeResult } from '@orkestrel/agent'
643
+ * import { AgentJudge, JudgeError } from '@orkestrel/agent'
644
+ * import { isFiniteNumber, isRecord } from '@orkestrel/contract'
645
+ *
646
+ * // A wire whose server answers one yes/no question per call as { "yes": 0.93 }.
647
+ * class YesJudge extends AgentJudge {
648
+ * readonly name = 'yes'
649
+ * body(request: JudgeRequest): object {
650
+ * return { model: this.model, state: request.state, questions: request.questions }
651
+ * }
652
+ * read(value: unknown, request: JudgeRequest): JudgeResult {
653
+ * const [id] = Object.keys(request.questions)
654
+ * if (id === undefined || !isRecord(value) || !isFiniteNumber(value.yes)) {
655
+ * throw new JudgeError('PROTOCOL', 'judge error: unreadable answer')
656
+ * }
657
+ * return { model: this.model, answers: { [id]: { form: 'noul', noul: value.yes } } }
658
+ * }
659
+ * }
660
+ *
661
+ * const judge = new YesJudge({
662
+ * url: 'http://localhost:8010',
663
+ * path: '/v1/yes',
664
+ * model: 'yes-1',
665
+ * batch: false,
666
+ * })
667
+ * ```
668
+ */
669
+ export declare abstract class AgentJudge implements AgentJudgeInterface {
670
+ #private;
671
+ constructor(input: AgentJudgeInput);
672
+ /** Identifies the concrete wire. */
673
+ abstract readonly name: string;
674
+ /** Exposes the instance's minted UUID. */
675
+ get id(): string;
676
+ /** Exposes the configured model identity. */
677
+ get model(): string;
678
+ /** Projects one call's request onto the concrete protocol's wire body. */
679
+ abstract body(request: JudgeRequest): object;
680
+ /** Decodes one call's parsed response body into the answers for that call's questions. */
681
+ abstract read(value: unknown, request: JudgeRequest): JudgeResult;
682
+ /**
683
+ * Asks every question of the request about its state and merges the answers of every call.
684
+ *
685
+ * @param request - The state and the questions keyed by caller id
686
+ * @param signal - The caller's cancellation bound
687
+ * @returns The merged answers, refusals, and usage of every call
688
+ * @throws JudgeAbortError Thrown when the caller's signal or a call's deadline fires, carrying
689
+ * the merged result of the completed calls
690
+ * @throws JudgeError Thrown with code `QUESTION` for an empty question map, a malformed question
691
+ * or state, or a wire refusal before any call; with code `HTTP` for a non-OK response; and with
692
+ * code `PROTOCOL` for a missing or unparsable response body
693
+ */
694
+ ask(request: JudgeRequest, signal: AbortSignal): Promise<JudgeResult>;
695
+ }
696
+
697
+ /**
698
+ * Configures the judge engine's destination, identity, and call split.
699
+ *
700
+ * @remarks
701
+ * `path` appends to `url`. `model` is the identity the judge reports when a response names none.
702
+ * `timeout`, `fetch`, and `headers` bound, carry, and authenticate each call as they do for a
703
+ * provider; the deadline applies to each call on its own.
704
+ */
705
+ export declare interface AgentJudgeInput extends Pick<ProviderOptions, 'timeout' | 'fetch' | 'headers'> {
706
+ readonly url: string;
707
+ readonly path?: string;
708
+ readonly model: string;
709
+ /** If true, one call carries every question; if false, the engine issues one call per question. Default: true. */
710
+ readonly batch?: boolean;
711
+ }
712
+
713
+ /** Defines the wire seams of the shared judge engine. */
714
+ export declare interface AgentJudgeInterface extends JudgeInterface {
715
+ /** Projects one call's request onto the concrete protocol's serializable body. */
716
+ body(request: JudgeRequest): object;
717
+ /** Decodes one call's parsed response body into the answers for that call's questions. */
718
+ read(value: unknown, request: JudgeRequest): JudgeResult;
719
+ }
720
+
627
721
  /**
628
722
  * Configures `createAgent` — the loop's bounds and pacing, the reserved `on` hooks, the
629
723
  * construction-time context wiring (`instructions` / `workspaces` / `scope`), the `conversations`
@@ -1350,6 +1444,31 @@ export declare interface AuthorityRule {
1350
1444
  readonly reason?: string;
1351
1445
  }
1352
1446
 
1447
+ /**
1448
+ * Merges the results of a judge request's calls into one result.
1449
+ *
1450
+ * @remarks
1451
+ * Pure and total. Answers and refusals are joined by question id; `refusals` is omitted when no
1452
+ * call refused a question. Each call's usage passes through {@link sanitizeUsage} and the totals
1453
+ * add through {@link sumUsage}; `usage` is omitted when no call reported one. The model is the
1454
+ * first call's, or the given model when the list is empty, as the empty partial of a cancel
1455
+ * before the first call requires.
1456
+ *
1457
+ * @param model - The judge's configured model, reported when no call completed
1458
+ * @param results - The completed calls' results in call order
1459
+ * @returns The merged result
1460
+ * @example
1461
+ * ```ts
1462
+ * buildJudgeResult('tev1:0.8b', []) // { model: 'tev1:0.8b', answers: {} }
1463
+ * buildJudgeResult('jev-latest', [
1464
+ * { model: 'jev-1.13.0', answers: { urgent: { form: 'noul', noul: 0.95 } } },
1465
+ * { model: 'jev-1.13.0', answers: {}, refusals: { team: { missing: ['sales'] } } },
1466
+ * ])
1467
+ * // { model: 'jev-1.13.0', answers: { urgent: ... }, refusals: { team: { missing: ['sales'] } } }
1468
+ * ```
1469
+ */
1470
+ export declare function buildJudgeResult(model: string, results: readonly JudgeResult[]): JudgeResult;
1471
+
1353
1472
  /**
1354
1473
  * Assembles a provider result with only populated optional fields.
1355
1474
  *
@@ -1482,6 +1601,22 @@ export declare interface ChannelInterface<T> {
1482
1601
  drain(): AsyncGenerator<T, void>;
1483
1602
  }
1484
1603
 
1604
+ /** Carries a choice distribution keyed by option name, in criteria order. */
1605
+ export declare interface ChoiceAnswer {
1606
+ readonly form: 'choice';
1607
+ readonly probabilities: Readonly<Record<string, number>>;
1608
+ }
1609
+
1610
+ /** Maps each option name the model sees to its description; null keeps an undescribed option in the map. */
1611
+ export declare type ChoiceCriteria = Readonly<Record<string, JudgeEntry | null>>;
1612
+
1613
+ /** Asks the model to pick one named option from its criteria. */
1614
+ export declare interface ChoiceQuestion {
1615
+ readonly form: 'choice';
1616
+ readonly instructions?: JudgeEntry;
1617
+ readonly criteria: ChoiceCriteria;
1618
+ }
1619
+
1485
1620
  /**
1486
1621
  * Collects the `base64` payload of the image files in a workspace file list — the data an agent
1487
1622
  * context attaches to the last user message.
@@ -1522,6 +1657,35 @@ export declare interface CompactOptions {
1522
1657
  readonly sections?: number;
1523
1658
  }
1524
1659
 
1660
+ /**
1661
+ * Derives the winner, its probability, the published confidence, and a score answer's expected
1662
+ * level from a judge answer's distribution.
1663
+ *
1664
+ * @remarks
1665
+ * The winner is the first strictly greatest candidate in enumeration order: an option name in
1666
+ * criteria order, a level index, or `'false'` then `'true'` for a noul, so a noul of exactly 0.5
1667
+ * names `'false'`. Confidence follows the TypeSafe formulas, whatever confidence a server sent: a
1668
+ * choice reads `(max(p) - 1/n) / (1 - 1/n)`, a noul reads `|2p - 1|` (the choice formula at
1669
+ * n = 2), and a score reads `max(0, 1 - spread / even_spread)`, where `spread` sums each level's
1670
+ * probability times its distance from the winning level and `even_spread` is the mean distance
1671
+ * from the middle level. `score` is the expected level `sum(i * p_i)` and is set only for a score
1672
+ * answer.
1673
+ *
1674
+ * @param answer - The answer whose distribution is read
1675
+ * @returns The derived measures
1676
+ * @throws JudgeError Thrown with code `PROTOCOL` when a choice or score answer has fewer than 2
1677
+ * candidates, because the confidence formulas divide by the candidate count
1678
+ * @example
1679
+ * ```ts
1680
+ * computeReading({ form: 'choice', probabilities: { billing: 0.88, technical: 0.12, sales: 0 } })
1681
+ * // { winner: 'billing', probability: 0.88, confidence: 0.82 } to two decimals
1682
+ * computeReading({ form: 'score', probabilities: [0, 0.57, 0.43] })
1683
+ * // { winner: '1', probability: 0.57, confidence: 0.355, score: 1.43 } to three decimals
1684
+ * computeReading({ form: 'noul', noul: 0.5 }) // { winner: 'false', probability: 0.5, confidence: 0 }
1685
+ * ```
1686
+ */
1687
+ export declare function computeReading(answer: JudgeAnswer): Reading;
1688
+
1525
1689
  /**
1526
1690
  * Holds a provider's optional context-framing default, keyed by section kind — the framing a model
1527
1691
  * prefers (for example XML tags against Markdown headers), declared by a {@link ProviderInterface}
@@ -2343,6 +2507,24 @@ export declare interface ConversationStoreInterface {
2343
2507
  */
2344
2508
  export declare type ConversationSummaryHandler = (messages: readonly Message[]) => Promise<string>;
2345
2509
 
2510
+ /**
2511
+ * Owns a value by serializing it to JSON and parsing the text, so the copy shares nothing with its source.
2512
+ *
2513
+ * @remarks
2514
+ * A value that JSON cannot carry, such as a cycle or a bigint, and a value that serializes to
2515
+ * nothing, such as a function, both return undefined, so a guard over the result refuses them.
2516
+ * A proxied value serializes through its traps, where a structured clone refuses it.
2517
+ *
2518
+ * @param value - The value to own
2519
+ * @returns The owned JSON copy, or undefined when the value is not JSON
2520
+ * @example
2521
+ * ```ts
2522
+ * copyJSON({ state: 'A ticket.' }) // { state: 'A ticket.' }
2523
+ * copyJSON(() => 1) // undefined
2524
+ * ```
2525
+ */
2526
+ export declare function copyJSON(value: unknown): unknown;
2527
+
2346
2528
  /**
2347
2529
  * Creates an agent loop — an {@link AgentInterface} composing a
2348
2530
  * {@link ProviderInterface}, its {@link AgentContextInterface}, and a tool registry
@@ -2926,6 +3108,18 @@ export declare function createScope(input: ScopeInput): ScopeInterface;
2926
3108
  */
2927
3109
  export declare function createScopeManager(options?: ScopeManagerOptions): ScopeManagerInterface;
2928
3110
 
3111
+ /**
3112
+ * Creates a judge that sends every question through the configured System One server.
3113
+ *
3114
+ * @param options - The server origin, model, and optional transport, headers, and timeout
3115
+ * @returns The configured judge behind its shared interface
3116
+ * @example
3117
+ * ```ts
3118
+ * const judge = createSystemOneJudge({ url: 'http://localhost:11434', model: 'tev1:0.8b' })
3119
+ * ```
3120
+ */
3121
+ export declare function createSystemOneJudge(options: SystemOneJudgeOptions): JudgeInterface;
3122
+
2929
3123
  /**
2930
3124
  * Creates a fresh stream-stateful `<think>` separator — a {@link ThinkSplitterInterface} that
2931
3125
  * splits a thinking model's in-content `<think>…</think>` reasoning spans away from the answer,
@@ -3142,6 +3336,40 @@ export declare function estimateMessages(messages: readonly Message[]): number;
3142
3336
  */
3143
3337
  export declare function estimateTokens(text: string): number;
3144
3338
 
3339
+ /**
3340
+ * Extracts a System One distribution in question criteria order and drops server measures.
3341
+ *
3342
+ * @remarks
3343
+ * Every requested candidate must have a finite probability in [0, 1]; the helper checks each
3344
+ * requested candidate itself and reads no other member. Score maps and arrays project onto the
3345
+ * requested levels. Additional candidates are ignored, sums are unconstrained, and probabilities
3346
+ * are preserved without normalization.
3347
+ *
3348
+ * @param answer - The wire answer to decode
3349
+ * @param question - The question defining the form and candidate order
3350
+ * @returns The domain answer, or undefined for a mismatched form or a requested candidate whose
3351
+ * probability is missing, not finite, or outside [0, 1]
3352
+ * @example
3353
+ * ```ts
3354
+ * extractSystemOneAnswer({ type: 'noul', noul: 0.9 }, { form: 'noul' })
3355
+ * // { form: 'noul', noul: 0.9 }
3356
+ * ```
3357
+ */
3358
+ export declare function extractSystemOneAnswer(answer: SystemOneAnswer, question: JudgeQuestion): JudgeAnswer | undefined;
3359
+
3360
+ /**
3361
+ * Maps complete System One token counts onto validated token usage.
3362
+ *
3363
+ * @param usage - The optional wire token counts
3364
+ * @returns Token usage, or undefined for missing, null, negative, or non-finite counts
3365
+ * @example
3366
+ * ```ts
3367
+ * extractSystemOneUsage({ input_tokens: 975, output_tokens: 4 })
3368
+ * // { prompt: 975, completion: 4, total: 979 }
3369
+ * ```
3370
+ */
3371
+ export declare function extractSystemOneUsage(usage: SystemOneUsage | undefined): TokenUsage | undefined;
3372
+
3145
3373
  /**
3146
3374
  * Filters a list of items by a {@link import('./types.js').ScopeInterface} allow-list of keys —
3147
3375
  * `undefined` passes everything, `[]` passes nothing, and a non-empty list passes the listed keys
@@ -3556,6 +3784,71 @@ export declare function isConversationError(value: unknown): value is Conversati
3556
3784
  */
3557
3785
  export declare function isConversationSnapshot(value: unknown): value is ConversationSnapshot;
3558
3786
 
3787
+ /**
3788
+ * Narrows a caught value to a {@link JudgeAbortError} through `instanceof`, so a `catch` can
3789
+ * recover its `partial` result.
3790
+ *
3791
+ * @param value - The caught value
3792
+ * @returns True if the value is a {@link JudgeAbortError}; false otherwise
3793
+ * @example
3794
+ * ```ts
3795
+ * isJudgeAbortError(new JudgeAbortError({ model: 'tev1:0.8b', answers: {} })) // true
3796
+ * ```
3797
+ */
3798
+ export declare function isJudgeAbortError(value: unknown): value is JudgeAbortError;
3799
+
3800
+ /**
3801
+ * Checks whether a value is a judge entry: a string, a JSON record, or a JSON array.
3802
+ *
3803
+ * @remarks
3804
+ * Total: a cycle, a non-JSON member, a class instance, and a hostile input return false. `null`
3805
+ * is not an entry; a criteria guard admits it where the protocol keeps it.
3806
+ *
3807
+ * @param value - The unknown entry candidate
3808
+ * @returns True if the value is a string or JSON structure the model can read; false otherwise
3809
+ * @example
3810
+ * ```ts
3811
+ * isJudgeEntry('Customer asks for a refund') // true
3812
+ * isJudgeEntry({ ticket: 4182, tags: ['billing'] }) // true
3813
+ * isJudgeEntry(null) // false
3814
+ * isJudgeEntry({ opened: new Date() }) // false
3815
+ * ```
3816
+ */
3817
+ export declare function isJudgeEntry(value: unknown): value is JudgeEntry;
3818
+
3819
+ /**
3820
+ * Narrows a caught value to the judge failure class through `instanceof`.
3821
+ *
3822
+ * @param value - The caught value
3823
+ * @returns True if the value is a {@link JudgeError}; false otherwise
3824
+ * @example
3825
+ * ```ts
3826
+ * isJudgeError(new JudgeError('HTTP', 'judge error: 429', { status: 429 })) // true
3827
+ * ```
3828
+ */
3829
+ export declare function isJudgeError(value: unknown): value is JudgeError;
3830
+
3831
+ /**
3832
+ * Checks whether a value is a well-formed judge question of the choice, score, or noul form.
3833
+ *
3834
+ * @remarks
3835
+ * Total: a hostile input returns false. A choice needs at least 2 options and a score at least 2
3836
+ * levels, because the confidence formula divides by the candidate count; a description or a level
3837
+ * can be `null`. `instructions` and noul `criteria` are omitted when absent and never `null`.
3838
+ * Server limits on option, level, and question counts are left to the server.
3839
+ *
3840
+ * @param value - The unknown question candidate
3841
+ * @returns True if the value is a question the judge engine can send; false otherwise
3842
+ * @example
3843
+ * ```ts
3844
+ * isJudgeQuestion({ form: 'choice', criteria: { billing: null, bug: 'Software defect' } }) // true
3845
+ * isJudgeQuestion({ form: 'score', criteria: ['Cosmetic', null, 'Blocking'] }) // true
3846
+ * isJudgeQuestion({ form: 'noul', instructions: 'Is a refund owed?' }) // true
3847
+ * isJudgeQuestion({ form: 'choice', criteria: { billing: null } }) // false
3848
+ * ```
3849
+ */
3850
+ export declare function isJudgeQuestion(value: unknown): value is JudgeQuestion;
3851
+
3559
3852
  /**
3560
3853
  * Checks whether a value satisfies the domain conversation-message contract.
3561
3854
  *
@@ -3629,6 +3922,44 @@ export declare function isProviderError(value: unknown): value is ProviderError;
3629
3922
  */
3630
3923
  export declare function isSection(value: unknown): value is Section;
3631
3924
 
3925
+ /**
3926
+ * Checks whether a value is a System One answer whose type and distribution the wire can read.
3927
+ *
3928
+ * @remarks
3929
+ * The guard checks `type` and the distribution the wire dereferences: the `noul` number for a
3930
+ * noul, the `probabilities` map for a choice, and the `probabilities` map or dense array for a
3931
+ * score, each probability a finite number in [0, 1]. The `choice`, `score`, `confidence`, and
3932
+ * `legend` members are carried unchecked, because the wire never reads them, so a value this
3933
+ * guard accepts can hold any value in those members. Distribution sums are not constrained;
3934
+ * values are never normalized. Hostile reads return false.
3935
+ *
3936
+ * @param value - The unknown answer candidate
3937
+ * @returns True if the type and the distribution satisfy the wire contract; false otherwise
3938
+ * @example
3939
+ * ```ts
3940
+ * isSystemOneAnswer({ type: 'noul', noul: 0.9 }) // true
3941
+ * isSystemOneAnswer({ type: 'score', probabilities: [0.2, 0.8] }) // true
3942
+ * isSystemOneAnswer({ type: 'noul', noul: 1.1 }) // false
3943
+ * ```
3944
+ */
3945
+ export declare function isSystemOneAnswer(value: unknown): value is SystemOneAnswer;
3946
+
3947
+ /**
3948
+ * Checks whether a value is a System One response envelope with optional model and usage.
3949
+ *
3950
+ * @remarks
3951
+ * Answers remain unknown until checked against their questions. Missing and null usage counts
3952
+ * are accepted. Extra members are ignored, and unreadable fields return false.
3953
+ *
3954
+ * @param value - The unknown response candidate
3955
+ * @returns True if the envelope fields have their wire types; false otherwise
3956
+ * @example
3957
+ * ```ts
3958
+ * isSystemOneResponse({ answers: {}, usage: { input_tokens: null } }) // true
3959
+ * ```
3960
+ */
3961
+ export declare function isSystemOneResponse(value: unknown): value is SystemOneResponse;
3962
+
3632
3963
  /**
3633
3964
  * Joins the reasoning a run's provider calls separated from the answer — the first call
3634
3965
  * seeds the accumulation, a later call appends blank-line separated so each turn's reasoning
@@ -3652,6 +3983,84 @@ export declare function isSection(value: unknown): value is Section;
3652
3983
  */
3653
3984
  export declare function joinThinking(running: string | undefined, next: string): string;
3654
3985
 
3986
+ /**
3987
+ * Reports a judge call cancelled by the caller's signal or its deadline, carrying the
3988
+ * {@link JudgeResult} merged from the calls that completed before the cancel and the
3989
+ * machine-readable `code` `'ABORT'`.
3990
+ *
3991
+ * @remarks
3992
+ * `partial` keeps the answers and the usage of every completed call, so usage reported by
3993
+ * completed calls is retained; a cancel before the first call carries an empty partial. `cause` holds the failure the
3994
+ * cancel superseded when a throw raced the abort, and is undefined when the cancel was the only
3995
+ * failure.
3996
+ */
3997
+ export declare class JudgeAbortError extends Error {
3998
+ /** Names the machine-readable condition — `'ABORT'`: a judge call cancelled mid-flight. */
3999
+ readonly code: "ABORT";
4000
+ readonly partial: JudgeResult;
4001
+ constructor(partial: JudgeResult, options?: ErrorOptions);
4002
+ }
4003
+
4004
+ /** Names one answer by its form; it stores only the distribution a server returned. */
4005
+ export declare type JudgeAnswer = ChoiceAnswer | ScoreAnswer | NoulAnswer;
4006
+
4007
+ /** Carries text or structured JSON the model reads; mirrors the TypeSafe `EntryType` without its null arm. */
4008
+ export declare type JudgeEntry = string | JSONRecord | readonly JSONValue[];
4009
+
4010
+ /**
4011
+ * Reports a coded judge failure with its HTTP status and underlying cause when available.
4012
+ *
4013
+ * @remarks
4014
+ * The judge engine throws this error for a non-OK HTTP response, a missing or unparsable
4015
+ * response body, and a request refused before inference. Concrete wire decoders also use it for
4016
+ * a response they cannot read and for a wire limit. `status` is present only for an `HTTP`
4017
+ * failure; other codes leave it undefined.
4018
+ */
4019
+ export declare class JudgeError extends Error {
4020
+ /** Names the machine-readable condition — `'HTTP'`: a non-OK response; `'PROTOCOL'`: a missing, unparsable, or unreadable response body; `'QUESTION'`: a request refused before inference. */
4021
+ readonly code: JudgeErrorCode;
4022
+ /** Holds the response status for an HTTP failure, or undefined for other codes. */
4023
+ readonly status: number | undefined;
4024
+ constructor(code: JudgeErrorCode, message: string, options?: ProviderErrorOptions);
4025
+ }
4026
+
4027
+ /** Names the machine-readable judge failure conditions. */
4028
+ export declare type JudgeErrorCode =
4029
+ /** Reports a non-OK HTTP response. */
4030
+ 'HTTP'
4031
+ /** Reports a missing or unparsable response body, or a response a wire cannot read. */
4032
+ | 'PROTOCOL'
4033
+ /** Reports a request refused before inference: an empty question map, a malformed question or state, a wire limit, or a judge configuration that would refuse every request. */
4034
+ | 'QUESTION';
4035
+
4036
+ /** Answers typed questions about one state with probabilities; the sibling of `ProviderInterface`, never a provider. */
4037
+ export declare interface JudgeInterface {
4038
+ readonly id: string;
4039
+ readonly name: string;
4040
+ /** Holds the configured model identity; the wire composes it from everything that changes an answer under one state. */
4041
+ readonly model: string;
4042
+ /** Asks every question of the request about its state and returns the merged answers. */
4043
+ ask(request: JudgeRequest, signal: AbortSignal): Promise<JudgeResult>;
4044
+ }
4045
+
4046
+ /** Names one question by its form, the protocol's type field under the fleet's named discriminant. */
4047
+ export declare type JudgeQuestion = ChoiceQuestion | ScoreQuestion | NoulQuestion;
4048
+
4049
+ /** Carries one state and the questions asked about it, keyed by caller ids the model never sees; each question is evaluated on its own. */
4050
+ export declare interface JudgeRequest {
4051
+ readonly state: JudgeEntry;
4052
+ readonly questions: Readonly<Record<string, JudgeQuestion>>;
4053
+ }
4054
+
4055
+ /** Carries the answering model, the answers keyed by question id, the refusals, and the usage the request's calls spent. */
4056
+ export declare interface JudgeResult {
4057
+ readonly model: string;
4058
+ readonly answers: Readonly<Record<string, JudgeAnswer>>;
4059
+ /** Holds each refused question by id; absent when none was refused. An id appears in answers or refusals, never both. */
4060
+ readonly refusals?: Readonly<Record<string, Refusal>>;
4061
+ readonly usage?: TokenUsage;
4062
+ }
4063
+
3655
4064
  /**
3656
4065
  * Bounds the decoded error excerpt's input in bytes — `2048`, the leading bytes of a non-OK
3657
4066
  * response body handed to the decoder before the read cancels the remainder, so a `ProviderError`
@@ -3733,6 +4142,8 @@ export declare class MemoryConversationStore implements ConversationStoreInterfa
3733
4142
  * `tool_calls` a prior generation produced, replayed back into the next request so
3734
4143
  * the model sees its own decision. A `tool` turn carries the tool's result in
3735
4144
  * `content` (the textual outcome), keyed back to the call by the conversation order.
4145
+ * For a successful tool result, a string is the content as is; any other value is
4146
+ * JSON-encoded. A failed tool result carries its error text unchanged.
3736
4147
  */
3737
4148
  export declare interface Message {
3738
4149
  readonly id: string;
@@ -3848,6 +4259,25 @@ arguments: ObjectShape<Record<never, never>, JSONShape>;
3848
4259
  images: OptionalShape<ArrayShape<StringShape>>;
3849
4260
  }, boolean | ContractShape>;
3850
4261
 
4262
+ /** Carries the probability that the answer is yes; the protocol's noul field. */
4263
+ export declare interface NoulAnswer {
4264
+ readonly form: 'noul';
4265
+ readonly noul: number;
4266
+ }
4267
+
4268
+ /** Carries what makes a noul answer true and what makes it false; an omitted side is undescribed. */
4269
+ export declare interface NoulCriteria {
4270
+ readonly true?: JudgeEntry;
4271
+ readonly false?: JudgeEntry;
4272
+ }
4273
+
4274
+ /** Asks the model whether a statement about the state holds. */
4275
+ export declare interface NoulQuestion {
4276
+ readonly form: 'noul';
4277
+ readonly instructions?: JudgeEntry;
4278
+ readonly criteria?: NoulCriteria;
4279
+ }
4280
+
3851
4281
  /**
3852
4282
  * Names the status a relay answers for a request body at or above its byte budget — `413`,
3853
4283
  * carried with no body and answered for an aborted inbound read as well.
@@ -4179,6 +4609,18 @@ export declare interface ProviderStreamOptions {
4179
4609
  readonly schema?: Readonly<Record<string, unknown>>;
4180
4610
  }
4181
4611
 
4612
+ /**
4613
+ * Projects a judge question onto the System One wire while preserving omitted members.
4614
+ *
4615
+ * @param question - The domain question to send
4616
+ * @returns The wire question with `type` replacing `form`
4617
+ * @example
4618
+ * ```ts
4619
+ * questionToSystemOne({ form: 'noul' }) // { type: 'noul' }
4620
+ * ```
4621
+ */
4622
+ export declare function questionToSystemOne(question: JudgeQuestion): SystemOneQuestion;
4623
+
4182
4624
  /**
4183
4625
  * Decodes UTF-8 chunks with a final flush and releases the stream on every exit.
4184
4626
  *
@@ -4193,6 +4635,41 @@ export declare interface ProviderStreamOptions {
4193
4635
  */
4194
4636
  export declare function readChunks(body: ReadableStream<Uint8Array>, signal?: AbortSignal): AsyncGenerator<string>;
4195
4637
 
4638
+ /**
4639
+ * Builds a request's JSON headers, awaiting the caller's header hook inside the call's
4640
+ * cancellation bound.
4641
+ *
4642
+ * @remarks
4643
+ * The headers start with `Content-Type: application/json`; each entry the hook returns is set
4644
+ * over them, so the hook overrides the content type only when it returns that header. The hook
4645
+ * receives `signal` and races its abort, and the abort listener is removed on every exit. Both
4646
+ * HTTP engines, the provider and the judge, read their headers here.
4647
+ *
4648
+ * @param hook - The caller's header hook, or undefined for the JSON content type alone
4649
+ * @param signal - The call's combined caller and deadline signal
4650
+ * @returns The request headers
4651
+ * @throws Thrown when the signal aborts before the hook settles, with the signal's reason, and
4652
+ * when the hook throws or rejects, with the hook's own failure
4653
+ * @example
4654
+ * ```ts
4655
+ * const signal = new AbortController().signal
4656
+ * const headers = await readHeaders(() => ({ authorization: 'Bearer KEY' }), signal)
4657
+ * headers.get('authorization') // 'Bearer KEY'
4658
+ * headers.get('content-type') // 'application/json'
4659
+ * ```
4660
+ */
4661
+ export declare function readHeaders(hook: ProviderOptions['headers'], signal: AbortSignal): Promise<Headers>;
4662
+
4663
+ /** Carries the measures `computeReading` derives from an answer; nothing stores them. */
4664
+ export declare interface Reading {
4665
+ /** Holds the first strictly greatest candidate in enumeration order: an option name, a level index, or true or false. */
4666
+ readonly winner: string;
4667
+ readonly probability: number;
4668
+ readonly confidence: number;
4669
+ /** Holds the expected level of a score answer; the protocol's score field. */
4670
+ readonly score?: number;
4671
+ }
4672
+
4196
4673
  /**
4197
4674
  * Reads a UTF-8 prefix of a byte stream and cancels its remainder.
4198
4675
  *
@@ -4216,6 +4693,11 @@ export declare function readChunks(body: ReadableStream<Uint8Array>, signal?: Ab
4216
4693
  */
4217
4694
  export declare function readText(body: ReadableStream<Uint8Array>, limit?: number, signal?: AbortSignal): Promise<TextRead>;
4218
4695
 
4696
+ /** Reports a question a wire could not read a candidate for; it lists the caller's keys and invents no probability. */
4697
+ export declare interface Refusal {
4698
+ readonly missing: readonly string[];
4699
+ }
4700
+
4219
4701
  /**
4220
4702
  * Names the relay's newline-delimited JSON content type — `'application/x-ndjson; charset=utf-8'`,
4221
4703
  * the header a relay response carries beside `cache-control: no-store`.
@@ -4858,6 +5340,26 @@ export declare interface ScopeManagerOptions {
4858
5340
  readonly error?: EmitterErrorHandler;
4859
5341
  }
4860
5342
 
5343
+ /** Carries a score distribution indexed by level. */
5344
+ export declare interface ScoreAnswer {
5345
+ readonly form: 'score';
5346
+ readonly probabilities: readonly number[];
5347
+ }
5348
+
5349
+ /** Lists at least two score levels from level 0 upward; null leaves a level undescribed. */
5350
+ export declare type ScoreCriteria = readonly [
5351
+ JudgeEntry | null,
5352
+ JudgeEntry | null,
5353
+ ...Array<JudgeEntry | null>
5354
+ ];
5355
+
5356
+ /** Asks the model to place the state on an ordered scale of levels. */
5357
+ export declare interface ScoreQuestion {
5358
+ readonly form: 'score';
5359
+ readonly instructions?: JudgeEntry;
5360
+ readonly criteria: ScoreCriteria;
5361
+ }
5362
+
4861
5363
  /**
4862
5364
  * Holds a slice of folded messages digested into a summary — the unit of compaction a
4863
5365
  * {@link ConversationInterface} produces when it `compact`s its live tail.
@@ -4949,6 +5451,144 @@ export declare interface StreamInterface<T, R> {
4949
5451
  */
4950
5452
  export declare function sumUsage(running: TokenUsage | undefined, next: TokenUsage): TokenUsage;
4951
5453
 
5454
+ /** Names the System One decision endpoint shared by compatible servers. */
5455
+ export declare const SYSTEM_ONE_PATH = "/v1/systemone";
5456
+
5457
+ /** Unites the TypeSafe System One answer forms. */
5458
+ export declare type SystemOneAnswer = SystemOneChoiceAnswer | SystemOneScoreAnswer | SystemOneNoulAnswer;
5459
+
5460
+ /** Transliterates a TypeSafe System One choice distribution and optional server measures. */
5461
+ export declare interface SystemOneChoiceAnswer {
5462
+ readonly type: 'choice';
5463
+ readonly probabilities: Readonly<Record<string, number>>;
5464
+ readonly choice?: string;
5465
+ readonly confidence?: number;
5466
+ }
5467
+
5468
+ /** Carries a TypeSafe System One entry, including the wire's explicit null. */
5469
+ export declare type SystemOneEntry = JudgeEntry | null;
5470
+
5471
+ /**
5472
+ * Carries judge questions over the System One protocol and derives answers from server distributions.
5473
+ *
5474
+ * @remarks
5475
+ * The caller supplies the server origin and model. Every question travels in one request.
5476
+ * Server measures are ignored; the response model is preserved.
5477
+ * `headers` supplies authentication through the shared judge engine.
5478
+ *
5479
+ * @example Asking a System One server a choice, a noul, and a score
5480
+ * ```ts
5481
+ * import { computeReading, createSystemOneJudge } from '@orkestrel/agent'
5482
+ *
5483
+ * const judge = createSystemOneJudge({ url: 'http://localhost:11434', model: 'tev1:0.8b' })
5484
+ * const result = await judge.ask(
5485
+ * {
5486
+ * state: 'Our checkout has returned 500 errors since 9am. I want a refund for today.',
5487
+ * questions: {
5488
+ * label: {
5489
+ * form: 'choice',
5490
+ * instructions: 'Which label fits this ticket?',
5491
+ * criteria: { billing: 'Payments and refunds', bug: 'Software errors', account: null },
5492
+ * },
5493
+ * refund: {
5494
+ * form: 'noul',
5495
+ * instructions: 'Does the customer ask for money back?',
5496
+ * criteria: {
5497
+ * true: 'The customer asks for a refund or for money back.',
5498
+ * false: 'The customer does not ask for money back.',
5499
+ * },
5500
+ * },
5501
+ * severity: {
5502
+ * form: 'score',
5503
+ * instructions: 'How severe is the reported issue?',
5504
+ * criteria: ['Cosmetic; no impact', 'Degraded, workaround exists', 'Blocking; no workaround'],
5505
+ * },
5506
+ * },
5507
+ * },
5508
+ * AbortSignal.timeout(30_000),
5509
+ * )
5510
+ * const readings = Object.fromEntries(
5511
+ * Object.entries(result.answers).map(([id, answer]) => [id, computeReading(answer)]),
5512
+ * )
5513
+ * result.model // 'tev1:0.8b' — the model the server named
5514
+ * result.usage // { prompt: 975, completion: 4, total: 979 }
5515
+ * readings.label // { winner: 'bug', probability: 0.9691, confidence: 0.9536 } to four decimals
5516
+ * readings.refund // { winner: 'true', probability: 0.9979, confidence: 0.9958 } to four decimals
5517
+ * readings.severity // { winner: '1', probability: 0.9494, confidence: 0.9241, score: 0.9919 } to four decimals
5518
+ * ```
5519
+ */
5520
+ export declare class SystemOneJudge extends AgentJudge {
5521
+ constructor(options: SystemOneJudgeOptions);
5522
+ /** Identifies the System One protocol. */
5523
+ readonly name = "systemone";
5524
+ /**
5525
+ * Projects the state and questions onto the System One request with the configured model.
5526
+ *
5527
+ * @param request - The state and questions keyed by caller id
5528
+ * @returns The System One request body
5529
+ */
5530
+ body(request: JudgeRequest): SystemOneRequest;
5531
+ /**
5532
+ * Decodes requested System One answers and reports the server model and available usage.
5533
+ *
5534
+ * @param value - The parsed response body
5535
+ * @param request - The questions defining the expected answers
5536
+ * @returns The decoded distributions, model, and available usage
5537
+ * @throws JudgeError Thrown with code `PROTOCOL` for a malformed envelope or an invalid answer naming its question id
5538
+ */
5539
+ read(value: unknown, request: JudgeRequest): JudgeResult;
5540
+ }
5541
+
5542
+ /** Configures the System One server, model, transport, authentication, and deadline. */
5543
+ export declare interface SystemOneJudgeOptions extends Pick<ProviderOptions, 'timeout' | 'fetch' | 'headers'> {
5544
+ /** Holds the server origin without a path. */
5545
+ readonly url: string;
5546
+ readonly model: string;
5547
+ }
5548
+
5549
+ /** Transliterates a TypeSafe System One yes probability and optional server confidence. */
5550
+ export declare interface SystemOneNoulAnswer {
5551
+ readonly type: 'noul';
5552
+ readonly noul: number;
5553
+ readonly confidence?: number;
5554
+ }
5555
+
5556
+ /** Transliterates a TypeSafe System One question with its protocol discriminant and criteria. */
5557
+ export declare interface SystemOneQuestion {
5558
+ readonly type: 'choice' | 'score' | 'noul';
5559
+ readonly instructions?: SystemOneEntry;
5560
+ readonly criteria?: ChoiceCriteria | ScoreCriteria | NoulCriteria | null;
5561
+ }
5562
+
5563
+ /** Transliterates the TypeSafe System One request body. */
5564
+ export declare interface SystemOneRequest {
5565
+ readonly state: SystemOneEntry;
5566
+ readonly model: string;
5567
+ readonly questions: Readonly<Record<string, SystemOneQuestion>>;
5568
+ }
5569
+
5570
+ /** Transliterates the System One response envelope before question-specific answer validation. */
5571
+ export declare interface SystemOneResponse {
5572
+ readonly model?: string;
5573
+ readonly answers: Readonly<Record<string, unknown>>;
5574
+ readonly usage?: SystemOneUsage;
5575
+ }
5576
+
5577
+ /** Accepts System One score probabilities and legends as maps or llama.cpp arrays. */
5578
+ export declare interface SystemOneScoreAnswer {
5579
+ readonly type: 'score';
5580
+ readonly probabilities: Readonly<Record<string, number>> | readonly number[];
5581
+ readonly score?: number;
5582
+ readonly legend?: Readonly<Record<string, SystemOneEntry>> | readonly SystemOneEntry[];
5583
+ readonly confidence?: number;
5584
+ }
5585
+
5586
+ /** Transliterates TypeSafe System One token counts with missing or null counts permitted. */
5587
+ export declare interface SystemOneUsage {
5588
+ readonly input_tokens?: number | null;
5589
+ readonly output_tokens?: number | null;
5590
+ }
5591
+
4952
5592
  /** Carries a decoded stream prefix and whether the stream ended within its byte budget. */
4953
5593
  export declare interface TextRead {
4954
5594
  readonly text: string;