@capyclaw/windows-control 0.0.0-stage → 0.1.3

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 (99) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +142 -2
  3. package/THIRD_PARTY_NOTICES.md +20 -0
  4. package/cordis.patch.yml +32 -0
  5. package/lib/config.mjs +126 -0
  6. package/lib/mcp-json.mjs +80 -0
  7. package/node_modules/@capyclaw/foundation/README.md +51 -0
  8. package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
  9. package/node_modules/@capyclaw/foundation/abi/bridge.mjs +88 -0
  10. package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
  11. package/node_modules/@capyclaw/foundation/abi/index.mjs +50 -0
  12. package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
  13. package/node_modules/@capyclaw/foundation/abi/loader.mjs +352 -0
  14. package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
  15. package/node_modules/@capyclaw/foundation/abi/spec.mjs +258 -0
  16. package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
  17. package/node_modules/@capyclaw/foundation/abi/version.mjs +140 -0
  18. package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
  19. package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
  20. package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
  21. package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
  22. package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
  23. package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
  24. package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +72 -0
  25. package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
  26. package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
  27. package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
  28. package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
  29. package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
  30. package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
  31. package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
  32. package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
  33. package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +38 -0
  34. package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +88 -0
  35. package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +54 -0
  36. package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +187 -0
  37. package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
  38. package/node_modules/@capyclaw/foundation/lib/kernel.mjs +195 -0
  39. package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
  40. package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
  41. package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
  42. package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +198 -0
  43. package/node_modules/@capyclaw/foundation/package.json +53 -0
  44. package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
  45. package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
  46. package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
  47. package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
  48. package/node_modules/@capyclaw/plugin-kit/package.json +71 -0
  49. package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
  50. package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
  51. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +132 -0
  52. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
  53. package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +60 -0
  54. package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
  55. package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
  56. package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
  57. package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
  58. package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
  59. package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
  60. package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
  61. package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
  62. package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
  63. package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
  64. package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
  65. package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
  66. package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
  67. package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
  68. package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
  69. package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
  70. package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
  71. package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
  72. package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
  73. package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
  74. package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +664 -0
  75. package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
  76. package/node_modules/@capyclaw/portal/README.md +70 -0
  77. package/node_modules/@capyclaw/portal/lib/index.mjs +387 -0
  78. package/node_modules/@capyclaw/portal/lib/ledger.mjs +194 -0
  79. package/node_modules/@capyclaw/portal/lib/portal-agent.ps1 +811 -0
  80. package/node_modules/@capyclaw/portal/lib/portal-darwin.mjs +183 -0
  81. package/node_modules/@capyclaw/portal/package.json +40 -0
  82. package/node_modules/@capyclaw/service-broker/README.md +104 -0
  83. package/node_modules/@capyclaw/service-broker/cordis.patch.yml +8 -0
  84. package/node_modules/@capyclaw/service-broker/dsh-suite.json +21 -0
  85. package/node_modules/@capyclaw/service-broker/lib/control.mjs +209 -0
  86. package/node_modules/@capyclaw/service-broker/lib/discovery.mjs +109 -0
  87. package/node_modules/@capyclaw/service-broker/lib/engine.mjs +1290 -0
  88. package/node_modules/@capyclaw/service-broker/lib/host.mjs +187 -0
  89. package/node_modules/@capyclaw/service-broker/lib/manifest.mjs +262 -0
  90. package/node_modules/@capyclaw/service-broker/lib/recovery.mjs +275 -0
  91. package/node_modules/@capyclaw/service-broker/lib/skills-client.mjs +48 -0
  92. package/node_modules/@capyclaw/service-broker/lib/spec.mjs +78 -0
  93. package/node_modules/@capyclaw/service-broker/package.json +65 -0
  94. package/node_modules/@capyclaw/service-broker/server.mjs +81 -0
  95. package/node_modules/@capyclaw/service-broker/service.schema.json +62 -0
  96. package/node_modules/@capyclaw/service-broker/services.json +3 -0
  97. package/package.json +65 -4
  98. package/plugin.mjs +32 -0
  99. package/server/relay.mjs +752 -0
