@kafukatree/dsh-skill-manager 0.0.0-stage → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (307) hide show
  1. package/LICENSE +21 -0
  2. package/LICENSE-lucide.txt +43 -0
  3. package/README.md +44 -2
  4. package/agent-tool.mjs +118 -0
  5. package/client-bridge.mjs +307 -0
  6. package/client-entry.mjs +131 -0
  7. package/client.js +6769 -0
  8. package/control-icon.mjs +40 -0
  9. package/cordis.patch.yml +4 -0
  10. package/design/skill-manager.pen +798 -0
  11. package/design.css +320 -0
  12. package/design.mjs +629 -0
  13. package/dialogs.css +460 -0
  14. package/dialogs.mjs +1130 -0
  15. package/document-link.mjs +13 -0
  16. package/file-actions.mjs +62 -0
  17. package/file-view.mjs +384 -0
  18. package/header-scroll.css +90 -0
  19. package/header-scroll.mjs +121 -0
  20. package/host.mjs +460 -0
  21. package/icon.svg +6 -0
  22. package/import-archive.mjs +41 -0
  23. package/import-commit.mjs +247 -0
  24. package/import-family.mjs +213 -0
  25. package/import-files.mjs +186 -0
  26. package/import-github.mjs +168 -0
  27. package/import-limits.mjs +10 -0
  28. package/import-native.mjs +79 -0
  29. package/imports.mjs +209 -0
  30. package/inventory.mjs +62 -0
  31. package/library.mjs +1334 -0
  32. package/local-families.mjs +277 -0
  33. package/locale/en.json +16 -0
  34. package/locale/zh.json +16 -0
  35. package/management-view.mjs +158 -0
  36. package/mode-view.mjs +72 -0
  37. package/node_modules/pend/LICENSE +23 -0
  38. package/node_modules/pend/README.md +41 -0
  39. package/node_modules/pend/index.js +55 -0
  40. package/node_modules/pend/package.json +18 -0
  41. package/node_modules/pend/test.js +137 -0
  42. package/node_modules/yaml/LICENSE +13 -0
  43. package/node_modules/yaml/README.md +172 -0
  44. package/node_modules/yaml/bin.mjs +11 -0
  45. package/node_modules/yaml/browser/dist/compose/compose-collection.js +88 -0
  46. package/node_modules/yaml/browser/dist/compose/compose-doc.js +43 -0
  47. package/node_modules/yaml/browser/dist/compose/compose-node.js +109 -0
  48. package/node_modules/yaml/browser/dist/compose/compose-scalar.js +86 -0
  49. package/node_modules/yaml/browser/dist/compose/composer.js +219 -0
  50. package/node_modules/yaml/browser/dist/compose/resolve-block-map.js +115 -0
  51. package/node_modules/yaml/browser/dist/compose/resolve-block-scalar.js +198 -0
  52. package/node_modules/yaml/browser/dist/compose/resolve-block-seq.js +49 -0
  53. package/node_modules/yaml/browser/dist/compose/resolve-end.js +37 -0
  54. package/node_modules/yaml/browser/dist/compose/resolve-flow-collection.js +207 -0
  55. package/node_modules/yaml/browser/dist/compose/resolve-flow-scalar.js +226 -0
  56. package/node_modules/yaml/browser/dist/compose/resolve-props.js +146 -0
  57. package/node_modules/yaml/browser/dist/compose/util-contains-newline.js +34 -0
  58. package/node_modules/yaml/browser/dist/compose/util-empty-scalar-position.js +26 -0
  59. package/node_modules/yaml/browser/dist/compose/util-flow-indent-check.js +15 -0
  60. package/node_modules/yaml/browser/dist/compose/util-map-includes.js +13 -0
  61. package/node_modules/yaml/browser/dist/doc/Document.js +335 -0
  62. package/node_modules/yaml/browser/dist/doc/anchors.js +71 -0
  63. package/node_modules/yaml/browser/dist/doc/applyReviver.js +55 -0
  64. package/node_modules/yaml/browser/dist/doc/createNode.js +88 -0
  65. package/node_modules/yaml/browser/dist/doc/directives.js +176 -0
  66. package/node_modules/yaml/browser/dist/errors.js +57 -0
  67. package/node_modules/yaml/browser/dist/index.js +17 -0
  68. package/node_modules/yaml/browser/dist/log.js +11 -0
  69. package/node_modules/yaml/browser/dist/nodes/Alias.js +118 -0
  70. package/node_modules/yaml/browser/dist/nodes/Collection.js +147 -0
  71. package/node_modules/yaml/browser/dist/nodes/Node.js +38 -0
  72. package/node_modules/yaml/browser/dist/nodes/Pair.js +36 -0
  73. package/node_modules/yaml/browser/dist/nodes/Scalar.js +24 -0
  74. package/node_modules/yaml/browser/dist/nodes/YAMLMap.js +144 -0
  75. package/node_modules/yaml/browser/dist/nodes/YAMLSeq.js +113 -0
  76. package/node_modules/yaml/browser/dist/nodes/addPairToJSMap.js +63 -0
  77. package/node_modules/yaml/browser/dist/nodes/identity.js +36 -0
  78. package/node_modules/yaml/browser/dist/nodes/toJS.js +37 -0
  79. package/node_modules/yaml/browser/dist/parse/cst-scalar.js +214 -0
  80. package/node_modules/yaml/browser/dist/parse/cst-stringify.js +61 -0
  81. package/node_modules/yaml/browser/dist/parse/cst-visit.js +97 -0
  82. package/node_modules/yaml/browser/dist/parse/cst.js +98 -0
  83. package/node_modules/yaml/browser/dist/parse/lexer.js +721 -0
  84. package/node_modules/yaml/browser/dist/parse/line-counter.js +39 -0
  85. package/node_modules/yaml/browser/dist/parse/parser.js +975 -0
  86. package/node_modules/yaml/browser/dist/public-api.js +102 -0
  87. package/node_modules/yaml/browser/dist/schema/Schema.js +37 -0
  88. package/node_modules/yaml/browser/dist/schema/common/map.js +17 -0
  89. package/node_modules/yaml/browser/dist/schema/common/null.js +15 -0
  90. package/node_modules/yaml/browser/dist/schema/common/seq.js +17 -0
  91. package/node_modules/yaml/browser/dist/schema/common/string.js +14 -0
  92. package/node_modules/yaml/browser/dist/schema/core/bool.js +19 -0
  93. package/node_modules/yaml/browser/dist/schema/core/float.js +43 -0
  94. package/node_modules/yaml/browser/dist/schema/core/int.js +38 -0
  95. package/node_modules/yaml/browser/dist/schema/core/schema.js +23 -0
  96. package/node_modules/yaml/browser/dist/schema/json/schema.js +62 -0
  97. package/node_modules/yaml/browser/dist/schema/tags.js +96 -0
  98. package/node_modules/yaml/browser/dist/schema/yaml-1.1/binary.js +58 -0
  99. package/node_modules/yaml/browser/dist/schema/yaml-1.1/bool.js +26 -0
  100. package/node_modules/yaml/browser/dist/schema/yaml-1.1/float.js +46 -0
  101. package/node_modules/yaml/browser/dist/schema/yaml-1.1/int.js +71 -0
  102. package/node_modules/yaml/browser/dist/schema/yaml-1.1/merge.js +67 -0
  103. package/node_modules/yaml/browser/dist/schema/yaml-1.1/omap.js +74 -0
  104. package/node_modules/yaml/browser/dist/schema/yaml-1.1/pairs.js +78 -0
  105. package/node_modules/yaml/browser/dist/schema/yaml-1.1/schema.js +39 -0
  106. package/node_modules/yaml/browser/dist/schema/yaml-1.1/set.js +93 -0
  107. package/node_modules/yaml/browser/dist/schema/yaml-1.1/timestamp.js +101 -0
  108. package/node_modules/yaml/browser/dist/stringify/foldFlowLines.js +146 -0
  109. package/node_modules/yaml/browser/dist/stringify/stringify.js +129 -0
  110. package/node_modules/yaml/browser/dist/stringify/stringifyCollection.js +153 -0
  111. package/node_modules/yaml/browser/dist/stringify/stringifyComment.js +20 -0
  112. package/node_modules/yaml/browser/dist/stringify/stringifyDocument.js +85 -0
  113. package/node_modules/yaml/browser/dist/stringify/stringifyNumber.js +25 -0
  114. package/node_modules/yaml/browser/dist/stringify/stringifyPair.js +150 -0
  115. package/node_modules/yaml/browser/dist/stringify/stringifyString.js +336 -0
  116. package/node_modules/yaml/browser/dist/util.js +11 -0
  117. package/node_modules/yaml/browser/dist/visit.js +233 -0
  118. package/node_modules/yaml/browser/index.js +5 -0
  119. package/node_modules/yaml/browser/package.json +3 -0
  120. package/node_modules/yaml/dist/cli.d.ts +8 -0
  121. package/node_modules/yaml/dist/cli.mjs +201 -0
  122. package/node_modules/yaml/dist/compose/compose-collection.d.ts +11 -0
  123. package/node_modules/yaml/dist/compose/compose-collection.js +90 -0
  124. package/node_modules/yaml/dist/compose/compose-doc.d.ts +7 -0
  125. package/node_modules/yaml/dist/compose/compose-doc.js +45 -0
  126. package/node_modules/yaml/dist/compose/compose-node.d.ts +29 -0
  127. package/node_modules/yaml/dist/compose/compose-node.js +112 -0
  128. package/node_modules/yaml/dist/compose/compose-scalar.d.ts +5 -0
  129. package/node_modules/yaml/dist/compose/compose-scalar.js +88 -0
  130. package/node_modules/yaml/dist/compose/composer.d.ts +63 -0
  131. package/node_modules/yaml/dist/compose/composer.js +224 -0
  132. package/node_modules/yaml/dist/compose/resolve-block-map.d.ts +6 -0
  133. package/node_modules/yaml/dist/compose/resolve-block-map.js +117 -0
  134. package/node_modules/yaml/dist/compose/resolve-block-scalar.d.ts +11 -0
  135. package/node_modules/yaml/dist/compose/resolve-block-scalar.js +200 -0
  136. package/node_modules/yaml/dist/compose/resolve-block-seq.d.ts +6 -0
  137. package/node_modules/yaml/dist/compose/resolve-block-seq.js +51 -0
  138. package/node_modules/yaml/dist/compose/resolve-end.d.ts +6 -0
  139. package/node_modules/yaml/dist/compose/resolve-end.js +39 -0
  140. package/node_modules/yaml/dist/compose/resolve-flow-collection.d.ts +7 -0
  141. package/node_modules/yaml/dist/compose/resolve-flow-collection.js +209 -0
  142. package/node_modules/yaml/dist/compose/resolve-flow-scalar.d.ts +10 -0
  143. package/node_modules/yaml/dist/compose/resolve-flow-scalar.js +228 -0
  144. package/node_modules/yaml/dist/compose/resolve-props.d.ts +23 -0
  145. package/node_modules/yaml/dist/compose/resolve-props.js +148 -0
  146. package/node_modules/yaml/dist/compose/util-contains-newline.d.ts +2 -0
  147. package/node_modules/yaml/dist/compose/util-contains-newline.js +36 -0
  148. package/node_modules/yaml/dist/compose/util-empty-scalar-position.d.ts +2 -0
  149. package/node_modules/yaml/dist/compose/util-empty-scalar-position.js +28 -0
  150. package/node_modules/yaml/dist/compose/util-flow-indent-check.d.ts +3 -0
  151. package/node_modules/yaml/dist/compose/util-flow-indent-check.js +17 -0
  152. package/node_modules/yaml/dist/compose/util-map-includes.d.ts +4 -0
  153. package/node_modules/yaml/dist/compose/util-map-includes.js +15 -0
  154. package/node_modules/yaml/dist/doc/Document.d.ts +141 -0
  155. package/node_modules/yaml/dist/doc/Document.js +337 -0
  156. package/node_modules/yaml/dist/doc/anchors.d.ts +24 -0
  157. package/node_modules/yaml/dist/doc/anchors.js +76 -0
  158. package/node_modules/yaml/dist/doc/applyReviver.d.ts +9 -0
  159. package/node_modules/yaml/dist/doc/applyReviver.js +57 -0
  160. package/node_modules/yaml/dist/doc/createNode.d.ts +17 -0
  161. package/node_modules/yaml/dist/doc/createNode.js +90 -0
  162. package/node_modules/yaml/dist/doc/directives.d.ts +49 -0
  163. package/node_modules/yaml/dist/doc/directives.js +178 -0
  164. package/node_modules/yaml/dist/errors.d.ts +21 -0
  165. package/node_modules/yaml/dist/errors.js +62 -0
  166. package/node_modules/yaml/dist/index.d.ts +25 -0
  167. package/node_modules/yaml/dist/index.js +50 -0
  168. package/node_modules/yaml/dist/log.d.ts +3 -0
  169. package/node_modules/yaml/dist/log.js +19 -0
  170. package/node_modules/yaml/dist/nodes/Alias.d.ts +29 -0
  171. package/node_modules/yaml/dist/nodes/Alias.js +120 -0
  172. package/node_modules/yaml/dist/nodes/Collection.d.ts +73 -0
  173. package/node_modules/yaml/dist/nodes/Collection.js +151 -0
  174. package/node_modules/yaml/dist/nodes/Node.d.ts +53 -0
  175. package/node_modules/yaml/dist/nodes/Node.js +40 -0
  176. package/node_modules/yaml/dist/nodes/Pair.d.ts +22 -0
  177. package/node_modules/yaml/dist/nodes/Pair.js +39 -0
  178. package/node_modules/yaml/dist/nodes/Scalar.d.ts +47 -0
  179. package/node_modules/yaml/dist/nodes/Scalar.js +27 -0
  180. package/node_modules/yaml/dist/nodes/YAMLMap.d.ts +53 -0
  181. package/node_modules/yaml/dist/nodes/YAMLMap.js +147 -0
  182. package/node_modules/yaml/dist/nodes/YAMLSeq.d.ts +60 -0
  183. package/node_modules/yaml/dist/nodes/YAMLSeq.js +115 -0
  184. package/node_modules/yaml/dist/nodes/addPairToJSMap.d.ts +4 -0
  185. package/node_modules/yaml/dist/nodes/addPairToJSMap.js +65 -0
  186. package/node_modules/yaml/dist/nodes/identity.d.ts +23 -0
  187. package/node_modules/yaml/dist/nodes/identity.js +53 -0
  188. package/node_modules/yaml/dist/nodes/toJS.d.ts +29 -0
  189. package/node_modules/yaml/dist/nodes/toJS.js +39 -0
  190. package/node_modules/yaml/dist/options.d.ts +350 -0
  191. package/node_modules/yaml/dist/parse/cst-scalar.d.ts +64 -0
  192. package/node_modules/yaml/dist/parse/cst-scalar.js +218 -0
  193. package/node_modules/yaml/dist/parse/cst-stringify.d.ts +8 -0
  194. package/node_modules/yaml/dist/parse/cst-stringify.js +63 -0
  195. package/node_modules/yaml/dist/parse/cst-visit.d.ts +39 -0
  196. package/node_modules/yaml/dist/parse/cst-visit.js +99 -0
  197. package/node_modules/yaml/dist/parse/cst.d.ts +109 -0
  198. package/node_modules/yaml/dist/parse/cst.js +112 -0
  199. package/node_modules/yaml/dist/parse/lexer.d.ts +87 -0
  200. package/node_modules/yaml/dist/parse/lexer.js +723 -0
  201. package/node_modules/yaml/dist/parse/line-counter.d.ts +22 -0
  202. package/node_modules/yaml/dist/parse/line-counter.js +41 -0
  203. package/node_modules/yaml/dist/parse/parser.d.ts +84 -0
  204. package/node_modules/yaml/dist/parse/parser.js +980 -0
  205. package/node_modules/yaml/dist/public-api.d.ts +44 -0
  206. package/node_modules/yaml/dist/public-api.js +107 -0
  207. package/node_modules/yaml/dist/schema/Schema.d.ts +17 -0
  208. package/node_modules/yaml/dist/schema/Schema.js +39 -0
  209. package/node_modules/yaml/dist/schema/common/map.d.ts +2 -0
  210. package/node_modules/yaml/dist/schema/common/map.js +19 -0
  211. package/node_modules/yaml/dist/schema/common/null.d.ts +4 -0
  212. package/node_modules/yaml/dist/schema/common/null.js +17 -0
  213. package/node_modules/yaml/dist/schema/common/seq.d.ts +2 -0
  214. package/node_modules/yaml/dist/schema/common/seq.js +19 -0
  215. package/node_modules/yaml/dist/schema/common/string.d.ts +2 -0
  216. package/node_modules/yaml/dist/schema/common/string.js +16 -0
  217. package/node_modules/yaml/dist/schema/core/bool.d.ts +4 -0
  218. package/node_modules/yaml/dist/schema/core/bool.js +21 -0
  219. package/node_modules/yaml/dist/schema/core/float.d.ts +4 -0
  220. package/node_modules/yaml/dist/schema/core/float.js +47 -0
  221. package/node_modules/yaml/dist/schema/core/int.d.ts +4 -0
  222. package/node_modules/yaml/dist/schema/core/int.js +42 -0
  223. package/node_modules/yaml/dist/schema/core/schema.d.ts +1 -0
  224. package/node_modules/yaml/dist/schema/core/schema.js +25 -0
  225. package/node_modules/yaml/dist/schema/json/schema.d.ts +2 -0
  226. package/node_modules/yaml/dist/schema/json/schema.js +64 -0
  227. package/node_modules/yaml/dist/schema/json-schema.d.ts +69 -0
  228. package/node_modules/yaml/dist/schema/tags.d.ts +48 -0
  229. package/node_modules/yaml/dist/schema/tags.js +99 -0
  230. package/node_modules/yaml/dist/schema/types.d.ts +92 -0
  231. package/node_modules/yaml/dist/schema/yaml-1.1/binary.d.ts +2 -0
  232. package/node_modules/yaml/dist/schema/yaml-1.1/binary.js +70 -0
  233. package/node_modules/yaml/dist/schema/yaml-1.1/bool.d.ts +7 -0
  234. package/node_modules/yaml/dist/schema/yaml-1.1/bool.js +29 -0
  235. package/node_modules/yaml/dist/schema/yaml-1.1/float.d.ts +4 -0
  236. package/node_modules/yaml/dist/schema/yaml-1.1/float.js +50 -0
  237. package/node_modules/yaml/dist/schema/yaml-1.1/int.d.ts +5 -0
  238. package/node_modules/yaml/dist/schema/yaml-1.1/int.js +76 -0
  239. package/node_modules/yaml/dist/schema/yaml-1.1/merge.d.ts +9 -0
  240. package/node_modules/yaml/dist/schema/yaml-1.1/merge.js +71 -0
  241. package/node_modules/yaml/dist/schema/yaml-1.1/omap.d.ts +22 -0
  242. package/node_modules/yaml/dist/schema/yaml-1.1/omap.js +77 -0
  243. package/node_modules/yaml/dist/schema/yaml-1.1/pairs.d.ts +10 -0
  244. package/node_modules/yaml/dist/schema/yaml-1.1/pairs.js +82 -0
  245. package/node_modules/yaml/dist/schema/yaml-1.1/schema.d.ts +1 -0
  246. package/node_modules/yaml/dist/schema/yaml-1.1/schema.js +41 -0
  247. package/node_modules/yaml/dist/schema/yaml-1.1/set.d.ts +28 -0
  248. package/node_modules/yaml/dist/schema/yaml-1.1/set.js +96 -0
  249. package/node_modules/yaml/dist/schema/yaml-1.1/timestamp.d.ts +6 -0
  250. package/node_modules/yaml/dist/schema/yaml-1.1/timestamp.js +105 -0
  251. package/node_modules/yaml/dist/stringify/foldFlowLines.d.ts +34 -0
  252. package/node_modules/yaml/dist/stringify/foldFlowLines.js +151 -0
  253. package/node_modules/yaml/dist/stringify/stringify.d.ts +21 -0
  254. package/node_modules/yaml/dist/stringify/stringify.js +132 -0
  255. package/node_modules/yaml/dist/stringify/stringifyCollection.d.ts +17 -0
  256. package/node_modules/yaml/dist/stringify/stringifyCollection.js +155 -0
  257. package/node_modules/yaml/dist/stringify/stringifyComment.d.ts +10 -0
  258. package/node_modules/yaml/dist/stringify/stringifyComment.js +24 -0
  259. package/node_modules/yaml/dist/stringify/stringifyDocument.d.ts +4 -0
  260. package/node_modules/yaml/dist/stringify/stringifyDocument.js +87 -0
  261. package/node_modules/yaml/dist/stringify/stringifyNumber.d.ts +2 -0
  262. package/node_modules/yaml/dist/stringify/stringifyNumber.js +27 -0
  263. package/node_modules/yaml/dist/stringify/stringifyPair.d.ts +3 -0
  264. package/node_modules/yaml/dist/stringify/stringifyPair.js +152 -0
  265. package/node_modules/yaml/dist/stringify/stringifyString.d.ts +9 -0
  266. package/node_modules/yaml/dist/stringify/stringifyString.js +338 -0
  267. package/node_modules/yaml/dist/test-events.d.ts +4 -0
  268. package/node_modules/yaml/dist/test-events.js +134 -0
  269. package/node_modules/yaml/dist/util.d.ts +16 -0
  270. package/node_modules/yaml/dist/util.js +28 -0
  271. package/node_modules/yaml/dist/visit.d.ts +102 -0
  272. package/node_modules/yaml/dist/visit.js +236 -0
  273. package/node_modules/yaml/package.json +97 -0
  274. package/node_modules/yaml/util.js +2 -0
  275. package/node_modules/yauzl/LICENSE +21 -0
  276. package/node_modules/yauzl/README.md +1015 -0
  277. package/node_modules/yauzl/crc32.js +45 -0
  278. package/node_modules/yauzl/fd-slicer.js +188 -0
  279. package/node_modules/yauzl/index.js +1096 -0
  280. package/node_modules/yauzl/package.json +40 -0
  281. package/package-files.mjs +244 -0
  282. package/package-view.mjs +375 -0
  283. package/package.css +131 -0
  284. package/package.json +75 -4
  285. package/removal.mjs +313 -0
  286. package/scripts/build.mjs +19 -0
  287. package/scripts/export-design.py +587 -0
  288. package/scripts/run-tests.mjs +14 -0
  289. package/skill-state.mjs +136 -0
  290. package/skills/skill-creator/SKILL.md +48 -0
  291. package/styles.mjs +10 -0
  292. package/surfaces.css +593 -0
  293. package/test/agent-tool.test.mjs +153 -0
  294. package/test/client-bridge.test.mjs +240 -0
  295. package/test/distribution.test.mjs +17 -0
  296. package/test/host.test.mjs +598 -0
  297. package/test/imports.test.mjs +491 -0
  298. package/test/inventory.test.mjs +144 -0
  299. package/test/library.test.mjs +785 -0
  300. package/test/local-families.test.mjs +220 -0
  301. package/test/management-view.test.mjs +102 -0
  302. package/test/mode-view.test.mjs +26 -0
  303. package/test/package-files.test.mjs +221 -0
  304. package/toolbar.css +228 -0
  305. package/typography.css +173 -0
  306. package/ui.css +399 -0
  307. package/ui.mjs +1545 -0
