fix: surface agent_sessions 500 and add db migration
- sessions/route.ts: replace inline CREATE TABLE DDL with a lightweight existence check; add `details` to all 500 responses; fix type-unsafe `p.id = $1::uuid` comparisons to `p.id::text = $1` to avoid the Postgres `text = uuid` operator error - app/api/admin/migrate: one-shot idempotent migration endpoint secured with ADMIN_MIGRATE_SECRET, creates fs_* tables + agent_sessions - scripts/migrate-fs-tables.sql: formal schema for all fs_* tables Made-with: Cursor
This commit is contained in:
@@ -15,28 +15,14 @@ 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).
|
||||
// Verify the agent_sessions table is reachable. If it doesn't exist yet,
|
||||
// throw a descriptive error instead of a generic "Failed to create session".
|
||||
// Run POST /api/admin/migrate once to create the table.
|
||||
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);
|
||||
`, []);
|
||||
await query(
|
||||
`SELECT 1 FROM agent_sessions LIMIT 0`,
|
||||
[]
|
||||
);
|
||||
}
|
||||
|
||||
// ── POST — create session ────────────────────────────────────────────────────
|
||||
@@ -69,7 +55,7 @@ export async function POST(
|
||||
const owns = await query<{ id: string; data: Record<string, unknown> }>(
|
||||
`SELECT p.id, p.data FROM fs_projects p
|
||||
JOIN fs_users u ON u.id = p.user_id
|
||||
WHERE p.id = $1::uuid AND u.data->>'email' = $2 LIMIT 1`,
|
||||
WHERE p.id::text = $1 AND u.data->>'email' = $2 LIMIT 1`,
|
||||
[projectId, session.user.email]
|
||||
);
|
||||
if (owns.length === 0) {
|
||||
@@ -122,7 +108,10 @@ export async function POST(
|
||||
return NextResponse.json({ sessionId }, { status: 201 });
|
||||
} catch (err) {
|
||||
console.error("[agent/sessions POST]", err);
|
||||
return NextResponse.json({ error: "Failed to create session" }, { status: 500 });
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to create session", details: err instanceof Error ? err.message : String(err) },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,9 +146,9 @@ export async function GET(
|
||||
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_projects p ON p.id::text = s.project_id::text
|
||||
JOIN fs_users u ON u.id = p.user_id
|
||||
WHERE s.project_id = $1::uuid AND u.data->>'email' = $2
|
||||
WHERE s.project_id::text = $1 AND u.data->>'email' = $2
|
||||
ORDER BY s.created_at DESC
|
||||
LIMIT 50`,
|
||||
[projectId, session.user.email]
|
||||
@@ -168,6 +157,9 @@ export async function GET(
|
||||
return NextResponse.json({ sessions });
|
||||
} catch (err) {
|
||||
console.error("[agent/sessions GET]", err);
|
||||
return NextResponse.json({ error: "Failed to list sessions" }, { status: 500 });
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to list sessions", details: err instanceof Error ? err.message : String(err) },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user