@fish-under-sea/magpie-purge 0.1.1 → 0.2.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 +50 -3
- package/hooks.js +42 -26
- package/lib/agentpurge.mjs +80 -0
- package/lib/inventory.mjs +5 -0
- package/lib/plan.mjs +100 -0
- package/lib/state.mjs +12 -2
- package/package.json +2 -2
- package/page/app.js +96 -6
- package/page/index.html +3 -0
- package/page/style.css +30 -0
- package/preview/bridge.js +9 -1
- package/preview/catalog.blocked.js +45 -0
- package/preview/catalog.demo.js +12 -0
- package/preview/serve.mjs +8 -6
package/README.md
CHANGED
|
@@ -46,8 +46,8 @@ magpie plugin list
|
|
|
46
46
|
|
|
47
47
|
| 字段 | 作用 |
|
|
48
48
|
| --- | --- |
|
|
49
|
-
| `mode` | `off` / `strict` / `replace` / `append
|
|
50
|
-
| `persona` | 用哪条人设的 id
|
|
49
|
+
| `mode` | `off` / `strict` / `replace` / `append`;**留空 = 跟随 `agent-purge` 的 `state.json`**,两边都没有就是 off |
|
|
50
|
+
| `persona` | 用哪条人设的 id;留空取 `agent-purge` 的人设,再留空取第一个启用的人设 |
|
|
51
51
|
| `agents` | 只对这些 magpie agent 生效,如 `["claude","codex"]`;空数组 = 全部 |
|
|
52
52
|
| `enabled` | `{条目id: true/false}`:技能必须显式为 `true` 才注入;人设默认启用 |
|
|
53
53
|
| `pinned` | `{条目id: 摘要}`:固定到某一版,网关用那一版的正文 |
|
|
@@ -56,6 +56,30 @@ magpie plugin list
|
|
|
56
56
|
| `inline` | 页面里直接贴进来的人设/技能正文,不进磁盘 |
|
|
57
57
|
| `gatewayTimeoutMs` | 钩子预算,默认 1500 ms(magpie 侧 100–10000) |
|
|
58
58
|
|
|
59
|
+
### 与 agent-purge 自己的设置怎么合并
|
|
60
|
+
|
|
61
|
+
本插件认 agent-purge 的原生语义(读 `AGENT_PURGE_HOME` 或 `~/.agent-purge/state.json`):
|
|
62
|
+
|
|
63
|
+
```json
|
|
64
|
+
{ "default": { "mode": "replace" }, "persona": "anima",
|
|
65
|
+
"overrides": { "codex": { "mode": "strict", "persona": "other" } },
|
|
66
|
+
"disabledHarnesses": ["grok"] }
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
按三层取,先到先得:
|
|
70
|
+
|
|
71
|
+
| 层 | `mode` | `persona` |
|
|
72
|
+
| --- | --- | --- |
|
|
73
|
+
| 1 | 插件页选过的值 | 插件页选过的人设 |
|
|
74
|
+
| 2 | `state.json` 的 `override[agent].mode` → `default.mode` | `override[agent].persona` → `state.persona` |
|
|
75
|
+
| 3 | 兜底 `off` | 第一个启用的人设 |
|
|
76
|
+
|
|
77
|
+
**所以两件事同时成立**:在 agent-purge 里换人设,下一个请求立刻跟着换;在插件页点一下,也立刻生效,
|
|
78
|
+
不必去改 `state.json`。agent-purge 里 `disabledHarnesses` 含这个 agent、或 `default.mode` 是 `off` 时,
|
|
79
|
+
本插件一样不做——页面的状态条会写明「agent-purge 里关掉了这个 harness」。
|
|
80
|
+
|
|
81
|
+
指向的人设不在来源里时,**不会偷偷换成别人**:状态条直接说「指定的 X 不在当前来源里」。
|
|
82
|
+
|
|
59
83
|
模式语义与上游一致:
|
|
60
84
|
|
|
61
85
|
- `off`:不动请求;
|
|
@@ -63,6 +87,16 @@ magpie plugin list
|
|
|
63
87
|
- `replace`:人设放最前,宿主 system 里「自称 + 点名模型/厂商」的行删掉(近似);
|
|
64
88
|
- `append`:人设放最前,宿主 system 原样保留。
|
|
65
89
|
|
|
90
|
+
## 页面怎么告诉你「到底注不注」
|
|
91
|
+
|
|
92
|
+
页面顶部那条状态就是答案,三种样子:
|
|
93
|
+
|
|
94
|
+
- **绿点**「下一次 agent 请求会注入「Anima」」+ 模式与人设的来源(本页设置 / agent-purge / 自动);
|
|
95
|
+
- **红点**「现在不会注入:请求原样放行」+ 具体原因 + 一个能点的下一步(「用 replace 开始注入」/「去看人设」/「导入一份人设」);
|
|
96
|
+
- 底部「最近一次生效」:真跑过就写人设、技能、字符数;被放行就写**为什么**放行。
|
|
97
|
+
|
|
98
|
+
导入的 `.zip` 是素材(会话导出包、DSH 插件包),**不会进 system**——检查器里会直说,别把素材当人设。
|
|
99
|
+
|
|
66
100
|
## 边界(照实说,不含糊)
|
|
67
101
|
|
|
68
102
|
- 只处理**经过 magpie 网关**的 agent 请求;客户端自己写死的行为、不经过网关的流量改不到。
|
|
@@ -81,6 +115,8 @@ Purge in Magpie/
|
|
|
81
115
|
│ ├── zip.mjs 极简 ZIP 只读解析(store + deflate,拦路径越界)
|
|
82
116
|
│ ├── inventory.mjs 判型、校验、摘要、去重与版本
|
|
83
117
|
│ ├── rewrite.mjs 四种 protocol × 四种模式的 system 改写
|
|
118
|
+
│ ├── agentpurge.mjs 读 agent-purge 的 state.json(原生语义)
|
|
119
|
+
│ ├── plan.mjs 三层优先级:这次注不注、注谁、为什么没注
|
|
84
120
|
│ └── state.mjs 快照落盘(state/catalog.json + page/catalog.js)
|
|
85
121
|
├── page/ 插件页:index.html / app.js / style.css / icon.*
|
|
86
122
|
├── preview/ 本地预览:mock 宿主 + 演示快照(只给人看界面)
|
|
@@ -101,7 +137,7 @@ node preview/serve.mjs --demo # http://127.0.0.1:55133
|
|
|
101
137
|
## 开发
|
|
102
138
|
|
|
103
139
|
```bash
|
|
104
|
-
# 测试(
|
|
140
|
+
# 测试(58 个用例:判型/校验/改写/选人/快照/钩子端到端/options 回读/agent-purge 语义/注入判定)
|
|
105
141
|
node --test --test-isolation=none "tests/*.test.mjs"
|
|
106
142
|
|
|
107
143
|
# 离线清点,看看本机 purge 目录里都认得什么
|
|
@@ -124,6 +160,17 @@ node tools/scan.mjs --json
|
|
|
124
160
|
| 页面 | 人设选择 + 导入 | 条目列表 + 检查器 + 模式 + 回滚 + 校验记录 |
|
|
125
161
|
| 快照 | 生成脚本写 `personas.js` | 后端每次扫描写 `catalog.js`,页面不读盘 |
|
|
126
162
|
| 版本 | 无 | 同 id 新摘要算新版本,留两版可回滚 |
|
|
163
|
+
| 与 agent-purge 的关系 | 读 `state.json` 但页面改不了模式 | **认它的 state.json**(mode/persona/overrides/disabledHarnesses),同时页面设置优先,两边都能改 |
|
|
164
|
+
| 状态可见性 | 无 | 顶部状态条直接回答「注不注、注谁、为什么没注」 |
|
|
165
|
+
|
|
166
|
+
## 版本
|
|
167
|
+
|
|
168
|
+
| 版本 | 要点 |
|
|
169
|
+
| --- | --- |
|
|
170
|
+
| `0.2.0` | 认 agent-purge 的 `state.json`(原生语义);页面加「当前注入」状态条与可点的下一步;导入素材后不再一头雾水 |
|
|
171
|
+
| `0.1.2` | README 版本表 |
|
|
172
|
+
| `0.1.1` | 修掉「没流量时插件页是空的」:钩子模块加载时先从 `plugins.json` 读回自己的 options 再预热扫描 |
|
|
173
|
+
| `0.1.0` | 首个版本 |
|
|
127
174
|
|
|
128
175
|
## 许可
|
|
129
176
|
|
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 {
|
|
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
|
|
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
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
+
}
|
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),
|
package/lib/plan.mjs
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
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
|
+
const auto = personas.find((e) => options.enabled?.[e.id] !== false)
|
|
61
|
+
if (auto) {
|
|
62
|
+
personaId = auto.id
|
|
63
|
+
personaFrom = 'auto'
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!personaId) {
|
|
68
|
+
if (missingPersona) {
|
|
69
|
+
return {
|
|
70
|
+
...blank(modeFrom, mode, '', 'persona-missing'),
|
|
71
|
+
blocked: `指定的「${missingPersona}」不在当前来源里:把它放进来源目录,或在这里挑一个`,
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const why = personas.length
|
|
75
|
+
? '还没有选人设:左边挑一条「人设」点“设为当前人设”'
|
|
76
|
+
: '来源里还没有人设(.md):导入一份,或把 agent-purge 的 personas 目录加进来源'
|
|
77
|
+
return { ...blank(modeFrom, mode, '', personas.length ? 'no-persona' : 'no-persona-file'), blocked: why }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const persona = byId.get(personaId)
|
|
81
|
+
const skills = entries
|
|
82
|
+
.filter((e) => e.kind !== 'persona' && options.enabled?.[e.id] === true)
|
|
83
|
+
.map((e) => ({ id: e.id, name: e.name, text: e.text }))
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
mode,
|
|
87
|
+
modeFrom,
|
|
88
|
+
personaId,
|
|
89
|
+
personaName: persona?.name || personaId,
|
|
90
|
+
personaFrom,
|
|
91
|
+
skills,
|
|
92
|
+
blocked: '',
|
|
93
|
+
blockedCode: '',
|
|
94
|
+
ready: true,
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function blank(modeFrom, mode, blocked, blockedCode = '') {
|
|
99
|
+
return { mode: mode || 'off', modeFrom, personaId: '', personaName: '', personaFrom: '', skills: [], blocked, blockedCode, ready: false }
|
|
100
|
+
}
|
package/lib/state.mjs
CHANGED
|
@@ -18,6 +18,8 @@ import {
|
|
|
18
18
|
scan,
|
|
19
19
|
snapshotOf,
|
|
20
20
|
} from './inventory.mjs'
|
|
21
|
+
import { readState } from './agentpurge.mjs'
|
|
22
|
+
import { resolve as resolvePlan } from './plan.mjs'
|
|
21
23
|
|
|
22
24
|
const IMPORT_EXT = new Set(['.zip', '.md', '.markdown', '.txt', '.json'])
|
|
23
25
|
|
|
@@ -121,7 +123,8 @@ export function sync(here, options = {}, { force = false, limits = LIMITS, now =
|
|
|
121
123
|
// 导入的副本本身就是一个来源目录:放在最前面,页面能看到「来源:导入」。
|
|
122
124
|
const scanned = scan([p.importsDir, ...sources], { limits, now })
|
|
123
125
|
const { entries, duplicates } = mergeEntries(scanned.entries, previous.entries)
|
|
124
|
-
|
|
126
|
+
// 重复不算「拒绝」:同一个目录每次重扫都会碰上它们,写进拒绝记录会把真正的问题埋掉。
|
|
127
|
+
const rejected = scanned.rejected
|
|
125
128
|
|
|
126
129
|
const catalog = {
|
|
127
130
|
version: (previous.version || 0) + 1,
|
|
@@ -130,6 +133,7 @@ export function sync(here, options = {}, { force = false, limits = LIMITS, now =
|
|
|
130
133
|
sources: scanned.sources,
|
|
131
134
|
entries,
|
|
132
135
|
rejected,
|
|
136
|
+
duplicates: duplicates.map((d) => ({ name: d.name, rel: d.source.rel, root: d.source.root })),
|
|
133
137
|
imports: importRows,
|
|
134
138
|
}
|
|
135
139
|
writeFileSync(p.catalogFile, JSON.stringify(catalog, null, 2), 'utf8')
|
|
@@ -138,7 +142,7 @@ export function sync(here, options = {}, { force = false, limits = LIMITS, now =
|
|
|
138
142
|
return { catalog, changed: true, snapshot }
|
|
139
143
|
}
|
|
140
144
|
|
|
141
|
-
/** snapshotFrom 合成页面快照:条目元数据 +
|
|
145
|
+
/** snapshotFrom 合成页面快照:条目元数据 + 当前设置 + 「这次会注入什么」的判定。 */
|
|
142
146
|
export function snapshotFrom(p, catalog, options, now = Date.now()) {
|
|
143
147
|
const snap = snapshotOf(catalog.entries, catalog.sources, catalog.rejected, {
|
|
144
148
|
version: catalog.version,
|
|
@@ -146,7 +150,13 @@ export function snapshotFrom(p, catalog, options, now = Date.now()) {
|
|
|
146
150
|
options: pickOptions(options),
|
|
147
151
|
})
|
|
148
152
|
snap.imports = catalog.imports || []
|
|
153
|
+
snap.duplicates = catalog.duplicates || []
|
|
149
154
|
snap.last = loadLast(p)
|
|
155
|
+
// 页面要能回答「现在到底注不注入、注谁、为什么没注」:这里就把答案算好
|
|
156
|
+
const usable = (catalog.entries || []).filter((e) => e.status === 'ok' && e.injectable && typeof e.text === 'string' && e.text.trim())
|
|
157
|
+
const state = readState()
|
|
158
|
+
snap.purge = { bound: state.bound, mode: state.mode, persona: state.persona, disabled: state.disabled, error: state.error }
|
|
159
|
+
snap.plan = resolvePlan({ options, state, entries: usable })
|
|
150
160
|
return snap
|
|
151
161
|
}
|
|
152
162
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fish-under-sea/magpie-purge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Purge in Magpie: a magpie page plugin that treats a local purge folder as a free plugin/skill source, inspects and validates what is in it, and rewrites the gateway's system prompt by mode (off / strict / replace / append).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
},
|
|
57
57
|
"options": {
|
|
58
58
|
"sources": [],
|
|
59
|
-
"mode": "
|
|
59
|
+
"mode": "",
|
|
60
60
|
"persona": "",
|
|
61
61
|
"agents": [],
|
|
62
62
|
"enabled": {},
|
package/page/app.js
CHANGED
|
@@ -24,6 +24,10 @@
|
|
|
24
24
|
replace: '人设放最前,宿主 system 里「自称 + 点名模型/厂商」的行删掉。',
|
|
25
25
|
append: '人设放最前,宿主 system 原样保留。',
|
|
26
26
|
}
|
|
27
|
+
/** NOT_INJECTABLE 说清「这条为什么不会进 system」,并指出下一步:导入的是素材,不是人设。 */
|
|
28
|
+
const NOT_INJECTABLE =
|
|
29
|
+
'素材条目:可查、可归档,不会写进 system。要注入请导入人设或技能(.md / .markdown / .txt),' +
|
|
30
|
+
'或在下方「从本机选文件」里挑一个 md。'
|
|
27
31
|
|
|
28
32
|
let options = {}
|
|
29
33
|
let catalog = window.PURGE_CATALOG ?? null
|
|
@@ -255,7 +259,7 @@
|
|
|
255
259
|
el('div', { class: 'head' },
|
|
256
260
|
el('div', {},
|
|
257
261
|
el('h2', { text: e.name }),
|
|
258
|
-
el('p', { class: 'note', text: e.meta?.description || (e.injectable ? '可写进 system 段的人设/技能文本。' :
|
|
262
|
+
el('p', { class: 'note', text: e.meta?.description || (e.injectable ? '可写进 system 段的人设/技能文本。' : NOT_INJECTABLE) }),
|
|
259
263
|
),
|
|
260
264
|
el('span', { class: 'kind', style: `color:${kind.color}` }, kind.label),
|
|
261
265
|
),
|
|
@@ -303,7 +307,15 @@
|
|
|
303
307
|
const host = $('last')
|
|
304
308
|
const last = catalog?.last
|
|
305
309
|
if (!last) {
|
|
306
|
-
host.replaceChildren(el('p', { class: 'note', text: '
|
|
310
|
+
host.replaceChildren(el('p', { class: 'note', text: '还没有请求经过网关。改完模式后,下一次 agent 请求会写下这一行。' }))
|
|
311
|
+
return
|
|
312
|
+
}
|
|
313
|
+
if (last.skipped) {
|
|
314
|
+
// 放行了也要说清为什么:只显示「没注入」等于什么也没说
|
|
315
|
+
host.replaceChildren(
|
|
316
|
+
el('p', { class: 'note', text: `${fmtTime(last.at)} · ${last.agent || '未知 agent'} · ${last.protocol || '—'}` }),
|
|
317
|
+
el('p', { class: 'warn', text: `这次没有注入:${last.skipped}` }),
|
|
318
|
+
)
|
|
307
319
|
return
|
|
308
320
|
}
|
|
309
321
|
host.replaceChildren(
|
|
@@ -320,8 +332,9 @@
|
|
|
320
332
|
const shown = visible().length
|
|
321
333
|
$('count').textContent = `${shown} / ${entries().length + inlineEntries().length} 条 · 快照 v${catalog.version} · ${fmtTime(catalog.generatedAt)}`
|
|
322
334
|
}
|
|
323
|
-
$('mode').value = MODES().includes(options.mode) ? options.mode : '
|
|
324
|
-
$('mode-help').textContent =
|
|
335
|
+
$('mode').value = MODES().includes(options.mode) ? options.mode : ''
|
|
336
|
+
$('mode-help').textContent = modeHelp($('mode').value)
|
|
337
|
+
renderPlan()
|
|
325
338
|
renderFilters()
|
|
326
339
|
renderList()
|
|
327
340
|
renderInspector()
|
|
@@ -330,7 +343,81 @@
|
|
|
330
343
|
renderSources()
|
|
331
344
|
}
|
|
332
345
|
|
|
333
|
-
|
|
346
|
+
/** MODES 是模式下拉的全部取值:空串 = 跟随 agent-purge。 */
|
|
347
|
+
const MODES = () => ['', 'off', 'strict', 'replace', 'append']
|
|
348
|
+
|
|
349
|
+
/** modeHelp 说清当前值到底意味着什么,尤其是「跟随 agent-purge」时用的是它的什么值。 */
|
|
350
|
+
function modeHelp(value) {
|
|
351
|
+
if (value) return MODE_HELP[value] || ''
|
|
352
|
+
const p = catalog?.purge
|
|
353
|
+
if (!p?.bound) return '没装 agent-purge 或读不到它的 state.json:等价于 off,不会改请求。'
|
|
354
|
+
if (p.error) return `agent-purge 的 state.json 有问题:${p.error};等价于 off。`
|
|
355
|
+
return `跟随 agent-purge:当前 ${p.mode || '未设置'}${p.persona ? ` · 人设 ${p.persona}` : ''}`
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* renderPlan 是「现在到底注不注、注谁、为什么没注」那一行。
|
|
360
|
+
* 判定由后端算好放在快照的 plan 里(页面不能读 agent-purge 的 state.json)。
|
|
361
|
+
*/
|
|
362
|
+
function renderPlan() {
|
|
363
|
+
const host = $('plan')
|
|
364
|
+
const plan = catalog?.plan
|
|
365
|
+
if (!plan) {
|
|
366
|
+
host.dataset.state = 'idle'
|
|
367
|
+
host.replaceChildren(
|
|
368
|
+
el('span', { class: 'dot' }),
|
|
369
|
+
el('span', { class: 'empty', text: '还没有判定:等后端第一次扫描' }),
|
|
370
|
+
)
|
|
371
|
+
return
|
|
372
|
+
}
|
|
373
|
+
const ready = Boolean(plan.ready)
|
|
374
|
+
const fromText = { page: '本页设置', 'agent-purge': 'agent-purge', auto: '自动(第一个启用的人设)', default: '默认' }
|
|
375
|
+
host.dataset.state = ready ? 'ready' : 'blocked'
|
|
376
|
+
|
|
377
|
+
// 没就绪时给一个能点的下一步:光说「不会注入」等于把问题丢回给人。
|
|
378
|
+
// 每个按钮都顺带 refreshAt:宿主重建插件进程后按新设置重扫,plan 才会跟着变。
|
|
379
|
+
const fix = []
|
|
380
|
+
const fixed = (patch, note) => {
|
|
381
|
+
save({ ...patch, refreshAt: Date.now() }, note)
|
|
382
|
+
pollSnapshot('等后端按新设置重扫…', 25)
|
|
383
|
+
}
|
|
384
|
+
if (!ready) {
|
|
385
|
+
if (plan.blockedCode === 'no-mode' || plan.blockedCode === 'mode-off') {
|
|
386
|
+
fix.push(el('button', { type: 'button', class: 'primary', onclick: () => fixed({ mode: 'replace' }, '模式改为 replace') }, '用 replace 开始注入'))
|
|
387
|
+
}
|
|
388
|
+
if (plan.blockedCode === 'no-persona' || plan.blockedCode === 'persona-missing') {
|
|
389
|
+
fix.push(el('button', {
|
|
390
|
+
type: 'button',
|
|
391
|
+
class: 'primary',
|
|
392
|
+
onclick: () => { filter = 'persona'; query = ''; render() },
|
|
393
|
+
}, '去看人设'))
|
|
394
|
+
}
|
|
395
|
+
if (plan.blockedCode === 'no-persona-file') {
|
|
396
|
+
fix.push(el('button', {
|
|
397
|
+
type: 'button',
|
|
398
|
+
class: 'primary',
|
|
399
|
+
onclick: () => { $('import-file')?.focus(); $('import-file')?.click?.() },
|
|
400
|
+
}, '导入一份人设'))
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const rows = [
|
|
405
|
+
el('span', { class: 'dot' }),
|
|
406
|
+
el('span', { class: ready ? 'who' : 'empty' }, ready
|
|
407
|
+
? `下一次 agent 请求会注入「${plan.personaName || plan.personaId}」`
|
|
408
|
+
: '现在不会注入:请求原样放行'),
|
|
409
|
+
el('span', { class: 'actions' },
|
|
410
|
+
el('span', { class: 'tag', text: `模式 ${plan.mode} · ${fromText[plan.modeFrom] || plan.modeFrom || '默认'}` }),
|
|
411
|
+
ready && plan.skills?.length ? el('span', { class: 'tag', text: `技能 ${plan.skills.length} 条` }) : null,
|
|
412
|
+
...fix,
|
|
413
|
+
el('button', { type: 'button', class: 'ghost', onclick: () => refresh() }, '重扫确认'),
|
|
414
|
+
),
|
|
415
|
+
el('span', { class: 'why', text: ready
|
|
416
|
+
? `人设来源:${fromText[plan.personaFrom] || plan.personaFrom}${catalog?.purge?.bound ? ';agent-purge 的 state.json 已读到' : ';没读到 agent-purge 的 state.json'}`
|
|
417
|
+
: `${plan.blocked}${catalog?.purge?.bound ? '' : '(没读到 agent-purge 的 state.json)'}` }),
|
|
418
|
+
]
|
|
419
|
+
host.replaceChildren(...rows.filter(Boolean))
|
|
420
|
+
}
|
|
334
421
|
|
|
335
422
|
function renderSources() {
|
|
336
423
|
const el2 = $('sources')
|
|
@@ -394,7 +481,10 @@
|
|
|
394
481
|
}
|
|
395
482
|
|
|
396
483
|
function wire() {
|
|
397
|
-
$('mode').addEventListener('change', (e) =>
|
|
484
|
+
$('mode').addEventListener('change', (e) => {
|
|
485
|
+
const value = e.target.value
|
|
486
|
+
save({ mode: value }, value ? `模式改为 ${value}` : '改为跟随 agent-purge')
|
|
487
|
+
})
|
|
398
488
|
$('refresh').addEventListener('click', refresh)
|
|
399
489
|
$('q').addEventListener('input', (e) => { query = e.target.value; render() })
|
|
400
490
|
|
package/page/index.html
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
<span class="mode-label" id="mode-label">改写模式</span>
|
|
24
24
|
<div class="mode-row">
|
|
25
25
|
<select id="mode" aria-labelledby="mode-label">
|
|
26
|
+
<option value="">跟随 agent-purge</option>
|
|
26
27
|
<option value="off">off · 不动请求</option>
|
|
27
28
|
<option value="strict">strict · 只留人设</option>
|
|
28
29
|
<option value="replace">replace · 人设 + 去掉自称</option>
|
|
@@ -40,6 +41,8 @@
|
|
|
40
41
|
页面不读磁盘:条目由插件后端扫描后写成快照,页面只显示它。
|
|
41
42
|
</p>
|
|
42
43
|
|
|
44
|
+
<section class="plan" id="plan" aria-label="当前注入状态" aria-live="polite"></section>
|
|
45
|
+
|
|
43
46
|
<div class="grid">
|
|
44
47
|
<section aria-label="条目列表">
|
|
45
48
|
<div class="rail-head">
|
package/page/style.css
CHANGED
|
@@ -78,6 +78,36 @@ textarea { width: 100%; resize: vertical; font-family: var(--mono); font-size: 1
|
|
|
78
78
|
border-radius: 0 8px 8px 0;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
+
/* 当前注入:这一行回答「到底注不注、注谁、为什么没注」 */
|
|
82
|
+
.plan {
|
|
83
|
+
display: grid;
|
|
84
|
+
grid-template-columns: max-content minmax(0, 1fr) max-content;
|
|
85
|
+
gap: 4px 12px;
|
|
86
|
+
align-items: center;
|
|
87
|
+
margin: 0 0 16px;
|
|
88
|
+
padding: 10px 13px;
|
|
89
|
+
border: 1px solid var(--line);
|
|
90
|
+
border-radius: 10px;
|
|
91
|
+
background: var(--card);
|
|
92
|
+
}
|
|
93
|
+
.plan[data-state="ready"] { border-color: color-mix(in oklab, var(--kind-plugin) 45%, var(--line)); }
|
|
94
|
+
.plan[data-state="blocked"] { border-color: color-mix(in oklab, var(--danger) 35%, var(--line)); }
|
|
95
|
+
.plan .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
|
|
96
|
+
.plan[data-state="ready"] .dot { background: var(--kind-plugin); }
|
|
97
|
+
.plan[data-state="blocked"] .dot { background: var(--danger); }
|
|
98
|
+
.plan .who { font-weight: 500; }
|
|
99
|
+
.plan .why { grid-column: 2 / -1; color: var(--muted); font-size: 12px; }
|
|
100
|
+
.plan .tag {
|
|
101
|
+
font-size: 12px;
|
|
102
|
+
padding: 1px 7px;
|
|
103
|
+
border-radius: 999px;
|
|
104
|
+
border: 1px solid var(--line);
|
|
105
|
+
color: var(--muted);
|
|
106
|
+
white-space: nowrap;
|
|
107
|
+
}
|
|
108
|
+
.plan .actions { display: flex; gap: 8px; align-items: center; }
|
|
109
|
+
.plan .empty { color: var(--muted); }
|
|
110
|
+
|
|
81
111
|
/* ---------- 主体:左列表 / 右检查器 ---------- */
|
|
82
112
|
.grid {
|
|
83
113
|
display: grid;
|
package/preview/bridge.js
CHANGED
|
@@ -22,8 +22,9 @@
|
|
|
22
22
|
for (const [k, v] of Object.entries(tokens)) root.style.setProperty('--magpie-' + k, v)
|
|
23
23
|
|
|
24
24
|
// 预览里 options 只活在内存:刷新页面就回到默认值。
|
|
25
|
+
// 第一次 get 时用演示快照的 options 打底——bridge 先于 catalog.js 加载,所以只能惰性取。
|
|
25
26
|
const store = {
|
|
26
|
-
mode: '
|
|
27
|
+
mode: '',
|
|
27
28
|
persona: '',
|
|
28
29
|
agents: [],
|
|
29
30
|
enabled: {},
|
|
@@ -32,6 +33,7 @@
|
|
|
32
33
|
refreshAt: 0,
|
|
33
34
|
inline: [],
|
|
34
35
|
}
|
|
36
|
+
let seeded = false
|
|
35
37
|
|
|
36
38
|
const api = {
|
|
37
39
|
theme: { dark, lang: 'zh-CN', tokens },
|
|
@@ -46,9 +48,15 @@
|
|
|
46
48
|
onAction() {},
|
|
47
49
|
options: {
|
|
48
50
|
async get() {
|
|
51
|
+
if (!seeded) {
|
|
52
|
+
seeded = true
|
|
53
|
+
const snapshot = window.PURGE_CATALOG?.options
|
|
54
|
+
if (snapshot && typeof snapshot === 'object') Object.assign(store, snapshot)
|
|
55
|
+
}
|
|
49
56
|
return JSON.parse(JSON.stringify(store))
|
|
50
57
|
},
|
|
51
58
|
async set(next) {
|
|
59
|
+
seeded = true
|
|
52
60
|
Object.assign(store, JSON.parse(JSON.stringify(next || {})))
|
|
53
61
|
return JSON.parse(JSON.stringify(store))
|
|
54
62
|
},
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// 演示快照(没就绪的那种):只有素材、没人设、模式也没选。
|
|
2
|
+
// 用来复看「导入了一个 zip,为什么不会注入」这一屏。
|
|
3
|
+
// node preview/serve.mjs --demo=blocked
|
|
4
|
+
window.PURGE_CATALOG = {
|
|
5
|
+
version: 9,
|
|
6
|
+
generatedAt: 1794564000000,
|
|
7
|
+
sources: [
|
|
8
|
+
{ path: 'D:/Fish-code/DSH/purge', ok: true, count: 1, error: '' },
|
|
9
|
+
{ path: 'C:/Users/you/.agent-purge', ok: false, count: 0, error: '目录不存在或没有权限' },
|
|
10
|
+
{ path: 'C:/Users/you/.magpie/purge', ok: false, count: 0, error: '目录不存在或没有权限' },
|
|
11
|
+
],
|
|
12
|
+
entries: [
|
|
13
|
+
{
|
|
14
|
+
id: '5556',
|
|
15
|
+
name: '5556',
|
|
16
|
+
kind: 'session-archive',
|
|
17
|
+
status: 'ok',
|
|
18
|
+
reason: '',
|
|
19
|
+
source: { root: 'D:/Fish-code/DSH/purge', rel: 'Anima loli tag.zip' },
|
|
20
|
+
size: 177986,
|
|
21
|
+
digest: '948c5f9615e0',
|
|
22
|
+
importedAt: 1794564000000,
|
|
23
|
+
version: 1,
|
|
24
|
+
meta: { format: 'dsh-session-archive', version: 1, sessions: 1, turns: 12, cwd: '/root/comfy ui' },
|
|
25
|
+
chars: 0,
|
|
26
|
+
injectable: false,
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
rejected: [],
|
|
30
|
+
options: { mode: '', persona: '', enabled: {}, agents: [] },
|
|
31
|
+
imports: [],
|
|
32
|
+
purge: { bound: false, mode: '', persona: '', disabled: [], error: '' },
|
|
33
|
+
plan: {
|
|
34
|
+
mode: 'off',
|
|
35
|
+
modeFrom: 'default',
|
|
36
|
+
personaId: '',
|
|
37
|
+
personaName: '',
|
|
38
|
+
personaFrom: '',
|
|
39
|
+
skills: [],
|
|
40
|
+
blocked: '还没选模式:模式是 off,请求原样放行。选一个模式(比如 replace)就开始注入',
|
|
41
|
+
blockedCode: 'no-mode',
|
|
42
|
+
ready: false,
|
|
43
|
+
},
|
|
44
|
+
last: null,
|
|
45
|
+
}
|
package/preview/catalog.demo.js
CHANGED
|
@@ -80,6 +80,18 @@ window.PURGE_CATALOG = {
|
|
|
80
80
|
],
|
|
81
81
|
options: { mode: 'replace', persona: 'anima', enabled: { outline: true }, agents: [] },
|
|
82
82
|
imports: [],
|
|
83
|
+
purge: { bound: true, mode: 'replace', persona: 'anima', disabled: [], error: '' },
|
|
84
|
+
plan: {
|
|
85
|
+
mode: 'replace',
|
|
86
|
+
modeFrom: 'page',
|
|
87
|
+
personaId: 'anima',
|
|
88
|
+
personaName: 'Anima',
|
|
89
|
+
personaFrom: 'page',
|
|
90
|
+
skills: [{ id: 'outline', name: '提纲', text: '先列三层' }],
|
|
91
|
+
blocked: '',
|
|
92
|
+
blockedCode: '',
|
|
93
|
+
ready: true,
|
|
94
|
+
},
|
|
83
95
|
last: {
|
|
84
96
|
at: 1794561421000,
|
|
85
97
|
agent: 'claude',
|
package/preview/serve.mjs
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
// 不实现网关、不跑 hooks、不碰真实 options;主题跟随 magpie 的变量名,
|
|
4
4
|
// 明暗由 ?theme=dark|light 决定。
|
|
5
5
|
//
|
|
6
|
-
// node preview/serve.mjs
|
|
7
|
-
// node preview/serve.mjs --demo
|
|
6
|
+
// node preview/serve.mjs → http://127.0.0.1:55133
|
|
7
|
+
// node preview/serve.mjs --demo → 用 preview/catalog.demo.js 顶替真实快照
|
|
8
|
+
// node preview/serve.mjs --demo=blocked → 只有素材、没选模式那一屏
|
|
8
9
|
//
|
|
9
10
|
// 真机里 page/catalog.js 由 hooks.js 扫描后写出;预览里要么用它,要么用演示快照。
|
|
10
11
|
import http from 'node:http'
|
|
@@ -14,7 +15,8 @@ import { fileURLToPath } from 'node:url'
|
|
|
14
15
|
|
|
15
16
|
const here = fileURLToPath(new URL('.', import.meta.url))
|
|
16
17
|
const root = normalize(join(here, '..'))
|
|
17
|
-
const
|
|
18
|
+
const demoArg = process.argv.find((a) => a === '--demo' || a.startsWith('--demo='))
|
|
19
|
+
const demoFile = demoArg ? (demoArg.includes('=') ? `catalog.${demoArg.split('=')[1]}.js` : 'catalog.demo.js') : ''
|
|
18
20
|
const port = Number(process.env.PREVIEW_PORT || 55133)
|
|
19
21
|
|
|
20
22
|
const mime = {
|
|
@@ -42,8 +44,8 @@ const server = http.createServer(async (req, res) => {
|
|
|
42
44
|
if (pathname.endsWith('/_magpie/bridge.js')) {
|
|
43
45
|
return send(await readFile(join(here, 'bridge.js'), 'utf8'), mime['.js'])
|
|
44
46
|
}
|
|
45
|
-
if (pathname.endsWith('/catalog.js') &&
|
|
46
|
-
return send(await readFile(join(here,
|
|
47
|
+
if (pathname.endsWith('/catalog.js') && demoFile) {
|
|
48
|
+
return send(await readFile(join(here, demoFile), 'utf8'), mime['.js'])
|
|
47
49
|
}
|
|
48
50
|
const file = pathname.startsWith('/page/') ? join(root, 'page', pathname.slice(6)) : join(root, pathname)
|
|
49
51
|
if (!file.startsWith(root)) return send('forbidden', 'text/plain', 403)
|
|
@@ -54,5 +56,5 @@ const server = http.createServer(async (req, res) => {
|
|
|
54
56
|
})
|
|
55
57
|
|
|
56
58
|
server.listen(port, '127.0.0.1', () => {
|
|
57
|
-
console.log(`purge preview: http://127.0.0.1:${port} (?theme=light 切浅色${
|
|
59
|
+
console.log(`purge preview: http://127.0.0.1:${port} (?theme=light 切浅色${demoFile ? `,演示快照 ${demoFile}` : ''})`)
|
|
58
60
|
})
|