vern-llm 2.9.0 → 3.0.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.
@@ -0,0 +1,582 @@
1
+ import { bt as ProviderRateLimitHint, n as LLMClient, v as WireStreamChunk } from "../client-HWxkwVvj.mjs";
2
+ //#region src/adapters/internal/nativeStructuredOutput.d.ts
3
+ /**
4
+ * A list of model ids, or a predicate, naming models with a capability.
5
+ * Native structured output has no built in list: which models support it
6
+ * is the provider's call and changes over time, and a wrong guess would
7
+ * trade a clear local error for a confusing provider one.
8
+ */
9
+ type ModelCapabilityOverride = string[] | ((model: string) => boolean);
10
+ //#endregion
11
+ //#region src/adapters/internal/reasoningBudget.utils.d.ts
12
+ /**
13
+ * Converts between `reasoningEffort` tiers and `budgetTokens`, for a
14
+ * provider that only understands the other one. The numbers are a guess,
15
+ * not a provider guarantee, and each adapter's `reasoningEffortTokens`
16
+ * overrides them.
17
+ */
18
+ type EffortTokenTable = Record<'minimal' | 'low' | 'medium' | 'high', number>;
19
+ //#endregion
20
+ //#region src/adapters/internal/imageFormat.d.ts
21
+ /**
22
+ * `ImageBlock.mimeType` values every adapter accepts: the types all
23
+ * supported providers share, so content valid for one is valid for all.
24
+ */
25
+ declare const SUPPORTED_IMAGE_MIME_TYPES: readonly ["image/png", "image/jpeg", "image/gif", "image/webp"];
26
+ type SupportedImageMimeType = (typeof SUPPORTED_IMAGE_MIME_TYPES)[number];
27
+ //#endregion
28
+ //#region src/adapters/claude/types.d.ts
29
+ /** Anthropic's native per-block content shape for a message. */
30
+ type AnthropicContentBlock = {
31
+ type: 'text';
32
+ text: string;
33
+ } | {
34
+ type: 'image';
35
+ source: {
36
+ type: 'base64';
37
+ media_type: SupportedImageMimeType;
38
+ data: string;
39
+ };
40
+ } | {
41
+ type: 'tool_use';
42
+ id: string;
43
+ name: string;
44
+ input: unknown;
45
+ } | {
46
+ type: 'tool_result';
47
+ tool_use_id: string;
48
+ content: string;
49
+ is_error?: boolean;
50
+ } | {
51
+ type: 'thinking';
52
+ thinking: string;
53
+ signature: string;
54
+ } | {
55
+ type: 'redacted_thinking';
56
+ data: string;
57
+ };
58
+ /** The input side of Anthropic's `usage`, which carries the cache counts. */
59
+ interface AnthropicUsage {
60
+ input_tokens?: number | null;
61
+ cache_creation_input_tokens?: number | null;
62
+ cache_read_input_tokens?: number | null;
63
+ cache_creation?: {
64
+ ephemeral_5m_input_tokens?: number;
65
+ ephemeral_1h_input_tokens?: number;
66
+ } | null;
67
+ }
68
+ /** Minimal structural type for the Anthropic SDK's `messages.create` */
69
+ interface AnthropicClient {
70
+ messages: {
71
+ create(params: {
72
+ model: string;
73
+ max_tokens: number;
74
+ temperature?: number;
75
+ system?: string;
76
+ messages: Array<{
77
+ role: 'user' | 'assistant';
78
+ content: string | AnthropicContentBlock[];
79
+ }>;
80
+ tools?: Array<{
81
+ name: string;
82
+ description?: string;
83
+ input_schema: {
84
+ type: 'object';
85
+ [key: string]: unknown;
86
+ };
87
+ strict?: boolean;
88
+ }>;
89
+ tool_choice?: {
90
+ type: 'auto';
91
+ } | {
92
+ type: 'any';
93
+ } | {
94
+ type: 'none';
95
+ } | {
96
+ type: 'tool';
97
+ name: string;
98
+ };
99
+ /**
100
+ * `format` is native structured output, which takes only `type` and
101
+ * `schema`. `effort` drives adaptive thinking.
102
+ */
103
+ output_config?: {
104
+ format?: {
105
+ type: 'json_schema';
106
+ schema: Record<string, unknown>;
107
+ };
108
+ effort?: 'low' | 'medium' | 'high' | 'xhigh' | 'max';
109
+ };
110
+ /** Manual budget thinking, or adaptive thinking paired with `output_config.effort`. */
111
+ thinking?: {
112
+ type: 'enabled';
113
+ budget_tokens: number;
114
+ } | {
115
+ type: 'adaptive';
116
+ };
117
+ }, options: {
118
+ signal: AbortSignal;
119
+ }): Promise<{
120
+ content: Array<{
121
+ type: string;
122
+ text?: string;
123
+ id?: string;
124
+ name?: string;
125
+ input?: unknown;
126
+ thinking?: string;
127
+ signature?: string;
128
+ data?: string;
129
+ }>;
130
+ stop_reason?: string | null;
131
+ usage?: AnthropicUsage & {
132
+ output_tokens?: number;
133
+ output_tokens_details?: {
134
+ thinking_tokens?: number;
135
+ } | null;
136
+ };
137
+ }>;
138
+ };
139
+ }
140
+ //#endregion
141
+ //#region src/adapters/claude/anthropic.d.ts
142
+ /** Optional configuration for `fromAnthropic`. */
143
+ interface AnthropicAdapterOptions {
144
+ /**
145
+ * Models that support native structured output (`output_config.format`),
146
+ * which can be combined with real `tools`. No default: other models
147
+ * emulate `jsonSchema` as a forced tool call.
148
+ */
149
+ nativeStructuredOutputModels?: ModelCapabilityOverride;
150
+ /** Overrides the token count each `reasoningEffort` tier maps onto. Omitted tiers keep the default. */
151
+ reasoningEffortTokens?: Partial<EffortTokenTable>;
152
+ /**
153
+ * Adds models that only accept adaptive thinking, on top of the built in
154
+ * rule (Claude Opus 4.7 and later, every Claude 5 tier model).
155
+ */
156
+ adaptiveOnlyModels?: ModelCapabilityOverride;
157
+ /**
158
+ * Replaces the built in list of models that reject a forced `tool_choice`
159
+ * (Claude Fable 5.1 and later, Opus 5.5 and later, every Claude major 6
160
+ * and later). On these, `toolChoice: 'required'` or `{ name }` throws
161
+ * `unsupported_capability` before dispatch, and `jsonSchema` always uses
162
+ * native structured output.
163
+ */
164
+ forcedToolChoiceUnsupportedModels?: ModelCapabilityOverride;
165
+ /**
166
+ * Whether `messages.create` supports `.withResponse()`, which AIMD's
167
+ * proactive path needs. Default `false`, since a fake or thin wrapper
168
+ * won't implement it.
169
+ */
170
+ supportsWithResponse?: boolean;
171
+ }
172
+ /**
173
+ * Wraps an Anthropic SDK client as an `LLMClient`. `jsonSchema` uses native
174
+ * structured output on covered models and a forced tool call elsewhere,
175
+ * which can't be combined with `tools`. `json_object` throws, since
176
+ * Anthropic can't guarantee it.
177
+ */
178
+ export declare function fromAnthropic(anthropicClient: AnthropicClient, options?: AnthropicAdapterOptions): LLMClient;
179
+ //#endregion
180
+ //#region src/adapters/gemini/types.d.ts
181
+ /** Gemini's per-part content shape; object-typed args match the real SDK. */
182
+ type GeminiPart = {
183
+ text: string;
184
+ } | {
185
+ inlineData: {
186
+ mimeType: string;
187
+ data: string;
188
+ };
189
+ } | {
190
+ functionCall: {
191
+ id?: string;
192
+ name: string;
193
+ args: Record<string, unknown>;
194
+ };
195
+ } | {
196
+ functionResponse: {
197
+ id?: string;
198
+ name: string;
199
+ response: Record<string, unknown>;
200
+ };
201
+ };
202
+ /**
203
+ * Structural type matching the model methods of the real `@google/genai`
204
+ * SDK (`ai.models`).
205
+ *
206
+ * Every field is shaped to be structurally assignable from the real SDK's
207
+ * generated types without importing them, so provider SDKs stay optional:
208
+ * `model` is required (the real SDK requires it), `functionCall.args` /
209
+ * `functionResponse.response` are `Record<string, unknown>` (matching the
210
+ * real SDK, not `unknown`), `toolConfig...mode` is `any` (TypeScript never
211
+ * treats a string-literal union as assignable to the real SDK's string
212
+ * enum), and response-side `functionCall.name` is optional (matching the
213
+ * real SDK).
214
+ */
215
+ interface GeminiModels {
216
+ generateContent(params: {
217
+ model: string;
218
+ contents: Array<{
219
+ role: 'user' | 'model';
220
+ parts: GeminiPart[];
221
+ }>;
222
+ config?: {
223
+ systemInstruction?: {
224
+ parts: Array<{
225
+ text: string;
226
+ }>;
227
+ };
228
+ temperature?: number;
229
+ maxOutputTokens?: number;
230
+ responseMimeType?: string;
231
+ responseSchema?: Record<string, unknown>;
232
+ tools?: Array<{
233
+ functionDeclarations: Array<{
234
+ name: string;
235
+ description?: string;
236
+ parameters: Record<string, unknown>;
237
+ }>;
238
+ }>;
239
+ toolConfig?: {
240
+ functionCallingConfig: {
241
+ mode: any;
242
+ allowedFunctionNames?: string[];
243
+ };
244
+ };
245
+ /** `thinkingBudget` up to Gemini 2.5, `thinkingLevel` from Gemini 3 on. */
246
+ thinkingConfig?: {
247
+ thinkingBudget?: number;
248
+ thinkingLevel?: any;
249
+ };
250
+ abortSignal?: AbortSignal;
251
+ };
252
+ }): Promise<{
253
+ candidates?: Array<{
254
+ content?: {
255
+ parts?: Array<{
256
+ text?: string;
257
+ functionCall?: {
258
+ id?: string;
259
+ name?: string;
260
+ args?: unknown;
261
+ };
262
+ }>;
263
+ };
264
+ finishReason?: string;
265
+ }>;
266
+ usageMetadata?: {
267
+ promptTokenCount?: number;
268
+ candidatesTokenCount?: number;
269
+ totalTokenCount?: number;
270
+ thoughtsTokenCount?: number;
271
+ cachedContentTokenCount?: number;
272
+ };
273
+ }>;
274
+ /** Required only for `stream: true`. Resolves to an iterable of partial responses. */
275
+ generateContentStream?(params: Parameters<GeminiModels['generateContent']>[0]): Promise<AsyncIterable<{
276
+ candidates?: Array<{
277
+ content?: {
278
+ parts?: Array<{
279
+ text?: string;
280
+ functionCall?: {
281
+ id?: string;
282
+ name?: string;
283
+ args?: unknown;
284
+ };
285
+ }>;
286
+ };
287
+ }>;
288
+ usageMetadata?: {
289
+ promptTokenCount?: number;
290
+ candidatesTokenCount?: number;
291
+ totalTokenCount?: number;
292
+ thoughtsTokenCount?: number;
293
+ cachedContentTokenCount?: number;
294
+ };
295
+ }>>;
296
+ }
297
+ /**
298
+ * The top level `@google/genai` client, `new GoogleGenAI(...)`:
299
+ *
300
+ * ```ts
301
+ * import { GoogleGenAI } from '@google/genai';
302
+ * const ai = new GoogleGenAI({ apiKey: '...' });
303
+ * const llm = new VernLLM({ client: fromGemini(ai), model: 'gemini-2.5-flash' });
304
+ * ```
305
+ */
306
+ interface GeminiClient {
307
+ models: GeminiModels;
308
+ /** Set by the SDK; names the provider as Vertex AI or the Gemini API. */
309
+ vertexai?: boolean;
310
+ }
311
+ //#endregion
312
+ //#region src/adapters/gemini/gemini.d.ts
313
+ /** Optional configuration for `fromGemini`. */
314
+ interface GeminiAdapterOptions {
315
+ /** Overrides the token count each `reasoningEffort` tier maps onto. Omitted tiers keep the default. */
316
+ reasoningEffortTokens?: Partial<EffortTokenTable>;
317
+ /**
318
+ * Adds models that use `thinkingLevel` instead of `thinkingBudget`, on top
319
+ * of the built in rule (Gemini 3 and later).
320
+ */
321
+ thinkingLevelModels?: ModelCapabilityOverride;
322
+ }
323
+ /**
324
+ * Wraps the top level `@google/genai` client as an `LLMClient`. `ai.models`
325
+ * throws at construction, since only the top level client says whether it
326
+ * talks to Vertex AI or the Gemini API. `responseSchema` and `tools` combine
327
+ * natively.
328
+ */
329
+ export declare function fromGemini(client: GeminiClient, options?: GeminiAdapterOptions): LLMClient;
330
+ //#endregion
331
+ //#region src/adapters/fetch/types.d.ts
332
+ /** The chat-completion-shaped request VernLLM builds internally */
333
+ type ChatRequest = Parameters<LLMClient['chat']['completions']['create']>[0];
334
+ /**
335
+ * The minimal response shape `fromFetch` reads. Native `fetch`'s `Response`
336
+ * satisfies it, as do thin wrappers around `axios`, `node-fetch` or `undici`.
337
+ */
338
+ interface ResponseLike {
339
+ ok: boolean;
340
+ status: number;
341
+ headers: {
342
+ get(name: string): string | null;
343
+ };
344
+ text(): Promise<string>;
345
+ json(): Promise<unknown>;
346
+ }
347
+ /** A fetch-compatible request function; defaults to native `fetch` */
348
+ type RequestLike = (url: string, init: {
349
+ method: string;
350
+ headers: Record<string, string>;
351
+ body?: string;
352
+ signal?: AbortSignal;
353
+ }) => Promise<ResponseLike>;
354
+ /**
355
+ * A streaming request function: resolves to the response body as an
356
+ * `AsyncIterable` of `Uint8Array` or `string` chunks. Defaults to native
357
+ * `fetch`.
358
+ */
359
+ type StreamRequestLike = (url: string, init: {
360
+ method: string;
361
+ headers: Record<string, string>;
362
+ body?: string;
363
+ signal?: AbortSignal;
364
+ }) => Promise<AsyncIterable<Uint8Array | string>>;
365
+ interface FetchAdapterConfig {
366
+ /** Endpoint URL, or a function of the request in case it depends on model/params */
367
+ url: string | ((params: ChatRequest) => string);
368
+ /** Static headers, or a function (sync or async) for things like refreshed auth tokens */
369
+ headers?: Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>);
370
+ /** HTTP method. Default 'POST' */
371
+ method?: string;
372
+ /**
373
+ * The provider behind `url`, in the OpenTelemetry `gen_ai.provider.name`
374
+ * vocabulary, reported on `AttemptContext.adapter`. Left unset when omitted.
375
+ */
376
+ provider?: string;
377
+ /** The HTTP transport. Defaults to native `fetch`. */
378
+ request?: RequestLike;
379
+ /** Maps VernLLMs internal chat-completion request into the providers raw request body */
380
+ mapRequest: (params: ChatRequest) => unknown;
381
+ /**
382
+ * Maps the provider's JSON response. `content` may be empty when the model
383
+ * only called tools. Each tool call's `arguments` is the JSON-encoded
384
+ * string, as on OpenAI's wire; VernLLM parses and validates it.
385
+ */
386
+ mapResponse: (json: unknown) => {
387
+ content?: string;
388
+ usage?: {
389
+ /** Every input token, cache reads and writes included. */
390
+ promptTokens?: number;
391
+ completionTokens?: number;
392
+ totalTokens?: number;
393
+ /** Cache reads, a subset of `promptTokens`. */
394
+ cacheReadTokens?: number;
395
+ /** Cache writes, a subset of `promptTokens`. */
396
+ cacheWriteTokens?: number;
397
+ /** `cacheWriteTokens` by TTL label, e.g. `{ '5m': 1200 }`. */
398
+ cacheWriteTokensByTtl?: Record<string, number>;
399
+ };
400
+ toolCalls?: Array<{
401
+ id: string;
402
+ name: string;
403
+ arguments: string;
404
+ }>;
405
+ };
406
+ /**
407
+ * The streaming transport. Defaults to native `fetch`, and is required for
408
+ * `stream: true` when `request` is set, since it never falls back to it.
409
+ */
410
+ requestStream?: StreamRequestLike;
411
+ /**
412
+ * Splits the raw stream bytes into events. Defaults to Server-Sent Events
413
+ * framing (`parseSseStream`).
414
+ */
415
+ parseStreamFrames?: (chunks: AsyncIterable<Uint8Array | string>) => AsyncIterable<unknown>;
416
+ /**
417
+ * Maps one parsed stream event into zero or more chunks; `undefined` skips
418
+ * it. Required for `stream: true`.
419
+ */
420
+ mapStreamEvent?: (event: unknown) => WireStreamChunk | WireStreamChunk[] | undefined;
421
+ /**
422
+ * Reads AIMD's proactive rate limit hint off a successful response.
423
+ * Defaults to OpenAI's header set.
424
+ */
425
+ parseRateLimitHint?: (headers: ResponseLike['headers']) => ProviderRateLimitHint;
426
+ }
427
+ //#endregion
428
+ //#region src/adapters/fetch/fetch.d.ts
429
+ /**
430
+ * A raw HTTP adapter for providers with no SDK: supply the URL, headers,
431
+ * and the request and response mapping. A non-2xx response throws an error
432
+ * carrying `status` and `headers`, so retries, `nonRetryableStatus` and
433
+ * Retry-After all apply.
434
+ */
435
+ export declare function fromFetch(config: FetchAdapterConfig): LLMClient;
436
+ //#endregion
437
+ //#region src/adapters/internal/sse.d.ts
438
+ /**
439
+ * Parses a Server-Sent-Events stream of bytes or text into each frame's
440
+ * JSON `data:` payload, in order, over any transport. Multi-line data is
441
+ * joined with `\n`, comment-only frames yield `SSE_PING`, other fields are
442
+ * ignored, and a `[DONE]` frame ends iteration. `\r\n` and bare `\r` count
443
+ * as line endings, including a `\r\n` split across two chunks. Malformed
444
+ * JSON throws `LLMError('parse')`.
445
+ */
446
+ export declare function parseSseStream(source: AsyncIterable<Uint8Array | string>): AsyncGenerator<unknown>;
447
+ /**
448
+ * Yielded for a comment-only frame, the SSE keep-alive ping, so a consumer
449
+ * can tell "still alive" apart from an empty frame.
450
+ */
451
+ export declare const SSE_PING: unique symbol;
452
+ //#endregion
453
+ //#region src/adapters/openai/openaiCompatible.d.ts
454
+ /** Optional configuration for `fromOpenAICompatible`. */
455
+ interface OpenAICompatibleAdapterOptions {
456
+ /**
457
+ * Whether the provider accepts `stream_options.include_usage`. Default
458
+ * `true`. With `false`, `stream_options` is omitted and streams report no
459
+ * usage.
460
+ */
461
+ supportsStreamUsage?: boolean;
462
+ /**
463
+ * Overrides the token counts `budgetTokens` buckets into when
464
+ * `reasoningEffort` isn't set. Omitted tiers keep the default.
465
+ */
466
+ reasoningEffortTokens?: Partial<EffortTokenTable>;
467
+ /**
468
+ * Whether the client supports `.withResponse()`, which AIMD's proactive
469
+ * path needs. Default `false`, since the client can't be verified.
470
+ */
471
+ supportsWithResponse?: boolean;
472
+ /**
473
+ * The provider, in the OpenTelemetry `gen_ai.provider.name` vocabulary,
474
+ * reported on `AttemptContext.adapter`. When omitted it is read off a well
475
+ * known `baseURL` host, and left unset otherwise.
476
+ */
477
+ provider?: string;
478
+ /**
479
+ * Models that only take `tools` on Chat Completions with
480
+ * `reasoning_effort: "none"`, replacing the built in rule (bare `gpt-` ids
481
+ * of major 6 and later, except `-chat` ids). With tools, `"none"` is sent,
482
+ * or the call throws `unsupported_capability` when reasoning was asked for.
483
+ */
484
+ noReasoningToolModels?: ModelCapabilityOverride;
485
+ }
486
+ /**
487
+ * Adapter for any client whose `chat.completions.create` speaks the OpenAI
488
+ * wire format. Requests pass through, except for message translation and
489
+ * the model specific rewrites above. The client is `unknown` because SDK
490
+ * types drift from `LLMClient`; the wire format is the contract.
491
+ */
492
+ export declare function fromOpenAICompatible(client: unknown, options?: OpenAICompatibleAdapterOptions): LLMClient;
493
+ //#endregion
494
+ //#region src/adapters/openai/aliases.d.ts
495
+ /**
496
+ * The OpenAI SDK. Wrap it rather than passing it directly, since newer SDK
497
+ * content-part types no longer typecheck against `LLMClient`.
498
+ */
499
+ export declare const fromOpenAI: typeof fromOpenAICompatible;
500
+ /** Groqs SDK matches the OpenAI wire format */
501
+ export declare const fromGroq: typeof fromOpenAICompatible;
502
+ /** Mistral's OpenAI-compatible endpoint, which accepts `stream_options.include_usage`. */
503
+ export declare const fromMistral: typeof fromOpenAICompatible;
504
+ /** DeepSeeks API is OpenAI-compatible */
505
+ export declare const fromDeepSeek: typeof fromOpenAICompatible;
506
+ /** Cerebras inference API is OpenAI-compatible */
507
+ export declare const fromCerebras: typeof fromOpenAICompatible;
508
+ /** Together AIs API is OpenAI-compatible */
509
+ export declare const fromTogether: typeof fromOpenAICompatible;
510
+ /** Fireworks AIs API is OpenAI-compatible */
511
+ export declare const fromFireworks: typeof fromOpenAICompatible;
512
+ /** Ollama's OpenAI-compatible `/v1/chat/completions` endpoint, not its native `/api/chat`. */
513
+ export declare const fromOllama: typeof fromOpenAICompatible;
514
+ /** OpenRouter's API is OpenAI-compatible */
515
+ export declare const fromOpenRouter: typeof fromOpenAICompatible;
516
+ /** Perplexity's API is OpenAI-compatible */
517
+ export declare const fromPerplexity: typeof fromOpenAICompatible;
518
+ /** DeepInfra's API is OpenAI-compatible */
519
+ export declare const fromDeepInfra: typeof fromOpenAICompatible;
520
+ /** Novita's API is OpenAI-compatible */
521
+ export declare const fromNovita: typeof fromOpenAICompatible;
522
+ /** Hyperbolic's API is OpenAI-compatible */
523
+ export declare const fromHyperbolic: typeof fromOpenAICompatible;
524
+ /** Moonshot's (Kimi) API is OpenAI-compatible */
525
+ export declare const fromMoonshot: typeof fromOpenAICompatible;
526
+ /** Zhipu's (GLM) API is OpenAI-compatible */
527
+ export declare const fromZhipu: typeof fromOpenAICompatible;
528
+ /**
529
+ * LM Studio exposes an OpenAI-compatible endpoint at `/v1/chat/completions`.
530
+ * Point an OpenAI SDK instance's `baseURL` at your local LM Studio server.
531
+ */
532
+ export declare const fromLMStudio: typeof fromOpenAICompatible;
533
+ /**
534
+ * vLLM's OpenAI-compatible server mode exposes `/v1/chat/completions`.
535
+ * Point an OpenAI SDK instance's `baseURL` at your vLLM server.
536
+ */
537
+ export declare const fromVLLM: typeof fromOpenAICompatible;
538
+ /** xAI's Grok API is OpenAI-compatible */
539
+ export declare const fromXAI: typeof fromOpenAICompatible;
540
+ /** NVIDIA NIM's hosted and self-hosted endpoints are OpenAI-compatible */
541
+ export declare const fromNvidiaNIM: typeof fromOpenAICompatible;
542
+ /** Vercel AI Gateway is OpenAI-compatible */
543
+ export declare const fromVercelAIGateway: typeof fromOpenAICompatible;
544
+ /** Cloudflare Workers AI exposes an OpenAI-compatible endpoint */
545
+ export declare const fromCloudflareWorkersAI: typeof fromOpenAICompatible;
546
+ /** Nebius AI Studio is OpenAI-compatible */
547
+ export declare const fromNebius: typeof fromOpenAICompatible;
548
+ /** SambaNova Cloud's API is OpenAI-compatible */
549
+ export declare const fromSambaNova: typeof fromOpenAICompatible;
550
+ /** Baseten's model hosting exposes an OpenAI-compatible endpoint */
551
+ export declare const fromBaseten: typeof fromOpenAICompatible;
552
+ /** Featherless AI's API is OpenAI-compatible */
553
+ export declare const fromFeatherless: typeof fromOpenAICompatible;
554
+ /** Friendli AI's serving endpoint is OpenAI-compatible */
555
+ export declare const fromFriendli: typeof fromOpenAICompatible;
556
+ /** SiliconFlow's API is OpenAI-compatible */
557
+ export declare const fromSiliconFlow: typeof fromOpenAICompatible;
558
+ /** Parasail's inference API is OpenAI-compatible */
559
+ export declare const fromParasail: typeof fromOpenAICompatible;
560
+ /** StepFun's API is OpenAI-compatible */
561
+ export declare const fromStepFun: typeof fromOpenAICompatible;
562
+ /** MiniMax's API is OpenAI-compatible */
563
+ export declare const fromMiniMax: typeof fromOpenAICompatible;
564
+ /** Lambda Labs' Inference API is OpenAI-compatible */
565
+ export declare const fromLambdaLabs: typeof fromOpenAICompatible;
566
+ /** Snowflake Cortex's LLM endpoint is OpenAI-compatible */
567
+ export declare const fromSnowflakeCortex: typeof fromOpenAICompatible;
568
+ /** Anyscale Endpoints' API is OpenAI-compatible */
569
+ export declare const fromAnyscale: typeof fromOpenAICompatible;
570
+ /** Lepton AI's inference API is OpenAI-compatible */
571
+ export declare const fromLepton: typeof fromOpenAICompatible;
572
+ /** Inference.net's API is OpenAI-compatible */
573
+ export declare const fromInferenceNet: typeof fromOpenAICompatible;
574
+ /** Infermatic's API is OpenAI-compatible */
575
+ export declare const fromInfermatic: typeof fromOpenAICompatible;
576
+ /** AtlasCloud's inference API is OpenAI-compatible */
577
+ export declare const fromAtlasCloud: typeof fromOpenAICompatible;
578
+ /** 01.AI's (Yi models) API is OpenAI-compatible */
579
+ export declare const from01AI: typeof fromOpenAICompatible;
580
+ //#endregion
581
+ export type { AnthropicAdapterOptions, AnthropicClient, FetchAdapterConfig, GeminiAdapterOptions, GeminiClient, OpenAICompatibleAdapterOptions, RequestLike, ResponseLike, StreamRequestLike };
582
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/adapters/internal/nativeStructuredOutput.ts","../../src/adapters/internal/reasoningBudget.utils.ts","../../src/adapters/internal/imageFormat.ts","../../src/adapters/claude/types.ts","../../src/adapters/claude/anthropic.ts","../../src/adapters/gemini/types.ts","../../src/adapters/gemini/gemini.ts","../../src/adapters/fetch/types.ts","../../src/adapters/fetch/fetch.ts","../../src/adapters/internal/sse.ts","../../src/adapters/openai/openaiCompatible.ts","../../src/adapters/openai/aliases.ts"],"mappings":"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"}
@@ -0,0 +1,12 @@
1
+ import{a as e,i as t,r as n,s as r,t as i}from"../responseBody.utils-B2tQiGUi.mjs";async function a(e){let{data:t,response:n}=await e.withResponse();return{data:t,headers:n.headers}}function o(e){return e.remainingRequests!==void 0||e.limitRequests!==void 0?{type:`rate_limit_hint`,hint:e}:void 0}async function s(e,t){return t?a(e()):{data:await e()}}const c={minimal:1024,low:4096,medium:16e3,high:32e3};function l(e){if(!e)return c;let t={...c,...e};if(!(t.minimal<t.low&&t.low<t.medium&&t.medium<t.high))throw new r(`reasoningEffortTokens must keep tiers in strictly ascending order (minimal < low < medium < high), got ${JSON.stringify(t)}. An out-of-order override doesn't just misrank tiers, it can make some of them unreachable.`,`invalid_params`);return t}function u(e,t=c){return t[e]}function d(e,t=c){return e<=t.minimal?`minimal`:e<=t.low?`low`:e<=t.medium?`medium`:`high`}function f(e){let t=/opus-(\d+)(?:-(\d+))?/.exec(e);if(!t)return null;let n=t[2],r=n===void 0||n.length>=8?0:Number(n);return[Number(t[1]),r]}function p(e){let t=f(e);if(t){let[e,n]=t;return e>4||e===4&&n>=7}return[`sonnet-5`,`fable-5`,`mythos`].some(t=>e.includes(t))}function m(e,t){return p(e)?!0:t?Array.isArray(t)?t.includes(e):t(e):!1}function h(e,t){return!m(e,t)}function g(e,t){if(e<1024)throw new r(`budgetTokens (${e}) is below Anthropic's minimum of 1024. Raise budgetTokens, or use a reasoningEffort tier of 'low' or above with the default conversion table.`,`invalid_params`);if(e>=t)throw new r(`budgetTokens (${e}) must be less than maxTokens (${t}); the thinking budget and the reply share the same max_tokens ceiling on Anthropic. Raise maxTokens, or lower budgetTokens/reasoningEffort.`,`invalid_params`)}function ee(e){if(e)throw new r(`budgetTokens/reasoningEffort was set alongside ${e}. Anthropic rejects thinking combined with a tool_choice that forces tool use, the model has to be able to reply with plain text for thinking to run. Use toolChoice: 'auto' (or omit toolChoice) for this call, or drop budgetTokens/reasoningEffort for it.`,`invalid_params`)}function te(e){return e===`minimal`?`low`:e}function _(e,t){return y(t,e.toUpperCase())}function v(e){let t=/gemini-\d+\.(\d+)/.exec(e);return t?Number(t[1]):0}function y(e,t){if(!e.includes(`pro`))return t;let n=b(e);return n===null||n<3?t:v(e)===0?t===`HIGH`?`HIGH`:`LOW`:t===`MINIMAL`?`LOW`:t}function b(e){let t=/gemini-(\d+)/.exec(e);return t?Number(t[1]):null}function ne(e){let t=b(e);return t!==null&&t>=3}function re(e,t){return ne(e)?!0:t?Array.isArray(t)?t.includes(e):t(e):!1}const x=new Set;function S(e,t,n){return r=>{if(x.has(e))return;let i=t();i!==void 0&&i>0&&(x.add(e),r.warn(`[VernLLM] ${e}: the SDK client retries up to ${i} times on its own, hidden from VernLLM's retries, circuit breaker, rate limiter, and events. ${n} so VernLLM is the only retry owner.`))}}function C(e){let t=e?.maxRetries;return typeof t==`number`?t:void 0}function w(e,t){return new r(e,`invalid_params`,{code:`unsupported_capability`,issues:{capability:t}})}function T(e,t,n){let r=i(`${e} (${t.status})`,n);return r.status=t.status,r.headers=t.headers,r}function E(e){let t=/claude-([a-z]+)-(\d+)(?:[-.](\d+))?/i.exec(e),n=t??/claude-(\d+)(?:[-.](\d+))?/i.exec(e);if(!n)return null;let[,r,i,a]=t?n:[n[0],void 0,n[1],n[2]],o=a===void 0||a.length>=8?0:Number(a);return{family:r?.toLowerCase(),major:Number(i),minor:o}}function D(e,t,n,r){return e>n||e===n&&t>=r}function ie(e){let t=E(e);if(!t)return!1;let{family:n,major:r,minor:i}=t;return r>=6?!0:n===`fable`?D(r,i,5,1):n===`opus`&&D(r,i,5,5)}function O(e,t){return t?Array.isArray(t)?t.includes(e):t(e):ie(e)}function ae(e,t,n,r){if(n&&n!==`auto`&&n!==`none`&&O(t,r))throw w(`${e} model "${t}" rejects a tool_choice that forces tool use, so ${n===`required`?`toolChoice: 'required'`:`toolChoice: { name: '${n.function.name}' }`} can't be sent. Use toolChoice: 'auto' and ask for the tool in the prompt instead. If this model does accept it, list the models that reject it in forcedToolChoiceUnsupportedModels.`,`forced_tool_choice`)}function oe(e,t){return t?Array.isArray(t)?t.includes(e):t(e):!1}function se(e,t,n,i){let a=t.response_format?.type===`json_schema`?t.response_format.json_schema:void 0,o=a?.name.trim();if(a&&!o)throw new r(`json_schema.name must not be empty.`,`validation`);let s=O(t.model,n.forcedToolChoiceUnsupportedModels);t.tools?.length&&ae(e,t.model,t.tool_choice,n.forcedToolChoiceUnsupportedModels);let c=!!a&&(s||oe(t.model,n.nativeStructuredOutputModels));if(a&&t.tools?.length&&!c)throw w(i.toolsWithJsonSchema(t.model),`tools_with_json_schema`);if(t.response_format?.type===`json_object`)throw new r(i.jsonObject,`validation`);return!a||!o?{mode:`none`}:{mode:c?`native`:`forcedTool`,jsonSchema:a,schemaName:o}}function ce(e,t,n){if(e.budget_tokens!==void 0||e.reasoning_effort!==void 0){if(ee(t),h(e.model,n.adaptiveOnlyModels)){let t=e.budget_tokens??u(e.reasoning_effort,n.effortTokenTable);return g(t,e.max_tokens),{thinking:{type:`enabled`,budget_tokens:t}}}return{thinking:{type:`adaptive`},effort:te(e.reasoning_effort??d(e.budget_tokens,n.effortTokenTable))}}}const k=[`image/png`,`image/jpeg`,`image/gif`,`image/webp`];function A(e){if(k.includes(e))return e;throw new r(`Unsupported image mimeType "${e}": expected one of ${k.join(`, `)}`,`invalid_params`)}function le(e){return e.map(e=>e.type===`image`?{type:`image`,source:{type:`base64`,media_type:A(e.mimeType),data:e.data}}:{type:`text`,text:e.text})}function j(e,t){if(e.type!==`object`)throw new r(`Tool "${t}"'s schema must have "type": "object" (Anthropic requires object-shaped tool parameters).`,`validation`);return e}function ue(e){let t=e||`auto`;switch(t){case`auto`:return{type:`auto`};case`none`:return{type:`none`};case`required`:return{type:`any`};default:return{type:`tool`,name:t.function.name}}}function de(e,t){return{tools:e.map(e=>({name:e.function.name,description:e.function.description,input_schema:j(e.function.parameters,e.function.name)})),toolChoice:ue(t)}}const fe={toolsWithJsonSchema:e=>`Anthropic model "${e}" is not covered by nativeStructuredOutputModels, so \`jsonSchema\` is emulated as a forced single tool call there, which collides with the \`tools\` you also provided. Either drop \`tools\` or \`jsonSchema\` for this call, or pass this model in fromAnthropic's \`nativeStructuredOutputModels\` option once you've confirmed it supports Anthropic's \`output_config.format\`.`,jsonObject:'response_format: "json_object" is not supported on Anthropic. Unlike OpenAI, Anthropic has no API-level field that mechanically guarantees valid JSON output for this mode, so it used to be emulated by injecting a "respond with JSON only" instruction into the system prompt, a guarantee this adapter can no longer make. Use `jsonSchema` instead, which maps to a real API-level constraint (Anthropic\'s native output_config.format on covered models, or a forced single tool call otherwise).'};function pe(e){if(e?.type===`tool`)return`toolChoice forcing the "${e.name}" tool`;if(e?.type===`any`)return`toolChoice: 'required' (Anthropic's "any" tool_choice)`}function M(e,t,n,r,i){let a=e.messages.find(e=>e.role===`system`),o=e.messages.filter(e=>e.role===`user`||e.role===`assistant`||e.role===`tool`),s=se(`Anthropic`,e,{nativeStructuredOutputModels:t,forcedToolChoiceUnsupportedModels:i},fe),c,l,u,d;if(s.mode===`forcedTool`){let{schema:e,description:t,strict:n}=s.jsonSchema;c=s.schemaName,u=[{name:c,description:t,input_schema:j(e,c),strict:n}],d={type:`tool`,name:c}}else s.mode===`native`&&(l={type:`json_schema`,schema:s.jsonSchema.schema}),e.tools?.length&&({tools:u,toolChoice:d}=de(e.tools,e.tool_choice));let f=ce(e,pe(d),{adaptiveOnlyModels:r,effortTokenTable:n}),p=f?.effort,m=a?.content,h=f?void 0:e.temperature;return{body:{model:e.model,max_tokens:e.max_tokens,...h===void 0?{}:{temperature:h},system:m||void 0,messages:N(o.map(e=>P(e))),...u?{tools:u,tool_choice:d}:{},...l||p?{output_config:{...l?{format:l}:{},...p?{effort:p}:{}}}:{},...f?{thinking:f.thinking}:{}},toolName:c}}function N(e){let t=e=>e.role===`user`&&Array.isArray(e.content)&&e.content.length>0&&e.content.every(e=>e.type===`tool_result`),n=[];for(let r of e){let e=n.at(-1);t(r)&&e&&t(e)?e.content.push(...r.content):n.push(r)}return n}function P(e){if(e.role===`tool`)return{role:`user`,content:[{type:`tool_result`,tool_use_id:e.tool_call_id,content:e.content,...e.is_error?{is_error:!0}:{}}]};if(e.role===`assistant`&&(e.tool_calls?.length||e.thinking?.length)){let t=(e.thinking??[]).map(e=>e.type===`thinking`?{type:`thinking`,thinking:e.thinking,signature:e.signature}:{type:`redacted_thinking`,data:e.data});e.content&&t.push({type:`text`,text:e.content});for(let n of e.tool_calls??[]){let e;try{e=n.function.arguments.trim()?JSON.parse(n.function.arguments):{}}catch(e){throw new r(`Assistant tool call "${n.function.name}" (${n.id}) has arguments that are not valid JSON.`,`validation`,{cause:e})}if(e===null||Array.isArray(e)||typeof e!=`object`)throw new r(`Assistant tool call "${n.function.name}" (${n.id}) arguments must be a JSON object.`,`validation`);t.push({type:`tool_use`,id:n.id,name:n.function.name,input:e})}return{role:`assistant`,content:t}}return{role:e.role,content:Array.isArray(e.content)?le(e.content):e.content??``}}function F(e,t){return e===t?{finish_reason:`length`}:{}}function I(e,t){throw new r(`${e} did not return the required structured output tool "${t}".`,`validation`)}function L(e,t,n){if(!n||typeof n!=`object`||Array.isArray(n))throw new r(`${e} returned invalid structured output for tool "${t}". Expected an object.`,`validation`)}const R=e=>typeof e==`number`&&Number.isFinite(e)?e:0;function z(e){let t=[e?.input_tokens,e?.cache_creation_input_tokens,e?.cache_read_input_tokens];if(!t.every(e=>e==null))return t.reduce((e,t)=>e+R(t),0)}function B(e){if(!e)return;let t=e.cache_creation?{"5m":R(e.cache_creation.ephemeral_5m_input_tokens),"1h":R(e.cache_creation.ephemeral_1h_input_tokens)}:void 0;return{cached_tokens:e.cache_read_input_tokens??void 0,cache_write_tokens:e.cache_creation_input_tokens??void 0,...t?{cache_write_tokens_by_ttl:t}:{}}}function me(e){return e.flatMap(e=>e.type===`thinking`?[{type:`thinking`,thinking:e.thinking??``,signature:e.signature??``}]:e.type===`redacted_thinking`?[{type:`redacted_thinking`,data:e.data??``}]:[])}function he(e,t){let n,r,i=me(e.content);if(t){let r=e.content.find(e=>e.type===`tool_use`&&e.name===t);r||I(`Anthropic`,t),L(`Anthropic`,t,r.input),n=JSON.stringify(r.input)}else{n=e.content.filter(e=>e.type===`text`).map(e=>e.text??``).join(``);let t=e.content.filter(e=>e.type===`tool_use`);t.length&&(r=t.map(e=>({id:e.id,type:`function`,function:{name:e.name,arguments:JSON.stringify(e.input??{})}})))}return{choices:[{message:{content:n,...r?{tool_calls:r}:{},...i.length?{thinking:i}:{}},...F(e.stop_reason,`max_tokens`)}],usage:{prompt_tokens:z(e.usage),completion_tokens:e.usage?.output_tokens,total_tokens:(z(e.usage)??0)+(e.usage?.output_tokens??0),prompt_tokens_details:B(e.usage),...e.usage?.output_tokens_details?.thinking_tokens===void 0?{}:{completion_tokens_details:{reasoning_tokens:e.usage.output_tokens_details.thinking_tokens}}}}}function*ge(e,t){let n=t.content_block;switch(n.type){case`tool_use`:{let r=n.name===e.toolName?`json-tool`:`tool_use`;e.blockKinds.set(t.index,r),r===`json-tool`?e.sawJsonTool=!0:e.toolName||(yield{type:`tool_call_delta`,index:t.index,id:n.id,name:n.name});return}case`thinking`:e.thinkingBlocks.set(t.index,{type:`thinking`,thinking:n.thinking??``,signature:n.signature??``});return;case`redacted_thinking`:e.thinkingBlocks.set(t.index,{type:`redacted_thinking`,data:n.data??``});return;default:e.blockKinds.set(t.index,`text`)}}function*_e(e,t){let{delta:n}=t;switch(n.type){case`text_delta`:e.toolName||(yield{type:`text-delta`,delta:n.text});return;case`thinking_delta`:{let r=e.thinkingBlocks.get(t.index);r?.type===`thinking`&&(r.thinking+=n.thinking),yield{type:`ping`};return}case`signature_delta`:{let r=e.thinkingBlocks.get(t.index);r?.type===`thinking`&&(r.signature+=n.signature),yield{type:`ping`};return}case`input_json_delta`:e.blockKinds.get(t.index)===`json-tool`?yield{type:`text-delta`,delta:n.partial_json}:e.toolName||(yield{type:`tool_call_delta`,index:t.index,argumentsDelta:n.partial_json})}}function*ve(e,t){let n=e.thinkingBlocks.get(t.index);n&&(e.thinkingBlocks.delete(t.index),yield{type:`thinking_block`,block:n})}const ye=[`input_tokens`,`cache_creation_input_tokens`,`cache_read_input_tokens`];function be(e,t){let n={...e};for(let e of ye){let r=t?.[e];r!=null&&(n[e]=r)}return n}function xe(e,t){let n=be(e.startUsage,t.usage),r=z(n)??0,i=t.usage?.output_tokens??0,a=t.usage?.output_tokens_details?.thinking_tokens;return{type:`usage`,usage:{prompt_tokens:r,completion_tokens:i,total_tokens:r+i,prompt_tokens_details:B(n),...a===void 0?{}:{completion_tokens_details:{reasoning_tokens:a}}}}}async function*Se(e,t){let n={toolName:t,blockKinds:new Map,thinkingBlocks:new Map,startUsage:void 0,sawJsonTool:!1};for await(let t of e)switch(t.type){case`message_start`:n.startUsage=t.message.usage;break;case`content_block_start`:yield*ge(n,t);break;case`content_block_delta`:yield*_e(n,t);break;case`content_block_stop`:yield*ve(n,t);break;case`message_delta`:yield xe(n,t);break;case`ping`:yield{type:`ping`}}t&&!n.sawJsonTool&&I(`Anthropic`,t)}function Ce(e,r){let i=l(r?.reasoningEffortTokens),a=r?.supportsWithResponse??!1,c=S(`anthropic`,()=>C(e),`Pass maxRetries: 0 to the client`),u=e=>M(e,r?.nativeStructuredOutputModels,i,r?.adaptiveOnlyModels,r?.forcedToolChoiceUnsupportedModels),d=(t,n)=>s(()=>e.messages.create(t,n),a);return{supportsJsonObjectMode:!1,cacheReadsCountTowardRateLimit:!1,adapter:{name:`anthropic`,provider:`anthropic`},setLogger:c,chat:{completions:{async create(e,r){let{body:i,toolName:a}=u(e),{data:o,headers:s}=await d(i,r),c=he(o,a);return s&&n(c,t(s)),c},async*createStream(e,n){let{body:r,toolName:i}=u(e),{data:a,headers:s}=await d({...r,stream:!0},n);if(s){let e=o(t(s));e&&(yield e)}yield*Se(a,i)}}}}}function we(e){return e.map(e=>e.type===`image`?{inlineData:{mimeType:A(e.mimeType),data:e.data}}:{text:e.text})}function Te(e){let t=e||`auto`;switch(t){case`auto`:return{functionCallingConfig:{mode:`AUTO`}};case`none`:return{functionCallingConfig:{mode:`NONE`}};case`required`:return{functionCallingConfig:{mode:`ANY`}};default:return{functionCallingConfig:{mode:`ANY`,allowedFunctionNames:[t.function.name]}}}}function Ee(e){let t=new Map;for(let n of e)if(n.role===`assistant`)for(let e of n.tool_calls??[])t.set(e.id,e.function.name);return t}function De(e,t){if(e.role===`tool`)return{role:`user`,parts:[{functionResponse:{id:e.tool_call_id,name:t.get(e.tool_call_id)??e.tool_call_id,response:ke(e.content)}}]};if(e.role===`assistant`&&e.tool_calls?.length){let t=[];return typeof e.content==`string`&&e.content&&t.push({text:e.content}),t.push(...e.tool_calls.map(e=>({functionCall:{id:e.id,name:e.function.name,args:Oe(e.function.arguments,e.function.name)}}))),{role:`model`,parts:t}}return{role:e.role===`assistant`?`model`:`user`,parts:Array.isArray(e.content)?we(e.content):[{text:e.content??``}]}}function Oe(e,t){let n;try{n=e.trim()?JSON.parse(e):{}}catch(e){throw new r(`Tool call "${t}" arguments are not valid JSON.`,`parse`,{cause:e,code:`tool_arguments_parse_failed`})}if(!n||Array.isArray(n)||typeof n!=`object`)throw new r(`Tool call "${t}" arguments must be a JSON object.`,`validation`);return n}function ke(e){let t;try{t=e.trim()?JSON.parse(e):``}catch{t=e}return t&&!Array.isArray(t)&&typeof t==`object`?t:{output:t}}function Ae(e){let t=e=>e.role===`user`&&e.parts.length>0&&e.parts.every(e=>`functionResponse`in e),n=[];for(let r of e){let e=n.at(-1);t(r)&&e&&t(e)?e.parts.push(...r.parts):n.push(r)}return n}function je(e,t,n){if(re(e.model,n)){let n=e.reasoning_effort??(e.budget_tokens===void 0?void 0:d(e.budget_tokens,t));return n===void 0?void 0:{thinkingLevel:_(n,e.model)}}let r=e.budget_tokens??(e.reasoning_effort?u(e.reasoning_effort,t):void 0);return r===void 0?void 0:{thinkingBudget:r}}function V(e,t,n){let r=e.messages.find(e=>e.role===`system`),i=e.messages.filter(e=>e.role===`user`||e.role===`assistant`||e.role===`tool`),a=!!e.response_format,o={...e.temperature===void 0?{}:{temperature:e.temperature},maxOutputTokens:e.max_tokens,...r?{systemInstruction:{parts:[{text:r.content}]}}:{}};if(a&&(o.responseMimeType=`application/json`),e.response_format?.type===`json_schema`){let{schema:t,description:n}=e.response_format.json_schema;o.responseSchema={...t,...n?{description:n}:{}}}e.tools?.length&&(o.tools=[{functionDeclarations:e.tools.map(e=>({name:e.function.name,description:e.function.description,parameters:e.function.parameters}))}],o.toolConfig=Te(e.tool_choice));let s=je(e,t,n);s&&(o.thinkingConfig=s);let c=Ee(i);return{model:e.model,contents:Ae(i.map(e=>De(e,c))),config:o}}function H(e,t){return`${e}#${t}`}function U(e){return{prompt_tokens:e?.promptTokenCount,completion_tokens:e?.candidatesTokenCount,total_tokens:e?.totalTokenCount,prompt_tokens_details:{cached_tokens:e?.cachedContentTokenCount},...e?.thoughtsTokenCount===void 0?{}:{completion_tokens_details:{reasoning_tokens:e.thoughtsTokenCount}}}}function Me(e){let t=e.candidates?.[0]?.content?.parts??[],n=t.map(e=>e.text??``).join(``),r=t.filter(e=>e.functionCall),i;if(r.length){let e=new Map;i=r.map(t=>{let n=t.functionCall.name,r=t.functionCall.id,i=e.get(n)??0;return e.set(n,i+1),{id:r??H(n,i),type:`function`,function:{name:n,arguments:JSON.stringify(t.functionCall.args??{})}}})}return{choices:[{message:{content:n,...i?{tool_calls:i}:{}},...F(e.candidates?.[0]?.finishReason,`MAX_TOKENS`)}],usage:U(e.usageMetadata)}}async function*Ne(e){let t=0,n=new Map,r;for await(let i of e){let e=i.candidates?.[0]?.content?.parts??[];for(let r of e)if(r.text&&(yield{type:`text-delta`,delta:r.text}),r.functionCall){let e=r.functionCall.name,i=r.functionCall.id,a=e?n.get(e)??0:0;e&&n.set(e,a+1),yield{type:`tool_call_delta`,index:t,id:i??(e?H(e,a):void 0),name:r.functionCall.name,argumentsDelta:JSON.stringify(r.functionCall.args??{}),complete:!0},t++}i.usageMetadata&&(r=i.usageMetadata)}r&&(yield{type:`usage`,usage:U(r)})}function Pe(e){return e.vertexai===!0?{provider:`gcp.vertex_ai`}:e.vertexai===!1?{provider:`gcp.gemini`}:{}}function Fe(e){let t=e.httpOptions?.retryOptions;if(t&&typeof t==`object`)return Math.max(1,Number(t.attempts??5))-1}function Ie(e,t){let n=l(t?.reasoningEffortTokens),r=t?.thinkingLevelModels,i=e.models;if(!i&&typeof e.generateContent==`function`)throw w(`fromGemini takes the top level client: pass ai (new GoogleGenAI(...)), not ai.models.`,`top_level_client`);if(typeof i?.generateContent!=`function`)throw w(`fromGemini requires a client with models.generateContent: pass ai (new GoogleGenAI(...)).`,`generateContent`);let a=S(`gemini`,()=>Fe(e),`Remove httpOptions.retryOptions from the client, or set its attempts to 1,`),o=i.generateContent.bind(i),s=typeof i.generateContentStream==`function`?i.generateContentStream.bind(i):void 0;return{adapter:{name:`gemini`,...Pe(e)},setLogger:a,chat:{completions:{async create(e,t){let i=V(e,n,r);return i.config={...i.config,abortSignal:t.signal},Me(await o(i))},async*createStream(e,t){if(!s)throw w(`stream: true requires a Gemini client with generateContentStream`,`generateContentStream`);let i=V(e,n,r);i.config={...i.config,abortSignal:t.signal},yield*Ne(await s(i))}}}}}async function*W(e){let t=new TextDecoder(`utf-8`,{fatal:!0}),n=Le();for await(let i of e){let e;try{e=typeof i==`string`?i:t.decode(i,{stream:!0})}catch(e){throw new r(`Invalid UTF-8 in SSE stream`,`parse`,{cause:e})}for(let t of n.push(e)){let e=J(t);if(e===G)return;e!==K&&(yield e)}}let i;try{i=t.decode()}catch(e){throw new r(`Invalid UTF-8 in SSE stream`,`parse`,{cause:e})}let{frames:a,trailing:o}=n.end(i);for(let e of a){let t=J(e);if(t===G)return;t!==K&&(yield t)}let s=o.trim();if(s){let e=J(s);e!==G&&e!==K&&(yield e)}}function Le(){let e=[],t=!1,n=!1;function r(r){let i=n?`\r${r}`:r;n=i.endsWith(`\r`),n&&(i=i.slice(0,-1)),i=i.replace(/\r\n?/g,`
2
+ `);let a=[],o=0;t&&i.startsWith(`
3
+ `)&&(a.push(e.join(``).slice(0,-1)),e=[],t=!1,o=1);let s=i.indexOf(`
4
+
5
+ `,o);for(;s!==-1;)e.push(i.slice(o,s)),a.push(e.join(``)),e=[],o=s+2,s=i.indexOf(`
6
+
7
+ `,o);let c=i.slice(o);return c?(e.push(c),t=c.endsWith(`
8
+ `)):o>0&&(t=!1),a}return{push:r,end(t){let i=r(t);return n&&(n=!1,i.push(...r(`
9
+ `))),{frames:i,trailing:e.join(``)}}}}const G=Symbol(`sse-stream-done`),K=Symbol(`sse-frame-no-data`),q=Symbol(`sse-frame-ping`);function J(e){let t=[],n=!1;for(let r of e.split(`
10
+ `)){if(r.startsWith(`:`)){n=!0;continue}r.startsWith(`data:`)&&t.push(r.startsWith(`data: `)?r.slice(6):r.slice(5))}if(!t.length)return n?q:K;let i=t.join(`
11
+ `);if(i===`[DONE]`)return G;try{return JSON.parse(i)}catch(e){throw new r(`Invalid JSON in SSE frame: ${i.slice(0,200)}`,`parse`,{cause:e,code:`stream_frame_invalid`})}}function Re(e){return e.cacheReadTokens!==void 0||e.cacheWriteTokens!==void 0||e.cacheWriteTokensByTtl!==void 0}function ze(e){let{content:t,usage:n,toolCalls:r}=e,i=r?.length?r.map(e=>({id:e.id,type:`function`,function:{name:e.name,arguments:e.arguments}})):void 0;return{choices:[{message:{content:t,...i?{tool_calls:i}:{}}}],usage:n?{prompt_tokens:n.promptTokens,completion_tokens:n.completionTokens,total_tokens:n.totalTokens,...Re(n)?{prompt_tokens_details:{cached_tokens:n.cacheReadTokens,cache_write_tokens:n.cacheWriteTokens,cache_write_tokens_by_ttl:n.cacheWriteTokensByTtl}}:{}}:void 0}}async function*Be(e,t){for await(let n of e){if(n===q){yield{type:`ping`};continue}let e=t(n);e&&(Array.isArray(e)?yield*e:yield e)}}async function*Ve(e){let t=e.getReader();try{for(;;){let{done:e,value:n}=await t.read();if(e)return;n&&(yield n)}}finally{try{await t.cancel()}catch{}t.releaseLock()}}async function He(e,t){let n=await fetch(e,t);if(!n.ok)throw T(`Fetch adapter stream request failed`,n,await n.text().catch(()=>``));if(!n.body)throw Error(`Fetch adapter stream request received a response with no body.`);return{headers:n.headers,body:Ve(n.body)}}async function Y(e,t,n){let r=typeof e.url==`function`?e.url(t):e.url,i=typeof e.headers==`function`?await e.headers():e.headers,a=e.method??`POST`,o=![`GET`,`HEAD`].includes(a.toUpperCase());return{url:r,method:a,headers:o?{"Content-Type":`application/json`,...i}:{...i},...o?{body:JSON.stringify(n)}:{}}}async function Ue(e,t,n){let{url:r,method:i,headers:a,body:o}=await Y(e,t,e.mapRequest(t)),s=await(e.request??fetch)(r,{method:i,headers:a,body:o,signal:n});if(!s.ok)throw T(`Fetch adapter request failed`,s,await s.text().catch(()=>``));return s}async function We(e,t,n){if(e.request&&!e.requestStream)throw w("`stream: true` requires `requestStream` to be configured on fromFetch when a custom `request` transport is set. `requestStream` does not fall back to `request` (it needs an async-iterable byte stream, which `RequestLike`'s buffered `ResponseLike` has no way to provide), without it, `stream: true` would silently use plain native `fetch` instead of your configured transport. Add a `requestStream` that opens the same connection your `request` does, or omit `request` if native `fetch` is fine for both.",`requestStream`);let{url:r,method:i,headers:a,body:o}=await Y(e,t,e.mapRequest(t)),s={method:i,headers:a,body:o,signal:n};return e.requestStream?{body:await e.requestStream(r,s)}:He(r,s)}function X(t,n){if(n&&typeof n.get==`function`)return(t.parseRateLimitHint??e)(n)}function Ge(e){return{adapter:{name:`fetch`,...typeof e.provider==`string`&&e.provider.trim()!==``?{provider:e.provider}:{}},chat:{completions:{async create(t,r){let i=await Ue(e,t,r.signal),a=ze(e.mapResponse(await i.json()));return n(a,X(e,i.headers)),a},async*createStream(t,n){let{mapStreamEvent:r}=e;if(!r)throw w(`stream: true requires mapStreamEvent to be configured on fromFetch`,`mapStreamEvent`);let i=e.parseStreamFrames??W,a=await We(e,t,n.signal),s=X(e,a.headers),c=s&&o(s);c&&(yield c),yield*Be(i(a.body),r)}}}}}const Ke=[[/^api\.openai\.com$/,`openai`],[/\.openai\.azure\.com$/,`azure.ai.openai`],[/^api\.groq\.com$/,`groq`],[/^api\.mistral\.ai$/,`mistral_ai`],[/^api\.deepseek\.com$/,`deepseek`],[/^api\.x\.ai$/,`x_ai`],[/^api\.perplexity\.ai$/,`perplexity`]];function qe(e,t){if(typeof t==`string`&&t.trim()!==``)return t;let n=e.baseURL;if(typeof n!=`string`)return;let r;try{r=new URL(n).hostname.toLowerCase()}catch{return}return Ke.find(([e])=>e.test(r))?.[1]}function Je(e){if(/-chat/.test(e))return!1;let t=/^gpt-(\d+)/.exec(e)?.[1];return t!==void 0&&Number(t)>=6}function Ye(e,t){return t?Array.isArray(t)?t.includes(e):t(e):Je(e)}function Xe(e,t){if(!e.tools?.length||!Ye(e.model,t))return!1;if(e.reasoning_effort!==void 0||e.budget_tokens!==void 0)throw w(`OpenAI model "${e.model}" only accepts tools on Chat Completions with reasoning_effort "none", but this call asks for reasoning through reasoningEffort or budgetTokens (an instance default counts). Pass reasoningEffort: null and budgetTokens: null for this call, or use the Responses API for reasoning with tools.`,`tools_with_reasoning`);return!0}function Ze(e,t,n){return t?(n?.debug(`[VernLLM] ${e.model}: sending reasoning_effort "none", required for tools on Chat Completions`),{...e,reasoning_effort:`none`}):e}function Qe(e,t){if(e.budget_tokens===void 0)return e;let{budget_tokens:n,...r}=e;return r.reasoning_effort===void 0?{...r,reasoning_effort:d(n,t)}:r}function $e(e){if(/-chat/.test(e))return!1;if(/^o\d/.test(e))return!0;let t=/^gpt-(\d+)/.exec(e)?.[1];return t!==void 0&&Number(t)>=5}function et(e){if(!$e(e.model))return e;let{max_tokens:t,temperature:n,...r}=e;return{...r,max_completion_tokens:t}}function tt(e){return e.map(e=>e.type===`image`?{type:`image_url`,image_url:{url:`data:${A(e.mimeType)};base64,${e.data}`}}:{type:`text`,text:e.text})}function nt(e){return e.messages.map(e=>{if(e.role===`user`&&Array.isArray(e.content))return{...e,content:tt(e.content)};if(e.role===`tool`){let{is_error:t,...n}=e;return t?{...n,content:`Error: ${n.content}`}:n}if(e.role===`assistant`&&e.thinking){let{thinking:t,...n}=e;return n}return e})}function rt(e,t){return t?.type===`json_object`?e.some(e=>{let t=e.content;return(Array.isArray(t)?t:[t]).some(e=>{let t=typeof e==`string`?e:e?.text;return typeof t==`string`&&/json/i.test(t)})})?e:[{role:`system`,content:`Respond with a valid JSON object.`},...e]:e}function Z(e){let t=e.prompt_tokens_details?.cached_tokens??e.prompt_cache_hit_tokens;return t===void 0?e:{...e,prompt_tokens_details:{...e.prompt_tokens_details,cached_tokens:t}}}function*it(e){let t=e.choices?.[0]?.delta;if(t?.content&&(yield{type:`text-delta`,delta:t.content}),t?.tool_calls?.length)for(let e of t.tool_calls)yield{type:`tool_call_delta`,index:e.index,id:e.id,name:e.function?.name,argumentsDelta:e.function?.arguments};e.usage&&(yield{type:`usage`,usage:Z(e.usage)})}function Q(t,r={}){let i=t,{supportsStreamUsage:a=!0,supportsWithResponse:c=!1}=r,u=l(r.reasoningEffortTokens),d=qe(t,r.provider),f,p=S(`openai-compatible`,()=>C(t),`Pass maxRetries: 0 to the client`),m=(e,t={})=>{let n=Xe(e,r.noReasoningToolModels),i=rt(nt(e),e.response_format);return Ze(et(Qe({...e,messages:i,...t},u)),n,f)},h=(e,t)=>s(()=>i.chat.completions.create(e,t),c);return{adapter:{name:`openai-compatible`,...d?{provider:d}:{}},setLogger(e){f=e,p(e)},chat:{completions:{async create(t,r){let{data:i,headers:a}=await h(m(t),r),o=i?.usage?{...i,usage:Z(i.usage)}:i;return a&&o&&typeof o==`object`&&n(o,e(a)),o},async*createStream(t,n){let r=m(t,{stream:!0,...a?{stream_options:{include_usage:!0}}:{}}),{data:i,headers:s}=await h(r,n);if(s){let t=o(e(s));t&&(yield t)}for await(let e of i)yield*it(e)}}}}}const at=Q,ot=Q,st=Q,ct=Q,lt=Q,ut=Q,dt=Q,ft=Q,pt=Q,mt=Q,ht=Q,gt=Q,_t=Q,vt=Q,yt=Q,bt=Q,xt=Q,St=Q,Ct=Q,wt=Q,Tt=Q,Et=Q,$=Q,Dt=Q,Ot=Q,kt=Q,At=Q,jt=Q,Mt=Q,Nt=Q,Pt=Q,Ft=Q,It=Q,Lt=Q,Rt=Q,zt=Q,Bt=Q,Vt=Q;export{q as SSE_PING,Vt as from01AI,Ce as fromAnthropic,It as fromAnyscale,Bt as fromAtlasCloud,Dt as fromBaseten,lt as fromCerebras,Tt as fromCloudflareWorkersAI,ht as fromDeepInfra,ct as fromDeepSeek,Ot as fromFeatherless,Ge as fromFetch,dt as fromFireworks,kt as fromFriendli,Ie as fromGemini,ot as fromGroq,_t as fromHyperbolic,Rt as fromInferenceNet,zt as fromInfermatic,bt as fromLMStudio,Pt as fromLambdaLabs,Lt as fromLepton,Nt as fromMiniMax,st as fromMistral,vt as fromMoonshot,Et as fromNebius,gt as fromNovita,Ct as fromNvidiaNIM,ft as fromOllama,at as fromOpenAI,Q as fromOpenAICompatible,pt as fromOpenRouter,jt as fromParasail,mt as fromPerplexity,$ as fromSambaNova,At as fromSiliconFlow,Ft as fromSnowflakeCortex,Mt as fromStepFun,ut as fromTogether,xt as fromVLLM,wt as fromVercelAIGateway,St as fromXAI,yt as fromZhipu,W as parseSseStream};
12
+ //# sourceMappingURL=index.mjs.map