Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 62 additions & 8 deletions agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.agentscope.core;

import static io.agentscope.core.util.ToolUtils.resolveToolTitle;

import com.fasterxml.jackson.databind.JsonNode;
import io.agentscope.core.agent.Agent;
import io.agentscope.core.agent.AgentBase;
Expand Down Expand Up @@ -2025,6 +2027,37 @@ private Mono<Msg> runPostReasoningPipeline(Msg msg, int iter) {
event -> {
Msg eventMsg = event.getReasoningMessage();
if (eventMsg != null) {
if (eventMsg.hasContentBlocks(ToolUseBlock.class)) {
List<ContentBlock> newContent =
eventMsg.getContent().stream()
.map(
block ->
block
instanceof
ToolUseBlock
tub
? tub.withTitle(
resolveToolTitle(
toolkit,
tub
.getName()))
: block)
.toList();
Msg newEventMsg =
Msg.builder()
.id(eventMsg.getId())
.name(eventMsg.getName())
.role(eventMsg.getRole())
.metadata(eventMsg.getMetadata())
.generateReason(
eventMsg.getGenerateReason())
.timestamp(eventMsg.getTimestamp())
.usage(eventMsg.getUsage())
.content(newContent)
.build();
eventMsg = newEventMsg;
event.setReasoningMessage(newEventMsg);
}
state.contextMutable().add(eventMsg);
}

Expand Down Expand Up @@ -2215,7 +2248,12 @@ private void startToolCall(String toolId, String toolName, List<AgentEvent> even
flushAllToolCalls(events);
boolean visibleTool = toolName != null && !toolName.startsWith("__");
if (visibleTool && startedToolCalls.putIfAbsent(toolId, toolName) == null) {
events.add(new ToolCallStartEvent(replyId, toolId, toolName));
events.add(
new ToolCallStartEvent(
replyId,
toolId,
toolName,
resolveToolTitle(toolkit, toolName)));
}
}

Expand All @@ -2233,7 +2271,12 @@ private void flushThinking(List<AgentEvent> events) {

private void flushAllToolCalls(List<AgentEvent> events) {
for (Map.Entry<String, String> tc : startedToolCalls.entrySet()) {
events.add(new ToolCallEndEvent(replyId, tc.getKey(), tc.getValue()));
events.add(
new ToolCallEndEvent(
replyId,
tc.getKey(),
tc.getValue(),
resolveToolTitle(toolkit, tc.getValue())));
}
startedToolCalls.clear();
}
Expand Down Expand Up @@ -2481,7 +2524,10 @@ private Flux<AgentEvent> runToolBatch(
ToolUseBlock use = entry.getKey();
return Flux.<AgentEvent>just(
new ToolResultStartEvent(
replyId, use.getId(), use.getName()),
replyId,
use.getId(),
use.getName(),
resolveToolTitle(toolkit, use.getName())),
new ToolResultTextDeltaEvent(
replyId,
use.getId(),
Expand All @@ -2491,6 +2537,7 @@ private Flux<AgentEvent> runToolBatch(
replyId,
use.getId(),
use.getName(),
resolveToolTitle(toolkit, use.getName()),
ToolResultState.DENIED));
});

Expand All @@ -2515,7 +2562,10 @@ private Flux<AgentEvent> runToolBatch(
new ToolResultStartEvent(
replyId,
tool.getId(),
tool.getName()));
tool.getName(),
resolveToolTitle(
toolkit,
tool.getName())));
}

