@openrouter/ai-sdk-provider 1.0.0-beta.0 → 1.0.0-beta.1

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
@@ -919,9 +919,6 @@ function withoutTrailingSlash(url) {
919
919
  return url == null ? void 0 : url.replace(/\/$/, "");
920
920
  }
921
921
 
922
- // src/chat/index.ts
923
- var import_v44 = require("zod/v4");
924
-
925
922
  // src/schemas/reasoning-details.ts
926
923
  var import_v4 = require("zod/v4");
927
924
  var ReasoningDetailSummarySchema = import_v4.z.object({
@@ -1170,6 +1167,105 @@ function getChatCompletionToolChoice(toolChoice) {
1170
1167
  }
1171
1168
  }
1172
1169
 
1170
+ // src/chat/schemas.ts
1171
+ var import_v44 = require("zod/v4");
1172
+ var OpenRouterChatCompletionBaseResponseSchema = import_v44.z.object({
1173
+ id: import_v44.z.string().optional(),
1174
+ model: import_v44.z.string().optional(),
1175
+ usage: import_v44.z.object({
1176
+ prompt_tokens: import_v44.z.number(),
1177
+ prompt_tokens_details: import_v44.z.object({
1178
+ cached_tokens: import_v44.z.number()
1179
+ }).nullish(),
1180
+ completion_tokens: import_v44.z.number(),
1181
+ completion_tokens_details: import_v44.z.object({
1182
+ reasoning_tokens: import_v44.z.number()
1183
+ }).nullish(),
1184
+ total_tokens: import_v44.z.number(),
1185
+ cost: import_v44.z.number().optional()
1186
+ }).nullish()
1187
+ });
1188
+ var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
1189
+ choices: import_v44.z.array(
1190
+ import_v44.z.object({
1191
+ message: import_v44.z.object({
1192
+ role: import_v44.z.literal("assistant"),
1193
+ content: import_v44.z.string().nullable().optional(),
1194
+ reasoning: import_v44.z.string().nullable().optional(),
1195
+ reasoning_details: ReasoningDetailArraySchema.nullish(),
1196
+ tool_calls: import_v44.z.array(
1197
+ import_v44.z.object({
1198
+ id: import_v44.z.string().optional().nullable(),
1199
+ type: import_v44.z.literal("function"),
1200
+ function: import_v44.z.object({
1201
+ name: import_v44.z.string(),
1202
+ arguments: import_v44.z.string()
1203
+ })
1204
+ })
1205
+ ).optional()
1206
+ }),
1207
+ index: import_v44.z.number().nullish(),
1208
+ logprobs: import_v44.z.object({
1209
+ content: import_v44.z.array(
1210
+ import_v44.z.object({
1211
+ token: import_v44.z.string(),
1212
+ logprob: import_v44.z.number(),
1213
+ top_logprobs: import_v44.z.array(
1214
+ import_v44.z.object({
1215
+ token: import_v44.z.string(),
1216
+ logprob: import_v44.z.number()
1217
+ })
1218
+ )
1219
+ })
1220
+ ).nullable()
1221
+ }).nullable().optional(),
1222
+ finish_reason: import_v44.z.string().optional().nullable()
1223
+ })
1224
+ )
1225
+ });
1226
+ var OpenRouterStreamChatCompletionChunkSchema = import_v44.z.union([
1227
+ OpenRouterChatCompletionBaseResponseSchema.extend({
1228
+ choices: import_v44.z.array(
1229
+ import_v44.z.object({
1230
+ delta: import_v44.z.object({
1231
+ role: import_v44.z.enum(["assistant"]).optional(),
1232
+ content: import_v44.z.string().nullish(),
1233
+ reasoning: import_v44.z.string().nullish().optional(),
1234
+ reasoning_details: ReasoningDetailArraySchema.nullish(),
1235
+ tool_calls: import_v44.z.array(
1236
+ import_v44.z.object({
1237
+ index: import_v44.z.number().nullish(),
1238
+ id: import_v44.z.string().nullish(),
1239
+ type: import_v44.z.literal("function").optional(),
1240
+ function: import_v44.z.object({
1241
+ name: import_v44.z.string().nullish(),
1242
+ arguments: import_v44.z.string().nullish()
1243
+ })
1244
+ })
1245
+ ).nullish()
1246
+ }).nullish(),
1247
+ logprobs: import_v44.z.object({
1248
+ content: import_v44.z.array(
1249
+ import_v44.z.object({
1250
+ token: import_v44.z.string(),
1251
+ logprob: import_v44.z.number(),
1252
+ top_logprobs: import_v44.z.array(
1253
+ import_v44.z.object({
1254
+ token: import_v44.z.string(),
1255
+ logprob: import_v44.z.number()
1256
+ })
1257
+ )
1258
+ })
1259
+ ).nullable()
1260
+ }).nullish(),
1261
+ finish_reason: import_v44.z.string().nullable().optional(),
1262
+ index: import_v44.z.number().nullish()
1263
+ })
1264
+ )
1265
+ }),
1266
+ OpenRouterErrorResponseSchema
1267
+ ]);
1268
+
1173
1269
  // src/chat/index.ts
