Spaces:
Sleeping
Sleeping
| 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() | |
| }; | |
| } | |