File size: 1,235 Bytes
538d81e
f7333d7
 
 
 
 
 
 
 
538d81e
f7333d7
 
e1633a4
538d81e
 
 
 
f7333d7
538d81e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1633a4
538d81e
 
03ab431
 
e1633a4
 
03ab431
538d81e
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
export async function generateAiMixObservation(args: {
  substances: Array<{
    id: string;
    name: string;
    formula: string;
    state: string;
    hazard: string;
    description: string;
  }>;
  temperatureC: number;
  // Back-compat (dev): optional ids
  substanceIds?: string[];
}): Promise<{ observation: string; equation: string; application: string }> {
  const resp = await fetch("/api/ai/mix", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      substances: args.substances,
      substanceIds: args.substanceIds,
      temperatureC: args.temperatureC,
    }),
  });

  if (!resp.ok) {
    const raw = await resp.text().catch(() => "");
    try {
      const parsed = JSON.parse(raw);
      throw new Error(parsed?.error || parsed?.message || `HTTP ${resp.status}`);
    } catch {
      throw new Error(raw || `HTTP ${resp.status}`);
    }
  }

  const data: any = await resp.json();
  const out = data?.text;
  if (!out || !out.observation || !out.equation || !out.application) {
    throw new Error("Reponse IA invalide");
  }
  return {
    observation: out.observation.trim(),
    equation: out.equation.trim(),
    application: out.application.trim()
  };
}