forked from Rockachopa/the-matrix
Implements the minimum viable conversation loop for Workshop #222: visitor arrives → sends message → Timmy barks back. - js/visitor.js: Visitor presence protocol (#41) - visitor_entered on load (with device detection: ipad/desktop/mobile) - visitor_left on unload or 30s hidden (iPad tab suspend) - visitor_message dispatched from chat input - visitor_interaction export for future tap-to-interact (#44) - Session duration tracking - js/bark.js: Bark display system (#42) - showBark() renders prominent viewport toasts with typing animation - Auto-dismiss after display time + typing duration - Queue system (max 3 simultaneous, overflow queued) - Demo barks in mock mode (Workshop-themed: 222, sovereignty, chain) - Barks also logged permanently in chat panel - index.html: Chat input bar (#40) - Terminal-styled input + send button at viewport bottom - Enter to send (desktop), button tap (iPad) - Safe-area padding for notched devices - Chat panel repositioned above input bar - Bark container in upper viewport third - js/websocket.js: New message handlers - 'bark' message → showBark() dispatch - 'ambient_state' message → placeholder for #43 - Demo barks start in mock mode - js/ui.js: appendChatMessage() accepts optional CSS class - Visitor messages styled differently from agent messages Build: 18 modules, 0 errors Tested: desktop (1280x800) + mobile (390x844) via Playwright Closes #40, #41, #42 Ref: rockachopa/Timmy-time-dashboard#222, #243
108 lines
3.3 KiB
JavaScript
108 lines
3.3 KiB
JavaScript
import { getAgentDefs } from './agents.js';
|
|
import { colorToCss } from './agent-defs.js';
|
|
|
|
const $agentCount = document.getElementById('agent-count');
|
|
const $activeJobs = document.getElementById('active-jobs');
|
|
const $fps = document.getElementById('fps');
|
|
const $agentList = document.getElementById('agent-list');
|
|
const $connStatus = document.getElementById('connection-status');
|
|
const $chatPanel = document.getElementById('chat-panel');
|
|
|
|
const MAX_CHAT_ENTRIES = 12;
|
|
const chatEntries = [];
|
|
|
|
const IDLE_COLOR = '#005500';
|
|
const ACTIVE_COLOR = '#00ff41';
|
|
|
|
export function initUI() {
|
|
renderAgentList();
|
|
}
|
|
|
|
function renderAgentList() {
|
|
const defs = getAgentDefs();
|
|
$agentList.innerHTML = defs.map(a => {
|
|
const css = escapeAttr(colorToCss(a.color));
|
|
const safeLabel = escapeHtml(a.label);
|
|
const safeId = escapeAttr(a.id);
|
|
return `<div class="agent-row">
|
|
<span class="label">[</span>
|
|
<span style="color:${css}">${safeLabel}</span>
|
|
<span class="label">]</span>
|
|
<span id="agent-state-${safeId}" style="color:${IDLE_COLOR}"> IDLE</span>
|
|
</div>`;
|
|
}).join('');
|
|
}
|
|
|
|
export function updateUI({ fps, agentCount, jobCount, connectionState }) {
|
|
$fps.textContent = `FPS: ${fps}`;
|
|
$agentCount.textContent = `AGENTS: ${agentCount}`;
|
|
$activeJobs.textContent = `JOBS: ${jobCount}`;
|
|
|
|
if (connectionState === 'connected') {
|
|
$connStatus.textContent = '● CONNECTED';
|
|
$connStatus.className = 'connected';
|
|
} else if (connectionState === 'connecting') {
|
|
$connStatus.textContent = '◌ CONNECTING...';
|
|
$connStatus.className = '';
|
|
} else {
|
|
$connStatus.textContent = '○ OFFLINE';
|
|
$connStatus.className = '';
|
|
}
|
|
|
|
const defs = getAgentDefs();
|
|
defs.forEach(a => {
|
|
const el = document.getElementById(`agent-state-${a.id}`);
|
|
if (el) {
|
|
el.textContent = ` ${a.state.toUpperCase()}`;
|
|
el.style.color = a.state === 'active' ? ACTIVE_COLOR : IDLE_COLOR;
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Append a line to the chat panel.
|
|
* @param {string} agentLabel — display name
|
|
* @param {string} message — message text (HTML-escaped before insertion)
|
|
* @param {string} cssColor — CSS color string, e.g. '#00ff88'
|
|
*/
|
|
export function appendChatMessage(agentLabel, message, cssColor, extraClass) {
|
|
const color = escapeAttr(cssColor || '#00ff41');
|
|
const entry = document.createElement('div');
|
|
entry.className = 'chat-entry' + (extraClass ? ' ' + extraClass : '');
|
|
entry.innerHTML = `<span class="agent-name" style="color:${color}">${escapeHtml(agentLabel)}</span>: ${escapeHtml(message)}`;
|
|
|
|
chatEntries.push(entry);
|
|
|
|
while (chatEntries.length > MAX_CHAT_ENTRIES) {
|
|
const removed = chatEntries.shift();
|
|
try { $chatPanel.removeChild(removed); } catch { /* already removed */ }
|
|
}
|
|
|
|
$chatPanel.appendChild(entry);
|
|
$chatPanel.scrollTop = $chatPanel.scrollHeight;
|
|
}
|
|
|
|
/**
|
|
* Escape HTML text content — prevents tag injection.
|
|
*/
|
|
function escapeHtml(str) {
|
|
return String(str)
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''');
|
|
}
|
|
|
|
/**
|
|
* Escape a value for use inside an HTML attribute (style="...", id="...").
|
|
*/
|
|
function escapeAttr(str) {
|
|
return String(str)
|
|
.replace(/&/g, '&')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>');
|
|
}
|