Files
HomeAgent/internal/plugins/skillmgr/plugin.go
JianFeeeee 6d38a164eb chore(module): Go module 路径随托管地迁移 gitcode → github
## 背景

仓库早已迁移到 GitHub(`github.com/JianFeeeee/HomeAgent`,CI/CD 都在那边跑),
gitcode 保留为**国内只读镜像**(`ci.yml` 的 sync-gitcode-main 快进推送)。
但 go.mod 的 module 路径一直是 `gitcode.com/JianFeeeee/...` —— 既与权威源
不符,`meta.SourceURL`(AGPL §13 的源码提供入口)也指向镜像而非实际运行的代码。

## 关键区分(这是本次改动安全的前提)

两个 module 是**不同**的,影响面差一个数量级:

| module | 谁依赖 | 改它的后果 |
|---|---|---|
| `.../HomeAgent`(主仓) | **只有主仓自己** | 内部重构,零外部影响 |
| `.../homeagent-sdk`(SDK 仓) | 主仓 49 文件 + 所有外部插件 | 需重编插件 |

★ 实测确认:**插件不 import 主仓 module**(`strings qq/plugin.bin | grep HomeAgent` = 0),
只依赖 SDK 仓。而插件与内核的边界是 **RPC 方法名字符串**
(`plugin.init` / `tool.invoke`…,见 proc/protocol.go),**与 Go module 名无关**。
⇒ module 改名**不改变任何 wire 协议**,现网已装的插件照常运行。

SDK 的 GitHub 仓名是 `homeagentsdk`(无连字符),module 路径必须与之精确匹配,
否则 `go get` 会因路径不符失败,故取 `github.com/JianFeeeee/homeagentsdk`。

## 改动

- `go.mod` / `go.sum`:两个 module 路径 + require/replace
- 277 个 Go 文件 + Makefile + 打包脚本的 import 与 ldflags 路径
- SDK 仓 `meta.CoreModule`、`tools/hmapdev` 的 module 声明与工程模板
- 对外文档/链接的仓库地址(README、PLUGIN_DEV、OVERVIEW 等)

## 明确**保留** gitcode 的地方(镜像机制,不能动)

- `GITCODE_TOKEN`、`GC_URL="…@gitcode.com"`(CI 同步 job)
- `gitcode.com/api/v5/...`(release 附件上传)
- git remote `gitcode`(镜像拉取/推送)
- `docs/zh/ci-cd-runbook.md`(整篇讲镜像运维)

## 验证

- `go build ./...` / `go vet ./...` 绿
- `go test ./... -count=1` → 48 包 ok / 0 FAIL
- hmapdev 工具链编译通过(`third_party/homeagent-sdk/tools/hmapdev`)
- 全仓残留检查:module 形态的 `gitcode.com/JianFeeeee` = 0
- 镜像机制抽验:GC_URL / @gitcode / api/v5 / remote 均完好

## 顺带修正

`hmapdev/templates.go` 的注释此前写「gitcode 的模块不在 proxy.golang.org 上」——
module 已迁 GitHub,该论断不再成立。改为如实说明**为何仍默认 replace 到本机 SDK**:
离线可构建、避免版本漂移、内网可用。理由与托管地无关,故 replace 保留。
2026-10-08 10:40:17 +08:00

