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

View file

@ -0,0 +1,83 @@
import { NextResponse } from "next/server";
import { getModelAliases, setModelAlias, deleteModelAlias, isCloudEnabled } from "@/models";
import { getConsistentMachineId } from "@/shared/utils/machineId";
import { syncToCloud } from "@/app/api/sync/cloud/route";
// GET /api/models/alias - Get all aliases
export async function GET() {
try {
const aliases = await getModelAliases();
return NextResponse.json({ aliases });
} catch (error) {
console.log("Error fetching aliases:", error);
return NextResponse.json({ error: "Failed to fetch aliases" }, { status: 500 });
}
}
// PUT /api/models/alias - Set model alias
export async function PUT(request) {
try {
const body = await request.json();
const { model, alias } = body;
if (!model || !alias) {
return NextResponse.json({ error: "Model and alias required" }, { status: 400 });
}
const aliases = await getModelAliases();
// Check if alias already used by different model
const existingModel = aliases[alias];
if (existingModel && existingModel !== model) {
return NextResponse.json({
error: `Alias '${alias}' already in use for model '${existingModel}'`
}, { status: 400 });
}
// Delete old alias for this model (if exists and different from new alias)
const oldAlias = Object.entries(aliases).find(([a, m]) => m === model && a !== alias)?.[0];
if (oldAlias) {
await deleteModelAlias(oldAlias);
}
await setModelAlias(alias, model);
await syncToCloudIfEnabled();
return NextResponse.json({ success: true, model, alias });
} catch (error) {
console.log("Error updating alias:", error);
return NextResponse.json({ error: "Failed to update alias" }, { status: 500 });
}
}
// DELETE /api/models/alias?alias=xxx - Delete alias
export async function DELETE(request) {
try {
const { searchParams } = new URL(request.url);
const alias = searchParams.get("alias");
if (!alias) {
return NextResponse.json({ error: "Alias required" }, { status: 400 });
}
await deleteModelAlias(alias);
await syncToCloudIfEnabled();
return NextResponse.json({ success: true });
} catch (error) {
console.log("Error deleting alias:", error);
return NextResponse.json({ error: "Failed to delete alias" }, { status: 500 });
}
}
async function syncToCloudIfEnabled() {
try {
const cloudEnabled = await isCloudEnabled();
if (!cloudEnabled) return;
const machineId = await getConsistentMachineId();
await syncToCloud(machineId);
} catch (error) {
console.log("Error syncing aliases to cloud:", error);
}
}

View file

@ -0,0 +1,55 @@
import { NextResponse } from "next/server";
import { getModelAliases, setModelAlias } from "@/models";
import { AI_MODELS } from "@/shared/constants/config";
// GET /api/models - Get models with aliases
export async function GET() {
try {
const modelAliases = await getModelAliases();
const models = AI_MODELS.map((m) => {
const fullModel = `${m.provider}/${m.model}`;
return {
...m,
fullModel,
alias: modelAliases[fullModel] || m.model,
};
});
return NextResponse.json({ models });
} catch (error) {
console.log("Error fetching models:", error);
return NextResponse.json({ error: "Failed to fetch models" }, { status: 500 });
}
}
// PUT /api/models - Update model alias
export async function PUT(request) {
try {
const body = await request.json();
const { model, alias } = body;
if (!model || !alias) {
return NextResponse.json({ error: "Model and alias required" }, { status: 400 });
}
const modelAliases = await getModelAliases();
// Check if alias already exists for different model
const existingModel = Object.entries(modelAliases).find(
([key, val]) => val === alias && key !== model
);
if (existingModel) {
return NextResponse.json({ error: "Alias already in use" }, { status: 400 });
}
// Update alias
await setModelAlias(model, alias);
return NextResponse.json({ success: true, model, alias });
} catch (error) {
console.log("Error updating alias:", error);
return NextResponse.json({ error: "Failed to update alias" }, { status: 500 });
}
}