@waq666/pneuma 0.0.0-stage → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (687) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +144 -2
  3. package/bin/dsc.js +91 -0
  4. package/bin/headless.cjs +40 -0
  5. package/lib/adapter/core-runtime.d.ts +11 -0
  6. package/lib/adapter/core-runtime.js +259 -0
  7. package/lib/adapter/host.d.ts +18 -0
  8. package/lib/adapter/host.js +14 -0
  9. package/lib/adapter/runtime.d.ts +15 -0
  10. package/lib/adapter/runtime.js +290 -0
  11. package/lib/adapter/transcript.d.ts +184 -0
  12. package/lib/adapter/transcript.js +605 -0
  13. package/lib/app/AgentsOverlay.d.ts +18 -0
  14. package/lib/app/AgentsOverlay.js +63 -0
  15. package/lib/app/App.d.ts +13 -0
  16. package/lib/app/App.js +1498 -0
  17. package/lib/app/ApprovalCard.d.ts +12 -0
  18. package/lib/app/ApprovalCard.js +76 -0
  19. package/lib/app/AskCard.d.ts +20 -0
  20. package/lib/app/AskCard.js +45 -0
  21. package/lib/app/ChatView.d.ts +32 -0
  22. package/lib/app/ChatView.js +198 -0
  23. package/lib/app/Composer.d.ts +50 -0
  24. package/lib/app/Composer.js +282 -0
  25. package/lib/app/DiffBlock.d.ts +6 -0
  26. package/lib/app/DiffBlock.js +37 -0
  27. package/lib/app/MarkdownView.d.ts +6 -0
  28. package/lib/app/MarkdownView.js +99 -0
  29. package/lib/app/ModelPicker.d.ts +7 -0
  30. package/lib/app/ModelPicker.js +18 -0
  31. package/lib/app/PlanReviewCard.d.ts +13 -0
  32. package/lib/app/PlanReviewCard.js +54 -0
  33. package/lib/app/PresetPicker.d.ts +11 -0
  34. package/lib/app/PresetPicker.js +19 -0
  35. package/lib/app/PreviewOverlay.d.ts +14 -0
  36. package/lib/app/PreviewOverlay.js +22 -0
  37. package/lib/app/SessionPicker.d.ts +43 -0
  38. package/lib/app/SessionPicker.js +56 -0
  39. package/lib/app/SettingsOverlay.d.ts +32 -0
  40. package/lib/app/SettingsOverlay.js +157 -0
  41. package/lib/app/SkillsPicker.d.ts +7 -0
  42. package/lib/app/SkillsPicker.js +19 -0
  43. package/lib/app/StatusBar.d.ts +22 -0
  44. package/lib/app/StatusBar.js +272 -0
  45. package/lib/app/TaskStrips.d.ts +13 -0
  46. package/lib/app/TaskStrips.js +40 -0
  47. package/lib/app/ToolCard.d.ts +5 -0
  48. package/lib/app/ToolCard.js +40 -0
  49. package/lib/app/TranscriptOverlay.d.ts +12 -0
  50. package/lib/app/TranscriptOverlay.js +17 -0
  51. package/lib/app/Welcome.d.ts +6 -0
  52. package/lib/app/Welcome.js +66 -0
  53. package/lib/app/attach.d.ts +16 -0
  54. package/lib/app/attach.js +58 -0
  55. package/lib/app/click.d.ts +27 -0
  56. package/lib/app/click.js +89 -0
  57. package/lib/app/clipboard-image.d.ts +11 -0
  58. package/lib/app/clipboard-image.js +66 -0
  59. package/lib/app/cursor.d.ts +4 -0
  60. package/lib/app/cursor.js +24 -0
  61. package/lib/app/hello.d.ts +11 -0
  62. package/lib/app/hello.js +8 -0
  63. package/lib/app/history-store.d.ts +4 -0
  64. package/lib/app/history-store.js +41 -0
  65. package/lib/app/image-blocks.d.ts +12 -0
  66. package/lib/app/image-blocks.js +125 -0
  67. package/lib/app/markdown.d.ts +54 -0
  68. package/lib/app/markdown.js +189 -0
  69. package/lib/app/settings-model.d.ts +69 -0
  70. package/lib/app/settings-model.js +114 -0
  71. package/lib/app/theme.d.ts +166 -0
  72. package/lib/app/theme.js +141 -0
  73. package/lib/boot.d.ts +1 -0
  74. package/lib/boot.js +34 -0
  75. package/lib/commands.d.ts +49 -0
  76. package/lib/commands.js +115 -0
  77. package/lib/contract.d.ts +1973 -0
  78. package/lib/contract.js +36 -0
  79. package/lib/core/agent-roles.d.ts +52 -0
  80. package/lib/core/agent-roles.js +274 -0
  81. package/lib/core/approval-floor.d.ts +183 -0
  82. package/lib/core/approval-floor.js +679 -0
  83. package/lib/core/approval.d.ts +42 -0
  84. package/lib/core/approval.js +8 -0
  85. package/lib/core/audit.d.ts +61 -0
  86. package/lib/core/audit.js +59 -0
  87. package/lib/core/boot-notices.d.ts +6 -0
  88. package/lib/core/boot-notices.js +61 -0
  89. package/lib/core/cdp/actions.d.ts +182 -0
  90. package/lib/core/cdp/actions.js +649 -0
  91. package/lib/core/cdp/launch.d.ts +192 -0
  92. package/lib/core/cdp/launch.js +491 -0
  93. package/lib/core/cdp/snapshot.d.ts +139 -0
  94. package/lib/core/cdp/snapshot.js +316 -0
  95. package/lib/core/cdp/transport.d.ts +160 -0
  96. package/lib/core/cdp/transport.js +359 -0
  97. package/lib/core/command-policy.d.ts +108 -0
  98. package/lib/core/command-policy.js +580 -0
  99. package/lib/core/commands-completion.d.ts +78 -0
  100. package/lib/core/commands-completion.js +167 -0
  101. package/lib/core/compact-anchors.d.ts +76 -0
  102. package/lib/core/compact-anchors.js +284 -0
  103. package/lib/core/compact.d.ts +110 -0
  104. package/lib/core/compact.js +210 -0
  105. package/lib/core/config-store.d.ts +38 -0
  106. package/lib/core/config-store.js +307 -0
  107. package/lib/core/config.d.ts +36 -0
  108. package/lib/core/config.js +124 -0
  109. package/lib/core/connection-test.d.ts +60 -0
  110. package/lib/core/connection-test.js +84 -0
  111. package/lib/core/desktop-control.d.ts +298 -0
  112. package/lib/core/desktop-control.js +1311 -0
  113. package/lib/core/diff-text.d.ts +74 -0
  114. package/lib/core/diff-text.js +257 -0
  115. package/lib/core/dock-browser.d.ts +137 -0
  116. package/lib/core/dock-browser.js +146 -0
  117. package/lib/core/dock-ops.d.ts +59 -0
  118. package/lib/core/dock-ops.js +104 -0
  119. package/lib/core/dock-session.d.ts +39 -0
  120. package/lib/core/dock-session.js +28 -0
  121. package/lib/core/dsh-compat/hook.d.ts +18 -0
  122. package/lib/core/dsh-compat/hook.js +52 -0
  123. package/lib/core/dsh-compat/resolve-hook.d.ts +5 -0
  124. package/lib/core/dsh-compat/resolve-hook.js +59 -0
  125. package/lib/core/dsh-compat/tools-facade.d.ts +47 -0
  126. package/lib/core/dsh-compat/tools-facade.js +108 -0
  127. package/lib/core/err-text.d.ts +13 -0
  128. package/lib/core/err-text.js +15 -0
  129. package/lib/core/events.d.ts +149 -0
  130. package/lib/core/events.js +1 -0
  131. package/lib/core/git-exec.d.ts +23 -0
  132. package/lib/core/git-exec.js +72 -0
  133. package/lib/core/git-info.d.ts +47 -0
  134. package/lib/core/git-info.js +157 -0
  135. package/lib/core/goal.d.ts +89 -0
  136. package/lib/core/goal.js +189 -0
  137. package/lib/core/hooks.d.ts +272 -0
  138. package/lib/core/hooks.js +818 -0
  139. package/lib/core/host-browser-link.d.ts +59 -0
  140. package/lib/core/host-browser-link.js +9 -0
  141. package/lib/core/host-methods.d.ts +25 -0
  142. package/lib/core/host-methods.js +103 -0
  143. package/lib/core/image-diet.d.ts +77 -0
  144. package/lib/core/image-diet.js +127 -0
  145. package/lib/core/learnings/ledger.d.ts +73 -0
  146. package/lib/core/learnings/ledger.js +273 -0
  147. package/lib/core/learnings/skill-write.d.ts +250 -0
  148. package/lib/core/learnings/skill-write.js +854 -0
  149. package/lib/core/learnings/store.d.ts +102 -0
  150. package/lib/core/learnings/store.js +266 -0
  151. package/lib/core/lifecycle-hooks.d.ts +189 -0
  152. package/lib/core/lifecycle-hooks.js +576 -0
  153. package/lib/core/llm.d.ts +272 -0
  154. package/lib/core/llm.js +548 -0
  155. package/lib/core/lockfile.d.ts +17 -0
  156. package/lib/core/lockfile.js +110 -0
  157. package/lib/core/loop.d.ts +185 -0
  158. package/lib/core/loop.js +587 -0
  159. package/lib/core/lsp/client.d.ts +189 -0
  160. package/lib/core/lsp/client.js +690 -0
  161. package/lib/core/lsp/framing.d.ts +84 -0
  162. package/lib/core/lsp/framing.js +181 -0
  163. package/lib/core/lsp/line-shift.d.ts +28 -0
  164. package/lib/core/lsp/line-shift.js +127 -0
  165. package/lib/core/lsp/manager.d.ts +93 -0
  166. package/lib/core/lsp/manager.js +418 -0
  167. package/lib/core/lsp/normalize.d.ts +32 -0
  168. package/lib/core/lsp/normalize.js +139 -0
  169. package/lib/core/lsp/servers.d.ts +106 -0
  170. package/lib/core/lsp/servers.js +352 -0
  171. package/lib/core/lsp/uri.d.ts +76 -0
  172. package/lib/core/lsp/uri.js +179 -0
  173. package/lib/core/market.d.ts +51 -0
  174. package/lib/core/market.js +358 -0
  175. package/lib/core/mcp.d.ts +201 -0
  176. package/lib/core/mcp.js +836 -0
  177. package/lib/core/memory-eval.d.ts +158 -0
  178. package/lib/core/memory-eval.js +215 -0
  179. package/lib/core/memory.d.ts +266 -0
  180. package/lib/core/memory.js +703 -0
  181. package/lib/core/mention.d.ts +57 -0
  182. package/lib/core/mention.js +129 -0
  183. package/lib/core/microcompact.d.ts +48 -0
  184. package/lib/core/microcompact.js +79 -0
  185. package/lib/core/migrate.d.ts +56 -0
  186. package/lib/core/migrate.js +134 -0
  187. package/lib/core/model-caps.d.ts +83 -0
  188. package/lib/core/model-caps.js +150 -0
  189. package/lib/core/model-discovery.d.ts +19 -0
  190. package/lib/core/model-discovery.js +69 -0
  191. package/lib/core/modes.d.ts +72 -0
  192. package/lib/core/modes.js +152 -0
  193. package/lib/core/native-bridge.d.ts +135 -0
  194. package/lib/core/native-bridge.js +563 -0
  195. package/lib/core/path-policy.d.ts +32 -0
  196. package/lib/core/path-policy.js +248 -0
  197. package/lib/core/permission-presets.d.ts +67 -0
  198. package/lib/core/permission-presets.js +75 -0
  199. package/lib/core/plugin-loader.d.ts +36 -0
  200. package/lib/core/plugin-loader.js +294 -0
  201. package/lib/core/plugin-registry.d.ts +113 -0
  202. package/lib/core/plugin-registry.js +256 -0
  203. package/lib/core/prefs.d.ts +82 -0
  204. package/lib/core/prefs.js +372 -0
  205. package/lib/core/presets.d.ts +80 -0
  206. package/lib/core/presets.js +373 -0
  207. package/lib/core/pricing.d.ts +90 -0
  208. package/lib/core/pricing.js +126 -0
  209. package/lib/core/prompt.d.ts +92 -0
  210. package/lib/core/prompt.js +339 -0
  211. package/lib/core/ptc.d.ts +66 -0
  212. package/lib/core/ptc.js +70 -0
  213. package/lib/core/remote/frames.d.ts +91 -0
  214. package/lib/core/remote/frames.js +174 -0
  215. package/lib/core/remote/notify.d.ts +54 -0
  216. package/lib/core/remote/notify.js +77 -0
  217. package/lib/core/remote/owner.d.ts +32 -0
  218. package/lib/core/remote/owner.js +75 -0
  219. package/lib/core/remote/pairing.d.ts +104 -0
  220. package/lib/core/remote/pairing.js +261 -0
  221. package/lib/core/remote/push.d.ts +80 -0
  222. package/lib/core/remote/push.js +222 -0
  223. package/lib/core/remote/store.d.ts +18 -0
  224. package/lib/core/remote/store.js +59 -0
  225. package/lib/core/remote/tickets.d.ts +20 -0
  226. package/lib/core/remote/tickets.js +49 -0
  227. package/lib/core/remote/uploads.d.ts +63 -0
  228. package/lib/core/remote/uploads.js +161 -0
  229. package/lib/core/request-attribution.d.ts +26 -0
  230. package/lib/core/request-attribution.js +86 -0
  231. package/lib/core/request-invariant.d.ts +61 -0
  232. package/lib/core/request-invariant.js +199 -0
  233. package/lib/core/sandbox/backends.d.ts +64 -0
  234. package/lib/core/sandbox/backends.js +194 -0
  235. package/lib/core/sandbox/execpolicy.d.ts +93 -0
  236. package/lib/core/sandbox/execpolicy.js +452 -0
  237. package/lib/core/sandbox/net-proxy.d.ts +31 -0
  238. package/lib/core/sandbox/net-proxy.js +162 -0
  239. package/lib/core/sandbox/policy.d.ts +211 -0
  240. package/lib/core/sandbox/policy.js +559 -0
  241. package/lib/core/sandbox/win/abi.d.ts +161 -0
  242. package/lib/core/sandbox/win/abi.js +169 -0
  243. package/lib/core/sandbox/win/account.d.ts +71 -0
  244. package/lib/core/sandbox/win/account.js +206 -0
  245. package/lib/core/sandbox/win/acl.d.ts +54 -0
  246. package/lib/core/sandbox/win/acl.js +339 -0
  247. package/lib/core/sandbox/win/backend.d.ts +61 -0
  248. package/lib/core/sandbox/win/backend.js +249 -0
  249. package/lib/core/sandbox/win/doctor.d.ts +24 -0
  250. package/lib/core/sandbox/win/doctor.js +272 -0
  251. package/lib/core/sandbox/win/ffi.d.ts +107 -0
  252. package/lib/core/sandbox/win/ffi.js +166 -0
  253. package/lib/core/sandbox/win/net-abi.d.ts +154 -0
  254. package/lib/core/sandbox/win/net-abi.js +316 -0
  255. package/lib/core/sandbox/win/runner-entry.d.ts +35 -0
  256. package/lib/core/sandbox/win/runner-entry.js +391 -0
  257. package/lib/core/sandbox/win/setup.d.ts +60 -0
  258. package/lib/core/sandbox/win/setup.js +986 -0
  259. package/lib/core/sandbox/win/token.d.ts +35 -0
  260. package/lib/core/sandbox/win/token.js +238 -0
  261. package/lib/core/sandbox/win/wfp.d.ts +41 -0
  262. package/lib/core/sandbox/win/wfp.js +200 -0
  263. package/lib/core/sandbox/win/workspace-sid.d.ts +9 -0
  264. package/lib/core/sandbox/win/workspace-sid.js +45 -0
  265. package/lib/core/schedule/rule.d.ts +184 -0
  266. package/lib/core/schedule/rule.js +745 -0
  267. package/lib/core/schedule/runner.d.ts +185 -0
  268. package/lib/core/schedule/runner.js +678 -0
  269. package/lib/core/schedule/store.d.ts +182 -0
  270. package/lib/core/schedule/store.js +455 -0
  271. package/lib/core/secrets.d.ts +41 -0
  272. package/lib/core/secrets.js +150 -0
  273. package/lib/core/session-export.d.ts +18 -0
  274. package/lib/core/session-export.js +73 -0
  275. package/lib/core/session-index.d.ts +159 -0
  276. package/lib/core/session-index.js +683 -0
  277. package/lib/core/session-meta.d.ts +41 -0
  278. package/lib/core/session-meta.js +97 -0
  279. package/lib/core/session.d.ts +356 -0
  280. package/lib/core/session.js +998 -0
  281. package/lib/core/skills.d.ts +60 -0
  282. package/lib/core/skills.js +171 -0
  283. package/lib/core/spill.d.ts +159 -0
  284. package/lib/core/spill.js +329 -0
  285. package/lib/core/surface-registry.d.ts +45 -0
  286. package/lib/core/surface-registry.js +67 -0
  287. package/lib/core/team-board.d.ts +95 -0
  288. package/lib/core/team-board.js +316 -0
  289. package/lib/core/throughput.d.ts +46 -0
  290. package/lib/core/throughput.js +40 -0
  291. package/lib/core/todo-reminder.d.ts +44 -0
  292. package/lib/core/todo-reminder.js +55 -0
  293. package/lib/core/todo.d.ts +81 -0
  294. package/lib/core/todo.js +222 -0
  295. package/lib/core/token-estimate.d.ts +55 -0
  296. package/lib/core/token-estimate.js +82 -0
  297. package/lib/core/tool-guards.d.ts +103 -0
  298. package/lib/core/tool-guards.js +77 -0
  299. package/lib/core/tool-search.d.ts +152 -0
  300. package/lib/core/tool-search.js +311 -0
  301. package/lib/core/tools/bash.d.ts +281 -0
  302. package/lib/core/tools/bash.js +871 -0
  303. package/lib/core/tools/command-runner.d.ts +84 -0
  304. package/lib/core/tools/command-runner.js +68 -0
  305. package/lib/core/tools/fs-tools.d.ts +23 -0
  306. package/lib/core/tools/fs-tools.js +247 -0
  307. package/lib/core/tools/index.d.ts +8 -0
  308. package/lib/core/tools/index.js +4 -0
  309. package/lib/core/tools/sandbox-args.d.ts +51 -0
  310. package/lib/core/tools/sandbox-args.js +90 -0
  311. package/lib/core/tools/search-tools.d.ts +3 -0
  312. package/lib/core/tools/search-tools.js +153 -0
  313. package/lib/core/tools.d.ts +96 -0
  314. package/lib/core/tools.js +53 -0
  315. package/lib/core/ui-boot-source.d.ts +71 -0
  316. package/lib/core/ui-boot-source.js +121 -0
  317. package/lib/core/ui-bundles.d.ts +249 -0
  318. package/lib/core/ui-bundles.js +830 -0
  319. package/lib/core/ui-combo.d.ts +49 -0
  320. package/lib/core/ui-combo.js +55 -0
  321. package/lib/core/ui-events.d.ts +81 -0
  322. package/lib/core/ui-events.js +364 -0
  323. package/lib/core/ui-hmr.d.ts +36 -0
  324. package/lib/core/ui-hmr.js +128 -0
  325. package/lib/core/ui-methods.d.ts +69 -0
  326. package/lib/core/ui-methods.js +341 -0
  327. package/lib/core/ui-route.d.ts +69 -0
  328. package/lib/core/ui-route.js +122 -0
  329. package/lib/core/ui-server.d.ts +75 -0
  330. package/lib/core/ui-server.js +369 -0
  331. package/lib/core/ui-trust.d.ts +47 -0
  332. package/lib/core/ui-trust.js +164 -0
  333. package/lib/core/untrusted.d.ts +30 -0
  334. package/lib/core/untrusted.js +48 -0
  335. package/lib/core/update-check.d.ts +81 -0
  336. package/lib/core/update-check.js +177 -0
  337. package/lib/core/usage-log.d.ts +82 -0
  338. package/lib/core/usage-log.js +306 -0
  339. package/lib/core/version.d.ts +1 -0
  340. package/lib/core/version.js +37 -0
  341. package/lib/core/web-http.d.ts +57 -0
  342. package/lib/core/web-http.js +137 -0
  343. package/lib/core/web-static.d.ts +31 -0
  344. package/lib/core/web-static.js +75 -0
  345. package/lib/headless.d.ts +1 -0
  346. package/lib/headless.js +31 -0
  347. package/lib/host/kernel.d.ts +33 -0
  348. package/lib/host/kernel.js +451 -0
  349. package/lib/index.d.ts +41 -0
  350. package/lib/index.js +35 -0
  351. package/lib/plugins/agent.d.ts +2 -0
  352. package/lib/plugins/agent.js +274 -0
  353. package/lib/plugins/approval-floor.d.ts +2 -0
  354. package/lib/plugins/approval-floor.js +223 -0
  355. package/lib/plugins/approval.d.ts +16 -0
  356. package/lib/plugins/approval.js +674 -0
  357. package/lib/plugins/ask.d.ts +2 -0
  358. package/lib/plugins/ask.js +245 -0
  359. package/lib/plugins/browser.d.ts +68 -0
  360. package/lib/plugins/browser.js +1341 -0
  361. package/lib/plugins/commands.d.ts +23 -0
  362. package/lib/plugins/commands.js +498 -0
  363. package/lib/plugins/compact.d.ts +21 -0
  364. package/lib/plugins/compact.js +274 -0
  365. package/lib/plugins/computer-use.d.ts +47 -0
  366. package/lib/plugins/computer-use.js +539 -0
  367. package/lib/plugins/desktop-dock.d.ts +2 -0
  368. package/lib/plugins/desktop-dock.js +626 -0
  369. package/lib/plugins/dock-browser.d.ts +34 -0
  370. package/lib/plugins/dock-browser.js +765 -0
  371. package/lib/plugins/dsh-compat.d.ts +25 -0
  372. package/lib/plugins/dsh-compat.js +37 -0
  373. package/lib/plugins/file-review.d.ts +108 -0
  374. package/lib/plugins/file-review.js +393 -0
  375. package/lib/plugins/git.d.ts +26 -0
  376. package/lib/plugins/git.js +132 -0
  377. package/lib/plugins/goal.d.ts +20 -0
  378. package/lib/plugins/goal.js +245 -0
  379. package/lib/plugins/guards.d.ts +14 -0
  380. package/lib/plugins/guards.js +18 -0
  381. package/lib/plugins/hooks.d.ts +19 -0
  382. package/lib/plugins/hooks.js +583 -0
  383. package/lib/plugins/host-stdio.d.ts +88 -0
  384. package/lib/plugins/host-stdio.js +317 -0
  385. package/lib/plugins/image-diet.d.ts +17 -0
  386. package/lib/plugins/image-diet.js +85 -0
  387. package/lib/plugins/lifecycle-hooks.d.ts +26 -0
  388. package/lib/plugins/lifecycle-hooks.js +338 -0
  389. package/lib/plugins/llm.d.ts +13 -0
  390. package/lib/plugins/llm.js +215 -0
  391. package/lib/plugins/lsp.d.ts +54 -0
  392. package/lib/plugins/lsp.js +608 -0
  393. package/lib/plugins/mcp.d.ts +16 -0
  394. package/lib/plugins/mcp.js +673 -0
  395. package/lib/plugins/memory.d.ts +17 -0
  396. package/lib/plugins/memory.js +858 -0
  397. package/lib/plugins/microcompact.d.ts +18 -0
  398. package/lib/plugins/microcompact.js +119 -0
  399. package/lib/plugins/mode.d.ts +18 -0
  400. package/lib/plugins/mode.js +115 -0
  401. package/lib/plugins/permission-presets.d.ts +18 -0
  402. package/lib/plugins/permission-presets.js +135 -0
  403. package/lib/plugins/plan.d.ts +2 -0
  404. package/lib/plugins/plan.js +192 -0
  405. package/lib/plugins/plugin-manager.d.ts +2 -0
  406. package/lib/plugins/plugin-manager.js +165 -0
  407. package/lib/plugins/presets.d.ts +2 -0
  408. package/lib/plugins/presets.js +182 -0
  409. package/lib/plugins/prompt.d.ts +2 -0
  410. package/lib/plugins/prompt.js +264 -0
  411. package/lib/plugins/ptc.d.ts +2 -0
  412. package/lib/plugins/ptc.js +272 -0
  413. package/lib/plugins/remote/http.d.ts +11 -0
  414. package/lib/plugins/remote/http.js +11 -0
  415. package/lib/plugins/remote/routes.d.ts +24 -0
  416. package/lib/plugins/remote/routes.js +384 -0
  417. package/lib/plugins/remote/types.d.ts +135 -0
  418. package/lib/plugins/remote/types.js +87 -0
  419. package/lib/plugins/remote.d.ts +17 -0
  420. package/lib/plugins/remote.js +1028 -0
  421. package/lib/plugins/runtime-api.d.ts +16 -0
  422. package/lib/plugins/runtime-api.js +99 -0
  423. package/lib/plugins/runtime.d.ts +10 -0
  424. package/lib/plugins/runtime.js +451 -0
  425. package/lib/plugins/sandbox.d.ts +2 -0
  426. package/lib/plugins/sandbox.js +761 -0
  427. package/lib/plugins/schedule.d.ts +26 -0
  428. package/lib/plugins/schedule.js +538 -0
  429. package/lib/plugins/self-improve.d.ts +61 -0
  430. package/lib/plugins/self-improve.js +953 -0
  431. package/lib/plugins/session-search.d.ts +16 -0
  432. package/lib/plugins/session-search.js +308 -0
  433. package/lib/plugins/session-title.d.ts +19 -0
  434. package/lib/plugins/session-title.js +109 -0
  435. package/lib/plugins/session.d.ts +7 -0
  436. package/lib/plugins/session.js +296 -0
  437. package/lib/plugins/settings.d.ts +3 -0
  438. package/lib/plugins/settings.js +705 -0
  439. package/lib/plugins/skills.d.ts +2 -0
  440. package/lib/plugins/skills.js +360 -0
  441. package/lib/plugins/spill.d.ts +16 -0
  442. package/lib/plugins/spill.js +200 -0
  443. package/lib/plugins/subagent.d.ts +2 -0
  444. package/lib/plugins/subagent.js +862 -0
  445. package/lib/plugins/surfaces.d.ts +11 -0
  446. package/lib/plugins/surfaces.js +20 -0
  447. package/lib/plugins/team.d.ts +16 -0
  448. package/lib/plugins/team.js +149 -0
  449. package/lib/plugins/todo.d.ts +19 -0
  450. package/lib/plugins/todo.js +188 -0
  451. package/lib/plugins/tool-search.d.ts +2 -0
  452. package/lib/plugins/tool-search.js +425 -0
  453. package/lib/plugins/tools-default.d.ts +12 -0
  454. package/lib/plugins/tools-default.js +275 -0
  455. package/lib/plugins/tools.d.ts +16 -0
  456. package/lib/plugins/tools.js +62 -0
  457. package/lib/plugins/transcript.d.ts +15 -0
  458. package/lib/plugins/transcript.js +311 -0
  459. package/lib/plugins/tui.d.ts +2 -0
  460. package/lib/plugins/tui.js +98 -0
  461. package/lib/plugins/waiting.d.ts +12 -0
  462. package/lib/plugins/waiting.js +34 -0
  463. package/lib/plugins/web-search.d.ts +14 -0
  464. package/lib/plugins/web-search.js +263 -0
  465. package/lib/plugins/webserver.d.ts +48 -0
  466. package/lib/plugins/webserver.js +228 -0
  467. package/lib/remote/assets/app-DAcUCOEn.js +14 -0
  468. package/lib/remote/assets/icon-192.png +0 -0
  469. package/lib/remote/assets/icon-512.png +0 -0
  470. package/lib/remote/assets/index.html +34 -0
  471. package/lib/remote/assets/manifest.json +27 -0
  472. package/lib/remote/assets/style-DIB6729x.css +1 -0
  473. package/lib/remote/assets/sw.js +66 -0
  474. package/lib/services/types.d.ts +1228 -0
  475. package/lib/services/types.js +1 -0
  476. package/lib/tools/config-cli.d.ts +1 -0
  477. package/lib/tools/config-cli.js +89 -0
  478. package/lib/ui/arrival.d.ts +72 -0
  479. package/lib/ui/arrival.js +105 -0
  480. package/lib/ui/assets/app-B70nVIKd.js +69 -0
  481. package/lib/ui/assets/chunk-2UxHyX5q.js +1 -0
  482. package/lib/ui/assets/chunk-B1dDrJ26.js +1 -0
  483. package/lib/ui/assets/chunk-B6aJPvgy.js +1 -0
  484. package/lib/ui/assets/chunk-BEwlwnbL.js +1 -0
  485. package/lib/ui/assets/chunk-BGcgwlCI.js +12 -0
  486. package/lib/ui/assets/chunk-BIGW1oBm.js +1 -0
  487. package/lib/ui/assets/chunk-BPQ3VLAy.js +1 -0
  488. package/lib/ui/assets/chunk-BRH0KZbk.js +141 -0
  489. package/lib/ui/assets/chunk-BX6H_ySx.js +1 -0
  490. package/lib/ui/assets/chunk-B_P3UbPa.js +1 -0
  491. package/lib/ui/assets/chunk-B_TYUkTI.js +1 -0
  492. package/lib/ui/assets/chunk-B_vNuMnf.js +1 -0
  493. package/lib/ui/assets/chunk-BajyYBss.js +10 -0
  494. package/lib/ui/assets/chunk-BdnUsdx6.js +1 -0
  495. package/lib/ui/assets/chunk-Buea-lGh.js +1 -0
  496. package/lib/ui/assets/chunk-C0PvZCCE.js +1 -0
  497. package/lib/ui/assets/chunk-C0m74zRD.js +1 -0
  498. package/lib/ui/assets/chunk-C27-OAKa.js +1 -0
  499. package/lib/ui/assets/chunk-C2oV4EkX.js +1 -0
  500. package/lib/ui/assets/chunk-C7zT0LnQ.js +1 -0
  501. package/lib/ui/assets/chunk-C9GSunVT.js +1 -0
  502. package/lib/ui/assets/chunk-C9tS-k6U.js +1 -0
  503. package/lib/ui/assets/chunk-CHLpvVh8.js +1 -0
  504. package/lib/ui/assets/chunk-CLxacb5B.js +1 -0
  505. package/lib/ui/assets/chunk-COt5Ahok.js +1 -0
  506. package/lib/ui/assets/chunk-CPTIWCL2.js +1 -0
  507. package/lib/ui/assets/chunk-CQLl-L-p.js +1 -0
  508. package/lib/ui/assets/chunk-CUWRgxuq.js +55 -0
  509. package/lib/ui/assets/chunk-CWanVSXC.js +1 -0
  510. package/lib/ui/assets/chunk-Cf5RLm7j.js +1 -0
  511. package/lib/ui/assets/chunk-CickPsom.js +1 -0
  512. package/lib/ui/assets/chunk-Cj5Yp3dK.js +1 -0
  513. package/lib/ui/assets/chunk-Cmh6b_Ma.js +1 -0
  514. package/lib/ui/assets/chunk-Cp-IABpG.js +1 -0
  515. package/lib/ui/assets/chunk-CqE71os6.js +1 -0
  516. package/lib/ui/assets/chunk-CtKP3W-j.js +1 -0
  517. package/lib/ui/assets/chunk-Cvjx9yec.js +1 -0
  518. package/lib/ui/assets/chunk-CxGP3dtY.js +1 -0
  519. package/lib/ui/assets/chunk-CylS5w8V.js +1 -0
  520. package/lib/ui/assets/chunk-D38fbIBN.js +1 -0
  521. package/lib/ui/assets/chunk-D97Zzqfu.js +1 -0
  522. package/lib/ui/assets/chunk-DAi9KRSo.js +1 -0
  523. package/lib/ui/assets/chunk-DHK4yRfg.js +1 -0
  524. package/lib/ui/assets/chunk-DJeabLx-.js +1 -0
  525. package/lib/ui/assets/chunk-DNRxmY5a.js +1 -0
  526. package/lib/ui/assets/chunk-DNSN1z5r.js +1 -0
  527. package/lib/ui/assets/chunk-DNasK_ZG.js +1 -0
  528. package/lib/ui/assets/chunk-DPvrY3IQ.js +1 -0
  529. package/lib/ui/assets/chunk-DXmwc3jG.js +1 -0
  530. package/lib/ui/assets/chunk-Dd0kTxKP.js +1 -0
  531. package/lib/ui/assets/chunk-De0Btta0.js +1 -0
  532. package/lib/ui/assets/chunk-Des-eS-w.js +1 -0
  533. package/lib/ui/assets/chunk-Df6bDoY_.js +1 -0
  534. package/lib/ui/assets/chunk-DijEV5ha.js +1 -0
  535. package/lib/ui/assets/chunk-DkvoVjww.js +1 -0
  536. package/lib/ui/assets/chunk-DlC-OlHO.js +1 -0
  537. package/lib/ui/assets/chunk-DnzrXTVv.js +1 -0
  538. package/lib/ui/assets/chunk-Dualf0xi.js +1 -0
  539. package/lib/ui/assets/chunk-EXT84bkm.js +1 -0
  540. package/lib/ui/assets/chunk-GwR3hkPK.js +1 -0
  541. package/lib/ui/assets/chunk-P80f7IUj.js +1 -0
  542. package/lib/ui/assets/chunk-VOosw3JB.js +1 -0
  543. package/lib/ui/assets/chunk-YQ4lDkt_.js +140 -0
  544. package/lib/ui/assets/chunk-Yzrsuije.js +1 -0
  545. package/lib/ui/assets/chunk-Z72CF80r.js +1 -0
  546. package/lib/ui/assets/chunk-g9-lgVsj.js +1 -0
  547. package/lib/ui/assets/chunk-vGWfd6FD.js +1 -0
  548. package/lib/ui/assets/chunk-wDzz0qaB.js +1 -0
  549. package/lib/ui/assets/chunk-ySF7HWmS.js +1 -0
  550. package/lib/ui/assets/icon.png +0 -0
  551. package/lib/ui/assets/index.html +14 -0
  552. package/lib/ui/assets/pdf.worker.min-CjEcRF4W.mjs +28 -0
  553. package/lib/ui/assets/style-Bq01xH_b.css +1 -0
  554. package/lib/ui/frames.d.ts +67 -0
  555. package/lib/ui/frames.js +146 -0
  556. package/lib/ui/index.d.ts +59 -0
  557. package/lib/ui/index.js +50 -0
  558. package/lib/ui/manifest.d.ts +172 -0
  559. package/lib/ui/manifest.js +158 -0
  560. package/lib/ui/plugin.d.ts +161 -0
  561. package/lib/ui/plugin.js +7 -0
  562. package/lib/ui/plugins/browser/client.js +290 -0
  563. package/lib/ui/plugins/browser/icon.png +0 -0
  564. package/lib/ui/plugins/composer/client.js +1843 -0
  565. package/lib/ui/plugins/composer/icon.png +0 -0
  566. package/lib/ui/plugins/files/client.js +646 -0
  567. package/lib/ui/plugins/files/icon.png +0 -0
  568. package/lib/ui/plugins/manifest.json +107 -0
  569. package/lib/ui/plugins/plugins/client.js +682 -0
  570. package/lib/ui/plugins/plugins/icon.png +0 -0
  571. package/lib/ui/plugins/preview/client.js +197 -0
  572. package/lib/ui/plugins/preview/icon.png +0 -0
  573. package/lib/ui/plugins/prompt/client.js +653 -0
  574. package/lib/ui/plugins/prompt/icon.png +0 -0
  575. package/lib/ui/plugins/sessions/client.js +1577 -0
  576. package/lib/ui/plugins/sessions/icon.png +0 -0
  577. package/lib/ui/plugins/settings/client.js +5470 -0
  578. package/lib/ui/plugins/settings/icon.png +0 -0
  579. package/lib/ui/plugins/skills/client.js +485 -0
  580. package/lib/ui/plugins/skills/icon.png +0 -0
  581. package/lib/ui/plugins/status/client.js +1420 -0
  582. package/lib/ui/plugins/status/icon.png +0 -0
  583. package/lib/ui/plugins/statusbar/client.js +504 -0
  584. package/lib/ui/plugins/statusbar/icon.png +0 -0
  585. package/lib/ui/plugins/tasks/client.js +52 -0
  586. package/lib/ui/plugins/tasks/icon.png +0 -0
  587. package/lib/ui/plugins/team/client.js +527 -0
  588. package/lib/ui/plugins/team/icon.png +0 -0
  589. package/lib/ui/plugins/terminal/client.js +232 -0
  590. package/lib/ui/plugins/terminal/icon.png +0 -0
  591. package/lib/ui/plugins/trace/client.js +1376 -0
  592. package/lib/ui/plugins/trace/icon.png +0 -0
  593. package/lib/ui/plugins/transcript/client.js +2661 -0
  594. package/lib/ui/plugins/transcript/icon.png +0 -0
  595. package/lib/ui/scoped-slots.d.ts +20 -0
  596. package/lib/ui/scoped-slots.js +21 -0
  597. package/lib/ui/sdk/@dsc/ui/index.d.ts +242 -0
  598. package/lib/ui/sdk/@dsc/ui/package.json +16 -0
  599. package/lib/ui/sdk/@dsc/ui-core/contract.d.ts +1973 -0
  600. package/lib/ui/sdk/@dsc/ui-core/core/diff-text.d.ts +74 -0
  601. package/lib/ui/sdk/@dsc/ui-core/core/dock-browser.d.ts +137 -0
  602. package/lib/ui/sdk/@dsc/ui-core/core/dock-ops.d.ts +59 -0
  603. package/lib/ui/sdk/@dsc/ui-core/core/model-caps.d.ts +83 -0
  604. package/lib/ui/sdk/@dsc/ui-core/core/native-bridge.d.ts +135 -0
  605. package/lib/ui/sdk/@dsc/ui-core/core/token-estimate.d.ts +55 -0
  606. package/lib/ui/sdk/@dsc/ui-core/package.json +16 -0
  607. package/lib/ui/sdk/@dsc/ui-core/ui/arrival.d.ts +72 -0
  608. package/lib/ui/sdk/@dsc/ui-core/ui/frames.d.ts +67 -0
  609. package/lib/ui/sdk/@dsc/ui-core/ui/index.d.ts +59 -0
  610. package/lib/ui/sdk/@dsc/ui-core/ui/manifest.d.ts +172 -0
  611. package/lib/ui/sdk/@dsc/ui-core/ui/plugin.d.ts +161 -0
  612. package/lib/ui/sdk/@dsc/ui-core/ui/scoped-slots.d.ts +20 -0
  613. package/lib/ui/sdk/@dsc/ui-core/ui/services-react.d.ts +27 -0
  614. package/lib/ui/sdk/@dsc/ui-core/ui/services.d.ts +63 -0
  615. package/lib/ui/sdk/@dsc/ui-core/ui/slots-react.d.ts +28 -0
  616. package/lib/ui/sdk/@dsc/ui-core/ui/slots.d.ts +481 -0
  617. package/lib/ui/sdk/@dsc/ui-core/ui/store.d.ts +89 -0
  618. package/lib/ui/sdk/@dsc/ui-dockkit/dock-model.d.ts +238 -0
  619. package/lib/ui/sdk/@dsc/ui-dockkit/dock-state.d.ts +163 -0
  620. package/lib/ui/sdk/@dsc/ui-dockkit/dock.d.ts +24 -0
  621. package/lib/ui/sdk/@dsc/ui-dockkit/index.d.ts +15 -0
  622. package/lib/ui/sdk/@dsc/ui-dockkit/package.json +16 -0
  623. package/lib/ui/sdk/@dsc/ui-layout/app.d.ts +74 -0
  624. package/lib/ui/sdk/@dsc/ui-layout/appearance.d.ts +70 -0
  625. package/lib/ui/sdk/@dsc/ui-layout/host-channel.d.ts +112 -0
  626. package/lib/ui/sdk/@dsc/ui-layout/index.d.ts +34 -0
  627. package/lib/ui/sdk/@dsc/ui-layout/package.json +16 -0
  628. package/lib/ui/sdk/@dsc/ui-layout/page-state.d.ts +14 -0
  629. package/lib/ui/sdk/@dsc/ui-layout/review-pane.d.ts +21 -0
  630. package/lib/ui/sdk/@dsc/ui-layout/runtime-context.d.ts +44 -0
  631. package/lib/ui/sdk/@dsc/ui-layout/thread-view.d.ts +28 -0
  632. package/lib/ui/sdk/@dsc/ui-layout/update-button.d.ts +21 -0
  633. package/lib/ui/sdk/@dsc/ui-primitives/code-file-icon-artwork.d.ts +53 -0
  634. package/lib/ui/sdk/@dsc/ui-primitives/code-view.d.ts +15 -0
  635. package/lib/ui/sdk/@dsc/ui-primitives/compat.d.ts +27 -0
  636. package/lib/ui/sdk/@dsc/ui-primitives/completion-sound.d.ts +24 -0
  637. package/lib/ui/sdk/@dsc/ui-primitives/confirm.d.ts +33 -0
  638. package/lib/ui/sdk/@dsc/ui-primitives/css.d.ts +47 -0
  639. package/lib/ui/sdk/@dsc/ui-primitives/diff-rows.d.ts +20 -0
  640. package/lib/ui/sdk/@dsc/ui-primitives/file-icons.d.ts +38 -0
  641. package/lib/ui/sdk/@dsc/ui-primitives/file-type-classify.d.ts +66 -0
  642. package/lib/ui/sdk/@dsc/ui-primitives/file-type-icon.d.ts +57 -0
  643. package/lib/ui/sdk/@dsc/ui-primitives/highlight.d.ts +24 -0
  644. package/lib/ui/sdk/@dsc/ui-primitives/hover-delay.d.ts +17 -0
  645. package/lib/ui/sdk/@dsc/ui-primitives/icons.d.ts +109 -0
  646. package/lib/ui/sdk/@dsc/ui-primitives/index.d.ts +55 -0
  647. package/lib/ui/sdk/@dsc/ui-primitives/lang-table.d.ts +10 -0
  648. package/lib/ui/sdk/@dsc/ui-primitives/markdown.d.ts +20 -0
  649. package/lib/ui/sdk/@dsc/ui-primitives/native.d.ts +41 -0
  650. package/lib/ui/sdk/@dsc/ui-primitives/package.json +16 -0
  651. package/lib/ui/sdk/@dsc/ui-primitives/panels.d.ts +86 -0
  652. package/lib/ui/sdk/@dsc/ui-primitives/paths.d.ts +11 -0
  653. package/lib/ui/sdk/@dsc/ui-primitives/pdf-view.d.ts +13 -0
  654. package/lib/ui/sdk/@dsc/ui-primitives/plugin-text.d.ts +15 -0
  655. package/lib/ui/sdk/@dsc/ui-primitives/select.d.ts +35 -0
  656. package/lib/ui/sdk/@dsc/ui-primitives/table-view.d.ts +23 -0
  657. package/lib/ui/sdk/@dsc/ui-primitives/thread-resizer.d.ts +22 -0
  658. package/lib/ui/sdk/@dsc/ui-primitives/time.d.ts +33 -0
  659. package/lib/ui/sdk/@dsc/ui-primitives/toast.d.ts +16 -0
  660. package/lib/ui/sdk/@dsc/ui-primitives/tokens.d.ts +14 -0
  661. package/lib/ui/sdk/@dsc/ui-primitives/tool-args.d.ts +49 -0
  662. package/lib/ui/sdk/@dsc/ui-primitives/tool-summary.d.ts +64 -0
  663. package/lib/ui/sdk/@dsc/ui-renderer/boot.d.ts +121 -0
  664. package/lib/ui/sdk/@dsc/ui-renderer/hmr.d.ts +43 -0
  665. package/lib/ui/sdk/@dsc/ui-renderer/index.d.ts +20 -0
  666. package/lib/ui/sdk/@dsc/ui-renderer/package.json +16 -0
  667. package/lib/ui/sdk/@dsc/ui-slots/index.d.ts +19 -0
  668. package/lib/ui/sdk/@dsc/ui-slots/package.json +16 -0
  669. package/lib/ui/sdk/@dsc/ui-slots/slots-react.d.ts +48 -0
  670. package/lib/ui/sdk/@dsc/ui-slots/slots.d.ts +102 -0
  671. package/lib/ui/services-react.d.ts +27 -0
  672. package/lib/ui/services-react.js +32 -0
  673. package/lib/ui/services.d.ts +63 -0
  674. package/lib/ui/services.js +121 -0
  675. package/lib/ui/slots-ink.d.ts +26 -0
  676. package/lib/ui/slots-ink.js +78 -0
  677. package/lib/ui/slots-react.d.ts +28 -0
  678. package/lib/ui/slots-react.js +88 -0
  679. package/lib/ui/slots.d.ts +481 -0
  680. package/lib/ui/slots.js +550 -0
  681. package/lib/ui/store.d.ts +89 -0
  682. package/lib/ui/store.js +62 -0
  683. package/lib/ui/tui-plugin-api.d.ts +23 -0
  684. package/lib/ui/tui-plugin-api.js +23 -0
  685. package/lib/ui/tui-plugins.d.ts +47 -0
  686. package/lib/ui/tui-plugins.js +220 -0
  687. package/package.json +58 -6
