File size: 9,976 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Authenticated client for the Matrix Builder /v1 workflow + generation API.
//
// Every call carries the self-issued session JWT (Authorization: Bearer …) via authHeaders(),
// so each UI action maps to exactly one typed, owner-scoped request. This is the single client
// the build screens should reach for; api-client.ts (unauthenticated) is being retired.

import { apiBaseUrl } from "./api-client";
import { authHeaders } from "./auth-token";
import type {
  BlueprintCandidateContract,
  BlueprintResultContract,
  ContractCoderId,
  IdeaRequestContract,
  MatrixBundleContract,
  PromptResponseContract,
  ValidationReportContract,
} from "../types/contracts";
import type {
  ArtifactResponse,
  BatchResponse,
  BlueprintImportResponse,
  ChangeType,
  CommitDiffResponse,
  CommitResponse,
  ExecutionRequest,
  ExecutionResponse,
  IdeaIntentResponse,
  IngestDocumentResponse,
  ProjectCreate,
  ProjectResponse,
  PromptPackResponse,
  RunEnqueueResponse,
  RunEvent,
  RunResponse,
  TimelineResponse,
  ValidationRunResponse,
  VersionCreate,
  VersionResponse,
} from "./workflow-types";

export class WorkflowApiError extends Error {
  status: number;
  constructor(status: number, message: string) {
    super(message);
    this.status = status;
    this.name = "WorkflowApiError";
  }
}

async function request<T>(path: string, init?: RequestInit): Promise<T> {
  const response = await fetch(`${apiBaseUrl}${path}`, {
    ...init,
    headers: {
      "content-type": "application/json",
      ...authHeaders(),
      ...(init?.headers ?? {}),
    },
  });
  if (!response.ok) {
    let detail = `Workflow API error ${response.status}`;
    try {
      const body = (await response.json()) as { detail?: string };
      if (body?.detail) detail = body.detail;
    } catch {
      // non-JSON error body; keep the default message
    }
    throw new WorkflowApiError(response.status, detail);
  }
  // 204/empty bodies are valid for some endpoints; tolerate them.
  if (response.status === 204) return undefined as T;
  const text = await response.text();
  return (text ? JSON.parse(text) : undefined) as T;
}

function post<T>(path: string, body?: unknown): Promise<T> {
  return request<T>(path, { method: "POST", body: body === undefined ? undefined : JSON.stringify(body) });
}

// --- Projects -----------------------------------------------------------------------------

export function createProject(payload: ProjectCreate): Promise<ProjectResponse> {
  return post(`/api/v1/projects`, payload);
}

export function listProjects(): Promise<ProjectResponse[]> {
  return request(`/api/v1/projects`);
}

export function getProject(projectId: string): Promise<ProjectResponse> {
  return request(`/api/v1/projects/${projectId}`);
}

// --- Versions -----------------------------------------------------------------------------

export function createVersion(payload: VersionCreate): Promise<VersionResponse> {
  return post(`/api/v1/versions`, payload);
}

export function getVersion(versionId: string): Promise<VersionResponse> {
  return request(`/api/v1/versions/${versionId}`);
}

export function getTimeline(versionId: string): Promise<TimelineResponse> {
  return request(`/api/v1/versions/${versionId}/timeline`);
}

// --- Batches ------------------------------------------------------------------------------

export function createBatch(
  versionId: string,
  goalMd: string,
  changeType: ChangeType,
  title?: string,
): Promise<BatchResponse> {
  return post(`/api/v1/batches`, {
    version_id: versionId,
    goal_md: goalMd,
    change_type: changeType,
    ...(title ? { title } : {}),
  });
}

export function getBatch(batchId: string): Promise<BatchResponse> {
  return request(`/api/v1/batches/${batchId}`);
}

export function generatePromptPack(batchId: string, coder: string): Promise<PromptPackResponse> {
  return post(`/api/v1/batches/${batchId}/prompt-pack`, { coder });
}

// --- Execution: submit what the coder changed (commit + validate in one call) --------------

export function submitExecution(
  batchId: string,
  payload: ExecutionRequest,
): Promise<ExecutionResponse> {
  return post(`/api/v1/batches/${batchId}/executions`, payload);
}

// --- Async runs (enqueue, then poll/stream events) -----------------------------------------

export function enqueueRun(batchId: string, payload: ExecutionRequest): Promise<RunEnqueueResponse> {
  return post(`/api/v1/batches/${batchId}/runs`, payload);
}

export function getRun(runId: string): Promise<RunResponse> {
  return request(`/api/v1/runs/${runId}`);
}

export function getRunEvents(runId: string, after = 0): Promise<RunEvent[]> {
  return request(`/api/v1/runs/${runId}/events?after=${after}`);
}

export function getValidationRun(runId: string): Promise<ValidationRunResponse> {
  return request(`/api/v1/validation-runs/${runId}`);
}

