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