mirror of
https://gitcode.com/JianFeeeee/homeagent-sdk.git
synced 2026-10-31 06:17:25 +00:00
* feat(commondev): 主管式多仓库开发流水线 + sidecar 后端抽象(pi/zcode)
## 是什么
agent 统筹开发流程的插件:登记项目 → 开工程线(每条线一个隔离工作目录 +
一个 sidecar 子进程 + 一对通道)→ 投递任务 → 同会话续投 → 评审/合入。
38 个工具:project_*/branch_*/workdir_*/job_*/review_*/test_*/
endpoint_*/agent_*/pi_*/conflict_check。
## sidecar 后端抽象(本轮核心)
传输层(local/ssh)与后端(pi/zcode/自定义)**正交**,四个自由组合:
driver.json 描述一个后端:script / cliPath / env / args / llmConfig
三级查找:插件数据目录 → **随包 rawfile** → 内置 pi 描述
占位符:{workdir} {dataDir} {pluginDir} {rawfile} {sessionId}
★ 内置 pi 回退**与默认后端解耦**(2026-10-17 修):原实现写
`if cli == defaultAgentCLI { return builtinPiDriver() }`,把"给谁兜底"
和"默认选谁"耦合到同一常量 —— 一旦有人改默认,pi 当场失去兜底,
显式 cli="pi" 开始报「驱动不存在」。现在判断字面 "pi"。
默认后端保持 **pi**(我们本地主用);交付方用 zcode 走两种局部开关:
· 单线:commondev_branch_open(cli="zcode")
· 按机器:commondev_endpoint_set(host=..., cli="zcode")
## zcode 接入(三条真机判据,全部要求真改工程文件)
· TestZcodeFullPipeline(29-34s):随包 driver + 真实 ZCode CLI
· TestZcodeOverSSH(39s):真 ssh + scp,远端 node 跑 zcode
· TestZcodeOverSSHWithPerHostEndpoints(68s):两个 host 别名配不同
cli_path,走端点表而不是全局 settings
★ 会话库必须隔离到工作区(sidecar.mjs 的 ensureWorkspaceSessionStore):
实测 ZCode 的会话库路径**硬编码 homedir()**,`ZCODE_DATA_BASE_DIR` 对它
完全无效(26 个会话全落全局库)。但 `<workdir>/.zcode/config.json` 的
`storage.sessionDbPath` **确实生效**。所以启动前写一份工作区配置,
一举解决:多线不共用历史 / 不污染全局 ~/.zcode / 删线不留孤儿会话。
只在不存在时写 —— 用户手工放的要尊重。
## 其他
· rawfile:项目根 rawfile/ 默认全收,解到 <插件目录>/rawfile/,绝对路径拒绝
· 凭证:命名引用 + ${ENV_VAR},独立 0600 文件,绝不进 config.db(0644)
· secretshell:给不受信 agent 用的安全 shell(ackey + safeshell)
· 冲突检查 / 评审判据 / 镜像 / 每条线独立 workdir(hash 命名,合并后删除)
判据 30+ 条,含 4 条「判据本身的判据」(防止判据假绿)。
* fix(sdk): 协议通道纪律 + hmapdev 模板升级 + 示例插件 stdout 污染
## 核心缺陷:插件往 stdout 打日志 = 污染 JSON-RPC 协议通道
子进程插件与内核用 stdout 传 NDJSON 帧,stderr 才是日志通道。
脚手架模板(及 4 个示例插件)用 `fmt.Printf` 打日志 ⇒ 内核收到非 JSON 文本:
[proc] weather 收到非法 JSON 帧(21 字节): invalid character 'i'
**生产实测 1038 条**(huawei_smarthome 720 / weather 113 / rss 103 / calendar 102)。
后果不只是日志噪音:那一行日志被**丢弃**,插件看起来"没启动"。
修法:
- hmapdev 模板 `fmt.Printf` → `log.Printf`(9 处)
- 示例插件 4 个 14 处:ai_image(2) calendar(5) rss(4) weather(3)
+ 补 "log" import
- 内核侧(主仓)同步让报错可定位:内容摘要 + 可能原因 + 修法
判别法:`package main` = 子进程(协议通道,禁用 stdout 打日志);
普通库包 = 进程内插件(编译进内核,写真实终端,**不是**缺陷)——
`internal/plugins/cli` 的 `fmt.Println` 属后者。
## hmapdev
- `cmd_build.go` +136:平台产物精确匹配(`--target` 是**append** 语义,
原脚本 `ls dist/*.hmap | head -1` 会按字典序抓到 amd64,
**导致 arm64 包里装的是 x86-64 二进制**,且退出码 0、校验和通过)
- `cmd_init.go` +46:脚手架 stdio 纪律
- `templates/proc_main.go.tmpl`:补齐 mgrStop/mgrStart/mgrIsRuntimeStopped
三态能力(插件管理器运行态停启)
- `templates/plg.json.tmpl` 删除(合并进 cmd_init)
## 判据
- `scaffold_stdout_test.go`(3 条):扫模板生成的代码不得写 stdout。
★ 判据自身先假红过一次:**模板的注释里就写了 `fmt.Printf`** ⇒
必须 `stripTemplateComments()` 之后才判
- `example_stdout_test.go`(2 条):扫全部 20 个子进程示例插件,
报错给**文件:行号**;变异咬住并给出准确行号
- `package_examples_test.go`(2 条):**执行真实打包脚本**
(第一版测的是 hmapdev 而不是脚本 ⇒ 变异不咬)
- `cmd_build` / `cmd_init` 新判据
## 其他
- sdk/settings.go +15(三态能力透传)
- `docs/versions.md` +§示例插件协议通道纪律(含生产 1038 条实测表格)
- `skills/homeagent-plugin-dev/SKILL.md` +§协议通道纪律(正误代码对比)
* fix(examples): 恢复 go.mod 相对路径 + 移除 plg.json 的 sdk 固定(修 CI 红)
CI 上 `Examples (linux/amd64)` 与 `(darwin/arm64)` 双红,只有两个失败:
✗ commondev 构建失败
✗ editdoc 构建失败
error: 项目声明 sdk=1.4.0,但本地 SDK 存储里没有这个版本
其余 20 个示例全部成功 —— 说明不是通用问题,而是这两个被改坏了。
## 根因(两个叠加)
**① go.mod 被本机绝对路径污染**
我在上一个提交里用 `git add -A`,把本机 `go build` **自动改写**过的
go.mod 一并提交了:
require github.com/JianFeeeee/homeagentsdk v1.4.0
replace github.com/JianFeeeee/homeagentsdk => /root/.homeagent/hmapdev/sdk/v1.4.0
基线(origin/main)是:
require github.com/JianFeeeee/homeagentsdk v0.0.0
replace github.com/JianFeeeee/homeagentsdk => ../../
本机那个目录真的存在 ⇒ 本地全绿;CI 没有 ⇒ 必红。
**② plg.json 声明了 `"sdk": "1.4.0"`**
一声明版本,hmapdev 就去**本地 SDK store** 找它,找不到直接拒绝构建。
全仓 20+ 示例**都不声明**,只有我改过的这两个声明了。
示例的正确依赖表达是 go.mod 的 `v0.0.0` + `replace ../../`。
## 修法
两个文件都恢复到与其余示例一致的写法。
## 判据(3 条,都是新加的)
· `TestExampleGoModHasNoAbsoluteReplace`:任何示例 go.mod 不得含
`/root/`、`/home/`、`/Users/`、`C:\`
· `TestExampleGoModUsesRelativeSDKReplace`:replace 必须是相对路径 `../../`
· `TestExamplePlgJsonDoesNotPinSDKVersion`:plg.json 不得声明 sdk
变异 2/2 咬住(重新引入绝对路径 / 重新声明 sdk,都准确报出文件路径)。
## 本地验证(模拟 CI 干净环境)
用干净 HOME 跑 `package/build-examples.sh`:
· 修复前:成功 18 / 失败 4(commondev, editdoc, browser, rss)
· 修复后:成功 20 / 失败 2(browser, rss —— **本机代理不通**,
`proxy.golang.org` 超时;用本地 modcache 验证这两个可正常构建)
即 22/22 源码都能构建,CI 有网络不受影响。
## 教训
`git add -A` 会把「构建工具自动改写的文件」一起提交。
提交前必须 `git status` 逐个看,尤其 go.mod / go.sum。
---------
Co-authored-by: JianFeeeee <JianFeeeee@users.noreply.gitcode.com>
503 lines
12 KiB
Go
503 lines
12 KiB
Go
package main
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"log"
|
||
"net/http"
|
||
"os"
|
||
"path/filepath"
|
||
"sort"
|
||
"strconv"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
|
||
sdk "github.com/JianFeeeee/homeagentsdk/sdk"
|
||
"github.com/mmcdole/gofeed"
|
||
)
|
||
|
||
const injectDedupWindow = 5 * time.Minute
|
||
|
||
type FeedSub struct {
|
||
URL string `json:"url"`
|
||
Title string `json:"title"`
|
||
AddedAt string `json:"added_at"`
|
||
Interval int `json:"interval"`
|
||
}
|
||
|
||
type Plugin struct {
|
||
name string
|
||
sdk *sdk.PluginSDK
|
||
client *http.Client
|
||
fp *gofeed.Parser
|
||
dataDir string
|
||
mu sync.RWMutex
|
||
feeds []FeedSub
|
||
seenGUIDs map[string]bool
|
||
injected map[string]time.Time
|
||
stopCh chan struct{}
|
||
stopOnce sync.Once
|
||
wg sync.WaitGroup
|
||
pollTicker *time.Ticker
|
||
}
|
||
|
||
func NewPluginFactory(name string, config map[string]interface{}) (sdk.Plugin, error) {
|
||
return &Plugin{name: name}, nil
|
||
}
|
||
|
||
func (p *Plugin) Name() string { return p.name }
|
||
|
||
func getSetting[T string | int64 | float64](s sdk.SettingsAPI, key string, fallback T) T {
|
||
v, err := s.Get(key)
|
||
if err != nil || v == nil {
|
||
return fallback
|
||
}
|
||
switch any(fallback).(type) {
|
||
case string:
|
||
if sv, ok := v.(string); ok {
|
||
return any(sv).(T)
|
||
}
|
||
case int64:
|
||
switch val := v.(type) {
|
||
case float64:
|
||
return any(int64(val)).(T)
|
||
case string:
|
||
if n, err := strconv.ParseInt(val, 10, 64); err == nil {
|
||
return any(n).(T)
|
||
}
|
||
}
|
||
case float64:
|
||
switch val := v.(type) {
|
||
case float64:
|
||
return any(val).(T)
|
||
case string:
|
||
if n, err := strconv.ParseFloat(val, 64); err == nil {
|
||
return any(n).(T)
|
||
}
|
||
}
|
||
}
|
||
return fallback
|
||
}
|
||
|
||
func readArg(args map[string]interface{}, key string) string {
|
||
if v, ok := args[key]; ok && v != nil {
|
||
if s, ok := v.(string); ok {
|
||
return s
|
||
}
|
||
}
|
||
return ""
|
||
}
|
||
|
||
func readArgInt(args map[string]interface{}, key string, fallback int) int {
|
||
if v, ok := args[key]; ok && v != nil {
|
||
switch n := v.(type) {
|
||
case float64:
|
||
return int(n)
|
||
case int64:
|
||
return int(n)
|
||
}
|
||
}
|
||
return fallback
|
||
}
|
||
|
||
func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||
s.SetAutoRestart(true)
|
||
p.sdk = s
|
||
p.client = &http.Client{Timeout: 30 * time.Second}
|
||
// 入站通道:本插件用 "rss" 通道注入输入(见 Inject* 调用),
|
||
// 输入侧必须显式登记 —— 否则"把该 inputch 划给驻留子"会报 `inputch 未注册`。
|
||
_ = s.RegisterInputChannel("rss", sdk.ChannelDef{NoMemory: true})
|
||
p.fp = gofeed.NewParser()
|
||
p.stopCh = make(chan struct{})
|
||
p.seenGUIDs = make(map[string]bool)
|
||
p.injected = make(map[string]time.Time)
|
||
p.feeds = []FeedSub{}
|
||
|
||
dataDirVal, err := s.Settings().GetCore("core.daemon.data_dir")
|
||
if err != nil || dataDirVal == "" {
|
||
dataDirVal = "."
|
||
}
|
||
p.dataDir = filepath.Join(fmt.Sprint(dataDirVal), "rss")
|
||
if err := os.MkdirAll(p.dataDir, 0755); err != nil {
|
||
log.Printf("[%s] mkdir %s: %v", p.name, p.dataDir, err)
|
||
}
|
||
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)",
|
||
Category: "rss",
|
||
})
|
||
|
||
tp := p.name + "_"
|
||
s.RegisterTool(tp+"subscribe", sdk.ToolDef{
|
||
Name: tp + "subscribe", Description: "Subscribe to an RSS/Atom feed URL",
|
||
NoMemory: true,
|
||
Parameters: map[string]interface{}{
|
||
"type": "object",
|
||
"properties": map[string]interface{}{
|
||
"url": map[string]interface{}{"type": "string", "description": "Feed URL"},
|
||
"interval": map[string]interface{}{"type": "integer", "description": "Poll interval in minutes (default: 30, minimum: 5)"},
|
||
},
|
||
"required": []string{"url"},
|
||
},
|
||
}, p.handleSubscribe)
|
||
|
||
s.RegisterTool(tp+"unsubscribe", sdk.ToolDef{
|
||
Name: tp + "unsubscribe", Description: "Unsubscribe from a feed",
|
||
NoMemory: true,
|
||
Parameters: map[string]interface{}{
|
||
"type": "object",
|
||
"properties": map[string]interface{}{
|
||
"url": map[string]interface{}{"type": "string", "description": "Feed URL to unsubscribe"},
|
||
},
|
||
"required": []string{"url"},
|
||
},
|
||
}, p.handleUnsubscribe)
|
||
|
||
s.RegisterTool(tp+"list", sdk.ToolDef{
|
||
Name: tp + "list", Description: "List all subscribed feeds",
|
||
Parameters: map[string]interface{}{
|
||
"type": "object",
|
||
"properties": map[string]interface{}{},
|
||
},
|
||
}, p.handleList)
|
||
|
||
s.RegisterTool(tp+"check_now", sdk.ToolDef{
|
||
Name: tp + "check_now", Description: "Manually check all feeds for new articles now",
|
||
NoMemory: true,
|
||
Parameters: map[string]interface{}{
|
||
"type": "object",
|
||
"properties": map[string]interface{}{},
|
||
},
|
||
}, p.handleCheckNow)
|
||
|
||
pollMin := int(getSetting(s.Settings(), "poll_interval", int64(30)))
|
||
if pollMin < 5 {
|
||
pollMin = 5
|
||
}
|
||
p.pollTicker = time.NewTicker(time.Duration(pollMin) * time.Minute)
|
||
|
||
p.wg.Add(1)
|
||
go p.pollLoop()
|
||
|
||
log.Printf("[%s] started (%d feeds, poll every %dm)", p.name, len(p.feeds), pollMin)
|
||
return nil
|
||
}
|
||
|
||
func (p *Plugin) Stop() error {
|
||
p.stopOnce.Do(func() { close(p.stopCh) })
|
||
p.pollTicker.Stop()
|
||
p.wg.Wait()
|
||
p.saveData()
|
||
log.Printf("[%s] stopped", p.name)
|
||
return nil
|
||
}
|
||
|
||
func (p *Plugin) pollLoop() {
|
||
defer p.wg.Done()
|
||
|
||
p.checkAllFeeds()
|
||
|
||
for {
|
||
select {
|
||
case <-p.pollTicker.C:
|
||
p.checkAllFeeds()
|
||
case <-p.stopCh:
|
||
return
|
||
}
|
||
}
|
||
}
|
||
|
||
func (p *Plugin) checkAllFeeds() {
|
||
p.mu.RLock()
|
||
feeds := make([]FeedSub, len(p.feeds))
|
||
copy(feeds, p.feeds)
|
||
p.mu.RUnlock()
|
||
|
||
for _, feed := range feeds {
|
||
select {
|
||
case <-p.stopCh:
|
||
return
|
||
default:
|
||
}
|
||
p.checkFeed(feed)
|
||
}
|
||
}
|
||
|
||
func (p *Plugin) checkFeed(sub FeedSub) {
|
||
parsed, err := p.fp.ParseURL(sub.URL)
|
||
if err != nil {
|
||
return
|
||
}
|
||
|
||
title := parsed.Title
|
||
if title == "" {
|
||
title = sub.URL
|
||
}
|
||
|
||
var newArticles []*gofeed.Item
|
||
for _, item := range parsed.Items {
|
||
guid := item.GUID
|
||
if guid == "" {
|
||
guid = item.Link
|
||
}
|
||
if guid == "" {
|
||
continue
|
||
}
|
||
guid = sub.URL + "|" + guid
|
||
p.mu.RLock()
|
||
seen := p.seenGUIDs[guid]
|
||
p.mu.RUnlock()
|
||
if !seen {
|
||
newArticles = append(newArticles, item)
|
||
}
|
||
}
|
||
|
||
if len(newArticles) == 0 {
|
||
return
|
||
}
|
||
|
||
now := time.Now()
|
||
toInject := make([]*gofeed.Item, 0, len(newArticles))
|
||
p.mu.Lock()
|
||
for _, item := range newArticles {
|
||
guid := item.GUID
|
||
if guid == "" {
|
||
guid = item.Link
|
||
}
|
||
if guid == "" {
|
||
continue
|
||
}
|
||
key := sub.URL + "|" + guid
|
||
if t, ok := p.injected[key]; ok && now.Sub(t) < injectDedupWindow {
|
||
continue
|
||
}
|
||
p.injected[key] = now
|
||
p.seenGUIDs[key] = true
|
||
toInject = append(toInject, item)
|
||
}
|
||
p.mu.Unlock()
|
||
|
||
if len(toInject) == 0 {
|
||
return
|
||
}
|
||
|
||
var lines []string
|
||
lines = append(lines, fmt.Sprintf("📡 %s (%s) — %d 篇新文章:", title, sub.URL, len(toInject)))
|
||
for _, item := range toInject {
|
||
pubDate := ""
|
||
if item.PublishedParsed != nil {
|
||
pubDate = item.PublishedParsed.Format("01-02 15:04")
|
||
}
|
||
line := fmt.Sprintf(" • %s", item.Title)
|
||
if pubDate != "" {
|
||
line += fmt.Sprintf(" [%s]", pubDate)
|
||
}
|
||
if item.Link != "" {
|
||
line += "\n " + item.Link
|
||
}
|
||
lines = append(lines, line)
|
||
}
|
||
|
||
// 中断注入是「系统通知」,NoMemory 写明意图:这类提醒不参与记忆计算,
|
||
// 原文仍进上下文(模型当轮看得到)。
|
||
p.sdk.InjectInterruptTextOpts("rss", "rss", strings.Join(lines, "\n"),
|
||
sdk.InjectOptions{NoMemory: true})
|
||
p.saveData()
|
||
}
|
||
|
||
func (p *Plugin) handleSubscribe(args map[string]interface{}) (interface{}, error) {
|
||
url := readArg(args, "url")
|
||
if url == "" {
|
||
return map[string]interface{}{"isError": true, "content": "URL is required"}, nil
|
||
}
|
||
|
||
p.mu.RLock()
|
||
for _, f := range p.feeds {
|
||
if f.URL == url {
|
||
p.mu.RUnlock()
|
||
return map[string]interface{}{"isError": true, "content": "Already subscribed to: " + url}, nil
|
||
}
|
||
}
|
||
p.mu.RUnlock()
|
||
|
||
interval := readArgInt(args, "interval", 30)
|
||
if interval < 5 {
|
||
interval = 5
|
||
}
|
||
|
||
parsed, err := p.fp.ParseURL(url)
|
||
if err != nil {
|
||
return map[string]interface{}{"isError": true, "content": "Failed to parse feed: " + err.Error()}, nil
|
||
}
|
||
|
||
feedTitle := parsed.Title
|
||
if feedTitle == "" {
|
||
feedTitle = url
|
||
}
|
||
|
||
sub := FeedSub{
|
||
URL: url,
|
||
Title: feedTitle,
|
||
AddedAt: time.Now().Format("2006-01-02 15:04"),
|
||
Interval: interval,
|
||
}
|
||
|
||
guidCount := 0
|
||
p.mu.Lock()
|
||
for _, item := range parsed.Items {
|
||
guid := item.GUID
|
||
if guid == "" {
|
||
guid = item.Link
|
||
}
|
||
if guid == "" {
|
||
continue
|
||
}
|
||
p.seenGUIDs[url+"|"+guid] = true
|
||
guidCount++
|
||
}
|
||
p.mu.Unlock()
|
||
|
||
p.mu.Lock()
|
||
p.feeds = append(p.feeds, sub)
|
||
p.mu.Unlock()
|
||
p.saveData()
|
||
|
||
return map[string]interface{}{
|
||
"content": fmt.Sprintf("Subscribed to: %s\nTitle: %s\nArticles found: %d\nPoll interval: %d min", url, feedTitle, guidCount, interval),
|
||
}, nil
|
||
}
|
||
|
||
func (p *Plugin) handleUnsubscribe(args map[string]interface{}) (interface{}, error) {
|
||
url := readArg(args, "url")
|
||
if url == "" {
|
||
return map[string]interface{}{"isError": true, "content": "URL is required"}, nil
|
||
}
|
||
|
||
p.mu.Lock()
|
||
found := false
|
||
for i, f := range p.feeds {
|
||
if f.URL == url {
|
||
p.feeds = append(p.feeds[:i], p.feeds[i+1:]...)
|
||
found = true
|
||
break
|
||
}
|
||
}
|
||
if !found {
|
||
p.mu.Unlock()
|
||
return map[string]interface{}{"isError": true, "content": "Not subscribed to: " + url}, nil
|
||
}
|
||
|
||
for guid := range p.seenGUIDs {
|
||
if strings.HasPrefix(guid, url+"|") {
|
||
delete(p.seenGUIDs, guid)
|
||
}
|
||
}
|
||
p.mu.Unlock()
|
||
p.saveData()
|
||
|
||
return map[string]interface{}{"content": "Unsubscribed: " + url}, nil
|
||
}
|
||
|
||
func (p *Plugin) handleList(args map[string]interface{}) (interface{}, error) {
|
||
p.mu.RLock()
|
||
defer p.mu.RUnlock()
|
||
|
||
if len(p.feeds) == 0 {
|
||
return map[string]interface{}{"content": "No subscriptions. Use rss_subscribe to add one."}, nil
|
||
}
|
||
|
||
sort.Slice(p.feeds, func(i, j int) bool {
|
||
return p.feeds[i].Title < p.feeds[j].Title
|
||
})
|
||
|
||
var lines []string
|
||
lines = append(lines, fmt.Sprintf("📡 Subscriptions (%d):", len(p.feeds)))
|
||
for _, f := range p.feeds {
|
||
lines = append(lines, fmt.Sprintf(" • %s\n %s (every %dm, added %s)", f.Title, f.URL, f.Interval, f.AddedAt))
|
||
}
|
||
|
||
return map[string]interface{}{"content": strings.Join(lines, "\n")}, nil
|
||
}
|
||
|
||
func (p *Plugin) handleCheckNow(args map[string]interface{}) (interface{}, error) {
|
||
select {
|
||
case <-p.stopCh:
|
||
return map[string]interface{}{"isError": true, "content": "plugin is stopping"}, nil
|
||
default:
|
||
}
|
||
p.wg.Add(1)
|
||
go func() {
|
||
defer p.wg.Done()
|
||
p.checkAllFeeds()
|
||
}()
|
||
return map[string]interface{}{"content": "Checking all feeds for updates..."}, nil
|
||
}
|
||
|
||
func (p *Plugin) dataFile() string {
|
||
return filepath.Join(p.dataDir, "feeds.json")
|
||
}
|
||
|
||
func (p *Plugin) loadData() {
|
||
b, err := os.ReadFile(p.dataFile())
|
||
if err != nil {
|
||
return
|
||
}
|
||
var data struct {
|
||
Feeds []FeedSub `json:"feeds"`
|
||
SeenGUIDs map[string]bool `json:"seen"`
|
||
}
|
||
if json.Unmarshal(b, &data) != nil {
|
||
return
|
||
}
|
||
if data.Feeds != nil {
|
||
p.feeds = data.Feeds
|
||
}
|
||
if data.SeenGUIDs != nil {
|
||
p.seenGUIDs = data.SeenGUIDs
|
||
}
|
||
}
|
||
|
||
func (p *Plugin) saveData() {
|
||
p.mu.RLock()
|
||
defer p.mu.RUnlock()
|
||
data := struct {
|
||
Feeds []FeedSub `json:"feeds"`
|
||
SeenGUIDs map[string]bool `json:"seen"`
|
||
}{
|
||
Feeds: p.feeds,
|
||
SeenGUIDs: p.seenGUIDs,
|
||
}
|
||
b, _ := json.MarshalIndent(data, "", " ")
|
||
atomicWriteJSON(p.dataFile(), b)
|
||
}
|
||
|
||
// cleanupData 卸载时清理订阅数据目录(feeds.json 等)
|
||
func (p *Plugin) cleanupData() {
|
||
p.mu.Lock()
|
||
defer p.mu.Unlock()
|
||
if p.dataDir == "" {
|
||
return
|
||
}
|
||
for _, f := range []string{"feeds.json"} {
|
||
path := filepath.Join(p.dataDir, f)
|
||
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
||
log.Printf("[%s] onRemove cleanup %s: %v", p.name, path, err)
|
||
}
|
||
}
|
||
}
|
||
|
||
// atomicWriteJSON 原子写 JSON:先写临时文件再 rename,避免进程崩溃截断数据文件。
|
||
func atomicWriteJSON(path string, data []byte) error {
|
||
tmp := path + ".tmp"
|
||
if err := os.WriteFile(tmp, data, 0644); err != nil {
|
||
return err
|
||
}
|
||
return os.Rename(tmp, path)
|
||
}
|