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.util.CollectionUtil;
022import com.agentsflex.core.util.StringUtil;
023import com.alibaba.fastjson.JSON;
024import com.alibaba.fastjson.JSONArray;
025import com.alibaba.fastjson.JSONObject;
026import dev.tinyflow.core.Tinyflow;
027import dev.tinyflow.core.parser.impl.*;
028
029import java.util.HashMap;
030import java.util.Map;
031
032public class ChainParser {
033
034    private Map<String, NodeParser> nodeParserMap = new HashMap<>();
035
036    public ChainParser() {
037
038        initDefaultParsers();
039    }
040
041    private void initDefaultParsers() {
042        nodeParserMap.put("startNode", new StartNodeParser());
043        nodeParserMap.put("codeNode", new CodeNodeParser());
044
045        nodeParserMap.put("httpNode", new HttpNodeParser());
046        nodeParserMap.put("knowledgeNode", new HttpNodeParser());
047        nodeParserMap.put("loopNode", new LoopNodeParser());
048        nodeParserMap.put("searchEngineNode", new SearchEngineNodeParser());
049        nodeParserMap.put("templateNode", new TemplateNodeParser());
050
051        nodeParserMap.put("endNode", new EndNodeParser());
052        nodeParserMap.put("llmNode", new LlmNodeParser());
053    }
054
055    public Map<String, NodeParser> getNodeParserMap() {
056        return nodeParserMap;
057    }
058
059    public void setNodeParserMap(Map<String, NodeParser> nodeParserMap) {
060        this.nodeParserMap = nodeParserMap;
061    }
062
063    public void addNodeParser(String type, NodeParser nodeParser) {
064        this.nodeParserMap.put(type, nodeParser);
065    }
066
067    public Chain parse(Tinyflow tinyflow) {
068        String jsonString = tinyflow.getData();
069        if (StringUtil.noText(jsonString)) {
070            return null;
071        }
072
073        JSONObject root = JSON.parseObject(jsonString);
074        JSONArray nodes = root.getJSONArray("nodes");
075        JSONArray edges = root.getJSONArray("edges");
076
077        return parse(tinyflow, nodes, edges, null);
078    }
079
080    public Chain parse(Tinyflow tinyflow, JSONArray nodes, JSONArray edges, JSONObject parentNode) {
081        if (CollectionUtil.noItems(nodes) || CollectionUtil.noItems(edges)) {
082            return null;
083        }
084
085        Chain chain = new Chain();
086        for (int i = 0; i < nodes.size(); i++) {
087            JSONObject nodeObject = nodes.getJSONObject(i);
088            if ((parentNode == null && StringUtil.noText(nodeObject.getString("parentId")))
089                    || (parentNode != null && parentNode.getString("id").equals(nodeObject.getString("parentId")))) {
090                ChainNode node = parseNode(tinyflow, nodeObject);
091                if (node != null) {
092                    node.setId(nodeObject.getString("id"));
093                    chain.addNode(node);
094                }
095            }
096        }
097
098        for (int i = 0; i < edges.size(); i++) {
099            JSONObject edgeObject = edges.getJSONObject(i);
100            JSONObject edgeData = edgeObject.getJSONObject("data");
101
102            if ((parentNode == null && (edgeData == null || StringUtil.noText(edgeData.getString("parentNodeId"))))
103                    || (parentNode != null && edgeData != null && edgeData.getString("parentNodeId").equals(parentNode.getString("id"))
104                    //不添加子流程里的第一条 edge(也就是父节点连接子节点的第一条线)
105                    && !parentNode.getString("id").equals(edgeObject.getString("source")))) {
106                ChainEdge edge = parseEdge(edgeObject);
107                if (edge != null) {
108                    chain.addEdge(edge);
109                }
110            }
111        }
112
113        return chain;
114    }
115
116    private ChainNode parseNode(Tinyflow tinyflow, JSONObject nodeObject) {
117        String type = nodeObject.getString("type");
118        if (StringUtil.noText(type)) {
119            return null;
120        }
121
122        NodeParser nodeParser = nodeParserMap.get(type);
123        return nodeParser == null ? null : nodeParser.parse(nodeObject, tinyflow);
124    }
125
126
127    private ChainEdge parseEdge(JSONObject edgeObject) {
128        if (edgeObject == null) return null;
129        ChainEdge edge = new ChainEdge();
130        edge.setId(edgeObject.getString("id"));
131        edge.setSource(edgeObject.getString("source"));
132        edge.setTarget(edgeObject.getString("target"));
133        return edge;
134    }
135}