dsh-data-window 0.0.0-stage → 0.1.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/AGENTS.md +111 -0
- package/CHANGELOG.md +239 -0
- package/LICENSE +21 -0
- package/README.md +146 -2
- package/client.js +7779 -0
- package/cordis.patch.yml +4 -0
- package/docs/BRIDGE.md +100 -0
- package/docs/DESIGN.md +417 -0
- package/docs/DETAIL.md +220 -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 +286 -0
- package/locale/zh.json +286 -0
- package/package.json +68 -4
- package/src/client/00-css.js +993 -0
- package/src/client/05-icons.js +137 -0
- package/src/client/05-theme.js +107 -0
- package/src/client/06-shortcuts.js +89 -0
- package/src/client/07-bridge.js +193 -0
- package/src/client/10-core.js +1352 -0
- package/src/client/20-tree.js +281 -0
- package/src/client/25-sql.js +631 -0
- package/src/client/30-editor.js +537 -0
- package/src/client/40-results.js +556 -0
- package/src/client/45-export.js +219 -0
- package/src/client/50-panel.js +761 -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 +98 -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 +172 -0
- package/src/host/yaml.js +335 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The "AI optimize" dialog for the SQL editor.
|
|
3
|
+
*
|
|
4
|
+
* The model is the one chosen in settings (`aiModel`); the Host calls it, and the
|
|
5
|
+
* answer is shown as a suggestion. Nothing is executed or written back to the
|
|
6
|
+
* editor automatically — copying is the only way out, which keeps the "never
|
|
7
|
+
* overwrite user SQL" rule intact.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
function dwOptimizeModelName(model) {
|
|
11
|
+
return String(model?.name ?? model?.model ?? '')
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function DwSqlOptimizeDialog({ store, tr, sql, database, onClose }) {
|
|
15
|
+
const [phase, setPhase] = useState('running')
|
|
16
|
+
const [result, setResult] = useState(null)
|
|
17
|
+
const [error, setError] = useState('')
|
|
18
|
+
const model = dwAiModel()
|
|
19
|
+
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
if (model === null) {
|
|
22
|
+
setPhase('error')
|
|
23
|
+
setError(tr('optimizeNoModel'))
|
|
24
|
+
return undefined
|
|
25
|
+
}
|
|
26
|
+
let alive = true
|
|
27
|
+
const run = async () => {
|
|
28
|
+
try {
|
|
29
|
+
const value = await dwAiWithTimeout(dwRpc('optimizeSql', {
|
|
30
|
+
provider: model.provider,
|
|
31
|
+
model: model.model,
|
|
32
|
+
sql,
|
|
33
|
+
database,
|
|
34
|
+
// The explanation should read like the rest of the window.
|
|
35
|
+
locale: String(globalThis.document?.documentElement?.lang ?? globalThis.navigator?.language ?? ''),
|
|
36
|
+
}), 130000)
|
|
37
|
+
if (!alive) return
|
|
38
|
+
setResult(value)
|
|
39
|
+
setPhase('done')
|
|
40
|
+
} catch (failure) {
|
|
41
|
+
if (!alive) return
|
|
42
|
+
setError(dwExplainFailure(tr, failure))
|
|
43
|
+
setPhase('error')
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
void run()
|
|
47
|
+
return () => { alive = false }
|
|
48
|
+
}, [])
|
|
49
|
+
|
|
50
|
+
const body = phase === 'running'
|
|
51
|
+
? h('div', { key: 'running', className: 'dw-empty', style: { minHeight: 120 } }, [
|
|
52
|
+
h(DwIcon, { key: 'i', name: 'spinner', size: 18 }),
|
|
53
|
+
h('div', { key: 't', className: 'dw-empty-hint' }, tr('optimizeRunning')),
|
|
54
|
+
])
|
|
55
|
+
: phase === 'error'
|
|
56
|
+
? h('div', { key: 'error', className: 'dw-error-text' }, error)
|
|
57
|
+
: h('div', { key: 'done' }, [
|
|
58
|
+
h('div', { key: 'm', className: 'dw-optimize-model' }, tr('optimizeModelLabel', {
|
|
59
|
+
model: dwOptimizeModelName(result?.model) === '' ? tr('aiModelUnknown') : dwOptimizeModelName(result?.model),
|
|
60
|
+
})),
|
|
61
|
+
h('pre', { key: 'sql', className: 'dw-cell-view' }, result?.sql ?? ''),
|
|
62
|
+
result?.notes === undefined || result.notes === ''
|
|
63
|
+
? null
|
|
64
|
+
: h('div', { key: 'notes' }, [
|
|
65
|
+
h('div', { key: 't', className: 'dw-ai-label', style: { paddingTop: 10 } }, tr('optimizeNotes')),
|
|
66
|
+
h('div', { key: 'v', className: 'dw-optimize-notes' }, result.notes),
|
|
67
|
+
]),
|
|
68
|
+
])
|
|
69
|
+
|
|
70
|
+
return h('div', {
|
|
71
|
+
className: 'dw-modal',
|
|
72
|
+
onClick: onClose,
|
|
73
|
+
}, h('div', {
|
|
74
|
+
key: 'card',
|
|
75
|
+
className: 'dw-modal-card dw-optimize-dialog',
|
|
76
|
+
onClick: (event) => event.stopPropagation(),
|
|
77
|
+
}, [
|
|
78
|
+
h('div', { key: 'title', className: 'dw-modal-title' }, [
|
|
79
|
+
h(DwIcon, { key: 'i', name: 'ai', size: 13 }),
|
|
80
|
+
h('span', { key: 't' }, tr('optimizeTitle')),
|
|
81
|
+
]),
|
|
82
|
+
h('div', { key: 'body', className: 'dw-modal-body' }, body),
|
|
83
|
+
h('div', { key: 'foot', className: 'dw-modal-foot' }, [
|
|
84
|
+
phase === 'done'
|
|
85
|
+
? h('button', {
|
|
86
|
+
key: 'copy',
|
|
87
|
+
type: 'button',
|
|
88
|
+
className: 'dw-btn',
|
|
89
|
+
onClick: () => {
|
|
90
|
+
const write = globalThis.navigator?.clipboard?.writeText?.(result?.sql ?? '')
|
|
91
|
+
if (write !== undefined) write.then(() => store.pushLog('info', tr('log.optimizeCopied'))).catch(() => undefined)
|
|
92
|
+
},
|
|
93
|
+
}, tr('optimizeCopy'))
|
|
94
|
+
: null,
|
|
95
|
+
h('button', { key: 'close', type: 'button', className: 'dw-btn dw-btn-accent', onClick: onClose }, tr('close')),
|
|
96
|
+
]),
|
|
97
|
+
]))
|
|
98
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client activation.
|
|
3
|
+
*
|
|
4
|
+
* Three seats and one mode:
|
|
5
|
+
*
|
|
6
|
+
* - `main` (keyed `dsh-data-window:database`) — the DataGrip-style window itself.
|
|
7
|
+
* - `sidebar.footer.action` — one always-visible control beside Settings, so the
|
|
8
|
+
* window is reachable without hunting through the navigation column.
|
|
9
|
+
* - `conversation.header.leading` is deliberately left alone: it is a single seat
|
|
10
|
+
* that another plugin already holds, and a second registration would replace
|
|
11
|
+
* that plugin's entry rather than sit beside it.
|
|
12
|
+
* - `shell.overlay` — a headless tracker that keeps the last Workspace while the
|
|
13
|
+
* Conversation still holds the main column.
|
|
14
|
+
*
|
|
15
|
+
* Opening the panel goes through `ctx.layout.selectPanel`, the same service the
|
|
16
|
+
* shipped navigation uses, so the window is a real main-column page rather than an
|
|
17
|
+
* overlay that would fight the layout.
|
|
18
|
+
*/
|
|
19
|
+
const DW_TRANSLATE_FALLBACK = (key) => key
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Whether the data window is the panel the main column is showing.
|
|
23
|
+
*
|
|
24
|
+
* The foot control is an action, not a navigation row: DSH paints the selected
|
|
25
|
+
* panel itself, and an occupant that brings a filled button reads as permanently
|
|
26
|
+
* selected. The highlight therefore follows `layout.panelInfo` — the source DSH's
|
|
27
|
+
* own panel rows read — and stays off where the layout service exposes none.
|
|
28
|
+
*/
|
|
29
|
+
function dwUsePanelActive(store) {
|
|
30
|
+
const info = store.layout?.()?.panelInfo
|
|
31
|
+
const subscribe = useCallback((onChange) => {
|
|
32
|
+
if (info === undefined || typeof info.subscribe !== 'function') return () => {}
|
|
33
|
+
return info.subscribe(onChange)
|
|
34
|
+
}, [info])
|
|
35
|
+
const getSnapshot = useCallback(() => info !== undefined
|
|
36
|
+
&& typeof info.getSnapshot === 'function'
|
|
37
|
+
&& info.getSnapshot()?.activePanelId === DW_PANEL_ID, [info])
|
|
38
|
+
return useSyncExternalStore(subscribe, getSnapshot, () => false)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The sidebar-foot control: icon-only, or with its label when the column is wide. */
|
|
42
|
+
function DwFooterAction({ store, translate, wide }) {
|
|
43
|
+
const tr = typeof translate === 'function' ? translate : DW_TRANSLATE_FALLBACK
|
|
44
|
+
const active = dwUsePanelActive(store)
|
|
45
|
+
const onClick = () => {
|
|
46
|
+
const layout = store.layout?.()
|
|
47
|
+
if (layout !== undefined && typeof layout.selectPanel === 'function') layout.selectPanel(DW_PANEL_ID)
|
|
48
|
+
}
|
|
49
|
+
return h('button', {
|
|
50
|
+
type: 'button',
|
|
51
|
+
className: active ? 'dw-foot-btn on' : 'dw-foot-btn',
|
|
52
|
+
title: tr('openPanelHint'),
|
|
53
|
+
'aria-label': tr('openPanelHint'),
|
|
54
|
+
'aria-current': active ? 'page' : undefined,
|
|
55
|
+
onClick,
|
|
56
|
+
style: wide === true ? { padding: '0 8px', gap: 6 } : { minWidth: 26, padding: '0 4px' },
|
|
57
|
+
}, [
|
|
58
|
+
h(DwStyles, { key: 'style' }),
|
|
59
|
+
h(DwIcon, { key: 'i', name: 'database', size: 15 }),
|
|
60
|
+
wide === true ? h('span', { key: 't' }, tr('openPanel')) : null,
|
|
61
|
+
])
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function apply(ctx) {
|
|
65
|
+
let bound
|
|
66
|
+
try {
|
|
67
|
+
if (ctx.locale !== undefined && typeof ctx.locale.register === 'function') {
|
|
68
|
+
ctx.locale.register(DW_NS, { en: DW_TEXT_EN, zh: DW_TEXT_ZH })
|
|
69
|
+
for (const alias of ['zh-CN', 'zh-Hans']) {
|
|
70
|
+
try {
|
|
71
|
+
ctx.locale.register(DW_NS, alias, DW_TEXT_ZH)
|
|
72
|
+
} catch (error) {
|
|
73
|
+
void error
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const candidate = ctx.locale.bind(DW_NS)
|
|
77
|
+
if (typeof candidate === 'function') bound = candidate
|
|
78
|
+
}
|
|
79
|
+
} catch (error) {
|
|
80
|
+
// A host without the locale face still renders: the translator falls back to
|
|
81
|
+
// the dictionary the bundle carries.
|
|
82
|
+
void error
|
|
83
|
+
}
|
|
84
|
+
const translate = dwMakeTranslator(ctx, bound)
|
|
85
|
+
const store = dwCreateStore(translate)
|
|
86
|
+
|
|
87
|
+
// `layout` is an optional service: it is resolved in its own scope so the plugin
|
|
88
|
+
// still activates where the shell has no panel navigation.
|
|
89
|
+
const layoutRef = { current: undefined }
|
|
90
|
+
try {
|
|
91
|
+
ctx.inject(['layout'], (scoped) => {
|
|
92
|
+
layoutRef.current = scoped.layout
|
|
93
|
+
})
|
|
94
|
+
} catch (error) {
|
|
95
|
+
void error
|
|
96
|
+
}
|
|
97
|
+
Object.defineProperty(store, 'layout', { value: () => layoutRef.current, enumerable: false })
|
|
98
|
+
|
|
99
|
+
// `uiWorkspace` opens the directory picker. It is resolved in its own scope, so
|
|
100
|
+
// a shell without it still activates — the pick button then simply reports that
|
|
101
|
+
// no picker is available instead of failing at click time.
|
|
102
|
+
const workspaceRef = { current: undefined }
|
|
103
|
+
try {
|
|
104
|
+
ctx.inject(['uiWorkspace'], (scoped) => {
|
|
105
|
+
workspaceRef.current = scoped.uiWorkspace
|
|
106
|
+
})
|
|
107
|
+
} catch (error) {
|
|
108
|
+
void error
|
|
109
|
+
}
|
|
110
|
+
const pickDirectory = async () => {
|
|
111
|
+
const service = workspaceRef.current
|
|
112
|
+
if (service === undefined || typeof service.pickDirectory !== 'function') return null
|
|
113
|
+
return await service.pickDirectory()
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
ctx.effect(() => ctx.slots.inject('main', () => ctx.slots.register({
|
|
117
|
+
name: 'main',
|
|
118
|
+
key: DW_PANEL_ID,
|
|
119
|
+
registrant: DW_NS,
|
|
120
|
+
locale: DW_NS,
|
|
121
|
+
inject: () => ({
|
|
122
|
+
store,
|
|
123
|
+
translate,
|
|
124
|
+
pickDirectory,
|
|
125
|
+
}),
|
|
126
|
+
}, DwPanel)), 'dsh-data-window: database panel')
|
|
127
|
+
|
|
128
|
+
ctx.effect(() => ctx.slots.inject('sidebar.footer.action', () => ctx.slots.register({
|
|
129
|
+
name: 'sidebar.footer.action',
|
|
130
|
+
id: `${DW_NS}:open`,
|
|
131
|
+
order: 7,
|
|
132
|
+
label: () => translate('openPanel'),
|
|
133
|
+
registrant: DW_NS,
|
|
134
|
+
locale: DW_NS,
|
|
135
|
+
inject: () => ({ store, translate }),
|
|
136
|
+
}, DwFooterAction)), 'dsh-data-window: footer action')
|
|
137
|
+
|
|
138
|
+
ctx.effect(() => dwInstallBridge({
|
|
139
|
+
store,
|
|
140
|
+
translate,
|
|
141
|
+
layout: () => layoutRef.current,
|
|
142
|
+
}), 'dsh-data-window: code workspace bridge')
|
|
143
|
+
|
|
144
|
+
ctx.effect(() => ctx.slots.inject('settings.section', () => ctx.slots.register({
|
|
145
|
+
name: 'settings.section',
|
|
146
|
+
id: `${DW_NS}:settings`,
|
|
147
|
+
order: 60,
|
|
148
|
+
label: () => translate('settingsNav'),
|
|
149
|
+
registrant: DW_NS,
|
|
150
|
+
locale: DW_NS,
|
|
151
|
+
inject: () => ({ translate, store }),
|
|
152
|
+
}, DwSettingsSection)), 'dsh-data-window: settings section')
|
|
153
|
+
|
|
154
|
+
ctx.effect(() => ctx.slots.inject('shell.overlay', () => ctx.slots.register({
|
|
155
|
+
name: 'shell.overlay',
|
|
156
|
+
id: `${DW_NS}:tracker`,
|
|
157
|
+
order: 42,
|
|
158
|
+
registrant: DW_NS,
|
|
159
|
+
inject: () => ({ store }),
|
|
160
|
+
}, DwTracker)), 'dsh-data-window: workspace tracker')
|
|
161
|
+
|
|
162
|
+
// Requirement: the open shortcut works from anywhere, with ⌘3 as its default.
|
|
163
|
+
// DSH's shortcut service owns its own catalog and the code workspace binds its
|
|
164
|
+
// panel the same way — a window-level listener, so the key works before the
|
|
165
|
+
// panel is open. The binding is editable in the plugin's settings section.
|
|
166
|
+
ctx.effect(() => {
|
|
167
|
+
dwWatchTheme()
|
|
168
|
+
const onKey = (event) => {
|
|
169
|
+
if (event.repeat === true) return
|
|
170
|
+
if (globalThis.__dwShortcutRecording === true) return
|
|
171
|
+
if (!dwShortcutMatches(dwOpenShortcut(), event)) return
|
|
172
|
+
const layout = layoutRef.current
|
|
173
|
+
if (layout === undefined || typeof layout.selectPanel !== 'function') return
|
|
174
|
+
event.preventDefault()
|
|
175
|
+
event.stopPropagation()
|
|
176
|
+
layout.selectPanel(DW_PANEL_ID)
|
|
177
|
+
}
|
|
178
|
+
globalThis.addEventListener('keydown', onKey, true)
|
|
179
|
+
return () => globalThis.removeEventListener('keydown', onKey, true)
|
|
180
|
+
}, 'dsh-data-window: open shortcut')
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export { apply, DwFooterAction }
|