@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.
@@ -136,7 +136,7 @@ type OpenRouterChatSettings = {
136
136
  token from being generated.
137
137
  */
138
138
  logitBias?: Record<number, number>;
139
- /**s
139
+ /**
140
140
  Return the log probabilities of the tokens. Including logprobs will increase
141
141
  the response size and can slow down response times. However, it can
142
142
  be useful to better understand how the model is behaving.
@@ -136,7 +136,7 @@ type OpenRouterChatSettings = {
136
136
  token from being generated.
137
137
  */
138
138
  logitBias?: Record<number, number>;
139
- /**s
139
+ /**
140
140
  Return the log probabilities of the tokens. Including logprobs will increase
141
141
  the response size and can slow down response times. However, it can
142
142
  be useful to better understand how the model is behaving.
@@ -52,6 +52,32 @@ __export(index_exports, {
52
52
  });
53
53
  module.exports = __toCommonJS(index_exports);
54
54
 
55
+ // src/schemas/reasoning-details.ts
56
+ var import_v4 = require("zod/v4");
57
+ var ReasoningDetailSummarySchema = import_v4.z.object({
58
+ type: import_v4.z.literal("reasoning.summary" /* Summary */),
59
+ summary: import_v4.z.string()
60
+ });
61
+ var ReasoningDetailEncryptedSchema = import_v4.z.object({
62
+ type: import_v4.z.literal("reasoning.encrypted" /* Encrypted */),
63
+ data: import_v4.z.string()
64
+ });
65
+ var ReasoningDetailTextSchema = import_v4.z.object({
66
+ type: import_v4.z.literal("reasoning.text" /* Text */),
67
+ text: import_v4.z.string().nullish(),
68
+ signature: import_v4.z.string().nullish()
69
+ });
70
+ var ReasoningDetailUnionSchema = import_v4.z.union([
71
+ ReasoningDetailSummarySchema,
72
+ ReasoningDetailEncryptedSchema,
73
+ ReasoningDetailTextSchema
74
+ ]);
75
+ var ReasoningDetailsWithUnknownSchema = import_v4.z.union([
76
+ ReasoningDetailUnionSchema,
77
+ import_v4.z.unknown().transform(() => null)
78
+ ]);
79
+ var ReasoningDetailArraySchema = import_v4.z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
80
+
55
81
  // node_modules/.pnpm/@ai-sdk+provider@2.0.0-beta.1/node_modules/@ai-sdk/provider/dist/index.mjs
56
82
  var marker = "vercel.ai.error";
57
83
  var symbol = Symbol.for(marker);
@@ -874,32 +900,6 @@ function convertUint8ArrayToBase64(array) {
874
900
  return btoa(latin1string);
875
901
  }
876
902
 
877
- // src/schemas/reasoning-details.ts
878
- var import_v4 = require("zod/v4");
879
- var ReasoningDetailSummarySchema = import_v4.z.object({
880
- type: import_v4.z.literal("reasoning.summary" /* Summary */),
881
- summary: import_v4.z.string()
882
- });
883
- var ReasoningDetailEncryptedSchema = import_v4.z.object({
884
- type: import_v4.z.literal("reasoning.encrypted" /* Encrypted */),
885
- data: import_v4.z.string()
886
- });
887
- var ReasoningDetailTextSchema = import_v4.z.object({
888
- type: import_v4.z.literal("reasoning.text" /* Text */),
889
- text: import_v4.z.string().nullish(),
890
- signature: import_v4.z.string().nullish()
891
- });
892
- var ReasoningDetailUnionSchema = import_v4.z.union([
893
- ReasoningDetailSummarySchema,
894
- ReasoningDetailEncryptedSchema,
895
- ReasoningDetailTextSchema
896
- ]);
897
- var ReasoningDetailsWithUnknownSchema = import_v4.z.union([
898
- ReasoningDetailUnionSchema,
899
- import_v4.z.unknown().transform(() => null)
900
- ]);
901
- var ReasoningDetailArraySchema = import_v4.z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
902
-
903
903
  // src/schemas/error-response.ts
904
904
  var import_v42 = require("zod/v4");
