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 AI Chat Hooks and Context for Improved Structure and Type Safety
Salman-Ahamedc42de6ef5d6f
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.tsxindex 09a9249b..ceb651be 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@@ -2,10 +2,10 @@ import { ChatContext, IChatContext } from '@react/features/ai-chat/contexts/chatimport {useAgentSettings,useChatState,- useCreateChatMutation,+ useCreateChat,useFileUpload,+ useSaveAgentSettings,useScrollToBottom,- useUpdateAgentSettingsMutation,} from '@react/features/ai-chat/hooks';import type { TChildren } from '@react/features/ai-chat/types';import { useAgent } from '@react/shared/hooks/agent';@@ -34,8 +34,8 @@ export const ChatContextProvider: FC<TChildren> = ({ children }) => {const { data: settingsData, isLoading: isAgentSettingsLoading } = useAgentSettings(agentId);const agentSettings = settingsData?.settings;- const { mutateAsync: createChat, isPending: isChatCreating } = useCreateChatMutation();- const { mutateAsync: updateAgentSettings } = useUpdateAgentSettingsMutation();+ const { mutateAsync: createChat, isPending: isChatCreating } = useCreateChat();+ const { mutateAsync: updateAgentSettings } = useSaveAgentSettings();const fileUpload = useFileUpload({agentId: agentId || '',chatId: agentSettings?.lastConversationId || '',diff --git a/packages/app/src/react/features/ai-chat/hooks/index.ts b/packages/app/src/react/features/ai-chat/hooks/index.tsindex f8fb627d..78e07529 100644--- a/packages/app/src/react/features/ai-chat/hooks/index.ts+++ b/packages/app/src/react/features/ai-chat/hooks/index.ts@@ -1,5 +1,5 @@export * from './use-agent-settings';-export * from './use-chat-mutation';+export * from './use-create-chat';export * from './use-chat-state';export * from './use-clipboard-paste';export * from './use-drag-and-drop';diff --git a/packages/app/src/react/features/ai-chat/hooks/use-agent-settings.ts b/packages/app/src/react/features/ai-chat/hooks/use-agent-settings.tsindex 2f005487..c1ef9aa5 100644--- a/packages/app/src/react/features/ai-chat/hooks/use-agent-settings.ts+++ b/packages/app/src/react/features/ai-chat/hooks/use-agent-settings.ts@@ -28,16 +28,10 @@ export const useAgentSettings = (agentId: string) => {return queryResult;};-export const useUpdateAgentSettingsMutation = () => {+export const useSaveAgentSettings = () => {const queryClient = useQueryClient();-- return useMutation(- (params: { agentId: string; settings: TAgentSetting }) =>- updateAgentSettings(params.agentId, params.settings),- {- onSuccess: (_, variables) => {- queryClient.invalidateQueries(['agent_settings', variables.agentId]);- },- },- );+ type TParams = { agentId: string; settings: TAgentSetting };+ return useMutation((params: TParams) => updateAgentSettings(params.agentId, params.settings), {+ onSuccess: (_, { agentId }) => queryClient.invalidateQueries(['agent_settings', agentId]),+ });};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.tsindex 6584bfb2..3b8273de 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@@ -350,7 +350,6 @@ export const useChatState = (options: TChatStateConfig): IChatState => {messages,setMessages,isStreaming,- setIsStreaming,sendMessage,stopStreaming,retryLastMessage,diff --git a/packages/app/src/react/features/ai-chat/hooks/use-chat-mutation.ts b/packages/app/src/react/features/ai-chat/hooks/use-create-chat.tssimilarity index 56%rename from packages/app/src/react/features/ai-chat/hooks/use-chat-mutation.tsrename to packages/app/src/react/features/ai-chat/hooks/use-create-chat.tsindex 9da78da6..f6d69f2e 100644--- a/packages/app/src/react/features/ai-chat/hooks/use-chat-mutation.ts+++ b/packages/app/src/react/features/ai-chat/hooks/use-create-chat.ts@@ -5,20 +5,13 @@ import { queryClient } from '@react/shared/query-client';import type { CreateChatRequest } from '@react/shared/types/api-payload.types';import type { CreateChatsResponse } from '@react/shared/types/api-results.types';-// Query keys for chat operationsconst CHAT_CREATE_MUTATION = ['post', '/api/page/chat/new'];const CHAT_LIST_QUERY = ['get', '/api/page/chat/list'];-// Create a singleton instance for chat operationsconst chatClient = new ChatAPIClient();-export const useCreateChatMutation = () =>- useMutation<CreateChatsResponse, Error, CreateChatRequest>(- CHAT_CREATE_MUTATION,- (data) => chatClient.createChat(data),- {- onSuccess: () => {- queryClient.invalidateQueries(CHAT_LIST_QUERY); // reset cache, if any, of chat list- },- },- );+type TUseCreateChat = ReturnType<typeof useMutation<CreateChatsResponse, Error, CreateChatRequest>>;+export const useCreateChat = (): TUseCreateChat =>+ useMutation(CHAT_CREATE_MUTATION, (data) => chatClient.createChat(data), {+ onSuccess: () => queryClient.invalidateQueries(CHAT_LIST_QUERY),+ });diff --git a/packages/app/src/react/features/ai-chat/types/hooks.ts b/packages/app/src/react/features/ai-chat/types/hooks.tsindex b2c44b6a..62474b68 100644--- a/packages/app/src/react/features/ai-chat/types/hooks.ts+++ b/packages/app/src/react/features/ai-chat/types/hooks.ts@@ -1,24 +1,21 @@+/* eslint-disable no-unused-vars */import type { Dispatch, RefObject, SetStateAction } from 'react';-import type { TAttachment, TChatMessage } from './message';-import type { TFileAttachment, TUploadStatus } from './file';+import type { TAttachment, TChatMessage, TFileAttachment, TUploadStatus } from '.';-export interface IFocusable {- focus: () => void;-}+export type TFocusable = { focus: () => void };export type TChatStateConfig = {agentId: string;chatId: string;modelId?: string;enableMetaMessages?: boolean;- inputRef?: RefObject<IFocusable | null>;+ inputRef?: RefObject<TFocusable | null>;};export interface IChatState {messages: TChatMessage[];setMessages: Dispatch<SetStateAction<TChatMessage[]>>;isStreaming: boolean;- setIsStreaming: Dispatch<SetStateAction<boolean>>;sendMessage: (message: string,currentAttachments?: TAttachment[],@@ -38,4 +35,3 @@ export interface IFileUpload {clear: (preserveBlobUrls?: boolean) => void;clearError: () => void;}-