// GET /api/projects/[id]/findings — returns the project's open findings // POST /api/projects/[id]/findings — runs detection, persists, returns the new set import { NextResponse } from "next/server"; import { loadProject, listOpenFindings, replaceFindings } from "../../../../../lib/db/repo"; import { detectFindings } from "../../../../../lib/llm/detect"; export async function GET(_req: Request, { params }: { params: Promise<{ projectId: string }> }) { const { projectId } = await params; const findings = await listOpenFindings(projectId); return NextResponse.json({ findings: findings.map(f => ({ id: f.id, kind: f.kind, text: f.text, linkedElementIds: f.linkedElementIds, confidence: f.confidence, severity: f.severity, validationCode: f.validationCode, modelVersion: f.modelVersion, provider: f.provider, llmModel: f.llmModel, })), }); } export async function POST(_req: Request, { params }: { params: Promise<{ projectId: string }> }) { const { projectId } = await params; try { const { model, version } = await loadProject(projectId); const result = await detectFindings(model); const stored = await replaceFindings(projectId, result.findings, version, result.provider, result.model); return NextResponse.json({ findings: stored.map(f => ({ id: f.id, kind: f.kind, text: f.text, linkedElementIds: f.linkedElementIds, confidence: f.confidence, severity: f.severity, validationCode: f.validationCode, modelVersion: f.modelVersion, provider: f.provider, llmModel: f.llmModel, })), meta: { provider: result.provider, model: result.model, inputTokens: result.inputTokens, outputTokens: result.outputTokens, durationMs: result.durationMs, strippedRefs: result.strippedRefs, droppedFindings: result.droppedFindings, }, }); } catch (err) { return NextResponse.json({ error: (err as Error).message }, { status: 500 }); } }