2026-01-04 21:58:59 -05:00
|
|
|
import { NextResponse } from "next/server";
|
2026-03-09 04:46:06 -04:00
|
|
|
import {
|
|
|
|
|
getProviderConnections,
|
|
|
|
|
createProviderConnection,
|
|
|
|
|
getProviderNodeById,
|
|
|
|
|
getProviderNodes,
|
|
|
|
|
getProxyPoolById,
|
|
|
|
|
} from "@/models";
|
2026-01-04 21:58:59 -05:00
|
|
|
import { APIKEY_PROVIDERS } from "@/shared/constants/config";
|
2026-05-08 23:05:22 -04:00
|
|
|
import { AI_PROVIDERS, FREE_TIER_PROVIDERS, WEB_COOKIE_PROVIDERS, isOpenAICompatibleProvider, isAnthropicCompatibleProvider, isCustomEmbeddingProvider } from "@/shared/constants/providers";
|
|
|
|
|
import { normalizeProviderId, normalizeProviderSpecificData } from "@/lib/providerNormalization";
|
2026-01-04 21:58:59 -05:00
|
|
|
|
2026-03-06 04:41:02 -05:00
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
|
|
2026-03-09 04:46:06 -04:00
|
|
|
function normalizeProxyConfig(body = {}) {
|
|
|
|
|
const enabled = body?.connectionProxyEnabled === true;
|
|
|
|
|
const url = typeof body?.connectionProxyUrl === "string" ? body.connectionProxyUrl.trim() : "";
|
|
|
|
|
const noProxy = typeof body?.connectionNoProxy === "string" ? body.connectionNoProxy.trim() : "";
|
|
|
|
|
|
|
|
|
|
if (enabled && !url) {
|
|
|
|
|
return { error: "Connection proxy URL is required when connection proxy is enabled" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
connectionProxyEnabled: enabled,
|
|
|
|
|
connectionProxyUrl: url,
|
|
|
|
|
connectionNoProxy: noProxy,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function normalizeProxyPoolId(proxyPoolId) {
|
|
|
|
|
if (proxyPoolId === undefined || proxyPoolId === null || proxyPoolId === "" || proxyPoolId === "__none__") {
|
|
|
|
|
return { proxyPoolId: null };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const normalizedId = String(proxyPoolId).trim();
|
|
|
|
|
if (!normalizedId) {
|
|
|
|
|
return { proxyPoolId: null };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const proxyPool = await getProxyPoolById(normalizedId);
|
|
|
|
|
if (!proxyPool) {
|
|
|
|
|
return { error: "Proxy pool not found" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { proxyPoolId: normalizedId };
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
// GET /api/providers - List all connections
|
|
|
|
|
export async function GET() {
|
|
|
|
|
try {
|
|
|
|
|
const connections = await getProviderConnections();
|
2026-02-27 22:04:57 -05:00
|
|
|
|
|
|
|
|
// Build nodeNameMap for compatible providers (id → name)
|
|
|
|
|
let nodeNameMap = {};
|
|
|
|
|
try {
|
|
|
|
|
const nodes = await getProviderNodes();
|
|
|
|
|
for (const node of nodes) {
|
|
|
|
|
if (node.id && node.name) nodeNameMap[node.id] = node.name;
|
|
|
|
|
}
|
2026-03-06 04:41:02 -05:00
|
|
|
} catch { }
|
|
|
|
|
|
2026-02-27 22:04:57 -05:00
|
|
|
// Hide sensitive fields, enrich name for compatible providers
|
|
|
|
|
const safeConnections = connections.map(c => {
|
|
|
|
|
const isCompatible = isOpenAICompatibleProvider(c.provider) || isAnthropicCompatibleProvider(c.provider);
|
|
|
|
|
const name = isCompatible
|
|
|
|
|
? (nodeNameMap[c.provider] || c.providerSpecificData?.nodeName || c.provider)
|
|
|
|
|
: c.name;
|
|
|
|
|
return {
|
|
|
|
|
...c,
|
|
|
|
|
name,
|
|
|
|
|
apiKey: undefined,
|
|
|
|
|
accessToken: undefined,
|
|
|
|
|
refreshToken: undefined,
|
|
|
|
|
idToken: undefined,
|
|
|
|
|
};
|
|
|
|
|
});
|
2026-01-04 21:58:59 -05:00
|
|
|
|
|
|
|
|
return NextResponse.json({ connections: safeConnections });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log("Error fetching providers:", error);
|
|
|
|
|
return NextResponse.json({ error: "Failed to fetch providers" }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST /api/providers - Create new connection (API Key only, OAuth via separate flow)
|
|
|
|
|
export async function POST(request) {
|
|
|
|
|
try {
|
|
|
|
|
const body = await request.json();
|
2026-05-08 23:05:22 -04:00
|
|
|
const provider = normalizeProviderId(body.provider);
|
|
|
|
|
const { apiKey, name, displayName, priority, globalPriority, defaultModel, testStatus } = body;
|
2026-03-09 04:46:06 -04:00
|
|
|
const proxyConfig = normalizeProxyConfig(body);
|
|
|
|
|
if (proxyConfig.error) {
|
|
|
|
|
return NextResponse.json({ error: proxyConfig.error }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const proxyPoolResult = await normalizeProxyPoolId(body.proxyPoolId);
|
|
|
|
|
if (proxyPoolResult.error) {
|
|
|
|
|
return NextResponse.json({ error: proxyPoolResult.error }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
const proxyPoolId = proxyPoolResult.proxyPoolId;
|
2026-01-04 21:58:59 -05:00
|
|
|
|
|
|
|
|
// Validation
|
2026-04-22 00:58:53 -04:00
|
|
|
const isWebCookieProvider = !!WEB_COOKIE_PROVIDERS[provider];
|
2026-03-06 04:41:02 -05:00
|
|
|
const isValidProvider = APIKEY_PROVIDERS[provider] ||
|
2026-03-31 04:41:52 -04:00
|
|
|
FREE_TIER_PROVIDERS[provider] ||
|
2026-04-22 00:58:53 -04:00
|
|
|
isWebCookieProvider ||
|
2026-03-06 04:41:02 -05:00
|
|
|
isOpenAICompatibleProvider(provider) ||
|
2026-04-25 05:22:30 -04:00
|
|
|
isAnthropicCompatibleProvider(provider) ||
|
|
|
|
|
isCustomEmbeddingProvider(provider);
|
2026-02-03 03:11:41 -05:00
|
|
|
|
|
|
|
|
if (!provider || !isValidProvider) {
|
2026-01-04 21:58:59 -05:00
|
|
|
return NextResponse.json({ error: "Invalid provider" }, { status: 400 });
|
|
|
|
|
}
|
feat: add STT support, Gemini TTS, and expand usage tracking
- Speech-to-Text: full pipeline with sttCore handler, /v1/audio/transcriptions
endpoint, sttConfig for OpenAI, Gemini, Groq, Deepgram, AssemblyAI,
HuggingFace, NVIDIA Parakeet; new 9router-stt skill
- Gemini TTS: add gemini provider with 30 prebuilt voices and TTS_PROVIDER_CONFIG
- Usage: implement GLM (intl/cn) and MiniMax (intl/cn) quota fetchers; refactor
Gemini CLI usage to use retrieveUserQuota with per-model buckets
- Disabled models: lowdb-backed disabledModelsDb + /api/models/disabled route
- Header search: reusable Zustand store (headerSearchStore) wired into Header
- CLI tools: add Claude Cowork tool card and cowork-settings API
- Providers: introduce mediaPriority sorting in getProvidersByKind, add
Kimi K2.6, reorder hermes, drop qwen STT kind
- UI: expand media-providers/[kind]/[id] page (+314), enhance OAuthModal,
ModelSelectModal, ProviderTopology, ProxyPools, ProviderLimits
- Assets: refresh provider PNGs (alicode, byteplus, cloudflare-ai, nvidia,
ollama, vertex, volcengine-ark) and add aws-polly, fal-ai, jina-ai, recraft,
runwayml, stability-ai, topaz, black-forest-labs
2026-05-04 23:32:59 -04:00
|
|
|
if (!apiKey && provider !== "ollama-local") {
|
2026-04-22 00:58:53 -04:00
|
|
|
return NextResponse.json({ error: `${isWebCookieProvider ? "Cookie value" : "API Key"} is required` }, { status: 400 });
|
2026-01-04 21:58:59 -05:00
|
|
|
}
|
2026-05-08 23:05:22 -04:00
|
|
|
const connectionName = name || displayName || AI_PROVIDERS[provider]?.name;
|
|
|
|
|
if (!connectionName) {
|
2026-01-04 21:58:59 -05:00
|
|
|
return NextResponse.json({ error: "Name is required" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 23:05:22 -04:00
|
|
|
let providerSpecificData = normalizeProviderSpecificData(provider, body, body.providerSpecificData);
|
2026-02-02 07:45:12 -05:00
|
|
|
|
|
|
|
|
if (isOpenAICompatibleProvider(provider)) {
|
|
|
|
|
const node = await getProviderNodeById(provider);
|
|
|
|
|
if (!node) {
|
|
|
|
|
return NextResponse.json({ error: "OpenAI Compatible node not found" }, { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
providerSpecificData = {
|
|
|
|
|
prefix: node.prefix,
|
|
|
|
|
apiType: node.apiType,
|
|
|
|
|
baseUrl: node.baseUrl,
|
|
|
|
|
nodeName: node.name,
|
|
|
|
|
};
|
2026-02-03 03:11:41 -05:00
|
|
|
} else if (isAnthropicCompatibleProvider(provider)) {
|
|
|
|
|
const node = await getProviderNodeById(provider);
|
|
|
|
|
if (!node) {
|
|
|
|
|
return NextResponse.json({ error: "Anthropic Compatible node not found" }, { status: 404 });
|
|
|
|
|
}
|
2026-04-25 05:22:30 -04:00
|
|
|
providerSpecificData = {
|
|
|
|
|
prefix: node.prefix,
|
|
|
|
|
baseUrl: node.baseUrl,
|
|
|
|
|
nodeName: node.name,
|
|
|
|
|
};
|
|
|
|
|
} else if (isCustomEmbeddingProvider(provider)) {
|
|
|
|
|
const node = await getProviderNodeById(provider);
|
|
|
|
|
if (!node) {
|
|
|
|
|
return NextResponse.json({ error: "Custom Embedding node not found" }, { status: 404 });
|
|
|
|
|
}
|
2026-02-03 03:11:41 -05:00
|
|
|
providerSpecificData = {
|
|
|
|
|
prefix: node.prefix,
|
|
|
|
|
baseUrl: node.baseUrl,
|
|
|
|
|
nodeName: node.name,
|
|
|
|
|
};
|
2026-02-02 07:45:12 -05:00
|
|
|
}
|
|
|
|
|
|
2026-03-09 04:46:06 -04:00
|
|
|
const mergedProviderSpecificData = {
|
|
|
|
|
...(providerSpecificData || {}),
|
|
|
|
|
connectionProxyEnabled: proxyConfig.connectionProxyEnabled,
|
|
|
|
|
connectionProxyUrl: proxyConfig.connectionProxyUrl,
|
|
|
|
|
connectionNoProxy: proxyConfig.connectionNoProxy,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (proxyPoolId !== null) {
|
|
|
|
|
mergedProviderSpecificData.proxyPoolId = proxyPoolId;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
const newConnection = await createProviderConnection({
|
|
|
|
|
provider,
|
2026-04-22 00:58:53 -04:00
|
|
|
authType: isWebCookieProvider ? "cookie" : "apikey",
|
2026-05-08 23:05:22 -04:00
|
|
|
name: connectionName,
|
feat: add STT support, Gemini TTS, and expand usage tracking
- Speech-to-Text: full pipeline with sttCore handler, /v1/audio/transcriptions
endpoint, sttConfig for OpenAI, Gemini, Groq, Deepgram, AssemblyAI,
HuggingFace, NVIDIA Parakeet; new 9router-stt skill
- Gemini TTS: add gemini provider with 30 prebuilt voices and TTS_PROVIDER_CONFIG
- Usage: implement GLM (intl/cn) and MiniMax (intl/cn) quota fetchers; refactor
Gemini CLI usage to use retrieveUserQuota with per-model buckets
- Disabled models: lowdb-backed disabledModelsDb + /api/models/disabled route
- Header search: reusable Zustand store (headerSearchStore) wired into Header
- CLI tools: add Claude Cowork tool card and cowork-settings API
- Providers: introduce mediaPriority sorting in getProvidersByKind, add
Kimi K2.6, reorder hermes, drop qwen STT kind
- UI: expand media-providers/[kind]/[id] page (+314), enhance OAuthModal,
ModelSelectModal, ProviderTopology, ProxyPools, ProviderLimits
- Assets: refresh provider PNGs (alicode, byteplus, cloudflare-ai, nvidia,
ollama, vertex, volcengine-ark) and add aws-polly, fal-ai, jina-ai, recraft,
runwayml, stability-ai, topaz, black-forest-labs
2026-05-04 23:32:59 -04:00
|
|
|
apiKey: apiKey || "",
|
2026-01-04 21:58:59 -05:00
|
|
|
priority: priority || 1,
|
|
|
|
|
globalPriority: globalPriority || null,
|
|
|
|
|
defaultModel: defaultModel || null,
|
2026-03-09 04:46:06 -04:00
|
|
|
providerSpecificData: mergedProviderSpecificData,
|
2026-01-04 21:58:59 -05:00
|
|
|
isActive: true,
|
|
|
|
|
testStatus: testStatus || "unknown",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Hide sensitive fields
|
|
|
|
|
const result = { ...newConnection };
|
|
|
|
|
delete result.apiKey;
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({ connection: result }, { status: 201 });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log("Error creating provider:", error);
|
|
|
|
|
return NextResponse.json({ error: "Failed to create provider" }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
}
|