@@ -31,6 +31,16 @@ type EventContext = Pick<
3131 | "streamSessionIssuer"
3232> ;
3333
34+ export type ProcessEventResult =
35+ | { kind : "ignored" }
36+ | { kind : "processed" }
37+ | {
38+ kind : "final-speech" ;
39+ call : CallRecord ;
40+ transcript : string ;
41+ waiterResolved : boolean ;
42+ } ;
43+
3444function shouldAcceptInbound ( config : EventContext [ "config" ] , from : string | undefined ) : boolean {
3545 const { inboundPolicy : policy , allowFrom } = config ;
3646
@@ -138,10 +148,10 @@ function persistRejectedInboundCall(params: {
138148 persistCallRecord ( params . ctx . storePath , rejectedCall ) ;
139149}
140150
141- export function processEvent ( ctx : EventContext , event : NormalizedEvent ) : void {
151+ export function processEvent ( ctx : EventContext , event : NormalizedEvent ) : ProcessEventResult {
142152 const dedupeKey = event . dedupeKey || event . id ;
143153 if ( ctx . processedEventIds . has ( dedupeKey ) ) {
144- return ;
154+ return { kind : "ignored" } ;
145155 }
146156
147157 let call = findCall ( {
@@ -166,11 +176,11 @@ export function processEvent(ctx: EventContext, event: NormalizedEvent): void {
166176 console . warn (
167177 `[voice-call] Inbound call rejected by policy but no provider to hang up (providerCallId: ${ pid } , from: ${ event . from } ); call will time out on provider side.` ,
168178 ) ;
169- return ;
179+ return { kind : "ignored" } ;
170180 }
171181 ctx . processedEventIds . add ( dedupeKey ) ;
172182 if ( ctx . rejectedProviderCallIds . has ( pid ) ) {
173- return ;
183+ return { kind : "ignored" } ;
174184 }
175185 ctx . rejectedProviderCallIds . add ( pid ) ;
176186 const callId = event . callId ?? pid ;
@@ -187,7 +197,7 @@ export function processEvent(ctx: EventContext, event: NormalizedEvent): void {
187197 const message = formatErrorMessage ( err ) ;
188198 console . warn ( `[voice-call] Failed to reject inbound call ${ pid } :` , message ) ;
189199 } ) ;
190- return ;
200+ return { kind : "processed" } ;
191201 }
192202
193203 call = createWebhookCall ( {
@@ -203,7 +213,7 @@ export function processEvent(ctx: EventContext, event: NormalizedEvent): void {
203213 }
204214
205215 if ( ! call ) {
206- return ;
216+ return { kind : "ignored" } ;
207217 }
208218
209219 if ( event . providerCallId && event . providerCallId !== call . providerCallId ) {
@@ -224,6 +234,7 @@ export function processEvent(ctx: EventContext, event: NormalizedEvent): void {
224234 call . processedEventIds . push ( dedupeKey ) ;
225235 }
226236
237+ let result : ProcessEventResult = { kind : "processed" } ;
227238 switch ( event . type ) {
228239 case "call.initiated" :
229240 transitionState ( call , "initiated" ) ;
@@ -305,9 +316,16 @@ export function processEvent(ctx: EventContext, event: NormalizedEvent): void {
305316 console . warn (
306317 `[voice-call] Ignoring speech event with mismatched turn token for ${ call . callId } ` ,
307318 ) ;
319+ result = { kind : "ignored" } ;
308320 break ;
309321 }
310322 addTranscriptEntry ( call , "user" , event . transcript ) ;
323+ result = {
324+ kind : "final-speech" ,
325+ call,
326+ transcript : event . transcript ,
327+ waiterResolved : resolved ,
328+ } ;
311329 }
312330 ensureMaxDurationTimerForLiveCall ( {
313331 ctx,
@@ -331,7 +349,7 @@ export function processEvent(ctx: EventContext, event: NormalizedEvent): void {
331349 endReason : event . reason ,
332350 endedAt : event . timestamp ,
333351 } ) ;
334- return ;
352+ return { kind : "processed" } ;
335353
336354 case "call.error" :
337355 if ( ! event . retryable ) {
@@ -342,12 +360,13 @@ export function processEvent(ctx: EventContext, event: NormalizedEvent): void {
342360 endedAt : event . timestamp ,
343361 transcriptRejectReason : `Call error: ${ event . error } ` ,
344362 } ) ;
345- return ;
363+ return { kind : "processed" } ;
346364 }
347365 // Keep retryable provider errors replayable so a redelivery can still
348366 // drive later recovery or terminal handling for the same event key.
349367 break ;
350368 }
351369
352370 persistCallRecord ( ctx . storePath , call ) ;
371+ return result ;
353372}
0 commit comments