@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.js CHANGED
@@ -58,10 +58,36 @@ module.exports = __toCommonJS(index_exports);
58
58
  // src/openrouter-facade.ts
59
59
  var import_provider_utils5 = require("@ai-sdk/provider-utils");
60
60
 
61
+ // src/schemas/reasoning-details.ts
62
+ var import_zod = require("zod");
63
+ var ReasoningDetailSummarySchema = import_zod.z.object({
64
+ type: import_zod.z.literal("reasoning.summary" /* Summary */),
65
+ summary: import_zod.z.string()
66
+ });
67
+ var ReasoningDetailEncryptedSchema = import_zod.z.object({
68
+ type: import_zod.z.literal("reasoning.encrypted" /* Encrypted */),
69
+ data: import_zod.z.string()
70
+ });
71
+ var ReasoningDetailTextSchema = import_zod.z.object({
72
+ type: import_zod.z.literal("reasoning.text" /* Text */),
73
+ text: import_zod.z.string().nullish(),
74
+ signature: import_zod.z.string().nullish()
75
+ });
76
+ var ReasoningDetailUnionSchema = import_zod.z.union([
77
+ ReasoningDetailSummarySchema,
78
+ ReasoningDetailEncryptedSchema,
79
+ ReasoningDetailTextSchema
80
+ ]);
81
+ var ReasoningDetailsWithUnknownSchema = import_zod.z.union([
82
+ ReasoningDetailUnionSchema,
83
+ import_zod.z.unknown().transform(() => null)
84
+ ]);
85
+ var ReasoningDetailArraySchema = import_zod.z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
86
+
61
87
  // src/openrouter-chat-language-model.ts
62
88
  var import_provider = require("@ai-sdk/provider");
63
89
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
64
- var import_zod2 = require("zod");
90
+ var import_zod3 = require("zod");
65
91
 
66
92
  // src/convert-to-openrouter-chat-messages.ts
67
93
  var import_provider_utils = require("@ai-sdk/provider-utils");
