magpie-plugin-loomy-auth 0.1.14 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/AGENTS.md ADDED
@@ -0,0 +1,110 @@
1
+ # AGENTS.md —— 给 AI 执行者的入口
2
+
3
+ > 人类请看 [README](./README.md)。这份只回答两件事:**你现在该读哪一份**、
4
+ > **忘了哪一条会静默出错**。
5
+ >
6
+ > 结构照搬 [magpie-plugin-scaffold](../magpie-plugin-scaffold/AGENTS.md);
7
+ > 第二节是本仓库自己的坑,来自 `docs/internals.md` 的实测记录。
8
+
9
+ ---
10
+
11
+ ## 一、先定位你在哪一步
12
+
13
+ | 你在做什么 | 必读(按顺序) | 做完的判据 |
14
+ |---|---|---|
15
+ | **第一次碰这个仓库**(想知道它为什么存在) | [docs/internals.md](./docs/internals.md) 的「这个插件修的是什么」 | 能说清 `used` 不上报会怎样(空号排队首) |
16
+ | **改额度 / `usage` 上报** | internals 的「这个插件修的是什么」+ HOOKS 九 | 今日池参与路由、永久池 `aside`、两池皆空有 `used:100` |
17
+ | **改签到** | internals 的「协议要点」签到那几行 | `outcome` 在契约六个合法值里 |
18
+ | **改积分 / 邀请 / 激活** | internals 的「协议要点」「一个账号的永久积分上限」 | 数值与实测账对得上(双方各 5000、上限 25,000) |
19
+ | **改登录 / 刷新** | internals 的「登录方式」 | 刷新有节流,不是每次都打 |
20
+ | **代码写完,准备交付** | [CHECKLIST.md](./CHECKLIST.md) 的 **A 表** | A 表全勾 + check 脚本退出 0 |
21
+ | **要发版本** | [UPDATING.md](https://github.com/MYCF711/magpie-plugin-scaffold/blob/main/UPDATING.md)(七步) | CHECKLIST B 表全勾 + 四条验证 |
22
+
23
+ **最容易漏的是最后一行。** `git push` 只是七步里的第 4 步。
24
+
25
+ ---
26
+
27
+ ## 二、本仓库的坑
28
+
29
+ | # | 症状(出问题时你看到什么) | 判据 | 出处 |
30
+ |---|---|---|---|
31
+ | 1 | 额度条正常,但**被用空的号反而最先被派活** | `usage` 必须上报 `used`。magpie 归一化时 `Math.max(0, num(w?.used))` —— 不给就是 0 = 满额 = 排队首。这是本仓库存在的理由 | internals 这个插件修的是什么 |
32
+ | 2 | 账号被误判「花完了」/ 或永远不被判花完 | **今日池**参与路由(`used`/`amount`/`limit`/`resetsAt` 齐全);**永久池**恒 `aside: true` 只给 display | internals 同上 |
33
+ | 3 | 两池都空的号仍然排在最前面 | 必须**合成一条 `used: 100` 的参与路由窗口**把它压到底;不画窗口 = 继续以 0% 排第一 | internals 同上 |
34
+ | 4 | 界面上额度占比是编的 | 今日上限未知时 → `aside` + `used: 0`,显示「N 积分剩余(今日上限未知)」。宁可降级也不谎报占比 | internals 同上 |
35
+ | 5 | 签到界面出现「签到失败 · 每日赠送积分已刷新」这种自相矛盾的行 | `outcome` 必须是契约六值之一(`claimed`/`done`/`ineligible`/`inactive`/`captcha`/`failed`)。返回 `"success"` 会被归为 failed **但保留 message** | internals 同上 |
36
+ | 6 | 额度读数慢、拖慢整张卡片 | 热路径用 `/api/v1/points/balance` —— 它只回 3 个数字,实测 61~233ms,是三家现有插件都没用的最小端点 | internals 协议要点 |
37
+ | 7 | 「今天已领」被显示成「刚领到」 | `POST /points/first-login` 幂等,判据是 `alreadyProcessed`;「今天已领」必须是 `done`,不是 `claimed` | internals 协议要点 |
38
+ | 8 | 频繁刷新把上游打烦 / 触发限流 | token 刷新要**节流**,不能每次都打 | internals 协议要点 |
39
+ | 9 | 邀请奖励的数字对不上 | 实测邀请双方**各得 5000**,不是 2000 | internals 协议要点 |
40
+ | 10 | 拿别的账号的凭据去请求,串号 | `getAuth()` 只在当前账号作用域(magpie 的 `inScope`)可见,拿不到别的账号 | internals 协议要点 |
41
+ | 11 | 以为能给「锁定永久积分」加个开关 | 不需要 —— 永久池恒 `aside`,magpie 只在其它号都不可用时才用它,已经是「优先别烧永久积分」 | internals 已知取舍 |
42
+ | 12 | `magpie plugin add xxx.tgz` 装不上 | 它把 tgz 当 JS 模块 `import()`;本地包走 `plugins.json` + `plugins/package.json` 登记 | internals 从本地 tgz 安装 |
43
+ | 13 | 改了代码重装,行为没变 | 同版本号重打包命中 bun 缓存 —— 必须 bump `version`;另外「装完要不要重启」实测**不必需**,但触发机制未查明 | internals 从本地 tgz 安装 |
44
+ | 14 | npm 上装到了别人的包 | 本仓库 **npm 包名 ≠ 仓库名**:npm 是 `magpie-plugin-loomy-auth`,仓库是 `magpie-loomy-auth`。加 `-plugin-` 段就是为了避开撞名 | internals 从本地 tgz 安装 |
45
+ | 15 | 卡片标题 / 源码按钮不对 | `registry-entry.json` 的 `package` 必须与 `package.json` 的 `name` **一字不差**;插件不写 magpie 的市场缓存 | internals 从本地 tgz 安装 |
46
+
47
+ ---
48
+
49
+ ## 三、命令
50
+
51
+ ```bash
52
+ # 合规检查(脚手架里的脚本,指到本仓库)
53
+ node ../magpie-plugin-scaffold/scripts/check.mjs .
54
+
55
+ # 语义测试(usage / 签到 outcome 那几条规则在这儿锁着)
56
+ node tests/semantics.test.mjs
57
+
58
+ # 打包面预演
59
+ npm pack --dry-run
60
+
61
+ # 发布(包内 name 是 Loomy,npm 包名是 magpie-plugin-loomy-auth)
62
+ npm run publish:npm
63
+
64
+ # Release(本仓库没有 .github/workflows,手工建)
65
+ git tag v0.1.16 && git push origin v0.1.16
66
+ # 然后 POST /repos/MYCF711/magpie-loomy-auth/releases
67
+ ```
68
+
69
+ ---
70
+
71
+ ## 四、两个交付门
72
+
73
+ **门 1 · 代码交付**
74
+
75
+ ```
76
+ □ node ../magpie-plugin-scaffold/scripts/check.mjs . 退出码 = 0
77
+ □ node tests/semantics.test.mjs 全绿(usage 语义不能靠眼睛看)
78
+ □ CHECKLIST.md 的 A 表全勾
79
+ ```
80
+
81
+ **门 2 · 发布**
82
+
83
+ ```
84
+ □ CHECKLIST.md 的 B 表全勾(七步)
85
+ □ 四条验证:npm view magpie-plugin-loomy-auth version / tarball 内容 /
86
+ magpie plugin list / Release 在
87
+ ```
88
+
89
+ ⚠ 没过门 2 不许说「已发布」。
90
+
91
+ ---
92
+
93
+ ## 五、文档地图
94
+
95
+ | 文件 | 管什么 | 什么时候读 |
96
+ |---|---|---|
97
+ | [README](./README.md) | 用户门面:功能、模型、签到、安装、常见问题 | 改用户可见行为时 |
98
+ | [docs/internals.md](./docs/internals.md) | 修的是什么、登录、协议要点、测试、已知取舍、永久积分上限 | 改代码前按章查 |
99
+ | [tests/semantics.test.mjs](./tests/semantics.test.mjs) | `usage` 与签到 `outcome` 的语义锁 | 改这两块必跑 |
100
+ | [CHANGELOG.md](./CHANGELOG.md) | 每版一节 | 写新版本时 |
101
+ | [CHECKLIST.md](./CHECKLIST.md) | 两张勾选表 | 两个交付门 |
102
+ | 脚手架 [AGENTS.md](../magpie-plugin-scaffold/AGENTS.md) | 通用坑与 check 能查什么 | 第一次接触这个插件族时 |
103
+
104
+ ---
105
+
106
+ ## 六、这份文件的维护规矩
107
+
108
+ 1. 任何一次「不报错、要几小时才找到」的坑,收工前在第二节加一行(症状 → 判据 → 出处)。
109
+ 2. 第二节只放一句话判据,论证留在 `docs/internals.md`。
110
+ 3. 判据要能当场验证(命令、字段、界面现象)。
package/CHANGELOG.md ADDED
@@ -0,0 +1,37 @@
1
+ # 更新记录
2
+
3
+ > 每个版本一节,**一条一行**,说用户能感知的变化。
4
+ > 格式与发布流程见 [magpie-plugin-scaffold](https://github.com/MYCF711/magpie-plugin-scaffold) 的 [UPDATING.md](https://github.com/MYCF711/magpie-plugin-scaffold/blob/main/UPDATING.md)。
5
+
6
+ ## 0.1.16 — 2026-10-28
7
+
8
+ - **新增 `AGENTS.md`** —— 给 AI 执行者的入口:按「你在哪一步」给阅读顺序,外加本仓库 15 条会静默出错的坑(症状 → 判据 → 出处)。核心那条就是本插件存在的理由:`usage` 不上报 `used` 会让**被用空的号以「满额」排队首**
9
+ - **新增 `CHECKLIST.md`** —— A 表 13 项代码交付前(含 usage 三态、签到 outcome 六值、热路径端点、刷新节流),B 表发版前七步
10
+ - [README](./README.md) —— 顶部加指路三行;发布是七步,`git push` 只是第 4 步
11
+ - [CHANGELOG](./CHANGELOG.md) 与 `publish-npm.mjs` 里的脚手架链接从旧名 `magpie-plugin-template` 改为 `magpie-plugin-scaffold`
12
+ - `package.json` 的 `files` 补上这两份,否则 npm 包里没有它们
13
+
14
+ ## 0.1.15 — 2026-10-27
15
+
16
+ - **插件不再写 magpie 自己的缓存** —— 删掉 `src/self-register.mjs` 与 index 里的调用:`~/.cache/magpie/plugin-market.json` 属于 magpie,每次加载都去改它等于把 registry 条目盖掉
17
+ - **卡片标题与文案改由 registry 条目承担** —— 提交到 `magpie-community/plugins` 的条目是唯一受支持的做法
18
+ - 其余行为(登录、模型、积分、签到)**一行未动**,凭据形状不变,不用重新登录
19
+
20
+ ## 0.1.14 — 2026-10-26
21
+
22
+ 首次留档。0.1.5–0.1.13 是同一天的连续迭代,没有分开记 —— 下面这些是这个插件**现在**
23
+ 给用户的东西:
24
+
25
+ - **两种积分池分开上报** —— 今日池参与路由,永久池只显示不参与;空号不会再以「满额」的姿态排在队首
26
+ - **签到一次做完三件事** —— 刷新今日额度、领 10,000 新手任务积分、兑换邀请码,全部幂等
27
+ - **免费模型按厂商口径判定** —— 不靠「倍率是 0」猜
28
+ - **邀请码只判断「这个号是否已被邀请过」** —— 不做多余判断
29
+ - **256px 图标** —— 卡片上不再是糊的
30
+ - **README 重写** —— 开头给数字、功能一条一行、安装放最后、参考源逐条列出
31
+ - **新增 `CHANGELOG.md`**,并让推 tag 自动生成 GitHub Release
32
+ - **仓库里的 `name` 改成 `Loomy`** —— 卡片标题就是包名(`plugins/package.json` 的依赖键由包内 `name` 决定),改这里才永久生效:`add` / `update` 不会再把它重置回 `magpie-plugin-loomy-auth`
33
+ - **新增 `publish-npm.mjs`** —— npm 上的包名仍是 `magpie-plugin-loomy-auth`(registry 不接受含大写的新包名);发布改用 `npm run publish:npm`,别再裸跑 `npm publish`
34
+
35
+ ## 更早
36
+
37
+ 未留档 —— `git log --oneline` 是唯一的记录。
package/CHECKLIST.md ADDED
@@ -0,0 +1,88 @@
1
+ # CHECKLIST —— 两张表,两个交付门
2
+
3
+ > 给 `magpie-loomy-auth`。`scripts/check.mjs`(在脚手架仓库里)只查**能自动查的那部分**;
4
+ > 下面这些查不到,但漏了照样出事,而且通常**不报错**。
5
+ >
6
+ > 逐条勾,勾不上就是还没好。
7
+
8
+ ---
9
+
10
+ ## A 表 · 代码交付前
11
+
12
+ ```
13
+ □ 1. usage 上报了 used(不是只画进度条)
14
+ 判据:被用空的号**不会**排到队首(magpie 归一化时 used 缺失 = 0 = 满额)
15
+ 出处:internals 这个插件修的是什么
16
+
17
+ □ 2. 今日池参与路由,永久池 aside
18
+ 判据:今日池有 used/amount/limit/resetsAt;永久池 aside: true 只给 display
19
+ 出处:同上
20
+
21
+ □ 3. 两池皆空时合成了 used:100 的参与路由窗口
22
+ 判据:空号沉底,不是以 0% 排第一
23
+ 出处:同上
24
+
25
+ □ 4. 上限未知时降级而不是谎报
26
+ 判据:aside + used: 0 + 文案「今日上限未知」
27
+ 出处:同上
28
+
29
+ □ 5. 签到 outcome 在契约六值里
30
+ 判据:claimed / done / ineligible / inactive / captcha / failed —— 没有 "success"
31
+ 出处:internals 同上
32
+
33
+ □ 6. 热路径用 /points/balance
34
+ 判据:额度读数不拖慢卡片(那个端点只回 3 个数字)
35
+ 出处:internals 协议要点
36
+
37
+ □ 7. first-login 的「今天已领」判据是 done
38
+ 判据:alreadyProcessed 时显示「今天已领」,不是「刚领到」
39
+ 出处:internals 协议要点
40
+
41
+ □ 8. token 刷新有节流
42
+ 判据:不是每次调用都打上游
43
+ 出处:internals 协议要点
44
+
45
+ □ 9. 只读当前账号作用域
46
+ 判据:getAuth() 拿不到别的账号(inScope 隔离)
47
+ 出处:internals 协议要点
48
+
49
+ □ 10. 没有写 magpie 的市场缓存
50
+ 判据:src 里搜不到 plugin-market.json
51
+ 出处:internals 从本地 tgz 安装
52
+
53
+ □ 11. registry-entry.json 的 package 与 package.json 的 name 一字不差
54
+ 判据:都是 Loomy(npm 包名是另一回事:magpie-plugin-loomy-auth)
55
+ 出处:同上
56
+
57
+ □ 12. node tests/semantics.test.mjs 全绿
58
+ 判据:usage 与 outcome 的语义锁没被破坏
59
+
60
+ □ 13. node ../magpie-plugin-scaffold/scripts/check.mjs . 退出码 = 0
61
+ ```
62
+
63
+ ---
64
+
65
+ ## B 表 · 发版前(七步,一步不能少)
66
+
67
+ > ⚠ **`git push` 只是第 4 步。**
68
+
69
+ ```
70
+ □ 第 0 步 · 写 CHANGELOG(一条一行)
71
+ □ 第 1 步 · bump version
72
+ 判据:package.json 的 version 变了(同版本重打命中 bun 缓存)
73
+ □ 第 2 步 · npm pack --dry-run
74
+ 判据:清单里有 README.md / CHANGELOG.md / docs / AGENTS.md / CHECKLIST.md
75
+ □ 第 3 步 · 提交
76
+ □ 第 4 步 · git push origin main
77
+ □ 第 5 步 · npm run publish:npm
78
+ 判据:返回 + magpie-plugin-loomy-auth@<版本>;package.json 的 name 已还原成 Loomy
79
+ □ 第 6 步 · tag + Release
80
+ 命令:git tag v<version> && git push origin v<version>
81
+ 本仓库**没有** .github/workflows ⇒ Release 手工建:
82
+ POST /repos/MYCF711/magpie-loomy-auth/releases
83
+ □ 第 7 步 · 四条验证
84
+ ① npm view magpie-plugin-loomy-auth version
85
+ ② 下载 dist.tarball 解包看内容(不只看版本号)
86
+ ③ magpie plugin list
87
+ ④ GET /releases/tags/v<version> → 200 且 draft=false
88
+ ```
package/README.md CHANGED
@@ -9,6 +9,11 @@
9
9
  今日积分 每天 5,000 每天重置
10
10
  ```
11
11
 
12
+ > **改代码 / 发版本之前**:先看 [AGENTS.md](./AGENTS.md) —— 按「你在哪一步」给阅读顺序,
13
+ > 外加本仓库 15 条**会静默出错**的坑(症状 → 判据 → 出处)。交付与发版逐条走
14
+ > [CHECKLIST.md](./CHECKLIST.md) 的两张表。
15
+ > 发布是**七步**,`git push` 只是第 4 步。
16
+
12
17
  ---
13
18
 
14
19
  ## 功能
@@ -114,6 +119,8 @@ magpie plugin list
114
119
 
115
120
  ---
116
121
 
122
+ > 更新记录:[CHANGELOG.md](./CHANGELOG.md)
123
+
117
124
  ## 许可
118
125
 
119
126
  MIT。
package/docs/internals.md CHANGED
@@ -304,8 +304,13 @@ cd ~/.config/magpie/plugins && ~/.cache/magpie/bun/1.4.4/magpie-bun.exe install
304
304
  magpie 的「有新版本」检查按**包名**去 npm 查,与仓库属主无关 —— 撞名会让界面提示
305
305
  一个**别人的**新版,点「更新」就把别人的包装进来顶掉本插件。加 `-plugin-` 段避开。
306
306
 
307
- ⚠ **插件内 `REGISTRY_ENTRY.package` 必须与 `package.json` 的 `name` 一字不差**,
308
- 否则自注册条目对不上市场卡片。
307
+ ⚠ **提交给 registry 的条目里 `package` 必须与 `package.json` 的 `name` 一字不差**
308
+ (`magpie-plugin-loomy-auth`),否则市场卡片对不上这个包。
309
+
310
+ ⚠ 插件**不写** magpie 的市场缓存(`~/.cache/magpie/plugin-market.json`)——
311
+ 那份文件属于 magpie,插件每次加载都去 merge,等于把 registry 条目盖掉。
312
+ 0.1.14 及更早曾用 `src/self-register.mjs` 这么干,0.1.15 删除。
313
+ 卡片标题与文案的唯一载体是 registry 条目。
309
314
 
310
315
  ---
311
316
 
package/index.mjs CHANGED
@@ -73,22 +73,10 @@ import { readDesktopSession, readAllLoomySessions, readAllLoomyAccounts } from "
73
73
  import { staticModels, liveModels } from "./src/models.mjs"
74
74
  import { readUsage, publicUsage, quotaToRemember, clearUsageCache } from "./src/usage.mjs"
75
75
  import { chatFetch } from "./src/chat.mjs"
76
- import { startSelfRegister } from "./src/self-register.mjs"
77
76
 
78
77
  const API_URL = `${INTEGRATION_BASE}/api/v1`
79
78
 
80
- const REGISTRY_ENTRY = {
81
- package: "magpie-plugin-loomy-auth",
82
- name: PROVIDER_NAME,
83
- providers: [PROVIDER],
84
- community: true,
85
- summary: {
86
- en: "Loomy (iFlytek) coding subscription for magpie: desktop-session import / SMS / session sign-in, live model catalog with reasoning efforts, chat with tool calling and images, and the two credit pools (daily + permanent) reported as the host routes them — the daily pool counts, the permanent one is shown aside.",
87
- zh: "讯飞 Loomy 编程订阅:桌面端登录态导入 / 短信 / 粘贴 session 三种登录,实时模型目录与思考档位,工具调用与图片对话,以及分池上报的积分(今日池参与路由、永久池仅展示)—— 修掉「退化账号显示剩余 100%」这类占比缺陷。",
88
- },
89
- }
90
-
91
- export const LoomyAuthPlugin = async ({ client, directory } = {}) => {
79
+ export const LoomyAuthPlugin = async ({ client } = {}) => {
92
80
  const log = (message, level = "info") => {
93
81
  try {
94
82
  client?.app?.log?.({ service: "loomy", level, message })
@@ -97,8 +85,8 @@ export const LoomyAuthPlugin = async ({ client, directory } = {}) => {
97
85
  }
98
86
  }
99
87
 
100
- // 自注册:写一次就够(magpie 静默时不碰市场缓存,实测 3 分钟零变化)
101
- startSelfRegister(REGISTRY_ENTRY, { configDir: directory, log })
88
+ // 卡片标题与文案不在这里做 —— 那是 registry 条目的职责(registry-entry.json)。
89
+ // 插件不写 magpie 自己的缓存目录:那份文件属于 magpie,插件只读不写。
102
90
 
103
91
  // 目录进程内缓存:session -> { at, models }
104
92
  const catalog = new Map()
package/package.json CHANGED
@@ -1,21 +1,27 @@
1
1
  {
2
2
  "name": "magpie-plugin-loomy-auth",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
4
4
  "description": "Loomy (iFlytek) for magpie: desktop-session import / SMS / session sign-in, live model catalog with reasoning efforts, chat with tools and images, and the two credit pools reported as the host routes them — the daily pool counts, the permanent one is shown aside.",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",
7
7
  "exports": "./index.mjs",
8
8
  "files": [
9
+ "AGENTS.md",
10
+ "CHECKLIST.md",
9
11
  "index.mjs",
10
12
  "src",
11
13
  "docs",
12
14
  "README.md",
15
+ "CHANGELOG.md",
13
16
  "NOTICE.md",
14
17
  "LICENSE",
15
- "assets"
18
+ "assets",
19
+ "publish-npm.mjs"
16
20
  ],
17
21
  "keywords": [
18
22
  "magpie",
23
+ "provider",
24
+ "auth",
19
25
  "magpie-plugin",
20
26
  "opencode",
21
27
  "opencode-plugin",
@@ -33,7 +39,8 @@
33
39
  "node": "^22.19.0 || >=24.0.0"
34
40
  },
35
41
  "scripts": {
36
- "test": "node --test tests/"
42
+ "test": "node --test tests/",
43
+ "publish:npm": "node publish-npm.mjs"
37
44
  },
38
45
  "magpie": {
39
46
  "icon": "https://raw.githubusercontent.com/MYCF711/magpie-loomy-auth/main/assets/icon.png",
@@ -0,0 +1,40 @@
1
+ // 发布到 npm 的包装器 —— 用 `npm run publish:npm`,不要裸跑 `npm publish`。
2
+ //
3
+ // 为什么要有它:两个读者开的是**同一个** package.json。
4
+ //
5
+ // bun(magpie 的 git 安装)读它 → 决定插件卡片上的标题(依赖键 = name)
6
+ // npm publish 读它 → 决定包名
7
+ //
8
+ // npm 不允许**新**包名含大写(registry 会拒:name can no longer contain capital
9
+ // letters),而卡片标题想要供应商自己的写法。所以发布这一下把 name 换成 npm 上的
10
+ // 包名,发完立刻还原 —— 仓库里永远留着好看的那个名字。
11
+ //
12
+ // ⚠ 从此不要裸跑 `npm publish`:那会拿 "Loomy" 去发,被 registry 拒绝。
13
+ // ⚠ 机制说明见 magpie-plugin-scaffold 的 UPDATING.md。
14
+
15
+ import fs from "node:fs"
16
+ import { fileURLToPath } from "node:url"
17
+ import { execSync } from "node:child_process"
18
+
19
+ /** npm 上的包名。与仓库里的 name 不同是**故意的** —— 已装的用户按这个名收更新。 */
20
+ const PUBLISH_AS = "magpie-plugin-loomy-auth"
21
+
22
+ const FILE = fileURLToPath(new URL("./package.json", import.meta.url))
23
+
24
+ const orig = fs.readFileSync(FILE, "utf8")
25
+ const pkg = JSON.parse(orig)
26
+
27
+ if (pkg.name === PUBLISH_AS) {
28
+ console.error(`package.json 的 name 已经是 ${PUBLISH_AS} —— 没有可换的,停。`)
29
+ process.exit(1)
30
+ }
31
+
32
+ pkg.name = PUBLISH_AS
33
+ fs.writeFileSync(FILE, JSON.stringify(pkg, null, 2) + "\n")
34
+
35
+ try {
36
+ execSync(process.argv[2] || "npm publish", { stdio: "inherit" })
37
+ } finally {
38
+ fs.writeFileSync(FILE, orig)
39
+ console.log(`name 已还原为 ${JSON.parse(orig).name}`)
40
+ }
@@ -1,234 +0,0 @@
1
- // 自注册:把本插件写进 magpie 的市场缓存
2
- //
3
- // ═══════════════════════════════════════════════════════════════════
4
- // 问题
5
- // ═══════════════════════════════════════════════════════════════════
6
- //
7
- // magpie 的插件卡片上,「源码」按钮和卡片标题来自
8
- // ~/.cache/magpie/plugin-market.json —— magpie 的社区插件列表缓存。
9
- //
10
- // 包不在那个列表里 ⇒ 卡片标题退回包名,没有源码按钮。
11
- //
12
- // ═══════════════════════════════════════════════════════════════════
13
- // 做法:攒一批,合并去重,写一次
14
- // ═══════════════════════════════════════════════════════════════════
15
- //
16
- // magpie 把**所有插件加载进同一个 Bun 进程**(它自己的文档写
17
- // "one Bun process, the plugin host")。所以插件之间可以通过
18
- // globalThis 共享一个队列。
19
- //
20
- // 插件 A 加载 → 入队
21
- // 插件 B 加载 → 入队
22
- // ... ↓ 100ms 窗口
23
- // 合并去重 → 一次写入
24
- //
25
- // 实测:10 个插件同时启动,**只写盘 1 次**,全部存活,官方条目不动。
26
- //
27
- // ═══════════════════════════════════════════════════════════════════
28
- // 只写一次,绝不轮询
29
- // ═══════════════════════════════════════════════════════════════════
30
- //
31
- // 实测(2026-10-25):
32
- //
33
- // 手工往缓存里加一条 → 等 10 秒 → 没被覆盖
34
- // 再跑 plugin update → 也没被覆盖
35
- // 连续观察 3 分钟 → mtime 零变化
36
- //
37
- // magpie 只在启动时重写这个文件,静默运行期间完全不碰。
38
- //
39
- // ⚠⚠ 绝对不要用 fs.watch 或 setInterval 去「补」。
40
- // 我试过:我们写文件(rename)会触发自己的 watch,watch 又写,
41
- // 每秒 3-4 次,永不停止(实测 60 秒内 mtime 变化 19 次)。
42
- // 删掉后 60 秒变化 0 次。
43
- //
44
- // ═══════════════════════════════════════════════════════════════════
45
-
46
- import fs from "node:fs"
47
- import path from "node:path"
48
- import os from "node:os"
49
-
50
- /** 官方 registry 的地址(缓存丢了时的兜底)*/
51
- const OFFICIAL_REGISTRY =
52
- "https://raw.githubusercontent.com/magpie-community/plugins/main/registry.json"
53
-
54
- /** 队列的共享键(Symbol.for 保证同一进程内所有插件拿到同一个)*/
55
- const QUEUE_KEY = Symbol.for("magpie.plugin-template.registry-queue")
56
-
57
- /** 攒批窗口。够短(卡片不卡),够长(同期插件都能进来)*/
58
- const FLUSH_MS = 100
59
-
60
- /**
61
- * 从 magpie 的配置目录推出它的缓存目录。
62
- *
63
- * 插件的 directory 是 magpie 的 config folder(~/.config/magpie)。
64
- * XDG 规则下 cache 和 config 平级。
65
- */
66
- export function cacheDirOf(configDir) {
67
- if (configDir && typeof configDir === "string") {
68
- const cfg = path.resolve(configDir)
69
- const guess = path.join(path.dirname(path.dirname(cfg)), ".cache", "magpie")
70
- if (fs.existsSync(guess)) return guess
71
- if (process.platform === "darwin") {
72
- const mac = path.join(os.homedir(), "Library", "Caches", "magpie")
73
- if (fs.existsSync(mac)) return mac
74
- }
75
- const xdg = process.env.XDG_CACHE_HOME
76
- if (xdg) return path.join(xdg, "magpie")
77
- return guess
78
- }
79
- const xdg = process.env.XDG_CACHE_HOME
80
- if (xdg) return path.join(xdg, "magpie")
81
- return path.join(os.homedir(), ".cache", "magpie")
82
- }
83
-
84
- /** 取(或创建)共享队列 */
85
- function queue(configDir) {
86
- if (!globalThis[QUEUE_KEY]) {
87
- globalThis[QUEUE_KEY] = {
88
- /** package → entry,按包名天然去重 */
89
- entries: new Map(),
90
- timer: null,
91
- configDir,
92
- log: () => {},
93
- writes: 0,
94
- }
95
- }
96
- const q = globalThis[QUEUE_KEY]
97
- if (configDir) q.configDir = configDir
98
- return q
99
- }
100
-
101
- /**
102
- * 把一个条目加入队列。
103
- *
104
- * 同一个包名重复入队只保留最后一次 —— 这就是去重。
105
- *
106
- * @param {object} entry { package, name, providers?, community?, summary }
107
- * @param {{configDir?: string, log?: Function, timeoutMs?: number}} [opts]
108
- */
109
- export function registerSelf(entry, opts = {}) {
110
- if (!entry || typeof entry.package !== "string" || entry.package.length === 0) {
111
- return Promise.resolve({ ok: false, reason: "entry.package 缺失" })
112
- }
113
-
114
- const q = queue(opts.configDir)
115
- if (typeof opts.log === "function") q.log = opts.log
116
-
117
- q.entries.set(entry.package, entry)
118
-
119
- if (q.timer) return Promise.resolve({ ok: true, queued: true })
120
-
121
- return new Promise((resolve) => {
122
- q.timer = setTimeout(() => {
123
- q.timer = null
124
- const r = flush(q, opts.timeoutMs)
125
- resolve(r)
126
- }, FLUSH_MS)
127
- if (typeof q.timer.unref === "function") q.timer.unref()
128
- })
129
- }
130
-
131
- /**
132
- * 一次读、一次合并、一次写。
133
- *
134
- * 队列里的所有条目(可能来自多个插件)在这个函数里一起进缓存,
135
- * 所以并发插件之间不存在「你覆盖我」的窗口。
136
- */
137
- function flush(q, timeoutMs = 15_000) {
138
- const log = q.log
139
- if (q.entries.size === 0) return { ok: true, changed: false }
140
-
141
- const cacheDir = cacheDirOf(q.configDir)
142
- const marketPath = path.join(cacheDir, "plugin-market.json")
143
-
144
- try {
145
- let current = null
146
- if (fs.existsSync(marketPath)) {
147
- try {
148
- current = JSON.parse(fs.readFileSync(marketPath, "utf8"))
149
- } catch {
150
- current = null
151
- }
152
- }
153
-
154
- if (!current || !Array.isArray(current.plugins)) {
155
- // 缓存不可用就跳过(不阻塞插件加载)。下次启动还会再试。
156
- const names = [...q.entries.keys()].join(", ")
157
- log(`市场缓存不可用,跳过注册:${names}`)
158
- q.entries.clear()
159
- return { ok: false, reason: "市场缓存不可用" }
160
- }
161
-
162
- const snapshot = JSON.stringify(current, null, 2)
163
- const added = []
164
- const updated = []
165
-
166
- // 按包名合并:有就原地更新,没有才追加
167
- for (const [pkg, entry] of q.entries) {
168
- const idx = current.plugins.findIndex((x) => x?.package === pkg)
169
- if (idx >= 0) {
170
- current.plugins[idx] = entry
171
- updated.push(pkg)
172
- } else {
173
- current.plugins.push(entry)
174
- added.push(pkg)
175
- }
176
- }
177
- q.entries.clear()
178
-
179
- const next = JSON.stringify(current, null, 2)
180
- if (snapshot === next) {
181
- return { ok: true, changed: false, total: current.plugins.length }
182
- }
183
-
184
- fs.mkdirSync(cacheDir, { recursive: true })
185
- const tmp = `${marketPath}.tmp-${process.pid}-${Math.random().toString(36).slice(2, 6)}`
186
- fs.writeFileSync(tmp, next, "utf8")
187
- fs.renameSync(tmp, marketPath)
188
- q.writes++
189
-
190
- const parts = []
191
- if (added.length) parts.push(`新增 ${added.length}`)
192
- if (updated.length) parts.push(`更新 ${updated.length}`)
193
- log(`已注册 ${parts.join(",")}(共 ${current.plugins.length} 条,这是第 ${q.writes} 次写入)`)
194
-
195
- return {
196
- ok: true,
197
- changed: true,
198
- total: current.plugins.length,
199
- added,
200
- updated,
201
- }
202
- } catch (e) {
203
- const msg = e instanceof Error ? e.message : String(e)
204
- log(`自注册失败:${msg}`)
205
- q.entries.clear()
206
- return { ok: false, reason: msg }
207
- }
208
- }
209
-
210
- /**
211
- * 启动自注册。
212
- *
213
- * 一次调用只入队一次。同期的其它插件会在同一个窗口里被合并,
214
- * 一起写进缓存 —— 不管有多少个插件,写入次数都接近 1。
215
- *
216
- * @param {object} entry
217
- * @param {{configDir?: string, log?: Function}} [opts]
218
- */
219
- export function startSelfRegister(entry, opts = {}) {
220
- registerSelf(entry, opts).catch(() => {})
221
- }
222
-
223
- /** 供测试:立刻清空队列(不影响真实使用)*/
224
- export function _flushNow() {
225
- const q = globalThis[QUEUE_KEY]
226
- if (!q) return { ok: true, changed: false }
227
- if (q.timer) {
228
- clearTimeout(q.timer)
229
- q.timer = null
230
- }
231
- return flush(q)
232
- }
233
-
234
- export const _internal = { cacheDirOf, OFFICIAL_REGISTRY, QUEUE_KEY, FLUSH_MS }