package/host.mjs ADDED
@@ -0,0 +1,460 @@
1
+ import { randomUUID } from 'node:crypto'
2
+ import { lstat, mkdir, mkdtemp, realpath, rm, stat, writeFile } from 'node:fs/promises'
3
+ import { homedir, tmpdir } from 'node:os'
4
+ import path from 'node:path'
5
+ import { registerAgentTool } from './agent-tool.mjs'
6
+ import { createLibrary, registerCreatorSkill } from './library.mjs'
7
+ import { managementSelection } from './management-view.mjs'
8
+ import { createImports } from './imports.mjs'
9
+ import { ImportFiles, limits, problem } from './import-files.mjs'
10
+ import { openOptions, openSkillItem } from './file-actions.mjs'
11
+ import { browsePackage, browseSkill, lookupSkillResources, skillResources } from './package-files.mjs'
12
+
13
+ export { Config } from './library.mjs'
14
+ export const name = 'dsh-skill-manager'
15
+ export const inject = ['connection', 'webServer', 'agents', 'workingDirectory', 'workspaceRegistry', 'sessionController', 'skills', 'tools']
16
+
17
+ const channel = '/dsh-skill-manager'
18
+ const lifetimeMs = 15 * 60 * 1000
19
+ const uploadPrefix = 'dsh-skill-manager-upload-'
20
+ const maxPending = 16
21
+
22
+ function text(value, label, max = 4096) {
23
+ if (typeof value !== 'string' || !value.trim() || value.length > max || value.includes('\0')) {
24
+ throw problem('INVALID_INPUT', `请提供有效的${label}。`)
25
+ }
26
+ return value
27
+ }
28
+
29
+ function boolean(value, label) {
30
+ if (typeof value !== 'boolean') throw problem('INVALID_INPUT', `${label}须为布尔值。`)
31
+ return value
32
+ }
33
+
34
+ async function contextOf(ctx, input, signal) {
35
+ const workspaceId = input.workspaceId === undefined ? undefined : text(input.workspaceId, '工作区 ID', 300)
36
+ const workspace = workspaceId && ctx.workspaceRegistry.get(workspaceId)
37
+ if (workspaceId && !workspace) throw problem('WORKSPACE_NOT_FOUND', '所选工作区已不存在。')
38
+ const cwd = workspace ? workspace.path : ctx.workingDirectory.defaultDirectory
39
+ if (typeof cwd !== 'string' || !path.isAbsolute(cwd)) throw problem('WORKSPACE_NOT_FOUND', '主机无法确定当前工作目录。')
40
+ const presetId = input.presetId == null ? null : text(input.presetId, '模式 ID', 300)
41
+ const agent = await managementSelection(ctx.get?.('agentPresets'), presetId, signal)
42
+ return { cwd, workspaceId, presetId, agent }
43
+ }
44
+
45
+ async function writableTarget(library, context, id) {
46
+ const targets = await library.targets(context.cwd, context.agent)
47
+ const target = targets.find(row => row.id === id)
48
+ if (target?.writable !== true || !path.isAbsolute(target.path)) throw problem('INVALID_TARGET', '请选择当前技能库中的可写目录。')
49
+ return target
50
+ }
51
+
52
+ function decodeUpload(files) {
53
+ if (!Array.isArray(files) || !files.length || files.length > limits.entries) throw problem('SIZE_LIMIT', '每次可上传 1 至 1024 个文件。')
54
+ const tree = new ImportFiles()
55
+ const entries = []
56
+ let total = 0
57
+ for (const file of files) {
58
+ const relative = text(file?.path, '文件名', 2048)
59
+ const data = file?.base64
60
+ const archive = files.length === 1 && !relative.includes('/') && /\.(zip|skill)$/i.test(relative)
61
+ const maxFileBytes = archive ? limits.archive : limits.file
62
+ if (typeof data !== 'string' || data.length > 4 * Math.ceil(maxFileBytes / 3)) {
63
+ throw problem('SIZE_LIMIT', '单个文件上限为 16 MiB,单独上传的压缩包上限为 32 MiB。')
64
+ }
65
+ const bytes = Buffer.from(data, 'base64')
66
+ if (data !== bytes.toString('base64')) throw problem('INVALID_UPLOAD', '上传内容不是有效的 Base64 数据。')
67
+ total += bytes.length
68
+ if (bytes.length > maxFileBytes || total > limits.bundle) throw problem('SIZE_LIMIT', '上传内容超过单个文件上限或 64 MiB 总量上限。')
69
+ // A lone archive has its own compressed-byte limit. Its entries are checked again during expansion.
70
+ tree.reserve(relative, false, archive ? 0 : bytes.length)
71
+ entries.push({ relative, bytes })
72
+ }
73
+ const root = entries[0].relative.split('/')[0]
74
+ if (entries.some(entry => entry.relative.split('/')[0] !== root)) throw problem('INVALID_UPLOAD', '每次请选择一个技能文件或一个完整的技能目录。')
75
+ return { root, entries }
76
+ }
77
+
78
+ async function removeStagedDirectory(directory, identity) {
79
+ let current
80
+ try { current = await lstat(directory) } catch (error) {
81
+ if (error.code === 'ENOENT') return
82
+ throw error
83
+ }
84
+ if (current.isDirectory() && current.dev === identity.dev && current.ino === identity.ino) {
85
+ await rm(directory, { recursive: true, force: true })
86
+ }
87
+ }
88
+
89
+ async function stageUpload(files, signal) {
90
+ const { root, entries } = decodeUpload(files)
91
+ const directory = await mkdtemp(path.join(await realpath(tmpdir()), uploadPrefix))
92
+ const identity = await lstat(directory)
93
+ try {
94
+ for (const { relative, bytes } of entries) {
95
+ signal.throwIfAborted()
96
+ const file = path.join(directory, relative)
97
+ await mkdir(path.dirname(file), { recursive: true, mode: 0o700 })
98
+ await writeFile(file, bytes, { flag: 'wx', mode: 0o600, signal })
99
+ }
100
+ signal.throwIfAborted()
101
+ return { directory, identity, path: path.join(directory, root), displayName: root }
102
+ } catch (error) {
103
+ await removeStagedDirectory(directory, identity)
104
+ throw error
105
+ }
106
+ }
107
+
108
+ async function localSource(value, cwd) {
109
+ const raw = text(value, '本地技能路径')
110
+ const absolute = /^~[\\/]/.test(raw) ? path.join(homedir(), raw.slice(2)) : path.resolve(cwd, raw)
111
+ const canonical = await realpath(absolute)
112
+ const first = path.relative(await realpath(tmpdir()), canonical).split(path.sep)[0]
113
+ if (first.startsWith(uploadPrefix)) throw problem('INVALID_INPUT', '上传文件请通过文件选择器或拖放导入。')
114
+ return canonical
115
+ }
116
+
117
+ function remoteSource(input, cwd, signal) {
118
+ if (input.type === 'github') return { type: 'github', url: text(input.url, '仓库地址'), cwd, signal }
119
+ if (input.type !== 'tool' || !['claude', 'codex', 'gemini'].includes(input.tool) || !['user', 'project'].includes(input.scope)) {
120
+ throw problem('INVALID_INPUT', '请选择有效的工具来源和扫描范围。')
121
+ }
122
+ return { type: 'tool', tool: input.tool, scope: input.scope, cwd, signal }
123
+ }
124
+
125
+ function failure(error) {
126
+ const details = {}
127
+ for (const key of ['existingPath', 'targetPath', 'backupPath', 'path', 'directoryId', 'enabled', 'memberCount', 'status']) {
128
+ if (['string', 'boolean', 'number'].includes(typeof error?.[key])) details[key] = error[key]
129
+ }
130
+ return { ok: false, error: {
131
+ code: error?.name === 'AbortError' ? 'CANCELLED' : typeof error?.code === 'string' ? error.code : 'SKILL_MANAGER_ERROR',
132
+ message: error instanceof Error ? error.message : '技能管理操作失败。',
133
+ details,
134
+ } }
135
+ }
136
+
137
+ // A distinct importer for each owner peer and workspace also isolates its opaque candidate IDs.
138
+ export function createGateway(ctx, library, makeImports = createImports) {
139
+ const scopes = new Map()
140
+ const removalPlans = new Map()
141
+ const lifecycle = new AbortController()
142
+ const activeIO = new Set()
143
+ const stagedDirectories = new Map()
144
+ let pendingUploads = 0
145
+ let pendingPreviews = 0
146
+
147
+ function track(promise) {
148
+ activeIO.add(promise)
149
+ const done = () => activeIO.delete(promise)
150
+ promise.then(done, done)
151
+ return promise
152
+ }
153
+
154
+ async function discardUpload(upload) {
155
+ // Only directories returned by our own mkdtemp calls are eligible for recursive removal.
156
+ const identity = stagedDirectories.get(upload.directory)
157
+ if (identity) {
158
+ await removeStagedDirectory(upload.directory, identity)
159
+ stagedDirectories.delete(upload.directory)
160
+ }
161
+ }
162
+
163
+ async function releaseUpload(scope, id) {
164
+ const upload = scope.uploads.get(id)
165
+ if (!upload) return
166
+ scope.uploads.delete(id)
167
+ upload.released = true
168
+ if (!upload.readers) await discardUpload(upload)
169
+ }
170
+
171
+ function lookupScope(peer, context, create) {
172
+ const key = JSON.stringify([peer.id, context.workspaceId, context.cwd, context.presetId])
173
+ let scope = scopes.get(key)
174
+ if (!scope && create) {
175
+ if (scopes.size >= 64) throw problem('BUSY', '打开的导入窗口过多,请稍后重试。')
176
+ scope = { imports: makeImports(), uploads: new Map(), previews: new Map(), active: 0 }
177
+ scopes.set(key, scope)
178
+ }
179
+ if (!scope) throw problem('PREVIEW_EXPIRED', '导入已过期,请重新预览。')
180
+ scope.lastUsed = Date.now()
181
+ return scope
182
+ }
183
+
184
+ async function sweep() {
185
+ const now = Date.now()
186
+ for (const [token, plan] of removalPlans) if (plan.expires <= now || plan.peerSignal.aborted) removalPlans.delete(token)
187
+ for (const [key, scope] of scopes) {
188
+ if (scope.active) continue
189
+ for (const [id, upload] of scope.uploads) {
190
+ if (upload.expires > now) continue
191
+ await releaseUpload(scope, id)
192
+ }
193
+ for (const [token, record] of scope.previews) {
194
+ if (record.expires > now || record.saving) continue
195
+ scope.previews.delete(token)
196
+ await scope.imports.commit({ token, conflict: 'cancel' }).catch(() => {})
197
+ }
198
+ if (!scope.active && scope.lastUsed + lifetimeMs <= now && !scope.uploads.size && !scope.previews.size) {
199
+ scopes.delete(key)
200
+ scope.imports.dispose()
201
+ }
202
+ }
203
+ }
204
+
205
+ async function upload(scope, input, signal) {
206
+ if (stagedDirectories.size + pendingUploads >= maxPending) throw problem('BUSY', '待处理的上传过多,请先完成或关闭已有导入。')
207
+ pendingUploads++
208
+ try {
209
+ const staged = await stageUpload(input.files, signal)
210
+ stagedDirectories.set(staged.directory, staged.identity)
211
+ if (signal.aborted) {
212
+ await discardUpload(staged)
213
+ signal.throwIfAborted()
214
+ }
215
+ const uploadId = randomUUID()
216
+ scope.uploads.set(uploadId, { ...staged, readers: 0, expires: Date.now() + lifetimeMs })
217
+ return { uploadId }
218
+ } finally {
219
+ pendingUploads--
220
+ }
221
+ }
222
+
223
+ async function preview(scope, input, context, signal) {
224
+ let count = pendingPreviews
225
+ for (const current of scopes.values()) {
226
+ for (const record of current.previews.values()) if (!record.receipt && record.expires > Date.now()) count++
227
+ }
228
+ if (count >= maxPending) throw problem('BUSY', '待确认的预览过多,请先完成或关闭已有预览。')
229
+ pendingPreviews++
230
+ let staged
231
+ try {
232
+ let source
233
+ if (input.type === 'local' || input.type === 'upload') {
234
+ if (input.type === 'upload' || input.uploadId !== undefined) {
235
+ const id = text(input.uploadId, '上传凭据', 128)
236
+ const uploaded = scope.uploads.get(id)
237
+ if (!uploaded || uploaded.expires <= Date.now()) {
238
+ await releaseUpload(scope, id)
239
+ throw problem('PREVIEW_EXPIRED', '上传已过期,请重新选择文件。')
240
+ }
241
+ staged = uploaded
242
+ staged.readers++
243
+ }
244
+ source = { type: 'local', path: staged?.path ?? await localSource(input.path, context.cwd), cwd: context.cwd, signal }
245
+ } else {
246
+ source = { ...remoteSource(input, context.cwd, signal), id: text(input.id, '候选技能 ID') }
247
+ }
248
+ const result = await scope.imports.preview(source)
249
+ if (signal.aborted) {
250
+ await scope.imports.commit({ token: result.token, conflict: 'cancel' })
251
+ signal.throwIfAborted()
252
+ }
253
+ scope.previews.set(result.token, { expires: Date.now() + lifetimeMs, displayName: staged?.displayName, family: result.kind === 'family' })
254
+ return staged ? { ...result, sourcePath: staged.displayName } : result
255
+ } finally {
256
+ pendingPreviews--
257
+ if (staged) {
258
+ staged.readers--
259
+ if (staged.released && !staged.readers) await discardUpload(staged)
260
+ }
261
+ }
262
+ }
263
+
264
+ async function commit(scope, input, context, signal) {
265
+ const token = text(input.token, '预览凭据', 128)
266
+ const record = scope.previews.get(token)
267
+ if (!record || record.expires <= Date.now()) throw problem('PREVIEW_EXPIRED', '预览已过期,请重新选择技能。')
268
+ const directoryId = text(input.directoryId, '目标目录 ID')
269
+ const enabled = input.enabled === undefined ? (record.family ? undefined : true) : boolean(input.enabled, '启用状态')
270
+ if (input.conflict !== undefined && input.conflict !== 'replace') throw problem('INVALID_INPUT', '无效的同名文件处理方式。')
271
+ if (record.saving) throw problem('BUSY', '此技能正在保存,请稍后。')
272
+ if (record.receipt && (directoryId !== record.receipt.directoryId || enabled !== record.requestedEnabled)) {
273
+ throw problem('ALREADY_SAVED', '本次导入已写入原目标目录,请使用原选项重试。')
274
+ }
275
+ record.saving = true
276
+ try {
277
+ if (!record.receipt) {
278
+ const directory = await writableTarget(library, context, directoryId)
279
+ signal.throwIfAborted()
280
+ const options = { token, directory, conflict: input.conflict }
281
+ if (record.family && enabled !== undefined) options.enabled = enabled
282
+ const saved = await track(scope.imports.commit(options))
283
+ record.receipt = record.displayName ? { ...saved, sourcePath: record.displayName } : saved
284
+ record.requestedEnabled = enabled
285
+ record.expires = Date.now() + lifetimeMs
286
+ }
287
+ if (!record.registered) {
288
+ try {
289
+ const snapshot = await library.imported(context.cwd, record.receipt, record.family ? undefined : enabled, context.agent)
290
+ const warnings = [record.receipt.warning, ...(snapshot?.warnings ?? [])].filter(Boolean)
291
+ if (record.family) {
292
+ const pkg = snapshot?.importedPackage
293
+ const items = new Map(snapshot?.importedItems?.map(item => [item.name, item]))
294
+ const members = record.receipt.members.map(member => {
295
+ const item = items.get(member.name)
296
+ if (!pkg?.id || item?.package?.id !== pkg.id || typeof item.enabled !== 'boolean') throw new Error('部分成员的登记仍待完成。')
297
+ return { ...member, id: item.id, enabled: item.enabled }
298
+ })
299
+ const actualEnabled = pkg.enabled ?? record.receipt.family.enabled
300
+ Object.assign(record.receipt, { enabled: actualEnabled, members, memberCount: members.length, package: pkg,
301
+ family: { ...record.receipt.family, enabled: actualEnabled, members } })
302
+ } else {
303
+ const item = snapshot?.importedItem ?? snapshot?.items?.find(row => row.directoryId === directoryId && row.path === record.receipt.path)
304
+ record.receipt.enabled = item?.enabled ?? enabled
305
+ }
306
+ if (warnings.length) record.receipt.warning = [...new Set(warnings)].join(' ')
307
+ record.registered = true
308
+ } catch (error) {
309
+ const savedPath = record.receipt.familyRoot ?? record.receipt.path
310
+ throw problem('REGISTRATION_FAILED', `文件已保存至 ${savedPath}。请重试以完成登记。${error.message}`, {
311
+ path: savedPath, directoryId, enabled: record.receipt.enabled ?? enabled, memberCount: record.receipt.members?.length,
312
+ })
313
+ }
314
+ }
315
+ return record.receipt
316
+ } finally {
317
+ record.saving = false
318
+ }
319
+ }
320
+
321
+ async function cancel(scope, input) {
322
+ if (input.uploadId !== undefined) {
323
+ const id = text(input.uploadId, '上传凭据', 128)
324
+ await releaseUpload(scope, id)
325
+ } else {
326
+ const token = text(input.token, '预览凭据', 128)
327
+ const record = scope.previews.get(token)
328
+ if (record?.saving) throw problem('BUSY', '此技能正在保存。')
329
+ if (record) {
330
+ await scope.imports.commit({ token, conflict: 'cancel' })
331
+ scope.previews.delete(token)
332
+ }
333
+ }
334
+ return { cancelled: true }
335
+ }
336
+
337
+ function removalScope(peer, context) {
338
+ return JSON.stringify([peer.id, context.workspaceId, context.cwd, context.presetId])
339
+ }
340
+
341
+ async function prepareRemoval(input, context, peer, signal) {
342
+ const { kind } = input
343
+ if (kind !== 'skill' && kind !== 'package') throw problem('INVALID_INPUT', '请选择技能或工具包。')
344
+ const id = text(input.id, '移除目标 ID')
345
+ const plan = await library.prepareRemoval(context.cwd, kind, id, context.agent, signal)
346
+ signal.throwIfAborted()
347
+ const owner = removalScope(peer, context)
348
+ // The workspace view presents one confirmation at a time.
349
+ for (const [token, pending] of removalPlans) {
350
+ if (pending.expires <= Date.now() || pending.peerSignal.aborted || pending.owner === owner) removalPlans.delete(token)
351
+ }
352
+ if (removalPlans.size >= maxPending) throw problem('BUSY', '待确认的移除请求过多,请稍后重试。')
353
+ const token = randomUUID()
354
+ removalPlans.set(token, { owner, plan, peerSignal: peer.signal, expires: Date.now() + lifetimeMs })
355
+ const { name, path, scope, members } = plan
356
+ return { token, kind, id, name, path, scope, ...(members && { members, memberCount: members.length }) }
357
+ }
358
+
359
+ async function dispatch(operation, input, signal, peer) {
360
+ if (peer?.principal?.kind !== 'owner' || !peer.id) throw problem('FORBIDDEN', '仅主机所有者可管理技能。')
361
+ signal = AbortSignal.any([signal, peer.signal, lifecycle.signal])
362
+ signal.throwIfAborted()
363
+ if (!input || typeof input !== 'object' || Array.isArray(input)) throw problem('INVALID_INPUT', '请求参数无效。')
364
+ const context = await contextOf(ctx, input, signal)
365
+ signal.throwIfAborted()
366
+ const { cwd, agent } = context
367
+ switch (operation) {
368
+ case 'list': return library.list(cwd, agent)
369
+ case 'refresh': return library.refresh(cwd, agent)
370
+ case 'detail': {
371
+ const item = await library.detail(cwd, text(input.id, '技能 ID'), agent)
372
+ const resource = await skillResources(item, signal)
373
+ return { ...item, files: resource?.files ?? null }
374
+ }
375
+ case 'packageEntries':
376
+ case 'packageFile': return browsePackage(library, context, text(input.id, '技能包 ID'), input, operation === 'packageEntries', signal)
377
+ case 'skillEntries':
378
+ case 'skillFile': return browseSkill(library, context, text(input.id, '技能 ID'), input, operation === 'skillEntries', signal)
379
+ case 'prepareRemoval': return prepareRemoval(input, context, peer, signal)
380
+ case 'commitRemoval': {
381
+ const token = text(input.token, '移除确认凭据', 128)
382
+ const pending = removalPlans.get(token)
383
+ if (!pending || pending.expires <= Date.now() || pending.peerSignal.aborted || pending.owner !== removalScope(peer, context)) {
384
+ throw problem('REMOVAL_EXPIRED', '移除确认已过期,请重新打开确认窗口。')
385
+ }
386
+ removalPlans.delete(token)
387
+ return track(library.commitRemoval(cwd, pending.plan, agent, signal))
388
+ }
389
+ case 'listRemovals': return library.listRemovals(cwd, agent, signal)
390
+ case 'restoreRemoval': return track(library.restoreRemoval(cwd, text(input.id, '恢复编号', 128), agent, signal))
391
+ case 'setPackageEnabled': return library.setPackageEnabled(cwd, text(input.id, '技能包 ID'), boolean(input.enabled, '启用状态'), agent)
392
+ case 'setEnabled': return library.setEnabled(cwd, text(input.id, '技能 ID'), boolean(input.enabled, '启用状态'), agent)
393
+ case 'addDirectory': return library.addDirectory(cwd, text(input.path, '目录路径'), input.label === undefined ? undefined : text(input.label, '目录名称', 200), agent)
394
+ case 'removeDirectory': return library.removeDirectory(cwd, text(input.id, '目录 ID'), agent)
395
+ case 'targets': return library.targets(cwd, agent)
396
+ case 'openOptions': {
397
+ const resource = await lookupSkillResources(library, context, text(input.id, '技能 ID'), signal)
398
+ return openOptions(ctx.sessionController, resource, signal)
399
+ }
400
+ case 'openItem': {
401
+ const { kind } = input
402
+ if (kind !== 'file' && kind !== 'directory') throw problem('INVALID_INPUT', '请选择打开说明文件或所在目录。')
403
+ const applicationId = input.applicationId === undefined ? undefined : text(input.applicationId, '应用 ID')
404
+ if (kind === 'directory' && applicationId !== undefined) throw problem('INVALID_INPUT', '显示所在位置由系统文件管理器处理。')
405
+ const resource = await lookupSkillResources(library, context, text(input.id, '技能 ID'), signal)
406
+ return openSkillItem(ctx.sessionController, resource, kind, applicationId, signal)
407
+ }
408
+ case 'prepareCreator': {
409
+ const info = await stat(cwd).catch(error => {
410
+ if (error.code === 'ENOENT' || error.code === 'ENOTDIR') return null
411
+ throw error
412
+ })
413
+ if (!info?.isDirectory()) throw problem('WORKSPACE_NOT_FOUND', '新会话工作目录不存在。')
414
+ return { cwd, workspaceId: context.workspaceId, agentPreset: await library.creatorPreset(cwd, agent) }
415
+ }
416
+ }
417
+ const createsScope = ['candidates', 'upload', 'preview'].includes(operation)
418
+ if (!createsScope && operation !== 'cancel' && operation !== 'commit') throw problem('UNKNOWN_OPERATION', '此技能管理操作不存在。')
419
+ const scope = lookupScope(peer, context, createsScope)
420
+ scope.active++
421
+ try {
422
+ switch (operation) {
423
+ case 'candidates': return await track(scope.imports.candidates(remoteSource(input, cwd, signal)))
424
+ case 'upload': return await track(upload(scope, input, signal))
425
+ case 'preview': return await track(preview(scope, input, context, signal))
426
+ case 'commit': return await commit(scope, input, context, signal)
427
+ case 'cancel': return await track(cancel(scope, input))
428
+ }
429
+ } finally {
430
+ scope.active--
431
+ scope.lastUsed = Date.now()
432
+ }
433
+ }
434
+
435
+ const timer = setInterval(() => { track(sweep()).catch(error => ctx.logger?.warn?.('技能导入暂存清理失败', error)) }, 30_000)
436
+ timer.unref()
437
+ return {
438
+ handle: (operation, input, signal, peer) => dispatch(operation, input, signal, peer).then(value => ({ ok: true, value }), failure),
439
+ async dispose() {
440
+ lifecycle.abort()
441
+ clearInterval(timer)
442
+ // Configuration registration may itself trigger a Cordis reload. Only wait for source/target I/O here.
443
+ await Promise.allSettled([...activeIO])
444
+ removalPlans.clear()
445
+ for (const scope of scopes.values()) scope.imports.dispose()
446
+ scopes.clear()
447
+ await Promise.all([...stagedDirectories.keys()].map(directory => discardUpload({ directory })))
448
+ },
449
+ }
450
+ }
451
+
452
+ export function apply(ctx, config) {
453
+ registerCreatorSkill(ctx)
454
+ const library = createLibrary(ctx, config)
455
+ registerAgentTool(ctx, library)
456
+ const gateway = createGateway(ctx, library)
457
+ ctx.effect(() => () => gateway.dispose(), 'skill manager import staging')
458
+ // The built-in Typert Gateway owns the sole /api interceptor in DSH 0.2.3-alpha.3.
459
+ ctx.connection.rpc.handle(channel, gateway.handle)
460
+ }
package/icon.svg ADDED
@@ -0,0 +1,6 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 36 36">
2
+ <path fill="#fff" d="M13 8.8a4.3 4.3 0 0 1 4-2.8h8.9a4.2 4.2 0 0 1 4.2 4.2V21a4.3 4.3 0 0 1-3.3 4.2V13.8a5 5 0 0 0-5-5Z"/>
3
+ <path fill="#000" d="M17.8 7.3h8.1a2.9 2.9 0 0 1 2.9 2.9v10.6c0 .9-.2 1.5-.7 2.1v-9.1a6.3 6.3 0 0 0-6.3-6.3h-4.4Z"/>
4
+ <rect x="5.9" y="10.1" width="19.6" height="19.9" rx="4.3" fill="#fff"/>
5
+ <path fill="#000" d="M10.2 11.4h1.4v10.4l4.1-2.6 4.1 2.6V11.4h1.4a3 3 0 0 1 3 3v11.3a3 3 0 0 1-3 3h-11a3 3 0 0 1-3-3V14.4a3 3 0 0 1 3-3Z"/>
6
+ </svg>
@@ -0,0 +1,41 @@
1
+ import { crc32 } from 'node:zlib'
2
+ import yauzl from 'yauzl'
3
+ import { checkPath, checkSize, ignoredResource, ImportFiles, limits, problem } from './import-files.mjs'
4
+
5
+ export async function readArchive(bytes, signal) {
6
+ const files = new ImportFiles()
7
+ try {
8
+ const archive = await yauzl.fromBufferPromise(bytes, { strictFileNames: true, validateEntrySizes: true })
9
+ checkSize(archive.entryCount, limits.entries)
10
+ for await (const entry of archive.eachEntry()) {
11
+ signal?.throwIfAborted()
12
+ const directory = entry.fileName.endsWith('/')
13
+ const name = directory ? entry.fileName.slice(0, -1) : entry.fileName
14
+ checkPath(name)
15
+ const mode = entry.externalFileAttributes >>> 16
16
+ const kind = mode & 0o170000
17
+ if (entry.isEncrypted() || (entry.externalFileAttributes & 0x400) || (kind && kind !== (directory ? 0o040000 : 0o100000)) || (directory && entry.uncompressedSize)) {
18
+ throw problem('UNSAFE_ARCHIVE', `归档中包含链接、特殊文件或加密项:${name}`)
19
+ }
20
+ if (ignoredResource(name)) continue
21
+ const record = files.reserve(name, directory, entry.uncompressedSize, Boolean(mode & 0o111))
22
+ if (directory) continue
23
+ const stream = await archive.openReadStreamPromise(entry)
24
+ const content = Buffer.alloc(entry.uncompressedSize)
25
+ let offset = 0
26
+ for await (const chunk of stream) {
27
+ signal?.throwIfAborted()
28
+ checkSize(offset + chunk.length, content.length)
29
+ chunk.copy(content, offset)
30
+ offset += chunk.length
31
+ }
32
+ if (offset !== content.length || crc32(content) !== entry.crc32) throw problem('INVALID_ARCHIVE', `文件校验失败:${name}`)
33
+ record.bytes = content
34
+ }
35
+ } catch (error) {
36
+ signal?.throwIfAborted()
37
+ if (['UNSAFE_PATH', 'UNSAFE_ARCHIVE', 'SIZE_LIMIT', 'INVALID_ARCHIVE'].includes(error.code)) throw error
38
+ throw problem('INVALID_ARCHIVE', `无法读取 ZIP 技能包。${error.message}`)
39
+ }
40
+ return [...files.nodes.values()]
41
+ }