MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / desktop / renderer / settings.js
| 1 | /** |
| 2 | * MojoLlama Studio — Settings Renderer |
| 3 | * |
| 4 | * Manages the settings window UI, loads/saves settings via IPC, |
| 5 | * and provides inline server control. |
| 6 | */ |
| 7 | |
| 8 | const api = window.mojollama; |
| 9 | let currentSettings = {}; |
| 10 | let statusUnsubscribe = null; |
| 11 | |
| 12 | // ─── Initialization ────────────────────────────────────────────────────────── |
| 13 | |
| 14 | async function init() { |
| 15 | // Load settings |
| 16 | try { |
| 17 | currentSettings = await api.settingsLoad(); |
| 18 | populateForm(currentSettings); |
| 19 | } catch (err) { |
| 20 | console.error('Failed to load settings:', err); |
| 21 | } |
| 22 | |
| 23 | // Listen for server status updates |
| 24 | if (api.onServerStatus) { |
| 25 | statusUnsubscribe = api.onServerStatus(updateStatusUI); |
| 26 | } |
| 27 | |
| 28 | // Fetch current status |
| 29 | try { |
| 30 | const status = await api.serverStatus(); |
| 31 | updateStatusUI(status); |
| 32 | } catch (err) { |
| 33 | console.error('Failed to get server status:', err); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | function populateForm(settings) { |
| 38 | document.getElementById('serverPort').value = settings.serverPort || 8080; |
| 39 | document.getElementById('llamaPort').value = settings.llamaPort || 8081; |
| 40 | document.getElementById('modelPath').value = settings.modelPath || ''; |
| 41 | document.getElementById('threads').value = settings.threads || 32; |
| 42 | document.getElementById('engine').value = settings.engine || 'server_batch_moe'; |
| 43 | document.getElementById('autoStart').checked = settings.autoStart !== false; |
| 44 | document.getElementById('minimizeToTray').checked = settings.minimizeToTray !== false; |
| 45 | document.getElementById('notifications').checked = settings.notifications !== false; |
| 46 | document.getElementById('debug').checked = settings.debug === true; |
| 47 | } |
| 48 | |
| 49 | function readForm() { |
| 50 | return { |
| 51 | serverPort: parseInt(document.getElementById('serverPort').value, 10) || 8080, |
| 52 | llamaPort: parseInt(document.getElementById('llamaPort').value, 10) || 8081, |
| 53 | modelPath: document.getElementById('modelPath').value.trim(), |
| 54 | threads: parseInt(document.getElementById('threads').value, 10) || 32, |
| 55 | engine: document.getElementById('engine').value, |
| 56 | autoStart: document.getElementById('autoStart').checked, |
| 57 | minimizeToTray: document.getElementById('minimizeToTray').checked, |
| 58 | notifications: document.getElementById('notifications').checked, |
| 59 | debug: document.getElementById('debug').checked, |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | // ─── Server Control ────────────────────────────────────────────────────────── |
| 64 | |
| 65 | async function startServer() { |
| 66 | const form = readForm(); |
| 67 | document.getElementById('btnStart').disabled = true; |
| 68 | document.getElementById('btnStart').textContent = 'Starting...'; |
| 69 | try { |
| 70 | await api.serverStart({ |
| 71 | port: form.serverPort, |
| 72 | modelPath: form.modelPath || undefined, |
| 73 | }); |
| 74 | } catch (err) { |
| 75 | console.error('Failed to start server:', err); |
| 76 | document.getElementById('btnStart').disabled = false; |
| 77 | document.getElementById('btnStart').textContent = 'Start'; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | async function stopServer() { |
| 82 | document.getElementById('btnStop').disabled = true; |
| 83 | document.getElementById('btnStop').textContent = 'Stopping...'; |
| 84 | try { |
| 85 | await api.serverStop(); |
| 86 | } catch (err) { |
| 87 | console.error('Failed to stop server:', err); |
| 88 | document.getElementById('btnStop').disabled = false; |
| 89 | document.getElementById('btnStop').textContent = 'Stop'; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | async function restartServer() { |
| 94 | try { |
| 95 | await api.serverRestart(); |
| 96 | } catch (err) { |
| 97 | console.error('Failed to restart server:', err); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // ─── Status UI ─────────────────────────────────────────────────────────────── |
| 102 | |
| 103 | function updateStatusUI(status) { |
| 104 | if (!status) return; |
| 105 | |
| 106 | const dot = document.getElementById('statusDot'); |
| 107 | const text = document.getElementById('statusText'); |
| 108 | const btnStart = document.getElementById('btnStart'); |
| 109 | const btnStop = document.getElementById('btnStop'); |
| 110 | const btnRestart = document.getElementById('btnRestart'); |
| 111 | |
| 112 | dot.className = 'status-dot ' + status.state; |
| 113 | |
| 114 | switch (status.state) { |
| 115 | case 'running': |
| 116 | text.textContent = `Running on port ${status.port} (PID: ${status.pid || 'N/A'})`; |
| 117 | btnStart.disabled = true; |
| 118 | btnStart.textContent = 'Start'; |
| 119 | btnStop.disabled = false; |
| 120 | btnStop.textContent = 'Stop'; |
| 121 | btnRestart.disabled = false; |
| 122 | break; |
| 123 | case 'starting': |
| 124 | text.textContent = 'Server is starting...'; |
| 125 | btnStart.disabled = true; |
| 126 | btnStart.textContent = 'Starting...'; |
| 127 | btnStop.disabled = true; |
| 128 | btnRestart.disabled = true; |
| 129 | break; |
| 130 | case 'stopping': |
| 131 | text.textContent = 'Server is stopping...'; |
| 132 | btnStart.disabled = true; |
| 133 | btnStop.disabled = true; |
| 134 | btnStop.textContent = 'Stopping...'; |
| 135 | btnRestart.disabled = true; |
| 136 | break; |
| 137 | case 'error': |
| 138 | text.textContent = 'Server encountered an error. Check the log.'; |
| 139 | btnStart.disabled = false; |
| 140 | btnStart.textContent = 'Start'; |
| 141 | btnStop.disabled = true; |
| 142 | btnStop.textContent = 'Stop'; |
| 143 | btnRestart.disabled = false; |
| 144 | break; |
| 145 | default: // stopped |
| 146 | text.textContent = 'Server is stopped.'; |
| 147 | btnStart.disabled = false; |
| 148 | btnStart.textContent = 'Start'; |
| 149 | btnStop.disabled = true; |
| 150 | btnStop.textContent = 'Stop'; |
| 151 | btnRestart.disabled = true; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // ─── Actions ───────────────────────────────────────────────────────────────── |
| 157 | |
| 158 | async function saveSettings() { |
| 159 | const newSettings = readForm(); |
| 160 | try { |
| 161 | const result = await api.settingsSave(newSettings); |
| 162 | if (result.success) { |
| 163 | currentSettings = newSettings; |
| 164 | showToast('Settings saved successfully.', 'success'); |
| 165 | } else { |
| 166 | showToast('Failed to save settings.', 'error'); |
| 167 | } |
| 168 | } catch (err) { |
| 169 | console.error('Failed to save settings:', err); |
| 170 | showToast('Error saving settings: ' + err.message, 'error'); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | function cancel() { |
| 175 | // Electron modal will close automatically or we can close via IPC |
| 176 | // Since we're modal, the window closes on ESC or Cmd+W normally |
| 177 | window.close(); |
| 178 | } |
| 179 | |
| 180 | function openLog() { |
| 181 | if (api.openLogWindow) { |
| 182 | api.openLogWindow(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // ─── Toast ─────────────────────────────────────────────────────────────────── |
| 187 | |
| 188 | function showToast(msg, type) { |
| 189 | const existing = document.querySelector('.toast'); |
| 190 | if (existing) existing.remove(); |
| 191 | |
| 192 | const el = document.createElement('div'); |
| 193 | el.className = 'toast ' + (type || 'info'); |
| 194 | el.textContent = msg; |
| 195 | el.style.cssText = ` |
| 196 | position: fixed; bottom: 16px; right: 16px; padding: 10px 16px; |
| 197 | border-radius: 6px; font-size: 12px; font-weight: 500; z-index: 1000; |
| 198 | background: ${type === 'success' ? '#1f3f2a' : type === 'error' ? '#3f1f1f' : '#1f2a3f'}; |
| 199 | border: 1px solid ${type === 'success' ? 'var(--green)' : type === 'error' ? 'var(--red)' : 'var(--accent)'}; |
| 200 | color: ${type === 'success' ? 'var(--green)' : type === 'error' ? 'var(--red)' : 'var(--accent)'}; |
| 201 | animation: slideUp .3s ease; |
| 202 | `; |
| 203 | document.body.appendChild(el); |
| 204 | setTimeout(() => el.remove(), 3000); |
| 205 | } |
| 206 | |
| 207 | // ─── Keyboard shortcuts ────────────────────────────────────────────────────── |
| 208 | |
| 209 | document.addEventListener('keydown', (e) => { |
| 210 | if (e.key === 'Escape') { |
| 211 | window.close(); |
| 212 | } |
| 213 | if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) { |
| 214 | saveSettings(); |
| 215 | } |
| 216 | }); |
| 217 | |
| 218 | // ─── Init ──────────────────────────────────────────────────────────────────── |
| 219 | |
| 220 | document.addEventListener('DOMContentLoaded', init); |