dsh-codex-model-effort-ui 0.0.0-stage → 1.1.6
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/LICENSE +22 -0
- package/README.md +153 -2
- package/client.effort.js +289 -0
- package/client.js +1237 -0
- package/client.styles.js +298 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +10 -0
- package/index.js +1 -0
- package/package.json +76 -4
package/client.js
ADDED
|
@@ -0,0 +1,1237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codex 模型 + 推理强度选择器 —— 客户端(Web)打包文件。
|
|
3
|
+
*
|
|
4
|
+
* 用 Codex 风格的弹出面板替换 DSH 紧凑的 `conversation.input.model` 座位,
|
|
5
|
+
* 面板包含两个页面:
|
|
6
|
+
*
|
|
7
|
+
* - `models` —— 按提供方分组的模型列表(Codex 列表样式)
|
|
8
|
+
* - `effort` —— 像素级复刻的推理强度滑杆
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
window.__ModuleLoader__.load({
|
|
12
|
+
id: 'dsh-codex-model-effort-ui',
|
|
13
|
+
factory(require) {
|
|
14
|
+
const React = require('react');
|
|
15
|
+
const h = React.createElement;
|
|
16
|
+
const { useState, useEffect, useLayoutEffect, useRef, useSyncExternalStore, useMemo, lazy, Suspense } = React;
|
|
17
|
+
|
|
18
|
+
const NS = 'dsh-codex-model-effort-ui';
|
|
19
|
+
const P = 'cmes'; // 类名前缀
|
|
20
|
+
|
|
21
|
+
/* ------------------------------------------------------------------ *
|
|
22
|
+
* 文案
|
|
23
|
+
* ------------------------------------------------------------------ */
|
|
24
|
+
/* 两个词典必须携带完全相同的键集合。强度标签也放在这里
|
|
25
|
+
* 语言时强度阶梯会实时重新本地化——无需刷新,也无需重新挂载。 */
|
|
26
|
+
const zh = {
|
|
27
|
+
choose: '选择模型', effort: '推理强度', fast: '1.5 倍速',
|
|
28
|
+
usage: '用量更多', visual: '(仅视觉效果)', reset: '重置为默认',
|
|
29
|
+
warning: '更快消耗使用额度', model: '切换模型', back: '返回',
|
|
30
|
+
loading: '正在加载模型', empty: '没有可用模型',
|
|
31
|
+
unsupported: '此模型未提供推理强度',
|
|
32
|
+
busy: '正在切换', failed: '切换失败',
|
|
33
|
+
unavailable: '此会话不能切换模型',
|
|
34
|
+
search: '搜索模型', noMatch: '没有匹配的模型',
|
|
35
|
+
team: '多团队协作',
|
|
36
|
+
teamDesc: '开启后由多个子代理协同完成当前会话的工作,可在任务面板查看进度',
|
|
37
|
+
teamFailed: '协作开关切换失败',
|
|
38
|
+
levelOne: '{count} 档', levels: '{count} 档',
|
|
39
|
+
noLevels: '该模型不支持推理等级', noLevelsShort: '不支持',
|
|
40
|
+
loadFailed: '{name} 加载失败:{message}',
|
|
41
|
+
resetDesc: '恢复默认推理强度,并关闭 Fast',
|
|
42
|
+
'effort.off': '关闭', 'effort.minimal': '极低', 'effort.low': '轻度',
|
|
43
|
+
'effort.medium': '中', 'effort.high': '高', 'effort.xhigh': '极高',
|
|
44
|
+
'effort.max': 'Ultra'
|
|
45
|
+
};
|
|
46
|
+
const en = {
|
|
47
|
+
choose: 'Select model', effort: 'Reasoning effort', fast: '1.5x speed',
|
|
48
|
+
usage: 'Uses more quota', visual: '(Visual only)', reset: 'Reset to default',
|
|
49
|
+
warning: 'Consumes usage quota faster', model: 'Switch model', back: 'Back',
|
|
50
|
+
loading: 'Loading models', empty: 'No available models',
|
|
51
|
+
unsupported: 'This model provides no reasoning effort levels',
|
|
52
|
+
busy: 'Switching', failed: 'Selection failed',
|
|
53
|
+
unavailable: 'Model selection unavailable',
|
|
54
|
+
search: 'Search models', noMatch: 'No matching models',
|
|
55
|
+
team: 'Agent Teams',
|
|
56
|
+
teamDesc: 'Let subagents work on this session together; progress appears in the task panel',
|
|
57
|
+
teamFailed: 'Could not switch Agent Teams',
|
|
58
|
+
levelOne: '{count} level', levels: '{count} levels',
|
|
59
|
+
noLevels: 'No reasoning levels for this model', noLevelsShort: 'Unsupported',
|
|
60
|
+
loadFailed: '{name} failed to load: {message}',
|
|
61
|
+
resetDesc: 'Restore the default reasoning effort and turn Fast off',
|
|
62
|
+
'effort.off': 'Off', 'effort.minimal': 'Minimal', 'effort.low': 'Light',
|
|
63
|
+
'effort.medium': 'Medium', 'effort.high': 'High', 'effort.xhigh': 'Extra High',
|
|
64
|
+
'effort.max': 'Ultra'
|
|
65
|
+
};
|
|
66
|
+
function fill(template, vars) {
|
|
67
|
+
return String(template).replace(/\{(\w+)\}/g, (whole, key) =>
|
|
68
|
+
Object.prototype.hasOwnProperty.call(vars, key) ? String(vars[key]) : whole);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function isDarkNow() {
|
|
72
|
+
try {
|
|
73
|
+
return typeof document !== 'undefined'
|
|
74
|
+
&& document.body != null
|
|
75
|
+
&& document.body.hasAttribute('data-ds-dark-theme') === true;
|
|
76
|
+
} catch { return false; }
|
|
77
|
+
}
|
|
78
|
+
function subscribeTheme(notify) {
|
|
79
|
+
if (typeof MutationObserver === 'undefined' || typeof document === 'undefined'
|
|
80
|
+
|| document.body == null) return () => {};
|
|
81
|
+
const mo = new MutationObserver(notify);
|
|
82
|
+
mo.observe(document.body, { attributes: true, attributeFilter: ['data-ds-dark-theme'] });
|
|
83
|
+
return () => mo.disconnect();
|
|
84
|
+
}
|
|
85
|
+
/** 组件树使用的响应式深色模式标志。 */
|
|
86
|
+
function useDarkTheme() {
|
|
87
|
+
return useSyncExternalStore(subscribeTheme, isDarkNow, () => false);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/* ------------------------------------------------------------------ *
|
|
91
|
+
* 图标(手绘路径;不含第三方资源)
|
|
92
|
+
* ------------------------------------------------------------------ */
|
|
93
|
+
const ICON = {
|
|
94
|
+
chevron: 'm9 18 6-6-6-6',
|
|
95
|
+
down: 'm6 9 6 6 6-6',
|
|
96
|
+
check: 'm20 6-11 11-5-5',
|
|
97
|
+
reset: 'M3 11a9 9 0 1 1 2.6 7M3 4v7h7',
|
|
98
|
+
info: 'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18ZM12 11v5M12 7.4v.01',
|
|
99
|
+
search: 'M11 4a7 7 0 1 0 0 14 7 7 0 0 0 0-14ZM20 20l-4-4',
|
|
100
|
+
model: 'M12 3 3 8l9 5 9-5-9-5ZM3 12l9 5 9-5M3 16l9 5 9-5',
|
|
101
|
+
bolt: 'M14.4 2Q16 .2 15.8 2.5L15.1 8.5H20.5Q22 8.5 21 9.8L10.7 21.7Q9 23.4 9.2 21.2L10 14.2H4.2Q2.6 14.2 3.6 12.9L14.4 2Z'
|
|
102
|
+
};
|
|
103
|
+
function icon(name, cls) {
|
|
104
|
+
return h('svg', {
|
|
105
|
+
className: cls || `${P}-icon`, viewBox: '0 0 24 24', 'aria-hidden': true,
|
|
106
|
+
fill: 'none', stroke: 'currentColor', strokeWidth: 1.7,
|
|
107
|
+
strokeLinecap: 'round', strokeLinejoin: 'round'
|
|
108
|
+
}, h('path', { d: ICON[name] || ICON.chevron }));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/* 焦点陷阱:弹层是 role="dialog",Tab 不该走出它落到背景页上。
|
|
112
|
+
* 在弹层打开期间拦下 Tab,在「首个 / 末个」可聚焦元素处掉头循环,
|
|
113
|
+
* 把焦点始终圈在弹层内。只处理 Tab,其余按键(方向键 / Enter /
|
|
114
|
+
* Escape)原样放行,不与滑轨、搜索框、模型列表的键盘逻辑抢事件。
|
|
115
|
+
* 宿主若注入了自己的陷阱,这里只是冗余兜底;未注入时补齐缺口。 */
|
|
116
|
+
const FOCUSABLE = 'a[href],button:not([disabled]),input:not([disabled]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex="-1"])';
|
|
117
|
+
function trapFocus(section, event) {
|
|
118
|
+
if (event.key !== 'Tab') return;
|
|
119
|
+
const nodes = Array.from(section.querySelectorAll(FOCUSABLE))
|
|
120
|
+
.filter(el => !el.closest(`.${P}-pane.hidden`) && (el.offsetWidth > 0 || el.offsetHeight > 0));
|
|
121
|
+
if (!nodes.length) return;
|
|
122
|
+
const first = nodes[0];
|
|
123
|
+
const last = nodes[nodes.length - 1];
|
|
124
|
+
const active = section.ownerDocument.activeElement;
|
|
125
|
+
if (event.shiftKey) {
|
|
126
|
+
if (active === first || !section.contains(active)) { event.preventDefault(); last.focus({ preventScroll: true }); }
|
|
127
|
+
} else if (active === last || !section.contains(active)) {
|
|
128
|
+
event.preventDefault(); first.focus({ preventScroll: true });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/* ------------------------------------------------------------------ *
|
|
133
|
+
* Fast 开关 —— 按会话持久化
|
|
134
|
+
* 本插件是 fail-open 的,因此每次存储访问都有保护:失败只损失重启后
|
|
135
|
+
* 的恢复,绝不会影响输入框。
|
|
136
|
+
* ------------------------------------------------------------------ */
|
|
137
|
+
const FAST_STORE_PREFIX = 'dsh.codex-effort-selector.fast.v1.';
|
|
138
|
+
const fastMemory = new Map();
|
|
139
|
+
function readFast(sessionId) {
|
|
140
|
+
if (!sessionId) return false;
|
|
141
|
+
if (fastMemory.has(sessionId)) return fastMemory.get(sessionId);
|
|
142
|
+
try {
|
|
143
|
+
if (typeof localStorage === 'undefined') return false;
|
|
144
|
+
return localStorage.getItem(FAST_STORE_PREFIX + sessionId) === '1';
|
|
145
|
+
} catch { return false; }
|
|
146
|
+
}
|
|
147
|
+
function writeFast(sessionId, on) {
|
|
148
|
+
if (!sessionId) return;
|
|
149
|
+
fastMemory.set(sessionId, on);
|
|
150
|
+
try {
|
|
151
|
+
if (typeof localStorage === 'undefined') return;
|
|
152
|
+
localStorage.setItem(FAST_STORE_PREFIX + sessionId, on ? '1' : '0');
|
|
153
|
+
} catch { /* 存储被禁用 —— 内存镜像仍然保留该值 */ }
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function Selector(props) {
|
|
157
|
+
return typeof props.useProjection === 'function'
|
|
158
|
+
? h(TeamProjectionBridge, props)
|
|
159
|
+
: h(SelectorImpl, props);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/* 只在宿主提供 `useProjection` 时挂载:这里对它的调用是无条件的。 */
|
|
163
|
+
function TeamProjectionBridge(props) {
|
|
164
|
+
const teamMode = props.useProjection('agentTeam');
|
|
165
|
+
return h(SelectorImpl, { ...props, teamMode });
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/* ================================================================== *
|
|
169
|
+
* 可复用的定位 / 状态 Hook
|
|
170
|
+
* ================================================================== */
|
|
171
|
+
|
|
172
|
+
/* 固定定位层所在的包含块,以视口坐标表示,由探针元素测量得到
|
|
173
|
+
* (见 `probeRef`):其左上角是包含块的 padding 原点,其底边是包含块
|
|
174
|
+
* 的底部。探针尚未挂载时,包含块就是视口。 */
|
|
175
|
+
function containingBoxOf(probe) {
|
|
176
|
+
if (probe) {
|
|
177
|
+
const box = probe.getBoundingClientRect();
|
|
178
|
+
return { left: box.left, top: box.top, bottom: box.bottom };
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
left: 0,
|
|
182
|
+
top: 0,
|
|
183
|
+
bottom: typeof window === 'undefined' ? 0 : window.innerHeight
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function positionAbove(anchor, tip, box, fallbackW, fallbackH) {
|
|
188
|
+
if (!anchor || !tip) return;
|
|
189
|
+
const r = anchor.getBoundingClientRect();
|
|
190
|
+
const tw = tip.offsetWidth || fallbackW;
|
|
191
|
+
const th = tip.offsetHeight || fallbackH;
|
|
192
|
+
const left = Math.max(8, Math.min(r.left + r.width / 2 - tw / 2, window.innerWidth - tw - 8));
|
|
193
|
+
const top = Math.max(8, r.top - th - 7);
|
|
194
|
+
tip.style.left = `${left - box.left}px`;
|
|
195
|
+
tip.style.top = `${top - box.top}px`;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function useAnchoredTip(shown, anchorRef, tipRef, probeRef, fallbackW, fallbackH) {
|
|
199
|
+
useLayoutEffect(() => {
|
|
200
|
+
if (!shown) return;
|
|
201
|
+
const place = () => positionAbove(
|
|
202
|
+
anchorRef.current, tipRef.current, containingBoxOf(probeRef.current), fallbackW, fallbackH
|
|
203
|
+
);
|
|
204
|
+
place();
|
|
205
|
+
window.addEventListener('resize', place);
|
|
206
|
+
document.addEventListener('scroll', place, true);
|
|
207
|
+
return () => {
|
|
208
|
+
window.removeEventListener('resize', place);
|
|
209
|
+
document.removeEventListener('scroll', place, true);
|
|
210
|
+
};
|
|
211
|
+
}, [shown, anchorRef, tipRef, probeRef, fallbackW, fallbackH]);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/* 弹出面板的定位、高度余量与外部关闭 / Escape。
|
|
215
|
+
* 回调经 ref 读取,监听器只随 open 重绑一次,避免闭包过期。 */
|
|
216
|
+
function usePopoverLayout({ open, rootRef, popupRef, probeRef, triggerRef, onOutside, onEscape }) {
|
|
217
|
+
const [room, setRoom] = useState(0);
|
|
218
|
+
const handlersRef = useRef({ onOutside, onEscape });
|
|
219
|
+
useLayoutEffect(() => { handlersRef.current = { onOutside, onEscape }; });
|
|
220
|
+
|
|
221
|
+
/* 把触发按钮对齐到弹层:按包含块换算 left/bottom,并回写高度余量。
|
|
222
|
+
* 只读 ref 与 window,因此是引用稳定的——既能被下面的 layout effect
|
|
223
|
+
* 绑定到事件上,也能被 `primeRoom` 在打开前直接调用,把首帧的
|
|
224
|
+
* left/bottom 一并预置(否则首帧会用 CSS 里的回退坐标画一帧再跳正)。 */
|
|
225
|
+
const align = React.useCallback(() => {
|
|
226
|
+
const trigger = triggerRef.current?.getBoundingClientRect();
|
|
227
|
+
const popup = popupRef.current;
|
|
228
|
+
if (!trigger || !popup || !rootRef.current) return;
|
|
229
|
+
const width = Math.min(272, window.innerWidth - 24);
|
|
230
|
+
const left = Math.max(12, Math.min((trigger.left + trigger.right - width) / 2, window.innerWidth - width - 12));
|
|
231
|
+
/* 弹出面板是 `position: fixed`,因此浏览器按包含块而非窗口解读
|
|
232
|
+
* 这些数值:用测量到的包含块重新换算,视口时原点就是 0,0。 */
|
|
233
|
+
const box = containingBoxOf(probeRef.current);
|
|
234
|
+
rootRef.current.style.setProperty(`--${P}-left`, `${left - box.left}px`);
|
|
235
|
+
rootRef.current.style.setProperty(`--${P}-bottom`, `${Math.max(12, box.bottom - trigger.top + 8)}px`);
|
|
236
|
+
/* 触发按钮上方的空间:驱动高度钳制,同时写回 state,让 render
|
|
237
|
+
* 不必自己读布局。 */
|
|
238
|
+
const roomAbove = Math.max(120, trigger.top - 20);
|
|
239
|
+
rootRef.current.style.setProperty(`--${P}-room`, `${roomAbove}px`);
|
|
240
|
+
setRoom(prev => (prev === roomAbove ? prev : roomAbove));
|
|
241
|
+
}, [rootRef, popupRef, probeRef, triggerRef]);
|
|
242
|
+
|
|
243
|
+
useLayoutEffect(() => {
|
|
244
|
+
if (!open) return;
|
|
245
|
+
let frame = 0;
|
|
246
|
+
function outside(event) {
|
|
247
|
+
if (!rootRef.current?.contains(event.target)) handlersRef.current.onOutside(event);
|
|
248
|
+
}
|
|
249
|
+
function escape(event) {
|
|
250
|
+
if (event.key !== 'Escape') return;
|
|
251
|
+
handlersRef.current.onEscape(event);
|
|
252
|
+
}
|
|
253
|
+
function resized() {
|
|
254
|
+
cancelAnimationFrame(frame);
|
|
255
|
+
const until = performance.now() + 500;
|
|
256
|
+
const settle = () => { align(); if (performance.now() < until) frame = requestAnimationFrame(settle); };
|
|
257
|
+
frame = requestAnimationFrame(settle);
|
|
258
|
+
}
|
|
259
|
+
document.addEventListener('pointerdown', outside);
|
|
260
|
+
document.addEventListener('keydown', escape, true);
|
|
261
|
+
window.addEventListener('resize', resized);
|
|
262
|
+
document.addEventListener('scroll', align, true);
|
|
263
|
+
/* 输入区是多行的:换行 / 贴图 / 切会话会让触发按钮在**既不产生 window
|
|
264
|
+
* resize、也未必冒泡出 scroll 事件**的情况下移动,而弹层的 bottom 是
|
|
265
|
+
* 打开那一刻按 trigger.top 算死的——按钮一动弹层不跟,于是相对按钮
|
|
266
|
+
* 上浮或下沉。用一个观察器盯住触发按钮的定位祖先(`offsetParent`,
|
|
267
|
+
* 正是那条 overflow:auto/hidden 的输入区链)与根节点:它们的盒子
|
|
268
|
+
* 尺寸一变(输入区长高、滚动条出现/消失、外框平移)就重跑 `align`,
|
|
269
|
+
* 把弹层重新锚定到当前按钮位置。观察器回调在绘制前触发,因此漂移
|
|
270
|
+
* 不会先画出来再纠正。
|
|
271
|
+
*
|
|
272
|
+
* 已知残留缺口:ResizeObserver 只对「盒子尺寸变化」敏感。若按钮因
|
|
273
|
+
* 某祖先的 CSS transform / 主动画被整体平移,而所有被观察盒子尺寸
|
|
274
|
+
* 都没变,观察器不会响——这类纯位移只能靠 IntersectionObserver 或
|
|
275
|
+
* rAF 轮询兜底,但对一个 fail-open 插件而言过重,故刻意不引入。
|
|
276
|
+
* scroll(capture)与 window resize 监听仍覆盖绝大多数实际场景。 */
|
|
277
|
+
let observer = null;
|
|
278
|
+
if (typeof ResizeObserver === 'function') {
|
|
279
|
+
observer = new ResizeObserver(() => align());
|
|
280
|
+
const anchor = triggerRef.current?.offsetParent;
|
|
281
|
+
if (anchor) observer.observe(anchor);
|
|
282
|
+
if (rootRef.current) observer.observe(rootRef.current);
|
|
283
|
+
}
|
|
284
|
+
align();
|
|
285
|
+
return () => {
|
|
286
|
+
document.removeEventListener('pointerdown', outside);
|
|
287
|
+
document.removeEventListener('keydown', escape, true);
|
|
288
|
+
window.removeEventListener('resize', resized);
|
|
289
|
+
document.removeEventListener('scroll', align, true);
|
|
290
|
+
if (observer) observer.disconnect();
|
|
291
|
+
cancelAnimationFrame(frame);
|
|
292
|
+
};
|
|
293
|
+
}, [open, align, rootRef, popupRef, probeRef, triggerRef]);
|
|
294
|
+
|
|
295
|
+
/* 打开前先量一次:首帧就有正确的高度 **和位置**,不会先回退到 CSS 里的
|
|
296
|
+
* 回退坐标(left:12 / bottom:40)再跳变。 */
|
|
297
|
+
function primeRoom() {
|
|
298
|
+
align();
|
|
299
|
+
}
|
|
300
|
+
return { room, primeRoom };
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/* Fast 开关的按会话持久化:内存镜像负责同一运行期,localStorage 负责
|
|
304
|
+
* 跨重启;每次存储访问都有保护(插件保持 fail-open)。 */
|
|
305
|
+
function useFastPreference(sessionId) {
|
|
306
|
+
const [fast, setFastState] = useState(() => readFast(sessionId));
|
|
307
|
+
const fastRef = useRef(fast);
|
|
308
|
+
const sessionRef = useRef(sessionId);
|
|
309
|
+
useLayoutEffect(() => { fastRef.current = fast; }, [fast]);
|
|
310
|
+
/* 宿主复用组件实例切换会话时,采纳新会话的偏好;真正的重新挂载已由
|
|
311
|
+
* useState 初始化器完成,此时观察不到变化。 */
|
|
312
|
+
useEffect(() => {
|
|
313
|
+
if (sessionRef.current === sessionId) return;
|
|
314
|
+
sessionRef.current = sessionId;
|
|
315
|
+
const stored = readFast(sessionId);
|
|
316
|
+
fastRef.current = stored;
|
|
317
|
+
setFastState(stored);
|
|
318
|
+
}, [sessionId]);
|
|
319
|
+
/* 写穿:接受值或更新函数,与它所包裹的 setState 行为一致。 */
|
|
320
|
+
function setFast(next) {
|
|
321
|
+
const value = typeof next === 'function' ? next(fastRef.current) : next;
|
|
322
|
+
if (value === fastRef.current) return;
|
|
323
|
+
fastRef.current = value;
|
|
324
|
+
setFastState(value);
|
|
325
|
+
writeFast(sessionId, value);
|
|
326
|
+
}
|
|
327
|
+
return [fast, setFast];
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/* 选择提交的串行化:同一时刻只发一次请求,后续选择合并为最新值。 */
|
|
331
|
+
function useSelectionCommit({ select, disabled, t, onError, onSettled, mountedRef }) {
|
|
332
|
+
const [busy, setBusy] = useState(false);
|
|
333
|
+
const pendingRef = useRef(null);
|
|
334
|
+
const busyRef = useRef(false);
|
|
335
|
+
|
|
336
|
+
async function commit(next) {
|
|
337
|
+
if (disabled || !next) return;
|
|
338
|
+
pendingRef.current = next;
|
|
339
|
+
if (busyRef.current) return;
|
|
340
|
+
busyRef.current = true;
|
|
341
|
+
setBusy(true);
|
|
342
|
+
onError('');
|
|
343
|
+
try {
|
|
344
|
+
while (pendingRef.current) {
|
|
345
|
+
const want = pendingRef.current;
|
|
346
|
+
pendingRef.current = null;
|
|
347
|
+
const result = await select(want);
|
|
348
|
+
if (result && !result.ok) throw new Error(result.error?.message || t('failed'));
|
|
349
|
+
if (result === undefined) throw new Error(t('unavailable'));
|
|
350
|
+
}
|
|
351
|
+
} catch (cause) {
|
|
352
|
+
pendingRef.current = null;
|
|
353
|
+
if (mountedRef.current) onError(cause?.message || t('failed'));
|
|
354
|
+
} finally {
|
|
355
|
+
busyRef.current = false;
|
|
356
|
+
if (mountedRef.current) {
|
|
357
|
+
setBusy(false);
|
|
358
|
+
onSettled();
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return { busy, busyRef, commit };
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/* Agent Teams 开关:能力门控 + 写入 + 失败上报。投影由桥接组件传入。 */
|
|
366
|
+
function useTeamToggle({ teamMode, setTeamMode, available, t, onError, mountedRef }) {
|
|
367
|
+
const [pending, setPending] = useState(false);
|
|
368
|
+
const supported = teamMode !== undefined
|
|
369
|
+
&& teamMode !== null
|
|
370
|
+
&& typeof setTeamMode === 'function'
|
|
371
|
+
&& available;
|
|
372
|
+
const enabled = supported && teamMode.enabled === true;
|
|
373
|
+
|
|
374
|
+
function toggle(next) {
|
|
375
|
+
if (!supported || pending) return;
|
|
376
|
+
setPending(true);
|
|
377
|
+
onError('');
|
|
378
|
+
Promise.resolve(setTeamMode(next)).then((result) => {
|
|
379
|
+
const failure = result && result.ok === false
|
|
380
|
+
? ((result.error && result.error.message) || t('teamFailed'))
|
|
381
|
+
: null;
|
|
382
|
+
if (failure) onError(failure);
|
|
383
|
+
}, (cause) => {
|
|
384
|
+
onError(cause instanceof Error ? cause.message : String(cause));
|
|
385
|
+
}).finally(() => { if (mountedRef.current) setPending(false); });
|
|
386
|
+
}
|
|
387
|
+
return { supported, enabled, pending, toggle };
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function useRailDrag({ railRef, efforts, index, disabled, onMove, onPick, onCancel, U }) {
|
|
391
|
+
const [dragging, setDragging] = useState(false);
|
|
392
|
+
const [hovered, setHovered] = useState(-1);
|
|
393
|
+
const dragRef = useRef(false);
|
|
394
|
+
const pointerRef = useRef(null);
|
|
395
|
+
const liveRef = useRef(null);
|
|
396
|
+
const dragStartIndexRef = useRef(0);
|
|
397
|
+
const lastAppliedRef = useRef(null);
|
|
398
|
+
const apiRef = useRef({});
|
|
399
|
+
|
|
400
|
+
/* 指针映射与吸附比例都在 effort 分块里,用真实滑轨矩形调用,便于单测。 */
|
|
401
|
+
const railBox = () => railRef.current?.getBoundingClientRect();
|
|
402
|
+
function fractionAt(clientX) {
|
|
403
|
+
return U.fractionAt(clientX, railBox());
|
|
404
|
+
}
|
|
405
|
+
function nearestIndex(clientX) {
|
|
406
|
+
return U.nearestIndex(clientX, railBox(), efforts.length, index);
|
|
407
|
+
}
|
|
408
|
+
function paintLive(frac) {
|
|
409
|
+
railRef.current?.style.setProperty(`--${P}-pos`, String(frac));
|
|
410
|
+
}
|
|
411
|
+
function stopFraction(i) {
|
|
412
|
+
return U.stopFraction(i, efforts.length);
|
|
413
|
+
}
|
|
414
|
+
/* 强制把圆钮放到档位上,绕过 React 对相同状态的跳过渲染。 */
|
|
415
|
+
function snapLive(i) {
|
|
416
|
+
railRef.current?.style.setProperty(`--${P}-pos`, String(stopFraction(i)));
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/* 只有一个地方把指针位置应用到滑轨:按压与文档级 move 都走这里。 */
|
|
420
|
+
function applyPointer(clientX) {
|
|
421
|
+
const frac = fractionAt(clientX);
|
|
422
|
+
liveRef.current = frac;
|
|
423
|
+
paintLive(frac);
|
|
424
|
+
const i = Math.round(frac * (efforts.length - 1));
|
|
425
|
+
const id = efforts[i]?.id;
|
|
426
|
+
if (id && id !== lastAppliedRef.current) {
|
|
427
|
+
lastAppliedRef.current = id;
|
|
428
|
+
onMove(id);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function pointerDown(event) {
|
|
433
|
+
/* 刻意不受 busy 门控:进行中的提交绝不能让滑轨不可抓握;提交会合并,
|
|
434
|
+
* 因此期间开始的拖拽直接取代它。 */
|
|
435
|
+
if (disabled || efforts.length < 2) return;
|
|
436
|
+
/* 抑制原生拖放/文本选择,它们会触发 pointercancel 杀死手势。 */
|
|
437
|
+
event.preventDefault();
|
|
438
|
+
railRef.current?.focus({ preventScroll: true });
|
|
439
|
+
/* 指针捕获只是便利手段:手势由文档级监听器掌管。 */
|
|
440
|
+
try { event.currentTarget.setPointerCapture(event.pointerId); } catch { /* 非致命 */ }
|
|
441
|
+
dragRef.current = true;
|
|
442
|
+
pointerRef.current = event.pointerId;
|
|
443
|
+
dragStartIndexRef.current = index;
|
|
444
|
+
lastAppliedRef.current = null;
|
|
445
|
+
setDragging(true);
|
|
446
|
+
setHovered(-1);
|
|
447
|
+
applyPointer(event.clientX);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/* 仅作悬停反馈;拖拽分支在文档级监听器里。 */
|
|
451
|
+
function pointerMove(event) {
|
|
452
|
+
if (dragRef.current) return;
|
|
453
|
+
const i = nearestIndex(event.clientX);
|
|
454
|
+
const box = railRef.current?.getBoundingClientRect();
|
|
455
|
+
if (!box) return;
|
|
456
|
+
const tickX = box.left + 15 + i / Math.max(1, efforts.length - 1) * (box.width - 30);
|
|
457
|
+
setHovered(Math.abs(event.clientX - tickX) < 10 && i !== index ? i : -1);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function finish(event) {
|
|
461
|
+
if (!dragRef.current) return;
|
|
462
|
+
dragRef.current = false;
|
|
463
|
+
pointerRef.current = null;
|
|
464
|
+
setDragging(false);
|
|
465
|
+
liveRef.current = null;
|
|
466
|
+
lastAppliedRef.current = null;
|
|
467
|
+
const known = event != null && Number.isFinite(event.clientX);
|
|
468
|
+
const i = known ? nearestIndex(event.clientX) : index;
|
|
469
|
+
snapLive(i);
|
|
470
|
+
const target = efforts[i] ?? efforts[index];
|
|
471
|
+
if (target) onPick(target.id);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/* 取消的拖拽回滚到按压时记录的档位(不提交)。 */
|
|
475
|
+
function cancelDrag() {
|
|
476
|
+
if (!dragRef.current) return;
|
|
477
|
+
dragRef.current = false;
|
|
478
|
+
pointerRef.current = null;
|
|
479
|
+
setDragging(false);
|
|
480
|
+
liveRef.current = null;
|
|
481
|
+
lastAppliedRef.current = null;
|
|
482
|
+
/* 展示层回滚(预览、峰值提示)由调用方负责,Hook 只管手势与吸附。 */
|
|
483
|
+
onCancel();
|
|
484
|
+
snapLive(dragStartIndexRef.current);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function keyDown(event) {
|
|
488
|
+
let next = index;
|
|
489
|
+
if (event.key === 'ArrowRight' || event.key === 'ArrowUp') next++;
|
|
490
|
+
else if (event.key === 'ArrowLeft' || event.key === 'ArrowDown') next--;
|
|
491
|
+
else if (event.key === 'Home') next = 0;
|
|
492
|
+
else if (event.key === 'End') next = efforts.length - 1;
|
|
493
|
+
else return;
|
|
494
|
+
event.preventDefault();
|
|
495
|
+
const i = Math.max(0, Math.min(efforts.length - 1, next));
|
|
496
|
+
/* 键盘移动本就落在档位上,仍显式吸附,避免同一次跳过渲染留下旧位置。 */
|
|
497
|
+
snapLive(i);
|
|
498
|
+
const target = efforts[i];
|
|
499
|
+
if (target) onPick(target.id);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
useLayoutEffect(() => {
|
|
503
|
+
apiRef.current = { applyPointer, finish, cancelDrag };
|
|
504
|
+
});
|
|
505
|
+
useEffect(() => {
|
|
506
|
+
const owns = e => pointerRef.current === null || e.pointerId === pointerRef.current;
|
|
507
|
+
const move = e => { if (owns(e) && dragRef.current) apiRef.current.applyPointer(e.clientX); };
|
|
508
|
+
const up = e => { if (owns(e) && dragRef.current) apiRef.current.finish(e); };
|
|
509
|
+
const cancel = e => { if (owns(e) && dragRef.current) apiRef.current.cancelDrag(); };
|
|
510
|
+
/* 窗口外释放不会到达文档;焦点丢失是唯一信号,手势不能一直锁死。 */
|
|
511
|
+
const blur = () => { if (dragRef.current) apiRef.current.finish(); };
|
|
512
|
+
document.addEventListener('pointermove', move, true);
|
|
513
|
+
document.addEventListener('pointerup', up, true);
|
|
514
|
+
document.addEventListener('pointercancel', cancel, true);
|
|
515
|
+
window.addEventListener('blur', blur);
|
|
516
|
+
return () => {
|
|
517
|
+
document.removeEventListener('pointermove', move, true);
|
|
518
|
+
document.removeEventListener('pointerup', up, true);
|
|
519
|
+
document.removeEventListener('pointercancel', cancel, true);
|
|
520
|
+
window.removeEventListener('blur', blur);
|
|
521
|
+
};
|
|
522
|
+
}, []);
|
|
523
|
+
|
|
524
|
+
return { dragging, hovered, setHovered, pointerDown, pointerMove, finish, cancelDrag, keyDown };
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
function SelectorImpl({ directory, load, select, available, locked, t, sessionId, setTeamMode, teamMode, css, M, U }) {
|
|
528
|
+
const state = useSyncExternalStore(
|
|
529
|
+
fn => directory.subscribe(fn),
|
|
530
|
+
() => directory.getSnapshot()
|
|
531
|
+
);
|
|
532
|
+
|
|
533
|
+
const [open, setOpen] = useState(false);
|
|
534
|
+
const [pane, setPane] = useState('effort');
|
|
535
|
+
const [preview, setPreview] = useState(null);
|
|
536
|
+
const [message, setMessage] = useState(false);
|
|
537
|
+
const [error, setError] = useState('');
|
|
538
|
+
const [query, setQuery] = useState('');
|
|
539
|
+
const [highlight, setHighlight] = useState(0);
|
|
540
|
+
/* tooltip 的可见性现在由状态驱动,而不再是 `:hover` CSS:tooltip
|
|
541
|
+
* 必须位于弹出面板之外,以逃出 backdrop-filter 的包含块,因此它不再
|
|
542
|
+
* 是 ⚡ 按钮的后代,任何后代选择器都无法选中它(完整诊断见 CSS 块)。 */
|
|
543
|
+
const [tipOn, setTipOn] = useState(false);
|
|
544
|
+
const [teamTipOn, setTeamTipOn] = useState(false);
|
|
545
|
+
const [resetTipOn, setResetTipOn] = useState(false);
|
|
546
|
+
const [bloom, setBloom] = useState(0);
|
|
547
|
+
const [view, setView] = useState(1);
|
|
548
|
+
const [subProvider, setSubProvider] = useState(null);
|
|
549
|
+
const rootRef = useRef(null);
|
|
550
|
+
const popupRef = useRef(null);
|
|
551
|
+
const probeRef = useRef(null);
|
|
552
|
+
const railRef = useRef(null);
|
|
553
|
+
const triggerRef = useRef(null);
|
|
554
|
+
const searchRef = useRef(null);
|
|
555
|
+
const tipRef = useRef(null);
|
|
556
|
+
const fastBtnRef = useRef(null);
|
|
557
|
+
const resetRef = useRef(null);
|
|
558
|
+
const resetTipRef = useRef(null);
|
|
559
|
+
const teamTipRef = useRef(null);
|
|
560
|
+
const teamInfoBtnRef = useRef(null);
|
|
561
|
+
const previewRef = useRef(null);
|
|
562
|
+
const mountedRef = useRef(true);
|
|
563
|
+
const timersRef = useRef([]);
|
|
564
|
+
const latest = useRef({});
|
|
565
|
+
const seenIndexRef = useRef(null);
|
|
566
|
+
|
|
567
|
+
/* ---------- 派生目录状态 ---------- */
|
|
568
|
+
const selection = state.pending || state.current;
|
|
569
|
+
const groups = state.groups || [];
|
|
570
|
+
const group = groups.find(g => g.id === selection?.provider);
|
|
571
|
+
const model = group?.models.find(m => m.id === selection?.model);
|
|
572
|
+
const efforts = U.sortEfforts(model?.reasoning?.efforts);
|
|
573
|
+
const savedEffort = selection?.reasoningEffort ?? model?.reasoning?.defaultEffort;
|
|
574
|
+
const displayed = preview ?? savedEffort;
|
|
575
|
+
const index = Math.max(0, efforts.findIndex(e => e.id === displayed));
|
|
576
|
+
const fraction = efforts.length > 1 ? index / (efforts.length - 1) : 0;
|
|
577
|
+
const peak = U.isPeakId(displayed);
|
|
578
|
+
const dark = useDarkTheme();
|
|
579
|
+
const modelName = model?.name || selection?.model || t('choose');
|
|
580
|
+
const disabled = Boolean(locked || !available);
|
|
581
|
+
const tipShown = tipOn && open && pane === 'effort';
|
|
582
|
+
const teamTipShown = teamTipOn && open && pane === 'effort';
|
|
583
|
+
const resetTipShown = resetTipOn && open && pane === 'effort';
|
|
584
|
+
|
|
585
|
+
const [fast, setFast] = useFastPreference(sessionId);
|
|
586
|
+
const team = useTeamToggle({ teamMode, setTeamMode, available, t, onError: setError, mountedRef });
|
|
587
|
+
/* 弹出面板定位 / 高度余量 / 外部关闭 / Escape。 */
|
|
588
|
+
const { room, primeRoom } = usePopoverLayout({
|
|
589
|
+
open, rootRef, popupRef, probeRef, triggerRef,
|
|
590
|
+
onOutside: handleOutside, onEscape: handleEscape
|
|
591
|
+
});
|
|
592
|
+
/* 提交串行化:结束后清掉预览值。 */
|
|
593
|
+
const { busy, busyRef, commit } = useSelectionCommit({
|
|
594
|
+
select, disabled, t, mountedRef,
|
|
595
|
+
onError: setError,
|
|
596
|
+
onSettled: clearPreview
|
|
597
|
+
});
|
|
598
|
+
/* 滑轨手势与键盘:移动只预览,释放才提交。 */
|
|
599
|
+
const rail = useRailDrag({
|
|
600
|
+
railRef, efforts, index, disabled,
|
|
601
|
+
onMove: showEffort, onPick: chooseEffort,
|
|
602
|
+
onCancel: cancelPreview, U
|
|
603
|
+
});
|
|
604
|
+
useAnchoredTip(tipShown, fastBtnRef, tipRef, probeRef, 120, 44);
|
|
605
|
+
useAnchoredTip(teamTipShown, teamInfoBtnRef, teamTipRef, probeRef, 220, 60);
|
|
606
|
+
useAnchoredTip(resetTipShown, resetRef, resetTipRef, probeRef, 140, 48);
|
|
607
|
+
|
|
608
|
+
const sparkles = useMemo(() => U.buildSparkles(22), [U]);
|
|
609
|
+
const bloomMotes = useMemo(() => U.buildBloom(U.BLOOM_COUNT), [U]);
|
|
610
|
+
|
|
611
|
+
/* `latest` 供 Escape 处理器读取「最近一次提交」的面板状态。只在 layout
|
|
612
|
+
* effect 里同步:render 期间写 ref 会破坏渲染的纯粹性。刻意不放
|
|
613
|
+
* open——Escape 监听器只在弹层打开期间绑定,这里只保留 Escape 退层
|
|
614
|
+
* 真正需要的 pane/view 等即时状态,避免读到关闭动画窗口内的过期值。 */
|
|
615
|
+
useLayoutEffect(() => {
|
|
616
|
+
latest.current = { fast, peak, pane, efforts, index, savedEffort, view };
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
/* ---------- 副作用 ---------- */
|
|
620
|
+
useEffect(() => { if (available) load(); }, [available, load]);
|
|
621
|
+
|
|
622
|
+
useEffect(() => {
|
|
623
|
+
mountedRef.current = true;
|
|
624
|
+
return () => {
|
|
625
|
+
mountedRef.current = false;
|
|
626
|
+
timersRef.current.forEach(clearTimeout);
|
|
627
|
+
};
|
|
628
|
+
}, []);
|
|
629
|
+
|
|
630
|
+
/* tooltip 绝不能比锚定它的状态活得更久 */
|
|
631
|
+
useEffect(() => { if (!open) setTipOn(false); }, [open]);
|
|
632
|
+
useEffect(() => { if (pane !== 'effort') setTipOn(false); }, [pane]);
|
|
633
|
+
useEffect(() => { if (!open) setTeamTipOn(false); }, [open]);
|
|
634
|
+
useEffect(() => { if (pane !== 'effort') setTeamTipOn(false); }, [pane]);
|
|
635
|
+
useEffect(() => { if (!open) setResetTipOn(false); }, [open]);
|
|
636
|
+
useEffect(() => { if (pane !== 'effort') setResetTipOn(false); }, [pane]);
|
|
637
|
+
/* 重置出错/进入 busy 时按钮会变成 disabled,指针离开事件可能收不到;
|
|
638
|
+
* 这里主动收起,避免提示卡悬在不响应的按钮上。 */
|
|
639
|
+
useEffect(() => { if (busy || efforts.length === 0) setResetTipOn(false); }, [busy, efforts.length]);
|
|
640
|
+
useEffect(() => {
|
|
641
|
+
if (!open) { seenIndexRef.current = null; return; }
|
|
642
|
+
if (seenIndexRef.current === null) { seenIndexRef.current = index; return; }
|
|
643
|
+
const arrived = index === efforts.length - 1 && seenIndexRef.current !== index;
|
|
644
|
+
seenIndexRef.current = index;
|
|
645
|
+
if (arrived) setBloom(n => n + 1);
|
|
646
|
+
}, [index, open, efforts.length]);
|
|
647
|
+
|
|
648
|
+
/* 统一清掉峰值提示的定时器:既 cancel 掉还没触发的,也把数组清空,
|
|
649
|
+
* 避免失效 id 无谓地累积(clearTimeout 对旧 id 是 no-op,但不整洁)。 */
|
|
650
|
+
function clearTimers() {
|
|
651
|
+
timersRef.current.forEach(clearTimeout);
|
|
652
|
+
timersRef.current = [];
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/* 进入峰值时的特效:先显示警告,随后落定 */
|
|
656
|
+
function enterPeak() {
|
|
657
|
+
clearTimers();
|
|
658
|
+
setMessage(true);
|
|
659
|
+
timersRef.current = [setTimeout(() => { if (mountedRef.current) setMessage(false); }, 1800)];
|
|
660
|
+
}
|
|
661
|
+
function showEffort(id) {
|
|
662
|
+
const before = previewRef.current ?? savedEffort;
|
|
663
|
+
previewRef.current = id;
|
|
664
|
+
setPreview(id);
|
|
665
|
+
if (U.isPeakId(id) && !U.isPeakId(before)) enterPeak();
|
|
666
|
+
else if (!U.isPeakId(id)) {
|
|
667
|
+
timersRef.current.forEach(clearTimeout);
|
|
668
|
+
setMessage(false);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/* 清掉预览值:提交结束/取消后回到“已保存档位”的显示。 */
|
|
673
|
+
function clearPreview() {
|
|
674
|
+
previewRef.current = null;
|
|
675
|
+
setPreview(null);
|
|
676
|
+
}
|
|
677
|
+
/* 取消手势时连峰值提示一起回滚。 */
|
|
678
|
+
function cancelPreview() {
|
|
679
|
+
clearPreview();
|
|
680
|
+
timersRef.current.forEach(clearTimeout);
|
|
681
|
+
setMessage(false);
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
function chooseEffort(id) {
|
|
685
|
+
if (!selection || disabled || !efforts.some(e => e.id === id)) return;
|
|
686
|
+
showEffort(id);
|
|
687
|
+
if (id !== savedEffort) void commit({ ...selection, reasoningEffort: id });
|
|
688
|
+
else clearPreview();
|
|
689
|
+
}
|
|
690
|
+
function defaultStopId() {
|
|
691
|
+
if (!efforts.length) return undefined;
|
|
692
|
+
const declared = model?.reasoning?.defaultEffort;
|
|
693
|
+
if (declared !== undefined && efforts.some(e => e.id === declared)) return declared;
|
|
694
|
+
return efforts[Math.floor(efforts.length / 2)].id;
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
function resetToDefault() {
|
|
698
|
+
if (disabled) return;
|
|
699
|
+
setFast(false);
|
|
700
|
+
const id = defaultStopId();
|
|
701
|
+
if (id === undefined) return; // 无处可重置(没有任何档位)
|
|
702
|
+
chooseEffort(id);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
function chooseModel(providerId, nextModel) {
|
|
706
|
+
if (disabled || busyRef.current) return;
|
|
707
|
+
const same = selection?.provider === providerId && selection?.model === nextModel.id;
|
|
708
|
+
/* 切换时取中间档(参考规则):floor(n/2)。没有声明档位的模型得到
|
|
709
|
+
* 的载荷完全不含 `reasoningEffort` 键——缺失表示“无意见”,这与
|
|
710
|
+
* 显式 null 不同。 */
|
|
711
|
+
const levels = U.sortEfforts(nextModel.reasoning?.efforts);
|
|
712
|
+
const mid = levels.length ? levels[Math.floor(levels.length / 2)].id : undefined;
|
|
713
|
+
const id = same ? savedEffort : mid;
|
|
714
|
+
setPane('effort');
|
|
715
|
+
setView(1);
|
|
716
|
+
setSubProvider(null);
|
|
717
|
+
setQuery('');
|
|
718
|
+
timersRef.current.forEach(clearTimeout);
|
|
719
|
+
setMessage(false);
|
|
720
|
+
setPreview(null);
|
|
721
|
+
previewRef.current = null;
|
|
722
|
+
void commit({
|
|
723
|
+
provider: providerId, model: nextModel.id,
|
|
724
|
+
...(id === undefined ? {} : { reasoningEffort: id })
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
function close() {
|
|
729
|
+
rail.cancelDrag();
|
|
730
|
+
setOpen(false);
|
|
731
|
+
setQuery('');
|
|
732
|
+
setPane('effort');
|
|
733
|
+
setView(1);
|
|
734
|
+
setSubProvider(null);
|
|
735
|
+
setHighlight(0);
|
|
736
|
+
triggerRef.current?.focus({ preventScroll: true });
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
/* ---------- 外部点击 + Escape(监听器由 usePopoverLayout 持有) ---------- */
|
|
740
|
+
function handleOutside(event) {
|
|
741
|
+
if (!rootRef.current?.contains(event.target)) close();
|
|
742
|
+
}
|
|
743
|
+
function handleEscape(event) {
|
|
744
|
+
event.preventDefault();
|
|
745
|
+
event.stopPropagation();
|
|
746
|
+
/* Escape 每次退回一层,与参考实现一致:
|
|
747
|
+
* 第 2 页 -> 第 1 页 -> 强度面板 -> 关闭。 */
|
|
748
|
+
if (latest.current.pane === 'models') {
|
|
749
|
+
if (latest.current.view === 2) { goBack(); }
|
|
750
|
+
else { setPane('effort'); setQuery(''); }
|
|
751
|
+
} else close();
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/* 模型页打开时聚焦搜索框 */
|
|
755
|
+
useLayoutEffect(() => {
|
|
756
|
+
if (open && pane === 'models' && searchRef.current) searchRef.current.focus({ preventScroll: true });
|
|
757
|
+
}, [open, pane]);
|
|
758
|
+
|
|
759
|
+
/* 第 2 页当前展示的提供方(第 1 页为 null)。 */
|
|
760
|
+
const activeProvider = subProvider === null
|
|
761
|
+
? null
|
|
762
|
+
: groups.find(g => g.id === subProvider) ?? null;
|
|
763
|
+
|
|
764
|
+
/* 第 2 页的行,按查询过滤。匹配规则与出厂选择器使用的
|
|
765
|
+
* 大小写不敏感有序子序列规则相同。 */
|
|
766
|
+
const providerModels = useMemo(() => {
|
|
767
|
+
if (!activeProvider) return [];
|
|
768
|
+
const q = query.trim().toLowerCase();
|
|
769
|
+
if (!q) return activeProvider.models;
|
|
770
|
+
return activeProvider.models.filter(m => {
|
|
771
|
+
const name = (m.name || m.id).toLowerCase();
|
|
772
|
+
if (name.includes(q)) return true;
|
|
773
|
+
let i = 0;
|
|
774
|
+
for (const ch of name) if (ch === q[i]) i++;
|
|
775
|
+
return i === q.length;
|
|
776
|
+
});
|
|
777
|
+
}, [activeProvider, query]);
|
|
778
|
+
|
|
779
|
+
/* 第 1 页只是提供方列表;那里没有需要过滤的内容。 */
|
|
780
|
+
const flatModels = useMemo(
|
|
781
|
+
() => (activeProvider
|
|
782
|
+
? providerModels.map(m => ({ group: activeProvider, model: m }))
|
|
783
|
+
: []),
|
|
784
|
+
[activeProvider, providerModels]
|
|
785
|
+
);
|
|
786
|
+
|
|
787
|
+
function enterProvider(gid) {
|
|
788
|
+
setSubProvider(gid);
|
|
789
|
+
setQuery('');
|
|
790
|
+
setHighlight(0);
|
|
791
|
+
setView(2);
|
|
792
|
+
}
|
|
793
|
+
function goBack() {
|
|
794
|
+
setView(1);
|
|
795
|
+
setSubProvider(null);
|
|
796
|
+
setQuery('');
|
|
797
|
+
setHighlight(0);
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
useEffect(() => { setHighlight(0); }, [query, pane, view]);
|
|
801
|
+
|
|
802
|
+
function modelKeyDown(event) {
|
|
803
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
804
|
+
event.preventDefault();
|
|
805
|
+
if (!flatModels.length) return;
|
|
806
|
+
const delta = event.key === 'ArrowDown' ? 1 : -1;
|
|
807
|
+
setHighlight(prev => (prev + delta + flatModels.length) % flatModels.length);
|
|
808
|
+
} else if (event.key === 'Enter') {
|
|
809
|
+
event.preventDefault();
|
|
810
|
+
const entry = flatModels[highlight];
|
|
811
|
+
if (entry) chooseModel(entry.group.id, entry.model);
|
|
812
|
+
} else if (event.key === 'Home') {
|
|
813
|
+
event.preventDefault(); setHighlight(0);
|
|
814
|
+
} else if (event.key === 'End') {
|
|
815
|
+
event.preventDefault(); setHighlight(flatModels.length - 1);
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/* ---------- 渲染 ---------- */
|
|
820
|
+
const currentLabel = efforts.length
|
|
821
|
+
? (U.effortLabel(efforts[index], t) || t('effort'))
|
|
822
|
+
: '';
|
|
823
|
+
const triggerLabel = U.effortLabel(efforts.find(e => e.id === savedEffort), t);
|
|
824
|
+
const status = error || (state.status === 'error' ? t('failed') : '');
|
|
825
|
+
/* N 档标签:单档时走单数模板,避免英文出现 “1 levels”。 */
|
|
826
|
+
const levelText = n => fill(t(n === 1 ? 'levelOne' : 'levels'), { count: n });
|
|
827
|
+
const ROW_H = M.rowH, MODEL_H = M.modelH, LIST_MAX_H = M.listMaxH, LIST_PAD_B = M.listPadB;
|
|
828
|
+
const HEAD_H = M.headH, SEARCH_H = M.searchH, EMPTY_H = M.emptyH, SEC_H = M.secH, PAD = M.padH;
|
|
829
|
+
const noticeRows = (state.failures?.length ?? 0);
|
|
830
|
+
const noticeH = noticeRows > 0
|
|
831
|
+
? Math.min(noticeRows, 4) * 30 + (groups.length === 0 ? 8 : 4)
|
|
832
|
+
: (groups.length === 0 ? EMPTY_H : 0);
|
|
833
|
+
|
|
834
|
+
let menuHeight;
|
|
835
|
+
if (view !== 2 || !activeProvider) {
|
|
836
|
+
menuHeight = PAD + Math.max(44, SEC_H + groups.length * ROW_H + noticeH);
|
|
837
|
+
} else {
|
|
838
|
+
const n = providerModels.length;
|
|
839
|
+
const listH = n === 0
|
|
840
|
+
? EMPTY_H + LIST_PAD_B
|
|
841
|
+
: Math.min(n * MODEL_H + LIST_PAD_B, LIST_MAX_H);
|
|
842
|
+
menuHeight = PAD + Math.max(44, HEAD_H + SEARCH_H + listH);
|
|
843
|
+
}
|
|
844
|
+
const roomAbove = room > 0
|
|
845
|
+
? room
|
|
846
|
+
: (typeof window === 'undefined' ? Infinity : Math.max(120, window.innerHeight - 52));
|
|
847
|
+
const cappedHeight = Math.min(menuHeight, roomAbove);
|
|
848
|
+
|
|
849
|
+
/* 错误提示行:原先用 bottom 锚定,会在「无推理档位」时压住
|
|
850
|
+
* .cmes-noeffort 文案(那块和滑轨同样占据 top:65 起、line-height 26
|
|
851
|
+
* 的带状区域,而 status 落在 bottom:6 正好与其重叠)。改为 top 锚定,
|
|
852
|
+
* 落在顶部 ⚡/重置 按钮之间的空档(y 8..50 的 modelLink 那一行),
|
|
853
|
+
* 那里无论有无档位、有无 team 行都恒为空,不会再与任何内容打架。 */
|
|
854
|
+
const statusTop = 8;
|
|
855
|
+
|
|
856
|
+
const classes = [
|
|
857
|
+
`${P}-popover`,
|
|
858
|
+
!open ? 'closed' : '',
|
|
859
|
+
peak ? 'peak' : '',
|
|
860
|
+
fast ? 'fast-mode' : '',
|
|
861
|
+
message ? 'message' : ''
|
|
862
|
+
].filter(Boolean).join(' ');
|
|
863
|
+
|
|
864
|
+
/* --- 模型页:第 1 页(提供方)+ 第 2 页(该提供方的模型) --- */
|
|
865
|
+
const providerNodes = groups.map(g => {
|
|
866
|
+
const isCurrent = selection?.provider === g.id;
|
|
867
|
+
return h('button', {
|
|
868
|
+
key: `p-${g.id}`,
|
|
869
|
+
className: `${P}-row`,
|
|
870
|
+
type: 'button',
|
|
871
|
+
'aria-expanded': view === 2 && subProvider === g.id,
|
|
872
|
+
onClick: () => enterProvider(g.id)
|
|
873
|
+
},
|
|
874
|
+
h('span', { className: `${P}-rowName` }, g.name),
|
|
875
|
+
h('span', { className: `${P}-rowDot`, hidden: !isCurrent, 'aria-hidden': true }),
|
|
876
|
+
icon('chevron', `${P}-rowChev`));
|
|
877
|
+
});
|
|
878
|
+
|
|
879
|
+
/* 失败会被展示出来,而不是被吞掉:接管座位之后,出厂选择器的按
|
|
880
|
+
* 提供方失败列表也归我们报告。 */
|
|
881
|
+
const noticeNodes = [];
|
|
882
|
+
if (noticeRows > 0) {
|
|
883
|
+
(state.failures || []).slice(0, 4).forEach((f, i) => {
|
|
884
|
+
noticeNodes.push(h('div', {
|
|
885
|
+
key: `f-${i}`, className: `${P}-failure`,
|
|
886
|
+
title: f.message
|
|
887
|
+
}, fill(t('loadFailed'), { name: f.name, message: f.message })));
|
|
888
|
+
});
|
|
889
|
+
} else if (groups.length === 0) {
|
|
890
|
+
noticeNodes.push(h('div', { key: 'empty', className: `${P}-empty` },
|
|
891
|
+
state.status === 'loading' ? t('loading') : t('empty')));
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
const modelNodes = providerModels.map(m => {
|
|
895
|
+
const entry = flatModels.findIndex(x => x.model.id === m.id);
|
|
896
|
+
const selected = selection?.provider === activeProvider?.id && selection?.model === m.id;
|
|
897
|
+
const levelCount = U.sortEfforts(m.reasoning?.efforts).length;
|
|
898
|
+
return h('button', {
|
|
899
|
+
key: `m-${m.id}`,
|
|
900
|
+
className: `${P}-model${selected ? ' sel' : ''}${levelCount ? '' : ' na'}${entry === highlight ? ' hovered' : ''}`,
|
|
901
|
+
type: 'button',
|
|
902
|
+
role: 'menuitemradio',
|
|
903
|
+
'aria-checked': selected,
|
|
904
|
+
disabled: busy,
|
|
905
|
+
title: levelCount ? levelText(levelCount) : t('noLevels'),
|
|
906
|
+
onMouseEnter: () => setHighlight(entry),
|
|
907
|
+
onClick: () => chooseModel(activeProvider.id, m)
|
|
908
|
+
},
|
|
909
|
+
h('span', { className: `${P}-modelName` },
|
|
910
|
+
h('span', null, m.name || m.id),
|
|
911
|
+
h('span', { className: `${P}-cnt` }, levelCount ? levelText(levelCount) : t('noLevelsShort'))),
|
|
912
|
+
icon('check', `${P}-ck`));
|
|
913
|
+
});
|
|
914
|
+
if (activeProvider && !modelNodes.length) {
|
|
915
|
+
modelNodes.push(h('div', { key: 'empty', className: `${P}-empty` }, t('noMatch')));
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
const modelsView = h('div', {
|
|
919
|
+
className: `${P}-pane ${P}-models${pane !== 'models' ? ' hidden' : ''}`,
|
|
920
|
+
role: 'menu', 'aria-label': t('model'),
|
|
921
|
+
inert: pane !== 'models' ? '' : undefined,
|
|
922
|
+
onKeyDown: modelKeyDown
|
|
923
|
+
},
|
|
924
|
+
h('div', {
|
|
925
|
+
className: `${P}-viewport${view === 2 ? ' view2' : ''}`
|
|
926
|
+
},
|
|
927
|
+
/* 第 1 页 —— 提供方(自有滚动容器,因此长列表可达) */
|
|
928
|
+
h('div', { className: `${P}-page slot1` },
|
|
929
|
+
h('div', { className: `${P}-sec` }, t('model')),
|
|
930
|
+
h('div', { className: `${P}-rows` }, ...providerNodes, ...noticeNodes)),
|
|
931
|
+
|
|
932
|
+
/* 第 2 页 —— 所选提供方的模型 */
|
|
933
|
+
h('div', {
|
|
934
|
+
className: `${P}-page slot2`,
|
|
935
|
+
inert: view !== 2 ? '' : undefined
|
|
936
|
+
},
|
|
937
|
+
h('div', { className: `${P}-head` },
|
|
938
|
+
h('button', {
|
|
939
|
+
type: 'button', className: `${P}-back`,
|
|
940
|
+
'aria-label': t('back'), title: t('back'),
|
|
941
|
+
onClick: goBack
|
|
942
|
+
}, icon('chevron', `${P}-iconSmall`)),
|
|
943
|
+
h('span', { className: `${P}-title` },
|
|
944
|
+
activeProvider ? activeProvider.name : '')),
|
|
945
|
+
h('label', { className: `${P}-search` },
|
|
946
|
+
icon('search', `${P}-iconSmall`),
|
|
947
|
+
h('input', {
|
|
948
|
+
ref: searchRef, type: 'text', value: query,
|
|
949
|
+
placeholder: t('search'), 'aria-label': t('search'),
|
|
950
|
+
spellCheck: false,
|
|
951
|
+
onChange: e => setQuery(e.target.value)
|
|
952
|
+
})),
|
|
953
|
+
h('div', { className: `${P}-list` }, ...modelNodes))));
|
|
954
|
+
|
|
955
|
+
/* --- 强度面板 --- */
|
|
956
|
+
const effortView = h('div', {
|
|
957
|
+
className: `${P}-pane ${P}-main${pane !== 'effort' ? ' hidden' : ''}`,
|
|
958
|
+
inert: pane !== 'effort' ? '' : undefined,
|
|
959
|
+
/* Agent Teams 行需要多一个行高;上面的弹出面板高度使用同一份
|
|
960
|
+
* 度量(M),因此两者一起增长 */
|
|
961
|
+
style: { [`--${P}-main-h`]: `${team.supported ? M.teamPaneH : M.effortPaneH}px` }
|
|
962
|
+
},
|
|
963
|
+
h('button', {
|
|
964
|
+
className: `${P}-tool ${P}-fast${fast ? ' active' : ''}`,
|
|
965
|
+
ref: fastBtnRef,
|
|
966
|
+
'aria-label': t('fast'), 'aria-pressed': fast,
|
|
967
|
+
'aria-describedby': `${P}-fast-tip`,
|
|
968
|
+
/* tooltip 绝不能作为此按钮的子节点:弹出面板的 backdrop-filter
|
|
969
|
+
* 会使它自身成为固定定位后代的包含块,从而裁掉该层。它在根节点
|
|
970
|
+
* 渲染(见返回值),并通过 `tipOn` 显示。 */
|
|
971
|
+
onPointerEnter: () => setTipOn(true),
|
|
972
|
+
onPointerLeave: () => setTipOn(false),
|
|
973
|
+
onFocus: () => setTipOn(true),
|
|
974
|
+
onBlur: () => setTipOn(false),
|
|
975
|
+
onClick: () => setFast(v => !v)
|
|
976
|
+
},
|
|
977
|
+
h('svg', { viewBox: '0 0 24 24', 'aria-hidden': true, style: { width: 15, height: 17, fill: fast ? 'currentColor' : 'none', stroke: 'currentColor', strokeWidth: fast ? 0.7 : 1.5, strokeLinejoin: 'round' } },
|
|
978
|
+
h('path', { d: ICON.bolt }))),
|
|
979
|
+
|
|
980
|
+
h('button', {
|
|
981
|
+
className: `${P}-modelLink`, onClick: () => setPane('models'),
|
|
982
|
+
'aria-label': t('model'), title: modelName
|
|
983
|
+
},
|
|
984
|
+
h('span', { className: `${P}-heading` }, currentLabel || t('choose'), icon('chevron', `${P}-iconSmall`)),
|
|
985
|
+
h('span', { className: `${P}-modelLinkName` }, modelName)),
|
|
986
|
+
|
|
987
|
+
h('div', { className: `${P}-warning` }, h('span', null, t('warning'))),
|
|
988
|
+
|
|
989
|
+
h('button', {
|
|
990
|
+
className: `${P}-tool ${P}-reset`,
|
|
991
|
+
ref: resetRef,
|
|
992
|
+
/* 只有在确实没有可重置内容时才禁用:旧的 `!model?.reasoning`
|
|
993
|
+
* 判断让按钮对常见的“有强度项但未声明默认值”提供方保持可用却
|
|
994
|
+
* 无效。 */
|
|
995
|
+
disabled: busy || efforts.length === 0,
|
|
996
|
+
'aria-label': t('reset'),
|
|
997
|
+
'aria-describedby': `${P}-reset-tip`,
|
|
998
|
+
/* 悬停提示改用与 ⚡ 相同的浮层卡片,而不是浏览器原生 title
|
|
999
|
+
* (原生提示样式不可控,与面板的玻璃材质也不协调)。 */
|
|
1000
|
+
onPointerEnter: () => setResetTipOn(true),
|
|
1001
|
+
onPointerLeave: () => setResetTipOn(false),
|
|
1002
|
+
onFocus: () => setResetTipOn(true),
|
|
1003
|
+
onBlur: () => setResetTipOn(false),
|
|
1004
|
+
onClick: resetToDefault
|
|
1005
|
+
}, icon('reset')),
|
|
1006
|
+
|
|
1007
|
+
efforts.length
|
|
1008
|
+
? h('div', {
|
|
1009
|
+
className: `${P}-rail${rail.dragging ? ' dragging' : ''}`,
|
|
1010
|
+
ref: railRef,
|
|
1011
|
+
/* 逐档着色(问题 3):填充、未填充轨道色调、标题墨色和圆点
|
|
1012
|
+
* 都由当前档位派生,因此每个档位都呈现自己的颜色,而不是只有
|
|
1013
|
+
* Ultra 特殊。 */
|
|
1014
|
+
style: {
|
|
1015
|
+
[`--${P}-fill`]: U.fillFor(displayed, dark),
|
|
1016
|
+
[`--${P}-track`]: U.trackFor(U.fillFor(displayed, dark), dark),
|
|
1017
|
+
[`--${P}-ink`]: U.inkFor(displayed, dark),
|
|
1018
|
+
[`--${P}-dot`]: 'var(--dsw-alias-label-caption,#b6b6b8)',
|
|
1019
|
+
/* 绽放粒子是当前档位颜色向白色提亮——对 Ultra 而言正好落在
|
|
1020
|
+
* 测量得到的 #ceb0fa 左右紫色,无需第二张硬编码表 */
|
|
1021
|
+
[`--${P}-mote`]: U.mixHex(U.fillFor(displayed, dark), '#ffffff', dark ? 0.30 : 0.42),
|
|
1022
|
+
/* 一个无量纲比例同时驱动圆钮和填充 */
|
|
1023
|
+
[`--${P}-pos`]: fraction
|
|
1024
|
+
},
|
|
1025
|
+
role: 'slider', tabIndex: 0,
|
|
1026
|
+
'aria-label': t('effort'),
|
|
1027
|
+
'aria-valuemin': 0, 'aria-valuemax': efforts.length - 1,
|
|
1028
|
+
'aria-valuenow': index, 'aria-valuetext': currentLabel,
|
|
1029
|
+
'aria-disabled': busy || disabled,
|
|
1030
|
+
onPointerDown: rail.pointerDown, onPointerMove: rail.pointerMove,
|
|
1031
|
+
onPointerUp: rail.finish, onPointerCancel: rail.cancelDrag,
|
|
1032
|
+
onLostPointerCapture: () => { /* 丢失捕获不算取消 */ },
|
|
1033
|
+
onPointerLeave: () => rail.setHovered(-1),
|
|
1034
|
+
onKeyDown: rail.keyDown
|
|
1035
|
+
},
|
|
1036
|
+
h('div', { className: `${P}-fill` }),
|
|
1037
|
+
...efforts.map((effort, i) => h('i', {
|
|
1038
|
+
key: effort.id,
|
|
1039
|
+
className: `${P}-dot${i <= index ? ' passed' : ''}${rail.hovered === i ? ' hovered' : ''}`,
|
|
1040
|
+
style: { [`--${P}-tick`]: efforts.length > 1 ? i / (efforts.length - 1) : 0 }
|
|
1041
|
+
})),
|
|
1042
|
+
h('div', { className: `${P}-sparks` },
|
|
1043
|
+
...sparkles.map(s => h('i', {
|
|
1044
|
+
key: s.key, className: `${P}-sp`,
|
|
1045
|
+
style: { top: s.top, [`--b`]: s.b, [`--dur`]: s.dur, [`--delay`]: s.delay }
|
|
1046
|
+
}))),
|
|
1047
|
+
/* 到达绽放:以到达计数器为 key,因此挂载新 key 会让每颗
|
|
1048
|
+
* 粒子的动画从 0% 重新开始 */
|
|
1049
|
+
bloom
|
|
1050
|
+
? h('div', {
|
|
1051
|
+
key: `bloom-${bloom}`, className: `${P}-bloom`, 'aria-hidden': true
|
|
1052
|
+
},
|
|
1053
|
+
h('i', { className: `${P}-halo` }),
|
|
1054
|
+
...bloomMotes.map(m => h('i', {
|
|
1055
|
+
key: m.key, className: `${P}-bp`,
|
|
1056
|
+
style: {
|
|
1057
|
+
[`--x0`]: m.x0, [`--y0`]: m.y0,
|
|
1058
|
+
[`--x1`]: m.x1, [`--y1`]: m.y1,
|
|
1059
|
+
[`--sz`]: m.sz, [`--bmax`]: m.bmax,
|
|
1060
|
+
[`--bd`]: m.bd, [`--bdel`]: m.bdel
|
|
1061
|
+
}
|
|
1062
|
+
})))
|
|
1063
|
+
: null,
|
|
1064
|
+
h('div', { className: `${P}-knob` }))
|
|
1065
|
+
: h('div', { className: `${P}-noeffort` }, t('unsupported')),
|
|
1066
|
+
|
|
1067
|
+
/* Agent Teams:DSH 放在模型菜单根面板里的开关,在滑杆下方重建。
|
|
1068
|
+
* 不具备该能力的宿主上完全隐藏,因此面板在那些宿主上保持原来的
|
|
1069
|
+
* 104px 形状。 */
|
|
1070
|
+
team.supported
|
|
1071
|
+
? h('div', { className: `${P}-team` },
|
|
1072
|
+
h('span', { className: `${P}-teamLabel` },
|
|
1073
|
+
h('span', null, t('team')),
|
|
1074
|
+
h('button', {
|
|
1075
|
+
type: 'button',
|
|
1076
|
+
className: `${P}-teamInfo`,
|
|
1077
|
+
ref: teamInfoBtnRef,
|
|
1078
|
+
/* 可访问名只用简短的「Agent Teams」;整句说明交给
|
|
1079
|
+
* aria-describedby 指向的提示卡,避免把长句塞进 label。 */
|
|
1080
|
+
'aria-label': t('team'),
|
|
1081
|
+
'aria-describedby': `${P}-team-tip`,
|
|
1082
|
+
onPointerEnter: () => setTeamTipOn(true),
|
|
1083
|
+
onPointerLeave: () => setTeamTipOn(false),
|
|
1084
|
+
onFocus: () => setTeamTipOn(true),
|
|
1085
|
+
onBlur: () => setTeamTipOn(false),
|
|
1086
|
+
/* 提示卡的显隐完全交给 hover / focus:这里若再切一刀,
|
|
1087
|
+
* 点击会把 hover 刚点亮的提示卡反过来关掉(指针还悬在上面,
|
|
1088
|
+
* 不会重新触发 enter),表现为提示卡在光标下凭空消失。
|
|
1089
|
+
* 点击只拦住冒泡,避免把整个弹层关掉。 */
|
|
1090
|
+
onClick: event => { event.preventDefault(); event.stopPropagation(); }
|
|
1091
|
+
}, icon('info', `${P}-teamInfoIcon`))),
|
|
1092
|
+
h('button', {
|
|
1093
|
+
type: 'button', role: 'switch',
|
|
1094
|
+
className: `${P}-teamSwitch`,
|
|
1095
|
+
'aria-checked': team.enabled ? 'true' : 'false',
|
|
1096
|
+
'aria-label': t('team'),
|
|
1097
|
+
disabled: Boolean(locked || team.pending),
|
|
1098
|
+
onClick: () => team.toggle(!team.enabled)
|
|
1099
|
+
}))
|
|
1100
|
+
: null);
|
|
1101
|
+
|
|
1102
|
+
return h('div', { className: `${P}-root`, ref: rootRef },
|
|
1103
|
+
h('style', null, css),
|
|
1104
|
+
h('div', { ref: probeRef, className: `${P}-probe`, 'aria-hidden': true }),
|
|
1105
|
+
|
|
1106
|
+
h('button', {
|
|
1107
|
+
className: `${P}-trigger${U.isPeakId(savedEffort) ? ' peak' : ''}${fast ? ' fast' : ''}`,
|
|
1108
|
+
ref: triggerRef, disabled,
|
|
1109
|
+
'aria-label': `${modelName} ${triggerLabel}`.trim(),
|
|
1110
|
+
'aria-expanded': open, 'aria-haspopup': 'dialog', 'aria-busy': busy,
|
|
1111
|
+
style: { [`--${P}-ink`]: U.inkFor(savedEffort, dark) },
|
|
1112
|
+
onClick: () => {
|
|
1113
|
+
if (open) close();
|
|
1114
|
+
else {
|
|
1115
|
+
/* 打开前先量一次可用高度,首帧就是正确值,不会高度弹跳。 */
|
|
1116
|
+
primeRoom();
|
|
1117
|
+
setPane(model ? 'effort' : 'models');
|
|
1118
|
+
setOpen(true);
|
|
1119
|
+
load();
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
},
|
|
1123
|
+
fast ? h('svg', { className: `${P}-bolt`, viewBox: '0 0 24 24', 'aria-hidden': true }, h('path', { d: ICON.bolt })) : null,
|
|
1124
|
+
h('span', { className: `${P}-compact` }, icon('model')),
|
|
1125
|
+
h('span', { className: `${P}-caption` },
|
|
1126
|
+
modelName),
|
|
1127
|
+
triggerLabel
|
|
1128
|
+
? h('span', { className: `${P}-triggerEffort` }, triggerLabel)
|
|
1129
|
+
: null,
|
|
1130
|
+
icon('down', `${P}-chev`)),
|
|
1131
|
+
|
|
1132
|
+
h('section', {
|
|
1133
|
+
className: classes, ref: popupRef, role: 'dialog',
|
|
1134
|
+
'aria-label': pane === 'models' ? t('model') : t('effort'),
|
|
1135
|
+
'aria-hidden': open ? undefined : 'true', inert: !open ? '' : undefined,
|
|
1136
|
+
onKeyDown: open ? event => trapFocus(popupRef.current, event) : undefined,
|
|
1137
|
+
style: {
|
|
1138
|
+
[`--${P}-height`]: `${pane === 'models' ? cappedHeight : (team.supported ? M.teamPaneH : M.effortPaneH)}px`,
|
|
1139
|
+
[`--${P}-ink`]: U.inkFor(displayed, dark)
|
|
1140
|
+
}
|
|
1141
|
+
},
|
|
1142
|
+
modelsView,
|
|
1143
|
+
effortView,
|
|
1144
|
+
busy ? h('div', { className: `${P}-sr`, role: 'status' }, t('busy')) : null,
|
|
1145
|
+
status ? h('div', { className: `${P}-status`, role: 'alert', style: { position: 'absolute', left: 12, right: 12, top: statusTop, color: 'var(--dsw-alias-state-error-primary)' } },
|
|
1146
|
+
h('span', null, status)) : null),
|
|
1147
|
+
h('span', {
|
|
1148
|
+
role: 'tooltip', id: `${P}-reset-tip`,
|
|
1149
|
+
className: `${P}-tip ${P}-resetTip${resetTipShown ? ' show' : ''}`,
|
|
1150
|
+
ref: resetTipRef, 'aria-hidden': resetTipShown ? undefined : 'true'
|
|
1151
|
+
},
|
|
1152
|
+
h('span', { className: `${P}-tipTitle` }, t('reset')),
|
|
1153
|
+
h('span', null, t('resetDesc'))),
|
|
1154
|
+
h('span', {
|
|
1155
|
+
role: 'tooltip', id: `${P}-fast-tip`,
|
|
1156
|
+
className: `${P}-tip${tipShown ? ' show' : ''}`,
|
|
1157
|
+
ref: tipRef, 'aria-hidden': tipShown ? undefined : 'true'
|
|
1158
|
+
},
|
|
1159
|
+
h('span', { className: `${P}-tipTitle` }, t('fast')),
|
|
1160
|
+
h('span', null, t('usage')),
|
|
1161
|
+
h('span', { className: `${P}-sr` }, t('visual'))),
|
|
1162
|
+
h('span', {
|
|
1163
|
+
role: 'tooltip', id: `${P}-team-tip`,
|
|
1164
|
+
className: `${P}-tip ${P}-teamTip${teamTipShown ? ' show' : ''}`,
|
|
1165
|
+
ref: teamTipRef, 'aria-hidden': teamTipShown ? undefined : 'true'
|
|
1166
|
+
},
|
|
1167
|
+
h('span', { className: `${P}-tipTitle` }, t('team')),
|
|
1168
|
+
h('span', null, t('teamDesc'))));
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
/* 懒加载样式表分块(client.styles.js)。React.lazy + Suspense 让座位
|
|
1172
|
+
* 在 CSS 到达前不绘制,因此绝不会出现无样式渲染——与终端插件加载自有
|
|
1173
|
+
* 分块采用同一模式。 */
|
|
1174
|
+
const SelectorBody = lazy(async () => {
|
|
1175
|
+
/* 两个分块按序加载:样式表负责 CSS 与布局度量,effort 分块负责强度
|
|
1176
|
+
* 阶梯/调色板/滑轨映射等纯函数(可脱离 React 单测)。
|
|
1177
|
+
* 分块加载失败时退回空组件,保持插件的 fail-open 约定:宁可不渲染,
|
|
1178
|
+
* 也不让输入框因一个懒加载错误而崩掉。 */
|
|
1179
|
+
try {
|
|
1180
|
+
const styles = await require.async('./client.styles.js');
|
|
1181
|
+
const effort = await require.async('./client.effort.js');
|
|
1182
|
+
const css = styles.css(P, styles.METRICS);
|
|
1183
|
+
return {
|
|
1184
|
+
default: props => h(Selector, {
|
|
1185
|
+
...props, css, M: styles.METRICS, U: effort
|
|
1186
|
+
})
|
|
1187
|
+
};
|
|
1188
|
+
} catch (cause) {
|
|
1189
|
+
console.warn(`${NS}: failed to load client chunks`, cause);
|
|
1190
|
+
return { default: () => null };
|
|
1191
|
+
}
|
|
1192
|
+
});
|
|
1193
|
+
function SelectorLazy(props) {
|
|
1194
|
+
return h(Suspense, { fallback: null }, h(SelectorBody, props));
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
/* ================================================================== *
|
|
1198
|
+
* 插件
|
|
1199
|
+
* ================================================================== */
|
|
1200
|
+
return {
|
|
1201
|
+
inject: ['slots', 'modelDirectories', 'sessions', 'locale', 'remote', 'remote.session'],
|
|
1202
|
+
apply(ctx) {
|
|
1203
|
+
ctx.effect(() => ctx.locale.register(NS, { zh, en }));
|
|
1204
|
+
let setTeamEnabled = null;
|
|
1205
|
+
try {
|
|
1206
|
+
if (typeof ctx.inject === 'function') {
|
|
1207
|
+
ctx.inject(['remote', 'remote.agentTeams'], (scope) => {
|
|
1208
|
+
setTeamEnabled = (sessionId, enabled) => scope.remote.agentTeams.setEnabled(sessionId, enabled);
|
|
1209
|
+
});
|
|
1210
|
+
}
|
|
1211
|
+
} catch {
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
ctx.slots.inject('conversation.input.model', () => ctx.slots.register({
|
|
1215
|
+
name: 'conversation.input.model',
|
|
1216
|
+
locale: NS,
|
|
1217
|
+
priority: -1,
|
|
1218
|
+
inject(sessionId) {
|
|
1219
|
+
const directory = ctx.modelDirectories.directoryFor(sessionId);
|
|
1220
|
+
const available = ctx.sessions.subagentAddress(sessionId) === undefined;
|
|
1221
|
+
return {
|
|
1222
|
+
available,
|
|
1223
|
+
sessionId,
|
|
1224
|
+
directory: directory.store,
|
|
1225
|
+
load: () => { if (available) directory.load().catch(() => {}); },
|
|
1226
|
+
select: sel => (available ? directory.select(sel) : Promise.resolve(undefined)),
|
|
1227
|
+
setTeamMode: enabled => (setTeamEnabled
|
|
1228
|
+
? setTeamEnabled(sessionId, enabled)
|
|
1229
|
+
/* 不携带报文:组件会回退到本地化的 `teamFailed` 文案 */
|
|
1230
|
+
: Promise.resolve({ ok: false }))
|
|
1231
|
+
};
|
|
1232
|
+
}
|
|
1233
|
+
}, SelectorLazy));
|
|
1234
|
+
}
|
|
1235
|
+
};
|
|
1236
|
+
}
|
|
1237
|
+
});
|