全部產品
Search
文件中心

Microservices Engine:AgentScope 對接 Nacos AI Registry 使用 Skill

更新時間:Jul 11, 2026

將 Nacos AI Registry 中的 Skill 包作為 AgentScope 的技能來源。通過 NacosSkillRepository,Agent 可以直接從 Nacos 下載 Skill ZIP 並載入為 AgentSkill 使用。

前提條件

模組資訊

  • Maven Artifact:io.agentscope:agentscope-extensions-nacos-skill

  • AgentScope 版本要求:1.0.12+

  • 主要依賴:io.agentscope:agentscope-core、com.alibaba.nacos:nacos-client

  • JDK 要求:JDK 17+

  • 注意:Nacos 匯出的 SKILL.md 中 YAML frontmatter 可能存在多行縮排,NacosSkillRepository 載入時自動處理為扁平格式,無需手動幹預。

完整樣本

以下是一個端到端的可運行樣本,展示從初始化 Nacos 串連到 Agent 對話的完整鏈路。請將 SERVER_ADDR 替換為公網存取點網域名稱或 VPC 私網網域名稱:

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.ai.AiFactory;
import com.alibaba.nacos.api.ai.AiService;
import io.agentscope.core.ReActAgent;
import io.agentscope.core.model.DashScopeChatModel;
import io.agentscope.core.nacos.skill.NacosSkillRepository;
import io.agentscope.core.skill.AgentSkill;
import io.agentscope.core.skill.SkillBox;
import java.util.Properties;

public class NacosSkillDemo {

    public static void main(String[] args) throws Exception {
        // 1. 建立 AiService
        Properties props = new Properties();
        props.put(PropertyKeyConst.SERVER_ADDR, "airegistry.cn-hangzhou.mse.aliyuncs.com:80");
        props.put(PropertyKeyConst.NAMESPACE, "your-namespace-id");
        props.put(PropertyKeyConst.ACCESS_KEY, "your-access-key");
        props.put(PropertyKeyConst.SECRET_KEY, "your-secret-key");

        AiService aiService = AiFactory.createAiService(props);

        // 2. 建立 NacosSkillRepository,載入 Skill
        try (NacosSkillRepository repo = new NacosSkillRepository(aiService, "public")) {
            AgentSkill skill = repo.getSkill("safe-commit-helper");
            System.out.println("Loaded: " + skill.getName());
            System.out.println("Description: " + skill.getDescription());

            // 3. 綁定到 Agent
            SkillBox skillBox = new SkillBox(repo);

            var model = DashScopeChatModel.builder()
                    .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                    .modelName("qwen-max")
                    .build();

            var agent = ReActAgent.builder()
                    .name("DevAssistant")
                    .systemPrompt("你是一個編程助手。")
                    .model(model)
                    .skillBox(skillBox)
                    .build();

            // 4. 使用 Agent
            var response = agent.call(io.agentscope.core.message.Msg.of("幫我產生一個commit message"));
            System.out.println(response.getText());
        } finally {
            aiService.shutdown();
        }
    }
}

分步詳解

1. 添加依賴

<dependency>
    <groupId>io.agentscope</groupId>
    <artifactId>agentscope-extensions-nacos-skill</artifactId>
</dependency>

推薦通過 agentscope-bom 統一管理版本:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.agentscope</groupId>
            <artifactId>agentscope-bom</artifactId>
            <version>1.0.12</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2. 初始化 AiService

建立 Nacos AiService 執行個體,配置 AI 治理中心訪問地址和憑證。SERVER_ADDR 可填寫公網存取點網域名稱或 VPC 私網網域名稱:

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.ai.AiFactory;
import com.alibaba.nacos.api.ai.AiService;
import java.util.Properties;

Properties props = new Properties();
props.put(PropertyKeyConst.SERVER_ADDR, "airegistry.cn-hangzhou.mse.aliyuncs.com:80");
props.put(PropertyKeyConst.NAMESPACE, "your-namespace-id");
props.put(PropertyKeyConst.ACCESS_KEY, "LTAI5tXXXXXXXXXXXXXX");
props.put(PropertyKeyConst.SECRET_KEY, "XXXXXXXXXXXXXXXXXXXXxx");

AiService aiService = AiFactory.createAiService(props);
一個 AiService 執行個體對應一個命名空間。應用內盡量複用執行個體,退出時調用 aiService.shutdown() 釋放資源。

3. 建立 NacosSkillRepository 並載入 Skill

使用 try-with-resources 方式建立 NacosSkillRepository:

import io.agentscope.core.nacos.skill.NacosSkillRepository;
import io.agentscope.core.skill.AgentSkill;

try (NacosSkillRepository repo = new NacosSkillRepository(aiService, "public")) {
    String name = "safe-commit-helper";
    if (repo.skillExists(name)) {
        AgentSkill skill = repo.getSkill(name);
        System.out.println(skill.getName() + " - " + skill.getDescription());
    } else {
        System.out.println("Skill not found: " + name);
    }
}

