@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.
@@ -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 z2 } from "zod";
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";
@@ -78,14 +104,15 @@ function convertToOpenRouterChatMessages(prompt) {
78
104
  const messageCacheControl = getCacheControl(providerMetadata);
79
105
  const contentParts = content.map(
80
106
  (part) => {
81
- var _a2, _b2, _c2, _d, _e, _f;
107
+ var _a2, _b2, _c2, _d;
108
+ const cacheControl = (_a2 = getCacheControl(part.providerMetadata)) != null ? _a2 : messageCacheControl;
82
109
  switch (part.type) {
83
110
  case "text":
84
111
  return {
85
112
  type: "text",
86
113
  text: part.text,
87
114
  // For text parts, only use part-specific cache control
88
- cache_control: (_a2 = getCacheControl(part.providerMetadata)) != null ? _a2 : messageCacheControl
115
+ cache_control: cacheControl
89
116
  };
90
117
  case "image":
91
118
  return {
@@ -96,18 +123,18 @@ function convertToOpenRouterChatMessages(prompt) {
96
123
  )}`
97
124
  },
98
125
  // For image parts, use part-specific or message-level cache control
99
- cache_control: (_c2 = getCacheControl(part.providerMetadata)) != null ? _c2 : messageCacheControl
126
+ cache_control: cacheControl
100
127
  };
101
128
  case "file":
102
129
  return {
103
130
  type: "file",
104
131
  file: {
105
132
  filename: String(
106
- (_e = (_d = part.providerMetadata) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.filename
133
+ (_d = (_c2 = part.providerMetadata) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d.filename
107
134
  ),
108
135
  file_data: part.data instanceof Uint8Array ? `data:${part.mimeType};base64,${convertUint8ArrayToBase64(part.data)}` : `data:${part.mimeType};base64,${part.data}`
109
136
  },
110
- cache_control: (_f = getCacheControl(part.providerMetadata)) != null ? _f : messageCacheControl
137
+ cache_control: cacheControl
111
138
  };
112
139
  default: {
113
140
  const _exhaustiveCheck = part;
@@ -126,6 +153,8 @@ function convertToOpenRouterChatMessages(prompt) {
126
153
  }
127
154
  case "assistant": {
128
155
  let text = "";
156
+ let reasoning = "";
157
+ const reasoningDetails = [];
129
158
  const toolCalls = [];
130
159
  for (const part of content) {
131
160
  switch (part.type) {
@@ -144,9 +173,23 @@ function convertToOpenRouterChatMessages(prompt) {
144
173
  });
145
174
  break;
146
175
  }
147
- // TODO: Handle reasoning and redacted-reasoning
148
- case "reasoning":
149
- case "redacted-reasoning":
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
+ }
192
+ case "file":
150
193
  break;
151
194
  default: {
152
195
  const _exhaustiveCheck = part;
@@ -158,6 +201,8 @@ function convertToOpenRouterChatMessages(prompt) {
158
201
  role: "assistant",
159
202
  content: text,
160
203
  tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
204
+ reasoning: reasoning || void 0,
205
+ reasoning_details: reasoningDetails.length > 0 ? reasoningDetails : void 0,
161
206
  cache_control: getCacheControl(providerMetadata)
162
207
  });
163
208
  break;
@@ -214,13 +259,13 @@ function mapOpenRouterFinishReason(finishReason) {
214
259
 
215
260
  // src/openrouter-error.ts
216
261
  import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
217
- import { z } from "zod";
218
- var OpenRouterErrorResponseSchema = z.object({
219
- error: z.object({
220
- message: z.string(),
221
- type: z.string(),
222
- param: z.any().nullable(),
223
- code: z.string().nullable()
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()
224
269
  })
225
270
  });
226
271
  var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
@@ -367,13 +412,56 @@ var OpenRouterChatLanguageModel = class {
367
412
  };
368
413
  }
369
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
+ ] : [];
370
458
  return __spreadValues({
371
459
  response: {
372
460
  id: response.id,
373
461
  modelId: response.model
374
462
  },
375
- text: (_h = choice.message.content) != null ? _h : void 0,
376
- reasoning: (_i = choice.message.reasoning) != null ? _i : void 0,
463
+ text: (_i = choice.message.content) != null ? _i : void 0,
464
+ reasoning,
377
465
  toolCalls: (_j = choice.message.tool_calls) == null ? void 0 : _j.map((toolCall) => {
378
466
  var _a2;
379
467
  return {
@@ -492,11 +580,56 @@ var OpenRouterChatLanguageModel = class {
492
580
  textDelta: delta.reasoning
493
581
  });
494
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
+ }
495
626
  const mappedLogprobs = mapOpenRouterChatLogProbsOutput(
496
627
  choice == null ? void 0 : choice.logprobs
497
628
  );
498
629
  if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
499
- if (logprobs === void 0) logprobs = [];
630
+ if (logprobs === void 0) {
631
+ logprobs = [];
632
+ }
500
633
  logprobs.push(...mappedLogprobs);
501
634
  }
502
635
  if (delta.tool_calls != null) {
@@ -619,95 +752,97 @@ var OpenRouterChatLanguageModel = class {
619
752
  };
620
753
  }
621
754
  };
622
- var OpenRouterChatCompletionBaseResponseSchema = z2.object({
623
- id: z2.string().optional(),
624
- model: z2.string().optional(),
625
- usage: z2.object({
626
- prompt_tokens: z2.number(),
627
- prompt_tokens_details: z2.object({
628
- cached_tokens: z2.number()
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()
629
762
  }).optional(),
630
- completion_tokens: z2.number(),
631
- completion_tokens_details: z2.object({
632
- reasoning_tokens: z2.number()
763
+ completion_tokens: z3.number(),
764
+ completion_tokens_details: z3.object({
765
+ reasoning_tokens: z3.number()
633
766
  }).optional(),
634
- total_tokens: z2.number(),
635
- cost: z2.number().optional()
767
+ total_tokens: z3.number(),
768
+ cost: z3.number().optional()
636
769
  }).nullish()
637
770
  });
638
771
  var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
639
- choices: z2.array(
640
- z2.object({
641
- message: z2.object({
642
- role: z2.literal("assistant"),
643
- content: z2.string().nullable().optional(),
644
- reasoning: z2.string().nullable().optional(),
645
- tool_calls: z2.array(
646
- z2.object({
647
- id: z2.string().optional().nullable(),
648
- type: z2.literal("function"),
649
- function: z2.object({
650
- name: z2.string(),
651
- arguments: z2.string()
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()
652
786
  })
653
787
  })
654
788
  ).optional()
655
789
  }),
656
- index: z2.number(),
657
- logprobs: z2.object({
658
- content: z2.array(
659
- z2.object({
660
- token: z2.string(),
661
- logprob: z2.number(),
662
- top_logprobs: z2.array(
663
- z2.object({
664
- token: z2.string(),
665
- logprob: z2.number()
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()
666
800
  })
667
801
  )
668
802
  })
669
803
  ).nullable()
670
804
  }).nullable().optional(),
671
- finish_reason: z2.string().optional().nullable()
805
+ finish_reason: z3.string().optional().nullable()
672
806
  })
673
807
  )
674
808
  });
675
- var OpenRouterStreamChatCompletionChunkSchema = z2.union([
809
+ var OpenRouterStreamChatCompletionChunkSchema = z3.union([
676
810
  OpenRouterChatCompletionBaseResponseSchema.extend({
677
- choices: z2.array(
678
- z2.object({
679
- delta: z2.object({
680
- role: z2.enum(["assistant"]).optional(),
681
- content: z2.string().nullish(),
682
- reasoning: z2.string().nullish().optional(),
683
- tool_calls: z2.array(
684
- z2.object({
685
- index: z2.number(),
686
- id: z2.string().nullish(),
687
- type: z2.literal("function").optional(),
688
- function: z2.object({
689
- name: z2.string().nullish(),
690
- arguments: z2.string().nullish()
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()
691
826
  })
692
827
  })
693
828
  ).nullish()
694
829
  }).nullish(),
695
- logprobs: z2.object({
696
- content: z2.array(
697
- z2.object({
698
- token: z2.string(),
699
- logprob: z2.number(),
700
- top_logprobs: z2.array(
701
- z2.object({
702
- token: z2.string(),
703
- logprob: z2.number()
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()
704
839
  })
705
840
  )
706
841
  })
707
842
  ).nullable()
708
843
  }).nullish(),
709
- finish_reason: z2.string().nullable().optional(),
710
- index: z2.number()
844
+ finish_reason: z3.string().nullable().optional(),
845
+ index: z3.number()
711
846
  })
712
847
  )
713
848
  }),
@@ -772,7 +907,7 @@ import {
772
907
  createJsonResponseHandler as createJsonResponseHandler2,
773
908
  postJsonToApi as postJsonToApi2
774
909
  } from "@ai-sdk/provider-utils";
775
- import { z as z3 } from "zod";
910
+ import { z as z4 } from "zod";
776
911
 
777
912
  // src/convert-to-openrouter-completion-prompt.ts
778
913
  import {
@@ -854,6 +989,11 @@ ${userMessage}
854
989
  functionality: "redacted reasoning messages"
855
990
  });
856
991
  }
992
+ case "file": {
993
+ throw new UnsupportedFunctionalityError2({
994
+ functionality: "file attachments"
995
+ });
996
+ }
857
997
  default: {
858
998
  const _exhaustiveCheck = part;
859
999
  throw new Error(
@@ -1100,7 +1240,9 @@ var OpenRouterCompletionLanguageModel = class {
1100
1240
  choice == null ? void 0 : choice.logprobs
1101
1241
  );
1102
1242
  if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
1103
- if (logprobs === void 0) logprobs = [];
1243
+ if (logprobs === void 0) {
1244
+ logprobs = [];
1245
+ }
1104
1246
  logprobs.push(...mappedLogprobs);
1105
1247
  }
1106
1248
  },
@@ -1120,26 +1262,27 @@ var OpenRouterCompletionLanguageModel = class {
1120
1262
  };
1121
1263
  }
1122
1264
  };
1123
- var OpenRouterCompletionChunkSchema = z3.union([
1124
- z3.object({
1125
- id: z3.string().optional(),
1126
- model: z3.string().optional(),
1127
- choices: z3.array(
1128
- z3.object({
1129
- text: z3.string(),
1130
- reasoning: z3.string().nullish().optional(),
1131
- finish_reason: z3.string().nullish(),
1132
- index: z3.number(),
1133
- logprobs: z3.object({
1134
- tokens: z3.array(z3.string()),
1135
- token_logprobs: z3.array(z3.number()),
1136
- top_logprobs: z3.array(z3.record(z3.string(), z3.number())).nullable()
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()
1137
1280
  }).nullable().optional()
1138
1281
  })
1139
1282
  ),
1140
- usage: z3.object({
1141
- prompt_tokens: z3.number(),
1142
- completion_tokens: z3.number()
1283
+ usage: z4.object({
1284
+ prompt_tokens: z4.number(),
1285
+ completion_tokens: z4.number()
1143
1286
  }).optional().nullable()
1144
1287
  }),
1145
1288
  OpenRouterErrorResponseSchema