lantern-harness 0.1.45 → 0.2.3

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.
Files changed (274) hide show
  1. package/CHANGELOG.md +242 -0
  2. package/README.md +25 -8
  3. package/builtin/case-backup-contract.mjs +27 -0
  4. package/builtin/case-workflow-contract.mjs +158 -0
  5. package/builtin/session/lh-fs.mjs +93 -4
  6. package/builtin/session/lh-nav-groups.mjs +36 -1
  7. package/builtin/session/mcp-client.mjs +7 -3
  8. package/builtin/session/session-runs.mjs +5 -1
  9. package/builtin/session/skill-validation.mjs +72 -0
  10. package/builtin/session/space-layout.mjs +8 -1
  11. package/builtin/session/space-skills.mjs +758 -0
  12. package/cli/data-root.mjs +21 -4
  13. package/cli/lantern.mjs +26 -1
  14. package/cli/mcp-server.mjs +3733 -0
  15. package/cli/server.mjs +10 -2
  16. package/config/app.defaults.json +4 -2
  17. package/dist/500.html +14 -6
  18. package/dist/_astro/Base.BQf1tZMl.css +1 -0
  19. package/dist/_astro/Base.astro_astro_type_script_index_0_lang.Dm3O_y-S.js +2 -0
  20. package/dist/_astro/BusinessMapBoard.CYxHcku_.js +5 -0
  21. package/dist/_astro/CaseExecBoard.CK9aGPmH.js +1 -0
  22. package/dist/_astro/CaseExecutionBoard.C94pmIMQ.js +5 -0
  23. package/dist/_astro/CaseGenerateApp.D5xlk9At.js +10 -0
  24. package/dist/_astro/CasesLibrary.BHQC955x.js +21 -0
  25. package/dist/_astro/ChatApp.CIt15w_1.js +1 -0
  26. package/dist/_astro/ChatApp.d4a9R3YS.js +262 -0
  27. package/dist/_astro/{ConfirmDialog.DrrPf1MB.js → ConfirmDialog.DQFd6g2r.js} +1 -1
  28. package/dist/_astro/DashboardBoard.qgc1s7pG.js +5 -0
  29. package/dist/_astro/{DatePicker.Bkl71M49.js → DatePicker.BRO_CjVQ.js} +1 -1
  30. package/dist/_astro/DefectSubmitEntry.JI85N1-i.js +2 -0
  31. package/dist/_astro/DocsFileBrowser.z5JZ74cy.js +1 -0
  32. package/dist/_astro/{DocsPager.Duf4I68E.js → DocsPager.Dg765uHW.js} +1 -1
  33. package/dist/_astro/{DrawerField.BH3p4Q4D.js → DrawerField.CLdvmHc6.js} +1 -1
  34. package/dist/_astro/KnowledgeLibrary.mkShtVun.js +1 -0
  35. package/dist/_astro/LifeKrTasksBoard.DpAwQffE.js +1 -0
  36. package/dist/_astro/LifeKrTodosBoard.BIjPiZ8H.js +1 -0
  37. package/dist/_astro/LifeObjectivesBoard.DYwLHPEG.js +1 -0
  38. package/dist/_astro/LifePlanTypeSelect.CYaziESp.js +1 -0
  39. package/dist/_astro/{LifeReviewsBoard.BPXeVpg1.js → LifeReviewsBoard.DIxoWBDA.js} +3 -3
  40. package/dist/_astro/{LifeShared.tnT7aQON.js → LifeShared.DuFQsMEx.js} +2 -2
  41. package/dist/_astro/LifeStagesBoard.DtOri9EW.js +1 -0
  42. package/dist/_astro/LifeYearCycleSelect.ByJ9-PGg.js +1 -0
  43. package/dist/_astro/LifeYearPlansBoard.BQN07wcv.js +1 -0
  44. package/dist/_astro/MultiSelectDropdown.D-rbpivp.js +1 -0
  45. package/dist/_astro/{NamePromptDialog.Csfnh5eM.js → NamePromptDialog.KPCeXmri.js} +1 -1
  46. package/dist/_astro/NavLibraries.BO4ezcUc.js +1 -0
  47. package/dist/_astro/PluginsPage.B1VeDtLx.js +18 -0
  48. package/dist/_astro/PmBugsBoard.Ddl3kqqt.js +1 -0
  49. package/dist/_astro/PmIterationsBoard.BJN7disI.js +1 -0
  50. package/dist/_astro/{PmProjectDetail.DneFVBpy.js → PmProjectDetail.D78gWhqk.js} +1 -1
  51. package/dist/_astro/PmProjectsBoard.Bf8VB3t6.js +1 -0
  52. package/dist/_astro/PmRequirementsBoard.CDyNHN2a.js +1 -0
  53. package/dist/_astro/PmRequirementsBoard.DarfGIrL.js +1 -0
  54. package/dist/_astro/PmShared.TOjBbtV8.js +1 -0
  55. package/dist/_astro/ReportsPage.BXGduuY3.js +1 -0
  56. package/dist/_astro/ReqAnalysisBoard.WLtIfBg8.js +39 -0
  57. package/dist/_astro/RequirementBreakdownLibrary.DMoeTeqX.js +2 -0
  58. package/dist/_astro/RequirementsLibrary.Iv7zOqWe.js +1 -0
  59. package/dist/_astro/SearchableSelect.DXclLMJI.js +1 -0
  60. package/dist/_astro/SelectDropdown.D7IeyHDI.js +1 -0
  61. package/dist/_astro/SettingsDialog.BvDU9EU6.js +1 -0
  62. package/dist/_astro/SlideOverDrawer.CvyB-quk.js +1 -0
  63. package/dist/_astro/SpaceSwitcher.D9bBNQBZ.js +7 -0
  64. package/dist/_astro/{TableFieldConfig.C7O-mL-p.js → TableFieldConfig.DHj25OKN.js} +1 -1
  65. package/dist/_astro/{TagSelect.UUXgkMqJ.js → TagSelect.B_r2p0fp.js} +1 -1
  66. package/dist/_astro/TasksBoard.CXmGdylV.js +1 -0
  67. package/dist/_astro/TemplatesLibrary.DsL2q-wp.js +1 -0
  68. package/dist/_astro/TestReportsLibrary.BBC1TYgP.js +1 -0
  69. package/dist/_astro/TestStatsBoard.VXjP4hu6.js +1 -0
  70. package/dist/_astro/Toast.BydY-X7T.js +1 -0
  71. package/dist/_astro/TodosBoard.-yKgp06M.js +1 -0
  72. package/dist/_astro/UiAutoBoard.BD5ZyLHj.js +3 -0
  73. package/dist/_astro/UiAutoEnvPanel.DrHUOlmH.js +10 -0
  74. package/dist/_astro/UiAutoGenerationDialog.gypq1zP9.js +12 -0
  75. package/dist/_astro/UiAutoProjectsPage.Dhzr09E9.js +3 -0
  76. package/dist/_astro/app-config.BSO5r82v.js +1 -0
  77. package/dist/_astro/app-version.CkDFkv4s.js +1 -0
  78. package/dist/_astro/{bash-tools.D9HCzC_7.js → bash-tools.C4DJiRJs.js} +1 -1
  79. package/dist/_astro/business-map.BiJ85oxm.js +1 -0
  80. package/dist/_astro/business-map.DKwdW1VL.css +1 -0
  81. package/dist/_astro/case-fields.C03NY3zC.js +148 -0
  82. package/dist/_astro/case-gen-run.C4p8uyU3.js +26 -0
  83. package/dist/_astro/case-prompt.ByLTgz4r.js +2 -0
  84. package/dist/_astro/{case-review.TdDs1WFM.js → case-review.BodP2jKm.js} +2 -2
  85. package/dist/_astro/{case-template.Cd2xNbgy.js → case-template.CC2eXegU.js} +2 -2
  86. package/dist/_astro/case-types.BjF1BivA.js +1 -0
  87. package/dist/_astro/case-workflow-contract.BBQ9bbKL.js +1 -0
  88. package/dist/_astro/defect-submit.Dc49lFjm.css +1 -0
  89. package/dist/_astro/defect-workbench.DBmohR-y.js +6 -0
  90. package/dist/_astro/dialog.XgRGNF_Q.js +1 -0
  91. package/dist/_astro/doc-roots.C-20VD0x.js +1 -0
  92. package/dist/_astro/{hydrate-cache.B15Nrpq0.js → hydrate-cache.CMMErx63.js} +1 -1
  93. package/dist/_astro/i18n.CEWjL5GX.js +7 -0
  94. package/dist/_astro/{llm.DN5S1KBK.js → llm.BCYNgppW.js} +1 -1
  95. package/dist/_astro/mcp-tools.Kf-lupGn.js +7 -0
  96. package/dist/_astro/{open-docs-in-session.CRiZT022.js → open-docs-in-session.BoyV6R2A.js} +1 -1
  97. package/dist/_astro/plugins.ChGmBEkF.js +1 -0
  98. package/dist/_astro/project-docs.BRgNjXhG.js +1 -0
  99. package/dist/_astro/prompts-meta.VAjE4OET.js +2 -0
  100. package/dist/_astro/report-prompt.CwHuOcRN.js +1 -0
  101. package/dist/_astro/req-analysis.BLvcktvQ.js +31 -0
  102. package/dist/_astro/requirement-breakdown-library.l0rPd7b3.js +1 -0
  103. package/dist/_astro/{settings.38J2uD5M.js → settings.BojKNwUC.js} +2 -2
  104. package/dist/_astro/space-skills.CPucW8zt.js +1 -0
  105. package/dist/_astro/{spaces-client.DV04tRyD.js → spaces-client.B28rzu8J.js} +1 -1
  106. package/dist/_astro/story-review.dEBWTeOW.js +7 -0
  107. package/dist/_astro/test-points.BuNgdNcU.js +8 -0
  108. package/dist/_astro/test-reports.0gr6-c_0.js +19 -0
  109. package/dist/_astro/ui-auto-prompt.BmO3ja2m.js +8 -0
  110. package/dist/_astro/ui-auto.LUcREUMe.js +1 -0
  111. package/dist/_astro/{view-prefs.DhdLZjGh.js → view-prefs.CWc5cQ2q.js} +1 -1
  112. package/dist/business-map/index.html +988 -0
  113. package/dist/case-exec/index.html +15 -7
  114. package/dist/{prompts → case-execution}/index.html +17 -9
  115. package/dist/cases/api/index.html +988 -0
  116. package/dist/cases/automation/index.html +988 -0
  117. package/dist/cases/functional/index.html +988 -0
  118. package/dist/cases/generate/index.html +14 -6
  119. package/dist/cases/index.html +1 -1
  120. package/dist/cases/library/index.html +1 -980
  121. package/dist/cases/other/index.html +988 -0
  122. package/dist/cases/performance/index.html +988 -0
  123. package/dist/dashboard/index.html +15 -7
  124. package/dist/defect-rules/index.html +695 -0
  125. package/dist/defect-submit/index.html +695 -0
  126. package/dist/defect-submit/templates/index.html +695 -0
  127. package/dist/docs/changelog/index.html +289 -52
  128. package/dist/docs/install/index.html +50 -12
  129. package/dist/docs/manual/index.html +230 -82
  130. package/dist/knowledge/index.html +15 -7
  131. package/dist/life/kr-tasks/index.html +15 -7
  132. package/dist/life/kr-todos/index.html +15 -7
  133. package/dist/life/objectives/index.html +15 -7
  134. package/dist/life/reviews/index.html +15 -7
  135. package/dist/life/stages/index.html +15 -7
  136. package/dist/life/year-plans/index.html +15 -7
  137. package/dist/plugins/index.html +15 -7
  138. package/dist/pm/bugs/index.html +15 -7
  139. package/dist/pm/iterations/index.html +15 -7
  140. package/dist/pm/projects/detail/index.html +15 -7
  141. package/dist/pm/projects/index.html +15 -7
  142. package/dist/pm/requirements/index.html +15 -7
  143. package/dist/reports/index.html +15 -7
  144. package/dist/requirement-analysis/index.html +15 -7
  145. package/dist/requirement-breakdowns/index.html +988 -0
  146. package/dist/requirements/index.html +15 -7
  147. package/dist/tasks/index.html +15 -7
  148. package/dist/templates/index.html +15 -7
  149. package/dist/test-automation/index.html +15 -7
  150. package/dist/test-automation/projects/index.html +15 -7
  151. package/dist/test-reports/index.html +15 -7
  152. package/dist/test-stats/index.html +15 -7
  153. package/dist/todos/index.html +15 -7
  154. package/dist/workbench/index.html +14 -6
  155. package/docs/Codex-MCP/351/233/206/346/210/220.md +340 -0
  156. package/docs//344/270/232/345/212/241/345/234/260/345/233/276/350/257/204/344/274/260.md +39 -0
  157. 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
  158. package/docs//344/274/232/350/257/235/346/217/222/344/273/266.md +28 -3
  159. package/docs//345/205/250/345/261/200/346/217/222/344/273/266.md +41 -1
  160. package/docs//345/256/211/350/243/205/346/211/213/345/206/214.md +65 -17
  161. 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
  162. package/docs//346/223/215/344/275/234/346/211/213/345/206/214.md +219 -74
  163. package/lh-plugins/business-map/assistant.mjs +958 -0
  164. package/lh-plugins/business-map/auth-paths.mjs +61 -0
  165. package/lh-plugins/business-map/jobs.mjs +749 -0
  166. package/lh-plugins/business-map/login.mjs +295 -0
  167. package/lh-plugins/business-map/sources.mjs +1014 -0
  168. package/lh-plugins/case-automation/service.mjs +921 -0
  169. package/lh-plugins/case-automation/store.mjs +231 -0
  170. package/lh-plugins/case-automation/tables.mjs +115 -0
  171. package/lh-plugins/case-automation-prompt.mjs +57 -0
  172. package/lh-plugins/case-execution/assets.mjs +180 -0
  173. package/lh-plugins/case-execution/files.mjs +829 -0
  174. package/lh-plugins/case-execution/plans.mjs +318 -0
  175. package/lh-plugins/case-execution/service.mjs +1026 -0
  176. package/lh-plugins/case-execution-prompt.mjs +36 -0
  177. package/lh-plugins/lh-business-map.mjs +2431 -0
  178. package/lh-plugins/lh-case-automation.mjs +169 -0
  179. package/lh-plugins/lh-case-design.mjs +456 -0
  180. package/lh-plugins/lh-case-execution.mjs +356 -0
  181. package/lh-plugins/lh-case-review.mjs +29 -11
  182. package/lh-plugins/lh-config.mjs +13 -0
  183. package/lh-plugins/lh-defect-submit.mjs +989 -0
  184. package/lh-plugins/lh-project-docs.mjs +55 -140
  185. package/lh-plugins/lh-reports.mjs +29 -57
  186. package/lh-plugins/lh-req-analysis.mjs +32 -3
  187. package/lh-plugins/lh-req-breakdown-library.mjs +1084 -0
  188. package/lh-plugins/lh-uiauto.mjs +625 -24
  189. package/lh-plugins/plugin-prompts.mjs +20 -15
  190. package/lh-plugins/req-analysis-revision.mjs +24 -0
  191. package/lh-plugins/test-points-prompt.mjs +2 -3
  192. package/lh-plugins/uiauto-adapters/playwright.mjs +44 -4
  193. package/lh-plugins/uiauto-adapters/shared.mjs +4 -0
  194. package/lh-plugins/uiauto-auth-setup.mjs +218 -0
  195. package/lh-plugins/uiauto-explorer/apps.mjs +13 -2
  196. package/lh-plugins/uiauto-explorer/emit-files.mjs +16 -2
  197. package/lh-plugins/uiauto-explorer/ir.mjs +3 -0
  198. package/lh-plugins/uiauto-explorer/merge.mjs +37 -1
  199. package/lh-plugins/uiauto-explorer/navigation-guard.mjs +255 -0
  200. package/lh-plugins/uiauto-explorer/runner.mjs +80 -5
  201. package/lh-plugins/uiauto-explorer/sessions.mjs +334 -18
  202. package/lh-plugins/uiauto-node.mjs +102 -11
  203. package/lh-plugins/uiauto-prompt.mjs +20 -15
  204. package/lh-plugins/uiauto-sso.mjs +407 -0
  205. package/package.json +5 -2
  206. package/skills/case-automation-bridge/SKILL.md +27 -0
  207. package/skills/case-execution/SKILL.md +41 -0
  208. package/skills/lantern-defect-submit/SKILL.md +15 -0
  209. package/skills/lantern-workflow/SKILL.md +95 -0
  210. package/skills/office/SKILL.md +26 -0
  211. package/dist/_astro/Base.Bkt1lrgu.css +0 -1
  212. package/dist/_astro/Base.astro_astro_type_script_index_0_lang.UKTVQqk7.js +0 -2
  213. package/dist/_astro/CaseExecBoard.DICluCbK.js +0 -1
  214. package/dist/_astro/CaseGenerateApp.Be3h5XPG.js +0 -10
  215. package/dist/_astro/CasesLibrary.Buys7Cy-.js +0 -21
  216. package/dist/_astro/ChatApp.D3BheKci.js +0 -227
  217. package/dist/_astro/DashboardBoard.Qvf8o4v1.js +0 -5
  218. package/dist/_astro/DocsFileBrowser.DwKBffbM.js +0 -1
  219. package/dist/_astro/KnowledgeLibrary.MDpMqeOd.js +0 -1
  220. package/dist/_astro/LifeKrTasksBoard.DsvW_S66.js +0 -1
  221. package/dist/_astro/LifeKrTodosBoard.D2KTqvg1.js +0 -1
  222. package/dist/_astro/LifeObjectivesBoard.B0cDK934.js +0 -1
  223. package/dist/_astro/LifePlanTypeSelect.BELDvMWU.js +0 -1
  224. package/dist/_astro/LifeStagesBoard.CZftLR9K.js +0 -1
  225. package/dist/_astro/LifeYearCycleSelect.Bw2eRbSm.js +0 -1
  226. package/dist/_astro/LifeYearPlansBoard.DB-RJFEl.js +0 -1
  227. package/dist/_astro/MultiSelectDropdown.CBC7aNOv.js +0 -1
  228. package/dist/_astro/NavLibraries.DdQYsZbe.js +0 -1
  229. package/dist/_astro/OverflowHint.CHZeFfUj.js +0 -1
  230. package/dist/_astro/PluginsPage.C1JnInH4.js +0 -17
  231. package/dist/_astro/PluginsPage.C3eEwy3K.js +0 -1
  232. package/dist/_astro/PmBugsBoard.CGpXUTPu.js +0 -1
  233. package/dist/_astro/PmIterationsBoard.BaKiFIam.js +0 -1
  234. package/dist/_astro/PmProjectsBoard.BxoMF9gK.js +0 -1
  235. package/dist/_astro/PmRequirementsBoard.-SI8VZXI.js +0 -1
  236. package/dist/_astro/PmRequirementsBoard.DsK6RF7Q.js +0 -1
  237. package/dist/_astro/PmShared.BK8kWRSV.js +0 -1
  238. package/dist/_astro/PromptMountDialog.mrTx3U5T.js +0 -1
  239. package/dist/_astro/PromptsLibrary.B_SUf47p.js +0 -1
  240. package/dist/_astro/ReportsPage.B9lGg1_s.js +0 -16
  241. package/dist/_astro/ReqAnalysisBoard.CIZKnCQH.js +0 -39
  242. package/dist/_astro/RequirementsLibrary.cJc-bnGQ.js +0 -1
  243. package/dist/_astro/SearchableSelect.SyLRS-Jj.js +0 -1
  244. package/dist/_astro/SelectDropdown.T3CKHaLt.js +0 -1
  245. package/dist/_astro/SettingsDialog.DYdtCvLc.js +0 -1
  246. package/dist/_astro/SpaceSwitcher.C47_S1g9.js +0 -7
  247. package/dist/_astro/TasksBoard.BUEg2BYk.js +0 -1
  248. package/dist/_astro/TemplatesLibrary.BLVIphXO.js +0 -1
  249. package/dist/_astro/TestReportsLibrary.DVq30Jae.js +0 -1
  250. package/dist/_astro/TestStatsBoard.-5BWQ6bI.js +0 -1
  251. package/dist/_astro/Toast.BO2NFckG.js +0 -1
  252. package/dist/_astro/TodosBoard.DI_lTp2e.js +0 -1
  253. package/dist/_astro/UiAutoBoard.ChFdgv7s.js +0 -2
  254. package/dist/_astro/UiAutoEnvPanel.B50q4kYb.js +0 -19
  255. package/dist/_astro/UiAutoGenerationDialog.CLkFAHrm.js +0 -1
  256. package/dist/_astro/UiAutoProjectsPage.DCsDeN5d.js +0 -3
  257. package/dist/_astro/app-config.DkyjuL-S.js +0 -1
  258. package/dist/_astro/app-version.Cy-5oZO2.js +0 -1
  259. package/dist/_astro/case-fields.CmePqYfh.js +0 -148
  260. package/dist/_astro/case-gen-run.BklG1H8k.js +0 -22
  261. package/dist/_astro/case-types.DlFkbZJN.js +0 -1
  262. package/dist/_astro/dialog.DZHZCe_T.js +0 -1
  263. package/dist/_astro/doc-roots.CO3IBiY6.js +0 -1
  264. package/dist/_astro/i18n.D_aOCHSN.js +0 -7
  265. package/dist/_astro/markdown-client.BS19nLL3.js +0 -62
  266. package/dist/_astro/plugins.BRvXWHLe.js +0 -1
  267. package/dist/_astro/project-docs.CCjtiFjB.js +0 -1
  268. package/dist/_astro/prompts-meta.BUGutlsk.js +0 -2
  269. package/dist/_astro/report-prompt.B9C4cv84.js +0 -1
  270. package/dist/_astro/req-analysis.B12_MGMQ.js +0 -31
  271. package/dist/_astro/story-review.D2W5tEOY.js +0 -14
  272. package/dist/_astro/test-reports.CuIjlEyK.js +0 -19
  273. package/dist/_astro/ui-auto-prompt.H9_yhau9.js +0 -9
  274. package/dist/_astro/ui-auto.DMv940x4.js +0 -1
