2026-01-04 21:58:59 -05:00
|
|
|
import { NextResponse } from "next/server";
|
2026-02-21 04:42:46 -05:00
|
|
|
import { getProviderConnections, createProviderConnection, getProviderNodeById } from "@/models";
|
2026-01-04 21:58:59 -05:00
|
|
|
import { APIKEY_PROVIDERS } from "@/shared/constants/config";
|
2026-02-03 03:11:41 -05:00
|
|
|
import { isOpenAICompatibleProvider, isAnthropicCompatibleProvider } from "@/shared/constants/providers";
|
2026-01-04 21:58:59 -05:00
|
|
|
|
|
|
|
|
// GET /api/providers - List all connections
|
|
|
|
|
export async function GET() {
|
|
|
|
|
try {
|
|
|
|
|
const connections = await getProviderConnections();
|
|
|
|
|
|
|
|
|
|
// Hide sensitive fields
|
|
|
|
|
const safeConnections = connections.map(c => ({
|
|
|
|
|
...c,
|
|
|
|
|
apiKey: undefined,
|
|
|
|
|
accessToken: undefined,
|
|
|
|
|
refreshToken: undefined,
|
|
|
|
|
idToken: undefined,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
const { provider, apiKey, name, priority, globalPriority, defaultModel, testStatus } = body;
|
|
|
|
|
|
|
|
|
|
// Validation
|
2026-02-03 03:11:41 -05:00
|
|
|
const isValidProvider = APIKEY_PROVIDERS[provider] ||
|
|
|
|
|
isOpenAICompatibleProvider(provider) ||
|
|
|
|
|
isAnthropicCompatibleProvider(provider);
|
|
|
|
|
|
|
|
|
|
if (!provider || !isValidProvider) {
|
2026-01-04 21:58:59 -05:00
|
|
|
return NextResponse.json({ error: "Invalid provider" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
if (!apiKey) {
|
|
|
|
|
return NextResponse.json({ error: "API Key is required" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
if (!name) {
|
|
|
|
|
return NextResponse.json({ error: "Name is required" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-02 07:45:12 -05:00
|
|
|
let providerSpecificData = null;
|
|
|
|
|
|
|
|
|
|
if (isOpenAICompatibleProvider(provider)) {
|
|
|
|
|
const node = await getProviderNodeById(provider);
|
|
|
|
|
if (!node) {
|
|
|
|
|
return NextResponse.json({ error: "OpenAI Compatible node not found" }, { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const existingConnections = await getProviderConnections({ provider });
|
|
|
|
|
if (existingConnections.length > 0) {
|
|
|
|
|
return NextResponse.json({ error: "Only one connection is allowed for this OpenAI Compatible node" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const existingConnections = await getProviderConnections({ provider });
|
|
|
|
|
if (existingConnections.length > 0) {
|
|
|
|
|
return NextResponse.json({ error: "Only one connection is allowed for this Anthropic Compatible node" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
providerSpecificData = {
|
|
|
|
|
prefix: node.prefix,
|
|
|
|
|
baseUrl: node.baseUrl,
|
|
|
|
|
nodeName: node.name,
|
|
|
|
|
};
|
2026-02-02 07:45:12 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
const newConnection = await createProviderConnection({
|
|
|
|
|
provider,
|
|
|
|
|
authType: "apikey",
|
|
|
|
|
name,
|
|
|
|
|
apiKey,
|
|
|
|
|
priority: priority || 1,
|
|
|
|
|
globalPriority: globalPriority || null,
|
|
|
|
|
defaultModel: defaultModel || null,
|
2026-02-02 07:45:12 -05:00
|
|
|
providerSpecificData,
|
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 });
|
|
|
|
|
}
|
|
|
|
|
}
|