Initial commit

This commit is contained in:
decolua
2026-01-05 09:58:59 +07:00
commit 3857598de4
159 changed files with 14537 additions and 0 deletions
+148
View File
@@ -0,0 +1,148 @@
import { NextResponse } from "next/server";
import { getProviderConnectionById } from "@/models";
// Provider models endpoints configuration
const PROVIDER_MODELS_CONFIG = {
claude: {
url: "https://api.anthropic.com/v1/models",
method: "GET",
headers: {
"Anthropic-Version": "2023-06-01",
"Content-Type": "application/json"
},
authHeader: "x-api-key",
parseResponse: (data) => data.data || []
},
gemini: {
url: "https://generativelanguage.googleapis.com/v1beta/models",
method: "GET",
headers: { "Content-Type": "application/json" },
authQuery: "key", // Use query param for API key
parseResponse: (data) => data.models || []
},
"gemini-cli": {
url: "https://generativelanguage.googleapis.com/v1beta/models",
method: "GET",
headers: { "Content-Type": "application/json" },
authHeader: "Authorization",
authPrefix: "Bearer ",
parseResponse: (data) => data.models || []
},
qwen: {
url: "https://portal.qwen.ai/v1/models",
method: "GET",
headers: { "Content-Type": "application/json" },
authHeader: "Authorization",
authPrefix: "Bearer ",
parseResponse: (data) => data.data || []
},
antigravity: {
url: "https://daily-cloudcode-pa.sandbox.googleapis.com/v1internal:models",
method: "POST",
headers: { "Content-Type": "application/json" },
authHeader: "Authorization",
authPrefix: "Bearer ",
body: {},
parseResponse: (data) => data.models || []
},
openai: {
url: "https://api.openai.com/v1/models",
method: "GET",
headers: { "Content-Type": "application/json" },
authHeader: "Authorization",
authPrefix: "Bearer ",
parseResponse: (data) => data.data || []
},
openrouter: {
url: "https://openrouter.ai/api/v1/models",
method: "GET",
headers: { "Content-Type": "application/json" },
authHeader: "Authorization",
authPrefix: "Bearer ",
parseResponse: (data) => data.data || []
},
anthropic: {
url: "https://api.anthropic.com/v1/models",
method: "GET",
headers: {
"Anthropic-Version": "2023-06-01",
"Content-Type": "application/json"
},
authHeader: "x-api-key",
parseResponse: (data) => data.data || []
}
};
/**
* GET /api/providers/[id]/models - Get models list from provider
*/
export async function GET(request, { params }) {
try {
const { id } = await params;
const connection = await getProviderConnectionById(id);
if (!connection) {
return NextResponse.json({ error: "Connection not found" }, { status: 404 });
}
const config = PROVIDER_MODELS_CONFIG[connection.provider];
if (!config) {
return NextResponse.json(
{ error: `Provider ${connection.provider} does not support models listing` },
{ status: 400 }
);
}
// Get auth token
const token = connection.accessToken || connection.apiKey;
if (!token) {
return NextResponse.json({ error: "No valid token found" }, { status: 401 });
}
// Build request URL
let url = config.url;
if (config.authQuery) {
url += `?${config.authQuery}=${token}`;
}
// Build headers
const headers = { ...config.headers };
if (config.authHeader && !config.authQuery) {
headers[config.authHeader] = (config.authPrefix || "") + token;
}
// Make request
const fetchOptions = {
method: config.method,
headers
};
if (config.body && config.method === "POST") {
fetchOptions.body = JSON.stringify(config.body);
}
const response = await fetch(url, fetchOptions);
if (!response.ok) {
const errorText = await response.text();
console.log(`Error fetching models from ${connection.provider}:`, errorText);
return NextResponse.json(
{ error: `Failed to fetch models: ${response.status}` },
{ status: response.status }
);
}
const data = await response.json();
const models = config.parseResponse(data);
return NextResponse.json({
provider: connection.provider,
connectionId: connection.id,
models
});
} catch (error) {
console.log("Error fetching provider models:", error);
return NextResponse.json({ error: "Failed to fetch models" }, { status: 500 });
}
}
+102
View File
@@ -0,0 +1,102 @@
import { NextResponse } from "next/server";
import { getProviderConnectionById, updateProviderConnection, deleteProviderConnection, isCloudEnabled } from "@/models";
import { getConsistentMachineId } from "@/shared/utils/machineId";
import { syncToCloud } from "@/app/api/sync/cloud/route";
// GET /api/providers/[id] - Get single connection
export async function GET(request, { params }) {
try {
const { id } = await params;
const connection = await getProviderConnectionById(id);
if (!connection) {
return NextResponse.json({ error: "Connection not found" }, { status: 404 });
}
// Hide sensitive fields
const result = { ...connection };
delete result.apiKey;
delete result.accessToken;
delete result.refreshToken;
delete result.idToken;
return NextResponse.json({ connection: result });
} catch (error) {
console.log("Error fetching connection:", error);
return NextResponse.json({ error: "Failed to fetch connection" }, { status: 500 });
}
}
// PUT /api/providers/[id] - Update connection
export async function PUT(request, { params }) {
try {
const { id } = await params;
const body = await request.json();
const { name, priority, globalPriority, defaultModel, isActive, apiKey } = body;
const existing = await getProviderConnectionById(id);
if (!existing) {
return NextResponse.json({ error: "Connection not found" }, { status: 404 });
}
const updateData = {};
if (name !== undefined) updateData.name = name;
if (priority !== undefined) updateData.priority = priority;
if (globalPriority !== undefined) updateData.globalPriority = globalPriority;
if (defaultModel !== undefined) updateData.defaultModel = defaultModel;
if (isActive !== undefined) updateData.isActive = isActive;
if (apiKey && existing.authType === "apikey") updateData.apiKey = apiKey;
const updated = await updateProviderConnection(id, updateData);
// Hide sensitive fields
const result = { ...updated };
delete result.apiKey;
delete result.accessToken;
delete result.refreshToken;
delete result.idToken;
// Auto sync to Cloud if enabled
await syncToCloudIfEnabled();
return NextResponse.json({ connection: result });
} catch (error) {
console.log("Error updating connection:", error);
return NextResponse.json({ error: "Failed to update connection" }, { status: 500 });
}
}
// DELETE /api/providers/[id] - Delete connection
export async function DELETE(request, { params }) {
try {
const { id } = await params;
const deleted = await deleteProviderConnection(id);
if (!deleted) {
return NextResponse.json({ error: "Connection not found" }, { status: 404 });
}
// Auto sync to Cloud if enabled
await syncToCloudIfEnabled();
return NextResponse.json({ message: "Connection deleted successfully" });
} catch (error) {
console.log("Error deleting connection:", error);
return NextResponse.json({ error: "Failed to delete connection" }, { 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);
}
}
+95
View File
@@ -0,0 +1,95 @@
import { NextResponse } from "next/server";
import { getProviderConnectionById, updateProviderConnection } from "@/lib/localDb";
// POST /api/providers/[id]/test - Test connection
export async function POST(request, { params }) {
try {
const { id } = await params;
const connection = await getProviderConnectionById(id);
if (!connection) {
return NextResponse.json({ error: "Connection not found" }, { status: 404 });
}
let isValid = false;
let error = null;
try {
if (connection.authType === "apikey") {
// Test API key
switch (connection.provider) {
case "openai":
const openaiRes = await fetch("https://api.openai.com/v1/models", {
headers: { "Authorization": `Bearer ${connection.apiKey}` },
});
isValid = openaiRes.ok;
break;
case "anthropic":
const anthropicRes = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"x-api-key": connection.apiKey,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
body: JSON.stringify({
model: "claude-3-haiku-20240307",
max_tokens: 1,
messages: [{ role: "user", content: "test" }],
}),
});
isValid = anthropicRes.status !== 401;
break;
case "gemini":
const geminiRes = await fetch(`https://generativelanguage.googleapis.com/v1/models?key=${connection.apiKey}`);
isValid = geminiRes.ok;
break;
case "openrouter":
const openrouterRes = await fetch("https://openrouter.ai/api/v1/models", {
headers: { "Authorization": `Bearer ${connection.apiKey}` },
});
isValid = openrouterRes.ok;
break;
default:
error = "Provider test not supported";
}
} else {
// OAuth - check if token exists and not expired
if (connection.accessToken) {
if (connection.expiresAt) {
const expiresAt = new Date(connection.expiresAt).getTime();
isValid = expiresAt > Date.now();
if (!isValid) error = "Token expired";
} else {
isValid = true;
}
} else {
error = "No access token";
}
}
} catch (err) {
error = err.message;
isValid = false;
}
// Update status in db
await updateProviderConnection(id, {
testStatus: isValid ? "active" : "error",
lastError: isValid ? null : error,
lastErrorAt: isValid ? null : new Date().toISOString(),
});
return NextResponse.json({
valid: isValid,
error: isValid ? null : error,
});
} catch (error) {
console.log("Error testing connection:", error);
return NextResponse.json({ error: "Test failed" }, { status: 500 });
}
}
+20
View File
@@ -0,0 +1,20 @@
import { NextResponse } from "next/server";
import { getProviderConnections } from "@/lib/localDb";
// GET /api/providers/client - List all connections for client (includes sensitive fields for sync)
export async function GET() {
try {
const connections = await getProviderConnections();
// Include sensitive fields for sync to cloud (only accessible from same origin)
const clientConnections = connections.map(c => ({
...c,
// Don't hide sensitive fields here since this is for internal sync
}));
return NextResponse.json({ connections: clientConnections });
} catch (error) {
console.log("Error fetching providers for client:", error);
return NextResponse.json({ error: "Failed to fetch providers" }, { status: 500 });
}
}
+84
View File
@@ -0,0 +1,84 @@
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);
}
}
+96
View File
@@ -0,0 +1,96 @@
import { NextResponse } from "next/server";
// POST /api/providers/validate - Validate API key with provider
export async function POST(request) {
try {
const body = await request.json();
const { provider, apiKey } = body;
if (!provider || !apiKey) {
return NextResponse.json({ error: "Provider and API key required" }, { status: 400 });
}
let isValid = false;
let error = null;
// Validate with each provider
try {
switch (provider) {
case "openai":
const openaiRes = await fetch("https://api.openai.com/v1/models", {
headers: { "Authorization": `Bearer ${apiKey}` },
});
isValid = openaiRes.ok;
break;
case "anthropic":
const anthropicRes = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"x-api-key": apiKey,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
body: JSON.stringify({
model: "claude-3-haiku-20240307",
max_tokens: 1,
messages: [{ role: "user", content: "test" }],
}),
});
isValid = anthropicRes.status !== 401;
break;
case "gemini":
const geminiRes = await fetch(`https://generativelanguage.googleapis.com/v1/models?key=${apiKey}`);
isValid = geminiRes.ok;
break;
case "openrouter":
const openrouterRes = await fetch("https://openrouter.ai/api/v1/models", {
headers: { "Authorization": `Bearer ${apiKey}` },
});
isValid = openrouterRes.ok;
break;
case "glm":
case "kimi":
case "minimax": {
const claudeBaseUrls = {
glm: "https://api.z.ai/api/anthropic/v1/messages",
kimi: "https://api.kimi.com/coding/v1/messages",
minimax: "https://api.minimax.io/anthropic/v1/messages",
};
const claudeRes = await fetch(claudeBaseUrls[provider], {
method: "POST",
headers: {
"x-api-key": apiKey,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
body: JSON.stringify({
model: "claude-sonnet-4-20250514",
max_tokens: 1,
messages: [{ role: "user", content: "test" }],
}),
});
isValid = claudeRes.status !== 401;
break;
}
default:
return NextResponse.json({ error: "Provider validation not supported" }, { status: 400 });
}
} catch (err) {
error = err.message;
isValid = false;
}
return NextResponse.json({
valid: isValid,
error: isValid ? null : (error || "Invalid API key"),
});
} catch (error) {
console.log("Error validating API key:", error);
return NextResponse.json({ error: "Validation failed" }, { status: 500 });
}
}