|
| 1 | +// ========================= |
| 2 | +// Section: Globals & Setup |
| 3 | +// ========================= |
| 4 | +const chatSessions = new Map(); // key: agentId, value: ChatSession |
| 5 | + |
| 6 | +window.addEventListener("beforeunload", persistChatSessions); |
| 7 | + |
| 8 | +// Restore on page load |
| 9 | +(function restoreSessions() { |
| 10 | + const saved = localStorage.getItem("openChats"); |
| 11 | + if (!saved) return; |
| 12 | + |
| 13 | + const chatData = JSON.parse(saved); |
| 14 | + for (const [agentId, data] of Object.entries(chatData)) { |
| 15 | + const session = new ChatSession(data.agentId, data.sessionId, data.agentHost, data.messages); |
| 16 | + chatSessions.set(agentId, session); |
| 17 | + } |
| 18 | +})(); |
| 19 | + |
| 20 | +// ========================= |
| 21 | +// Section: ChatSession Class |
| 22 | +// ========================= |
| 23 | +class ChatSession { |
| 24 | + constructor(agentId, sessionId, agentHost, preloadMessages = []) { |
| 25 | + this.agentId = agentId; |
| 26 | + this.sessionId = sessionId; |
| 27 | + this.agentHost = agentHost; |
| 28 | + this.chatGroupId = `${agentId}-${sessionId}`; |
| 29 | + this.messages = preloadMessages || []; |
| 30 | + this.connection = null; |
| 31 | + |
| 32 | + this.connect(); |
| 33 | + } |
| 34 | + |
| 35 | + connect() { |
| 36 | + const protocol = location.protocol === "https:" ? "https" : "http"; |
| 37 | + const uri = `${protocol}://${this.agentHost}/api/v1/chat/attach/subscribe?sessionId=${encodeURIComponent(this.sessionId)}&chatGroupId=${this.chatGroupId}`; |
| 38 | + this.connection = new SockJS(uri); |
| 39 | + this.connection.onmessage = (e) => this.handleMessage(e); |
| 40 | + this.connection.onopen = () => this.heartbeat(); |
| 41 | + } |
| 42 | + |
| 43 | + handleMessage(e) { |
| 44 | + try { |
| 45 | + const binary = Uint8Array.from(atob(e.data), c => c.charCodeAt(0)); |
| 46 | + const msg = proto.io.sentrius.protobuf.ChatMessage.deserializeBinary(binary); |
| 47 | + const sender = msg.getSender(); |
| 48 | + const message = msg.getMessage(); |
| 49 | + this.messages.push({ sender, message }); |
| 50 | + |
| 51 | + const activeAgentId = document.getElementById("chat-container").dataset.agentId; |
| 52 | + if (activeAgentId === this.agentId) { |
| 53 | + appendToChatWindow(sender, message); |
| 54 | + } |
| 55 | + |
| 56 | + persistChatSessions(); |
| 57 | + } catch (err) { |
| 58 | + console.error("Failed to handle chat message", err); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + send(text) { |
| 63 | + const msg = new proto.io.sentrius.protobuf.ChatMessage(); |
| 64 | + msg.setSender("user"); |
| 65 | + msg.setMessage(text); |
| 66 | + |
| 67 | + this.connection.send(btoa(String.fromCharCode(...msg.serializeBinary()))); |
| 68 | + this.messages.push({ sender: "You", message: text }); |
| 69 | + |
| 70 | + const activeAgentId = document.getElementById("chat-container").dataset.agentId; |
| 71 | + if (activeAgentId === this.agentId) { |
| 72 | + appendToChatWindow("You", text); |
| 73 | + } |
| 74 | + |
| 75 | + persistChatSessions(); |
| 76 | + } |
| 77 | + |
| 78 | + heartbeat() { |
| 79 | + if (!this.connection || this.connection.readyState !== 1) return; |
| 80 | + |
| 81 | + const msg = new proto.io.sentrius.protobuf.ChatMessage(); |
| 82 | + msg.setSender("system"); |
| 83 | + msg.setMessage("heartbeat"); |
| 84 | + |
| 85 | + this.connection.send(btoa(String.fromCharCode(...msg.serializeBinary()))); |
| 86 | + setTimeout(() => this.heartbeat(), 5000); |
| 87 | + } |
| 88 | + |
| 89 | + toJSON() { |
| 90 | + return { |
| 91 | + agentId: this.agentId, |
| 92 | + sessionId: this.sessionId, |
| 93 | + agentHost: this.agentHost, |
| 94 | + messages: this.messages |
| 95 | + }; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// ========================= |
| 100 | +// Section: UI Interaction |
| 101 | +// ========================= |
| 102 | + |
| 103 | +function switchToAgent(agentId, sessionId, agentHost) { |
| 104 | + let session = chatSessions.get(agentId); |
| 105 | + if (!session) { |
| 106 | + session = new ChatSession(agentId, sessionId, agentHost); |
| 107 | + chatSessions.set(agentId, session); |
| 108 | + } |
| 109 | + |
| 110 | + const container = document.getElementById("chat-container"); |
| 111 | + container.dataset.agentId = agentId; |
| 112 | + container.dataset.agentHost = agentHost; |
| 113 | + document.getElementById("chat-agent-name").textContent = agentId; |
| 114 | + |
| 115 | + const messagesDiv = document.getElementById("chat-messages"); |
| 116 | + messagesDiv.innerHTML = ""; |
| 117 | + session.messages.forEach(msg => { |
| 118 | + appendToChatWindow(msg.sender, msg.message); |
| 119 | + }); |
| 120 | + |
| 121 | + container.style.display = "block"; |
| 122 | +} |
| 123 | + |
| 124 | +function sendMessage(event) { |
| 125 | + if (event.key !== "Enter") return; |
| 126 | + |
| 127 | + const input = document.getElementById("chat-input"); |
| 128 | + const messageText = input.value.trim(); |
| 129 | + if (!messageText) return; |
| 130 | + |
| 131 | + const container = document.getElementById("chat-container"); |
| 132 | + const agentId = container.dataset.agentId; |
| 133 | + const session = chatSessions.get(agentId); |
| 134 | + if (session) { |
| 135 | + session.send(messageText); |
| 136 | + } |
| 137 | + |
| 138 | + input.value = ""; |
| 139 | +} |
| 140 | + |
| 141 | +function appendToChatWindow(sender, message) { |
| 142 | + const chatBox = document.getElementById("chat-messages"); |
| 143 | + const div = document.createElement("div"); |
| 144 | + div.classList.add("chat-message"); |
| 145 | + div.innerHTML = `<strong>${sender}:</strong> ${message}`; |
| 146 | + chatBox.appendChild(div); |
| 147 | + chatBox.scrollTop = chatBox.scrollHeight; |
| 148 | +} |
| 149 | + |
| 150 | +function toggleChat() { |
| 151 | + const container = document.getElementById("chat-container"); |
| 152 | + container.classList.toggle("hidden"); |
| 153 | +} |
| 154 | + |
| 155 | +// ========================= |
| 156 | +// Section: Persistence |
| 157 | +// ========================= |
| 158 | +function persistChatSessions() { |
| 159 | + const obj = {}; |
| 160 | + chatSessions.forEach((session, agentId) => { |
| 161 | + obj[agentId] = session.toJSON(); |
| 162 | + }); |
| 163 | + localStorage.setItem("openChats", JSON.stringify(obj)); |
| 164 | +} |
0 commit comments