@openrouter/ai-sdk-provider 0.6.0 → 0.7.0
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/README.md +6 -4
- package/dist/index.d.mts +21 -19
- package/dist/index.d.ts +21 -19
- package/dist/index.js +231 -95
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +231 -95
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +33 -31
- package/dist/internal/index.d.ts +33 -31
- package/dist/internal/index.js +231 -95
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +231 -95
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -33,6 +33,32 @@ var __objRest = (source, exclude) => {
|
|
|
33
33
|
// src/openrouter-facade.ts
|
|
34
34
|
import { loadApiKey, withoutTrailingSlash } from "@ai-sdk/provider-utils";
|
|
35
35
|
|
|
36
|
+
// src/schemas/reasoning-details.ts
|
|
37
|
+
import { z } from "zod";
|
|
38
|
+
var ReasoningDetailSummarySchema = z.object({
|
|
39
|
+
type: z.literal("reasoning.summary" /* Summary */),
|
|
40
|
+
summary: z.string()
|
|
41
|
+
});
|
|
42
|
+
var ReasoningDetailEncryptedSchema = z.object({
|
|
43
|
+
type: z.literal("reasoning.encrypted" /* Encrypted */),
|
|
44
|
+
data: z.string()
|
|
45
|
+
});
|
|
46
|
+
var ReasoningDetailTextSchema = z.object({
|
|
47
|
+
type: z.literal("reasoning.text" /* Text */),
|
|
48
|
+
text: z.string().nullish(),
|
|
49
|
+
signature: z.string().nullish()
|
|
50
|
+
});
|
|
51
|
+
var ReasoningDetailUnionSchema = z.union([
|
|
52
|
+
ReasoningDetailSummarySchema,
|
|
53
|
+
ReasoningDetailEncryptedSchema,
|
|
54
|
+
ReasoningDetailTextSchema
|
|
55
|
+
]);
|
|
56
|
+
var ReasoningDetailsWithUnknownSchema = z.union([
|
|
57
|
+
ReasoningDetailUnionSchema,
|
|
58
|
+
z.unknown().transform(() => null)
|
|
59
|
+
]);
|
|
60
|
+
var ReasoningDetailArraySchema = z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
61
|
+
|
|
36
62
|
// src/openrouter-chat-language-model.ts
|
|
37
63
|
import {
|
|
38
64
|
InvalidResponseDataError,
|
|
@@ -46,7 +72,7 @@ import {
|
|
|
46
72
|
isParsableJson,
|
|
47
73
|
postJsonToApi
|
|
48
74
|
} from "@ai-sdk/provider-utils";
|
|
49
|
-
import { z as
|
|
75
|
+
import { z as z3 } from "zod";
|
|
50
76
|
|
|
51
77
|
// src/convert-to-openrouter-chat-messages.ts
|
|
52
78
|
import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
|
|
@@ -130,6 +156,8 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
130
156
|
}
|
|
131
157
|
case "assistant": {
|
|
132
158
|
let text = "";
|
|
159
|
+
let reasoning = "";
|
|
160
|
+
const reasoningDetails = [];
|
|
133
161
|
const toolCalls = [];
|
|
134
162
|
for (const part of content) {
|
|
135
163
|
switch (part.type) {
|
|
@@ -148,10 +176,23 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
148
176
|
});
|
|
149
177
|
break;
|
|
150
178
|
}
|
|
179
|
+
case "reasoning": {
|
|
180
|
+
reasoning += part.text;
|
|
181
|
+
reasoningDetails.push({
|
|
182
|
+
type: "reasoning.text" /* Text */,
|
|
183
|
+
text: part.text,
|
|
184
|
+
signature: part.signature
|
|
185
|
+
});
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
case "redacted-reasoning": {
|
|
189
|
+
reasoningDetails.push({
|
|
190
|
+
type: "reasoning.encrypted" /* Encrypted */,
|
|
191
|
+
data: part.data
|
|
192
|
+
});
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
151
195
|
case "file":
|
|
152
|
-
// TODO: Handle reasoning and redacted-reasoning
|
|
153
|
-
case "reasoning":
|
|
154
|
-
case "redacted-reasoning":
|
|
155
196
|
break;
|
|
156
197
|
default: {
|
|
157
198
|
const _exhaustiveCheck = part;
|
|
@@ -163,6 +204,8 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
163
204
|
role: "assistant",
|
|
164
205
|
content: text,
|
|
165
206
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
207
|
+
reasoning: reasoning || void 0,
|
|
208
|
+
reasoning_details: reasoningDetails.length > 0 ? reasoningDetails : void 0,
|
|
166
209
|
cache_control: getCacheControl(providerMetadata)
|
|
167
210
|
});
|
|
168
211
|
break;
|
|
@@ -219,13 +262,13 @@ function mapOpenRouterFinishReason(finishReason) {
|
|
|
219
262
|
|
|
220
263
|
// src/openrouter-error.ts
|
|
221
264
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
222
|
-
import { z } from "zod";
|
|
223
|
-
var OpenRouterErrorResponseSchema =
|
|
224
|
-
error:
|
|
225
|
-
message:
|
|
226
|
-
type:
|
|
227
|
-
param:
|
|
228
|
-
code:
|
|
265
|
+
import { z as z2 } from "zod";
|
|
266
|
+
var OpenRouterErrorResponseSchema = z2.object({
|
|
267
|
+
error: z2.object({
|
|
268
|
+
message: z2.string(),
|
|
269
|
+
type: z2.string(),
|
|
270
|
+
param: z2.any().nullable(),
|
|
271
|
+
code: z2.string().nullable()
|
|
229
272
|
})
|
|
230
273
|
});
|
|
231
274
|
var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
|
|
@@ -372,13 +415,56 @@ var OpenRouterChatLanguageModel = class {
|
|
|
372
415
|
};
|
|
373
416
|
}
|
|
374
417
|
const hasProviderMetadata = Object.keys(providerMetadata).length > 0;
|
|
418
|
+
const reasoningDetails = (_h = choice.message.reasoning_details) != null ? _h : [];
|
|
419
|
+
const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
|
|
420
|
+
var _a2;
|
|
421
|
+
switch (detail.type) {
|
|
422
|
+
case "reasoning.text" /* Text */: {
|
|
423
|
+
if (detail.text) {
|
|
424
|
+
return {
|
|
425
|
+
type: "text",
|
|
426
|
+
text: detail.text,
|
|
427
|
+
signature: (_a2 = detail.signature) != null ? _a2 : void 0
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
case "reasoning.summary" /* Summary */: {
|
|
433
|
+
if (detail.summary) {
|
|
434
|
+
return {
|
|
435
|
+
type: "text",
|
|
436
|
+
text: detail.summary
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
break;
|
|
440
|
+
}
|
|
441
|
+
case "reasoning.encrypted" /* Encrypted */: {
|
|
442
|
+
if (detail.data) {
|
|
443
|
+
return {
|
|
444
|
+
type: "redacted",
|
|
445
|
+
data: detail.data
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
default: {
|
|
451
|
+
detail;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return null;
|
|
455
|
+
}).filter((p) => p !== null) : choice.message.reasoning ? [
|
|
456
|
+
{
|
|
457
|
+
type: "text",
|
|
458
|
+
text: choice.message.reasoning
|
|
459
|
+
}
|
|
460
|
+
] : [];
|
|
375
461
|
return __spreadValues({
|
|
376
462
|
response: {
|
|
377
463
|
id: response.id,
|
|
378
464
|
modelId: response.model
|
|
379
465
|
},
|
|
380
|
-
text: (
|
|
381
|
-
reasoning
|
|
466
|
+
text: (_i = choice.message.content) != null ? _i : void 0,
|
|
467
|
+
reasoning,
|
|
382
468
|
toolCalls: (_j = choice.message.tool_calls) == null ? void 0 : _j.map((toolCall) => {
|
|
383
469
|
var _a2;
|
|
384
470
|
return {
|
|
@@ -497,11 +583,56 @@ var OpenRouterChatLanguageModel = class {
|
|
|
497
583
|
textDelta: delta.reasoning
|
|
498
584
|
});
|
|
499
585
|
}
|
|
586
|
+
if (delta.reasoning_details && delta.reasoning_details.length > 0) {
|
|
587
|
+
for (const detail of delta.reasoning_details) {
|
|
588
|
+
switch (detail.type) {
|
|
589
|
+
case "reasoning.text" /* Text */: {
|
|
590
|
+
if (detail.text) {
|
|
591
|
+
controller.enqueue({
|
|
592
|
+
type: "reasoning",
|
|
593
|
+
textDelta: detail.text
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
if (detail.signature) {
|
|
597
|
+
controller.enqueue({
|
|
598
|
+
type: "reasoning-signature",
|
|
599
|
+
signature: detail.signature
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
break;
|
|
603
|
+
}
|
|
604
|
+
case "reasoning.encrypted" /* Encrypted */: {
|
|
605
|
+
if (detail.data) {
|
|
606
|
+
controller.enqueue({
|
|
607
|
+
type: "redacted-reasoning",
|
|
608
|
+
data: detail.data
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
613
|
+
case "reasoning.summary" /* Summary */: {
|
|
614
|
+
if (detail.summary) {
|
|
615
|
+
controller.enqueue({
|
|
616
|
+
type: "reasoning",
|
|
617
|
+
textDelta: detail.summary
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
default: {
|
|
623
|
+
detail;
|
|
624
|
+
break;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
500
629
|
const mappedLogprobs = mapOpenRouterChatLogProbsOutput(
|
|
501
630
|
choice == null ? void 0 : choice.logprobs
|
|
502
631
|
);
|
|
503
632
|
if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
|
|
504
|
-
if (logprobs === void 0)
|
|
633
|
+
if (logprobs === void 0) {
|
|
634
|
+
logprobs = [];
|
|
635
|
+
}
|
|
505
636
|
logprobs.push(...mappedLogprobs);
|
|
506
637
|
}
|
|
507
638
|
if (delta.tool_calls != null) {
|
|
@@ -624,95 +755,97 @@ var OpenRouterChatLanguageModel = class {
|
|
|
624
755
|
};
|
|
625
756
|
}
|
|
626
757
|
};
|
|
627
|
-
var OpenRouterChatCompletionBaseResponseSchema =
|
|
628
|
-
id:
|
|
629
|
-
model:
|
|
630
|
-
usage:
|
|
631
|
-
prompt_tokens:
|
|
632
|
-
prompt_tokens_details:
|
|
633
|
-
cached_tokens:
|
|
758
|
+
var OpenRouterChatCompletionBaseResponseSchema = z3.object({
|
|
759
|
+
id: z3.string().optional(),
|
|
760
|
+
model: z3.string().optional(),
|
|
761
|
+
usage: z3.object({
|
|
762
|
+
prompt_tokens: z3.number(),
|
|
763
|
+
prompt_tokens_details: z3.object({
|
|
764
|
+
cached_tokens: z3.number()
|
|
634
765
|
}).optional(),
|
|
635
|
-
completion_tokens:
|
|
636
|
-
completion_tokens_details:
|
|
637
|
-
reasoning_tokens:
|
|
766
|
+
completion_tokens: z3.number(),
|
|
767
|
+
completion_tokens_details: z3.object({
|
|
768
|
+
reasoning_tokens: z3.number()
|
|
638
769
|
}).optional(),
|
|
639
|
-
total_tokens:
|
|
640
|
-
cost:
|
|
770
|
+
total_tokens: z3.number(),
|
|
771
|
+
cost: z3.number().optional()
|
|
641
772
|
}).nullish()
|
|
642
773
|
});
|
|
643
774
|
var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
644
|
-
choices:
|
|
645
|
-
|
|
646
|
-
message:
|
|
647
|
-
role:
|
|
648
|
-
content:
|
|
649
|
-
reasoning:
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
775
|
+
choices: z3.array(
|
|
776
|
+
z3.object({
|
|
777
|
+
message: z3.object({
|
|
778
|
+
role: z3.literal("assistant"),
|
|
779
|
+
content: z3.string().nullable().optional(),
|
|
780
|
+
reasoning: z3.string().nullable().optional(),
|
|
781
|
+
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
782
|
+
tool_calls: z3.array(
|
|
783
|
+
z3.object({
|
|
784
|
+
id: z3.string().optional().nullable(),
|
|
785
|
+
type: z3.literal("function"),
|
|
786
|
+
function: z3.object({
|
|
787
|
+
name: z3.string(),
|
|
788
|
+
arguments: z3.string()
|
|
657
789
|
})
|
|
658
790
|
})
|
|
659
791
|
).optional()
|
|
660
792
|
}),
|
|
661
|
-
index:
|
|
662
|
-
logprobs:
|
|
663
|
-
content:
|
|
664
|
-
|
|
665
|
-
token:
|
|
666
|
-
logprob:
|
|
667
|
-
top_logprobs:
|
|
668
|
-
|
|
669
|
-
token:
|
|
670
|
-
logprob:
|
|
793
|
+
index: z3.number(),
|
|
794
|
+
logprobs: z3.object({
|
|
795
|
+
content: z3.array(
|
|
796
|
+
z3.object({
|
|
797
|
+
token: z3.string(),
|
|
798
|
+
logprob: z3.number(),
|
|
799
|
+
top_logprobs: z3.array(
|
|
800
|
+
z3.object({
|
|
801
|
+
token: z3.string(),
|
|
802
|
+
logprob: z3.number()
|
|
671
803
|
})
|
|
672
804
|
)
|
|
673
805
|
})
|
|
674
806
|
).nullable()
|
|
675
807
|
}).nullable().optional(),
|
|
676
|
-
finish_reason:
|
|
808
|
+
finish_reason: z3.string().optional().nullable()
|
|
677
809
|
})
|
|
678
810
|
)
|
|
679
811
|
});
|
|
680
|
-
var OpenRouterStreamChatCompletionChunkSchema =
|
|
812
|
+
var OpenRouterStreamChatCompletionChunkSchema = z3.union([
|
|
681
813
|
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
682
|
-
choices:
|
|
683
|
-
|
|
684
|
-
delta:
|
|
685
|
-
role:
|
|
686
|
-
content:
|
|
687
|
-
reasoning:
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
814
|
+
choices: z3.array(
|
|
815
|
+
z3.object({
|
|
816
|
+
delta: z3.object({
|
|
817
|
+
role: z3.enum(["assistant"]).optional(),
|
|
818
|
+
content: z3.string().nullish(),
|
|
819
|
+
reasoning: z3.string().nullish().optional(),
|
|
820
|
+
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
821
|
+
tool_calls: z3.array(
|
|
822
|
+
z3.object({
|
|
823
|
+
index: z3.number(),
|
|
824
|
+
id: z3.string().nullish(),
|
|
825
|
+
type: z3.literal("function").optional(),
|
|
826
|
+
function: z3.object({
|
|
827
|
+
name: z3.string().nullish(),
|
|
828
|
+
arguments: z3.string().nullish()
|
|
696
829
|
})
|
|
697
830
|
})
|
|
698
831
|
).nullish()
|
|
699
832
|
}).nullish(),
|
|
700
|
-
logprobs:
|
|
701
|
-
content:
|
|
702
|
-
|
|
703
|
-
token:
|
|
704
|
-
logprob:
|
|
705
|
-
top_logprobs:
|
|
706
|
-
|
|
707
|
-
token:
|
|
708
|
-
logprob:
|
|
833
|
+
logprobs: z3.object({
|
|
834
|
+
content: z3.array(
|
|
835
|
+
z3.object({
|
|
836
|
+
token: z3.string(),
|
|
837
|
+
logprob: z3.number(),
|
|
838
|
+
top_logprobs: z3.array(
|
|
839
|
+
z3.object({
|
|
840
|
+
token: z3.string(),
|
|
841
|
+
logprob: z3.number()
|
|
709
842
|
})
|
|
710
843
|
)
|
|
711
844
|
})
|
|
712
845
|
).nullable()
|
|
713
846
|
}).nullish(),
|
|
714
|
-
finish_reason:
|
|
715
|
-
index:
|
|
847
|
+
finish_reason: z3.string().nullable().optional(),
|
|
848
|
+
index: z3.number()
|
|
716
849
|
})
|
|
717
850
|
)
|
|
718
851
|
}),
|
|
@@ -777,7 +910,7 @@ import {
|
|
|
777
910
|
createJsonResponseHandler as createJsonResponseHandler2,
|
|
778
911
|
postJsonToApi as postJsonToApi2
|
|
779
912
|
} from "@ai-sdk/provider-utils";
|
|
780
|
-
import { z as
|
|
913
|
+
import { z as z4 } from "zod";
|
|
781
914
|
|
|
782
915
|
// src/convert-to-openrouter-completion-prompt.ts
|
|
783
916
|
import {
|
|
@@ -1110,7 +1243,9 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
1110
1243
|
choice == null ? void 0 : choice.logprobs
|
|
1111
1244
|
);
|
|
1112
1245
|
if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
|
|
1113
|
-
if (logprobs === void 0)
|
|
1246
|
+
if (logprobs === void 0) {
|
|
1247
|
+
logprobs = [];
|
|
1248
|
+
}
|
|
1114
1249
|
logprobs.push(...mappedLogprobs);
|
|
1115
1250
|
}
|
|
1116
1251
|
},
|
|
@@ -1130,26 +1265,27 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
1130
1265
|
};
|
|
1131
1266
|
}
|
|
1132
1267
|
};
|
|
1133
|
-
var OpenRouterCompletionChunkSchema =
|
|
1134
|
-
|
|
1135
|
-
id:
|
|
1136
|
-
model:
|
|
1137
|
-
choices:
|
|
1138
|
-
|
|
1139
|
-
text:
|
|
1140
|
-
reasoning:
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1268
|
+
var OpenRouterCompletionChunkSchema = z4.union([
|
|
1269
|
+
z4.object({
|
|
1270
|
+
id: z4.string().optional(),
|
|
1271
|
+
model: z4.string().optional(),
|
|
1272
|
+
choices: z4.array(
|
|
1273
|
+
z4.object({
|
|
1274
|
+
text: z4.string(),
|
|
1275
|
+
reasoning: z4.string().nullish().optional(),
|
|
1276
|
+
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1277
|
+
finish_reason: z4.string().nullish(),
|
|
1278
|
+
index: z4.number(),
|
|
1279
|
+
logprobs: z4.object({
|
|
1280
|
+
tokens: z4.array(z4.string()),
|
|
1281
|
+
token_logprobs: z4.array(z4.number()),
|
|
1282
|
+
top_logprobs: z4.array(z4.record(z4.string(), z4.number())).nullable()
|
|
1147
1283
|
}).nullable().optional()
|
|
1148
1284
|
})
|
|
1149
1285
|
),
|
|
1150
|
-
usage:
|
|
1151
|
-
prompt_tokens:
|
|
1152
|
-
completion_tokens:
|
|
1286
|
+
usage: z4.object({
|
|
1287
|
+
prompt_tokens: z4.number(),
|
|
1288
|
+
completion_tokens: z4.number()
|
|
1153
1289
|
}).optional().nullable()
|
|
1154
1290
|
}),
|
|
1155
1291
|
OpenRouterErrorResponseSchema
|