From aee63a4f9817448051ae73255008271530137eff Mon Sep 17 00:00:00 2001 From: root Date: Sun, 2 Aug 2026 13:37:59 +0800 Subject: [PATCH] =?UTF-8?q?example:=20=E6=BC=94=E7=A4=BA=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=20onRemove=20=E6=B8=85=E7=90=86=E8=A1=A5=E9=BD=90=EF=BC=88memo?= =?UTF-8?q?/rss/weather=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - memo: 卸载时删除 memos.json 数据文件 - rss: 卸载时清理 .homeagent/rss 订阅数据目录 - weather: 卸载时清理 .homeagent/weather 缓存目录 - 与 calendar(events.json)一致:仅删除触发、重载不触发; files(filesDir 为用户配置的访问根目录)、bili/qq(用户下载资产)、 ocr(临时目录函数内自清理)按语义不加入删除回调 --- example/memo/plugin.go | 10 ++++++++++ example/rss/plugin.go | 10 ++++++++++ example/weather/plugin.go | 14 +++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/example/memo/plugin.go b/example/memo/plugin.go index 4dcef98..6f325af 100644 --- a/example/memo/plugin.go +++ b/example/memo/plugin.go @@ -46,6 +46,9 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error { p.filePath = filepath.Join(fmt.Sprint(dataDirVal), "memos.json") p.load() + // 卸载(删除)时清理备忘数据文件;重载不触发 + s.RegisterOnRemoveHandler(p.cleanupData) + s.RegisterTool(p.tp+"create", sdk.ToolDef{ Name: p.tp + "create", Description: "创建一条备忘条目。备忘内容应包含具体事项的完整描述。", @@ -272,3 +275,10 @@ func errorResult(msg string) map[string]interface{} { func NewPluginFactory(name string, config map[string]interface{}) (sdk.Plugin, error) { return &Plugin{name: name}, nil } + +// cleanupData 卸载时清理备忘数据文件 +func (p *Plugin) cleanupData() { + if p.filePath != "" { + os.Remove(p.filePath) + } +} diff --git a/example/rss/plugin.go b/example/rss/plugin.go index c30035a..4a703f1 100644 --- a/example/rss/plugin.go +++ b/example/rss/plugin.go @@ -113,6 +113,9 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error { os.MkdirAll(p.dataDir, 0755) p.loadData() + // 卸载(删除)时清理订阅数据目录;重载不触发 + s.RegisterOnRemoveHandler(p.cleanupData) + s.Settings().RegisterDef(sdk.ConfigDef{ Key: "poll_interval", Default: "30", Type: "string", DisplayName: "Poll Interval", Description: "Default polling interval in minutes (default: 30)", @@ -437,4 +440,11 @@ func (p *Plugin) saveData() { os.WriteFile(p.dataFile(), b, 0644) } +// cleanupData 卸载时清理订阅数据目录(feeds.json 等) +func (p *Plugin) cleanupData() { + if p.dataDir != "" { + os.RemoveAll(p.dataDir) + } +} + diff --git a/example/weather/plugin.go b/example/weather/plugin.go index ccafc44..61c9c05 100644 --- a/example/weather/plugin.go +++ b/example/weather/plugin.go @@ -19,6 +19,7 @@ type Plugin struct { sdk *sdk.PluginSDK client *http.Client defaultLoc string + dataDir string } func NewPluginFactory(name string, config map[string]interface{}) (sdk.Plugin, error) { @@ -48,7 +49,11 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error { if dataHome == "" { dataHome = "/tmp" } - os.MkdirAll(filepath.Join(dataHome, ".homeagent", "weather"), 0755) + p.dataDir = filepath.Join(dataHome, ".homeagent", "weather") + os.MkdirAll(p.dataDir, 0755) + + // 卸载(删除)时清理天气缓存目录;重载不触发 + s.RegisterOnRemoveHandler(p.cleanupData) tp := p.name + "_" s.RegisterTool(tp+"current", sdk.ToolDef{ @@ -423,3 +428,10 @@ func (p *Plugin) handleSetLocation(args map[string]interface{}) (interface{}, er p.defaultLoc = loc return map[string]interface{}{"content": fmt.Sprintf("Default location set to: %s", loc)}, nil } + +// cleanupData 卸载时清理天气缓存目录 +func (p *Plugin) cleanupData() { + if p.dataDir != "" { + os.RemoveAll(p.dataDir) + } +}