@openrouter/ai-sdk-provider 0.4.5 → 0.5.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 +49 -12
- package/dist/index.d.mts +28 -2
- package/dist/index.d.ts +28 -2
- package/dist/index.js +100 -42
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +100 -42
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +28 -2
- package/dist/internal/index.d.ts +28 -2
- package/dist/internal/index.js +99 -39
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +99 -39
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +6 -2
package/dist/internal/index.js
CHANGED
|
@@ -92,7 +92,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
92
92
|
const messageCacheControl = getCacheControl(providerMetadata);
|
|
93
93
|
const contentParts = content.map(
|
|
94
94
|
(part) => {
|
|
95
|
-
var _a2, _b2, _c2, _d;
|
|
95
|
+
var _a2, _b2, _c2, _d, _e, _f;
|
|
96
96
|
switch (part.type) {
|
|
97
97
|
case "text":
|
|
98
98
|
return {
|
|
@@ -114,9 +114,14 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
114
114
|
};
|
|
115
115
|
case "file":
|
|
116
116
|
return {
|
|
117
|
-
type: "
|
|
118
|
-
|
|
119
|
-
|
|
117
|
+
type: "file",
|
|
118
|
+
file: {
|
|
119
|
+
filename: String(
|
|
120
|
+
(_e = (_d = part.providerMetadata) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.filename
|
|
121
|
+
),
|
|
122
|
+
file_data: part.data instanceof Uint8Array ? `data:${part.mimeType};base64,${(0, import_provider_utils.convertUint8ArrayToBase64)(part.data)}` : `data:${part.mimeType};base64,${part.data}`
|
|
123
|
+
},
|
|
124
|
+
cache_control: (_f = getCacheControl(part.providerMetadata)) != null ? _f : messageCacheControl
|
|
120
125
|
};
|
|
121
126
|
default: {
|
|
122
127
|
const _exhaustiveCheck = part;
|
|
@@ -268,7 +273,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
268
273
|
}) {
|
|
269
274
|
var _a;
|
|
270
275
|
const type = mode.type;
|
|
271
|
-
const extraCallingBody = (_a = providerMetadata == null ? void 0 : providerMetadata
|
|
276
|
+
const extraCallingBody = (_a = providerMetadata == null ? void 0 : providerMetadata.openrouter) != null ? _a : {};
|
|
272
277
|
const baseArgs = __spreadValues(__spreadValues(__spreadValues({
|
|
273
278
|
// model id:
|
|
274
279
|
model: this.modelId,
|
|
@@ -293,7 +298,8 @@ var OpenRouterChatLanguageModel = class {
|
|
|
293
298
|
messages: convertToOpenRouterChatMessages(prompt),
|
|
294
299
|
// OpenRouter specific settings:
|
|
295
300
|
include_reasoning: this.settings.includeReasoning,
|
|
296
|
-
reasoning: this.settings.reasoning
|
|
301
|
+
reasoning: this.settings.reasoning,
|
|
302
|
+
usage: this.settings.usage
|
|
297
303
|
}, this.config.extraBody), this.settings.extraBody), extraCallingBody);
|
|
298
304
|
switch (type) {
|
|
299
305
|
case "regular": {
|
|
@@ -329,7 +335,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
329
335
|
}
|
|
330
336
|
}
|
|
331
337
|
async doGenerate(options) {
|
|
332
|
-
var _b, _c, _d, _e, _f, _g, _h;
|
|
338
|
+
var _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
333
339
|
const args = this.getArgs(options);
|
|
334
340
|
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
|
335
341
|
url: this.config.url({
|
|
@@ -350,14 +356,39 @@ var OpenRouterChatLanguageModel = class {
|
|
|
350
356
|
if (!choice) {
|
|
351
357
|
throw new Error("No choice in response");
|
|
352
358
|
}
|
|
353
|
-
|
|
359
|
+
const usageInfo = response.usage ? {
|
|
360
|
+
promptTokens: (_b = response.usage.prompt_tokens) != null ? _b : 0,
|
|
361
|
+
completionTokens: (_c = response.usage.completion_tokens) != null ? _c : 0
|
|
362
|
+
} : {
|
|
363
|
+
promptTokens: 0,
|
|
364
|
+
completionTokens: 0
|
|
365
|
+
};
|
|
366
|
+
const providerMetadata = {};
|
|
367
|
+
if (response.usage && ((_d = this.settings.usage) == null ? void 0 : _d.include)) {
|
|
368
|
+
providerMetadata.openrouter = {
|
|
369
|
+
usage: {
|
|
370
|
+
promptTokens: response.usage.prompt_tokens,
|
|
371
|
+
promptTokensDetails: response.usage.prompt_tokens_details ? {
|
|
372
|
+
cachedTokens: (_e = response.usage.prompt_tokens_details.cached_tokens) != null ? _e : 0
|
|
373
|
+
} : void 0,
|
|
374
|
+
completionTokens: response.usage.completion_tokens,
|
|
375
|
+
completionTokensDetails: response.usage.completion_tokens_details ? {
|
|
376
|
+
reasoningTokens: (_f = response.usage.completion_tokens_details.reasoning_tokens) != null ? _f : 0
|
|
377
|
+
} : void 0,
|
|
378
|
+
cost: response.usage.cost,
|
|
379
|
+
totalTokens: (_g = response.usage.total_tokens) != null ? _g : 0
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
const hasProviderMetadata = Object.keys(providerMetadata).length > 0;
|
|
384
|
+
return __spreadValues({
|
|
354
385
|
response: {
|
|
355
386
|
id: response.id,
|
|
356
387
|
modelId: response.model
|
|
357
388
|
},
|
|
358
|
-
text: (
|
|
359
|
-
reasoning: (
|
|
360
|
-
toolCalls: (
|
|
389
|
+
text: (_h = choice.message.content) != null ? _h : void 0,
|
|
390
|
+
reasoning: (_i = choice.message.reasoning) != null ? _i : void 0,
|
|
391
|
+
toolCalls: (_j = choice.message.tool_calls) == null ? void 0 : _j.map((toolCall) => {
|
|
361
392
|
var _a2;
|
|
362
393
|
return {
|
|
363
394
|
toolCallType: "function",
|
|
@@ -367,17 +398,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
367
398
|
};
|
|
368
399
|
}),
|
|
369
400
|
finishReason: mapOpenRouterFinishReason(choice.finish_reason),
|
|
370
|
-
usage:
|
|
371
|
-
promptTokens: (_f = (_e = response.usage) == null ? void 0 : _e.prompt_tokens) != null ? _f : 0,
|
|
372
|
-
completionTokens: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : 0
|
|
373
|
-
},
|
|
401
|
+
usage: usageInfo,
|
|
374
402
|
rawCall: { rawPrompt, rawSettings },
|
|
375
403
|
rawResponse: { headers: responseHeaders },
|
|
376
404
|
warnings: [],
|
|
377
405
|
logprobs: mapOpenRouterChatLogProbsOutput(choice.logprobs)
|
|
378
|
-
};
|
|
406
|
+
}, hasProviderMetadata ? { providerMetadata } : {});
|
|
379
407
|
}
|
|
380
408
|
async doStream(options) {
|
|
409
|
+
var _a, _c;
|
|
381
410
|
const args = this.getArgs(options);
|
|
382
411
|
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
|
383
412
|
url: this.config.url({
|
|
@@ -388,7 +417,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
388
417
|
body: __spreadProps(__spreadValues({}, args), {
|
|
389
418
|
stream: true,
|
|
390
419
|
// only include stream_options when in strict compatibility mode:
|
|
391
|
-
stream_options: this.config.compatibility === "strict" ? {
|
|
420
|
+
stream_options: this.config.compatibility === "strict" ? __spreadValues({
|
|
421
|
+
include_usage: true
|
|
422
|
+
}, ((_a = this.settings.usage) == null ? void 0 : _a.include) ? { include_usage: true } : {}) : void 0
|
|
392
423
|
}),
|
|
393
424
|
failedResponseHandler: openrouterFailedResponseHandler,
|
|
394
425
|
successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(
|
|
@@ -397,7 +428,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
397
428
|
abortSignal: options.abortSignal,
|
|
398
429
|
fetch: this.config.fetch
|
|
399
430
|
});
|
|
400
|
-
const
|
|
431
|
+
const _b = args, { messages: rawPrompt } = _b, rawSettings = __objRest(_b, ["messages"]);
|
|
401
432
|
const toolCalls = [];
|
|
402
433
|
let finishReason = "other";
|
|
403
434
|
let usage = {
|
|
@@ -405,11 +436,13 @@ var OpenRouterChatLanguageModel = class {
|
|
|
405
436
|
completionTokens: Number.NaN
|
|
406
437
|
};
|
|
407
438
|
let logprobs;
|
|
439
|
+
const openrouterUsage = {};
|
|
440
|
+
const shouldIncludeUsageAccounting = !!((_c = this.settings.usage) == null ? void 0 : _c.include);
|
|
408
441
|
return {
|
|
409
442
|
stream: response.pipeThrough(
|
|
410
443
|
new TransformStream({
|
|
411
444
|
transform(chunk, controller) {
|
|
412
|
-
var _a2,
|
|
445
|
+
var _a2, _b2, _c2, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
413
446
|
if (!chunk.success) {
|
|
414
447
|
finishReason = "error";
|
|
415
448
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -438,6 +471,20 @@ var OpenRouterChatLanguageModel = class {
|
|
|
438
471
|
promptTokens: value.usage.prompt_tokens,
|
|
439
472
|
completionTokens: value.usage.completion_tokens
|
|
440
473
|
};
|
|
474
|
+
openrouterUsage.promptTokens = value.usage.prompt_tokens;
|
|
475
|
+
if (value.usage.prompt_tokens_details) {
|
|
476
|
+
openrouterUsage.promptTokensDetails = {
|
|
477
|
+
cachedTokens: (_a2 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a2 : 0
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
openrouterUsage.completionTokens = value.usage.completion_tokens;
|
|
481
|
+
if (value.usage.completion_tokens_details) {
|
|
482
|
+
openrouterUsage.completionTokensDetails = {
|
|
483
|
+
reasoningTokens: (_b2 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b2 : 0
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
openrouterUsage.cost = value.usage.cost;
|
|
487
|
+
openrouterUsage.totalTokens = value.usage.total_tokens;
|
|
441
488
|
}
|
|
442
489
|
const choice = value.choices[0];
|
|
443
490
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
@@ -482,7 +529,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
482
529
|
message: `Expected 'id' to be a string.`
|
|
483
530
|
});
|
|
484
531
|
}
|
|
485
|
-
if (((
|
|
532
|
+
if (((_c2 = toolCallDelta.function) == null ? void 0 : _c2.name) == null) {
|
|
486
533
|
throw new import_provider.InvalidResponseDataError({
|
|
487
534
|
data: toolCallDelta,
|
|
488
535
|
message: `Expected 'function.name' to be a string.`
|
|
@@ -493,7 +540,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
493
540
|
type: "function",
|
|
494
541
|
function: {
|
|
495
542
|
name: toolCallDelta.function.name,
|
|
496
|
-
arguments: (
|
|
543
|
+
arguments: (_d = toolCallDelta.function.arguments) != null ? _d : ""
|
|
497
544
|
},
|
|
498
545
|
sent: false
|
|
499
546
|
};
|
|
@@ -501,7 +548,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
501
548
|
if (toolCall2 == null) {
|
|
502
549
|
throw new Error("Tool call is missing");
|
|
503
550
|
}
|
|
504
|
-
if (((
|
|
551
|
+
if (((_e = toolCall2.function) == null ? void 0 : _e.name) != null && ((_f = toolCall2.function) == null ? void 0 : _f.arguments) != null && (0, import_provider_utils3.isParsableJson)(toolCall2.function.arguments)) {
|
|
505
552
|
controller.enqueue({
|
|
506
553
|
type: "tool-call-delta",
|
|
507
554
|
toolCallType: "function",
|
|
@@ -512,7 +559,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
512
559
|
controller.enqueue({
|
|
513
560
|
type: "tool-call",
|
|
514
561
|
toolCallType: "function",
|
|
515
|
-
toolCallId: (
|
|
562
|
+
toolCallId: (_g = toolCall2.id) != null ? _g : (0, import_provider_utils3.generateId)(),
|
|
516
563
|
toolName: toolCall2.function.name,
|
|
517
564
|
args: toolCall2.function.arguments
|
|
518
565
|
});
|
|
@@ -524,21 +571,21 @@ var OpenRouterChatLanguageModel = class {
|
|
|
524
571
|
if (toolCall == null) {
|
|
525
572
|
throw new Error("Tool call is missing");
|
|
526
573
|
}
|
|
527
|
-
if (((
|
|
528
|
-
toolCall.function.arguments += (
|
|
574
|
+
if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
|
|
575
|
+
toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
|
|
529
576
|
}
|
|
530
577
|
controller.enqueue({
|
|
531
578
|
type: "tool-call-delta",
|
|
532
579
|
toolCallType: "function",
|
|
533
580
|
toolCallId: toolCall.id,
|
|
534
581
|
toolName: toolCall.function.name,
|
|
535
|
-
argsTextDelta: (
|
|
582
|
+
argsTextDelta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
|
|
536
583
|
});
|
|
537
|
-
if (((
|
|
584
|
+
if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && (0, import_provider_utils3.isParsableJson)(toolCall.function.arguments)) {
|
|
538
585
|
controller.enqueue({
|
|
539
586
|
type: "tool-call",
|
|
540
587
|
toolCallType: "function",
|
|
541
|
-
toolCallId: (
|
|
588
|
+
toolCallId: (_n = toolCall.id) != null ? _n : (0, import_provider_utils3.generateId)(),
|
|
542
589
|
toolName: toolCall.function.name,
|
|
543
590
|
args: toolCall.function.arguments
|
|
544
591
|
});
|
|
@@ -564,12 +611,19 @@ var OpenRouterChatLanguageModel = class {
|
|
|
564
611
|
}
|
|
565
612
|
}
|
|
566
613
|
}
|
|
567
|
-
|
|
614
|
+
const providerMetadata = {};
|
|
615
|
+
if (shouldIncludeUsageAccounting && (openrouterUsage.totalTokens !== void 0 || openrouterUsage.cost !== void 0 || openrouterUsage.promptTokensDetails !== void 0 || openrouterUsage.completionTokensDetails !== void 0)) {
|
|
616
|
+
providerMetadata.openrouter = {
|
|
617
|
+
usage: openrouterUsage
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
const hasProviderMetadata = Object.keys(providerMetadata).length > 0 && shouldIncludeUsageAccounting;
|
|
621
|
+
controller.enqueue(__spreadValues({
|
|
568
622
|
type: "finish",
|
|
569
623
|
finishReason,
|
|
570
624
|
logprobs,
|
|
571
625
|
usage
|
|
572
|
-
});
|
|
626
|
+
}, hasProviderMetadata ? { providerMetadata } : {}));
|
|
573
627
|
}
|
|
574
628
|
})
|
|
575
629
|
),
|
|
@@ -584,8 +638,15 @@ var OpenRouterChatCompletionBaseResponseSchema = import_zod2.z.object({
|
|
|
584
638
|
model: import_zod2.z.string().optional(),
|
|
585
639
|
usage: import_zod2.z.object({
|
|
586
640
|
prompt_tokens: import_zod2.z.number(),
|
|
641
|
+
prompt_tokens_details: import_zod2.z.object({
|
|
642
|
+
cached_tokens: import_zod2.z.number()
|
|
643
|
+
}).optional(),
|
|
587
644
|
completion_tokens: import_zod2.z.number(),
|
|
588
|
-
|
|
645
|
+
completion_tokens_details: import_zod2.z.object({
|
|
646
|
+
reasoning_tokens: import_zod2.z.number()
|
|
647
|
+
}).optional(),
|
|
648
|
+
total_tokens: import_zod2.z.number(),
|
|
649
|
+
cost: import_zod2.z.number().optional()
|
|
589
650
|
}).nullish()
|
|
590
651
|
});
|
|
591
652
|
var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
@@ -682,14 +743,13 @@ function prepareToolsAndToolChoice(mode) {
|
|
|
682
743
|
parameters: tool.parameters
|
|
683
744
|
}
|
|
684
745
|
};
|
|
685
|
-
} else {
|
|
686
|
-
return {
|
|
687
|
-
type: "function",
|
|
688
|
-
function: {
|
|
689
|
-
name: tool.name
|
|
690
|
-
}
|
|
691
|
-
};
|
|
692
746
|
}
|
|
747
|
+
return {
|
|
748
|
+
type: "function",
|
|
749
|
+
function: {
|
|
750
|
+
name: tool.name
|
|
751
|
+
}
|
|
752
|
+
};
|
|
693
753
|
});
|
|
694
754
|
const toolChoice = mode.toolChoice;
|
|
695
755
|
if (toolChoice == null) {
|
|
@@ -878,7 +938,7 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
878
938
|
}) {
|
|
879
939
|
var _a, _b;
|
|
880
940
|
const type = mode.type;
|
|
881
|
-
const extraCallingBody = (_a = providerMetadata == null ? void 0 : providerMetadata
|
|
941
|
+
const extraCallingBody = (_a = providerMetadata == null ? void 0 : providerMetadata.openrouter) != null ? _a : {};
|
|
882
942
|
const { prompt: completionPrompt } = convertToOpenRouterCompletionPrompt({
|
|
883
943
|
prompt,
|
|
884
944
|
inputFormat
|