Skip to content

Commit 42f1e8d

Browse files
committed
fix(openai): terminalize preconnect lifecycle
1 parent 0f31254 commit 42f1e8d

2 files changed

Lines changed: 47 additions & 10 deletions

File tree

‎extensions/openai/realtime-voice-lifecycle.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ import { describe, expect, it } from "vitest";
22
import { OpenAIRealtimeVoiceLifecycle } from "./realtime-voice-lifecycle.js";
33

44
describe("OpenAIRealtimeVoiceLifecycle", () => {
5+
it("terminalizes preconnect cancellation until an explicit fresh connection", () => {
6+
const lifecycle = new OpenAIRealtimeVoiceLifecycle();
7+
8+
expect(lifecycle.phase()).toBe("idle");
9+
expect(lifecycle.cancel()).toBe(true);
10+
expect(lifecycle.phase()).toBe("terminal");
11+
expect(lifecycle.cancel()).toBe(false);
12+
13+
const connection = lifecycle.connect();
14+
expect(lifecycle.phase()).toBe("connecting");
15+
expect(lifecycle.ready(connection)).toBe(true);
16+
expect(lifecycle.phase()).toBe("ready");
17+
});
18+
519
it("moves a connection from connecting to ready", () => {
620
const lifecycle = new OpenAIRealtimeVoiceLifecycle();
721
const connection = lifecycle.connect();

‎extensions/openai/realtime-voice-lifecycle.ts‎

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
type OpenAIRealtimeVoiceLifecyclePhase = "connecting" | "ready" | "retry-wait" | "terminal";
1+
type OpenAIRealtimeVoiceLifecyclePhase =
2+
| "idle"
3+
| "connecting"
4+
| "ready"
5+
| "retry-wait"
6+
| "terminal";
27

38
type 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

1929
export 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

Comments
 (0)