11import type { ChatGenerationResetReason , ChatViewMessage } from './chat-view' ;
22import { parseChatViewMessages } from './chat-view' ;
3- import type { ChatStopIntent , UserMessageDeliveryStatus } from './chat-types' ;
3+ import {
4+ CHAT_STOP_OUTCOMES ,
5+ CHAT_PROCESSING_PHASES ,
6+ type ChatProcessingEntry ,
7+ type ChatProcessingPhase ,
8+ type ChatStopIntent ,
9+ type ChatStopOutcome ,
10+ type UserMessageDeliveryStatus ,
11+ } from './chat-types' ;
412import type {
513 PendingUserInput ,
614 PendingUserInputClearReason ,
@@ -120,7 +128,7 @@ export class ChatSessionStoppedMessage {
120128 readonly type = 'chat-session-stopped' as const ;
121129 constructor (
122130 public chatId : string ,
123- public success : boolean ,
131+ public outcome : ChatStopOutcome ,
124132 public intent : ChatStopIntent ,
125133 ) { }
126134}
@@ -129,7 +137,7 @@ export class ChatProcessingUpdatedMessage {
129137 readonly type = 'chat-processing-updated' as const ;
130138 constructor (
131139 public chatId : string ,
132- public isProcessing : boolean ,
140+ public phase : ChatProcessingPhase | null ,
133141 ) { }
134142}
135143
@@ -155,14 +163,14 @@ export type ReconnectControlResult =
155163 | { chatId : string ; outcome : 'not-found' }
156164 | { chatId : string ; outcome : 'unavailable' } ;
157165
158- export type ReconnectProcessingResult =
159- | { outcome : 'snapshot' ; runningChatIds : string [ ] }
166+ export type ChatProcessingSnapshotResult =
167+ | { outcome : 'snapshot' ; chats : ChatProcessingEntry [ ] }
160168 | { outcome : 'unavailable' } ;
161169
162170export class ReconnectStateMessage {
163171 readonly type = 'reconnect-state' as const ;
164172 constructor (
165- public processing : ReconnectProcessingResult ,
173+ public processing : ChatProcessingSnapshotResult ,
166174 public controlResults : ReconnectControlResult [ ] ,
167175 public clientRequestId ?: string ,
168176 ) { }
@@ -202,6 +210,7 @@ export class WsPongMessage {
202210 public clientRequestId : string ,
203211 public sentAt : number ,
204212 public serverTime : string ,
213+ public processing : ChatProcessingSnapshotResult ,
205214 ) { }
206215}
207216
@@ -378,24 +387,26 @@ function reconnectControlResults(value: unknown): ReconnectControlResult[] | nul
378387 return results ;
379388}
380389
381- function reconnectProcessingResult ( value : unknown ) : ReconnectProcessingResult | null {
390+ function chatProcessingSnapshotResult ( value : unknown ) : ChatProcessingSnapshotResult | null {
382391 if ( ! value || typeof value !== 'object' || Array . isArray ( value ) ) return null ;
383392
384393 const result = value as Record < string , unknown > ;
385394 if ( result . outcome === 'unavailable' ) return { outcome : 'unavailable' } ;
386- if ( result . outcome !== 'snapshot' || ! Array . isArray ( result . runningChatIds ) ) return null ;
395+ if ( result . outcome !== 'snapshot' || ! Array . isArray ( result . chats ) ) return null ;
387396
388- const runningChatIds : string [ ] = [ ] ;
397+ const chats : ChatProcessingEntry [ ] = [ ] ;
389398 const seen = new Set < string > ( ) ;
390- for ( const valueChatId of result . runningChatIds ) {
391- const chatId = requiredStr ( valueChatId ) ;
392- if ( ! chatId ) return null ;
393- if ( seen . has ( chatId ) ) continue ;
399+ for ( const valueEntry of result . chats ) {
400+ if ( ! valueEntry || typeof valueEntry !== 'object' || Array . isArray ( valueEntry ) ) return null ;
401+ const entry = valueEntry as Record < string , unknown > ;
402+ const chatId = requiredStr ( entry . chatId ) ;
403+ const phase = CHAT_PROCESSING_PHASES . find ( ( valuePhase ) => valuePhase === entry . phase ) ;
404+ if ( ! chatId || ! phase || seen . has ( chatId ) ) return null ;
394405 seen . add ( chatId ) ;
395- runningChatIds . push ( chatId ) ;
406+ chats . push ( { chatId, phase } ) ;
396407 }
397408
398- return { outcome : 'snapshot' , runningChatIds } ;
409+ return { outcome : 'snapshot' , chats } ;
399410}
400411
401412function hasField ( data : Record < string , unknown > , key : string ) : boolean {
@@ -584,18 +595,22 @@ export function parseServerWsMessage(
584595 case 'chat-session-stopped' : {
585596 const chatId = requiredStr ( data . chatId ) ;
586597 const intent = data . intent ;
598+ const outcome = CHAT_STOP_OUTCOMES . find ( ( entry ) => entry === data . outcome ) ;
587599 return chatId && (
588600 intent === 'stop'
589601 || intent === 'interrupt-and-send'
590602 || intent === 'chat-deletion'
591- )
592- ? new ChatSessionStoppedMessage ( chatId , Boolean ( data . success ) , intent )
603+ ) && outcome
604+ ? new ChatSessionStoppedMessage ( chatId , outcome , intent )
593605 : null ;
594606 }
595607 case 'chat-processing-updated' : {
596608 const chatId = requiredStr ( data . chatId ) ;
597- return chatId
598- ? new ChatProcessingUpdatedMessage ( chatId , Boolean ( data . isProcessing ) )
609+ const phase = data . phase === null
610+ ? null
611+ : CHAT_PROCESSING_PHASES . find ( ( entry ) => entry === data . phase ) ;
612+ return chatId && phase !== undefined
613+ ? new ChatProcessingUpdatedMessage ( chatId , phase )
599614 : null ;
600615 }
601616 case 'chat-execution-control-updated' : {
@@ -617,7 +632,7 @@ export function parseServerWsMessage(
617632 : null ;
618633 }
619634 case 'reconnect-state' : {
620- const processing = reconnectProcessingResult ( data . processing ) ;
635+ const processing = chatProcessingSnapshotResult ( data . processing ) ;
621636 const controlResults = reconnectControlResults ( data . controlResults ) ;
622637 if ( ! processing || ! controlResults ) return null ;
623638 return new ReconnectStateMessage (
@@ -664,8 +679,9 @@ export function parseServerWsMessage(
664679 ? data . sentAt
665680 : null ;
666681 const serverTime = requiredStr ( data . serverTime ) ;
667- return clientRequestId && sentAt !== null && serverTime
668- ? new WsPongMessage ( clientRequestId , sentAt , serverTime )
682+ const processing = chatProcessingSnapshotResult ( data . processing ) ;
683+ return clientRequestId && sentAt !== null && serverTime && processing
684+ ? new WsPongMessage ( clientRequestId , sentAt , serverTime , processing )
669685 : null ;
670686 }
671687 case 'chat-title-updated' : {
0 commit comments