dsh-plugins-scope 0.0.0-stage → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/client.js ADDED
@@ -0,0 +1,693 @@
1
+ window.__ModuleLoader__.load({
2
+ id: 'dsh-plugins-scope',
3
+ factory(require) {
4
+ const React = require('react');
5
+ const h = React.createElement;
6
+ const { useState, useEffect, useCallback } = React;
7
+
8
+ const API = '/plugin-scopes/api';
9
+
10
+ async function call(payload) {
11
+ const response = await fetch(API, {
12
+ method: 'POST',
13
+ headers: { 'content-type': 'application/json' },
14
+ body: JSON.stringify(payload),
15
+ });
16
+ const body = await response.json();
17
+ if (!body.ok) throw new Error(body.error ?? 'scope api failed');
18
+ return body.data;
19
+ }
20
+
21
+ const LABEL = { on: '开', off: '关' };
22
+ const COLOR = {
23
+ on: 'var(--dsw-alias-state-success-primary)',
24
+ off: 'var(--dsw-alias-state-error-primary)',
25
+ };
26
+
27
+ const theme = {
28
+ border: '1px solid var(--dsw-alias-border-l1)',
29
+ strongBorder: '1px solid var(--dsw-alias-border-l2)',
30
+ muted: 'var(--dsw-alias-label-secondary)',
31
+ faint: 'var(--dsw-alias-label-tertiary)',
32
+ text: 'var(--dsw-alias-label-primary)',
33
+ surface: 'var(--dsw-alias-bg-layer-1)',
34
+ raised: 'var(--dsw-alias-bg-layer-2)',
35
+ overlay: 'var(--dsw-alias-bg-overlay)',
36
+ warn: 'var(--dsw-alias-state-warn-primary)',
37
+ brand: 'var(--dsw-alias-brand-primary)',
38
+ };
39
+
40
+ function sourceOf(row, level) {
41
+ if (!row.scoped) return '全局';
42
+ if (level === 'session' && (row.session === 'on' || row.session === 'off')) return '本会话';
43
+ if (row.workspace === 'on' || row.workspace === 'off') return '工作区';
44
+ return '全局';
45
+ }
46
+
47
+ function capabilityText(row) {
48
+ const parts = [];
49
+ if (row.tools?.length) parts.push(`${row.tools.length} 个工具`);
50
+ if (row.promptSections?.length) parts.push(`${row.promptSections.length} 段提示词`);
51
+ if (row.promptContexts?.length) parts.push(`${row.promptContexts.length} 段上下文`);
52
+ if (parts.length) return parts.join(' + ');
53
+ if (row.scoped && row.category === 'agent') return '请求拦截';
54
+ return row.scoped ? '暂无可隐藏能力' : '无 agent 能力';
55
+ }
56
+
57
+ function reasonText(row) {
58
+ if (row.reason) return row.reason;
59
+ return row.scoped ? '按注册能力判定' : '仅提供界面或后台服务';
60
+ }
61
+
62
+ function ScopeBadge({ row }) {
63
+ const scoped = !!row.scoped;
64
+ return h('span', {
65
+ style: {
66
+ flex: '0 0 auto',
67
+ fontSize: '10px',
68
+ lineHeight: '16px',
69
+ padding: '0 6px',
70
+ borderRadius: '999px',
71
+ border: scoped
72
+ ? '1px solid color-mix(in srgb, var(--dsw-alias-brand-primary) 55%, transparent)'
73
+ : theme.border,
74
+ color: scoped ? theme.brand : theme.faint,
75
+ background: scoped
76
+ ? 'color-mix(in srgb, var(--dsw-alias-brand-primary) 12%, transparent)'
77
+ : 'transparent',
78
+ },
79
+ }, scoped ? '会话级' : '仅全局');
80
+ }
81
+
82
+ function StatusDot({ value }) {
83
+ const on = value === 'on';
84
+ return h('span', {
85
+ style: {
86
+ display: 'inline-block', width: '7px', height: '7px', borderRadius: '50%',
87
+ background: on ? COLOR.on : COLOR.off, flex: '0 0 auto',
88
+ },
89
+ });
90
+ }
91
+
92
+ function localValue(row, level) {
93
+ if (level === 'session') return row.session;
94
+ if (level === 'workspace') return row.workspace;
95
+ return row.global;
96
+ }
97
+
98
+ function isExplicit(value) {
99
+ return value === 'on' || value === 'off';
100
+ }
101
+
102
+ function parentName(level) {
103
+ return level === 'session' ? '工作区' : '全局';
104
+ }
105
+
106
+ function parentValue(row, level) {
107
+ return level === 'session' ? row.parent : row.global;
108
+ }
109
+
110
+ // Copy of the shipped two-state switch: a 36x20 pill with a 14px knob.
111
+ function Switch({ checked, disabled, label, hint, onChange }) {
112
+ return h('button', {
113
+ type: 'button',
114
+ role: 'switch',
115
+ 'aria-checked': checked ? 'true' : 'false',
116
+ 'aria-label': label,
117
+ title: hint || label,
118
+ disabled: !!disabled,
119
+ onClick: () => onChange(!checked),
120
+ style: {
121
+ width: '36px',
122
+ height: '20px',
123
+ flex: 'none',
124
+ padding: 0,
125
+ border: checked ? '1px solid transparent' : '1px solid var(--dsw-alias-border-l2)',
126
+ borderRadius: '999px',
127
+ background: checked ? 'var(--dsw-alias-brand-primary)' : 'var(--dsw-alias-bg-layer-2)',
128
+ position: 'relative',
129
+ cursor: disabled ? 'default' : 'pointer',
130
+ opacity: disabled ? 0.45 : 1,
131
+ transition: 'background .15s ease, opacity .15s ease',
132
+ },
133
+ }, h('span', {
134
+ style: {
135
+ position: 'absolute',
136
+ top: '2px',
137
+ left: checked ? '18px' : '2px',
138
+ width: '14px',
139
+ height: '14px',
140
+ borderRadius: '50%',
141
+ background: '#fff',
142
+ boxShadow: '0 1px 2px rgba(0,0,0,.28)',
143
+ transition: 'left .15s ease',
144
+ },
145
+ }));
146
+ }
147
+
148
+ // ---- open dialog state shared between context menus and the overlay slot ----
149
+ let dialogSpec = null;
150
+ const dialogSubs = new Set();
151
+ function openDialog(spec) {
152
+ dialogSpec = spec;
153
+ for (const notify of dialogSubs) notify();
154
+ }
155
+ function closeDialog() {
156
+ dialogSpec = null;
157
+ for (const notify of dialogSubs) notify();
158
+ }
159
+ function useDialogSpec() {
160
+ const [, force] = useState(0);
161
+ useEffect(() => {
162
+ const notify = () => force((n) => n + 1);
163
+ dialogSubs.add(notify);
164
+ return () => { dialogSubs.delete(notify); };
165
+ }, []);
166
+ return dialogSpec;
167
+ }
168
+
169
+ function ScopeDialog({ spec, onClose }) {
170
+ const [state, setState] = useState(null);
171
+ const [error, setError] = useState('');
172
+ const [busy, setBusy] = useState('');
173
+ const [query, setQuery] = useState('');
174
+ const [diag, setDiag] = useState('');
175
+ const level = spec.level;
176
+ const isSession = level === 'session';
177
+
178
+ const load = useCallback(async (fresh) => {
179
+ try {
180
+ const data = await call(isSession
181
+ ? { op: fresh ? 'refresh' : 'state', sessionId: spec.id }
182
+ : { op: fresh ? 'refresh' : 'state', workspaceId: spec.id });
183
+ setState(data);
184
+ setError('');
185
+ } catch (err) {
186
+ setError(String(err?.message ?? err));
187
+ }
188
+ }, [isSession, spec.id]);
189
+
190
+ useEffect(() => { load(true); }, [load]);
191
+
192
+ useEffect(() => {
193
+ const onKey = (event) => { if (event.key === 'Escape') onClose(); };
194
+ document.addEventListener('keydown', onKey);
195
+ return () => document.removeEventListener('keydown', onKey);
196
+ }, [onClose]);
197
+
198
+ const pick = async (row, value) => {
199
+ setBusy(row.key);
200
+ setError('');
201
+ try {
202
+ const result = await call({
203
+ op: 'set',
204
+ level,
205
+ id: spec.id,
206
+ key: row.key,
207
+ value,
208
+ });
209
+ if (result?.application === 'restart-required') setError('已保存;需要重启 DSH 才能完全生效');
210
+ await load(false);
211
+ } catch (err) {
212
+ setError(String(err?.message ?? err));
213
+ } finally {
214
+ setBusy('');
215
+ }
216
+ };
217
+
218
+ const toggle = (row) => {
219
+ const value = localValue(row, level);
220
+ const current = isExplicit(value) ? value : row.effective;
221
+ const next = current === 'on' ? 'off' : 'on';
222
+ const parent = parentValue(row, level);
223
+ pick(row, next === parent ? 'inherit' : next).catch(() => {});
224
+ };
225
+
226
+ const rows = (state?.rows ?? []).filter((row) => row.scoped && (
227
+ !query || row.name.toLowerCase().includes(query.toLowerCase())
228
+ ));
229
+ const onRows = rows.filter((row) => row.parent !== 'off');
230
+ const offRows = rows.filter((row) => row.parent === 'off');
231
+
232
+ const renderRow = (row) => {
233
+ const value = localValue(row, level);
234
+ const explicit = isExplicit(value);
235
+ const checked = explicit ? value === 'on' : row.effective === 'on';
236
+ const parent = parentValue(row, level);
237
+ return h('div', {
238
+ key: row.key,
239
+ style: {
240
+ display: 'grid',
241
+ gridTemplateColumns: 'minmax(0, 1fr) auto',
242
+ gap: '16px',
243
+ alignItems: 'center',
244
+ padding: '11px 4px',
245
+ borderTop: theme.border,
246
+ },
247
+ },
248
+ h('div', { style: { minWidth: 0 } },
249
+ h('div', { style: { display: 'flex', alignItems: 'center', gap: '7px', minWidth: 0 } },
250
+ h('div', { title: row.name, style: { fontWeight: 500, color: theme.text, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, row.name),
251
+ h(ScopeBadge, { row }),
252
+ ),
253
+ h('div', { style: { marginTop: '3px', fontSize: '11px', color: theme.faint, lineHeight: 1.5 } },
254
+ `${capabilityText(row)} · ${reasonText(row)}`),
255
+ ),
256
+ h('div', { style: { display: 'flex', alignItems: 'center', gap: '14px' } },
257
+ h('div', { style: { textAlign: 'right', minWidth: '148px' } },
258
+ h('div', { style: { fontSize: '12px', color: theme.muted } },
259
+ explicit ? '本层已固定' : `跟随${parentName(level)}`),
260
+ h('div', { style: { marginTop: '3px', fontSize: '11px', color: theme.faint, display: 'flex', alignItems: 'center', justifyContent: 'flex-end', gap: '5px' } },
261
+ explicit
262
+ ? h('button', {
263
+ type: 'button',
264
+ disabled: busy === row.key,
265
+ onClick: () => { pick(row, 'inherit').catch(() => {}); },
266
+ style: {
267
+ border: 0, background: 'transparent', padding: 0, cursor: 'pointer',
268
+ color: 'var(--dsw-alias-brand-primary)', fontSize: '11px', marginLeft: '6px',
269
+ },
270
+ }, '恢复继承')
271
+ : null),
272
+ ),
273
+ row.scoped
274
+ ? h(Switch, {
275
+ checked,
276
+ disabled: busy === row.key,
277
+ label: `${row.name} 在本层${checked ? '关闭' : '开启'}`,
278
+ hint: row.capability ? '' : '该插件通过 llm/stream 拦截请求;关闭后本会话跳过它',
279
+ onChange: () => toggle(row),
280
+ })
281
+ : h('span', { style: { fontSize: '11px', color: theme.faint, minWidth: '36px', textAlign: 'center' } }, '仅全局'),
282
+ ),
283
+ );
284
+ };
285
+
286
+ const group = (title, list) => list.length === 0 ? null : h('div', { style: { marginTop: '10px' } },
287
+ h('div', { style: { fontSize: '11px', color: theme.faint, padding: '0 4px 2px' } }, title),
288
+ list.map(renderRow),
289
+ );
290
+
291
+ return h('div', {
292
+ style: {
293
+ position: 'fixed', inset: 0, zIndex: 1000,
294
+ background: 'rgba(0,0,0,.38)',
295
+ display: 'flex', alignItems: 'center', justifyContent: 'center',
296
+ },
297
+ onMouseDown: (event) => { if (event.target === event.currentTarget) onClose(); },
298
+ },
299
+ h('div', {
300
+ role: 'dialog',
301
+ 'aria-modal': 'true',
302
+ style: {
303
+ width: 'min(780px, 94vw)', maxHeight: '86vh',
304
+ display: 'flex', flexDirection: 'column',
305
+ borderRadius: '14px', padding: '18px 20px',
306
+ background: theme.overlay,
307
+ color: theme.text,
308
+ boxShadow: '0 24px 70px rgba(0,0,0,.35)',
309
+ border: theme.strongBorder,
310
+ fontSize: '13px',
311
+ },
312
+ },
313
+ h('div', { style: { display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between', gap: '12px' } },
314
+ h('div', null,
315
+ h('div', { style: { fontSize: '15px', fontWeight: 600, color: theme.text } },
316
+ isSession ? '插件作用域 · 这个会话' : '插件作用域 · 这个工作区'),
317
+ spec.title
318
+ ? h('div', { style: { marginTop: '3px', fontSize: '12px', color: theme.muted } }, `「${spec.title}」`)
319
+ : null,
320
+ h('div', { style: { marginTop: '3px', fontSize: '12px', color: theme.faint } },
321
+ isSession
322
+ ? (state?.workspaceTitle
323
+ ? `默认跟随工作区「${state.workspaceTitle}」;改动只影响这个会话。`
324
+ : '默认跟随工作区;改动只影响这个会话。')
325
+ : '默认跟随全局;改动只影响这个工作区。'),
326
+ ),
327
+ h('button', {
328
+ type: 'button',
329
+ onClick: onClose,
330
+ style: {
331
+ border: theme.border, background: 'transparent', color: theme.text,
332
+ borderRadius: '7px', padding: '5px 11px', cursor: 'pointer', fontSize: '12px',
333
+ },
334
+ }, '关闭'),
335
+ ),
336
+ error ? h('div', { style: { marginTop: '8px', fontSize: '12px', color: theme.warn } }, error) : null,
337
+ diag ? h('div', { style: { marginTop: '6px', fontSize: '12px', color: theme.muted, whiteSpace: 'pre-wrap' } }, diag) : null,
338
+ !state
339
+ ? h('div', { style: { padding: '30px 0', color: theme.muted } }, '正在读取…')
340
+ : rows.length === 0
341
+ ? h('div', { style: { padding: '26px 0', color: theme.muted, lineHeight: 1.7 } },
342
+ query
343
+ ? '没有匹配的会话级插件。'
344
+ : '这个层级没有会话级插件;仅全局插件在“插件管理 → 作用域”里管理。')
345
+ : h('div', { style: { display: 'flex', flexDirection: 'column', minHeight: 0, flex: 1 } },
346
+ h('div', { style: { display: 'flex', gap: '8px', alignItems: 'center', marginTop: '12px' } },
347
+ h('input', {
348
+ value: query,
349
+ placeholder: '按名字筛选…',
350
+ onChange: (event) => setQuery(event.target.value),
351
+ style: {
352
+ flex: 1, minWidth: '160px', fontSize: '12px', padding: '6px 9px',
353
+ borderRadius: '7px', border: theme.border, background: theme.surface, color: theme.text,
354
+ },
355
+ }),
356
+ h('button', {
357
+ type: 'button',
358
+ onClick: () => { load(true).catch(() => {}); },
359
+ style: { border: theme.border, background: 'transparent', color: theme.text, borderRadius: '7px', padding: '5px 11px', cursor: 'pointer', fontSize: '12px' },
360
+ }, '刷新'),
361
+ isSession ? h('button', {
362
+ type: 'button',
363
+ onClick: async () => {
364
+ setDiag('正在自检…');
365
+ try {
366
+ const data = await call({ op: 'debug', sessionId: spec.id });
367
+ const agent = data.agents?.[0];
368
+ setDiag(`本会话隐藏工具 ${data.hidden?.length ?? 0} 个;实际可见 ${agent?.visibleCount ?? '?'} 个${agent?.fallback ? '(静态估算)' : ''};ops_read_docx 可见=${agent?.opsVisible ?? '?'};agent=${agent?.agentId ?? '未找到'}`);
369
+ } catch (err) {
370
+ setDiag(String(err?.message ?? err));
371
+ }
372
+ },
373
+ style: { border: theme.border, background: 'transparent', color: theme.text, borderRadius: '7px', padding: '5px 11px', cursor: 'pointer', fontSize: '12px' },
374
+ }, '自检') : null,
375
+ ),
376
+ h('div', { style: { marginTop: '8px', fontSize: '11px', color: theme.faint, lineHeight: 1.6 } },
377
+ '只列会话级插件:工具或提示词会随会话变化。开关只改这一层——拨动后固定本层,点“恢复继承”回到跟随。改动在下一次模型请求时生效。'),
378
+ h('div', { style: { maxHeight: '58vh', overflowY: 'auto', paddingRight: '4px', marginTop: '4px' } },
379
+ group('上一层已开启', onRows),
380
+ group('上一层已关闭', offRows),
381
+ ),
382
+ ),
383
+ ),
384
+ );
385
+ }
386
+
387
+ function OverlayHost() {
388
+ const spec = useDialogSpec();
389
+ if (!spec) return null;
390
+ return h(ScopeDialog, { key: `${spec.level}:${spec.id}`, spec, onClose: closeDialog });
391
+ }
392
+
393
+ function ScopesTab() {
394
+ const [state, setState] = useState(null);
395
+ const [workspaceId, setWorkspaceId] = useState('');
396
+ const [message, setMessage] = useState('');
397
+ const [busy, setBusy] = useState('');
398
+ const [query, setQuery] = useState('');
399
+
400
+ const load = useCallback((ws, fresh) => {
401
+ call({ op: fresh ? 'refresh' : 'state', workspaceId: ws || undefined })
402
+ .then((data) => { setState(data); setMessage(''); })
403
+ .catch((error) => setMessage(String(error?.message ?? error)));
404
+ }, []);
405
+
406
+ useEffect(() => { load(workspaceId, !state); }, [load, workspaceId]);
407
+
408
+ const pick = useCallback(async (row, level, value) => {
409
+ const payload = { op: 'set', level, key: row.key, value };
410
+ if (level === 'workspace') {
411
+ if (!workspaceId) { setMessage('先在上方选择一个工作区'); return; }
412
+ payload.id = workspaceId;
413
+ }
414
+ setBusy(`${row.key}:${level}`);
415
+ setMessage('');
416
+ try {
417
+ const result = await call(payload);
418
+ if (result?.application === 'restart-required') setMessage('已保存;该改动需要重启 DSH 后完全生效');
419
+ else if (result?.application === 'overridden') setMessage('已保存;当前被别的配置覆盖,重启后再看');
420
+ await load(workspaceId, false);
421
+ } catch (error) {
422
+ setMessage(String(error?.message ?? error));
423
+ } finally {
424
+ setBusy('');
425
+ }
426
+ }, [workspaceId, load]);
427
+
428
+ const manage = useCallback(async (row, on) => {
429
+ setBusy(`${row.key}:manage`);
430
+ setMessage('');
431
+ try {
432
+ const result = await call({ op: 'manage', key: row.key, value: on ? 'on' : 'off' });
433
+ if (result?.restartRequired) setMessage('已写入;这项改动需要重启 DSH 后才会真正接管');
434
+ else if (result?.application === 'applied') setMessage(on ? '已接管:新会话按作用域挂载,旧会话不受影响' : '已取消接管');
435
+ await load(workspaceId, false);
436
+ } catch (error) {
437
+ setMessage(String(error?.message ?? error));
438
+ } finally {
439
+ setBusy('');
440
+ }
441
+ }, [workspaceId, load]);
442
+
443
+ if (!state) {
444
+ return h('div', { style: { padding: '18px', fontSize: '13px', color: theme.muted } },
445
+ message || '正在读取插件作用域…');
446
+ }
447
+
448
+ const rows = state.rows ?? [];
449
+ const needle = query.trim().toLowerCase();
450
+ const match = (row) => !needle || row.name.toLowerCase().includes(needle);
451
+ const filtered = rows.filter((row) => !row.protected && match(row));
452
+ const sessionRows = filtered.filter((row) => row.scoped);
453
+ const globalRows = filtered.filter((row) => !row.scoped);
454
+ const selectedWorkspace = (state.workspaces ?? []).find((workspace) => workspace.id === workspaceId);
455
+ const level = workspaceId ? 'workspace' : 'global';
456
+ const GRID = 'minmax(0, 1.8fr) minmax(120px, 1fr) 52px';
457
+
458
+ const heading = h('div', {
459
+ style: {
460
+ display: 'grid', gridTemplateColumns: GRID, gap: '10px',
461
+ padding: '0 4px 7px', fontSize: '11px', color: theme.faint,
462
+ },
463
+ }, ['插件', '本层状态', '开关'].map((cell, index) => h('div', {
464
+ key: cell,
465
+ style: index === 0 ? undefined : { textAlign: 'right' },
466
+ }, cell)));
467
+
468
+ const section = (title, count, list) => h('div', { key: title, style: { marginTop: '10px' } },
469
+ h('div', {
470
+ style: {
471
+ display: 'flex', alignItems: 'center', gap: '8px',
472
+ padding: '7px 4px 5px', fontSize: '11px', color: theme.muted,
473
+ borderBottom: theme.border,
474
+ },
475
+ },
476
+ h('span', { style: { fontWeight: 600 } }, title),
477
+ h('span', { style: { color: theme.faint } }, `${count} 个`),
478
+ ),
479
+ list.length === 0
480
+ ? h('div', { style: { padding: '12px 4px', fontSize: '12px', color: theme.faint } }, '没有匹配项。')
481
+ : list.map(renderRow),
482
+ );
483
+
484
+ const renderRow = (row) => {
485
+ const busyHere = busy.startsWith(`${row.key}:`);
486
+ const value = level === 'workspace' ? row.workspace : row.global;
487
+ const explicit = isExplicit(value);
488
+ const checked = explicit ? value === 'on' : row.effective === 'on';
489
+ const parent = row.global;
490
+ const canToggle = level === 'global' ? true : (row.scoped && !!workspaceId);
491
+
492
+ return h('div', {
493
+ key: row.key,
494
+ style: { display: 'grid', gridTemplateColumns: GRID, gap: '10px', padding: '10px 4px', borderTop: theme.border, alignItems: 'center' },
495
+ },
496
+ h('div', { style: { minWidth: 0 } },
497
+ h('div', { style: { display: 'flex', alignItems: 'center', gap: '7px', minWidth: 0 } },
498
+ h('div', { title: row.name, style: { fontWeight: 500, color: theme.text, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' } },
499
+ row.name),
500
+ h(ScopeBadge, { row }),
501
+ row.protected ? h('span', { style: { fontSize: '10px', color: theme.faint, flex: '0 0 auto' } }, '基础 · 不参与') : null),
502
+ h('div', { style: { marginTop: '3px', fontSize: '11px', color: theme.faint, lineHeight: 1.5 } },
503
+ `${capabilityText(row)} · ${reasonText(row)}`),
504
+ ),
505
+ h('div', { style: { fontSize: '12px', color: theme.muted, textAlign: 'right' } },
506
+ level === 'global'
507
+ ? '全局默认'
508
+ : (explicit
509
+ ? h('span', null,
510
+ '本层已固定',
511
+ h('button', {
512
+ type: 'button',
513
+ disabled: busyHere,
514
+ onClick: () => { pick(row, 'workspace', 'inherit').catch(() => {}); },
515
+ style: { marginLeft: '8px', border: 0, background: 'transparent', color: theme.brand, cursor: 'pointer', fontSize: '11px', padding: 0 },
516
+ }, '恢复继承'))
517
+ : '跟随全局')),
518
+ h('div', { style: { display: 'flex', alignItems: 'center', justifyContent: 'flex-end' } },
519
+ canToggle
520
+ ? h(Switch, {
521
+ checked,
522
+ disabled: busyHere || (row.protected && checked),
523
+ label: `${row.name} ${checked ? '关闭' : '开启'}`,
524
+ hint: row.capability ? '' : '该插件通过 llm/stream 拦截请求;关闭后本会话跳过它',
525
+ onChange: () => {
526
+ const current = explicit ? value : row.effective;
527
+ pick(row, level, current === 'on' ? 'off' : 'on').catch(() => {});
528
+ },
529
+ })
530
+ : h('span', { style: { fontSize: '12px', color: theme.faint } }, '—')),
531
+ );
532
+ };
533
+
534
+ return h('div', {
535
+ style: { display: 'flex', flexDirection: 'column', gap: '14px', fontSize: '13px', padding: '4px 2px', color: theme.text },
536
+ },
537
+ h('div', null,
538
+ h('div', { style: { fontSize: '15px', fontWeight: 600, color: theme.text } }, '第三方插件作用域'),
539
+ h('div', { style: { marginTop: '4px', color: theme.muted, lineHeight: 1.65 } },
540
+ '类型按插件描述自动判定:工具、模型、提示词等会随会话变化的是“会话级”;壁纸、界面、管理等只在全局生效的是“仅全局”。开关只改“这一层”,右侧文字说明它当前是跟随上一层还是本层已固定。'),
541
+ message
542
+ ? h('div', { style: { marginTop: '6px', fontSize: '12px', color: theme.warn } }, message)
543
+ : null,
544
+ ),
545
+ h('div', { style: { display: 'flex', alignItems: 'center', gap: '10px', flexWrap: 'wrap' } },
546
+ h('span', { style: { fontSize: '12px', color: theme.muted } }, '编辑层级:'),
547
+ h('select', {
548
+ value: workspaceId,
549
+ onChange: (event) => setWorkspaceId(event.target.value),
550
+ style: {
551
+ fontSize: '12px', padding: '5px 9px', borderRadius: '7px',
552
+ border: theme.border, background: theme.surface, color: theme.text, minWidth: '210px',
553
+ },
554
+ },
555
+ h('option', { value: '' }, '全局'),
556
+ (state.workspaces ?? []).map((workspace) =>
557
+ h('option', { key: workspace.id, value: workspace.id }, `工作区 · ${workspace.title}`)),
558
+ ),
559
+ h('input', {
560
+ value: query,
561
+ placeholder: '按名字筛选…',
562
+ onChange: (event) => setQuery(event.target.value),
563
+ style: {
564
+ flex: 1, minWidth: '150px', fontSize: '12px', padding: '6px 9px',
565
+ borderRadius: '7px', border: theme.border, background: theme.surface, color: theme.text,
566
+ },
567
+ }),
568
+ h('button', {
569
+ type: 'button',
570
+ onClick: () => { load(workspaceId, true).catch(() => {}); },
571
+ style: { border: theme.border, background: 'transparent', color: theme.text, borderRadius: '7px', padding: '5px 11px', cursor: 'pointer', fontSize: '12px' },
572
+ }, '刷新'),
573
+ ),
574
+ h('div', { style: { fontSize: '11px', color: theme.faint } },
575
+ workspaceId
576
+ ? `正在编辑工作区「${selectedWorkspace?.title ?? workspaceId}」;会话级开关在会话 “…” 菜单里。`
577
+ : '正在编辑全局默认;选择工作区可改该工作区的覆盖。会话级开关在会话 “…” 菜单里。'),
578
+ h('div', { style: { borderTop: theme.border, paddingTop: '8px', maxHeight: '62vh', overflowY: 'auto', paddingRight: '4px' } },
579
+ heading,
580
+ section('会话级插件', sessionRows.length, sessionRows),
581
+ section('仅全局插件', globalRows.length, globalRows),
582
+ ),
583
+ h('div', { style: { fontSize: '11px', color: theme.faint } },
584
+ `存储:${state.storage ?? ''}`),
585
+ );
586
+ }
587
+
588
+ // Sits in the New Session row beside the workspace and preset controls, so a
589
+ // session can be scoped before its first message is sent.
590
+ function NewSessionScope({ sessionId }) {
591
+ if (!sessionId) return null;
592
+ return h('button', {
593
+ type: 'button',
594
+ title: '设置这个新会话的第三方插件作用域',
595
+ onClick: () => openDialog({ level: 'session', id: sessionId, title: '新会话' }),
596
+ style: {
597
+ border: theme.border,
598
+ background: 'transparent',
599
+ color: theme.muted,
600
+ borderRadius: '999px',
601
+ padding: '3px 10px',
602
+ cursor: 'pointer',
603
+ fontSize: '12px',
604
+ lineHeight: '18px',
605
+ whiteSpace: 'nowrap',
606
+ },
607
+ }, '插件作用域');
608
+ }
609
+
610
+ function IconScope() {
611
+ return h('svg', {
612
+ width: 16, height: 16, viewBox: '0 0 16 16', fill: 'none',
613
+ stroke: 'currentColor', strokeWidth: 1.2, strokeLinejoin: 'round', 'aria-hidden': 'true',
614
+ },
615
+ h('rect', { x: 2.25, y: 2.25, width: 11.5, height: 3.1, rx: 1.1 }),
616
+ h('rect', { x: 2.25, y: 6.45, width: 8.5, height: 3.1, rx: 1.1 }),
617
+ h('rect', { x: 2.25, y: 10.65, width: 5.4, height: 3.1, rx: 1.1 }),
618
+ )
619
+ }
620
+
621
+ function workspaceMenu(ctx) {
622
+ return ctx.effect(() => ctx.contextMenus.register({
623
+ id: 'dsh-plugins-scope:workspace',
624
+ target: 'workspace',
625
+ order: 150,
626
+ resolve: async (target) => {
627
+ const workspaceId = target.workspaceId ?? target.id;
628
+ return [{
629
+ id: 'plugin-scopes:open',
630
+ label: '插件作用域…',
631
+ icon: { key: 'plugin-scope' },
632
+ run: () => openDialog({
633
+ level: 'workspace',
634
+ id: workspaceId,
635
+ title: target.displayTitle ?? '',
636
+ }),
637
+ }];
638
+ },
639
+ }), 'plugin-scopes: workspace menu');
640
+ }
641
+
642
+ function sessionMenu(ctx) {
643
+ return ctx.effect(() => ctx.contextMenus.register({
644
+ id: 'dsh-plugins-scope:session',
645
+ target: 'session',
646
+ order: 250,
647
+ resolve: async (target) => {
648
+ const sessionId = target.sessionId ?? target.id;
649
+ return [{
650
+ id: 'plugin-scopes:open',
651
+ label: '插件作用域…',
652
+ icon: { key: 'plugin-scope' },
653
+ run: () => openDialog({
654
+ level: 'session',
655
+ id: sessionId,
656
+ title: target.displayTitle ?? '',
657
+ }),
658
+ }];
659
+ },
660
+ }), 'plugin-scopes: session menu');
661
+ }
662
+
663
+ return {
664
+ inject: ['slots', 'contextMenus'],
665
+ apply(ctx) {
666
+ ctx.slots.inject('plugins.tab', () => ctx.slots.register({
667
+ name: 'plugins.tab',
668
+ id: 'plugin-scopes',
669
+ order: 20,
670
+ label: '作用域',
671
+ }, ScopesTab));
672
+ ctx.slots.inject('conversation.hero.options', () => ctx.slots.register({
673
+ name: 'conversation.hero.options',
674
+ id: 'plugin-scopes-new',
675
+ order: 30,
676
+ label: '插件作用域',
677
+ }, NewSessionScope));
678
+ ctx.slots.inject('shell.overlay', () => ctx.slots.register({
679
+ name: 'shell.overlay',
680
+ id: 'plugin-scopes-dialog',
681
+ order: 800,
682
+ label: '插件作用域窗口',
683
+ }, OverlayHost));
684
+ ctx.slots.inject('contextMenu.icon', () => ctx.slots.register({
685
+ name: 'contextMenu.icon',
686
+ key: 'plugin-scope',
687
+ }, IconScope));
688
+ workspaceMenu(ctx);
689
+ sessionMenu(ctx);
690
+ },
691
+ };
692
+ },
693
+ });