@jixing475/magpie-marvis 0.0.0-stage → 0.3.0

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.
Files changed (79) hide show
  1. package/AGENTS.md +255 -0
  2. package/CHANGELOG.md +726 -0
  3. package/CHECKLIST.md +161 -0
  4. package/CONTRIBUTING.md +83 -0
  5. package/GUIDE.md +275 -0
  6. package/HOOKS.md +1333 -0
  7. package/LICENSE +21 -0
  8. package/MULTI-PROVIDER.md +414 -0
  9. package/NOTICE.md +46 -0
  10. package/OFFICIAL-SOURCES.md +142 -0
  11. package/README-TEMPLATE.md +271 -0
  12. package/README.md +300 -3
  13. package/SCORING.md +151 -0
  14. package/UPDATING.md +660 -0
  15. package/examples/split/README.md +66 -0
  16. package/examples/split/index.mjs +75 -0
  17. package/examples/split/src/hooks/config.mjs +18 -0
  18. package/examples/split/src/hooks/models.mjs +49 -0
  19. package/examples/split/src/hooks/usage.mjs +55 -0
  20. package/extract-contract.py +56 -0
  21. package/index.mjs +48 -0
  22. package/package.json +95 -4
  23. package/panel/first-line.css +112 -0
  24. package/panel/first-line.html +37 -0
  25. package/panel/first-line.js +137 -0
  26. package/registry-entry.json +11 -0
  27. package/scripts/check-magpie-stable.mjs +109 -0
  28. package/scripts/check-maintenance.mjs +146 -0
  29. package/scripts/check.mjs +521 -0
  30. package/scripts/cli.mjs +54 -0
  31. package/scripts/contract-diff.mjs +112 -0
  32. package/scripts/create.mjs +270 -0
  33. package/scripts/npm-name.mjs +19 -0
  34. package/scripts/probe-served-model.mjs +172 -0
  35. package/scripts/probe-vision.mjs +173 -0
  36. package/scripts/publish-npm.mjs +44 -0
  37. package/scripts/release.mjs +442 -0
  38. package/scripts/score.mjs +280 -0
  39. package/src/account-name.mjs +145 -0
  40. package/src/auth.mjs +27 -0
  41. package/src/context.mjs +369 -0
  42. package/src/hooks/config.mjs +236 -0
  43. package/src/hooks/loader.mjs +593 -0
  44. package/src/hooks/methods.mjs +398 -0
  45. package/src/hooks/models.mjs +25 -0
  46. package/src/hooks/refresh.mjs +149 -0
  47. package/src/hooks/usage.mjs +117 -0
  48. package/src/marvis/beacon-mac.mjs +213 -0
  49. package/src/marvis/beacon-shim.mjs +143 -0
  50. package/src/marvis/browser.mjs +598 -0
  51. package/src/marvis/desktop-mac.mjs +232 -0
  52. package/src/marvis/desktop.mjs +273 -0
  53. package/src/marvis/device.mjs +85 -0
  54. package/src/marvis/expiry.mjs +60 -0
  55. package/src/marvis/fingerprint.mjs +168 -0
  56. package/src/marvis/images.mjs +230 -0
  57. package/src/marvis/login.mjs +512 -0
  58. package/src/marvis/profile.mjs +113 -0
  59. package/src/marvis/protocol.mjs +160 -0
  60. package/src/marvis/tool-schema.mjs +97 -0
  61. package/src/quota.mjs +212 -0
  62. package/src/refresh-lock.mjs +115 -0
  63. package/tests/device-registry.test.mjs +48 -0
  64. package/tests/e2e-live.test.mjs +120 -0
  65. package/tests/image-input.test.mjs +314 -0
  66. package/tests/legacy-model-config.test.mjs +349 -0
  67. package/tests/legacy-model-loader.test.mjs +306 -0
  68. package/tests/loader-model-support.test.mjs +823 -0
  69. package/tests/manual-paste-login.test.mjs +228 -0
  70. package/tests/model-alias.test.mjs +59 -0
  71. package/tests/model-catalog-support.test.mjs +241 -0
  72. package/tests/mount.test.mjs +259 -0
  73. package/tests/oauth-exchange.test.mjs +362 -0
  74. package/tests/panel-manifest.test.mjs +73 -0
  75. package/tests/plugin-options.test.mjs +100 -0
  76. package/tests/profile.test.mjs +96 -0
  77. package/tests/refresh-contract.test.mjs +318 -0
  78. package/tests/request-identity.test.mjs +18 -0
  79. package/tests/tool-schema.test.mjs +66 -0
