@ai-sdk/openai-compatible 1.0.0-alpha.9 → 1.0.0-beta.10
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 +142 -0
- package/dist/index.d.mts +13 -86
- package/dist/index.d.ts +13 -86
- package/dist/index.js +205 -145
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +116 -56
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +2 -2
- package/dist/internal/index.d.ts +2 -2
- package/dist/internal/index.js +15 -2
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +15 -2
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.mjs
CHANGED
@@ -12,7 +12,7 @@ import {
|
|
12
12
|
parseProviderOptions,
|
13
13
|
postJsonToApi
|
14
14
|
} from "@ai-sdk/provider-utils";
|
15
|
-
import { z as z3 } from "zod";
|
15
|
+
import { z as z3 } from "zod/v4";
|
16
16
|
|
17
17
|
// src/convert-to-openai-compatible-chat-messages.ts
|
18
18
|
import {
|
@@ -86,7 +86,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
86
86
|
type: "function",
|
87
87
|
function: {
|
88
88
|
name: part.toolName,
|
89
|
-
arguments: JSON.stringify(part.
|
89
|
+
arguments: JSON.stringify(part.input)
|
90
90
|
},
|
91
91
|
...partMetadata
|
92
92
|
});
|
@@ -104,11 +104,24 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
104
104
|
}
|
105
105
|
case "tool": {
|
106
106
|
for (const toolResponse of content) {
|
107
|
+
const output = toolResponse.output;
|
108
|
+
let contentValue;
|
109
|
+
switch (output.type) {
|
110
|
+
case "text":
|
111
|
+
case "error-text":
|
112
|
+
contentValue = output.value;
|
113
|
+
break;
|
114
|
+
case "content":
|
115
|
+
case "json":
|
116
|
+
case "error-json":
|
117
|
+
contentValue = JSON.stringify(output.value);
|
118
|
+
break;
|
119
|
+
}
|
107
120
|
const toolResponseMetadata = getOpenAIMetadata(toolResponse);
|
108
121
|
messages.push({
|
109
122
|
role: "tool",
|
110
123
|
tool_call_id: toolResponse.toolCallId,
|
111
|
-
content:
|
124
|
+
content: contentValue,
|
112
125
|
...toolResponseMetadata
|
113
126
|
});
|
114
127
|
}
|
@@ -154,7 +167,7 @@ function mapOpenAICompatibleFinishReason(finishReason) {
|
|
154
167
|
}
|
155
168
|
|
156
169
|
// src/openai-compatible-chat-options.ts
|
157
|
-
import { z } from "zod";
|
170
|
+
import { z } from "zod/v4";
|
158
171
|
var openaiCompatibleProviderOptions = z.object({
|
159
172
|
/**
|
160
173
|
* A unique identifier representing your end-user, which can help the provider to
|
@@ -164,11 +177,11 @@ var openaiCompatibleProviderOptions = z.object({
|
|
164
177
|
/**
|
165
178
|
* Reasoning effort for reasoning models. Defaults to `medium`.
|
166
179
|
*/
|
167
|
-
reasoningEffort: z.
|
180
|
+
reasoningEffort: z.string().optional()
|
168
181
|
});
|
169
182
|
|
170
183
|
// src/openai-compatible-error.ts
|
171
|
-
import { z as z2 } from "zod";
|
184
|
+
import { z as z2 } from "zod/v4";
|
172
185
|
var openaiCompatibleErrorDataSchema = z2.object({
|
173
186
|
error: z2.object({
|
174
187
|
message: z2.string(),
|
@@ -208,7 +221,7 @@ function prepareTools({
|
|
208
221
|
function: {
|
209
222
|
name: tool.name,
|
210
223
|
description: tool.description,
|
211
|
-
parameters: tool.
|
224
|
+
parameters: tool.inputSchema
|
212
225
|
}
|
213
226
|
});
|
214
227
|
}
|
@@ -384,10 +397,9 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
384
397
|
for (const toolCall of choice.message.tool_calls) {
|
385
398
|
content.push({
|
386
399
|
type: "tool-call",
|
387
|
-
toolCallType: "function",
|
388
400
|
toolCallId: (_a = toolCall.id) != null ? _a : generateId(),
|
389
401
|
toolName: toolCall.function.name,
|
390
|
-
|
402
|
+
input: toolCall.function.arguments
|
391
403
|
});
|
392
404
|
}
|
393
405
|
}
|
@@ -465,6 +477,8 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
465
477
|
};
|
466
478
|
let isFirstChunk = true;
|
467
479
|
const providerOptionsName = this.providerOptionsName;
|
480
|
+
let isActiveReasoning = false;
|
481
|
+
let isActiveText = false;
|
468
482
|
return {
|
469
483
|
stream: response.pipeThrough(
|
470
484
|
new TransformStream({
|
@@ -474,6 +488,9 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
474
488
|
// TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
|
475
489
|
transform(chunk, controller) {
|
476
490
|
var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
491
|
+
if (options.includeRawChunks) {
|
492
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
493
|
+
}
|
477
494
|
if (!chunk.success) {
|
478
495
|
finishReason = "error";
|
479
496
|
controller.enqueue({ type: "error", error: chunk.error });
|
@@ -528,27 +545,34 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
528
545
|
}
|
529
546
|
const delta = choice.delta;
|
530
547
|
if (delta.reasoning_content != null) {
|
548
|
+
if (!isActiveReasoning) {
|
549
|
+
controller.enqueue({
|
550
|
+
type: "reasoning-start",
|
551
|
+
id: "reasoning-0"
|
552
|
+
});
|
553
|
+
isActiveReasoning = true;
|
554
|
+
}
|
531
555
|
controller.enqueue({
|
532
|
-
type: "reasoning",
|
533
|
-
|
556
|
+
type: "reasoning-delta",
|
557
|
+
id: "reasoning-0",
|
558
|
+
delta: delta.reasoning_content
|
534
559
|
});
|
535
560
|
}
|
536
561
|
if (delta.content != null) {
|
562
|
+
if (!isActiveText) {
|
563
|
+
controller.enqueue({ type: "text-start", id: "txt-0" });
|
564
|
+
isActiveText = true;
|
565
|
+
}
|
537
566
|
controller.enqueue({
|
538
|
-
type: "text",
|
539
|
-
|
567
|
+
type: "text-delta",
|
568
|
+
id: "txt-0",
|
569
|
+
delta: delta.content
|
540
570
|
});
|
541
571
|
}
|
542
572
|
if (delta.tool_calls != null) {
|
543
573
|
for (const toolCallDelta of delta.tool_calls) {
|
544
574
|
const index = toolCallDelta.index;
|
545
575
|
if (toolCalls[index] == null) {
|
546
|
-
if (toolCallDelta.type !== "function") {
|
547
|
-
throw new InvalidResponseDataError({
|
548
|
-
data: toolCallDelta,
|
549
|
-
message: `Expected 'function' type.`
|
550
|
-
});
|
551
|
-
}
|
552
576
|
if (toolCallDelta.id == null) {
|
553
577
|
throw new InvalidResponseDataError({
|
554
578
|
data: toolCallDelta,
|
@@ -561,6 +585,11 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
561
585
|
message: `Expected 'function.name' to be a string.`
|
562
586
|
});
|
563
587
|
}
|
588
|
+
controller.enqueue({
|
589
|
+
type: "tool-input-start",
|
590
|
+
id: toolCallDelta.id,
|
591
|
+
toolName: toolCallDelta.function.name
|
592
|
+
});
|
564
593
|
toolCalls[index] = {
|
565
594
|
id: toolCallDelta.id,
|
566
595
|
type: "function",
|
@@ -574,20 +603,21 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
574
603
|
if (((_c = toolCall2.function) == null ? void 0 : _c.name) != null && ((_d = toolCall2.function) == null ? void 0 : _d.arguments) != null) {
|
575
604
|
if (toolCall2.function.arguments.length > 0) {
|
576
605
|
controller.enqueue({
|
577
|
-
type: "tool-
|
578
|
-
|
579
|
-
|
580
|
-
toolName: toolCall2.function.name,
|
581
|
-
argsTextDelta: toolCall2.function.arguments
|
606
|
+
type: "tool-input-start",
|
607
|
+
id: toolCall2.id,
|
608
|
+
toolName: toolCall2.function.name
|
582
609
|
});
|
583
610
|
}
|
584
611
|
if (isParsableJson(toolCall2.function.arguments)) {
|
612
|
+
controller.enqueue({
|
613
|
+
type: "tool-input-end",
|
614
|
+
id: toolCall2.id
|
615
|
+
});
|
585
616
|
controller.enqueue({
|
586
617
|
type: "tool-call",
|
587
|
-
toolCallType: "function",
|
588
618
|
toolCallId: (_e = toolCall2.id) != null ? _e : generateId(),
|
589
619
|
toolName: toolCall2.function.name,
|
590
|
-
|
620
|
+
input: toolCall2.function.arguments
|
591
621
|
});
|
592
622
|
toolCall2.hasFinished = true;
|
593
623
|
}
|
@@ -602,19 +632,20 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
602
632
|
toolCall.function.arguments += (_h = (_g = toolCallDelta.function) == null ? void 0 : _g.arguments) != null ? _h : "";
|
603
633
|
}
|
604
634
|
controller.enqueue({
|
605
|
-
type: "tool-
|
606
|
-
|
607
|
-
|
608
|
-
toolName: toolCall.function.name,
|
609
|
-
argsTextDelta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
|
635
|
+
type: "tool-input-delta",
|
636
|
+
id: toolCall.id,
|
637
|
+
delta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
|
610
638
|
});
|
611
639
|
if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && isParsableJson(toolCall.function.arguments)) {
|
640
|
+
controller.enqueue({
|
641
|
+
type: "tool-input-end",
|
642
|
+
id: toolCall.id
|
643
|
+
});
|
612
644
|
controller.enqueue({
|
613
645
|
type: "tool-call",
|
614
|
-
toolCallType: "function",
|
615
646
|
toolCallId: (_l = toolCall.id) != null ? _l : generateId(),
|
616
647
|
toolName: toolCall.function.name,
|
617
|
-
|
648
|
+
input: toolCall.function.arguments
|
618
649
|
});
|
619
650
|
toolCall.hasFinished = true;
|
620
651
|
}
|
@@ -622,7 +653,27 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
622
653
|
}
|
623
654
|
},
|
624
655
|
flush(controller) {
|
625
|
-
var _a2, _b, _c, _d, _e;
|
656
|
+
var _a2, _b, _c, _d, _e, _f;
|
657
|
+
if (isActiveReasoning) {
|
658
|
+
controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
|
659
|
+
}
|
660
|
+
if (isActiveText) {
|
661
|
+
controller.enqueue({ type: "text-end", id: "txt-0" });
|
662
|
+
}
|
663
|
+
for (const toolCall of toolCalls.filter(
|
664
|
+
(toolCall2) => !toolCall2.hasFinished
|
665
|
+
)) {
|
666
|
+
controller.enqueue({
|
667
|
+
type: "tool-input-end",
|
668
|
+
id: toolCall.id
|
669
|
+
});
|
670
|
+
controller.enqueue({
|
671
|
+
type: "tool-call",
|
672
|
+
toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
|
673
|
+
toolName: toolCall.function.name,
|
674
|
+
input: toolCall.function.arguments
|
675
|
+
});
|
676
|
+
}
|
626
677
|
const providerMetadata = {
|
627
678
|
[providerOptionsName]: {},
|
628
679
|
...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
|
@@ -637,11 +688,11 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
637
688
|
type: "finish",
|
638
689
|
finishReason,
|
639
690
|
usage: {
|
640
|
-
inputTokens: (
|
641
|
-
outputTokens: (
|
642
|
-
totalTokens: (
|
643
|
-
reasoningTokens: (
|
644
|
-
cachedInputTokens: (
|
691
|
+
inputTokens: (_b = usage.promptTokens) != null ? _b : void 0,
|
692
|
+
outputTokens: (_c = usage.completionTokens) != null ? _c : void 0,
|
693
|
+
totalTokens: (_d = usage.totalTokens) != null ? _d : void 0,
|
694
|
+
reasoningTokens: (_e = usage.completionTokensDetails.reasoningTokens) != null ? _e : void 0,
|
695
|
+
cachedInputTokens: (_f = usage.promptTokensDetails.cachedTokens) != null ? _f : void 0
|
645
696
|
},
|
646
697
|
providerMetadata
|
647
698
|
});
|
@@ -679,7 +730,6 @@ var OpenAICompatibleChatResponseSchema = z3.object({
|
|
679
730
|
tool_calls: z3.array(
|
680
731
|
z3.object({
|
681
732
|
id: z3.string().nullish(),
|
682
|
-
type: z3.literal("function"),
|
683
733
|
function: z3.object({
|
684
734
|
name: z3.string(),
|
685
735
|
arguments: z3.string()
|
@@ -707,7 +757,6 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => z3.union([
|
|
707
757
|
z3.object({
|
708
758
|
index: z3.number(),
|
709
759
|
id: z3.string().nullish(),
|
710
|
-
type: z3.literal("function").nullish(),
|
711
760
|
function: z3.object({
|
712
761
|
name: z3.string().nullish(),
|
713
762
|
arguments: z3.string().nullish()
|
@@ -732,7 +781,7 @@ import {
|
|
732
781
|
parseProviderOptions as parseProviderOptions2,
|
733
782
|
postJsonToApi as postJsonToApi2
|
734
783
|
} from "@ai-sdk/provider-utils";
|
735
|
-
import { z as z5 } from "zod";
|
784
|
+
import { z as z5 } from "zod/v4";
|
736
785
|
|
737
786
|
// src/convert-to-openai-compatible-completion-prompt.ts
|
738
787
|
import {
|
@@ -813,7 +862,7 @@ ${user}:`]
|
|
813
862
|
}
|
814
863
|
|
815
864
|
// src/openai-compatible-completion-options.ts
|
816
|
-
import { z as z4 } from "zod";
|
865
|
+
import { z as z4 } from "zod/v4";
|
817
866
|
var openaiCompatibleCompletionProviderOptions = z4.object({
|
818
867
|
/**
|
819
868
|
* Echo back the prompt in addition to the completion.
|
@@ -1006,6 +1055,9 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
1006
1055
|
},
|
1007
1056
|
transform(chunk, controller) {
|
1008
1057
|
var _a, _b, _c;
|
1058
|
+
if (options.includeRawChunks) {
|
1059
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
1060
|
+
}
|
1009
1061
|
if (!chunk.success) {
|
1010
1062
|
finishReason = "error";
|
1011
1063
|
controller.enqueue({ type: "error", error: chunk.error });
|
@@ -1023,6 +1075,10 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
1023
1075
|
type: "response-metadata",
|
1024
1076
|
...getResponseMetadata(value)
|
1025
1077
|
});
|
1078
|
+
controller.enqueue({
|
1079
|
+
type: "text-start",
|
1080
|
+
id: "0"
|
1081
|
+
});
|
1026
1082
|
}
|
1027
1083
|
if (value.usage != null) {
|
1028
1084
|
usage.inputTokens = (_a = value.usage.prompt_tokens) != null ? _a : void 0;
|
@@ -1037,12 +1093,16 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
1037
1093
|
}
|
1038
1094
|
if ((choice == null ? void 0 : choice.text) != null) {
|
1039
1095
|
controller.enqueue({
|
1040
|
-
type: "text",
|
1041
|
-
|
1096
|
+
type: "text-delta",
|
1097
|
+
id: "0",
|
1098
|
+
delta: choice.text
|
1042
1099
|
});
|
1043
1100
|
}
|
1044
1101
|
},
|
1045
1102
|
flush(controller) {
|
1103
|
+
if (!isFirstChunk) {
|
1104
|
+
controller.enqueue({ type: "text-end", id: "0" });
|
1105
|
+
}
|
1046
1106
|
controller.enqueue({
|
1047
1107
|
type: "finish",
|
1048
1108
|
finishReason,
|
@@ -1101,10 +1161,10 @@ import {
|
|
1101
1161
|
parseProviderOptions as parseProviderOptions3,
|
1102
1162
|
postJsonToApi as postJsonToApi3
|
1103
1163
|
} from "@ai-sdk/provider-utils";
|
1104
|
-
import { z as z7 } from "zod";
|
1164
|
+
import { z as z7 } from "zod/v4";
|
1105
1165
|
|
1106
1166
|
// src/openai-compatible-embedding-options.ts
|
1107
|
-
import { z as z6 } from "zod";
|
1167
|
+
import { z as z6 } from "zod/v4";
|
1108
1168
|
var openaiCompatibleEmbeddingProviderOptions = z6.object({
|
1109
1169
|
/**
|
1110
1170
|
* The number of dimensions the resulting output embeddings should have.
|
@@ -1211,7 +1271,7 @@ import {
|
|
1211
1271
|
createJsonResponseHandler as createJsonResponseHandler4,
|
1212
1272
|
postJsonToApi as postJsonToApi4
|
1213
1273
|
} from "@ai-sdk/provider-utils";
|
1214
|
-
import { z as z8 } from "zod";
|
1274
|
+
import { z as z8 } from "zod/v4";
|
1215
1275
|
var OpenAICompatibleImageModel = class {
|
1216
1276
|
constructor(modelId, config) {
|
1217
1277
|
this.modelId = modelId;
|
@@ -1305,14 +1365,14 @@ function createOpenAICompatible(options) {
|
|
1305
1365
|
fetch: options.fetch
|
1306
1366
|
});
|
1307
1367
|
const createLanguageModel = (modelId) => createChatModel(modelId);
|
1308
|
-
const createChatModel = (modelId) => new OpenAICompatibleChatLanguageModel(
|
1309
|
-
|
1310
|
-
|
1311
|
-
);
|
1312
|
-
const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(
|
1313
|
-
|
1314
|
-
|
1315
|
-
);
|
1368
|
+
const createChatModel = (modelId) => new OpenAICompatibleChatLanguageModel(modelId, {
|
1369
|
+
...getCommonModelConfig("chat"),
|
1370
|
+
includeUsage: options.includeUsage
|
1371
|
+
});
|
1372
|
+
const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(modelId, {
|
1373
|
+
...getCommonModelConfig("completion"),
|
1374
|
+
includeUsage: options.includeUsage
|
1375
|
+
});
|
1316
1376
|
const createEmbeddingModel = (modelId) => new OpenAICompatibleEmbeddingModel(modelId, {
|
1317
1377
|
...getCommonModelConfig("embedding")
|
1318
1378
|
});
|