@capyclaw/browser-suite 0.1.2 → 0.2.37

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 (217) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +9 -5
  3. package/composition.lock.json +37 -25
  4. package/contract/browser-contract.json +484 -25
  5. package/cordis.patch.yml +3 -9
  6. package/dsh-suite.json +110 -82
  7. package/lib/index.mjs +1 -1
  8. package/node_modules/@capyclaw/asset-service/README.md +48 -0
  9. package/node_modules/@capyclaw/asset-service/cordis.patch.yml +4 -0
  10. package/node_modules/@capyclaw/asset-service/dsh-suite.json +29 -0
  11. package/node_modules/@capyclaw/asset-service/lib/host.mjs +14 -0
  12. package/node_modules/@capyclaw/asset-service/package.json +59 -0
  13. package/node_modules/@capyclaw/asset-service/src/index.mjs +304 -0
  14. package/node_modules/@capyclaw/browser-skill-api/README.md +3 -0
  15. package/node_modules/@capyclaw/browser-skill-api/contract/browser-contract.json +1349 -0
  16. package/node_modules/@capyclaw/browser-skill-api/cordis.patch.yml +4 -0
  17. package/node_modules/@capyclaw/browser-skill-api/package.json +49 -0
  18. package/node_modules/@capyclaw/browser-skill-api/plugin.mjs +15 -0
  19. package/node_modules/@capyclaw/browser-skill-api/skills/browser-api/SKILL.md +85 -0
  20. package/node_modules/@capyclaw/browser-skill-api/skills/browser-api/references/tool-contract.md +692 -0
  21. package/node_modules/@capyclaw/browser-skill-browser/README.md +3 -0
  22. package/node_modules/@capyclaw/browser-skill-browser/contract/browser-contract.json +1349 -0
  23. package/node_modules/@capyclaw/browser-skill-browser/cordis.patch.yml +4 -0
  24. package/node_modules/@capyclaw/browser-skill-browser/package.json +49 -0
  25. package/node_modules/@capyclaw/browser-skill-browser/plugin.mjs +15 -0
  26. package/node_modules/@capyclaw/browser-skill-browser/skills/browser/SKILL.md +27 -0
  27. package/node_modules/@capyclaw/browser-skill-ops/README.md +3 -0
  28. package/node_modules/@capyclaw/browser-skill-ops/contract/browser-contract.json +1349 -0
  29. package/node_modules/@capyclaw/browser-skill-ops/cordis.patch.yml +4 -0
  30. package/node_modules/@capyclaw/browser-skill-ops/package.json +49 -0
  31. package/node_modules/@capyclaw/browser-skill-ops/plugin.mjs +15 -0
  32. package/node_modules/@capyclaw/browser-skill-ops/skills/browser-ops/SKILL.md +63 -0
  33. package/node_modules/@capyclaw/browser-workbench/README.md +89 -0
  34. package/node_modules/@capyclaw/browser-workbench/contract/browser-contract.json +1349 -0
  35. package/node_modules/@capyclaw/browser-workbench/demo/form.html +112 -0
  36. package/node_modules/@capyclaw/browser-workbench/lib/host.mjs +1 -0
  37. package/node_modules/@capyclaw/browser-workbench/package.json +71 -0
  38. package/node_modules/@capyclaw/browser-workbench/plugin.mjs +1 -0
  39. package/node_modules/@capyclaw/browser-workbench/server/constants.mjs +16 -0
  40. package/node_modules/@capyclaw/browser-workbench/server/contract-snapshots.mjs +81 -0
  41. package/node_modules/@capyclaw/browser-workbench/server/contract.mjs +24 -0
  42. package/node_modules/@capyclaw/browser-workbench/server/embedded-host.mjs +247 -0
  43. package/node_modules/@capyclaw/browser-workbench/server/engine.mjs +2466 -0
  44. package/node_modules/@capyclaw/browser-workbench/server/governor.mjs +189 -0
  45. package/node_modules/@capyclaw/browser-workbench/server/http-utils.mjs +2 -0
  46. package/node_modules/@capyclaw/browser-workbench/server/human.mjs +628 -0
  47. package/node_modules/@capyclaw/browser-workbench/server/infra/http.mjs +2 -0
  48. package/node_modules/@capyclaw/browser-workbench/server/infra/tuning.mjs +2 -0
  49. package/node_modules/@capyclaw/browser-workbench/server/input.mjs +154 -0
  50. package/node_modules/@capyclaw/browser-workbench/server/login-vault.mjs +109 -0
  51. package/node_modules/@capyclaw/browser-workbench/server/mcp/handler.mjs +2 -0
  52. package/node_modules/@capyclaw/browser-workbench/server/mcp/instructions.mjs +31 -0
  53. package/node_modules/@capyclaw/browser-workbench/server/orphans.mjs +163 -0
  54. package/node_modules/@capyclaw/browser-workbench/server/relay.mjs +198 -0
  55. package/node_modules/@capyclaw/browser-workbench/server/routes/api.mjs +233 -0
  56. package/node_modules/@capyclaw/browser-workbench/server/routes/assets.mjs +2 -0
  57. package/node_modules/@capyclaw/browser-workbench/server/routes/mcp.mjs +17 -0
  58. package/node_modules/@capyclaw/browser-workbench/server/routes/workbench.mjs +33 -0
  59. package/node_modules/@capyclaw/browser-workbench/server/session-engines.mjs +223 -0
  60. package/node_modules/@capyclaw/browser-workbench/server/shared/SHARED_MANIFEST.json +70 -0
  61. package/node_modules/@capyclaw/browser-workbench/server/shared/asset-service.mjs +2 -0
  62. package/node_modules/@capyclaw/browser-workbench/server/shared/editor-host.mjs +2 -0
  63. package/node_modules/@capyclaw/browser-workbench/server/shared/kit/editor/lease.mjs +2 -0
  64. package/node_modules/@capyclaw/browser-workbench/server/shared/kit/service/api.mjs +2 -0
  65. package/node_modules/@capyclaw/browser-workbench/server/shared/kit/service/http.mjs +2 -0
  66. package/node_modules/@capyclaw/browser-workbench/server/shared/kit/service/mcp-http.mjs +2 -0
  67. package/node_modules/@capyclaw/browser-workbench/server/shared/kit/tools/validate.mjs +2 -0
  68. package/node_modules/@capyclaw/browser-workbench/server/shared/kit/tuning.mjs +2 -0
  69. package/node_modules/@capyclaw/browser-workbench/server/shared/kit/util/fs-lock.mjs +2 -0
  70. package/node_modules/@capyclaw/browser-workbench/server/shared/service-host.mjs +2 -0
  71. package/node_modules/@capyclaw/browser-workbench/server/shared/tool-host.mjs +2 -0
  72. package/node_modules/@capyclaw/browser-workbench/server/shared/tuning.mjs +2 -0
  73. package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/contract.mjs +55 -0
  74. package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/evaluate.mjs +36 -0
  75. package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/index.mjs +26 -0
  76. package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/interaction.mjs +215 -0
  77. package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/navigation.mjs +35 -0
  78. package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/screenshot.mjs +50 -0
  79. package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/session.mjs +81 -0
  80. package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/snapshot.mjs +30 -0
  81. package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/tabs.mjs +35 -0
  82. package/node_modules/@capyclaw/browser-workbench/server/tools/index.mjs +12 -0
  83. package/node_modules/@capyclaw/browser-workbench/server/tools/kernel/define.mjs +5 -0
  84. package/node_modules/@capyclaw/browser-workbench/server/tools/kernel/output.mjs +30 -0
  85. package/node_modules/@capyclaw/browser-workbench/server/tools/kernel/ports.mjs +59 -0
  86. package/node_modules/@capyclaw/browser-workbench/server/tools/kernel/registry.mjs +17 -0
  87. package/node_modules/@capyclaw/browser-workbench/server/tools.mjs +3 -0
  88. package/node_modules/@capyclaw/browser-workbench/server/workbench-framework.mjs +305 -0
  89. package/node_modules/@capyclaw/browser-workbench/ui/app.js +447 -0
  90. package/node_modules/@capyclaw/browser-workbench/ui/index.html +46 -0
  91. package/node_modules/@capyclaw/browser-workbench/ui/style.css +144 -0
  92. package/node_modules/@capyclaw/document-workbench/README.md +125 -0
  93. package/node_modules/@capyclaw/document-workbench/package.json +44 -0
  94. package/node_modules/@capyclaw/document-workbench/src/bridge/index.mjs +321 -0
  95. package/node_modules/@capyclaw/document-workbench/src/client/index.mjs +625 -0
  96. package/node_modules/@capyclaw/document-workbench/src/client/screenshot.mjs +98 -0
  97. package/node_modules/@capyclaw/document-workbench/src/index.mjs +9 -0
  98. package/node_modules/@capyclaw/document-workbench/src/model/audit.mjs +101 -0
  99. package/node_modules/@capyclaw/document-workbench/src/model/conformance.mjs +92 -0
  100. package/node_modules/@capyclaw/document-workbench/src/model/ids.mjs +56 -0
  101. package/node_modules/@capyclaw/document-workbench/src/model/index.mjs +18 -0
  102. package/node_modules/@capyclaw/document-workbench/src/model/locate.mjs +118 -0
  103. package/node_modules/@capyclaw/document-workbench/src/model/manifest.mjs +148 -0
  104. package/node_modules/@capyclaw/document-workbench/src/model/ops.mjs +160 -0
  105. package/node_modules/@capyclaw/document-workbench/src/model/validate.mjs +72 -0
  106. package/node_modules/@capyclaw/document-workbench/src/ops/index.mjs +93 -0
  107. package/node_modules/@capyclaw/document-workbench/src/session/context.mjs +103 -0
  108. package/node_modules/@capyclaw/document-workbench/src/session/index.mjs +394 -0
  109. package/node_modules/@capyclaw/document-workbench/src/session/lease.mjs +105 -0
  110. package/node_modules/@capyclaw/document-workbench/src/session/mode.mjs +80 -0
  111. package/node_modules/@capyclaw/document-workbench/src/session/sanitize.mjs +107 -0
  112. package/node_modules/@capyclaw/document-workbench/src/session/state.mjs +159 -0
  113. package/node_modules/@capyclaw/document-workbench/src/session/watch.mjs +63 -0
  114. package/node_modules/@capyclaw/document-workbench/src/store/file-backend.mjs +158 -0
  115. package/node_modules/@capyclaw/document-workbench/src/testing/index.mjs +75 -0
  116. package/node_modules/@capyclaw/foundation/README.md +51 -0
  117. package/node_modules/@capyclaw/foundation/abi/bridge.mjs +86 -0
  118. package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
  119. package/node_modules/@capyclaw/foundation/abi/index.mjs +35 -0
  120. package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
  121. package/node_modules/@capyclaw/foundation/abi/loader.mjs +345 -0
  122. package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
  123. package/node_modules/@capyclaw/foundation/abi/spec.mjs +255 -0
  124. package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
  125. package/node_modules/@capyclaw/foundation/abi/version.mjs +82 -0
  126. package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
  127. package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
  128. package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
  129. package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
  130. package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
  131. package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +55 -0
  132. package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
  133. package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
  134. package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
  135. package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
  136. package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
  137. package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
  138. package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
  139. package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
  140. package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +34 -0
  141. package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +85 -0
  142. package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +50 -0
  143. package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +160 -0
  144. package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
  145. package/node_modules/@capyclaw/foundation/lib/kernel.mjs +185 -0
  146. package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
  147. package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
  148. package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
  149. package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +188 -0
  150. package/node_modules/@capyclaw/foundation/package.json +53 -0
  151. package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
  152. package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
  153. package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
  154. package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
  155. package/node_modules/@capyclaw/plugin-kit/package.json +69 -0
  156. package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
  157. package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
  158. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +129 -0
  159. package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +139 -0
  160. package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
  161. package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
  162. package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
  163. package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
  164. package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
  165. package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
  166. package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
  167. package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
  168. package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
  169. package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
  170. package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
  171. package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
  172. package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
  173. package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
  174. package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
  175. package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
  176. package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
  177. package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
  178. package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
  179. package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +656 -0
  180. package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
  181. package/node_modules/@capyclaw/portal/README.md +70 -0
  182. package/node_modules/@capyclaw/portal/lib/index.mjs +387 -0
  183. package/node_modules/@capyclaw/portal/lib/ledger.mjs +194 -0
  184. package/node_modules/@capyclaw/portal/lib/portal-agent.ps1 +811 -0
  185. package/node_modules/@capyclaw/portal/lib/portal-darwin.mjs +183 -0
  186. package/node_modules/@capyclaw/portal/package.json +40 -0
  187. package/node_modules/@capyclaw/service-broker/README.md +104 -0
  188. package/node_modules/@capyclaw/service-broker/cordis.patch.yml +8 -0
  189. package/node_modules/@capyclaw/service-broker/dsh-suite.json +21 -0
  190. package/node_modules/@capyclaw/service-broker/lib/control.mjs +193 -0
  191. package/node_modules/@capyclaw/service-broker/lib/discovery.mjs +109 -0
  192. package/node_modules/@capyclaw/service-broker/lib/engine.mjs +1236 -0
  193. package/node_modules/@capyclaw/service-broker/lib/host.mjs +182 -0
  194. package/node_modules/@capyclaw/service-broker/lib/manifest.mjs +249 -0
  195. package/node_modules/@capyclaw/service-broker/lib/recovery.mjs +325 -0
  196. package/node_modules/@capyclaw/service-broker/lib/skills-client.mjs +28 -0
  197. package/node_modules/@capyclaw/service-broker/lib/spec.mjs +78 -0
  198. package/node_modules/@capyclaw/service-broker/package.json +65 -0
  199. package/node_modules/@capyclaw/service-broker/server.mjs +81 -0
  200. package/node_modules/@capyclaw/service-broker/service.schema.json +62 -0
  201. package/node_modules/@capyclaw/service-broker/services.json +3 -0
  202. package/node_modules/@capyclaw/service-host/README.md +86 -0
  203. package/node_modules/@capyclaw/service-host/cordis.patch.yml +4 -0
  204. package/node_modules/@capyclaw/service-host/dsh-suite.json +29 -0
  205. package/node_modules/@capyclaw/service-host/lib/host.mjs +22 -0
  206. package/node_modules/@capyclaw/service-host/package.json +60 -0
  207. package/node_modules/@capyclaw/service-host/src/index.mjs +571 -0
  208. package/node_modules/@capyclaw/service-host/src/tuning.mjs +63 -0
  209. package/node_modules/@capyclaw/tool-host/README.md +56 -0
  210. package/node_modules/@capyclaw/tool-host/cordis.patch.yml +4 -0
  211. package/node_modules/@capyclaw/tool-host/dsh-suite.json +30 -0
  212. package/node_modules/@capyclaw/tool-host/lib/host.mjs +16 -0
  213. package/node_modules/@capyclaw/tool-host/package.json +59 -0
  214. package/node_modules/@capyclaw/tool-host/src/descriptors.mjs +166 -0
  215. package/node_modules/@capyclaw/tool-host/src/index.mjs +416 -0
  216. package/package.json +31 -14
  217. package/services.json +1 -21
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@capyclaw/plugin-kit",
3
+ "version": "0.2.6",
4
+ "description": "DSH 插件基础设施标准件:HTTP/MCP、单实例锁、参数校验、技能 provider、journal 存储、编辑器传输与租约。",
5
+ "type": "module",
6
+ "main": "./src/index.mjs",
7
+ "exports": {
8
+ ".": "./src/index.mjs",
9
+ "./service/http": "./src/service/http.mjs",
10
+ "./service/mcp-http": "./src/service/mcp-http.mjs",
11
+ "./service/server": "./src/service/server.mjs",
12
+ "./service/api": "./src/service/api.mjs",
13
+ "./tools/validate": "./src/tools/validate.mjs",
14
+ "./tools/result": "./src/tools/result.mjs",
15
+ "./skills/provider": "./src/skills/provider.mjs",
16
+ "./editor/transport": "./src/editor/transport.js",
17
+ "./editor/lease": "./src/editor/lease.mjs",
18
+ "./editor/shadow": "./src/editor/shadow.mjs",
19
+ "./store/journal-store": "./src/store/journal-store.mjs",
20
+ "./store/blob-store": "./src/store/blob-store.mjs",
21
+ "./store/content-store": "./src/store/content-store.mjs",
22
+ "./workspace/manager": "./src/workspace/manager.mjs",
23
+ "./runtime/context": "./src/runtime/context.mjs",
24
+ "./packaging/pack": "./src/pack.mjs",
25
+ "./packaging/zip": "./src/zip.mjs",
26
+ "./packaging/self-contained": "./src/self-contained.mjs",
27
+ "./cli": "./src/cli.mjs",
28
+ "./package.json": "./package.json",
29
+ "./config": "./src/config.mjs",
30
+ "./tuning": "./src/tuning.mjs",
31
+ "./util/fs-lock": "./src/util/fs-lock.mjs",
32
+ "./util/http": "./src/util/http.mjs",
33
+ "./editor/heartbeat-worker": "./src/editor/heartbeat-worker.js"
34
+ },
35
+ "bin": {
36
+ "@capyclaw/plugin-kit-sync": "./bin/sync.mjs"
37
+ },
38
+ "files": [
39
+ "bin/",
40
+ "src/",
41
+ "README.md",
42
+ "INTEGRATION.md"
43
+ ],
44
+ "scripts": {
45
+ "test": "node --test \"test/*.test.mjs\""
46
+ },
47
+ "engines": {
48
+ "node": ">=18"
49
+ },
50
+ "license": "MIT",
51
+ "publishConfig": {
52
+ "access": "public"
53
+ },
54
+ "repository": {
55
+ "type": "git",
56
+ "url": "git+https://github.com/Maple496/capyclaw.git",
57
+ "directory": "C/packages/capyclaw-plugin-kit"
58
+ },
59
+ "keywords": [
60
+ "dsh",
61
+ "dsh-plugin",
62
+ "cordis",
63
+ "plugin-sdk"
64
+ ],
65
+ "bugs": {
66
+ "url": "https://github.com/Maple496/capyclaw/issues"
67
+ },
68
+ "homepage": "https://github.com/Maple496/capyclaw/tree/main/C/packages/capyclaw-plugin-kit#readme"
69
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * CLI 脚手架(纯函数,无副作用)。
3
+ *
4
+ * 全仓 8+ 个 bin 脚本各自手写参数解析/导入守卫/失败退出——这里收敛为一套:
5
+ * 新的 bin 只需要写「命令表 + 用法文本 + 业务逻辑」,解析与守卫交给本模块。
6
+ *
7
+ * import { parseArgs, isMain, fail } from '@capyclaw/plugin-kit'
8
+ *
9
+ * const commands = { async do(arg, flags) { … } }
10
+ * if (isMain(import.meta.url)) { … dispatch & print usage … }
11
+ */
12
+ import { resolve } from 'node:path'
13
+ import { pathToFileURL } from 'node:url'
14
+
15
+ /**
16
+ * 解析命令行:位置参数与 `--flag`/`--flag value` 混排。
17
+ * @param {string[]} argv - 不含 node 与脚本路径的原始参数
18
+ * @returns {{ positional: string[], flags: Record<string, string|boolean> }}
19
+ * `--flag value` → 字符串;`--flag`(后随缺失或另一个 -- 项)→ true;`--flag=x` → 字符串
20
+ */
21
+ export function parseArgs(argv) {
22
+ const positional = []
23
+ const flags = {}
24
+ for (let i = 0; i < argv.length; i++) {
25
+ const arg = argv[i]
26
+ if (arg.startsWith('--')) {
27
+ const eq = arg.indexOf('=')
28
+ if (eq > 2) {
29
+ flags[arg.slice(2, eq)] = arg.slice(eq + 1)
30
+ continue
31
+ }
32
+ const key = arg.slice(2)
33
+ const next = argv[i + 1]
34
+ if (next !== undefined && !next.startsWith('--')) { flags[key] = next; i++ }
35
+ else flags[key] = true
36
+ } else {
37
+ positional.push(arg)
38
+ }
39
+ }
40
+ return { positional, flags }
41
+ }
42
+
43
+ /**
44
+ * 导入守卫:仅当本模块被直接执行(而非被测试/其他模块 import)时为 true。
45
+ * @param {string} importMetaUrl - 调用方的 import.meta.url
46
+ */
47
+ export function isMain(importMetaUrl, argv = process.argv) {
48
+ const entry = argv[1] ? pathToFileURL(resolve(argv[1])).href : ''
49
+ return importMetaUrl === entry
50
+ }
51
+
52
+ /** 统一失败出口:打印 ✗ 后以非零码退出。 */
53
+ export function fail(message) {
54
+ console.error(`✗ ${message}`)
55
+ process.exit(1)
56
+ }
@@ -0,0 +1,142 @@
1
+ /**
2
+ * 插件配置解析(统一入口)。
3
+ *
4
+ * 目的:让插件不把端口/URL/路径写死在代码或 cordis.patch.yml 里,
5
+ * 而是遵循一条可预测的优先级链:
6
+ *
7
+ * ctx config(由 stack / profile patch 注入的运行时配置)
8
+ * → 环境变量(逐字段,如 DSH_LAUNCHPAD_PORT)
9
+ * → defaults(安全默认值)
10
+ *
11
+ * 所有取值都带上来源,便于 doctor / 日志说明「这个值是哪来的」。
12
+ *
13
+ * 用法:
14
+ * import { resolveConfig } from '@capyclaw/plugin-kit/config'
15
+ *
16
+ * export function apply(ctx, config = {}) {
17
+ * const cfg = resolveConfig(config, {
18
+ * envPrefix: 'DSH_PPT',
19
+ * 不写端口默认值:端口由基建的 runtime.json / env 注入(见 INTEGRATION.md)。
20
+ * defaults: { host: '127.0.0.1' },
21
+ * fields: {
22
+ * port: { env: 'PORT', type: 'port' },
23
+ * host: { env: 'HOST' },
24
+ * backendUrl: { env: 'BACKEND_URL', type: 'url' },
25
+ * },
26
+ * })
27
+ * cfg.value.port // 解析后的值
28
+ * cfg.source.port // 'config' | 'env:DSH_PPT_PORT' | 'default'
29
+ * }
30
+ */
31
+
32
+ const TYPE_CHECKS = {
33
+ port(value) {
34
+ const n = Number(value)
35
+ return Number.isInteger(n) && n > 0 && n < 65536 ? n : null
36
+ },
37
+ number(value) {
38
+ const n = Number(value)
39
+ return Number.isFinite(n) ? n : null
40
+ },
41
+ boolean(value) {
42
+ if (typeof value === 'boolean') return value
43
+ const s = String(value).trim().toLowerCase()
44
+ if (['1', 'true', 'yes', 'on'].includes(s)) return true
45
+ if (['0', 'false', 'no', 'off'].includes(s)) return false
46
+ return null
47
+ },
48
+ url(value) {
49
+ const s = String(value).trim()
50
+ try {
51
+ const u = new URL(s)
52
+ return u.protocol === 'http:' || u.protocol === 'https:' ? s : null
53
+ } catch {
54
+ return null
55
+ }
56
+ },
57
+ string(value) {
58
+ const s = String(value)
59
+ return s.length ? s : null
60
+ },
61
+ }
62
+
63
+ function coerce(raw, type) {
64
+ if (raw === undefined || raw === null || raw === '') return { ok: false }
65
+ const check = TYPE_CHECKS[type] || TYPE_CHECKS.string
66
+ const value = check(raw)
67
+ return value === null ? { ok: false } : { ok: true, value }
68
+ }
69
+
70
+ /**
71
+ * @param {object} config dsh 传给 apply() 的 config(来自 cordis.patch.yml / stack configs)
72
+ * @param {object} options
73
+ * @param {string} [options.envPrefix] 环境变量前缀,如 'DSH_PPT'
74
+ * @param {object} [options.defaults] 默认值表
75
+ * @param {object} [options.fields] { 字段名: { env?: string, type?: 'port'|'number'|'boolean'|'url'|'string' } }
76
+ * @param {(msg:string)=>void} [options.onInvalid] 无效值回调(默认收集到 issues)
77
+ * @returns {{ value: object, source: object, issues: string[] }}
78
+ */
79
+ export function resolveConfig(config = {}, options = {}) {
80
+ const { envPrefix = '', defaults = {}, fields = {}, env = process.env } = options
81
+ const value = {}
82
+ const source = {}
83
+ const issues = []
84
+
85
+ const keys = new Set([...Object.keys(defaults), ...Object.keys(fields)])
86
+
87
+ for (const key of keys) {
88
+ const field = fields[key] || {}
89
+ const type = field.type || inferType(defaults[key])
90
+ const envName = field.env
91
+ ? (envPrefix ? `${envPrefix}_${field.env}` : field.env)
92
+ : (envPrefix ? `${envPrefix}_${key.replace(/([a-z0-9])([A-Z])/g, '$1_$2').toUpperCase()}` : '')
93
+
94
+ // 1) ctx config
95
+ const fromConfig = coerce(config?.[key], type)
96
+ if (fromConfig.ok) {
97
+ value[key] = fromConfig.value
98
+ source[key] = 'config'
99
+ continue
100
+ }
101
+ if (config?.[key] !== undefined && config?.[key] !== null && config?.[key] !== '') {
102
+ issues.push(`${key}: config 值无效(期望 ${type}): ${JSON.stringify(config[key])},已降级到下一来源`)
103
+ }
104
+
105
+ // 2) 环境变量
106
+ if (envName && env?.[envName] !== undefined) {
107
+ const fromEnv = coerce(env[envName], type)
108
+ if (fromEnv.ok) {
109
+ value[key] = fromEnv.value
110
+ source[key] = `env:${envName}`
111
+ continue
112
+ }
113
+ issues.push(`${key}: 环境变量 ${envName} 值无效(期望 ${type}): ${JSON.stringify(env[envName])},已降级到默认值`)
114
+ }
115
+
116
+ // 3) 默认值
117
+ if (defaults[key] !== undefined) {
118
+ value[key] = defaults[key]
119
+ source[key] = 'default'
120
+ continue
121
+ }
122
+
123
+ // 无默认值且没解析到 → 不写入,交给调用方判断是否必填
124
+ source[key] = 'unset'
125
+ }
126
+
127
+ return { value, source, issues }
128
+ }
129
+
130
+ function inferType(sample) {
131
+ if (typeof sample === 'boolean') return 'boolean'
132
+ if (typeof sample === 'number') return 'number'
133
+ if (typeof sample === 'string' && /^https?:\/\//.test(sample)) return 'url'
134
+ return 'string'
135
+ }
136
+
137
+ /** 便于在 apply() 里打一条可读的配置来源日志。 */
138
+ export function describeConfig({ value, source }) {
139
+ return Object.entries(value)
140
+ .map(([k, v]) => `${k}=${JSON.stringify(v)} (${source[k] || 'unknown'})`)
141
+ .join(', ')
142
+ }
@@ -0,0 +1,129 @@
1
+ /* Web Worker:独立线程保持心跳,不受浏览器标签页节流影响
2
+ *
3
+ * 设计原则:
4
+ * 1. 健壮降级:任何失败都通知主线程,主线程决定是否降级
5
+ * 2. 自我监控:内置健康检查,连续失败自动报告
6
+ * 3. 优雅退出:stop 后立即停止,不留残留定时器
7
+ */
8
+
9
+ let active = false;
10
+ let clientId = null;
11
+ let apiBase = null;
12
+ let intervalHandle = null;
13
+ let consecutiveFailures = 0;
14
+ const MAX_FAILURES = 3; // 连续失败 3 次后通知主线程降级
15
+
16
+ self.onmessage = function(e) {
17
+ const { type, data } = e.data;
18
+
19
+ if (type === 'start') {
20
+ active = true;
21
+ clientId = data.clientId;
22
+ apiBase = data.apiBase;
23
+ consecutiveFailures = 0;
24
+ startHeartbeat();
25
+ self.postMessage({ type: 'started', clientId });
26
+ } else if (type === 'stop') {
27
+ active = false;
28
+ if (intervalHandle) {
29
+ clearInterval(intervalHandle);
30
+ intervalHandle = null;
31
+ }
32
+ self.postMessage({ type: 'stopped' });
33
+ } else if (type === 'ping') {
34
+ // 主线程健康检查:返回当前状态
35
+ self.postMessage({
36
+ type: 'pong',
37
+ active,
38
+ clientId,
39
+ consecutiveFailures,
40
+ timestamp: Date.now()
41
+ });
42
+ }
43
+ };
44
+
45
+ function startHeartbeat() {
46
+ if (intervalHandle) clearInterval(intervalHandle);
47
+
48
+ // 每 4 秒发送心跳(留 1 秒缓冲,避免刚好 5 秒时被判过期)
49
+ intervalHandle = setInterval(() => {
50
+ if (!active) return;
51
+
52
+ sendClaim().catch(err => {
53
+ consecutiveFailures++;
54
+ self.postMessage({
55
+ type: 'error',
56
+ message: err.message,
57
+ consecutiveFailures,
58
+ fatal: consecutiveFailures >= MAX_FAILURES
59
+ });
60
+
61
+ // 连续失败达到阈值,建议主线程降级
62
+ if (consecutiveFailures >= MAX_FAILURES) {
63
+ self.postMessage({
64
+ type: 'fatal',
65
+ message: `连续 ${MAX_FAILURES} 次心跳失败,建议降级到主线程`
66
+ });
67
+ }
68
+ });
69
+ }, 4000);
70
+
71
+ // 立即发送第一次
72
+ sendClaim().catch(() => {});
73
+ }
74
+
75
+ async function sendClaim() {
76
+ if (!apiBase || !clientId) {
77
+ throw new Error('Worker 未初始化:缺少 apiBase 或 clientId');
78
+ }
79
+
80
+ const url = `${apiBase}/claim`;
81
+ const controller = new AbortController();
82
+ const timeoutId = setTimeout(() => controller.abort(), 8000); // 8 秒超时
83
+
84
+ try {
85
+ const response = await fetch(url, {
86
+ method: 'POST',
87
+ headers: { 'content-type': 'application/json' },
88
+ body: JSON.stringify({ id: clientId }),
89
+ signal: controller.signal,
90
+ });
91
+
92
+ clearTimeout(timeoutId);
93
+
94
+ if (!response.ok) {
95
+ throw new Error(`HTTP ${response.status}`);
96
+ }
97
+
98
+ const data = await response.json();
99
+
100
+ /* HTTP 200 但 ok:false = 租约已被其他窗口抢走(claim 被拒)。
101
+ * 不能当成功:停止心跳并通知主线程回到待命,否则被抢占的窗口会一直
102
+ * 以为自己还是 owner,继续写共享画面。 */
103
+ if (data && data.ok === false) {
104
+ active = false;
105
+ if (intervalHandle) {
106
+ clearInterval(intervalHandle);
107
+ intervalHandle = null;
108
+ }
109
+ self.postMessage({ type: 'lost', owner: data.owner, epoch: data.epoch });
110
+ return;
111
+ }
112
+
113
+ // 成功:重置失败计数
114
+ if (consecutiveFailures > 0) {
115
+ consecutiveFailures = 0;
116
+ self.postMessage({ type: 'recovered' }); // 通知主线程已恢复
117
+ }
118
+
119
+ self.postMessage({
120
+ type: 'heartbeat',
121
+ timestamp: Date.now(),
122
+ response: data,
123
+ consecutiveFailures: 0
124
+ });
125
+ } catch (err) {
126
+ clearTimeout(timeoutId);
127
+ throw err;
128
+ }
129
+ }
@@ -0,0 +1,139 @@
1
+ /**
2
+ * 「显示即连接」编辑器租约标准模块。
3
+ *
4
+ * 活动权(claim)只存在于内部仲裁;不要把它渲染成 UI 状态。
5
+ * 新打开的窗口强制抢占,旧窗口静默转入待命。
6
+ */
7
+ import { tuning } from '../tuning.mjs'
8
+
9
+ const DEFAULT_TTL = 15 // 秒
10
+ const DEFAULT_HEARTBEAT = 3 // 秒(心跳间隔应小于 TTL)
11
+
12
+ /**
13
+ * 创建编辑器租约仲裁器。
14
+ * @param {{ ttl?: number }} [opts]
15
+ */
16
+ export function createEditorLease(opts = {}) {
17
+ const ttl = Number(opts.ttl) || DEFAULT_TTL
18
+ let owner = null // { id, ts }
19
+
20
+ return {
21
+ claim(id, force = false) {
22
+ const now = Date.now() / 1000
23
+ if (force || !owner || owner.id === id || now - owner.ts > ttl) {
24
+ owner = { id, ts: now }
25
+ return { ok: true, owner: id }
26
+ }
27
+ return { ok: false, owner: owner.id }
28
+ },
29
+
30
+ release(id) {
31
+ if (owner && owner.id === id) owner = null
32
+ return { ok: true }
33
+ },
34
+
35
+ isLive() {
36
+ return !!owner && Date.now() / 1000 - owner.ts <= ttl
37
+ },
38
+
39
+ status() {
40
+ return owner
41
+ ? { owner: owner.id, age: Math.round((Date.now() / 1000 - owner.ts) * 100) / 100 }
42
+ : null
43
+ },
44
+
45
+ async wait(o = {}) {
46
+ const { leaseWaitPollMs: pollMs, leaseWaitTimeoutMs: defaultTimeoutMs } = tuning()
47
+ const deadline = Date.now() + (o.timeoutMs || defaultTimeoutMs)
48
+ while (true) {
49
+ if (this.isLive()) return this.status()
50
+ if (Date.now() >= deadline) {
51
+ const hint = o.openHint ? `(打开 ${o.openHint})` : ''
52
+ const panel = o.panelHint || '编辑器面板'
53
+ throw new Error(`${panel}尚未连接${hint}。页面加载后会自动连接,无需其他操作。`)
54
+ }
55
+ await new Promise((resolve) => setTimeout(resolve, pollMs))
56
+ }
57
+ },
58
+ }
59
+ }
60
+
61
+ /**
62
+ * 编辑器端自动挂接循环。
63
+ * 首次 claim 强制抢占(后打开者赢),之后静默心跳。
64
+ * @param {{
65
+ * clientId: string,
66
+ * claim: (force: boolean) => Promise<boolean>,
67
+ * release?: () => void,
68
+ * onActive?: () => void,
69
+ * onStandby?: () => void,
70
+ * retryMs?: number,
71
+ * heartbeatMs?: number,
72
+ * setInterval?: (cb: () => void) => any,
73
+ * clearInterval?: (handle: any) => void
74
+ * }} o
75
+ */
76
+ export function startAutoAttach(o) {
77
+ if (!o || !o.clientId) throw new Error('startAutoAttach: clientId 必填')
78
+ if (typeof o.claim !== 'function') throw new Error('startAutoAttach: claim 必须是函数')
79
+
80
+ const retryMs = o.retryMs || 1000
81
+ const heartbeatMs = o.heartbeatMs || DEFAULT_HEARTBEAT * 1000
82
+ const setI = o.setInterval || ((cb) => setInterval(cb, heartbeatMs))
83
+ const clearI = o.clearInterval || ((handle) => clearInterval(handle))
84
+
85
+ let active = false
86
+ let heartbeat = null
87
+ let disposed = false
88
+
89
+ async function tryClaim(force) {
90
+ try {
91
+ return await o.claim(force)
92
+ } catch {
93
+ return false
94
+ }
95
+ }
96
+
97
+ function goActive() {
98
+ if (active) return
99
+ active = true
100
+ try { o.onActive && o.onActive() } catch { /* 用户回调失败不阻塞挂接 */ }
101
+ heartbeat = setI(async () => {
102
+ if (disposed) return
103
+ const ok = await tryClaim(false)
104
+ if (!ok) goStandby()
105
+ })
106
+ }
107
+
108
+ function goStandby() {
109
+ if (!active) return
110
+ active = false
111
+ if (heartbeat != null) {
112
+ clearI(heartbeat)
113
+ heartbeat = null
114
+ }
115
+ try { o.onStandby && o.onStandby() } catch { /* 静默 */ }
116
+ }
117
+
118
+ ;(async () => {
119
+ let first = true
120
+ while (!disposed) {
121
+ const ok = await tryClaim(first)
122
+ first = false
123
+ if (ok) {
124
+ goActive()
125
+ return
126
+ }
127
+ await new Promise((resolve) => setTimeout(resolve, retryMs))
128
+ }
129
+ })()
130
+
131
+ return {
132
+ get active() { return active },
133
+ dispose() {
134
+ disposed = true
135
+ goStandby()
136
+ try { o.release && o.release() } catch { /* 释放失败不阻塞退出 */ }
137
+ },
138
+ }
139
+ }