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.

Refactor useChatActions to streamline thinking message handling by updating existing system messages instead of creating new bubbles. Clear thinking messages from all system messages upon response completion.

Salman-Ahamed6db1f8cb6498
diff --git a/packages/app/src/react/features/ai-chat/hooks/use-chat-actions.ts b/packages/app/src/react/features/ai-chat/hooks/use-chat-actions.ts
index dcbe5498..dd07eac4 100644
--- a/packages/app/src/react/features/ai-chat/hooks/use-chat-actions.ts
+++ b/packages/app/src/react/features/ai-chat/hooks/use-chat-actions.ts
@@ -135,6 +135,7 @@ export const useChatActions = ({
updatedBubbles[lastSystemMessageIndex].isReplying = false;
updatedBubbles[lastSystemMessageIndex].isError = errorInfo?.isError || false;
updatedBubbles[lastSystemMessageIndex].hideMessageBubble = false; // Show the message bubble
+ updatedBubbles[lastSystemMessageIndex].thinkingMessage = undefined; // Clear thinking message
return updatedBubbles;
}
return filteredBubbles;
@@ -153,39 +154,15 @@ export const useChatActions = ({
.filter(({ msg }) => msg.type === 'system')
.pop()?.index ?? -1;
- // Check if there's already a thinking message after the last system message
- const thinkingAfterSystem = bubbles
- .slice(lastSystemIndex + 1)
- .find((msg) => msg.type === 'thinking');
-
- if (thinkingAfterSystem) {
- // Update existing thinking message
- thinkingAfterSystem.message = thinking.message;
- return [...bubbles];
- } else {
- // Add new thinking message after the last system message
- const thinkingMessage: IChatMessage = {
- me: false,
- message: thinking.message,
- type: 'thinking',
- avatar,
- isReplying: false, // Hide loading indicator for thinking messages
- };
-
- const newBubbles = [...bubbles];
- if (lastSystemIndex !== -1) {
- // Insert after the last system message
- newBubbles.splice(lastSystemIndex + 1, 0, thinkingMessage);
- // Hide the system message when thinking starts
- newBubbles[lastSystemIndex].hideMessageBubble = true;
- newBubbles[lastSystemIndex].isReplying = false;
- } else {
- // Add at the end if no system message
- newBubbles.push(thinkingMessage);
- }
-
- return newBubbles;
+ if (lastSystemIndex !== -1) {
+ // Update the existing system message with thinking message instead of creating separate bubble
+ const updatedBubbles = [...bubbles];
+ updatedBubbles[lastSystemIndex].thinkingMessage = thinking.message;
+ updatedBubbles[lastSystemIndex].isReplying = false;
+ return updatedBubbles;
}
+
+ return bubbles;
});
},
onStart: () => {
@@ -203,6 +180,14 @@ export const useChatActions = ({
// Remove thinking messages when response is complete
const filteredMessages = prev.filter((msg) => msg.type !== 'thinking');
const newMessages = [...filteredMessages];
+
+ // Clear thinkingMessage from all system messages
+ newMessages.forEach((msg) => {
+ if (msg.type === 'system') {
+ msg.thinkingMessage = undefined;
+ }
+ });
+
const lastMessage = newMessages[newMessages.length - 1];
if (lastMessage) {
lastMessage.isReplying = false;