mirror of
https://gitcode.com/JianFeeeee/homeagent-sdk.git
synced 2026-09-20 17:08:01 +00:00
- sdk: RegisterOnRemoveHandler/RunOnRemoveHandlers(仅卸载时触发,重载不触发; 后注册先执行、幂等),与 RegisterStopHandler/RunStopHandlers 并存 - plugindev: 模板 main.go.tmpl 新增 onRemove 演示(删配置键),templates.go 同步 - example/calendar: RegisterOnRemoveHandler(p.cleanupData) 卸载时清理 events.json - README/README_EN: 生命周期文档补充 onRemove - example/calendar/plg.json: 版本对齐
62 lines
1.4 KiB
Cheetah
62 lines
1.4 KiB
Cheetah
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"gitcode.com/JianFeeeee/homeagent-sdk/sdk"
|
|
)
|
|
|
|
type Plugin struct {
|
|
name string
|
|
sdk *sdk.PluginSDK
|
|
}
|
|
|
|
func NewPlugin(name string, config map[string]interface{}) (sdk.Plugin, error) {
|
|
return &Plugin{name: name}, nil
|
|
}
|
|
|
|
func (p *Plugin) Name() string { return p.name }
|
|
|
|
func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
|
p.sdk = s
|
|
|
|
s.RegisterStopHandler(func() {
|
|
fmt.Printf("[%s] stop handler running\n", p.name)
|
|
})
|
|
s.RegisterOnRemoveHandler(func() {
|
|
fmt.Printf("[%s] onRemove handler running\n", p.name)
|
|
})
|
|
s.Settings().RegisterDef(sdk.ConfigDef{
|
|
Key: "plugin.{{.Plg.Name}}.example",
|
|
Default: "hello",
|
|
Type: "string",
|
|
DisplayName: "示例配置",
|
|
Description: "An example configuration key",
|
|
Category: "{{.Plg.Name}}",
|
|
})
|
|
|
|
tp := p.name + "_"
|
|
s.RegisterTool(tp+"hello", sdk.ToolDef{
|
|
Name: tp + "hello",
|
|
Description: "A hello world tool",
|
|
Parameters: map[string]interface{}{
|
|
"type": "object",
|
|
"properties": map[string]interface{}{},
|
|
},
|
|
}, p.handleHello)
|
|
|
|
fmt.Printf("[%s] started\n", p.name)
|
|
return nil
|
|
}
|
|
|
|
func (p *Plugin) Stop() error {
|
|
fmt.Printf("[%s] stopped\n", p.name)
|
|
return nil
|
|
}
|
|
|
|
func (p *Plugin) handleHello(args map[string]interface{}) (interface{}, error) {
|
|
return map[string]interface{}{
|
|
"content": "Hello from {{.Plg.Name}} plugin!",
|
|
}, nil
|
|
}
|