-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_debug_js.js
More file actions
601 lines (564 loc) · 25.7 KB
/
Copy pathapp_debug_js.js
File metadata and controls
601 lines (564 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
const enrollBtn = document.getElementById('enrollBtn');
const clearBtn = document.getElementById('clearBtn');
const statusEl = document.getElementById('status');
const saveStatus = document.getElementById('saveStatus');
const transcriptEl = document.getElementById('transcript');
const preview = document.getElementById('preview');
const levelEl = document.getElementById('level');
const latest = document.getElementById('latest');
const graph = document.getElementById('graph');
const authStatus = document.getElementById('authStatus');
const authResult = document.getElementById('authResult');
const authScore = document.getElementById('authScore');
const authAccepted = document.getElementById('authAccepted');
const enrollCount = document.getElementById('enrollCount');
const captureCount = document.getElementById('captureCount');
const userId = document.getElementById('userId');
const sessionId = document.getElementById('sessionId');
const activityContext = document.getElementById('activityContext');
const fallback = document.getElementById('fallback');
const audioFile = document.getElementById('audioFile');
const adminKey = document.getElementById('adminKey');
const voiceprintBtn = document.getElementById('voiceprintBtn');
const clearKeyBtn = document.getElementById('clearKeyBtn');
const voiceprintStatus = document.getElementById('voiceprintStatus');
const agentModeBtn = document.getElementById('agentModeBtn');
const meetingModeBtn = document.getElementById('meetingModeBtn');
const agentMode = document.getElementById('agentMode');
const meetingMode = document.getElementById('meetingMode');
const meetingFile = document.getElementById('meetingFile');
const processMeetingBtn = document.getElementById('processMeetingBtn');
const meetingStatus = document.getElementById('meetingStatus');
const meetingProgress = document.getElementById('meetingProgress');
const meetingResults = document.getElementById('meetingResults');
const meetingMeta = document.getElementById('meetingMeta');
const meetingTranscript = document.getElementById('meetingTranscript');
const meetingSummary = document.getElementById('meetingSummary');
const summaryText = document.getElementById('summaryText');
const dispatchModeBtn = document.getElementById('dispatchModeBtn');
const dispatchMode = document.getElementById('dispatchMode');
const assistxPill = document.getElementById('assistxPill');
const dispatchUrl = document.getElementById('dispatchUrl');
const dispatchToken = document.getElementById('dispatchToken');
const dispatchEventType = document.getElementById('dispatchEventType');
const dispatchAuto = document.getElementById('dispatchAuto');
const dispatchText = document.getElementById('dispatchText');
const dispatchMeta = document.getElementById('dispatchMeta');
const dispatchSendBtn = document.getElementById('dispatchSendBtn');
const dispatchLog = document.getElementById('dispatchLog');
const dispatchAuthBtn = document.getElementById('dispatchAuthBtn');
const dispatchMeetingBtn = document.getElementById('dispatchMeetingBtn');
const downloadTranscriptBtn = document.getElementById('downloadTranscriptBtn');
const meetingHistoryList = document.getElementById('meetingHistoryList');
const meetingHistoryStatus = document.getElementById('meetingHistoryStatus');
const speakerList = document.getElementById('speakerList');
const refreshSpeakersBtn = document.getElementById('refreshSpeakersBtn');
const autoDispatchToggle = document.getElementById('autoDispatchToggle');
const autoDispatchPill = document.getElementById('autoDispatchPill');
const saveDispatchConfigBtn = document.getElementById('saveDispatchConfigBtn');
let lastScore = null, lastAccepted = null;
let recorder, stream, chunks = [], blob = null, selectedFile = null, startedAt = 0, recognition = null;
let audioCtx, analyser, raf, wavSource, wavProcessor, wavGain;
let wavBuffers = [], wavSampleRate = 0, wavBlob = null;
let cachedContext = null;
let lastAuthResult = null, lastMeetingResult = null;
function getDeviceId() {
const key = 'sophia_device_id';
let value = localStorage.getItem(key);
if (!value) {
value = (crypto.randomUUID && crypto.randomUUID()) || ('device-' + Date.now() + '-' + Math.random().toString(16).slice(2));
localStorage.setItem(key, value);
}
return value;
}
async function sha256(text) {
if (!crypto.subtle) return btoa(text).slice(0, 64);
const bytes = new TextEncoder().encode(text);
const hash = await crypto.subtle.digest('SHA-256', bytes);
return Array.from(new Uint8Array(hash)).map(b => b.toString(16).padStart(2, '0')).join('');
}
function getPosition() {
return new Promise(resolve => {
if (!navigator.geolocation) return resolve({});
navigator.geolocation.getCurrentPosition(
pos => resolve({
location_lat: pos.coords.latitude,
location_lng: pos.coords.longitude,
location_accuracy_m: pos.coords.accuracy
}),
() => resolve({}),
{ enableHighAccuracy: false, timeout: 3500, maximumAge: 300000 }
);
});
}
async function collectContext() {
const base = {
device_id: getDeviceId(),
user_agent: navigator.userAgent,
platform: navigator.platform || '',
language: navigator.language || '',
languages: navigator.languages || [],
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone || '',
timezone_offset_minutes: new Date().getTimezoneOffset(),
screen: { width: screen.width, height: screen.height, pixel_ratio: window.devicePixelRatio || 1 },
hardware_concurrency: navigator.hardwareConcurrency || null,
device_memory_gb: navigator.deviceMemory || null,
online: navigator.onLine,
visibility_state: document.visibilityState,
page_url: location.href,
referrer: document.referrer || '',
activity_context: activityContext.value || '',
captured_at: new Date().toISOString()
};
const fingerprintSource = JSON.stringify({
ua: base.user_agent,
platform: base.platform,
language: base.language,
timezone: base.timezone,
screen: base.screen,
cores: base.hardware_concurrency,
memory: base.device_memory_gb
});
base.device_fingerprint = await sha256(fingerprintSource);
Object.assign(base, await getPosition());
cachedContext = base;
return base;
}
async function refreshGraph() {
try {
const res = await fetch('/memory-graph/status');
const data = await res.json();
graph.textContent = data.has_password ? 'graph connected: ' + (data.database || 'default') : 'graph needs credentials';
} catch {
graph.textContent = 'graph unavailable';
}
}
async function refreshEventLog() {
try {
const res = await fetch('/events?limit=50');
const data = await res.json();
const evts = (data.events || []).slice(-50);
const el = document.getElementById('eventLog');
if (!evts.length) {
el.innerHTML = '<div style="color:#64748b;">No events yet.</div>';
return;
}
el.innerHTML = evts.map(e => {
const p = e.payload || {};
const text = p.text || p.transcript || '';
const score = p.score !== undefined ? ' score=' + p.score.toFixed(3) : '';
const accepted = p.accepted !== undefined ? (p.accepted ? '✓' : '✗') : '';
const info = text ? ' <span style="color:#94a3b8;">' + text.slice(0, 60) + '</span>' : '';
return '<div style="color:#7dd3fc;">' + e.type + '</div><div style="color:#64748b;margin-left:12px;">' + score + accepted + info + '</div>';
}).join('');
} catch {}
}
document.getElementById('refreshEventsBtn').onclick = refreshEventLog;
async function refreshStatus() {
try {
const r = await fetch('/status');
const d = await r.json();
const llm = d.llm || {};
const prov = llm.intent_provider || llm.provider || 'unknown';
const enabled = llm.intent_draft_enabled ? '✅' : '❌';
document.getElementById('llmStatus').innerHTML = 'LLM: <span style="color:#7dd3fc;">' + prov + '</span>'
+ ' <span style="color:#64748b;">model=' + (llm.intent_model || llm.model) + '</span>'
+ ' <span>' + enabled + '</span>';
} catch {}
}
document.getElementById('refreshStatusBtn').onclick = refreshStatus;
async function testLlm() {
const btn = document.getElementById('testLlmBtn');
const result = document.getElementById('llmTestResult');
btn.disabled = true;
result.textContent = 'testing...';
try {
const r = await fetch('/llm/test', { method: 'POST' });
const d = await r.json();
result.innerHTML = d.ok
? '<span style="color:#34d399;">✅ ' + d.provider + ' ok</span>'
: '<span style="color:#fb7185;">❌ ' + (d.error || 'failed') + '</span>';
} catch (err) {
result.innerHTML = '<span style="color:#fb7185;">❌ ' + err.message + '</span>';
} finally {
btn.disabled = false;
}
}
document.getElementById('testLlmBtn').onclick = testLlm;
function hasLiveMic() {
const isLocal = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1';
return Boolean((window.isSecureContext || isLocal) && navigator.mediaDevices && navigator.mediaDevices.getUserMedia && window.MediaRecorder);
}
function setAuthPill(state, text) {
authStatus.className = 'pill auth ' + state;
authStatus.textContent = text;
}
function showAuthResult(score, accepted, deviceLabel) {
authResult.classList.remove('hidden');
authScore.textContent = (score * 100).toFixed(0) + '%';
authAccepted.textContent = accepted ? 'ACCEPTED' : 'REJECTED';
authAccepted.className = accepted ? 'auth-accepted pass' : 'auth-accepted fail';
const devEl = document.getElementById('authDevice');
if (devEl) {
devEl.textContent = deviceLabel ? 'via ' + deviceLabel : '';
devEl.style.display = deviceLabel ? '' : 'none';
}
}
async function verifyVoice() {
const audioSrc = selectedFile || blob;
if (!audioSrc) { return; }
const form = new FormData();
form.append('audio', audioSrc, audioSrc.name || 'capture.webm');
form.append('user_id', userId.value || 'default');
saveStatus.textContent = 'Verifying voice...';
setAuthPill('enrolling', 'checking...');
try {
const res = await fetch('/auth/verify', { method: 'POST', body: form });
const data = await res.json();
if (!res.ok) throw new Error(data.detail || 'Verify failed');
lastScore = data.score;
lastAccepted = data.accepted;
lastAuthResult = { userId: userId.value, score: data.score, accepted: data.accepted, device_id: data.device_id };
const matchedDevice = data.device_id && data.device_id !== 'default' ? ' [' + data.device_id + ']' : '';
showAuthResult(data.score, data.accepted, matchedDevice);
const msg = data.accepted
? 'Voice verified ✓ score=' + data.score.toFixed(4) + matchedDevice
: 'Voice rejected ✗ score=' + data.score.toFixed(4) + ' — you can still Force Enroll if this is truly you';
saveStatus.textContent = msg;
enrollBtn.disabled = false;
if (data.accepted) {
enrollBtn.className = 'warning';
enrollBtn.textContent = '⬆ Enroll Voice';
} else {
enrollBtn.className = 'danger';
enrollBtn.textContent = '⚠ Force Enroll';
}
if (data.accepted && autoDispatchToggle.checked) {
saveStatus.textContent = msg + ' — dispatching to AssistX...';
const result = await autoDispatch('voice_auth',
'Voice auth: user=' + userId.value + ' score=' + (data.score * 100).toFixed(0) + '%',
{ score: data.score, accepted: data.accepted, userId: userId.value, device_id: data.device_id }
);
saveStatus.textContent = msg + (result.sent ? ' — dispatched ✓' : ' — dispatch failed');
}
} catch (err) {
setAuthPill('fail', 'error');
saveStatus.textContent = 'Verify error: ' + err.message;
}
}
async function enrollVoice() {
const audioSrc = selectedFile || blob;
if (!audioSrc) { return; }
const isForced = !lastAccepted;
if (isForced && !confirm('Voice verification failed. Are you sure you want to FORCE enroll this sample? Use this only for training data improvements.')) {
return;
}
const form = new FormData();
form.append('audio', audioSrc, audioSrc.name || 'capture.webm');
form.append('user_id', userId.value || 'default');
form.append('force', isForced ? 'true' : 'false');
const devId = document.getElementById('deviceId').value.trim();
if (devId) form.append('device_id', devId);
saveStatus.textContent = isForced ? 'Force enrolling voice sample...' : 'Enrolling voice sample...';
setAuthPill('enrolling', 'enrolling...');
try {
const res = await fetch('/voiceprints/enroll', { method: 'POST', body: form });
const data = await res.json();
if (!res.ok) throw new Error(data.detail || 'Enroll failed');
const label = data.device_id && data.device_id !== 'default' ? ' (' + data.device_id + ')' : '';
setAuthPill('pass', 'enrolled' + label);
enrollCount.textContent = data.sample_count + ' samples enrolled' + label;
saveStatus.textContent = (isForced ? 'Forced enrollment successful ✓ ' : 'Voice enrolled ✓ ') + '(' + data.sample_count + ' total samples)' + label;
loadSpeakers();
} catch (err) {
setAuthPill('fail', 'error');
saveStatus.textContent = 'Enroll error: ' + err.message;
}
}
function enableFallback(message) {
fallback.classList.add('active');
startBtn.disabled = true;
stopBtn.disabled = true;
if (message) statusEl.textContent = message;
}
function updateActionButtons() {
const hasAudio = Boolean(blob || selectedFile || wavBlob);
saveBtn.disabled = !hasAudio && !transcriptEl.value.trim();
voiceprintBtn.disabled = !hasAudio;
}
function makeRecognition() {
const Ctor = window.SpeechRecognition || window.webkitSpeechRecognition;
if (!Ctor) return null;
const rec = new Ctor();
rec.continuous = true;
rec.interimResults = true;
rec.lang = 'en-US';
rec.onresult = (event) => {
let finalText = '';
let interim = '';
for (let i = 0; i < event.results.length; i++) {
const text = event.results[i][0].transcript;
if (event.results[i].isFinal) finalText += text + ' ';
else interim += text;
}
transcriptEl.value = (finalText + interim).trim();
updateActionButtons();
};
rec.onerror = () => { statusEl.textContent = 'Browser transcription paused; audio is still recording.'; };
return rec;
}
function startMeter(inputStream) {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const source = audioCtx.createMediaStreamSource(inputStream);
analyser = audioCtx.createAnalyser();
analyser.fftSize = 256;
source.connect(analyser);
const data = new Uint8Array(analyser.frequencyBinCount);
const tick = () => {
analyser.getByteFrequencyData(data);
const avg = data.reduce((a, b) => a + b, 0) / data.length;
levelEl.style.width = Math.min(100, avg * 1.6) + '%';
raf = requestAnimationFrame(tick);
};
tick();
}
function startWavCapture(inputStream) {
if (!audioCtx) return;
wavBuffers = [];
wavBlob = null;
wavSampleRate = audioCtx.sampleRate;
wavSource = audioCtx.createMediaStreamSource(inputStream);
wavProcessor = audioCtx.createScriptProcessor(4096, 1, 1);
wavGain = audioCtx.createGain();
wavGain.gain.value = 0;
wavProcessor.onaudioprocess = (event) => {
const input = event.inputBuffer.getChannelData(0);
wavBuffers.push(new Float32Array(input));
};
wavSource.connect(wavProcessor);
wavProcessor.connect(wavGain);
wavGain.connect(audioCtx.destination);
}
function stopWavCapture() {
try { wavProcessor && wavProcessor.disconnect(); } catch {}
try { wavSource && wavSource.disconnect(); } catch {}
try { wavGain && wavGain.disconnect(); } catch {}
wavProcessor = null;
wavSource = null;
wavGain = null;
if (wavBuffers.length && wavSampleRate) wavBlob = encodeWav(wavBuffers, wavSampleRate);
}
function encodeWav(buffers, sampleRate) {
const length = buffers.reduce((total, buf) => total + buf.length, 0);
const samples = new Float32Array(length);
let offset = 0;
buffers.forEach(buf => { samples.set(buf, offset); offset += buf.length; });
const buffer = new ArrayBuffer(44 + samples.length * 2);
const view = new DataView(buffer);
function writeString(pos, value) {
for (let i = 0; i < value.length; i++) view.setUint8(pos + i, value.charCodeAt(i));
}
writeString(0, 'RIFF');
view.setUint32(4, 36 + samples.length * 2, true);
writeString(8, 'WAVE');
writeString(12, 'fmt ');
view.setUint32(16, 16, true);
view.setUint16(20, 1, true);
view.setUint16(22, 1, true);
view.setUint32(24, sampleRate, true);
view.setUint32(28, sampleRate * 2, true);
view.setUint16(32, 2, true);
view.setUint16(34, 16, true);
writeString(36, 'data');
view.setUint32(40, samples.length * 2, true);
let pos = 44;
for (let i = 0; i < samples.length; i++, pos += 2) {
const s = Math.max(-1, Math.min(1, samples[i]));
view.setInt16(pos, s < 0 ? s * 0x8000 : s * 0x7fff, true);
}
return new Blob([view], { type: 'audio/wav' });
}
async function start() {
chunks = [];
blob = null;
selectedFile = null;
wavBlob = null;
updateActionButtons();
saveStatus.textContent = '';
if (!hasLiveMic()) {
enableFallback('Live microphone recording needs HTTPS on iOS Safari. Use the audio file picker below to record or upload.');
return;
}
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
startMeter(stream);
startWavCapture(stream);
recorder = new MediaRecorder(stream);
recorder.ondataavailable = (ev) => { if (ev.data.size) chunks.push(ev.data); };
recorder.onstop = () => {
blob = new Blob(chunks, { type: recorder.mimeType || 'audio/webm' });
preview.src = URL.createObjectURL(blob);
preview.hidden = false;
updateActionButtons();
};
recognition = makeRecognition();
try { recognition && recognition.start(); } catch {}
recorder.start();
startedAt = Date.now();
startBtn.disabled = true;
stopBtn.disabled = false;
statusEl.textContent = 'Recording...';
}
function stop() {
if (recorder && recorder.state !== 'inactive') recorder.stop();
if (recognition) { try { recognition.stop(); } catch {} }
stopWavCapture();
if (stream) stream.getTracks().forEach(t => t.stop());
if (raf) cancelAnimationFrame(raf);
if (audioCtx) audioCtx.close();
levelEl.style.width = '0%';
startBtn.disabled = false;
stopBtn.disabled = true;
updateActionButtons();
statusEl.textContent = 'Recording stopped. Review transcript, then save or append to voiceprint.';
}
async function save() {
const form = new FormData();
if (selectedFile) form.append('audio', selectedFile, selectedFile.name || 'ios-capture');
else if (blob) form.append('audio', blob, 'capture.webm');
form.append('transcript', transcriptEl.value || '');
form.append('user_id', userId.value || 'default');
form.append('session_id', sessionId.value || 'mobile');
form.append('duration_ms', String(startedAt ? Date.now() - startedAt : 0));
const context = await collectContext();
form.append('device_id', context.device_id || '');
form.append('device_fingerprint', context.device_fingerprint || '');
form.append('client_context', JSON.stringify(context));
form.append('activity_context', activityContext.value || '');
if (context.location_lat != null) form.append('location_lat', String(context.location_lat));
if (context.location_lng != null) form.append('location_lng', String(context.location_lng));
if (context.location_accuracy_m != null) form.append('location_accuracy_m', String(context.location_accuracy_m));
saveStatus.textContent = 'Saving...';
const res = await fetch('/capture', { method: 'POST', body: form });
const data = await res.json();
latest.textContent = JSON.stringify(data, null, 2);
if (!res.ok) throw new Error(data.detail || 'Capture failed');
const count = parseInt(localStorage.getItem('sophia_capture_count') || '0') + 1;
localStorage.setItem('sophia_capture_count', String(count));
captureCount.textContent = count + ' capture' + (count !== 1 ? 's' : '') + ' saved';
saveStatus.textContent = data.graph_saved ? 'Saved to sidecar and Neo4j.' : 'Saved locally; graph not written.';
}
async function appendVoiceprint() {
const key = adminKey.value || '';
if (!key.trim()) throw new Error('Admin enrollment key is required.');
const form = new FormData();
if (selectedFile) form.append('audio', selectedFile, selectedFile.name || 'owner-admin-upload.wav');
else if (wavBlob) form.append('audio', wavBlob, 'owner-admin-recording.wav');
else if (blob) form.append('audio', blob, 'owner-admin-recording.webm');
else throw new Error('Record or upload an audio clip first.');
form.append('user_id', userId.value || 'scott');
form.append('session_id', sessionId.value || 'mobile');
form.append('admin_key', key);
form.append('transcript', transcriptEl.value || '');
form.append('activity_context', activityContext.value || '');
const context = cachedContext || await collectContext();
form.append('device_id', context.device_id || '');
form.append('device_fingerprint', context.device_fingerprint || '');
form.append('client_context', JSON.stringify(context));
voiceprintStatus.textContent = 'Appending voiceprint sample...';
const res = await fetch('/voiceprints/owner-override-enroll', { method: 'POST', body: form });
const data = await res.json();
latest.textContent = JSON.stringify(data, null, 2);
if (!res.ok) throw new Error(data.detail || 'Voiceprint append failed');
voiceprintStatus.textContent = 'Voiceprint updated. Run a held-out verification clip next.';
}
startBtn.onclick = () => start().catch(err => {
enableFallback('Microphone unavailable here: ' + (err && err.message ? err.message : 'browser blocked live capture'));
});
stopBtn.onclick = stop;
saveBtn.onclick = () => save().catch(err => { saveStatus.textContent = err.message; });
voiceprintBtn.onclick = () => appendVoiceprint().catch(err => { voiceprintStatus.textContent = err.message; });
verifyBtn.onclick = () => verifyVoice();
enrollBtn.onclick = () => enrollVoice();
transcriptEl.oninput = updateActionButtons;
audioFile.onchange = () => {
selectedFile = audioFile.files && audioFile.files[0] ? audioFile.files[0] : null;
blob = null;
wavBlob = null;
if (selectedFile) {
preview.src = URL.createObjectURL(selectedFile);
preview.hidden = false;
updateActionButtons();
verifyBtn.disabled = false;
enrollBtn.disabled = true;
authResult.classList.add('hidden');
startedAt = Date.now();
statusEl.textContent = 'Audio selected. Add or edit transcript, then save or append to voiceprint.';
}
};
clearKeyBtn.onclick = () => {
adminKey.value = '';
voiceprintStatus.textContent = '';
};
clearBtn.onclick = () => {
transcriptEl.value = '';
activityContext.value = '';
preview.hidden = true;
blob = null;
selectedFile = null;
wavBlob = null;
audioFile.value = '';
updateActionButtons();
verifyBtn.disabled = true;
enrollBtn.disabled = true;
authResult.classList.add('hidden');
latest.textContent = '{}';
};
function switchMode(mode) {
[agentMode, meetingMode, dispatchMode].forEach(el => el.style.display = 'none');
[agentModeBtn, meetingModeBtn, dispatchModeBtn].forEach(el => el.classList.remove('active'));
if (mode === 'meeting') {
meetingMode.style.display = '';
meetingModeBtn.classList.add('active');
loadMeetingHistory();
} else if (mode === 'dispatch') {
dispatchMode.style.display = '';
dispatchModeBtn.classList.add('active');
refreshDispatchStatus();
} else {
agentMode.style.display = '';
agentModeBtn.classList.add('active');
}
}
agentModeBtn.onclick = () => switchMode('agent');
meetingModeBtn.onclick = () => switchMode('meeting');
dispatchModeBtn.onclick = () => switchMode('dispatch');
meetingFile.onchange = () => {
processMeetingBtn.disabled = !(meetingFile.files && meetingFile.files[0]);
meetingStatus.textContent = meetingFile.files && meetingFile.files[0]
? 'Ready to process: ' + meetingFile.files[0].name
: 'Upload an audio file to process.';
meetingResults.style.display = 'none';
};
async function processMeeting() {
const file = meetingFile.files && meetingFile.files[0];
if (!file) return;
processMeetingBtn.disabled = true;
meetingProgress.style.width = '5%';
meetingStatus.textContent = 'Uploading...';
meetingResults.style.display = 'none';
const form = new FormData();
form.append('audio', file, file.name || 'meeting.webm');
form.append('summarize', 'true');
const maxSpk = document.getElementById('meetingMaxSpeakers').value;
if (maxSpk) form.append('max_speakers', maxSpk);
try {
const res = await fetch('/meeting/process', { method: 'POST', body: form });
const init = await res.json();
if (!res.ok) throw new Error(init.detail || 'Upload failed');
const taskId = init.task_id;
meetingStatus.textContent = 'Processing...';
let done = false;
while (!done) {
await new Promise(r => setTimeout(r, 1500));
const sr = await fetch('/meeting/status/' + taskId);
const t = await sr.json();