Skip to content

Conversation

@michael-han-dev
Copy link
Contributor

Background

PR #11888 adds output/outputError to the streamText onFinish callback. @lgrammel feedback: the three callback types (GenerateTextOnFinishCallback, StreamTextOnFinishCallback, ToolLoopAgentOnFinishCallback) share identical structure and should be unified first.

Summary

Extracted the common callback event structure into a shared TextOnFinishEvent type. All three callback types now reference this base instead of duplicating 20ish lines each.

New file: text-on-finish-callback.ts

  • TextOnFinishEvent - the shared event structure
  • TextOnFinishCallback - base callback type

Breaking change: ToolLoopAgentOnFinishCallback.experimental_context is now required instead of optional. This aligns it with GenerateTextOnFinishCallback and StreamTextOnFinishCallback which already had it required. Low risk because the SDK always provides this value when invoking callbacks. User code that consumes callbacks won't break since they're reading from the event, not constructing it.

Manual Verification

This is a type-only refactor with no runtime behavior changes. The callback invocation code remains identical. Verified by:

  1. Ran existing onFinish examples to confirm callbacks still fire:
    pnpm tsx src/stream-text/openai-on-finish.ts
    pnpm tsx src/generate-text/openai-on-finish.ts
    pnpm tsx src/agent/openai-generate-on-finish.ts

  2. Type tests confirm all three callback types are assignable to the base TextOnFinishCallback type.

  3. All existing tests pass for the affected modules.

No new runtime behavior to manually verify since we only changed type definitions.

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Future Work

This is PR 1 of 4 in a series to add output/outputError to all onFinish callbacks:

PR 2: Rebase #11888 - After this lands, #11888 can extend TextOnFinishEvent with an OUTPUT generic and add output/outputError properties to StreamTextOnFinishCallback.

PR 3: generateText output - Add the same OUTPUT generic and output/outputError to GenerateTextOnFinishCallback. Requires reordering generateText internals to parse output before calling onFinish.

PR 4: Agent output - Add OUTPUT generic and output/outputError to ToolLoopAgentOnFinishCallback.

Related Issues

#11888

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant