MacBook pro commited on
Commit
243a057
·
1 Parent(s): fa48be9

chore(static): remove legacy webrtc_prod.js and document static asset inventory

Browse files
Files changed (2) hide show
  1. STATIC_ASSETS.md +28 -0
  2. static/webrtc_prod.js +0 -666
STATIC_ASSETS.md ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Static Asset Inventory
2
+
3
+ This document enumerates all files under `static/` after removal of the legacy `webrtc_prod.js` and clarifies their current status.
4
+
5
+ | File | Purpose | Loaded By | Active Use | Notes |
6
+ |------|---------|-----------|------------|-------|
7
+ | `index.html` | Main application UI (enterprise-grade layout) | FastAPI root (`/`) serves file contents | Yes | References only `webrtc_enterprise.js` |
8
+ | `webrtc_enterprise.js` | Primary WebRTC + UI control client (camera, datachannel, metrics, TURN retry) | `<script src="/static/webrtc_enterprise.js">` in `index.html` | Yes | Canonical implementation; supersedes older scripts |
9
+ | `worklet.js` | AudioWorklet processors (`pcm-chunker`, `pcm-player`) for deprecated WebSocket pipeline | Historically loaded by `app.js` | Legacy / Potentially removable | Not referenced by `index.html`; keep temporarily in case of fallback testing |
10
+ | `app.js` | Deprecated WebSocket streaming client (pre-WebRTC) | Not referenced | Legacy / Safe to remove later | Contains only legacy code; flagged as deprecated in header comment |
11
+ | `webrtc_client.js` | Placeholder legacy bootstrap (empty IIFE) | Not referenced | Legacy / Remove | Safe to delete in a future cleanup PR |
12
+
13
+ ## Summary
14
+
15
+ Only two files are required for the production UI: `index.html` and `webrtc_enterprise.js`.
16
+
17
+ Recommended cleanup (next steps):
18
+ 1. Remove `app.js` and `webrtc_client.js` after confirming no external bookmarks / embeddings rely on them.
19
+ 2. If no audio worklet fallback path is desired, remove `worklet.js` and excise related dead code from `app.js`.
20
+ 3. Add a minimal regression test ensuring `/` returns HTML containing `webrtc_enterprise.js` to prevent accidental script reference regressions.
21
+
22
+ ## Rationale for Keeping Some Legacy Files (Temporarily)
23
+
24
+ `worklet.js` is retained for a short deprecation window in case future experimentation with pure WebSocket audio is required. Once WebRTC audio processing is fully stable and no fallbacks are needed, it can be removed.
25
+
26
+ ## Enforcement Suggestion
27
+
28
+ Introduce a CI lint step that fails if new unreferenced large JS bundles appear in `static/` without being documented here. This keeps the static surface lean and auditable.
static/webrtc_prod.js DELETED
@@ -1,666 +0,0 @@
1
- /* Enterprise-grade WebRTC client with premium UI integration */
2
- (function(){
3
- const state = {
4
- pc: null,
5
- control: null,
6
- localStream: null,
7
- metricsTimer: null,
8
- referenceImage: null,
9
- connected: false,
10
- authToken: null,
11
- connecting: false,
12
- cancelled: false,
13
- initialized: false
14
- };
15
-
16
- const params = new URLSearchParams(location.search);
17
- const FORCE_RELAY_URL = params.get('relay') === '1';
18
-
19
- const els = {
20
- // File upload
21
- ref: document.getElementById('referenceInput'),
22
- uploadButton: document.getElementById('uploadButton'),
23
- uploadText: document.getElementById('u // Auto-initialize on page load (idempotent)
24
- (async ()=>{
25
- try {
26
- setSystemStatus('connecting', 'Auto-initializing pipeline...');
27
- const r = await fetch('/initialize', {method:'POST'});
28
- const j = await r.json().catch(()=>({}));
29
- if (r.ok && j && (j.status==='success' || j.status==='already_initialized')){
30
- state.initialized = true;
31
- setSystemStatus('connected', j.message || 'System ready');
32
- if (els.init) {
33
- els.init.classList.remove('btn-secondary');
34
- els.init.classList.add('btn-success');
35
- els.init.innerHTML = `
36
- <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
37
- <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/>
38
- <polyline points="22,4 12,14.01 9,11.01"/>
39
- </svg>
40
- Pipeline Ready
41
- `;
42
- }
43
- } else {
44
- console.warn('auto-initialize response', r.status, j);
45
- setSystemStatus('idle', 'Click Initialize to start');
46
- }
47
- } catch(e){
48
- setSystemStatus('idle', 'Click Initialize to start');
49
- }
50
- })(); // Control buttons
51
- init: document.getElementById('initBtn'),
52
- debug: document.getElementById('debugBtn'),
53
- connect: document.getElementById('connectBtn'),
54
- disconnect: document.getElementById('disconnectBtn'),
55
-
56
- // Video elements
57
- localVideo: document.getElementById('localVideo'),
58
- remoteVideo: document.getElementById('remoteVideo'),
59
- localWrapper: document.getElementById('localWrapper'),
60
- avatarWrapper: document.getElementById('avatarWrapper'),
61
- localOverlay: document.getElementById('localOverlay'),
62
- avatarOverlay: document.getElementById('avatarOverlay'),
63
-
64
- // Status indicators
65
- systemStatus: document.getElementById('systemStatus'),
66
- localStatus: document.getElementById('localStatus'),
67
- avatarStatus: document.getElementById('avatarStatus'),
68
- statusText: document.getElementById('statusText'),
69
- localStatusText: document.getElementById('localStatusText'),
70
- avatarStatusText: document.getElementById('avatarStatusText'),
71
-
72
- // Metrics
73
- latencyValue: document.getElementById('latencyValue'),
74
- fpsValue: document.getElementById('fpsValue'),
75
- gpuValue: document.getElementById('gpuValue'),
76
- qualityValue: document.getElementById('qualityValue'),
77
-
78
- // Toast
79
- toast: document.getElementById('toast'),
80
- toastContent: document.getElementById('toastContent')
81
- };
82
-
83
- // UI Helper Functions
84
- function setSystemStatus(status, text) {
85
- els.systemStatus.className = `status-indicator status-${status}`;
86
- els.statusText.textContent = text;
87
- }
88
-
89
- function setLocalStatus(status, text) {
90
- els.localStatus.className = `status-indicator status-${status}`;
91
- els.localStatusText.textContent = text;
92
- }
93
-
94
- function setAvatarStatus(status, text) {
95
- els.avatarStatus.className = `status-indicator status-${status}`;
96
- els.avatarStatusText.textContent = text;
97
- }
98
-
99
- function showToast(message, type = 'info') {
100
- els.toastContent.textContent = message;
101
- els.toast.className = `toast toast-${type} show`;
102
- setTimeout(() => {
103
- els.toast.classList.remove('show');
104
- }, 4000);
105
- }
106
-
107
- function updateMetrics(latency, fps, gpu, quality = 'HD') {
108
- els.latencyValue.textContent = latency || '--';
109
- els.fpsValue.textContent = fps || '--';
110
- els.gpuValue.textContent = gpu || '--';
111
- els.qualityValue.textContent = quality || '--';
112
- }
113
-
114
- function setButtonLoading(button, loading = true) {
115
- if (loading) {
116
- button.disabled = true;
117
- const originalText = button.innerHTML;
118
- button.dataset.originalText = originalText;
119
- button.innerHTML = '<span class="loading-spinner"></span> Processing...';
120
- } else {
121
- button.disabled = false;
122
- if (button.dataset.originalText) {
123
- button.innerHTML = button.dataset.originalText;
124
- delete button.dataset.originalText;
125
- }
126
- }
127
- }
128
-
129
- function setStatus(txt) {
130
- setSystemStatus('idle', txt);
131
- }
132
-
133
- function log(...a) {
134
- console.log('[MIRAGE]', ...a);
135
- }
136
- // Verbose toggle (can be overridden by backend-provided global or URL param ?wv=1)
137
- const VERBOSE = (window.MIRAGE_WEBRTC_VERBOSE === true) || (new URLSearchParams(location.search).get('wv')==='1');
138
- const STATS_INTERVAL_MS = (window.MIRAGE_WEBRTC_STATS_INTERVAL_MS) || 5000;
139
- let statsTimer = null;
140
-
141
- function vlog(...args){ if(VERBOSE) console.log('[PROD][VERBOSE]', ...args); }
142
-
143
- function attachPcDiagnostics(pc){
144
- if(!pc) return;
145
- const evMap = ['signalingstatechange','iceconnectionstatechange','icegatheringstatechange','connectionstatechange','negotiationneeded'];
146
- evMap.forEach(ev => {
147
- pc.addEventListener(ev, ()=>{ vlog('pc event', ev, diagSnapshot()); });
148
- });
149
- pc.addEventListener('track', ev => { vlog('pc track event', ev.track && ev.track.kind, ev.streams && ev.streams.length); });
150
- pc.onicecandidate = (e)=>{ if(e.candidate){ vlog('ice candidate', e.candidate.type, e.candidate.protocol, e.candidate.address, e.candidate.relatedAddress||null); } else { vlog('ice candidate gathering complete'); } };
151
- }
152
-
153
- function diagSnapshot(){
154
- if(!state.pc) return {};
155
- return {
156
- signaling: state.pc.signalingState,
157
- iceConnection: state.pc.iceConnectionState,
158
- iceGathering: state.pc.iceGatheringState,
159
- connection: state.pc.connectionState,
160
- localTracks: state.pc.getSenders().map(s=>({kind:s.track && s.track.kind, ready:s.track && s.track.readyState})),
161
- remoteTracks: state.pc.getReceivers().map(r=>({kind:r.track && r.track.kind, ready:r.track && r.track.readyState}))
162
- };
163
- }
164
-
165
- async function collectStats(){
166
- if(!state.pc) return;
167
- try {
168
- const stats = await state.pc.getStats();
169
- const summary = { ts: Date.now(), outbound: {}, inbound: {}, candidatePairs: [] };
170
- stats.forEach(report => {
171
- if(report.type === 'outbound-rtp' && !report.isRemote){
172
- summary.outbound[report.kind||report.mediaType||'unknown'] = {
173
- bitrateKbps: report.bytesSent && report.timestamp ? undefined : undefined,
174
- frames: report.framesEncoded,
175
- q: report.qualityLimitationReason,
176
- packetsSent: report.packetsSent
177
- };
178
- } else if(report.type === 'inbound-rtp' && !report.isRemote){
179
- summary.inbound[report.kind||report.mediaType||'unknown'] = {
180
- jitter: report.jitter,
181
- packetsLost: report.packetsLost,
182
- frames: report.framesDecoded,
183
- packetsReceived: report.packetsReceived
184
- };
185
- } else if(report.type === 'candidate-pair' && report.state === 'succeeded'){
186
- summary.candidatePairs.push({
187
- current: report.nominated,
188
- bytesSent: report.bytesSent,
189
- bytesReceived: report.bytesReceived,
190
- rtt: report.currentRoundTripTime,
191
- availableOutgoingBitrate: report.availableOutgoingBitrate
192
- });
193
- }
194
- });
195
- vlog('webrtc stats', summary);
196
- } catch(e){ vlog('stats error', e); }
197
- }
198
-
199
- async function handleReference(e){
200
- const file = e.target.files && e.target.files[0];
201
- if(!file) {
202
- els.uploadButton.classList.remove('has-file');
203
- els.uploadText.textContent = 'Choose Reference Image';
204
- return;
205
- }
206
-
207
- // Update UI immediately
208
- els.uploadButton.classList.add('has-file');
209
- els.uploadText.textContent = `✓ ${file.name}`;
210
- showToast(`Reference image selected: ${file.name}`, 'success');
211
-
212
- // Cache base64 for datachannel use
213
- const buf = await file.arrayBuffer();
214
- const b64 = btoa(String.fromCharCode(...new Uint8Array(buf)));
215
- state.referenceImage = b64;
216
-
217
- // Also POST to HTTP endpoint so the pipeline has the reference even before WebRTC connects
218
- try {
219
- setSystemStatus('connecting', 'Uploading reference image...');
220
- const fd = new FormData();
221
- fd.append('file', new Blob([buf], {type: file.type||'application/octet-stream'}), file.name||'reference');
222
- const resp = await fetch('/set_reference', {method:'POST', body: fd});
223
- const jr = await resp.json().catch(()=>({}));
224
- if (resp.ok && jr && (jr.status==='success' || jr.status==='ok')){
225
- setSystemStatus('connected', 'Reference image uploaded successfully');
226
- showToast('Reference image set successfully', 'success');
227
- } else {
228
- setSystemStatus('error', 'Reference upload failed');
229
- showToast('Failed to upload reference image', 'error');
230
- console.warn('set_reference response', resp.status, jr);
231
- }
232
- } catch(err){
233
- console.warn('set_reference error', err);
234
- setSystemStatus('error', 'Reference upload error');
235
- showToast('Error uploading reference image', 'error');
236
- }
237
-
238
- // If already connected, also send via data channel for immediate in-session update
239
- try {
240
- if (state.connected && state.control && state.control.readyState === 'open') {
241
- state.control.send(JSON.stringify({type:'set_reference', image_base64: state.referenceImage}));
242
- }
243
- } catch(_) {}
244
- }
245
-
246
- async function connect(options){
247
- const overrideRelay = options && options.forceRelay === true;
248
- if(state.connected) return;
249
- if(state.connecting) return;
250
- try {
251
- setSystemStatus('connecting', 'Requesting camera access...');
252
- setLocalStatus('connecting', 'Initializing');
253
- setButtonLoading(els.connect, true);
254
- els.disconnect.disabled = false; // allow cancel during negotiation
255
- state.cancelled = false; state.connecting = true;
256
- // Quick ping to verify router is mounted
257
- try {
258
- const ping = await fetch('/webrtc/ping');
259
- if(ping.ok){ const j = await ping.json(); log('webrtc ping', j); }
260
- } catch(_){ }
261
- // Fetch short-lived auth token (if server requires)
262
- let authToken = state.authToken;
263
- try {
264
- const t = await fetch('/webrtc/token');
265
- if (t.ok) {
266
- const j = await t.json();
267
- authToken = j.token; state.authToken = authToken;
268
- } else if (t.status === 404) {
269
- // Likely router not mounted; keep null and let server decide
270
- console.warn('Token endpoint 404 - proceeding without token');
271
- }
272
- } catch(_){}
273
- state.localStream = await navigator.mediaDevices.getUserMedia({video:true,audio:true});
274
- els.localVideo.srcObject = state.localStream;
275
- els.localWrapper.classList.add('active');
276
- setLocalStatus('connected', 'Camera Active');
277
- try { els.localVideo.play && els.localVideo.play(); } catch(_) {}
278
- setSystemStatus('connecting', 'Establishing connection...');
279
- let iceCfg = {iceServers:[{urls:['stun:stun.l.google.com:19302']}]};
280
- try {
281
- const ic = await fetch('/webrtc/ice_config');
282
- if (ic.ok) { iceCfg = await ic.json(); }
283
- } catch(_){ }
284
- state._lastIceCfg = iceCfg; // keep for potential retry decision
285
- if (overrideRelay || FORCE_RELAY_URL || iceCfg.forceRelay === true) { iceCfg.iceTransportPolicy = 'relay'; }
286
- log('ice config', iceCfg);
287
- state.pc = new RTCPeerConnection(iceCfg);
288
- attachPcDiagnostics(state.pc);
289
- state._usedRelay = !!iceCfg.iceTransportPolicy && iceCfg.iceTransportPolicy === 'relay';
290
- state._relayFallbackTried = !!overrideRelay || !!FORCE_RELAY_URL; // if already forcing relay, don't fallback again
291
- state.pc.oniceconnectionstatechange = ()=>{
292
- log('ice state', state.pc.iceConnectionState);
293
- if(['failed','closed'].includes(state.pc.iceConnectionState)){
294
- if(!state.cancelled) disconnect();
295
- }
296
- if(state.pc.iceConnectionState === 'disconnected'){
297
- vlog('ICE disconnected snapshot', diagSnapshot());
298
- }
299
- };
300
- state.pc.onconnectionstatechange = ()=>{
301
- const st = state.pc.connectionState;
302
- log('pc state', st);
303
- if(st === 'connected' && !statsTimer){
304
- statsTimer = setInterval(collectStats, STATS_INTERVAL_MS);
305
- }
306
- if(st === 'disconnected'){
307
- vlog('PC disconnected snapshot', diagSnapshot());
308
- try { state.pc.restartIce && state.pc.restartIce(); vlog('Attempted ICE restart'); } catch(_){ }
309
- }
310
- if(['failed','closed'].includes(st)){
311
- if(statsTimer){ clearInterval(statsTimer); statsTimer=null; }
312
- const hasTurn = state._lastIceCfg && (state._lastIceCfg.turnCount||0) > 0;
313
- const tryRelay = hasTurn && !state._usedRelay && !state._relayFallbackTried;
314
- const snapshot = diagSnapshot();
315
- vlog('Final failure snapshot', snapshot, {hasTurn, usedRelay: state._usedRelay, relayTried: state._relayFallbackTried});
316
- disconnect().then(()=>{
317
- if (tryRelay) {
318
- state._relayFallbackTried = true;
319
- log('retrying with relay-only');
320
- setStatus('Retrying with TURN relay');
321
- connect({forceRelay:true});
322
- } else if (!hasTurn && !state._usedRelay && !state._relayFallbackTried) {
323
- log('skipping relay-only retry: no TURN servers available');
324
- setStatus('No TURN servers; cannot retry relay-only');
325
- }
326
- });
327
- }
328
- };
329
- state.pc.ontrack = ev => {
330
- try {
331
- const tr = ev.track;
332
- log('ontrack', tr && tr.kind, tr && tr.readyState, ev.streams && ev.streams.length);
333
- if (tr && tr.kind === 'video') {
334
- setSystemStatus('connected', 'Avatar stream received');
335
- setAvatarStatus('connected', 'Active');
336
- let stream;
337
- if(ev.streams && ev.streams[0]){
338
- stream = ev.streams[0];
339
- log('Using provided stream:', stream.id, 'tracks:', stream.getTracks().length);
340
- } else {
341
- stream = new MediaStream([ev.track]);
342
- log('Created new MediaStream:', stream.id);
343
- }
344
-
345
- // Force new srcObject every time to avoid conflicts
346
- log('Setting srcObject on video element, current value:', els.remoteVideo.srcObject);
347
- els.remoteVideo.srcObject = null;
348
- els.remoteVideo.srcObject = stream;
349
- els.avatarWrapper.classList.add('active');
350
- log('srcObject set, waiting for loadeddata...');
351
-
352
- // Add more event listeners for debugging
353
- els.remoteVideo.onloadstart = () => log('video: loadstart');
354
- els.remoteVideo.onloadedmetadata = () => log('video: loadedmetadata', els.remoteVideo.videoWidth, 'x', els.remoteVideo.videoHeight);
355
- els.remoteVideo.onloadeddata = () => {
356
- log('video: loadeddata, attempting play()');
357
- els.remoteVideo.play().catch(e => {
358
- log('play error', e.name, e.message);
359
- // Try playing again after a short delay
360
- setTimeout(() => {
361
- log('Retry play() after error...');
362
- els.remoteVideo.play().catch(e2 => log('Retry play failed:', e2.name));
363
- }, 100);
364
- });
365
- };
366
- els.remoteVideo.onplaying = () => {
367
- log('video: playing');
368
- showToast('Avatar stream connected successfully', 'success');
369
- };
370
- els.remoteVideo.onerror = (e) => {
371
- log('video error:', e);
372
- setAvatarStatus('error', 'Stream Error');
373
- };
374
- els.remoteVideo.onstalled = () => log('video: stalled');
375
-
376
- // Monitor video track state changes
377
- tr.onended = () => {
378
- log('video track ended');
379
- setAvatarStatus('idle', 'Disconnected');
380
- els.avatarWrapper.classList.remove('active');
381
- };
382
- tr.onmute = () => {
383
- log('video track muted');
384
- setAvatarStatus('warning', 'Muted');
385
- };
386
- tr.onunmute = () => {
387
- log('video track unmuted');
388
- setAvatarStatus('connected', 'Active');
389
- };
390
-
391
- } else if (tr && tr.kind === 'audio') {
392
- setSystemStatus('connected', 'Audio stream received');
393
- }
394
- } catch(e){
395
- log('ontrack error', e);
396
- setAvatarStatus('error', 'Connection Error');
397
- }
398
- };
399
- state.control = state.pc.createDataChannel('control');
400
- state.control.onopen = ()=>{
401
- setSystemStatus('connected', 'WebRTC connection established');
402
- state.connected = true;
403
- state.connecting = false;
404
- setButtonLoading(els.connect, false);
405
- els.connect.disabled = true;
406
- els.disconnect.disabled = false;
407
- showToast('WebRTC connection established', 'success');
408
-
409
- if(state.referenceImage){
410
- try {
411
- state.control.send(JSON.stringify({type:'set_reference', image_base64: state.referenceImage}));
412
- showToast('Reference image sent to avatar', 'success');
413
- } catch(e) {
414
- showToast('Failed to send reference image', 'error');
415
- }
416
- }
417
- // Metrics polling
418
- state.metricsTimer = setInterval(()=>{
419
- try { state.control.send(JSON.stringify({type:'metrics_request'})); }catch(_){ }
420
- }, 4000);
421
- };
422
- state.control.onmessage = (e)=>{
423
- try {
424
- const data = JSON.parse(e.data);
425
- if(data.type==='metrics' && data.payload){
426
- updatePerf(data.payload);
427
- } else if (data.type === 'reference_ack'){
428
- setStatus('Reference set');
429
- } else if (data.type === 'error' && data.message){
430
- setStatus('Error: '+data.message);
431
- }
432
- } catch(_){ }
433
- };
434
- state.localStream.getTracks().forEach(t=> state.pc.addTrack(t, state.localStream));
435
- const offer = await state.pc.createOffer({offerToReceiveAudio:true,offerToReceiveVideo:true});
436
- await state.pc.setLocalDescription(offer);
437
- // Wait for ICE gathering to complete (non-trickle) to avoid connectivity issues
438
- setStatus('Gathering ICE');
439
- await new Promise((resolve)=>{
440
- if(state.pc.iceGatheringState === 'complete') return resolve();
441
- const to = setTimeout(()=>{ resolve(); }, 7000);
442
- state.pc.onicegatheringstatechange = ()=>{
443
- if(state.pc.iceGatheringState === 'complete'){
444
- clearTimeout(to); resolve();
445
- }
446
- };
447
- });
448
- setStatus('Negotiating');
449
- const headers = {'Content-Type':'application/json'};
450
- if (authToken) headers['X-Auth-Token'] = authToken;
451
- // Use the possibly-updated localDescription (with ICE candidates)
452
- const ld = state.pc.localDescription;
453
- const r = await fetch('/webrtc/offer',{method:'POST', headers, body: JSON.stringify({sdp:ld.sdp, type:ld.type})});
454
- if(!r.ok){
455
- let bodyText = '';
456
- try { bodyText = await r.text(); } catch(_){ }
457
- if(r.status===401 || r.status===403){
458
- setStatus('Unauthorized (check API key/token)');
459
- } else if (r.status===404){
460
- setStatus('Offer endpoint not found (server not exposing /webrtc)');
461
- } else if (r.status===503){
462
- try { const txt = bodyText || ''; setStatus('Offer failed 503'); console.warn('503 body', txt); }
463
- catch(_){ setStatus('Offer failed 503'); }
464
- } else {
465
- setStatus('Offer failed '+r.status);
466
- console.warn('offer error body', bodyText);
467
- }
468
- els.connect.disabled=false; els.disconnect.disabled=true; state.connecting=false; return;
469
- }
470
- const answer = await r.json();
471
- await state.pc.setRemoteDescription(answer);
472
- setStatus('Finalizing');
473
- } catch(e){
474
- log('connect error', e);
475
- setStatus('Error');
476
- els.connect.disabled = false; els.disconnect.disabled=true;
477
- }
478
- finally {
479
- state.connecting = false;
480
- }
481
- }
482
-
483
- function updatePerf(p){
484
- try {
485
- const fps = (p.video_fps || 0).toFixed(1);
486
- const lat = Math.round(p.avg_video_latency_ms || 0);
487
- const gpu = (p.gpu_memory_used !== undefined) ? (p.gpu_memory_used.toFixed(2)+'GB') : '--';
488
- const method = p.last_method ? String(p.last_method) : '--';
489
- els.perf.textContent = `Latency: ${lat} ms · FPS: ${fps} · GPU: ${gpu} · Mode: ${method}`;
490
- } catch(_){}
491
- }
492
-
493
- async function disconnect(){
494
- state.cancelled = true;
495
- if(statsTimer){ clearInterval(statsTimer); statsTimer=null; }
496
- if(state.metricsTimer){ clearInterval(state.metricsTimer); state.metricsTimer=null; }
497
- if(state.control){ try { state.control.onmessage=null; state.control.close(); }catch(_){} }
498
- if(state.pc){ try { state.pc.ontrack=null; state.pc.onconnectionstatechange=null; state.pc.oniceconnectionstatechange=null; state.pc.onicegatheringstatechange=null; state.pc.close(); }catch(_){} }
499
- if(state.localStream){ try { state.localStream.getTracks().forEach(t=>t.stop()); } catch(_){} }
500
-
501
- // Clear media elements and UI state
502
- try {
503
- els.localVideo.srcObject = null;
504
- els.localWrapper.classList.remove('active');
505
- setLocalStatus('idle', 'Inactive');
506
- } catch(_){}
507
- try {
508
- if (els.remoteVideo.srcObject) {
509
- els.remoteVideo.pause();
510
- els.remoteVideo.srcObject = null;
511
- }
512
- els.avatarWrapper.classList.remove('active');
513
- setAvatarStatus('idle', 'Inactive');
514
- } catch(_){}
515
-
516
- // Reset metrics
517
- updateMetrics('--', '--', '--', '--');
518
-
519
- // Best-effort server cleanup
520
- try {
521
- const hdrs = {};
522
- if (state.authToken) hdrs['X-Auth-Token'] = state.authToken;
523
- await fetch('/webrtc/cleanup', {method:'POST', headers: hdrs});
524
- } catch(_){ }
525
-
526
- state.pc=null; state.control=null; state.localStream=null; state.connected=false; state.connecting=false;
527
- setButtonLoading(els.connect, false);
528
- els.connect.disabled=false; els.disconnect.disabled=true;
529
- setSystemStatus('idle', 'Disconnected');
530
- showToast('Connection terminated', 'warning');
531
- }
532
-
533
- els.ref.addEventListener('change', handleReference);
534
- if (els.init) {
535
- els.init.addEventListener('click', async ()=>{
536
- try {
537
- setSystemStatus('connecting', 'Initializing AI pipeline...');
538
- setButtonLoading(els.init, true);
539
- const r = await fetch('/initialize', {method:'POST'});
540
- const j = await r.json().catch(()=>({}));
541
- if (r.ok && j && (j.status==='success' || j.status==='already_initialized')){
542
- state.initialized = true;
543
- setSystemStatus('connected', j.message || 'Pipeline initialized');
544
- showToast('AI pipeline initialized successfully', 'success');
545
- els.init.classList.remove('btn-secondary');
546
- els.init.classList.add('btn-success');
547
- els.init.innerHTML = `
548
- <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
549
- <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/>
550
- <polyline points="22,4 12,14.01 9,11.01"/>
551
- </svg>
552
- Pipeline Ready
553
- `;
554
- } else {
555
- setSystemStatus('error', 'Pipeline initialization failed');
556
- showToast('Failed to initialize AI pipeline', 'error');
557
- console.warn('initialize response', r.status, j);
558
- }
559
- } catch(e){
560
- setSystemStatus('error', 'Pipeline initialization error');
561
- showToast('Error initializing AI pipeline', 'error');
562
- } finally {
563
- setButtonLoading(els.init, false);
564
- }
565
- });
566
- }
567
- if (els.debug) {
568
- els.debug.addEventListener('click', async ()=>{
569
- try {
570
- setSystemStatus('connecting', 'Fetching debug information...');
571
- setButtonLoading(els.debug, true);
572
- const r = await fetch('/debug/models');
573
- const j = await r.json();
574
- console.log('[DEBUG] /debug/models', j);
575
-
576
- // Show debug info in toast
577
- const modelCount = Object.keys(j.files || {}).length;
578
- const existingModels = Object.values(j.files || {}).filter(f => f.exists).length;
579
- showToast(`Debug: ${existingModels}/${modelCount} models loaded`, 'info');
580
-
581
- const app = j.files?.['appearance_feature_extractor.onnx'];
582
- const motion = j.files?.['motion_extractor.onnx'];
583
- const inswapper = j.files?.['inswapper_128_fp16.onnx'] || j.files?.['inswapper_128.onnx'];
584
-
585
- let statusText = `Models: InSwapper=${inswapper?.exists?'✓':'✗'}, App=${app?.exists?'✓':'✗'}, Motion=${motion?.exists?'✓':'✗'}`;
586
- setSystemStatus(inswapper?.exists ? 'connected' : 'warning', statusText);
587
-
588
- // If missing critical models, try to download
589
- if (!inswapper?.exists) {
590
- setSystemStatus('connecting', 'Downloading models...');
591
- try {
592
- const d = await fetch('/debug/download_models', {method:'POST'});
593
- const dj = await d.json().catch(()=>({}));
594
- console.log('[DEBUG] /debug/download_models', dj);
595
- showToast('Model download initiated', 'info');
596
-
597
- // Refresh model status
598
- setTimeout(async () => {
599
- const r2 = await fetch('/debug/models');
600
- const j2 = await r2.json();
601
- const inswapper2 = j2.files?.['inswapper_128_fp16.onnx'] || j2.files?.['inswapper_128.onnx'];
602
- const newStatus = `Models refreshed: InSwapper=${inswapper2?.exists?'✓':'✗'}`;
603
- setSystemStatus(inswapper2?.exists ? 'connected' : 'warning', newStatus);
604
- }, 2000);
605
- } catch(e) {
606
- showToast('Model download failed', 'error');
607
- console.warn('download_models failed', e);
608
- }
609
- }
610
- } catch(e){
611
- setSystemStatus('error', 'Debug fetch failed');
612
- showToast('Failed to fetch debug information', 'error');
613
- } finally {
614
- setButtonLoading(els.debug, false);
615
- }
616
- });
617
- }
618
-
619
- // Update performance metrics display
620
- function updatePerf(metrics) {
621
- try {
622
- const latency = metrics.latency_ms ? `${Math.round(metrics.latency_ms)}` : '--';
623
- const fps = metrics.fps ? `${Math.round(metrics.fps)}` : '--';
624
- const gpu = metrics.gpu_memory_used_mb ? `${Math.round(metrics.gpu_memory_used_mb)}MB` : '--';
625
- const quality = metrics.quality || (fps > 25 ? 'HD' : fps > 15 ? 'SD' : 'Low');
626
-
627
- updateMetrics(latency, fps, gpu, quality);
628
-
629
- // Update connection quality indicator
630
- if (metrics.latency_ms) {
631
- if (metrics.latency_ms < 100) {
632
- setAvatarStatus('connected', 'Excellent');
633
- } else if (metrics.latency_ms < 250) {
634
- setAvatarStatus('connected', 'Good');
635
- } else {
636
- setAvatarStatus('warning', 'High Latency');
637
- }
638
- }
639
- } catch(e) {
640
- console.warn('updatePerf error', e);
641
- }
642
- }
643
-
644
- // Event listeners
645
- els.connect.addEventListener('click', connect);
646
- els.disconnect.addEventListener('click', disconnect);
647
-
648
- // Auto-initialize on page load (idempotent). Helps when HTML cache hides the button.
649
- (async ()=>{
650
- try {
651
- setStatus('Initializing pipeline...');
652
- const r = await fetch('/initialize', {method:'POST'});
653
- const j = await r.json().catch(()=>({}));
654
- if (r.ok && j && (j.status==='success' || j.status==='already_initialized')){
655
- setStatus(j.message || 'Initialized');
656
- } else {
657
- // Don’t spam status if it fails; user can still proceed to Connect
658
- console.warn('auto-initialize response', r.status, j);
659
- setStatus('Idle');
660
- }
661
- } catch(e){
662
- // Silent fail; keep UI responsive
663
- setStatus('Idle');
664
- }
665
- })();
666
- })();