Spaces:
Runtime error
Runtime error
File size: 2,586 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { isAuthenticated } from "@/shared/utils/apiAuth";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
import {
syncModelsDev,
getModelsDevPricing,
getSyncedCapabilities,
getSyncStatus,
startPeriodicSync,
stopPeriodicSync,
} from "@/lib/modelsDevSync";
const modelsDevActionSchema = z.object({
action: z.enum(["sync", "start", "stop"]),
dryRun: z.boolean().optional(),
syncCapabilities: z.boolean().optional(),
});
export async function GET(request: NextRequest) {
if (!(await isAuthenticated(request))) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const { searchParams } = new URL(request.url);
const action = searchParams.get("action");
if (action === "status") {
const status = getSyncStatus();
const pricing = getModelsDevPricing();
const caps = getSyncedCapabilities();
const providerCount = Object.keys(pricing).length;
const modelCount = Object.values(pricing).reduce(
(sum, models) => sum + Object.keys(models).length,
0
);
const capabilityCount = Object.values(caps).reduce(
(sum, models) => sum + Object.keys(models).length,
0
);
return NextResponse.json({
...status,
providerCount,
modelCount,
capabilityCount,
});
}
return NextResponse.json({ error: "Unknown action" }, { status: 400 });
}
export async function POST(request: NextRequest) {
if (!(await isAuthenticated(request))) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
let rawBody: unknown;
try {
rawBody = await request.json();
} catch {
return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
}
const validation = validateBody(modelsDevActionSchema, rawBody);
if (isValidationFailure(validation)) {
return validation.response;
}
const { action, dryRun, syncCapabilities } = validation.data;
if (action === "sync") {
const result = await syncModelsDev({
dryRun: dryRun ?? false,
syncCapabilities: syncCapabilities !== false,
});
return NextResponse.json(result);
}
if (action === "start") {
startPeriodicSync();
return NextResponse.json({ success: true, message: "Periodic sync started" });
}
if (action === "stop") {
stopPeriodicSync();
return NextResponse.json({ success: true, message: "Periodic sync stopped" });
}
return NextResponse.json({ error: "Unknown action" }, { status: 400 });
}
|