@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
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
{
|
|
2
|
+
"title": { "zh-CN": "CLI 终端启动器", "en": "CLI Terminal Launcher" },
|
|
3
|
+
"fields": [
|
|
4
|
+
{
|
|
5
|
+
"key": "terminalApp",
|
|
6
|
+
"type": "enum",
|
|
7
|
+
"default": "auto",
|
|
8
|
+
"group": { "zh-CN": "终端环境", "en": "Terminal Environment" },
|
|
9
|
+
"options": [
|
|
10
|
+
{ "value": "auto", "label": "自动探测" },
|
|
11
|
+
{ "value": "terminal", "label": "macOS Terminal.app" },
|
|
12
|
+
{ "value": "iterm2", "label": "iTerm2" },
|
|
13
|
+
{ "value": "ghostty", "label": "Ghostty" },
|
|
14
|
+
{ "value": "warp", "label": "Warp" },
|
|
15
|
+
{ "value": "kitty", "label": "Kitty" },
|
|
16
|
+
{ "value": "wezterm", "label": "WezTerm" },
|
|
17
|
+
{ "value": "cmux", "label": "cmux" },
|
|
18
|
+
{ "value": "alacritty", "label": "Alacritty" },
|
|
19
|
+
{ "value": "rex", "label": "Rex" },
|
|
20
|
+
{ "value": "tmux", "label": "tmux" },
|
|
21
|
+
{ "value": "wt", "label": "Windows Terminal" },
|
|
22
|
+
{ "value": "powershell", "label": "PowerShell" },
|
|
23
|
+
{ "value": "wsl", "label": "WSL" }
|
|
24
|
+
],
|
|
25
|
+
"label": { "zh-CN": "目标终端 / 多路复用器", "en": "Target terminal / multiplexer" },
|
|
26
|
+
"description": { "zh-CN": "从顶栏点击 CLI 胶囊时优先唤起的终端或会话复用器", "en": "Terminal or multiplexer launched when clicking the topbar CLI capsule" }
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"key": "customTerminalPath",
|
|
30
|
+
"type": "string",
|
|
31
|
+
"default": "",
|
|
32
|
+
"group": { "zh-CN": "终端环境", "en": "Terminal Environment" },
|
|
33
|
+
"placeholder": "/Applications/Ghostty.app",
|
|
34
|
+
"label": { "zh-CN": "终端程序路径", "en": "Terminal executable path" },
|
|
35
|
+
"description": { "zh-CN": "切换终端后自动检测并回显路径;检测失败时请手动输入或点击「浏览选择」", "en": "Auto-detected on terminal switch; enter a path or browse if detection fails" },
|
|
36
|
+
"probe": {
|
|
37
|
+
"command": "cli.detectTerminalPath",
|
|
38
|
+
"watchKeys": ["terminalApp"],
|
|
39
|
+
"autoFill": true,
|
|
40
|
+
"runOnOpen": true,
|
|
41
|
+
"buttonLabel": { "zh-CN": "重新检测", "en": "Detect" }
|
|
42
|
+
},
|
|
43
|
+
"browse": {
|
|
44
|
+
"command": "cli.browseTerminalPath",
|
|
45
|
+
"label": { "zh-CN": "浏览选择", "en": "Browse" }
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"key": "cliMuxHost",
|
|
50
|
+
"type": "string",
|
|
51
|
+
"default": "",
|
|
52
|
+
"group": { "zh-CN": "终端环境", "en": "Terminal Environment" },
|
|
53
|
+
"placeholder": "user@dev-box",
|
|
54
|
+
"options": [
|
|
55
|
+
{ "value": "", "label": { "zh-CN": "本地终端(留空)", "en": "Local terminal (empty)" } }
|
|
56
|
+
],
|
|
57
|
+
"optionsCommand": "cli.listMuxHosts",
|
|
58
|
+
"label": { "zh-CN": "SSH 多路复用主机", "en": "SSH mux host" },
|
|
59
|
+
"description": { "zh-CN": "支持下拉选择 ~/.ssh/config 主机或直接输入 user@host;留空则在本地启动", "en": "Select a host from ~/.ssh/config or type user@host; leave empty for local launch" }
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
"key": "cliLabel",
|
|
63
|
+
"type": "string",
|
|
64
|
+
"default": "CLI",
|
|
65
|
+
"group": { "zh-CN": "启动参数", "en": "Launch Options" },
|
|
66
|
+
"placeholder": "CLI",
|
|
67
|
+
"label": { "zh-CN": "默认按钮显示名称", "en": "Default button label" },
|
|
68
|
+
"description": { "zh-CN": "自定义顶栏默认 CLI 启动按钮与菜单标题的显示名称", "en": "Custom display name for the default CLI topbar button and launcher header" }
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"key": "cliDefaultModel",
|
|
72
|
+
"type": "string",
|
|
73
|
+
"default": "",
|
|
74
|
+
"group": { "zh-CN": "启动参数", "en": "Launch Options" },
|
|
75
|
+
"placeholder": "gemini-2.5-pro",
|
|
76
|
+
"options": [
|
|
77
|
+
{ "value": "", "label": { "zh-CN": "沿用 CLI 默认模型(留空)", "en": "Use CLI default (empty)" } },
|
|
78
|
+
{ "value": "gemini-2.5-pro", "label": "gemini-2.5-pro" },
|
|
79
|
+
{ "value": "gemini-2.5-flash", "label": "gemini-2.5-flash" },
|
|
80
|
+
{ "value": "gemini-3-pro-preview", "label": "gemini-3-pro-preview" },
|
|
81
|
+
{ "value": "claude-sonnet-4-5", "label": "claude-sonnet-4-5" },
|
|
82
|
+
{ "value": "claude-opus-4-6", "label": "claude-opus-4-6" }
|
|
83
|
+
],
|
|
84
|
+
"optionsCommand": "cli.listModels",
|
|
85
|
+
"label": { "zh-CN": "默认 CLI 模型", "en": "Default CLI model" },
|
|
86
|
+
"description": { "zh-CN": "支持下拉选择可用模型或直接输入自定义模型标识,留空沿用 CLI 默认配置", "en": "Select an available model or enter a custom model ID; leave blank for default" }
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"key": "cliPermissionMode",
|
|
90
|
+
"type": "enum",
|
|
91
|
+
"default": "default",
|
|
92
|
+
"group": { "zh-CN": "启动参数", "en": "Launch Options" },
|
|
93
|
+
"options": [
|
|
94
|
+
{ "value": "default", "label": { "zh-CN": "默认(沿用 CLI 默认权限)", "en": "Default (CLI default permissions)" } },
|
|
95
|
+
{ "value": "plan", "label": { "zh-CN": "规划只读模式 (--mode plan)", "en": "Plan mode (--mode plan)" } },
|
|
96
|
+
{ "value": "accept-edits", "label": { "zh-CN": "自动接受编辑 (--mode accept-edits)", "en": "Accept edits (--mode accept-edits)" } },
|
|
97
|
+
{ "value": "auto", "label": { "zh-CN": "自动执行模式 (--mode auto)", "en": "Auto mode (--mode auto)" } },
|
|
98
|
+
{ "value": "skip-permissions", "label": { "zh-CN": "免审核 / 跳过全部权限确认 (--dangerously-skip-permissions)", "en": "Skip all permissions (--dangerously-skip-permissions)" } }
|
|
99
|
+
],
|
|
100
|
+
"label": { "zh-CN": "启动权限模式", "en": "Launch permission mode" },
|
|
101
|
+
"description": { "zh-CN": "设置启动 agy 时的工具执行权限策略;「免审核」仅建议在受信任工作区使用", "en": "Permission policy passed to agy on launch; use skip-permissions only in trusted workspaces" }
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
"key": "cliDefaultCommand",
|
|
105
|
+
"type": "string",
|
|
106
|
+
"default": "",
|
|
107
|
+
"group": { "zh-CN": "启动参数", "en": "Launch Options" },
|
|
108
|
+
"placeholder": "agy",
|
|
109
|
+
"label": { "zh-CN": "默认启动命令", "en": "Default launch command" },
|
|
110
|
+
"description": { "zh-CN": "根据模型与权限选项自动拼接生成,支持直接编辑或追加参数;点击启动时会自动探测当前所处项目目录并在对应工作区绝对路径下启动 agy", "en": "Auto-composed from model & permission options; clicking launch automatically detects the active workspace directory and runs agy in its absolute path" },
|
|
111
|
+
"composer": {
|
|
112
|
+
"command": "cli.buildDefaultCommand",
|
|
113
|
+
"watchKeys": ["cliDefaultModel", "cliPermissionMode"],
|
|
114
|
+
"resetLabel": { "zh-CN": "恢复默认", "en": "Restore Default" },
|
|
115
|
+
"runOnOpen": true
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
"key": "quickCommands",
|
|
120
|
+
"type": "list",
|
|
121
|
+
"maxItems": 20,
|
|
122
|
+
"default": [],
|
|
123
|
+
"group": { "zh-CN": "顶栏快捷启动", "en": "Topbar Quick Launch" },
|
|
124
|
+
"label": { "zh-CN": "自定义快捷启动命令", "en": "Custom quick launch commands" },
|
|
125
|
+
"description": { "zh-CN": "添加任意终端命令用于顶栏快速启动(如 claude、codex、npm run dev 等),可选择固定在顶栏或收纳进 CLI 菜单", "en": "Add any terminal command to launch from the topbar (pinned button or CLI launcher menu)" },
|
|
126
|
+
"itemEditor": {
|
|
127
|
+
"kind": "command-preset",
|
|
128
|
+
"namePlaceholder": { "zh-CN": "按钮名称(如 Claude / Dev)", "en": "Label (e.g. Claude / Dev)" },
|
|
129
|
+
"commandPlaceholder": { "zh-CN": "任意终端命令(如 claude --continue)", "en": "Any terminal command" },
|
|
130
|
+
"allowPinToTopbar": true
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
],
|
|
134
|
+
"actions": [
|
|
135
|
+
{
|
|
136
|
+
"id": "launch",
|
|
137
|
+
"command": "cli.launch",
|
|
138
|
+
"label": { "zh-CN": "在新终端启动 CLI", "en": "Launch CLI in Terminal" }
|
|
139
|
+
}
|
|
140
|
+
]
|
|
141
|
+
}
|
package/src/node.js
ADDED
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const { spawn, execFile } = require('child_process');
|
|
7
|
+
const {
|
|
8
|
+
TERMINAL_CATALOG,
|
|
9
|
+
CLI_PERMISSION_MODES,
|
|
10
|
+
parseQuickCommands,
|
|
11
|
+
buildDefaultCliCommand,
|
|
12
|
+
getWindowsHelperPath,
|
|
13
|
+
normalizeWorkspacePath,
|
|
14
|
+
extractWorkspacesFromTrajectory,
|
|
15
|
+
resolveWorkspaceFromTrajectories,
|
|
16
|
+
extractWorkspaceFromBrainDir,
|
|
17
|
+
sanitizeMuxHost,
|
|
18
|
+
detectAvailableTerminals,
|
|
19
|
+
detectTerminalPath,
|
|
20
|
+
verifyTerminalPath,
|
|
21
|
+
listSshMuxHosts,
|
|
22
|
+
buildLaunchPlan
|
|
23
|
+
} = require('./terminals');
|
|
24
|
+
|
|
25
|
+
const DEFAULT_MODEL_OPTIONS = [
|
|
26
|
+
{ value: '', label: '沿用 CLI 默认模型(留空)' },
|
|
27
|
+
{ value: 'gemini-2.5-pro', label: 'gemini-2.5-pro' },
|
|
28
|
+
{ value: 'gemini-2.5-flash', label: 'gemini-2.5-flash' },
|
|
29
|
+
{ value: 'gemini-3-pro-preview', label: 'gemini-3-pro-preview' },
|
|
30
|
+
{ value: 'claude-sonnet-4-5', label: 'claude-sonnet-4-5' },
|
|
31
|
+
{ value: 'claude-opus-4-6', label: 'claude-opus-4-6' }
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
let disposers = [];
|
|
35
|
+
|
|
36
|
+
function isTestEnv() {
|
|
37
|
+
return process.env.AGY_DADDY_TEST_MODE === '1' || process.env.NODE_ENV === 'test';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function normalizeManagedSettings(raw = {}) {
|
|
41
|
+
const rawMode = typeof raw.cliPermissionMode === 'string' ? raw.cliPermissionMode.trim() : '';
|
|
42
|
+
let cliPermissionMode = CLI_PERMISSION_MODES.includes(rawMode) ? rawMode : '';
|
|
43
|
+
if (!cliPermissionMode) {
|
|
44
|
+
cliPermissionMode = raw.cliSkipPermissions ? 'skip-permissions' : 'default';
|
|
45
|
+
}
|
|
46
|
+
const cliDefaultModel = typeof raw.cliDefaultModel === 'string' ? raw.cliDefaultModel : '';
|
|
47
|
+
const cliLabel = typeof raw.cliLabel === 'string' && raw.cliLabel.trim() ? raw.cliLabel.trim() : 'CLI';
|
|
48
|
+
const cliDefaultCommand = typeof raw.cliDefaultCommand === 'string'
|
|
49
|
+
? raw.cliDefaultCommand
|
|
50
|
+
: buildDefaultCliCommand({ cliBin: 'agy', cliDefaultModel, cliPermissionMode });
|
|
51
|
+
return {
|
|
52
|
+
terminalApp: typeof raw.terminalApp === 'string' && raw.terminalApp ? raw.terminalApp : 'auto',
|
|
53
|
+
customTerminalPath: typeof raw.customTerminalPath === 'string' ? raw.customTerminalPath : '',
|
|
54
|
+
cliMuxHost: typeof raw.cliMuxHost === 'string' ? raw.cliMuxHost : '',
|
|
55
|
+
cliLabel,
|
|
56
|
+
cliDefaultModel,
|
|
57
|
+
cliPermissionMode,
|
|
58
|
+
cliDefaultCommand,
|
|
59
|
+
cliSkipPermissions: cliPermissionMode === 'skip-permissions',
|
|
60
|
+
quickCommands: Array.isArray(raw.quickCommands) ? raw.quickCommands.map((v) => String(v)) : []
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function spawnDetachedSafely(executable, args = [], spawnOpts = {}) {
|
|
65
|
+
return new Promise((resolve) => {
|
|
66
|
+
let settled = false;
|
|
67
|
+
let timer = null;
|
|
68
|
+
const done = (result) => {
|
|
69
|
+
if (settled) return;
|
|
70
|
+
settled = true;
|
|
71
|
+
if (timer) clearTimeout(timer);
|
|
72
|
+
resolve(result);
|
|
73
|
+
};
|
|
74
|
+
let child;
|
|
75
|
+
try {
|
|
76
|
+
child = spawn(executable, args, {
|
|
77
|
+
detached: true,
|
|
78
|
+
stdio: 'ignore',
|
|
79
|
+
...spawnOpts
|
|
80
|
+
});
|
|
81
|
+
} catch (err) {
|
|
82
|
+
done({ ok: false, error: err?.message || String(err), code: err?.code || 'SPAWN_FAILED' });
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
child.on('error', (err) => {
|
|
86
|
+
if (!settled) {
|
|
87
|
+
done({ ok: false, error: err?.message || String(err), code: err?.code || 'SPAWN_FAILED' });
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
child.once('spawn', () => {
|
|
91
|
+
try { child.unref(); } catch {}
|
|
92
|
+
done({ ok: true, pid: child.pid || null });
|
|
93
|
+
});
|
|
94
|
+
timer = setTimeout(() => {
|
|
95
|
+
try { child.unref(); } catch {}
|
|
96
|
+
done({ ok: true, pid: child.pid || null });
|
|
97
|
+
}, 200);
|
|
98
|
+
if (typeof timer.unref === 'function') timer.unref();
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function runExecFile(cmd, args = [], opts = {}) {
|
|
103
|
+
return new Promise((resolve) => {
|
|
104
|
+
execFile(cmd, args, { timeout: opts.timeout || 30000 }, (err, stdout, stderr) => {
|
|
105
|
+
resolve({
|
|
106
|
+
ok: !err,
|
|
107
|
+
code: err?.code ?? 0,
|
|
108
|
+
stdout: String(stdout || ''),
|
|
109
|
+
stderr: String(stderr || ''),
|
|
110
|
+
error: err?.message || ''
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function activate(host) {
|
|
117
|
+
let settings = normalizeManagedSettings(host.settings.get());
|
|
118
|
+
let syncingSettings = false;
|
|
119
|
+
|
|
120
|
+
const stopSettingsSub = host.settings.onChange((nextValues) => {
|
|
121
|
+
if (syncingSettings) return;
|
|
122
|
+
settings = normalizeManagedSettings(nextValues);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
async function resolveCliEnv() {
|
|
126
|
+
let base = { ...process.env };
|
|
127
|
+
try {
|
|
128
|
+
const proxyRes = await host.services.call('proxy', 'getCliEnv', [base], { optional: true });
|
|
129
|
+
if (proxyRes?.ok && proxyRes.value && typeof proxyRes.value === 'object') {
|
|
130
|
+
return proxyRes.value;
|
|
131
|
+
}
|
|
132
|
+
} catch {}
|
|
133
|
+
delete base.ELECTRON_RUN_AS_NODE;
|
|
134
|
+
delete base.NO_COLOR;
|
|
135
|
+
base.TERM = 'xterm-256color';
|
|
136
|
+
base.COLORTERM = 'truecolor';
|
|
137
|
+
return base;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function resolveLaunchWorkspace(opts = {}) {
|
|
141
|
+
const platform = opts.platform || process.platform;
|
|
142
|
+
const rawMux = opts.muxHost !== undefined ? opts.muxHost : settings.cliMuxHost;
|
|
143
|
+
const muxClean = rawMux ? sanitizeMuxHost(rawMux) : '';
|
|
144
|
+
const localOnly = !muxClean;
|
|
145
|
+
const existsFn = typeof opts.existsSync === 'function' ? opts.existsSync : fs.existsSync;
|
|
146
|
+
const statFn = typeof opts.statSync === 'function' ? opts.statSync : fs.statSync;
|
|
147
|
+
|
|
148
|
+
if (typeof opts.cwd === 'string' && opts.cwd.trim()) {
|
|
149
|
+
const explicitCwd = normalizeWorkspacePath(opts.cwd, { platform });
|
|
150
|
+
if (explicitCwd) {
|
|
151
|
+
return { cwd: explicitCwd, source: 'explicit', matchedCascadeId: opts.cascadeId || null };
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const hints = opts.workspaceHints && typeof opts.workspaceHints === 'object' ? opts.workspaceHints : null;
|
|
156
|
+
if (hints && Array.isArray(hints.domPaths) && hints.domPaths.length > 0) {
|
|
157
|
+
const fromDom = resolveWorkspaceFromTrajectories(null, {
|
|
158
|
+
workspaceHints: hints,
|
|
159
|
+
localOnly,
|
|
160
|
+
platform,
|
|
161
|
+
existsSync: existsFn,
|
|
162
|
+
statSync: statFn
|
|
163
|
+
});
|
|
164
|
+
if (fromDom && fromDom.cwd) {
|
|
165
|
+
return fromDom;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const cascadeId = typeof opts.cascadeId === 'string' ? opts.cascadeId.trim() : '';
|
|
170
|
+
const contextCascadeId = typeof opts.contextCascadeId === 'string' ? opts.contextCascadeId.trim() : '';
|
|
171
|
+
|
|
172
|
+
if (host.rpc && typeof host.rpc.call === 'function') {
|
|
173
|
+
try {
|
|
174
|
+
const allResp = await host.rpc.call('GetAllCascadeTrajectories', {
|
|
175
|
+
metadata: { ideName: 'antigravity', extensionName: 'antigravity' }
|
|
176
|
+
});
|
|
177
|
+
const summaries = allResp?.trajectorySummaries || {};
|
|
178
|
+
const fromRpc = resolveWorkspaceFromTrajectories(summaries, {
|
|
179
|
+
cascadeId,
|
|
180
|
+
contextCascadeId,
|
|
181
|
+
workspaceHints: hints,
|
|
182
|
+
localOnly,
|
|
183
|
+
platform,
|
|
184
|
+
existsSync: existsFn,
|
|
185
|
+
statSync: statFn
|
|
186
|
+
});
|
|
187
|
+
if (fromRpc && fromRpc.cwd) {
|
|
188
|
+
return fromRpc;
|
|
189
|
+
}
|
|
190
|
+
} catch {}
|
|
191
|
+
|
|
192
|
+
const targetCascadeId = cascadeId || contextCascadeId;
|
|
193
|
+
if (targetCascadeId) {
|
|
194
|
+
try {
|
|
195
|
+
const trajResp = await host.rpc.call('GetCascadeTrajectory', { cascadeId: targetCascadeId });
|
|
196
|
+
const cands = extractWorkspacesFromTrajectory(trajResp, { platform });
|
|
197
|
+
const chosen = cands.find((cand) => {
|
|
198
|
+
if (!localOnly) return true;
|
|
199
|
+
try { return existsFn(cand); } catch { return false; }
|
|
200
|
+
}) || cands[0];
|
|
201
|
+
if (chosen) {
|
|
202
|
+
return { cwd: chosen, source: 'rpc-trajectory', matchedCascadeId: targetCascadeId };
|
|
203
|
+
}
|
|
204
|
+
} catch {}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const brainCascadeId = cascadeId || contextCascadeId;
|
|
209
|
+
if (brainCascadeId) {
|
|
210
|
+
const fromBrain = serviceMethods.resolveWorkspaceFromBrain(opts.brainDir, brainCascadeId, { platform });
|
|
211
|
+
if (fromBrain) {
|
|
212
|
+
return { cwd: fromBrain, source: 'brain', matchedCascadeId: brainCascadeId };
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const fallbackCwd = normalizeWorkspacePath(process.cwd(), { platform }) || process.cwd();
|
|
217
|
+
return { cwd: fallbackCwd, source: 'process-cwd', matchedCascadeId: null };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const serviceMethods = {
|
|
221
|
+
listTerminals(opts = {}) {
|
|
222
|
+
return detectAvailableTerminals(opts);
|
|
223
|
+
},
|
|
224
|
+
getTerminalCatalog() {
|
|
225
|
+
return [...TERMINAL_CATALOG];
|
|
226
|
+
},
|
|
227
|
+
resolveWorkspaceFromBrain(brainDir, cascadeId, opts = {}) {
|
|
228
|
+
const dir = brainDir || process.env.AGY_DADDY_BRAIN_DIR || path.join(os.homedir(), '.gemini', 'antigravity', 'brain');
|
|
229
|
+
return extractWorkspaceFromBrainDir(dir, cascadeId, opts);
|
|
230
|
+
},
|
|
231
|
+
async resolveWorkspace(opts = {}) {
|
|
232
|
+
const resolved = await resolveLaunchWorkspace(opts || {});
|
|
233
|
+
return { ok: true, ...resolved };
|
|
234
|
+
},
|
|
235
|
+
getWindowsHelperPath() {
|
|
236
|
+
return getWindowsHelperPath();
|
|
237
|
+
},
|
|
238
|
+
getSettings() {
|
|
239
|
+
return { ...settings };
|
|
240
|
+
},
|
|
241
|
+
async saveSettings(patch = {}) {
|
|
242
|
+
const cleanPatch = {};
|
|
243
|
+
for (const k of ['terminalApp', 'customTerminalPath', 'cliMuxHost', 'cliLabel', 'cliDefaultModel', 'cliPermissionMode', 'cliDefaultCommand', 'quickCommands']) {
|
|
244
|
+
if (patch[k] !== undefined) cleanPatch[k] = patch[k];
|
|
245
|
+
}
|
|
246
|
+
if (patch.cliSkipPermissions !== undefined && cleanPatch.cliPermissionMode === undefined) {
|
|
247
|
+
cleanPatch.cliPermissionMode = patch.cliSkipPermissions ? 'skip-permissions' : 'default';
|
|
248
|
+
}
|
|
249
|
+
settings = normalizeManagedSettings({ ...settings, ...cleanPatch });
|
|
250
|
+
if (Object.keys(cleanPatch).length > 0) {
|
|
251
|
+
syncingSettings = true;
|
|
252
|
+
try {
|
|
253
|
+
await host.settings.update(cleanPatch);
|
|
254
|
+
} finally {
|
|
255
|
+
syncingSettings = false;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return { ...settings };
|
|
259
|
+
},
|
|
260
|
+
detectTerminalPath(opts = {}) {
|
|
261
|
+
const values = opts.values && typeof opts.values === 'object' ? opts.values : {};
|
|
262
|
+
const trigger = String(opts.trigger || 'manual');
|
|
263
|
+
const targetApp = opts.terminalApp || values.terminalApp || settings.terminalApp || 'auto';
|
|
264
|
+
const customPath = opts.customTerminalPath !== undefined
|
|
265
|
+
? String(opts.customTerminalPath || '').trim()
|
|
266
|
+
: (values.customTerminalPath !== undefined ? String(values.customTerminalPath || '').trim() : String(settings.customTerminalPath || '').trim());
|
|
267
|
+
|
|
268
|
+
const detectOpts = {
|
|
269
|
+
platform: opts.platform || process.platform,
|
|
270
|
+
env: opts.env,
|
|
271
|
+
existsSync: opts.existsSync,
|
|
272
|
+
homeDir: opts.homeDir
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
if (trigger === 'watch' || trigger === 'manual') {
|
|
276
|
+
return detectTerminalPath(targetApp, detectOpts);
|
|
277
|
+
}
|
|
278
|
+
if (customPath) {
|
|
279
|
+
return verifyTerminalPath(customPath, detectOpts);
|
|
280
|
+
}
|
|
281
|
+
return detectTerminalPath(targetApp, detectOpts);
|
|
282
|
+
},
|
|
283
|
+
async browseTerminalPath(opts = {}) {
|
|
284
|
+
if (typeof opts.pickerFn === 'function') {
|
|
285
|
+
return await opts.pickerFn(opts);
|
|
286
|
+
}
|
|
287
|
+
if (isTestEnv()) {
|
|
288
|
+
return { ok: true, cancelled: true, path: '' };
|
|
289
|
+
}
|
|
290
|
+
const platform = opts.platform || process.platform;
|
|
291
|
+
if (platform === 'darwin') {
|
|
292
|
+
const res = await runExecFile('osascript', [
|
|
293
|
+
'-e',
|
|
294
|
+
'tell current application to activate',
|
|
295
|
+
'-e',
|
|
296
|
+
'POSIX path of (choose file with prompt "选择终端程序或可执行文件" default location POSIX file "/Applications")'
|
|
297
|
+
], { timeout: 120000 });
|
|
298
|
+
if (!res.ok) {
|
|
299
|
+
return { ok: true, cancelled: true, path: '' };
|
|
300
|
+
}
|
|
301
|
+
const picked = res.stdout.trim().replace(/\/+$/, '');
|
|
302
|
+
return picked ? { ok: true, cancelled: false, path: picked } : { ok: true, cancelled: true, path: '' };
|
|
303
|
+
}
|
|
304
|
+
if (platform === 'win32') {
|
|
305
|
+
const psScript = [
|
|
306
|
+
'Add-Type -AssemblyName System.Windows.Forms',
|
|
307
|
+
'$f = New-Object System.Windows.Forms.OpenFileDialog',
|
|
308
|
+
'$f.Title = "选择终端程序"',
|
|
309
|
+
'if ($f.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { Write-Output $f.FileName }'
|
|
310
|
+
].join('; ');
|
|
311
|
+
const res = await runExecFile('powershell.exe', ['-NoProfile', '-Command', psScript], { timeout: 120000 });
|
|
312
|
+
const picked = res.ok ? res.stdout.trim() : '';
|
|
313
|
+
return picked ? { ok: true, cancelled: false, path: picked } : { ok: true, cancelled: true, path: '' };
|
|
314
|
+
}
|
|
315
|
+
const zen = await runExecFile('zenity', ['--file-selection', '--title=选择终端程序'], { timeout: 120000 });
|
|
316
|
+
if (zen.ok && zen.stdout.trim()) {
|
|
317
|
+
return { ok: true, cancelled: false, path: zen.stdout.trim() };
|
|
318
|
+
}
|
|
319
|
+
const kd = await runExecFile('kdialog', ['--getopenfilename', '/usr/bin'], { timeout: 120000 });
|
|
320
|
+
if (kd.ok && kd.stdout.trim()) {
|
|
321
|
+
return { ok: true, cancelled: false, path: kd.stdout.trim() };
|
|
322
|
+
}
|
|
323
|
+
return { ok: true, cancelled: true, path: '' };
|
|
324
|
+
},
|
|
325
|
+
listMuxHosts(opts = {}) {
|
|
326
|
+
const hosts = listSshMuxHosts(opts);
|
|
327
|
+
return {
|
|
328
|
+
ok: true,
|
|
329
|
+
options: [
|
|
330
|
+
{ value: '', label: '本地终端(留空)' },
|
|
331
|
+
...hosts
|
|
332
|
+
]
|
|
333
|
+
};
|
|
334
|
+
},
|
|
335
|
+
async listModels(opts = {}) {
|
|
336
|
+
const out = [];
|
|
337
|
+
const seen = new Set();
|
|
338
|
+
const pushOpt = (opt) => {
|
|
339
|
+
if (!opt) return;
|
|
340
|
+
const val = typeof opt === 'string' ? opt : String(opt.value ?? '');
|
|
341
|
+
if (seen.has(val)) return;
|
|
342
|
+
seen.add(val);
|
|
343
|
+
out.push(typeof opt === 'string' ? { value: opt, label: opt } : { value: val, label: String(opt.label || val), ...(opt.description ? { description: opt.description } : {}) });
|
|
344
|
+
};
|
|
345
|
+
pushOpt(DEFAULT_MODEL_OPTIONS[0]);
|
|
346
|
+
|
|
347
|
+
if (host.rpc && typeof host.rpc.call === 'function') {
|
|
348
|
+
try {
|
|
349
|
+
const resp = await host.rpc.call('GetAvailableModels', { forceRefresh: false });
|
|
350
|
+
const modelsObj = resp?.response?.models || resp?.models || {};
|
|
351
|
+
const entries = Array.isArray(modelsObj)
|
|
352
|
+
? modelsObj.map((m) => [m.model || m.modelId || m.id || '', m])
|
|
353
|
+
: Object.entries(modelsObj);
|
|
354
|
+
for (const [k, v] of entries) {
|
|
355
|
+
if (!v || typeof v !== 'object') continue;
|
|
356
|
+
const modelId = String(v.model || v.modelId || v.id || k || '').trim();
|
|
357
|
+
if (!modelId) continue;
|
|
358
|
+
const displayName = String(v.displayName || v.label || modelId).trim();
|
|
359
|
+
pushOpt({
|
|
360
|
+
value: modelId,
|
|
361
|
+
label: displayName,
|
|
362
|
+
description: displayName !== modelId ? modelId : ''
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
} catch {}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (Array.isArray(opts.extraModels)) {
|
|
369
|
+
for (const m of opts.extraModels) pushOpt(m);
|
|
370
|
+
}
|
|
371
|
+
for (const def of DEFAULT_MODEL_OPTIONS.slice(1)) {
|
|
372
|
+
pushOpt(def);
|
|
373
|
+
}
|
|
374
|
+
return { ok: true, options: out };
|
|
375
|
+
},
|
|
376
|
+
getQuickCommands() {
|
|
377
|
+
return parseQuickCommands(settings.quickCommands);
|
|
378
|
+
},
|
|
379
|
+
buildDefaultCommand(opts = {}) {
|
|
380
|
+
const values = opts.values && typeof opts.values === 'object' ? opts.values : {};
|
|
381
|
+
const cliDefaultModel = opts.cliDefaultModel !== undefined
|
|
382
|
+
? opts.cliDefaultModel
|
|
383
|
+
: (values.cliDefaultModel !== undefined ? values.cliDefaultModel : settings.cliDefaultModel);
|
|
384
|
+
const cliPermissionMode = opts.cliPermissionMode !== undefined
|
|
385
|
+
? opts.cliPermissionMode
|
|
386
|
+
: (values.cliPermissionMode !== undefined ? values.cliPermissionMode : settings.cliPermissionMode);
|
|
387
|
+
const cliBin = opts.cliBin || 'agy';
|
|
388
|
+
const command = buildDefaultCliCommand({ cliBin, cliDefaultModel, cliPermissionMode });
|
|
389
|
+
return { ok: true, value: command, command };
|
|
390
|
+
},
|
|
391
|
+
async planLaunch(opts = {}) {
|
|
392
|
+
const env = await resolveCliEnv();
|
|
393
|
+
const ws = await resolveLaunchWorkspace(opts);
|
|
394
|
+
const resolvedCwd = ws.cwd;
|
|
395
|
+
let rawCommand = typeof opts.rawCommand === 'string' ? opts.rawCommand.trim() : '';
|
|
396
|
+
if (!rawCommand && opts.commandId) {
|
|
397
|
+
const hit = parseQuickCommands(settings.quickCommands).find((c) => c.id === opts.commandId);
|
|
398
|
+
if (hit) rawCommand = hit.command;
|
|
399
|
+
}
|
|
400
|
+
const hasExplicitCliOverrides = opts.cliDefaultModel !== undefined
|
|
401
|
+
|| opts.cliPermissionMode !== undefined
|
|
402
|
+
|| opts.cliSkipPermissions !== undefined
|
|
403
|
+
|| (Array.isArray(opts.commandArgs) && opts.commandArgs.length > 0);
|
|
404
|
+
if (!rawCommand && !hasExplicitCliOverrides) {
|
|
405
|
+
const customDefaultCmd = typeof opts.cliDefaultCommand === 'string'
|
|
406
|
+
? opts.cliDefaultCommand.trim()
|
|
407
|
+
: String(settings.cliDefaultCommand || '').trim();
|
|
408
|
+
if (customDefaultCmd) {
|
|
409
|
+
rawCommand = customDefaultCmd;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
let cliPermissionMode = opts.cliPermissionMode;
|
|
413
|
+
if (cliPermissionMode === undefined && opts.cliSkipPermissions !== undefined) {
|
|
414
|
+
cliPermissionMode = opts.cliSkipPermissions ? 'skip-permissions' : 'default';
|
|
415
|
+
}
|
|
416
|
+
if (cliPermissionMode === undefined) {
|
|
417
|
+
cliPermissionMode = settings.cliPermissionMode;
|
|
418
|
+
}
|
|
419
|
+
return buildLaunchPlan({
|
|
420
|
+
terminalApp: opts.terminalApp || settings.terminalApp,
|
|
421
|
+
customTerminalPath: opts.customTerminalPath !== undefined ? opts.customTerminalPath : settings.customTerminalPath,
|
|
422
|
+
cwd: resolvedCwd,
|
|
423
|
+
workspaceSource: ws.source,
|
|
424
|
+
cliBin: opts.cliBin || 'agy',
|
|
425
|
+
commandArgs: opts.commandArgs,
|
|
426
|
+
rawCommand,
|
|
427
|
+
muxHost: opts.muxHost !== undefined ? opts.muxHost : settings.cliMuxHost,
|
|
428
|
+
cliPermissionMode,
|
|
429
|
+
cliSkipPermissions: opts.cliSkipPermissions,
|
|
430
|
+
cliDefaultModel: opts.cliDefaultModel !== undefined ? opts.cliDefaultModel : settings.cliDefaultModel,
|
|
431
|
+
platform: opts.platform || process.platform,
|
|
432
|
+
env
|
|
433
|
+
});
|
|
434
|
+
},
|
|
435
|
+
async launch(opts = {}) {
|
|
436
|
+
const plan = await serviceMethods.planLaunch(opts);
|
|
437
|
+
if (opts.dryRun || (isTestEnv() && !opts.testRealSpawn)) {
|
|
438
|
+
return { ok: true, dryRun: true, cwd: plan.cwd, workspaceSource: plan.workspaceSource, plan };
|
|
439
|
+
}
|
|
440
|
+
const spawnOpts = { env: plan.env };
|
|
441
|
+
if (!plan.muxHost && plan.cwd && (opts.platform || process.platform) === process.platform) {
|
|
442
|
+
try {
|
|
443
|
+
if (fs.existsSync(plan.cwd) && fs.statSync(plan.cwd).isDirectory()) {
|
|
444
|
+
spawnOpts.cwd = plan.cwd;
|
|
445
|
+
}
|
|
446
|
+
} catch {}
|
|
447
|
+
}
|
|
448
|
+
const spawnRes = await spawnDetachedSafely(plan.executable, plan.args, spawnOpts);
|
|
449
|
+
if (!spawnRes.ok) {
|
|
450
|
+
return {
|
|
451
|
+
ok: false,
|
|
452
|
+
dryRun: false,
|
|
453
|
+
cwd: plan.cwd,
|
|
454
|
+
workspaceSource: plan.workspaceSource,
|
|
455
|
+
terminalId: plan.terminalId,
|
|
456
|
+
executable: plan.executable,
|
|
457
|
+
error: spawnRes.error,
|
|
458
|
+
code: spawnRes.code
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
if (plan.activateApp) {
|
|
462
|
+
void spawnDetachedSafely('open', ['-a', plan.activateApp], {});
|
|
463
|
+
}
|
|
464
|
+
return {
|
|
465
|
+
ok: true,
|
|
466
|
+
dryRun: false,
|
|
467
|
+
cwd: plan.cwd,
|
|
468
|
+
workspaceSource: plan.workspaceSource,
|
|
469
|
+
pid: spawnRes.pid || null,
|
|
470
|
+
terminalId: plan.terminalId
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
const stopSvc = host.services.provide('cli', serviceMethods, { realm: 'node', version: '1.0.0' });
|
|
476
|
+
const stopTermRoute = await host.bridge.registerRoute('/terminals', async () => serviceMethods.listTerminals());
|
|
477
|
+
const stopDetectRoute = await host.bridge.registerRoute('/detect-terminal', async (body) => serviceMethods.detectTerminalPath(body || {}));
|
|
478
|
+
const stopWorkspaceRoute = await host.bridge.registerRoute('/workspace', async (body) => serviceMethods.resolveWorkspace(body || {}));
|
|
479
|
+
const stopPlanRoute = await host.bridge.registerRoute('/plan', async (body) => serviceMethods.planLaunch(body || {}));
|
|
480
|
+
const stopLaunchRoute = await host.bridge.registerRoute('/launch', async (body) => serviceMethods.launch(body || {}));
|
|
481
|
+
const stopLaunchCmd = host.commands.register('cli.launch', async (opts = {}) => {
|
|
482
|
+
const res = await serviceMethods.launch(opts || {});
|
|
483
|
+
if (!res.ok) {
|
|
484
|
+
return {
|
|
485
|
+
...res,
|
|
486
|
+
message: `启动失败:${res.error || '未知错误'}`
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
const label = opts?.commandName || settings.cliLabel || 'CLI';
|
|
490
|
+
const cwdSuffix = res.cwd ? `(${res.cwd})` : '';
|
|
491
|
+
return {
|
|
492
|
+
...res,
|
|
493
|
+
message: res.dryRun
|
|
494
|
+
? `已生成启动计划 (${res.plan?.terminalId || 'auto'})${cwdSuffix}`
|
|
495
|
+
: `已在 ${res.terminalId || '终端'} 启动 ${label}${cwdSuffix}`
|
|
496
|
+
};
|
|
497
|
+
});
|
|
498
|
+
const stopDetectCmd = host.commands.register('cli.detectTerminalPath', async (opts = {}) => serviceMethods.detectTerminalPath(opts || {}));
|
|
499
|
+
const stopBrowseCmd = host.commands.register('cli.browseTerminalPath', async (opts = {}) => serviceMethods.browseTerminalPath(opts || {}));
|
|
500
|
+
const stopMuxHostsCmd = host.commands.register('cli.listMuxHosts', async (opts = {}) => serviceMethods.listMuxHosts(opts || {}));
|
|
501
|
+
const stopModelsCmd = host.commands.register('cli.listModels', async (opts = {}) => serviceMethods.listModels(opts || {}));
|
|
502
|
+
const stopBuildDefaultCmd = host.commands.register('cli.buildDefaultCommand', async (opts = {}) => serviceMethods.buildDefaultCommand(opts || {}));
|
|
503
|
+
|
|
504
|
+
disposers.push(
|
|
505
|
+
stopSettingsSub,
|
|
506
|
+
stopSvc,
|
|
507
|
+
stopTermRoute,
|
|
508
|
+
stopDetectRoute,
|
|
509
|
+
stopWorkspaceRoute,
|
|
510
|
+
stopPlanRoute,
|
|
511
|
+
stopLaunchRoute,
|
|
512
|
+
stopLaunchCmd,
|
|
513
|
+
stopDetectCmd,
|
|
514
|
+
stopBrowseCmd,
|
|
515
|
+
stopMuxHostsCmd,
|
|
516
|
+
stopModelsCmd,
|
|
517
|
+
stopBuildDefaultCmd
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
async function deactivate() {
|
|
522
|
+
for (const fn of disposers) {
|
|
523
|
+
try { if (typeof fn === 'function') fn(); } catch {}
|
|
524
|
+
}
|
|
525
|
+
disposers = [];
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
module.exports = {
|
|
529
|
+
activate,
|
|
530
|
+
deactivate
|
|
531
|
+
};
|