@ai-sdk/openai-compatible 3.0.0-beta.2 → 3.0.0-beta.21
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 +153 -8
- package/README.md +2 -0
- package/dist/index.d.mts +27 -27
- package/dist/index.d.ts +27 -27
- package/dist/index.js +110 -39
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +110 -37
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +10 -10
- package/dist/internal/index.d.ts +10 -10
- package/dist/internal/index.js +8 -3
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +7 -1
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +3 -5
- package/src/chat/convert-openai-compatible-chat-usage.ts +2 -2
- package/src/chat/convert-to-openai-compatible-chat-messages.ts +11 -4
- package/src/chat/map-openai-compatible-finish-reason.ts +2 -2
- package/src/chat/openai-compatible-api-types.ts +2 -4
- package/src/chat/openai-compatible-chat-language-model.ts +69 -36
- package/src/chat/openai-compatible-metadata-extractor.ts +3 -3
- package/src/chat/openai-compatible-prepare-tools.ts +6 -6
- package/src/completion/convert-openai-compatible-completion-usage.ts +2 -2
- package/src/completion/convert-to-openai-compatible-completion-prompt.ts +2 -2
- package/src/completion/map-openai-compatible-finish-reason.ts +2 -2
- package/src/completion/openai-compatible-completion-language-model.ts +41 -25
- package/src/embedding/openai-compatible-embedding-model.ts +18 -9
- package/src/image/openai-compatible-image-model.ts +22 -17
- package/src/openai-compatible-provider.ts +13 -13
- package/src/utils/to-camel-case.ts +43 -0
package/dist/index.js
CHANGED
|
@@ -18,8 +18,8 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
20
|
// src/index.ts
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
23
|
OpenAICompatibleChatLanguageModel: () => OpenAICompatibleChatLanguageModel,
|
|
24
24
|
OpenAICompatibleCompletionLanguageModel: () => OpenAICompatibleCompletionLanguageModel,
|
|
25
25
|
OpenAICompatibleEmbeddingModel: () => OpenAICompatibleEmbeddingModel,
|
|
@@ -27,13 +27,39 @@ __export(src_exports, {
|
|
|
27
27
|
VERSION: () => VERSION,
|
|
28
28
|
createOpenAICompatible: () => createOpenAICompatible
|
|
29
29
|
});
|
|
30
|
-
module.exports = __toCommonJS(
|
|
30
|
+
module.exports = __toCommonJS(index_exports);
|
|
31
31
|
|
|
32
32
|
// src/chat/openai-compatible-chat-language-model.ts
|
|
33
33
|
var import_provider3 = require("@ai-sdk/provider");
|
|
34
34
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
35
35
|
var import_v43 = require("zod/v4");
|
|
36
36
|
|
|
37
|
+
// src/utils/to-camel-case.ts
|
|
38
|
+
function toCamelCase(str) {
|
|
39
|
+
return str.replace(/[_-]([a-z])/g, (g) => g[1].toUpperCase());
|
|
40
|
+
}
|
|
41
|
+
function resolveProviderOptionsKey(rawName, providerOptions) {
|
|
42
|
+
const camelName = toCamelCase(rawName);
|
|
43
|
+
if (camelName !== rawName && (providerOptions == null ? void 0 : providerOptions[camelName]) != null) {
|
|
44
|
+
return camelName;
|
|
45
|
+
}
|
|
46
|
+
return rawName;
|
|
47
|
+
}
|
|
48
|
+
function warnIfDeprecatedProviderOptionsKey({
|
|
49
|
+
rawName,
|
|
50
|
+
providerOptions,
|
|
51
|
+
warnings
|
|
52
|
+
}) {
|
|
53
|
+
const camelName = toCamelCase(rawName);
|
|
54
|
+
if (camelName !== rawName && (providerOptions == null ? void 0 : providerOptions[rawName]) != null) {
|
|
55
|
+
warnings.push({
|
|
56
|
+
type: "deprecated",
|
|
57
|
+
setting: `providerOptions key '${rawName}'`,
|
|
58
|
+
message: `Use '${camelName}' instead.`
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
37
63
|
// src/openai-compatible-error.ts
|
|
38
64
|
var import_v4 = require("zod/v4");
|
|
39
65
|
var openaiCompatibleErrorDataSchema = import_v4.z.object({
|
|
@@ -138,6 +164,11 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
138
164
|
return { type: "text", text: part.text, ...partMetadata };
|
|
139
165
|
}
|
|
140
166
|
case "file": {
|
|
167
|
+
if ((0, import_provider_utils.isProviderReference)(part.data)) {
|
|
168
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
169
|
+
functionality: "file parts with provider references"
|
|
170
|
+
});
|
|
171
|
+
}
|
|
141
172
|
if (part.mediaType.startsWith("image/")) {
|
|
142
173
|
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
143
174
|
return {
|
|
@@ -408,7 +439,7 @@ function prepareTools({
|
|
|
408
439
|
var OpenAICompatibleChatLanguageModel = class {
|
|
409
440
|
// type inferred via constructor
|
|
410
441
|
constructor(modelId, config) {
|
|
411
|
-
this.specificationVersion = "
|
|
442
|
+
this.specificationVersion = "v4";
|
|
412
443
|
var _a, _b;
|
|
413
444
|
this.modelId = modelId;
|
|
414
445
|
this.config = config;
|
|
@@ -441,6 +472,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
441
472
|
topK,
|
|
442
473
|
frequencyPenalty,
|
|
443
474
|
presencePenalty,
|
|
475
|
+
reasoning,
|
|
444
476
|
providerOptions,
|
|
445
477
|
stopSequences,
|
|
446
478
|
responseFormat,
|
|
@@ -448,7 +480,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
448
480
|
toolChoice,
|
|
449
481
|
tools
|
|
450
482
|
}) {
|
|
451
|
-
var _a, _b, _c, _d, _e;
|
|
483
|
+
var _a, _b, _c, _d, _e, _f;
|
|
452
484
|
const warnings = [];
|
|
453
485
|
const deprecatedOptions = await (0, import_provider_utils2.parseProviderOptions)({
|
|
454
486
|
provider: "openai-compatible",
|
|
@@ -457,10 +489,16 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
457
489
|
});
|
|
458
490
|
if (deprecatedOptions != null) {
|
|
459
491
|
warnings.push({
|
|
460
|
-
type: "
|
|
461
|
-
|
|
492
|
+
type: "deprecated",
|
|
493
|
+
setting: "providerOptions key 'openai-compatible'",
|
|
494
|
+
message: "Use 'openaiCompatible' instead."
|
|
462
495
|
});
|
|
463
496
|
}
|
|
497
|
+
warnIfDeprecatedProviderOptionsKey({
|
|
498
|
+
rawName: this.providerOptionsName,
|
|
499
|
+
providerOptions,
|
|
500
|
+
warnings
|
|
501
|
+
});
|
|
464
502
|
const compatibleOptions = Object.assign(
|
|
465
503
|
deprecatedOptions != null ? deprecatedOptions : {},
|
|
466
504
|
(_a = await (0, import_provider_utils2.parseProviderOptions)({
|
|
@@ -472,9 +510,14 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
472
510
|
provider: this.providerOptionsName,
|
|
473
511
|
providerOptions,
|
|
474
512
|
schema: openaiCompatibleLanguageModelChatOptions
|
|
475
|
-
})) != null ? _b : {}
|
|
513
|
+
})) != null ? _b : {},
|
|
514
|
+
(_c = await (0, import_provider_utils2.parseProviderOptions)({
|
|
515
|
+
provider: toCamelCase(this.providerOptionsName),
|
|
516
|
+
providerOptions,
|
|
517
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
518
|
+
})) != null ? _c : {}
|
|
476
519
|
);
|
|
477
|
-
const strictJsonSchema = (
|
|
520
|
+
const strictJsonSchema = (_d = compatibleOptions == null ? void 0 : compatibleOptions.strictJsonSchema) != null ? _d : true;
|
|
478
521
|
if (topK != null) {
|
|
479
522
|
warnings.push({ type: "unsupported", feature: "topK" });
|
|
480
523
|
}
|
|
@@ -493,7 +536,12 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
493
536
|
tools,
|
|
494
537
|
toolChoice
|
|
495
538
|
});
|
|
539
|
+
const metadataKey = resolveProviderOptionsKey(
|
|
540
|
+
this.providerOptionsName,
|
|
541
|
+
providerOptions
|
|
542
|
+
);
|
|
496
543
|
return {
|
|
544
|
+
metadataKey,
|
|
497
545
|
args: {
|
|
498
546
|
// model id:
|
|
499
547
|
model: this.modelId,
|
|
@@ -510,22 +558,23 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
510
558
|
json_schema: {
|
|
511
559
|
schema: responseFormat.schema,
|
|
512
560
|
strict: strictJsonSchema,
|
|
513
|
-
name: (
|
|
561
|
+
name: (_e = responseFormat.name) != null ? _e : "response",
|
|
514
562
|
description: responseFormat.description
|
|
515
563
|
}
|
|
516
564
|
} : { type: "json_object" } : void 0,
|
|
517
565
|
stop: stopSequences,
|
|
518
566
|
seed,
|
|
519
567
|
...Object.fromEntries(
|
|
520
|
-
Object.entries(
|
|
521
|
-
|
|
522
|
-
|
|
568
|
+
Object.entries({
|
|
569
|
+
...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
|
|
570
|
+
...providerOptions == null ? void 0 : providerOptions[toCamelCase(this.providerOptionsName)]
|
|
571
|
+
}).filter(
|
|
523
572
|
([key]) => !Object.keys(
|
|
524
573
|
openaiCompatibleLanguageModelChatOptions.shape
|
|
525
574
|
).includes(key)
|
|
526
575
|
)
|
|
527
576
|
),
|
|
528
|
-
reasoning_effort: compatibleOptions.reasoningEffort,
|
|
577
|
+
reasoning_effort: (_f = compatibleOptions.reasoningEffort) != null ? _f : (0, import_provider_utils2.isCustomReasoning)(reasoning) && reasoning !== "none" ? reasoning : void 0,
|
|
529
578
|
verbosity: compatibleOptions.textVerbosity,
|
|
530
579
|
// messages:
|
|
531
580
|
messages: convertToOpenAICompatibleChatMessages(prompt),
|
|
@@ -538,7 +587,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
538
587
|
}
|
|
539
588
|
async doGenerate(options) {
|
|
540
589
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
541
|
-
const { args, warnings } = await this.getArgs({ ...options });
|
|
590
|
+
const { args, warnings, metadataKey } = await this.getArgs({ ...options });
|
|
542
591
|
const transformedBody = this.transformRequestBody(args);
|
|
543
592
|
const body = JSON.stringify(transformedBody);
|
|
544
593
|
const {
|
|
@@ -582,24 +631,24 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
582
631
|
input: toolCall.function.arguments,
|
|
583
632
|
...thoughtSignature ? {
|
|
584
633
|
providerMetadata: {
|
|
585
|
-
[
|
|
634
|
+
[metadataKey]: { thoughtSignature }
|
|
586
635
|
}
|
|
587
636
|
} : {}
|
|
588
637
|
});
|
|
589
638
|
}
|
|
590
639
|
}
|
|
591
640
|
const providerMetadata = {
|
|
592
|
-
[
|
|
641
|
+
[metadataKey]: {},
|
|
593
642
|
...await ((_f = (_e = this.config.metadataExtractor) == null ? void 0 : _e.extractMetadata) == null ? void 0 : _f.call(_e, {
|
|
594
643
|
parsedBody: rawResponse
|
|
595
644
|
}))
|
|
596
645
|
};
|
|
597
646
|
const completionTokenDetails = (_g = responseBody.usage) == null ? void 0 : _g.completion_tokens_details;
|
|
598
647
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
|
|
599
|
-
providerMetadata[
|
|
648
|
+
providerMetadata[metadataKey].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
|
|
600
649
|
}
|
|
601
650
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
|
|
602
|
-
providerMetadata[
|
|
651
|
+
providerMetadata[metadataKey].rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
|
|
603
652
|
}
|
|
604
653
|
return {
|
|
605
654
|
content,
|
|
@@ -620,7 +669,9 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
620
669
|
}
|
|
621
670
|
async doStream(options) {
|
|
622
671
|
var _a;
|
|
623
|
-
const { args, warnings } = await this.getArgs({
|
|
672
|
+
const { args, warnings, metadataKey } = await this.getArgs({
|
|
673
|
+
...options
|
|
674
|
+
});
|
|
624
675
|
const body = this.transformRequestBody({
|
|
625
676
|
...args,
|
|
626
677
|
stream: true,
|
|
@@ -649,7 +700,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
649
700
|
};
|
|
650
701
|
let usage = void 0;
|
|
651
702
|
let isFirstChunk = true;
|
|
652
|
-
const providerOptionsName =
|
|
703
|
+
const providerOptionsName = metadataKey;
|
|
653
704
|
let isActiveReasoning = false;
|
|
654
705
|
let isActiveText = false;
|
|
655
706
|
return {
|
|
@@ -1148,7 +1199,7 @@ var openaiCompatibleLanguageModelCompletionOptions = import_v44.z.object({
|
|
|
1148
1199
|
var OpenAICompatibleCompletionLanguageModel = class {
|
|
1149
1200
|
// type inferred via constructor
|
|
1150
1201
|
constructor(modelId, config) {
|
|
1151
|
-
this.specificationVersion = "
|
|
1202
|
+
this.specificationVersion = "v4";
|
|
1152
1203
|
var _a;
|
|
1153
1204
|
this.modelId = modelId;
|
|
1154
1205
|
this.config = config;
|
|
@@ -1183,13 +1234,25 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1183
1234
|
tools,
|
|
1184
1235
|
toolChoice
|
|
1185
1236
|
}) {
|
|
1186
|
-
var _a;
|
|
1237
|
+
var _a, _b;
|
|
1187
1238
|
const warnings = [];
|
|
1188
|
-
|
|
1189
|
-
|
|
1239
|
+
warnIfDeprecatedProviderOptionsKey({
|
|
1240
|
+
rawName: this.providerOptionsName,
|
|
1190
1241
|
providerOptions,
|
|
1191
|
-
|
|
1192
|
-
})
|
|
1242
|
+
warnings
|
|
1243
|
+
});
|
|
1244
|
+
const completionOptions = Object.assign(
|
|
1245
|
+
(_a = await (0, import_provider_utils3.parseProviderOptions)({
|
|
1246
|
+
provider: this.providerOptionsName,
|
|
1247
|
+
providerOptions,
|
|
1248
|
+
schema: openaiCompatibleLanguageModelCompletionOptions
|
|
1249
|
+
})) != null ? _a : {},
|
|
1250
|
+
(_b = await (0, import_provider_utils3.parseProviderOptions)({
|
|
1251
|
+
provider: toCamelCase(this.providerOptionsName),
|
|
1252
|
+
providerOptions,
|
|
1253
|
+
schema: openaiCompatibleLanguageModelCompletionOptions
|
|
1254
|
+
})) != null ? _b : {}
|
|
1255
|
+
);
|
|
1193
1256
|
if (topK != null) {
|
|
1194
1257
|
warnings.push({ type: "unsupported", feature: "topK" });
|
|
1195
1258
|
}
|
|
@@ -1225,6 +1288,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1225
1288
|
presence_penalty: presencePenalty,
|
|
1226
1289
|
seed,
|
|
1227
1290
|
...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
|
|
1291
|
+
...providerOptions == null ? void 0 : providerOptions[toCamelCase(this.providerOptionsName)],
|
|
1228
1292
|
// prompt:
|
|
1229
1293
|
prompt: completionPrompt,
|
|
1230
1294
|
// stop sequences:
|
|
@@ -1427,7 +1491,7 @@ var openaiCompatibleEmbeddingModelOptions = import_v46.z.object({
|
|
|
1427
1491
|
// src/embedding/openai-compatible-embedding-model.ts
|
|
1428
1492
|
var OpenAICompatibleEmbeddingModel = class {
|
|
1429
1493
|
constructor(modelId, config) {
|
|
1430
|
-
this.specificationVersion = "
|
|
1494
|
+
this.specificationVersion = "v4";
|
|
1431
1495
|
this.modelId = modelId;
|
|
1432
1496
|
this.config = config;
|
|
1433
1497
|
}
|
|
@@ -1460,10 +1524,16 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
1460
1524
|
});
|
|
1461
1525
|
if (deprecatedOptions != null) {
|
|
1462
1526
|
warnings.push({
|
|
1463
|
-
type: "
|
|
1464
|
-
|
|
1527
|
+
type: "deprecated",
|
|
1528
|
+
setting: "providerOptions key 'openai-compatible'",
|
|
1529
|
+
message: "Use 'openaiCompatible' instead."
|
|
1465
1530
|
});
|
|
1466
1531
|
}
|
|
1532
|
+
warnIfDeprecatedProviderOptionsKey({
|
|
1533
|
+
rawName: this.providerOptionsName,
|
|
1534
|
+
providerOptions,
|
|
1535
|
+
warnings
|
|
1536
|
+
});
|
|
1467
1537
|
const compatibleOptions = Object.assign(
|
|
1468
1538
|
deprecatedOptions != null ? deprecatedOptions : {},
|
|
1469
1539
|
(_a = await (0, import_provider_utils4.parseProviderOptions)({
|
|
@@ -1533,7 +1603,7 @@ var OpenAICompatibleImageModel = class {
|
|
|
1533
1603
|
constructor(modelId, config) {
|
|
1534
1604
|
this.modelId = modelId;
|
|
1535
1605
|
this.config = config;
|
|
1536
|
-
this.specificationVersion = "
|
|
1606
|
+
this.specificationVersion = "v4";
|
|
1537
1607
|
this.maxImagesPerCall = 10;
|
|
1538
1608
|
}
|
|
1539
1609
|
get provider() {
|
|
@@ -1545,8 +1615,12 @@ var OpenAICompatibleImageModel = class {
|
|
|
1545
1615
|
get providerOptionsKey() {
|
|
1546
1616
|
return this.config.provider.split(".")[0].trim();
|
|
1547
1617
|
}
|
|
1548
|
-
|
|
1549
|
-
|
|
1618
|
+
getArgs(providerOptions, warnings) {
|
|
1619
|
+
warnIfDeprecatedProviderOptionsKey({
|
|
1620
|
+
rawName: this.providerOptionsKey,
|
|
1621
|
+
providerOptions,
|
|
1622
|
+
warnings
|
|
1623
|
+
});
|
|
1550
1624
|
return {
|
|
1551
1625
|
...providerOptions[this.providerOptionsKey],
|
|
1552
1626
|
...providerOptions[toCamelCase(this.providerOptionsKey)]
|
|
@@ -1577,7 +1651,7 @@ var OpenAICompatibleImageModel = class {
|
|
|
1577
1651
|
warnings.push({ type: "unsupported", feature: "seed" });
|
|
1578
1652
|
}
|
|
1579
1653
|
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);
|
|
1654
|
+
const args = this.getArgs(providerOptions, warnings);
|
|
1581
1655
|
if (files != null && files.length > 0) {
|
|
1582
1656
|
const { value: response2, responseHeaders: responseHeaders2 } = await (0, import_provider_utils5.postFormDataToApi)({
|
|
1583
1657
|
url: this.config.url({
|
|
@@ -1657,15 +1731,12 @@ async function fileToBlob(file) {
|
|
|
1657
1731
|
const data = file.data instanceof Uint8Array ? file.data : (0, import_provider_utils5.convertBase64ToUint8Array)(file.data);
|
|
1658
1732
|
return new Blob([data], { type: file.mediaType });
|
|
1659
1733
|
}
|
|
1660
|
-
function toCamelCase(str) {
|
|
1661
|
-
return str.replace(/[_-]([a-z])/g, (g) => g[1].toUpperCase());
|
|
1662
|
-
}
|
|
1663
1734
|
|
|
1664
1735
|
// src/openai-compatible-provider.ts
|
|
1665
1736
|
var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
|
1666
1737
|
|
|
1667
1738
|
// src/version.ts
|
|
1668
|
-
var VERSION = true ? "3.0.0-beta.
|
|
1739
|
+
var VERSION = true ? "3.0.0-beta.21" : "0.0.0-test";
|
|
1669
1740
|
|
|
1670
1741
|
// src/openai-compatible-provider.ts
|
|
1671
1742
|
function createOpenAICompatible(options) {
|
|
@@ -1705,7 +1776,7 @@ function createOpenAICompatible(options) {
|
|
|
1705
1776
|
});
|
|
1706
1777
|
const createImageModel = (modelId) => new OpenAICompatibleImageModel(modelId, getCommonModelConfig("image"));
|
|
1707
1778
|
const provider = (modelId) => createLanguageModel(modelId);
|
|
1708
|
-
provider.specificationVersion = "
|
|
1779
|
+
provider.specificationVersion = "v4";
|
|
1709
1780
|
provider.languageModel = createLanguageModel;
|
|
1710
1781
|
provider.chatModel = createChatModel;
|
|
1711
1782
|
provider.completionModel = createCompletionModel;
|