@ai-sdk/openai-compatible 2.0.39 → 2.0.41
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 +12 -0
- package/README.md +2 -0
- package/dist/index.js +51 -23
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +51 -23
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/chat/openai-compatible-chat-language-model.ts +23 -10
- package/src/completion/openai-compatible-completion-language-model.ts +11 -3
- package/src/image/openai-compatible-image-model.ts +1 -4
- package/src/utils/to-camel-case.ts +19 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @ai-sdk/openai-compatible
|
|
2
2
|
|
|
3
|
+
## 2.0.41
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- d42076d: Add AI Gateway hint to provider READMEs
|
|
8
|
+
|
|
9
|
+
## 2.0.40
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 01c9c16: fix(openai-compatible): honor camelCase providerOptions key in chat and completion models
|
|
14
|
+
|
|
3
15
|
## 2.0.39
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -4,6 +4,8 @@ This package provides a foundation for implementing providers that expose an Ope
|
|
|
4
4
|
|
|
5
5
|
The primary [OpenAI provider](../openai/README.md) is more feature-rich, including OpenAI-specific experimental and legacy features. This package offers a lighter-weight alternative focused on core OpenAI-compatible functionality.
|
|
6
6
|
|
|
7
|
+
> **Deploying to Vercel?** With Vercel's AI Gateway you can access hundreds of models from any provider — no additional packages, API keys, or extra cost. [Get started with AI Gateway](https://vercel.com/ai-gateway).
|
|
8
|
+
|
|
7
9
|
## Setup
|
|
8
10
|
|
|
9
11
|
The provider is available in the `@ai-sdk/openai-compatible` module. You can install it with
|
package/dist/index.js
CHANGED
|
@@ -34,6 +34,18 @@ 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
|
+
|
|
37
49
|
// src/openai-compatible-error.ts
|
|
38
50
|
var import_v4 = require("zod/v4");
|
|
39
51
|
var openaiCompatibleErrorDataSchema = import_v4.z.object({
|
|
@@ -472,9 +484,14 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
472
484
|
provider: this.providerOptionsName,
|
|
473
485
|
providerOptions,
|
|
474
486
|
schema: openaiCompatibleLanguageModelChatOptions
|
|
475
|
-
})) != null ? _b : {}
|
|
487
|
+
})) != null ? _b : {},
|
|
488
|
+
(_c = await (0, import_provider_utils2.parseProviderOptions)({
|
|
489
|
+
provider: toCamelCase(this.providerOptionsName),
|
|
490
|
+
providerOptions,
|
|
491
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
492
|
+
})) != null ? _c : {}
|
|
476
493
|
);
|
|
477
|
-
const strictJsonSchema = (
|
|
494
|
+
const strictJsonSchema = (_d = compatibleOptions == null ? void 0 : compatibleOptions.strictJsonSchema) != null ? _d : true;
|
|
478
495
|
if (topK != null) {
|
|
479
496
|
warnings.push({ type: "unsupported", feature: "topK" });
|
|
480
497
|
}
|
|
@@ -493,7 +510,12 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
493
510
|
tools,
|
|
494
511
|
toolChoice
|
|
495
512
|
});
|
|
513
|
+
const metadataKey = resolveProviderOptionsKey(
|
|
514
|
+
this.providerOptionsName,
|
|
515
|
+
providerOptions
|
|
516
|
+
);
|
|
496
517
|
return {
|
|
518
|
+
metadataKey,
|
|
497
519
|
args: {
|
|
498
520
|
// model id:
|
|
499
521
|
model: this.modelId,
|
|
@@ -510,16 +532,17 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
510
532
|
json_schema: {
|
|
511
533
|
schema: responseFormat.schema,
|
|
512
534
|
strict: strictJsonSchema,
|
|
513
|
-
name: (
|
|
535
|
+
name: (_e = responseFormat.name) != null ? _e : "response",
|
|
514
536
|
description: responseFormat.description
|
|
515
537
|
}
|
|
516
538
|
} : { type: "json_object" } : void 0,
|
|
517
539
|
stop: stopSequences,
|
|
518
540
|
seed,
|
|
519
541
|
...Object.fromEntries(
|
|
520
|
-
Object.entries(
|
|
521
|
-
|
|
522
|
-
|
|
542
|
+
Object.entries({
|
|
543
|
+
...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
|
|
544
|
+
...providerOptions == null ? void 0 : providerOptions[toCamelCase(this.providerOptionsName)]
|
|
545
|
+
}).filter(
|
|
523
546
|
([key]) => !Object.keys(
|
|
524
547
|
openaiCompatibleLanguageModelChatOptions.shape
|
|
525
548
|
).includes(key)
|
|
@@ -538,7 +561,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
538
561
|
}
|
|
539
562
|
async doGenerate(options) {
|
|
540
563
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
541
|
-
const { args, warnings } = await this.getArgs({ ...options });
|
|
564
|
+
const { args, warnings, metadataKey } = await this.getArgs({ ...options });
|
|
542
565
|
const transformedBody = this.transformRequestBody(args);
|
|
543
566
|
const body = JSON.stringify(transformedBody);
|
|
544
567
|
const {
|
|
@@ -582,24 +605,24 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
582
605
|
input: toolCall.function.arguments,
|
|
583
606
|
...thoughtSignature ? {
|
|
584
607
|
providerMetadata: {
|
|
585
|
-
[
|
|
608
|
+
[metadataKey]: { thoughtSignature }
|
|
586
609
|
}
|
|
587
610
|
} : {}
|
|
588
611
|
});
|
|
589
612
|
}
|
|
590
613
|
}
|
|
591
614
|
const providerMetadata = {
|
|
592
|
-
[
|
|
615
|
+
[metadataKey]: {},
|
|
593
616
|
...await ((_f = (_e = this.config.metadataExtractor) == null ? void 0 : _e.extractMetadata) == null ? void 0 : _f.call(_e, {
|
|
594
617
|
parsedBody: rawResponse
|
|
595
618
|
}))
|
|
596
619
|
};
|
|
597
620
|
const completionTokenDetails = (_g = responseBody.usage) == null ? void 0 : _g.completion_tokens_details;
|
|
598
621
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
|
|
599
|
-
providerMetadata[
|
|
622
|
+
providerMetadata[metadataKey].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
|
|
600
623
|
}
|
|
601
624
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
|
|
602
|
-
providerMetadata[
|
|
625
|
+
providerMetadata[metadataKey].rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
|
|
603
626
|
}
|
|
604
627
|
return {
|
|
605
628
|
content,
|
|
@@ -620,7 +643,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
620
643
|
}
|
|
621
644
|
async doStream(options) {
|
|
622
645
|
var _a;
|
|
623
|
-
const { args, warnings } = await this.getArgs({ ...options });
|
|
646
|
+
const { args, warnings, metadataKey } = await this.getArgs({ ...options });
|
|
624
647
|
const body = this.transformRequestBody({
|
|
625
648
|
...args,
|
|
626
649
|
stream: true,
|
|
@@ -649,7 +672,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
649
672
|
};
|
|
650
673
|
let usage = void 0;
|
|
651
674
|
let isFirstChunk = true;
|
|
652
|
-
const providerOptionsName =
|
|
675
|
+
const providerOptionsName = metadataKey;
|
|
653
676
|
let isActiveReasoning = false;
|
|
654
677
|
let isActiveText = false;
|
|
655
678
|
return {
|
|
@@ -1183,13 +1206,20 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1183
1206
|
tools,
|
|
1184
1207
|
toolChoice
|
|
1185
1208
|
}) {
|
|
1186
|
-
var _a;
|
|
1209
|
+
var _a, _b;
|
|
1187
1210
|
const warnings = [];
|
|
1188
|
-
const completionOptions =
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1211
|
+
const completionOptions = Object.assign(
|
|
1212
|
+
(_a = await (0, import_provider_utils3.parseProviderOptions)({
|
|
1213
|
+
provider: this.providerOptionsName,
|
|
1214
|
+
providerOptions,
|
|
1215
|
+
schema: openaiCompatibleLanguageModelCompletionOptions
|
|
1216
|
+
})) != null ? _a : {},
|
|
1217
|
+
(_b = await (0, import_provider_utils3.parseProviderOptions)({
|
|
1218
|
+
provider: toCamelCase(this.providerOptionsName),
|
|
1219
|
+
providerOptions,
|
|
1220
|
+
schema: openaiCompatibleLanguageModelCompletionOptions
|
|
1221
|
+
})) != null ? _b : {}
|
|
1222
|
+
);
|
|
1193
1223
|
if (topK != null) {
|
|
1194
1224
|
warnings.push({ type: "unsupported", feature: "topK" });
|
|
1195
1225
|
}
|
|
@@ -1225,6 +1255,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1225
1255
|
presence_penalty: presencePenalty,
|
|
1226
1256
|
seed,
|
|
1227
1257
|
...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
|
|
1258
|
+
...providerOptions == null ? void 0 : providerOptions[toCamelCase(this.providerOptionsName)],
|
|
1228
1259
|
// prompt:
|
|
1229
1260
|
prompt: completionPrompt,
|
|
1230
1261
|
// stop sequences:
|
|
@@ -1657,15 +1688,12 @@ async function fileToBlob(file) {
|
|
|
1657
1688
|
const data = file.data instanceof Uint8Array ? file.data : (0, import_provider_utils5.convertBase64ToUint8Array)(file.data);
|
|
1658
1689
|
return new Blob([data], { type: file.mediaType });
|
|
1659
1690
|
}
|
|
1660
|
-
function toCamelCase(str) {
|
|
1661
|
-
return str.replace(/[_-]([a-z])/g, (g) => g[1].toUpperCase());
|
|
1662
|
-
}
|
|
1663
1691
|
|
|
1664
1692
|
// src/openai-compatible-provider.ts
|
|
1665
1693
|
var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
|
1666
1694
|
|
|
1667
1695
|
// src/version.ts
|
|
1668
|
-
var VERSION = true ? "2.0.
|
|
1696
|
+
var VERSION = true ? "2.0.41" : "0.0.0-test";
|
|
1669
1697
|
|
|
1670
1698
|
// src/openai-compatible-provider.ts
|
|
1671
1699
|
function createOpenAICompatible(options) {
|