Files
HomeAgent/internal/plugins/cfgmgr/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

381 lines
12 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 cfgmgr
import (
"encoding/json"
"log"
"sort"
"github.com/JianFeeeee/HomeAgent/internal/plugin"
sdk "github.com/JianFeeeee/HomeAgent/internal/sdk"
)
func init() {
plugin.RegisterPluginMeta("cfgmgr", "配置管理", "Config Manager")
plugin.RegisterFactory("cfgmgr", func(name string, config map[string]interface{}) (sdk.Plugin, error) {
return New(name), nil
})
}
type Plugin struct {
name string
}
func New(name string) *Plugin {
return &Plugin{name: name}
}
func (p *Plugin) Name() string { return p.name }
func (p *Plugin) Start(s *sdk.PluginSDK) error {
s.SetAutoRestart(true)
s.RegisterTool("config_list_plugins", sdk.ToolDef{
Name: "config_list_plugins",
Description: "列出所有插件的配置命名空间,返回每个插件的名称和配置项数量",
Parameters: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{},
},
// 已核实只读:只列举插件名
ParallelSafe: true,
}, p.handleListPlugins(s))
s.RegisterTool("config_get", sdk.ToolDef{
Name: "config_get",
Description: "获取指定插件或核心的配置项值。scope=core 读取核心配置,scope=插件名 读取该插件的配置",
Parameters: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"scope": map[string]interface{}{"type": "string", "description": "配置作用域:core 表示核心配置,插件名表示该插件的配置"},
"key": map[string]interface{}{"type": "string", "description": "配置项键名"},
},
"required": []string{"scope", "key"},
},
// 已核实只读:底层 ConfigRegistry 有 RWMutex,且本工具不改任何状态
ParallelSafe: true,
}, p.handleGet(s))
s.RegisterTool("config_set", sdk.ToolDef{
Name: "config_set",
Description: "设置指定插件或核心的配置项值。scope=core 写入核心配置,scope=插件名 写入该插件的配置。修改后立即生效,无需重启",
Parameters: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"scope": map[string]interface{}{"type": "string", "description": "配置作用域:core 表示核心配置,插件名表示该插件的配置"},
"key": map[string]interface{}{"type": "string", "description": "配置项键名"},
"value": map[string]interface{}{"type": "string", "description": "配置项值(字符串形式,布尔用 true/false,数字用字面量)"},
},
"required": []string{"scope", "key", "value"},
},
// 写配置:即使底层有锁也按声明序执行
Serial: true,
}, p.handleSet(s))
s.RegisterTool("config_list_keys", sdk.ToolDef{
Name: "config_list_keys",
Description: "列出指定插件或核心的所有配置项键名和当前值。scope=core 列出核心配置,scope=插件名 列出该插件配置",
Parameters: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"scope": map[string]interface{}{"type": "string", "description": "配置作用域:core 或插件名"},
"prefix": map[string]interface{}{"type": "string", "description": "键名前缀过滤(可选,默认空表示全部)"},
},
"required": []string{"scope"},
},
// 已核实只读:只列举键值
ParallelSafe: true,
}, p.handleListKeys(s))
s.RegisterTool("config_get_defs", sdk.ToolDef{
Name: "config_get_defs",
Description: "获取指定插件或核心的配置项定义(类型、描述、默认值、选项等),用于了解每个配置项的含义和合法值",
Parameters: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"scope": map[string]interface{}{"type": "string", "description": "配置作用域:core 或插件名"},
"prefix": map[string]interface{}{"type": "string", "description": "键名前缀过滤(可选)"},
},
"required": []string{"scope"},
},
// 已核实只读:返回配置项定义,不改状态
ParallelSafe: true,
}, p.handleGetDefs(s))
s.RegisterTool("config_dump", sdk.ToolDef{
Name: "config_dump",
Description: "导出所有配置(核心+所有插件)的完整快照,用于全局审查或备份",
Parameters: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{},
},
// 已核实只读:导出当前配置
ParallelSafe: true,
}, p.handleDump(s))
s.RegisterTool("config_batch_set", sdk.ToolDef{
Name: "config_batch_set",
Description: "批量设置配置项,一次修改多个键值对。items 为 JSON 数组,每项包含 scope/key/value",
Parameters: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"items": map[string]interface{}{
"type": "array",
"description": "配置项数组,每项: {\"scope\":\"core|插件名\", \"key\":\"键名\", \"value\":\"值\"}",
"items": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"scope": map[string]interface{}{"type": "string", "description": "配置作用域"},
"key": map[string]interface{}{"type": "string", "description": "配置项键名"},
"value": map[string]interface{}{"type": "string", "description": "配置项值"},
},
"required": []string{"scope", "key", "value"},
},
},
},
"required": []string{"items"},
},
// 批量写配置:同上
Serial: true,
}, p.handleBatchSet(s))
log.Printf("[cfgmgr] started")
return nil
}
func (p *Plugin) Stop() error {
return nil
}
func (p *Plugin) handleListPlugins(s *sdk.PluginSDK) sdk.ToolHandler {
return func(args map[string]interface{}) (interface{}, error) {
sett := s.Settings()
if sett == nil {
return map[string]interface{}{"error": "settings API not available"}, nil
}
plugins := sett.Plugins()
result := make([]map[string]interface{}, 0, len(plugins))
for _, name := range plugins {
entry := map[string]interface{}{"name": name}
if name != "core" {
keys, err := sett.ListPlugin(name, "")
if err == nil {
entry["key_count"] = len(keys)
}
} else {
keys, err := sett.ListCore("")
if err == nil {
entry["key_count"] = len(keys)
}
}
result = append(result, entry)
}
return map[string]interface{}{"plugins": result}, nil
}
}
func (p *Plugin) handleGet(s *sdk.PluginSDK) sdk.ToolHandler {
return func(args map[string]interface{}) (interface{}, error) {
sett := s.Settings()
if sett == nil {
return map[string]interface{}{"error": "settings API not available"}, nil
}
scope, _ := args["scope"].(string)
key, _ := args["key"].(string)
if scope == "" || key == "" {
return map[string]interface{}{"error": "scope and key are required"}, nil
}
var val interface{}
var err error
if scope == "core" {
val, err = sett.GetCore(key)
} else {
val, err = sett.GetPlugin(scope, key)
}
if err != nil {
return map[string]interface{}{"error": err.Error(), "scope": scope, "key": key}, nil
}
return map[string]interface{}{"scope": scope, "key": key, "value": val}, nil
}
}
func (p *Plugin) handleSet(s *sdk.PluginSDK) sdk.ToolHandler {
return func(args map[string]interface{}) (interface{}, error) {
sett := s.Settings()
if sett == nil {
return map[string]interface{}{"error": "settings API not available"}, nil
}
scope, _ := args["scope"].(string)
key, _ := args["key"].(string)
value, _ := args["value"].(string)
if scope == "" || key == "" {
return map[string]interface{}{"error": "scope and key are required"}, nil
}
var err error
if scope == "core" {
err = sett.SetCore(key, value)
} else {
err = sett.SetPlugin(scope, key, value)
}
if err != nil {
return map[string]interface{}{"error": err.Error()}, nil
}
log.Printf("[cfgmgr] set %s.%s = %s", scope, key, value)
return map[string]interface{}{"status": "ok", "scope": scope, "key": key, "value": value}, nil
}
}
func (p *Plugin) handleListKeys(s *sdk.PluginSDK) sdk.ToolHandler {
return func(args map[string]interface{}) (interface{}, error) {
sett := s.Settings()
if sett == nil {
return map[string]interface{}{"error": "settings API not available"}, nil
}
scope, _ := args["scope"].(string)
prefix, _ := args["prefix"].(string)
if scope == "" {
return map[string]interface{}{"error": "scope is required"}, nil
}
var keys []string
var err error
if scope == "core" {
keys, err = sett.ListCore(prefix)
} else {
keys, err = sett.ListPlugin(scope, prefix)
}
if err != nil {
return map[string]interface{}{"error": err.Error()}, nil
}
sort.Strings(keys)
items := make([]map[string]interface{}, 0, len(keys))
for _, k := range keys {
var val interface{}
if scope == "core" {
val, _ = sett.GetCore(k)
} else {
val, _ = sett.GetPlugin(scope, k)
}
items = append(items, map[string]interface{}{"key": k, "value": val})
}
return map[string]interface{}{"scope": scope, "count": len(items), "items": items}, nil
}
}
func (p *Plugin) handleGetDefs(s *sdk.PluginSDK) sdk.ToolHandler {
return func(args map[string]interface{}) (interface{}, error) {
sett := s.Settings()
if sett == nil {
return map[string]interface{}{"error": "settings API not available"}, nil
}
scope, _ := args["scope"].(string)
prefix, _ := args["prefix"].(string)
if scope == "" {
return map[string]interface{}{"error": "scope is required"}, nil
}
defPrefix := buildDefPrefix(scope, prefix)
defs := sett.Defs(defPrefix)
result := make([]map[string]interface{}, 0, len(defs))
for _, d := range defs {
if d == nil {
continue
}
entry := map[string]interface{}{
"key": d.Key,
"type": d.Type,
"display_name": d.DisplayName,
"description": d.Description,
}
if d.Default != nil {
entry["default"] = d.Default
}
if len(d.Options) > 0 {
entry["options"] = d.Options
}
if d.Category != "" {
entry["category"] = d.Category
}
if d.Secret {
entry["secret"] = true
}
result = append(result, entry)
}
return map[string]interface{}{"scope": scope, "count": len(result), "defs": result}, nil
}
}
func (p *Plugin) handleDump(s *sdk.PluginSDK) sdk.ToolHandler {
return func(args map[string]interface{}) (interface{}, error) {
sett := s.Settings()
if sett == nil {
return map[string]interface{}{"error": "settings API not available"}, nil
}
dump := sett.Dump()
return map[string]interface{}{"config": dump}, nil
}
}
func (p *Plugin) handleBatchSet(s *sdk.PluginSDK) sdk.ToolHandler {
return func(args map[string]interface{}) (interface{}, error) {
sett := s.Settings()
if sett == nil {
return map[string]interface{}{"error": "settings API not available"}, nil
}
rawItems, ok := args["items"].([]interface{})
if !ok || len(rawItems) == 0 {
return map[string]interface{}{"error": "items must be a non-empty array"}, nil
}
var results []map[string]interface{}
applied := 0
for i, raw := range rawItems {
item, ok := raw.(map[string]interface{})
if !ok {
results = append(results, map[string]interface{}{"index": i, "error": "invalid item format"})
continue
}
scope, _ := item["scope"].(string)
key, _ := item["key"].(string)
value, _ := item["value"].(string)
if scope == "" || key == "" {
results = append(results, map[string]interface{}{"index": i, "error": "scope and key required"})
continue
}
var err error
if scope == "core" {
err = sett.SetCore(key, value)
} else {
err = sett.SetPlugin(scope, key, value)
}
if err != nil {
results = append(results, map[string]interface{}{"index": i, "scope": scope, "key": key, "error": err.Error()})
continue
}
applied++
results = append(results, map[string]interface{}{"index": i, "scope": scope, "key": key, "value": value, "status": "ok"})
}
log.Printf("[cfgmgr] batch set: %d/%d applied", applied, len(rawItems))
return map[string]interface{}{"applied": applied, "total": len(rawItems), "results": results}, nil
}
}
func buildDefPrefix(scope, prefix string) string {
if scope == "core" {
if prefix == "" {
return "core."
}
return "core." + prefix
}
p := "plugin." + scope + "."
if prefix != "" {
p += prefix
}
return p
}
var _ = json.Marshal