@openrouter/ai-sdk-provider 1.2.8 → 1.3.0
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/README.md +157 -0
- package/dist/index.d.mts +15 -2
- package/dist/index.d.ts +15 -2
- package/dist/index.js +67 -34
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +67 -34
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +15 -2
- package/dist/internal/index.d.ts +15 -2
- package/dist/internal/index.js +66 -33
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +66 -33
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -995,7 +995,7 @@ var OpenRouterProviderMetadataSchema = z3.object({
|
|
|
995
995
|
cost: z3.number().optional(),
|
|
996
996
|
costDetails: z3.object({
|
|
997
997
|
upstreamInferenceCost: z3.number()
|
|
998
|
-
}).passthrough()
|
|
998
|
+
}).passthrough().optional()
|
|
999
999
|
}).passthrough()
|
|
1000
1000
|
}).passthrough();
|
|
1001
1001
|
var OpenRouterProviderOptionsSchema = z3.object({
|
|
@@ -1114,9 +1114,8 @@ function getCacheControl(providerMetadata) {
|
|
|
1114
1114
|
return (_c = (_b = (_a15 = openrouter2 == null ? void 0 : openrouter2.cacheControl) != null ? _a15 : openrouter2 == null ? void 0 : openrouter2.cache_control) != null ? _b : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
|
|
1115
1115
|
}
|
|
1116
1116
|
function convertToOpenRouterChatMessages(prompt) {
|
|
1117
|
-
var _a15, _b, _c, _d, _e, _f;
|
|
1117
|
+
var _a15, _b, _c, _d, _e, _f, _g, _h;
|
|
1118
1118
|
const messages = [];
|
|
1119
|
-
const accumulatedReasoningDetails = [];
|
|
1120
1119
|
for (const { role, content, providerOptions } of prompt) {
|
|
1121
1120
|
switch (role) {
|
|
1122
1121
|
case "system": {
|
|
@@ -1146,7 +1145,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1146
1145
|
const messageCacheControl = getCacheControl(providerOptions);
|
|
1147
1146
|
const contentParts = content.map(
|
|
1148
1147
|
(part) => {
|
|
1149
|
-
var _a16, _b2, _c2, _d2, _e2, _f2,
|
|
1148
|
+
var _a16, _b2, _c2, _d2, _e2, _f2, _g2;
|
|
1150
1149
|
const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
|
|
1151
1150
|
switch (part.type) {
|
|
1152
1151
|
case "text":
|
|
@@ -1179,7 +1178,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1179
1178
|
};
|
|
1180
1179
|
}
|
|
1181
1180
|
const fileName = String(
|
|
1182
|
-
(
|
|
1181
|
+
(_g2 = (_f2 = (_e2 = (_d2 = part.providerOptions) == null ? void 0 : _d2.openrouter) == null ? void 0 : _e2.filename) != null ? _f2 : part.filename) != null ? _g2 : ""
|
|
1183
1182
|
);
|
|
1184
1183
|
const fileData = getFileUrl({
|
|
1185
1184
|
part,
|
|
@@ -1226,6 +1225,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1226
1225
|
let text = "";
|
|
1227
1226
|
let reasoning = "";
|
|
1228
1227
|
const toolCalls = [];
|
|
1228
|
+
const accumulatedReasoningDetails = [];
|
|
1229
1229
|
for (const part of content) {
|
|
1230
1230
|
switch (part.type) {
|
|
1231
1231
|
case "text": {
|
|
@@ -1251,6 +1251,12 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1251
1251
|
}
|
|
1252
1252
|
case "reasoning": {
|
|
1253
1253
|
reasoning += part.text;
|
|
1254
|
+
const parsedPartProviderOptions = OpenRouterProviderOptionsSchema.safeParse(part.providerOptions);
|
|
1255
|
+
if (parsedPartProviderOptions.success && ((_e = (_d = parsedPartProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details)) {
|
|
1256
|
+
accumulatedReasoningDetails.push(
|
|
1257
|
+
...parsedPartProviderOptions.data.openrouter.reasoning_details
|
|
1258
|
+
);
|
|
1259
|
+
}
|
|
1254
1260
|
break;
|
|
1255
1261
|
}
|
|
1256
1262
|
case "file":
|
|
@@ -1261,7 +1267,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1261
1267
|
}
|
|
1262
1268
|
}
|
|
1263
1269
|
const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
|
|
1264
|
-
const messageReasoningDetails = parsedProviderOptions.success ? (
|
|
1270
|
+
const messageReasoningDetails = parsedProviderOptions.success ? (_g = (_f = parsedProviderOptions.data) == null ? void 0 : _f.openrouter) == null ? void 0 : _g.reasoning_details : void 0;
|
|
1265
1271
|
const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : accumulatedReasoningDetails.length > 0 ? accumulatedReasoningDetails : void 0;
|
|
1266
1272
|
messages.push({
|
|
1267
1273
|
role: "assistant",
|
|
@@ -1280,7 +1286,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1280
1286
|
role: "tool",
|
|
1281
1287
|
tool_call_id: toolResponse.toolCallId,
|
|
1282
1288
|
content: content2,
|
|
1283
|
-
cache_control: (
|
|
1289
|
+
cache_control: (_h = getCacheControl(providerOptions)) != null ? _h : getCacheControl(toolResponse.providerOptions)
|
|
1284
1290
|
});
|
|
1285
1291
|
}
|
|
1286
1292
|
break;
|
|
@@ -1596,7 +1602,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1596
1602
|
plugins: this.settings.plugins,
|
|
1597
1603
|
web_search_options: this.settings.web_search_options,
|
|
1598
1604
|
// Provider routing settings:
|
|
1599
|
-
provider: this.settings.provider
|
|
1605
|
+
provider: this.settings.provider,
|
|
1606
|
+
// Debug settings:
|
|
1607
|
+
debug: this.settings.debug
|
|
1600
1608
|
}, this.config.extraBody), this.settings.extraBody);
|
|
1601
1609
|
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null) {
|
|
1602
1610
|
return __spreadProps(__spreadValues({}, baseArgs), {
|
|
@@ -1631,7 +1639,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1631
1639
|
return baseArgs;
|
|
1632
1640
|
}
|
|
1633
1641
|
async doGenerate(options) {
|
|
1634
|
-
var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v
|
|
1642
|
+
var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
1635
1643
|
const providerOptions = options.providerOptions || {};
|
|
1636
1644
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1637
1645
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -1798,21 +1806,24 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1798
1806
|
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
1799
1807
|
provider: (_k = response.provider) != null ? _k : "",
|
|
1800
1808
|
reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
|
|
1801
|
-
usage: {
|
|
1809
|
+
usage: __spreadValues(__spreadValues(__spreadValues({
|
|
1802
1810
|
promptTokens: (_m = usageInfo.inputTokens) != null ? _m : 0,
|
|
1803
1811
|
completionTokens: (_n = usageInfo.outputTokens) != null ? _n : 0,
|
|
1804
1812
|
totalTokens: (_o = usageInfo.totalTokens) != null ? _o : 0,
|
|
1805
|
-
cost: (_p = response.usage) == null ? void 0 : _p.cost
|
|
1813
|
+
cost: (_p = response.usage) == null ? void 0 : _p.cost
|
|
1814
|
+
}, ((_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? {
|
|
1806
1815
|
promptTokensDetails: {
|
|
1807
|
-
cachedTokens:
|
|
1808
|
-
}
|
|
1816
|
+
cachedTokens: response.usage.prompt_tokens_details.cached_tokens
|
|
1817
|
+
}
|
|
1818
|
+
} : {}), ((_t = (_s = response.usage) == null ? void 0 : _s.completion_tokens_details) == null ? void 0 : _t.reasoning_tokens) != null ? {
|
|
1809
1819
|
completionTokensDetails: {
|
|
1810
|
-
reasoningTokens:
|
|
1811
|
-
}
|
|
1820
|
+
reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
|
|
1821
|
+
}
|
|
1822
|
+
} : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.cost_details) == null ? void 0 : _v.upstream_inference_cost) != null ? {
|
|
1812
1823
|
costDetails: {
|
|
1813
|
-
upstreamInferenceCost:
|
|
1824
|
+
upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
|
|
1814
1825
|
}
|
|
1815
|
-
}
|
|
1826
|
+
} : {})
|
|
1816
1827
|
})
|
|
1817
1828
|
},
|
|
1818
1829
|
request: { body: args },
|
|
@@ -1869,7 +1880,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1869
1880
|
stream: response.pipeThrough(
|
|
1870
1881
|
new TransformStream({
|
|
1871
1882
|
transform(chunk, controller) {
|
|
1872
|
-
var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
1883
|
+
var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
1873
1884
|
if (!chunk.success) {
|
|
1874
1885
|
finishReason = "error";
|
|
1875
1886
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -1919,6 +1930,12 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1919
1930
|
}
|
|
1920
1931
|
openrouterUsage.cost = value.usage.cost;
|
|
1921
1932
|
openrouterUsage.totalTokens = value.usage.total_tokens;
|
|
1933
|
+
const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
|
|
1934
|
+
if (upstreamInferenceCost != null) {
|
|
1935
|
+
openrouterUsage.costDetails = {
|
|
1936
|
+
upstreamInferenceCost
|
|
1937
|
+
};
|
|
1938
|
+
}
|
|
1922
1939
|
}
|
|
1923
1940
|
const choice = value.choices[0];
|
|
1924
1941
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
@@ -1928,16 +1945,18 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1928
1945
|
return;
|
|
1929
1946
|
}
|
|
1930
1947
|
const delta = choice.delta;
|
|
1931
|
-
const emitReasoningChunk = (chunkText) => {
|
|
1948
|
+
const emitReasoningChunk = (chunkText, providerMetadata) => {
|
|
1932
1949
|
if (!reasoningStarted) {
|
|
1933
1950
|
reasoningId = openrouterResponseId || generateId();
|
|
1934
1951
|
controller.enqueue({
|
|
1952
|
+
providerMetadata,
|
|
1935
1953
|
type: "reasoning-start",
|
|
1936
1954
|
id: reasoningId
|
|
1937
1955
|
});
|
|
1938
1956
|
reasoningStarted = true;
|
|
1939
1957
|
}
|
|
1940
1958
|
controller.enqueue({
|
|
1959
|
+
providerMetadata,
|
|
1941
1960
|
type: "reasoning-delta",
|
|
1942
1961
|
delta: chunkText,
|
|
1943
1962
|
id: reasoningId || generateId()
|
|
@@ -1958,23 +1977,28 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1958
1977
|
accumulatedReasoningDetails.push(detail);
|
|
1959
1978
|
}
|
|
1960
1979
|
}
|
|
1980
|
+
const reasoningMetadata = {
|
|
1981
|
+
openrouter: {
|
|
1982
|
+
reasoning_details: delta.reasoning_details
|
|
1983
|
+
}
|
|
1984
|
+
};
|
|
1961
1985
|
for (const detail of delta.reasoning_details) {
|
|
1962
1986
|
switch (detail.type) {
|
|
1963
1987
|
case "reasoning.text" /* Text */: {
|
|
1964
1988
|
if (detail.text) {
|
|
1965
|
-
emitReasoningChunk(detail.text);
|
|
1989
|
+
emitReasoningChunk(detail.text, reasoningMetadata);
|
|
1966
1990
|
}
|
|
1967
1991
|
break;
|
|
1968
1992
|
}
|
|
1969
1993
|
case "reasoning.encrypted" /* Encrypted */: {
|
|
1970
1994
|
if (detail.data) {
|
|
1971
|
-
emitReasoningChunk("[REDACTED]");
|
|
1995
|
+
emitReasoningChunk("[REDACTED]", reasoningMetadata);
|
|
1972
1996
|
}
|
|
1973
1997
|
break;
|
|
1974
1998
|
}
|
|
1975
1999
|
case "reasoning.summary" /* Summary */: {
|
|
1976
2000
|
if (detail.summary) {
|
|
1977
|
-
emitReasoningChunk(detail.summary);
|
|
2001
|
+
emitReasoningChunk(detail.summary, reasoningMetadata);
|
|
1978
2002
|
}
|
|
1979
2003
|
break;
|
|
1980
2004
|
}
|
|
@@ -2029,7 +2053,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
2029
2053
|
}
|
|
2030
2054
|
if (delta.tool_calls != null) {
|
|
2031
2055
|
for (const toolCallDelta of delta.tool_calls) {
|
|
2032
|
-
const index = (
|
|
2056
|
+
const index = (_d = toolCallDelta.index) != null ? _d : toolCalls.length - 1;
|
|
2033
2057
|
if (toolCalls[index] == null) {
|
|
2034
2058
|
if (toolCallDelta.type !== "function") {
|
|
2035
2059
|
throw new InvalidResponseDataError({
|
|
@@ -2043,7 +2067,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
2043
2067
|
message: `Expected 'id' to be a string.`
|
|
2044
2068
|
});
|
|
2045
2069
|
}
|
|
2046
|
-
if (((
|
|
2070
|
+
if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) {
|
|
2047
2071
|
throw new InvalidResponseDataError({
|
|
2048
2072
|
data: toolCallDelta,
|
|
2049
2073
|
message: `Expected 'function.name' to be a string.`
|
|
@@ -2054,7 +2078,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
2054
2078
|
type: "function",
|
|
2055
2079
|
function: {
|
|
2056
2080
|
name: toolCallDelta.function.name,
|
|
2057
|
-
arguments: (
|
|
2081
|
+
arguments: (_f = toolCallDelta.function.arguments) != null ? _f : ""
|
|
2058
2082
|
},
|
|
2059
2083
|
inputStarted: false,
|
|
2060
2084
|
sent: false
|
|
@@ -2066,7 +2090,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
2066
2090
|
message: `Tool call at index ${index} is missing after creation.`
|
|
2067
2091
|
});
|
|
2068
2092
|
}
|
|
2069
|
-
if (((
|
|
2093
|
+
if (((_g = toolCall2.function) == null ? void 0 : _g.name) != null && ((_h = toolCall2.function) == null ? void 0 : _h.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
|
|
2070
2094
|
toolCall2.inputStarted = true;
|
|
2071
2095
|
controller.enqueue({
|
|
2072
2096
|
type: "tool-input-start",
|
|
@@ -2116,18 +2140,18 @@ var OpenRouterChatLanguageModel = class {
|
|
|
2116
2140
|
toolName: toolCall.function.name
|
|
2117
2141
|
});
|
|
2118
2142
|
}
|
|
2119
|
-
if (((
|
|
2120
|
-
toolCall.function.arguments += (
|
|
2143
|
+
if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
|
|
2144
|
+
toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : "";
|
|
2121
2145
|
}
|
|
2122
2146
|
controller.enqueue({
|
|
2123
2147
|
type: "tool-input-delta",
|
|
2124
2148
|
id: toolCall.id,
|
|
2125
|
-
delta: (
|
|
2149
|
+
delta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
|
|
2126
2150
|
});
|
|
2127
|
-
if (((
|
|
2151
|
+
if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall.function.arguments)) {
|
|
2128
2152
|
controller.enqueue({
|
|
2129
2153
|
type: "tool-call",
|
|
2130
|
-
toolCallId: (
|
|
2154
|
+
toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
|
|
2131
2155
|
toolName: toolCall.function.name,
|
|
2132
2156
|
input: toolCall.function.arguments,
|
|
2133
2157
|
providerMetadata: {
|
|
@@ -2343,7 +2367,10 @@ var OpenRouterCompletionChunkSchema = z8.union([
|
|
|
2343
2367
|
reasoning_tokens: z8.number()
|
|
2344
2368
|
}).passthrough().nullish(),
|
|
2345
2369
|
total_tokens: z8.number(),
|
|
2346
|
-
cost: z8.number().optional()
|
|
2370
|
+
cost: z8.number().optional(),
|
|
2371
|
+
cost_details: z8.object({
|
|
2372
|
+
upstream_inference_cost: z8.number().nullish()
|
|
2373
|
+
}).passthrough().nullish()
|
|
2347
2374
|
}).passthrough().nullish()
|
|
2348
2375
|
}).passthrough(),
|
|
2349
2376
|
OpenRouterErrorResponseSchema
|
|
@@ -2516,7 +2543,7 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
2516
2543
|
stream: response.pipeThrough(
|
|
2517
2544
|
new TransformStream({
|
|
2518
2545
|
transform(chunk, controller) {
|
|
2519
|
-
var _a15, _b;
|
|
2546
|
+
var _a15, _b, _c;
|
|
2520
2547
|
if (!chunk.success) {
|
|
2521
2548
|
finishReason = "error";
|
|
2522
2549
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -2550,6 +2577,12 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
2550
2577
|
}
|
|
2551
2578
|
openrouterUsage.cost = value.usage.cost;
|
|
2552
2579
|
openrouterUsage.totalTokens = value.usage.total_tokens;
|
|
2580
|
+
const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
|
|
2581
|
+
if (upstreamInferenceCost != null) {
|
|
2582
|
+
openrouterUsage.costDetails = {
|
|
2583
|
+
upstreamInferenceCost
|
|
2584
|
+
};
|
|
2585
|
+
}
|
|
2553
2586
|
}
|
|
2554
2587
|
const choice = value.choices[0];
|
|
2555
2588
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
@@ -2645,7 +2678,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
|
2645
2678
|
}
|
|
2646
2679
|
|
|
2647
2680
|
// src/version.ts
|
|
2648
|
-
var VERSION = false ? "0.0.0-test" : "1.
|
|
2681
|
+
var VERSION = false ? "0.0.0-test" : "1.3.0";
|
|
2649
2682
|
|
|
2650
2683
|
// src/provider.ts
|
|
2651
2684
|
function createOpenRouter(options = {}) {
|