@openrouter/ai-sdk-provider 1.5.4 → 6.0.0-alpha.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/dist/index.d.mts +331 -493
- package/dist/index.d.ts +331 -493
- package/dist/index.js +1047 -2782
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1040 -2768
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +315 -361
- package/dist/internal/index.d.ts +315 -361
- package/dist/internal/index.js +372 -2593
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +365 -2582
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +40 -36
|
@@ -1,410 +1,364 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { z } from 'zod/v4';
|
|
4
|
-
import * as models from '@openrouter/sdk/models';
|
|
1
|
+
import { LanguageModelV3Usage, JSONObject, LanguageModelV3FinishReason, LanguageModelV3Prompt } from '@ai-sdk/provider';
|
|
2
|
+
import { ChatGenerationTokenUsage } from '@openrouter/sdk/models';
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Raw usage data from OpenRouter API response.
|
|
6
|
+
*/
|
|
7
|
+
interface OpenRouterRawUsage {
|
|
8
|
+
inputTokens?: number;
|
|
9
|
+
outputTokens?: number;
|
|
10
|
+
inputTokensDetails?: {
|
|
11
|
+
cachedTokens?: number;
|
|
12
|
+
};
|
|
13
|
+
outputTokensDetails?: {
|
|
14
|
+
reasoningTokens?: number;
|
|
15
|
+
};
|
|
18
16
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
type: z.ZodLiteral<ReasoningDetailType.Encrypted>;
|
|
27
|
-
data: z.ZodString;
|
|
28
|
-
id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
29
|
-
format: z.ZodOptional<z.ZodNullable<z.ZodEnum<typeof ReasoningFormat>>>;
|
|
30
|
-
index: z.ZodOptional<z.ZodNumber>;
|
|
31
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
32
|
-
type: z.ZodLiteral<ReasoningDetailType.Text>;
|
|
33
|
-
text: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
34
|
-
signature: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
35
|
-
id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
36
|
-
format: z.ZodOptional<z.ZodNullable<z.ZodEnum<typeof ReasoningFormat>>>;
|
|
37
|
-
index: z.ZodOptional<z.ZodNumber>;
|
|
38
|
-
}, z.core.$strip>]>;
|
|
39
|
-
type ReasoningDetailUnion = z.infer<typeof ReasoningDetailUnionSchema>;
|
|
17
|
+
/**
|
|
18
|
+
* Builds a LanguageModelV3Usage object from OpenRouter API usage data.
|
|
19
|
+
*
|
|
20
|
+
* @param usage - The raw usage data from the OpenRouter API response.
|
|
21
|
+
* @returns A LanguageModelV3Usage object with standardized token counts.
|
|
22
|
+
*/
|
|
23
|
+
declare function buildUsage(usage: OpenRouterRawUsage | undefined): LanguageModelV3Usage;
|
|
40
24
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
Accepts a JSON object that maps tokens (specified by their token ID in
|
|
47
|
-
the GPT tokenizer) to an associated bias value from -100 to 100. You
|
|
48
|
-
can use this tokenizer tool to convert text to token IDs. Mathematically,
|
|
49
|
-
the bias is added to the logits generated by the model prior to sampling.
|
|
50
|
-
The exact effect will vary per model, but values between -1 and 1 should
|
|
51
|
-
decrease or increase likelihood of selection; values like -100 or 100
|
|
52
|
-
should result in a ban or exclusive selection of the relevant token.
|
|
53
|
-
|
|
54
|
-
As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
|
|
55
|
-
token from being generated.
|
|
56
|
-
*/
|
|
57
|
-
logitBias?: Record<number, number>;
|
|
58
|
-
/**
|
|
59
|
-
Return the log probabilities of the tokens. Including logprobs will increase
|
|
60
|
-
the response size and can slow down response times. However, it can
|
|
61
|
-
be useful to understand better how the model is behaving.
|
|
62
|
-
|
|
63
|
-
Setting to true will return the log probabilities of the tokens that
|
|
64
|
-
were generated.
|
|
65
|
-
|
|
66
|
-
Setting to a number will return the log probabilities of the top n
|
|
67
|
-
tokens that were generated.
|
|
68
|
-
*/
|
|
69
|
-
logprobs?: boolean | number;
|
|
25
|
+
/**
|
|
26
|
+
* OpenRouter-specific provider metadata structure.
|
|
27
|
+
*/
|
|
28
|
+
interface OpenRouterProviderMetadata {
|
|
70
29
|
/**
|
|
71
|
-
|
|
30
|
+
* The response ID from OpenRouter.
|
|
72
31
|
*/
|
|
73
|
-
|
|
32
|
+
responseId?: string;
|
|
74
33
|
/**
|
|
75
|
-
|
|
76
|
-
monitor and detect abuse. Learn more.
|
|
77
|
-
*/
|
|
78
|
-
user?: string;
|
|
79
|
-
/**
|
|
80
|
-
* Plugin configurations for enabling various capabilities
|
|
34
|
+
* The upstream provider that served the request.
|
|
81
35
|
*/
|
|
82
|
-
|
|
83
|
-
id: models.IdWeb;
|
|
84
|
-
max_results?: number;
|
|
85
|
-
search_prompt?: string;
|
|
86
|
-
engine?: models.Engine;
|
|
87
|
-
} | {
|
|
88
|
-
id: models.IdFileParser;
|
|
89
|
-
max_files?: number;
|
|
90
|
-
pdf?: {
|
|
91
|
-
engine?: models.PdfEngine;
|
|
92
|
-
};
|
|
93
|
-
} | {
|
|
94
|
-
id: models.IdModeration;
|
|
95
|
-
}>;
|
|
36
|
+
provider?: string;
|
|
96
37
|
/**
|
|
97
|
-
*
|
|
38
|
+
* Detailed usage information.
|
|
98
39
|
*/
|
|
99
|
-
|
|
40
|
+
usage?: {
|
|
41
|
+
promptTokens?: number;
|
|
42
|
+
completionTokens?: number;
|
|
43
|
+
totalTokens?: number;
|
|
44
|
+
promptTokensDetails?: {
|
|
45
|
+
cachedTokens?: number;
|
|
46
|
+
cacheWriteTokens?: number;
|
|
47
|
+
audioTokens?: number;
|
|
48
|
+
videoTokens?: number;
|
|
49
|
+
};
|
|
50
|
+
completionTokensDetails?: {
|
|
51
|
+
reasoningTokens?: number;
|
|
52
|
+
imageTokens?: number;
|
|
53
|
+
};
|
|
100
54
|
/**
|
|
101
|
-
*
|
|
55
|
+
* Cost in USD (omit if unavailable).
|
|
102
56
|
*/
|
|
103
|
-
|
|
57
|
+
cost?: number;
|
|
104
58
|
/**
|
|
105
|
-
*
|
|
59
|
+
* Whether the request used BYOK (Bring Your Own Key).
|
|
106
60
|
*/
|
|
107
|
-
|
|
61
|
+
isByok?: boolean;
|
|
108
62
|
/**
|
|
109
|
-
*
|
|
110
|
-
* - "native": Use provider's built-in web search
|
|
111
|
-
* - "exa": Use Exa's search API
|
|
112
|
-
* - undefined: Native if supported, otherwise Exa
|
|
113
|
-
* @see https://openrouter.ai/docs/features/web-search
|
|
63
|
+
* Detailed cost breakdown.
|
|
114
64
|
*/
|
|
115
|
-
|
|
65
|
+
costDetails?: JSONObject;
|
|
116
66
|
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Extended prompt tokens details with fields not in SDK types.
|
|
70
|
+
*/
|
|
71
|
+
interface ExtendedPromptTokensDetails {
|
|
72
|
+
cachedTokens?: number | null;
|
|
73
|
+
cacheWriteTokens?: number | null;
|
|
74
|
+
audioTokens?: number | null;
|
|
75
|
+
videoTokens?: number | null;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Extended completion tokens details with fields not in SDK types.
|
|
79
|
+
*/
|
|
80
|
+
interface ExtendedCompletionTokensDetails {
|
|
81
|
+
reasoningTokens?: number | null;
|
|
82
|
+
imageTokens?: number | null;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Extended usage type with additional fields from raw OpenRouter API response.
|
|
86
|
+
* The SDK types don't include cost/is_byok but the API returns them.
|
|
87
|
+
*/
|
|
88
|
+
interface OpenRouterUsageExtended extends Omit<ChatGenerationTokenUsage, 'promptTokensDetails' | 'completionTokensDetails'> {
|
|
89
|
+
cost?: number;
|
|
90
|
+
isByok?: boolean;
|
|
91
|
+
costDetails?: JSONObject;
|
|
92
|
+
promptTokensDetails?: ExtendedPromptTokensDetails | null;
|
|
93
|
+
completionTokensDetails?: ExtendedCompletionTokensDetails | null;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Response data from OpenRouter API used to build provider metadata.
|
|
97
|
+
* Note: The SDK transforms snake_case to camelCase for all fields.
|
|
98
|
+
* The `provider` field exists in raw API responses but isn't in SDK types.
|
|
99
|
+
*/
|
|
100
|
+
interface OpenRouterResponseData {
|
|
101
|
+
id?: string;
|
|
117
102
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* @see https://openrouter.ai/docs/api-reference/debugging
|
|
103
|
+
* The upstream provider that served the request (e.g. "Google", "Anthropic").
|
|
104
|
+
* This field exists in the raw API response but isn't in SDK TypeScript types.
|
|
121
105
|
*/
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* When true, echoes back the request body that was sent to the upstream provider.
|
|
125
|
-
* The debug data will be returned as the first chunk in the stream with a `debug.echo_upstream_body` field.
|
|
126
|
-
* Sensitive data like user IDs and base64 content will be redacted.
|
|
127
|
-
*/
|
|
128
|
-
echo_upstream_body?: boolean;
|
|
129
|
-
};
|
|
106
|
+
provider?: string;
|
|
130
107
|
/**
|
|
131
|
-
*
|
|
108
|
+
* Usage data in camelCase (as transformed by SDK).
|
|
132
109
|
*/
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Only use providers that support all parameters in your request (default: false)
|
|
144
|
-
*/
|
|
145
|
-
require_parameters?: boolean;
|
|
146
|
-
/**
|
|
147
|
-
* Control whether to use providers that may store data
|
|
148
|
-
*/
|
|
149
|
-
data_collection?: models.DataCollection;
|
|
150
|
-
/**
|
|
151
|
-
* List of provider slugs to allow for this request
|
|
152
|
-
*/
|
|
153
|
-
only?: string[];
|
|
154
|
-
/**
|
|
155
|
-
* List of provider slugs to skip for this request
|
|
156
|
-
*/
|
|
157
|
-
ignore?: string[];
|
|
158
|
-
/**
|
|
159
|
-
* List of quantization levels to filter by (e.g. ["int4", "int8"])
|
|
160
|
-
*/
|
|
161
|
-
quantizations?: Array<models.Quantization>;
|
|
162
|
-
/**
|
|
163
|
-
* Sort providers by price, throughput, or latency
|
|
164
|
-
*/
|
|
165
|
-
sort?: models.ProviderSort;
|
|
166
|
-
/**
|
|
167
|
-
* Maximum pricing you want to pay for this request
|
|
168
|
-
*/
|
|
169
|
-
max_price?: {
|
|
170
|
-
prompt?: number | string;
|
|
171
|
-
completion?: number | string;
|
|
172
|
-
image?: number | string;
|
|
173
|
-
audio?: number | string;
|
|
174
|
-
request?: number | string;
|
|
175
|
-
};
|
|
176
|
-
/**
|
|
177
|
-
* Whether to restrict routing to only ZDR (Zero Data Retention) endpoints.
|
|
178
|
-
* When true, only endpoints that do not retain prompts will be used.
|
|
179
|
-
*/
|
|
180
|
-
zdr?: boolean;
|
|
181
|
-
};
|
|
182
|
-
} & OpenRouterSharedSettings;
|
|
110
|
+
usage?: OpenRouterUsageExtended;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Builds provider metadata from OpenRouter API response data.
|
|
114
|
+
*
|
|
115
|
+
* @param response - The response data from OpenRouter API (with camelCase fields from SDK).
|
|
116
|
+
* @returns Provider metadata in the format expected by AI SDK.
|
|
117
|
+
*/
|
|
118
|
+
declare function buildProviderMetadata(response: OpenRouterResponseData | undefined): Record<string, JSONObject> | undefined;
|
|
183
119
|
|
|
184
|
-
|
|
185
|
-
|
|
120
|
+
/**
|
|
121
|
+
* Configuration for an OpenRouter plugin.
|
|
122
|
+
*
|
|
123
|
+
* @description
|
|
124
|
+
* Plugins extend OpenRouter functionality with features like web search, code execution,
|
|
125
|
+
* and more. Each plugin has a unique identifier and optional configuration parameters.
|
|
126
|
+
* Additional plugin-specific properties can be included as needed.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* const model = openrouter('anthropic/claude-3.5-sonnet', {
|
|
131
|
+
* plugins: [
|
|
132
|
+
* { id: 'web-search' },
|
|
133
|
+
* { id: 'code-interpreter', config: { timeout: 30000 } },
|
|
134
|
+
* ],
|
|
135
|
+
* });
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
interface OpenRouterPluginConfig {
|
|
186
139
|
/**
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
Accepts a JSON object that maps tokens (specified by their token ID in
|
|
190
|
-
the GPT tokenizer) to an associated bias value from -100 to 100. You
|
|
191
|
-
can use this tokenizer tool to convert text to token IDs. Mathematically,
|
|
192
|
-
the bias is added to the logits generated by the model prior to sampling.
|
|
193
|
-
The exact effect will vary per model, but values between -1 and 1 should
|
|
194
|
-
decrease or increase likelihood of selection; values like -100 or 100
|
|
195
|
-
should result in a ban or exclusive selection of the relevant token.
|
|
196
|
-
|
|
197
|
-
As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
|
|
198
|
-
token from being generated.
|
|
140
|
+
* The plugin identifier.
|
|
199
141
|
*/
|
|
200
|
-
|
|
142
|
+
id: string;
|
|
201
143
|
/**
|
|
202
|
-
|
|
203
|
-
the response size and can slow down response times. However, it can
|
|
204
|
-
be useful to better understand how the model is behaving.
|
|
205
|
-
|
|
206
|
-
Setting to true will return the log probabilities of the tokens that
|
|
207
|
-
were generated.
|
|
208
|
-
|
|
209
|
-
Setting to a number will return the log probabilities of the top n
|
|
210
|
-
tokens that were generated.
|
|
144
|
+
* Plugin-specific configuration.
|
|
211
145
|
*/
|
|
212
|
-
|
|
146
|
+
config?: Record<string, unknown>;
|
|
213
147
|
/**
|
|
214
|
-
|
|
148
|
+
* Allow any additional plugin-specific properties.
|
|
215
149
|
*/
|
|
216
|
-
|
|
217
|
-
} & OpenRouterSharedSettings;
|
|
218
|
-
|
|
219
|
-
type OpenRouterCompletionConfig = {
|
|
220
|
-
provider: string;
|
|
221
|
-
compatibility: 'strict' | 'compatible';
|
|
222
|
-
headers: () => Record<string, string | undefined>;
|
|
223
|
-
url: (options: {
|
|
224
|
-
modelId: string;
|
|
225
|
-
path: string;
|
|
226
|
-
}) => string;
|
|
227
|
-
fetch?: typeof fetch;
|
|
228
|
-
extraBody?: Record<string, unknown>;
|
|
229
|
-
};
|
|
230
|
-
declare class OpenRouterCompletionLanguageModel implements LanguageModelV2 {
|
|
231
|
-
readonly specificationVersion: "v2";
|
|
232
|
-
readonly provider = "openrouter";
|
|
233
|
-
readonly modelId: OpenRouterCompletionModelId;
|
|
234
|
-
readonly supportsImageUrls = true;
|
|
235
|
-
readonly supportedUrls: Record<string, RegExp[]>;
|
|
236
|
-
readonly defaultObjectGenerationMode: undefined;
|
|
237
|
-
readonly settings: OpenRouterCompletionSettings;
|
|
238
|
-
private readonly config;
|
|
239
|
-
constructor(modelId: OpenRouterCompletionModelId, settings: OpenRouterCompletionSettings, config: OpenRouterCompletionConfig);
|
|
240
|
-
private getArgs;
|
|
241
|
-
doGenerate(options: LanguageModelV2CallOptions): Promise<Awaited<ReturnType<LanguageModelV2['doGenerate']>>>;
|
|
242
|
-
doStream(options: LanguageModelV2CallOptions): Promise<Awaited<ReturnType<LanguageModelV2['doStream']>>>;
|
|
150
|
+
[key: string]: unknown;
|
|
243
151
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
152
|
+
/**
|
|
153
|
+
* Configuration for OpenRouter's provider routing behavior.
|
|
154
|
+
*
|
|
155
|
+
* @description
|
|
156
|
+
* Controls how OpenRouter selects and falls back between different AI providers
|
|
157
|
+
* when routing requests. Use this to specify provider preferences, enable/disable
|
|
158
|
+
* fallbacks, and require specific provider parameters.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* const model = openrouter('openai/gpt-4', {
|
|
163
|
+
* provider: {
|
|
164
|
+
* order: ['Azure', 'OpenAI'],
|
|
165
|
+
* allowFallbacks: true,
|
|
166
|
+
* },
|
|
167
|
+
* });
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
interface OpenRouterProviderRoutingConfig {
|
|
247
171
|
/**
|
|
248
|
-
*
|
|
249
|
-
* monitor and detect abuse.
|
|
172
|
+
* Provider order preference.
|
|
250
173
|
*/
|
|
251
|
-
|
|
174
|
+
order?: string[];
|
|
252
175
|
/**
|
|
253
|
-
*
|
|
176
|
+
* Allow fallbacks to other providers.
|
|
254
177
|
*/
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
178
|
+
allowFallbacks?: boolean;
|
|
179
|
+
/**
|
|
180
|
+
* Required provider parameters.
|
|
181
|
+
*/
|
|
182
|
+
requireParameters?: boolean;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Model-specific options for OpenRouter requests.
|
|
186
|
+
*
|
|
187
|
+
* @description
|
|
188
|
+
* Options that can be passed when creating a model to customize its behavior.
|
|
189
|
+
* These include OpenRouter-specific features like plugins, transforms, model
|
|
190
|
+
* fallbacks, and routing configuration. Additional properties are passed through
|
|
191
|
+
* to the underlying API.
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* const model = openrouter('anthropic/claude-3.5-sonnet', {
|
|
196
|
+
* usage: { include: true },
|
|
197
|
+
* transforms: ['middle-out'],
|
|
198
|
+
* models: ['anthropic/claude-3-opus', 'openai/gpt-4'], // fallbacks
|
|
199
|
+
* provider: {
|
|
200
|
+
* order: ['Anthropic'],
|
|
201
|
+
* allowFallbacks: false,
|
|
202
|
+
* },
|
|
203
|
+
* });
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
interface OpenRouterModelOptions {
|
|
207
|
+
/**
|
|
208
|
+
* Usage accounting configuration.
|
|
209
|
+
*/
|
|
210
|
+
usage?: {
|
|
284
211
|
/**
|
|
285
|
-
*
|
|
212
|
+
* Whether to include usage information in the response.
|
|
286
213
|
*/
|
|
287
|
-
|
|
288
|
-
prompt?: number | string;
|
|
289
|
-
completion?: number | string;
|
|
290
|
-
image?: number | string;
|
|
291
|
-
audio?: number | string;
|
|
292
|
-
request?: number | string;
|
|
293
|
-
};
|
|
214
|
+
include?: boolean;
|
|
294
215
|
};
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
216
|
+
/**
|
|
217
|
+
* OpenRouter plugins to enable.
|
|
218
|
+
*/
|
|
219
|
+
plugins?: OpenRouterPluginConfig[];
|
|
220
|
+
/**
|
|
221
|
+
* Message transforms to apply.
|
|
222
|
+
*/
|
|
223
|
+
transforms?: string[];
|
|
224
|
+
/**
|
|
225
|
+
* Fallback model IDs.
|
|
226
|
+
*/
|
|
298
227
|
models?: string[];
|
|
299
228
|
/**
|
|
300
|
-
*
|
|
301
|
-
* One of `max_tokens` or `effort` is required.
|
|
302
|
-
* If `exclude` is true, reasoning will be removed from the response. Default is false.
|
|
229
|
+
* Routing strategy.
|
|
303
230
|
*/
|
|
304
|
-
|
|
305
|
-
enabled?: boolean;
|
|
306
|
-
exclude?: boolean;
|
|
307
|
-
} & ({
|
|
308
|
-
max_tokens: number;
|
|
309
|
-
} | {
|
|
310
|
-
effort: 'high' | 'medium' | 'low';
|
|
311
|
-
});
|
|
231
|
+
route?: string;
|
|
312
232
|
/**
|
|
313
|
-
*
|
|
314
|
-
* help OpenRouter to monitor and detect abuse.
|
|
233
|
+
* Provider routing configuration.
|
|
315
234
|
*/
|
|
316
|
-
|
|
317
|
-
};
|
|
318
|
-
type OpenRouterSharedSettings = OpenRouterProviderOptions & {
|
|
235
|
+
provider?: OpenRouterProviderRoutingConfig;
|
|
319
236
|
/**
|
|
320
|
-
*
|
|
237
|
+
* How to handle system messages for reasoning models.
|
|
238
|
+
* - 'system': Standard system message (default)
|
|
239
|
+
* - 'developer': Convert system to developer role for reasoning models
|
|
240
|
+
* - 'remove': Strip system messages entirely
|
|
321
241
|
*/
|
|
322
|
-
|
|
323
|
-
extraBody?: Record<string, unknown>;
|
|
242
|
+
systemMessageMode?: 'system' | 'developer' | 'remove';
|
|
324
243
|
/**
|
|
325
|
-
*
|
|
326
|
-
*
|
|
244
|
+
* Allow any additional model-specific options.
|
|
245
|
+
* These are passed through to the API.
|
|
327
246
|
*/
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
*/
|
|
332
|
-
include: boolean;
|
|
333
|
-
};
|
|
334
|
-
};
|
|
247
|
+
[key: string]: unknown;
|
|
248
|
+
}
|
|
249
|
+
|
|
335
250
|
/**
|
|
336
|
-
*
|
|
337
|
-
* @see https://openrouter.ai/docs/use-cases/usage-accounting
|
|
251
|
+
* Result of parsing provider options, including any warnings.
|
|
338
252
|
*/
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
upstreamInferenceCost: number;
|
|
352
|
-
};
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
type OpenRouterChatConfig = {
|
|
356
|
-
provider: string;
|
|
357
|
-
compatibility: 'strict' | 'compatible';
|
|
358
|
-
headers: () => Record<string, string | undefined>;
|
|
359
|
-
url: (options: {
|
|
360
|
-
modelId: string;
|
|
361
|
-
path: string;
|
|
362
|
-
}) => string;
|
|
363
|
-
fetch?: typeof fetch;
|
|
364
|
-
extraBody?: Record<string, unknown>;
|
|
365
|
-
};
|
|
366
|
-
declare class OpenRouterChatLanguageModel implements LanguageModelV2 {
|
|
367
|
-
readonly specificationVersion: "v2";
|
|
368
|
-
readonly provider = "openrouter";
|
|
369
|
-
readonly defaultObjectGenerationMode: "tool";
|
|
370
|
-
readonly modelId: OpenRouterChatModelId;
|
|
371
|
-
readonly supportsImageUrls = true;
|
|
372
|
-
readonly supportedUrls: Record<string, RegExp[]>;
|
|
373
|
-
readonly settings: OpenRouterChatSettings;
|
|
374
|
-
private readonly config;
|
|
375
|
-
constructor(modelId: OpenRouterChatModelId, settings: OpenRouterChatSettings, config: OpenRouterChatConfig);
|
|
376
|
-
private getArgs;
|
|
377
|
-
doGenerate(options: LanguageModelV2CallOptions): Promise<{
|
|
378
|
-
content: Array<LanguageModelV2Content>;
|
|
379
|
-
finishReason: LanguageModelV2FinishReason;
|
|
380
|
-
usage: LanguageModelV2Usage;
|
|
381
|
-
warnings: Array<LanguageModelV2CallWarning>;
|
|
382
|
-
providerMetadata?: {
|
|
383
|
-
openrouter: {
|
|
384
|
-
provider: string;
|
|
385
|
-
reasoning_details?: ReasoningDetailUnion[];
|
|
386
|
-
usage: OpenRouterUsageAccounting;
|
|
387
|
-
};
|
|
388
|
-
};
|
|
389
|
-
request?: {
|
|
390
|
-
body?: unknown;
|
|
391
|
-
};
|
|
392
|
-
response?: LanguageModelV2ResponseMetadata & {
|
|
393
|
-
headers?: SharedV2Headers;
|
|
394
|
-
body?: unknown;
|
|
395
|
-
};
|
|
396
|
-
}>;
|
|
397
|
-
doStream(options: LanguageModelV2CallOptions): Promise<{
|
|
398
|
-
stream: ReadableStream<LanguageModelV2StreamPart>;
|
|
399
|
-
warnings: Array<LanguageModelV2CallWarning>;
|
|
400
|
-
request?: {
|
|
401
|
-
body?: unknown;
|
|
402
|
-
};
|
|
403
|
-
response?: LanguageModelV2ResponseMetadata & {
|
|
404
|
-
headers?: SharedV2Headers;
|
|
405
|
-
body?: unknown;
|
|
406
|
-
};
|
|
253
|
+
interface ParsedProviderOptions {
|
|
254
|
+
/**
|
|
255
|
+
* The merged options.
|
|
256
|
+
*/
|
|
257
|
+
options: OpenRouterModelOptions;
|
|
258
|
+
/**
|
|
259
|
+
* Warnings generated during parsing (e.g., unknown keys).
|
|
260
|
+
*/
|
|
261
|
+
warnings: Array<{
|
|
262
|
+
type: 'unsupported';
|
|
263
|
+
feature: string;
|
|
264
|
+
details?: string;
|
|
407
265
|
}>;
|
|
408
266
|
}
|
|
267
|
+
/**
|
|
268
|
+
* Parses and merges provider options from model-level and call-time sources.
|
|
269
|
+
*
|
|
270
|
+
* Call-time options override model-level options for the same keys.
|
|
271
|
+
* Unknown keys pass through (per design spec - they are passed to the API).
|
|
272
|
+
*
|
|
273
|
+
* @param modelOptions - Options set at model creation time.
|
|
274
|
+
* @param callOptions - Options set at call time via providerOptions.openrouter.
|
|
275
|
+
* @returns The merged options and any warnings.
|
|
276
|
+
*/
|
|
277
|
+
declare function parseOpenRouterOptions(modelOptions: OpenRouterModelOptions | undefined, callOptions: Record<string, unknown> | undefined): ParsedProviderOptions;
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Maps OpenRouter finish reasons to the AI SDK V3 unified format.
|
|
281
|
+
*
|
|
282
|
+
* Mapping table:
|
|
283
|
+
* - 'end_turn', 'stop', 'stop_sequence' -> 'stop'
|
|
284
|
+
* - 'max_tokens', 'length' -> 'length'
|
|
285
|
+
* - 'tool_use', 'tool_calls' -> 'tool-calls'
|
|
286
|
+
* - 'content_filter' -> 'content-filter'
|
|
287
|
+
* - 'error' -> 'error'
|
|
288
|
+
* - null/undefined/unknown -> 'other'
|
|
289
|
+
*
|
|
290
|
+
* @param finishReason - The finish reason from OpenRouter API response
|
|
291
|
+
* @returns Object containing the unified finish reason and raw original value
|
|
292
|
+
*/
|
|
293
|
+
declare function mapOpenRouterFinishReason(finishReason: string | null | undefined): LanguageModelV3FinishReason;
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* OpenRouter Responses API input item types.
|
|
297
|
+
* These match the OpenResponsesEasyInputMessage format used by the Responses API.
|
|
298
|
+
*/
|
|
299
|
+
type OpenRouterInputItem = OpenRouterEasyInputMessage | OpenRouterFunctionCall | OpenRouterFunctionCallOutput;
|
|
300
|
+
/**
|
|
301
|
+
* Reasoning object for assistant messages.
|
|
302
|
+
* Contains the reasoning details to send back to the API.
|
|
303
|
+
*/
|
|
304
|
+
interface OpenRouterReasoning {
|
|
305
|
+
text?: string;
|
|
306
|
+
summary?: string;
|
|
307
|
+
encrypted?: string;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Easy input message format for Responses API.
|
|
311
|
+
* Supports user, system, assistant, and developer roles.
|
|
312
|
+
*/
|
|
313
|
+
interface OpenRouterEasyInputMessage {
|
|
314
|
+
type?: 'message';
|
|
315
|
+
role: 'user' | 'system' | 'assistant' | 'developer';
|
|
316
|
+
content: string | OpenRouterInputContent[];
|
|
317
|
+
reasoning?: OpenRouterReasoning;
|
|
318
|
+
}
|
|
319
|
+
interface OpenRouterFunctionCall {
|
|
320
|
+
type: 'function_call';
|
|
321
|
+
callId: string;
|
|
322
|
+
name: string;
|
|
323
|
+
arguments: string;
|
|
324
|
+
}
|
|
325
|
+
interface OpenRouterFunctionCallOutput {
|
|
326
|
+
type: 'function_call_output';
|
|
327
|
+
callId: string;
|
|
328
|
+
output: string;
|
|
329
|
+
status: 'completed' | 'incomplete';
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Input content types for Responses API.
|
|
333
|
+
* Uses 'input_text', 'input_image', 'input_file' type prefixes.
|
|
334
|
+
*/
|
|
335
|
+
type OpenRouterInputContent = {
|
|
336
|
+
type: 'input_text';
|
|
337
|
+
text: string;
|
|
338
|
+
} | {
|
|
339
|
+
type: 'input_image';
|
|
340
|
+
imageUrl: string;
|
|
341
|
+
detail?: 'auto' | 'low' | 'high';
|
|
342
|
+
} | {
|
|
343
|
+
type: 'input_file';
|
|
344
|
+
fileUrl: string;
|
|
345
|
+
filename?: string;
|
|
346
|
+
};
|
|
347
|
+
/**
|
|
348
|
+
* Converts AI SDK V3 prompt format to OpenRouter Responses API input format.
|
|
349
|
+
*
|
|
350
|
+
* Mapping table:
|
|
351
|
+
* - system { content } -> { role: 'system', content: string }
|
|
352
|
+
* - user text -> { role: 'user', content: [{ type: 'input_text', text }] }
|
|
353
|
+
* - user image -> { role: 'user', content: [{ type: 'input_image', imageUrl }] }
|
|
354
|
+
* - user file -> { role: 'user', content: [{ type: 'input_file', fileUrl }] }
|
|
355
|
+
* - assistant text -> { role: 'assistant', content: string }
|
|
356
|
+
* - assistant tool-call -> { type: 'function_call', ... }
|
|
357
|
+
* - tool result -> { type: 'function_call_output', ... }
|
|
358
|
+
*
|
|
359
|
+
* @param prompt - The AI SDK V3 prompt
|
|
360
|
+
* @returns Array of input items in OpenRouter Responses API format
|
|
361
|
+
*/
|
|
362
|
+
declare function convertToOpenRouterMessages(prompt: LanguageModelV3Prompt): OpenRouterInputItem[];
|
|
409
363
|
|
|
410
|
-
export {
|
|
364
|
+
export { type OpenRouterProviderMetadata, type OpenRouterRawUsage, type OpenRouterResponseData, type ParsedProviderOptions, buildProviderMetadata, buildUsage, convertToOpenRouterMessages, mapOpenRouterFinishReason, parseOpenRouterOptions };
|