vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
9.44k stars 1.38k forks source link

When using >0 maxToolRoundTrips onFinish gets called prematurely #2761

Open mwrshah opened 4 weeks ago

mwrshah commented 4 weeks ago

Description

I have an implementation that runs a saveChat() function when the useChat hook finishes. Perhaps I am misunderstanding how this is supposed to work - my expectation is that onFinish should get called when the useChat hook finishes streaming all the roundtrips, but that's not what happens. my SaveChat is dependant on the isStreaming state which I am setting with a delay of around a second, and I manage to capture a tiny snippet of the after ToolCall in my saveChat but not the entire post Tool Call message.

Code example

const { messages, input, setInput, handleInputChange, handleSubmit, data, setMessages, stop } = useChat({ api: '/api/chat', id: chatKey.toString(), initialMessages, maxToolRoundtrips: 4, body: { session: session, template: selectedTemplate, chatId: currentChatId }, onFinish: () => { console.log('onFinish called') setFiles(undefined); setTimeout(() => { setIsStreaming(false) }, 1000) setFinished(true) setToolResult(getLatestToolInvocation(messages)) }, onError: (error) => { stop() console.error('Error:', error) setIsStreaming(false) setFinished(true) } })

Additional context

No response

mwrshah commented 3 weeks ago

The onFinish exposes the last Message, so I guess it's intentional. I still content, it would be quite useful to be able to do db actions just once for multiple tool calls, otherwise it degrades performance.