File size: 1,660 Bytes
f8a3ca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { BayesResult, SampleInfo } from "./types";

const BASE = import.meta.env.VITE_API_BASE_URL || "";

async function jsonFetch<T>(path: string, init?: RequestInit): Promise<T> {
  const r = await fetch(BASE + path, {
    headers: { "Content-Type": "application/json" },
    ...init,
  });
  if (!r.ok) {
    const text = await r.text().catch(() => "");
    throw new Error(`${r.status} ${r.statusText}: ${text || path}`);
  }
  return (await r.json()) as T;
}

export const api = {
  health: () =>
    jsonFetch<{
      status: string;
      service: string;
      version: string;
      anthropic_key_present: boolean;
      anthropic_model: string;
    }>("/api/health"),

  listSamples: () => jsonFetch<SampleInfo[]>("/api/data/samples"),

  getSample: (id: string) =>
    jsonFetch<SampleInfo & { values: number[] }>(`/api/data/samples/${id}`),

  bayesianCompute: (body: {
    data: number[];
    judgments: number[];
    R: number;
    scenario_name?: string;
    reference_case?: string;
  }) =>
    jsonFetch<BayesResult>("/api/bayesian/compute", {
      method: "POST",
      body: JSON.stringify(body),
    }),

  bayesianKDE: (body: { data: number[]; judgments: number[]; R: number }) =>
    jsonFetch<{ points: Array<{ x: number; y: number }> }>(
      "/api/bayesian/kde",
      { method: "POST", body: JSON.stringify(body) }
    ),

  bayesianReport: (body: {
    data: number[];
    judgments: number[];
    R: number;
    scenario_name?: string;
    reference_case?: string;
  }) =>
    jsonFetch<{ markdown: string; result: BayesResult }>(
      "/api/bayesian/report",
      { method: "POST", body: JSON.stringify(body) }
    ),
};