feat: agent session retry + follow-up UX

- retry/route.ts: reset failed/stopped session and re-fire agent runner
  with optional continueTask follow-up text
- build/page.tsx: Retry button and Follow up input appear on failed/stopped
  sessions so users can continue without losing context or creating a
  duplicate session; task input hint clarifies each Run = new session

Made-with: Cursor
This commit is contained in:
2026-03-07 12:25:58 -08:00
parent 28b48b74af
commit 8c19dc1802
2 changed files with 187 additions and 1 deletions

View File

@@ -278,6 +278,9 @@ function AgentMode({ projectId, appName, appPath }: { projectId: string; appName
const [approveMsg, setApproveMsg] = useState("");
const [showApproveInput, setShowApproveInput] = useState(false);
const [approveResult, setApproveResult] = useState<string | null>(null);
const [retrying, setRetrying] = useState(false);
const [followUp, setFollowUp] = useState("");
const [showFollowUp, setShowFollowUp] = useState(false);
const outputRef = useCallback((el: HTMLDivElement | null) => {
if (el) el.scrollTop = el.scrollHeight;
}, []);
@@ -346,6 +349,24 @@ function AgentMode({ projectId, appName, appPath }: { projectId: string; appName
setActiveSession(prev => prev ? { ...prev, status: "stopped" } : null);
};
const handleRetry = async (continueTask?: string) => {
if (!activeSessionId) return;
setRetrying(true);
try {
const r = await fetch(`/api/projects/${projectId}/agent/sessions/${activeSessionId}/retry`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ continueTask: continueTask?.trim() || undefined }),
});
const d = await r.json();
if (d.sessionId) {
setActiveSession(prev => prev ? { ...prev, status: "running", output: [], error: null } : null);
setShowFollowUp(false);
setFollowUp("");
}
} finally { setRetrying(false); }
};
const handleApprove = async () => {
if (!activeSessionId || !approveMsg.trim()) return;
setApproving(true);
@@ -459,6 +480,57 @@ function AgentMode({ projectId, appName, appPath }: { projectId: string; appName
)}
</div>
{/* Retry / follow-up panel for failed or stopped sessions */}
{["failed", "stopped"].includes(activeSession.status) && (
<div style={{ borderTop: "1px solid #e8e4dc", background: "#fff", padding: "12px 20px", flexShrink: 0 }}>
{showFollowUp ? (
<div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
<div style={{ fontSize: "0.72rem", color: "#6b6560", fontFamily: "Outfit, sans-serif" }}>
Add a follow-up instruction (optional) then retry:
</div>
<div style={{ display: "flex", gap: 8, alignItems: "flex-end" }}>
<textarea
autoFocus
value={followUp}
onChange={e => setFollowUp(e.target.value)}
onKeyDown={e => { if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) handleRetry(followUp); if (e.key === "Escape") setShowFollowUp(false); }}
placeholder="e.g. Also update the TypeScript types, or just leave blank to retry as-is…"
rows={2}
style={{ flex: 1, resize: "none", border: "1px solid #e8e4dc", borderRadius: 7, padding: "8px 11px", fontSize: "0.78rem", fontFamily: "Outfit, sans-serif", outline: "none", background: "#faf8f5" }}
/>
<div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
<button onClick={() => handleRetry(followUp)} disabled={retrying}
style={{ padding: "8px 14px", background: "#1a1a1a", color: "#fff", border: "none", borderRadius: 7, fontSize: "0.75rem", fontWeight: 600, cursor: "pointer", fontFamily: "Outfit, sans-serif", whiteSpace: "nowrap" }}>
{retrying ? "Retrying…" : "Retry"}
</button>
<button onClick={() => setShowFollowUp(false)}
style={{ padding: "6px 14px", background: "transparent", color: "#a09a90", border: "1px solid #e8e4dc", borderRadius: 7, fontSize: "0.73rem", cursor: "pointer", fontFamily: "Outfit, sans-serif" }}>
Cancel
</button>
</div>
</div>
<div style={{ fontSize: "0.63rem", color: "#b5b0a6", fontFamily: "Outfit, sans-serif" }}>
to retry · Esc to cancel · Same session, fresh run
</div>
</div>
) : (
<div style={{ display: "flex", gap: 8, alignItems: "center" }}>
<div style={{ fontSize: "0.75rem", color: activeSession.status === "failed" ? "#c62828" : "#a09a90", fontFamily: "Outfit, sans-serif", flex: 1 }}>
{activeSession.status === "failed" ? "Session failed — retry without losing context" : "Session stopped"}
</div>
<button onClick={() => handleRetry()} disabled={retrying}
style={{ padding: "7px 14px", background: "#fef9f0", color: "#92400e", border: "1px solid #fde68a", borderRadius: 7, fontSize: "0.75rem", fontWeight: 600, cursor: "pointer", fontFamily: "Outfit, sans-serif", whiteSpace: "nowrap" }}>
{retrying ? "Retrying…" : "↺ Retry"}
</button>
<button onClick={() => setShowFollowUp(true)}
style={{ padding: "7px 14px", background: "#f0ece4", color: "#1a1a1a", border: "1px solid #e8e4dc", borderRadius: 7, fontSize: "0.75rem", cursor: "pointer", fontFamily: "Outfit, sans-serif", whiteSpace: "nowrap" }}>
Follow up
</button>
</div>
)}
</div>
)}
{/* Changed files */}
{activeSession.changed_files.length > 0 && (
<div style={{ borderTop: "1px solid #e8e4dc", background: "#fff", padding: "12px 20px", flexShrink: 0 }}>
@@ -576,7 +648,7 @@ function AgentMode({ projectId, appName, appPath }: { projectId: string; appName
</button>
</div>
<div style={{ fontSize: "0.65rem", color: "#b5b0a6", marginTop: 5, fontFamily: "Outfit, sans-serif" }}>
to run · Session persists if you close the browser
to start · Each task is a new session · Use "Follow up" on a failed session to continue it
</div>
</div>
</div>

View File

@@ -0,0 +1,114 @@
/**
* POST /api/projects/[projectId]/agent/sessions/[sessionId]/retry
*
* Re-run a failed or stopped session, optionally with a follow-up instruction.
* Resets the session row to `running` and fires the agent-runner again.
*
* Body: { continueTask?: string }
* continueTask — if provided, appended to the original task so the agent
* understands what was already tried
*/
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 });
}
const body = await req.json().catch(() => ({})) as { continueTask?: string };
// Verify ownership and load the original session
const rows = await query<{
id: string;
project_id: string;
app_name: string;
app_path: string;
task: string;
status: string;
}>(
`SELECT s.id, s.project_id, s.app_name, s.app_path, s.task, s.status
FROM agent_sessions s
JOIN fs_projects p ON p.id::text = s.project_id::text
JOIN fs_users u ON u.id = p.user_id
WHERE s.id = $1::uuid AND s.project_id::text = $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 });
}
const s = rows[0];
if (!["failed", "stopped"].includes(s.status)) {
return NextResponse.json(
{ error: `Session is ${s.status} — can only retry failed or stopped sessions` },
{ status: 409 }
);
}
// Fetch giteaRepo from the project
const proj = await query<{ data: Record<string, unknown> }>(
`SELECT data FROM fs_projects WHERE id::text = $1 LIMIT 1`,
[projectId]
);
const giteaRepo = proj[0]?.data?.giteaRepo as string | undefined;
// Reset the session row so the frontend shows it as running again
await query(
`UPDATE agent_sessions
SET status = 'running',
error = NULL,
output = '[]'::jsonb,
changed_files = '[]'::jsonb,
started_at = now(),
completed_at = NULL,
updated_at = now()
WHERE id = $1::uuid`,
[sessionId]
);
// Re-fire the agent runner
fetch(`${AGENT_RUNNER_URL}/agent/execute`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
sessionId,
projectId,
appName: s.app_name,
appPath: s.app_path,
giteaRepo,
task: s.task,
continueTask: body.continueTask?.trim() || undefined,
}),
}).catch(err => {
console.warn("[retry] runner not reachable:", err.message);
query(
`UPDATE agent_sessions
SET status = 'failed', error = 'Agent runner not reachable', completed_at = now(), updated_at = now()
WHERE id = $1::uuid`,
[sessionId]
).catch(() => {});
});
return NextResponse.json({ sessionId, status: "running" });
} catch (err) {
console.error("[retry POST]", err);
return NextResponse.json(
{ error: "Failed to retry session", details: err instanceof Error ? err.message : String(err) },
{ status: 500 }
);
}
}