1174
1270
  var OpenRouterChatLanguageModel = class {
1175
1271
  constructor(modelId, settings, config) {
@@ -1414,7 +1510,7 @@ var OpenRouterChatLanguageModel = class {
1414
1510
  stream: response.pipeThrough(
1415
1511
  new TransformStream({
1416
1512
  transform(chunk, controller) {
1417
- var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
1513
+ var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1418
1514
  if (!chunk.success) {
1419
1515
  finishReason = "error";
1420
1516
  controller.enqueue({ type: "error", error: chunk.error });
@@ -1531,7 +1627,7 @@ var OpenRouterChatLanguageModel = class {
1531
1627
  }
1532
1628
  if (delta.tool_calls != null) {
1533
1629
  for (const toolCallDelta of delta.tool_calls) {
1534
- const index = toolCallDelta.index;
1630
+ const index = (_c = toolCallDelta.index) != null ? _c : toolCalls.length - 1;
1535
1631
  if (toolCalls[index] == null) {
1536
1632
  if (toolCallDelta.type !== "function") {
1537
1633
  throw new InvalidResponseDataError({
@@ -1545,7 +1641,7 @@ var OpenRouterChatLanguageModel = class {
1545
1641
  message: `Expected 'id' to be a string.`
1546
1642
  });
1547
1643
  }
1548
- if (((_c = toolCallDelta.function) == null ? void 0 : _c.name) == null) {
1644
+ if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) {
1549
1645
  throw new InvalidResponseDataError({
1550
1646
  data: toolCallDelta,
1551
1647
  message: `Expected 'function.name' to be a string.`
@@ -1556,7 +1652,7 @@ var OpenRouterChatLanguageModel = class {
1556
1652
  type: "function",
1557
1653
  function: {
1558
1654
  name: toolCallDelta.function.name,
1559
- arguments: (_d = toolCallDelta.function.arguments) != null ? _d : ""
1655
+ arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
1560
1656
  },
1561
1657
  sent: false
1562
1658
  };
@@ -1564,7 +1660,7 @@ var OpenRouterChatLanguageModel = class {
1564
1660
  if (toolCall2 == null) {
1565
1661
  throw new Error("Tool call is missing");
1566
1662
  }
1567
- if (((_e = toolCall2.function) == null ? void 0 : _e.name) != null && ((_f = toolCall2.function) == null ? void 0 : _f.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
1663
+ if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
1568
1664
  controller.enqueue({
1569
1665
  type: "tool-input-start",
1570
1666
  id: toolCall2.id,
@@ -1593,25 +1689,25 @@ var OpenRouterChatLanguageModel = class {
1593
1689
  if (toolCall == null) {
1594
1690
  throw new Error("Tool call is missing");
1595
1691
  }
1596
- if (((_g = toolCallDelta.function) == null ? void 0 : _g.name) != null) {
1692
+ if (((_h = toolCallDelta.function) == null ? void 0 : _h.name) != null) {
1597
1693
  controller.enqueue({
1598
1694
  type: "tool-input-start",
1599
1695
  id: toolCall.id,
1600
1696
  toolName: toolCall.function.name
1601
1697
  });
1602
1698
  }
1603
- if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
1604
- toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
1699
+ if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
1700
+ toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : "";
1605
1701
  }
1606
1702
  controller.enqueue({
1607
1703
  type: "tool-input-delta",
1608
1704
  id: toolCall.id,
1609
- delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
1705
+ delta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
1610
1706
  });
1611
- if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1707
+ if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1612
1708
  controller.enqueue({
1613
1709
  type: "tool-call",
1614
- toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
1710
+ toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
1615
1711
  toolName: toolCall.function.name,
1616
1712
  input: toolCall.function.arguments
1617
1713
  });
@@ -1655,105 +1751,6 @@ var OpenRouterChatLanguageModel = class {
1655
1751
  };
1656
1752
  }
1657
1753
  };
1658
- var OpenRouterChatCompletionBaseResponseSchema = import_v44.z.object({
1659
- id: import_v44.z.string().optional(),
1660
- model: import_v44.z.string().optional(),
1661
- usage: import_v44.z.object({
1662
- prompt_tokens: import_v44.z.number(),
1663
- prompt_tokens_details: import_v44.z.object({
1664
- cached_tokens: import_v44.z.number()
1665
- }).nullish(),
1666
- completion_tokens: import_v44.z.number(),
1667
- completion_tokens_details: import_v44.z.object({
1668
- reasoning_tokens: import_v44.z.number()
1669
- }).nullish(),
1670
- total_tokens: import_v44.z.number(),
1671
- cost: import_v44.z.number().optional()
1672
- }).nullish()
1673
- });
1674
- var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
1675
- choices: import_v44.z.array(
1676
- import_v44.z.object({
1677
- message: import_v44.z.object({
1678
- role: import_v44.z.literal("assistant"),
1679
- content: import_v44.z.string().nullable().optional(),
1680
- reasoning: import_v44.z.string().nullable().optional(),
1681
- reasoning_details: ReasoningDetailArraySchema.nullish(),
1682
- tool_calls: import_v44.z.array(
1683
- import_v44.z.object({
1684
- id: import_v44.z.string().optional().nullable(),
1685
- type: import_v44.z.literal("function"),
1686
- function: import_v44.z.object({
1687
- name: import_v44.z.string(),
1688
- arguments: import_v44.z.string()
1689
- })
1690
- })
1691
- ).optional()
1692
- }),
1693
- index: import_v44.z.number(),
1694
- logprobs: import_v44.z.object({
1695
- content: import_v44.z.array(
1696
- import_v44.z.object({
1697
- token: import_v44.z.string(),
1698
- logprob: import_v44.z.number(),
1699
- top_logprobs: import_v44.z.array(
1700
- import_v44.z.object({
1701
- token: import_v44.z.string(),
1702
- logprob: import_v44.z.number()
1703
- })
1704
- )
1705
- })
1706
- ).nullable()
1707
- }).nullable().optional(),
1708
- finish_reason: import_v44.z.string().optional().nullable()
1709
- })
1710
- )
1711
- });
1712
- var OpenRouterStreamChatCompletionChunkSchema = import_v44.z.union([
1713
- OpenRouterChatCompletionBaseResponseSchema.extend({
1714
- choices: import_v44.z.array(
1715
- import_v44.z.object({
1716
- delta: import_v44.z.object({
1717
- role: import_v44.z.enum(["assistant"]).optional(),
1718
- content: import_v44.z.string().nullish(),
1719
- reasoning: import_v44.z.string().nullish().optional(),
1720
- reasoning_details: ReasoningDetailArraySchema.nullish(),
1721
- tool_calls: import_v44.z.array(
1722
- import_v44.z.object({
1723
- index: import_v44.z.number(),
1724
- id: import_v44.z.string().nullish(),
1725
- type: import_v44.z.literal("function").optional(),
1726
- function: import_v44.z.object({
1727
- name: import_v44.z.string().nullish(),
1728
- arguments: import_v44.z.string().nullish()
1729
- })
1730
- })
1731
- ).nullish()
1732
- }).nullish(),
1733
- logprobs: import_v44.z.object({
1734
- content: import_v44.z.array(
1735
- import_v44.z.object({
1736
- token: import_v44.z.string(),
1737
- logprob: import_v44.z.number(),
1738
- top_logprobs: import_v44.z.array(
1739
- import_v44.z.object({
1740
- token: import_v44.z.string(),
1741
- logprob: import_v44.z.number()
1742
- })
1743
- )
1744
- })
1745
- ).nullable()
1746
- }).nullish(),
1747
- finish_reason: import_v44.z.string().nullable().optional(),
1748
- index: import_v44.z.number()
1749
- })
1750
- )
1751
- }),
1752
- OpenRouterErrorResponseSchema
1753
- ]);
1754
-
1755
- // src/completion/index.ts
1756
- var import_v45 = require("zod/v4");
1757
1754
 
1758
1755
  // src/completion/convert-to-openrouter-completion-prompt.ts
1759
1756
  function convertToOpenRouterCompletionPrompt({
@@ -1858,7 +1855,8 @@ ${assistantMessage}
1858
1855
  };
1859
1856
  }
1860
1857
 
1861
- // src/completion/index.ts
1858
+ // src/completion/schemas.ts
1859
+ var import_v45 = require("zod/v4");
1862
1860
  var OpenRouterCompletionChunkSchema = import_v45.z.union([
1863
1861
  import_v45.z.object({
1864
1862
  id: import_v45.z.string().optional(),
@@ -1869,7 +1867,7 @@ var OpenRouterCompletionChunkSchema = import_v45.z.union([
1869
1867
  reasoning: import_v45.z.string().nullish().optional(),
1870
1868
  reasoning_details: ReasoningDetailArraySchema.nullish(),
1871
1869
  finish_reason: import_v45.z.string().nullish(),
1872
- index: import_v45.z.number(),
1870
+ index: import_v45.z.number().nullish(),
1873
1871
  logprobs: import_v45.z.object({
1874
1872
  tokens: import_v45.z.array(import_v45.z.string()),
1875
1873
  token_logprobs: import_v45.z.array(import_v45.z.number()),
@@ -1892,6 +1890,8 @@ var OpenRouterCompletionChunkSchema = import_v45.z.union([
1892
1890
  }),
1893
1891
  OpenRouterErrorResponseSchema
1894
1892
  ]);
1893
+
1894
+ // src/completion/index.ts
1895
1895
  var OpenRouterCompletionLanguageModel = class {
1896
1896
  constructor(modelId, settings, config) {
1897
1897
  this.specificationVersion = "v2";