@openrouter/ai-sdk-provider 0.4.2 → 0.4.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/README.md +1 -0
- package/dist/index.js +62 -59
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +62 -59
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +62 -59
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +62 -59
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -50,15 +50,15 @@ import { z as z2 } from "zod";
|
|
|
50
50
|
|
|
51
51
|
// src/convert-to-openrouter-chat-messages.ts
|
|
52
52
|
import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
|
|
53
|
+
function getCacheControl(providerMetadata) {
|
|
54
|
+
var _a, _b, _c;
|
|
55
|
+
const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
|
|
56
|
+
const openrouter2 = providerMetadata == null ? void 0 : providerMetadata.openrouter;
|
|
57
|
+
return (_c = (_b = (_a = openrouter2 == null ? void 0 : openrouter2.cacheControl) != null ? _a : openrouter2 == null ? void 0 : openrouter2.cache_control) != null ? _b : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
|
|
58
|
+
}
|
|
53
59
|
function convertToOpenRouterChatMessages(prompt) {
|
|
54
60
|
var _a, _b, _c;
|
|
55
61
|
const messages = [];
|
|
56
|
-
function getCacheControl(providerMetadata) {
|
|
57
|
-
var _a2;
|
|
58
|
-
const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
|
|
59
|
-
const cacheControlValue = (_a2 = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a2 : anthropic == null ? void 0 : anthropic.cache_control;
|
|
60
|
-
return cacheControlValue;
|
|
61
|
-
}
|
|
62
62
|
for (const { role, content, providerMetadata } of prompt) {
|
|
63
63
|
switch (role) {
|
|
64
64
|
case "system": {
|
|
@@ -81,31 +81,31 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
81
81
|
const messageCacheControl = getCacheControl(providerMetadata);
|
|
82
82
|
const contentParts = content.map(
|
|
83
83
|
(part) => {
|
|
84
|
-
var _a2, _b2, _c2;
|
|
84
|
+
var _a2, _b2, _c2, _d;
|
|
85
85
|
switch (part.type) {
|
|
86
86
|
case "text":
|
|
87
87
|
return {
|
|
88
88
|
type: "text",
|
|
89
89
|
text: part.text,
|
|
90
90
|
// For text parts, only use part-specific cache control
|
|
91
|
-
cache_control: getCacheControl(part.providerMetadata)
|
|
91
|
+
cache_control: (_a2 = getCacheControl(part.providerMetadata)) != null ? _a2 : messageCacheControl
|
|
92
92
|
};
|
|
93
93
|
case "image":
|
|
94
94
|
return {
|
|
95
95
|
type: "image_url",
|
|
96
96
|
image_url: {
|
|
97
|
-
url: part.image instanceof URL ? part.image.toString() : `data:${(
|
|
97
|
+
url: part.image instanceof URL ? part.image.toString() : `data:${(_b2 = part.mimeType) != null ? _b2 : "image/jpeg"};base64,${convertUint8ArrayToBase64(
|
|
98
98
|
part.image
|
|
99
99
|
)}`
|
|
100
100
|
},
|
|
101
101
|
// For image parts, use part-specific or message-level cache control
|
|
102
|
-
cache_control: (
|
|
102
|
+
cache_control: (_c2 = getCacheControl(part.providerMetadata)) != null ? _c2 : messageCacheControl
|
|
103
103
|
};
|
|
104
104
|
case "file":
|
|
105
105
|
return {
|
|
106
106
|
type: "text",
|
|
107
107
|
text: part.data instanceof URL ? part.data.toString() : part.data,
|
|
108
|
-
cache_control: (
|
|
108
|
+
cache_control: (_d = getCacheControl(part.providerMetadata)) != null ? _d : messageCacheControl
|
|
109
109
|
};
|
|
110
110
|
default: {
|
|
111
111
|
const _exhaustiveCheck = part;
|
|
@@ -213,7 +213,7 @@ function mapOpenRouterFinishReason(finishReason) {
|
|
|
213
213
|
// src/openrouter-error.ts
|
|
214
214
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
215
215
|
import { z } from "zod";
|
|
216
|
-
var
|
|
216
|
+
var OpenRouterErrorResponseSchema = z.object({
|
|
217
217
|
error: z.object({
|
|
218
218
|
message: z.string(),
|
|
219
219
|
type: z.string(),
|
|
@@ -222,7 +222,7 @@ var openAIErrorDataSchema = z.object({
|
|
|
222
222
|
})
|
|
223
223
|
});
|
|
224
224
|
var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
|
|
225
|
-
errorSchema:
|
|
225
|
+
errorSchema: OpenRouterErrorResponseSchema,
|
|
226
226
|
errorToMessage: (data) => data.error.message
|
|
227
227
|
});
|
|
228
228
|
|
|
@@ -318,7 +318,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
318
318
|
}
|
|
319
319
|
}
|
|
320
320
|
async doGenerate(options) {
|
|
321
|
-
var _b, _c, _d;
|
|
321
|
+
var _b, _c, _d, _e, _f, _g, _h;
|
|
322
322
|
const args = this.getArgs(options);
|
|
323
323
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
324
324
|
url: this.config.url({
|
|
@@ -329,17 +329,21 @@ var OpenRouterChatLanguageModel = class {
|
|
|
329
329
|
body: args,
|
|
330
330
|
failedResponseHandler: openrouterFailedResponseHandler,
|
|
331
331
|
successfulResponseHandler: createJsonResponseHandler(
|
|
332
|
-
|
|
332
|
+
OpenRouterNonStreamChatCompletionResponseSchema
|
|
333
333
|
),
|
|
334
334
|
abortSignal: options.abortSignal,
|
|
335
335
|
fetch: this.config.fetch
|
|
336
336
|
});
|
|
337
337
|
const _a = args, { messages: rawPrompt } = _a, rawSettings = __objRest(_a, ["messages"]);
|
|
338
338
|
const choice = response.choices[0];
|
|
339
|
-
if (choice
|
|
339
|
+
if (!choice) {
|
|
340
340
|
throw new Error("No choice in response");
|
|
341
341
|
}
|
|
342
342
|
return {
|
|
343
|
+
response: {
|
|
344
|
+
id: response.id,
|
|
345
|
+
modelId: response.model
|
|
346
|
+
},
|
|
343
347
|
text: (_b = choice.message.content) != null ? _b : void 0,
|
|
344
348
|
reasoning: (_c = choice.message.reasoning) != null ? _c : void 0,
|
|
345
349
|
toolCalls: (_d = choice.message.tool_calls) == null ? void 0 : _d.map((toolCall) => {
|
|
@@ -353,8 +357,8 @@ var OpenRouterChatLanguageModel = class {
|
|
|
353
357
|
}),
|
|
354
358
|
finishReason: mapOpenRouterFinishReason(choice.finish_reason),
|
|
355
359
|
usage: {
|
|
356
|
-
promptTokens: response.usage.prompt_tokens,
|
|
357
|
-
completionTokens: response.usage.completion_tokens
|
|
360
|
+
promptTokens: (_f = (_e = response.usage) == null ? void 0 : _e.prompt_tokens) != null ? _f : 0,
|
|
361
|
+
completionTokens: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : 0
|
|
358
362
|
},
|
|
359
363
|
rawCall: { rawPrompt, rawSettings },
|
|
360
364
|
rawResponse: { headers: responseHeaders },
|
|
@@ -377,7 +381,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
377
381
|
}),
|
|
378
382
|
failedResponseHandler: openrouterFailedResponseHandler,
|
|
379
383
|
successfulResponseHandler: createEventSourceResponseHandler(
|
|
380
|
-
|
|
384
|
+
OpenRouterStreamChatCompletionChunkSchema
|
|
381
385
|
),
|
|
382
386
|
abortSignal: options.abortSignal,
|
|
383
387
|
fetch: this.config.fetch
|
|
@@ -412,6 +416,12 @@ var OpenRouterChatLanguageModel = class {
|
|
|
412
416
|
id: value.id
|
|
413
417
|
});
|
|
414
418
|
}
|
|
419
|
+
if (value.model) {
|
|
420
|
+
controller.enqueue({
|
|
421
|
+
type: "response-metadata",
|
|
422
|
+
modelId: value.model
|
|
423
|
+
});
|
|
424
|
+
}
|
|
415
425
|
if (value.usage != null) {
|
|
416
426
|
usage = {
|
|
417
427
|
promptTokens: value.usage.prompt_tokens,
|
|
@@ -539,7 +549,16 @@ var OpenRouterChatLanguageModel = class {
|
|
|
539
549
|
};
|
|
540
550
|
}
|
|
541
551
|
};
|
|
542
|
-
var
|
|
552
|
+
var OpenRouterChatCompletionBaseResponseSchema = z2.object({
|
|
553
|
+
id: z2.string().optional(),
|
|
554
|
+
model: z2.string().optional(),
|
|
555
|
+
usage: z2.object({
|
|
556
|
+
prompt_tokens: z2.number(),
|
|
557
|
+
completion_tokens: z2.number(),
|
|
558
|
+
total_tokens: z2.number()
|
|
559
|
+
}).nullish()
|
|
560
|
+
});
|
|
561
|
+
var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
543
562
|
choices: z2.array(
|
|
544
563
|
z2.object({
|
|
545
564
|
message: z2.object({
|
|
@@ -574,15 +593,10 @@ var openAIChatResponseSchema = z2.object({
|
|
|
574
593
|
}).nullable().optional(),
|
|
575
594
|
finish_reason: z2.string().optional().nullable()
|
|
576
595
|
})
|
|
577
|
-
)
|
|
578
|
-
usage: z2.object({
|
|
579
|
-
prompt_tokens: z2.number(),
|
|
580
|
-
completion_tokens: z2.number()
|
|
581
|
-
})
|
|
596
|
+
)
|
|
582
597
|
});
|
|
583
|
-
var
|
|
584
|
-
|
|
585
|
-
id: z2.string().optional(),
|
|
598
|
+
var OpenRouterStreamChatCompletionChunkSchema = z2.union([
|
|
599
|
+
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
586
600
|
choices: z2.array(
|
|
587
601
|
z2.object({
|
|
588
602
|
delta: z2.object({
|
|
@@ -618,13 +632,9 @@ var openrouterChatChunkSchema = z2.union([
|
|
|
618
632
|
finish_reason: z2.string().nullable().optional(),
|
|
619
633
|
index: z2.number()
|
|
620
634
|
})
|
|
621
|
-
)
|
|
622
|
-
usage: z2.object({
|
|
623
|
-
prompt_tokens: z2.number(),
|
|
624
|
-
completion_tokens: z2.number()
|
|
625
|
-
}).nullish()
|
|
635
|
+
)
|
|
626
636
|
}),
|
|
627
|
-
|
|
637
|
+
OpenRouterErrorResponseSchema
|
|
628
638
|
]);
|
|
629
639
|
function prepareToolsAndToolChoice(mode) {
|
|
630
640
|
var _a;
|
|
@@ -910,6 +920,7 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
910
920
|
}
|
|
911
921
|
}
|
|
912
922
|
async doGenerate(options) {
|
|
923
|
+
var _b, _c, _d, _e, _f;
|
|
913
924
|
const args = this.getArgs(options);
|
|
914
925
|
const { responseHeaders, value: response } = await postJsonToApi2({
|
|
915
926
|
url: this.config.url({
|
|
@@ -920,22 +931,29 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
920
931
|
body: args,
|
|
921
932
|
failedResponseHandler: openrouterFailedResponseHandler,
|
|
922
933
|
successfulResponseHandler: createJsonResponseHandler2(
|
|
923
|
-
|
|
934
|
+
OpenRouterCompletionChunkSchema
|
|
924
935
|
),
|
|
925
936
|
abortSignal: options.abortSignal,
|
|
926
937
|
fetch: this.config.fetch
|
|
927
938
|
});
|
|
928
939
|
const _a = args, { prompt: rawPrompt } = _a, rawSettings = __objRest(_a, ["prompt"]);
|
|
940
|
+
if ("error" in response) {
|
|
941
|
+
throw new Error(`${response.error.message}`);
|
|
942
|
+
}
|
|
929
943
|
const choice = response.choices[0];
|
|
930
944
|
if (!choice) {
|
|
931
945
|
throw new Error("No choice in OpenRouter completion response");
|
|
932
946
|
}
|
|
933
947
|
return {
|
|
934
|
-
|
|
948
|
+
response: {
|
|
949
|
+
id: response.id,
|
|
950
|
+
modelId: response.model
|
|
951
|
+
},
|
|
952
|
+
text: (_b = choice.text) != null ? _b : "",
|
|
935
953
|
reasoning: choice.reasoning || void 0,
|
|
936
954
|
usage: {
|
|
937
|
-
promptTokens: response.usage.prompt_tokens,
|
|
938
|
-
completionTokens: response.usage.completion_tokens
|
|
955
|
+
promptTokens: (_d = (_c = response.usage) == null ? void 0 : _c.prompt_tokens) != null ? _d : 0,
|
|
956
|
+
completionTokens: (_f = (_e = response.usage) == null ? void 0 : _e.completion_tokens) != null ? _f : 0
|
|
939
957
|
},
|
|
940
958
|
finishReason: mapOpenRouterFinishReason(choice.finish_reason),
|
|
941
959
|
logprobs: mapOpenRouterCompletionLogProbs(choice.logprobs),
|
|
@@ -959,7 +977,7 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
959
977
|
}),
|
|
960
978
|
failedResponseHandler: openrouterFailedResponseHandler,
|
|
961
979
|
successfulResponseHandler: createEventSourceResponseHandler2(
|
|
962
|
-
|
|
980
|
+
OpenRouterCompletionChunkSchema
|
|
963
981
|
),
|
|
964
982
|
abortSignal: options.abortSignal,
|
|
965
983
|
fetch: this.config.fetch
|
|
@@ -1026,29 +1044,14 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
1026
1044
|
};
|
|
1027
1045
|
}
|
|
1028
1046
|
};
|
|
1029
|
-
var
|
|
1030
|
-
choices: z3.array(
|
|
1031
|
-
z3.object({
|
|
1032
|
-
text: z3.string(),
|
|
1033
|
-
reasoning: z3.string().nullish().optional(),
|
|
1034
|
-
finish_reason: z3.string(),
|
|
1035
|
-
logprobs: z3.object({
|
|
1036
|
-
tokens: z3.array(z3.string()),
|
|
1037
|
-
token_logprobs: z3.array(z3.number()),
|
|
1038
|
-
top_logprobs: z3.array(z3.record(z3.string(), z3.number())).nullable()
|
|
1039
|
-
}).nullable().optional()
|
|
1040
|
-
})
|
|
1041
|
-
),
|
|
1042
|
-
usage: z3.object({
|
|
1043
|
-
prompt_tokens: z3.number(),
|
|
1044
|
-
completion_tokens: z3.number()
|
|
1045
|
-
})
|
|
1046
|
-
});
|
|
1047
|
-
var openrouterCompletionChunkSchema = z3.union([
|
|
1047
|
+
var OpenRouterCompletionChunkSchema = z3.union([
|
|
1048
1048
|
z3.object({
|
|
1049
|
+
id: z3.string().optional(),
|
|
1050
|
+
model: z3.string().optional(),
|
|
1049
1051
|
choices: z3.array(
|
|
1050
1052
|
z3.object({
|
|
1051
1053
|
text: z3.string(),
|
|
1054
|
+
reasoning: z3.string().nullish().optional(),
|
|
1052
1055
|
finish_reason: z3.string().nullish(),
|
|
1053
1056
|
index: z3.number(),
|
|
1054
1057
|
logprobs: z3.object({
|
|
@@ -1063,7 +1066,7 @@ var openrouterCompletionChunkSchema = z3.union([
|
|
|
1063
1066
|
completion_tokens: z3.number()
|
|
1064
1067
|
}).optional().nullable()
|
|
1065
1068
|
}),
|
|
1066
|
-
|
|
1069
|
+
OpenRouterErrorResponseSchema
|
|
1067
1070
|
]);
|
|
1068
1071
|
|
|
1069
1072
|
// src/openrouter-facade.ts
|