尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

Spring AI Alibaba 项目源码学习(九)-其他继承BaseAgent

Spring AI Alibaba 项目源码学习(九)-其他继承BaseAgent
📅 发布时间:2026/6/19 15:26:53

其他继承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 patternprivate 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;}@Overrideprotected 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;}@Overridepublic ScheduledAgentTask schedule(ScheduleConfig scheduleConfig) throws GraphStateException {throw new UnsupportedOperationException("A2aRemoteAgent has not support schedule.");}public static Builder builder() {return new Builder();}@Overridepublic 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 调用的节点动作。

关键代码:

@Overridepublic Map<String, Object> apply(OverAllState state, RunnableConfig config) throws Exception {// Extract messages from stateList<Message> messages = (List<Message>) state.value("messages").orElseThrow();// Prepare request for remote agentAgentRequest request = AgentRequest.builder().messages(messages).instruction(instruction).shareState(shareState ? state.data() : null).build();// Call remote agentAgentResponse response;if (streaming) {// Handle streaming responseresponse = agentCard.callStreaming(request);} else {// Handle non-streaming responseresponse = agentCard.call(request);}// Update state with responseMap<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 的类关系:

image.png

关键流程

以下 PlantUML 时序图展示了 A2aRemoteAgent 的调用流程:
image.png

实现关键点说明

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 配置
  • 支持参数验证
  • 支持链式调用

总结说明

核心设计理念

  1. 远程调用抽象:通过 AgentCard 抽象远程 Agent 的调用接口
  2. 状态管理:支持状态共享和隔离两种模式
  3. 流式支持:支持流式调用提高响应性
  4. 可组合性:通过 asNode() 支持与其他 Agent 组合

关键优势

  • 分布式支持:支持分布式 Agent 部署和调用
  • 灵活性:支持多种调用模式和状态共享策略
  • 可扩展性:通过 AgentCardProvider 支持动态 Agent 发现
  • 一致性:与本地 Agent 使用相同的接口

使用场景

  • 分布式系统:Agent 部署在不同节点
  • 微服务架构:Agent 作为微服务提供能力
  • Agent 编排:在 FlowAgent 中调用远程 Agent
  • 服务发现:通过 AgentCardProvider 动态发现 Agent

A2aRemoteAgent 为 Agent Framework 提供了分布式调用能力,使开发者能够构建跨节点的多 Agent 系统。

相关新闻

  • Linux进程状态 - 教程
  • mybatis_generate_demo
  • 换歌换歌

最新新闻

  • 2026无保卡表盒无需担心,青岛本地甄选名表回收门店实测变现技巧 - 讯息早知道
  • 2026 杭州奢侈品回收实测:5家门店综合评级榜单 - 讯息早知道
  • AI辅助决策与GTO策略:构建你的扑克智能工作流
  • 深入解析NXP SB0800 SPI接口:从硬件连接到故障诊断的嵌入式驱动实践
  • 微信小程序二维码生成实战:weapp-qrcode高效解决方案深度解析
  • uniTerm v1.0正式发布内置自主AI Agent一站式跨平台全能终端

日新闻

  • 信任的进化:技术实现详解——如何用JavaScript构建博弈论模拟器
  • Terrakube自定义工作流:如何集成OPA、Infracost等工具扩展IaC能力
  • grunt-concurrent快速入门:5分钟学会并行运行Grunt任务

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号