Skip to content

fix(ai): restore ToolLoopAgent onFinish callbacks#14336

Open
paulelliotco wants to merge 1 commit intovercel:release-v6.0from
paulelliotco:fix-toolloopagent-onfinish
Open

fix(ai): restore ToolLoopAgent onFinish callbacks#14336
paulelliotco wants to merge 1 commit intovercel:release-v6.0from
paulelliotco:fix-toolloopagent-onfinish

Conversation

@paulelliotco
Copy link
Copy Markdown

Summary

This fixes a v6 regression where ToolLoopAgent stopped forwarding onFinish to generateText() / streamText().

As a result:

  • constructor-level onFinish callbacks in ToolLoopAgentSettings no longer fired
  • per-call agent.generate({ onFinish }) / agent.stream({ onFinish }) were no longer accepted in the agent call types

This PR:

  • re-adds onFinish to AgentCallParameters and AgentStreamParameters
  • restores onFinish forwarding in ToolLoopAgent.generate() and stream()
  • merges constructor-level and per-call onFinish callbacks, invoking the constructor callback first to match onStepFinish
  • adds runtime and type regression tests covering callback acceptance and invocation order

Fixes #14330.

Manual Verification

  • pnpm --filter ai test:node src/agent/tool-loop-agent.test.ts
  • pnpm --filter ai test:node src/agent/tool-loop-agent.test-d.ts
  • pnpm type-check:full
  • pnpm check

@tigent tigent bot added ai/core core functions like generateText, streamText, etc. Provider utils, and provider spec. bug Something isn't working as documented labels Apr 10, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 365a242b63

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

/**
* Callback that is called when all steps are finished and the response is complete.
*/
onFinish?: ToolLoopAgentOnFinishCallback<TOOLS>;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Exclude onFinish from prepareCall option type

Adding onFinish to AgentCallParameters also widens ToolLoopAgentSettings.prepareCall (it is typed as Omit<AgentCallParameters, 'onStepFinish'>), but ToolLoopAgent.generate()/stream() immediately destructure onFinish and call prepareCall(options) without it. This creates a type/runtime mismatch where prepareCall appears able to read per-call onFinish but will always see it as absent, which can silently break user code that branches on this callback. Please either omit onFinish from the prepareCall input type as well or pass it through consistently.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

@vercel vercel bot left a comment

Choose a reason for hiding this comment

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

Additional Suggestion:

The prepareCall type parameter omits onStepFinish from AgentCallParameters but fails to also omit onFinish, creating a type that includes a property that is stripped at runtime.

Fix on Vercel

@paulelliotco paulelliotco force-pushed the fix-toolloopagent-onfinish branch from 365a242 to 0574616 Compare April 10, 2026 21:14
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 057461691b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

/**
* Callback that is called when all steps are finished and the response is complete.
*/
onFinish?: ToolLoopAgentOnFinishCallback<TOOLS>;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Remove onFinish from prepareCall input type

Adding onFinish to AgentCallParameters widens ToolLoopAgentSettings.prepareCall (it is defined as Omit<AgentCallParameters<...>, 'onStepFinish'>), but ToolLoopAgent.generate() and stream() destructure onFinish and call prepareCall(options) without it, so prepareCall can never observe the callback at runtime. This creates a type/runtime mismatch that can silently break prepareCall logic that branches on onFinish; either omit onFinish from the prepareCall input type as well or pass it through consistently.

Useful? React with 👍 / 👎.

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

Labels

ai/core core functions like generateText, streamText, etc. Provider utils, and provider spec. bug Something isn't working as documented

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant