|
| 1 | +import type { |
| 2 | + RealtimeTalkJsonPcmWebSocketSessionResult, |
| 3 | + RealtimeTalkTransportStartResult, |
| 4 | +} from "./realtime-talk-shared.ts"; |
| 5 | + |
| 6 | +const GOOGLE_LIVE_WEBSOCKET_HOST = "generativelanguage.googleapis.com"; |
| 7 | +const GOOGLE_LIVE_WEBSOCKET_PATH = |
| 8 | + /^\/ws\/google\.ai\.generativelanguage\.v[0-9a-z]+\.GenerativeService\.BidiGenerateContent(?:Constrained)?$/; |
| 9 | +export const GOOGLE_LIVE_SETUP_TIMEOUT_MS = 30_000; |
| 10 | + |
| 11 | +export function buildGoogleLiveUrl(session: RealtimeTalkJsonPcmWebSocketSessionResult): string { |
| 12 | + let url: URL; |
| 13 | + try { |
| 14 | + url = new URL(session.websocketUrl); |
| 15 | + } catch { |
| 16 | + throw new Error("Invalid Google Live WebSocket URL"); |
| 17 | + } |
| 18 | + if (url.protocol !== "wss:") { |
| 19 | + throw new Error("Google Live WebSocket URL must use wss://"); |
| 20 | + } |
| 21 | + if (url.hostname.toLowerCase() !== GOOGLE_LIVE_WEBSOCKET_HOST) { |
| 22 | + throw new Error("Untrusted Google Live WebSocket host"); |
| 23 | + } |
| 24 | + if (url.username || url.password) { |
| 25 | + throw new Error("Google Live WebSocket URL must not include credentials"); |
| 26 | + } |
| 27 | + if (!GOOGLE_LIVE_WEBSOCKET_PATH.test(url.pathname)) { |
| 28 | + throw new Error("Untrusted Google Live WebSocket path"); |
| 29 | + } |
| 30 | + url.search = ""; |
| 31 | + url.searchParams.set("access_token", session.clientSecret); |
| 32 | + return url.toString(); |
| 33 | +} |
| 34 | + |
| 35 | +type GoogleLiveConnectionState = |
| 36 | + | "idle" |
| 37 | + | "connecting" |
| 38 | + | "ready" |
| 39 | + | "active" |
| 40 | + | "cancelled" |
| 41 | + | "failed"; |
| 42 | + |
| 43 | +type StartupWaiter = { |
| 44 | + resolve: (result: RealtimeTalkTransportStartResult) => void; |
| 45 | + reject: (error: Error) => void; |
| 46 | +}; |
| 47 | + |
| 48 | +export class GoogleLiveConnectionLifecycle { |
| 49 | + private state: GoogleLiveConnectionState = "idle"; |
| 50 | + private socket: WebSocket | null = null; |
| 51 | + private waiter: StartupWaiter | null = null; |
| 52 | + private error: Error | null = null; |
| 53 | + |
| 54 | + get currentState(): GoogleLiveConnectionState { |
| 55 | + return this.state; |
| 56 | + } |
| 57 | + |
| 58 | + get isActive(): boolean { |
| 59 | + return this.state === "active"; |
| 60 | + } |
| 61 | + |
| 62 | + get setupComplete(): boolean { |
| 63 | + return this.state === "ready" || this.state === "active"; |
| 64 | + } |
| 65 | + |
| 66 | + begin(socket: WebSocket): Promise<RealtimeTalkTransportStartResult> { |
| 67 | + this.state = "connecting"; |
| 68 | + this.socket = socket; |
| 69 | + this.error = null; |
| 70 | + return new Promise((resolve, reject) => { |
| 71 | + this.waiter = { resolve, reject }; |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + markReady(socket: WebSocket): boolean { |
| 76 | + if (this.state !== "connecting" || this.socket !== socket) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + this.state = "ready"; |
| 80 | + this.takeWaiter()?.resolve("ready"); |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + finishStart(result: RealtimeTalkTransportStartResult): RealtimeTalkTransportStartResult { |
| 85 | + if (this.error) { |
| 86 | + throw this.error; |
| 87 | + } |
| 88 | + return this.state === "cancelled" ? "cancelled" : result; |
| 89 | + } |
| 90 | + |
| 91 | + activate(): boolean { |
| 92 | + if (this.state === "active") { |
| 93 | + return false; |
| 94 | + } |
| 95 | + if (this.error) { |
| 96 | + throw this.error; |
| 97 | + } |
| 98 | + if (this.state === "cancelled" || this.state === "idle") { |
| 99 | + return false; |
| 100 | + } |
| 101 | + if (this.state !== "ready") { |
| 102 | + throw new Error("Google Live transport activated before setup completed"); |
| 103 | + } |
| 104 | + this.state = "active"; |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + failStartup(socket: WebSocket, error: Error): boolean { |
| 109 | + if (this.socket !== socket || (this.state !== "connecting" && this.state !== "ready")) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + this.state = "failed"; |
| 113 | + this.error = error; |
| 114 | + this.takeWaiter()?.reject(error); |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + cancel(): void { |
| 119 | + if (this.state === "cancelled" || this.state === "failed") { |
| 120 | + return; |
| 121 | + } |
| 122 | + this.state = "cancelled"; |
| 123 | + this.takeWaiter()?.resolve("cancelled"); |
| 124 | + } |
| 125 | + |
| 126 | + private takeWaiter(): StartupWaiter | null { |
| 127 | + const waiter = this.waiter; |
| 128 | + this.waiter = null; |
| 129 | + return waiter; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +export function runRealtimeTalkCleanup(steps: Array<() => void>): void { |
| 134 | + let firstError: Error | undefined; |
| 135 | + for (const step of steps) { |
| 136 | + try { |
| 137 | + step(); |
| 138 | + } catch (error) { |
| 139 | + firstError ??= |
| 140 | + error instanceof Error |
| 141 | + ? error |
| 142 | + : new Error("Realtime Talk cleanup failed", { cause: error }); |
| 143 | + } |
| 144 | + } |
| 145 | + if (firstError) { |
| 146 | + throw firstError; |
| 147 | + } |
| 148 | +} |
0 commit comments