"use client"; import { ClientApiError } from "@/lib/client/api"; /** POST /api/arena/:id/export?format= and save the file through a temporary object URL. */ export async function downloadArenaExport(sessionId: string, format: "markdown" | "json"): Promise { const res = await fetch(`/api/arena/${encodeURIComponent(sessionId)}/export?format=${format}`, { method: "POST", credentials: "same-origin" }); if (!res.ok) { let err: { error?: { code?: string; message?: string } } = {}; try { err = await res.json(); } catch { /* ignore */ } throw new ClientApiError(res.status, err.error?.code ?? "HTTP_ERROR", err.error?.message ?? `Export failed (${res.status})`); } const blob = await res.blob(); const cd = res.headers.get("Content-Disposition") ?? ""; const filename = res.headers.get("X-Filename") ?? /filename="([^"]+)"/.exec(cd)?.[1] ?? `arena.${format === "json" ? "json" : "md"}`; const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = filename; document.body.appendChild(a); a.click(); a.remove(); setTimeout(() => URL.revokeObjectURL(url), 2000); return filename; }