Repository navigation
examples: handle Live replies and session finalization #3060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b7ee161
d79d8ea
edcda56
9e5dbc7
1cb75ad
a2a618e
f474293
beb1b6d
e506abd
311351c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import { createInterface } from 'node:readline'; | ||
| import type { Interface } from 'node:readline'; | ||
| import OpenAI from 'openai'; | ||
| import { prepareTranscriptDemo, runTranscriptDemo } from './transcript-demo'; | ||
| import { createPCMPlayback } from './transcript-playback'; | ||
|
|
@@ -6,28 +8,59 @@ async function main(): Promise<void> { | |
| const playback = process.argv.includes('--no-audio') | ||
| ? undefined | ||
| : { user: createPCMPlayback(), assistant: createPCMPlayback() }; | ||
| const finish = new AbortController(); | ||
| const cancel = new AbortController(); | ||
| let input: Interface | undefined; | ||
| const stop = () => finish.abort(); | ||
| const interrupt = () => { | ||
| cancel.abort(); | ||
| void playback?.user.close(); | ||
| void playback?.assistant.close(); | ||
| }; | ||
| try { | ||
| const client = new OpenAI({ logLevel: 'off' }); | ||
| console.log('All voices are AI-generated. Preparing both TTS clips before connecting to Live…'); | ||
| const audio = await prepareTranscriptDemo(client); | ||
| console.log(playback ? 'Playing both voices through playAudio (ffplay).' : 'Audio playback disabled.'); | ||
| console.log(playback ? 'Playing both voices through ffplay.' : 'Audio playback disabled.'); | ||
| console.log('TTS ready. Starting a picnic conversation, then interrupting with a breakfast request.'); | ||
| process.once('SIGINT', interrupt); | ||
| if (process.stdin.isTTY) { | ||
| input = createInterface({ input: process.stdin, output: process.stdout }); | ||
| input.once('line', stop); | ||
| input.once('close', stop); | ||
| input.once('SIGINT', interrupt); | ||
| console.log('Press Enter to finish capturing the transcript and close the session.'); | ||
| } else { | ||
| console.log('Capturing for up to 45 seconds, then closing the session.'); | ||
| } | ||
| const result = await runTranscriptDemo(client, audio, { | ||
| finishSignal: input ? finish.signal : AbortSignal.timeout(45_000), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Preserve automatic completion for the default terminal invocation. Running the documented command in a terminal previously captured a conversation and returned without keyboard input. It now selects an untimed |
||
| signal: cancel.signal, | ||
| interrupt: true, | ||
| onInputAudio: (pcm) => playback?.user.write(pcm), | ||
| onOutputAudio: (pcm) => playback?.assistant.write(pcm), | ||
| onClosed: ({ segment, reason }) => console.log(`[${segment.speaker}; ${reason}] ${segment.text}`), | ||
| }); | ||
| await Promise.all([playback?.user.finish(), playback?.assistant.finish()]); | ||
| cancel.signal.throwIfAborted(); | ||
| console.log( | ||
| `${result.rawTranscriptEvents} raw transcript fragments → ${result.updateCount} bubble updates → ${result.segments.length} final bubbles.`, | ||
| ); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : 'The Live example failed'; | ||
| const key = process.env['OPENAI_API_KEY']; | ||
| console.error(key ? message.split(key).join('[REDACTED]') : message); | ||
| process.exitCode = 1; | ||
| if (cancel.signal.aborted) { | ||
| process.exitCode = 130; | ||
| } else { | ||
| const message = error instanceof Error ? error.message : 'The Live example failed'; | ||
| const key = process.env['OPENAI_API_KEY']; | ||
| console.error(key ? message.split(key).join('[REDACTED]') : message); | ||
| process.exitCode = 1; | ||
| } | ||
| } finally { | ||
| process.removeListener('SIGINT', interrupt); | ||
| input?.removeListener('line', stop); | ||
| input?.removeListener('close', stop); | ||
| input?.removeListener('SIGINT', interrupt); | ||
| input?.close(); | ||
| await Promise.all([playback?.user.close(), playback?.assistant.close()]); | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.