Skip to content

Conversation

@whoiskatrin
Copy link
Contributor

@whoiskatrin whoiskatrin commented Aug 4, 2025

Updated from AI SDK v4 → v5 (createUIMessageStream, UIMessageStreamWriter, etc.)

@changeset-bot
Copy link

changeset-bot bot commented Aug 4, 2025

🦋 Changeset detected

Latest commit: 8527613

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
agents Minor
hono-agents Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Aug 4, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@391

commit: 8527613

Copy link
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a first pass. the code seems ok, but the typecasting seems too much. I'll test this out tomorrow and see if I can fix the types.

@ryleyrandall22
Copy link

When this lands, how do we migrate existing persisted messages to use the new message types?

@threepointone
Copy link
Contributor

good point. we should add an automatic migration.

@ryleyrandall22
Copy link

@whoiskatrin Looking at the code to migrate v4 messages. The latest version of v4 messages actually had content and parts. But v5 modified the part types as well so a full migration would likely required mapping the part types as well

@whoiskatrin
Copy link
Contributor Author

@ryleyrandall22 thanks for checking, this is work in progress pretty much and not ready for review

@nicksrandall
Copy link

Do you think it makes sense to add only send latest message functionality to this PR? I wrote a PR for this functionality for the v4 sdk and I'm happy to make another one for v5 if this PR lands before that. Or it might make sense to just include it in this PR since we're refactoring things anyway.

See:
https://ai-sdk.dev/docs/ai-sdk-ui/chatbot-message-persistence#sending-only-the-last-message

@yusuf-eren
Copy link

@whoiskatrin I checked it after the last commit. The last message is not being saved.
I'm using examples/playground for running it from your fork. Did you test that? I may be missing something

fix applied, thanks!

Just tested it across multiple apps, and it works perfectly. Thanks a lot for the fix and your efforts!

Comment on lines 1939 to 1943
export {
useAgentChat,
detectToolsRequiringConfirmation,
type AITool
} from "./ai-react";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@whoiskatrin These exports from ./ai-react are causing errors when building my RSC project which imports from agents (in a server context) and from agents/react or agents/ai-react (in a use client context).

Would it be ok to remove this re-export and always import these from agents/react or agents/ai-react.

