fix(ai): onStepFinish type for AgentUIStream methods#12843
fix(ai): onStepFinish type for AgentUIStream methods#12843rovo89 wants to merge 1 commit intovercel:mainfrom
Conversation
This changes the type of the `onStepFinish` callback from `ToolLoopAgentOnStepFinishCallback<TOOLS>` to `UIMessageStreamOnStepFinishCallback<UI_MESSAGE>` for methods which create a `UIMessageChunk` stream for an Agent, giving the implementation access to the generated`UIMessage` so far instead of just the raw parts. This partly reverts vercel#11980, which explicitly added the `onStepFinish` callback to the method signatures. Instead, `onStepFinish` is now added to the `UIMessageStreamOptions` type in the same way as `onFinish` already was, and is passed through via `options`. Affected methods are: - `createAgentUIStream()` - `createAgentUIStreamResponse()` - `pipeAgentUIStreamToResponse()` For even more consistency, the `onStepFinish` callback is also added to: - `DefaultStreamTextResult.toUIMessageStream()` (used by the methods above) - `DefaultStreamTextResult.toUIMessageStreamResponse()` - `DefaultStreamTextResult.pipeUIMessageStreamToResponse()` Callers which want to receive the `ToolLoopAgentOnStepFinishCallback` can easily do that with the `onStepFinish` callback to the constructor of the `ToolLoopAgent` that they're passing to the methods above. Fixes vercel#12841.
|
@lgrammel @aayush-kapoor @sullyo Could you please have a look? I think this makes the |
| */ | ||
| options?: CALL_OPTIONS; | ||
| } & Omit<UIMessageStreamOptions<UI_MESSAGE>, 'onFinish'>; | ||
| } & Omit<UIMessageStreamOptions<UI_MESSAGE>, 'onFinish' | 'onStepFinish'>; |
There was a problem hiding this comment.
I don't really have an idea why onFinish is omitted here, but I found it while searching for references to UIMessageStreamOptions. Since onStepFinish is now added to that type and is similar to onFinish, I added it here. If that's not correct, I'll remove it, but this way we can at least talk about it.
|
@lgrammel @aayush-kapoor @sullyo Friendly ping. I think this PR would make the types for onStepFinish much more consistent, support it in more places, remove special handling and documentation is fully adjusted. Lots of wins. 😉 And with v7 betas coming in, even the tiny (easy to resolve) backwards-incompatibility isn't a problem. Would really appreciate getting this merged. |
|
@rovo89 can you update the relevant examples / add examples to show how things would look after the change? when designing this feature, I discussed with @nicoalbanese where one would most likely want to listen for the agent finish, and i want to make sure this does not break the intended agent definition / agent execution separation. |
|
@lgrammel Sure, I'll have a look. You're referring to https://ai-sdk.dev/docs/reference/ai-sdk-core/tool-loop-agent, https://ai-sdk.dev/docs/reference/ai-sdk-core/create-agent-ui-stream etc.? Currently I don't see any examples for neither But maybe these tables is even more helpful to understand my intention. This is the current situation:
And I think it should be:
That gives devs the capability to hook into either the low-level part with |
Background
See #12841. Recently,
onStepFinishcallbacks where added in a few places, but the type for AgentUIStreams focussed on the "agent" part instead of the "UI stream" part.Summary
This changes the type of the
onStepFinishcallback fromToolLoopAgentOnStepFinishCallback<TOOLS>toUIMessageStreamOnStepFinishCallback<UI_MESSAGE>for methods which create a
UIMessageChunkstream for an Agent, giving the implementation access to the generatedUIMessageso far instead of just the raw parts.This partly reverts #11980, which explicitly added the
onStepFinishcallback to the method signatures. Instead,onStepFinishis now added to theUIMessageStreamOptionstype in the same way asonFinishalready was, and is passed through viaoptions.Affected methods are:
createAgentUIStream()createAgentUIStreamResponse()pipeAgentUIStreamToResponse()For even more consistency, the
onStepFinishcallback is also added to:DefaultStreamTextResult.toUIMessageStream()(used by the methods above)DefaultStreamTextResult.toUIMessageStreamResponse()DefaultStreamTextResult.pipeUIMessageStreamToResponse()Callers which want to receive the
ToolLoopAgentOnStepFinishCallbackcan easily do that with theonStepFinishcallback to the constructor of theToolLoopAgentthat they're passing to the methods above.Fixes #12841.
Manual Verification
Used
createAgentUIStream()in a project, added aonStepFinishcallback, logged theresponseMessageand verified that it looks as expected.Checklist
pnpm changesetin the project root)Related Issues
Issues #12841 and #12383 describe the motivation to have the callback.
PRs #11980 and #12448 added implementations recently.
PR #12654 also touched callbacks afterwards.