Skip to content

Commit ed01750

Browse files
authored
Merge pull request #349 from FlowiseAI/feature/Thinking-Message
Feat/add thinking message
2 parents eea4f58 + a6f5c35 commit ed01750

10 files changed

Lines changed: 168594 additions & 4 deletions

File tree

dist/components/Bot.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ export type MessageType = {
7777
id?: string;
7878
followUpPrompts?: string;
7979
dateTime?: string;
80+
thinking?: string;
81+
thinkingDuration?: number;
82+
isThinking?: boolean;
8083
};
8184
type observerConfigType = (accessor: string | boolean | object | MessageType[]) => void;
8285
export type observersConfigType = Record<'observeUserInput' | 'observeLoading' | 'observeMessages', observerConfigType>;

dist/components/Bot.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/components/bubbles/BotBubble.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type Props = {
2+
thinking?: string;
3+
thinkingDuration?: number;
4+
isThinking?: boolean;
5+
backgroundColor?: string;
6+
textColor?: string;
7+
};
8+
export declare const ThinkingCard: (props: Props) => import("solid-js").JSX.Element;
9+
export {};
10+
//# sourceMappingURL=ThinkingBubble.d.ts.map

dist/components/bubbles/ThinkingBubble.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web.js

Lines changed: 84151 additions & 1 deletion
Large diffs are not rendered by default.

dist/web.umd.js

Lines changed: 84159 additions & 1 deletion
Large diffs are not rendered by default.

src/components/Bot.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ export type MessageType = {
134134
id?: string;
135135
followUpPrompts?: string;
136136
dateTime?: string;
137+
thinking?: string;
138+
thinkingDuration?: number;
139+
isThinking?: boolean;
137140
};
138141

139142
type IUploads = {
@@ -496,6 +499,7 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
496499
const [isLeadSaved, setIsLeadSaved] = createSignal(false);
497500
const [leadEmail, setLeadEmail] = createSignal('');
498501
const [disclaimerPopupOpen, setDisclaimerPopupOpen] = createSignal(false);
502+
const [isThinking, setIsThinking] = createSignal(false);
499503

500504
const [openFeedbackDialog, setOpenFeedbackDialog] = createSignal(false);
501505
const [feedback, setFeedback] = createSignal('');
@@ -789,6 +793,41 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
789793
});
790794
};
791795

796+
const handleThinkingEvent = (data: string, duration?: number) => {
797+
if (data && duration === undefined) {
798+
setIsThinking(true);
799+
setMessages((prevMessages) => {
800+
const lastMsg = prevMessages[prevMessages.length - 1];
801+
if (lastMsg.type === 'userMessage') return prevMessages;
802+
const allMessages = [...prevMessages.slice(0, -1), { ...lastMsg, thinking: (lastMsg.thinking || '') + data, isThinking: true }];
803+
addChatMessage(allMessages);
804+
return allMessages;
805+
});
806+
} else if (data === '' && duration !== undefined) {
807+
setIsThinking(false);
808+
setMessages((prevMessages) => {
809+
const lastMsg = prevMessages[prevMessages.length - 1];
810+
if (lastMsg.type === 'userMessage') return prevMessages;
811+
const allMessages = [...prevMessages.slice(0, -1), { ...lastMsg, thinkingDuration: duration, isThinking: false }];
812+
addChatMessage(allMessages);
813+
return allMessages;
814+
});
815+
}
816+
};
817+
818+
const finalizeThinking = () => {
819+
if (isThinking()) {
820+
setIsThinking(false);
821+
setMessages((prevMessages) => {
822+
const lastMsg = prevMessages[prevMessages.length - 1];
823+
if (lastMsg.type === 'userMessage') return prevMessages;
824+
const allMessages = [...prevMessages.slice(0, -1), { ...lastMsg, isThinking: false }];
825+
addChatMessage(allMessages);
826+
return allMessages;
827+
});
828+
}
829+
};
830+
792831
const clearPreviews = () => {
793832
// Revoke the data uris to avoid memory leaks
794833
previews().forEach((file) => URL.revokeObjectURL(file.preview));
@@ -919,6 +958,9 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
919958
case 'agentReasoning':
920959
updateLastMessageAgentReasoning(payload.data);
921960
break;
961+
case 'thinking':
962+
handleThinkingEvent(payload.data, payload.duration);
963+
break;
922964
case 'agentFlowEvent':
923965
updateAgentFlowEvent(payload.data);
924966
break;
@@ -942,6 +984,7 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
942984
closeResponse();
943985
break;
944986
case 'end':
987+
finalizeThinking();
945988
setLocalStorageChatflow(chatflowid, chatId);
946989
closeResponse();
947990
break;
@@ -1194,6 +1237,8 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
11941237
agentFlowExecutedData: data?.agentFlowExecutedData,
11951238
action: data?.action,
11961239
artifacts: data?.artifacts,
1240+
thinking: data?.reasonContent?.thinking,
1241+
thinkingDuration: data?.reasonContent?.thinkingDuration,
11971242
type: 'apiMessage' as messageType,
11981243
feedback: null,
11991244
dateTime: new Date().toISOString(),
@@ -1315,6 +1360,7 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
13151360
messages.push({ message: '', type: 'leadCaptureMessage' });
13161361
}
13171362
setMessages(messages);
1363+
setShowScrollButton(false);
13181364
} catch (error: any) {
13191365
const errorData = error.response.data || `${error.response.status}: ${error.response.statusText}`;
13201366
console.error(`error: ${errorData}`);
@@ -1398,6 +1444,12 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
13981444
if (message.fileAnnotations) chatHistory.fileAnnotations = message.fileAnnotations;
13991445
if (message.fileUploads) chatHistory.fileUploads = message.fileUploads;
14001446
if (message.agentReasoning) chatHistory.agentReasoning = message.agentReasoning;
1447+
if ((message as any).reasonContent && typeof (message as any).reasonContent === 'object') {
1448+
chatHistory.thinking = (message as any).reasonContent.thinking;
1449+
chatHistory.thinkingDuration = (message as any).reasonContent.thinkingDuration;
1450+
}
1451+
if (message.thinking) chatHistory.thinking = message.thinking;
1452+
if (message.thinkingDuration !== undefined) chatHistory.thinkingDuration = message.thinkingDuration;
14011453
if (message.action) chatHistory.action = message.action;
14021454
if (message.artifacts) chatHistory.artifacts = message.artifacts;
14031455
if (message.followUpPrompts) chatHistory.followUpPrompts = message.followUpPrompts;

src/components/bubbles/BotBubble.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { TickIcon, XIcon } from '../icons';
1212
import { SourceBubble } from '../bubbles/SourceBubble';
1313
import { DateTimeToggleTheme } from '@/features/bubble/types';
1414
import { WorkflowTreeView } from '../treeview/WorkflowTreeView';
15+
import { ThinkingCard } from './ThinkingBubble';
1516

1617
type Props = {
1718
message: MessageType;
@@ -455,6 +456,17 @@ export const BotBubble = (props: Props) => {
455456
</For>
456457
</div>
457458
)}
459+
{props.message.thinking && (
460+
<div class="ml-2 mb-1 max-w-full">
461+
<ThinkingCard
462+
thinking={props.message.thinking}
463+
thinkingDuration={props.message.thinkingDuration}
464+
isThinking={props.message.isThinking}
465+
backgroundColor={props.backgroundColor ?? defaultBackgroundColor}
466+
textColor={props.textColor ?? defaultTextColor}
467+
/>
468+
</div>
469+
)}
458470
{props.message.message && (
459471
<span
460472
ref={setBotMessageRef}

0 commit comments

Comments
 (0)