interplane.Get Omega
Read in your language

MojoLlama

Archived Mojo/MAX quantizer and serving research repository (read-only history)

Brought by drakestapleton

branch main Branches Tags History

MojoLlama / desktop / renderer / log.js

232 lines · 7752 bytes Raw Blame History
1/**
2 * MojoLlama Studio — Console Log Renderer
3 *
4 * Displays real-time server output with filtering, search, and
5 * auto-scrolling support.
6 */
7
8const api = window.mojollama;
9let entries = []; // all log entries
10let filteredEntries = []; // entries after filter + search
11let activeLevels = new Set(['all', 'info', 'warn', 'error', 'debug']);
12let statusUnsubscribe = null;
13let logUnsubscribe = null;
14let clearUnsubscribe = null;
15let autoScroll = true;
16
17// ─── Init ────────────────────────────────────────────────────────────────────
18
19async function init() {
20 // Load existing log buffer
21 try {
22 const buffer = await api.logGet();
23 entries = buffer || [];
24 applyFilter();
25 } catch (err) {
26 console.error('Failed to load log buffer:', err);
27 }
28
29 // Listen for new entries
30 if (api.onLogEntry) {
31 logUnsubscribe = api.onLogEntry((entry) => {
32 entries.push(entry);
33 if (shouldShowEntry(entry)) {
34 filteredEntries.push(entry);
35 appendEntryDOM(entry);
36 updateCounts();
37 if (autoScroll) scrollToBottom();
38 }
39 });
40 }
41
42 // Listen for log cleared
43 if (api.onLogCleared) {
44 clearUnsubscribe = api.onLogCleared(() => {
45 entries = [];
46 filteredEntries = [];
47 document.getElementById('logContainer').innerHTML =
48 '<div class="empty-state">Log cleared.</div>';
49 updateCounts();
50 });
51 }
52
53 // Listen for server status
54 if (api.onServerStatus) {
55 statusUnsubscribe = api.onServerStatus(updateStatus);
56 }
57
58 // Fetch current status
59 try {
60 const status = await api.serverStatus();
61 updateStatus(status);
62 } catch (err) {
63 console.error('Failed to get server status:', err);
64 }
65}
66
67// ─── Filtering ───────────────────────────────────────────────────────────────
68
69function shouldShowEntry(entry) {
70 // Level filter
71 if (!activeLevels.has('all') && !activeLevels.has(entry.level)) {
72 return false;
73 }
74
75 // Search text filter
76 const searchText = document.getElementById('searchInput').value.toLowerCase().trim();
77 if (searchText) {
78 const text = (entry.text || '').toLowerCase();
79 const level = (entry.level || '').toLowerCase();
80 const ts = (entry.timestamp || '').toLowerCase();
81 if (!text.includes(searchText) && !level.includes(searchText) && !ts.includes(searchText)) {
82 return false;
83 }
84 }
85
86 return true;
87}
88
89function applyFilter() {
90 filteredEntries = entries.filter(e => shouldShowEntry(e));
91
92 const container = document.getElementById('logContainer');
93 container.innerHTML = '';
94
95 if (filteredEntries.length === 0) {
96 container.innerHTML = entries.length === 0
97 ? '<div class="empty-state">Waiting for server output...</div>'
98 : '<div class="empty-state">No entries match the current filter.</div>';
99 updateCounts();
100 return;
101 }
102
103 // Batch DOM append for performance
104 const fragment = document.createDocumentFragment();
105 for (const entry of filteredEntries) {
106 fragment.appendChild(createEntryElement(entry));
107 }
108 container.appendChild(fragment);
109 updateCounts();
110
111 if (autoScroll) scrollToBottom();
112}
113
114function toggleFilter(btn, level) {
115 btn.classList.toggle('active');
116 if (activeLevels.has(level)) {
117 activeLevels.delete(level);
118 } else {
119 activeLevels.add(level);
120 }
121 applyFilter();
122}
123
124// ─── DOM rendering ───────────────────────────────────────────────────────────
125
126function createEntryElement(entry) {
127 const div = document.createElement('div');
128 div.className = 'log-entry level-' + (entry.level || 'info');
129
130 // Timestamp
131 const ts = document.createElement('span');
132 ts.className = 'timestamp';
133 try {
134 const d = new Date(entry.timestamp);
135 ts.textContent = d.toLocaleTimeString('en-US', { hour12: false }) + '.' +
136 String(d.getMilliseconds()).padStart(3, '0');
137 } catch (e) {
138 ts.textContent = '--:--:--';
139 }
140 div.appendChild(ts);
141
142 // Level tag
143 const tag = document.createElement('span');
144 tag.className = 'level-tag';
145 tag.textContent = (entry.level || 'info').toUpperCase();
146 div.appendChild(tag);
147
148 // Message
149 const msg = document.createElement('span');
150 msg.className = 'message';
151 msg.textContent = entry.text || '';
152 div.appendChild(msg);
153
154 // Click to copy
155 div.addEventListener('dblclick', () => {
156 const text = `${entry.timestamp} [${entry.level}] ${entry.text}`;
157 navigator.clipboard.writeText(text).catch(() => {});
158 });
159
160 return div;
161}
162
163function appendEntryDOM(entry) {
164 const container = document.getElementById('logContainer');
165
166 // Remove empty state if present
167 const emptyState = container.querySelector('.empty-state');
168 if (emptyState) emptyState.remove();
169
170 container.appendChild(createEntryElement(entry));
171}
172
173function updateCounts() {
174 document.getElementById('entryCount').textContent =
175 `${filteredEntries.length} / ${entries.length} entries`;
176}
177
178function scrollToBottom() {
179 const container = document.getElementById('logContainer');
180 requestAnimationFrame(() => {
181 container.scrollTop = container.scrollHeight;
182 });
183}
184
185// ─── Auto-scroll ─────────────────────────────────────────────────────────────
186
187// Detect if user scrolled up
188document.getElementById('logContainer').addEventListener('scroll', () => {
189 const container = document.getElementById('logContainer');
190 const atBottom = container.scrollHeight - container.scrollTop - container.clientHeight < 50;
191 autoScroll = atBottom;
192 document.getElementById('autoScrollLabel').style.opacity = autoScroll ? '1' : '0.5';
193});
194
195// ─── Status ──────────────────────────────────────────────────────────────────
196
197function updateStatus(status) {
198 if (!status) return;
199 const dot = document.getElementById('statusDot');
200 const label = document.getElementById('statusLabel');
201 dot.className = 'badge-dot ' + status.state;
202 label.textContent = `Server: ${status.state}`;
203 if (status.state === 'running') {
204 label.textContent = `Server: running on port ${status.port}`;
205 }
206}
207
208// ─── Actions ─────────────────────────────────────────────────────────────────
209
210async function clearLog() {
211 try {
212 await api.logClear();
213 // The 'log-cleared' event will handle the UI
214 } catch (err) {
215 console.error('Failed to clear log:', err);
216 }
217}
218
219// ─── Keyboard shortcuts ──────────────────────────────────────────────────────
220
221document.addEventListener('keydown', (e) => {
222 if (e.key === 'Escape') window.close();
223 if (e.key === 'l' && (e.ctrlKey || e.metaKey)) clearLog();
224 if (e.key === 'f' && (e.ctrlKey || e.metaKey)) {
225 e.preventDefault();
226 document.getElementById('searchInput').focus();
227 }
228});
229
230// ─── Init ────────────────────────────────────────────────────────────────────
231
232document.addEventListener('DOMContentLoaded', init);