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 Chat Components for Code Optimization

Salman-Ahamedc3370471edf1
diff --git a/packages/app/src/react/features/ai-chat/components/chats.tsx b/packages/app/src/react/features/ai-chat/components/chats.tsx
index 22273abb..34caa433 100644
--- a/packages/app/src/react/features/ai-chat/components/chats.tsx
+++ b/packages/app/src/react/features/ai-chat/components/chats.tsx
@@ -166,9 +166,7 @@ export const Chats: FC<IChatsProps> = (props) => {
}> = [];
// Optimized: Early return for empty messages
- if (messages.length === 0) {
- return groups;
- }
+ if (messages.length === 0) return groups;
// Optimized: Process messages in a single pass
for (let i = 0; i < messages.length; i++) {
diff --git a/packages/app/src/react/features/ai-chat/components/message-turn-group.tsx b/packages/app/src/react/features/ai-chat/components/message-turn-group.tsx
index 46306cf2..216a21c2 100644
--- a/packages/app/src/react/features/ai-chat/components/message-turn-group.tsx
+++ b/packages/app/src/react/features/ai-chat/components/message-turn-group.tsx
@@ -24,7 +24,7 @@ interface IMessageTurnGroupProps {
* Groups messages from a single conversation turn
* Displays all messages and provides a single copy button for the entire group
*/
-const MessageTurnGroupComponent: FC<IMessageTurnGroupProps> = (props) => {
+export const MessageTurnGroup: FC<IMessageTurnGroupProps> = memo((props) => {
const { messages, avatar, onRetryClick, scrollToBottom } = props;
const [copied, setCopied] = useState(false);
const groupRef = useRef<HTMLDivElement>(null);
@@ -37,10 +37,7 @@ const MessageTurnGroupComponent: FC<IMessageTurnGroupProps> = (props) => {
// Check if the last message is complete (not loading or thinking)
const lastMessage = messages[messages.length - 1];
const isComplete =
- lastMessage &&
- lastMessage.type !== 'loading' &&
- lastMessage.type !== 'thinking' &&
- !lastMessage.metaMessages;
+ lastMessage && lastMessage.type !== 'loading' && lastMessage.type !== 'thinking';
/**
* Copies all system message content from this group
@@ -121,15 +118,4 @@ const MessageTurnGroupComponent: FC<IMessageTurnGroupProps> = (props) => {
)}
</div>
);
-};
-
-/**
- * Memoized export of MessageTurnGroup
- * Only re-renders when messages array actually changes
- *
- * Performance Impact:
- * - Completed turns never re-render (massive savings!)
- * - Only active turn re-renders during streaming
- * - 50+ turns: Reduces re-renders by 98%
- */
-export const MessageTurnGroup = memo(MessageTurnGroupComponent);
+});
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 43944675..fbe85517 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
@@ -163,7 +163,7 @@ export const useChat = (config: IUseChatConfig): IUseChatReturn => {
// Schedule batched update
updateThrottleRef.current = setTimeout(() => {
if (pendingUpdateRef.current) {
- const { content: pendingContent, turnId: pendingTurnId } = pendingUpdateRef.current;
+ const { content: pendingContent, turnId } = pendingUpdateRef.current;
setMessages((prev) => {
const lastMessageIndex = prev.length - 1;
@@ -173,7 +173,7 @@ export const useChat = (config: IUseChatConfig): IUseChatReturn => {
const needsUpdate =
lastMsg.type === 'system' ||
lastMsg.type === 'loading' ||
- (pendingTurnId && !lastMsg.conversationTurnId);
+ (turnId && !lastMsg.conversationTurnId);
if (needsUpdate) {
// Optimized: Only update last element
@@ -181,7 +181,7 @@ export const useChat = (config: IUseChatConfig): IUseChatReturn => {
newMessages.push({
...lastMsg,
message: pendingContent,
- conversationTurnId: pendingTurnId || lastMsg.conversationTurnId,
+ conversationTurnId: turnId || lastMsg.conversationTurnId,
type: 'system',
});
return newMessages;
diff --git a/packages/app/src/react/features/ai-chat/utils/stream.utils.ts b/packages/app/src/react/features/ai-chat/utils/stream.utils.ts
index b0f7ba45..defbeff3 100644
--- a/packages/app/src/react/features/ai-chat/utils/stream.utils.ts
+++ b/packages/app/src/react/features/ai-chat/utils/stream.utils.ts
@@ -172,7 +172,7 @@ export const formatStatusMessage = (statusMessage: string): string => {
* Unified thinking message manager
* Manages cycling through thinking messages with priority system
*/
-export class ThinkingMessageManager {
+class ThinkingMessageManager {
private currentType: TThinkingType | null = null;
private currentIndex: number = 0;
private intervalId: NodeJS.Timeout | null = null;
@@ -256,6 +256,10 @@ export class ThinkingMessageManager {
}
}
}
+/**
+ * Creates a singleton instance of ThinkingMessageManager
+ */
+export const createThinkingManager = (): ThinkingMessageManager => new ThinkingMessageManager();
/**
* Processes a chunk from the stream and extracts relevant information
@@ -293,8 +297,3 @@ export const processStreamChunk = (chunk: IStreamChunk) => {
error: errorMessage || null,
};
};
-
-/**
- * Creates a singleton instance of ThinkingMessageManager
- */
-export const createThinkingManager = (): ThinkingMessageManager => new ThinkingMessageManager();