@@ -0,0 +1,758 @@
1
+ /** 空间技能注册与迁移。旧提示词路径保留为兼容别名,canonical 正文只维护一份。 */
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import crypto from 'node:crypto';
5
+ import { resolveContentRoot, spaceLocalFile } from './space-layout.mjs';
6
+ import { validateSkillOutput } from './skill-validation.mjs';
7
+ const hash = (text) => crypto.createHash('sha256').update(text).digest('hex');
8
+ const catalog = {
9
+ '默认提示词.md': [
10
+ 'foundation',
11
+ '空间基础规则',
12
+ '',
13
+ 'foundation',
14
+ '选定空间并加载基础规则;在此基础上按需使用任务技能。',
15
+ ],
16
+ '需求分析提示词.md': [
17
+ 'requirements',
18
+ '需求分析',
19
+ 'reqAnalysis',
20
+ 'analysis',
21
+ '读取需求原文→提取来源条目→拆解用户故事→调用既有覆盖检查→列出待确认项。',
22
+ ],
23
+ '原文结构提示词.md': [
24
+ 'outline',
25
+ '原文结构提取',
26
+ 'reqAnalysis',
27
+ 'analysis',
28
+ '读取原文→保留标题顺序和稳定来源标识→校验节点重复及缺失→输出结构。',
29
+ ],
30
+ '用户故事审查提示词.md': [
31
+ 'story-review',
32
+ '用户故事审查',
33
+ 'storyReview',
34
+ 'analysis',
35
+ '先做既有确定性覆盖检查→分析场景遗漏→预览修复建议→重新校验。',
36
+ ],
37
+ '测试点生成提示词.md': [
38
+ 'test-points',
39
+ '测试点设计',
40
+ 'testPoints',
41
+ 'analysis',
42
+ '读取用户故事和来源→生成五维测试点→分配P0–P3→核对引用→使用既有导出。',
43
+ ],
44
+ '功能测试用例提示词.md': [
45
+ 'functional-cases',
46
+ '功能用例设计',
47
+ 'caseGen',
48
+ 'cases',
49
+ '读取需求、测试点和当前模板→生成用例→校验表头、编号、来源→预览后按原入口保存。',
50
+ ],
51
+ '功能测试用例提示词_按需求合并场景.md': [
52
+ 'functional-cases',
53
+ '功能用例设计',
54
+ 'caseGen',
55
+ 'cases',
56
+ '按需求分组→合并兼容场景→保留异常与未覆盖说明→校验模板、引用与编号。',
57
+ ],
58
+ '接口测试用例提示词.md': [
59
+ 'api-cases',
60
+ '接口用例设计',
61
+ 'caseGen',
62
+ 'cases',
63
+ '读取接口契约→设计鉴权、参数与异常断言→核对模板和契约引用;不自动请求目标系统。',
64
+ ],
65
+ '性能测试用例提示词.md': [
66
+ 'performance-cases',
67
+ '性能用例设计',
68
+ 'caseGen',
69
+ 'cases',
70
+ '读取实际目标与环境→设计负载和指标→核对单位及阈值来源;缺失项列待确认,不直接压测。',
71
+ ],
72
+ '自动化测试用例提示词.md': [
73
+ 'automation-cases',
74
+ '自动化用例设计',
75
+ 'caseGen',
76
+ 'cases',
77
+ '读取需求与模板→判断可自动化范围→设计步骤断言→核对覆盖与来源。',
78
+ ],
79
+ '功能用例转UI自动化提示词.md': [
80
+ 'case-automation-bridge',
81
+ '功能用例转UI自动化',
82
+ 'uiAuto',
83
+ 'automation',
84
+ '读取选中的功能用例与目标 UI 项目→评估可自动化范围→拆解自动化用例与 Plan→校验可执行性和风险。',
85
+ ],
86
+ '自动化执行提示词.md': [
87
+ 'automation-run',
88
+ '自动化生成与执行',
89
+ 'uiAuto',
90
+ 'automation',
91
+ '读取项目环境→通过既有探索器规划与取证→生成代码→检查→按原权限执行→归档报告。',
92
+ ],
93
+ 'Playwright提示词.md': [
94
+ 'automation-run',
95
+ '自动化生成与执行',
96
+ 'uiAuto',
97
+ 'automation',
98
+ '旧版变体:保留原文与绑定,使用既有自动化执行器;不得自动替换其他变体。',
99
+ ],
100
+ '自动化项目提示词.md': [
101
+ 'automation-project',
102
+ '自动化项目创建',
103
+ 'uiAuto',
104
+ 'automation',
105
+ '读取技术栈→体检现有骨架→补配置→预览差异→按既有授权创建及检查环境。',
106
+ ],
107
+ '用例质量评审提示词.md': [
108
+ 'case-review',
109
+ '用例质量评审',
110
+ 'caseReview',
111
+ 'review',
112
+ '解析已有用例→逐条评审→核对评分范围及统计分母→分级→输出建议和既有报告。',
113
+ ],
114
+ '场景遗漏补全提示词.md': [
115
+ 'case-gaps',
116
+ '用例场景补全',
117
+ 'caseBlindspot',
118
+ 'review',
119
+ '读取需求与已有用例→识别缺口→去重→预览→按既有备份机制补写→校验表头和来源。',
120
+ ],
121
+ '用例执行提示词.md': [
122
+ 'case-execution',
123
+ '用例执行',
124
+ 'caseExecution',
125
+ 'execution',
126
+ '读取单条用例→检查登录和环境→按步骤操作浏览器→观察实际结果→截图取证→给出通过/不通过/阻塞/跳过;由用户验证后手动选择执行结果,不自动判定通过或建缺陷。',
127
+ ],
128
+ '功能测试报告提示词.md': [
129
+ 'test-report',
130
+ '测试报告整理',
131
+ 'testReports',
132
+ 'report',
133
+ '读取真实功能执行记录与模板→汇总统计→检查数量和章节→补缺段;不得虚构执行结果。',
134
+ ],
135
+ '接口测试报告提示词.md': [
136
+ 'test-report',
137
+ '测试报告整理',
138
+ 'testReports',
139
+ 'report',
140
+ '读取接口执行记录→汇总实际统计→核对失败事实→保留模板章节并整理报告。',
141
+ ],
142
+ '性能测试报告提示词.md': [
143
+ 'test-report',
144
+ '测试报告整理',
145
+ 'testReports',
146
+ 'report',
147
+ '读取真实压测结果→核对单位、分位数和阈值→检查统计→生成结论;数据不足列缺项。',
148
+ ],
149
+ '工作汇报提示词.md': [
150
+ 'work-report',
151
+ '工作汇报',
152
+ 'reports',
153
+ 'report',
154
+ '指定周期→读取既有本期工作数据→沿用区间和基线口径→生成汇报→按原规则落盘。',
155
+ ],
156
+ '业务地图识别提示词.md': [
157
+ 'business-map',
158
+ '业务地图识别',
159
+ 'businessMap',
160
+ 'analysis',
161
+ '选择单个、多个或全量→既有后台只读采集→分批分析→验证证据→低风险草稿或待审核。',
162
+ ],
163
+ };
164
+ export const SPACE_SKILL_CATALOG = catalog;
165
+ const root = (space) => resolveContentRoot(space, '技能管理').abs;
166
+ const prompts = (space) => resolveContentRoot(space, '提示词管理').abs;
167
+ const indexFile = (space) => path.join(root(space), 'index.json');
168
+ const RULE_RESOURCE = 'references/business-rules.md';
169
+ const LEGACY_RULE_RESOURCE = 'prompts/instructions.md';
170
+ const LEGACY_MANIFEST = 'skill.json';
171
+ function readIndex(space) {
172
+ const file = indexFile(space);
173
+ if (!fs.existsSync(file)) return { version: 1, revision: 0, skills: [] };
174
+ return JSON.parse(fs.readFileSync(file, 'utf8'));
175
+ }
176
+ function atomic(file, content) {
177
+ fs.mkdirSync(path.dirname(file), { recursive: true });
178
+ const tmp = `${file}.${crypto.randomBytes(6).toString('hex')}.tmp`;
179
+ fs.writeFileSync(tmp, content);
180
+ fs.renameSync(tmp, file);
181
+ }
182
+ function saveIndex(space, index) {
183
+ index.revision++;
184
+ atomic(indexFile(space), JSON.stringify(index, null, 2) + '\n');
185
+ }
186
+ /**
187
+ * 旧索引把插件绑定写成单个 owner。v2 改为 pluginRefs;owner 只保留兼容投影。
188
+ * 返回是否发生了结构升级,供迁移流程决定是否落盘。
189
+ */
190
+ function normalizeEntry(entry) {
191
+ const refs = Array.isArray(entry.pluginRefs)
192
+ ? [...new Set(entry.pluginRefs.map(String).filter(Boolean))]
193
+ : entry.owner
194
+ ? [String(entry.owner)]
195
+ : [];
196
+ const source =
197
+ entry.source ||
198
+ (refs.length
199
+ ? 'bundled'
200
+ : entry.migrated
201
+ ? 'prompt'
202
+ : entry.kind === 'custom'
203
+ ? 'custom'
204
+ : 'bundled');
205
+ const system =
206
+ typeof entry.system === 'boolean'
207
+ ? entry.system
208
+ : Boolean(refs.length || entry.kind === 'foundation');
209
+ const mandatory = refs.length > 0 || entry.kind === 'foundation';
210
+ const enabled = mandatory ? true : entry.enabled !== false;
211
+ const owner = entry.owner || refs[0] || '';
212
+ const info = catalog[path.basename(entry.legacyPath || '')];
213
+ const description =
214
+ info && entry.description?.includes('与对应业务入口共用当前空间的规则')
215
+ ? describeSkill(info)
216
+ : entry.description;
217
+ const changed =
218
+ owner !== entry.owner ||
219
+ JSON.stringify(refs) !== JSON.stringify(entry.pluginRefs) ||
220
+ source !== entry.source ||
221
+ system !== entry.system ||
222
+ enabled !== entry.enabled ||
223
+ description !== entry.description;
224
+ Object.assign(entry, { owner, pluginRefs: refs, source, system, enabled, description });
225
+ return changed;
226
+ }
227
+ function mustStayEnabled(entry) {
228
+ return (
229
+ entry.kind === 'foundation' || (Array.isArray(entry.pluginRefs) && entry.pluginRefs.length > 0)
230
+ );
231
+ }
232
+ function fail(message, statusCode = 400) {
233
+ throw Object.assign(new Error(message), { statusCode });
234
+ }
235
+ function inside(base, relative) {
236
+ const dest = path.resolve(base, relative);
237
+ if (!dest.startsWith(path.resolve(base) + path.sep)) fail('资源路径越界');
238
+ // 不跟随技能资源或根目录中的符号链接。
239
+ for (let p = dest; p !== path.dirname(p); p = path.dirname(p)) {
240
+ if (fs.existsSync(p) && fs.lstatSync(p).isSymbolicLink()) fail('技能路径不允许符号链接');
241
+ if (p === path.resolve(base)) break;
242
+ }
243
+ return dest;
244
+ }
245
+ function entryDir(space, id) {
246
+ if (!/^[a-z0-9-]+$/.test(id)) fail('技能 ID 无效');
247
+ return inside(root(space), id);
248
+ }
249
+ function bodyFile(space, entry) {
250
+ return inside(entryDir(space, entry.id), RULE_RESOURCE);
251
+ }
252
+ function skillFile(space, entry) {
253
+ return inside(entryDir(space, entry.id), 'SKILL.md');
254
+ }
255
+ function removeLegacyManifest(space, entry) {
256
+ const file = inside(entryDir(space, entry.id), LEGACY_MANIFEST);
257
+ try {
258
+ if (fs.existsSync(file)) {
259
+ fs.unlinkSync(file);
260
+ return true;
261
+ }
262
+ } catch {
263
+ // 旧清单只是缓存;删不掉不阻断标准格式读取。
264
+ }
265
+ return false;
266
+ }
267
+ function instructions(entry) {
268
+ return `---\nname: ${entry.id}\ndescription: ${JSON.stringify(entry.description)}\n---\n\n# ${entry.name}\n\n## 适用范围\n${entry.description}\n\n## 流程\n${entry.workflow}\n\n## 输入与输出\n使用本轮实际材料与选定模板;缺少材料时列出缺项。按原业务入口的结构输出,保存与执行沿用原权限。\n\n## 指令\n读取 [业务规则](references/business-rules.md),保留当前变体的业务口径。\n\n## 校验\n${entry.kind === 'foundation' ? '作为当前空间基础规则常驻加载,不额外执行任务。' : '可调用 skill_validate 检查输出结构、模板列、编号、来源、枚举与统计。实际业务执行使用已关联的插件入口;加载技能本身不会执行脚本或写入数据。'}\n`;
269
+ }
270
+ function describeSkill(info) {
271
+ return info[4].replaceAll('→', ';');
272
+ }
273
+ function makeEntry(relative) {
274
+ const name = path.basename(relative),
275
+ info = catalog[name];
276
+ const id = (info?.[0] ?? 'custom') + '-' + hash(relative).slice(0, 10);
277
+ const pluginRefs = info?.[2] ? [info[2]] : [];
278
+ return {
279
+ id,
280
+ group: info?.[0] ?? id,
281
+ name: info?.[1] ?? name.replace(/\.md$/i, ''),
282
+ variant: name.replace(/\.md$/i, ''),
283
+ legacyPath: relative,
284
+ owner: info?.[2] ?? '',
285
+ pluginRefs,
286
+ source: info ? 'bundled' : 'prompt',
287
+ system: Boolean(pluginRefs.length || info?.[0] === 'foundation'),
288
+ kind: info?.[3] ?? 'custom',
289
+ description: info
290
+ ? describeSkill(info)
291
+ : '用户维护的独立能力包;按正文和流程执行,不自动推断工具权限。',
292
+ workflow: info?.[4] ?? '读取本轮输入→按业务规则分析→检查缺项和输出→按用户授权使用工具。',
293
+ enabled: true,
294
+ version: 1,
295
+ updatedAt: new Date().toISOString(),
296
+ };
297
+ }
298
+ function walk(base, relative = '') {
299
+ if (!fs.existsSync(base)) return [];
300
+ return fs.readdirSync(base, { withFileTypes: true }).flatMap((item) => {
301
+ const rel = relative ? `${relative}/${item.name}` : item.name;
302
+ if (item.isSymbolicLink() || item.name.startsWith('.')) return [];
303
+ return item.isDirectory()
304
+ ? walk(path.join(base, item.name), rel)
305
+ : item.isFile() && /\.md$/i.test(item.name)
306
+ ? [rel]
307
+ : [];
308
+ });
309
+ }
310
+
311
+ /** 把旧入口文案收敛到技能页;不改业务规则本身。 */
312
+ function migrateSkillRuleWording(text) {
313
+ return text
314
+ .replaceAll('<!-- 仅供参考:运行时与日常维护以空间「提示词管理」下同名文件为准。 -->\n', '')
315
+ .replace(/^> 插件开启期间不能删除本文件;关闭插件后可以删掉,再次打开会重新生成。\r?\n/gm, '')
316
+ .replace(/^> 这份提示词由(.+?)插件生成并引用(.*)$/gm, '> 由$1插件关联$2')
317
+ .replaceAll(
318
+ '日常增删改请只改本空间「提示词管理」里的本文件。',
319
+ '日常增删改请到本空间「插件仓库 → 技能」维护。',
320
+ )
321
+ .replaceAll('(请在提示词管理中维护:', '(请在关联技能中维护:')
322
+ .replaceAll('提示词管理中', '关联技能中')
323
+ .replaceAll('「提示词管理」', '「插件仓库 → 技能」');
324
+ }
325
+
326
+ /** 旧 `prompts/instructions.md` → 标准 `references/business-rules.md`。 */
327
+ function migrateLegacySkillLayout(space, entry) {
328
+ const dir = entryDir(space, entry.id);
329
+ const legacy = inside(dir, LEGACY_RULE_RESOURCE);
330
+ const target = bodyFile(space, entry);
331
+ let changed = false;
332
+ if (!entry.sourceHash && entry.legacyPath) {
333
+ const source = inside(prompts(space), entry.legacyPath);
334
+ try {
335
+ if (fs.existsSync(source)) {
336
+ entry.sourceHash = hash(fs.readFileSync(source, 'utf8'));
337
+ changed = true;
338
+ }
339
+ } catch {
340
+ // 旧原文件缺失不影响标准资源迁移。
341
+ }
342
+ }
343
+ if (fs.existsSync(legacy)) {
344
+ fs.mkdirSync(path.dirname(target), { recursive: true });
345
+ if (!fs.existsSync(target)) {
346
+ fs.renameSync(legacy, target);
347
+ } else if (hash(fs.readFileSync(legacy, 'utf8')) !== hash(fs.readFileSync(target, 'utf8'))) {
348
+ const backup = spaceLocalFile(space, `skills/migrations/legacy-${entry.id}-${Date.now()}.md`);
349
+ atomic(backup, fs.readFileSync(legacy, 'utf8'));
350
+ fs.unlinkSync(legacy);
351
+ } else {
352
+ fs.unlinkSync(legacy);
353
+ }
354
+ const legacyDir = inside(dir, 'prompts');
355
+ try {
356
+ if (fs.existsSync(legacyDir) && fs.readdirSync(legacyDir).length === 0) {
357
+ fs.rmdirSync(legacyDir);
358
+ }
359
+ } catch {
360
+ // 保留非空旧目录,不影响标准入口。
361
+ }
362
+ changed = true;
363
+ }
364
+ if (!fs.existsSync(target)) {
365
+ atomic(target, '');
366
+ }
367
+ const rules = fs.readFileSync(target, 'utf8');
368
+ const cleanedRules = migrateSkillRuleWording(rules);
369
+ if (cleanedRules !== rules) {
370
+ atomic(target, cleanedRules);
371
+ if (entry.originalHash === hash(rules)) {
372
+ entry.sourceHash ||= entry.originalHash;
373
+ entry.originalHash = hash(cleanedRules);
374
+ }
375
+ changed = true;
376
+ }
377
+ const skillPath = skillFile(space, entry);
378
+ if (!fs.existsSync(skillPath)) {
379
+ atomic(skillPath, instructions(entry));
380
+ changed = true;
381
+ } else {
382
+ const current = fs.readFileSync(skillPath, 'utf8');
383
+ const next = current.replaceAll(LEGACY_RULE_RESOURCE, RULE_RESOURCE);
384
+ if (next !== current) {
385
+ atomic(skillPath, next);
386
+ changed = true;
387
+ }
388
+ }
389
+ if (removeLegacyManifest(space, entry)) changed = true;
390
+ return changed;
391
+ }
392
+
393
+ /** 迁移幂等;保留旧文件作为原始备份。注册表只有在该技能的资源全部落盘后更新。 */
394
+ export function migrateSpaceSkills(space) {
395
+ const index = readIndex(space),
396
+ added = [];
397
+ let upgraded = false;
398
+ if (index.version !== 3) {
399
+ index.version = 3;
400
+ upgraded = true;
401
+ }
402
+ for (const entry of index.skills) {
403
+ if (normalizeEntry(entry)) upgraded = true;
404
+ if (migrateLegacySkillLayout(space, entry)) upgraded = true;
405
+ if (entry.migrated == null) {
406
+ entry.migrated = Boolean(entry.legacyPath);
407
+ upgraded = true;
408
+ }
409
+ }
410
+ if (upgraded) saveIndex(space, index);
411
+ for (const relative of walk(prompts(space))) {
412
+ if (index.skills.some((x) => x.legacyPath === relative)) continue;
413
+ const entry = makeEntry(relative),
414
+ sourceContent = fs.readFileSync(inside(prompts(space), relative), 'utf8'),
415
+ content = migrateSkillRuleWording(sourceContent);
416
+ entry.sourceHash = hash(sourceContent);
417
+ entry.originalHash = hash(content);
418
+ entry.migrated = true;
419
+ const dest = bodyFile(space, entry);
420
+ // 上次中断留下的正文必须与原文一致;不得覆盖未知修改。
421
+ if (fs.existsSync(dest) && hash(fs.readFileSync(dest, 'utf8')) !== entry.originalHash)
422
+ fail(`迁移冲突:${relative}`, 409);
423
+ atomic(dest, content);
424
+ atomic(skillFile(space, entry), instructions(entry));
425
+ if (entry.kind !== 'foundation') {
426
+ const validator = fs.readFileSync(new URL('./skill-validation.mjs', import.meta.url), 'utf8');
427
+ atomic(
428
+ inside(entryDir(space, entry.id), 'scripts/validate.mjs'),
429
+ validator +
430
+ `\nif (process.argv[1] && (await import('node:fs')).realpathSync((await import('node:url')).fileURLToPath(import.meta.url)) === (await import('node:fs')).realpathSync(process.argv[1])) {\n const input = JSON.parse((await import('node:fs')).readFileSync(0,'utf8')); const result=validateSkillOutput(input); console.log(JSON.stringify(result,null,2)); process.exitCode=result.ok?0:1;\n}\n`,
431
+ );
432
+ }
433
+ index.skills.push(entry);
434
+ saveIndex(space, index);
435
+ added.push(entry.id);
436
+ }
437
+ return { added, count: index.skills.length };
438
+ }
439
+ export function migratedPromptPath(space, name) {
440
+ const entry = readIndex(space).skills.find((x) => x.legacyPath === name);
441
+ return entry ? bodyFile(space, entry) : null;
442
+ }
443
+ function pluginEnabled(space, id) {
444
+ const read = (name) => {
445
+ try {
446
+ return JSON.parse(fs.readFileSync(spaceLocalFile(space, name), 'utf8')).plugins ?? {};
447
+ } catch {
448
+ return {};
449
+ }
450
+ };
451
+ const defaults = read('app.defaults.json'),
452
+ config = read('app.config.json');
453
+ return (config[id] ?? defaults[id]) === true;
454
+ }
455
+ export function listSpaceSkills(space) {
456
+ return readIndex(space).skills.map((entry) => {
457
+ normalizeEntry(entry);
458
+ return {
459
+ ...entry,
460
+ scope: 'space',
461
+ key: `space:${entry.id}`,
462
+ path: skillFile(space, entry),
463
+ active: entry.enabled,
464
+ pluginActive: entry.pluginRefs.some((id) => pluginEnabled(space, id)),
465
+ foundation: entry.kind === 'foundation',
466
+ };
467
+ });
468
+ }
469
+ export function getSpaceSkill(space, id) {
470
+ const entry = readIndex(space).skills.find((x) => x.id === String(id).replace(/^space:/, ''));
471
+ if (!entry) fail('技能不存在', 404);
472
+ normalizeEntry(entry);
473
+ const content = fs.readFileSync(bodyFile(space, entry), 'utf8');
474
+ const workflow = fs.readFileSync(skillFile(space, entry), 'utf8');
475
+ return {
476
+ ...entry,
477
+ content,
478
+ instructions: workflow,
479
+ hash: hash(content + '\0' + workflow),
480
+ resources: fs
481
+ .readdirSync(entryDir(space, entry.id), { recursive: true })
482
+ .filter((x) => fs.statSync(inside(entryDir(space, entry.id), x)).isFile())
483
+ .map(String),
484
+ };
485
+ }
486
+ export function updateSpaceSkill(space, id, patch) {
487
+ const index = readIndex(space),
488
+ entry = index.skills.find((x) => x.id === id);
489
+ if (!entry) fail('技能不存在', 404);
490
+ normalizeEntry(entry);
491
+ const current = getSpaceSkill(space, id);
492
+ if (patch.hash !== current.hash) fail('技能已修改,请重新加载', 409);
493
+ if (patch.enabled === false && mustStayEnabled(entry))
494
+ fail(entry.kind === 'foundation' ? '空间基础规则不可停用' : '已关联业务的技能不可停用');
495
+ const content = patch.content ?? current.content,
496
+ workflow = patch.instructions ?? current.instructions;
497
+ if (
498
+ typeof content !== 'string' ||
499
+ typeof workflow !== 'string' ||
500
+ Buffer.byteLength(content + workflow) > 1024 * 1024
501
+ )
502
+ fail('技能正文无效或超过1MB');
503
+ const history = spaceLocalFile(space, `skills/history/${id}/${entry.version}.json`);
504
+ atomic(history, JSON.stringify(current, null, 2));
505
+ atomic(bodyFile(space, entry), content);
506
+ atomic(skillFile(space, entry), workflow);
507
+ entry.enabled = patch.enabled ?? entry.enabled;
508
+ entry.version++;
509
+ entry.updatedAt = new Date().toISOString();
510
+ saveIndex(space, index);
511
+ return getSpaceSkill(space, id);
512
+ }
513
+ export function createSpaceSkill(space, name, content) {
514
+ if (typeof name !== 'string' || !name.trim() || name.length > 120 || /[/\\\0]/.test(name))
515
+ fail('技能名称无效');
516
+ if (typeof content !== 'string' || !content.trim() || Buffer.byteLength(content) > 1024 * 1024)
517
+ fail('请填写技能指令,最多1MB');
518
+ const index = readIndex(space),
519
+ entry = makeEntry(`${name.trim()}.md`);
520
+ entry.id = 'custom-' + crypto.randomBytes(8).toString('hex');
521
+ entry.group = entry.id;
522
+ entry.legacyPath = `${name.trim()}.md`;
523
+ if (
524
+ fs.existsSync(inside(prompts(space), entry.legacyPath)) ||
525
+ index.skills.some((x) => x.legacyPath === entry.legacyPath)
526
+ )
527
+ entry.legacyPath = `${name.trim()}-${entry.id.slice(-8)}.md`;
528
+ entry.originalHash = hash(content);
529
+ entry.migrated = false;
530
+ entry.source = 'custom';
531
+ entry.system = false;
532
+ entry.pluginRefs = [];
533
+ entry.owner = '';
534
+ atomic(inside(prompts(space), entry.legacyPath), content);
535
+ entry.name = name.trim();
536
+ entry.variant = name.trim();
537
+ entry.description = '用户维护的独立能力包;按正文和流程执行,不自动推断工具权限。';
538
+ atomic(bodyFile(space, entry), content);
539
+ atomic(skillFile(space, entry), instructions(entry));
540
+ removeLegacyManifest(space, entry);
541
+ index.skills.push(entry);
542
+ saveIndex(space, index);
543
+ return getSpaceSkill(space, entry.id);
544
+ }
545
+
546
+ /**
547
+ * 确保插件内置技能存在;未修改过的旧种子随代码升级,用户修改保持不动。
548
+ * `originalHash` 表示该技能当前正文对应的最近一次内置种子。
549
+ */
550
+ export function ensureSpaceSkillSeed(space, name, content, owner = '') {
551
+ const rel = String(name ?? '')
552
+ .trim()
553
+ .endsWith('.md')
554
+ ? String(name).trim()
555
+ : `${String(name ?? '').trim()}.md`;
556
+ if (!rel || rel.includes('/') || rel.includes('\\') || !String(content ?? '').trim()) {
557
+ fail('技能种子名称或正文无效');
558
+ }
559
+ migrateSpaceSkills(space);
560
+ const index = readIndex(space);
561
+ let entry = index.skills.find((item) => item.legacyPath === rel);
562
+ const seededHash = hash(content);
563
+ if (!entry) {
564
+ entry = makeEntry(rel);
565
+ if (owner) {
566
+ entry.pluginRefs = [...new Set([...(entry.pluginRefs || []), owner])];
567
+ entry.owner = entry.pluginRefs[0] || owner;
568
+ entry.source = 'bundled';
569
+ entry.system = true;
570
+ }
571
+ entry.originalHash = seededHash;
572
+ atomic(bodyFile(space, entry), content);
573
+ atomic(skillFile(space, entry), instructions(entry));
574
+ if (entry.kind !== 'foundation') {
575
+ const validator = fs.readFileSync(new URL('./skill-validation.mjs', import.meta.url), 'utf8');
576
+ atomic(
577
+ inside(entryDir(space, entry.id), 'scripts/validate.mjs'),
578
+ validator +
579
+ `\nif (process.argv[1] && (await import('node:fs')).realpathSync((await import('node:url')).fileURLToPath(import.meta.url)) === (await import('node:fs')).realpathSync(process.argv[1])) {\n const input = JSON.parse((await import('node:fs')).readFileSync(0,'utf8')); const result=validateSkillOutput(input); console.log(JSON.stringify(result,null,2)); process.exitCode=result.ok?0:1;\n}\n`,
580
+ );
581
+ }
582
+ index.skills.push(entry);
583
+ saveIndex(space, index);
584
+ return { created: true, updated: false, version: entry.version, legacyPath: rel };
585
+ }
586
+
587
+ normalizeEntry(entry);
588
+ const current = fs.readFileSync(bodyFile(space, entry), 'utf8');
589
+ const expectedSeedHash = entry.originalHash || hash(current);
590
+ if (hash(current) !== expectedSeedHash) {
591
+ return { created: false, updated: false, version: entry.version, legacyPath: rel };
592
+ }
593
+ if (current === content) {
594
+ return { created: false, updated: false, version: entry.version, legacyPath: rel };
595
+ }
596
+ atomic(bodyFile(space, entry), content);
597
+ atomic(skillFile(space, entry), instructions(entry));
598
+ entry.originalHash = seededHash;
599
+ entry.version++;
600
+ entry.updatedAt = new Date().toISOString();
601
+ saveIndex(space, index);
602
+ return { created: false, updated: true, version: entry.version, legacyPath: rel };
603
+ }
604
+ export function restoreSpaceSkill(space, id, version, expectedHash) {
605
+ getSpaceSkill(space, id);
606
+ if (!Number.isInteger(version) || version < 1) fail('版本无效');
607
+ const previous = JSON.parse(
608
+ fs.readFileSync(spaceLocalFile(space, `skills/history/${id}/${version}.json`), 'utf8'),
609
+ );
610
+ return updateSpaceSkill(space, id, {
611
+ content: previous.content,
612
+ instructions: previous.instructions,
613
+ hash: expectedHash,
614
+ });
615
+ }
616
+ export function loadSpaceSkill(space, key) {
617
+ const entry = listSpaceSkills(space).find((x) => x.key === key || x.id === key);
618
+ if (!entry) return null;
619
+ if (!entry.active && !entry.foundation) return { ok: false, error: '技能未启用或关联插件已关闭' };
620
+ const skill = getSpaceSkill(space, entry.id),
621
+ text = skill.instructions + '\n\n## 当前变体业务规则\n' + skill.content;
622
+ const buf = Buffer.from(text),
623
+ truncated = buf.length > 65_536;
624
+ return {
625
+ ok: true,
626
+ id: entry.key,
627
+ name: entry.name,
628
+ path: entry.path,
629
+ text: buf.subarray(0, 65_536).toString(),
630
+ bytes: buf.length,
631
+ truncated,
632
+ version: entry.version,
633
+ hash: skill.hash,
634
+ };
635
+ }
636
+ export function syncPluginSkills(space, owner, enabled) {
637
+ migrateSpaceSkills(space);
638
+ return listSpaceSkills(space);
639
+ }
640
+
641
+ /** 一个技能(同一 group)统一启停;内容版本不变,插件关系也不被改写。 */
642
+ export function setSpaceSkillGroupEnabled(space, group, enabled) {
643
+ migrateSpaceSkills(space);
644
+ const index = readIndex(space),
645
+ entries = index.skills.filter((x) => x.group === group);
646
+ if (!entries.length) fail('技能不存在', 404);
647
+ for (const entry of entries) normalizeEntry(entry);
648
+ if (enabled === false && entries.some((entry) => mustStayEnabled(entry)))
649
+ fail(
650
+ entries.some((entry) => entry.kind === 'foundation')
651
+ ? '空间基础规则不可停用'
652
+ : '已关联业务的技能不可停用',
653
+ );
654
+ for (const entry of entries) {
655
+ entry.enabled = enabled === true;
656
+ entry.updatedAt = new Date().toISOString();
657
+ }
658
+ saveIndex(space, index);
659
+ return listSpaceSkills(space);
660
+ }
661
+ export function validateSpaceSkill(space, id, input) {
662
+ getSpaceSkill(space, id);
663
+ return validateSkillOutput(input);
664
+ }
665
+ export function skillHistory(space, id) {
666
+ getSpaceSkill(space, id);
667
+ const dir = spaceLocalFile(space, `skills/history/${id}`);
668
+ return fs.existsSync(dir)
669
+ ? fs
670
+ .readdirSync(dir)
671
+ .filter((x) => /^\d+\.json$/.test(x))
672
+ .map((x) => Number(x.slice(0, -5)))
673
+ .sort((a, b) => b - a)
674
+ : [];
675
+ }
676
+ export function skillFingerprintForPrompt(space, name) {
677
+ const entry = readIndex(space).skills.find((x) => x.legacyPath === name);
678
+ return entry
679
+ ? { skillId: entry.id, version: entry.version, hash: getSpaceSkill(space, entry.id).hash }
680
+ : null;
681
+ }
682
+
683
+ /** 插件种子或显式重置后同步技能版本;正文已由调用方写入 canonical 资源。 */
684
+ export function touchMigratedSkill(space, name) {
685
+ migrateSpaceSkills(space);
686
+ const index = readIndex(space);
687
+ const entry = index.skills.find((x) => x.legacyPath === name);
688
+ if (!entry) return null;
689
+ entry.originalHash = hash(fs.readFileSync(bodyFile(space, entry), 'utf8'));
690
+ entry.version++;
691
+ entry.updatedAt = new Date().toISOString();
692
+ saveIndex(space, index);
693
+ return entry;
694
+ }
695
+
696
+ /** 导入/导出只处理技能资源,不执行导入包脚本。 */
697
+ export function exportSpaceSkill(space, id) {
698
+ const skill = getSpaceSkill(space, id),
699
+ base = entryDir(space, id),
700
+ files = {};
701
+ function collect(dir, rel = '') {
702
+ for (const item of fs.readdirSync(dir, { withFileTypes: true })) {
703
+ if (item.isSymbolicLink()) fail('导出资源不允许符号链接');
704
+ const key = rel ? `${rel}/${item.name}` : item.name;
705
+ if (item.isDirectory()) collect(path.join(dir, item.name), key);
706
+ else if (item.isFile() && key !== LEGACY_MANIFEST)
707
+ files[key] = fs.readFileSync(inside(base, key), 'utf8');
708
+ }
709
+ }
710
+ collect(base);
711
+ if (Buffer.byteLength(JSON.stringify(files)) > 2 * 1024 * 1024) fail('导出包超过2MB');
712
+ return { schemaVersion: 1, name: skill.name, files };
713
+ }
714
+ export function importSpaceSkill(space, bundle) {
715
+ if (bundle?.schemaVersion !== 1 || !bundle.files || typeof bundle.files !== 'object')
716
+ fail('技能包格式无效');
717
+ if (Buffer.byteLength(JSON.stringify(bundle)) > 2 * 1024 * 1024) fail('技能包超过2MB');
718
+ const files = Object.entries(bundle.files);
719
+ if (files.length > 100 || !files.some(([key]) => key === 'SKILL.md'))
720
+ fail('技能包需包含SKILL.md,最多100个资源');
721
+ for (const [key, text] of files)
722
+ if (
723
+ typeof text !== 'string' ||
724
+ key.includes('\\') ||
725
+ key.split('/').some((p) => p === '..' || p === '.' || !p) ||
726
+ path.isAbsolute(key) ||
727
+ !/\.(md|json|mjs|txt|csv)$/i.test(key)
728
+ )
729
+ fail('技能资源路径或格式无效');
730
+ const ruleText =
731
+ bundle.files[RULE_RESOURCE] || bundle.files[LEGACY_RULE_RESOURCE] || bundle.files['SKILL.md'];
732
+ const result = createSpaceSkill(space, bundle.name || '导入技能', ruleText);
733
+ for (const [key, text] of files) {
734
+ if (key === LEGACY_MANIFEST) continue;
735
+ const target = key === LEGACY_RULE_RESOURCE ? RULE_RESOURCE : key;
736
+ atomic(inside(entryDir(space, result.id), target), text);
737
+ }
738
+ return getSpaceSkill(space, result.id);
739
+ }
740
+ export function rollbackSkillMigration(space) {
741
+ const index = readIndex(space),
742
+ legacy = index.skills.filter((x) => x.migrated);
743
+ for (const entry of legacy) {
744
+ const source = inside(prompts(space), entry.legacyPath);
745
+ const expected = entry.sourceHash || entry.originalHash;
746
+ if (!fs.existsSync(source) || hash(fs.readFileSync(source, 'utf8')) !== expected)
747
+ fail(`原文件发生变动,不能自动回退:${entry.legacyPath}`, 409);
748
+ }
749
+ atomic(
750
+ spaceLocalFile(space, `skills/migrations/rollback-${Date.now()}.json`),
751
+ JSON.stringify(index, null, 2),
752
+ );
753
+ for (const entry of legacy)
754
+ atomic(inside(prompts(space), entry.legacyPath), getSpaceSkill(space, entry.id).content);
755
+ index.skills = index.skills.filter((x) => !x.migrated);
756
+ saveIndex(space, index);
757
+ return { restored: legacy.length };
758
+ }