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.
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.
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