当前位置: 首页 > news >正文

一个类,一次注册,搞定 2 个工具 + 1 个 Skill + 1 个 Sub-Agent

「Regnexe 实战系列」第 4 篇(共 10 篇),对应仓库ExampleReadme04PluginAnnotationTest。上一篇:03. Sub-Agent:自己的模型,自己的工具。

痛点:能力一多,注册代码比业务代码还长

前三篇我们分别用withToolwithSkillwithSubAgent注册了工具、Skill、Sub-Agent。如果一个业务模块同时需要这三种能力,按现在的写法得分别new三个对象,再分三次with*调用——能力一多,这部分"装配代码"很容易比业务逻辑本身还长。

Regnexe 解决这个问题的思路很简单:@Plugin注解一类工具方法一样,把 Skill 和 Sub-Agent 也变成注解,而且可以嵌套进同一个类,共享同一个pluginId

先复习一下 @Plugin

Java 老用户应该很熟悉这个套路——类上加@Plugin,方法上加@AgentTool

@Plugin(id="weather",name="Weather Plugin",description="天气查询")publicclassWeatherPlugin{@AgentTool("Get today's weather for a city.")publicStringgetWeather(Stringcity){return"Beijing: sunny, 22 C.";}}

这个用法没变。变化在于:现在@AgentSkill@AgentSubAgent可以作为这个类的内部静态类,跟@AgentTool方法长在一起。

实战代码:四种能力,一个类,一次注册

仓库ExampleReadme04PluginAnnotationTest里的WeatherPlugin

