@sanqianx/project-knowledge 5.2.1 → 5.3.1
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/.claude-plugin/marketplace.json +1 -1
- package/CHANGELOG.md +6 -0
- package/README.md +6 -0
- package/_site/_test/ai-team-ui-integration-test.js +219 -0
- package/_site/_test/automatic-context-test.js +6 -0
- package/_site/_test/context-session-index-test.js +19 -9
- package/_site/_test/helpers/ai-team-ui-fixture.js +235 -0
- package/_site/_test/helpers/parallel-memory-shell.js +15 -7
- package/_site/_test/helpers/parallel-qa-engine-endpoint-isolate.js +3 -1
- package/_site/_test/helpers/single-repo-maintenance-fixture.js +94 -0
- package/_site/_test/helpers/single-repo-maintenance-tool.js +56 -0
- package/_site/_test/memory-groups-ui-test.js +27 -3
- package/_site/_test/parallel-capture-safety-r5-mem005-health-test.js +2 -2
- package/_site/_test/parallel-engine-final-eng012-test.js +4 -4
- package/_site/_test/parallel-memory-candidate-test.js +7 -5
- package/_site/_test/parallel-memory-capture-r3-mem005-persistence-test.js +1 -1
- package/_site/_test/parallel-memory-capture-r4-mem005-health-test.js +2 -2
- package/_site/_test/parallel-memory-home-review-test.js +1 -1
- package/_site/_test/parallel-memory-manual-test.js +14 -25
- package/_site/_test/parallel-memory-scope-test.js +66 -264
- package/_site/_test/parallel-memory-source-test.js +5 -7
- package/_site/_test/parallel-memory-sync-test.js +18 -9
- package/_site/_test/parallel-qa-engine-endpoint-guard-test.js +4 -2
- package/_site/_test/project-team-ui-test.js +11 -15
- package/_site/_test/shared-scope-root-binding-test.js +7 -6
- package/_site/_test/single-repo-maintenance-e2e-test.js +200 -0
- package/_site/_test/t07f-mem-008-coverage-test.js +0 -1
- package/_site/_test/workbench-context-receipts-test.js +9 -4
- package/_site/lib/engine-connection.js +2 -2
- package/_site/lib/engine-knowledge-client.js +1 -1
- package/_site/scripts/verify-retrieval-gold.js +10 -13
- package/module-runtime-manifest.json +31 -26
- package/package.json +3 -3
- package/plugins/project-knowledge/.claude-plugin/plugin.json +1 -1
- package/plugins/project-knowledge/.codex-plugin/plugin.json +1 -1
- package/plugins/project-knowledge/.mcp.json +1 -1
- package/plugins/project-knowledge/scripts/context-hook.cjs +3 -3
- package/plugins/project-knowledge/skills/project-knowledge/SKILL.md +5 -6
- package/ui/product.css +20 -0
- package/ui/product.js +161 -67
package/ui/product.js
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
overview: null, overviewError: null, overviewLoading: false, overviewEpoch: 0,
|
|
26
26
|
groups: { items: [], total: 0, loading: false, epoch: 0 },
|
|
27
27
|
memory: { tab: 'library', folder: 'all', q: '', items: [], total: 0, cursors: [null], page: 0, loading: false, epoch: 0 },
|
|
28
|
-
candidates: { items: [], total: 0, cursors: [null], page: 0, loading: false, epoch: 0 },
|
|
28
|
+
candidates: { items: [], total: 0, cursors: [null], page: 0, projectFilter: null, loading: false, epoch: 0 },
|
|
29
29
|
projects: [], projectsPage: { q: '', domain: '', items: [], total: 0, cursors: [null], page: 0, loading: false, epoch: 0, detail: null },
|
|
30
30
|
detailGroups: { detail: null, epoch: 0 },
|
|
31
31
|
search: { q: '', draft: '', projectId: '', scope: 'brain', type: 'all', done: false, results: [], page: 0, loading: false, epoch: 0 },
|
|
@@ -39,7 +39,6 @@
|
|
|
39
39
|
const projectName = id => state.projects.find(p => p.projectId === id)?.name || id;
|
|
40
40
|
const time = value => value ? new Date(value).toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }) : '—';
|
|
41
41
|
const clamp2 = text => String(text || '').replace(/\n\s*\n/g, '\n');
|
|
42
|
-
const SCOPE_LABEL = { project: '项目记忆', domain: '领域记忆', global: '全局记忆', group: '组合记忆' };
|
|
43
42
|
const recoveryLabel = run => {
|
|
44
43
|
const recovery = run.recovery;
|
|
45
44
|
if (['failed', 'index-pending'].includes(run.state)) {
|
|
@@ -86,6 +85,17 @@
|
|
|
86
85
|
}
|
|
87
86
|
function on(id, event, callback) { $(id).addEventListener(event, e => Promise.resolve(callback(e)).catch(error => toast(error.message))); }
|
|
88
87
|
function post(key, message) { const frame = frames[key]; if (frame.ready) $(frame.id).contentWindow.postMessage(message, location.origin); }
|
|
88
|
+
function postTheme(key) {
|
|
89
|
+
if (!frames[key].ready) return;
|
|
90
|
+
const root = document.documentElement;
|
|
91
|
+
const message = { type: 'kb:theme', theme: root.dataset.theme };
|
|
92
|
+
if (key === 'workflow') {
|
|
93
|
+
const style = getComputedStyle(root);
|
|
94
|
+
const tokens = ['page', 'surface', 'surface2', 'line', 'text', 'muted', 'accent', 'accent-soft', 'good', 'bad', 'bad-soft'];
|
|
95
|
+
message.palette = Object.fromEntries(tokens.map(token => [token, style.getPropertyValue('--' + token).trim()]));
|
|
96
|
+
}
|
|
97
|
+
post(key, message);
|
|
98
|
+
}
|
|
89
99
|
function initializeFrame(key) { const frame = frames[key]; if (!frame.initialized) { $(frame.id).src = frame.url; frame.initialized = true; } }
|
|
90
100
|
function frameVisible(key) { return key === 'engine' ? $('engineDialog').open : key === 'eventBridge' && state.view === 'activity' && state.activityTab === 'sessions'; }
|
|
91
101
|
function frameContext(key) {
|
|
@@ -97,7 +107,7 @@
|
|
|
97
107
|
const signature = JSON.stringify([state.epoch, ref || '', document.documentElement.dataset.theme, frameVisible(key)]);
|
|
98
108
|
if (frame.contextSignature === signature) return;
|
|
99
109
|
frame.contextSignature = signature;
|
|
100
|
-
|
|
110
|
+
postTheme(key);
|
|
101
111
|
post(key, { type: 'kb:select-project', project: ref || '', projectId: project?.projectId || '', requestId: frame.requestId });
|
|
102
112
|
post(key, { type: 'kb:visibility', visible: frameVisible(key) });
|
|
103
113
|
}
|
|
@@ -244,6 +254,7 @@
|
|
|
244
254
|
function selectProject(id) {
|
|
245
255
|
if (id === state.projectId && activeProject()) return;
|
|
246
256
|
state.projectId = id; state.epoch++;
|
|
257
|
+
if (state.candidates.projectFilter === null) { state.candidates.page = 0; state.candidates.cursors = [null]; }
|
|
247
258
|
state.snapshot.runs = []; state.activityProject = null; state.activityBefore = null; state.runDetails = new Map();
|
|
248
259
|
void refreshSnapshot();
|
|
249
260
|
referenceEpoch++; $('referenceDialog').close();
|
|
@@ -378,7 +389,7 @@
|
|
|
378
389
|
${overview.totals?.recovering ? `<div class="recovery-banner">${overview.totals?.recovering} 项正在自动恢复,无需操作<button data-action="activity-recovering">查看</button></div>` : ''}
|
|
379
390
|
</section>
|
|
380
391
|
<section class="panel"><h2>待确认记忆 ${overview.totals?.candidates ? badge(overview.totals?.candidates, '') : ''}</h2>${candidateRows}
|
|
381
|
-
${overview.totals?.candidates > candidates.length ? '<button data-go="memory">查看候选箱</button>' : ''}
|
|
392
|
+
${overview.totals?.candidates > candidates.length ? '<button data-go="memory" data-memory-tab="candidate">查看候选箱</button>' : ''}
|
|
382
393
|
</section>
|
|
383
394
|
</div>
|
|
384
395
|
${teamSummaryCard()}
|
|
@@ -388,13 +399,18 @@
|
|
|
388
399
|
async function loadGroups() {
|
|
389
400
|
const epoch = ++state.groups.epoch;
|
|
390
401
|
state.groups.loading = true;
|
|
402
|
+
let groups = state.groups.items;
|
|
391
403
|
try {
|
|
392
404
|
const result = await api('/api/engine/memory-groups?limit=100');
|
|
393
|
-
|
|
394
|
-
|
|
405
|
+
groups = result.groups;
|
|
406
|
+
if (epoch === state.groups.epoch) {
|
|
407
|
+
state.groups.items = groups;
|
|
408
|
+
state.groups.total = result.total;
|
|
409
|
+
}
|
|
395
410
|
} catch (error) { if (epoch === state.groups.epoch) toast('组合列表读取失败:' + error.message); }
|
|
396
411
|
finally { if (epoch === state.groups.epoch) state.groups.loading = false; }
|
|
397
|
-
if (state.view === 'memory') renderMemories();
|
|
412
|
+
if (epoch === state.groups.epoch && state.view === 'memory') renderMemories();
|
|
413
|
+
return groups;
|
|
398
414
|
}
|
|
399
415
|
const groupById = id => state.groups.items.find(g => g.id === id);
|
|
400
416
|
async function loadMemoryPage() {
|
|
@@ -416,24 +432,44 @@
|
|
|
416
432
|
finally { if (epoch === memory.epoch) memory.loading = false; }
|
|
417
433
|
if (state.view === 'memory') renderMemories();
|
|
418
434
|
}
|
|
435
|
+
const candidateProjectId = () => state.candidates.projectFilter ?? activeProject()?.projectId ?? '';
|
|
436
|
+
const candidateSourceProjectId = memory => memory.evidence?.projectId || null;
|
|
437
|
+
async function projectCandidates(projectId) {
|
|
438
|
+
const params = new URLSearchParams({ status: 'candidate', sourceProjectId: projectId });
|
|
439
|
+
const result = await api('/api/engine/memories?' + params);
|
|
440
|
+
return result.memories.filter(memory => candidateSourceProjectId(memory) === projectId)
|
|
441
|
+
.sort((a, b) => String(b.updatedAt || b.createdAt || '').localeCompare(String(a.updatedAt || a.createdAt || '')) || a.id.localeCompare(b.id));
|
|
442
|
+
}
|
|
419
443
|
async function loadCandidates() {
|
|
420
444
|
const candidates = state.candidates;
|
|
421
445
|
const epoch = ++candidates.epoch;
|
|
422
446
|
candidates.loading = true;
|
|
447
|
+
const projectId = candidateProjectId();
|
|
448
|
+
if (state.view === 'memory' && state.memory.tab === 'candidate') renderMemories();
|
|
423
449
|
try {
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
450
|
+
if (projectId) {
|
|
451
|
+
const items = await projectCandidates(projectId);
|
|
452
|
+
if (epoch !== candidates.epoch) return;
|
|
453
|
+
candidates.total = items.length;
|
|
454
|
+
candidates.page = Math.min(candidates.page, Math.max(0, Math.ceil(items.length / 10) - 1));
|
|
455
|
+
const start = candidates.page * 10;
|
|
456
|
+
candidates.items = items.slice(start, start + 10);
|
|
457
|
+
candidates.cursors = Array.from({ length: Math.ceil(items.length / 10) }, (_, page) => page * 10);
|
|
458
|
+
} else {
|
|
459
|
+
const params = new URLSearchParams({ status: 'candidate', limit: '10' });
|
|
460
|
+
const cursor = candidates.cursors[candidates.page];
|
|
461
|
+
if (cursor) params.set('cursor', cursor);
|
|
462
|
+
const result = await api('/api/engine/memories?' + params);
|
|
463
|
+
if (epoch !== candidates.epoch) return;
|
|
464
|
+
candidates.items = result.memories; candidates.total = result.total;
|
|
465
|
+
if (result.nextCursor) candidates.cursors[candidates.page + 1] = result.nextCursor; else candidates.cursors.length = candidates.page + 1;
|
|
466
|
+
}
|
|
431
467
|
} catch (error) { if (epoch === candidates.epoch) toast('候选列表读取失败:' + error.message); }
|
|
432
468
|
finally { if (epoch === candidates.epoch) candidates.loading = false; }
|
|
433
469
|
if (state.view === 'memory') renderMemories();
|
|
434
470
|
}
|
|
435
471
|
function memoryCard(m) {
|
|
436
|
-
const scopeText = m.
|
|
472
|
+
const scopeText = m.groups?.length ? `组合:${m.groups.map(g => esc(g.name)).join('、')}` : '未归组合';
|
|
437
473
|
const statusBadge = m.targetAvailable === false ? badge('目标已失效,不会注入', 'issue') : m.syncStatus === 'pending' ? badge('写入待恢复', 'issue') : '';
|
|
438
474
|
return `<article class="memory-item"><button class="reference" data-action="memory-detail" data-id="${esc(m.id)}"><b>${esc(m.title)}</b><small>${esc(clamp2(m.text).slice(0, 160))}</small><small>${scopeText}</small></button><div class="actions">${statusBadge}<button data-action="memory-edit" data-id="${esc(m.id)}">编辑</button><button data-action="memory-groups" data-id="${esc(m.id)}">组合归属</button>${m.sourceType !== 'manual' ? `<button data-action="memory-evidence" data-id="${esc(m.id)}">查看依据</button>` : ''}<button data-action="memory-delete" data-id="${esc(m.id)}">删除</button></div></article>`;
|
|
439
475
|
}
|
|
@@ -446,9 +482,12 @@
|
|
|
446
482
|
if (!state.engineAvailable) { $('memoryContent').innerHTML = '<div class="empty">知识引擎暂时不可用。未读取到的记忆不会显示为已确认。</div>'; return; }
|
|
447
483
|
const memory = state.memory, candidates = state.candidates;
|
|
448
484
|
if (memory.tab === 'candidate') {
|
|
485
|
+
const projectId = candidateProjectId();
|
|
486
|
+
const projectOptions = ['<option value="">全部项目</option>', ...state.projects.map(project => `<option value="${esc(project.projectId)}" ${project.projectId === projectId ? 'selected' : ''}>${esc(project.name)}</option>`)].join('');
|
|
449
487
|
const rows = candidates.items.map(m => `<div class="row"><div><b>${esc(m.title)}</b><small>${esc(clamp2(m.text).slice(0, 140))}</small><small>${m.evidence ? `来源:${esc(projectName(m.evidence.projectId))} · ${esc(m.evidence.commitSha.slice(0, 8))}` : '来源:手动添加'}</small></div><button class="primary" data-action="review-candidate" data-id="${esc(m.id)}">审核</button></div>`).join('');
|
|
450
488
|
$('memoryContent').innerHTML = `<div class="segmented" style="margin-bottom:20px"><button data-memory-tab="library">记忆库</button><button class="active" data-memory-tab="candidate">候选箱 ${candidates.total || ''}</button></div>
|
|
451
|
-
|
|
489
|
+
<label>按项目查看<select id="candidateProjectFilter">${projectOptions}</select></label>
|
|
490
|
+
${candidates.loading ? '<p class="small-empty">正在读取候选…</p>' : rows || '<div class="empty small-empty">这个范围内没有待确认的记忆。</div>'}
|
|
452
491
|
${pagerUi('candidate', candidates.page, candidates.total, 10)}`;
|
|
453
492
|
return;
|
|
454
493
|
}
|
|
@@ -584,7 +623,7 @@
|
|
|
584
623
|
const project = state.projectsPage.items.find(p => p.projectId === detail.projectId);
|
|
585
624
|
if (!project) { state.projectsPage.detail = null; return renderProjects(); }
|
|
586
625
|
const effective = detail.effective;
|
|
587
|
-
const items = effective.items.map(m => `<article class="memory-item"><button class="reference" data-action="memory-detail" data-id="${esc(m.id)}"><b>${esc(m.title)}</b><small>${esc(clamp2(m.text).slice(0, 160))}</small><small>${(m.sources || []).map(s =>
|
|
626
|
+
const items = effective.items.map(m => `<article class="memory-item"><button class="reference" data-action="memory-detail" data-id="${esc(m.id)}"><b>${esc(m.title)}</b><small>${esc(clamp2(m.text).slice(0, 160))}</small><small>${(m.sources || []).map(s => esc(s.groupName)).join(' · ')}</small></button></article>`).join('') || '<div class="empty small-empty">为项目添加组合后,在这里查看生效记忆。</div>';
|
|
588
627
|
$('projectsContent').innerHTML = `
|
|
589
628
|
<div class="section-heading"><button class="link-button" data-action="project-list">‹ 返回项目列表</button><div class="actions"><button data-action="unbind">解绑并保留数据</button><button data-action="edit-project" data-id="${esc(project.projectId)}">项目设置</button></div></div>
|
|
590
629
|
<h2>${esc(project.name)}</h2><p>${esc(domainName(project.domainId))}</p>
|
|
@@ -592,10 +631,42 @@
|
|
|
592
631
|
<section class="panel" style="margin-top:20px"><div class="section-heading" style="margin-bottom:12px"><h2>使用的组合</h2><button data-action="project-groups" data-id="${esc(project.projectId)}">+ 添加组合</button></div>
|
|
593
632
|
<div class="group-tags">${detail.groups.map(g => groupTag(g, project.projectId)).join('') || '<small>尚未添加组合</small>'}</div></section>
|
|
594
633
|
<section class="panel" style="margin-top:20px"><div class="section-heading"><h2>AI 团队</h2>${projectTeamControl(project.projectId)}</div></section>
|
|
595
|
-
<section class="panel" style="margin-top:20px"><h2>生效记忆 <small>${effective.total} 条 · 已按记忆去重</small></h2>
|
|
634
|
+
<section class="panel" style="margin-top:20px"><div class="section-heading"><h2>生效记忆 <small>${effective.total} 条 · 已按记忆去重</small></h2>
|
|
635
|
+
<button data-action="export-memories" data-id="${esc(project.projectId)}" ${effective.total ? '' : 'disabled'}>导出 Markdown</button></div>
|
|
596
636
|
<div class="memory-list">${items}</div>
|
|
597
637
|
${pagerUi('effective', effective.page, effective.total, 10)}</section>`;
|
|
598
638
|
}
|
|
639
|
+
async function exportProjectMemories(projectId) {
|
|
640
|
+
const project = state.projectsPage.items.find(item => item.projectId === projectId);
|
|
641
|
+
if (!project) return;
|
|
642
|
+
const items = new Map(), cursors = new Set();
|
|
643
|
+
let cursor = null, total = null;
|
|
644
|
+
do {
|
|
645
|
+
const params = new URLSearchParams({ limit: '100' });
|
|
646
|
+
if (cursor) params.set('cursor', cursor);
|
|
647
|
+
const page = await api(`/api/engine/projects/${encodeURIComponent(projectId)}/effective-memories?${params}`);
|
|
648
|
+
total ??= page.total;
|
|
649
|
+
if (page.total !== total || (page.nextCursor && cursors.has(page.nextCursor))) throw new Error('生效记忆列表已变化,请重新导出。');
|
|
650
|
+
for (const memory of page.memories) items.set(memory.id, memory);
|
|
651
|
+
cursor = page.nextCursor;
|
|
652
|
+
if (cursor) cursors.add(cursor);
|
|
653
|
+
} while (cursor);
|
|
654
|
+
if (items.size !== total) throw new Error('生效记忆列表已变化,请重新导出。');
|
|
655
|
+
const memories = [...items.values()].filter(memory => memory.syncStatus === 'ready' && memory.targetAvailable && memory.sources.some(source => source.kind === 'group'));
|
|
656
|
+
if (!memories.length) return toast('当前没有已同步的生效记忆。');
|
|
657
|
+
const sections = memories.map(memory => {
|
|
658
|
+
const sources = memory.sources.map(source => source.groupName);
|
|
659
|
+
const provenance = memory.evidence ? [projectName(memory.evidence.projectId), memory.evidence.entryId, memory.evidence.commitSha].filter(Boolean).join(' / ') : '';
|
|
660
|
+
return `## ${memory.title}\n\n${memory.text}\n\n生效来源:${sources.join('、')}${provenance ? `\n\n依据:${provenance}` : ''}\n`;
|
|
661
|
+
});
|
|
662
|
+
const markdown = `# ${project.name.replace(/[\r\n]+/g, ' ')} · 生效记忆\n\n${sections.join('\n')}`;
|
|
663
|
+
const url = URL.createObjectURL(new Blob([markdown], { type: 'text/markdown;charset=utf-8' }));
|
|
664
|
+
const link = document.createElement('a');
|
|
665
|
+
link.href = url;
|
|
666
|
+
link.download = `${project.name.replace(/[<>:"/\\|?*\x00-\x1f]/g, '-').trim() || 'project'}-MEMORY.md`;
|
|
667
|
+
link.click();
|
|
668
|
+
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
669
|
+
}
|
|
599
670
|
// ============ 检索:真实引擎 API ============
|
|
600
671
|
function renderSearch() {
|
|
601
672
|
const search = state.search;
|
|
@@ -604,7 +675,7 @@
|
|
|
604
675
|
let resultsHtml = '<div class="empty small-empty">输入关键词,查找已保存的项目知识和长期记忆。</div>';
|
|
605
676
|
if (search.done) {
|
|
606
677
|
const filtered = search.results.filter(r => search.type === 'all' || (search.type === 'memory') === Boolean(r.memoryId));
|
|
607
|
-
const pageRows = filtered.slice(search.page * 8, (search.page + 1) * 8).map((r, i) => `<article class="result-row"><button class="reference" data-action="search-detail" data-index="${search.page * 8 + i}"><b>${esc(r.title)}</b><small>${esc(clamp2(r.snippet || r.chunk_text || '').slice(0, 180))}</small></button><small>${r.memoryId ?
|
|
678
|
+
const pageRows = filtered.slice(search.page * 8, (search.page + 1) * 8).map((r, i) => `<article class="result-row"><button class="reference" data-action="search-detail" data-index="${search.page * 8 + i}"><b>${esc(r.title)}</b><small>${esc(clamp2(r.snippet || r.chunk_text || '').slice(0, 180))}</small></button><small>${r.memoryId ? '长期记忆' : `${esc(projectName(r.scope_project_id || r.projectId))}${r.commitSha ? ` · commit ${esc(r.commitSha.slice(0, 8))}` : ''}`}</small></article>`).join('');
|
|
608
679
|
resultsHtml = `<p class="muted" style="margin:12px 0">${search.results.length} 条结果${filtered.length !== search.results.length ? `(当前筛选 ${filtered.length} 条)` : ''}</p>${pageRows || '<div class="empty small-empty">没有找到匹配内容,试试其他关键词。</div>'}${pagerUi('search', search.page, filtered.length, 8)}`;
|
|
609
680
|
}
|
|
610
681
|
$('searchContent').innerHTML = `
|
|
@@ -1187,58 +1258,31 @@
|
|
|
1187
1258
|
function memoryById(id) {
|
|
1188
1259
|
return state.memory.items.find(m => m.id === id) || state.candidates.items.find(m => m.id === id) || state.detailGroups.detail?.effective.items.find(m => m.id === id);
|
|
1189
1260
|
}
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
const
|
|
1193
|
-
const groupOptions = ['<option value="">暂不加入组合</option>', ...state.groups.items.map(g => `<option value="${esc(g.id)}" ${memory?.groups?.[0]?.id === g.id ? 'selected' : ''}>${esc(g.name)}</option>`)].join('');
|
|
1194
|
-
const scopeOptions = isLegacy || !memory
|
|
1195
|
-
? `<label>适用范围<select name="scopeType"><option value="group" ${!isLegacy ? 'selected' : ''}>组合记忆:通过组合应用到项目</option><option value="global" ${memory?.scopeType === 'global' ? 'selected' : ''}>全局:所有领域与项目</option><option value="domain" ${memory?.scopeType === 'domain' ? 'selected' : ''}>领域:指定领域内的项目</option><option value="project" ${memory?.scopeType === 'project' ? 'selected' : ''}>项目:仅指定项目</option></select></label>
|
|
1196
|
-
<label data-role="scope-target" hidden>适用目标<select name="scopeTarget"></select></label>`
|
|
1197
|
-
: '';
|
|
1261
|
+
async function memoryEditor(memory = null) {
|
|
1262
|
+
const groups = memory ? state.groups.items : await loadGroups();
|
|
1263
|
+
const groupOptions = ['<option value="">暂不加入组合</option>', ...groups.map(g => `<option value="${esc(g.id)}" ${memory?.groups?.[0]?.id === g.id ? 'selected' : ''}>${esc(g.name)}</option>`)].join('');
|
|
1198
1264
|
appDialogOpen(memory ? '编辑记忆' : '添加记忆', `
|
|
1199
1265
|
<form id="memoryForm">
|
|
1200
1266
|
<label>标题<input name="title" maxlength="120" required value="${esc(memory?.title || '')}" placeholder="例如:开发时保护日常服务"></label>
|
|
1201
1267
|
<label>内容<textarea name="text" required rows="8" placeholder="这是一条完整记忆,可以包含多个段落和列表。">${esc(memory?.text || '')}</textarea></label>
|
|
1202
1268
|
${memory ? '' : `<label>加入组合<select name="groupId">${groupOptions}</select></label>`}
|
|
1203
|
-
|
|
1204
|
-
<p class="muted">${memory?.scopeType === 'group' ? '这条记忆通过组合应用到项目;正文只有一份,被所有引用共享。' : '多段正文保存在同一条记忆中,不会按换行拆分。'}</p>
|
|
1269
|
+
<p class="muted">记忆通过组合生效。各组合共享同一条正文。</p>
|
|
1205
1270
|
</form>`, [
|
|
1206
1271
|
{ label: '取消', run: appDialogClose },
|
|
1207
1272
|
{ label: memory ? '保存' : '添加', class: 'primary', form: 'memoryForm' },
|
|
1208
1273
|
]);
|
|
1209
1274
|
const form = $('memoryForm');
|
|
1210
|
-
const scopeSelect = form.elements.scopeType;
|
|
1211
|
-
const targetLabel = form.querySelector('[data-role="scope-target"]');
|
|
1212
|
-
const fillTargets = type => {
|
|
1213
|
-
const targets = type === 'domain' ? state.brain.domains.map(d => ({ id: d.id, name: d.name })) : type === 'project' ? state.projects.map(p => ({ id: p.projectId, name: p.name })) : [];
|
|
1214
|
-
targetLabel.hidden = targets.length === 0;
|
|
1215
|
-
form.elements.scopeTarget.innerHTML = targets.map(t => `<option value="${esc(t.id)}" ${memory?.scopeId === t.id ? 'selected' : ''}>${esc(t.name)}</option>`).join('');
|
|
1216
|
-
};
|
|
1217
|
-
if (scopeSelect) {
|
|
1218
|
-
fillTargets(scopeSelect.value);
|
|
1219
|
-
scopeSelect.addEventListener('change', () => fillTargets(scopeSelect.value));
|
|
1220
|
-
}
|
|
1221
1275
|
form.addEventListener('submit', async event => {
|
|
1222
1276
|
event.preventDefault();
|
|
1223
1277
|
const title = form.elements.title.value.trim();
|
|
1224
1278
|
const text = form.elements.text.value.trim();
|
|
1225
1279
|
if (!title || !text) return appDialogError('请填写标题和记忆内容。');
|
|
1226
|
-
const scopeType = form.elements.scopeType?.value || 'group';
|
|
1227
|
-
if (scopeType !== 'group' && scopeType !== 'global' && !form.elements.scopeTarget?.value) return appDialogError('请选择适用目标。');
|
|
1228
1280
|
try {
|
|
1229
|
-
let saved;
|
|
1230
1281
|
if (memory) {
|
|
1231
|
-
|
|
1232
|
-
if (form.elements.scopeType && (memory.scopeType !== scopeType || scopeType !== 'group')) {
|
|
1233
|
-
body.scopeType = scopeType;
|
|
1234
|
-
body.scopeId = scopeType === 'global' ? state.brain.id : form.elements.scopeTarget?.value;
|
|
1235
|
-
}
|
|
1236
|
-
saved = await api(`/api/engine/memories/${encodeURIComponent(memory.id)}`, { method: 'PATCH', body });
|
|
1282
|
+
await api(`/api/engine/memories/${encodeURIComponent(memory.id)}`, { method: 'PATCH', body: { title, text } });
|
|
1237
1283
|
} else {
|
|
1238
|
-
const
|
|
1239
|
-
|
|
1240
|
-
const groupId = form.elements.groupId?.value;
|
|
1241
|
-
if (groupId) await api(`/api/engine/memory-groups/${encodeURIComponent(groupId)}/members`, { method: 'POST', body: { addMemoryIds: [saved.memory.id] } });
|
|
1284
|
+
const groupId = form.elements.groupId.value;
|
|
1285
|
+
await api('/api/engine/memories', { method: 'POST', body: { title, text, brainId: state.brain.id, ...(groupId ? { groupId } : {}) } });
|
|
1242
1286
|
}
|
|
1243
1287
|
appDialogClose();
|
|
1244
1288
|
toast(memory ? '记忆已保存,多行内容保持在同一条记忆中。' : '记忆已保存。');
|
|
@@ -1247,37 +1291,79 @@
|
|
|
1247
1291
|
} catch (error) { appDialogError(error.message); }
|
|
1248
1292
|
});
|
|
1249
1293
|
}
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1294
|
+
function textTrigrams(text) {
|
|
1295
|
+
const normalized = text.normalize('NFKC').toLowerCase().replace(/[^\p{L}\p{N}]+/gu, '');
|
|
1296
|
+
return new Set(Array.from({ length: Math.max(0, normalized.length - 2) }, (_, i) => normalized.slice(i, i + 3)));
|
|
1297
|
+
}
|
|
1298
|
+
function nearbyCandidates(memory, candidates) {
|
|
1299
|
+
const base = textTrigrams(memory.text);
|
|
1300
|
+
return candidates.filter(candidate => candidate.id !== memory.id).map(candidate => {
|
|
1301
|
+
const terms = textTrigrams(candidate.text);
|
|
1302
|
+
let shared = 0;
|
|
1303
|
+
for (const term of terms) if (base.has(term)) shared++;
|
|
1304
|
+
return { candidate, similarity: shared ? 2 * shared / (base.size + terms.size) : 0 };
|
|
1305
|
+
}).filter(item => item.similarity >= 0.5)
|
|
1306
|
+
.sort((a, b) => b.similarity - a.similarity).slice(0, 3).map(item => item.candidate);
|
|
1307
|
+
}
|
|
1308
|
+
async function reviewCandidate(memory) {
|
|
1309
|
+
const sourceProjectId = candidateSourceProjectId(memory);
|
|
1310
|
+
const sourceGroups = sourceProjectId ? api(`/api/engine/projects/${encodeURIComponent(sourceProjectId)}/memory-groups`).catch(() => null) : null;
|
|
1311
|
+
const groups = await loadGroups();
|
|
1312
|
+
const bound = (await sourceGroups)?.groups || [];
|
|
1313
|
+
const preferred = bound.length === 1 ? bound[0].id : '';
|
|
1314
|
+
const groupOptions = ['<option value="">仅收录到记忆库(暂不生效)</option>', ...groups.map(group => `<option value="${esc(group.id)}" ${group.id === preferred ? 'selected' : ''}>${esc(group.name)}</option>`)].join('');
|
|
1253
1315
|
appDialogOpen('审核候选记忆', `
|
|
1254
1316
|
<form id="reviewForm">
|
|
1255
1317
|
<h3>${esc(memory.title)}</h3>
|
|
1256
1318
|
<p class="feedback" style="white-space:pre-wrap">${esc(memory.text)}</p>
|
|
1257
1319
|
<p class="muted">${memory.evidence ? `来源:${esc(projectName(memory.evidence.projectId))} · commit ${esc(memory.evidence.commitSha.slice(0, 8))} · ${esc(memory.evidence.entryId)}` : '来源:手动添加'}</p>
|
|
1258
1320
|
${memory.evidence?.quote ? `<details class="agent-detail" style="margin:12px 0"><summary>查看原始依据(冻结原文)</summary><p class="feedback" style="white-space:pre-wrap">${esc(memory.evidence.quote)}</p></details>` : ''}
|
|
1321
|
+
<details class="agent-detail" data-role="similar" hidden></details>
|
|
1259
1322
|
<label>确认后加入组合<select name="groupId">${groupOptions}</select></label>
|
|
1260
|
-
<p class="muted" data-role="impact"
|
|
1323
|
+
<p class="muted" data-role="impact"></p>
|
|
1261
1324
|
</form>`, [
|
|
1262
1325
|
{ label: '忽略', class: 'danger', run: async () => { await api(`/api/engine/memories/${encodeURIComponent(memory.id)}`, { method: 'PATCH', body: { status: 'ignored' } }); appDialogClose(); toast('已忽略候选。'); await Promise.all([loadCandidates(), loadOverview(true)]); } },
|
|
1263
1326
|
{ label: '取消', run: appDialogClose },
|
|
1264
1327
|
{ label: '确认收录', class: 'primary', form: 'reviewForm' },
|
|
1265
1328
|
]);
|
|
1266
1329
|
const form = $('reviewForm');
|
|
1267
|
-
form.
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1330
|
+
const dialogIsOpen = () => form.isConnected && $('appDialog').open;
|
|
1331
|
+
const updateImpact = async () => {
|
|
1332
|
+
const groupId = form.elements.groupId.value;
|
|
1333
|
+
const group = groupById(groupId);
|
|
1334
|
+
const impact = form.querySelector('[data-role="impact"]');
|
|
1335
|
+
impact.textContent = group
|
|
1336
|
+
? `加入「${group.name}」后生效于 ${group.boundProjectCount} 个项目。`
|
|
1337
|
+
: '确认后保存在记忆库中,应用到项目的组合才会让它生效。';
|
|
1338
|
+
if (!group?.boundProjectCount) return;
|
|
1339
|
+
const result = await api(`/api/engine/memory-groups/${encodeURIComponent(groupId)}`).catch(() => null);
|
|
1340
|
+
if (!dialogIsOpen() || form.elements.groupId.value !== groupId || !result?.group?.boundProjectIds) return;
|
|
1341
|
+
const names = result.group.boundProjectIds.slice(0, 5).map(projectName).join('、');
|
|
1342
|
+
impact.textContent = `加入「${group.name}」后生效于 ${names}${result.group.boundProjectIds.length > 5 ? '等 ' + result.group.boundProjectIds.length + ' 个项目' : ''}。`;
|
|
1343
|
+
};
|
|
1344
|
+
form.elements.groupId.addEventListener('change', updateImpact);
|
|
1345
|
+
void updateImpact();
|
|
1346
|
+
if (sourceProjectId) void projectCandidates(sourceProjectId).then(candidates => {
|
|
1347
|
+
if (!dialogIsOpen()) return;
|
|
1348
|
+
const related = nearbyCandidates(memory, candidates);
|
|
1349
|
+
if (!related.length) return;
|
|
1350
|
+
const details = form.querySelector('[data-role="similar"]');
|
|
1351
|
+
details.innerHTML = `<summary>可能重复的候选(${related.length})</summary>${related.map(item => `<p class="feedback" style="white-space:pre-wrap"><b>${esc(item.title)}</b><br>${esc(item.text)}</p>`).join('')}`;
|
|
1352
|
+
details.hidden = false;
|
|
1353
|
+
}).catch(() => {});
|
|
1354
|
+
let saving = false;
|
|
1271
1355
|
form.addEventListener('submit', async event => {
|
|
1272
1356
|
event.preventDefault();
|
|
1357
|
+
if (saving) return;
|
|
1358
|
+
saving = true;
|
|
1273
1359
|
try {
|
|
1274
|
-
await api(`/api/engine/memories/${encodeURIComponent(memory.id)}`, { method: 'PATCH', body: { status: 'confirmed' } });
|
|
1275
1360
|
const groupId = form.elements.groupId.value;
|
|
1276
|
-
|
|
1361
|
+
await api(`/api/engine/memories/${encodeURIComponent(memory.id)}`, { method: 'PATCH', body: { status: 'confirmed', ...(groupId ? { groupId } : {}) } });
|
|
1277
1362
|
appDialogClose();
|
|
1278
1363
|
toast(groupId ? `已确认并加入「${groupById(groupId)?.name}」。` : '已确认,保留在记忆库中。');
|
|
1279
1364
|
await Promise.all([loadCandidates(), loadGroups(), loadMemoryPage(), loadOverview(true)]);
|
|
1280
1365
|
} catch (error) { appDialogError(error.message); }
|
|
1366
|
+
finally { saving = false; }
|
|
1281
1367
|
});
|
|
1282
1368
|
}
|
|
1283
1369
|
// ---- 记忆详情 ----
|
|
@@ -1286,7 +1372,7 @@
|
|
|
1286
1372
|
appDialogOpen(memory.title, `
|
|
1287
1373
|
<p class="muted">${memory.sourceType === 'manual' ? `手动添加 · ${time(memory.createdAt)}` : memory.evidence ? `${esc(projectName(memory.evidence.projectId))} · commit ${esc(memory.evidence.commitSha.slice(0, 8))} · ${time(memory.updatedAt)}` : time(memory.updatedAt)}</p>
|
|
1288
1374
|
<p class="feedback" style="white-space:pre-wrap;max-height:40vh;overflow:auto">${esc(memory.text)}</p>
|
|
1289
|
-
<p class="muted">所在组合:${groups.map(g => esc(g.name)).join('、') || '未归组合'}
|
|
1375
|
+
<p class="muted">所在组合:${groups.map(g => esc(g.name)).join('、') || '未归组合'}</p>`, [
|
|
1290
1376
|
{ label: '关闭', run: appDialogClose },
|
|
1291
1377
|
...(memory.status === 'confirmed' ? [
|
|
1292
1378
|
{ label: '组合归属', run: () => memoryGroupsDialog(memory) },
|
|
@@ -1566,11 +1652,12 @@
|
|
|
1566
1652
|
if (button.dataset.action === 'group-settings') groupEditor(groupById(button.dataset.id));
|
|
1567
1653
|
if (button.dataset.action === 'group-projects') await groupProjectsDialog(groupById(button.dataset.id));
|
|
1568
1654
|
if (button.dataset.action === 'open-group') { setView('memory'); state.memory.tab = 'library'; state.memory.folder = button.dataset.id; state.memory.q = ''; state.memory.page = 0; state.memory.cursors = [null]; await loadMemoryPage(); }
|
|
1569
|
-
if (button.dataset.action === 'new-memory') memoryEditor();
|
|
1655
|
+
if (button.dataset.action === 'new-memory') await memoryEditor();
|
|
1656
|
+
if (button.dataset.action === 'export-memories') await exportProjectMemories(button.dataset.id);
|
|
1570
1657
|
if (button.dataset.action === 'memory-edit') memoryEditor(memoryById(button.dataset.id));
|
|
1571
1658
|
if (button.dataset.action === 'memory-detail') { const memory = memoryById(button.dataset.id); if (memory) memoryDetail(memory); else toast('列表已刷新,请重试。'); }
|
|
1572
1659
|
if (button.dataset.action === 'memory-groups') { const memory = memoryById(button.dataset.id); if (memory) await memoryGroupsDialog(memory); }
|
|
1573
|
-
if (button.dataset.action === 'review-candidate') { const memory = state.candidates.items.find(m => m.id === button.dataset.id) || state.overview?.candidatesPreview?.find(m => m.id === button.dataset.id); if (memory) reviewCandidate(memory); else { await loadCandidates(); const fresh = state.candidates.items.find(m => m.id === button.dataset.id); if (fresh) reviewCandidate(fresh); else toast('候选不存在或已处理。'); } }
|
|
1660
|
+
if (button.dataset.action === 'review-candidate') { const memory = state.candidates.items.find(m => m.id === button.dataset.id) || state.overview?.candidatesPreview?.find(m => m.id === button.dataset.id); if (memory) await reviewCandidate(memory); else { await loadCandidates(); const fresh = state.candidates.items.find(m => m.id === button.dataset.id); if (fresh) await reviewCandidate(fresh); else toast('候选不存在或已处理。'); } }
|
|
1574
1661
|
if (button.dataset.action === 'memory-evidence') { const memory = memoryById(button.dataset.id); if (memory?.evidence) await openReference({ ...memory.evidence, readFile: false, text: memory.evidence.quote, title: '记忆来源依据(冻结原文)' }); }
|
|
1575
1662
|
if (button.dataset.action === 'memory-delete') { const memory = memoryById(button.dataset.id); if (memory) confirmDialog('删除这条记忆?', `「${memory.title}」将从所有组合中移除。其他小记忆保留。`, '删除', async () => {
|
|
1576
1663
|
await api(`/api/engine/memories/${encodeURIComponent(memory.id)}`, { method: 'DELETE' });
|
|
@@ -1605,6 +1692,12 @@
|
|
|
1605
1692
|
});
|
|
1606
1693
|
document.addEventListener('change', async event => {
|
|
1607
1694
|
if (event.target.id === 'projectDomainFilter') { state.projectsPage.domain = event.target.value; state.projectsPage.page = 0; state.projectsPage.cursors = [null]; await loadProjectsPage(); }
|
|
1695
|
+
if (event.target.id === 'candidateProjectFilter') {
|
|
1696
|
+
state.candidates.projectFilter = event.target.value;
|
|
1697
|
+
state.candidates.page = 0;
|
|
1698
|
+
state.candidates.cursors = [null];
|
|
1699
|
+
await loadCandidates();
|
|
1700
|
+
}
|
|
1608
1701
|
});
|
|
1609
1702
|
// 拖放:组合 → 项目行 = 应用;标签 → 拖回条 = 解除(键盘等价操作见各按钮/对话框)
|
|
1610
1703
|
let drag = null;
|
|
@@ -1660,7 +1753,7 @@
|
|
|
1660
1753
|
}
|
|
1661
1754
|
});
|
|
1662
1755
|
on('projectSelect', 'change', e => selectProject(e.target.value));
|
|
1663
|
-
on('themeToggle', 'click', () => { document.documentElement.dataset.theme = document.documentElement.dataset.theme === 'dark' ? 'light' : 'dark'; localStorage.setItem('kb.theme', document.documentElement.dataset.theme); applyAppearance(); Object.keys(frames).
|
|
1756
|
+
on('themeToggle', 'click', () => { document.documentElement.dataset.theme = document.documentElement.dataset.theme === 'dark' ? 'light' : 'dark'; localStorage.setItem('kb.theme', document.documentElement.dataset.theme); applyAppearance(); Object.keys(frames).filter(key => key !== 'workflow').forEach(postTheme); void pushKnowledgeToPanel(); });
|
|
1664
1757
|
const appearanceTokens = ['page','surface','surface2','surface3','line','text','muted','faint','nav','accent','accent-soft','good','bad','bad-soft'];
|
|
1665
1758
|
const appearanceDefaults = {
|
|
1666
1759
|
pure: {
|
|
@@ -1693,6 +1786,7 @@
|
|
|
1693
1786
|
const theme = root.dataset.theme === 'dark' ? 'dark' : 'light';
|
|
1694
1787
|
for (const token of appearanceTokens) root.style.removeProperty(`--${token}`);
|
|
1695
1788
|
for (const [token, value] of Object.entries(appearance.overrides[theme] || {})) if (appearanceTokens.includes(token)) root.style.setProperty(`--${token}`, value);
|
|
1789
|
+
postTheme('workflow');
|
|
1696
1790
|
}
|
|
1697
1791
|
function syncAppearanceControls() {
|
|
1698
1792
|
if (!$('skinPreset')) return;
|