11import { useEffect , useCallback } from 'react' ;
2- import { RcbPreProcessBlockEvent , RcbPostProcessBlockEvent } from 'react-chatbotify' ;
2+ import { RcbPreProcessBlockEvent , RcbPostProcessBlockEvent , Message } from 'react-chatbotify' ;
33import { LlmConnectorBlock } from '../types/LlmConnectorBlock' ;
4+ import { Provider } from '../types/Provider' ;
45
56/**
67 * Handles pre-processing and post-processing of blocks.
78 *
89 * @param getBotId id of the chatbot
9- * @param toggleIsBotTyping toggles typing indicator for the chatbot
10- * @param toggleTextAreaDisabled toggles text area disabled state for the chatbot
11- * @param focusTextArea focuses on the text area
10+ * @param refs object containing relevant refs
11+ * @param actions object containing relevant actions
1212 */
1313const useProcessBlock = (
1414 getBotId : ( ) => string | null ,
15- toggleIsBotTyping : ( active ?: boolean ) => void ,
16- toggleTextAreaDisabled : ( active ?: boolean ) => void ,
17- focusTextArea : ( ) => void
15+ refs : {
16+ providerRef : React . MutableRefObject < Provider | null > ;
17+ messagesRef : React . MutableRefObject < Message [ ] > ;
18+ outputTypeRef : React . MutableRefObject < 'character' | 'chunk' | 'full' > ;
19+ outputSpeedRef : React . MutableRefObject < number > ;
20+ historySizeRef : React . MutableRefObject < number > ;
21+ initialMessageRef : React . MutableRefObject < string > ;
22+ errorMessageRef : React . MutableRefObject < string > ;
23+ onUserMessageRef : React . MutableRefObject < ( ( msg : Message ) => Promise < string | null > ) | null > ;
24+ onKeyDownRef : React . MutableRefObject < ( ( e : KeyboardEvent ) => Promise < string | null > ) | null > ;
25+ } ,
26+ actions : {
27+ speakAudio : ( text : string ) => void ;
28+ injectMessage : ( content : string | JSX . Element , sender ?: string ) => Promise < Message | null > ;
29+ simulateStreamMessage : ( content : string , sender ?: string ) => Promise < Message | null > ;
30+ streamMessage : ( msg : string ) => void ;
31+ endStreamMessage : ( ) => void ;
32+ toggleTextAreaDisabled : ( active ?: boolean ) => void ;
33+ toggleIsBotTyping : ( active ?: boolean ) => void ;
34+ focusTextArea : ( ) => void ;
35+ goToPath : ( path : string ) => void ;
36+ }
1837) => {
38+ const { outputTypeRef } = refs ;
39+ const { toggleTextAreaDisabled, toggleIsBotTyping, focusTextArea, injectMessage, simulateStreamMessage } = actions ;
40+
1941 /**
2042 * Handles blocking of pre-processing and post-processing of block for full custom control.
2143 *
@@ -42,6 +64,13 @@ const useProcessBlock = (
4264 // since pre-processing event is disabled, we simulate
4365 // disabling typing indicator, enabling text area and focusing on it again
4466 if ( event . type === 'rcb-pre-process-block' ) {
67+ if ( block . llmConnector ?. initialMessage ) {
68+ if ( outputTypeRef . current === "full" ) {
69+ injectMessage ( refs . initialMessageRef . current ) ;
70+ } else {
71+ simulateStreamMessage ( refs . initialMessageRef . current ) ;
72+ }
73+ }
4574 toggleIsBotTyping ( false ) ;
4675 toggleTextAreaDisabled ( false ) ;
4776 setTimeout ( ( ) => {
0 commit comments