File size: 11,063 Bytes
45a648e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import express from "express";
import Anthropic from "@anthropic-ai/sdk";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import { SYSTEM_PROMPT } from "./kb.js";

const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
app.set("trust proxy", 1); // Render/Fly sit behind a proxy → use X-Forwarded-For
app.use(express.json({ limit: "256kb" }));
app.use(express.static(join(__dirname, "public")));

/* ---------- Lightweight in-memory per-IP rate limiter (no deps) ----------
   Sliding window. Protects the spendy endpoints from abuse / runaway crawlers.
   Tune with env vars; defaults are sensible for a public showcase. */
const RL_WINDOW_MS = Number(process.env.RATE_WINDOW_MS || 60_000);
const RL_MAX = Number(process.env.RATE_MAX || 20); // requests per window per IP
const hits = new Map(); // ip -> number[] (timestamps)
function rateLimit(req, res, next) {
  const ip = req.ip || req.socket.remoteAddress || "unknown";
  const now = Date.now();
  const arr = (hits.get(ip) || []).filter((t) => now - t < RL_WINDOW_MS);
  if (arr.length >= RL_MAX) {
    const retry = Math.ceil((RL_WINDOW_MS - (now - arr[0])) / 1000);
    res.setHeader("Retry-After", String(retry));
    return res.status(429).json({ error: "rate_limited", retry_after: retry });
  }
  arr.push(now);
  hits.set(ip, arr);
  next();
}
// Periodically evict idle IPs so the map can't grow unbounded.
setInterval(() => {
  const now = Date.now();
  for (const [ip, arr] of hits) {
    const live = arr.filter((t) => now - t < RL_WINDOW_MS);
    if (live.length) hits.set(ip, live); else hits.delete(ip);
  }
}, RL_WINDOW_MS).unref();

const MODEL = "claude-opus-4-8";
const hasKey = !!process.env.ANTHROPIC_API_KEY;
const client = hasKey ? new Anthropic() : null;

