import { reply } from 'backend/ai';
import wixLocation from 'wix-location';
let messages = [];
$w.onReady(() => {
$w('#crisisBox').collapse();
$w('#disclaimerText').text = "Educational support only — not a crisis service or a substitute for therapy. Do not share personal health information here. For personalized care, contact a licensed professional.";
$w('#chatLog').text = "Assistant: Hi! I can share skills and psychoeducation. I’m not a crisis or therapy service. What would you like help with today?\n";
$w('#sendBtn').onClick(handleSend);
$w('#userInput').onKeyPress((event) => {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault();
handleSend();
}
});
// Optional quick reply buttons
if ($w('#qrBreathing') && $w('#qrGrounding') && $w('#qrCoping')) {
$w('#qrBreathing').onClick(() => injectQuick("Can you guide a 1-minute breathing exercise using the TIPP skill?"));
$w('#qrGrounding').onClick(() => injectQuick("Walk me through the 5-4-3-2-1 grounding technique in 60 seconds."));
$w('#qrCoping').onClick(() => injectQuick("Give me a tiny coping plan for evening anxiety—3 steps."));
}
$w('#resourcesBtn').onClick(() => wixLocation.to("https://988lifeline.org/"));
});
async function handleSend() {
const userText = ($w('#userInput').value || '').trim();
if (!userText) return;
appendLine(`You: ${userText}`);
$w('#userInput').value = '';
setBusy(true);
try {
const r = await reply(userText);
if (r.kind === 'crisis') {
showCrisis(r.message);
appendLine(`Assistant: ${stripMd(r.message)}`);
return;
}
appendLine(`Assistant: ${stripMd(r.message)}`);
} catch (e) {
appendLine("Assistant: Sorry—something went wrong. Please try again in a moment.");
console.error(e);
} finally {
setBusy(false);
}
}
function injectQuick(text) {
$w('#userInput').value = text;
handleSend();
}
function appendLine(line) {
messages.push(line);
$w('#chatLog').text = messages.join('\n');
}
function showCrisis(text) {
$w('#crisisText').text = stripMd(text);
if ($w('#crisisBox').collapsed) $w('#crisisBox').expand();
}
function setBusy(isBusy) {
$w('#sendBtn').label = isBusy ? 'Sending…' : 'Send';
$w('#sendBtn').disable(isBusy);
}
function stripMd(s='') {
return s.replace(/\*\*(.*?)\*\*/g, '$1').replace(/\*(.*?)\*/g, '$1');
}
top of page
Resources
This Chat AI is for Educational support only — not a crisis service or a substitute for therapy. Do not share personal health information here.