@cognipeer/agent-sdk 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1502 -150
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +103 -506
- package/dist/index.d.ts +103 -506
- package/dist/index.mjs +1484 -151
- package/dist/index.mjs.map +1 -1
- package/dist/model-rules-Bg9nSqyN.d.cts +658 -0
- package/dist/model-rules-Bg9nSqyN.d.ts +658 -0
- package/dist/model-rules.cjs +763 -0
- package/dist/model-rules.cjs.map +1 -0
- package/dist/model-rules.d.cts +1 -0
- package/dist/model-rules.d.ts +1 -0
- package/dist/model-rules.json +622 -0
- package/dist/model-rules.mjs +756 -0
- package/dist/model-rules.mjs.map +1 -0
- package/package.json +8 -2
|
@@ -0,0 +1,658 @@
|
|
|
1
|
+
type RuleFamily = "openai" | "anthropic" | "gemini" | "other";
|
|
2
|
+
type ReasoningStyle = "effort" | "adaptive" | "budget" | "toggle" | "none";
|
|
3
|
+
/** How "reasoning off" is spelled on the wire (`none` = the effort value itself). */
|
|
4
|
+
type OffMechanism = "none" | "disabled" | "between_tools";
|
|
5
|
+
type TemperatureRule = "any" | "default-only" | "unsupported" | "thinking-requires-1";
|
|
6
|
+
type MaxTokensParam = "max_tokens" | "max_completion_tokens";
|
|
7
|
+
type RuleSource = "verified-2026-10-14" | "docs" | "learned";
|
|
8
|
+
type ModelRule = {
|
|
9
|
+
id: string;
|
|
10
|
+
family: RuleFamily;
|
|
11
|
+
/** Case-insensitive regex tested against the normalised candidate ids. */
|
|
12
|
+
match: string;
|
|
13
|
+
reasoning: {
|
|
14
|
+
style: ReasoningStyle;
|
|
15
|
+
/** Effort values the wire accepts (Chat Completions / Messages). Empty = no reasoning control. */
|
|
16
|
+
efforts: ReasoningEffort[];
|
|
17
|
+
/** Efforts the OpenAI Responses endpoint accepts, when they differ from `efforts`. */
|
|
18
|
+
responsesEfforts?: ReasoningEffort[];
|
|
19
|
+
off: OffMechanism | null;
|
|
20
|
+
};
|
|
21
|
+
sampling: {
|
|
22
|
+
temperature: TemperatureRule;
|
|
23
|
+
topP: boolean;
|
|
24
|
+
topK: boolean;
|
|
25
|
+
penalties: boolean;
|
|
26
|
+
logprobs: boolean;
|
|
27
|
+
};
|
|
28
|
+
maxTokensParam: MaxTokensParam;
|
|
29
|
+
chatTools: {
|
|
30
|
+
reasoning: "any" | "force-none";
|
|
31
|
+
};
|
|
32
|
+
/** Request fields to leave out, learned from a provider 400 (not used in the shipped JSON). */
|
|
33
|
+
dropParams?: string[];
|
|
34
|
+
source: RuleSource;
|
|
35
|
+
note: string;
|
|
36
|
+
};
|
|
37
|
+
type ModelRules = {
|
|
38
|
+
version: number;
|
|
39
|
+
effortLadder: ReasoningEffort[];
|
|
40
|
+
/** Thinking-token budgets for budget-style engines (Anthropic 3.7/4.x, Bedrock). */
|
|
41
|
+
budgets: Record<Exclude<ReasoningEffort, "none">, number>;
|
|
42
|
+
geminiBudgets: Record<Exclude<ReasoningEffort, "none">, number>;
|
|
43
|
+
rules: ModelRule[];
|
|
44
|
+
};
|
|
45
|
+
/** A sparse override merged over a rule (what a provider 400 teaches us). */
|
|
46
|
+
type RulePatch = {
|
|
47
|
+
reasoning?: Partial<ModelRule["reasoning"]>;
|
|
48
|
+
sampling?: Partial<ModelRule["sampling"]>;
|
|
49
|
+
maxTokensParam?: MaxTokensParam;
|
|
50
|
+
chatTools?: Partial<ModelRule["chatTools"]>;
|
|
51
|
+
dropParams?: string[];
|
|
52
|
+
};
|
|
53
|
+
type ResolvedModelRule = ModelRule & {
|
|
54
|
+
/** Id of the rule that matched (e.g. `openai-gpt-6-plus`). */
|
|
55
|
+
matchedRuleId: string;
|
|
56
|
+
/** True when only the family's permissive default matched: the model is not in the JSON. */
|
|
57
|
+
isDefault: boolean;
|
|
58
|
+
/** True when a learned adjustment is merged into this rule. */
|
|
59
|
+
learned: boolean;
|
|
60
|
+
};
|
|
61
|
+
declare const MODEL_RULES: ModelRules;
|
|
62
|
+
/** Normalised ids a rule is matched against: lowercase, last path segment, gateway prefixes stripped. */
|
|
63
|
+
declare function modelIdCandidates(model: string | undefined): string[];
|
|
64
|
+
declare function mergeRulePatch<T extends RulePatch>(base: T, patch: RulePatch | undefined): T;
|
|
65
|
+
/** Records (merges) a learned patch; the oldest entry is evicted past 500. */
|
|
66
|
+
declare function rememberModelRulePatch(provider: string, model: string, patch: RulePatch, scope?: string): void;
|
|
67
|
+
declare function getLearnedModelRulePatch(provider: string, model: string, scope?: string): RulePatch | undefined;
|
|
68
|
+
/** Snapshot of everything learned so far (hosts can persist it and re-seed with `rememberModelRulePatch`). */
|
|
69
|
+
declare function listLearnedModelRules(): Array<{
|
|
70
|
+
provider: string;
|
|
71
|
+
model: string;
|
|
72
|
+
scope?: string;
|
|
73
|
+
patch: RulePatch;
|
|
74
|
+
}>;
|
|
75
|
+
declare function clearLearnedModelRules(): void;
|
|
76
|
+
/**
|
|
77
|
+
* Finds the rule for a model: the first matching rule of the provider's family
|
|
78
|
+
* (or families, for gateways such as Bedrock), else the family's permissive
|
|
79
|
+
* default. Learned adjustments for (provider, model) are merged in; `patch`
|
|
80
|
+
* lets a caller layer one more override on top (used by the retry loop).
|
|
81
|
+
*/
|
|
82
|
+
declare function resolveModelRule(opts: {
|
|
83
|
+
provider?: string;
|
|
84
|
+
model?: string;
|
|
85
|
+
patch?: RulePatch;
|
|
86
|
+
scope?: string;
|
|
87
|
+
}): ResolvedModelRule;
|
|
88
|
+
type EffortMapping = {
|
|
89
|
+
/** The effort value to put on the wire. */
|
|
90
|
+
effort?: ReasoningEffort;
|
|
91
|
+
/** The off mechanism to use instead (`disabled` / `between_tools`). */
|
|
92
|
+
off?: Exclude<OffMechanism, "none">;
|
|
93
|
+
/** False only when the requested value goes through untouched. */
|
|
94
|
+
mapped: boolean;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Maps a requested effort onto what the rule accepts. A supported value passes;
|
|
98
|
+
* an unsupported one goes to the nearest LOWER supported value (nearest higher
|
|
99
|
+
* if nothing lower exists). `none` uses the rule's off mechanism, else the
|
|
100
|
+
* lowest supported value. A requested non-`none` effort never maps to `none`
|
|
101
|
+
* (asking for "minimal" must not switch thinking off). A rule without reasoning
|
|
102
|
+
* control returns `{ mapped: true }` with no value.
|
|
103
|
+
*/
|
|
104
|
+
declare function mapReasoningEffort(rule: ModelRule, effort: ReasoningEffort, endpoint?: "chat" | "responses"): EffortMapping;
|
|
105
|
+
type SamplingField = "temperature" | "topP" | "topK" | "presencePenalty" | "frequencyPenalty" | "logprobs";
|
|
106
|
+
/** Whether the rule lets a sampling field through. `thinkingOn` matters for `thinking-requires-1`. */
|
|
107
|
+
declare function allowsSampling(rule: ModelRule, field: SamplingField, value?: unknown, thinkingOn?: boolean): boolean;
|
|
108
|
+
/** Removes `rule.dropParams` (top-level keys) from a request body. */
|
|
109
|
+
declare function applyDropParams(body: Record<string, any>, rule: ModelRule): void;
|
|
110
|
+
/** Token budget for a budget-style engine; 0 for `none`. */
|
|
111
|
+
declare function effortBudget(effort: ReasoningEffort, table?: "anthropic" | "gemini"): number;
|
|
112
|
+
|
|
113
|
+
declare const MAX_ADJUSTMENTS = 3;
|
|
114
|
+
type ParsedModelErrorKind = "unsupported_parameter" | "unsupported_value" | "max_tokens_rename" | "default_only" | "chat_tools_effort" | "thinking_enabled_unsupported" | "thinking_adaptive_unsupported" | "thinking_off_between_tools" | "thinking_disabled_unsupported" | "temperature_thinking";
|
|
115
|
+
type ParsedModelError = {
|
|
116
|
+
kind: ParsedModelErrorKind;
|
|
117
|
+
param?: string;
|
|
118
|
+
value?: string;
|
|
119
|
+
supportedValues?: string[];
|
|
120
|
+
/** `max_tokens` / `max_completion_tokens` for `max_tokens_rename`. */
|
|
121
|
+
renameTo?: "max_tokens" | "max_completion_tokens";
|
|
122
|
+
message: string;
|
|
123
|
+
};
|
|
124
|
+
type ModelRuleLearnedEvent = {
|
|
125
|
+
provider: string;
|
|
126
|
+
model: string;
|
|
127
|
+
/** Short label, e.g. `drop:temperature`, `max_tokens->max_completion_tokens`. */
|
|
128
|
+
adjustment: string;
|
|
129
|
+
patch: RulePatch;
|
|
130
|
+
/** The provider's error message that triggered it. */
|
|
131
|
+
reason: string;
|
|
132
|
+
};
|
|
133
|
+
/** Recognises the 400 shapes OpenAI-wire servers and Anthropic use for a bad parameter or value. */
|
|
134
|
+
declare function parseModelError(text: string): ParsedModelError | undefined;
|
|
135
|
+
type AdjustmentRun<T> = {
|
|
136
|
+
provider: string;
|
|
137
|
+
model: string;
|
|
138
|
+
/** `chat` (default), `responses`, `messages` or `converse`; selects which effort list a value error teaches. */
|
|
139
|
+
endpoint?: string;
|
|
140
|
+
onLearned?: (event: ModelRuleLearnedEvent) => void;
|
|
141
|
+
/** Keeps learned patches apart per backend (e.g. the base URL): same model id, different deployment. */
|
|
142
|
+
scope?: string;
|
|
143
|
+
/** Request fields the caller set itself (`request.extra` keys): corrected for this call, never learned for the model. */
|
|
144
|
+
callerParams?: string[];
|
|
145
|
+
/**
|
|
146
|
+
* Performs the call for the given overlay. `guard(body)` must be called with
|
|
147
|
+
* the request body just before sending: a retry whose body is identical to
|
|
148
|
+
* the previous attempt's is pointless and ends the loop with the typed error.
|
|
149
|
+
*/
|
|
150
|
+
run: (overlay: RulePatch, guard: (body: unknown) => void) => Promise<T>;
|
|
151
|
+
};
|
|
152
|
+
/** Runs a request, correcting it from the provider's own 400s (see file header). */
|
|
153
|
+
declare function runWithModelAdjustments<T>(opts: AdjustmentRun<T>): Promise<T>;
|
|
154
|
+
/** Converts a 400 ProviderError into the typed error without retrying (Vertex and other single-shot paths). */
|
|
155
|
+
declare function asModelRequestError(err: unknown, provider: string, model: string): unknown;
|
|
156
|
+
|
|
157
|
+
type MessageRole = "system" | "user" | "assistant" | "tool";
|
|
158
|
+
type TextContent = {
|
|
159
|
+
type: "text";
|
|
160
|
+
text: string;
|
|
161
|
+
};
|
|
162
|
+
type ImageContent = {
|
|
163
|
+
type: "image";
|
|
164
|
+
source: {
|
|
165
|
+
type: "base64";
|
|
166
|
+
mediaType: string;
|
|
167
|
+
data: string;
|
|
168
|
+
} | {
|
|
169
|
+
type: "url";
|
|
170
|
+
url: string;
|
|
171
|
+
mediaType?: string;
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* A document attachment (PDF, DOCX, CSV, …). Providers map this to their
|
|
176
|
+
* native document blocks: Gemini `inlineData`/`fileData`, Anthropic
|
|
177
|
+
* `document`, OpenAI Chat Completions `file` / Responses `input_file`,
|
|
178
|
+
* Bedrock Converse `document`. Providers without document support degrade
|
|
179
|
+
* to a text placeholder referencing the file name/URL.
|
|
180
|
+
*/
|
|
181
|
+
type FileContent = {
|
|
182
|
+
type: "file";
|
|
183
|
+
source: {
|
|
184
|
+
type: "base64";
|
|
185
|
+
mediaType: string;
|
|
186
|
+
data: string;
|
|
187
|
+
} | {
|
|
188
|
+
type: "url";
|
|
189
|
+
url: string;
|
|
190
|
+
mediaType?: string;
|
|
191
|
+
};
|
|
192
|
+
/** Original file name, when known (required by some providers, e.g. Bedrock). */
|
|
193
|
+
fileName?: string;
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* An audio attachment. Gemini accepts it as `inlineData`/`fileData`; OpenAI
|
|
197
|
+
* audio-capable models as `input_audio`. Providers without audio support
|
|
198
|
+
* degrade to a text placeholder.
|
|
199
|
+
*/
|
|
200
|
+
type AudioContent = {
|
|
201
|
+
type: "audio";
|
|
202
|
+
source: {
|
|
203
|
+
type: "base64";
|
|
204
|
+
mediaType: string;
|
|
205
|
+
data: string;
|
|
206
|
+
} | {
|
|
207
|
+
type: "url";
|
|
208
|
+
url: string;
|
|
209
|
+
mediaType?: string;
|
|
210
|
+
};
|
|
211
|
+
};
|
|
212
|
+
type ContentPart = TextContent | ImageContent | FileContent | AudioContent;
|
|
213
|
+
type ToolCall = {
|
|
214
|
+
id: string;
|
|
215
|
+
name: string;
|
|
216
|
+
arguments: string;
|
|
217
|
+
/** Gemini `thoughtSignature` attached to a functionCall part; must be echoed back on the next turn. */
|
|
218
|
+
thoughtSignature?: string;
|
|
219
|
+
/** Position of the call within the assistant turn. Set on streamed deltas so a consumer can frame them (OpenAI `tool_calls[].index`). */
|
|
220
|
+
index?: number;
|
|
221
|
+
};
|
|
222
|
+
/**
|
|
223
|
+
* A provider-native reasoning/thinking block. Captured from the model response
|
|
224
|
+
* and replayed verbatim on subsequent turns (required by Anthropic extended
|
|
225
|
+
* thinking when tools are involved — the `signature` must round-trip intact).
|
|
226
|
+
*/
|
|
227
|
+
type ReasoningBlock = {
|
|
228
|
+
type: "thinking" | "redacted_thinking";
|
|
229
|
+
/** Plain-text thinking content (absent for redacted blocks). */
|
|
230
|
+
text?: string;
|
|
231
|
+
/** Cryptographic signature Anthropic requires to be echoed back verbatim. */
|
|
232
|
+
signature?: string;
|
|
233
|
+
/** Opaque payload for redacted_thinking blocks. */
|
|
234
|
+
data?: string;
|
|
235
|
+
};
|
|
236
|
+
/** Captured reasoning payload attached to assistant turns / responses. */
|
|
237
|
+
type ReasoningPayload = {
|
|
238
|
+
/** Verbatim provider blocks to replay on the next turn. */
|
|
239
|
+
blocks?: ReasoningBlock[];
|
|
240
|
+
/** Human-readable reasoning summary, when the provider exposes one. */
|
|
241
|
+
summary?: string;
|
|
242
|
+
};
|
|
243
|
+
type UnifiedMessage = {
|
|
244
|
+
role: MessageRole;
|
|
245
|
+
content: string | ContentPart[];
|
|
246
|
+
name?: string;
|
|
247
|
+
toolCalls?: ToolCall[];
|
|
248
|
+
toolCallId?: string;
|
|
249
|
+
/** Native reasoning blocks to replay verbatim (Anthropic/Bedrock thinking). */
|
|
250
|
+
reasoning?: ReasoningPayload;
|
|
251
|
+
};
|
|
252
|
+
type ToolDefinition = {
|
|
253
|
+
name: string;
|
|
254
|
+
description: string;
|
|
255
|
+
parameters: Record<string, any>;
|
|
256
|
+
strict?: boolean;
|
|
257
|
+
};
|
|
258
|
+
type ChatCompletionRequest = {
|
|
259
|
+
model: string;
|
|
260
|
+
messages: UnifiedMessage[];
|
|
261
|
+
temperature?: number;
|
|
262
|
+
maxTokens?: number;
|
|
263
|
+
topP?: number;
|
|
264
|
+
stop?: string[];
|
|
265
|
+
/** OpenAI-family only; other providers ignore it. */
|
|
266
|
+
presencePenalty?: number;
|
|
267
|
+
/** OpenAI-family only; other providers ignore it. */
|
|
268
|
+
frequencyPenalty?: number;
|
|
269
|
+
tools?: ToolDefinition[];
|
|
270
|
+
toolChoice?: "auto" | "required" | "none" | {
|
|
271
|
+
name: string;
|
|
272
|
+
};
|
|
273
|
+
responseFormat?: {
|
|
274
|
+
type: "json_schema" | "json_object" | "text";
|
|
275
|
+
schema?: Record<string, any>;
|
|
276
|
+
name?: string;
|
|
277
|
+
};
|
|
278
|
+
stream?: boolean;
|
|
279
|
+
/**
|
|
280
|
+
* Streaming only. `false` opts out of `stream_options.include_usage` on
|
|
281
|
+
* OpenAI-shaped endpoints (some servers reject the option). Default true.
|
|
282
|
+
*/
|
|
283
|
+
streamUsage?: boolean;
|
|
284
|
+
/** OpenAI-family: deterministic sampling seed. Passed through. */
|
|
285
|
+
seed?: number;
|
|
286
|
+
/** OpenAI-family: number of choices. Pass-through only; only choice 0 is parsed. */
|
|
287
|
+
n?: number;
|
|
288
|
+
/** OpenAI-family: allow/forbid parallel tool calls. Passed through when set. */
|
|
289
|
+
parallelToolCalls?: boolean;
|
|
290
|
+
/** OpenAI-family: return token log probabilities. Passed through. */
|
|
291
|
+
logprobs?: boolean;
|
|
292
|
+
/** OpenAI-family: how many top log probabilities per token. Passed through. */
|
|
293
|
+
topLogprobs?: number;
|
|
294
|
+
extra?: Record<string, any>;
|
|
295
|
+
/**
|
|
296
|
+
* Unified native-reasoning request config. Each provider maps this to its
|
|
297
|
+
* own shape:
|
|
298
|
+
* - OpenAI o-series: body.reasoning_effort = effort
|
|
299
|
+
* - OpenAI Responses: body.reasoning = { effort }
|
|
300
|
+
* - Anthropic: body.thinking = { type: "enabled", budget_tokens }
|
|
301
|
+
* - Google / Vertex: body.generationConfig.thinkingConfig = { thinkingBudget, includeThoughts }
|
|
302
|
+
*
|
|
303
|
+
* Providers silently ignore unsupported fields.
|
|
304
|
+
*/
|
|
305
|
+
reasoning?: ReasoningRequestConfig;
|
|
306
|
+
/**
|
|
307
|
+
* Cancels the HTTP request (and an in-flight stream) when it aborts. The agent
|
|
308
|
+
* loop sets it from the run's cancellation token / abort signal; a caller using
|
|
309
|
+
* a provider directly can pass its own. It is never serialised into the body.
|
|
310
|
+
*/
|
|
311
|
+
signal?: AbortSignal;
|
|
312
|
+
};
|
|
313
|
+
/**
|
|
314
|
+
* `none` is OFF, and it is a value rather than the absence of one.
|
|
315
|
+
*
|
|
316
|
+
* On a thinking-by-default model — every Qwen3 under vLLM, gpt-5.1 and newer on
|
|
317
|
+
* OpenAI — sending no reasoning field leaves the model thinking. So "do not
|
|
318
|
+
* think" is an instruction that has to travel: `reasoning_effort:"none"` on
|
|
319
|
+
* OpenAI, `thinkingBudget:0` on Gemini, and no thinking block at all on
|
|
320
|
+
* Anthropic (where thinking is opt-in and the absence IS off). Omitting
|
|
321
|
+
* `effort` still means "say nothing about reasoning", which is the different
|
|
322
|
+
* and equally necessary state.
|
|
323
|
+
*
|
|
324
|
+
* `xhigh` / `max` are only accepted by some models. The SDK takes every ladder
|
|
325
|
+
* value for every model and maps it to the nearest lower value that model's rule
|
|
326
|
+
* accepts (see `mapReasoningEffort` and model-rules.json).
|
|
327
|
+
*/
|
|
328
|
+
type ReasoningEffort = "none" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
|
|
329
|
+
type ReasoningRequestConfig = {
|
|
330
|
+
/** Qualitative effort hint (mapped per-provider). */
|
|
331
|
+
effort?: ReasoningEffort;
|
|
332
|
+
/** Explicit per-call token budget for reasoning/thinking. */
|
|
333
|
+
budgetTokens?: number;
|
|
334
|
+
/**
|
|
335
|
+
* Gemini 3 style discrete thinking level (e.g. "low" | "high"). Sent as
|
|
336
|
+
* `thinkingConfig.thinkingLevel` and replaces the derived `thinkingBudget`
|
|
337
|
+
* (the API rejects both together). Ignored by other providers.
|
|
338
|
+
*/
|
|
339
|
+
thinkingLevel?: string;
|
|
340
|
+
/** Whether the provider should expose its reasoning/thought summary to the caller. Gemini: default false. Claude adaptive thinking (5.x): default summarized (`thinking.display = "summarized"`) unless this is `false`; never added when the caller passes an explicit `extra.thinking`. */
|
|
341
|
+
includeThoughts?: boolean;
|
|
342
|
+
/** Provider-specific passthrough merged on top of the mapped shape. */
|
|
343
|
+
providerExtras?: Record<string, any>;
|
|
344
|
+
};
|
|
345
|
+
type TokenUsage = {
|
|
346
|
+
inputTokens: number;
|
|
347
|
+
outputTokens: number;
|
|
348
|
+
totalTokens: number;
|
|
349
|
+
cachedInputTokens: number;
|
|
350
|
+
cachedWriteTokens: number;
|
|
351
|
+
cachedOutputTokens: number;
|
|
352
|
+
reasoningTokens: number;
|
|
353
|
+
};
|
|
354
|
+
declare function emptyUsage(): TokenUsage;
|
|
355
|
+
type FinishReason = "stop" | "tool_calls" | "length" | "content_filter" | "error";
|
|
356
|
+
type ProviderResponseFields = {
|
|
357
|
+
system_fingerprint?: string;
|
|
358
|
+
service_tier?: string;
|
|
359
|
+
annotations?: any[];
|
|
360
|
+
refusal?: string | null;
|
|
361
|
+
logprobs?: any;
|
|
362
|
+
[key: string]: any;
|
|
363
|
+
};
|
|
364
|
+
type ChatCompletionResponse = {
|
|
365
|
+
id: string;
|
|
366
|
+
model: string;
|
|
367
|
+
content: string | null;
|
|
368
|
+
toolCalls: ToolCall[];
|
|
369
|
+
usage: TokenUsage;
|
|
370
|
+
finishReason: FinishReason;
|
|
371
|
+
/** The provider's own finish/stop reason string, unnormalised (e.g. "end_turn", "function_call"). */
|
|
372
|
+
rawFinishReason?: string;
|
|
373
|
+
/**
|
|
374
|
+
* Provider response fields the unified shape has no slot for, kept when present:
|
|
375
|
+
* `system_fingerprint`, `service_tier`, `annotations`, `refusal`, `logprobs`.
|
|
376
|
+
*/
|
|
377
|
+
providerFields?: ProviderResponseFields;
|
|
378
|
+
/** Native reasoning blocks + summary captured from the response, if any. */
|
|
379
|
+
reasoning?: ReasoningPayload;
|
|
380
|
+
raw: any;
|
|
381
|
+
};
|
|
382
|
+
type ChatCompletionChunk = {
|
|
383
|
+
id: string;
|
|
384
|
+
model: string;
|
|
385
|
+
delta: {
|
|
386
|
+
content?: string;
|
|
387
|
+
toolCalls?: Partial<ToolCall>[];
|
|
388
|
+
/** Incremental reasoning text / signature deltas (Anthropic thinking). */
|
|
389
|
+
reasoning?: ReasoningPayload;
|
|
390
|
+
};
|
|
391
|
+
usage?: TokenUsage;
|
|
392
|
+
finishReason?: FinishReason;
|
|
393
|
+
/** The provider's own finish reason string, unnormalised. Present on the chunk that carries it. */
|
|
394
|
+
rawFinishReason?: string;
|
|
395
|
+
/** See ChatCompletionResponse.providerFields; per-chunk (fingerprint, tier, delta refusal/annotations, logprobs). */
|
|
396
|
+
providerFields?: ProviderResponseFields;
|
|
397
|
+
};
|
|
398
|
+
/** Why the text is being embedded; mapped to each provider's own task/input type. */
|
|
399
|
+
type EmbeddingInputType = "search_document" | "search_query" | "classification" | "clustering" | "semantic_similarity" | "question_answering" | "fact_verification" | "code_retrieval_query" | (string & {});
|
|
400
|
+
type EmbeddingRequest = {
|
|
401
|
+
/** Model id (OpenAI/compatible), deployment name (Azure), model id (Bedrock), model name (Vertex). */
|
|
402
|
+
model: string;
|
|
403
|
+
/** Texts to embed. One vector per entry, in the same order. */
|
|
404
|
+
input: string[];
|
|
405
|
+
/** Requested output dimensions, for models that support shortening (OpenAI v3, Titan v2, Cohere v4, Gemini). */
|
|
406
|
+
dimensions?: number;
|
|
407
|
+
/** OpenAI-shaped endpoints: wire encoding. Vectors are always returned as number[] either way. */
|
|
408
|
+
encodingFormat?: "float" | "base64";
|
|
409
|
+
/** Task hint (Cohere on Bedrock, Gemini on Vertex). Ignored by providers without one. */
|
|
410
|
+
inputType?: EmbeddingInputType;
|
|
411
|
+
/** Max texts per HTTP request. Defaults per provider (OpenAI 2048, Vertex 100, Gemini-embedding 1, Titan 1). */
|
|
412
|
+
batchSize?: number;
|
|
413
|
+
/** Provider-specific fields merged into each request body (escape hatch). */
|
|
414
|
+
extra?: Record<string, any>;
|
|
415
|
+
signal?: AbortSignal;
|
|
416
|
+
};
|
|
417
|
+
type EmbeddingUsage = {
|
|
418
|
+
inputTokens: number;
|
|
419
|
+
totalTokens: number;
|
|
420
|
+
};
|
|
421
|
+
type EmbeddingResponse = {
|
|
422
|
+
model: string;
|
|
423
|
+
/** One vector per input, in input order. */
|
|
424
|
+
vectors: number[][];
|
|
425
|
+
usage: EmbeddingUsage;
|
|
426
|
+
/** Raw provider responses, one per HTTP request made. */
|
|
427
|
+
raw: any[];
|
|
428
|
+
};
|
|
429
|
+
type ProviderRetryConfig = {
|
|
430
|
+
/** Maximum retries on 429/5xx (default 3). */
|
|
431
|
+
maxRetries?: number;
|
|
432
|
+
/** Initial delay between retries in ms (default 500, doubled each attempt). */
|
|
433
|
+
baseDelayMs?: number;
|
|
434
|
+
/** Hard cap on a single retry delay in ms (default 8000). */
|
|
435
|
+
maxDelayMs?: number;
|
|
436
|
+
/** Custom predicate to opt in/out of a specific retry. */
|
|
437
|
+
shouldRetry?: (error: any, attempt: number) => boolean;
|
|
438
|
+
};
|
|
439
|
+
/** Transport options shared by every provider config. */
|
|
440
|
+
type ProviderTransportConfig = {
|
|
441
|
+
/**
|
|
442
|
+
* Custom `fetch` used at EVERY HTTP call site of the provider (chat, stream,
|
|
443
|
+
* Responses, token exchange). Lets a host inject an SSRF-safe or proxied
|
|
444
|
+
* fetch. Defaults to the global `fetch`.
|
|
445
|
+
*/
|
|
446
|
+
fetch?: typeof fetch;
|
|
447
|
+
/**
|
|
448
|
+
* Per-request timeout in ms, measured until the response headers arrive
|
|
449
|
+
* (an established stream is not cut by it). Default: none.
|
|
450
|
+
*/
|
|
451
|
+
timeoutMs?: number;
|
|
452
|
+
/**
|
|
453
|
+
* Called when a provider 400 taught the SDK something about a model that is
|
|
454
|
+
* not in (or drifted from) model-rules.json — e.g. "drop temperature",
|
|
455
|
+
* "max_tokens -> max_completion_tokens". The adjustment is already
|
|
456
|
+
* remembered in-process; hosts can log or persist it.
|
|
457
|
+
*/
|
|
458
|
+
onModelRuleLearned?: (event: ModelRuleLearnedEvent) => void;
|
|
459
|
+
};
|
|
460
|
+
type OpenAIProviderConfig = ProviderTransportConfig & {
|
|
461
|
+
/**
|
|
462
|
+
* Which token-limit field to send: `max_tokens` or `max_completion_tokens`.
|
|
463
|
+
* `auto` (default) uses `max_completion_tokens` for o-series / gpt-5 models
|
|
464
|
+
* and `max_tokens` otherwise.
|
|
465
|
+
*/
|
|
466
|
+
maxTokensParam?: "auto" | "max_tokens" | "max_completion_tokens";
|
|
467
|
+
provider: "openai";
|
|
468
|
+
apiKey: string;
|
|
469
|
+
baseURL?: string;
|
|
470
|
+
organization?: string;
|
|
471
|
+
defaultModel?: string;
|
|
472
|
+
defaultHeaders?: Record<string, string>;
|
|
473
|
+
retry?: ProviderRetryConfig;
|
|
474
|
+
/**
|
|
475
|
+
* @deprecated Use `api: "chat" | "responses" | "auto"`. Kept as an alias:
|
|
476
|
+
* `never` = chat, `always` = responses, `auto` = the name-based heuristic.
|
|
477
|
+
* When `api` is also set, `api` wins.
|
|
478
|
+
*
|
|
479
|
+
* WHICH API a reasoning-carrying request is addressed to.
|
|
480
|
+
*
|
|
481
|
+
* auto (the default for OpenAI and Azure) route to the Responses API when
|
|
482
|
+
* the request carries `reasoning` AND the model name looks like an
|
|
483
|
+
* o-series/gpt-5 reasoning model.
|
|
484
|
+
* never always Chat Completions. `reasoning` is still mapped, as
|
|
485
|
+
* `reasoning_effort` plus any `providerExtras`.
|
|
486
|
+
* always always Responses, whatever the model is called.
|
|
487
|
+
*
|
|
488
|
+
* `auto` is a name-based guess, and it decides far more than where reasoning
|
|
489
|
+
* goes: Responses is a different request shape (`input` not `messages`,
|
|
490
|
+
* `text.format` not `response_format`, flat tool objects) and a different
|
|
491
|
+
* response parse. A caller with its own measurements of the Chat Completions
|
|
492
|
+
* body — a gateway, a proxy, an on-prem server that serves one and not the
|
|
493
|
+
* other, or anything that has tuned `extra` — must be able to say so, rather
|
|
494
|
+
* than discover that asking for less thinking silently changed the transport.
|
|
495
|
+
*
|
|
496
|
+
* Applies to any OpenAI-shaped endpoint, which is also why the guess is weak
|
|
497
|
+
* where it matters most: a self-hosted model is named by whoever served it.
|
|
498
|
+
*/
|
|
499
|
+
responsesApi?: "auto" | "never" | "always";
|
|
500
|
+
/** @internal Set by OpenAICompatibleProvider: `auto` then means Chat Completions. */
|
|
501
|
+
_autoMeansChat?: boolean;
|
|
502
|
+
/**
|
|
503
|
+
* Which upstream API requests are sent to. `"auto"` (default) keeps the
|
|
504
|
+
* 0.11 behaviour of each class: OpenAI and Azure use Responses when the
|
|
505
|
+
* request carries `reasoning` and the model name looks like an o-series /
|
|
506
|
+
* gpt-5 model, otherwise Chat Completions; OpenAI-compatible always uses
|
|
507
|
+
* Chat Completions (the model name is the server operator's). `"chat"` always
|
|
508
|
+
* sends Chat Completions. `"responses"`
|
|
509
|
+
* sends every request to the Responses API (`POST /responses`) and streams it
|
|
510
|
+
* over real SSE (text, tool-call arguments and reasoning-summary deltas;
|
|
511
|
+
* set `responsesStreaming: false` to get one non-streaming call instead).
|
|
512
|
+
* Works on `OpenAIProvider`, `AzureProvider` and `OpenAICompatibleProvider`.
|
|
513
|
+
* When set, it wins over the deprecated `responsesApi`.
|
|
514
|
+
*/
|
|
515
|
+
api?: "chat" | "responses" | "auto";
|
|
516
|
+
/**
|
|
517
|
+
* Models that go to the Responses API, named explicitly. Opt-in; unset by
|
|
518
|
+
* default.
|
|
519
|
+
*
|
|
520
|
+
* A string matches the request's model (or `defaultModel`) exactly; a RegExp
|
|
521
|
+
* is tested against it. A match routes the request to Responses whether or
|
|
522
|
+
* not it carries `reasoning` — the case this exists for is a model that
|
|
523
|
+
* reasons by default and rejects function tools on Chat Completions, where
|
|
524
|
+
* sending no reasoning field does not help. `responsesApi: "never"` still
|
|
525
|
+
* wins; under `"always"` the routing is unchanged.
|
|
526
|
+
*
|
|
527
|
+
* Requests for a matched model also get, on the Responses body only:
|
|
528
|
+
* - Chat Completions spellings arriving through `extra`, which /responses
|
|
529
|
+
* rejects with a 400, re-spelled with their values unchanged:
|
|
530
|
+
* `reasoning_effort` → `reasoning.effort` (a request `reasoning` effort
|
|
531
|
+
* still wins), `max_tokens` / `max_completion_tokens` →
|
|
532
|
+
* `max_output_tokens`, `response_format` → `text.format`, and
|
|
533
|
+
* `stream_options.include_usage` dropped.
|
|
534
|
+
* - real SSE streaming, when `responsesStreaming` is also true.
|
|
535
|
+
*
|
|
536
|
+
* Honoured by `OpenAIProvider`, `AzureProvider` and
|
|
537
|
+
* `OpenAICompatibleProvider` alike.
|
|
538
|
+
*/
|
|
539
|
+
responsesApiModels?: Array<string | RegExp>;
|
|
540
|
+
/**
|
|
541
|
+
* Stream Responses API output as it arrives (text, tool-call argument and
|
|
542
|
+
* reasoning-summary deltas) for `api: "responses"` and for models in
|
|
543
|
+
* `responsesApiModels`. Default true with `api: "responses"`, false
|
|
544
|
+
* otherwise (one non-streaming Responses call emitted as a single chunk).
|
|
545
|
+
* Has no effect on Chat Completions requests.
|
|
546
|
+
*/
|
|
547
|
+
responsesStreaming?: boolean;
|
|
548
|
+
};
|
|
549
|
+
type AnthropicProviderConfig = ProviderTransportConfig & {
|
|
550
|
+
provider: "anthropic";
|
|
551
|
+
apiKey: string;
|
|
552
|
+
baseURL?: string;
|
|
553
|
+
defaultModel?: string;
|
|
554
|
+
defaultHeaders?: Record<string, string>;
|
|
555
|
+
anthropicVersion?: string;
|
|
556
|
+
retry?: ProviderRetryConfig;
|
|
557
|
+
/**
|
|
558
|
+
* Prompt caching configuration. When enabled, places cache_control:
|
|
559
|
+
* ephemeral breakpoints on the system prompt and the final tool definition,
|
|
560
|
+
* which is the recommended Anthropic recipe for static system + tools.
|
|
561
|
+
*/
|
|
562
|
+
prompt_caching?: {
|
|
563
|
+
enabled: boolean;
|
|
564
|
+
};
|
|
565
|
+
/**
|
|
566
|
+
* Thinking request shape. `auto` (default) picks per model id: Claude 5.x
|
|
567
|
+
* and newer use `thinking:{type:"adaptive"}` + `output_config.effort`, older
|
|
568
|
+
* models use `thinking:{type:"enabled", budget_tokens}`. Force one with
|
|
569
|
+
* `adaptive` / `budget`. On the specific 400 for the wrong shape the
|
|
570
|
+
* provider retries once with the other form.
|
|
571
|
+
*/
|
|
572
|
+
thinkingMode?: "auto" | "adaptive" | "budget";
|
|
573
|
+
};
|
|
574
|
+
type AzureProviderConfig = ProviderTransportConfig & {
|
|
575
|
+
provider: "azure";
|
|
576
|
+
retry?: ProviderRetryConfig;
|
|
577
|
+
/** See OpenAIProviderConfig.maxTokensParam. */
|
|
578
|
+
maxTokensParam?: "auto" | "max_tokens" | "max_completion_tokens";
|
|
579
|
+
apiKey: string;
|
|
580
|
+
endpoint: string;
|
|
581
|
+
apiVersion?: string;
|
|
582
|
+
deploymentName?: string;
|
|
583
|
+
defaultModel?: string;
|
|
584
|
+
defaultHeaders?: Record<string, string>;
|
|
585
|
+
/** See OpenAIProviderConfig.api. Default "auto". */
|
|
586
|
+
api?: "chat" | "responses" | "auto";
|
|
587
|
+
/** @deprecated Use `api`. */
|
|
588
|
+
responsesApi?: "auto" | "never" | "always";
|
|
589
|
+
responsesApiModels?: Array<string | RegExp>;
|
|
590
|
+
responsesStreaming?: boolean;
|
|
591
|
+
};
|
|
592
|
+
type OpenAICompatibleProviderConfig = ProviderTransportConfig & {
|
|
593
|
+
provider: "openai-compatible";
|
|
594
|
+
organization?: string;
|
|
595
|
+
retry?: ProviderRetryConfig;
|
|
596
|
+
/** @deprecated Use `api`. Default: see `api`. */
|
|
597
|
+
responsesApi?: "auto" | "never" | "always";
|
|
598
|
+
/** See OpenAIProviderConfig.api. Default "auto". */
|
|
599
|
+
api?: "chat" | "responses" | "auto";
|
|
600
|
+
responsesApiModels?: Array<string | RegExp>;
|
|
601
|
+
responsesStreaming?: boolean;
|
|
602
|
+
/** See OpenAIProviderConfig.maxTokensParam. Compatible servers default to `auto`. */
|
|
603
|
+
maxTokensParam?: "auto" | "max_tokens" | "max_completion_tokens";
|
|
604
|
+
apiKey: string;
|
|
605
|
+
baseURL: string;
|
|
606
|
+
defaultModel?: string;
|
|
607
|
+
defaultHeaders?: Record<string, string>;
|
|
608
|
+
};
|
|
609
|
+
type BedrockProviderConfig = ProviderTransportConfig & {
|
|
610
|
+
provider: "bedrock";
|
|
611
|
+
region: string;
|
|
612
|
+
/** Thinking request shape for Claude models: `auto` (by model id), `adaptive` or `budget`. */
|
|
613
|
+
thinkingMode?: "auto" | "adaptive" | "budget";
|
|
614
|
+
accessKeyId?: string;
|
|
615
|
+
secretAccessKey?: string;
|
|
616
|
+
sessionToken?: string;
|
|
617
|
+
defaultModel?: string;
|
|
618
|
+
defaultHeaders?: Record<string, string>;
|
|
619
|
+
retry?: ProviderRetryConfig;
|
|
620
|
+
/**
|
|
621
|
+
* Insert Bedrock Converse `cachePoint` blocks at the end of the system
|
|
622
|
+
* prompt and tools list. Only supported by models that implement Converse
|
|
623
|
+
* prompt caching (most Anthropic Claude variants on Bedrock).
|
|
624
|
+
*/
|
|
625
|
+
prompt_caching?: {
|
|
626
|
+
enabled: boolean;
|
|
627
|
+
};
|
|
628
|
+
};
|
|
629
|
+
type VertexProviderConfig = ProviderTransportConfig & {
|
|
630
|
+
provider: "vertex";
|
|
631
|
+
projectId: string;
|
|
632
|
+
location?: string;
|
|
633
|
+
accessToken?: string;
|
|
634
|
+
serviceAccountJson?: string | Record<string, any>;
|
|
635
|
+
defaultModel?: string;
|
|
636
|
+
defaultHeaders?: Record<string, string>;
|
|
637
|
+
/** Retry policy for 429/5xx (default 3 retries, exponential backoff). */
|
|
638
|
+
retry?: ProviderRetryConfig;
|
|
639
|
+
};
|
|
640
|
+
type JevProviderConfig = ProviderTransportConfig & {
|
|
641
|
+
provider: "jev";
|
|
642
|
+
apiKey: string;
|
|
643
|
+
/** Defaults to https://api.typesafe.ai. */
|
|
644
|
+
baseURL?: string;
|
|
645
|
+
defaultModel?: string;
|
|
646
|
+
defaultHeaders?: Record<string, string>;
|
|
647
|
+
retry?: ProviderRetryConfig;
|
|
648
|
+
};
|
|
649
|
+
type ProviderConfig = OpenAIProviderConfig | AnthropicProviderConfig | AzureProviderConfig | OpenAICompatibleProviderConfig | BedrockProviderConfig | VertexProviderConfig | JevProviderConfig;
|
|
650
|
+
type ProviderType = ProviderConfig["provider"];
|
|
651
|
+
declare class ProviderError extends Error {
|
|
652
|
+
readonly provider: string;
|
|
653
|
+
readonly statusCode?: number | undefined;
|
|
654
|
+
readonly responseBody?: any | undefined;
|
|
655
|
+
constructor(message: string, provider: string, statusCode?: number | undefined, responseBody?: any | undefined);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
export { type EmbeddingUsage as $, type AnthropicProviderConfig as A, type BedrockProviderConfig as B, type ChatCompletionRequest as C, type ModelRule as D, type EmbeddingRequest as E, type ModelRules as F, type ResolvedModelRule as G, type ReasoningEffort as H, type EffortMapping as I, type JevProviderConfig as J, type RuleFamily as K, type RuleSource as L, type ModelRuleLearnedEvent as M, type ReasoningStyle as N, type OpenAICompatibleProviderConfig as O, type ProviderType as P, type OffMechanism as Q, type RulePatch as R, type TemperatureRule as S, type ToolDefinition as T, type UnifiedMessage as U, type VertexProviderConfig as V, type MaxTokensParam as W, type SamplingField as X, type ParsedModelError as Y, type FinishReason as Z, type ProviderResponseFields as _, type EmbeddingResponse as a, type EmbeddingInputType as a0, type TextContent as a1, type ImageContent as a2, type FileContent as a3, type AudioContent as a4, type ToolCall as a5, type ProviderTransportConfig as a6, type ChatCompletionResponse as b, type ChatCompletionChunk as c, type OpenAIProviderConfig as d, type TokenUsage as e, type AzureProviderConfig as f, type ReasoningRequestConfig as g, type ProviderConfig as h, ProviderError as i, emptyUsage as j, MODEL_RULES as k, modelIdCandidates as l, mapReasoningEffort as m, allowsSampling as n, applyDropParams as o, effortBudget as p, mergeRulePatch as q, resolveModelRule as r, parseModelError as s, runWithModelAdjustments as t, asModelRequestError as u, MAX_ADJUSTMENTS as v, listLearnedModelRules as w, getLearnedModelRulePatch as x, rememberModelRulePatch as y, clearLearnedModelRules as z };
|