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.chain.node.GroovyExecNode;
019import com.agentsflex.chain.node.QLExpressExecNode;
020import com.agentsflex.core.chain.Chain;
021import com.agentsflex.core.chain.ChainNode;
022import com.agentsflex.core.chain.node.CodeNode;
023import com.agentsflex.core.chain.node.LlmNode;
024import com.agentsflex.core.llm.ChatOptions;
025import com.agentsflex.core.llm.Llm;
026import com.agentsflex.core.util.StringUtil;
027import com.alibaba.fastjson.JSONObject;
028import dev.tinyflow.core.Tinyflow;
029import dev.tinyflow.core.parser.BaseNodeParser;
030import dev.tinyflow.core.provder.LlmProvider;
031
032import java.util.Collections;
033import java.util.Map;
034
035public class CodeNodeParser extends BaseNodeParser {
036
037    @Override
038    public ChainNode parse(JSONObject nodeJSONObject, Tinyflow tinyflow) {
039        CodeNode codeNode;
040        JSONObject data = getData(nodeJSONObject);
041        String engine = data.getString("engine");
042        if ("groovy".equalsIgnoreCase(engine)) {
043            codeNode = new GroovyExecNode();
044        } else {
045            codeNode = new QLExpressExecNode();
046        }
047
048        codeNode.setName(data.getString("label"));
049        codeNode.setCode(data.getString("code"));
050
051        addParameters(codeNode, data);
052        addOutputKeys(codeNode, data);
053
054        return codeNode;
055    }
056}