/// routerAdd( "POST", "/api/mdb/generate-rapport", (e) => { if (!e.auth) { return e.json(401, { message: "Non autorisé" }); } const body = e.requestInfo().body || {}; const notes_brutes = typeof body.notes_brutes === "string" ? body.notes_brutes : ""; const checklist_reponses = typeof body.checklist_reponses === "object" && body.checklist_reponses != null ? body.checklist_reponses : {}; const bien_info = typeof body.bien_info === "object" && body.bien_info != null ? body.bien_info : {}; const key = $os.getenv("ANTHROPIC_API_KEY"); if (!key) { return e.json(500, { message: "ANTHROPIC_API_KEY manquante sur le serveur" }); } const payload = { model: "claude-sonnet-4-20250514", max_tokens: 1500, messages: [ { role: "user", content: "Génère un compte-rendu de visite professionnel en français (titres courts, listes à puces si utile). Bien: " + JSON.stringify(bien_info) + "\n\nNotes libres:\n" + notes_brutes + "\n\nChecklist (états: ok, attention, probleme, non):\n" + JSON.stringify(checklist_reponses), }, ], }; const res = $http.send({ url: "https://api.anthropic.com/v1/messages", method: "POST", headers: { "x-api-key": key, "anthropic-version": "2023-06-01", "content-type": "application/json", }, body: JSON.stringify(payload), timeout: 90, }); if (res.statusCode >= 400) { const errText = typeof res.raw === "string" ? res.raw : ""; return e.json(502, { message: "Réponse Anthropic invalide", statusCode: res.statusCode, detail: errText.slice(0, 2000), }); } let parsed = res.json; if (parsed == null && typeof res.raw === "string" && res.raw.length > 0) { try { parsed = JSON.parse(res.raw); } catch (_) { parsed = null; } } const text = parsed && Array.isArray(parsed.content) && parsed.content[0] && parsed.content[0].text ? parsed.content[0].text : ""; return e.json(200, { rapport: text }); }, $apis.requireAuth(), );