From 2b0895451e8c34380ae855f3b6ff6dded9ebf426 Mon Sep 17 00:00:00 2001 From: 0007 <0007@qq.com> Date: Wed, 27 Aug 2025 19:58:11 +0800 Subject: [PATCH] Add File --- .../core/test/chain/ConfirmNodeTest.java | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 agents-flex-core/src/test/java/com/agentsflex/core/test/chain/ConfirmNodeTest.java diff --git a/agents-flex-core/src/test/java/com/agentsflex/core/test/chain/ConfirmNodeTest.java b/agents-flex-core/src/test/java/com/agentsflex/core/test/chain/ConfirmNodeTest.java new file mode 100644 index 0000000..f93cc93 --- /dev/null +++ b/agents-flex-core/src/test/java/com/agentsflex/core/test/chain/ConfirmNodeTest.java @@ -0,0 +1,126 @@ +package com.agentsflex.core.test.chain; + +import com.agentsflex.core.chain.*; +import com.agentsflex.core.chain.listener.ChainSuspendListener; +import com.agentsflex.core.chain.node.ConfirmNode; +import com.agentsflex.core.util.Maps; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ConfirmNodeTest { + + @Test + public void test() { + + Chain chain = new Chain(); + + ChainNode a = new ChainNode() { + @Override + protected Map execute(Chain chain) { + System.out.println("a-->>execute"); + return Maps.of(); + } + }; + a.setId("a"); + chain.addNode(a); + + ChainNode b = new ChainNode() { + @Override + protected Map execute(Chain chain) { + System.out.println("b-->>execute"); + + Map image1 = new HashMap<>(); + image1.put("src", "https://image1"); + + Map image2 = new HashMap<>(); + image2.put("src", "https://image2"); + + List> images = new ArrayList<>(); + images.add(image1); + images.add(image2); + + + return Maps.of("images", images); + } + }; + b.setId("b"); + chain.addNode(b); + + + ConfirmNode c = new ConfirmNode(); + c.setMessage("请确认 xx 是否正确?"); + c.setId("c"); + + Parameter p = new Parameter(); + p.setName("image"); + p.setRef("b.images.src"); + p.setRequired(true); + + List confirms = new ArrayList<>(); + confirms.add(p); + c.setConfirms(confirms); + + chain.addNode(c); + + ChainNode d = new ChainNode() { + @Override + protected Map execute(Chain chain) { + System.out.println("d-->>execute"); + return Maps.of(); + } + }; + d.setId("d"); +// d.setLoopEnable(true); +// d.setMaxLoopCount(3); +// d.setLoopIntervalMs(1000L * 5); + chain.addNode(d); + + ChainEdge ab = new ChainEdge(); + ab.setSource("a"); + ab.setTarget("b"); + chain.addEdge(ab); + + ChainEdge bc = new ChainEdge(); + bc.setSource("b"); + bc.setTarget("c"); + chain.addEdge(bc); + + ChainEdge cd = new ChainEdge(); + cd.setSource("c"); + cd.setTarget("d"); + chain.addEdge(cd); + + + chain.addSuspendListener(new ChainSuspendListener() { + @Override + public void onSuspend(Chain chain) { + System.out.println("suspend!!!"); + } + }); + + try { + // A→B→C(ConfirmNode)→D + chain.executeForResult(new HashMap<>()); + } catch (ChainSuspendException e) { + List suspendForParameters = chain.getSuspendForParameters(); + System.out.println("suspendForParameters:" + suspendForParameters); + + + String json = chain.toJSON(); + System.out.println("json:" + json); + Chain newChain = Chain.fromJSON(json); + + Map data = new HashMap<>(); + for (Parameter parameter : suspendForParameters) { + data.put(parameter.getName(), parameter.getEnums().get(0)); + } + newChain.resume(data); + System.out.println("result:: " + newChain.getMemory().getAll()); + } + + } +}