@absolutejs/ai 0.3.2 → 0.3.3
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 +6 -0
- package/README.md +19 -0
- package/changelog.json +20 -0
- package/dist/ai/client/index.js +1 -3
- package/dist/ai/client/index.js.map +2 -2
- package/dist/ai/index.js +7807 -7748
- 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 +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 +122 -124
- package/dist/ai/providers/openrouter.js.map +2 -2
- 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 +9 -7
- package/dist/angular/ai/index.js.map +2 -2
- 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(),
|
|
@@ -2481,16 +2479,16 @@ async function* parseSSEStream3(body, providerName, signal) {
|
|
|
2481
2479
|
reader.releaseLock();
|
|
2482
2480
|
}
|
|
2483
2481
|
}
|
|
2484
|
-
var fetchAndStream = async function* (baseUrl,
|
|
2482
|
+
var fetchAndStream = async function* (baseUrl, config, params, configuredMax, promptCaching, providerName) {
|
|
2485
2483
|
const builtBody = buildRequestBody2(params, configuredMax, promptCaching);
|
|
2486
|
-
const body =
|
|
2484
|
+
const body = config.transformRequestBody ? config.transformRequestBody(builtBody, params) : builtBody;
|
|
2487
2485
|
const target = `${baseUrl}/v1/messages`;
|
|
2488
|
-
const fetchImpl =
|
|
2489
|
-
const token =
|
|
2490
|
-
const suppliedHeaders = typeof
|
|
2486
|
+
const fetchImpl = config.fetch ?? fetch;
|
|
2487
|
+
const token = config.tokenSource ? await Promise.resolve(config.tokenSource()) : config.apiKey;
|
|
2488
|
+
const suppliedHeaders = typeof config.headers === "function" ? await config.headers(params) : config.headers ?? {};
|
|
2491
2489
|
const requestHeaders = new Headers(suppliedHeaders);
|
|
2492
2490
|
requestHeaders.set("Content-Type", "application/json");
|
|
2493
|
-
if (
|
|
2491
|
+
if (config.authStyle === "bearer") {
|
|
2494
2492
|
requestHeaders.set("Authorization", `Bearer ${token}`);
|
|
2495
2493
|
} else {
|
|
2496
2494
|
requestHeaders.set("anthropic-version", API_VERSION);
|
|
@@ -2516,24 +2514,24 @@ var fetchAndStream = async function* (baseUrl, config2, params, configuredMax, p
|
|
|
2516
2514
|
}
|
|
2517
2515
|
yield* parseSSEStream3(response.body, providerName, params.signal);
|
|
2518
2516
|
};
|
|
2519
|
-
var anthropic = (
|
|
2520
|
-
if (!
|
|
2517
|
+
var anthropic = (config) => {
|
|
2518
|
+
if (!config.apiKey && !config.tokenSource)
|
|
2521
2519
|
throw new Error("anthropic() requires either apiKey or tokenSource");
|
|
2522
|
-
const baseUrl =
|
|
2523
|
-
const configuredMax =
|
|
2524
|
-
const promptCaching =
|
|
2525
|
-
const providerName =
|
|
2520
|
+
const baseUrl = config.baseUrl ?? DEFAULT_BASE_URL3;
|
|
2521
|
+
const configuredMax = config.maxTokens ?? DEFAULT_MAX_TOKENS;
|
|
2522
|
+
const promptCaching = config.promptCaching ?? true;
|
|
2523
|
+
const providerName = config.providerName ?? "anthropic";
|
|
2526
2524
|
const capacityRequest = async (params, path, body) => {
|
|
2527
|
-
const token =
|
|
2528
|
-
const headers = new Headers(typeof
|
|
2525
|
+
const token = config.tokenSource ? await config.tokenSource() : config.apiKey;
|
|
2526
|
+
const headers = new Headers(typeof config.headers === "function" ? await config.headers(params) : config.headers);
|
|
2529
2527
|
headers.set("Content-Type", "application/json");
|
|
2530
|
-
if (
|
|
2528
|
+
if (config.authStyle === "bearer")
|
|
2531
2529
|
headers.set("Authorization", `Bearer ${token}`);
|
|
2532
2530
|
else {
|
|
2533
2531
|
headers.set("anthropic-version", API_VERSION);
|
|
2534
2532
|
headers.set("x-api-key", token);
|
|
2535
2533
|
}
|
|
2536
|
-
return capacityJson(await (
|
|
2534
|
+
return capacityJson(await (config.fetch ?? fetch)(`${baseUrl}${path}`, {
|
|
2537
2535
|
method: body ? "POST" : "GET",
|
|
2538
2536
|
headers,
|
|
2539
2537
|
body: body ? JSON.stringify(body) : undefined,
|
|
@@ -2542,7 +2540,7 @@ var anthropic = (config2) => {
|
|
|
2542
2540
|
};
|
|
2543
2541
|
const countingRequest = (params) => {
|
|
2544
2542
|
const built = buildRequestBody2(params, configuredMax, promptCaching);
|
|
2545
|
-
const body =
|
|
2543
|
+
const body = config.transformRequestBody ? config.transformRequestBody(built, params) : built;
|
|
2546
2544
|
const counted = {};
|
|
2547
2545
|
for (const key of [
|
|
2548
2546
|
"model",
|
|
@@ -2572,7 +2570,7 @@ var anthropic = (config2) => {
|
|
|
2572
2570
|
const inputCapacity = {
|
|
2573
2571
|
outputTokens: (params) => {
|
|
2574
2572
|
const built = buildRequestBody2(params, configuredMax, promptCaching);
|
|
2575
|
-
const body =
|
|
2573
|
+
const body = config.transformRequestBody ? config.transformRequestBody(built, params) : built;
|
|
2576
2574
|
return positiveTokenLimit(body.max_tokens);
|
|
2577
2575
|
},
|
|
2578
2576
|
getLimits: cacheModelLimits(async (params) => {
|
|
@@ -2583,7 +2581,7 @@ var anthropic = (config2) => {
|
|
|
2583
2581
|
contextWindowTokens: context,
|
|
2584
2582
|
maxOutputTokens: positiveTokenLimit(data.max_tokens)
|
|
2585
2583
|
};
|
|
2586
|
-
},
|
|
2584
|
+
}, config.tokenSource || typeof config.headers === "function" ? 0 : 300000),
|
|
2587
2585
|
countScope: (params) => countingRequest(params).scope,
|
|
2588
2586
|
countTokens: async (params) => {
|
|
2589
2587
|
const { counted } = countingRequest(params);
|
|
@@ -2592,7 +2590,7 @@ var anthropic = (config2) => {
|
|
|
2592
2590
|
};
|
|
2593
2591
|
return instrumentAIProvider({
|
|
2594
2592
|
inputCapacity,
|
|
2595
|
-
stream: (params) => fetchAndStream(baseUrl,
|
|
2593
|
+
stream: (params) => fetchAndStream(baseUrl, config, params, configuredMax, promptCaching, providerName)
|
|
2596
2594
|
}, providerName);
|
|
2597
2595
|
};
|
|
2598
2596
|
|
|
@@ -2783,8 +2781,8 @@ var constantTimeEqual = (left, right) => {
|
|
|
2783
2781
|
};
|
|
2784
2782
|
var verifyOpenRouterWebhookSignature = async (options) => {
|
|
2785
2783
|
const fields = new Map(options.header.split(",").map((part) => {
|
|
2786
|
-
const [
|
|
2787
|
-
return [
|
|
2784
|
+
const [key, ...rest] = part.trim().split("=");
|
|
2785
|
+
return [key, rest.join("=")];
|
|
2788
2786
|
}));
|
|
2789
2787
|
const timestamp = fields.get("t");
|
|
2790
2788
|
const supplied = fields.get("v1");
|
|
@@ -2805,17 +2803,17 @@ var verifyOpenRouterWebhookSignature = async (options) => {
|
|
|
2805
2803
|
const suppliedBytes = hexToBytes(supplied);
|
|
2806
2804
|
return suppliedBytes ? constantTimeEqual(expected, suppliedBytes) : false;
|
|
2807
2805
|
};
|
|
2808
|
-
var createOpenRouterClient = (
|
|
2809
|
-
if (!
|
|
2806
|
+
var createOpenRouterClient = (config) => {
|
|
2807
|
+
if (!config.apiKey && !config.tokenSource)
|
|
2810
2808
|
throw new Error("createOpenRouterClient() requires either apiKey or tokenSource");
|
|
2811
|
-
const baseUrl = (
|
|
2812
|
-
const batchBaseUrl = (
|
|
2813
|
-
const fetchImpl =
|
|
2814
|
-
const allowedModels =
|
|
2815
|
-
const defaultWorkspaceId =
|
|
2809
|
+
const baseUrl = (config.baseUrl ?? DEFAULT_BASE_URL4).replace(/\/$/, "");
|
|
2810
|
+
const batchBaseUrl = (config.batchBaseUrl ?? (config.baseUrl ? new URL("../beta", `${baseUrl}/`).toString() : DEFAULT_BATCH_BASE_URL)).replace(/\/$/, "");
|
|
2811
|
+
const fetchImpl = config.fetch ?? globalThis.fetch;
|
|
2812
|
+
const allowedModels = config.allowedModels ? [...config.allowedModels] : undefined;
|
|
2813
|
+
const defaultWorkspaceId = config.workspaceId;
|
|
2816
2814
|
const requestRawAt = async (rootUrl, path, options = {}) => {
|
|
2817
|
-
const token =
|
|
2818
|
-
const suppliedHeaders = typeof
|
|
2815
|
+
const token = config.tokenSource ? await Promise.resolve(config.tokenSource()) : config.apiKey;
|
|
2816
|
+
const suppliedHeaders = typeof config.headers === "function" ? await config.headers() : config.headers ?? {};
|
|
2819
2817
|
const headers = new Headers(suppliedHeaders);
|
|
2820
2818
|
new Headers(options.headers).forEach((value, key) => headers.set(key, value));
|
|
2821
2819
|
headers.set("Authorization", `Bearer ${token}`);
|
|
@@ -3120,20 +3118,20 @@ var assertNonEmptyPolicy = (label, value) => {
|
|
|
3120
3118
|
throw new Error(`openrouter() ${label} must not be empty`);
|
|
3121
3119
|
}
|
|
3122
3120
|
};
|
|
3123
|
-
var assertRoutingPolicy = (
|
|
3124
|
-
assertNonEmptyPolicy("allowedProviders",
|
|
3125
|
-
assertNonEmptyPolicy("routing.only",
|
|
3126
|
-
assertNonEmptyPolicy("allowedPresets",
|
|
3127
|
-
if (
|
|
3121
|
+
var assertRoutingPolicy = (config) => {
|
|
3122
|
+
assertNonEmptyPolicy("allowedProviders", config.allowedProviders);
|
|
3123
|
+
assertNonEmptyPolicy("routing.only", config.routing?.only);
|
|
3124
|
+
assertNonEmptyPolicy("allowedPresets", config.allowedPresets);
|
|
3125
|
+
if (config.appCategories && config.appCategories.length > MAX_APP_CATEGORIES) {
|
|
3128
3126
|
throw new Error("openrouter() appCategories supports at most 2 entries");
|
|
3129
3127
|
}
|
|
3130
|
-
if (!
|
|
3128
|
+
if (!config.allowedProviders)
|
|
3131
3129
|
return;
|
|
3132
3130
|
const selected = [
|
|
3133
|
-
...
|
|
3134
|
-
...
|
|
3131
|
+
...config.routing?.only ?? [],
|
|
3132
|
+
...config.routing?.order ?? []
|
|
3135
3133
|
];
|
|
3136
|
-
const denied = selected.find((provider) => !
|
|
3134
|
+
const denied = selected.find((provider) => !config.allowedProviders.some((rule) => providerMatchesRule(provider, rule)));
|
|
3137
3135
|
if (denied) {
|
|
3138
3136
|
throw new Error(`openrouter() provider "${denied}" is outside allowedProviders`);
|
|
3139
3137
|
}
|
|
@@ -3186,17 +3184,17 @@ var requestOptionsFor2 = (params, defaults) => {
|
|
|
3186
3184
|
...supplied
|
|
3187
3185
|
};
|
|
3188
3186
|
};
|
|
3189
|
-
var resolveAttributionHeaders = async (
|
|
3190
|
-
const supplied = typeof
|
|
3187
|
+
var resolveAttributionHeaders = async (config, params) => {
|
|
3188
|
+
const supplied = typeof config.headers === "function" ? await config.headers() : config.headers ?? {};
|
|
3191
3189
|
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,
|
|
3190
|
+
if (config.appUrl)
|
|
3191
|
+
headers.set("HTTP-Referer", config.appUrl);
|
|
3192
|
+
if (config.appName)
|
|
3193
|
+
headers.set("X-OpenRouter-Title", config.appName);
|
|
3194
|
+
if (config.appCategories?.length) {
|
|
3195
|
+
headers.set("X-OpenRouter-Categories", config.appCategories.join(","));
|
|
3196
|
+
}
|
|
3197
|
+
const options = requestOptionsFor2(params, config.requestOptions);
|
|
3200
3198
|
if (options.routerMetadata ?? true)
|
|
3201
3199
|
headers.set("X-OpenRouter-Metadata", "enabled");
|
|
3202
3200
|
if (options.sessionId)
|
|
@@ -3356,13 +3354,13 @@ var assertAllowedModel2 = (model, allowedModels) => {
|
|
|
3356
3354
|
return;
|
|
3357
3355
|
throw new Error(`OpenRouter model "${model}" is not allowed`);
|
|
3358
3356
|
};
|
|
3359
|
-
var snapshotPolicy = (
|
|
3360
|
-
allowedModels:
|
|
3361
|
-
allowedPresets:
|
|
3357
|
+
var snapshotPolicy = (config) => ({
|
|
3358
|
+
allowedModels: config.allowedModels ? [...config.allowedModels] : undefined,
|
|
3359
|
+
allowedPresets: config.allowedPresets ? [...config.allowedPresets] : undefined
|
|
3362
3360
|
});
|
|
3363
|
-
var transformOpenRouterRequest = (
|
|
3364
|
-
const options = requestOptionsFor2(params,
|
|
3365
|
-
assertRequestOptions(options, allowedModels, allowedPresets,
|
|
3361
|
+
var transformOpenRouterRequest = (config, allowedModels, allowedPresets, body, params, skin = "openai") => {
|
|
3362
|
+
const options = requestOptionsFor2(params, config.requestOptions);
|
|
3363
|
+
assertRequestOptions(options, allowedModels, allowedPresets, config.allowedProviders);
|
|
3366
3364
|
const transformed = { ...body, ...options.extraBody };
|
|
3367
3365
|
if (options.audioOutput) {
|
|
3368
3366
|
transformed.audio = options.audioOutput;
|
|
@@ -3395,7 +3393,7 @@ var transformOpenRouterRequest = (config2, allowedModels, allowedPresets, body,
|
|
|
3395
3393
|
transformed.prompt_cache_key = options.promptCacheKey;
|
|
3396
3394
|
if (options.promptCacheOptions)
|
|
3397
3395
|
transformed.prompt_cache_options = options.promptCacheOptions;
|
|
3398
|
-
const routing = mapRouting({ ...
|
|
3396
|
+
const routing = mapRouting({ ...config.routing, ...options.routing }, config.allowedProviders);
|
|
3399
3397
|
if (Object.keys(routing).length > 0)
|
|
3400
3398
|
transformed.provider = routing;
|
|
3401
3399
|
if (options.fallbackModels)
|
|
@@ -3454,48 +3452,48 @@ var withOpenRouterPolicy = (provider, allowedModels, allowedPresets) => ({
|
|
|
3454
3452
|
return provider.stream(params);
|
|
3455
3453
|
}
|
|
3456
3454
|
});
|
|
3457
|
-
var openrouter = (
|
|
3458
|
-
assertRoutingPolicy(
|
|
3459
|
-
const { allowedModels, allowedPresets } = snapshotPolicy(
|
|
3455
|
+
var openrouter = (config) => {
|
|
3456
|
+
assertRoutingPolicy(config);
|
|
3457
|
+
const { allowedModels, allowedPresets } = snapshotPolicy(config);
|
|
3460
3458
|
const provider = openai({
|
|
3461
|
-
apiKey:
|
|
3462
|
-
baseUrl:
|
|
3463
|
-
fetch:
|
|
3464
|
-
headers: (params) => resolveAttributionHeaders(
|
|
3459
|
+
apiKey: config.apiKey,
|
|
3460
|
+
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL5,
|
|
3461
|
+
fetch: config.fetch,
|
|
3462
|
+
headers: (params) => resolveAttributionHeaders(config, params),
|
|
3465
3463
|
modelForCapabilities: modelForOpenAICapabilities,
|
|
3466
3464
|
providerName: "openrouter",
|
|
3467
|
-
tokenSource:
|
|
3468
|
-
transformRequestBody: (body, params) => transformOpenRouterRequest(
|
|
3465
|
+
tokenSource: config.tokenSource,
|
|
3466
|
+
transformRequestBody: (body, params) => transformOpenRouterRequest(config, allowedModels, allowedPresets, body, params)
|
|
3469
3467
|
});
|
|
3470
3468
|
return withOpenRouterPolicy(provider, allowedModels, allowedPresets);
|
|
3471
3469
|
};
|
|
3472
|
-
var openrouterResponses = (
|
|
3473
|
-
assertRoutingPolicy(
|
|
3474
|
-
const { allowedModels, allowedPresets } = snapshotPolicy(
|
|
3470
|
+
var openrouterResponses = (config) => {
|
|
3471
|
+
assertRoutingPolicy(config);
|
|
3472
|
+
const { allowedModels, allowedPresets } = snapshotPolicy(config);
|
|
3475
3473
|
const provider = openaiResponses({
|
|
3476
|
-
apiKey:
|
|
3477
|
-
baseUrl:
|
|
3478
|
-
fetch:
|
|
3479
|
-
headers: (params) => resolveAttributionHeaders(
|
|
3474
|
+
apiKey: config.apiKey,
|
|
3475
|
+
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL5,
|
|
3476
|
+
fetch: config.fetch,
|
|
3477
|
+
headers: (params) => resolveAttributionHeaders(config, params),
|
|
3480
3478
|
modelForCapabilities: modelForOpenAICapabilities,
|
|
3481
3479
|
providerName: "openrouter",
|
|
3482
|
-
tokenSource:
|
|
3483
|
-
transformRequestBody: (body, params) => transformOpenRouterRequest(
|
|
3480
|
+
tokenSource: config.tokenSource,
|
|
3481
|
+
transformRequestBody: (body, params) => transformOpenRouterRequest(config, allowedModels, allowedPresets, body, params)
|
|
3484
3482
|
});
|
|
3485
3483
|
return withOpenRouterPolicy(provider, allowedModels, allowedPresets);
|
|
3486
3484
|
};
|
|
3487
|
-
var openrouterMessages = (
|
|
3488
|
-
assertRoutingPolicy(
|
|
3489
|
-
const { allowedModels, allowedPresets } = snapshotPolicy(
|
|
3485
|
+
var openrouterMessages = (config) => {
|
|
3486
|
+
assertRoutingPolicy(config);
|
|
3487
|
+
const { allowedModels, allowedPresets } = snapshotPolicy(config);
|
|
3490
3488
|
const provider = anthropic({
|
|
3491
|
-
apiKey:
|
|
3489
|
+
apiKey: config.apiKey,
|
|
3492
3490
|
authStyle: "bearer",
|
|
3493
|
-
baseUrl:
|
|
3494
|
-
fetch:
|
|
3495
|
-
headers: (params) => resolveAttributionHeaders(
|
|
3491
|
+
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL5,
|
|
3492
|
+
fetch: config.fetch,
|
|
3493
|
+
headers: (params) => resolveAttributionHeaders(config, params),
|
|
3496
3494
|
providerName: "openrouter",
|
|
3497
|
-
tokenSource:
|
|
3498
|
-
transformRequestBody: (body, params) => transformOpenRouterRequest(
|
|
3495
|
+
tokenSource: config.tokenSource,
|
|
3496
|
+
transformRequestBody: (body, params) => transformOpenRouterRequest(config, allowedModels, allowedPresets, body, params, "messages")
|
|
3499
3497
|
});
|
|
3500
3498
|
return withOpenRouterPolicy(provider, allowedModels, allowedPresets);
|
|
3501
3499
|
};
|
|
@@ -3514,5 +3512,5 @@ export {
|
|
|
3514
3512
|
verifyOpenRouterWebhookSignature
|
|
3515
3513
|
};
|
|
3516
3514
|
|
|
3517
|
-
//# debugId=
|
|
3515
|
+
//# debugId=B813A4B59FA2961264756E2164756E21
|
|
3518
3516
|
//# sourceMappingURL=openrouter.js.map
|