diff --git a/shared/utils.ts b/shared/utils.ts index 845b0894..afd89df8 100644 --- a/shared/utils.ts +++ b/shared/utils.ts @@ -1,6 +1,9 @@ import { ReorChatMessage } from '@/components/Chat/types' -const generateChatName = (messages: ReorChatMessage[]): string => { +const generateChatName = (messages: ReorChatMessage[], userInput?: string): string => { + if (userInput) { + return userInput.slice(0, 50) + } if (!messages || messages.length === 0 || !messages[0].content) { return 'Empty Chat' } diff --git a/src/components/Chat/index.tsx b/src/components/Chat/index.tsx index fdcf1a35..3262d43b 100644 --- a/src/components/Chat/index.tsx +++ b/src/components/Chat/index.tsx @@ -50,17 +50,17 @@ const ChatComponent: React.FC = () => { }, [currentOpenChatID, saveChat]) const handleNewChatMessage = useCallback( - async (userTextFieldInput?: string, chatFilters?: AgentConfig) => { + async (chat: Chat | undefined, userTextFieldInput?: string, chatFilters?: AgentConfig) => { try { const defaultLLMName = await window.llm.getDefaultLLMName() - if (!userTextFieldInput?.trim() && (!currentChat || currentChat.messages.length === 0)) { + if (!userTextFieldInput?.trim() && (!chat || chat.messages.length === 0)) { return } let outputChat = userTextFieldInput?.trim() - ? await appendToOrCreateChat(currentChat, userTextFieldInput, chatFilters) - : currentChat + ? await appendToOrCreateChat(chat, userTextFieldInput, chatFilters) + : chat if (!outputChat) { return @@ -96,13 +96,17 @@ const ChatComponent: React.FC = () => { } if (!abortControllerRef.current.signal.aborted) { - outputChat.messages = await appendToolCallsAndAutoExecuteTools( + const { messages: outputMessages, allToolCallsHaveBeenExecuted } = await appendToolCallsAndAutoExecuteTools( outputChat.messages, outputChat.toolDefinitions, await toolCalls, ) + outputChat.messages = outputMessages setCurrentChat(outputChat) await saveChat(outputChat) + if (allToolCallsHaveBeenExecuted) { + handleNewChatMessage(outputChat, undefined, chatFilters) + } } setLoadingState('idle') @@ -113,7 +117,7 @@ const ChatComponent: React.FC = () => { abortControllerRef.current = null } }, - [setCurrentOpenChatID, saveChat, currentChat], + [setCurrentOpenChatID, saveChat], ) return ( @@ -124,10 +128,17 @@ const ChatComponent: React.FC = () => { currentChat={currentChat} setCurrentChat={setCurrentChat} loadingState={loadingState} - handleNewChatMessage={handleNewChatMessage} + handleNewChatMessage={(userTextFieldInput?: string, chatFilters?: AgentConfig) => + handleNewChatMessage(currentChat, userTextFieldInput, chatFilters) + } /> ) : ( - + + handleNewChatMessage(undefined, userTextFieldInput, chatFilters) + } + /> )} diff --git a/src/components/Chat/utils.ts b/src/components/Chat/utils.ts index bdf7cb38..c0cedc99 100644 --- a/src/components/Chat/utils.ts +++ b/src/components/Chat/utils.ts @@ -125,17 +125,23 @@ const autoExecuteTools = async ( // eslint-disable-next-line no-await-in-loop outputMessages = await makeAndAddToolResultToMessages(outputMessages, toolCall, lastMessage) } - return outputMessages + const allToolCallsHaveBeenExecuted = + toolsThatNeedExecuting.length > 0 && toolsThatNeedExecuting.length === toolCalls.length + return { messages: outputMessages, allToolCallsHaveBeenExecuted } } export const appendToolCallsAndAutoExecuteTools = async ( messages: ReorChatMessage[], toolDefinitions: ToolDefinition[], toolCalls: ToolCallPart[], -): Promise => { +): Promise<{ messages: ReorChatMessage[]; allToolCallsHaveBeenExecuted: boolean }> => { const messagesWithToolCalls = appendToolCallPartsToMessages(messages, toolCalls) - const messagesWithToolResults = await autoExecuteTools(messagesWithToolCalls, toolDefinitions, toolCalls) - return messagesWithToolResults + const { messages: messagesWithToolResults, allToolCallsHaveBeenExecuted } = await autoExecuteTools( + messagesWithToolCalls, + toolDefinitions, + toolCalls, + ) + return { messages: messagesWithToolResults, allToolCallsHaveBeenExecuted } } export const convertMessageToString = (message: ReorChatMessage | undefined): string => { @@ -226,7 +232,7 @@ export const generateInitialChat = async (userTextFieldInput: string, agentConfi return { id: Date.now().toString(), messages: ragMessages, - displayName: generateChatName(ragMessages), + displayName: generateChatName(ragMessages, userTextFieldInput), timeOfLastMessage: Date.now(), toolDefinitions: agentConfig.toolDefinitions, }