@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.
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +90 -22
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +90 -22
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +1 -1
- package/dist/internal/index.d.ts +1 -1
- package/dist/internal/index.js +116 -48
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +116 -48
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +1 -1
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
|
|
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
|
|
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:
|
|
1012
|
-
|
|
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",
|
|
@@ -1480,11 +1533,14 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1480
1533
|
const openrouterUsage = {};
|
|
1481
1534
|
let textStarted = false;
|
|
1482
1535
|
let reasoningStarted = false;
|
|
1536
|
+
let textId;
|
|
1537
|
+
let reasoningId;
|
|
1538
|
+
let openrouterResponseId;
|
|
1483
1539
|
return {
|
|
1484
1540
|
stream: response.pipeThrough(
|
|
1485
1541
|
new TransformStream({
|
|
1486
1542
|
transform(chunk, controller) {
|
|
1487
|
-
var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n
|
|
1543
|
+
var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
1488
1544
|
if (!chunk.success) {
|
|
1489
1545
|
finishReason = "error";
|
|
1490
1546
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -1497,6 +1553,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1497
1553
|
return;
|
|
1498
1554
|
}
|
|
1499
1555
|
if (value.id) {
|
|
1556
|
+
openrouterResponseId = value.id;
|
|
1500
1557
|
controller.enqueue({
|
|
1501
1558
|
type: "response-metadata",
|
|
1502
1559
|
id: value.id
|
|
@@ -1541,30 +1598,32 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1541
1598
|
const delta = choice.delta;
|
|
1542
1599
|
if (delta.content != null) {
|
|
1543
1600
|
if (!textStarted) {
|
|
1601
|
+
textId = openrouterResponseId || generateId();
|
|
1544
1602
|
controller.enqueue({
|
|
1545
1603
|
type: "text-start",
|
|
1546
|
-
id:
|
|
1604
|
+
id: textId
|
|
1547
1605
|
});
|
|
1548
1606
|
textStarted = true;
|
|
1549
1607
|
}
|
|
1550
1608
|
controller.enqueue({
|
|
1551
1609
|
type: "text-delta",
|
|
1552
1610
|
delta: delta.content,
|
|
1553
|
-
id: generateId()
|
|
1611
|
+
id: textId || generateId()
|
|
1554
1612
|
});
|
|
1555
1613
|
}
|
|
1556
1614
|
const emitReasoningChunk = (chunkText) => {
|
|
1557
1615
|
if (!reasoningStarted) {
|
|
1616
|
+
reasoningId = openrouterResponseId || generateId();
|
|
1558
1617
|
controller.enqueue({
|
|
1559
1618
|
type: "reasoning-start",
|
|
1560
|
-
id:
|
|
1619
|
+
id: reasoningId
|
|
1561
1620
|
});
|
|
1562
1621
|
reasoningStarted = true;
|
|
1563
1622
|
}
|
|
1564
1623
|
controller.enqueue({
|
|
1565
1624
|
type: "reasoning-delta",
|
|
1566
1625
|
delta: chunkText,
|
|
1567
|
-
id: generateId()
|
|
1626
|
+
id: reasoningId || generateId()
|
|
1568
1627
|
});
|
|
1569
1628
|
};
|
|
1570
1629
|
if (delta.reasoning != null) {
|
|
@@ -1627,6 +1686,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1627
1686
|
name: toolCallDelta.function.name,
|
|
1628
1687
|
arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
|
|
1629
1688
|
},
|
|
1689
|
+
inputStarted: false,
|
|
1630
1690
|
sent: false
|
|
1631
1691
|
};
|
|
1632
1692
|
const toolCall2 = toolCalls[index];
|
|
@@ -1634,6 +1694,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1634
1694
|
throw new Error("Tool call is missing");
|
|
1635
1695
|
}
|
|
1636
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;
|
|
1637
1698
|
controller.enqueue({
|
|
1638
1699
|
type: "tool-input-start",
|
|
1639
1700
|
id: toolCall2.id,
|
|
@@ -1662,25 +1723,26 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1662
1723
|
if (toolCall == null) {
|
|
1663
1724
|
throw new Error("Tool call is missing");
|
|
1664
1725
|
}
|
|
1665
|
-
if (
|
|
1726
|
+
if (!toolCall.inputStarted) {
|
|
1727
|
+
toolCall.inputStarted = true;
|
|
1666
1728
|
controller.enqueue({
|
|
1667
1729
|
type: "tool-input-start",
|
|
1668
1730
|
id: toolCall.id,
|
|
1669
1731
|
toolName: toolCall.function.name
|
|
1670
1732
|
});
|
|
1671
1733
|
}
|
|
1672
|
-
if (((
|
|
1673
|
-
toolCall.function.arguments += (
|
|
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 : "";
|
|
1674
1736
|
}
|
|
1675
1737
|
controller.enqueue({
|
|
1676
1738
|
type: "tool-input-delta",
|
|
1677
1739
|
id: toolCall.id,
|
|
1678
|
-
delta: (
|
|
1740
|
+
delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
|
|
1679
1741
|
});
|
|
1680
|
-
if (((
|
|
1742
|
+
if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
|
|
1681
1743
|
controller.enqueue({
|
|
1682
1744
|
type: "tool-call",
|
|
1683
|
-
toolCallId: (
|
|
1745
|
+
toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
|
|
1684
1746
|
toolName: toolCall.function.name,
|
|
1685
1747
|
input: toolCall.function.arguments
|
|
1686
1748
|
});
|
|
@@ -1706,10 +1768,16 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1706
1768
|
}
|
|
1707
1769
|
}
|
|
1708
1770
|
if (textStarted) {
|
|
1709
|
-
controller.enqueue({
|
|
1771
|
+
controller.enqueue({
|
|
1772
|
+
type: "text-end",
|
|
1773
|
+
id: textId || generateId()
|
|
1774
|
+
});
|
|
1710
1775
|
}
|
|
1711
1776
|
if (reasoningStarted) {
|
|
1712
|
-
controller.enqueue({
|
|
1777
|
+
controller.enqueue({
|
|
1778
|
+
type: "reasoning-end",
|
|
1779
|
+
id: reasoningId || generateId()
|
|
1780
|
+
});
|
|
1713
1781
|
}
|
|
1714
1782
|
controller.enqueue({
|
|
1715
1783
|
type: "finish",
|