Skip to content

fix(ReActAgent): preserve user chunk callback when using copied toolkit#894

Open
JGoP-L wants to merge 4 commits intoagentscope-ai:mainfrom
JGoP-L:codex/fix-issue-870-chunk-callback
Open

fix(ReActAgent): preserve user chunk callback when using copied toolkit#894
JGoP-L wants to merge 4 commits intoagentscope-ai:mainfrom
JGoP-L:codex/fix-issue-870-chunk-callback

Conversation

@JGoP-L
Copy link
Contributor

@JGoP-L JGoP-L commented Mar 9, 2026

AgentScope-Java Version

1.0.10-SNAPSHOT

Description

Fixes #870.

Previously, when a user configured a chunk callback via Toolkit#setChunkCallback(...) and then passed that toolkit into ReActAgent.builder().toolkit(toolkit).build(), the callback could be overwritten by the internal callback registered by ReActAgent during tool execution.

This PR separates user-defined chunk callbacks from framework-internal chunk callbacks so both can work together:

  • preserve user callbacks across Toolkit.copy()
  • keep ActingChunkEvent emission in ReActAgent
  • add a regression test in ToolEmitterIntegrationTest

How to test

mvn -pl agentscope-core -Dtest=ToolEmitterIntegrationTest test

@JGoP-L JGoP-L requested review from a team and Copilot March 9, 2026 04:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a regression where user-configured Toolkit#setChunkCallback(...) was effectively overwritten by ReActAgent during tool execution, ensuring both user chunk callbacks and ActingChunkEvent hook emission work simultaneously (issue #870).

Changes:

  • Split tool chunk callback handling into user vs framework-internal callbacks in ToolExecutor.
  • Added Toolkit#setInternalChunkCallback(...) so ReActAgent can emit ActingChunkEvent without overwriting user callbacks.
  • Preserved user chunk callbacks across Toolkit.copy() and added a regression test covering the real ReActAgent copied-toolkit scenario.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
agentscope-core/src/main/java/io/agentscope/core/tool/ToolExecutor.java Separates user/internal chunk callbacks and composes an effective callback for ToolEmitter.
agentscope-core/src/main/java/io/agentscope/core/tool/Toolkit.java Adds internal chunk-callback API and preserves user chunk callback during copy().
agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java Registers an internal chunk callback (instead of overwriting the user callback) to emit ActingChunkEvent.
agentscope-core/src/test/java/io/agentscope/core/tool/ToolEmitterIntegrationTest.java Adds integration regression test verifying both user callback and ActingChunkEvent receive tool-emitted chunks under ReActAgent.

Comment on lines +444 to +454
/**
* Set the framework-internal chunk callback for streaming tool responses.
*
* <p>This method is used by ReActAgent to forward tool chunks into ActingChunkEvent hooks
* without overwriting any user callback configured via {@link #setChunkCallback(BiConsumer)}.
*
* @param callback Internal callback to invoke when tools emit chunks via ToolEmitter
*/
public void setInternalChunkCallback(BiConsumer<ToolUseBlock, ToolResultBlock> callback) {
executor.setInternalChunkCallback(callback);
}
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

Toolkit#setInternalChunkCallback(...) is a public method but appears intended as framework-only API. In this codebase, similar public-but-internal APIs (e.g., Toolkit#callTools(...)) are explicitly labeled in Javadoc as “Internal API - Not recommended for external use.” Consider adding the same explicit warning here to discourage external callers and set expectations about API stability.

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Mar 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

[Bug]:Toolkit#setChunkCallback not triggered due to missing callback invocation in AgentScope 1.0.9

2 participants