Spring Ai Alibaba Graph源码解读系列—action

原文链接地址:Spring Ai Alibaba Graph源码解读系列---action

从今天起,我们来系统梳理下Spring Ai Alibaba Graph的源码,会给出各个部分的功能、作用,以及会新增许多Graph的案例,初步会在一个月内完结。因为目前相关代码正在快速迭代中,维护的飞书在线云文档会同步最新的源码解读及案例,让我们持续跟进业界最新技术,Let's Go!

本期先介绍action部分,主要是接收OverAllState、RunnableConfig对节点、边触发相应的动作

  • 如果你初步使用过Graph框架,你会发现我们的节点都是NodeAction的实现类,条件边都是EdgeAction的实现类

action

NodeAction

函数式接口,定义图中节点的基本同步操作

  • apply 方法:接受当前状态,并返回更新的状态映射
java 复制代码
package com.alibaba.cloud.ai.graph.action;

import com.alibaba.cloud.ai.graph.OverAllState;

import java.util.Map;

@FunctionalInterface
public interface NodeAction {

    Map<String, Object> apply(OverAllState state) throws Exception;

}

AsyncNodeAction

函数式接口,定义节点的异步操作行为,表示接受状态参数并返回异步结果

|-----------|---------------------------------------------------|
| 方法名称 | 描述 |
| apply | 核心执行方法,接受当前状态,返回包含更新状态的 CompletableFuture |
| nodeasync | 将同步的NodeAction转换为异步版本,通过CompletableFuture包装同步操作结果 |

java 复制代码
package com.alibaba.cloud.ai.graph.action;

import com.alibaba.cloud.ai.graph.OverAllState;
import io.opentelemetry.context.Context;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

/**
 * Represents an asynchronous node action that operates on an agent state and returns
 * state update.
 *
 */
@FunctionalInterface
public interface AsyncNodeAction extends Function<OverAllState, CompletableFuture<Map<String, Object>>> {

    /**
     * Applies this action to the given agent state.
     * @param state the agent state
     * @return a CompletableFuture representing the result of the action
     */
    CompletableFuture<Map<String, Object>> apply(OverAllState state);

    /**
     * Creates an asynchronous node action from a synchronous node action.
     * @param syncAction the synchronous node action
     * @return an asynchronous node action
     */
    static AsyncNodeAction nodeasync(NodeAction syncAction) {
       return state -> {
          Context context = Context.current();
          CompletableFuture<Map<String, Object>> result = new CompletableFuture<>();
          try {
             result.complete(syncAction.apply(state));
          }
          catch (Exception e) {
             result.completeExceptionally(e);
          }
          return result;
       };
    }

}

NodeActionWithConfig

函数式接口,定义图中节点的基本同步操作,同时携带运行配置信息

  • apply 方法:接受当前状态和运行配置,并返回更新的状态映射
java 复制代码
package com.alibaba.cloud.ai.graph.action;

import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.RunnableConfig;

import java.util.Map;

@FunctionalInterface
public interface NodeActionWithConfig {

    Map<String, Object> apply(OverAllState state, RunnableConfig config) throws Exception;

}

AsyncNodeActionWithConfig

函数式接口,用于定义节点的异步操作行为,接受状态和配置参数并返回异步结果

|-----------|-------------------------------------------------------------------------|
| 方法名称 | 描述 |
| apply | 核心执行方法,接受当前状态和运行配置,返回包含更新状态的 CompletableFuture |
| nodeasync | 将同步的NodeActionWithConfig转换为异步版本,通过CompletableFuture包装同步操作结果 |
| of | 将简单的AsyncNodeAction (只接受状态参数) 转换为 AsyncNodeActionWithConfig (接受状态和配置参数) |

java 复制代码
package com.alibaba.cloud.ai.graph.action;

import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.RunnableConfig;
import io.opentelemetry.context.Context;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;

