@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.
@@ -839,9 +839,6 @@ function convertUint8ArrayToBase64(array) {
839
839
  return btoa(latin1string);
840
840
  }
841
841
 
842
- // src/chat/index.ts
843
- import { z as z5 } from "zod/v4";
844
-
845
842
  // src/schemas/reasoning-details.ts
846
843
  import { z } from "zod/v4";
847
844
  var ReasoningDetailSummarySchema = z.object({
@@ -1090,6 +1087,105 @@ function getChatCompletionToolChoice(toolChoice) {
1090
1087
  }
1091
1088
  }
1092
1089
 
1090
+ // src/chat/schemas.ts
1091
+ import { z as z5 } from "zod/v4";
1092
+ var OpenRouterChatCompletionBaseResponseSchema = z5.object({
1093
+ id: z5.string().optional(),
1094
+ model: z5.string().optional(),
1095
+ usage: z5.object({
1096
+ prompt_tokens: z5.number(),
1097
+ prompt_tokens_details: z5.object({
1098
+ cached_tokens: z5.number()
1099
+ }).nullish(),
1100
+ completion_tokens: z5.number(),
1101
+ completion_tokens_details: z5.object({
1102
+ reasoning_tokens: z5.number()
1103
+ }).nullish(),
1104
+ total_tokens: z5.number(),
1105
+ cost: z5.number().optional()
1106
+ }).nullish()
1107
+ });
1108
+ var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
1109
+ choices: z5.array(
1110
+ z5.object({
1111
+ message: z5.object({
1112
+ role: z5.literal("assistant"),
1113
+ content: z5.string().nullable().optional(),
1114
+ reasoning: z5.string().nullable().optional(),
1115
+ reasoning_details: ReasoningDetailArraySchema.nullish(),
1116
+ tool_calls: z5.array(
1117
+ z5.object({
1118
+ id: z5.string().optional().nullable(),
1119
+ type: z5.literal("function"),
1120
+ function: z5.object({
1121
+ name: z5.string(),
1122
+ arguments: z5.string()
1123
+ })
1124
+ })
1125
+ ).optional()
1126
+ }),
1127
+ index: z5.number().nullish(),
1128
+ logprobs: z5.object({
1129
+ content: z5.array(
1130
+ z5.object({
1131
+ token: z5.string(),
1132
+ logprob: z5.number(),
1133
+ top_logprobs: z5.array(
1134
+ z5.object({
1135
+ token: z5.string(),
1136
+ logprob: z5.number()
1137
+ })
1138
+ )
1139
+ })
1140
+ ).nullable()
1141
+ }).nullable().optional(),
1142
+ finish_reason: z5.string().optional().nullable()
1143
+ })
1144
+ )
1145
+ });
1146
+ var OpenRouterStreamChatCompletionChunkSchema = z5.union([
1147
+ OpenRouterChatCompletionBaseResponseSchema.extend({
1148
+ choices: z5.array(
1149
+ z5.object({
1150
+ delta: z5.object({
1151
+ role: z5.enum(["assistant"]).optional(),
1152
+ content: z5.string().nullish(),
1153
+ reasoning: z5.string().nullish().optional(),
1154
+ reasoning_details: ReasoningDetailArraySchema.nullish(),
1155
+ tool_calls: z5.array(
1156
+ z5.object({
1157
+ index: z5.number().nullish(),
1158
+ id: z5.string().nullish(),
1159
+ type: z5.literal("function").optional(),
1160
+ function: z5.object({
1161
+ name: z5.string().nullish(),
1162
+ arguments: z5.string().nullish()
1163
+ })
1164
+ })
1165
+ ).nullish()
1166
+ }).nullish(),
1167
+ logprobs: z5.object({
1168
+ content: z5.array(
1169
+ z5.object({
1170
+ token: z5.string(),
1171
+ logprob: z5.number(),
1172
+ top_logprobs: z5.array(
1173
+ z5.object({
1174
+ token: z5.string(),
1175
+ logprob: z5.number()
1176
+ })
1177
+ )
1178
+ })
1179
+ ).nullable()
1180
+ }).nullish(),
1181
+ finish_reason: z5.string().nullable().optional(),
1182
+ index: z5.number().nullish()
1183
+ })
1184
+ )
1185
+ }),
1186
+ OpenRouterErrorResponseSchema
1187
+ ]);
1188
+
1093
1189
  // src/chat/index.ts
