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.EndNodeParser;
028import dev.tinyflow.core.parser.impl.LlmNodeParser;
029import dev.tinyflow.core.parser.impl.StartNodeParser;
030
031import java.util.HashMap;
032import java.util.Map;
033
034public class ChainParser {
035
036    private static final Map<String, NodeParser> nodeParserMap = new HashMap<>();
037
038    static {
039        nodeParserMap.put("startNode", new StartNodeParser());
040        nodeParserMap.put("endNode", new EndNodeParser());
041        nodeParserMap.put("llmNode", new LlmNodeParser());
042    }
043
044    public static Chain parse(Tinyflow tinyflow) {
045        String jsonString = tinyflow.getData();
046        if (StringUtil.noText(jsonString)) {
047            return null;
048        }
049
050        JSONObject root = JSON.parseObject(jsonString);
051        JSONArray nodes = root.getJSONArray("nodes");
052        JSONArray edges = root.getJSONArray("edges");
053
054        return parse(tinyflow, nodes, edges, null);
055    }
056
057    public static Chain parse(Tinyflow tinyflow, JSONArray nodes, JSONArray edges, JSONObject parentNode) {
058        if (CollectionUtil.noItems(nodes) || CollectionUtil.noItems(edges)) {
059            return null;
060        }
061
062        Chain chain = new Chain();
063        for (int i = 0; i < nodes.size(); i++) {
064            JSONObject nodeObject = nodes.getJSONObject(i);
065            if ((parentNode == null && StringUtil.noText(nodeObject.getString("parentId")))
066                    || (parentNode != null && parentNode.getString("id").equals(nodeObject.getString("parentId")))) {
067                ChainNode node = parseNode(tinyflow, nodeObject);
068                if (node != null) {
069                    node.setId(nodeObject.getString("id"));
070                    chain.addNode(node);
071                }
072            }
073        }
074
075        for (int i = 0; i < edges.size(); i++) {
076            JSONObject edgeObject = edges.getJSONObject(i);
077            JSONObject edgeData = edgeObject.getJSONObject("data");
078
079            if ((parentNode == null && (edgeData == null || StringUtil.noText(edgeData.getString("parentNodeId"))))
080                    || (parentNode != null && edgeData != null && edgeData.getString("parentNodeId").equals(parentNode.getString("id"))
081                    //不添加子流程里的第一条 edge(也就是父节点连接子节点的第一条线)
082                    && !parentNode.getString("id").equals(edgeObject.getString("source")))) {
083                ChainEdge edge = parseEdge(edgeObject);
084                if (edge != null) {
085                    chain.addEdge(edge);
086                }
087            }
088        }
089
090        return chain;
091    }
092
093    private static ChainNode parseNode(Tinyflow tinyflow, JSONObject nodeObject) {
094        String type = nodeObject.getString("type");
095        if (StringUtil.noText(type)) {
096            return null;
097        }
098
099        NodeParser nodeParser = nodeParserMap.get(type);
100        return nodeParser == null ? null : nodeParser.parse(nodeObject, tinyflow);
101    }
102
103
104    private static ChainEdge parseEdge(JSONObject edgeObject) {
105        if (edgeObject == null) return null;
106        ChainEdge edge = new ChainEdge();
107        edge.setId(edgeObject.getString("id"));
108        edge.setSource(edgeObject.getString("source"));
109        edge.setTarget(edgeObject.getString("target"));
110        return edge;
111    }
112}