Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
185 lines (158 loc) · 5.2 KB
/
Copy pathapp.js
File metadata and controls
185 lines (158 loc) · 5.2 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
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
const runTextBtn = document.getElementById('runTextBtn');
const transcriptEl = document.getElementById('transcript');
const statusEl = document.getElementById('status');
const sourceChip = document.getElementById('sourceChip');
const confidenceChip = document.getElementById('confidenceChip');
const timeChip = document.getElementById('timeChip');
const assistantResponseEl = document.getElementById('assistantResponse');
const callsEl = document.getElementById('calls');
const actionsEl = document.getElementById('actions');
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
let recognition = null;
function setStatus(text) {
statusEl.textContent = text;
}
async function parseJsonSafe(response) {
const text = await response.text();
if (!text) return {};
try {
return JSON.parse(text);
} catch {
return { error: text };
}
}
async function postJson(url, payload) {
let response;
try {
response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
} catch (err) {
throw new Error('Cannot reach backend API. Make sure you opened http://127.0.0.1:8080 and server is running.');
}
const data = await parseJsonSafe(response);
if (!response.ok || !data.ok) {
throw new Error(data.error || `Request failed (${response.status})`);
}
return data;
}
function renderActions(actions) {
actionsEl.innerHTML = '';
if (!actions || actions.length === 0) {
const li = document.createElement('li');
li.textContent = 'No actions returned.';
actionsEl.appendChild(li);
return;
}
actions.forEach(action => {
const li = document.createElement('li');
li.textContent = action;
actionsEl.appendChild(li);
});
}
function renderResult(data) {
sourceChip.textContent = `source: ${data.source || '-'}`;
confidenceChip.textContent = `confidence: ${Number(data.confidence || 0).toFixed(3)}`;
timeChip.textContent = `time: ${Math.round(data.total_time_ms || 0)}ms`;
assistantResponseEl.textContent = data.assistant_response || 'No response generated.';
callsEl.textContent = JSON.stringify(data.function_calls || [], null, 2);
renderActions(data.actions || []);
}
async function runWorkflow() {
const transcript = transcriptEl.value.trim();
if (!transcript) {
setStatus('Please provide a transcript first.');
return;
}
setStatus('Routing transcript...');
runTextBtn.disabled = true;
try {
const data = await postJson('/api/route', { transcript });
renderResult(data);
setStatus(data.assistant_response || 'Done.');
} catch (err) {
setStatus(`Error: ${err.message}`);
} finally {
runTextBtn.disabled = false;
}
}
function initSpeech() {
if (!SpeechRecognition) {
setStatus('Speech recognition not available in this browser. Use text input + Run Text.');
startBtn.disabled = true;
stopBtn.disabled = true;
return;
}
recognition = new SpeechRecognition();
recognition.lang = 'en-US';
recognition.continuous = false;
recognition.interimResults = true;
recognition.onstart = () => {
setStatus('Listening...');
startBtn.disabled = true;
stopBtn.disabled = false;
};
recognition.onresult = (event) => {
let finalText = '';
let interimText = '';
for (let i = event.resultIndex; i < event.results.length; i += 1) {
const text = event.results[i][0].transcript;
if (event.results[i].isFinal) {
finalText += text;
} else {
interimText += text;
}
}
const merged = (finalText || interimText).trim();
if (merged) transcriptEl.value = merged;
};
recognition.onerror = (event) => {
setStatus(`Speech error: ${event.error}`);
};
recognition.onend = () => {
startBtn.disabled = false;
stopBtn.disabled = true;
setStatus('Stopped listening. Click Run Text to route.');
};
}
async function runStartupChecks() {
if (window.location.protocol === 'file:') {
setStatus('You are on file://. Run python voice_web_server.py and open http://127.0.0.1:8080');
return;
}
try {
const response = await fetch('/api/health');
const data = await parseJsonSafe(response);
if (!response.ok || !data.ok) {
setStatus('Backend health check failed. Is voice_web_server.py running?');
return;
}
if (!data.cactus_bindings) {
setStatus('Backend up, but cactus Python bindings not available. Re-run cactus setup and env.');
return;
}
if (!data.whisper_weights_exists || !data.whisper_config_exists) {
setStatus(`Backend up. Whisper missing at ${data.whisper_weights_path}. Download whisper-small weights.`);
return;
}
setStatus('Ready. Mic + backend checks look good.');
} catch {
setStatus('Cannot reach backend. Start server with: python voice_web_server.py');
}
}
startBtn.addEventListener('click', () => {
if (!recognition) return;
transcriptEl.value = '';
recognition.start();
});
stopBtn.addEventListener('click', () => {
if (!recognition) return;
recognition.stop();
});
runTextBtn.addEventListener('click', runWorkflow);
initSpeech();
runStartupChecks();