1094
1190
  var OpenRouterChatLanguageModel = class {
1095
1191
  constructor(modelId, settings, config) {
@@ -1334,7 +1430,7 @@ var OpenRouterChatLanguageModel = class {
1334
1430
  stream: response.pipeThrough(
1335
1431
  new TransformStream({
1336
1432
  transform(chunk, controller) {
1337
- var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
1433
+ var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1338
1434
  if (!chunk.success) {
1339
1435
  finishReason = "error";
1340
1436
  controller.enqueue({ type: "error", error: chunk.error });
@@ -1451,7 +1547,7 @@ var OpenRouterChatLanguageModel = class {
1451
1547
  }
1452
1548
  if (delta.tool_calls != null) {
1453
1549
  for (const toolCallDelta of delta.tool_calls) {
1454
- const index = toolCallDelta.index;
1550
+ const index = (_c = toolCallDelta.index) != null ? _c : toolCalls.length - 1;
1455
1551
  if (toolCalls[index] == null) {
1456
1552
  if (toolCallDelta.type !== "function") {
1457
1553
  throw new InvalidResponseDataError({
@@ -1465,7 +1561,7 @@ var OpenRouterChatLanguageModel = class {
1465
1561
  message: `Expected 'id' to be a string.`
1466
1562
  });
1467
1563
  }
1468
- if (((_c = toolCallDelta.function) == null ? void 0 : _c.name) == null) {
1564
+ if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) {
1469
1565
  throw new InvalidResponseDataError({
1470
1566
  data: toolCallDelta,
1471
1567
  message: `Expected 'function.name' to be a string.`
@@ -1476,7 +1572,7 @@ var OpenRouterChatLanguageModel = class {
1476
1572
  type: "function",
1477
1573
  function: {
1478
1574
  name: toolCallDelta.function.name,
1479
- arguments: (_d = toolCallDelta.function.arguments) != null ? _d : ""
1575
+ arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
1480
1576
  },
1481
1577
  sent: false
1482
1578
  };
@@ -1484,7 +1580,7 @@ var OpenRouterChatLanguageModel = class {
1484
1580
  if (toolCall2 == null) {
1485
1581
  throw new Error("Tool call is missing");
1486
1582
  }
1487
- if (((_e = toolCall2.function) == null ? void 0 : _e.name) != null && ((_f = toolCall2.function) == null ? void 0 : _f.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
1583
+ if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
1488
1584
  controller.enqueue({
1489
1585
  type: "tool-input-start",
1490
1586
  id: toolCall2.id,
@@ -1513,25 +1609,25 @@ var OpenRouterChatLanguageModel = class {
1513
1609
  if (toolCall == null) {
1514
1610
  throw new Error("Tool call is missing");
1515
1611
  }
1516
- if (((_g = toolCallDelta.function) == null ? void 0 : _g.name) != null) {
1612
+ if (((_h = toolCallDelta.function) == null ? void 0 : _h.name) != null) {
1517
1613
  controller.enqueue({
1518
1614
  type: "tool-input-start",
1519
1615
  id: toolCall.id,
1520
1616
  toolName: toolCall.function.name
1521
1617
  });
1522
1618
  }
1523
- if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
1524
- toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
1619
+ if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
1620
+ toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : "";
1525
1621
  }
1526
1622
  controller.enqueue({
1527
1623
  type: "tool-input-delta",
1528
1624
  id: toolCall.id,
1529
- delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
1625
+ delta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
1530
1626
  });
1531
- if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1627
+ if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1532
1628
  controller.enqueue({
1533
1629
  type: "tool-call",
1534
- toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
1630
+ toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
1535
1631
  toolName: toolCall.function.name,
1536
1632
  input: toolCall.function.arguments
1537
1633
  });
@@ -1575,105 +1671,6 @@ var OpenRouterChatLanguageModel = class {
1575
1671
  };
1576
1672
  }
1577
1673
  };
1578
- var OpenRouterChatCompletionBaseResponseSchema = z5.object({
1579
- id: z5.string().optional(),
1580
- model: z5.string().optional(),
1581
- usage: z5.object({
1582
- prompt_tokens: z5.number(),
1583
- prompt_tokens_details: z5.object({
1584
- cached_tokens: z5.number()
1585
- }).nullish(),
1586
- completion_tokens: z5.number(),
1587
- completion_tokens_details: z5.object({
1588
- reasoning_tokens: z5.number()
1589
- }).nullish(),
1590
- total_tokens: z5.number(),
1591
- cost: z5.number().optional()
1592
- }).nullish()
1593
- });
1594
- var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
1595
- choices: z5.array(
1596
- z5.object({
1597
- message: z5.object({
1598
- role: z5.literal("assistant"),
1599
- content: z5.string().nullable().optional(),
1600
- reasoning: z5.string().nullable().optional(),
1601
- reasoning_details: ReasoningDetailArraySchema.nullish(),
1602
- tool_calls: z5.array(
1603
- z5.object({
1604
- id: z5.string().optional().nullable(),
1605
- type: z5.literal("function"),
1606
- function: z5.object({
1607
- name: z5.string(),
1608
- arguments: z5.string()
1609
- })
1610
- })
1611
- ).optional()
1612
- }),
1613
- index: z5.number(),
1614
- logprobs: z5.object({
1615
- content: z5.array(
1616
- z5.object({
1617
- token: z5.string(),
1618
- logprob: z5.number(),
1619
- top_logprobs: z5.array(
1620
- z5.object({
1621
- token: z5.string(),
1622
- logprob: z5.number()
1623
- })
1624
- )
1625
- })
1626
- ).nullable()
1627
- }).nullable().optional(),
1628
- finish_reason: z5.string().optional().nullable()
1629
- })
1630
- )
1631
- });
1632
- var OpenRouterStreamChatCompletionChunkSchema = z5.union([
1633
- OpenRouterChatCompletionBaseResponseSchema.extend({
1634
- choices: z5.array(
1635
- z5.object({
1636
- delta: z5.object({
1637
- role: z5.enum(["assistant"]).optional(),
1638
- content: z5.string().nullish(),
1639
- reasoning: z5.string().nullish().optional(),
1640
- reasoning_details: ReasoningDetailArraySchema.nullish(),
1641
- tool_calls: z5.array(
1642
- z5.object({
1643
- index: z5.number(),
1644
- id: z5.string().nullish(),
1645
- type: z5.literal("function").optional(),
1646
- function: z5.object({
1647
- name: z5.string().nullish(),
1648
- arguments: z5.string().nullish()
1649
- })
1650
- })
1651
- ).nullish()
1652
- }).nullish(),
1653
- logprobs: z5.object({
1654
- content: z5.array(
1655
- z5.object({
1656
- token: z5.string(),
1657
- logprob: z5.number(),
1658
- top_logprobs: z5.array(
1659
- z5.object({
1660
- token: z5.string(),
1661
- logprob: z5.number()
1662
- })
1663
- )
1664
- })
1665
- ).nullable()
1666
- }).nullish(),
1667
- finish_reason: z5.string().nullable().optional(),
1668
- index: z5.number()
1669
- })
1670
- )
1671
- }),
1672
- OpenRouterErrorResponseSchema
1673
- ]);
1674
-
1675
- // src/completion/index.ts
1676
- import { z as z6 } from "zod/v4";
1677
1674
 
1678
1675
  // src/completion/convert-to-openrouter-completion-prompt.ts
1679
1676
  function convertToOpenRouterCompletionPrompt({
@@ -1778,7 +1775,8 @@ ${assistantMessage}
1778
1775
  };
1779
1776
  }
1780
1777
 
1781
- // src/completion/index.ts
1778
+ // src/completion/schemas.ts
1779
+ import { z as z6 } from "zod/v4";
1782
1780
  var OpenRouterCompletionChunkSchema = z6.union([
1783
1781
  z6.object({
1784
1782
  id: z6.string().optional(),
@@ -1789,7 +1787,7 @@ var OpenRouterCompletionChunkSchema = z6.union([
1789
1787
  reasoning: z6.string().nullish().optional(),
1790
1788
  reasoning_details: ReasoningDetailArraySchema.nullish(),
1791
1789
  finish_reason: z6.string().nullish(),
1792
- index: z6.number(),
1790
+ index: z6.number().nullish(),
1793
1791
  logprobs: z6.object({
1794
1792
  tokens: z6.array(z6.string()),
1795
1793
  token_logprobs: z6.array(z6.number()),
@@ -1812,6 +1810,8 @@ var OpenRouterCompletionChunkSchema = z6.union([
1812
1810
  }),
1813
1811
  OpenRouterErrorResponseSchema
1814
1812
  ]);
1813
+
1814
+ // src/completion/index.ts
1815
1815
  var OpenRouterCompletionLanguageModel = class {
1816
1816
  constructor(modelId, settings, config) {
1817
1817
  this.specificationVersion = "v2";