@openrouter/ai-sdk-provider 2.0.4 → 2.1.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 +28 -0
- package/dist/index.d.mts +147 -12
- package/dist/index.d.ts +147 -12
- package/dist/index.js +290 -60
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +290 -60
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +181 -12
- package/dist/internal/index.d.ts +181 -12
- package/dist/internal/index.js +324 -46
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +321 -45
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +1 -4
package/dist/internal/index.mjs
CHANGED
|
@@ -2723,13 +2723,14 @@ var OpenRouterNonStreamChatCompletionResponseSchema = z7.union([
|
|
|
2723
2723
|
annotations: z7.array(
|
|
2724
2724
|
z7.union([
|
|
2725
2725
|
// URL citation from web search
|
|
2726
|
+
// title, start_index, end_index are optional as some upstream providers may omit them
|
|
2726
2727
|
z7.object({
|
|
2727
2728
|
type: z7.literal("url_citation"),
|
|
2728
2729
|
url_citation: z7.object({
|
|
2729
|
-
end_index: z7.number(),
|
|
2730
|
-
start_index: z7.number(),
|
|
2731
|
-
title: z7.string(),
|
|
2732
2730
|
url: z7.string(),
|
|
2731
|
+
title: z7.string().optional(),
|
|
2732
|
+
start_index: z7.number().optional(),
|
|
2733
|
+
end_index: z7.number().optional(),
|
|
2733
2734
|
content: z7.string().optional()
|
|
2734
2735
|
}).passthrough()
|
|
2735
2736
|
}).passthrough(),
|
|
@@ -2806,13 +2807,14 @@ var OpenRouterStreamChatCompletionChunkSchema = z7.union([
|
|
|
2806
2807
|
annotations: z7.array(
|
|
2807
2808
|
z7.union([
|
|
2808
2809
|
// URL citation from web search
|
|
2810
|
+
// title, start_index, end_index are optional as some upstream providers may omit them
|
|
2809
2811
|
z7.object({
|
|
2810
2812
|
type: z7.literal("url_citation"),
|
|
2811
2813
|
url_citation: z7.object({
|
|
2812
|
-
end_index: z7.number(),
|
|
2813
|
-
start_index: z7.number(),
|
|
2814
|
-
title: z7.string(),
|
|
2815
2814
|
url: z7.string(),
|
|
2815
|
+
title: z7.string().optional(),
|
|
2816
|
+
start_index: z7.number().optional(),
|
|
2817
|
+
end_index: z7.number().optional(),
|
|
2816
2818
|
content: z7.string().optional()
|
|
2817
2819
|
}).passthrough()
|
|
2818
2820
|
}).passthrough(),
|
|
@@ -2959,7 +2961,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
2959
2961
|
return baseArgs;
|
|
2960
2962
|
}
|
|
2961
2963
|
async doGenerate(options) {
|
|
2962
|
-
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;
|
|
2964
|
+
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, _y, _z, _A, _B;
|
|
2963
2965
|
const providerOptions = options.providerOptions || {};
|
|
2964
2966
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
2965
2967
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -3009,7 +3011,8 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3009
3011
|
total: (_d = response.usage.completion_tokens) != null ? _d : 0,
|
|
3010
3012
|
text: void 0,
|
|
3011
3013
|
reasoning: (_f = (_e = response.usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : void 0
|
|
3012
|
-
}
|
|
3014
|
+
},
|
|
3015
|
+
raw: response.usage
|
|
3013
3016
|
} : {
|
|
3014
3017
|
inputTokens: {
|
|
3015
3018
|
total: 0,
|
|
@@ -3021,7 +3024,8 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3021
3024
|
total: 0,
|
|
3022
3025
|
text: void 0,
|
|
3023
3026
|
reasoning: void 0
|
|
3024
|
-
}
|
|
3027
|
+
},
|
|
3028
|
+
raw: void 0
|
|
3025
3029
|
};
|
|
3026
3030
|
const reasoningDetails = (_g = choice.message.reasoning_details) != null ? _g : [];
|
|
3027
3031
|
const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
|
|
@@ -3121,17 +3125,19 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3121
3125
|
sourceType: "url",
|
|
3122
3126
|
id: annotation.url_citation.url,
|
|
3123
3127
|
url: annotation.url_citation.url,
|
|
3124
|
-
title: annotation.url_citation.title,
|
|
3128
|
+
title: (_j = annotation.url_citation.title) != null ? _j : "",
|
|
3125
3129
|
providerMetadata: {
|
|
3126
3130
|
openrouter: {
|
|
3127
|
-
content: annotation.url_citation.content
|
|
3131
|
+
content: (_k = annotation.url_citation.content) != null ? _k : "",
|
|
3132
|
+
startIndex: (_l = annotation.url_citation.start_index) != null ? _l : 0,
|
|
3133
|
+
endIndex: (_m = annotation.url_citation.end_index) != null ? _m : 0
|
|
3128
3134
|
}
|
|
3129
3135
|
}
|
|
3130
3136
|
});
|
|
3131
3137
|
}
|
|
3132
3138
|
}
|
|
3133
3139
|
}
|
|
3134
|
-
const fileAnnotations = (
|
|
3140
|
+
const fileAnnotations = (_n = choice.message.annotations) == null ? void 0 : _n.filter(
|
|
3135
3141
|
(a) => a.type === "file"
|
|
3136
3142
|
);
|
|
3137
3143
|
const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
|
|
@@ -3139,7 +3145,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3139
3145
|
(d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
|
|
3140
3146
|
);
|
|
3141
3147
|
const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
|
|
3142
|
-
const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (
|
|
3148
|
+
const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_o = choice.finish_reason) != null ? _o : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
|
|
3143
3149
|
return {
|
|
3144
3150
|
content,
|
|
3145
3151
|
finishReason: effectiveFinishReason,
|
|
@@ -3147,23 +3153,22 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3147
3153
|
warnings: [],
|
|
3148
3154
|
providerMetadata: {
|
|
3149
3155
|
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
3150
|
-
provider: (
|
|
3151
|
-
reasoning_details: (
|
|
3156
|
+
provider: (_p = response.provider) != null ? _p : "",
|
|
3157
|
+
reasoning_details: (_q = choice.message.reasoning_details) != null ? _q : [],
|
|
3152
3158
|
annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
|
|
3153
|
-
usage: __spreadValues(__spreadValues(__spreadValues({
|
|
3154
|
-
promptTokens: (
|
|
3155
|
-
completionTokens: (
|
|
3156
|
-
totalTokens: ((
|
|
3157
|
-
|
|
3158
|
-
}, ((_t = (_s = response.usage) == null ? void 0 : _s.prompt_tokens_details) == null ? void 0 : _t.cached_tokens) != null ? {
|
|
3159
|
+
usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
3160
|
+
promptTokens: (_r = usageInfo.inputTokens.total) != null ? _r : 0,
|
|
3161
|
+
completionTokens: (_s = usageInfo.outputTokens.total) != null ? _s : 0,
|
|
3162
|
+
totalTokens: ((_t = usageInfo.inputTokens.total) != null ? _t : 0) + ((_u = usageInfo.outputTokens.total) != null ? _u : 0)
|
|
3163
|
+
}, ((_v = response.usage) == null ? void 0 : _v.cost) != null ? { cost: response.usage.cost } : {}), ((_x = (_w = response.usage) == null ? void 0 : _w.prompt_tokens_details) == null ? void 0 : _x.cached_tokens) != null ? {
|
|
3159
3164
|
promptTokensDetails: {
|
|
3160
3165
|
cachedTokens: response.usage.prompt_tokens_details.cached_tokens
|
|
3161
3166
|
}
|
|
3162
|
-
} : {}), ((
|
|
3167
|
+
} : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
|
|
3163
3168
|
completionTokensDetails: {
|
|
3164
3169
|
reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
|
|
3165
3170
|
}
|
|
3166
|
-
} : {}), ((
|
|
3171
|
+
} : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
|
|
3167
3172
|
costDetails: {
|
|
3168
3173
|
upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
|
|
3169
3174
|
}
|
|
@@ -3216,9 +3221,11 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3216
3221
|
total: void 0,
|
|
3217
3222
|
text: void 0,
|
|
3218
3223
|
reasoning: void 0
|
|
3219
|
-
}
|
|
3224
|
+
},
|
|
3225
|
+
raw: void 0
|
|
3220
3226
|
};
|
|
3221
3227
|
const openrouterUsage = {};
|
|
3228
|
+
let rawUsage;
|
|
3222
3229
|
const accumulatedReasoningDetails = [];
|
|
3223
3230
|
let reasoningDetailsAttachedToToolCall = false;
|
|
3224
3231
|
const accumulatedFileAnnotations = [];
|
|
@@ -3232,7 +3239,10 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3232
3239
|
stream: response.pipeThrough(
|
|
3233
3240
|
new TransformStream({
|
|
3234
3241
|
transform(chunk, controller) {
|
|
3235
|
-
var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
3242
|
+
var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
|
|
3243
|
+
if (options.includeRawChunks) {
|
|
3244
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
3245
|
+
}
|
|
3236
3246
|
if (!chunk.success) {
|
|
3237
3247
|
finishReason = createFinishReason("error");
|
|
3238
3248
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -3263,6 +3273,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3263
3273
|
if (value.usage != null) {
|
|
3264
3274
|
usage.inputTokens.total = value.usage.prompt_tokens;
|
|
3265
3275
|
usage.outputTokens.total = value.usage.completion_tokens;
|
|
3276
|
+
rawUsage = value.usage;
|
|
3266
3277
|
openrouterUsage.promptTokens = value.usage.prompt_tokens;
|
|
3267
3278
|
if (value.usage.prompt_tokens_details) {
|
|
3268
3279
|
const cachedInputTokens = (_a17 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a17 : 0;
|
|
@@ -3279,7 +3290,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3279
3290
|
reasoningTokens
|
|
3280
3291
|
};
|
|
3281
3292
|
}
|
|
3282
|
-
|
|
3293
|
+
if (value.usage.cost != null) {
|
|
3294
|
+
openrouterUsage.cost = value.usage.cost;
|
|
3295
|
+
}
|
|
3283
3296
|
openrouterUsage.totalTokens = value.usage.total_tokens;
|
|
3284
3297
|
const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
|
|
3285
3298
|
if (upstreamInferenceCost != null) {
|
|
@@ -3392,10 +3405,12 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3392
3405
|
sourceType: "url",
|
|
3393
3406
|
id: annotation.url_citation.url,
|
|
3394
3407
|
url: annotation.url_citation.url,
|
|
3395
|
-
title: annotation.url_citation.title,
|
|
3408
|
+
title: (_d = annotation.url_citation.title) != null ? _d : "",
|
|
3396
3409
|
providerMetadata: {
|
|
3397
3410
|
openrouter: {
|
|
3398
|
-
content: annotation.url_citation.content
|
|
3411
|
+
content: (_e = annotation.url_citation.content) != null ? _e : "",
|
|
3412
|
+
startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
|
|
3413
|
+
endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
|
|
3399
3414
|
}
|
|
3400
3415
|
}
|
|
3401
3416
|
});
|
|
@@ -3411,7 +3426,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3411
3426
|
}
|
|
3412
3427
|
if (delta.tool_calls != null) {
|
|
3413
3428
|
for (const toolCallDelta of delta.tool_calls) {
|
|
3414
|
-
const index = (
|
|
3429
|
+
const index = (_h = toolCallDelta.index) != null ? _h : toolCalls.length - 1;
|
|
3415
3430
|
if (toolCalls[index] == null) {
|
|
3416
3431
|
if (toolCallDelta.type !== "function") {
|
|
3417
3432
|
throw new InvalidResponseDataError({
|
|
@@ -3425,7 +3440,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3425
3440
|
message: `Expected 'id' to be a string.`
|
|
3426
3441
|
});
|
|
3427
3442
|
}
|
|
3428
|
-
if (((
|
|
3443
|
+
if (((_i = toolCallDelta.function) == null ? void 0 : _i.name) == null) {
|
|
3429
3444
|
throw new InvalidResponseDataError({
|
|
3430
3445
|
data: toolCallDelta,
|
|
3431
3446
|
message: `Expected 'function.name' to be a string.`
|
|
@@ -3436,7 +3451,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3436
3451
|
type: "function",
|
|
3437
3452
|
function: {
|
|
3438
3453
|
name: toolCallDelta.function.name,
|
|
3439
|
-
arguments: (
|
|
3454
|
+
arguments: (_j = toolCallDelta.function.arguments) != null ? _j : ""
|
|
3440
3455
|
},
|
|
3441
3456
|
inputStarted: false,
|
|
3442
3457
|
sent: false
|
|
@@ -3448,7 +3463,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3448
3463
|
message: `Tool call at index ${index} is missing after creation.`
|
|
3449
3464
|
});
|
|
3450
3465
|
}
|
|
3451
|
-
if (((
|
|
3466
|
+
if (((_k = toolCall2.function) == null ? void 0 : _k.name) != null && ((_l = toolCall2.function) == null ? void 0 : _l.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
|
|
3452
3467
|
toolCall2.inputStarted = true;
|
|
3453
3468
|
controller.enqueue({
|
|
3454
3469
|
type: "tool-input-start",
|
|
@@ -3499,18 +3514,18 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3499
3514
|
toolName: toolCall.function.name
|
|
3500
3515
|
});
|
|
3501
3516
|
}
|
|
3502
|
-
if (((
|
|
3503
|
-
toolCall.function.arguments += (
|
|
3517
|
+
if (((_m = toolCallDelta.function) == null ? void 0 : _m.arguments) != null) {
|
|
3518
|
+
toolCall.function.arguments += (_o = (_n = toolCallDelta.function) == null ? void 0 : _n.arguments) != null ? _o : "";
|
|
3504
3519
|
}
|
|
3505
3520
|
controller.enqueue({
|
|
3506
3521
|
type: "tool-input-delta",
|
|
3507
3522
|
id: toolCall.id,
|
|
3508
|
-
delta: (
|
|
3523
|
+
delta: (_p = toolCallDelta.function.arguments) != null ? _p : ""
|
|
3509
3524
|
});
|
|
3510
|
-
if (((
|
|
3525
|
+
if (((_q = toolCall.function) == null ? void 0 : _q.name) != null && ((_r = toolCall.function) == null ? void 0 : _r.arguments) != null && isParsableJson(toolCall.function.arguments)) {
|
|
3511
3526
|
controller.enqueue({
|
|
3512
3527
|
type: "tool-call",
|
|
3513
|
-
toolCallId: (
|
|
3528
|
+
toolCallId: (_s = toolCall.id) != null ? _s : generateId(),
|
|
3514
3529
|
toolName: toolCall.function.name,
|
|
3515
3530
|
input: toolCall.function.arguments,
|
|
3516
3531
|
providerMetadata: !reasoningDetailsAttachedToToolCall ? {
|
|
@@ -3587,6 +3602,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3587
3602
|
if (accumulatedFileAnnotations.length > 0) {
|
|
3588
3603
|
openrouterMetadata.annotations = accumulatedFileAnnotations;
|
|
3589
3604
|
}
|
|
3605
|
+
usage.raw = rawUsage;
|
|
3590
3606
|
controller.enqueue({
|
|
3591
3607
|
type: "finish",
|
|
3592
3608
|
finishReason,
|
|
@@ -3714,6 +3730,7 @@ var OpenRouterCompletionChunkSchema = z8.union([
|
|
|
3714
3730
|
z8.object({
|
|
3715
3731
|
id: z8.string().optional(),
|
|
3716
3732
|
model: z8.string().optional(),
|
|
3733
|
+
provider: z8.string().optional(),
|
|
3717
3734
|
choices: z8.array(
|
|
3718
3735
|
z8.object({
|
|
3719
3736
|
text: z8.string(),
|
|
@@ -3821,7 +3838,7 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
3821
3838
|
}, this.config.extraBody), this.settings.extraBody);
|
|
3822
3839
|
}
|
|
3823
3840
|
async doGenerate(options) {
|
|
3824
|
-
var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
3841
|
+
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, _y, _z, _A, _B;
|
|
3825
3842
|
const providerOptions = options.providerOptions || {};
|
|
3826
3843
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
3827
3844
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -3878,9 +3895,32 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
3878
3895
|
total: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : 0,
|
|
3879
3896
|
text: void 0,
|
|
3880
3897
|
reasoning: (_k = (_j = (_i = response.usage) == null ? void 0 : _i.completion_tokens_details) == null ? void 0 : _j.reasoning_tokens) != null ? _k : void 0
|
|
3881
|
-
}
|
|
3898
|
+
},
|
|
3899
|
+
raw: (_l = response.usage) != null ? _l : void 0
|
|
3882
3900
|
},
|
|
3883
3901
|
warnings: [],
|
|
3902
|
+
providerMetadata: {
|
|
3903
|
+
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
3904
|
+
provider: (_m = response.provider) != null ? _m : "",
|
|
3905
|
+
usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
3906
|
+
promptTokens: (_o = (_n = response.usage) == null ? void 0 : _n.prompt_tokens) != null ? _o : 0,
|
|
3907
|
+
completionTokens: (_q = (_p = response.usage) == null ? void 0 : _p.completion_tokens) != null ? _q : 0,
|
|
3908
|
+
totalTokens: ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens) != null ? _s : 0) + ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens) != null ? _u : 0)
|
|
3909
|
+
}, ((_v = response.usage) == null ? void 0 : _v.cost) != null ? { cost: response.usage.cost } : {}), ((_x = (_w = response.usage) == null ? void 0 : _w.prompt_tokens_details) == null ? void 0 : _x.cached_tokens) != null ? {
|
|
3910
|
+
promptTokensDetails: {
|
|
3911
|
+
cachedTokens: response.usage.prompt_tokens_details.cached_tokens
|
|
3912
|
+
}
|
|
3913
|
+
} : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
|
|
3914
|
+
completionTokensDetails: {
|
|
3915
|
+
reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
|
|
3916
|
+
}
|
|
3917
|
+
} : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
|
|
3918
|
+
costDetails: {
|
|
3919
|
+
upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
|
|
3920
|
+
}
|
|
3921
|
+
} : {})
|
|
3922
|
+
})
|
|
3923
|
+
},
|
|
3884
3924
|
response: {
|
|
3885
3925
|
headers: responseHeaders
|
|
3886
3926
|
}
|
|
@@ -3920,14 +3960,20 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
3920
3960
|
total: void 0,
|
|
3921
3961
|
text: void 0,
|
|
3922
3962
|
reasoning: void 0
|
|
3923
|
-
}
|
|
3963
|
+
},
|
|
3964
|
+
raw: void 0
|
|
3924
3965
|
};
|
|
3925
3966
|
const openrouterUsage = {};
|
|
3967
|
+
let provider;
|
|
3968
|
+
let rawUsage;
|
|
3926
3969
|
return {
|
|
3927
3970
|
stream: response.pipeThrough(
|
|
3928
3971
|
new TransformStream({
|
|
3929
3972
|
transform(chunk, controller) {
|
|
3930
3973
|
var _a16, _b16, _c;
|
|
3974
|
+
if (options.includeRawChunks) {
|
|
3975
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
3976
|
+
}
|
|
3931
3977
|
if (!chunk.success) {
|
|
3932
3978
|
finishReason = createFinishReason("error");
|
|
3933
3979
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -3939,9 +3985,13 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
3939
3985
|
controller.enqueue({ type: "error", error: value.error });
|
|
3940
3986
|
return;
|
|
3941
3987
|
}
|
|
3988
|
+
if (value.provider) {
|
|
3989
|
+
provider = value.provider;
|
|
3990
|
+
}
|
|
3942
3991
|
if (value.usage != null) {
|
|
3943
3992
|
usage.inputTokens.total = value.usage.prompt_tokens;
|
|
3944
3993
|
usage.outputTokens.total = value.usage.completion_tokens;
|
|
3994
|
+
rawUsage = value.usage;
|
|
3945
3995
|
openrouterUsage.promptTokens = value.usage.prompt_tokens;
|
|
3946
3996
|
if (value.usage.prompt_tokens_details) {
|
|
3947
3997
|
const cachedInputTokens = (_a16 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a16 : 0;
|
|
@@ -3958,7 +4008,9 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
3958
4008
|
reasoningTokens
|
|
3959
4009
|
};
|
|
3960
4010
|
}
|
|
3961
|
-
|
|
4011
|
+
if (value.usage.cost != null) {
|
|
4012
|
+
openrouterUsage.cost = value.usage.cost;
|
|
4013
|
+
}
|
|
3962
4014
|
openrouterUsage.totalTokens = value.usage.total_tokens;
|
|
3963
4015
|
const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
|
|
3964
4016
|
if (upstreamInferenceCost != null) {
|
|
@@ -3980,14 +4032,19 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
3980
4032
|
}
|
|
3981
4033
|
},
|
|
3982
4034
|
flush(controller) {
|
|
4035
|
+
usage.raw = rawUsage;
|
|
4036
|
+
const openrouterMetadata = {
|
|
4037
|
+
usage: openrouterUsage
|
|
4038
|
+
};
|
|
4039
|
+
if (provider !== void 0) {
|
|
4040
|
+
openrouterMetadata.provider = provider;
|
|
4041
|
+
}
|
|
3983
4042
|
controller.enqueue({
|
|
3984
4043
|
type: "finish",
|
|
3985
4044
|
finishReason,
|
|
3986
4045
|
usage,
|
|
3987
4046
|
providerMetadata: {
|
|
3988
|
-
openrouter:
|
|
3989
|
-
usage: openrouterUsage
|
|
3990
|
-
}
|
|
4047
|
+
openrouter: openrouterMetadata
|
|
3991
4048
|
}
|
|
3992
4049
|
});
|
|
3993
4050
|
}
|
|
@@ -3999,8 +4056,227 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
3999
4056
|
};
|
|
4000
4057
|
}
|
|
4001
4058
|
};
|
|
4059
|
+
|
|
4060
|
+
// src/embedding/schemas.ts
|
|
4061
|
+
import { z as z9 } from "zod/v4";
|
|
4062
|
+
var openrouterEmbeddingUsageSchema = z9.object({
|
|
4063
|
+
prompt_tokens: z9.number(),
|
|
4064
|
+
total_tokens: z9.number(),
|
|
4065
|
+
cost: z9.number().optional()
|
|
4066
|
+
});
|
|
4067
|
+
var openrouterEmbeddingDataSchema = z9.object({
|
|
4068
|
+
object: z9.literal("embedding"),
|
|
4069
|
+
embedding: z9.array(z9.number()),
|
|
4070
|
+
index: z9.number().optional()
|
|
4071
|
+
});
|
|
4072
|
+
var OpenRouterEmbeddingResponseSchema = z9.object({
|
|
4073
|
+
id: z9.string().optional(),
|
|
4074
|
+
object: z9.literal("list"),
|
|
4075
|
+
data: z9.array(openrouterEmbeddingDataSchema),
|
|
4076
|
+
model: z9.string(),
|
|
4077
|
+
provider: z9.string().optional(),
|
|
4078
|
+
usage: openrouterEmbeddingUsageSchema.optional()
|
|
4079
|
+
});
|
|
4080
|
+
|
|
4081
|
+
// src/embedding/index.ts
|
|
4082
|
+
var OpenRouterEmbeddingModel = class {
|
|
4083
|
+
constructor(modelId, settings, config) {
|
|
4084
|
+
this.specificationVersion = "v3";
|
|
4085
|
+
this.provider = "openrouter";
|
|
4086
|
+
this.maxEmbeddingsPerCall = void 0;
|
|
4087
|
+
this.supportsParallelCalls = true;
|
|
4088
|
+
this.modelId = modelId;
|
|
4089
|
+
this.settings = settings;
|
|
4090
|
+
this.config = config;
|
|
4091
|
+
}
|
|
4092
|
+
async doEmbed(options) {
|
|
4093
|
+
var _a16, _b16, _c, _d, _e, _f;
|
|
4094
|
+
const { values, abortSignal, headers } = options;
|
|
4095
|
+
const args = __spreadValues(__spreadValues({
|
|
4096
|
+
model: this.modelId,
|
|
4097
|
+
input: values,
|
|
4098
|
+
user: this.settings.user,
|
|
4099
|
+
provider: this.settings.provider
|
|
4100
|
+
}, this.config.extraBody), this.settings.extraBody);
|
|
4101
|
+
const { value: responseValue, responseHeaders } = await postJsonToApi({
|
|
4102
|
+
url: this.config.url({
|
|
4103
|
+
path: "/embeddings",
|
|
4104
|
+
modelId: this.modelId
|
|
4105
|
+
}),
|
|
4106
|
+
headers: combineHeaders(this.config.headers(), headers),
|
|
4107
|
+
body: args,
|
|
4108
|
+
failedResponseHandler: openrouterFailedResponseHandler,
|
|
4109
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
4110
|
+
OpenRouterEmbeddingResponseSchema
|
|
4111
|
+
),
|
|
4112
|
+
abortSignal,
|
|
4113
|
+
fetch: this.config.fetch
|
|
4114
|
+
});
|
|
4115
|
+
return {
|
|
4116
|
+
embeddings: responseValue.data.map((item) => item.embedding),
|
|
4117
|
+
usage: responseValue.usage ? { tokens: responseValue.usage.prompt_tokens } : void 0,
|
|
4118
|
+
providerMetadata: {
|
|
4119
|
+
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
4120
|
+
provider: (_a16 = responseValue.provider) != null ? _a16 : "",
|
|
4121
|
+
usage: __spreadValues({
|
|
4122
|
+
promptTokens: (_c = (_b16 = responseValue.usage) == null ? void 0 : _b16.prompt_tokens) != null ? _c : 0,
|
|
4123
|
+
completionTokens: 0,
|
|
4124
|
+
totalTokens: (_e = (_d = responseValue.usage) == null ? void 0 : _d.total_tokens) != null ? _e : 0
|
|
4125
|
+
}, ((_f = responseValue.usage) == null ? void 0 : _f.cost) != null ? { cost: responseValue.usage.cost } : {})
|
|
4126
|
+
})
|
|
4127
|
+
},
|
|
4128
|
+
response: {
|
|
4129
|
+
headers: responseHeaders,
|
|
4130
|
+
body: responseValue
|
|
4131
|
+
},
|
|
4132
|
+
warnings: []
|
|
4133
|
+
};
|
|
4134
|
+
}
|
|
4135
|
+
};
|
|
4136
|
+
|
|
4137
|
+
// src/image/schemas.ts
|
|
4138
|
+
import { z as z10 } from "zod/v4";
|
|
4139
|
+
var OpenRouterImageResponseSchema = z10.object({
|
|
4140
|
+
id: z10.string().optional(),
|
|
4141
|
+
object: z10.string().optional(),
|
|
4142
|
+
created: z10.number().optional(),
|
|
4143
|
+
model: z10.string(),
|
|
4144
|
+
choices: z10.array(
|
|
4145
|
+
z10.object({
|
|
4146
|
+
index: z10.number(),
|
|
4147
|
+
message: z10.object({
|
|
4148
|
+
role: z10.string(),
|
|
4149
|
+
content: z10.string().nullable().optional(),
|
|
4150
|
+
images: z10.array(
|
|
4151
|
+
z10.object({
|
|
4152
|
+
type: z10.literal("image_url"),
|
|
4153
|
+
image_url: z10.object({
|
|
4154
|
+
url: z10.string()
|
|
4155
|
+
})
|
|
4156
|
+
}).passthrough()
|
|
4157
|
+
).optional()
|
|
4158
|
+
}).passthrough(),
|
|
4159
|
+
finish_reason: z10.string().nullable().optional()
|
|
4160
|
+
}).passthrough()
|
|
4161
|
+
),
|
|
4162
|
+
usage: z10.object({
|
|
4163
|
+
prompt_tokens: z10.number(),
|
|
4164
|
+
completion_tokens: z10.number(),
|
|
4165
|
+
total_tokens: z10.number()
|
|
4166
|
+
}).passthrough().optional()
|
|
4167
|
+
}).passthrough();
|
|
4168
|
+
|
|
4169
|
+
// src/image/index.ts
|
|
4170
|
+
var OpenRouterImageModel = class {
|
|
4171
|
+
constructor(modelId, settings, config) {
|
|
4172
|
+
this.specificationVersion = "v3";
|
|
4173
|
+
this.provider = "openrouter";
|
|
4174
|
+
this.maxImagesPerCall = 1;
|
|
4175
|
+
this.modelId = modelId;
|
|
4176
|
+
this.settings = settings;
|
|
4177
|
+
this.config = config;
|
|
4178
|
+
}
|
|
4179
|
+
async doGenerate(options) {
|
|
4180
|
+
var _a16;
|
|
4181
|
+
const {
|
|
4182
|
+
prompt,
|
|
4183
|
+
n,
|
|
4184
|
+
size,
|
|
4185
|
+
aspectRatio,
|
|
4186
|
+
seed,
|
|
4187
|
+
files,
|
|
4188
|
+
mask,
|
|
4189
|
+
abortSignal,
|
|
4190
|
+
headers,
|
|
4191
|
+
providerOptions
|
|
4192
|
+
} = options;
|
|
4193
|
+
const openrouterOptions = (providerOptions == null ? void 0 : providerOptions.openrouter) || {};
|
|
4194
|
+
const warnings = [];
|
|
4195
|
+
if (files !== void 0 && files.length > 0) {
|
|
4196
|
+
throw new UnsupportedFunctionalityError({
|
|
4197
|
+
functionality: "image editing (files parameter)"
|
|
4198
|
+
});
|
|
4199
|
+
}
|
|
4200
|
+
if (mask !== void 0) {
|
|
4201
|
+
throw new UnsupportedFunctionalityError({
|
|
4202
|
+
functionality: "image inpainting (mask parameter)"
|
|
4203
|
+
});
|
|
4204
|
+
}
|
|
4205
|
+
if (n > 1) {
|
|
4206
|
+
warnings.push({
|
|
4207
|
+
type: "unsupported",
|
|
4208
|
+
feature: "n > 1",
|
|
4209
|
+
details: `OpenRouter image generation returns 1 image per call. Requested ${n} images.`
|
|
4210
|
+
});
|
|
4211
|
+
}
|
|
4212
|
+
if (size !== void 0) {
|
|
4213
|
+
warnings.push({
|
|
4214
|
+
type: "unsupported",
|
|
4215
|
+
feature: "size",
|
|
4216
|
+
details: "Use aspectRatio instead. Size parameter is not supported by OpenRouter image generation."
|
|
4217
|
+
});
|
|
4218
|
+
}
|
|
4219
|
+
const imageConfig = aspectRatio !== void 0 ? { aspect_ratio: aspectRatio } : void 0;
|
|
4220
|
+
const body = __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
4221
|
+
model: this.modelId,
|
|
4222
|
+
messages: [
|
|
4223
|
+
{
|
|
4224
|
+
role: "user",
|
|
4225
|
+
content: prompt != null ? prompt : ""
|
|
4226
|
+
}
|
|
4227
|
+
],
|
|
4228
|
+
modalities: ["image", "text"]
|
|
4229
|
+
}, imageConfig !== void 0 && { image_config: imageConfig }), seed !== void 0 && { seed }), this.settings.user !== void 0 && { user: this.settings.user }), this.settings.provider !== void 0 && {
|
|
4230
|
+
provider: this.settings.provider
|
|
4231
|
+
}), this.config.extraBody), this.settings.extraBody), openrouterOptions);
|
|
4232
|
+
const { value: responseValue, responseHeaders } = await postJsonToApi({
|
|
4233
|
+
url: this.config.url({
|
|
4234
|
+
path: "/chat/completions",
|
|
4235
|
+
modelId: this.modelId
|
|
4236
|
+
}),
|
|
4237
|
+
headers: combineHeaders(this.config.headers(), headers),
|
|
4238
|
+
body,
|
|
4239
|
+
failedResponseHandler: openrouterFailedResponseHandler,
|
|
4240
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
4241
|
+
OpenRouterImageResponseSchema
|
|
4242
|
+
),
|
|
4243
|
+
abortSignal,
|
|
4244
|
+
fetch: this.config.fetch
|
|
4245
|
+
});
|
|
4246
|
+
const choice = responseValue.choices[0];
|
|
4247
|
+
if (!choice) {
|
|
4248
|
+
throw new NoContentGeneratedError({
|
|
4249
|
+
message: "No choice in response"
|
|
4250
|
+
});
|
|
4251
|
+
}
|
|
4252
|
+
const images = [];
|
|
4253
|
+
if ((_a16 = choice.message) == null ? void 0 : _a16.images) {
|
|
4254
|
+
for (const image of choice.message.images) {
|
|
4255
|
+
const dataUrl = image.image_url.url;
|
|
4256
|
+
images.push(getBase64FromDataUrl(dataUrl));
|
|
4257
|
+
}
|
|
4258
|
+
}
|
|
4259
|
+
const usage = responseValue.usage ? {
|
|
4260
|
+
inputTokens: responseValue.usage.prompt_tokens,
|
|
4261
|
+
outputTokens: responseValue.usage.completion_tokens,
|
|
4262
|
+
totalTokens: responseValue.usage.total_tokens
|
|
4263
|
+
} : void 0;
|
|
4264
|
+
return {
|
|
4265
|
+
images,
|
|
4266
|
+
warnings,
|
|
4267
|
+
response: {
|
|
4268
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
4269
|
+
modelId: responseValue.model,
|
|
4270
|
+
headers: responseHeaders
|
|
4271
|
+
},
|
|
4272
|
+
usage
|
|
4273
|
+
};
|
|
4274
|
+
}
|
|
4275
|
+
};
|
|
4002
4276
|
export {
|
|
4003
4277
|
OpenRouterChatLanguageModel,
|
|
4004
|
-
OpenRouterCompletionLanguageModel
|
|
4278
|
+
OpenRouterCompletionLanguageModel,
|
|
4279
|
+
OpenRouterEmbeddingModel,
|
|
4280
|
+
OpenRouterImageModel
|
|
4005
4281
|
};
|
|
4006
4282
|
//# sourceMappingURL=index.mjs.map
|