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 AI Chat State Management and Scroll Behavior

Salman-Ahamed2b1af7539dff
diff --git a/packages/app/src/react/features/ai-chat/hooks/use-chat-state.ts b/packages/app/src/react/features/ai-chat/hooks/use-chat-state.ts
index d82da5aa..6584bfb2 100644
--- a/packages/app/src/react/features/ai-chat/hooks/use-chat-state.ts
+++ b/packages/app/src/react/features/ai-chat/hooks/use-chat-state.ts
@@ -1,13 +1,14 @@
import { ChatAPIClient } from '@react/features/ai-chat/clients/chat-api.client';
import { MESSAGE_TYPES } from '@react/features/ai-chat/constants';
import type {
+ IChatState,
TAttachment,
TChatError,
- IChatState,
- TChatStateConfig,
TChatMessage,
+ TChatStateConfig,
TMetaMessage,
} from '@react/features/ai-chat/types';
+import { forceScrollToBottomImmediate } from '@react/features/ai-chat/utils';
import { useCallback, useMemo, useRef, useState } from 'react';
export const useChatState = (options: TChatStateConfig): IChatState => {
@@ -301,23 +302,15 @@ export const useChatState = (options: TChatStateConfig): IChatState => {
);
const stopStreaming = useCallback((): void => {
- if (abortRef.current) {
- abortRef.current.abort();
- }
+ if (abortRef.current) abortRef.current.abort();
+
setIsStreaming(false);
setMessages((prev) => {
- const filtered = prev.filter((msg) => msg.type !== MESSAGE_TYPES.LOADING);
-
- const updated = filtered.map((msg) => {
- if (msg.metaMessages?.debugOn === true) {
- return {
- ...msg,
- metaMessages: { ...msg.metaMessages, debugOn: false },
- };
- }
- return msg;
- });
+ // Filter out LOADING and META messages when stopping stream
+ const filtered = prev.filter(
+ (msg) => msg.type !== MESSAGE_TYPES.LOADING && msg.type !== MESSAGE_TYPES.META,
+ );
const errorMessage: TChatMessage = {
id: Date.now() + Math.random(),
@@ -326,9 +319,14 @@ export const useChatState = (options: TChatStateConfig): IChatState => {
updatedAt: Date.now(),
};
- return [...updated, errorMessage];
+ return [...filtered, errorMessage];
});
+ // Scroll to bottom to show error message
+ setTimeout(() => {
+ forceScrollToBottomImmediate({ behavior: 'smooth', delay: 0 });
+ }, 100);
+
// Auto-focus input after stopping stream
inputRef?.current?.focus();
}, [inputRef]);
@@ -337,9 +335,11 @@ export const useChatState = (options: TChatStateConfig): IChatState => {
const lastUserMessage = messages.filter((msg) => msg.type === MESSAGE_TYPES.USER).pop();
if (lastUserMessage) {
- setMessages((prev) =>
- prev.filter((msg) => msg.type !== MESSAGE_TYPES.ERROR && msg.id !== lastUserMessage.id),
- );
+ // Find the index of the last user message
+ const lastUserMessageIndex = messages.findIndex((msg) => msg.id === lastUserMessage.id);
+
+ // Remove the user message and all messages after it (system response, error, meta, etc.)
+ setMessages((prev) => prev.slice(0, lastUserMessageIndex));
const content = typeof lastUserMessage.content === 'string' ? lastUserMessage.content : '';
sendMessage(content, lastUserMessage.attachments || [], true);
diff --git a/packages/app/src/react/features/ai-chat/hooks/use-scroll.ts b/packages/app/src/react/features/ai-chat/hooks/use-scroll.ts
index f4820d63..a821279d 100644
--- a/packages/app/src/react/features/ai-chat/hooks/use-scroll.ts
+++ b/packages/app/src/react/features/ai-chat/hooks/use-scroll.ts
@@ -2,13 +2,24 @@ import { RefObject, useCallback, useEffect, useState } from 'react';
import { scrollManager } from '@react/features/ai-chat/utils';
+/**
+ * Hook for managing scroll-to-bottom behavior in chat interface
+ *
+ * @param ref - Reference to the scrollable container element
+ * @returns Scroll state and control functions
+ */
export const useScrollToBottom = (ref: RefObject<HTMLElement>) => {
const [showScrollButton, setShowScrollButton] = useState(false);
+ // Initialize scroll manager with container reference
useEffect(() => {
if (ref.current) scrollManager.init(ref.current);
}, [ref]);
+ /**
+ * Handles scroll events to determine if scroll button should be shown
+ * Ignores scroll events when user has text selected
+ */
const handleScroll = useCallback(() => {
const selection = window.getSelection();
if (selection && selection.toString().length > 0) return;
@@ -22,22 +33,27 @@ export const useScrollToBottom = (ref: RefObject<HTMLElement>) => {
}
}, [ref]);
- const scrollToBottom = useCallback(
- (smooth: boolean = true) => {
- if (ref.current) {
- ref.current.scrollTo({
- top: ref.current.scrollHeight,
- behavior: smooth ? 'smooth' : 'auto',
- });
- }
- },
- [ref],
- );
+ /**
+ * Force scrolls to bottom regardless of current position
+ * Used for user-initiated scroll actions (button clicks)
+ */
+ const scrollToBottom = useCallback((smooth: boolean = true) => {
+ scrollManager.forceScrollToBottomImmediate({
+ behavior: smooth ? 'smooth' : 'auto',
+ delay: 0,
+ });
+ }, []);
+ /**
+ * Smart scroll that respects user's scroll position
+ * Only scrolls if user is near bottom (within 200px threshold)
+ * Used for content updates during streaming
+ */
const smartScrollToBottom = useCallback((smooth: boolean = true) => {
scrollManager.smartScrollToBottom({ behavior: smooth ? 'smooth' : 'auto', delay: 0 });
}, []);
+ // Attach scroll event listener
useEffect(() => {
const container = ref.current;
if (container) {