@ai-sdk/openai-compatible 1.0.2 → 1.0.4
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 +13 -0
- package/dist/index.js +33 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +33 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
@@ -359,7 +359,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
359
359
|
};
|
360
360
|
}
|
361
361
|
async doGenerate(options) {
|
362
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|
362
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
|
363
363
|
const { args, warnings } = await this.getArgs({ ...options });
|
364
364
|
const body = JSON.stringify(args);
|
365
365
|
const {
|
@@ -386,7 +386,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
386
386
|
if (text != null && text.length > 0) {
|
387
387
|
content.push({ type: "text", text });
|
388
388
|
}
|
389
|
-
const reasoning = choice.message.reasoning_content;
|
389
|
+
const reasoning = (_a = choice.message.reasoning_content) != null ? _a : choice.message.reasoning;
|
390
390
|
if (reasoning != null && reasoning.length > 0) {
|
391
391
|
content.push({
|
392
392
|
type: "reasoning",
|
@@ -397,7 +397,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
397
397
|
for (const toolCall of choice.message.tool_calls) {
|
398
398
|
content.push({
|
399
399
|
type: "tool-call",
|
400
|
-
toolCallId: (
|
400
|
+
toolCallId: (_b = toolCall.id) != null ? _b : generateId(),
|
401
401
|
toolName: toolCall.function.name,
|
402
402
|
input: toolCall.function.arguments
|
403
403
|
});
|
@@ -405,11 +405,11 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
405
405
|
}
|
406
406
|
const providerMetadata = {
|
407
407
|
[this.providerOptionsName]: {},
|
408
|
-
...await ((
|
408
|
+
...await ((_d = (_c = this.config.metadataExtractor) == null ? void 0 : _c.extractMetadata) == null ? void 0 : _d.call(_c, {
|
409
409
|
parsedBody: rawResponse
|
410
410
|
}))
|
411
411
|
};
|
412
|
-
const completionTokenDetails = (
|
412
|
+
const completionTokenDetails = (_e = responseBody.usage) == null ? void 0 : _e.completion_tokens_details;
|
413
413
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
|
414
414
|
providerMetadata[this.providerOptionsName].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
|
415
415
|
}
|
@@ -420,11 +420,11 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
420
420
|
content,
|
421
421
|
finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
422
422
|
usage: {
|
423
|
-
inputTokens: (
|
424
|
-
outputTokens: (
|
425
|
-
totalTokens: (
|
426
|
-
reasoningTokens: (
|
427
|
-
cachedInputTokens: (
|
423
|
+
inputTokens: (_g = (_f = responseBody.usage) == null ? void 0 : _f.prompt_tokens) != null ? _g : void 0,
|
424
|
+
outputTokens: (_i = (_h = responseBody.usage) == null ? void 0 : _h.completion_tokens) != null ? _i : void 0,
|
425
|
+
totalTokens: (_k = (_j = responseBody.usage) == null ? void 0 : _j.total_tokens) != null ? _k : void 0,
|
426
|
+
reasoningTokens: (_n = (_m = (_l = responseBody.usage) == null ? void 0 : _l.completion_tokens_details) == null ? void 0 : _m.reasoning_tokens) != null ? _n : void 0,
|
427
|
+
cachedInputTokens: (_q = (_p = (_o = responseBody.usage) == null ? void 0 : _o.prompt_tokens_details) == null ? void 0 : _p.cached_tokens) != null ? _q : void 0
|
428
428
|
},
|
429
429
|
providerMetadata,
|
430
430
|
request: { body },
|
@@ -487,7 +487,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
487
487
|
},
|
488
488
|
// TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
|
489
489
|
transform(chunk, controller) {
|
490
|
-
var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
490
|
+
var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|
491
491
|
if (options.includeRawChunks) {
|
492
492
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
493
493
|
}
|
@@ -510,14 +510,15 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
510
510
|
...getResponseMetadata(value)
|
511
511
|
});
|
512
512
|
}
|
513
|
-
|
513
|
+
const effectiveUsage = (_c = value.usage) != null ? _c : (_b = (_a2 = value.choices) == null ? void 0 : _a2[0]) == null ? void 0 : _b.usage;
|
514
|
+
if (effectiveUsage != null) {
|
514
515
|
const {
|
515
516
|
prompt_tokens,
|
516
517
|
completion_tokens,
|
517
518
|
total_tokens,
|
518
519
|
prompt_tokens_details,
|
519
520
|
completion_tokens_details
|
520
|
-
} =
|
521
|
+
} = effectiveUsage;
|
521
522
|
usage.promptTokens = prompt_tokens != null ? prompt_tokens : void 0;
|
522
523
|
usage.completionTokens = completion_tokens != null ? completion_tokens : void 0;
|
523
524
|
usage.totalTokens = total_tokens != null ? total_tokens : void 0;
|
@@ -544,7 +545,8 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
544
545
|
return;
|
545
546
|
}
|
546
547
|
const delta = choice.delta;
|
547
|
-
|
548
|
+
const reasoningContent = (_d = delta.reasoning_content) != null ? _d : delta.reasoning;
|
549
|
+
if (reasoningContent) {
|
548
550
|
if (!isActiveReasoning) {
|
549
551
|
controller.enqueue({
|
550
552
|
type: "reasoning-start",
|
@@ -555,7 +557,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
555
557
|
controller.enqueue({
|
556
558
|
type: "reasoning-delta",
|
557
559
|
id: "reasoning-0",
|
558
|
-
delta:
|
560
|
+
delta: reasoningContent
|
559
561
|
});
|
560
562
|
}
|
561
563
|
if (delta.content) {
|
@@ -579,7 +581,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
579
581
|
message: `Expected 'id' to be a string.`
|
580
582
|
});
|
581
583
|
}
|
582
|
-
if (((
|
584
|
+
if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) {
|
583
585
|
throw new InvalidResponseDataError({
|
584
586
|
data: toolCallDelta,
|
585
587
|
message: `Expected 'function.name' to be a string.`
|
@@ -595,12 +597,12 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
595
597
|
type: "function",
|
596
598
|
function: {
|
597
599
|
name: toolCallDelta.function.name,
|
598
|
-
arguments: (
|
600
|
+
arguments: (_f = toolCallDelta.function.arguments) != null ? _f : ""
|
599
601
|
},
|
600
602
|
hasFinished: false
|
601
603
|
};
|
602
604
|
const toolCall2 = toolCalls[index];
|
603
|
-
if (((
|
605
|
+
if (((_g = toolCall2.function) == null ? void 0 : _g.name) != null && ((_h = toolCall2.function) == null ? void 0 : _h.arguments) != null) {
|
604
606
|
if (toolCall2.function.arguments.length > 0) {
|
605
607
|
controller.enqueue({
|
606
608
|
type: "tool-input-start",
|
@@ -615,7 +617,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
615
617
|
});
|
616
618
|
controller.enqueue({
|
617
619
|
type: "tool-call",
|
618
|
-
toolCallId: (
|
620
|
+
toolCallId: (_i = toolCall2.id) != null ? _i : generateId(),
|
619
621
|
toolName: toolCall2.function.name,
|
620
622
|
input: toolCall2.function.arguments
|
621
623
|
});
|
@@ -628,22 +630,22 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
628
630
|
if (toolCall.hasFinished) {
|
629
631
|
continue;
|
630
632
|
}
|
631
|
-
if (((
|
632
|
-
toolCall.function.arguments += (
|
633
|
+
if (((_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null) {
|
634
|
+
toolCall.function.arguments += (_l = (_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null ? _l : "";
|
633
635
|
}
|
634
636
|
controller.enqueue({
|
635
637
|
type: "tool-input-delta",
|
636
638
|
id: toolCall.id,
|
637
|
-
delta: (
|
639
|
+
delta: (_m = toolCallDelta.function.arguments) != null ? _m : ""
|
638
640
|
});
|
639
|
-
if (((
|
641
|
+
if (((_n = toolCall.function) == null ? void 0 : _n.name) != null && ((_o = toolCall.function) == null ? void 0 : _o.arguments) != null && isParsableJson(toolCall.function.arguments)) {
|
640
642
|
controller.enqueue({
|
641
643
|
type: "tool-input-end",
|
642
644
|
id: toolCall.id
|
643
645
|
});
|
644
646
|
controller.enqueue({
|
645
647
|
type: "tool-call",
|
646
|
-
toolCallId: (
|
648
|
+
toolCallId: (_p = toolCall.id) != null ? _p : generateId(),
|
647
649
|
toolName: toolCall.function.name,
|
648
650
|
input: toolCall.function.arguments
|
649
651
|
});
|
@@ -727,6 +729,7 @@ var OpenAICompatibleChatResponseSchema = z3.object({
|
|
727
729
|
role: z3.literal("assistant").nullish(),
|
728
730
|
content: z3.string().nullish(),
|
729
731
|
reasoning_content: z3.string().nullish(),
|
732
|
+
reasoning: z3.string().nullish(),
|
730
733
|
tool_calls: z3.array(
|
731
734
|
z3.object({
|
732
735
|
id: z3.string().nullish(),
|
@@ -752,7 +755,10 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => z3.union([
|
|
752
755
|
delta: z3.object({
|
753
756
|
role: z3.enum(["assistant"]).nullish(),
|
754
757
|
content: z3.string().nullish(),
|
758
|
+
// Most openai-compatible models set `reasoning_content`, but some
|
759
|
+
// providers serving `gpt-oss` set `reasoning`. See #7866
|
755
760
|
reasoning_content: z3.string().nullish(),
|
761
|
+
reasoning: z3.string().nullish(),
|
756
762
|
tool_calls: z3.array(
|
757
763
|
z3.object({
|
758
764
|
index: z3.number(),
|
@@ -764,7 +770,9 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => z3.union([
|
|
764
770
|
})
|
765
771
|
).nullish()
|
766
772
|
}).nullish(),
|
767
|
-
finish_reason: z3.string().nullish()
|
773
|
+
finish_reason: z3.string().nullish(),
|
774
|
+
// Some providers report usage within each choice in streaming chunks
|
775
|
+
usage: openaiCompatibleTokenUsageSchema
|
768
776
|
})
|
769
777
|
),
|
770
778
|
usage: openaiCompatibleTokenUsageSchema
|