8
8
import React , { createContext , useCallback , useContext , useEffect , useState } from "react" ;
9
9
import useWebSocket , { ReadyState } from "react-use-websocket" ;
10
10
import { useLocation } from "react-router-dom" ;
11
- import { BotMessage , DebugMessage , AiModel } from "../../types/api/entities/bot" ;
11
+ import { BotMessage , DebugMessage , AiModel , StateMessage } from "../../types/api/entities/bot" ;
12
12
import { getChatsWithWholeThreads } from "../../api/bot/getChatsWithWholeThreads" ;
13
13
import { getChats } from "api/bot/getChats" ;
14
14
import { useAlertSnackbar } from "@postgres.ai/shared/components/AlertSnackbar/useAlertSnackbar" ;
@@ -45,18 +45,19 @@ type UseAiBotReturnType = {
45
45
wsReadyState : ReadyState ;
46
46
changeChatVisibility : ( threadId : string , isPublic : boolean ) => void ;
47
47
isChangeVisibilityLoading : boolean ;
48
- unsubscribe : ( threadId : string ) => void
49
- chatVisibility : 'public' | 'private'
50
- debugMessages : DebugMessage [ ] | null
51
- getDebugMessagesForWholeThread : ( ) => void
48
+ unsubscribe : ( threadId : string ) => void ;
49
+ chatVisibility : 'public' | 'private' ;
50
+ debugMessages : DebugMessage [ ] | null ;
51
+ getDebugMessagesForWholeThread : ( ) => void ;
52
52
chatsList : UseBotChatsListHook [ 'chatsList' ] ;
53
53
chatsListLoading : UseBotChatsListHook [ 'loading' ] ;
54
- getChatsList : UseBotChatsListHook [ 'getChatsList' ]
55
- aiModel : UseAiModelsList [ 'aiModel' ] ,
56
- setAiModel : UseAiModelsList [ 'setAiModel' ]
57
- aiModels : UseAiModelsList [ 'aiModels' ]
58
- aiModelsLoading : UseAiModelsList [ 'loading' ]
54
+ getChatsList : UseBotChatsListHook [ 'getChatsList' ] ;
55
+ aiModel : UseAiModelsList [ 'aiModel' ] ;
56
+ setAiModel : UseAiModelsList [ 'setAiModel' ] ;
57
+ aiModels : UseAiModelsList [ 'aiModels' ] ;
58
+ aiModelsLoading : UseAiModelsList [ 'loading' ] ;
59
59
debugMessagesLoading : boolean ;
60
+ stateMessage : StateMessage | null ;
60
61
}
61
62
62
63
type UseAiBotArgs = {
@@ -88,6 +89,7 @@ export const useAiBotProviderValue = (args: UseAiBotArgs): UseAiBotReturnType =>
88
89
const [ error , setError ] = useState < ErrorType | null > ( null ) ;
89
90
const [ wsLoading , setWsLoading ] = useState < boolean > ( false ) ;
90
91
const [ chatVisibility , setChatVisibility ] = useState < UseAiBotReturnType [ 'chatVisibility' ] > ( 'public' ) ;
92
+ const [ stateMessage , setStateMessage ] = useState < StateMessage | null > ( null )
91
93
92
94
const [ isChangeVisibilityLoading , setIsChangeVisibilityLoading ] = useState < boolean > ( false ) ;
93
95
@@ -100,16 +102,25 @@ export const useAiBotProviderValue = (args: UseAiBotArgs): UseAiBotReturnType =>
100
102
101
103
const onWebSocketMessage = ( event : WebSocketEventMap [ 'message' ] ) => {
102
104
if ( event . data ) {
103
- const messageData : BotMessage | DebugMessage = JSON . parse ( event . data ) ;
105
+ const messageData : BotMessage | DebugMessage | StateMessage = JSON . parse ( event . data ) ;
104
106
if ( messageData ) {
105
107
const isThreadMatching = threadId && threadId === messageData . thread_id ;
106
108
const isParentMatching = ! threadId && 'parent_id' in messageData && messageData . parent_id && messages ;
107
109
const isDebugMessage = messageData . type === 'debug' ;
108
- if ( isThreadMatching || isParentMatching || isDebugMessage ) {
110
+ const isStateMessage = messageData . type === 'state' ;
111
+ if ( isThreadMatching || isParentMatching || isDebugMessage || isStateMessage ) {
109
112
if ( isDebugMessage ) {
110
113
let currentDebugMessages = [ ...( debugMessages || [ ] ) ] ;
111
114
currentDebugMessages . push ( messageData )
112
115
setDebugMessages ( currentDebugMessages )
116
+ } else if ( isStateMessage ) {
117
+ if ( isThreadMatching || ! threadId ) {
118
+ if ( messageData . state ) {
119
+ setStateMessage ( messageData )
120
+ } else {
121
+ setStateMessage ( null )
122
+ }
123
+ }
113
124
} else {
114
125
// Check if the last message needs its data updated
115
126
let currentMessages = [ ...( messages || [ ] ) ] ;
@@ -368,7 +379,8 @@ export const useAiBotProviderValue = (args: UseAiBotArgs): UseAiBotReturnType =>
368
379
aiModelsLoading,
369
380
chatVisibility,
370
381
debugMessages,
371
- debugMessagesLoading
382
+ debugMessagesLoading,
383
+ stateMessage,
372
384
}
373
385
}
374
386
0 commit comments