@openrouter/ai-sdk-provider 2.0.1 → 2.0.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.js +87 -69
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +87 -69
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +86 -68
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +86 -68
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -2478,7 +2478,7 @@ function getCacheControl(providerMetadata) {
|
|
|
2478
2478
|
return (_c = (_b16 = (_a16 = openrouter2 == null ? void 0 : openrouter2.cacheControl) != null ? _a16 : openrouter2 == null ? void 0 : openrouter2.cache_control) != null ? _b16 : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
|
|
2479
2479
|
}
|
|
2480
2480
|
function convertToOpenRouterChatMessages(prompt) {
|
|
2481
|
-
var _a16, _b16, _c, _d, _e, _f, _g, _h
|
|
2481
|
+
var _a16, _b16, _c, _d, _e, _f, _g, _h;
|
|
2482
2482
|
const messages = [];
|
|
2483
2483
|
for (const { role, content, providerOptions } of prompt) {
|
|
2484
2484
|
switch (role) {
|
|
@@ -2507,42 +2507,46 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
2507
2507
|
break;
|
|
2508
2508
|
}
|
|
2509
2509
|
const messageCacheControl = getCacheControl(providerOptions);
|
|
2510
|
+
let lastTextPartIndex = -1;
|
|
2511
|
+
for (let i = content.length - 1; i >= 0; i--) {
|
|
2512
|
+
if (((_c = content[i]) == null ? void 0 : _c.type) === "text") {
|
|
2513
|
+
lastTextPartIndex = i;
|
|
2514
|
+
break;
|
|
2515
|
+
}
|
|
2516
|
+
}
|
|
2510
2517
|
const contentParts = content.map(
|
|
2511
|
-
(part) => {
|
|
2512
|
-
var _a17, _b17, _c2, _d2, _e2, _f2
|
|
2513
|
-
const
|
|
2518
|
+
(part, index) => {
|
|
2519
|
+
var _a17, _b17, _c2, _d2, _e2, _f2;
|
|
2520
|
+
const isLastTextPart = part.type === "text" && index === lastTextPartIndex;
|
|
2521
|
+
const partCacheControl = getCacheControl(part.providerOptions);
|
|
2522
|
+
const cacheControl = part.type === "text" ? partCacheControl != null ? partCacheControl : isLastTextPart ? messageCacheControl : void 0 : partCacheControl;
|
|
2514
2523
|
switch (part.type) {
|
|
2515
2524
|
case "text":
|
|
2516
|
-
return {
|
|
2525
|
+
return __spreadValues({
|
|
2517
2526
|
type: "text",
|
|
2518
|
-
text: part.text
|
|
2519
|
-
|
|
2520
|
-
cache_control: cacheControl
|
|
2521
|
-
};
|
|
2527
|
+
text: part.text
|
|
2528
|
+
}, cacheControl && { cache_control: cacheControl });
|
|
2522
2529
|
case "file": {
|
|
2523
|
-
if ((
|
|
2530
|
+
if ((_a17 = part.mediaType) == null ? void 0 : _a17.startsWith("image/")) {
|
|
2524
2531
|
const url = getFileUrl({
|
|
2525
2532
|
part,
|
|
2526
2533
|
defaultMediaType: "image/jpeg"
|
|
2527
2534
|
});
|
|
2528
|
-
return {
|
|
2535
|
+
return __spreadValues({
|
|
2529
2536
|
type: "image_url",
|
|
2530
2537
|
image_url: {
|
|
2531
2538
|
url
|
|
2532
|
-
}
|
|
2533
|
-
|
|
2534
|
-
cache_control: cacheControl
|
|
2535
|
-
};
|
|
2539
|
+
}
|
|
2540
|
+
}, cacheControl && { cache_control: cacheControl });
|
|
2536
2541
|
}
|
|
2537
|
-
if ((
|
|
2538
|
-
return {
|
|
2542
|
+
if ((_b17 = part.mediaType) == null ? void 0 : _b17.startsWith("audio/")) {
|
|
2543
|
+
return __spreadValues({
|
|
2539
2544
|
type: "input_audio",
|
|
2540
|
-
input_audio: getInputAudioData(part)
|
|
2541
|
-
|
|
2542
|
-
};
|
|
2545
|
+
input_audio: getInputAudioData(part)
|
|
2546
|
+
}, cacheControl && { cache_control: cacheControl });
|
|
2543
2547
|
}
|
|
2544
2548
|
const fileName = String(
|
|
2545
|
-
(
|
|
2549
|
+
(_f2 = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f2 : ""
|
|
2546
2550
|
);
|
|
2547
2551
|
const fileData = getFileUrl({
|
|
2548
2552
|
part,
|
|
@@ -2560,21 +2564,19 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
2560
2564
|
}
|
|
2561
2565
|
};
|
|
2562
2566
|
}
|
|
2563
|
-
return {
|
|
2567
|
+
return __spreadValues({
|
|
2564
2568
|
type: "file",
|
|
2565
2569
|
file: {
|
|
2566
2570
|
filename: fileName,
|
|
2567
2571
|
file_data: fileData
|
|
2568
|
-
}
|
|
2569
|
-
|
|
2570
|
-
};
|
|
2572
|
+
}
|
|
2573
|
+
}, cacheControl && { cache_control: cacheControl });
|
|
2571
2574
|
}
|
|
2572
2575
|
default: {
|
|
2573
|
-
return {
|
|
2576
|
+
return __spreadValues({
|
|
2574
2577
|
type: "text",
|
|
2575
|
-
text: ""
|
|
2576
|
-
|
|
2577
|
-
};
|
|
2578
|
+
text: ""
|
|
2579
|
+
}, cacheControl && { cache_control: cacheControl });
|
|
2578
2580
|
}
|
|
2579
2581
|
}
|
|
2580
2582
|
}
|
|
@@ -2589,7 +2591,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
2589
2591
|
let text = "";
|
|
2590
2592
|
let reasoning = "";
|
|
2591
2593
|
const toolCalls = [];
|
|
2592
|
-
const accumulatedReasoningDetails = [];
|
|
2593
2594
|
for (const part of content) {
|
|
2594
2595
|
switch (part.type) {
|
|
2595
2596
|
case "text": {
|
|
@@ -2597,12 +2598,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
2597
2598
|
break;
|
|
2598
2599
|
}
|
|
2599
2600
|
case "tool-call": {
|
|
2600
|
-
const partReasoningDetails = (_c = part.providerOptions) == null ? void 0 : _c.openrouter;
|
|
2601
|
-
if ((partReasoningDetails == null ? void 0 : partReasoningDetails.reasoning_details) && Array.isArray(partReasoningDetails.reasoning_details)) {
|
|
2602
|
-
accumulatedReasoningDetails.push(
|
|
2603
|
-
...partReasoningDetails.reasoning_details
|
|
2604
|
-
);
|
|
2605
|
-
}
|
|
2606
2601
|
toolCalls.push({
|
|
2607
2602
|
id: part.toolCallId,
|
|
2608
2603
|
type: "function",
|
|
@@ -2615,12 +2610,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
2615
2610
|
}
|
|
2616
2611
|
case "reasoning": {
|
|
2617
2612
|
reasoning += part.text;
|
|
2618
|
-
const parsedPartProviderOptions = OpenRouterProviderOptionsSchema.safeParse(part.providerOptions);
|
|
2619
|
-
if (parsedPartProviderOptions.success && ((_e = (_d = parsedPartProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details)) {
|
|
2620
|
-
accumulatedReasoningDetails.push(
|
|
2621
|
-
...parsedPartProviderOptions.data.openrouter.reasoning_details
|
|
2622
|
-
);
|
|
2623
|
-
}
|
|
2624
2613
|
break;
|
|
2625
2614
|
}
|
|
2626
2615
|
case "file":
|
|
@@ -2631,9 +2620,9 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
2631
2620
|
}
|
|
2632
2621
|
}
|
|
2633
2622
|
const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
|
|
2634
|
-
const messageReasoningDetails = parsedProviderOptions.success ? (
|
|
2635
|
-
const messageAnnotations = parsedProviderOptions.success ? (
|
|
2636
|
-
const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails :
|
|
2623
|
+
const messageReasoningDetails = parsedProviderOptions.success ? (_e = (_d = parsedProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details : void 0;
|
|
2624
|
+
const messageAnnotations = parsedProviderOptions.success ? (_g = (_f = parsedProviderOptions.data) == null ? void 0 : _f.openrouter) == null ? void 0 : _g.annotations : void 0;
|
|
2625
|
+
const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : findFirstReasoningDetails(content);
|
|
2637
2626
|
messages.push({
|
|
2638
2627
|
role: "assistant",
|
|
2639
2628
|
content: text,
|
|
@@ -2655,7 +2644,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
2655
2644
|
role: "tool",
|
|
2656
2645
|
tool_call_id: toolResponse.toolCallId,
|
|
2657
2646
|
content: content2,
|
|
2658
|
-
cache_control: (
|
|
2647
|
+
cache_control: (_h = getCacheControl(providerOptions)) != null ? _h : getCacheControl(toolResponse.providerOptions)
|
|
2659
2648
|
});
|
|
2660
2649
|
}
|
|
2661
2650
|
break;
|
|
@@ -2681,6 +2670,29 @@ function getToolResultContent(input) {
|
|
|
2681
2670
|
return (_a16 = input.output.reason) != null ? _a16 : "Tool execution denied";
|
|
2682
2671
|
}
|
|
2683
2672
|
}
|
|
2673
|
+
function findFirstReasoningDetails(content) {
|
|
2674
|
+
var _a16, _b16, _c;
|
|
2675
|
+
for (const part of content) {
|
|
2676
|
+
if (part.type === "tool-call") {
|
|
2677
|
+
const openrouter2 = (_a16 = part.providerOptions) == null ? void 0 : _a16.openrouter;
|
|
2678
|
+
const details = openrouter2 == null ? void 0 : openrouter2.reasoning_details;
|
|
2679
|
+
if (Array.isArray(details) && details.length > 0) {
|
|
2680
|
+
return details;
|
|
2681
|
+
}
|
|
2682
|
+
}
|
|
2683
|
+
}
|
|
2684
|
+
for (const part of content) {
|
|
2685
|
+
if (part.type === "reasoning") {
|
|
2686
|
+
const parsed = OpenRouterProviderOptionsSchema.safeParse(
|
|
2687
|
+
part.providerOptions
|
|
2688
|
+
);
|
|
2689
|
+
if (parsed.success && ((_c = (_b16 = parsed.data) == null ? void 0 : _b16.openrouter) == null ? void 0 : _c.reasoning_details) && parsed.data.openrouter.reasoning_details.length > 0) {
|
|
2690
|
+
return parsed.data.openrouter.reasoning_details;
|
|
2691
|
+
}
|
|
2692
|
+
}
|
|
2693
|
+
}
|
|
2694
|
+
return void 0;
|
|
2695
|
+
}
|
|
2684
2696
|
|
|
2685
2697
|
// src/chat/get-tool-choice.ts
|
|
2686
2698
|
var import_v44 = require("zod/v4");
|
|
@@ -2772,7 +2784,7 @@ var OpenRouterNonStreamChatCompletionResponseSchema = import_v46.z.union([
|
|
|
2772
2784
|
type: import_v46.z.literal("function"),
|
|
2773
2785
|
function: import_v46.z.object({
|
|
2774
2786
|
name: import_v46.z.string(),
|
|
2775
|
-
arguments: import_v46.z.string()
|
|
2787
|
+
arguments: import_v46.z.string().optional()
|
|
2776
2788
|
}).passthrough()
|
|
2777
2789
|
}).passthrough()
|
|
2778
2790
|
).optional(),
|
|
@@ -3015,7 +3027,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3015
3027
|
return baseArgs;
|
|
3016
3028
|
}
|
|
3017
3029
|
async doGenerate(options) {
|
|
3018
|
-
var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
|
|
3030
|
+
var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
|
|
3019
3031
|
const providerOptions = options.providerOptions || {};
|
|
3020
3032
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
3021
3033
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -3144,18 +3156,20 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3144
3156
|
});
|
|
3145
3157
|
}
|
|
3146
3158
|
if (choice.message.tool_calls) {
|
|
3159
|
+
let reasoningDetailsAttachedToToolCall = false;
|
|
3147
3160
|
for (const toolCall of choice.message.tool_calls) {
|
|
3148
3161
|
content.push({
|
|
3149
3162
|
type: "tool-call",
|
|
3150
3163
|
toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
|
|
3151
3164
|
toolName: toolCall.function.name,
|
|
3152
|
-
input: toolCall.function.arguments,
|
|
3153
|
-
providerMetadata: {
|
|
3165
|
+
input: (_i = toolCall.function.arguments) != null ? _i : "{}",
|
|
3166
|
+
providerMetadata: !reasoningDetailsAttachedToToolCall ? {
|
|
3154
3167
|
openrouter: {
|
|
3155
3168
|
reasoning_details: reasoningDetails
|
|
3156
3169
|
}
|
|
3157
|
-
}
|
|
3170
|
+
} : void 0
|
|
3158
3171
|
});
|
|
3172
|
+
reasoningDetailsAttachedToToolCall = true;
|
|
3159
3173
|
}
|
|
3160
3174
|
}
|
|
3161
3175
|
if (choice.message.images) {
|
|
@@ -3185,7 +3199,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3185
3199
|
}
|
|
3186
3200
|
}
|
|
3187
3201
|
}
|
|
3188
|
-
const fileAnnotations = (
|
|
3202
|
+
const fileAnnotations = (_j = choice.message.annotations) == null ? void 0 : _j.filter(
|
|
3189
3203
|
(a) => a.type === "file"
|
|
3190
3204
|
);
|
|
3191
3205
|
const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
|
|
@@ -3193,7 +3207,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3193
3207
|
(d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
|
|
3194
3208
|
);
|
|
3195
3209
|
const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
|
|
3196
|
-
const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (
|
|
3210
|
+
const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_k = choice.finish_reason) != null ? _k : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
|
|
3197
3211
|
return {
|
|
3198
3212
|
content,
|
|
3199
3213
|
finishReason: effectiveFinishReason,
|
|
@@ -3201,23 +3215,23 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3201
3215
|
warnings: [],
|
|
3202
3216
|
providerMetadata: {
|
|
3203
3217
|
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
3204
|
-
provider: (
|
|
3205
|
-
reasoning_details: (
|
|
3218
|
+
provider: (_l = response.provider) != null ? _l : "",
|
|
3219
|
+
reasoning_details: (_m = choice.message.reasoning_details) != null ? _m : [],
|
|
3206
3220
|
annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
|
|
3207
3221
|
usage: __spreadValues(__spreadValues(__spreadValues({
|
|
3208
|
-
promptTokens: (
|
|
3209
|
-
completionTokens: (
|
|
3210
|
-
totalTokens: ((
|
|
3211
|
-
cost: (
|
|
3212
|
-
}, ((
|
|
3222
|
+
promptTokens: (_n = usageInfo.inputTokens.total) != null ? _n : 0,
|
|
3223
|
+
completionTokens: (_o = usageInfo.outputTokens.total) != null ? _o : 0,
|
|
3224
|
+
totalTokens: ((_p = usageInfo.inputTokens.total) != null ? _p : 0) + ((_q = usageInfo.outputTokens.total) != null ? _q : 0),
|
|
3225
|
+
cost: (_r = response.usage) == null ? void 0 : _r.cost
|
|
3226
|
+
}, ((_t = (_s = response.usage) == null ? void 0 : _s.prompt_tokens_details) == null ? void 0 : _t.cached_tokens) != null ? {
|
|
3213
3227
|
promptTokensDetails: {
|
|
3214
3228
|
cachedTokens: response.usage.prompt_tokens_details.cached_tokens
|
|
3215
3229
|
}
|
|
3216
|
-
} : {}), ((
|
|
3230
|
+
} : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.completion_tokens_details) == null ? void 0 : _v.reasoning_tokens) != null ? {
|
|
3217
3231
|
completionTokensDetails: {
|
|
3218
3232
|
reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
|
|
3219
3233
|
}
|
|
3220
|
-
} : {}), ((
|
|
3234
|
+
} : {}), ((_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? {
|
|
3221
3235
|
costDetails: {
|
|
3222
3236
|
upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
|
|
3223
3237
|
}
|
|
@@ -3274,6 +3288,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3274
3288
|
};
|
|
3275
3289
|
const openrouterUsage = {};
|
|
3276
3290
|
const accumulatedReasoningDetails = [];
|
|
3291
|
+
let reasoningDetailsAttachedToToolCall = false;
|
|
3277
3292
|
const accumulatedFileAnnotations = [];
|
|
3278
3293
|
let textStarted = false;
|
|
3279
3294
|
let reasoningStarted = false;
|
|
@@ -3522,12 +3537,13 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3522
3537
|
toolCallId: toolCall2.id,
|
|
3523
3538
|
toolName: toolCall2.function.name,
|
|
3524
3539
|
input: toolCall2.function.arguments,
|
|
3525
|
-
providerMetadata: {
|
|
3540
|
+
providerMetadata: !reasoningDetailsAttachedToToolCall ? {
|
|
3526
3541
|
openrouter: {
|
|
3527
3542
|
reasoning_details: accumulatedReasoningDetails
|
|
3528
3543
|
}
|
|
3529
|
-
}
|
|
3544
|
+
} : void 0
|
|
3530
3545
|
});
|
|
3546
|
+
reasoningDetailsAttachedToToolCall = true;
|
|
3531
3547
|
toolCall2.sent = true;
|
|
3532
3548
|
}
|
|
3533
3549
|
continue;
|
|
@@ -3565,12 +3581,13 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3565
3581
|
toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
|
|
3566
3582
|
toolName: toolCall.function.name,
|
|
3567
3583
|
input: toolCall.function.arguments,
|
|
3568
|
-
providerMetadata: {
|
|
3584
|
+
providerMetadata: !reasoningDetailsAttachedToToolCall ? {
|
|
3569
3585
|
openrouter: {
|
|
3570
3586
|
reasoning_details: accumulatedReasoningDetails
|
|
3571
3587
|
}
|
|
3572
|
-
}
|
|
3588
|
+
} : void 0
|
|
3573
3589
|
});
|
|
3590
|
+
reasoningDetailsAttachedToToolCall = true;
|
|
3574
3591
|
toolCall.sent = true;
|
|
3575
3592
|
}
|
|
3576
3593
|
}
|
|
@@ -3603,12 +3620,13 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3603
3620
|
toolName: toolCall.function.name,
|
|
3604
3621
|
// Coerce invalid arguments to an empty JSON object
|
|
3605
3622
|
input: isParsableJson(toolCall.function.arguments) ? toolCall.function.arguments : "{}",
|
|
3606
|
-
providerMetadata: {
|
|
3623
|
+
providerMetadata: !reasoningDetailsAttachedToToolCall ? {
|
|
3607
3624
|
openrouter: {
|
|
3608
3625
|
reasoning_details: accumulatedReasoningDetails
|
|
3609
3626
|
}
|
|
3610
|
-
}
|
|
3627
|
+
} : void 0
|
|
3611
3628
|
});
|
|
3629
|
+
reasoningDetailsAttachedToToolCall = true;
|
|
3612
3630
|
toolCall.sent = true;
|
|
3613
3631
|
}
|
|
3614
3632
|
}
|
|
@@ -4200,7 +4218,7 @@ function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
|
|
|
4200
4218
|
}
|
|
4201
4219
|
|
|
4202
4220
|
// src/version.ts
|
|
4203
|
-
var VERSION2 = false ? "0.0.0-test" : "2.0.
|
|
4221
|
+
var VERSION2 = false ? "0.0.0-test" : "2.0.4";
|
|
4204
4222
|
|
|
4205
4223
|
// src/provider.ts
|
|
4206
4224
|
function createOpenRouter(options = {}) {
|