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 AI Chat Components for Improved Structure and Type Safety

Salman-Ahamed6a57ff73eb91
diff --git a/packages/app/src/react/features/ai-chat/components/chat/chat.tsx b/packages/app/src/react/features/ai-chat/components/chat/chat.tsx
index 5a0b8bab..6d563cb8 100644
--- a/packages/app/src/react/features/ai-chat/components/chat/chat.tsx
+++ b/packages/app/src/react/features/ai-chat/components/chat/chat.tsx
@@ -1,38 +1,30 @@
import { FC, memo } from 'react';
-import {
- ErrorMessage,
- Loading,
- MetaMessage,
- SystemMessage,
- UserMessage,
-} from '@react/features/ai-chat/components';
+import { Error, Loading, Meta, System, User } from '@react/features/ai-chat/components';
import { MESSAGE_TYPES } from '@react/features/ai-chat/constants';
import { TChatMessage } from '@react/features/ai-chat/types';
-import '../../styles/index.css';
-
-interface IChatProps extends TChatMessage {
+interface IProps extends TChatMessage {
avatar: string;
- onRetryClick?: () => void;
+ retry?: () => void;
scrollToBottom?: () => void;
}
-export const Chat: FC<IChatProps> = memo((props) => {
- const { type, attachments, avatar, content, metaMessages, onRetryClick, scrollToBottom } = props;
+export const Chat: FC<IProps> = memo((props) => {
+ const { type, attachments, avatar, content, metaMessages, retry, scrollToBottom } = props;
switch (type) {
case MESSAGE_TYPES.USER:
- return <UserMessage message={content} files={attachments} />;
+ return <User message={content} files={attachments} />;
case MESSAGE_TYPES.LOADING:
return <Loading />;
case MESSAGE_TYPES.META:
- return <MetaMessage data={{ avatar, metaMessages }} scrollToBottom={scrollToBottom} />;
+ return <Meta data={{ avatar, metaMessages }} scrollToBottom={scrollToBottom} />;
case MESSAGE_TYPES.SYSTEM:
- return <SystemMessage message={content} scrollToBottom={scrollToBottom} />;
+ return <System message={content} onUpdate={scrollToBottom} />;
case MESSAGE_TYPES.ERROR:
- return <ErrorMessage message={content} onRetryClick={onRetryClick} />;
+ return <Error message={content} onRetryClick={retry} />;
default:
- return <ErrorMessage message="Something went wrong!" onRetryClick={onRetryClick} />;
+ return <Error message="Something went wrong!" onRetryClick={retry} />;
}
});
diff --git a/packages/app/src/react/features/ai-chat/components/chat/chats.tsx b/packages/app/src/react/features/ai-chat/components/chat/chats.tsx
index 292211a5..bd7e14eb 100644
--- a/packages/app/src/react/features/ai-chat/components/chat/chats.tsx
+++ b/packages/app/src/react/features/ai-chat/components/chat/chats.tsx
@@ -1,59 +1,33 @@
import { Chat } from '@react/features/ai-chat/components';
import { useChatStores, useDragAndDrop } from '@react/features/ai-chat/hooks';
-import { FC, MutableRefObject, RefObject, useRef } from 'react';
+import { FC } from 'react';
-/**
- * Combines multiple refs into a single ref callback
- * Useful for managing multiple refs on the same element
- *
- * @param refs - Array of refs to combine
- * @returns Combined ref callback
- */
-const combineRefs =
- <T extends HTMLElement>(...refs: Array<RefObject<T> | MutableRefObject<T | null>>) =>
- (element: T | null) => {
- refs.forEach((ref) => {
- if (!ref) return;
- (ref as MutableRefObject<T | null>).current = element;
- });
- };
-
-/**
- * Chats Component
- * Renders chat messages with smart auto-scroll and drag-and-drop support
- */
export const Chats: FC = () => {
- const { ref: allRefs, agent: agentData, chat, files, scroll } = useChatStores() || {};
+ const { refs, agent, chat, files, scroll } = useChatStores() || {};
+ const { container: containerRef } = refs || {};
const { messages, retryMessage } = chat || {};
const { handleScroll, smartScrollToBottom } = scroll || {};
- const agent = agentData?.data;
- const containerRef = allRefs?.container;
- const handleFileDrop = files.addFiles;
+ const avatar = agent.settings?.avatar;
- const ref = useRef<HTMLDivElement>(null);
- const dropzoneRef = useDragAndDrop({ onDrop: handleFileDrop });
+ useDragAndDrop({ ref: containerRef, onDrop: files.addFiles });
return (
<div
data-chat-container
onScroll={handleScroll}
- ref={combineRefs(containerRef, dropzoneRef)}
+ ref={containerRef}
className="w-full h-full overflow-auto relative scroll-smooth mt-16 flex justify-center items-center"
>
- <div
- ref={ref}
- onScroll={handleScroll}
- className="w-full h-full max-w-4xl flex-1 pb-10 space-y-3.5"
- >
- {messages.map((message, index) => (
+ <div className="w-full h-full max-w-4xl flex-1 pb-10 space-y-3.5">
+ {messages.map((msg, i) => (
<Chat
- {...message}
- key={message.id}
- avatar={agent?.aiAgentSettings?.avatar}
+ {...msg}
+ key={msg.id}
+ avatar={avatar}
scrollToBottom={smartScrollToBottom}
- onRetryClick={index === messages.length - 1 ? retryMessage : undefined}
+ retry={i === messages.length - 1 ? retryMessage : undefined}
/>
))}
</div>
diff --git a/packages/app/src/react/features/ai-chat/components/chat/error-message.tsx b/packages/app/src/react/features/ai-chat/components/chat/error-message.tsx
deleted file mode 100644
index 111cb63e..00000000
--- a/packages/app/src/react/features/ai-chat/components/chat/error-message.tsx
+++ /dev/null
@@ -1,84 +0,0 @@
-import { FC } from 'react';
-import ReactMarkdown from 'react-markdown';
-import remarkGfm from 'remark-gfm';
-
-interface IErrorMessage {
- message: string;
- onRetryClick: () => void;
- isRetrying?: boolean;
-}
-
-export const ErrorMessage: FC<IErrorMessage> = ({ message, onRetryClick, isRetrying }) => {
- const isApiKeyError =
- message.includes('Incorrect API key provided') ||
- (message.includes('401') &&
- message.toLowerCase().includes('api') &&
- message.toLocaleLowerCase().includes('key'));
-
- return (
- <div className="flex flex-col items-start">
- <div className="rounded-lg bg-pink-50 border border-pink-200 p-4 max-w-screen-md flex justify-between items-center gap-5">
- <div className="flex items-start gap-3">
- <div className="flex-shrink-0 mt-0.5">
- <svg
- width="20"
- height="20"
- viewBox="0 0 20 20"
- fill="currentColor"
- xmlns="http://www.w3.org/2000/svg"
- className="text-red-500"
- >
- <path d="M10 9.25C10.4142 9.25002 10.75 9.5858 10.75 10V13.333C10.75 13.7472 10.4142 14.083 10 14.083C9.58579 14.083 9.25 13.7472 9.25 13.333V10C9.25 9.58579 9.58579 9.25 10 9.25Z"></path>
- <path d="M10 5.83301C10.5293 5.83303 10.958 6.26273 10.958 6.79199C10.9578 7.3211 10.5291 7.74998 10 7.75C9.47084 7.75 9.04217 7.32111 9.04199 6.79199C9.04199 6.26272 9.47073 5.83301 10 5.83301Z"></path>
- <path
- fillRule="evenodd"
- clipRule="evenodd"
- d="M10 2.08496C14.3713 2.08496 17.915 5.62867 17.915 10C17.915 14.3713 14.3713 17.915 10 17.915C5.62867 17.915 2.08496 14.3713 2.08496 10C2.08496 5.62867 5.62867 2.08496 10 2.08496ZM10 3.41504C6.3632 3.41504 3.41504 6.3632 3.41504 10C3.41504 13.6368 6.3632 16.585 10 16.585C13.6368 16.585 16.585 13.6368 16.585 10C16.585 6.3632 13.6368 3.41504 10 3.41504Z"
- ></path>
- </svg>
- </div>
-
- <div className="flex-1 text-red-700 text-sm leading-relaxed">
- {isApiKeyError && (
- <>
- <h6 className="font-bold">Invalid API Key</h6>
- <p>
- The API key you provided is not valid. Please set a valid key to continue:&nbsp;
- <a
- href="/vault"
- target="_blank"
- className="text-red-700 hover:text-red-900 font-semibold"
- >
- Set API Key
- </a>
- </p>
- <h6 className="font-semibold pt-1">Error details:</h6>
- </>
- )}
- <ReactMarkdown remarkPlugins={[remarkGfm]}>{message}</ReactMarkdown>
- </div>
- </div>
- {onRetryClick && !isRetrying && (
- <button
- disabled={isRetrying}
- onClick={onRetryClick}
- className="inline-flex items-center px-4 gap-x-1 py-2 border border-gray-300 text-sm font-medium rounded-[18px] text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50 disabled:cursor-not-allowed transition-colors shadow-sm"
- >
- <svg
- width="20"
- height="20"
- viewBox="0 0 20 20"
- fill="currentColor"
- xmlns="http://www.w3.org/2000/svg"
- aria-label=""
- className="-ms-0.5 icon"
- >
- <path d="M16.4183 9.99967C16.4181 6.45518 13.5448 3.58188 10.0003 3.58171C7.96895 3.58171 6.15935 4.52712 4.98273 6.00163H7.50031L7.6341 6.0153C7.93707 6.07735 8.16535 6.34535 8.16535 6.66667C8.16535 6.98799 7.93707 7.25598 7.6341 7.31803L7.50031 7.33171H3.75031C3.73055 7.33171 3.71104 7.32656 3.69172 7.32487C3.68913 7.32464 3.68649 7.32513 3.68391 7.32487C3.64304 7.32082 3.60409 7.31254 3.56574 7.30143C3.56188 7.30031 3.55788 7.2997 3.55402 7.2985C3.51366 7.286 3.47546 7.27023 3.43879 7.25065L3.43586 7.24967C3.43444 7.24891 3.43337 7.24752 3.43195 7.24675C3.35757 7.20587 3.29219 7.15262 3.23859 7.08757C3.23505 7.08329 3.23128 7.07923 3.22785 7.07487C3.20549 7.04631 3.1858 7.01609 3.16828 6.98405C3.16491 6.97791 3.16169 6.97173 3.15852 6.9655C3.14408 6.93698 3.13265 6.90732 3.12238 6.87663C3.11744 6.86205 3.11264 6.84757 3.10871 6.83268C3.10316 6.81117 3.09843 6.78955 3.09504 6.76725C3.09108 6.74233 3.08833 6.71738 3.08723 6.69206C3.08691 6.68361 3.08527 6.67519 3.08527 6.66667V2.91667C3.08527 2.5494 3.38304 2.25163 3.75031 2.25163C4.11743 2.2518 4.41535 2.54951 4.41535 2.91667V4.62956C5.82462 3.16447 7.80565 2.25163 10.0003 2.25163C14.2793 2.2518 17.7482 5.72065 17.7484 9.99967C17.7484 14.2789 14.2794 17.7485 10.0003 17.7487C6.02899 17.7487 2.75629 14.7612 2.305 10.9108L2.96516 10.8337L3.62531 10.7555C3.99895 13.9437 6.7115 16.4186 10.0003 16.4186C13.5449 16.4184 16.4183 13.5443 16.4183 9.99967ZM2.88801 10.1725C3.25252 10.13 3.58237 10.3911 3.62531 10.7555L2.305 10.9108C2.26225 10.546 2.52323 10.2153 2.88801 10.1725Z"></path>
- </svg>
- {isRetrying ? 'Retrying...' : 'Retry'}
- </button>
- )}
- </div>
- </div>
- );
-};
diff --git a/packages/app/src/react/features/ai-chat/components/chat/index.ts b/packages/app/src/react/features/ai-chat/components/chat/index.ts
index c6cc82c0..85e57a30 100644
--- a/packages/app/src/react/features/ai-chat/components/chat/index.ts
+++ b/packages/app/src/react/features/ai-chat/components/chat/index.ts
@@ -1,10 +1,7 @@
export * from './chat';
export * from './chats';
export * from './code-block';
-export * from './error-message';
-export * from './markdown-renderer';
export * from './loading';
-export * from './system-message';
-export * from './thinking-message';
+export * from './markdown-renderer';
+export * from './message';
export * from './typewriter';
-export * from './user-message';
diff --git a/packages/app/src/react/features/ai-chat/components/chat/markdown-renderer.tsx b/packages/app/src/react/features/ai-chat/components/chat/markdown-renderer.tsx
index 1d1f6300..45f3093a 100644
--- a/packages/app/src/react/features/ai-chat/components/chat/markdown-renderer.tsx
+++ b/packages/app/src/react/features/ai-chat/components/chat/markdown-renderer.tsx
@@ -3,18 +3,14 @@ import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import { CodeBlock } from '@react/features/ai-chat/components';
-import { cn } from '@src/react/shared/utils/general';
+import { cn } from '@react/shared/utils/general';
-interface IMarkdownRendererProps {
+type TProps = {
message: string;
className?: string;
-}
+};
-/**
- * Shared Markdown renderer component with consistent styling
- * Used by both SystemMessage and Typewriter components
- */
-export const MarkdownRenderer: FC<IMarkdownRendererProps> = ({ message, className }) => {
+export const MarkdownRenderer: FC<TProps> = ({ message, className }) => {
return (
<div className={className}>
<ReactMarkdown
diff --git a/packages/app/src/react/features/ai-chat/components/chat/message/error.tsx b/packages/app/src/react/features/ai-chat/components/chat/message/error.tsx
new file mode 100644
index 00000000..8b83040c
--- /dev/null
+++ b/packages/app/src/react/features/ai-chat/components/chat/message/error.tsx
@@ -0,0 +1,58 @@
+import { ErrorInfoIcon, RetryIcon } from '@react/features/ai-chat/components';
+import { FC } from 'react';
+import ReactMarkdown from 'react-markdown';
+import remarkGfm from 'remark-gfm';
+
+type TProps = {
+ message: string;
+ onRetryClick: () => void;
+};
+
+export const Error: FC<TProps> = ({ message, onRetryClick }) => {
+ const isApiKeyError =
+ message.includes('Incorrect API key provided') ||
+ (message.includes('401') &&
+ message.toLowerCase().includes('api') &&
+ message.toLocaleLowerCase().includes('key'));
+
+ return (
+ <div className="flex flex-col items-start">
+ <div className="rounded-lg bg-pink-50 border border-pink-200 p-4 max-w-screen-md flex justify-between items-center gap-5">
+ <div className="flex items-start gap-3">
+ <div className="flex-shrink-0 mt-0.5">
+ <ErrorInfoIcon />
+ </div>
+
+ <div className="flex-1 text-red-700 text-sm leading-relaxed">
+ {isApiKeyError && (
+ <>
+ <h6 className="font-bold">Invalid API Key</h6>
+ <p>
+ The API key you provided is not valid. Please set a valid key to continue:&nbsp;
+ <a
+ href="/vault"
+ target="_blank"
+ className="text-red-700 hover:text-red-900 font-semibold"
+ >
+ Set API Key
+ </a>
+ </p>
+ <h6 className="font-semibold pt-1">Error details:</h6>
+ </>
+ )}
+ <ReactMarkdown remarkPlugins={[remarkGfm]}>{message}</ReactMarkdown>
+ </div>
+ </div>
+ {onRetryClick && (
+ <button
+ onClick={onRetryClick}
+ className="inline-flex items-center px-4 gap-x-1 py-2 border border-gray-300 text-sm font-medium rounded-[18px] text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50 disabled:cursor-not-allowed transition-colors shadow-sm"
+ >
+ <RetryIcon />
+ Retry
+ </button>
+ )}
+ </div>
+ </div>
+ );
+};
diff --git a/packages/app/src/react/features/ai-chat/components/chat/message/index.ts b/packages/app/src/react/features/ai-chat/components/chat/message/index.ts
new file mode 100644
index 00000000..dfc8125c
--- /dev/null
+++ b/packages/app/src/react/features/ai-chat/components/chat/message/index.ts
@@ -0,0 +1,4 @@
+export * from './error';
+export * from './meta';
+export * from './system';
+export * from './user';
diff --git a/packages/app/src/react/features/ai-chat/components/chat/message/meta.tsx b/packages/app/src/react/features/ai-chat/components/chat/message/meta.tsx
new file mode 100644
index 00000000..b06e3702
--- /dev/null
+++ b/packages/app/src/react/features/ai-chat/components/chat/message/meta.tsx
@@ -0,0 +1,57 @@
+import { TMetaMessage } from '@react/features/ai-chat/types';
+import { createThinkingManager, formatStatusMessage } from '@react/features/ai-chat/utils';
+import { FC, useEffect, useRef, useState } from 'react';
+
+type TProps = {
+ scrollToBottom: () => void;
+ data: { avatar?: string; metaMessages?: TMetaMessage };
+};
+
+export const Meta: FC<TProps> = ({ data, scrollToBottom }) => {
+ const { avatar, metaMessages } = data;
+
+ const [thinkingMessage, setThinkingMessage] = useState<string>('');
+ const thinkingManagerRef = useRef(createThinkingManager());
+
+ useEffect(() => {
+ scrollToBottom?.();
+ }, [scrollToBottom]);
+
+ useEffect(() => {
+ const manager = thinkingManagerRef.current;
+
+ if (metaMessages?.statusMessage) {
+ const formatted = formatStatusMessage(metaMessages.statusMessage);
+ setThinkingMessage(formatted);
+ manager.stop();
+ return;
+ }
+
+ const toolName = metaMessages?.title || metaMessages?.functionCall?.name;
+ manager.start(setThinkingMessage, toolName || '');
+
+ return () => manager.stop();
+ }, [metaMessages]);
+
+ return (
+ <div className="flex items-center gap-3 px-3">
+ <div className="flex-shrink-0">
+ <div className="size-6 rounded-full bg-gray-200 flex items-center justify-center overflow-hidden">
+ {avatar ? (
+ <img src={avatar} alt="AI Avatar" className="w-full h-full object-cover" />
+ ) : (
+ <div className="w-full h-full bg-gradient-to-br from-blue-400 to-purple-500 flex items-center justify-center">
+ <span className="text-white text-xs font-semibold">AI</span>
+ </div>
+ )}
+ </div>
+ </div>
+
+ <div className="flex-1">
+ <span className="text-sm text-gray-500 font-medium smooth-blink-text">
+ {thinkingMessage}
+ </span>
+ </div>
+ </div>
+ );
+};
diff --git a/packages/app/src/react/features/ai-chat/components/chat/message/system.tsx b/packages/app/src/react/features/ai-chat/components/chat/message/system.tsx
new file mode 100644
index 00000000..0d38a0ea
--- /dev/null
+++ b/packages/app/src/react/features/ai-chat/components/chat/message/system.tsx
@@ -0,0 +1,23 @@
+import { FC } from 'react';
+
+import { MarkdownRenderer, Typewriter } from '@react/features/ai-chat/components';
+
+type TProps = {
+ message: string;
+ enableTypewriter?: boolean;
+ onUpdate?: () => void;
+};
+
+export const System: FC<TProps> = ({ message, onUpdate, enableTypewriter = true }) => {
+ if (!message) return null;
+
+ return (
+ <div className="system-message-bubble relative chat-rich-text-response rounded-lg px-3 text-[#141414]">
+ {enableTypewriter ? (
+ <Typewriter message={message} onUpdate={onUpdate} />
+ ) : (
+ <MarkdownRenderer message={message} />
+ )}
+ </div>
+ );
+};
diff --git a/packages/app/src/react/features/ai-chat/components/chat/user-message.tsx b/packages/app/src/react/features/ai-chat/components/chat/message/user.tsx
similarity index 70%
rename from packages/app/src/react/features/ai-chat/components/chat/user-message.tsx
rename to packages/app/src/react/features/ai-chat/components/chat/message/user.tsx
index d9daf1b4..a71a7d4d 100644
--- a/packages/app/src/react/features/ai-chat/components/chat/user-message.tsx
+++ b/packages/app/src/react/features/ai-chat/components/chat/message/user.tsx
@@ -2,28 +2,31 @@ import { FileItemPreview } from '@react/features/ai-chat/components';
import type { TAttachment } from '@react/features/ai-chat/types';
import { FC } from 'react';
-type TUserMessageProps = {
+type TProps = {
message: string;
files?: TAttachment[];
};
-export const UserMessage: FC<TUserMessageProps> = ({ message, files }) => {
+export const User: FC<TProps> = ({ message, files }) => {
const hasFiles = files && files.length > 0;
+ const hasMessage = message.trim().length > 0;
+
+ if (!hasFiles && !hasMessage) return null;
return (
<div className="break-all flex flex-col items-end">
{hasFiles && (
<div className="flex flex-nowrap gap-2 mb-2 overflow-x-auto">
- {files.map((attachment, index) => (
+ {files.map((attachment) => (
<FileItemPreview
- key={`${attachment.name}-${index}-${attachment.url || attachment.blobUrl || index}`}
+ key={attachment.blobUrl || attachment.url || attachment.name}
attachment={attachment}
/>
))}
</div>
)}
- {message && (
+ {hasMessage && (
<div className="rounded-[18px] bg-[#f4f4f4] text-[#2b2b2b] p-3 px-4 w-fit whitespace-pre-wrap text-wrap max-w-[535px]">
{message}
</div>
diff --git a/packages/app/src/react/features/ai-chat/components/chat/system-message.tsx b/packages/app/src/react/features/ai-chat/components/chat/system-message.tsx
deleted file mode 100644
index 16886074..00000000
--- a/packages/app/src/react/features/ai-chat/components/chat/system-message.tsx
+++ /dev/null
@@ -1,35 +0,0 @@
-import { FC, useRef } from 'react';
-
-import { MarkdownRenderer, Typewriter } from '@react/features/ai-chat/components';
-
-interface ISystemMessageProps {
- message: string;
- typingAnimation?: boolean;
- scrollToBottom?: () => void;
-}
-
-export const SystemMessage: FC<ISystemMessageProps> = (props) => {
- const { message, scrollToBottom, typingAnimation = true } = props;
-
- const contentRef = useRef<HTMLDivElement>(null);
-
- if (!message) return null; // if message is empty, return null
-
- return (
- <div
- ref={contentRef}
- className="system-message-bubble relative chat-rich-text-response rounded-lg px-3 text-[#141414]"
- >
- {typingAnimation ? (
- <Typewriter
- message={message}
- isTyping={typingAnimation}
- onComplete={scrollToBottom}
- onTypingProgress={scrollToBottom}
- />
- ) : (
- <MarkdownRenderer message={message} />
- )}
- </div>
- );
-};
diff --git a/packages/app/src/react/features/ai-chat/components/chat/thinking-message.tsx b/packages/app/src/react/features/ai-chat/components/chat/thinking-message.tsx
deleted file mode 100644
index c912438b..00000000
--- a/packages/app/src/react/features/ai-chat/components/chat/thinking-message.tsx
+++ /dev/null
@@ -1,79 +0,0 @@
-import {
- createThinkingManager,
- formatStatusMessage,
-} from '@src/react/features/ai-chat/utils/stream';
-import { FC, useEffect, useRef, useState } from 'react';
-import { TMetaMessage } from '../../types';
-
-interface IProps {
- data: {
- avatar?: string;
- metaMessages?: TMetaMessage;
- };
- scrollToBottom: () => void;
-}
-
-const blinkAnimation = `
- @keyframes smoothBlink {
- 0% { opacity: 1; }
- 50% { opacity: 0.3; }
- 100% { opacity: 1; }
- }
- .smooth-blink-text {
- animation: smoothBlink 1.5s ease-in-out infinite;
- color: #6b7280;
- font-weight: 500;
- }
-`;
-
-export const MetaMessage: FC<IProps> = ({ data, scrollToBottom }) => {
- const { avatar, metaMessages } = data;
-
- const [thinkingMessage, setThinkingMessage] = useState<string>('');
- const thinkingManagerRef = useRef(createThinkingManager());
-
- useEffect(() => {
- scrollToBottom?.();
- }, [scrollToBottom]);
-
- useEffect(() => {
- const manager = thinkingManagerRef.current;
-
- if (metaMessages?.statusMessage) {
- const formatted = formatStatusMessage(metaMessages.statusMessage);
- setThinkingMessage(formatted);
- manager.stop();
- return;
- }
-
- const toolName = metaMessages?.title || metaMessages?.functionCall?.name;
- manager.start(setThinkingMessage, toolName || '');
-
- return () => manager.stop();
- }, [metaMessages]);
-
- return (
- <>
- <style>{blinkAnimation}</style>
- <div className="flex items-center gap-3 px-3">
- <div className="flex-shrink-0">
- <div className="size-6 rounded-full bg-gray-200 flex items-center justify-center overflow-hidden">
- {avatar ? (
- <img src={avatar} alt="AI Avatar" className="w-full h-full object-cover" />
- ) : (
- <div className="w-full h-full bg-gradient-to-br from-blue-400 to-purple-500 flex items-center justify-center">
- <span className="text-white text-xs font-semibold">AI</span>
- </div>
- )}
- </div>
- </div>
-
- <div className="flex-1">
- <span className="text-sm text-gray-500 font-medium smooth-blink-text">
- {thinkingMessage}
- </span>
- </div>
- </div>
- </>
- );
-};
diff --git a/packages/app/src/react/features/ai-chat/components/chat/typewriter.tsx b/packages/app/src/react/features/ai-chat/components/chat/typewriter.tsx
index efadf4f4..f3da1715 100644
--- a/packages/app/src/react/features/ai-chat/components/chat/typewriter.tsx
+++ b/packages/app/src/react/features/ai-chat/components/chat/typewriter.tsx
@@ -1,91 +1,31 @@
-import { FC, useEffect, useState } from 'react';
+import { FC, useEffect, useRef, useState } from 'react';
import { MarkdownRenderer } from '@react/features/ai-chat/components';
-import { useChatStore } from '@react/features/ai-chat/hooks';
-interface ITypewriterProps {
+type TProps = {
message: string;
- onComplete?: () => void;
- isTyping?: boolean;
- minSpeed?: number;
- maxSpeed?: number;
- onTypingProgress?: () => void;
-}
+ onUpdate?: () => void;
+};
-export const Typewriter: FC<ITypewriterProps> = ({
- message,
- onComplete,
- isTyping = true,
- minSpeed = 1,
- maxSpeed = 20,
- onTypingProgress,
-}) => {
+export const Typewriter: FC<TProps> = ({ message, onUpdate }) => {
const [displayedText, setDisplayedText] = useState('');
- const [currentIndex, setCurrentIndex] = useState(0);
- const [lastMessageLength, setLastMessageLength] = useState(0);
- const { isStreaming } = useChatStore('chat') || {};
-
- const TYPING_SPEED = 0;
+ const prevMessageLengthRef = useRef(0);
useEffect(() => {
- if (!isStreaming && message.length > 0) {
- setDisplayedText(message);
- setCurrentIndex(message.length);
- onComplete?.();
- return;
- }
- }, [isStreaming, message, onComplete]);
-
- useEffect(() => {
- if (!isTyping) {
- setDisplayedText(message);
- setCurrentIndex(message.length);
- onComplete?.();
+ // Reset detection: message got shorter or empty
+ if (message.length < prevMessageLengthRef.current || message === '') {
+ setDisplayedText('');
+ prevMessageLengthRef.current = 0;
return;
}
- if (message.length < 3) {
+ // Update displayed text and notify
+ if (message.length > prevMessageLengthRef.current) {
setDisplayedText(message);
- setCurrentIndex(message.length);
- onComplete?.();
- return;
- }
-
- if (currentIndex < message.length) {
- const batchSize = message.length - currentIndex;
- const charsToAdd = message.slice(currentIndex, currentIndex + batchSize);
-
- const timer = setTimeout(() => {
- setDisplayedText((prevText) => prevText + charsToAdd);
- setCurrentIndex((prevIndex) => prevIndex + batchSize);
- onTypingProgress?.();
- }, TYPING_SPEED);
-
- return () => clearTimeout(timer);
- } else if (currentIndex >= message.length && displayedText.length === message.length) {
- onComplete?.();
- }
- }, [
- message,
- currentIndex,
- displayedText.length,
- isTyping,
- onComplete,
- onTypingProgress,
- minSpeed,
- maxSpeed,
- TYPING_SPEED,
- ]);
-
- useEffect(() => {
- if (message.length < lastMessageLength || message === '') {
- setDisplayedText('');
- setCurrentIndex(0);
- setLastMessageLength(0);
- } else if (message.length > lastMessageLength) {
- setLastMessageLength(message.length);
+ prevMessageLengthRef.current = message.length;
+ onUpdate?.();
}
- }, [message, lastMessageLength]);
+ }, [message, onUpdate]);
return (
<div className="typewriter-content type-writer-text type-writer-inner w-full text-wrap">
diff --git a/packages/app/src/react/features/ai-chat/components/common/icons.tsx b/packages/app/src/react/features/ai-chat/components/common/icons.tsx
index 33539d15..1a8e6930 100644
--- a/packages/app/src/react/features/ai-chat/components/common/icons.tsx
+++ b/packages/app/src/react/features/ai-chat/components/common/icons.tsx
@@ -39,4 +39,39 @@ export const SendIcon = ({ className }: { className?: string }) => (
fill="currentColor"
/>
</svg>
+);
+
+/** Error info circle icon (red warning indicator) */
+export const ErrorInfoIcon = ({ className }: { className?: string }) => (
+ <svg
+ width="20"
+ height="20"
+ viewBox="0 0 20 20"
+ fill="currentColor"
+ xmlns="http://www.w3.org/2000/svg"
+ className={cn('text-red-500', className)}
+ >
+ <path d="M10 9.25C10.4142 9.25002 10.75 9.5858 10.75 10V13.333C10.75 13.7472 10.4142 14.083 10 14.083C9.58579 14.083 9.25 13.7472 9.25 13.333V10C9.25 9.58579 9.58579 9.25 10 9.25Z" />
+ <path d="M10 5.83301C10.5293 5.83303 10.958 6.26273 10.958 6.79199C10.9578 7.3211 10.5291 7.74998 10 7.75C9.47084 7.75 9.04217 7.32111 9.04199 6.79199C9.04199 6.26272 9.47073 5.83301 10 5.83301Z" />
+ <path
+ fillRule="evenodd"
+ clipRule="evenodd"
+ d="M10 2.08496C14.3713 2.08496 17.915 5.62867 17.915 10C17.915 14.3713 14.3713 17.915 10 17.915C5.62867 17.915 2.08496 14.3713 2.08496 10C2.08496 5.62867 5.62867 2.08496 10 2.08496ZM10 3.41504C6.3632 3.41504 3.41504 6.3632 3.41504 10C3.41504 13.6368 6.3632 16.585 10 16.585C13.6368 16.585 16.585 13.6368 16.585 10C16.585 6.3632 13.6368 3.41504 10 3.41504Z"
+ />
+ </svg>
+);
+
+/** Retry/refresh icon (circular arrow) */
+export const RetryIcon = ({ className }: { className?: string }) => (
+ <svg
+ width="20"
+ height="20"
+ viewBox="0 0 20 20"
+ fill="currentColor"
+ xmlns="http://www.w3.org/2000/svg"
+ className={cn('-ms-0.5', className)}
+ aria-hidden="true"
+ >
+ <path d="M16.4183 9.99967C16.4181 6.45518 13.5448 3.58188 10.0003 3.58171C7.96895 3.58171 6.15935 4.52712 4.98273 6.00163H7.50031L7.6341 6.0153C7.93707 6.07735 8.16535 6.34535 8.16535 6.66667C8.16535 6.98799 7.93707 7.25598 7.6341 7.31803L7.50031 7.33171H3.75031C3.73055 7.33171 3.71104 7.32656 3.69172 7.32487C3.68913 7.32464 3.68649 7.32513 3.68391 7.32487C3.64304 7.32082 3.60409 7.31254 3.56574 7.30143C3.56188 7.30031 3.55788 7.2997 3.55402 7.2985C3.51366 7.286 3.47546 7.27023 3.43879 7.25065L3.43586 7.24967C3.43444 7.24891 3.43337 7.24752 3.43195 7.24675C3.35757 7.20587 3.29219 7.15262 3.23859 7.08757C3.23505 7.08329 3.23128 7.07923 3.22785 7.07487C3.20549 7.04631 3.1858 7.01609 3.16828 6.98405C3.16491 6.97791 3.16169 6.97173 3.15852 6.9655C3.14408 6.93698 3.13265 6.90732 3.12238 6.87663C3.11744 6.86205 3.11264 6.84757 3.10871 6.83268C3.10316 6.81117 3.09843 6.78955 3.09504 6.76725C3.09108 6.74233 3.08833 6.71738 3.08723 6.69206C3.08691 6.68361 3.08527 6.67519 3.08527 6.66667V2.91667C3.08527 2.5494 3.38304 2.25163 3.75031 2.25163C4.11743 2.2518 4.41535 2.54951 4.41535 2.91667V4.62956C5.82462 3.16447 7.80565 2.25163 10.0003 2.25163C14.2793 2.2518 17.7482 5.72065 17.7484 9.99967C17.7484 14.2789 14.2794 17.7485 10.0003 17.7487C6.02899 17.7487 2.75629 14.7612 2.305 10.9108L2.96516 10.8337L3.62531 10.7555C3.99895 13.9437 6.7115 16.4186 10.0003 16.4186C13.5449 16.4184 16.4183 13.5443 16.4183 9.99967ZM2.88801 10.1725C3.25252 10.13 3.58237 10.3911 3.62531 10.7555L2.305 10.9108C2.26225 10.546 2.52323 10.2153 2.88801 10.1725Z" />
+ </svg>
);
\ No newline at end of file
diff --git a/packages/app/src/react/features/ai-chat/components/container.tsx b/packages/app/src/react/features/ai-chat/components/container.tsx
index a675caca..705be463 100644
--- a/packages/app/src/react/features/ai-chat/components/container.tsx
+++ b/packages/app/src/react/features/ai-chat/components/container.tsx
@@ -1,6 +1,8 @@
-import { TChildren } from '@react/features/ai-chat/types';
+import type { TChildren } from '@react/features/ai-chat/types';
import { FC, useCallback, useEffect, useRef } from 'react';
+import '../styles/index.css';
+
export const Container: FC<TChildren> = ({ children }) => {
const parentElementRef = useRef<HTMLElement | null>(null);
const originalStateRef = useRef<{ className: string; maxWidth: string } | null>(null);
diff --git a/packages/app/src/react/features/ai-chat/components/footer/error-toast.tsx b/packages/app/src/react/features/ai-chat/components/footer/error-toast.tsx
index e24b8712..10c3bb7e 100644
--- a/packages/app/src/react/features/ai-chat/components/footer/error-toast.tsx
+++ b/packages/app/src/react/features/ai-chat/components/footer/error-toast.tsx
@@ -1,6 +1,6 @@
+import { CloseIcon } from '@react/features/ai-chat/components';
import { FC } from 'react';
import { FaCircleExclamation } from 'react-icons/fa6';
-import { CloseIcon } from '../common/icons';
interface IProps {
message: string;
diff --git a/packages/app/src/react/features/ai-chat/components/footer/footer.tsx b/packages/app/src/react/features/ai-chat/components/footer/footer.tsx
index c70a4a8a..414c39e7 100644
--- a/packages/app/src/react/features/ai-chat/components/footer/footer.tsx
+++ b/packages/app/src/react/features/ai-chat/components/footer/footer.tsx
@@ -2,8 +2,6 @@ import { ChatInput, ErrorToast, ScrollToBottomButton } from '@react/features/ai-
import { useChatStores } from '@react/features/ai-chat/hooks';
import { FC } from 'react';
-const CHAT_WARNING_INFO = `SmythOS can make mistakes, always check your work. We don't store chat history, save important work.`;
-
export const Footer: FC = () => {
const { files, scroll } = useChatStores();
const { errorMessage, clearError } = files;
@@ -17,7 +15,10 @@ export const Footer: FC = () => {
<ChatInput />
</div>
- <h6 className="py-4 text-center text-xs text-gray-500">{CHAT_WARNING_INFO}</h6>
+ <h6 className="py-4 text-center text-xs text-gray-500">
+ SmythOS can make mistakes, always check your work. We don&apos;t store chat history, save
+ important work.
+ </h6>
</div>
);
};
diff --git a/packages/app/src/react/features/ai-chat/components/footer/input.tsx b/packages/app/src/react/features/ai-chat/components/footer/input.tsx
index ca3c0196..3fac4936 100644
--- a/packages/app/src/react/features/ai-chat/components/footer/input.tsx
+++ b/packages/app/src/react/features/ai-chat/components/footer/input.tsx
@@ -1,5 +1,4 @@
import { ChangeEvent, KeyboardEvent, useCallback, useEffect, useRef, useState } from 'react';
-import '../../styles/index.css';
import { AttachmentButton, FileItemPreview, SendButton } from '@react/features/ai-chat/components';
import { useChatStores, useClipboardPaste } from '@react/features/ai-chat/hooks';
@@ -15,20 +14,20 @@ import { cn } from '@react/shared/utils/general';
const LARGE_TEXT_THRESHOLD = 4000;
export const ChatInput = () => {
- const { ref: allRefs, files: filesData, chat, agent } = useChatStores();
+ const { refs, files: filesData, chat, agent } = useChatStores();
const [message, setMessage] = useState('');
const fileInputRef = useRef<HTMLInputElement>(null);
- // Use inputRef from context (chatbot pattern - no useImperativeHandle needed)
- const inputRef = allRefs?.input;
+ const inputRef = refs?.input;
const { isChatCreating } = chat || {};
+ const { isLoading, data: agentData } = agent || {};
const { isStreaming, sendMessage, stopStreaming } = chat || {};
const { attachments, status, addFiles, remove, clear, uploading } = filesData || {};
const maxLength = MAX_CHAT_MESSAGE_LENGTH;
const isMaxFilesUploaded = attachments.length >= 10;
- const isDisabled = isChatCreating || agent.isLoading.agent;
+ const isDisabled = isChatCreating || isLoading.agent;
// Adjust textarea height when message changes (chatbot pattern)
useEffect(() => {
@@ -141,7 +140,7 @@ export const ChatInput = () => {
onKeyDown={handleKeyDown}
aria-label="Message input"
onClick={(e) => e.stopPropagation()}
- placeholder={`Message ${agent.data?.name || ''}...`}
+ placeholder={`Message ${agentData?.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]"
/>
diff --git a/packages/app/src/react/features/ai-chat/components/footer/send-button.tsx b/packages/app/src/react/features/ai-chat/components/footer/send-button.tsx
index ba84b3bf..c073da61 100644
--- a/packages/app/src/react/features/ai-chat/components/footer/send-button.tsx
+++ b/packages/app/src/react/features/ai-chat/components/footer/send-button.tsx
@@ -1,17 +1,17 @@
import { FC } from 'react';
import { FaStop } from 'react-icons/fa6';
-import { SendIcon } from '@react/features/ai-chat/components/common/icons';
+import { SendIcon } from '@react/features/ai-chat/components';
import { Button } from '@react/shared/components/ui/newDesign/button';
-import { cn } from '@src/react/shared/utils/general';
+import { cn } from '@react/shared/utils/general';
-interface SendButtonProps {
+interface IProps {
isStreaming: boolean;
disabled: boolean;
onClick: () => void;
}
-export const SendButton: FC<SendButtonProps> = ({ isStreaming, disabled, onClick }) => (
+export const SendButton: FC<IProps> = ({ isStreaming, disabled, onClick }) => (
<Button
addIcon
variant="primary"
diff --git a/packages/app/src/react/features/ai-chat/components/header/model-dropdown/index.tsx b/packages/app/src/react/features/ai-chat/components/header/model-dropdown/index.tsx
index 571f8a77..85041214 100644
--- a/packages/app/src/react/features/ai-chat/components/header/model-dropdown/index.tsx
+++ b/packages/app/src/react/features/ai-chat/components/header/model-dropdown/index.tsx
@@ -8,7 +8,7 @@ import { EVENTS } from '@shared/posthog/constants/events';
import { LLMRegistry } from '@shared/services/LLMRegistry.service';
import { llmModelsStore } from '@shared/state_stores/llm-models';
-import type { TLLMModel } from '@react/features/ai-chat/types';
+import { TLLMModel } from '@react/features/ai-chat/types';
import { getBadgeFromTags } from '@react/features/ai-chat/utils';
import { ModelPanel } from './model-panel';
import { ProviderPanel } from './provider-panel';
diff --git a/packages/app/src/react/features/ai-chat/components/header/model-dropdown/model-panel.tsx b/packages/app/src/react/features/ai-chat/components/header/model-dropdown/model-panel.tsx
index b7fff49f..4c43112a 100644
--- a/packages/app/src/react/features/ai-chat/components/header/model-dropdown/model-panel.tsx
+++ b/packages/app/src/react/features/ai-chat/components/header/model-dropdown/model-panel.tsx
@@ -2,7 +2,7 @@ import { FC } from 'react';
import { cn } from '@react/shared/utils/general';
-import type { TLLMModel } from '@react/features/ai-chat/types';
+import { TLLMModel } from '@react/features/ai-chat/types';
import { getBadgeFromTags } from '@react/features/ai-chat/utils';
interface IProps {
diff --git a/packages/app/src/react/features/ai-chat/contexts/chat.provider.tsx b/packages/app/src/react/features/ai-chat/contexts/chat.provider.tsx
index 36e7e038..09a9249b 100644
--- a/packages/app/src/react/features/ai-chat/contexts/chat.provider.tsx
+++ b/packages/app/src/react/features/ai-chat/contexts/chat.provider.tsx
@@ -1,3 +1,4 @@
+import { ChatContext, IChatContext } from '@react/features/ai-chat/contexts/chat';
import {
useAgentSettings,
useChatState,
@@ -10,20 +11,17 @@ import type { TChildren } from '@react/features/ai-chat/types';
import { useAgent } from '@react/shared/hooks/agent';
import { Observability } from '@shared/observability';
import { EVENTS } from '@shared/posthog/constants/events';
-import { ChatContext, IChatContext } from '@react/features/ai-chat/contexts/chat';
import { FC, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
export const ChatContextProvider: FC<TChildren> = ({ children }) => {
- const params = useParams<{ agentId: string }>();
- const agentId = params?.agentId;
+ const navigate = useNavigate();
+ const { agentId } = useParams<{ agentId: string }>();
const inputRef = useRef<HTMLTextAreaElement>(null);
const containerRef = useRef<HTMLDivElement>(null);
-
- const navigate = useNavigate();
-
const hasInitializedChatRef = useRef(false);
+
const [modelOverride, setModelOverride] = useState<string | null>(null);
const { data: agent, isLoading: isAgentLoading } = useAgent(agentId, {
@@ -34,11 +32,10 @@ export const ChatContextProvider: FC<TChildren> = ({ children }) => {
});
const { data: settingsData, isLoading: isAgentSettingsLoading } = useAgentSettings(agentId);
- const { mutateAsync: createChat, isPending: isChatCreating } = useCreateChatMutation();
- const { mutateAsync: updateAgentSettings } = useUpdateAgentSettingsMutation();
-
const agentSettings = settingsData?.settings;
+ const { mutateAsync: createChat, isPending: isChatCreating } = useCreateChatMutation();
+ const { mutateAsync: updateAgentSettings } = useUpdateAgentSettingsMutation();
const fileUpload = useFileUpload({
agentId: agentId || '',
chatId: agentSettings?.lastConversationId || '',
@@ -46,7 +43,14 @@ export const ChatContextProvider: FC<TChildren> = ({ children }) => {
const scroll = useScrollToBottom(containerRef);
- const chatStateV2 = useChatState({
+ const {
+ messages,
+ setMessages,
+ isStreaming,
+ sendMessage: addMessage,
+ stopStreaming,
+ retryLastMessage,
+ } = useChatState({
agentId: agentId || '',
chatId: agentSettings?.lastConversationId || '',
modelId: modelOverride || agentSettings?.chatGptModel,
@@ -54,14 +58,26 @@ export const ChatContextProvider: FC<TChildren> = ({ children }) => {
inputRef,
});
- const {
- messages,
- setMessages,
- isStreaming,
- sendMessage: sendMessageV2,
- stopStreaming,
- retryLastMessage,
- } = chatStateV2;
+ const createSession = useCallback(async () => {
+ try {
+ const conversation = await createChat({
+ conversation: {
+ summary: '',
+ chunkSize: 100,
+ lastChunkID: '0',
+ label: 'New Chat',
+ aiAgentId: agentId,
+ },
+ });
+
+ await updateAgentSettings({
+ agentId,
+ settings: { key: 'lastConversationId', value: conversation?.id },
+ });
+ } catch (error) {
+ console.error('Error creating chat session', error); // eslint-disable-line no-console
+ }
+ }, [agentId, createChat, updateAgentSettings]);
const sendMessage = useCallback(
async (message: string) => {
@@ -77,72 +93,50 @@ export const ChatContextProvider: FC<TChildren> = ({ children }) => {
file: f.file,
}));
- await sendMessageV2(message, attachments);
+ await addMessage(message, attachments);
fileUpload.clear();
} catch (error) {
console.error('Failed to send message:', error); // eslint-disable-line no-console
}
},
- [fileUpload, sendMessageV2],
+ [fileUpload, addMessage],
);
- const createSession = useCallback(async () => {
- try {
- const conversation = await createChat({
- conversation: {
- summary: '',
- chunkSize: 100,
- lastChunkID: '0',
- label: 'New Chat',
- aiAgentId: agentId,
- },
- });
-
- await updateAgentSettings({
- agentId,
- settings: { key: 'lastConversationId', value: conversation?.id },
- });
- } catch (error) {
- console.error('Error creating chat session', error); // eslint-disable-line no-console
- }
- }, [agentId, createChat, updateAgentSettings]);
-
const resetSession = useCallback(async () => {
stopStreaming();
setMessages([]);
fileUpload.clear();
await createSession();
- inputRef?.current?.focus();
+ inputRef.current?.focus();
Observability.observeInteraction(EVENTS.CHAT_EVENTS.SESSION_END);
Observability.observeInteraction(EVENTS.CHAT_EVENTS.SESSION_START);
- }, [createSession, setMessages, fileUpload, stopStreaming, inputRef]);
-
- useEffect(() => {
- if (agentSettings && agent && !hasInitializedChatRef.current) {
- agent.aiAgentSettings = agentSettings;
- agent.id = agentId;
-
- hasInitializedChatRef.current = true;
- createSession().then(() => inputRef?.current?.focus());
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [agentSettings, agent, agentId]);
+ }, [createSession, setMessages, fileUpload, stopStreaming]);
+ // Session tracking
useEffect(() => {
Observability.observeInteraction(EVENTS.CHAT_EVENTS.SESSION_START);
return () => Observability.observeInteraction(EVENTS.CHAT_EVENTS.SESSION_END);
}, []);
+ // Initialize chat session
+ useEffect(() => {
+ if (agentSettings && agent && !hasInitializedChatRef.current) {
+ hasInitializedChatRef.current = true;
+ createSession().then(() => inputRef.current?.focus());
+ }
+ }, [agentSettings, agent, createSession]);
+
+ // Auto-focus input
useEffect(() => {
- const inputDisabled = isChatCreating || isAgentLoading || isStreaming;
- if (!isAgentLoading && !inputDisabled) inputRef.current?.focus();
+ const isInputDisabled = isChatCreating || isAgentLoading || isStreaming;
+ if (!isAgentLoading && !isInputDisabled) inputRef.current?.focus();
}, [isAgentLoading, isChatCreating, isStreaming]);
- const values: IChatContext = useMemo(
+ const contextValue: IChatContext = useMemo(
() => ({
- ref: { input: inputRef, container: containerRef },
+ refs: { input: inputRef, container: containerRef },
agent: {
data: agent,
settings: agentSettings,
@@ -158,33 +152,27 @@ export const ChatContextProvider: FC<TChildren> = ({ children }) => {
stopStreaming,
retryMessage: retryLastMessage,
},
-
scroll,
-
modelOverride,
setModelOverride,
}),
[
- inputRef,
- containerRef,
agent,
agentSettings,
+ fileUpload,
isAgentLoading,
isAgentSettingsLoading,
- fileUpload,
isChatCreating,
- messages,
isStreaming,
- sendMessage,
- retryLastMessage,
- stopStreaming,
+ messages,
+ modelOverride,
resetSession,
+ retryLastMessage,
scroll,
- modelOverride,
- setModelOverride,
+ sendMessage,
+ stopStreaming,
],
);
- return <ChatContext.Provider value={values}>{children}</ChatContext.Provider>;
+ return <ChatContext.Provider value={contextValue}>{children}</ChatContext.Provider>;
};
-
diff --git a/packages/app/src/react/features/ai-chat/contexts/chat.ts b/packages/app/src/react/features/ai-chat/contexts/chat.ts
index f8ae35ae..4713f255 100644
--- a/packages/app/src/react/features/ai-chat/contexts/chat.ts
+++ b/packages/app/src/react/features/ai-chat/contexts/chat.ts
@@ -1,10 +1,10 @@
+/* eslint-disable no-unused-vars */
import type { IFileUpload, TChatMessage } from '@react/features/ai-chat/types';
import type { AgentDetails, AgentSettings } from '@react/shared/types/agent-data.types';
import { createContext, RefObject } from 'react';
export interface IChatContext {
- /** Refs for input textarea and chat container (chatbot pattern - direct element refs) */
- ref: {
+ refs: {
input?: RefObject<HTMLTextAreaElement | null>;
container?: RefObject<HTMLDivElement | null>;
};
@@ -13,7 +13,6 @@ export interface IChatContext {
settings?: AgentSettings;
isLoading: { agent: boolean; settings: boolean };
};
- /** File upload management using V2 hook */
files: IFileUpload;
chat: {
isChatCreating: boolean;
@@ -35,7 +34,7 @@ export interface IChatContext {
}
export const ChatContext = createContext<IChatContext>({
- ref: { input: undefined, container: undefined },
+ refs: { input: undefined, container: undefined },
agent: { data: undefined, settings: undefined, isLoading: { agent: false, settings: false } },
files: {
attachments: [],
@@ -65,4 +64,3 @@ export const ChatContext = createContext<IChatContext>({
modelOverride: null,
setModelOverride: () => {},
});
-
diff --git a/packages/app/src/react/features/ai-chat/hooks/use-drag-and-drop.ts b/packages/app/src/react/features/ai-chat/hooks/use-drag-and-drop.ts
index 00c2cfe2..b5c0e93c 100644
--- a/packages/app/src/react/features/ai-chat/hooks/use-drag-and-drop.ts
+++ b/packages/app/src/react/features/ai-chat/hooks/use-drag-and-drop.ts
@@ -1,32 +1,38 @@
import { RefObject, useCallback, useEffect, useRef } from 'react';
type TDragAndDropConfig = {
- onDrop: (files: File[]) => Promise<void>;
+ ref: RefObject<HTMLElement | null>;
+ onDrop: (files: File[]) => Promise<void>; // eslint-disable-line no-unused-vars
};
-export const useDragAndDrop = ({ onDrop }: TDragAndDropConfig): RefObject<HTMLElement> => {
- const containerRef = useRef<HTMLElement>(null);
+export const useDragAndDrop = ({ ref, onDrop }: TDragAndDropConfig): void => {
const dragCounterRef = useRef(0);
- const handleDragEnter = useCallback((e: DragEvent) => {
- e.preventDefault();
- e.stopPropagation();
+ const handleDragEnter = useCallback(
+ (e: DragEvent) => {
+ e.preventDefault();
+ e.stopPropagation();
- dragCounterRef.current++;
- if (dragCounterRef.current === 1) {
- containerRef.current?.classList.add('file-drag-active');
- }
- }, []);
+ dragCounterRef.current++;
+ if (dragCounterRef.current === 1) {
+ ref.current?.classList.add('file-drag-active');
+ }
+ },
+ [ref],
+ );
- const handleDragLeave = useCallback((e: DragEvent) => {
- e.preventDefault();
- e.stopPropagation();
+ const handleDragLeave = useCallback(
+ (e: DragEvent) => {
+ e.preventDefault();
+ e.stopPropagation();
- dragCounterRef.current--;
- if (dragCounterRef.current === 0) {
- containerRef.current?.classList.remove('file-drag-active');
- }
- }, []);
+ dragCounterRef.current--;
+ if (dragCounterRef.current === 0) {
+ ref.current?.classList.remove('file-drag-active');
+ }
+ },
+ [ref],
+ );
const handleDragOver = useCallback((e: DragEvent) => {
e.preventDefault();
@@ -39,18 +45,18 @@ export const useDragAndDrop = ({ onDrop }: TDragAndDropConfig): RefObject<HTMLEl
e.stopPropagation();
dragCounterRef.current = 0;
- containerRef.current?.classList.remove('file-drag-active');
+ ref.current?.classList.remove('file-drag-active');
const droppedFiles = Array.from(e.dataTransfer?.files || []);
if (droppedFiles.length > 0) {
await onDrop(droppedFiles);
}
},
- [onDrop],
+ [ref, onDrop],
);
useEffect(() => {
- const element = containerRef.current;
+ const element = ref.current;
if (!element) return;
element.addEventListener('dragenter', handleDragEnter);
@@ -65,7 +71,5 @@ export const useDragAndDrop = ({ onDrop }: TDragAndDropConfig): RefObject<HTMLEl
element.removeEventListener('drop', handleDrop);
element.classList.remove('file-drag-active');
};
- }, [handleDragEnter, handleDragLeave, handleDragOver, handleDrop]);
-
- return containerRef;
+ }, [ref, handleDragEnter, handleDragLeave, handleDragOver, handleDrop]);
};
diff --git a/packages/app/src/react/features/ai-chat/styles/index.css b/packages/app/src/react/features/ai-chat/styles/index.css
index c1121d74..67908d6e 100644
--- a/packages/app/src/react/features/ai-chat/styles/index.css
+++ b/packages/app/src/react/features/ai-chat/styles/index.css
@@ -30,4 +30,23 @@
100% {
left: calc(-100% + var(--my-scroll-var) - 10px);
}
+}
+
+/* Smooth blink animation for thinking/loading states */
+@keyframes smoothBlink {
+ 0% {
+ opacity: 1;
+ }
+
+ 50% {
+ opacity: 0.3;
+ }
+
+ 100% {
+ opacity: 1;
+ }
+}
+
+.smooth-blink-text {
+ animation: smoothBlink 1.5s ease-in-out infinite;
}
\ No newline at end of file