@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.
@@ -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",
@@ -1435,11 +1488,14 @@ var OpenRouterChatLanguageModel = class {
1435
1488
  const openrouterUsage = {};
1436
1489
  let textStarted = false;
1437
1490
  let reasoningStarted = false;
1491
+ let textId;
1492
+ let reasoningId;
1493
+ let openrouterResponseId;
1438
1494
  return {
1439
1495
  stream: response.pipeThrough(
1440
1496
  new TransformStream({
1441
1497
  transform(chunk, controller) {
1442
- 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;
1443
1499
  if (!chunk.success) {
1444
1500
  finishReason = "error";
1445
1501
  controller.enqueue({ type: "error", error: chunk.error });
@@ -1452,6 +1508,7 @@ var OpenRouterChatLanguageModel = class {
1452
1508
  return;
1453
1509
  }
1454
1510
  if (value.id) {
1511
+ openrouterResponseId = value.id;
1455
1512
  controller.enqueue({
1456
1513
  type: "response-metadata",
1457
1514
  id: value.id
@@ -1496,30 +1553,32 @@ var OpenRouterChatLanguageModel = class {
1496
1553
  const delta = choice.delta;
1497
1554
  if (delta.content != null) {
1498
1555
  if (!textStarted) {
1556
+ textId = openrouterResponseId || generateId();
1499
1557
  controller.enqueue({
1500
1558
  type: "text-start",
1501
- id: generateId()
1559
+ id: textId
1502
1560
  });
1503
1561
  textStarted = true;
1504
1562
  }
1505
1563
  controller.enqueue({
1506
1564
  type: "text-delta",
1507
1565
  delta: delta.content,
1508
- id: generateId()
1566
+ id: textId || generateId()
1509
1567
  });
1510
1568
  }
1511
1569
  const emitReasoningChunk = (chunkText) => {
1512
1570
  if (!reasoningStarted) {
1571
+ reasoningId = openrouterResponseId || generateId();
1513
1572
  controller.enqueue({
1514
1573
  type: "reasoning-start",
1515
- id: generateId()
1574
+ id: reasoningId
1516
1575
  });
1517
1576
  reasoningStarted = true;
1518
1577
  }
1519
1578
  controller.enqueue({
1520
1579
  type: "reasoning-delta",
1521
1580
  delta: chunkText,
1522
- id: generateId()
1581
+ id: reasoningId || generateId()
1523
1582
  });
1524
1583
  };
1525
1584
  if (delta.reasoning != null) {
@@ -1582,6 +1641,7 @@ var OpenRouterChatLanguageModel = class {
1582
1641
  name: toolCallDelta.function.name,
1583
1642
  arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
1584
1643
  },
1644
+ inputStarted: false,
1585
1645
  sent: false
1586
1646
  };
1587
1647
  const toolCall2 = toolCalls[index];
@@ -1589,6 +1649,7 @@ var OpenRouterChatLanguageModel = class {
1589
1649
  throw new Error("Tool call is missing");
1590
1650
  }
1591
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;
1592
1653
  controller.enqueue({
1593
1654
  type: "tool-input-start",
1594
1655
  id: toolCall2.id,
@@ -1617,25 +1678,26 @@ var OpenRouterChatLanguageModel = class {
1617
1678
  if (toolCall == null) {
1618
1679
  throw new Error("Tool call is missing");
1619
1680
  }
1620
- if (((_h = toolCallDelta.function) == null ? void 0 : _h.name) != null) {
1681
+ if (!toolCall.inputStarted) {
1682
+ toolCall.inputStarted = true;
1621
1683
  controller.enqueue({
1622
1684
  type: "tool-input-start",
1623
1685
  id: toolCall.id,
1624
1686
  toolName: toolCall.function.name
1625
1687
  });
1626
1688
  }
1627
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
1628
- 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 : "";
1629
1691
  }
1630
1692
  controller.enqueue({
1631
1693
  type: "tool-input-delta",
1632
1694
  id: toolCall.id,
1633
- delta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
1695
+ delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
1634
1696
  });
1635
- 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)) {
1636
1698
  controller.enqueue({
1637
1699
  type: "tool-call",
1638
- toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
1700
+ toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
1639
1701
  toolName: toolCall.function.name,
1640
1702
  input: toolCall.function.arguments
1641
1703
  });
@@ -1661,10 +1723,16 @@ var OpenRouterChatLanguageModel = class {
1661
1723
  }
1662
1724
  }
1663
1725
  if (textStarted) {
1664
- controller.enqueue({ type: "text-end", id: generateId() });
1726
+ controller.enqueue({
1727
+ type: "text-end",
1728
+ id: textId || generateId()
1729
+ });
1665
1730
  }
1666
1731
  if (reasoningStarted) {
1667
- controller.enqueue({ type: "reasoning-end", id: generateId() });
1732
+ controller.enqueue({
1733
+ type: "reasoning-end",
1734
+ id: reasoningId || generateId()
1735
+ });
1668
1736
  }
1669
1737
  controller.enqueue({
1670
1738
  type: "finish",