@ai-sdk/openai-compatible 3.0.0-beta.3 → 3.0.0-beta.30

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.mjs DELETED
@@ -1,1742 +0,0 @@
1
- // src/chat/openai-compatible-chat-language-model.ts
2
- import {
3
- InvalidResponseDataError
4
- } from "@ai-sdk/provider";
5
- import {
6
- combineHeaders,
7
- createEventSourceResponseHandler,
8
- createJsonErrorResponseHandler,
9
- createJsonResponseHandler,
10
- generateId,
11
- isParsableJson,
12
- parseProviderOptions,
13
- postJsonToApi
14
- } from "@ai-sdk/provider-utils";
15
- import { z as z3 } from "zod/v4";
16
-
17
- // src/openai-compatible-error.ts
18
- import { z } from "zod/v4";
19
- var openaiCompatibleErrorDataSchema = z.object({
20
- error: z.object({
21
- message: z.string(),
22
- // The additional information below is handled loosely to support
23
- // OpenAI-compatible providers that have slightly different error
24
- // responses:
25
- type: z.string().nullish(),
26
- param: z.any().nullish(),
27
- code: z.union([z.string(), z.number()]).nullish()
28
- })
29
- });
30
- var defaultOpenAICompatibleErrorStructure = {
31
- errorSchema: openaiCompatibleErrorDataSchema,
32
- errorToMessage: (data) => data.error.message
33
- };
34
-
35
- // src/chat/convert-openai-compatible-chat-usage.ts
36
- function convertOpenAICompatibleChatUsage(usage) {
37
- var _a, _b, _c, _d, _e, _f;
38
- if (usage == null) {
39
- return {
40
- inputTokens: {
41
- total: void 0,
42
- noCache: void 0,
43
- cacheRead: void 0,
44
- cacheWrite: void 0
45
- },
46
- outputTokens: {
47
- total: void 0,
48
- text: void 0,
49
- reasoning: void 0
50
- },
51
- raw: void 0
52
- };
53
- }
54
- const promptTokens = (_a = usage.prompt_tokens) != null ? _a : 0;
55
- const completionTokens = (_b = usage.completion_tokens) != null ? _b : 0;
56
- const cacheReadTokens = (_d = (_c = usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : 0;
57
- const reasoningTokens = (_f = (_e = usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : 0;
58
- return {
59
- inputTokens: {
60
- total: promptTokens,
61
- noCache: promptTokens - cacheReadTokens,
62
- cacheRead: cacheReadTokens,
63
- cacheWrite: void 0
64
- },
65
- outputTokens: {
66
- total: completionTokens,
67
- text: completionTokens - reasoningTokens,
68
- reasoning: reasoningTokens
69
- },
70
- raw: usage
71
- };
72
- }
73
-
74
- // src/chat/convert-to-openai-compatible-chat-messages.ts
75
- import {
76
- UnsupportedFunctionalityError
77
- } from "@ai-sdk/provider";
78
- import {
79
- convertBase64ToUint8Array,
80
- convertToBase64
81
- } from "@ai-sdk/provider-utils";
82
- function getOpenAIMetadata(message) {
83
- var _a, _b;
84
- return (_b = (_a = message == null ? void 0 : message.providerOptions) == null ? void 0 : _a.openaiCompatible) != null ? _b : {};
85
- }
86
- function getAudioFormat(mediaType) {
87
- switch (mediaType) {
88
- case "audio/wav":
89
- return "wav";
90
- case "audio/mp3":
91
- case "audio/mpeg":
92
- return "mp3";
93
- default:
94
- return null;
95
- }
96
- }
97
- function convertToOpenAICompatibleChatMessages(prompt) {
98
- var _a, _b, _c;
99
- const messages = [];
100
- for (const { role, content, ...message } of prompt) {
101
- const metadata = getOpenAIMetadata({ ...message });
102
- switch (role) {
103
- case "system": {
104
- messages.push({ role: "system", content, ...metadata });
105
- break;
106
- }
107
- case "user": {
108
- if (content.length === 1 && content[0].type === "text") {
109
- messages.push({
110
- role: "user",
111
- content: content[0].text,
112
- ...getOpenAIMetadata(content[0])
113
- });
114
- break;
115
- }
116
- messages.push({
117
- role: "user",
118
- content: content.map((part) => {
119
- var _a2;
120
- const partMetadata = getOpenAIMetadata(part);
121
- switch (part.type) {
122
- case "text": {
123
- return { type: "text", text: part.text, ...partMetadata };
124
- }
125
- case "file": {
126
- if (part.mediaType.startsWith("image/")) {
127
- const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
128
- return {
129
- type: "image_url",
130
- image_url: {
131
- url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`
132
- },
133
- ...partMetadata
134
- };
135
- }
136
- if (part.mediaType.startsWith("audio/")) {
137
- if (part.data instanceof URL) {
138
- throw new UnsupportedFunctionalityError({
139
- functionality: "audio file parts with URLs"
140
- });
141
- }
142
- const format = getAudioFormat(part.mediaType);
143
- if (format === null) {
144
- throw new UnsupportedFunctionalityError({
145
- functionality: `audio media type ${part.mediaType}`
146
- });
147
- }
148
- return {
149
- type: "input_audio",
150
- input_audio: {
151
- data: convertToBase64(part.data),
152
- format
153
- },
154
- ...partMetadata
155
- };
156
- }
157
- if (part.mediaType === "application/pdf") {
158
- if (part.data instanceof URL) {
159
- throw new UnsupportedFunctionalityError({
160
- functionality: "PDF file parts with URLs"
161
- });
162
- }
163
- return {
164
- type: "file",
165
- file: {
166
- filename: (_a2 = part.filename) != null ? _a2 : "document.pdf",
167
- file_data: `data:application/pdf;base64,${convertToBase64(part.data)}`
168
- },
169
- ...partMetadata
170
- };
171
- }
172
- if (part.mediaType.startsWith("text/")) {
173
- const textContent = part.data instanceof URL ? part.data.toString() : typeof part.data === "string" ? new TextDecoder().decode(
174
- convertBase64ToUint8Array(part.data)
175
- ) : new TextDecoder().decode(part.data);
176
- return {
177
- type: "text",
178
- text: textContent,
179
- ...partMetadata
180
- };
181
- }
182
- throw new UnsupportedFunctionalityError({
183
- functionality: `file part media type ${part.mediaType}`
184
- });
185
- }
186
- }
187
- }),
188
- ...metadata
189
- });
190
- break;
191
- }
192
- case "assistant": {
193
- let text = "";
194
- let reasoning = "";
195
- const toolCalls = [];
196
- for (const part of content) {
197
- const partMetadata = getOpenAIMetadata(part);
198
- switch (part.type) {
199
- case "text": {
200
- text += part.text;
201
- break;
202
- }
203
- case "reasoning": {
204
- reasoning += part.text;
205
- break;
206
- }
207
- case "tool-call": {
208
- const thoughtSignature = (_b = (_a = part.providerOptions) == null ? void 0 : _a.google) == null ? void 0 : _b.thoughtSignature;
209
- toolCalls.push({
210
- id: part.toolCallId,
211
- type: "function",
212
- function: {
213
- name: part.toolName,
214
- arguments: JSON.stringify(part.input)
215
- },
216
- ...partMetadata,
217
- // Include extra_content for Google Gemini thought signatures
218
- ...thoughtSignature ? {
219
- extra_content: {
220
- google: {
221
- thought_signature: String(thoughtSignature)
222
- }
223
- }
224
- } : {}
225
- });
226
- break;
227
- }
228
- }
229
- }
230
- messages.push({
231
- role: "assistant",
232
- content: text,
233
- ...reasoning.length > 0 ? { reasoning_content: reasoning } : {},
234
- tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
235
- ...metadata
236
- });
237
- break;
238
- }
239
- case "tool": {
240
- for (const toolResponse of content) {
241
- if (toolResponse.type === "tool-approval-response") {
242
- continue;
243
- }
244
- const output = toolResponse.output;
245
- let contentValue;
246
- switch (output.type) {
247
- case "text":
248
- case "error-text":
249
- contentValue = output.value;
250
- break;
251
- case "execution-denied":
252
- contentValue = (_c = output.reason) != null ? _c : "Tool execution denied.";
253
- break;
254
- case "content":
255
- case "json":
256
- case "error-json":
257
- contentValue = JSON.stringify(output.value);
258
- break;
259
- }
260
- const toolResponseMetadata = getOpenAIMetadata(toolResponse);
261
- messages.push({
262
- role: "tool",
263
- tool_call_id: toolResponse.toolCallId,
264
- content: contentValue,
265
- ...toolResponseMetadata
266
- });
267
- }
268
- break;
269
- }
270
- default: {
271
- const _exhaustiveCheck = role;
272
- throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
273
- }
274
- }
275
- }
276
- return messages;
277
- }
278
-
279
- // src/chat/get-response-metadata.ts
280
- function getResponseMetadata({
281
- id,
282
- model,
283
- created
284
- }) {
285
- return {
286
- id: id != null ? id : void 0,
287
- modelId: model != null ? model : void 0,
288
- timestamp: created != null ? new Date(created * 1e3) : void 0
289
- };
290
- }
291
-
292
- // src/chat/map-openai-compatible-finish-reason.ts
293
- function mapOpenAICompatibleFinishReason(finishReason) {
294
- switch (finishReason) {
295
- case "stop":
296
- return "stop";
297
- case "length":
298
- return "length";
299
- case "content_filter":
300
- return "content-filter";
301
- case "function_call":
302
- case "tool_calls":
303
- return "tool-calls";
304
- default:
305
- return "other";
306
- }
307
- }
308
-
309
- // src/chat/openai-compatible-chat-options.ts
310
- import { z as z2 } from "zod/v4";
311
- var openaiCompatibleLanguageModelChatOptions = z2.object({
312
- /**
313
- * A unique identifier representing your end-user, which can help the provider to
314
- * monitor and detect abuse.
315
- */
316
- user: z2.string().optional(),
317
- /**
318
- * Reasoning effort for reasoning models. Defaults to `medium`.
319
- */
320
- reasoningEffort: z2.string().optional(),
321
- /**
322
- * Controls the verbosity of the generated text. Defaults to `medium`.
323
- */
324
- textVerbosity: z2.string().optional(),
325
- /**
326
- * Whether to use strict JSON schema validation.
327
- * When true, the model uses constrained decoding to guarantee schema compliance.
328
- * Only used when the provider supports structured outputs and a schema is provided.
329
- *
330
- * @default true
331
- */
332
- strictJsonSchema: z2.boolean().optional()
333
- });
334
-
335
- // src/chat/openai-compatible-prepare-tools.ts
336
- import {
337
- UnsupportedFunctionalityError as UnsupportedFunctionalityError2
338
- } from "@ai-sdk/provider";
339
- function prepareTools({
340
- tools,
341
- toolChoice
342
- }) {
343
- tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
344
- const toolWarnings = [];
345
- if (tools == null) {
346
- return { tools: void 0, toolChoice: void 0, toolWarnings };
347
- }
348
- const openaiCompatTools = [];
349
- for (const tool of tools) {
350
- if (tool.type === "provider") {
351
- toolWarnings.push({
352
- type: "unsupported",
353
- feature: `provider-defined tool ${tool.id}`
354
- });
355
- } else {
356
- openaiCompatTools.push({
357
- type: "function",
358
- function: {
359
- name: tool.name,
360
- description: tool.description,
361
- parameters: tool.inputSchema,
362
- ...tool.strict != null ? { strict: tool.strict } : {}
363
- }
364
- });
365
- }
366
- }
367
- if (toolChoice == null) {
368
- return { tools: openaiCompatTools, toolChoice: void 0, toolWarnings };
369
- }
370
- const type = toolChoice.type;
371
- switch (type) {
372
- case "auto":
373
- case "none":
374
- case "required":
375
- return { tools: openaiCompatTools, toolChoice: type, toolWarnings };
376
- case "tool":
377
- return {
378
- tools: openaiCompatTools,
379
- toolChoice: {
380
- type: "function",
381
- function: { name: toolChoice.toolName }
382
- },
383
- toolWarnings
384
- };
385
- default: {
386
- const _exhaustiveCheck = type;
387
- throw new UnsupportedFunctionalityError2({
388
- functionality: `tool choice type: ${_exhaustiveCheck}`
389
- });
390
- }
391
- }
392
- }
393
-
394
- // src/chat/openai-compatible-chat-language-model.ts
395
- var OpenAICompatibleChatLanguageModel = class {
396
- // type inferred via constructor
397
- constructor(modelId, config) {
398
- this.specificationVersion = "v4";
399
- var _a, _b;
400
- this.modelId = modelId;
401
- this.config = config;
402
- const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
403
- this.chunkSchema = createOpenAICompatibleChatChunkSchema(
404
- errorStructure.errorSchema
405
- );
406
- this.failedResponseHandler = createJsonErrorResponseHandler(errorStructure);
407
- this.supportsStructuredOutputs = (_b = config.supportsStructuredOutputs) != null ? _b : false;
408
- }
409
- get provider() {
410
- return this.config.provider;
411
- }
412
- get providerOptionsName() {
413
- return this.config.provider.split(".")[0].trim();
414
- }
415
- get supportedUrls() {
416
- var _a, _b, _c;
417
- return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
418
- }
419
- transformRequestBody(args) {
420
- var _a, _b, _c;
421
- return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
422
- }
423
- async getArgs({
424
- prompt,
425
- maxOutputTokens,
426
- temperature,
427
- topP,
428
- topK,
429
- frequencyPenalty,
430
- presencePenalty,
431
- providerOptions,
432
- stopSequences,
433
- responseFormat,
434
- seed,
435
- toolChoice,
436
- tools
437
- }) {
438
- var _a, _b, _c, _d, _e;
439
- const warnings = [];
440
- const deprecatedOptions = await parseProviderOptions({
441
- provider: "openai-compatible",
442
- providerOptions,
443
- schema: openaiCompatibleLanguageModelChatOptions
444
- });
445
- if (deprecatedOptions != null) {
446
- warnings.push({
447
- type: "other",
448
- message: `The 'openai-compatible' key in providerOptions is deprecated. Use 'openaiCompatible' instead.`
449
- });
450
- }
451
- const compatibleOptions = Object.assign(
452
- deprecatedOptions != null ? deprecatedOptions : {},
453
- (_a = await parseProviderOptions({
454
- provider: "openaiCompatible",
455
- providerOptions,
456
- schema: openaiCompatibleLanguageModelChatOptions
457
- })) != null ? _a : {},
458
- (_b = await parseProviderOptions({
459
- provider: this.providerOptionsName,
460
- providerOptions,
461
- schema: openaiCompatibleLanguageModelChatOptions
462
- })) != null ? _b : {}
463
- );
464
- const strictJsonSchema = (_c = compatibleOptions == null ? void 0 : compatibleOptions.strictJsonSchema) != null ? _c : true;
465
- if (topK != null) {
466
- warnings.push({ type: "unsupported", feature: "topK" });
467
- }
468
- if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.supportsStructuredOutputs) {
469
- warnings.push({
470
- type: "unsupported",
471
- feature: "responseFormat",
472
- details: "JSON response format schema is only supported with structuredOutputs"
473
- });
474
- }
475
- const {
476
- tools: openaiTools,
477
- toolChoice: openaiToolChoice,
478
- toolWarnings
479
- } = prepareTools({
480
- tools,
481
- toolChoice
482
- });
483
- return {
484
- args: {
485
- // model id:
486
- model: this.modelId,
487
- // model specific settings:
488
- user: compatibleOptions.user,
489
- // standardized settings:
490
- max_tokens: maxOutputTokens,
491
- temperature,
492
- top_p: topP,
493
- frequency_penalty: frequencyPenalty,
494
- presence_penalty: presencePenalty,
495
- response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? this.supportsStructuredOutputs === true && responseFormat.schema != null ? {
496
- type: "json_schema",
497
- json_schema: {
498
- schema: responseFormat.schema,
499
- strict: strictJsonSchema,
500
- name: (_d = responseFormat.name) != null ? _d : "response",
501
- description: responseFormat.description
502
- }
503
- } : { type: "json_object" } : void 0,
504
- stop: stopSequences,
505
- seed,
506
- ...Object.fromEntries(
507
- Object.entries(
508
- (_e = providerOptions == null ? void 0 : providerOptions[this.providerOptionsName]) != null ? _e : {}
509
- ).filter(
510
- ([key]) => !Object.keys(
511
- openaiCompatibleLanguageModelChatOptions.shape
512
- ).includes(key)
513
- )
514
- ),
515
- reasoning_effort: compatibleOptions.reasoningEffort,
516
- verbosity: compatibleOptions.textVerbosity,
517
- // messages:
518
- messages: convertToOpenAICompatibleChatMessages(prompt),
519
- // tools:
520
- tools: openaiTools,
521
- tool_choice: openaiToolChoice
522
- },
523
- warnings: [...warnings, ...toolWarnings]
524
- };
525
- }
526
- async doGenerate(options) {
527
- var _a, _b, _c, _d, _e, _f, _g, _h;
528
- const { args, warnings } = await this.getArgs({ ...options });
529
- const transformedBody = this.transformRequestBody(args);
530
- const body = JSON.stringify(transformedBody);
531
- const {
532
- responseHeaders,
533
- value: responseBody,
534
- rawValue: rawResponse
535
- } = await postJsonToApi({
536
- url: this.config.url({
537
- path: "/chat/completions",
538
- modelId: this.modelId
539
- }),
540
- headers: combineHeaders(this.config.headers(), options.headers),
541
- body: transformedBody,
542
- failedResponseHandler: this.failedResponseHandler,
543
- successfulResponseHandler: createJsonResponseHandler(
544
- OpenAICompatibleChatResponseSchema
545
- ),
546
- abortSignal: options.abortSignal,
547
- fetch: this.config.fetch
548
- });
549
- const choice = responseBody.choices[0];
550
- const content = [];
551
- const text = choice.message.content;
552
- if (text != null && text.length > 0) {
553
- content.push({ type: "text", text });
554
- }
555
- const reasoning = (_a = choice.message.reasoning_content) != null ? _a : choice.message.reasoning;
556
- if (reasoning != null && reasoning.length > 0) {
557
- content.push({
558
- type: "reasoning",
559
- text: reasoning
560
- });
561
- }
562
- if (choice.message.tool_calls != null) {
563
- for (const toolCall of choice.message.tool_calls) {
564
- const thoughtSignature = (_c = (_b = toolCall.extra_content) == null ? void 0 : _b.google) == null ? void 0 : _c.thought_signature;
565
- content.push({
566
- type: "tool-call",
567
- toolCallId: (_d = toolCall.id) != null ? _d : generateId(),
568
- toolName: toolCall.function.name,
569
- input: toolCall.function.arguments,
570
- ...thoughtSignature ? {
571
- providerMetadata: {
572
- [this.providerOptionsName]: { thoughtSignature }
573
- }
574
- } : {}
575
- });
576
- }
577
- }
578
- const providerMetadata = {
579
- [this.providerOptionsName]: {},
580
- ...await ((_f = (_e = this.config.metadataExtractor) == null ? void 0 : _e.extractMetadata) == null ? void 0 : _f.call(_e, {
581
- parsedBody: rawResponse
582
- }))
583
- };
584
- const completionTokenDetails = (_g = responseBody.usage) == null ? void 0 : _g.completion_tokens_details;
585
- if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
586
- providerMetadata[this.providerOptionsName].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
587
- }
588
- if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
589
- providerMetadata[this.providerOptionsName].rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
590
- }
591
- return {
592
- content,
593
- finishReason: {
594
- unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
595
- raw: (_h = choice.finish_reason) != null ? _h : void 0
596
- },
597
- usage: convertOpenAICompatibleChatUsage(responseBody.usage),
598
- providerMetadata,
599
- request: { body },
600
- response: {
601
- ...getResponseMetadata(responseBody),
602
- headers: responseHeaders,
603
- body: rawResponse
604
- },
605
- warnings
606
- };
607
- }
608
- async doStream(options) {
609
- var _a;
610
- const { args, warnings } = await this.getArgs({ ...options });
611
- const body = this.transformRequestBody({
612
- ...args,
613
- stream: true,
614
- // only include stream_options when in strict compatibility mode:
615
- stream_options: this.config.includeUsage ? { include_usage: true } : void 0
616
- });
617
- const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
618
- const { responseHeaders, value: response } = await postJsonToApi({
619
- url: this.config.url({
620
- path: "/chat/completions",
621
- modelId: this.modelId
622
- }),
623
- headers: combineHeaders(this.config.headers(), options.headers),
624
- body,
625
- failedResponseHandler: this.failedResponseHandler,
626
- successfulResponseHandler: createEventSourceResponseHandler(
627
- this.chunkSchema
628
- ),
629
- abortSignal: options.abortSignal,
630
- fetch: this.config.fetch
631
- });
632
- const toolCalls = [];
633
- let finishReason = {
634
- unified: "other",
635
- raw: void 0
636
- };
637
- let usage = void 0;
638
- let isFirstChunk = true;
639
- const providerOptionsName = this.providerOptionsName;
640
- let isActiveReasoning = false;
641
- let isActiveText = false;
642
- return {
643
- stream: response.pipeThrough(
644
- new TransformStream({
645
- start(controller) {
646
- controller.enqueue({ type: "stream-start", warnings });
647
- },
648
- transform(chunk, controller) {
649
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
650
- if (options.includeRawChunks) {
651
- controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
652
- }
653
- if (!chunk.success) {
654
- finishReason = { unified: "error", raw: void 0 };
655
- controller.enqueue({ type: "error", error: chunk.error });
656
- return;
657
- }
658
- metadataExtractor == null ? void 0 : metadataExtractor.processChunk(chunk.rawValue);
659
- if ("error" in chunk.value) {
660
- finishReason = { unified: "error", raw: void 0 };
661
- controller.enqueue({
662
- type: "error",
663
- error: chunk.value.error.message
664
- });
665
- return;
666
- }
667
- const value = chunk.value;
668
- if (isFirstChunk) {
669
- isFirstChunk = false;
670
- controller.enqueue({
671
- type: "response-metadata",
672
- ...getResponseMetadata(value)
673
- });
674
- }
675
- if (value.usage != null) {
676
- usage = value.usage;
677
- }
678
- const choice = value.choices[0];
679
- if ((choice == null ? void 0 : choice.finish_reason) != null) {
680
- finishReason = {
681
- unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
682
- raw: (_a2 = choice.finish_reason) != null ? _a2 : void 0
683
- };
684
- }
685
- if ((choice == null ? void 0 : choice.delta) == null) {
686
- return;
687
- }
688
- const delta = choice.delta;
689
- const reasoningContent = (_b = delta.reasoning_content) != null ? _b : delta.reasoning;
690
- if (reasoningContent) {
691
- if (!isActiveReasoning) {
692
- controller.enqueue({
693
- type: "reasoning-start",
694
- id: "reasoning-0"
695
- });
696
- isActiveReasoning = true;
697
- }
698
- controller.enqueue({
699
- type: "reasoning-delta",
700
- id: "reasoning-0",
701
- delta: reasoningContent
702
- });
703
- }
704
- if (delta.content) {
705
- if (isActiveReasoning) {
706
- controller.enqueue({
707
- type: "reasoning-end",
708
- id: "reasoning-0"
709
- });
710
- isActiveReasoning = false;
711
- }
712
- if (!isActiveText) {
713
- controller.enqueue({ type: "text-start", id: "txt-0" });
714
- isActiveText = true;
715
- }
716
- controller.enqueue({
717
- type: "text-delta",
718
- id: "txt-0",
719
- delta: delta.content
720
- });
721
- }
722
- if (delta.tool_calls != null) {
723
- if (isActiveReasoning) {
724
- controller.enqueue({
725
- type: "reasoning-end",
726
- id: "reasoning-0"
727
- });
728
- isActiveReasoning = false;
729
- }
730
- for (const toolCallDelta of delta.tool_calls) {
731
- const index = (_c = toolCallDelta.index) != null ? _c : toolCalls.length;
732
- if (toolCalls[index] == null) {
733
- if (toolCallDelta.id == null) {
734
- throw new InvalidResponseDataError({
735
- data: toolCallDelta,
736
- message: `Expected 'id' to be a string.`
737
- });
738
- }
739
- if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) {
740
- throw new InvalidResponseDataError({
741
- data: toolCallDelta,
742
- message: `Expected 'function.name' to be a string.`
743
- });
744
- }
745
- controller.enqueue({
746
- type: "tool-input-start",
747
- id: toolCallDelta.id,
748
- toolName: toolCallDelta.function.name
749
- });
750
- toolCalls[index] = {
751
- id: toolCallDelta.id,
752
- type: "function",
753
- function: {
754
- name: toolCallDelta.function.name,
755
- arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
756
- },
757
- hasFinished: false,
758
- thoughtSignature: (_h = (_g = (_f = toolCallDelta.extra_content) == null ? void 0 : _f.google) == null ? void 0 : _g.thought_signature) != null ? _h : void 0
759
- };
760
- const toolCall2 = toolCalls[index];
761
- if (((_i = toolCall2.function) == null ? void 0 : _i.name) != null && ((_j = toolCall2.function) == null ? void 0 : _j.arguments) != null) {
762
- if (toolCall2.function.arguments.length > 0) {
763
- controller.enqueue({
764
- type: "tool-input-delta",
765
- id: toolCall2.id,
766
- delta: toolCall2.function.arguments
767
- });
768
- }
769
- if (isParsableJson(toolCall2.function.arguments)) {
770
- controller.enqueue({
771
- type: "tool-input-end",
772
- id: toolCall2.id
773
- });
774
- controller.enqueue({
775
- type: "tool-call",
776
- toolCallId: (_k = toolCall2.id) != null ? _k : generateId(),
777
- toolName: toolCall2.function.name,
778
- input: toolCall2.function.arguments,
779
- ...toolCall2.thoughtSignature ? {
780
- providerMetadata: {
781
- [providerOptionsName]: {
782
- thoughtSignature: toolCall2.thoughtSignature
783
- }
784
- }
785
- } : {}
786
- });
787
- toolCall2.hasFinished = true;
788
- }
789
- }
790
- continue;
791
- }
792
- const toolCall = toolCalls[index];
793
- if (toolCall.hasFinished) {
794
- continue;
795
- }
796
- if (((_l = toolCallDelta.function) == null ? void 0 : _l.arguments) != null) {
797
- toolCall.function.arguments += (_n = (_m = toolCallDelta.function) == null ? void 0 : _m.arguments) != null ? _n : "";
798
- }
799
- controller.enqueue({
800
- type: "tool-input-delta",
801
- id: toolCall.id,
802
- delta: (_o = toolCallDelta.function.arguments) != null ? _o : ""
803
- });
804
- if (((_p = toolCall.function) == null ? void 0 : _p.name) != null && ((_q = toolCall.function) == null ? void 0 : _q.arguments) != null && isParsableJson(toolCall.function.arguments)) {
805
- controller.enqueue({
806
- type: "tool-input-end",
807
- id: toolCall.id
808
- });
809
- controller.enqueue({
810
- type: "tool-call",
811
- toolCallId: (_r = toolCall.id) != null ? _r : generateId(),
812
- toolName: toolCall.function.name,
813
- input: toolCall.function.arguments,
814
- ...toolCall.thoughtSignature ? {
815
- providerMetadata: {
816
- [providerOptionsName]: {
817
- thoughtSignature: toolCall.thoughtSignature
818
- }
819
- }
820
- } : {}
821
- });
822
- toolCall.hasFinished = true;
823
- }
824
- }
825
- }
826
- },
827
- flush(controller) {
828
- var _a2, _b, _c, _d, _e;
829
- if (isActiveReasoning) {
830
- controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
831
- }
832
- if (isActiveText) {
833
- controller.enqueue({ type: "text-end", id: "txt-0" });
834
- }
835
- for (const toolCall of toolCalls.filter(
836
- (toolCall2) => !toolCall2.hasFinished
837
- )) {
838
- controller.enqueue({
839
- type: "tool-input-end",
840
- id: toolCall.id
841
- });
842
- controller.enqueue({
843
- type: "tool-call",
844
- toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
845
- toolName: toolCall.function.name,
846
- input: toolCall.function.arguments,
847
- ...toolCall.thoughtSignature ? {
848
- providerMetadata: {
849
- [providerOptionsName]: {
850
- thoughtSignature: toolCall.thoughtSignature
851
- }
852
- }
853
- } : {}
854
- });
855
- }
856
- const providerMetadata = {
857
- [providerOptionsName]: {},
858
- ...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
859
- };
860
- if (((_b = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _b.accepted_prediction_tokens) != null) {
861
- providerMetadata[providerOptionsName].acceptedPredictionTokens = (_c = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _c.accepted_prediction_tokens;
862
- }
863
- if (((_d = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _d.rejected_prediction_tokens) != null) {
864
- providerMetadata[providerOptionsName].rejectedPredictionTokens = (_e = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _e.rejected_prediction_tokens;
865
- }
866
- controller.enqueue({
867
- type: "finish",
868
- finishReason,
869
- usage: convertOpenAICompatibleChatUsage(usage),
870
- providerMetadata
871
- });
872
- }
873
- })
874
- ),
875
- request: { body },
876
- response: { headers: responseHeaders }
877
- };
878
- }
879
- };
880
- var openaiCompatibleTokenUsageSchema = z3.looseObject({
881
- prompt_tokens: z3.number().nullish(),
882
- completion_tokens: z3.number().nullish(),
883
- total_tokens: z3.number().nullish(),
884
- prompt_tokens_details: z3.object({
885
- cached_tokens: z3.number().nullish()
886
- }).nullish(),
887
- completion_tokens_details: z3.object({
888
- reasoning_tokens: z3.number().nullish(),
889
- accepted_prediction_tokens: z3.number().nullish(),
890
- rejected_prediction_tokens: z3.number().nullish()
891
- }).nullish()
892
- }).nullish();
893
- var OpenAICompatibleChatResponseSchema = z3.looseObject({
894
- id: z3.string().nullish(),
895
- created: z3.number().nullish(),
896
- model: z3.string().nullish(),
897
- choices: z3.array(
898
- z3.object({
899
- message: z3.object({
900
- role: z3.literal("assistant").nullish(),
901
- content: z3.string().nullish(),
902
- reasoning_content: z3.string().nullish(),
903
- reasoning: z3.string().nullish(),
904
- tool_calls: z3.array(
905
- z3.object({
906
- id: z3.string().nullish(),
907
- function: z3.object({
908
- name: z3.string(),
909
- arguments: z3.string()
910
- }),
911
- // Support for Google Gemini thought signatures via OpenAI compatibility
912
- extra_content: z3.object({
913
- google: z3.object({
914
- thought_signature: z3.string().nullish()
915
- }).nullish()
916
- }).nullish()
917
- })
918
- ).nullish()
919
- }),
920
- finish_reason: z3.string().nullish()
921
- })
922
- ),
923
- usage: openaiCompatibleTokenUsageSchema
924
- });
925
- var chunkBaseSchema = z3.looseObject({
926
- id: z3.string().nullish(),
927
- created: z3.number().nullish(),
928
- model: z3.string().nullish(),
929
- choices: z3.array(
930
- z3.object({
931
- delta: z3.object({
932
- role: z3.enum(["assistant"]).nullish(),
933
- content: z3.string().nullish(),
934
- // Most openai-compatible models set `reasoning_content`, but some
935
- // providers serving `gpt-oss` set `reasoning`. See #7866
936
- reasoning_content: z3.string().nullish(),
937
- reasoning: z3.string().nullish(),
938
- tool_calls: z3.array(
939
- z3.object({
940
- index: z3.number().nullish(),
941
- //google does not send index
942
- id: z3.string().nullish(),
943
- function: z3.object({
944
- name: z3.string().nullish(),
945
- arguments: z3.string().nullish()
946
- }),
947
- // Support for Google Gemini thought signatures via OpenAI compatibility
948
- extra_content: z3.object({
949
- google: z3.object({
950
- thought_signature: z3.string().nullish()
951
- }).nullish()
952
- }).nullish()
953
- })
954
- ).nullish()
955
- }).nullish(),
956
- finish_reason: z3.string().nullish()
957
- })
958
- ),
959
- usage: openaiCompatibleTokenUsageSchema
960
- });
961
- var createOpenAICompatibleChatChunkSchema = (errorSchema) => z3.union([chunkBaseSchema, errorSchema]);
962
-
963
- // src/completion/openai-compatible-completion-language-model.ts
964
- import {
965
- combineHeaders as combineHeaders2,
966
- createEventSourceResponseHandler as createEventSourceResponseHandler2,
967
- createJsonErrorResponseHandler as createJsonErrorResponseHandler2,
968
- createJsonResponseHandler as createJsonResponseHandler2,
969
- parseProviderOptions as parseProviderOptions2,
970
- postJsonToApi as postJsonToApi2
971
- } from "@ai-sdk/provider-utils";
972
- import { z as z5 } from "zod/v4";
973
-
974
- // src/completion/convert-openai-compatible-completion-usage.ts
975
- function convertOpenAICompatibleCompletionUsage(usage) {
976
- var _a, _b;
977
- if (usage == null) {
978
- return {
979
- inputTokens: {
980
- total: void 0,
981
- noCache: void 0,
982
- cacheRead: void 0,
983
- cacheWrite: void 0
984
- },
985
- outputTokens: {
986
- total: void 0,
987
- text: void 0,
988
- reasoning: void 0
989
- },
990
- raw: void 0
991
- };
992
- }
993
- const promptTokens = (_a = usage.prompt_tokens) != null ? _a : 0;
994
- const completionTokens = (_b = usage.completion_tokens) != null ? _b : 0;
995
- return {
996
- inputTokens: {
997
- total: promptTokens,
998
- noCache: promptTokens,
999
- cacheRead: void 0,
1000
- cacheWrite: void 0
1001
- },
1002
- outputTokens: {
1003
- total: completionTokens,
1004
- text: completionTokens,
1005
- reasoning: void 0
1006
- },
1007
- raw: usage
1008
- };
1009
- }
1010
-
1011
- // src/completion/convert-to-openai-compatible-completion-prompt.ts
1012
- import {
1013
- InvalidPromptError,
1014
- UnsupportedFunctionalityError as UnsupportedFunctionalityError3
1015
- } from "@ai-sdk/provider";
1016
- function convertToOpenAICompatibleCompletionPrompt({
1017
- prompt,
1018
- user = "user",
1019
- assistant = "assistant"
1020
- }) {
1021
- let text = "";
1022
- if (prompt[0].role === "system") {
1023
- text += `${prompt[0].content}
1024
-
1025
- `;
1026
- prompt = prompt.slice(1);
1027
- }
1028
- for (const { role, content } of prompt) {
1029
- switch (role) {
1030
- case "system": {
1031
- throw new InvalidPromptError({
1032
- message: "Unexpected system message in prompt: ${content}",
1033
- prompt
1034
- });
1035
- }
1036
- case "user": {
1037
- const userMessage = content.map((part) => {
1038
- switch (part.type) {
1039
- case "text": {
1040
- return part.text;
1041
- }
1042
- }
1043
- }).filter(Boolean).join("");
1044
- text += `${user}:
1045
- ${userMessage}
1046
-
1047
- `;
1048
- break;
1049
- }
1050
- case "assistant": {
1051
- const assistantMessage = content.map((part) => {
1052
- switch (part.type) {
1053
- case "text": {
1054
- return part.text;
1055
- }
1056
- case "tool-call": {
1057
- throw new UnsupportedFunctionalityError3({
1058
- functionality: "tool-call messages"
1059
- });
1060
- }
1061
- }
1062
- }).join("");
1063
- text += `${assistant}:
1064
- ${assistantMessage}
1065
-
1066
- `;
1067
- break;
1068
- }
1069
- case "tool": {
1070
- throw new UnsupportedFunctionalityError3({
1071
- functionality: "tool messages"
1072
- });
1073
- }
1074
- default: {
1075
- const _exhaustiveCheck = role;
1076
- throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
1077
- }
1078
- }
1079
- }
1080
- text += `${assistant}:
1081
- `;
1082
- return {
1083
- prompt: text,
1084
- stopSequences: [`
1085
- ${user}:`]
1086
- };
1087
- }
1088
-
1089
- // src/completion/get-response-metadata.ts
1090
- function getResponseMetadata2({
1091
- id,
1092
- model,
1093
- created
1094
- }) {
1095
- return {
1096
- id: id != null ? id : void 0,
1097
- modelId: model != null ? model : void 0,
1098
- timestamp: created != null ? new Date(created * 1e3) : void 0
1099
- };
1100
- }
1101
-
1102
- // src/completion/map-openai-compatible-finish-reason.ts
1103
- function mapOpenAICompatibleFinishReason2(finishReason) {
1104
- switch (finishReason) {
1105
- case "stop":
1106
- return "stop";
1107
- case "length":
1108
- return "length";
1109
- case "content_filter":
1110
- return "content-filter";
1111
- case "function_call":
1112
- case "tool_calls":
1113
- return "tool-calls";
1114
- default:
1115
- return "other";
1116
- }
1117
- }
1118
-
1119
- // src/completion/openai-compatible-completion-options.ts
1120
- import { z as z4 } from "zod/v4";
1121
- var openaiCompatibleLanguageModelCompletionOptions = z4.object({
1122
- /**
1123
- * Echo back the prompt in addition to the completion.
1124
- */
1125
- echo: z4.boolean().optional(),
1126
- /**
1127
- * Modify the likelihood of specified tokens appearing in the completion.
1128
- *
1129
- * Accepts a JSON object that maps tokens (specified by their token ID in
1130
- * the GPT tokenizer) to an associated bias value from -100 to 100.
1131
- */
1132
- logitBias: z4.record(z4.string(), z4.number()).optional(),
1133
- /**
1134
- * The suffix that comes after a completion of inserted text.
1135
- */
1136
- suffix: z4.string().optional(),
1137
- /**
1138
- * A unique identifier representing your end-user, which can help providers to
1139
- * monitor and detect abuse.
1140
- */
1141
- user: z4.string().optional()
1142
- });
1143
-
1144
- // src/completion/openai-compatible-completion-language-model.ts
1145
- var OpenAICompatibleCompletionLanguageModel = class {
1146
- // type inferred via constructor
1147
- constructor(modelId, config) {
1148
- this.specificationVersion = "v4";
1149
- var _a;
1150
- this.modelId = modelId;
1151
- this.config = config;
1152
- const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
1153
- this.chunkSchema = createOpenAICompatibleCompletionChunkSchema(
1154
- errorStructure.errorSchema
1155
- );
1156
- this.failedResponseHandler = createJsonErrorResponseHandler2(errorStructure);
1157
- }
1158
- get provider() {
1159
- return this.config.provider;
1160
- }
1161
- get providerOptionsName() {
1162
- return this.config.provider.split(".")[0].trim();
1163
- }
1164
- get supportedUrls() {
1165
- var _a, _b, _c;
1166
- return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
1167
- }
1168
- async getArgs({
1169
- prompt,
1170
- maxOutputTokens,
1171
- temperature,
1172
- topP,
1173
- topK,
1174
- frequencyPenalty,
1175
- presencePenalty,
1176
- stopSequences: userStopSequences,
1177
- responseFormat,
1178
- seed,
1179
- providerOptions,
1180
- tools,
1181
- toolChoice
1182
- }) {
1183
- var _a;
1184
- const warnings = [];
1185
- const completionOptions = (_a = await parseProviderOptions2({
1186
- provider: this.providerOptionsName,
1187
- providerOptions,
1188
- schema: openaiCompatibleLanguageModelCompletionOptions
1189
- })) != null ? _a : {};
1190
- if (topK != null) {
1191
- warnings.push({ type: "unsupported", feature: "topK" });
1192
- }
1193
- if (tools == null ? void 0 : tools.length) {
1194
- warnings.push({ type: "unsupported", feature: "tools" });
1195
- }
1196
- if (toolChoice != null) {
1197
- warnings.push({ type: "unsupported", feature: "toolChoice" });
1198
- }
1199
- if (responseFormat != null && responseFormat.type !== "text") {
1200
- warnings.push({
1201
- type: "unsupported",
1202
- feature: "responseFormat",
1203
- details: "JSON response format is not supported."
1204
- });
1205
- }
1206
- const { prompt: completionPrompt, stopSequences } = convertToOpenAICompatibleCompletionPrompt({ prompt });
1207
- const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
1208
- return {
1209
- args: {
1210
- // model id:
1211
- model: this.modelId,
1212
- // model specific settings:
1213
- echo: completionOptions.echo,
1214
- logit_bias: completionOptions.logitBias,
1215
- suffix: completionOptions.suffix,
1216
- user: completionOptions.user,
1217
- // standardized settings:
1218
- max_tokens: maxOutputTokens,
1219
- temperature,
1220
- top_p: topP,
1221
- frequency_penalty: frequencyPenalty,
1222
- presence_penalty: presencePenalty,
1223
- seed,
1224
- ...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
1225
- // prompt:
1226
- prompt: completionPrompt,
1227
- // stop sequences:
1228
- stop: stop.length > 0 ? stop : void 0
1229
- },
1230
- warnings
1231
- };
1232
- }
1233
- async doGenerate(options) {
1234
- const { args, warnings } = await this.getArgs(options);
1235
- const {
1236
- responseHeaders,
1237
- value: response,
1238
- rawValue: rawResponse
1239
- } = await postJsonToApi2({
1240
- url: this.config.url({
1241
- path: "/completions",
1242
- modelId: this.modelId
1243
- }),
1244
- headers: combineHeaders2(this.config.headers(), options.headers),
1245
- body: args,
1246
- failedResponseHandler: this.failedResponseHandler,
1247
- successfulResponseHandler: createJsonResponseHandler2(
1248
- openaiCompatibleCompletionResponseSchema
1249
- ),
1250
- abortSignal: options.abortSignal,
1251
- fetch: this.config.fetch
1252
- });
1253
- const choice = response.choices[0];
1254
- const content = [];
1255
- if (choice.text != null && choice.text.length > 0) {
1256
- content.push({ type: "text", text: choice.text });
1257
- }
1258
- return {
1259
- content,
1260
- usage: convertOpenAICompatibleCompletionUsage(response.usage),
1261
- finishReason: {
1262
- unified: mapOpenAICompatibleFinishReason2(choice.finish_reason),
1263
- raw: choice.finish_reason
1264
- },
1265
- request: { body: args },
1266
- response: {
1267
- ...getResponseMetadata2(response),
1268
- headers: responseHeaders,
1269
- body: rawResponse
1270
- },
1271
- warnings
1272
- };
1273
- }
1274
- async doStream(options) {
1275
- const { args, warnings } = await this.getArgs(options);
1276
- const body = {
1277
- ...args,
1278
- stream: true,
1279
- // only include stream_options when in strict compatibility mode:
1280
- stream_options: this.config.includeUsage ? { include_usage: true } : void 0
1281
- };
1282
- const { responseHeaders, value: response } = await postJsonToApi2({
1283
- url: this.config.url({
1284
- path: "/completions",
1285
- modelId: this.modelId
1286
- }),
1287
- headers: combineHeaders2(this.config.headers(), options.headers),
1288
- body,
1289
- failedResponseHandler: this.failedResponseHandler,
1290
- successfulResponseHandler: createEventSourceResponseHandler2(
1291
- this.chunkSchema
1292
- ),
1293
- abortSignal: options.abortSignal,
1294
- fetch: this.config.fetch
1295
- });
1296
- let finishReason = {
1297
- unified: "other",
1298
- raw: void 0
1299
- };
1300
- let usage = void 0;
1301
- let isFirstChunk = true;
1302
- return {
1303
- stream: response.pipeThrough(
1304
- new TransformStream({
1305
- start(controller) {
1306
- controller.enqueue({ type: "stream-start", warnings });
1307
- },
1308
- transform(chunk, controller) {
1309
- var _a;
1310
- if (options.includeRawChunks) {
1311
- controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1312
- }
1313
- if (!chunk.success) {
1314
- finishReason = { unified: "error", raw: void 0 };
1315
- controller.enqueue({ type: "error", error: chunk.error });
1316
- return;
1317
- }
1318
- const value = chunk.value;
1319
- if ("error" in value) {
1320
- finishReason = { unified: "error", raw: void 0 };
1321
- controller.enqueue({ type: "error", error: value.error });
1322
- return;
1323
- }
1324
- if (isFirstChunk) {
1325
- isFirstChunk = false;
1326
- controller.enqueue({
1327
- type: "response-metadata",
1328
- ...getResponseMetadata2(value)
1329
- });
1330
- controller.enqueue({
1331
- type: "text-start",
1332
- id: "0"
1333
- });
1334
- }
1335
- if (value.usage != null) {
1336
- usage = value.usage;
1337
- }
1338
- const choice = value.choices[0];
1339
- if ((choice == null ? void 0 : choice.finish_reason) != null) {
1340
- finishReason = {
1341
- unified: mapOpenAICompatibleFinishReason2(choice.finish_reason),
1342
- raw: (_a = choice.finish_reason) != null ? _a : void 0
1343
- };
1344
- }
1345
- if ((choice == null ? void 0 : choice.text) != null) {
1346
- controller.enqueue({
1347
- type: "text-delta",
1348
- id: "0",
1349
- delta: choice.text
1350
- });
1351
- }
1352
- },
1353
- flush(controller) {
1354
- if (!isFirstChunk) {
1355
- controller.enqueue({ type: "text-end", id: "0" });
1356
- }
1357
- controller.enqueue({
1358
- type: "finish",
1359
- finishReason,
1360
- usage: convertOpenAICompatibleCompletionUsage(usage)
1361
- });
1362
- }
1363
- })
1364
- ),
1365
- request: { body },
1366
- response: { headers: responseHeaders }
1367
- };
1368
- }
1369
- };
1370
- var usageSchema = z5.object({
1371
- prompt_tokens: z5.number(),
1372
- completion_tokens: z5.number(),
1373
- total_tokens: z5.number()
1374
- });
1375
- var openaiCompatibleCompletionResponseSchema = z5.object({
1376
- id: z5.string().nullish(),
1377
- created: z5.number().nullish(),
1378
- model: z5.string().nullish(),
1379
- choices: z5.array(
1380
- z5.object({
1381
- text: z5.string(),
1382
- finish_reason: z5.string()
1383
- })
1384
- ),
1385
- usage: usageSchema.nullish()
1386
- });
1387
- var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => z5.union([
1388
- z5.object({
1389
- id: z5.string().nullish(),
1390
- created: z5.number().nullish(),
1391
- model: z5.string().nullish(),
1392
- choices: z5.array(
1393
- z5.object({
1394
- text: z5.string(),
1395
- finish_reason: z5.string().nullish(),
1396
- index: z5.number()
1397
- })
1398
- ),
1399
- usage: usageSchema.nullish()
1400
- }),
1401
- errorSchema
1402
- ]);
1403
-
1404
- // src/embedding/openai-compatible-embedding-model.ts
1405
- import {
1406
- TooManyEmbeddingValuesForCallError
1407
- } from "@ai-sdk/provider";
1408
- import {
1409
- combineHeaders as combineHeaders3,
1410
- createJsonErrorResponseHandler as createJsonErrorResponseHandler3,
1411
- createJsonResponseHandler as createJsonResponseHandler3,
1412
- parseProviderOptions as parseProviderOptions3,
1413
- postJsonToApi as postJsonToApi3
1414
- } from "@ai-sdk/provider-utils";
1415
- import { z as z7 } from "zod/v4";
1416
-
1417
- // src/embedding/openai-compatible-embedding-options.ts
1418
- import { z as z6 } from "zod/v4";
1419
- var openaiCompatibleEmbeddingModelOptions = z6.object({
1420
- /**
1421
- * The number of dimensions the resulting output embeddings should have.
1422
- * Only supported in text-embedding-3 and later models.
1423
- */
1424
- dimensions: z6.number().optional(),
1425
- /**
1426
- * A unique identifier representing your end-user, which can help providers to
1427
- * monitor and detect abuse.
1428
- */
1429
- user: z6.string().optional()
1430
- });
1431
-
1432
- // src/embedding/openai-compatible-embedding-model.ts
1433
- var OpenAICompatibleEmbeddingModel = class {
1434
- constructor(modelId, config) {
1435
- this.specificationVersion = "v4";
1436
- this.modelId = modelId;
1437
- this.config = config;
1438
- }
1439
- get provider() {
1440
- return this.config.provider;
1441
- }
1442
- get maxEmbeddingsPerCall() {
1443
- var _a;
1444
- return (_a = this.config.maxEmbeddingsPerCall) != null ? _a : 2048;
1445
- }
1446
- get supportsParallelCalls() {
1447
- var _a;
1448
- return (_a = this.config.supportsParallelCalls) != null ? _a : true;
1449
- }
1450
- get providerOptionsName() {
1451
- return this.config.provider.split(".")[0].trim();
1452
- }
1453
- async doEmbed({
1454
- values,
1455
- headers,
1456
- abortSignal,
1457
- providerOptions
1458
- }) {
1459
- var _a, _b, _c;
1460
- const warnings = [];
1461
- const deprecatedOptions = await parseProviderOptions3({
1462
- provider: "openai-compatible",
1463
- providerOptions,
1464
- schema: openaiCompatibleEmbeddingModelOptions
1465
- });
1466
- if (deprecatedOptions != null) {
1467
- warnings.push({
1468
- type: "other",
1469
- message: `The 'openai-compatible' key in providerOptions is deprecated. Use 'openaiCompatible' instead.`
1470
- });
1471
- }
1472
- const compatibleOptions = Object.assign(
1473
- deprecatedOptions != null ? deprecatedOptions : {},
1474
- (_a = await parseProviderOptions3({
1475
- provider: "openaiCompatible",
1476
- providerOptions,
1477
- schema: openaiCompatibleEmbeddingModelOptions
1478
- })) != null ? _a : {},
1479
- (_b = await parseProviderOptions3({
1480
- provider: this.providerOptionsName,
1481
- providerOptions,
1482
- schema: openaiCompatibleEmbeddingModelOptions
1483
- })) != null ? _b : {}
1484
- );
1485
- if (values.length > this.maxEmbeddingsPerCall) {
1486
- throw new TooManyEmbeddingValuesForCallError({
1487
- provider: this.provider,
1488
- modelId: this.modelId,
1489
- maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
1490
- values
1491
- });
1492
- }
1493
- const {
1494
- responseHeaders,
1495
- value: response,
1496
- rawValue
1497
- } = await postJsonToApi3({
1498
- url: this.config.url({
1499
- path: "/embeddings",
1500
- modelId: this.modelId
1501
- }),
1502
- headers: combineHeaders3(this.config.headers(), headers),
1503
- body: {
1504
- model: this.modelId,
1505
- input: values,
1506
- encoding_format: "float",
1507
- dimensions: compatibleOptions.dimensions,
1508
- user: compatibleOptions.user
1509
- },
1510
- failedResponseHandler: createJsonErrorResponseHandler3(
1511
- (_c = this.config.errorStructure) != null ? _c : defaultOpenAICompatibleErrorStructure
1512
- ),
1513
- successfulResponseHandler: createJsonResponseHandler3(
1514
- openaiTextEmbeddingResponseSchema
1515
- ),
1516
- abortSignal,
1517
- fetch: this.config.fetch
1518
- });
1519
- return {
1520
- warnings,
1521
- embeddings: response.data.map((item) => item.embedding),
1522
- usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
1523
- providerMetadata: response.providerMetadata,
1524
- response: { headers: responseHeaders, body: rawValue }
1525
- };
1526
- }
1527
- };
1528
- var openaiTextEmbeddingResponseSchema = z7.object({
1529
- data: z7.array(z7.object({ embedding: z7.array(z7.number()) })),
1530
- usage: z7.object({ prompt_tokens: z7.number() }).nullish(),
1531
- providerMetadata: z7.record(z7.string(), z7.record(z7.string(), z7.any())).optional()
1532
- });
1533
-
1534
- // src/image/openai-compatible-image-model.ts
1535
- import {
1536
- combineHeaders as combineHeaders4,
1537
- convertBase64ToUint8Array as convertBase64ToUint8Array2,
1538
- convertToFormData,
1539
- createJsonErrorResponseHandler as createJsonErrorResponseHandler4,
1540
- createJsonResponseHandler as createJsonResponseHandler4,
1541
- downloadBlob,
1542
- postFormDataToApi,
1543
- postJsonToApi as postJsonToApi4
1544
- } from "@ai-sdk/provider-utils";
1545
- import { z as z8 } from "zod/v4";
1546
- var OpenAICompatibleImageModel = class {
1547
- constructor(modelId, config) {
1548
- this.modelId = modelId;
1549
- this.config = config;
1550
- this.specificationVersion = "v4";
1551
- this.maxImagesPerCall = 10;
1552
- }
1553
- get provider() {
1554
- return this.config.provider;
1555
- }
1556
- /**
1557
- * The provider options key used to extract provider-specific options.
1558
- */
1559
- get providerOptionsKey() {
1560
- return this.config.provider.split(".")[0].trim();
1561
- }
1562
- // TODO: deprecate non-camelCase keys and remove in future major version
1563
- getArgs(providerOptions) {
1564
- return {
1565
- ...providerOptions[this.providerOptionsKey],
1566
- ...providerOptions[toCamelCase(this.providerOptionsKey)]
1567
- };
1568
- }
1569
- async doGenerate({
1570
- prompt,
1571
- n,
1572
- size,
1573
- aspectRatio,
1574
- seed,
1575
- providerOptions,
1576
- headers,
1577
- abortSignal,
1578
- files,
1579
- mask
1580
- }) {
1581
- var _a, _b, _c, _d, _e;
1582
- const warnings = [];
1583
- if (aspectRatio != null) {
1584
- warnings.push({
1585
- type: "unsupported",
1586
- feature: "aspectRatio",
1587
- details: "This model does not support aspect ratio. Use `size` instead."
1588
- });
1589
- }
1590
- if (seed != null) {
1591
- warnings.push({ type: "unsupported", feature: "seed" });
1592
- }
1593
- const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
1594
- const args = this.getArgs(providerOptions);
1595
- if (files != null && files.length > 0) {
1596
- const { value: response2, responseHeaders: responseHeaders2 } = await postFormDataToApi({
1597
- url: this.config.url({
1598
- path: "/images/edits",
1599
- modelId: this.modelId
1600
- }),
1601
- headers: combineHeaders4(this.config.headers(), headers),
1602
- formData: convertToFormData({
1603
- model: this.modelId,
1604
- prompt,
1605
- image: await Promise.all(files.map((file) => fileToBlob(file))),
1606
- mask: mask != null ? await fileToBlob(mask) : void 0,
1607
- n,
1608
- size,
1609
- ...args
1610
- }),
1611
- failedResponseHandler: createJsonErrorResponseHandler4(
1612
- (_d = this.config.errorStructure) != null ? _d : defaultOpenAICompatibleErrorStructure
1613
- ),
1614
- successfulResponseHandler: createJsonResponseHandler4(
1615
- openaiCompatibleImageResponseSchema
1616
- ),
1617
- abortSignal,
1618
- fetch: this.config.fetch
1619
- });
1620
- return {
1621
- images: response2.data.map((item) => item.b64_json),
1622
- warnings,
1623
- response: {
1624
- timestamp: currentDate,
1625
- modelId: this.modelId,
1626
- headers: responseHeaders2
1627
- }
1628
- };
1629
- }
1630
- const { value: response, responseHeaders } = await postJsonToApi4({
1631
- url: this.config.url({
1632
- path: "/images/generations",
1633
- modelId: this.modelId
1634
- }),
1635
- headers: combineHeaders4(this.config.headers(), headers),
1636
- body: {
1637
- model: this.modelId,
1638
- prompt,
1639
- n,
1640
- size,
1641
- ...args,
1642
- response_format: "b64_json"
1643
- },
1644
- failedResponseHandler: createJsonErrorResponseHandler4(
1645
- (_e = this.config.errorStructure) != null ? _e : defaultOpenAICompatibleErrorStructure
1646
- ),
1647
- successfulResponseHandler: createJsonResponseHandler4(
1648
- openaiCompatibleImageResponseSchema
1649
- ),
1650
- abortSignal,
1651
- fetch: this.config.fetch
1652
- });
1653
- return {
1654
- images: response.data.map((item) => item.b64_json),
1655
- warnings,
1656
- response: {
1657
- timestamp: currentDate,
1658
- modelId: this.modelId,
1659
- headers: responseHeaders
1660
- }
1661
- };
1662
- }
1663
- };
1664
- var openaiCompatibleImageResponseSchema = z8.object({
1665
- data: z8.array(z8.object({ b64_json: z8.string() }))
1666
- });
1667
- async function fileToBlob(file) {
1668
- if (file.type === "url") {
1669
- return downloadBlob(file.url);
1670
- }
1671
- const data = file.data instanceof Uint8Array ? file.data : convertBase64ToUint8Array2(file.data);
1672
- return new Blob([data], { type: file.mediaType });
1673
- }
1674
- function toCamelCase(str) {
1675
- return str.replace(/[_-]([a-z])/g, (g) => g[1].toUpperCase());
1676
- }
1677
-
1678
- // src/openai-compatible-provider.ts
1679
- import {
1680
- withoutTrailingSlash,
1681
- withUserAgentSuffix
1682
- } from "@ai-sdk/provider-utils";
1683
-
1684
- // src/version.ts
1685
- var VERSION = true ? "3.0.0-beta.3" : "0.0.0-test";
1686
-
1687
- // src/openai-compatible-provider.ts
1688
- function createOpenAICompatible(options) {
1689
- const baseURL = withoutTrailingSlash(options.baseURL);
1690
- const providerName = options.name;
1691
- const headers = {
1692
- ...options.apiKey && { Authorization: `Bearer ${options.apiKey}` },
1693
- ...options.headers
1694
- };
1695
- const getHeaders = () => withUserAgentSuffix(headers, `ai-sdk/openai-compatible/${VERSION}`);
1696
- const getCommonModelConfig = (modelType) => ({
1697
- provider: `${providerName}.${modelType}`,
1698
- url: ({ path }) => {
1699
- const url = new URL(`${baseURL}${path}`);
1700
- if (options.queryParams) {
1701
- url.search = new URLSearchParams(options.queryParams).toString();
1702
- }
1703
- return url.toString();
1704
- },
1705
- headers: getHeaders,
1706
- fetch: options.fetch
1707
- });
1708
- const createLanguageModel = (modelId) => createChatModel(modelId);
1709
- const createChatModel = (modelId) => new OpenAICompatibleChatLanguageModel(modelId, {
1710
- ...getCommonModelConfig("chat"),
1711
- includeUsage: options.includeUsage,
1712
- supportsStructuredOutputs: options.supportsStructuredOutputs,
1713
- transformRequestBody: options.transformRequestBody,
1714
- metadataExtractor: options.metadataExtractor
1715
- });
1716
- const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(modelId, {
1717
- ...getCommonModelConfig("completion"),
1718
- includeUsage: options.includeUsage
1719
- });
1720
- const createEmbeddingModel = (modelId) => new OpenAICompatibleEmbeddingModel(modelId, {
1721
- ...getCommonModelConfig("embedding")
1722
- });
1723
- const createImageModel = (modelId) => new OpenAICompatibleImageModel(modelId, getCommonModelConfig("image"));
1724
- const provider = (modelId) => createLanguageModel(modelId);
1725
- provider.specificationVersion = "v4";
1726
- provider.languageModel = createLanguageModel;
1727
- provider.chatModel = createChatModel;
1728
- provider.completionModel = createCompletionModel;
1729
- provider.embeddingModel = createEmbeddingModel;
1730
- provider.textEmbeddingModel = createEmbeddingModel;
1731
- provider.imageModel = createImageModel;
1732
- return provider;
1733
- }
1734
- export {
1735
- OpenAICompatibleChatLanguageModel,
1736
- OpenAICompatibleCompletionLanguageModel,
1737
- OpenAICompatibleEmbeddingModel,
1738
- OpenAICompatibleImageModel,
1739
- VERSION,
1740
- createOpenAICompatible
1741
- };
1742
- //# sourceMappingURL=index.mjs.map