Skip to content

Commit 79bcfe9

Browse files
committed
refactor(SubAgent): Optimization
1. The `remove` method in the `SubAgentPendingStore` class now returns the removed object instead of void type. 2. Correspondingly, the places where this method is called in the `SubAgentContext` class have been updated, directly using the returned result for subsequent processing. 3. At the same time, some unnecessary method definitions, such as `clearToolResult`, have been cleaned up. 4. The relevant documentation and test cases have been updated to match the new API changes. These changes have made the code more concise and easier to maintain.
1 parent cc6448d commit 79bcfe9

8 files changed

Lines changed: 18 additions & 47 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public void submitSubAgentResult(String subAgentToolId, ToolResultBlock pendingR
286286
if (!isEnableSubAgentHITL()) {
287287
throw new IllegalStateException(
288288
"SubAgent HITL is not enabled. Please enable it via"
289-
+ " builder.enableSubAgentHitl(true)");
289+
+ " builder.enableSubAgentHITL(true)");
290290
}
291291

292292
if (pendingResult == null) {
@@ -311,7 +311,7 @@ public void submitSubAgentResults(String subAgentToolId, List<ToolResultBlock> p
311311
if (!isEnableSubAgentHITL()) {
312312
throw new IllegalStateException(
313313
"SubAgent HITL is not enabled. Please enable it via"
314-
+ " builder.enableSubAgentHitl(true)");
314+
+ " builder.enableSubAgentHITL(true)");
315315
}
316316

317317
if (pendingResults == null || pendingResults.isEmpty()) {
@@ -1489,7 +1489,7 @@ public Builder subAgentContext(SubAgentContext subAgentContext) {
14891489
* to handle sub-agent suspension and resumption. This allows sub-agents to be
14901490
* suspended when they need user input and resumed when the user provides results.
14911491
*
1492-
* @param enableSubAgentHITL true to enable sub-agent HITL support (default: true)
1492+
* @param enableSubAgentHITL true to enable sub-agent HITL support (default: false)
14931493
* @return This builder instance for method chaining
14941494
* @see SubAgentHook
14951495
*/

agentscope-core/src/main/java/io/agentscope/core/tool/subagent/SubAgentContext.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,27 +152,10 @@ public Optional<String> getSessionId(String toolId) {
152152
* @return An Optional containing the pending context, or empty if none exist
153153
*/
154154
public Optional<SubAgentPendingContext> consumePendingResult(String toolId) {
155-
if (!pendingStore.contains(toolId)) {
155+
if (toolId == null) {
156156
return Optional.empty();
157157
}
158-
SubAgentPendingContext pending =
159-
new SubAgentPendingContext(
160-
toolId,
161-
pendingStore.getSessionId(toolId),
162-
pendingStore.getPendingResults(toolId));
163-
clearToolResult(toolId);
164-
return Optional.of(pending);
165-
}
166-
167-
/**
168-
* Clears the pending context for a tool.
169-
*
170-
* <p>This removes both the session ID and all pending results for the tool.
171-
*
172-
* @param toolId The tool ID
173-
*/
174-
public void clearToolResult(String toolId) {
175-
pendingStore.remove(toolId);
158+
return Optional.ofNullable(pendingStore.remove(toolId));
176159
}
177160

178161
/**
@@ -304,7 +287,6 @@ public void saveTo(Session session, SessionKey sessionKey) {
304287
*/
305288
@Override
306289
public void loadFrom(Session session, SessionKey sessionKey) {
307-
this.pendingStore = null;
308290
session.get(sessionKey, "subagent_context", SubAgentPendingStore.class)
309291
.ifPresent(state -> this.pendingStore = state);
310292
}

agentscope-core/src/main/java/io/agentscope/core/tool/subagent/SubAgentHook.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ private Mono<PreActingEvent> handlePreActing(PreActingEvent event) {
138138
metadata.put(PREVIOUS_TOOL_RESULT, pendingResult.get());
139139
Map<String, Object> newInput = new HashMap<>(toolUse.getInput());
140140
newInput.put("session_id", pendingContext.get().sessionId());
141-
context.clearToolResult(toolUse.getId());
142141

143142
ToolUseBlock modifiedToolUse =
144143
ToolUseBlock.builder()

agentscope-core/src/main/java/io/agentscope/core/tool/subagent/SubAgentPendingStore.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,11 @@ public void addResults(String toolId, List<ToolResultBlock> result) {
200200
*
201201
* @param toolId The tool ID
202202
*/
203-
public void remove(String toolId) {
203+
public SubAgentPendingContext remove(String toolId) {
204204
if (toolId != null) {
205-
toolIdToContext.remove(toolId);
205+
return toolIdToContext.remove(toolId);
206206
}
207+
return null;
207208
}
208209

209210
/**

agentscope-core/src/test/java/io/agentscope/core/tool/subagent/SubAgentContextTest.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.agentscope.core.tool.subagent;
1717

1818
import static org.junit.Assert.assertNull;
19+
import static org.junit.Assert.assertSame;
1920
import static org.junit.Assert.assertThrows;
2021
import static org.junit.jupiter.api.Assertions.assertEquals;
2122
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -269,20 +270,6 @@ void testClear() {
269270
assertFalse(context.getSessionId("tool-2").isPresent());
270271
}
271272

272-
@Test
273-
@DisplayName("Should clear only pending results when specified")
274-
void testClearPendingResults() {
275-
context.setSessionId("tool-1", "session-1");
276-
context.submitSubAgentResult("tool-1", createToolResultBlock("tool-1", "Result 1"));
277-
context.setSessionId("tool-2", "session-2");
278-
279-
// Clear all pending results
280-
context.clearToolResult("tool-1");
281-
282-
assertFalse(context.hasPendingResult("tool-1"));
283-
assertTrue(context.getSessionId("tool-2").isPresent());
284-
}
285-
286273
@Test
287274
@DisplayName("Should maintain state after multiple operations")
288275
void testStateAfterMultipleOperations() {
@@ -699,10 +686,12 @@ void testLoadFromNonExistentSession() {
699686

700687
// Should not throw exception
701688
SubAgentContext newContext = new SubAgentContext();
702-
newContext.loadFrom(session, nonExistentKey);
689+
SubAgentPendingStore oldStore = newContext.getPendingStore();
703690

691+
newContext.loadFrom(session, nonExistentKey);
692+
SubAgentPendingStore newStore = newContext.getPendingStore();
704693
// Context should remain empty
705-
assertNull(newContext.getPendingStore());
694+
assertSame(oldStore, newStore);
706695
}
707696

708697
@Test

agentscope-core/src/test/java/io/agentscope/core/tool/subagent/SubAgentHITLTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ void testResumeFromPausedState() {
308308
registerSubAgent(mockSubAgent);
309309

310310
MockModel mainModel =
311-
createSequentialToolModel("call_pausablesubagent", 2, "All done!");
311+
createSequentialToolModel("call_pausablesubagent", 1, "All done!");
312312
ReActAgent mainAgent = createHitlAgent(mainModel, context);
313313

314314
Msg response1 = mainAgent.call(userMessage("Start task")).block(TEST_TIMEOUT);

docs/en/multi-agent/agent-as-tool.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ while (response.getGenerateReason() == GenerateReason.TOOL_SUSPENDED) {
213213
.map(t -> ToolResultBlock.of(t.getId(), t.getName(),
214214
TextBlock.builder().text("Operation cancelled").build()))
215215
.toList();
216-
mainAgent.submitSubAgentResult(resultBlock.getId(), cancelResults);
216+
mainAgent.submitSubAgentResults(resultBlock.getId(), cancelResults);
217217
}
218218
response = mainAgent.call().block();
219219
}
@@ -238,4 +238,4 @@ System.out.println(response.getTextContent());
238238
**Resume methods**:
239239
- `mainAgent.call()` — Continue executing pending tools
240240
- `mainAgent.submitSubAgentResult(String, ToolResultBlock)` — Submit single tool result
241-
- `mainAgent.submitSubAgentResult(String, List<ToolResultBlock>)` — Submit multiple tool results
241+
- `mainAgent.submitSubAgentResults(String, List<ToolResultBlock>)` — Submit multiple tool results

docs/zh/multi-agent/agent-as-tool.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ while (response.getGenerateReason() == GenerateReason.TOOL_SUSPENDED) {
213213
.map(t -> ToolResultBlock.of(t.getId(), t.getName(),
214214
TextBlock.builder().text("操作已取消").build()))
215215
.toList();
216-
mainAgent.submitSubAgentResult(resultBlock.getId(), cancelResults);
216+
mainAgent.submitSubAgentResults(resultBlock.getId(), cancelResults);
217217
}
218218
response = mainAgent.call().block();
219219
}
@@ -237,4 +237,4 @@ System.out.println(response.getTextContent());
237237

238238
**恢复方法**
239239
- `ReActAgent.submitSubAgentResult(String, ToolResultBlock)` — 提交单个工具结果
240-
- `ReActAgent.submitSubAgentResult(String, List<ToolResultBlock>)` — 批量提交工具结果
240+
- `ReActAgent.submitSubAgentResults(String, List<ToolResultBlock>)` — 批量提交工具结果

0 commit comments

Comments
 (0)