Set<String> chunkedToolIds =
Expand Down Expand Up @@ -2931,6 +2981,9 @@ private Mono<PostActingEvent> notifyPostActingHook(
ToolUseBlock toolUse = entry.getKey();
ToolResultBlock result = entry.getValue();

ToolUseBlock newToolUse =
toolUse.withTitle(resolveToolTitle(toolkit, toolUse.getName()));

// FIX: determine the final state and update ToolResultBlock before
// adding to contextMutable(), so that history queries via
// agent.getAgentState(ctx).contextMutable() reflect the correct
Expand All @@ -2939,17 +2992,18 @@ private Mono<PostActingEvent> notifyPostActingHook(
ToolResultBlock updatedResult = result.withState(finalState);

Msg toolMsg =
ToolResultMessageBuilder.buildToolResultMsg(updatedResult, toolUse, getName());
ToolResultMessageBuilder.buildToolResultMsg(
updatedResult, newToolUse, getName());

return hookDispatcher
.firePostActing(toolUse, updatedResult, toolkit, toolMsg)
.doOnNext(
e -> {
if (soTool != null
&& STRUCTURED_OUTPUT_TOOL_NAME.equals(toolUse.getName())
&& result.getMetadata() != null
&& STRUCTURED_OUTPUT_TOOL_NAME.equals(newToolUse.getName())
&& updatedResult.getMetadata() != null
&& Boolean.TRUE.equals(
result.getMetadata().get("success"))) {
updatedResult.getMetadata().get("success"))) {
soCompleted = true;
soResultMsg = e.getToolResultMsg();
e.stopAgent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class ToolCallsAccumulator implements ContentAccumulator<ToolUseBlock> {
private static class ToolCallBuilder {
String toolId;
String name;
String title;
Map<String, Object> args = new HashMap<>();
StringBuilder rawContent = new StringBuilder();
Map<String, Object> metadata = new HashMap<>();
Expand All @@ -67,6 +68,10 @@ void merge(ToolUseBlock block) {
this.name = block.getName();
}

if (block.getTitle() != null) {
this.title = block.getTitle();
}

// Merge parameters
if (block.getInput() != null) {
this.args.putAll(block.getInput());
Expand Down Expand Up @@ -116,6 +121,7 @@ ToolUseBlock build() {
return ToolUseBlock.builder()
.id(toolId != null ? toolId : generateId())
.name(name)
.title(title)
.input(finalArgs)
.content(contentStr)
.metadata(metadata.isEmpty() ? null : metadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,29 @@ public class ToolCallEndEvent extends AgentEvent {
private final String replyId;
private final String toolCallId;
private final String toolCallName;
private final String toolCallTitle;

@JsonCreator
public ToolCallEndEvent(
@JsonProperty("id") String id,
@JsonProperty("createdAt") String createdAt,
@JsonProperty("replyId") String replyId,
@JsonProperty("toolCallId") String toolCallId,
@JsonProperty("toolCallName") String toolCallName) {
@JsonProperty("toolCallName") String toolCallName,
@JsonProperty("toolCallTitle") String toolCallTitle) {
super(id, createdAt);
this.replyId = replyId;
this.toolCallId = toolCallId;
this.toolCallName = toolCallName;
this.toolCallTitle = toolCallTitle;
}

public ToolCallEndEvent(String replyId, String toolCallId, String toolCallName) {
public ToolCallEndEvent(
String replyId, String toolCallId, String toolCallName, String toolCallTitle) {
this.replyId = replyId;
this.toolCallId = toolCallId;
this.toolCallName = toolCallName;
this.toolCallTitle = toolCallTitle;
}

@Override
Expand All @@ -59,4 +64,8 @@ public String getToolCallId() {
public String getToolCallName() {
return toolCallName;
}

public String getToolCallTitle() {
return toolCallTitle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,29 @@ public class ToolCallStartEvent extends AgentEvent {
private final String replyId;
private final String toolCallId;
private final String toolCallName;
private final String toolCallTitle;

@JsonCreator
public ToolCallStartEvent(
@JsonProperty("id") String id,
@JsonProperty("createdAt") String createdAt,
@JsonProperty("replyId") String replyId,
@JsonProperty("toolCallId") String toolCallId,
@JsonProperty("toolCallName") String toolCallName) {
@JsonProperty("toolCallName") String toolCallName,
@JsonProperty("toolCallTitle") String toolCallTitle) {
super(id, createdAt);
this.replyId = replyId;
this.toolCallId = toolCallId;
this.toolCallName = toolCallName;
this.toolCallTitle = toolCallTitle;
}

public ToolCallStartEvent(String replyId, String toolCallId, String toolCallName) {
public ToolCallStartEvent(
String replyId, String toolCallId, String toolCallName, String toolCallTitle) {
this.replyId = replyId;
this.toolCallId = toolCallId;
this.toolCallName = toolCallName;
this.toolCallTitle = toolCallTitle;
}

@Override
Expand All @@ -59,4 +64,8 @@ public String getToolCallId() {
public String getToolCallName() {
return toolCallName;
}

public String getToolCallTitle() {
return toolCallTitle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ToolResultEndEvent extends AgentEvent {
private final String replyId;
private final String toolCallId;
private final String toolCallName;
private final String toolCallTitle;
private final ToolResultState state;

@JsonCreator
Expand All @@ -33,19 +34,31 @@ public ToolResultEndEvent(
@JsonProperty("replyId") String replyId,
@JsonProperty("toolCallId") String toolCallId,
@JsonProperty("toolCallName") String toolCallName,
@JsonProperty("toolCallTitle") String toolCallTitle,
@JsonProperty("state") ToolResultState state) {
super(id, createdAt);
this.replyId = replyId;
this.toolCallId = toolCallId;
this.toolCallName = toolCallName;
this.toolCallTitle = toolCallTitle;
this.state = state;
}

public ToolResultEndEvent(
String replyId, String toolCallId, String toolCallName, ToolResultState state) {
this(replyId, toolCallId, toolCallName, null, state);
}

public ToolResultEndEvent(
String replyId,
String toolCallId,
String toolCallName,
String toolCallTitle,
ToolResultState state) {
this.replyId = replyId;
this.toolCallId = toolCallId;
this.toolCallName = toolCallName;
this.toolCallTitle = toolCallTitle;
this.state = state;
}

Expand All @@ -66,6 +79,10 @@ public String getToolCallName() {
return toolCallName;
}

public String getToolCallTitle() {
return toolCallTitle;
}

public ToolResultState getState() {
return state;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,29 @@ public class ToolResultStartEvent extends AgentEvent {
private final String replyId;
private final String toolCallId;
private final String toolCallName;
private final String toolCallTitle;

@JsonCreator
public ToolResultStartEvent(
@JsonProperty("id") String id,
@JsonProperty("createdAt") String createdAt,
@JsonProperty("replyId") String replyId,
@JsonProperty("toolCallId") String toolCallId,
@JsonProperty("toolCallName") String toolCallName) {
@JsonProperty("toolCallName") String toolCallName,
@JsonProperty("toolCallTitle") String toolCallTitle) {
super(id, createdAt);
this.replyId = replyId;
this.toolCallId = toolCallId;
this.toolCallName = toolCallName;
this.toolCallTitle = toolCallTitle;
}

public ToolResultStartEvent(String replyId, String toolCallId, String toolCallName) {
public ToolResultStartEvent(
String replyId, String toolCallId, String toolCallName, String toolCallTitle) {
this.replyId = replyId;
this.toolCallId = toolCallId;
this.toolCallName = toolCallName;
this.toolCallTitle = toolCallTitle;
}

@Override
Expand All @@ -59,4 +64,8 @@ public String getToolCallId() {
public String getToolCallName() {
return toolCallName;
}

public String getToolCallTitle() {
return toolCallTitle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.agentscope.core.hook;

import static io.agentscope.core.util.ToolUtils.resolveToolTitle;

import io.agentscope.core.ReActAgent;
import io.agentscope.core.agent.accumulator.ReasoningContext;
import io.agentscope.core.message.ContentBlock;
Expand Down Expand Up @@ -83,7 +85,11 @@ public Mono<Void> fireReasoningChunk(Msg chunkMsg, ReasoningContext context, Str
ThinkingBlock.builder().thinking(context.getAccumulatedThinking()).build();
} else if (content instanceof ToolUseBlock tub) {
ToolUseBlock accumulated = context.getAccumulatedToolCall(tub.getId());
accumulatedContent = accumulated != null ? accumulated : tub;
accumulatedContent =
accumulated != null
? accumulated.withTitle(
resolveToolTitle(agent.getToolkit(), accumulated.getName()))
: tub.withTitle(resolveToolTitle(agent.getToolkit(), tub.getName()));
}

if (accumulatedContent != null) {
Expand Down
Loading
Loading