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
60 changes: 49 additions & 11 deletions agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,7 @@ private Mono<Msg> reasoning(int iter, boolean ignoreMaxIters) {
ToolSchema.builder()
.name(soTool.getName())
.description(soTool.getDescription())
.title(soTool.getTitle())
.parameters(soTool.getParameters())
.strict(soTool.getStrict())
.outputSchema(soTool.getOutputSchema())
Expand Down Expand Up @@ -2042,7 +2043,7 @@ private Flux<AgentEvent> modelCallStream(
String replyId = UUID.randomUUID().toString().replace("-", "");
AtomicBoolean textStarted = new AtomicBoolean(false);
AtomicBoolean thinkingStarted = new AtomicBoolean(false);
Map<String, String> startedToolCalls = new ConcurrentHashMap<>();
Map<String, String[]> startedToolCalls = new ConcurrentHashMap<>();

Flux<AgentEvent> modelEvents =
mci.model().stream(mci.messages(), mci.tools(), mci.options())
Expand All @@ -2069,7 +2070,8 @@ private Flux<AgentEvent> modelCallStream(
thinkingStarted,
withToolEvents
? startedToolCalls
: new ConcurrentHashMap<>(),
: new ConcurrentHashMap<
String, String[]>(),
events);
}
return Flux.fromIterable(events);
Expand All @@ -2085,10 +2087,13 @@ private Flux<AgentEvent> modelCallStream(
if (thinkingStarted.get()) {
events.add(new ThinkingBlockEndEvent(replyId, "thinking"));
}
for (Map.Entry<String, String> tc : startedToolCalls.entrySet()) {
for (Map.Entry<String, String[]> tc : startedToolCalls.entrySet()) {
events.add(
new ToolCallEndEvent(
replyId, tc.getKey(), tc.getValue()));
replyId,
tc.getKey(),
tc.getValue()[0],
tc.getValue()[1]));
}
events.add(new ModelCallEndEvent(replyId, context.getChatUsage()));
return Flux.fromIterable(events);
Expand All @@ -2103,7 +2108,7 @@ private void emitBlockEvents(
ReasoningContext context,
AtomicBoolean textStarted,
AtomicBoolean thinkingStarted,
Map<String, String> startedToolCalls,
Map<String, String[]> startedToolCalls,
List<AgentEvent> events) {

if (block instanceof TextBlock tb) {
Expand All @@ -2123,9 +2128,12 @@ private void emitBlockEvents(
} else if (block instanceof ToolUseBlock tub) {
String toolId = resolveToolCallId(tub, context);
String toolName = tub.getName();
if (toolId != null && startedToolCalls.putIfAbsent(toolId, toolName) == null) {
String toolTitle = resolveToolTitle(toolName);
if (toolId != null
&& startedToolCalls.putIfAbsent(toolId, new String[] {toolName, toolTitle})
== null) {
if (toolName != null && !toolName.startsWith("__")) {
events.add(new ToolCallStartEvent(replyId, toolId, toolName));
events.add(new ToolCallStartEvent(replyId, toolId, toolName, toolTitle));
}
}
if (tub.getContent() != null && !tub.getContent().isEmpty()) {
Expand All @@ -2134,6 +2142,7 @@ private void emitBlockEvents(
replyId,
toolId != null ? toolId : "",
toolName,
toolTitle,
tub.getContent()));
}
}
Expand All @@ -2147,6 +2156,12 @@ private String resolveToolCallId(ToolUseBlock tub, ReasoningContext context) {
return accumulated != null ? accumulated.getId() : null;
}

private String resolveToolTitle(String toolName) {
if (toolName == null) return null;
AgentTool tool = toolkit.getTool(toolName);
return tool != null ? tool.getTitle() : null;
}

/**
* Execute the acting phase.
*
Expand Down Expand Up @@ -2372,18 +2387,24 @@ private Flux<AgentEvent> runToolBatch(
.concatMap(
entry -> {
ToolUseBlock use = entry.getKey();
String toolTitle = resolveToolTitle(use.getName());
return Flux.<AgentEvent>just(
new ToolResultStartEvent(
replyId, use.getId(), use.getName()),
replyId,
use.getId(),
use.getName(),
toolTitle),
new ToolResultTextDeltaEvent(
replyId,
use.getId(),
use.getName(),
toolTitle,
"Permission denied by user"),
new ToolResultEndEvent(
replyId,
use.getId(),
use.getName(),
toolTitle,
ToolResultState.DENIED));
});

Expand All @@ -2404,11 +2425,14 @@ private Flux<AgentEvent> runToolBatch(
Flux.<AgentEvent>create(
sink -> {
for (ToolUseBlock tool : approved) {
String toolTitle =
resolveToolTitle(tool.getName());
sink.next(
new ToolResultStartEvent(
replyId,
tool.getId(),
tool.getName()));
tool.getName(),
toolTitle));
}

Set<String> chunkedToolIds =
Expand All @@ -2420,6 +2444,9 @@ private Flux<AgentEvent> runToolBatch(
&& !chunk.getOutput()
.isEmpty()) {
chunkedToolIds.add(toolUse.getId());
String chunkToolTitle =
resolveToolTitle(
toolUse.getName());
for (ContentBlock block :
chunk.getOutput()) {
if (block
Expand All @@ -2432,6 +2459,7 @@ private Flux<AgentEvent> runToolBatch(
.getId(),
toolUse
.getName(),
chunkToolTitle,
tb
.getText()));
} else {
Expand All @@ -2442,6 +2470,7 @@ private Flux<AgentEvent> runToolBatch(
.getId(),
toolUse
.getName(),
chunkToolTitle,
block));
}
}
Expand Down Expand Up @@ -2478,13 +2507,18 @@ private Flux<AgentEvent> runToolBatch(
determineToolResultState(
entry
.getValue());
String endToolTitle =
resolveToolTitle(
entry.getKey()
.getName());
sink.next(
new ToolResultEndEvent(
replyId,
entry.getKey()
.getId(),
entry.getKey()
.getName(),
endToolTitle,
state));
}
sink.complete();
Expand Down Expand Up @@ -2594,6 +2628,7 @@ private void emitToolResultDelta(
Set<String> chunkedToolIds) {
String toolId = entry.getKey().getId();
String toolName = entry.getKey().getName();
String toolTitle = resolveToolTitle(toolName);
if (chunkedToolIds.contains(toolId)) {
return;
}
Expand All @@ -2604,9 +2639,12 @@ private void emitToolResultDelta(
for (ContentBlock block : output) {
if (block instanceof TextBlock tb) {
sink.next(
new ToolResultTextDeltaEvent(replyId, toolId, toolName, tb.getText()));
new ToolResultTextDeltaEvent(
replyId, toolId, toolName, toolTitle, tb.getText()));
} else {
sink.next(new ToolResultDataDeltaEvent(replyId, toolId, toolName, block));
sink.next(
new ToolResultDataDeltaEvent(
replyId, toolId, toolName, toolTitle, block));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ToolCallDeltaEvent extends AgentEvent {
private final String replyId;
private final String toolCallId;
private final String toolCallName;
private final String toolCallTitle;
private final String delta;

@JsonCreator
Expand All @@ -35,19 +36,26 @@ public ToolCallDeltaEvent(
@JsonProperty("replyId") String replyId,
@JsonProperty("toolCallId") String toolCallId,
@JsonProperty("toolCallName") String toolCallName,
@JsonProperty("toolCallTitle") String toolCallTitle,
@JsonProperty("delta") String delta) {
super(id, createdAt);
this.replyId = replyId;
this.toolCallId = toolCallId;
this.toolCallName = toolCallName;
this.toolCallTitle = toolCallTitle;
this.delta = delta;
}

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

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

public String getToolCallTitle() {
return toolCallTitle;
}

public String getDelta() {
return delta;
}
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 ToolResultDataDeltaEvent extends AgentEvent {
private final String replyId;
private final String toolCallId;
private final String toolCallName;
private final String toolCallTitle;
private final ContentBlock data;

@JsonCreator
Expand All @@ -33,19 +34,26 @@ public ToolResultDataDeltaEvent(
@JsonProperty("replyId") String replyId,
@JsonProperty("toolCallId") String toolCallId,
@JsonProperty("toolCallName") String toolCallName,
@JsonProperty("toolCallTitle") String toolCallTitle,
@JsonProperty("data") ContentBlock data) {
super(id, createdAt);
this.replyId = replyId;
this.toolCallId = toolCallId;
this.toolCallName = toolCallName;
this.toolCallTitle = toolCallTitle;
this.data = data;
}

public ToolResultDataDeltaEvent(
String replyId, String toolCallId, String toolCallName, ContentBlock data) {
String replyId,
String toolCallId,
String toolCallName,
String toolCallTitle,
ContentBlock data) {
this.replyId = replyId;
this.toolCallId = toolCallId;
this.toolCallName = toolCallName;
this.toolCallTitle = toolCallTitle;
this.data = data;
}

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

public String getToolCallTitle() {
return toolCallTitle;
}

public ContentBlock getData() {
return data;
}
Expand Down
Loading
Loading