@openrouter/ai-sdk-provider 1.3.0 → 1.4.1
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 +58 -0
- package/dist/index.d.mts +128 -2
- package/dist/index.d.ts +128 -2
- package/dist/index.js +150 -33
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +150 -33
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +186 -119
- package/dist/internal/index.d.ts +186 -119
- package/dist/internal/index.js +46 -28
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +46 -28
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1014,9 +1014,23 @@ var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
1014
1014
|
|
|
1015
1015
|
// src/schemas/provider-metadata.ts
|
|
1016
1016
|
var import_v43 = require("zod/v4");
|
|
1017
|
+
var FileAnnotationSchema = import_v43.z.object({
|
|
1018
|
+
type: import_v43.z.literal("file"),
|
|
1019
|
+
file: import_v43.z.object({
|
|
1020
|
+
hash: import_v43.z.string(),
|
|
1021
|
+
name: import_v43.z.string(),
|
|
1022
|
+
content: import_v43.z.array(
|
|
1023
|
+
import_v43.z.object({
|
|
1024
|
+
type: import_v43.z.string(),
|
|
1025
|
+
text: import_v43.z.string().optional()
|
|
1026
|
+
}).passthrough()
|
|
1027
|
+
).optional()
|
|
1028
|
+
}).passthrough()
|
|
1029
|
+
});
|
|
1017
1030
|
var OpenRouterProviderMetadataSchema = import_v43.z.object({
|
|
1018
1031
|
provider: import_v43.z.string(),
|
|
1019
1032
|
reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional(),
|
|
1033
|
+
annotations: import_v43.z.array(FileAnnotationSchema).optional(),
|
|
1020
1034
|
usage: import_v43.z.object({
|
|
1021
1035
|
promptTokens: import_v43.z.number(),
|
|
1022
1036
|
promptTokensDetails: import_v43.z.object({
|
|
@@ -1035,7 +1049,8 @@ var OpenRouterProviderMetadataSchema = import_v43.z.object({
|
|
|
1035
1049
|
}).passthrough();
|
|
1036
1050
|
var OpenRouterProviderOptionsSchema = import_v43.z.object({
|
|
1037
1051
|
openrouter: import_v43.z.object({
|
|
1038
|
-
reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional()
|
|
1052
|
+
reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional(),
|
|
1053
|
+
annotations: import_v43.z.array(FileAnnotationSchema).optional()
|
|
1039
1054
|
}).optional()
|
|
1040
1055
|
}).optional();
|
|
1041
1056
|
|
|
@@ -1149,7 +1164,7 @@ function getCacheControl(providerMetadata) {
|
|
|
1149
1164
|
return (_c = (_b = (_a15 = openrouter2 == null ? void 0 : openrouter2.cacheControl) != null ? _a15 : openrouter2 == null ? void 0 : openrouter2.cache_control) != null ? _b : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
|
|
1150
1165
|
}
|
|
1151
1166
|
function convertToOpenRouterChatMessages(prompt) {
|
|
1152
|
-
var _a15, _b, _c, _d, _e, _f, _g, _h;
|
|
1167
|
+
var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
1153
1168
|
const messages = [];
|
|
1154
1169
|
for (const { role, content, providerOptions } of prompt) {
|
|
1155
1170
|
switch (role) {
|
|
@@ -1303,6 +1318,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1303
1318
|
}
|
|
1304
1319
|
const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
|
|
1305
1320
|
const messageReasoningDetails = parsedProviderOptions.success ? (_g = (_f = parsedProviderOptions.data) == null ? void 0 : _f.openrouter) == null ? void 0 : _g.reasoning_details : void 0;
|
|
1321
|
+
const messageAnnotations = parsedProviderOptions.success ? (_i = (_h = parsedProviderOptions.data) == null ? void 0 : _h.openrouter) == null ? void 0 : _i.annotations : void 0;
|
|
1306
1322
|
const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : accumulatedReasoningDetails.length > 0 ? accumulatedReasoningDetails : void 0;
|
|
1307
1323
|
messages.push({
|
|
1308
1324
|
role: "assistant",
|
|
@@ -1310,6 +1326,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1310
1326
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1311
1327
|
reasoning: reasoning || void 0,
|
|
1312
1328
|
reasoning_details: finalReasoningDetails,
|
|
1329
|
+
annotations: messageAnnotations,
|
|
1313
1330
|
cache_control: getCacheControl(providerOptions)
|
|
1314
1331
|
});
|
|
1315
1332
|
break;
|
|
@@ -1321,7 +1338,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1321
1338
|
role: "tool",
|
|
1322
1339
|
tool_call_id: toolResponse.toolCallId,
|
|
1323
1340
|
content: content2,
|
|
1324
|
-
cache_control: (
|
|
1341
|
+
cache_control: (_j = getCacheControl(providerOptions)) != null ? _j : getCacheControl(toolResponse.providerOptions)
|
|
1325
1342
|
});
|
|
1326
1343
|
}
|
|
1327
1344
|
break;
|
|
@@ -1580,6 +1597,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1580
1597
|
this.specificationVersion = "v2";
|
|
1581
1598
|
this.provider = "openrouter";
|
|
1582
1599
|
this.defaultObjectGenerationMode = "tool";
|
|
1600
|
+
this.supportsImageUrls = true;
|
|
1583
1601
|
this.supportedUrls = {
|
|
1584
1602
|
"image/*": [
|
|
1585
1603
|
/^data:image\/[a-zA-Z]+;base64,/,
|
|
@@ -1625,7 +1643,16 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1625
1643
|
presence_penalty: presencePenalty,
|
|
1626
1644
|
seed,
|
|
1627
1645
|
stop: stopSequences,
|
|
1628
|
-
response_format: responseFormat
|
|
1646
|
+
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? responseFormat.schema != null ? {
|
|
1647
|
+
type: "json_schema",
|
|
1648
|
+
json_schema: __spreadValues({
|
|
1649
|
+
schema: responseFormat.schema,
|
|
1650
|
+
strict: true,
|
|
1651
|
+
name: (_a15 = responseFormat.name) != null ? _a15 : "response"
|
|
1652
|
+
}, responseFormat.description && {
|
|
1653
|
+
description: responseFormat.description
|
|
1654
|
+
})
|
|
1655
|
+
} : { type: "json_object" } : void 0,
|
|
1629
1656
|
top_k: topK,
|
|
1630
1657
|
// messages:
|
|
1631
1658
|
messages: convertToOpenRouterChatMessages(prompt),
|
|
@@ -1641,20 +1668,6 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1641
1668
|
// Debug settings:
|
|
1642
1669
|
debug: this.settings.debug
|
|
1643
1670
|
}, this.config.extraBody), this.settings.extraBody);
|
|
1644
|
-
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null) {
|
|
1645
|
-
return __spreadProps(__spreadValues({}, baseArgs), {
|
|
1646
|
-
response_format: {
|
|
1647
|
-
type: "json_schema",
|
|
1648
|
-
json_schema: __spreadValues({
|
|
1649
|
-
schema: responseFormat.schema,
|
|
1650
|
-
strict: true,
|
|
1651
|
-
name: (_a15 = responseFormat.name) != null ? _a15 : "response"
|
|
1652
|
-
}, responseFormat.description && {
|
|
1653
|
-
description: responseFormat.description
|
|
1654
|
-
})
|
|
1655
|
-
}
|
|
1656
|
-
});
|
|
1657
|
-
}
|
|
1658
1671
|
if (tools && tools.length > 0) {
|
|
1659
1672
|
const mappedTools = tools.filter(
|
|
1660
1673
|
(tool) => tool.type === "function"
|
|
@@ -1674,7 +1687,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1674
1687
|
return baseArgs;
|
|
1675
1688
|
}
|
|
1676
1689
|
async doGenerate(options) {
|
|
1677
|
-
var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
1690
|
+
var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
|
|
1678
1691
|
const providerOptions = options.providerOptions || {};
|
|
1679
1692
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1680
1693
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -1832,6 +1845,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1832
1845
|
}
|
|
1833
1846
|
}
|
|
1834
1847
|
}
|
|
1848
|
+
const fileAnnotations = (_k = choice.message.annotations) == null ? void 0 : _k.filter(
|
|
1849
|
+
(a) => a.type === "file"
|
|
1850
|
+
);
|
|
1835
1851
|
return {
|
|
1836
1852
|
content,
|
|
1837
1853
|
finishReason: mapOpenRouterFinishReason(choice.finish_reason),
|
|
@@ -1839,22 +1855,23 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1839
1855
|
warnings: [],
|
|
1840
1856
|
providerMetadata: {
|
|
1841
1857
|
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
1842
|
-
provider: (
|
|
1843
|
-
reasoning_details: (
|
|
1858
|
+
provider: (_l = response.provider) != null ? _l : "",
|
|
1859
|
+
reasoning_details: (_m = choice.message.reasoning_details) != null ? _m : [],
|
|
1860
|
+
annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
|
|
1844
1861
|
usage: __spreadValues(__spreadValues(__spreadValues({
|
|
1845
|
-
promptTokens: (
|
|
1846
|
-
completionTokens: (
|
|
1847
|
-
totalTokens: (
|
|
1848
|
-
cost: (
|
|
1849
|
-
}, ((
|
|
1862
|
+
promptTokens: (_n = usageInfo.inputTokens) != null ? _n : 0,
|
|
1863
|
+
completionTokens: (_o = usageInfo.outputTokens) != null ? _o : 0,
|
|
1864
|
+
totalTokens: (_p = usageInfo.totalTokens) != null ? _p : 0,
|
|
1865
|
+
cost: (_q = response.usage) == null ? void 0 : _q.cost
|
|
1866
|
+
}, ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? {
|
|
1850
1867
|
promptTokensDetails: {
|
|
1851
1868
|
cachedTokens: response.usage.prompt_tokens_details.cached_tokens
|
|
1852
1869
|
}
|
|
1853
|
-
} : {}), ((
|
|
1870
|
+
} : {}), ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? {
|
|
1854
1871
|
completionTokensDetails: {
|
|
1855
1872
|
reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
|
|
1856
1873
|
}
|
|
1857
|
-
} : {}), ((
|
|
1874
|
+
} : {}), ((_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? {
|
|
1858
1875
|
costDetails: {
|
|
1859
1876
|
upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
|
|
1860
1877
|
}
|
|
@@ -2416,6 +2433,7 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
2416
2433
|
constructor(modelId, settings, config) {
|
|
2417
2434
|
this.specificationVersion = "v2";
|
|
2418
2435
|
this.provider = "openrouter";
|
|
2436
|
+
this.supportsImageUrls = true;
|
|
2419
2437
|
this.supportedUrls = {
|
|
2420
2438
|
"image/*": [
|
|
2421
2439
|
/^data:image\/[a-zA-Z]+;base64,/,
|
|
@@ -2652,6 +2670,78 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
2652
2670
|
}
|
|
2653
2671
|
};
|
|
2654
2672
|
|
|
2673
|
+
// src/embedding/schemas.ts
|
|
2674
|
+
var import_v48 = require("zod/v4");
|
|
2675
|
+
var openrouterEmbeddingUsageSchema = import_v48.z.object({
|
|
2676
|
+
prompt_tokens: import_v48.z.number(),
|
|
2677
|
+
total_tokens: import_v48.z.number(),
|
|
2678
|
+
cost: import_v48.z.number().optional()
|
|
2679
|
+
});
|
|
2680
|
+
var openrouterEmbeddingDataSchema = import_v48.z.object({
|
|
2681
|
+
object: import_v48.z.literal("embedding"),
|
|
2682
|
+
embedding: import_v48.z.array(import_v48.z.number()),
|
|
2683
|
+
index: import_v48.z.number().optional()
|
|
2684
|
+
});
|
|
2685
|
+
var OpenRouterEmbeddingResponseSchema = import_v48.z.object({
|
|
2686
|
+
id: import_v48.z.string().optional(),
|
|
2687
|
+
object: import_v48.z.literal("list"),
|
|
2688
|
+
data: import_v48.z.array(openrouterEmbeddingDataSchema),
|
|
2689
|
+
model: import_v48.z.string(),
|
|
2690
|
+
usage: openrouterEmbeddingUsageSchema.optional()
|
|
2691
|
+
});
|
|
2692
|
+
|
|
2693
|
+
// src/embedding/index.ts
|
|
2694
|
+
var OpenRouterEmbeddingModel = class {
|
|
2695
|
+
constructor(modelId, settings, config) {
|
|
2696
|
+
this.specificationVersion = "v2";
|
|
2697
|
+
this.provider = "openrouter";
|
|
2698
|
+
this.maxEmbeddingsPerCall = void 0;
|
|
2699
|
+
this.supportsParallelCalls = true;
|
|
2700
|
+
this.modelId = modelId;
|
|
2701
|
+
this.settings = settings;
|
|
2702
|
+
this.config = config;
|
|
2703
|
+
}
|
|
2704
|
+
async doEmbed(options) {
|
|
2705
|
+
var _a15;
|
|
2706
|
+
const { values, abortSignal, headers } = options;
|
|
2707
|
+
const args = __spreadValues(__spreadValues({
|
|
2708
|
+
model: this.modelId,
|
|
2709
|
+
input: values,
|
|
2710
|
+
user: this.settings.user,
|
|
2711
|
+
provider: this.settings.provider
|
|
2712
|
+
}, this.config.extraBody), this.settings.extraBody);
|
|
2713
|
+
const { value: responseValue, responseHeaders } = await postJsonToApi({
|
|
2714
|
+
url: this.config.url({
|
|
2715
|
+
path: "/embeddings",
|
|
2716
|
+
modelId: this.modelId
|
|
2717
|
+
}),
|
|
2718
|
+
headers: combineHeaders(this.config.headers(), headers),
|
|
2719
|
+
body: args,
|
|
2720
|
+
failedResponseHandler: openrouterFailedResponseHandler,
|
|
2721
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
2722
|
+
OpenRouterEmbeddingResponseSchema
|
|
2723
|
+
),
|
|
2724
|
+
abortSignal,
|
|
2725
|
+
fetch: this.config.fetch
|
|
2726
|
+
});
|
|
2727
|
+
return {
|
|
2728
|
+
embeddings: responseValue.data.map((item) => item.embedding),
|
|
2729
|
+
usage: responseValue.usage ? { tokens: responseValue.usage.prompt_tokens } : void 0,
|
|
2730
|
+
providerMetadata: ((_a15 = responseValue.usage) == null ? void 0 : _a15.cost) ? {
|
|
2731
|
+
openrouter: {
|
|
2732
|
+
usage: {
|
|
2733
|
+
cost: responseValue.usage.cost
|
|
2734
|
+
}
|
|
2735
|
+
}
|
|
2736
|
+
} : void 0,
|
|
2737
|
+
response: {
|
|
2738
|
+
headers: responseHeaders,
|
|
2739
|
+
body: responseValue
|
|
2740
|
+
}
|
|
2741
|
+
};
|
|
2742
|
+
}
|
|
2743
|
+
};
|
|
2744
|
+
|
|
2655
2745
|
// src/facade.ts
|
|
2656
2746
|
var OpenRouter = class {
|
|
2657
2747
|
/**
|
|
@@ -2662,17 +2752,20 @@ var OpenRouter = class {
|
|
|
2662
2752
|
this.baseURL = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
|
|
2663
2753
|
this.apiKey = options.apiKey;
|
|
2664
2754
|
this.headers = options.headers;
|
|
2755
|
+
this.api_keys = options.api_keys;
|
|
2665
2756
|
}
|
|
2666
2757
|
get baseConfig() {
|
|
2667
2758
|
return {
|
|
2668
2759
|
baseURL: this.baseURL,
|
|
2669
|
-
headers: () => __spreadValues({
|
|
2760
|
+
headers: () => __spreadValues(__spreadValues({
|
|
2670
2761
|
Authorization: `Bearer ${loadApiKey({
|
|
2671
2762
|
apiKey: this.apiKey,
|
|
2672
2763
|
environmentVariableName: "OPENROUTER_API_KEY",
|
|
2673
2764
|
description: "OpenRouter"
|
|
2674
2765
|
})}`
|
|
2675
|
-
}, this.headers)
|
|
2766
|
+
}, this.headers), this.api_keys && Object.keys(this.api_keys).length > 0 && {
|
|
2767
|
+
"X-Provider-API-Keys": JSON.stringify(this.api_keys)
|
|
2768
|
+
})
|
|
2676
2769
|
};
|
|
2677
2770
|
}
|
|
2678
2771
|
chat(modelId, settings = {}) {
|
|
@@ -2691,6 +2784,19 @@ var OpenRouter = class {
|
|
|
2691
2784
|
url: ({ path }) => `${this.baseURL}${path}`
|
|
2692
2785
|
}));
|
|
2693
2786
|
}
|
|
2787
|
+
textEmbeddingModel(modelId, settings = {}) {
|
|
2788
|
+
return new OpenRouterEmbeddingModel(modelId, settings, __spreadProps(__spreadValues({
|
|
2789
|
+
provider: "openrouter.embedding"
|
|
2790
|
+
}, this.baseConfig), {
|
|
2791
|
+
url: ({ path }) => `${this.baseURL}${path}`
|
|
2792
|
+
}));
|
|
2793
|
+
}
|
|
2794
|
+
/**
|
|
2795
|
+
* @deprecated Use textEmbeddingModel instead
|
|
2796
|
+
*/
|
|
2797
|
+
embedding(modelId, settings = {}) {
|
|
2798
|
+
return this.textEmbeddingModel(modelId, settings);
|
|
2799
|
+
}
|
|
2694
2800
|
};
|
|
2695
2801
|
|
|
2696
2802
|
// src/utils/remove-undefined.ts
|
|
@@ -2713,7 +2819,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
|
2713
2819
|
}
|
|
2714
2820
|
|
|
2715
2821
|
// src/version.ts
|
|
2716
|
-
var VERSION = false ? "0.0.0-test" : "1.
|
|
2822
|
+
var VERSION = false ? "0.0.0-test" : "1.4.1";
|
|
2717
2823
|
|
|
2718
2824
|
// src/provider.ts
|
|
2719
2825
|
function createOpenRouter(options = {}) {
|
|
@@ -2721,13 +2827,15 @@ function createOpenRouter(options = {}) {
|
|
|
2721
2827
|
const baseURL = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
|
|
2722
2828
|
const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
|
|
2723
2829
|
const getHeaders = () => withUserAgentSuffix(
|
|
2724
|
-
__spreadValues({
|
|
2830
|
+
__spreadValues(__spreadValues({
|
|
2725
2831
|
Authorization: `Bearer ${loadApiKey({
|
|
2726
2832
|
apiKey: options.apiKey,
|
|
2727
2833
|
environmentVariableName: "OPENROUTER_API_KEY",
|
|
2728
2834
|
description: "OpenRouter"
|
|
2729
2835
|
})}`
|
|
2730
|
-
}, options.headers),
|
|
2836
|
+
}, options.headers), options.api_keys && Object.keys(options.api_keys).length > 0 && {
|
|
2837
|
+
"X-Provider-API-Keys": JSON.stringify(options.api_keys)
|
|
2838
|
+
}),
|
|
2731
2839
|
`ai-sdk/openrouter/${VERSION}`
|
|
2732
2840
|
);
|
|
2733
2841
|
const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
|
|
@@ -2746,6 +2854,13 @@ function createOpenRouter(options = {}) {
|
|
|
2746
2854
|
fetch: options.fetch,
|
|
2747
2855
|
extraBody: options.extraBody
|
|
2748
2856
|
});
|
|
2857
|
+
const createEmbeddingModel = (modelId, settings = {}) => new OpenRouterEmbeddingModel(modelId, settings, {
|
|
2858
|
+
provider: "openrouter.embedding",
|
|
2859
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
2860
|
+
headers: getHeaders,
|
|
2861
|
+
fetch: options.fetch,
|
|
2862
|
+
extraBody: options.extraBody
|
|
2863
|
+
});
|
|
2749
2864
|
const createLanguageModel = (modelId, settings) => {
|
|
2750
2865
|
if (new.target) {
|
|
2751
2866
|
throw new Error(
|
|
@@ -2764,6 +2879,8 @@ function createOpenRouter(options = {}) {
|
|
|
2764
2879
|
provider.languageModel = createLanguageModel;
|
|
2765
2880
|
provider.chat = createChatModel;
|
|
2766
2881
|
provider.completion = createCompletionModel;
|
|
2882
|
+
provider.textEmbeddingModel = createEmbeddingModel;
|
|
2883
|
+
provider.embedding = createEmbeddingModel;
|
|
2767
2884
|
return provider;
|
|
2768
2885
|
}
|
|
2769
2886
|
var openrouter = createOpenRouter({
|