document.addEventListener("DOMContentLoaded", () => {
const webhookUrl = "https://hook.us2.make.com/7hqc4i0d8khtocwo2opyf2nnzec45l4a";
const state = {
stage: "task", // task → validate → research → presentation
userId: (() => {
if (!localStorage.getItem("userId")) {
localStorage.setItem("userId", crypto.randomUUID());
}
return localStorage.getItem("userId");
})()
};
const $msgs = document.getElementById("cwMessages");
const $input = document.getElementById("cwInput");
const $composer = document.getElementById("cwComposer");
const $status = document.getElementById("cwStatus");
const $reset = document.getElementById("cwReset");
function setStatus() {
const map = {
task: "Этап: Формулирование задачи",
validate: "Этап: Уточнение и согласование",
research: "Этап: Сбор информации",
presentation: "Этап: Формирование презентации"
};
$status.textContent = map[state.stage];
}
function addMessage(text, role = "bot") {
const msg = document.createElement("div");
msg.className = "cw-msg " + (role === "user" ? "cw-user" : "cw-bot");
msg.innerText = text;
$msgs.appendChild(msg);
$msgs.scrollTop = $msgs.scrollHeight;
}
function sendToWebhook(payload) {
return fetch(webhookUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload)
}).then(r => r.text());
}
function nextStage() {
if (state.stage === "task") state.stage = "validate";
else if (state.stage === "validate") state.stage = "research";
else if (state.stage === "research") state.stage = "presentation";
setStatus();
}
$composer.addEventListener("submit", async (e) => {
e.preventDefault();
const text = $input.value.trim();
if (!text) return;
addMessage(text, "user");
$input.value = "";
const payload = {
stage: state.stage,
text,
userId: state.userId
};
try {
const response = await sendToWebhook(payload);
addMessage(response || "Нет ответа");
// автоматический переход этапов
nextStage();
} catch (err) {
addMessage("Ошибка соединения");
}
});
$reset.addEventListener("click", () => {
if (confirm("Сбросить диалог?")) {
location.reload();
}
});
// стартовое сообщение
addMessage("Опишите задачу, которую нужно исследовать. Я помогу её уточнить и собрать данные.");
setStatus();
});