public interface AsyncNodeActionWithConfig
       extends BiFunction<OverAllState, RunnableConfig, CompletableFuture<Map<String, Object>>> {

    /**
     * Applies this action to the given agent state.
     * @param state the agent state
     * @return a CompletableFuture representing the result of the action
     */
    CompletableFuture<Map<String, Object>> apply(OverAllState state, RunnableConfig config);

    static AsyncNodeActionWithConfig nodeasync(NodeActionWithConfig syncAction) {
       return (state, config) -> {
          Context context = Context.current();
          CompletableFuture<Map<String, Object>> result = new CompletableFuture<>();
          try {
             result.complete(syncAction.apply(state, config));
          }
          catch (Exception e) {
             result.completeExceptionally(e);
          }
          return result;
       };
    }

    /**
     * Adapts a simple AsyncNodeAction to an AsyncNodeActionWithConfig.
     * @param action the simple AsyncNodeAction to be adapted
     * @return an AsyncNodeActionWithConfig that wraps the given AsyncNodeAction
     */
    static AsyncNodeActionWithConfig of(AsyncNodeAction action) {
       return (t, config) -> action.apply(t);
    }

}

EdgeAction

函数式接口,定义图中边的条件判断操作

  • apply 方法:接受当前状态,返回下一个执行节点 ID
java 复制代码
package com.alibaba.cloud.ai.graph.action;

import com.alibaba.cloud.ai.graph.OverAllState;

/**
 * Represents an edge action that operates on an agent state and returns a result.
 *
 */
@FunctionalInterface
public interface EdgeAction {

    /**
     * Applies this action to the given agent state.
     * @param state the agent state
     * @return a result of the action
     * @throws Exception if an error occurs during the action
     */
    String apply(OverAllState state) throws Exception;

}

AsyncEdgeAction

函数式接口,定义图中边的异步条件判断操作,接受状态参数并返回字符串结果

|-----------|---------------------------------------------------|
| 方法名称 | 描述 |
| apply | 核心执行方法,接受当前状态,返回下一步执行的节点ID |
| edgeasync | 将同步的EdgeAction转换为异步版本,通过CompletableFuture包装同步操作结果 |

java 复制代码
package com.alibaba.cloud.ai.graph.action;

import com.alibaba.cloud.ai.graph.OverAllState;
import io.opentelemetry.context.Context;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

/**
 * Represents an asynchronous edge action that operates on an agent state and returns a
 * new route.
 *
 */
@FunctionalInterface
public interface AsyncEdgeAction extends Function<OverAllState, CompletableFuture<String>> {

    /**
     * Applies this action to the given agent state.
     * @param state the agent state
     * @return a CompletableFuture representing the result of the action
     */
    CompletableFuture<String> apply(OverAllState state);

    /**
     * Creates an asynchronous edge action from a synchronous edge action.
     * @param syncAction the synchronous edge action
     * @return an asynchronous edge action
     */
    static AsyncEdgeAction edgeasync(EdgeAction syncAction) {
       return state -> {
          Context context = Context.current();
          CompletableFuture<String> result = new CompletableFuture<>();
          try {
             result.complete(syncAction.apply(state));
          }
          catch (Exception e) {
             result.completeExceptionally(e);
          }
          return result;
       };
    }

}

Command

封装图执行的接下一个节点,及状态更新

java 复制代码
package com.alibaba.cloud.ai.graph.action;

import com.alibaba.cloud.ai.graph.OverAllState;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * Represents the outcome of a {@link CommandAction} within a LangGraph4j graph. A
 * {@code Command} encapsulates instructions for the graph's next step, including an
 * optional target node to transition to and a map of updates to be applied to the
 * {@link OverAllState}.
 *
 * @param gotoNode containing the name of the next node to execute.
 * @param update A {@link Map} containing key-value pairs representing updates to be
 * merged into the current agent state. An empty map indicates no state updates.
 */
public record Command(String gotoNode, Map<String, Object> update) {

    public Command {
       Objects.requireNonNull(gotoNode, "gotoNode cannot be null");
       Objects.requireNonNull(update, "update cannot be null");
    }

    /**
     * Constructs a {@code Command} that specifies only the next node to transition to,
     * with no state updates. If {@code gotoNode} is null, it will be treated as an empty
     * {@link Optional}.
     * @param gotoNode The name of the next node to transition to. Can be null.
     */
    public Command(String gotoNode) {
       this(gotoNode, Map.of());
    }

}

CommandAction

函数式接口,定义 Command 对象的节点操作

  • apply 方法:接受当前状态和运行配置,返回 Command 对象
java 复制代码
package com.alibaba.cloud.ai.graph.action;

import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.RunnableConfig;

@FunctionalInterface
public interface CommandAction {

