@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.js
CHANGED
|
@@ -60,7 +60,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
60
60
|
var index_exports = {};
|
|
61
61
|
__export(index_exports, {
|
|
62
62
|
OpenRouterChatLanguageModel: () => OpenRouterChatLanguageModel,
|
|
63
|
-
OpenRouterCompletionLanguageModel: () => OpenRouterCompletionLanguageModel
|
|
63
|
+
OpenRouterCompletionLanguageModel: () => OpenRouterCompletionLanguageModel,
|
|
64
|
+
OpenRouterEmbeddingModel: () => OpenRouterEmbeddingModel,
|
|
65
|
+
OpenRouterImageModel: () => OpenRouterImageModel
|
|
64
66
|
});
|
|
65
67
|
module.exports = __toCommonJS(index_exports);
|
|
66
68
|
|
|
@@ -2755,13 +2757,14 @@ var OpenRouterNonStreamChatCompletionResponseSchema = import_v46.z.union([
|
|
|
2755
2757
|
annotations: import_v46.z.array(
|
|
2756
2758
|
import_v46.z.union([
|
|
2757
2759
|
// URL citation from web search
|
|
2760
|
+
// title, start_index, end_index are optional as some upstream providers may omit them
|
|
2758
2761
|
import_v46.z.object({
|
|
2759
2762
|
type: import_v46.z.literal("url_citation"),
|
|
2760
2763
|
url_citation: import_v46.z.object({
|
|
2761
|
-
end_index: import_v46.z.number(),
|
|
2762
|
-
start_index: import_v46.z.number(),
|
|
2763
|
-
title: import_v46.z.string(),
|
|
2764
2764
|
url: import_v46.z.string(),
|
|
2765
|
+
title: import_v46.z.string().optional(),
|
|
2766
|
+
start_index: import_v46.z.number().optional(),
|
|
2767
|
+
end_index: import_v46.z.number().optional(),
|
|
2765
2768
|
content: import_v46.z.string().optional()
|
|
2766
2769
|
}).passthrough()
|
|
2767
2770
|
}).passthrough(),
|
|
@@ -2838,13 +2841,14 @@ var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
|
|
|
2838
2841
|
annotations: import_v46.z.array(
|
|
2839
2842
|
import_v46.z.union([
|
|
2840
2843
|
// URL citation from web search
|
|
2844
|
+
// title, start_index, end_index are optional as some upstream providers may omit them
|
|
2841
2845
|
import_v46.z.object({
|
|
2842
2846
|
type: import_v46.z.literal("url_citation"),
|
|
2843
2847
|
url_citation: import_v46.z.object({
|
|
2844
|
-
end_index: import_v46.z.number(),
|
|
2845
|
-
start_index: import_v46.z.number(),
|
|
2846
|
-
title: import_v46.z.string(),
|
|
2847
2848
|
url: import_v46.z.string(),
|
|
2849
|
+
title: import_v46.z.string().optional(),
|
|
2850
|
+
start_index: import_v46.z.number().optional(),
|
|
2851
|
+
end_index: import_v46.z.number().optional(),
|
|
2848
2852
|
content: import_v46.z.string().optional()
|
|
2849
2853
|
}).passthrough()
|
|
2850
2854
|
}).passthrough(),
|
|
@@ -2991,7 +2995,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
2991
2995
|
return baseArgs;
|
|
2992
2996
|
}
|
|
2993
2997
|
async doGenerate(options) {
|
|
2994
|
-
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;
|
|
2998
|
+
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;
|
|
2995
2999
|
const providerOptions = options.providerOptions || {};
|
|
2996
3000
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
2997
3001
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -3041,7 +3045,8 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3041
3045
|
total: (_d = response.usage.completion_tokens) != null ? _d : 0,
|
|
3042
3046
|
text: void 0,
|
|
3043
3047
|
reasoning: (_f = (_e = response.usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : void 0
|
|
3044
|
-
}
|
|
3048
|
+
},
|
|
3049
|
+
raw: response.usage
|
|
3045
3050
|
} : {
|
|
3046
3051
|
inputTokens: {
|
|
3047
3052
|
total: 0,
|
|
@@ -3053,7 +3058,8 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3053
3058
|
total: 0,
|
|
3054
3059
|
text: void 0,
|
|
3055
3060
|
reasoning: void 0
|
|
3056
|
-
}
|
|
3061
|
+
},
|
|
3062
|
+
raw: void 0
|
|
3057
3063
|
};
|
|
3058
3064
|
const reasoningDetails = (_g = choice.message.reasoning_details) != null ? _g : [];
|
|
3059
3065
|
const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
|
|
@@ -3153,17 +3159,19 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3153
3159
|
sourceType: "url",
|
|
3154
3160
|
id: annotation.url_citation.url,
|
|
3155
3161
|
url: annotation.url_citation.url,
|
|
3156
|
-
title: annotation.url_citation.title,
|
|
3162
|
+
title: (_j = annotation.url_citation.title) != null ? _j : "",
|
|
3157
3163
|
providerMetadata: {
|
|
3158
3164
|
openrouter: {
|
|
3159
|
-
content: annotation.url_citation.content
|
|
3165
|
+
content: (_k = annotation.url_citation.content) != null ? _k : "",
|
|
3166
|
+
startIndex: (_l = annotation.url_citation.start_index) != null ? _l : 0,
|
|
3167
|
+
endIndex: (_m = annotation.url_citation.end_index) != null ? _m : 0
|
|
3160
3168
|
}
|
|
3161
3169
|
}
|
|
3162
3170
|
});
|
|
3163
3171
|
}
|
|
3164
3172
|
}
|
|
3165
3173
|
}
|
|
3166
|
-
const fileAnnotations = (
|
|
3174
|
+
const fileAnnotations = (_n = choice.message.annotations) == null ? void 0 : _n.filter(
|
|
3167
3175
|
(a) => a.type === "file"
|
|
3168
3176
|
);
|
|
3169
3177
|
const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
|
|
@@ -3171,7 +3179,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3171
3179
|
(d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
|
|
3172
3180
|
);
|
|
3173
3181
|
const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
|
|
3174
|
-
const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (
|
|
3182
|
+
const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_o = choice.finish_reason) != null ? _o : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
|
|
3175
3183
|
return {
|
|
3176
3184
|
content,
|
|
3177
3185
|
finishReason: effectiveFinishReason,
|
|
@@ -3179,23 +3187,22 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3179
3187
|
warnings: [],
|
|
3180
3188
|
providerMetadata: {
|
|
3181
3189
|
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
3182
|
-
provider: (
|
|
3183
|
-
reasoning_details: (
|
|
3190
|
+
provider: (_p = response.provider) != null ? _p : "",
|
|
3191
|
+
reasoning_details: (_q = choice.message.reasoning_details) != null ? _q : [],
|
|
3184
3192
|
annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
|
|
3185
|
-
usage: __spreadValues(__spreadValues(__spreadValues({
|
|
3186
|
-
promptTokens: (
|
|
3187
|
-
completionTokens: (
|
|
3188
|
-
totalTokens: ((
|
|
3189
|
-
|
|
3190
|
-
}, ((_t = (_s = response.usage) == null ? void 0 : _s.prompt_tokens_details) == null ? void 0 : _t.cached_tokens) != null ? {
|
|
3193
|
+
usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
3194
|
+
promptTokens: (_r = usageInfo.inputTokens.total) != null ? _r : 0,
|
|
3195
|
+
completionTokens: (_s = usageInfo.outputTokens.total) != null ? _s : 0,
|
|
3196
|
+
totalTokens: ((_t = usageInfo.inputTokens.total) != null ? _t : 0) + ((_u = usageInfo.outputTokens.total) != null ? _u : 0)
|
|
3197
|
+
}, ((_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 ? {
|
|
3191
3198
|
promptTokensDetails: {
|
|
3192
3199
|
cachedTokens: response.usage.prompt_tokens_details.cached_tokens
|
|
3193
3200
|
}
|
|
3194
|
-
} : {}), ((
|
|
3201
|
+
} : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
|
|
3195
3202
|
completionTokensDetails: {
|
|
3196
3203
|
reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
|
|
3197
3204
|
}
|
|
3198
|
-
} : {}), ((
|
|
3205
|
+
} : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
|
|
3199
3206
|
costDetails: {
|
|
3200
3207
|
upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
|
|
3201
3208
|
}
|
|
@@ -3248,9 +3255,11 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3248
3255
|
total: void 0,
|
|
3249
3256
|
text: void 0,
|
|
3250
3257
|
reasoning: void 0
|
|
3251
|
-
}
|
|
3258
|
+
},
|
|
3259
|
+
raw: void 0
|
|
3252
3260
|
};
|
|
3253
3261
|
const openrouterUsage = {};
|
|
3262
|
+
let rawUsage;
|
|
3254
3263
|
const accumulatedReasoningDetails = [];
|
|
3255
3264
|
let reasoningDetailsAttachedToToolCall = false;
|
|
3256
3265
|
const accumulatedFileAnnotations = [];
|
|
@@ -3264,7 +3273,10 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3264
3273
|
stream: response.pipeThrough(
|
|
3265
3274
|
new TransformStream({
|
|
3266
3275
|
transform(chunk, controller) {
|
|
3267
|
-
var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
3276
|
+
var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
|
|
3277
|
+
if (options.includeRawChunks) {
|
|
3278
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
3279
|
+
}
|
|
3268
3280
|
if (!chunk.success) {
|
|
3269
3281
|
finishReason = createFinishReason("error");
|
|
3270
3282
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -3295,6 +3307,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3295
3307
|
if (value.usage != null) {
|
|
3296
3308
|
usage.inputTokens.total = value.usage.prompt_tokens;
|
|
3297
3309
|
usage.outputTokens.total = value.usage.completion_tokens;
|
|
3310
|
+
rawUsage = value.usage;
|
|
3298
3311
|
openrouterUsage.promptTokens = value.usage.prompt_tokens;
|
|
3299
3312
|
if (value.usage.prompt_tokens_details) {
|
|
3300
3313
|
const cachedInputTokens = (_a17 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a17 : 0;
|
|
@@ -3311,7 +3324,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3311
3324
|
reasoningTokens
|
|
3312
3325
|
};
|
|
3313
3326
|
}
|
|
3314
|
-
|
|
3327
|
+
if (value.usage.cost != null) {
|
|
3328
|
+
openrouterUsage.cost = value.usage.cost;
|
|
3329
|
+
}
|
|
3315
3330
|
openrouterUsage.totalTokens = value.usage.total_tokens;
|
|
3316
3331
|
const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
|
|
3317
3332
|
if (upstreamInferenceCost != null) {
|
|
@@ -3424,10 +3439,12 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3424
3439
|
sourceType: "url",
|
|
3425
3440
|
id: annotation.url_citation.url,
|
|
3426
3441
|
url: annotation.url_citation.url,
|
|
3427
|
-
title: annotation.url_citation.title,
|
|
3442
|
+
title: (_d = annotation.url_citation.title) != null ? _d : "",
|
|
3428
3443
|
providerMetadata: {
|
|
3429
3444
|
openrouter: {
|
|
3430
|
-
content: annotation.url_citation.content
|
|
3445
|
+
content: (_e = annotation.url_citation.content) != null ? _e : "",
|
|
3446
|
+
startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
|
|
3447
|
+
endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
|
|
3431
3448
|
}
|
|
3432
3449
|
}
|
|
3433
3450
|
});
|
|
@@ -3443,7 +3460,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3443
3460
|
}
|
|
3444
3461
|
if (delta.tool_calls != null) {
|
|
3445
3462
|
for (const toolCallDelta of delta.tool_calls) {
|
|
3446
|
-
const index = (
|
|
3463
|
+
const index = (_h = toolCallDelta.index) != null ? _h : toolCalls.length - 1;
|
|
3447
3464
|
if (toolCalls[index] == null) {
|
|
3448
3465
|
if (toolCallDelta.type !== "function") {
|
|
3449
3466
|
throw new InvalidResponseDataError({
|
|
@@ -3457,7 +3474,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3457
3474
|
message: `Expected 'id' to be a string.`
|
|
3458
3475
|
});
|
|
3459
3476
|
}
|
|
3460
|
-
if (((
|
|
3477
|
+
if (((_i = toolCallDelta.function) == null ? void 0 : _i.name) == null) {
|
|
3461
3478
|
throw new InvalidResponseDataError({
|
|
3462
3479
|
data: toolCallDelta,
|
|
3463
3480
|
message: `Expected 'function.name' to be a string.`
|
|
@@ -3468,7 +3485,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3468
3485
|
type: "function",
|
|
3469
3486
|
function: {
|
|
3470
3487
|
name: toolCallDelta.function.name,
|
|
3471
|
-
arguments: (
|
|
3488
|
+
arguments: (_j = toolCallDelta.function.arguments) != null ? _j : ""
|
|
3472
3489
|
},
|
|
3473
3490
|
inputStarted: false,
|
|
3474
3491
|
sent: false
|
|
@@ -3480,7 +3497,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3480
3497
|
message: `Tool call at index ${index} is missing after creation.`
|
|
3481
3498
|
});
|
|
3482
3499
|
}
|
|
3483
|
-
if (((
|
|
3500
|
+
if (((_k = toolCall2.function) == null ? void 0 : _k.name) != null && ((_l = toolCall2.function) == null ? void 0 : _l.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
|
|
3484
3501
|
toolCall2.inputStarted = true;
|
|
3485
3502
|
controller.enqueue({
|
|
3486
3503
|
type: "tool-input-start",
|
|
@@ -3531,18 +3548,18 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3531
3548
|
toolName: toolCall.function.name
|
|
3532
3549
|
});
|
|
3533
3550
|
}
|
|
3534
|
-
if (((
|
|
3535
|
-
toolCall.function.arguments += (
|
|
3551
|
+
if (((_m = toolCallDelta.function) == null ? void 0 : _m.arguments) != null) {
|
|
3552
|
+
toolCall.function.arguments += (_o = (_n = toolCallDelta.function) == null ? void 0 : _n.arguments) != null ? _o : "";
|
|
3536
3553
|
}
|
|
3537
3554
|
controller.enqueue({
|
|
3538
3555
|
type: "tool-input-delta",
|
|
3539
3556
|
id: toolCall.id,
|
|
3540
|
-
delta: (
|
|
3557
|
+
delta: (_p = toolCallDelta.function.arguments) != null ? _p : ""
|
|
3541
3558
|
});
|
|
3542
|
-
if (((
|
|
3559
|
+
if (((_q = toolCall.function) == null ? void 0 : _q.name) != null && ((_r = toolCall.function) == null ? void 0 : _r.arguments) != null && isParsableJson(toolCall.function.arguments)) {
|
|
3543
3560
|
controller.enqueue({
|
|
3544
3561
|
type: "tool-call",
|
|
3545
|
-
toolCallId: (
|
|
3562
|
+
toolCallId: (_s = toolCall.id) != null ? _s : generateId(),
|
|
3546
3563
|
toolName: toolCall.function.name,
|
|
3547
3564
|
input: toolCall.function.arguments,
|
|
3548
3565
|
providerMetadata: !reasoningDetailsAttachedToToolCall ? {
|
|
@@ -3619,6 +3636,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3619
3636
|
if (accumulatedFileAnnotations.length > 0) {
|
|
3620
3637
|
openrouterMetadata.annotations = accumulatedFileAnnotations;
|
|
3621
3638
|
}
|
|
3639
|
+
usage.raw = rawUsage;
|
|
3622
3640
|
controller.enqueue({
|
|
3623
3641
|
type: "finish",
|
|
3624
3642
|
finishReason,
|
|
@@ -3746,6 +3764,7 @@ var OpenRouterCompletionChunkSchema = import_v47.z.union([
|
|
|
3746
3764
|
import_v47.z.object({
|
|
3747
3765
|
id: import_v47.z.string().optional(),
|
|
3748
3766
|
model: import_v47.z.string().optional(),
|
|
3767
|
+
provider: import_v47.z.string().optional(),
|
|
3749
3768
|
choices: import_v47.z.array(
|
|
3750
3769
|
import_v47.z.object({
|
|
3751
3770
|
text: import_v47.z.string(),
|
|
@@ -3853,7 +3872,7 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
3853
3872
|
}, this.config.extraBody), this.settings.extraBody);
|
|
3854
3873
|
}
|
|
3855
3874
|
async doGenerate(options) {
|
|
3856
|
-
var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
3875
|
+
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;
|
|
3857
3876
|
const providerOptions = options.providerOptions || {};
|
|
3858
3877
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
3859
3878
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -3910,9 +3929,32 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
3910
3929
|
total: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : 0,
|
|
3911
3930
|
text: void 0,
|
|
3912
3931
|
reasoning: (_k = (_j = (_i = response.usage) == null ? void 0 : _i.completion_tokens_details) == null ? void 0 : _j.reasoning_tokens) != null ? _k : void 0
|
|
3913
|
-
}
|
|
3932
|
+
},
|
|
3933
|
+
raw: (_l = response.usage) != null ? _l : void 0
|
|
3914
3934
|
},
|
|
3915
3935
|
warnings: [],
|
|
3936
|
+
providerMetadata: {
|
|
3937
|
+
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
3938
|
+
provider: (_m = response.provider) != null ? _m : "",
|
|
3939
|
+
usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
3940
|
+
promptTokens: (_o = (_n = response.usage) == null ? void 0 : _n.prompt_tokens) != null ? _o : 0,
|
|
3941
|
+
completionTokens: (_q = (_p = response.usage) == null ? void 0 : _p.completion_tokens) != null ? _q : 0,
|
|
3942
|
+
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)
|
|
3943
|
+
}, ((_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 ? {
|
|
3944
|
+
promptTokensDetails: {
|
|
3945
|
+
cachedTokens: response.usage.prompt_tokens_details.cached_tokens
|
|
3946
|
+
}
|
|
3947
|
+
} : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
|
|
3948
|
+
completionTokensDetails: {
|
|
3949
|
+
reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
|
|
3950
|
+
}
|
|
3951
|
+
} : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
|
|
3952
|
+
costDetails: {
|
|
3953
|
+
upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
|
|
3954
|
+
}
|
|
3955
|
+
} : {})
|
|
3956
|
+
})
|
|
3957
|
+
},
|
|
3916
3958
|
response: {
|
|
3917
3959
|
headers: responseHeaders
|
|
3918
3960
|
}
|
|
@@ -3952,14 +3994,20 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
3952
3994
|
total: void 0,
|
|
3953
3995
|
text: void 0,
|
|
3954
3996
|
reasoning: void 0
|
|
3955
|
-
}
|
|
3997
|
+
},
|
|
3998
|
+
raw: void 0
|
|
3956
3999
|
};
|
|
3957
4000
|
const openrouterUsage = {};
|
|
4001
|
+
let provider;
|
|
4002
|
+
let rawUsage;
|
|
3958
4003
|
return {
|
|
3959
4004
|
stream: response.pipeThrough(
|
|
3960
4005
|
new TransformStream({
|
|
3961
4006
|
transform(chunk, controller) {
|
|
3962
4007
|
var _a16, _b16, _c;
|
|
4008
|
+
if (options.includeRawChunks) {
|
|
4009
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
4010
|
+
}
|
|
3963
4011
|
if (!chunk.success) {
|
|
3964
4012
|
finishReason = createFinishReason("error");
|
|
3965
4013
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -3971,9 +4019,13 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
3971
4019
|
controller.enqueue({ type: "error", error: value.error });
|
|
3972
4020
|
return;
|
|
3973
4021
|
}
|
|
4022
|
+
if (value.provider) {
|
|
4023
|
+
provider = value.provider;
|
|
4024
|
+
}
|
|
3974
4025
|
if (value.usage != null) {
|
|
3975
4026
|
usage.inputTokens.total = value.usage.prompt_tokens;
|
|
3976
4027
|
usage.outputTokens.total = value.usage.completion_tokens;
|
|
4028
|
+
rawUsage = value.usage;
|
|
3977
4029
|
openrouterUsage.promptTokens = value.usage.prompt_tokens;
|
|
3978
4030
|
if (value.usage.prompt_tokens_details) {
|
|
3979
4031
|
const cachedInputTokens = (_a16 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a16 : 0;
|
|
@@ -3990,7 +4042,9 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
3990
4042
|
reasoningTokens
|
|
3991
4043
|
};
|
|
3992
4044
|
}
|
|
3993
|
-
|
|
4045
|
+
if (value.usage.cost != null) {
|
|
4046
|
+
openrouterUsage.cost = value.usage.cost;
|
|
4047
|
+
}
|
|
3994
4048
|
openrouterUsage.totalTokens = value.usage.total_tokens;
|
|
3995
4049
|
const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
|
|
3996
4050
|
if (upstreamInferenceCost != null) {
|
|
@@ -4012,14 +4066,19 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
4012
4066
|
}
|
|
4013
4067
|
},
|
|
4014
4068
|
flush(controller) {
|
|
4069
|
+
usage.raw = rawUsage;
|
|
4070
|
+
const openrouterMetadata = {
|
|
4071
|
+
usage: openrouterUsage
|
|
4072
|
+
};
|
|
4073
|
+
if (provider !== void 0) {
|
|
4074
|
+
openrouterMetadata.provider = provider;
|
|
4075
|
+
}
|
|
4015
4076
|
controller.enqueue({
|
|
4016
4077
|
type: "finish",
|
|
4017
4078
|
finishReason,
|
|
4018
4079
|
usage,
|
|
4019
4080
|
providerMetadata: {
|
|
4020
|
-
openrouter:
|
|
4021
|
-
usage: openrouterUsage
|
|
4022
|
-
}
|
|
4081
|
+
openrouter: openrouterMetadata
|
|
4023
4082
|
}
|
|
4024
4083
|
});
|
|
4025
4084
|
}
|
|
@@ -4031,9 +4090,228 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
4031
4090
|
};
|
|
4032
4091
|
}
|
|
4033
4092
|
};
|
|
4093
|
+
|
|
4094
|
+
// src/embedding/schemas.ts
|
|
4095
|
+
var import_v48 = require("zod/v4");
|
|
4096
|
+
var openrouterEmbeddingUsageSchema = import_v48.z.object({
|
|
4097
|
+
prompt_tokens: import_v48.z.number(),
|
|
4098
|
+
total_tokens: import_v48.z.number(),
|
|
4099
|
+
cost: import_v48.z.number().optional()
|
|
4100
|
+
});
|
|
4101
|
+
var openrouterEmbeddingDataSchema = import_v48.z.object({
|
|
4102
|
+
object: import_v48.z.literal("embedding"),
|
|
4103
|
+
embedding: import_v48.z.array(import_v48.z.number()),
|
|
4104
|
+
index: import_v48.z.number().optional()
|
|
4105
|
+
});
|
|
4106
|
+
var OpenRouterEmbeddingResponseSchema = import_v48.z.object({
|
|
4107
|
+
id: import_v48.z.string().optional(),
|
|
4108
|
+
object: import_v48.z.literal("list"),
|
|
4109
|
+
data: import_v48.z.array(openrouterEmbeddingDataSchema),
|
|
4110
|
+
model: import_v48.z.string(),
|
|
4111
|
+
provider: import_v48.z.string().optional(),
|
|
4112
|
+
usage: openrouterEmbeddingUsageSchema.optional()
|
|
4113
|
+
});
|
|
4114
|
+
|
|
4115
|
+
// src/embedding/index.ts
|
|
4116
|
+
var OpenRouterEmbeddingModel = class {
|
|
4117
|
+
constructor(modelId, settings, config) {
|
|
4118
|
+
this.specificationVersion = "v3";
|
|
4119
|
+
this.provider = "openrouter";
|
|
4120
|
+
this.maxEmbeddingsPerCall = void 0;
|
|
4121
|
+
this.supportsParallelCalls = true;
|
|
4122
|
+
this.modelId = modelId;
|
|
4123
|
+
this.settings = settings;
|
|
4124
|
+
this.config = config;
|
|
4125
|
+
}
|
|
4126
|
+
async doEmbed(options) {
|
|
4127
|
+
var _a16, _b16, _c, _d, _e, _f;
|
|
4128
|
+
const { values, abortSignal, headers } = options;
|
|
4129
|
+
const args = __spreadValues(__spreadValues({
|
|
4130
|
+
model: this.modelId,
|
|
4131
|
+
input: values,
|
|
4132
|
+
user: this.settings.user,
|
|
4133
|
+
provider: this.settings.provider
|
|
4134
|
+
}, this.config.extraBody), this.settings.extraBody);
|
|
4135
|
+
const { value: responseValue, responseHeaders } = await postJsonToApi({
|
|
4136
|
+
url: this.config.url({
|
|
4137
|
+
path: "/embeddings",
|
|
4138
|
+
modelId: this.modelId
|
|
4139
|
+
}),
|
|
4140
|
+
headers: combineHeaders(this.config.headers(), headers),
|
|
4141
|
+
body: args,
|
|
4142
|
+
failedResponseHandler: openrouterFailedResponseHandler,
|
|
4143
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
4144
|
+
OpenRouterEmbeddingResponseSchema
|
|
4145
|
+
),
|
|
4146
|
+
abortSignal,
|
|
4147
|
+
fetch: this.config.fetch
|
|
4148
|
+
});
|
|
4149
|
+
return {
|
|
4150
|
+
embeddings: responseValue.data.map((item) => item.embedding),
|
|
4151
|
+
usage: responseValue.usage ? { tokens: responseValue.usage.prompt_tokens } : void 0,
|
|
4152
|
+
providerMetadata: {
|
|
4153
|
+
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
4154
|
+
provider: (_a16 = responseValue.provider) != null ? _a16 : "",
|
|
4155
|
+
usage: __spreadValues({
|
|
4156
|
+
promptTokens: (_c = (_b16 = responseValue.usage) == null ? void 0 : _b16.prompt_tokens) != null ? _c : 0,
|
|
4157
|
+
completionTokens: 0,
|
|
4158
|
+
totalTokens: (_e = (_d = responseValue.usage) == null ? void 0 : _d.total_tokens) != null ? _e : 0
|
|
4159
|
+
}, ((_f = responseValue.usage) == null ? void 0 : _f.cost) != null ? { cost: responseValue.usage.cost } : {})
|
|
4160
|
+
})
|
|
4161
|
+
},
|
|
4162
|
+
response: {
|
|
4163
|
+
headers: responseHeaders,
|
|
4164
|
+
body: responseValue
|
|
4165
|
+
},
|
|
4166
|
+
warnings: []
|
|
4167
|
+
};
|
|
4168
|
+
}
|
|
4169
|
+
};
|
|
4170
|
+
|
|
4171
|
+
// src/image/schemas.ts
|
|
4172
|
+
var import_v49 = require("zod/v4");
|
|
4173
|
+
var OpenRouterImageResponseSchema = import_v49.z.object({
|
|
4174
|
+
id: import_v49.z.string().optional(),
|
|
4175
|
+
object: import_v49.z.string().optional(),
|
|
4176
|
+
created: import_v49.z.number().optional(),
|
|
4177
|
+
model: import_v49.z.string(),
|
|
4178
|
+
choices: import_v49.z.array(
|
|
4179
|
+
import_v49.z.object({
|
|
4180
|
+
index: import_v49.z.number(),
|
|
4181
|
+
message: import_v49.z.object({
|
|
4182
|
+
role: import_v49.z.string(),
|
|
4183
|
+
content: import_v49.z.string().nullable().optional(),
|
|
4184
|
+
images: import_v49.z.array(
|
|
4185
|
+
import_v49.z.object({
|
|
4186
|
+
type: import_v49.z.literal("image_url"),
|
|
4187
|
+
image_url: import_v49.z.object({
|
|
4188
|
+
url: import_v49.z.string()
|
|
4189
|
+
})
|
|
4190
|
+
}).passthrough()
|
|
4191
|
+
).optional()
|
|
4192
|
+
}).passthrough(),
|
|
4193
|
+
finish_reason: import_v49.z.string().nullable().optional()
|
|
4194
|
+
}).passthrough()
|
|
4195
|
+
),
|
|
4196
|
+
usage: import_v49.z.object({
|
|
4197
|
+
prompt_tokens: import_v49.z.number(),
|
|
4198
|
+
completion_tokens: import_v49.z.number(),
|
|
4199
|
+
total_tokens: import_v49.z.number()
|
|
4200
|
+
}).passthrough().optional()
|
|
4201
|
+
}).passthrough();
|
|
4202
|
+
|
|
4203
|
+
// src/image/index.ts
|
|
4204
|
+
var OpenRouterImageModel = class {
|
|
4205
|
+
constructor(modelId, settings, config) {
|
|
4206
|
+
this.specificationVersion = "v3";
|
|
4207
|
+
this.provider = "openrouter";
|
|
4208
|
+
this.maxImagesPerCall = 1;
|
|
4209
|
+
this.modelId = modelId;
|
|
4210
|
+
this.settings = settings;
|
|
4211
|
+
this.config = config;
|
|
4212
|
+
}
|
|
4213
|
+
async doGenerate(options) {
|
|
4214
|
+
var _a16;
|
|
4215
|
+
const {
|
|
4216
|
+
prompt,
|
|
4217
|
+
n,
|
|
4218
|
+
size,
|
|
4219
|
+
aspectRatio,
|
|
4220
|
+
seed,
|
|
4221
|
+
files,
|
|
4222
|
+
mask,
|
|
4223
|
+
abortSignal,
|
|
4224
|
+
headers,
|
|
4225
|
+
providerOptions
|
|
4226
|
+
} = options;
|
|
4227
|
+
const openrouterOptions = (providerOptions == null ? void 0 : providerOptions.openrouter) || {};
|
|
4228
|
+
const warnings = [];
|
|
4229
|
+
if (files !== void 0 && files.length > 0) {
|
|
4230
|
+
throw new UnsupportedFunctionalityError({
|
|
4231
|
+
functionality: "image editing (files parameter)"
|
|
4232
|
+
});
|
|
4233
|
+
}
|
|
4234
|
+
if (mask !== void 0) {
|
|
4235
|
+
throw new UnsupportedFunctionalityError({
|
|
4236
|
+
functionality: "image inpainting (mask parameter)"
|
|
4237
|
+
});
|
|
4238
|
+
}
|
|
4239
|
+
if (n > 1) {
|
|
4240
|
+
warnings.push({
|
|
4241
|
+
type: "unsupported",
|
|
4242
|
+
feature: "n > 1",
|
|
4243
|
+
details: `OpenRouter image generation returns 1 image per call. Requested ${n} images.`
|
|
4244
|
+
});
|
|
4245
|
+
}
|
|
4246
|
+
if (size !== void 0) {
|
|
4247
|
+
warnings.push({
|
|
4248
|
+
type: "unsupported",
|
|
4249
|
+
feature: "size",
|
|
4250
|
+
details: "Use aspectRatio instead. Size parameter is not supported by OpenRouter image generation."
|
|
4251
|
+
});
|
|
4252
|
+
}
|
|
4253
|
+
const imageConfig = aspectRatio !== void 0 ? { aspect_ratio: aspectRatio } : void 0;
|
|
4254
|
+
const body = __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
4255
|
+
model: this.modelId,
|
|
4256
|
+
messages: [
|
|
4257
|
+
{
|
|
4258
|
+
role: "user",
|
|
4259
|
+
content: prompt != null ? prompt : ""
|
|
4260
|
+
}
|
|
4261
|
+
],
|
|
4262
|
+
modalities: ["image", "text"]
|
|
4263
|
+
}, imageConfig !== void 0 && { image_config: imageConfig }), seed !== void 0 && { seed }), this.settings.user !== void 0 && { user: this.settings.user }), this.settings.provider !== void 0 && {
|
|
4264
|
+
provider: this.settings.provider
|
|
4265
|
+
}), this.config.extraBody), this.settings.extraBody), openrouterOptions);
|
|
4266
|
+
const { value: responseValue, responseHeaders } = await postJsonToApi({
|
|
4267
|
+
url: this.config.url({
|
|
4268
|
+
path: "/chat/completions",
|
|
4269
|
+
modelId: this.modelId
|
|
4270
|
+
}),
|
|
4271
|
+
headers: combineHeaders(this.config.headers(), headers),
|
|
4272
|
+
body,
|
|
4273
|
+
failedResponseHandler: openrouterFailedResponseHandler,
|
|
4274
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
4275
|
+
OpenRouterImageResponseSchema
|
|
4276
|
+
),
|
|
4277
|
+
abortSignal,
|
|
4278
|
+
fetch: this.config.fetch
|
|
4279
|
+
});
|
|
4280
|
+
const choice = responseValue.choices[0];
|
|
4281
|
+
if (!choice) {
|
|
4282
|
+
throw new NoContentGeneratedError({
|
|
4283
|
+
message: "No choice in response"
|
|
4284
|
+
});
|
|
4285
|
+
}
|
|
4286
|
+
const images = [];
|
|
4287
|
+
if ((_a16 = choice.message) == null ? void 0 : _a16.images) {
|
|
4288
|
+
for (const image of choice.message.images) {
|
|
4289
|
+
const dataUrl = image.image_url.url;
|
|
4290
|
+
images.push(getBase64FromDataUrl(dataUrl));
|
|
4291
|
+
}
|
|
4292
|
+
}
|
|
4293
|
+
const usage = responseValue.usage ? {
|
|
4294
|
+
inputTokens: responseValue.usage.prompt_tokens,
|
|
4295
|
+
outputTokens: responseValue.usage.completion_tokens,
|
|
4296
|
+
totalTokens: responseValue.usage.total_tokens
|
|
4297
|
+
} : void 0;
|
|
4298
|
+
return {
|
|
4299
|
+
images,
|
|
4300
|
+
warnings,
|
|
4301
|
+
response: {
|
|
4302
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
4303
|
+
modelId: responseValue.model,
|
|
4304
|
+
headers: responseHeaders
|
|
4305
|
+
},
|
|
4306
|
+
usage
|
|
4307
|
+
};
|
|
4308
|
+
}
|
|
4309
|
+
};
|
|
4034
4310
|
// Annotate the CommonJS export names for ESM import in node:
|
|
4035
4311
|
0 && (module.exports = {
|
|
4036
4312
|
OpenRouterChatLanguageModel,
|
|
4037
|
-
OpenRouterCompletionLanguageModel
|
|
4313
|
+
OpenRouterCompletionLanguageModel,
|
|
4314
|
+
OpenRouterEmbeddingModel,
|
|
4315
|
+
OpenRouterImageModel
|
|
4038
4316
|
});
|
|
4039
4317
|
//# sourceMappingURL=index.js.map
|