// Optional shared access code. When ACCESS_CODE is set, the chat endpoint
// requires a matching `x-access-code` header. Leave unset for open local dev.
const ACCESS_CODE = process.env.ACCESS_CODE || "";
const gateOn = !!ACCESS_CODE;
// Constant-time-ish compare to avoid trivial timing leaks.
function codeOk(supplied) {
  if (!gateOn) return true;
  const a = String(supplied || ""), b = ACCESS_CODE;
  if (a.length !== b.length) return false;
  let diff = 0;
  for (let i = 0; i < b.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
  return diff === 0;
}

// Verify an access code (used by the UI login overlay).
app.post("/api/gate", rateLimit, (req, res) => {
  res.json({ ok: codeOk(req.body?.code) });
});

/* ---------- Deterministic safety gate (runs BEFORE the LLM) ----------
   Clinical/emergency questions never reach the model. This is the
   defense-in-depth layer; the system prompt is the second layer. */
const EMERGENCY = /(brustschmerz|chest pain|herzinfarkt|heart attack|schlaganfall|stroke|atemnot|can'?t breathe|cannot breathe|trouble breathing|bewusstlos|unconscious|überdosis|overdose|stark.*blut|severe bleeding|anaphyla|suizid|suicid|umbringen|kill myself|notfall|боль в груди|задыхаюсь|без сознания|ألم في الصدر|إغماء|نوبة قلبية)/i;
const CLINICAL = /(wie ?viel|how much|how many|dosier|dosage|\bdose\b|einnehmen|should i take|darf ich.*nehmen|can i take|wechselwirk|interaction|kombinier|combine|zusammen.*einnehmen|nebenwirkung|side effect|symptom|diagnos|krankheit|\bdisease\b|schwanger|pregnan|behandeln|\btreat\b|wogegen hilft|what should i take|is it safe to|gefährlich.*nehmen|сколько.*принимать|дозиров|можно ли принимать|كم.*جرعة|الجرعة|تفاعل)/i;
const PII = /(r[üue]+ckruf|call me back|ruf(t|en sie)? mich.*(zur[üue]+ck)|zur[üue]+ckruf|обратный звонок|معاودة الاتصال|geri arama)/i;

function classify(text) {
  if (EMERGENCY.test(text)) return "EMERGENCY";
  if (CLINICAL.test(text)) return "CLINICAL";
  if (PII.test(text)) return "PII";
  return "SAFE";
}

// Localised fixed templates for the gated categories (no LLM call needed).
function detectLang(s) {
  const x = " " + s.toLowerCase() + " ";
  if (/[؀-ۿ]/.test(s)) return "ar";
  if (/[Ѐ-ӿ]/.test(s)) return "ru";
  if (/[şğıİ]|merhaba|eczane|hangi/.test(x)) return "tr";
  if (/\b(how|what|where|can|i|the|my|chest|take|emergency|hours|pharmacy)\b/.test(x)) return "en";
  return "de";
}
const TPL = {
  EMERGENCY: {
    de: "🚨 Das klingt nach einem Notfall. Bitte rufen Sie sofort den Rettungsdienst: 112. Die diensthabende Notapotheke finden Sie tagesaktuell über die Notdienstsuche (aponet.de). Bleiben Sie ruhig – Hilfe ist unterwegs.",
    en: "🚨 This sounds like an emergency. Please call emergency services immediately: 112. You can find the on-duty pharmacy via the official Notdienst search (aponet.de). Stay calm — help is on the way.",
    tr: "🚨 Bu acil bir durum gibi görünüyor. Lütfen hemen 112'yi arayın. Nöbetçi eczaneyi aponet.de üzerinden bulabilirsiniz. Sakin olun, yardım yolda.",
    ru: "🚨 Похоже на экстренный случай. Срочно позвоните 112. Дежурную аптеку можно найти на aponet.de. Сохраняйте спокойствие, помощь уже в пути.",
    ar: "🚨 يبدو أنها حالة طارئة. يُرجى الاتصال فوراً بالرقم 112. يمكنك إيجاد صيدلية الطوارئ عبر aponet.de. ابقَ هادئاً، المساعدة في الطريق.",
  },
  CLINICAL: {
    de: "Das ist eine medizinische Frage, die ich nicht beantworten darf. 🩺 Das bespricht am besten unser Apothekenteam persönlich mit Ihnen. Möchten Sie einen Termin, einen Rückruf oder die nächste Filiale?",
    en: "That's a medical question I'm not allowed to answer. 🩺 Our pharmacy team is the right place for this. Would you like an appointment, a callback, or your nearest branch?",
    tr: "Bu tıbbi bir soru ve yanıtlayamam. 🩺 Bunu en iyi eczane ekibimiz değerlendirir. Randevu, geri arama veya en yakın şube ister misiniz?",
    ru: "Это медицинский вопрос, на который я не могу ответить. 🩺 Лучше всего поможет наша команда аптеки. Хотите записаться, заказать обратный звонок или узнать ближайший филиал?",
    ar: "هذا سؤال طبي لا يمكنني الإجابة عليه. 🩺 فريق الصيدلية هو الأفضل لمساعدتك. هل تريد موعداً أو معاودة اتصال أو أقرب فرع؟",
  },
  PII: {
    de: "📞 Gern! Bitte hinterlassen Sie Name und Telefonnummer, dann ruft unser Team zurück. (Hinweis: In dieser Demo werden keine echten Daten gespeichert; in der Produktion würde vorab Ihre Einwilligung eingeholt.)",
    en: "📞 Of course! Please leave your name and phone number and our team will call you back. (Note: no real data is stored in this demo; in production we'd request your consent first.)",
    tr: "📞 Tabii! Lütfen adınızı ve telefon numaranızı bırakın, ekibimiz sizi arasın. (Not: bu demoda gerçek veri saklanmaz.)",
    ru: "📞 Конечно! Оставьте имя и номер телефона, и наша команда перезвонит. (В этой демоверсии данные не сохраняются.)",
    ar: "📞 بالطبع! يُرجى ترك اسمك ورقم هاتفك وسيعاود فريقنا الاتصال بك. (ملاحظة: لا تُحفظ بيانات حقيقية في هذا العرض.)",
  },
};
function template(cat, lang) {
  const m = TPL[cat];
  return (m && (m[lang] || m.en)) || "";
}

/* ---------------------- Chat endpoint (SSE stream) ---------------------- */
app.post("/api/chat", rateLimit, async (req, res) => {
  if (!codeOk(req.get("x-access-code"))) {
    return res.status(401).json({ error: "unauthorized" });
  }
  const messages = Array.isArray(req.body?.messages) ? req.body.messages : [];
  const last = [...messages].reverse().find((m) => m.role === "user");
  const userText = last?.content || "";
  const forcedLang = req.body?.lang && req.body.lang !== "auto" ? req.body.lang : null;
  const lang = forcedLang || detectLang(userText);

  // SSE headers
  res.setHeader("Content-Type", "text/event-stream");
  res.setHeader("Cache-Control", "no-cache");
  res.setHeader("Connection", "keep-alive");
  const send = (obj) => res.write(`data: ${JSON.stringify(obj)}\n\n`);

  const cat = classify(userText);

  // Gated categories → deterministic template, no model call.
  if (cat !== "SAFE") {
    send({ type: "meta", category: cat });
    send({ type: "delta", text: template(cat, lang) });
    send({ type: "done" });
    return res.end();
  }

  send({ type: "meta", category: "SAFE" });

  if (!hasKey) {
    send({
      type: "delta",
      text:
        "⚙️ Demo backend is running, but no ANTHROPIC_API_KEY is configured, so the live model isn't connected.\n\nSet the key and restart to enable real AI answers:\n  • PowerShell:  $env:ANTHROPIC_API_KEY=\"sk-ant-...\"; npm start\n\n(The safety gate above — emergency & clinical routing — works without a key.)",
    });
    send({ type: "done" });
    return res.end();
  }

  try {
    // Build clean history for the API (user/assistant only).
    const apiMessages = messages
      .filter((m) => m.role === "user" || m.role === "assistant")
      .map((m) => ({ role: m.role, content: String(m.content || "") }))
      .slice(-12);
    if (apiMessages.length === 0 || apiMessages[0].role !== "user") {
      apiMessages.unshift({ role: "user", content: userText });
    }

    const stream = client.messages.stream({
      model: MODEL,
      max_tokens: 1024,
      // No thinking → snappy FAQ-style replies; system prompt forbids exposing reasoning.
      system: [{ type: "text", text: SYSTEM_PROMPT, cache_control: { type: "ephemeral" } }],
      messages: apiMessages,
    });

    stream.on("text", (delta) => send({ type: "delta", text: delta }));
    const final = await stream.finalMessage();
    send({ type: "done", usage: final.usage });
    res.end();
  } catch (err) {
    console.error("LLM error:", err?.message || err);
    send({
      type: "delta",
      text:
        lang === "de"
          ? "Entschuldigung, es gab gerade ein technisches Problem. Bitte versuchen Sie es erneut oder kontaktieren Sie Ihre SaniPlus-Filiale."
          : "Sorry, there was a technical problem. Please try again or contact your SaniPlus branch.",
    });
    send({ type: "done", error: true });
    res.end();
  }
});

app.get("/api/health", (_req, res) => res.json({ ok: true, model: MODEL, llm: hasKey, gate: gateOn }));

const PORT = process.env.PORT || 8100;
app.listen(PORT, () => {
  console.log(`SaniPlus LLM chatbot on http://localhost:${PORT}`);
  console.log(hasKey ? "LLM: connected (Claude)" : "LLM: NO API KEY — safety gate works, model answers disabled");
  console.log(gateOn ? "Access gate: ON (ACCESS_CODE required)" : "Access gate: OFF (open)");
});