@@ -0,0 +1,306 @@
1
+ /**
2
+ * 用量日志:每次模型请求追加一条到 `~/.dsc/usage/usage.jsonl`,设置「用量统计」
3
+ * 分区从这里聚合(热力图 / 趋势 / 模型占比 / 连续活跃天数)。
4
+ *
5
+ * 为什么单独一个文件而不是塞进会话 jsonl:会话日志是重放格式,行里没有 usage
6
+ * 也没有模型名,改它就要动重放语义;而用量是跨会话的全局量,天然是自己的流。
7
+ * agent 插件在拿到模型响应的 usage 事件时落一条(见 plugins/agent.ts),
8
+ * 这份日志从功能上线那刻开始积累,更早的会话没有记录。
9
+ *
10
+ * 2026-10-03 起记录前缀缓存明细(`ch`/`cm`):命中率是「请求前缀有没有被改写」
11
+ * 的直接读数——系统提示词改一个字节、注入并进头部,整段历史都会按全价重读。
12
+ *
13
+ * 0.6.67 起这个文件是两种行的**计量流**:请求行(带 token 与这一步的墙钟读数
14
+ * `lm`/`ft`/`d`,界面据此算输出速度与首字延迟)与工具行(`k: 'tool'`,带调用耗时)。
15
+ * 只认请求行的聚合(本文件的 readUsageRecords、设置页、会话用量卡)不受工具行影响。
16
+ *
17
+ * @module dsc/core/usage-log
18
+ */
19
+ import { appendFileSync, existsSync, mkdirSync, readFileSync } from 'node:fs';
20
+ import { dscPath } from './path-policy.js';
21
+ /** 用量日志的绝对路径(宿主写、壳进程读,两边都从这里取,绝不各拼一份)。 */
22
+ export function usageLogFile() {
23
+ return dscPath('usage', 'usage.jsonl');
24
+ }
25
+ /** 追加一条用量记录。写失败绝不打断对话轮次——统计少一条无所谓。 */
26
+ export function appendUsageRecord(record) {
27
+ append(record);
28
+ }
29
+ /** 追加一条工具调用记录(同上:写失败不影响轮次)。 */
30
+ export function appendToolRecord(record) {
31
+ append({ k: 'tool', ...record });
32
+ }
33
+ function append(row) {
34
+ try {
35
+ mkdirSync(dscPath('usage'), { recursive: true });
36
+ appendFileSync(usageLogFile(), `${JSON.stringify({ t: Date.now(), ...row })}\n`, 'utf8');
37
+ }
38
+ catch {
39
+ // 磁盘满 / 目录被占用:放弃这一条,下轮再试
40
+ }
41
+ }
42
+ /**
43
+ * 读全部**请求**行。坏行跳过;文件不存在 = 还没有任何记录。
44
+ * 工具行(`k: 'tool'`)没有 i/o,下面的校验自然把它们挡在外面——只认 token 记账的
45
+ * 聚合(设置页用量统计、连续活跃天)因此不会被工具行改变口径。
46
+ */
47
+ export function readUsageRecords() {
48
+ const file = usageLogFile();
49
+ if (!existsSync(file))
50
+ return [];
51
+ const out = [];
52
+ for (const line of readFileSync(file, 'utf8').split(/\r?\n/)) {
53
+ if (line === '')
54
+ continue;
55
+ try {
56
+ const row = JSON.parse(line);
57
+ if (typeof row.t !== 'number' || typeof row.i !== 'number' || typeof row.o !== 'number')
58
+ continue;
59
+ out.push({
60
+ t: row.t,
61
+ provider: typeof row.provider === 'string' ? row.provider : '',
62
+ model: typeof row.model === 'string' ? row.model : '',
63
+ i: row.i,
64
+ o: row.o,
65
+ ...(typeof row.ch === 'number' ? { ch: row.ch } : {}),
66
+ ...(typeof row.cm === 'number' ? { cm: row.cm } : {}),
67
+ sid: typeof row.sid === 'string' ? row.sid : '',
68
+ ...(typeof row.lm === 'number' && row.lm >= 0 ? { lm: row.lm } : {}),
69
+ ...(typeof row.ft === 'number' && row.ft >= 0 ? { ft: row.ft } : {}),
70
+ ...(typeof row.d === 'number' && row.d >= 0 ? { d: row.d } : {}),
71
+ });
72
+ }
73
+ catch {
74
+ // 半截行(写盘中断)跳过
75
+ }
76
+ }
77
+ return out;
78
+ }
79
+ /** 本地时区的 YYYY-MM-DD(getMonth/getDate 都按本地算,热力图的「天」跟着系统走)。 */
80
+ export function localDateKey(ts) {
81
+ const date = new Date(ts);
82
+ const month = String(date.getMonth() + 1).padStart(2, '0');
83
+ const day = String(date.getDate()).padStart(2, '0');
84
+ return `${String(date.getFullYear())}-${month}-${day}`;
85
+ }
86
+ /** 热力图最多回看多久:一年多一点,跨年也能和 GitHub 一样看到整条带子。 */
87
+ const MAX_SPAN_DAYS = 370;
88
+ /**
89
+ * 把原始记录聚合成设置页要用的统计视图。
90
+ * days 从首条记录所在日(不足一年时)或 370 天前(更早时)到今天,逐日给全
91
+ * (没记录的日子是零日)——热力图要连续的格子,空档由这里补齐。
92
+ * 前缀缓存明细只统计上报了它的记录(`ch`/`cm` 缺失按 0 计),命中率因此不会被老记录拉偏。
93
+ */
94
+ export function aggregateUsage(records) {
95
+ const days = new Map();
96
+ const models = new Map();
97
+ let totalInput = 0;
98
+ let totalOutput = 0;
99
+ let totalTurns = 0;
100
+ let totalCacheHit = 0;
101
+ let totalCacheMiss = 0;
102
+ let sinceTs = null;
103
+ for (const record of records) {
104
+ const key = localDateKey(record.t);
105
+ let day = days.get(key);
106
+ if (day === undefined) {
107
+ day = { date: key, inputTokens: 0, outputTokens: 0, turns: 0, byModel: {} };
108
+ days.set(key, day);
109
+ }
110
+ const tokens = record.i + record.o;
111
+ day.inputTokens += record.i;
112
+ day.outputTokens += record.o;
113
+ day.turns += 1;
114
+ const modelKey = `${record.provider}/${record.model}`;
115
+ day.byModel[modelKey] = (day.byModel[modelKey] ?? 0) + tokens;
116
+ totalInput += record.i;
117
+ totalOutput += record.o;
118
+ totalTurns += 1;
119
+ totalCacheHit += record.ch ?? 0;
120
+ totalCacheMiss += record.cm ?? 0;
121
+ if (sinceTs === null || record.t < sinceTs)
122
+ sinceTs = record.t;
123
+ let model = models.get(modelKey);
124
+ if (model === undefined) {
125
+ model = { key: modelKey, inputTokens: 0, outputTokens: 0, turns: 0, cacheHitTokens: 0, cacheMissTokens: 0 };
126
+ models.set(modelKey, model);
127
+ }
128
+ model.inputTokens += record.i;
129
+ model.outputTokens += record.o;
130
+ model.turns += 1;
131
+ model.cacheHitTokens += record.ch ?? 0;
132
+ model.cacheMissTokens += record.cm ?? 0;
133
+ }
134
+ // 逐日补零:起点 = max(首条记录所在日, 今天-369天),终点 = 今天
135
+ const today = new Date();
136
+ today.setHours(12, 0, 0, 0);
137
+ const filled = [];
138
+ if (sinceTs !== null) {
139
+ const first = new Date(sinceTs);
140
+ first.setHours(12, 0, 0, 0);
141
+ const earliest = new Date(today);
142
+ earliest.setDate(earliest.getDate() - (MAX_SPAN_DAYS - 1));
143
+ const cursor = new Date(first < earliest ? earliest : first);
144
+ while (cursor <= today) {
145
+ const key = localDateKey(cursor.getTime());
146
+ filled.push(days.get(key) ?? { date: key, inputTokens: 0, outputTokens: 0, turns: 0, byModel: {} });
147
+ cursor.setDate(cursor.getDate() + 1);
148
+ }
149
+ }
150
+ // 连续活跃天数:从今天(今天没有就从昨天)往前数连续有记录的日子
151
+ const active = new Set(days.keys());
152
+ let currentStreak = 0;
153
+ const probe = new Date(today);
154
+ if (!active.has(localDateKey(probe.getTime())))
155
+ probe.setDate(probe.getDate() - 1);
156
+ while (active.has(localDateKey(probe.getTime()))) {
157
+ currentStreak += 1;
158
+ probe.setDate(probe.getDate() - 1);
159
+ }
160
+ let longestStreak = 0;
161
+ let run = 0;
162
+ for (const day of filled) {
163
+ if (day.turns > 0) {
164
+ run += 1;
165
+ if (run > longestStreak)
166
+ longestStreak = run;
167
+ }
168
+ else {
169
+ run = 0;
170
+ }
171
+ }
172
+ let peakDay = null;
173
+ for (const day of days.values()) {
174
+ const tokens = day.inputTokens + day.outputTokens;
175
+ const peakTokens = peakDay === null ? -1 : peakDay.inputTokens + peakDay.outputTokens;
176
+ if (tokens > peakTokens)
177
+ peakDay = day;
178
+ }
179
+ return {
180
+ sinceTs,
181
+ totalInputTokens: totalInput,
182
+ totalOutputTokens: totalOutput,
183
+ totalCacheHitTokens: totalCacheHit,
184
+ totalCacheMissTokens: totalCacheMiss,
185
+ totalTurns,
186
+ activeDays: active.size,
187
+ currentStreakDays: currentStreak,
188
+ longestStreakDays: longestStreak,
189
+ peakDay,
190
+ days: filled,
191
+ models: [...models.values()].sort((a, b) => b.inputTokens + b.outputTokens - (a.inputTokens + a.outputTokens)),
192
+ };
193
+ }
194
+ /** 读 + 聚合一步到位(runtime 的 usageStats() 用)。 */
195
+ export function buildUsageStats() {
196
+ return aggregateUsage(readUsageRecords());
197
+ }
198
+ /** 非负有限数才算真值(缺项、负数、NaN 一律当没上报)。 */
199
+ function readNumber(value) {
200
+ return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : null;
201
+ }
202
+ /**
203
+ * 按会话 id 折叠这份日志(纯函数:喂文本,出视图)——壳进程与测试都用它,口径只此一份。
204
+ *
205
+ * 两种行各算各的:
206
+ * - 请求行(有 i/o)进请求数、输入/输出 token 与缓存明细;墙钟三项按「首字延迟与解码期
207
+ * 成对」的规则累计——只有拿到第一口(`ft`)的请求才能再进 `d` 与那批 token,
208
+ * 0.6.67 之前的老行两个都没有,于是既不进分子也不进分母(宁缺不假);
209
+ * - 工具行(`k: 'tool'`)只进工具耗时与次数,不碰任何 token 口径。
210
+ *
211
+ * 这个会话一行都没有时返回 null(界面据此整段省略,不显示一个全 0 的账)。
212
+ */
213
+ export function foldSessionUsage(text, sessionId) {
214
+ if (sessionId === '')
215
+ return null;
216
+ const view = {
217
+ requests: 0,
218
+ inputTokens: 0,
219
+ outputTokens: 0,
220
+ cacheHitTokens: 0,
221
+ cacheMissTokens: 0,
222
+ lastInputTokens: 0,
223
+ lastAt: 0,
224
+ llmMs: 0,
225
+ ttftMs: 0,
226
+ ttftSteps: 0,
227
+ decodeMs: 0,
228
+ decodeTokens: 0,
229
+ toolMs: 0,
230
+ toolCalls: 0,
231
+ };
232
+ for (const line of text.split(/\r?\n/)) {
233
+ if (line === '')
234
+ continue;
235
+ let row;
236
+ try {
237
+ row = JSON.parse(line);
238
+ }
239
+ catch {
240
+ continue; // 半截行(写盘中断)跳过
241
+ }
242
+ if (row.sid !== sessionId)
243
+ continue;
244
+ // 工具行:只有耗时,没有 token
245
+ if (row.k === 'tool') {
246
+ const ms = readNumber(row.ms);
247
+ if (ms === null)
248
+ continue;
249
+ view.toolMs += ms;
250
+ view.toolCalls += 1;
251
+ continue;
252
+ }
253
+ const input = readNumber(row.i);
254
+ const output = readNumber(row.o);
255
+ if (input === null || output === null)
256
+ continue;
257
+ view.requests += 1;
258
+ view.inputTokens += input;
259
+ view.outputTokens += output;
260
+ // 缓存两栏成对上报才算数(写入端一次写两个键):缺一个的行不进分母,不然命中率会被算低
261
+ const cacheHit = readNumber(row.ch);
262
+ const cacheMiss = readNumber(row.cm);
263
+ if (cacheHit !== null && cacheMiss !== null) {
264
+ view.cacheHitTokens += cacheHit;
265
+ view.cacheMissTokens += cacheMiss;
266
+ }
267
+ const llm = readNumber(row.lm);
268
+ if (llm !== null)
269
+ view.llmMs += llm;
270
+ const ttft = readNumber(row.ft);
271
+ if (ttft !== null) {
272
+ view.ttftMs += ttft;
273
+ view.ttftSteps += 1;
274
+ // 解码期是「第一口输出 → 定稿」,没有第一口就没有解码期;有解码期才带上输出 token
275
+ const decode = readNumber(row.d);
276
+ if (decode !== null) {
277
+ view.decodeMs += decode;
278
+ view.decodeTokens += output;
279
+ }
280
+ }
281
+ // 「最后一次」按时间戳取,不靠文件顺序:追加写不保证同一毫秒里的先后
282
+ const at = readNumber(row.t) ?? 0;
283
+ if (at >= view.lastAt) {
284
+ view.lastAt = at;
285
+ view.lastInputTokens = input;
286
+ }
287
+ }
288
+ return view.requests === 0 && view.toolCalls === 0 ? null : view;
289
+ }
290
+ /**
291
+ * 读 + 折叠当前会话的用量(壳进程的 `dsc:session-usage` IPC 与测试用)。
292
+ * 文件不存在、读不了、这个会话一行都没有:一律 null——界面据此整段省略。
293
+ */
294
+ export function readSessionUsage(sessionId) {
295
+ if (sessionId === '')
296
+ return null;
297
+ let text;
298
+ try {
299
+ text = readFileSync(usageLogFile(), 'utf8');
300
+ }
301
+ catch {
302
+ // 还没生成 / 正被别的进程占着:这一拍给不出统计
303
+ return null;
304
+ }
305
+ return foldSessionUsage(text, sessionId);
306
+ }
@@ -0,0 +1 @@
1
+ export declare const DSC_VERSION: string;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * dsc 版本号:从包根 package.json 读一次(设置「关于」分区展示)。
3
+ * 宿主可能跑在 `lib/` 或打包后的任意深度,因此逐级上溯找本包的清单
4
+ * (包名 `pneuma`,兼容旧名 `muse-code`、`dsc-tui`;都不认就会一直上溯到盘根)。
5
+ *
6
+ * @module dsc/core/version
7
+ */
8
+ import { readFileSync } from 'node:fs';
9
+ import { dirname, join } from 'node:path';
10
+ import { fileURLToPath } from 'node:url';
11
+ /**
12
+ * 本包的包名:改名时要同步这里,否则「关于」分区会退回 `0.0.0`。
13
+ * 旧名留着是给已发布过的包名与旧构建物兜底的,别删。
14
+ */
15
+ const PACKAGE_NAMES = ['pneuma', 'muse-code', 'dsc-tui'];
16
+ export const DSC_VERSION = (() => {
17
+ let dir = dirname(fileURLToPath(import.meta.url));
18
+ for (;;) {
19
+ try {
20
+ const doc = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
21
+ // scoped 名(@waq666/pneuma)按最后一段比,发布改名不再踩同一个坑
22
+ const shortName = typeof doc.name === 'string' ? doc.name.split('/').pop() : undefined;
23
+ if (shortName !== undefined &&
24
+ PACKAGE_NAMES.includes(shortName) &&
25
+ typeof doc.version === 'string') {
26
+ return doc.version;
27
+ }
28
+ }
29
+ catch {
30
+ // 这一层没有 package.json,继续往上
31
+ }
32
+ const parent = dirname(dir);
33
+ if (parent === dir)
34
+ return '0.0.0';
35
+ dir = parent;
36
+ }
37
+ })();
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Web 层的小工具:请求体读取、JSON 应答、Bearer 解析、Host 校验。
3
+ *
4
+ * 为什么落在 core:原来这些函数长在 `plugins/remote/http.ts` 里,只服务远程控制那一个
5
+ * 服务。本机界面服务(`plugins/webserver.ts`)要的是同一套(Host 头防护、JSON 应答、
6
+ * 一次性令牌),所以上移到 core 共用一份;`plugins/remote/http.ts` 变成一层转发,
7
+ * 远程那边的引用一行都不用改。
8
+ *
9
+ * 这里只有函数,没有状态。
10
+ *
11
+ * @module dsc/core/web-http
12
+ */
13
+ import type { IncomingMessage, ServerResponse } from 'node:http';
14
+ /** 常规请求体上限:配对、取票据、推送订阅都只有几十个字节,64KB 已经宽得离谱了。 */
15
+ export declare const MAX_BODY_BYTES: number;
16
+ /** 解析 lastSeq 查询参数:缺省、非数字、负数、0 一律 0(= 要全量)。 */
17
+ export declare function readLastSeq(raw: string | null): number;
18
+ export declare function isRecord(value: unknown): value is Record<string, unknown>;
19
+ /**
20
+ * 读请求体的原始字节。
21
+ *
22
+ * 超过上限时不再累积字节、只 reject 一次,让调用方回一个 413:
23
+ * 这里**不能** destroy 请求——那会把连接直接掐掉,413 还没写出去客户端就看到 socket hang up。
24
+ *
25
+ * @param limit - 这一次读最多收多少字节(JSON 路由 64KB,上传路由 20MB)
26
+ */
27
+ export declare function readBodyBytes(req: IncomingMessage, limit: number): Promise<Buffer>;
28
+ /** 读一个 JSON 请求体(按 UTF-8 解码)。 */
29
+ export declare function readBody(req: IncomingMessage): Promise<string>;
30
+ export declare function sendJson(res: ServerResponse, status: number, payload: unknown): void;
31
+ /** 发一段纯文本(诊断路由用)。 */
32
+ export declare function sendText(res: ServerResponse, status: number, text: string): void;
33
+ /**
34
+ * 取 `Authorization: Bearer <token>` 里的 token;没有就给 null。
35
+ *
36
+ * 按字符串解析而不是正则捕获:头部整段必须是「Bearer 前缀 + 一段没有空白的 token」,
37
+ * 带 scheme 以外的内容(多个词)一律不认。
38
+ */
39
+ export declare function bearerToken(header: string | undefined): string | null;
40
+ /**
41
+ * Host 头校验(防 DNS rebinding)。
42
+ *
43
+ * 攻击手法:恶意网页把自己的域名解析到 127.0.0.1,浏览器就会照那个域名向本机服务发请求。
44
+ * 只看「来源是回环地址」拦不住(请求确实从本机发出),能看的是 Host 头——那里带着攻击者的
45
+ * 域名。所以规矩两条:hostname 必须是 IP 字面量或 localhost,且端口必须等于我们在听的端口。
46
+ *
47
+ * @param host - 请求里的 Host 头(`127.0.0.1:17321`、`[::1]:17321`、`192.168.1.5:17321`)
48
+ * @param port - 本服务真正在听的端口
49
+ */
50
+ export declare function hostHeaderAllowed(host: string | undefined | null, port: number): boolean;
51
+ /**
52
+ * 从 Cookie 头里取一个 cookie 的值。
53
+ *
54
+ * 只按 `;` 切、只认第一个 `=`:cookie 值本身不允许含这些字符(我们自己下发的令牌是 base64url),
55
+ * 所以不需要解码与引号处理。
56
+ */
57
+ export declare function readCookie(header: string | undefined, name: string): string | null;
@@ -0,0 +1,137 @@
1
+ import { isIP } from 'node:net';
2
+ /** 常规请求体上限:配对、取票据、推送订阅都只有几十个字节,64KB 已经宽得离谱了。 */
3
+ export const MAX_BODY_BYTES = 64 * 1024;
4
+ /** 解析 lastSeq 查询参数:缺省、非数字、负数、0 一律 0(= 要全量)。 */
5
+ export function readLastSeq(raw) {
6
+ if (raw === null)
7
+ return 0;
8
+ const value = Number(raw);
9
+ if (!Number.isFinite(value) || value <= 0)
10
+ return 0;
11
+ return Math.trunc(value);
12
+ }
13
+ export function isRecord(value) {
14
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
15
+ }
16
+ /**
17
+ * 读请求体的原始字节。
18
+ *
19
+ * 超过上限时不再累积字节、只 reject 一次,让调用方回一个 413:
20
+ * 这里**不能** destroy 请求——那会把连接直接掐掉,413 还没写出去客户端就看到 socket hang up。
21
+ *
22
+ * @param limit - 这一次读最多收多少字节(JSON 路由 64KB,上传路由 20MB)
23
+ */
24
+ export function readBodyBytes(req, limit) {
25
+ return new Promise((resolveBody, rejectBody) => {
26
+ const chunks = [];
27
+ let size = 0;
28
+ let overLimit = false;
29
+ req.on('data', (chunk) => {
30
+ if (overLimit)
31
+ return; // 已经超了:后面的字节丢掉,等 handler 回 413
32
+ size += chunk.length;
33
+ if (size > limit) {
34
+ overLimit = true;
35
+ const shown = limit >= 1024 * 1024 ? `${String(Math.round(limit / 1024 / 1024))}MB` : `${String(Math.round(limit / 1024))}KB`;
36
+ rejectBody(new Error(`请求体超过 ${shown}`));
37
+ return;
38
+ }
39
+ chunks.push(chunk);
40
+ });
41
+ req.on('end', () => {
42
+ if (!overLimit)
43
+ resolveBody(Buffer.concat(chunks));
44
+ });
45
+ req.on('error', (error) => {
46
+ if (!overLimit)
47
+ rejectBody(error);
48
+ });
49
+ });
50
+ }
51
+ /** 读一个 JSON 请求体(按 UTF-8 解码)。 */
52
+ export async function readBody(req) {
53
+ return (await readBodyBytes(req, MAX_BODY_BYTES)).toString('utf8');
54
+ }
55
+ export function sendJson(res, status, payload) {
56
+ const body = JSON.stringify(payload);
57
+ res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'content-length': Buffer.byteLength(body) });
58
+ res.end(body);
59
+ }
60
+ /** 发一段纯文本(诊断路由用)。 */
61
+ export function sendText(res, status, text) {
62
+ res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8', 'content-length': Buffer.byteLength(text) });
63
+ res.end(text);
64
+ }
65
+ /**
66
+ * 取 `Authorization: Bearer <token>` 里的 token;没有就给 null。
67
+ *
68
+ * 按字符串解析而不是正则捕获:头部整段必须是「Bearer 前缀 + 一段没有空白的 token」,
69
+ * 带 scheme 以外的内容(多个词)一律不认。
70
+ */
71
+ export function bearerToken(header) {
72
+ if (header === undefined)
73
+ return null;
74
+ const prefix = 'bearer ';
75
+ const trimmed = header.trim();
76
+ if (!trimmed.toLowerCase().startsWith(prefix))
77
+ return null;
78
+ const token = trimmed.slice(prefix.length).trim();
79
+ if (token === '' || /\s/.test(token))
80
+ return null;
81
+ return token;
82
+ }
83
+ /**
84
+ * Host 头校验(防 DNS rebinding)。
85
+ *
86
+ * 攻击手法:恶意网页把自己的域名解析到 127.0.0.1,浏览器就会照那个域名向本机服务发请求。
87
+ * 只看「来源是回环地址」拦不住(请求确实从本机发出),能看的是 Host 头——那里带着攻击者的
88
+ * 域名。所以规矩两条:hostname 必须是 IP 字面量或 localhost,且端口必须等于我们在听的端口。
89
+ *
90
+ * @param host - 请求里的 Host 头(`127.0.0.1:17321`、`[::1]:17321`、`192.168.1.5:17321`)
91
+ * @param port - 本服务真正在听的端口
92
+ */
93
+ export function hostHeaderAllowed(host, port) {
94
+ if (host === undefined || host === null)
95
+ return false;
96
+ let rest = host.trim().toLowerCase();
97
+ if (rest === '')
98
+ return false;
99
+ let hostname;
100
+ let portText;
101
+ if (rest.startsWith('[')) {
102
+ const end = rest.indexOf(']');
103
+ if (end < 0)
104
+ return false;
105
+ hostname = rest.slice(1, end);
106
+ portText = rest.slice(end + 1).replace(/^:/, '');
107
+ }
108
+ else {
109
+ const index = rest.lastIndexOf(':');
110
+ hostname = index < 0 ? rest : rest.slice(0, index);
111
+ portText = index < 0 ? '' : rest.slice(index + 1);
112
+ }
113
+ if (portText !== String(port))
114
+ return false;
115
+ if (hostname === 'localhost')
116
+ return true;
117
+ return isIP(hostname) !== 0;
118
+ }
119
+ /**
120
+ * 从 Cookie 头里取一个 cookie 的值。
121
+ *
122
+ * 只按 `;` 切、只认第一个 `=`:cookie 值本身不允许含这些字符(我们自己下发的令牌是 base64url),
123
+ * 所以不需要解码与引号处理。
124
+ */
125
+ export function readCookie(header, name) {
126
+ if (header === undefined)
127
+ return null;
128
+ for (const part of header.split(';')) {
129
+ const index = part.indexOf('=');
130
+ if (index < 0)
131
+ continue;
132
+ if (part.slice(0, index).trim() !== name)
133
+ continue;
134
+ return part.slice(index + 1).trim();
135
+ }
136
+ return null;
137
+ }
@@ -0,0 +1,31 @@
1
+ import type { ServerResponse } from 'node:http';
2
+ /** 认得的扩展名 → content-type;认不得的按二进制发。 */
3
+ export declare const CONTENT_TYPES: Readonly<Record<string, string>>;
4
+ /** 目标路径必须在给定目录里面(`..` 与绝对路径都挡掉)。 */
5
+ export declare function inside(dir: string, target: string): boolean;
6
+ /** 伺服一个静态目录时要调的那几个旋钮。 */
7
+ export interface StaticOptions {
8
+ /** 目录不存在(界面还没构建)时 `/` 返回的占位页;不传就回 404。 */
9
+ placeholderHtml?: string;
10
+ /** index.html 送出前的改写钩子(注入启动清单)。 */
11
+ transformHtml?(html: string, pathname: string): string;
12
+ /**
13
+ * 缓存策略。界面产物带内容哈希、index.html 不带,所以默认 no-cache(刷新就能拿到新版);
14
+ * 插件 bundle 走内容寻址,调用方会显式传一年不可变。
15
+ */
16
+ cacheControl?: string;
17
+ /**
18
+ * 内容安全策略。给了就随每个命中响应下发(对 HTML 文档生效,其余资源无害)。
19
+ * 只有宿主界面服务传它——文档里跑着第三方插件 bundle,是唯一值得收口的面。
20
+ */
21
+ contentSecurityPolicy?: string;
22
+ }
23
+ /**
24
+ * 伺服静态文件;无论命中与否**都会应答**(命中 200、`/` 回占位页、其余 404)。
25
+ * @param res - HTTP 应答
26
+ * @param pathname - 请求路径(`/`、`/app-abc.js`)
27
+ * @param dir - 产物目录
28
+ * @param options - 改写与缓存策略
29
+ * @returns 命中了真实文件就是 true(调用方拿它决定要不要记日志)
30
+ */
31
+ export declare function serveStatic(res: ServerResponse, pathname: string, dir: string, options?: StaticOptions): boolean;
@@ -0,0 +1,75 @@
1
+ /**
2
+ * 静态资源伺服:把界面产物目录挂在 HTTP 上。
3
+ *
4
+ * 原来这段长在 `plugins/remote/routes.ts` 里(服务手机端界面)。本机界面服务要的是
5
+ * 同一件事(服务 `lib/ui/assets`),只多一个「送 index.html 之前改写一下」的钩子——
6
+ * 宿主就是靠它把启动清单注进页面的。所以上移到 core,两处共用。
7
+ *
8
+ * @module dsc/core/web-static
9
+ */
10
+ import { existsSync, readFileSync, statSync } from 'node:fs';
11
+ import { extname, resolve, sep } from 'node:path';
12
+ /** 认得的扩展名 → content-type;认不得的按二进制发。 */
13
+ export const CONTENT_TYPES = {
14
+ '.html': 'text/html; charset=utf-8',
15
+ '.js': 'text/javascript; charset=utf-8',
16
+ '.mjs': 'text/javascript; charset=utf-8',
17
+ '.css': 'text/css; charset=utf-8',
18
+ '.json': 'application/json; charset=utf-8',
19
+ '.svg': 'image/svg+xml',
20
+ '.png': 'image/png',
21
+ '.jpg': 'image/jpeg',
22
+ '.jpeg': 'image/jpeg',
23
+ '.webp': 'image/webp',
24
+ '.ico': 'image/x-icon',
25
+ '.woff': 'font/woff',
26
+ '.woff2': 'font/woff2',
27
+ '.map': 'application/json; charset=utf-8',
28
+ };
29
+ /** 目标路径必须在给定目录里面(`..` 与绝对路径都挡掉)。 */
30
+ export function inside(dir, target) {
31
+ const root = resolve(dir);
32
+ return target === root || target.startsWith(root + sep);
33
+ }
34
+ /**
35
+ * 伺服静态文件;无论命中与否**都会应答**(命中 200、`/` 回占位页、其余 404)。
36
+ * @param res - HTTP 应答
37
+ * @param pathname - 请求路径(`/`、`/app-abc.js`)
38
+ * @param dir - 产物目录
39
+ * @param options - 改写与缓存策略
40
+ * @returns 命中了真实文件就是 true(调用方拿它决定要不要记日志)
41
+ */
42
+ export function serveStatic(res, pathname, dir, options = {}) {
43
+ const cacheControl = options.cacheControl ?? 'no-cache';
44
+ const rel = pathname === '/' ? 'index.html' : decodeURIComponent(pathname).replace(/^\/+/, '');
45
+ const target = resolve(dir, rel);
46
+ if (inside(dir, target) && existsSync(target) && statSync(target).isFile()) {
47
+ const type = CONTENT_TYPES[extname(target).toLowerCase()] ?? 'application/octet-stream';
48
+ let body = readFileSync(target);
49
+ if (body.length > 0 && (target.endsWith('.html') || target.endsWith('.htm')) && options.transformHtml !== undefined) {
50
+ body = options.transformHtml(body.toString('utf8'), pathname);
51
+ }
52
+ const buffer = typeof body === 'string' ? Buffer.from(body, 'utf8') : body;
53
+ res.writeHead(200, {
54
+ 'content-type': type,
55
+ 'content-length': buffer.length,
56
+ 'cache-control': cacheControl,
57
+ ...(options.contentSecurityPolicy === undefined ? {} : { 'content-security-policy': options.contentSecurityPolicy }),
58
+ });
59
+ res.end(buffer);
60
+ return true;
61
+ }
62
+ if (pathname === '/' && options.placeholderHtml !== undefined) {
63
+ const body = options.placeholderHtml;
64
+ res.writeHead(200, {
65
+ 'content-type': 'text/html; charset=utf-8',
66
+ 'content-length': Buffer.byteLength(body),
67
+ 'cache-control': 'no-cache',
68
+ ...(options.contentSecurityPolicy === undefined ? {} : { 'content-security-policy': options.contentSecurityPolicy }),
69
+ });
70
+ res.end(body);
71
+ return false;
72
+ }
73
+ res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' }).end('没有这个文件');
74
+ return false;
75
+ }
@@ -0,0 +1 @@
1
+ export {};