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.node;
017
018import com.agentsflex.core.chain.Chain;
019import com.agentsflex.core.chain.node.BaseNode;
020import com.agentsflex.core.util.Maps;
021import com.agentsflex.core.util.StringUtil;
022import com.jfinal.template.Engine;
023import com.jfinal.template.Template;
024
025import java.io.ByteArrayOutputStream;
026import java.util.Map;
027
028public class TemplateNode extends BaseNode {
029
030    private static final Engine engine;
031    private String template;
032    private String outputDef;
033
034    static {
035        engine = Engine.create("template", e -> {
036            e.addSharedStaticMethod(StringUtil.class);
037        });
038    }
039
040    public String getTemplate() {
041        return template;
042    }
043
044    public void setTemplate(String template) {
045        this.template = template;
046    }
047
048    public String getOutputDef() {
049        return outputDef;
050    }
051
052    public void setOutputDef(String outputDef) {
053        this.outputDef = outputDef;
054    }
055
056    @Override
057    protected Map<String, Object> execute(Chain chain) {
058        Map<String, Object> parameters = getParameters(chain);
059
060        ByteArrayOutputStream result = new ByteArrayOutputStream();
061
062        Template templateByString = engine.getTemplateByString(template);
063        templateByString.render(parameters, result);
064
065        String outputDef = this.outputDef;
066        if (StringUtil.noText(outputDef)) outputDef = "output";
067
068        return Maps.of(outputDef, result.toString());
069    }
070
071
072    @Override
073    public String toString() {
074        return "TemplateNode{" +
075                "template='" + template + '\'' +
076                ", outputDef='" + outputDef + '\'' +
077                ", description='" + description + '\'' +
078                ", parameters=" + parameters +
079                ", outputDefs=" + outputDefs +
080                ", id='" + id + '\'' +
081                ", name='" + name + '\'' +
082                ", async=" + async +
083                ", inwardEdges=" + inwardEdges +
084                ", outwardEdges=" + outwardEdges +
085                ", condition=" + condition +
086                ", memory=" + memory +
087                ", nodeStatus=" + nodeStatus +
088                '}';
089    }
090}