feat(insights): /api/insights/reprocess auth-gated endpoint to re-run pipeline on existing sessions

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Semih
2026-05-13 22:26:13 +00:00
parent fc6a3f7a39
commit 7606e1b0bf
2 changed files with 47 additions and 1 deletions

View File

@@ -0,0 +1,46 @@
import { NextResponse } from "next/server";
import { auth } from "@/lib/auth";
import { headers } from "next/headers";
import { prisma } from "@/lib/db";
export const dynamic = "force-dynamic";
export async function POST(req: Request) {
const session = await auth.api.getSession({ headers: await headers() });
if (!session) return NextResponse.json({ ok: false, error: "unauthenticated" }, { status: 401 });
const body = (await req.json().catch(() => ({}))) as { scope?: string; limit?: number };
const scope = body.scope ?? "tagged-and-discarded";
const limit = Math.min(Math.max(body.limit ?? 200, 1), 1000);
// Reset session status so the pipeline picks them up again with the new v1.2 logic.
const where =
scope === "all"
? { status: { in: ["tagged", "compressed", "discarded"] as string[] } }
: scope === "compressed"
? { status: "compressed" }
: scope === "discarded"
? { status: "discarded" }
: { status: { in: ["tagged", "discarded"] as string[] } };
const candidates = await prisma.sessionMeta.findMany({
where,
select: { id: true },
orderBy: { startedAt: "desc" },
take: limit,
});
const ids = candidates.map((c) => c.id);
if (!ids.length) return NextResponse.json({ ok: true, reset: 0 });
const updated = await prisma.sessionMeta.updateMany({
where: { id: { in: ids } },
data: { status: "pending_signal", processedAt: null, tags: [], severity: null, score: null },
});
// Drop old compression rows so they get rebuilt
await prisma.compressedSession.deleteMany({ where: { sessionId: { in: ids } } });
// Drop cached custom events so enrichment refetches
await prisma.sessionCustomEvent.deleteMany({ where: { sessionId: { in: ids } } });
return NextResponse.json({ ok: true, reset: updated.count, scope, limit });
}

File diff suppressed because one or more lines are too long