Spaces:
Runtime error
Runtime error
File size: 2,154 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 | import { NextResponse } from "next/server";
import { z } from "zod";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
import { runSingleModelTest } from "@/lib/api/modelTestRunner";
import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error.ts";
const testModelSchema = z.object({
providerId: z.string().min(1),
modelId: z.string().min(1),
connectionId: z.string().min(1).optional(),
});
const SINGLE_TEST_TIMEOUT_MS = 20_000;
export async function POST(request: Request) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
let rawBody;
try {
rawBody = await request.json();
} catch {
return NextResponse.json(
{
error: {
message: "Invalid request",
details: [{ field: "body", message: "Invalid JSON body" }],
},
},
{ status: 400 }
);
}
try {
const validation = testModelSchema.safeParse(rawBody);
if (!validation.success) {
return NextResponse.json({ error: validation.error.format() }, { status: 400 });
}
const { providerId, modelId, connectionId } = validation.data;
const result = await runSingleModelTest({
providerId,
modelId,
...(connectionId ? { connectionId } : {}),
timeoutMs: SINGLE_TEST_TIMEOUT_MS,
});
if (result.status === "ok") {
return NextResponse.json({
status: "ok",
latencyMs: result.latencyMs,
responseText: result.responseText,
});
}
const body: Record<string, unknown> = {
status: "error",
latencyMs: result.latencyMs,
error: result.error || "Unknown error",
};
if (result.statusCode !== undefined) body.statusCode = result.statusCode;
if (result.rateLimited) body.rateLimited = true;
if (result.retryAfter !== undefined) body.retryAfter = result.retryAfter;
return NextResponse.json(body, { status: result.httpStatus });
} catch (error: unknown) {
return NextResponse.json(
{
status: "error",
error: sanitizeErrorMessage(error) || "Unknown error",
},
{ status: 500 }
);
}
}
|