Msg response = agent.call(userMsg).block();
// 检查是否有待确认的工具调用
while (response.hasContentBlocks(ToolUseBlock.class)) {
// 展示待执行的工具
List<ToolUseBlock> pending = response.getContentBlocks(ToolUseBlock.class);
for (ToolUseBlock tool : pending) {
System.out.println("工具: " + tool.getName());
System.out.println("参数: " + tool.getInput());
}
if (userConfirms()) {
// 用户确认,继续执行
response = agent.call().block();
} else {
// 用户拒绝,返回取消信息
Msg cancelResult = Msg.builder()
.role(MsgRole.TOOL)
.content(pending.stream()
.map(t -> ToolResultBlock.of(t.getId(), t.getName(),
TextBlock.builder().text("操作已取消").build()))
.toArray(ToolResultBlock[]::new))
.build();
response = agent.call(cancelResult).block();
}
}
// 最终响应
System.out.println(response.getTextContent());