@@ -96,14 +122,15 @@ function convertToOpenRouterChatMessages(prompt) {
96
122
  const messageCacheControl = getCacheControl(providerMetadata);
97
123
  const contentParts = content.map(
98
124
  (part) => {
99
- var _a2, _b2, _c2, _d, _e, _f;
125
+ var _a2, _b2, _c2, _d;
126
+ const cacheControl = (_a2 = getCacheControl(part.providerMetadata)) != null ? _a2 : messageCacheControl;
100
127
  switch (part.type) {
101
128
  case "text":
102
129
  return {
103
130
  type: "text",
104
131
  text: part.text,
105
132
  // For text parts, only use part-specific cache control
106
- cache_control: (_a2 = getCacheControl(part.providerMetadata)) != null ? _a2 : messageCacheControl
133
+ cache_control: cacheControl
107
134
  };
108
135
  case "image":
109
136
  return {
@@ -114,18 +141,18 @@ function convertToOpenRouterChatMessages(prompt) {
114
141
  )}`
115
142
  },
116
143
  // For image parts, use part-specific or message-level cache control
117
- cache_control: (_c2 = getCacheControl(part.providerMetadata)) != null ? _c2 : messageCacheControl
144
+ cache_control: cacheControl
118
145
  };
119
146
  case "file":
120
147
  return {
121
148
  type: "file",
122
149
  file: {
123
150
  filename: String(
124
- (_e = (_d = part.providerMetadata) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.filename
151
+ (_d = (_c2 = part.providerMetadata) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d.filename
125
152
  ),
126
153
  file_data: part.data instanceof Uint8Array ? `data:${part.mimeType};base64,${(0, import_provider_utils.convertUint8ArrayToBase64)(part.data)}` : `data:${part.mimeType};base64,${part.data}`
127
154
  },
128
- cache_control: (_f = getCacheControl(part.providerMetadata)) != null ? _f : messageCacheControl
155
+ cache_control: cacheControl
129
156
  };
130
157
  default: {
131
158
  const _exhaustiveCheck = part;
@@ -144,6 +171,8 @@ function convertToOpenRouterChatMessages(prompt) {
144
171
  }
145
172
  case "assistant": {
146
173
  let text = "";
174
+ let reasoning = "";
175
+ const reasoningDetails = [];
147
176
  const toolCalls = [];
148
177
  for (const part of content) {
149
178
  switch (part.type) {
@@ -162,9 +191,23 @@ function convertToOpenRouterChatMessages(prompt) {
162
191
  });
163
192
  break;
164
193
  }
165
- // TODO: Handle reasoning and redacted-reasoning
166
- case "reasoning":
167
- case "redacted-reasoning":
194
+ case "reasoning": {
195
+ reasoning += part.text;
196
+ reasoningDetails.push({
197
+ type: "reasoning.text" /* Text */,
198
+ text: part.text,
199
+ signature: part.signature
200
+ });
201
+ break;
202
+ }
203
+ case "redacted-reasoning": {
204
+ reasoningDetails.push({
205
+ type: "reasoning.encrypted" /* Encrypted */,
206
+ data: part.data
207
+ });
208
+ break;
209
+ }
210
+ case "file":
168
211
  break;
169
212
  default: {
170
213
  const _exhaustiveCheck = part;
@@ -176,6 +219,8 @@ function convertToOpenRouterChatMessages(prompt) {
176
219
  role: "assistant",
177
220
  content: text,
178
221
  tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
222
+ reasoning: reasoning || void 0,
223
+ reasoning_details: reasoningDetails.length > 0 ? reasoningDetails : void 0,
179
224
  cache_control: getCacheControl(providerMetadata)
180
225
  });
181
226
  break;
@@ -232,13 +277,13 @@ function mapOpenRouterFinishReason(finishReason) {
232
277
 
233
278
  // src/openrouter-error.ts
234
279
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
235
- var import_zod = require("zod");
236
- var OpenRouterErrorResponseSchema = import_zod.z.object({
237
- error: import_zod.z.object({
238
- message: import_zod.z.string(),
239
- type: import_zod.z.string(),
240
- param: import_zod.z.any().nullable(),
241
- code: import_zod.z.string().nullable()
280
+ var import_zod2 = require("zod");
281
+ var OpenRouterErrorResponseSchema = import_zod2.z.object({
282
+ error: import_zod2.z.object({
283
+ message: import_zod2.z.string(),
284
+ type: import_zod2.z.string(),
285
+ param: import_zod2.z.any().nullable(),
286
+ code: import_zod2.z.string().nullable()
242
287
  })
243
288
  });
244
289
  var openrouterFailedResponseHandler = (0, import_provider_utils2.createJsonErrorResponseHandler)({
@@ -385,13 +430,56 @@ var OpenRouterChatLanguageModel = class {
385
430
  };
386
431
  }
387
432
  const hasProviderMetadata = Object.keys(providerMetadata).length > 0;
433
+ const reasoningDetails = (_h = choice.message.reasoning_details) != null ? _h : [];
434
+ const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
435
+ var _a2;
436
+ switch (detail.type) {
437
+ case "reasoning.text" /* Text */: {
438
+ if (detail.text) {
439
+ return {
440
+ type: "text",
441
+ text: detail.text,
442
+ signature: (_a2 = detail.signature) != null ? _a2 : void 0
443
+ };
444
+ }
445
+ break;
446
+ }
447
+ case "reasoning.summary" /* Summary */: {
448
+ if (detail.summary) {
449
+ return {
450
+ type: "text",
451
+ text: detail.summary
452
+ };
453
+ }
454
+ break;
455
+ }
456
+ case "reasoning.encrypted" /* Encrypted */: {
457
+ if (detail.data) {
458
+ return {
459
+ type: "redacted",
460
+ data: detail.data
461
+ };
462
+ }
463
+ break;
464
+ }
465
+ default: {
466
+ detail;
467
+ }
468
+ }
469
+ return null;
470
+ }).filter((p) => p !== null) : choice.message.reasoning ? [
471
+ {
472
+ type: "text",
473
+ text: choice.message.reasoning
474
+ }
475
+ ] : [];
388
476
  return __spreadValues({
389
477
  response: {
390
478
  id: response.id,
391
479
  modelId: response.model
392
480
  },
393
- text: (_h = choice.message.content) != null ? _h : void 0,
394
- reasoning: (_i = choice.message.reasoning) != null ? _i : void 0,
481
+ text: (_i = choice.message.content) != null ? _i : void 0,
482
+ reasoning,
395
483
  toolCalls: (_j = choice.message.tool_calls) == null ? void 0 : _j.map((toolCall) => {
396
484
  var _a2;
397
485
  return {
@@ -510,11 +598,56 @@ var OpenRouterChatLanguageModel = class {
510
598
  textDelta: delta.reasoning
511
599
  });
512
600
  }
601
+ if (delta.reasoning_details && delta.reasoning_details.length > 0) {
602
+ for (const detail of delta.reasoning_details) {
603
+ switch (detail.type) {
604
+ case "reasoning.text" /* Text */: {
605
+ if (detail.text) {
606
+ controller.enqueue({
607
+ type: "reasoning",
608
+ textDelta: detail.text
609
+ });
610
+ }
611
+ if (detail.signature) {
612
+ controller.enqueue({
613
+ type: "reasoning-signature",
614
+ signature: detail.signature
615
+ });
616
+ }
617
+ break;
618
+ }
619
+ case "reasoning.encrypted" /* Encrypted */: {
620
+ if (detail.data) {
621
+ controller.enqueue({
622
+ type: "redacted-reasoning",
623
+ data: detail.data
624
+ });
625
+ }
626
+ break;
627
+ }
628
+ case "reasoning.summary" /* Summary */: {
629
+ if (detail.summary) {
630
+ controller.enqueue({
631
+ type: "reasoning",
632
+ textDelta: detail.summary
633
+ });
634
+ }
635
+ break;
636
+ }
637
+ default: {
638
+ detail;
639
+ break;
640
+ }
641
+ }
642
+ }
643
+ }
513
644
  const mappedLogprobs = mapOpenRouterChatLogProbsOutput(
514
645
  choice == null ? void 0 : choice.logprobs
515
646
  );
516
647
  if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
517
- if (logprobs === void 0) logprobs = [];
648
+ if (logprobs === void 0) {
649
+ logprobs = [];
650
+ }
518
651
  logprobs.push(...mappedLogprobs);
519
652
  }
520
653
  if (delta.tool_calls != null) {
@@ -637,95 +770,97 @@ var OpenRouterChatLanguageModel = class {
637
770
  };
638
771
  }
639
772
  };
640
- var OpenRouterChatCompletionBaseResponseSchema = import_zod2.z.object({
641
- id: import_zod2.z.string().optional(),
642
- model: import_zod2.z.string().optional(),
643
- usage: import_zod2.z.object({
644
- prompt_tokens: import_zod2.z.number(),
645
- prompt_tokens_details: import_zod2.z.object({
646
- cached_tokens: import_zod2.z.number()
773
+ var OpenRouterChatCompletionBaseResponseSchema = import_zod3.z.object({
774
+ id: import_zod3.z.string().optional(),
775
+ model: import_zod3.z.string().optional(),
776
+ usage: import_zod3.z.object({
777
+ prompt_tokens: import_zod3.z.number(),
778
+ prompt_tokens_details: import_zod3.z.object({
779
+ cached_tokens: import_zod3.z.number()
647
780
  }).optional(),
648
- completion_tokens: import_zod2.z.number(),
649
- completion_tokens_details: import_zod2.z.object({
650
- reasoning_tokens: import_zod2.z.number()
781
+ completion_tokens: import_zod3.z.number(),
782
+ completion_tokens_details: import_zod3.z.object({
783
+ reasoning_tokens: import_zod3.z.number()
651
784
  }).optional(),
652
- total_tokens: import_zod2.z.number(),
653
- cost: import_zod2.z.number().optional()
785
+ total_tokens: import_zod3.z.number(),
786
+ cost: import_zod3.z.number().optional()
654
787
  }).nullish()
655
788
  });
656
789
  var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
657
- choices: import_zod2.z.array(
658
- import_zod2.z.object({
659
- message: import_zod2.z.object({
660
- role: import_zod2.z.literal("assistant"),
661
- content: import_zod2.z.string().nullable().optional(),
662
- reasoning: import_zod2.z.string().nullable().optional(),
663
- tool_calls: import_zod2.z.array(
664
- import_zod2.z.object({
665
- id: import_zod2.z.string().optional().nullable(),
666
- type: import_zod2.z.literal("function"),
667
- function: import_zod2.z.object({
668
- name: import_zod2.z.string(),
669
- arguments: import_zod2.z.string()
790
+ choices: import_zod3.z.array(
791
+ import_zod3.z.object({
792
+ message: import_zod3.z.object({
793
+ role: import_zod3.z.literal("assistant"),
794
+ content: import_zod3.z.string().nullable().optional(),
795
+ reasoning: import_zod3.z.string().nullable().optional(),
796
+ reasoning_details: ReasoningDetailArraySchema.nullish(),
797
+ tool_calls: import_zod3.z.array(
798
+ import_zod3.z.object({
799
+ id: import_zod3.z.string().optional().nullable(),
800
+ type: import_zod3.z.literal("function"),
801
+ function: import_zod3.z.object({
802
+ name: import_zod3.z.string(),
803
+ arguments: import_zod3.z.string()
670
804
  })
671
805
  })
672
806
  ).optional()
673
807
  }),
674
- index: import_zod2.z.number(),
675
- logprobs: import_zod2.z.object({
676
- content: import_zod2.z.array(
677
- import_zod2.z.object({
678
- token: import_zod2.z.string(),
679
- logprob: import_zod2.z.number(),
680
- top_logprobs: import_zod2.z.array(
681
- import_zod2.z.object({
682
- token: import_zod2.z.string(),
683
- logprob: import_zod2.z.number()
808
+ index: import_zod3.z.number(),
809
+ logprobs: import_zod3.z.object({
810
+ content: import_zod3.z.array(
811
+ import_zod3.z.object({
812
+ token: import_zod3.z.string(),
813
+ logprob: import_zod3.z.number(),
814
+ top_logprobs: import_zod3.z.array(
815
+ import_zod3.z.object({
816
+ token: import_zod3.z.string(),
817
+ logprob: import_zod3.z.number()
684
818
  })
685
819
  )
686
820
  })
687
821
  ).nullable()
688
822
  }).nullable().optional(),
689
- finish_reason: import_zod2.z.string().optional().nullable()
823
+ finish_reason: import_zod3.z.string().optional().nullable()
690
824
  })
691
825
  )
692
826
  });
693
- var OpenRouterStreamChatCompletionChunkSchema = import_zod2.z.union([
827
+ var OpenRouterStreamChatCompletionChunkSchema = import_zod3.z.union([
694
828
  OpenRouterChatCompletionBaseResponseSchema.extend({
695
- choices: import_zod2.z.array(
696
- import_zod2.z.object({
697
- delta: import_zod2.z.object({
698
- role: import_zod2.z.enum(["assistant"]).optional(),
699
- content: import_zod2.z.string().nullish(),
700
- reasoning: import_zod2.z.string().nullish().optional(),
701
- tool_calls: import_zod2.z.array(
702
- import_zod2.z.object({
703
- index: import_zod2.z.number(),
704
- id: import_zod2.z.string().nullish(),
705
- type: import_zod2.z.literal("function").optional(),
706
- function: import_zod2.z.object({
707
- name: import_zod2.z.string().nullish(),
708
- arguments: import_zod2.z.string().nullish()
829
+ choices: import_zod3.z.array(
830
+ import_zod3.z.object({
831
+ delta: import_zod3.z.object({
832
+ role: import_zod3.z.enum(["assistant"]).optional(),
833
+ content: import_zod3.z.string().nullish(),
834
+ reasoning: import_zod3.z.string().nullish().optional(),
835
+ reasoning_details: ReasoningDetailArraySchema.nullish(),
836
+ tool_calls: import_zod3.z.array(
837
+ import_zod3.z.object({
838
+ index: import_zod3.z.number(),
839
+ id: import_zod3.z.string().nullish(),
840
+ type: import_zod3.z.literal("function").optional(),
841
+ function: import_zod3.z.object({
842
+ name: import_zod3.z.string().nullish(),
843
+ arguments: import_zod3.z.string().nullish()
709
844
  })
710
845
  })
711
846
  ).nullish()
712
847
  }).nullish(),
713
- logprobs: import_zod2.z.object({
714
- content: import_zod2.z.array(
715
- import_zod2.z.object({
716
- token: import_zod2.z.string(),
717
- logprob: import_zod2.z.number(),
718
- top_logprobs: import_zod2.z.array(
719
- import_zod2.z.object({
720
- token: import_zod2.z.string(),
721
- logprob: import_zod2.z.number()
848
+ logprobs: import_zod3.z.object({
849
+ content: import_zod3.z.array(
850
+ import_zod3.z.object({
851
+ token: import_zod3.z.string(),
852
+ logprob: import_zod3.z.number(),
853
+ top_logprobs: import_zod3.z.array(
854
+ import_zod3.z.object({
855
+ token: import_zod3.z.string(),
856
+ logprob: import_zod3.z.number()
722
857
  })
723
858
  )
724
859
  })
725
860
  ).nullable()
726
861
  }).nullish(),
727
- finish_reason: import_zod2.z.string().nullable().optional(),
728
- index: import_zod2.z.number()
862
+ finish_reason: import_zod3.z.string().nullable().optional(),
863
+ index: import_zod3.z.number()
729
864
  })
730
865
  )
731
866
  }),
@@ -785,7 +920,7 @@ function prepareToolsAndToolChoice(mode) {
785
920
  // src/openrouter-completion-language-model.ts
786
921
  var import_provider3 = require("@ai-sdk/provider");
787
922
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
788
- var import_zod3 = require("zod");
923
+ var import_zod4 = require("zod");
789
924
 
790
925
  // src/convert-to-openrouter-completion-prompt.ts
791
926
  var import_provider2 = require("@ai-sdk/provider");
@@ -864,6 +999,11 @@ ${userMessage}
864
999
  functionality: "redacted reasoning messages"
865
1000
  });
866
1001
  }
1002
+ case "file": {
1003
+ throw new import_provider2.UnsupportedFunctionalityError({
1004
+ functionality: "file attachments"
1005
+ });
1006
+ }
867
1007
  default: {
868
1008
  const _exhaustiveCheck = part;
869
1009
  throw new Error(
@@ -1110,7 +1250,9 @@ var OpenRouterCompletionLanguageModel = class {
1110
1250
  choice == null ? void 0 : choice.logprobs
1111
1251
  );
1112
1252
  if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
1113
- if (logprobs === void 0) logprobs = [];
1253
+ if (logprobs === void 0) {
1254
+ logprobs = [];
1255
+ }
1114
1256
  logprobs.push(...mappedLogprobs);
1115
1257
  }
1116
1258
  },
@@ -1130,26 +1272,27 @@ var OpenRouterCompletionLanguageModel = class {
1130
1272
  };
1131
1273
  }
1132
1274
  };
1133
- var OpenRouterCompletionChunkSchema = import_zod3.z.union([
1134
- import_zod3.z.object({
1135
- id: import_zod3.z.string().optional(),
1136
- model: import_zod3.z.string().optional(),
1137
- choices: import_zod3.z.array(
1138
- import_zod3.z.object({
1139
- text: import_zod3.z.string(),
1140
- reasoning: import_zod3.z.string().nullish().optional(),
1141
- finish_reason: import_zod3.z.string().nullish(),
1142
- index: import_zod3.z.number(),
1143
- logprobs: import_zod3.z.object({
1144
- tokens: import_zod3.z.array(import_zod3.z.string()),
1145
- token_logprobs: import_zod3.z.array(import_zod3.z.number()),
1146
- top_logprobs: import_zod3.z.array(import_zod3.z.record(import_zod3.z.string(), import_zod3.z.number())).nullable()
1275
+ var OpenRouterCompletionChunkSchema = import_zod4.z.union([
1276
+ import_zod4.z.object({
1277
+ id: import_zod4.z.string().optional(),
1278
+ model: import_zod4.z.string().optional(),
1279
+ choices: import_zod4.z.array(
1280
+ import_zod4.z.object({
1281
+ text: import_zod4.z.string(),
1282
+ reasoning: import_zod4.z.string().nullish().optional(),
1283
+ reasoning_details: ReasoningDetailArraySchema.nullish(),
1284
+ finish_reason: import_zod4.z.string().nullish(),
1285
+ index: import_zod4.z.number(),
1286
+ logprobs: import_zod4.z.object({
1287
+ tokens: import_zod4.z.array(import_zod4.z.string()),
1288
+ token_logprobs: import_zod4.z.array(import_zod4.z.number()),
1289
+ top_logprobs: import_zod4.z.array(import_zod4.z.record(import_zod4.z.string(), import_zod4.z.number())).nullable()
1147
1290
  }).nullable().optional()
1148
1291
  })
1149
1292
  ),
1150
- usage: import_zod3.z.object({
1151
- prompt_tokens: import_zod3.z.number(),
1152
- completion_tokens: import_zod3.z.number()
1293
+ usage: import_zod4.z.object({
1294
+ prompt_tokens: import_zod4.z.number(),
1295
+ completion_tokens: import_zod4.z.number()
1153
1296
  }).optional().nullable()
1154
1297
  }),
1155
1298
  OpenRouterErrorResponseSchema