forked from openai/openai-realtime-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsolePage.tsx
454 lines (409 loc) · 14 KB
/
ConsolePage.tsx
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
/**
* Running a local relay server will allow you to hide your API key
* and run custom logic on the server
*
* Set the local relay server address to:
* REACT_APP_LOCAL_RELAY_SERVER_URL=http://localhost:8081
*
* This will also require you to set OPENAI_API_KEY= in a `.env` file
* You can run it with `npm run relay`, in parallel with `npm start`
*/
const LOCAL_RELAY_SERVER_URL: string =
process.env.REACT_APP_LOCAL_RELAY_SERVER_URL || '';
import { useEffect, useRef, useCallback, useState } from 'react';
import { RealtimeClient } from '@openai/realtime-api-beta';
import { ItemType } from '@openai/realtime-api-beta/dist/lib/client.js';
import { WavRecorder, WavStreamPlayer } from '../lib/wavtools/index.js';
import { instructions } from '../utils/conversation_config.js';
import { WavRenderer } from '../utils/wav_renderer';
import { X, Edit, Zap, ArrowUp, ArrowDown } from 'react-feather';
import { Button } from '../components/button/Button';
import { Toggle } from '../components/toggle/Toggle';
import './ConsolePage.scss';
import { isJsxOpeningLikeElement } from 'typescript';
/**
* Type for all event logs
*/
interface RealtimeEvent {
time: string;
source: 'client' | 'server';
count?: number;
event: { [key: string]: any };
}
export function ConsolePage() {
/**
* Ask user for API Key
* If we're using the local relay server, we don't need this
*/
const apiKey = LOCAL_RELAY_SERVER_URL
? ''
: localStorage.getItem('tmp::voice_api_key') ||
prompt('OpenAI API Key') ||
'';
if (apiKey !== '') {
localStorage.setItem('tmp::voice_api_key', apiKey);
}
/**
* Instantiate:
* - WavRecorder (speech input)
* - WavStreamPlayer (speech output)
* - RealtimeClient (API client)
*/
const wavRecorderRef = useRef<WavRecorder>(
new WavRecorder({ sampleRate: 24000 })
);
const wavStreamPlayerRef = useRef<WavStreamPlayer>(
new WavStreamPlayer({ sampleRate: 24000 })
);
const clientRef = useRef<RealtimeClient>(
new RealtimeClient(
LOCAL_RELAY_SERVER_URL
? { url: LOCAL_RELAY_SERVER_URL }
: {
apiKey: apiKey,
dangerouslyAllowAPIKeyInBrowser: true,
}
)
);
/**
* References for
* - Rendering audio visualization (canvas)
* - Autoscrolling event logs
* - Timing delta for event log displays
*/
const clientCanvasRef = useRef<HTMLCanvasElement>(null);
const serverCanvasRef = useRef<HTMLCanvasElement>(null);
const startTimeRef = useRef<string>(new Date().toISOString());
/**
* All of our variables for displaying application state
* - items are all conversation items (dialog)
* - realtimeEvents are event logs, which can be expanded
* - coords, marker are for get_weather() function
*/
const [items, setItems] = useState<ItemType[]>([]);
const [isConnected, setIsConnected] = useState(false);
const [canPushToTalk, setCanPushToTalk] = useState(true);
const [isRecording, setIsRecording] = useState(false);
const [showConversations, setShowConversations] = useState(false);
const [circleScale, setCircleScale] = useState(1);
const currentScaleRef = useRef(1);
/**
* Utility for formatting the timing of logs
*/
const formatTime = useCallback((timestamp: string) => {
const startTime = startTimeRef.current;
const t0 = new Date(startTime).valueOf();
const t1 = new Date(timestamp).valueOf();
const delta = t1 - t0;
const hs = Math.floor(delta / 10) % 100;
const s = Math.floor(delta / 1000) % 60;
const m = Math.floor(delta / 60_000) % 60;
const pad = (n: number) => {
let s = n + '';
while (s.length < 2) {
s = '0' + s;
}
return s;
};
return `${pad(m)}:${pad(s)}.${pad(hs)}`;
}, []);
/**
* When you click the API key
*/
const resetAPIKey = useCallback(() => {
const apiKey = prompt('OpenAI API Key');
if (apiKey !== null) {
localStorage.clear();
localStorage.setItem('tmp::voice_api_key', apiKey);
window.location.reload();
}
}, []);
/**
* Connect to conversation:
* WavRecorder taks speech input, WavStreamPlayer output, client is API client
*/
const connectConversation = useCallback(async () => {
const client = clientRef.current;
const wavRecorder = wavRecorderRef.current;
const wavStreamPlayer = wavStreamPlayerRef.current;
// Set state variables
startTimeRef.current = new Date().toISOString();
setIsConnected(true);
setItems(client.conversation.getItems());
// Connect to microphone
await wavRecorder.begin();
// Connect to audio output
await wavStreamPlayer.connect();
// Connect to realtime API
await client.connect();
client.sendUserMessageContent([
{
type: `input_text`,
text: `Hello!`,
// text: `For testing purposes, I want you to list ten car brands. Number each item, e.g. "one (or whatever number you are one): the item name".`
},
]);
if (client.getTurnDetectionType() === 'server_vad') {
await wavRecorder.record((data) => client.appendInputAudio(data.mono));
}
}, []);
/**
* Disconnect and reset conversation state
*/
const disconnectConversation = useCallback(async () => {
setIsConnected(false);
setItems([]);
const client = clientRef.current;
client.disconnect();
const wavRecorder = wavRecorderRef.current;
await wavRecorder.end();
const wavStreamPlayer = wavStreamPlayerRef.current;
await wavStreamPlayer.interrupt();
}, []);
const deleteConversationItem = useCallback(async (id: string) => {
const client = clientRef.current;
client.deleteItem(id);
}, []);
/**
* In push-to-talk mode, start recording
* .appendInputAudio() for each sample
*/
const startRecording = async () => {
setIsRecording(true);
const client = clientRef.current;
const wavRecorder = wavRecorderRef.current;
const wavStreamPlayer = wavStreamPlayerRef.current;
const trackSampleOffset = await wavStreamPlayer.interrupt();
if (trackSampleOffset?.trackId) {
const { trackId, offset } = trackSampleOffset;
await client.cancelResponse(trackId, offset);
}
await wavRecorder.record((data) => client.appendInputAudio(data.mono));
};
/**
* In push-to-talk mode, stop recording
*/
const stopRecording = async () => {
setIsRecording(false);
const client = clientRef.current;
const wavRecorder = wavRecorderRef.current;
await wavRecorder.pause();
client.createResponse();
};
/**
* Switch between Manual <> VAD mode for communication
*/
const changeTurnEndType = async (value: string) => {
const client = clientRef.current;
const wavRecorder = wavRecorderRef.current;
if (value === 'none' && wavRecorder.getStatus() === 'recording') {
await wavRecorder.pause();
}
client.updateSession({
turn_detection: value === 'none' ? null : { type: 'server_vad' },
});
if (value === 'server_vad' && client.isConnected()) {
await wavRecorder.record((data) => client.appendInputAudio(data.mono));
}
setCanPushToTalk(value === 'none');
};
/**
* Auto-scroll the conversation logs
*/
useEffect(() => {
const conversationEls = [].slice.call(
document.body.querySelectorAll('[data-conversation-content]')
);
for (const el of conversationEls) {
const conversationEl = el as HTMLDivElement;
conversationEl.scrollTop = conversationEl.scrollHeight;
}
}, [items]);
/**
* Set up render loops for the visualization canvas
*/
useEffect(() => {
let isLoaded = true;
const wavRecorder = wavRecorderRef.current;
const wavStreamPlayer = wavStreamPlayerRef.current;
const render = () => {
if (isLoaded) {
// Get input amplitude (user speaking)
const inputResult = wavRecorder?.recording && wavRecorder.getStatus() === 'recording'
? wavRecorder.getFrequencies('voice')
: { values: new Float32Array([0]) };
// Get output amplitude (assistant speaking)
const outputResult = wavStreamPlayer?.analyser
? wavStreamPlayer.getFrequencies('voice')
: { values: new Float32Array([0]) };
// Calculate the average amplitude from both input and output
const inputAmplitude =
inputResult.values.reduce((sum, value) => sum + value, 0) / inputResult.values.length;
const outputAmplitude =
outputResult.values.reduce((sum, value) => sum + value, 0) / outputResult.values.length;
// Use the larger of the two amplitudes
const amplitude = Math.max(inputAmplitude, outputAmplitude);
// Calculate target scale
const targetScale = 1 + Math.min(amplitude * 2, 0.5);
// Smooth interpolation between current and target scale
const smoothingFactor = 0.15; // Adjust this value to control smoothing (0-1)
currentScaleRef.current += (targetScale - currentScaleRef.current) * smoothingFactor;
// Update the circle scale
setCircleScale(currentScaleRef.current);
requestAnimationFrame(render);
}
};
render();
return () => {
isLoaded = false;
};
}, []);
/**
* Core RealtimeClient and audio capture setup
* Set all of our instructions, tools, events and more
*/
useEffect(() => {
// Get refs
const wavStreamPlayer = wavStreamPlayerRef.current;
const client = clientRef.current;
// Set instructions
client.updateSession({ instructions: instructions });
// Set voice
client.updateSession({ voice: 'echo' });
// Set transcription, otherwise we don't get user transcriptions back
client.updateSession({ input_audio_transcription: { model: 'whisper-1' } });
// Add tools
client.addTool(
{
name: 'get_weather',
description:
'Retrieves the weather for a given lat, lng coordinate pair. Specify a label for the location.',
parameters: {
type: 'object',
properties: {
lat: {
type: 'number',
description: 'Latitude',
},
lng: {
type: 'number',
description: 'Longitude',
},
location: {
type: 'string',
description: 'Name of the location',
},
},
required: ['lat', 'lng', 'location'],
},
},
async ({ lat, lng, location }: { [key: string]: any }) => {
const result = await fetch(
`https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lng}¤t=temperature_2m,wind_speed_10m`
);
const json = await result.json();
const temperature = {
value: json.current.temperature_2m as number,
units: json.current_units.temperature_2m as string,
};
const wind_speed = {
value: json.current.wind_speed_10m as number,
units: json.current_units.wind_speed_10m as string,
};
return json;
}
);
// handle realtime events from client + server for event logging
client.on('error', (event: any) => console.error(event));
client.on('conversation.interrupted', async () => {
const trackSampleOffset = await wavStreamPlayer.interrupt();
if (trackSampleOffset?.trackId) {
const { trackId, offset } = trackSampleOffset;
await client.cancelResponse(trackId, offset);
}
});
client.on('conversation.updated', async ({ item, delta }: any) => {
try {
const items = client.conversation.getItems();
if (delta?.audio && items.find(i => i.id === item.id)) {
await wavStreamPlayer.add16BitPCM(delta.audio, item.id);
}
const cleanedItems = items.map(item => {
if (item.formatted?.file) {
const { file, ...rest } = item.formatted;
return { ...item, formatted: rest };
}
return item;
});
setItems(cleanedItems);
} catch (error) {
console.warn('Error processing conversation update:', error);
}
});
setItems(client.conversation.getItems());
return () => {
// cleanup; resets to defaults
client.reset();
};
}, []);
/**
* Render the application
*/
return (
<div data-component="ConsolePage">
<div className="content-top">
<div className="content-title">
<img src="/openai-logomark.svg" />
<span>live love</span>
</div>
<div className="content-api-key">
{!LOCAL_RELAY_SERVER_URL && (
<Button
icon={Edit}
iconPosition="end"
buttonStyle="flush"
label={`api key: ${apiKey.slice(0, 3)}...`}
onClick={() => resetAPIKey()}
/>
)}
</div>
</div>
<div className="content-main">
<div className="content-logs">
{/* Remove content-actions from here */}
</div>
</div>
{/* Add content-actions here at root level */}
<div className="content-actions">
<Toggle
defaultValue={false}
labels={['push', 'live']}
values={['none', 'server_vad']}
onChange={(_, value) => changeTurnEndType(value)}
/>
<div className="spacer" />
{isConnected && canPushToTalk && (
<Button
label={isRecording ? 'release to send' : 'push to talk'}
buttonStyle={isRecording ? 'alert' : 'regular'}
disabled={!isConnected || !canPushToTalk}
onMouseDown={startRecording}
onMouseUp={stopRecording}
/>
)}
<div className="spacer" />
<Button
label={isConnected ? 'disconnect' : 'connect'}
iconPosition={isConnected ? 'end' : 'start'}
icon={isConnected ? X : Zap}
buttonStyle={isConnected ? 'regular' : 'action'}
onClick={isConnected ? disconnectConversation : connectConversation}
/>
</div>
<div
className="center-circle"
style={{ transform: `translate(-50%, -50%) scale(${circleScale})` }}
></div>
</div>
);
}