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