@binblink/wasmnest-plugin-sdk-desktop 0.0.0-stage → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (271) hide show
  1. package/README.md +307 -2
  2. package/README_en.md +284 -0
  3. package/bin/cli.js +602 -0
  4. package/bin/manifest-gen.js +458 -0
  5. package/bin/manifest-validate.js +494 -0
  6. package/bin/permission-vocabulary.json +5 -0
  7. package/bin/plugin-watch.js +331 -0
  8. package/bin/wasm-hash.js +124 -0
  9. package/dev-shell/README.md +122 -0
  10. package/dev-shell/index.html +12 -0
  11. package/dev-shell/package.json +28 -0
  12. package/dev-shell/pnpm-workspace.yaml +9 -0
  13. package/dev-shell/postcss.config.js +6 -0
  14. package/dev-shell/src/App.vue +90 -0
  15. package/dev-shell/src/components/LogPanel.vue +90 -0
  16. package/dev-shell/src/loader.ts +104 -0
  17. package/dev-shell/src/locale.ts +29 -0
  18. package/dev-shell/src/locales.ts +50 -0
  19. package/dev-shell/src/main.ts +46 -0
  20. package/dev-shell/src/mock/app-env.ts +102 -0
  21. package/dev-shell/src/mock/commands.ts +54 -0
  22. package/dev-shell/src/mock/domain-context.ts +46 -0
  23. package/dev-shell/src/mock/events.ts +16 -0
  24. package/dev-shell/src/mock/file-transfer.ts +512 -0
  25. package/dev-shell/src/mock/tauri-ipc.ts +109 -0
  26. package/dev-shell/src/registry.ts +90 -0
  27. package/dev-shell/src/shell/components/AppMountSlot.vue +241 -0
  28. package/dev-shell/src/shell/components/ShellIcon.vue +141 -0
  29. package/dev-shell/src/shell/components/ShellStatusBar.vue +87 -0
  30. package/dev-shell/src/shell/components/ShellToolbar.vue +287 -0
  31. package/dev-shell/src/shell/mount/appSession.ts +45 -0
  32. package/dev-shell/src/shell/mount/devAppMount.ts +80 -0
  33. package/dev-shell/src/shell/types.ts +121 -0
  34. package/dev-shell/src/styles/dev.css +27 -0
  35. package/dev-shell/src/styles/shell.css +752 -0
  36. package/dev-shell/src/styles/style.css +1178 -0
  37. package/dev-shell/src/theme.ts +140 -0
  38. package/dev-shell/src/vite-env.d.ts +24 -0
  39. package/dev-shell/tailwind.config.js +98 -0
  40. package/dev-shell/vite.config.ts +174 -0
  41. package/dist/index.d.ts +150 -0
  42. package/dist/index.js +238 -0
  43. package/dist/types.d.ts +632 -0
  44. package/dist/types.js +0 -0
  45. package/dist/vite-plugin.d.ts +25 -0
  46. package/dist/vite-plugin.js +80 -0
  47. package/go/.gitignore +5 -0
  48. package/go/README.md +80 -0
  49. package/go/gen/wasmnest/api-call/host-api-call/empty.s +3 -0
  50. package/go/gen/wasmnest/api-call/host-api-call/host-api-call.wasm.go +13 -0
  51. package/go/gen/wasmnest/api-call/host-api-call/host-api-call.wit.go +34 -0
  52. package/go/gen/wasmnest/app/host-app/empty.s +3 -0
  53. package/go/gen/wasmnest/app/host-app/host-app.wasm.go +29 -0
  54. package/go/gen/wasmnest/app/host-app/host-app.wit.go +108 -0
  55. package/go/gen/wasmnest/auth-center/host-auth-center/abi.go +14 -0
  56. package/go/gen/wasmnest/auth-center/host-auth-center/empty.s +3 -0
  57. package/go/gen/wasmnest/auth-center/host-auth-center/host-auth-center.wasm.go +49 -0
  58. package/go/gen/wasmnest/auth-center/host-auth-center/host-auth-center.wit.go +225 -0
  59. package/go/gen/wasmnest/clipboard/host-clipboard/abi.go +14 -0
  60. package/go/gen/wasmnest/clipboard/host-clipboard/empty.s +3 -0
  61. package/go/gen/wasmnest/clipboard/host-clipboard/host-clipboard.wasm.go +17 -0
  62. package/go/gen/wasmnest/clipboard/host-clipboard/host-clipboard.wit.go +68 -0
  63. package/go/gen/wasmnest/connection-registry/host-connection-registry/empty.s +3 -0
  64. package/go/gen/wasmnest/connection-registry/host-connection-registry/host-connection-registry.wasm.go +13 -0
  65. package/go/gen/wasmnest/connection-registry/host-connection-registry/host-connection-registry.wit.go +50 -0
  66. package/go/gen/wasmnest/crypto/host-crypto/abi.go +18 -0
  67. package/go/gen/wasmnest/crypto/host-crypto/empty.s +3 -0
  68. package/go/gen/wasmnest/crypto/host-crypto/host-crypto.wasm.go +37 -0
  69. package/go/gen/wasmnest/crypto/host-crypto/host-crypto.wit.go +139 -0
  70. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/abi.go +14 -0
  71. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/empty.s +3 -0
  72. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/host-fs-desktop.wasm.go +21 -0
  73. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/host-fs-desktop.wit.go +62 -0
  74. package/go/gen/wasmnest/http/host-http/abi.go +14 -0
  75. package/go/gen/wasmnest/http/host-http/empty.s +3 -0
  76. package/go/gen/wasmnest/http/host-http/host-http.wasm.go +13 -0
  77. package/go/gen/wasmnest/http/host-http/host-http.wit.go +40 -0
  78. package/go/gen/wasmnest/http-endpoint/host-http-endpoint/empty.s +3 -0
  79. package/go/gen/wasmnest/http-endpoint/host-http-endpoint/host-http-endpoint.wasm.go +17 -0
  80. package/go/gen/wasmnest/http-endpoint/host-http-endpoint/host-http-endpoint.wit.go +69 -0
  81. package/go/gen/wasmnest/mdns/host-mdns/empty.s +3 -0
  82. package/go/gen/wasmnest/mdns/host-mdns/host-mdns.wasm.go +29 -0
  83. package/go/gen/wasmnest/mdns/host-mdns/host-mdns.wit.go +101 -0
  84. package/go/gen/wasmnest/notify/host-notify/empty.s +3 -0
  85. package/go/gen/wasmnest/notify/host-notify/host-notify.wasm.go +13 -0
  86. package/go/gen/wasmnest/notify/host-notify/host-notify.wit.go +64 -0
  87. package/go/gen/wasmnest/peer/host-peer/abi.go +14 -0
  88. package/go/gen/wasmnest/peer/host-peer/empty.s +3 -0
  89. package/go/gen/wasmnest/peer/host-peer/host-peer.wasm.go +85 -0
  90. package/go/gen/wasmnest/peer/host-peer/host-peer.wit.go +357 -0
  91. package/go/gen/wasmnest/platform-desktop/host-platform-desktop/empty.s +3 -0
  92. package/go/gen/wasmnest/platform-desktop/host-platform-desktop/host-platform-desktop.wasm.go +21 -0
  93. package/go/gen/wasmnest/platform-desktop/host-platform-desktop/host-platform-desktop.wit.go +70 -0
  94. package/go/gen/wasmnest/plugin/abi-form/abi-form.exports.go +11 -0
  95. package/go/gen/wasmnest/plugin/abi-form/abi-form.wasm.go +13 -0
  96. package/go/gen/wasmnest/plugin/abi-form/abi-form.wit.go +7 -0
  97. package/go/gen/wasmnest/plugin/abi-form/empty.s +3 -0
  98. package/go/gen/wasmnest/plugin/command/command.exports.go +11 -0
  99. package/go/gen/wasmnest/plugin/command/command.wasm.go +19 -0
  100. package/go/gen/wasmnest/plugin/command/command.wit.go +6 -0
  101. package/go/gen/wasmnest/plugin/command/empty.s +3 -0
  102. package/go/gen/wasmnest/plugin/events/empty.s +3 -0
  103. package/go/gen/wasmnest/plugin/events/events.exports.go +52 -0
  104. package/go/gen/wasmnest/plugin/events/events.wasm.go +74 -0
  105. package/go/gen/wasmnest/plugin/events/events.wit.go +195 -0
  106. package/go/gen/wasmnest/plugin/host-bus/empty.s +3 -0
  107. package/go/gen/wasmnest/plugin/host-bus/host-bus.wasm.go +29 -0
  108. package/go/gen/wasmnest/plugin/host-bus/host-bus.wit.go +94 -0
  109. package/go/gen/wasmnest/plugin/host-config/abi.go +14 -0
  110. package/go/gen/wasmnest/plugin/host-config/empty.s +3 -0
  111. package/go/gen/wasmnest/plugin/host-config/host-config.wasm.go +13 -0
  112. package/go/gen/wasmnest/plugin/host-config/host-config.wit.go +21 -0
  113. package/go/gen/wasmnest/plugin/host-events/empty.s +3 -0
  114. package/go/gen/wasmnest/plugin/host-events/host-events.wasm.go +9 -0
  115. package/go/gen/wasmnest/plugin/host-events/host-events.wit.go +27 -0
  116. package/go/gen/wasmnest/plugin/host-fs/abi.go +14 -0
  117. package/go/gen/wasmnest/plugin/host-fs/empty.s +3 -0
  118. package/go/gen/wasmnest/plugin/host-fs/host-fs.wasm.go +33 -0
  119. package/go/gen/wasmnest/plugin/host-fs/host-fs.wit.go +86 -0
  120. package/go/gen/wasmnest/plugin/host-log/empty.s +3 -0
  121. package/go/gen/wasmnest/plugin/host-log/host-log.wasm.go +25 -0
  122. package/go/gen/wasmnest/plugin/host-log/host-log.wit.go +65 -0
  123. package/go/gen/wasmnest/plugin/host-metrics/empty.s +3 -0
  124. package/go/gen/wasmnest/plugin/host-metrics/host-metrics.wasm.go +13 -0
  125. package/go/gen/wasmnest/plugin/host-metrics/host-metrics.wit.go +34 -0
  126. package/go/gen/wasmnest/plugin/host-platform/empty.s +3 -0
  127. package/go/gen/wasmnest/plugin/host-platform/host-platform.wasm.go +21 -0
  128. package/go/gen/wasmnest/plugin/host-platform/host-platform.wit.go +85 -0
  129. package/go/gen/wasmnest/plugin/host-plugin-database/abi.go +14 -0
  130. package/go/gen/wasmnest/plugin/host-plugin-database/empty.s +3 -0
  131. package/go/gen/wasmnest/plugin/host-plugin-database/host-plugin-database.wasm.go +29 -0
  132. package/go/gen/wasmnest/plugin/host-plugin-database/host-plugin-database.wit.go +70 -0
  133. package/go/gen/wasmnest/plugin/host-storage/abi.go +14 -0
  134. package/go/gen/wasmnest/plugin/host-storage/empty.s +3 -0
  135. package/go/gen/wasmnest/plugin/host-storage/host-storage.wasm.go +21 -0
  136. package/go/gen/wasmnest/plugin/host-storage/host-storage.wit.go +44 -0
  137. package/go/gen/wasmnest/plugin/host-worker/empty.s +3 -0
  138. package/go/gen/wasmnest/plugin/host-worker/host-worker.wasm.go +17 -0
  139. package/go/gen/wasmnest/plugin/host-worker/host-worker.wit.go +50 -0
  140. package/go/gen/wasmnest/plugin/lifecycle/empty.s +3 -0
  141. package/go/gen/wasmnest/plugin/lifecycle/lifecycle.exports.go +32 -0
  142. package/go/gen/wasmnest/plugin/lifecycle/lifecycle.wasm.go +41 -0
  143. package/go/gen/wasmnest/plugin/lifecycle/lifecycle.wit.go +9 -0
  144. package/go/gen/wasmnest/plugin/manifest/empty.s +3 -0
  145. package/go/gen/wasmnest/plugin/manifest/manifest.exports.go +11 -0
  146. package/go/gen/wasmnest/plugin/manifest/manifest.wasm.go +13 -0
  147. package/go/gen/wasmnest/plugin/manifest/manifest.wit.go +6 -0
  148. package/go/gen/wasmnest/plugin/plugin/plugin.wit.go +4 -0
  149. package/go/gen/wasmnest/process/host-process/empty.s +3 -0
  150. package/go/gen/wasmnest/process/host-process/host-process.wasm.go +21 -0
  151. package/go/gen/wasmnest/process/host-process/host-process.wit.go +56 -0
  152. package/go/gen/wasmnest/pty/host-pty/abi.go +14 -0
  153. package/go/gen/wasmnest/pty/host-pty/empty.s +3 -0
  154. package/go/gen/wasmnest/pty/host-pty/host-pty.wasm.go +33 -0
  155. package/go/gen/wasmnest/pty/host-pty/host-pty.wit.go +164 -0
  156. package/go/gen/wasmnest/task/host-task/abi.go +14 -0
  157. package/go/gen/wasmnest/task/host-task/empty.s +3 -0
  158. package/go/gen/wasmnest/task/host-task/host-task.wasm.go +29 -0
  159. package/go/gen/wasmnest/task/host-task/host-task.wit.go +110 -0
  160. package/go/gen/wasmnest/timer/host-timer/empty.s +3 -0
  161. package/go/gen/wasmnest/timer/host-timer/host-timer.wasm.go +13 -0
  162. package/go/gen/wasmnest/timer/host-timer/host-timer.wit.go +25 -0
  163. package/go/gen/wasmnest/ws/host-websocket/empty.s +3 -0
  164. package/go/gen/wasmnest/ws/host-websocket/host-websocket.wasm.go +29 -0
  165. package/go/gen/wasmnest/ws/host-websocket/host-websocket.wit.go +109 -0
  166. package/go/gen/wasmnest/ws-server/host-websocket-server/empty.s +3 -0
  167. package/go/gen/wasmnest/ws-server/host-websocket-server/host-websocket-server.wasm.go +49 -0
  168. package/go/gen/wasmnest/ws-server/host-websocket-server/host-websocket-server.wit.go +220 -0
  169. package/go/go.mod +14 -0
  170. package/go/go.sum +2 -0
  171. package/go/sdk/dispatch.go +49 -0
  172. package/go/sdk/dispatch_test.go +81 -0
  173. package/go/sdk/entry_native.go +13 -0
  174. package/go/sdk/entry_wasip2.go +188 -0
  175. package/go/sdk/host_http_wasip2.go +42 -0
  176. package/go/sdk/host_wasip2.go +251 -0
  177. package/go/sdk/plugin.go +103 -0
  178. package/go/testplugin/index.js +1 -0
  179. package/go/testplugin/main.go +67 -0
  180. package/go/testplugin/plugin.json +24 -0
  181. package/go/tools/assemble-wit.mjs +152 -0
  182. package/go/tools/gen-bindings.mjs +51 -0
  183. package/go/wit/core.wit +273 -0
  184. package/go/wit/deps/api-call/cap-api-call.wit +20 -0
  185. package/go/wit/deps/app/cap-app.wit +54 -0
  186. package/go/wit/deps/auth-center/cap-auth-center.wit +124 -0
  187. package/go/wit/deps/clipboard/clipboard.wit +48 -0
  188. package/go/wit/deps/clocks/monotonic-clock.wit +45 -0
  189. package/go/wit/deps/clocks/wall-clock.wit +42 -0
  190. package/go/wit/deps/clocks/world.wit +6 -0
  191. package/go/wit/deps/connection-registry/cap-connection-registry.wit +40 -0
  192. package/go/wit/deps/crypto/cap-crypto.wit +68 -0
  193. package/go/wit/deps/filesystem/preopens.wit +8 -0
  194. package/go/wit/deps/filesystem/types.wit +634 -0
  195. package/go/wit/deps/filesystem/world.wit +6 -0
  196. package/go/wit/deps/fs-desktop/cap-fs-desktop.wit +29 -0
  197. package/go/wit/deps/http/http.wit +28 -0
  198. package/go/wit/deps/http-endpoint/cap-http.wit +44 -0
  199. package/go/wit/deps/io/error.wit +34 -0
  200. package/go/wit/deps/io/poll.wit +41 -0
  201. package/go/wit/deps/io/streams.wit +262 -0
  202. package/go/wit/deps/io/world.wit +6 -0
  203. package/go/wit/deps/mdns/mdns.wit +45 -0
  204. package/go/wit/deps/notify/notify.wit +51 -0
  205. package/go/wit/deps/peer/peer.wit +126 -0
  206. package/go/wit/deps/platform-desktop/cap-platform-desktop.wit +38 -0
  207. package/go/wit/deps/process/cap-process.wit +25 -0
  208. package/go/wit/deps/pty/pty.wit +80 -0
  209. package/go/wit/deps/random/insecure-seed.wit +25 -0
  210. package/go/wit/deps/random/insecure.wit +22 -0
  211. package/go/wit/deps/random/random.wit +26 -0
  212. package/go/wit/deps/random/world.wit +7 -0
  213. package/go/wit/deps/sockets/instance-network.wit +9 -0
  214. package/go/wit/deps/sockets/ip-name-lookup.wit +51 -0
  215. package/go/wit/deps/sockets/network.wit +145 -0
  216. package/go/wit/deps/sockets/tcp-create-socket.wit +27 -0
  217. package/go/wit/deps/sockets/tcp.wit +353 -0
  218. package/go/wit/deps/sockets/udp-create-socket.wit +27 -0
  219. package/go/wit/deps/sockets/udp.wit +266 -0
  220. package/go/wit/deps/sockets/world.wit +11 -0
  221. package/go/wit/deps/task/cap-task.wit +56 -0
  222. package/go/wit/deps/timer/cap-timer.wit +14 -0
  223. package/go/wit/deps/wasi-cli/command.wit +7 -0
  224. package/go/wit/deps/wasi-cli/environment.wit +18 -0
  225. package/go/wit/deps/wasi-cli/exit.wit +4 -0
  226. package/go/wit/deps/wasi-cli/imports.wit +20 -0
  227. package/go/wit/deps/wasi-cli/run.wit +4 -0
  228. package/go/wit/deps/wasi-cli/stdio.wit +17 -0
  229. package/go/wit/deps/wasi-cli/terminal.wit +49 -0
  230. package/go/wit/deps/ws/ws.wit +50 -0
  231. package/go/wit/deps/ws-server/cap-ws.wit +99 -0
  232. package/go/wit/go-plugin.wit +28 -0
  233. package/go/wit/plugin-face.wit +355 -0
  234. package/go/wit/wasmnest.wit +25 -0
  235. package/package.json +100 -3
  236. package/src/config.ts +49 -0
  237. package/src/global-dialog.ts +169 -0
  238. package/src/index.ts +88 -0
  239. package/src/runtime.ts +54 -0
  240. package/src/types.ts +681 -0
  241. package/src/ui/AppUpdateSection.d.ts +44 -0
  242. package/src/ui/AppUpdateSection.vue +253 -0
  243. package/src/ui/MarkdownEditor.vue +520 -0
  244. package/src/ui/NumberField.d.ts +49 -0
  245. package/src/ui/NumberField.vue +390 -0
  246. package/src/ui/PluginGlobalDialog.vue +401 -0
  247. package/src/ui/Select.d.ts +36 -0
  248. package/src/ui/Select.vue +220 -0
  249. package/src/ui/TerminalInputRail.vue +184 -0
  250. package/src/ui/apply-select-position.ts +65 -0
  251. package/src/ui/markdown-editor-syntax.ts +234 -0
  252. package/src/ui/markdown-editor.d.ts +36 -0
  253. package/src/ui/plugin-global-dialog.d.ts +12 -0
  254. package/src/ui/select-position.ts +79 -0
  255. package/src/ui/terminal-input-rail.d.ts +30 -0
  256. package/src/ui/zoom-compensation.d.ts +9 -0
  257. package/src/ui/zoom-compensation.ts +52 -0
  258. package/src/update.ts +123 -0
  259. package/src/vite-plugin.ts +124 -0
  260. package/template/README.md +39 -0
  261. package/template/package.json +23 -0
  262. package/template/plugin.json +14 -0
  263. package/template/rust/Cargo.toml +21 -0
  264. package/template/rust/src/lib.rs +39 -0
  265. package/template/src/env.d.ts +24 -0
  266. package/template/src/index.ts +37 -0
  267. package/template/tsconfig.json +17 -0
  268. package/template/vite.config.ts +21 -0
  269. package/tools/componentize/Cargo.lock +299 -0
  270. package/tools/componentize/Cargo.toml +18 -0
  271. package/tools/componentize/src/main.rs +197 -0
