lantern-harness 0.1.44 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +224 -0
- package/README.md +25 -8
- package/builtin/session/lh-fs.mjs +93 -4
- package/builtin/session/lh-nav-groups.mjs +3 -0
- package/builtin/session/mcp-client.mjs +7 -3
- package/builtin/session/session-runs.mjs +5 -1
- package/builtin/session/skill-validation.mjs +72 -0
- package/builtin/session/space-skills.mjs +751 -0
- package/cli/data-root.mjs +21 -4
- package/cli/lantern.mjs +26 -1
- package/cli/mcp-server.mjs +2779 -0
- package/cli/server.mjs +8 -2
- package/config/app.defaults.json +4 -2
- package/dist/500.html +9 -6
- package/dist/_astro/Base.Du_-4L-4.css +1 -0
- package/dist/_astro/Base.astro_astro_type_script_index_0_lang.XUV2H6ea.js +2 -0
- package/dist/_astro/BusinessMapBoard.mRxl2G3D.js +5 -0
- package/dist/_astro/CaseExecBoard.Cqkxuf5c.js +1 -0
- package/dist/_astro/CaseExecutionBoard.CjX4CoKd.js +5 -0
- package/dist/_astro/CaseGenerateApp.acgnxl79.js +10 -0
- package/dist/_astro/CasesLibrary.zmzOQwdn.js +21 -0
- package/dist/_astro/ChatApp.Ben9LFra.js +1 -0
- package/dist/_astro/ChatApp.DBi6q7Qv.js +262 -0
- package/dist/_astro/{ConfirmDialog.DrrPf1MB.js → ConfirmDialog.DQFd6g2r.js} +1 -1
- package/dist/_astro/DashboardBoard.CAJVE2G4.js +5 -0
- package/dist/_astro/{DatePicker.Bkl71M49.js → DatePicker.BRO_CjVQ.js} +1 -1
- package/dist/_astro/DefectSubmitEntry.BzKXJ-zq.js +2 -0
- package/dist/_astro/DocsFileBrowser.apKMbGVf.js +1 -0
- package/dist/_astro/{DocsPager.Duf4I68E.js → DocsPager.DMN9-Tjg.js} +1 -1
- package/dist/_astro/{DrawerField.BH3p4Q4D.js → DrawerField.CLdvmHc6.js} +1 -1
- package/dist/_astro/KnowledgeLibrary.BcoMQeUd.js +1 -0
- package/dist/_astro/LifeKrTasksBoard.DJU0-QI-.js +1 -0
- package/dist/_astro/LifeKrTodosBoard.GK7KRAO3.js +1 -0
- package/dist/_astro/LifeObjectivesBoard.DuKa6Q3k.js +1 -0
- package/dist/_astro/LifePlanTypeSelect.Cm2lUmnH.js +1 -0
- package/dist/_astro/{LifeReviewsBoard.BPXeVpg1.js → LifeReviewsBoard.BWOOVMtz.js} +3 -3
- package/dist/_astro/{LifeShared.tnT7aQON.js → LifeShared.bY2l01Ha.js} +2 -2
- package/dist/_astro/LifeStagesBoard.CIzxPOJB.js +1 -0
- package/dist/_astro/LifeYearCycleSelect.BlsyPmmI.js +1 -0
- package/dist/_astro/LifeYearPlansBoard.DaA9O08j.js +1 -0
- package/dist/_astro/MultiSelectDropdown.D-rbpivp.js +1 -0
- package/dist/_astro/{NamePromptDialog.Csfnh5eM.js → NamePromptDialog.KPCeXmri.js} +1 -1
- package/dist/_astro/NavLibraries.DVR5Ua-S.js +1 -0
- package/dist/_astro/PluginsPage.B063QIlQ.js +18 -0
- package/dist/_astro/PmBugsBoard.Cu-v0Gfd.js +1 -0
- package/dist/_astro/PmIterationsBoard.CgRzkP-j.js +1 -0
- package/dist/_astro/{PmProjectDetail.DneFVBpy.js → PmProjectDetail.DVCEh0Lm.js} +1 -1
- package/dist/_astro/PmProjectsBoard.z-bQa98r.js +1 -0
- package/dist/_astro/PmRequirementsBoard.B0W6bsW2.js +1 -0
- package/dist/_astro/PmRequirementsBoard.CyfBI0u_.js +1 -0
- package/dist/_astro/PmShared.BQVzwsiz.js +1 -0
- package/dist/_astro/ReportsPage.najSzJeT.js +1 -0
- package/dist/_astro/ReqAnalysisBoard.BQb8dHFD.js +39 -0
- package/dist/_astro/RequirementBreakdownLibrary.Cn6OV6xt.js +2 -0
- package/dist/_astro/RequirementsLibrary.CqKNFVsP.js +1 -0
- package/dist/_astro/SearchableSelect.DXclLMJI.js +1 -0
- package/dist/_astro/SelectDropdown.D7IeyHDI.js +1 -0
- package/dist/_astro/SettingsDialog.db3BpFw0.js +1 -0
- package/dist/_astro/SlideOverDrawer.BMg2tce6.js +1 -0
- package/dist/_astro/SpaceSwitcher.DSUVAPOi.js +7 -0
- package/dist/_astro/{TableFieldConfig.C7O-mL-p.js → TableFieldConfig.72AL-8p-.js} +1 -1
- package/dist/_astro/{TagSelect.UUXgkMqJ.js → TagSelect.B_r2p0fp.js} +1 -1
- package/dist/_astro/TasksBoard.CCDhCSR7.js +1 -0
- package/dist/_astro/TemplatesLibrary.peYzrHZ2.js +1 -0
- package/dist/_astro/TestReportsLibrary.fWiEyCRx.js +1 -0
- package/dist/_astro/TestStatsBoard.BSUBsWSu.js +1 -0
- package/dist/_astro/Toast.BydY-X7T.js +1 -0
- package/dist/_astro/TodosBoard.By5YdlUe.js +1 -0
- package/dist/_astro/UiAutoBoard.BGdlp5Sr.js +3 -0
- package/dist/_astro/UiAutoEnvPanel.BUASgPLB.js +10 -0
- package/dist/_astro/UiAutoGenerationDialog.D1PyCgrD.js +12 -0
- package/dist/_astro/UiAutoProjectsPage.B6A9NlVK.js +3 -0
- package/dist/_astro/app-config.Dvfzl5_N.js +1 -0
- package/dist/_astro/app-version.CNpnKIb9.js +1 -0
- package/dist/_astro/{bash-tools.D9HCzC_7.js → bash-tools.CS2w8LOb.js} +1 -1
- package/dist/_astro/business-map.BiJ85oxm.js +1 -0
- package/dist/_astro/business-map.DKwdW1VL.css +1 -0
- package/dist/_astro/case-gen-run.Cvvdv_kv.js +26 -0
- package/dist/_astro/case-prompt.CZ3vWUju.js +2 -0
- package/dist/_astro/{case-review.TdDs1WFM.js → case-review.DFWZkoR9.js} +1 -1
- package/dist/_astro/{case-template.Cd2xNbgy.js → case-template.Clb7HrDB.js} +1 -1
- package/dist/_astro/defect-submit.Dc49lFjm.css +1 -0
- package/dist/_astro/defect-workbench.CjTb4FDv.js +6 -0
- package/dist/_astro/dialog.XgRGNF_Q.js +1 -0
- package/dist/_astro/doc-roots.C-20VD0x.js +1 -0
- package/dist/_astro/{hydrate-cache.B15Nrpq0.js → hydrate-cache.O4CIma9y.js} +1 -1
- package/dist/_astro/i18n.Cp4so7KR.js +7 -0
- package/dist/_astro/{llm.DN5S1KBK.js → llm.BCYNgppW.js} +1 -1
- package/dist/_astro/mcp-tools.Kf-lupGn.js +7 -0
- package/dist/_astro/{open-docs-in-session.CRiZT022.js → open-docs-in-session.BpcAcBSI.js} +1 -1
- package/dist/_astro/plugins.Db9xoFZb.js +1 -0
- package/dist/_astro/project-docs.bB9lQ1Ki.js +1 -0
- package/dist/_astro/prompts-meta.VAjE4OET.js +2 -0
- package/dist/_astro/report-prompt.CwHuOcRN.js +1 -0
- package/dist/_astro/req-analysis.C_gARMrB.js +31 -0
- package/dist/_astro/requirement-breakdown-library.l0rPd7b3.js +1 -0
- package/dist/_astro/{settings.38J2uD5M.js → settings.D02wZj3w.js} +2 -2
- package/dist/_astro/space-skills.CPucW8zt.js +1 -0
- package/dist/_astro/{spaces-client.DV04tRyD.js → spaces-client.BAtG6PkW.js} +1 -1
- package/dist/_astro/story-review.DTN91Vl9.js +7 -0
- package/dist/_astro/test-points.C7ZgggQT.js +8 -0
- package/dist/_astro/test-reports.0gr6-c_0.js +19 -0
- package/dist/_astro/ui-auto-prompt.BmO3ja2m.js +8 -0
- package/dist/_astro/{view-prefs.DhdLZjGh.js → view-prefs.COAKEQtZ.js} +1 -1
- package/dist/business-map/index.html +983 -0
- package/dist/case-exec/index.html +10 -7
- package/dist/{prompts → case-execution}/index.html +12 -9
- package/dist/cases/generate/index.html +9 -6
- package/dist/cases/library/index.html +10 -7
- package/dist/dashboard/index.html +10 -7
- package/dist/defect-rules/index.html +690 -0
- package/dist/defect-submit/index.html +690 -0
- package/dist/defect-submit/templates/index.html +690 -0
- package/dist/docs/changelog/index.html +265 -51
- package/dist/docs/install/index.html +44 -12
- package/dist/docs/manual/index.html +213 -75
- package/dist/knowledge/index.html +10 -7
- package/dist/life/kr-tasks/index.html +10 -7
- package/dist/life/kr-todos/index.html +10 -7
- package/dist/life/objectives/index.html +10 -7
- package/dist/life/reviews/index.html +10 -7
- package/dist/life/stages/index.html +10 -7
- package/dist/life/year-plans/index.html +10 -7
- package/dist/plugins/index.html +10 -7
- package/dist/pm/bugs/index.html +10 -7
- package/dist/pm/iterations/index.html +10 -7
- package/dist/pm/projects/detail/index.html +10 -7
- package/dist/pm/projects/index.html +10 -7
- package/dist/pm/requirements/index.html +10 -7
- package/dist/reports/index.html +10 -7
- package/dist/requirement-analysis/index.html +10 -7
- package/dist/requirement-breakdowns/index.html +983 -0
- package/dist/requirements/index.html +10 -7
- package/dist/tasks/index.html +10 -7
- package/dist/templates/index.html +10 -7
- package/dist/test-automation/index.html +10 -7
- package/dist/test-automation/projects/index.html +10 -7
- package/dist/test-reports/index.html +10 -7
- package/dist/test-stats/index.html +10 -7
- package/dist/todos/index.html +10 -7
- package/dist/workbench/index.html +9 -6
- package/docs/Codex-MCP/351/233/206/346/210/220.md +296 -0
- package/docs//344/270/232/345/212/241/345/234/260/345/233/276/350/257/204/344/274/260.md +39 -0
- package/docs//344/270/232/345/212/241/345/234/260/345/233/276/350/257/206/345/210/253/351/207/215/346/236/204/346/226/271/346/241/210.md +88 -0
- package/docs//344/274/232/350/257/235/346/217/222/344/273/266.md +22 -3
- package/docs//345/205/250/345/261/200/346/217/222/344/273/266.md +41 -1
- package/docs//345/256/211/350/243/205/346/211/213/345/206/214.md +63 -17
- package/docs//346/217/222/344/273/266/344/273/223/345/272/223/344/270/216/346/212/200/350/203/275/346/225/264/345/220/210/346/226/271/346/241/210.md +187 -0
- package/docs//346/223/215/344/275/234/346/211/213/345/206/214.md +208 -69
- package/lh-plugins/business-map/assistant.mjs +958 -0
- package/lh-plugins/business-map/auth-paths.mjs +61 -0
- package/lh-plugins/business-map/jobs.mjs +749 -0
- package/lh-plugins/business-map/login.mjs +295 -0
- package/lh-plugins/business-map/sources.mjs +1014 -0
- package/lh-plugins/case-execution/files.mjs +777 -0
- package/lh-plugins/case-execution/service.mjs +959 -0
- package/lh-plugins/case-execution-prompt.mjs +33 -0
- package/lh-plugins/lh-business-map.mjs +2431 -0
- package/lh-plugins/lh-case-execution.mjs +304 -0
- package/lh-plugins/lh-config.mjs +13 -0
- package/lh-plugins/lh-defect-submit.mjs +989 -0
- package/lh-plugins/lh-project-docs.mjs +31 -140
- package/lh-plugins/lh-reports.mjs +29 -57
- package/lh-plugins/lh-req-analysis.mjs +32 -3
- package/lh-plugins/lh-req-breakdown-library.mjs +1084 -0
- package/lh-plugins/lh-uiauto.mjs +514 -22
- package/lh-plugins/plugin-prompts.mjs +19 -15
- package/lh-plugins/req-analysis-revision.mjs +24 -0
- package/lh-plugins/test-points-prompt.mjs +2 -3
- package/lh-plugins/uiauto-auth-setup.mjs +218 -0
- package/lh-plugins/uiauto-explorer/apps.mjs +13 -2
- package/lh-plugins/uiauto-explorer/runner.mjs +15 -2
- package/lh-plugins/uiauto-explorer/sessions.mjs +305 -17
- package/lh-plugins/uiauto-node.mjs +102 -11
- package/lh-plugins/uiauto-prompt.mjs +20 -15
- package/lh-plugins/uiauto-sso.mjs +407 -0
- package/package.json +5 -2
- package/skills/case-execution/SKILL.md +30 -0
- package/skills/lantern-defect-submit/SKILL.md +15 -0
- package/skills/lantern-workflow/SKILL.md +57 -0
- package/skills/office/SKILL.md +26 -0
- package/dist/_astro/Base.DIbzr8uT.css +0 -1
- package/dist/_astro/Base.astro_astro_type_script_index_0_lang.CPR_CfsE.js +0 -2
- package/dist/_astro/CaseExecBoard.DICluCbK.js +0 -1
- package/dist/_astro/CaseGenerateApp.CcAtVSQJ.js +0 -10
- package/dist/_astro/CasesLibrary.Ci_0hYqL.js +0 -21
- package/dist/_astro/ChatApp.DFKZOkZy.js +0 -227
- package/dist/_astro/DashboardBoard.Qvf8o4v1.js +0 -5
- package/dist/_astro/DocsFileBrowser.DwKBffbM.js +0 -1
- package/dist/_astro/KnowledgeLibrary.MDpMqeOd.js +0 -1
- package/dist/_astro/LifeKrTasksBoard.DsvW_S66.js +0 -1
- package/dist/_astro/LifeKrTodosBoard.D2KTqvg1.js +0 -1
- package/dist/_astro/LifeObjectivesBoard.B0cDK934.js +0 -1
- package/dist/_astro/LifePlanTypeSelect.BELDvMWU.js +0 -1
- package/dist/_astro/LifeStagesBoard.CZftLR9K.js +0 -1
- package/dist/_astro/LifeYearCycleSelect.Bw2eRbSm.js +0 -1
- package/dist/_astro/LifeYearPlansBoard.DB-RJFEl.js +0 -1
- package/dist/_astro/MultiSelectDropdown.CBC7aNOv.js +0 -1
- package/dist/_astro/NavLibraries.DdQYsZbe.js +0 -1
- package/dist/_astro/OverflowHint.CHZeFfUj.js +0 -1
- package/dist/_astro/PluginsPage.BVrMtkLi.js +0 -17
- package/dist/_astro/PluginsPage.xdbDGigE.js +0 -1
- package/dist/_astro/PmBugsBoard.CGpXUTPu.js +0 -1
- package/dist/_astro/PmIterationsBoard.BaKiFIam.js +0 -1
- package/dist/_astro/PmProjectsBoard.BxoMF9gK.js +0 -1
- package/dist/_astro/PmRequirementsBoard.-SI8VZXI.js +0 -1
- package/dist/_astro/PmRequirementsBoard.DsK6RF7Q.js +0 -1
- package/dist/_astro/PmShared.BK8kWRSV.js +0 -1
- package/dist/_astro/PromptMountDialog.mrTx3U5T.js +0 -1
- package/dist/_astro/PromptsLibrary.B2LMGsrj.js +0 -1
- package/dist/_astro/ReportsPage.B9lGg1_s.js +0 -16
- package/dist/_astro/ReqAnalysisBoard.D-KmRwQM.js +0 -39
- package/dist/_astro/RequirementsLibrary.cJc-bnGQ.js +0 -1
- package/dist/_astro/SearchableSelect.SyLRS-Jj.js +0 -1
- package/dist/_astro/SelectDropdown.T3CKHaLt.js +0 -1
- package/dist/_astro/SettingsDialog.C62iJkMt.js +0 -1
- package/dist/_astro/SpaceSwitcher.C47_S1g9.js +0 -7
- package/dist/_astro/TasksBoard.BUEg2BYk.js +0 -1
- package/dist/_astro/TemplatesLibrary.BLVIphXO.js +0 -1
- package/dist/_astro/TestReportsLibrary.DVq30Jae.js +0 -1
- package/dist/_astro/TestStatsBoard.-5BWQ6bI.js +0 -1
- package/dist/_astro/Toast.BO2NFckG.js +0 -1
- package/dist/_astro/TodosBoard.DI_lTp2e.js +0 -1
- package/dist/_astro/UiAutoBoard.ChFdgv7s.js +0 -2
- package/dist/_astro/UiAutoEnvPanel.B50q4kYb.js +0 -19
- package/dist/_astro/UiAutoGenerationDialog.CLkFAHrm.js +0 -1
- package/dist/_astro/UiAutoProjectsPage.DCsDeN5d.js +0 -3
- package/dist/_astro/app-config.DkyjuL-S.js +0 -1
- package/dist/_astro/app-version.3eY3FSw1.js +0 -1
- package/dist/_astro/case-gen-run.BQeK-XUw.js +0 -22
- package/dist/_astro/case-types.DlFkbZJN.js +0 -1
- package/dist/_astro/dialog.DZHZCe_T.js +0 -1
- package/dist/_astro/doc-roots.CO3IBiY6.js +0 -1
- package/dist/_astro/i18n.D_aOCHSN.js +0 -7
- package/dist/_astro/markdown-client.BS19nLL3.js +0 -62
- package/dist/_astro/plugins.BRvXWHLe.js +0 -1
- package/dist/_astro/project-docs.CCjtiFjB.js +0 -1
- package/dist/_astro/prompts-meta.BUGutlsk.js +0 -2
- package/dist/_astro/report-prompt.B9C4cv84.js +0 -1
- package/dist/_astro/req-analysis.FrD6BfwK.js +0 -31
- package/dist/_astro/story-review.R8iWiFzl.js +0 -14
- package/dist/_astro/test-reports.CuIjlEyK.js +0 -19
- package/dist/_astro/ui-auto-prompt.H9_yhau9.js +0 -9
- package/dist/_astro/ui-auto.DMv940x4.js +0 -1
|
@@ -0,0 +1,959 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
|
|
6
|
+
import { getUserConfigDir } from '../../cli/user-config.mjs';
|
|
7
|
+
import { resolveModelForSpace } from '../lh-config.mjs';
|
|
8
|
+
import { readPluginPrompt } from '../plugin-prompts.mjs';
|
|
9
|
+
import {
|
|
10
|
+
callExplorer,
|
|
11
|
+
hasExplorer,
|
|
12
|
+
startExplorer,
|
|
13
|
+
stopExplorer,
|
|
14
|
+
} from '../uiauto-explorer/sessions.mjs';
|
|
15
|
+
import { CASE_EXECUTION_PROMPT_FILE } from '../case-execution-prompt.mjs';
|
|
16
|
+
import { EXECUTION_STATUSES, applyCaseExecutionResult, loadCaseDocument } from './files.mjs';
|
|
17
|
+
|
|
18
|
+
const PACKAGE_ROOT = path.resolve(fileURLToPath(new URL('../..', import.meta.url)));
|
|
19
|
+
const MODEL_TIMEOUT_MS = 90_000;
|
|
20
|
+
const MODEL_HEARTBEAT_MS = 5_000;
|
|
21
|
+
const MAX_TOOL_ROUNDS = 60;
|
|
22
|
+
const AI_RESULT_STATUSES = ['通过', '不通过', '阻塞', '跳过'];
|
|
23
|
+
const ACTIVE = new Map();
|
|
24
|
+
const AUTH_ACTIVE = new Map();
|
|
25
|
+
const CLIENTS = new Map();
|
|
26
|
+
|
|
27
|
+
const str = (value, max = 4000) => (typeof value === 'string' ? value.trim().slice(0, max) : '');
|
|
28
|
+
const now = () => new Date().toISOString();
|
|
29
|
+
|
|
30
|
+
export const caseExecutionRoot = (root) => path.join(root, '.lantern', 'local', 'case-execution');
|
|
31
|
+
|
|
32
|
+
const runsDir = (root) => path.join(caseExecutionRoot(root), 'runs');
|
|
33
|
+
const runPath = (root, runId) => path.join(runsDir(root), `${runId}.json`);
|
|
34
|
+
|
|
35
|
+
function readJson(file, fallback) {
|
|
36
|
+
try {
|
|
37
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
38
|
+
} catch {
|
|
39
|
+
return fallback;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function writeJson(file, value) {
|
|
44
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
45
|
+
const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
46
|
+
fs.writeFileSync(temp, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600 });
|
|
47
|
+
fs.renameSync(temp, file);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function safeId(value) {
|
|
51
|
+
const id = str(value, 80);
|
|
52
|
+
if (!/^[a-zA-Z0-9_-]+$/.test(id)) {
|
|
53
|
+
throw Object.assign(new Error('执行编号无效'), { statusCode: 400 });
|
|
54
|
+
}
|
|
55
|
+
return id;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function targetUrl(value) {
|
|
59
|
+
let url;
|
|
60
|
+
try {
|
|
61
|
+
url = new URL(str(value, 2000));
|
|
62
|
+
} catch {
|
|
63
|
+
throw Object.assign(new Error('请填写有效的 HTTP / HTTPS 测试地址'), { statusCode: 400 });
|
|
64
|
+
}
|
|
65
|
+
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password) {
|
|
66
|
+
throw Object.assign(new Error('测试地址必须是有效的 HTTP / HTTPS 地址'), {
|
|
67
|
+
statusCode: 400,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return url.href;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function authRoot() {
|
|
74
|
+
return path.join(getUserConfigDir(), 'case-execution', 'auth');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function authKey(root, url) {
|
|
78
|
+
const space = crypto.createHash('sha256').update(path.resolve(root)).digest('hex');
|
|
79
|
+
const origin = crypto.createHash('sha256').update(new URL(url).origin).digest('hex');
|
|
80
|
+
return path.join(authRoot(), space, `${origin}.json`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function savedAuthPath(root, url) {
|
|
84
|
+
const file = authKey(root, url);
|
|
85
|
+
return fs.existsSync(file) ? file : '';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function normalizeResult(raw) {
|
|
89
|
+
const status = AI_RESULT_STATUSES.includes(str(raw?.status, 20)) ? str(raw?.status, 20) : '阻塞';
|
|
90
|
+
return {
|
|
91
|
+
status,
|
|
92
|
+
actualResult: str(raw?.actualResult ?? raw?.actual_result, 30000),
|
|
93
|
+
rationale: str(raw?.rationale, 12000),
|
|
94
|
+
unverified: Array.isArray(raw?.unverified)
|
|
95
|
+
? raw.unverified
|
|
96
|
+
.map((item) => str(item, 1000))
|
|
97
|
+
.filter(Boolean)
|
|
98
|
+
.slice(0, 100)
|
|
99
|
+
: [],
|
|
100
|
+
blocker: str(raw?.blocker, 4000),
|
|
101
|
+
stepResults: Array.isArray(raw?.stepResults)
|
|
102
|
+
? raw.stepResults
|
|
103
|
+
.slice(0, 100)
|
|
104
|
+
.map((item) => ({
|
|
105
|
+
expected: str(item?.expected, 4000),
|
|
106
|
+
actual: str(item?.actual, 4000),
|
|
107
|
+
passed: item?.passed === true,
|
|
108
|
+
evidence: str(item?.evidence, 2000),
|
|
109
|
+
}))
|
|
110
|
+
.filter((item) => item.expected || item.actual || item.evidence)
|
|
111
|
+
: [],
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function emptyRun({ casePath, rowIndex, caseData, fileHash, target, authPath }) {
|
|
116
|
+
const at = now();
|
|
117
|
+
return {
|
|
118
|
+
version: 1,
|
|
119
|
+
id: crypto.randomUUID(),
|
|
120
|
+
casePath,
|
|
121
|
+
rowIndex,
|
|
122
|
+
case: caseData,
|
|
123
|
+
fileHash,
|
|
124
|
+
target: {
|
|
125
|
+
url: target,
|
|
126
|
+
authPath: authPath || '',
|
|
127
|
+
},
|
|
128
|
+
status: 'idle',
|
|
129
|
+
runId: '',
|
|
130
|
+
seq: 0,
|
|
131
|
+
events: [],
|
|
132
|
+
progress: {
|
|
133
|
+
label: '等待执行',
|
|
134
|
+
elapsedSeconds: 0,
|
|
135
|
+
toolCount: 0,
|
|
136
|
+
},
|
|
137
|
+
result: null,
|
|
138
|
+
confirmed: null,
|
|
139
|
+
error: '',
|
|
140
|
+
createdAt: at,
|
|
141
|
+
updatedAt: at,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function publicRun(run) {
|
|
146
|
+
return {
|
|
147
|
+
...run,
|
|
148
|
+
target: { ...run.target, authPath: run.target.authPath ? '[local]' : '' },
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function appendEvent(run, event, root) {
|
|
153
|
+
run.seq += 1;
|
|
154
|
+
const item = {
|
|
155
|
+
id: crypto.randomUUID(),
|
|
156
|
+
seq: run.seq,
|
|
157
|
+
at: now(),
|
|
158
|
+
...event,
|
|
159
|
+
};
|
|
160
|
+
run.events.push(item);
|
|
161
|
+
run.events = run.events.slice(-800);
|
|
162
|
+
run.updatedAt = now();
|
|
163
|
+
for (const res of CLIENTS.get(`${root}\0${run.id}`) || []) {
|
|
164
|
+
if (res.destroyed || res.writableEnded) continue;
|
|
165
|
+
res.write(`data: ${JSON.stringify(item)}\n\n`);
|
|
166
|
+
}
|
|
167
|
+
return item;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function saveRun(root, run) {
|
|
171
|
+
run.updatedAt = now();
|
|
172
|
+
writeJson(runPath(root, run.id), run);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function loadRun(root, runId) {
|
|
176
|
+
const run = readJson(runPath(root, safeId(runId)), null);
|
|
177
|
+
if (!run) throw Object.assign(new Error('执行记录不存在'), { statusCode: 404 });
|
|
178
|
+
if (run.status === 'running' && !ACTIVE.has(`${root}\0${run.id}`)) {
|
|
179
|
+
run.status = 'interrupted';
|
|
180
|
+
run.error = '服务重启导致执行中断,请重新执行或手动记录结果';
|
|
181
|
+
appendEvent(run, { type: 'error', title: '执行中断', detail: run.error }, root);
|
|
182
|
+
saveRun(root, run);
|
|
183
|
+
}
|
|
184
|
+
return run;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function listRuns(root, casePath = '') {
|
|
188
|
+
if (!fs.existsSync(runsDir(root))) return [];
|
|
189
|
+
return fs
|
|
190
|
+
.readdirSync(runsDir(root))
|
|
191
|
+
.filter((name) => name.endsWith('.json'))
|
|
192
|
+
.map((name) => readJson(path.join(runsDir(root), name), null))
|
|
193
|
+
.filter(Boolean)
|
|
194
|
+
.filter((run) => !casePath || run.casePath === casePath)
|
|
195
|
+
.sort((left, right) => String(right.createdAt).localeCompare(String(left.createdAt)))
|
|
196
|
+
.slice(0, 200)
|
|
197
|
+
.map(publicRun);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function shotsDir(root, runId) {
|
|
201
|
+
return path.join(caseExecutionRoot(root), 'shots', runId);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function clip(value, max = 12000) {
|
|
205
|
+
const text = String(value ?? '');
|
|
206
|
+
return text.length > max ? `${text.slice(0, max)}\n...(已截断)` : text;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const TOOL_DEFS = [
|
|
210
|
+
{
|
|
211
|
+
type: 'function',
|
|
212
|
+
function: {
|
|
213
|
+
name: 'case_browser_open',
|
|
214
|
+
description: '打开当前测试目标地址。第一次执行必须先调用。',
|
|
215
|
+
parameters: {
|
|
216
|
+
type: 'object',
|
|
217
|
+
properties: {
|
|
218
|
+
url: { type: 'string', description: '默认使用任务目标;可传相对路径' },
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
type: 'function',
|
|
225
|
+
function: {
|
|
226
|
+
name: 'case_browser_snapshot',
|
|
227
|
+
description: '读取当前页面可交互元素快照,ref 只在本页有效。',
|
|
228
|
+
parameters: {
|
|
229
|
+
type: 'object',
|
|
230
|
+
properties: {
|
|
231
|
+
depth: { type: 'number' },
|
|
232
|
+
targetRef: { type: 'string' },
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
type: 'function',
|
|
239
|
+
function: {
|
|
240
|
+
name: 'case_browser_click',
|
|
241
|
+
description: '点击页面元素,成功后自动返回新快照。',
|
|
242
|
+
parameters: {
|
|
243
|
+
type: 'object',
|
|
244
|
+
properties: {
|
|
245
|
+
ref: { type: 'string' },
|
|
246
|
+
note: { type: 'string' },
|
|
247
|
+
},
|
|
248
|
+
required: ['ref'],
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
type: 'function',
|
|
254
|
+
function: {
|
|
255
|
+
name: 'case_browser_fill',
|
|
256
|
+
description: '清空并填写输入框。',
|
|
257
|
+
parameters: {
|
|
258
|
+
type: 'object',
|
|
259
|
+
properties: {
|
|
260
|
+
ref: { type: 'string' },
|
|
261
|
+
text: { type: 'string' },
|
|
262
|
+
note: { type: 'string' },
|
|
263
|
+
},
|
|
264
|
+
required: ['ref', 'text'],
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
type: 'function',
|
|
270
|
+
function: {
|
|
271
|
+
name: 'case_browser_select',
|
|
272
|
+
description: '选择下拉选项。',
|
|
273
|
+
parameters: {
|
|
274
|
+
type: 'object',
|
|
275
|
+
properties: {
|
|
276
|
+
ref: { type: 'string' },
|
|
277
|
+
value: { type: 'string' },
|
|
278
|
+
note: { type: 'string' },
|
|
279
|
+
},
|
|
280
|
+
required: ['ref', 'value'],
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
type: 'function',
|
|
286
|
+
function: {
|
|
287
|
+
name: 'case_browser_press',
|
|
288
|
+
description: '按键盘按键。',
|
|
289
|
+
parameters: {
|
|
290
|
+
type: 'object',
|
|
291
|
+
properties: {
|
|
292
|
+
key: { type: 'string' },
|
|
293
|
+
ref: { type: 'string' },
|
|
294
|
+
},
|
|
295
|
+
required: ['key'],
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
{
|
|
300
|
+
type: 'function',
|
|
301
|
+
function: {
|
|
302
|
+
name: 'case_browser_hover',
|
|
303
|
+
description: '悬停到页面元素。',
|
|
304
|
+
parameters: {
|
|
305
|
+
type: 'object',
|
|
306
|
+
properties: {
|
|
307
|
+
ref: { type: 'string' },
|
|
308
|
+
note: { type: 'string' },
|
|
309
|
+
},
|
|
310
|
+
required: ['ref'],
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
},
|
|
314
|
+
{
|
|
315
|
+
type: 'function',
|
|
316
|
+
function: {
|
|
317
|
+
name: 'case_browser_wait',
|
|
318
|
+
description: '等待页面出现指定文案或等待指定毫秒。',
|
|
319
|
+
parameters: {
|
|
320
|
+
type: 'object',
|
|
321
|
+
properties: {
|
|
322
|
+
text: { type: 'string' },
|
|
323
|
+
ms: { type: 'number' },
|
|
324
|
+
timeout: { type: 'number' },
|
|
325
|
+
},
|
|
326
|
+
},
|
|
327
|
+
},
|
|
328
|
+
},
|
|
329
|
+
{
|
|
330
|
+
type: 'function',
|
|
331
|
+
function: {
|
|
332
|
+
name: 'case_browser_screenshot',
|
|
333
|
+
description: '保存当前页面截图作为执行证据。',
|
|
334
|
+
parameters: {
|
|
335
|
+
type: 'object',
|
|
336
|
+
properties: { label: { type: 'string' } },
|
|
337
|
+
},
|
|
338
|
+
},
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
type: 'function',
|
|
342
|
+
function: {
|
|
343
|
+
name: 'case_browser_console',
|
|
344
|
+
description: '读取当前页面控制台错误、警告和 pageerror。',
|
|
345
|
+
parameters: { type: 'object', properties: {} },
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
{
|
|
349
|
+
type: 'function',
|
|
350
|
+
function: {
|
|
351
|
+
name: 'case_browser_network',
|
|
352
|
+
description: '读取最近失败的网络请求。',
|
|
353
|
+
parameters: { type: 'object', properties: {} },
|
|
354
|
+
},
|
|
355
|
+
},
|
|
356
|
+
{
|
|
357
|
+
type: 'function',
|
|
358
|
+
function: {
|
|
359
|
+
name: 'finish_case_execution',
|
|
360
|
+
description: '提交本条用例的最终执行结论。完成所有可执行步骤后必须调用。',
|
|
361
|
+
parameters: {
|
|
362
|
+
type: 'object',
|
|
363
|
+
properties: {
|
|
364
|
+
status: { type: 'string', enum: AI_RESULT_STATUSES },
|
|
365
|
+
actualResult: { type: 'string' },
|
|
366
|
+
rationale: { type: 'string' },
|
|
367
|
+
unverified: { type: 'array', items: { type: 'string' } },
|
|
368
|
+
blocker: { type: 'string' },
|
|
369
|
+
stepResults: {
|
|
370
|
+
type: 'array',
|
|
371
|
+
items: {
|
|
372
|
+
type: 'object',
|
|
373
|
+
properties: {
|
|
374
|
+
expected: { type: 'string' },
|
|
375
|
+
actual: { type: 'string' },
|
|
376
|
+
passed: { type: 'boolean' },
|
|
377
|
+
evidence: { type: 'string' },
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
},
|
|
381
|
+
},
|
|
382
|
+
required: ['status', 'actualResult', 'rationale'],
|
|
383
|
+
},
|
|
384
|
+
},
|
|
385
|
+
},
|
|
386
|
+
];
|
|
387
|
+
|
|
388
|
+
function modelMessages(skill, run) {
|
|
389
|
+
return [
|
|
390
|
+
{
|
|
391
|
+
role: 'system',
|
|
392
|
+
content: `${skill}
|
|
393
|
+
|
|
394
|
+
当前任务只执行一条测试用例。目标地址:${run.target.url}
|
|
395
|
+
你可以使用提供的浏览器工具。每次操作前先读取快照,元素 ref 在页面变化后会失效。
|
|
396
|
+
只有真正观察到预期结果才能判定通过。最终必须调用 finish_case_execution。
|
|
397
|
+
安全边界:只操作当前目标站点;遇到验证码、登录墙、系统原生弹窗、文件选择器、支付、短信验证码或不可逆高风险动作时标记阻塞。`,
|
|
398
|
+
},
|
|
399
|
+
{
|
|
400
|
+
role: 'user',
|
|
401
|
+
content: JSON.stringify({
|
|
402
|
+
case: run.case,
|
|
403
|
+
targetUrl: run.target.url,
|
|
404
|
+
requiredExecutionColumns: {
|
|
405
|
+
status: true,
|
|
406
|
+
actualResult: true,
|
|
407
|
+
},
|
|
408
|
+
}),
|
|
409
|
+
},
|
|
410
|
+
];
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
async function callModel(settings, messages, signal) {
|
|
414
|
+
const url = new URL(settings.baseURL.replace(/\/+$/, '') + '/chat/completions');
|
|
415
|
+
const response = await fetch(url, {
|
|
416
|
+
method: 'POST',
|
|
417
|
+
headers: {
|
|
418
|
+
'Content-Type': 'application/json',
|
|
419
|
+
Authorization: `Bearer ${settings.apiKey}`,
|
|
420
|
+
},
|
|
421
|
+
body: JSON.stringify({
|
|
422
|
+
model: settings.model,
|
|
423
|
+
messages,
|
|
424
|
+
tools: TOOL_DEFS,
|
|
425
|
+
tool_choice: 'auto',
|
|
426
|
+
temperature: 0.1,
|
|
427
|
+
stream: false,
|
|
428
|
+
}),
|
|
429
|
+
signal,
|
|
430
|
+
});
|
|
431
|
+
if (!response.ok) {
|
|
432
|
+
const detail = (await response.text().catch(() => '')).slice(0, 400);
|
|
433
|
+
throw new Error(`模型请求失败(HTTP ${response.status})${detail ? `:${detail}` : ''}`);
|
|
434
|
+
}
|
|
435
|
+
const data = await response.json();
|
|
436
|
+
return {
|
|
437
|
+
usage: data.usage || {},
|
|
438
|
+
message: data.choices?.[0]?.message || {},
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
async function executeTool(root, run, name, args) {
|
|
443
|
+
if (name === 'case_browser_open') {
|
|
444
|
+
const result = await callExplorer(run.id, 'open', {
|
|
445
|
+
url: str(args.url, 2000) || run.target.url,
|
|
446
|
+
settle: 450,
|
|
447
|
+
});
|
|
448
|
+
return {
|
|
449
|
+
url: result.url,
|
|
450
|
+
title: result.title,
|
|
451
|
+
count: result.count,
|
|
452
|
+
snapshot: clip(result.snapshot),
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
if (name === 'case_browser_snapshot') {
|
|
456
|
+
const result = await callExplorer(run.id, 'snapshot', {
|
|
457
|
+
depth: Number(args.depth) || 7,
|
|
458
|
+
targetRef: str(args.targetRef, 40),
|
|
459
|
+
});
|
|
460
|
+
return {
|
|
461
|
+
url: result.url,
|
|
462
|
+
title: result.title,
|
|
463
|
+
count: result.count,
|
|
464
|
+
snapshot: clip(result.snapshot),
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
if (
|
|
468
|
+
name === 'case_browser_click' ||
|
|
469
|
+
name === 'case_browser_fill' ||
|
|
470
|
+
name === 'case_browser_select' ||
|
|
471
|
+
name === 'case_browser_hover'
|
|
472
|
+
) {
|
|
473
|
+
const action = name.replace('case_browser_', '');
|
|
474
|
+
const result = await callExplorer(
|
|
475
|
+
run.id,
|
|
476
|
+
'action',
|
|
477
|
+
{
|
|
478
|
+
action,
|
|
479
|
+
ref: str(args.ref, 40),
|
|
480
|
+
text: str(args.text, 8000),
|
|
481
|
+
value: str(args.value, 8000),
|
|
482
|
+
note: str(args.note, 200),
|
|
483
|
+
},
|
|
484
|
+
90_000,
|
|
485
|
+
);
|
|
486
|
+
return {
|
|
487
|
+
ok: true,
|
|
488
|
+
url: result.url,
|
|
489
|
+
snapshot: clip(result.snapshot, 9000),
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
if (name === 'case_browser_press') {
|
|
493
|
+
const result = await callExplorer(run.id, 'action', {
|
|
494
|
+
action: 'press',
|
|
495
|
+
ref: str(args.ref, 40),
|
|
496
|
+
key: str(args.key, 40),
|
|
497
|
+
});
|
|
498
|
+
return { ok: true, url: result.url, snapshot: clip(result.snapshot, 9000) };
|
|
499
|
+
}
|
|
500
|
+
if (name === 'case_browser_wait') {
|
|
501
|
+
const result = await callExplorer(
|
|
502
|
+
run.id,
|
|
503
|
+
'waitFor',
|
|
504
|
+
{
|
|
505
|
+
text: str(args.text, 200),
|
|
506
|
+
ms: Number(args.ms) || 0,
|
|
507
|
+
timeout: Math.min(60_000, Math.max(1000, Number(args.timeout) || 15_000)),
|
|
508
|
+
},
|
|
509
|
+
90_000,
|
|
510
|
+
);
|
|
511
|
+
return { ok: true, url: result.url, snapshot: clip(result.snapshot, 9000) };
|
|
512
|
+
}
|
|
513
|
+
if (name === 'case_browser_screenshot') {
|
|
514
|
+
const result = await callExplorer(run.id, 'screenshot', {
|
|
515
|
+
label: str(args.label, 80) || `step-${run.progress.toolCount}`,
|
|
516
|
+
});
|
|
517
|
+
const url = `/__tb_case_run/runs/${encodeURIComponent(run.id)}/shots/${encodeURIComponent(result.file)}`;
|
|
518
|
+
appendEvent(run, { type: 'screenshot', title: '执行截图', detail: result.file, url }, root);
|
|
519
|
+
return { ok: true, file: result.file, url };
|
|
520
|
+
}
|
|
521
|
+
if (name === 'case_browser_console') {
|
|
522
|
+
const result = await callExplorer(run.id, 'console', {});
|
|
523
|
+
return { count: result.messages?.length || 0, messages: (result.messages || []).slice(-40) };
|
|
524
|
+
}
|
|
525
|
+
if (name === 'case_browser_network') {
|
|
526
|
+
const result = await callExplorer(run.id, 'network', {});
|
|
527
|
+
return { count: result.requests?.length || 0, requests: (result.requests || []).slice(-40) };
|
|
528
|
+
}
|
|
529
|
+
throw new Error(`未知工具:${name}`);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
async function executeRun(root, run, skill, settings, signal) {
|
|
533
|
+
const active = ACTIVE.get(`${root}\0${run.id}`);
|
|
534
|
+
try {
|
|
535
|
+
run.status = 'running';
|
|
536
|
+
saveRun(root, run);
|
|
537
|
+
appendEvent(run, { type: 'status', title: '开始执行', detail: run.case.title }, root);
|
|
538
|
+
fs.mkdirSync(caseExecutionRoot(root), { recursive: true });
|
|
539
|
+
if (!run.target.authPath || !fs.existsSync(run.target.authPath)) {
|
|
540
|
+
throw new Error('目标登录态不存在,请先填写登录态文件或完成手动登录');
|
|
541
|
+
}
|
|
542
|
+
await startExplorer({
|
|
543
|
+
baseDir: caseExecutionRoot(root),
|
|
544
|
+
project: { dir: PACKAGE_ROOT },
|
|
545
|
+
sessionId: run.id,
|
|
546
|
+
baseURL: run.target.url,
|
|
547
|
+
headed: true,
|
|
548
|
+
storageState: run.target.authPath,
|
|
549
|
+
});
|
|
550
|
+
fs.mkdirSync(shotsDir(root, run.id), { recursive: true });
|
|
551
|
+
const messages = modelMessages(skill, run);
|
|
552
|
+
const startedAt = Date.now();
|
|
553
|
+
let result = null;
|
|
554
|
+
for (let round = 0; round < MAX_TOOL_ROUNDS && !result; round += 1) {
|
|
555
|
+
if (signal.aborted) throw signal.reason || new Error('已停止');
|
|
556
|
+
run.progress.elapsedSeconds = Math.max(1, Math.round((Date.now() - startedAt) / 1000));
|
|
557
|
+
run.progress.label = `AI 正在执行(第 ${round + 1} 轮)`;
|
|
558
|
+
saveRun(root, run);
|
|
559
|
+
const heartbeat = setInterval(() => {
|
|
560
|
+
run.progress.elapsedSeconds = Math.max(1, Math.round((Date.now() - startedAt) / 1000));
|
|
561
|
+
run.progress.label = `AI 正在执行(已等待 ${run.progress.elapsedSeconds} 秒)`;
|
|
562
|
+
appendEvent(
|
|
563
|
+
run,
|
|
564
|
+
{
|
|
565
|
+
type: 'progress',
|
|
566
|
+
title: run.progress.label,
|
|
567
|
+
detail: `${run.progress.toolCount} 次工具调用`,
|
|
568
|
+
},
|
|
569
|
+
root,
|
|
570
|
+
);
|
|
571
|
+
saveRun(root, run);
|
|
572
|
+
}, MODEL_HEARTBEAT_MS);
|
|
573
|
+
let response;
|
|
574
|
+
try {
|
|
575
|
+
response = await callModel(settings, messages, signal);
|
|
576
|
+
} finally {
|
|
577
|
+
clearInterval(heartbeat);
|
|
578
|
+
}
|
|
579
|
+
const message = response.message || {};
|
|
580
|
+
messages.push(message);
|
|
581
|
+
const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
|
|
582
|
+
if (!toolCalls.length) {
|
|
583
|
+
const content = str(message.content, 30000);
|
|
584
|
+
if (content) {
|
|
585
|
+
try {
|
|
586
|
+
const parsed = JSON.parse(
|
|
587
|
+
content.replace(/^```(?:json)?\s*/i, '').replace(/\s*```$/, ''),
|
|
588
|
+
);
|
|
589
|
+
result = normalizeResult(parsed);
|
|
590
|
+
break;
|
|
591
|
+
} catch {
|
|
592
|
+
messages.push({
|
|
593
|
+
role: 'user',
|
|
594
|
+
content: '请继续执行;完成后必须调用 finish_case_execution,不要只返回普通文本。',
|
|
595
|
+
});
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
messages.push({
|
|
600
|
+
role: 'user',
|
|
601
|
+
content: '请继续执行,并在完成时调用 finish_case_execution。',
|
|
602
|
+
});
|
|
603
|
+
continue;
|
|
604
|
+
}
|
|
605
|
+
for (const call of toolCalls) {
|
|
606
|
+
if (signal.aborted) throw signal.reason || new Error('已停止');
|
|
607
|
+
const name = str(call?.function?.name, 100);
|
|
608
|
+
let args = {};
|
|
609
|
+
try {
|
|
610
|
+
args = JSON.parse(str(call?.function?.arguments, 20000) || '{}');
|
|
611
|
+
} catch {
|
|
612
|
+
messages.push({
|
|
613
|
+
role: 'tool',
|
|
614
|
+
tool_call_id: str(call?.id, 200),
|
|
615
|
+
content: JSON.stringify({ error: '工具参数不是合法 JSON' }),
|
|
616
|
+
});
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
if (name === 'finish_case_execution') {
|
|
620
|
+
result = normalizeResult(args);
|
|
621
|
+
messages.push({
|
|
622
|
+
role: 'tool',
|
|
623
|
+
tool_call_id: str(call?.id, 200),
|
|
624
|
+
content: JSON.stringify({ ok: true, accepted: true }),
|
|
625
|
+
});
|
|
626
|
+
break;
|
|
627
|
+
}
|
|
628
|
+
run.progress.toolCount += 1;
|
|
629
|
+
appendEvent(
|
|
630
|
+
run,
|
|
631
|
+
{
|
|
632
|
+
type: 'tool',
|
|
633
|
+
title: name,
|
|
634
|
+
detail: str(args.note, 300) || '执行浏览器操作',
|
|
635
|
+
status: 'running',
|
|
636
|
+
},
|
|
637
|
+
root,
|
|
638
|
+
);
|
|
639
|
+
try {
|
|
640
|
+
const output = await executeTool(root, run, name, args);
|
|
641
|
+
messages.push({
|
|
642
|
+
role: 'tool',
|
|
643
|
+
tool_call_id: str(call?.id, 200),
|
|
644
|
+
content: JSON.stringify(output),
|
|
645
|
+
});
|
|
646
|
+
appendEvent(
|
|
647
|
+
run,
|
|
648
|
+
{
|
|
649
|
+
type: 'tool',
|
|
650
|
+
title: name,
|
|
651
|
+
detail: str(args.note, 300) || '已完成',
|
|
652
|
+
status: 'done',
|
|
653
|
+
},
|
|
654
|
+
root,
|
|
655
|
+
);
|
|
656
|
+
} catch (error) {
|
|
657
|
+
const messageText = error instanceof Error ? error.message : String(error);
|
|
658
|
+
messages.push({
|
|
659
|
+
role: 'tool',
|
|
660
|
+
tool_call_id: str(call?.id, 200),
|
|
661
|
+
content: JSON.stringify({ error: messageText }),
|
|
662
|
+
});
|
|
663
|
+
appendEvent(
|
|
664
|
+
run,
|
|
665
|
+
{ type: 'tool', title: name, detail: messageText, status: 'error' },
|
|
666
|
+
root,
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
if (!result) {
|
|
672
|
+
result = normalizeResult({
|
|
673
|
+
status: '阻塞',
|
|
674
|
+
actualResult: 'AI 执行达到工具轮次上限,未形成最终结论。',
|
|
675
|
+
rationale: '执行过程未完成全部步骤或未观察到可判定结果。',
|
|
676
|
+
blocker: '达到工具轮次上限',
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
if (!run.result && !result.actualResult) {
|
|
680
|
+
result.actualResult = result.blocker || result.rationale || '未完成用例执行';
|
|
681
|
+
}
|
|
682
|
+
run.result = result;
|
|
683
|
+
run.status = 'awaiting_confirmation';
|
|
684
|
+
run.progress.label = '执行完成,等待确认';
|
|
685
|
+
appendEvent(
|
|
686
|
+
run,
|
|
687
|
+
{
|
|
688
|
+
type: 'result',
|
|
689
|
+
title: '执行完成',
|
|
690
|
+
detail: `${result.status} · ${result.actualResult.slice(0, 160)}`,
|
|
691
|
+
},
|
|
692
|
+
root,
|
|
693
|
+
);
|
|
694
|
+
saveRun(root, run);
|
|
695
|
+
} catch (error) {
|
|
696
|
+
const aborted = signal.aborted;
|
|
697
|
+
const message = aborted ? '执行已停止' : error instanceof Error ? error.message : String(error);
|
|
698
|
+
run.status = aborted ? 'stopped' : 'error';
|
|
699
|
+
run.error = message;
|
|
700
|
+
run.result = normalizeResult({
|
|
701
|
+
status: '阻塞',
|
|
702
|
+
actualResult: message,
|
|
703
|
+
rationale: '执行未能完成。',
|
|
704
|
+
blocker: message,
|
|
705
|
+
unverified: ['全部用例步骤'],
|
|
706
|
+
});
|
|
707
|
+
appendEvent(
|
|
708
|
+
run,
|
|
709
|
+
{
|
|
710
|
+
type: 'error',
|
|
711
|
+
title: run.status === 'stopped' ? '执行已停止' : '执行失败',
|
|
712
|
+
detail: message,
|
|
713
|
+
},
|
|
714
|
+
root,
|
|
715
|
+
);
|
|
716
|
+
saveRun(root, run);
|
|
717
|
+
} finally {
|
|
718
|
+
if (hasExplorer(run.id)) stopExplorer(run.id);
|
|
719
|
+
active?.controller?.abort();
|
|
720
|
+
const key = `${root}\0${run.id}`;
|
|
721
|
+
for (const client of CLIENTS.get(key) || []) {
|
|
722
|
+
if (!client.destroyed && !client.writableEnded) client.end();
|
|
723
|
+
}
|
|
724
|
+
CLIENTS.delete(key);
|
|
725
|
+
ACTIVE.delete(`${root}\0${run.id}`);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
function skillText(root) {
|
|
730
|
+
return (
|
|
731
|
+
readPluginPrompt(CASE_EXECUTION_PROMPT_FILE, root).trim() ||
|
|
732
|
+
'执行当前单条测试用例,真实操作并给出通过、不通过、阻塞或跳过的可核验结论。'
|
|
733
|
+
);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
export function startRun(root, raw) {
|
|
737
|
+
if (ACTIVE.size) {
|
|
738
|
+
throw Object.assign(new Error('当前空间已有用例正在执行'), { statusCode: 409 });
|
|
739
|
+
}
|
|
740
|
+
const casePath = str(raw.casePath, 1000);
|
|
741
|
+
const rowIndex = Number(raw.rowIndex);
|
|
742
|
+
const url = targetUrl(raw.url || raw.targetUrl);
|
|
743
|
+
const authPath = str(raw.storageStatePath, 2000) || savedAuthPath(root, url) || '';
|
|
744
|
+
if (!authPath) {
|
|
745
|
+
throw Object.assign(new Error('请先填写登录态文件或完成手动登录'), { statusCode: 400 });
|
|
746
|
+
}
|
|
747
|
+
const authAbs = path.resolve(authPath);
|
|
748
|
+
if (!fs.existsSync(authAbs) || !fs.statSync(authAbs).isFile()) {
|
|
749
|
+
throw Object.assign(new Error('登录态文件不存在'), { statusCode: 400 });
|
|
750
|
+
}
|
|
751
|
+
return (async () => {
|
|
752
|
+
const document = await loadCaseDocument(root, casePath);
|
|
753
|
+
if (!document.executable) {
|
|
754
|
+
throw Object.assign(new Error(document.executionIssue), {
|
|
755
|
+
statusCode: 422,
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
if (!Number.isInteger(rowIndex) || rowIndex < 0 || rowIndex >= document.cases.length) {
|
|
759
|
+
throw Object.assign(new Error('用例行不存在'), { statusCode: 404 });
|
|
760
|
+
}
|
|
761
|
+
const caseData = document.cases[rowIndex];
|
|
762
|
+
const run = emptyRun({
|
|
763
|
+
casePath,
|
|
764
|
+
rowIndex,
|
|
765
|
+
caseData,
|
|
766
|
+
fileHash: document.fileHash,
|
|
767
|
+
target: url,
|
|
768
|
+
authPath: authAbs,
|
|
769
|
+
});
|
|
770
|
+
const settings = resolveModelForSpace(root);
|
|
771
|
+
if (!settings.baseURL || !settings.apiKey || !settings.model) {
|
|
772
|
+
throw Object.assign(new Error('请先在模型设置中配置可用模型'), { statusCode: 400 });
|
|
773
|
+
}
|
|
774
|
+
const controller = new AbortController();
|
|
775
|
+
run.status = 'running';
|
|
776
|
+
run.runId = run.id;
|
|
777
|
+
saveRun(root, run);
|
|
778
|
+
ACTIVE.set(`${root}\0${run.id}`, { controller, run });
|
|
779
|
+
void executeRun(root, run, skillText(root), settings, controller.signal);
|
|
780
|
+
return publicRun(run);
|
|
781
|
+
})();
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
export function getRun(root, runId) {
|
|
785
|
+
return publicRun(loadRun(root, runId));
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
export function listExecutionRuns(root, casePath) {
|
|
789
|
+
return listRuns(root, str(casePath, 1000));
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
export function stopRun(root, runId) {
|
|
793
|
+
const run = loadRun(root, runId);
|
|
794
|
+
const active = ACTIVE.get(`${root}\0${run.id}`);
|
|
795
|
+
if (!active) return publicRun(run);
|
|
796
|
+
active.controller.abort(new Error('用户停止执行'));
|
|
797
|
+
if (hasExplorer(run.id)) stopExplorer(run.id);
|
|
798
|
+
run.status = 'stopped';
|
|
799
|
+
run.error = '用户停止执行';
|
|
800
|
+
appendEvent(run, { type: 'status', title: '执行已停止', status: 'stopped' }, root);
|
|
801
|
+
saveRun(root, run);
|
|
802
|
+
return publicRun(run);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
export async function confirmRun(root, runId, raw) {
|
|
806
|
+
const run = loadRun(root, runId);
|
|
807
|
+
if (run.status === 'running') {
|
|
808
|
+
throw Object.assign(new Error('用例仍在执行,请等待结束或先停止'), { statusCode: 409 });
|
|
809
|
+
}
|
|
810
|
+
const status = str(raw.status, 20);
|
|
811
|
+
const actualResult = str(raw.actualResult, 30000);
|
|
812
|
+
if (!EXECUTION_STATUSES.includes(status)) {
|
|
813
|
+
throw Object.assign(new Error('用例状态无效'), { statusCode: 400 });
|
|
814
|
+
}
|
|
815
|
+
if (!actualResult) {
|
|
816
|
+
throw Object.assign(new Error('实际结果不能为空'), { statusCode: 400 });
|
|
817
|
+
}
|
|
818
|
+
const applied = await applyCaseExecutionResult(root, {
|
|
819
|
+
path: run.casePath,
|
|
820
|
+
expectedFileHash: run.fileHash,
|
|
821
|
+
rowIndex: run.rowIndex,
|
|
822
|
+
status,
|
|
823
|
+
actualResult,
|
|
824
|
+
executor: str(raw.executor, 200) || 'AI + 人工确认',
|
|
825
|
+
executedAt: now().replace('T', ' ').slice(0, 19),
|
|
826
|
+
evidence: run.events
|
|
827
|
+
.filter((event) => event.type === 'screenshot')
|
|
828
|
+
.map((event) => event.url)
|
|
829
|
+
.slice(-8)
|
|
830
|
+
.join('\n'),
|
|
831
|
+
defectNo: str(raw.defectNo, 200),
|
|
832
|
+
});
|
|
833
|
+
run.status = 'confirmed';
|
|
834
|
+
run.confirmed = {
|
|
835
|
+
status,
|
|
836
|
+
actualResult,
|
|
837
|
+
at: now(),
|
|
838
|
+
backup: applied.backup,
|
|
839
|
+
fileHash: applied.fileHash,
|
|
840
|
+
};
|
|
841
|
+
appendEvent(
|
|
842
|
+
run,
|
|
843
|
+
{
|
|
844
|
+
type: 'applied',
|
|
845
|
+
title: '结果已写入用例文件',
|
|
846
|
+
detail: `${status} · ${actualResult.slice(0, 120)}`,
|
|
847
|
+
},
|
|
848
|
+
root,
|
|
849
|
+
);
|
|
850
|
+
saveRun(root, run);
|
|
851
|
+
return { run: publicRun(run), applied };
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
export async function applyManualResult(root, raw) {
|
|
855
|
+
const status = str(raw.status, 20);
|
|
856
|
+
const actualResult = str(raw.actualResult, 30000);
|
|
857
|
+
if (!EXECUTION_STATUSES.includes(status)) {
|
|
858
|
+
throw Object.assign(new Error('用例状态无效'), { statusCode: 400 });
|
|
859
|
+
}
|
|
860
|
+
if (!actualResult) {
|
|
861
|
+
throw Object.assign(new Error('实际结果不能为空'), { statusCode: 400 });
|
|
862
|
+
}
|
|
863
|
+
return applyCaseExecutionResult(root, {
|
|
864
|
+
path: str(raw.casePath, 1000),
|
|
865
|
+
expectedFileHash: str(raw.expectedFileHash, 80),
|
|
866
|
+
rowIndex: Number(raw.rowIndex),
|
|
867
|
+
status,
|
|
868
|
+
actualResult,
|
|
869
|
+
executor: str(raw.executor, 200) || '人工',
|
|
870
|
+
executedAt: now().replace('T', ' ').slice(0, 19),
|
|
871
|
+
evidence: str(raw.evidence, 4000),
|
|
872
|
+
defectNo: str(raw.defectNo, 200),
|
|
873
|
+
});
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
export function subscribeRun(root, runId, after, res) {
|
|
877
|
+
const run = loadRun(root, runId);
|
|
878
|
+
const key = `${root}\0${run.id}`;
|
|
879
|
+
res.statusCode = 200;
|
|
880
|
+
res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
|
|
881
|
+
res.setHeader('Cache-Control', 'no-cache, no-transform');
|
|
882
|
+
res.setHeader('Connection', 'keep-alive');
|
|
883
|
+
res.flushHeaders?.();
|
|
884
|
+
for (const event of run.events.filter((item) => Number(item.seq) > Number(after || 0))) {
|
|
885
|
+
res.write(`data: ${JSON.stringify(event)}\n\n`);
|
|
886
|
+
}
|
|
887
|
+
if (run.status !== 'running') {
|
|
888
|
+
res.end();
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
const set = CLIENTS.get(key) || new Set();
|
|
892
|
+
set.add(res);
|
|
893
|
+
CLIENTS.set(key, set);
|
|
894
|
+
reqOnClose(res, () => {
|
|
895
|
+
set.delete(res);
|
|
896
|
+
if (!set.size) CLIENTS.delete(key);
|
|
897
|
+
});
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
function reqOnClose(res, fn) {
|
|
901
|
+
res.once('close', fn);
|
|
902
|
+
res.once('finish', fn);
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
export async function startManualLogin(root, raw) {
|
|
906
|
+
if (AUTH_ACTIVE.size) {
|
|
907
|
+
throw Object.assign(new Error('已有登录窗口正在等待完成'), { statusCode: 409 });
|
|
908
|
+
}
|
|
909
|
+
const url = targetUrl(raw.url);
|
|
910
|
+
const { chromium } = await import('playwright');
|
|
911
|
+
const browser = await chromium.launch({ headless: false });
|
|
912
|
+
const context = await browser.newContext();
|
|
913
|
+
const page = await context.newPage();
|
|
914
|
+
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 30_000 });
|
|
915
|
+
const id = crypto.randomUUID();
|
|
916
|
+
const session = { id, root, url, browser, context, page, status: 'awaiting_login' };
|
|
917
|
+
AUTH_ACTIVE.set(root, session);
|
|
918
|
+
return { ok: true, authId: id, status: session.status, url: page.url() };
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
export async function completeManualLogin(root) {
|
|
922
|
+
const session = AUTH_ACTIVE.get(root);
|
|
923
|
+
if (!session) {
|
|
924
|
+
throw Object.assign(new Error('没有等待完成的登录窗口'), { statusCode: 404 });
|
|
925
|
+
}
|
|
926
|
+
const pageUrl = new URL(session.page.url());
|
|
927
|
+
if (pageUrl.origin !== new URL(session.url).origin) {
|
|
928
|
+
throw Object.assign(new Error('请先完成登录并进入目标业务系统'), { statusCode: 409 });
|
|
929
|
+
}
|
|
930
|
+
const state = await session.context.storageState();
|
|
931
|
+
const file = authKey(root, session.url);
|
|
932
|
+
writeJson(file, state);
|
|
933
|
+
await session.browser.close().catch(() => undefined);
|
|
934
|
+
AUTH_ACTIVE.delete(root);
|
|
935
|
+
return { ok: true, status: 'ready', authPath: file };
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
export async function cancelManualLogin(root) {
|
|
939
|
+
const session = AUTH_ACTIVE.get(root);
|
|
940
|
+
if (!session) return { ok: true, status: 'idle' };
|
|
941
|
+
await session.browser.close().catch(() => undefined);
|
|
942
|
+
AUTH_ACTIVE.delete(root);
|
|
943
|
+
return { ok: true, status: 'cancelled' };
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
export function savedAuthFor(root, raw) {
|
|
947
|
+
const url = targetUrl(raw.url);
|
|
948
|
+
const file = savedAuthPath(root, url);
|
|
949
|
+
return { exists: Boolean(file) };
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
export function shotPath(root, runId, file) {
|
|
953
|
+
const base = path.resolve(shotsDir(root, safeId(runId)));
|
|
954
|
+
const abs = path.resolve(base, path.basename(str(file, 200)));
|
|
955
|
+
if (!abs.startsWith(base + path.sep)) {
|
|
956
|
+
throw Object.assign(new Error('截图路径无效'), { statusCode: 400 });
|
|
957
|
+
}
|
|
958
|
+
return abs;
|
|
959
|
+
}
|