2025-12-22 22:36:36 +09:00
|
|
|
import { NextResponse } from "next/server"
|
2026-10-05 22:39:32 +09:00
|
|
|
import { checkAccessCode, rejectCrossSite } from "@/lib/access-code"
|
2026-06-15 12:54:18 +08:00
|
|
|
import {
|
2026-10-12 12:35:55 +09:00
|
|
|
type TestModelRequest,
|
|
|
|
|
testModelConnection,
|
|
|
|
|
} from "@/lib/test-model-connection"
|
2025-12-22 22:36:36 +09:00
|
|
|
|
|
|
|
|
export const runtime = "nodejs"
|
|
|
|
|
|
2026-10-12 12:35:55 +09:00
|
|
|
// The Settings dialog's Test button: a model on the user's own credentials
|
2025-12-22 22:36:36 +09:00
|
|
|
export async function POST(req: Request) {
|
2026-10-05 22:39:32 +09:00
|
|
|
const crossSite = rejectCrossSite(req)
|
|
|
|
|
if (crossSite) return crossSite
|
2026-10-12 12:35:55 +09:00
|
|
|
// Lets the server send requests to arbitrary URLs, so require the access code
|
2026-10-05 22:39:32 +09:00
|
|
|
const accessError = checkAccessCode(req)
|
2026-10-12 12:35:55 +09:00
|
|
|
if (accessError) return accessError
|
2026-10-05 22:39:32 +09:00
|
|
|
|
2026-10-12 12:35:55 +09:00
|
|
|
let body: TestModelRequest
|
2025-12-22 22:36:36 +09:00
|
|
|
try {
|
2026-10-12 12:35:55 +09:00
|
|
|
body = await req.json()
|
|
|
|
|
} catch {
|
2025-12-22 22:36:36 +09:00
|
|
|
return NextResponse.json(
|
2026-10-12 12:35:55 +09:00
|
|
|
{ valid: false, error: "Invalid JSON body" },
|
|
|
|
|
{ status: 400 },
|
2025-12-22 22:36:36 +09:00
|
|
|
)
|
|
|
|
|
}
|
2026-10-12 12:35:55 +09:00
|
|
|
// A URL in the request is one the user typed, never the server's own
|
|
|
|
|
return testModelConnection(req, body, false)
|
2025-12-22 22:36:36 +09:00
|
|
|
}
|