askrobots / dbbasic-web-native
Public
100+ copy-paste UI components in plain HTML and CSS: no dependencies, no build step.
danq9
Add canonical.html v1.0 - The single source of truth for usability standards
fb6e650
1 year ago
History
devtools.js · 20.5 KB · 588 lines
| 1 | /** |
| 2 | * Semantic Components Developer Tools |
| 3 | * Real-time debugging and inspection interface |
| 4 | * Shows context state, attention budget, intent logs, and performance metrics |
| 5 | */ |
| 6 | |
| 7 | class SemanticDevTools { |
| 8 | constructor() { |
| 9 | this.isVisible = false; |
| 10 | this.intentLog = []; |
| 11 | this.maxLogEntries = 50; |
| 12 | this.performanceMetrics = { |
| 13 | intentsFired: 0, |
| 14 | contextswitches: 0, |
| 15 | avgResponseTime: 0 |
| 16 | }; |
| 17 | |
| 18 | this.init(); |
| 19 | } |
| 20 | |
| 21 | init() { |
| 22 | this.createUI(); |
| 23 | this.setupEventListeners(); |
| 24 | this.startMonitoring(); |
| 25 | |
| 26 | // Keyboard shortcut: Ctrl+Shift+D |
| 27 | document.addEventListener('keydown', (e) => { |
| 28 | if (e.ctrlKey && e.shiftKey && e.key === 'D') { |
| 29 | e.preventDefault(); |
| 30 | this.toggle(); |
| 31 | } |
| 32 | }); |
| 33 | |
| 34 | console.log('DevTools initialized. Press Ctrl+Shift+D to toggle.'); |
| 35 | } |
| 36 | |
| 37 | createUI() { |
| 38 | const devtools = document.createElement('div'); |
| 39 | devtools.id = 'semantic-devtools'; |
| 40 | devtools.innerHTML = ` |
| 41 | <style> |
| 42 | #semantic-devtools { |
| 43 | position: fixed; |
| 44 | top: 0; |
| 45 | right: -450px; |
| 46 | width: 450px; |
| 47 | height: 100vh; |
| 48 | background: #1a1a1a; |
| 49 | border-left: 2px solid #0066cc; |
| 50 | box-shadow: -4px 0 20px rgba(0, 0, 0, 0.5); |
| 51 | z-index: 999999; |
| 52 | font-family: 'Monaco', 'Courier New', monospace; |
| 53 | font-size: 12px; |
| 54 | color: #fff; |
| 55 | transition: right 0.3s; |
| 56 | display: flex; |
| 57 | flex-direction: column; |
| 58 | overflow: hidden; |
| 59 | } |
| 60 | |
| 61 | #semantic-devtools.visible { |
| 62 | right: 0; |
| 63 | } |
| 64 | |
| 65 | .devtools-header { |
| 66 | padding: 16px; |
| 67 | background: #0066cc; |
| 68 | display: flex; |
| 69 | justify-content: space-between; |
| 70 | align-items: center; |
| 71 | flex-shrink: 0; |
| 72 | } |
| 73 | |
| 74 | .devtools-title { |
| 75 | font-weight: 700; |
| 76 | font-size: 14px; |
| 77 | } |
| 78 | |
| 79 | .devtools-close { |
| 80 | background: none; |
| 81 | border: none; |
| 82 | color: white; |
| 83 | font-size: 20px; |
| 84 | cursor: pointer; |
| 85 | padding: 0; |
| 86 | width: 24px; |
| 87 | height: 24px; |
| 88 | } |
| 89 | |
| 90 | .devtools-tabs { |
| 91 | display: flex; |
| 92 | background: #2a2a2a; |
| 93 | border-bottom: 1px solid #3a3a3a; |
| 94 | flex-shrink: 0; |
| 95 | } |
| 96 | |
| 97 | .devtools-tab { |
| 98 | padding: 12px 16px; |
| 99 | cursor: pointer; |
| 100 | border-bottom: 2px solid transparent; |
| 101 | transition: all 0.2s; |
| 102 | } |
| 103 | |
| 104 | .devtools-tab:hover { |
| 105 | background: #3a3a3a; |
| 106 | } |
| 107 | |
| 108 | .devtools-tab.active { |
| 109 | border-bottom-color: #0066cc; |
| 110 | background: #3a3a3a; |
| 111 | } |
| 112 | |
| 113 | .devtools-content { |
| 114 | flex: 1; |
| 115 | overflow-y: auto; |
| 116 | padding: 16px; |
| 117 | } |
| 118 | |
| 119 | .devtools-panel { |
| 120 | display: none; |
| 121 | } |
| 122 | |
| 123 | .devtools-panel.active { |
| 124 | display: block; |
| 125 | } |
| 126 | |
| 127 | .metric-group { |
| 128 | margin-bottom: 20px; |
| 129 | } |
| 130 | |
| 131 | .metric-label { |
| 132 | color: #98989d; |
| 133 | font-size: 11px; |
| 134 | text-transform: uppercase; |
| 135 | letter-spacing: 1px; |
| 136 | margin-bottom: 8px; |
| 137 | } |
| 138 | |
| 139 | .metric-value { |
| 140 | font-size: 24px; |
| 141 | font-weight: 700; |
| 142 | color: #409cff; |
| 143 | } |
| 144 | |
| 145 | .metric-bar { |
| 146 | height: 8px; |
| 147 | background: #2a2a2a; |
| 148 | border-radius: 4px; |
| 149 | overflow: hidden; |
| 150 | margin-top: 8px; |
| 151 | } |
| 152 | |
| 153 | .metric-bar-fill { |
| 154 | height: 100%; |
| 155 | background: linear-gradient(90deg, #0066cc, #00ff88); |
| 156 | transition: width 0.3s; |
| 157 | } |
| 158 | |
| 159 | .log-entry { |
| 160 | padding: 8px; |
| 161 | margin-bottom: 4px; |
| 162 | background: #2a2a2a; |
| 163 | border-left: 3px solid; |
| 164 | border-radius: 4px; |
| 165 | font-size: 11px; |
| 166 | line-height: 1.4; |
| 167 | } |
| 168 | |
| 169 | .log-entry.intent { |
| 170 | border-color: #0066cc; |
| 171 | } |
| 172 | |
| 173 | .log-entry.context { |
| 174 | border-color: #ff9500; |
| 175 | } |
| 176 | |
| 177 | .log-entry.voice { |
| 178 | border-color: #ff3b30; |
| 179 | } |
| 180 | |
| 181 | .log-entry.error { |
| 182 | border-color: #ff3b30; |
| 183 | background: rgba(255, 59, 48, 0.1); |
| 184 | } |
| 185 | |
| 186 | .log-time { |
| 187 | color: #98989d; |
| 188 | margin-right: 8px; |
| 189 | } |
| 190 | |
| 191 | .log-type { |
| 192 | display: inline-block; |
| 193 | padding: 2px 6px; |
| 194 | border-radius: 3px; |
| 195 | background: #0066cc; |
| 196 | margin-right: 8px; |
| 197 | font-size: 10px; |
| 198 | } |
| 199 | |
| 200 | .component-list { |
| 201 | list-style: none; |
| 202 | } |
| 203 | |
| 204 | .component-item { |
| 205 | padding: 8px; |
| 206 | margin-bottom: 4px; |
| 207 | background: #2a2a2a; |
| 208 | border-radius: 4px; |
| 209 | } |
| 210 | |
| 211 | .component-name { |
| 212 | font-weight: 600; |
| 213 | color: #409cff; |
| 214 | } |
| 215 | |
| 216 | .component-state { |
| 217 | font-size: 11px; |
| 218 | color: #98989d; |
| 219 | margin-top: 4px; |
| 220 | } |
| 221 | |
| 222 | .context-grid { |
| 223 | display: grid; |
| 224 | grid-template-columns: 1fr 1fr; |
| 225 | gap: 12px; |
| 226 | } |
| 227 | |
| 228 | .context-item { |
| 229 | padding: 12px; |
| 230 | background: #2a2a2a; |
| 231 | border-radius: 4px; |
| 232 | } |
| 233 | |
| 234 | .context-item-label { |
| 235 | font-size: 10px; |
| 236 | color: #98989d; |
| 237 | text-transform: uppercase; |
| 238 | margin-bottom: 4px; |
| 239 | } |
| 240 | |
| 241 | .context-item-value { |
| 242 | font-size: 16px; |
| 243 | font-weight: 600; |
| 244 | } |
| 245 | |
| 246 | .status-indicator { |
| 247 | display: inline-block; |
| 248 | width: 8px; |
| 249 | height: 8px; |
| 250 | border-radius: 50%; |
| 251 | margin-right: 6px; |
| 252 | } |
| 253 | |
| 254 | .status-active { background: #34c759; } |
| 255 | .status-idle { background: #ff9500; } |
| 256 | .status-error { background: #ff3b30; } |
| 257 | |
| 258 | .toggle-button { |
| 259 | position: fixed; |
| 260 | top: 50%; |
| 261 | right: 0; |
| 262 | transform: translateY(-50%); |
| 263 | background: #0066cc; |
| 264 | border: none; |
| 265 | border-radius: 8px 0 0 8px; |
| 266 | padding: 12px 8px; |
| 267 | color: white; |
| 268 | cursor: pointer; |
| 269 | font-size: 16px; |
| 270 | z-index: 999998; |
| 271 | writing-mode: vertical-rl; |
| 272 | text-orientation: mixed; |
| 273 | font-weight: 700; |
| 274 | letter-spacing: 1px; |
| 275 | } |
| 276 | |
| 277 | .toggle-button:hover { |
| 278 | background: #0056b3; |
| 279 | } |
| 280 | </style> |
| 281 | |
| 282 | <div class="devtools-header"> |
| 283 | <div class="devtools-title">⚡ Semantic DevTools</div> |
| 284 | <button class="devtools-close" onclick="window.semanticDevTools.toggle()">×</button> |
| 285 | </div> |
| 286 | |
| 287 | <div class="devtools-tabs"> |
| 288 | <div class="devtools-tab active" data-tab="overview">Overview</div> |
| 289 | <div class="devtools-tab" data-tab="context">Context</div> |
| 290 | <div class="devtools-tab" data-tab="components">Components</div> |
| 291 | <div class="devtools-tab" data-tab="log">Log</div> |
| 292 | </div> |
| 293 | |
| 294 | <div class="devtools-content"> |
| 295 | <!-- Overview Panel --> |
| 296 | <div class="devtools-panel active" id="panel-overview"> |
| 297 | <div class="metric-group"> |
| 298 | <div class="metric-label">Attention Budget</div> |
| 299 | <div class="metric-value" id="metric-screen">0%</div> |
| 300 | <div class="metric-bar"> |
| 301 | <div class="metric-bar-fill" id="bar-screen" style="width: 0%"></div> |
| 302 | </div> |
| 303 | </div> |
| 304 | |
| 305 | <div class="metric-group"> |
| 306 | <div class="metric-label">Audio Time</div> |
| 307 | <div class="metric-value" id="metric-audio">0s</div> |
| 308 | <div class="metric-bar"> |
| 309 | <div class="metric-bar-fill" id="bar-audio" style="width: 0%"></div> |
| 310 | </div> |
| 311 | </div> |
| 312 | |
| 313 | <div class="metric-group"> |
| 314 | <div class="metric-label">Cognitive Load</div> |
| 315 | <div class="metric-value" id="metric-cognitive">0/3</div> |
| 316 | <div class="metric-bar"> |
| 317 | <div class="metric-bar-fill" id="bar-cognitive" style="width: 0%"></div> |
| 318 | </div> |
| 319 | </div> |
| 320 | |
| 321 | <div class="metric-group"> |
| 322 | <div class="metric-label">Performance</div> |
| 323 | <div style="font-size: 14px; color: #fff; margin-top: 8px;"> |
| 324 | <div>Intents Fired: <span id="perf-intents">0</span></div> |
| 325 | <div>Context Switches: <span id="perf-switches">0</span></div> |
| 326 | <div>Components: <span id="perf-components">0</span></div> |
| 327 | </div> |
| 328 | </div> |
| 329 | </div> |
| 330 | |
| 331 | <!-- Context Panel --> |
| 332 | <div class="devtools-panel" id="panel-context"> |
| 333 | <div class="metric-group"> |
| 334 | <div class="metric-label">Current Modality</div> |
| 335 | <div class="metric-value" id="context-modality">screen</div> |
| 336 | </div> |
| 337 | |
| 338 | <div class="context-grid"> |
| 339 | <div class="context-item"> |
| 340 | <div class="context-item-label">Noise Level</div> |
| 341 | <div class="context-item-value" id="context-noise">0%</div> |
| 342 | </div> |
| 343 | <div class="context-item"> |
| 344 | <div class="context-item-label">Viewers</div> |
| 345 | <div class="context-item-value" id="context-viewers">1</div> |
| 346 | </div> |
| 347 | <div class="context-item"> |
| 348 | <div class="context-item-label">Lighting</div> |
| 349 | <div class="context-item-value" id="context-lighting">normal</div> |
| 350 | </div> |
| 351 | <div class="context-item"> |
| 352 | <div class="context-item-label">User State</div> |
| 353 | <div class="context-item-value" id="context-state">active</div> |
| 354 | </div> |
| 355 | </div> |
| 356 | |
| 357 | <div class="metric-group" style="margin-top: 20px;"> |
| 358 | <div class="metric-label">Voice Recognition</div> |
| 359 | <div style="font-size: 14px; margin-top: 8px;"> |
| 360 | <span class="status-indicator" id="voice-status"></span> |
| 361 | <span id="voice-text">Inactive</span> |
| 362 | </div> |
| 363 | </div> |
| 364 | |
| 365 | <div class="metric-group"> |
| 366 | <div class="metric-label">Context Detection</div> |
| 367 | <div style="font-size: 14px; margin-top: 8px;"> |
| 368 | <span class="status-indicator" id="detection-status"></span> |
| 369 | <span id="detection-text">Inactive</span> |
| 370 | </div> |
| 371 | </div> |
| 372 | </div> |
| 373 | |
| 374 | <!-- Components Panel --> |
| 375 | <div class="devtools-panel" id="panel-components"> |
| 376 | <ul class="component-list" id="component-list"> |
| 377 | <!-- Populated dynamically --> |
| 378 | </ul> |
| 379 | </div> |
| 380 | |
| 381 | <!-- Log Panel --> |
| 382 | <div class="devtools-panel" id="panel-log"> |
| 383 | <div id="log-container"> |
| 384 | <!-- Populated dynamically --> |
| 385 | </div> |
| 386 | </div> |
| 387 | </div> |
| 388 | |
| 389 | <button class="toggle-button" onclick="window.semanticDevTools.toggle()">DEVTOOLS</button> |
| 390 | `; |
| 391 | |
| 392 | document.body.appendChild(devtools); |
| 393 | this.panel = devtools; |
| 394 | } |
| 395 | |
| 396 | setupEventListeners() { |
| 397 | // Tab switching |
| 398 | document.querySelectorAll('.devtools-tab').forEach(tab => { |
| 399 | tab.addEventListener('click', () => { |
| 400 | const tabName = tab.dataset.tab; |
| 401 | this.switchTab(tabName); |
| 402 | }); |
| 403 | }); |
| 404 | |
| 405 | // Listen to all intents |
| 406 | document.addEventListener('intent', (e) => { |
| 407 | this.logIntent(e.detail); |
| 408 | this.performanceMetrics.intentsFired++; |
| 409 | }); |
| 410 | |
| 411 | // Listen to context changes |
| 412 | if (window.semanticContext) { |
| 413 | window.semanticContext.onChange((context) => { |
| 414 | this.logContext(context); |
| 415 | this.performanceMetrics.contextswitches++; |
| 416 | this.updateContextDisplay(); |
| 417 | }); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | switchTab(tabName) { |
| 422 | document.querySelectorAll('.devtools-tab').forEach(t => t.classList.remove('active')); |
| 423 | document.querySelectorAll('.devtools-panel').forEach(p => p.classList.remove('active')); |
| 424 | |
| 425 | document.querySelector(`[data-tab="${tabName}"]`).classList.add('active'); |
| 426 | document.getElementById(`panel-${tabName}`).classList.add('active'); |
| 427 | } |
| 428 | |
| 429 | startMonitoring() { |
| 430 | setInterval(() => { |
| 431 | this.updateMetrics(); |
| 432 | this.updateComponents(); |
| 433 | }, 1000); |
| 434 | } |
| 435 | |
| 436 | updateMetrics() { |
| 437 | if (!window.semanticContext) return; |
| 438 | |
| 439 | const context = window.semanticContext.context; |
| 440 | const attention = context.attention; |
| 441 | |
| 442 | // Screen space |
| 443 | const screenPercent = (attention.usedScreenSpace / attention.maxScreenSpace) * 100; |
| 444 | document.getElementById('metric-screen').textContent = Math.round(attention.usedScreenSpace) + '%'; |
| 445 | document.getElementById('bar-screen').style.width = screenPercent + '%'; |
| 446 | |
| 447 | // Audio time |
| 448 | const audioPercent = (attention.usedAudioTime / attention.maxAudioTime) * 100; |
| 449 | document.getElementById('metric-audio').textContent = attention.usedAudioTime.toFixed(1) + 's'; |
| 450 | document.getElementById('bar-audio').style.width = audioPercent + '%'; |
| 451 | |
| 452 | // Cognitive load |
| 453 | const cognitivePercent = (attention.usedCognitiveLoad / attention.maxCognitiveLoad) * 100; |
| 454 | document.getElementById('metric-cognitive').textContent = attention.usedCognitiveLoad.toFixed(1) + '/3'; |
| 455 | document.getElementById('bar-cognitive').style.width = cognitivePercent + '%'; |
| 456 | |
| 457 | // Performance |
| 458 | document.getElementById('perf-intents').textContent = this.performanceMetrics.intentsFired; |
| 459 | document.getElementById('perf-switches').textContent = this.performanceMetrics.contextswitches; |
| 460 | document.getElementById('perf-components').textContent = window.semanticContext.components.size; |
| 461 | } |
| 462 | |
| 463 | updateContextDisplay() { |
| 464 | if (!window.semanticContext) return; |
| 465 | |
| 466 | const context = window.semanticContext.context; |
| 467 | |
| 468 | document.getElementById('context-modality').textContent = context.modality; |
| 469 | document.getElementById('context-noise').textContent = context.environment.noiseLevel + '%'; |
| 470 | document.getElementById('context-viewers').textContent = context.environment.viewerCount; |
| 471 | document.getElementById('context-lighting').textContent = context.environment.lighting; |
| 472 | |
| 473 | const userState = context.user.isBusy ? 'Busy' : context.user.inFocusMode ? 'Focus' : 'Active'; |
| 474 | document.getElementById('context-state').textContent = userState; |
| 475 | |
| 476 | // Voice status |
| 477 | if (window.voiceSystem) { |
| 478 | const voiceActive = window.voiceSystem.isListening; |
| 479 | document.getElementById('voice-status').className = 'status-indicator ' + (voiceActive ? 'status-active' : 'status-idle'); |
| 480 | document.getElementById('voice-text').textContent = voiceActive ? 'Active' : 'Inactive'; |
| 481 | } |
| 482 | |
| 483 | // Detection status |
| 484 | if (window.contextDetection) { |
| 485 | const detectionActive = window.contextDetection.isRunning; |
| 486 | document.getElementById('detection-status').className = 'status-indicator ' + (detectionActive ? 'status-active' : 'status-idle'); |
| 487 | document.getElementById('detection-text').textContent = detectionActive ? 'Running' : 'Stopped'; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | updateComponents() { |
| 492 | if (!window.semanticContext) return; |
| 493 | |
| 494 | const list = document.getElementById('component-list'); |
| 495 | list.innerHTML = ''; |
| 496 | |
| 497 | Array.from(window.semanticContext.components).forEach(component => { |
| 498 | const tagName = component.tagName.toLowerCase(); |
| 499 | const attentionState = component.getAttribute('attention-state') || 'unknown'; |
| 500 | const id = component.id || 'unnamed'; |
| 501 | |
| 502 | const li = document.createElement('li'); |
| 503 | li.className = 'component-item'; |
| 504 | li.innerHTML = ` |
| 505 | <div class="component-name"><${tagName}> ${id}</div> |
| 506 | <div class="component-state"> |
| 507 | Attention: ${attentionState} |
| 508 | ${component.computeAttentionScore ? ` | Score: ${component.computeAttentionScore(window.semanticContext.context)}` : ''} |
| 509 | </div> |
| 510 | `; |
| 511 | |
| 512 | list.appendChild(li); |
| 513 | }); |
| 514 | } |
| 515 | |
| 516 | logIntent(detail) { |
| 517 | this.addLogEntry('intent', `${detail.type}`, detail); |
| 518 | } |
| 519 | |
| 520 | logContext(context) { |
| 521 | this.addLogEntry('context', 'Context updated', context); |
| 522 | } |
| 523 | |
| 524 | addLogEntry(type, message, data = null) { |
| 525 | const entry = { |
| 526 | type, |
| 527 | message, |
| 528 | data, |
| 529 | timestamp: new Date().toLocaleTimeString() |
| 530 | }; |
| 531 | |
| 532 | this.intentLog.unshift(entry); |
| 533 | |
| 534 | if (this.intentLog.length > this.maxLogEntries) { |
| 535 | this.intentLog.pop(); |
| 536 | } |
| 537 | |
| 538 | this.updateLog(); |
| 539 | } |
| 540 | |
| 541 | updateLog() { |
| 542 | const container = document.getElementById('log-container'); |
| 543 | container.innerHTML = this.intentLog.map(entry => ` |
| 544 | <div class="log-entry ${entry.type}"> |
| 545 | <span class="log-time">${entry.timestamp}</span> |
| 546 | <span class="log-type">${entry.type.toUpperCase()}</span> |
| 547 | ${entry.message} |
| 548 | ${entry.data ? `<pre style="margin-top: 4px; opacity: 0.7; font-size: 10px;">${JSON.stringify(entry.data, null, 2).substring(0, 200)}...</pre>` : ''} |
| 549 | </div> |
| 550 | `).join(''); |
| 551 | } |
| 552 | |
| 553 | toggle() { |
| 554 | this.isVisible = !this.isVisible; |
| 555 | this.panel.classList.toggle('visible', this.isVisible); |
| 556 | |
| 557 | if (this.isVisible) { |
| 558 | this.updateMetrics(); |
| 559 | this.updateContextDisplay(); |
| 560 | this.updateComponents(); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | show() { |
| 565 | this.isVisible = true; |
| 566 | this.panel.classList.add('visible'); |
| 567 | this.updateMetrics(); |
| 568 | this.updateContextDisplay(); |
| 569 | this.updateComponents(); |
| 570 | } |
| 571 | |
| 572 | hide() { |
| 573 | this.isVisible = false; |
| 574 | this.panel.classList.remove('visible'); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | // Initialize devtools |
| 579 | if (typeof window !== 'undefined') { |
| 580 | window.addEventListener('DOMContentLoaded', () => { |
| 581 | window.semanticDevTools = new SemanticDevTools(); |
| 582 | }); |
| 583 | } |
| 584 | |
| 585 | // Export |
| 586 | if (typeof module !== 'undefined' && module.exports) { |
| 587 | module.exports = SemanticDevTools; |
| 588 | } |