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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Same-origin client for the Next.js API routes (projects, tasks, messages) + legacy benchmark/model/campaign shims.
import type {
  Artifact,
  Benchmark,
  ChatMessage,
  DatasetSearchResponse,
  DatasetSearchResult,
  ModelEntry,
  ProjectRecord,
  ResourceKind,
  ResourceRef,
  Skill,
  SyncStatusEntry,
  TaskRecord,
} from "./types";
import { BENCHMARKS, MODELS } from "./data";

// Legacy backend base β€” some deployments host the agent backend on a separate
// origin. New task/project/artifact routes always live same-origin under the
// Next.js app; only the legacy catalog endpoints (benchmarks, models, campaigns)
// respect this base.
const LEGACY_BASE = process.env.NEXT_PUBLIC_API_URL || "";

/** True when running without Tailscale (local dev). In production, Tailscale
 *  Serve injects identity headers at the reverse-proxy layer so the JS side
 *  doesn't need to add anything. In dev mode we pass X-Dev-User so the backend
 *  can still identify the caller. */
const IS_DEV =
  typeof window !== "undefined" &&
  (window.location.hostname === "localhost" ||
    window.location.hostname === "127.0.0.1");

/** Fetch with a 4-second timeout so the UI doesn't hang. */
async function fetchJSON<T>(path: string, init?: RequestInit): Promise<T> {
  return fetchJSONAt(path, init);
}

async function fetchJSONAt<T>(path: string, init?: RequestInit, base = ""): Promise<T> {
  const controller = new AbortController();
  const timeout = setTimeout(() => controller.abort(), 4000);
  const devHeaders: Record<string, string> = IS_DEV
    ? { "X-Dev-User": "dev@proteinea.local" }
    : {};
  try {
    const res = await fetch(`${base}${path}`, {
      ...init,
      signal: controller.signal,
      headers: { "Content-Type": "application/json", ...devHeaders, ...init?.headers },
    });
    if (!res.ok) throw new Error(`API ${res.status}`);
    return (await res.json()) as T;
  } finally {
    clearTimeout(timeout);
  }
}

// ---- Projects ----

export async function listProjects(): Promise<{ projects: ProjectRecord[] }> {
  return fetchJSON<{ projects: ProjectRecord[] }>("/api/projects");
}

export async function ensureDefaultProject(): Promise<ProjectRecord> {
  const { projects } = await listProjects();
  if (projects.length === 0) {
    throw new Error("No projects returned from /api/projects");
  }
  return projects[0];
}

// ---- Tasks ----

export async function listTasks(projectId: string): Promise<{ tasks: TaskRecord[] }> {
  return fetchJSON<{ tasks: TaskRecord[] }>(
    `/api/projects/${encodeURIComponent(projectId)}/tasks`,
  );
}

export async function createTask(
  projectId: string,
  firstMessage: string,
): Promise<TaskRecord> {
  const { task } = await fetchJSON<{ task: TaskRecord }>(
    `/api/projects/${encodeURIComponent(projectId)}/tasks`,
    {
      method: "POST",
      body: JSON.stringify({ firstMessage }),
    },
  );
  return task;
}

export async function getTask(taskId: string): Promise<TaskRecord | null> {
  const controller = new AbortController();
  const timeout = setTimeout(() => controller.abort(), 4000);
  try {
    const res = await fetch(`/api/tasks/${encodeURIComponent(taskId)}`, {
      signal: controller.signal,
      headers: { "Content-Type": "application/json" },
    });
    if (res.status === 404) return null;
    if (!res.ok) throw new Error(`API ${res.status}`);
    const data = (await res.json()) as { task: TaskRecord };
    return data.task;
  } finally {
    clearTimeout(timeout);
  }
}

export async function appendMessage(
  taskId: string,
  message: ChatMessage,
): Promise<TaskRecord> {
  const { task } = await fetchJSON<{ task: TaskRecord }>(
    `/api/tasks/${encodeURIComponent(taskId)}/messages`,
    {
      method: "POST",
      body: JSON.stringify({ message }),
    },
  );
  return task;
}

export async function patchTask(
  taskId: string,
  patch: Partial<TaskRecord>,
): Promise<TaskRecord> {
  const body: Record<string, unknown> = {};
  if (patch.title !== undefined) body.title = patch.title;
  if (patch.messages !== undefined) body.messages = patch.messages;
  if (patch.artifacts !== undefined) body.artifacts = patch.artifacts;
  const { task } = await fetchJSON<{ task: TaskRecord }>(
    `/api/tasks/${encodeURIComponent(taskId)}`,
    {
      method: "PATCH",
      body: JSON.stringify(body),
    },
  );
  return task;
}

