From ce34803e36c583dd00d3e8f2ecb8db9ead6ae9fa Mon Sep 17 00:00:00 2001 From: Kevin Kipp Date: Wed, 18 Dec 2024 09:54:22 -0600 Subject: [PATCH] Only allow one request to be pending at a time --- app/durableObjects/ChatRoom.server.ts | 75 ++++++++++++++++----------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/app/durableObjects/ChatRoom.server.ts b/app/durableObjects/ChatRoom.server.ts index 52341445..24f17c59 100644 --- a/app/durableObjects/ChatRoom.server.ts +++ b/app/durableObjects/ChatRoom.server.ts @@ -454,42 +454,59 @@ export class ChatRoom extends Server { } } case 'requestAiControl': { - const aiSessionId = await this.ctx.storage.get('ai:sessionId') - invariant(aiSessionId) - const openAiSession = new CallsSession( - aiSessionId, - { - Authorization: `Bearer ${this.env.CALLS_APP_SECRET}`, - 'Content-Type': 'application/json', - }, - `https://rtc.live.cloudflare.com/apps/${this.env.CALLS_APP_ID}` + const userControllingPending = await this.ctx.storage.get( + 'ai:userControlling:pending' ) - - const { track } = data - - console.log('starting exchangeStepTwo, pulling', { - session: track.sessionId, - trackName: track.trackName, - }) - const exchangeStepTwo = await openAiSession.NewTracks({ - tracks: [ + if (userControllingPending) { + break + } + await this.ctx.storage.put( + 'ai:userControlling:pending', + connection.id + ) + try { + const aiSessionId = + await this.ctx.storage.get('ai:sessionId') + invariant(aiSessionId) + const openAiSession = new CallsSession( + aiSessionId, { - location: 'remote', - sessionId: track.sessionId, - trackName: track.trackName, - // Let Calls to find out the actual mid value - mid: `#ai-generated-voice`, + Authorization: `Bearer ${this.env.CALLS_APP_SECRET}`, + 'Content-Type': 'application/json', }, - ], - }) - console.log('exchangeStepTwo result', exchangeStepTwo) - checkNewTracksResponse(exchangeStepTwo) + `https://rtc.live.cloudflare.com/apps/${this.env.CALLS_APP_ID}` + ) - this.ctx.storage.put('ai:userControlling', connection.id) - this.broadcastRoomState() + const { track } = data + + console.log('starting exchangeStepTwo, pulling', { + session: track.sessionId, + trackName: track.trackName, + }) + const exchangeStepTwo = await openAiSession.NewTracks({ + tracks: [ + { + location: 'remote', + sessionId: track.sessionId, + trackName: track.trackName, + // Let Calls to find out the actual mid value + mid: `#ai-generated-voice`, + }, + ], + }) + + console.log('exchangeStepTwo result', exchangeStepTwo) + checkNewTracksResponse(exchangeStepTwo) + + await this.ctx.storage.put('ai:userControlling', connection.id) + this.broadcastRoomState() + } finally { + await this.ctx.storage.delete('ai:userControlling:pending') + } break } case 'relenquishAiControl': { + await this.ctx.storage.delete('ai:userControlling:pending') this.ctx.storage.delete('ai:userControlling') this.broadcastRoomState() break