@kingsleyasher/dsh-knowledge-base 0.0.0-stage → 3.1.5

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 (516) hide show
  1. package/ARCHITECTURE.en.md +35 -0
  2. package/ARCHITECTURE.md +40 -0
  3. package/BETA4.md +31 -0
  4. package/BETA7.md +35 -0
  5. package/BETA8.md +39 -0
  6. package/BETA9.md +38 -0
  7. package/CONTRIBUTING.md +5 -0
  8. package/DEVELOPMENT.en.md +26 -0
  9. package/DEVELOPMENT.md +27 -0
  10. package/EXISTING-FRAMEWORK-OPTIMIZATION.md +101 -0
  11. package/LICENSE +21 -0
  12. package/MONOREPO.md +34 -0
  13. package/NPM-EDITION.md +12 -0
  14. package/PARSING-PREVIEW.md +84 -0
  15. package/PERFORMANCE.md +43 -0
  16. package/PRIVACY.md +7 -0
  17. package/PROJECT-RECORD.en.md +782 -0
  18. package/PROJECT-RECORD.md +793 -0
  19. package/README.md +109 -3
  20. package/README.zh-CN.md +117 -0
  21. package/RELEASE.en.md +559 -0
  22. package/RELEASE.md +570 -0
  23. package/RETRIEVAL.md +93 -0
  24. package/SECURITY.md +7 -0
  25. package/THIRD-PARTY-NOTICES.md +5 -0
  26. package/VERSIONING.en.md +51 -0
  27. package/VERSIONING.md +50 -0
  28. package/WORKBENCH.en.md +201 -0
  29. package/WORKBENCH.md +195 -0
  30. package/browser-extension/background.js +146 -0
  31. package/browser-extension/icons/knowledge-base-128.png +0 -0
  32. package/browser-extension/icons/knowledge-base-16.png +0 -0
  33. package/browser-extension/icons/knowledge-base-32.png +0 -0
  34. package/browser-extension/icons/knowledge-base-48.png +0 -0
  35. package/browser-extension/manifest.json +25 -0
  36. package/browser-extension/options.html +22 -0
  37. package/browser-extension/options.js +41 -0
  38. package/browser-extension/popup.html +17 -0
  39. package/browser-extension/popup.js +58 -0
  40. package/browser-extension/style.css +11 -0
  41. package/cordis.patch.yml +88 -0
  42. package/docs/LEGACY-246-BOOTSTRAP.md +21 -0
  43. package/docs/LIBRARY-FRAMEWORK-MIGRATION.md +45 -0
  44. package/docs/ORIGINAL-FILE-INDEX-ARCHITECTURE.md +43 -0
  45. package/docs/TODO-LIFECYCLE-VALIDATION.md +42 -0
  46. package/docs/UPDATE-RECOVERY.en.md +18 -0
  47. package/docs/UPDATE-RECOVERY.md +70 -0
  48. package/docs/WINDOWS-SIGNING.md +30 -0
  49. package/docs/client/ARCHITECTURE.en.md +17 -0
  50. package/docs/client/ARCHITECTURE.md +17 -0
  51. package/docs/client/CONTRIBUTING.md +5 -0
  52. package/docs/client/CROSS-VOLUME-TRASH.md +13 -0
  53. package/docs/client/DEVELOPMENT.en.md +33 -0
  54. package/docs/client/DEVELOPMENT.md +33 -0
  55. package/docs/client/EXISTING-FRAMEWORK-OPTIMIZATION.md +3 -0
  56. package/docs/client/PERFORMANCE.md +3 -0
  57. package/docs/client/PRIVACY.md +7 -0
  58. package/docs/client/PROJECT-RECORD.en.md +199 -0
  59. package/docs/client/PROJECT-RECORD.md +219 -0
  60. package/docs/client/README.md +55 -0
  61. package/docs/client/README.zh-CN.md +45 -0
  62. package/docs/client/RELEASE.en.md +148 -0
  63. package/docs/client/RELEASE.md +149 -0
  64. package/docs/client/RETRIEVAL.md +3 -0
  65. package/docs/client/SECURITY.md +7 -0
  66. package/docs/client/THIRD-PARTY-NOTICES.md +5 -0
  67. package/docs/client/VERSIONING.en.md +9 -0
  68. package/docs/client/VERSIONING.md +9 -0
  69. package/docs/client/WORKBENCH.en.md +24 -0
  70. package/docs/client/WORKBENCH.md +24 -0
  71. package/docs/client/skills/personal-knowledge-base/SKILL.md +38 -0
  72. package/docs/releases/client-1.0.1.md +32 -0
  73. package/docs/releases/client-1.0.2.md +32 -0
  74. package/docs/releases/client-1.0.3.md +31 -0
  75. package/docs/releases/client-1.1.1.md +28 -0
  76. package/docs/releases/client-1.1.2.md +16 -0
  77. package/docs/releases/client-1.1.4.md +7 -0
  78. package/docs/releases/client-2.0.0.en.md +13 -0
  79. package/docs/releases/client-2.0.0.md +13 -0
  80. package/docs/releases/client-2.0.1.en.md +10 -0
  81. package/docs/releases/client-2.0.1.md +10 -0
  82. package/docs/releases/client-2.0.3.en.md +8 -0
  83. package/docs/releases/client-2.0.3.md +8 -0
  84. package/docs/releases/plugin-2.4.1.md +24 -0
  85. package/docs/releases/plugin-2.4.10.md +22 -0
  86. package/docs/releases/plugin-2.4.11.md +14 -0
  87. package/docs/releases/plugin-2.4.12.md +14 -0
  88. package/docs/releases/plugin-2.4.13.md +27 -0
  89. package/docs/releases/plugin-2.4.14.md +18 -0
  90. package/docs/releases/plugin-2.4.2.md +31 -0
  91. package/docs/releases/plugin-2.4.3.md +25 -0
  92. package/docs/releases/plugin-2.4.4.md +31 -0
  93. package/docs/releases/plugin-2.4.5.md +23 -0
  94. package/docs/releases/plugin-2.4.6.md +23 -0
  95. package/docs/releases/plugin-2.4.7.md +14 -0
  96. package/docs/releases/plugin-2.4.8.md +14 -0
  97. package/docs/releases/plugin-2.4.9.md +18 -0
  98. package/docs/releases/plugin-2.5.10.md +11 -0
  99. package/docs/releases/plugin-2.5.13.en.md +11 -0
  100. package/docs/releases/plugin-2.5.13.md +11 -0
  101. package/docs/releases/plugin-2.5.3.md +28 -0
  102. package/docs/releases/plugin-2.5.4.md +14 -0
  103. package/docs/releases/plugin-2.5.5.md +16 -0
  104. package/docs/releases/plugin-2.5.6.md +3 -0
  105. package/docs/releases/plugin-2.5.7.md +7 -0
  106. package/docs/releases/plugin-2.5.8.md +7 -0
  107. package/docs/releases/plugin-2.5.9.md +7 -0
  108. package/docs/releases/plugin-3.0.0.en.md +13 -0
  109. package/docs/releases/plugin-3.0.0.md +13 -0
  110. package/docs/releases/plugin-3.0.1.en.md +10 -0
  111. package/docs/releases/plugin-3.0.1.md +10 -0
  112. package/docs/releases/plugin-3.1.3.en.md +11 -0
  113. package/docs/releases/plugin-3.1.3.md +11 -0
  114. package/docs/validation/windows-auto-update-2.4.13.md +31 -0
  115. package/docs/validation/windows-direct-246-to-2413.md +32 -0
  116. package/package.json +78 -6
  117. package/packages/kb-contracts/desktop.js +29 -0
  118. package/packages/kb-contracts/index.js +132 -0
  119. package/packages/kb-contracts/node.js +3 -0
  120. package/packages/kb-contracts/package.json +17 -0
  121. package/packages/kb-contracts/schema.js +42 -0
  122. package/packages/kb-contracts/tools.js +1 -0
  123. package/packages/kb-contracts/version-policy.js +45 -0
  124. package/packages/kb-core/index.js +9 -0
  125. package/packages/kb-core/package.json +15 -0
  126. package/packages/kb-core/src/capture-surface.js +22 -0
  127. package/packages/kb-core/src/documents.js +105 -0
  128. package/packages/kb-core/src/embedded.js +29 -0
  129. package/packages/kb-core/src/file-catalog.js +151 -0
  130. package/packages/kb-core/src/file-classification.js +60 -0
  131. package/packages/kb-core/src/flow-workspace.js +66 -0
  132. package/packages/kb-core/src/flows.js +1136 -0
  133. package/packages/kb-core/src/fsindex.js +293 -0
  134. package/packages/kb-core/src/import-service.js +486 -0
  135. package/packages/kb-core/src/importer.js +909 -0
  136. package/packages/kb-core/src/inbox-flow.js +2 -0
  137. package/packages/kb-core/src/index-architecture.js +48 -0
  138. package/packages/kb-core/src/index-maintenance.js +81 -0
  139. package/packages/kb-core/src/index-policy-service.js +191 -0
  140. package/packages/kb-core/src/index-policy.js +121 -0
  141. package/packages/kb-core/src/kbcore.js +2519 -0
  142. package/packages/kb-core/src/library-framework.js +186 -0
  143. package/packages/kb-core/src/library-state.js +68 -0
  144. package/packages/kb-core/src/managed-files.js +69 -0
  145. package/packages/kb-core/src/map.js +171 -0
  146. package/packages/kb-core/src/parse-credentials.js +49 -0
  147. package/packages/kb-core/src/parse-discovery.js +119 -0
  148. package/packages/kb-core/src/parse-job-storage.js +40 -0
  149. package/packages/kb-core/src/parse-native.js +87 -0
  150. package/packages/kb-core/src/parse-presets.js +46 -0
  151. package/packages/kb-core/src/parse-source-authority.js +72 -0
  152. package/packages/kb-core/src/parsing.js +2307 -0
  153. package/packages/kb-core/src/relation-name-lookup.js +43 -0
  154. package/packages/kb-core/src/retrieval-api.js +79 -0
  155. package/packages/kb-core/src/retrieval-cache.js +22 -0
  156. package/packages/kb-core/src/retrieval-chunks.js +128 -0
  157. package/packages/kb-core/src/retrieval-pool.js +102 -0
  158. package/packages/kb-core/src/retrieval-worker.js +190 -0
  159. package/packages/kb-core/src/retrieval.js +618 -0
  160. package/packages/kb-core/src/runtime-resources.js +73 -0
  161. package/packages/kb-core/src/service-ownership.js +18 -0
  162. package/packages/kb-core/src/source-references.js +61 -0
  163. package/packages/kb-core/src/storage-io.js +121 -0
  164. package/packages/kb-core/src/system-upgrades.js +104 -0
  165. package/packages/kb-core/src/workbench-backup.js +219 -0
  166. package/packages/kb-core/src/workbench-deliverables.js +73 -0
  167. package/packages/kb-core/src/workbench-files.js +25 -0
  168. package/packages/kb-core/src/workbench-inbox.js +57 -0
  169. package/packages/kb-core/src/workbench-links.js +236 -0
  170. package/packages/kb-core/src/workbench-question.js +131 -0
  171. package/packages/kb-core/src/workbench-relation-index.js +110 -0
  172. package/packages/kb-core/src/workbench-state.js +102 -0
  173. package/packages/kb-core/src/workbench-trash.js +213 -0
  174. package/packages/kb-core/src/workbench.js +936 -0
  175. package/packages/kb-service/auth.js +39 -0
  176. package/packages/kb-service/build.js +18 -0
  177. package/packages/kb-service/cli.js +26 -0
  178. package/packages/kb-service/client.js +64 -0
  179. package/packages/kb-service/clipper.js +15 -0
  180. package/packages/kb-service/compatibility.js +34 -0
  181. package/packages/kb-service/desktop-files.js +514 -0
  182. package/packages/kb-service/desktop-host-links.js +39 -0
  183. package/packages/kb-service/dsh-broker.js +64 -0
  184. package/packages/kb-service/dsh-host.js +168 -0
  185. package/packages/kb-service/dsh-stream-relay.js +32 -0
  186. package/packages/kb-service/engines.js +32 -0
  187. package/packages/kb-service/extension.js +8 -0
  188. package/packages/kb-service/first-library-handoff.js +28 -0
  189. package/packages/kb-service/handoff-api.js +22 -0
  190. package/packages/kb-service/host-adapter.js +151 -0
  191. package/packages/kb-service/host-assets.js +9 -0
  192. package/packages/kb-service/host-python.js +30 -0
  193. package/packages/kb-service/host-updates.js +97 -0
  194. package/packages/kb-service/inbox-host.js +47 -0
  195. package/packages/kb-service/inbox-jobs.js +54 -0
  196. package/packages/kb-service/index.js +3 -0
  197. package/packages/kb-service/io.js +24 -0
  198. package/packages/kb-service/launch-lock.js +27 -0
  199. package/packages/kb-service/launcher.js +18 -0
  200. package/packages/kb-service/legacy-handoff.js +27 -0
  201. package/packages/kb-service/legacy-ports.js +18 -0
  202. package/packages/kb-service/main.js +13 -0
  203. package/packages/kb-service/memory-channel.js +57 -0
  204. package/packages/kb-service/memory-sync.js +42 -0
  205. package/packages/kb-service/migration.js +131 -0
  206. package/packages/kb-service/native.py +216 -0
  207. package/packages/kb-service/operations.js +19 -0
  208. package/packages/kb-service/package.json +1 -0
  209. package/packages/kb-service/runtime.js +185 -0
  210. package/packages/kb-service/safe-update.js +104 -0
  211. package/packages/kb-service/service.js +494 -0
  212. package/packages/kb-service/tool-definitions.json +934 -0
  213. package/packages/kb-service/tool-router.js +45 -0
  214. package/packages/kb-service/vector-profiles.js +41 -0
  215. package/presets/01-knowledge-agent.patch.yml +347 -0
  216. package/runtime/python/LICENSE.txt +676 -0
  217. package/runtime/python/_asyncio.pyd +0 -0
  218. package/runtime/python/_bz2.pyd +0 -0
  219. package/runtime/python/_ctypes.pyd +0 -0
  220. package/runtime/python/_decimal.pyd +0 -0
  221. package/runtime/python/_elementtree.pyd +0 -0
  222. package/runtime/python/_hashlib.pyd +0 -0
  223. package/runtime/python/_lzma.pyd +0 -0
  224. package/runtime/python/_multiprocessing.pyd +0 -0
  225. package/runtime/python/_overlapped.pyd +0 -0
  226. package/runtime/python/_queue.pyd +0 -0
  227. package/runtime/python/_remote_debugging.pyd +0 -0
  228. package/runtime/python/_socket.pyd +0 -0
  229. package/runtime/python/_sqlite3.pyd +0 -0
  230. package/runtime/python/_ssl.pyd +0 -0
  231. package/runtime/python/_uuid.pyd +0 -0
  232. package/runtime/python/_wmi.pyd +0 -0
  233. package/runtime/python/_zoneinfo.pyd +0 -0
  234. package/runtime/python/_zstd.pyd +0 -0
  235. package/runtime/python/libcrypto-3.dll +0 -0
  236. package/runtime/python/libffi-8.dll +0 -0
  237. package/runtime/python/libssl-3.dll +0 -0
  238. package/runtime/python/libtommath.dll +0 -0
  239. package/runtime/python/pyexpat.pyd +0 -0
  240. package/runtime/python/python.cat +0 -0
  241. package/runtime/python/python.exe +0 -0
  242. package/runtime/python/python3.dll +0 -0
  243. package/runtime/python/python314._pth +5 -0
  244. package/runtime/python/python314.dll +0 -0
  245. package/runtime/python/python314.zip +0 -0
  246. package/runtime/python/pythonw.exe +0 -0
  247. package/runtime/python/select.pyd +0 -0
  248. package/runtime/python/sqlite3.dll +0 -0
  249. package/runtime/python/unicodedata.pyd +0 -0
  250. package/runtime/python/vcruntime140.dll +0 -0
  251. package/runtime/python/vcruntime140_1.dll +0 -0
  252. package/runtime/python/winsound.pyd +0 -0
  253. package/skills/personal-knowledge-base/SKILL.md +38 -0
  254. package/src/client/capture-icon-192.png +0 -0
  255. package/src/client/capture-icon-512.png +0 -0
  256. package/src/client/capture-icon.svg +12 -0
  257. package/src/client/capture-sw.js +10 -0
  258. package/src/client/capture.css +1 -0
  259. package/src/client/capture.html +1 -0
  260. package/src/client/capture.js +37 -0
  261. package/src/client/capture.webmanifest +1 -0
  262. package/src/client/inbox-organize-bridge.js +22 -0
  263. package/src/client/index.js +4975 -0
  264. package/src/client/knowledge-ui.css +255 -0
  265. package/src/client/pdfjs/LICENSE +177 -0
  266. package/src/client/pdfjs/cmaps/78-EUC-H.bcmap +0 -0
  267. package/src/client/pdfjs/cmaps/78-EUC-V.bcmap +0 -0
  268. package/src/client/pdfjs/cmaps/78-H.bcmap +0 -0
  269. package/src/client/pdfjs/cmaps/78-RKSJ-H.bcmap +0 -0
  270. package/src/client/pdfjs/cmaps/78-RKSJ-V.bcmap +0 -0
  271. package/src/client/pdfjs/cmaps/78-V.bcmap +0 -0
  272. package/src/client/pdfjs/cmaps/78ms-RKSJ-H.bcmap +0 -0
  273. package/src/client/pdfjs/cmaps/78ms-RKSJ-V.bcmap +0 -0
  274. package/src/client/pdfjs/cmaps/83pv-RKSJ-H.bcmap +0 -0
  275. package/src/client/pdfjs/cmaps/90ms-RKSJ-H.bcmap +0 -0
  276. package/src/client/pdfjs/cmaps/90ms-RKSJ-V.bcmap +0 -0
  277. package/src/client/pdfjs/cmaps/90msp-RKSJ-H.bcmap +0 -0
  278. package/src/client/pdfjs/cmaps/90msp-RKSJ-V.bcmap +0 -0
  279. package/src/client/pdfjs/cmaps/90pv-RKSJ-H.bcmap +0 -0
  280. package/src/client/pdfjs/cmaps/90pv-RKSJ-V.bcmap +0 -0
  281. package/src/client/pdfjs/cmaps/Add-H.bcmap +0 -0
  282. package/src/client/pdfjs/cmaps/Add-RKSJ-H.bcmap +0 -0
  283. package/src/client/pdfjs/cmaps/Add-RKSJ-V.bcmap +0 -0
  284. package/src/client/pdfjs/cmaps/Add-V.bcmap +0 -0
  285. package/src/client/pdfjs/cmaps/Adobe-CNS1-0.bcmap +0 -0
  286. package/src/client/pdfjs/cmaps/Adobe-CNS1-1.bcmap +0 -0
  287. package/src/client/pdfjs/cmaps/Adobe-CNS1-2.bcmap +0 -0
  288. package/src/client/pdfjs/cmaps/Adobe-CNS1-3.bcmap +0 -0
  289. package/src/client/pdfjs/cmaps/Adobe-CNS1-4.bcmap +0 -0
  290. package/src/client/pdfjs/cmaps/Adobe-CNS1-5.bcmap +0 -0
  291. package/src/client/pdfjs/cmaps/Adobe-CNS1-6.bcmap +0 -0
  292. package/src/client/pdfjs/cmaps/Adobe-CNS1-UCS2.bcmap +0 -0
  293. package/src/client/pdfjs/cmaps/Adobe-GB1-0.bcmap +0 -0
  294. package/src/client/pdfjs/cmaps/Adobe-GB1-1.bcmap +0 -0
  295. package/src/client/pdfjs/cmaps/Adobe-GB1-2.bcmap +0 -0
  296. package/src/client/pdfjs/cmaps/Adobe-GB1-3.bcmap +0 -0
  297. package/src/client/pdfjs/cmaps/Adobe-GB1-4.bcmap +0 -0
  298. package/src/client/pdfjs/cmaps/Adobe-GB1-5.bcmap +0 -0
  299. package/src/client/pdfjs/cmaps/Adobe-GB1-UCS2.bcmap +0 -0
  300. package/src/client/pdfjs/cmaps/Adobe-Japan1-0.bcmap +0 -0
  301. package/src/client/pdfjs/cmaps/Adobe-Japan1-1.bcmap +0 -0
  302. package/src/client/pdfjs/cmaps/Adobe-Japan1-2.bcmap +0 -0
  303. package/src/client/pdfjs/cmaps/Adobe-Japan1-3.bcmap +0 -0
  304. package/src/client/pdfjs/cmaps/Adobe-Japan1-4.bcmap +0 -0
  305. package/src/client/pdfjs/cmaps/Adobe-Japan1-5.bcmap +0 -0
  306. package/src/client/pdfjs/cmaps/Adobe-Japan1-6.bcmap +0 -0
  307. package/src/client/pdfjs/cmaps/Adobe-Japan1-UCS2.bcmap +0 -0
  308. package/src/client/pdfjs/cmaps/Adobe-Korea1-0.bcmap +0 -0
  309. package/src/client/pdfjs/cmaps/Adobe-Korea1-1.bcmap +0 -0
  310. package/src/client/pdfjs/cmaps/Adobe-Korea1-2.bcmap +0 -0
  311. package/src/client/pdfjs/cmaps/Adobe-Korea1-UCS2.bcmap +0 -0
  312. package/src/client/pdfjs/cmaps/B5-H.bcmap +0 -0
  313. package/src/client/pdfjs/cmaps/B5-V.bcmap +0 -0
  314. package/src/client/pdfjs/cmaps/B5pc-H.bcmap +0 -0
  315. package/src/client/pdfjs/cmaps/B5pc-V.bcmap +0 -0
  316. package/src/client/pdfjs/cmaps/CNS-EUC-H.bcmap +0 -0
  317. package/src/client/pdfjs/cmaps/CNS-EUC-V.bcmap +0 -0
  318. package/src/client/pdfjs/cmaps/CNS1-H.bcmap +0 -0
  319. package/src/client/pdfjs/cmaps/CNS1-V.bcmap +0 -0
  320. package/src/client/pdfjs/cmaps/CNS2-H.bcmap +0 -0
  321. package/src/client/pdfjs/cmaps/CNS2-V.bcmap +3 -0
  322. package/src/client/pdfjs/cmaps/ETHK-B5-H.bcmap +0 -0
  323. package/src/client/pdfjs/cmaps/ETHK-B5-V.bcmap +0 -0
  324. package/src/client/pdfjs/cmaps/ETen-B5-H.bcmap +0 -0
  325. package/src/client/pdfjs/cmaps/ETen-B5-V.bcmap +0 -0
  326. package/src/client/pdfjs/cmaps/ETenms-B5-H.bcmap +3 -0
  327. package/src/client/pdfjs/cmaps/ETenms-B5-V.bcmap +0 -0
  328. package/src/client/pdfjs/cmaps/EUC-H.bcmap +0 -0
  329. package/src/client/pdfjs/cmaps/EUC-V.bcmap +0 -0
  330. package/src/client/pdfjs/cmaps/Ext-H.bcmap +0 -0
  331. package/src/client/pdfjs/cmaps/Ext-RKSJ-H.bcmap +0 -0
  332. package/src/client/pdfjs/cmaps/Ext-RKSJ-V.bcmap +0 -0
  333. package/src/client/pdfjs/cmaps/Ext-V.bcmap +0 -0
  334. package/src/client/pdfjs/cmaps/GB-EUC-H.bcmap +0 -0
  335. package/src/client/pdfjs/cmaps/GB-EUC-V.bcmap +0 -0
  336. package/src/client/pdfjs/cmaps/GB-H.bcmap +4 -0
  337. package/src/client/pdfjs/cmaps/GB-V.bcmap +0 -0
  338. package/src/client/pdfjs/cmaps/GBK-EUC-H.bcmap +0 -0
  339. package/src/client/pdfjs/cmaps/GBK-EUC-V.bcmap +0 -0
  340. package/src/client/pdfjs/cmaps/GBK2K-H.bcmap +0 -0
  341. package/src/client/pdfjs/cmaps/GBK2K-V.bcmap +0 -0
  342. package/src/client/pdfjs/cmaps/GBKp-EUC-H.bcmap +0 -0
  343. package/src/client/pdfjs/cmaps/GBKp-EUC-V.bcmap +0 -0
  344. package/src/client/pdfjs/cmaps/GBT-EUC-H.bcmap +0 -0
  345. package/src/client/pdfjs/cmaps/GBT-EUC-V.bcmap +0 -0
  346. package/src/client/pdfjs/cmaps/GBT-H.bcmap +0 -0
  347. package/src/client/pdfjs/cmaps/GBT-V.bcmap +0 -0
  348. package/src/client/pdfjs/cmaps/GBTpc-EUC-H.bcmap +0 -0
  349. package/src/client/pdfjs/cmaps/GBTpc-EUC-V.bcmap +0 -0
  350. package/src/client/pdfjs/cmaps/GBpc-EUC-H.bcmap +0 -0
  351. package/src/client/pdfjs/cmaps/GBpc-EUC-V.bcmap +0 -0
  352. package/src/client/pdfjs/cmaps/H.bcmap +0 -0
  353. package/src/client/pdfjs/cmaps/HKdla-B5-H.bcmap +0 -0
  354. package/src/client/pdfjs/cmaps/HKdla-B5-V.bcmap +0 -0
  355. package/src/client/pdfjs/cmaps/HKdlb-B5-H.bcmap +0 -0
  356. package/src/client/pdfjs/cmaps/HKdlb-B5-V.bcmap +0 -0
  357. package/src/client/pdfjs/cmaps/HKgccs-B5-H.bcmap +0 -0
  358. package/src/client/pdfjs/cmaps/HKgccs-B5-V.bcmap +0 -0
  359. package/src/client/pdfjs/cmaps/HKm314-B5-H.bcmap +0 -0
  360. package/src/client/pdfjs/cmaps/HKm314-B5-V.bcmap +0 -0
  361. package/src/client/pdfjs/cmaps/HKm471-B5-H.bcmap +0 -0
  362. package/src/client/pdfjs/cmaps/HKm471-B5-V.bcmap +0 -0
  363. package/src/client/pdfjs/cmaps/HKscs-B5-H.bcmap +0 -0
  364. package/src/client/pdfjs/cmaps/HKscs-B5-V.bcmap +0 -0
  365. package/src/client/pdfjs/cmaps/Hankaku.bcmap +0 -0
  366. package/src/client/pdfjs/cmaps/Hiragana.bcmap +0 -0
  367. package/src/client/pdfjs/cmaps/KSC-EUC-H.bcmap +0 -0
  368. package/src/client/pdfjs/cmaps/KSC-EUC-V.bcmap +0 -0
  369. package/src/client/pdfjs/cmaps/KSC-H.bcmap +0 -0
  370. package/src/client/pdfjs/cmaps/KSC-Johab-H.bcmap +0 -0
  371. package/src/client/pdfjs/cmaps/KSC-Johab-V.bcmap +0 -0
  372. package/src/client/pdfjs/cmaps/KSC-V.bcmap +0 -0
  373. package/src/client/pdfjs/cmaps/KSCms-UHC-H.bcmap +0 -0
  374. package/src/client/pdfjs/cmaps/KSCms-UHC-HW-H.bcmap +0 -0
  375. package/src/client/pdfjs/cmaps/KSCms-UHC-HW-V.bcmap +0 -0
  376. package/src/client/pdfjs/cmaps/KSCms-UHC-V.bcmap +0 -0
  377. package/src/client/pdfjs/cmaps/KSCpc-EUC-H.bcmap +0 -0
  378. package/src/client/pdfjs/cmaps/KSCpc-EUC-V.bcmap +0 -0
  379. package/src/client/pdfjs/cmaps/Katakana.bcmap +0 -0
  380. package/src/client/pdfjs/cmaps/LICENSE +36 -0
  381. package/src/client/pdfjs/cmaps/NWP-H.bcmap +0 -0
  382. package/src/client/pdfjs/cmaps/NWP-V.bcmap +0 -0
  383. package/src/client/pdfjs/cmaps/RKSJ-H.bcmap +0 -0
  384. package/src/client/pdfjs/cmaps/RKSJ-V.bcmap +0 -0
  385. package/src/client/pdfjs/cmaps/Roman.bcmap +0 -0
  386. package/src/client/pdfjs/cmaps/UniCNS-UCS2-H.bcmap +0 -0
  387. package/src/client/pdfjs/cmaps/UniCNS-UCS2-V.bcmap +0 -0
  388. package/src/client/pdfjs/cmaps/UniCNS-UTF16-H.bcmap +0 -0
  389. package/src/client/pdfjs/cmaps/UniCNS-UTF16-V.bcmap +0 -0
  390. package/src/client/pdfjs/cmaps/UniCNS-UTF32-H.bcmap +0 -0
  391. package/src/client/pdfjs/cmaps/UniCNS-UTF32-V.bcmap +0 -0
  392. package/src/client/pdfjs/cmaps/UniCNS-UTF8-H.bcmap +0 -0
  393. package/src/client/pdfjs/cmaps/UniCNS-UTF8-V.bcmap +0 -0
  394. package/src/client/pdfjs/cmaps/UniGB-UCS2-H.bcmap +0 -0
  395. package/src/client/pdfjs/cmaps/UniGB-UCS2-V.bcmap +0 -0
  396. package/src/client/pdfjs/cmaps/UniGB-UTF16-H.bcmap +0 -0
  397. package/src/client/pdfjs/cmaps/UniGB-UTF16-V.bcmap +0 -0
  398. package/src/client/pdfjs/cmaps/UniGB-UTF32-H.bcmap +0 -0
  399. package/src/client/pdfjs/cmaps/UniGB-UTF32-V.bcmap +0 -0
  400. package/src/client/pdfjs/cmaps/UniGB-UTF8-H.bcmap +0 -0
  401. package/src/client/pdfjs/cmaps/UniGB-UTF8-V.bcmap +0 -0
  402. package/src/client/pdfjs/cmaps/UniJIS-UCS2-H.bcmap +0 -0
  403. package/src/client/pdfjs/cmaps/UniJIS-UCS2-HW-H.bcmap +0 -0
  404. package/src/client/pdfjs/cmaps/UniJIS-UCS2-HW-V.bcmap +0 -0
  405. package/src/client/pdfjs/cmaps/UniJIS-UCS2-V.bcmap +0 -0
  406. package/src/client/pdfjs/cmaps/UniJIS-UTF16-H.bcmap +0 -0
  407. package/src/client/pdfjs/cmaps/UniJIS-UTF16-V.bcmap +0 -0
  408. package/src/client/pdfjs/cmaps/UniJIS-UTF32-H.bcmap +0 -0
  409. package/src/client/pdfjs/cmaps/UniJIS-UTF32-V.bcmap +0 -0
  410. package/src/client/pdfjs/cmaps/UniJIS-UTF8-H.bcmap +0 -0
  411. package/src/client/pdfjs/cmaps/UniJIS-UTF8-V.bcmap +0 -0
  412. package/src/client/pdfjs/cmaps/UniJIS2004-UTF16-H.bcmap +0 -0
  413. package/src/client/pdfjs/cmaps/UniJIS2004-UTF16-V.bcmap +0 -0
  414. package/src/client/pdfjs/cmaps/UniJIS2004-UTF32-H.bcmap +0 -0
  415. package/src/client/pdfjs/cmaps/UniJIS2004-UTF32-V.bcmap +0 -0
  416. package/src/client/pdfjs/cmaps/UniJIS2004-UTF8-H.bcmap +0 -0
  417. package/src/client/pdfjs/cmaps/UniJIS2004-UTF8-V.bcmap +0 -0
  418. package/src/client/pdfjs/cmaps/UniJISPro-UCS2-HW-V.bcmap +0 -0
  419. package/src/client/pdfjs/cmaps/UniJISPro-UCS2-V.bcmap +0 -0
  420. package/src/client/pdfjs/cmaps/UniJISPro-UTF8-V.bcmap +0 -0
  421. package/src/client/pdfjs/cmaps/UniJISX0213-UTF32-H.bcmap +0 -0
  422. package/src/client/pdfjs/cmaps/UniJISX0213-UTF32-V.bcmap +0 -0
  423. package/src/client/pdfjs/cmaps/UniJISX02132004-UTF32-H.bcmap +0 -0
  424. package/src/client/pdfjs/cmaps/UniJISX02132004-UTF32-V.bcmap +0 -0
  425. package/src/client/pdfjs/cmaps/UniKS-UCS2-H.bcmap +0 -0
  426. package/src/client/pdfjs/cmaps/UniKS-UCS2-V.bcmap +0 -0
  427. package/src/client/pdfjs/cmaps/UniKS-UTF16-H.bcmap +0 -0
  428. package/src/client/pdfjs/cmaps/UniKS-UTF16-V.bcmap +0 -0
  429. package/src/client/pdfjs/cmaps/UniKS-UTF32-H.bcmap +0 -0
  430. package/src/client/pdfjs/cmaps/UniKS-UTF32-V.bcmap +0 -0
  431. package/src/client/pdfjs/cmaps/UniKS-UTF8-H.bcmap +0 -0
  432. package/src/client/pdfjs/cmaps/UniKS-UTF8-V.bcmap +0 -0
  433. package/src/client/pdfjs/cmaps/V.bcmap +0 -0
  434. package/src/client/pdfjs/cmaps/WP-Symbol.bcmap +0 -0
  435. package/src/client/pdfjs/pdf.mjs +26779 -0
  436. package/src/client/pdfjs/pdf.worker.mjs +63420 -0
  437. package/src/client/pdfjs/standard_fonts/FoxitDingbats.pfb +0 -0
  438. package/src/client/pdfjs/standard_fonts/FoxitFixed.pfb +0 -0
  439. package/src/client/pdfjs/standard_fonts/FoxitFixedBold.pfb +0 -0
  440. package/src/client/pdfjs/standard_fonts/FoxitFixedBoldItalic.pfb +0 -0
  441. package/src/client/pdfjs/standard_fonts/FoxitFixedItalic.pfb +0 -0
  442. package/src/client/pdfjs/standard_fonts/FoxitSerif.pfb +0 -0
  443. package/src/client/pdfjs/standard_fonts/FoxitSerifBold.pfb +0 -0
  444. package/src/client/pdfjs/standard_fonts/FoxitSerifBoldItalic.pfb +0 -0
  445. package/src/client/pdfjs/standard_fonts/FoxitSerifItalic.pfb +0 -0
  446. package/src/client/pdfjs/standard_fonts/FoxitSymbol.pfb +0 -0
  447. package/src/client/pdfjs/standard_fonts/LICENSE_FOXIT +27 -0
  448. package/src/client/pdfjs/standard_fonts/LICENSE_LIBERATION +102 -0
  449. package/src/client/pdfjs/standard_fonts/LiberationSans-Bold.ttf +0 -0
  450. package/src/client/pdfjs/standard_fonts/LiberationSans-BoldItalic.ttf +0 -0
  451. package/src/client/pdfjs/standard_fonts/LiberationSans-Italic.ttf +0 -0
  452. package/src/client/pdfjs/standard_fonts/LiberationSans-Regular.ttf +0 -0
  453. package/src/client/pdfjs/wasm/LICENSE_JBIG2 +196 -0
  454. package/src/client/pdfjs/wasm/LICENSE_OPENJPEG +39 -0
  455. package/src/client/pdfjs/wasm/LICENSE_PDFJS_JBIG2 +13 -0
  456. package/src/client/pdfjs/wasm/LICENSE_PDFJS_OPENJPEG +22 -0
  457. package/src/client/pdfjs/wasm/LICENSE_PDFJS_QCMS +22 -0
  458. package/src/client/pdfjs/wasm/LICENSE_QCMS +21 -0
  459. package/src/client/pdfjs/wasm/jbig2.wasm +0 -0
  460. package/src/client/pdfjs/wasm/openjpeg.wasm +0 -0
  461. package/src/client/pdfjs/wasm/openjpeg_nowasm_fallback.js +17 -0
  462. package/src/client/pdfjs/wasm/qcms_bg.wasm +0 -0
  463. package/src/client/todo-lifecycle-ui.js +58 -0
  464. package/src/client/workbench-features.js +148 -0
  465. package/src/client/workbench-relations.js +86 -0
  466. package/src/client/workbench-theme.css +81 -0
  467. package/src/client/workbench-ui.js +1761 -0
  468. package/src/host/capture-surface.js +2 -0
  469. package/src/host/citation-guard.js +40 -0
  470. package/src/host/command-description.js +43 -0
  471. package/src/host/flow-workspace.js +62 -0
  472. package/src/host/flows.js +2 -0
  473. package/src/host/fsindex.js +2 -0
  474. package/src/host/gateway.js +916 -0
  475. package/src/host/import-service.js +2 -0
  476. package/src/host/importer.js +2 -0
  477. package/src/host/index-maintenance.js +2 -0
  478. package/src/host/index-policy-service.js +2 -0
  479. package/src/host/index-policy.js +2 -0
  480. package/src/host/index.js +2220 -0
  481. package/src/host/kbcore.js +2 -0
  482. package/src/host/managed-files.js +2 -0
  483. package/src/host/map.js +2 -0
  484. package/src/host/parse-credentials.js +2 -0
  485. package/src/host/parse-discovery.js +2 -0
  486. package/src/host/parse-native.js +2 -0
  487. package/src/host/parse-presets.js +2 -0
  488. package/src/host/parse-source-authority.js +2 -0
  489. package/src/host/parsing.js +7 -0
  490. package/src/host/relation-name-lookup.js +2 -0
  491. package/src/host/retrieval-api.js +2 -0
  492. package/src/host/retrieval-cache.js +2 -0
  493. package/src/host/retrieval-chunks.js +2 -0
  494. package/src/host/retrieval-pool.js +2 -0
  495. package/src/host/retrieval-worker.js +2 -0
  496. package/src/host/retrieval.js +2 -0
  497. package/src/host/runtime-resources.js +2 -0
  498. package/src/host/selftest.js +747 -0
  499. package/src/host/storage-io.js +2 -0
  500. package/src/host/system-upgrades.js +2 -0
  501. package/src/host/todo-lifecycle.js +66 -0
  502. package/src/host/updates.js +281 -0
  503. package/src/host/versioning.js +2 -0
  504. package/src/host/workbench-backup.js +2 -0
  505. package/src/host/workbench-deliverables.js +2 -0
  506. package/src/host/workbench-files.js +2 -0
  507. package/src/host/workbench-inbox.js +2 -0
  508. package/src/host/workbench-links.js +2 -0
  509. package/src/host/workbench-question.js +2 -0
  510. package/src/host/workbench-relation-index.js +2 -0
  511. package/src/host/workbench-state.js +2 -0
  512. package/src/host/workbench-trash.js +2 -0
  513. package/src/host/workbench.js +5 -0
  514. package/tools/kb-local-model.py +141 -0
  515. package/tools/kb-pdf-pages.py +78 -0
  516. package/tools/kb-transactional-update.py +292 -0
