I’m working on a feature where my app will have multiple client-side tools. One of the client-side tools (tools without execute
) is to allow user to choose a person from a list of presented users by AI with Person List as args (which my tool will render with)
Example of how everything would work:
- User will say: Update the first name to user to “John”.
- Bot: Sure, select a user from this list. (a client side tool which displays all people in UL/LI in HTML).
- User: Types the “current old/name” (or clicks on that UL or LI).
- Bot: Sure, it’s completed.
Current versus Expected behavior
Initially, I started using the addToolResult
to complete the tool execution. But, that’s incomplete. I’d like to also post a message on “chat window” as soon as user interacts with any of these tools (in this example, selects a person).
Now, in order for me to also post on chat window, I started append()
call in addition to addToResult()
. And, that caused ChatWindow screen to show weird flash because “messages” array from useChat() has inconsistent responses.
Here’s my sample code:
const handleOnClick = async ({ person }) => {
await chatContext?.addToolResult({
toolCallId,
result: { personId: person.id },
});
// Delay for 2 seconds
await new Promise(resolve => setTimeout(resolve, 2000));
await chatContext?.append({
content: person.personDisplayName,
role: 'user',
createdAt: new Date(),
id: Math.random().toString(),
});
}
<ul>
<li><Button onClick={handleOnClick} /></li>
...
</ul>
My questions:
- What’s the right way to achieve this? I want to post a message on the chat window when user interacts with any client side tool, without causing any weird flash.
- How do I complete tool execution by typing a message “without having to always call” addToolResult().?
@ai-sdk latest version.