@openrouter/ai-sdk-provider 1.2.8 → 1.4.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.
@@ -934,9 +934,23 @@ var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
934
934
 
935
935
  // src/schemas/provider-metadata.ts
936
936
  import { z as z3 } from "zod/v4";
937
+ var FileAnnotationSchema = z3.object({
938
+ type: z3.literal("file"),
939
+ file: z3.object({
940
+ hash: z3.string(),
941
+ name: z3.string(),
942
+ content: z3.array(
943
+ z3.object({
944
+ type: z3.string(),
945
+ text: z3.string().optional()
946
+ }).passthrough()
947
+ ).optional()
948
+ }).passthrough()
949
+ });
937
950
  var OpenRouterProviderMetadataSchema = z3.object({
938
951
  provider: z3.string(),
939
952
  reasoning_details: z3.array(ReasoningDetailUnionSchema).optional(),
953
+ annotations: z3.array(FileAnnotationSchema).optional(),
940
954
  usage: z3.object({
941
955
  promptTokens: z3.number(),
942
956
  promptTokensDetails: z3.object({
@@ -950,12 +964,13 @@ var OpenRouterProviderMetadataSchema = z3.object({
950
964
  cost: z3.number().optional(),
951
965
  costDetails: z3.object({
952
966
  upstreamInferenceCost: z3.number()
953
- }).passthrough()
967
+ }).passthrough().optional()
954
968
  }).passthrough()
955
969
  }).passthrough();
956
970
  var OpenRouterProviderOptionsSchema = z3.object({
957
971
  openrouter: z3.object({
958
- reasoning_details: z3.array(ReasoningDetailUnionSchema).optional()
972
+ reasoning_details: z3.array(ReasoningDetailUnionSchema).optional(),
973
+ annotations: z3.array(FileAnnotationSchema).optional()
959
974
  }).optional()
960
975
  }).optional();
961
976
 
@@ -1069,9 +1084,8 @@ function getCacheControl(providerMetadata) {
1069
1084
  return (_c = (_b = (_a15 = openrouter == null ? void 0 : openrouter.cacheControl) != null ? _a15 : openrouter == null ? void 0 : openrouter.cache_control) != null ? _b : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
1070
1085
  }
1071
1086
  function convertToOpenRouterChatMessages(prompt) {
1072
- var _a15, _b, _c, _d, _e, _f;
1087
+ var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1073
1088
  const messages = [];
1074
- const accumulatedReasoningDetails = [];
1075
1089
  for (const { role, content, providerOptions } of prompt) {
1076
1090
  switch (role) {
1077
1091
  case "system": {
@@ -1101,7 +1115,7 @@ function convertToOpenRouterChatMessages(prompt) {
1101
1115
  const messageCacheControl = getCacheControl(providerOptions);
1102
1116
  const contentParts = content.map(
1103
1117
  (part) => {
1104
- var _a16, _b2, _c2, _d2, _e2, _f2, _g;
1118
+ var _a16, _b2, _c2, _d2, _e2, _f2, _g2;
1105
1119
  const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
1106
1120
  switch (part.type) {
1107
1121
  case "text":
@@ -1134,7 +1148,7 @@ function convertToOpenRouterChatMessages(prompt) {
1134
1148
  };
1135
1149
  }
1136
1150
  const fileName = String(
1137
- (_g = (_f2 = (_e2 = (_d2 = part.providerOptions) == null ? void 0 : _d2.openrouter) == null ? void 0 : _e2.filename) != null ? _f2 : part.filename) != null ? _g : ""
1151
+ (_g2 = (_f2 = (_e2 = (_d2 = part.providerOptions) == null ? void 0 : _d2.openrouter) == null ? void 0 : _e2.filename) != null ? _f2 : part.filename) != null ? _g2 : ""
1138
1152
  );
1139
1153
  const fileData = getFileUrl({
1140
1154
  part,
@@ -1181,6 +1195,7 @@ function convertToOpenRouterChatMessages(prompt) {
1181
1195
  let text = "";
1182
1196
  let reasoning = "";
1183
1197
  const toolCalls = [];
1198
+ const accumulatedReasoningDetails = [];
1184
1199
  for (const part of content) {
1185
1200
  switch (part.type) {
1186
1201
  case "text": {
@@ -1206,6 +1221,12 @@ function convertToOpenRouterChatMessages(prompt) {
1206
1221
  }
1207
1222
  case "reasoning": {
1208
1223
  reasoning += part.text;
1224
+ const parsedPartProviderOptions = OpenRouterProviderOptionsSchema.safeParse(part.providerOptions);
1225
+ if (parsedPartProviderOptions.success && ((_e = (_d = parsedPartProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details)) {
1226
+ accumulatedReasoningDetails.push(
1227
+ ...parsedPartProviderOptions.data.openrouter.reasoning_details
1228
+ );
1229
+ }
1209
1230
  break;
1210
1231
  }
1211
1232
  case "file":
@@ -1216,7 +1237,8 @@ function convertToOpenRouterChatMessages(prompt) {
1216
1237
  }
1217
1238
  }
1218
1239
  const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
1219
- const messageReasoningDetails = parsedProviderOptions.success ? (_e = (_d = parsedProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details : void 0;
1240
+ const messageReasoningDetails = parsedProviderOptions.success ? (_g = (_f = parsedProviderOptions.data) == null ? void 0 : _f.openrouter) == null ? void 0 : _g.reasoning_details : void 0;
1241
+ const messageAnnotations = parsedProviderOptions.success ? (_i = (_h = parsedProviderOptions.data) == null ? void 0 : _h.openrouter) == null ? void 0 : _i.annotations : void 0;
1220
1242
  const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : accumulatedReasoningDetails.length > 0 ? accumulatedReasoningDetails : void 0;
1221
1243
  messages.push({
1222
1244
  role: "assistant",
@@ -1224,6 +1246,7 @@ function convertToOpenRouterChatMessages(prompt) {
1224
1246
  tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
1225
1247
  reasoning: reasoning || void 0,
1226
1248
  reasoning_details: finalReasoningDetails,
1249
+ annotations: messageAnnotations,
1227
1250
  cache_control: getCacheControl(providerOptions)
1228
1251
  });
1229
1252
  break;
@@ -1235,7 +1258,7 @@ function convertToOpenRouterChatMessages(prompt) {
1235
1258
  role: "tool",
1236
1259
  tool_call_id: toolResponse.toolCallId,
1237
1260
  content: content2,
1238
- cache_control: (_f = getCacheControl(providerOptions)) != null ? _f : getCacheControl(toolResponse.providerOptions)
1261
+ cache_control: (_j = getCacheControl(providerOptions)) != null ? _j : getCacheControl(toolResponse.providerOptions)
1239
1262
  });
1240
1263
  }
1241
1264
  break;
@@ -1494,6 +1517,7 @@ var OpenRouterChatLanguageModel = class {
1494
1517
  this.specificationVersion = "v2";
1495
1518
  this.provider = "openrouter";
1496
1519
  this.defaultObjectGenerationMode = "tool";
1520
+ this.supportsImageUrls = true;
1497
1521
  this.supportedUrls = {
1498
1522
  "image/*": [
1499
1523
  /^data:image\/[a-zA-Z]+;base64,/,
@@ -1551,7 +1575,9 @@ var OpenRouterChatLanguageModel = class {
1551
1575
  plugins: this.settings.plugins,
1552
1576
  web_search_options: this.settings.web_search_options,
1553
1577
  // Provider routing settings:
1554
- provider: this.settings.provider
1578
+ provider: this.settings.provider,
1579
+ // Debug settings:
1580
+ debug: this.settings.debug
1555
1581
  }, this.config.extraBody), this.settings.extraBody);
1556
1582
  if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null) {
1557
1583
  return __spreadProps(__spreadValues({}, baseArgs), {
@@ -1586,7 +1612,7 @@ var OpenRouterChatLanguageModel = class {
1586
1612
  return baseArgs;
1587
1613
  }
1588
1614
  async doGenerate(options) {
1589
- var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
1615
+ var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
1590
1616
  const providerOptions = options.providerOptions || {};
1591
1617
  const openrouterOptions = providerOptions.openrouter || {};
1592
1618
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -1744,6 +1770,9 @@ var OpenRouterChatLanguageModel = class {
1744
1770
  }
1745
1771
  }
1746
1772
  }
1773
+ const fileAnnotations = (_k = choice.message.annotations) == null ? void 0 : _k.filter(
1774
+ (a) => a.type === "file"
1775
+ );
1747
1776
  return {
1748
1777
  content,
1749
1778
  finishReason: mapOpenRouterFinishReason(choice.finish_reason),
@@ -1751,23 +1780,27 @@ var OpenRouterChatLanguageModel = class {
1751
1780
  warnings: [],
1752
1781
  providerMetadata: {
1753
1782
  openrouter: OpenRouterProviderMetadataSchema.parse({
1754
- provider: (_k = response.provider) != null ? _k : "",
1755
- reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
1756
- usage: {
1757
- promptTokens: (_m = usageInfo.inputTokens) != null ? _m : 0,
1758
- completionTokens: (_n = usageInfo.outputTokens) != null ? _n : 0,
1759
- totalTokens: (_o = usageInfo.totalTokens) != null ? _o : 0,
1760
- cost: (_p = response.usage) == null ? void 0 : _p.cost,
1783
+ provider: (_l = response.provider) != null ? _l : "",
1784
+ reasoning_details: (_m = choice.message.reasoning_details) != null ? _m : [],
1785
+ annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
1786
+ usage: __spreadValues(__spreadValues(__spreadValues({
1787
+ promptTokens: (_n = usageInfo.inputTokens) != null ? _n : 0,
1788
+ completionTokens: (_o = usageInfo.outputTokens) != null ? _o : 0,
1789
+ totalTokens: (_p = usageInfo.totalTokens) != null ? _p : 0,
1790
+ cost: (_q = response.usage) == null ? void 0 : _q.cost
1791
+ }, ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? {
1761
1792
  promptTokensDetails: {
1762
- cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
1763
- },
1793
+ cachedTokens: response.usage.prompt_tokens_details.cached_tokens
1794
+ }
1795
+ } : {}), ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? {
1764
1796
  completionTokensDetails: {
1765
- reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
1766
- },
1797
+ reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
1798
+ }
1799
+ } : {}), ((_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? {
1767
1800
  costDetails: {
1768
- upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
1801
+ upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
1769
1802
  }
1770
- }
1803
+ } : {})
1771
1804
  })
1772
1805
  },
1773
1806
  request: { body: args },
@@ -1824,7 +1857,7 @@ var OpenRouterChatLanguageModel = class {
1824
1857
  stream: response.pipeThrough(
1825
1858
  new TransformStream({
1826
1859
  transform(chunk, controller) {
1827
- var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
1860
+ var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1828
1861
  if (!chunk.success) {
1829
1862
  finishReason = "error";
1830
1863
  controller.enqueue({ type: "error", error: chunk.error });
@@ -1874,6 +1907,12 @@ var OpenRouterChatLanguageModel = class {
1874
1907
  }
1875
1908
  openrouterUsage.cost = value.usage.cost;
1876
1909
  openrouterUsage.totalTokens = value.usage.total_tokens;
1910
+ const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
1911
+ if (upstreamInferenceCost != null) {
1912
+ openrouterUsage.costDetails = {
1913
+ upstreamInferenceCost
1914
+ };
1915
+ }
1877
1916
  }
1878
1917
  const choice = value.choices[0];
1879
1918
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
@@ -1883,16 +1922,18 @@ var OpenRouterChatLanguageModel = class {
1883
1922
  return;
1884
1923
  }
1885
1924
  const delta = choice.delta;
1886
- const emitReasoningChunk = (chunkText) => {
1925
+ const emitReasoningChunk = (chunkText, providerMetadata) => {
1887
1926
  if (!reasoningStarted) {
1888
1927
  reasoningId = openrouterResponseId || generateId();
1889
1928
  controller.enqueue({
1929
+ providerMetadata,
1890
1930
  type: "reasoning-start",
1891
1931
  id: reasoningId
1892
1932
  });
1893
1933
  reasoningStarted = true;
1894
1934
  }
1895
1935
  controller.enqueue({
1936
+ providerMetadata,
1896
1937
  type: "reasoning-delta",
1897
1938
  delta: chunkText,
1898
1939
  id: reasoningId || generateId()
@@ -1913,23 +1954,28 @@ var OpenRouterChatLanguageModel = class {
1913
1954
  accumulatedReasoningDetails.push(detail);
1914
1955
  }
1915
1956
  }
1957
+ const reasoningMetadata = {
1958
+ openrouter: {
1959
+ reasoning_details: delta.reasoning_details
1960
+ }
1961
+ };
1916
1962
  for (const detail of delta.reasoning_details) {
1917
1963
  switch (detail.type) {
1918
1964
  case "reasoning.text" /* Text */: {
1919
1965
  if (detail.text) {
1920
- emitReasoningChunk(detail.text);
1966
+ emitReasoningChunk(detail.text, reasoningMetadata);
1921
1967
  }
1922
1968
  break;
1923
1969
  }
1924
1970
  case "reasoning.encrypted" /* Encrypted */: {
1925
1971
  if (detail.data) {
1926
- emitReasoningChunk("[REDACTED]");
1972
+ emitReasoningChunk("[REDACTED]", reasoningMetadata);
1927
1973
  }
1928
1974
  break;
1929
1975
  }
1930
1976
  case "reasoning.summary" /* Summary */: {
1931
1977
  if (detail.summary) {
1932
- emitReasoningChunk(detail.summary);
1978
+ emitReasoningChunk(detail.summary, reasoningMetadata);
1933
1979
  }
1934
1980
  break;
1935
1981
  }
@@ -1984,7 +2030,7 @@ var OpenRouterChatLanguageModel = class {
1984
2030
  }
1985
2031
  if (delta.tool_calls != null) {
1986
2032
  for (const toolCallDelta of delta.tool_calls) {
1987
- const index = (_c = toolCallDelta.index) != null ? _c : toolCalls.length - 1;
2033
+ const index = (_d = toolCallDelta.index) != null ? _d : toolCalls.length - 1;
1988
2034
  if (toolCalls[index] == null) {
1989
2035
  if (toolCallDelta.type !== "function") {
1990
2036
  throw new InvalidResponseDataError({
@@ -1998,7 +2044,7 @@ var OpenRouterChatLanguageModel = class {
1998
2044
  message: `Expected 'id' to be a string.`
1999
2045
  });
2000
2046
  }
2001
- if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) {
2047
+ if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) {
2002
2048
  throw new InvalidResponseDataError({
2003
2049
  data: toolCallDelta,
2004
2050
  message: `Expected 'function.name' to be a string.`
@@ -2009,7 +2055,7 @@ var OpenRouterChatLanguageModel = class {
2009
2055
  type: "function",
2010
2056
  function: {
2011
2057
  name: toolCallDelta.function.name,
2012
- arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
2058
+ arguments: (_f = toolCallDelta.function.arguments) != null ? _f : ""
2013
2059
  },
2014
2060
  inputStarted: false,
2015
2061
  sent: false
@@ -2021,7 +2067,7 @@ var OpenRouterChatLanguageModel = class {
2021
2067
  message: `Tool call at index ${index} is missing after creation.`
2022
2068
  });
2023
2069
  }
2024
- if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
2070
+ if (((_g = toolCall2.function) == null ? void 0 : _g.name) != null && ((_h = toolCall2.function) == null ? void 0 : _h.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
2025
2071
  toolCall2.inputStarted = true;
2026
2072
  controller.enqueue({
2027
2073
  type: "tool-input-start",
@@ -2071,18 +2117,18 @@ var OpenRouterChatLanguageModel = class {
2071
2117
  toolName: toolCall.function.name
2072
2118
  });
2073
2119
  }
2074
- if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
2075
- toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
2120
+ if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
2121
+ toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : "";
2076
2122
  }
2077
2123
  controller.enqueue({
2078
2124
  type: "tool-input-delta",
2079
2125
  id: toolCall.id,
2080
- delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
2126
+ delta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
2081
2127
  });
2082
- if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
2128
+ if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall.function.arguments)) {
2083
2129
  controller.enqueue({
2084
2130
  type: "tool-call",
2085
- toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
2131
+ toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
2086
2132
  toolName: toolCall.function.name,
2087
2133
  input: toolCall.function.arguments,
2088
2134
  providerMetadata: {
@@ -2298,7 +2344,10 @@ var OpenRouterCompletionChunkSchema = z8.union([
2298
2344
  reasoning_tokens: z8.number()
2299
2345
  }).passthrough().nullish(),
2300
2346
  total_tokens: z8.number(),
2301
- cost: z8.number().optional()
2347
+ cost: z8.number().optional(),
2348
+ cost_details: z8.object({
2349
+ upstream_inference_cost: z8.number().nullish()
2350
+ }).passthrough().nullish()
2302
2351
  }).passthrough().nullish()
2303
2352
  }).passthrough(),
2304
2353
  OpenRouterErrorResponseSchema
@@ -2309,6 +2358,7 @@ var OpenRouterCompletionLanguageModel = class {
2309
2358
  constructor(modelId, settings, config) {
2310
2359
  this.specificationVersion = "v2";
2311
2360
  this.provider = "openrouter";
2361
+ this.supportsImageUrls = true;
2312
2362
  this.supportedUrls = {
2313
2363
  "image/*": [
2314
2364
  /^data:image\/[a-zA-Z]+;base64,/,
@@ -2471,7 +2521,7 @@ var OpenRouterCompletionLanguageModel = class {
2471
2521
  stream: response.pipeThrough(
2472
2522
  new TransformStream({
2473
2523
  transform(chunk, controller) {
2474
- var _a15, _b;
2524
+ var _a15, _b, _c;
2475
2525
  if (!chunk.success) {
2476
2526
  finishReason = "error";
2477
2527
  controller.enqueue({ type: "error", error: chunk.error });
@@ -2505,6 +2555,12 @@ var OpenRouterCompletionLanguageModel = class {
2505
2555
  }
2506
2556
  openrouterUsage.cost = value.usage.cost;
2507
2557
  openrouterUsage.totalTokens = value.usage.total_tokens;
2558
+ const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
2559
+ if (upstreamInferenceCost != null) {
2560
+ openrouterUsage.costDetails = {
2561
+ upstreamInferenceCost
2562
+ };
2563
+ }
2508
2564
  }
2509
2565
  const choice = value.choices[0];
2510
2566
  if ((choice == null ? void 0 : choice.finish_reason) != null) {