@@ -0,0 +1,458 @@
1
+ /**
2
+ * manifest-gen — 桌面端 plugin.json contributes/permissions 自动填充
3
+ *
4
+ * 单一事实来源是插件源码:前端 context.ui.register* 调用推导 views 扩展点,
5
+ * 前端 context API 使用 + Rust host 调用推导权限,Rust invoke_command 匹配臂推导 commands,
6
+ * on_terminal_input/output 实现推导 terminal handlers。
7
+ *
8
+ * 与移动端 manifest-gen 的区别(桌面端扩展点/权限体系不同):
9
+ * - 扩展点:sidebar/toolbox/statusbar(views)+ fileHandlers(无 navTab/settings/toolbarItems)
10
+ * - 权限:ui:sidebar / ui:toolbox / ui:statusbar / ui:pageToolbar / ui:settings / ui:input
11
+ * / ui:fileHandler / terminal:input / session:read / broadcast 等桌面端权限
12
+ * (v27 起 `terminal:observe` / `session:write` 已退役,不在其列)
13
+ *
14
+ * 合并策略(保守,避免误删导致运行时拒绝):
15
+ * - permissions:派生结果与手工声明取并集
16
+ * - contributes.views/fileHandlers:扫描到注册调用时以扫描结果为准
17
+ * (按 id 从旧条目继承无法静态求值的字段,如 i18n.t() 动态 title);未扫描到时保留原值
18
+ * - contributes.commands:以 Rust invoke_command 匹配臂为准,title 从旧条目继承
19
+ * - terminal.inputHandlers/outputParsers:从 Rust 实现检测填充
20
+ * - configuration/lifecycle/icon 等手工字段永不覆盖
21
+ */
22
+
23
+ import { existsSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'
24
+ import { join } from 'node:path'
25
+ import { permissionVocabulary } from './manifest-validate.js'
26
+
27
+ // ==================== 权限映射表 ====================
28
+
29
+ /**
30
+ * 前端 UI 注册调用 → 权限(从 SDK 权限词汇生成物的 apiMap 派生)
31
+ *
32
+ * 生成物里 `ui.registerXxx` 方法名去掉 `ui.` 前缀就是这里的扫描键(源码里以
33
+ * `.registerXxx(` 出现)。派生而非手抄:手抄表已经落后过一次真源(新增注册 API
34
+ * 忘记登记 → manifest 缺权限位 → 运行时被门),现由
35
+ * `bin/permission-vocabulary.json` 单点决定。
36
+ */
37
+ const REGISTER_PERMISSIONS = Object.fromEntries(
38
+ Object.entries(permissionVocabulary().apiMap).flatMap(([perm, methods]) =>
39
+ (methods || [])
40
+ .filter((m) => m.startsWith('ui.register'))
41
+ .map((m) => [m.slice('ui.'.length), perm]),
42
+ ),
43
+ )
44
+
45
+ /** 前端 context API 使用 → 权限(正则按合并后的前端源码匹配) */
46
+ const FRONTEND_PERMISSION_RULES = [
47
+ { re: /\.storage\s*\.\s*(get|set|delete|flush)\b/, perm: 'storage' },
48
+ // v27(票 10):`terminal.sendInput`(宿主导流输入)与
49
+ // `terminal.onInputSubmitted`(提交行观察)随宿主会话面退役——对应规则删除;
50
+ // `terminal:input` 仍由 `.terminal.onInput` 推断,`terminal:observe` 位已不存在
51
+ { re: /\.terminal\s*\.\s*onInput\b/, perm: 'terminal:input' },
52
+ { re: /\.terminal\s*\.\s*onOutput\b/, perm: 'terminal:output' },
53
+ // v27(票 10):`session:read` 现在只剩宿主终端窗口事实(数据面 `list` / `get`
54
+ // 与事件面 `onStatusChange` 已退役);`session:write` 位已不存在
55
+ { re: /\.session\s*\.\s*(predictTerminalSize|openTerminal|closeTerminal|isTerminalOpen)\b/, perm: 'session:read' },
56
+ { re: /\.http\s*\.\s*registerEndpoint\b/, perm: 'network:http' },
57
+ { re: /\.events\s*\.\s*(on|emit)\b/, perm: 'broadcast' },
58
+ ]
59
+
60
+ /** Rust host API 调用 → 权限 */
61
+ const RUST_PERMISSION_RULES = [
62
+ // 插件私有 KV(host-storage)
63
+ { re: /\b(storage_get|storage_set|storage_delete)\b/, perm: 'storage' },
64
+ // 主库 SQL 面已随 host-database 退役(票 18 主库收归:内核不再有插件主库
65
+ // 原语,`database:main` 权限位从 permission.rs 移除)——不再映射 `db_*`。
66
+ { re: /\bterminal_send\b/, perm: 'terminal:input' },
67
+ // host-pty 两域(与权限词汇表 apiMap 逐字对应):spawn/kill 属高风险面,
68
+ // 数据面(write/resize/ring_fetch/is_running)归 pty:io
69
+ { re: /\b(pty_spawn|pty_kill)\b/, perm: 'pty:spawn' },
70
+ { re: /\b(pty_write|pty_resize|pty_ring_fetch|pty_is_running)\b/, perm: 'pty:io' },
71
+ // 会话读取面:`session_list` / `session_get` + v23 起留作**一次性 legacy 迁移通道**的
72
+ // `session_config_list` / `session_config_get`(`config-get` 改挂 `session:read`)。
73
+ // **`session_config_upsert` / `session_config_delete` 已随 v23 从 ABI 删除**,`session:config`
74
+ // 权限位同步退役——映射表若留着旧项,就会给插件注入词汇表外的权限(见下方加载期自检)。
75
+ { re: /\b(session_list|session_get|session_config_list|session_config_get)\b/, perm: 'session:read' },
76
+ // 在册连接清单(票 04):SDK 方法名 `connections_list`(WIT `host-connection`),
77
+ // 权限位与域名同源 `connection:read`——不再由 `session:read` 代答「谁能读连接清单」。
78
+ { re: /\bconnections_list\b/, perm: 'connection:read' },
79
+ { re: /\bhttp_fetch\b/, perm: 'network:http' },
80
+ // ABI v29 服务端域(动态路由注册):register/unregister 同挂 network:http
81
+ // (与前端面 `http.registerEndpoint` 同权限位)
82
+ { re: /\bhttp_register_endpoint\b/, perm: 'network:http' },
83
+ { re: /\bhttp_unregister_endpoint\b/, perm: 'network:http' },
84
+ { re: /\b(fs_read|fs_copy)\b/, perm: 'fs:read' },
85
+ { re: /\bfs_write\b/, perm: 'fs:write' },
86
+ // 系统文件选择器(WIT `host-platform`,SDK 方法名 `platform_pick_files` /
87
+ // `platform_pick_folder` / `platform_pick_folders`):宿主要求 manifest **单独**
88
+ // 声明 `fs:pick` 才能调起系统原生对话框,缺声明即调用显性失败——因此扫描到调用
89
+ // 就必须把该位补进 manifest(与 fs:read / fs:write 分域,不合并推导)
90
+ { re: /\bplatform_pick_(files|folder|folders)\b/, perm: 'fs:pick' },
91
+ // 插件私有库(host-plugin-database)与私有 KV 同挂 storage
92
+ { re: /\bplugin_db_\w+\b/, perm: 'storage' },
93
+ // 运行监控(WIT `host-metrics.snapshot`):只读、全局可见(一次授权看全部插件)
94
+ { re: /\bmetrics_snapshot\b/, perm: 'metrics:read' },
95
+ // 内网穿透(T06):服务端 / 客户端分域;`configure` / `status` 是共用面
96
+ // (任一权限即可),不单独推导,避免给应用注入它没声明的位。
97
+ { re: /\btunnel_(expose|unexpose|issue_pairing_ticket)\b/, perm: 'tunnel:serve' },
98
+ { re: /\btunnel_(connect|close)\b/, perm: 'tunnel:connect' },
99
+ ]
100
+
101
+ /**
102
+ * 映射表加载期自检:任何指向「词汇表外权限」的规则立即抛错,绝不静默往下走。
103
+ *
104
+ * 真源是 `rust/src/permission.rs`(生成 `bin/permission-vocabulary.json`),而本表是手写的
105
+ * 宿主方法名映射——两者漏跟演一次,后果是 manifest-gen 给插件注入非法权限位,随后
106
+ * `plugin-build.js` 的 manifest 校验必挂(整条 `plugins:build` 链不可用,2026-09-22 实测:
107
+ * v23 退役 `session:config` 后本表未跟演)。
108
+ */
109
+ {
110
+ const vocab = new Set(permissionVocabulary().permissions)
111
+ for (const rule of [...FRONTEND_PERMISSION_RULES, ...RUST_PERMISSION_RULES]) {
112
+ if (!vocab.has(rule.perm)) {
113
+ throw new Error(
114
+ `manifest-gen: 权限映射表含词汇表外权限 '${rule.perm}'(规则 ${rule.re})——` +
115
+ `真源 packages/plugin-sdk-desktop/rust/src/permission.rs 变更(退役/改名)后必须跟演本表,` +
116
+ `并重跑 SDK 的 pnpm run gen:permissions`,
117
+ )
118
+ }
119
+ }
120
+ }
121
+
122
+ // ==================== 文件收集 ====================
123
+
124
+ const FRONTEND_EXTS = new Set(['.ts', '.tsx', '.vue', '.js'])
125
+
126
+ /** 递归收集指定扩展名的文件 */
127
+ function collectFiles(dir, exts, out = []) {
128
+ if (!existsSync(dir)) return out
129
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
130
+ if (entry.name === 'node_modules' || entry.name === 'dist' || entry.name.startsWith('.'))
131
+ continue
132
+ const full = join(dir, entry.name)
133
+ if (entry.isDirectory()) {
134
+ collectFiles(full, exts, out)
135
+ } else if (entry.isFile() && exts.some((e) => entry.name.endsWith(e))) {
136
+ out.push(full)
137
+ }
138
+ }
139
+ return out
140
+ }
141
+
142
+ // ==================== 对象字面量解析 ====================
143
+
144
+ /** 从源码指定下标起提取配对的 {...} 对象字面量文本 */
145
+ function extractObjectLiteral(source, fromIndex) {
146
+ const start = source.indexOf('{', fromIndex)
147
+ if (start === -1) return null
148
+ let depth = 0
149
+ let inStr = null
150
+ for (let i = start; i < source.length; i++) {
151
+ const ch = source[i]
152
+ if (inStr) {
153
+ if (ch === '\\') i++
154
+ else if (ch === inStr) inStr = null
155
+ continue
156
+ }
157
+ if (ch === "'" || ch === '"' || ch === '`') inStr = ch
158
+ else if (ch === '{') depth++
159
+ else if (ch === '}') {
160
+ depth--
161
+ if (depth === 0) return source.slice(start, i + 1)
162
+ }
163
+ }
164
+ return null
165
+ }
166
+
167
+ /** 按顶层逗号切分对象字面量内部(忽略嵌套与字符串内的逗号) */
168
+ function splitTopLevel(body) {
169
+ const parts = []
170
+ let depth = 0
171
+ let inStr = null
172
+ let cur = ''
173
+ for (let i = 0; i < body.length; i++) {
174
+ const ch = body[i]
175
+ if (inStr) {
176
+ cur += ch
177
+ if (ch === '\\') cur += body[++i] ?? ''
178
+ else if (ch === inStr) inStr = null
179
+ continue
180
+ }
181
+ if (ch === "'" || ch === '"' || ch === '`') {
182
+ inStr = ch
183
+ cur += ch
184
+ continue
185
+ }
186
+ if (ch === '{' || ch === '[' || ch === '(') depth++
187
+ else if (ch === '}' || ch === ']' || ch === ')') depth--
188
+ if (ch === ',' && depth === 0) {
189
+ parts.push(cur)
190
+ cur = ''
191
+ } else {
192
+ cur += ch
193
+ }
194
+ }
195
+ if (cur.trim()) parts.push(cur)
196
+ return parts
197
+ }
198
+
199
+ /** 分类字面量值:字符串/数字/布尔/标识符,动态表达式返回 null */
200
+ function classifyValue(raw) {
201
+ const text = raw.trim().replace(/,$/, '').trim()
202
+ const strMatch = text.match(/^(['"])((?:\\.|(?!\1).)*)\1$/s)
203
+ if (strMatch) return strMatch[2]
204
+ if (/^-?\d+(\.\d+)?$/.test(text)) return Number(text)
205
+ if (text === 'true') return true
206
+ if (text === 'false') return false
207
+ if (/^[A-Za-z_$][\w$]*$/.test(text)) return { __ident: text }
208
+ return null
209
+ }
210
+
211
+ /** 解析简单对象字面量为键值映射(嵌套对象/动态值 → null) */
212
+ function parseObjectLiteral(literal) {
213
+ const body = literal.slice(literal.indexOf('{') + 1, literal.lastIndexOf('}'))
214
+ const result = {}
215
+ for (const part of splitTopLevel(body)) {
216
+ const m = part.match(/^\s*(?:['"](\w+)['"]|(\w+))\s*:\s*([\s\S]+)$/)
217
+ if (!m) continue
218
+ const key = m[1] || m[2]
219
+ result[key] = classifyValue(m[3])
220
+ }
221
+ return result
222
+ }
223
+
224
+ /** 提取源码中所有 `context.ui.callName({...})` 注册调用的对象参数 */
225
+ function findRegisterCalls(source, callName) {
226
+ const results = []
227
+ const re = new RegExp(`\\.${callName}\\s*\\(`, 'g')
228
+ let m
229
+ while ((m = re.exec(source)) !== null) {
230
+ const literal = extractObjectLiteral(source, m.index + m[0].length - 1)
231
+ if (literal) results.push(parseObjectLiteral(literal))
232
+ }
233
+ return results
234
+ }
235
+
236
+ // ==================== 字段合并 ====================
237
+
238
+ /** 旧条目按 id 建索引 */
239
+ function indexById(entries) {
240
+ const map = new Map()
241
+ for (const e of entries || []) {
242
+ if (e && e.id) map.set(e.id, e)
243
+ }
244
+ return map
245
+ }
246
+
247
+ /**
248
+ * 允许「源码标识符直接当清单值」的字段:**只有 component**。
249
+ *
250
+ * 扫描器把裸标识符(`component: ChatView`)求值成 `{__ident}`——这类字段里
251
+ * 「符号名本身就是清单要的值」,写回字符串正确。但其余字段出现裸标识符
252
+ * (`order: SOME_CONST`)意味着源码引用了一个常量,**静态求不出数值**:曾把它
253
+ * 当字符串写回(`"order": "AGENT_HUB_SIDEBAR_ORDER"`),宿主读成非数字 → 侧栏
254
+ * 排序静默失效,且清单校验无类型检查不报错,于是每次构建静默改写受版本跟踪的
255
+ * plugin.json。这类字段与 null 同口径:跳过(保留清单既有值)+ 日志点名。
256
+ */
257
+ const IDENT_AS_STRING_KEYS = new Set(['component'])
258
+
259
+ /** 合并扫描条目与旧条目:扫描值优先,null(动态表达式)回退旧值,再回退默认值 */
260
+ function mergeEntry(scanned, old, defaults = {}) {
261
+ const merged = { ...defaults }
262
+ if (old) Object.assign(merged, old)
263
+ for (const [key, value] of Object.entries(scanned)) {
264
+ if (value === null || value === undefined) continue
265
+ if (value && typeof value === 'object' && value.__ident) {
266
+ if (!IDENT_AS_STRING_KEYS.has(key)) {
267
+ console.warn(
268
+ `[manifest-gen] ${key} 引用标识符 ${value.__ident}(常量值静态不可求):保留 plugin.json 既有值`,
269
+ )
270
+ continue
271
+ }
272
+ merged[key] = value.__ident
273
+ continue
274
+ }
275
+ merged[key] = value
276
+ }
277
+ return merged
278
+ }
279
+
280
+ // ==================== Rust 扫描 ====================
281
+
282
+ /** 提取 invoke_command 匹配臂中的 command id 列表(过滤 _ 前缀内置分支) */
283
+ function extractRustCommands(rustSource) {
284
+ const fnIndex = rustSource.search(/\bfn\s+invoke_command\b/)
285
+ if (fnIndex === -1) return []
286
+ const body = extractObjectLiteral(rustSource, fnIndex)
287
+ if (!body) return []
288
+ const ids = []
289
+ const re = /"([\w][\w.-]*)"\s*=>/g
290
+ let m
291
+ while ((m = re.exec(body)) !== null) {
292
+ // 下划线前缀为内置控制分支(如 _http_endpoint),不是插件命令
293
+ if (!m[1].startsWith('_')) ids.push(m[1])
294
+ }
295
+ return [...new Set(ids)]
296
+ }
297
+
298
+ /** 检测 rust 源码是否实现了终端处理导出 */
299
+ function extractTerminalHandlers(rustSource) {
300
+ const handlers = []
301
+ if (/\bfn\s+on_terminal_input\b/.test(rustSource)) handlers.push('on_terminal_input')
302
+ if (/\bfn\s+on_terminal_output\b/.test(rustSource)) handlers.push('on_terminal_output')
303
+ return handlers
304
+ }
305
+
306
+ // ==================== 主入口 ====================
307
+
308
+ /**
309
+ * 扫描插件源码并自动填充 plugin.json 的 contributes/permissions
310
+ * @param {string} cwd 插件工程根目录
311
+ * @param {{ check?: boolean }} options check 模式只报告不写入
312
+ * @returns {{ changed: boolean, report: string[] }}
313
+ */
314
+ export function generateManifest(cwd, { check = false } = {}) {
315
+ const manifestPath = join(cwd, 'plugin.json')
316
+ if (!existsSync(manifestPath)) {
317
+ throw new Error(`plugin.json 不存在: ${manifestPath}`)
318
+ }
319
+ let manifest
320
+ try {
321
+ manifest = JSON.parse(readFileSync(manifestPath, 'utf-8'))
322
+ } catch (e) {
323
+ // JSON.parse 对非法 JSON 抛 SyntaxError:包一层带路径的可读错误,
324
+ // 便于定位是哪个插件的 manifest 损坏(坏 JSON 是配置错误,应显性失败)
325
+ throw new Error(`plugin.json 非法 JSON,请检查文件: ${manifestPath} (${e.message})`)
326
+ }
327
+ const report = []
328
+ const permissions = new Set(manifest.permissions || [])
329
+ const contributes = { ...(manifest.contributes || {}) }
330
+
331
+ // ---------- 前端扫描 ----------
332
+ // 剔除注释后再扫:贡献面扫描只认可执行代码——注释里说明「旧机制已退役」是合法
333
+ // 文档(2026-10-17 桌面 UI 宿主修正后各 app 注释提及退役的 registerSidebarPanel,
334
+ // 不剥注释会把文档说明误判成真注册调用)
335
+ const frontendSource = collectFiles(join(cwd, 'src'), [...FRONTEND_EXTS])
336
+ .map((f) => readFileSync(f, 'utf-8'))
337
+ .join('\n')
338
+ .replace(/\/\*[\s\S]*?\*\//g, '')
339
+ .replace(/(^|[^:])\/\/.*$/gm, '$1')
340
+
341
+ // views:sidebar / toolbox / statusbar 三种类型
342
+ const sidebarPanels = findRegisterCalls(frontendSource, 'registerSidebarPanel')
343
+ const toolboxPages = findRegisterCalls(frontendSource, 'registerToolboxPage')
344
+ const statusBarItems = findRegisterCalls(frontendSource, 'registerStatusBarItem')
345
+
346
+ const scannedViews = [
347
+ ...sidebarPanels.map((s) => mergeEntry(s, undefined, { type: 'sidebar' })),
348
+ ...toolboxPages.map((s) => mergeEntry(s, undefined, { type: 'toolbox' })),
349
+ ...statusBarItems.map((s) => mergeEntry(s, undefined, { type: 'statusbar' })),
350
+ ].filter((v) => {
351
+ // id 解析不出来 = 注册描述符用了变量/表达式(如 `id: panel.id` 的循环注册),
352
+ // 扫描器无法静态求值。此时**保留 manifest 里的既有声明**:写回一条缺 id 的条目
353
+ // 会让宿主 `get_manifest()` 反序列化直接失败(插件加载不了),比漏一条声明严重得多
354
+ if (v && v.id) return true
355
+ console.warn(
356
+ `[manifest-gen] 跳过一个无法静态解析 id 的 ${v?.type ?? 'view'} 注册(保留 plugin.json 既有声明)`,
357
+ )
358
+ return false
359
+ })
360
+
361
+ if (scannedViews.length > 0) {
362
+ const old = indexById(contributes.views)
363
+ contributes.views = scannedViews.map((s) => mergeEntry(s, old.get(s.id)))
364
+ report.push(`contributes.views ← ${contributes.views.map((v) => v.id).join(', ')}`)
365
+ }
366
+
367
+ // fileHandlers
368
+ const fileHandlers = findRegisterCalls(frontendSource, 'registerFileHandler')
369
+ if (fileHandlers.length > 0) {
370
+ const old = indexById(contributes.fileHandlers)
371
+ contributes.fileHandlers = fileHandlers.map((s) => mergeEntry(s, old.get(s.id)))
372
+ report.push(
373
+ `contributes.fileHandlers ← ${contributes.fileHandlers.map((v) => v.id).join(', ')}`,
374
+ )
375
+ }
376
+
377
+ // 注册调用 → 权限
378
+ for (const [callName, perm] of Object.entries(REGISTER_PERMISSIONS)) {
379
+ if (new RegExp(`\\.${callName}\\s*\\(`).test(frontendSource)) {
380
+ if (!permissions.has(perm)) report.push(`permissions + ${perm}(${callName} 调用)`)
381
+ permissions.add(perm)
382
+ }
383
+ }
384
+
385
+ // 前端 context API 使用 → 权限
386
+ for (const rule of FRONTEND_PERMISSION_RULES) {
387
+ if (rule.re.test(frontendSource)) {
388
+ if (!permissions.has(rule.perm)) report.push(`permissions + ${rule.perm}(前端 API 使用)`)
389
+ permissions.add(rule.perm)
390
+ }
391
+ }
392
+
393
+ // ---------- Rust 扫描 ----------
394
+ const rustSource = collectFiles(join(cwd, 'rust/src'), ['.rs'])
395
+ .map((f) => readFileSync(f, 'utf-8'))
396
+ .join('\n')
397
+
398
+ if (rustSource) {
399
+ for (const rule of RUST_PERMISSION_RULES) {
400
+ if (rule.re.test(rustSource)) {
401
+ if (!permissions.has(rule.perm)) report.push(`permissions + ${rule.perm}(Rust host 调用)`)
402
+ permissions.add(rule.perm)
403
+ }
404
+ }
405
+
406
+ const commandIds = extractRustCommands(rustSource)
407
+ if (commandIds.length > 0) {
408
+ // 票 17 口径(用户裁决 ①):manifest 已声明 commands 即视为**人工裁剪过的
409
+ // 用户可见命令面**,生成器不再按匹配臂覆写——`invoke_command` 的臂含宿主
410
+ // 桥接与闭环调试入口(com.wasmnest.bedcode 实测 28 声明 / 50 臂),全量覆写
411
+ // 等于把内部接缝 advertise 成产品命令。改为只报告差集:未声明的臂(提示,
412
+ // 由人判断是否属于用户可见面)、声明了但源码没有的(真漂移,必须修)。
413
+ // 未声明 commands 的插件沿用自动填充(prefactor 前形态)。
414
+ if (contributes.commands && contributes.commands.length > 0) {
415
+ const declared = new Set(contributes.commands.map((c) => c.id))
416
+ const missing = commandIds.filter((id) => !declared.has(id))
417
+ const stale = [...declared].filter((id) => !commandIds.includes(id))
418
+ if (missing.length > 0) {
419
+ report.push(
420
+ `contributes.commands 未声明的 invoke_command 臂 ${missing.length} 个(人工裁剪面,不自动写入): ${missing.join(', ')}`,
421
+ )
422
+ }
423
+ if (stale.length > 0) {
424
+ report.push(`contributes.commands 声明但源码无对应臂 ${stale.join(', ')}`)
425
+ }
426
+ } else {
427
+ // 本分支下 manifest 未声明任何命令,无旧条目可继承
428
+ contributes.commands = commandIds.map((id) => ({ id, title: id }))
429
+ report.push(`contributes.commands ← ${commandIds.length} 个`)
430
+ }
431
+ }
432
+
433
+ const handlers = extractTerminalHandlers(rustSource)
434
+ if (handlers.length > 0) {
435
+ const terminal = { ...(contributes.terminal || {}) }
436
+ const inputHandlers = handlers.filter((h) => h === 'on_terminal_input')
437
+ const outputParsers = handlers.filter((h) => h === 'on_terminal_output')
438
+ if (inputHandlers.length > 0) terminal.inputHandlers = inputHandlers
439
+ if (outputParsers.length > 0) terminal.outputParsers = outputParsers
440
+ contributes.terminal = terminal
441
+ report.push(`contributes.terminal handlers ← ${handlers.join(', ')}`)
442
+ }
443
+ }
444
+
445
+ // ---------- 生成结果 ----------
446
+ const generated = {
447
+ ...manifest,
448
+ permissions: [...permissions].sort(),
449
+ contributes,
450
+ }
451
+ const changed = JSON.stringify(generated, null, 2) !== JSON.stringify(manifest, null, 2)
452
+
453
+ if (changed && !check) {
454
+ writeFileSync(manifestPath, JSON.stringify(generated, null, 2) + '\n', 'utf-8')
455
+ }
456
+
457
+ return { changed, report }
458
+ }