@openrouter/ai-sdk-provider 0.5.0 → 0.7.0-alpha.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/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 z2 } from "zod";
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";
@@ -81,14 +107,15 @@ function convertToOpenRouterChatMessages(prompt) {
81
107
  const messageCacheControl = getCacheControl(providerMetadata);
82
108
  const contentParts = content.map(
83
109
  (part) => {
84
- var _a2, _b2, _c2, _d, _e, _f;
110
+ var _a2, _b2, _c2, _d;
111
+ const cacheControl = (_a2 = getCacheControl(part.providerMetadata)) != null ? _a2 : messageCacheControl;
85
112
  switch (part.type) {
86
113
  case "text":
87
114
  return {
88
115
  type: "text",
89
116
  text: part.text,
90
117
  // For text parts, only use part-specific cache control
91
- cache_control: (_a2 = getCacheControl(part.providerMetadata)) != null ? _a2 : messageCacheControl
118
+ cache_control: cacheControl
92
119
  };
93
120
  case "image":
94
121
  return {
@@ -99,18 +126,18 @@ function convertToOpenRouterChatMessages(prompt) {
99
126
  )}`
100
127
  },
101
128
  // For image parts, use part-specific or message-level cache control
102
- cache_control: (_c2 = getCacheControl(part.providerMetadata)) != null ? _c2 : messageCacheControl
129
+ cache_control: cacheControl
103
130
  };
104
131
  case "file":
105
132
  return {
106
133
  type: "file",
107
134
  file: {
108
135
  filename: String(
109
- (_e = (_d = part.providerMetadata) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.filename
136
+ (_d = (_c2 = part.providerMetadata) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d.filename
110
137
  ),
111
138
  file_data: part.data instanceof Uint8Array ? `data:${part.mimeType};base64,${convertUint8ArrayToBase64(part.data)}` : `data:${part.mimeType};base64,${part.data}`
112
139
  },
113
- cache_control: (_f = getCacheControl(part.providerMetadata)) != null ? _f : messageCacheControl
140
+ cache_control: cacheControl
114
141
  };
115
142
  default: {
116
143
  const _exhaustiveCheck = part;
@@ -129,6 +156,8 @@ function convertToOpenRouterChatMessages(prompt) {
129
156
  }
130
157
  case "assistant": {
131
158
  let text = "";
159
+ let reasoning = "";
160
+ const reasoningDetails = [];
132
161
  const toolCalls = [];
133
162
  for (const part of content) {
134
163
  switch (part.type) {
@@ -147,9 +176,23 @@ function convertToOpenRouterChatMessages(prompt) {
147
176
  });
148
177
  break;
149
178
  }
150
- // TODO: Handle reasoning and redacted-reasoning
151
- case "reasoning":
152
- case "redacted-reasoning":
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
+ }
195
+ case "file":
153
196
  break;
154
197
  default: {
155
198
  const _exhaustiveCheck = part;
@@ -161,6 +204,8 @@ function convertToOpenRouterChatMessages(prompt) {
161
204
  role: "assistant",
162
205
  content: text,
163
206
  tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
207
+ reasoning: reasoning || void 0,
208
+ reasoning_details: reasoningDetails.length > 0 ? reasoningDetails : void 0,
164
209
  cache_control: getCacheControl(providerMetadata)
165
210
  });
166
211
  break;
@@ -217,13 +262,13 @@ function mapOpenRouterFinishReason(finishReason) {
217
262
 
218
263
  // src/openrouter-error.ts
219
264
  import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
220
- import { z } from "zod";
221
- var OpenRouterErrorResponseSchema = z.object({
222
- error: z.object({
223
- message: z.string(),
224
- type: z.string(),
225
- param: z.any().nullable(),
226
- code: z.string().nullable()
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()
227
272
  })
228
273
  });
229
274
  var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
@@ -370,13 +415,56 @@ var OpenRouterChatLanguageModel = class {
370
415
  };
371
416
  }
372
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
+ ] : [];
373
461
  return __spreadValues({
374
462
  response: {
375
463
  id: response.id,
376
464
  modelId: response.model
377
465
  },
378
- text: (_h = choice.message.content) != null ? _h : void 0,
379
- reasoning: (_i = choice.message.reasoning) != null ? _i : void 0,
466
+ text: (_i = choice.message.content) != null ? _i : void 0,
467
+ reasoning,
380
468
  toolCalls: (_j = choice.message.tool_calls) == null ? void 0 : _j.map((toolCall) => {
381
469
  var _a2;
382
470
  return {
@@ -495,11 +583,56 @@ var OpenRouterChatLanguageModel = class {
495
583
  textDelta: delta.reasoning
496
584
  });
497
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
+ }
498
629
  const mappedLogprobs = mapOpenRouterChatLogProbsOutput(
499
630
  choice == null ? void 0 : choice.logprobs
500
631
  );
501
632
  if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
502
- if (logprobs === void 0) logprobs = [];
633
+ if (logprobs === void 0) {
634
+ logprobs = [];
635
+ }
503
636
  logprobs.push(...mappedLogprobs);
504
637
  }
505
638
  if (delta.tool_calls != null) {
@@ -622,95 +755,97 @@ var OpenRouterChatLanguageModel = class {
622
755
  };
623
756
  }
624
757
  };
625
- var OpenRouterChatCompletionBaseResponseSchema = z2.object({
626
- id: z2.string().optional(),
627
- model: z2.string().optional(),
628
- usage: z2.object({
629
- prompt_tokens: z2.number(),
630
- prompt_tokens_details: z2.object({
631
- cached_tokens: z2.number()
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()
632
765
  }).optional(),
633
- completion_tokens: z2.number(),
634
- completion_tokens_details: z2.object({
635
- reasoning_tokens: z2.number()
766
+ completion_tokens: z3.number(),
767
+ completion_tokens_details: z3.object({
768
+ reasoning_tokens: z3.number()
636
769
  }).optional(),
637
- total_tokens: z2.number(),
638
- cost: z2.number().optional()
770
+ total_tokens: z3.number(),
771
+ cost: z3.number().optional()
639
772
  }).nullish()
640
773
  });
641
774
  var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
642
- choices: z2.array(
643
- z2.object({
644
- message: z2.object({
645
- role: z2.literal("assistant"),
646
- content: z2.string().nullable().optional(),
647
- reasoning: z2.string().nullable().optional(),
648
- tool_calls: z2.array(
649
- z2.object({
650
- id: z2.string().optional().nullable(),
651
- type: z2.literal("function"),
652
- function: z2.object({
653
- name: z2.string(),
654
- arguments: z2.string()
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()
655
789
  })
656
790
  })
657
791
  ).optional()
658
792
  }),
659
- index: z2.number(),
660
- logprobs: z2.object({
661
- content: z2.array(
662
- z2.object({
663
- token: z2.string(),
664
- logprob: z2.number(),
665
- top_logprobs: z2.array(
666
- z2.object({
667
- token: z2.string(),
668
- logprob: z2.number()
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()
669
803
  })
670
804
  )
671
805
  })
672
806
  ).nullable()
673
807
  }).nullable().optional(),
674
- finish_reason: z2.string().optional().nullable()
808
+ finish_reason: z3.string().optional().nullable()
675
809
  })
676
810
  )
677
811
  });
678
- var OpenRouterStreamChatCompletionChunkSchema = z2.union([
812
+ var OpenRouterStreamChatCompletionChunkSchema = z3.union([
679
813
  OpenRouterChatCompletionBaseResponseSchema.extend({
680
- choices: z2.array(
681
- z2.object({
682
- delta: z2.object({
683
- role: z2.enum(["assistant"]).optional(),
684
- content: z2.string().nullish(),
685
- reasoning: z2.string().nullish().optional(),
686
- tool_calls: z2.array(
687
- z2.object({
688
- index: z2.number(),
689
- id: z2.string().nullish(),
690
- type: z2.literal("function").optional(),
691
- function: z2.object({
692
- name: z2.string().nullish(),
693
- arguments: z2.string().nullish()
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()
694
829
  })
695
830
  })
696
831
  ).nullish()
697
832
  }).nullish(),
698
- logprobs: z2.object({
699
- content: z2.array(
700
- z2.object({
701
- token: z2.string(),
702
- logprob: z2.number(),
703
- top_logprobs: z2.array(
704
- z2.object({
705
- token: z2.string(),
706
- logprob: z2.number()
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()
707
842
  })
708
843
  )
709
844
  })
710
845
  ).nullable()
711
846
  }).nullish(),
712
- finish_reason: z2.string().nullable().optional(),
713
- index: z2.number()
847
+ finish_reason: z3.string().nullable().optional(),
848
+ index: z3.number()
714
849
  })
715
850
  )
716
851
  }),
@@ -775,7 +910,7 @@ import {
775
910
  createJsonResponseHandler as createJsonResponseHandler2,
776
911
  postJsonToApi as postJsonToApi2
777
912
  } from "@ai-sdk/provider-utils";
778
- import { z as z3 } from "zod";
913
+ import { z as z4 } from "zod";
779
914
 
780
915
  // src/convert-to-openrouter-completion-prompt.ts
781
916
  import {
@@ -857,6 +992,11 @@ ${userMessage}
857
992
  functionality: "redacted reasoning messages"
858
993
  });
859
994
  }
995
+ case "file": {
996
+ throw new UnsupportedFunctionalityError2({
997
+ functionality: "file attachments"
998
+ });
999
+ }
860
1000
  default: {
861
1001
  const _exhaustiveCheck = part;
862
1002
  throw new Error(
@@ -1103,7 +1243,9 @@ var OpenRouterCompletionLanguageModel = class {
1103
1243
  choice == null ? void 0 : choice.logprobs
1104
1244
  );
1105
1245
  if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
1106
- if (logprobs === void 0) logprobs = [];
1246
+ if (logprobs === void 0) {
1247
+ logprobs = [];
1248
+ }
1107
1249
  logprobs.push(...mappedLogprobs);
1108
1250
  }
1109
1251
  },
@@ -1123,26 +1265,27 @@ var OpenRouterCompletionLanguageModel = class {
1123
1265
  };
1124
1266
  }
1125
1267
  };
1126
- var OpenRouterCompletionChunkSchema = z3.union([
1127
- z3.object({
1128
- id: z3.string().optional(),
1129
- model: z3.string().optional(),
1130
- choices: z3.array(
1131
- z3.object({
1132
- text: z3.string(),
1133
- reasoning: z3.string().nullish().optional(),
1134
- finish_reason: z3.string().nullish(),
1135
- index: z3.number(),
1136
- logprobs: z3.object({
1137
- tokens: z3.array(z3.string()),
1138
- token_logprobs: z3.array(z3.number()),
1139
- top_logprobs: z3.array(z3.record(z3.string(), z3.number())).nullable()
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()
1140
1283
  }).nullable().optional()
1141
1284
  })
1142
1285
  ),
1143
- usage: z3.object({
1144
- prompt_tokens: z3.number(),
1145
- completion_tokens: z3.number()
1286
+ usage: z4.object({
1287
+ prompt_tokens: z4.number(),
1288
+ completion_tokens: z4.number()
1146
1289
  }).optional().nullable()
1147
1290
  }),
1148
1291
  OpenRouterErrorResponseSchema