dsh-uitest 2.3.1 → 2.4.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 +72 -11
- package/bin/uitest.mjs +189 -11
- package/client.js +81 -11
- package/host/agent-bridge.mjs +9 -2
- package/host/agent-test.mjs +1 -1
- package/host/context-budget.mjs +1 -1
- package/index.js +14 -2
- package/lib/agent-session.mjs +81 -3
- package/lib/agent-tasks.mjs +212 -0
- package/lib/batch.mjs +204 -13
- package/lib/browser-ab.mjs +21 -6
- package/lib/browser.mjs +5 -5
- package/lib/config.mjs +1 -0
- package/lib/convert.mjs +178 -40
- package/lib/doc-structure.mjs +316 -0
- package/lib/flow.mjs +49 -14
- package/lib/knowledge-central-episodes.mjs +120 -0
- package/lib/knowledge-central-import.mjs +16 -1
- package/lib/knowledge-central.mjs +149 -22
- package/lib/knowledge-clean.mjs +749 -0
- package/lib/knowledge-consolidate.mjs +1271 -0
- package/lib/knowledge-coverage.mjs +150 -0
- package/lib/knowledge-docs.mjs +3 -3
- package/lib/knowledge-effect.mjs +148 -0
- package/lib/knowledge-facts.mjs +1008 -0
- package/lib/knowledge-maintain.mjs +185 -0
- package/lib/knowledge-permission.mjs +447 -0
- package/lib/knowledge-query.mjs +304 -26
- package/lib/knowledge-store.mjs +169 -14
- package/lib/knowledge-summarize.mjs +332 -0
- package/lib/knowledge.mjs +944 -47
- package/lib/learn-run.mjs +157 -51
- package/lib/run-learn.mjs +34 -0
- package/lib/run-observe.mjs +95 -27
- package/lib/run-stats.mjs +71 -0
- package/package.json +12 -2
- package/scripts/knowledge-central-purge.mjs +248 -0
- package/scripts/knowledge-central-sync.mjs +102 -20
- package/scripts/knowledge-consolidate.mjs +158 -0
- package/scripts/knowledge-coverage.mjs +94 -0
- package/scripts/knowledge-effect.mjs +82 -0
- package/scripts/knowledge-permission-probe.mjs +196 -0
- package/scripts/knowledge-permission-report.mjs +140 -0
- package/scripts/knowledge-report.mjs +492 -0
- package/scripts/knowledge-summarize.mjs +200 -0
- package/skills/uitest/SKILL.md +4 -2
- package/skills/uitest/references/convert-knowledge.md +66 -13
- package/studio/host.mjs +120 -3
- package/studio/knowledge.mjs +195 -45
- package/studio/snapshot.mjs +6 -1
package/lib/knowledge-store.mjs
CHANGED
|
@@ -5,6 +5,8 @@ import path from 'node:path';
|
|
|
5
5
|
|
|
6
6
|
import { ConfigError, ensureDir, nowIso, slug, truncate, writeFileAtomic, writeJsonFile } from './util.mjs';
|
|
7
7
|
import { learnFromDocument } from './doc-learn.mjs';
|
|
8
|
+
import { classifyDocumentKind, isCaseReferenced, isLearnableDocument, normalizeKey } from './knowledge-clean.mjs';
|
|
9
|
+
import { extractDocumentStructure, mergeDocumentStructure } from './doc-structure.mjs';
|
|
8
10
|
import {
|
|
9
11
|
KNOWLEDGE_FILES,
|
|
10
12
|
KNOWLEDGE_MANIFEST,
|
|
@@ -658,9 +660,88 @@ export function scanBundleFolder(dir, { limit = 50 } = {}) {
|
|
|
658
660
|
return { dir, bundles, errors };
|
|
659
661
|
}
|
|
660
662
|
|
|
663
|
+
const SYSTEM_LISTS = ['roles', 'permissions', 'glossary'];
|
|
664
|
+
|
|
665
|
+
function systemEntryKey(field, entry) {
|
|
666
|
+
if (field === 'roles') return normalizeKey(entry?.name || entry);
|
|
667
|
+
if (field === 'glossary') return normalizeKey(entry?.term);
|
|
668
|
+
if (field === 'permissions') return normalizeKey(`${entry?.role || ''}|${entry?.action || ''}|${entry?.allowed}`);
|
|
669
|
+
return '';
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
function normalizeSystemEntry(field, entry) {
|
|
673
|
+
if (field === 'roles' && typeof entry === 'string') return { name: entry.trim(), source: 'agent' };
|
|
674
|
+
if (!entry || typeof entry !== 'object') return null;
|
|
675
|
+
if (field === 'roles') return entry.name ? { ...entry, name: String(entry.name).trim(), source: entry.source || 'agent' } : null;
|
|
676
|
+
if (field === 'glossary') {
|
|
677
|
+
const term = String(entry.term || entry.name || '').trim();
|
|
678
|
+
const definition = String(entry.definition || entry.desc || '').trim();
|
|
679
|
+
return term && definition ? { ...entry, term, definition, source: entry.source || 'agent' } : null;
|
|
680
|
+
}
|
|
681
|
+
if (field === 'permissions') {
|
|
682
|
+
const action = String(entry.action || entry.note || '').trim();
|
|
683
|
+
return action ? { ...entry, action, allowed: entry.allowed === undefined ? null : entry.allowed, source: entry.source || 'agent' } : null;
|
|
684
|
+
}
|
|
685
|
+
return null;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/** 把 Agent 观察到的角色/权限/术语并入 system,返回 added/merged 计数。 */
|
|
689
|
+
function mergeSystemPatch(system, patch) {
|
|
690
|
+
const counts = { roles: { added: 0, merged: 0 }, permissions: { added: 0, merged: 0 }, glossary: { added: 0, merged: 0 } };
|
|
691
|
+
for (const field of SYSTEM_LISTS) {
|
|
692
|
+
const additions = Array.isArray(patch?.[field]) ? patch[field] : [];
|
|
693
|
+
if (!Array.isArray(system[field])) system[field] = [];
|
|
694
|
+
for (const raw of additions) {
|
|
695
|
+
const entry = normalizeSystemEntry(field, raw);
|
|
696
|
+
const key = entry ? systemEntryKey(field, entry) : '';
|
|
697
|
+
if (!key) continue;
|
|
698
|
+
const index = system[field].findIndex((item) => systemEntryKey(field, normalizeSystemEntry(field, item)) === key);
|
|
699
|
+
if (index < 0) {
|
|
700
|
+
system[field].push(entry);
|
|
701
|
+
counts[field].added += 1;
|
|
702
|
+
} else {
|
|
703
|
+
system[field][index] = { ...system[field][index], ...entry };
|
|
704
|
+
counts[field].merged += 1;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
return counts;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/** 应用 Agent 观察到的文档结构;文档不存在时创建一条结构记录。 */
|
|
712
|
+
function mergeDocumentPatch(documents, entry, invalid) {
|
|
713
|
+
const title = String(entry.title || entry.name || '').trim();
|
|
714
|
+
const id = String(entry.id || (title ? slug(title) : '')).trim();
|
|
715
|
+
if (!id && !title) throw invalid('documents 条目必须包含 id/title');
|
|
716
|
+
const existing = documents.find((doc) => (id && doc.id === id) || (title && doc.title === title)) || null;
|
|
717
|
+
const base = existing || { id: id || slug(title), title: title || id, format: 'md' };
|
|
718
|
+
const structure = mergeDocumentStructure(existing || {}, entry);
|
|
719
|
+
const merged = {
|
|
720
|
+
...base,
|
|
721
|
+
...entry,
|
|
722
|
+
id: base.id,
|
|
723
|
+
title: base.title || title,
|
|
724
|
+
kind: entry.kind || existing?.kind || classifyDocumentKind({ title: base.title || title }),
|
|
725
|
+
updatedAt: nowIso(),
|
|
726
|
+
source: appendSource(existing?.source, 'agent'),
|
|
727
|
+
};
|
|
728
|
+
merged.learnable = isLearnableDocument(merged.kind);
|
|
729
|
+
for (const field of ['fields', 'statuses', 'transitions', 'roles', 'permissions', 'glossary']) {
|
|
730
|
+
if (structure[field].length) merged[field] = structure[field];
|
|
731
|
+
else delete merged[field];
|
|
732
|
+
}
|
|
733
|
+
if (existing) {
|
|
734
|
+
documents[documents.indexOf(existing)] = merged;
|
|
735
|
+
return { added: 0, merged: 1 };
|
|
736
|
+
}
|
|
737
|
+
documents.push(merged);
|
|
738
|
+
return { added: 1, merged: 0 };
|
|
739
|
+
}
|
|
740
|
+
|
|
661
741
|
/**
|
|
662
|
-
* 应用知识补丁(Agent 产出):{pages, recipes, processes, business}。
|
|
742
|
+
* 应用知识补丁(Agent 产出):{pages, recipes, processes, business, system, documents}。
|
|
663
743
|
* pages 按 id/urlName/title 合并并合并 actions;其他按 id/title/name 合并数组去重;source 追加 agent。
|
|
744
|
+
* system/documents 只能写正典事实,含用例号/单号的补丁会被拒绝。
|
|
664
745
|
*/
|
|
665
746
|
export function applyKnowledgePatch(config, patch) {
|
|
666
747
|
const invalid = (message) => new KnowledgeError(message, { code: 'KNOWLEDGE_PATCH_INVALID' });
|
|
@@ -673,6 +754,29 @@ export function applyKnowledgePatch(config, patch) {
|
|
|
673
754
|
if (!Array.isArray(value)) throw invalid(`知识补丁字段 ${field} 必须是数组`);
|
|
674
755
|
normalized[field] = value;
|
|
675
756
|
}
|
|
757
|
+
if (patch.system !== undefined && (!patch.system || typeof patch.system !== 'object' || Array.isArray(patch.system))) {
|
|
758
|
+
throw invalid('知识补丁字段 system 必须是对象');
|
|
759
|
+
}
|
|
760
|
+
for (const field of SYSTEM_LISTS) {
|
|
761
|
+
if (patch.system?.[field] !== undefined && !Array.isArray(patch.system[field])) {
|
|
762
|
+
throw invalid(`知识补丁字段 system.${field} 必须是数组`);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
if (patch.documents !== undefined && !Array.isArray(patch.documents)) {
|
|
766
|
+
throw invalid('知识补丁字段 documents 必须是数组');
|
|
767
|
+
}
|
|
768
|
+
for (const doc of patch.documents || []) {
|
|
769
|
+
if (!doc || typeof doc !== 'object' || Array.isArray(doc)) throw invalid('documents 条目必须是对象');
|
|
770
|
+
if (!doc.id && !doc.title && !doc.name) throw invalid('documents 条目必须包含 id/title');
|
|
771
|
+
for (const field of ['fields', 'statuses', 'transitions', 'roles', 'permissions', 'glossary']) {
|
|
772
|
+
if (doc[field] !== undefined && !Array.isArray(doc[field])) throw invalid(`documents[].${field} 必须是数组`);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
if ((patch.system || patch.documents) && isCaseReferenced(JSON.stringify({ system: patch.system, documents: patch.documents }))) {
|
|
776
|
+
throw new KnowledgeError('知识补丁的 system/documents 只能写系统级事实,不能含用例号或单据号', {
|
|
777
|
+
code: 'KNOWLEDGE_PATCH_INSTANCE_REF',
|
|
778
|
+
});
|
|
779
|
+
}
|
|
676
780
|
for (const page of normalized.pages) {
|
|
677
781
|
if (!page || typeof page !== 'object' || Array.isArray(page)) throw invalid('pages 条目必须是对象');
|
|
678
782
|
if (!page.id && !page.urlName && !page.title) throw invalid('pages 条目必须包含 id/urlName/title 之一');
|
|
@@ -826,6 +930,15 @@ export function applyKnowledgePatch(config, patch) {
|
|
|
826
930
|
counts.business.added += 1;
|
|
827
931
|
}
|
|
828
932
|
}
|
|
933
|
+
const systemCounts = mergeSystemPatch(knowledge.system, patch.system || {});
|
|
934
|
+
const documentCounts = { added: 0, merged: 0 };
|
|
935
|
+
for (const entry of patch.documents || []) {
|
|
936
|
+
const result = mergeDocumentPatch(knowledge.documents.documents, entry, invalid);
|
|
937
|
+
documentCounts.added += result.added;
|
|
938
|
+
documentCounts.merged += result.merged;
|
|
939
|
+
}
|
|
940
|
+
knowledge.system.updatedAt = nowIso();
|
|
941
|
+
knowledge.documents.updatedAt = nowIso();
|
|
829
942
|
knowledge.pages.updatedAt = nowIso();
|
|
830
943
|
knowledge.recipes.updatedAt = nowIso();
|
|
831
944
|
knowledge.processes.updatedAt = nowIso();
|
|
@@ -834,6 +947,8 @@ export function applyKnowledgePatch(config, patch) {
|
|
|
834
947
|
return {
|
|
835
948
|
ok: true,
|
|
836
949
|
...counts,
|
|
950
|
+
system: systemCounts,
|
|
951
|
+
documents: documentCounts,
|
|
837
952
|
target: { source: target.source, dir: target.dir, name: target.name || target.folderName || target.libraryName || '项目本地' },
|
|
838
953
|
};
|
|
839
954
|
}
|
|
@@ -867,24 +982,42 @@ export function importDocument(config, { file, name, system, into, noLearn = fal
|
|
|
867
982
|
const proxy = proxyForDir(targetDir);
|
|
868
983
|
ensureDir(targetDir);
|
|
869
984
|
const knowledge = loadKnowledge(proxy, { create: true });
|
|
870
|
-
|
|
985
|
+
const parsedDocumentTitle =
|
|
986
|
+
parsed.document.title && parsed.document.title !== '导入文档' ? parsed.document.title : '';
|
|
987
|
+
const documentTitle = String(name || parsedDocumentTitle || path.basename(absolute, ext));
|
|
988
|
+
// 文档类型决定能否喂养正典:测试报告/会议纪要只作为证据,不生成页面/规则/系统维度。
|
|
989
|
+
const documentKind = classifyDocumentKind({ title: documentTitle, content });
|
|
990
|
+
const learnable = isLearnableDocument(documentKind);
|
|
991
|
+
// 证据文档(测试报告/会议纪要)只登记文档本身,不抽取字段/状态等正典结构。
|
|
992
|
+
const documentStructure = learnable
|
|
993
|
+
? extractDocumentStructure(content, { title: documentTitle })
|
|
994
|
+
: { fields: [], statuses: [], transitions: [], roles: [], permissions: [], glossary: [] };
|
|
995
|
+
if ((parsed.system && learnable) || system) {
|
|
871
996
|
knowledge.system = mergeSystem(knowledge.system, {
|
|
872
|
-
...(parsed.system
|
|
997
|
+
...(learnable && parsed.system ? parsed.system : {}),
|
|
873
998
|
...(system ? { name: String(system), title: String(system) } : {}),
|
|
874
999
|
source: 'import-doc',
|
|
875
1000
|
});
|
|
876
1001
|
}
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
1002
|
+
if (learnable) {
|
|
1003
|
+
for (const page of parsed.pages) {
|
|
1004
|
+
mergeImportedPage(knowledge.pages.pages, page, documentTitle);
|
|
1005
|
+
}
|
|
1006
|
+
for (const entry of parsed.business) mergeImportedBusiness(knowledge.business.entries, entry);
|
|
1007
|
+
for (const process of parsed.processes) mergeImportedProcess(knowledge.processes.processes, process);
|
|
1008
|
+
const withSource = (list) => list.map((item) => ({ ...item, source: documentTitle }));
|
|
1009
|
+
mergeSystemPatch(knowledge.system, {
|
|
1010
|
+
roles: withSource(documentStructure.roles),
|
|
1011
|
+
permissions: withSource(documentStructure.permissions),
|
|
1012
|
+
glossary: withSource(documentStructure.glossary),
|
|
1013
|
+
});
|
|
1014
|
+
knowledge.system.updatedAt = nowIso();
|
|
882
1015
|
}
|
|
883
|
-
for (const entry of parsed.business) mergeImportedBusiness(knowledge.business.entries, entry);
|
|
884
|
-
for (const process of parsed.processes) mergeImportedProcess(knowledge.processes.processes, process);
|
|
885
1016
|
|
|
886
|
-
//
|
|
887
|
-
const
|
|
1017
|
+
// 文档自动学习:仅设计文档/手册/术语表参与;失败不影响导入
|
|
1018
|
+
const learnEnvOff = String(process.env.UITEST_DOC_LEARN || '').toLowerCase() === 'off';
|
|
1019
|
+
const learnEnabled = !noLearn && !learnEnvOff && learnable;
|
|
1020
|
+
const learnSkipped = noLearn ? 'requested' : learnEnvOff ? 'env' : !learnable ? 'document-kind:' + documentKind : '';
|
|
888
1021
|
const warnings = [];
|
|
889
1022
|
let learnedResult = null;
|
|
890
1023
|
const learned = { documents: 0, pages: 0, business: 0, processes: 0, rules: 0, pagesMerged: 0, businessAdded: 0, processesAdded: 0 };
|
|
@@ -934,17 +1067,37 @@ export function importDocument(config, { file, name, system, into, noLearn = fal
|
|
|
934
1067
|
}
|
|
935
1068
|
}
|
|
936
1069
|
|
|
1070
|
+
const documentId = slug(documentTitle);
|
|
1071
|
+
const existingDocument = knowledge.documents.documents.find((item) => item.id === documentId);
|
|
1072
|
+
const mergedStructure = mergeDocumentStructure(existingDocument || {}, documentStructure);
|
|
937
1073
|
const document = {
|
|
938
1074
|
...parsed.document,
|
|
939
|
-
id:
|
|
1075
|
+
id: documentId,
|
|
940
1076
|
title: documentTitle,
|
|
941
1077
|
path: absolute,
|
|
942
1078
|
importedAt: nowIso(),
|
|
1079
|
+
kind: documentKind,
|
|
1080
|
+
learnable,
|
|
1081
|
+
...(learnable && mergedStructure.fields.length ? { fields: mergedStructure.fields } : {}),
|
|
1082
|
+
...(learnable && mergedStructure.statuses.length ? { statuses: mergedStructure.statuses } : {}),
|
|
1083
|
+
...(learnable && mergedStructure.transitions.length ? { transitions: mergedStructure.transitions } : {}),
|
|
1084
|
+
...(learnable && mergedStructure.roles.length ? { roles: mergedStructure.roles } : {}),
|
|
1085
|
+
...(learnable && mergedStructure.permissions.length ? { permissions: mergedStructure.permissions } : {}),
|
|
1086
|
+
...(learnable && mergedStructure.glossary.length ? { glossary: mergedStructure.glossary } : {}),
|
|
943
1087
|
...(learnedResult
|
|
944
1088
|
? { summary: learnedResult.summary.text, learned: { documents: 1, pages: learned.pages, business: learned.business, processes: learned.processes, rules: learned.rules } }
|
|
945
1089
|
: {}),
|
|
1090
|
+
...(learnSkipped ? { learnSkipped } : {}),
|
|
946
1091
|
};
|
|
947
|
-
|
|
1092
|
+
if (!learnable) {
|
|
1093
|
+
mergeById(knowledge.documents.documents, document, { arrays: ['headings'] });
|
|
1094
|
+
const saved = knowledge.documents.documents.find((item) => item.id === documentId);
|
|
1095
|
+
for (const field of ['fields', 'statuses', 'transitions', 'roles', 'permissions', 'glossary']) {
|
|
1096
|
+
if (saved) delete saved[field];
|
|
1097
|
+
}
|
|
1098
|
+
} else {
|
|
1099
|
+
mergeById(knowledge.documents.documents, document, { arrays: ['headings'] });
|
|
1100
|
+
}
|
|
948
1101
|
knowledge.pages.updatedAt = nowIso();
|
|
949
1102
|
knowledge.business.updatedAt = nowIso();
|
|
950
1103
|
knowledge.processes.updatedAt = nowIso();
|
|
@@ -955,6 +1108,8 @@ export function importDocument(config, { file, name, system, into, noLearn = fal
|
|
|
955
1108
|
targetDir,
|
|
956
1109
|
target: into ? String(into) : undefined,
|
|
957
1110
|
document,
|
|
1111
|
+
documentKind,
|
|
1112
|
+
learnSkipped: learnSkipped || undefined,
|
|
958
1113
|
pages: knowledge.pages.pages.length,
|
|
959
1114
|
business: knowledge.business.entries.length,
|
|
960
1115
|
processes: knowledge.processes.processes.length,
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
// 知识摘要:把正典页面/流程/规则/单据/系统压缩成可复用的一句话摘要。
|
|
2
|
+
// 规划与应用是纯函数;LLM 调用由 scripts/knowledge-summarize.mjs 通过中心 /summarize 完成。
|
|
3
|
+
import crypto from 'node:crypto';
|
|
4
|
+
|
|
5
|
+
import { cleanCanonicalText, isCaseReferenced, isLearnableDocument } from './knowledge-clean.mjs';
|
|
6
|
+
import { truncate } from './util.mjs';
|
|
7
|
+
|
|
8
|
+
export const SUMMARY_KINDS = ['page', 'process', 'business', 'document', 'system'];
|
|
9
|
+
const MAX_INPUT_CHARS = 8000;
|
|
10
|
+
const MAX_SUMMARY_CHARS = 600;
|
|
11
|
+
|
|
12
|
+
function textOf(value) {
|
|
13
|
+
if (value === null || value === undefined) return '';
|
|
14
|
+
if (typeof value === 'string') return cleanCanonicalText(value);
|
|
15
|
+
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
|
|
16
|
+
if (Array.isArray(value)) return value.map(textOf).filter(Boolean).join('、');
|
|
17
|
+
if (typeof value === 'object') {
|
|
18
|
+
return textOf(value.text || value.desc || value.title || value.name || value.action || value.value || '');
|
|
19
|
+
}
|
|
20
|
+
return '';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function linesOf(values, { limit = 40, maxLength = 300 } = {}) {
|
|
24
|
+
const seen = new Set();
|
|
25
|
+
const lines = [];
|
|
26
|
+
for (const value of Array.isArray(values) ? values : []) {
|
|
27
|
+
const text = truncate(textOf(value), maxLength);
|
|
28
|
+
if (!text || seen.has(text)) continue;
|
|
29
|
+
seen.add(text);
|
|
30
|
+
lines.push(text);
|
|
31
|
+
if (lines.length >= limit) break;
|
|
32
|
+
}
|
|
33
|
+
return lines;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function stepText(step) {
|
|
37
|
+
if (typeof step === 'string') return cleanCanonicalText(step);
|
|
38
|
+
if (!step || typeof step !== 'object') return '';
|
|
39
|
+
const parts = [step.text, step.desc, step.action, step.selector].map(textOf).filter(Boolean);
|
|
40
|
+
if (!parts.length) return '';
|
|
41
|
+
if (step.action && step.selector) return cleanCanonicalText(step.action + ' ' + step.selector);
|
|
42
|
+
return cleanCanonicalText(parts.join(' '));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function pageText(page) {
|
|
46
|
+
const blocks = [];
|
|
47
|
+
if (page.title) blocks.push('页面:' + textOf(page.title));
|
|
48
|
+
if (page.url) blocks.push('入口:' + truncate(textOf(page.url), 500));
|
|
49
|
+
const keywords = linesOf(page.keywords, { limit: 20, maxLength: 80 });
|
|
50
|
+
if (keywords.length) blocks.push('关键词:' + keywords.join('、'));
|
|
51
|
+
const headers = linesOf(page.table?.headers, { limit: 40, maxLength: 80 });
|
|
52
|
+
if (headers.length) blocks.push('表头:' + headers.join('、'));
|
|
53
|
+
const actions = [];
|
|
54
|
+
for (const action of (page.actions || []).slice(0, 20)) {
|
|
55
|
+
const title = textOf(action?.title || action?.id);
|
|
56
|
+
const steps = (action?.steps || []).map(stepText).filter(Boolean).slice(0, 10);
|
|
57
|
+
const notes = linesOf(action?.notes, { limit: 3, maxLength: 240 });
|
|
58
|
+
const line = [title, ...steps, ...notes].filter(Boolean).join(';');
|
|
59
|
+
if (line) actions.push('- ' + truncate(line, 500));
|
|
60
|
+
}
|
|
61
|
+
if (actions.length) blocks.push('操作:\n' + actions.join('\n'));
|
|
62
|
+
const notes = linesOf(page.notes, { limit: 10, maxLength: 300 });
|
|
63
|
+
if (notes.length) blocks.push('提示:' + notes.join(';'));
|
|
64
|
+
const hints = linesOf([...(page.elementHints || []), ...(page.assertionHints || [])], { limit: 10, maxLength: 240 });
|
|
65
|
+
if (hints.length) blocks.push('定位/断言:' + hints.join(';'));
|
|
66
|
+
return blocks.join('\n');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function processText(process) {
|
|
70
|
+
const blocks = [];
|
|
71
|
+
if (process.name || process.title) blocks.push('流程:' + textOf(process.name || process.title));
|
|
72
|
+
const keywords = linesOf(process.keywords, { limit: 20, maxLength: 80 });
|
|
73
|
+
if (keywords.length) blocks.push('关键词:' + keywords.join('、'));
|
|
74
|
+
const steps = (process.steps || []).map(stepText).filter(Boolean).slice(0, 40);
|
|
75
|
+
if (steps.length) blocks.push('步骤:\n' + steps.map((step, index) => `${index + 1}. ${truncate(step, 400)}`).join('\n'));
|
|
76
|
+
const notes = linesOf(process.notes, { limit: 10, maxLength: 300 });
|
|
77
|
+
if (notes.length) blocks.push('注意事项:' + notes.join(';'));
|
|
78
|
+
return blocks.join('\n');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function businessText(entry) {
|
|
82
|
+
const blocks = [];
|
|
83
|
+
if (entry.title || entry.name) blocks.push('规则集:' + textOf(entry.title || entry.name));
|
|
84
|
+
const rules = linesOf(entry.rules, { limit: 40, maxLength: 400 });
|
|
85
|
+
if (rules.length) blocks.push('规则:\n' + rules.map((rule) => '- ' + truncate(rule, 400)).join('\n'));
|
|
86
|
+
if (!rules.length && entry.text) blocks.push('说明:' + truncate(textOf(entry.text), 1000));
|
|
87
|
+
return blocks.join('\n');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function documentText(doc) {
|
|
91
|
+
const blocks = [];
|
|
92
|
+
blocks.push('单据:' + textOf(doc.title || doc.id) + (doc.kind ? '(' + doc.kind + ')' : ''));
|
|
93
|
+
const fields = linesOf((doc.fields || []).map((field) => {
|
|
94
|
+
if (!field || typeof field !== 'object') return textOf(field);
|
|
95
|
+
const meta = [field.type, field.required ? '必填' : ''].filter(Boolean).join(',');
|
|
96
|
+
return textOf(field.name || field.title) + (meta ? '(' + meta + ')' : '');
|
|
97
|
+
}), { limit: 80, maxLength: 100 });
|
|
98
|
+
if (fields.length) blocks.push('字段:' + fields.join('、'));
|
|
99
|
+
const statuses = linesOf((doc.statuses || []).map((status) => {
|
|
100
|
+
if (!status || typeof status !== 'object') return textOf(status);
|
|
101
|
+
const name = textOf(status.name || status.title);
|
|
102
|
+
const meaning = textOf(status.meaning || status.desc);
|
|
103
|
+
return name + (meaning ? '(' + meaning + ')' : '');
|
|
104
|
+
}), { limit: 40, maxLength: 160 });
|
|
105
|
+
if (statuses.length) blocks.push('状态:' + statuses.join('、'));
|
|
106
|
+
const transitions = linesOf((doc.transitions || []).map((item) => {
|
|
107
|
+
if (!item || typeof item !== 'object') return textOf(item);
|
|
108
|
+
const from = textOf(item.from || item.source);
|
|
109
|
+
const to = textOf(item.to || item.target);
|
|
110
|
+
const trigger = textOf(item.trigger || item.action || item.via);
|
|
111
|
+
if (!from || !to) return '';
|
|
112
|
+
return from + ' → ' + to + (trigger ? '(' + trigger + ')' : '');
|
|
113
|
+
}), { limit: 60, maxLength: 180 });
|
|
114
|
+
if (transitions.length) blocks.push('流转:' + transitions.join(';'));
|
|
115
|
+
const roles = linesOf((doc.roles || []).map((role) => role?.name || role), { limit: 30, maxLength: 80 });
|
|
116
|
+
if (roles.length) blocks.push('角色:' + roles.join('、'));
|
|
117
|
+
const permissions = linesOf((doc.permissions || []).map((permission) => {
|
|
118
|
+
if (!permission || typeof permission !== 'object') return textOf(permission);
|
|
119
|
+
return [permission.role, permission.action, permission.allowed === false ? '禁止' : '允许'].filter(Boolean).join(' ');
|
|
120
|
+
}), { limit: 40, maxLength: 120 });
|
|
121
|
+
if (permissions.length) blocks.push('权限:' + permissions.join(';'));
|
|
122
|
+
const glossary = linesOf((doc.glossary || []).map((term) => {
|
|
123
|
+
if (!term || typeof term !== 'object') return textOf(term);
|
|
124
|
+
return textOf(term.term || term.name) + (term.definition ? ':' + textOf(term.definition) : '');
|
|
125
|
+
}), { limit: 60, maxLength: 240 });
|
|
126
|
+
if (glossary.length) blocks.push('术语:' + glossary.join(';'));
|
|
127
|
+
return blocks.join('\n');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function systemText(system) {
|
|
131
|
+
const blocks = [];
|
|
132
|
+
blocks.push('系统:' + textOf(system.system || system.name || system.title || '订单管理系统'));
|
|
133
|
+
const menus = [];
|
|
134
|
+
if (system.menus && typeof system.menus === 'object' && !Array.isArray(system.menus)) {
|
|
135
|
+
for (const [group, items] of Object.entries(system.menus)) {
|
|
136
|
+
const names = linesOf(items, { limit: 40, maxLength: 80 });
|
|
137
|
+
if (names.length) menus.push(group + ':' + names.join('、'));
|
|
138
|
+
}
|
|
139
|
+
} else {
|
|
140
|
+
menus.push(...linesOf(system.menus, { limit: 80, maxLength: 120 }));
|
|
141
|
+
}
|
|
142
|
+
if (menus.length) blocks.push('菜单:' + menus.join(';'));
|
|
143
|
+
const urls = linesOf(Object.values(system.urls || {}), { limit: 40, maxLength: 200 });
|
|
144
|
+
if (urls.length) blocks.push('入口:' + urls.join(';'));
|
|
145
|
+
const roles = linesOf((system.roles || []).map((role) => role?.name || role), { limit: 60, maxLength: 80 });
|
|
146
|
+
if (roles.length) blocks.push('角色:' + roles.join('、'));
|
|
147
|
+
const permissions = linesOf((system.permissions || []).map((permission) => {
|
|
148
|
+
if (!permission || typeof permission !== 'object') return textOf(permission);
|
|
149
|
+
return [permission.role, permission.action, permission.allowed === false ? '禁止' : '允许'].filter(Boolean).join(' ');
|
|
150
|
+
}), { limit: 80, maxLength: 120 });
|
|
151
|
+
if (permissions.length) blocks.push('权限:' + permissions.join(';'));
|
|
152
|
+
const glossary = linesOf((system.glossary || []).map((term) => {
|
|
153
|
+
if (!term || typeof term !== 'object') return textOf(term);
|
|
154
|
+
return textOf(term.term || term.name) + (term.definition ? ':' + textOf(term.definition) : '');
|
|
155
|
+
}), { limit: 80, maxLength: 240 });
|
|
156
|
+
if (glossary.length) blocks.push('术语:' + glossary.join(';'));
|
|
157
|
+
const notes = linesOf(system.notes, { limit: 10, maxLength: 240 });
|
|
158
|
+
if (notes.length) blocks.push('提示:' + notes.join(';'));
|
|
159
|
+
return blocks.join('\n');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function summaryInput(kind, record) {
|
|
163
|
+
if (kind === 'page') return pageText(record || {});
|
|
164
|
+
if (kind === 'process') return processText(record || {});
|
|
165
|
+
if (kind === 'business') return businessText(record || {});
|
|
166
|
+
if (kind === 'document') return documentText(record || {});
|
|
167
|
+
if (kind === 'system') return systemText(record || {});
|
|
168
|
+
return '';
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function summaryHashFor(kind, record) {
|
|
172
|
+
const text = summaryInput(kind, record);
|
|
173
|
+
if (!text) return '';
|
|
174
|
+
return crypto.createHash('sha1').update(text).digest('hex');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 清理与当前内容不匹配的 LLM 摘要。合并/清洗会改写页面/流程/规则,
|
|
179
|
+
* 旧摘要若继续保留就会在重新摘要前冒充最新事实;无 summaryHash 的
|
|
180
|
+
* 旧文档摘要(doc-learn 生成)不动。
|
|
181
|
+
*/
|
|
182
|
+
export function invalidateStaleSummaries(knowledge) {
|
|
183
|
+
let removed = 0;
|
|
184
|
+
for (const kind of SUMMARY_KINDS) {
|
|
185
|
+
for (const target of targetsOf(knowledge, kind)) {
|
|
186
|
+
const record = target.activity;
|
|
187
|
+
if (!record?.summaryHash) continue;
|
|
188
|
+
if (record.summaryHash === summaryHashFor(kind, record)) continue;
|
|
189
|
+
delete record.summary;
|
|
190
|
+
delete record.summaryHash;
|
|
191
|
+
delete record.summaryAt;
|
|
192
|
+
delete record.summarySource;
|
|
193
|
+
removed += 1;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return removed;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function targetsOf(knowledge, kind) {
|
|
200
|
+
if (kind === 'page') return (knowledge.pages?.pages || []).map((record) => ({ kind, id: record.id, activity: record }));
|
|
201
|
+
if (kind === 'process') return (knowledge.processes?.processes || []).map((record) => ({ kind, id: record.id, activity: record }));
|
|
202
|
+
if (kind === 'business') return (knowledge.business?.entries || []).map((record) => ({ kind, id: record.id, activity: record }));
|
|
203
|
+
if (kind === 'document') return (knowledge.documents?.documents || []).map((record) => ({ kind, id: record.id, activity: record }));
|
|
204
|
+
if (kind === 'system') return knowledge.system ? [{ kind, id: 'system', activity: knowledge.system }] : [];
|
|
205
|
+
return [];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function eligible(kind, record) {
|
|
209
|
+
if (!record) return false;
|
|
210
|
+
if (kind === 'page') {
|
|
211
|
+
if (record.status === 'stub') return false;
|
|
212
|
+
// 只有 URL 的空页面没有可摘要内容,等实测补齐动作/表头/关键词后再生成摘要。
|
|
213
|
+
return Boolean(
|
|
214
|
+
(record.actions || []).length
|
|
215
|
+
|| (record.table?.headers || []).length
|
|
216
|
+
|| (record.keywords || []).length
|
|
217
|
+
|| (record.notes || []).length
|
|
218
|
+
|| (record.elementHints || []).length
|
|
219
|
+
|| (record.assertionHints || []).length,
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
if (kind === 'process') return (record.steps || []).length > 0;
|
|
223
|
+
if (kind === 'business') return (record.rules || []).length > 0;
|
|
224
|
+
if (kind === 'document') {
|
|
225
|
+
if (record.learnable === false || !isLearnableDocument(record.kind)) return false;
|
|
226
|
+
return ['fields', 'statuses', 'transitions', 'roles', 'permissions', 'glossary'].some((field) => (record[field] || []).length);
|
|
227
|
+
}
|
|
228
|
+
if (kind === 'system') {
|
|
229
|
+
return ['menus', 'urls', 'roles', 'permissions', 'glossary'].some((field) => {
|
|
230
|
+
const value = record[field];
|
|
231
|
+
return Array.isArray(value) ? value.length > 0 : Boolean(value && Object.keys(value).length);
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* 规划需要摘要的目标。已存在且摘要输入未变化时跳过,除非 force。
|
|
239
|
+
* @returns {{items:Array, skippedUpToDate:number, skippedIneligible:number, byKind:object}}
|
|
240
|
+
*/
|
|
241
|
+
export function planSummaries(knowledge, { kinds = SUMMARY_KINDS, force = false, limit = 0 } = {}) {
|
|
242
|
+
const wanted = new Set(kinds.filter((kind) => SUMMARY_KINDS.includes(kind)));
|
|
243
|
+
const items = [];
|
|
244
|
+
const byKind = {};
|
|
245
|
+
let skippedUpToDate = 0;
|
|
246
|
+
let skippedIneligible = 0;
|
|
247
|
+
for (const kind of SUMMARY_KINDS) {
|
|
248
|
+
if (!wanted.has(kind)) continue;
|
|
249
|
+
for (const target of targetsOf(knowledge, kind)) {
|
|
250
|
+
if (!target.id || !eligible(kind, target.activity)) {
|
|
251
|
+
skippedIneligible += 1;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const text = summaryInput(kind, target.activity);
|
|
255
|
+
if (!text) {
|
|
256
|
+
skippedIneligible += 1;
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
const hash = summaryHashFor(kind, target.activity);
|
|
260
|
+
if (!force && target.activity.summary && target.activity.summaryHash === hash) {
|
|
261
|
+
skippedUpToDate += 1;
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
items.push({
|
|
265
|
+
key: kind + ':' + target.id,
|
|
266
|
+
kind,
|
|
267
|
+
id: String(target.id),
|
|
268
|
+
title: textOf(kind === 'system' ? (target.activity.system || target.activity.name) : (target.activity.title || target.activity.name || target.activity.id)),
|
|
269
|
+
text: truncate(text, MAX_INPUT_CHARS),
|
|
270
|
+
hash,
|
|
271
|
+
});
|
|
272
|
+
byKind[kind] = (byKind[kind] || 0) + 1;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return {
|
|
276
|
+
items: limit > 0 ? items.slice(0, limit) : items,
|
|
277
|
+
skippedUpToDate,
|
|
278
|
+
skippedIneligible,
|
|
279
|
+
byKind,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function findTarget(knowledge, item) {
|
|
284
|
+
if (item.kind === 'system') return knowledge.system || null;
|
|
285
|
+
if (item.kind === 'page') return (knowledge.pages?.pages || []).find((record) => String(record.id) === String(item.id)) || null;
|
|
286
|
+
if (item.kind === 'process') return (knowledge.processes?.processes || []).find((record) => String(record.id) === String(item.id)) || null;
|
|
287
|
+
if (item.kind === 'business') return (knowledge.business?.entries || []).find((record) => String(record.id) === String(item.id)) || null;
|
|
288
|
+
if (item.kind === 'document') return (knowledge.documents?.documents || []).find((record) => String(record.id) === String(item.id)) || null;
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function cleanSummary(value) {
|
|
293
|
+
const text = cleanCanonicalText(String(value || '').replace(/\s+/g, ' ').trim());
|
|
294
|
+
if (!text || isCaseReferenced(text)) return '';
|
|
295
|
+
return truncate(text, MAX_SUMMARY_CHARS);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* 把 LLM 结果写回知识对象。逐项校验来源哈希,知识在规划后变更的条目跳过。
|
|
300
|
+
* @returns {{applied:number, skipped:number, stale:number, rejected:number, byKind:object, appliedItems:Array}}
|
|
301
|
+
*/
|
|
302
|
+
export function applySummaries(knowledge, items = [], results = [], { at = '', source = 'llm' } = {}) {
|
|
303
|
+
const byKey = new Map((Array.isArray(results) ? results : []).map((result) => [String(result?.key || result?.id || ''), result]));
|
|
304
|
+
const stats = { applied: 0, skipped: 0, stale: 0, rejected: 0, byKind: {}, appliedItems: [], rejectedItems: [] };
|
|
305
|
+
for (const item of items) {
|
|
306
|
+
const target = findTarget(knowledge, item);
|
|
307
|
+
if (!target) {
|
|
308
|
+
stats.skipped += 1;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
const currentHash = summaryHashFor(item.kind, target);
|
|
312
|
+
if (currentHash !== item.hash) {
|
|
313
|
+
stats.stale += 1;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
const result = byKey.get(item.key) || byKey.get(item.id);
|
|
317
|
+
const summary = cleanSummary(result?.summary);
|
|
318
|
+
if (!summary) {
|
|
319
|
+
stats.rejected += 1;
|
|
320
|
+
stats.rejectedItems.push({ key: item.key, kind: item.kind, id: item.id });
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
target.summary = summary;
|
|
324
|
+
target.summaryHash = item.hash;
|
|
325
|
+
target.summaryAt = at || '';
|
|
326
|
+
target.summarySource = source;
|
|
327
|
+
stats.applied += 1;
|
|
328
|
+
stats.byKind[item.kind] = (stats.byKind[item.kind] || 0) + 1;
|
|
329
|
+
stats.appliedItems.push({ key: item.key, kind: item.kind, id: item.id, summary });
|
|
330
|
+
}
|
|
331
|
+
return stats;
|
|
332
|
+
}
|