import { getAgent, getAgentMemory, deleteAgentMemory } from '@/lib/runtime.js';

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

export async function GET(_req, { params }) {
  const { id } = await params;
  const agent = getAgent(id);
  if (!agent) return Response.json({ ok: false, error: 'agent not found' }, { status: 404 });
  return Response.json({ ok: true, agent_id: id, memory: getAgentMemory(id) || [] });
}

export async function DELETE(req, { params }) {
  const { id } = await params;
  let body;
  try { body = await req.json(); } catch { body = {}; }
  const result = deleteAgentMemory(id, body.index);
  if (!result.ok) {
    const status = result.error === 'agent not found' ? 404 : 400;
    return Response.json(result, { status });
  }
  return Response.json({ ok: true, agent_id: id, removed: result.removed, memory: result.memory });
}
