import { NextResponse } from "next/server"; import { getServerSession } from "next-auth"; import { authOptions } from "@/lib/auth/authOptions"; import { query } from "@/lib/db-postgres"; const AGENT_RUNNER_URL = process.env.AGENT_RUNNER_URL ?? "http://localhost:3333"; export async function POST( _req: Request, { params }: { params: Promise<{ projectId: string; sessionId: string }> } ) { try { const { projectId, sessionId } = await params; const session = await getServerSession(authOptions); if (!session?.user?.email) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } // Verify ownership const rows = await query<{ status: string }>( `SELECT s.status FROM agent_sessions s JOIN fs_projects p ON p.id = s.project_id JOIN fs_users u ON u.id = p.user_id WHERE s.id = $1 AND s.project_id = $2 AND u.data->>'email' = $3 LIMIT 1`, [sessionId, projectId, session.user.email] ); if (rows.length === 0) { return NextResponse.json({ error: "Session not found" }, { status: 404 }); } if (rows[0].status !== "running" && rows[0].status !== "pending") { return NextResponse.json({ error: "Session is not running" }, { status: 400 }); } // Tell the agent runner to stop (best-effort) fetch(`${AGENT_RUNNER_URL}/agent/stop`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ sessionId }), }).catch(() => {}); // Mark as stopped in DB immediately await query( `UPDATE agent_sessions SET status = 'stopped', completed_at = now(), updated_at = now(), output = output || '[{"ts": "now", "type": "info", "text": "Stopped by user."}]'::jsonb WHERE id = $1`, [sessionId] ); return NextResponse.json({ ok: true }); } catch (err) { console.error("[agent/sessions/stop]", err); return NextResponse.json({ error: "Failed to stop session" }, { status: 500 }); } }