@fish-under-sea/magpie-purge 0.1.2 → 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.
package/README.md CHANGED
@@ -12,10 +12,36 @@
12
12
  `stripIdentity` 去身份策略都来自它;`magpie-plugin/` 子目录是这条链的起点。没有它就没有这个包。
13
13
  - **[yetone/magpie](https://github.com/yetone/magpie)** — 页面插件与异步网关钩子的宿主契约
14
14
  (`magpie.page` / `magpie.options` / `page/_magpie/bridge.js` / `onRequest(body, ctx)`)由 magpie 提供。
15
+ - **本机 `dsh-purge` 红队技能库** — 内置的 9 位专家由它的 23 份原子技能(`skills/redteam/`)提炼而来,
16
+ 逐条注明原文件;`DSH\purge` 会话导出包里的 Prompt Forge 人设也一并内化为 `personas/prompt-forge.md`。
15
17
 
16
18
  完整来源与改动范围见 [`THIRD_PARTY_NOTICES.md`](THIRD_PARTY_NOTICES.md);上游 MIT 原文原样保留在
17
19
  [`LICENSE`](LICENSE) 里(Copyright (c) 2026 ZiChuanLan)。
18
20
 
21
+ ## 内置专家技能包
22
+
23
+ 装好就有,不用去别处找 md 再导入。`skills/` 里是 9 位专家,`personas/` 里是 1 份人设,随包分发、
24
+ 随包更新;它们在页面里带「内置」标,可启用可停用,但删不掉(不是用户导入的)。
25
+
26
+ | 专家 | 管什么 | 原文来源 |
27
+ | --- | --- | --- |
28
+ | **交战指挥** `engagement-lead` | 开工前置、资源要齐、降级口径、按阶段调度其余八位 | `redteam-setup` |
29
+ | **侦察测绘专家** `recon-specialist` | 被动源 + FOFA 测绘 + ProjectDiscovery 流水线 → 资产全景 | `passive-recon`、`active-scan`、`web-fingerprint`、`asset-correlation`、`recon-pipeline`、`fofa-recon` |
30
+ | **入口猎手** `entry-hunter` | 目录爆破找后台/备份/泄露,浏览器自动化抓 SPA 与接口 | `dir-bruteforce`、`browser-automation`、`kimi-webbridge` |
31
+ | **漏洞打击专家** `vuln-striker` | nuclei 模板化检测:按指纹选模板、按回显判真伪 | `nuclei-scan` |
32
+ | **凭据攻坚专家** `credential-breaker` | 在线爆破(含锁号风险控制)与离线破解、字典策略 | `credential-attack` |
33
+ | **隧道工程专家** `tunnel-engineer` | suo5 / chisel / frp / 代理池的选型与落地 | `suo5-tunnel`、`chisel-tunnel`、`frp-tunnel`、`cn-proxy-pool` |
34
+ | **载荷投送专家** `payload-delivery` | 反弹 shell 选型、VPS 中转、加密 WebShell | `shell-handler`、`vps-reverse-shell`、`webshell-toolkit` |
35
+ | **内网横向专家** `intranet-pivot` | fscan / gogo / Impacket,低噪与发包可控 | `fscan-intranet`、`gogo-intranet`、`lateral-movement` |
36
+ | **成人提示词专家** `nsfw-prompt-smith` | 出图给参数头 + 标签栈 + 变体,出文给场景卡 + 节拍表(角色一律成年) | `nsfw-prompt-gen`(收窄为成年) |
37
+ | **Prompt Forge**(人设) | Danbooru 风格标签编译器,固定正向/负向/三档变体 | `DSH\purge` 会话导出包里的人设 |
38
+
39
+ 用法:页面上挑一条,点「启用」,它就会按你的模式拼进 system(人设在前,技能按
40
+ `## 技能:<名字>` 分段)。内置人设**不会**被自动选中——不点名就注入工具,等于偷偷改你的 system。
41
+
42
+ 提炼原则:不复制原文,按「一条作业线一位专家」重写——每位只留**判据、命令骨干、交接边界、纪律**四块,
43
+ 把散在 23 份文件里的同名坑收成一处。
44
+
19
45
  ## 它解决什么
20
46
 
21
47
  `agent-purge` 的 magpie 插件只认 `~/.agent-purge/` 的人设,页面也不能读盘。
@@ -46,8 +72,8 @@ magpie plugin list
46
72
 
47
73
  | 字段 | 作用 |
48
74
  | --- | --- |
49
- | `mode` | `off` / `strict` / `replace` / `append`;默认 `off` |
50
- | `persona` | 用哪条人设的 id;留空取第一个启用的人设 |
75
+ | `mode` | `off` / `strict` / `replace` / `append`;**留空 = 跟随 `agent-purge` 的 `state.json`**,两边都没有就是 off |
76
+ | `persona` | 用哪条人设的 id;留空取 `agent-purge` 的人设,再留空取第一个启用的人设 |
51
77
  | `agents` | 只对这些 magpie agent 生效,如 `["claude","codex"]`;空数组 = 全部 |
52
78
  | `enabled` | `{条目id: true/false}`:技能必须显式为 `true` 才注入;人设默认启用 |
53
79
  | `pinned` | `{条目id: 摘要}`:固定到某一版,网关用那一版的正文 |
@@ -56,6 +82,30 @@ magpie plugin list
56
82
  | `inline` | 页面里直接贴进来的人设/技能正文,不进磁盘 |
57
83
  | `gatewayTimeoutMs` | 钩子预算,默认 1500 ms(magpie 侧 100–10000) |
58
84
 
85
+ ### 与 agent-purge 自己的设置怎么合并
86
+
87
+ 本插件认 agent-purge 的原生语义(读 `AGENT_PURGE_HOME` 或 `~/.agent-purge/state.json`):
88
+
89
+ ```json
90
+ { "default": { "mode": "replace" }, "persona": "anima",
91
+ "overrides": { "codex": { "mode": "strict", "persona": "other" } },
92
+ "disabledHarnesses": ["grok"] }
93
+ ```
94
+
95
+ 按三层取,先到先得:
96
+
97
+ | 层 | `mode` | `persona` |
98
+ | --- | --- | --- |
99
+ | 1 | 插件页选过的值 | 插件页选过的人设 |
100
+ | 2 | `state.json` 的 `override[agent].mode` → `default.mode` | `override[agent].persona` → `state.persona` |
101
+ | 3 | 兜底 `off` | 第一个启用的人设 |
102
+
103
+ **所以两件事同时成立**:在 agent-purge 里换人设,下一个请求立刻跟着换;在插件页点一下,也立刻生效,
104
+ 不必去改 `state.json`。agent-purge 里 `disabledHarnesses` 含这个 agent、或 `default.mode` 是 `off` 时,
105
+ 本插件一样不做——页面的状态条会写明「agent-purge 里关掉了这个 harness」。
106
+
107
+ 指向的人设不在来源里时,**不会偷偷换成别人**:状态条直接说「指定的 X 不在当前来源里」。
108
+
59
109
  模式语义与上游一致:
60
110
 
61
111
  - `off`:不动请求;
@@ -63,6 +113,16 @@ magpie plugin list
63
113
  - `replace`:人设放最前,宿主 system 里「自称 + 点名模型/厂商」的行删掉(近似);
64
114
  - `append`:人设放最前,宿主 system 原样保留。
65
115
 
116
+ ## 页面怎么告诉你「到底注不注」
117
+
118
+ 页面顶部那条状态就是答案,三种样子:
119
+
120
+ - **绿点**「下一次 agent 请求会注入「Anima」」+ 模式与人设的来源(本页设置 / agent-purge / 自动);
121
+ - **红点**「现在不会注入:请求原样放行」+ 具体原因 + 一个能点的下一步(「用 replace 开始注入」/「去看人设」/「导入一份人设」);
122
+ - 底部「最近一次生效」:真跑过就写人设、技能、字符数;被放行就写**为什么**放行。
123
+
124
+ 导入的 `.zip` 是素材(会话导出包、DSH 插件包),**不会进 system**——检查器里会直说,别把素材当人设。
125
+
66
126
  ## 边界(照实说,不含糊)
67
127
 
68
128
  - 只处理**经过 magpie 网关**的 agent 请求;客户端自己写死的行为、不经过网关的流量改不到。
@@ -77,15 +137,20 @@ magpie plugin list
77
137
  Purge in Magpie/
78
138
  ├── package.json 插件清单:magpie.hooks / magpie.page / magpie.options
79
139
  ├── hooks.js 网关钩子:清点 + 改写(Bun 侧,能读盘)
140
+ ├── skills/ 9 位内置专家(随包分发,一位专家一个 .md)
141
+ ├── personas/ 内置人设(Prompt Forge,内化自 DSH\purge 的导出会话)
80
142
  ├── lib/
81
143
  │ ├── zip.mjs 极简 ZIP 只读解析(store + deflate,拦路径越界)
82
144
  │ ├── inventory.mjs 判型、校验、摘要、去重与版本
83
145
  │ ├── rewrite.mjs 四种 protocol × 四种模式的 system 改写
146
+ │ ├── agentpurge.mjs 读 agent-purge 的 state.json(原生语义)
147
+ │ ├── plan.mjs 三层优先级:这次注不注、注谁、为什么没注
148
+ │ ├── bundled.mjs 读包内 skills/ 与 personas/,标 builtin
84
149
  │ └── state.mjs 快照落盘(state/catalog.json + page/catalog.js)
85
150
  ├── page/ 插件页:index.html / app.js / style.css / icon.*
86
151
  ├── preview/ 本地预览:mock 宿主 + 演示快照(只给人看界面)
87
152
  ├── tools/scan.mjs 离线清点(预览与真机同一条路径)
88
- └── tests/ node --test:判型、校验、改写、选人、快照
153
+ └── tests/ node --test:判型、校验、改写、选人、快照、内置包
89
154
  ```
90
155
 
91
156
  ## 本地预览
@@ -101,7 +166,7 @@ node preview/serve.mjs --demo # http://127.0.0.1:55133
101
166
  ## 开发
102
167
 
103
168
  ```bash
104
- # 测试(36 个用例:判型/校验/改写/选人/快照/钩子端到端/options 回读)
169
+ # 测试(68 个用例:判型/校验/改写/选人/快照/钩子端到端/options 回读/agent-purge 语义/注入判定/控制面过滤/内置专家包)
105
170
  node --test --test-isolation=none "tests/*.test.mjs"
106
171
 
107
172
  # 离线清点,看看本机 purge 目录里都认得什么
@@ -118,17 +183,22 @@ node tools/scan.mjs --json
118
183
 
119
184
  | 项 | agent-purge 的 `magpie-plugin/` | 本插件 |
120
185
  | --- | --- | --- |
121
- | 来源 | 只读 `~/.agent-purge/` | 可配多个目录 + `state/imports` |
186
+ | 来源 | 只读 `~/.agent-purge/` | 可配多个目录 + `state/imports` + 内置专家包 |
122
187
  | 判型 | 只有人设 markdown | 导出包 / DSH 插件包 / 人设 / 技能 / 文本 / 清单 |
123
- | 注入 | 一个人设 | 人设 + 显式启用的技能 |
188
+ | 注入 | 一个人设 | 人设 + 显式启用的技能(含 9 位内置专家) |
124
189
  | 页面 | 人设选择 + 导入 | 条目列表 + 检查器 + 模式 + 回滚 + 校验记录 |
125
190
  | 快照 | 生成脚本写 `personas.js` | 后端每次扫描写 `catalog.js`,页面不读盘 |
126
191
  | 版本 | 无 | 同 id 新摘要算新版本,留两版可回滚 |
192
+ | 与 agent-purge 的关系 | 读 `state.json` 但页面改不了模式 | **认它的 state.json**(mode/persona/overrides/disabledHarnesses),同时页面设置优先,两边都能改 |
193
+ | 状态可见性 | 无 | 顶部状态条直接回答「注不注、注谁、为什么没注」 |
127
194
 
128
195
  ## 版本
129
196
 
130
197
  | 版本 | 要点 |
131
198
  | --- | --- |
199
+ | `0.3.0` | 内置 9 位专家技能包(由本机 `dsh-purge` 的 23 份原子技能提炼)+ 内化 Prompt Forge 人设;内置条目带「内置」标、可停用不可删、不参与自动选中 |
200
+ | `0.2.0` | 认 agent-purge 的 `state.json`(原生语义);页面加「当前注入」状态条与可点的下一步;导入素材后不再一头雾水 |
201
+ | `0.1.2` | README 版本表 |
132
202
  | `0.1.1` | 修掉「没流量时插件页是空的」:钩子模块加载时先从 `plugins.json` 读回自己的 options 再预热扫描 |
133
203
  | `0.1.0` | 首个版本 |
134
204
 
@@ -32,12 +32,24 @@
32
32
  异步网关钩子的 `onRequest(body, ctx)` 形状与 `ctx.report` 码。
33
33
  - 本插件不修改 magpie 源码,只按它的公开扩展点接入。
34
34
 
35
- ## 3. dsh-session-archive(识别样本)
35
+ ## 3. dsh-purge 红队技能库(内置专家的来源)
36
+
37
+ - 位置:本机 `D:\Fish-code\DSH\plugins\dsh-purge\skills\redteam\`(23 份原子技能,共约 143 KB)
38
+ - 用途:`skills/` 下 9 位内置专家由它们**提炼**而来,不是原样复制;每份 SKILL.md 的 `source:`
39
+ 字段写明对应原文件
40
+ - 提炼方式:按「一条作业线一位专家」重写,只保留判据、命令骨干、交接边界、纪律四块;
41
+ 原文里的工具绝对路径、环境变量占位符在提炼时被泛化为通用写法
42
+ - 未做的事:不修改 `dsh-purge` 里的任何文件;本插件也不加载它的运行时代码
43
+
44
+ `DSH\purge` 会话导出包里那份 Prompt Forge 人设,内化为 `personas/prompt-forge.md`,
45
+ 角色范围收窄为成年(原文没有这项限定)。
46
+
47
+ ## 4. dsh-session-archive(识别样本)
36
48
 
37
49
  - 本地样本:`@local/session-archive`(DSH cordis 插件),用于「DSH 插件包」判型夹具;
38
50
  - 它只作为**被判型的对象**出现,本插件不加载、不执行导入件里的任何代码。
39
51
 
40
- ## 4. 许可与再分发
52
+ ## 5. 许可与再分发
41
53
 
42
54
  本插件整体按 MIT 发布。上游 MIT 文本已原样附在 [`LICENSE`](LICENSE) 里;
43
55
  分发本插件时请一并保留该文件,别把上游的版权行删掉。
package/hooks.js CHANGED
@@ -12,7 +12,9 @@
12
12
  import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
13
13
  import { dirname, join } from 'node:path'
14
14
  import { fileURLToPath } from 'node:url'
15
- import { MODES, compose, rewrite, supported } from './lib/rewrite.mjs'
15
+ import { compose, rewrite, supported } from './lib/rewrite.mjs'
16
+ import { readState } from './lib/agentpurge.mjs'
17
+ import { resolve as resolvePlan } from './lib/plan.mjs'
16
18
  import { loadCatalog, ownOptions, paths, sync } from './lib/state.mjs'
17
19
 
18
20
  const here = dirname(fileURLToPath(import.meta.url))
@@ -37,15 +39,14 @@ export function inlineEntries(options = {}) {
37
39
  }
38
40
 
39
41
  /** pick 选出这一次要注入的文本:人设一条,显式启用的技能若干。 */
40
- export function pick(catalog, options = {}) {
42
+ export function pick(catalog, options = {}, state = null, agent = '') {
41
43
  const entries = [...(Array.isArray(catalog?.entries) ? catalog.entries : []), ...inlineEntries(options)]
42
44
  const usable = entries.filter((e) => e.status === 'ok' && e.injectable && typeof e.text === 'string' && e.text.trim())
43
- const enabled = (id) => options.enabled?.[id]
44
45
  const pinned = options.pinned && typeof options.pinned === 'object' ? options.pinned : {}
45
46
 
46
47
  /** resolve 按 pinned 取版本:固定到某一版时用那一版的正文,找不到就如实报告。 */
47
48
  const missingPin = []
48
- const resolve = (entry) => {
49
+ const resolveEntry = (entry) => {
49
50
  const pin = pinned[entry.id]
50
51
  if (!pin || !entry.digest || pin === entry.digest) return entry
51
52
  const old = (entry.history || []).find((v) => v.digest === pin && v.text)
@@ -53,21 +54,16 @@ export function pick(catalog, options = {}) {
53
54
  missingPin.push(entry.id)
54
55
  return entry
55
56
  }
56
-
57
+ const resolved = usable.map(resolveEntry)
57
58
  const byId = new Map()
58
- for (const e of usable) if (!byId.has(e.id)) byId.set(e.id, resolve(e))
59
-
60
- let personaId = typeof options.persona === 'string' ? options.persona.trim() : ''
61
- if (personaId && !byId.has(personaId)) personaId = '' // 指向了不存在的条目:回落到默认
62
- if (!personaId) {
63
- const firstPersona = usable.find((e) => e.kind === 'persona' && enabled(e.id) !== false)
64
- personaId = firstPersona?.id ?? ''
65
- }
66
- const persona = personaId ? byId.get(personaId) : null
67
- const skills = usable.filter((e) => e.kind !== 'persona' && enabled(e.id) === true).map(resolve)
68
-
69
- const text = compose({ persona: persona?.text || '', skills })
70
- return { persona, personaId, skills, text, missingPin }
59
+ for (const e of resolved) if (!byId.has(e.id)) byId.set(e.id, e)
60
+
61
+ // 三层优先级(页面 > agent-purge state.json > 默认)都在 plan.resolve 里判定
62
+ const plan = resolvePlan({ options, state, entries: resolved, agent })
63
+ const persona = plan.personaId ? byId.get(plan.personaId) ?? null : null
64
+ const skills = plan.skills.map((s) => byId.get(s.id)).filter(Boolean)
65
+ const text = plan.ready ? compose({ persona: persona?.text || '', skills }) : ''
66
+ return { persona, personaId: plan.personaId, skills, text, missingPin, plan }
71
67
  }
72
68
 
73
69
  /**
@@ -147,7 +143,6 @@ export function createHook(root = here, { deadlineMs = 400, rescanMs = 30_000, w
147
143
  */
148
144
  async function onRequest(body, ctx) {
149
145
  const options = ctx?.options && typeof ctx.options === 'object' ? ctx.options : {}
150
- const mode = MODES.includes(options.mode) ? options.mode : 'off'
151
146
  const report = (code, bytes = 0) => {
152
147
  try {
153
148
  ctx?.report?.(code, bytes)
@@ -157,10 +152,7 @@ export function createHook(root = here, { deadlineMs = 400, rescanMs = 30_000, w
157
152
  }
158
153
  try {
159
154
  if (!body || typeof body !== 'object' || Array.isArray(body)) return
160
- if (mode === 'off') {
161
- report('skipped', 0)
162
- return
163
- }
155
+ // 页面只限定范围;真正「这次要不要改」由 plan 判定(页面设置 > agent-purge state > 默认)
164
156
  const agents = Array.isArray(options.agents) ? options.agents.filter((a) => typeof a === 'string') : []
165
157
  if (agents.length && !agents.includes(ctx?.agent)) {
166
158
  report('skipped', 0)
@@ -177,19 +169,24 @@ export function createHook(root = here, { deadlineMs = 400, rescanMs = 30_000, w
177
169
  report('skipped', 0)
178
170
  return
179
171
  }
180
- const picked = pick(catalog, options)
172
+ // state.json 每请求读一次(很小的文件),这样在 agent-purge 里换人设立刻生效
173
+ const state = readState()
174
+ const picked = pick(catalog, options, state, ctx?.agent)
181
175
  if (!picked.text) {
182
176
  report('skipped', 0)
177
+ rememberSkip(catalog, picked, ctx)
183
178
  return
184
179
  }
185
- rewrite(body, ctx.protocol, picked.text, mode)
180
+ rewrite(body, ctx.protocol, picked.text, picked.plan.mode)
186
181
  remember({
187
182
  at: Date.now(),
188
183
  agent: ctx?.agent || '',
189
184
  protocol: ctx.protocol,
190
185
  model: ctx?.model || '',
191
- mode,
186
+ mode: picked.plan.mode,
187
+ modeFrom: picked.plan.modeFrom,
192
188
  persona: picked.personaId,
189
+ personaFrom: picked.plan.personaFrom,
193
190
  skills: picked.skills.map((s) => s.id),
194
191
  chars: picked.text.length,
195
192
  missingPin: picked.missingPin,
@@ -203,6 +200,25 @@ export function createHook(root = here, { deadlineMs = 400, rescanMs = 30_000, w
203
200
  }
204
201
  }
205
202
 
203
+ /** rememberSkip 把「为什么没注入」也写下:页面要能回答这个问题,而不是只显示空。 */
204
+ function rememberSkip(catalog, picked, ctx) {
205
+ remember({
206
+ at: Date.now(),
207
+ agent: ctx?.agent || '',
208
+ protocol: ctx?.protocol || '',
209
+ model: ctx?.model || '',
210
+ mode: picked.plan?.mode || 'off',
211
+ modeFrom: picked.plan?.modeFrom || '',
212
+ persona: '',
213
+ personaFrom: picked.plan?.personaFrom || '',
214
+ skills: [],
215
+ chars: 0,
216
+ missingPin: [],
217
+ skipped: picked.plan?.blocked || '',
218
+ catalogVersion: catalog?.version ?? 0,
219
+ })
220
+ }
221
+
206
222
  return { onRequest, pick, ensureCatalog, warm }
207
223
  }
208
224
 
@@ -0,0 +1,80 @@
1
+ // agent-purge 的原生语义:`~/.agent-purge/state.json`。
2
+ //
3
+ // 上游 agent-purge 用这份 state 决定「现在用哪个人设、什么模式、哪个 harness 关掉」:
4
+ //
5
+ // state.default.mode 默认模式(off / strict / replace / append)
6
+ // state.persona 当前人设 id
7
+ // state.overrides[agent] 单个 harness 的覆盖:{ mode, persona }
8
+ // state.disabledHarnesses[] 整个关掉的 harness 列表
9
+ //
10
+ // 本插件认这份语义,但**不接管它**:页面里显式选过的 mode/persona 优先,
11
+ // 没选过才回落到这里。这样两件事都成立——
12
+ // - 在 agent-purge 里换人设,下一个请求立刻换(用户原来怎么用,现在还怎么用);
13
+ // - 在插件页里点「设为当前人设」立刻生效,不需要去改 state.json。
14
+ //
15
+ // state.json 坏掉或缺失时返回 bound=false:没有 state 就当「没装过 agent-purge」,
16
+ // 走插件自己的设置,绝不让一份坏 JSON 把请求改坏。
17
+ import { readFileSync, statSync } from 'node:fs'
18
+ import { homedir } from 'node:os'
19
+ import { isAbsolute, join, resolve } from 'node:path'
20
+ import { MODES } from './rewrite.mjs'
21
+
22
+ /** purgeHome 是 agent-purge 的数据目录:AGENT_PURGE_HOME 优先,否则 ~/.agent-purge。 */
23
+ export function purgeHome(env = process.env) {
24
+ const raw = String(env.AGENT_PURGE_HOME || env.PURGE_HOME || '').trim()
25
+ if (!raw) return join(homedir(), '.agent-purge')
26
+ return isAbsolute(raw) ? raw : resolve(raw)
27
+ }
28
+
29
+ /** statePath 是 agent-purge 的状态文件。 */
30
+ export const statePath = (env) => join(purgeHome(env), 'state.json')
31
+
32
+ /**
33
+ * readState 读 agent-purge 的状态。
34
+ * @returns {{ bound: boolean, mode: string, persona: string, disabled: string[], overrides: object, error: string }}
35
+ */
36
+ export function readState(env = process.env) {
37
+ const empty = { bound: false, mode: '', persona: '', disabled: [], overrides: {}, error: '' }
38
+ const file = statePath(env)
39
+ let st
40
+ try {
41
+ st = statSync(file)
42
+ } catch {
43
+ return empty // 没装过 agent-purge:不是错
44
+ }
45
+ if (!st.isFile()) return { ...empty, error: 'state.json 不是文件' }
46
+ let raw
47
+ try {
48
+ raw = JSON.parse(readFileSync(file, 'utf8'))
49
+ } catch (error) {
50
+ return { ...empty, error: `state.json 坏了:${error?.message || error}` }
51
+ }
52
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
53
+ return { ...empty, error: 'state.json 不是一个对象' }
54
+ }
55
+ const mode = typeof raw.default?.mode === 'string' && MODES.includes(raw.default.mode) ? raw.default.mode : ''
56
+ const persona = typeof raw.persona === 'string' ? raw.persona.trim() : ''
57
+ const disabled = Array.isArray(raw.disabledHarnesses)
58
+ ? raw.disabledHarnesses.filter((x) => typeof x === 'string' && x.trim()).map((x) => x.trim())
59
+ : []
60
+ const overrides = raw.overrides && typeof raw.overrides === 'object' && !Array.isArray(raw.overrides) ? raw.overrides : {}
61
+ return { bound: true, mode, persona, disabled, overrides, error: '' }
62
+ }
63
+
64
+ /**
65
+ * forAgent 取某个 agent 在这里生效的设置。
66
+ *
67
+ * 上游的顺序是:override 的 mode/persona 盖住全局;disabledHarnesses 里的一律不做;
68
+ * default.mode 为 off 时全局关掉。这里照抄,但把「为什么不做」写进 blocked。
69
+ * @returns {{ mode: string, persona: string, blocked: string }}
70
+ */
71
+ export function forAgent(state, agent) {
72
+ if (!state?.bound) return { mode: '', persona: '', blocked: '' }
73
+ const name = typeof agent === 'string' ? agent : ''
74
+ if (name && state.disabled.includes(name)) return { mode: '', persona: '', blocked: 'agent-purge 里关掉了这个 harness' }
75
+ const o = name && state.overrides && typeof state.overrides[name] === 'object' ? state.overrides[name] : null
76
+ const mode = (o && typeof o.mode === 'string' && MODES.includes(o.mode) ? o.mode : '') || state.mode
77
+ if (mode === 'off') return { mode: 'off', persona: '', blocked: 'agent-purge 的 default.mode 是 off' }
78
+ const persona = (o && typeof o.persona === 'string' && o.persona.trim() ? o.persona.trim() : '') || state.persona
79
+ return { mode, persona, blocked: '' }
80
+ }
@@ -0,0 +1,88 @@
1
+ // 插件自带的专家技能包与人设。
2
+ //
3
+ // 为什么要有这一层:用户装个插件,不该先去别处找一堆 .md 再一个个导入。
4
+ // `skills/` 与 `personas/` 随包分发,永远在场;它们不是「来源目录」,而是插件的一部分——
5
+ // 删不掉、改不坏,但可以被停用(停用写进 options.enabled,不碰文件)。
6
+ //
7
+ // 判定优先级:内置条目属于「来源里的一条」,但页面要能分开显示,所以扫描时
8
+ // 给每个条目加上 `builtin: true`,并让它的 root 指向包内目录。
9
+ import { readdirSync, readFileSync, statSync } from 'node:fs'
10
+ import { dirname, join } from 'node:path'
11
+ import { fileURLToPath } from 'node:url'
12
+ import { digestOf, frontmatter, slug } from './inventory.mjs'
13
+
14
+ /** bundledDir 是包内某个内置目录的绝对路径(skills/ 或 personas/)。 */
15
+ export const bundledDir = (root, sub = 'skills') => join(root, sub)
16
+
17
+ /** 内置目录与它产出的类型:skills/ 是专家技能,personas/ 是完整人设。 */
18
+ const BUNDLED = [
19
+ { sub: 'skills', kind: 'skill' },
20
+ { sub: 'personas', kind: 'persona' },
21
+ ]
22
+
23
+ /**
24
+ * readBundled 读包内专家技能与人设。
25
+ *
26
+ * 只认 `skills/*.md`、`personas/*.md`(一级,不递归):一层目录就是「一位专家一个文件」,
27
+ * 递归会让内部参考文件混进专家列表。
28
+ * @returns {{ entries: object[], dir: string, ok: boolean, error: string }}
29
+ */
30
+ export function readBundled(root) {
31
+ const out = { entries: [], dir: bundledDir(root), ok: false, error: '' }
32
+ let any = false
33
+ for (const { sub, kind } of BUNDLED) {
34
+ const dir = bundledDir(root, sub)
35
+ let names
36
+ try {
37
+ names = readdirSync(dir, { withFileTypes: true })
38
+ } catch (error) {
39
+ if (error?.code !== 'ENOENT') out.error = `读不到内置目录 ${sub}/:${error?.message || error}`
40
+ continue
41
+ }
42
+ any = true
43
+ for (const dirent of names) {
44
+ if (!dirent.isFile() || !dirent.name.endsWith('.md')) continue
45
+ const file = join(dir, dirent.name)
46
+ try {
47
+ const st = statSync(file)
48
+ if (st.size > 256 * 1024) continue
49
+ const raw = readFileSync(file, 'utf8')
50
+ const { fields, body } = frontmatter(raw)
51
+ if (!body.trim()) continue
52
+ out.entries.push({
53
+ id: String(fields.id || slug(fields.name || dirent.name)),
54
+ name: String(fields.name || dirent.name.replace(/\.md$/, '')),
55
+ // 人设文件里显式写了 kind 就听它,否则按目录定
56
+ kind: fields.kind === 'persona' || fields.kind === 'skill' ? fields.kind : kind,
57
+ status: 'ok',
58
+ reason: '',
59
+ source: { root: dir, rel: dirent.name },
60
+ size: st.size,
61
+ digest: digestOf(Buffer.from(raw)),
62
+ importedAt: 0,
63
+ meta: {
64
+ format: 'bundled',
65
+ description: String(fields.description || ''),
66
+ version: String(fields.version || ''),
67
+ whenToUse: String(fields.whenToUse || ''),
68
+ source: String(fields.source || ''),
69
+ },
70
+ chars: body.trim().length,
71
+ injectable: true,
72
+ text: body.trim(),
73
+ builtin: true,
74
+ })
75
+ } catch {
76
+ /* 单个坏文件不该让整包读不出来 */
77
+ }
78
+ }
79
+ }
80
+ out.ok = any
81
+ out.entries.sort((a, b) => (a.kind === b.kind ? a.id.localeCompare(b.id) : a.kind === 'persona' ? -1 : 1))
82
+ return out
83
+ }
84
+
85
+ /** here 是 lib/ 的上级(包根)。 */
86
+ const here = dirname(fileURLToPath(import.meta.url))
87
+
88
+ export const bundled = (root = join(here, '..')) => readBundled(root)
package/lib/inventory.mjs CHANGED
@@ -381,6 +381,11 @@ function inspectJson(root, file, ctx) {
381
381
  ctx.rejected.push(reject(file, '只有会话清单,缺 session.v4.jsonl;请导入完整导出包', ctx.now, { root }))
382
382
  return
383
383
  }
384
+ // agent-purge 自己的控制面文件不是素材:它决定「注不注、注谁」,已经由 agentpurge 那条路读了。
385
+ // 列进条目表会让页面看起来像「导入了一个清单」,实际什么也不会发生。
386
+ if (basename(file) === 'state.json' && (parsed?.default || parsed?.overrides || parsed?.persona)) {
387
+ return
388
+ }
384
389
  ctx.entries.push({
385
390
  id: slug(basename(file)),
386
391
  name: typeof parsed?.name === 'string' ? parsed.name : basename(file),
@@ -510,6 +515,7 @@ export function snapshotOf(entries, sources, rejected, { version = 0, now = Date
510
515
  meta: e.meta,
511
516
  chars: e.chars,
512
517
  injectable: e.injectable,
518
+ builtin: e.builtin === true,
513
519
  })),
514
520
  rejected,
515
521
  options,
package/lib/plan.mjs ADDED
@@ -0,0 +1,102 @@
1
+ // 「这一次请求会注入什么」的判定:钩子拿它决定改不改,页面拿它显示状态。
2
+ //
3
+ // 三层,先到先得:
4
+ // 1. 插件页显式选过的(options.mode / options.persona);
5
+ // 2. agent-purge 的 state.json(全局 mode/persona,或该 agent 的 override);
6
+ // 3. 兜底:mode 视为 off,人设取第一个启用的人设。
7
+ //
8
+ // 这样两件事同时成立:在 agent-purge 里换人设,下一个请求立刻跟着换;
9
+ // 在插件页里点一下,也立刻生效,不必去改 state.json。
10
+ import { MODES } from './rewrite.mjs'
11
+ import { forAgent } from './agentpurge.mjs'
12
+
13
+ /**
14
+ * resolve 判定这次注入。
15
+ * @param {object} p
16
+ * @param {object} p.options 插件 options(plugins.json)
17
+ * @param {object} p.state agent-purge 的 readState() 结果;没有就传 null
18
+ * @param {object[]} p.entries 可注入的条目(带 text 的)
19
+ * @param {string} [p.agent] 当前请求的 agent 名;页面判定全局时不传
20
+ * @returns {{mode: string, modeFrom: string, personaId: string, personaName: string, personaFrom: string, skills: object[], blocked: string, ready: boolean}}
21
+ */
22
+ export function resolve({ options = {}, state = null, entries = [], agent = '' } = {}) {
23
+ const ap = forAgent(state, agent)
24
+ if (ap.blocked) {
25
+ return blank('agent-purge', ap.mode || '', ap.blocked, ap.mode === 'off' ? 'ap-off' : 'ap-disabled')
26
+ }
27
+
28
+ // ---- 模式 ----
29
+ const pageMode = MODES.includes(options.mode) ? options.mode : ''
30
+ const mode = pageMode || ap.mode || 'off'
31
+ const modeFrom = pageMode ? 'page' : ap.mode ? 'agent-purge' : 'default'
32
+ if (mode === 'off') {
33
+ return {
34
+ ...blank(modeFrom, mode, '', modeFrom === 'default' ? 'no-mode' : 'mode-off'),
35
+ blocked: modeFrom === 'default'
36
+ ? '还没选模式:模式是 off,请求原样放行。选一个模式(比如 replace)就开始注入'
37
+ : '当前模式是 off,请求原样放行',
38
+ }
39
+ }
40
+
41
+ // ---- 人设 ----
42
+ const byId = new Map()
43
+ for (const e of entries) if (e && typeof e.id === 'string' && !byId.has(e.id)) byId.set(e.id, e)
44
+ const personas = entries.filter((e) => e.kind === 'persona')
45
+
46
+ let personaId = ''
47
+ let personaFrom = ''
48
+ let missingPersona = ''
49
+ const pagePersona = typeof options.persona === 'string' ? options.persona.trim() : ''
50
+ if (pagePersona && byId.has(pagePersona)) {
51
+ personaId = pagePersona
52
+ personaFrom = 'page'
53
+ } else if (ap.persona && byId.has(ap.persona)) {
54
+ personaId = ap.persona
55
+ personaFrom = 'agent-purge'
56
+ } else if (pagePersona || ap.persona) {
57
+ // 指名要谁却找不到,就不偷偷换一个人设顶上:那会让「明明选了 A,注入的却是 B」
58
+ missingPersona = pagePersona || ap.persona
59
+ } else {
60
+ // 自动只挑用户自己的人设:内置人设(Prompt Forge 这类)是工具,不该在用户没选的时候
61
+ // 悄悄接管请求——那会让人以为「我什么都没配,怎么 system 被换了」。
62
+ const auto = personas.find((e) => e.builtin !== true && options.enabled?.[e.id] !== false)
63
+ if (auto) {
64
+ personaId = auto.id
65
+ personaFrom = 'auto'
66
+ }
67
+ }
68
+
69
+ if (!personaId) {
70
+ if (missingPersona) {
71
+ return {
72
+ ...blank(modeFrom, mode, '', 'persona-missing'),
73
+ blocked: `指定的「${missingPersona}」不在当前来源里:把它放进来源目录,或在这里挑一个`,
74
+ }
75
+ }
76
+ const why = personas.length
77
+ ? '还没有选人设:左边挑一条「人设」点“设为当前人设”'
78
+ : '来源里还没有人设(.md):导入一份,或把 agent-purge 的 personas 目录加进来源'
79
+ return { ...blank(modeFrom, mode, '', personas.length ? 'no-persona' : 'no-persona-file'), blocked: why }
80
+ }
81
+
82
+ const persona = byId.get(personaId)
83
+ const skills = entries
84
+ .filter((e) => e.kind !== 'persona' && options.enabled?.[e.id] === true)
85
+ .map((e) => ({ id: e.id, name: e.name, text: e.text }))
86
+
87
+ return {
88
+ mode,
89
+ modeFrom,
90
+ personaId,
91
+ personaName: persona?.name || personaId,
92
+ personaFrom,
93
+ skills,
94
+ blocked: '',
95
+ blockedCode: '',
96
+ ready: true,
97
+ }
98
+ }
99
+
100
+ function blank(modeFrom, mode, blocked, blockedCode = '') {
101
+ return { mode: mode || 'off', modeFrom, personaId: '', personaName: '', personaFrom: '', skills: [], blocked, blockedCode, ready: false }
102
+ }