@capyclaw/caption-map 0.0.0-stage → 0.3.1

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 (78) hide show
  1. package/README.md +321 -2
  2. package/client/client.chat.js +441 -0
  3. package/client/client.js +1519 -0
  4. package/cordis.patch.yml +14 -0
  5. package/index.js +47 -0
  6. package/node_modules/@capyclaw/foundation/README.md +51 -0
  7. package/node_modules/@capyclaw/foundation/abi/bridge.mjs +86 -0
  8. package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
  9. package/node_modules/@capyclaw/foundation/abi/index.mjs +35 -0
  10. package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
  11. package/node_modules/@capyclaw/foundation/abi/loader.mjs +352 -0
  12. package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
  13. package/node_modules/@capyclaw/foundation/abi/spec.mjs +258 -0
  14. package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
  15. package/node_modules/@capyclaw/foundation/abi/version.mjs +140 -0
  16. package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
  17. package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
  18. package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
  19. package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
  20. package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
  21. package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +55 -0
  22. package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
  23. package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
  24. package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
  25. package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
  26. package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
  27. package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
  28. package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
  29. package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
  30. package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +34 -0
  31. package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +85 -0
  32. package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +50 -0
  33. package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +187 -0
  34. package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
  35. package/node_modules/@capyclaw/foundation/lib/kernel.mjs +185 -0
  36. package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
  37. package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
  38. package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
  39. package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +198 -0
  40. package/node_modules/@capyclaw/foundation/package.json +53 -0
  41. package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
  42. package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
  43. package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
  44. package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
  45. package/node_modules/@capyclaw/plugin-kit/package.json +69 -0
  46. package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
  47. package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
  48. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +129 -0
  49. package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +60 -0
  50. package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
  51. package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
  52. package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
  53. package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
  54. package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
  55. package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
  56. package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
  57. package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
  58. package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
  59. package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
  60. package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
  61. package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
  62. package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
  63. package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
  64. package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
  65. package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
  66. package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
  67. package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
  68. package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
  69. package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +656 -0
  70. package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
  71. package/node_modules/@capyclaw/window-kit/README.md +91 -0
  72. package/node_modules/@capyclaw/window-kit/client/client.js +531 -0
  73. package/node_modules/@capyclaw/window-kit/cordis.patch.yml +9 -0
  74. package/node_modules/@capyclaw/window-kit/icon.svg +8 -0
  75. package/node_modules/@capyclaw/window-kit/index.js +11 -0
  76. package/node_modules/@capyclaw/window-kit/package.json +62 -0
  77. package/options.mjs +27 -0
  78. package/package.json +67 -4
