@openrouter/ai-sdk-provider 1.0.0-beta.2 → 1.0.0-beta.4

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.d.mts CHANGED
@@ -18,7 +18,7 @@ type OpenRouterChatSettings = {
18
18
  token from being generated.
19
19
  */
20
20
  logitBias?: Record<number, number>;
21
- /**s
21
+ /**
22
22
  Return the log probabilities of the tokens. Including logprobs will increase
23
23
  the response size and can slow down response times. However, it can
24
24
  be useful to better understand how the model is behaving.
package/dist/index.d.ts CHANGED
@@ -18,7 +18,7 @@ type OpenRouterChatSettings = {
18
18
  token from being generated.
19
19
  */
20
20
  logitBias?: Record<number, number>;
21
- /**s
21
+ /**
22
22
  Return the log probabilities of the tokens. Including logprobs will increase
23
23
  the response size and can slow down response times. However, it can
24
24
  be useful to better understand how the model is behaving.
package/dist/index.js CHANGED
@@ -978,6 +978,39 @@ function mapOpenRouterFinishReason(finishReason) {
978
978
  }
979
979
  }
980
980
 
981
+ // src/chat/is-url.ts
982
+ function isUrl({
983
+ url,
984
+ protocols
985
+ }) {
986
+ try {
987
+ const urlObj = new URL(url);
988
+ return protocols.has(urlObj.protocol);
989
+ } catch (_) {
990
+ return false;
991
+ }
992
+ }
993
+
994
+ // src/chat/file-url-utils.ts
995
+ function getFileUrl({
996
+ part,
997
+ defaultMediaType
998
+ }) {
999
+ var _a15, _b;
1000
+ if (part.data instanceof Uint8Array) {
1001
+ const base64 = convertUint8ArrayToBase64(part.data);
1002
+ return `data:${(_a15 = part.mediaType) != null ? _a15 : defaultMediaType};base64,${base64}`;
1003
+ }
1004
+ const stringUrl = part.data.toString();
1005
+ if (isUrl({
1006
+ url: stringUrl,
1007
+ protocols: /* @__PURE__ */ new Set(["http:", "https:"])
1008
+ })) {
1009
+ return stringUrl;
1010
+ }
1011
+ return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b = part.mediaType) != null ? _b : defaultMediaType};base64,${stringUrl}`;
1012
+ }
1013
+
981
1014
  // src/chat/convert-to-openrouter-chat-messages.ts
982
1015
  function getCacheControl(providerMetadata) {
983
1016
  var _a15, _b, _c;
@@ -1017,7 +1050,7 @@ function convertToOpenRouterChatMessages(prompt) {
1017
1050
  const messageCacheControl = getCacheControl(providerOptions);
1018
1051
  const contentParts = content.map(
1019
1052
  (part) => {
1020
- var _a16, _b2, _c2, _d, _e, _f, _g;
1053
+ var _a16, _b2, _c2, _d, _e, _f;
1021
1054
  const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
1022
1055
  switch (part.type) {
1023
1056
  case "text":
@@ -1027,29 +1060,49 @@ function convertToOpenRouterChatMessages(prompt) {
1027
1060
  // For text parts, only use part-specific cache control
1028
1061
  cache_control: cacheControl
1029
1062
  };
1030
- case "file":
1063
+ case "file": {
1031
1064
  if ((_b2 = part.mediaType) == null ? void 0 : _b2.startsWith("image/")) {
1065
+ const url = getFileUrl({
1066
+ part,
1067
+ defaultMediaType: "image/jpeg"
1068
+ });
1032
1069
  return {
1033
1070
  type: "image_url",
1034
1071
  image_url: {
1035
- url: part.data instanceof URL ? part.data.toString() : `data:${(_c2 = part.mediaType) != null ? _c2 : "image/jpeg"};base64,${convertUint8ArrayToBase64(
1036
- part.data instanceof Uint8Array ? part.data : new Uint8Array()
1037
- )}`
1072
+ url
1038
1073
  },
1039
1074
  // For image parts, use part-specific or message-level cache control
1040
1075
  cache_control: cacheControl
1041
1076
  };
1042
1077
  }
1078
+ const fileName = String(
1079
+ (_f = (_e = (_d = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d.filename) != null ? _e : part.filename) != null ? _f : ""
1080
+ );
1081
+ const fileData = getFileUrl({
1082
+ part,
1083
+ defaultMediaType: "application/pdf"
1084
+ });
1085
+ if (isUrl({
1086
+ url: fileData,
1087
+ protocols: /* @__PURE__ */ new Set(["http:", "https:"])
1088
+ })) {
1089
+ return {
1090
+ type: "file",
1091
+ file: {
1092
+ filename: fileName,
1093
+ file_data: fileData
1094
+ }
1095
+ };
1096
+ }
1043
1097
  return {
1044
1098
  type: "file",
1045
1099
  file: {
1046
- filename: String(
1047
- (_g = (_f = (_e = (_d = part.providerOptions) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.filename) != null ? _f : part.filename) != null ? _g : ""
1048
- ),
1049
- file_data: part.data instanceof Uint8Array ? `data:${part.mediaType};base64,${convertUint8ArrayToBase64(part.data)}` : `data:${part.mediaType};base64,${part.data}`
1100
+ filename: fileName,
1101
+ file_data: fileData
1050
1102
  },
1051
1103
  cache_control: cacheControl
1052
1104
  };
1105
+ }
1053
1106
  default: {
1054
1107
  return {
1055
1108
  type: "text",
@@ -1515,11 +1568,14 @@ var OpenRouterChatLanguageModel = class {
1515
1568
  const openrouterUsage = {};
1516
1569
  let textStarted = false;
1517
1570
  let reasoningStarted = false;
1571
+ let textId;
1572
+ let reasoningId;
1573
+ let openrouterResponseId;
1518
1574
  return {
1519
1575
  stream: response.pipeThrough(
1520
1576
  new TransformStream({
1521
1577
  transform(chunk, controller) {
1522
- var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1578
+ var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
1523
1579
  if (!chunk.success) {
1524
1580
  finishReason = "error";
1525
1581
  controller.enqueue({ type: "error", error: chunk.error });
@@ -1532,6 +1588,7 @@ var OpenRouterChatLanguageModel = class {
1532
1588
  return;
1533
1589
  }
1534
1590
  if (value.id) {
1591
+ openrouterResponseId = value.id;
1535
1592
  controller.enqueue({
1536
1593
  type: "response-metadata",
1537
1594
  id: value.id
@@ -1576,30 +1633,32 @@ var OpenRouterChatLanguageModel = class {
1576
1633
  const delta = choice.delta;
1577
1634
  if (delta.content != null) {
1578
1635
  if (!textStarted) {
1636
+ textId = openrouterResponseId || generateId();
1579
1637
  controller.enqueue({
1580
1638
  type: "text-start",
1581
- id: generateId()
1639
+ id: textId
1582
1640
  });
1583
1641
  textStarted = true;
1584
1642
  }
1585
1643
  controller.enqueue({
1586
1644
  type: "text-delta",
1587
1645
  delta: delta.content,
1588
- id: generateId()
1646
+ id: textId || generateId()
1589
1647
  });
1590
1648
  }
1591
1649
  const emitReasoningChunk = (chunkText) => {
1592
1650
  if (!reasoningStarted) {
1651
+ reasoningId = openrouterResponseId || generateId();
1593
1652
  controller.enqueue({
1594
1653
  type: "reasoning-start",
1595
- id: generateId()
1654
+ id: reasoningId
1596
1655
  });
1597
1656
  reasoningStarted = true;
1598
1657
  }
1599
1658
  controller.enqueue({
1600
1659
  type: "reasoning-delta",
1601
1660
  delta: chunkText,
1602
- id: generateId()
1661
+ id: reasoningId || generateId()
1603
1662
  });
1604
1663
  };
1605
1664
  if (delta.reasoning != null) {
@@ -1662,6 +1721,7 @@ var OpenRouterChatLanguageModel = class {
1662
1721
  name: toolCallDelta.function.name,
1663
1722
  arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
1664
1723
  },
1724
+ inputStarted: false,
1665
1725
  sent: false
1666
1726
  };
1667
1727
  const toolCall2 = toolCalls[index];
@@ -1669,6 +1729,7 @@ var OpenRouterChatLanguageModel = class {
1669
1729
  throw new Error("Tool call is missing");
1670
1730
  }
1671
1731
  if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
1732
+ toolCall2.inputStarted = true;
1672
1733
  controller.enqueue({
1673
1734
  type: "tool-input-start",
1674
1735
  id: toolCall2.id,
@@ -1697,25 +1758,26 @@ var OpenRouterChatLanguageModel = class {
1697
1758
  if (toolCall == null) {
1698
1759
  throw new Error("Tool call is missing");
1699
1760
  }
1700
- if (((_h = toolCallDelta.function) == null ? void 0 : _h.name) != null) {
1761
+ if (!toolCall.inputStarted) {
1762
+ toolCall.inputStarted = true;
1701
1763
  controller.enqueue({
1702
1764
  type: "tool-input-start",
1703
1765
  id: toolCall.id,
1704
1766
  toolName: toolCall.function.name
1705
1767
  });
1706
1768
  }
1707
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
1708
- toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : "";
1769
+ if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
1770
+ toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
1709
1771
  }
1710
1772
  controller.enqueue({
1711
1773
  type: "tool-input-delta",
1712
1774
  id: toolCall.id,
1713
- delta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
1775
+ delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
1714
1776
  });
1715
- if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1777
+ if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1716
1778
  controller.enqueue({
1717
1779
  type: "tool-call",
1718
- toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
1780
+ toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
1719
1781
  toolName: toolCall.function.name,
1720
1782
  input: toolCall.function.arguments
1721
1783
  });
@@ -1741,10 +1803,16 @@ var OpenRouterChatLanguageModel = class {
1741
1803
  }
1742
1804
  }
1743
1805
  if (textStarted) {
1744
- controller.enqueue({ type: "text-end", id: generateId() });
1806
+ controller.enqueue({
1807
+ type: "text-end",
1808
+ id: textId || generateId()
1809
+ });
1745
1810
  }
1746
1811
  if (reasoningStarted) {
1747
- controller.enqueue({ type: "reasoning-end", id: generateId() });
1812
+ controller.enqueue({
1813
+ type: "reasoning-end",
1814
+ id: reasoningId || generateId()
1815
+ });
1748
1816
  }
1749
1817
  controller.enqueue({
1750
1818
  type: "finish",