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:
2026-03-06 17:56:10 -08:00
parent 93a2b4a0ac
commit ad3abd427b
4 changed files with 650 additions and 1 deletions

View File

@@ -0,0 +1,118 @@
/**
* GET /api/projects/[projectId]/agent/sessions/[sessionId]
* Fetch a session's full state — status, output log, changed files.
* Frontend polls this (or will switch to WebSocket in Phase 3).
*
* POST /api/projects/[projectId]/agent/sessions/[sessionId]/stop
* (handled in /stop/route.ts)
*/
import { NextResponse } from "next/server";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth/authOptions";
import { query } from "@/lib/db-postgres";
export async function GET(
_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 });
}
const rows = await query<{
id: string;
app_name: string;
app_path: string;
task: string;
plan: unknown;
status: string;
output: Array<{ ts: string; type: string; text: string }>;
changed_files: Array<{ path: string; status: string }>;
error: string | null;
created_at: string;
started_at: string | null;
completed_at: string | null;
}>(
`SELECT s.id, s.app_name, s.app_path, s.task, s.plan,
s.status, s.output, s.changed_files, s.error,
s.created_at, s.started_at, s.completed_at
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 });
}
return NextResponse.json({ session: rows[0] });
} catch (err) {
console.error("[agent/sessions/[id] GET]", err);
return NextResponse.json({ error: "Failed to fetch session" }, { status: 500 });
}
}
export async function PATCH(
req: Request,
{ params }: { params: Promise<{ projectId: string; sessionId: string }> }
) {
/**
* Internal endpoint called by vibn-agent-runner to append output lines
* and update status. Not exposed to users directly.
*
* Body: { status?, outputLine?, changedFile? }
*/
try {
const { sessionId } = await params;
const body = await req.json() as {
status?: string;
outputLine?: { ts: string; type: string; text: string };
changedFile?: { path: string; status: string };
error?: string;
};
const updates: string[] = ["updated_at = now()"];
const values: unknown[] = [];
let idx = 1;
if (body.status) {
updates.push(`status = $${idx++}`);
values.push(body.status);
if (body.status === "done" || body.status === "failed" || body.status === "stopped") {
updates.push(`completed_at = now()`);
}
}
if (body.error) {
updates.push(`error = $${idx++}`);
values.push(body.error);
}
if (body.outputLine) {
updates.push(`output = output || $${idx++}::jsonb`);
values.push(JSON.stringify([body.outputLine]));
}
if (body.changedFile) {
updates.push(`changed_files = changed_files || $${idx++}::jsonb`);
values.push(JSON.stringify([body.changedFile]));
}
values.push(sessionId);
await query(
`UPDATE agent_sessions SET ${updates.join(", ")} WHERE id = $${idx}`,
values
);
return NextResponse.json({ ok: true });
} catch (err) {
console.error("[agent/sessions/[id] PATCH]", err);
return NextResponse.json({ error: "Failed to update session" }, { status: 500 });
}
}

View File

@@ -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 });
}
}

View File

@@ -0,0 +1,170 @@
/**
* Agent Sessions API
*
* POST /api/projects/[projectId]/agent/sessions
* Create a new agent session and kick it off via vibn-agent-runner.
* Body: { appName, appPath, task }
*
* GET /api/projects/[projectId]/agent/sessions
* List all sessions for a project, newest first.
*/
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";
// Ensure the agent_sessions table exists (idempotent).
async function ensureTable() {
await query(`
CREATE TABLE IF NOT EXISTS agent_sessions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
project_id UUID NOT NULL,
app_name TEXT NOT NULL,
app_path TEXT NOT NULL,
task TEXT NOT NULL,
plan JSONB,
status TEXT NOT NULL DEFAULT 'pending',
output JSONB NOT NULL DEFAULT '[]'::jsonb,
changed_files JSONB NOT NULL DEFAULT '[]'::jsonb,
error TEXT,
started_at TIMESTAMPTZ,
completed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS agent_sessions_project_idx
ON agent_sessions(project_id, created_at DESC);
`, []);
}
// ── POST — create session ────────────────────────────────────────────────────
export async function POST(
req: Request,
{ params }: { params: Promise<{ projectId: string }> }
) {
try {
const { projectId } = await params;
const session = await getServerSession(authOptions);
if (!session?.user?.email) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const body = await req.json();
const { appName, appPath, task } = body as {
appName: string;
appPath: string;
task: string;
};
if (!appName || !appPath || !task?.trim()) {
return NextResponse.json({ error: "appName, appPath and task are required" }, { status: 400 });
}
await ensureTable();
// Verify ownership
const owns = await query<{ id: string }>(
`SELECT p.id FROM fs_projects p
JOIN fs_users u ON u.id = p.user_id
WHERE p.id = $1 AND u.data->>'email' = $2 LIMIT 1`,
[projectId, session.user.email]
);
if (owns.length === 0) {
return NextResponse.json({ error: "Project not found" }, { status: 404 });
}
// Create the session row
const rows = await query<{ id: string }>(
`INSERT INTO agent_sessions (project_id, app_name, app_path, task, status, started_at)
VALUES ($1, $2, $3, $4, 'running', now())
RETURNING id`,
[projectId, appName, appPath, task.trim()]
);
const sessionId = rows[0].id;
// Fire-and-forget: call agent-runner to start the execution loop
// The agent runner is responsible for updating the session row as it works.
fetch(`${AGENT_RUNNER_URL}/agent/execute`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
sessionId,
projectId,
appName,
appPath,
task: task.trim(),
}),
}).catch(err => {
// Agent runner may not be wired yet — log but don't fail
console.warn("[agent] runner not reachable:", err.message);
// Mark session as failed if runner unreachable
query(
`UPDATE agent_sessions
SET status = 'failed',
error = 'Agent runner not reachable',
completed_at = now(),
output = jsonb_build_array(jsonb_build_object(
'ts', now()::text,
'type', 'error',
'text', 'Agent runner service is not connected yet. Phase 2 implementation pending.'
))
WHERE id = $1`,
[sessionId]
).catch(() => {});
});
return NextResponse.json({ sessionId }, { status: 201 });
} catch (err) {
console.error("[agent/sessions POST]", err);
return NextResponse.json({ error: "Failed to create session" }, { status: 500 });
}
}
// ── GET — list sessions ──────────────────────────────────────────────────────
export async function GET(
req: Request,
{ params }: { params: Promise<{ projectId: string }> }
) {
try {
const { projectId } = await params;
const session = await getServerSession(authOptions);
if (!session?.user?.email) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
await ensureTable();
const sessions = await query<{
id: string;
app_name: string;
task: string;
status: string;
created_at: string;
started_at: string | null;
completed_at: string | null;
output: Array<{ ts: string; type: string; text: string }>;
changed_files: Array<{ path: string; status: string }>;
error: string | null;
}>(
`SELECT s.id, s.app_name, s.task, s.status,
s.created_at, s.started_at, s.completed_at,
s.output, s.changed_files, s.error
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.project_id = $1 AND u.data->>'email' = $2
ORDER BY s.created_at DESC
LIMIT 50`,
[projectId, session.user.email]
);
return NextResponse.json({ sessions });
} catch (err) {
console.error("[agent/sessions GET]", err);
return NextResponse.json({ error: "Failed to list sessions" }, { status: 500 });
}
}