@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.
- package/README.md +321 -2
- package/client/client.chat.js +441 -0
- package/client/client.js +1519 -0
- package/cordis.patch.yml +14 -0
- package/index.js +47 -0
- package/node_modules/@capyclaw/foundation/README.md +51 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +86 -0
- package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
- package/node_modules/@capyclaw/foundation/abi/index.mjs +35 -0
- package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
- package/node_modules/@capyclaw/foundation/abi/loader.mjs +352 -0
- package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
- package/node_modules/@capyclaw/foundation/abi/spec.mjs +258 -0
- package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
- package/node_modules/@capyclaw/foundation/abi/version.mjs +140 -0
- package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
- package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +55 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
- package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
- package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +34 -0
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +85 -0
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +187 -0
- package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +185 -0
- package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +198 -0
- package/node_modules/@capyclaw/foundation/package.json +53 -0
- package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
- package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
- package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
- package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
- package/node_modules/@capyclaw/plugin-kit/package.json +69 -0
- package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
- package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +129 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +60 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
- package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
- package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
- package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
- package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +656 -0
- package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
- package/node_modules/@capyclaw/window-kit/README.md +91 -0
- package/node_modules/@capyclaw/window-kit/client/client.js +531 -0
- package/node_modules/@capyclaw/window-kit/cordis.patch.yml +9 -0
- package/node_modules/@capyclaw/window-kit/icon.svg +8 -0
- package/node_modules/@capyclaw/window-kit/index.js +11 -0
- package/node_modules/@capyclaw/window-kit/package.json +62 -0
- package/options.mjs +27 -0
- package/package.json +67 -4
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @capyclaw/caption-map / client.chat.js —— 聊天窗停靠(纯前端搬迁)。
|
|
3
|
+
*
|
|
4
|
+
* 只做一件事:把会话面板(`[data-conversation-content]` 的父节点 —— 「标题 + 页签 +
|
|
5
|
+
* 正文 + 输入框」那一整块)搬进侧栏列底部的一个槽里。宽度跟着侧栏列走,高度占一截,
|
|
6
|
+
* 正文照旧由 dsh 自己渲染、自己滚动,输入框照旧在面板底部。
|
|
7
|
+
*
|
|
8
|
+
* 和老 @capyclaw/chat-dock 的区别:没有浮窗外壳、没有「钉住 / 收起 / 召回按钮」、
|
|
9
|
+
* 没有主区舞台协议(capyclaw:stage)、没有 localStorage 几何账本、没有拖动。
|
|
10
|
+
* 那套是为「能拖、能收、跟着 portal-hud 换页重钉」准备的;现在只要「搬过去、
|
|
11
|
+
* 一直待着」,于是整份实现就只剩:找列 → 建槽 → 搬 → 盯住 React 换节点 → 拆。
|
|
12
|
+
*
|
|
13
|
+
* 但「盯住 React 换节点」正是 chat-dock 交过学费的地方,稳定性机制必须原样继承
|
|
14
|
+
* (回归用例见 test/chat-slot.test.mjs):
|
|
15
|
+
* 1. 容错补丁打 swallowed 标记:React 来摘已被搬走的孩子时不抛错,并记下
|
|
16
|
+
* `__capyclawChatSwallowed` —— 这是「React 已经卸载它」的凭证;
|
|
17
|
+
* 2. 僵尸只摘不复活:被卸载的旧面板绝不能搬回原父 —— 搬回去就复活在主区,
|
|
18
|
+
* 比槽里的新面板大,conversationRoot(largestVisible)下一拍又选它 →
|
|
19
|
+
* 新旧轮流重钉的死循环(chat-dock 用户报过的「点其他会话卡死」);
|
|
20
|
+
* 3. 两拍断环 prevPanel:上一拍刚换下的节点这一拍又被选中(过渡态)先不换回;
|
|
21
|
+
* 记忆只由真正的重钉覆盖或断环分支消费,自身确认拍不清(自己搬节点也触发
|
|
22
|
+
* observer,清了断环就失效);
|
|
23
|
+
* 4. 同层守卫:侧栏列被整体卸掉(面板连槽脱离文档)时先搬回原父保住会话节点;
|
|
24
|
+
* 5. healStale:装填时收拾上一版留下的钉子(热更只换脚本没跑拆卸的情形);
|
|
25
|
+
* 6. 监听面不只 childList:还有 style/class(侧栏折叠展开是 class 变化、不增减
|
|
26
|
+
* 节点),documentElement 的 drawer/collapse 属性在 body 子树外单独盯;
|
|
27
|
+
* 7. 开局重试 40×250ms:apply 时侧栏/会话可能还没渲染出来,observer 是主路径,
|
|
28
|
+
* 这是保险;
|
|
29
|
+
* 8. 同步搬迁 + 一批一次:重钉在 observer 回调里当场做(微任务、先于绘制),防抖
|
|
30
|
+
* 只留给「这一拍没钉成」的补试——防抖后再搬 = 切会话时主区先闪一下聊天窗;
|
|
31
|
+
* 同批多次回调用 queueMicrotask 合并成一次 place,免得断环刚清记忆就被第二次
|
|
32
|
+
* place 换回去;
|
|
33
|
+
* 9. 座位定位一个字不改:现版座位是滚动容器里的 sticky bottom:0,覆盖成
|
|
34
|
+
* relative 会把输入框打进消息流、跟着滚动(chat-dock 的旧规则是给旧版
|
|
35
|
+
* fixed 底座写的,不能照抄)。
|
|
36
|
+
*
|
|
37
|
+
* 装载协议:包内分块。`__ModuleLoader__.load({ id, chunk: 'client.chat.js', factory })`,
|
|
38
|
+
* 由同包的 `client/client.js` 在 apply 里 `require.async('./client.chat.js')` 取 install()。
|
|
39
|
+
* 注意:分块注册也必须带 id(包名)——加载器用 `chunkId(registration.id, chunk)` 当
|
|
40
|
+
* 模块键,缺了 id 就注册到 "undefined/client.chat.js",importChunk 等不到而报
|
|
41
|
+
* 「loaded without registering」。
|
|
42
|
+
* 页面改动全部走 @capyclaw/window-kit 的账本(`WINDOW_KIT.effect`),停用即还原。
|
|
43
|
+
*/
|
|
44
|
+
window.__ModuleLoader__.load({
|
|
45
|
+
id: '@capyclaw/caption-map',
|
|
46
|
+
chunk: 'client.chat.js',
|
|
47
|
+
factory: function (require) {
|
|
48
|
+
var module = { exports: {} }
|
|
49
|
+
var exports = module.exports
|
|
50
|
+
|
|
51
|
+
/* 底座依赖:几何探针 + 页面账本都在 @capyclaw/window-kit。它没启用时本分块缺席,
|
|
52
|
+
* 留一条明确告警,不牵连 caption-map 自己的标题栏映射。 */
|
|
53
|
+
var WINDOW_KIT = null
|
|
54
|
+
try { WINDOW_KIT = require('@capyclaw/window-kit') } catch (error) {
|
|
55
|
+
console.warn('[caption-map] 聊天窗停靠缺少 @capyclaw/window-kit,这一半缺席:请在插件列表里启用它。', error && error.message)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
var SLOT_ATTR = 'data-capyclaw-chat-slot'
|
|
59
|
+
var PANEL_ATTR = 'data-capyclaw-chat-panel'
|
|
60
|
+
var STYLE_ID = 'capyclaw-chat-slot-style'
|
|
61
|
+
/** 槽高:跟着侧栏列高走,两头夹住;短窗口不至于把会话列表挤没。
|
|
62
|
+
* 标题栏形态下侧栏已被区域映射搬空,另有「占满」覆盖规则把槽放成 flex:1。 */
|
|
63
|
+
var SLOT_HEIGHT = 'clamp(220px, 42%, 520px)'
|
|
64
|
+
/* 占满门控:仅 Windows 标题栏 + 菜单栏在场(与区域映射 active() 同前提)、
|
|
65
|
+
* 非 drawer、非折叠——此时侧栏的 logoRow / 新会话行 / 滚动区内容都已被区域映射
|
|
66
|
+
* fixed 搬走或收掉,列里只剩空占位;纯 web / 折叠 / drawer 下原生内容还在流里,
|
|
67
|
+
* 这套覆盖必须缺席。
|
|
68
|
+
* 关键层级:新壳在列与根之间包了一层 display:contents 的插槽 host
|
|
69
|
+
* (div[data-slot="sidebar"]),真正装槽、带 logoRow/newSessionRow/scrollRegion
|
|
70
|
+
* 子级、有内边距的是 host 里那层 occupant(SidebarRoot 的 .root,即
|
|
71
|
+
* window-kit.sidebarPanel().el,槽就插在它的 footArea 之前)。所以门控主体必须是
|
|
72
|
+
* 「host 的后代里、直接装着槽的那层」——`[data-slot="sidebar"] :has(> 槽)`。
|
|
73
|
+
* 不能写成 host 自己 `:has(> 槽)`:槽是 host 的孙级,那样永不命中,占满整套失效
|
|
74
|
+
* (用户报「占满没生效」的根因);且 padding/子级收零都得落在 occupant 上,
|
|
75
|
+
* 落在 display:contents 的 host 上是无效的。 */
|
|
76
|
+
var FILL_SEL = 'html[data-windows-titlebar]:has([data-windows-menu]):not([data-sidebar-drawer]):not(:has([data-sidebar-collapsed="true"])) [data-slot="sidebar"] :has(> [' + SLOT_ATTR + '])'
|
|
77
|
+
|
|
78
|
+
/* 只写「搬过去之后怎么摆」,不碰颜色、不碰主题:面板自己带着当前主题的底色,
|
|
79
|
+
* 写死色值会在浅色主题下变成黑底黑字(chat-dock 踩过)。 */
|
|
80
|
+
var CSS = [
|
|
81
|
+
'[' + SLOT_ATTR + ']{',
|
|
82
|
+
'flex:0 0 auto !important;',
|
|
83
|
+
'height:' + SLOT_HEIGHT + ' !important;',
|
|
84
|
+
'min-height:0 !important;',
|
|
85
|
+
'display:flex !important;',
|
|
86
|
+
'flex-direction:column !important;',
|
|
87
|
+
'overflow:hidden !important;',
|
|
88
|
+
'border-top:1px solid var(--dsw-alias-border-l1,rgba(127,127,127,.18)) !important;',
|
|
89
|
+
'}',
|
|
90
|
+
/* 面板本体:铺满槽;内部保持「正文 flex:1 + 输入框贴底」的列布局。 */
|
|
91
|
+
'[' + PANEL_ATTR + ']{',
|
|
92
|
+
'flex:1 1 auto !important;',
|
|
93
|
+
'min-height:0 !important;',
|
|
94
|
+
'width:100% !important;',
|
|
95
|
+
'height:100% !important;',
|
|
96
|
+
'display:flex !important;',
|
|
97
|
+
'flex-direction:column !important;',
|
|
98
|
+
'overflow:hidden !important;',
|
|
99
|
+
'}',
|
|
100
|
+
/* 拖窗口用的标题条在这扇内嵌面板里没有意义(也没有窗口可拖)。 */
|
|
101
|
+
'[' + PANEL_ATTR + '] > header,',
|
|
102
|
+
'[' + PANEL_ATTR + '] header[data-window-drag]{display:none !important;}',
|
|
103
|
+
/* 座位([data-composer-seat])的定位**一个字都不改**:现版 dsh 的座位是
|
|
104
|
+
* scrollBody 里的 `position:sticky; bottom:0; margin-top:auto`
|
|
105
|
+
* (ConversationRoot.module.css 的 active 相规则)——滚动时钉在滚动容器底边,
|
|
106
|
+
* 这正是主区里「输入框不随消息滚」的原生机制。搬进侧栏后同一套结构照旧成立。
|
|
107
|
+
* 教训:chat-dock 那套「座位回文档流 position:relative」是给旧版 fixed 底座写的,
|
|
108
|
+
* 照抄到现版会把 sticky 打掉,座位掉进消息流跟着滚(用户报过)。 */
|
|
109
|
+
/* 输入框底下那排统计条(轮数 / tok/s / 上下文 %)在窄侧栏里是噪音,藏掉;
|
|
110
|
+
* 主区那份不受影响(规则只挂在停靠标记上)。 */
|
|
111
|
+
'[' + PANEL_ATTR + '] [data-composer-dock]{display:none !important;}',
|
|
112
|
+
/* 宿主给输入框卡片留的居中边距 / 最大宽度在窄侧栏里就是两条空带,铺满。 */
|
|
113
|
+
'[' + PANEL_ATTR + '] [data-composer-card]{',
|
|
114
|
+
'max-width:none !important;',
|
|
115
|
+
'margin-left:0 !important;',
|
|
116
|
+
'margin-right:0 !important;',
|
|
117
|
+
'}',
|
|
118
|
+
/* 占满侧栏:侧栏搬空后聊天窗吃掉整列高度(用户要求「占满全部空间」)。
|
|
119
|
+
* 槽改 flex:1,空占位(logoRow 40px 条 / 新会话行 margin / 滚动区 flex:1)
|
|
120
|
+
* 收零,root 内边距归零做全出血;fixed 搬走的控件不受父级 height:0 影响。 */
|
|
121
|
+
FILL_SEL + '{padding:0 !important;}',
|
|
122
|
+
FILL_SEL + ' > [' + SLOT_ATTR + ']{flex:1 1 auto !important;height:auto !important;}',
|
|
123
|
+
FILL_SEL + ' > [class*="logoRow"]{flex:0 0 auto !important;height:0 !important;min-height:0 !important;padding:0 !important;margin:0 !important;}',
|
|
124
|
+
FILL_SEL + ' > [class*="newSessionRow"]{flex:0 0 auto !important;height:0 !important;min-height:0 !important;margin:0 !important;}',
|
|
125
|
+
FILL_SEL + ' > [class*="scrollRegion"]{flex:0 0 auto !important;height:0 !important;min-height:0 !important;margin:0 !important;overflow:hidden !important;}',
|
|
126
|
+
].join('')
|
|
127
|
+
|
|
128
|
+
/** 替换父节点的三个「摘孩子」方法:孩子被搬走后 React 再来摘,不抛错,
|
|
129
|
+
* 并给被吞掉的孩子打 `__capyclawChatSwallowed` 标记 —— 之后换节点时靠它
|
|
130
|
+
* 区分「活节点(搬回原父)」和「僵尸(只摘不复活)」。 */
|
|
131
|
+
function patchParent(node) {
|
|
132
|
+
if (!node || node.__capyclawChatOrig) return
|
|
133
|
+
var orig = {
|
|
134
|
+
removeChild: node.removeChild,
|
|
135
|
+
insertBefore: node.insertBefore,
|
|
136
|
+
replaceChild: node.replaceChild,
|
|
137
|
+
}
|
|
138
|
+
node.__capyclawChatOrig = orig
|
|
139
|
+
node.removeChild = function (child) {
|
|
140
|
+
if (child && child.parentElement === node) return orig.removeChild.call(node, child)
|
|
141
|
+
try { if (child) child.__capyclawChatSwallowed = true } catch (error) { /* 忽略 */ }
|
|
142
|
+
return child
|
|
143
|
+
}
|
|
144
|
+
node.insertBefore = function (child, ref) {
|
|
145
|
+
if (ref && ref.parentElement !== node) return node.appendChild(child)
|
|
146
|
+
return orig.insertBefore.call(node, child, ref)
|
|
147
|
+
}
|
|
148
|
+
node.replaceChild = function (child, old) {
|
|
149
|
+
if (old && old.parentElement !== node) {
|
|
150
|
+
try { old.__capyclawChatSwallowed = true } catch (error) { /* 忽略 */ }
|
|
151
|
+
return node.appendChild(child)
|
|
152
|
+
}
|
|
153
|
+
return orig.replaceChild.call(node, child, old)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function unpatchParent(node) {
|
|
158
|
+
if (!node || !node.__capyclawChatOrig) return
|
|
159
|
+
var orig = node.__capyclawChatOrig
|
|
160
|
+
node.removeChild = orig.removeChild
|
|
161
|
+
node.insertBefore = orig.insertBefore
|
|
162
|
+
node.replaceChild = orig.replaceChild
|
|
163
|
+
try { delete node.__capyclawChatOrig } catch (error) { node.__capyclawChatOrig = null }
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** 面板里那块真正打字的 contenteditable(Lexical 的 root 载体)。 */
|
|
167
|
+
function composerInputOf(panel) {
|
|
168
|
+
if (!panel || typeof panel.querySelector !== 'function') return null
|
|
169
|
+
try { return panel.querySelector('[data-composer-input]') } catch (error) { return null }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/*
|
|
173
|
+
* 选面板时优先「输入框还活着」的那份。
|
|
174
|
+
*
|
|
175
|
+
* 为什么需要它:输入框是 Lexical 编辑器,editor 由会话 shell 常驻持有,
|
|
176
|
+
* ComposerContentEditable 挂载时 `editor.setRootElement(el)`、卸载时置 null。
|
|
177
|
+
* 把面板搬进侧栏后,React 某些重渲染/切会话会卸载旧 composer(其 cleanup 把
|
|
178
|
+
* editor 的 root 置 null 或绑去新节点),而旧面板的 DOM 被容错补丁留在槽里成了
|
|
179
|
+
* 「僵尸」——看得见、却不再被 Lexical 驱动:打字不更新 draft(placeholder 常驻)、
|
|
180
|
+
* paste 不被拦截(图片按浏览器默认原样塞满框)、发送钮因 draft 为空而禁用。
|
|
181
|
+
* largestVisible 只量面积,僵尸往往正是槽里最大的那份,于是 place() 的
|
|
182
|
+
* `chat === panel` 分支一直钉着它不换。这里用「contenteditable 上是否挂着
|
|
183
|
+
* __lexicalEditor」当活/死凭证:chat 失绑时,在同标记候选里找已绑定且可见的
|
|
184
|
+
* 最大者替代,保证钉住的永远是活面板。找不到(开局 composer 还没挂载)才退回 chat。
|
|
185
|
+
*/
|
|
186
|
+
function preferLivePanel(chat) {
|
|
187
|
+
if (!chat) return chat
|
|
188
|
+
var input = composerInputOf(chat)
|
|
189
|
+
if (input && input.__lexicalEditor) return chat
|
|
190
|
+
if (typeof document.querySelectorAll !== 'function') return chat
|
|
191
|
+
var bodies = []
|
|
192
|
+
try { bodies = Array.prototype.slice.call(document.querySelectorAll('[data-conversation-content]')) } catch (error) { return chat }
|
|
193
|
+
var best = null
|
|
194
|
+
for (var i = 0; i < bodies.length; i += 1) {
|
|
195
|
+
var p = bodies[i].parentElement || bodies[i]
|
|
196
|
+
if (p === chat || p.__capyclawChatSwallowed) continue
|
|
197
|
+
var inp = composerInputOf(p)
|
|
198
|
+
if (!inp || !inp.__lexicalEditor) continue
|
|
199
|
+
var box = null
|
|
200
|
+
try { box = p.getBoundingClientRect() } catch (error) { box = null }
|
|
201
|
+
if (!box || !isFinite(box.width) || !isFinite(box.height) || box.width < 120 || box.height < 80) continue
|
|
202
|
+
var area = box.width * box.height
|
|
203
|
+
if (!best || area > best.area) best = { node: p, area: area }
|
|
204
|
+
}
|
|
205
|
+
return best ? best.node : chat
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** 把聊天面板搬进侧栏底部的唯一实现;返回卸载函数。 */
|
|
209
|
+
function mount(page) {
|
|
210
|
+
page.style(STYLE_ID, CSS)
|
|
211
|
+
|
|
212
|
+
var slot = null
|
|
213
|
+
var panel = null
|
|
214
|
+
/* 两拍断环:上一拍刚换下的节点。这一拍它又被选中(僵尸/过渡态)先不换回,
|
|
215
|
+
* 免得新旧来回重钉不收敛;同节点稳定待满一拍就清掉。 */
|
|
216
|
+
var prevPanel = null
|
|
217
|
+
var timer = 0
|
|
218
|
+
var bootTimer = 0
|
|
219
|
+
var bootTries = 0
|
|
220
|
+
|
|
221
|
+
function sidebarBox() {
|
|
222
|
+
try { return WINDOW_KIT.sidebarPanel() } catch (error) { return null }
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** 在侧栏列里保证有一个槽(插在底栏之前;底栏不在时追加到列尾)。
|
|
226
|
+
* 列里已有槽(上一版留下的)就复用,不叠第二个。 */
|
|
227
|
+
function ensureSlot(box) {
|
|
228
|
+
if (!box || !box.el || box.el.isConnected === false) return false
|
|
229
|
+
var col = box.el
|
|
230
|
+
if (slot && slot.isConnected && slot.parentElement === col) return true
|
|
231
|
+
var found = null
|
|
232
|
+
try { found = col.querySelector('[' + SLOT_ATTR + ']') } catch (error) { found = null }
|
|
233
|
+
var el = found
|
|
234
|
+
if (!el) {
|
|
235
|
+
el = document.createElement('div')
|
|
236
|
+
el.setAttribute(SLOT_ATTR, '')
|
|
237
|
+
if (box.foot && box.foot.parentElement === col) col.insertBefore(el, box.foot)
|
|
238
|
+
else col.appendChild(el)
|
|
239
|
+
}
|
|
240
|
+
var old = slot
|
|
241
|
+
slot = el
|
|
242
|
+
/* 列被重建过:面板还在旧槽里就带过来;旧槽空了就拆掉。 */
|
|
243
|
+
if (old && old !== el) {
|
|
244
|
+
if (panel && panel.parentElement === old) el.appendChild(panel)
|
|
245
|
+
if (!old.children.length && old.parentElement) {
|
|
246
|
+
try { old.parentElement.removeChild(old) } catch (error) { /* 忽略 */ }
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return true
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** 活节点搬回原父。原位记在节点 expando 上(跨热更存活);原父也没了就不硬塞。 */
|
|
253
|
+
function returnHome(node) {
|
|
254
|
+
if (!node) return
|
|
255
|
+
var home = node.__capyclawChatHome
|
|
256
|
+
try { node.removeAttribute(PANEL_ATTR) } catch (error) { /* 忽略 */ }
|
|
257
|
+
if (home && home.parent) {
|
|
258
|
+
unpatchParent(home.parent)
|
|
259
|
+
try {
|
|
260
|
+
if (home.next && typeof home.parent.contains === 'function' && home.parent.contains(home.next)) {
|
|
261
|
+
home.parent.insertBefore(node, home.next)
|
|
262
|
+
} else {
|
|
263
|
+
home.parent.appendChild(node)
|
|
264
|
+
}
|
|
265
|
+
} catch (error) { /* 原父也没了:节点交给 React 后续处置 */ }
|
|
266
|
+
try { node.__capyclawChatHome = null } catch (error) { /* 忽略 */ }
|
|
267
|
+
}
|
|
268
|
+
if (node === panel) panel = null
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** 僵尸(React 已卸载、removeChild 被容错补丁吞掉):只从槽里摘掉,绝不搬回
|
|
272
|
+
* 原父 —— 搬回去就复活在主区,比槽里的新节点大,conversationRoot 下一拍又选它,
|
|
273
|
+
* 新旧轮流重钉的死循环。原父的容错补丁保留,React 后续清理不会抛错。 */
|
|
274
|
+
function detach(node) {
|
|
275
|
+
if (!node) return
|
|
276
|
+
try { if (node.parentElement) node.parentElement.removeChild(node) } catch (error) { /* 忽略 */ }
|
|
277
|
+
try { node.removeAttribute(PANEL_ATTR) } catch (error) { /* 忽略 */ }
|
|
278
|
+
try { node.__capyclawChatHome = null; node.__capyclawChatSwallowed = false } catch (error) { /* 忽略 */ }
|
|
279
|
+
if (node === panel) panel = null
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** 收拾上一版留下的钉子(热更只换脚本没跑拆卸):活节点搬回、僵尸只摘。 */
|
|
283
|
+
function healStale() {
|
|
284
|
+
if (typeof document.querySelectorAll !== 'function') return
|
|
285
|
+
var pins = []
|
|
286
|
+
try { pins = document.querySelectorAll('[' + PANEL_ATTR + ']') } catch (error) { return }
|
|
287
|
+
for (var i = 0; i < pins.length; i += 1) {
|
|
288
|
+
var el = pins[i]
|
|
289
|
+
if (el === panel) continue
|
|
290
|
+
if (el.__capyclawChatSwallowed) detach(el)
|
|
291
|
+
else returnHome(el)
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/*
|
|
296
|
+
* 复绑输入框:把常驻 editor 的 root 重新指回当前钉住面板里那块可见的
|
|
297
|
+
* contenteditable。editor 在见到绑定时 stash 到面板 expando(失绑后
|
|
298
|
+
* __lexicalEditor 被置 null,就只能靠这份记忆找回 editor)。
|
|
299
|
+
* 只对活面板做(僵尸已被 preferLivePanel 换下);root 已就位则不动作,
|
|
300
|
+
* 因此 setRootElement 引发的 style/attr 回流不会造成 observer 循环。
|
|
301
|
+
*/
|
|
302
|
+
function healComposer() {
|
|
303
|
+
if (!panel || panel.__capyclawChatSwallowed) return
|
|
304
|
+
var input = composerInputOf(panel)
|
|
305
|
+
if (!input || input.isConnected === false) return
|
|
306
|
+
if (input.__lexicalEditor) panel.__capyclawComposerEditor = input.__lexicalEditor
|
|
307
|
+
var editor = panel.__capyclawComposerEditor
|
|
308
|
+
if (!editor || typeof editor.setRootElement !== 'function' || typeof editor.getRootElement !== 'function') return
|
|
309
|
+
if (editor.getRootElement() !== input) {
|
|
310
|
+
try { editor.setRootElement(input) } catch (error) { /* 忽略:下一拍再试 */ }
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function place() {
|
|
315
|
+
if (!WINDOW_KIT || typeof document === 'undefined') return
|
|
316
|
+
var root = document.documentElement
|
|
317
|
+
/* drawer 形态:侧栏是一层临时浮层,这时候别往里搬(收起后列就没了)。 */
|
|
318
|
+
if (root && typeof root.hasAttribute === 'function' && root.hasAttribute('data-sidebar-drawer')) return
|
|
319
|
+
|
|
320
|
+
/* 同层守卫:列被整体卸掉(面板连槽脱离文档)→ 先搬回原父保住会话节点,
|
|
321
|
+
* 再交给下面的重钉逻辑。 */
|
|
322
|
+
if (panel && typeof document.contains === 'function' && !document.contains(panel)) returnHome(panel)
|
|
323
|
+
|
|
324
|
+
var chat = preferLivePanel(WINDOW_KIT.conversationRoot())
|
|
325
|
+
/* React 换树有短暂空档:这一拍找不到就等下一拍,别动槽里的面板。 */
|
|
326
|
+
if (!chat) return
|
|
327
|
+
|
|
328
|
+
if (chat === panel) {
|
|
329
|
+
/* 自身确认拍:**不清 prevPanel**。我们自己搬节点也会触发 observer,这一拍
|
|
330
|
+
* 选中的就是刚钉上的当前面板;若在这里清掉断环记忆,紧接着的过渡态就能把
|
|
331
|
+
* 刚换下的节点换回来(两拍断环失效)。prevPanel 留给真正的重钉去覆盖、
|
|
332
|
+
* 或让下面的断环分支消费。 */
|
|
333
|
+
ensureSlot(sidebarBox())
|
|
334
|
+
healComposer()
|
|
335
|
+
return
|
|
336
|
+
}
|
|
337
|
+
/* 两拍断环:这一拍挑中的是上一拍刚换下的 → 这一拍先不换回。 */
|
|
338
|
+
if (chat === prevPanel) { prevPanel = null; return }
|
|
339
|
+
|
|
340
|
+
var old = panel
|
|
341
|
+
prevPanel = old
|
|
342
|
+
if (old) {
|
|
343
|
+
if (old.__capyclawChatSwallowed) detach(old)
|
|
344
|
+
else returnHome(old)
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
var box = sidebarBox()
|
|
348
|
+
if (!box || !box.el) return
|
|
349
|
+
var parent = chat.parentElement
|
|
350
|
+
if (!parent) return
|
|
351
|
+
/* 已经在槽里(上一轮自己搬过、引用丢了)就别再套一层。 */
|
|
352
|
+
if (parent === slot) { panel = chat; healComposer(); return }
|
|
353
|
+
if (ensureSlot(box) !== true) return
|
|
354
|
+
try { chat.__capyclawChatHome = { parent: parent, next: chat.nextSibling } } catch (error) { /* 忽略 */ }
|
|
355
|
+
patchParent(parent)
|
|
356
|
+
try { chat.setAttribute(PANEL_ATTR, '') } catch (error) { /* 老环境:只是少了布局规则 */ }
|
|
357
|
+
slot.appendChild(chat)
|
|
358
|
+
panel = chat
|
|
359
|
+
healComposer()
|
|
360
|
+
if (bootTimer) { clearTimeout(bootTimer); bootTimer = 0 }
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function schedule() {
|
|
364
|
+
if (timer) return
|
|
365
|
+
timer = setTimeout(function () { timer = 0; place() }, 120)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/* 搬迁必须**同步**发生在 observer 回调里:回调是微任务,跑在浏览器绘制之前,
|
|
369
|
+
* 新面板这一帧就已经在槽里,主区来不及把它画出来。防抖 120ms 再搬 = 切会话时
|
|
370
|
+
* 主区先闪一下聊天窗(用户报过)。chat-dock 的重钉也是回调里当场 adopt。
|
|
371
|
+
* 防抖只留作「这一拍没钉成」(侧栏/面板还没就绪)的补试。
|
|
372
|
+
*
|
|
373
|
+
* 一批只搬一次:body 与 documentElement 两个 observer、以及我们自己搬节点引发的
|
|
374
|
+
* 回流,可能在同一批里多次敲到 onMutate。若每次都 place,断环分支刚把 prevPanel
|
|
375
|
+
* 清空、紧随其后的第二次 place 就把刚换下的节点又钉回来(两拍断环失效)。用
|
|
376
|
+
* queueMicrotask 把「批」界定出来:第一次同步 place,其余同批回调跳过,微任务末尾
|
|
377
|
+
* 复位。仍是微任务级、先于绘制,不引入闪烁。 */
|
|
378
|
+
var inBatch = false
|
|
379
|
+
function onMutate() {
|
|
380
|
+
if (inBatch) return
|
|
381
|
+
inBatch = true
|
|
382
|
+
place()
|
|
383
|
+
if (!panel) schedule()
|
|
384
|
+
if (typeof queueMicrotask === 'function') queueMicrotask(function () { inBatch = false })
|
|
385
|
+
else inBatch = false
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
var mo = typeof MutationObserver === 'function' ? new MutationObserver(onMutate) : null
|
|
389
|
+
/* style/class 也要盯:侧栏折叠/展开是 class 变化,不增减节点,childList 看不见。 */
|
|
390
|
+
if (mo) mo.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] })
|
|
391
|
+
/* documentElement 在 body 子树外:drawer / 折叠属性单独盯一份。 */
|
|
392
|
+
var moRoot = typeof MutationObserver === 'function' ? new MutationObserver(onMutate) : null
|
|
393
|
+
if (moRoot) moRoot.observe(document.documentElement, { attributes: true, attributeFilter: ['data-sidebar-drawer', 'data-sidebar-collapsed', 'class'] })
|
|
394
|
+
|
|
395
|
+
healStale()
|
|
396
|
+
place()
|
|
397
|
+
/* 开局重试:apply 时侧栏/会话可能还没渲染出来。钉上即停;observer 是长路。 */
|
|
398
|
+
if (!panel) {
|
|
399
|
+
bootTries = 1
|
|
400
|
+
bootTimer = setTimeout(function bootStep() {
|
|
401
|
+
bootTimer = 0
|
|
402
|
+
place()
|
|
403
|
+
if (!panel && bootTries < 40) { bootTries += 1; bootTimer = setTimeout(bootStep, 250) }
|
|
404
|
+
}, 250)
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
return function () {
|
|
408
|
+
if (timer) clearTimeout(timer)
|
|
409
|
+
if (bootTimer) clearTimeout(bootTimer)
|
|
410
|
+
timer = 0
|
|
411
|
+
bootTimer = 0
|
|
412
|
+
if (mo) mo.disconnect()
|
|
413
|
+
if (moRoot) moRoot.disconnect()
|
|
414
|
+
if (panel) {
|
|
415
|
+
/* 僵尸路径的 detach 平时保留原父补丁(React 可能还在清理);但整体卸载
|
|
416
|
+
* 必须把补丁也还原 —— 「页面改动必须可撤销」,不能留半个补丁在宿主上。 */
|
|
417
|
+
var homeParent = panel.__capyclawChatHome && panel.__capyclawChatHome.parent
|
|
418
|
+
if (panel.__capyclawChatSwallowed) detach(panel)
|
|
419
|
+
else returnHome(panel)
|
|
420
|
+
if (homeParent) unpatchParent(homeParent)
|
|
421
|
+
}
|
|
422
|
+
prevPanel = null
|
|
423
|
+
if (slot && slot.parentElement) {
|
|
424
|
+
try { slot.parentElement.removeChild(slot) } catch (error) { /* 忽略 */ }
|
|
425
|
+
}
|
|
426
|
+
slot = null
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function install(ctx) {
|
|
431
|
+
if (!WINDOW_KIT || typeof WINDOW_KIT.effect !== 'function') return function () {}
|
|
432
|
+
var book = WINDOW_KIT.effect(ctx, 'caption-map: 聊天窗停靠', mount)
|
|
433
|
+
return function () {
|
|
434
|
+
if (book && typeof book.dispose === 'function') book.dispose()
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
module.exports = { install: install, SLOT_ATTR: SLOT_ATTR, PANEL_ATTR: PANEL_ATTR, STYLE_ID: STYLE_ID }
|
|
439
|
+
return module.exports
|
|
440
|
+
},
|
|
441
|
+
})
|