@absolutejs/ai 0.2.11 → 0.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/CHANGELOG.md +10 -0
- package/changelog.json +368 -345
- package/dist/ai/client/index.js +1 -3
- package/dist/ai/client/index.js.map +2 -2
- package/dist/ai/index.js +7686 -7677
- 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 +25 -27
- package/dist/ai/providers/anthropic.js.map +2 -2
- 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 +43 -48
- package/dist/ai/providers/openai.js.map +3 -3
- package/dist/ai/providers/openaiCompatible.js +61 -66
- package/dist/ai/providers/openaiCompatible.js.map +3 -3
- package/dist/ai/providers/openaiResponses.js +23 -28
- package/dist/ai/providers/openaiResponses.js.map +3 -3
- package/dist/ai/providers/openrouter.js +127 -132
- 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 +2 -72
- package/dist/ai/ui/index.js.map +2 -2
- 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/generateAI.d.ts +5 -1
- package/dist/src/ai/streamAIWithTools.d.ts +8 -2
- 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 +5 -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,
|
|
@@ -821,18 +819,15 @@ var mapBlockToResponsesFormat = (block) => {
|
|
|
821
819
|
}
|
|
822
820
|
if (block.type === "image") {
|
|
823
821
|
return {
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
},
|
|
822
|
+
detail: "auto",
|
|
823
|
+
image_url: block.source.type === "url" ? block.source.url : `data:${block.source.media_type};base64,${block.source.data}`,
|
|
827
824
|
type: "input_image"
|
|
828
825
|
};
|
|
829
826
|
}
|
|
830
827
|
if (block.type === "document") {
|
|
831
|
-
return {
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
filename: block.name ?? "document.pdf"
|
|
835
|
-
},
|
|
828
|
+
return block.source.type === "url" ? { file_url: block.source.url, type: "input_file" } : {
|
|
829
|
+
file_data: `data:${block.source.media_type};base64,${block.source.data}`,
|
|
830
|
+
filename: block.name ?? "document.pdf",
|
|
836
831
|
type: "input_file"
|
|
837
832
|
};
|
|
838
833
|
}
|
|
@@ -1317,31 +1312,31 @@ var resolveImageModels = (imageModels) => {
|
|
|
1317
1312
|
}
|
|
1318
1313
|
return new Set(imageModels);
|
|
1319
1314
|
};
|
|
1320
|
-
var openaiResponses = (
|
|
1321
|
-
if (!
|
|
1315
|
+
var openaiResponses = (config) => {
|
|
1316
|
+
if (!config.apiKey && !config.tokenSource)
|
|
1322
1317
|
throw new Error("openaiResponses() requires either apiKey or tokenSource");
|
|
1323
|
-
const baseUrl =
|
|
1324
|
-
const fetchImpl =
|
|
1325
|
-
const imageModels = resolveImageModels(
|
|
1326
|
-
const providerName =
|
|
1327
|
-
const resolveKey = async () =>
|
|
1328
|
-
const resolveHeaders = async (params) => typeof
|
|
1318
|
+
const baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
|
|
1319
|
+
const fetchImpl = config.fetch ?? globalThis.fetch;
|
|
1320
|
+
const imageModels = resolveImageModels(config.imageModels);
|
|
1321
|
+
const providerName = config.providerName ?? "openai-responses";
|
|
1322
|
+
const resolveKey = async () => config.tokenSource ? await Promise.resolve(config.tokenSource()) : config.apiKey;
|
|
1323
|
+
const resolveHeaders = async (params) => typeof config.headers === "function" ? await config.headers(params) : config.headers ?? {};
|
|
1329
1324
|
return instrumentAIProvider({
|
|
1330
1325
|
inputCapacity: openaiInputCapacity({
|
|
1331
1326
|
baseUrl,
|
|
1332
1327
|
fetch: fetchImpl,
|
|
1333
1328
|
key: resolveKey,
|
|
1334
1329
|
headers: resolveHeaders,
|
|
1335
|
-
modelLimits:
|
|
1330
|
+
modelLimits: config.modelLimits,
|
|
1336
1331
|
body: (params) => {
|
|
1337
|
-
const built = buildResponsesRequestBody(params, imageModels.has(params.model),
|
|
1338
|
-
return
|
|
1332
|
+
const built = buildResponsesRequestBody(params, imageModels.has(params.model), config.modelForCapabilities?.(params.model) ?? params.model);
|
|
1333
|
+
return config.transformRequestBody ? config.transformRequestBody(built, params) : built;
|
|
1339
1334
|
}
|
|
1340
1335
|
}),
|
|
1341
1336
|
stream: (params) => {
|
|
1342
1337
|
const isImageModel = imageModels.has(params.model);
|
|
1343
|
-
const builtBody = buildResponsesRequestBody(params, isImageModel,
|
|
1344
|
-
const body =
|
|
1338
|
+
const builtBody = buildResponsesRequestBody(params, isImageModel, config.modelForCapabilities?.(params.model) ?? params.model);
|
|
1339
|
+
const body = config.transformRequestBody ? config.transformRequestBody(builtBody, params) : builtBody;
|
|
1345
1340
|
return async function* () {
|
|
1346
1341
|
const [apiKey, headers] = await Promise.all([
|
|
1347
1342
|
resolveKey(),
|
|
@@ -1725,12 +1720,12 @@ var processSSELine2 = function* (line, pendingToolCalls, providerName) {
|
|
|
1725
1720
|
}
|
|
1726
1721
|
if (isRecord2(parsed.error)) {
|
|
1727
1722
|
const error = parsed.error;
|
|
1728
|
-
const
|
|
1723
|
+
const metadata = isRecord2(error.metadata) ? error.metadata : undefined;
|
|
1729
1724
|
const status = typeof error.code === "number" ? error.code : null;
|
|
1730
|
-
const type =
|
|
1725
|
+
const type = metadata && typeof metadata.error_type === "string" ? metadata.error_type : typeof error.error_type === "string" ? error.error_type : null;
|
|
1731
1726
|
throw new ProviderError({
|
|
1732
1727
|
message: typeof error.message === "string" ? error.message : "OpenRouter stream failed",
|
|
1733
|
-
metadata
|
|
1728
|
+
metadata,
|
|
1734
1729
|
provider: providerName,
|
|
1735
1730
|
retryable: status === 408 || status === 409 || status === 425 || status === 429 || status !== null && status >= 500,
|
|
1736
1731
|
status,
|
|
@@ -1755,15 +1750,15 @@ var processSSELine2 = function* (line, pendingToolCalls, providerName) {
|
|
|
1755
1750
|
}
|
|
1756
1751
|
if (isRecord2(firstChoice.error)) {
|
|
1757
1752
|
const error = firstChoice.error;
|
|
1758
|
-
const
|
|
1753
|
+
const metadata = isRecord2(error.metadata) ? error.metadata : undefined;
|
|
1759
1754
|
const status = typeof error.code === "number" ? error.code : null;
|
|
1760
1755
|
throw new ProviderError({
|
|
1761
1756
|
message: typeof error.message === "string" ? error.message : "OpenRouter stream failed",
|
|
1762
|
-
metadata
|
|
1757
|
+
metadata,
|
|
1763
1758
|
provider: providerName,
|
|
1764
1759
|
retryable: status === 429 || status !== null && status >= 500,
|
|
1765
1760
|
status,
|
|
1766
|
-
type:
|
|
1761
|
+
type: metadata && typeof metadata.error_type === "string" ? metadata.error_type : null
|
|
1767
1762
|
});
|
|
1768
1763
|
}
|
|
1769
1764
|
yield* processChoice(firstChoice, pendingToolCalls);
|
|
@@ -1862,32 +1857,32 @@ var fetchOpenAIStream = async function* (baseUrl, apiKey, body, fetchImpl, heade
|
|
|
1862
1857
|
}
|
|
1863
1858
|
}, providerName, signal);
|
|
1864
1859
|
};
|
|
1865
|
-
var openai = (
|
|
1866
|
-
const baseUrl =
|
|
1867
|
-
const fetchImpl =
|
|
1868
|
-
const providerName =
|
|
1869
|
-
if (!
|
|
1860
|
+
var openai = (config) => {
|
|
1861
|
+
const baseUrl = config.baseUrl ?? DEFAULT_BASE_URL2;
|
|
1862
|
+
const fetchImpl = config.fetch ?? globalThis.fetch;
|
|
1863
|
+
const providerName = config.providerName ?? "openai";
|
|
1864
|
+
if (!config.apiKey && !config.tokenSource) {
|
|
1870
1865
|
throw new Error("openai() requires either apiKey or tokenSource");
|
|
1871
1866
|
}
|
|
1872
1867
|
const resolveKey = async () => {
|
|
1873
|
-
if (
|
|
1874
|
-
return await Promise.resolve(
|
|
1868
|
+
if (config.tokenSource) {
|
|
1869
|
+
return await Promise.resolve(config.tokenSource());
|
|
1875
1870
|
}
|
|
1876
|
-
return
|
|
1871
|
+
return config.apiKey;
|
|
1877
1872
|
};
|
|
1878
|
-
const resolveHeaders = async (params) => typeof
|
|
1873
|
+
const resolveHeaders = async (params) => typeof config.headers === "function" ? await config.headers(params) : config.headers ?? {};
|
|
1879
1874
|
return instrumentAIProvider({
|
|
1880
|
-
inputCapacity:
|
|
1875
|
+
inputCapacity: config.transformRequestBody ? undefined : openaiInputCapacity({
|
|
1881
1876
|
baseUrl,
|
|
1882
1877
|
fetch: fetchImpl,
|
|
1883
1878
|
key: resolveKey,
|
|
1884
1879
|
headers: resolveHeaders,
|
|
1885
|
-
modelLimits:
|
|
1886
|
-
body: (params) => buildResponsesRequestBody(params, false,
|
|
1880
|
+
modelLimits: config.modelLimits,
|
|
1881
|
+
body: (params) => buildResponsesRequestBody(params, false, config.modelForCapabilities?.(params.model) ?? params.model)
|
|
1887
1882
|
}),
|
|
1888
1883
|
stream: (params) => {
|
|
1889
|
-
const openaiBody = buildRequestBody(params,
|
|
1890
|
-
const body =
|
|
1884
|
+
const openaiBody = buildRequestBody(params, config.modelForCapabilities?.(params.model) ?? params.model, providerName);
|
|
1885
|
+
const body = config.transformRequestBody ? config.transformRequestBody(openaiBody, params) : openaiBody;
|
|
1891
1886
|
return async function* () {
|
|
1892
1887
|
const [apiKey, headers] = await Promise.all([
|
|
1893
1888
|
resolveKey(),
|
|
@@ -2463,16 +2458,16 @@ async function* parseSSEStream3(body, providerName, signal) {
|
|
|
2463
2458
|
reader.releaseLock();
|
|
2464
2459
|
}
|
|
2465
2460
|
}
|
|
2466
|
-
var fetchAndStream = async function* (baseUrl,
|
|
2461
|
+
var fetchAndStream = async function* (baseUrl, config, params, configuredMax, promptCaching, providerName) {
|
|
2467
2462
|
const builtBody = buildRequestBody2(params, configuredMax, promptCaching);
|
|
2468
|
-
const body =
|
|
2463
|
+
const body = config.transformRequestBody ? config.transformRequestBody(builtBody, params) : builtBody;
|
|
2469
2464
|
const target = `${baseUrl}/v1/messages`;
|
|
2470
|
-
const fetchImpl =
|
|
2471
|
-
const token =
|
|
2472
|
-
const suppliedHeaders = typeof
|
|
2465
|
+
const fetchImpl = config.fetch ?? fetch;
|
|
2466
|
+
const token = config.tokenSource ? await Promise.resolve(config.tokenSource()) : config.apiKey;
|
|
2467
|
+
const suppliedHeaders = typeof config.headers === "function" ? await config.headers(params) : config.headers ?? {};
|
|
2473
2468
|
const requestHeaders = new Headers(suppliedHeaders);
|
|
2474
2469
|
requestHeaders.set("Content-Type", "application/json");
|
|
2475
|
-
if (
|
|
2470
|
+
if (config.authStyle === "bearer") {
|
|
2476
2471
|
requestHeaders.set("Authorization", `Bearer ${token}`);
|
|
2477
2472
|
} else {
|
|
2478
2473
|
requestHeaders.set("anthropic-version", API_VERSION);
|
|
@@ -2498,24 +2493,24 @@ var fetchAndStream = async function* (baseUrl, config2, params, configuredMax, p
|
|
|
2498
2493
|
}
|
|
2499
2494
|
yield* parseSSEStream3(response.body, providerName, params.signal);
|
|
2500
2495
|
};
|
|
2501
|
-
var anthropic = (
|
|
2502
|
-
if (!
|
|
2496
|
+
var anthropic = (config) => {
|
|
2497
|
+
if (!config.apiKey && !config.tokenSource)
|
|
2503
2498
|
throw new Error("anthropic() requires either apiKey or tokenSource");
|
|
2504
|
-
const baseUrl =
|
|
2505
|
-
const configuredMax =
|
|
2506
|
-
const promptCaching =
|
|
2507
|
-
const providerName =
|
|
2499
|
+
const baseUrl = config.baseUrl ?? DEFAULT_BASE_URL3;
|
|
2500
|
+
const configuredMax = config.maxTokens ?? DEFAULT_MAX_TOKENS;
|
|
2501
|
+
const promptCaching = config.promptCaching ?? true;
|
|
2502
|
+
const providerName = config.providerName ?? "anthropic";
|
|
2508
2503
|
const capacityRequest = async (params, path, body) => {
|
|
2509
|
-
const token =
|
|
2510
|
-
const headers = new Headers(typeof
|
|
2504
|
+
const token = config.tokenSource ? await config.tokenSource() : config.apiKey;
|
|
2505
|
+
const headers = new Headers(typeof config.headers === "function" ? await config.headers(params) : config.headers);
|
|
2511
2506
|
headers.set("Content-Type", "application/json");
|
|
2512
|
-
if (
|
|
2507
|
+
if (config.authStyle === "bearer")
|
|
2513
2508
|
headers.set("Authorization", `Bearer ${token}`);
|
|
2514
2509
|
else {
|
|
2515
2510
|
headers.set("anthropic-version", API_VERSION);
|
|
2516
2511
|
headers.set("x-api-key", token);
|
|
2517
2512
|
}
|
|
2518
|
-
return capacityJson(await (
|
|
2513
|
+
return capacityJson(await (config.fetch ?? fetch)(`${baseUrl}${path}`, {
|
|
2519
2514
|
method: body ? "POST" : "GET",
|
|
2520
2515
|
headers,
|
|
2521
2516
|
body: body ? JSON.stringify(body) : undefined,
|
|
@@ -2524,7 +2519,7 @@ var anthropic = (config2) => {
|
|
|
2524
2519
|
};
|
|
2525
2520
|
const countingRequest = (params) => {
|
|
2526
2521
|
const built = buildRequestBody2(params, configuredMax, promptCaching);
|
|
2527
|
-
const body =
|
|
2522
|
+
const body = config.transformRequestBody ? config.transformRequestBody(built, params) : built;
|
|
2528
2523
|
const counted = {};
|
|
2529
2524
|
for (const key of [
|
|
2530
2525
|
"model",
|
|
@@ -2554,7 +2549,7 @@ var anthropic = (config2) => {
|
|
|
2554
2549
|
const inputCapacity = {
|
|
2555
2550
|
outputTokens: (params) => {
|
|
2556
2551
|
const built = buildRequestBody2(params, configuredMax, promptCaching);
|
|
2557
|
-
const body =
|
|
2552
|
+
const body = config.transformRequestBody ? config.transformRequestBody(built, params) : built;
|
|
2558
2553
|
return positiveTokenLimit(body.max_tokens);
|
|
2559
2554
|
},
|
|
2560
2555
|
getLimits: cacheModelLimits(async (params) => {
|
|
@@ -2565,7 +2560,7 @@ var anthropic = (config2) => {
|
|
|
2565
2560
|
contextWindowTokens: context,
|
|
2566
2561
|
maxOutputTokens: positiveTokenLimit(data.max_tokens)
|
|
2567
2562
|
};
|
|
2568
|
-
},
|
|
2563
|
+
}, config.tokenSource || typeof config.headers === "function" ? 0 : 300000),
|
|
2569
2564
|
countScope: (params) => countingRequest(params).scope,
|
|
2570
2565
|
countTokens: async (params) => {
|
|
2571
2566
|
const { counted } = countingRequest(params);
|
|
@@ -2574,7 +2569,7 @@ var anthropic = (config2) => {
|
|
|
2574
2569
|
};
|
|
2575
2570
|
return instrumentAIProvider({
|
|
2576
2571
|
inputCapacity,
|
|
2577
|
-
stream: (params) => fetchAndStream(baseUrl,
|
|
2572
|
+
stream: (params) => fetchAndStream(baseUrl, config, params, configuredMax, promptCaching, providerName)
|
|
2578
2573
|
}, providerName);
|
|
2579
2574
|
};
|
|
2580
2575
|
|
|
@@ -2765,8 +2760,8 @@ var constantTimeEqual = (left, right) => {
|
|
|
2765
2760
|
};
|
|
2766
2761
|
var verifyOpenRouterWebhookSignature = async (options) => {
|
|
2767
2762
|
const fields = new Map(options.header.split(",").map((part) => {
|
|
2768
|
-
const [
|
|
2769
|
-
return [
|
|
2763
|
+
const [key, ...rest] = part.trim().split("=");
|
|
2764
|
+
return [key, rest.join("=")];
|
|
2770
2765
|
}));
|
|
2771
2766
|
const timestamp = fields.get("t");
|
|
2772
2767
|
const supplied = fields.get("v1");
|
|
@@ -2787,17 +2782,17 @@ var verifyOpenRouterWebhookSignature = async (options) => {
|
|
|
2787
2782
|
const suppliedBytes = hexToBytes(supplied);
|
|
2788
2783
|
return suppliedBytes ? constantTimeEqual(expected, suppliedBytes) : false;
|
|
2789
2784
|
};
|
|
2790
|
-
var createOpenRouterClient = (
|
|
2791
|
-
if (!
|
|
2785
|
+
var createOpenRouterClient = (config) => {
|
|
2786
|
+
if (!config.apiKey && !config.tokenSource)
|
|
2792
2787
|
throw new Error("createOpenRouterClient() requires either apiKey or tokenSource");
|
|
2793
|
-
const baseUrl = (
|
|
2794
|
-
const batchBaseUrl = (
|
|
2795
|
-
const fetchImpl =
|
|
2796
|
-
const allowedModels =
|
|
2797
|
-
const defaultWorkspaceId =
|
|
2788
|
+
const baseUrl = (config.baseUrl ?? DEFAULT_BASE_URL4).replace(/\/$/, "");
|
|
2789
|
+
const batchBaseUrl = (config.batchBaseUrl ?? (config.baseUrl ? new URL("../beta", `${baseUrl}/`).toString() : DEFAULT_BATCH_BASE_URL)).replace(/\/$/, "");
|
|
2790
|
+
const fetchImpl = config.fetch ?? globalThis.fetch;
|
|
2791
|
+
const allowedModels = config.allowedModels ? [...config.allowedModels] : undefined;
|
|
2792
|
+
const defaultWorkspaceId = config.workspaceId;
|
|
2798
2793
|
const requestRawAt = async (rootUrl, path, options = {}) => {
|
|
2799
|
-
const token =
|
|
2800
|
-
const suppliedHeaders = typeof
|
|
2794
|
+
const token = config.tokenSource ? await Promise.resolve(config.tokenSource()) : config.apiKey;
|
|
2795
|
+
const suppliedHeaders = typeof config.headers === "function" ? await config.headers() : config.headers ?? {};
|
|
2801
2796
|
const headers = new Headers(suppliedHeaders);
|
|
2802
2797
|
new Headers(options.headers).forEach((value, key) => headers.set(key, value));
|
|
2803
2798
|
headers.set("Authorization", `Bearer ${token}`);
|
|
@@ -3102,20 +3097,20 @@ var assertNonEmptyPolicy = (label, value) => {
|
|
|
3102
3097
|
throw new Error(`openrouter() ${label} must not be empty`);
|
|
3103
3098
|
}
|
|
3104
3099
|
};
|
|
3105
|
-
var assertRoutingPolicy = (
|
|
3106
|
-
assertNonEmptyPolicy("allowedProviders",
|
|
3107
|
-
assertNonEmptyPolicy("routing.only",
|
|
3108
|
-
assertNonEmptyPolicy("allowedPresets",
|
|
3109
|
-
if (
|
|
3100
|
+
var assertRoutingPolicy = (config) => {
|
|
3101
|
+
assertNonEmptyPolicy("allowedProviders", config.allowedProviders);
|
|
3102
|
+
assertNonEmptyPolicy("routing.only", config.routing?.only);
|
|
3103
|
+
assertNonEmptyPolicy("allowedPresets", config.allowedPresets);
|
|
3104
|
+
if (config.appCategories && config.appCategories.length > MAX_APP_CATEGORIES) {
|
|
3110
3105
|
throw new Error("openrouter() appCategories supports at most 2 entries");
|
|
3111
3106
|
}
|
|
3112
|
-
if (!
|
|
3107
|
+
if (!config.allowedProviders)
|
|
3113
3108
|
return;
|
|
3114
3109
|
const selected = [
|
|
3115
|
-
...
|
|
3116
|
-
...
|
|
3110
|
+
...config.routing?.only ?? [],
|
|
3111
|
+
...config.routing?.order ?? []
|
|
3117
3112
|
];
|
|
3118
|
-
const denied = selected.find((provider) => !
|
|
3113
|
+
const denied = selected.find((provider) => !config.allowedProviders.some((rule) => providerMatchesRule(provider, rule)));
|
|
3119
3114
|
if (denied) {
|
|
3120
3115
|
throw new Error(`openrouter() provider "${denied}" is outside allowedProviders`);
|
|
3121
3116
|
}
|
|
@@ -3168,17 +3163,17 @@ var requestOptionsFor2 = (params, defaults) => {
|
|
|
3168
3163
|
...supplied
|
|
3169
3164
|
};
|
|
3170
3165
|
};
|
|
3171
|
-
var resolveAttributionHeaders = async (
|
|
3172
|
-
const supplied = typeof
|
|
3166
|
+
var resolveAttributionHeaders = async (config, params) => {
|
|
3167
|
+
const supplied = typeof config.headers === "function" ? await config.headers() : config.headers ?? {};
|
|
3173
3168
|
const headers = new Headers(supplied);
|
|
3174
|
-
if (
|
|
3175
|
-
headers.set("HTTP-Referer",
|
|
3176
|
-
if (
|
|
3177
|
-
headers.set("X-OpenRouter-Title",
|
|
3178
|
-
if (
|
|
3179
|
-
headers.set("X-OpenRouter-Categories",
|
|
3180
|
-
}
|
|
3181
|
-
const options = requestOptionsFor2(params,
|
|
3169
|
+
if (config.appUrl)
|
|
3170
|
+
headers.set("HTTP-Referer", config.appUrl);
|
|
3171
|
+
if (config.appName)
|
|
3172
|
+
headers.set("X-OpenRouter-Title", config.appName);
|
|
3173
|
+
if (config.appCategories?.length) {
|
|
3174
|
+
headers.set("X-OpenRouter-Categories", config.appCategories.join(","));
|
|
3175
|
+
}
|
|
3176
|
+
const options = requestOptionsFor2(params, config.requestOptions);
|
|
3182
3177
|
if (options.routerMetadata ?? true)
|
|
3183
3178
|
headers.set("X-OpenRouter-Metadata", "enabled");
|
|
3184
3179
|
if (options.sessionId)
|
|
@@ -3338,13 +3333,13 @@ var assertAllowedModel2 = (model, allowedModels) => {
|
|
|
3338
3333
|
return;
|
|
3339
3334
|
throw new Error(`OpenRouter model "${model}" is not allowed`);
|
|
3340
3335
|
};
|
|
3341
|
-
var snapshotPolicy = (
|
|
3342
|
-
allowedModels:
|
|
3343
|
-
allowedPresets:
|
|
3336
|
+
var snapshotPolicy = (config) => ({
|
|
3337
|
+
allowedModels: config.allowedModels ? [...config.allowedModels] : undefined,
|
|
3338
|
+
allowedPresets: config.allowedPresets ? [...config.allowedPresets] : undefined
|
|
3344
3339
|
});
|
|
3345
|
-
var transformOpenRouterRequest = (
|
|
3346
|
-
const options = requestOptionsFor2(params,
|
|
3347
|
-
assertRequestOptions(options, allowedModels, allowedPresets,
|
|
3340
|
+
var transformOpenRouterRequest = (config, allowedModels, allowedPresets, body, params, skin = "openai") => {
|
|
3341
|
+
const options = requestOptionsFor2(params, config.requestOptions);
|
|
3342
|
+
assertRequestOptions(options, allowedModels, allowedPresets, config.allowedProviders);
|
|
3348
3343
|
const transformed = { ...body, ...options.extraBody };
|
|
3349
3344
|
if (options.audioOutput) {
|
|
3350
3345
|
transformed.audio = options.audioOutput;
|
|
@@ -3377,7 +3372,7 @@ var transformOpenRouterRequest = (config2, allowedModels, allowedPresets, body,
|
|
|
3377
3372
|
transformed.prompt_cache_key = options.promptCacheKey;
|
|
3378
3373
|
if (options.promptCacheOptions)
|
|
3379
3374
|
transformed.prompt_cache_options = options.promptCacheOptions;
|
|
3380
|
-
const routing = mapRouting({ ...
|
|
3375
|
+
const routing = mapRouting({ ...config.routing, ...options.routing }, config.allowedProviders);
|
|
3381
3376
|
if (Object.keys(routing).length > 0)
|
|
3382
3377
|
transformed.provider = routing;
|
|
3383
3378
|
if (options.fallbackModels)
|
|
@@ -3436,48 +3431,48 @@ var withOpenRouterPolicy = (provider, allowedModels, allowedPresets) => ({
|
|
|
3436
3431
|
return provider.stream(params);
|
|
3437
3432
|
}
|
|
3438
3433
|
});
|
|
3439
|
-
var openrouter = (
|
|
3440
|
-
assertRoutingPolicy(
|
|
3441
|
-
const { allowedModels, allowedPresets } = snapshotPolicy(
|
|
3434
|
+
var openrouter = (config) => {
|
|
3435
|
+
assertRoutingPolicy(config);
|
|
3436
|
+
const { allowedModels, allowedPresets } = snapshotPolicy(config);
|
|
3442
3437
|
const provider = openai({
|
|
3443
|
-
apiKey:
|
|
3444
|
-
baseUrl:
|
|
3445
|
-
fetch:
|
|
3446
|
-
headers: (params) => resolveAttributionHeaders(
|
|
3438
|
+
apiKey: config.apiKey,
|
|
3439
|
+
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL5,
|
|
3440
|
+
fetch: config.fetch,
|
|
3441
|
+
headers: (params) => resolveAttributionHeaders(config, params),
|
|
3447
3442
|
modelForCapabilities: modelForOpenAICapabilities,
|
|
3448
3443
|
providerName: "openrouter",
|
|
3449
|
-
tokenSource:
|
|
3450
|
-
transformRequestBody: (body, params) => transformOpenRouterRequest(
|
|
3444
|
+
tokenSource: config.tokenSource,
|
|
3445
|
+
transformRequestBody: (body, params) => transformOpenRouterRequest(config, allowedModels, allowedPresets, body, params)
|
|
3451
3446
|
});
|
|
3452
3447
|
return withOpenRouterPolicy(provider, allowedModels, allowedPresets);
|
|
3453
3448
|
};
|
|
3454
|
-
var openrouterResponses = (
|
|
3455
|
-
assertRoutingPolicy(
|
|
3456
|
-
const { allowedModels, allowedPresets } = snapshotPolicy(
|
|
3449
|
+
var openrouterResponses = (config) => {
|
|
3450
|
+
assertRoutingPolicy(config);
|
|
3451
|
+
const { allowedModels, allowedPresets } = snapshotPolicy(config);
|
|
3457
3452
|
const provider = openaiResponses({
|
|
3458
|
-
apiKey:
|
|
3459
|
-
baseUrl:
|
|
3460
|
-
fetch:
|
|
3461
|
-
headers: (params) => resolveAttributionHeaders(
|
|
3453
|
+
apiKey: config.apiKey,
|
|
3454
|
+
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL5,
|
|
3455
|
+
fetch: config.fetch,
|
|
3456
|
+
headers: (params) => resolveAttributionHeaders(config, params),
|
|
3462
3457
|
modelForCapabilities: modelForOpenAICapabilities,
|
|
3463
3458
|
providerName: "openrouter",
|
|
3464
|
-
tokenSource:
|
|
3465
|
-
transformRequestBody: (body, params) => transformOpenRouterRequest(
|
|
3459
|
+
tokenSource: config.tokenSource,
|
|
3460
|
+
transformRequestBody: (body, params) => transformOpenRouterRequest(config, allowedModels, allowedPresets, body, params)
|
|
3466
3461
|
});
|
|
3467
3462
|
return withOpenRouterPolicy(provider, allowedModels, allowedPresets);
|
|
3468
3463
|
};
|
|
3469
|
-
var openrouterMessages = (
|
|
3470
|
-
assertRoutingPolicy(
|
|
3471
|
-
const { allowedModels, allowedPresets } = snapshotPolicy(
|
|
3464
|
+
var openrouterMessages = (config) => {
|
|
3465
|
+
assertRoutingPolicy(config);
|
|
3466
|
+
const { allowedModels, allowedPresets } = snapshotPolicy(config);
|
|
3472
3467
|
const provider = anthropic({
|
|
3473
|
-
apiKey:
|
|
3468
|
+
apiKey: config.apiKey,
|
|
3474
3469
|
authStyle: "bearer",
|
|
3475
|
-
baseUrl:
|
|
3476
|
-
fetch:
|
|
3477
|
-
headers: (params) => resolveAttributionHeaders(
|
|
3470
|
+
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL5,
|
|
3471
|
+
fetch: config.fetch,
|
|
3472
|
+
headers: (params) => resolveAttributionHeaders(config, params),
|
|
3478
3473
|
providerName: "openrouter",
|
|
3479
|
-
tokenSource:
|
|
3480
|
-
transformRequestBody: (body, params) => transformOpenRouterRequest(
|
|
3474
|
+
tokenSource: config.tokenSource,
|
|
3475
|
+
transformRequestBody: (body, params) => transformOpenRouterRequest(config, allowedModels, allowedPresets, body, params, "messages")
|
|
3481
3476
|
});
|
|
3482
3477
|
return withOpenRouterPolicy(provider, allowedModels, allowedPresets);
|
|
3483
3478
|
};
|
|
@@ -3496,5 +3491,5 @@ export {
|
|
|
3496
3491
|
verifyOpenRouterWebhookSignature
|
|
3497
3492
|
};
|
|
3498
3493
|
|
|
3499
|
-
//# debugId=
|
|
3494
|
+
//# debugId=FD5B8AEE2525C1BD64756E2164756E21
|
|
3500
3495
|
//# sourceMappingURL=openrouter.js.map
|