@absolutejs/ai 0.3.2 → 0.3.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/CHANGELOG.md +12 -0
- package/README.md +19 -0
- package/changelog.json +33 -0
- package/dist/ai/client/index.js +1 -3
- package/dist/ai/client/index.js.map +2 -2
- package/dist/ai/index.js +7818 -7753
- package/dist/ai/index.js.map +20 -20
- package/dist/ai/models/index.js +1 -71
- package/dist/ai/models/index.js.map +2 -2
- package/dist/ai/providers/anthropic.js +33 -29
- package/dist/ai/providers/anthropic.js.map +3 -3
- package/dist/ai/providers/gemini.js +11 -13
- package/dist/ai/providers/gemini.js.map +2 -2
- package/dist/ai/providers/ollama.js +3 -5
- package/dist/ai/providers/ollama.js.map +2 -2
- package/dist/ai/providers/openai.js +38 -40
- package/dist/ai/providers/openai.js.map +2 -2
- package/dist/ai/providers/openaiCompatible.js +56 -58
- package/dist/ai/providers/openaiCompatible.js.map +2 -2
- package/dist/ai/providers/openaiResponses.js +18 -20
- package/dist/ai/providers/openaiResponses.js.map +2 -2
- package/dist/ai/providers/openrouter.js +130 -126
- package/dist/ai/providers/openrouter.js.map +3 -3
- package/dist/ai/providers/openrouterSDK.js +1 -3
- package/dist/ai/providers/openrouterSDK.js.map +2 -2
- package/dist/ai/research.js +2 -4
- package/dist/ai/research.js.map +2 -2
- package/dist/ai/tools/index.js +55 -58
- package/dist/ai/tools/index.js.map +5 -5
- package/dist/ai/tools/untrusted.js +1 -3
- package/dist/ai/tools/untrusted.js.map +2 -2
- package/dist/ai/ui/index.js +83 -87
- package/dist/ai/ui/index.js.map +4 -4
- package/dist/angular/ai/index.js +1 -1
- package/dist/angular/ai/index.js.map +1 -1
- package/dist/react/ai/index.js +4 -74
- package/dist/react/ai/index.js.map +2 -2
- package/dist/src/ai/ui/catalog.d.ts +3 -0
- package/dist/src/ai/ui/index.d.ts +2 -2
- package/dist/src/ai/ui/uiCards.d.ts +9 -3
- package/dist/svelte/ai/index.js +1 -71
- package/dist/svelte/ai/index.js.map +2 -2
- package/dist/vue/ai/index.js +1 -71
- package/dist/vue/ai/index.js.map +2 -2
- package/package.json +1 -1
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var __require = import.meta.require;
|
|
3
|
-
|
|
4
2
|
// src/ai/inputCapacity.ts
|
|
5
3
|
class AIInputError extends Error {
|
|
6
4
|
code;
|
|
@@ -791,13 +789,13 @@ var compatibleReasoningBody = (provider, model, reasoning) => {
|
|
|
791
789
|
var geminiThinkingConfig = (model, reasoning) => {
|
|
792
790
|
if (!reasoning)
|
|
793
791
|
return;
|
|
794
|
-
const
|
|
792
|
+
const profile = getModelReasoning("google", model);
|
|
795
793
|
const effort = resolveEffort(reasoning);
|
|
796
|
-
if (!
|
|
794
|
+
if (!profile || !effort)
|
|
797
795
|
return;
|
|
798
|
-
if (!
|
|
796
|
+
if (!profile.efforts.includes(effort))
|
|
799
797
|
throw new Error(`Unsupported reasoning effort ${effort} for ${model}`);
|
|
800
|
-
if (
|
|
798
|
+
if (profile.kind === "effort")
|
|
801
799
|
return { thinkingLevel: effort };
|
|
802
800
|
const budgets = {
|
|
803
801
|
none: 0,
|
|
@@ -1335,31 +1333,31 @@ var resolveImageModels = (imageModels) => {
|
|
|
1335
1333
|
}
|
|
1336
1334
|
return new Set(imageModels);
|
|
1337
1335
|
};
|
|
1338
|
-
var openaiResponses = (
|
|
1339
|
-
if (!
|
|
1336
|
+
var openaiResponses = (config) => {
|
|
1337
|
+
if (!config.apiKey && !config.tokenSource)
|
|
1340
1338
|
throw new Error("openaiResponses() requires either apiKey or tokenSource");
|
|
1341
|
-
const baseUrl =
|
|
1342
|
-
const fetchImpl =
|
|
1343
|
-
const imageModels = resolveImageModels(
|
|
1344
|
-
const providerName =
|
|
1345
|
-
const resolveKey = async () =>
|
|
1346
|
-
const resolveHeaders = async (params) => typeof
|
|
1339
|
+
const baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
|
|
1340
|
+
const fetchImpl = config.fetch ?? globalThis.fetch;
|
|
1341
|
+
const imageModels = resolveImageModels(config.imageModels);
|
|
1342
|
+
const providerName = config.providerName ?? "openai-responses";
|
|
1343
|
+
const resolveKey = async () => config.tokenSource ? await Promise.resolve(config.tokenSource()) : config.apiKey;
|
|
1344
|
+
const resolveHeaders = async (params) => typeof config.headers === "function" ? await config.headers(params) : config.headers ?? {};
|
|
1347
1345
|
return instrumentAIProvider({
|
|
1348
1346
|
inputCapacity: openaiInputCapacity({
|
|
1349
1347
|
baseUrl,
|
|
1350
1348
|
fetch: fetchImpl,
|
|
1351
1349
|
key: resolveKey,
|
|
1352
1350
|
headers: resolveHeaders,
|
|
1353
|
-
modelLimits:
|
|
1351
|
+
modelLimits: config.modelLimits,
|
|
1354
1352
|
body: (params) => {
|
|
1355
|
-
const built = buildResponsesRequestBody(params, imageModels.has(params.model),
|
|
1356
|
-
return
|
|
1353
|
+
const built = buildResponsesRequestBody(params, imageModels.has(params.model), config.modelForCapabilities?.(params.model) ?? params.model);
|
|
1354
|
+
return config.transformRequestBody ? config.transformRequestBody(built, params) : built;
|
|
1357
1355
|
}
|
|
1358
1356
|
}),
|
|
1359
1357
|
stream: (params) => {
|
|
1360
1358
|
const isImageModel = imageModels.has(params.model);
|
|
1361
|
-
const builtBody = buildResponsesRequestBody(params, isImageModel,
|
|
1362
|
-
const body =
|
|
1359
|
+
const builtBody = buildResponsesRequestBody(params, isImageModel, config.modelForCapabilities?.(params.model) ?? params.model);
|
|
1360
|
+
const body = config.transformRequestBody ? config.transformRequestBody(builtBody, params) : builtBody;
|
|
1363
1361
|
return async function* () {
|
|
1364
1362
|
const [apiKey, headers] = await Promise.all([
|
|
1365
1363
|
resolveKey(),
|
|
@@ -1743,12 +1741,12 @@ var processSSELine2 = function* (line, pendingToolCalls, providerName) {
|
|
|
1743
1741
|
}
|
|
1744
1742
|
if (isRecord2(parsed.error)) {
|
|
1745
1743
|
const error = parsed.error;
|
|
1746
|
-
const
|
|
1744
|
+
const metadata = isRecord2(error.metadata) ? error.metadata : undefined;
|
|
1747
1745
|
const status = typeof error.code === "number" ? error.code : null;
|
|
1748
|
-
const type =
|
|
1746
|
+
const type = metadata && typeof metadata.error_type === "string" ? metadata.error_type : typeof error.error_type === "string" ? error.error_type : null;
|
|
1749
1747
|
throw new ProviderError({
|
|
1750
1748
|
message: typeof error.message === "string" ? error.message : "OpenRouter stream failed",
|
|
1751
|
-
metadata
|
|
1749
|
+
metadata,
|
|
1752
1750
|
provider: providerName,
|
|
1753
1751
|
retryable: status === 408 || status === 409 || status === 425 || status === 429 || status !== null && status >= 500,
|
|
1754
1752
|
status,
|
|
@@ -1773,15 +1771,15 @@ var processSSELine2 = function* (line, pendingToolCalls, providerName) {
|
|
|
1773
1771
|
}
|
|
1774
1772
|
if (isRecord2(firstChoice.error)) {
|
|
1775
1773
|
const error = firstChoice.error;
|
|
1776
|
-
const
|
|
1774
|
+
const metadata = isRecord2(error.metadata) ? error.metadata : undefined;
|
|
1777
1775
|
const status = typeof error.code === "number" ? error.code : null;
|
|
1778
1776
|
throw new ProviderError({
|
|
1779
1777
|
message: typeof error.message === "string" ? error.message : "OpenRouter stream failed",
|
|
1780
|
-
metadata
|
|
1778
|
+
metadata,
|
|
1781
1779
|
provider: providerName,
|
|
1782
1780
|
retryable: status === 429 || status !== null && status >= 500,
|
|
1783
1781
|
status,
|
|
1784
|
-
type:
|
|
1782
|
+
type: metadata && typeof metadata.error_type === "string" ? metadata.error_type : null
|
|
1785
1783
|
});
|
|
1786
1784
|
}
|
|
1787
1785
|
yield* processChoice(firstChoice, pendingToolCalls);
|
|
@@ -1880,32 +1878,32 @@ var fetchOpenAIStream = async function* (baseUrl, apiKey, body, fetchImpl, heade
|
|
|
1880
1878
|
}
|
|
1881
1879
|
}, providerName, signal);
|
|
1882
1880
|
};
|
|
1883
|
-
var openai = (
|
|
1884
|
-
const baseUrl =
|
|
1885
|
-
const fetchImpl =
|
|
1886
|
-
const providerName =
|
|
1887
|
-
if (!
|
|
1881
|
+
var openai = (config) => {
|
|
1882
|
+
const baseUrl = config.baseUrl ?? DEFAULT_BASE_URL2;
|
|
1883
|
+
const fetchImpl = config.fetch ?? globalThis.fetch;
|
|
1884
|
+
const providerName = config.providerName ?? "openai";
|
|
1885
|
+
if (!config.apiKey && !config.tokenSource) {
|
|
1888
1886
|
throw new Error("openai() requires either apiKey or tokenSource");
|
|
1889
1887
|
}
|
|
1890
1888
|
const resolveKey = async () => {
|
|
1891
|
-
if (
|
|
1892
|
-
return await Promise.resolve(
|
|
1889
|
+
if (config.tokenSource) {
|
|
1890
|
+
return await Promise.resolve(config.tokenSource());
|
|
1893
1891
|
}
|
|
1894
|
-
return
|
|
1892
|
+
return config.apiKey;
|
|
1895
1893
|
};
|
|
1896
|
-
const resolveHeaders = async (params) => typeof
|
|
1894
|
+
const resolveHeaders = async (params) => typeof config.headers === "function" ? await config.headers(params) : config.headers ?? {};
|
|
1897
1895
|
return instrumentAIProvider({
|
|
1898
|
-
inputCapacity:
|
|
1896
|
+
inputCapacity: config.transformRequestBody ? undefined : openaiInputCapacity({
|
|
1899
1897
|
baseUrl,
|
|
1900
1898
|
fetch: fetchImpl,
|
|
1901
1899
|
key: resolveKey,
|
|
1902
1900
|
headers: resolveHeaders,
|
|
1903
|
-
modelLimits:
|
|
1904
|
-
body: (params) => buildResponsesRequestBody(params, false,
|
|
1901
|
+
modelLimits: config.modelLimits,
|
|
1902
|
+
body: (params) => buildResponsesRequestBody(params, false, config.modelForCapabilities?.(params.model) ?? params.model)
|
|
1905
1903
|
}),
|
|
1906
1904
|
stream: (params) => {
|
|
1907
|
-
const openaiBody = buildRequestBody(params,
|
|
1908
|
-
const body =
|
|
1905
|
+
const openaiBody = buildRequestBody(params, config.modelForCapabilities?.(params.model) ?? params.model, providerName);
|
|
1906
|
+
const body = config.transformRequestBody ? config.transformRequestBody(openaiBody, params) : openaiBody;
|
|
1909
1907
|
return async function* () {
|
|
1910
1908
|
const [apiKey, headers] = await Promise.all([
|
|
1911
1909
|
resolveKey(),
|
|
@@ -2060,9 +2058,15 @@ var buildRequestBody2 = (params, configuredMax, configCaching) => {
|
|
|
2060
2058
|
const options = requestOptionsFor(params);
|
|
2061
2059
|
const messages = params.messages.filter((msg) => msg.role !== "system").map(mapMessage);
|
|
2062
2060
|
if (caching && messages.length > 1) {
|
|
2063
|
-
const
|
|
2061
|
+
const lastIndex = messages.length - 1;
|
|
2062
|
+
const last = messages[lastIndex];
|
|
2064
2063
|
if (last) {
|
|
2065
|
-
messages[
|
|
2064
|
+
messages[lastIndex] = cacheLastContentBlock(last);
|
|
2065
|
+
}
|
|
2066
|
+
const previousUserIndex = messages.findLastIndex((message, index) => index < lastIndex - 1 && message.role === "user");
|
|
2067
|
+
const previous = messages[previousUserIndex];
|
|
2068
|
+
if (previous) {
|
|
2069
|
+
messages[previousUserIndex] = cacheLastContentBlock(previous);
|
|
2066
2070
|
}
|
|
2067
2071
|
}
|
|
2068
2072
|
const max = typeof params.maxTokens === "number" ? params.maxTokens : configuredMax;
|
|
@@ -2481,16 +2485,16 @@ async function* parseSSEStream3(body, providerName, signal) {
|
|
|
2481
2485
|
reader.releaseLock();
|
|
2482
2486
|
}
|
|
2483
2487
|
}
|
|
2484
|
-
var fetchAndStream = async function* (baseUrl,
|
|
2488
|
+
var fetchAndStream = async function* (baseUrl, config, params, configuredMax, promptCaching, providerName) {
|
|
2485
2489
|
const builtBody = buildRequestBody2(params, configuredMax, promptCaching);
|
|
2486
|
-
const body =
|
|
2490
|
+
const body = config.transformRequestBody ? config.transformRequestBody(builtBody, params) : builtBody;
|
|
2487
2491
|
const target = `${baseUrl}/v1/messages`;
|
|
2488
|
-
const fetchImpl =
|
|
2489
|
-
const token =
|
|
2490
|
-
const suppliedHeaders = typeof
|
|
2492
|
+
const fetchImpl = config.fetch ?? fetch;
|
|
2493
|
+
const token = config.tokenSource ? await Promise.resolve(config.tokenSource()) : config.apiKey;
|
|
2494
|
+
const suppliedHeaders = typeof config.headers === "function" ? await config.headers(params) : config.headers ?? {};
|
|
2491
2495
|
const requestHeaders = new Headers(suppliedHeaders);
|
|
2492
2496
|
requestHeaders.set("Content-Type", "application/json");
|
|
2493
|
-
if (
|
|
2497
|
+
if (config.authStyle === "bearer") {
|
|
2494
2498
|
requestHeaders.set("Authorization", `Bearer ${token}`);
|
|
2495
2499
|
} else {
|
|
2496
2500
|
requestHeaders.set("anthropic-version", API_VERSION);
|
|
@@ -2516,24 +2520,24 @@ var fetchAndStream = async function* (baseUrl, config2, params, configuredMax, p
|
|
|
2516
2520
|
}
|
|
2517
2521
|
yield* parseSSEStream3(response.body, providerName, params.signal);
|
|
2518
2522
|
};
|
|
2519
|
-
var anthropic = (
|
|
2520
|
-
if (!
|
|
2523
|
+
var anthropic = (config) => {
|
|
2524
|
+
if (!config.apiKey && !config.tokenSource)
|
|
2521
2525
|
throw new Error("anthropic() requires either apiKey or tokenSource");
|
|
2522
|
-
const baseUrl =
|
|
2523
|
-
const configuredMax =
|
|
2524
|
-
const promptCaching =
|
|
2525
|
-
const providerName =
|
|
2526
|
+
const baseUrl = config.baseUrl ?? DEFAULT_BASE_URL3;
|
|
2527
|
+
const configuredMax = config.maxTokens ?? DEFAULT_MAX_TOKENS;
|
|
2528
|
+
const promptCaching = config.promptCaching ?? true;
|
|
2529
|
+
const providerName = config.providerName ?? "anthropic";
|
|
2526
2530
|
const capacityRequest = async (params, path, body) => {
|
|
2527
|
-
const token =
|
|
2528
|
-
const headers = new Headers(typeof
|
|
2531
|
+
const token = config.tokenSource ? await config.tokenSource() : config.apiKey;
|
|
2532
|
+
const headers = new Headers(typeof config.headers === "function" ? await config.headers(params) : config.headers);
|
|
2529
2533
|
headers.set("Content-Type", "application/json");
|
|
2530
|
-
if (
|
|
2534
|
+
if (config.authStyle === "bearer")
|
|
2531
2535
|
headers.set("Authorization", `Bearer ${token}`);
|
|
2532
2536
|
else {
|
|
2533
2537
|
headers.set("anthropic-version", API_VERSION);
|
|
2534
2538
|
headers.set("x-api-key", token);
|
|
2535
2539
|
}
|
|
2536
|
-
return capacityJson(await (
|
|
2540
|
+
return capacityJson(await (config.fetch ?? fetch)(`${baseUrl}${path}`, {
|
|
2537
2541
|
method: body ? "POST" : "GET",
|
|
2538
2542
|
headers,
|
|
2539
2543
|
body: body ? JSON.stringify(body) : undefined,
|
|
@@ -2542,7 +2546,7 @@ var anthropic = (config2) => {
|
|
|
2542
2546
|
};
|
|
2543
2547
|
const countingRequest = (params) => {
|
|
2544
2548
|
const built = buildRequestBody2(params, configuredMax, promptCaching);
|
|
2545
|
-
const body =
|
|
2549
|
+
const body = config.transformRequestBody ? config.transformRequestBody(built, params) : built;
|
|
2546
2550
|
const counted = {};
|
|
2547
2551
|
for (const key of [
|
|
2548
2552
|
"model",
|
|
@@ -2572,7 +2576,7 @@ var anthropic = (config2) => {
|
|
|
2572
2576
|
const inputCapacity = {
|
|
2573
2577
|
outputTokens: (params) => {
|
|
2574
2578
|
const built = buildRequestBody2(params, configuredMax, promptCaching);
|
|
2575
|
-
const body =
|
|
2579
|
+
const body = config.transformRequestBody ? config.transformRequestBody(built, params) : built;
|
|
2576
2580
|
return positiveTokenLimit(body.max_tokens);
|
|
2577
2581
|
},
|
|
2578
2582
|
getLimits: cacheModelLimits(async (params) => {
|
|
@@ -2583,7 +2587,7 @@ var anthropic = (config2) => {
|
|
|
2583
2587
|
contextWindowTokens: context,
|
|
2584
2588
|
maxOutputTokens: positiveTokenLimit(data.max_tokens)
|
|
2585
2589
|
};
|
|
2586
|
-
},
|
|
2590
|
+
}, config.tokenSource || typeof config.headers === "function" ? 0 : 300000),
|
|
2587
2591
|
countScope: (params) => countingRequest(params).scope,
|
|
2588
2592
|
countTokens: async (params) => {
|
|
2589
2593
|
const { counted } = countingRequest(params);
|
|
@@ -2592,7 +2596,7 @@ var anthropic = (config2) => {
|
|
|
2592
2596
|
};
|
|
2593
2597
|
return instrumentAIProvider({
|
|
2594
2598
|
inputCapacity,
|
|
2595
|
-
stream: (params) => fetchAndStream(baseUrl,
|
|
2599
|
+
stream: (params) => fetchAndStream(baseUrl, config, params, configuredMax, promptCaching, providerName)
|
|
2596
2600
|
}, providerName);
|
|
2597
2601
|
};
|
|
2598
2602
|
|
|
@@ -2783,8 +2787,8 @@ var constantTimeEqual = (left, right) => {
|
|
|
2783
2787
|
};
|
|
2784
2788
|
var verifyOpenRouterWebhookSignature = async (options) => {
|
|
2785
2789
|
const fields = new Map(options.header.split(",").map((part) => {
|
|
2786
|
-
const [
|
|
2787
|
-
return [
|
|
2790
|
+
const [key, ...rest] = part.trim().split("=");
|
|
2791
|
+
return [key, rest.join("=")];
|
|
2788
2792
|
}));
|
|
2789
2793
|
const timestamp = fields.get("t");
|
|
2790
2794
|
const supplied = fields.get("v1");
|
|
@@ -2805,17 +2809,17 @@ var verifyOpenRouterWebhookSignature = async (options) => {
|
|
|
2805
2809
|
const suppliedBytes = hexToBytes(supplied);
|
|
2806
2810
|
return suppliedBytes ? constantTimeEqual(expected, suppliedBytes) : false;
|
|
2807
2811
|
};
|
|
2808
|
-
var createOpenRouterClient = (
|
|
2809
|
-
if (!
|
|
2812
|
+
var createOpenRouterClient = (config) => {
|
|
2813
|
+
if (!config.apiKey && !config.tokenSource)
|
|
2810
2814
|
throw new Error("createOpenRouterClient() requires either apiKey or tokenSource");
|
|
2811
|
-
const baseUrl = (
|
|
2812
|
-
const batchBaseUrl = (
|
|
2813
|
-
const fetchImpl =
|
|
2814
|
-
const allowedModels =
|
|
2815
|
-
const defaultWorkspaceId =
|
|
2815
|
+
const baseUrl = (config.baseUrl ?? DEFAULT_BASE_URL4).replace(/\/$/, "");
|
|
2816
|
+
const batchBaseUrl = (config.batchBaseUrl ?? (config.baseUrl ? new URL("../beta", `${baseUrl}/`).toString() : DEFAULT_BATCH_BASE_URL)).replace(/\/$/, "");
|
|
2817
|
+
const fetchImpl = config.fetch ?? globalThis.fetch;
|
|
2818
|
+
const allowedModels = config.allowedModels ? [...config.allowedModels] : undefined;
|
|
2819
|
+
const defaultWorkspaceId = config.workspaceId;
|
|
2816
2820
|
const requestRawAt = async (rootUrl, path, options = {}) => {
|
|
2817
|
-
const token =
|
|
2818
|
-
const suppliedHeaders = typeof
|
|
2821
|
+
const token = config.tokenSource ? await Promise.resolve(config.tokenSource()) : config.apiKey;
|
|
2822
|
+
const suppliedHeaders = typeof config.headers === "function" ? await config.headers() : config.headers ?? {};
|
|
2819
2823
|
const headers = new Headers(suppliedHeaders);
|
|
2820
2824
|
new Headers(options.headers).forEach((value, key) => headers.set(key, value));
|
|
2821
2825
|
headers.set("Authorization", `Bearer ${token}`);
|
|
@@ -3120,20 +3124,20 @@ var assertNonEmptyPolicy = (label, value) => {
|
|
|
3120
3124
|
throw new Error(`openrouter() ${label} must not be empty`);
|
|
3121
3125
|
}
|
|
3122
3126
|
};
|
|
3123
|
-
var assertRoutingPolicy = (
|
|
3124
|
-
assertNonEmptyPolicy("allowedProviders",
|
|
3125
|
-
assertNonEmptyPolicy("routing.only",
|
|
3126
|
-
assertNonEmptyPolicy("allowedPresets",
|
|
3127
|
-
if (
|
|
3127
|
+
var assertRoutingPolicy = (config) => {
|
|
3128
|
+
assertNonEmptyPolicy("allowedProviders", config.allowedProviders);
|
|
3129
|
+
assertNonEmptyPolicy("routing.only", config.routing?.only);
|
|
3130
|
+
assertNonEmptyPolicy("allowedPresets", config.allowedPresets);
|
|
3131
|
+
if (config.appCategories && config.appCategories.length > MAX_APP_CATEGORIES) {
|
|
3128
3132
|
throw new Error("openrouter() appCategories supports at most 2 entries");
|
|
3129
3133
|
}
|
|
3130
|
-
if (!
|
|
3134
|
+
if (!config.allowedProviders)
|
|
3131
3135
|
return;
|
|
3132
3136
|
const selected = [
|
|
3133
|
-
...
|
|
3134
|
-
...
|
|
3137
|
+
...config.routing?.only ?? [],
|
|
3138
|
+
...config.routing?.order ?? []
|
|
3135
3139
|
];
|
|
3136
|
-
const denied = selected.find((provider) => !
|
|
3140
|
+
const denied = selected.find((provider) => !config.allowedProviders.some((rule) => providerMatchesRule(provider, rule)));
|
|
3137
3141
|
if (denied) {
|
|
3138
3142
|
throw new Error(`openrouter() provider "${denied}" is outside allowedProviders`);
|
|
3139
3143
|
}
|
|
@@ -3186,17 +3190,17 @@ var requestOptionsFor2 = (params, defaults) => {
|
|
|
3186
3190
|
...supplied
|
|
3187
3191
|
};
|
|
3188
3192
|
};
|
|
3189
|
-
var resolveAttributionHeaders = async (
|
|
3190
|
-
const supplied = typeof
|
|
3193
|
+
var resolveAttributionHeaders = async (config, params) => {
|
|
3194
|
+
const supplied = typeof config.headers === "function" ? await config.headers() : config.headers ?? {};
|
|
3191
3195
|
const headers = new Headers(supplied);
|
|
3192
|
-
if (
|
|
3193
|
-
headers.set("HTTP-Referer",
|
|
3194
|
-
if (
|
|
3195
|
-
headers.set("X-OpenRouter-Title",
|
|
3196
|
-
if (
|
|
3197
|
-
headers.set("X-OpenRouter-Categories",
|
|
3198
|
-
}
|
|
3199
|
-
const options = requestOptionsFor2(params,
|
|
3196
|
+
if (config.appUrl)
|
|
3197
|
+
headers.set("HTTP-Referer", config.appUrl);
|
|
3198
|
+
if (config.appName)
|
|
3199
|
+
headers.set("X-OpenRouter-Title", config.appName);
|
|
3200
|
+
if (config.appCategories?.length) {
|
|
3201
|
+
headers.set("X-OpenRouter-Categories", config.appCategories.join(","));
|
|
3202
|
+
}
|
|
3203
|
+
const options = requestOptionsFor2(params, config.requestOptions);
|
|
3200
3204
|
if (options.routerMetadata ?? true)
|
|
3201
3205
|
headers.set("X-OpenRouter-Metadata", "enabled");
|
|
3202
3206
|
if (options.sessionId)
|
|
@@ -3356,13 +3360,13 @@ var assertAllowedModel2 = (model, allowedModels) => {
|
|
|
3356
3360
|
return;
|
|
3357
3361
|
throw new Error(`OpenRouter model "${model}" is not allowed`);
|
|
3358
3362
|
};
|
|
3359
|
-
var snapshotPolicy = (
|
|
3360
|
-
allowedModels:
|
|
3361
|
-
allowedPresets:
|
|
3363
|
+
var snapshotPolicy = (config) => ({
|
|
3364
|
+
allowedModels: config.allowedModels ? [...config.allowedModels] : undefined,
|
|
3365
|
+
allowedPresets: config.allowedPresets ? [...config.allowedPresets] : undefined
|
|
3362
3366
|
});
|
|
3363
|
-
var transformOpenRouterRequest = (
|
|
3364
|
-
const options = requestOptionsFor2(params,
|
|
3365
|
-
assertRequestOptions(options, allowedModels, allowedPresets,
|
|
3367
|
+
var transformOpenRouterRequest = (config, allowedModels, allowedPresets, body, params, skin = "openai") => {
|
|
3368
|
+
const options = requestOptionsFor2(params, config.requestOptions);
|
|
3369
|
+
assertRequestOptions(options, allowedModels, allowedPresets, config.allowedProviders);
|
|
3366
3370
|
const transformed = { ...body, ...options.extraBody };
|
|
3367
3371
|
if (options.audioOutput) {
|
|
3368
3372
|
transformed.audio = options.audioOutput;
|
|
@@ -3395,7 +3399,7 @@ var transformOpenRouterRequest = (config2, allowedModels, allowedPresets, body,
|
|
|
3395
3399
|
transformed.prompt_cache_key = options.promptCacheKey;
|
|
3396
3400
|
if (options.promptCacheOptions)
|
|
3397
3401
|
transformed.prompt_cache_options = options.promptCacheOptions;
|
|
3398
|
-
const routing = mapRouting({ ...
|
|
3402
|
+
const routing = mapRouting({ ...config.routing, ...options.routing }, config.allowedProviders);
|
|
3399
3403
|
if (Object.keys(routing).length > 0)
|
|
3400
3404
|
transformed.provider = routing;
|
|
3401
3405
|
if (options.fallbackModels)
|
|
@@ -3454,48 +3458,48 @@ var withOpenRouterPolicy = (provider, allowedModels, allowedPresets) => ({
|
|
|
3454
3458
|
return provider.stream(params);
|
|
3455
3459
|
}
|
|
3456
3460
|
});
|
|
3457
|
-
var openrouter = (
|
|
3458
|
-
assertRoutingPolicy(
|
|
3459
|
-
const { allowedModels, allowedPresets } = snapshotPolicy(
|
|
3461
|
+
var openrouter = (config) => {
|
|
3462
|
+
assertRoutingPolicy(config);
|
|
3463
|
+
const { allowedModels, allowedPresets } = snapshotPolicy(config);
|
|
3460
3464
|
const provider = openai({
|
|
3461
|
-
apiKey:
|
|
3462
|
-
baseUrl:
|
|
3463
|
-
fetch:
|
|
3464
|
-
headers: (params) => resolveAttributionHeaders(
|
|
3465
|
+
apiKey: config.apiKey,
|
|
3466
|
+
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL5,
|
|
3467
|
+
fetch: config.fetch,
|
|
3468
|
+
headers: (params) => resolveAttributionHeaders(config, params),
|
|
3465
3469
|
modelForCapabilities: modelForOpenAICapabilities,
|
|
3466
3470
|
providerName: "openrouter",
|
|
3467
|
-
tokenSource:
|
|
3468
|
-
transformRequestBody: (body, params) => transformOpenRouterRequest(
|
|
3471
|
+
tokenSource: config.tokenSource,
|
|
3472
|
+
transformRequestBody: (body, params) => transformOpenRouterRequest(config, allowedModels, allowedPresets, body, params)
|
|
3469
3473
|
});
|
|
3470
3474
|
return withOpenRouterPolicy(provider, allowedModels, allowedPresets);
|
|
3471
3475
|
};
|
|
3472
|
-
var openrouterResponses = (
|
|
3473
|
-
assertRoutingPolicy(
|
|
3474
|
-
const { allowedModels, allowedPresets } = snapshotPolicy(
|
|
3476
|
+
var openrouterResponses = (config) => {
|
|
3477
|
+
assertRoutingPolicy(config);
|
|
3478
|
+
const { allowedModels, allowedPresets } = snapshotPolicy(config);
|
|
3475
3479
|
const provider = openaiResponses({
|
|
3476
|
-
apiKey:
|
|
3477
|
-
baseUrl:
|
|
3478
|
-
fetch:
|
|
3479
|
-
headers: (params) => resolveAttributionHeaders(
|
|
3480
|
+
apiKey: config.apiKey,
|
|
3481
|
+
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL5,
|
|
3482
|
+
fetch: config.fetch,
|
|
3483
|
+
headers: (params) => resolveAttributionHeaders(config, params),
|
|
3480
3484
|
modelForCapabilities: modelForOpenAICapabilities,
|
|
3481
3485
|
providerName: "openrouter",
|
|
3482
|
-
tokenSource:
|
|
3483
|
-
transformRequestBody: (body, params) => transformOpenRouterRequest(
|
|
3486
|
+
tokenSource: config.tokenSource,
|
|
3487
|
+
transformRequestBody: (body, params) => transformOpenRouterRequest(config, allowedModels, allowedPresets, body, params)
|
|
3484
3488
|
});
|
|
3485
3489
|
return withOpenRouterPolicy(provider, allowedModels, allowedPresets);
|
|
3486
3490
|
};
|
|
3487
|
-
var openrouterMessages = (
|
|
3488
|
-
assertRoutingPolicy(
|
|
3489
|
-
const { allowedModels, allowedPresets } = snapshotPolicy(
|
|
3491
|
+
var openrouterMessages = (config) => {
|
|
3492
|
+
assertRoutingPolicy(config);
|
|
3493
|
+
const { allowedModels, allowedPresets } = snapshotPolicy(config);
|
|
3490
3494
|
const provider = anthropic({
|
|
3491
|
-
apiKey:
|
|
3495
|
+
apiKey: config.apiKey,
|
|
3492
3496
|
authStyle: "bearer",
|
|
3493
|
-
baseUrl:
|
|
3494
|
-
fetch:
|
|
3495
|
-
headers: (params) => resolveAttributionHeaders(
|
|
3497
|
+
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL5,
|
|
3498
|
+
fetch: config.fetch,
|
|
3499
|
+
headers: (params) => resolveAttributionHeaders(config, params),
|
|
3496
3500
|
providerName: "openrouter",
|
|
3497
|
-
tokenSource:
|
|
3498
|
-
transformRequestBody: (body, params) => transformOpenRouterRequest(
|
|
3501
|
+
tokenSource: config.tokenSource,
|
|
3502
|
+
transformRequestBody: (body, params) => transformOpenRouterRequest(config, allowedModels, allowedPresets, body, params, "messages")
|
|
3499
3503
|
});
|
|
3500
3504
|
return withOpenRouterPolicy(provider, allowedModels, allowedPresets);
|
|
3501
3505
|
};
|
|
@@ -3514,5 +3518,5 @@ export {
|
|
|
3514
3518
|
verifyOpenRouterWebhookSignature
|
|
3515
3519
|
};
|
|
3516
3520
|
|
|
3517
|
-
//# debugId=
|
|
3521
|
+
//# debugId=47F788AD8F60934664756E2164756E21
|
|
3518
3522
|
//# sourceMappingURL=openrouter.js.map
|