export function createRepairBatch(runId: string, coder: string): Promise<PromptPackResponse> {
  return post(`/api/v1/repair-batches`, { validation_run_id: runId, coder });
}

// --- Commits, diffs and artifacts (the immutable record of an accepted change) -------------

export function getCommit(commitId: string): Promise<CommitResponse> {
  return request(`/api/v1/commits/${commitId}`);
}

export function getCommitDiff(commitId: string): Promise<CommitDiffResponse> {
  return request(`/api/v1/commits/${commitId}/diff`);
}

export function getCommitArtifacts(commitId: string): Promise<ArtifactResponse[]> {
  return request(`/api/v1/commits/${commitId}/artifacts`);
}

// --- Generation (idea → candidates → bundle), now auth-headered too ------------------------

export function parseIdea(payload: Partial<IdeaRequestContract>): Promise<IdeaIntentResponse> {
  return post(`/api/v1/ideas/parse`, payload);
}

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

export function generateBlueprint(
  payload: Partial<IdeaRequestContract>,
  candidateId?: string,
): Promise<BlueprintResultContract> {
  return post(`/api/v1/blueprints/generate`, {
    idea_request: payload,
    ...(candidateId ? { candidate_id: candidateId } : {}),
  });
}

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

// --- Import existing plan: brief upload (Path B) and Blueprint JSON (Path C, skip-AI) ----------

// Upload a PDF/DOCX/Markdown/TXT brief; returns a deterministic ProjectBrief + a derived idea.
export async function ingestDocument(file: File): Promise<IngestDocumentResponse> {
  const form = new FormData();
  form.append("file", file);
  // No content-type header: the browser sets the multipart boundary.
  const response = await fetch(`${apiBaseUrl}/api/v1/ingest/document`, {
    method: "POST",
    headers: { ...authHeaders() },
    body: form,
  });
  if (!response.ok) {
    let detail = `Upload failed (${response.status})`;
    try { const b = (await response.json()) as { detail?: string }; if (b?.detail) detail = b.detail; } catch { /* keep default */ }
    throw new WorkflowApiError(response.status, detail);
  }
  return (await response.json()) as IngestDocumentResponse;
}

// Validate a complete Blueprint JSON against the contract (no AI). valid=false carries errors.
export function importBlueprint(blueprint: unknown): Promise<BlueprintImportResponse> {
  return post(`/api/v1/ingest/blueprint`, { blueprint });
}

// Compile a validated blueprint verbatim into a Matrix Bundle — AI skipped.
export function generateBundleFromBlueprint(
  blueprint: unknown,
  preferredCoder: ContractCoderId,
): Promise<MatrixBundleContract> {
  return post(`/api/v1/bundles`, { blueprint, preferred_coder: preferredCoder });
}

export function getBundle(bundleId: string): Promise<MatrixBundleContract> {
  return request(`/api/v1/bundles/${bundleId}`);
}

export function getBundlePrompt(
  bundleId: string,
  coder: ContractCoderId,
): Promise<PromptResponseContract> {
  return request(`/api/v1/bundles/${bundleId}/prompt/${coder}`);
}

export function validateBundle(bundleId: string): Promise<ValidationReportContract> {
  return post(`/api/v1/bundles/${bundleId}/validate`, {});
}

// Validate a submitted patch (the files the AI coder changed) against the bundle's contract.
// Stateless and real (the metadata contract check) — returns findings/score without a DB-backed run.
export function validateChanges(
  bundleId: string,
  changedFiles: Array<{ path: string; status?: "added" | "modified" | "deleted" | "renamed" }>,
): Promise<ValidationReportContract> {
  return post(`/api/v1/validation/patch`, {
    bundle_id: bundleId,
    mode: "patch",
    changed_files: changedFiles.map((f) => ({ path: f.path, status: f.status ?? "modified" })),
  });
}

// The engine's bundle zip (byte-for-byte what the CLI ships). Fetched with auth so owner-scoped
// bundles download; returns the raw Blob for the browser to save.
export async function downloadBundleZip(bundleId: string): Promise<Blob> {
  const response = await fetch(`${apiBaseUrl}/api/v1/bundles/${bundleId}/download`, {
    headers: { ...authHeaders() },
  });
  if (!response.ok) throw new WorkflowApiError(response.status, "bundle download unavailable");
  return response.blob();
}

// Thumbnails are owner-scoped; fetch with auth and inline the SVG markup.
export async function fetchThumbnail(versionId: string): Promise<string> {
  const response = await fetch(`${apiBaseUrl}/api/v1/versions/${versionId}/thumbnail.svg`, {
    headers: { ...authHeaders() },
  });
  if (!response.ok) throw new WorkflowApiError(response.status, "thumbnail unavailable");
  return response.text();
}