@ai-sdk/openai-compatible 1.0.0-canary.1 → 1.0.0-canary.11
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 +117 -0
- package/dist/index.d.mts +87 -71
- package/dist/index.d.ts +87 -71
- package/dist/index.js +437 -452
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +407 -421
- package/dist/index.mjs.map +1 -1
- package/{internal/dist → dist/internal}/index.d.mts +8 -9
- package/{internal/dist → dist/internal}/index.d.ts +8 -9
- package/{internal/dist → dist/internal}/index.js +15 -15
- package/dist/internal/index.js.map +1 -0
- package/{internal/dist → dist/internal}/index.mjs +15 -15
- package/dist/internal/index.mjs.map +1 -0
- package/internal.d.ts +1 -0
- package/package.json +17 -16
- package/internal/dist/index.js.map +0 -1
- package/internal/dist/index.mjs.map +0 -1
package/dist/index.js
CHANGED
@@ -30,15 +30,14 @@ module.exports = __toCommonJS(src_exports);
|
|
30
30
|
|
31
31
|
// src/openai-compatible-chat-language-model.ts
|
32
32
|
var import_provider3 = require("@ai-sdk/provider");
|
33
|
-
var
|
34
|
-
var
|
33
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
34
|
+
var import_zod3 = require("zod");
|
35
35
|
|
36
36
|
// src/convert-to-openai-compatible-chat-messages.ts
|
37
37
|
var import_provider = require("@ai-sdk/provider");
|
38
|
-
var import_provider_utils = require("@ai-sdk/provider-utils");
|
39
38
|
function getOpenAIMetadata(message) {
|
40
39
|
var _a, _b;
|
41
|
-
return (_b = (_a = message == null ? void 0 : message.
|
40
|
+
return (_b = (_a = message == null ? void 0 : message.providerOptions) == null ? void 0 : _a.openaiCompatible) != null ? _b : {};
|
42
41
|
}
|
43
42
|
function convertToOpenAICompatibleChatMessages(prompt) {
|
44
43
|
const messages = [];
|
@@ -61,25 +60,26 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
61
60
|
messages.push({
|
62
61
|
role: "user",
|
63
62
|
content: content.map((part) => {
|
64
|
-
var _a;
|
65
63
|
const partMetadata = getOpenAIMetadata(part);
|
66
64
|
switch (part.type) {
|
67
65
|
case "text": {
|
68
66
|
return { type: "text", text: part.text, ...partMetadata };
|
69
67
|
}
|
70
|
-
case "image": {
|
71
|
-
return {
|
72
|
-
type: "image_url",
|
73
|
-
image_url: {
|
74
|
-
url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${(0, import_provider_utils.convertUint8ArrayToBase64)(part.image)}`
|
75
|
-
},
|
76
|
-
...partMetadata
|
77
|
-
};
|
78
|
-
}
|
79
68
|
case "file": {
|
80
|
-
|
81
|
-
|
82
|
-
|
69
|
+
if (part.mediaType.startsWith("image/")) {
|
70
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
71
|
+
return {
|
72
|
+
type: "image_url",
|
73
|
+
image_url: {
|
74
|
+
url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${part.data}`
|
75
|
+
},
|
76
|
+
...partMetadata
|
77
|
+
};
|
78
|
+
} else {
|
79
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
80
|
+
functionality: `file part media type ${part.mediaType}`
|
81
|
+
});
|
82
|
+
}
|
83
83
|
}
|
84
84
|
}
|
85
85
|
}),
|
@@ -170,17 +170,31 @@ function mapOpenAICompatibleFinishReason(finishReason) {
|
|
170
170
|
}
|
171
171
|
}
|
172
172
|
|
173
|
-
// src/openai-compatible-
|
173
|
+
// src/openai-compatible-chat-options.ts
|
174
174
|
var import_zod = require("zod");
|
175
|
-
var
|
176
|
-
|
177
|
-
|
175
|
+
var openaiCompatibleProviderOptions = import_zod.z.object({
|
176
|
+
/**
|
177
|
+
* A unique identifier representing your end-user, which can help the provider to
|
178
|
+
* monitor and detect abuse.
|
179
|
+
*/
|
180
|
+
user: import_zod.z.string().optional(),
|
181
|
+
/**
|
182
|
+
* Reasoning effort for reasoning models. Defaults to `medium`.
|
183
|
+
*/
|
184
|
+
reasoningEffort: import_zod.z.enum(["low", "medium", "high"]).optional()
|
185
|
+
});
|
186
|
+
|
187
|
+
// src/openai-compatible-error.ts
|
188
|
+
var import_zod2 = require("zod");
|
189
|
+
var openaiCompatibleErrorDataSchema = import_zod2.z.object({
|
190
|
+
error: import_zod2.z.object({
|
191
|
+
message: import_zod2.z.string(),
|
178
192
|
// The additional information below is handled loosely to support
|
179
193
|
// OpenAI-compatible providers that have slightly different error
|
180
194
|
// responses:
|
181
|
-
type:
|
182
|
-
param:
|
183
|
-
code:
|
195
|
+
type: import_zod2.z.string().nullish(),
|
196
|
+
param: import_zod2.z.any().nullish(),
|
197
|
+
code: import_zod2.z.union([import_zod2.z.string(), import_zod2.z.number()]).nullish()
|
184
198
|
})
|
185
199
|
});
|
186
200
|
var defaultOpenAICompatibleErrorStructure = {
|
@@ -191,16 +205,14 @@ var defaultOpenAICompatibleErrorStructure = {
|
|
191
205
|
// src/openai-compatible-prepare-tools.ts
|
192
206
|
var import_provider2 = require("@ai-sdk/provider");
|
193
207
|
function prepareTools({
|
194
|
-
|
195
|
-
|
208
|
+
tools,
|
209
|
+
toolChoice
|
196
210
|
}) {
|
197
|
-
|
198
|
-
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
211
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
199
212
|
const toolWarnings = [];
|
200
213
|
if (tools == null) {
|
201
|
-
return { tools: void 0,
|
214
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
202
215
|
}
|
203
|
-
const toolChoice = mode.toolChoice;
|
204
216
|
const openaiCompatTools = [];
|
205
217
|
for (const tool of tools) {
|
206
218
|
if (tool.type === "provider-defined") {
|
@@ -217,29 +229,27 @@ function prepareTools({
|
|
217
229
|
}
|
218
230
|
}
|
219
231
|
if (toolChoice == null) {
|
220
|
-
return { tools: openaiCompatTools,
|
232
|
+
return { tools: openaiCompatTools, toolChoice: void 0, toolWarnings };
|
221
233
|
}
|
222
234
|
const type = toolChoice.type;
|
223
235
|
switch (type) {
|
224
236
|
case "auto":
|
225
237
|
case "none":
|
226
238
|
case "required":
|
227
|
-
return { tools: openaiCompatTools,
|
239
|
+
return { tools: openaiCompatTools, toolChoice: type, toolWarnings };
|
228
240
|
case "tool":
|
229
241
|
return {
|
230
242
|
tools: openaiCompatTools,
|
231
|
-
|
243
|
+
toolChoice: {
|
232
244
|
type: "function",
|
233
|
-
function: {
|
234
|
-
name: toolChoice.toolName
|
235
|
-
}
|
245
|
+
function: { name: toolChoice.toolName }
|
236
246
|
},
|
237
247
|
toolWarnings
|
238
248
|
};
|
239
249
|
default: {
|
240
250
|
const _exhaustiveCheck = type;
|
241
251
|
throw new import_provider2.UnsupportedFunctionalityError({
|
242
|
-
functionality: `
|
252
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
243
253
|
});
|
244
254
|
}
|
245
255
|
}
|
@@ -248,50 +258,59 @@ function prepareTools({
|
|
248
258
|
// src/openai-compatible-chat-language-model.ts
|
249
259
|
var OpenAICompatibleChatLanguageModel = class {
|
250
260
|
// type inferred via constructor
|
251
|
-
constructor(modelId,
|
261
|
+
constructor(modelId, config) {
|
252
262
|
this.specificationVersion = "v2";
|
253
263
|
var _a, _b;
|
254
264
|
this.modelId = modelId;
|
255
|
-
this.settings = settings;
|
256
265
|
this.config = config;
|
257
266
|
const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
|
258
267
|
this.chunkSchema = createOpenAICompatibleChatChunkSchema(
|
259
268
|
errorStructure.errorSchema
|
260
269
|
);
|
261
|
-
this.failedResponseHandler = (0,
|
270
|
+
this.failedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)(errorStructure);
|
262
271
|
this.supportsStructuredOutputs = (_b = config.supportsStructuredOutputs) != null ? _b : false;
|
263
272
|
}
|
264
|
-
get defaultObjectGenerationMode() {
|
265
|
-
return this.config.defaultObjectGenerationMode;
|
266
|
-
}
|
267
273
|
get provider() {
|
268
274
|
return this.config.provider;
|
269
275
|
}
|
270
276
|
get providerOptionsName() {
|
271
277
|
return this.config.provider.split(".")[0].trim();
|
272
278
|
}
|
273
|
-
|
274
|
-
|
279
|
+
async getSupportedUrls() {
|
280
|
+
var _a, _b, _c;
|
281
|
+
return (_c = (_b = (_a = this.config).getSupportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
|
282
|
+
}
|
283
|
+
async getArgs({
|
275
284
|
prompt,
|
276
|
-
|
285
|
+
maxOutputTokens,
|
277
286
|
temperature,
|
278
287
|
topP,
|
279
288
|
topK,
|
280
289
|
frequencyPenalty,
|
281
290
|
presencePenalty,
|
282
|
-
|
291
|
+
providerOptions,
|
283
292
|
stopSequences,
|
284
293
|
responseFormat,
|
285
|
-
seed
|
294
|
+
seed,
|
295
|
+
toolChoice,
|
296
|
+
tools
|
286
297
|
}) {
|
287
|
-
var _a, _b;
|
288
|
-
const type = mode.type;
|
298
|
+
var _a, _b, _c;
|
289
299
|
const warnings = [];
|
300
|
+
const compatibleOptions = Object.assign(
|
301
|
+
(_a = await (0, import_provider_utils.parseProviderOptions)({
|
302
|
+
provider: "openai-compatible",
|
303
|
+
providerOptions,
|
304
|
+
schema: openaiCompatibleProviderOptions
|
305
|
+
})) != null ? _a : {},
|
306
|
+
(_b = await (0, import_provider_utils.parseProviderOptions)({
|
307
|
+
provider: this.providerOptionsName,
|
308
|
+
providerOptions,
|
309
|
+
schema: openaiCompatibleProviderOptions
|
310
|
+
})) != null ? _b : {}
|
311
|
+
);
|
290
312
|
if (topK != null) {
|
291
|
-
warnings.push({
|
292
|
-
type: "unsupported-setting",
|
293
|
-
setting: "topK"
|
294
|
-
});
|
313
|
+
warnings.push({ type: "unsupported-setting", setting: "topK" });
|
295
314
|
}
|
296
315
|
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.supportsStructuredOutputs) {
|
297
316
|
warnings.push({
|
@@ -300,118 +319,101 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
300
319
|
details: "JSON response format schema is only supported with structuredOutputs"
|
301
320
|
});
|
302
321
|
}
|
303
|
-
const
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
322
|
+
const {
|
323
|
+
tools: openaiTools,
|
324
|
+
toolChoice: openaiToolChoice,
|
325
|
+
toolWarnings
|
326
|
+
} = prepareTools({
|
327
|
+
tools,
|
328
|
+
toolChoice
|
329
|
+
});
|
330
|
+
return {
|
331
|
+
args: {
|
332
|
+
// model id:
|
333
|
+
model: this.modelId,
|
334
|
+
// model specific settings:
|
335
|
+
user: compatibleOptions.user,
|
336
|
+
// standardized settings:
|
337
|
+
max_tokens: maxOutputTokens,
|
338
|
+
temperature,
|
339
|
+
top_p: topP,
|
340
|
+
frequency_penalty: frequencyPenalty,
|
341
|
+
presence_penalty: presencePenalty,
|
342
|
+
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? this.supportsStructuredOutputs === true && responseFormat.schema != null ? {
|
343
|
+
type: "json_schema",
|
344
|
+
json_schema: {
|
345
|
+
schema: responseFormat.schema,
|
346
|
+
name: (_c = responseFormat.name) != null ? _c : "response",
|
347
|
+
description: responseFormat.description
|
348
|
+
}
|
349
|
+
} : { type: "json_object" } : void 0,
|
350
|
+
stop: stopSequences,
|
351
|
+
seed,
|
352
|
+
...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
|
353
|
+
reasoning_effort: compatibleOptions.reasoningEffort,
|
354
|
+
// messages:
|
355
|
+
messages: convertToOpenAICompatibleChatMessages(prompt),
|
356
|
+
// tools:
|
357
|
+
tools: openaiTools,
|
358
|
+
tool_choice: openaiToolChoice
|
359
|
+
},
|
360
|
+
warnings: [...warnings, ...toolWarnings]
|
327
361
|
};
|
328
|
-
switch (type) {
|
329
|
-
case "regular": {
|
330
|
-
const { tools, tool_choice, toolWarnings } = prepareTools({
|
331
|
-
mode,
|
332
|
-
structuredOutputs: this.supportsStructuredOutputs
|
333
|
-
});
|
334
|
-
return {
|
335
|
-
args: { ...baseArgs, tools, tool_choice },
|
336
|
-
warnings: [...warnings, ...toolWarnings]
|
337
|
-
};
|
338
|
-
}
|
339
|
-
case "object-json": {
|
340
|
-
return {
|
341
|
-
args: {
|
342
|
-
...baseArgs,
|
343
|
-
response_format: this.supportsStructuredOutputs === true && mode.schema != null ? {
|
344
|
-
type: "json_schema",
|
345
|
-
json_schema: {
|
346
|
-
schema: mode.schema,
|
347
|
-
name: (_b = mode.name) != null ? _b : "response",
|
348
|
-
description: mode.description
|
349
|
-
}
|
350
|
-
} : { type: "json_object" }
|
351
|
-
},
|
352
|
-
warnings
|
353
|
-
};
|
354
|
-
}
|
355
|
-
case "object-tool": {
|
356
|
-
return {
|
357
|
-
args: {
|
358
|
-
...baseArgs,
|
359
|
-
tool_choice: {
|
360
|
-
type: "function",
|
361
|
-
function: { name: mode.tool.name }
|
362
|
-
},
|
363
|
-
tools: [
|
364
|
-
{
|
365
|
-
type: "function",
|
366
|
-
function: {
|
367
|
-
name: mode.tool.name,
|
368
|
-
description: mode.tool.description,
|
369
|
-
parameters: mode.tool.parameters
|
370
|
-
}
|
371
|
-
}
|
372
|
-
]
|
373
|
-
},
|
374
|
-
warnings
|
375
|
-
};
|
376
|
-
}
|
377
|
-
default: {
|
378
|
-
const _exhaustiveCheck = type;
|
379
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
380
|
-
}
|
381
|
-
}
|
382
362
|
}
|
383
363
|
async doGenerate(options) {
|
384
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i
|
385
|
-
const { args, warnings } = this.getArgs({ ...options });
|
364
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
365
|
+
const { args, warnings } = await this.getArgs({ ...options });
|
386
366
|
const body = JSON.stringify(args);
|
387
367
|
const {
|
388
368
|
responseHeaders,
|
389
369
|
value: responseBody,
|
390
370
|
rawValue: rawResponse
|
391
|
-
} = await (0,
|
371
|
+
} = await (0, import_provider_utils.postJsonToApi)({
|
392
372
|
url: this.config.url({
|
393
373
|
path: "/chat/completions",
|
394
374
|
modelId: this.modelId
|
395
375
|
}),
|
396
|
-
headers: (0,
|
376
|
+
headers: (0, import_provider_utils.combineHeaders)(this.config.headers(), options.headers),
|
397
377
|
body: args,
|
398
378
|
failedResponseHandler: this.failedResponseHandler,
|
399
|
-
successfulResponseHandler: (0,
|
379
|
+
successfulResponseHandler: (0, import_provider_utils.createJsonResponseHandler)(
|
400
380
|
OpenAICompatibleChatResponseSchema
|
401
381
|
),
|
402
382
|
abortSignal: options.abortSignal,
|
403
383
|
fetch: this.config.fetch
|
404
384
|
});
|
405
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
406
385
|
const choice = responseBody.choices[0];
|
386
|
+
const content = [];
|
387
|
+
const text = choice.message.content;
|
388
|
+
if (text != null && text.length > 0) {
|
389
|
+
content.push({ type: "text", text });
|
390
|
+
}
|
391
|
+
const reasoning = choice.message.reasoning_content;
|
392
|
+
if (reasoning != null && reasoning.length > 0) {
|
393
|
+
content.push({
|
394
|
+
type: "reasoning",
|
395
|
+
text: reasoning
|
396
|
+
});
|
397
|
+
}
|
398
|
+
if (choice.message.tool_calls != null) {
|
399
|
+
for (const toolCall of choice.message.tool_calls) {
|
400
|
+
content.push({
|
401
|
+
type: "tool-call",
|
402
|
+
toolCallType: "function",
|
403
|
+
toolCallId: (_a = toolCall.id) != null ? _a : (0, import_provider_utils.generateId)(),
|
404
|
+
toolName: toolCall.function.name,
|
405
|
+
args: toolCall.function.arguments
|
406
|
+
});
|
407
|
+
}
|
408
|
+
}
|
407
409
|
const providerMetadata = {
|
408
410
|
[this.providerOptionsName]: {},
|
409
|
-
...(
|
411
|
+
...await ((_c = (_b = this.config.metadataExtractor) == null ? void 0 : _b.extractMetadata) == null ? void 0 : _c.call(_b, {
|
410
412
|
parsedBody: rawResponse
|
411
|
-
})
|
413
|
+
}))
|
412
414
|
};
|
413
|
-
const completionTokenDetails = (
|
414
|
-
const promptTokenDetails = (
|
415
|
+
const completionTokenDetails = (_d = responseBody.usage) == null ? void 0 : _d.completion_tokens_details;
|
416
|
+
const promptTokenDetails = (_e = responseBody.usage) == null ? void 0 : _e.prompt_tokens_details;
|
415
417
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens) != null) {
|
416
418
|
providerMetadata[this.providerOptionsName].reasoningTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens;
|
417
419
|
}
|
@@ -425,106 +427,46 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
425
427
|
providerMetadata[this.providerOptionsName].cachedPromptTokens = promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens;
|
426
428
|
}
|
427
429
|
return {
|
428
|
-
|
429
|
-
reasoning: (_f = choice.message.reasoning_content) != null ? _f : void 0,
|
430
|
-
toolCalls: (_g = choice.message.tool_calls) == null ? void 0 : _g.map((toolCall) => {
|
431
|
-
var _a2;
|
432
|
-
return {
|
433
|
-
toolCallType: "function",
|
434
|
-
toolCallId: (_a2 = toolCall.id) != null ? _a2 : (0, import_provider_utils2.generateId)(),
|
435
|
-
toolName: toolCall.function.name,
|
436
|
-
args: toolCall.function.arguments
|
437
|
-
};
|
438
|
-
}),
|
430
|
+
content,
|
439
431
|
finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
440
432
|
usage: {
|
441
|
-
|
442
|
-
|
433
|
+
inputTokens: (_g = (_f = responseBody.usage) == null ? void 0 : _f.prompt_tokens) != null ? _g : void 0,
|
434
|
+
outputTokens: (_i = (_h = responseBody.usage) == null ? void 0 : _h.completion_tokens) != null ? _i : void 0
|
443
435
|
},
|
444
436
|
providerMetadata,
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
437
|
+
request: { body },
|
438
|
+
response: {
|
439
|
+
...getResponseMetadata(responseBody),
|
440
|
+
headers: responseHeaders,
|
441
|
+
body: rawResponse
|
442
|
+
},
|
443
|
+
warnings
|
450
444
|
};
|
451
445
|
}
|
452
446
|
async doStream(options) {
|
453
447
|
var _a;
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
for (const part of result.reasoning) {
|
462
|
-
if (part.type === "text") {
|
463
|
-
controller.enqueue({
|
464
|
-
type: "reasoning",
|
465
|
-
textDelta: part.text
|
466
|
-
});
|
467
|
-
}
|
468
|
-
}
|
469
|
-
} else {
|
470
|
-
controller.enqueue({
|
471
|
-
type: "reasoning",
|
472
|
-
textDelta: result.reasoning
|
473
|
-
});
|
474
|
-
}
|
475
|
-
}
|
476
|
-
if (result.text) {
|
477
|
-
controller.enqueue({
|
478
|
-
type: "text-delta",
|
479
|
-
textDelta: result.text
|
480
|
-
});
|
481
|
-
}
|
482
|
-
if (result.toolCalls) {
|
483
|
-
for (const toolCall of result.toolCalls) {
|
484
|
-
controller.enqueue({
|
485
|
-
type: "tool-call",
|
486
|
-
...toolCall
|
487
|
-
});
|
488
|
-
}
|
489
|
-
}
|
490
|
-
controller.enqueue({
|
491
|
-
type: "finish",
|
492
|
-
finishReason: result.finishReason,
|
493
|
-
usage: result.usage,
|
494
|
-
logprobs: result.logprobs,
|
495
|
-
providerMetadata: result.providerMetadata
|
496
|
-
});
|
497
|
-
controller.close();
|
498
|
-
}
|
499
|
-
});
|
500
|
-
return {
|
501
|
-
stream: simulatedStream,
|
502
|
-
rawCall: result.rawCall,
|
503
|
-
rawResponse: result.rawResponse,
|
504
|
-
warnings: result.warnings
|
505
|
-
};
|
506
|
-
}
|
507
|
-
const { args, warnings } = this.getArgs({ ...options });
|
508
|
-
const body = JSON.stringify({ ...args, stream: true });
|
448
|
+
const { args, warnings } = await this.getArgs({ ...options });
|
449
|
+
const body = {
|
450
|
+
...args,
|
451
|
+
stream: true,
|
452
|
+
// only include stream_options when in strict compatibility mode:
|
453
|
+
stream_options: this.config.includeUsage ? { include_usage: true } : void 0
|
454
|
+
};
|
509
455
|
const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
|
510
|
-
const { responseHeaders, value: response } = await (0,
|
456
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils.postJsonToApi)({
|
511
457
|
url: this.config.url({
|
512
458
|
path: "/chat/completions",
|
513
459
|
modelId: this.modelId
|
514
460
|
}),
|
515
|
-
headers: (0,
|
516
|
-
body
|
517
|
-
...args,
|
518
|
-
stream: true
|
519
|
-
},
|
461
|
+
headers: (0, import_provider_utils.combineHeaders)(this.config.headers(), options.headers),
|
462
|
+
body,
|
520
463
|
failedResponseHandler: this.failedResponseHandler,
|
521
|
-
successfulResponseHandler: (0,
|
464
|
+
successfulResponseHandler: (0, import_provider_utils.createEventSourceResponseHandler)(
|
522
465
|
this.chunkSchema
|
523
466
|
),
|
524
467
|
abortSignal: options.abortSignal,
|
525
468
|
fetch: this.config.fetch
|
526
469
|
});
|
527
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
528
470
|
const toolCalls = [];
|
529
471
|
let finishReason = "unknown";
|
530
472
|
let usage = {
|
@@ -544,6 +486,9 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
544
486
|
return {
|
545
487
|
stream: response.pipeThrough(
|
546
488
|
new TransformStream({
|
489
|
+
start(controller) {
|
490
|
+
controller.enqueue({ type: "stream-start", warnings });
|
491
|
+
},
|
547
492
|
// TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
|
548
493
|
transform(chunk, controller) {
|
549
494
|
var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
@@ -601,13 +546,13 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
601
546
|
if (delta.reasoning_content != null) {
|
602
547
|
controller.enqueue({
|
603
548
|
type: "reasoning",
|
604
|
-
|
549
|
+
text: delta.reasoning_content
|
605
550
|
});
|
606
551
|
}
|
607
552
|
if (delta.content != null) {
|
608
553
|
controller.enqueue({
|
609
|
-
type: "text
|
610
|
-
|
554
|
+
type: "text",
|
555
|
+
text: delta.content
|
611
556
|
});
|
612
557
|
}
|
613
558
|
if (delta.tool_calls != null) {
|
@@ -652,11 +597,11 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
652
597
|
argsTextDelta: toolCall2.function.arguments
|
653
598
|
});
|
654
599
|
}
|
655
|
-
if ((0,
|
600
|
+
if ((0, import_provider_utils.isParsableJson)(toolCall2.function.arguments)) {
|
656
601
|
controller.enqueue({
|
657
602
|
type: "tool-call",
|
658
603
|
toolCallType: "function",
|
659
|
-
toolCallId: (_e = toolCall2.id) != null ? _e : (0,
|
604
|
+
toolCallId: (_e = toolCall2.id) != null ? _e : (0, import_provider_utils.generateId)(),
|
660
605
|
toolName: toolCall2.function.name,
|
661
606
|
args: toolCall2.function.arguments
|
662
607
|
});
|
@@ -679,11 +624,11 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
679
624
|
toolName: toolCall.function.name,
|
680
625
|
argsTextDelta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
|
681
626
|
});
|
682
|
-
if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && (0,
|
627
|
+
if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && (0, import_provider_utils.isParsableJson)(toolCall.function.arguments)) {
|
683
628
|
controller.enqueue({
|
684
629
|
type: "tool-call",
|
685
630
|
toolCallType: "function",
|
686
|
-
toolCallId: (_l = toolCall.id) != null ? _l : (0,
|
631
|
+
toolCallId: (_l = toolCall.id) != null ? _l : (0, import_provider_utils.generateId)(),
|
687
632
|
toolName: toolCall.function.name,
|
688
633
|
args: toolCall.function.arguments
|
689
634
|
});
|
@@ -714,83 +659,81 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
714
659
|
type: "finish",
|
715
660
|
finishReason,
|
716
661
|
usage: {
|
717
|
-
|
718
|
-
|
662
|
+
inputTokens: (_a2 = usage.promptTokens) != null ? _a2 : void 0,
|
663
|
+
outputTokens: (_b = usage.completionTokens) != null ? _b : void 0
|
719
664
|
},
|
720
665
|
providerMetadata
|
721
666
|
});
|
722
667
|
}
|
723
668
|
})
|
724
669
|
),
|
725
|
-
|
726
|
-
|
727
|
-
warnings,
|
728
|
-
request: { body }
|
670
|
+
request: { body },
|
671
|
+
response: { headers: responseHeaders }
|
729
672
|
};
|
730
673
|
}
|
731
674
|
};
|
732
|
-
var openaiCompatibleTokenUsageSchema =
|
733
|
-
prompt_tokens:
|
734
|
-
completion_tokens:
|
735
|
-
prompt_tokens_details:
|
736
|
-
cached_tokens:
|
675
|
+
var openaiCompatibleTokenUsageSchema = import_zod3.z.object({
|
676
|
+
prompt_tokens: import_zod3.z.number().nullish(),
|
677
|
+
completion_tokens: import_zod3.z.number().nullish(),
|
678
|
+
prompt_tokens_details: import_zod3.z.object({
|
679
|
+
cached_tokens: import_zod3.z.number().nullish()
|
737
680
|
}).nullish(),
|
738
|
-
completion_tokens_details:
|
739
|
-
reasoning_tokens:
|
740
|
-
accepted_prediction_tokens:
|
741
|
-
rejected_prediction_tokens:
|
681
|
+
completion_tokens_details: import_zod3.z.object({
|
682
|
+
reasoning_tokens: import_zod3.z.number().nullish(),
|
683
|
+
accepted_prediction_tokens: import_zod3.z.number().nullish(),
|
684
|
+
rejected_prediction_tokens: import_zod3.z.number().nullish()
|
742
685
|
}).nullish()
|
743
686
|
}).nullish();
|
744
|
-
var OpenAICompatibleChatResponseSchema =
|
745
|
-
id:
|
746
|
-
created:
|
747
|
-
model:
|
748
|
-
choices:
|
749
|
-
|
750
|
-
message:
|
751
|
-
role:
|
752
|
-
content:
|
753
|
-
reasoning_content:
|
754
|
-
tool_calls:
|
755
|
-
|
756
|
-
id:
|
757
|
-
type:
|
758
|
-
function:
|
759
|
-
name:
|
760
|
-
arguments:
|
687
|
+
var OpenAICompatibleChatResponseSchema = import_zod3.z.object({
|
688
|
+
id: import_zod3.z.string().nullish(),
|
689
|
+
created: import_zod3.z.number().nullish(),
|
690
|
+
model: import_zod3.z.string().nullish(),
|
691
|
+
choices: import_zod3.z.array(
|
692
|
+
import_zod3.z.object({
|
693
|
+
message: import_zod3.z.object({
|
694
|
+
role: import_zod3.z.literal("assistant").nullish(),
|
695
|
+
content: import_zod3.z.string().nullish(),
|
696
|
+
reasoning_content: import_zod3.z.string().nullish(),
|
697
|
+
tool_calls: import_zod3.z.array(
|
698
|
+
import_zod3.z.object({
|
699
|
+
id: import_zod3.z.string().nullish(),
|
700
|
+
type: import_zod3.z.literal("function"),
|
701
|
+
function: import_zod3.z.object({
|
702
|
+
name: import_zod3.z.string(),
|
703
|
+
arguments: import_zod3.z.string()
|
761
704
|
})
|
762
705
|
})
|
763
706
|
).nullish()
|
764
707
|
}),
|
765
|
-
finish_reason:
|
708
|
+
finish_reason: import_zod3.z.string().nullish()
|
766
709
|
})
|
767
710
|
),
|
768
711
|
usage: openaiCompatibleTokenUsageSchema
|
769
712
|
});
|
770
|
-
var createOpenAICompatibleChatChunkSchema = (errorSchema) =>
|
771
|
-
|
772
|
-
id:
|
773
|
-
created:
|
774
|
-
model:
|
775
|
-
choices:
|
776
|
-
|
777
|
-
delta:
|
778
|
-
role:
|
779
|
-
content:
|
780
|
-
reasoning_content:
|
781
|
-
tool_calls:
|
782
|
-
|
783
|
-
index:
|
784
|
-
id:
|
785
|
-
type:
|
786
|
-
function:
|
787
|
-
name:
|
788
|
-
arguments:
|
713
|
+
var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_zod3.z.union([
|
714
|
+
import_zod3.z.object({
|
715
|
+
id: import_zod3.z.string().nullish(),
|
716
|
+
created: import_zod3.z.number().nullish(),
|
717
|
+
model: import_zod3.z.string().nullish(),
|
718
|
+
choices: import_zod3.z.array(
|
719
|
+
import_zod3.z.object({
|
720
|
+
delta: import_zod3.z.object({
|
721
|
+
role: import_zod3.z.enum(["assistant"]).nullish(),
|
722
|
+
content: import_zod3.z.string().nullish(),
|
723
|
+
reasoning_content: import_zod3.z.string().nullish(),
|
724
|
+
tool_calls: import_zod3.z.array(
|
725
|
+
import_zod3.z.object({
|
726
|
+
index: import_zod3.z.number(),
|
727
|
+
id: import_zod3.z.string().nullish(),
|
728
|
+
type: import_zod3.z.literal("function").nullish(),
|
729
|
+
function: import_zod3.z.object({
|
730
|
+
name: import_zod3.z.string().nullish(),
|
731
|
+
arguments: import_zod3.z.string().nullish()
|
789
732
|
})
|
790
733
|
})
|
791
734
|
).nullish()
|
792
735
|
}).nullish(),
|
793
|
-
finish_reason:
|
736
|
+
finish_reason: import_zod3.z.string().nullish()
|
794
737
|
})
|
795
738
|
),
|
796
739
|
usage: openaiCompatibleTokenUsageSchema
|
@@ -799,9 +742,8 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_zod2.z.union
|
|
799
742
|
]);
|
800
743
|
|
801
744
|
// src/openai-compatible-completion-language-model.ts
|
802
|
-
var
|
803
|
-
var
|
804
|
-
var import_zod3 = require("zod");
|
745
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
746
|
+
var import_zod5 = require("zod");
|
805
747
|
|
806
748
|
// src/convert-to-openai-compatible-completion-prompt.ts
|
807
749
|
var import_provider4 = require("@ai-sdk/provider");
|
@@ -835,13 +777,8 @@ function convertToOpenAICompatibleCompletionPrompt({
|
|
835
777
|
case "text": {
|
836
778
|
return part.text;
|
837
779
|
}
|
838
|
-
case "image": {
|
839
|
-
throw new import_provider4.UnsupportedFunctionalityError({
|
840
|
-
functionality: "images"
|
841
|
-
});
|
842
|
-
}
|
843
780
|
}
|
844
|
-
}).join("");
|
781
|
+
}).filter(Boolean).join("");
|
845
782
|
text += `${user}:
|
846
783
|
${userMessage}
|
847
784
|
|
@@ -887,21 +824,44 @@ ${user}:`]
|
|
887
824
|
};
|
888
825
|
}
|
889
826
|
|
827
|
+
// src/openai-compatible-completion-options.ts
|
828
|
+
var import_zod4 = require("zod");
|
829
|
+
var openaiCompatibleCompletionProviderOptions = import_zod4.z.object({
|
830
|
+
/**
|
831
|
+
* Echo back the prompt in addition to the completion.
|
832
|
+
*/
|
833
|
+
echo: import_zod4.z.boolean().optional(),
|
834
|
+
/**
|
835
|
+
* Modify the likelihood of specified tokens appearing in the completion.
|
836
|
+
*
|
837
|
+
* Accepts a JSON object that maps tokens (specified by their token ID in
|
838
|
+
* the GPT tokenizer) to an associated bias value from -100 to 100.
|
839
|
+
*/
|
840
|
+
logitBias: import_zod4.z.record(import_zod4.z.string(), import_zod4.z.number()).optional(),
|
841
|
+
/**
|
842
|
+
* The suffix that comes after a completion of inserted text.
|
843
|
+
*/
|
844
|
+
suffix: import_zod4.z.string().optional(),
|
845
|
+
/**
|
846
|
+
* A unique identifier representing your end-user, which can help providers to
|
847
|
+
* monitor and detect abuse.
|
848
|
+
*/
|
849
|
+
user: import_zod4.z.string().optional()
|
850
|
+
});
|
851
|
+
|
890
852
|
// src/openai-compatible-completion-language-model.ts
|
891
853
|
var OpenAICompatibleCompletionLanguageModel = class {
|
892
854
|
// type inferred via constructor
|
893
|
-
constructor(modelId,
|
855
|
+
constructor(modelId, config) {
|
894
856
|
this.specificationVersion = "v2";
|
895
|
-
this.defaultObjectGenerationMode = void 0;
|
896
857
|
var _a;
|
897
858
|
this.modelId = modelId;
|
898
|
-
this.settings = settings;
|
899
859
|
this.config = config;
|
900
860
|
const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
|
901
861
|
this.chunkSchema = createOpenAICompatibleCompletionChunkSchema(
|
902
862
|
errorStructure.errorSchema
|
903
863
|
);
|
904
|
-
this.failedResponseHandler = (0,
|
864
|
+
this.failedResponseHandler = (0, import_provider_utils2.createJsonErrorResponseHandler)(errorStructure);
|
905
865
|
}
|
906
866
|
get provider() {
|
907
867
|
return this.config.provider;
|
@@ -909,11 +869,14 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
909
869
|
get providerOptionsName() {
|
910
870
|
return this.config.provider.split(".")[0].trim();
|
911
871
|
}
|
912
|
-
|
913
|
-
|
872
|
+
async getSupportedUrls() {
|
873
|
+
var _a, _b, _c;
|
874
|
+
return (_c = (_b = (_a = this.config).getSupportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
|
875
|
+
}
|
876
|
+
async getArgs({
|
914
877
|
inputFormat,
|
915
878
|
prompt,
|
916
|
-
|
879
|
+
maxOutputTokens,
|
917
880
|
temperature,
|
918
881
|
topP,
|
919
882
|
topK,
|
@@ -922,16 +885,25 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
922
885
|
stopSequences: userStopSequences,
|
923
886
|
responseFormat,
|
924
887
|
seed,
|
925
|
-
|
888
|
+
providerOptions,
|
889
|
+
tools,
|
890
|
+
toolChoice
|
926
891
|
}) {
|
927
892
|
var _a;
|
928
|
-
const type = mode.type;
|
929
893
|
const warnings = [];
|
894
|
+
const completionOptions = (_a = await (0, import_provider_utils2.parseProviderOptions)({
|
895
|
+
provider: this.providerOptionsName,
|
896
|
+
providerOptions,
|
897
|
+
schema: openaiCompatibleCompletionProviderOptions
|
898
|
+
})) != null ? _a : {};
|
930
899
|
if (topK != null) {
|
931
|
-
warnings.push({
|
932
|
-
|
933
|
-
|
934
|
-
});
|
900
|
+
warnings.push({ type: "unsupported-setting", setting: "topK" });
|
901
|
+
}
|
902
|
+
if (tools == null ? void 0 : tools.length) {
|
903
|
+
warnings.push({ type: "unsupported-setting", setting: "tools" });
|
904
|
+
}
|
905
|
+
if (toolChoice != null) {
|
906
|
+
warnings.push({ type: "unsupported-setting", setting: "toolChoice" });
|
935
907
|
}
|
936
908
|
if (responseFormat != null && responseFormat.type !== "text") {
|
937
909
|
warnings.push({
|
@@ -942,125 +914,109 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
942
914
|
}
|
943
915
|
const { prompt: completionPrompt, stopSequences } = convertToOpenAICompatibleCompletionPrompt({ prompt, inputFormat });
|
944
916
|
const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
917
|
+
return {
|
918
|
+
args: {
|
919
|
+
// model id:
|
920
|
+
model: this.modelId,
|
921
|
+
// model specific settings:
|
922
|
+
echo: completionOptions.echo,
|
923
|
+
logit_bias: completionOptions.logitBias,
|
924
|
+
suffix: completionOptions.suffix,
|
925
|
+
user: completionOptions.user,
|
926
|
+
// standardized settings:
|
927
|
+
max_tokens: maxOutputTokens,
|
928
|
+
temperature,
|
929
|
+
top_p: topP,
|
930
|
+
frequency_penalty: frequencyPenalty,
|
931
|
+
presence_penalty: presencePenalty,
|
932
|
+
seed,
|
933
|
+
...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
|
934
|
+
// prompt:
|
935
|
+
prompt: completionPrompt,
|
936
|
+
// stop sequences:
|
937
|
+
stop: stop.length > 0 ? stop : void 0
|
938
|
+
},
|
939
|
+
warnings
|
965
940
|
};
|
966
|
-
switch (type) {
|
967
|
-
case "regular": {
|
968
|
-
if ((_a = mode.tools) == null ? void 0 : _a.length) {
|
969
|
-
throw new import_provider5.UnsupportedFunctionalityError({
|
970
|
-
functionality: "tools"
|
971
|
-
});
|
972
|
-
}
|
973
|
-
if (mode.toolChoice) {
|
974
|
-
throw new import_provider5.UnsupportedFunctionalityError({
|
975
|
-
functionality: "toolChoice"
|
976
|
-
});
|
977
|
-
}
|
978
|
-
return { args: baseArgs, warnings };
|
979
|
-
}
|
980
|
-
case "object-json": {
|
981
|
-
throw new import_provider5.UnsupportedFunctionalityError({
|
982
|
-
functionality: "object-json mode"
|
983
|
-
});
|
984
|
-
}
|
985
|
-
case "object-tool": {
|
986
|
-
throw new import_provider5.UnsupportedFunctionalityError({
|
987
|
-
functionality: "object-tool mode"
|
988
|
-
});
|
989
|
-
}
|
990
|
-
default: {
|
991
|
-
const _exhaustiveCheck = type;
|
992
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
993
|
-
}
|
994
|
-
}
|
995
941
|
}
|
996
942
|
async doGenerate(options) {
|
997
943
|
var _a, _b, _c, _d;
|
998
|
-
const { args, warnings } = this.getArgs(options);
|
944
|
+
const { args, warnings } = await this.getArgs(options);
|
999
945
|
const {
|
1000
946
|
responseHeaders,
|
1001
947
|
value: response,
|
1002
948
|
rawValue: rawResponse
|
1003
|
-
} = await (0,
|
949
|
+
} = await (0, import_provider_utils2.postJsonToApi)({
|
1004
950
|
url: this.config.url({
|
1005
951
|
path: "/completions",
|
1006
952
|
modelId: this.modelId
|
1007
953
|
}),
|
1008
|
-
headers: (0,
|
954
|
+
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
|
1009
955
|
body: args,
|
1010
956
|
failedResponseHandler: this.failedResponseHandler,
|
1011
|
-
successfulResponseHandler: (0,
|
957
|
+
successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
|
1012
958
|
openaiCompatibleCompletionResponseSchema
|
1013
959
|
),
|
1014
960
|
abortSignal: options.abortSignal,
|
1015
961
|
fetch: this.config.fetch
|
1016
962
|
});
|
1017
|
-
const { prompt: rawPrompt, ...rawSettings } = args;
|
1018
963
|
const choice = response.choices[0];
|
964
|
+
const content = [];
|
965
|
+
if (choice.text != null && choice.text.length > 0) {
|
966
|
+
content.push({ type: "text", text: choice.text });
|
967
|
+
}
|
1019
968
|
return {
|
1020
|
-
|
969
|
+
content,
|
1021
970
|
usage: {
|
1022
|
-
|
1023
|
-
|
971
|
+
inputTokens: (_b = (_a = response.usage) == null ? void 0 : _a.prompt_tokens) != null ? _b : void 0,
|
972
|
+
outputTokens: (_d = (_c = response.usage) == null ? void 0 : _c.completion_tokens) != null ? _d : void 0
|
1024
973
|
},
|
1025
974
|
finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
975
|
+
request: { body: args },
|
976
|
+
response: {
|
977
|
+
...getResponseMetadata(response),
|
978
|
+
headers: responseHeaders,
|
979
|
+
body: rawResponse
|
980
|
+
},
|
981
|
+
warnings
|
1031
982
|
};
|
1032
983
|
}
|
1033
984
|
async doStream(options) {
|
1034
|
-
const { args, warnings } = this.getArgs(options);
|
985
|
+
const { args, warnings } = await this.getArgs(options);
|
1035
986
|
const body = {
|
1036
987
|
...args,
|
1037
|
-
stream: true
|
988
|
+
stream: true,
|
989
|
+
// only include stream_options when in strict compatibility mode:
|
990
|
+
stream_options: this.config.includeUsage ? { include_usage: true } : void 0
|
1038
991
|
};
|
1039
|
-
const { responseHeaders, value: response } = await (0,
|
992
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
1040
993
|
url: this.config.url({
|
1041
994
|
path: "/completions",
|
1042
995
|
modelId: this.modelId
|
1043
996
|
}),
|
1044
|
-
headers: (0,
|
997
|
+
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
|
1045
998
|
body,
|
1046
999
|
failedResponseHandler: this.failedResponseHandler,
|
1047
|
-
successfulResponseHandler: (0,
|
1000
|
+
successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
|
1048
1001
|
this.chunkSchema
|
1049
1002
|
),
|
1050
1003
|
abortSignal: options.abortSignal,
|
1051
1004
|
fetch: this.config.fetch
|
1052
1005
|
});
|
1053
|
-
const { prompt: rawPrompt, ...rawSettings } = args;
|
1054
1006
|
let finishReason = "unknown";
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1007
|
+
const usage = {
|
1008
|
+
inputTokens: void 0,
|
1009
|
+
outputTokens: void 0
|
1058
1010
|
};
|
1059
1011
|
let isFirstChunk = true;
|
1060
1012
|
return {
|
1061
1013
|
stream: response.pipeThrough(
|
1062
1014
|
new TransformStream({
|
1015
|
+
start(controller) {
|
1016
|
+
controller.enqueue({ type: "stream-start", warnings });
|
1017
|
+
},
|
1063
1018
|
transform(chunk, controller) {
|
1019
|
+
var _a, _b;
|
1064
1020
|
if (!chunk.success) {
|
1065
1021
|
finishReason = "error";
|
1066
1022
|
controller.enqueue({ type: "error", error: chunk.error });
|
@@ -1080,10 +1036,8 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
1080
1036
|
});
|
1081
1037
|
}
|
1082
1038
|
if (value.usage != null) {
|
1083
|
-
usage =
|
1084
|
-
|
1085
|
-
completionTokens: value.usage.completion_tokens
|
1086
|
-
};
|
1039
|
+
usage.inputTokens = (_a = value.usage.prompt_tokens) != null ? _a : void 0;
|
1040
|
+
usage.outputTokens = (_b = value.usage.completion_tokens) != null ? _b : void 0;
|
1087
1041
|
}
|
1088
1042
|
const choice = value.choices[0];
|
1089
1043
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
@@ -1093,8 +1047,8 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
1093
1047
|
}
|
1094
1048
|
if ((choice == null ? void 0 : choice.text) != null) {
|
1095
1049
|
controller.enqueue({
|
1096
|
-
type: "text
|
1097
|
-
|
1050
|
+
type: "text",
|
1051
|
+
text: choice.text
|
1098
1052
|
});
|
1099
1053
|
}
|
1100
1054
|
},
|
@@ -1107,57 +1061,71 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
1107
1061
|
}
|
1108
1062
|
})
|
1109
1063
|
),
|
1110
|
-
|
1111
|
-
|
1112
|
-
warnings,
|
1113
|
-
request: { body: JSON.stringify(body) }
|
1064
|
+
request: { body },
|
1065
|
+
response: { headers: responseHeaders }
|
1114
1066
|
};
|
1115
1067
|
}
|
1116
1068
|
};
|
1117
|
-
var openaiCompatibleCompletionResponseSchema =
|
1118
|
-
id:
|
1119
|
-
created:
|
1120
|
-
model:
|
1121
|
-
choices:
|
1122
|
-
|
1123
|
-
text:
|
1124
|
-
finish_reason:
|
1069
|
+
var openaiCompatibleCompletionResponseSchema = import_zod5.z.object({
|
1070
|
+
id: import_zod5.z.string().nullish(),
|
1071
|
+
created: import_zod5.z.number().nullish(),
|
1072
|
+
model: import_zod5.z.string().nullish(),
|
1073
|
+
choices: import_zod5.z.array(
|
1074
|
+
import_zod5.z.object({
|
1075
|
+
text: import_zod5.z.string(),
|
1076
|
+
finish_reason: import_zod5.z.string()
|
1125
1077
|
})
|
1126
1078
|
),
|
1127
|
-
usage:
|
1128
|
-
prompt_tokens:
|
1129
|
-
completion_tokens:
|
1079
|
+
usage: import_zod5.z.object({
|
1080
|
+
prompt_tokens: import_zod5.z.number(),
|
1081
|
+
completion_tokens: import_zod5.z.number()
|
1130
1082
|
}).nullish()
|
1131
1083
|
});
|
1132
|
-
var createOpenAICompatibleCompletionChunkSchema = (errorSchema) =>
|
1133
|
-
|
1134
|
-
id:
|
1135
|
-
created:
|
1136
|
-
model:
|
1137
|
-
choices:
|
1138
|
-
|
1139
|
-
text:
|
1140
|
-
finish_reason:
|
1141
|
-
index:
|
1084
|
+
var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => import_zod5.z.union([
|
1085
|
+
import_zod5.z.object({
|
1086
|
+
id: import_zod5.z.string().nullish(),
|
1087
|
+
created: import_zod5.z.number().nullish(),
|
1088
|
+
model: import_zod5.z.string().nullish(),
|
1089
|
+
choices: import_zod5.z.array(
|
1090
|
+
import_zod5.z.object({
|
1091
|
+
text: import_zod5.z.string(),
|
1092
|
+
finish_reason: import_zod5.z.string().nullish(),
|
1093
|
+
index: import_zod5.z.number()
|
1142
1094
|
})
|
1143
1095
|
),
|
1144
|
-
usage:
|
1145
|
-
prompt_tokens:
|
1146
|
-
completion_tokens:
|
1096
|
+
usage: import_zod5.z.object({
|
1097
|
+
prompt_tokens: import_zod5.z.number(),
|
1098
|
+
completion_tokens: import_zod5.z.number()
|
1147
1099
|
}).nullish()
|
1148
1100
|
}),
|
1149
1101
|
errorSchema
|
1150
1102
|
]);
|
1151
1103
|
|
1152
1104
|
// src/openai-compatible-embedding-model.ts
|
1153
|
-
var
|
1154
|
-
var
|
1155
|
-
var
|
1105
|
+
var import_provider5 = require("@ai-sdk/provider");
|
1106
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
1107
|
+
var import_zod7 = require("zod");
|
1108
|
+
|
1109
|
+
// src/openai-compatible-embedding-options.ts
|
1110
|
+
var import_zod6 = require("zod");
|
1111
|
+
var openaiCompatibleEmbeddingProviderOptions = import_zod6.z.object({
|
1112
|
+
/**
|
1113
|
+
* The number of dimensions the resulting output embeddings should have.
|
1114
|
+
* Only supported in text-embedding-3 and later models.
|
1115
|
+
*/
|
1116
|
+
dimensions: import_zod6.z.number().optional(),
|
1117
|
+
/**
|
1118
|
+
* A unique identifier representing your end-user, which can help providers to
|
1119
|
+
* monitor and detect abuse.
|
1120
|
+
*/
|
1121
|
+
user: import_zod6.z.string().optional()
|
1122
|
+
});
|
1123
|
+
|
1124
|
+
// src/openai-compatible-embedding-model.ts
|
1156
1125
|
var OpenAICompatibleEmbeddingModel = class {
|
1157
|
-
constructor(modelId,
|
1158
|
-
this.specificationVersion = "
|
1126
|
+
constructor(modelId, config) {
|
1127
|
+
this.specificationVersion = "v2";
|
1159
1128
|
this.modelId = modelId;
|
1160
|
-
this.settings = settings;
|
1161
1129
|
this.config = config;
|
1162
1130
|
}
|
1163
1131
|
get provider() {
|
@@ -1171,37 +1139,57 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
1171
1139
|
var _a;
|
1172
1140
|
return (_a = this.config.supportsParallelCalls) != null ? _a : true;
|
1173
1141
|
}
|
1142
|
+
get providerOptionsName() {
|
1143
|
+
return this.config.provider.split(".")[0].trim();
|
1144
|
+
}
|
1174
1145
|
async doEmbed({
|
1175
1146
|
values,
|
1176
1147
|
headers,
|
1177
|
-
abortSignal
|
1148
|
+
abortSignal,
|
1149
|
+
providerOptions
|
1178
1150
|
}) {
|
1179
|
-
var _a;
|
1151
|
+
var _a, _b, _c;
|
1152
|
+
const compatibleOptions = Object.assign(
|
1153
|
+
(_a = await (0, import_provider_utils3.parseProviderOptions)({
|
1154
|
+
provider: "openai-compatible",
|
1155
|
+
providerOptions,
|
1156
|
+
schema: openaiCompatibleEmbeddingProviderOptions
|
1157
|
+
})) != null ? _a : {},
|
1158
|
+
(_b = await (0, import_provider_utils3.parseProviderOptions)({
|
1159
|
+
provider: this.providerOptionsName,
|
1160
|
+
providerOptions,
|
1161
|
+
schema: openaiCompatibleEmbeddingProviderOptions
|
1162
|
+
})) != null ? _b : {}
|
1163
|
+
);
|
1180
1164
|
if (values.length > this.maxEmbeddingsPerCall) {
|
1181
|
-
throw new
|
1165
|
+
throw new import_provider5.TooManyEmbeddingValuesForCallError({
|
1182
1166
|
provider: this.provider,
|
1183
1167
|
modelId: this.modelId,
|
1184
1168
|
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
1185
1169
|
values
|
1186
1170
|
});
|
1187
1171
|
}
|
1188
|
-
const {
|
1172
|
+
const {
|
1173
|
+
responseHeaders,
|
1174
|
+
value: response,
|
1175
|
+
rawValue
|
1176
|
+
} = await (0, import_provider_utils3.postJsonToApi)({
|
1189
1177
|
url: this.config.url({
|
1190
1178
|
path: "/embeddings",
|
1191
1179
|
modelId: this.modelId
|
1192
1180
|
}),
|
1193
|
-
headers: (0,
|
1181
|
+
headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), headers),
|
1194
1182
|
body: {
|
1195
1183
|
model: this.modelId,
|
1196
1184
|
input: values,
|
1197
1185
|
encoding_format: "float",
|
1198
|
-
dimensions:
|
1199
|
-
user:
|
1186
|
+
dimensions: compatibleOptions.dimensions,
|
1187
|
+
user: compatibleOptions.user
|
1200
1188
|
},
|
1201
|
-
failedResponseHandler: (0,
|
1202
|
-
(
|
1189
|
+
failedResponseHandler: (0, import_provider_utils3.createJsonErrorResponseHandler)(
|
1190
|
+
(_c = this.config.errorStructure) != null ? _c : defaultOpenAICompatibleErrorStructure
|
1203
1191
|
),
|
1204
|
-
successfulResponseHandler: (0,
|
1192
|
+
successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
|
1205
1193
|
openaiTextEmbeddingResponseSchema
|
1206
1194
|
),
|
1207
1195
|
abortSignal,
|
@@ -1210,18 +1198,18 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
1210
1198
|
return {
|
1211
1199
|
embeddings: response.data.map((item) => item.embedding),
|
1212
1200
|
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
1213
|
-
|
1201
|
+
response: { headers: responseHeaders, body: rawValue }
|
1214
1202
|
};
|
1215
1203
|
}
|
1216
1204
|
};
|
1217
|
-
var openaiTextEmbeddingResponseSchema =
|
1218
|
-
data:
|
1219
|
-
usage:
|
1205
|
+
var openaiTextEmbeddingResponseSchema = import_zod7.z.object({
|
1206
|
+
data: import_zod7.z.array(import_zod7.z.object({ embedding: import_zod7.z.array(import_zod7.z.number()) })),
|
1207
|
+
usage: import_zod7.z.object({ prompt_tokens: import_zod7.z.number() }).nullish()
|
1220
1208
|
});
|
1221
1209
|
|
1222
1210
|
// src/openai-compatible-image-model.ts
|
1223
|
-
var
|
1224
|
-
var
|
1211
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
1212
|
+
var import_zod8 = require("zod");
|
1225
1213
|
var OpenAICompatibleImageModel = class {
|
1226
1214
|
constructor(modelId, settings, config) {
|
1227
1215
|
this.modelId = modelId;
|
@@ -1259,12 +1247,12 @@ var OpenAICompatibleImageModel = class {
|
|
1259
1247
|
warnings.push({ type: "unsupported-setting", setting: "seed" });
|
1260
1248
|
}
|
1261
1249
|
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
1262
|
-
const { value: response, responseHeaders } = await (0,
|
1250
|
+
const { value: response, responseHeaders } = await (0, import_provider_utils4.postJsonToApi)({
|
1263
1251
|
url: this.config.url({
|
1264
1252
|
path: "/images/generations",
|
1265
1253
|
modelId: this.modelId
|
1266
1254
|
}),
|
1267
|
-
headers: (0,
|
1255
|
+
headers: (0, import_provider_utils4.combineHeaders)(this.config.headers(), headers),
|
1268
1256
|
body: {
|
1269
1257
|
model: this.modelId,
|
1270
1258
|
prompt,
|
@@ -1274,10 +1262,10 @@ var OpenAICompatibleImageModel = class {
|
|
1274
1262
|
response_format: "b64_json",
|
1275
1263
|
...this.settings.user ? { user: this.settings.user } : {}
|
1276
1264
|
},
|
1277
|
-
failedResponseHandler: (0,
|
1265
|
+
failedResponseHandler: (0, import_provider_utils4.createJsonErrorResponseHandler)(
|
1278
1266
|
(_e = this.config.errorStructure) != null ? _e : defaultOpenAICompatibleErrorStructure
|
1279
1267
|
),
|
1280
|
-
successfulResponseHandler: (0,
|
1268
|
+
successfulResponseHandler: (0, import_provider_utils4.createJsonResponseHandler)(
|
1281
1269
|
openaiCompatibleImageResponseSchema
|
1282
1270
|
),
|
1283
1271
|
abortSignal,
|
@@ -1294,14 +1282,14 @@ var OpenAICompatibleImageModel = class {
|
|
1294
1282
|
};
|
1295
1283
|
}
|
1296
1284
|
};
|
1297
|
-
var openaiCompatibleImageResponseSchema =
|
1298
|
-
data:
|
1285
|
+
var openaiCompatibleImageResponseSchema = import_zod8.z.object({
|
1286
|
+
data: import_zod8.z.array(import_zod8.z.object({ b64_json: import_zod8.z.string() }))
|
1299
1287
|
});
|
1300
1288
|
|
1301
1289
|
// src/openai-compatible-provider.ts
|
1302
|
-
var
|
1290
|
+
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
1303
1291
|
function createOpenAICompatible(options) {
|
1304
|
-
const baseURL = (0,
|
1292
|
+
const baseURL = (0, import_provider_utils5.withoutTrailingSlash)(options.baseURL);
|
1305
1293
|
const providerName = options.name;
|
1306
1294
|
const getHeaders = () => ({
|
1307
1295
|
...options.apiKey && { Authorization: `Bearer ${options.apiKey}` },
|
@@ -1319,27 +1307,24 @@ function createOpenAICompatible(options) {
|
|
1319
1307
|
headers: getHeaders,
|
1320
1308
|
fetch: options.fetch
|
1321
1309
|
});
|
1322
|
-
const createLanguageModel = (modelId
|
1323
|
-
const createChatModel = (modelId
|
1324
|
-
...getCommonModelConfig("chat"),
|
1325
|
-
defaultObjectGenerationMode: "tool"
|
1326
|
-
});
|
1327
|
-
const createCompletionModel = (modelId, settings = {}) => new OpenAICompatibleCompletionLanguageModel(
|
1310
|
+
const createLanguageModel = (modelId) => createChatModel(modelId);
|
1311
|
+
const createChatModel = (modelId) => new OpenAICompatibleChatLanguageModel(
|
1328
1312
|
modelId,
|
1329
|
-
|
1330
|
-
getCommonModelConfig("completion")
|
1313
|
+
getCommonModelConfig("chat")
|
1331
1314
|
);
|
1332
|
-
const
|
1315
|
+
const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(
|
1333
1316
|
modelId,
|
1334
|
-
|
1335
|
-
getCommonModelConfig("embedding")
|
1317
|
+
getCommonModelConfig("completion")
|
1336
1318
|
);
|
1319
|
+
const createEmbeddingModel = (modelId) => new OpenAICompatibleEmbeddingModel(modelId, {
|
1320
|
+
...getCommonModelConfig("embedding")
|
1321
|
+
});
|
1337
1322
|
const createImageModel = (modelId, settings = {}) => new OpenAICompatibleImageModel(
|
1338
1323
|
modelId,
|
1339
1324
|
settings,
|
1340
1325
|
getCommonModelConfig("image")
|
1341
1326
|
);
|
1342
|
-
const provider = (modelId
|
1327
|
+
const provider = (modelId) => createLanguageModel(modelId);
|
1343
1328
|
provider.languageModel = createLanguageModel;
|
1344
1329
|
provider.chatModel = createChatModel;
|
1345
1330
|
provider.completionModel = createCompletionModel;
|