Potentially this is not an agents issue even, but it feels "buggy" that onFinish callback of toUIMessageStreamResponse does not have id of the last message (responseMessage or last message in messages).
Example might explain it better:
export class ChatAgent extends AIChatAgent<Env> {
async onChatMessage() {
// ......
return result.toUIMessageStreamResponse({
originalMessages: this.messages,
onFinish: ({ messages, responseMessage }) => {
// neither of these has id for the last message
console.log("Chat finished. Final messages:", messages);
console.log("Final response message:", responseMessage);
setTimeout(() => {
// here you can actually grab latest message and it has an id
// but it feels hacky that you have to wait for the next tick to get it
// not sure if this is a correct way
console.log(this.messages);
});
}
});
}
}
Potentially this is not an
agentsissue even, but it feels "buggy" thatonFinishcallback oftoUIMessageStreamResponsedoes not haveidof the last message (responseMessageor last message inmessages).Example might explain it better: