nexus-ai-pro 2.18.0 → 2.20.2
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/API_STABILITY.md +64 -1
- package/CHANGELOG.md +113 -1
- package/README.md +11 -3
- package/dist/agent/capabilities.d.ts +3 -0
- package/dist/agent/openapi.d.ts +139 -0
- package/dist/agent/openapi.js +455 -0
- package/dist/capabilities/negotiate.js +5 -0
- package/dist/decisions/completion.d.ts +48 -0
- package/dist/decisions/completion.js +195 -0
- package/dist/decisions/core.d.ts +525 -0
- package/dist/decisions/core.js +493 -0
- package/dist/decisions/index.d.ts +9 -0
- package/dist/decisions/index.js +3 -0
- package/dist/decisions/integrations.d.ts +142 -0
- package/dist/decisions/integrations.js +170 -0
- package/dist/decisions/mock.d.ts +32 -0
- package/dist/decisions/mock.js +47 -0
- package/dist/decisions/typesafe.d.ts +45 -0
- package/dist/decisions/typesafe.js +144 -0
- package/dist/evaluate/emulate.d.ts +73 -6
- package/dist/evaluate/emulate.js +114 -12
- package/dist/index.d.ts +1 -1
- package/dist/optimizer/densifier.js +20 -11
- package/dist/protocols/a2a-grpc.js +1 -1
- package/dist/protocols/a2a-proto.d.ts +2 -2
- package/dist/protocols/a2a-proto.js +76 -1
- package/dist/protocols/a2a.d.ts +121 -0
- package/dist/protocols/a2a.js +88 -29
- package/dist/providers/google.js +28 -0
- package/dist/providers/ollama.js +23 -0
- package/dist/providers/openai.d.ts +6 -0
- package/dist/providers/openai.js +26 -0
- package/dist/types/lifecycle.d.ts +1 -1
- package/dist/types/messages.d.ts +9 -0
- package/dist/types/providers.d.ts +2 -0
- package/dist/types/response.d.ts +17 -0
- package/dist/types/tracing.d.ts +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/workflow/chains.d.ts +13 -1
- package/dist/workflow/chains.js +40 -0
- package/dist-cjs/agent/capabilities.d.ts +3 -0
- package/dist-cjs/agent/openapi.d.ts +139 -0
- package/dist-cjs/agent/openapi.js +461 -0
- package/dist-cjs/capabilities/negotiate.js +5 -0
- package/dist-cjs/decisions/completion.d.ts +48 -0
- package/dist-cjs/decisions/completion.js +198 -0
- package/dist-cjs/decisions/core.d.ts +525 -0
- package/dist-cjs/decisions/core.js +509 -0
- package/dist-cjs/decisions/index.d.ts +9 -0
- package/dist-cjs/decisions/index.js +27 -0
- package/dist-cjs/decisions/integrations.d.ts +142 -0
- package/dist-cjs/decisions/integrations.js +178 -0
- package/dist-cjs/decisions/mock.d.ts +32 -0
- package/dist-cjs/decisions/mock.js +51 -0
- package/dist-cjs/decisions/typesafe.d.ts +45 -0
- package/dist-cjs/decisions/typesafe.js +148 -0
- package/dist-cjs/evaluate/emulate.d.ts +73 -6
- package/dist-cjs/evaluate/emulate.js +116 -13
- package/dist-cjs/index.d.ts +1 -1
- package/dist-cjs/optimizer/densifier.js +20 -11
- package/dist-cjs/protocols/a2a-grpc.js +1 -1
- package/dist-cjs/protocols/a2a-proto.d.ts +2 -2
- package/dist-cjs/protocols/a2a-proto.js +76 -1
- package/dist-cjs/protocols/a2a.d.ts +121 -0
- package/dist-cjs/protocols/a2a.js +88 -29
- package/dist-cjs/providers/google.js +28 -0
- package/dist-cjs/providers/ollama.js +23 -0
- package/dist-cjs/providers/openai.d.ts +6 -0
- package/dist-cjs/providers/openai.js +26 -0
- package/dist-cjs/types/lifecycle.d.ts +1 -1
- package/dist-cjs/types/messages.d.ts +9 -0
- package/dist-cjs/types/providers.d.ts +2 -0
- package/dist-cjs/types/response.d.ts +17 -0
- package/dist-cjs/types/tracing.d.ts +1 -1
- package/dist-cjs/version.d.ts +1 -1
- package/dist-cjs/version.js +1 -1
- package/dist-cjs/workflow/chains.d.ts +13 -1
- package/dist-cjs/workflow/chains.js +40 -0
- package/llms.txt +2 -1
- package/package.json +54 -2
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed decisions: questions a model answers with a yes or no, a choice, or a score, each with its
|
|
3
|
+
* probabilities and a confidence read from them, so software acts on an answer without parsing text.
|
|
4
|
+
*/
|
|
5
|
+
import type { CacheAdapter } from '../cache/adapters.js';
|
|
6
|
+
import type { OperationLifecycleLike, ProviderCallContext } from '../types/lifecycle.js';
|
|
7
|
+
import type { ResponseCost } from '../types/response.js';
|
|
8
|
+
import type { RunKind } from '../types/tracing.js';
|
|
9
|
+
/** The kinds of question there are: a yes or no, a choice among options, or a score on levels. */
|
|
10
|
+
export type DecisionQuestionType = 'yes-no' | 'choice' | 'score';
|
|
11
|
+
/** A question answered yes or no, with the probability that it is yes. */
|
|
12
|
+
export interface YesNoQuestion {
|
|
13
|
+
/** Discriminates the question. */
|
|
14
|
+
type: 'yes-no';
|
|
15
|
+
/** The question, or a statement that is true or false, such as "The customer asks for a refund." */
|
|
16
|
+
question?: string;
|
|
17
|
+
/** What counts as yes and as no, when the question alone leaves it open. */
|
|
18
|
+
criteria?: {
|
|
19
|
+
yes?: string;
|
|
20
|
+
no?: string;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** A question answered by one of its options, with each option's probability. */
|
|
24
|
+
export interface ChoiceQuestion<K extends string = string> {
|
|
25
|
+
/** Discriminates the question. */
|
|
26
|
+
type: 'choice';
|
|
27
|
+
/** The question, such as "Which team should handle this?". */
|
|
28
|
+
question?: string;
|
|
29
|
+
/** The options, by name, each with what it means, or only their names. Up to 255. */
|
|
30
|
+
options: readonly K[] | Readonly<Record<K, string>>;
|
|
31
|
+
}
|
|
32
|
+
/** A question answered on an ordered rubric: level 0 first, with each level's probability. */
|
|
33
|
+
export interface ScoreQuestion {
|
|
34
|
+
/** Discriminates the question. */
|
|
35
|
+
type: 'score';
|
|
36
|
+
/** The question, such as "How urgent is this?". */
|
|
37
|
+
question?: string;
|
|
38
|
+
/** What each level means, lowest first. Two to ten. */
|
|
39
|
+
levels: readonly string[];
|
|
40
|
+
}
|
|
41
|
+
/** Any question. */
|
|
42
|
+
export type DecisionQuestion = YesNoQuestion | ChoiceQuestion | ScoreQuestion;
|
|
43
|
+
/** Questions by the names their answers come back under. */
|
|
44
|
+
export type DecisionQuestions = Record<string, DecisionQuestion>;
|
|
45
|
+
/** What every question of a decision is about: text, or any JSON value. */
|
|
46
|
+
export type DecisionState = string | Record<string, unknown> | readonly unknown[];
|
|
47
|
+
/** A yes-or-no question's answer. */
|
|
48
|
+
export interface YesNoAnswer {
|
|
49
|
+
/** Discriminates the answer. */
|
|
50
|
+
type: 'yes-no';
|
|
51
|
+
/** Whether it is yes: the probability is at least one half. */
|
|
52
|
+
yes: boolean;
|
|
53
|
+
/** The probability that it is yes, from 0 to 1. */
|
|
54
|
+
probability: number;
|
|
55
|
+
/** How far the probability is from an even chance, from 0 to 1: `|2p - 1|`. */
|
|
56
|
+
confidence: number;
|
|
57
|
+
}
|
|
58
|
+
/** A choice question's answer. */
|
|
59
|
+
export interface ChoiceAnswer<K extends string = string> {
|
|
60
|
+
/** Discriminates the answer. */
|
|
61
|
+
type: 'choice';
|
|
62
|
+
/** The most probable option; of equally probable ones, the first given. */
|
|
63
|
+
choice: K;
|
|
64
|
+
/** Every option's probability, summing to 1. */
|
|
65
|
+
probabilities: Record<K, number>;
|
|
66
|
+
/**
|
|
67
|
+
* How far the top probability is above an even split, from 0 to 1: `(pmax - 1/n) / (1 - 1/n)` of
|
|
68
|
+
* `n` options.
|
|
69
|
+
*/
|
|
70
|
+
confidence: number;
|
|
71
|
+
}
|
|
72
|
+
/** A score question's answer. */
|
|
73
|
+
export interface ScoreAnswer {
|
|
74
|
+
/** Discriminates the answer. */
|
|
75
|
+
type: 'score';
|
|
76
|
+
/** The probability-weighted level, which may fall between two. */
|
|
77
|
+
score: number;
|
|
78
|
+
/** The most probable level; of equally probable ones, the lowest. */
|
|
79
|
+
level: number;
|
|
80
|
+
/** Each level's probability, lowest first, summing to 1. */
|
|
81
|
+
probabilities: number[];
|
|
82
|
+
/**
|
|
83
|
+
* How close the probability sits around the most probable level, from 0 to 1: one less the
|
|
84
|
+
* probability-weighted distance from it, over that distance for an even spread, and 0 at an even
|
|
85
|
+
* spread or wider.
|
|
86
|
+
*/
|
|
87
|
+
confidence: number;
|
|
88
|
+
}
|
|
89
|
+
/** Any answer. */
|
|
90
|
+
export type DecisionAnswer = YesNoAnswer | ChoiceAnswer | ScoreAnswer;
|
|
91
|
+
/** The options of a choice question, as a union of their names. */
|
|
92
|
+
type OptionsOf<O> = O extends readonly (infer K)[] ? K : keyof O;
|
|
93
|
+
/** The answer a question gets: a choice's answer names only its options. */
|
|
94
|
+
export type AnswerOf<Q> = Q extends YesNoQuestion ? YesNoAnswer : Q extends {
|
|
95
|
+
type: 'choice';
|
|
96
|
+
options: infer O;
|
|
97
|
+
} ? ChoiceAnswer<Extract<OptionsOf<O>, string>> : Q extends ScoreQuestion ? ScoreAnswer : DecisionAnswer;
|
|
98
|
+
/** Answers by their questions' names, each typed by its question. */
|
|
99
|
+
export type DecisionAnswers<Q extends DecisionQuestions> = {
|
|
100
|
+
[K in keyof Q]: AnswerOf<Q[K]>;
|
|
101
|
+
};
|
|
102
|
+
/** A decision: questions about one state, answered together and each in isolation. */
|
|
103
|
+
export interface DecisionRequest<Q extends DecisionQuestions = DecisionQuestions> {
|
|
104
|
+
/** What every question is about. */
|
|
105
|
+
state: DecisionState;
|
|
106
|
+
/** The questions, by the names their answers come back under. */
|
|
107
|
+
questions: Q;
|
|
108
|
+
/** The provider to ask. Defaults to the manager's default, then the first registered. */
|
|
109
|
+
provider?: string;
|
|
110
|
+
/** The model to ask. Defaults to the provider's own. */
|
|
111
|
+
model?: string;
|
|
112
|
+
/** The tenant it is for: its credentials, regions, and budget apply. */
|
|
113
|
+
tenantId?: string;
|
|
114
|
+
/** Stops the call. */
|
|
115
|
+
signal?: AbortSignal;
|
|
116
|
+
/** Labels for the trace and the audit log. Never include the state's private data. */
|
|
117
|
+
metadata?: Record<string, unknown>;
|
|
118
|
+
/** `false` asks the provider even when the cache holds an answer. */
|
|
119
|
+
cache?: boolean;
|
|
120
|
+
}
|
|
121
|
+
/** What a decision used, where the provider reports it. */
|
|
122
|
+
export interface DecisionUsage {
|
|
123
|
+
/** Input tokens. */
|
|
124
|
+
inputTokens?: number;
|
|
125
|
+
/** Output tokens. */
|
|
126
|
+
outputTokens?: number;
|
|
127
|
+
/** Model calls made, for a provider that asks a completion model. */
|
|
128
|
+
calls?: number;
|
|
129
|
+
}
|
|
130
|
+
/** A decision's answers, and who gave them. */
|
|
131
|
+
export interface DecisionResult<Q extends DecisionQuestions = DecisionQuestions> {
|
|
132
|
+
/** The answers, by question name. */
|
|
133
|
+
answers: DecisionAnswers<Q>;
|
|
134
|
+
/** The provider that answered. */
|
|
135
|
+
provider: string;
|
|
136
|
+
/** The model that answered, as the provider names it. */
|
|
137
|
+
model: string;
|
|
138
|
+
/** What it used. */
|
|
139
|
+
usage?: DecisionUsage;
|
|
140
|
+
/** What it cost, when the provider or the manager's prices price it. */
|
|
141
|
+
cost?: ResponseCost;
|
|
142
|
+
/** How long it took, in milliseconds. */
|
|
143
|
+
latencyMs: number;
|
|
144
|
+
/** True when the answers came from the cache. */
|
|
145
|
+
cached?: boolean;
|
|
146
|
+
}
|
|
147
|
+
/** A question as a provider receives it: a choice's options in order, each with its description. */
|
|
148
|
+
export type DecisionProviderQuestion = YesNoQuestion | {
|
|
149
|
+
type: 'choice';
|
|
150
|
+
question?: string;
|
|
151
|
+
options: Array<{
|
|
152
|
+
name: string;
|
|
153
|
+
description?: string;
|
|
154
|
+
}>;
|
|
155
|
+
} | ScoreQuestion;
|
|
156
|
+
/** What a provider is asked. */
|
|
157
|
+
export interface DecisionProviderRequest {
|
|
158
|
+
/** The model to ask. */
|
|
159
|
+
model: string;
|
|
160
|
+
/** What every question is about. */
|
|
161
|
+
state: DecisionState;
|
|
162
|
+
/** The questions, by name. */
|
|
163
|
+
questions: Record<string, DecisionProviderQuestion>;
|
|
164
|
+
}
|
|
165
|
+
/** A provider's probabilities for one question, from which the answer and its confidence are read. */
|
|
166
|
+
export type DecisionDistribution = {
|
|
167
|
+
type: 'yes-no';
|
|
168
|
+
yes: number;
|
|
169
|
+
} | {
|
|
170
|
+
type: 'choice';
|
|
171
|
+
probabilities: Record<string, number>;
|
|
172
|
+
} | {
|
|
173
|
+
type: 'score';
|
|
174
|
+
probabilities: readonly number[];
|
|
175
|
+
};
|
|
176
|
+
/** What a provider answers. */
|
|
177
|
+
export interface DecisionProviderResult {
|
|
178
|
+
/** The model that answered, when it differs from the one asked, as an alias resolves. */
|
|
179
|
+
model?: string;
|
|
180
|
+
/** Each question's probabilities, by name. */
|
|
181
|
+
distributions: Record<string, DecisionDistribution>;
|
|
182
|
+
/** What it used. */
|
|
183
|
+
usage?: DecisionUsage;
|
|
184
|
+
/** What it cost, when the provider reports it. */
|
|
185
|
+
cost?: ResponseCost;
|
|
186
|
+
}
|
|
187
|
+
/** What a decision provider takes. */
|
|
188
|
+
export interface DecisionProviderCapabilities {
|
|
189
|
+
/** The kinds of question it answers. */
|
|
190
|
+
types: readonly DecisionQuestionType[];
|
|
191
|
+
/** Most options a choice may have. */
|
|
192
|
+
maxOptions: number;
|
|
193
|
+
/** Most levels a score may have. */
|
|
194
|
+
maxLevels: number;
|
|
195
|
+
/** Most questions one decision may ask, when it has a limit. */
|
|
196
|
+
maxQuestions?: number;
|
|
197
|
+
/** The model asked when a request names none. */
|
|
198
|
+
defaultModel: string;
|
|
199
|
+
/** The models it serves, when it lists them. */
|
|
200
|
+
models?: readonly string[];
|
|
201
|
+
}
|
|
202
|
+
/** Identifies a decision provider and what it takes. */
|
|
203
|
+
export interface DecisionProviderInfo {
|
|
204
|
+
/** Its name, which results carry. */
|
|
205
|
+
name: string;
|
|
206
|
+
/** What it takes. */
|
|
207
|
+
capabilities: DecisionProviderCapabilities;
|
|
208
|
+
/**
|
|
209
|
+
* True for a provider whose calls are charged where they are made, such as one asking a client's
|
|
210
|
+
* own completions: its decisions report their cost, and hold and charge nothing to the budget again.
|
|
211
|
+
*/
|
|
212
|
+
chargedByItsCalls?: boolean;
|
|
213
|
+
}
|
|
214
|
+
/** What every decision call receives: how to stop, the request's id, and the tenant's credential. */
|
|
215
|
+
export interface DecisionCallContext {
|
|
216
|
+
/** Stops the call. */
|
|
217
|
+
signal: AbortSignal;
|
|
218
|
+
/** The request's id, for logs and traces. */
|
|
219
|
+
requestId: string;
|
|
220
|
+
/** The tenant it is for. */
|
|
221
|
+
tenantId?: string;
|
|
222
|
+
/**
|
|
223
|
+
* The tenant's credential for a provider, with a credential resolver: an adapter sends it in place
|
|
224
|
+
* of the key it was configured with.
|
|
225
|
+
*/
|
|
226
|
+
credential?: ProviderCallContext['credential'];
|
|
227
|
+
}
|
|
228
|
+
/** A backend that answers decisions: a decision model, a completion model, or a stand-in. */
|
|
229
|
+
export interface DecisionProvider {
|
|
230
|
+
/** What it is and takes. */
|
|
231
|
+
readonly info: DecisionProviderInfo;
|
|
232
|
+
/** Answers every question of a decision. */
|
|
233
|
+
decide(request: DecisionProviderRequest, context: DecisionCallContext): Promise<DecisionProviderResult>;
|
|
234
|
+
}
|
|
235
|
+
/** Why a decision failed. */
|
|
236
|
+
export type DecisionErrorCode = 'DECISION_NO_PROVIDER' | 'DECISION_UNSUPPORTED' | 'DECISION_INVALID' | 'DECISION_FAILED';
|
|
237
|
+
/** Raised for a decision no provider can serve, a question that is malformed, or a provider's failure. */
|
|
238
|
+
export declare class DecisionError extends Error {
|
|
239
|
+
/**
|
|
240
|
+
* A stable code: `DECISION_NO_PROVIDER`, `DECISION_UNSUPPORTED` (a provider cannot take the
|
|
241
|
+
* question), `DECISION_INVALID` (the request, or an answer, is malformed), or `DECISION_FAILED`.
|
|
242
|
+
*/
|
|
243
|
+
readonly code: DecisionErrorCode;
|
|
244
|
+
/** Whether asking again, or another provider, may help: a timeout or an overloaded service may. */
|
|
245
|
+
readonly retryable: boolean;
|
|
246
|
+
/** The HTTP status a provider answered with, when it did. */
|
|
247
|
+
readonly status?: number | undefined;
|
|
248
|
+
constructor(message: string,
|
|
249
|
+
/**
|
|
250
|
+
* A stable code: `DECISION_NO_PROVIDER`, `DECISION_UNSUPPORTED` (a provider cannot take the
|
|
251
|
+
* question), `DECISION_INVALID` (the request, or an answer, is malformed), or `DECISION_FAILED`.
|
|
252
|
+
*/
|
|
253
|
+
code: DecisionErrorCode,
|
|
254
|
+
/** Whether asking again, or another provider, may help: a timeout or an overloaded service may. */
|
|
255
|
+
retryable?: boolean,
|
|
256
|
+
/** The HTTP status a provider answered with, when it did. */
|
|
257
|
+
status?: number | undefined);
|
|
258
|
+
}
|
|
259
|
+
/** A yes-or-no answer's confidence: how far its probability is from an even chance, `|2p - 1|`. */
|
|
260
|
+
export declare function yesNoConfidence(probability: number): number;
|
|
261
|
+
/**
|
|
262
|
+
* A choice's confidence, from its options' probabilities: how far the top one is above an even split,
|
|
263
|
+
* `(pmax - 1/n) / (1 - 1/n)`. One option is certain.
|
|
264
|
+
*/
|
|
265
|
+
export declare function choiceConfidence(probabilities: readonly number[]): number;
|
|
266
|
+
/**
|
|
267
|
+
* A score's confidence, from its levels' probabilities: one less the probability-weighted distance
|
|
268
|
+
* from the most probable level, over that distance for an even spread, and 0 at an even spread or
|
|
269
|
+
* wider. One level is certain.
|
|
270
|
+
*/
|
|
271
|
+
export declare function scoreConfidence(probabilities: readonly number[]): number;
|
|
272
|
+
/**
|
|
273
|
+
* An answer from a provider's probabilities for one question: normalized to sum to 1, with its
|
|
274
|
+
* confidence read the same way whichever provider gave them. Throws `DecisionError`
|
|
275
|
+
* `DECISION_INVALID` for probabilities that do not fit the question.
|
|
276
|
+
*/
|
|
277
|
+
export declare function decisionAnswer(question: DecisionProviderQuestion, distribution: DecisionDistribution): DecisionAnswer;
|
|
278
|
+
/** What to do with an answer: act on it, have a person review it, or neither. */
|
|
279
|
+
export type GateOutcome = 'act' | 'review' | 'abstain';
|
|
280
|
+
/** The confidences a gate acts at, and has reviewed at. */
|
|
281
|
+
export interface GateThresholds {
|
|
282
|
+
/** At or above this confidence, act. */
|
|
283
|
+
act: number;
|
|
284
|
+
/** At or above this, and below `act`, a person reviews it; below it, abstain. Defaults to 0. */
|
|
285
|
+
review?: number;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Turns an answer's confidence into what to do: act at or above `act`, have a person review it at or
|
|
289
|
+
* above `review`, and abstain below. Set the thresholds by risk, and choose them with `calibrate()`
|
|
290
|
+
* on the application's own labelled examples.
|
|
291
|
+
*/
|
|
292
|
+
export declare function gate(answer: {
|
|
293
|
+
confidence: number;
|
|
294
|
+
}, thresholds: GateThresholds): GateOutcome;
|
|
295
|
+
/** One answer whose right answer is known, and whether it was right. */
|
|
296
|
+
export interface CalibrationSample {
|
|
297
|
+
/** The answer. */
|
|
298
|
+
answer: DecisionAnswer;
|
|
299
|
+
/** Whether it was right, as `answeredRight()` reads it. */
|
|
300
|
+
correct: boolean;
|
|
301
|
+
}
|
|
302
|
+
/** Answers that gave their own answer a probability in one range, and how often they were right. */
|
|
303
|
+
export interface CalibrationBin {
|
|
304
|
+
/** The range's lower bound, included. */
|
|
305
|
+
from: number;
|
|
306
|
+
/** Its upper bound, excluded, except for the last range's 1. */
|
|
307
|
+
to: number;
|
|
308
|
+
/** How many answers fell in it. */
|
|
309
|
+
count: number;
|
|
310
|
+
/** The mean probability they gave their own answers. */
|
|
311
|
+
probability: number;
|
|
312
|
+
/** The share of them that were right. */
|
|
313
|
+
accuracy: number;
|
|
314
|
+
}
|
|
315
|
+
/** Options for `calibrate()`. */
|
|
316
|
+
export interface CalibrateOptions {
|
|
317
|
+
/** How many equal ranges of probability to count in. Defaults to 10. */
|
|
318
|
+
bins?: number;
|
|
319
|
+
/** The accuracy the threshold must hold: the share of answers at or above it that are right. */
|
|
320
|
+
targetAccuracy?: number;
|
|
321
|
+
/** The fewest answers the threshold must keep, so one lucky answer does not choose it. Defaults to 10. */
|
|
322
|
+
minSamples?: number;
|
|
323
|
+
}
|
|
324
|
+
/** How well answers' probabilities tracked how often they were right, and the threshold that holds the target. */
|
|
325
|
+
export interface CalibrationReport {
|
|
326
|
+
/** How many answers were counted. */
|
|
327
|
+
samples: number;
|
|
328
|
+
/** The share of all of them that were right. */
|
|
329
|
+
accuracy: number;
|
|
330
|
+
/**
|
|
331
|
+
* The expected calibration error: the bins' gaps between the probability answers gave themselves and
|
|
332
|
+
* how often they were right, weighted by their counts. 0 when answers are as often right as they say.
|
|
333
|
+
*/
|
|
334
|
+
expectedCalibrationError: number;
|
|
335
|
+
/** The answers by range of probability, empty ranges left out. */
|
|
336
|
+
bins: CalibrationBin[];
|
|
337
|
+
/**
|
|
338
|
+
* The lowest confidence at which the answers at or above it reach `targetAccuracy`, keeping at least
|
|
339
|
+
* `minSamples`: the `act` threshold for a gate. Absent when no threshold reaches it.
|
|
340
|
+
*/
|
|
341
|
+
threshold?: number;
|
|
342
|
+
/** The share of answers at or above `threshold`: how much a gate there acts on. */
|
|
343
|
+
coverage?: number;
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* The probability an answer gives its own answer: a yes or no's `max(p, 1 - p)`, a choice's for its
|
|
347
|
+
* option, and a score's for its level. Its confidence measures how far that is above an even spread.
|
|
348
|
+
*/
|
|
349
|
+
export declare function answerProbability(answer: DecisionAnswer): number;
|
|
350
|
+
/**
|
|
351
|
+
* Measures how well answers' probabilities track how often they are right, and, with
|
|
352
|
+
* `targetAccuracy`, chooses the gate threshold that holds it. Answers are calibrated when those that
|
|
353
|
+
* give their answer 0.8 are right about 80% of the time. The threshold is on confidence, which gates
|
|
354
|
+
* act on: it holds on answers like these, not on a different kind of question or state.
|
|
355
|
+
*/
|
|
356
|
+
export declare function calibrate(samples: readonly CalibrationSample[], options?: CalibrateOptions): CalibrationReport;
|
|
357
|
+
/**
|
|
358
|
+
* Whether an answer is right: a yes-or-no or a choice by its answer, and a score by its most probable
|
|
359
|
+
* level, or by its weighted score within `tolerance` levels when one is given.
|
|
360
|
+
*/
|
|
361
|
+
export declare function answeredRight(answer: DecisionAnswer, expected: boolean | string | number, options?: {
|
|
362
|
+
tolerance?: number;
|
|
363
|
+
}): boolean;
|
|
364
|
+
/** A decision model's price, in US dollars per million tokens. */
|
|
365
|
+
export interface DecisionPrice {
|
|
366
|
+
/** Per million input tokens. */
|
|
367
|
+
inputPerMillion: number;
|
|
368
|
+
/** Per million output tokens. Defaults to 0. */
|
|
369
|
+
outputPerMillion?: number;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Bundled decision prices, by model name or a prefix of it: TypeSafe AI's Jev at its published
|
|
373
|
+
* price, $42 per billion input tokens, with output free, as of 2026-10-24.
|
|
374
|
+
*/
|
|
375
|
+
export declare const DECISION_PRICES: Readonly<Record<string, DecisionPrice>>;
|
|
376
|
+
/**
|
|
377
|
+
* What a decision cost, from its usage and the model's price; `undefined` for a model without a price
|
|
378
|
+
* or usage without tokens, never zero.
|
|
379
|
+
*/
|
|
380
|
+
export declare function priceDecision(model: string, usage: DecisionUsage | undefined, prices?: Readonly<Record<string, DecisionPrice>>): ResponseCost | undefined;
|
|
381
|
+
/** A decision's run in a trace, as decisions finish it. */
|
|
382
|
+
export interface DecisionRunLike {
|
|
383
|
+
/** Finishes the run with its answers, usage, and cost, or its error. */
|
|
384
|
+
finish(options?: {
|
|
385
|
+
outputs?: unknown;
|
|
386
|
+
error?: unknown;
|
|
387
|
+
usage?: Record<string, number>;
|
|
388
|
+
cost?: number;
|
|
389
|
+
model?: string;
|
|
390
|
+
provider?: string;
|
|
391
|
+
}): Promise<void>;
|
|
392
|
+
}
|
|
393
|
+
/** A tracer, or the part of one decisions use: a run per decision, with the provider's calls inside it. */
|
|
394
|
+
export interface DecisionTracerLike {
|
|
395
|
+
/** Starts a run. */
|
|
396
|
+
startRun(options: {
|
|
397
|
+
name: string;
|
|
398
|
+
kind?: RunKind;
|
|
399
|
+
inputs?: unknown;
|
|
400
|
+
metadata?: Record<string, unknown>;
|
|
401
|
+
model?: string;
|
|
402
|
+
provider?: string;
|
|
403
|
+
}): DecisionRunLike;
|
|
404
|
+
/** Runs `fn` with the run in scope, so a model call inside it, through a traced client, nests beneath. */
|
|
405
|
+
within<T>(handle: DecisionRunLike, fn: () => T): T;
|
|
406
|
+
}
|
|
407
|
+
/** Configuration for `DecisionManager`. */
|
|
408
|
+
export interface DecisionConfig {
|
|
409
|
+
/** Providers, by name. */
|
|
410
|
+
providers?: Record<string, DecisionProvider>;
|
|
411
|
+
/** The provider a request that names none goes to. Otherwise the first registered. */
|
|
412
|
+
defaultProvider?: string;
|
|
413
|
+
/**
|
|
414
|
+
* Providers to ask, in order, when the one asked fails in a way another may not: a timeout, a
|
|
415
|
+
* refused connection, or an overloaded service. A malformed request fails at once.
|
|
416
|
+
*/
|
|
417
|
+
fallbacks?: readonly string[];
|
|
418
|
+
/** Prices by model or prefix, beside the bundled ones. */
|
|
419
|
+
prices?: Readonly<Record<string, DecisionPrice>>;
|
|
420
|
+
/**
|
|
421
|
+
* Keeps each decision's answers, so the same questions about the same state are answered once: a
|
|
422
|
+
* `CacheAdapter`, such as the memory, Redis, or SQLite one, and how long they keep (a day).
|
|
423
|
+
*/
|
|
424
|
+
cache?: {
|
|
425
|
+
store: CacheAdapter<CachedDecision>;
|
|
426
|
+
ttlSeconds?: number;
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
/** A decision's answers as the cache keeps them. */
|
|
430
|
+
export interface CachedDecision {
|
|
431
|
+
/** The answers, by question name. */
|
|
432
|
+
answers: Record<string, DecisionAnswer>;
|
|
433
|
+
/** The provider that answered. */
|
|
434
|
+
provider: string;
|
|
435
|
+
/** The model that answered. */
|
|
436
|
+
model: string;
|
|
437
|
+
/** What it used. */
|
|
438
|
+
usage?: DecisionUsage;
|
|
439
|
+
}
|
|
440
|
+
/** What the manager runs on. */
|
|
441
|
+
export interface DecisionRuntime {
|
|
442
|
+
/**
|
|
443
|
+
* Runs every decision as one operation of a client's lifecycle, family `decision`: pass
|
|
444
|
+
* `ai.lifecycle`, so it is authorized, budgeted, rate limited, and audited, on the tenant's key.
|
|
445
|
+
*/
|
|
446
|
+
lifecycle?: OperationLifecycleLike;
|
|
447
|
+
/** Records each decision as a run of kind `decision`, with its answers, usage, and cost. */
|
|
448
|
+
tracer?: DecisionTracerLike;
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Decisions behind one handle: each goes to the provider named, the default, or the first
|
|
452
|
+
* registered, with failover to `fallbacks`, a cache, and every call inside a client's lifecycle.
|
|
453
|
+
*
|
|
454
|
+
* ```ts
|
|
455
|
+
* const decisions = new DecisionManager(
|
|
456
|
+
* { providers: { jev: new TypeSafeDecisionProvider({ apiKey }) }, fallbacks: ['gpt'] },
|
|
457
|
+
* { lifecycle: ai.lifecycle },
|
|
458
|
+
* );
|
|
459
|
+
* const { answers } = await decisions.decide({
|
|
460
|
+
* state: ticket,
|
|
461
|
+
* questions: {
|
|
462
|
+
* team: { type: 'choice', options: { billing: 'Charges and invoices', technical: 'Errors and outages' } },
|
|
463
|
+
* urgent: { type: 'yes-no', question: 'Does the customer need an answer today?' },
|
|
464
|
+
* },
|
|
465
|
+
* });
|
|
466
|
+
* if (gate(answers.team, { act: 0.8 }) === 'act') route(answers.team.choice);
|
|
467
|
+
* ```
|
|
468
|
+
*/
|
|
469
|
+
export declare class DecisionManager {
|
|
470
|
+
private readonly config;
|
|
471
|
+
private readonly runtime;
|
|
472
|
+
private readonly providers;
|
|
473
|
+
constructor(config?: DecisionConfig, runtime?: DecisionRuntime);
|
|
474
|
+
/** Registers a provider under a name. Returns the manager, for chaining. */
|
|
475
|
+
registerDecisionProvider(name: string, provider: DecisionProvider): this;
|
|
476
|
+
/** Whether a provider is registered. */
|
|
477
|
+
hasDecisionProvider(name: string): boolean;
|
|
478
|
+
/** Every registered provider's name. */
|
|
479
|
+
listDecisionProviders(): string[];
|
|
480
|
+
/**
|
|
481
|
+
* Answers every question of a decision, each typed by its question: a choice's answer names only
|
|
482
|
+
* its options. Throws `DecisionError` when the request is malformed or no provider can answer.
|
|
483
|
+
*/
|
|
484
|
+
decide<const Q extends DecisionQuestions>(request: DecisionRequest<Q>): Promise<DecisionResult<Q>>;
|
|
485
|
+
private ask;
|
|
486
|
+
private prices;
|
|
487
|
+
}
|
|
488
|
+
/** Answers a decision with one provider, outside any manager: no failover, cache, or lifecycle. */
|
|
489
|
+
export declare function decide<const Q extends DecisionQuestions>(provider: DecisionProvider, request: Omit<DecisionRequest<Q>, 'provider'>): Promise<DecisionResult<Q>>;
|
|
490
|
+
/** A question whose right answer is known, for `runDecisionConformance()`. */
|
|
491
|
+
export interface DecisionConformanceCase {
|
|
492
|
+
/** What the question is about. */
|
|
493
|
+
state: DecisionState;
|
|
494
|
+
/** The question. */
|
|
495
|
+
question: DecisionQuestion;
|
|
496
|
+
/** The right answer: a yes or no, a choice's option, or a score's level. */
|
|
497
|
+
expected: boolean | string | number;
|
|
498
|
+
}
|
|
499
|
+
/** Options for `runDecisionConformance()`. */
|
|
500
|
+
export interface DecisionConformanceOptions {
|
|
501
|
+
/** The model to ask. Defaults to the provider's own. */
|
|
502
|
+
model?: string;
|
|
503
|
+
/**
|
|
504
|
+
* Questions whose answers are known, each of which must be answered right. A stand-in that answers
|
|
505
|
+
* as it is told passes without them; a real model is held to them.
|
|
506
|
+
*/
|
|
507
|
+
cases?: readonly DecisionConformanceCase[];
|
|
508
|
+
}
|
|
509
|
+
/** How a provider did against the decision contract. */
|
|
510
|
+
export interface DecisionConformanceReport {
|
|
511
|
+
/** The provider. */
|
|
512
|
+
provider: string;
|
|
513
|
+
/** Checks that passed. */
|
|
514
|
+
passed: string[];
|
|
515
|
+
/** Checks that failed, each with why. */
|
|
516
|
+
failures: string[];
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Checks a provider against the decision contract: its capabilities are coherent; each question type
|
|
520
|
+
* it declares comes back as probabilities that fit the question; several questions in one decision
|
|
521
|
+
* each get their own answer; a stopped call is not answered; and every case is answered right. Never
|
|
522
|
+
* throws; failures are listed.
|
|
523
|
+
*/
|
|
524
|
+
export declare function runDecisionConformance(provider: DecisionProvider, options?: DecisionConformanceOptions): Promise<DecisionConformanceReport>;
|
|
525
|
+
export {};
|