Spring AI Alibaba 项目源码学习(九)-其他继承BaseAgent
其他继承BaseAgent 实现分析
请关注微信公众号:阿呆-bot
概述
本文档分析 Spring AI Alibaba Agent Framework 中除 ReactAgent 和 FlowAgent 之外的其他 BaseAgent 实现,主要包括 A2aRemoteAgent(Agent-to-Agent 远程调用)的实现机制。
入口类说明
A2aRemoteAgent - Agent-to-Agent 远程调用
A2aRemoteAgent 实现了 Agent 之间的远程调用能力,支持分布式 Agent 部署和调用。
核心职责:
- 封装远程 Agent 的调用接口
- 管理 AgentCard(Agent 卡片信息)
- 支持流式调用
- 支持状态共享
关键代码:
public class A2aRemoteAgent extends BaseAgent {
Logger logger = Logger.getLogger(A2aRemoteAgent.class.getName());
private final AgentCardWrapper agentCard;
private KeyStrategyFactory keyStrategyFactory;
private String instruction;
private boolean streaming;
private boolean shareState;
// Private constructor for Builder pattern
private A2aRemoteAgent(Builder builder) throws GraphStateException {
super(builder.name, builder.description, builder.includeContents, builder.returnReasoningContents, builder.outputKey, builder.outputKeyStrategy);
this.agentCard = builder.agentCard;
this.keyStrategyFactory = builder.keyStrategyFactory;
this.compileConfig = builder.compileConfig;
this.includeContents = builder.includeContents;
this.streaming = builder.streaming;
this.instruction = builder.instruction;
this.shareState = builder.shareState;
}
@Override
protected StateGraph initGraph() throws GraphStateException {
if (keyStrategyFactory == null) {
this.keyStrategyFactory = () -> {
HashMap<String, KeyStrategy> keyStrategyHashMap = new HashMap<>();
keyStrategyHashMap.put("messages", new AppendStrategy());
return keyStrategyHashMap;
};
}
StateGraph graph = new StateGraph(name, this.keyStrategyFactory);
graph.addNode("A2aNode", AsyncNodeActionWithConfig.node_async(new A2aNodeActionWithConfig(agentCard, includeContents, outputKey, instruction, streaming)));
graph.addEdge(StateGraph.START, "A2aNode");
graph.addEdge("A2aNode", StateGraph.END);
return graph;
}
@Override
public ScheduledAgentTask schedule(ScheduleConfig scheduleConfig) throws GraphStateException {
throw new UnsupportedOperationException("A2aRemoteAgent has not support schedule.");
}
public static Builder builder() {
return new Builder();
}
@Override
public Node asNode(boolean includeContents, boolean returnReasoningContents, String outputKeyToParent) {
return new A2aRemoteAgentNode(this.name, includeContents, returnReasoningContents, outputKeyToParent, this.instruction, this.agentCard, this.streaming, this.shareState, this.getAndCompileGraph());
}
关键特性:
- AgentCard:封装远程 Agent 的元数据(名称、描述、端点等)
- 流式支持:支持流式调用远程 Agent
- 状态共享:支持与远程 Agent 共享状态
- 指令传递:支持向远程 Agent 传递指令
A2aNodeActionWithConfig - 远程调用节点动作
A2aNodeActionWithConfig 是执行远程 Agent 调用的节点动作。
关键代码:
@Override
public Map<String, Object> apply(OverAllState state, RunnableConfig config) throws Exception {
// Extract messages from state
List<Message> messages = (List<Message>) state.value("messages").orElseThrow();
// Prepare request for remote agent
AgentRequest request = AgentRequest.builder()
.messages(messages)
.instruction(instruction)
.shareState(shareState ? state.data() : null)
.build();
// Call remote agent
AgentResponse response;
if (streaming) {
// Handle streaming response
response = agentCard.callStreaming(request);
} else {
// Handle non-streaming response
response = agentCard.call(request);
}
// Update state with response
Map<String, Object> updatedState = new HashMap<>();
if (includeContents) {
updatedState.put("messages", response.getMessages());
}
if (outputKey != null) {
updatedState.put(outputKey, response.getOutput());
}
return updatedState;
}
AgentCardProvider - Agent 卡片提供者
AgentCardProvider 提供 Agent 卡片的获取能力,支持按名称查找或获取默认卡片。
关键代码:
public interface AgentCardProvider {
/**
* Get agent card by name.
* @param name agent name
* @return agent card
*/
AgentCard getAgentCard(String name);
/**
* Get default agent card.
* @return agent card
*/
AgentCard getAgentCard();
/**
* Check if provider supports getting agent card by name.
* @return true if supports
*/
boolean supportGetAgentCardByName();
}
关键类关系
以下 PlantUML 类图展示了 A2aRemoteAgent 的类关系:

关键流程
以下 PlantUML 时序图展示了 A2aRemoteAgent 的调用流程:

实现关键点说明
1. AgentCard 机制
AgentCard 封装了远程 Agent 的元数据:
- 名称和描述:Agent 的标识信息
- 端点信息:远程调用的地址
- 能力描述:支持的功能(如流式调用)
- 协议信息:调用协议(HTTP、gRPC 等)
2. 状态共享机制
A2aRemoteAgent 支持两种状态共享模式:
- 共享状态(shareState=true):将当前状态传递给远程 Agent
- 隔离状态(shareState=false):只传递消息,不共享状态
3. 流式调用支持
支持流式调用远程 Agent:
- 通过
streaming标志控制 - 使用
callStreaming()方法 - 支持流式响应处理
4. 节点转换
A2aRemoteAgent 通过 asNode() 方法转换为节点:
- 创建
A2aRemoteAgentNode - 实现
SubGraphNode接口 - 支持嵌套在 FlowAgent 中使用
5. Builder 模式
使用 Builder 模式构建 A2aRemoteAgent:
- 支持通过
AgentCard或AgentCardProvider配置 - 支持参数验证
- 支持链式调用
总结说明
核心设计理念
- 远程调用抽象:通过 AgentCard 抽象远程 Agent 的调用接口
- 状态管理:支持状态共享和隔离两种模式
- 流式支持:支持流式调用提高响应性
- 可组合性:通过
asNode()支持与其他 Agent 组合
关键优势
- 分布式支持:支持分布式 Agent 部署和调用
- 灵活性:支持多种调用模式和状态共享策略
- 可扩展性:通过 AgentCardProvider 支持动态 Agent 发现
- 一致性:与本地 Agent 使用相同的接口
使用场景
- 分布式系统:Agent 部署在不同节点
- 微服务架构:Agent 作为微服务提供能力
- Agent 编排:在 FlowAgent 中调用远程 Agent
- 服务发现:通过 AgentCardProvider 动态发现 Agent
A2aRemoteAgent 为 Agent Framework 提供了分布式调用能力,使开发者能够构建跨节点的多 Agent 系统。

浙公网安备 33010602011771号