2026-02-14 00:20:41 -08:00
|
|
|
import { NextResponse } from 'next/server';
|
2026-02-14 09:34:10 +00:00
|
|
|
import path from 'node:path';
|
2026-02-14 00:20:41 -08:00
|
|
|
import { readIssuesFromDisk } from '../../../lib/read-issues';
|
|
|
|
|
import { activityEventBus } from '../../../lib/realtime';
|
|
|
|
|
import { buildSessionTaskFeed, getCommunicationSummary } from '../../../lib/agent-sessions';
|
|
|
|
|
|
2026-02-14 08:43:04 +00:00
|
|
|
function isValidProjectRoot(root: string): boolean {
|
|
|
|
|
try {
|
2026-02-14 09:34:10 +00:00
|
|
|
const resolved = path.resolve(root);
|
2026-02-14 16:36:27 +00:00
|
|
|
if (!path.isAbsolute(resolved)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-02-14 17:57:12 +00:00
|
|
|
// Prevent path traversal by ensuring resolved path stays within the project root
|
|
|
|
|
const allowedBase = process.cwd();
|
|
|
|
|
const relative = path.relative(allowedBase, resolved);
|
|
|
|
|
// If "resolved" is outside "allowedBase", "relative" will start with ".."
|
|
|
|
|
if (relative.startsWith('..') || path.isAbsolute(relative)) {
|
2026-02-14 16:36:27 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2026-02-14 08:43:04 +00:00
|
|
|
} catch {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 00:20:41 -08:00
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
|
|
|
|
|
|
export async function GET(request: Request): Promise<Response> {
|
|
|
|
|
const url = new URL(request.url);
|
2026-02-14 08:43:04 +00:00
|
|
|
const projectRootParam = url.searchParams.get('projectRoot');
|
|
|
|
|
const projectRoot = projectRootParam ?? process.cwd();
|
|
|
|
|
|
|
|
|
|
if (projectRootParam && !isValidProjectRoot(projectRoot)) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ ok: false, error: { classification: 'validation', message: 'Invalid projectRoot path' } },
|
|
|
|
|
{ status: 400 }
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-02-14 00:20:41 -08:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const issues = await readIssuesFromDisk({ projectRoot, preferBd: true });
|
|
|
|
|
const activity = activityEventBus.getHistory(projectRoot);
|
|
|
|
|
const communication = await getCommunicationSummary();
|
|
|
|
|
|
|
|
|
|
const feed = buildSessionTaskFeed(issues, activity, communication);
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({ ok: true, feed });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('[API/Sessions] Failed to load session feed:', error);
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{
|
|
|
|
|
ok: false,
|
|
|
|
|
error: {
|
2026-02-14 09:34:10 +00:00
|
|
|
classification: 'internal_error',
|
|
|
|
|
message: 'An internal error occurred while loading the session feed.',
|
2026-02-14 00:20:41 -08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ status: 500 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|