@fish-under-sea/magpie-purge 0.4.0 → 0.4.2
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 +19 -6
- package/hooks.js +7 -3
- package/lib/inventory.mjs +0 -15
- package/lib/plan.mjs +9 -3
- package/lib/state.mjs +44 -21
- package/package.json +1 -1
- package/page/app.js +214 -64
- package/page/index.html +11 -12
- package/page/style.css +3 -0
- package/preview/catalog.demo.js +12 -4
- package/preview/host.html +3 -2
- package/preview/purge-preview.html +240 -79
- package/tools/scan.mjs +11 -9
package/page/app.js
CHANGED
|
@@ -48,6 +48,10 @@
|
|
|
48
48
|
let busy = false
|
|
49
49
|
/** showingText 是「查看原文」的展开态;换条目时要收回去,否则新条目一进来就是展开的。 */
|
|
50
50
|
let showingText = false
|
|
51
|
+
/** pendingFile 是「点过选择、还没点导入」的那个文件;两步导入靠它接上。 */
|
|
52
|
+
let pendingFile = null
|
|
53
|
+
/** importKind 是导入时选的类型:auto 交给判定,persona/skill 覆盖它。 */
|
|
54
|
+
let importKind = 'auto'
|
|
51
55
|
/** confirmedSig 记「用户按下确认时的那套配置」;配置一变就失效,退回「确认注入」。 */
|
|
52
56
|
let confirmedSig = ''
|
|
53
57
|
/** modeMenuOpen 是模式下拉面板是否展开;每次重画都要记住,否则点一下就收。 */
|
|
@@ -92,8 +96,42 @@
|
|
|
92
96
|
|
|
93
97
|
const entries = () => (Array.isArray(catalog?.entries) ? catalog.entries : [])
|
|
94
98
|
const inlineEntries = () => (Array.isArray(options.inline) ? options.inline : [])
|
|
95
|
-
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* shaped 给「页面导入」的条目补齐其它条目都有的字段。
|
|
102
|
+
*
|
|
103
|
+
* 列表与检查器必须看同一份形状:之前只有列表补了,检查器拿到的是裸对象,
|
|
104
|
+
* 于是 source 缺失 → 认不出是 inline → 不给删除按钮、还把它当素材。
|
|
105
|
+
*/
|
|
106
|
+
const shaped = (e) => ({
|
|
107
|
+
...e,
|
|
108
|
+
status: 'ok',
|
|
109
|
+
reason: '',
|
|
110
|
+
source: { rel: '页面导入(options)', root: 'inline' },
|
|
111
|
+
size: 0,
|
|
112
|
+
meta: { format: 'inline', description: '从「导入」里挑进来的文件,正文存在插件的 options 里。' },
|
|
113
|
+
version: e.version ?? 1,
|
|
114
|
+
importedAt: 0,
|
|
115
|
+
chars: String(e.text || '').length,
|
|
116
|
+
injectable: true,
|
|
117
|
+
builtin: false,
|
|
118
|
+
digest: e.digest || '',
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
// findEntry 统一走 shaped:检查器与列表必须看到同一份对象,否则判定会打架。
|
|
122
|
+
const findEntry = (id) =>
|
|
123
|
+
entries().find((e) => e.id === id) ||
|
|
124
|
+
(() => {
|
|
125
|
+
const hit = inlineEntries().find((e) => e.id === id)
|
|
126
|
+
return hit ? shaped(hit) : null
|
|
127
|
+
})()
|
|
96
128
|
const isEnabled = (id) => (options.enabled?.[id] !== undefined ? options.enabled[id] : findEntry(id)?.kind === 'persona')
|
|
129
|
+
/** defaultPersona 是「现在最能顶上的人设」:内置底座优先,其次第一个可用的人设。 */
|
|
130
|
+
const defaultPersona = () =>
|
|
131
|
+
entries().find((e) => e.kind === 'persona' && e.default === true) ||
|
|
132
|
+
entries().find((e) => e.kind === 'persona') ||
|
|
133
|
+
inlineEntries().find((e) => e.kind !== 'skill') ||
|
|
134
|
+
null
|
|
97
135
|
|
|
98
136
|
// ---------- 与宿主说话 ----------
|
|
99
137
|
|
|
@@ -110,11 +148,18 @@
|
|
|
110
148
|
|
|
111
149
|
async function flush() {
|
|
112
150
|
if (!pendingSave || !bridge?.options?.set) return
|
|
151
|
+
// 记住这一轮自己刚写下去的键:宿主回包只用来「补全它规范化的字段」,
|
|
152
|
+
// 不许它把我们刚设的值盖回去——否则「选了模式又被换回」会反复发生。
|
|
153
|
+
const sent = { ...(pendingSave || {}) }
|
|
113
154
|
const body = { ...options }
|
|
114
155
|
pendingSave = null
|
|
115
156
|
try {
|
|
116
157
|
const saved = await bridge.options.set(body)
|
|
117
|
-
if (saved && typeof saved === 'object')
|
|
158
|
+
if (saved && typeof saved === 'object') {
|
|
159
|
+
const merged = { ...body, ...saved }
|
|
160
|
+
for (const key of Object.keys(sent)) merged[key] = body[key]
|
|
161
|
+
options = merged
|
|
162
|
+
}
|
|
118
163
|
} catch (error) {
|
|
119
164
|
say(`保存失败:${error?.message || error}`, 'bad')
|
|
120
165
|
}
|
|
@@ -148,17 +193,7 @@
|
|
|
148
193
|
inlineEntries()
|
|
149
194
|
.filter((e) => (filter ? e.kind === filter : true))
|
|
150
195
|
.filter((e) => (q ? `${e.name} ${e.id}`.toLowerCase().includes(q) : true))
|
|
151
|
-
.map(
|
|
152
|
-
...e,
|
|
153
|
-
status: 'ok',
|
|
154
|
-
source: { rel: '页面导入(options)', root: 'inline' },
|
|
155
|
-
size: 0,
|
|
156
|
-
meta: { format: 'inline', description: '' },
|
|
157
|
-
version: 1,
|
|
158
|
-
chars: String(e.text || '').length,
|
|
159
|
-
injectable: true,
|
|
160
|
-
digest: '',
|
|
161
|
-
})),
|
|
196
|
+
.map(shaped),
|
|
162
197
|
)
|
|
163
198
|
}
|
|
164
199
|
|
|
@@ -170,7 +205,7 @@
|
|
|
170
205
|
return
|
|
171
206
|
}
|
|
172
207
|
if (!rows.length) {
|
|
173
|
-
host.replaceChildren(el('p', { class: 'empty', text: '
|
|
208
|
+
host.replaceChildren(el('p', { class: 'empty', text: '没有匹配的条目。清掉搜索,或导入一份文件再来。' }))
|
|
174
209
|
return
|
|
175
210
|
}
|
|
176
211
|
// 分组比逐行挂「内置」胶囊干净:内置是「这一整组都随包来」,说一次就够。
|
|
@@ -327,8 +362,9 @@
|
|
|
327
362
|
const kind = KINDS[e.kind] || { label: e.kind }
|
|
328
363
|
const kindCls = kindClass(e.kind)
|
|
329
364
|
const inline = e.source?.root === 'inline' || e.meta?.format === 'inline'
|
|
330
|
-
// 只有复制进插件自己的 state/imports
|
|
331
|
-
|
|
365
|
+
// 只有复制进插件自己的 state/imports 的副本才谈得上「删副本」:原件不动。
|
|
366
|
+
// root 可能是绝对路径(真机)也可能是相对路径(演示快照),所以不要求前导分隔符。
|
|
367
|
+
const imported = Boolean(e.source?.root && /(^|[\\/])state[\\/]imports\/?$/.test(e.source.root))
|
|
332
368
|
const enabled = isEnabled(e.id)
|
|
333
369
|
const pinned = options.pinned?.[e.id]
|
|
334
370
|
const history = Array.isArray(e.history) ? e.history : []
|
|
@@ -377,18 +413,53 @@
|
|
|
377
413
|
type: 'button',
|
|
378
414
|
class: 'danger',
|
|
379
415
|
onclick: () => {
|
|
416
|
+
// 删掉导入的条目时,指向它的引用也要一起清:否则 plan 会卡在 persona-missing,
|
|
417
|
+
// 用户还得再点一次「改用」才能恢复注入。
|
|
380
418
|
const kept = inlineEntries().filter((x) => x.id !== e.id)
|
|
381
419
|
const enabledNext = { ...(options.enabled || {}) }
|
|
382
420
|
delete enabledNext[e.id]
|
|
383
|
-
|
|
421
|
+
const pinnedNext = { ...(options.pinned || {}) }
|
|
422
|
+
delete pinnedNext[e.id]
|
|
423
|
+
const fallback = kept.find((x) => x.kind !== 'skill')
|
|
424
|
+
save({
|
|
425
|
+
inline: kept,
|
|
426
|
+
enabled: enabledNext,
|
|
427
|
+
pinned: pinnedNext,
|
|
428
|
+
persona: options.persona === e.id ? (fallback?.id || '') : options.persona,
|
|
429
|
+
}, `已删除「${e.name}」`)
|
|
384
430
|
if (selected === e.id) selected = ''
|
|
431
|
+
render()
|
|
385
432
|
},
|
|
386
|
-
}, '
|
|
433
|
+
}, '删除导入'))
|
|
434
|
+
// 类型选错时能就地改:重扫猜出来的类型不一定对,硬编码成「人设」更糟。
|
|
435
|
+
actions.push(el('button', {
|
|
436
|
+
type: 'button',
|
|
437
|
+
class: 'quiet',
|
|
438
|
+
onclick: () => {
|
|
439
|
+
const nextKind = e.kind === 'skill' ? 'persona' : 'skill'
|
|
440
|
+
const next = inlineEntries().map((x) => (x.id === e.id ? { ...x, kind: nextKind } : x))
|
|
441
|
+
save({ inline: next }, `已改判为${nextKind === 'skill' ? '技能' : '人设'}`)
|
|
442
|
+
render()
|
|
443
|
+
},
|
|
444
|
+
}, e.kind === 'skill' ? '改判为人设' : '改判为技能'))
|
|
387
445
|
} else if (imported) {
|
|
388
446
|
actions.push(el('button', {
|
|
389
447
|
type: 'button',
|
|
390
448
|
class: 'danger',
|
|
391
|
-
onclick: () =>
|
|
449
|
+
onclick: () => {
|
|
450
|
+
// 删副本同时把指向它的引用清掉:留着会让 plan 卡在 persona-missing。
|
|
451
|
+
const enabledNext = { ...(options.enabled || {}) }
|
|
452
|
+
delete enabledNext[e.id]
|
|
453
|
+
const pinnedNext = { ...(options.pinned || {}) }
|
|
454
|
+
delete pinnedNext[e.id]
|
|
455
|
+
save({
|
|
456
|
+
enabled: enabledNext,
|
|
457
|
+
pinned: pinnedNext,
|
|
458
|
+
persona: options.persona === e.id ? '' : options.persona,
|
|
459
|
+
})
|
|
460
|
+
importRequest({ op: 'delete', path: e.source.rel }, `已请求删除「${e.name}」的副本(只删插件自己的 state/imports)`)
|
|
461
|
+
if (selected === e.id) selected = ''
|
|
462
|
+
},
|
|
392
463
|
}, '删除副本'))
|
|
393
464
|
}
|
|
394
465
|
|
|
@@ -491,8 +562,8 @@
|
|
|
491
562
|
const n = entries().length
|
|
492
563
|
host.replaceChildren(el('li', {
|
|
493
564
|
text: n
|
|
494
|
-
? '
|
|
495
|
-
: '
|
|
565
|
+
? '没有拒绝项:现在这些文件都认得出来。'
|
|
566
|
+
: '还没有扫描结果:导入一份文件,或点一下「导入」区里的按钮触发重扫。',
|
|
496
567
|
}))
|
|
497
568
|
return
|
|
498
569
|
}
|
|
@@ -539,7 +610,6 @@
|
|
|
539
610
|
$('inspect').hidden = !picked
|
|
540
611
|
renderRejects()
|
|
541
612
|
renderLast()
|
|
542
|
-
renderSources()
|
|
543
613
|
}
|
|
544
614
|
|
|
545
615
|
/** MODES 是模式下拉的全部取值:没有「跟随上游」这一档,模式必须自己选。 */
|
|
@@ -551,9 +621,23 @@
|
|
|
551
621
|
return '还没选模式:请求原样放行。选一个(比如 replace)才开始注入。'
|
|
552
622
|
}
|
|
553
623
|
|
|
624
|
+
/**
|
|
625
|
+
* planStale 判断「快照里的判定」是否已经跟不上本地设置了。
|
|
626
|
+
*
|
|
627
|
+
* 只比这两件会直接改变结论的事:模式与生效的人设。比完就知道该不该按旧结论劝人。
|
|
628
|
+
*/
|
|
629
|
+
function planStale(plan) {
|
|
630
|
+
if (!plan) return false
|
|
631
|
+
const localMode = MODES().includes(options.mode) ? options.mode : ''
|
|
632
|
+
if (localMode && localMode !== plan.mode) return true
|
|
633
|
+
const localPersona = typeof options.persona === 'string' ? options.persona.trim() : ''
|
|
634
|
+
if (localPersona && localPersona !== plan.personaId) return true
|
|
635
|
+
return false
|
|
636
|
+
}
|
|
637
|
+
|
|
554
638
|
/**
|
|
555
639
|
* renderPlan 是「现在到底注不注、注谁、为什么没注」那一行。
|
|
556
|
-
* 判定由后端算好放在快照的 plan
|
|
640
|
+
* 判定由后端算好放在快照的 plan 里(页面不能读磁盘)。
|
|
557
641
|
*/
|
|
558
642
|
function renderPlan() {
|
|
559
643
|
const host = $('plan')
|
|
@@ -567,6 +651,10 @@
|
|
|
567
651
|
return
|
|
568
652
|
}
|
|
569
653
|
const ready = Boolean(plan.ready)
|
|
654
|
+
// plan 是后端上一次扫描算出来的;本地刚改过设置时它就过期了。
|
|
655
|
+
// 状态条左边说「已选 append」、右边却还在劝「用 replace 开始注入」,
|
|
656
|
+
// 就是因为胶囊读本地、按钮读快照。这里先认出来,过期就别再按旧的劝。
|
|
657
|
+
const stale = planStale(plan)
|
|
570
658
|
// personaFrom 的三种来源,用户一眼要能看懂「谁在顶着」:
|
|
571
659
|
// page = 自己点的、auto = 底座人设自动顶上、default = agent-purge(仅在显式开启时)
|
|
572
660
|
const fromText = { page: '本页设置', auto: '底座人设(没选人设时自动顶上)', default: '默认' }
|
|
@@ -579,37 +667,57 @@
|
|
|
579
667
|
save({ ...patch, refreshAt: Date.now() }, note)
|
|
580
668
|
pollSnapshot('等后端按新设置重扫…', 25)
|
|
581
669
|
}
|
|
670
|
+
// 本地已经选好模式时,绝不再劝「用 replace 开始注入」——
|
|
671
|
+
// 那正是图一里「写着 append、旁边却叫你换成 replace」的自相矛盾。
|
|
672
|
+
const localMode = MODES().includes(options.mode) ? options.mode : ''
|
|
673
|
+
if (!ready && !localMode) {
|
|
674
|
+
fix.push(el('button', { type: 'button', class: 'primary', onclick: () => fixed({ mode: 'replace' }, '模式改为 replace') }, '用 replace 开始注入'))
|
|
675
|
+
}
|
|
582
676
|
if (!ready) {
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
677
|
+
// 旧人设被删掉后 options.persona 会一直指着它,卡在 persona-missing:
|
|
678
|
+
// 这个问题点「去看人设」永远绕不出来,得给一个直接改掉它的动作。
|
|
679
|
+
if (plan.blockedCode === 'persona-missing' && defaultPersona()) {
|
|
680
|
+
const next = defaultPersona()
|
|
587
681
|
fix.push(el('button', {
|
|
588
682
|
type: 'button',
|
|
589
683
|
class: 'primary',
|
|
684
|
+
onclick: () => fixed({ persona: next.id }, `人设改为「${next.name}」`),
|
|
685
|
+
}, `改用「${next.name}」`))
|
|
686
|
+
}
|
|
687
|
+
if (!stale && (plan.blockedCode === 'no-persona' || plan.blockedCode === 'persona-missing')) {
|
|
688
|
+
fix.push(el('button', {
|
|
689
|
+
type: 'button',
|
|
590
690
|
onclick: () => { filter = 'persona'; query = ''; render() },
|
|
591
691
|
}, '去看人设'))
|
|
592
692
|
}
|
|
593
|
-
if (plan.blockedCode === 'no-persona-file') {
|
|
693
|
+
if (!stale && plan.blockedCode === 'no-persona-file') {
|
|
594
694
|
fix.push(el('button', {
|
|
595
695
|
type: 'button',
|
|
596
696
|
class: 'primary',
|
|
597
|
-
onclick: () => {
|
|
697
|
+
onclick: () => { filter = 'persona'; query = ''; render() },
|
|
598
698
|
}, '导入一份人设'))
|
|
599
699
|
}
|
|
600
700
|
}
|
|
601
701
|
|
|
602
702
|
// 两行文字放进同一列:第一行是结论,第二行是依据;右边的胶囊组跨两行垂直居中。
|
|
603
703
|
// 原来「为什么」那行缩进 20px,看起来像挂件;现在两行左对齐,读起来是一段话。
|
|
704
|
+
// 过期时不复述旧结论:直接说「等重扫」,免得左边写 append、右边说「还没选模式」。
|
|
705
|
+
const who = stale
|
|
706
|
+
? '设置已改,等后端重扫确认'
|
|
707
|
+
: ready
|
|
708
|
+
? `下一次 agent 请求会注入「${plan.personaName || plan.personaId}」`
|
|
709
|
+
: '现在不会注入:请求原样放行'
|
|
710
|
+
const why = stale
|
|
711
|
+
? '刚改完模式或人设:下一次请求按新设置走,这里显示的是上一次扫描的判定'
|
|
712
|
+
: ready
|
|
713
|
+
? `人设来源:${fromText[plan.personaFrom] || plan.personaFrom}`
|
|
714
|
+
: plan.blocked
|
|
715
|
+
|
|
604
716
|
const rows = [
|
|
605
717
|
el('span', { class: 'dot' }),
|
|
606
718
|
el('span', { class: 'plan-text' },
|
|
607
|
-
el('span', { class: ready ? 'who' : 'who is-empty' },
|
|
608
|
-
|
|
609
|
-
: '现在不会注入:请求原样放行'),
|
|
610
|
-
el('span', { class: 'why', text: ready
|
|
611
|
-
? `人设来源:${fromText[plan.personaFrom] || plan.personaFrom}`
|
|
612
|
-
: plan.blocked }),
|
|
719
|
+
el('span', { class: ready && !stale ? 'who' : 'who is-empty' }, who),
|
|
720
|
+
el('span', { class: 'why', text: why }),
|
|
613
721
|
),
|
|
614
722
|
el('span', { class: 'plan-controls' },
|
|
615
723
|
modePill(),
|
|
@@ -717,9 +825,13 @@
|
|
|
717
825
|
* 签名一变,「已注入」自动退回「确认注入」。
|
|
718
826
|
*/
|
|
719
827
|
function confirmButton(plan) {
|
|
720
|
-
|
|
828
|
+
// 签名用「实际会生效的那一套」:刚改了模式但后端还没重扫时,plan 是旧的,
|
|
829
|
+
// 拿它算签名会让按钮继续显示「已注入」——那等于替一套还没生效的配置背书。
|
|
830
|
+
const mode = MODES().includes(options.mode) ? options.mode : plan.mode
|
|
831
|
+
const persona = (typeof options.persona === 'string' && options.persona.trim()) || plan.personaId || ''
|
|
832
|
+
const sig = `${mode}|${persona}`
|
|
721
833
|
const last = catalog?.last
|
|
722
|
-
const evidence = Boolean(last && !last.skipped && last.mode ===
|
|
834
|
+
const evidence = Boolean(last && !last.skipped && last.mode === mode && (last.persona || '') === persona)
|
|
723
835
|
const applied = evidence || confirmedSig === sig
|
|
724
836
|
return el('button', {
|
|
725
837
|
type: 'button',
|
|
@@ -738,15 +850,6 @@
|
|
|
738
850
|
}, applied ? '已注入' : '确认注入')
|
|
739
851
|
}
|
|
740
852
|
|
|
741
|
-
function renderSources() {
|
|
742
|
-
const el2 = $('sources')
|
|
743
|
-
if (!el2 || document.activeElement === el2) return
|
|
744
|
-
const list = Array.isArray(options.sources) && options.sources.length
|
|
745
|
-
? options.sources
|
|
746
|
-
: (catalog?.sources || []).filter((s) => s.path && !/[\\/]state[\\/]imports\/?$/.test(s.path)).map((s) => s.path)
|
|
747
|
-
el2.value = list.join('\n')
|
|
748
|
-
}
|
|
749
|
-
|
|
750
853
|
// ---------- 动作 ----------
|
|
751
854
|
|
|
752
855
|
function importRequest(req, note) {
|
|
@@ -774,7 +877,7 @@
|
|
|
774
877
|
return
|
|
775
878
|
}
|
|
776
879
|
if (Date.now() < until) setTimeout(tick, 1200)
|
|
777
|
-
else say('后端还没写出新快照:magpie
|
|
880
|
+
else say('后端还没写出新快照:magpie 里插件可能没重启,或它写不进自己的 state 目录。', 'bad')
|
|
778
881
|
}
|
|
779
882
|
script.onerror = () => {
|
|
780
883
|
script.remove()
|
|
@@ -809,37 +912,84 @@
|
|
|
809
912
|
renderPlan()
|
|
810
913
|
})
|
|
811
914
|
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
915
|
+
// 导入分两步:先选文件(把路径填进输入框),再点「导入」才真的导入。
|
|
916
|
+
// 一步到底容易误触:浏览器选完就导入,用户还没确认类型就落盘了。
|
|
917
|
+
$('import-pick').addEventListener('click', () => {
|
|
918
|
+
const input = document.createElement('input')
|
|
919
|
+
input.type = 'file'
|
|
920
|
+
input.accept = '.md,.markdown,.txt,.json,.zip'
|
|
921
|
+
input.addEventListener('change', () => {
|
|
922
|
+
const file = input.files?.[0]
|
|
923
|
+
if (!file) return
|
|
924
|
+
// 浏览器出于安全不暴露真实路径,只能拿到文件名。
|
|
925
|
+
// 这一步先把名字填进去,让人看见选中了什么;真正导入走下面那条 inline 通道。
|
|
926
|
+
$('import-path').value = file.name
|
|
927
|
+
pendingFile = file
|
|
928
|
+
say(`已选中「${file.name}」,再点一次「导入」就写入`, 'good')
|
|
929
|
+
})
|
|
930
|
+
input.click()
|
|
817
931
|
})
|
|
818
932
|
|
|
819
|
-
$('import-
|
|
820
|
-
const
|
|
821
|
-
|
|
933
|
+
$('import-run').addEventListener('click', async () => {
|
|
934
|
+
const typed = $('import-path').value.trim()
|
|
935
|
+
// 手填的是绝对路径:交给后端复制(原文件不动,副本进 state/imports)。
|
|
936
|
+
if (typed && !pendingFile) {
|
|
937
|
+
importRequest({ op: 'import', path: typed, kind: importKind }, '已请求导入')
|
|
938
|
+
$('import-path').value = ''
|
|
939
|
+
return
|
|
940
|
+
}
|
|
941
|
+
const file = pendingFile
|
|
942
|
+
if (!file) return say('先点「选择」挑一个文件,或在框里填绝对路径', 'bad')
|
|
822
943
|
try {
|
|
823
944
|
const raw = await file.text()
|
|
824
945
|
if (raw.length > 64 * 1024) throw new Error('超过 64 KB:这么大的正文不该塞进 options')
|
|
825
946
|
const id = (file.name.replace(/\.[^.]+$/, '').toLowerCase().replace(/[^a-z0-9._-]+/g, '-') || 'inline').slice(0, 48)
|
|
826
947
|
const kept = inlineEntries().filter((x) => x.id !== id)
|
|
827
|
-
const body = raw.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n?/, '').trim()
|
|
828
948
|
const fm = raw.match(/^---\r?\n([\s\S]*?)\r?\n---/)
|
|
949
|
+
const declared = fm?.[1]?.match(/^kind:\s*(persona|skill)\s*$/m)?.[1]
|
|
829
950
|
const name = fm?.[1]?.match(/^name:\s*(.+)$/m)?.[1]?.replace(/^["']|["']$/g, '') || file.name.replace(/\.[^.]+$/, '')
|
|
830
|
-
|
|
831
|
-
|
|
951
|
+
const body = raw.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n?/, '').trim()
|
|
952
|
+
// 类型的优先级:用户明确选的 > 文件自己写的 > 猜。
|
|
953
|
+
const kind = importKind !== 'auto' ? importKind : declared || guessKind(body)
|
|
954
|
+
save({ inline: [...kept, { id, name, kind, text: body }] }, `已导入「${name}」(${kind === 'skill' ? '技能' : '人设'})`)
|
|
955
|
+
pendingFile = null
|
|
956
|
+
$('import-path').value = ''
|
|
957
|
+
refreshAfterImport()
|
|
832
958
|
} catch (error) {
|
|
833
959
|
say(`导入失败:${error?.message || error}`, 'bad')
|
|
834
960
|
}
|
|
835
961
|
})
|
|
836
962
|
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
963
|
+
// 导入类型:三选一,选中项自己管自己的 aria-pressed。
|
|
964
|
+
for (const chip of document.querySelectorAll('#import-kind .chip')) {
|
|
965
|
+
chip.addEventListener('click', () => {
|
|
966
|
+
importKind = chip.dataset.kind || 'auto'
|
|
967
|
+
for (const other of document.querySelectorAll('#import-kind .chip')) {
|
|
968
|
+
other.setAttribute('aria-pressed', other === chip ? 'true' : 'false')
|
|
969
|
+
}
|
|
970
|
+
})
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
* guessKind 猜一份 .md 是人设还是技能。
|
|
976
|
+
*
|
|
977
|
+
* 判据来自本插件自己的两种文件长相:人设是完整第二人称,通常带 `## 你是什么`/`你是谁`;
|
|
978
|
+
* 技能是「某个领域的专家」,标题一般是 `# X专家`,正文多含命令与表格。
|
|
979
|
+
*/
|
|
980
|
+
function guessKind(body) {
|
|
981
|
+
const text = String(body || '')
|
|
982
|
+
if (/^##\s*(你是什么|你是谁|你是谁?)/m.test(text)) return 'persona'
|
|
983
|
+
if (/^(你是|你现在是)/m.test(text) && !/^#\s/m.test(text)) return 'persona'
|
|
984
|
+
if (/^#\s/m.test(text) && /(\||命令|工具|参数)/.test(text)) return 'skill'
|
|
985
|
+
return 'persona'
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
/** refreshAfterImport 让新导入的条目立刻出现在列表里,并让后端按新 options 重扫。 */
|
|
989
|
+
function refreshAfterImport() {
|
|
990
|
+
save({ refreshAt: Date.now() })
|
|
991
|
+
render()
|
|
992
|
+
pollSnapshot('等后端按新导入重扫…', 20)
|
|
843
993
|
}
|
|
844
994
|
|
|
845
995
|
async function boot() {
|
package/page/index.html
CHANGED
|
@@ -40,28 +40,27 @@
|
|
|
40
40
|
|
|
41
41
|
<footer class="foot">
|
|
42
42
|
<details class="fold">
|
|
43
|
-
<summary
|
|
43
|
+
<summary>导入</summary>
|
|
44
44
|
<div class="fold-body">
|
|
45
45
|
<label class="field">
|
|
46
46
|
<span class="field-label">文件路径</span>
|
|
47
47
|
<div class="inline-row">
|
|
48
|
-
<input id="import-path" type="text" placeholder="
|
|
48
|
+
<input id="import-path" type="text" placeholder="留空点「选择」挑文件,或直接粘一个绝对路径" aria-label="要导入的文件路径" spellcheck="false">
|
|
49
|
+
<button id="import-pick" type="button">选择</button>
|
|
49
50
|
<button id="import-run" type="button">导入</button>
|
|
50
51
|
</div>
|
|
51
52
|
</label>
|
|
52
|
-
<p class="note">只认 <code>.md</code> <code>.markdown</code> <code>.txt</code> <code>.json</code> <code>.zip</code
|
|
53
|
+
<p class="note" id="import-hint">只认 <code>.md</code> <code>.markdown</code> <code>.txt</code> <code>.json</code> <code>.zip</code>。选文件后路径会填进来,再点一次「导入」才真的导入。</p>
|
|
53
54
|
|
|
54
55
|
<label class="field">
|
|
55
|
-
<span class="field-label"
|
|
56
|
-
<
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
<span class="field-label">来源目录(一行一个)</span>
|
|
62
|
-
<textarea id="sources" rows="3" spellcheck="false" aria-label="来源目录,一行一个" placeholder="D:\purge ~/.magpie/purge"></textarea>
|
|
56
|
+
<span class="field-label">这份文件是</span>
|
|
57
|
+
<div class="chips" id="import-kind" role="group" aria-label="导入类型">
|
|
58
|
+
<button type="button" class="chip" data-kind="auto" aria-pressed="true">自动判断</button>
|
|
59
|
+
<button type="button" class="chip" data-kind="persona" aria-pressed="false">人设</button>
|
|
60
|
+
<button type="button" class="chip" data-kind="skill" aria-pressed="false">技能</button>
|
|
61
|
+
</div>
|
|
63
62
|
</label>
|
|
64
|
-
<
|
|
63
|
+
<p class="note">「自动判断」按 frontmatter 与正文结构猜;猜错就在这里指定,它会覆盖判定结果。</p>
|
|
65
64
|
</div>
|
|
66
65
|
</details>
|
|
67
66
|
|
package/page/style.css
CHANGED
package/preview/catalog.demo.js
CHANGED
|
@@ -6,7 +6,6 @@ window.PURGE_CATALOG = {
|
|
|
6
6
|
generatedAt: 1794561661000,
|
|
7
7
|
sources: [
|
|
8
8
|
{ path: '(包内)skills + personas', ok: true, count: 10, error: '', bundled: true },
|
|
9
|
-
{ path: 'C:/Users/you/.magpie/purge', ok: true, count: 1, error: '' },
|
|
10
9
|
],
|
|
11
10
|
entries: [
|
|
12
11
|
{
|
|
@@ -101,7 +100,7 @@ window.PURGE_CATALOG = {
|
|
|
101
100
|
kind: 'persona',
|
|
102
101
|
status: 'ok',
|
|
103
102
|
reason: '',
|
|
104
|
-
source: { root: '
|
|
103
|
+
source: { root: 'state/imports', rel: 'anima.md' },
|
|
105
104
|
size: 1204,
|
|
106
105
|
digest: 'ef84b655238b9a71',
|
|
107
106
|
importedAt: 1794561661000,
|
|
@@ -118,7 +117,7 @@ window.PURGE_CATALOG = {
|
|
|
118
117
|
kind: 'session-archive',
|
|
119
118
|
status: 'ok',
|
|
120
119
|
reason: '',
|
|
121
|
-
source: { root: '
|
|
120
|
+
source: { root: 'state/imports', rel: '5556.zip' },
|
|
122
121
|
size: 177986,
|
|
123
122
|
digest: '948c5f9615e0c1d2',
|
|
124
123
|
importedAt: 1794561661000,
|
|
@@ -162,4 +161,13 @@ window.PURGE_CATALOG = {
|
|
|
162
161
|
// 不加这个参数时,演示快照里 last 与 plan 对得上,按钮直接是「已注入」。
|
|
163
162
|
if (new URLSearchParams(location.search).get('unsent') === '1') {
|
|
164
163
|
window.PURGE_CATALOG.last = null
|
|
165
|
-
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// 预览开关:?inline=1 造一条「页面导入」的条目,用来看那条路径的检查器
|
|
167
|
+
// (它有「删除导入」「改判」两个按钮,不导入就看不到)。
|
|
168
|
+
if (new URLSearchParams(location.search).get('inline') === '1') {
|
|
169
|
+
window.PURGE_CATALOG.options = {
|
|
170
|
+
...window.PURGE_CATALOG.options,
|
|
171
|
+
inline: [{ id: 'mine', name: '我的技能', kind: 'skill', text: '这是从页面导入的正文。\n\n## 用法\n\n点一下就来。\n' }],
|
|
172
|
+
}
|
|
173
|
+
}
|
package/preview/host.html
CHANGED
|
@@ -38,8 +38,9 @@
|
|
|
38
38
|
// 把预览用的开关一起转给业务页:unsent=1 抹掉「最近一次生效」,
|
|
39
39
|
// 好看「确认注入」还没按下的那副样子。
|
|
40
40
|
const pass = new URLSearchParams({ theme })
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
for (const flag of ['unsent', 'menu', 'inline']) {
|
|
42
|
+
if (new URLSearchParams(location.search).get(flag) === '1') pass.set(flag, '1')
|
|
43
|
+
}
|
|
43
44
|
frame.src = `/page/index.html?${pass}`
|
|
44
45
|
document.getElementById('mode').textContent = `预览:mock 宿主 · ${theme === 'light' ? '浅色' : '深色'}`
|
|
45
46
|
// 默认不选任何条目:右栏默认就是「注入组装」,那是最该看的一屏。
|