905
905
  var OpenRouterErrorResponseSchema = import_v42.z.object({
@@ -932,6 +932,39 @@ function mapOpenRouterFinishReason(finishReason) {
932
932
  }
933
933
  }
934
934
 
935
+ // src/chat/is-url.ts
936
+ function isUrl({
937
+ url,
938
+ protocols
939
+ }) {
940
+ try {
941
+ const urlObj = new URL(url);
942
+ return protocols.has(urlObj.protocol);
943
+ } catch (_) {
944
+ return false;
945
+ }
946
+ }
947
+
948
+ // src/chat/file-url-utils.ts
949
+ function getFileUrl({
950
+ part,
951
+ defaultMediaType
952
+ }) {
953
+ var _a15, _b;
954
+ if (part.data instanceof Uint8Array) {
955
+ const base64 = convertUint8ArrayToBase64(part.data);
956
+ return `data:${(_a15 = part.mediaType) != null ? _a15 : defaultMediaType};base64,${base64}`;
957
+ }
958
+ const stringUrl = part.data.toString();
959
+ if (isUrl({
960
+ url: stringUrl,
961
+ protocols: /* @__PURE__ */ new Set(["http:", "https:"])
962
+ })) {
963
+ return stringUrl;
964
+ }
965
+ return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b = part.mediaType) != null ? _b : defaultMediaType};base64,${stringUrl}`;
966
+ }
967
+
935
968
  // src/chat/convert-to-openrouter-chat-messages.ts
936
969
  function getCacheControl(providerMetadata) {
937
970
  var _a15, _b, _c;
@@ -971,7 +1004,7 @@ function convertToOpenRouterChatMessages(prompt) {
971
1004
  const messageCacheControl = getCacheControl(providerOptions);
972
1005
  const contentParts = content.map(
973
1006
  (part) => {
974
- var _a16, _b2, _c2, _d, _e, _f, _g;
1007
+ var _a16, _b2, _c2, _d, _e, _f;
975
1008
  const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
976
1009
  switch (part.type) {
977
1010
  case "text":
@@ -981,29 +1014,49 @@ function convertToOpenRouterChatMessages(prompt) {
981
1014
  // For text parts, only use part-specific cache control
982
1015
  cache_control: cacheControl
983
1016
  };
984
- case "file":
1017
+ case "file": {
985
1018
  if ((_b2 = part.mediaType) == null ? void 0 : _b2.startsWith("image/")) {
1019
+ const url = getFileUrl({
1020
+ part,
1021
+ defaultMediaType: "image/jpeg"
1022
+ });
986
1023
  return {
987
1024
  type: "image_url",
988
1025
  image_url: {
989
- url: part.data instanceof URL ? part.data.toString() : `data:${(_c2 = part.mediaType) != null ? _c2 : "image/jpeg"};base64,${convertUint8ArrayToBase64(
990
- part.data instanceof Uint8Array ? part.data : new Uint8Array()
991
- )}`
1026
+ url
992
1027
  },
993
1028
  // For image parts, use part-specific or message-level cache control
994
1029
  cache_control: cacheControl
995
1030
  };
996
1031
  }
1032
+ const fileName = String(
1033
+ (_f = (_e = (_d = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d.filename) != null ? _e : part.filename) != null ? _f : ""
1034
+ );
1035
+ const fileData = getFileUrl({
1036
+ part,
1037
+ defaultMediaType: "application/pdf"
1038
+ });
1039
+ if (isUrl({
1040
+ url: fileData,
1041
+ protocols: /* @__PURE__ */ new Set(["http:", "https:"])
1042
+ })) {
1043
+ return {
1044
+ type: "file",
1045
+ file: {
1046
+ filename: fileName,
1047
+ file_data: fileData
1048
+ }
1049
+ };
1050
+ }
997
1051
  return {
998
1052
  type: "file",
999
1053
  file: {
1000
- filename: String(
1001
- (_g = (_f = (_e = (_d = part.providerOptions) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.filename) != null ? _f : part.filename) != null ? _g : ""
1002
- ),
1003
- file_data: part.data instanceof Uint8Array ? `data:${part.mediaType};base64,${convertUint8ArrayToBase64(part.data)}` : `data:${part.mediaType};base64,${part.data}`
1054
+ filename: fileName,
1055
+ file_data: fileData
1004
1056
  },
1005
1057
  cache_control: cacheControl
1006
1058
  };
1059
+ }
1007
1060
  default: {
1008
1061
  return {
1009
1062
  type: "text",
@@ -1469,11 +1522,14 @@ var OpenRouterChatLanguageModel = class {
1469
1522
  const openrouterUsage = {};
1470
1523
  let textStarted = false;
1471
1524
  let reasoningStarted = false;
1525
+ let textId;
1526
+ let reasoningId;
1527
+ let openrouterResponseId;
1472
1528
  return {
1473
1529
  stream: response.pipeThrough(
1474
1530
  new TransformStream({
1475
1531
  transform(chunk, controller) {
1476
- var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1532
+ var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
1477
1533
  if (!chunk.success) {
1478
1534
  finishReason = "error";
1479
1535
  controller.enqueue({ type: "error", error: chunk.error });
@@ -1486,6 +1542,7 @@ var OpenRouterChatLanguageModel = class {
1486
1542
  return;
1487
1543
  }
1488
1544
  if (value.id) {
1545
+ openrouterResponseId = value.id;
1489
1546
  controller.enqueue({
1490
1547
  type: "response-metadata",
1491
1548
  id: value.id
@@ -1530,30 +1587,32 @@ var OpenRouterChatLanguageModel = class {
1530
1587
  const delta = choice.delta;
1531
1588
  if (delta.content != null) {
1532
1589
  if (!textStarted) {
1590
+ textId = openrouterResponseId || generateId();
1533
1591
  controller.enqueue({
1534
1592
  type: "text-start",
1535
- id: generateId()
1593
+ id: textId
1536
1594
  });
1537
1595
  textStarted = true;
1538
1596
  }
1539
1597
  controller.enqueue({
1540
1598
  type: "text-delta",
1541
1599
  delta: delta.content,
1542
- id: generateId()
1600
+ id: textId || generateId()
1543
1601
  });
1544
1602
  }
1545
1603
  const emitReasoningChunk = (chunkText) => {
1546
1604
  if (!reasoningStarted) {
1605
+ reasoningId = openrouterResponseId || generateId();
1547
1606
  controller.enqueue({
1548
1607
  type: "reasoning-start",
1549
- id: generateId()
1608
+ id: reasoningId
1550
1609
  });
1551
1610
  reasoningStarted = true;
1552
1611
  }
1553
1612
  controller.enqueue({
1554
1613
  type: "reasoning-delta",
1555
1614
  delta: chunkText,
1556
- id: generateId()
1615
+ id: reasoningId || generateId()
1557
1616
  });
1558
1617
  };
1559
1618
  if (delta.reasoning != null) {
@@ -1616,6 +1675,7 @@ var OpenRouterChatLanguageModel = class {
1616
1675
  name: toolCallDelta.function.name,
1617
1676
  arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
1618
1677
  },
1678
+ inputStarted: false,
1619
1679
  sent: false
1620
1680
  };
1621
1681
  const toolCall2 = toolCalls[index];
@@ -1623,6 +1683,7 @@ var OpenRouterChatLanguageModel = class {
1623
1683
  throw new Error("Tool call is missing");
1624
1684
  }
1625
1685
  if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
1686
+ toolCall2.inputStarted = true;
1626
1687
  controller.enqueue({
1627
1688
  type: "tool-input-start",
1628
1689
  id: toolCall2.id,
@@ -1651,25 +1712,26 @@ var OpenRouterChatLanguageModel = class {
1651
1712
  if (toolCall == null) {
1652
1713
  throw new Error("Tool call is missing");
1653
1714
  }
1654
- if (((_h = toolCallDelta.function) == null ? void 0 : _h.name) != null) {
1715
+ if (!toolCall.inputStarted) {
1716
+ toolCall.inputStarted = true;
1655
1717
  controller.enqueue({
1656
1718
  type: "tool-input-start",
1657
1719
  id: toolCall.id,
1658
1720
  toolName: toolCall.function.name
1659
1721
  });
1660
1722
  }
1661
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
1662
- toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : "";
1723
+ if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
1724
+ toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
1663
1725
  }
1664
1726
  controller.enqueue({
1665
1727
  type: "tool-input-delta",
1666
1728
  id: toolCall.id,
1667
- delta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
1729
+ delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
1668
1730
  });
1669
- if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1731
+ if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1670
1732
  controller.enqueue({
1671
1733
  type: "tool-call",
1672
- toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
1734
+ toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
1673
1735
  toolName: toolCall.function.name,
1674
1736
  input: toolCall.function.arguments
1675
1737
  });
@@ -1695,10 +1757,16 @@ var OpenRouterChatLanguageModel = class {
1695
1757
  }
1696
1758
  }
1697
1759
  if (textStarted) {
1698
- controller.enqueue({ type: "text-end", id: generateId() });
1760
+ controller.enqueue({
1761
+ type: "text-end",
1762
+ id: textId || generateId()
1763
+ });
1699
1764
  }
1700
1765
  if (reasoningStarted) {
1701
- controller.enqueue({ type: "reasoning-end", id: generateId() });
1766
+ controller.enqueue({
1767
+ type: "reasoning-end",
1768
+ id: reasoningId || generateId()
1769
+ });
1702
1770
  }
1703
1771
  controller.enqueue({
1704
1772
  type: "finish",