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/convert.mjs
CHANGED
|
@@ -21,7 +21,6 @@ export const CONVERT_DIAGNOSTICS = [
|
|
|
21
21
|
'INVALID_FLOW',
|
|
22
22
|
'CASE_NOT_FOUND',
|
|
23
23
|
'REGENERATED',
|
|
24
|
-
'RUN_HISTORY_ASSERTION',
|
|
25
24
|
'RUN_HISTORY_SELECTOR',
|
|
26
25
|
'RUN_HISTORY_TIMING',
|
|
27
26
|
'NO_RUN_HISTORY',
|
|
@@ -118,6 +117,12 @@ export function caseText(caseItem = {}) {
|
|
|
118
117
|
.join(' ');
|
|
119
118
|
}
|
|
120
119
|
|
|
120
|
+
/** 页面打分文本:去掉前置条件里的登录噪音,否则每个用例的「客服登录」都会给登录页加分。 */
|
|
121
|
+
function pageMatchText(caseItem = {}) {
|
|
122
|
+
const precondition = String(caseItem?.precondition || '').replace(/登录[^,。;\s]*/g, '');
|
|
123
|
+
return caseText({ ...caseItem, precondition });
|
|
124
|
+
}
|
|
125
|
+
|
|
121
126
|
/** case 的 l1/l2 文本(页面标题优先级最高)。 */
|
|
122
127
|
export function caseL2L1Text(caseItem = {}) {
|
|
123
128
|
return [caseItem.l1, caseItem.l2].filter(Boolean).join(' ');
|
|
@@ -128,7 +133,7 @@ export function caseL2L1Text(caseItem = {}) {
|
|
|
128
133
|
* 基础分(scorePage)+ 标题命中 +6(l2/l1 含 page.title,或括号内页面名命中)。
|
|
129
134
|
*/
|
|
130
135
|
export function scorePageForCase(page, caseItem) {
|
|
131
|
-
const base = scorePage(page,
|
|
136
|
+
const base = scorePage(page, pageMatchText(caseItem));
|
|
132
137
|
const l2l1 = caseL2L1Text(caseItem);
|
|
133
138
|
let titleBoost = 0;
|
|
134
139
|
if (page?.title && l2l1.includes(String(page.title))) titleBoost = 6;
|
|
@@ -162,14 +167,20 @@ export function scorePage(page, text) {
|
|
|
162
167
|
return { score, keywordHits, headerHits, actionHits, textHit };
|
|
163
168
|
}
|
|
164
169
|
|
|
165
|
-
/**
|
|
170
|
+
/** 可执行页面:有跳转地址、入口页或动作,才有办法生成出能跑起来的流程。 */
|
|
171
|
+
export function navigablePage(page) {
|
|
172
|
+
return Boolean(page?.urlName || page?.url || page?.basePageId || (page?.actions || []).length);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** 在 knowledge.pages 中为 case 找最优页面(标题命中优先;同等条件下只考虑可执行页面)。 */
|
|
166
176
|
export function findPageForCase(knowledge, caseItem) {
|
|
167
177
|
const pages = Array.isArray(knowledge?.pages?.pages) ? knowledge.pages.pages : [];
|
|
168
178
|
const scoredPages = pages
|
|
169
179
|
.map((page) => ({ page, ...scorePageForCase(page, caseItem) }))
|
|
170
180
|
.sort((a, b) => b.score - a.score);
|
|
171
|
-
const best = scoredPages[0] || null;
|
|
172
|
-
|
|
181
|
+
const best = scoredPages.find((item) => navigablePage(item.page)) || scoredPages[0] || null;
|
|
182
|
+
const ranked = scoredPages.filter((item) => navigablePage(item.page));
|
|
183
|
+
return { best, pages, scoredPages, ranked };
|
|
173
184
|
}
|
|
174
185
|
|
|
175
186
|
/* ------------------------------------------------------------------ */
|
|
@@ -203,6 +214,10 @@ export function resolveEntryPage(pages, page) {
|
|
|
203
214
|
const detailLike =
|
|
204
215
|
/detail|详情/i.test(String(page.id || '')) || /详情/.test(String(page.title || ''));
|
|
205
216
|
if (!detailLike) return { entryPage: page, detailViaList: false };
|
|
217
|
+
const byBase = page.basePageId
|
|
218
|
+
? (pages || []).find((candidate) => candidate.id === page.basePageId && candidate.id !== page.id)
|
|
219
|
+
: null;
|
|
220
|
+
if (byBase) return { entryPage: byBase, detailViaList: true };
|
|
206
221
|
const entry = (pages || []).find(
|
|
207
222
|
(candidate) =>
|
|
208
223
|
candidate.id !== page.id &&
|
|
@@ -243,6 +258,50 @@ function openingActionIds(pagesList) {
|
|
|
243
258
|
return ids;
|
|
244
259
|
}
|
|
245
260
|
|
|
261
|
+
const DIALOG_SELECTOR_RE = /\.ui-dialog-wrap/;
|
|
262
|
+
|
|
263
|
+
function stepTargetsDialog(step) {
|
|
264
|
+
return DIALOG_SELECTOR_RE.test(String(step?.selector || '')) || DIALOG_SELECTOR_RE.test(String(step?.text || ''));
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function stepOpensDetail(step) {
|
|
268
|
+
return step?.action === 'click' && /详情/.test(String(step.text || '')) && !DIALOG_SELECTOR_RE.test(String(step.text || ''));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** 能完成「查询 → 点详情」的入口动作;优先步骤最少且含搜索链路的一条。 */
|
|
272
|
+
function findDetailOpener(pagesList) {
|
|
273
|
+
let best = null;
|
|
274
|
+
for (const page of pagesList || []) {
|
|
275
|
+
for (const action of page?.actions || []) {
|
|
276
|
+
const steps = action?.steps || [];
|
|
277
|
+
if (!/open.*detail|detail.*open|打开.*详情|进入.*详情|查看.*详情/i.test(`${action.id || ''} ${action.title || ''}`)) continue;
|
|
278
|
+
const hasSearch = steps.some((step) => step.action === 'click' && /搜索|查询/.test(String(step.text || '')));
|
|
279
|
+
const hasFill = steps.some((step) => step.action === 'fill');
|
|
280
|
+
const hasDetail = steps.some((step) => step.action === 'click' && /详情/.test(String(step.text || '')));
|
|
281
|
+
if (!(hasSearch && hasFill && hasDetail)) continue;
|
|
282
|
+
if (!best || steps.length < best.action.steps.length) best = { page, action };
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return best;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** 打开计划详情(中间弹窗)的动作:在右侧抽屉里点计划行「详情」,并等待计划详情标题。 */
|
|
289
|
+
function findPlanModalOpener(pagesList) {
|
|
290
|
+
for (const page of pagesList || []) {
|
|
291
|
+
for (const action of page?.actions || []) {
|
|
292
|
+
const steps = action?.steps || [];
|
|
293
|
+
const opens = steps.some(
|
|
294
|
+
(step) => step.action === 'click' && DIALOG_SELECTOR_RE.test(String(step.selector || '')) && /ui-dialog-wrap-right/.test(String(step.selector || '')),
|
|
295
|
+
);
|
|
296
|
+
const waits = steps.some(
|
|
297
|
+
(step) => ['wait', 'expect'].includes(step.action) && /履约计划详情/.test(String(step.text || '')),
|
|
298
|
+
);
|
|
299
|
+
if (opens && waits) return { page, action };
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return null;
|
|
303
|
+
}
|
|
304
|
+
|
|
246
305
|
/**
|
|
247
306
|
* 解析 case.steps → flow steps。
|
|
248
307
|
* - action 命中跨页面(详情页可复用列表页动作),同一 action id 在 flow 内最多一次;
|
|
@@ -400,6 +459,26 @@ export function parseCaseSteps(caseItem, page, { config, extraPages = [], gotoPa
|
|
|
400
459
|
/* 四、预期结果解析 */
|
|
401
460
|
/* ------------------------------------------------------------------ */
|
|
402
461
|
|
|
462
|
+
const ASSIGNMENT_RE = /([一-龥A-Za-z][一-龥A-Za-z0-9_ ]{0,11})\s*[==]\s*([^\s、,,。;;]+)/g;
|
|
463
|
+
|
|
464
|
+
/** 「状态=进行中、执行方式=外部采购」→ ['进行中','外部采购'];只收短且可观察的值。 */
|
|
465
|
+
function assignmentValues(line, { texts = [], headers = [] } = {}) {
|
|
466
|
+
const values = [];
|
|
467
|
+
for (const match of String(line || '').matchAll(ASSIGNMENT_RE)) {
|
|
468
|
+
const value = String(match[2] || '').trim();
|
|
469
|
+
if (value.length < 2 || value.length > 20) continue;
|
|
470
|
+
if (JUDGEMENT_RE.test(value)) continue;
|
|
471
|
+
const observable =
|
|
472
|
+
/^[一-龥]{2,8}$/.test(value) ||
|
|
473
|
+
texts.includes(value) ||
|
|
474
|
+
headers.includes(value) ||
|
|
475
|
+
/^(是|否|无|--)$/.test(value);
|
|
476
|
+
if (!observable) continue;
|
|
477
|
+
if (!values.includes(value)) values.push(value);
|
|
478
|
+
}
|
|
479
|
+
return values;
|
|
480
|
+
}
|
|
481
|
+
|
|
403
482
|
/**
|
|
404
483
|
* 解析 case.expected → 保守 expect steps(continueOnFail + needsReview)。
|
|
405
484
|
* 无法识别的行生成 comment + EXPECT_NEEDS_REVIEW 诊断。
|
|
@@ -441,6 +520,16 @@ export function parseCaseExpectations(caseItem, page) {
|
|
|
441
520
|
continue;
|
|
442
521
|
}
|
|
443
522
|
|
|
523
|
+
// 1b) 「字段=值」抽成多个独立断言,避免把「进行中、执行方式=外部采购」整句当文案
|
|
524
|
+
const assigned = assignmentValues(line, { texts, headers });
|
|
525
|
+
if (assigned.length) {
|
|
526
|
+
for (const value of assigned) {
|
|
527
|
+
steps.push({ action: 'expect', text: value, continueOnFail: true, needsReview: true, desc: line });
|
|
528
|
+
}
|
|
529
|
+
recognized += 1;
|
|
530
|
+
continue;
|
|
531
|
+
}
|
|
532
|
+
|
|
444
533
|
// 2) 判断性描述(展示/正确/一致/保留/顺序/共N列/对齐/颜色…)一律转 comment,避免误报
|
|
445
534
|
if (JUDGEMENT_RE.test(line)) {
|
|
446
535
|
const hint = /表头|列名|列顺序|共\s*\d+\s*列/.test(line)
|
|
@@ -464,6 +553,7 @@ export function parseCaseExpectations(caseItem, page) {
|
|
|
464
553
|
value &&
|
|
465
554
|
value.length >= 2 &&
|
|
466
555
|
value.length <= 20 &&
|
|
556
|
+
!/\s/.test(value) &&
|
|
467
557
|
!JUDGEMENT_RE.test(value) &&
|
|
468
558
|
(OBSERVABLE_RE.test(value) || texts.includes(value) || headers.includes(value))
|
|
469
559
|
) {
|
|
@@ -472,13 +562,9 @@ export function parseCaseExpectations(caseItem, page) {
|
|
|
472
562
|
}
|
|
473
563
|
}
|
|
474
564
|
if (!candidate) {
|
|
565
|
+
// 整行只有当它就是知识库里记录过的文案/表头时才可断言;带动词的叙述句不是页面文本。
|
|
475
566
|
const stripped = line.replace(/^[\d、..))\s]+/, '').trim();
|
|
476
|
-
if (
|
|
477
|
-
stripped.length > 0 &&
|
|
478
|
-
stripped.length <= 20 &&
|
|
479
|
-
!JUDGEMENT_RE.test(stripped) &&
|
|
480
|
-
(OBSERVABLE_RE.test(stripped) || headers.includes(stripped) || texts.includes(stripped))
|
|
481
|
-
) {
|
|
567
|
+
if (stripped && (texts.includes(stripped) || headers.includes(stripped))) {
|
|
482
568
|
candidate = stripped;
|
|
483
569
|
}
|
|
484
570
|
}
|
|
@@ -861,31 +947,17 @@ function actionsAcrossPagesForHistory(pages, text) {
|
|
|
861
947
|
}
|
|
862
948
|
|
|
863
949
|
/** 用 run 历史修正生成步骤:失败 expect 降级、失败 click/fill 优先知识 action 否则降级。 */
|
|
864
|
-
function applyRunHistory({ steps, runs, pages, page, diagnostics, usedActions }) {
|
|
950
|
+
export function applyRunHistory({ steps, runs, pages, page, diagnostics, usedActions }) {
|
|
865
951
|
const failed = runs.flatMap((run) => run.failedSteps.map((step) => ({ ...step, runDir: run.runDir })));
|
|
866
952
|
const next = steps.map((step) => ({ ...step }));
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
(step) =>
|
|
870
|
-
step.action === 'expect' &&
|
|
871
|
-
(step.desc === fail.desc || (fail.message && step.text && fail.message.includes(String(step.text)))),
|
|
872
|
-
);
|
|
873
|
-
if (!match) continue;
|
|
874
|
-
match.action = 'comment';
|
|
875
|
-
match.desc = `待人工确认断言:${match.desc || match.text}(上次断言失败:${truncate(fail.message || '无错误详情', 160)})`;
|
|
876
|
-
match.needsReview = true;
|
|
877
|
-
delete match.text;
|
|
878
|
-
delete match.continueOnFail;
|
|
879
|
-
diagnostics.push({
|
|
880
|
-
level: 'warn',
|
|
881
|
-
code: 'RUN_HISTORY_ASSERTION',
|
|
882
|
-
message: `上次运行断言失败,已降级为 comment:${fail.desc || fail.message}`,
|
|
883
|
-
source: fail.desc || null,
|
|
884
|
-
});
|
|
885
|
-
}
|
|
953
|
+
// 只修正原 flow 的步骤;本函数插入的替换步骤不再被后续失败记录二次替换。
|
|
954
|
+
const originals = new Set(next);
|
|
886
955
|
for (const fail of failed.filter((step) => ['click', 'fill', 'select', 'timeout'].includes(step.action))) {
|
|
887
956
|
const match = next.find(
|
|
888
|
-
(step) =>
|
|
957
|
+
(step) =>
|
|
958
|
+
originals.has(step) &&
|
|
959
|
+
['click', 'fill', 'select'].includes(step.action) &&
|
|
960
|
+
(step.desc === fail.desc || (fail.desc && step.desc && step.desc.includes(fail.desc))),
|
|
889
961
|
);
|
|
890
962
|
if (!match) continue;
|
|
891
963
|
const actionMatch = actionsAcrossPagesForHistory([page, ...pages].filter(Boolean), fail.desc || match.desc || '').find(
|
|
@@ -1152,7 +1224,8 @@ export function convertCase({ knowledge, caseItem, config, options = {} }) {
|
|
|
1152
1224
|
});
|
|
1153
1225
|
}
|
|
1154
1226
|
|
|
1155
|
-
const { best, scoredPages } = findPageForCase(knowledge, caseItem);
|
|
1227
|
+
const { best, scoredPages, ranked } = findPageForCase(knowledge, caseItem);
|
|
1228
|
+
const rankedPages = ranked.length ? ranked : scoredPages;
|
|
1156
1229
|
let page = null;
|
|
1157
1230
|
let pageScore = best?.score ?? 0;
|
|
1158
1231
|
if (!pages.length) {
|
|
@@ -1163,7 +1236,7 @@ export function convertCase({ knowledge, caseItem, config, options = {} }) {
|
|
|
1163
1236
|
candidates: [],
|
|
1164
1237
|
});
|
|
1165
1238
|
} else {
|
|
1166
|
-
const candidates =
|
|
1239
|
+
const candidates = rankedPages.slice(0, 3).map((item) => ({ id: item.page.id, title: item.page.title, score: item.score }));
|
|
1167
1240
|
if (!best || best.score < 3) {
|
|
1168
1241
|
diagnostics.push({
|
|
1169
1242
|
level: 'warn',
|
|
@@ -1173,21 +1246,21 @@ export function convertCase({ knowledge, caseItem, config, options = {} }) {
|
|
|
1173
1246
|
});
|
|
1174
1247
|
} else {
|
|
1175
1248
|
page = best.page;
|
|
1176
|
-
if (
|
|
1177
|
-
const gap =
|
|
1249
|
+
if (rankedPages.length > 1) {
|
|
1250
|
+
const gap = rankedPages[0].score - rankedPages[1].score;
|
|
1178
1251
|
if (gap < 2) {
|
|
1179
|
-
const detailCandidate =
|
|
1252
|
+
const detailCandidate = rankedPages.find(
|
|
1180
1253
|
(item) => /detail|详情/i.test(String(item.page.id || '')) || /详情/.test(String(item.page.title || '')),
|
|
1181
1254
|
);
|
|
1182
1255
|
const caseMentionsDetail = /详情/.test(caseText(caseItem)) && /详情/.test(caseL2L1Text(caseItem));
|
|
1183
|
-
if (detailCandidate && caseMentionsDetail && detailCandidate.score >=
|
|
1256
|
+
if (detailCandidate && caseMentionsDetail && detailCandidate.score >= rankedPages[0].score - 2) {
|
|
1184
1257
|
page = detailCandidate.page;
|
|
1185
1258
|
}
|
|
1186
1259
|
pageScore = scoredPages.find((item) => item.page.id === page.id)?.score ?? best.score;
|
|
1187
1260
|
diagnostics.push({
|
|
1188
1261
|
level: 'info',
|
|
1189
1262
|
code: 'PAGE_AMBIGUOUS',
|
|
1190
|
-
message: `页面分差 < 2(${
|
|
1263
|
+
message: `页面分差 < 2(${rankedPages[0].page.id} ${rankedPages[0].score} / ${rankedPages[1].page.id} ${rankedPages[1].score}),已选择 ${page.id}`,
|
|
1191
1264
|
candidates,
|
|
1192
1265
|
});
|
|
1193
1266
|
}
|
|
@@ -1230,7 +1303,7 @@ export function convertCase({ knowledge, caseItem, config, options = {} }) {
|
|
|
1230
1303
|
const rest = parsedSteps.filter((step) => !isOpeningStep(step));
|
|
1231
1304
|
parsedSteps = [...opening, ...rest];
|
|
1232
1305
|
} else {
|
|
1233
|
-
const opening = findOpeningAction(pagesList);
|
|
1306
|
+
const opening = findDetailOpener(pagesList) || findOpeningAction(pagesList);
|
|
1234
1307
|
if (opening) {
|
|
1235
1308
|
parsedSteps = [
|
|
1236
1309
|
...(opening.action.steps || []).map((step) => ({
|
|
@@ -1268,6 +1341,71 @@ export function convertCase({ knowledge, caseItem, config, options = {} }) {
|
|
|
1268
1341
|
}
|
|
1269
1342
|
}
|
|
1270
1343
|
|
|
1344
|
+
// 弹窗选择器要求详情抽屉先打开;运行历史替换后也要复核,避免替换出的弹窗动作悬空。
|
|
1345
|
+
const dialogIndex = combinedSteps.findIndex(stepTargetsDialog);
|
|
1346
|
+
if (dialogIndex >= 0 && !combinedSteps.slice(0, dialogIndex).some(stepOpensDetail)) {
|
|
1347
|
+
const pagesList = [page, entryPage].filter(Boolean);
|
|
1348
|
+
const opener = findDetailOpener(pagesList);
|
|
1349
|
+
if (opener) {
|
|
1350
|
+
const firstReal = combinedSteps.findIndex((step) => step.action !== 'goto' && step.action !== 'comment');
|
|
1351
|
+
const at = firstReal < 0 ? combinedSteps.length : firstReal;
|
|
1352
|
+
const entrySteps = (opener.action.steps || []).map((step) => ({
|
|
1353
|
+
...structuredClone(step),
|
|
1354
|
+
desc: `打开履约单详情(知识动作 ${opener.action.id})`,
|
|
1355
|
+
}));
|
|
1356
|
+
combinedSteps = [...combinedSteps.slice(0, at), ...entrySteps, ...combinedSteps.slice(at)];
|
|
1357
|
+
if (!usedActions.includes(opener.action.id)) usedActions.push(opener.action.id);
|
|
1358
|
+
diagnostics.push({
|
|
1359
|
+
level: 'info',
|
|
1360
|
+
code: 'STEP_UNMATCHED',
|
|
1361
|
+
message: `弹窗动作缺少入口,已插入知识动作 ${opener.action.id} 打开详情`,
|
|
1362
|
+
source: null,
|
|
1363
|
+
});
|
|
1364
|
+
} else {
|
|
1365
|
+
diagnostics.push({
|
|
1366
|
+
level: 'warn',
|
|
1367
|
+
code: 'STEP_UNMATCHED',
|
|
1368
|
+
message: '弹窗动作缺少「打开详情」入口动作,flow 需人工补充导航',
|
|
1369
|
+
source: null,
|
|
1370
|
+
});
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
// 计划详情是第二层弹窗:中间弹窗的选择器还要求先点计划行「详情」。
|
|
1375
|
+
const middleIndex = combinedSteps.findIndex(
|
|
1376
|
+
(step) => /ui-dialog-wrap-middle/.test(String(step.selector || '')) || /ui-dialog-wrap-middle/.test(String(step.text || '')),
|
|
1377
|
+
);
|
|
1378
|
+
if (middleIndex >= 0) {
|
|
1379
|
+
const beforeMiddle = combinedSteps.slice(0, middleIndex);
|
|
1380
|
+
const planOpened = beforeMiddle.some(
|
|
1381
|
+
(step) => step.action === 'click' && /ui-dialog-wrap-right/.test(String(step.selector || '')),
|
|
1382
|
+
);
|
|
1383
|
+
if (!planOpened) {
|
|
1384
|
+
const opener = findPlanModalOpener([page, entryPage].filter(Boolean));
|
|
1385
|
+
if (opener) {
|
|
1386
|
+
const entrySteps = (opener.action.steps || []).map((step) => ({
|
|
1387
|
+
...structuredClone(step),
|
|
1388
|
+
desc: `打开履约计划详情(知识动作 ${opener.action.id})`,
|
|
1389
|
+
}));
|
|
1390
|
+
combinedSteps = [...combinedSteps.slice(0, middleIndex), ...entrySteps, ...combinedSteps.slice(middleIndex)];
|
|
1391
|
+
if (!usedActions.includes(opener.action.id)) usedActions.push(opener.action.id);
|
|
1392
|
+
diagnostics.push({
|
|
1393
|
+
level: 'info',
|
|
1394
|
+
code: 'STEP_UNMATCHED',
|
|
1395
|
+
message: `计划弹窗动作缺少入口,已插入知识动作 ${opener.action.id} 打开计划详情`,
|
|
1396
|
+
source: null,
|
|
1397
|
+
});
|
|
1398
|
+
} else {
|
|
1399
|
+
diagnostics.push({
|
|
1400
|
+
level: 'warn',
|
|
1401
|
+
code: 'STEP_UNMATCHED',
|
|
1402
|
+
message: '计划弹窗动作缺少「打开计划详情」入口动作,flow 需人工补充导航',
|
|
1403
|
+
source: null,
|
|
1404
|
+
});
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1271
1409
|
let feedbacks = existing && regen ? feedbackForCase(config, tcId, { flow: existing.name, limit: 20 }) : [];
|
|
1272
1410
|
let feedbackSource = feedbacks.length ? 'feedback' : null;
|
|
1273
1411
|
if (existing && regen && !feedbacks.length && page?.feedbacks?.length) {
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
// 从设计文档抽取系统知识结构:单据字段、状态、流转、角色、权限与术语。
|
|
2
|
+
// 只做保守的显式结构识别(章节、表格、定义行),不做语义猜测。
|
|
3
|
+
import { classifyDocumentKind, isLearnableDocument, normalizeKey } from './knowledge-clean.mjs';
|
|
4
|
+
|
|
5
|
+
export { classifyDocumentKind, isLearnableDocument };
|
|
6
|
+
|
|
7
|
+
const ARROW_RE = /\s*(?:→|⇒|=>|->|-->|—>|➡)\s*/;
|
|
8
|
+
const SECTION_MODES = [
|
|
9
|
+
{ mode: 'glossary', re: /术语|名词|字典|枚举/ },
|
|
10
|
+
{ mode: 'status', re: /状态|流转|生命周期|状态机/ },
|
|
11
|
+
{ mode: 'permission', re: /权限/ },
|
|
12
|
+
{ mode: 'role', re: /角色|岗位/ },
|
|
13
|
+
{ mode: 'field', re: /字段|表单|列表列|表头|列名|信息项/ },
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
function cleanItem(value) {
|
|
17
|
+
return String(value || '')
|
|
18
|
+
.replace(/^[-*•]\s+/, '')
|
|
19
|
+
.replace(/^\d+[.、))]\s*/, '')
|
|
20
|
+
.replace(/[*`]/g, '')
|
|
21
|
+
.replace(/\s+/g, ' ')
|
|
22
|
+
.trim();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function cleanDefinition(value) {
|
|
26
|
+
return cleanItem(value)
|
|
27
|
+
.replace(/[。;;,,]\s*$/, '')
|
|
28
|
+
.slice(0, 240)
|
|
29
|
+
.trim();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function cleanName(value, { max = 24 } = {}) {
|
|
33
|
+
const name = cleanItem(value)
|
|
34
|
+
.replace(/^状态(?:为|是)?[::]?\s*/, '')
|
|
35
|
+
.replace(/^字段[::]?\s*/, '')
|
|
36
|
+
.replace(/^角色[::]?\s*/, '')
|
|
37
|
+
.replace(/[::]\s*$/, '')
|
|
38
|
+
.trim();
|
|
39
|
+
if (!name || name.length > max) return '';
|
|
40
|
+
if (/[。;;,,()()]/.test(name)) return '';
|
|
41
|
+
if (/^(?:该|本|此|这个|当前|其中|如果|若|当|所有|全部)$/.test(name)) return '';
|
|
42
|
+
return name;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function splitList(value) {
|
|
46
|
+
return String(value || '')
|
|
47
|
+
.split(/[,,、/|;;]/)
|
|
48
|
+
.map((item) => cleanName(item))
|
|
49
|
+
.filter(Boolean);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function sectionMode(section) {
|
|
53
|
+
return SECTION_MODES.find((item) => item.re.test(String(section || '')))?.mode || '';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function headerMode(cells = []) {
|
|
57
|
+
const text = cells.join(' ');
|
|
58
|
+
if (/术语|名词/.test(text)) return 'glossary';
|
|
59
|
+
if (/状态/.test(text)) return 'status';
|
|
60
|
+
if (/角色|岗位/.test(text)) return 'role';
|
|
61
|
+
if (/权限/.test(text)) return 'permission';
|
|
62
|
+
if (/字段|列名|名称/.test(text) && /说明|类型|必填|含义/.test(text)) return 'field';
|
|
63
|
+
return '';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function columnIndex(cells, pattern, fallback) {
|
|
67
|
+
const index = cells.findIndex((cell) => pattern.test(String(cell || '')));
|
|
68
|
+
return index >= 0 ? index : fallback;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function pushUnique(list, entry, keyOf) {
|
|
72
|
+
const key = keyOf(entry);
|
|
73
|
+
if (!key) return;
|
|
74
|
+
if (list.some((item) => keyOf(item) === key)) return;
|
|
75
|
+
list.push(entry);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 抽取文档结构。返回的列表都带来源文档标题,供合并与追溯。
|
|
80
|
+
*/
|
|
81
|
+
export function extractDocumentStructure(content, { title = '' } = {}) {
|
|
82
|
+
const text = String(content || '');
|
|
83
|
+
const lines = text.split(/\r?\n/);
|
|
84
|
+
const fields = [];
|
|
85
|
+
const statuses = [];
|
|
86
|
+
const transitions = [];
|
|
87
|
+
const roles = [];
|
|
88
|
+
const permissions = [];
|
|
89
|
+
const glossary = [];
|
|
90
|
+
let section = '';
|
|
91
|
+
let mode = '';
|
|
92
|
+
let tableHeader = null;
|
|
93
|
+
let tableMode = '';
|
|
94
|
+
let inCodeFence = false;
|
|
95
|
+
|
|
96
|
+
const addStatus = (name, meaning = '') => {
|
|
97
|
+
pushUnique(statuses, { name, meaning }, (item) => normalizeKey(item.name));
|
|
98
|
+
};
|
|
99
|
+
const addField = (name, extra = {}) => {
|
|
100
|
+
pushUnique(fields, { name, ...extra }, (item) => normalizeKey(item.name));
|
|
101
|
+
};
|
|
102
|
+
const addRole = (name, note = '') => {
|
|
103
|
+
pushUnique(roles, { name, note }, (item) => normalizeKey(item.name));
|
|
104
|
+
};
|
|
105
|
+
const addGlossary = (term, definition) => {
|
|
106
|
+
if (!term || !definition || term.length > 24) return;
|
|
107
|
+
pushUnique(glossary, { term, definition }, (item) => normalizeKey(item.term));
|
|
108
|
+
};
|
|
109
|
+
const addTransition = (from, to, trigger = '') => {
|
|
110
|
+
if (!from || !to || from === to) return;
|
|
111
|
+
pushUnique(transitions, { from, to, trigger }, (item) => normalizeKey(`${item.from}>${item.to}>${item.trigger}`));
|
|
112
|
+
addStatus(from);
|
|
113
|
+
addStatus(to);
|
|
114
|
+
};
|
|
115
|
+
const addPermission = (raw) => {
|
|
116
|
+
const line = cleanItem(raw);
|
|
117
|
+
if (line.length < 4) return;
|
|
118
|
+
const match = /^(.{1,30}?)(不允许|不能|不可|可以|可|允许|能)(.{2,60})$/.exec(line);
|
|
119
|
+
if (!match) {
|
|
120
|
+
pushUnique(permissions, { role: '', action: cleanDefinition(line), allowed: null }, (item) => normalizeKey(`${item.role}|${item.action}`));
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const role = cleanName(match[1].replace(/^具有|^拥有/, '').replace(/的$/, '').replace(/角色$/, ''), { max: 20 });
|
|
124
|
+
const allowed = !/^不/.test(match[2]);
|
|
125
|
+
const action = cleanDefinition(match[3]);
|
|
126
|
+
pushUnique(permissions, { role, action, allowed }, (item) => normalizeKey(`${item.role}|${item.action}|${item.allowed}`));
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const consumeValue = (label, value) => {
|
|
130
|
+
const key = String(label || '').trim();
|
|
131
|
+
const items = splitList(value);
|
|
132
|
+
if (/状态/.test(key)) items.forEach((name) => addStatus(name));
|
|
133
|
+
else if (/字段|列名|表头/.test(key)) items.forEach((name) => addField(name));
|
|
134
|
+
else if (/角色|岗位/.test(key)) items.forEach((name) => addRole(name));
|
|
135
|
+
else if (/术语/.test(key)) {
|
|
136
|
+
const pair = /^(.{1,24})[::]\s*(.+)$/.exec(cleanItem(value));
|
|
137
|
+
if (pair) addGlossary(cleanName(pair[1]), cleanDefinition(pair[2]));
|
|
138
|
+
else return false;
|
|
139
|
+
}
|
|
140
|
+
return items.length > 0 || /术语/.test(key);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
for (const rawLine of lines) {
|
|
144
|
+
const line = rawLine.trim();
|
|
145
|
+
if (/^```/.test(line)) {
|
|
146
|
+
inCodeFence = !inCodeFence;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (inCodeFence || !line) {
|
|
150
|
+
if (!line) tableHeader = null;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const heading = /^#{1,6}\s+(.+)$/.exec(line) || /^\*\*([^*]+)\*\*\s*$/.exec(line);
|
|
155
|
+
if (heading) {
|
|
156
|
+
section = heading[1].trim();
|
|
157
|
+
mode = sectionMode(section);
|
|
158
|
+
tableHeader = null;
|
|
159
|
+
tableMode = '';
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (/^\|/.test(line)) {
|
|
164
|
+
if (/^[\s|:.-]+$/.test(line.replace(/\|/g, ''))) continue;
|
|
165
|
+
const cells = line.split('|').slice(1, -1).map((cell) => cleanItem(cell));
|
|
166
|
+
if (!tableHeader) {
|
|
167
|
+
tableHeader = cells;
|
|
168
|
+
tableMode = headerMode(cells);
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
const active = tableMode || mode;
|
|
172
|
+
if (!active || cells.length < 2) continue;
|
|
173
|
+
if (active === 'glossary') {
|
|
174
|
+
addGlossary(cells[0], cleanDefinition(cells[1]));
|
|
175
|
+
} else if (active === 'status') {
|
|
176
|
+
addStatus(cleanName(cells[0]), cleanDefinition(cells[1]));
|
|
177
|
+
} else if (active === 'role') {
|
|
178
|
+
addRole(cleanName(cells[0]), cleanDefinition(cells[1]));
|
|
179
|
+
} else if (active === 'permission') {
|
|
180
|
+
addPermission(cells.join(' '));
|
|
181
|
+
} else if (active === 'field') {
|
|
182
|
+
const nameIndex = columnIndex(tableHeader, /字段|列名|名称/, 0);
|
|
183
|
+
const typeIndex = columnIndex(tableHeader, /类型/, -1);
|
|
184
|
+
const requiredIndex = columnIndex(tableHeader, /必填|必录/, -1);
|
|
185
|
+
const noteIndex = columnIndex(tableHeader, /说明|含义|备注/, -1);
|
|
186
|
+
addField(cleanName(cells[nameIndex], { max: 20 }), {
|
|
187
|
+
...(typeIndex >= 0 && cells[typeIndex] ? { type: cells[typeIndex] } : {}),
|
|
188
|
+
...(requiredIndex >= 0 && /是|必填|必录|Y|✔/.test(cells[requiredIndex]) ? { required: true } : {}),
|
|
189
|
+
...(noteIndex >= 0 && cells[noteIndex] ? { note: cleanDefinition(cells[noteIndex]) } : {}),
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// 箭头流转:待处理 → 进行中(下发)→ 已完成
|
|
196
|
+
if (ARROW_RE.test(line) && /状态|流转|→|⇒|=>|->|—>/.test(`${section} ${line}`)) {
|
|
197
|
+
const parts = line
|
|
198
|
+
.split(ARROW_RE)
|
|
199
|
+
.map((part) => cleanItem(part))
|
|
200
|
+
.filter(Boolean);
|
|
201
|
+
if (parts.length >= 2) {
|
|
202
|
+
const parsePart = (part) => {
|
|
203
|
+
const paren = /^(.+?)[((]([^))]*)[))]?$/.exec(part);
|
|
204
|
+
return paren ? { name: cleanName(paren[1], { max: 16 }), trigger: cleanDefinition(paren[2]) } : { name: cleanName(part, { max: 16 }), trigger: '' };
|
|
205
|
+
};
|
|
206
|
+
const parsed = parts.map(parsePart);
|
|
207
|
+
for (let index = 0; index < parsed.length - 1; index += 1) {
|
|
208
|
+
addTransition(parsed[index].name, parsed[index + 1].name, parsed[index + 1].trigger);
|
|
209
|
+
}
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// 显式标签行:状态:A、B;字段:A、B;角色:A、B;术语:定义
|
|
215
|
+
const labelled = /^(状态|字段|列名|表头|角色|岗位|术语)\s*[::]\s*(.+)$/.exec(line);
|
|
216
|
+
if (labelled && consumeValue(labelled[1], labelled[2])) continue;
|
|
217
|
+
|
|
218
|
+
// 章节内的定义行:待处理:新建未下发;报价单号:系统自动生成
|
|
219
|
+
const definition = /^([^::]{1,24})[::]\s*(.{2,240})$/.exec(cleanItem(line));
|
|
220
|
+
if (definition) {
|
|
221
|
+
const name = cleanName(definition[1]);
|
|
222
|
+
const meaning = cleanDefinition(definition[2]);
|
|
223
|
+
if (mode === 'glossary') addGlossary(name, meaning);
|
|
224
|
+
else if (mode === 'status') addStatus(name, meaning);
|
|
225
|
+
else if (mode === 'field') addField(name, { note: meaning });
|
|
226
|
+
else if (mode === 'role') addRole(name, meaning);
|
|
227
|
+
else if (mode === 'permission') addPermission(line);
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (mode === 'permission' || /权限/.test(section)) {
|
|
232
|
+
if (/(?:可以|可|允许|能|不允许|不能|不可)/.test(line)) addPermission(line);
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
if (mode === 'role') {
|
|
236
|
+
const role = cleanName(line);
|
|
237
|
+
if (role && line.length <= 30) addRole(role);
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
if (mode === 'glossary') {
|
|
241
|
+
const clause = /^(.{1,24}?)[::]\s*(.+)$/.exec(line);
|
|
242
|
+
if (clause) addGlossary(cleanName(clause[1]), cleanDefinition(clause[2]));
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (mode === 'status' || mode === 'field') {
|
|
246
|
+
const items = splitList(line);
|
|
247
|
+
if (items.length >= 2) {
|
|
248
|
+
if (mode === 'status') items.forEach((name) => addStatus(name));
|
|
249
|
+
else items.forEach((name) => addField(name));
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const diagnostics = [];
|
|
255
|
+
if (!fields.length && !statuses.length && !roles.length && !glossary.length) {
|
|
256
|
+
diagnostics.push({ level: 'info', code: 'DOC_STRUCTURE_EMPTY', message: `文档「${title || '未命名'}」未识别到字段/状态/角色/术语结构` });
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return {
|
|
260
|
+
kind: classifyDocumentKind({ title, content: text }),
|
|
261
|
+
fields,
|
|
262
|
+
statuses,
|
|
263
|
+
transitions,
|
|
264
|
+
roles,
|
|
265
|
+
permissions,
|
|
266
|
+
glossary,
|
|
267
|
+
diagnostics,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** 合并两份结构(按名称/内容去重,补充非空属性)。 */
|
|
272
|
+
export function mergeDocumentStructure(existing = {}, patch = {}) {
|
|
273
|
+
const mergeList = (left = [], right = [], keyOf, mergeItem) => {
|
|
274
|
+
const out = [...left];
|
|
275
|
+
for (const item of right) {
|
|
276
|
+
const key = keyOf(item);
|
|
277
|
+
if (!key) continue;
|
|
278
|
+
const index = out.findIndex((candidate) => keyOf(candidate) === key);
|
|
279
|
+
if (index < 0) out.push(item);
|
|
280
|
+
else out[index] = mergeItem ? mergeItem(out[index], item) : { ...out[index], ...item };
|
|
281
|
+
}
|
|
282
|
+
return out;
|
|
283
|
+
};
|
|
284
|
+
return {
|
|
285
|
+
fields: mergeList(existing.fields, patch.fields, (item) => normalizeKey(item.name), (a, b) => ({
|
|
286
|
+
...b,
|
|
287
|
+
...a,
|
|
288
|
+
...(a.name ? {} : { name: b.name }),
|
|
289
|
+
...(a.note ? {} : b.note ? { note: b.note } : {}),
|
|
290
|
+
...(a.type ? {} : b.type ? { type: b.type } : {}),
|
|
291
|
+
required: Boolean(a.required || b.required),
|
|
292
|
+
})),
|
|
293
|
+
statuses: mergeList(existing.statuses, patch.statuses, (item) => normalizeKey(item.name), (a, b) => ({
|
|
294
|
+
...a,
|
|
295
|
+
meaning: a.meaning || b.meaning || '',
|
|
296
|
+
})),
|
|
297
|
+
transitions: mergeList(
|
|
298
|
+
existing.transitions,
|
|
299
|
+
patch.transitions,
|
|
300
|
+
(item) => normalizeKey(`${item.from}>${item.to}>${item.trigger}`),
|
|
301
|
+
),
|
|
302
|
+
roles: mergeList(existing.roles, patch.roles, (item) => normalizeKey(item.name), (a, b) => ({
|
|
303
|
+
...a,
|
|
304
|
+
note: a.note || b.note || '',
|
|
305
|
+
})),
|
|
306
|
+
permissions: mergeList(
|
|
307
|
+
existing.permissions,
|
|
308
|
+
patch.permissions,
|
|
309
|
+
(item) => normalizeKey(`${item.role}|${item.action}|${item.allowed}`),
|
|
310
|
+
),
|
|
311
|
+
glossary: mergeList(existing.glossary, patch.glossary, (item) => normalizeKey(item.term), (a, b) => ({
|
|
312
|
+
...a,
|
|
313
|
+
definition: a.definition || b.definition || '',
|
|
314
|
+
})),
|
|
315
|
+
};
|
|
316
|
+
}
|