File size: 2,503 Bytes
22b729d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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<T>(path: string, payload: unknown): Promise<T> {
  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<T>(path: string): Promise<T> {
  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<Response> {
  return fetch(`${apiBaseUrl}/health`);
}

export function parseIdea(payload: Partial<IdeaRequestContract>) {
  return postJson("/api/v1/ideas/parse", payload);
}

export function getBlueprintCandidates(payload: Partial<IdeaRequestContract>) {
  return postJson<{ candidates: BlueprintCandidateContract[] }>(
    "/api/v1/blueprints/candidates",
    payload,
  );
}

export function generateBlueprint(payload: Partial<IdeaRequestContract>) {
  return postJson<BlueprintResultContract>("/api/v1/blueprints", payload);
}

export function generateBundle(
  ideaRequest: Partial<IdeaRequestContract>,
  preferredCoder: ContractCoderId,
  candidateId?: string,
) {
  return postJson<MatrixBundleContract>("/api/v1/bundles", {
    idea_request: ideaRequest,
    preferred_coder: preferredCoder,
    candidate_id: candidateId,
  });
}

export function getBundle(bundleId: string) {
  return getJson<MatrixBundleContract>(`/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<ValidationReportContract>(`/api/v1/bundles/${bundleId}/validate`, {});
}