package/HOOKS.md ADDED
@@ -0,0 +1,1333 @@
1
+ # magpie 插件钩子清单(权威版)
2
+
3
+ 来源:`magpie-windows-amd64.exe` 内嵌的插件契约源码,用 `extract-contract.py` 提取。
4
+
5
+ **当前基线:`0.1.1327`(稳定渠道 / Stable)**
6
+
7
+ | 日期 | 提取自 | 结果 |
8
+ |---|---|---|
9
+ | 2026-10-26 | 0.1.1254(当时最新) | 首次整理(这一份的主体) |
10
+ | **2026-10-28** | **`0.1.1327`(stable)** | **基线改到稳定渠道**;补「十一之三」(模型条目的 7 个漏字段 + 插件供图) |
11
+ | 2026-10-28 | 0.1.1763(每版本渠道,对照) | 23 段 / 264,822 字符。与 stable 逐字段比对:插件相关的字段**完全一致**;多出的 2 段是 `summaries` / `last seen`(GUI 历史,不是插件契约) |
12
+
13
+ > ⚠⚠ **判断「要不要同步」看稳定渠道,不看每版本渠道。**
14
+ >
15
+ > ```bash
16
+ > curl -s "https://usemagpie.ai/api/latest?channel=stable" | grep -o '"version":"[^"]*"'
17
+ > ```
18
+ >
19
+ > 每版本渠道一晚能发四个版本(实测 1763→1767),追它追不完,而且大多数是 beta。
20
+ > 这就是 magpie 设置里的「更新渠道:每个版本 / 稳定版」——**用户默认跑的是稳定版**,
21
+ > 插件契约要跟的是它。两个渠道的数字差得很夸张(2026-10-28:每版本 0.1.1763 /
22
+ > 稳定版 0.1.1327),但插件相关的字段一条不差 —— 说明「追每版本」几乎全是白跑。
23
+ >
24
+ > 稳定版版本号变了,才需要:重新 `extract-contract.py` → 跑 `node scripts/contract-diff.mjs`
25
+ > → 看差集里有没有「不写会关掉某个能力」的。
26
+
27
+ ⚠ **内嵌契约抓不到的东西**:`draw`(插件供图)、`DrawsID` / `ImagesAPI` 这些判据
28
+ 都在 Go 实现和 `docs/subsystems/provider-plugins.md` 里,不在契约源码里 ——
29
+ 见「十一之三」末尾那段的说明。
30
+
31
+ **这份契约和 `usemagpie.ai/docs/plugins` 分工**:官方文档讲**行为**(哪个钩子、什么时候调、
32
+ 返回值什么意思),这里讲**字段**(少写一个会怎样、写错一个会怎样)。
33
+ `auth.checkin` 曾经是官方文档的缺口,2026-10-26 已在 magpie **v0.1.1582** 补齐
34
+ (en/zh/ja,[issue #392](https://github.com/magpie-community/plugins/issues/392))——
35
+ 所以这里不再声称「比官方全」,只声称「比官方细」。
36
+
37
+ > 一个包装多个供应商(国内 / 国际这种)时,`config` / `auth` / `provider` 三处都要
38
+ > 按站点参数化,而 `auth` 里**不能**再存站点字段 —— 写法、五条铁律与验证清单见
39
+ > [MULTI-PROVIDER.md](./MULTI-PROVIDER.md)。
40
+
41
+ ---
42
+
43
+ ## 一、六个钩子(不是五个)
44
+
45
+ ```js
46
+ export const XxxAuthPlugin = async ({ client }, options) => ({
47
+ // ① 声明 provider 和静态模型表
48
+ config(cfg) {
49
+ cfg.provider ??= {}
50
+ cfg.provider["xxx"] ??= {
51
+ name: "Xxx",
52
+ npm: "@ai-sdk/openai-compatible",
53
+ api: BASE,
54
+ models: staticModels(), // ⚠ 未登录时也必须有,否则显示 "0 models"
55
+ }
56
+ },
57
+
58
+ auth: {
59
+ provider: "xxx",
60
+ icon: "https://...", // 或 data:image/...
61
+ maxConcurrency: 4,
62
+ refreshLead: 5 * 60_000, // token 到期前多久续期
63
+
64
+ // ② 登录方式
65
+ methods: [{
66
+ type: "oauth", // 或 "api"
67
+ label: "Sign in with Xxx",
68
+ placeholder: "...", // api 类型时是输入框提示
69
+ prompts: [], // 可选:登录前的问题
70
+ async authorize() {
71
+ return {
72
+ url, // magpie 打开
73
+ instructions, // 显示给用户
74
+ method: "auto", // "auto" | "code"
75
+ opened: false, // true = 插件自己已经开了页面
76
+ async callback() {
77
+ return { type: "success", refresh, access, expires, accountId }
78
+ },
79
+ }
80
+ },
81
+ }],
82
+
83
+ // ③ 续期
84
+ async refresh(auth, provider) {
85
+ return { access, refresh, expires } // 合并到旧账号上
86
+ },
87
+
88
+ // ④ 请求接管
89
+ async loader(getAuth, provider) {
90
+ return { baseURL, apiKey, headers, fetch }
91
+ },
92
+
93
+ // ⑤ 额度
94
+ async usage(getAuth, provider) {
95
+ return { plan, user, until, renew, balance, error, windows, resets, signIn }
96
+ },
97
+
98
+ // ⑥ 签到(文档漏了这个)
99
+ async checkin(getAuth, provider) {
100
+ return { outcome, credit, streak, message }
101
+ },
102
+ },
103
+
104
+ // ⑦ 每账号的模型目录
105
+ provider: {
106
+ id: "xxx",
107
+ async models(provider, { auth }) { ... },
108
+ },
109
+
110
+ // ⑧ 每个请求加头
111
+ "chat.headers": async (input, output) => {
112
+ output.headers["x-xxx-session"] = input.sessionID
113
+ },
114
+ })
115
+ ```
116
+
117
+ ---
118
+
119
+ ## 二、`auth.checkin` 完整契约
120
+
121
+ ```js
122
+ auth.checkin(getAuth, provider) {
123
+ outcome: "claimed" // 刚签到成功
124
+ | "done" // 今天已经签过了
125
+ | "ineligible" // 这个账号不能参加
126
+ | "inactive" // 没有签到活动
127
+ | "captcha" // 厂商要验证码 —— 让用户去官方 App 手动签,永不解
128
+ | "failed", // 失败
129
+ credit?: number, // 给了多少积分
130
+ streak?: number, // 连续多少天
131
+ message?: string, // 显示给用户的话
132
+ }
133
+ ```
134
+
135
+ ### 调用规则(原文)
136
+
137
+ > magpie asks **once a Beijing day per account** while the user has it on,
138
+ > and **again later that day after a failure**; a hook that throws is a failure.
139
+ > Run in the account's scope, a token it renews is saved to that account.
140
+
141
+ | 规则 | 说明 |
142
+ |---|---|
143
+ | 时间口径 | **北京时间**(不是 UTC,不是太平洋时间)|
144
+ | 频率 | 每账号每天一次 |
145
+ | 重试 | 当天失败后还会再调 |
146
+ | 抛异常 | 等同 `failed` |
147
+ | 作用域 | 在账号的 scope 里,它刷新的 token 会存到该账号 |
148
+
149
+ ### 状态文件
150
+
151
+ ```
152
+ ~/.config/magpie/plugin-checkin.json (通用)
153
+ ~/.config/magpie/workbuddy-checkin.json (CodeBuddy 专用,magpie 内置)
154
+ ```
155
+
156
+ 格式:
157
+ ```json
158
+ {
159
+ "codearts|codearts": {
160
+ "day": "2026-10-25",
161
+ "at": "2026-10-25T17:16:46+08:00",
162
+ "outcome": "done",
163
+ "msg": "今天已领取"
164
+ },
165
+ "qoder-cn|qoder-cn": {
166
+ "day": "2026-10-25",
167
+ "outcome": "claimed",
168
+ "credit": 100,
169
+ "claimed": "2026-10-25T17:16:43+08:00"
170
+ }
171
+ }
172
+ ```
173
+
174
+ ---
175
+
176
+ ## 三、`auth.usage` 完整字段
177
+
178
+ ```js
179
+ {
180
+ plan?: string, // "Pro"
181
+ user?: string, // 厂商那边这个账号的名字
182
+ until?: string | number, // 套餐到期
183
+ renew?: "auto" | "off",
184
+ balance?: string, // "$12.40"
185
+ error?: string, // 替代 windows 显示
186
+
187
+ resets?: {
188
+ count: number,
189
+ until?: string | number,
190
+ byWindow?: boolean,
191
+ fiveHour?: number,
192
+ weekly?: number,
193
+ team?: boolean, // 团队套餐的,不是个人
194
+ each?: [{ // 每个还能用的重置
195
+ until?: string | number, // 无 = 永不过期
196
+ window?: "fiveHour" | "weekly",
197
+ }],
198
+ },
199
+
200
+ windows?: [{
201
+ name: string,
202
+ used: number, // ⚠ 必填。百分比 0-100,超额时 >100
203
+ // 它是**路由排序输入**,不是只给进度条用的:
204
+ // 缺了 magpie 归一成 0 = 「满额」,会把一个
205
+ // 耗尽的账号顶到路由队首(见第十一节)
206
+ resetsAt?: string | number,
207
+ resetSecs?: number,
208
+ display?: string, // "370 / 1000 credits"
209
+ amount?: number,
210
+ limit?: number,
211
+ unit?: string, // "credits"
212
+ span?: number, // 窗口时长(秒)
213
+ model?: string, // 只统计 id 含该词的模型
214
+ models?: string[], // 精确的 id 列表
215
+ notModels?: string[], // 除这些之外
216
+ aside?: boolean, // true: 用光不影响账号
217
+ runsOutAt?: string | number, // 剩余额度什么时候失效
218
+ runsOut?: number, // 失效时损失多少
219
+ }],
220
+
221
+ signIn?: "expired" | "kept" | "renewed",
222
+ }
223
+ ```
224
+
225
+ **时间可以是 ISO 字符串、秒、毫秒。**
226
+
227
+ ### ⚠⚠ 三条「写错不报错」的窗口规矩
228
+
229
+ 这三条都是**静默失败**:钩子不抛异常、magpie 也不报错,只是卡片悄悄变成
230
+ 「剩余额度未知」,或者算出一个没有意义的百分比。实测撞过,代价是整个额度区报废。
231
+
232
+ #### ① `aside: true` 的窗口不进路由 —— 但「丢占比」的元凶是**没有 `used`**,不是 `aside`
233
+
234
+ > ⚠ 2026-10-29 更正(issue [#468](https://github.com/magpie-community/plugins/issues/468),magpie v0.1.1934):
235
+ > 判断一条窗口「有没有占比」**只看 `used`**。没有 `used` 的窗口是 `Text`,路由不计入 ——
236
+ > 与 `aside` 无关;而且 magpie ≥ `1390aa458` 起,有 `amount` + `limit` 却没有 `used` 的窗口会用
237
+ > `amount / limit` 推算占比。本文原来把两件事混成了一件,下面按更正后的机制重写。
238
+
239
+ 这条最贵,因为它有两个后果,而只有第一个显眼。
240
+
241
+ 先看**显示层**的坑:`aside` 的含义是「用光也不影响账号」,它本来就没有上限、没有周期。
242
+ 给了 `amount`/`limit`,magpie 会把它当成**一条真额度去算占比**:
243
+
244
+ ```js
245
+ quotaCount(w){ if (!(w.limit > 0)) return w.display || ""; … }
246
+ ```
247
+
248
+ 而「总量 = 当前余额」这种假总量,算出来永远是「剩余 100%」——数字看着漂亮,
249
+ 含义是错的(这也是市场现有插件被诟病的点)。
250
+
251
+ ```js
252
+ // ❌ 假占比:分母就是"剩余"本身,永远 100%
253
+ { name: "积分余额", used: 0, amount: 0, limit: 6291, aside: true }
254
+
255
+ // ✅ 只想展示一行文字(这个号还有别的窗口参与路由):只给 display
256
+ { name: "积分余额", used: 0, aside: true, display: "6,291 积分", unit: "积分" }
257
+ ```
258
+
259
+ **再看路由层的坑(2026-10-28 真机踩中,代价是整个号在路由里消失):**
260
+
261
+ `aside: true` 的窗口,和 `used` 缺省后被判成的 `Text` 窗口,**都会**被 `allowanceOf()`
262
+ 整条跳过(`internal/provider/routing.go`)—— 两个条件独立,与有没有 `amount`/`limit` 无关:
263
+
264
+ ```go
265
+ for _, w := range ws {
266
+ if w.Aside || w.Text { continue } // ← 整条跳过
267
+ ...
268
+ }
269
+ if len(a) == 0 { return nil, false } // ← 一条不剩 ⇒ 这个号 Known = false
270
+ ```
271
+
272
+ ⇒ **一个账号的窗口全标了 `aside`,它在路由页就是「剩余额度未知」,
273
+ 而且一律排在已知账号之后。** 这不是「不参与排序」,是「这个号在路由里不存在」。
274
+
275
+ **官方文档逐字(`docs/subsystems/provider-plugins.md`,issue #3650):**
276
+ > An account whose windows all tell no share has no allowance, so routing counts it
277
+ > not known ... as one never read, rather than as the one with all of it left.
278
+ > A window that says `used: 0` is a reading at 0, as before.
279
+
280
+ **所以两种用法,别搞混:**
281
+
282
+ | 目的 | 写法 |
283
+ |---|---|
284
+ | 一行文字要展示,**且这个号还有别的窗口参与路由** | `aside: true` + `display` ✅(**别给 `amount`/`limit`**:分母是余额本身,永远「剩余 100%」) |
285
+ | 这个号**只有**这一条额度信息 | **不要标 `aside`**:给 `used` + `limit`(哪怕百分比没有信息量),否则路由里没有这个号 |
286
+
287
+ **判据:** `curl -s http://127.0.0.1:3425/v1/magpie/trace`,看这个 provider 的
288
+ `known` 是 `true` 还是**字段缺失**。缓存侧看 `~/.config/magpie/quotas.json` 的
289
+ `spans`:`[{}]` = 没标,`[{"aside":true}]` = 还标着。
290
+
291
+ #### ② `resetsAt` / `runsOutAt` 必须是 **ISO 字符串或数字**
292
+
293
+ 厂商接口常回 `"2026-10-28 23:59:59"`(空格分隔、无时区)。**直接塞进去,
294
+ magpie 解析不了,整条窗口会被跳过** —— 表现是卡片「剩余额度未知」,而
295
+ `usage()` 本身返回完全正常(有 plan、有 windows、没有 error),极难查。
296
+
297
+ ```js
298
+ // ❌ 服务端原样格式
299
+ { resetsAt: "2026-10-28 23:59:59" }
300
+
301
+ // ✅ 自己按本机时区拆字段再构造(别指望 new Date(str) 跨引擎一致)
302
+ { resetsAt: new Date(2026, 9, 28, 23, 59, 59).toISOString() }
303
+ ```
304
+
305
+ #### ③ 有「哪批额度先作废」的信息时,用 `runsOutAt` + `runsOut`,别拼进 `display`
306
+
307
+ `display` 是给「已用 / 总量」那种数字用的。把「185 分今天到期」拼进去,
308
+ magpie 只当普通文字显示,不会当作失效提醒处理。这两个字段专为这件事准备:
309
+
310
+ ```js
311
+ { name: "积分余额", used: 0, aside: true, display: "6,291 积分",
312
+ runsOutAt: "2026-10-28T15:59:59.000Z", runsOut: 185 } // 最早作废的那批
313
+ ```
314
+
315
+ **判据**(三分钟自查):
316
+
317
+ ```bash
318
+ curl -s http://127.0.0.1:3425/v1/magpie/quotas
319
+ ```
320
+
321
+ 看这个 provider 的 `windows[0]`:
322
+ - 有 `display` 且 `text: true` ⇒ aside 窗口被正确识别成文本
323
+ - 有 `runsOutAt`(ISO 形式,如 `2026-10-28T15:59:59Z`)⇒ 失效提醒生效
324
+ - 卡片显示「剩余额度未知」⇒ 八成是 ② 的时间格式,或窗口整个不合法
325
+
326
+ ---
327
+
328
+ ## 四、登录的陷阱
329
+
330
+ ### 陷阱 1:`authorize()` 里不能等用户
331
+
332
+ ```js
333
+ // ❌ 死锁
334
+ async authorize() {
335
+ const token = await waitForUserToLogin() // 这里等 = magpie 双向死锁
336
+ return { type: "success", access: token }
337
+ }
338
+
339
+ // ✅
340
+ async authorize() {
341
+ const flow = startLoginFlow()
342
+ return {
343
+ url: flow.url,
344
+ method: "auto",
345
+ callback: async () => { // 真正等登录的是这里
346
+ const token = await flow.result
347
+ return { type: "success", access: token }
348
+ },
349
+ }
350
+ }
351
+ ```
352
+
353
+ ### 陷阱 2:`inputs` 只在有 `prompts` 时传入
354
+
355
+ > the inputs go only to a method that asks something... an object with none
356
+ > is how its CLI calls, and a plugin told so asks its questions on the terminal
357
+
358
+ ```js
359
+ // 没有 prompts → 收到 undefined
360
+ // 有 prompts → 收到 { region: "cn", team: "t-xxx" }
361
+ ```
362
+
363
+ ### 陷阱 3:`opened: true` 防止开两次页面
364
+
365
+ ```js
366
+ // Grok Build 的 `grok login` 在 macOS/Windows 上自己开了页面
367
+ // magpie 再开一次 → 用户看到两个一样的窗口
368
+ return { url, opened: true }
369
+ ```
370
+
371
+ ### 陷阱 4:`single: true` 只允许一个账号
372
+
373
+ ```js
374
+ auth: {
375
+ single: true, // 再登录会替换而不是新增
376
+ }
377
+ ```
378
+
379
+ ### 陷阱 5:多步问答必须用 `ask()`,不是 `prompts` 数组
380
+
381
+ `prompts` 是**静态**的:magpie 读它列问题,一次问完。要让「邀请码 → 主登录的问题」串起来,
382
+ method 上还要再加一个 `ask(inputs)`,每次返回**下一个**问题,问完返回 `null`:
383
+
384
+ ```js
385
+ {
386
+ type: "oauth",
387
+ label: "… 邀请码",
388
+ get prompts() { return [invitePrompt()] }, // 面板列表要靠它
389
+ async ask(inputs) {
390
+ if (!hasOwn(inputs, "invite")) return invitePrompt()
391
+ if (!hasOwn(inputs, "region")) return regionPrompt
392
+ return null // 问完了
393
+ },
394
+ async authorize(inputs) {
395
+ saveInvite(inputs.invite) // 保存策略
396
+ return await mainMethod.authorize(inputs) // 直接接主登录
397
+ },
398
+ }
399
+ ```
400
+
401
+ 契约:`internal/plugin/providers.go` 的 `NextPrompt()` → 插件 `prompt` 钩子 → `method.ask(inputs)`。
402
+ CLI 与 GUI 都走它(`internal/cli/plugins_cli.go:453`、`internal/gui/plugins.go:746`)。
403
+
404
+ ⚠ **同时保留 `get prompts()`**:只给 `ask` 不给 `prompts`,那条登录方式在列表里看起来像什么都不问。
405
+ ⚠ `authorize` 里**复用主登录的 authorize**,不要复制一份 callback —— 复制必然只改一边。
406
+ ⚠ 判断「问过没有」用 `Object.prototype.hasOwnProperty.call(inputs, key)`,
407
+ 不要用 `inputs[key] !== undefined`:用户填了空字符串也是「问过了」。
408
+
409
+ ---
410
+
411
+ ### 陷阱 6:`authorize()` 的 `account` 恒为 `new` —— 插件拿不到是哪张卡
412
+
413
+ 宿主的 `authorize` 命令确实带 `account` 参数(`host.js` 的
414
+ `authorize({ provider, method, inputs, account, proxy })`,由 `signInKey()` 决定写哪个
415
+ key),但 **magpie 自己从不传账号键**:
416
+
417
+ - GUI 的「Sign in again」/「Add account」→ `internal/provider/pluginsignin.go:121`
418
+ `plugin.Authorize(ctx, id, method, inputs, plugin.NewAccount)`
419
+ - CLI `magpie plugin login` → `internal/cli/plugins_cli.go:492`,同样 `plugin.NewAccount`
420
+ - 账号名只作为 `SignInOptions.User` 到 Go 侧,唯一用途是 `pluginUsing(saved, user)`
421
+ 判断「登录的是不是正在用的那个账号」,**不会进插件**
422
+
423
+ `signInKey(provider, "new")` 走 `freshKey(provider)`,`inScope(provider, 新 key)` 里调
424
+ `m.authorize(...)`;插件客户端也没有 `auth.get`(只有 `set`/`remove`),`getAuth` 只传给
425
+ `loader` / `models` / `usage` / `checkin`。
426
+
427
+ ⇒ **不要在 `authorize()` 里假设能知道用户点的是哪张卡**。要「按账号」的入口只能用
428
+ `usage().signIn = "expired"` 借 magpie 自带的「Sign in again」按钮(它同样拿不到账号,
429
+ 只能按 provider 级状态判断),或在登录说明/落地页里放一个通用入口。
430
+
431
+ (2026-10-30 核对:本地 v0.1.1775 源码与安装版 0.1.2188 内嵌 `host.js`/`app.js` 一致。)
432
+
433
+ ---
434
+
435
+ ## 四之二、登录的三种形态(决定弹不弹浏览器)
436
+
437
+ | 形态 | 形状 | 弹窗口? | 前提 | 实测结论 |
438
+ |---|---|---|---|---|
439
+ | **A 浏览器 OAuth** | `{ url, method: "auto", callback }`,callback 里等 loopback 回来 | **会** | 无 | 最通用;厂商的登录页、滑块、验证码都交给厂商自己 |
440
+ | **B 本机客户端导入** | `{ url, opened: true, method: "auto" }` + callback 里读客户端文件 | **不会** | 用户已装并登录过官方客户端 | 最省事;见 [`src/desktop-import.mjs`](./src/desktop-import.mjs)(DPAPI + AES-GCM) |
441
+ | **C 内置验证码(headless)** | 后台起 headless 浏览器走登录页,`method: "code"` 让用户只填验证码 | **不会** | 本机有 Chromium 内核浏览器 | **不建议默认提供**:滑块/风控/限流都要自己扛,实测同一出口 IP 连续发码会被静默拦下(页面停在发送页、不报错) |
442
+
443
+ **选择顺序**:B(如果有客户端)→ A(默认)→ C(只在 B 不可用且厂商没风控时)。
444
+
445
+ `auth.methods` 里可以同时给出 A 和 B:B 的 `opened: true` 让 magpie 不再开页面,
446
+ 整个登录就是「点一下 → 读文件 → 成功」。
447
+
448
+ ---
449
+
450
+ ## 五、`auth.refresh` 的语义
451
+
452
+ ```js
453
+ async refresh(auth, provider) {
454
+ // 返回值 **合并** 到旧账号上(与 client.auth.set 的整体替换不同)
455
+ return { access, refresh: newRefresh ?? auth.refresh, expires }
456
+ }
457
+ ```
458
+
459
+ ### 运行时机
460
+
461
+ - OAuth 账号的 `expires` 距现在 < `refreshLead` 时
462
+ - 在 loader / provider.models / auth.usage / 请求 **之前**
463
+
464
+ ### 串行化
465
+
466
+ > Requests that find the token due wait for one renewal, so a vendor that
467
+ > spends a refresh token once never sees it twice. They wait up to 15 seconds.
468
+
469
+ **一次性 refresh_token 的厂商(如 CodeBuddy)不会被并发用两次。**
470
+
471
+ ### 失败处理
472
+
473
+ | 情况 | magpie 行为 |
474
+ |---|---|
475
+ | `throw Object.assign(e, { signIn: "expired" })` | 标记账号,不再重试直到重新登录 |
476
+ | 其它错误 | 30 秒后重试 |
477
+ | 返回 `undefined` | 保持原样,不续期 |
478
+
479
+ ### 不续期的情况
480
+
481
+ > An account with `expires` 0 or none is never renewed.
482
+
483
+ **`expires` 为 0 或不存在的账号永不续期。**
484
+
485
+ ---
486
+
487
+ ## 六、`loader` 返回的四个字段
488
+
489
+ ```js
490
+ {
491
+ baseURL, // 请求去哪(优先于模型的 api 和 provider 的 api)
492
+ apiKey, // 按 npm 包自动加头:见下表
493
+ headers, // 每个请求都加
494
+ fetch, // 真正的请求函数
495
+ }
496
+ ```
497
+
498
+ ### apiKey 的发送方式(magpie 自动处理)
499
+
500
+ ```js
501
+ // 契约源码原文
502
+ function sdkHeaders(npm, key) {
503
+ if (!key) return {}
504
+ if (npm === "@ai-sdk/anthropic" || npm === "@ai-sdk/google-vertex/anthropic")
505
+ return { "x-api-key": key }
506
+ if (npm === "@ai-sdk/google")
507
+ return { "x-goog-api-key": key }
508
+ return { authorization: `Bearer ${key}` }
509
+ }
510
+ ```
511
+
512
+ ### 请求头的设置顺序(后者覆盖前者)
513
+
514
+ ```
515
+ 1. sdkHeaders(apiKey)
516
+ 2. loader.headers
517
+ 3. gateway 的请求头
518
+ 4. chat.headers 钩子加的
519
+ ```
520
+
521
+ ### `fetch(input, init)` 收到什么
522
+
523
+ - `input`:完整 URL
524
+ - `init.method`:通常 POST
525
+ - `init.body`:该 API 形状的 JSON 字符串(非 UTF-8 时是 bytes)
526
+ - `init.signal`:agent 放弃时 abort
527
+
528
+ **必须返回 Response。** 状态码、头、body 原样透传。
529
+
530
+ **改写流式 body 时要去掉 `content-length` 和 `content-encoding`。**
531
+
532
+ ---
533
+
534
+ ## 七、失败处理的层次
535
+
536
+ | 情况 | 做法 | magpie 的行为 |
537
+ |---|---|---|
538
+ | 模型不存在 | 返回 **404** + 错误 JSON | 只搁置**这个模型** |
539
+ | 登录被拒 | 401 或 `signIn: "expired"` | 标记**这个账号** |
540
+ | 这个 401 是别的事 | 响应头 `X-Magpie-Sign-In: kept` | 不标记 |
541
+ | 已续期 | 响应头 `X-Magpie-Sign-In: renewed` | 清除标记 |
542
+ | 限流 / 5xx | 原样透传 | 自动换账号或模型 |
543
+ | 插件内部错误 | 抛异常 | 请求失败,故障转移继续 |
544
+
545
+ **尽量用该 API 自己的错误形状回答**,agent 才能显示供应商的原始消息。
546
+
547
+ ---
548
+
549
+ ## 八、账号标识(决定卡片显示什么)
550
+
551
+ ### 三级回退(契约源码 `whoOf`)
552
+
553
+ ```js
554
+ function whoOf(a) {
555
+ return a?.accountId ?? a?.metadata?.email ?? a?.email ?? ""
556
+ }
557
+ ```
558
+
559
+ **登录成功时返回的字段决定卡片显示:**
560
+
561
+ ```js
562
+ return {
563
+ type: "success",
564
+ refresh, access, expires,
565
+ accountId: "user@example.com", // ← 首选
566
+ uid: "0d1606bf-...", // ← 区分同名账号
567
+ }
568
+ ```
569
+
570
+ | 字段 | 作用 |
571
+ |---|---|
572
+ | `accountId` | 卡片显示的账号名 |
573
+ | `metadata.email` | 次选 |
574
+ | `email` | 再次选 |
575
+ | `uid` | 厂商自己的 id,区分同名账号(issue #413)|
576
+
577
+ ### `accountId` 是名字,`uid` 是身份 —— 别拿内部 ID 当名字
578
+
579
+ `accountId` 只放**人扫一眼就能分辨**的东西:打码手机号(`189****8051`)、邮箱、昵称。
580
+ 厂商的内部 ID(`iam::<domain>:user:<name>`、`urn:…`、uuid、裸 hash、华为的 `hw0615…`)
581
+ 不是名字,它们进 `uid`。
582
+
583
+ **写错的代价是永久性的**:`auth.refresh` 返回的字段会**合并**进旧记录,而插件自己的
584
+ 落盘(`persistCredential`)通常又会优先保留已有的 `accountId` —— URN 进去一次,卡片就再也
585
+ 变不回来。用户看到的是三行 `iam::01a1642a…:user:hw0615…`,只会以为「插件显示乱码」。
586
+
587
+ ```js
588
+ import { resolveAccount } from "./src/account-name.mjs"
589
+
590
+ // 登录成功:名字给人看,身份给宿主去重
591
+ const { phone, name, urn } = await readWhoSignedIn(cred) // 你的接口
592
+ return { type: "success", refresh, access, expires, ...resolveAccount({ phone, name, urn }) }
593
+ // → { accountId: "189****8051", uid: "iam::01a1642ad2ae7f93bf6387ad2fc5f09e:user:hw00000000000000" }
594
+ ```
595
+
596
+ 三条判据:
597
+
598
+ 1. 手机号一律**打码**(`maskPhone`:`0086-…` / `+86 …` / 裸号 / 已打码的值都认)。
599
+ 2. 像内部 ID 的值**不当名字**(`looksLikeIdentifier`:含 `::`、`urn:`、uuid、裸 hash、`hw…`)。
600
+ 名字全取不到就**不给 `accountId`**,让宿主回退供应商名 —— **绝不拿 URN 顶上**。
601
+ 3. **老记录要能改**:续期时把 `accountId` 重新算一遍,并在 `refresh` 的返回值里带上;
602
+ magpie 会用它覆盖旧记录(`host.js`:`if ("refresh" in result) setAuth(key, { …, ...extra })`)。
603
+ 只改登录路径的话,已登录的账号会永远停在旧名字上。
604
+
605
+ > 真实事故:CodeArts 0.2.4 从 refresh token 的 `user_profile` 取 `principal_urn` 当 `accountId`,
606
+ > 三个账号显示成三串 ID;0.2.5 改成登录后读一次华为云 IAM 的手机号(`GET /v3.0/OS-USER/users/
607
+ > {principal_id}` → `user.phone`),打码成 `189****8051`,URN 改走 `uid`。
608
+
609
+ ### 没有 accountId 时
610
+
611
+ ```js
612
+ // hintOf tells an account with no id from another: the end of its key.
613
+ function hintOf(a) {
614
+ return a?.type === "api" && typeof a.key === "string" && a.key.length >= 12
615
+ ? a.key.slice(-4) : ""
616
+ }
617
+ ```
618
+
619
+ **API key 类型没有 accountId → 卡片显示 key 的最后 4 位。**
620
+
621
+ ### 同名账号自动合并(`settle`)
622
+
623
+ ```js
624
+ // settle keeps a sign-in just saved at key once: one to an account already
625
+ // signed in (the same account id and, where both have one, the same uid,
626
+ // else the same secret) replaces that one's and goes.
627
+ ```
628
+
629
+ 判定顺序:
630
+ 1. `accountId` 相同 **且** `uid` 相同(两边都有 uid 时)→ 替换旧账号
631
+ 2. 都没有 accountId,但 secret 相同 → 替换
632
+ 3. 否则 → 新增账号
633
+
634
+ **所以重复登录同一账号不会产生重复条目。**
635
+
636
+ ### 账号在存储里的 key
637
+
638
+ ```js
639
+ // accountsOf are the keys provider's accounts are kept under, the one under
640
+ // its own id first.
641
+ function accountsOf(all, provider) {
642
+ return Object.keys(all)
643
+ .filter((k) => k === provider || k.startsWith(provider + "#"))
644
+ .sort(...)
645
+ }
646
+ ```
647
+
648
+ ```
649
+ 第一个账号 → provider
650
+ 第二个账号 → provider#a1b2c3
651
+ 第三个账号 → provider#d4e5f6
652
+ ```
653
+
654
+ ---
655
+
656
+ ## 九、图标与并发(两级回退)
657
+
658
+ ### 图标(契约源码 `iconOf`)
659
+
660
+ ```js
661
+ function iconOf(a) {
662
+ const ok = (v) => {
663
+ const s = typeof v === "string" ? v.trim() : ""
664
+ // data URI 超过 1MB 不携带
665
+ return s.length <= 3 << 19 && /^(https:\/\/|data:image\/)/i.test(s) ? s : ""
666
+ }
667
+ // ① auth 钩子的 icon 优先
668
+ const own = ok(a.auth.icon)
669
+ if (own || !a.target) return own
670
+ // ② 否则读 package.json 的 magpie.icon
671
+ return ok(readJSON(path.join(dir, "package.json"))?.magpie?.icon)
672
+ }
673
+ ```
674
+
675
+ **优先级:`auth.icon` > `package.json` 的 `magpie.icon`**
676
+
677
+ **约束:**
678
+ - 必须是 `https://` 或 `data:image/` 开头
679
+ - 大小 ≤ 1 MB(`3 << 19` = 1,572,864 字节)
680
+ - SVG、PNG 都行
681
+
682
+ ### 并发(契约源码 `concurrencyOf`)
683
+
684
+ ```js
685
+ function concurrencyOf(a) {
686
+ const ok = (v) => (Number.isInteger(v) && v > 0 ? Math.min(v, 1000) : 0)
687
+ const own = ok(a.auth.maxConcurrency)
688
+ if (own || !a.target) return own
689
+ return ok(readJSON(path.join(dir, "package.json"))?.magpie?.maxConcurrency)
690
+ }
691
+ ```
692
+
693
+ **优先级:`auth.maxConcurrency` > `package.json` 的 `magpie.maxConcurrency`**
694
+
695
+ **范围:1-1000 的整数。** 用户自己设的会覆盖它。
696
+
697
+ ### 插件自己的图标:`magpie.icon` —— 和上面那个 `iconOf` 不是一回事
698
+
699
+ 上面讲的 `iconOf` 管的是**供应商卡片**。**插件卡片**(插件列表那一行、插件详情页左上角)
700
+ 走的是另一条路,源码 `internal/plugin/market.go`:
701
+
702
+ ```go
703
+ // InstalledIcon is the picture the installed plugin spec's package.json
704
+ // gives in magpie.icon, as ownIcon takes one: the plugin's own, for its
705
+ // row, apart from those its providers give themselves in the auth hook (#1775).
706
+ func InstalledIcon(spec string) string {
707
+ b, _ := os.ReadFile(filepath.Join(Target(spec), "package.json"))
708
+ var p struct {
709
+ Magpie struct {
710
+ Icon string `json:"icon"`
711
+ } `json:"magpie"`
712
+ }
713
+ // ...
714
+ }
715
+ ```
716
+
717
+ ⚠ **`auth.icon` 到不了插件卡片。** 只写 `auth.icon` 的后果:
718
+
719
+ | 位置 | 有图标吗 |
720
+ |---|---|
721
+ | 供应商列表那一行 | ✓ |
722
+ | 用量概览的卡 | ✓ |
723
+ | **插件列表那一行** | ✗ 灰方块(回退到默认图) |
724
+ | **插件详情页左上角** | ✗ 灰方块 |
725
+
726
+ ⇒ **两个都要写。** 内容一样就抄两份:
727
+
728
+ ```json
729
+ {
730
+ "name": "Magicrew",
731
+ "magpie": {
732
+ "icon": "data:image/png;base64,iVBORw0KGgo...",
733
+ "maxConcurrency": 4
734
+ }
735
+ }
736
+ ```
737
+
738
+ 同一份 data URI 也放进 `auth.icon`,两处各一份。
739
+
740
+ ## 九之三、卡片上的徽章从哪来
741
+
742
+ 插件行上除了名字和版本,还会出现几类 chip。哪些插件能给自己挣到,哪些挣不到:
743
+
744
+ | 徽章 | 数据源 | 插件能控制吗 |
745
+ |---|---|---|
746
+ | `已是最新` / `有新提交` / `v{x} out` | npm 版本、git HEAD,magpie 自己比 | 间接(发版即可) |
747
+ | `已自动更新` | magpie 自己更新过 | — |
748
+ | `供应商` / `Request hook` / `中间件` / `页面` | 插件导出里带了什么钩子 | ✓ 有钩子就有 |
749
+ | **`承载 {names}`** | `moved`(内置订阅迁到插件的)+ `serves`(**它注册的 provider 名**) | ✓ **就是你的供应商名字**(magpie ≥ 0.1.1744) |
750
+
751
+ ### `承载` 现在由宿主派生(v0.1.1744 起)
752
+
753
+ `766356c` 之后(issue [#3902](https://github.com/yetone/magpie/issues/3902)),
754
+ `internal/gui/plugins.go` 在 `moved` 之外再填一份 `serves`:
755
+
756
+ ```go
757
+ j.Moved = provider.MovedOnto(e.Spec) // 内置订阅迁过来的
758
+ j.Serves = servesBeside(j.Moved, provs[e.Spec]) // 它自己注册的 provider 名
759
+ ```
760
+
761
+ `servesBeside` 的规矩(`internal/gui/plugins.go`):
762
+
763
+ - 取每个注册 provider 的 `name`(空则退回 `id`),每个一次,大小写不同算重名;
764
+ - `moved` 里的 id 跳过 —— 页面已经按内置名画了,不重复;
765
+ - **`moved` 为空且只注册了一个 provider ⇒ 返回 nil**,不画徽章(行标题已经说了);
766
+ - 前端把 `moved` 的名字和 `serves` 拼成一个 chip:
767
+ `承载 {moved 的名字 + serves}`;tooltip「… 正由此插件代替内置运行」**只在 `moved` 非空时**才挂。
768
+
769
+ 所以第三方插件现在**能**挣到这个徽章了 —— 徽章内容就是你的供应商名:
770
+
771
+ | 你的包 | 卡片 |
772
+ |---|---|
773
+ | 一个 provider | 无徽章 |
774
+ | 两个 provider,品牌名不同 | `承载 超级麦吉、MagiCrew` |
775
+ | 两个 provider,共用一个品牌名 | `承载 Qoder 国际版 (qoder.com)、Qoder CN`(名字里得带站点,magpie 内置名就这么写) |
776
+
777
+ > ⚠ 曾经草案里有个 `magpie.carries` 字段(#3902 的 option 1),从未随任何 magpie 发布:
778
+ > 上游选了派生。写了不报错,只是没人读;`scripts/check.mjs` 会提醒删掉。
779
+ > 插件关闭时(`off`)不画这个 chip,和别的 chip 一样。
780
+
781
+
782
+ ---
783
+
784
+ ## 九之二、插件名(卡片标题显示什么)
785
+
786
+ ### 先把三个「名字」分清
787
+
788
+ 这张表把最常混的三件事钉死:
789
+
790
+ | 你写的 | 在哪 | 显示在哪 |
791
+ |---|---|---|
792
+ | `package.json` 的 `name` | 包根 | **插件卡片标题** —— 插件列表那一行、详情页标题。链路:bun 按它写 `plugins/package.json` 的依赖键 → magpie 反查键名当标题 |
793
+ | `config.provider[id].name` | 插件代码 | **供应商列表那一行**(`magicrew (超级麦吉, 27 models)` 里的「超级麦吉」) |
794
+ | `registry-entry.json` 的 `name` | 仓库根(提交给 registry) | 市场条目里的显示名 —— **不是卡片标题** |
795
+ | 仓库名 | GitHub | 只进链接(`repository`、源码按钮),不参与任何显示 |
796
+
797
+ 拿 `magpie-magicrew-auth` 举例:
798
+
799
+ ```
800
+ package.json name: "Magicrew" → 插件卡片标题「Magicrew」
801
+ config.provider name: "超级麦吉" → 供应商列表「超级麦吉」
802
+ name: "MagiCrew" → 供应商列表「MagiCrew」
803
+ 仓库名 magpie-magicrew-auth → 只出现在「源码」按钮的链接里
804
+ ```
805
+
806
+ ⚠ **「插件叫什么」和「供应商叫什么」是两件事。** 一个包两个供应商时,卡片标题只有一个
807
+ (包的 `name`),供应商名有两个。把站点名写进包的 `name` 会让卡片变成
808
+ 「超级麦吉 / MagiCrew」这种缝合物。
809
+
810
+
811
+ ### 结论先说:插件名**没有** `displayName`
812
+
813
+ magpie 不读 `package.json` 的 `name`、也不读任何自定义显示字段来当卡片标题。标题来自
814
+ `plugin.Name()`(`internal/plugin/store.go`):
815
+
816
+ ```go
817
+ func Name(spec string) string {
818
+ if IsPath(spec) { return spec } // 路径安装 → 路径
819
+ if IsGit(spec) { // git 安装
820
+ if n := gitName(spec); n != "" { return n } // → dependencies 的键名
821
+ return spec // → spec 原文
822
+ }
823
+ at := strings.LastIndex(spec, "@") // npm 安装 → 去掉 @version
824
+ if at > 0 { return spec[:at] }
825
+ return spec
826
+ }
827
+ ```
828
+
829
+ | 安装方式 | 卡片标题 |
830
+ |---|---|
831
+ | `magpie plugin add ./my-plugin` | 那个路径 |
832
+ | `magpie plugin add github:owner/repo` | **`plugins/package.json` 里 dependencies 的键名** |
833
+ | `magpie plugin add my-npm-package` | 包名(去掉 `@version`) |
834
+
835
+ ### git 安装:名字 = dependencies 的键名
836
+
837
+ `gitName()` 的实现就是反查:
838
+
839
+ ```go
840
+ for name, s := range pj.Dependencies {
841
+ if s == spec { return name } // ← 键名就是显示名
842
+ }
843
+ ```
844
+
845
+ **键名同时是 `node_modules` 下的目录名**,所以改名必须三处同步,漏一处就加载失败。
846
+
847
+ ### 改名的完整步骤(实测)
848
+
849
+ 以 `github:owner/repo` 改叫 `Loomy` 为例:
850
+
851
+ ```
852
+ ~/.config/magpie/plugins/
853
+ ├── package.json ┐
854
+ ├── bun.lock ├─ 三处都要把旧键改成新名
855
+ └── node_modules/ ┘
856
+ └── Loomy/ ← 目录名必须跟着改
857
+ ```
858
+
859
+ ```bash
860
+ cd ~/.config/magpie/plugins
861
+
862
+ # 1. package.json:把键改成想显示的名字
863
+ # "magpie-plugin-loomy-auth": "github:owner/repo"
864
+ # → "Loomy": "github:owner/repo"
865
+
866
+ # 2. bun.lock:同样换键(两处:workspaces.dependencies 和 packages)
867
+ # "magpie-plugin-loomy-auth": "github:..." → "Loomy": "github:..."
868
+ # "magpie-plugin-loomy-auth": [...] → "Loomy": [...]
869
+
870
+ # 3. node_modules 目录改名
871
+ mv node_modules/magpie-plugin-loomy-auth node_modules/Loomy
872
+ ```
873
+
874
+ 然后重启 magpie。
875
+
876
+ **只改 1、2 不改 3 会报:**
877
+
878
+ ```
879
+ plugin github:owner/repo didn't load:
880
+ Error: no such plugin: ...\plugins\node_modules\Loomy
881
+ ```
882
+
883
+ **已经装好之后改名,不需要重新下载。** 三处键名一致、目录存在即可。
884
+
885
+ ### 陷阱
886
+
887
+ ⚠ **不要试图靠改仓库名来改名。** 仓库名不影响显示(git 安装看的是 dependencies 键),
888
+ 改仓库名只会让所有已安装用户的 spec 失效。
889
+
890
+ ⚠ **`magpie plugin add` 会覆盖你手改的键名。** 每次 `add` / `update` 都由 bun 重新写
891
+ `package.json`,键名回到包自己的 `package.json` 里那个 `name`。所以**正路是改包内的
892
+ `name`** —— 改完装出来就是那个名字,再也不用管(实测见 [UPDATING.md](./UPDATING.md));
893
+ 不想改 `name` 才退回去手改,接受每次更新后重改。
894
+
895
+ ⚠ **npm 安装无法改名。** 标题就是包名,只能通过改包名(重新发布)实现。
896
+
897
+ ### 卡片上的其他文字来自哪
898
+
899
+ | 位置 | 来源 |
900
+ |---|---|
901
+ | 标题 | 上面说的 `plugin.Name()` |
902
+ | 标题下一行 | 安装 spec(`github:owner/repo` 或包名) |
903
+ | 「版本」 | 已装包自己的 `package.json` 的 `version` |
904
+ | 「版本」后面那串 | **只有 git 源有**:已装那份代码的 **commit 短 sha**(`plugin.ShortCommit`)|
905
+ | 「更新于」 | npm registry 的发布时间,或 git commit 的时间 |
906
+ | 「npm ↗ / 源码 ↗」按钮 | npm 安装显示 npm,git 安装显示源码(`package.json` 的 `repository` 决定链接) |
907
+ | 介绍正文 | **README.md 的内容**(见第十章) |
908
+
909
+ **为什么 git 源要补一个 commit。** 那边「版本」不是可靠的标识 —— 同一个 `0.1.14`
910
+ 可以对应任意多个 commit(改了代码、不发 npm,包内 `version` 就不会动)。所以 magpie
911
+ 把短 sha 跟在版本号后面,那一串才是「你装的到底是哪份代码」:
912
+
913
+ ```
914
+ Loomy 0.1.14 c2aacc2 ← 版本号 + 本机装的那次 commit
915
+ ```
916
+
917
+ 短 sha **从哪读**(`internal/plugin/gitrev.go` 的 `GitCommit()`,按顺序):
918
+
919
+ 1. `node_modules/<目录名>/.bun-tag` —— bun 写的,形如 `MYCF711-magpie-loomy-auth-c2aacc2`,取最后一段
920
+ 2. 否则读 `plugins/bun.lock` 的 `"<名>": ["<名>@…#<sha>"…]`
921
+
922
+ ⚠ 第 2 条的正则**要求键名与值首段同名**。改过别名的依赖
923
+ (`"OfficeAce": ["officeace-auth@…#…"]`)在那里**匹配不上** —— 所以现役路径其实是
924
+ `.bun-tag`;那个文件没了,这串也就没了。
925
+
926
+ **它不是「有更新时才有」。** `internal/gui/plugins.go` 里只有一个条件:
927
+
928
+ ```go
929
+ if plugin.IsGit(e.Spec) {
930
+ j.Commit, j.Head = plugin.ShortCommit(plugin.GitCommit(e.Spec)), plugin.ShortCommit(plugin.GitHeadCached(e.Spec))
931
+ }
932
+ ```
933
+
934
+ git 源**总是**填 `Commit`(已装)和 `Head`(远端最新),npm 源两个都不填。
935
+ ⇒ **那串只在 git 源出现,而且在插件侧去不掉** —— 想让版本位置干净,只有改用 npm 源。
936
+
937
+ 同一套符号解释了两件事:
938
+
939
+ | 现象 | 机制 |
940
+ |---|---|
941
+ | 改了代码、推上去,version 没变,界面也提示有新版本 | git 源比的是 **commit**(`GitBehind` / `SameCommit`),不是 version —— 文案 `已安装 {have},{repo} 已到 {head}`、`{head} 是 {repo} 的最新提交` |
942
+ | 同一版本号,两串却不一样 | 两个仓库各自的 commit 不同;光看 version 分不清 |
943
+
944
+ ⚠ **npm 源没有这个兜底** —— 那边比的只有 `version`,所以改了代码**必须 bump**,
945
+ 否则用户看不到任何更新提示(见 [UPDATING.md](./UPDATING.md))。
946
+
947
+ ⚠ **已安装卡片的元数据是有限的**:只显示版本、更新于、按钮和 README 摘要。
948
+ **publisher、周下载量、license、stars 只在「发现」面板的卡片上显示**,不在已安装列表里
949
+ (`internal/gui/plugins.go` 的 `pluginEntryJSON` 没有传给这些字段)。想让某条信息出现在
950
+ 已安装卡片上,**写进 README**。
951
+
952
+ ---
953
+
954
+ ## 十、卡片介绍页(不是钩子)
955
+
956
+ **magpie 用 npm 包的 README 渲染插件卡片的介绍正文。**
957
+
958
+ 官方文档原文:
959
+ > **README.** A plugin's page in magpie shows its npm README. Say what it signs in to,
960
+ > how, where the sign-in is kept, and which models it has.
961
+
962
+ **所以 README 要写清楚四件事:**
963
+ 1. 签入什么服务
964
+ 2. 怎么登录
965
+ 3. 登录态存在哪
966
+ 4. 支持哪几家发行商(`DeepSeek / GLM / GPT`),各家只列最新 / 热门一两行
967
+
968
+ > 官方原文是「which models it has」,但**别写成「支持 N 个模型」** —— 平台目录 ≠ 计费清单,
969
+ > 那个数还要跟着上下架维护。写法见
970
+ > [README-TEMPLATE](./README-TEMPLATE.md) 的「模型」段:发行商开头,表格留最新 / 热门,
971
+ > 全量由 `magpie plugin list` 出。
972
+
973
+ ### 卡片上其它元素(全部自动)
974
+
975
+ | 元素 | 来源 |
976
+ |---|---|
977
+ | 版本号 | `package.json` 的 `version` |
978
+ | 许可证 | `package.json` 的 `license` |
979
+ | 「npm ↗」链接 | `package.json` 的 `name` |
980
+ | 「源码 ↗」链接 | `package.json` 的 `repository` |
981
+ | 更新时间 | npm 的发布记录 |
982
+ | 下载量 | npm 统计 |
983
+ | 「已登录」绿勾 | 有 auth 记录 |
984
+ | 「非官方插件」警告 | GitHub topic 安装的会自动标 |
985
+
986
+ ### provider 信息的完整组装(契约源码)
987
+
988
+ ```js
989
+ {
990
+ spec: a.spec,
991
+ name: p.name,
992
+ npm: p.npm ?? "",
993
+ api: p.api ?? "",
994
+ methods: methods(a.auth),
995
+ single: a.auth.single === true,
996
+ icon: iconOf(a),
997
+ usage: typeof a.auth.usage === "function", // ← 有无 usage 钩子
998
+ checkin: typeof a.auth.checkin === "function", // ← 有无 checkin 钩子
999
+ options: optionsOf(a.auth),
1000
+ decide: p.decide === true,
1001
+ searches: p.searches === true,
1002
+ maxConcurrency: concurrencyOf(a),
1003
+ signedIn: keys.length > 0,
1004
+ authType: first?.type ?? "",
1005
+ accountId: whoOf(first),
1006
+ fellBack: keys.length > 0 && !!p.fellBack,
1007
+ listError: ...,
1008
+ accounts: keys.map((k, i) => ({
1009
+ key: k,
1010
+ type: stored[k]?.type ?? "",
1011
+ accountId: whoOf(stored[k]),
1012
+ hint: hintOf(stored[k]),
1013
+ models: ...,
1014
+ fellBack: ...,
1015
+ listError: ...,
1016
+ })),
1017
+ models: [...],
1018
+ }
1019
+ ```
1020
+
1021
+ **`usage: typeof a.auth.usage === "function"` —— magpie 直接探测你有没有这个钩子。**
1022
+
1023
+ ---
1024
+
1025
+ ## 十一、`usage` 字段怎么变成你看到的进度条
1026
+
1027
+ ```js
1028
+ windows: [{
1029
+ name: "积分", // 进度条上方的标签
1030
+ used: 62.7, // 已用百分比(0-100,超额 >100)
1031
+ display: "2,131.63 / 3,400 积分", // 数字显示
1032
+ amount: 2131.63, // 已用
1033
+ limit: 3400, // 总量
1034
+ unit: "积分", // 单位
1035
+ resetsAt: "2026-11-23T00:00:00Z", // 重置时间
1036
+ aside: false, // 用光是否停账号
1037
+ }]
1038
+ ```
1039
+
1040
+ 渲染结果:
1041
+
1042
+ ```
1043
+ 积分 ████████░░░░ 2,131.63 / 3,400 积分 · 剩余 62.7%
1044
+ ```
1045
+
1046
+ **如果只给 `used` 不给 `display`/`amount`/`limit`,就只有一个光秃秃的进度条。**
1047
+
1048
+ **`aside: true` 的窗口用光不会停账号**(比如赠送积分),
1049
+ magpie 会把它显示成「附加额度」。
1050
+
1051
+ ### ⚠⚠ `used` 是必填,而且它决定路由,不只是画条
1052
+
1053
+ 这是本节最要紧的一条,踩过的人付出了很实的代价。
1054
+
1055
+ magpie 归一化时是这么取的(宿主源码原文):
1056
+
1057
+ ```js
1058
+ used: Math.max(0, num(w?.used)), // past 100 when overspent
1059
+ ```
1060
+
1061
+ **`num(undefined)` 是 0** —— 于是「插件没上报 `used`」和「插件上报了 0%」
1062
+ 在宿主眼里完全一样。而这个值还被拿去排序账号:
1063
+
1064
+ ```js
1065
+ const group = (w) => w.used >= 98 ? "spent" : w.used >= 90 ? "low" : "fine"
1066
+ ```
1067
+
1068
+ `spent` 的账号在智能路由里的解释是 *"all but used up, it answers only when
1069
+ nothing else can"* —— **排到最后**。
1070
+
1071
+ 所以漏一个 `used` 会同时造成两件坏事:
1072
+
1073
+ | 后果 | 机制 |
1074
+ |---|---|
1075
+ | 卡片显示「剩余 100%」 | 设置里「显示剩余」时 `100 - used`,`used=0` ⇒ 100% |
1076
+ | **耗尽的账号被排到队首** | `used=0` ⇒ `fine` ⇒ 排在真在用的账号前面 |
1077
+
1078
+ 实测(2026-10-26,本机三天路由日志):一个余额 0 的账号因为插件没上报
1079
+ `used`,在其所在路由组被选中 **256 次里占 255 次**,11 次请求被上游以
1080
+ 「余额不足」拒掉。补上 `used` 后同一个账号从 **3/13 → 13/13**,并被 magpie
1081
+ 按窗口休息到下一个结算日,之后再没被选中。
1082
+
1083
+ **每条窗口都必须给一个有限的数字**,包括这些边角情形:
1084
+
1085
+ ```js
1086
+ // 号被蹬空(两池皆 0):合成一条 used:100 的窗口,让它沉底。
1087
+ // 千万不要「什么都不画」—— 不画 = 没有参与路由的窗口 = 它继续排前面。
1088
+ { name: "今日额度", used: 100, display: "已耗尽" }
1089
+
1090
+ // 上限拿不到(厂商不下发 total):别编一个 limit,但也别漏 used。
1091
+ // 标 aside,让它只展示、不参与路由打分。
1092
+ { name: "今日额度", used: 0, aside: true, display: "1,234 剩余(上限未知)" }
1093
+
1094
+ // 没有上限、没有周期的池(永久积分/赠送额度):aside,used 给 0。
1095
+ // ⚠ 不要用 total = 剩余 伪造总量,那等于谎报占比。
1096
+ { name: "永久积分", used: 0, aside: true, display: "5,500" }
1097
+ ```
1098
+
1099
+ **判据:`windows` 里每一条都要有 `used`,且是 `Number.isFinite` 为真的数字。**
1100
+ 写完自己过一遍这四种情形:正常、耗尽、上限未知、只有永久池。
1101
+ (`magpie-loomy-auth` 的 `tests/semantics.test.mjs` 就是这么锁的,可以参照。)
1102
+
1103
+ ---
1104
+
1105
+ ## 十一之二、`free`:让模型进「免费优先」那一档
1106
+
1107
+ 模型条目上的 `free` 是布尔值,喂给 magpie 的 **Free first** 路由模式
1108
+ (界面上叫「Free first」,路由 id 是 `free`)。契约原文:
1109
+
1110
+ > **Free first** — "Requests go to what costs nothing first: the models their
1111
+ > vendor serves **at no cost to this subscription's allowance or credits**, in
1112
+ > their listed order, and only when those can't answer to one that spends them."
1113
+
1114
+ 路由解释也能印证:
1115
+
1116
+ ```js
1117
+ case "free":
1118
+ if (f.free) return t("Free first: {model} costs nothing on {who}, so it's tried before any that spends the allowance.")
1119
+ return t("Free first: every one that costs nothing is spent or resting, so {model} on {who}, which spends the allowance, is next.")
1120
+ ```
1121
+
1122
+ ### ⚠ 判据是「花不花这份订阅的额度」,不是「倍率是否为 0」
1123
+
1124
+ 这是最容易错的一处。实测(2026-10-26,本机 `plugin-providers.json`):
1125
+
1126
+ ```
1127
+ rate === 0 的条目 51 条
1128
+ 其中 free === true 13 条
1129
+ 其中 free === false 38 条 ← 「套餐内不按次计费」,仍消耗套餐额度
1130
+ ```
1131
+
1132
+ 那 38 条里包括 qoder 的 `ultimate` / `dfmodel` 等 —— 它们不按次计价,
1133
+ 但照样吃套餐配额,**不是免费**。若按 `rate === 0` 一刀切,会把一整批
1134
+ 套餐模型误标成免费,Free first 就会一直派到它们身上。
1135
+
1136
+ ```js
1137
+ // ❌ 错:把「不按次计费」当免费
1138
+ free: m.rate === 0
1139
+
1140
+ // ❌ 也错:有倍率就一定是收费 —— `x0.0` 是 0 倍,等于不扣分
1141
+ if (m.rate) { model.rate = m.rate; model.free = false }
1142
+
1143
+ // ✅ 对:只认厂商自己的声明
1144
+ function isFreeModel(displayName, rate) {
1145
+ if (/免费/.test(String(displayName ?? ""))) return true // 展示名写明「限时免费」
1146
+ return typeof rate === "string" && /^x0(\.0+)?$/i.test(rate.trim()) // 倍率 x0.0
1147
+ }
1148
+ ```
1149
+
1150
+ **每个厂商的判据不同**,但都应当**来自厂商自己的说法**(展示名文本、
1151
+ 模型条目里的价格字段、或厂商文档),而不是从别的字段反推。
1152
+
1153
+ ### 自查
1154
+
1155
+ ```js
1156
+ // 拿你的模型表跑一遍,确认该免费的确实是 true,不该的是 false
1157
+ for (const [id, m] of Object.entries(models)) console.log(id, m.free, m.rate)
1158
+ ```
1159
+
1160
+ ⚠ `free` 只影响 `free` 这个路由模式;`pace` / `usage` / `weight` 等不看它。
1161
+ 但它标错的后果很实:本该免费被优先派发的模型拿不到优先,或者反过来
1162
+ 把收费模型当免费一直用。
1163
+
1164
+ ---
1165
+
1166
+ ## 十一之三、模型条目上的全部字段(这一版的完整表)
1167
+
1168
+ > 2026-10-28 从 magpie **0.1.1763** 的二进制重新提取契约后补的。原来这一份漏了
1169
+ > 6 个字段:`searches` / `decides` / `rateWas` / `fast` / `serviceTiers` / `modalities`。
1170
+ > 漏了不会报错 —— 那些字段全是「不写就等于关掉某个能力」。
1171
+
1172
+ **模型级 · 顶层**(`cfg.models[<id>]`,或 `provider.models` 返回的每条):
1173
+
1174
+ | 字段 | 形状 | 不写会怎样 |
1175
+ |---|---|---|
1176
+ | `id` / `name` | string | 必须;`id` 是 agent 用的那个 |
1177
+ | `apiId` | string | 上游真正的模型 id 与 `id` 不同时才写(改名 / 加前缀时用) |
1178
+ | `providerID` | string | magpie 内部填的,插件一般不用写 |
1179
+ | `provider.api` / `provider.npm` | string | 不写就继承 provider 级 |
1180
+ | `limit.context` / `limit.output` | number | 0 时 magpie 会去查模型库;别填猜的数字(见十六节) |
1181
+ | `released` / `release_date` / `family` | string | 只影响排序与展示;**不写不会关掉能力** |
1182
+ | `cost` | object | 只用于显示 |
1183
+ | `variants` | object | 思考档位的可选值 |
1184
+ | `rate` | number \| `"x0.03"` | 不给就不画倍率胶囊 |
1185
+ | `rateWas` | 同上 | **折扣前倍率**(Qoder 的 `price_factor`);折扣中时两个一起给 |
1186
+ | `free` | boolean | 判据见十一之二,**不能从 rate 反推** |
1187
+ | `fast` | boolean | `service_tier: priority` 时跑快(Cursor 那种);不写就没有这档 |
1188
+ | `serviceTiers` | array | 厂商按档位服务的那些档(#2514);不写则只有默认档 |
1189
+ | `decides` | boolean | **decision 模型**:从 agent 列表里隐藏,只作路由组的分类器用 |
1190
+ | `searches` | boolean | 这个模型**自己会联网**(用平实的话问就行);不写则 magpie 走它自己的搜索 |
1191
+ | `headers` / `options` | object | 单模型额外的请求头 / 厂商专属参数 |
1192
+
1193
+ **模型级 · `capabilities` 下面**(宿主会把 `reasoning` / `attachment` / `toolcall` /
1194
+ `modalities` 归到 `capabilities` 里,写的时候直接给这几个平字段,别自己嵌一层):
1195
+
1196
+ > ⚠ **两个位置两种拼写,别混**:`provider.models` 返回的模型走 OpenCode 的 `Model` 形状,
1197
+ > 里面是 **`toolcall`**(无下划线,[官方 `#provider`](https://usemagpie.ai/docs/zh/plugins#provider) 这么写的);
1198
+ > `config` 钩子里写死的静态模型用的是 **`tool_call`**(官方 `#quick` 的示例这么写的)。
1199
+ > 写错位置的拼写不会报错,只会让「能不能调工具」这一项静默为假。
1200
+
1201
+ | 字段 | 形状 | 不写会怎样 |
1202
+ |---|---|---|
1203
+ | `reasoning` | boolean | 没有思考档位 |
1204
+ | `attachment` | boolean | **不能收文件/图片** |
1205
+ | `toolcall` | boolean | 不能调工具 |
1206
+ | `temperature` | boolean | 不给则视为支持(`false` 才关) |
1207
+ | `modalities.input` / `modalities.output` | `["text","image",…]` | **必须显式写全,`image` 要写成布尔值。** 省掉不等于「按 text 处理」:magpie 只认 `typeof input.image === "boolean"`(宿主按它算 `imageSaid`),没写就是**厂商未表态**,会退到 models.dev 按裸模型 id 投票 —— 同名模型只要在别家标过 `image`,这个实际收不了图的模型就会被当成能收图 |
1208
+ | `interleaved` | boolean | 思考与正文交错输出(少数厂商) |
1209
+ | `maxTokens` | number | 单次输出上限,与 `limit.output` 不同:这是「这一次」的上限 |
1210
+ | `imageSaid` | boolean | **宿主自动算的**(`typeof capabilities.input.image === "boolean"`),插件不用写、写了也不读。想让 magpie 把「厂商说过」当真,就把 `input.image` 写成布尔值 |
1211
+
1212
+ > ⚠ **text-only 接口的写法(照抄)**:静态模型和 `provider.models` 返回的每条都要给全,
1213
+ > 只写 `input: { text: true }` 等于没说。模板:
1214
+ >
1215
+ > ```js
1216
+ > const TEXT_ONLY = { input: { text: true, image: false, audio: false, video: false, pdf: false } }
1217
+ > // config 钩子里的静态模型
1218
+ > { id, name, attachment: false, modalities: { input: ["text"], output: ["text"] } }
1219
+ > // provider.models 返回的模型
1220
+ > capabilities: { ..., attachment: false, input: { ...TEXT_ONLY.input, ...(base.capabilities?.input ?? {}) } }
1221
+ > ```
1222
+ >
1223
+ > 实测代价(2026-10-29,magpie 0.1.1932):CodeArts 8 个模型全部漏写 `image`,
1224
+ > 四天里 161 次把截图直发上游,全部被 `The request model is not multimodal (InferHub.001001020.406)` 拒绝;
1225
+ > 补上 `image: false` 后同一个请求改走识图模型描述,正常读出图内文字。
1226
+
1227
+ > `capabilities` 这一层最容易漏。漏了的效果是「模型能跑,但收不了图 / 不显示思考档」——
1228
+ > 不报错,只是能力少一块。`python extract-contract.py` + `node scripts/contract-diff.mjs`
1229
+ > 会把这类漏抄直接列出来。
1230
+
1231
+ **provider 级**(`config` 钩子里的 `cfg.provider[<id>]`):
1232
+
1233
+ | 字段 | 形状 | 含义 |
1234
+ |---|---|---|
1235
+ | `searches` | boolean | 这个 provider 的上游**自己回答** web_search(Anthropic 或 Responses API 上,#2027):客户的 `web_search` 原样发过去,不走 magpie 的搜索 |
1236
+ | `decide` | boolean | 这个 provider 服务一个 **decision API**(System One),在 base `/systemone`,走它的 `fetch` |
1237
+
1238
+ > ⚠ 这两个 `searches` 不是一回事:provider 级是「上游整体会搜」,模型级是「这一条会搜」。
1239
+ > 给错的后果是 magpie 把搜索请求发给一个不会搜的模型 —— 它不会报错,只会答非所问。
1240
+
1241
+ ### 插件供图(`draw`)
1242
+
1243
+ magpie **0.1.1707 起**支持插件提供图像模型(0.1.1747 接入 SenseNova 的
1244
+ `sensenova-u1.5-lite`)。三条判据同时在,才会出现在 Settings 的「图像生成」选择器里:
1245
+
1246
+ 1. 模型 id 像画图的:`catalog.DrawsID` —— 含 `image` / `imagen` / `dall-e` / `flux` /
1247
+ `seedream` / `cogview` / `stable-diffusion` / `sdxl` / `wanx` / `kolors` / `hidream` /
1248
+ `banana` / `sensenova-u1` 之一
1249
+ 2. 它走 images API 而不是在 chat 里回图:`catalog.ImagesAPI` —— 同上列表减去
1250
+ `gemini-*-image` 与 `gpt-5-image`(那两类在 chat 里回图)
1251
+ 3. 这个模型所在的 npm 包是 `@ai-sdk/openai-compatible`
1252
+
1253
+ 满足后,magpie 把请求发到 `/v1/images/generations`(或 `/v1/images/edits`),
1254
+ 走插件的 `fetch`,签名方式与 chat 一样。
1255
+
1256
+ ⚠ **但它同时也会出现在模型列表里。** `pluginCatalog` 不按 `DrawsID` 过滤 —— 所以用户
1257
+ 可能在 chat 里选中一个只会画图的模型,然后收到 400。要不要列,是个取舍:
1258
+ - 列 → 多了「用它画图」的能力,代价是 chat 列表里多一条选不得的条目
1259
+ - 不列 → 干净,但放弃一个 magpie 已经支持的能力
1260
+
1261
+ ⚠ 这条**不在二进制契约里**(是 Go 实现 + `docs/subsystems/provider-plugins.md`),
1262
+ 所以 `extract-contract.py` 抓不到。判据来自源码:`internal/catalog/catalog.go` 的
1263
+ `DrawsID` / `ImagesAPI`,`internal/gateway/draw.go` 的 `Drawers`,
1264
+ `internal/provider/plugins.go` 的 `PluginOpenAICompatible`。
1265
+
1266
+ ---
1267
+
1268
+ ## 十二、magpie 已经做好的事(插件不用重做)
1269
+
1270
+ | 能力 | magpie 的实现 |
1271
+ |---|---|
1272
+ | 签到调度 | 北京时间每账号每天一次 + 失败重试 |
1273
+ | 签到记账 | `plugin-checkin.json` |
1274
+ | 签到 UI | 「今日已签到 +100 · 连续 21 天」 |
1275
+ | 额度趋势 | `credits-daily.json` |
1276
+ | token 续期 | `refreshLead` + 串行化 + 合并保存 |
1277
+ | 登录作用域 | `inScope(provider, key, ...)` |
1278
+ | 代理 | `via.run(proxy, ...)`,登录和请求都走 |
1279
+ | SDK 请求头 | `sdkHeaders(npm, key)` |
1280
+ | 请求头顺序 | sdkHeaders → loader → gateway → chat.headers |
1281
+ | 流式转发 | 分帧 + 背压 + base64 |
1282
+ | 登录态标记 | `X-Magpie-Sign-In` 解析 |
1283
+ | 多账号分配 | `signInKey` → `provider` 或 `provider#xxxxxx` |
1284
+ | prompts 流程 | 顺序提问 + when/condition/validate |
1285
+ | 故障转移 | 限流/5xx 自动换账号或模型 |
1286
+ | 自动更新 | `@magpie-community/*` 启动后和每小时 |
1287
+ | 市场发现 | GitHub topic `magpie-plugin`(registry.json 是上游推荐位,提 PR 不必费心)|
1288
+
1289
+ **插件只写六个钩子,其余全交给 magpie。**
1290
+
1291
+ ---
1292
+
1293
+ ## 十三、写插件前的检查清单
1294
+
1295
+ 1. `~/.config/magpie/` 下有没有相关状态文件(`plugin-checkin.json` 等)
1296
+ 2. `magpie plugin list` 有没有同类 provider
1297
+ 3. magpie 界面「观测 → 用量」有没有现成展示
1298
+ 4. `magpie-community/plugins` 的 README 表格(25 个插件)
1299
+ 5. 提取二进制里的契约源码(`extract-contract.py`)
1300
+
1301
+ **只有确认 magpie 没有、社区也没有,才值得自己写。**
1302
+
1303
+ ---
1304
+
1305
+ ## 十四、部署(tgz 的坑)
1306
+
1307
+ ⚠ **`magpie plugin add xxx.tgz` 有 bug** —— 它把 tgz 当 JS 文件 `import()`,
1308
+ 报 `exports no OpenCode plugin function`。
1309
+
1310
+ **正确步骤**:
1311
+
1312
+ ```bash
1313
+ # 1. 解压
1314
+ DST=~/.config/magpie/plugins/node_modules/<包名>
1315
+ mkdir -p "$DST"
1316
+ tar -xzf <包名>.tgz -C "$DST" --strip-components=1
1317
+
1318
+ # 2. plugins.json 的 plugins 数组加
1319
+ {"spec": "<包名>"}
1320
+
1321
+ # 3. plugins/package.json 的 dependencies 加
1322
+ "<包名>": "file:./node_modules/<包名>"
1323
+
1324
+ # 4. 安装
1325
+ magpie-bun install
1326
+ ```
1327
+
1328
+ **删除时四处都要清**:
1329
+ 1. `plugins.json`
1330
+ 2. `plugins/package.json`
1331
+ 3. `plugins/node_modules/<包名>/`
1332
+ 4. `plugin-auth.json`(账号)
1333
+ 5. `plugin-providers.json`(缓存)