import { getAgentRun, listAgentRunEvents } from '@/lib/delegation.js';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

export async function GET(_req, { params }) {
  const { id } = await params;
  const run = getAgentRun(id);
  if (!run) return new Response('Run not found', { status: 404 });
  const events = listAgentRunEvents(id);
  return Response.json({
    run,
    log: events
      .filter(event => ['log', 'warning', 'error', 'approval_required', 'tool_result', 'tool_start', 'lifecycle'].includes(event.event_type))
      .map(event => ({ seq: event.seq, level: event.level, message: event.message, event_type: event.event_type, payload: event.payload, created_at: event.created_at })),
  });
}
