@agy-daddy/plugin-cli 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 +22 -0
- package/NOTICE +7 -0
- package/README.md +7 -2
- package/agy-plugin.json +49 -0
- package/baseline.json +10 -0
- package/locales/en.json +4 -0
- package/locales/zh-CN.json +4 -0
- package/package.json +31 -4
- package/scripts/cli-window-helper.cs +453 -0
- package/settings.schema.json +141 -0
- package/src/node.js +531 -0
- package/src/renderer.js +491 -0
- package/src/terminals.js +1075 -0
package/src/renderer.js
ADDED
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
let disposers = [];
|
|
4
|
+
|
|
5
|
+
const TERMINAL_ICON_SVG = '<svg width="13" height="13" viewBox="0 0 16 16" fill="none" aria-hidden="true">'
|
|
6
|
+
+ '<rect x="1.5" y="2.5" width="13" height="11" rx="2" stroke="currentColor" stroke-width="1.4"/>'
|
|
7
|
+
+ '<path d="M4.5 6.2L6.8 8.2L4.5 10.2" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"/>'
|
|
8
|
+
+ '<line x1="8.2" y1="10.2" x2="11.5" y2="10.2" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"/>'
|
|
9
|
+
+ '</svg>';
|
|
10
|
+
|
|
11
|
+
function parseQuickCommands(rawList) {
|
|
12
|
+
if (!Array.isArray(rawList)) return [];
|
|
13
|
+
const out = [];
|
|
14
|
+
rawList.forEach((rawItem, idx) => {
|
|
15
|
+
let name = '';
|
|
16
|
+
let command = '';
|
|
17
|
+
let pinToTopbar = true;
|
|
18
|
+
let id = `quick-${idx}`;
|
|
19
|
+
if (rawItem && typeof rawItem === 'object' && !Array.isArray(rawItem)) {
|
|
20
|
+
command = String(rawItem.command || '').trim();
|
|
21
|
+
name = String(rawItem.name || '').trim();
|
|
22
|
+
pinToTopbar = rawItem.pinToTopbar !== false;
|
|
23
|
+
if (rawItem.id) id = String(rawItem.id);
|
|
24
|
+
} else {
|
|
25
|
+
const rawStr = String(rawItem ?? '').trim();
|
|
26
|
+
if (!rawStr) return;
|
|
27
|
+
if (rawStr.startsWith('{')) {
|
|
28
|
+
try {
|
|
29
|
+
const parsed = JSON.parse(rawStr);
|
|
30
|
+
if (parsed && typeof parsed === 'object') {
|
|
31
|
+
command = String(parsed.command || '').trim();
|
|
32
|
+
name = String(parsed.name || '').trim();
|
|
33
|
+
pinToTopbar = parsed.pinToTopbar !== false;
|
|
34
|
+
if (parsed.id) id = String(parsed.id);
|
|
35
|
+
}
|
|
36
|
+
} catch {}
|
|
37
|
+
}
|
|
38
|
+
if (!command) command = rawStr;
|
|
39
|
+
}
|
|
40
|
+
if (!command) return;
|
|
41
|
+
if (!name) name = command.split(/\s+/)[0] || `命令 ${idx + 1}`;
|
|
42
|
+
out.push({ id, name, command, pinToTopbar });
|
|
43
|
+
});
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function getPermissionLabel(mode) {
|
|
48
|
+
switch (mode) {
|
|
49
|
+
case 'plan': return '规划只读';
|
|
50
|
+
case 'accept-edits': return '自动接受编辑';
|
|
51
|
+
case 'auto': return '自动执行';
|
|
52
|
+
case 'skip-permissions': return '免审核';
|
|
53
|
+
default: return '默认权限';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function isAbsoluteWorkspaceHint(raw) {
|
|
58
|
+
const s = String(raw || '').trim();
|
|
59
|
+
if (!s || s.length > 512) return false;
|
|
60
|
+
if (/^file:\/\//i.test(s)) return true;
|
|
61
|
+
if (/^[A-Za-z]:[\\/]/.test(s)) return true;
|
|
62
|
+
if (s.startsWith('/') && !s.startsWith('//') && s.length > 1) return true;
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function collectClickWorkspaceContext(ctx, docRef, contextCascadeId = '') {
|
|
67
|
+
const doc = docRef || globalThis.document;
|
|
68
|
+
const cascadeId = String(ctx?.hostDom?.getSessionId?.() || '').trim();
|
|
69
|
+
const ctxCid = String(contextCascadeId || '').trim();
|
|
70
|
+
const pageUrl = String((globalThis.location && globalThis.location.href) || doc?.URL || '').trim();
|
|
71
|
+
const pageTitle = String(doc?.title || '').trim();
|
|
72
|
+
|
|
73
|
+
let activeConversationTitle = '';
|
|
74
|
+
const sidebarRows = [];
|
|
75
|
+
try {
|
|
76
|
+
const fromHostDom = ctx?.hostDom?.getSidebarRows?.();
|
|
77
|
+
if (Array.isArray(fromHostDom)) sidebarRows.push(...fromHostDom);
|
|
78
|
+
} catch {}
|
|
79
|
+
if (sidebarRows.length === 0 && typeof doc?.querySelectorAll === 'function') {
|
|
80
|
+
try {
|
|
81
|
+
const queried = doc.querySelectorAll('[data-testid="conversation-row-sidebar"]');
|
|
82
|
+
if (queried) sidebarRows.push(...Array.from(queried));
|
|
83
|
+
} catch {}
|
|
84
|
+
}
|
|
85
|
+
for (const row of sidebarRows) {
|
|
86
|
+
if (!row || typeof row.getAttribute !== 'function') continue;
|
|
87
|
+
const isActive = row.getAttribute('aria-selected') === 'true'
|
|
88
|
+
|| row.getAttribute('aria-current') === 'true'
|
|
89
|
+
|| row.getAttribute('data-active') === 'true'
|
|
90
|
+
|| row.getAttribute('data-selected') === 'true'
|
|
91
|
+
|| row.getAttribute('data-state') === 'active';
|
|
92
|
+
if (!isActive) continue;
|
|
93
|
+
const txt = String(row.textContent || '').trim().split(/\r?\n/).map((l) => l.trim()).find(Boolean) || '';
|
|
94
|
+
if (txt) {
|
|
95
|
+
activeConversationTitle = txt.slice(0, 200);
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const domPaths = [];
|
|
101
|
+
const pushDomPath = (raw) => {
|
|
102
|
+
const s = String(raw || '').trim();
|
|
103
|
+
if (!isAbsoluteWorkspaceHint(s) || domPaths.includes(s)) return;
|
|
104
|
+
if (domPaths.length < 8) domPaths.push(s);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
if (typeof doc?.querySelectorAll === 'function') {
|
|
108
|
+
try {
|
|
109
|
+
const wsNodes = doc.querySelectorAll('[data-workspace-path], [data-workspace-uri], [data-project-path], [data-cwd]');
|
|
110
|
+
for (const el of Array.from(wsNodes || [])) {
|
|
111
|
+
for (const attr of ['data-workspace-path', 'data-workspace-uri', 'data-project-path', 'data-cwd']) {
|
|
112
|
+
pushDomPath(el?.getAttribute?.(attr));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
} catch {}
|
|
116
|
+
try {
|
|
117
|
+
const titlebarEls = doc.querySelectorAll('[class*="@container/titlebar"] [title], [class*="@container/titlebar"] [aria-label]');
|
|
118
|
+
for (const el of Array.from(titlebarEls || [])) {
|
|
119
|
+
if (typeof el?.closest === 'function' && el.closest('#agy-daddy-host')) continue;
|
|
120
|
+
pushDomPath(el?.getAttribute?.('title'));
|
|
121
|
+
pushDomPath(el?.getAttribute?.('aria-label'));
|
|
122
|
+
}
|
|
123
|
+
} catch {}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const titlebarTexts = [];
|
|
127
|
+
const pushTitlebarText = (raw) => {
|
|
128
|
+
const s = String(raw || '').trim();
|
|
129
|
+
if (!s || s.length > 120 || titlebarTexts.includes(s)) return;
|
|
130
|
+
if (titlebarTexts.length < 12) titlebarTexts.push(s);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
if (typeof doc?.querySelectorAll === 'function') {
|
|
134
|
+
try {
|
|
135
|
+
const bars = doc.querySelectorAll('[class*="@container/titlebar"]');
|
|
136
|
+
for (const bar of Array.from(bars || [])) {
|
|
137
|
+
if (typeof bar?.closest === 'function' && bar.closest('#agy-daddy-host')) continue;
|
|
138
|
+
const leafCandidates = typeof bar.querySelectorAll === 'function'
|
|
139
|
+
? Array.from(bar.querySelectorAll('span, div, button, a, h1, h2'))
|
|
140
|
+
: [];
|
|
141
|
+
for (const node of leafCandidates) {
|
|
142
|
+
if (typeof node?.closest === 'function' && node.closest('#agy-daddy-host')) continue;
|
|
143
|
+
if (Array.isArray(node.children) && node.children.length > 0) continue;
|
|
144
|
+
const txt = String(node.textContent || '').trim();
|
|
145
|
+
if (txt && txt.length >= 2) pushTitlebarText(txt);
|
|
146
|
+
}
|
|
147
|
+
if (leafCandidates.length === 0) {
|
|
148
|
+
const barText = String(bar.textContent || '').trim();
|
|
149
|
+
if (barText) {
|
|
150
|
+
for (const line of barText.split(/\r?\n/)) {
|
|
151
|
+
if (line.trim().length >= 2) pushTitlebarText(line.trim());
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
} catch {}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const workspaceNameHints = [];
|
|
160
|
+
const pushWsName = (raw) => {
|
|
161
|
+
const s = String(raw || '').trim();
|
|
162
|
+
if (!s || s.length < 2 || s.length > 120 || workspaceNameHints.includes(s)) return;
|
|
163
|
+
if (workspaceNameHints.length < 12) workspaceNameHints.push(s);
|
|
164
|
+
};
|
|
165
|
+
for (const t of titlebarTexts) pushWsName(t);
|
|
166
|
+
if (pageTitle) {
|
|
167
|
+
for (const seg of pageTitle.split(/\s+[—–\-|·]\s+|\s+-\s+/)) {
|
|
168
|
+
pushWsName(seg);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
cascadeId: cascadeId || undefined,
|
|
174
|
+
contextCascadeId: ctxCid || undefined,
|
|
175
|
+
workspaceHints: {
|
|
176
|
+
pageUrl,
|
|
177
|
+
pageTitle,
|
|
178
|
+
activeConversationTitle,
|
|
179
|
+
domPaths,
|
|
180
|
+
titlebarTexts,
|
|
181
|
+
workspaceNameHints
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function mount(ctx) {
|
|
187
|
+
let settings = {
|
|
188
|
+
terminalApp: 'auto',
|
|
189
|
+
customTerminalPath: '',
|
|
190
|
+
cliMuxHost: '',
|
|
191
|
+
cliLabel: 'CLI',
|
|
192
|
+
cliDefaultModel: '',
|
|
193
|
+
cliPermissionMode: 'default',
|
|
194
|
+
cliDefaultCommand: '',
|
|
195
|
+
quickCommands: [],
|
|
196
|
+
...(ctx.settings.get() || {})
|
|
197
|
+
};
|
|
198
|
+
const quickUnits = new Map();
|
|
199
|
+
const feedbackTimers = new Map();
|
|
200
|
+
let contextCascadeId = '';
|
|
201
|
+
let popoverNode = null;
|
|
202
|
+
let popover = null;
|
|
203
|
+
|
|
204
|
+
const stopContextSub = typeof ctx.state?.subscribe === 'function'
|
|
205
|
+
? ctx.state.subscribe('state:context.snapshot@1', (snap) => {
|
|
206
|
+
const cid = typeof snap?.cascadeId === 'string' ? snap.cascadeId.trim() : '';
|
|
207
|
+
if (cid) contextCascadeId = cid;
|
|
208
|
+
})
|
|
209
|
+
: () => {};
|
|
210
|
+
|
|
211
|
+
const getCliLabel = () => String(settings.cliLabel || '').trim() || 'CLI';
|
|
212
|
+
const getDefaultCommandPreview = () => String(settings.cliDefaultCommand || '').trim() || 'agy';
|
|
213
|
+
|
|
214
|
+
const getUnitNormalTitle = (unitId) => {
|
|
215
|
+
if (unitId === 'cli') return getDefaultCommandPreview();
|
|
216
|
+
const rec = quickUnits.get(unitId);
|
|
217
|
+
return rec ? rec.command : '';
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const setUnitFeedbackTimer = (unitId, ms) => {
|
|
221
|
+
const prev = feedbackTimers.get(unitId);
|
|
222
|
+
if (prev) clearTimeout(prev);
|
|
223
|
+
const tid = setTimeout(() => {
|
|
224
|
+
feedbackTimers.delete(unitId);
|
|
225
|
+
void ctx.topbar.updateUnit(unitId, { tone: '', title: getUnitNormalTitle(unitId) }).catch?.(() => {});
|
|
226
|
+
}, ms);
|
|
227
|
+
feedbackTimers.set(unitId, tid);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const launchCli = async (payload = {}, unitId = 'cli') => {
|
|
231
|
+
const targetUnitId = unitId || 'cli';
|
|
232
|
+
const prevTimer = feedbackTimers.get(targetUnitId);
|
|
233
|
+
if (prevTimer) {
|
|
234
|
+
clearTimeout(prevTimer);
|
|
235
|
+
feedbackTimers.delete(targetUnitId);
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
void ctx.topbar.updateUnit(targetUnitId, { tone: 'accent' }).catch?.(() => {});
|
|
239
|
+
} catch {}
|
|
240
|
+
const docRef = popoverNode?.ownerDocument || globalThis.document;
|
|
241
|
+
const clickCtx = collectClickWorkspaceContext(ctx, docRef, contextCascadeId);
|
|
242
|
+
const body = {
|
|
243
|
+
...payload,
|
|
244
|
+
...(clickCtx.cascadeId ? { cascadeId: clickCtx.cascadeId } : {}),
|
|
245
|
+
...(clickCtx.contextCascadeId ? { contextCascadeId: clickCtx.contextCascadeId } : {}),
|
|
246
|
+
workspaceHints: clickCtx.workspaceHints
|
|
247
|
+
};
|
|
248
|
+
try {
|
|
249
|
+
const res = await ctx.bridge.post('/launch', body);
|
|
250
|
+
if (!res || res.ok === false) {
|
|
251
|
+
const errMsg = res?.error || '启动终端失败';
|
|
252
|
+
console.warn('[agy-daddy.cli] launch failed:', errMsg);
|
|
253
|
+
void ctx.topbar.updateUnit(targetUnitId, { tone: 'danger', title: `启动失败: ${errMsg}` }).catch?.(() => {});
|
|
254
|
+
setUnitFeedbackTimer(targetUnitId, 4000);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
setUnitFeedbackTimer(targetUnitId, 400);
|
|
258
|
+
} catch (err) {
|
|
259
|
+
const errMsg = err?.message || String(err);
|
|
260
|
+
console.warn('[agy-daddy.cli] launch error:', errMsg);
|
|
261
|
+
void ctx.topbar.updateUnit(targetUnitId, { tone: 'danger', title: `启动失败: ${errMsg}` }).catch?.(() => {});
|
|
262
|
+
setUnitFeedbackTimer(targetUnitId, 4000);
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const renderPopover = () => {
|
|
267
|
+
if (!popoverNode) return;
|
|
268
|
+
const docRef = popoverNode.ownerDocument || globalThis.document;
|
|
269
|
+
const commands = parseQuickCommands(settings.quickCommands);
|
|
270
|
+
const cliLabel = getCliLabel();
|
|
271
|
+
const defaultCmd = getDefaultCommandPreview();
|
|
272
|
+
if (!docRef?.createElement) {
|
|
273
|
+
popoverNode.textContent = [cliLabel, defaultCmd, ...commands.map((c) => `${c.name}: ${c.command}`)].join('\n');
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
popoverNode.textContent = '';
|
|
277
|
+
|
|
278
|
+
const defaultCard = docRef.createElement('div');
|
|
279
|
+
defaultCard.className = 'settings-card-box';
|
|
280
|
+
const metaRow = docRef.createElement('div');
|
|
281
|
+
metaRow.className = 'ag-card-title';
|
|
282
|
+
const titleSpan = docRef.createElement('span');
|
|
283
|
+
titleSpan.textContent = cliLabel;
|
|
284
|
+
const permChip = docRef.createElement('span');
|
|
285
|
+
permChip.className = 'ag-chip';
|
|
286
|
+
permChip.textContent = getPermissionLabel(settings.cliPermissionMode);
|
|
287
|
+
metaRow.appendChild(titleSpan);
|
|
288
|
+
metaRow.appendChild(permChip);
|
|
289
|
+
defaultCard.appendChild(metaRow);
|
|
290
|
+
|
|
291
|
+
const infoLine = docRef.createElement('div');
|
|
292
|
+
infoLine.className = 'ag-muted';
|
|
293
|
+
const parts = [`终端: ${settings.terminalApp || 'auto'}`];
|
|
294
|
+
if (settings.cliDefaultModel) parts.push(`模型: ${settings.cliDefaultModel}`);
|
|
295
|
+
if (settings.cliMuxHost) parts.push(`SSH: ${settings.cliMuxHost}`);
|
|
296
|
+
parts.push(`命令: ${defaultCmd}`);
|
|
297
|
+
infoLine.textContent = parts.join(' · ');
|
|
298
|
+
defaultCard.appendChild(infoLine);
|
|
299
|
+
|
|
300
|
+
const cwdLine = docRef.createElement('div');
|
|
301
|
+
cwdLine.className = 'ag-muted';
|
|
302
|
+
cwdLine.setAttribute('data-cli-workspace-preview', 'true');
|
|
303
|
+
cwdLine.textContent = '工作区: 探测中...';
|
|
304
|
+
defaultCard.appendChild(cwdLine);
|
|
305
|
+
if (typeof ctx.bridge?.post === 'function') {
|
|
306
|
+
const clickCtx = collectClickWorkspaceContext(ctx, docRef, contextCascadeId);
|
|
307
|
+
void ctx.bridge.post('/workspace', clickCtx).then((wsRes) => {
|
|
308
|
+
if (wsRes?.ok && wsRes.cwd) {
|
|
309
|
+
cwdLine.textContent = `工作区: ${wsRes.cwd}`;
|
|
310
|
+
} else {
|
|
311
|
+
cwdLine.textContent = '工作区: 将在点击时自动探测';
|
|
312
|
+
}
|
|
313
|
+
}).catch(() => {
|
|
314
|
+
cwdLine.textContent = '工作区: 将在点击时自动探测';
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const launchBtn = docRef.createElement('button');
|
|
319
|
+
launchBtn.type = 'button';
|
|
320
|
+
launchBtn.className = 'ag-btn ag-btn-primary';
|
|
321
|
+
launchBtn.setAttribute('data-cli-launch-default', 'true');
|
|
322
|
+
launchBtn.textContent = `启动 ${cliLabel}`;
|
|
323
|
+
launchBtn.addEventListener('click', () => {
|
|
324
|
+
popover?.toggle?.(false);
|
|
325
|
+
void launchCli({ commandName: cliLabel }, 'cli');
|
|
326
|
+
});
|
|
327
|
+
defaultCard.appendChild(launchBtn);
|
|
328
|
+
popoverNode.appendChild(defaultCard);
|
|
329
|
+
|
|
330
|
+
if (commands.length > 0) {
|
|
331
|
+
const cmdCard = docRef.createElement('div');
|
|
332
|
+
cmdCard.className = 'settings-card-box';
|
|
333
|
+
const cmdHead = docRef.createElement('div');
|
|
334
|
+
cmdHead.className = 'ag-card-title';
|
|
335
|
+
const cmdTitle = docRef.createElement('span');
|
|
336
|
+
cmdTitle.textContent = '自定义快捷命令';
|
|
337
|
+
cmdHead.appendChild(cmdTitle);
|
|
338
|
+
cmdCard.appendChild(cmdHead);
|
|
339
|
+
|
|
340
|
+
for (const cmd of commands) {
|
|
341
|
+
const itemBtn = docRef.createElement('button');
|
|
342
|
+
itemBtn.type = 'button';
|
|
343
|
+
itemBtn.className = 'ag-btn';
|
|
344
|
+
itemBtn.setAttribute('data-cli-quick-launch', cmd.id);
|
|
345
|
+
itemBtn.setAttribute('title', cmd.command);
|
|
346
|
+
itemBtn.textContent = `${cmd.name} (${cmd.command})`;
|
|
347
|
+
itemBtn.addEventListener('click', () => {
|
|
348
|
+
popover?.toggle?.(false);
|
|
349
|
+
void launchCli({ rawCommand: cmd.command, commandName: cmd.name }, cmd.id);
|
|
350
|
+
});
|
|
351
|
+
cmdCard.appendChild(itemBtn);
|
|
352
|
+
}
|
|
353
|
+
popoverNode.appendChild(cmdCard);
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
const syncQuickUnits = () => {
|
|
358
|
+
const commands = parseQuickCommands(settings.quickCommands);
|
|
359
|
+
const desired = new Map();
|
|
360
|
+
commands.forEach((cmd, idx) => {
|
|
361
|
+
if (cmd.pinToTopbar === false) return;
|
|
362
|
+
desired.set(cmd.id, {
|
|
363
|
+
id: cmd.id,
|
|
364
|
+
order: 41 + idx,
|
|
365
|
+
label: cmd.name,
|
|
366
|
+
command: cmd.command
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
for (const [id, rec] of Array.from(quickUnits.entries())) {
|
|
371
|
+
if (!desired.has(id)) {
|
|
372
|
+
try { if (typeof rec.dispose === 'function') rec.dispose(); } catch {}
|
|
373
|
+
quickUnits.delete(id);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
for (const [id, spec] of desired.entries()) {
|
|
378
|
+
const existing = quickUnits.get(id);
|
|
379
|
+
const clickHandler = () => {
|
|
380
|
+
void launchCli({ rawCommand: spec.command, commandName: spec.label }, id);
|
|
381
|
+
};
|
|
382
|
+
if (existing) {
|
|
383
|
+
existing.label = spec.label;
|
|
384
|
+
existing.command = spec.command;
|
|
385
|
+
existing.order = spec.order;
|
|
386
|
+
void ctx.topbar.updateUnit(id, {
|
|
387
|
+
label: spec.label,
|
|
388
|
+
title: spec.command,
|
|
389
|
+
order: spec.order,
|
|
390
|
+
onClick: clickHandler
|
|
391
|
+
});
|
|
392
|
+
} else {
|
|
393
|
+
const stopQuick = ctx.topbar.registerUnit({
|
|
394
|
+
id,
|
|
395
|
+
order: spec.order,
|
|
396
|
+
label: spec.label,
|
|
397
|
+
iconSvg: TERMINAL_ICON_SVG,
|
|
398
|
+
title: spec.command,
|
|
399
|
+
onClick: clickHandler
|
|
400
|
+
});
|
|
401
|
+
quickUnits.set(id, {
|
|
402
|
+
dispose: stopQuick,
|
|
403
|
+
label: spec.label,
|
|
404
|
+
command: spec.command,
|
|
405
|
+
order: spec.order
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
const stopUnit = ctx.topbar.registerUnit({
|
|
412
|
+
id: 'cli',
|
|
413
|
+
order: 40,
|
|
414
|
+
label: getCliLabel(),
|
|
415
|
+
title: getDefaultCommandPreview(),
|
|
416
|
+
iconSvg: TERMINAL_ICON_SVG,
|
|
417
|
+
onClick() {
|
|
418
|
+
const commands = parseQuickCommands(settings.quickCommands);
|
|
419
|
+
const hasMenuItems = commands.some((c) => c.pinToTopbar === false);
|
|
420
|
+
if (hasMenuItems && popover) {
|
|
421
|
+
popover.toggle();
|
|
422
|
+
} else {
|
|
423
|
+
void launchCli({ commandName: getCliLabel() }, 'cli');
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
if (typeof ctx.topbar.registerPopover === 'function') {
|
|
429
|
+
popover = ctx.topbar.registerPopover({
|
|
430
|
+
id: 'cli-launcher',
|
|
431
|
+
anchorId: 'cli',
|
|
432
|
+
title: 'CLI 快速启动',
|
|
433
|
+
render(bodyEl) {
|
|
434
|
+
popoverNode = bodyEl;
|
|
435
|
+
renderPopover();
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
syncQuickUnits();
|
|
441
|
+
|
|
442
|
+
const stopSettingsSub = ctx.settings.onChange((nextValues) => {
|
|
443
|
+
if (nextValues && typeof nextValues === 'object') {
|
|
444
|
+
settings = { ...settings, ...nextValues };
|
|
445
|
+
void ctx.topbar.updateUnit('cli', {
|
|
446
|
+
label: getCliLabel(),
|
|
447
|
+
title: getDefaultCommandPreview()
|
|
448
|
+
});
|
|
449
|
+
syncQuickUnits();
|
|
450
|
+
renderPopover();
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
disposers.push(
|
|
455
|
+
() => {
|
|
456
|
+
for (const tid of feedbackTimers.values()) {
|
|
457
|
+
clearTimeout(tid);
|
|
458
|
+
}
|
|
459
|
+
feedbackTimers.clear();
|
|
460
|
+
},
|
|
461
|
+
() => {
|
|
462
|
+
for (const rec of quickUnits.values()) {
|
|
463
|
+
try { if (typeof rec.dispose === 'function') rec.dispose(); } catch {}
|
|
464
|
+
}
|
|
465
|
+
quickUnits.clear();
|
|
466
|
+
},
|
|
467
|
+
() => {
|
|
468
|
+
try { popover?.dispose?.(); } catch {}
|
|
469
|
+
popover = null;
|
|
470
|
+
popoverNode = null;
|
|
471
|
+
},
|
|
472
|
+
stopUnit,
|
|
473
|
+
stopSettingsSub,
|
|
474
|
+
stopContextSub
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function dispose() {
|
|
479
|
+
for (const fn of disposers) {
|
|
480
|
+
try { if (typeof fn === 'function') fn(); } catch {}
|
|
481
|
+
}
|
|
482
|
+
disposers = [];
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
module.exports = {
|
|
486
|
+
TERMINAL_ICON_SVG,
|
|
487
|
+
parseQuickCommands,
|
|
488
|
+
collectClickWorkspaceContext,
|
|
489
|
+
mount,
|
|
490
|
+
dispose
|
|
491
|
+
};
|