dsh-data-window 0.0.0-stage → 0.1.2
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/AGENTS.md +111 -0
- package/CHANGELOG.md +267 -0
- package/LICENSE +21 -0
- package/README.md +151 -2
- package/client.js +8271 -0
- package/cordis.patch.yml +4 -0
- package/docs/BRIDGE.md +106 -0
- package/docs/DESIGN.md +417 -0
- package/docs/DETAIL.md +242 -0
- package/docs/screenshots/ddl.png +0 -0
- package/docs/screenshots/workbench.png +0 -0
- package/icon.svg +30 -0
- package/index.js +180 -0
- package/locale/en.json +304 -0
- package/locale/zh.json +304 -0
- package/package.json +68 -4
- package/src/client/00-css.js +1110 -0
- package/src/client/05-icons.js +156 -0
- package/src/client/05-theme.js +107 -0
- package/src/client/06-shortcuts.js +89 -0
- package/src/client/07-bridge.js +202 -0
- package/src/client/10-core.js +1320 -0
- package/src/client/20-tree.js +293 -0
- package/src/client/25-sql.js +631 -0
- package/src/client/30-editor.js +742 -0
- package/src/client/40-results.js +556 -0
- package/src/client/45-export.js +219 -0
- package/src/client/50-panel.js +768 -0
- package/src/client/60-settings.js +180 -0
- package/src/client/63-schema.js +186 -0
- package/src/client/64-manual.js +350 -0
- package/src/client/65-ai.js +320 -0
- package/src/client/66-optimize.js +217 -0
- package/src/client/90-apply.js +183 -0
- package/src/host/ai.js +721 -0
- package/src/host/cache.js +157 -0
- package/src/host/catalog.js +161 -0
- package/src/host/consoles.js +116 -0
- package/src/host/discovery.js +603 -0
- package/src/host/manual.js +276 -0
- package/src/host/mysql.js +650 -0
- package/src/host/remembered.js +225 -0
- package/src/host/rpc.js +487 -0
- package/src/host/shared.js +180 -0
- package/src/host/yaml.js +335 -0
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Manually added connections.
|
|
3
|
+
*
|
|
4
|
+
* The rule scan and the AI fallback both try to derive a connection from the
|
|
5
|
+
* workspace. When the database a user needs is described nowhere — a local
|
|
6
|
+
* container, a shared test instance, a customer's replica — this form is the third
|
|
7
|
+
* path. The window keeps these entries per workspace, and the Host proves the
|
|
8
|
+
* connection once before saving it, so a typo fails here instead of producing a
|
|
9
|
+
* tree row that never opens.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Empty form state; the port defaults to MySQL's. */
|
|
13
|
+
const DW_MANUAL_FORM = { label: '', host: '127.0.0.1', port: '3306', user: '', password: '', database: '' }
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The "add a connection" modal.
|
|
17
|
+
*
|
|
18
|
+
* @param props `{ store, tr, onClose, onSaved }`; `onSaved` refreshes the tree
|
|
19
|
+
*/
|
|
20
|
+
function DwManualDialog({ store, tr, onClose, onSaved }) {
|
|
21
|
+
const [form, setForm] = useState(() => ({ ...DW_MANUAL_FORM }))
|
|
22
|
+
const [connections, setConnections] = useState([])
|
|
23
|
+
const [sourceId, setSourceId] = useState('')
|
|
24
|
+
const [saving, setSaving] = useState(false)
|
|
25
|
+
const [error, setError] = useState('')
|
|
26
|
+
const savingRef = useRef(false)
|
|
27
|
+
savingRef.current = saving
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
const onKey = (event) => {
|
|
31
|
+
if (event.key !== 'Escape' || savingRef.current) return
|
|
32
|
+
onClose()
|
|
33
|
+
}
|
|
34
|
+
globalThis.addEventListener('keydown', onKey)
|
|
35
|
+
return () => globalThis.removeEventListener('keydown', onKey)
|
|
36
|
+
}, [onClose])
|
|
37
|
+
|
|
38
|
+
// Other workspaces usually point at the same hosts and accounts, so the form
|
|
39
|
+
// offers what this machine already resolved instead of asking for it again.
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
let alive = true
|
|
42
|
+
dwAiWithTimeout(dwRpc('savedConnections'), 10000).then((value) => {
|
|
43
|
+
if (!alive) return
|
|
44
|
+
setConnections(Array.isArray(value?.connections) ? value.connections : [])
|
|
45
|
+
}).catch(() => {
|
|
46
|
+
// Without the list the form still works; it is a convenience, not a dependency.
|
|
47
|
+
})
|
|
48
|
+
return () => {
|
|
49
|
+
alive = false
|
|
50
|
+
}
|
|
51
|
+
}, [])
|
|
52
|
+
|
|
53
|
+
const update = (key) => (event) => {
|
|
54
|
+
setForm({ ...form, [key]: event.target.value })
|
|
55
|
+
// Editing the identity of the connection detaches it from the imported one, so
|
|
56
|
+
// its stored password is not silently reused for a different host or account.
|
|
57
|
+
if (key === 'host' || key === 'port' || key === 'user' || key === 'password') setSourceId('')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const chooseSource = (event) => {
|
|
61
|
+
const id = event.target.value
|
|
62
|
+
const entry = connections.find((item) => item.id === id)
|
|
63
|
+
setSourceId(entry === undefined ? '' : id)
|
|
64
|
+
if (entry === undefined) return
|
|
65
|
+
// An imported connection carries the environment it was saved under ("prod"),
|
|
66
|
+
// which is what the user expects to see in the tree — but only when they have
|
|
67
|
+
// not named the new environment themselves.
|
|
68
|
+
const importedLabel = entry.labels.length > 0
|
|
69
|
+
? String(entry.labels[0]).split(' · ').pop().trim()
|
|
70
|
+
: ''
|
|
71
|
+
setForm({
|
|
72
|
+
...form,
|
|
73
|
+
label: form.label.trim() === '' ? importedLabel : form.label,
|
|
74
|
+
host: entry.host,
|
|
75
|
+
port: String(entry.port),
|
|
76
|
+
user: entry.user,
|
|
77
|
+
password: '',
|
|
78
|
+
database: entry.databases.length === 1 ? entry.databases[0] : form.database,
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const save = async () => {
|
|
83
|
+
if (saving === true) return
|
|
84
|
+
const root = String(store.getState?.()?.root ?? '')
|
|
85
|
+
if (root === '') {
|
|
86
|
+
setError(tr('manualNoWorkspace'))
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
setSaving(true)
|
|
90
|
+
setError('')
|
|
91
|
+
try {
|
|
92
|
+
await dwRpc('manualSave', {
|
|
93
|
+
root,
|
|
94
|
+
entry: {
|
|
95
|
+
label: form.label,
|
|
96
|
+
host: form.host,
|
|
97
|
+
port: Number(form.port),
|
|
98
|
+
user: form.user,
|
|
99
|
+
password: form.password,
|
|
100
|
+
database: form.database,
|
|
101
|
+
},
|
|
102
|
+
sourceId,
|
|
103
|
+
})
|
|
104
|
+
if (typeof onSaved === 'function') onSaved()
|
|
105
|
+
onClose()
|
|
106
|
+
} catch (failure) {
|
|
107
|
+
setError(dwExplainFailure(tr, failure))
|
|
108
|
+
setSaving(false)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const field = (key, label, props, hint) => h('label', { key, className: 'dw-form-row' }, [
|
|
113
|
+
h('span', { key: 'l', className: 'dw-form-label' }, label),
|
|
114
|
+
h('input', {
|
|
115
|
+
key: 'i',
|
|
116
|
+
className: 'dw-form-input',
|
|
117
|
+
value: form[key],
|
|
118
|
+
spellCheck: false,
|
|
119
|
+
autoComplete: 'off',
|
|
120
|
+
onChange: update(key),
|
|
121
|
+
...props,
|
|
122
|
+
}),
|
|
123
|
+
hint === undefined ? null : h('span', { key: 'h', className: 'dw-form-hint' }, hint),
|
|
124
|
+
])
|
|
125
|
+
|
|
126
|
+
return h('div', {
|
|
127
|
+
className: 'dw-modal',
|
|
128
|
+
onClick: (event) => {
|
|
129
|
+
if (event.target === event.currentTarget && saving !== true) onClose()
|
|
130
|
+
},
|
|
131
|
+
}, h('div', {
|
|
132
|
+
key: 'card',
|
|
133
|
+
className: 'dw-modal-card',
|
|
134
|
+
role: 'dialog',
|
|
135
|
+
'aria-modal': 'true',
|
|
136
|
+
'aria-label': tr('manualTitle'),
|
|
137
|
+
}, [
|
|
138
|
+
h('div', { key: 'title', className: 'dw-modal-title' }, [
|
|
139
|
+
h(DwIcon, { key: 'i', name: 'link', size: 15 }),
|
|
140
|
+
h('span', { key: 't' }, tr('manualTitle')),
|
|
141
|
+
]),
|
|
142
|
+
h('div', { key: 'body', className: 'dw-modal-body' }, [
|
|
143
|
+
h('div', { key: 'hint', className: 'dw-ai-note' }, tr('manualHint')),
|
|
144
|
+
h('div', { key: 'form', className: 'dw-form' }, [
|
|
145
|
+
connections.length === 0
|
|
146
|
+
? null
|
|
147
|
+
: h('label', { key: 'source', className: 'dw-form-row' }, [
|
|
148
|
+
h('span', { key: 'l', className: 'dw-form-label' }, tr('manualFromSaved')),
|
|
149
|
+
h('select', {
|
|
150
|
+
key: 's',
|
|
151
|
+
className: 'dw-form-input',
|
|
152
|
+
value: sourceId,
|
|
153
|
+
onChange: chooseSource,
|
|
154
|
+
}, [
|
|
155
|
+
h('option', { key: 'none', value: '' }, tr('manualFromSavedNone')),
|
|
156
|
+
...connections.map((entry) => h('option', {
|
|
157
|
+
key: entry.id,
|
|
158
|
+
value: entry.id,
|
|
159
|
+
}, `${entry.host}:${entry.port} · ${entry.user}${entry.labels.length > 0 ? ` · ${entry.labels[0]}` : ''}`)),
|
|
160
|
+
]),
|
|
161
|
+
h('span', { key: 'h', className: 'dw-form-hint' }, sourceId === '' ? tr('manualFromSavedHint') : tr('manualSourceHint')),
|
|
162
|
+
]),
|
|
163
|
+
field('label', tr('manualLabel'), { placeholder: tr('manualLabelPlaceholder') }, tr('manualLabelHint')),
|
|
164
|
+
h('div', { key: 'addr', className: 'dw-form-split' }, [
|
|
165
|
+
field('host', tr('manualHost'), { placeholder: 'mysql.internal' }),
|
|
166
|
+
field('port', tr('manualPort'), { inputMode: 'numeric', placeholder: '3306' }),
|
|
167
|
+
]),
|
|
168
|
+
field('user', tr('manualUser'), { placeholder: 'app_user' }),
|
|
169
|
+
field('password', tr('manualPassword'), { type: 'password' }),
|
|
170
|
+
field('database', tr('manualDatabase'), { placeholder: 'demo_order' }, tr('manualDatabaseHint')),
|
|
171
|
+
]),
|
|
172
|
+
error === ''
|
|
173
|
+
? null
|
|
174
|
+
: h('div', { key: 'err', className: 'dw-ai-error' }, h('div', { key: 'm', className: 'dw-ai-note' }, error)),
|
|
175
|
+
h('div', { key: 'privacy', className: 'dw-ai-note' }, tr('manualPrivacy')),
|
|
176
|
+
]),
|
|
177
|
+
h('div', { key: 'foot', className: 'dw-modal-foot' }, [
|
|
178
|
+
h('button', {
|
|
179
|
+
key: 'cancel',
|
|
180
|
+
type: 'button',
|
|
181
|
+
className: 'dw-btn',
|
|
182
|
+
disabled: saving === true,
|
|
183
|
+
onClick: onClose,
|
|
184
|
+
}, tr('cancel')),
|
|
185
|
+
h('button', {
|
|
186
|
+
key: 'save',
|
|
187
|
+
type: 'button',
|
|
188
|
+
className: 'dw-btn dw-btn-accent',
|
|
189
|
+
disabled: saving === true,
|
|
190
|
+
onClick: () => void save(),
|
|
191
|
+
}, saving === true ? tr('manualSaving') : tr('manualSave')),
|
|
192
|
+
]),
|
|
193
|
+
]))
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Rename one manual environment.
|
|
198
|
+
*
|
|
199
|
+
* The environment name is what the tree groups by, and it also feeds the derived
|
|
200
|
+
* instance ids — the Host moves the user's remembered databases along with the new
|
|
201
|
+
* name, so renaming never costs the schema selection.
|
|
202
|
+
*/
|
|
203
|
+
function DwRenameEnvDialog({ store, tr, label, onClose, onRenamed }) {
|
|
204
|
+
const [value, setValue] = useState(label)
|
|
205
|
+
const [saving, setSaving] = useState(false)
|
|
206
|
+
const [error, setError] = useState('')
|
|
207
|
+
const savingRef = useRef(false)
|
|
208
|
+
savingRef.current = saving
|
|
209
|
+
|
|
210
|
+
useEffect(() => {
|
|
211
|
+
const onKey = (event) => {
|
|
212
|
+
if (event.key !== 'Escape' || savingRef.current) return
|
|
213
|
+
onClose()
|
|
214
|
+
}
|
|
215
|
+
globalThis.addEventListener('keydown', onKey)
|
|
216
|
+
return () => globalThis.removeEventListener('keydown', onKey)
|
|
217
|
+
}, [onClose])
|
|
218
|
+
|
|
219
|
+
const save = async () => {
|
|
220
|
+
if (saving === true) return
|
|
221
|
+
const root = String(store.getState?.()?.root ?? '')
|
|
222
|
+
const next = String(value ?? '').trim().slice(0, 40)
|
|
223
|
+
if (root === '' || next === '' || next === label) {
|
|
224
|
+
onClose()
|
|
225
|
+
return
|
|
226
|
+
}
|
|
227
|
+
setSaving(true)
|
|
228
|
+
setError('')
|
|
229
|
+
try {
|
|
230
|
+
await dwRpc('renameManualEnvironment', { root, from: label, to: next })
|
|
231
|
+
if (typeof onRenamed === 'function') onRenamed()
|
|
232
|
+
onClose()
|
|
233
|
+
} catch (failure) {
|
|
234
|
+
setError(dwExplainFailure(tr, failure))
|
|
235
|
+
setSaving(false)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return h('div', {
|
|
240
|
+
className: 'dw-modal',
|
|
241
|
+
onClick: (event) => {
|
|
242
|
+
if (event.target === event.currentTarget && saving !== true) onClose()
|
|
243
|
+
},
|
|
244
|
+
}, h('div', {
|
|
245
|
+
key: 'card',
|
|
246
|
+
className: 'dw-modal-card',
|
|
247
|
+
role: 'dialog',
|
|
248
|
+
'aria-modal': 'true',
|
|
249
|
+
'aria-label': tr('renameTitle'),
|
|
250
|
+
}, [
|
|
251
|
+
h('div', { key: 'title', className: 'dw-modal-title' }, [
|
|
252
|
+
h(DwIcon, { key: 'i', name: 'link', size: 15 }),
|
|
253
|
+
h('span', { key: 't' }, tr('renameTitle')),
|
|
254
|
+
]),
|
|
255
|
+
h('div', { key: 'body', className: 'dw-modal-body' }, [
|
|
256
|
+
h('label', { key: 'field', className: 'dw-form-row' }, [
|
|
257
|
+
h('span', { key: 'l', className: 'dw-form-label' }, tr('renameLabel')),
|
|
258
|
+
h('input', {
|
|
259
|
+
key: 'i',
|
|
260
|
+
className: 'dw-form-input',
|
|
261
|
+
value,
|
|
262
|
+
spellCheck: false,
|
|
263
|
+
autoComplete: 'off',
|
|
264
|
+
onChange: (event) => setValue(event.target.value),
|
|
265
|
+
onKeyDown: (event) => {
|
|
266
|
+
if (event.key === 'Enter') void save()
|
|
267
|
+
},
|
|
268
|
+
}),
|
|
269
|
+
]),
|
|
270
|
+
h('div', { key: 'hint', className: 'dw-ai-note' }, tr('renameHint')),
|
|
271
|
+
error === ''
|
|
272
|
+
? null
|
|
273
|
+
: h('div', { key: 'err', className: 'dw-ai-note dw-ai-warn' }, error),
|
|
274
|
+
]),
|
|
275
|
+
h('div', { key: 'foot', className: 'dw-modal-foot' }, [
|
|
276
|
+
h('button', { key: 'cancel', type: 'button', className: 'dw-btn', disabled: saving === true, onClick: onClose }, tr('cancel')),
|
|
277
|
+
h('button', {
|
|
278
|
+
key: 'save',
|
|
279
|
+
type: 'button',
|
|
280
|
+
className: 'dw-btn dw-btn-accent',
|
|
281
|
+
disabled: saving === true,
|
|
282
|
+
onClick: () => void save(),
|
|
283
|
+
}, saving === true ? tr('schemaApplying') : tr('renameApply')),
|
|
284
|
+
]),
|
|
285
|
+
]))
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* A small confirmation for one destructive tree action.
|
|
290
|
+
*
|
|
291
|
+
* Removing an environment also removes the databases picked inside it, so the action
|
|
292
|
+
* asks once instead of acting on a stray right-click.
|
|
293
|
+
*/
|
|
294
|
+
function DwConfirmDialog({ tr, title, message, confirmLabel, onConfirm, onClose }) {
|
|
295
|
+
const [busy, setBusy] = useState(false)
|
|
296
|
+
const busyRef = useRef(false)
|
|
297
|
+
busyRef.current = busy
|
|
298
|
+
|
|
299
|
+
useEffect(() => {
|
|
300
|
+
const onKey = (event) => {
|
|
301
|
+
if (event.key !== 'Escape' || busyRef.current) return
|
|
302
|
+
onClose()
|
|
303
|
+
}
|
|
304
|
+
globalThis.addEventListener('keydown', onKey)
|
|
305
|
+
return () => globalThis.removeEventListener('keydown', onKey)
|
|
306
|
+
}, [onClose])
|
|
307
|
+
|
|
308
|
+
const confirm = async () => {
|
|
309
|
+
if (busy === true) return
|
|
310
|
+
setBusy(true)
|
|
311
|
+
try {
|
|
312
|
+
await onConfirm()
|
|
313
|
+
} finally {
|
|
314
|
+
setBusy(false)
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return h('div', {
|
|
319
|
+
className: 'dw-modal',
|
|
320
|
+
onClick: (event) => {
|
|
321
|
+
if (event.target === event.currentTarget && busy !== true) onClose()
|
|
322
|
+
},
|
|
323
|
+
}, h('div', {
|
|
324
|
+
key: 'card',
|
|
325
|
+
className: 'dw-modal-card',
|
|
326
|
+
role: 'dialog',
|
|
327
|
+
'aria-modal': 'true',
|
|
328
|
+
'aria-label': title,
|
|
329
|
+
}, [
|
|
330
|
+
h('div', { key: 'title', className: 'dw-modal-title' }, title),
|
|
331
|
+
h('div', { key: 'body', className: 'dw-modal-body' }, [
|
|
332
|
+
h('div', { key: 'm', className: 'dw-ai-note' }, message),
|
|
333
|
+
]),
|
|
334
|
+
h('div', { key: 'foot', className: 'dw-modal-foot' }, [
|
|
335
|
+
h('button', { key: 'cancel', type: 'button', className: 'dw-btn', disabled: busy === true, onClick: onClose }, tr('cancel')),
|
|
336
|
+
h('button', {
|
|
337
|
+
key: 'confirm',
|
|
338
|
+
type: 'button',
|
|
339
|
+
className: 'dw-btn dw-btn-accent',
|
|
340
|
+
disabled: busy === true,
|
|
341
|
+
onClick: () => void confirm(),
|
|
342
|
+
}, confirmLabel),
|
|
343
|
+
]),
|
|
344
|
+
]))
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** The tree's badge for a manually added connection. */
|
|
348
|
+
function dwManualBadge(tr) {
|
|
349
|
+
return h('span', { key: 'badge', className: 'dw-node-tag' }, tr('manualBadge'))
|
|
350
|
+
}
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The AI fallback dialog.
|
|
3
|
+
*
|
|
4
|
+
* The configuration scan only knows the shapes this team already writes. When it
|
|
5
|
+
* finds nothing, the user may hand the workspace to a model — but the model only
|
|
6
|
+
* proposes file paths and keys; the Host reads the value and proves the address
|
|
7
|
+
* with a real connection. This dialog is where the model is chosen (DSH's own
|
|
8
|
+
* configured providers), and where the result is reported.
|
|
9
|
+
*
|
|
10
|
+
* The choice is a display preference, so it lives with the other preferences in
|
|
11
|
+
* localStorage: another machine is expected to have another model configured.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** The saved model choice, or null when the user never picked one. */
|
|
15
|
+
function dwAiModel() {
|
|
16
|
+
return dwReadPrefs().aiModel ?? null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Persist one model choice and return the normalized record. */
|
|
20
|
+
function dwAiSaveModel(selection) {
|
|
21
|
+
const provider = String(selection?.provider ?? '').trim()
|
|
22
|
+
const model = String(selection?.model ?? '').trim()
|
|
23
|
+
const name = String(selection?.name ?? '').trim()
|
|
24
|
+
if (provider === '' || model === '') return null
|
|
25
|
+
const saved = { provider, model, name: name === '' ? model : name }
|
|
26
|
+
dwWritePrefs({ aiModel: saved })
|
|
27
|
+
return saved
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Bound one request so a stuck provider cannot leave the dialog spinning forever. */
|
|
31
|
+
function dwAiWithTimeout(promise, milliseconds) {
|
|
32
|
+
return new Promise((resolvePromise, reject) => {
|
|
33
|
+
const timer = setTimeout(() => reject(new Error(`timed out after ${milliseconds} ms`)), milliseconds)
|
|
34
|
+
Promise.resolve(promise).then(
|
|
35
|
+
(value) => {
|
|
36
|
+
clearTimeout(timer)
|
|
37
|
+
resolvePromise(value)
|
|
38
|
+
},
|
|
39
|
+
(error) => {
|
|
40
|
+
clearTimeout(timer)
|
|
41
|
+
reject(error)
|
|
42
|
+
},
|
|
43
|
+
)
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The first selectable model of a catalog, for a first-time default. */
|
|
48
|
+
function dwAiFirstModel(groups) {
|
|
49
|
+
for (const group of Array.isArray(groups) ? groups : []) {
|
|
50
|
+
const model = (Array.isArray(group?.models) ? group.models : [])[0]
|
|
51
|
+
if (model !== undefined) return { provider: group.id, model: model.id, name: model.name ?? model.id }
|
|
52
|
+
}
|
|
53
|
+
return null
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The preset DSH is configured with, when the catalog still offers it. */
|
|
57
|
+
function dwAiPreset(value, groups) {
|
|
58
|
+
const provider = String(value?.default?.provider ?? '')
|
|
59
|
+
const model = String(value?.default?.model ?? '')
|
|
60
|
+
const group = (Array.isArray(groups) ? groups : []).find((entry) => entry.id === provider)
|
|
61
|
+
const found = group === undefined ? undefined : group.models.find((entry) => entry.id === model)
|
|
62
|
+
if (found === undefined) return null
|
|
63
|
+
return { provider, model, name: found.name ?? found.id }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** "DeepSeek Chat · deepseek" for a chip; empty when nothing was chosen. */
|
|
67
|
+
function dwAiModelLabel(selection) {
|
|
68
|
+
if (selection === null || selection === undefined) return ''
|
|
69
|
+
return `${selection.name ?? selection.model} · ${selection.provider}`
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Modal used by both the empty state and the settings page.
|
|
74
|
+
*
|
|
75
|
+
* @param props `{ store, tr, onClose }`; `store.runAiDiscovery` performs the scan
|
|
76
|
+
*/
|
|
77
|
+
function DwAiDialog({ store, tr, onClose, onManual }) {
|
|
78
|
+
const state = useSyncExternalStore(store.subscribe, store.getState, store.getState)
|
|
79
|
+
const [mode, setMode] = useState('models-loading')
|
|
80
|
+
const [groups, setGroups] = useState([])
|
|
81
|
+
const [error, setError] = useState('')
|
|
82
|
+
const [report, setReport] = useState(null)
|
|
83
|
+
const [selection, setSelection] = useState(() => dwAiModel())
|
|
84
|
+
const [preset, setPreset] = useState(null)
|
|
85
|
+
const [attempt, setAttempt] = useState(0)
|
|
86
|
+
const modeRef = useRef(mode)
|
|
87
|
+
modeRef.current = mode
|
|
88
|
+
|
|
89
|
+
const close = () => {
|
|
90
|
+
if (modeRef.current === 'running') return
|
|
91
|
+
onClose()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
const onKey = (event) => {
|
|
96
|
+
if (event.key !== 'Escape') return
|
|
97
|
+
if (modeRef.current === 'running') return
|
|
98
|
+
onClose()
|
|
99
|
+
}
|
|
100
|
+
globalThis.addEventListener('keydown', onKey)
|
|
101
|
+
return () => globalThis.removeEventListener('keydown', onKey)
|
|
102
|
+
}, [onClose])
|
|
103
|
+
|
|
104
|
+
// The catalog is DSH's, not this plugin's: ask the Host once per attempt and let
|
|
105
|
+
// the user retry a provider whose discovery failed.
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
let alive = true
|
|
108
|
+
setMode('models-loading')
|
|
109
|
+
setError('')
|
|
110
|
+
dwAiWithTimeout(dwRpc('models'), 20000).then((value) => {
|
|
111
|
+
if (!alive) return
|
|
112
|
+
if (value?.available !== true) {
|
|
113
|
+
setError(tr('aiNoLlm'))
|
|
114
|
+
setMode('models-error')
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
const list = (Array.isArray(value.groups) ? value.groups : [])
|
|
118
|
+
.map((group) => ({
|
|
119
|
+
id: String(group?.id ?? ''),
|
|
120
|
+
name: String(group?.name ?? group?.id ?? ''),
|
|
121
|
+
error: String(group?.error ?? ''),
|
|
122
|
+
models: (Array.isArray(group?.models) ? group.models : []).filter((model) => typeof model?.id === 'string'),
|
|
123
|
+
}))
|
|
124
|
+
.filter((group) => group.id !== '' && (group.models.length > 0 || group.error !== ''))
|
|
125
|
+
setGroups(list)
|
|
126
|
+
// A saved choice can point at a model the user has since removed from DSH;
|
|
127
|
+
// otherwise pre-select the model DSH itself is set to use.
|
|
128
|
+
const suggested = dwAiPreset(value, list) ?? dwAiFirstModel(list)
|
|
129
|
+
setPreset(suggested)
|
|
130
|
+
setSelection((current) => {
|
|
131
|
+
const exists = current !== null && list.some((group) => group.id === current.provider
|
|
132
|
+
&& group.models.some((model) => model.id === current.model))
|
|
133
|
+
return exists ? current : suggested
|
|
134
|
+
})
|
|
135
|
+
setMode('choose')
|
|
136
|
+
}).catch((failure) => {
|
|
137
|
+
if (!alive) return
|
|
138
|
+
setError(dwExplainFailure(tr, failure))
|
|
139
|
+
setMode('models-error')
|
|
140
|
+
})
|
|
141
|
+
return () => {
|
|
142
|
+
alive = false
|
|
143
|
+
}
|
|
144
|
+
}, [attempt])
|
|
145
|
+
|
|
146
|
+
const start = async () => {
|
|
147
|
+
if (selection === null) return
|
|
148
|
+
const saved = dwAiSaveModel(selection)
|
|
149
|
+
if (saved === null) return
|
|
150
|
+
setSelection(saved)
|
|
151
|
+
setError('')
|
|
152
|
+
setReport(null)
|
|
153
|
+
setMode('running')
|
|
154
|
+
const outcome = await store.runAiDiscovery(saved)
|
|
155
|
+
if (outcome?.ok === true) {
|
|
156
|
+
setReport(outcome.value ?? null)
|
|
157
|
+
setMode('done')
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
setError(outcome?.error ?? tr('aiFailed'))
|
|
161
|
+
setMode('run-error')
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const models = groups.filter((group) => group.models.length > 0)
|
|
165
|
+
const body = []
|
|
166
|
+
if (mode === 'models-loading') {
|
|
167
|
+
body.push(h('div', { key: 'loading', className: 'dw-ai-loading' }, [
|
|
168
|
+
h(DwIcon, { key: 'i', name: 'spinner', size: 16 }),
|
|
169
|
+
h('span', { key: 't' }, tr('aiLoadingModels')),
|
|
170
|
+
]))
|
|
171
|
+
}
|
|
172
|
+
if (mode === 'choose') {
|
|
173
|
+
body.push(h('div', { key: 'pick', className: 'dw-ai-pick' }, [
|
|
174
|
+
h('div', { key: 'label', className: 'dw-ai-label' }, tr('aiPickModel')),
|
|
175
|
+
models.length === 0
|
|
176
|
+
? h('div', { key: 'none', className: 'dw-ai-note' }, tr('aiNoModels'))
|
|
177
|
+
: h('div', { key: 'list', className: 'dw-ai-list', role: 'radiogroup', 'aria-label': tr('aiPickModel') },
|
|
178
|
+
models.map((group) => h('div', { key: group.id, className: 'dw-ai-group' }, [
|
|
179
|
+
h('div', { key: 'head', className: 'dw-ai-group-title' }, group.name === '' ? group.id : group.name),
|
|
180
|
+
...group.models.map((model) => {
|
|
181
|
+
const chosen = selection !== null && selection.provider === group.id && selection.model === model.id
|
|
182
|
+
return h('button', {
|
|
183
|
+
key: `${group.id}:${model.id}`,
|
|
184
|
+
type: 'button',
|
|
185
|
+
role: 'radio',
|
|
186
|
+
'aria-checked': chosen,
|
|
187
|
+
className: chosen ? 'dw-ai-model on' : 'dw-ai-model',
|
|
188
|
+
onClick: () => setSelection({ provider: group.id, model: model.id, name: model.name ?? model.id }),
|
|
189
|
+
}, [
|
|
190
|
+
h('span', { key: 'name', className: 'dw-ai-model-name' }, [
|
|
191
|
+
h('span', { key: 'label' }, model.name ?? model.id),
|
|
192
|
+
preset !== null && preset.provider === group.id && preset.model === model.id
|
|
193
|
+
? h('span', { key: 'tag', className: 'dw-ai-model-tag' }, tr('aiDefaultTag'))
|
|
194
|
+
: null,
|
|
195
|
+
]),
|
|
196
|
+
model.description === undefined || model.description === ''
|
|
197
|
+
? null
|
|
198
|
+
: h('span', { key: 'desc', className: 'dw-ai-model-desc' }, model.description),
|
|
199
|
+
])
|
|
200
|
+
}),
|
|
201
|
+
group.error === ''
|
|
202
|
+
? null
|
|
203
|
+
: h('div', { key: 'err', className: 'dw-ai-note dw-ai-warn' }, tr('aiProviderFailed', { message: group.error })),
|
|
204
|
+
]))),
|
|
205
|
+
h('div', { key: 'note', className: 'dw-ai-note' }, tr('aiPrivacy')),
|
|
206
|
+
typeof onManual === 'function'
|
|
207
|
+
? h('button', {
|
|
208
|
+
key: 'manual',
|
|
209
|
+
type: 'button',
|
|
210
|
+
className: 'dw-ai-link',
|
|
211
|
+
onClick: () => {
|
|
212
|
+
onManual()
|
|
213
|
+
onClose()
|
|
214
|
+
},
|
|
215
|
+
}, tr('aiManualInstead'))
|
|
216
|
+
: null,
|
|
217
|
+
]))
|
|
218
|
+
}
|
|
219
|
+
if (mode === 'running') {
|
|
220
|
+
body.push(h('div', { key: 'running', className: 'dw-ai-loading' }, [
|
|
221
|
+
h(DwIcon, { key: 'i', name: 'spinner', size: 16 }),
|
|
222
|
+
h('div', { key: 'box' }, [
|
|
223
|
+
h('div', { key: 't' }, tr('aiRunning', { model: dwAiModelLabel(selection) })),
|
|
224
|
+
h('div', { key: 'h', className: 'dw-ai-note' }, tr('aiRunningHint')),
|
|
225
|
+
]),
|
|
226
|
+
]))
|
|
227
|
+
}
|
|
228
|
+
if (mode === 'models-error' || mode === 'run-error') {
|
|
229
|
+
body.push(h('div', { key: 'error', className: 'dw-ai-error' }, [
|
|
230
|
+
h('div', { key: 't' }, tr('aiFailedTitle')),
|
|
231
|
+
h('div', { key: 'm', className: 'dw-ai-note' }, error === '' ? tr('aiFailed') : error),
|
|
232
|
+
]))
|
|
233
|
+
}
|
|
234
|
+
if (mode === 'done') {
|
|
235
|
+
const warnings = Array.isArray(report?.warnings) ? report.warnings : []
|
|
236
|
+
const accepted = Number(report?.accepted ?? 0)
|
|
237
|
+
body.push(h('div', { key: 'done', className: 'dw-ai-done' }, [
|
|
238
|
+
h('div', { key: 'title', className: accepted > 0 ? 'dw-ai-ok' : 'dw-ai-warn' }, accepted > 0
|
|
239
|
+
? tr('aiDone', { accepted, candidates: Number(report?.candidates ?? 0) })
|
|
240
|
+
: tr('aiDoneEmpty')),
|
|
241
|
+
h('div', { key: 'hint', className: 'dw-ai-note' }, tr('aiDoneHint')),
|
|
242
|
+
warnings.length === 0
|
|
243
|
+
? null
|
|
244
|
+
: h('div', { key: 'warn', className: 'dw-code-block' }, warnings.map((warning) => dwWarningText(warning, tr)).join('\n')),
|
|
245
|
+
typeof report?.notes === 'string' && report.notes !== ''
|
|
246
|
+
? h('div', { key: 'notes', className: 'dw-ai-note' }, report.notes)
|
|
247
|
+
: null,
|
|
248
|
+
]))
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const footer = []
|
|
252
|
+
if (mode === 'choose') {
|
|
253
|
+
footer.push(h('button', { key: 'cancel', type: 'button', className: 'dw-btn', onClick: close }, tr('cancel')))
|
|
254
|
+
footer.push(h('button', {
|
|
255
|
+
key: 'start',
|
|
256
|
+
type: 'button',
|
|
257
|
+
className: 'dw-btn dw-btn-accent',
|
|
258
|
+
disabled: selection === null || models.length === 0 || state.aiRunning === true,
|
|
259
|
+
onClick: () => void start(),
|
|
260
|
+
}, tr('aiStart')))
|
|
261
|
+
} else if (mode === 'running') {
|
|
262
|
+
footer.push(h('button', { key: 'cancel', type: 'button', className: 'dw-btn', disabled: true }, tr('cancel')))
|
|
263
|
+
} else if (mode === 'done') {
|
|
264
|
+
footer.push(h('button', {
|
|
265
|
+
key: 'again',
|
|
266
|
+
type: 'button',
|
|
267
|
+
className: 'dw-btn',
|
|
268
|
+
onClick: () => {
|
|
269
|
+
setReport(null)
|
|
270
|
+
setError('')
|
|
271
|
+
setMode('choose')
|
|
272
|
+
},
|
|
273
|
+
}, tr('aiAgain')))
|
|
274
|
+
footer.push(h('button', { key: 'close', type: 'button', className: 'dw-btn dw-btn-accent', onClick: close }, tr('close')))
|
|
275
|
+
} else {
|
|
276
|
+
footer.push(h('button', { key: 'retry', type: 'button', className: 'dw-btn', onClick: () => setAttempt(attempt + 1) }, tr('aiRetry')))
|
|
277
|
+
footer.push(h('button', { key: 'close', type: 'button', className: 'dw-btn', onClick: close }, tr('close')))
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return h('div', {
|
|
281
|
+
className: 'dw-modal',
|
|
282
|
+
onClick: (event) => {
|
|
283
|
+
if (event.target === event.currentTarget) close()
|
|
284
|
+
},
|
|
285
|
+
}, h('div', {
|
|
286
|
+
key: 'card',
|
|
287
|
+
className: 'dw-modal-card',
|
|
288
|
+
role: 'dialog',
|
|
289
|
+
'aria-modal': 'true',
|
|
290
|
+
'aria-label': tr('aiTitle'),
|
|
291
|
+
}, [
|
|
292
|
+
h('div', { key: 'title', className: 'dw-modal-title' }, [
|
|
293
|
+
h(DwIcon, { key: 'i', name: 'ai', size: 15 }),
|
|
294
|
+
h('span', { key: 't' }, tr('aiTitle')),
|
|
295
|
+
]),
|
|
296
|
+
h('div', { key: 'body', className: 'dw-modal-body' }, body),
|
|
297
|
+
h('div', { key: 'foot', className: 'dw-modal-foot' }, footer),
|
|
298
|
+
]))
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** The settings row that shows (and changes) the chosen model. */
|
|
302
|
+
function DwAiSettingsRow({ tr, prefs, onOpen }) {
|
|
303
|
+
const label = prefs.aiModel === null ? tr('settingsAiModelNone') : dwAiModelLabel(prefs.aiModel)
|
|
304
|
+
return h('div', { className: 'dw-settings-row' }, [
|
|
305
|
+
h('div', { key: 'head', className: 'dw-settings-head' }, [
|
|
306
|
+
h('span', { key: 'label', className: 'dw-settings-label' }, tr('settingsAiModel')),
|
|
307
|
+
h('div', { key: 'right', className: 'dw-settings-shortcut-actions' }, [
|
|
308
|
+
h('span', { key: 'value', className: 'dw-settings-value' }, label),
|
|
309
|
+
h('button', {
|
|
310
|
+
key: 'change',
|
|
311
|
+
type: 'button',
|
|
312
|
+
className: 'dw-btn',
|
|
313
|
+
onClick: onOpen,
|
|
314
|
+
}, prefs.aiModel === null ? tr('settingsAiChoose') : tr('settingsAiChange')),
|
|
315
|
+
]),
|
|
316
|
+
]),
|
|
317
|
+
h('div', { key: 'desc', className: 'dw-settings-desc' }, tr('settingsAiModelHint')),
|
|
318
|
+
h('div', { key: 'note', className: 'dw-settings-desc dw-ai-warn' }, tr('aiPrivacy')),
|
|
319
|
+
])
|
|
320
|
+
}
|