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.impl;
017
018import com.agentsflex.core.chain.DataType;
019import com.agentsflex.core.chain.RefType;
020import com.agentsflex.core.chain.node.BaseNode;
021import com.agentsflex.core.chain.node.ConfirmNode;
022import com.alibaba.fastjson.JSONArray;
023import com.alibaba.fastjson.JSONObject;
024import dev.tinyflow.core.Tinyflow;
025import dev.tinyflow.core.parser.BaseNodeParser;
026
027import java.util.ArrayList;
028import java.util.List;
029
030public class ConfirmNodeParser extends BaseNodeParser {
031
032    @Override
033    public BaseNode doParse(JSONObject root, JSONObject data, Tinyflow tinyflow) {
034
035        ConfirmNode confirmNode = new ConfirmNode();
036        confirmNode.setMessage(data.getString("message"));
037
038        JSONArray confirmsJSONArray = data.getJSONArray("confirms");
039        if (confirmsJSONArray != null && !confirmsJSONArray.isEmpty()) {
040            List<ConfirmNode.ConfirmParameter> parameters = new ArrayList<>(confirmsJSONArray.size());
041            for (int i = 0; i < confirmsJSONArray.size(); i++) {
042                JSONObject parameterJsonObject = confirmsJSONArray.getJSONObject(i);
043                ConfirmNode.ConfirmParameter parameter = new ConfirmNode.ConfirmParameter();
044
045                // Parameter 基础信息
046                parameter.setId(parameterJsonObject.getString("id"));
047                parameter.setName(parameterJsonObject.getString("name"));
048                parameter.setDescription(parameterJsonObject.getString("description"));
049                parameter.setDataType(DataType.ofValue(parameterJsonObject.getString("dataType")));
050                parameter.setRef(parameterJsonObject.getString("ref"));
051                parameter.setRefType(RefType.ofValue(parameterJsonObject.getString("refType")));
052                parameter.setRequired(parameterJsonObject.getBooleanValue("required"));
053                parameter.setDefaultValue(parameterJsonObject.getString("defaultValue"));
054                parameter.setValue(parameterJsonObject.getString("value"));
055
056                // ConfirmParameter 内容
057                parameter.setSelectionDataType(parameterJsonObject.getString("selectionDataType"));
058                parameter.setSelectionMode(parameterJsonObject.getString("selectionMode"));
059
060                parameters.add(parameter);
061            }
062            confirmNode.setConfirms(parameters);
063        }
064
065
066        return confirmNode;
067    }
068}