85 lines
2.6 KiB
JavaScript
85 lines
2.6 KiB
JavaScript
|
|
import { NextResponse } from "next/server";
|
||
|
|
import { getProviderConnections, createProviderConnection, isCloudEnabled } from "@/models";
|
||
|
|
import { APIKEY_PROVIDERS } from "@/shared/constants/config";
|
||
|
|
import { getConsistentMachineId } from "@/shared/utils/machineId";
|
||
|
|
import { syncToCloud } from "@/app/api/sync/cloud/route";
|
||
|
|
|
||
|
|
// 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
|
||
|
|
if (!provider || !APIKEY_PROVIDERS[provider]) {
|
||
|
|
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 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const newConnection = await createProviderConnection({
|
||
|
|
provider,
|
||
|
|
authType: "apikey",
|
||
|
|
name,
|
||
|
|
apiKey,
|
||
|
|
priority: priority || 1,
|
||
|
|
globalPriority: globalPriority || null,
|
||
|
|
defaultModel: defaultModel || null,
|
||
|
|
isActive: true,
|
||
|
|
testStatus: testStatus || "unknown",
|
||
|
|
});
|
||
|
|
|
||
|
|
// Hide sensitive fields
|
||
|
|
const result = { ...newConnection };
|
||
|
|
delete result.apiKey;
|
||
|
|
|
||
|
|
// Auto sync to Cloud if enabled
|
||
|
|
await syncToCloudIfEnabled();
|
||
|
|
|
||
|
|
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 });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Sync to Cloud if enabled
|
||
|
|
*/
|
||
|
|
async function syncToCloudIfEnabled() {
|
||
|
|
try {
|
||
|
|
const cloudEnabled = await isCloudEnabled();
|
||
|
|
if (!cloudEnabled) return;
|
||
|
|
|
||
|
|
const machineId = await getConsistentMachineId();
|
||
|
|
await syncToCloud(machineId);
|
||
|
|
} catch (error) {
|
||
|
|
console.log("Error syncing providers to cloud:", error);
|
||
|
|
}
|
||
|
|
}
|