Skip to content

Commit 27a8705

Browse files
committed
feat: Add initial message property
1 parent 2cf99ae commit 27a8705

File tree

5 files changed

+70
-35
lines changed

5 files changed

+70
-35
lines changed

src/core/useRcbPlugin.tsx

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const useRcbPlugin = (pluginConfig?: PluginConfig): ReturnType<Plugin> => {
2929
const outputTypeRef = useRef<'character' | 'chunk' | 'full'>('chunk');
3030
const outputSpeedRef = useRef<number>(30);
3131
const historySizeRef = useRef<number>(0);
32+
const initialMessageRef = useRef<string>("");
3233
const errorMessageRef = useRef<string>('Unable to get response, please try again.');
3334
const onUserMessageRef = useRef<((msg: Message) => Promise<string | null>) | null>(null);
3435
const onKeyDownRef = useRef<((e: KeyboardEvent) => Promise<string | null>) | null>(null);
@@ -54,39 +55,41 @@ const useRcbPlugin = (pluginConfig?: PluginConfig): ReturnType<Plugin> => {
5455
outputTypeRef.current = block.llmConnector?.outputType ?? 'chunk';
5556
outputSpeedRef.current = block.llmConnector?.outputSpeed ?? 30;
5657
historySizeRef.current = block.llmConnector?.historySize ?? 0;
58+
initialMessageRef.current = block.llmConnector?.initialMessage ?? "";
5759
errorMessageRef.current = block.llmConnector?.errorMessage ?? 'Unable to get response, please try again.';
5860
onUserMessageRef.current = block.llmConnector?.stopConditions?.onUserMessage ?? null;
5961
onKeyDownRef.current = block.llmConnector?.stopConditions?.onKeyDown ?? null;
6062
});
6163

64+
const refs = {
65+
providerRef,
66+
messagesRef,
67+
outputTypeRef,
68+
outputSpeedRef,
69+
historySizeRef,
70+
initialMessageRef,
71+
errorMessageRef,
72+
onUserMessageRef,
73+
onKeyDownRef,
74+
};
75+
76+
const actions = {
77+
speakAudio,
78+
injectMessage,
79+
simulateStreamMessage,
80+
streamMessage,
81+
endStreamMessage,
82+
toggleTextAreaDisabled,
83+
toggleIsBotTyping,
84+
focusTextArea,
85+
goToPath,
86+
};
87+
6288
// handles pre-processing and post-processing of blocks.
63-
useProcessBlock(getBotId, toggleIsBotTyping, toggleTextAreaDisabled, focusTextArea);
89+
useProcessBlock(getBotId, refs, actions);
6490

6591
// handles message events
66-
useMessageHandler(
67-
getBotId,
68-
{
69-
providerRef,
70-
messagesRef,
71-
outputTypeRef,
72-
outputSpeedRef,
73-
historySizeRef,
74-
errorMessageRef,
75-
onUserMessageRef,
76-
onKeyDownRef,
77-
},
78-
{
79-
speakAudio,
80-
injectMessage,
81-
simulateStreamMessage,
82-
streamMessage,
83-
endStreamMessage,
84-
toggleTextAreaDisabled,
85-
toggleIsBotTyping,
86-
focusTextArea,
87-
goToPath,
88-
}
89-
);
92+
useMessageHandler(getBotId, refs, actions);
9093

9194
// initializes plugin metadata with plugin name
9295
const pluginMetaData: ReturnType<Plugin> = { name: '@rcb-plugins/llm-connector' };

src/hooks/useMessageHandler.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const useMessageHandler = (
2727
outputTypeRef: React.MutableRefObject<'character' | 'chunk' | 'full'>;
2828
outputSpeedRef: React.MutableRefObject<number>;
2929
historySizeRef: React.MutableRefObject<number>;
30+
initialMessageRef: React.MutableRefObject<string>;
3031
errorMessageRef: React.MutableRefObject<string>;
3132
onUserMessageRef: React.MutableRefObject<((msg: Message) => Promise<string | null>) | null>;
3233
onKeyDownRef: React.MutableRefObject<((e: KeyboardEvent) => Promise<string | null>) | null>;
@@ -43,7 +44,7 @@ const useMessageHandler = (
4344
goToPath: (path: string) => void;
4445
}
4546
) => {
46-
const { messagesRef, outputTypeRef, onUserMessageRef, onKeyDownRef } = refs;
47+
const { messagesRef, outputTypeRef, onUserMessageRef, onKeyDownRef, errorMessageRef } = refs;
4748
const {
4849
injectMessage,
4950
simulateStreamMessage,
@@ -102,9 +103,9 @@ const useMessageHandler = (
102103
});
103104
console.error('LLM prompt failed', err);
104105
if (outputTypeRef.current === "full") {
105-
injectMessage(refs.errorMessageRef.current);
106+
injectMessage(errorMessageRef.current);
106107
} else {
107-
simulateStreamMessage(refs.errorMessageRef.current);
108+
simulateStreamMessage(errorMessageRef.current);
108109
}
109110
});
110111
}, STREAM_DEBOUNCE_MS);

src/hooks/useProcessBlock.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
11
import { useEffect, useCallback } from 'react';
2-
import { RcbPreProcessBlockEvent, RcbPostProcessBlockEvent } from 'react-chatbotify';
2+
import { RcbPreProcessBlockEvent, RcbPostProcessBlockEvent, Message } from 'react-chatbotify';
33
import { 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
*/
1313
const 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(() => {

src/types/LlmConnectorBlock.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type LlmConnectorBlock = Block & {
1010
outputType?: 'character' | 'chunk' | 'full';
1111
outputSpeed?: number;
1212
historySize?: number;
13+
initialMessage?: string;
1314
errorMessage?: string;
1415
stopConditions?: {
1516
onUserMessage?: (message: Message) => Promise<string | null>;

src/utils/promptHandler.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const handlePrompt = async (
3535
outputTypeRef: React.MutableRefObject<'character' | 'chunk' | 'full'>;
3636
outputSpeedRef: React.MutableRefObject<number>;
3737
historySizeRef: React.MutableRefObject<number>;
38+
initialMessageRef: React.MutableRefObject<string>;
3839
errorMessageRef: React.MutableRefObject<string>;
3940
onUserMessageRef: React.MutableRefObject<((msg: Message) => Promise<string | null>) | null>;
4041
onKeyDownRef: React.MutableRefObject<((e: KeyboardEvent) => Promise<string | null>) | null>;

0 commit comments

Comments
 (0)