@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.
@@ -18,6 +18,32 @@ var __spreadValues = (a, b) => {
18
18
  };
19
19
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
20
 
21
+ // src/schemas/reasoning-details.ts
22
+ import { z } from "zod/v4";
23
+ var ReasoningDetailSummarySchema = z.object({
24
+ type: z.literal("reasoning.summary" /* Summary */),
25
+ summary: z.string()
26
+ });
27
+ var ReasoningDetailEncryptedSchema = z.object({
28
+ type: z.literal("reasoning.encrypted" /* Encrypted */),
29
+ data: z.string()
30
+ });
31
+ var ReasoningDetailTextSchema = z.object({
32
+ type: z.literal("reasoning.text" /* Text */),
33
+ text: z.string().nullish(),
34
+ signature: z.string().nullish()
35
+ });
36
+ var ReasoningDetailUnionSchema = z.union([
37
+ ReasoningDetailSummarySchema,
38
+ ReasoningDetailEncryptedSchema,
39
+ ReasoningDetailTextSchema
40
+ ]);
41
+ var ReasoningDetailsWithUnknownSchema = z.union([
42
+ ReasoningDetailUnionSchema,
43
+ z.unknown().transform(() => null)
44
+ ]);
45
+ var ReasoningDetailArraySchema = z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
46
+
21
47
  // node_modules/.pnpm/@ai-sdk+provider@2.0.0-beta.1/node_modules/@ai-sdk/provider/dist/index.mjs
22
48
  var marker = "vercel.ai.error";
23
49
  var symbol = Symbol.for(marker);
@@ -840,32 +866,6 @@ function convertUint8ArrayToBase64(array) {
840
866
  return btoa(latin1string);
841
867
  }
842
868
 
843
- // src/schemas/reasoning-details.ts
844
- import { z } from "zod/v4";
845
- var ReasoningDetailSummarySchema = z.object({
846
- type: z.literal("reasoning.summary" /* Summary */),
847
- summary: z.string()
848
- });
849
- var ReasoningDetailEncryptedSchema = z.object({
850
- type: z.literal("reasoning.encrypted" /* Encrypted */),
851
- data: z.string()
852
- });
853
- var ReasoningDetailTextSchema = z.object({
854
- type: z.literal("reasoning.text" /* Text */),
855
- text: z.string().nullish(),
856
- signature: z.string().nullish()
857
- });
858
- var ReasoningDetailUnionSchema = z.union([
859
- ReasoningDetailSummarySchema,
860
- ReasoningDetailEncryptedSchema,
861
- ReasoningDetailTextSchema
862
- ]);
863
- var ReasoningDetailsWithUnknownSchema = z.union([
864
- ReasoningDetailUnionSchema,
865
- z.unknown().transform(() => null)
866
- ]);
867
- var ReasoningDetailArraySchema = z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
868
-
869
869
  // src/schemas/error-response.ts
870
870
  import { z as z2 } from "zod/v4";
