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 Hooks for Enhanced Type Consistency and Clarity

Salman-Ahamed4f13838695f6
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.ts
index 7bb4830f..8f16eb2a 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
@@ -9,6 +9,7 @@
import { useCallback, useEffect, useRef } from 'react';
import { useNavigate } from 'react-router-dom';
+import { IChatMessage, IMessageFile } from '@react/features/ai-chat';
import {
useAgentSettings,
useCreateChatMutation,
@@ -17,7 +18,6 @@ import {
} from '@react/features/ai-chat/hooks';
import { useChat } from '@react/features/ai-chat/hooks/use-chat';
import { useAgent } from '@react/shared/hooks/agent';
-import { FileWithMetadata, IChatMessage } from '../types/chat.types';
import { Observability } from '@shared/observability';
import { EVENTS } from '@shared/posthog/constants/events';
@@ -41,7 +41,7 @@ export interface IUseAgentChatContextReturn {
/** Chat context value ready to be passed to ChatProvider */
chatContextValue: {
// File handling
- files: FileWithMetadata[];
+ files: IMessageFile[];
uploadingFiles: Set<string>;
isUploadInProgress: boolean;
isMaxFilesUploaded: boolean;
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 c487cc0e..fd1ccf0d 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
@@ -1,8 +1,8 @@
import { useCallback, useRef, useState } from 'react';
+import { IChatMessage, IMessageFile } from '@react/features/ai-chat';
import { CHAT_ERROR_MESSAGE } from '@react/features/ai-chat/constants';
import { chatUtils } from '@react/features/ai-chat/utils';
-import { FileWithMetadata, IChatMessage } from '../types/chat.types';
interface UseChatActionsProps {
agentId: string;
@@ -16,7 +16,7 @@ interface UseChatActionsReturn {
isGenerating: boolean;
isInputProcessing: boolean;
isRetrying: boolean;
- sendMessage: (message: string, attachedFiles?: FileWithMetadata[]) => Promise<void>; // eslint-disable-line no-unused-vars
+ sendMessage: (message: string, attachedFiles?: IMessageFile[]) => Promise<void>; // eslint-disable-line no-unused-vars
retryLastMessage: () => void;
stopGenerating: () => void;
clearMessages: () => void;
@@ -25,7 +25,7 @@ interface UseChatActionsReturn {
// Interface to store last user message with files
interface LastUserMessage {
message: string;
- attachedFiles?: FileWithMetadata[];
+ attachedFiles?: IMessageFile[];
}
export const useChatActions = ({
@@ -58,7 +58,7 @@ export const useChatActions = ({
}, []);
const sendMessage = useCallback(
- async (message: string, attachedFiles?: FileWithMetadata[]) => {
+ async (message: string, attachedFiles?: IMessageFile[]) => {
if (!message.trim() && (!attachedFiles || attachedFiles.length === 0)) return;
// Store the complete message with files for retry functionality
diff --git a/packages/app/src/react/features/ai-chat/hooks/use-chat-stream.ts b/packages/app/src/react/features/ai-chat/hooks/use-chat-stream.ts
index 3da06e5c..692be12d 100644
--- a/packages/app/src/react/features/ai-chat/hooks/use-chat-stream.ts
+++ b/packages/app/src/react/features/ai-chat/hooks/use-chat-stream.ts
@@ -3,9 +3,9 @@
* Provides clean interface for streaming chat responses with lifecycle management
*/
+import { IChatError, IStreamCallbacks, IStreamConfig } from '@react/features/ai-chat';
+import { ChatAPIClient } from '@react/features/ai-chat/clients/chat-api.client';
import { useCallback, useRef, useState } from 'react';
-import { ChatAPIClient } from '../clients/chat-api.client';
-import { IChatError, IChatStreamCallbacks, IChatStreamConfig } from '../types/chat.types';
/**
* Hook configuration interface
@@ -28,7 +28,7 @@ interface IUseChatStreamReturn {
/** Current error if any */
error: IChatError | null;
/** Start a new chat stream */
- startStream: (config: IChatStreamConfig, callbacks: IChatStreamCallbacks) => Promise<void>;
+ startStream: (config: IStreamConfig, callbacks: IStreamCallbacks) => Promise<void>; // eslint-disable-line no-unused-vars
/** Abort the current stream */
abortStream: () => void;
/** Clear error state */
@@ -84,7 +84,7 @@ export const useChatStream = (config: IUseChatStreamConfig = {}): IUseChatStream
* Automatically manages abort controller and lifecycle callbacks
*/
const startStream = useCallback(
- async (streamConfig: IChatStreamConfig, callbacks: IChatStreamCallbacks): Promise<void> => {
+ async (streamConfig: IStreamConfig, callbacks: IStreamCallbacks): Promise<void> => {
// Abort any existing stream
if (abortControllerRef.current) {
abortControllerRef.current.abort();
@@ -107,7 +107,7 @@ export const useChatStream = (config: IUseChatStreamConfig = {}): IUseChatStream
try {
// Wrap callbacks to manage lifecycle
- const wrappedCallbacks: IChatStreamCallbacks = {
+ const wrappedCallbacks: IStreamCallbacks = {
...callbacks,
onError: (streamError: IChatError) => {
setError(streamError);
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 3d1d0bdf..20c35cf0 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
@@ -1,17 +1,19 @@
+/* eslint-disable no-unused-vars, @typescript-eslint/no-unused-vars */
+
/**
* Professional React hook for complete chat state management
* Combines streaming, message history, file uploads, and error handling
*/
-import { useCallback, useRef, useState } from 'react';
-import { ChatAPIClient } from '../clients/chat-api.client';
import {
IChatMessage,
IFileAttachment,
IMessageFile,
IUseChatReturn,
TThinkingType,
-} from '../types/chat.types';
+} from '@react/features/ai-chat';
+import { useCallback, useRef, useState } from 'react';
+import { ChatAPIClient } from '../clients/chat-api.client';
import { useChatStream } from './use-chat-stream';
/**
@@ -305,7 +307,7 @@ export const useChat = (config: IUseChatConfig): IUseChatReturn => {
currentAIMessageRef.current += content;
updateAIMessage(currentAIMessageRef.current);
},
- onThinking: (thinkingMsg: string, _type: TThinkingType) => {
+ onThinking: (thinkingMsg: string, type: TThinkingType) => {
// Mark that we're in thinking state
isThinkingRef.current = true;
hasThinkingOccurredRef.current = true;
diff --git a/packages/app/src/react/features/ai-chat/hooks/use-fileupload.ts b/packages/app/src/react/features/ai-chat/hooks/use-fileupload.ts
index f685e00b..c5c7ee3b 100644
--- a/packages/app/src/react/features/ai-chat/hooks/use-fileupload.ts
+++ b/packages/app/src/react/features/ai-chat/hooks/use-fileupload.ts
@@ -1,6 +1,7 @@
/* eslint-disable no-unused-vars */
import { ChangeEvent, useCallback, useEffect, useState } from 'react';
+import { IMessageFile } from '@react/features/ai-chat';
import {
createFileMetadata,
deleteFile,
@@ -8,7 +9,6 @@ import {
uploadFile,
validateSingleFile,
} from '@react/features/ai-chat/utils/file';
-import { FileWithMetadata } from '../types/chat.types';
interface FileUploadError {
show: boolean;
@@ -16,7 +16,7 @@ interface FileUploadError {
}
interface UseFileUploadReturn {
- files: FileWithMetadata[];
+ files: IMessageFile[];
uploadingFiles: Set<string>;
uploadError: FileUploadError;
handleFileChange: (e: ChangeEvent<HTMLInputElement>) => Promise<void>;
@@ -32,10 +32,13 @@ const generateUniqueFileId = (file: File): string => {
return `${file.name}-${file.size}-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
};
-export const useFileUpload = (params?: { agentId?: string; chatId?: string }): UseFileUploadReturn => {
+export const useFileUpload = (params?: {
+ agentId?: string;
+ chatId?: string;
+}): UseFileUploadReturn => {
const agentId = params?.agentId || '';
const chatId = params?.chatId;
- const [files, setFiles] = useState<FileWithMetadata[]>([]);
+ const [files, setFiles] = useState<IMessageFile[]>([]);
const [uploadingFiles, setUploadingFiles] = useState<Set<string>>(new Set());
const [uploadError, setUploadError] = useState<FileUploadError>({ show: false, message: '' });
@@ -116,7 +119,11 @@ export const useFileUpload = (params?: { agentId?: string; chatId?: string }): U
if (result.success) {
// Normalize response from runtime (/aichat/upload)
- const runtimeFile = result.data?.files?.[0];
+ const data = result.data as {
+ files?: Array<{ url?: string; mimetype?: string }>;
+ file?: { url?: string; type?: string };
+ };
+ const runtimeFile = data?.files?.[0];
setFiles((prevFiles) =>
prevFiles.map((f) =>
f.id === id
@@ -125,8 +132,8 @@ export const useFileUpload = (params?: { agentId?: string; chatId?: string }): U
metadata: {
...f.metadata,
// Do not set key for runtime uploads (avoid delete attempts)
- publicUrl: runtimeFile?.url || result.data?.file?.url,
- fileType: runtimeFile?.mimetype || result.data?.file?.type,
+ publicUrl: runtimeFile?.url || data?.file?.url,
+ fileType: runtimeFile?.mimetype || data?.file?.type,
isUploading: false,
},
}