100+ copy-paste UI components in plain HTML and CSS: no dependencies, no build step.

0 downloads

dbbasic-web-native/dist/devtools.js
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-web-native.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-web-native.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-web-native.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

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