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.*; 020import com.agentsflex.core.chain.node.BaseNode; 021import com.agentsflex.core.util.StringUtil; 022import com.alibaba.fastjson.JSONArray; 023import com.alibaba.fastjson.JSONObject; 024import dev.tinyflow.core.Tinyflow; 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 return getParameters(data.getJSONArray(key)); 047 } 048 049 public List<Parameter> getParameters(JSONArray parametersJsonArray) { 050 if (parametersJsonArray == null || parametersJsonArray.isEmpty()) { 051 return Collections.emptyList(); 052 } 053 List<Parameter> parameters = new ArrayList<>(parametersJsonArray.size()); 054 for (int i = 0; i < parametersJsonArray.size(); i++) { 055 JSONObject parameterJsonObject = parametersJsonArray.getJSONObject(i); 056 Parameter parameter = new Parameter(); 057 parameter.setId(parameterJsonObject.getString("id")); 058 parameter.setName(parameterJsonObject.getString("name")); 059 parameter.setDescription(parameterJsonObject.getString("description")); 060 parameter.setDataType(DataType.ofValue(parameterJsonObject.getString("dataType"))); 061 parameter.setRef(parameterJsonObject.getString("ref")); 062 parameter.setValue(parameterJsonObject.getString("value")); 063 parameter.setDefaultValue(parameterJsonObject.getString("defaultValue")); 064 parameter.setRefType(RefType.ofValue(parameterJsonObject.getString("refType"))); 065 parameter.setDataType(DataType.ofValue(parameterJsonObject.getString("dataType"))); 066 parameter.setRequired(parameterJsonObject.getBooleanValue("required")); 067 parameter.setDefaultValue(parameterJsonObject.getString("defaultValue")); 068 069 //新增 070 parameter.setContentType(parameterJsonObject.getString("contentType")); 071 parameter.setEnums(parameterJsonObject.getJSONArray("enums")); 072 parameter.setFormType(parameterJsonObject.getString("formType")); 073 parameter.setFormLabel(parameterJsonObject.getString("formLabel")); 074 parameter.setFormPlaceholder(parameterJsonObject.getString("formPlaceholder")); 075 parameter.setFormAttrs(parameterJsonObject.getString("formAttrs")); 076 parameter.setFormDescription(parameterJsonObject.getString("formDescription")); 077 078 JSONArray children = parameterJsonObject.getJSONArray("children"); 079 if (children != null && !children.isEmpty()) { 080 parameter.addChildren(getParameters(children)); 081 } 082 083 parameters.add(parameter); 084 } 085 086 return parameters; 087 } 088 089 090 public void addOutputDefs(BaseNode node, JSONObject data) { 091 List<Parameter> outputDefs = getParameters(data, "outputDefs"); 092 if (outputDefs == null || outputDefs.isEmpty()) { 093 return; 094 } 095 node.setOutputDefs(outputDefs); 096 } 097 098 099 @Override 100 public ChainNode parse(JSONObject nodeJSONObject, Tinyflow tinyflow) { 101 JSONObject data = getData(nodeJSONObject); 102 BaseNode node = doParse(nodeJSONObject, data, tinyflow); 103 if (node != null) { 104 105 node.setId(nodeJSONObject.getString("id")); 106 node.setName(nodeJSONObject.getString("label")); 107 node.setDescription(nodeJSONObject.getString("description")); 108 109 if (!data.isEmpty()) { 110 111 addParameters(node, data); 112 addOutputDefs(node, data); 113 114 String conditionString = data.getString("condition"); 115 116 if (StringUtil.hasText(conditionString)) { 117 node.setCondition(new JsCodeCondition(conditionString.trim())); 118 } 119 120 Boolean async = data.getBoolean("async"); 121 if (async != null) { 122 node.setAsync(async); 123 } 124 125 String name = data.getString("title"); 126 if (StringUtil.hasText(name)) { 127 node.setName(name); 128 } 129 130 String description = data.getString("description"); 131 if (StringUtil.hasText(description)) { 132 node.setDescription(description); 133 } 134 135 Boolean loopEnable = data.getBoolean("loopEnable"); 136 if (loopEnable != null) { 137 node.setLoopEnable(loopEnable); 138 } 139 140 Long loopIntervalMs = data.getLong("loopIntervalMs"); 141 if (loopIntervalMs != null) { 142 node.setLoopIntervalMs(loopIntervalMs); 143 } 144 145 Integer maxLoopCount = data.getInteger("maxLoopCount"); 146 if (maxLoopCount != null) { 147 node.setMaxLoopCount(maxLoopCount); 148 } 149 150 String loopBreakCondition = data.getString("loopBreakCondition"); 151 if (StringUtil.hasText(loopBreakCondition)) { 152 node.setLoopBreakCondition(new JsCodeCondition(loopBreakCondition.trim())); 153 } 154 } 155 } 156 157 return node; 158 } 159 160 protected abstract BaseNode doParse(JSONObject root, JSONObject data, Tinyflow tinyflow); 161}