@openrouter/ai-sdk-provider 2.2.5 → 2.3.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 +19 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +30 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +30 -26
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +19 -0
- package/dist/internal/index.d.ts +19 -0
- package/dist/internal/index.js +29 -25
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +29 -25
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -151,6 +151,25 @@ type OpenRouterChatSettings = {
|
|
|
151
151
|
*/
|
|
152
152
|
engine?: Engine;
|
|
153
153
|
};
|
|
154
|
+
/**
|
|
155
|
+
* Enable Anthropic automatic prompt caching by setting a top-level cache_control
|
|
156
|
+
* directive on the request body. When set to `{ type: 'ephemeral' }`, Anthropic
|
|
157
|
+
* will automatically cache eligible content in your prompts.
|
|
158
|
+
*
|
|
159
|
+
* Only works with Anthropic models through OpenRouter.
|
|
160
|
+
*
|
|
161
|
+
* @see https://platform.claude.com/docs/en/build-with-claude/prompt-caching#automatic-caching
|
|
162
|
+
* @see https://openrouter.ai/docs
|
|
163
|
+
*/
|
|
164
|
+
cache_control?: {
|
|
165
|
+
type: 'ephemeral';
|
|
166
|
+
/**
|
|
167
|
+
* Optional time-to-live for the cache entry.
|
|
168
|
+
* - `'5m'` — 5 minutes (default when omitted)
|
|
169
|
+
* - `'1h'` — 1 hour
|
|
170
|
+
*/
|
|
171
|
+
ttl?: '5m' | '1h';
|
|
172
|
+
};
|
|
154
173
|
/**
|
|
155
174
|
* Debug options for troubleshooting API requests.
|
|
156
175
|
* Only works with streaming requests.
|
package/dist/index.d.ts
CHANGED
|
@@ -151,6 +151,25 @@ type OpenRouterChatSettings = {
|
|
|
151
151
|
*/
|
|
152
152
|
engine?: Engine;
|
|
153
153
|
};
|
|
154
|
+
/**
|
|
155
|
+
* Enable Anthropic automatic prompt caching by setting a top-level cache_control
|
|
156
|
+
* directive on the request body. When set to `{ type: 'ephemeral' }`, Anthropic
|
|
157
|
+
* will automatically cache eligible content in your prompts.
|
|
158
|
+
*
|
|
159
|
+
* Only works with Anthropic models through OpenRouter.
|
|
160
|
+
*
|
|
161
|
+
* @see https://platform.claude.com/docs/en/build-with-claude/prompt-caching#automatic-caching
|
|
162
|
+
* @see https://openrouter.ai/docs
|
|
163
|
+
*/
|
|
164
|
+
cache_control?: {
|
|
165
|
+
type: 'ephemeral';
|
|
166
|
+
/**
|
|
167
|
+
* Optional time-to-live for the cache entry.
|
|
168
|
+
* - `'5m'` — 5 minutes (default when omitted)
|
|
169
|
+
* - `'1h'` — 1 hour
|
|
170
|
+
*/
|
|
171
|
+
ttl?: '5m' | '1h';
|
|
172
|
+
};
|
|
154
173
|
/**
|
|
155
174
|
* Debug options for troubleshooting API requests.
|
|
156
175
|
* Only works with streaming requests.
|
package/dist/index.js
CHANGED
|
@@ -3179,7 +3179,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3179
3179
|
// Provider routing settings:
|
|
3180
3180
|
provider: this.settings.provider,
|
|
3181
3181
|
// Debug settings:
|
|
3182
|
-
debug: this.settings.debug
|
|
3182
|
+
debug: this.settings.debug,
|
|
3183
|
+
// Anthropic automatic caching:
|
|
3184
|
+
cache_control: this.settings.cache_control
|
|
3183
3185
|
}, this.config.extraBody), this.settings.extraBody);
|
|
3184
3186
|
if (tools && tools.length > 0) {
|
|
3185
3187
|
const mappedTools = tools.filter(
|
|
@@ -3200,10 +3202,11 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3200
3202
|
return baseArgs;
|
|
3201
3203
|
}
|
|
3202
3204
|
async doGenerate(options) {
|
|
3203
|
-
var
|
|
3205
|
+
var _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
|
|
3204
3206
|
const providerOptions = options.providerOptions || {};
|
|
3205
3207
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
3206
|
-
const
|
|
3208
|
+
const _a16 = openrouterOptions, { cacheControl } = _a16, restOpenrouterOptions = __objRest(_a16, ["cacheControl"]);
|
|
3209
|
+
const args = __spreadValues(__spreadValues(__spreadValues({}, this.getArgs(options)), restOpenrouterOptions), cacheControl != null && !("cache_control" in restOpenrouterOptions) ? { cache_control: cacheControl } : {});
|
|
3207
3210
|
const { value: responseValue, responseHeaders } = await postJsonToApi({
|
|
3208
3211
|
url: this.config.url({
|
|
3209
3212
|
path: "/chat/completions",
|
|
@@ -3240,7 +3243,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3240
3243
|
});
|
|
3241
3244
|
}
|
|
3242
3245
|
const usageInfo = response.usage ? computeTokenUsage(response.usage) : emptyUsage();
|
|
3243
|
-
const reasoningDetails = (
|
|
3246
|
+
const reasoningDetails = (_b16 = choice.message.reasoning_details) != null ? _b16 : [];
|
|
3244
3247
|
const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
|
|
3245
3248
|
switch (detail.type) {
|
|
3246
3249
|
case "reasoning.text" /* Text */: {
|
|
@@ -3309,9 +3312,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3309
3312
|
for (const toolCall of choice.message.tool_calls) {
|
|
3310
3313
|
content.push({
|
|
3311
3314
|
type: "tool-call",
|
|
3312
|
-
toolCallId: (
|
|
3315
|
+
toolCallId: (_c = toolCall.id) != null ? _c : generateId(),
|
|
3313
3316
|
toolName: toolCall.function.name,
|
|
3314
|
-
input: (
|
|
3317
|
+
input: (_d = toolCall.function.arguments) != null ? _d : "{}",
|
|
3315
3318
|
providerMetadata: !reasoningDetailsAttachedToToolCall ? {
|
|
3316
3319
|
openrouter: {
|
|
3317
3320
|
reasoning_details: reasoningDetails
|
|
@@ -3338,19 +3341,19 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3338
3341
|
sourceType: "url",
|
|
3339
3342
|
id: annotation.url_citation.url,
|
|
3340
3343
|
url: annotation.url_citation.url,
|
|
3341
|
-
title: (
|
|
3344
|
+
title: (_e = annotation.url_citation.title) != null ? _e : "",
|
|
3342
3345
|
providerMetadata: {
|
|
3343
3346
|
openrouter: {
|
|
3344
|
-
content: (
|
|
3345
|
-
startIndex: (
|
|
3346
|
-
endIndex: (
|
|
3347
|
+
content: (_f = annotation.url_citation.content) != null ? _f : "",
|
|
3348
|
+
startIndex: (_g = annotation.url_citation.start_index) != null ? _g : 0,
|
|
3349
|
+
endIndex: (_h = annotation.url_citation.end_index) != null ? _h : 0
|
|
3347
3350
|
}
|
|
3348
3351
|
}
|
|
3349
3352
|
});
|
|
3350
3353
|
}
|
|
3351
3354
|
}
|
|
3352
3355
|
}
|
|
3353
|
-
const fileAnnotations = (
|
|
3356
|
+
const fileAnnotations = (_i = choice.message.annotations) == null ? void 0 : _i.filter(
|
|
3354
3357
|
(a) => a.type === "file"
|
|
3355
3358
|
);
|
|
3356
3359
|
const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
|
|
@@ -3358,7 +3361,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3358
3361
|
(d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
|
|
3359
3362
|
);
|
|
3360
3363
|
const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
|
|
3361
|
-
const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (
|
|
3364
|
+
const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_j = choice.finish_reason) != null ? _j : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
|
|
3362
3365
|
return {
|
|
3363
3366
|
content,
|
|
3364
3367
|
finishReason: effectiveFinishReason,
|
|
@@ -3366,22 +3369,22 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3366
3369
|
warnings: [],
|
|
3367
3370
|
providerMetadata: {
|
|
3368
3371
|
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
3369
|
-
provider: (
|
|
3370
|
-
reasoning_details: (
|
|
3372
|
+
provider: (_k = response.provider) != null ? _k : "",
|
|
3373
|
+
reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
|
|
3371
3374
|
annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
|
|
3372
3375
|
usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
3373
|
-
promptTokens: (
|
|
3374
|
-
completionTokens: (
|
|
3375
|
-
totalTokens: ((
|
|
3376
|
-
}, ((
|
|
3376
|
+
promptTokens: (_m = usageInfo.inputTokens.total) != null ? _m : 0,
|
|
3377
|
+
completionTokens: (_n = usageInfo.outputTokens.total) != null ? _n : 0,
|
|
3378
|
+
totalTokens: ((_o = usageInfo.inputTokens.total) != null ? _o : 0) + ((_p = usageInfo.outputTokens.total) != null ? _p : 0)
|
|
3379
|
+
}, ((_q = response.usage) == null ? void 0 : _q.cost) != null ? { cost: response.usage.cost } : {}), ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? {
|
|
3377
3380
|
promptTokensDetails: {
|
|
3378
3381
|
cachedTokens: response.usage.prompt_tokens_details.cached_tokens
|
|
3379
3382
|
}
|
|
3380
|
-
} : {}), ((
|
|
3383
|
+
} : {}), ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? {
|
|
3381
3384
|
completionTokensDetails: {
|
|
3382
3385
|
reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
|
|
3383
3386
|
}
|
|
3384
|
-
} : {}), ((
|
|
3387
|
+
} : {}), ((_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? {
|
|
3385
3388
|
costDetails: {
|
|
3386
3389
|
upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
|
|
3387
3390
|
}
|
|
@@ -3397,10 +3400,11 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3397
3400
|
};
|
|
3398
3401
|
}
|
|
3399
3402
|
async doStream(options) {
|
|
3400
|
-
var
|
|
3403
|
+
var _b16;
|
|
3401
3404
|
const providerOptions = options.providerOptions || {};
|
|
3402
3405
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
3403
|
-
const
|
|
3406
|
+
const _a16 = openrouterOptions, { cacheControl } = _a16, restOpenrouterOptions = __objRest(_a16, ["cacheControl"]);
|
|
3407
|
+
const args = __spreadValues(__spreadValues(__spreadValues({}, this.getArgs(options)), restOpenrouterOptions), cacheControl != null && !("cache_control" in restOpenrouterOptions) ? { cache_control: cacheControl } : {});
|
|
3404
3408
|
const { value: response, responseHeaders } = await postJsonToApi({
|
|
3405
3409
|
url: this.config.url({
|
|
3406
3410
|
path: "/chat/completions",
|
|
@@ -3412,7 +3416,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3412
3416
|
// only include stream_options when in strict compatibility mode:
|
|
3413
3417
|
stream_options: this.config.compatibility === "strict" ? __spreadValues({
|
|
3414
3418
|
include_usage: true
|
|
3415
|
-
}, ((
|
|
3419
|
+
}, ((_b16 = this.settings.usage) == null ? void 0 : _b16.include) ? { include_usage: true } : {}) : void 0
|
|
3416
3420
|
}),
|
|
3417
3421
|
failedResponseHandler: openrouterFailedResponseHandler,
|
|
3418
3422
|
successfulResponseHandler: createEventSourceResponseHandler(
|
|
@@ -3452,7 +3456,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3452
3456
|
stream: response.pipeThrough(
|
|
3453
3457
|
new TransformStream({
|
|
3454
3458
|
transform(chunk, controller) {
|
|
3455
|
-
var _a17,
|
|
3459
|
+
var _a17, _b17, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
|
|
3456
3460
|
if (options.includeRawChunks) {
|
|
3457
3461
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
3458
3462
|
}
|
|
@@ -3489,7 +3493,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
3489
3493
|
Object.assign(usage.outputTokens, computed.outputTokens);
|
|
3490
3494
|
rawUsage = value.usage;
|
|
3491
3495
|
const promptTokens = (_a17 = value.usage.prompt_tokens) != null ? _a17 : 0;
|
|
3492
|
-
const completionTokens = (
|
|
3496
|
+
const completionTokens = (_b17 = value.usage.completion_tokens) != null ? _b17 : 0;
|
|
3493
3497
|
openrouterUsage.promptTokens = promptTokens;
|
|
3494
3498
|
if (value.usage.prompt_tokens_details) {
|
|
3495
3499
|
openrouterUsage.promptTokensDetails = {
|
|
@@ -4605,7 +4609,7 @@ function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
|
|
|
4605
4609
|
}
|
|
4606
4610
|
|
|
4607
4611
|
// src/version.ts
|
|
4608
|
-
var VERSION2 = false ? "0.0.0-test" : "2.
|
|
4612
|
+
var VERSION2 = false ? "0.0.0-test" : "2.3.0";
|
|
4609
4613
|
|
|
4610
4614
|
// src/provider.ts
|
|
4611
4615
|
function createOpenRouter(options = {}) {
|