@openrouter/ai-sdk-provider 0.0.4 → 0.0.6
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 +226 -150
- package/dist/index.d.mts +12 -19
- package/dist/index.d.ts +12 -19
- package/dist/index.js +75 -44
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +75 -44
- package/dist/index.mjs.map +1 -1
- package/internal/dist/index.d.mts +3 -1
- package/internal/dist/index.d.ts +3 -1
- package/internal/dist/index.js +67 -31
- package/internal/dist/index.js.map +1 -1
- package/internal/dist/index.mjs +67 -31
- package/internal/dist/index.mjs.map +1 -1
- package/package.json +5 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LanguageModelV1 } from '@ai-sdk/provider';
|
|
2
|
+
export { LanguageModelV1 } from '@ai-sdk/provider';
|
|
2
3
|
|
|
3
4
|
type OpenRouterChatModelId = string;
|
|
4
5
|
interface OpenRouterChatSettings {
|
|
@@ -49,6 +50,7 @@ type OpenRouterChatConfig = {
|
|
|
49
50
|
path: string;
|
|
50
51
|
}) => string;
|
|
51
52
|
fetch?: typeof fetch;
|
|
53
|
+
extraBody?: Record<string, unknown>;
|
|
52
54
|
};
|
|
53
55
|
declare class OpenRouterChatLanguageModel implements LanguageModelV1 {
|
|
54
56
|
readonly specificationVersion = "v1";
|
|
@@ -63,7 +65,7 @@ declare class OpenRouterChatLanguageModel implements LanguageModelV1 {
|
|
|
63
65
|
doStream(options: Parameters<LanguageModelV1["doStream"]>[0]): Promise<Awaited<ReturnType<LanguageModelV1["doStream"]>>>;
|
|
64
66
|
}
|
|
65
67
|
|
|
66
|
-
type OpenRouterCompletionModelId =
|
|
68
|
+
type OpenRouterCompletionModelId = string & {};
|
|
67
69
|
interface OpenRouterCompletionSettings {
|
|
68
70
|
/**
|
|
69
71
|
Echo back the prompt in addition to the completion.
|
|
@@ -116,6 +118,7 @@ type OpenRouterCompletionConfig = {
|
|
|
116
118
|
path: string;
|
|
117
119
|
}) => string;
|
|
118
120
|
fetch?: typeof fetch;
|
|
121
|
+
extraBody?: Record<string, unknown>;
|
|
119
122
|
};
|
|
120
123
|
declare class OpenRouterCompletionLanguageModel implements LanguageModelV1 {
|
|
121
124
|
readonly specificationVersion = "v1";
|
|
@@ -158,14 +161,6 @@ interface OpenRouterProviderSettings {
|
|
|
158
161
|
*/
|
|
159
162
|
apiKey?: string;
|
|
160
163
|
/**
|
|
161
|
-
OpenRouter Organization.
|
|
162
|
-
*/
|
|
163
|
-
organization?: string;
|
|
164
|
-
/**
|
|
165
|
-
OpenRouter project.
|
|
166
|
-
*/
|
|
167
|
-
project?: string;
|
|
168
|
-
/**
|
|
169
164
|
Custom headers to include in the requests.
|
|
170
165
|
*/
|
|
171
166
|
headers?: Record<string, string>;
|
|
@@ -180,6 +175,10 @@ interface OpenRouterProviderSettings {
|
|
|
180
175
|
or to provide a custom fetch implementation for e.g. testing.
|
|
181
176
|
*/
|
|
182
177
|
fetch?: typeof fetch;
|
|
178
|
+
/**
|
|
179
|
+
A JSON object to send as the request body to access OpenRouter features & upstream provider features.
|
|
180
|
+
*/
|
|
181
|
+
extraBody?: Record<string, unknown>;
|
|
183
182
|
}
|
|
184
183
|
/**
|
|
185
184
|
Create an OpenRouter provider instance.
|
|
@@ -201,18 +200,10 @@ declare class OpenRouter {
|
|
|
201
200
|
readonly baseURL: string;
|
|
202
201
|
/**
|
|
203
202
|
API key that is being send using the `Authorization` header.
|
|
204
|
-
It defaults to the `
|
|
203
|
+
It defaults to the `OPENROUTER_API_KEY` environment variable.
|
|
205
204
|
*/
|
|
206
205
|
readonly apiKey?: string;
|
|
207
206
|
/**
|
|
208
|
-
OpenRouter Organization.
|
|
209
|
-
*/
|
|
210
|
-
readonly organization?: string;
|
|
211
|
-
/**
|
|
212
|
-
OpenRouter project.
|
|
213
|
-
*/
|
|
214
|
-
readonly project?: string;
|
|
215
|
-
/**
|
|
216
207
|
Custom headers to include in the requests.
|
|
217
208
|
*/
|
|
218
209
|
readonly headers?: Record<string, string>;
|
|
@@ -225,4 +216,6 @@ declare class OpenRouter {
|
|
|
225
216
|
completion(modelId: OpenRouterCompletionModelId, settings?: OpenRouterCompletionSettings): OpenRouterCompletionLanguageModel;
|
|
226
217
|
}
|
|
227
218
|
|
|
228
|
-
|
|
219
|
+
type OpenRouterLanguageModel = LanguageModelV1;
|
|
220
|
+
|
|
221
|
+
export { OpenRouter, type OpenRouterLanguageModel, type OpenRouterProvider, type OpenRouterProviderSettings, createOpenRouter, openrouter };
|
package/dist/index.js
CHANGED
|
@@ -79,24 +79,39 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
79
79
|
messages.push({ role: "user", content: content[0].text });
|
|
80
80
|
break;
|
|
81
81
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
content: content.map((part) => {
|
|
82
|
+
const contentParts = content.map(
|
|
83
|
+
(part) => {
|
|
85
84
|
var _a2;
|
|
86
85
|
switch (part.type) {
|
|
87
|
-
case "text":
|
|
88
|
-
return {
|
|
89
|
-
|
|
90
|
-
|
|
86
|
+
case "text":
|
|
87
|
+
return {
|
|
88
|
+
type: "text",
|
|
89
|
+
text: part.text
|
|
90
|
+
};
|
|
91
|
+
case "image":
|
|
91
92
|
return {
|
|
92
93
|
type: "image_url",
|
|
93
94
|
image_url: {
|
|
94
95
|
url: part.image instanceof URL ? part.image.toString() : `data:${(_a2 = part.mimeType) != null ? _a2 : "image/jpeg"};base64,${(0, import_provider_utils.convertUint8ArrayToBase64)(part.image)}`
|
|
95
96
|
}
|
|
96
97
|
};
|
|
98
|
+
case "file":
|
|
99
|
+
return {
|
|
100
|
+
type: "text",
|
|
101
|
+
text: part.data instanceof URL ? part.data.toString() : part.data
|
|
102
|
+
};
|
|
103
|
+
default: {
|
|
104
|
+
const _exhaustiveCheck = part;
|
|
105
|
+
throw new Error(
|
|
106
|
+
`Unsupported content part type: ${_exhaustiveCheck}`
|
|
107
|
+
);
|
|
97
108
|
}
|
|
98
109
|
}
|
|
99
|
-
}
|
|
110
|
+
}
|
|
111
|
+
);
|
|
112
|
+
messages.push({
|
|
113
|
+
role: "user",
|
|
114
|
+
content: contentParts
|
|
100
115
|
});
|
|
101
116
|
break;
|
|
102
117
|
}
|
|
@@ -199,6 +214,9 @@ var openrouterFailedResponseHandler = (0, import_provider_utils2.createJsonError
|
|
|
199
214
|
});
|
|
200
215
|
|
|
201
216
|
// src/openrouter-chat-language-model.ts
|
|
217
|
+
function isFunctionTool(tool) {
|
|
218
|
+
return "parameters" in tool;
|
|
219
|
+
}
|
|
202
220
|
var OpenRouterChatLanguageModel = class {
|
|
203
221
|
constructor(modelId, settings, config) {
|
|
204
222
|
this.specificationVersion = "v1";
|
|
@@ -221,7 +239,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
221
239
|
seed
|
|
222
240
|
}) {
|
|
223
241
|
const type = mode.type;
|
|
224
|
-
const baseArgs = {
|
|
242
|
+
const baseArgs = __spreadValues({
|
|
225
243
|
// model id:
|
|
226
244
|
model: this.modelId,
|
|
227
245
|
// model specific settings:
|
|
@@ -239,7 +257,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
239
257
|
seed,
|
|
240
258
|
// messages:
|
|
241
259
|
messages: convertToOpenRouterChatMessages(prompt)
|
|
242
|
-
};
|
|
260
|
+
}, this.config.extraBody);
|
|
243
261
|
switch (type) {
|
|
244
262
|
case "regular": {
|
|
245
263
|
return __spreadValues(__spreadValues({}, baseArgs), prepareToolsAndToolChoice(mode));
|
|
@@ -264,14 +282,11 @@ var OpenRouterChatLanguageModel = class {
|
|
|
264
282
|
]
|
|
265
283
|
});
|
|
266
284
|
}
|
|
267
|
-
case "object-grammar": {
|
|
268
|
-
throw new import_provider.UnsupportedFunctionalityError({
|
|
269
|
-
functionality: "object-grammar mode"
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
285
|
default: {
|
|
273
286
|
const _exhaustiveCheck = type;
|
|
274
|
-
throw new
|
|
287
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
288
|
+
functionality: `${_exhaustiveCheck} mode`
|
|
289
|
+
});
|
|
275
290
|
}
|
|
276
291
|
}
|
|
277
292
|
}
|
|
@@ -574,14 +589,25 @@ function prepareToolsAndToolChoice(mode) {
|
|
|
574
589
|
if (tools == null) {
|
|
575
590
|
return { tools: void 0, tool_choice: void 0 };
|
|
576
591
|
}
|
|
577
|
-
const mappedTools = tools.map((tool) =>
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
592
|
+
const mappedTools = tools.map((tool) => {
|
|
593
|
+
if (isFunctionTool(tool)) {
|
|
594
|
+
return {
|
|
595
|
+
type: "function",
|
|
596
|
+
function: {
|
|
597
|
+
name: tool.name,
|
|
598
|
+
description: tool.description,
|
|
599
|
+
parameters: tool.parameters
|
|
600
|
+
}
|
|
601
|
+
};
|
|
602
|
+
} else {
|
|
603
|
+
return {
|
|
604
|
+
type: "function",
|
|
605
|
+
function: {
|
|
606
|
+
name: tool.name
|
|
607
|
+
}
|
|
608
|
+
};
|
|
583
609
|
}
|
|
584
|
-
})
|
|
610
|
+
});
|
|
585
611
|
const toolChoice = mode.toolChoice;
|
|
586
612
|
if (toolChoice == null) {
|
|
587
613
|
return { tools: mappedTools, tool_choice: void 0 };
|
|
@@ -651,6 +677,15 @@ function convertToOpenRouterCompletionPrompt({
|
|
|
651
677
|
functionality: "images"
|
|
652
678
|
});
|
|
653
679
|
}
|
|
680
|
+
case "file": {
|
|
681
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
682
|
+
functionality: "file attachments"
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
default: {
|
|
686
|
+
const _exhaustiveCheck = part;
|
|
687
|
+
throw new Error(`Unsupported content type: ${_exhaustiveCheck}`);
|
|
688
|
+
}
|
|
654
689
|
}
|
|
655
690
|
}).join("");
|
|
656
691
|
text += `${user}:
|
|
@@ -670,6 +705,10 @@ ${userMessage}
|
|
|
670
705
|
functionality: "tool-call messages"
|
|
671
706
|
});
|
|
672
707
|
}
|
|
708
|
+
default: {
|
|
709
|
+
const _exhaustiveCheck = part;
|
|
710
|
+
throw new Error(`Unsupported content type: ${_exhaustiveCheck}`);
|
|
711
|
+
}
|
|
673
712
|
}
|
|
674
713
|
}).join("");
|
|
675
714
|
text += `${assistant}:
|
|
@@ -741,7 +780,7 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
741
780
|
var _a;
|
|
742
781
|
const type = mode.type;
|
|
743
782
|
const { prompt: completionPrompt, stopSequences } = convertToOpenRouterCompletionPrompt({ prompt, inputFormat });
|
|
744
|
-
const baseArgs = {
|
|
783
|
+
const baseArgs = __spreadValues({
|
|
745
784
|
// model id:
|
|
746
785
|
model: this.modelId,
|
|
747
786
|
// model specific settings:
|
|
@@ -761,7 +800,7 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
761
800
|
prompt: completionPrompt,
|
|
762
801
|
// stop sequences:
|
|
763
802
|
stop: stopSequences
|
|
764
|
-
};
|
|
803
|
+
}, this.config.extraBody);
|
|
765
804
|
switch (type) {
|
|
766
805
|
case "regular": {
|
|
767
806
|
if ((_a = mode.tools) == null ? void 0 : _a.length) {
|
|
@@ -786,14 +825,11 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
786
825
|
functionality: "object-tool mode"
|
|
787
826
|
});
|
|
788
827
|
}
|
|
789
|
-
case "object-grammar": {
|
|
790
|
-
throw new import_provider3.UnsupportedFunctionalityError({
|
|
791
|
-
functionality: "object-grammar mode"
|
|
792
|
-
});
|
|
793
|
-
}
|
|
794
828
|
default: {
|
|
795
829
|
const _exhaustiveCheck = type;
|
|
796
|
-
throw new
|
|
830
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
831
|
+
functionality: `${_exhaustiveCheck} mode`
|
|
832
|
+
});
|
|
797
833
|
}
|
|
798
834
|
}
|
|
799
835
|
}
|
|
@@ -961,22 +997,17 @@ var OpenRouter = class {
|
|
|
961
997
|
var _a, _b;
|
|
962
998
|
this.baseURL = (_b = (0, import_provider_utils5.withoutTrailingSlash)((_a = options.baseURL) != null ? _a : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
|
|
963
999
|
this.apiKey = options.apiKey;
|
|
964
|
-
this.organization = options.organization;
|
|
965
|
-
this.project = options.project;
|
|
966
1000
|
this.headers = options.headers;
|
|
967
1001
|
}
|
|
968
1002
|
get baseConfig() {
|
|
969
1003
|
return {
|
|
970
|
-
organization: this.organization,
|
|
971
1004
|
baseURL: this.baseURL,
|
|
972
1005
|
headers: () => __spreadValues({
|
|
973
1006
|
Authorization: `Bearer ${(0, import_provider_utils5.loadApiKey)({
|
|
974
1007
|
apiKey: this.apiKey,
|
|
975
|
-
environmentVariableName: "
|
|
1008
|
+
environmentVariableName: "OPENROUTER_API_KEY",
|
|
976
1009
|
description: "OpenRouter"
|
|
977
|
-
})}
|
|
978
|
-
"OpenRouter-Organization": this.organization,
|
|
979
|
-
"OpenRouter-Project": this.project
|
|
1010
|
+
})}`
|
|
980
1011
|
}, this.headers)
|
|
981
1012
|
};
|
|
982
1013
|
}
|
|
@@ -1007,25 +1038,25 @@ function createOpenRouter(options = {}) {
|
|
|
1007
1038
|
const getHeaders = () => __spreadValues({
|
|
1008
1039
|
Authorization: `Bearer ${(0, import_provider_utils6.loadApiKey)({
|
|
1009
1040
|
apiKey: options.apiKey,
|
|
1010
|
-
environmentVariableName: "
|
|
1041
|
+
environmentVariableName: "OPENROUTER_API_KEY",
|
|
1011
1042
|
description: "OpenRouter"
|
|
1012
|
-
})}
|
|
1013
|
-
"OpenRouter-Organization": options.organization,
|
|
1014
|
-
"OpenRouter-Project": options.project
|
|
1043
|
+
})}`
|
|
1015
1044
|
}, options.headers);
|
|
1016
1045
|
const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
|
|
1017
1046
|
provider: "openrouter.chat",
|
|
1018
1047
|
url: ({ path }) => `${baseURL}${path}`,
|
|
1019
1048
|
headers: getHeaders,
|
|
1020
1049
|
compatibility,
|
|
1021
|
-
fetch: options.fetch
|
|
1050
|
+
fetch: options.fetch,
|
|
1051
|
+
extraBody: options.extraBody
|
|
1022
1052
|
});
|
|
1023
1053
|
const createCompletionModel = (modelId, settings = {}) => new OpenRouterCompletionLanguageModel(modelId, settings, {
|
|
1024
1054
|
provider: "openrouter.completion",
|
|
1025
1055
|
url: ({ path }) => `${baseURL}${path}`,
|
|
1026
1056
|
headers: getHeaders,
|
|
1027
1057
|
compatibility,
|
|
1028
|
-
fetch: options.fetch
|
|
1058
|
+
fetch: options.fetch,
|
|
1059
|
+
extraBody: options.extraBody
|
|
1029
1060
|
});
|
|
1030
1061
|
const createLanguageModel = (modelId, settings) => {
|
|
1031
1062
|
if (new.target) {
|