From e59e89a5adf2c23d8c5d8aa00141683809fb95ab Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Mon, 30 Jun 2025 23:31:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(ai-web):=20=E5=B0=9D=E8=AF=95=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E6=B5=81=E7=A8=8B=E5=BC=95=E6=93=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ai/web/engine/FlowEdge.java | 19 ------------ .../service/ai/web/engine/FlowExecutor.java | 31 +++++++++++++++++++ .../service/ai/web/engine/FlowNode.java | 20 ------------ .../ai/web/engine/entity/FlowContext.java | 10 ++++++ .../ai/web/engine/entity/FlowEdge.java | 16 ++++++++++ .../ai/web/engine/entity/FlowGraph.java | 16 ++++++++++ .../ai/web/engine/entity/FlowNode.java | 17 ++++++++++ .../ai/web/engine/entity/FlowNodeRunner.java | 5 +++ .../ai/web/engine/store/FlowStore.java | 10 ------ 9 files changed, 95 insertions(+), 49 deletions(-) delete mode 100644 service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/FlowEdge.java delete mode 100644 service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/FlowNode.java create mode 100644 service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowContext.java create mode 100644 service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowEdge.java create mode 100644 service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowGraph.java create mode 100644 service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowNode.java create mode 100644 service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowNodeRunner.java delete mode 100644 service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/store/FlowStore.java diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/FlowEdge.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/FlowEdge.java deleted file mode 100644 index d55a4ce..0000000 --- a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/FlowEdge.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.lanyuanxiaoyao.service.ai.web.engine; - -import lombok.Data; -import lombok.EqualsAndHashCode; - -/** - * 流程图中的边 - * - * @author lanyuanxiaoyao - * @version 20250630 - */ -@Data -@EqualsAndHashCode(onlyExplicitlyIncluded = true) -public class FlowEdge { - @EqualsAndHashCode.Include - private String id; - private String source; - private String target; -} diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/FlowExecutor.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/FlowExecutor.java index 7a41e21..85fd701 100644 --- a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/FlowExecutor.java +++ b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/FlowExecutor.java @@ -1,5 +1,15 @@ package com.lanyuanxiaoyao.service.ai.web.engine; +import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext; +import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowEdge; +import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowGraph; +import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowNode; +import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowNodeRunner; +import java.lang.reflect.InvocationTargetException; +import java.util.LinkedList; +import java.util.Queue; +import org.eclipse.collections.api.map.ImmutableMap; + /** * 流程执行器 * @@ -7,4 +17,25 @@ package com.lanyuanxiaoyao.service.ai.web.engine; * @version 20250630 */ public class FlowExecutor { + private final ImmutableMap> runnerMap; + + public FlowExecutor(ImmutableMap> runnerMap) { + this.runnerMap = runnerMap; + } + + public void execute(FlowGraph graph) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + var nodeInputMap = graph.edges().groupBy(FlowEdge::source); + var nodeOutputMap = graph.edges().groupBy(FlowEdge::target); + var startNodes = graph.nodes().select(node -> !nodeInputMap.containsKey(node.id())); + Queue queue = new LinkedList<>(); + for (FlowNode node : startNodes) { + queue.offer(node); + } + while (!queue.isEmpty()) { + var node = queue.poll(); + var clazz = runnerMap.get(node.type()); + var runner = clazz.getDeclaredConstructor(FlowContext.class).newInstance(); + runner.run(); + } + } } diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/FlowNode.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/FlowNode.java deleted file mode 100644 index a942176..0000000 --- a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/FlowNode.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.lanyuanxiaoyao.service.ai.web.engine; - -import lombok.Data; -import lombok.EqualsAndHashCode; -import org.eclipse.collections.api.set.ImmutableSet; - -/** - * 流程图中的节点 - * - * @author lanyuanxiaoyao - * @version 20250630 - */ -@Data -@EqualsAndHashCode(onlyExplicitlyIncluded = true) -public class FlowNode { - @EqualsAndHashCode.Include - private String id; - private ImmutableSet sourceHandlers; - private ImmutableSet targetHandlers; -} diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowContext.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowContext.java new file mode 100644 index 0000000..39ee6c9 --- /dev/null +++ b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowContext.java @@ -0,0 +1,10 @@ +package com.lanyuanxiaoyao.service.ai.web.engine.entity; + +import lombok.Data; +import org.eclipse.collections.api.factory.Maps; +import org.eclipse.collections.api.map.MutableMap; + +@Data +public class FlowContext { + private MutableMap data = Maps.mutable.empty().asSynchronized(); +} diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowEdge.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowEdge.java new file mode 100644 index 0000000..76d80ec --- /dev/null +++ b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowEdge.java @@ -0,0 +1,16 @@ +package com.lanyuanxiaoyao.service.ai.web.engine.entity; + +/** + * 流程图中的边 + * + * @author lanyuanxiaoyao + * @version 20250630 + */ +public record FlowEdge( + String id, + String source, + String target, + String sourcePoint, + String targetPoint +) { +} diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowGraph.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowGraph.java new file mode 100644 index 0000000..d11be20 --- /dev/null +++ b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowGraph.java @@ -0,0 +1,16 @@ +package com.lanyuanxiaoyao.service.ai.web.engine.entity; + +import org.eclipse.collections.api.set.ImmutableSet; + +/** + * 流程图 + * + * @author lanyuanxiaoyao + * @version 20250630 + */ +public record FlowGraph( + String id, + ImmutableSet nodes, + ImmutableSet edges +) { +} diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowNode.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowNode.java new file mode 100644 index 0000000..cba56d2 --- /dev/null +++ b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowNode.java @@ -0,0 +1,17 @@ +package com.lanyuanxiaoyao.service.ai.web.engine.entity; + +import org.eclipse.collections.api.set.ImmutableSet; + +/** + * 流程图中的节点 + * + * @author lanyuanxiaoyao + * @version 20250630 + */ +public record FlowNode( + String id, + String type, + ImmutableSet inputPoints, + ImmutableSet outputPoints +) { +} diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowNodeRunner.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowNodeRunner.java new file mode 100644 index 0000000..85c7d4f --- /dev/null +++ b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/entity/FlowNodeRunner.java @@ -0,0 +1,5 @@ +package com.lanyuanxiaoyao.service.ai.web.engine.entity; + +public abstract class FlowNodeRunner implements Runnable { + private final FlowContext context; +} diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/store/FlowStore.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/store/FlowStore.java deleted file mode 100644 index 0f7d401..0000000 --- a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/engine/store/FlowStore.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.lanyuanxiaoyao.service.ai.web.engine.store; - -/** - * 数据存储 - * - * @author lanyuanxiaoyao - * @version 20250630 - */ -public interface FlowStore { -}