dsh-model-guard 0.0.0-stage → 1.0.0
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 +21 -0
- package/README.md +81 -2
- package/README.zh.md +56 -0
- package/assets/composer-blocked.png +0 -0
- package/assets/icon-180.png +0 -0
- package/assets/rule-dialog.png +0 -0
- package/assets/settings.png +0 -0
- package/client.js +1347 -0
- package/cordis.patch.yml +3 -0
- package/icon.svg +5 -0
- package/index.js +203 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +64 -4
package/client.js
ADDED
|
@@ -0,0 +1,1347 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: 'dsh-model-guard',
|
|
3
|
+
factory(require) {
|
|
4
|
+
const React = require('react')
|
|
5
|
+
const h = React.createElement
|
|
6
|
+
|
|
7
|
+
/** Locale namespace owned by this plugin. */
|
|
8
|
+
const NS = 'modelGuard'
|
|
9
|
+
|
|
10
|
+
/** Host plugin entry id; the config form namespace is the entry id. */
|
|
11
|
+
const ENTRY_ID = 'dsh-model-guard'
|
|
12
|
+
|
|
13
|
+
/** Simplified Chinese copy. */
|
|
14
|
+
const zh = {
|
|
15
|
+
nav: '模型守卫',
|
|
16
|
+
title: '模型守卫',
|
|
17
|
+
intro: '为每个目录设定可用的模型。最深的匹配规则会作用于该目录及所有子目录;没有规则的目录不受限制。',
|
|
18
|
+
addRule: '添加目录规则',
|
|
19
|
+
addTitle: '新建目录规则',
|
|
20
|
+
editTitle: '编辑目录规则',
|
|
21
|
+
directory: '目录',
|
|
22
|
+
directoryHint: '请填写绝对路径;正反斜杠均可,保存时会统一为正斜杠。',
|
|
23
|
+
directoryPlaceholder: '/home/me/project',
|
|
24
|
+
useCurrent: '使用当前工作区',
|
|
25
|
+
noCurrent: '当前没有可用的工作区目录',
|
|
26
|
+
allowedModels: '可用模型',
|
|
27
|
+
modelsHint: '可多选。留空表示该目录禁止使用任何模型。',
|
|
28
|
+
loadingModels: '正在加载模型目录…',
|
|
29
|
+
catalogError: '无法加载模型目录:{message}。仍可手动添加模型 ID。',
|
|
30
|
+
noCatalogModels: '当前部署没有可用模型,请手动添加模型 ID。',
|
|
31
|
+
manualPlaceholder: 'provider/model 或 model',
|
|
32
|
+
add: '添加',
|
|
33
|
+
save: '保存规则',
|
|
34
|
+
cancel: '取消',
|
|
35
|
+
edit: '编辑',
|
|
36
|
+
remove: '删除',
|
|
37
|
+
noRules: '还没有目录规则,所有目录均不受限制。',
|
|
38
|
+
modelCount: '{count} 个模型',
|
|
39
|
+
noneAllowed: '未允许任何模型——该目录将无法发送。',
|
|
40
|
+
selectedModels: '已选模型',
|
|
41
|
+
saved: '规则已保存。',
|
|
42
|
+
removed: '规则已删除。',
|
|
43
|
+
errorDirectory: '请输入目录路径。',
|
|
44
|
+
errorAbsolute: '请输入绝对路径,例如 /home/me/project 或 C:/work/project。',
|
|
45
|
+
errorDuplicate: '该目录已有规则,请直接编辑已有规则。',
|
|
46
|
+
errorWrite: '无法保存规则:{message}',
|
|
47
|
+
loadingRules: '正在加载规则…',
|
|
48
|
+
unavailable: 'Host 未提供此插件的配置,无法读取或保存规则。',
|
|
49
|
+
readOnly: '当前部署的设置是只读的,无法在此修改规则。',
|
|
50
|
+
saving: '正在保存…',
|
|
51
|
+
badge: '模型受限',
|
|
52
|
+
blockedModel: '当前目录 {directory} 不允许使用模型 {model}。请切换模型,或修改“设置 > 模型守卫”。',
|
|
53
|
+
blockedEmpty: '当前目录 {directory} 未允许任何模型。请在“设置 > 模型守卫”中添加。',
|
|
54
|
+
menuSetRules: '设置模型白名单',
|
|
55
|
+
menuEditRules: '编辑模型白名单',
|
|
56
|
+
dialogTitle: '目录模型白名单',
|
|
57
|
+
dialogDescription: '为 {name} 设置可用模型。规则会作用于该目录及所有子目录。',
|
|
58
|
+
dialogDelete: '删除规则',
|
|
59
|
+
dialogClose: '关闭',
|
|
60
|
+
dialogInherited: '当前继承自 {directory} 的规则;保存后会在本目录创建一条覆盖规则。',
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** English copy. */
|
|
64
|
+
const en = {
|
|
65
|
+
nav: 'Model guard',
|
|
66
|
+
title: 'Directory model guard',
|
|
67
|
+
intro: 'Choose which models may be used in each folder. The deepest matching rule applies to its folder and every subfolder; a folder without a rule is unrestricted.',
|
|
68
|
+
addRule: 'Add directory rule',
|
|
69
|
+
addTitle: 'New directory rule',
|
|
70
|
+
editTitle: 'Edit directory rule',
|
|
71
|
+
directory: 'Directory',
|
|
72
|
+
directoryHint: 'Absolute path. Forward or back slashes are accepted and stored normalized.',
|
|
73
|
+
directoryPlaceholder: '/home/me/project',
|
|
74
|
+
useCurrent: 'Use current workspace',
|
|
75
|
+
noCurrent: 'No workspace directory is available',
|
|
76
|
+
allowedModels: 'Allowed models',
|
|
77
|
+
modelsHint: 'Select one or more. An empty list blocks every model in this folder.',
|
|
78
|
+
loadingModels: 'Loading model catalog…',
|
|
79
|
+
catalogError: 'Could not load the model catalog: {message}. You can still add model IDs manually.',
|
|
80
|
+
noCatalogModels: 'This deployment reports no models. Add model IDs manually.',
|
|
81
|
+
manualPlaceholder: 'provider/model or model',
|
|
82
|
+
add: 'Add',
|
|
83
|
+
save: 'Save rule',
|
|
84
|
+
cancel: 'Cancel',
|
|
85
|
+
edit: 'Edit',
|
|
86
|
+
remove: 'Remove',
|
|
87
|
+
noRules: 'No directory rules yet. Every folder is unrestricted.',
|
|
88
|
+
modelCount: '{count} models',
|
|
89
|
+
noneAllowed: 'No model allowed — sending is blocked in this folder.',
|
|
90
|
+
selectedModels: 'Selected models',
|
|
91
|
+
saved: 'Rule saved.',
|
|
92
|
+
removed: 'Rule removed.',
|
|
93
|
+
errorDirectory: 'Enter a folder path.',
|
|
94
|
+
errorAbsolute: 'Enter an absolute folder path, such as /home/me/project or C:/work/project.',
|
|
95
|
+
errorDuplicate: 'A rule for this directory already exists. Edit that rule instead.',
|
|
96
|
+
errorWrite: 'Could not save the rules: {message}',
|
|
97
|
+
loadingRules: 'Loading rules…',
|
|
98
|
+
unavailable: 'The Host did not expose this plugin\'s configuration, so rules cannot be read or saved.',
|
|
99
|
+
readOnly: 'This deployment stores settings read-only. Rules cannot be edited here.',
|
|
100
|
+
saving: 'Saving…',
|
|
101
|
+
badge: 'Model restricted',
|
|
102
|
+
blockedModel: 'Model {model} is not allowed in {directory}. Pick another model or update Settings > Model guard.',
|
|
103
|
+
blockedEmpty: 'No model is allowed in {directory}. Add one in Settings > Model guard.',
|
|
104
|
+
menuSetRules: 'Set model whitelist',
|
|
105
|
+
menuEditRules: 'Edit model whitelist',
|
|
106
|
+
dialogTitle: 'Directory model whitelist',
|
|
107
|
+
dialogDescription: 'Set the allowed models for {name}. The rule applies to this directory and every subfolder.',
|
|
108
|
+
dialogDelete: 'Delete rule',
|
|
109
|
+
dialogClose: 'Close',
|
|
110
|
+
dialogInherited: 'This folder inherits the rule from {directory}. Saving creates an override for this folder.',
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Styles shared by the Settings page. */
|
|
114
|
+
const PAGE_CSS = `
|
|
115
|
+
.dsh-mg {
|
|
116
|
+
display: flex;
|
|
117
|
+
flex-direction: column;
|
|
118
|
+
gap: 16px;
|
|
119
|
+
width: 100%;
|
|
120
|
+
max-width: 840px;
|
|
121
|
+
color: var(--dsw-alias-label-primary);
|
|
122
|
+
font-family: inherit;
|
|
123
|
+
}
|
|
124
|
+
.dsh-mg h2, .dsh-mg h3 { margin: 0; color: var(--dsw-alias-label-primary); }
|
|
125
|
+
.dsh-mg h2 { font-size: 20px; line-height: 28px; font-weight: 600; }
|
|
126
|
+
.dsh-mg h3 { font-size: 15px; line-height: 22px; font-weight: 600; }
|
|
127
|
+
.dsh-mg-intro {
|
|
128
|
+
margin: 8px 0 0;
|
|
129
|
+
color: var(--dsw-alias-label-secondary);
|
|
130
|
+
font-size: 14px;
|
|
131
|
+
line-height: 22px;
|
|
132
|
+
}
|
|
133
|
+
.dsh-mg-toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
|
|
134
|
+
.dsh-mg-list { display: flex; flex-direction: column; gap: 10px; }
|
|
135
|
+
.dsh-mg-card {
|
|
136
|
+
box-sizing: border-box;
|
|
137
|
+
display: flex;
|
|
138
|
+
flex-direction: column;
|
|
139
|
+
gap: 10px;
|
|
140
|
+
padding: 14px 16px;
|
|
141
|
+
border: 1px solid var(--dsw-alias-border-l2);
|
|
142
|
+
border-radius: var(--dsw-radius-lg);
|
|
143
|
+
background: var(--dsw-alias-bg-module-platform, transparent);
|
|
144
|
+
}
|
|
145
|
+
.dsh-mg-editor { gap: 14px; }
|
|
146
|
+
.dsh-mg-card-head {
|
|
147
|
+
display: flex;
|
|
148
|
+
gap: 10px;
|
|
149
|
+
align-items: center;
|
|
150
|
+
justify-content: space-between;
|
|
151
|
+
min-width: 0;
|
|
152
|
+
}
|
|
153
|
+
.dsh-mg-path {
|
|
154
|
+
min-width: 0;
|
|
155
|
+
overflow: hidden;
|
|
156
|
+
color: var(--dsw-alias-label-primary);
|
|
157
|
+
font-size: 12px;
|
|
158
|
+
text-overflow: ellipsis;
|
|
159
|
+
white-space: nowrap;
|
|
160
|
+
}
|
|
161
|
+
.dsh-mg-count { flex: none; color: var(--dsw-alias-label-caption); font-size: 11px; white-space: nowrap; }
|
|
162
|
+
.dsh-mg-field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
|
|
163
|
+
.dsh-mg-label-row { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; }
|
|
164
|
+
.dsh-mg-label { color: var(--dsw-alias-label-secondary); font-size: 12px; font-weight: 600; }
|
|
165
|
+
.dsh-mg-hint { margin: 0; color: var(--dsw-alias-label-caption); font-size: 11px; line-height: 17px; }
|
|
166
|
+
.dsh-mg-row { display: flex; gap: 8px; align-items: flex-end; min-width: 0; }
|
|
167
|
+
.dsh-mg-row > .dsh-mg-input { flex: 1; min-width: 0; }
|
|
168
|
+
.dsh-mg-input {
|
|
169
|
+
box-sizing: border-box;
|
|
170
|
+
width: 100%;
|
|
171
|
+
height: 34px;
|
|
172
|
+
padding: 0 10px;
|
|
173
|
+
border: 1px solid var(--dsw-alias-border-l2);
|
|
174
|
+
border-radius: var(--dsw-radius-sm);
|
|
175
|
+
background: var(--dsw-specific-input-major, transparent);
|
|
176
|
+
color: var(--dsw-alias-label-primary);
|
|
177
|
+
font: inherit;
|
|
178
|
+
font-size: 13px;
|
|
179
|
+
}
|
|
180
|
+
.dsh-mg-input::placeholder { color: var(--dsw-alias-label-quaternary); }
|
|
181
|
+
.dsh-mg-input:focus-visible {
|
|
182
|
+
outline: 2px solid var(--dsw-alias-state-business-primary);
|
|
183
|
+
outline-offset: 1px;
|
|
184
|
+
}
|
|
185
|
+
.dsh-mg-btn {
|
|
186
|
+
box-sizing: border-box;
|
|
187
|
+
display: inline-flex;
|
|
188
|
+
flex: none;
|
|
189
|
+
align-items: center;
|
|
190
|
+
justify-content: center;
|
|
191
|
+
height: 30px;
|
|
192
|
+
padding: 0 10px;
|
|
193
|
+
border: 1px solid var(--dsw-alias-border-l2);
|
|
194
|
+
border-radius: var(--dsw-radius-sm);
|
|
195
|
+
background: transparent;
|
|
196
|
+
color: var(--dsw-alias-label-primary);
|
|
197
|
+
font: inherit;
|
|
198
|
+
font-size: 12px;
|
|
199
|
+
line-height: 18px;
|
|
200
|
+
white-space: nowrap;
|
|
201
|
+
cursor: pointer;
|
|
202
|
+
}
|
|
203
|
+
.dsh-mg-btn:hover:not(:disabled) { background: var(--dsw-alias-interactive-bg-hover); }
|
|
204
|
+
.dsh-mg-btn:disabled { opacity: 0.45; cursor: default; }
|
|
205
|
+
.dsh-mg-btn.primary {
|
|
206
|
+
border-color: transparent;
|
|
207
|
+
background: var(--dsw-alias-button-primary-fill);
|
|
208
|
+
color: var(--dsw-alias-label-on-color);
|
|
209
|
+
}
|
|
210
|
+
.dsh-mg-btn.primary:hover:not(:disabled) {
|
|
211
|
+
background: var(--dsw-alias-button-primary-hover, var(--dsw-alias-button-primary-fill));
|
|
212
|
+
}
|
|
213
|
+
.dsh-mg-btn.danger { color: var(--dsw-alias-state-error-primary); }
|
|
214
|
+
.dsh-mg-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
|
|
215
|
+
.dsh-mg-models {
|
|
216
|
+
box-sizing: border-box;
|
|
217
|
+
display: grid;
|
|
218
|
+
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
|
|
219
|
+
gap: 1px 14px;
|
|
220
|
+
max-height: 270px;
|
|
221
|
+
padding: 8px;
|
|
222
|
+
overflow: auto;
|
|
223
|
+
border: 1px solid var(--dsw-alias-border-l2);
|
|
224
|
+
border-radius: var(--dsw-radius-sm);
|
|
225
|
+
}
|
|
226
|
+
.dsh-mg-provider {
|
|
227
|
+
grid-column: 1 / -1;
|
|
228
|
+
margin: 8px 0 2px;
|
|
229
|
+
color: var(--dsw-alias-label-caption);
|
|
230
|
+
font-size: 10px;
|
|
231
|
+
font-weight: 600;
|
|
232
|
+
letter-spacing: 0.04em;
|
|
233
|
+
text-transform: uppercase;
|
|
234
|
+
}
|
|
235
|
+
.dsh-mg-model {
|
|
236
|
+
display: flex;
|
|
237
|
+
align-items: center;
|
|
238
|
+
gap: 7px;
|
|
239
|
+
min-width: 0;
|
|
240
|
+
padding: 3px 4px;
|
|
241
|
+
border-radius: var(--dsw-radius-xs, 4px);
|
|
242
|
+
cursor: pointer;
|
|
243
|
+
}
|
|
244
|
+
.dsh-mg-model:hover { background: var(--dsw-alias-interactive-bg-hover); }
|
|
245
|
+
.dsh-mg-model input { flex: none; margin: 0; accent-color: var(--dsw-alias-state-business-primary); }
|
|
246
|
+
.dsh-mg-model-name {
|
|
247
|
+
min-width: 0;
|
|
248
|
+
overflow: hidden;
|
|
249
|
+
font-size: 12px;
|
|
250
|
+
text-overflow: ellipsis;
|
|
251
|
+
white-space: nowrap;
|
|
252
|
+
}
|
|
253
|
+
.dsh-mg-model code {
|
|
254
|
+
max-width: 48%;
|
|
255
|
+
margin-left: auto;
|
|
256
|
+
overflow: hidden;
|
|
257
|
+
color: var(--dsw-alias-label-caption);
|
|
258
|
+
font-size: 10px;
|
|
259
|
+
text-overflow: ellipsis;
|
|
260
|
+
white-space: nowrap;
|
|
261
|
+
}
|
|
262
|
+
.dsh-mg-chips { display: flex; flex-wrap: wrap; gap: 6px; min-width: 0; }
|
|
263
|
+
.dsh-mg-chip {
|
|
264
|
+
display: inline-flex;
|
|
265
|
+
align-items: center;
|
|
266
|
+
gap: 4px;
|
|
267
|
+
max-width: 100%;
|
|
268
|
+
padding: 2px 7px;
|
|
269
|
+
overflow: hidden;
|
|
270
|
+
border-radius: 999px;
|
|
271
|
+
background: var(--dsw-alias-fill-tsp-secondary);
|
|
272
|
+
color: var(--dsw-alias-label-secondary);
|
|
273
|
+
font-size: 11px;
|
|
274
|
+
line-height: 18px;
|
|
275
|
+
text-overflow: ellipsis;
|
|
276
|
+
white-space: nowrap;
|
|
277
|
+
}
|
|
278
|
+
.dsh-mg-chip button {
|
|
279
|
+
padding: 0;
|
|
280
|
+
border: 0;
|
|
281
|
+
background: transparent;
|
|
282
|
+
color: inherit;
|
|
283
|
+
font: inherit;
|
|
284
|
+
line-height: 1;
|
|
285
|
+
cursor: pointer;
|
|
286
|
+
}
|
|
287
|
+
.dsh-mg-empty {
|
|
288
|
+
padding: 18px;
|
|
289
|
+
border: 1px dashed var(--dsw-alias-border-l2);
|
|
290
|
+
border-radius: var(--dsw-radius-lg);
|
|
291
|
+
color: var(--dsw-alias-label-secondary);
|
|
292
|
+
font-size: 13px;
|
|
293
|
+
line-height: 20px;
|
|
294
|
+
}
|
|
295
|
+
.dsh-mg-notice {
|
|
296
|
+
padding: 8px 10px;
|
|
297
|
+
border-radius: var(--dsw-radius-sm);
|
|
298
|
+
font-size: 12px;
|
|
299
|
+
line-height: 18px;
|
|
300
|
+
overflow-wrap: anywhere;
|
|
301
|
+
}
|
|
302
|
+
.dsh-mg-notice[data-tone="error"] {
|
|
303
|
+
background: color-mix(in srgb, var(--dsw-alias-state-error-primary) 10%, transparent);
|
|
304
|
+
color: var(--dsw-alias-state-error-primary);
|
|
305
|
+
}
|
|
306
|
+
.dsh-mg-notice[data-tone="ok"] {
|
|
307
|
+
background: var(--dsw-alias-fill-tsp-secondary);
|
|
308
|
+
color: var(--dsw-alias-label-secondary);
|
|
309
|
+
}
|
|
310
|
+
.dsh-mg-warn { color: var(--dsw-alias-state-error-primary); font-size: 11px; line-height: 17px; }
|
|
311
|
+
.dsh-mg-backdrop {
|
|
312
|
+
position: fixed;
|
|
313
|
+
inset: 0;
|
|
314
|
+
z-index: 1000;
|
|
315
|
+
box-sizing: border-box;
|
|
316
|
+
display: grid;
|
|
317
|
+
place-items: center;
|
|
318
|
+
padding: 24px;
|
|
319
|
+
background: color-mix(in srgb, #000000 42%, transparent);
|
|
320
|
+
}
|
|
321
|
+
.dsh-mg-dialog {
|
|
322
|
+
box-sizing: border-box;
|
|
323
|
+
display: flex;
|
|
324
|
+
flex-direction: column;
|
|
325
|
+
gap: 14px;
|
|
326
|
+
width: min(720px, 100%);
|
|
327
|
+
max-height: min(760px, calc(100vh - 48px));
|
|
328
|
+
padding: 20px;
|
|
329
|
+
overflow: auto;
|
|
330
|
+
border: 1px solid var(--dsw-alias-border-l2);
|
|
331
|
+
border-radius: var(--dsw-radius-lg);
|
|
332
|
+
background: var(--dsw-alias-bg-module-platform, var(--dsw-specific-input-major));
|
|
333
|
+
box-shadow: var(--dsw-elevation-soft);
|
|
334
|
+
color: var(--dsw-alias-label-primary);
|
|
335
|
+
font-family: inherit;
|
|
336
|
+
}
|
|
337
|
+
.dsh-mg-dialog-head {
|
|
338
|
+
display: flex;
|
|
339
|
+
align-items: flex-start;
|
|
340
|
+
justify-content: space-between;
|
|
341
|
+
gap: 16px;
|
|
342
|
+
}
|
|
343
|
+
.dsh-mg-dialog h2 { margin: 0; font-size: 18px; line-height: 26px; font-weight: 600; }
|
|
344
|
+
.dsh-mg-dialog .dsh-mg-intro { margin-top: 6px; font-size: 13px; line-height: 20px; }
|
|
345
|
+
.dsh-mg-dialog-path {
|
|
346
|
+
display: block;
|
|
347
|
+
padding: 6px 8px;
|
|
348
|
+
border-radius: var(--dsw-radius-sm);
|
|
349
|
+
background: var(--dsw-alias-fill-tsp-secondary);
|
|
350
|
+
overflow-wrap: anywhere;
|
|
351
|
+
white-space: normal;
|
|
352
|
+
}
|
|
353
|
+
.dsh-mg-icon-btn {
|
|
354
|
+
display: inline-flex;
|
|
355
|
+
flex: none;
|
|
356
|
+
align-items: center;
|
|
357
|
+
justify-content: center;
|
|
358
|
+
width: 28px;
|
|
359
|
+
height: 28px;
|
|
360
|
+
padding: 0;
|
|
361
|
+
border: 0;
|
|
362
|
+
border-radius: var(--dsw-radius-sm);
|
|
363
|
+
background: transparent;
|
|
364
|
+
color: var(--dsw-alias-label-secondary);
|
|
365
|
+
font: inherit;
|
|
366
|
+
font-size: 18px;
|
|
367
|
+
line-height: 1;
|
|
368
|
+
cursor: pointer;
|
|
369
|
+
}
|
|
370
|
+
.dsh-mg-icon-btn:hover { background: var(--dsw-alias-interactive-bg-hover); }
|
|
371
|
+
`
|
|
372
|
+
|
|
373
|
+
/** Styles for the composer badge and the guarded send button. */
|
|
374
|
+
const GUARD_CSS = `
|
|
375
|
+
.dsh-mg-badge {
|
|
376
|
+
display: inline-flex;
|
|
377
|
+
flex: none;
|
|
378
|
+
align-items: center;
|
|
379
|
+
gap: 4px;
|
|
380
|
+
height: 24px;
|
|
381
|
+
padding: 0 7px;
|
|
382
|
+
border: 1px solid color-mix(in srgb, var(--dsw-alias-state-error-primary) 28%, transparent);
|
|
383
|
+
border-radius: 999px;
|
|
384
|
+
background: color-mix(in srgb, var(--dsw-alias-state-error-primary) 10%, transparent);
|
|
385
|
+
color: var(--dsw-alias-state-error-primary);
|
|
386
|
+
font-family: inherit;
|
|
387
|
+
font-size: 11px;
|
|
388
|
+
line-height: 16px;
|
|
389
|
+
white-space: nowrap;
|
|
390
|
+
}
|
|
391
|
+
.dsh-mg-badge svg { flex: none; }
|
|
392
|
+
@media (max-width: 640px) {
|
|
393
|
+
.dsh-mg-badge-text { display: none; }
|
|
394
|
+
.dsh-mg-badge { width: 24px; padding: 0; justify-content: center; }
|
|
395
|
+
}
|
|
396
|
+
/* The slot renderer wraps the model seat in a stable data-slot anchor. */
|
|
397
|
+
[data-composer-card][data-dsh-model-guard="blocked"] [data-slot="conversation.input.model"] button {
|
|
398
|
+
background: var(--dsw-alias-state-error-primary, #d92d20) !important;
|
|
399
|
+
color: var(--dsw-alias-label-on-color, #ffffff) !important;
|
|
400
|
+
cursor: not-allowed !important;
|
|
401
|
+
}
|
|
402
|
+
/* The primary action stays uncolored while a turn is running (it is Stop);
|
|
403
|
+
only the idle send button carries the send-specific marker. */
|
|
404
|
+
[data-composer-card][data-dsh-model-guard-send="blocked"] > div:last-child > div:last-child > button:last-child {
|
|
405
|
+
background: var(--dsw-alias-state-error-primary, #d92d20) !important;
|
|
406
|
+
color: var(--dsw-alias-label-on-color, #ffffff) !important;
|
|
407
|
+
opacity: 1 !important;
|
|
408
|
+
cursor: not-allowed !important;
|
|
409
|
+
}
|
|
410
|
+
`
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Normalize a directory path for storage and comparison.
|
|
414
|
+
* @param {unknown} input - user input.
|
|
415
|
+
* @returns {string} normalized path, or an empty string.
|
|
416
|
+
*/
|
|
417
|
+
function normalizeDirectory(input) {
|
|
418
|
+
if (typeof input !== 'string') return ''
|
|
419
|
+
let value = input.trim().replace(/\\/gu, '/')
|
|
420
|
+
if (value === '') return ''
|
|
421
|
+
value = value.replace(/\/+/gu, '/')
|
|
422
|
+
if (/^[A-Za-z]:\//u.test(value)) value = value[0].toUpperCase() + value.slice(1)
|
|
423
|
+
if (value.length > 1 && value.endsWith('/') && !/^[A-Za-z]:\/$/u.test(value)) value = value.slice(0, -1)
|
|
424
|
+
return value
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Whether a directory path is absolute enough for a rule.
|
|
429
|
+
* @param {string} directory - normalized path.
|
|
430
|
+
* @returns {boolean} whether it is absolute.
|
|
431
|
+
*/
|
|
432
|
+
function isAbsoluteDirectory(directory) {
|
|
433
|
+
return directory.startsWith('/') || /^[A-Za-z]:\//u.test(directory)
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Compare two directories under the same platform-like casing rules.
|
|
438
|
+
* @param {string} left - first normalized path.
|
|
439
|
+
* @param {string} right - second normalized path.
|
|
440
|
+
* @returns {boolean} whether they name the same directory.
|
|
441
|
+
*/
|
|
442
|
+
function sameDirectory(left, right) {
|
|
443
|
+
const a = normalizeDirectory(left)
|
|
444
|
+
const b = normalizeDirectory(right)
|
|
445
|
+
if (/^[A-Za-z]:\//u.test(a) || /^[A-Za-z]:\//u.test(b)) return a.toLowerCase() === b.toLowerCase()
|
|
446
|
+
return a === b
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Whether one working directory falls inside a rule directory.
|
|
451
|
+
* @param {string} directory - normalized working directory.
|
|
452
|
+
* @param {string} ruleDirectory - normalized rule directory.
|
|
453
|
+
* @returns {boolean} whether the rule applies.
|
|
454
|
+
*/
|
|
455
|
+
function isWithin(directory, ruleDirectory) {
|
|
456
|
+
const dir = /^[A-Za-z]:\//u.test(directory) ? directory.toLowerCase() : directory
|
|
457
|
+
const rule = /^[A-Za-z]:\//u.test(ruleDirectory) ? ruleDirectory.toLowerCase() : ruleDirectory
|
|
458
|
+
if (dir === rule) return true
|
|
459
|
+
return dir.startsWith(rule.endsWith('/') ? rule : `${rule}/`)
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Normalize a list of rule records.
|
|
464
|
+
* @param {unknown} source - parsed storage or editor array.
|
|
465
|
+
* @returns {Array<{ directory: string, models: string[] }>} normalized rules.
|
|
466
|
+
*/
|
|
467
|
+
function normalizeRuleList(source) {
|
|
468
|
+
if (!Array.isArray(source)) return []
|
|
469
|
+
const rules = []
|
|
470
|
+
for (const row of source) {
|
|
471
|
+
if (row === null || typeof row !== 'object') continue
|
|
472
|
+
const directory = normalizeDirectory(row.directory)
|
|
473
|
+
if (directory === '') continue
|
|
474
|
+
const models = Array.isArray(row.models)
|
|
475
|
+
? [...new Set(row.models.filter(model => typeof model === 'string').map(model => model.trim()).filter(Boolean))]
|
|
476
|
+
: []
|
|
477
|
+
rules.push({ directory, models })
|
|
478
|
+
}
|
|
479
|
+
return rules
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Pick the deepest matching rule.
|
|
484
|
+
* @param {Array<{ directory: string, models: string[] }>} rules - normalized rules.
|
|
485
|
+
* @param {string} cwd - current working directory.
|
|
486
|
+
* @returns {{ directory: string, models: string[] } | undefined} matching rule.
|
|
487
|
+
*/
|
|
488
|
+
function findRule(rules, cwd) {
|
|
489
|
+
const directory = normalizeDirectory(cwd)
|
|
490
|
+
if (directory === '') return undefined
|
|
491
|
+
let found
|
|
492
|
+
for (const rule of rules) {
|
|
493
|
+
if (!isWithin(directory, rule.directory)) continue
|
|
494
|
+
if (found === undefined || rule.directory.length > found.directory.length) found = rule
|
|
495
|
+
}
|
|
496
|
+
return found
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Whether a selection is allowed by one rule.
|
|
501
|
+
* @param {{ models: string[] }} rule - matching rule.
|
|
502
|
+
* @param {{ provider: string, model: string }} selection - current model selection.
|
|
503
|
+
* @returns {boolean} whether the model is allowed.
|
|
504
|
+
*/
|
|
505
|
+
function isAllowed(rule, selection) {
|
|
506
|
+
const key = `${selection.provider}/${selection.model}`
|
|
507
|
+
return rule.models.some((entry) => {
|
|
508
|
+
return entry === '*'
|
|
509
|
+
|| entry === key
|
|
510
|
+
|| entry === selection.model
|
|
511
|
+
|| entry === `${selection.provider}/*`
|
|
512
|
+
})
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Read rules from a config form value.
|
|
517
|
+
* @param {unknown} value - form value such as `{ rules: [...] }`.
|
|
518
|
+
* @returns {Array<{ directory: string, models: string[] }>} normalized rules.
|
|
519
|
+
*/
|
|
520
|
+
function rulesOf(value) {
|
|
521
|
+
const source = value !== null && typeof value === 'object' ? value.rules : undefined
|
|
522
|
+
return normalizeRuleList(source)
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Create a small observable store for transient UI values.
|
|
527
|
+
* @param {unknown} initial - initial value.
|
|
528
|
+
* @returns {{
|
|
529
|
+
* getSnapshot: () => unknown,
|
|
530
|
+
* subscribe: (listener: () => void) => () => void,
|
|
531
|
+
* set: (next: unknown) => void,
|
|
532
|
+
* }} value store.
|
|
533
|
+
*/
|
|
534
|
+
function createValueStore(initial) {
|
|
535
|
+
const listeners = new Set()
|
|
536
|
+
let snapshot = initial
|
|
537
|
+
return {
|
|
538
|
+
getSnapshot: () => snapshot,
|
|
539
|
+
subscribe: (listener) => {
|
|
540
|
+
listeners.add(listener)
|
|
541
|
+
return () => { listeners.delete(listener) }
|
|
542
|
+
},
|
|
543
|
+
set: (next) => {
|
|
544
|
+
if (Object.is(next, snapshot)) return
|
|
545
|
+
snapshot = next
|
|
546
|
+
for (const listener of listeners) listener()
|
|
547
|
+
},
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Subscribe a component to an observable store.
|
|
553
|
+
* @param {{ getSnapshot: () => unknown, subscribe: (listener: () => void) => () => void }} store - store.
|
|
554
|
+
* @returns {unknown} current snapshot.
|
|
555
|
+
*/
|
|
556
|
+
function useStoreSnapshot(store) {
|
|
557
|
+
return React.useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot)
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Subscribe to a Host config form and parse its rules.
|
|
562
|
+
* @param {{ getSnapshot: () => { value?: unknown }, subscribe: Function }} form - config form.
|
|
563
|
+
* @returns {{ snapshot: object, rules: Array<{ directory: string, models: string[] }> }} form state.
|
|
564
|
+
*/
|
|
565
|
+
function useRules(form) {
|
|
566
|
+
const subscribe = React.useCallback((listener) => form.subscribe(listener), [form])
|
|
567
|
+
const getSnapshot = React.useCallback(() => form.getSnapshot(), [form])
|
|
568
|
+
const snapshot = React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot)
|
|
569
|
+
const rules = React.useMemo(() => rulesOf(snapshot.value), [snapshot.value])
|
|
570
|
+
return { snapshot, rules }
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Load the Host model catalog for an editor.
|
|
575
|
+
* @param {{ session?: { modelCatalog?: () => Promise<object> } } | undefined} remote - client remote service.
|
|
576
|
+
* @returns {{ status: string, catalog?: object, message?: string }} catalog state.
|
|
577
|
+
*/
|
|
578
|
+
function useModelCatalog(remote) {
|
|
579
|
+
const [catalog, setCatalog] = React.useState({ status: 'loading' })
|
|
580
|
+
React.useEffect(() => {
|
|
581
|
+
let live = true
|
|
582
|
+
const sessionRemote = remote === undefined ? undefined : remote.session
|
|
583
|
+
if (sessionRemote === undefined || typeof sessionRemote.modelCatalog !== 'function') {
|
|
584
|
+
setCatalog({ status: 'error', message: 'remote.session.modelCatalog is unavailable' })
|
|
585
|
+
return () => { live = false }
|
|
586
|
+
}
|
|
587
|
+
sessionRemote.modelCatalog().then((result) => {
|
|
588
|
+
if (!live) return
|
|
589
|
+
if (result !== undefined && result.ok) setCatalog({ status: 'ready', catalog: result.value })
|
|
590
|
+
else setCatalog({ status: 'error', message: result?.error?.message ?? 'unknown error' })
|
|
591
|
+
}).catch((cause) => {
|
|
592
|
+
if (live) setCatalog({ status: 'error', message: cause instanceof Error ? cause.message : String(cause) })
|
|
593
|
+
})
|
|
594
|
+
return () => { live = false }
|
|
595
|
+
}, [])
|
|
596
|
+
return catalog
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* Resolve a catalog model key to its display name.
|
|
601
|
+
* @param {{ status: string, catalog?: object }} catalog - catalog state.
|
|
602
|
+
* @param {string} key - `provider/model`.
|
|
603
|
+
* @returns {string} display name, or the key.
|
|
604
|
+
*/
|
|
605
|
+
function modelLabel(catalog, key) {
|
|
606
|
+
if (catalog.status !== 'ready') return key
|
|
607
|
+
for (const group of catalog.catalog?.groups ?? []) {
|
|
608
|
+
for (const model of group.models) {
|
|
609
|
+
if (`${group.id}/${model.id}` === key) return model.name
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
return key
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Checkbox list over the Host model catalog.
|
|
617
|
+
* @param {{ t: Function, catalog: object, selected: string[], disabled?: boolean, onToggle: (key: string) => void }} props - picker props.
|
|
618
|
+
* @returns {import('react').ReactElement} picker content.
|
|
619
|
+
*/
|
|
620
|
+
function ModelPicker({ t, catalog, selected, disabled = false, onToggle }) {
|
|
621
|
+
if (catalog.status === 'loading') return h('p', { className: 'dsh-mg-hint' }, t('loadingModels'))
|
|
622
|
+
if (catalog.status !== 'ready') {
|
|
623
|
+
return h('div', { className: 'dsh-mg-notice', 'data-tone': 'error' },
|
|
624
|
+
t('catalogError', { message: catalog.message ?? '' }))
|
|
625
|
+
}
|
|
626
|
+
const groups = catalog.catalog?.groups ?? []
|
|
627
|
+
if (groups.length === 0) return h('p', { className: 'dsh-mg-hint' }, t('noCatalogModels'))
|
|
628
|
+
const rows = []
|
|
629
|
+
for (const group of groups) {
|
|
630
|
+
rows.push(h('div', { className: 'dsh-mg-provider', key: `provider:${group.id}` }, group.name))
|
|
631
|
+
for (const model of group.models) {
|
|
632
|
+
const key = `${group.id}/${model.id}`
|
|
633
|
+
rows.push(h('label', { className: 'dsh-mg-model', key },
|
|
634
|
+
h('input', {
|
|
635
|
+
type: 'checkbox',
|
|
636
|
+
checked: selected.includes(key),
|
|
637
|
+
disabled,
|
|
638
|
+
onChange: () => { onToggle(key) },
|
|
639
|
+
}),
|
|
640
|
+
h('span', { className: 'dsh-mg-model-name', title: model.description ?? key }, model.name),
|
|
641
|
+
h('code', { title: key }, key)))
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
return h('div', { className: 'dsh-mg-models' }, rows)
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* Workspace context-menu glyph: a shield with a check.
|
|
649
|
+
* @returns {import('react').ReactElement} 14px icon.
|
|
650
|
+
*/
|
|
651
|
+
function WorkspaceMenuIcon() {
|
|
652
|
+
return h('svg', {
|
|
653
|
+
viewBox: '0 0 16 16',
|
|
654
|
+
width: 14,
|
|
655
|
+
height: 14,
|
|
656
|
+
'aria-hidden': true,
|
|
657
|
+
},
|
|
658
|
+
h('path', {
|
|
659
|
+
d: 'M8 1.6 3.4 3.3v4.2c0 3.1 1.9 5.2 4.6 6.7 2.7-1.5 4.6-3.6 4.6-6.7V3.3L8 1.6Z',
|
|
660
|
+
fill: 'none',
|
|
661
|
+
stroke: 'currentColor',
|
|
662
|
+
strokeWidth: 1.3,
|
|
663
|
+
}),
|
|
664
|
+
h('path', {
|
|
665
|
+
d: 'M6 7.9 7.4 9.3 10.1 6.5',
|
|
666
|
+
fill: 'none',
|
|
667
|
+
stroke: 'currentColor',
|
|
668
|
+
strokeWidth: 1.3,
|
|
669
|
+
strokeLinecap: 'round',
|
|
670
|
+
strokeLinejoin: 'round',
|
|
671
|
+
}))
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
/**
|
|
675
|
+
* Resolve the first visible workspace directory for the Settings page.
|
|
676
|
+
* @param {Function} useSessions - standard global Session selector hook.
|
|
677
|
+
* @returns {string} a workspace path, or an empty string.
|
|
678
|
+
*/
|
|
679
|
+
function useCurrentWorkspace(useSessions) {
|
|
680
|
+
return useSessions((state) => {
|
|
681
|
+
const rows = Object.values(state.byId)
|
|
682
|
+
const active = rows.find((row) => {
|
|
683
|
+
return (row.retainedBy?.mainView ?? 0) > 0
|
|
684
|
+
&& typeof row.cwd === 'string'
|
|
685
|
+
&& row.cwd !== ''
|
|
686
|
+
})
|
|
687
|
+
if (active !== undefined) return active.cwd
|
|
688
|
+
const any = rows.find((row) => typeof row.cwd === 'string' && row.cwd !== '')
|
|
689
|
+
return any === undefined ? '' : any.cwd
|
|
690
|
+
})
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Install the composer guard and the Settings page.
|
|
695
|
+
* @param {import('@deepseek-ai/cordis').Context} ctx - Client root context.
|
|
696
|
+
*/
|
|
697
|
+
function apply(ctx) {
|
|
698
|
+
ctx.effect(() => ctx.locale.register(NS, { zh, en }), 'model-guard: dictionaries')
|
|
699
|
+
|
|
700
|
+
const form = ctx.configForms.get(ENTRY_ID)
|
|
701
|
+
const workspaceRequest = createValueStore(null)
|
|
702
|
+
const boundT = ctx.locale.bind(NS)
|
|
703
|
+
/**
|
|
704
|
+
* Composer seat: reads the current Session's folder and model, raises or
|
|
705
|
+
* clears the composer block, and marks the card for the red send style.
|
|
706
|
+
* @param {Record<string, unknown>} props - Session-scoped slot props.
|
|
707
|
+
* @returns {import('react').ReactElement} hidden anchor plus an optional badge.
|
|
708
|
+
*/
|
|
709
|
+
function GuardSeat(props) {
|
|
710
|
+
const t = typeof props.t === 'function' ? props.t : (key) => key
|
|
711
|
+
const sessionId = props.sessionId
|
|
712
|
+
const { rules } = useRules(form)
|
|
713
|
+
const cwd = props.useSessions((state) => state.byId[sessionId]?.cwd ?? '')
|
|
714
|
+
const selection = props.useProjection(
|
|
715
|
+
'modelSelection',
|
|
716
|
+
(value) => value?.next ?? value?.lastUsed ?? null,
|
|
717
|
+
)
|
|
718
|
+
const running = props.useSession((state) => state.running) ?? false
|
|
719
|
+
const rule = findRule(rules, cwd)
|
|
720
|
+
const known = selection !== null && selection !== undefined
|
|
721
|
+
const blocked = rule !== undefined && known && !isAllowed(rule, selection)
|
|
722
|
+
const key = known ? `${selection.provider}/${selection.model}` : ''
|
|
723
|
+
const reason = blocked
|
|
724
|
+
? rule.models.length === 0
|
|
725
|
+
? t('blockedEmpty', { directory: rule.directory })
|
|
726
|
+
: t('blockedModel', { directory: rule.directory, model: key })
|
|
727
|
+
: null
|
|
728
|
+
const anchorRef = React.useRef(null)
|
|
729
|
+
|
|
730
|
+
React.useEffect(() => {
|
|
731
|
+
if (sessionId === undefined || !blocked || reason === null) return undefined
|
|
732
|
+
const blocks = ctx.conversation?.blocks
|
|
733
|
+
if (blocks === undefined) return undefined
|
|
734
|
+
blocks.set(sessionId, { reason })
|
|
735
|
+
return () => {
|
|
736
|
+
const current = blocks.storeFor(sessionId).getSnapshot()
|
|
737
|
+
if (current !== undefined && current.reason === reason) blocks.set(sessionId, undefined)
|
|
738
|
+
}
|
|
739
|
+
}, [blocked, reason, sessionId])
|
|
740
|
+
|
|
741
|
+
React.useEffect(() => {
|
|
742
|
+
const anchor = anchorRef.current
|
|
743
|
+
const card = anchor === null ? null : anchor.closest('[data-composer-card]')
|
|
744
|
+
if (card === null) return undefined
|
|
745
|
+
if (blocked) card.setAttribute('data-dsh-model-guard', 'blocked')
|
|
746
|
+
else card.removeAttribute('data-dsh-model-guard')
|
|
747
|
+
if (blocked && !running) card.setAttribute('data-dsh-model-guard-send', 'blocked')
|
|
748
|
+
else card.removeAttribute('data-dsh-model-guard-send')
|
|
749
|
+
return () => {
|
|
750
|
+
card.removeAttribute('data-dsh-model-guard')
|
|
751
|
+
card.removeAttribute('data-dsh-model-guard-send')
|
|
752
|
+
}
|
|
753
|
+
}, [blocked, running, sessionId])
|
|
754
|
+
|
|
755
|
+
return h(React.Fragment, null,
|
|
756
|
+
h('style', null, GUARD_CSS),
|
|
757
|
+
h('span', { ref: anchorRef, hidden: true, 'data-dsh-model-guard-anchor': '' }),
|
|
758
|
+
blocked
|
|
759
|
+
? h('span', {
|
|
760
|
+
className: 'dsh-mg-badge',
|
|
761
|
+
title: reason,
|
|
762
|
+
'aria-label': reason,
|
|
763
|
+
},
|
|
764
|
+
h('svg', {
|
|
765
|
+
viewBox: '0 0 16 16',
|
|
766
|
+
width: 12,
|
|
767
|
+
height: 12,
|
|
768
|
+
'aria-hidden': true,
|
|
769
|
+
},
|
|
770
|
+
h('path', {
|
|
771
|
+
d: 'M8 1.5 3.5 3.2v4.2c0 3.1 1.8 5.2 4.5 6.6 2.7-1.4 4.5-3.5 4.5-6.6V3.2L8 1.5Z',
|
|
772
|
+
fill: 'none',
|
|
773
|
+
stroke: 'currentColor',
|
|
774
|
+
strokeWidth: 1.4,
|
|
775
|
+
}),
|
|
776
|
+
h('path', {
|
|
777
|
+
d: 'M6.2 7.8 7.6 9.2 10.2 6.5',
|
|
778
|
+
fill: 'none',
|
|
779
|
+
stroke: 'currentColor',
|
|
780
|
+
strokeWidth: 1.4,
|
|
781
|
+
strokeLinecap: 'round',
|
|
782
|
+
strokeLinejoin: 'round',
|
|
783
|
+
})),
|
|
784
|
+
h('span', { className: 'dsh-mg-badge-text' }, t('badge')))
|
|
785
|
+
: null,
|
|
786
|
+
)
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
/**
|
|
790
|
+
* Settings section: edit the Host-backed directory rules.
|
|
791
|
+
* @param {Record<string, unknown>} props - root settings section props.
|
|
792
|
+
* @returns {import('react').ReactElement} section content.
|
|
793
|
+
*/
|
|
794
|
+
function SettingsPage(props) {
|
|
795
|
+
const t = typeof props.t === 'function' ? props.t : (key) => key
|
|
796
|
+
const { snapshot, rules } = useRules(form)
|
|
797
|
+
const currentCwd = useCurrentWorkspace(props.useSessions)
|
|
798
|
+
const catalog = useModelCatalog(ctx.remote)
|
|
799
|
+
const [editor, setEditor] = React.useState(null)
|
|
800
|
+
const [manual, setManual] = React.useState('')
|
|
801
|
+
const [error, setError] = React.useState(null)
|
|
802
|
+
const [notice, setNotice] = React.useState(null)
|
|
803
|
+
const [saving, setSaving] = React.useState(false)
|
|
804
|
+
const writable = snapshot.status === 'ready' && snapshot.writable
|
|
805
|
+
|
|
806
|
+
const openAdd = () => {
|
|
807
|
+
setEditor({ index: -1, directory: currentCwd, models: [] })
|
|
808
|
+
setManual('')
|
|
809
|
+
setError(null)
|
|
810
|
+
setNotice(null)
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
const openEdit = (index) => {
|
|
814
|
+
const rule = rules[index]
|
|
815
|
+
if (rule === undefined) return
|
|
816
|
+
setEditor({ index, directory: rule.directory, models: [...rule.models] })
|
|
817
|
+
setManual('')
|
|
818
|
+
setError(null)
|
|
819
|
+
setNotice(null)
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
const patchEditor = (patch) => {
|
|
823
|
+
setEditor((current) => current === null ? current : { ...current, ...patch })
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
const toggleModel = (key) => {
|
|
827
|
+
setEditor((current) => {
|
|
828
|
+
if (current === null) return current
|
|
829
|
+
const selected = current.models.includes(key)
|
|
830
|
+
return {
|
|
831
|
+
...current,
|
|
832
|
+
models: selected
|
|
833
|
+
? current.models.filter((model) => model !== key)
|
|
834
|
+
: [...current.models, key],
|
|
835
|
+
}
|
|
836
|
+
})
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
const addManual = () => {
|
|
840
|
+
const entries = manual.split(/[\s,]+/u).map((entry) => entry.trim()).filter(Boolean)
|
|
841
|
+
if (entries.length === 0) return
|
|
842
|
+
setEditor((current) => current === null
|
|
843
|
+
? current
|
|
844
|
+
: { ...current, models: [...new Set([...current.models, ...entries])] })
|
|
845
|
+
setManual('')
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
const writeRules = async (next) => {
|
|
849
|
+
if (!writable) {
|
|
850
|
+
setError(t(snapshot.status === 'unavailable' ? 'unavailable' : 'readOnly'))
|
|
851
|
+
return false
|
|
852
|
+
}
|
|
853
|
+
setSaving(true)
|
|
854
|
+
setError(null)
|
|
855
|
+
setNotice(null)
|
|
856
|
+
try {
|
|
857
|
+
const accepted = await form.set('rules', next)
|
|
858
|
+
if (!accepted) setError(t('errorSave'))
|
|
859
|
+
return accepted
|
|
860
|
+
} catch (cause) {
|
|
861
|
+
setError(t('errorWrite', { message: cause instanceof Error ? cause.message : String(cause) }))
|
|
862
|
+
return false
|
|
863
|
+
} finally {
|
|
864
|
+
setSaving(false)
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
const saveEditor = async () => {
|
|
869
|
+
if (editor === null || saving) return
|
|
870
|
+
const directory = normalizeDirectory(editor.directory)
|
|
871
|
+
if (directory === '') {
|
|
872
|
+
setError(t('errorDirectory'))
|
|
873
|
+
return
|
|
874
|
+
}
|
|
875
|
+
if (!isAbsoluteDirectory(directory)) {
|
|
876
|
+
setError(t('errorAbsolute'))
|
|
877
|
+
return
|
|
878
|
+
}
|
|
879
|
+
const duplicate = rules.some((rule, index) => index !== editor.index && sameDirectory(rule.directory, directory))
|
|
880
|
+
if (duplicate) {
|
|
881
|
+
setError(t('errorDuplicate'))
|
|
882
|
+
return
|
|
883
|
+
}
|
|
884
|
+
const next = rules.map((rule) => ({ directory: rule.directory, models: [...rule.models] }))
|
|
885
|
+
const rule = {
|
|
886
|
+
directory,
|
|
887
|
+
models: [...new Set(editor.models.map((entry) => entry.trim()).filter(Boolean))],
|
|
888
|
+
}
|
|
889
|
+
if (editor.index >= 0 && editor.index < next.length) next[editor.index] = rule
|
|
890
|
+
else next.push(rule)
|
|
891
|
+
const accepted = await writeRules(next)
|
|
892
|
+
if (accepted) {
|
|
893
|
+
setEditor(null)
|
|
894
|
+
setNotice(t('saved'))
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
const removeRule = async (index) => {
|
|
899
|
+
const accepted = await writeRules(rules.filter((_rule, position) => position !== index))
|
|
900
|
+
if (accepted) {
|
|
901
|
+
setEditor(null)
|
|
902
|
+
setNotice(t('removed'))
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
const labelOf = (key) => modelLabel(catalog, key)
|
|
907
|
+
|
|
908
|
+
const editorCard = () => {
|
|
909
|
+
if (editor === null) return null
|
|
910
|
+
const selectedChips = editor.models.map((key) => h('span', { className: 'dsh-mg-chip', key, title: key },
|
|
911
|
+
labelOf(key),
|
|
912
|
+
h('button', {
|
|
913
|
+
type: 'button',
|
|
914
|
+
'aria-label': `${t('remove')} ${key}`,
|
|
915
|
+
onClick: () => { toggleModel(key) },
|
|
916
|
+
}, '×')))
|
|
917
|
+
return h('section', { className: 'dsh-mg-card dsh-mg-editor' },
|
|
918
|
+
h('h3', null, editor.index >= 0 ? t('editTitle') : t('addTitle')),
|
|
919
|
+
h('div', { className: 'dsh-mg-field' },
|
|
920
|
+
h('label', { className: 'dsh-mg-label' }, t('directory')),
|
|
921
|
+
h('div', { className: 'dsh-mg-row' },
|
|
922
|
+
h('input', {
|
|
923
|
+
className: 'dsh-mg-input',
|
|
924
|
+
value: editor.directory,
|
|
925
|
+
placeholder: t('directoryPlaceholder'),
|
|
926
|
+
spellCheck: false,
|
|
927
|
+
disabled: saving,
|
|
928
|
+
onChange: (event) => { patchEditor({ directory: event.target.value }) },
|
|
929
|
+
}),
|
|
930
|
+
h('button', {
|
|
931
|
+
type: 'button',
|
|
932
|
+
className: 'dsh-mg-btn',
|
|
933
|
+
disabled: saving || currentCwd === '',
|
|
934
|
+
title: currentCwd === '' ? t('noCurrent') : currentCwd,
|
|
935
|
+
onClick: () => { patchEditor({ directory: currentCwd }) },
|
|
936
|
+
}, t('useCurrent'))),
|
|
937
|
+
h('p', { className: 'dsh-mg-hint' }, t('directoryHint'))),
|
|
938
|
+
h('div', { className: 'dsh-mg-field' },
|
|
939
|
+
h('div', { className: 'dsh-mg-label-row' },
|
|
940
|
+
h('span', { className: 'dsh-mg-label' }, t('allowedModels')),
|
|
941
|
+
h('span', { className: 'dsh-mg-hint' }, t('modelsHint')))),
|
|
942
|
+
h(ModelPicker, { t, catalog, selected: editor.models, disabled: saving || !writable, onToggle: toggleModel }),
|
|
943
|
+
h('div', { className: 'dsh-mg-field' },
|
|
944
|
+
h('span', { className: 'dsh-mg-label' }, t('selectedModels')),
|
|
945
|
+
editor.models.length === 0
|
|
946
|
+
? h('span', { className: 'dsh-mg-warn' }, t('noneAllowed'))
|
|
947
|
+
: h('div', { className: 'dsh-mg-chips' }, selectedChips)),
|
|
948
|
+
h('div', { className: 'dsh-mg-row' },
|
|
949
|
+
h('input', {
|
|
950
|
+
className: 'dsh-mg-input',
|
|
951
|
+
value: manual,
|
|
952
|
+
placeholder: t('manualPlaceholder'),
|
|
953
|
+
spellCheck: false,
|
|
954
|
+
disabled: saving,
|
|
955
|
+
onChange: (event) => { setManual(event.target.value) },
|
|
956
|
+
onKeyDown: (event) => {
|
|
957
|
+
if (event.key === 'Enter') {
|
|
958
|
+
event.preventDefault()
|
|
959
|
+
addManual()
|
|
960
|
+
}
|
|
961
|
+
},
|
|
962
|
+
}),
|
|
963
|
+
h('button', {
|
|
964
|
+
type: 'button',
|
|
965
|
+
className: 'dsh-mg-btn',
|
|
966
|
+
disabled: saving || manual.trim() === '',
|
|
967
|
+
onClick: addManual,
|
|
968
|
+
}, t('add'))),
|
|
969
|
+
h('div', { className: 'dsh-mg-actions' },
|
|
970
|
+
h('button', {
|
|
971
|
+
type: 'button',
|
|
972
|
+
className: 'dsh-mg-btn',
|
|
973
|
+
disabled: saving,
|
|
974
|
+
onClick: () => { setEditor(null); setError(null) },
|
|
975
|
+
}, t('cancel')),
|
|
976
|
+
h('button', {
|
|
977
|
+
type: 'button',
|
|
978
|
+
className: 'dsh-mg-btn primary',
|
|
979
|
+
disabled: saving || !writable,
|
|
980
|
+
onClick: () => { void saveEditor() },
|
|
981
|
+
}, saving ? t('saving') : t('save'))))
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
const ruleCard = (rule, index) => {
|
|
985
|
+
const chips = rule.models.slice(0, 6).map((key) => h('span', { className: 'dsh-mg-chip', key, title: key }, labelOf(key)))
|
|
986
|
+
if (rule.models.length > 6) {
|
|
987
|
+
chips.push(h('span', { className: 'dsh-mg-chip', key: 'more' }, `+${rule.models.length - 6}`))
|
|
988
|
+
}
|
|
989
|
+
return h('article', { className: 'dsh-mg-card', key: `${rule.directory}:${index}` },
|
|
990
|
+
h('div', { className: 'dsh-mg-card-head' },
|
|
991
|
+
h('code', { className: 'dsh-mg-path', title: rule.directory }, rule.directory),
|
|
992
|
+
h('span', { className: 'dsh-mg-count' }, t('modelCount', { count: rule.models.length }))),
|
|
993
|
+
rule.models.length === 0
|
|
994
|
+
? h('span', { className: 'dsh-mg-warn' }, t('noneAllowed'))
|
|
995
|
+
: h('div', { className: 'dsh-mg-chips' }, chips),
|
|
996
|
+
h('div', { className: 'dsh-mg-actions' },
|
|
997
|
+
h('button', {
|
|
998
|
+
type: 'button',
|
|
999
|
+
className: 'dsh-mg-btn',
|
|
1000
|
+
disabled: saving || !writable,
|
|
1001
|
+
onClick: () => { openEdit(index) },
|
|
1002
|
+
}, t('edit')),
|
|
1003
|
+
h('button', {
|
|
1004
|
+
type: 'button',
|
|
1005
|
+
className: 'dsh-mg-btn danger',
|
|
1006
|
+
disabled: saving || !writable,
|
|
1007
|
+
onClick: () => { void removeRule(index) },
|
|
1008
|
+
}, t('remove'))))
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
const header = h('header', null,
|
|
1012
|
+
h('h2', null, t('title')),
|
|
1013
|
+
h('p', { className: 'dsh-mg-intro' }, t('intro')))
|
|
1014
|
+
|
|
1015
|
+
if (snapshot.status === 'loading') {
|
|
1016
|
+
return h('section', { className: 'dsh-mg' },
|
|
1017
|
+
h('style', null, PAGE_CSS),
|
|
1018
|
+
header,
|
|
1019
|
+
h('p', { className: 'dsh-mg-hint' }, t('loadingRules')))
|
|
1020
|
+
}
|
|
1021
|
+
if (snapshot.status === 'unavailable') {
|
|
1022
|
+
return h('section', { className: 'dsh-mg' },
|
|
1023
|
+
h('style', null, PAGE_CSS),
|
|
1024
|
+
header,
|
|
1025
|
+
h('div', { className: 'dsh-mg-notice', 'data-tone': 'error' }, t('unavailable')))
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
return h('section', { className: 'dsh-mg' },
|
|
1029
|
+
h('style', null, PAGE_CSS),
|
|
1030
|
+
header,
|
|
1031
|
+
writable
|
|
1032
|
+
? null
|
|
1033
|
+
: h('div', { className: 'dsh-mg-notice', 'data-tone': 'error' }, t('readOnly')),
|
|
1034
|
+
error === null ? null : h('div', { className: 'dsh-mg-notice', 'data-tone': 'error', role: 'alert' }, error),
|
|
1035
|
+
notice === null ? null : h('div', { className: 'dsh-mg-notice', 'data-tone': 'ok', role: 'status' }, notice),
|
|
1036
|
+
h('div', { className: 'dsh-mg-toolbar' },
|
|
1037
|
+
h('button', {
|
|
1038
|
+
type: 'button',
|
|
1039
|
+
className: 'dsh-mg-btn primary',
|
|
1040
|
+
disabled: saving || !writable,
|
|
1041
|
+
onClick: openAdd,
|
|
1042
|
+
}, t('addRule'))),
|
|
1043
|
+
editorCard(),
|
|
1044
|
+
rules.length === 0
|
|
1045
|
+
? h('div', { className: 'dsh-mg-empty' }, t('noRules'))
|
|
1046
|
+
: h('div', { className: 'dsh-mg-list' }, rules.map(ruleCard)))
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
/**
|
|
1050
|
+
* Quick rule editor opened from one Workspace header menu. The directory
|
|
1051
|
+
* is fixed to the clicked Workspace; the Settings page remains the place
|
|
1052
|
+
* for arbitrary paths.
|
|
1053
|
+
* @param {{ target: { path: string, displayTitle: string }, onClose: () => void, t: Function }} props - dialog props.
|
|
1054
|
+
* @returns {import('react').ReactElement} dialog.
|
|
1055
|
+
*/
|
|
1056
|
+
function WorkspaceRuleDialog({ target, onClose, t }) {
|
|
1057
|
+
const { snapshot, rules } = useRules(form)
|
|
1058
|
+
const catalog = useModelCatalog(ctx.remote)
|
|
1059
|
+
const existing = rules.find((rule) => sameDirectory(rule.directory, target.path))
|
|
1060
|
+
const effective = findRule(rules, target.path)
|
|
1061
|
+
const writable = snapshot.status === 'ready' && snapshot.writable
|
|
1062
|
+
const [models, setModels] = React.useState(null)
|
|
1063
|
+
const [manual, setManual] = React.useState('')
|
|
1064
|
+
const [error, setError] = React.useState(null)
|
|
1065
|
+
const [saving, setSaving] = React.useState(false)
|
|
1066
|
+
const dialogRef = React.useRef(null)
|
|
1067
|
+
const closeRef = React.useRef(null)
|
|
1068
|
+
const titleId = 'dsh-mg-dialog-title'
|
|
1069
|
+
|
|
1070
|
+
React.useEffect(() => {
|
|
1071
|
+
if (models !== null || snapshot.status !== 'ready') return
|
|
1072
|
+
if (existing !== undefined) setModels([...existing.models])
|
|
1073
|
+
else if (effective !== undefined) setModels([...effective.models])
|
|
1074
|
+
else setModels([])
|
|
1075
|
+
}, [models, snapshot.status, existing, effective])
|
|
1076
|
+
|
|
1077
|
+
React.useEffect(() => {
|
|
1078
|
+
const doc = globalThis.document
|
|
1079
|
+
if (doc === undefined) return undefined
|
|
1080
|
+
const previous = doc.activeElement
|
|
1081
|
+
const previousOverflow = doc.body.style.overflow
|
|
1082
|
+
doc.body.style.overflow = 'hidden'
|
|
1083
|
+
const onKeyDown = (event) => {
|
|
1084
|
+
if (event.key === 'Escape') {
|
|
1085
|
+
event.preventDefault()
|
|
1086
|
+
onClose()
|
|
1087
|
+
return
|
|
1088
|
+
}
|
|
1089
|
+
if (event.key !== 'Tab' || dialogRef.current === null) return
|
|
1090
|
+
const focusable = [...dialogRef.current.querySelectorAll('button, input, [href], [tabindex]:not([tabindex="-1"])')]
|
|
1091
|
+
.filter((element) => !element.disabled)
|
|
1092
|
+
if (focusable.length === 0) return
|
|
1093
|
+
const first = focusable[0]
|
|
1094
|
+
const last = focusable[focusable.length - 1]
|
|
1095
|
+
if (event.shiftKey && doc.activeElement === first) {
|
|
1096
|
+
event.preventDefault()
|
|
1097
|
+
last.focus()
|
|
1098
|
+
} else if (!event.shiftKey && doc.activeElement === last) {
|
|
1099
|
+
event.preventDefault()
|
|
1100
|
+
first.focus()
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
doc.addEventListener('keydown', onKeyDown)
|
|
1104
|
+
const timer = globalThis.setTimeout(() => { closeRef.current?.focus() }, 0)
|
|
1105
|
+
return () => {
|
|
1106
|
+
globalThis.clearTimeout(timer)
|
|
1107
|
+
doc.removeEventListener('keydown', onKeyDown)
|
|
1108
|
+
doc.body.style.overflow = previousOverflow
|
|
1109
|
+
if (previous !== null && previous !== undefined && typeof previous.focus === 'function') previous.focus()
|
|
1110
|
+
}
|
|
1111
|
+
}, [])
|
|
1112
|
+
|
|
1113
|
+
const toggle = (key) => {
|
|
1114
|
+
setModels((current) => {
|
|
1115
|
+
const selected = current ?? []
|
|
1116
|
+
return selected.includes(key)
|
|
1117
|
+
? selected.filter((model) => model !== key)
|
|
1118
|
+
: [...selected, key]
|
|
1119
|
+
})
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
const addManual = () => {
|
|
1123
|
+
const entries = manual.split(/[\s,]+/u).map((entry) => entry.trim()).filter(Boolean)
|
|
1124
|
+
if (entries.length === 0) return
|
|
1125
|
+
setModels((current) => [...new Set([...(current ?? []), ...entries])])
|
|
1126
|
+
setManual('')
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
const save = async () => {
|
|
1130
|
+
if (saving) return
|
|
1131
|
+
if (!writable) {
|
|
1132
|
+
setError(t(snapshot.status === 'unavailable' ? 'unavailable' : 'readOnly'))
|
|
1133
|
+
return
|
|
1134
|
+
}
|
|
1135
|
+
const directory = normalizeDirectory(target.path)
|
|
1136
|
+
if (!isAbsoluteDirectory(directory)) {
|
|
1137
|
+
setError(t('errorAbsolute'))
|
|
1138
|
+
return
|
|
1139
|
+
}
|
|
1140
|
+
const next = rules.filter((rule) => !sameDirectory(rule.directory, directory))
|
|
1141
|
+
next.push({
|
|
1142
|
+
directory,
|
|
1143
|
+
models: [...new Set((models ?? []).map((entry) => entry.trim()).filter(Boolean))],
|
|
1144
|
+
})
|
|
1145
|
+
setSaving(true)
|
|
1146
|
+
setError(null)
|
|
1147
|
+
try {
|
|
1148
|
+
const accepted = await form.set('rules', next)
|
|
1149
|
+
if (accepted) onClose()
|
|
1150
|
+
else setError(t('errorSave'))
|
|
1151
|
+
} catch (cause) {
|
|
1152
|
+
setError(t('errorWrite', { message: cause instanceof Error ? cause.message : String(cause) }))
|
|
1153
|
+
} finally {
|
|
1154
|
+
setSaving(false)
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
const remove = async () => {
|
|
1159
|
+
if (saving) return
|
|
1160
|
+
if (!writable) {
|
|
1161
|
+
setError(t(snapshot.status === 'unavailable' ? 'unavailable' : 'readOnly'))
|
|
1162
|
+
return
|
|
1163
|
+
}
|
|
1164
|
+
const directory = normalizeDirectory(target.path)
|
|
1165
|
+
setSaving(true)
|
|
1166
|
+
setError(null)
|
|
1167
|
+
try {
|
|
1168
|
+
const accepted = await form.set('rules', rules.filter((rule) => !sameDirectory(rule.directory, directory)))
|
|
1169
|
+
if (accepted) onClose()
|
|
1170
|
+
else setError(t('errorSave'))
|
|
1171
|
+
} catch (cause) {
|
|
1172
|
+
setError(t('errorWrite', { message: cause instanceof Error ? cause.message : String(cause) }))
|
|
1173
|
+
} finally {
|
|
1174
|
+
setSaving(false)
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
const selectedChips = (models ?? []).map((key) => h('span', { className: 'dsh-mg-chip', key, title: key },
|
|
1179
|
+
modelLabel(catalog, key),
|
|
1180
|
+
h('button', {
|
|
1181
|
+
type: 'button',
|
|
1182
|
+
'aria-label': `${t('remove')} ${key}`,
|
|
1183
|
+
onClick: () => { toggle(key) },
|
|
1184
|
+
}, '×')))
|
|
1185
|
+
|
|
1186
|
+
return h(React.Fragment, null,
|
|
1187
|
+
h('style', null, PAGE_CSS),
|
|
1188
|
+
h('div', {
|
|
1189
|
+
className: 'dsh-mg-backdrop',
|
|
1190
|
+
onMouseDown: (event) => { if (event.target === event.currentTarget) onClose() },
|
|
1191
|
+
},
|
|
1192
|
+
h('div', {
|
|
1193
|
+
className: 'dsh-mg-dialog',
|
|
1194
|
+
role: 'dialog',
|
|
1195
|
+
'aria-modal': 'true',
|
|
1196
|
+
'aria-labelledby': titleId,
|
|
1197
|
+
ref: dialogRef,
|
|
1198
|
+
onMouseDown: (event) => { event.stopPropagation() },
|
|
1199
|
+
},
|
|
1200
|
+
h('div', { className: 'dsh-mg-dialog-head' },
|
|
1201
|
+
h('div', null,
|
|
1202
|
+
h('h2', { id: titleId }, t('dialogTitle')),
|
|
1203
|
+
h('p', { className: 'dsh-mg-intro' }, t('dialogDescription', { name: target.displayTitle || target.path }))),
|
|
1204
|
+
h('button', {
|
|
1205
|
+
ref: closeRef,
|
|
1206
|
+
type: 'button',
|
|
1207
|
+
className: 'dsh-mg-icon-btn',
|
|
1208
|
+
'aria-label': t('dialogClose'),
|
|
1209
|
+
onClick: onClose,
|
|
1210
|
+
}, '×')),
|
|
1211
|
+
error === null ? null : h('div', { className: 'dsh-mg-notice', 'data-tone': 'error', role: 'alert' }, error),
|
|
1212
|
+
snapshot.status === 'loading'
|
|
1213
|
+
? h('div', { className: 'dsh-mg-hint' }, t('loadingRules'))
|
|
1214
|
+
: writable
|
|
1215
|
+
? null
|
|
1216
|
+
: h('div', { className: 'dsh-mg-notice', 'data-tone': 'error' },
|
|
1217
|
+
t(snapshot.status === 'unavailable' ? 'unavailable' : 'readOnly')),
|
|
1218
|
+
h('div', { className: 'dsh-mg-field' },
|
|
1219
|
+
h('span', { className: 'dsh-mg-label' }, t('directory')),
|
|
1220
|
+
h('code', { className: 'dsh-mg-path dsh-mg-dialog-path', title: target.path }, target.path),
|
|
1221
|
+
existing === undefined && effective !== undefined
|
|
1222
|
+
? h('p', { className: 'dsh-mg-hint' }, t('dialogInherited', { directory: effective.directory }))
|
|
1223
|
+
: null),
|
|
1224
|
+
h('div', { className: 'dsh-mg-field' },
|
|
1225
|
+
h('div', { className: 'dsh-mg-label-row' },
|
|
1226
|
+
h('span', { className: 'dsh-mg-label' }, t('allowedModels')),
|
|
1227
|
+
h('span', { className: 'dsh-mg-hint' }, t('modelsHint')))),
|
|
1228
|
+
h(ModelPicker, { t, catalog, selected: models ?? [], disabled: saving || !writable, onToggle: toggle }),
|
|
1229
|
+
h('div', { className: 'dsh-mg-field' },
|
|
1230
|
+
h('span', { className: 'dsh-mg-label' }, t('selectedModels')),
|
|
1231
|
+
(models ?? []).length === 0
|
|
1232
|
+
? h('span', { className: 'dsh-mg-warn' }, t('noneAllowed'))
|
|
1233
|
+
: h('div', { className: 'dsh-mg-chips' }, selectedChips)),
|
|
1234
|
+
h('div', { className: 'dsh-mg-row' },
|
|
1235
|
+
h('input', {
|
|
1236
|
+
className: 'dsh-mg-input',
|
|
1237
|
+
value: manual,
|
|
1238
|
+
placeholder: t('manualPlaceholder'),
|
|
1239
|
+
spellCheck: false,
|
|
1240
|
+
disabled: saving || !writable,
|
|
1241
|
+
onChange: (event) => { setManual(event.target.value) },
|
|
1242
|
+
onKeyDown: (event) => {
|
|
1243
|
+
if (event.key === 'Enter') {
|
|
1244
|
+
event.preventDefault()
|
|
1245
|
+
addManual()
|
|
1246
|
+
}
|
|
1247
|
+
},
|
|
1248
|
+
}),
|
|
1249
|
+
h('button', {
|
|
1250
|
+
type: 'button',
|
|
1251
|
+
className: 'dsh-mg-btn',
|
|
1252
|
+
disabled: saving || !writable || manual.trim() === '',
|
|
1253
|
+
onClick: addManual,
|
|
1254
|
+
}, t('add'))),
|
|
1255
|
+
h('div', { className: 'dsh-mg-actions' },
|
|
1256
|
+
existing === undefined
|
|
1257
|
+
? null
|
|
1258
|
+
: h('button', {
|
|
1259
|
+
type: 'button',
|
|
1260
|
+
className: 'dsh-mg-btn danger',
|
|
1261
|
+
disabled: saving || !writable,
|
|
1262
|
+
onClick: () => { void remove() },
|
|
1263
|
+
}, t('dialogDelete')),
|
|
1264
|
+
h('button', {
|
|
1265
|
+
type: 'button',
|
|
1266
|
+
className: 'dsh-mg-btn',
|
|
1267
|
+
disabled: saving,
|
|
1268
|
+
onClick: onClose,
|
|
1269
|
+
}, t('cancel')),
|
|
1270
|
+
h('button', {
|
|
1271
|
+
type: 'button',
|
|
1272
|
+
className: 'dsh-mg-btn primary',
|
|
1273
|
+
disabled: saving || !writable,
|
|
1274
|
+
onClick: () => { void save() },
|
|
1275
|
+
}, saving ? t('saving') : t('save'))))))
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
/**
|
|
1279
|
+
* Host of the Workspace dialog; renders only while a menu action asked
|
|
1280
|
+
* for one.
|
|
1281
|
+
* @param {{ t: Function }} props - overlay props.
|
|
1282
|
+
* @returns {import('react').ReactElement | null} dialog or nothing.
|
|
1283
|
+
*/
|
|
1284
|
+
function WorkspaceRuleHost(props) {
|
|
1285
|
+
const t = typeof props.t === 'function' ? props.t : (key) => key
|
|
1286
|
+
const target = useStoreSnapshot(workspaceRequest)
|
|
1287
|
+
if (target === null) return null
|
|
1288
|
+
return h(WorkspaceRuleDialog, {
|
|
1289
|
+
key: target.path,
|
|
1290
|
+
target,
|
|
1291
|
+
t,
|
|
1292
|
+
onClose: () => { workspaceRequest.set(null) },
|
|
1293
|
+
})
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
ctx.slots.inject('conversation.input.right', () => ctx.slots.register({
|
|
1297
|
+
name: 'conversation.input.right',
|
|
1298
|
+
id: 'model-guard',
|
|
1299
|
+
locale: NS,
|
|
1300
|
+
}, GuardSeat))
|
|
1301
|
+
|
|
1302
|
+
ctx.slots.inject('settings.section', () => ctx.slots.register({
|
|
1303
|
+
name: 'settings.section',
|
|
1304
|
+
id: 'model-guard',
|
|
1305
|
+
order: 55,
|
|
1306
|
+
label: () => boundT('nav'),
|
|
1307
|
+
locale: NS,
|
|
1308
|
+
}, SettingsPage))
|
|
1309
|
+
|
|
1310
|
+
ctx.inject(['contextMenus'], (scope) => {
|
|
1311
|
+
scope.effect(() => scope.contextMenus.register({
|
|
1312
|
+
id: 'dsh-model-guard.workspace-rules',
|
|
1313
|
+
target: 'workspace',
|
|
1314
|
+
order: 200,
|
|
1315
|
+
observe: () => [form],
|
|
1316
|
+
resolve: ({ path, displayTitle }) => {
|
|
1317
|
+
if (typeof path !== 'string' || path === '') return []
|
|
1318
|
+
const hasRule = rulesOf(form.getSnapshot().value).some((rule) => sameDirectory(rule.directory, path))
|
|
1319
|
+
return [{
|
|
1320
|
+
id: 'set-rules',
|
|
1321
|
+
label: boundT(hasRule ? 'menuEditRules' : 'menuSetRules'),
|
|
1322
|
+
icon: { key: 'workspace.model-guard' },
|
|
1323
|
+
run: () => { workspaceRequest.set({ path, displayTitle }) },
|
|
1324
|
+
}]
|
|
1325
|
+
},
|
|
1326
|
+
}), 'model-guard: workspace menu action')
|
|
1327
|
+
|
|
1328
|
+
scope.slots.inject('contextMenu.icon', () => scope.slots.register({
|
|
1329
|
+
name: 'contextMenu.icon',
|
|
1330
|
+
key: 'workspace.model-guard',
|
|
1331
|
+
}, WorkspaceMenuIcon))
|
|
1332
|
+
|
|
1333
|
+
scope.slots.inject('shell.overlay', () => scope.slots.register({
|
|
1334
|
+
name: 'shell.overlay',
|
|
1335
|
+
id: 'model-guard-workspace-rule',
|
|
1336
|
+
locale: NS,
|
|
1337
|
+
}, WorkspaceRuleHost))
|
|
1338
|
+
})
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
return {
|
|
1342
|
+
/** Services the guard and its Settings page read. */
|
|
1343
|
+
inject: ['slots', 'sessions', 'locale', 'conversation', 'configForms', 'remote', 'remote.session'],
|
|
1344
|
+
apply,
|
|
1345
|
+
}
|
|
1346
|
+
},
|
|
1347
|
+
})
|