// ---- Skills ----

export async function listSkillsAPI(): Promise<{ skills: Skill[] }> {
  return fetchJSON<{ skills: Skill[] }>("/api/skills");
}

// ---- Jobs ----

export interface JobSubmitResult {
  campaign_job_id: string;
  hub_task_execution_id: string;
  provider: string;
  hub_response: Record<string, unknown>;
}

export interface BackendArtifact {
  id: string;
  hub_output_key: string;
  phylo_tags?: string[];
  scientist_note?: string | null;
}

export interface JobStatusResult {
  campaign_job_id: string;
  campaign_id: string;
  role: string;
  submitted_by: string;
  hub_task_execution_id: string;
  hub: Record<string, unknown>;
  artifacts: BackendArtifact[];
}

export interface CostEstimate {
  estimated_usd: number;
  gpu_type: string;
  minutes_used: number;
  cost_alert: boolean;
}

/** Submit a tool run job. FormData must include campaign_id, endpoint_name,
 *  call_params (JSON string), and input_file. */
export async function submitJobAPI(
  campaignId: string,
  formData: FormData,
): Promise<JobSubmitResult> {
  formData.set("campaign_id", campaignId);
  const controller = new AbortController();
  // Allow 60s for job submission (file upload may be large).
  const timeout = setTimeout(() => controller.abort(), 60_000);
  const devHeaders: Record<string, string> = IS_DEV
    ? { "X-Dev-User": "dev@proteinea.local" }
    : {};
  try {
    const res = await fetch("/api/jobs/submit", {
      method: "POST",
      body: formData,
      signal: controller.signal,
      headers: devHeaders,
      // Do NOT set Content-Type β€” the browser sets the multipart boundary.
    });
    if (!res.ok) throw new Error(`API ${res.status}`);
    return (await res.json()) as JobSubmitResult;
  } finally {
    clearTimeout(timeout);
  }
}

/** Poll job status. */
export async function getJobStatusAPI(
  jobId: string,
): Promise<JobStatusResult> {
  return fetchJSON<JobStatusResult>(`/api/jobs/${encodeURIComponent(jobId)}`);
}

/** Pre-submit cost estimate for a tool. */
export async function estimateToolCostAPI(
  toolName: string,
  gpuType?: string,
): Promise<CostEstimate> {
  return fetchJSON<CostEstimate>(
    `/api/tools/${encodeURIComponent(toolName)}/estimate`,
    {
      method: "POST",
      body: JSON.stringify(gpuType ? { gpu_type: gpuType } : {}),
    },
  );
}

// ---- Resources ----

export async function listResourcesAPI(params?: {
  type?: ResourceKind;
  q?: string;
}): Promise<{ resources: ResourceRef[]; counts: Record<string, number> }> {
  const qs = new URLSearchParams();
  if (params?.type) qs.set("type", params.type);
  if (params?.q) qs.set("q", params.q);
  const suffix = qs.toString() ? `?${qs.toString()}` : "";
  return fetchJSON<{ resources: ResourceRef[]; counts: Record<string, number> }>(
    `/api/resources${suffix}`,
  );
}

// ---- Datasets ----

export async function searchDatasetsAPI(
  query: Record<string, unknown>,
): Promise<DatasetSearchResponse> {
  return fetchJSON<DatasetSearchResponse>("/api/datasets/search", {
    method: "POST",
    body: JSON.stringify(query),
  });
}

export async function searchDatasetSourceAPI(
  source: string,
  query: Record<string, unknown>,
): Promise<DatasetSearchResult[]> {
  return fetchJSON<DatasetSearchResult[]>(
    `/api/datasets/${encodeURIComponent(source)}/search`,
    {
      method: "POST",
      body: JSON.stringify(query),
    },
  );
}

export async function importDatasetEntriesAPI(
  entries: { source: string; entry_id: string }[],
): Promise<{ artifacts: Artifact[]; errors: Record<string, string> }> {
  return fetchJSON<{ artifacts: Artifact[]; errors: Record<string, string> }>(
    "/api/datasets/import",
    {
      method: "POST",
      body: JSON.stringify({ entries }),
    },
  );
}

export async function getDatasetSyncStatusAPI(): Promise<SyncStatusEntry[]> {
  return fetchJSON<SyncStatusEntry[]>("/api/datasets/sync/status");
}

