Skip to content

Commit d3cafaa

Browse files
authored
fix(core): fix ReActAgent stream event block end ordering (agentscope-ai#1829)
Fixes agentscope-ai#1634
1 parent 3e1bc62 commit d3cafaa

2 files changed

Lines changed: 274 additions & 59 deletions

File tree

‎agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java‎

Lines changed: 94 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,9 +2091,7 @@ private Flux<AgentEvent> modelCallStream(
20912091
ReasoningContext context, ModelCallInput mci, boolean withToolEvents) {
20922092

20932093
String replyId = UUID.randomUUID().toString().replace("-", "");
2094-
AtomicBoolean textStarted = new AtomicBoolean(false);
2095-
AtomicBoolean thinkingStarted = new AtomicBoolean(false);
2096-
Map<String, String> startedToolCalls = new ConcurrentHashMap<>();
2094+
ModelCallBlockLifecycle blockLifecycle = new ModelCallBlockLifecycle(replyId);
20972095

20982096
Flux<AgentEvent> modelEvents =
20992097
mci.model().stream(mci.messages(), mci.tools(), mci.options())
@@ -2114,13 +2112,9 @@ private Flux<AgentEvent> modelCallStream(
21142112
for (ContentBlock block : chunk.getContent()) {
21152113
emitBlockEvents(
21162114
block,
2117-
replyId,
21182115
context,
2119-
textStarted,
2120-
thinkingStarted,
2121-
withToolEvents
2122-
? startedToolCalls
2123-
: new ConcurrentHashMap<>(),
2116+
blockLifecycle,
2117+
withToolEvents,
21242118
events);
21252119
}
21262120
return Flux.fromIterable(events);
@@ -2130,17 +2124,7 @@ private Flux<AgentEvent> modelCallStream(
21302124
Flux.defer(
21312125
() -> {
21322126
List<AgentEvent> events = new ArrayList<>();
2133-
if (textStarted.get()) {
2134-
events.add(new TextBlockEndEvent(replyId, "text"));
2135-
}
2136-
if (thinkingStarted.get()) {
2137-
events.add(new ThinkingBlockEndEvent(replyId, "thinking"));
2138-
}
2139-
for (Map.Entry<String, String> tc : startedToolCalls.entrySet()) {
2140-
events.add(
2141-
new ToolCallEndEvent(
2142-
replyId, tc.getKey(), tc.getValue()));
2143-
}
2127+
blockLifecycle.flushAll(events);
21442128
events.add(new ModelCallEndEvent(replyId, context.getChatUsage()));
21452129
return Flux.fromIterable(events);
21462130
});
@@ -2150,46 +2134,109 @@ private Flux<AgentEvent> modelCallStream(
21502134

21512135
private void emitBlockEvents(
21522136
ContentBlock block,
2153-
String replyId,
21542137
ReasoningContext context,
2155-
AtomicBoolean textStarted,
2156-
AtomicBoolean thinkingStarted,
2157-
Map<String, String> startedToolCalls,
2138+
ModelCallBlockLifecycle blockLifecycle,
2139+
boolean withToolEvents,
21582140
List<AgentEvent> events) {
21592141

21602142
if (block instanceof TextBlock tb) {
2161-
if (textStarted.compareAndSet(false, true)) {
2162-
events.add(new TextBlockStartEvent(replyId, "text"));
2163-
}
2143+
blockLifecycle.startText(events);
21642144
if (tb.getText() != null && !tb.getText().isEmpty()) {
2165-
events.add(new TextBlockDeltaEvent(replyId, "text", tb.getText()));
2145+
events.add(
2146+
new TextBlockDeltaEvent(blockLifecycle.replyId, "text", tb.getText()));
21662147
}
21672148
} else if (block instanceof ThinkingBlock tb) {
2168-
if (thinkingStarted.compareAndSet(false, true)) {
2169-
events.add(new ThinkingBlockStartEvent(replyId, "thinking"));
2170-
}
2149+
blockLifecycle.startThinking(events);
21712150
if (tb.getThinking() != null && !tb.getThinking().isEmpty()) {
2172-
events.add(new ThinkingBlockDeltaEvent(replyId, "thinking", tb.getThinking()));
2151+
events.add(
2152+
new ThinkingBlockDeltaEvent(
2153+
blockLifecycle.replyId, "thinking", tb.getThinking()));
21732154
}
2174-
} else if (block instanceof ToolUseBlock tub) {
2155+
} else if (withToolEvents && block instanceof ToolUseBlock tub) {
21752156
String toolId = resolveToolCallId(tub, context);
21762157
String toolName = tub.getName();
2177-
if (toolId != null && startedToolCalls.putIfAbsent(toolId, toolName) == null) {
2178-
if (toolName != null && !toolName.startsWith("__")) {
2179-
events.add(new ToolCallStartEvent(replyId, toolId, toolName));
2180-
}
2181-
}
2158+
blockLifecycle.startToolCall(toolId, toolName, events);
21822159
if (tub.getContent() != null && !tub.getContent().isEmpty()) {
21832160
events.add(
21842161
new ToolCallDeltaEvent(
2185-
replyId,
2162+
blockLifecycle.replyId,
21862163
toolId != null ? toolId : "",
21872164
toolName,
21882165
tub.getContent()));
21892166
}
21902167
}
21912168
}
21922169

2170+
/**
2171+
* Tracks block lifecycle within one model-call subscription.
2172+
*
2173+
* <p>The model stream is consumed through {@code concatMap}, but the state holders keep the
2174+
* previous thread-safe shape because model providers may deliver chunk content
2175+
* unpredictably. This helper only changes when pending end events are flushed; it does not
2176+
* change the block identity or event payloads.
2177+
*/
2178+
private final class ModelCallBlockLifecycle {
2179+
private final String replyId;
2180+
private final AtomicBoolean textStarted = new AtomicBoolean(false);
2181+
private final AtomicBoolean thinkingStarted = new AtomicBoolean(false);
2182+
private final Map<String, String> startedToolCalls = new ConcurrentHashMap<>();
2183+
2184+
private ModelCallBlockLifecycle(String replyId) {
2185+
this.replyId = replyId;
2186+
}
2187+
2188+
private void startText(List<AgentEvent> events) {
2189+
flushThinking(events);
2190+
if (textStarted.compareAndSet(false, true)) {
2191+
events.add(new TextBlockStartEvent(replyId, "text"));
2192+
}
2193+
}
2194+
2195+
private void startThinking(List<AgentEvent> events) {
2196+
if (thinkingStarted.compareAndSet(false, true)) {
2197+
events.add(new ThinkingBlockStartEvent(replyId, "thinking"));
2198+
}
2199+
}
2200+
2201+
private void startToolCall(String toolId, String toolName, List<AgentEvent> events) {
2202+
if (toolId == null || startedToolCalls.containsKey(toolId)) {
2203+
return;
2204+
}
2205+
flushText(events);
2206+
flushThinking(events);
2207+
flushAllToolCalls(events);
2208+
boolean visibleTool = toolName != null && !toolName.startsWith("__");
2209+
if (visibleTool && startedToolCalls.putIfAbsent(toolId, toolName) == null) {
2210+
events.add(new ToolCallStartEvent(replyId, toolId, toolName));
2211+
}
2212+
}
2213+
2214+
private void flushText(List<AgentEvent> events) {
2215+
if (textStarted.compareAndSet(true, false)) {
2216+
events.add(new TextBlockEndEvent(replyId, "text"));
2217+
}
2218+
}
2219+
2220+
private void flushThinking(List<AgentEvent> events) {
2221+
if (thinkingStarted.compareAndSet(true, false)) {
2222+
events.add(new ThinkingBlockEndEvent(replyId, "thinking"));
2223+
}
2224+
}
2225+
2226+
private void flushAllToolCalls(List<AgentEvent> events) {
2227+
for (Map.Entry<String, String> tc : startedToolCalls.entrySet()) {
2228+
events.add(new ToolCallEndEvent(replyId, tc.getKey(), tc.getValue()));
2229+
}
2230+
startedToolCalls.clear();
2231+
}
2232+
2233+
private void flushAll(List<AgentEvent> events) {
2234+
flushText(events);
2235+
flushThinking(events);
2236+
flushAllToolCalls(events);
2237+
}
2238+
}
2239+
21932240
private String resolveToolCallId(ToolUseBlock tub, ReasoningContext context) {
21942241
if (tub.getId() != null && !tub.getId().isEmpty()) {
21952242
return tub.getId();
@@ -3010,8 +3057,7 @@ private Flux<AgentEvent> summaryModelCallStream(
30103057
ReasoningContext context, ModelCallInput mci, GenerateOptions hookOptions) {
30113058

30123059
String replyId = UUID.randomUUID().toString().replace("-", "");
3013-
AtomicBoolean textStarted = new AtomicBoolean(false);
3014-
AtomicBoolean thinkingStarted = new AtomicBoolean(false);
3060+
ModelCallBlockLifecycle blockLifecycle = new ModelCallBlockLifecycle(replyId);
30153061

30163062
Flux<AgentEvent> modelEvents =
30173063
mci.model().stream(mci.messages(), mci.tools(), mci.options())
@@ -3032,28 +3078,22 @@ private Flux<AgentEvent> summaryModelCallStream(
30323078
List<AgentEvent> events = new ArrayList<>();
30333079
for (ContentBlock block : chunk.getContent()) {
30343080
if (block instanceof TextBlock tb) {
3035-
if (textStarted.compareAndSet(false, true)) {
3036-
events.add(
3037-
new TextBlockStartEvent(
3038-
replyId, "text"));
3039-
}
3081+
blockLifecycle.startText(events);
30403082
if (tb.getText() != null
30413083
&& !tb.getText().isEmpty()) {
30423084
events.add(
30433085
new TextBlockDeltaEvent(
3044-
replyId, "text", tb.getText()));
3086+
blockLifecycle.replyId,
3087+
"text",
3088+
tb.getText()));
30453089
}
30463090
} else if (block instanceof ThinkingBlock tb) {
3047-
if (thinkingStarted.compareAndSet(false, true)) {
3048-
events.add(
3049-
new ThinkingBlockStartEvent(
3050-
replyId, "thinking"));
3051-
}
3091+
blockLifecycle.startThinking(events);
30523092
if (tb.getThinking() != null
30533093
&& !tb.getThinking().isEmpty()) {
30543094
events.add(
30553095
new ThinkingBlockDeltaEvent(
3056-
replyId,
3096+
blockLifecycle.replyId,
30573097
"thinking",
30583098
tb.getThinking()));
30593099
}
@@ -3066,12 +3106,7 @@ private Flux<AgentEvent> summaryModelCallStream(
30663106
Flux.defer(
30673107
() -> {
30683108
List<AgentEvent> events = new ArrayList<>();
3069-
if (textStarted.get()) {
3070-
events.add(new TextBlockEndEvent(replyId, "text"));
3071-
}
3072-
if (thinkingStarted.get()) {
3073-
events.add(new ThinkingBlockEndEvent(replyId, "thinking"));
3074-
}
3109+
blockLifecycle.flushAll(events);
30753110
events.add(new ModelCallEndEvent(replyId, context.getChatUsage()));
30763111
return Flux.fromIterable(events);
30773112
});

0 commit comments

Comments
 (0)