如果 namespaceId 為空白或 null,自動使用 "public"。

4. 將 Skill 綁定到 Agent

通過 SkillBox 將 Skill 倉庫關聯到 Agent,Agent 在需要時按需從倉庫載入 Skill:

import io.agentscope.core.skill.SkillBox;
import io.agentscope.core.ReActAgent;

SkillBox skillBox = new SkillBox(repo);

var agent = ReActAgent.builder()
        .name("MyAgent")
        .systemPrompt("你是一個智能助手。")
        .model(model)
        .skillBox(skillBox)
        .build();

版本與標籤配置

NacosSkillRepository 根據 skillVersion 和 skillLabel 決定從 Nacos 下載哪個版本的 Skill ZIP,兩者各自按以下優先順序解析,首個非空值生效:

優先順序

來源

配置 Key

1(最高)

建構函式傳入的 Properties

agentscope.nacos.skill.version / agentscope.nacos.skill.label

2

JVM 系統屬性

同上

3(最低)

環境變數

AGENTSCOPE_NACOS_SKILL_VERSION / AGENTSCOPE_NACOS_SKILL_LABEL

當 Properties、JVM 屬性、環境變數同時設定 version 時,Properties 中的值優先生效;當 version 和 label 同時配置時,version 優先,label 被忽略。

配置方式

通過建構函式 Properties:

按版本載入:

Properties appProps = new Properties();
appProps.setProperty(NacosSkillRepository.SKILL_VERSION_PATH, "v1.0.0");

try (NacosSkillRepository repo =
             new NacosSkillRepository(aiService, "public", appProps)) {
    AgentSkill skill = repo.getSkill("safe-commit-helper");
}

按標籤載入:

Properties appProps = new Properties();
appProps.setProperty(NacosSkillRepository.SKILL_LABEL_PATH, "stable");

try (NacosSkillRepository repo =
             new NacosSkillRepository(aiService, "public", appProps)) {
    AgentSkill skill = repo.getSkill("safe-commit-helper");
}

通過環境變數:

export AGENTSCOPE_NACOS_SKILL_VERSION=v1.0.0
# 或
export AGENTSCOPE_NACOS_SKILL_LABEL=stable

通過 JVM 系統屬性:

java -Dagentscope.nacos.skill.version=v1.0.0 -jar myapp.jar

優先順序樣本:

// JVM: -Dagentscope.nacos.skill.version=jvm-ver
// ENV:  AGENTSCOPE_NACOS_SKILL_VERSION=env-ver

Properties props = new Properties();
props.setProperty(NacosSkillRepository.SKILL_VERSION_PATH, "app-ver");

// 此處 "app-ver" 生效
NacosSkillRepository repo = new NacosSkillRepository(aiService, "public", props);
repo.getSkill("my-skill");  // 實際調用 downloadSkillZipByVersion("my-skill", "app-ver")

下載 API 選擇邏輯

以下為 NacosSkillRepository 內部實現說明,供理解調用邏輯參考。

NacosSkillRepository 根據 version / label 自動選擇下載方式(version 優先):

  1. 若 skillVersion 非空 → AiService.downloadSkillZipByVersion(name, version)

  2. 否則若 skillLabel 非空 → AiService.downloadSkillZipByLabel(name, label)

  3. 否則 → AiService.downloadSkillZip(name)

NacosSkillRepository 能力矩陣

核心類:io.agentscope.core.nacos.skill.NacosSkillRepository,實現了 AgentSkillRepository 介面。

能力

方法

狀態

說明

載入單個 Skill

getSkill(name)

支援

下載 ZIP → 適配 frontmatter → 構建 AgentSkill

判斷 Skill 是否存在

skillExists(name)

支援

依據下載結果 / NOT_FOUND 錯誤碼判定

擷取倉庫元資訊

getRepositoryInfo()

支援

類型固定 nacos,位置為 namespace:<namespaceId>

擷取來源標識

getSource()

支援

格式:nacos:<namespaceId>

唯讀聲明

isWriteable()

恒為 false

setWriteable() 被忽略並列印 warn

列舉全部 Skill

getAllSkillNames() / getAllSkills()

暫未實現

返回空集合,列印 warn

寫操作

save() / delete()

暫未實現

返回 false,列印 warn

環境變數參考

以下為 Nacos 通用環境變數參考,本文程式碼範例使用 PropertyKeyConst 常量方式進行配置,與下表中的環境變數無關。

變數

說明

AGENTSCOPE_NACOS_SKILL_VERSION

指定 Skill 版本(可選)

AGENTSCOPE_NACOS_SKILL_LABEL

指定 Skill 標籤(可選)

更多參考