// ---- Context Enrichment ----

export interface EnrichmentResult {
  entities: Record<string, string[]>;
  context: Record<string, unknown>;
  enriched_prompt: string;
}

export async function enrichContextAPI(
  message: string,
  maxResultsPerSource = 5,
): Promise<EnrichmentResult> {
  return fetchJSON<EnrichmentResult>("/api/context/enrich", {
    method: "POST",
    body: JSON.stringify({ message, max_results_per_source: maxResultsPerSource }),
  });
}

// ---- Legacy catalog endpoints (no server routes yet; fall back to embedded JSON) ----

export async function listBenchmarks(): Promise<{ benchmarks: Benchmark[]; count: number }> {
  try {
    return await fetchJSONAt<{ benchmarks: Benchmark[]; count: number }>("/api/benchmarks", undefined, LEGACY_BASE);
  } catch {
    return { benchmarks: BENCHMARKS, count: BENCHMARKS.length };
  }
}

export async function listModels(params?: {
  capability?: string;
}): Promise<{ models: ModelEntry[]; count: number }> {
  try {
    const qs = params?.capability ? `?capability=${encodeURIComponent(params.capability)}` : "";
    return await fetchJSONAt<{ models: ModelEntry[]; count: number }>(`/api/models${qs}`, undefined, LEGACY_BASE);
  } catch {
    let models = MODELS;
    if (params?.capability) {
      const cap = params.capability;
      models = models.filter((m) => m.capabilities.includes(cap));
    }
    return { models, count: models.length };
  }
}

interface CampaignEntry {
  campaign_id: string;
  conversation_id: string;
  active_jobs: Record<string, string>;
}

export async function listCampaigns(): Promise<{ campaigns: CampaignEntry[]; count: number }> {
  try {
    return await fetchJSONAt<{ campaigns: CampaignEntry[]; count: number }>("/api/campaigns", undefined, LEGACY_BASE);
  } catch {
    return { campaigns: [], count: 0 };
  }
}

// ---- Phylo Backend (Innovation Hub) ----

const PHYLO_BACKEND = process.env.NEXT_PUBLIC_PHYLO_BACKEND_URL || "http://127.0.0.1:8601";

/** GET /api/campaigns β€” list all design campaigns from Phylo backend */
export async function listPhyloCampaigns(): Promise<PhyloCampaign[]> {
  return fetchJSONAt<PhyloCampaign[]>("/api/campaigns", undefined, PHYLO_BACKEND);
}

export interface PhyloCampaign {
  id: string;
  name: string;
  target: string;
  modality: string;
  goal: string;
  constraints?: Record<string, unknown>;
  created_by: string;
  created_at?: string;
}

/** POST /api/campaigns β€” create a new design campaign */
export async function createPhyloCampaign(params: {
  name: string;
  target: string;
  modality: string;
  goal: string;
  constraints?: Record<string, unknown>;
}): Promise<{ id: string; name: string; target: string; [k: string]: unknown }> {
  return fetchJSONAt<{ id: string; name: string; target: string }>(
    "/api/campaigns",
    { method: "POST", body: JSON.stringify(params) },
    PHYLO_BACKEND,
  );
}

/** POST /api/campaigns/{id}/jobs β€” submit a job (multipart form) */
export async function submitPhyloJob(
  campaignId: string,
  formData: FormData,
): Promise<{
  campaign_job_id: string;
  hub_task_execution_id: string;
  provider: string;
  hub_response: unknown;
}> {
  const controller = new AbortController();
  const timeout = setTimeout(() => controller.abort(), 120_000); // jobs can take time
  try {
    const res = await fetch(
      `${PHYLO_BACKEND}/api/campaigns/${encodeURIComponent(campaignId)}/jobs`,
      { method: "POST", body: formData, signal: controller.signal },
    );
    if (!res.ok) throw new Error(`Phylo API ${res.status}: ${await res.text()}`);
    return (await res.json()) as {
      campaign_job_id: string;
      hub_task_execution_id: string;
      provider: string;
      hub_response: unknown;
    };
  } finally {
    clearTimeout(timeout);
  }
}

/** GET /api/jobs/{id} β€” poll job status */
export async function getPhyloJobStatus(jobId: string): Promise<{
  hub: { status: string; output_data?: unknown; cost_estimate?: number; [k: string]: unknown };
  artifacts: { name: string; url: string; mime?: string; bytes?: number }[];
}> {
  return fetchJSONAt(
    `/api/jobs/${encodeURIComponent(jobId)}`,
    undefined,
    PHYLO_BACKEND,
  );
}

