feat: agent execution scaffold — sessions DB, API, and Browse/Agent/Terminal UI
Session model:
- agent_sessions table (auto-created on first use): id, project_id,
app_name, app_path, task, status, output (JSONB log), changed_files,
error, timestamps
- POST /agent/sessions — create session, fires off to agent-runner
(gracefully degrades when runner not yet wired)
- GET /agent/sessions — list sessions newest first
- GET /agent/sessions/[id] — full session state for polling
- PATCH /agent/sessions/[id] — internal: agent-runner appends output lines
- POST /agent/sessions/[id]/stop — stop running session
Build > Code section now has three mode tabs:
- Browse — existing file tree + code viewer
- Agent — task input, session list sidebar, live output stream,
changed files panel, Approve & commit / Open in Theia actions,
2s polling (Phase 3 will replace with WebSocket)
- Terminal — xterm.js placeholder (Phase 4)
Architecture documented in AGENT_EXECUTION_ARCHITECTURE.md
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user