@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.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",
@@ -1522,7 +1575,7 @@ var OpenRouterChatLanguageModel = class {
1522
1575
  stream: response.pipeThrough(
1523
1576
  new TransformStream({
1524
1577
  transform(chunk, controller) {
1525
- 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;
1526
1579
  if (!chunk.success) {
1527
1580
  finishReason = "error";
1528
1581
  controller.enqueue({ type: "error", error: chunk.error });
@@ -1668,6 +1721,7 @@ var OpenRouterChatLanguageModel = class {
1668
1721
  name: toolCallDelta.function.name,
1669
1722
  arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
1670
1723
  },
1724
+ inputStarted: false,
1671
1725
  sent: false
1672
1726
  };
1673
1727
  const toolCall2 = toolCalls[index];
@@ -1675,6 +1729,7 @@ var OpenRouterChatLanguageModel = class {
1675
1729
  throw new Error("Tool call is missing");
1676
1730
  }
1677
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;
1678
1733
  controller.enqueue({
1679
1734
  type: "tool-input-start",
1680
1735
  id: toolCall2.id,
@@ -1703,25 +1758,26 @@ var OpenRouterChatLanguageModel = class {
1703
1758
  if (toolCall == null) {
1704
1759
  throw new Error("Tool call is missing");
1705
1760
  }
1706
- if (((_h = toolCallDelta.function) == null ? void 0 : _h.name) != null) {
1761
+ if (!toolCall.inputStarted) {
1762
+ toolCall.inputStarted = true;
1707
1763
  controller.enqueue({
1708
1764
  type: "tool-input-start",
1709
1765
  id: toolCall.id,
1710
1766
  toolName: toolCall.function.name
1711
1767
  });
1712
1768
  }
1713
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
1714
- 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 : "";
1715
1771
  }
1716
1772
  controller.enqueue({
1717
1773
  type: "tool-input-delta",
1718
1774
  id: toolCall.id,
1719
- delta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
1775
+ delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
1720
1776
  });
1721
- 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)) {
1722
1778
  controller.enqueue({
1723
1779
  type: "tool-call",
1724
- toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
1780
+ toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
1725
1781
  toolName: toolCall.function.name,
1726
1782
  input: toolCall.function.arguments
1727
1783
  });
@@ -1747,10 +1803,16 @@ var OpenRouterChatLanguageModel = class {
1747
1803
  }
1748
1804
  }
1749
1805
  if (textStarted) {
1750
- controller.enqueue({ type: "text-end", id: textId || generateId() });
1806
+ controller.enqueue({
1807
+ type: "text-end",
1808
+ id: textId || generateId()
1809
+ });
1751
1810
  }
1752
1811
  if (reasoningStarted) {
1753
- controller.enqueue({ type: "reasoning-end", id: reasoningId || generateId() });
1812
+ controller.enqueue({
1813
+ type: "reasoning-end",
1814
+ id: reasoningId || generateId()
1815
+ });
1754
1816
  }
1755
1817
  controller.enqueue({
1756
1818
  type: "finish",