import { NextRequest } from 'next/server' import { getCurrentUser } from '@/lib/session' export const dynamic = 'force-dynamic' const AGENT_URL = process.env.OPS_AGENT_URL ?? 'http://127.0.0.1:3099' const AGENT_SECRET = process.env.OPS_AGENT_SECRET ?? '' export async function POST( request: NextRequest, { params }: { params: Promise<{ path: string[] }> }, ) { const user = await getCurrentUser() if (!user) { return Response.json({ error: 'unauthorized' }, { status: 401 }) } const { path } = await params const subpath = path.join('/') const body = await request.text() let agentResponse: Response try { agentResponse = await fetch(`${AGENT_URL}/agent/v1/${subpath}`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${AGENT_SECRET}`, }, body, }) } catch (err) { const message = err instanceof Error ? err.message : 'agent unreachable' return Response.json({ error: message }, { status: 502 }) } const contentType = agentResponse.headers.get('Content-Type') ?? 'application/json' return new Response(agentResponse.body, { status: agentResponse.status, headers: { 'Content-Type': contentType, 'Cache-Control': 'no-cache', Connection: 'keep-alive', }, }) }