1- type OpenAIRealtimeVoiceLifecyclePhase = "connecting" | "ready" | "retry-wait" | "terminal" ;
1+ type OpenAIRealtimeVoiceLifecyclePhase =
2+ | "idle"
3+ | "connecting"
4+ | "ready"
5+ | "retry-wait"
6+ | "terminal" ;
27
38type OpenAIRealtimeVoiceTerminalOutcome = "completed" | "error" ;
49
@@ -7,20 +12,29 @@ export type OpenAIRealtimeVoiceConnection = Readonly<{
712 signal : AbortSignal ;
813} > ;
914
10- type OpenAIRealtimeVoiceLifecycleState = {
15+ type OpenAIRealtimeVoiceIdleState = {
16+ phase : "idle" | "terminal" ;
17+ terminalOutcome ?: "completed" ;
18+ } ;
19+
20+ type OpenAIRealtimeVoiceConnectionState = {
1121 connection : OpenAIRealtimeVoiceConnection ;
1222 controller : AbortController ;
13- phase : OpenAIRealtimeVoiceLifecyclePhase ;
23+ phase : Exclude < OpenAIRealtimeVoiceLifecyclePhase , "idle" > ;
1424 retryAttempts : number ;
1525 terminalOutcome ?: OpenAIRealtimeVoiceTerminalOutcome ;
1626 terminalNotified : boolean ;
1727} ;
1828
1929export class OpenAIRealtimeVoiceLifecycle {
20- private state : OpenAIRealtimeVoiceLifecycleState | undefined ;
30+ private state : OpenAIRealtimeVoiceIdleState | OpenAIRealtimeVoiceConnectionState = {
31+ phase : "idle" ,
32+ } ;
2133
2234 connect ( ) : OpenAIRealtimeVoiceConnection {
23- this . state ?. controller . abort ( new Error ( "OpenAI realtime voice connection replaced" ) ) ;
35+ if ( "controller" in this . state ) {
36+ this . state . controller . abort ( new Error ( "OpenAI realtime voice connection replaced" ) ) ;
37+ }
2438 const controller = new AbortController ( ) ;
2539 const connection = this . createConnection ( controller ) ;
2640 this . state = {
@@ -72,9 +86,16 @@ export class OpenAIRealtimeVoiceLifecycle {
7286
7387 cancel ( ) : boolean {
7488 const state = this . state ;
75- if ( ! state || state . terminalOutcome ) {
89+ if ( state . phase === "terminal" ) {
7690 return false ;
7791 }
92+ if ( state . phase === "idle" ) {
93+ this . state = {
94+ phase : "terminal" ,
95+ terminalOutcome : "completed" ,
96+ } ;
97+ return true ;
98+ }
7899 state . phase = "terminal" ;
79100 state . terminalOutcome = "completed" ;
80101 state . controller . abort ( new Error ( "OpenAI realtime voice session canceled" ) ) ;
@@ -125,8 +146,8 @@ export class OpenAIRealtimeVoiceLifecycle {
125146 return this . state ?. phase === "ready" ;
126147 }
127148
128- phase ( ) : OpenAIRealtimeVoiceLifecyclePhase | undefined {
129- return this . state ? .phase ;
149+ phase ( ) : OpenAIRealtimeVoiceLifecyclePhase {
150+ return this . state . phase ;
130151 }
131152
132153 terminalOutcome (
@@ -141,7 +162,9 @@ export class OpenAIRealtimeVoiceLifecycle {
141162
142163 private currentState (
143164 connection : OpenAIRealtimeVoiceConnection ,
144- ) : OpenAIRealtimeVoiceLifecycleState | undefined {
145- return this . state ?. connection . id === connection . id ? this . state : undefined ;
165+ ) : OpenAIRealtimeVoiceConnectionState | undefined {
166+ return "connection" in this . state && this . state . connection . id === connection . id
167+ ? this . state
168+ : undefined ;
146169 }
147170}
0 commit comments