📤 获取 Agent 结果
# 方式 1:通过 slot.responseData(默认)
ReActAgentComponent 默认会把 Agent 的文本回复写入 slot.responseData。
下游节点直接读取:
@LiteflowComponent("recordReply")
public class RecordReplyCmp extends NodeComponent {
@Override
public void process() {
String reply = (String) this.getSlot().getResponseData();
// 后续处理...
}
}
链路结束后,外部调用方也可以获取:
LiteflowResponse response = flowExecutor.execute2Resp("deepseekChain", "你好");
Object reply = response.getSlot().getResponseData();
# 方式 2:覆写 handleReply 写入自定义位置
当链路中有多个 Agent 节点时,默认的 responseData 会被后一个覆盖。建议覆写 handleReply:
@Override
protected void handleReply(Msg reply) {
String text = reply == null ? null : reply.getTextContent();
// 写入 ContextBean
ctx().getSlot().getContextBean(MyAgentCtx.class).setReply(getNodeId(), text);
// 或以 nodeId 为 key 存入 slot
ctx().getSlot().setOutput(getNodeId(), text);
}
下游节点用 slot.getContextBean(MyAgentCtx.class) 或 slot.getOutput(nodeId) 读取。
# 同时获取 token 用量
@Override
protected void handleReply(Msg reply) {
String text = reply == null ? null : reply.getTextContent();
ctx().getSlot().setOutput(getNodeId(), text);
ChatUsage usage = ctx().getChatUsage();
if (usage != null) {
ctx().getSlot().setOutput(getNodeId() + ".usage", Map.of(
"inputTokens", usage.getInputTokens(),
"outputTokens", usage.getOutputTokens(),
"totalTokens", usage.getTotalTokens(),
"timeSeconds", usage.getTime()
));
}
}
ChatUsage 来自 io.agentscope.core.model.ChatUsage。
多 Agent 注意事项
默认 responseData 是 slot 级别的单一字段,后写覆盖先写。链路中存在多个 Re-Act Agent 时,请务必覆写 handleReply,用 setOutput(nodeId, ...) 或自定义 ContextBean 区分各 Agent 的输出。
帮助我们改善此文档 (opens new window)
上次更新: 2026/05/26, 10:50:41


