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.document.Document;
021import com.agentsflex.core.prompt.template.TextPromptTemplate;
022import com.agentsflex.core.util.Maps;
023import com.agentsflex.core.util.StringUtil;
024import dev.tinyflow.core.searchengine.SearchEngine;
025import org.slf4j.Logger;
026
027import java.util.Collections;
028import java.util.List;
029import java.util.Map;
030
031public class SearchEngineNode extends BaseNode {
032
033    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(SearchEngineNode.class);
034
035
036    private String engine;
037    private SearchEngine searchEngine;
038    private String limit;
039    private String keyword;
040
041    public String getEngine() {
042        return engine;
043    }
044
045    public void setEngine(String engine) {
046        this.engine = engine;
047    }
048
049    public SearchEngine getSearchEngine() {
050        return searchEngine;
051    }
052
053    public void setSearchEngine(SearchEngine searchEngine) {
054        this.searchEngine = searchEngine;
055    }
056
057    public String getLimit() {
058        return limit;
059    }
060
061    public void setLimit(String limit) {
062        this.limit = limit;
063    }
064
065    public String getKeyword() {
066        return keyword;
067    }
068
069    public void setKeyword(String keyword) {
070        this.keyword = keyword;
071    }
072
073    @Override
074    protected Map<String, Object> execute(Chain chain) {
075        Map<String, Object> argsMap = chain.getParameterValues(this);
076        String realKeyword = TextPromptTemplate.of(keyword).formatToString(argsMap);
077        String realLimitString = TextPromptTemplate.of(limit).formatToString(argsMap);
078        int realLimit = 10;
079        if (StringUtil.hasText(realLimitString)) {
080            try {
081                realLimit = Integer.parseInt(realLimitString);
082            } catch (Exception e) {
083                logger.error(e.toString(), e);
084            }
085        }
086
087        if (searchEngine == null) {
088            return Collections.emptyMap();
089        }
090
091        List<Document> result = searchEngine.search(realKeyword, realLimit, this, chain);
092        return Maps.of("documents", result);
093
094    }
095
096
097    @Override
098    public String toString() {
099        return "SearchEngineNode{" +
100                "engine='" + engine + '\'' +
101                ", searchEngine=" + searchEngine +
102                ", limit='" + limit + '\'' +
103                ", keyword='" + keyword + '\'' +
104                ", description='" + description + '\'' +
105                ", parameters=" + parameters +
106                ", outputDefs=" + outputDefs +
107                ", id='" + id + '\'' +
108                ", name='" + name + '\'' +
109                ", async=" + async +
110                ", inwardEdges=" + inwardEdges +
111                ", outwardEdges=" + outwardEdges +
112                ", condition=" + condition +
113                ", memory=" + memory +
114                ", nodeStatus=" + nodeStatus +
115                '}';
116    }
117}