001/**
002 * Copyright (c) 2025-2026, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.gnu.org/licenses/lgpl-3.0.txt
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package dev.tinyflow.core.parser;
017
018import com.agentsflex.core.chain.Chain;
019import com.agentsflex.core.chain.ChainEdge;
020import com.agentsflex.core.chain.ChainNode;
021import com.agentsflex.core.chain.JavascriptStringCondition;
022import com.agentsflex.core.util.CollectionUtil;
023import com.agentsflex.core.util.StringUtil;
024import com.alibaba.fastjson.JSON;
025import com.alibaba.fastjson.JSONArray;
026import com.alibaba.fastjson.JSONObject;
027import dev.tinyflow.core.Tinyflow;
028import dev.tinyflow.core.parser.impl.*;
029
030import java.util.HashMap;
031import java.util.Map;
032
033public class ChainParser {
034
035    private Map<String, NodeParser> nodeParserMap = new HashMap<>();
036
037    public ChainParser() {
038
039        initDefaultParsers();
040    }
041
042    private void initDefaultParsers() {
043        nodeParserMap.put("startNode", new StartNodeParser());
044        nodeParserMap.put("codeNode", new CodeNodeParser());
045
046        nodeParserMap.put("httpNode", new HttpNodeParser());
047        nodeParserMap.put("knowledgeNode", new KnowledgeNodeParser());
048        nodeParserMap.put("loopNode", new LoopNodeParser());
049        nodeParserMap.put("searchEngineNode", new SearchEngineNodeParser());
050        nodeParserMap.put("templateNode", new TemplateNodeParser());
051
052        nodeParserMap.put("endNode", new EndNodeParser());
053        nodeParserMap.put("llmNode", new LlmNodeParser());
054    }
055
056    public Map<String, NodeParser> getNodeParserMap() {
057        return nodeParserMap;
058    }
059
060    public void setNodeParserMap(Map<String, NodeParser> nodeParserMap) {
061        this.nodeParserMap = nodeParserMap;
062    }
063
064    public void addNodeParser(String type, NodeParser nodeParser) {
065        this.nodeParserMap.put(type, nodeParser);
066    }
067
068    public Chain parse(Tinyflow tinyflow) {
069        String jsonString = tinyflow.getData();
070        if (StringUtil.noText(jsonString)) {
071            return null;
072        }
073
074        JSONObject root = JSON.parseObject(jsonString);
075        JSONArray nodes = root.getJSONArray("nodes");
076        JSONArray edges = root.getJSONArray("edges");
077
078        return parse(tinyflow, nodes, edges, null);
079    }
080
081    public Chain parse(Tinyflow tinyflow, JSONArray nodes, JSONArray edges, JSONObject parentNode) {
082        if (CollectionUtil.noItems(nodes) || CollectionUtil.noItems(edges)) {
083            return null;
084        }
085
086        Chain chain = new Chain();
087        for (int i = 0; i < nodes.size(); i++) {
088            JSONObject nodeObject = nodes.getJSONObject(i);
089            if ((parentNode == null && StringUtil.noText(nodeObject.getString("parentId")))
090                    || (parentNode != null && parentNode.getString("id").equals(nodeObject.getString("parentId")))) {
091                ChainNode node = parseNode(tinyflow, nodeObject);
092                if (node != null) {
093
094
095                    node.setId(nodeObject.getString("id"));
096                    node.setName(nodeObject.getString("label"));
097                    node.setDescription(nodeObject.getString("description"));
098
099                    JSONObject dataJsonObject = nodeObject.getJSONObject("data");
100                    if (dataJsonObject != null && !dataJsonObject.isEmpty()) {
101                        String conditionString = dataJsonObject.getString("condition");
102
103                        if (StringUtil.hasText(conditionString)) {
104                            node.setCondition(new JavascriptStringCondition(conditionString.trim()));
105                        }
106
107                        Boolean async = dataJsonObject.getBoolean("async");
108                        if (async != null) {
109                            node.setAsync(async);
110                        }
111
112                        String name = dataJsonObject.getString("title");
113                        if (StringUtil.hasText(name)) {
114                            node.setName(name);
115                        }
116
117                        String description = dataJsonObject.getString("description");
118                        if (StringUtil.hasText(description)) {
119                            node.setDescription(description);
120                        }
121                    }
122
123                    chain.addNode(node);
124                }
125            }
126        }
127
128        for (int i = 0; i < edges.size(); i++) {
129            JSONObject edgeObject = edges.getJSONObject(i);
130            JSONObject edgeData = edgeObject.getJSONObject("data");
131
132            if ((parentNode == null && (edgeData == null || StringUtil.noText(edgeData.getString("parentNodeId"))))
133                    || (parentNode != null && edgeData != null && edgeData.getString("parentNodeId").equals(parentNode.getString("id"))
134                    //不添加子流程里的第一条 edge(也就是父节点连接子节点的第一条线)
135                    && !parentNode.getString("id").equals(edgeObject.getString("source")))) {
136                ChainEdge edge = parseEdge(edgeObject);
137                if (edge != null) {
138                    chain.addEdge(edge);
139                }
140            }
141        }
142
143        return chain;
144    }
145
146    private ChainNode parseNode(Tinyflow tinyflow, JSONObject nodeObject) {
147        String type = nodeObject.getString("type");
148        if (StringUtil.noText(type)) {
149            return null;
150        }
151
152        NodeParser nodeParser = nodeParserMap.get(type);
153        return nodeParser == null ? null : nodeParser.parse(nodeObject, tinyflow);
154    }
155
156
157    private ChainEdge parseEdge(JSONObject edgeObject) {
158        if (edgeObject == null) return null;
159        ChainEdge edge = new ChainEdge();
160        edge.setId(edgeObject.getString("id"));
161        edge.setSource(edgeObject.getString("source"));
162        edge.setTarget(edgeObject.getString("target"));
163
164        JSONObject data = edgeObject.getJSONObject("data");
165        if (data == null || data.isEmpty()) {
166            return edge;
167        }
168
169        String conditionString = data.getString("condition");
170        if (StringUtil.hasText(conditionString)) {
171            edge.setCondition(new JavascriptStringCondition(conditionString.trim()));
172        }
173        return edge;
174    }
175}