Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

Enhance Thinking State Management in useChat Hook

Salman-Ahamed591fe137ffde
diff --git a/packages/app/src/react/features/ai-chat/hooks/use-chat.ts b/packages/app/src/react/features/ai-chat/hooks/use-chat.ts
index c41911aa..77726a30 100644
--- a/packages/app/src/react/features/ai-chat/hooks/use-chat.ts
+++ b/packages/app/src/react/features/ai-chat/hooks/use-chat.ts
@@ -84,11 +84,17 @@ export const useChat = (config: IUseChatConfig): IUseChatReturn => {
// Current AI message being constructed
const currentAIMessageRef = useRef<string>('');
+ // Track if we're in thinking state to create new message after
+ const isThinkingRef = useRef<boolean>(false);
+ const hasThinkingOccurredRef = useRef<boolean>(false);
+
// Stream management
const { isStreaming, error, startStream, abortStream, clearError } = useChatStream({
client,
onStreamStart: () => {
currentAIMessageRef.current = '';
+ isThinkingRef.current = false;
+ hasThinkingOccurredRef.current = false;
},
onStreamEnd: () => {
setIsProcessing(false);
@@ -270,11 +276,32 @@ export const useChat = (config: IUseChatConfig): IUseChatReturn => {
},
{
onContent: (content: string) => {
+ // If content comes after thinking, we need to:
+ // 1. Clear thinking message from current message
+ // 2. Create new message for new content
+ if (hasThinkingOccurredRef.current && isThinkingRef.current) {
+ // Clear thinking message from current message
+ updateThinkingMessage('');
+
+ // Mark that we've handled the thinking transition
+ isThinkingRef.current = false;
+
+ // Reset accumulator for new message
+ currentAIMessageRef.current = '';
+
+ // Add new AI message for content after thinking
+ addAIMessage();
+ }
+
// Accumulate content
currentAIMessageRef.current += content;
updateAIMessage(currentAIMessageRef.current);
},
onThinking: (thinkingMsg: string, _type: TThinkingType) => {
+ // Mark that we're in thinking state
+ isThinkingRef.current = true;
+ hasThinkingOccurredRef.current = true;
+
// Update thinking message
updateThinkingMessage(thinkingMsg);
},