@openrouter/ai-sdk-provider 1.2.7 → 1.3.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
@@ -915,9 +915,9 @@ var ReasoningFormat = /* @__PURE__ */ ((ReasoningFormat2) => {
915
915
  // src/schemas/reasoning-details.ts
916
916
  var CommonReasoningDetailSchema = z.object({
917
917
  id: z.string().nullish(),
918
- format: z.nativeEnum(ReasoningFormat).nullish(),
918
+ format: z.enum(ReasoningFormat).nullish(),
919
919
  index: z.number().optional()
920
- }).passthrough();
920
+ }).loose();
921
921
  var ReasoningDetailSummarySchema = z.object({
922
922
  type: z.literal("reasoning.summary" /* Summary */),
923
923
  summary: z.string()
@@ -995,7 +995,7 @@ var OpenRouterProviderMetadataSchema = z3.object({
995
995
  cost: z3.number().optional(),
996
996
  costDetails: z3.object({
997
997
  upstreamInferenceCost: z3.number()
998
- }).passthrough()
998
+ }).passthrough().optional()
999
999
  }).passthrough()
1000
1000
  }).passthrough();
1001
1001
  var OpenRouterProviderOptionsSchema = z3.object({
@@ -1114,9 +1114,8 @@ function getCacheControl(providerMetadata) {
1114
1114
  return (_c = (_b = (_a15 = openrouter2 == null ? void 0 : openrouter2.cacheControl) != null ? _a15 : openrouter2 == null ? void 0 : openrouter2.cache_control) != null ? _b : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
1115
1115
  }
1116
1116
  function convertToOpenRouterChatMessages(prompt) {
1117
- var _a15, _b, _c, _d, _e, _f;
1117
+ var _a15, _b, _c, _d, _e, _f, _g, _h;
1118
1118
  const messages = [];
1119
- const accumulatedReasoningDetails = [];
1120
1119
  for (const { role, content, providerOptions } of prompt) {
1121
1120
  switch (role) {
1122
1121
  case "system": {
@@ -1146,7 +1145,7 @@ function convertToOpenRouterChatMessages(prompt) {
1146
1145
  const messageCacheControl = getCacheControl(providerOptions);
1147
1146
  const contentParts = content.map(
1148
1147
  (part) => {
1149
- var _a16, _b2, _c2, _d2, _e2, _f2, _g;
1148
+ var _a16, _b2, _c2, _d2, _e2, _f2, _g2;
1150
1149
  const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
1151
1150
  switch (part.type) {
1152
1151
  case "text":
@@ -1179,7 +1178,7 @@ function convertToOpenRouterChatMessages(prompt) {
1179
1178
  };
1180
1179
  }
1181
1180
  const fileName = String(
1182
- (_g = (_f2 = (_e2 = (_d2 = part.providerOptions) == null ? void 0 : _d2.openrouter) == null ? void 0 : _e2.filename) != null ? _f2 : part.filename) != null ? _g : ""
1181
+ (_g2 = (_f2 = (_e2 = (_d2 = part.providerOptions) == null ? void 0 : _d2.openrouter) == null ? void 0 : _e2.filename) != null ? _f2 : part.filename) != null ? _g2 : ""
1183
1182
  );
1184
1183
  const fileData = getFileUrl({
1185
1184
  part,
@@ -1226,6 +1225,7 @@ function convertToOpenRouterChatMessages(prompt) {
1226
1225
  let text = "";
1227
1226
  let reasoning = "";
1228
1227
  const toolCalls = [];
1228
+ const accumulatedReasoningDetails = [];
1229
1229
  for (const part of content) {
1230
1230
  switch (part.type) {
1231
1231
  case "text": {
@@ -1251,6 +1251,12 @@ function convertToOpenRouterChatMessages(prompt) {
1251
1251
  }
1252
1252
  case "reasoning": {
1253
1253
  reasoning += part.text;
1254
+ const parsedPartProviderOptions = OpenRouterProviderOptionsSchema.safeParse(part.providerOptions);
1255
+ if (parsedPartProviderOptions.success && ((_e = (_d = parsedPartProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details)) {
1256
+ accumulatedReasoningDetails.push(
1257
+ ...parsedPartProviderOptions.data.openrouter.reasoning_details
1258
+ );
1259
+ }
1254
1260
  break;
1255
1261
  }
1256
1262
  case "file":
@@ -1261,7 +1267,7 @@ function convertToOpenRouterChatMessages(prompt) {
1261
1267
  }
1262
1268
  }
1263
1269
  const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
1264
- const messageReasoningDetails = parsedProviderOptions.success ? (_e = (_d = parsedProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details : void 0;
1270
+ const messageReasoningDetails = parsedProviderOptions.success ? (_g = (_f = parsedProviderOptions.data) == null ? void 0 : _f.openrouter) == null ? void 0 : _g.reasoning_details : void 0;
1265
1271
  const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : accumulatedReasoningDetails.length > 0 ? accumulatedReasoningDetails : void 0;
1266
1272
  messages.push({
1267
1273
  role: "assistant",
@@ -1280,7 +1286,7 @@ function convertToOpenRouterChatMessages(prompt) {
1280
1286
  role: "tool",
1281
1287
  tool_call_id: toolResponse.toolCallId,
1282
1288
  content: content2,
1283
- cache_control: (_f = getCacheControl(providerOptions)) != null ? _f : getCacheControl(toolResponse.providerOptions)
1289
+ cache_control: (_h = getCacheControl(providerOptions)) != null ? _h : getCacheControl(toolResponse.providerOptions)
1284
1290
  });
1285
1291
  }
1286
1292
  break;
@@ -1596,7 +1602,9 @@ var OpenRouterChatLanguageModel = class {
1596
1602
  plugins: this.settings.plugins,
1597
1603
  web_search_options: this.settings.web_search_options,
1598
1604
  // Provider routing settings:
1599
- provider: this.settings.provider
1605
+ provider: this.settings.provider,
1606
+ // Debug settings:
1607
+ debug: this.settings.debug
1600
1608
  }, this.config.extraBody), this.settings.extraBody);
1601
1609
  if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null) {
1602
1610
  return __spreadProps(__spreadValues({}, baseArgs), {
@@ -1631,7 +1639,7 @@ var OpenRouterChatLanguageModel = class {
1631
1639
  return baseArgs;
1632
1640
  }
1633
1641
  async doGenerate(options) {
1634
- 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;
1642
+ var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
1635
1643
  const providerOptions = options.providerOptions || {};
1636
1644
  const openrouterOptions = providerOptions.openrouter || {};
1637
1645
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -1798,21 +1806,24 @@ var OpenRouterChatLanguageModel = class {
1798
1806
  openrouter: OpenRouterProviderMetadataSchema.parse({
1799
1807
  provider: (_k = response.provider) != null ? _k : "",
1800
1808
  reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
1801
- usage: {
1809
+ usage: __spreadValues(__spreadValues(__spreadValues({
1802
1810
  promptTokens: (_m = usageInfo.inputTokens) != null ? _m : 0,
1803
1811
  completionTokens: (_n = usageInfo.outputTokens) != null ? _n : 0,
1804
1812
  totalTokens: (_o = usageInfo.totalTokens) != null ? _o : 0,
1805
- cost: (_p = response.usage) == null ? void 0 : _p.cost,
1813
+ cost: (_p = response.usage) == null ? void 0 : _p.cost
1814
+ }, ((_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? {
1806
1815
  promptTokensDetails: {
1807
- cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
1808
- },
1816
+ cachedTokens: response.usage.prompt_tokens_details.cached_tokens
1817
+ }
1818
+ } : {}), ((_t = (_s = response.usage) == null ? void 0 : _s.completion_tokens_details) == null ? void 0 : _t.reasoning_tokens) != null ? {
1809
1819
  completionTokensDetails: {
1810
- reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
1811
- },
1820
+ reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
1821
+ }
1822
+ } : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.cost_details) == null ? void 0 : _v.upstream_inference_cost) != null ? {
1812
1823
  costDetails: {
1813
- upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
1824
+ upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
1814
1825
  }
1815
- }
1826
+ } : {})
1816
1827
  })
1817
1828
  },
1818
1829
  request: { body: args },
@@ -1869,7 +1880,7 @@ var OpenRouterChatLanguageModel = class {
1869
1880
  stream: response.pipeThrough(
1870
1881
  new TransformStream({
1871
1882
  transform(chunk, controller) {
1872
- var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
1883
+ var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1873
1884
  if (!chunk.success) {
1874
1885
  finishReason = "error";
1875
1886
  controller.enqueue({ type: "error", error: chunk.error });
@@ -1919,6 +1930,12 @@ var OpenRouterChatLanguageModel = class {
1919
1930
  }
1920
1931
  openrouterUsage.cost = value.usage.cost;
1921
1932
  openrouterUsage.totalTokens = value.usage.total_tokens;
1933
+ const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
1934
+ if (upstreamInferenceCost != null) {
1935
+ openrouterUsage.costDetails = {
1936
+ upstreamInferenceCost
1937
+ };
1938
+ }
1922
1939
  }
1923
1940
  const choice = value.choices[0];
1924
1941
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
@@ -1928,16 +1945,18 @@ var OpenRouterChatLanguageModel = class {
1928
1945
  return;
1929
1946
  }
1930
1947
  const delta = choice.delta;
1931
- const emitReasoningChunk = (chunkText) => {
1948
+ const emitReasoningChunk = (chunkText, providerMetadata) => {
1932
1949
  if (!reasoningStarted) {
1933
1950
  reasoningId = openrouterResponseId || generateId();
1934
1951
  controller.enqueue({
1952
+ providerMetadata,
1935
1953
  type: "reasoning-start",
1936
1954
  id: reasoningId
1937
1955
  });
1938
1956
  reasoningStarted = true;
1939
1957
  }
1940
1958
  controller.enqueue({
1959
+ providerMetadata,
1941
1960
  type: "reasoning-delta",
1942
1961
  delta: chunkText,
1943
1962
  id: reasoningId || generateId()
@@ -1949,6 +1968,8 @@ var OpenRouterChatLanguageModel = class {
1949
1968
  const lastDetail = accumulatedReasoningDetails[accumulatedReasoningDetails.length - 1];
1950
1969
  if ((lastDetail == null ? void 0 : lastDetail.type) === "reasoning.text" /* Text */) {
1951
1970
  lastDetail.text = (lastDetail.text || "") + (detail.text || "");
1971
+ lastDetail.signature = lastDetail.signature || detail.signature;
1972
+ lastDetail.format = lastDetail.format || detail.format;
1952
1973
  } else {
1953
1974
  accumulatedReasoningDetails.push(__spreadValues({}, detail));
1954
1975
  }
@@ -1956,23 +1977,28 @@ var OpenRouterChatLanguageModel = class {
1956
1977
  accumulatedReasoningDetails.push(detail);
1957
1978
  }
1958
1979
  }
1980
+ const reasoningMetadata = {
1981
+ openrouter: {
1982
+ reasoning_details: delta.reasoning_details
1983
+ }
1984
+ };
1959
1985
  for (const detail of delta.reasoning_details) {
1960
1986
  switch (detail.type) {
1961
1987
  case "reasoning.text" /* Text */: {
1962
1988
  if (detail.text) {
1963
- emitReasoningChunk(detail.text);
1989
+ emitReasoningChunk(detail.text, reasoningMetadata);
1964
1990
  }
1965
1991
  break;
1966
1992
  }
1967
1993
  case "reasoning.encrypted" /* Encrypted */: {
1968
1994
  if (detail.data) {
1969
- emitReasoningChunk("[REDACTED]");
1995
+ emitReasoningChunk("[REDACTED]", reasoningMetadata);
1970
1996
  }
1971
1997
  break;
1972
1998
  }
1973
1999
  case "reasoning.summary" /* Summary */: {
1974
2000
  if (detail.summary) {
1975
- emitReasoningChunk(detail.summary);
2001
+ emitReasoningChunk(detail.summary, reasoningMetadata);
1976
2002
  }
1977
2003
  break;
1978
2004
  }
@@ -2027,7 +2053,7 @@ var OpenRouterChatLanguageModel = class {
2027
2053
  }
2028
2054
  if (delta.tool_calls != null) {
2029
2055
  for (const toolCallDelta of delta.tool_calls) {
2030
- const index = (_c = toolCallDelta.index) != null ? _c : toolCalls.length - 1;
2056
+ const index = (_d = toolCallDelta.index) != null ? _d : toolCalls.length - 1;
2031
2057
  if (toolCalls[index] == null) {
2032
2058
  if (toolCallDelta.type !== "function") {
2033
2059
  throw new InvalidResponseDataError({
@@ -2041,7 +2067,7 @@ var OpenRouterChatLanguageModel = class {
2041
2067
  message: `Expected 'id' to be a string.`
2042
2068
  });
2043
2069
  }
2044
- if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) {
2070
+ if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) {
2045
2071
  throw new InvalidResponseDataError({
2046
2072
  data: toolCallDelta,
2047
2073
  message: `Expected 'function.name' to be a string.`
@@ -2052,7 +2078,7 @@ var OpenRouterChatLanguageModel = class {
2052
2078
  type: "function",
2053
2079
  function: {
2054
2080
  name: toolCallDelta.function.name,
2055
- arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
2081
+ arguments: (_f = toolCallDelta.function.arguments) != null ? _f : ""
2056
2082
  },
2057
2083
  inputStarted: false,
2058
2084
  sent: false
@@ -2064,7 +2090,7 @@ var OpenRouterChatLanguageModel = class {
2064
2090
  message: `Tool call at index ${index} is missing after creation.`
2065
2091
  });
2066
2092
  }
2067
- if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
2093
+ if (((_g = toolCall2.function) == null ? void 0 : _g.name) != null && ((_h = toolCall2.function) == null ? void 0 : _h.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
2068
2094
  toolCall2.inputStarted = true;
2069
2095
  controller.enqueue({
2070
2096
  type: "tool-input-start",
@@ -2114,18 +2140,18 @@ var OpenRouterChatLanguageModel = class {
2114
2140
  toolName: toolCall.function.name
2115
2141
  });
2116
2142
  }
2117
- if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
2118
- toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
2143
+ if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
2144
+ toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : "";
2119
2145
  }
2120
2146
  controller.enqueue({
2121
2147
  type: "tool-input-delta",
2122
2148
  id: toolCall.id,
2123
- delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
2149
+ delta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
2124
2150
  });
2125
- if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
2151
+ if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall.function.arguments)) {
2126
2152
  controller.enqueue({
2127
2153
  type: "tool-call",
2128
- toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
2154
+ toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
2129
2155
  toolName: toolCall.function.name,
2130
2156
  input: toolCall.function.arguments,
2131
2157
  providerMetadata: {
@@ -2341,7 +2367,10 @@ var OpenRouterCompletionChunkSchema = z8.union([
2341
2367
  reasoning_tokens: z8.number()
2342
2368
  }).passthrough().nullish(),
2343
2369
  total_tokens: z8.number(),
2344
- cost: z8.number().optional()
2370
+ cost: z8.number().optional(),
2371
+ cost_details: z8.object({
2372
+ upstream_inference_cost: z8.number().nullish()
2373
+ }).passthrough().nullish()
2345
2374
  }).passthrough().nullish()
2346
2375
  }).passthrough(),
2347
2376
  OpenRouterErrorResponseSchema
@@ -2514,7 +2543,7 @@ var OpenRouterCompletionLanguageModel = class {
2514
2543
  stream: response.pipeThrough(
2515
2544
  new TransformStream({
2516
2545
  transform(chunk, controller) {
2517
- var _a15, _b;
2546
+ var _a15, _b, _c;
2518
2547
  if (!chunk.success) {
2519
2548
  finishReason = "error";
2520
2549
  controller.enqueue({ type: "error", error: chunk.error });
@@ -2548,6 +2577,12 @@ var OpenRouterCompletionLanguageModel = class {
2548
2577
  }
2549
2578
  openrouterUsage.cost = value.usage.cost;
2550
2579
  openrouterUsage.totalTokens = value.usage.total_tokens;
2580
+ const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
2581
+ if (upstreamInferenceCost != null) {
2582
+ openrouterUsage.costDetails = {
2583
+ upstreamInferenceCost
2584
+ };
2585
+ }
2551
2586
  }
2552
2587
  const choice = value.choices[0];
2553
2588
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
@@ -2643,7 +2678,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
2643
2678
  }
2644
2679
 
2645
2680
  // src/version.ts
2646
- var VERSION = false ? "0.0.0-test" : "1.2.7";
2681
+ var VERSION = false ? "0.0.0-test" : "1.3.0";
2647
2682
 
2648
2683
  // src/provider.ts
2649
2684
  function createOpenRouter(options = {}) {