-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathagent_app.html
More file actions
375 lines (320 loc) · 12.7 KB
/
agent_app.html
File metadata and controls
375 lines (320 loc) · 12.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Agent</title>
<style>
body {
margin: 0;
padding: 0;
font-family: system-ui, -apple-system, sans-serif;
background: #000;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
}
.container {
text-align: center;
padding: 1rem;
}
.status {
font-size: 0.9rem;
margin-bottom: 0.25rem;
}
.connecting {
opacity: 0.7;
}
.connected {
color: #10b981;
}
.error {
color: #ef4444;
}
.controls {
margin-top: 0.5rem;
display: flex;
align-items: center;
gap: 1rem;
justify-content: center;
height: 40px;
}
.volume-control {
display: flex;
align-items: center;
gap: 0.25rem;
}
.volume-slider {
width: 60px;
height: 3px;
background: #333;
border-radius: 2px;
outline: none;
appearance: none;
}
.volume-slider::-webkit-slider-thumb {
appearance: none;
width: 12px;
height: 12px;
background: #10b981;
border-radius: 50%;
cursor: pointer;
}
.volume-slider::-moz-range-thumb {
width: 12px;
height: 12px;
background: #10b981;
border-radius: 50%;
cursor: pointer;
border: none;
}
.audiometers {
display: flex;
gap: 0.5rem;
align-items: center;
}
.meter {
display: flex;
align-items: center;
gap: 0.25rem;
}
.meter-label {
font-size: 0.6rem;
opacity: 0.7;
min-width: 20px;
}
.meter-bar {
width: 30px;
height: 6px;
background: #333;
border-radius: 3px;
position: relative;
overflow: hidden;
}
.meter-fill {
position: absolute;
left: 0;
top: 0;
height: 100%;
background: linear-gradient(to right, #10b981, #22c55e, #eab308, #ef4444);
transition: width 0.1s ease;
border-radius: 3px;
}
.volume-label {
font-size: 0.6rem;
opacity: 0.7;
}
.volume-value {
font-size: 0.6rem;
opacity: 0.7;
min-width: 25px;
}
</style>
</head>
<body>
<div class="container">
<div id="status" class="status connecting">Connecting to agent...</div>
<div id="controls" class="controls" style="display: none;">
<div class="volume-control">
<span class="volume-label">Volume:</span>
<input type="range" id="volumeSlider" class="volume-slider" min="0" max="1" step="0.01" value="0.5">
<span id="volumeValue" class="volume-value">50%</span>
</div>
<div class="audiometers">
<div class="meter">
<div class="meter-label">Input</div>
<div class="meter-bar">
<div id="inputMeter" class="meter-fill" style="width: 0%;"></div>
</div>
</div>
<div class="meter">
<div class="meter-label">Output</div>
<div class="meter-bar">
<div id="outputMeter" class="meter-fill" style="width: 0%;"></div>
</div>
</div>
</div>
</div>
</div>
<script type="module">
import { tools } from "./workerd/tool-defs.ts"
import { Conversation } from '@elevenlabs/client'
const statusEl = document.getElementById('status')
const controlsEl = document.getElementById('controls')
const volumeSlider = document.getElementById('volumeSlider')
const volumeValue = document.getElementById('volumeValue')
const inputMeter = document.getElementById('inputMeter')
const outputMeter = document.getElementById('outputMeter')
import * as Comlink from "comlink";
const wrapper = Comlink.wrap(Comlink.windowEndpoint(window.parent, window))
console.log('wrapper', wrapper)
window.onmessage = (event) => {
console.log('onmessage', event)
}
// Wait a bit for parent to be ready
setTimeout(async () => {
try {
console.log('Calling wrapper.add...')
const result = await wrapper.add(1, 3)
console.log('result', result)
} catch (error) {
console.error('error', error)
}
}, 1000)
console.log('tools', tools)
let conversation = null
let volumePollingInterval = null
// Update volume display
function updateVolumeDisplay(value) {
volumeValue.textContent = Math.round(value * 100) + '%'
}
// Update meter display
function updateMeter(element, volume) {
const percentage = Math.min(volume * 100, 100)
element.style.width = percentage + '%'
}
// Volume slider handler
volumeSlider.addEventListener('input', async (e) => {
const volume = parseFloat(e.target.value)
updateVolumeDisplay(volume)
if (conversation) {
try {
await conversation.setVolume({ volume: volume })
console.log('Volume set to:', volume)
} catch (error) {
console.error('Failed to set volume:', error)
}
}
})
// Start volume polling
function startVolumePolling() {
if (volumePollingInterval) {
clearInterval(volumePollingInterval)
}
volumePollingInterval = setInterval(async () => {
if (conversation) {
try {
const inputVolume = await conversation.getInputVolume()
const outputVolume = await conversation.getOutputVolume()
updateMeter(inputMeter, inputVolume)
updateMeter(outputMeter, outputVolume)
} catch (error) {
console.error('Failed to get volume levels:', error)
}
}
}, 100) // Poll every 100ms for smooth animation
}
// Stop volume polling
function stopVolumePolling() {
if (volumePollingInterval) {
clearInterval(volumePollingInterval)
volumePollingInterval = null
}
}
// Get agent ID and token from query parameters
function getAgentIdFromUrl() {
const urlParams = new URLSearchParams(window.location.search)
return urlParams.get('agent_id') || ''
}
// function getTokenFromUrl() {
// const urlParams = new URLSearchParams(window.location.search)
// return urlParams.get('token') || ''
// }
// conversation.sendContextualUpdate(
// "User navigated to another page. Consider it for next response, but don't react to this contextual update."
// );
async function startAgent() {
try {
const agentId = getAgentIdFromUrl()
// const token = getTokenFromUrl()
if (!agentId) {
statusEl.textContent = 'No agent ID provided in URL'
statusEl.className = 'status error'
return
}
// if (!token) {
// statusEl.textContent = 'No Eleven Labs token provided'
// statusEl.className = 'status error'
// return
// }
statusEl.textContent = `Starting conversation with agent ${agentId}...`
const conv = await Conversation.startSession({
agentId: agentId,
// apiKey: token,
// onConnect - handler called when the conversation websocket connection is established.
// onDisconnect - handler called when the conversation websocket connection is ended.
// onMessage - handler called when a new text message is received. These can be tentative or final transcriptions of user voice, replies produced by LLM. Primarily used for handling conversation transcription.
// onError - handler called when an error is encountered.
// onStatusChange - handler called whenever connection status changes. Can be connected, connecting and disconnected (initial).
// onModeChange - handler called when a status changes, eg. agent switches from speaking to listening, or the other way around.
onConnect: () => {
console.log('onConnect')
},
clientTools: {
...Object.fromEntries(Object.entries(tools).map(([key, tool]) => {
if (!tool.execute) {
tool.execute = async (args) => {
console.log('TODO: tool.execute', args)
return ``
}
}
return[key, tool.execute]
}))
// listPlatformTools: async ({ context }) => {
// console.log('listPlatformTools: ', { context })
// return ``
// }
// executePlatformTool: async (args) => {
// // console.log('message tool `log`', message)
// // return `Logged: ${message}`
// }
},
onDisconnect: () => {
console.log('onDisconnect')
stopVolumePolling()
controlsEl.style.display = 'none'
},
onMessage: (message) => {
console.log('onMessage', message)
// Send message to parent frame
window.parent.postMessage({
type: 'agent-message',
data: message
}, '*')
},
onError: (error) => {
console.log('onError', error)
},
onStatusChange: (status) => {
console.log('onStatusChange', status)
},
onModeChange: (mode) => {
console.log('onModeChange', mode)
}
})
conversation = conv
statusEl.textContent = 'Voice Agent connected'
statusEl.className = 'status connected'
// Show controls and start volume polling
controlsEl.style.display = 'flex'
startVolumePolling()
// Set initial volume
await conversation.setVolume({ volume: 0.5 })
// Auto-cleanup on page unload
window.addEventListener('beforeunload', async () => {
stopVolumePolling()
await conv.endSession()
})
} catch (error) {
console.error('Failed to start agent:', error)
statusEl.textContent = 'Failed to connect to agent'
statusEl.className = 'status error'
}
}
// Start immediately when loaded
startAgent()
</script>
</body>
</html>