import { NextResponse } from "next/server"; import { isAbsolute } from "node:path"; import { findEquaxisRoot, runEquaxisScript } from "@/lib/equaxis-project"; import { getAllowedFileRoots, isExistingFilePathAllowed } from "@/lib/file-access"; import { isApiRequestAllowed } from "@/lib/request-security"; export const dynamic = "force-dynamic"; // GET /api/harness?cwd= -> Equaxis harness snapshot (config, dashboard, traces) export async function GET(request: Request) { if (!isApiRequestAllowed(request)) { return NextResponse.json({ error: "Untrusted API request" }, { status: 403 }); } const url = new URL(request.url); const cwd = url.searchParams.get("cwd")?.trim() ?? ""; if (!cwd || !isAbsolute(cwd)) { return NextResponse.json({ error: "cwd required" }, { status: 400 }); } const root = findEquaxisRoot(cwd); if (!root) { return NextResponse.json({ error: "Not an Equaxis project (.pi/equaxis.json missing)" }, { status: 400 }); } const allowedRoots = await getAllowedFileRoots(); if (!isExistingFilePathAllowed(root, allowedRoots)) { return NextResponse.json({ error: "Access denied" }, { status: 403 }); } try { const snapshot = await runEquaxisScript(root, "harness-snapshot.mjs"); return NextResponse.json(snapshot); } catch (error) { return NextResponse.json({ error: String(error) }, { status: 500 }); } }