|
| 1 | +/* |
| 2 | + * Copyright 2024-2026 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.agentscope.core.nacos.skill; |
| 17 | + |
| 18 | +import com.alibaba.nacos.api.ai.AiService; |
| 19 | +import com.alibaba.nacos.api.ai.model.skills.Skill; |
| 20 | +import com.alibaba.nacos.api.exception.NacosException; |
| 21 | +import com.alibaba.nacos.common.utils.StringUtils; |
| 22 | +import io.agentscope.core.skill.AgentSkill; |
| 23 | +import io.agentscope.core.skill.repository.AgentSkillRepository; |
| 24 | +import io.agentscope.core.skill.repository.AgentSkillRepositoryInfo; |
| 25 | +import java.util.List; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +/** |
| 30 | + * Nacos-based implementation of {@link AgentSkillRepository}. |
| 31 | + * |
| 32 | + * <p>Reads skills from Nacos Config via {@link AiService#loadSkill(String)}. This implementation |
| 33 | + * currently supports only read operations: {@link #getSkill(String)}, {@link #skillExists(String)}, |
| 34 | + * {@link #getRepositoryInfo()}, {@link #getSource()}, and {@link #isWriteable()}. List and write |
| 35 | + * operations are not implemented. |
| 36 | + */ |
| 37 | +public class NacosSkillRepository implements AgentSkillRepository { |
| 38 | + |
| 39 | + private static final Logger log = LoggerFactory.getLogger(NacosSkillRepository.class); |
| 40 | + |
| 41 | + private static final String REPO_TYPE = "nacos"; |
| 42 | + private static final String LOCATION_PREFIX = "namespace:"; |
| 43 | + |
| 44 | + private final AiService aiService; |
| 45 | + private final String namespaceId; |
| 46 | + private final String source; |
| 47 | + private final String location; |
| 48 | + private boolean writeable; |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates a Nacos skill repository. |
| 52 | + * |
| 53 | + * @param aiService the Nacos AI service (must not be null) |
| 54 | + * @param namespaceId the Nacos namespace ID (null or blank is treated as default namespace) |
| 55 | + */ |
| 56 | + public NacosSkillRepository(AiService aiService, String namespaceId) { |
| 57 | + if (aiService == null) { |
| 58 | + throw new IllegalArgumentException("AiService cannot be null"); |
| 59 | + } |
| 60 | + this.aiService = aiService; |
| 61 | + this.namespaceId = StringUtils.isBlank(namespaceId) ? "public" : namespaceId.trim(); |
| 62 | + this.source = REPO_TYPE + ":" + this.namespaceId; |
| 63 | + this.location = LOCATION_PREFIX + this.namespaceId; |
| 64 | + this.writeable = false; |
| 65 | + log.info("NacosSkillRepository initialized for namespace: {}", this.namespaceId); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public AgentSkill getSkill(String name) { |
| 70 | + if (name == null || name.isBlank()) { |
| 71 | + throw new IllegalArgumentException("Skill name cannot be null or empty"); |
| 72 | + } |
| 73 | + try { |
| 74 | + Skill nacosSkill = aiService.loadSkill(name.trim()); |
| 75 | + if (nacosSkill == null) { |
| 76 | + throw new IllegalArgumentException("Skill not found: " + name); |
| 77 | + } |
| 78 | + return NacosSkillToAgentSkillConverter.toAgentSkill(nacosSkill, getSource()); |
| 79 | + } catch (NacosException e) { |
| 80 | + if (e.getErrCode() == NacosException.NOT_FOUND) { |
| 81 | + throw new IllegalArgumentException("Skill not found: " + name, e); |
| 82 | + } |
| 83 | + throw new RuntimeException("Failed to load skill from Nacos: " + name, e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean skillExists(String skillName) { |
| 89 | + if (skillName == null || skillName.isBlank()) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + try { |
| 93 | + Skill skill = aiService.loadSkill(skillName.trim()); |
| 94 | + return skill != null; |
| 95 | + } catch (NacosException e) { |
| 96 | + if (e.getErrCode() == NacosException.NOT_FOUND) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + log.warn("Error checking skill existence for {}: {}", skillName, e.getMessage()); |
| 100 | + return false; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public AgentSkillRepositoryInfo getRepositoryInfo() { |
| 106 | + return new AgentSkillRepositoryInfo(REPO_TYPE, location, writeable); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public String getSource() { |
| 111 | + return source; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void setWriteable(boolean writeable) { |
| 116 | + this.writeable = writeable; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public boolean isWriteable() { |
| 121 | + return writeable; |
| 122 | + } |
| 123 | + |
| 124 | + // ---------- Unsupported operations (list and write) ---------- |
| 125 | + |
| 126 | + @Override |
| 127 | + public List<String> getAllSkillNames() { |
| 128 | + throw new UnsupportedOperationException("getAllSkillNames is not implemented for NacosSkillRepository"); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public List<AgentSkill> getAllSkills() { |
| 133 | + throw new UnsupportedOperationException("getAllSkills is not implemented for NacosSkillRepository"); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public boolean save(List<AgentSkill> skills, boolean force) { |
| 138 | + throw new UnsupportedOperationException("save is not implemented for NacosSkillRepository"); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public boolean delete(String skillName) { |
| 143 | + throw new UnsupportedOperationException("delete is not implemented for NacosSkillRepository"); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void close() { |
| 148 | + // AiService lifecycle is managed by the caller; nothing to release here |
| 149 | + } |
| 150 | +} |
0 commit comments