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 for Enhanced Message Type Handling

Salman-Ahamed95aa2424e698
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 4a9cf464..c487cc0e 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
@@ -2,7 +2,7 @@ import { useCallback, useRef, useState } from 'react';
import { CHAT_ERROR_MESSAGE } from '@react/features/ai-chat/constants';
import { chatUtils } from '@react/features/ai-chat/utils';
-import { FileWithMetadata, IChatMessage } from '@react/shared/types/chat.types';
+import { FileWithMetadata, IChatMessage } from '../types/chat.types';
interface UseChatActionsProps {
agentId: string;
@@ -71,9 +71,8 @@ export const useChatActions = ({
if (message.trim()) {
setMessagesHistory((prev) => {
const newMessage: IChatMessage = {
- me: true,
message: message.replace(/\n/g, '\n'),
- type: 'user',
+ type: 'user', // Type determines user vs system
files: attachedFiles,
};
return [...prev, newMessage];
@@ -81,23 +80,19 @@ export const useChatActions = ({
} else if (attachedFiles?.length) {
setMessagesHistory((prev) => {
const newMessage: IChatMessage = {
- me: true,
- message: '',
- type: 'user',
+ message: '', // Empty message - will auto-hide bubble
+ type: 'user', // Type determines user vs system
files: attachedFiles,
- hideMessage: true,
};
return [...prev, newMessage];
});
}
- // Add initial AI response message
+ // Add initial AI response message with loading state
const replyMessage: IChatMessage = {
- me: false,
message: '',
- type: 'system',
+ type: 'loading', // ✅ Type determines loading state
avatar,
- isReplying: true,
};
setMessagesHistory((prev) => [...prev, replyMessage]);
@@ -139,9 +134,9 @@ export const useChatActions = ({
// Update the system message with final response and error info
const updatedBubbles = [...filteredBubbles];
updatedBubbles[lastSystemMessageIndex].message = value;
- updatedBubbles[lastSystemMessageIndex].isReplying = false;
- updatedBubbles[lastSystemMessageIndex].isError = errorInfo?.isError || false;
- updatedBubbles[lastSystemMessageIndex].hideMessage = false; // Show the message bubble
+ updatedBubbles[lastSystemMessageIndex].type = errorInfo?.isError
+ ? 'error'
+ : 'system';
updatedBubbles[lastSystemMessageIndex].thinkingMessage = undefined; // Clear thinking message
return updatedBubbles;
}
@@ -154,18 +149,18 @@ export const useChatActions = ({
},
onThinking: (thinking) => {
setMessagesHistory((bubbles) => {
- // Find the last system message index
+ // Find the last system or loading message index
const lastSystemIndex =
bubbles
.map((msg, index) => ({ msg, index }))
- .filter(({ msg }) => msg.type === 'system')
+ .filter(({ msg }) => msg.type === 'system' || msg.type === 'loading')
.pop()?.index ?? -1;
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;
+ updatedBubbles[lastSystemIndex].type = 'system'; // Change from loading to system
return updatedBubbles;
}
@@ -176,10 +171,13 @@ export const useChatActions = ({
setMessagesHistory((prev) => {
const newMessages = [...prev];
const lastMessage = newMessages[newMessages.length - 1];
- if (lastMessage && lastMessage.type === 'system') {
+ if (
+ lastMessage &&
+ (lastMessage.type === 'system' || lastMessage.type === 'loading')
+ ) {
// Show initial processing message and loading indicator
lastMessage.message = '';
- lastMessage.isReplying = true; // Show loading indicator for system message
+ lastMessage.type = 'loading'; // Show loading state
}
return newMessages;
});
@@ -198,8 +196,8 @@ export const useChatActions = ({
});
const lastMessage = newMessages[newMessages.length - 1];
- if (lastMessage) {
- lastMessage.isReplying = false;
+ if (lastMessage && lastMessage.type === 'loading') {
+ lastMessage.type = 'system'; // Change from loading to system
}
return newMessages;
});
@@ -215,7 +213,7 @@ export const useChatActions = ({
const newMessages = [...prev];
const lastMessage = newMessages[newMessages.length - 1];
if (lastMessage) {
- lastMessage.isReplying = false;
+ lastMessage.type = 'system'; // Change from loading to system on abort
lastMessage.message += '';
}
return newMessages;
@@ -225,9 +223,8 @@ export const useChatActions = ({
const newMessages = [...prev];
const lastMessage = newMessages[newMessages.length - 1];
if (lastMessage) {
- lastMessage.isReplying = false;
+ lastMessage.type = 'error'; // Set error type
lastMessage.message = CHAT_ERROR_MESSAGE;
- lastMessage.isError = true;
}
return newMessages;
});
@@ -252,15 +249,12 @@ export const useChatActions = ({
const newMessages = [...prev];
const lastMessageIndex = newMessages.length - 1;
- if (lastMessageIndex >= 0 && newMessages[lastMessageIndex].isError) {
- // Replace the error message with a new AI response message
+ if (lastMessageIndex >= 0 && newMessages[lastMessageIndex].type === 'error') {
+ // Replace the error message with a new loading message
newMessages[lastMessageIndex] = {
- me: false,
message: '',
- type: 'system',
+ type: 'loading', // Type determines loading state
avatar,
- isReplying: true,
- isError: false,
};
}
@@ -306,9 +300,9 @@ export const useChatActions = ({
// Update the system message with final response and error info
const updatedBubbles = [...filteredBubbles];
updatedBubbles[lastSystemMessageIndex].message = value;
- updatedBubbles[lastSystemMessageIndex].isReplying = false;
- updatedBubbles[lastSystemMessageIndex].isError = errorInfo?.isError || false;
- updatedBubbles[lastSystemMessageIndex].hideMessage = false;
+ updatedBubbles[lastSystemMessageIndex].type = errorInfo?.isError
+ ? 'error'
+ : 'system';
return updatedBubbles;
}
return filteredBubbles;
@@ -320,54 +314,33 @@ export const useChatActions = ({
},
onThinking: (thinking) => {
setMessagesHistory((bubbles) => {
- // Find the last system message index
+ // Find the last system or loading message index
const lastSystemIndex =
bubbles
.map((msg, index) => ({ msg, index }))
- .filter(({ msg }) => msg.type === 'system')
+ .filter(({ msg }) => msg.type === 'system' || msg.type === 'loading')
.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,
- };
-
- 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].hideMessage = 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
+ const updatedBubbles = [...bubbles];
+ updatedBubbles[lastSystemIndex].thinkingMessage = thinking.message;
+ updatedBubbles[lastSystemIndex].type = 'system'; // Change from loading to system
+ return updatedBubbles;
}
+
+ return bubbles;
});
},
onStart: () => {
setMessagesHistory((prev) => {
const newMessages = [...prev];
const lastMessage = newMessages[newMessages.length - 1];
- if (lastMessage && lastMessage.type === 'system') {
- lastMessage.isReplying = true;
+ if (
+ lastMessage &&
+ (lastMessage.type === 'system' || lastMessage.type === 'loading')
+ ) {
+ lastMessage.type = 'loading'; // Show loading state
}
return newMessages;
});
@@ -378,8 +351,8 @@ export const useChatActions = ({
const filteredMessages = prev.filter((msg) => msg.type !== 'thinking');
const newMessages = [...filteredMessages];
const lastMessage = newMessages[newMessages.length - 1];
- if (lastMessage) {
- lastMessage.isReplying = false;
+ if (lastMessage && lastMessage.type === 'loading') {
+ lastMessage.type = 'system'; // Change from loading to system
}
return newMessages;
});
@@ -395,7 +368,7 @@ export const useChatActions = ({
const newMessages = [...prev];
const lastMessage = newMessages[newMessages.length - 1];
if (lastMessage) {
- lastMessage.isReplying = false;
+ lastMessage.type = 'system'; // Change from loading to system on abort
lastMessage.message += '';
}
return newMessages;
@@ -405,9 +378,8 @@ export const useChatActions = ({
const newMessages = [...prev];
const lastMessage = newMessages[newMessages.length - 1];
if (lastMessage) {
- lastMessage.isReplying = false;
+ lastMessage.type = 'error'; // Set error type
lastMessage.message = error.error || error.message || CHAT_ERROR_MESSAGE;
- lastMessage.isError = true;
}
return newMessages;
});