@ai-sdk/openai-compatible 3.0.0-beta.23 → 3.0.0-beta.25
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/CHANGELOG.md +29 -0
- package/dist/index.d.ts +39 -6
- package/dist/index.js +319 -245
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +1 -1
- package/dist/internal/index.js +24 -47
- package/dist/internal/index.js.map +1 -1
- package/package.json +9 -10
- package/src/chat/openai-compatible-chat-language-model.ts +24 -4
- package/src/completion/openai-compatible-completion-language-model.ts +23 -3
- package/src/embedding/openai-compatible-embedding-model.ts +19 -2
- package/src/image/openai-compatible-image-model.ts +20 -3
- package/dist/index.d.mts +0 -290
- package/dist/index.mjs +0 -1815
- package/dist/index.mjs.map +0 -1
- package/dist/internal/index.d.mts +0 -193
- package/dist/internal/index.mjs +0 -346
- package/dist/internal/index.mjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,38 +1,22 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
OpenAICompatibleChatLanguageModel: () => OpenAICompatibleChatLanguageModel,
|
|
24
|
-
OpenAICompatibleCompletionLanguageModel: () => OpenAICompatibleCompletionLanguageModel,
|
|
25
|
-
OpenAICompatibleEmbeddingModel: () => OpenAICompatibleEmbeddingModel,
|
|
26
|
-
OpenAICompatibleImageModel: () => OpenAICompatibleImageModel,
|
|
27
|
-
VERSION: () => VERSION,
|
|
28
|
-
createOpenAICompatible: () => createOpenAICompatible
|
|
29
|
-
});
|
|
30
|
-
module.exports = __toCommonJS(index_exports);
|
|
31
|
-
|
|
32
1
|
// src/chat/openai-compatible-chat-language-model.ts
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
2
|
+
import {
|
|
3
|
+
InvalidResponseDataError
|
|
4
|
+
} from "@ai-sdk/provider";
|
|
5
|
+
import {
|
|
6
|
+
combineHeaders,
|
|
7
|
+
createEventSourceResponseHandler,
|
|
8
|
+
createJsonErrorResponseHandler,
|
|
9
|
+
createJsonResponseHandler,
|
|
10
|
+
generateId,
|
|
11
|
+
isCustomReasoning,
|
|
12
|
+
isParsableJson,
|
|
13
|
+
parseProviderOptions,
|
|
14
|
+
postJsonToApi,
|
|
15
|
+
serializeModelOptions,
|
|
16
|
+
WORKFLOW_SERIALIZE,
|
|
17
|
+
WORKFLOW_DESERIALIZE
|
|
18
|
+
} from "@ai-sdk/provider-utils";
|
|
19
|
+
import { z as z3 } from "zod/v4";
|
|
36
20
|
|
|
37
21
|
// src/utils/to-camel-case.ts
|
|
38
22
|
function toCamelCase(str) {
|
|
@@ -61,16 +45,16 @@ function warnIfDeprecatedProviderOptionsKey({
|
|
|
61
45
|
}
|
|
62
46
|
|
|
63
47
|
// src/openai-compatible-error.ts
|
|
64
|
-
|
|
65
|
-
var openaiCompatibleErrorDataSchema =
|
|
66
|
-
error:
|
|
67
|
-
message:
|
|
48
|
+
import { z } from "zod/v4";
|
|
49
|
+
var openaiCompatibleErrorDataSchema = z.object({
|
|
50
|
+
error: z.object({
|
|
51
|
+
message: z.string(),
|
|
68
52
|
// The additional information below is handled loosely to support
|
|
69
53
|
// OpenAI-compatible providers that have slightly different error
|
|
70
54
|
// responses:
|
|
71
|
-
type:
|
|
72
|
-
param:
|
|
73
|
-
code:
|
|
55
|
+
type: z.string().nullish(),
|
|
56
|
+
param: z.any().nullish(),
|
|
57
|
+
code: z.union([z.string(), z.number()]).nullish()
|
|
74
58
|
})
|
|
75
59
|
});
|
|
76
60
|
var defaultOpenAICompatibleErrorStructure = {
|
|
@@ -118,8 +102,14 @@ function convertOpenAICompatibleChatUsage(usage) {
|
|
|
118
102
|
}
|
|
119
103
|
|
|
120
104
|
// src/chat/convert-to-openai-compatible-chat-messages.ts
|
|
121
|
-
|
|
122
|
-
|
|
105
|
+
import {
|
|
106
|
+
UnsupportedFunctionalityError
|
|
107
|
+
} from "@ai-sdk/provider";
|
|
108
|
+
import {
|
|
109
|
+
convertBase64ToUint8Array,
|
|
110
|
+
convertToBase64,
|
|
111
|
+
isProviderReference
|
|
112
|
+
} from "@ai-sdk/provider-utils";
|
|
123
113
|
function getOpenAIMetadata(message) {
|
|
124
114
|
var _a, _b;
|
|
125
115
|
return (_b = (_a = message == null ? void 0 : message.providerOptions) == null ? void 0 : _a.openaiCompatible) != null ? _b : {};
|
|
@@ -164,8 +154,8 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
164
154
|
return { type: "text", text: part.text, ...partMetadata };
|
|
165
155
|
}
|
|
166
156
|
case "file": {
|
|
167
|
-
if (
|
|
168
|
-
throw new
|
|
157
|
+
if (isProviderReference(part.data)) {
|
|
158
|
+
throw new UnsupportedFunctionalityError({
|
|
169
159
|
functionality: "file parts with provider references"
|
|
170
160
|
});
|
|
171
161
|
}
|
|
@@ -174,27 +164,27 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
174
164
|
return {
|
|
175
165
|
type: "image_url",
|
|
176
166
|
image_url: {
|
|
177
|
-
url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${
|
|
167
|
+
url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`
|
|
178
168
|
},
|
|
179
169
|
...partMetadata
|
|
180
170
|
};
|
|
181
171
|
}
|
|
182
172
|
if (part.mediaType.startsWith("audio/")) {
|
|
183
173
|
if (part.data instanceof URL) {
|
|
184
|
-
throw new
|
|
174
|
+
throw new UnsupportedFunctionalityError({
|
|
185
175
|
functionality: "audio file parts with URLs"
|
|
186
176
|
});
|
|
187
177
|
}
|
|
188
178
|
const format = getAudioFormat(part.mediaType);
|
|
189
179
|
if (format === null) {
|
|
190
|
-
throw new
|
|
180
|
+
throw new UnsupportedFunctionalityError({
|
|
191
181
|
functionality: `audio media type ${part.mediaType}`
|
|
192
182
|
});
|
|
193
183
|
}
|
|
194
184
|
return {
|
|
195
185
|
type: "input_audio",
|
|
196
186
|
input_audio: {
|
|
197
|
-
data:
|
|
187
|
+
data: convertToBase64(part.data),
|
|
198
188
|
format
|
|
199
189
|
},
|
|
200
190
|
...partMetadata
|
|
@@ -202,7 +192,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
202
192
|
}
|
|
203
193
|
if (part.mediaType === "application/pdf") {
|
|
204
194
|
if (part.data instanceof URL) {
|
|
205
|
-
throw new
|
|
195
|
+
throw new UnsupportedFunctionalityError({
|
|
206
196
|
functionality: "PDF file parts with URLs"
|
|
207
197
|
});
|
|
208
198
|
}
|
|
@@ -210,14 +200,14 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
210
200
|
type: "file",
|
|
211
201
|
file: {
|
|
212
202
|
filename: (_a2 = part.filename) != null ? _a2 : "document.pdf",
|
|
213
|
-
file_data: `data:application/pdf;base64,${
|
|
203
|
+
file_data: `data:application/pdf;base64,${convertToBase64(part.data)}`
|
|
214
204
|
},
|
|
215
205
|
...partMetadata
|
|
216
206
|
};
|
|
217
207
|
}
|
|
218
208
|
if (part.mediaType.startsWith("text/")) {
|
|
219
209
|
const textContent = part.data instanceof URL ? part.data.toString() : typeof part.data === "string" ? new TextDecoder().decode(
|
|
220
|
-
|
|
210
|
+
convertBase64ToUint8Array(part.data)
|
|
221
211
|
) : new TextDecoder().decode(part.data);
|
|
222
212
|
return {
|
|
223
213
|
type: "text",
|
|
@@ -225,7 +215,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
225
215
|
...partMetadata
|
|
226
216
|
};
|
|
227
217
|
}
|
|
228
|
-
throw new
|
|
218
|
+
throw new UnsupportedFunctionalityError({
|
|
229
219
|
functionality: `file part media type ${part.mediaType}`
|
|
230
220
|
});
|
|
231
221
|
}
|
|
@@ -353,21 +343,21 @@ function mapOpenAICompatibleFinishReason(finishReason) {
|
|
|
353
343
|
}
|
|
354
344
|
|
|
355
345
|
// src/chat/openai-compatible-chat-options.ts
|
|
356
|
-
|
|
357
|
-
var openaiCompatibleLanguageModelChatOptions =
|
|
346
|
+
import { z as z2 } from "zod/v4";
|
|
347
|
+
var openaiCompatibleLanguageModelChatOptions = z2.object({
|
|
358
348
|
/**
|
|
359
349
|
* A unique identifier representing your end-user, which can help the provider to
|
|
360
350
|
* monitor and detect abuse.
|
|
361
351
|
*/
|
|
362
|
-
user:
|
|
352
|
+
user: z2.string().optional(),
|
|
363
353
|
/**
|
|
364
354
|
* Reasoning effort for reasoning models. Defaults to `medium`.
|
|
365
355
|
*/
|
|
366
|
-
reasoningEffort:
|
|
356
|
+
reasoningEffort: z2.string().optional(),
|
|
367
357
|
/**
|
|
368
358
|
* Controls the verbosity of the generated text. Defaults to `medium`.
|
|
369
359
|
*/
|
|
370
|
-
textVerbosity:
|
|
360
|
+
textVerbosity: z2.string().optional(),
|
|
371
361
|
/**
|
|
372
362
|
* Whether to use strict JSON schema validation.
|
|
373
363
|
* When true, the model uses constrained decoding to guarantee schema compliance.
|
|
@@ -375,11 +365,13 @@ var openaiCompatibleLanguageModelChatOptions = import_v42.z.object({
|
|
|
375
365
|
*
|
|
376
366
|
* @default true
|
|
377
367
|
*/
|
|
378
|
-
strictJsonSchema:
|
|
368
|
+
strictJsonSchema: z2.boolean().optional()
|
|
379
369
|
});
|
|
380
370
|
|
|
381
371
|
// src/chat/openai-compatible-prepare-tools.ts
|
|
382
|
-
|
|
372
|
+
import {
|
|
373
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
374
|
+
} from "@ai-sdk/provider";
|
|
383
375
|
function prepareTools({
|
|
384
376
|
tools,
|
|
385
377
|
toolChoice
|
|
@@ -428,7 +420,7 @@ function prepareTools({
|
|
|
428
420
|
};
|
|
429
421
|
default: {
|
|
430
422
|
const _exhaustiveCheck = type;
|
|
431
|
-
throw new
|
|
423
|
+
throw new UnsupportedFunctionalityError2({
|
|
432
424
|
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
433
425
|
});
|
|
434
426
|
}
|
|
@@ -436,8 +428,7 @@ function prepareTools({
|
|
|
436
428
|
}
|
|
437
429
|
|
|
438
430
|
// src/chat/openai-compatible-chat-language-model.ts
|
|
439
|
-
var OpenAICompatibleChatLanguageModel = class {
|
|
440
|
-
// type inferred via constructor
|
|
431
|
+
var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel {
|
|
441
432
|
constructor(modelId, config) {
|
|
442
433
|
this.specificationVersion = "v4";
|
|
443
434
|
var _a, _b;
|
|
@@ -447,9 +438,22 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
447
438
|
this.chunkSchema = createOpenAICompatibleChatChunkSchema(
|
|
448
439
|
errorStructure.errorSchema
|
|
449
440
|
);
|
|
450
|
-
this.failedResponseHandler =
|
|
441
|
+
this.failedResponseHandler = createJsonErrorResponseHandler(errorStructure);
|
|
451
442
|
this.supportsStructuredOutputs = (_b = config.supportsStructuredOutputs) != null ? _b : false;
|
|
452
443
|
}
|
|
444
|
+
// type inferred via constructor
|
|
445
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
446
|
+
return serializeModelOptions({
|
|
447
|
+
modelId: model.modelId,
|
|
448
|
+
config: model.config
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
452
|
+
return new _OpenAICompatibleChatLanguageModel(
|
|
453
|
+
options.modelId,
|
|
454
|
+
options.config
|
|
455
|
+
);
|
|
456
|
+
}
|
|
453
457
|
get provider() {
|
|
454
458
|
return this.config.provider;
|
|
455
459
|
}
|
|
@@ -482,7 +486,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
482
486
|
}) {
|
|
483
487
|
var _a, _b, _c, _d, _e, _f;
|
|
484
488
|
const warnings = [];
|
|
485
|
-
const deprecatedOptions = await
|
|
489
|
+
const deprecatedOptions = await parseProviderOptions({
|
|
486
490
|
provider: "openai-compatible",
|
|
487
491
|
providerOptions,
|
|
488
492
|
schema: openaiCompatibleLanguageModelChatOptions
|
|
@@ -501,17 +505,17 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
501
505
|
});
|
|
502
506
|
const compatibleOptions = Object.assign(
|
|
503
507
|
deprecatedOptions != null ? deprecatedOptions : {},
|
|
504
|
-
(_a = await
|
|
508
|
+
(_a = await parseProviderOptions({
|
|
505
509
|
provider: "openaiCompatible",
|
|
506
510
|
providerOptions,
|
|
507
511
|
schema: openaiCompatibleLanguageModelChatOptions
|
|
508
512
|
})) != null ? _a : {},
|
|
509
|
-
(_b = await
|
|
513
|
+
(_b = await parseProviderOptions({
|
|
510
514
|
provider: this.providerOptionsName,
|
|
511
515
|
providerOptions,
|
|
512
516
|
schema: openaiCompatibleLanguageModelChatOptions
|
|
513
517
|
})) != null ? _b : {},
|
|
514
|
-
(_c = await
|
|
518
|
+
(_c = await parseProviderOptions({
|
|
515
519
|
provider: toCamelCase(this.providerOptionsName),
|
|
516
520
|
providerOptions,
|
|
517
521
|
schema: openaiCompatibleLanguageModelChatOptions
|
|
@@ -574,7 +578,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
574
578
|
).includes(key)
|
|
575
579
|
)
|
|
576
580
|
),
|
|
577
|
-
reasoning_effort: (_f = compatibleOptions.reasoningEffort) != null ? _f :
|
|
581
|
+
reasoning_effort: (_f = compatibleOptions.reasoningEffort) != null ? _f : isCustomReasoning(reasoning) && reasoning !== "none" ? reasoning : void 0,
|
|
578
582
|
verbosity: compatibleOptions.textVerbosity,
|
|
579
583
|
// messages:
|
|
580
584
|
messages: convertToOpenAICompatibleChatMessages(prompt),
|
|
@@ -586,7 +590,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
586
590
|
};
|
|
587
591
|
}
|
|
588
592
|
async doGenerate(options) {
|
|
589
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
593
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
590
594
|
const { args, warnings, metadataKey } = await this.getArgs({ ...options });
|
|
591
595
|
const transformedBody = this.transformRequestBody(args);
|
|
592
596
|
const body = JSON.stringify(transformedBody);
|
|
@@ -594,15 +598,15 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
594
598
|
responseHeaders,
|
|
595
599
|
value: responseBody,
|
|
596
600
|
rawValue: rawResponse
|
|
597
|
-
} = await
|
|
601
|
+
} = await postJsonToApi({
|
|
598
602
|
url: this.config.url({
|
|
599
603
|
path: "/chat/completions",
|
|
600
604
|
modelId: this.modelId
|
|
601
605
|
}),
|
|
602
|
-
headers: (
|
|
606
|
+
headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
603
607
|
body: transformedBody,
|
|
604
608
|
failedResponseHandler: this.failedResponseHandler,
|
|
605
|
-
successfulResponseHandler:
|
|
609
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
606
610
|
OpenAICompatibleChatResponseSchema
|
|
607
611
|
),
|
|
608
612
|
abortSignal: options.abortSignal,
|
|
@@ -614,7 +618,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
614
618
|
if (text != null && text.length > 0) {
|
|
615
619
|
content.push({ type: "text", text });
|
|
616
620
|
}
|
|
617
|
-
const reasoning = (
|
|
621
|
+
const reasoning = (_c = choice.message.reasoning_content) != null ? _c : choice.message.reasoning;
|
|
618
622
|
if (reasoning != null && reasoning.length > 0) {
|
|
619
623
|
content.push({
|
|
620
624
|
type: "reasoning",
|
|
@@ -623,10 +627,10 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
623
627
|
}
|
|
624
628
|
if (choice.message.tool_calls != null) {
|
|
625
629
|
for (const toolCall of choice.message.tool_calls) {
|
|
626
|
-
const thoughtSignature = (
|
|
630
|
+
const thoughtSignature = (_e = (_d = toolCall.extra_content) == null ? void 0 : _d.google) == null ? void 0 : _e.thought_signature;
|
|
627
631
|
content.push({
|
|
628
632
|
type: "tool-call",
|
|
629
|
-
toolCallId: (
|
|
633
|
+
toolCallId: (_f = toolCall.id) != null ? _f : generateId(),
|
|
630
634
|
toolName: toolCall.function.name,
|
|
631
635
|
input: toolCall.function.arguments,
|
|
632
636
|
...thoughtSignature ? {
|
|
@@ -639,11 +643,11 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
639
643
|
}
|
|
640
644
|
const providerMetadata = {
|
|
641
645
|
[metadataKey]: {},
|
|
642
|
-
...await ((
|
|
646
|
+
...await ((_h = (_g = this.config.metadataExtractor) == null ? void 0 : _g.extractMetadata) == null ? void 0 : _h.call(_g, {
|
|
643
647
|
parsedBody: rawResponse
|
|
644
648
|
}))
|
|
645
649
|
};
|
|
646
|
-
const completionTokenDetails = (
|
|
650
|
+
const completionTokenDetails = (_i = responseBody.usage) == null ? void 0 : _i.completion_tokens_details;
|
|
647
651
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
|
|
648
652
|
providerMetadata[metadataKey].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
|
|
649
653
|
}
|
|
@@ -654,7 +658,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
654
658
|
content,
|
|
655
659
|
finishReason: {
|
|
656
660
|
unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
|
657
|
-
raw: (
|
|
661
|
+
raw: (_j = choice.finish_reason) != null ? _j : void 0
|
|
658
662
|
},
|
|
659
663
|
usage: convertOpenAICompatibleChatUsage(responseBody.usage),
|
|
660
664
|
providerMetadata,
|
|
@@ -668,7 +672,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
668
672
|
};
|
|
669
673
|
}
|
|
670
674
|
async doStream(options) {
|
|
671
|
-
var _a;
|
|
675
|
+
var _a, _b, _c;
|
|
672
676
|
const { args, warnings, metadataKey } = await this.getArgs({
|
|
673
677
|
...options
|
|
674
678
|
});
|
|
@@ -679,15 +683,15 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
679
683
|
stream_options: this.config.includeUsage ? { include_usage: true } : void 0
|
|
680
684
|
});
|
|
681
685
|
const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
|
|
682
|
-
const { responseHeaders, value: response } = await
|
|
686
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
683
687
|
url: this.config.url({
|
|
684
688
|
path: "/chat/completions",
|
|
685
689
|
modelId: this.modelId
|
|
686
690
|
}),
|
|
687
|
-
headers: (
|
|
691
|
+
headers: combineHeaders((_c = (_b = this.config).headers) == null ? void 0 : _c.call(_b), options.headers),
|
|
688
692
|
body,
|
|
689
693
|
failedResponseHandler: this.failedResponseHandler,
|
|
690
|
-
successfulResponseHandler:
|
|
694
|
+
successfulResponseHandler: createEventSourceResponseHandler(
|
|
691
695
|
this.chunkSchema
|
|
692
696
|
),
|
|
693
697
|
abortSignal: options.abortSignal,
|
|
@@ -710,7 +714,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
710
714
|
controller.enqueue({ type: "stream-start", warnings });
|
|
711
715
|
},
|
|
712
716
|
transform(chunk, controller) {
|
|
713
|
-
var _a2,
|
|
717
|
+
var _a2, _b2, _c2, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
|
|
714
718
|
if (options.includeRawChunks) {
|
|
715
719
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
716
720
|
}
|
|
@@ -750,7 +754,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
750
754
|
return;
|
|
751
755
|
}
|
|
752
756
|
const delta = choice.delta;
|
|
753
|
-
const reasoningContent = (
|
|
757
|
+
const reasoningContent = (_b2 = delta.reasoning_content) != null ? _b2 : delta.reasoning;
|
|
754
758
|
if (reasoningContent) {
|
|
755
759
|
if (!isActiveReasoning) {
|
|
756
760
|
controller.enqueue({
|
|
@@ -792,16 +796,16 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
792
796
|
isActiveReasoning = false;
|
|
793
797
|
}
|
|
794
798
|
for (const toolCallDelta of delta.tool_calls) {
|
|
795
|
-
const index = (
|
|
799
|
+
const index = (_c2 = toolCallDelta.index) != null ? _c2 : toolCalls.length;
|
|
796
800
|
if (toolCalls[index] == null) {
|
|
797
801
|
if (toolCallDelta.id == null) {
|
|
798
|
-
throw new
|
|
802
|
+
throw new InvalidResponseDataError({
|
|
799
803
|
data: toolCallDelta,
|
|
800
804
|
message: `Expected 'id' to be a string.`
|
|
801
805
|
});
|
|
802
806
|
}
|
|
803
807
|
if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) {
|
|
804
|
-
throw new
|
|
808
|
+
throw new InvalidResponseDataError({
|
|
805
809
|
data: toolCallDelta,
|
|
806
810
|
message: `Expected 'function.name' to be a string.`
|
|
807
811
|
});
|
|
@@ -830,14 +834,14 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
830
834
|
delta: toolCall2.function.arguments
|
|
831
835
|
});
|
|
832
836
|
}
|
|
833
|
-
if (
|
|
837
|
+
if (isParsableJson(toolCall2.function.arguments)) {
|
|
834
838
|
controller.enqueue({
|
|
835
839
|
type: "tool-input-end",
|
|
836
840
|
id: toolCall2.id
|
|
837
841
|
});
|
|
838
842
|
controller.enqueue({
|
|
839
843
|
type: "tool-call",
|
|
840
|
-
toolCallId: (_k = toolCall2.id) != null ? _k :
|
|
844
|
+
toolCallId: (_k = toolCall2.id) != null ? _k : generateId(),
|
|
841
845
|
toolName: toolCall2.function.name,
|
|
842
846
|
input: toolCall2.function.arguments,
|
|
843
847
|
...toolCall2.thoughtSignature ? {
|
|
@@ -865,14 +869,14 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
865
869
|
id: toolCall.id,
|
|
866
870
|
delta: (_o = toolCallDelta.function.arguments) != null ? _o : ""
|
|
867
871
|
});
|
|
868
|
-
if (((_p = toolCall.function) == null ? void 0 : _p.name) != null && ((_q = toolCall.function) == null ? void 0 : _q.arguments) != null &&
|
|
872
|
+
if (((_p = toolCall.function) == null ? void 0 : _p.name) != null && ((_q = toolCall.function) == null ? void 0 : _q.arguments) != null && isParsableJson(toolCall.function.arguments)) {
|
|
869
873
|
controller.enqueue({
|
|
870
874
|
type: "tool-input-end",
|
|
871
875
|
id: toolCall.id
|
|
872
876
|
});
|
|
873
877
|
controller.enqueue({
|
|
874
878
|
type: "tool-call",
|
|
875
|
-
toolCallId: (_r = toolCall.id) != null ? _r :
|
|
879
|
+
toolCallId: (_r = toolCall.id) != null ? _r : generateId(),
|
|
876
880
|
toolName: toolCall.function.name,
|
|
877
881
|
input: toolCall.function.arguments,
|
|
878
882
|
...toolCall.thoughtSignature ? {
|
|
@@ -889,7 +893,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
889
893
|
}
|
|
890
894
|
},
|
|
891
895
|
flush(controller) {
|
|
892
|
-
var _a2,
|
|
896
|
+
var _a2, _b2, _c2, _d, _e;
|
|
893
897
|
if (isActiveReasoning) {
|
|
894
898
|
controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
|
|
895
899
|
}
|
|
@@ -905,7 +909,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
905
909
|
});
|
|
906
910
|
controller.enqueue({
|
|
907
911
|
type: "tool-call",
|
|
908
|
-
toolCallId: (_a2 = toolCall.id) != null ? _a2 :
|
|
912
|
+
toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
|
|
909
913
|
toolName: toolCall.function.name,
|
|
910
914
|
input: toolCall.function.arguments,
|
|
911
915
|
...toolCall.thoughtSignature ? {
|
|
@@ -921,8 +925,8 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
921
925
|
[providerOptionsName]: {},
|
|
922
926
|
...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
|
|
923
927
|
};
|
|
924
|
-
if (((
|
|
925
|
-
providerMetadata[providerOptionsName].acceptedPredictionTokens = (
|
|
928
|
+
if (((_b2 = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _b2.accepted_prediction_tokens) != null) {
|
|
929
|
+
providerMetadata[providerOptionsName].acceptedPredictionTokens = (_c2 = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _c2.accepted_prediction_tokens;
|
|
926
930
|
}
|
|
927
931
|
if (((_d = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _d.rejected_prediction_tokens) != null) {
|
|
928
932
|
providerMetadata[providerOptionsName].rejectedPredictionTokens = (_e = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _e.rejected_prediction_tokens;
|
|
@@ -941,92 +945,102 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
941
945
|
};
|
|
942
946
|
}
|
|
943
947
|
};
|
|
944
|
-
var openaiCompatibleTokenUsageSchema =
|
|
945
|
-
prompt_tokens:
|
|
946
|
-
completion_tokens:
|
|
947
|
-
total_tokens:
|
|
948
|
-
prompt_tokens_details:
|
|
949
|
-
cached_tokens:
|
|
948
|
+
var openaiCompatibleTokenUsageSchema = z3.looseObject({
|
|
949
|
+
prompt_tokens: z3.number().nullish(),
|
|
950
|
+
completion_tokens: z3.number().nullish(),
|
|
951
|
+
total_tokens: z3.number().nullish(),
|
|
952
|
+
prompt_tokens_details: z3.object({
|
|
953
|
+
cached_tokens: z3.number().nullish()
|
|
950
954
|
}).nullish(),
|
|
951
|
-
completion_tokens_details:
|
|
952
|
-
reasoning_tokens:
|
|
953
|
-
accepted_prediction_tokens:
|
|
954
|
-
rejected_prediction_tokens:
|
|
955
|
+
completion_tokens_details: z3.object({
|
|
956
|
+
reasoning_tokens: z3.number().nullish(),
|
|
957
|
+
accepted_prediction_tokens: z3.number().nullish(),
|
|
958
|
+
rejected_prediction_tokens: z3.number().nullish()
|
|
955
959
|
}).nullish()
|
|
956
960
|
}).nullish();
|
|
957
|
-
var OpenAICompatibleChatResponseSchema =
|
|
958
|
-
id:
|
|
959
|
-
created:
|
|
960
|
-
model:
|
|
961
|
-
choices:
|
|
962
|
-
|
|
963
|
-
message:
|
|
964
|
-
role:
|
|
965
|
-
content:
|
|
966
|
-
reasoning_content:
|
|
967
|
-
reasoning:
|
|
968
|
-
tool_calls:
|
|
969
|
-
|
|
970
|
-
id:
|
|
971
|
-
function:
|
|
972
|
-
name:
|
|
973
|
-
arguments:
|
|
961
|
+
var OpenAICompatibleChatResponseSchema = z3.looseObject({
|
|
962
|
+
id: z3.string().nullish(),
|
|
963
|
+
created: z3.number().nullish(),
|
|
964
|
+
model: z3.string().nullish(),
|
|
965
|
+
choices: z3.array(
|
|
966
|
+
z3.object({
|
|
967
|
+
message: z3.object({
|
|
968
|
+
role: z3.literal("assistant").nullish(),
|
|
969
|
+
content: z3.string().nullish(),
|
|
970
|
+
reasoning_content: z3.string().nullish(),
|
|
971
|
+
reasoning: z3.string().nullish(),
|
|
972
|
+
tool_calls: z3.array(
|
|
973
|
+
z3.object({
|
|
974
|
+
id: z3.string().nullish(),
|
|
975
|
+
function: z3.object({
|
|
976
|
+
name: z3.string(),
|
|
977
|
+
arguments: z3.string()
|
|
974
978
|
}),
|
|
975
979
|
// Support for Google Gemini thought signatures via OpenAI compatibility
|
|
976
|
-
extra_content:
|
|
977
|
-
google:
|
|
978
|
-
thought_signature:
|
|
980
|
+
extra_content: z3.object({
|
|
981
|
+
google: z3.object({
|
|
982
|
+
thought_signature: z3.string().nullish()
|
|
979
983
|
}).nullish()
|
|
980
984
|
}).nullish()
|
|
981
985
|
})
|
|
982
986
|
).nullish()
|
|
983
987
|
}),
|
|
984
|
-
finish_reason:
|
|
988
|
+
finish_reason: z3.string().nullish()
|
|
985
989
|
})
|
|
986
990
|
),
|
|
987
991
|
usage: openaiCompatibleTokenUsageSchema
|
|
988
992
|
});
|
|
989
|
-
var chunkBaseSchema =
|
|
990
|
-
id:
|
|
991
|
-
created:
|
|
992
|
-
model:
|
|
993
|
-
choices:
|
|
994
|
-
|
|
995
|
-
delta:
|
|
996
|
-
role:
|
|
997
|
-
content:
|
|
993
|
+
var chunkBaseSchema = z3.looseObject({
|
|
994
|
+
id: z3.string().nullish(),
|
|
995
|
+
created: z3.number().nullish(),
|
|
996
|
+
model: z3.string().nullish(),
|
|
997
|
+
choices: z3.array(
|
|
998
|
+
z3.object({
|
|
999
|
+
delta: z3.object({
|
|
1000
|
+
role: z3.enum(["assistant"]).nullish(),
|
|
1001
|
+
content: z3.string().nullish(),
|
|
998
1002
|
// Most openai-compatible models set `reasoning_content`, but some
|
|
999
1003
|
// providers serving `gpt-oss` set `reasoning`. See #7866
|
|
1000
|
-
reasoning_content:
|
|
1001
|
-
reasoning:
|
|
1002
|
-
tool_calls:
|
|
1003
|
-
|
|
1004
|
-
index:
|
|
1004
|
+
reasoning_content: z3.string().nullish(),
|
|
1005
|
+
reasoning: z3.string().nullish(),
|
|
1006
|
+
tool_calls: z3.array(
|
|
1007
|
+
z3.object({
|
|
1008
|
+
index: z3.number().nullish(),
|
|
1005
1009
|
//google does not send index
|
|
1006
|
-
id:
|
|
1007
|
-
function:
|
|
1008
|
-
name:
|
|
1009
|
-
arguments:
|
|
1010
|
+
id: z3.string().nullish(),
|
|
1011
|
+
function: z3.object({
|
|
1012
|
+
name: z3.string().nullish(),
|
|
1013
|
+
arguments: z3.string().nullish()
|
|
1010
1014
|
}),
|
|
1011
1015
|
// Support for Google Gemini thought signatures via OpenAI compatibility
|
|
1012
|
-
extra_content:
|
|
1013
|
-
google:
|
|
1014
|
-
thought_signature:
|
|
1016
|
+
extra_content: z3.object({
|
|
1017
|
+
google: z3.object({
|
|
1018
|
+
thought_signature: z3.string().nullish()
|
|
1015
1019
|
}).nullish()
|
|
1016
1020
|
}).nullish()
|
|
1017
1021
|
})
|
|
1018
1022
|
).nullish()
|
|
1019
1023
|
}).nullish(),
|
|
1020
|
-
finish_reason:
|
|
1024
|
+
finish_reason: z3.string().nullish()
|
|
1021
1025
|
})
|
|
1022
1026
|
),
|
|
1023
1027
|
usage: openaiCompatibleTokenUsageSchema
|
|
1024
1028
|
});
|
|
1025
|
-
var createOpenAICompatibleChatChunkSchema = (errorSchema) =>
|
|
1029
|
+
var createOpenAICompatibleChatChunkSchema = (errorSchema) => z3.union([chunkBaseSchema, errorSchema]);
|
|
1026
1030
|
|
|
1027
1031
|
// src/completion/openai-compatible-completion-language-model.ts
|
|
1028
|
-
|
|
1029
|
-
|
|
1032
|
+
import {
|
|
1033
|
+
combineHeaders as combineHeaders2,
|
|
1034
|
+
createEventSourceResponseHandler as createEventSourceResponseHandler2,
|
|
1035
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler2,
|
|
1036
|
+
createJsonResponseHandler as createJsonResponseHandler2,
|
|
1037
|
+
parseProviderOptions as parseProviderOptions2,
|
|
1038
|
+
postJsonToApi as postJsonToApi2,
|
|
1039
|
+
serializeModelOptions as serializeModelOptions2,
|
|
1040
|
+
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE2,
|
|
1041
|
+
WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE2
|
|
1042
|
+
} from "@ai-sdk/provider-utils";
|
|
1043
|
+
import { z as z5 } from "zod/v4";
|
|
1030
1044
|
|
|
1031
1045
|
// src/completion/convert-openai-compatible-completion-usage.ts
|
|
1032
1046
|
function convertOpenAICompatibleCompletionUsage(usage) {
|
|
@@ -1066,7 +1080,10 @@ function convertOpenAICompatibleCompletionUsage(usage) {
|
|
|
1066
1080
|
}
|
|
1067
1081
|
|
|
1068
1082
|
// src/completion/convert-to-openai-compatible-completion-prompt.ts
|
|
1069
|
-
|
|
1083
|
+
import {
|
|
1084
|
+
InvalidPromptError,
|
|
1085
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
1086
|
+
} from "@ai-sdk/provider";
|
|
1070
1087
|
function convertToOpenAICompatibleCompletionPrompt({
|
|
1071
1088
|
prompt,
|
|
1072
1089
|
user = "user",
|
|
@@ -1082,7 +1099,7 @@ function convertToOpenAICompatibleCompletionPrompt({
|
|
|
1082
1099
|
for (const { role, content } of prompt) {
|
|
1083
1100
|
switch (role) {
|
|
1084
1101
|
case "system": {
|
|
1085
|
-
throw new
|
|
1102
|
+
throw new InvalidPromptError({
|
|
1086
1103
|
message: "Unexpected system message in prompt: ${content}",
|
|
1087
1104
|
prompt
|
|
1088
1105
|
});
|
|
@@ -1108,7 +1125,7 @@ ${userMessage}
|
|
|
1108
1125
|
return part.text;
|
|
1109
1126
|
}
|
|
1110
1127
|
case "tool-call": {
|
|
1111
|
-
throw new
|
|
1128
|
+
throw new UnsupportedFunctionalityError3({
|
|
1112
1129
|
functionality: "tool-call messages"
|
|
1113
1130
|
});
|
|
1114
1131
|
}
|
|
@@ -1121,7 +1138,7 @@ ${assistantMessage}
|
|
|
1121
1138
|
break;
|
|
1122
1139
|
}
|
|
1123
1140
|
case "tool": {
|
|
1124
|
-
throw new
|
|
1141
|
+
throw new UnsupportedFunctionalityError3({
|
|
1125
1142
|
functionality: "tool messages"
|
|
1126
1143
|
});
|
|
1127
1144
|
}
|
|
@@ -1171,33 +1188,32 @@ function mapOpenAICompatibleFinishReason2(finishReason) {
|
|
|
1171
1188
|
}
|
|
1172
1189
|
|
|
1173
1190
|
// src/completion/openai-compatible-completion-options.ts
|
|
1174
|
-
|
|
1175
|
-
var openaiCompatibleLanguageModelCompletionOptions =
|
|
1191
|
+
import { z as z4 } from "zod/v4";
|
|
1192
|
+
var openaiCompatibleLanguageModelCompletionOptions = z4.object({
|
|
1176
1193
|
/**
|
|
1177
1194
|
* Echo back the prompt in addition to the completion.
|
|
1178
1195
|
*/
|
|
1179
|
-
echo:
|
|
1196
|
+
echo: z4.boolean().optional(),
|
|
1180
1197
|
/**
|
|
1181
1198
|
* Modify the likelihood of specified tokens appearing in the completion.
|
|
1182
1199
|
*
|
|
1183
1200
|
* Accepts a JSON object that maps tokens (specified by their token ID in
|
|
1184
1201
|
* the GPT tokenizer) to an associated bias value from -100 to 100.
|
|
1185
1202
|
*/
|
|
1186
|
-
logitBias:
|
|
1203
|
+
logitBias: z4.record(z4.string(), z4.number()).optional(),
|
|
1187
1204
|
/**
|
|
1188
1205
|
* The suffix that comes after a completion of inserted text.
|
|
1189
1206
|
*/
|
|
1190
|
-
suffix:
|
|
1207
|
+
suffix: z4.string().optional(),
|
|
1191
1208
|
/**
|
|
1192
1209
|
* A unique identifier representing your end-user, which can help providers to
|
|
1193
1210
|
* monitor and detect abuse.
|
|
1194
1211
|
*/
|
|
1195
|
-
user:
|
|
1212
|
+
user: z4.string().optional()
|
|
1196
1213
|
});
|
|
1197
1214
|
|
|
1198
1215
|
// src/completion/openai-compatible-completion-language-model.ts
|
|
1199
|
-
var OpenAICompatibleCompletionLanguageModel = class {
|
|
1200
|
-
// type inferred via constructor
|
|
1216
|
+
var OpenAICompatibleCompletionLanguageModel = class _OpenAICompatibleCompletionLanguageModel {
|
|
1201
1217
|
constructor(modelId, config) {
|
|
1202
1218
|
this.specificationVersion = "v4";
|
|
1203
1219
|
var _a;
|
|
@@ -1207,7 +1223,20 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1207
1223
|
this.chunkSchema = createOpenAICompatibleCompletionChunkSchema(
|
|
1208
1224
|
errorStructure.errorSchema
|
|
1209
1225
|
);
|
|
1210
|
-
this.failedResponseHandler = (
|
|
1226
|
+
this.failedResponseHandler = createJsonErrorResponseHandler2(errorStructure);
|
|
1227
|
+
}
|
|
1228
|
+
// type inferred via constructor
|
|
1229
|
+
static [WORKFLOW_SERIALIZE2](model) {
|
|
1230
|
+
return serializeModelOptions2({
|
|
1231
|
+
modelId: model.modelId,
|
|
1232
|
+
config: model.config
|
|
1233
|
+
});
|
|
1234
|
+
}
|
|
1235
|
+
static [WORKFLOW_DESERIALIZE2](options) {
|
|
1236
|
+
return new _OpenAICompatibleCompletionLanguageModel(
|
|
1237
|
+
options.modelId,
|
|
1238
|
+
options.config
|
|
1239
|
+
);
|
|
1211
1240
|
}
|
|
1212
1241
|
get provider() {
|
|
1213
1242
|
return this.config.provider;
|
|
@@ -1242,12 +1271,12 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1242
1271
|
warnings
|
|
1243
1272
|
});
|
|
1244
1273
|
const completionOptions = Object.assign(
|
|
1245
|
-
(_a = await (
|
|
1274
|
+
(_a = await parseProviderOptions2({
|
|
1246
1275
|
provider: this.providerOptionsName,
|
|
1247
1276
|
providerOptions,
|
|
1248
1277
|
schema: openaiCompatibleLanguageModelCompletionOptions
|
|
1249
1278
|
})) != null ? _a : {},
|
|
1250
|
-
(_b = await (
|
|
1279
|
+
(_b = await parseProviderOptions2({
|
|
1251
1280
|
provider: toCamelCase(this.providerOptionsName),
|
|
1252
1281
|
providerOptions,
|
|
1253
1282
|
schema: openaiCompatibleLanguageModelCompletionOptions
|
|
@@ -1298,20 +1327,21 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1298
1327
|
};
|
|
1299
1328
|
}
|
|
1300
1329
|
async doGenerate(options) {
|
|
1330
|
+
var _a, _b;
|
|
1301
1331
|
const { args, warnings } = await this.getArgs(options);
|
|
1302
1332
|
const {
|
|
1303
1333
|
responseHeaders,
|
|
1304
1334
|
value: response,
|
|
1305
1335
|
rawValue: rawResponse
|
|
1306
|
-
} = await (
|
|
1336
|
+
} = await postJsonToApi2({
|
|
1307
1337
|
url: this.config.url({
|
|
1308
1338
|
path: "/completions",
|
|
1309
1339
|
modelId: this.modelId
|
|
1310
1340
|
}),
|
|
1311
|
-
headers: (
|
|
1341
|
+
headers: combineHeaders2((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
1312
1342
|
body: args,
|
|
1313
1343
|
failedResponseHandler: this.failedResponseHandler,
|
|
1314
|
-
successfulResponseHandler: (
|
|
1344
|
+
successfulResponseHandler: createJsonResponseHandler2(
|
|
1315
1345
|
openaiCompatibleCompletionResponseSchema
|
|
1316
1346
|
),
|
|
1317
1347
|
abortSignal: options.abortSignal,
|
|
@@ -1339,6 +1369,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1339
1369
|
};
|
|
1340
1370
|
}
|
|
1341
1371
|
async doStream(options) {
|
|
1372
|
+
var _a, _b;
|
|
1342
1373
|
const { args, warnings } = await this.getArgs(options);
|
|
1343
1374
|
const body = {
|
|
1344
1375
|
...args,
|
|
@@ -1346,15 +1377,15 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1346
1377
|
// only include stream_options when in strict compatibility mode:
|
|
1347
1378
|
stream_options: this.config.includeUsage ? { include_usage: true } : void 0
|
|
1348
1379
|
};
|
|
1349
|
-
const { responseHeaders, value: response } = await (
|
|
1380
|
+
const { responseHeaders, value: response } = await postJsonToApi2({
|
|
1350
1381
|
url: this.config.url({
|
|
1351
1382
|
path: "/completions",
|
|
1352
1383
|
modelId: this.modelId
|
|
1353
1384
|
}),
|
|
1354
|
-
headers: (
|
|
1385
|
+
headers: combineHeaders2((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
1355
1386
|
body,
|
|
1356
1387
|
failedResponseHandler: this.failedResponseHandler,
|
|
1357
|
-
successfulResponseHandler: (
|
|
1388
|
+
successfulResponseHandler: createEventSourceResponseHandler2(
|
|
1358
1389
|
this.chunkSchema
|
|
1359
1390
|
),
|
|
1360
1391
|
abortSignal: options.abortSignal,
|
|
@@ -1373,7 +1404,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1373
1404
|
controller.enqueue({ type: "stream-start", warnings });
|
|
1374
1405
|
},
|
|
1375
1406
|
transform(chunk, controller) {
|
|
1376
|
-
var
|
|
1407
|
+
var _a2;
|
|
1377
1408
|
if (options.includeRawChunks) {
|
|
1378
1409
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
1379
1410
|
}
|
|
@@ -1406,7 +1437,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1406
1437
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
1407
1438
|
finishReason = {
|
|
1408
1439
|
unified: mapOpenAICompatibleFinishReason2(choice.finish_reason),
|
|
1409
|
-
raw: (
|
|
1440
|
+
raw: (_a2 = choice.finish_reason) != null ? _a2 : void 0
|
|
1410
1441
|
};
|
|
1411
1442
|
}
|
|
1412
1443
|
if ((choice == null ? void 0 : choice.text) != null) {
|
|
@@ -1434,33 +1465,33 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1434
1465
|
};
|
|
1435
1466
|
}
|
|
1436
1467
|
};
|
|
1437
|
-
var usageSchema =
|
|
1438
|
-
prompt_tokens:
|
|
1439
|
-
completion_tokens:
|
|
1440
|
-
total_tokens:
|
|
1468
|
+
var usageSchema = z5.object({
|
|
1469
|
+
prompt_tokens: z5.number(),
|
|
1470
|
+
completion_tokens: z5.number(),
|
|
1471
|
+
total_tokens: z5.number()
|
|
1441
1472
|
});
|
|
1442
|
-
var openaiCompatibleCompletionResponseSchema =
|
|
1443
|
-
id:
|
|
1444
|
-
created:
|
|
1445
|
-
model:
|
|
1446
|
-
choices:
|
|
1447
|
-
|
|
1448
|
-
text:
|
|
1449
|
-
finish_reason:
|
|
1473
|
+
var openaiCompatibleCompletionResponseSchema = z5.object({
|
|
1474
|
+
id: z5.string().nullish(),
|
|
1475
|
+
created: z5.number().nullish(),
|
|
1476
|
+
model: z5.string().nullish(),
|
|
1477
|
+
choices: z5.array(
|
|
1478
|
+
z5.object({
|
|
1479
|
+
text: z5.string(),
|
|
1480
|
+
finish_reason: z5.string()
|
|
1450
1481
|
})
|
|
1451
1482
|
),
|
|
1452
1483
|
usage: usageSchema.nullish()
|
|
1453
1484
|
});
|
|
1454
|
-
var createOpenAICompatibleCompletionChunkSchema = (errorSchema) =>
|
|
1455
|
-
|
|
1456
|
-
id:
|
|
1457
|
-
created:
|
|
1458
|
-
model:
|
|
1459
|
-
choices:
|
|
1460
|
-
|
|
1461
|
-
text:
|
|
1462
|
-
finish_reason:
|
|
1463
|
-
index:
|
|
1485
|
+
var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => z5.union([
|
|
1486
|
+
z5.object({
|
|
1487
|
+
id: z5.string().nullish(),
|
|
1488
|
+
created: z5.number().nullish(),
|
|
1489
|
+
model: z5.string().nullish(),
|
|
1490
|
+
choices: z5.array(
|
|
1491
|
+
z5.object({
|
|
1492
|
+
text: z5.string(),
|
|
1493
|
+
finish_reason: z5.string().nullish(),
|
|
1494
|
+
index: z5.number()
|
|
1464
1495
|
})
|
|
1465
1496
|
),
|
|
1466
1497
|
usage: usageSchema.nullish()
|
|
@@ -1469,27 +1500,38 @@ var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => import_v45.z.
|
|
|
1469
1500
|
]);
|
|
1470
1501
|
|
|
1471
1502
|
// src/embedding/openai-compatible-embedding-model.ts
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1503
|
+
import {
|
|
1504
|
+
TooManyEmbeddingValuesForCallError
|
|
1505
|
+
} from "@ai-sdk/provider";
|
|
1506
|
+
import {
|
|
1507
|
+
combineHeaders as combineHeaders3,
|
|
1508
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler3,
|
|
1509
|
+
createJsonResponseHandler as createJsonResponseHandler3,
|
|
1510
|
+
parseProviderOptions as parseProviderOptions3,
|
|
1511
|
+
postJsonToApi as postJsonToApi3,
|
|
1512
|
+
serializeModelOptions as serializeModelOptions3,
|
|
1513
|
+
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE3,
|
|
1514
|
+
WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE3
|
|
1515
|
+
} from "@ai-sdk/provider-utils";
|
|
1516
|
+
import { z as z7 } from "zod/v4";
|
|
1475
1517
|
|
|
1476
1518
|
// src/embedding/openai-compatible-embedding-options.ts
|
|
1477
|
-
|
|
1478
|
-
var openaiCompatibleEmbeddingModelOptions =
|
|
1519
|
+
import { z as z6 } from "zod/v4";
|
|
1520
|
+
var openaiCompatibleEmbeddingModelOptions = z6.object({
|
|
1479
1521
|
/**
|
|
1480
1522
|
* The number of dimensions the resulting output embeddings should have.
|
|
1481
1523
|
* Only supported in text-embedding-3 and later models.
|
|
1482
1524
|
*/
|
|
1483
|
-
dimensions:
|
|
1525
|
+
dimensions: z6.number().optional(),
|
|
1484
1526
|
/**
|
|
1485
1527
|
* A unique identifier representing your end-user, which can help providers to
|
|
1486
1528
|
* monitor and detect abuse.
|
|
1487
1529
|
*/
|
|
1488
|
-
user:
|
|
1530
|
+
user: z6.string().optional()
|
|
1489
1531
|
});
|
|
1490
1532
|
|
|
1491
1533
|
// src/embedding/openai-compatible-embedding-model.ts
|
|
1492
|
-
var OpenAICompatibleEmbeddingModel = class {
|
|
1534
|
+
var OpenAICompatibleEmbeddingModel = class _OpenAICompatibleEmbeddingModel {
|
|
1493
1535
|
constructor(modelId, config) {
|
|
1494
1536
|
this.specificationVersion = "v4";
|
|
1495
1537
|
this.modelId = modelId;
|
|
@@ -1506,6 +1548,15 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
1506
1548
|
var _a;
|
|
1507
1549
|
return (_a = this.config.supportsParallelCalls) != null ? _a : true;
|
|
1508
1550
|
}
|
|
1551
|
+
static [WORKFLOW_SERIALIZE3](model) {
|
|
1552
|
+
return serializeModelOptions3({
|
|
1553
|
+
modelId: model.modelId,
|
|
1554
|
+
config: model.config
|
|
1555
|
+
});
|
|
1556
|
+
}
|
|
1557
|
+
static [WORKFLOW_DESERIALIZE3](options) {
|
|
1558
|
+
return new _OpenAICompatibleEmbeddingModel(options.modelId, options.config);
|
|
1559
|
+
}
|
|
1509
1560
|
get providerOptionsName() {
|
|
1510
1561
|
return this.config.provider.split(".")[0].trim();
|
|
1511
1562
|
}
|
|
@@ -1515,9 +1566,9 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
1515
1566
|
abortSignal,
|
|
1516
1567
|
providerOptions
|
|
1517
1568
|
}) {
|
|
1518
|
-
var _a, _b, _c;
|
|
1569
|
+
var _a, _b, _c, _d, _e;
|
|
1519
1570
|
const warnings = [];
|
|
1520
|
-
const deprecatedOptions = await (
|
|
1571
|
+
const deprecatedOptions = await parseProviderOptions3({
|
|
1521
1572
|
provider: "openai-compatible",
|
|
1522
1573
|
providerOptions,
|
|
1523
1574
|
schema: openaiCompatibleEmbeddingModelOptions
|
|
@@ -1536,19 +1587,19 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
1536
1587
|
});
|
|
1537
1588
|
const compatibleOptions = Object.assign(
|
|
1538
1589
|
deprecatedOptions != null ? deprecatedOptions : {},
|
|
1539
|
-
(_a = await (
|
|
1590
|
+
(_a = await parseProviderOptions3({
|
|
1540
1591
|
provider: "openaiCompatible",
|
|
1541
1592
|
providerOptions,
|
|
1542
1593
|
schema: openaiCompatibleEmbeddingModelOptions
|
|
1543
1594
|
})) != null ? _a : {},
|
|
1544
|
-
(_b = await (
|
|
1595
|
+
(_b = await parseProviderOptions3({
|
|
1545
1596
|
provider: this.providerOptionsName,
|
|
1546
1597
|
providerOptions,
|
|
1547
1598
|
schema: openaiCompatibleEmbeddingModelOptions
|
|
1548
1599
|
})) != null ? _b : {}
|
|
1549
1600
|
);
|
|
1550
1601
|
if (values.length > this.maxEmbeddingsPerCall) {
|
|
1551
|
-
throw new
|
|
1602
|
+
throw new TooManyEmbeddingValuesForCallError({
|
|
1552
1603
|
provider: this.provider,
|
|
1553
1604
|
modelId: this.modelId,
|
|
1554
1605
|
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
@@ -1559,12 +1610,12 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
1559
1610
|
responseHeaders,
|
|
1560
1611
|
value: response,
|
|
1561
1612
|
rawValue
|
|
1562
|
-
} = await (
|
|
1613
|
+
} = await postJsonToApi3({
|
|
1563
1614
|
url: this.config.url({
|
|
1564
1615
|
path: "/embeddings",
|
|
1565
1616
|
modelId: this.modelId
|
|
1566
1617
|
}),
|
|
1567
|
-
headers: (
|
|
1618
|
+
headers: combineHeaders3((_d = (_c = this.config).headers) == null ? void 0 : _d.call(_c), headers),
|
|
1568
1619
|
body: {
|
|
1569
1620
|
model: this.modelId,
|
|
1570
1621
|
input: values,
|
|
@@ -1572,10 +1623,10 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
1572
1623
|
dimensions: compatibleOptions.dimensions,
|
|
1573
1624
|
user: compatibleOptions.user
|
|
1574
1625
|
},
|
|
1575
|
-
failedResponseHandler: (
|
|
1576
|
-
(
|
|
1626
|
+
failedResponseHandler: createJsonErrorResponseHandler3(
|
|
1627
|
+
(_e = this.config.errorStructure) != null ? _e : defaultOpenAICompatibleErrorStructure
|
|
1577
1628
|
),
|
|
1578
|
-
successfulResponseHandler: (
|
|
1629
|
+
successfulResponseHandler: createJsonResponseHandler3(
|
|
1579
1630
|
openaiTextEmbeddingResponseSchema
|
|
1580
1631
|
),
|
|
1581
1632
|
abortSignal,
|
|
@@ -1590,16 +1641,28 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
1590
1641
|
};
|
|
1591
1642
|
}
|
|
1592
1643
|
};
|
|
1593
|
-
var openaiTextEmbeddingResponseSchema =
|
|
1594
|
-
data:
|
|
1595
|
-
usage:
|
|
1596
|
-
providerMetadata:
|
|
1644
|
+
var openaiTextEmbeddingResponseSchema = z7.object({
|
|
1645
|
+
data: z7.array(z7.object({ embedding: z7.array(z7.number()) })),
|
|
1646
|
+
usage: z7.object({ prompt_tokens: z7.number() }).nullish(),
|
|
1647
|
+
providerMetadata: z7.record(z7.string(), z7.record(z7.string(), z7.any())).optional()
|
|
1597
1648
|
});
|
|
1598
1649
|
|
|
1599
1650
|
// src/image/openai-compatible-image-model.ts
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1651
|
+
import {
|
|
1652
|
+
combineHeaders as combineHeaders4,
|
|
1653
|
+
convertBase64ToUint8Array as convertBase64ToUint8Array2,
|
|
1654
|
+
convertToFormData,
|
|
1655
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler4,
|
|
1656
|
+
createJsonResponseHandler as createJsonResponseHandler4,
|
|
1657
|
+
downloadBlob,
|
|
1658
|
+
postFormDataToApi,
|
|
1659
|
+
postJsonToApi as postJsonToApi4,
|
|
1660
|
+
serializeModelOptions as serializeModelOptions4,
|
|
1661
|
+
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE4,
|
|
1662
|
+
WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE4
|
|
1663
|
+
} from "@ai-sdk/provider-utils";
|
|
1664
|
+
import { z as z8 } from "zod/v4";
|
|
1665
|
+
var OpenAICompatibleImageModel = class _OpenAICompatibleImageModel {
|
|
1603
1666
|
constructor(modelId, config) {
|
|
1604
1667
|
this.modelId = modelId;
|
|
1605
1668
|
this.config = config;
|
|
@@ -1615,6 +1678,15 @@ var OpenAICompatibleImageModel = class {
|
|
|
1615
1678
|
get providerOptionsKey() {
|
|
1616
1679
|
return this.config.provider.split(".")[0].trim();
|
|
1617
1680
|
}
|
|
1681
|
+
static [WORKFLOW_SERIALIZE4](model) {
|
|
1682
|
+
return serializeModelOptions4({
|
|
1683
|
+
modelId: model.modelId,
|
|
1684
|
+
config: model.config
|
|
1685
|
+
});
|
|
1686
|
+
}
|
|
1687
|
+
static [WORKFLOW_DESERIALIZE4](options) {
|
|
1688
|
+
return new _OpenAICompatibleImageModel(options.modelId, options.config);
|
|
1689
|
+
}
|
|
1618
1690
|
getArgs(providerOptions, warnings) {
|
|
1619
1691
|
warnIfDeprecatedProviderOptionsKey({
|
|
1620
1692
|
rawName: this.providerOptionsKey,
|
|
@@ -1638,7 +1710,7 @@ var OpenAICompatibleImageModel = class {
|
|
|
1638
1710
|
files,
|
|
1639
1711
|
mask
|
|
1640
1712
|
}) {
|
|
1641
|
-
var _a, _b, _c, _d, _e;
|
|
1713
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1642
1714
|
const warnings = [];
|
|
1643
1715
|
if (aspectRatio != null) {
|
|
1644
1716
|
warnings.push({
|
|
@@ -1653,13 +1725,13 @@ var OpenAICompatibleImageModel = class {
|
|
|
1653
1725
|
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
1654
1726
|
const args = this.getArgs(providerOptions, warnings);
|
|
1655
1727
|
if (files != null && files.length > 0) {
|
|
1656
|
-
const { value: response2, responseHeaders: responseHeaders2 } = await
|
|
1728
|
+
const { value: response2, responseHeaders: responseHeaders2 } = await postFormDataToApi({
|
|
1657
1729
|
url: this.config.url({
|
|
1658
1730
|
path: "/images/edits",
|
|
1659
1731
|
modelId: this.modelId
|
|
1660
1732
|
}),
|
|
1661
|
-
headers: (
|
|
1662
|
-
formData:
|
|
1733
|
+
headers: combineHeaders4((_e = (_d = this.config).headers) == null ? void 0 : _e.call(_d), headers),
|
|
1734
|
+
formData: convertToFormData({
|
|
1663
1735
|
model: this.modelId,
|
|
1664
1736
|
prompt,
|
|
1665
1737
|
image: await Promise.all(files.map((file) => fileToBlob(file))),
|
|
@@ -1668,10 +1740,10 @@ var OpenAICompatibleImageModel = class {
|
|
|
1668
1740
|
size,
|
|
1669
1741
|
...args
|
|
1670
1742
|
}),
|
|
1671
|
-
failedResponseHandler: (
|
|
1672
|
-
(
|
|
1743
|
+
failedResponseHandler: createJsonErrorResponseHandler4(
|
|
1744
|
+
(_f = this.config.errorStructure) != null ? _f : defaultOpenAICompatibleErrorStructure
|
|
1673
1745
|
),
|
|
1674
|
-
successfulResponseHandler: (
|
|
1746
|
+
successfulResponseHandler: createJsonResponseHandler4(
|
|
1675
1747
|
openaiCompatibleImageResponseSchema
|
|
1676
1748
|
),
|
|
1677
1749
|
abortSignal,
|
|
@@ -1687,12 +1759,12 @@ var OpenAICompatibleImageModel = class {
|
|
|
1687
1759
|
}
|
|
1688
1760
|
};
|
|
1689
1761
|
}
|
|
1690
|
-
const { value: response, responseHeaders } = await (
|
|
1762
|
+
const { value: response, responseHeaders } = await postJsonToApi4({
|
|
1691
1763
|
url: this.config.url({
|
|
1692
1764
|
path: "/images/generations",
|
|
1693
1765
|
modelId: this.modelId
|
|
1694
1766
|
}),
|
|
1695
|
-
headers: (
|
|
1767
|
+
headers: combineHeaders4((_h = (_g = this.config).headers) == null ? void 0 : _h.call(_g), headers),
|
|
1696
1768
|
body: {
|
|
1697
1769
|
model: this.modelId,
|
|
1698
1770
|
prompt,
|
|
@@ -1701,10 +1773,10 @@ var OpenAICompatibleImageModel = class {
|
|
|
1701
1773
|
...args,
|
|
1702
1774
|
response_format: "b64_json"
|
|
1703
1775
|
},
|
|
1704
|
-
failedResponseHandler: (
|
|
1705
|
-
(
|
|
1776
|
+
failedResponseHandler: createJsonErrorResponseHandler4(
|
|
1777
|
+
(_i = this.config.errorStructure) != null ? _i : defaultOpenAICompatibleErrorStructure
|
|
1706
1778
|
),
|
|
1707
|
-
successfulResponseHandler: (
|
|
1779
|
+
successfulResponseHandler: createJsonResponseHandler4(
|
|
1708
1780
|
openaiCompatibleImageResponseSchema
|
|
1709
1781
|
),
|
|
1710
1782
|
abortSignal,
|
|
@@ -1721,32 +1793,35 @@ var OpenAICompatibleImageModel = class {
|
|
|
1721
1793
|
};
|
|
1722
1794
|
}
|
|
1723
1795
|
};
|
|
1724
|
-
var openaiCompatibleImageResponseSchema =
|
|
1725
|
-
data:
|
|
1796
|
+
var openaiCompatibleImageResponseSchema = z8.object({
|
|
1797
|
+
data: z8.array(z8.object({ b64_json: z8.string() }))
|
|
1726
1798
|
});
|
|
1727
1799
|
async function fileToBlob(file) {
|
|
1728
1800
|
if (file.type === "url") {
|
|
1729
|
-
return
|
|
1801
|
+
return downloadBlob(file.url);
|
|
1730
1802
|
}
|
|
1731
|
-
const data = file.data instanceof Uint8Array ? file.data : (
|
|
1803
|
+
const data = file.data instanceof Uint8Array ? file.data : convertBase64ToUint8Array2(file.data);
|
|
1732
1804
|
return new Blob([data], { type: file.mediaType });
|
|
1733
1805
|
}
|
|
1734
1806
|
|
|
1735
1807
|
// src/openai-compatible-provider.ts
|
|
1736
|
-
|
|
1808
|
+
import {
|
|
1809
|
+
withoutTrailingSlash,
|
|
1810
|
+
withUserAgentSuffix
|
|
1811
|
+
} from "@ai-sdk/provider-utils";
|
|
1737
1812
|
|
|
1738
1813
|
// src/version.ts
|
|
1739
|
-
var VERSION = true ? "3.0.0-beta.
|
|
1814
|
+
var VERSION = true ? "3.0.0-beta.25" : "0.0.0-test";
|
|
1740
1815
|
|
|
1741
1816
|
// src/openai-compatible-provider.ts
|
|
1742
1817
|
function createOpenAICompatible(options) {
|
|
1743
|
-
const baseURL =
|
|
1818
|
+
const baseURL = withoutTrailingSlash(options.baseURL);
|
|
1744
1819
|
const providerName = options.name;
|
|
1745
1820
|
const headers = {
|
|
1746
1821
|
...options.apiKey && { Authorization: `Bearer ${options.apiKey}` },
|
|
1747
1822
|
...options.headers
|
|
1748
1823
|
};
|
|
1749
|
-
const getHeaders = () =>
|
|
1824
|
+
const getHeaders = () => withUserAgentSuffix(headers, `ai-sdk/openai-compatible/${VERSION}`);
|
|
1750
1825
|
const getCommonModelConfig = (modelType) => ({
|
|
1751
1826
|
provider: `${providerName}.${modelType}`,
|
|
1752
1827
|
url: ({ path }) => {
|
|
@@ -1785,13 +1860,12 @@ function createOpenAICompatible(options) {
|
|
|
1785
1860
|
provider.imageModel = createImageModel;
|
|
1786
1861
|
return provider;
|
|
1787
1862
|
}
|
|
1788
|
-
|
|
1789
|
-
0 && (module.exports = {
|
|
1863
|
+
export {
|
|
1790
1864
|
OpenAICompatibleChatLanguageModel,
|
|
1791
1865
|
OpenAICompatibleCompletionLanguageModel,
|
|
1792
1866
|
OpenAICompatibleEmbeddingModel,
|
|
1793
1867
|
OpenAICompatibleImageModel,
|
|
1794
1868
|
VERSION,
|
|
1795
1869
|
createOpenAICompatible
|
|
1796
|
-
}
|
|
1870
|
+
};
|
|
1797
1871
|
//# sourceMappingURL=index.js.map
|