generalbots/botui/ui/suite/chat/chat-init.js
Rodrigo Rodriguez be190bd0a4 fix: Export mention handlers to window scope
- Export handleMentionInput, handleMentionKeydown, hideMentionDropdown to window object
- Fix chat-init.js to use window.handleMentionInput with proper checks
- Prevents ReferenceError when chat initializes
- Ensures suggestion buttons and switchers work correctly
2026-05-01 21:57:54 -03:00

163 lines
4.5 KiB
JavaScript

function sendMessage(messageContent) {
var input = document.getElementById("messageInput");
if (!input) return;
var content = messageContent || input.value.trim();
if (!content) return;
if (ChatState.isStreaming && ChatState.streamingMessageId) {
finalizeStreaming();
ChatState.isStreaming = false;
}
if (!messageContent) {
hideMentionDropdown();
input.value = "";
input.focus();
}
addMessage("user", content);
if (ChatState.ws && ChatState.ws.readyState === WebSocket.OPEN) {
ChatState.ws.send(JSON.stringify({
bot_id: ChatState.currentBotId,
user_id: ChatState.currentUserId,
session_id: ChatState.currentSessionId,
channel: "web",
content: content,
message_type: MessageType.USER,
active_switchers: Array.from(ChatState.activeSwitchers),
timestamp: new Date().toISOString(),
}));
} else {
notify("Not connected to server. Message not sent.", "warning");
}
}
window.sendMessage = sendMessage;
window.getChatSessionInfo = function () {
return {
ws: ChatState.ws,
currentBotId: ChatState.currentBotId,
currentUserId: ChatState.currentUserId,
currentSessionId: ChatState.currentSessionId,
currentBotName: ChatState.currentBotName,
};
};
function proceedWithChatInit() {
var botName = window.__INITIAL_BOT_NAME__ || "default";
var storageKey = "gb_chat_" + botName;
var stored = {};
try { stored = JSON.parse(localStorage.getItem(storageKey) || "{}"); } catch (e) {}
var authUrl = "/api/auth?bot_name=" + encodeURIComponent(botName);
if (stored.user_id) authUrl += "&user_id=" + encodeURIComponent(stored.user_id);
if (stored.session_id) authUrl += "&session_id=" + encodeURIComponent(stored.session_id);
fetch(authUrl)
.then(function (response) { return response.json(); })
.then(function (auth) {
ChatState.currentUserId = auth.user_id;
ChatState.currentSessionId = auth.session_id;
ChatState.currentBotId = auth.bot_id || "default";
ChatState.currentBotName = botName;
try {
localStorage.setItem(storageKey, JSON.stringify({ user_id: auth.user_id, session_id: auth.session_id }));
} catch (e) {}
connectWebSocket();
})
.catch(function () {
notify("Failed to connect to chat server", "error");
setTimeout(proceedWithChatInit, 3000);
});
}
function setupEventHandlers() {
var form = document.getElementById("chatForm");
var input = document.getElementById("messageInput");
var sendBtn = document.getElementById("sendBtn");
if (form) {
form.onsubmit = function (e) { e.preventDefault(); sendMessage(); return false; };
}
if (input) {
// Only attach mention handlers if they exist
var mentionInputHandler = window.handleMentionInput;
var mentionKeydownHandler = window.handleMentionKeydown;
if (mentionInputHandler) {
input.addEventListener("input", mentionInputHandler);
}
if (mentionKeydownHandler) {
input.onkeydown = function (e) {
if (mentionKeydownHandler(e)) return;
if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); sendMessage(); }
};
} else {
input.onkeydown = function (e) {
if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); sendMessage(); }
};
}
}
if (sendBtn) {
sendBtn.onclick = function (e) { e.preventDefault(); sendMessage(); };
}
var scrollBtn = document.getElementById("scrollToBottom");
if (scrollBtn) {
scrollBtn.addEventListener("click", function () { scrollToBottom(true); ChatState.isUserScrolling = false; });
}
var messagesEl = document.getElementById("messages");
if (messagesEl) {
messagesEl.addEventListener("scroll", function () {
ChatState.isUserScrolling = true;
updateScrollButton();
clearTimeout(messagesEl.scrollTimeout);
messagesEl.scrollTimeout = setTimeout(function () { ChatState.isUserScrolling = false; }, 1000);
});
}
document.addEventListener("click", function (e) {
if (!e.target.closest("#mentionDropdown") && !e.target.closest("#messageInput")) {
var hideMention = window.hideMentionDropdown;
if (hideMention) {
hideMention();
}
}
});
}
function initChat() {
if (typeof loadBotConfig === 'function') {
loadBotConfig();
}
proceedWithChatInit();
}
function showChatApp() {
var chatApp = document.getElementById("chat-app");
if (chatApp) {
chatApp.style.opacity = "1";
chatApp.style.visibility = "visible";
}
}
window.showChatApp = showChatApp;
// Wait for DOM to be ready before initializing
if (typeof document !== 'undefined') {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function() {
setupEventHandlers();
initChat();
});
} else {
setupEventHandlers();
initChat();
}
}