import { relayRealtimeToolCall } from '../../../../../lib/realtime-openai.js';

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

export async function POST(request) {
  let body;
  try {
    body = await request.json();
  } catch {
    return Response.json({ error: 'invalid JSON body' }, { status: 400 });
  }

  try {
    const result = await relayRealtimeToolCall({
      session_id: body?.session_id,
      call_id: body?.call_id,
      name: body?.name,
      args: body?.args,
      project_id: body?.project_id || null,
      agent_id: body?.agent_id || 'main',
    });
    return Response.json(result, { headers: { 'Cache-Control': 'no-store' } });
  } catch (err) {
    return Response.json({ error: String(err?.message || err) }, { status: 400 });
  }
}