    Command apply(OverAllState state, RunnableConfig config) throws Exception;

}

AsyncCommandAction

函数式接口,定义异步的命令动作,接受状态和配置参数并返回异步命令结果

java 复制代码
package com.alibaba.cloud.ai.graph.action;

import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.RunnableConfig;
import io.opentelemetry.context.Context;

import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;

public interface AsyncCommandAction extends BiFunction<OverAllState, RunnableConfig, CompletableFuture<Command>> {

    static AsyncCommandAction nodeasync(CommandAction syncAction) {
       return (state, config) -> {
          Context context = Context.current();
          var result = new CompletableFuture<Command>();
          try {
             result.complete(syncAction.apply(state, config));
          }
          catch (Exception e) {
             result.completeExceptionally(e);
          }
          return result;
       };
    }

    static AsyncCommandAction of(AsyncEdgeAction action) {
       return (state, config) -> action.apply(state).thenApply(Command::new);
    }

}

往期文章

第一章内容

SpringAI(GA)的chat:快速上手+自动注入源码解读

SpringAI(GA):ChatClient调用链路解读

第二章内容

SpringAI的Advisor:快速上手+源码解读

SpringAI(GA):Sqlite、Mysql、Redis消息存储快速上手

第三章内容

SpringAI(GA):Tool工具整合---快速上手

SpringAI(GA):Tool源码+工具触发链路解读

第四章内容

SpringAI(GA):结构化输出的快速上手+源码解读

第五章内容

SpringAI(GA):内存、Redis、ES的向量数据库存储---快速上手

SpringAI(GA):向量数据库理论源码解读+Redis、Es接入源码

第六章内容

SpringAI(GA):RAG快速上手+模块化解读

SpringAI(GA):RAG下的ETL快速上手

SpringAI(GA):RAG下的ETL源码解读

第七章内容

SpringAI(GA):Nacos2下的分布式MCP

SpringAI(GA):Nacos3下的分布式MCP

SpringAI(GA):MCP源码解读

SpringAI(GA): SpringAI下的MCP源码解读

进阶:MCP服务鉴权案例

Spring AI Alibaba MCP Gateway GateWay无缝斜街存量应用转换 MCP 工具

MCP的SSE重连机制,源码分析

MCP的returnDirect生效了?

第八章内容

SpringAI(GA): 多模型评估篇

第九章内容

SpringAI(GA):观测篇快速上手+源码解读

第十章内容

Spring AI Alibaba Graph:快速入门

Spring AI Alibaba Graph:多节点并行---快速上手

Spring AI Alibaba Graph:节点流式透传案例

Spring AI Alibaba Graph:分配MCP到指定节点

Spring AI Alibaba Graph:中断!人类反馈介入,流程丝滑走完~

说明:往前文章可能有些细节有所调整,可69.9付费获取飞书云文档在线版预览,体验感更加,更有专属SpringAI会员教程群答疑

学习交流圈

你好,我是影子,曾先后在🐻、新能源、老铁就职,兼任Spring AI Alibaba开源社区的Committer。目前新建了一个交流群,一个人走得快,一群人走得远,另外,本人长期维护一套飞书云文档笔记,涵盖后端、大数据系统化的面试资料,可私信免费获取

相关推荐
xuejianxinokok16 分钟前
解惑rust中的 Send/Sync(译)
后端·rust
Siler26 分钟前
Oracle利用数据泵进行数据迁移
后端
用户67570498850236 分钟前
3分钟,手摸手教你用OpenResty搭建高性能隧道代理(附完整配置!)
后端
coding随想1 小时前
网络世界的“快递站”:深入浅出OSI七层模型
后端·网络协议
skeletron20111 小时前
🚀AI评测这么玩(2)——使用开源评测引擎eval-engine实现问答相似度评估
前端·后端
shark_chili1 小时前
颠覆认知!这才是synchronized最硬核的打开方式
后端
就是帅我不改1 小时前
99%的Java程序员都写错了!高并发下你的Service层正在拖垮整个系统!
后端·架构
Apifox1 小时前
API 文档中有多种参数结构怎么办?Apifox 里用 oneOf/anyOf/allOf 这样写
前端·后端·测试
似水流年流不尽思念1 小时前
如何实现一个线程安全的单例模式?
后端·面试
楽码1 小时前
了解HMAC及实现步骤
后端·算法·微服务