@openrouter/ai-sdk-provider 1.0.0-beta.3 → 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.mjs CHANGED
@@ -943,6 +943,39 @@ function mapOpenRouterFinishReason(finishReason) {
943
943
  }
944
944
  }
945
945
 
946
+ // src/chat/is-url.ts
947
+ function isUrl({
948
+ url,
949
+ protocols
950
+ }) {
951
+ try {
952
+ const urlObj = new URL(url);
953
+ return protocols.has(urlObj.protocol);
954
+ } catch (_) {
955
+ return false;
956
+ }
957
+ }
958
+
959
+ // src/chat/file-url-utils.ts
960
+ function getFileUrl({
961
+ part,
962
+ defaultMediaType
963
+ }) {
964
+ var _a15, _b;
965
+ if (part.data instanceof Uint8Array) {
966
+ const base64 = convertUint8ArrayToBase64(part.data);
967
+ return `data:${(_a15 = part.mediaType) != null ? _a15 : defaultMediaType};base64,${base64}`;
968
+ }
969
+ const stringUrl = part.data.toString();
970
+ if (isUrl({
971
+ url: stringUrl,
972
+ protocols: /* @__PURE__ */ new Set(["http:", "https:"])
973
+ })) {
974
+ return stringUrl;
975
+ }
976
+ return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b = part.mediaType) != null ? _b : defaultMediaType};base64,${stringUrl}`;
977
+ }
978
+
946
979
  // src/chat/convert-to-openrouter-chat-messages.ts
947
980
  function getCacheControl(providerMetadata) {
948
981
  var _a15, _b, _c;
@@ -982,7 +1015,7 @@ function convertToOpenRouterChatMessages(prompt) {
982
1015
  const messageCacheControl = getCacheControl(providerOptions);
983
1016
  const contentParts = content.map(
984
1017
  (part) => {
985
- var _a16, _b2, _c2, _d, _e, _f, _g;
1018
+ var _a16, _b2, _c2, _d, _e, _f;
986
1019
  const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
987
1020
  switch (part.type) {
988
1021
  case "text":
@@ -992,29 +1025,49 @@ function convertToOpenRouterChatMessages(prompt) {
992
1025
  // For text parts, only use part-specific cache control
993
1026
  cache_control: cacheControl
994
1027
  };
995
- case "file":
1028
+ case "file": {
996
1029
  if ((_b2 = part.mediaType) == null ? void 0 : _b2.startsWith("image/")) {
1030
+ const url = getFileUrl({
1031
+ part,
1032
+ defaultMediaType: "image/jpeg"
1033
+ });
997
1034
  return {
998
1035
  type: "image_url",
999
1036
  image_url: {
1000
- url: part.data instanceof URL ? part.data.toString() : `data:${(_c2 = part.mediaType) != null ? _c2 : "image/jpeg"};base64,${convertUint8ArrayToBase64(
1001
- part.data instanceof Uint8Array ? part.data : new Uint8Array()
1002
- )}`
1037
+ url
1003
1038
  },
1004
1039
  // For image parts, use part-specific or message-level cache control
1005
1040
  cache_control: cacheControl
1006
1041
  };
1007
1042
  }
1043
+ const fileName = String(
1044
+ (_f = (_e = (_d = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d.filename) != null ? _e : part.filename) != null ? _f : ""
1045
+ );
1046
+ const fileData = getFileUrl({
1047
+ part,
1048
+ defaultMediaType: "application/pdf"
1049
+ });
1050
+ if (isUrl({
1051
+ url: fileData,
1052
+ protocols: /* @__PURE__ */ new Set(["http:", "https:"])
1053
+ })) {
1054
+ return {
1055
+ type: "file",
1056
+ file: {
1057
+ filename: fileName,
1058
+ file_data: fileData
1059
+ }
1060
+ };
1061
+ }
1008
1062
  return {
1009
1063
  type: "file",
1010
1064
  file: {
1011
- filename: String(
1012
- (_g = (_f = (_e = (_d = part.providerOptions) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.filename) != null ? _f : part.filename) != null ? _g : ""
1013
- ),
1014
- file_data: part.data instanceof Uint8Array ? `data:${part.mediaType};base64,${convertUint8ArrayToBase64(part.data)}` : `data:${part.mediaType};base64,${part.data}`
1065
+ filename: fileName,
1066
+ file_data: fileData
1015
1067
  },
1016
1068
  cache_control: cacheControl
1017
1069
  };
1070
+ }
1018
1071
  default: {
1019
1072
  return {
1020
1073
  type: "text",
@@ -1487,7 +1540,7 @@ var OpenRouterChatLanguageModel = class {
1487
1540
  stream: response.pipeThrough(
1488
1541
  new TransformStream({
1489
1542
  transform(chunk, controller) {
1490
- var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1543
+ var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
1491
1544
  if (!chunk.success) {
1492
1545
  finishReason = "error";
1493
1546
  controller.enqueue({ type: "error", error: chunk.error });
@@ -1633,6 +1686,7 @@ var OpenRouterChatLanguageModel = class {
1633
1686
  name: toolCallDelta.function.name,
1634
1687
  arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
1635
1688
  },
1689
+ inputStarted: false,
1636
1690
  sent: false
1637
1691
  };
1638
1692
  const toolCall2 = toolCalls[index];
@@ -1640,6 +1694,7 @@ var OpenRouterChatLanguageModel = class {
1640
1694
  throw new Error("Tool call is missing");
1641
1695
  }
1642
1696
  if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
1697
+ toolCall2.inputStarted = true;
1643
1698
  controller.enqueue({
1644
1699
  type: "tool-input-start",
1645
1700
  id: toolCall2.id,
@@ -1668,25 +1723,26 @@ var OpenRouterChatLanguageModel = class {
1668
1723
  if (toolCall == null) {
1669
1724
  throw new Error("Tool call is missing");
1670
1725
  }
1671
- if (((_h = toolCallDelta.function) == null ? void 0 : _h.name) != null) {
1726
+ if (!toolCall.inputStarted) {
1727
+ toolCall.inputStarted = true;
1672
1728
  controller.enqueue({
1673
1729
  type: "tool-input-start",
1674
1730
  id: toolCall.id,
1675
1731
  toolName: toolCall.function.name
1676
1732
  });
1677
1733
  }
1678
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
1679
- toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : "";
1734
+ if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
1735
+ toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
1680
1736
  }
1681
1737
  controller.enqueue({
1682
1738
  type: "tool-input-delta",
1683
1739
  id: toolCall.id,
1684
- delta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
1740
+ delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
1685
1741
  });
1686
- if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1742
+ if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1687
1743
  controller.enqueue({
1688
1744
  type: "tool-call",
1689
- toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
1745
+ toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
1690
1746
  toolName: toolCall.function.name,
1691
1747
  input: toolCall.function.arguments
1692
1748
  });
@@ -1712,10 +1768,16 @@ var OpenRouterChatLanguageModel = class {
1712
1768
  }
1713
1769
  }
1714
1770
  if (textStarted) {
1715
- controller.enqueue({ type: "text-end", id: textId || generateId() });
1771
+ controller.enqueue({
1772
+ type: "text-end",
1773
+ id: textId || generateId()
1774
+ });
1716
1775
  }
1717
1776
  if (reasoningStarted) {
1718
- controller.enqueue({ type: "reasoning-end", id: reasoningId || generateId() });
1777
+ controller.enqueue({
1778
+ type: "reasoning-end",
1779
+ id: reasoningId || generateId()
1780
+ });
1719
1781
  }
1720
1782
  controller.enqueue({
1721
1783
  type: "finish",