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
018
019import com.agentsflex.core.chain.DataType;
020import com.agentsflex.core.chain.Parameter;
021import com.agentsflex.core.chain.RefType;
022import com.agentsflex.core.chain.node.BaseNode;
023import com.alibaba.fastjson.JSONArray;
024import com.alibaba.fastjson.JSONObject;
025
026import java.util.ArrayList;
027import java.util.Collections;
028import java.util.List;
029
030
031public abstract class BaseNodeParser implements NodeParser {
032
033    private static final JSONObject EMPTY_JSON_OBJECT = new JSONObject(Collections.emptyMap());
034
035    public JSONObject getData(JSONObject nodeObject) {
036        JSONObject jsonObject = nodeObject.getJSONObject("data");
037        return jsonObject != null ? jsonObject : EMPTY_JSON_OBJECT;
038    }
039
040    public void addParameters(BaseNode node, JSONObject data) {
041        List<Parameter> inputParameters = getParameters(data, "parameters");
042        node.setParameters(inputParameters);
043    }
044
045    public List<Parameter> getParameters(JSONObject data, String key) {
046        JSONArray parametersJsonArray = data.getJSONArray(key);
047        if (parametersJsonArray == null || parametersJsonArray.isEmpty()) {
048            return Collections.emptyList();
049        }
050        List<Parameter> parameters = new ArrayList<>(parametersJsonArray.size());
051        for (int i = 0; i < parametersJsonArray.size(); i++) {
052            JSONObject inputParam = parametersJsonArray.getJSONObject(i);
053            Parameter parameter = new Parameter();
054            parameter.setName(inputParam.getString("name"));
055            parameter.setDescription(inputParam.getString("description"));
056            parameter.setRef(inputParam.getString("ref"));
057            parameter.setRefType(RefType.ofValue(inputParam.getString("refType")));
058            parameter.setDataType(DataType.ofValue(inputParam.getString("dataType")));
059            parameter.setRequired(inputParam.getBooleanValue("required"));
060            parameters.add(parameter);
061        }
062
063        return parameters;
064    }
065
066    public void addOutputKeys(BaseNode node, JSONObject data) {
067        JSONArray outputParams = data.getJSONArray("outputDefs");
068        if (outputParams != null) for (int i = 0; i < outputParams.size(); i++) {
069            JSONObject outputParam = outputParams.getJSONObject(i);
070            Parameter outputDef = new Parameter();
071            outputDef.setName(outputParam.getString("name"));
072            outputDef.setDescription(outputParam.getString("description"));
073            outputDef.setRef(outputParam.getString("ref"));
074            outputDef.setRefType(RefType.ofValue(outputParam.getString("refType")));
075            outputDef.setDataType(DataType.ofValue(outputParam.getString("dataType")));
076            node.addOutputDef(outputDef);
077        }
078    }
079}