(more details here and here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@threepointone should we? You asked me to do the opposite, so referring to you here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah let's not export these from the root

@jldec
Copy link

jldec commented Sep 2, 2025

I noticed this behavior running guides/human-in-the-loop
The time returned by the tool reverts to "null" when you reload the browser. (It does not appear to be persisted)
Screenshot 2025-09-01 at 22 11 29
Screenshot 2025-09-01 at 22 11 40

@whoiskatrin
Copy link
Contributor Author

whoiskatrin commented Sep 2, 2025

@jldec i think most of the things should be solved now, persistance is fixed, just need to do the export for you

Copy link
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tentative approval, looks good, pending nits and notes

@yusuf-eren
Copy link

yusuf-eren commented Sep 2, 2025

I had a quick question;

Is there any way to send warnings to ui without writing this.broadcast(JSON.stringify({ ... }) in ai-sdk v5 with agents?
I'm writing a custom warning hook, which will be sent to client via WebSocket. It will not be stored in mesages, just a toast message. You think this is an ok approach or am I missing a function in the agents library?

@deathbyknowledge
Copy link
Contributor

I had a quick question;

Is there any way to send warnings to ui without writing this.broadcast(JSON.stringify({ ... }) in ai-sdk v5 with agents? I'm writing a custom warning hook, which will be sent to client via WebSocket. It will not be stored in mesages, just a toast message. You think this is an ok approach or am I missing a function in the agents library?

https://ai-sdk.dev/docs/ai-sdk-ui/streaming-data#transient-data-parts-ephemeral should work

@jcheese1
Copy link

jcheese1 commented Sep 2, 2025

@deathbyknowledge i noticed, when i used https://ai-sdk.dev/docs/ai-sdk-ui/streaming-data#transient-data-parts-ephemeral, it only sent the data to the user that triggered it, and it didnt broadcast to all connections. I'm assuming thats intended?

@whoiskatrin
Copy link
Contributor Author

@deathbyknowledge i noticed, when i used https://ai-sdk.dev/docs/ai-sdk-ui/streaming-data#transient-data-parts-ephemeral, it only sent the data to the user that triggered it, and it didnt broadcast to all connections. I'm assuming thats intended?

I believe that transient data parts are designed to be delivered only to the user connection that triggered the event, yes.

@jldec
Copy link

jldec commented Sep 2, 2025

@whoiskatrin I think i figured out the cause of the issue I noticed earlier

When invoking the time tool in guides/human-in-the-loop, the tool is running twice, once on the server and once on the client. The UI shows the server tool output, but it sends a message back with the client tool output, which is what is persisted. If you make the tool outputs different in guides/human-in-the-loop/src/tools.ts you'll see that it's the client result which is saved and appears on reload.

@whoiskatrin
Copy link
Contributor Author

@whoiskatrin I think i figured out the cause of the issue I noticed earlier

When invoking the time tool in guides/human-in-the-loop, the tool is running twice, once on the server and once on the client. The UI shows the server tool output, but it sends a message back with the client tool output, which is what is persisted. If you make the tool outputs different in guides/human-in-the-loop/src/tools.ts you'll see that it's the client result which is saved and appears on reload.

this has been addressed btw, you shouldn't be experiencing this. there was a root cause and we've eliminated that

@jldec
Copy link

jldec commented Sep 3, 2025

Thanks @whoiskatrin - I confirmed that the issues I saw previously are now fixed.

I did do a little bit of digging to figure out how to show errors in the client UI, and when I expose the error value returned by the useAgentChat hook, I noticed this error appearing after processing any input message (confirmed by adding error output to /guides/human-in-the-loop/src/app.tsx as in the screenshot below)

Screenshot 2025-09-02 at 22 42 40 Screenshot 2025-09-02 at 22 45 33

@whoiskatrin
Copy link
Contributor Author

whoiskatrin commented Sep 3, 2025

Thanks @whoiskatrin - I confirmed that the issues I saw previously are now fixed.

I did do a little bit of digging to figure out how to show errors in the client UI, and when I expose the error value returned by the useAgentChat hook, I noticed this error appearing after processing any input message (confirmed by adding error output to /guides/human-in-the-loop/src/app.tsx as in the screenshot below)

Oh, good catch, I've just pushed the fix.

@whoiskatrin whoiskatrin merged commit ecf8926 into cloudflare:main Sep 3, 2025
3 checks passed
@threepointone threepointone mentioned this pull request Sep 3, 2025
@mksglu
Copy link

mksglu commented Sep 3, 2025

Thank you!

@fforres
Copy link

fforres commented Sep 3, 2025

Haven't been as excited for a PR in a while 🙏🏼

This is a-mazing 😄 ❤️

@jcheese1
Copy link

jcheese1 commented Sep 4, 2025

@whoiskatrin sorry this is after merged but can we add a way to override the type of this.message within the AIChatAgent class? Im thinking the easiest way is to do something like this:

<Env = unknown, State = unknown, Message extends UIMessage = UIMessage>

@threepointone
Copy link
Contributor

Can you open a new issue for this? We probably need a new signature because we already have a third aegument incoming there

@jcheese1
Copy link

jcheese1 commented Sep 4, 2025

@threepointone created! #433

@sscowden
Copy link

sscowden commented Sep 5, 2025

@whoiskatrin This is really amazing, thank you for this update. Are there plans to update the starter template to use the latest version?

@whoiskatrin
Copy link
Contributor Author

@sscowden yes! it's almost done, we will try to publish it on monday

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.