@@ -0,0 +1,486 @@
1
+ // 导入服务(R1):UI(设置页 /kb-core/api)与模型工具(kb_import)共用的**唯一**执行入口。
2
+ //
3
+ // 关键不变量(对应审查意见 R1 与复核意见第 1 点):
4
+ // 1. 任务清单自带目标身份:{target, targetKbId, taskStateDir};执行方用**独立 core** 写到目标库,
5
+ // 不读、不改共享的「当前绑定」。
6
+ // 2. 独立 core 不等于目标已固定:它的状态目录里可能残留旧 config.json(kbPath 指向别的库),
7
+ // 所以创建后必须 loadConfig + status,核对**实际根目录**与 kbId;不一致立即拒绝(不写一个字节)。
8
+ // 3. 切库(commit)放在导入完成且核验通过之后;且只在「当前绑定仍是开始时那一个」时提交,
9
+ // 用户中途换库不会被覆盖。
10
+ // 4. 失败 / 取消 / 异常一律不碰当前绑定(没有"先切再恢复"这一步)。
11
+ import path from 'node:path'
12
+ import { mkdir } from 'node:fs/promises'
13
+ import { createCore } from './kbcore.js'
14
+ import { inspectFolder, planImport, runImport, readManifest, saveManifest, cancelImport, listImports, isValidImportId, resolveImportDir } from './importer.js'
15
+
16
+ export const TERMINAL_STAGES = new Set(['done', 'partial', 'failed', 'canceled', 'conflict'])
17
+ const norm = (p) => String(p ?? '').replace(/\\/g, '/').toLowerCase()
18
+ // All service instances for one profile share admission and running state.
19
+ const serviceStates = new Map()
20
+
21
+ export function createImportService({ stateDir, getBaseConfig, getCurrentCore = null, log = () => {}, coreFactory = createCore, commitBinding = null, getParsingService = null }) {
22
+ const stateKey = process.platform === 'win32' ? path.resolve(stateDir).toLowerCase() : path.resolve(stateDir)
23
+ if (!serviceStates.has(stateKey)) serviceStates.set(stateKey, { jobs: new Map(), starting: new Map() })
24
+ const { jobs, starting } = serviceStates.get(stateKey)
25
+ const taskStateDirOf = (importId) => path.join(stateDir, 'imports', importId, 'core-state')
26
+ const isActive = (importId) => Boolean(jobs.get(importId) && jobs.get(importId).running)
27
+
28
+ const currentCoreSafe = async () => (typeof getCurrentCore === 'function' ? await getCurrentCore() : null)
29
+ const currentStatus = async () => {
30
+ const c = await currentCoreSafe().catch(() => null)
31
+ if (!c) return null
32
+ return await c.status().catch(() => null)
33
+ }
34
+
35
+ /** R2:任务目录在解析阶段就要判(读清单会把"越界"错报成 not-found) */
36
+ async function taskDirError(importId) {
37
+ if (!isValidImportId(importId)) return { reason: 'bad-import-id', error: 'importId 非法(必须形如 imp-xxxxxxxxxx)' }
38
+ const r = await resolveImportDir(stateDir, importId)
39
+ if (!r.ok) return { reason: r.reason, error: '导入任务目录非法:' + r.reason }
40
+ return null
41
+ }
42
+
43
+ async function baseConf() {
44
+ try {
45
+ return (await getBaseConfig()) ?? {}
46
+ } catch {
47
+ return {}
48
+ }
49
+ }
50
+
51
+ /** 为任务创建一个**只指向 manifest.target** 的独立 core,并核对它真实落到的位置。 */
52
+ async function makeTaskCore(manifest) {
53
+ const base = await baseConf()
54
+ const taskStateDir = manifest.taskStateDir || taskStateDirOf(manifest.importId)
55
+ const cfg = { ...base, kbPath: manifest.target, stateDir: taskStateDir }
56
+ const core = coreFactory(cfg, log)
57
+ if (typeof core.loadConfig === 'function') await core.loadConfig()
58
+ const st = await core.status()
59
+ const gotRoot = st && st.kbPath ? path.resolve(st.kbPath) : null
60
+ if (!gotRoot || norm(gotRoot) !== norm(manifest.target)) {
61
+ return {
62
+ ok: false,
63
+ reason: 'target-root-mismatch',
64
+ error: `任务目标的独立 core 实际绑定在 ${st && st.kbPath ? st.kbPath : '(未绑定)'},与清单目标 ${manifest.target} 不一致;已拒绝执行(任务状态目录里可能有旧配置)`,
65
+ actualRoot: st && st.kbPath ? st.kbPath : null,
66
+ taskStateDir,
67
+ }
68
+ }
69
+ if (manifest.targetKbId && st.kbId && manifest.targetKbId !== st.kbId) {
70
+ return {
71
+ ok: false,
72
+ reason: 'target-kbid-mismatch',
73
+ error: `任务目标 kbId=${manifest.targetKbId},独立 core 读到的 kbId=${st.kbId};已拒绝执行(目标库被替换过)`,
74
+ actualKbId: st.kbId,
75
+ taskStateDir,
76
+ }
77
+ }
78
+ return { ok: true, core, status: st, taskStateDir }
79
+ }
80
+
81
+ async function stampTarget(manifest) {
82
+ let changed = false
83
+ if (!manifest.taskStateDir) {
84
+ manifest.taskStateDir = taskStateDirOf(manifest.importId)
85
+ changed = true
86
+ }
87
+ if (!manifest.targetKbId) {
88
+ const base = await baseConf()
89
+ const probe = coreFactory({ ...base, kbPath: manifest.target, stateDir: taskStateDirOf(manifest.importId) }, log)
90
+ if (typeof probe.loadConfig === 'function') await probe.loadConfig()
91
+ const st = await probe.status().catch(() => null)
92
+ const sameRoot = st && st.kbPath && norm(st.kbPath) === norm(manifest.target)
93
+ if (sameRoot && st.kbId) {
94
+ manifest.targetKbId = st.kbId
95
+ changed = true
96
+ }
97
+ }
98
+ if (changed) await saveManifest(stateDir, manifest)
99
+ return manifest
100
+ }
101
+
102
+ /** 冻结清单(只读;R3:必须在建标准结构之前调用)。 */
103
+ async function plan({ source, mode: modeIn, current }) {
104
+ const srcRaw = String(source ?? '').trim()
105
+ if (srcRaw === '') return { ok: false, reason: 'empty-source', error: 'plan 需要 source(绝对路径)' }
106
+ const mode = modeIn === 'copy' ? 'copy' : 'attach'
107
+ const cur = current ?? (await currentStatus())
108
+ if (mode === 'copy' && !(cur && cur.kbPath)) return { ok: false, reason: 'unbound', error: '复制导入需要先绑定目标知识库' }
109
+ const target = mode === 'copy' ? cur.kbPath : path.resolve(srcRaw)
110
+ const manifest = await planImport({ stateDir, source: srcRaw, target, mode, log, isActive: id => isActive(id) || starting.has(id) })
111
+ if (manifest && manifest.error) return { ok: false, reason: manifest.reason ?? 'plan-failed', error: manifest.error }
112
+ if (manifest.running) {
113
+ return { ok: true, running: true, importId: manifest.importId, mode: manifest.mode, source: manifest.source, target: manifest.target, stage: manifest.stage, counts: manifest.counts, note: manifest.note }
114
+ }
115
+ await stampTarget(manifest)
116
+ return {
117
+ ok: true,
118
+ importId: manifest.importId,
119
+ mode: manifest.mode,
120
+ source: manifest.source,
121
+ target: manifest.target,
122
+ targetKbId: manifest.targetKbId ?? null,
123
+ stage: manifest.stage,
124
+ taskStateDir: manifest.taskStateDir ?? null,
125
+ counts: manifest.counts,
126
+ scan: manifest.scan,
127
+ skippedDirs: manifest.skippedDirs,
128
+ skippedFiles: (manifest.skippedFiles ?? []).length,
129
+ }
130
+ }
131
+
132
+ /** 启动(后台执行)。校验顺序:id → 清单 → 目标初始化 → 独立 core 实际身份 → 运行。 */
133
+ async function start({ importId }) {
134
+ const pending = starting.get(importId)
135
+ if (pending) {
136
+ const result = await pending
137
+ return result.ok && result.started
138
+ ? { ...result, started: false, running: isActive(importId), reason: 'already-running' }
139
+ : result
140
+ }
141
+ // Reserve synchronously, before directory/configuration I/O can yield.
142
+ const admission = Promise.resolve().then(() => startLocked({ importId }))
143
+ starting.set(importId, admission)
144
+ try { return await admission } finally { if (starting.get(importId) === admission) starting.delete(importId) }
145
+ }
146
+
147
+ async function startLocked({ importId }) {
148
+ const dirError = await taskDirError(importId)
149
+ if (dirError) return { ok: false, ...dirError }
150
+ const manifest = await readManifest(stateDir, importId)
151
+ if (manifest === null) return { ok: false, reason: 'not-found', error: '找不到导入任务:' + importId }
152
+ if (isActive(importId)) return { ok: true, started: false, running: true, reason: 'already-running', importId, stage: manifest.stage }
153
+ if (manifest.stage === 'done') return { ok: true, started: false, alreadyDone: true, importId, stage: 'done', counts: manifest.counts }
154
+ if (manifest.stage === 'conflict') return { ok: false, reason: 'conflict', error: '上次执行发现源文件在计划后发生变化:请重新 plan 再 run', conflicts: manifest.conflicts ?? [] }
155
+
156
+ // 目标库必须已初始化:没有 kbId 就在目标处初始化(**不切换当前绑定**)
157
+ if (!manifest.targetKbId) {
158
+ const initCore = await currentCoreSafe()
159
+ if (!initCore || typeof initCore.initializeLibrary !== 'function') return { ok: false, reason: 'no-current-core', error: '缺少当前 core,无法初始化任务目标' }
160
+ const init = await initCore.initializeLibrary(manifest.target, { rebuildIndex: true })
161
+ if (!init.ok) return { ok: false, reason: 'target-init-failed', error: '任务目标初始化失败:' + (init.error ?? init.reason ?? 'unknown'), init }
162
+ manifest.targetKbId = init.kbId
163
+ await saveManifest(stateDir, manifest)
164
+ }
165
+ if (!manifest.taskStateDir) {
166
+ manifest.taskStateDir = taskStateDirOf(importId)
167
+ await saveManifest(stateDir, manifest)
168
+ }
169
+
170
+ const made = await makeTaskCore(manifest)
171
+ if (!made.ok) {
172
+ log('error', `导入 ${importId} 拒绝执行:${made.error}`)
173
+ return { ok: false, reason: made.reason, error: made.error, actualRoot: made.actualRoot ?? null, target: manifest.target }
174
+ }
175
+
176
+ const before = await currentStatus()
177
+ const job = { running: true, lastError: null, result: null, target: manifest.target, targetKbId: manifest.targetKbId, startedAt: new Date().toISOString() }
178
+ jobs.set(importId, job)
179
+ job.promise = (async () => {
180
+ let r = null
181
+ try {
182
+ r = await runImport({ core: made.core, stateDir, importId, log })
183
+ } catch (error) {
184
+ job.running = false
185
+ job.lastError = String((error && error.message) ?? error)
186
+ log('error', `导入任务异常:${job.lastError}`)
187
+ // 异常不能让任务永远停在 importing:落一个明确的终态,并把原因写进清单
188
+ try {
189
+ const m = await readManifest(stateDir, importId)
190
+ if (m && !TERMINAL_STAGES.has(m.stage)) {
191
+ m.stage = 'partial'
192
+ m.terminalReasons = [...(m.terminalReasons ?? []), '执行异常:' + job.lastError.slice(0, 160)]
193
+ await saveManifest(stateDir, m)
194
+ }
195
+ } catch {
196
+ /* 清单也写不了时,status() 会用 job.lastError 报 failed(不假装 done) */
197
+ }
198
+ return
199
+ }
200
+ job.result = {
201
+ ok: r.ok === true,
202
+ stage: r.manifest ? r.manifest.stage : null,
203
+ counts: r.manifest ? r.manifest.counts : null,
204
+ canceled: Boolean(r.canceled),
205
+ conflict: Boolean(r.conflict),
206
+ reasons: (r.manifest && r.manifest.terminalReasons) || [],
207
+ }
208
+ if (r.ok !== true) {
209
+ job.running = false
210
+ return
211
+ }
212
+ // running 覆盖到"提交切库"完成:否则轮询会看到 running=false 而 committed 还没写(可观察的竞态)
213
+ try {
214
+ // R1:提交切库只在导入完成且核验通过之后;且当前绑定必须仍是开始时那一个
215
+ const now = await currentStatus()
216
+ if (!now) {
217
+ job.committed = false
218
+ job.commitReason = 'no-status'
219
+ return
220
+ }
221
+ if (norm(now.kbPath) === norm(manifest.target)) {
222
+ job.committed = true
223
+ job.commitReason = 'already-bound'
224
+ return
225
+ }
226
+ const samePath = Boolean(before) && norm(before.kbPath) === norm(now.kbPath)
227
+ const sameKb = Boolean(before) && String(before.kbId ?? '') === String(now.kbId ?? '')
228
+ const beforeRev = before && before.config ? before.config.revision ?? null : null
229
+ const nowRev = now.config ? now.config.revision ?? null : null
230
+ const sameRev = beforeRev === nowRev
231
+ if (!samePath || !sameKb || !sameRev) {
232
+ job.committed = false
233
+ job.commitReason = samePath && sameKb ? 'config-changed' : 'binding-changed'
234
+ log('warn', `导入完成但当前绑定/配置已被用户改动(path=${now.kbPath} rev=${String(beforeRev)}→${String(nowRev)}):不覆盖,保留为待提交(目标 ${manifest.target})`)
235
+ return
236
+ }
237
+ if (typeof commitBinding !== 'function') {
238
+ job.committed = false
239
+ job.commitReason = 'no-commit-hook'
240
+ return
241
+ }
242
+ try {
243
+ const c = await commitBinding(manifest.target, {
244
+ expectedConfigRevision: beforeRev,
245
+ expectedCurrentKbPath: before ? before.kbPath : null,
246
+ expectedCurrentKbId: before ? before.kbId ?? null : null,
247
+ expectedTargetKbId: manifest.targetKbId ?? null,
248
+ })
249
+ job.committed = c && c.ok === true
250
+ job.commitReason = job.committed ? 'committed' : String((c && (c.reason || c.note)) || 'commit-failed')
251
+ job.commit = c
252
+ if (!job.committed) log('warn', `导入完成但切库未成功:${job.commitReason}`)
253
+ } catch (error) {
254
+ job.committed = false
255
+ job.commitReason = 'commit-threw'
256
+ job.commitError = String((error && error.message) ?? error)
257
+ }
258
+ } finally {
259
+ job.running = false
260
+ }
261
+ })()
262
+ return { ok: true, started: true, importId, stage: 'running', mode: manifest.mode, source: manifest.source, target: manifest.target, targetKbId: manifest.targetKbId, taskStateDir: made.taskStateDir }
263
+ }
264
+
265
+ async function status({ importId } = {}) {
266
+ if (importId === undefined || importId === null || String(importId) === '') {
267
+ const imports = await listImports(stateDir)
268
+ return { ok: true, imports: imports.map((m) => ({ ...m, running: isActive(m.importId), terminal: TERMINAL_STAGES.has(m.stage) })) }
269
+ }
270
+ const dirError = await taskDirError(importId)
271
+ if (dirError) return { ok: false, ...dirError }
272
+ const manifest = await readManifest(stateDir, importId)
273
+ if (manifest === null) return { ok: false, reason: 'not-found', error: '找不到导入任务:' + importId }
274
+ const job = jobs.get(importId) ?? null
275
+ const counts = manifest.counts
276
+ // 异常中断(job 已停但清单还写着 importing)→ 如实报 failed,不冒充"仍在跑"也不冒充 done
277
+ const endedByError = Boolean(job && !job.running && job.lastError)
278
+ const stage = endedByError ? 'failed' : manifest.stage
279
+ const terminalReasons = endedByError ? [...(manifest.terminalReasons ?? []), '执行异常:' + String(job.lastError).slice(0, 160)] : manifest.terminalReasons ?? []
280
+ return {
281
+ ok: true,
282
+ importId,
283
+ running: Boolean(job && job.running),
284
+ stage,
285
+ manifestStage: manifest.stage,
286
+ mode: manifest.mode,
287
+ source: manifest.source,
288
+ target: manifest.target,
289
+ targetKbId: manifest.targetKbId ?? null,
290
+ taskStateDir: manifest.taskStateDir ?? null,
291
+ counts,
292
+ scan: manifest.scan,
293
+ verify: manifest.verify ?? null,
294
+ terminalReasons,
295
+ conflicts: (manifest.conflicts ?? []).slice(0, 20),
296
+ lastError: job ? job.lastError : null,
297
+ committed: job ? job.committed === true : false,
298
+ commitReason: job ? job.commitReason ?? null : null,
299
+ recent: manifest.items.filter((it) => it.status === 'failed').slice(0, 10).map((it) => ({ rel: it.rel, error: it.error })),
300
+ samples: manifest.items.filter((it) => it.status === 'done').slice(0, 8).map((it) => ({ rel: it.rel, entryPath: it.entryPath })),
301
+ }
302
+ }
303
+
304
+ async function cancel({ importId } = {}) {
305
+ const dirError = await taskDirError(importId)
306
+ if (dirError) return { ok: false, ...dirError }
307
+ return await cancelImport(stateDir, importId)
308
+ }
309
+
310
+ // ── 第二轮:待解析项 → 解析任务(与工具/设置页/流程同一个任务服务)───────────
311
+ /** 列出导入清单里"待解析/待识别"的项,并标出解析是否可用。 */
312
+ async function waiting({ importId } = {}) {
313
+ const dirError = await taskDirError(importId)
314
+ if (dirError) return { ok: false, ...dirError }
315
+ const manifest = await readManifest(stateDir, importId)
316
+ if (!manifest) return { ok: false, reason: 'not-found', error: '没有这个导入任务' }
317
+ const cur = await currentStatus()
318
+ const sameKb = Boolean(cur && cur.kbId && manifest.targetKbId && cur.kbId === manifest.targetKbId)
319
+ const parsing = typeof getParsingService === 'function' ? getParsingService() : null
320
+ const items = (manifest.items ?? [])
321
+ .filter((it) => ['doc', 'media', 'other'].includes(it.class))
322
+ .slice(0, 200)
323
+ .map((it) => ({
324
+ rel: it.rel,
325
+ class: it.class,
326
+ bytes: it.bytes,
327
+ suggestedKind: suggestParseKind(it),
328
+ parseJobId: it.parseJobId ?? null,
329
+ parseStatus: it.parseStatus ?? null,
330
+ entryId: it.entryId ?? null,
331
+ }))
332
+ return {
333
+ ok: true,
334
+ importId,
335
+ stage: manifest.stage,
336
+ targetKbId: manifest.targetKbId ?? null,
337
+ currentKbId: (cur && cur.kbId) || null,
338
+ parseAvailable: Boolean(parsing) && sameKb,
339
+ note: sameKb
340
+ ? parsing
341
+ ? '可以解析:解析结果落隔离底稿,落库另一步'
342
+ : '解析服务未就绪'
343
+ : '该导入任务的目标库不是当前绑定的库:解析只对当前绑定库内的原件开放(不提供任意路径解析)',
344
+ items,
345
+ }
346
+ }
347
+
348
+ /** 把一个待解析项交给解析服务(不直接落库;落库仍是显式一步)。 */
349
+ async function parseItem({ importId, rel, kind, providerId } = {}, { requestedBy } = {}) {
350
+ const dirError = await taskDirError(importId)
351
+ if (dirError) return { ok: false, ...dirError }
352
+ const manifest = await readManifest(stateDir, importId)
353
+ if (!manifest) return { ok: false, reason: 'not-found', error: '没有这个导入任务' }
354
+ const item = (manifest.items ?? []).find((it) => norm(it.rel) === norm(rel))
355
+ if (!item) return { ok: false, reason: 'item-not-found', error: '导入清单里没有这一项:' + String(rel) }
356
+ const cur = await currentStatus()
357
+ if (!cur || !cur.kbId || !manifest.targetKbId || cur.kbId !== manifest.targetKbId) {
358
+ return { ok: false, reason: 'PARSE_TARGET_NOT_BOUND', error: '该导入任务的目标库不是当前绑定的库:拒绝对非当前绑定库的路径发起解析' }
359
+ }
360
+ const parsing = typeof getParsingService === 'function' ? getParsingService() : null
361
+ if (!parsing) return { ok: false, reason: 'PARSE_NOT_ENABLED', error: '解析服务未就绪' }
362
+ const sourceRel = String(item.originalPath || item.rel)
363
+ const kindUse = kind || suggestParseKind(item)
364
+ const submitted = await parsing.submit(
365
+ { sourceRel, kind: kindUse, providerId, purpose: 'import-item' },
366
+ { requestedBy: requestedBy?.via === 'tool'
367
+ ? { via: 'tool', origin: 'import', importId, gate: requestedBy.gate ?? null, session: requestedBy.session ?? null, agent: requestedBy.agent ?? null }
368
+ : { via: 'import', importId } },
369
+ )
370
+ if (submitted.ok !== true) return { ...submitted, rel: item.rel }
371
+ item.parseJobId = submitted.jobId
372
+ item.parseStatus = submitted.status
373
+ await saveManifest(stateDir, manifest)
374
+ return { ok: true, importId, rel: item.rel, kind: kindUse, sourceRel, jobId: submitted.jobId, status: submitted.status, deduped: submitted.deduped === true }
375
+ }
376
+
377
+ function suggestParseKind(item) {
378
+ const ext = String(item.rel ?? '').split('.').pop().toLowerCase()
379
+ if (['wav', 'mp3', 'm4a', 'flac', 'ogg', 'aac', 'mp4'].includes(ext)) return 'asr'
380
+ return 'ocr'
381
+ }
382
+
383
+ /**
384
+ * 「自动建库 / 识别」一次点击(S4 的两个主动作之二)。
385
+ * R3 的关键顺序:**先冻结清单,再建标准结构**;R1:导入完成并通过核验后才切换当前库。
386
+ */
387
+ async function setupFolder(folderRaw, modeIn) {
388
+ const raw = String(folderRaw ?? '').trim()
389
+ if (raw === '') return { ok: false, blocked: true, action: 'blocked', note: '路径为空:请先选择或粘贴一个文件夹绝对路径' }
390
+ const mode = modeIn === 'copy' ? 'copy' : 'attach'
391
+ let info = await inspectFolder(raw)
392
+ const base = { ok: false, folder: info.path, kind: info.kind, reasons: info.reasons, info }
393
+ if (info.kind === 'missing') {
394
+ if (info.creatable !== true) return { ...base, blocked: true, action: 'blocked', note: info.reasons.join(';') || '目录不存在且上级不可写' }
395
+ try {
396
+ await mkdir(path.resolve(raw), { recursive: true })
397
+ } catch (error) {
398
+ return { ...base, blocked: true, action: 'create-failed', note: '创建目录失败:' + String((error && (error.code || error.message)) ?? error) }
399
+ }
400
+ info = await inspectFolder(raw)
401
+ if (!info.exists) return { ...base, blocked: true, action: 'create-failed', note: '创建后仍无法识别目录' }
402
+ }
403
+ const base2 = { ok: false, folder: info.path, kind: info.kind, reasons: info.reasons, info }
404
+ if (['not-a-directory', 'incompatible', 'corrupt'].includes(info.kind)) {
405
+ return { ...base2, blocked: true, action: 'blocked', note: info.kind === 'incompatible' || info.kind === 'corrupt' ? '元数据不兼容或已损坏:已拒绝初始化,未改动任何文件' : info.reasons.join(';') }
406
+ }
407
+ if (!info.writable) return { ...base2, blocked: true, action: 'blocked', note: info.reasons.join(';') || '目录不可写' }
408
+
409
+ const cur = await currentStatus()
410
+ const prev = cur && cur.kbPath && norm(cur.kbPath) !== norm(info.path) ? cur : null
411
+ const restore = async () => {
412
+ if (!prev || typeof commitBinding !== 'function') return false
413
+ const now = await currentStatus()
414
+ // 只在「当前绑定仍是那次失败的候选」时回退,不覆盖用户后来的选择
415
+ if (!now || norm(now.kbPath) !== norm(info.path)) return false
416
+ const r = await commitBinding(prev.kbPath, { expectedKbId: prev.kbId ?? null })
417
+ return Boolean(r && r.ok)
418
+ }
419
+
420
+ if (info.kind === 'ours' || info.kind === 'ours-partial') {
421
+ let init = null
422
+ if (info.kind === 'ours-partial') {
423
+ init = await (await currentCoreSafe()).initializeLibrary(info.path, { rebuildIndex: true }).catch((error) => ({ ok: false, reason: 'init-threw', error: String((error && error.message) ?? error) }))
424
+ if (!init.ok) return { ...base2, ok: false, action: 'attach-failed', failed: '初始化未完成:' + (init.error ?? init.reason ?? '') }
425
+ }
426
+ const bound = await (await currentCoreSafe()).bind(info.path, { confirmInit: false })
427
+ if (!bound || bound.ok !== true) return { ...base2, ok: false, action: 'attach-failed', failed: (bound && (bound.note || bound.reason)) || '绑定失败' }
428
+ const after = await currentStatus()
429
+ if (!after || after.healthy !== true) {
430
+ const restored = await restore()
431
+ return { ...base2, ok: false, action: 'attach-failed', failed: '库结构仍不健康:' + ((after && after.healthReason) || 'unknown'), init, bindingRestored: restored }
432
+ }
433
+ return { ...base2, ok: true, action: info.kind === 'ours' ? 'attached' : 'attached-repaired', kbId: after.kbId ?? null, init, after: { kbPath: after.kbPath, kbId: after.kbId ?? null, healthy: after.healthy } }
434
+ }
435
+
436
+ if (info.kind === 'empty') {
437
+ const currentCore = await currentCoreSafe()
438
+ const init = await currentCore.initializeLibrary(info.path, { rebuildIndex: true })
439
+ if (!init.ok) return { ...base2, ok: false, action: 'create-failed', failed: '建库失败:' + (init.error ?? init.reason ?? '') }
440
+ const bound = await currentCore.bind(info.path, { confirmInit: false })
441
+ if (!bound || bound.ok !== true) {
442
+ const restored = await restore()
443
+ return { ...base2, ok: false, action: 'create-failed', failed: (bound && (bound.note || bound.reason)) || '绑定失败', bindingRestored: restored }
444
+ }
445
+ const after = await currentCore.status()
446
+ if (after.healthy !== true) {
447
+ const restored = await restore()
448
+ return { ...base2, ok: false, action: 'create-failed', failed: '建库后库结构仍不健康:' + (after.healthReason || 'unknown'), bindingRestored: restored }
449
+ }
450
+ return { ...base2, ok: true, action: 'created', kbId: init.kbId ?? null, index: { rebuilt: true, entries: after.counts ? after.counts.entries : 0 }, after: { kbPath: after.kbPath, kbId: after.kbId ?? null, healthy: after.healthy } }
451
+ }
452
+
453
+ // foreign:先冻结清单(此时还没有标准结构),再建库结构,再后台导入
454
+ if (mode === 'copy') {
455
+ if (!cur || !cur.kbPath) return { ...base2, blocked: true, action: 'blocked', note: '复制导入需要先绑定目标知识库' }
456
+ const planned = await plan({ source: info.path, mode, current: cur })
457
+ if (!planned.ok) return { ...base2, ok: false, action: 'plan-failed', failed: planned.error ?? planned.reason }
458
+ const started = await start({ importId: planned.importId })
459
+ return { ...base2, ok: started.ok === true, action: started.ok ? 'import-started' : 'import-failed', importId: planned.importId, mode, counts: planned.counts, scan: planned.scan, skippedDirs: planned.skippedDirs, started, note: started.ok ? '复制导入已开始(目标 = 当前库);完成后按状态查看' : started.error }
460
+ }
461
+ const planned = await plan({ source: info.path, mode: 'attach', current: cur })
462
+ if (!planned.ok) return { ...base2, ok: false, action: 'plan-failed', failed: planned.error ?? planned.reason }
463
+ const currentCore = await currentCoreSafe()
464
+ const init = await currentCore.initializeLibrary(info.path, { rebuildIndex: false })
465
+ if (!init.ok) return { ...base2, ok: false, action: 'create-failed', failed: '建标准结构失败:' + (init.error ?? init.reason ?? '') }
466
+ const manifest = await readManifest(stateDir, planned.importId)
467
+ manifest.targetKbId = init.kbId
468
+ await saveManifest(stateDir, manifest)
469
+ const started = await start({ importId: planned.importId })
470
+ return {
471
+ ...base2,
472
+ ok: started.ok === true,
473
+ action: started.ok ? 'import-started' : 'import-failed',
474
+ importId: planned.importId,
475
+ mode,
476
+ targetKbId: init.kbId,
477
+ counts: planned.counts,
478
+ scan: planned.scan,
479
+ skippedDirs: planned.skippedDirs,
480
+ started,
481
+ note: started.ok ? '已建标准结构并开始导入;导入完成且核验通过后才切换当前库' : started.error,
482
+ }
483
+ }
484
+
485
+ return { plan, start, status, cancel, setupFolder, isActive, jobs, waiting, parseItem }
486
+ }