-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.html
More file actions
540 lines (512 loc) · 16.3 KB
/
page.html
File metadata and controls
540 lines (512 loc) · 16.3 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>Drive Coding</title>
<style>
:root {
--bg: #0d1117;
--surface: #161b22;
--border: #30363d;
--text: #e6edf3;
--muted: #8b949e;
--accent: #58a6ff;
--recording: #f85149;
--success: #3fb950;
--play: #3fb950;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'SF Pro', system-ui, sans-serif;
background: var(--bg);
color: var(--text);
min-height: 100vh;
display: flex;
flex-direction: column;
-webkit-user-select: none;
user-select: none;
}
header {
padding: 12px 20px 10px;
border-bottom: 1px solid var(--border);
display: flex;
justify-content: space-between;
align-items: center;
}
header h1 { font-size: 17px; font-weight: 600; letter-spacing: -0.3px; }
.status {
font-size: 12px;
color: var(--muted);
display: flex;
align-items: center;
gap: 6px;
}
.status-dot {
width: 8px; height: 8px; border-radius: 50%;
background: var(--success);
}
.status-dot.active { background: var(--accent); animation: pulse 1.5s infinite; }
.status-dot.recording { background: var(--recording); animation: pulse 0.8s infinite; }
.status-dot.ready-play { background: var(--play); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.4; } }
#transcript {
flex: 1;
overflow-y: auto;
padding: 12px 16px;
display: flex;
flex-direction: column;
gap: 10px;
-webkit-overflow-scrolling: touch;
}
.msg {
max-width: 85%;
padding: 10px 14px;
border-radius: 16px;
font-size: 15px;
line-height: 1.4;
word-wrap: break-word;
}
.msg.user {
align-self: flex-end;
background: var(--accent);
color: #fff;
border-bottom-right-radius: 4px;
}
.msg.assistant {
align-self: flex-start;
background: var(--surface);
border: 1px solid var(--border);
border-bottom-left-radius: 4px;
}
.msg .meta { font-size: 11px; color: var(--muted); margin-top: 4px; }
.msg.user .meta { color: rgba(255,255,255,0.6); }
.controls {
padding: 12px 20px 24px;
border-top: 1px solid var(--border);
display: flex;
flex-direction: column;
gap: 10px;
align-items: center;
}
/* The Big Button */
#walkie-btn {
width: 96px;
height: 96px;
border-radius: 50%;
border: 4px solid var(--accent);
background: transparent;
color: var(--accent);
font-size: 14px;
font-weight: 600;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 4px;
cursor: pointer;
transition: all 0.2s;
-webkit-tap-highlight-color: transparent;
}
#walkie-btn .icon { font-size: 32px; line-height: 1; }
#walkie-btn .label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; }
#walkie-btn.recording {
border-color: var(--recording);
background: var(--recording);
color: #fff;
animation: pulse-border 1s infinite;
}
#walkie-btn.waiting {
border-color: var(--accent);
color: var(--muted);
opacity: 0.5;
pointer-events: none;
}
#walkie-btn.play-ready {
border-color: var(--play);
background: var(--play);
color: #fff;
animation: pulse-border-green 1.5s infinite;
}
#walkie-btn.speaking {
border-color: var(--play);
color: var(--play);
opacity: 0.8;
}
@keyframes pulse-border {
0%,100% { box-shadow: 0 0 0 0 rgba(248,81,73,0.4); }
50% { box-shadow: 0 0 0 14px rgba(248,81,73,0); }
}
@keyframes pulse-border-green {
0%,100% { box-shadow: 0 0 0 0 rgba(63,185,80,0.4); }
50% { box-shadow: 0 0 0 14px rgba(63,185,80,0); }
}
.text-input-row {
display: flex;
gap: 8px;
width: 100%;
}
#text-input {
flex: 1;
background: var(--surface);
border: 1px solid var(--border);
color: var(--text);
padding: 10px 14px;
border-radius: 20px;
font-size: 14px;
outline: none;
}
#text-input:focus { border-color: var(--accent); }
#send-btn {
width: 40px; height: 40px;
border-radius: 50%;
border: none;
background: var(--accent);
color: #fff;
font-size: 16px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
-webkit-tap-highlight-color: transparent;
}
.settings {
display: flex;
gap: 16px;
font-size: 11px;
color: var(--muted);
}
.settings label { display: flex; align-items: center; gap: 4px; }
.settings input[type="range"] { width: 60px; }
#loop-val { min-width: 20px; text-align: center; }
</style>
</head>
<body>
<header>
<h1>Drive Coding</h1>
<div class="status">
<div class="status-dot" id="status-dot"></div>
<span id="status-text">Ready</span>
</div>
</header>
<div id="transcript"></div>
<div class="controls">
<button id="walkie-btn">
<span class="icon" id="btn-icon">🎤</span>
<span class="label" id="btn-label">Record</span>
</button>
<div class="text-input-row">
<input type="text" id="text-input" placeholder="or type..." autocomplete="off" autocorrect="on">
<button id="send-btn">
<svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor"><path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z"/></svg>
</button>
</div>
<div class="settings">
<label>Poll <input type="range" id="loop-speed" min="2" max="30" value="5" step="1"><span id="loop-val">5s</span></label>
</div>
</div>
<script>
// ─── State machine ──────────────────────────────────────────
let state = 'idle';
let mediaRecorder = null;
let audioChunks = [];
let pollTimer = null;
let pendingText = '';
let currentAudio = null; // Audio element for Kokoro playback
const transcript = document.getElementById('transcript');
const walkieBtn = document.getElementById('walkie-btn');
const btnIcon = document.getElementById('btn-icon');
const btnLabel = document.getElementById('btn-label');
const statusDot = document.getElementById('status-dot');
const statusText = document.getElementById('status-text');
const loopSpeed = document.getElementById('loop-speed');
const loopVal = document.getElementById('loop-val');
const textInput = document.getElementById('text-input');
const sendBtn = document.getElementById('send-btn');
const synth = window.speechSynthesis;
function setState(newState) {
state = newState;
walkieBtn.className = '';
statusDot.className = 'status-dot';
switch (state) {
case 'idle':
btnIcon.textContent = '\u{1F3A4}';
btnLabel.textContent = 'Record';
setStatus('Tap to talk');
break;
case 'recording':
walkieBtn.className = 'recording';
statusDot.className = 'status-dot recording';
btnIcon.textContent = '\u{23F9}';
btnLabel.textContent = 'Stop';
setStatus('Recording...');
break;
case 'sending':
walkieBtn.className = 'waiting';
statusDot.className = 'status-dot active';
btnIcon.textContent = '\u{2197}';
btnLabel.textContent = 'Sending';
setStatus('Transcribing...');
break;
case 'waiting':
walkieBtn.className = 'waiting';
statusDot.className = 'status-dot active';
btnIcon.textContent = '\u{231B}';
btnLabel.textContent = 'Waiting';
setStatus('Waiting for Claude...');
break;
case 'play-ready':
walkieBtn.className = 'play-ready';
statusDot.className = 'status-dot ready-play';
btnIcon.textContent = '\u{25B6}';
btnLabel.textContent = 'Play';
setStatus('Response ready \u2014 tap to hear');
break;
case 'speaking':
walkieBtn.className = 'speaking';
statusDot.className = 'status-dot active';
btnIcon.textContent = '\u{1F50A}';
btnLabel.textContent = 'Playing';
setStatus('Speaking...');
break;
}
}
// ─── The Big Button handler ──────────────────────────────────
walkieBtn.addEventListener('click', async () => {
switch (state) {
case 'idle':
startRecording();
break;
case 'recording':
stopRecording();
break;
case 'play-ready':
playResponse();
break;
case 'speaking':
// Barge-in: kill audio, start recording
stopAudio();
pendingText = '';
startRecording();
break;
}
});
// ─── Stop any playing audio ─────────────────────────────────
function stopAudio() {
if (currentAudio) {
currentAudio.pause();
currentAudio.src = '';
currentAudio = null;
}
synth.cancel();
}
// ─── MediaRecorder ───────────────────────────────────────────
async function startRecording() {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const mimeType = MediaRecorder.isTypeSupported('audio/webm;codecs=opus')
? 'audio/webm;codecs=opus' : 'audio/mp4';
mediaRecorder = new MediaRecorder(stream, { mimeType });
audioChunks = [];
mediaRecorder.ondataavailable = (e) => {
if (e.data.size > 0) audioChunks.push(e.data);
};
mediaRecorder.onstop = async () => {
stream.getTracks().forEach(t => t.stop());
if (audioChunks.length === 0) { setState('idle'); return; }
const blob = new Blob(audioChunks, { type: mimeType });
if (blob.size < 1000) { setState('idle'); return; }
await sendAudio(blob, mimeType);
};
mediaRecorder.start();
setState('recording');
} catch (e) {
console.error('Mic error:', e);
setStatus('Mic access denied');
}
}
function stopRecording() {
if (mediaRecorder && state === 'recording') {
setState('sending');
mediaRecorder.stop();
}
}
// ─── Send audio to server ────────────────────────────────────
async function sendAudio(blob, mimeType) {
const formData = new FormData();
const ext = mimeType.includes('mp4') ? 'mp4' : 'webm';
formData.append('audio', blob, `recording.${ext}`);
try {
const r = await fetch('/inbox-audio', { method: 'POST', body: formData });
const data = await r.json();
if (data.text && data.text.trim()) {
addMessage(data.text, 'user');
setState('waiting');
startPolling();
} else {
setStatus('No speech detected');
setTimeout(() => setState('idle'), 1500);
}
} catch (e) {
console.error(e);
setStatus('Send failed');
setTimeout(() => setState('idle'), 1500);
}
}
// ─── Text input (fallback) ───────────────────────────────────
async function sendToInbox(text) {
addMessage(text, 'user');
setState('waiting');
try {
const r = await fetch('/inbox', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text }),
});
if (r.ok) startPolling();
} catch (e) {
setStatus('Send failed');
setTimeout(() => setState('idle'), 1500);
}
}
// ─── Poll for response ───────────────────────────────────────
function startPolling() {
if (pollTimer) clearInterval(pollTimer);
const interval = parseInt(loopSpeed.value) * 1000;
pollOutbox();
pollTimer = setInterval(pollOutbox, interval);
}
async function pollOutbox() {
try {
const r = await fetch('/outbox');
const data = await r.json();
if (data.text) {
if (pollTimer) { clearInterval(pollTimer); pollTimer = null; }
addMessage(data.text, 'assistant');
saveMessages();
pendingText = data.text;
setState('play-ready');
}
} catch (e) {
console.error('Poll error:', e);
}
}
// ─── Play response (server-side Kokoro TTS → audio element) ─
async function playResponse() {
if (!pendingText) { setState('idle'); return; }
setState('speaking');
// Try server-side Kokoro TTS first
try {
const r = await fetch('/tts', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: pendingText }),
});
if (r.ok) {
const blob = await r.blob();
const url = URL.createObjectURL(blob);
currentAudio = new Audio(url);
currentAudio.onended = () => {
URL.revokeObjectURL(url);
currentAudio = null;
if (state === 'speaking') { pendingText = ''; setState('idle'); }
};
currentAudio.onerror = () => {
URL.revokeObjectURL(url);
currentAudio = null;
fallbackTTS();
};
currentAudio.play();
// Safety timeout
setTimeout(() => { if (state === 'speaking') { stopAudio(); pendingText = ''; setState('idle'); } }, 60000);
return;
}
} catch (e) {
console.error('Kokoro TTS fetch failed:', e);
}
// Fallback: browser speechSynthesis
fallbackTTS();
}
function fallbackTTS() {
if (!synth || !pendingText) { pendingText = ''; setState('idle'); return; }
const utt = new SpeechSynthesisUtterance(pendingText);
const voices = synth.getVoices();
const preferred = ['Samantha', 'Karen', 'Daniel', 'Moira'];
for (const name of preferred) {
const v = voices.find(v => v.name.includes(name) && v.lang.startsWith('en'));
if (v) { utt.voice = v; break; }
}
utt.rate = 1.0;
utt.onend = () => { pendingText = ''; setState('idle'); };
utt.onerror = () => { pendingText = ''; setState('idle'); };
setTimeout(() => { if (state === 'speaking') setState('idle'); }, 30000);
synth.speak(utt);
}
// ─── Persistence ─────────────────────────────────────────────
const STORAGE_KEY = 'drivecoding_messages';
function saveMessages() {
const msgs = [];
transcript.querySelectorAll('.msg').forEach(el => {
msgs.push({
role: el.classList.contains('user') ? 'user' : 'assistant',
html: el.innerHTML,
});
});
try { localStorage.setItem(STORAGE_KEY, JSON.stringify(msgs)); } catch(e) {}
}
function loadMessages() {
try {
const saved = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
saved.forEach(m => {
const div = document.createElement('div');
div.className = `msg ${m.role}`;
div.innerHTML = m.html;
transcript.appendChild(div);
});
if (saved.length) transcript.scrollTop = transcript.scrollHeight;
} catch(e) {}
}
// ─── UI helpers ──────────────────────────────────────────────
function addMessage(text, role) {
const div = document.createElement('div');
div.className = `msg ${role}`;
const time = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
div.innerHTML = `${escapeHtml(text)}<div class="meta">${time}</div>`;
transcript.appendChild(div);
transcript.scrollTop = transcript.scrollHeight;
saveMessages();
}
function escapeHtml(s) {
const d = document.createElement('div');
d.textContent = s;
return d.innerHTML;
}
function setStatus(text) {
statusText.textContent = text;
}
// ─── Event Handlers ──────────────────────────────────────────
loopSpeed.addEventListener('input', () => { loopVal.textContent = loopSpeed.value + 's'; });
function sendTextInput() {
const text = textInput.value.trim();
if (!text || state === 'waiting' || state === 'sending') return;
textInput.value = '';
sendToInbox(text);
}
sendBtn.addEventListener('click', sendTextInput);
textInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') { e.preventDefault(); sendTextInput(); }
});
// Wake lock
async function requestWakeLock() {
try { if ('wakeLock' in navigator) await navigator.wakeLock.request('screen'); } catch(e) {}
}
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') requestWakeLock();
});
requestWakeLock();
// ─── Init ────────────────────────────────────────────────────
loadMessages();
setState('idle');
</script>
</body>
</html>