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.
JavaScript a2aagentic-aiai-agentsdevtoolsmcp-clientn8n-alternativeno-codeopen-sourceorchestrationsmythosvisual-builder
Refactor Chat Input and Context for Improved State Management and Code Clarity
Salman-Ahamedbda2b27d4f7e
diff --git a/packages/app/src/react/features/ai-chat/components/input.tsx b/packages/app/src/react/features/ai-chat/components/input.tsxindex d4f23ae5..f292d06c 100644--- a/packages/app/src/react/features/ai-chat/components/input.tsx+++ b/packages/app/src/react/features/ai-chat/components/input.tsx@@ -20,28 +20,22 @@ import {import { MAX_CHAT_MESSAGE_LENGTH } from '@react/shared/constants';import { cn } from '@react/shared/utils/general';-export interface ChatInputRef {- focus: () => void;- getValue: () => string;- setValue: (content: string) => void; // eslint-disable-line no-unused-vars-}-const TEXTAREA_MAX_HEIGHT = 160;const LARGE_TEXT_THRESHOLD = 4000;export const ChatInput = () => {const { ref: allRefs, files: filesData, chat, agent } = useChatStores();+ const [message, setMessage] = useState('');+ const inputRef = useRef<HTMLTextAreaElement>(null);+ const fileInputRef = useRef<HTMLInputElement>(null);- const chatInputRef = allRefs?.input;const { isStreaming, isChatCreating, isProcessing, sendMessage, stopGenerating } = chat || {};const { data: files, addFiles, removeFile, clearFiles } = filesData || {};const maxLength = MAX_CHAT_MESSAGE_LENGTH;const isFilesLoading = filesData?.isLoading;const isMaxFilesUploaded = files.length >= 10;- const inputDisabled = isChatCreating || agent.isLoading.agent || isProcessing;- const inputPlaceholder = agent.data?.name ? `Message ${agent.data.name}...` : 'Message ...';- const submitDisabled = chat.isChatCreating || agent.isLoading.agent || isFilesLoading;+ const isDisabled = isChatCreating || agent.isLoading.agent;const handleFileChange = useCallback((e: ChangeEvent<HTMLInputElement>) => {@@ -51,10 +45,6 @@ export const ChatInput = () => {[addFiles],);- const [message, setMessage] = useState('');- const inputRef = useRef<HTMLTextAreaElement>(null);- const fileInputRef = useRef<HTMLInputElement>(null);-// Debounce ref for textarea height adjustmentconst heightAdjustmentTimerRef = useRef<NodeJS.Timeout | null>(null);@@ -91,7 +81,7 @@ export const ChatInput = () => {}, [adjustTextareaHeight]);useImperativeHandle(- chatInputRef,+ allRefs?.input,() => ({focus: () => inputRef.current?.focus(),getValue: () => message,@@ -177,7 +167,7 @@ export const ChatInput = () => {forceScrollToBottomImmediate({ behavior: 'smooth', delay: 0 });}, 150); // 150ms delay to ensure DOM is updated}- }, [message, files, isStreaming, inputDisabled, adjustTextareaHeight]); // eslint-disable-line react-hooks/exhaustive-deps+ }, [message, files, isStreaming, isDisabled, adjustTextareaHeight]); // eslint-disable-line react-hooks/exhaustive-depsconst handleKeyDown = useCallback((e: KeyboardEvent<HTMLTextAreaElement>) => {@@ -222,7 +212,7 @@ export const ChatInput = () => {const isMaxLengthReached = message.length === maxLength;const canSubmit =- !submitDisabled && (message.trim().length > 0 || isStreaming || files.length > 0);+ !isDisabled && isFilesLoading && (message.trim().length > 0 || isStreaming || files.length > 0);const handleContainerClick = () => inputRef.current?.focus();@@ -268,8 +258,8 @@ export const ChatInput = () => {value={message}onChange={handleChange}onKeyDown={handleKeyDown}- placeholder={inputPlaceholder}maxLength={maxLength}+ placeholder={`Message ${agent.data?.name || ''}...`}className="bg-white border-none outline-none ring-0 focus:outline-none focus:border-none flex-1 max-h-36 resize-none ph-no-capture text-[16px] font-[400] text-gray-900 placeholder:text-gray-500 placeholder:text-[16px] placeholder:font-[400]"aria-label="Message input"onClick={(e) => e.stopPropagation()}@@ -289,9 +279,7 @@ export const ChatInput = () => {<div onClick={(e) => e.stopPropagation()}><AttachmentButtononClick={handleAttachmentClick}- fileAttachmentDisabled={- isMaxFilesUploaded || inputDisabled || isProcessing || isStreaming- }+ fileAttachmentDisabled={isMaxFilesUploaded || isDisabled || isProcessing || isStreaming}isMaxFilesUploaded={isMaxFilesUploaded}/></div>diff --git a/packages/app/src/react/features/ai-chat/contexts/chat-context.tsx b/packages/app/src/react/features/ai-chat/contexts/chat-context.tsxindex 37e022d9..5fb53237 100644--- a/packages/app/src/react/features/ai-chat/contexts/chat-context.tsx+++ b/packages/app/src/react/features/ai-chat/contexts/chat-context.tsx@@ -1,6 +1,5 @@/* eslint-disable no-unused-vars */-import { ChatInputRef } from '@react/features/ai-chat/components/input';-import { IChatMessage, IMessageFile } from '@react/features/ai-chat/types/chat.types';+import { ChatInputRef, IChatMessage, IMessageFile } from '@react/features/ai-chat/types/chat.types';import { AgentDetails, AgentSettings } from '@react/shared/types/agent-data.types';import { createContext, Dispatch, MutableRefObject, SetStateAction } from 'react';diff --git a/packages/app/src/react/features/ai-chat/contexts/index.new.tsx b/packages/app/src/react/features/ai-chat/contexts/index.new.tsxindex d4134030..82df322b 100644--- a/packages/app/src/react/features/ai-chat/contexts/index.new.tsx+++ b/packages/app/src/react/features/ai-chat/contexts/index.new.tsx@@ -1,18 +1,18 @@-import { ChatInputRef } from '@react/features/ai-chat/components/input';import { ChatContext, IChatContext } from '@react/features/ai-chat/contexts/chat-context';import {useAgentSettings,useAttachments,+ useChat,useCreateChatMutation,useScrollToBottom,useUpdateAgentSettingsMutation,} from '@react/features/ai-chat/hooks';+import { ChatInputRef } from '@react/features/ai-chat/types/chat.types';import { useAgent } from '@react/shared/hooks/agent';import { Observability } from '@shared/observability';import { EVENTS } from '@shared/posthog/constants/events';import { FC, ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react';import { useNavigate, useParams } from 'react-router-dom';-import { useChat } from '../hooks/use-chat';interface IProps {children: ReactNode;diff --git a/packages/app/src/react/features/ai-chat/hooks/use-agent-chat-context.ts b/packages/app/src/react/features/ai-chat/hooks/use-agent-chat-context.tsindex 6ceac31d..54d7fb3a 100644--- a/packages/app/src/react/features/ai-chat/hooks/use-agent-chat-context.ts+++ b/packages/app/src/react/features/ai-chat/hooks/use-agent-chat-context.ts@@ -8,11 +8,10 @@ import {useUpdateAgentSettingsMutation,} from '@react/features/ai-chat/hooks';import { useChat } from '@react/features/ai-chat/hooks/use-chat';-import { IChatMessage, IMessageFile } from '@react/features/ai-chat/types/chat.types';+import { ChatInputRef, IChatMessage, IMessageFile } from '@react/features/ai-chat/types/chat.types';import { useAgent } from '@react/shared/hooks/agent';import { Observability } from '@shared/observability';import { EVENTS } from '@shared/posthog/constants/events';-import { ChatInputRef } from '../components';/*** Configuration options for useAgentChatContext hookdiff --git a/packages/app/src/react/features/ai-chat/pages/ai-chat.tsx b/packages/app/src/react/features/ai-chat/pages/ai-chat.tsxindex 879fa149..527d8c08 100644--- a/packages/app/src/react/features/ai-chat/pages/ai-chat.tsx+++ b/packages/app/src/react/features/ai-chat/pages/ai-chat.tsx@@ -1,16 +1,11 @@import { useEffect, useRef } from 'react';import { useParams } from 'react-router-dom';-import {- ChatHeader,- ChatInputRef,- Chats,- Container,- Footer,-} from '@react/features/ai-chat/components';+import { ChatHeader, Chats, Container, Footer } from '@react/features/ai-chat/components';import { ChatProvider } from '@react/features/ai-chat/contexts';import { ChatContextProvider } from '@react/features/ai-chat/contexts/index.new';import { useAgentChatContext } from '@react/features/ai-chat/hooks';+import { ChatInputRef } from '@react/features/ai-chat/types/chat.types';/*** Agent Chat Page Componentdiff --git a/packages/app/src/react/features/ai-chat/types/chat.types.ts b/packages/app/src/react/features/ai-chat/types/chat.types.tsindex 2e1c1347..264140a9 100644--- a/packages/app/src/react/features/ai-chat/types/chat.types.ts+++ b/packages/app/src/react/features/ai-chat/types/chat.types.ts@@ -180,3 +180,9 @@ export interface IAPIConfig {timeout?: number; // Request timeoutretry?: { attempts: number; delay: number }; // Retry configuration}++export interface ChatInputRef {+ focus: () => void;+ getValue: () => string;+ setValue: (content: string) => void;+}