Skip to content

Commit

Permalink
Jp/tool bug (#408)
Browse files Browse the repository at this point in the history
* Abort.

* CallingNice

* Pure.

* AutoRespond.

* user input field
  • Loading branch information
joseplayero authored Sep 20, 2024
1 parent bf6b92c commit 62c4e82
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
5 changes: 4 additions & 1 deletion shared/utils.ts
Original file line number Diff line number Diff line change
@@ -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'
}
Expand Down
27 changes: 19 additions & 8 deletions src/components/Chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -113,7 +117,7 @@ const ChatComponent: React.FC = () => {
abortControllerRef.current = null
}
},
[setCurrentOpenChatID, saveChat, currentChat],
[setCurrentOpenChatID, saveChat],
)

return (
Expand All @@ -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)
}
/>
) : (
<StartChat defaultModelName={defaultModelName} handleNewChatMessage={handleNewChatMessage} />
<StartChat
defaultModelName={defaultModelName}
handleNewChatMessage={(userTextFieldInput?: string, chatFilters?: AgentConfig) =>
handleNewChatMessage(undefined, userTextFieldInput, chatFilters)
}
/>
)}
</div>
</div>
Expand Down
16 changes: 11 additions & 5 deletions src/components/Chat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReorChatMessage[]> => {
): 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 => {
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit 62c4e82

Please sign in to comment.