@@ -0,0 +1,194 @@
1
+ /**
2
+ * 端口活动台账 —— 「谁被操作过、操作了多少次」的唯一事实来源。
3
+ *
4
+ * 由来:以前每次工具调用都会强弹一扇窗,很吵。现在改成:**调用只记账,不弹窗**;
5
+ * DSH 界面右下角有一个浮动图标(@capyclaw/portal-hud),显示最近被操作过的端口
6
+ * 与累计次数(封顶 99+),点端口才真的开窗。
7
+ *
8
+ * 落盘位置:`$DSH_HOME/portal-ledger.json`(缺省 `~/.dsh/portal-ledger.json`),
9
+ * 与 `services.json` 同级 —— 跨进程(桥 / broker / HUD 宿主)共享同一份账本。
10
+ *
11
+ * 并发:读-改-写在 4KB 级别的小文件上同步完成,原子写(tmp + rename)。
12
+ * 台账是「展示用的计数」,丢一次更新无伤大雅,不做文件锁。
13
+ */
14
+
15
+ import fs from 'node:fs'
16
+ import os from 'node:os'
17
+ import path from 'node:path'
18
+
19
+ const VERSION = 1
20
+
21
+ /** 最多保留多少个端口(按 lastAt 排序)——浮动面板不需要无限历史。 */
22
+ const MAX_PORTS = 24
23
+
24
+ /** 计数上限:面板上显示 99+,但内部仍继续累加(1024 封顶,避免数字无限膨胀)。 */
25
+ const MAX_COUNT = 1024
26
+
27
+ function homeDir() {
28
+ const env = String(process.env.DSH_HOME || '').trim()
29
+ if (env) return env
30
+ return path.join(os.homedir(), '.dsh')
31
+ }
32
+
33
+ /** 台账文件路径;`DSH_PORTAL_LEDGER` 可覆盖(测试用)。 */
34
+ export function ledgerPath() {
35
+ const override = String(process.env.DSH_PORTAL_LEDGER || '').trim()
36
+ if (override) return override
37
+ return path.join(homeDir(), 'portal-ledger.json')
38
+ }
39
+
40
+ function emptyLedger() {
41
+ return { version: VERSION, updatedAt: new Date().toISOString(), total: 0, ports: [] }
42
+ }
43
+
44
+ /** 从 URL 里取端口号(`http://127.0.0.1:<port>/` → `<port>`;取不到返回 0)。 */
45
+ export function servicePort(url) {
46
+ const text = String(url || '')
47
+ const hit = text.match(/^https?:\/\/[^/:]+:(\d+)/i)
48
+ return hit ? Number(hit[1]) : 0
49
+ }
50
+
51
+ function normalizeEntry(raw) {
52
+ if (!raw || typeof raw !== 'object') return null
53
+ const url = String(raw.url || '')
54
+ const id = String(raw.id || '')
55
+ if (!url && !id) return null
56
+ return {
57
+ id,
58
+ title: String(raw.title || id || url),
59
+ url,
60
+ port: Number(raw.port || servicePort(url) || 0) || null,
61
+ count: Math.max(0, Math.min(MAX_COUNT, Number(raw.count) || 0)),
62
+ firstAt: String(raw.firstAt || raw.lastAt || ''),
63
+ lastAt: String(raw.lastAt || ''),
64
+ window: raw.window && typeof raw.window === 'object' ? { ...raw.window } : null,
65
+ }
66
+ }
67
+
68
+ /**
69
+ * 同一个服务重启后端口会变(`ports.main: "auto"`):台账必须按 **id** 认人,
70
+ * 不能按 URL 认 —— 否则旧端口的死条目会一直留在面板上,点它就是「打不开」。
71
+ * 读到历史遗留的重复条目时合并:计数相加,保留最新一条的地址。
72
+ */
73
+ function mergeDuplicates(ports) {
74
+ const merged = []
75
+ const byKey = new Map()
76
+ for (const one of ports) {
77
+ const key = one.id || one.url
78
+ const known = byKey.get(key)
79
+ if (!known) {
80
+ byKey.set(key, one)
81
+ merged.push(one)
82
+ continue
83
+ }
84
+ const [newer, older] = String(one.lastAt) >= String(known.lastAt) ? [one, known] : [known, one]
85
+ newer.count = Math.min(MAX_COUNT, newer.count + older.count)
86
+ newer.firstAt = older.firstAt || newer.firstAt
87
+ if (!newer.url && older.url) {
88
+ newer.url = older.url
89
+ newer.port = older.port
90
+ }
91
+ const index = merged.indexOf(older)
92
+ if (index >= 0) merged[index] = newer
93
+ byKey.set(key, newer)
94
+ }
95
+ return merged
96
+ }
97
+
98
+ /** 读台账;文件不存在/损坏时返回空台账(永不抛)。 */
99
+ export function readLedger() {
100
+ const file = ledgerPath()
101
+ let text = ''
102
+ try {
103
+ text = fs.readFileSync(file, 'utf8')
104
+ } catch {
105
+ return emptyLedger()
106
+ }
107
+ let parsed = null
108
+ try {
109
+ parsed = JSON.parse(text)
110
+ } catch {
111
+ return emptyLedger()
112
+ }
113
+ const raw = Array.isArray(parsed?.ports) ? parsed.ports.map(normalizeEntry).filter(Boolean) : []
114
+ const ports = mergeDuplicates(raw)
115
+ ports.sort((a, b) => String(b.lastAt).localeCompare(String(a.lastAt)))
116
+ const total = ports.reduce((sum, one) => sum + one.count, 0)
117
+ return {
118
+ version: VERSION,
119
+ updatedAt: String(parsed?.updatedAt || ''),
120
+ total,
121
+ ports: ports.slice(0, MAX_PORTS),
122
+ }
123
+ }
124
+
125
+ function writeLedger(ledger) {
126
+ const file = ledgerPath()
127
+ fs.mkdirSync(path.dirname(file), { recursive: true })
128
+ const tmp = `${file}.${process.pid}.tmp`
129
+ fs.writeFileSync(tmp, `${JSON.stringify(ledger, null, 2)}\n`, 'utf8')
130
+ fs.renameSync(tmp, file)
131
+ }
132
+
133
+ /**
134
+ * 记一次活动:同一端口 count +1(封顶 99+ 展示)。
135
+ *
136
+ * 调用方(MCP 桥 / service-broker)在**每次**工具调用时调用它,
137
+ * 这里只碰一个小 JSON 文件,失败也只吞掉 —— 记账绝不能拖慢或打断工具调用。
138
+ */
139
+ export function recordActivity(input = {}) {
140
+ const url = String(input.url || '')
141
+ const id = String(input.id || '')
142
+ if (!url && !id) return null
143
+ const at = String(input.at || new Date().toISOString())
144
+ const ledger = readLedger()
145
+ /* 按 id 认人:服务重启换了端口,是同一条记录换地址,而不是第二条端口。 */
146
+ const existing = id
147
+ ? ledger.ports.find((one) => one.id === id)
148
+ : ledger.ports.find((one) => one.url === url)
149
+ if (existing) {
150
+ existing.count = Math.min(MAX_COUNT, existing.count + 1)
151
+ existing.lastAt = at
152
+ if (url) {
153
+ existing.url = url
154
+ existing.port = Number(input.port || servicePort(url) || 0) || existing.port || null
155
+ }
156
+ if (input.title) existing.title = String(input.title)
157
+ if (input.window && typeof input.window === 'object') existing.window = { ...input.window }
158
+ } else {
159
+ ledger.ports.unshift(normalizeEntry({
160
+ id,
161
+ title: input.title || id || url,
162
+ url,
163
+ port: input.port || servicePort(url),
164
+ count: 1,
165
+ firstAt: at,
166
+ lastAt: at,
167
+ window: input.window || null,
168
+ }))
169
+ }
170
+ ledger.ports.sort((a, b) => String(b.lastAt).localeCompare(String(a.lastAt)))
171
+ ledger.ports = ledger.ports.slice(0, MAX_PORTS)
172
+ ledger.total = ledger.ports.reduce((sum, one) => sum + one.count, 0)
173
+ ledger.updatedAt = at
174
+ try {
175
+ writeLedger(ledger)
176
+ } catch {
177
+ /* 记不上就算了,绝不影响调用方 */
178
+ }
179
+ const saved = id
180
+ ? ledger.ports.find((one) => one.id === id)
181
+ : ledger.ports.find((one) => one.url === url)
182
+ return saved || null
183
+ }
184
+
185
+ /** 清空计数(浮动面板上的「清空」按钮)。 */
186
+ export function resetLedger() {
187
+ const ledger = emptyLedger()
188
+ try {
189
+ writeLedger(ledger)
190
+ } catch {
191
+ /* 忽略 */
192
+ }
193
+ return ledger
194
+ }