269 lines
7.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package skillmgr 实现原生 SKILL 插件管理器。
//
// 职责边界:
// - 本插件是 native skill(SKILL.md / skill.json 指令文档型插件)的唯一归属者,
// 负责其全生命周期:加载、卸载、启停、生成(模板)、导出(.skm 包)、安装。
// - clawhubadapter(OpenClaw 兼容层)扫描 skills 目录时发现纯 SKILL 条目后
// 发布 skill_detected 事件移交本插件注册;sidecar/OC plugin 不归本插件管。
//
// skill 的运行模型:SKILL.md 是给 LLM 的指令文档,agent 通过 skill_info 读取全文后
// 用 cmd_run 等工具按文档执行;skill 不注册可调用 handler 工具。
package skillmgr
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"github.com/JianFeeeee/HomeAgent/internal/events"
"github.com/JianFeeeee/HomeAgent/internal/plugin"
"github.com/JianFeeeee/HomeAgent/internal/sdk"
)
const (
// SkillFileName 标准 skill 主文件名
SkillFileName = "SKILL.md"
// MetaFileName 可选元数据文件(覆盖 SKILL.md frontmatter 字段)
MetaFileName = "skill.json"
// PackExt 导出包扩展名
PackExt = ".skm"
// detectEvent 兼容层移交事件类型
detectEvent = events.EventSkillDetected
)
func init() {
plugin.RegisterPluginMeta("skillmgr", "技能管理器", "Skill Manager")
plugin.RegisterFactory("skillmgr", func(name string, cfg map[string]interface{}) (sdk.Plugin, error) {
dir := ""
if dataDir, ok := cfg["data_dir"].(string); ok && dataDir != "" {
dir = filepath.Join(dataDir, "skills")
}
return New(name, dir), nil
})
}
// SkillEntry 一个已加载的 native skill 及其实时状态。
type SkillEntry struct {
sk *plugin.SKILLPlugin
path string
enabled bool
}
type Plugin struct {
name string
skillsDir string
mu sync.RWMutex
skills map[string]*SkillEntry // name -> entry
sdk *sdk.PluginSDK
unsub func() // skill_detected 订阅注销
}
func New(name, skillsDir string) *Plugin {
return &Plugin{
name: name,
skillsDir: skillsDir,
skills: map[string]*SkillEntry{},
}
}
func (p *Plugin) Name() string { return p.name }
func (p *Plugin) Start(s *sdk.PluginSDK) error {
p.sdk = s
// 默认目录:内核 data_dir 下 skills 目录(与配置 core.daemon.data_dir 对齐)
if p.skillsDir == "" {
if v, _ := s.Settings().GetCore("daemon.data_dir"); v != nil {
if dir, ok := v.(string); ok && dir != "" {
p.skillsDir = filepath.Join(dir, "skills")
}
}
}
if err := os.MkdirAll(p.skillsDir, 0755); err != nil {
return fmt.Errorf("mkdir skills dir: %w", err)
}
// 订阅兼容层移交事件(clawhubadapter 发现纯 SKILL 后发布)
p.unsub = s.Subscribe(detectEvent, func(evt *events.Event) {
path, _ := evt.Payload["path"].(string)
if path == "" {
return
}
if _, err := p.loadOne(path); err != nil {
log.Printf("[skillmgr] detected skill %s: %v", path, err)
return
}
log.Printf("[skillmgr] registered via %s event: %s", detectEvent, filepath.Base(path))
})
// 启动全扫:兜底接管所有已存在的纯 SKILL 目录/单文件
// (时序上 skillmgr(c<s) 先于 clawhubadapter 启动,此处先扫到的条目
// 后续事件重复到达时 loadOne 幂等跳过)
n := p.scanExisting()
p.registerTools()
log.Printf("[skillmgr] started, dir=%s, loaded %d native skills", p.skillsDir, n)
return nil
}
func (p *Plugin) Stop() error {
if p.unsub != nil {
p.unsub()
p.unsub = nil
}
return nil
}
// scanExisting 扫描 skills 目录加载全部纯 SKILL 条目。返回加载数量。
func (p *Plugin) scanExisting() int {
entries, err := os.ReadDir(p.skillsDir)
if err != nil {
return 0
}
count := 0
for _, e := range entries {
if strings.HasPrefix(e.Name(), ".") || e.Name() == "node_modules" {
continue
}
path := filepath.Join(p.skillsDir, e.Name())
if !isNativeSkillPath(path) {
continue
}
if _, err := p.loadOne(path); err != nil {
log.Printf("[skillmgr] scan skip %s: %v", e.Name(), err)
continue
}
count++
}
return count
}
// isNativeSkillPath 判断路径是否为纯 SKILL 类型(非 sidecar 非 OC plugin)。
// sidecar: main.js/main.py;OC plugin: openclaw.plugin.json 或带 openclaw 扩展的 package.json。
func isNativeSkillPath(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
if !info.IsDir() {
return strings.HasSuffix(strings.ToLower(path), ".md")
}
// 目录内含 sidecar / OC plugin 标志文件的归兼容层管
for _, flag := range []string{"main.js", "main.py", "openclaw.plugin.json"} {
if fileExists(filepath.Join(path, flag)) {
return false
}
}
if hasOCExtensions(filepath.Join(path, "package.json")) {
return false
}
// 纯 skill:有 SKILL.md 或 skill.json 即认
return fileExists(filepath.Join(path, SkillFileName)) ||
fileExists(filepath.Join(path, MetaFileName))
}
// hasOCExtensions 检查 package.json 是否带 openclaw 扩展声明(与 clawhubadapter 同语义)。
func hasOCExtensions(pkgPath string) bool {
data, err := os.ReadFile(pkgPath)
if err != nil {
return false
}
var pkg struct {
Openclaw json.RawMessage `json:"openclaw"`
}
return json.Unmarshal(data, &pkg) == nil && len(pkg.Openclaw) > 0
}
func fileExists(path string) bool {
info, err := os.Stat(path)
return err == nil && !info.IsDir()
}
// loadOne 从路径加载单个 skill 并入表(幂等:已存在且未变更则跳过)。
func (p *Plugin) loadOne(path string) (*SkillEntry, error) {
sk, err := plugin.LoadSKILL(path)
if err != nil {
return nil, err
}
name := sk.Name()
if name == "" || name == "." {
return nil, fmt.Errorf("skill at %s has empty name", path)
}
p.mu.Lock()
defer p.mu.Unlock()
if old, ok := p.skills[name]; ok {
// 幂等:同路径同内容不重复加载
if old.path == path {
return old, nil
}
// 重名不同路径:拒绝并提示
p.mu.Unlock()
return nil, fmt.Errorf("skill name conflict: %q already loaded from %s", name, old.path)
}
entry := &SkillEntry{sk: sk, path: path, enabled: true}
p.skills[name] = entry
return entry, nil
}
// removeOne 从表中移除 skill(不动磁盘)。
func (p *Plugin) removeOne(name string) bool {
p.mu.Lock()
defer p.mu.Unlock()
if _, ok := p.skills[name]; !ok {
return false
}
delete(p.skills, name)
return true
}
// snapshot 返回排序后的 skill 名列表。
func (p *Plugin) snapshot() []*SkillEntry {
p.mu.RLock()
defer p.mu.RUnlock()
names := make([]string, 0, len(p.skills))
for n := range p.skills {
names = append(names, n)
}
sort.Strings(names)
out := make([]*SkillEntry, 0, len(names))
for _, n := range names {
out = append(out, p.skills[n])
}
return out
}
func (p *Plugin) get(name string) (*SkillEntry, bool) {
p.mu.RLock()
defer p.mu.RUnlock()
e, ok := p.skills[name]
return e, ok
}
// SkillIndex 实现 agentCore.SkillIndexProvider:返回已启用技能的精炼索引。
// 每次 LLM 调用都会执行,必须轻量(只读锁 + 字符串拼接,无 IO)。
func (p *Plugin) SkillIndex() string {
entries := p.snapshot()
var b strings.Builder
for _, e := range entries {
if !e.enabled {
continue
}
desc := e.sk.Description()
if desc == "" || desc == "---" {
desc = "(无描述)"
}
if len(desc) > 80 {
desc = desc[:80] + "..."
}
fmt.Fprintf(&b, "%s v%s - %s\n", e.sk.Name(), e.sk.Version(), desc)
}
return strings.TrimRight(b.String(), "\n")
}