871
871
  var OpenRouterErrorResponseSchema = z2.object({
@@ -898,6 +898,39 @@ function mapOpenRouterFinishReason(finishReason) {
898
898
  }
899
899
  }
900
900
 
901
+ // src/chat/is-url.ts
902
+ function isUrl({
903
+ url,
904
+ protocols
905
+ }) {
906
+ try {
907
+ const urlObj = new URL(url);
908
+ return protocols.has(urlObj.protocol);
909
+ } catch (_) {
910
+ return false;
911
+ }
912
+ }
913
+
914
+ // src/chat/file-url-utils.ts
915
+ function getFileUrl({
916
+ part,
917
+ defaultMediaType
918
+ }) {
919
+ var _a15, _b;
920
+ if (part.data instanceof Uint8Array) {
921
+ const base64 = convertUint8ArrayToBase64(part.data);
922
+ return `data:${(_a15 = part.mediaType) != null ? _a15 : defaultMediaType};base64,${base64}`;
923
+ }
924
+ const stringUrl = part.data.toString();
925
+ if (isUrl({
926
+ url: stringUrl,
927
+ protocols: /* @__PURE__ */ new Set(["http:", "https:"])
928
+ })) {
929
+ return stringUrl;
930
+ }
931
+ return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b = part.mediaType) != null ? _b : defaultMediaType};base64,${stringUrl}`;
932
+ }
933
+
901
934
  // src/chat/convert-to-openrouter-chat-messages.ts
902
935
  function getCacheControl(providerMetadata) {
903
936
  var _a15, _b, _c;
@@ -937,7 +970,7 @@ function convertToOpenRouterChatMessages(prompt) {
937
970
  const messageCacheControl = getCacheControl(providerOptions);
938
971
  const contentParts = content.map(
939
972
  (part) => {
940
- var _a16, _b2, _c2, _d, _e, _f, _g;
973
+ var _a16, _b2, _c2, _d, _e, _f;
941
974
  const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
942
975
  switch (part.type) {
943
976
  case "text":
@@ -947,29 +980,49 @@ function convertToOpenRouterChatMessages(prompt) {
947
980
  // For text parts, only use part-specific cache control
948
981
  cache_control: cacheControl
949
982
  };
950
- case "file":
983
+ case "file": {
951
984
  if ((_b2 = part.mediaType) == null ? void 0 : _b2.startsWith("image/")) {
985
+ const url = getFileUrl({
986
+ part,
987
+ defaultMediaType: "image/jpeg"
988
+ });
952
989
  return {
953
990
  type: "image_url",
954
991
  image_url: {
955
- url: part.data instanceof URL ? part.data.toString() : `data:${(_c2 = part.mediaType) != null ? _c2 : "image/jpeg"};base64,${convertUint8ArrayToBase64(
956
- part.data instanceof Uint8Array ? part.data : new Uint8Array()
957
- )}`
992
+ url
958
993
  },
959
994
  // For image parts, use part-specific or message-level cache control
960
995
  cache_control: cacheControl
961
996
  };
962
997
  }
998
+ const fileName = String(
999
+ (_f = (_e = (_d = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d.filename) != null ? _e : part.filename) != null ? _f : ""
1000
+ );
1001
+ const fileData = getFileUrl({
1002
+ part,
1003
+ defaultMediaType: "application/pdf"
1004
+ });
1005
+ if (isUrl({
1006
+ url: fileData,
1007
+ protocols: /* @__PURE__ */ new Set(["http:", "https:"])
1008
+ })) {
1009
+ return {
1010
+ type: "file",
1011
+ file: {
1012
+ filename: fileName,
1013
+ file_data: fileData
1014
+ }
1015
+ };
1016
+ }
963
1017
  return {
964
1018
  type: "file",
965
1019
  file: {
966
- filename: String(
967
- (_g = (_f = (_e = (_d = part.providerOptions) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.filename) != null ? _f : part.filename) != null ? _g : ""
968
- ),
969
- file_data: part.data instanceof Uint8Array ? `data:${part.mediaType};base64,${convertUint8ArrayToBase64(part.data)}` : `data:${part.mediaType};base64,${part.data}`
1020
+ filename: fileName,
1021
+ file_data: fileData
970
1022
  },
971
1023
  cache_control: cacheControl
972
1024
  };
1025
+ }
973
1026
  default: {
974
1027
  return {
975
1028
  type: "text",
@@ -1442,7 +1495,7 @@ var OpenRouterChatLanguageModel = class {
1442
1495
  stream: response.pipeThrough(
1443
1496
  new TransformStream({
1444
1497
  transform(chunk, controller) {
1445
- var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1498
+ var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
1446
1499
  if (!chunk.success) {
1447
1500
  finishReason = "error";
1448
1501
  controller.enqueue({ type: "error", error: chunk.error });
@@ -1588,6 +1641,7 @@ var OpenRouterChatLanguageModel = class {
1588
1641
  name: toolCallDelta.function.name,
1589
1642
  arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
1590
1643
  },
1644
+ inputStarted: false,
1591
1645
  sent: false
1592
1646
  };
1593
1647
  const toolCall2 = toolCalls[index];
@@ -1595,6 +1649,7 @@ var OpenRouterChatLanguageModel = class {
1595
1649
  throw new Error("Tool call is missing");
1596
1650
  }
1597
1651
  if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
1652
+ toolCall2.inputStarted = true;
1598
1653
  controller.enqueue({
1599
1654
  type: "tool-input-start",
1600
1655
  id: toolCall2.id,
@@ -1623,25 +1678,26 @@ var OpenRouterChatLanguageModel = class {
1623
1678
  if (toolCall == null) {
1624
1679
  throw new Error("Tool call is missing");
1625
1680
  }
1626
- if (((_h = toolCallDelta.function) == null ? void 0 : _h.name) != null) {
1681
+ if (!toolCall.inputStarted) {
1682
+ toolCall.inputStarted = true;
1627
1683
  controller.enqueue({
1628
1684
  type: "tool-input-start",
1629
1685
  id: toolCall.id,
1630
1686
  toolName: toolCall.function.name
1631
1687
  });
1632
1688
  }
1633
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
1634
- toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : "";
1689
+ if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
1690
+ toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
1635
1691
  }
1636
1692
  controller.enqueue({
1637
1693
  type: "tool-input-delta",
1638
1694
  id: toolCall.id,
1639
- delta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
1695
+ delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
1640
1696
  });
1641
- if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1697
+ if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1642
1698
  controller.enqueue({
1643
1699
  type: "tool-call",
1644
- toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
1700
+ toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
1645
1701
  toolName: toolCall.function.name,
1646
1702
  input: toolCall.function.arguments
1647
1703
  });
@@ -1667,10 +1723,16 @@ var OpenRouterChatLanguageModel = class {
1667
1723
  }
1668
1724
  }
1669
1725
  if (textStarted) {
1670
- controller.enqueue({ type: "text-end", id: textId || generateId() });
1726
+ controller.enqueue({
1727
+ type: "text-end",
1728
+ id: textId || generateId()
1729
+ });
1671
1730
  }
1672
1731
  if (reasoningStarted) {
1673
- controller.enqueue({ type: "reasoning-end", id: reasoningId || generateId() });
1732
+ controller.enqueue({
1733
+ type: "reasoning-end",
1734
+ id: reasoningId || generateId()
1735
+ });
1674
1736
  }
1675
1737
  controller.enqueue({
1676
1738
  type: "finish",