import type { BlueprintCandidateContract, BlueprintResultContract, ContractCoderId, IdeaRequestContract, MatrixBundleContract, ValidationReportContract, } from "../types/contracts"; // Default to the same-origin proxy path (/api/builder → rewritten to the backend in // next.config.ts). This makes Vercel work with ZERO env vars. Override with // NEXT_PUBLIC_API_BASE_URL=http://localhost:8000 only for local dev against a raw backend. export const apiBaseUrl = process.env.NEXT_PUBLIC_API_BASE_URL ?? "/api/builder"; async function postJson(path: string, payload: unknown): Promise { const response = await fetch(`${apiBaseUrl}${path}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(payload), }); if (!response.ok) throw new Error(`Matrix Builder API error ${response.status}`); return (await response.json()) as T; } async function getJson(path: string): Promise { const response = await fetch(`${apiBaseUrl}${path}`); if (!response.ok) throw new Error(`Matrix Builder API error ${response.status}`); return (await response.json()) as T; } export function getHealth(): Promise { return fetch(`${apiBaseUrl}/health`); } export function parseIdea(payload: Partial) { return postJson("/api/v1/ideas/parse", payload); } export function getBlueprintCandidates(payload: Partial) { return postJson<{ candidates: BlueprintCandidateContract[] }>( "/api/v1/blueprints/candidates", payload, ); } export function generateBlueprint(payload: Partial) { return postJson("/api/v1/blueprints", payload); } export function generateBundle( ideaRequest: Partial, preferredCoder: ContractCoderId, candidateId?: string, ) { return postJson("/api/v1/bundles", { idea_request: ideaRequest, preferred_coder: preferredCoder, candidate_id: candidateId, }); } export function getBundle(bundleId: string) { return getJson(`/api/v1/bundles/${bundleId}`); } export function getBundlePrompt(bundleId: string, coder: ContractCoderId) { return getJson<{ coder: ContractCoderId; prompt: string; contract_files: string[] }>( `/api/v1/bundles/${bundleId}/prompt/${coder}`, ); } export function validateBundle(bundleId: string) { return postJson(`/api/v1/bundles/${bundleId}/validate`, {}); }