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