配套核心仓「webui 通用反向代理」。SDK 1.4.0 尚未发布,接口未冻结,
本次按开发期自由变更处理(正式发版时并入版本号推进)。
## 声明契约
plugin.json 的 proxies 字段(声明式,静态可发现)或 RegisterProxy
(运行期,供没有 plugin.json 的内置插件用):
{"name":"ui","host":"myapp","path":"/p/myapp","strip_path":true,
"target":"127.0.0.1:12100","auth":"homeagent"}
命名与既有能力对齐(ToolDef / ChannelDef / ConfigDef / RegisterTool /
ToolRegistrar)——第一版写成 ProxyDecl / DeclareProxy / ProxyDeclarer
被评审指出「跟 SDK 其他接口不是一个风格」,已全面改名。
## strip_path:Path 的两种语义
Path 不能一刀切成「原样保留」,真实需求有两种且**不能自动判定**
(同一个 path 在两种语义下都说得通,猜错即全部 404 且像上游故障):
strip_path 缺省/false(别名模式)—— path 是上游真实路径的一部分
/api/v1/device/ws + path=/api/v1/device → 上游收到原样
适用:客户端**已硬编码**路径的机器接口(设备网关即如此)
strip_path=true(前缀模式)—— path 只是门户上的挂载点
/p/myapp/api/status + path=/p/myapp → 上游收到 /api/status
适用:自带 UI 的服务(前端用相对路径)
非法组合(strip_path 而无 path)被 ValidateProxyDef 挡住。
## 单一入口原则(契约级要求)
一个声明 = 一个入口。两种挂载形态对「根路径」处理截然不同:
Host 形态下根路径是插件的根(fetch('/api/x') 天然正确);
Path 形态下根路径**属于门户**,同样代码会打到门户自己身上
(静默错路由:页面能开、功能全坏)。
故被反代的插件必须**一律使用相对路径**,绝不硬编码以 / 开头的绝对路径。
这样同一份前端在两种形态下都正确,插件不必知道自己被挂在哪,
反代层也能按外部条件(子域是否有证书/放行)自由选择形态。
## 判据
sdk/proxy_test.go:两种语义的映射、非法组合、单一入口原则的契约存在性。
hmapdev proxy_config_test.go:schema 漂移保护(新增字段忘了同步就判红)、
非法声明在**打包时**就被拒(不必装到 HomeAgent 才看到)。
两模块 go test 全绿;文档站已重新生成(ProxyDef 与单一入口原则进入
docs/api/misc.md 与 llms-full.txt)。
## 顺带修回的一处(此前随工作树丢失)
writePluginJSON 漏写 proxies 键 —— 漏写的话插件装得上、启动正常、
就是不出现,没有任何报错。该 bug 曾在核心仓侧出现过(判据抓到过),
这次移植时由 TestWritePluginJSONPreservesProxies 再次判红并修复。
4.9 KiB
配置(Settings)
声明插件自己的配置项,内核会把它渲染到 WebUI 的设置页,并为每个插件维护独立的配置表。
SettingsAPI
| 方法 | 说明 |
|---|---|
DataDir |
DataDir returns the plugin-specific data directory (guaranteed to exist): |
Defs |
Defs returns config definitions matching the prefix. |
Dump |
Dump returns all config values. |
Get |
Get reads the plugin's own config value (config_ table). |
GetCore |
GetCore reads the core config table. |
GetPlugin |
GetPlugin reads another plugin's config table. |
List |
List returns all keys matching the given prefix. |
ListCore |
ListCore lists core config keys matching the prefix. |
ListPlugin |
ListPlugin lists another plugin's config keys matching the prefix. |
Plugins |
Plugins returns a list of all plugin config namespaces. |
RegisterDef |
RegisterDef registers a config definition for UI display. |
Set |
Set writes a config value to the plugin's own config table. |
SetCore |
SetCore writes to the core config table. |
SetPlugin |
SetPlugin writes to another plugin's config table. |
SettingsAPI.DataDir
DataDir() string
DataDir returns the plugin-specific data directory (guaranteed to exist): /plugin_data/<plugin_name>. Plugins should persist any runtime files (generated images, caches, downloads) here.
settings.go:25
SettingsAPI.Defs
Defs(prefix string) []*ConfigDef
Defs returns config definitions matching the prefix.
settings.go:40
SettingsAPI.Dump
Dump() map[string]interface{}
Dump returns all config values.
settings.go:43
SettingsAPI.Get
Get(key string) (interface{}, error)
Get reads the plugin's own config value (config_ table).
settings.go:5
SettingsAPI.GetCore
GetCore(key string) (interface{}, error)
GetCore reads the core config table.
settings.go:14
SettingsAPI.GetPlugin
GetPlugin(plugin, key string) (interface{}, error)
GetPlugin reads another plugin's config table.
settings.go:28
SettingsAPI.List
List(prefix string) ([]string, error)
List returns all keys matching the given prefix.
settings.go:11
SettingsAPI.ListCore
ListCore(prefix string) ([]string, error)
ListCore lists core config keys matching the prefix.
settings.go:20
SettingsAPI.ListPlugin
ListPlugin(plugin, prefix string) ([]string, error)
ListPlugin lists another plugin's config keys matching the prefix.
settings.go:34
SettingsAPI.Plugins
Plugins() []string
Plugins returns a list of all plugin config namespaces.
settings.go:46
SettingsAPI.RegisterDef
RegisterDef(def ConfigDef)
RegisterDef registers a config definition for UI display.
settings.go:37
SettingsAPI.Set
Set(key string, value interface{}) error
Set writes a config value to the plugin's own config table.
settings.go:8
SettingsAPI.SetCore
SetCore(key string, value interface{}) error
SetCore writes to the core config table.
settings.go:17
SettingsAPI.SetPlugin
SetPlugin(plugin, key string, value interface{}) error
SetPlugin writes to another plugin's config table.
settings.go:31
ConfigDef
type ConfigDef struct { Key string `json:"key"` Default interface{} `json:"default,omitempty"` Type string `json:"type"` DisplayName string …
ConfigDef describes a configuration field for the WebUI.
settings.go:50
PluginSDK.Settings
func (s *PluginSDK) Settings() SettingsAPI
Settings returns the settings API for reading/writing plugin configuration. sett 在 New 时一次性写入且无 setter,故不需要加锁。
示例插件里的真实用法
| 插件 | 位置 | 代码 |
|---|---|---|
a2a |
example/a2a/plugin.go:68 |
s.Settings().RegisterDef(sdk.ConfigDef{ |
acp |
example/acp/plugin.go:63 |
s.Settings().RegisterDef(sdk.ConfigDef{ |
ai_image |
example/ai_image/plugin.go:114 |
s.Settings().RegisterDef(sdk.ConfigDef{ |
bili |
example/bili/plugin.go:29 |
s.Settings().RegisterDef(sdk.ConfigDef{ |
plugin.go:406