📤 获取 Agent 结果
# 获取完整回复
完成组件与流程配置后,注入 FlowExecutor,使用通常的 LiteFlow 调用方式:
LiteflowResponse response = flowExecutor.execute2Resp("chatChain", "介绍一下 LiteFlow");
if (!response.isSuccess()) {
throw new IllegalStateException("Agent 执行失败", response.getCause());
}
String answer = response.getSlot().getResponseData();
String conversationId = response.getConversationId();
以上代码使用 com.yomahub.liteflow.core.FlowExecutor 和 com.yomahub.liteflow.flow.LiteflowResponse。默认输出是完整文本;开启结构化输出后,应按指定类型接收。
execute2Resp(...) 会等待本次流程结束。调用返回不等于成功,读取结果前先检查 response.isSuccess(),失败原因从 response.getCause() 获取。
# 自定义回复处理
覆写 handleReply,可将回复或用量写入业务上下文。下面的片段放在自己的 Agent 组件中:
@Override
protected void handleReply(io.agentscope.core.message.Msg reply,
LiteFlowAgentContext context) {
super.handleReply(reply, context);
Object result = context.getSlot().getResponseData();
io.agentscope.core.model.ChatUsage usage = context.getChatUsage();
// 将 result、usage 写入业务上下文或记录本次调用用量。
}
调用 super.handleReply(...) 会保留默认的 responseData 写入。模型未提供用量时,getChatUsage() 可能为空;已成功加载的技能可通过 context.getUsedSkills() 查询。
一条链中有多个 Agent 时,默认结果会依次覆盖 responseData。需要保存各节点结果时,使用独立的业务上下文字段,详见多 Agent 协作。
帮助我们改善此文档 (opens new window)
上次更新: 2026/09/20, 11:49:27