@Plugin(id="weather",name="Weather Plugin",description="Weather, air quality, travel advice, and trip cost estimation")publicclassWeatherPlugin{@AgentTool("Get today's weather for a city.")publicStringgetWeather(Stringcity){return"Beijing: sunny, 22 C.";}@AgentTool("Get today's air quality index (AQI) for a city.")publicStringgetAirQuality(Stringcity){return"Beijing: AQI 35, excellent air quality.";}@AgentSkill(id="travel_advisor",description="Gives outdoor-activity advice based on the current weather for a city. "+"TRIGGER: Use when the user asks whether the weather is suitable for an outdoor activity.",systemPrompt=""" You are an outdoor-activity advisor. 1. Call get_weather for the city the user mentions. 2. Based on the result, give a short, direct go/no-go recommendation. """,allowedTools={"weather.get_weather"}// 注意:插件内的完整能力 id)publicstaticclassTravelAdvisorSkill{// 不需要 @AgentTool 方法——Skill 不能拥有私有工具}@AgentSubAgent(id="expense_estimator",description="Estimates the total cost of a business trip. "+"TRIGGER: Use when the user asks for a trip budget or cost estimate.",model="aliyun:qwen-plus",systemPrompt=""" You are a travel expense estimator. 1. Call estimate_trip_cost with the trip length and destination. 2. Report the total and a one-line breakdown. """)publicstaticclassExpenseEstimatorSubAgent{@AgentTool("Estimates total cost for a multi-day business trip.")publicStringestimateTripCost(intdays,Stringcity){return"3-day Chengdu trip estimate: 3600 CNY total.";}}}

注册只需要一行:

RegnexeAgentagent=regnexeAgentBuilder.withDefaultModel(Vendor.ALIYUN,"deepseek-v4-flash").withPlugin(newWeatherPlugin()).withEventListener(newConsoleEventListener()).build();

一次withPlugin(new WeatherPlugin()),市场里就出现了 4 个能力:weather.get_weatherweather.get_air_qualityweather.travel_advisorweather.expense_estimator,全部挂在同一个pluginIdweather)下面。

两个容易踩的坑

坑 1:allowedTools要写完整 id。因为travel_advisorget_weather现在共享同一个 pluginId,工具的真实能力 id 是weather.get_weather,不是裸的get_weather。这里写错,Skill 内部就找不到这个工具,表现上会是"Skill 选中了,但回答里没有真实数据"。

坑 2:@AgentSkill类里千万别写@AgentTool方法。Skill 设计上就不持有私有工具,写了也不会被采集——记住"Skill 只能借"这条铁律(上一篇讲的)。

这个设计巧妙在哪

@AgentSubAgent内部复用的是和@Plugin完全一样的扫描机制——@AgentTool方法照样会被扫描出来,只是扫描结果的去向不同:

  • 长在@Plugin类上的@AgentTool→ 变成独立的 MCP_TOOL 能力,谁都能调
  • 长在@AgentSubAgent类上的@AgentTool→ 变成这个 Sub-Agent 的私有ownTools,外面看不到

同一套扫描逻辑,靠"挂在哪个注解下面"决定能力的可见性。不需要学两套 API——这正是 harness 设计上的一个好处:底层只维护一套扫描/装配机制,上层暴露多种语义。

小结

  • 工具不多、不需要打包管理 → 直接withTool(第 1 篇)
  • 需要按业务模块打包、加标签、做版本管理 →@Plugin+ 嵌套@AgentSkill/@AgentSubAgent,一个类搞定一整套能力
  • @AgentSkill/@AgentSubAgent也可以单独用(不嵌套),各自注册成独立的单能力插件

下一篇会把视角拉高一层:除了写代码注册,插件还能怎么"打包"——纯代码构造、包扫描、文件系统目录三种方式一次讲完。


📌 上一篇:03. Sub-Agent:自己的模型,自己的工具 | 下一篇:05. 插件打包的三种姿势
📌 项目地址:https://github.com/flower-trees/regnexe-agent

http://www.jsqmd.com/news/1112601/

相关文章:

  • 如何3分钟快速上手开源炉石传说脚本:Hearthstone-Script终极指南
  • 批处理策略的数学建模:从静态 Batching 到 Continuous Batching 的吞吐分析
  • 【会员专享数据】1979—2025年中国5km分辨率逐年土壤湿度指数栅格数据
  • 音乐文件NCM怎么改成MP3?网易云歌曲ncm格式转换mp3方法
  • 设计 Token 自动同步:别让颜色停在设计稿里
  • 机器学习数据预处理:标签编码与连续变量处理实战
  • 大数据毕业设计选题指南:技术前沿与实战要点
  • 代价函数:业务价值的数学编码与实战设计指南
  • 用 AI 工具提升刷题效率:实验要有指标,别只看爽感
  • orcale的锁模式
  • 【 Elasticsearch】安装配置 GitHub Copilot CLI 插件
  • 科研AI工具全家桶实战测评:从部署到工作流整合的完整指南
  • 持续集成对于微服务的意义:拆之前要先解决合的问题
  • 为什么AI可以帮助任何有具体专业性且爱思考的人成立OPC
  • AI驱动的Three.js渲染优化:霓虹城市的智能帧率管理
  • 航天电路板为啥不能出一点错?
  • Agent越来越智能,但我发现软件工程仍然很重要
  • Prompt 版本管理:提示词也要像代码一样可回滚
  • AI Agent 编排落地:别让流程像即兴 Solo 一样失控
  • 2025-6-15模拟测验
  • 高压安全防护设计:BMS 过压/过流/过温/绝缘检测原理与硬件保护机制
  • 从 Paper 到产品原型:只取能验证商业假设的部分
  • KNN算法实战:从数据预处理到模型调优全解析
  • WebAssembly AI 插件沙箱:插件能跑,更要能管
  • 智慧营区部队体能训练考核系统:有哪些优点和缺点
  • lanceDB数据胡
  • 浮点数的存储简述
  • PyTorch DDP 梯度同步:慢卡问题通常不是显存不够
  • 每天忙到停不下来,却不知道时间去哪了?用Traggo记录真实投入
  • 跨境电商选灵爪AI开发需看真实案例与预算