/** GET /api/tools β€” list all Hub tools */
export async function listPhyloTools(): Promise<
  { endpoint_name: string; provider: string; [k: string]: unknown }[]
> {
  return fetchJSONAt<
    { endpoint_name: string; provider: string; [k: string]: unknown }[]
  >("/api/tools", undefined, PHYLO_BACKEND);
}

/** POST /api/tools/{name}/estimate β€” cost estimate for a tool run */
export async function estimateToolCost(
  endpointName: string,
  gpuType = "A100",
): Promise<{
  estimated_usd: number;
  gpu_type: string;
  minutes_used: number;
  cost_alert?: string;
}> {
  return fetchJSONAt(
    `/api/tools/${encodeURIComponent(endpointName)}/estimate`,
    { method: "POST", body: JSON.stringify({ gpu_type: gpuType }) },
    PHYLO_BACKEND,
  );
}

// ---- Legacy conversation shims (route into the new task system) ----

/** Create a conversation. Tries the Phylo backend first (creates a task
 * in the local Next.js system, then uses the task ID for the stream).
 * If NEXT_PUBLIC_API_URL is set, falls back to legacy backend. */
export async function createConversation(): Promise<{ conversation_id: string }> {
  // Always create a local task for persistence
  try {
    const project = await ensureDefaultProject();
    const task = await createTask(project.id, "(new conversation)");
    return { conversation_id: task.id };
  } catch {
    // Task system unavailable β€” generate a temp ID (demo mode will activate)
    return { conversation_id: `temp-${Date.now()}` };
  }
}

/** Stream URL β€” points to the Phylo backend's agentic SSE endpoint.
 * This is where Claude processes the message and calls Hub tools. */
export function getStreamUrl(conversationId: string): string {
  return `${PHYLO_BACKEND}/api/tasks/${encodeURIComponent(conversationId)}/messages/stream`;
}

// ---- File Upload ----

export async function uploadFiles(
  taskId: string,
  files: File[],
  note?: string,
): Promise<{ artifacts: Artifact[]; message: ChatMessage }> {
  const form = new FormData();
  for (const file of files) {
    form.append("files", file);
  }
  if (note) form.append("note", note);

  const controller = new AbortController();
  const timeout = setTimeout(() => controller.abort(), 60_000);
  const devHeaders: Record<string, string> = IS_DEV
    ? { "X-Dev-User": "dev@proteinea.local" }
    : {};
  try {
    const res = await fetch(
      `/api/tasks/${encodeURIComponent(taskId)}/upload`,
      {
        method: "POST",
        body: form,
        signal: controller.signal,
        headers: devHeaders,
      },
    );
    if (!res.ok) throw new Error(`API ${res.status}`);
    return (await res.json()) as { artifacts: Artifact[]; message: ChatMessage };
  } finally {
    clearTimeout(timeout);
  }
}

// ---- Knowledge Bases ----

export interface KnowledgeBaseEntry {
  id: string;
  name: string;
  description: string;
  entryCount: number;
  source: "embedded" | "dataset" | "custom";
}

export async function listKnowledgeBases(): Promise<{ knowledgeBases: KnowledgeBaseEntry[] }> {
  return fetchJSON<{ knowledgeBases: KnowledgeBaseEntry[] }>("/api/knowledge-bases");
}

// ---- Task Skills ----

export async function listTaskSkills(taskId: string): Promise<{ skills: unknown[]; taskId: string }> {
  return fetchJSON<{ skills: unknown[]; taskId: string }>(
    `/api/tasks/${encodeURIComponent(taskId)}/skills`,
  );
}
export interface DatasetListEntry {
  id: string;
  name: string;
  source: string;
  type: "structure" | "sequence" | "gene" | "benchmark" | "mixed";
  description: string;
  entryCount: number;
  lastUpdated: string | null;
  sizeBytes: number;
}

export async function listDatasetsAPI(params?: {
  q?: string;
  type?: string;
}): Promise<{ datasets: DatasetListEntry[]; count: number }> {
  const qs = new URLSearchParams();
  if (params?.q) qs.set("q", params.q);
  if (params?.type) qs.set("type", params.type);
  const suffix = qs.toString() ? `?${qs.toString()}` : "";
  return fetchJSON<{ datasets: DatasetListEntry[]; count: number }>(
    `/api/datasets/list${suffix}`,
  );
}