CoDEVX / app /api /test-connection /route.ts
Tiger's Macbook Air
Build agentic PM demo app
3f76ff4
import { NextResponse } from "next/server";
import { z } from "zod";
import { generateLlmText } from "@/lib/llm-client";
import { isLlmConfigReady, normalizeLlmConfig } from "@/lib/llm-config";
const RequestSchema = z.object({
llmConfig: z.object({
apiKey: z.string().optional(),
baseUrl: z.string().optional(),
model: z.string().optional(),
}),
});
export async function POST(req: Request) {
const json = await req.json().catch(() => null);
const parsed = RequestSchema.safeParse(json);
if (!parsed.success) {
return NextResponse.json(
{ ok: false, status: "error", message: "Bad request." },
{ status: 400 },
);
}
if (!isLlmConfigReady(parsed.data.llmConfig)) {
return NextResponse.json({
ok: false,
status: "idle",
message: "Add API key, base URL, and model to test the connection.",
});
}
const config = normalizeLlmConfig(parsed.data.llmConfig);
try {
const result = await generateLlmText({
config,
systemPrompt:
"You are running a connection test. Reply with a short confirmation only.",
userPrompt:
"Connection test. Reply with exactly: Connected to the configured model.",
temperature: 0,
maxTokens: 60,
});
return NextResponse.json({
ok: true,
status: "connected",
message: result.text,
model: config.model,
endpoint: result.endpoint,
});
} catch (error) {
return NextResponse.json({
ok: false,
status: "error",
message: String(error),
model: config.model,
});
}
}