@ai-sdk/openai-compatible 3.0.0-beta.5 → 3.0.0-beta.57
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 +432 -8
- package/README.md +2 -0
- package/dist/index.d.ts +69 -8
- package/dist/index.js +548 -428
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +20 -2
- package/dist/internal/index.js +91 -91
- package/dist/internal/index.js.map +1 -1
- package/docs/index.mdx +1 -0
- package/package.json +16 -17
- package/src/chat/convert-openai-compatible-chat-usage.ts +1 -1
- package/src/chat/convert-to-openai-compatible-chat-messages.ts +94 -73
- package/src/chat/map-openai-compatible-finish-reason.ts +1 -1
- package/src/chat/openai-compatible-api-types.ts +3 -5
- package/src/chat/openai-compatible-chat-language-model.ts +205 -191
- package/src/chat/openai-compatible-metadata-extractor.ts +1 -1
- package/src/chat/openai-compatible-prepare-tools.ts +2 -3
- package/src/completion/convert-openai-compatible-completion-usage.ts +1 -1
- package/src/completion/convert-to-openai-compatible-completion-prompt.ts +1 -2
- package/src/completion/map-openai-compatible-finish-reason.ts +1 -1
- package/src/completion/openai-compatible-completion-language-model.ts +52 -17
- package/src/embedding/openai-compatible-embedding-model.ts +36 -10
- package/src/image/openai-compatible-image-model.ts +35 -13
- package/src/index.ts +3 -3
- package/src/openai-compatible-error.ts +1 -2
- package/src/openai-compatible-provider.ts +18 -5
- package/src/utils/to-camel-case.ts +43 -0
- package/dist/index.d.mts +0 -290
- package/dist/index.mjs +0 -1742
- package/dist/index.mjs.map +0 -1
- package/dist/internal/index.d.mts +0 -193
- package/dist/internal/index.mjs +0 -340
- package/dist/internal/index.mjs.map +0 -1
- /package/src/chat/{openai-compatible-chat-options.ts → openai-compatible-chat-language-model-options.ts} +0 -0
- /package/src/completion/{openai-compatible-completion-options.ts → openai-compatible-completion-language-model-options.ts} +0 -0
- /package/src/embedding/{openai-compatible-embedding-options.ts → openai-compatible-embedding-model-options.ts} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,50 +1,57 @@
|
|
|
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
|
+
combineHeaders,
|
|
4
|
+
createEventSourceResponseHandler,
|
|
5
|
+
createJsonErrorResponseHandler,
|
|
6
|
+
createJsonResponseHandler,
|
|
7
|
+
generateId,
|
|
8
|
+
isCustomReasoning,
|
|
9
|
+
parseProviderOptions,
|
|
10
|
+
postJsonToApi,
|
|
11
|
+
serializeModelOptions,
|
|
12
|
+
StreamingToolCallTracker,
|
|
13
|
+
WORKFLOW_SERIALIZE,
|
|
14
|
+
WORKFLOW_DESERIALIZE
|
|
15
|
+
} from "@ai-sdk/provider-utils";
|
|
16
|
+
import { z as z3 } from "zod/v4";
|
|
17
|
+
|
|
18
|
+
// src/utils/to-camel-case.ts
|
|
19
|
+
function toCamelCase(str) {
|
|
20
|
+
return str.replace(/[_-]([a-z])/g, (g) => g[1].toUpperCase());
|
|
21
|
+
}
|
|
22
|
+
function resolveProviderOptionsKey(rawName, providerOptions) {
|
|
23
|
+
const camelName = toCamelCase(rawName);
|
|
24
|
+
if (camelName !== rawName && (providerOptions == null ? void 0 : providerOptions[camelName]) != null) {
|
|
25
|
+
return camelName;
|
|
26
|
+
}
|
|
27
|
+
return rawName;
|
|
28
|
+
}
|
|
29
|
+
function warnIfDeprecatedProviderOptionsKey({
|
|
30
|
+
rawName,
|
|
31
|
+
providerOptions,
|
|
32
|
+
warnings
|
|
33
|
+
}) {
|
|
34
|
+
const camelName = toCamelCase(rawName);
|
|
35
|
+
if (camelName !== rawName && (providerOptions == null ? void 0 : providerOptions[rawName]) != null) {
|
|
36
|
+
warnings.push({
|
|
37
|
+
type: "deprecated",
|
|
38
|
+
setting: `providerOptions key '${rawName}'`,
|
|
39
|
+
message: `Use '${camelName}' instead.`
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
36
43
|
|
|
37
44
|
// src/openai-compatible-error.ts
|
|
38
|
-
|
|
39
|
-
var openaiCompatibleErrorDataSchema =
|
|
40
|
-
error:
|
|
41
|
-
message:
|
|
45
|
+
import { z } from "zod/v4";
|
|
46
|
+
var openaiCompatibleErrorDataSchema = z.object({
|
|
47
|
+
error: z.object({
|
|
48
|
+
message: z.string(),
|
|
42
49
|
// The additional information below is handled loosely to support
|
|
43
50
|
// OpenAI-compatible providers that have slightly different error
|
|
44
51
|
// responses:
|
|
45
|
-
type:
|
|
46
|
-
param:
|
|
47
|
-
code:
|
|
52
|
+
type: z.string().nullish(),
|
|
53
|
+
param: z.any().nullish(),
|
|
54
|
+
code: z.union([z.string(), z.number()]).nullish()
|
|
48
55
|
})
|
|
49
56
|
});
|
|
50
57
|
var defaultOpenAICompatibleErrorStructure = {
|
|
@@ -92,8 +99,15 @@ function convertOpenAICompatibleChatUsage(usage) {
|
|
|
92
99
|
}
|
|
93
100
|
|
|
94
101
|
// src/chat/convert-to-openai-compatible-chat-messages.ts
|
|
95
|
-
|
|
96
|
-
|
|
102
|
+
import {
|
|
103
|
+
UnsupportedFunctionalityError
|
|
104
|
+
} from "@ai-sdk/provider";
|
|
105
|
+
import {
|
|
106
|
+
convertBase64ToUint8Array,
|
|
107
|
+
convertToBase64,
|
|
108
|
+
getTopLevelMediaType,
|
|
109
|
+
resolveFullMediaType
|
|
110
|
+
} from "@ai-sdk/provider-utils";
|
|
97
111
|
function getOpenAIMetadata(message) {
|
|
98
112
|
var _a, _b;
|
|
99
113
|
return (_b = (_a = message == null ? void 0 : message.providerOptions) == null ? void 0 : _a.openaiCompatible) != null ? _b : {};
|
|
@@ -138,65 +152,87 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
138
152
|
return { type: "text", text: part.text, ...partMetadata };
|
|
139
153
|
}
|
|
140
154
|
case "file": {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
image_url: {
|
|
146
|
-
url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${(0, import_provider_utils.convertToBase64)(part.data)}`
|
|
147
|
-
},
|
|
148
|
-
...partMetadata
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
if (part.mediaType.startsWith("audio/")) {
|
|
152
|
-
if (part.data instanceof URL) {
|
|
153
|
-
throw new import_provider.UnsupportedFunctionalityError({
|
|
154
|
-
functionality: "audio file parts with URLs"
|
|
155
|
+
switch (part.data.type) {
|
|
156
|
+
case "reference": {
|
|
157
|
+
throw new UnsupportedFunctionalityError({
|
|
158
|
+
functionality: "file parts with provider references"
|
|
155
159
|
});
|
|
156
160
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
functionality: `audio media type ${part.mediaType}`
|
|
161
|
+
case "text": {
|
|
162
|
+
throw new UnsupportedFunctionalityError({
|
|
163
|
+
functionality: "text file parts"
|
|
161
164
|
});
|
|
162
165
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
166
|
+
case "url":
|
|
167
|
+
case "data": {
|
|
168
|
+
const topLevel = getTopLevelMediaType(part.mediaType);
|
|
169
|
+
if (topLevel === "image") {
|
|
170
|
+
return {
|
|
171
|
+
type: "image_url",
|
|
172
|
+
image_url: {
|
|
173
|
+
url: part.data.type === "url" ? part.data.url.toString() : `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`
|
|
174
|
+
},
|
|
175
|
+
...partMetadata
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
if (topLevel === "audio") {
|
|
179
|
+
if (part.data.type === "url") {
|
|
180
|
+
throw new UnsupportedFunctionalityError({
|
|
181
|
+
functionality: "audio file parts with URLs"
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
const fullMediaType = resolveFullMediaType({ part });
|
|
185
|
+
const format = getAudioFormat(fullMediaType);
|
|
186
|
+
if (format === null) {
|
|
187
|
+
throw new UnsupportedFunctionalityError({
|
|
188
|
+
functionality: `audio media type ${fullMediaType}`
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
return {
|
|
192
|
+
type: "input_audio",
|
|
193
|
+
input_audio: {
|
|
194
|
+
data: convertToBase64(part.data.data),
|
|
195
|
+
format
|
|
196
|
+
},
|
|
197
|
+
...partMetadata
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
if (topLevel === "application") {
|
|
201
|
+
if (part.data.type === "url") {
|
|
202
|
+
throw new UnsupportedFunctionalityError({
|
|
203
|
+
functionality: "PDF file parts with URLs"
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
const fullMediaType = resolveFullMediaType({ part });
|
|
207
|
+
if (fullMediaType !== "application/pdf") {
|
|
208
|
+
throw new UnsupportedFunctionalityError({
|
|
209
|
+
functionality: `file part media type ${fullMediaType}`
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
return {
|
|
213
|
+
type: "file",
|
|
214
|
+
file: {
|
|
215
|
+
filename: (_a2 = part.filename) != null ? _a2 : "document.pdf",
|
|
216
|
+
file_data: `data:application/pdf;base64,${convertToBase64(part.data.data)}`
|
|
217
|
+
},
|
|
218
|
+
...partMetadata
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
if (topLevel === "text") {
|
|
222
|
+
const textContent = part.data.type === "url" ? part.data.url.toString() : typeof part.data.data === "string" ? new TextDecoder().decode(
|
|
223
|
+
convertBase64ToUint8Array(part.data.data)
|
|
224
|
+
) : new TextDecoder().decode(part.data.data);
|
|
225
|
+
return {
|
|
226
|
+
type: "text",
|
|
227
|
+
text: textContent,
|
|
228
|
+
...partMetadata
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
throw new UnsupportedFunctionalityError({
|
|
232
|
+
functionality: `file part media type ${part.mediaType}`
|
|
176
233
|
});
|
|
177
234
|
}
|
|
178
|
-
return {
|
|
179
|
-
type: "file",
|
|
180
|
-
file: {
|
|
181
|
-
filename: (_a2 = part.filename) != null ? _a2 : "document.pdf",
|
|
182
|
-
file_data: `data:application/pdf;base64,${(0, import_provider_utils.convertToBase64)(part.data)}`
|
|
183
|
-
},
|
|
184
|
-
...partMetadata
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
if (part.mediaType.startsWith("text/")) {
|
|
188
|
-
const textContent = part.data instanceof URL ? part.data.toString() : typeof part.data === "string" ? new TextDecoder().decode(
|
|
189
|
-
(0, import_provider_utils.convertBase64ToUint8Array)(part.data)
|
|
190
|
-
) : new TextDecoder().decode(part.data);
|
|
191
|
-
return {
|
|
192
|
-
type: "text",
|
|
193
|
-
text: textContent,
|
|
194
|
-
...partMetadata
|
|
195
|
-
};
|
|
196
235
|
}
|
|
197
|
-
throw new import_provider.UnsupportedFunctionalityError({
|
|
198
|
-
functionality: `file part media type ${part.mediaType}`
|
|
199
|
-
});
|
|
200
236
|
}
|
|
201
237
|
}
|
|
202
238
|
}),
|
|
@@ -244,7 +280,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
244
280
|
}
|
|
245
281
|
messages.push({
|
|
246
282
|
role: "assistant",
|
|
247
|
-
content: text,
|
|
283
|
+
content: toolCalls.length > 0 ? text || null : text,
|
|
248
284
|
...reasoning.length > 0 ? { reasoning_content: reasoning } : {},
|
|
249
285
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
250
286
|
...metadata
|
|
@@ -264,7 +300,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
264
300
|
contentValue = output.value;
|
|
265
301
|
break;
|
|
266
302
|
case "execution-denied":
|
|
267
|
-
contentValue = (_c = output.reason) != null ? _c : "Tool execution denied.";
|
|
303
|
+
contentValue = (_c = output.reason) != null ? _c : "Tool call execution denied.";
|
|
268
304
|
break;
|
|
269
305
|
case "content":
|
|
270
306
|
case "json":
|
|
@@ -321,22 +357,22 @@ function mapOpenAICompatibleFinishReason(finishReason) {
|
|
|
321
357
|
}
|
|
322
358
|
}
|
|
323
359
|
|
|
324
|
-
// src/chat/openai-compatible-chat-options.ts
|
|
325
|
-
|
|
326
|
-
var openaiCompatibleLanguageModelChatOptions =
|
|
360
|
+
// src/chat/openai-compatible-chat-language-model-options.ts
|
|
361
|
+
import { z as z2 } from "zod/v4";
|
|
362
|
+
var openaiCompatibleLanguageModelChatOptions = z2.object({
|
|
327
363
|
/**
|
|
328
364
|
* A unique identifier representing your end-user, which can help the provider to
|
|
329
365
|
* monitor and detect abuse.
|
|
330
366
|
*/
|
|
331
|
-
user:
|
|
367
|
+
user: z2.string().optional(),
|
|
332
368
|
/**
|
|
333
369
|
* Reasoning effort for reasoning models. Defaults to `medium`.
|
|
334
370
|
*/
|
|
335
|
-
reasoningEffort:
|
|
371
|
+
reasoningEffort: z2.string().optional(),
|
|
336
372
|
/**
|
|
337
373
|
* Controls the verbosity of the generated text. Defaults to `medium`.
|
|
338
374
|
*/
|
|
339
|
-
textVerbosity:
|
|
375
|
+
textVerbosity: z2.string().optional(),
|
|
340
376
|
/**
|
|
341
377
|
* Whether to use strict JSON schema validation.
|
|
342
378
|
* When true, the model uses constrained decoding to guarantee schema compliance.
|
|
@@ -344,11 +380,13 @@ var openaiCompatibleLanguageModelChatOptions = import_v42.z.object({
|
|
|
344
380
|
*
|
|
345
381
|
* @default true
|
|
346
382
|
*/
|
|
347
|
-
strictJsonSchema:
|
|
383
|
+
strictJsonSchema: z2.boolean().optional()
|
|
348
384
|
});
|
|
349
385
|
|
|
350
386
|
// src/chat/openai-compatible-prepare-tools.ts
|
|
351
|
-
|
|
387
|
+
import {
|
|
388
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
389
|
+
} from "@ai-sdk/provider";
|
|
352
390
|
function prepareTools({
|
|
353
391
|
tools,
|
|
354
392
|
toolChoice
|
|
@@ -397,7 +435,7 @@ function prepareTools({
|
|
|
397
435
|
};
|
|
398
436
|
default: {
|
|
399
437
|
const _exhaustiveCheck = type;
|
|
400
|
-
throw new
|
|
438
|
+
throw new UnsupportedFunctionalityError2({
|
|
401
439
|
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
402
440
|
});
|
|
403
441
|
}
|
|
@@ -405,8 +443,7 @@ function prepareTools({
|
|
|
405
443
|
}
|
|
406
444
|
|
|
407
445
|
// src/chat/openai-compatible-chat-language-model.ts
|
|
408
|
-
var OpenAICompatibleChatLanguageModel = class {
|
|
409
|
-
// type inferred via constructor
|
|
446
|
+
var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel {
|
|
410
447
|
constructor(modelId, config) {
|
|
411
448
|
this.specificationVersion = "v4";
|
|
412
449
|
var _a, _b;
|
|
@@ -416,9 +453,22 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
416
453
|
this.chunkSchema = createOpenAICompatibleChatChunkSchema(
|
|
417
454
|
errorStructure.errorSchema
|
|
418
455
|
);
|
|
419
|
-
this.failedResponseHandler =
|
|
456
|
+
this.failedResponseHandler = createJsonErrorResponseHandler(errorStructure);
|
|
420
457
|
this.supportsStructuredOutputs = (_b = config.supportsStructuredOutputs) != null ? _b : false;
|
|
421
458
|
}
|
|
459
|
+
// type inferred via constructor
|
|
460
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
461
|
+
return serializeModelOptions({
|
|
462
|
+
modelId: model.modelId,
|
|
463
|
+
config: model.config
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
467
|
+
return new _OpenAICompatibleChatLanguageModel(
|
|
468
|
+
options.modelId,
|
|
469
|
+
options.config
|
|
470
|
+
);
|
|
471
|
+
}
|
|
422
472
|
get provider() {
|
|
423
473
|
return this.config.provider;
|
|
424
474
|
}
|
|
@@ -433,6 +483,10 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
433
483
|
var _a, _b, _c;
|
|
434
484
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
435
485
|
}
|
|
486
|
+
convertUsage(usage) {
|
|
487
|
+
var _a, _b, _c;
|
|
488
|
+
return (_c = (_b = (_a = this.config).convertUsage) == null ? void 0 : _b.call(_a, usage)) != null ? _c : convertOpenAICompatibleChatUsage(usage);
|
|
489
|
+
}
|
|
436
490
|
async getArgs({
|
|
437
491
|
prompt,
|
|
438
492
|
maxOutputTokens,
|
|
@@ -441,6 +495,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
441
495
|
topK,
|
|
442
496
|
frequencyPenalty,
|
|
443
497
|
presencePenalty,
|
|
498
|
+
reasoning,
|
|
444
499
|
providerOptions,
|
|
445
500
|
stopSequences,
|
|
446
501
|
responseFormat,
|
|
@@ -448,33 +503,44 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
448
503
|
toolChoice,
|
|
449
504
|
tools
|
|
450
505
|
}) {
|
|
451
|
-
var _a, _b, _c, _d, _e;
|
|
506
|
+
var _a, _b, _c, _d, _e, _f;
|
|
452
507
|
const warnings = [];
|
|
453
|
-
const deprecatedOptions = await
|
|
508
|
+
const deprecatedOptions = await parseProviderOptions({
|
|
454
509
|
provider: "openai-compatible",
|
|
455
510
|
providerOptions,
|
|
456
511
|
schema: openaiCompatibleLanguageModelChatOptions
|
|
457
512
|
});
|
|
458
513
|
if (deprecatedOptions != null) {
|
|
459
514
|
warnings.push({
|
|
460
|
-
type: "
|
|
461
|
-
|
|
515
|
+
type: "deprecated",
|
|
516
|
+
setting: "providerOptions key 'openai-compatible'",
|
|
517
|
+
message: "Use 'openaiCompatible' instead."
|
|
462
518
|
});
|
|
463
519
|
}
|
|
520
|
+
warnIfDeprecatedProviderOptionsKey({
|
|
521
|
+
rawName: this.providerOptionsName,
|
|
522
|
+
providerOptions,
|
|
523
|
+
warnings
|
|
524
|
+
});
|
|
464
525
|
const compatibleOptions = Object.assign(
|
|
465
526
|
deprecatedOptions != null ? deprecatedOptions : {},
|
|
466
|
-
(_a = await
|
|
527
|
+
(_a = await parseProviderOptions({
|
|
467
528
|
provider: "openaiCompatible",
|
|
468
529
|
providerOptions,
|
|
469
530
|
schema: openaiCompatibleLanguageModelChatOptions
|
|
470
531
|
})) != null ? _a : {},
|
|
471
|
-
(_b = await
|
|
532
|
+
(_b = await parseProviderOptions({
|
|
472
533
|
provider: this.providerOptionsName,
|
|
473
534
|
providerOptions,
|
|
474
535
|
schema: openaiCompatibleLanguageModelChatOptions
|
|
475
|
-
})) != null ? _b : {}
|
|
536
|
+
})) != null ? _b : {},
|
|
537
|
+
(_c = await parseProviderOptions({
|
|
538
|
+
provider: toCamelCase(this.providerOptionsName),
|
|
539
|
+
providerOptions,
|
|
540
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
541
|
+
})) != null ? _c : {}
|
|
476
542
|
);
|
|
477
|
-
const strictJsonSchema = (
|
|
543
|
+
const strictJsonSchema = (_d = compatibleOptions == null ? void 0 : compatibleOptions.strictJsonSchema) != null ? _d : true;
|
|
478
544
|
if (topK != null) {
|
|
479
545
|
warnings.push({ type: "unsupported", feature: "topK" });
|
|
480
546
|
}
|
|
@@ -493,7 +559,12 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
493
559
|
tools,
|
|
494
560
|
toolChoice
|
|
495
561
|
});
|
|
562
|
+
const metadataKey = resolveProviderOptionsKey(
|
|
563
|
+
this.providerOptionsName,
|
|
564
|
+
providerOptions
|
|
565
|
+
);
|
|
496
566
|
return {
|
|
567
|
+
metadataKey,
|
|
497
568
|
args: {
|
|
498
569
|
// model id:
|
|
499
570
|
model: this.modelId,
|
|
@@ -510,22 +581,23 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
510
581
|
json_schema: {
|
|
511
582
|
schema: responseFormat.schema,
|
|
512
583
|
strict: strictJsonSchema,
|
|
513
|
-
name: (
|
|
584
|
+
name: (_e = responseFormat.name) != null ? _e : "response",
|
|
514
585
|
description: responseFormat.description
|
|
515
586
|
}
|
|
516
587
|
} : { type: "json_object" } : void 0,
|
|
517
588
|
stop: stopSequences,
|
|
518
589
|
seed,
|
|
519
590
|
...Object.fromEntries(
|
|
520
|
-
Object.entries(
|
|
521
|
-
|
|
522
|
-
|
|
591
|
+
Object.entries({
|
|
592
|
+
...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
|
|
593
|
+
...providerOptions == null ? void 0 : providerOptions[toCamelCase(this.providerOptionsName)]
|
|
594
|
+
}).filter(
|
|
523
595
|
([key]) => !Object.keys(
|
|
524
596
|
openaiCompatibleLanguageModelChatOptions.shape
|
|
525
597
|
).includes(key)
|
|
526
598
|
)
|
|
527
599
|
),
|
|
528
|
-
reasoning_effort: compatibleOptions.reasoningEffort,
|
|
600
|
+
reasoning_effort: (_f = compatibleOptions.reasoningEffort) != null ? _f : isCustomReasoning(reasoning) && reasoning !== "none" ? reasoning : void 0,
|
|
529
601
|
verbosity: compatibleOptions.textVerbosity,
|
|
530
602
|
// messages:
|
|
531
603
|
messages: convertToOpenAICompatibleChatMessages(prompt),
|
|
@@ -537,23 +609,23 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
537
609
|
};
|
|
538
610
|
}
|
|
539
611
|
async doGenerate(options) {
|
|
540
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
541
|
-
const { args, warnings } = await this.getArgs({ ...options });
|
|
612
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
613
|
+
const { args, warnings, metadataKey } = await this.getArgs({ ...options });
|
|
542
614
|
const transformedBody = this.transformRequestBody(args);
|
|
543
615
|
const body = JSON.stringify(transformedBody);
|
|
544
616
|
const {
|
|
545
617
|
responseHeaders,
|
|
546
618
|
value: responseBody,
|
|
547
619
|
rawValue: rawResponse
|
|
548
|
-
} = await
|
|
620
|
+
} = await postJsonToApi({
|
|
549
621
|
url: this.config.url({
|
|
550
622
|
path: "/chat/completions",
|
|
551
623
|
modelId: this.modelId
|
|
552
624
|
}),
|
|
553
|
-
headers: (
|
|
625
|
+
headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
554
626
|
body: transformedBody,
|
|
555
627
|
failedResponseHandler: this.failedResponseHandler,
|
|
556
|
-
successfulResponseHandler:
|
|
628
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
557
629
|
OpenAICompatibleChatResponseSchema
|
|
558
630
|
),
|
|
559
631
|
abortSignal: options.abortSignal,
|
|
@@ -565,7 +637,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
565
637
|
if (text != null && text.length > 0) {
|
|
566
638
|
content.push({ type: "text", text });
|
|
567
639
|
}
|
|
568
|
-
const reasoning = (
|
|
640
|
+
const reasoning = (_c = choice.message.reasoning_content) != null ? _c : choice.message.reasoning;
|
|
569
641
|
if (reasoning != null && reasoning.length > 0) {
|
|
570
642
|
content.push({
|
|
571
643
|
type: "reasoning",
|
|
@@ -574,40 +646,40 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
574
646
|
}
|
|
575
647
|
if (choice.message.tool_calls != null) {
|
|
576
648
|
for (const toolCall of choice.message.tool_calls) {
|
|
577
|
-
const thoughtSignature = (
|
|
649
|
+
const thoughtSignature = (_e = (_d = toolCall.extra_content) == null ? void 0 : _d.google) == null ? void 0 : _e.thought_signature;
|
|
578
650
|
content.push({
|
|
579
651
|
type: "tool-call",
|
|
580
|
-
toolCallId: (
|
|
652
|
+
toolCallId: (_f = toolCall.id) != null ? _f : generateId(),
|
|
581
653
|
toolName: toolCall.function.name,
|
|
582
654
|
input: toolCall.function.arguments,
|
|
583
655
|
...thoughtSignature ? {
|
|
584
656
|
providerMetadata: {
|
|
585
|
-
[
|
|
657
|
+
[metadataKey]: { thoughtSignature }
|
|
586
658
|
}
|
|
587
659
|
} : {}
|
|
588
660
|
});
|
|
589
661
|
}
|
|
590
662
|
}
|
|
591
663
|
const providerMetadata = {
|
|
592
|
-
[
|
|
593
|
-
...await ((
|
|
664
|
+
[metadataKey]: {},
|
|
665
|
+
...await ((_h = (_g = this.config.metadataExtractor) == null ? void 0 : _g.extractMetadata) == null ? void 0 : _h.call(_g, {
|
|
594
666
|
parsedBody: rawResponse
|
|
595
667
|
}))
|
|
596
668
|
};
|
|
597
|
-
const completionTokenDetails = (
|
|
669
|
+
const completionTokenDetails = (_i = responseBody.usage) == null ? void 0 : _i.completion_tokens_details;
|
|
598
670
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
|
|
599
|
-
providerMetadata[
|
|
671
|
+
providerMetadata[metadataKey].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
|
|
600
672
|
}
|
|
601
673
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
|
|
602
|
-
providerMetadata[
|
|
674
|
+
providerMetadata[metadataKey].rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
|
|
603
675
|
}
|
|
604
676
|
return {
|
|
605
677
|
content,
|
|
606
678
|
finishReason: {
|
|
607
679
|
unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
|
608
|
-
raw: (
|
|
680
|
+
raw: (_j = choice.finish_reason) != null ? _j : void 0
|
|
609
681
|
},
|
|
610
|
-
usage:
|
|
682
|
+
usage: this.convertUsage(responseBody.usage),
|
|
611
683
|
providerMetadata,
|
|
612
684
|
request: { body },
|
|
613
685
|
response: {
|
|
@@ -619,8 +691,10 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
619
691
|
};
|
|
620
692
|
}
|
|
621
693
|
async doStream(options) {
|
|
622
|
-
var _a;
|
|
623
|
-
const { args, warnings } = await this.getArgs({
|
|
694
|
+
var _a, _b, _c;
|
|
695
|
+
const { args, warnings, metadataKey } = await this.getArgs({
|
|
696
|
+
...options
|
|
697
|
+
});
|
|
624
698
|
const body = this.transformRequestBody({
|
|
625
699
|
...args,
|
|
626
700
|
stream: true,
|
|
@@ -628,38 +702,96 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
628
702
|
stream_options: this.config.includeUsage ? { include_usage: true } : void 0
|
|
629
703
|
});
|
|
630
704
|
const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
|
|
631
|
-
const { responseHeaders, value: response } = await
|
|
705
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
632
706
|
url: this.config.url({
|
|
633
707
|
path: "/chat/completions",
|
|
634
708
|
modelId: this.modelId
|
|
635
709
|
}),
|
|
636
|
-
headers: (
|
|
710
|
+
headers: combineHeaders((_c = (_b = this.config).headers) == null ? void 0 : _c.call(_b), options.headers),
|
|
637
711
|
body,
|
|
638
712
|
failedResponseHandler: this.failedResponseHandler,
|
|
639
|
-
successfulResponseHandler:
|
|
713
|
+
successfulResponseHandler: createEventSourceResponseHandler(
|
|
640
714
|
this.chunkSchema
|
|
641
715
|
),
|
|
642
716
|
abortSignal: options.abortSignal,
|
|
643
717
|
fetch: this.config.fetch
|
|
644
718
|
});
|
|
645
|
-
const
|
|
719
|
+
const providerOptionsName = metadataKey;
|
|
720
|
+
let toolCallTracker;
|
|
721
|
+
const pendingToolCalls = /* @__PURE__ */ new Map();
|
|
722
|
+
const forwardedToolCallIndices = /* @__PURE__ */ new Set();
|
|
723
|
+
const processToolCallDelta = (toolCallDelta) => {
|
|
724
|
+
var _a2, _b2, _c2, _d, _e;
|
|
725
|
+
const index = toolCallDelta.index;
|
|
726
|
+
if (index == null || forwardedToolCallIndices.has(index)) {
|
|
727
|
+
toolCallTracker.processDelta(toolCallDelta);
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
let pending = pendingToolCalls.get(index);
|
|
731
|
+
if (pending == null) {
|
|
732
|
+
pending = {
|
|
733
|
+
id: (_a2 = toolCallDelta.id) != null ? _a2 : null,
|
|
734
|
+
bufferedArguments: "",
|
|
735
|
+
extraContent: (_b2 = toolCallDelta.extra_content) != null ? _b2 : null
|
|
736
|
+
};
|
|
737
|
+
pendingToolCalls.set(index, pending);
|
|
738
|
+
} else {
|
|
739
|
+
if (pending.id == null && toolCallDelta.id != null) {
|
|
740
|
+
pending.id = toolCallDelta.id;
|
|
741
|
+
}
|
|
742
|
+
if (pending.extraContent == null && toolCallDelta.extra_content != null) {
|
|
743
|
+
pending.extraContent = toolCallDelta.extra_content;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
const argumentsDelta = (_c2 = toolCallDelta.function) == null ? void 0 : _c2.arguments;
|
|
747
|
+
if (argumentsDelta != null) {
|
|
748
|
+
pending.bufferedArguments += argumentsDelta;
|
|
749
|
+
}
|
|
750
|
+
const name = (_d = toolCallDelta.function) == null ? void 0 : _d.name;
|
|
751
|
+
if (name != null) {
|
|
752
|
+
const forwardDelta = {
|
|
753
|
+
index,
|
|
754
|
+
id: pending.id,
|
|
755
|
+
function: {
|
|
756
|
+
name,
|
|
757
|
+
arguments: pending.bufferedArguments
|
|
758
|
+
},
|
|
759
|
+
extra_content: (_e = pending.extraContent) != null ? _e : void 0
|
|
760
|
+
};
|
|
761
|
+
toolCallTracker.processDelta(forwardDelta);
|
|
762
|
+
pendingToolCalls.delete(index);
|
|
763
|
+
forwardedToolCallIndices.add(index);
|
|
764
|
+
}
|
|
765
|
+
};
|
|
646
766
|
let finishReason = {
|
|
647
767
|
unified: "other",
|
|
648
768
|
raw: void 0
|
|
649
769
|
};
|
|
650
770
|
let usage = void 0;
|
|
651
771
|
let isFirstChunk = true;
|
|
652
|
-
const providerOptionsName = this.providerOptionsName;
|
|
653
772
|
let isActiveReasoning = false;
|
|
654
773
|
let isActiveText = false;
|
|
774
|
+
const convertUsage = (usage2) => this.convertUsage(usage2);
|
|
655
775
|
return {
|
|
656
776
|
stream: response.pipeThrough(
|
|
657
777
|
new TransformStream({
|
|
658
778
|
start(controller) {
|
|
779
|
+
toolCallTracker = new StreamingToolCallTracker(
|
|
780
|
+
controller,
|
|
781
|
+
{
|
|
782
|
+
generateId,
|
|
783
|
+
extractMetadata: (delta) => {
|
|
784
|
+
var _a2, _b2;
|
|
785
|
+
const thoughtSignature = (_b2 = (_a2 = delta.extra_content) == null ? void 0 : _a2.google) == null ? void 0 : _b2.thought_signature;
|
|
786
|
+
return thoughtSignature ? { [providerOptionsName]: { thoughtSignature } } : void 0;
|
|
787
|
+
},
|
|
788
|
+
buildToolCallProviderMetadata: (metadata) => metadata
|
|
789
|
+
}
|
|
790
|
+
);
|
|
659
791
|
controller.enqueue({ type: "stream-start", warnings });
|
|
660
792
|
},
|
|
661
793
|
transform(chunk, controller) {
|
|
662
|
-
var _a2,
|
|
794
|
+
var _a2, _b2;
|
|
663
795
|
if (options.includeRawChunks) {
|
|
664
796
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
665
797
|
}
|
|
@@ -699,7 +831,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
699
831
|
return;
|
|
700
832
|
}
|
|
701
833
|
const delta = choice.delta;
|
|
702
|
-
const reasoningContent = (
|
|
834
|
+
const reasoningContent = (_b2 = delta.reasoning_content) != null ? _b2 : delta.reasoning;
|
|
703
835
|
if (reasoningContent) {
|
|
704
836
|
if (!isActiveReasoning) {
|
|
705
837
|
controller.enqueue({
|
|
@@ -741,145 +873,41 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
741
873
|
isActiveReasoning = false;
|
|
742
874
|
}
|
|
743
875
|
for (const toolCallDelta of delta.tool_calls) {
|
|
744
|
-
|
|
745
|
-
if (toolCalls[index] == null) {
|
|
746
|
-
if (toolCallDelta.id == null) {
|
|
747
|
-
throw new import_provider3.InvalidResponseDataError({
|
|
748
|
-
data: toolCallDelta,
|
|
749
|
-
message: `Expected 'id' to be a string.`
|
|
750
|
-
});
|
|
751
|
-
}
|
|
752
|
-
if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) {
|
|
753
|
-
throw new import_provider3.InvalidResponseDataError({
|
|
754
|
-
data: toolCallDelta,
|
|
755
|
-
message: `Expected 'function.name' to be a string.`
|
|
756
|
-
});
|
|
757
|
-
}
|
|
758
|
-
controller.enqueue({
|
|
759
|
-
type: "tool-input-start",
|
|
760
|
-
id: toolCallDelta.id,
|
|
761
|
-
toolName: toolCallDelta.function.name
|
|
762
|
-
});
|
|
763
|
-
toolCalls[index] = {
|
|
764
|
-
id: toolCallDelta.id,
|
|
765
|
-
type: "function",
|
|
766
|
-
function: {
|
|
767
|
-
name: toolCallDelta.function.name,
|
|
768
|
-
arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
|
|
769
|
-
},
|
|
770
|
-
hasFinished: false,
|
|
771
|
-
thoughtSignature: (_h = (_g = (_f = toolCallDelta.extra_content) == null ? void 0 : _f.google) == null ? void 0 : _g.thought_signature) != null ? _h : void 0
|
|
772
|
-
};
|
|
773
|
-
const toolCall2 = toolCalls[index];
|
|
774
|
-
if (((_i = toolCall2.function) == null ? void 0 : _i.name) != null && ((_j = toolCall2.function) == null ? void 0 : _j.arguments) != null) {
|
|
775
|
-
if (toolCall2.function.arguments.length > 0) {
|
|
776
|
-
controller.enqueue({
|
|
777
|
-
type: "tool-input-delta",
|
|
778
|
-
id: toolCall2.id,
|
|
779
|
-
delta: toolCall2.function.arguments
|
|
780
|
-
});
|
|
781
|
-
}
|
|
782
|
-
if ((0, import_provider_utils2.isParsableJson)(toolCall2.function.arguments)) {
|
|
783
|
-
controller.enqueue({
|
|
784
|
-
type: "tool-input-end",
|
|
785
|
-
id: toolCall2.id
|
|
786
|
-
});
|
|
787
|
-
controller.enqueue({
|
|
788
|
-
type: "tool-call",
|
|
789
|
-
toolCallId: (_k = toolCall2.id) != null ? _k : (0, import_provider_utils2.generateId)(),
|
|
790
|
-
toolName: toolCall2.function.name,
|
|
791
|
-
input: toolCall2.function.arguments,
|
|
792
|
-
...toolCall2.thoughtSignature ? {
|
|
793
|
-
providerMetadata: {
|
|
794
|
-
[providerOptionsName]: {
|
|
795
|
-
thoughtSignature: toolCall2.thoughtSignature
|
|
796
|
-
}
|
|
797
|
-
}
|
|
798
|
-
} : {}
|
|
799
|
-
});
|
|
800
|
-
toolCall2.hasFinished = true;
|
|
801
|
-
}
|
|
802
|
-
}
|
|
803
|
-
continue;
|
|
804
|
-
}
|
|
805
|
-
const toolCall = toolCalls[index];
|
|
806
|
-
if (toolCall.hasFinished) {
|
|
807
|
-
continue;
|
|
808
|
-
}
|
|
809
|
-
if (((_l = toolCallDelta.function) == null ? void 0 : _l.arguments) != null) {
|
|
810
|
-
toolCall.function.arguments += (_n = (_m = toolCallDelta.function) == null ? void 0 : _m.arguments) != null ? _n : "";
|
|
811
|
-
}
|
|
812
|
-
controller.enqueue({
|
|
813
|
-
type: "tool-input-delta",
|
|
814
|
-
id: toolCall.id,
|
|
815
|
-
delta: (_o = toolCallDelta.function.arguments) != null ? _o : ""
|
|
816
|
-
});
|
|
817
|
-
if (((_p = toolCall.function) == null ? void 0 : _p.name) != null && ((_q = toolCall.function) == null ? void 0 : _q.arguments) != null && (0, import_provider_utils2.isParsableJson)(toolCall.function.arguments)) {
|
|
818
|
-
controller.enqueue({
|
|
819
|
-
type: "tool-input-end",
|
|
820
|
-
id: toolCall.id
|
|
821
|
-
});
|
|
822
|
-
controller.enqueue({
|
|
823
|
-
type: "tool-call",
|
|
824
|
-
toolCallId: (_r = toolCall.id) != null ? _r : (0, import_provider_utils2.generateId)(),
|
|
825
|
-
toolName: toolCall.function.name,
|
|
826
|
-
input: toolCall.function.arguments,
|
|
827
|
-
...toolCall.thoughtSignature ? {
|
|
828
|
-
providerMetadata: {
|
|
829
|
-
[providerOptionsName]: {
|
|
830
|
-
thoughtSignature: toolCall.thoughtSignature
|
|
831
|
-
}
|
|
832
|
-
}
|
|
833
|
-
} : {}
|
|
834
|
-
});
|
|
835
|
-
toolCall.hasFinished = true;
|
|
836
|
-
}
|
|
876
|
+
processToolCallDelta(toolCallDelta);
|
|
837
877
|
}
|
|
838
878
|
}
|
|
839
879
|
},
|
|
840
880
|
flush(controller) {
|
|
841
|
-
var _a2,
|
|
881
|
+
var _a2, _b2, _c2, _d;
|
|
842
882
|
if (isActiveReasoning) {
|
|
843
883
|
controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
|
|
844
884
|
}
|
|
845
885
|
if (isActiveText) {
|
|
846
886
|
controller.enqueue({ type: "text-end", id: "txt-0" });
|
|
847
887
|
}
|
|
848
|
-
for (const
|
|
849
|
-
(
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
id: toolCall.id
|
|
854
|
-
});
|
|
855
|
-
controller.enqueue({
|
|
856
|
-
type: "tool-call",
|
|
857
|
-
toolCallId: (_a2 = toolCall.id) != null ? _a2 : (0, import_provider_utils2.generateId)(),
|
|
858
|
-
toolName: toolCall.function.name,
|
|
859
|
-
input: toolCall.function.arguments,
|
|
860
|
-
...toolCall.thoughtSignature ? {
|
|
861
|
-
providerMetadata: {
|
|
862
|
-
[providerOptionsName]: {
|
|
863
|
-
thoughtSignature: toolCall.thoughtSignature
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
} : {}
|
|
888
|
+
for (const [index, pending] of pendingToolCalls) {
|
|
889
|
+
toolCallTracker.processDelta({
|
|
890
|
+
index,
|
|
891
|
+
id: pending.id,
|
|
892
|
+
function: { arguments: pending.bufferedArguments }
|
|
867
893
|
});
|
|
868
894
|
}
|
|
895
|
+
pendingToolCalls.clear();
|
|
896
|
+
toolCallTracker.flush();
|
|
869
897
|
const providerMetadata = {
|
|
870
898
|
[providerOptionsName]: {},
|
|
871
899
|
...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
|
|
872
900
|
};
|
|
873
|
-
if (((
|
|
874
|
-
providerMetadata[providerOptionsName].acceptedPredictionTokens = (
|
|
901
|
+
if (((_a2 = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _a2.accepted_prediction_tokens) != null) {
|
|
902
|
+
providerMetadata[providerOptionsName].acceptedPredictionTokens = (_b2 = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _b2.accepted_prediction_tokens;
|
|
875
903
|
}
|
|
876
|
-
if (((
|
|
877
|
-
providerMetadata[providerOptionsName].rejectedPredictionTokens = (
|
|
904
|
+
if (((_c2 = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _c2.rejected_prediction_tokens) != null) {
|
|
905
|
+
providerMetadata[providerOptionsName].rejectedPredictionTokens = (_d = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _d.rejected_prediction_tokens;
|
|
878
906
|
}
|
|
879
907
|
controller.enqueue({
|
|
880
908
|
type: "finish",
|
|
881
909
|
finishReason,
|
|
882
|
-
usage:
|
|
910
|
+
usage: convertUsage(usage),
|
|
883
911
|
providerMetadata
|
|
884
912
|
});
|
|
885
913
|
}
|
|
@@ -890,92 +918,102 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
890
918
|
};
|
|
891
919
|
}
|
|
892
920
|
};
|
|
893
|
-
var openaiCompatibleTokenUsageSchema =
|
|
894
|
-
prompt_tokens:
|
|
895
|
-
completion_tokens:
|
|
896
|
-
total_tokens:
|
|
897
|
-
prompt_tokens_details:
|
|
898
|
-
cached_tokens:
|
|
921
|
+
var openaiCompatibleTokenUsageSchema = z3.looseObject({
|
|
922
|
+
prompt_tokens: z3.number().nullish(),
|
|
923
|
+
completion_tokens: z3.number().nullish(),
|
|
924
|
+
total_tokens: z3.number().nullish(),
|
|
925
|
+
prompt_tokens_details: z3.object({
|
|
926
|
+
cached_tokens: z3.number().nullish()
|
|
899
927
|
}).nullish(),
|
|
900
|
-
completion_tokens_details:
|
|
901
|
-
reasoning_tokens:
|
|
902
|
-
accepted_prediction_tokens:
|
|
903
|
-
rejected_prediction_tokens:
|
|
928
|
+
completion_tokens_details: z3.object({
|
|
929
|
+
reasoning_tokens: z3.number().nullish(),
|
|
930
|
+
accepted_prediction_tokens: z3.number().nullish(),
|
|
931
|
+
rejected_prediction_tokens: z3.number().nullish()
|
|
904
932
|
}).nullish()
|
|
905
933
|
}).nullish();
|
|
906
|
-
var OpenAICompatibleChatResponseSchema =
|
|
907
|
-
id:
|
|
908
|
-
created:
|
|
909
|
-
model:
|
|
910
|
-
choices:
|
|
911
|
-
|
|
912
|
-
message:
|
|
913
|
-
role:
|
|
914
|
-
content:
|
|
915
|
-
reasoning_content:
|
|
916
|
-
reasoning:
|
|
917
|
-
tool_calls:
|
|
918
|
-
|
|
919
|
-
id:
|
|
920
|
-
function:
|
|
921
|
-
name:
|
|
922
|
-
arguments:
|
|
934
|
+
var OpenAICompatibleChatResponseSchema = z3.looseObject({
|
|
935
|
+
id: z3.string().nullish(),
|
|
936
|
+
created: z3.number().nullish(),
|
|
937
|
+
model: z3.string().nullish(),
|
|
938
|
+
choices: z3.array(
|
|
939
|
+
z3.object({
|
|
940
|
+
message: z3.object({
|
|
941
|
+
role: z3.literal("assistant").nullish(),
|
|
942
|
+
content: z3.string().nullish(),
|
|
943
|
+
reasoning_content: z3.string().nullish(),
|
|
944
|
+
reasoning: z3.string().nullish(),
|
|
945
|
+
tool_calls: z3.array(
|
|
946
|
+
z3.object({
|
|
947
|
+
id: z3.string().nullish(),
|
|
948
|
+
function: z3.object({
|
|
949
|
+
name: z3.string(),
|
|
950
|
+
arguments: z3.string()
|
|
923
951
|
}),
|
|
924
952
|
// Support for Google Gemini thought signatures via OpenAI compatibility
|
|
925
|
-
extra_content:
|
|
926
|
-
google:
|
|
927
|
-
thought_signature:
|
|
953
|
+
extra_content: z3.object({
|
|
954
|
+
google: z3.object({
|
|
955
|
+
thought_signature: z3.string().nullish()
|
|
928
956
|
}).nullish()
|
|
929
957
|
}).nullish()
|
|
930
958
|
})
|
|
931
959
|
).nullish()
|
|
932
960
|
}),
|
|
933
|
-
finish_reason:
|
|
961
|
+
finish_reason: z3.string().nullish()
|
|
934
962
|
})
|
|
935
963
|
),
|
|
936
964
|
usage: openaiCompatibleTokenUsageSchema
|
|
937
965
|
});
|
|
938
|
-
var chunkBaseSchema =
|
|
939
|
-
id:
|
|
940
|
-
created:
|
|
941
|
-
model:
|
|
942
|
-
choices:
|
|
943
|
-
|
|
944
|
-
delta:
|
|
945
|
-
role:
|
|
946
|
-
content:
|
|
966
|
+
var chunkBaseSchema = z3.looseObject({
|
|
967
|
+
id: z3.string().nullish(),
|
|
968
|
+
created: z3.number().nullish(),
|
|
969
|
+
model: z3.string().nullish(),
|
|
970
|
+
choices: z3.array(
|
|
971
|
+
z3.object({
|
|
972
|
+
delta: z3.object({
|
|
973
|
+
role: z3.enum(["assistant", ""]).nullish(),
|
|
974
|
+
content: z3.string().nullish(),
|
|
947
975
|
// Most openai-compatible models set `reasoning_content`, but some
|
|
948
976
|
// providers serving `gpt-oss` set `reasoning`. See #7866
|
|
949
|
-
reasoning_content:
|
|
950
|
-
reasoning:
|
|
951
|
-
tool_calls:
|
|
952
|
-
|
|
953
|
-
index:
|
|
977
|
+
reasoning_content: z3.string().nullish(),
|
|
978
|
+
reasoning: z3.string().nullish(),
|
|
979
|
+
tool_calls: z3.array(
|
|
980
|
+
z3.object({
|
|
981
|
+
index: z3.number().nullish(),
|
|
954
982
|
//google does not send index
|
|
955
|
-
id:
|
|
956
|
-
function:
|
|
957
|
-
name:
|
|
958
|
-
arguments:
|
|
983
|
+
id: z3.string().nullish(),
|
|
984
|
+
function: z3.object({
|
|
985
|
+
name: z3.string().nullish(),
|
|
986
|
+
arguments: z3.string().nullish()
|
|
959
987
|
}),
|
|
960
988
|
// Support for Google Gemini thought signatures via OpenAI compatibility
|
|
961
|
-
extra_content:
|
|
962
|
-
google:
|
|
963
|
-
thought_signature:
|
|
989
|
+
extra_content: z3.object({
|
|
990
|
+
google: z3.object({
|
|
991
|
+
thought_signature: z3.string().nullish()
|
|
964
992
|
}).nullish()
|
|
965
993
|
}).nullish()
|
|
966
994
|
})
|
|
967
995
|
).nullish()
|
|
968
996
|
}).nullish(),
|
|
969
|
-
finish_reason:
|
|
997
|
+
finish_reason: z3.string().nullish()
|
|
970
998
|
})
|
|
971
999
|
),
|
|
972
1000
|
usage: openaiCompatibleTokenUsageSchema
|
|
973
1001
|
});
|
|
974
|
-
var createOpenAICompatibleChatChunkSchema = (errorSchema) =>
|
|
1002
|
+
var createOpenAICompatibleChatChunkSchema = (errorSchema) => z3.union([chunkBaseSchema, errorSchema]);
|
|
975
1003
|
|
|
976
1004
|
// src/completion/openai-compatible-completion-language-model.ts
|
|
977
|
-
|
|
978
|
-
|
|
1005
|
+
import {
|
|
1006
|
+
combineHeaders as combineHeaders2,
|
|
1007
|
+
createEventSourceResponseHandler as createEventSourceResponseHandler2,
|
|
1008
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler2,
|
|
1009
|
+
createJsonResponseHandler as createJsonResponseHandler2,
|
|
1010
|
+
parseProviderOptions as parseProviderOptions2,
|
|
1011
|
+
postJsonToApi as postJsonToApi2,
|
|
1012
|
+
serializeModelOptions as serializeModelOptions2,
|
|
1013
|
+
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE2,
|
|
1014
|
+
WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE2
|
|
1015
|
+
} from "@ai-sdk/provider-utils";
|
|
1016
|
+
import { z as z5 } from "zod/v4";
|
|
979
1017
|
|
|
980
1018
|
// src/completion/convert-openai-compatible-completion-usage.ts
|
|
981
1019
|
function convertOpenAICompatibleCompletionUsage(usage) {
|
|
@@ -1015,7 +1053,10 @@ function convertOpenAICompatibleCompletionUsage(usage) {
|
|
|
1015
1053
|
}
|
|
1016
1054
|
|
|
1017
1055
|
// src/completion/convert-to-openai-compatible-completion-prompt.ts
|
|
1018
|
-
|
|
1056
|
+
import {
|
|
1057
|
+
InvalidPromptError,
|
|
1058
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
1059
|
+
} from "@ai-sdk/provider";
|
|
1019
1060
|
function convertToOpenAICompatibleCompletionPrompt({
|
|
1020
1061
|
prompt,
|
|
1021
1062
|
user = "user",
|
|
@@ -1031,7 +1072,7 @@ function convertToOpenAICompatibleCompletionPrompt({
|
|
|
1031
1072
|
for (const { role, content } of prompt) {
|
|
1032
1073
|
switch (role) {
|
|
1033
1074
|
case "system": {
|
|
1034
|
-
throw new
|
|
1075
|
+
throw new InvalidPromptError({
|
|
1035
1076
|
message: "Unexpected system message in prompt: ${content}",
|
|
1036
1077
|
prompt
|
|
1037
1078
|
});
|
|
@@ -1057,7 +1098,7 @@ ${userMessage}
|
|
|
1057
1098
|
return part.text;
|
|
1058
1099
|
}
|
|
1059
1100
|
case "tool-call": {
|
|
1060
|
-
throw new
|
|
1101
|
+
throw new UnsupportedFunctionalityError3({
|
|
1061
1102
|
functionality: "tool-call messages"
|
|
1062
1103
|
});
|
|
1063
1104
|
}
|
|
@@ -1070,7 +1111,7 @@ ${assistantMessage}
|
|
|
1070
1111
|
break;
|
|
1071
1112
|
}
|
|
1072
1113
|
case "tool": {
|
|
1073
|
-
throw new
|
|
1114
|
+
throw new UnsupportedFunctionalityError3({
|
|
1074
1115
|
functionality: "tool messages"
|
|
1075
1116
|
});
|
|
1076
1117
|
}
|
|
@@ -1119,34 +1160,33 @@ function mapOpenAICompatibleFinishReason2(finishReason) {
|
|
|
1119
1160
|
}
|
|
1120
1161
|
}
|
|
1121
1162
|
|
|
1122
|
-
// src/completion/openai-compatible-completion-options.ts
|
|
1123
|
-
|
|
1124
|
-
var openaiCompatibleLanguageModelCompletionOptions =
|
|
1163
|
+
// src/completion/openai-compatible-completion-language-model-options.ts
|
|
1164
|
+
import { z as z4 } from "zod/v4";
|
|
1165
|
+
var openaiCompatibleLanguageModelCompletionOptions = z4.object({
|
|
1125
1166
|
/**
|
|
1126
1167
|
* Echo back the prompt in addition to the completion.
|
|
1127
1168
|
*/
|
|
1128
|
-
echo:
|
|
1169
|
+
echo: z4.boolean().optional(),
|
|
1129
1170
|
/**
|
|
1130
1171
|
* Modify the likelihood of specified tokens appearing in the completion.
|
|
1131
1172
|
*
|
|
1132
1173
|
* Accepts a JSON object that maps tokens (specified by their token ID in
|
|
1133
1174
|
* the GPT tokenizer) to an associated bias value from -100 to 100.
|
|
1134
1175
|
*/
|
|
1135
|
-
logitBias:
|
|
1176
|
+
logitBias: z4.record(z4.string(), z4.number()).optional(),
|
|
1136
1177
|
/**
|
|
1137
1178
|
* The suffix that comes after a completion of inserted text.
|
|
1138
1179
|
*/
|
|
1139
|
-
suffix:
|
|
1180
|
+
suffix: z4.string().optional(),
|
|
1140
1181
|
/**
|
|
1141
1182
|
* A unique identifier representing your end-user, which can help providers to
|
|
1142
1183
|
* monitor and detect abuse.
|
|
1143
1184
|
*/
|
|
1144
|
-
user:
|
|
1185
|
+
user: z4.string().optional()
|
|
1145
1186
|
});
|
|
1146
1187
|
|
|
1147
1188
|
// src/completion/openai-compatible-completion-language-model.ts
|
|
1148
|
-
var OpenAICompatibleCompletionLanguageModel = class {
|
|
1149
|
-
// type inferred via constructor
|
|
1189
|
+
var OpenAICompatibleCompletionLanguageModel = class _OpenAICompatibleCompletionLanguageModel {
|
|
1150
1190
|
constructor(modelId, config) {
|
|
1151
1191
|
this.specificationVersion = "v4";
|
|
1152
1192
|
var _a;
|
|
@@ -1156,7 +1196,20 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1156
1196
|
this.chunkSchema = createOpenAICompatibleCompletionChunkSchema(
|
|
1157
1197
|
errorStructure.errorSchema
|
|
1158
1198
|
);
|
|
1159
|
-
this.failedResponseHandler = (
|
|
1199
|
+
this.failedResponseHandler = createJsonErrorResponseHandler2(errorStructure);
|
|
1200
|
+
}
|
|
1201
|
+
// type inferred via constructor
|
|
1202
|
+
static [WORKFLOW_SERIALIZE2](model) {
|
|
1203
|
+
return serializeModelOptions2({
|
|
1204
|
+
modelId: model.modelId,
|
|
1205
|
+
config: model.config
|
|
1206
|
+
});
|
|
1207
|
+
}
|
|
1208
|
+
static [WORKFLOW_DESERIALIZE2](options) {
|
|
1209
|
+
return new _OpenAICompatibleCompletionLanguageModel(
|
|
1210
|
+
options.modelId,
|
|
1211
|
+
options.config
|
|
1212
|
+
);
|
|
1160
1213
|
}
|
|
1161
1214
|
get provider() {
|
|
1162
1215
|
return this.config.provider;
|
|
@@ -1183,13 +1236,25 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1183
1236
|
tools,
|
|
1184
1237
|
toolChoice
|
|
1185
1238
|
}) {
|
|
1186
|
-
var _a;
|
|
1239
|
+
var _a, _b;
|
|
1187
1240
|
const warnings = [];
|
|
1188
|
-
|
|
1189
|
-
|
|
1241
|
+
warnIfDeprecatedProviderOptionsKey({
|
|
1242
|
+
rawName: this.providerOptionsName,
|
|
1190
1243
|
providerOptions,
|
|
1191
|
-
|
|
1192
|
-
})
|
|
1244
|
+
warnings
|
|
1245
|
+
});
|
|
1246
|
+
const completionOptions = Object.assign(
|
|
1247
|
+
(_a = await parseProviderOptions2({
|
|
1248
|
+
provider: this.providerOptionsName,
|
|
1249
|
+
providerOptions,
|
|
1250
|
+
schema: openaiCompatibleLanguageModelCompletionOptions
|
|
1251
|
+
})) != null ? _a : {},
|
|
1252
|
+
(_b = await parseProviderOptions2({
|
|
1253
|
+
provider: toCamelCase(this.providerOptionsName),
|
|
1254
|
+
providerOptions,
|
|
1255
|
+
schema: openaiCompatibleLanguageModelCompletionOptions
|
|
1256
|
+
})) != null ? _b : {}
|
|
1257
|
+
);
|
|
1193
1258
|
if (topK != null) {
|
|
1194
1259
|
warnings.push({ type: "unsupported", feature: "topK" });
|
|
1195
1260
|
}
|
|
@@ -1225,6 +1290,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1225
1290
|
presence_penalty: presencePenalty,
|
|
1226
1291
|
seed,
|
|
1227
1292
|
...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
|
|
1293
|
+
...providerOptions == null ? void 0 : providerOptions[toCamelCase(this.providerOptionsName)],
|
|
1228
1294
|
// prompt:
|
|
1229
1295
|
prompt: completionPrompt,
|
|
1230
1296
|
// stop sequences:
|
|
@@ -1234,20 +1300,21 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1234
1300
|
};
|
|
1235
1301
|
}
|
|
1236
1302
|
async doGenerate(options) {
|
|
1303
|
+
var _a, _b;
|
|
1237
1304
|
const { args, warnings } = await this.getArgs(options);
|
|
1238
1305
|
const {
|
|
1239
1306
|
responseHeaders,
|
|
1240
1307
|
value: response,
|
|
1241
1308
|
rawValue: rawResponse
|
|
1242
|
-
} = await (
|
|
1309
|
+
} = await postJsonToApi2({
|
|
1243
1310
|
url: this.config.url({
|
|
1244
1311
|
path: "/completions",
|
|
1245
1312
|
modelId: this.modelId
|
|
1246
1313
|
}),
|
|
1247
|
-
headers: (
|
|
1314
|
+
headers: combineHeaders2((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
1248
1315
|
body: args,
|
|
1249
1316
|
failedResponseHandler: this.failedResponseHandler,
|
|
1250
|
-
successfulResponseHandler: (
|
|
1317
|
+
successfulResponseHandler: createJsonResponseHandler2(
|
|
1251
1318
|
openaiCompatibleCompletionResponseSchema
|
|
1252
1319
|
),
|
|
1253
1320
|
abortSignal: options.abortSignal,
|
|
@@ -1275,6 +1342,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1275
1342
|
};
|
|
1276
1343
|
}
|
|
1277
1344
|
async doStream(options) {
|
|
1345
|
+
var _a, _b;
|
|
1278
1346
|
const { args, warnings } = await this.getArgs(options);
|
|
1279
1347
|
const body = {
|
|
1280
1348
|
...args,
|
|
@@ -1282,15 +1350,15 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1282
1350
|
// only include stream_options when in strict compatibility mode:
|
|
1283
1351
|
stream_options: this.config.includeUsage ? { include_usage: true } : void 0
|
|
1284
1352
|
};
|
|
1285
|
-
const { responseHeaders, value: response } = await (
|
|
1353
|
+
const { responseHeaders, value: response } = await postJsonToApi2({
|
|
1286
1354
|
url: this.config.url({
|
|
1287
1355
|
path: "/completions",
|
|
1288
1356
|
modelId: this.modelId
|
|
1289
1357
|
}),
|
|
1290
|
-
headers: (
|
|
1358
|
+
headers: combineHeaders2((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
1291
1359
|
body,
|
|
1292
1360
|
failedResponseHandler: this.failedResponseHandler,
|
|
1293
|
-
successfulResponseHandler: (
|
|
1361
|
+
successfulResponseHandler: createEventSourceResponseHandler2(
|
|
1294
1362
|
this.chunkSchema
|
|
1295
1363
|
),
|
|
1296
1364
|
abortSignal: options.abortSignal,
|
|
@@ -1309,7 +1377,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1309
1377
|
controller.enqueue({ type: "stream-start", warnings });
|
|
1310
1378
|
},
|
|
1311
1379
|
transform(chunk, controller) {
|
|
1312
|
-
var
|
|
1380
|
+
var _a2;
|
|
1313
1381
|
if (options.includeRawChunks) {
|
|
1314
1382
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
1315
1383
|
}
|
|
@@ -1342,7 +1410,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1342
1410
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
1343
1411
|
finishReason = {
|
|
1344
1412
|
unified: mapOpenAICompatibleFinishReason2(choice.finish_reason),
|
|
1345
|
-
raw: (
|
|
1413
|
+
raw: (_a2 = choice.finish_reason) != null ? _a2 : void 0
|
|
1346
1414
|
};
|
|
1347
1415
|
}
|
|
1348
1416
|
if ((choice == null ? void 0 : choice.text) != null) {
|
|
@@ -1370,33 +1438,33 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1370
1438
|
};
|
|
1371
1439
|
}
|
|
1372
1440
|
};
|
|
1373
|
-
var usageSchema =
|
|
1374
|
-
prompt_tokens:
|
|
1375
|
-
completion_tokens:
|
|
1376
|
-
total_tokens:
|
|
1441
|
+
var usageSchema = z5.object({
|
|
1442
|
+
prompt_tokens: z5.number(),
|
|
1443
|
+
completion_tokens: z5.number(),
|
|
1444
|
+
total_tokens: z5.number()
|
|
1377
1445
|
});
|
|
1378
|
-
var openaiCompatibleCompletionResponseSchema =
|
|
1379
|
-
id:
|
|
1380
|
-
created:
|
|
1381
|
-
model:
|
|
1382
|
-
choices:
|
|
1383
|
-
|
|
1384
|
-
text:
|
|
1385
|
-
finish_reason:
|
|
1446
|
+
var openaiCompatibleCompletionResponseSchema = z5.object({
|
|
1447
|
+
id: z5.string().nullish(),
|
|
1448
|
+
created: z5.number().nullish(),
|
|
1449
|
+
model: z5.string().nullish(),
|
|
1450
|
+
choices: z5.array(
|
|
1451
|
+
z5.object({
|
|
1452
|
+
text: z5.string(),
|
|
1453
|
+
finish_reason: z5.string()
|
|
1386
1454
|
})
|
|
1387
1455
|
),
|
|
1388
1456
|
usage: usageSchema.nullish()
|
|
1389
1457
|
});
|
|
1390
|
-
var createOpenAICompatibleCompletionChunkSchema = (errorSchema) =>
|
|
1391
|
-
|
|
1392
|
-
id:
|
|
1393
|
-
created:
|
|
1394
|
-
model:
|
|
1395
|
-
choices:
|
|
1396
|
-
|
|
1397
|
-
text:
|
|
1398
|
-
finish_reason:
|
|
1399
|
-
index:
|
|
1458
|
+
var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => z5.union([
|
|
1459
|
+
z5.object({
|
|
1460
|
+
id: z5.string().nullish(),
|
|
1461
|
+
created: z5.number().nullish(),
|
|
1462
|
+
model: z5.string().nullish(),
|
|
1463
|
+
choices: z5.array(
|
|
1464
|
+
z5.object({
|
|
1465
|
+
text: z5.string(),
|
|
1466
|
+
finish_reason: z5.string().nullish(),
|
|
1467
|
+
index: z5.number()
|
|
1400
1468
|
})
|
|
1401
1469
|
),
|
|
1402
1470
|
usage: usageSchema.nullish()
|
|
@@ -1405,27 +1473,38 @@ var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => import_v45.z.
|
|
|
1405
1473
|
]);
|
|
1406
1474
|
|
|
1407
1475
|
// src/embedding/openai-compatible-embedding-model.ts
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1476
|
+
import {
|
|
1477
|
+
TooManyEmbeddingValuesForCallError
|
|
1478
|
+
} from "@ai-sdk/provider";
|
|
1479
|
+
import {
|
|
1480
|
+
combineHeaders as combineHeaders3,
|
|
1481
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler3,
|
|
1482
|
+
createJsonResponseHandler as createJsonResponseHandler3,
|
|
1483
|
+
parseProviderOptions as parseProviderOptions3,
|
|
1484
|
+
postJsonToApi as postJsonToApi3,
|
|
1485
|
+
serializeModelOptions as serializeModelOptions3,
|
|
1486
|
+
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE3,
|
|
1487
|
+
WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE3
|
|
1488
|
+
} from "@ai-sdk/provider-utils";
|
|
1489
|
+
import { z as z7 } from "zod/v4";
|
|
1411
1490
|
|
|
1412
|
-
// src/embedding/openai-compatible-embedding-options.ts
|
|
1413
|
-
|
|
1414
|
-
var openaiCompatibleEmbeddingModelOptions =
|
|
1491
|
+
// src/embedding/openai-compatible-embedding-model-options.ts
|
|
1492
|
+
import { z as z6 } from "zod/v4";
|
|
1493
|
+
var openaiCompatibleEmbeddingModelOptions = z6.object({
|
|
1415
1494
|
/**
|
|
1416
1495
|
* The number of dimensions the resulting output embeddings should have.
|
|
1417
1496
|
* Only supported in text-embedding-3 and later models.
|
|
1418
1497
|
*/
|
|
1419
|
-
dimensions:
|
|
1498
|
+
dimensions: z6.number().optional(),
|
|
1420
1499
|
/**
|
|
1421
1500
|
* A unique identifier representing your end-user, which can help providers to
|
|
1422
1501
|
* monitor and detect abuse.
|
|
1423
1502
|
*/
|
|
1424
|
-
user:
|
|
1503
|
+
user: z6.string().optional()
|
|
1425
1504
|
});
|
|
1426
1505
|
|
|
1427
1506
|
// src/embedding/openai-compatible-embedding-model.ts
|
|
1428
|
-
var OpenAICompatibleEmbeddingModel = class {
|
|
1507
|
+
var OpenAICompatibleEmbeddingModel = class _OpenAICompatibleEmbeddingModel {
|
|
1429
1508
|
constructor(modelId, config) {
|
|
1430
1509
|
this.specificationVersion = "v4";
|
|
1431
1510
|
this.modelId = modelId;
|
|
@@ -1442,6 +1521,15 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
1442
1521
|
var _a;
|
|
1443
1522
|
return (_a = this.config.supportsParallelCalls) != null ? _a : true;
|
|
1444
1523
|
}
|
|
1524
|
+
static [WORKFLOW_SERIALIZE3](model) {
|
|
1525
|
+
return serializeModelOptions3({
|
|
1526
|
+
modelId: model.modelId,
|
|
1527
|
+
config: model.config
|
|
1528
|
+
});
|
|
1529
|
+
}
|
|
1530
|
+
static [WORKFLOW_DESERIALIZE3](options) {
|
|
1531
|
+
return new _OpenAICompatibleEmbeddingModel(options.modelId, options.config);
|
|
1532
|
+
}
|
|
1445
1533
|
get providerOptionsName() {
|
|
1446
1534
|
return this.config.provider.split(".")[0].trim();
|
|
1447
1535
|
}
|
|
@@ -1451,34 +1539,40 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
1451
1539
|
abortSignal,
|
|
1452
1540
|
providerOptions
|
|
1453
1541
|
}) {
|
|
1454
|
-
var _a, _b, _c;
|
|
1542
|
+
var _a, _b, _c, _d, _e;
|
|
1455
1543
|
const warnings = [];
|
|
1456
|
-
const deprecatedOptions = await (
|
|
1544
|
+
const deprecatedOptions = await parseProviderOptions3({
|
|
1457
1545
|
provider: "openai-compatible",
|
|
1458
1546
|
providerOptions,
|
|
1459
1547
|
schema: openaiCompatibleEmbeddingModelOptions
|
|
1460
1548
|
});
|
|
1461
1549
|
if (deprecatedOptions != null) {
|
|
1462
1550
|
warnings.push({
|
|
1463
|
-
type: "
|
|
1464
|
-
|
|
1551
|
+
type: "deprecated",
|
|
1552
|
+
setting: "providerOptions key 'openai-compatible'",
|
|
1553
|
+
message: "Use 'openaiCompatible' instead."
|
|
1465
1554
|
});
|
|
1466
1555
|
}
|
|
1556
|
+
warnIfDeprecatedProviderOptionsKey({
|
|
1557
|
+
rawName: this.providerOptionsName,
|
|
1558
|
+
providerOptions,
|
|
1559
|
+
warnings
|
|
1560
|
+
});
|
|
1467
1561
|
const compatibleOptions = Object.assign(
|
|
1468
1562
|
deprecatedOptions != null ? deprecatedOptions : {},
|
|
1469
|
-
(_a = await (
|
|
1563
|
+
(_a = await parseProviderOptions3({
|
|
1470
1564
|
provider: "openaiCompatible",
|
|
1471
1565
|
providerOptions,
|
|
1472
1566
|
schema: openaiCompatibleEmbeddingModelOptions
|
|
1473
1567
|
})) != null ? _a : {},
|
|
1474
|
-
(_b = await (
|
|
1568
|
+
(_b = await parseProviderOptions3({
|
|
1475
1569
|
provider: this.providerOptionsName,
|
|
1476
1570
|
providerOptions,
|
|
1477
1571
|
schema: openaiCompatibleEmbeddingModelOptions
|
|
1478
1572
|
})) != null ? _b : {}
|
|
1479
1573
|
);
|
|
1480
1574
|
if (values.length > this.maxEmbeddingsPerCall) {
|
|
1481
|
-
throw new
|
|
1575
|
+
throw new TooManyEmbeddingValuesForCallError({
|
|
1482
1576
|
provider: this.provider,
|
|
1483
1577
|
modelId: this.modelId,
|
|
1484
1578
|
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
@@ -1489,12 +1583,12 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
1489
1583
|
responseHeaders,
|
|
1490
1584
|
value: response,
|
|
1491
1585
|
rawValue
|
|
1492
|
-
} = await (
|
|
1586
|
+
} = await postJsonToApi3({
|
|
1493
1587
|
url: this.config.url({
|
|
1494
1588
|
path: "/embeddings",
|
|
1495
1589
|
modelId: this.modelId
|
|
1496
1590
|
}),
|
|
1497
|
-
headers: (
|
|
1591
|
+
headers: combineHeaders3((_d = (_c = this.config).headers) == null ? void 0 : _d.call(_c), headers),
|
|
1498
1592
|
body: {
|
|
1499
1593
|
model: this.modelId,
|
|
1500
1594
|
input: values,
|
|
@@ -1502,10 +1596,10 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
1502
1596
|
dimensions: compatibleOptions.dimensions,
|
|
1503
1597
|
user: compatibleOptions.user
|
|
1504
1598
|
},
|
|
1505
|
-
failedResponseHandler: (
|
|
1506
|
-
(
|
|
1599
|
+
failedResponseHandler: createJsonErrorResponseHandler3(
|
|
1600
|
+
(_e = this.config.errorStructure) != null ? _e : defaultOpenAICompatibleErrorStructure
|
|
1507
1601
|
),
|
|
1508
|
-
successfulResponseHandler: (
|
|
1602
|
+
successfulResponseHandler: createJsonResponseHandler3(
|
|
1509
1603
|
openaiTextEmbeddingResponseSchema
|
|
1510
1604
|
),
|
|
1511
1605
|
abortSignal,
|
|
@@ -1520,16 +1614,28 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
1520
1614
|
};
|
|
1521
1615
|
}
|
|
1522
1616
|
};
|
|
1523
|
-
var openaiTextEmbeddingResponseSchema =
|
|
1524
|
-
data:
|
|
1525
|
-
usage:
|
|
1526
|
-
providerMetadata:
|
|
1617
|
+
var openaiTextEmbeddingResponseSchema = z7.object({
|
|
1618
|
+
data: z7.array(z7.object({ embedding: z7.array(z7.number()) })),
|
|
1619
|
+
usage: z7.object({ prompt_tokens: z7.number() }).nullish(),
|
|
1620
|
+
providerMetadata: z7.record(z7.string(), z7.record(z7.string(), z7.any())).optional()
|
|
1527
1621
|
});
|
|
1528
1622
|
|
|
1529
1623
|
// src/image/openai-compatible-image-model.ts
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1624
|
+
import {
|
|
1625
|
+
combineHeaders as combineHeaders4,
|
|
1626
|
+
convertBase64ToUint8Array as convertBase64ToUint8Array2,
|
|
1627
|
+
convertToFormData,
|
|
1628
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler4,
|
|
1629
|
+
createJsonResponseHandler as createJsonResponseHandler4,
|
|
1630
|
+
downloadBlob,
|
|
1631
|
+
postFormDataToApi,
|
|
1632
|
+
postJsonToApi as postJsonToApi4,
|
|
1633
|
+
serializeModelOptions as serializeModelOptions4,
|
|
1634
|
+
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE4,
|
|
1635
|
+
WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE4
|
|
1636
|
+
} from "@ai-sdk/provider-utils";
|
|
1637
|
+
import { z as z8 } from "zod/v4";
|
|
1638
|
+
var OpenAICompatibleImageModel = class _OpenAICompatibleImageModel {
|
|
1533
1639
|
constructor(modelId, config) {
|
|
1534
1640
|
this.modelId = modelId;
|
|
1535
1641
|
this.config = config;
|
|
@@ -1545,8 +1651,21 @@ var OpenAICompatibleImageModel = class {
|
|
|
1545
1651
|
get providerOptionsKey() {
|
|
1546
1652
|
return this.config.provider.split(".")[0].trim();
|
|
1547
1653
|
}
|
|
1548
|
-
|
|
1549
|
-
|
|
1654
|
+
static [WORKFLOW_SERIALIZE4](model) {
|
|
1655
|
+
return serializeModelOptions4({
|
|
1656
|
+
modelId: model.modelId,
|
|
1657
|
+
config: model.config
|
|
1658
|
+
});
|
|
1659
|
+
}
|
|
1660
|
+
static [WORKFLOW_DESERIALIZE4](options) {
|
|
1661
|
+
return new _OpenAICompatibleImageModel(options.modelId, options.config);
|
|
1662
|
+
}
|
|
1663
|
+
getArgs(providerOptions, warnings) {
|
|
1664
|
+
warnIfDeprecatedProviderOptionsKey({
|
|
1665
|
+
rawName: this.providerOptionsKey,
|
|
1666
|
+
providerOptions,
|
|
1667
|
+
warnings
|
|
1668
|
+
});
|
|
1550
1669
|
return {
|
|
1551
1670
|
...providerOptions[this.providerOptionsKey],
|
|
1552
1671
|
...providerOptions[toCamelCase(this.providerOptionsKey)]
|
|
@@ -1564,7 +1683,7 @@ var OpenAICompatibleImageModel = class {
|
|
|
1564
1683
|
files,
|
|
1565
1684
|
mask
|
|
1566
1685
|
}) {
|
|
1567
|
-
var _a, _b, _c, _d, _e;
|
|
1686
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1568
1687
|
const warnings = [];
|
|
1569
1688
|
if (aspectRatio != null) {
|
|
1570
1689
|
warnings.push({
|
|
@@ -1577,15 +1696,15 @@ var OpenAICompatibleImageModel = class {
|
|
|
1577
1696
|
warnings.push({ type: "unsupported", feature: "seed" });
|
|
1578
1697
|
}
|
|
1579
1698
|
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
1580
|
-
const args = this.getArgs(providerOptions);
|
|
1699
|
+
const args = this.getArgs(providerOptions, warnings);
|
|
1581
1700
|
if (files != null && files.length > 0) {
|
|
1582
|
-
const { value: response2, responseHeaders: responseHeaders2 } = await
|
|
1701
|
+
const { value: response2, responseHeaders: responseHeaders2 } = await postFormDataToApi({
|
|
1583
1702
|
url: this.config.url({
|
|
1584
1703
|
path: "/images/edits",
|
|
1585
1704
|
modelId: this.modelId
|
|
1586
1705
|
}),
|
|
1587
|
-
headers: (
|
|
1588
|
-
formData:
|
|
1706
|
+
headers: combineHeaders4((_e = (_d = this.config).headers) == null ? void 0 : _e.call(_d), headers),
|
|
1707
|
+
formData: convertToFormData({
|
|
1589
1708
|
model: this.modelId,
|
|
1590
1709
|
prompt,
|
|
1591
1710
|
image: await Promise.all(files.map((file) => fileToBlob(file))),
|
|
@@ -1594,10 +1713,10 @@ var OpenAICompatibleImageModel = class {
|
|
|
1594
1713
|
size,
|
|
1595
1714
|
...args
|
|
1596
1715
|
}),
|
|
1597
|
-
failedResponseHandler: (
|
|
1598
|
-
(
|
|
1716
|
+
failedResponseHandler: createJsonErrorResponseHandler4(
|
|
1717
|
+
(_f = this.config.errorStructure) != null ? _f : defaultOpenAICompatibleErrorStructure
|
|
1599
1718
|
),
|
|
1600
|
-
successfulResponseHandler: (
|
|
1719
|
+
successfulResponseHandler: createJsonResponseHandler4(
|
|
1601
1720
|
openaiCompatibleImageResponseSchema
|
|
1602
1721
|
),
|
|
1603
1722
|
abortSignal,
|
|
@@ -1613,12 +1732,12 @@ var OpenAICompatibleImageModel = class {
|
|
|
1613
1732
|
}
|
|
1614
1733
|
};
|
|
1615
1734
|
}
|
|
1616
|
-
const { value: response, responseHeaders } = await (
|
|
1735
|
+
const { value: response, responseHeaders } = await postJsonToApi4({
|
|
1617
1736
|
url: this.config.url({
|
|
1618
1737
|
path: "/images/generations",
|
|
1619
1738
|
modelId: this.modelId
|
|
1620
1739
|
}),
|
|
1621
|
-
headers: (
|
|
1740
|
+
headers: combineHeaders4((_h = (_g = this.config).headers) == null ? void 0 : _h.call(_g), headers),
|
|
1622
1741
|
body: {
|
|
1623
1742
|
model: this.modelId,
|
|
1624
1743
|
prompt,
|
|
@@ -1627,10 +1746,10 @@ var OpenAICompatibleImageModel = class {
|
|
|
1627
1746
|
...args,
|
|
1628
1747
|
response_format: "b64_json"
|
|
1629
1748
|
},
|
|
1630
|
-
failedResponseHandler: (
|
|
1631
|
-
(
|
|
1749
|
+
failedResponseHandler: createJsonErrorResponseHandler4(
|
|
1750
|
+
(_i = this.config.errorStructure) != null ? _i : defaultOpenAICompatibleErrorStructure
|
|
1632
1751
|
),
|
|
1633
|
-
successfulResponseHandler: (
|
|
1752
|
+
successfulResponseHandler: createJsonResponseHandler4(
|
|
1634
1753
|
openaiCompatibleImageResponseSchema
|
|
1635
1754
|
),
|
|
1636
1755
|
abortSignal,
|
|
@@ -1647,35 +1766,35 @@ var OpenAICompatibleImageModel = class {
|
|
|
1647
1766
|
};
|
|
1648
1767
|
}
|
|
1649
1768
|
};
|
|
1650
|
-
var openaiCompatibleImageResponseSchema =
|
|
1651
|
-
data:
|
|
1769
|
+
var openaiCompatibleImageResponseSchema = z8.object({
|
|
1770
|
+
data: z8.array(z8.object({ b64_json: z8.string() }))
|
|
1652
1771
|
});
|
|
1653
1772
|
async function fileToBlob(file) {
|
|
1654
1773
|
if (file.type === "url") {
|
|
1655
|
-
return
|
|
1774
|
+
return downloadBlob(file.url);
|
|
1656
1775
|
}
|
|
1657
|
-
const data = file.data instanceof Uint8Array ? file.data : (
|
|
1776
|
+
const data = file.data instanceof Uint8Array ? file.data : convertBase64ToUint8Array2(file.data);
|
|
1658
1777
|
return new Blob([data], { type: file.mediaType });
|
|
1659
1778
|
}
|
|
1660
|
-
function toCamelCase(str) {
|
|
1661
|
-
return str.replace(/[_-]([a-z])/g, (g) => g[1].toUpperCase());
|
|
1662
|
-
}
|
|
1663
1779
|
|
|
1664
1780
|
// src/openai-compatible-provider.ts
|
|
1665
|
-
|
|
1781
|
+
import {
|
|
1782
|
+
withoutTrailingSlash,
|
|
1783
|
+
withUserAgentSuffix
|
|
1784
|
+
} from "@ai-sdk/provider-utils";
|
|
1666
1785
|
|
|
1667
1786
|
// src/version.ts
|
|
1668
|
-
var VERSION = true ? "3.0.0-beta.
|
|
1787
|
+
var VERSION = true ? "3.0.0-beta.57" : "0.0.0-test";
|
|
1669
1788
|
|
|
1670
1789
|
// src/openai-compatible-provider.ts
|
|
1671
1790
|
function createOpenAICompatible(options) {
|
|
1672
|
-
const baseURL =
|
|
1791
|
+
const baseURL = withoutTrailingSlash(options.baseURL);
|
|
1673
1792
|
const providerName = options.name;
|
|
1674
1793
|
const headers = {
|
|
1675
1794
|
...options.apiKey && { Authorization: `Bearer ${options.apiKey}` },
|
|
1676
1795
|
...options.headers
|
|
1677
1796
|
};
|
|
1678
|
-
const getHeaders = () =>
|
|
1797
|
+
const getHeaders = () => withUserAgentSuffix(headers, `ai-sdk/openai-compatible/${VERSION}`);
|
|
1679
1798
|
const getCommonModelConfig = (modelType) => ({
|
|
1680
1799
|
provider: `${providerName}.${modelType}`,
|
|
1681
1800
|
url: ({ path }) => {
|
|
@@ -1693,8 +1812,10 @@ function createOpenAICompatible(options) {
|
|
|
1693
1812
|
...getCommonModelConfig("chat"),
|
|
1694
1813
|
includeUsage: options.includeUsage,
|
|
1695
1814
|
supportsStructuredOutputs: options.supportsStructuredOutputs,
|
|
1815
|
+
supportedUrls: options.supportedUrls,
|
|
1696
1816
|
transformRequestBody: options.transformRequestBody,
|
|
1697
|
-
metadataExtractor: options.metadataExtractor
|
|
1817
|
+
metadataExtractor: options.metadataExtractor,
|
|
1818
|
+
convertUsage: options.convertUsage
|
|
1698
1819
|
});
|
|
1699
1820
|
const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(modelId, {
|
|
1700
1821
|
...getCommonModelConfig("completion"),
|
|
@@ -1714,13 +1835,12 @@ function createOpenAICompatible(options) {
|
|
|
1714
1835
|
provider.imageModel = createImageModel;
|
|
1715
1836
|
return provider;
|
|
1716
1837
|
}
|
|
1717
|
-
|
|
1718
|
-
0 && (module.exports = {
|
|
1838
|
+
export {
|
|
1719
1839
|
OpenAICompatibleChatLanguageModel,
|
|
1720
1840
|
OpenAICompatibleCompletionLanguageModel,
|
|
1721
1841
|
OpenAICompatibleEmbeddingModel,
|
|
1722
1842
|
OpenAICompatibleImageModel,
|
|
1723
1843
|
VERSION,
|
|
1724
1844
|
createOpenAICompatible
|
|
1725
|
-
}
|
|
1845
|
+
};
|
|
1726
1846
|
//# sourceMappingURL=index.js.map
|