@@ -0,0 +1,531 @@
1
+ /*
2
+ * @capyclaw/window-kit —— 窗口几何与 dsh DOM 探针的**唯一底座**。
3
+ *
4
+ * 这是一个纯客户端模块包(无宿主路由、无模型面):factory 只返回一份 WINDOW_KIT
5
+ * 对象,本身不产生任何副作用(不发全局、不碰 DOM)。两个功能包
6
+ * (@capyclaw/portal-hud 主区页面、@capyclaw/chat-dock 会话副窗)各自在
7
+ * package.json 里声明 `dsh.client.external: ["@capyclaw/window-kit"]`,factory 里
8
+ * `require('@capyclaw/window-kit')` 拿到的就是这同一份(模块系统记忆化,单一真源)。
9
+ * 两功能包彼此不 require —— 它们之间只走 window CustomEvent 契约。
10
+ *
11
+ * 为什么单独成包:0.2.0 之前这套算式在两个包里各写一份,靠 window 全局握手对齐,
12
+ * 「谁先装载 / 探不到对方全局就罢工」+ 多重实现漂移(用户原话「说明存在多重实现」)。
13
+ * 现在算式只有这里一份,装载序由模块系统的 composition 保证(provider 先于 consumer),
14
+ * 不再有运行时探测分支。
15
+ *
16
+ * 本文件是单文件自包含 module node(不拆相对 chunk):它是叶子,不 require 任何消费者。
17
+ */
18
+ window.__ModuleLoader__.load({
19
+ id: '@capyclaw/window-kit',
20
+ factory: function (require) {
21
+ var module = { exports: {} }
22
+
23
+ var WINDOW_KIT = {
24
+ GUTTER: 12,
25
+ MIN_WIDTH: 360,
26
+ MIN_HEIGHT: 260,
27
+ DEFAULT_WIDTH: 560,
28
+ DEFAULT_HEIGHT: 420,
29
+ /* 新窗层叠落位时每一层错开的距离。 */
30
+ CASCADE: 24,
31
+ /* 浮层底座的层号(浮层按钮 = zBase + 400,派生见下方 Z.overlayButton)。 */
32
+ zBase: 1200,
33
+ /* 万一宿主没发布顶栏高度,也不能把 dsh 自己的标题栏盖死。 */
34
+ FALLBACK_CLEARANCE_WINDOWS: 32,
35
+ FALLBACK_CLEARANCE_DARWIN: 48,
36
+ /*
37
+ * 一个契约:数值大小就是层次,改这里等于改全局的「谁盖谁」。
38
+ * 会话副窗那两层(面板 + 壳);浮层按钮(chat-dock 召回按钮)在 Z.overlayButton,
39
+ * 由 zBase 派生(见下)。主区页面不画在浮层里,而是真的挂进主区容器
40
+ * (见 portal-hud 的 EmbeddedPanel),所以没有它的层号。
41
+ */
42
+ Z: { nativePanel: 1151, nativeFrame: 1152 },
43
+
44
+ /*
45
+ * 浮窗不许跑到 dsh 窗口外面:整块矩形收进视口,尺寸大过视口就先缩,再夹位置。
46
+ *
47
+ * **全工程只有这一份。** 主区页面(portal-hud 的 fitRect)与会话副窗
48
+ * (chat-dock 的 clampToViewport)调的都是它,差别只有 `area.top`:
49
+ * 会话副窗贴视口顶,主区那扇要让出 dsh 顶栏。其余规则一个字都不差。
50
+ */
51
+ fitRect: function (rect, area, options) {
52
+ var opts = options || {}
53
+ /* 防御性取数:任何非有限数(NaN / Infinity / undefined / 字符串)都退回安全默认,
54
+ * 绝不让一个坏字段把整块矩形染成 NaN(半残几何曾从账本 / 测量渗进来)。
55
+ * 合法数值原样透传 —— 既有精确输出一个像素都不变。 */
56
+ var num = function (v, fallback) { return typeof v === 'number' && isFinite(v) ? v : fallback }
57
+ rect = rect || {}
58
+ area = area || {}
59
+ var gutter = num(opts.gutter, WINDOW_KIT.GUTTER)
60
+ var top = num(area.top, gutter)
61
+ var areaWidth = num(area.width, WINDOW_KIT.DEFAULT_WIDTH + gutter * 2)
62
+ var areaHeight = num(area.height, WINDOW_KIT.DEFAULT_HEIGHT + top + gutter)
63
+ var areaW = Math.max(1, areaWidth - gutter * 2)
64
+ var areaH = Math.max(1, areaHeight - top - gutter)
65
+ var minW = Math.max(1, Math.min(num(opts.minWidth, WINDOW_KIT.MIN_WIDTH), areaW))
66
+ var minH = Math.max(1, Math.min(num(opts.minHeight, WINDOW_KIT.MIN_HEIGHT), areaH))
67
+ var w = Math.round(Math.min(Math.max(num(rect.w, WINDOW_KIT.DEFAULT_WIDTH), minW), areaW))
68
+ var h = Math.round(Math.min(Math.max(num(rect.h, WINDOW_KIT.DEFAULT_HEIGHT), minH), areaH))
69
+ return {
70
+ x: Math.round(Math.min(Math.max(num(rect.x, gutter), gutter), Math.max(gutter, areaWidth - gutter - w))),
71
+ y: Math.round(Math.min(Math.max(num(rect.y, top), top), Math.max(top, areaHeight - gutter - h))),
72
+ w: w,
73
+ h: h
74
+ }
75
+ },
76
+
77
+ /*
78
+ * 窗口画在哪儿:只有这一条规则 —— 账本里的矩形就是画面上的矩形。
79
+ * `live` 参数保留签名(恒传 null):单形态定稿后没有任何会拖的窗,几何不再有第二份。
80
+ */
81
+ frameLayout: function (box, live) {
82
+ var base = live || box
83
+ return { x: base.x, y: base.y, w: base.w, h: base.h }
84
+ },
85
+
86
+ /*
87
+ * 从一堆同标记的节点里挑「看得见、占地方最大的那个」。
88
+ * 同一枚标记在页面上可能不止一份(切会话后旧面板没立刻卸载、预渲染那份也在 DOM 里),
89
+ * 判据只有这一份,两个功能包不会算出两种答案。
90
+ */
91
+ largestVisible: function (selector, minWidth, minHeight, map) {
92
+ var list = []
93
+ try {
94
+ list = document.querySelectorAll ? Array.prototype.slice.call(document.querySelectorAll(selector)) : []
95
+ } catch (error) { list = [] }
96
+ if (!list.length) {
97
+ var single = null
98
+ try { single = document.querySelector(selector) } catch (error) { single = null }
99
+ if (single) list = [single]
100
+ }
101
+ var best = null
102
+ for (var index = 0; index < list.length; index += 1) {
103
+ var node = null
104
+ try { node = map ? map(list[index]) : list[index] } catch (error) { node = null }
105
+ if (!node) continue
106
+ var box = null
107
+ try { box = node.getBoundingClientRect() } catch (error) { box = null }
108
+ /* 非有限尺寸(脱挂 / 半残节点量出 NaN、Infinity)一律不算「看得见」,
109
+ * 否则 NaN 面积会骗过大小比较、Infinity 会永远压过真面板。 */
110
+ if (!box || !isFinite(box.width) || !isFinite(box.height) || box.width < minWidth || box.height < minHeight) continue
111
+ var area = box.width * box.height
112
+ if (!best || area > best.area) best = { node: node, box: box, area: area }
113
+ }
114
+ return best
115
+ },
116
+
117
+ /* 主区容器(可能有多份,取看得见的那份):把一页真正嵌进聊天区要靠它。 */
118
+ mainPanel: function () {
119
+ var hit = WINDOW_KIT.largestVisible('[class*="_mainPanel"]', 160, 120)
120
+ return hit ? hit.node : null
121
+ },
122
+
123
+ /*
124
+ * 左侧栏列(工作区 / 会话列表 + 底部「...更多」行那一整条)。
125
+ * 会话副窗的停泊几何以它为真源:输入框钉底行正上方、副窗钉输入框正上方,同享侧栏宽。
126
+ * 锚点是底行 `_footArea`(哈希无关,与 _mainPanel 同一约定),列 = 它的父节点。
127
+ * 折叠成窄轨(宽 < 120)或底行被藏(高 0)时算「不在」:调用方把副窗藏起、
128
+ * 还原列表预留空间,展开后再显现。返回 { el, foot, rect, footRect }。
129
+ */
130
+ sidebarPanel: function () {
131
+ var foot = null
132
+ try { foot = document.querySelector('[class*="_footArea"]') } catch (error) { foot = null }
133
+ if (!foot || typeof foot.getBoundingClientRect !== 'function') return null
134
+ var col = foot.parentElement
135
+ if (!col || typeof col.getBoundingClientRect !== 'function') return null
136
+ try {
137
+ var r = col.getBoundingClientRect()
138
+ var fr = foot.getBoundingClientRect()
139
+ if (!r || !fr || !isFinite(r.width) || !isFinite(r.height) ||
140
+ !isFinite(fr.width) || !isFinite(fr.height) ||
141
+ r.width < 120 || r.height < 160) return null
142
+ /* 底行真正被藏(display:none / 脱挂 → 宽高全 0)才算「侧栏不在」。
143
+ * 注意区分「空但还在」(高 0 宽 > 0:席位被区域映射搬走、或本就没有底部注册)——
144
+ * 那种情况侧栏仍在,副窗应贴列底显示,不能误判 lost。 */
145
+ if (fr.width <= 0 && fr.height <= 0) return null
146
+ /* 底行可能被「区域映射」类插件(caption-map)整行 fixed 搬去顶栏,或退化成
147
+ * 逐席位搬走后塌成高 0。这两种情况下底行不再代表「列的底边」,若仍拿它的
148
+ * getBoundingClientRect 当底锚,副窗/输入框/列表预留会全部算错(顶到 0、
149
+ * 高度夹到下限、底部留空、列表不让位)。所以底锚改以「列的底边」为真源:
150
+ * 底行还在流里就用它顶边,被搬走(fixed/absolute)或量不到就用列底。 */
151
+ var pos = ''
152
+ try { pos = getComputedStyle(foot).position } catch (e) { pos = '' }
153
+ var inFlow = pos !== 'fixed' && pos !== 'absolute'
154
+ var anchorTop = inFlow ? fr.top : r.bottom
155
+ var anchorH = inFlow ? fr.height : 0
156
+ if (!isFinite(anchorTop) || anchorTop < r.top - 1 || anchorTop > r.bottom + 1) anchorTop = r.bottom
157
+ var footRect = {
158
+ top: anchorTop, bottom: anchorTop + anchorH, height: anchorH,
159
+ width: fr.width > 0 ? fr.width : r.width, left: r.left, right: r.right
160
+ }
161
+ return { el: col, foot: foot, rect: r, footRect: footRect, footRelocated: !inFlow }
162
+ } catch (error) { return null }
163
+ },
164
+
165
+ /* 侧栏纵向滚动视口(导航 + 会话列表一起滚的那层):副窗占住底部时给它加
166
+ * padding-bottom 预留空间,列表末项才滚得到副窗上方。优先稳定 data 记号。 */
167
+ sidebarScroller: function (col) {
168
+ if (!col || typeof col.querySelector !== 'function') return null
169
+ try {
170
+ return col.querySelector('[data-sidebar-scrollport]') || col.querySelector('[class*="_scrollArea"]') || null
171
+ } catch (error) { return null }
172
+ },
173
+
174
+ /*
175
+ * 会话面板的根节点(「标题 + 页签 + 正文」那一整块)。
176
+ * 主区判断「会话还在不在屏幕上」、副窗把这块钉成停泊窗,判据必须同一个。
177
+ */
178
+ conversationRoot: function () {
179
+ var hit = WINDOW_KIT.largestVisible('[data-conversation-content]', 120, 80, function (body) {
180
+ return body.parentElement || body
181
+ })
182
+ return hit ? hit.node : null
183
+ },
184
+
185
+ /* 会话还在屏幕上吗(点侧栏的「插件」「自动化任务」时 dsh 会把整个会话视图卸掉)。 */
186
+ conversationShown: function () {
187
+ return !!WINDOW_KIT.conversationRoot()
188
+ },
189
+
190
+ /*
191
+ * 浮层注册的样板:等声明方把 `shell.overlay` 的 children 表注册出来再登记。
192
+ * 直接 register 会抛 `slot "shell.overlay" is not declared`(客户端条目比 ui-layout
193
+ * 先激活),而客户端条目 apply 抛错 = 整页 web boot 失败。两个功能包共用这一段。
194
+ */
195
+ mountOverlay: function (ctx, slot, entries) {
196
+ var list = entries || []
197
+ /* 同一页里只能有一份:宿主把插件 apply 两次(重复登记、热更没清干净)时,
198
+ * 两份浮层会各画一层。见过的 id 记在 ctx 上,两个模块实例共用同一个 ctx 也认得彼此。 */
199
+ var seen = null
200
+ try {
201
+ if (ctx) seen = ctx.__capyclawMountedIds || (ctx.__capyclawMountedIds = Object.create(null))
202
+ } catch (error) { seen = null }
203
+ var disposers = []
204
+ var mine = []
205
+ function undoEntries() {
206
+ for (var index = 0; index < disposers.length; index += 1) {
207
+ var fn = disposers[index]
208
+ disposers[index] = null
209
+ if (typeof fn === 'function') { try { fn() } catch (error) { /* 忽略 */ } }
210
+ }
211
+ /* 记账一并清掉:同一个 ctx 再装一次要能重新登记。 */
212
+ if (seen) for (var k = 0; k < mine.length; k += 1) delete seen[mine[k]]
213
+ mine.length = 0
214
+ }
215
+ var undoInject = null
216
+ try {
217
+ undoInject = ctx.slots.inject(slot, function () {
218
+ for (var index = 0; index < list.length; index += 1) {
219
+ var one = list[index]
220
+ var key = one.spec && one.spec.id
221
+ if (seen && key) {
222
+ if (seen[key]) continue
223
+ seen[key] = true
224
+ mine.push(key)
225
+ }
226
+ disposers.push(ctx.slots.register(one.spec, one.render))
227
+ }
228
+ return undoEntries
229
+ })
230
+ } catch (error) {
231
+ /* 插槽不可用只应让这块浮层缺席,不该把整个应用带崩。 */
232
+ console.warn('[' + (ctx && ctx.name ? ctx.name : 'surface') + '] 浮层插槽不可用:', error && error.message)
233
+ }
234
+ /* 卸载入口:插槽的注册(React 树)和本函数登记过的条目一起撤。幂等。 */
235
+ return function () {
236
+ try { if (typeof undoInject === 'function') undoInject() } catch (error) { /* 忽略 */ }
237
+ undoEntries()
238
+ }
239
+ },
240
+
241
+ /* 两个矩形是否完全相等(几何通知的去重判据,两功能包共用)。 */
242
+ sameRect: function (a, b) {
243
+ return !!a && !!b && a.x === b.x && a.y === b.y && a.w === b.w && a.h === b.h
244
+ },
245
+
246
+ /* 面板里可能存着半残几何:四个数不全是有限数,就当它没有。 */
247
+ finiteRect: function (panel) {
248
+ if (!panel) return null
249
+ var keys = ['x', 'y', 'w', 'h']
250
+ for (var i = 0; i < keys.length; i++) {
251
+ if (typeof panel[keys[i]] !== 'number' || !isFinite(panel[keys[i]])) return null
252
+ }
253
+ return { x: panel.x, y: panel.y, w: panel.w, h: panel.h }
254
+ }
255
+ }
256
+
257
+ /* ── 页面账本:页面改动的统一卸载契约 ────────────────────────────────
258
+ * 为什么要有它:同样是「把东西挪个位置」,caption-map 取消插件当场复原,chat-dock
259
+ * 取消后界面纹丝不动、只能刷新 —— 差别不在宿主(两边都是 live applied,`plugin_manager`
260
+ * 对两边都回 `application: "applied"`),在插件有没有一条对称的拆卸路径。手写拆卸一定
261
+ * 会漏(chat-dock 漏过 <style>、侧栏列 position、三个 window 全局),而「登记」是装填
262
+ * 动作的副产物:写了 page.adopt 就不可能忘记搬回。
263
+ *
264
+ * 契约(README「页面改动契约」):
265
+ * 1. 凡是改页面的动作都走账本:own / adopt / put / attr / style / on / global / timer / wrap;
266
+ * 2. apply 用 WINDOW_KIT.effect(ctx, label, mount) 包起来,卸载时账本**倒序**回滚;
267
+ * 3. 记录顺序 = 因果顺序(先建容器、再往里搬),回滚自然先搬出、再删容器;
268
+ * 4. undo 幂等、单步失败不挡其余;seal 之后异步回调再记账静默落空。
269
+ */
270
+
271
+ /* 宿主窗口:装载器可能把本模块裹在一个壳作用域里,那里的 `window` 不是页面宿主
272
+ * (DSH 的模块壳、测试里 `new Function('window', src)` 都是这样)。真宿主优先从
273
+ * globalThis 取,退化到自带 document 的那个 `window`。 */
274
+ function hostWindow() {
275
+ try {
276
+ if (typeof globalThis !== 'undefined' && globalThis.window) return globalThis.window
277
+ } catch (error) { /* 忽略 */ }
278
+ try {
279
+ if (typeof window !== 'undefined' && window && window.document) return window
280
+ } catch (error) { /* 忽略 */ }
281
+ return null
282
+ }
283
+
284
+ /* 宿主文档:优先宿主窗口的 document,退化到全局 document。 */
285
+ function hostDocument() {
286
+ var win = hostWindow()
287
+ if (win && win.document) return win.document
288
+ try {
289
+ if (typeof document !== 'undefined' && document) return document
290
+ } catch (error) { /* 忽略 */ }
291
+ return null
292
+ }
293
+
294
+ /* 把节点搬回账本记下的原位;原位没了(宿主重建过)就留在原地,别硬塞。 */
295
+ function restoreNode(node, home) {
296
+ if (!node || !home || !home.parent) return
297
+ try {
298
+ if (home.next && typeof home.parent.contains === 'function' && home.parent.contains(home.next)) {
299
+ home.parent.insertBefore(node, home.next)
300
+ } else {
301
+ home.parent.appendChild(node)
302
+ }
303
+ } catch (error) { /* 原位不可用:比抛错好 */ }
304
+ }
305
+
306
+ function pageLedger() {
307
+ var steps = []
308
+ var alive = true
309
+
310
+ function record(undo) {
311
+ if (alive) steps.push(undo)
312
+ }
313
+
314
+ var book = {
315
+ /* 账还开着吗:异步回调(定时器/观察器)先问一句再动页面。 */
316
+ alive: function () { return alive },
317
+ size: function () { return steps.length },
318
+ /* 结账:之后登记的步骤直接丢弃(卸载已经发生,晚到的回调不该再改页面)。 */
319
+ seal: function () { alive = false },
320
+ /* 倒序回滚。幂等:第二次调用拿不到步骤,什么都不做。 */
321
+ undo: function () {
322
+ alive = false
323
+ while (steps.length) {
324
+ var undo = steps.pop()
325
+ try { undo() } catch (error) { /* 单步失败不挡其余回滚 */ }
326
+ }
327
+ },
328
+
329
+ /* 自造节点:卸载时摘掉(连同子树)。 */
330
+ own: function (node) {
331
+ if (!node) return node
332
+ record(function () {
333
+ try { if (node.parentElement) node.parentElement.removeChild(node) } catch (error) { /* 已被别人摘走 */ }
334
+ })
335
+ return node
336
+ },
337
+
338
+ /* 搬宿主已有的节点:第一次搬时记原位,卸载搬回。和 chat-dock 的
339
+ * `__capyclawDockHome` 是同一个 expando,两边互相认得。 */
340
+ adopt: function (node, parent) {
341
+ if (!node || !parent) return null
342
+ if (!node.__capyclawHome) {
343
+ node.__capyclawHome = { parent: node.parentElement, next: node.nextSibling }
344
+ record(function () {
345
+ var home = node.__capyclawHome
346
+ node.__capyclawHome = null
347
+ restoreNode(node, home)
348
+ })
349
+ }
350
+ if (node.parentElement !== parent) {
351
+ try { parent.appendChild(node) } catch (error) { return null }
352
+ }
353
+ return node
354
+ },
355
+
356
+ /* 改别人的内联样式:第一次碰这个元素时存一份 cssText,卸载整块还原
357
+ * (比逐条 removeProperty 稳:宿主自己写过的属性也一并回到原样)。 */
358
+ put: function (el, prop, value, priority) {
359
+ if (!el || !el.style || typeof el.style.setProperty !== 'function') return el
360
+ if (el.__capyclawCss === undefined) {
361
+ el.__capyclawCss = el.style.cssText || ''
362
+ record(function () {
363
+ try { el.style.cssText = el.__capyclawCss } catch (error) { /* 忽略 */ }
364
+ try { delete el.__capyclawCss } catch (error) { /* 忽略 */ }
365
+ })
366
+ }
367
+ try { el.style.setProperty(prop, value, priority || 'important') } catch (error) { /* 忽略 */ }
368
+ return el
369
+ },
370
+
371
+ /* 改别人的属性:第一次改某个名字时记「有没有 / 原值」。 */
372
+ attr: function (el, name, value) {
373
+ if (!el || typeof el.setAttribute !== 'function') return el
374
+ if (!el.__capyclawAttrs) el.__capyclawAttrs = Object.create(null)
375
+ if (!(name in el.__capyclawAttrs)) {
376
+ el.__capyclawAttrs[name] = {
377
+ had: typeof el.hasAttribute === 'function' ? el.hasAttribute(name) : true,
378
+ old: typeof el.getAttribute === 'function' ? el.getAttribute(name) : null,
379
+ }
380
+ record(function () {
381
+ var was = el.__capyclawAttrs && el.__capyclawAttrs[name]
382
+ try {
383
+ if (!was || !was.had) el.removeAttribute(name)
384
+ else el.setAttribute(name, was.old)
385
+ } catch (error) { /* 忽略 */ }
386
+ try { delete el.__capyclawAttrs[name] } catch (error) { /* 忽略 */ }
387
+ })
388
+ }
389
+ try { el.setAttribute(name, value) } catch (error) { /* 忽略 */ }
390
+ return el
391
+ },
392
+
393
+ /* 注入样式表:卸载摘掉自己那一条(同名但已被新实例换过的,不摘,免得害了它)。 */
394
+ style: function (id, css) {
395
+ var doc = hostDocument()
396
+ if (!doc || !doc.head || typeof doc.createElement !== 'function') return null
397
+ var el = null
398
+ try { el = typeof doc.getElementById === 'function' ? doc.getElementById(id) : null } catch (error) { el = null }
399
+ if (el && el.parentElement === doc.head) {
400
+ if (el.textContent !== css) { try { el.textContent = css } catch (error) { /* 忽略 */ } }
401
+ } else {
402
+ el = doc.createElement('style')
403
+ try { el.id = id } catch (error) { /* 忽略 */ }
404
+ el.textContent = css || ''
405
+ try { doc.head.appendChild(el) } catch (error) { return null }
406
+ }
407
+ var mine = el
408
+ record(function () {
409
+ try { if (mine.parentElement === doc.head) doc.head.removeChild(mine) } catch (error) { /* 忽略 */ }
410
+ })
411
+ return el
412
+ },
413
+
414
+ /* 监听:卸载摘掉。 */
415
+ on: function (target, type, fn, options) {
416
+ if (!target || typeof target.addEventListener !== 'function') return target
417
+ try { target.addEventListener(type, fn, options) } catch (error) { return target }
418
+ record(function () { try { target.removeEventListener(type, fn, options) } catch (error) { /* 忽略 */ } })
419
+ return target
420
+ },
421
+
422
+ /* window 上的全局名:卸载还原/删除,且只回滚自己那份(别人后来覆盖过就不动)。 */
423
+ global: function (key, value) {
424
+ var host = hostWindow()
425
+ if (!host || !key) return value
426
+ var had = key in host
427
+ var old = host[key]
428
+ try { host[key] = value } catch (error) { return value }
429
+ record(function () {
430
+ try {
431
+ if (host[key] !== value) return
432
+ if (had) host[key] = old
433
+ else delete host[key]
434
+ } catch (error) { /* 忽略 */ }
435
+ })
436
+ return value
437
+ },
438
+
439
+ /* 定时器:卸载取消;回调晚到(账已结)就静默。 */
440
+ timer: function (fn, ms) {
441
+ var host = hostWindow()
442
+ if (!host || typeof host.setTimeout !== 'function') return 0
443
+ var id = host.setTimeout(function () {
444
+ id = 0
445
+ if (alive) { try { fn() } catch (error) { /* 忽略 */ } }
446
+ }, ms)
447
+ record(function () { if (id) { try { host.clearTimeout(id) } catch (error) { /* 忽略 */ } id = 0 } })
448
+ return id
449
+ },
450
+
451
+ /* 包别人的方法(React 单所有权的容错补丁):卸载还原,且原来没有就删掉。 */
452
+ wrap: function (target, key, make) {
453
+ if (!target || !key) return null
454
+ var had = Object.prototype.hasOwnProperty.call(target, key)
455
+ var old = target[key]
456
+ try { target[key] = make(old) } catch (error) { return null }
457
+ record(function () {
458
+ try {
459
+ if (had) target[key] = old
460
+ else delete target[key]
461
+ } catch (error) { /* 忽略 */ }
462
+ })
463
+ return old
464
+ },
465
+ }
466
+ book.dispose = book.undo
467
+ return book
468
+ }
469
+
470
+ /*
471
+ * 页面改动的标准入口:装填包进 cordis effect,卸载 = 自定义拆卸 + 账本倒序回滚。
472
+ *
473
+ * `mount(page)` 里做全部装填;返回值(函数)会在账本回滚**之前**执行 —— 需要按语义
474
+ * 收尾(把面板搬回原父、放开侧栏预留)就放这里,剩下的零碎(style/全局/监听/定时器)
475
+ * 交给账本。没有 ctx.effect 的环境(单测桩)退化成「装填后返回账本」,测试可以手动 undo。
476
+ */
477
+ function pageEffect(ctx, label, mount) {
478
+ var book = pageLedger()
479
+ var rollback = book.undo
480
+ var extra = null
481
+ function run() {
482
+ try { extra = mount(book) } catch (error) {
483
+ /* 装填失败只该让这块界面缺席,不该把整个 web boot 带崩。 */
484
+ extra = null
485
+ console.warn('[' + (ctx && ctx.name ? ctx.name : 'page') + '] 装填失败:', error && error.message)
486
+ }
487
+ return function () {
488
+ var mine = extra
489
+ extra = null
490
+ try { if (typeof mine === 'function') mine() } catch (error) { /* 自定义拆卸失败仍然回滚账本 */ }
491
+ rollback()
492
+ }
493
+ }
494
+ if (!ctx || typeof ctx.effect !== 'function') {
495
+ /* 桩环境(单测)没有 cordis effect:装填当场跑,句柄的 undo = 自定义收尾 + 回滚。 */
496
+ var teardown = run()
497
+ book.undo = book.dispose = function () {
498
+ try { teardown() } catch (error) { /* 忽略 */ }
499
+ }
500
+ return book
501
+ }
502
+ try { ctx.effect(run, label) } catch (error) {
503
+ console.warn('[' + (ctx.name || 'page') + '] effect 注册失败:', error && error.message)
504
+ run()
505
+ }
506
+ return book
507
+ }
508
+
509
+ WINDOW_KIT.page = pageLedger
510
+ WINDOW_KIT.effect = pageEffect
511
+
512
+ /*
513
+ * 浮层按钮(chat-dock 收起后的召回按钮)的层号:唯一真源在这里由 zBase 派生,
514
+ * 消费者只读 Z.overlayButton,不再各自硬编码 `zBase + 400`。改 zBase 时它自动跟随。
515
+ */
516
+ WINDOW_KIT.Z.overlayButton = WINDOW_KIT.zBase + 400
517
+
518
+ /*
519
+ * 它同时是 boot graph 里的一条条目:模块系统给每个条目建 cordis fiber 并激活,
520
+ * fiber 拿 factory 的返回值当插件。纯几何对象没有 apply 会停在「未激活」,
521
+ * 所以补 name/inject/空操作 apply 让它作为合法插件激活。require 拿到的仍是这份
522
+ * 几何对象(多出的三个键不影响任何消费方,几何算式一个字节都没动)。
523
+ */
524
+ WINDOW_KIT.name = '@capyclaw/window-kit'
525
+ WINDOW_KIT.inject = []
526
+ WINDOW_KIT.apply = function () {}
527
+
528
+ module.exports = WINDOW_KIT
529
+ return module.exports
530
+ }
531
+ })
@@ -0,0 +1,9 @@
1
+ # dsh-window-kit bundle patch:把这份纯客户端底座挂进 Harness 的模块图。
2
+ #
3
+ # 宿主半(index.js)是空操作 —— 本包没有路由、没有模型面,只提供一个浏览器模块。
4
+ # 浏览器半(client/client.js)注册 id 为 @capyclaw/window-kit 的 factory,返回 WINDOW_KIT。
5
+ # portal-hud / chat-dock 在各自 dsh.client.external 里声明这个 id,factory 里 require 它;
6
+ # 模块系统保证 provider 先于 consumer 装载,且记忆化 ⇒ 两消费者拿到同一份(单一真源)。
7
+ - insert:
8
+ - id: window-kit
9
+ name: "@capyclaw/window-kit"
@@ -0,0 +1,8 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="窗口几何底座">
2
+ <rect x="4" y="4" width="56" height="56" rx="16" fill="#1f6feb"/>
3
+ <rect x="16" y="18" width="32" height="24" rx="5" fill="none" stroke="#ffffff" stroke-width="3.2"/>
4
+ <path d="M16 26h32" stroke="#ffffff" stroke-width="3.2" stroke-linecap="round"/>
5
+ <circle cx="22" cy="22" r="1.9" fill="#ffffff"/>
6
+ <circle cx="28" cy="22" r="1.9" fill="#ffffff"/>
7
+ <path d="M26 48h12" stroke="#ffffff" stroke-width="3.2" stroke-linecap="round"/>
8
+ </svg>
@@ -0,0 +1,11 @@
1
+ /*
2
+ * @capyclaw/window-kit 宿主半:空操作。
3
+ *
4
+ * 本包是纯客户端模块底座(几何算式 + dsh DOM 探针),没有 HTTP 路由、没有模型面。
5
+ * 宿主半存在的唯一理由是让 cordis 有一个 Loader entry,从而把浏览器半
6
+ * (client/client.js,id '@capyclaw/window-kit')送进模块图,供 portal-hud / chat-dock
7
+ * 通过 dsh.client.external + require 取用。真正的实现全在浏览器半。
8
+ */
9
+ export const name = 'window-kit'
10
+ export const inject = []
11
+ export function apply() {}
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@capyclaw/window-kit",
3
+ "version": "0.1.0",
4
+ "description": "窗口几何与 dsh DOM 探针的唯一底座:fitRect 夹算式、frameLayout、mainPanel/conversationRoot 探针、Z 层序、mountOverlay 浮层样板。纯客户端模块包,@capyclaw/portal-hud 与 @capyclaw/chat-dock 都 require 它(彼此不依赖),单一真源、零重复实现。",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "exports": {
8
+ ".": "./index.js",
9
+ "./client": "./client/client.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "dsh": {
13
+ "bundle": {
14
+ "patch": "./cordis.patch.yml"
15
+ },
16
+ "client": {
17
+ "platform": "web",
18
+ "immediately": true
19
+ },
20
+ "category": "ui",
21
+ "platform": "web"
22
+ },
23
+ "meta": {
24
+ "title": "窗口底座",
25
+ "description": "窗口几何与 dsh DOM 探针的唯一底座:一份 fitRect 夹算式、frameLayout、mainPanel/conversationRoot 探针、Z 层序与 mountOverlay 浮层样板。主区页面包(portal-hud)与会话副窗包(chat-dock)都 require 这同一份,彼此不依赖——单一真源,不再有「谁先装载、探不到全局就罢工」的跨包约定。"
26
+ },
27
+ "files": [
28
+ "index.js",
29
+ "client/",
30
+ "cordis.patch.yml",
31
+ "README.md",
32
+ "icon.svg"
33
+ ],
34
+ "scripts": {
35
+ "test": "node --test \"test/*.test.mjs\""
36
+ },
37
+ "dependencies": {
38
+ "@capyclaw/foundation": "^0.1.1"
39
+ },
40
+ "engines": {
41
+ "dsh": "^0.2.0-rc.2",
42
+ "node": ">=18"
43
+ },
44
+ "author": {
45
+ "name": "CapyClaw",
46
+ "url": "https://github.com/capyclaw",
47
+ "email": "support@capyclaw.dev"
48
+ },
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "git+https://github.com/Maple496/capyclaw.git",
52
+ "directory": "C/packages/capyclaw-window-kit"
53
+ },
54
+ "bugs": {
55
+ "url": "https://github.com/Maple496/capyclaw/issues"
56
+ },
57
+ "homepage": "https://github.com/capyclaw/dsh-plugins",
58
+ "license": "MIT",
59
+ "publishConfig": {
60
+ "access": "public"
61
+ }
62
+ }
package/options.mjs ADDED
@@ -0,0 +1,27 @@
1
+ /**
2
+ * @capyclaw/caption-map · 配置收敛(宿主半共享)
3
+ *
4
+ * cordis.patch.yml 的 config 经 normalizeConfig 收敛后,由 clientPanel 的
5
+ * bootGlobals 注入成页面开机全局 __DSH_CAPTION_MAP__,浏览器半据此开关各设定。
6
+ * bootGlobals 每次 index 渲染重放:改 config 后刷新页面生效。
7
+ */
8
+
9
+ /** 页面开机全局键名(浏览器半读 window.__DSH_CAPTION_MAP__)。 */
10
+ export const BOOT_GLOBAL = '__DSH_CAPTION_MAP__'
11
+
12
+ /** 设定默认值:侧栏宽度固定为外壳拖拽上限(columns.ts SIDEBAR_MAX = 420)。 */
13
+ export const DEFAULTS = {
14
+ fixedSidebarWidth: true,
15
+ }
16
+
17
+ /**
18
+ * 收敛插件 config:只认布尔开关,缺省一律默认值。
19
+ * @param {unknown} config cordis 行 config(可能缺省/形状不对)
20
+ * @returns {{fixedSidebarWidth: boolean}}
21
+ */
22
+ export function normalizeConfig(config) {
23
+ const c = config && typeof config === 'object' ? config : {}
24
+ return {
25
+ fixedSidebarWidth: c.fixedSidebarWidth !== false,
26
+ }
27
+ }