@yivan-lab/pretty-please 1.1.0 → 1.2.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/README.md +283 -1
- package/bin/pls.tsx +1022 -104
- package/dist/bin/pls.js +894 -84
- package/dist/package.json +4 -4
- package/dist/src/alias.d.ts +41 -0
- package/dist/src/alias.js +240 -0
- package/dist/src/chat-history.js +10 -1
- package/dist/src/components/Chat.js +2 -1
- package/dist/src/components/CodeColorizer.js +26 -20
- package/dist/src/components/CommandBox.js +2 -1
- package/dist/src/components/ConfirmationPrompt.js +2 -1
- package/dist/src/components/Duration.js +2 -1
- package/dist/src/components/InlineRenderer.js +2 -1
- package/dist/src/components/MarkdownDisplay.js +2 -1
- package/dist/src/components/MultiStepCommandGenerator.d.ts +3 -1
- package/dist/src/components/MultiStepCommandGenerator.js +20 -10
- package/dist/src/components/TableRenderer.js +2 -1
- package/dist/src/config.d.ts +34 -3
- package/dist/src/config.js +71 -31
- package/dist/src/multi-step.d.ts +22 -6
- package/dist/src/multi-step.js +27 -4
- package/dist/src/remote-history.d.ts +63 -0
- package/dist/src/remote-history.js +315 -0
- package/dist/src/remote.d.ts +113 -0
- package/dist/src/remote.js +634 -0
- package/dist/src/shell-hook.d.ts +53 -0
- package/dist/src/shell-hook.js +242 -19
- package/dist/src/ui/theme.d.ts +27 -24
- package/dist/src/ui/theme.js +71 -21
- package/dist/src/upgrade.d.ts +41 -0
- package/dist/src/upgrade.js +348 -0
- package/dist/src/utils/console.js +22 -11
- package/package.json +4 -4
- package/src/alias.ts +301 -0
- package/src/chat-history.ts +11 -1
- package/src/components/Chat.tsx +2 -1
- package/src/components/CodeColorizer.tsx +27 -19
- package/src/components/CommandBox.tsx +2 -1
- package/src/components/ConfirmationPrompt.tsx +2 -1
- package/src/components/Duration.tsx +2 -1
- package/src/components/InlineRenderer.tsx +2 -1
- package/src/components/MarkdownDisplay.tsx +2 -1
- package/src/components/MultiStepCommandGenerator.tsx +25 -11
- package/src/components/TableRenderer.tsx +2 -1
- package/src/config.ts +117 -32
- package/src/multi-step.ts +43 -6
- package/src/remote-history.ts +390 -0
- package/src/remote.ts +800 -0
- package/src/shell-hook.ts +271 -19
- package/src/ui/theme.ts +101 -24
- package/src/upgrade.ts +397 -0
- package/src/utils/console.ts +22 -11
package/dist/bin/pls.js
CHANGED
|
@@ -1,31 +1,75 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import React from 'react';
|
|
3
|
-
import { render } from 'ink';
|
|
4
2
|
import { Command } from 'commander';
|
|
5
3
|
import { fileURLToPath } from 'url';
|
|
6
4
|
import { dirname, join } from 'path';
|
|
7
5
|
import { exec } from 'child_process';
|
|
8
6
|
import os from 'os';
|
|
9
7
|
import chalk from 'chalk';
|
|
10
|
-
|
|
11
|
-
import
|
|
8
|
+
// React 和 Ink 懒加载(只在需要 UI 时加载)
|
|
9
|
+
// import React from 'react'
|
|
10
|
+
// import { render } from 'ink'
|
|
11
|
+
// import { MultiStepCommandGenerator } from '../src/components/MultiStepCommandGenerator.js'
|
|
12
|
+
// import { Chat } from '../src/components/Chat.js'
|
|
12
13
|
import { isConfigValid, setConfigValue, getConfig, maskApiKey } from '../src/config.js';
|
|
13
14
|
import { clearHistory, addHistory, getHistory, getHistoryFilePath } from '../src/history.js';
|
|
14
15
|
import { clearChatHistory, getChatRoundCount, getChatHistoryFilePath, displayChatHistory } from '../src/chat-history.js';
|
|
15
16
|
import { installShellHook, uninstallShellHook, getHookStatus, detectShell, getShellConfigPath, displayShellHistory, clearShellHistory, } from '../src/shell-hook.js';
|
|
17
|
+
import { checkForUpdates, showUpdateNotice, performUpgrade, } from '../src/upgrade.js';
|
|
18
|
+
import { getCurrentTheme } from '../src/ui/theme.js';
|
|
19
|
+
import { addAlias, removeAlias, displayAliases, resolveAlias, } from '../src/alias.js';
|
|
20
|
+
import { addRemote, removeRemote, displayRemotes, getRemote, testRemoteConnection, sshExec, collectRemoteSysInfo, setRemoteWorkDir, getRemoteWorkDir, generateBatchRemoteCommands, executeBatchRemoteCommands, } from '../src/remote.js';
|
|
21
|
+
import { addRemoteHistory, displayRemoteHistory, clearRemoteHistory, fetchRemoteShellHistory, displayRemoteShellHistory, clearRemoteShellHistory, } from '../src/remote-history.js';
|
|
22
|
+
import { detectRemoteShell, getRemoteShellConfigPath, installRemoteShellHook, uninstallRemoteShellHook, getRemoteHookStatus, } from '../src/shell-hook.js';
|
|
23
|
+
// 获取主题颜色的辅助函数
|
|
24
|
+
function getThemeColors() {
|
|
25
|
+
const theme = getCurrentTheme();
|
|
26
|
+
return {
|
|
27
|
+
primary: theme.primary,
|
|
28
|
+
success: theme.success,
|
|
29
|
+
error: theme.error,
|
|
30
|
+
warning: theme.warning,
|
|
31
|
+
info: theme.info,
|
|
32
|
+
muted: theme.text.muted,
|
|
33
|
+
secondary: theme.text.secondary,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
16
36
|
import * as console2 from '../src/utils/console.js';
|
|
17
37
|
// 导入 package.json(Bun 会自动打包进二进制)
|
|
18
|
-
import packageJson from '../package.json';
|
|
38
|
+
import packageJson from '../package.json' with { type: 'json' };
|
|
19
39
|
// 保留这些用于其他可能的用途
|
|
20
40
|
const __filename = fileURLToPath(import.meta.url);
|
|
21
41
|
const __dirname = dirname(__filename);
|
|
22
42
|
const program = new Command();
|
|
43
|
+
// 启动时异步检查更新(不阻塞主流程)
|
|
44
|
+
let updateCheckResult = null;
|
|
45
|
+
const isUpgradeCommand = process.argv.includes('upgrade');
|
|
46
|
+
// 延迟更新检查到命令解析后(减少启动时间)
|
|
47
|
+
// 非 upgrade 命令时才检查更新
|
|
48
|
+
if (!isUpgradeCommand) {
|
|
49
|
+
// 延迟 100ms 开始检查,避免影响简单命令的响应速度
|
|
50
|
+
setTimeout(() => {
|
|
51
|
+
checkForUpdates(packageJson.version)
|
|
52
|
+
.then((result) => {
|
|
53
|
+
updateCheckResult = result;
|
|
54
|
+
})
|
|
55
|
+
.catch(() => {
|
|
56
|
+
// 静默失败
|
|
57
|
+
});
|
|
58
|
+
}, 100);
|
|
59
|
+
}
|
|
60
|
+
// 程序退出时显示更新提示
|
|
61
|
+
process.on('beforeExit', () => {
|
|
62
|
+
if (updateCheckResult?.hasUpdate && updateCheckResult.latestVersion && !isUpgradeCommand) {
|
|
63
|
+
showUpdateNotice(packageJson.version, updateCheckResult.latestVersion);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
23
66
|
/**
|
|
24
67
|
* 执行命令(原生版本)
|
|
25
68
|
*/
|
|
26
69
|
function executeCommand(command) {
|
|
27
70
|
return new Promise((resolve) => {
|
|
28
|
-
let
|
|
71
|
+
let stdout = '';
|
|
72
|
+
let stderr = '';
|
|
29
73
|
let hasOutput = false;
|
|
30
74
|
console.log(''); // 空行
|
|
31
75
|
// 计算命令框宽度,让分隔线长度一致
|
|
@@ -33,14 +77,15 @@ function executeCommand(command) {
|
|
|
33
77
|
const maxContentWidth = Math.max(...lines.map(l => console2.getDisplayWidth(l)));
|
|
34
78
|
const boxWidth = Math.max(maxContentWidth + 4, console2.getDisplayWidth('生成命令') + 6, 20);
|
|
35
79
|
console2.printSeparator('输出', boxWidth);
|
|
36
|
-
|
|
80
|
+
// 使用 bash 并启用 pipefail,确保管道中任何命令失败都能正确返回非零退出码
|
|
81
|
+
const child = exec(`set -o pipefail; ${command}`, { shell: '/bin/bash' });
|
|
37
82
|
child.stdout?.on('data', (data) => {
|
|
38
|
-
|
|
83
|
+
stdout += data;
|
|
39
84
|
hasOutput = true;
|
|
40
85
|
process.stdout.write(data);
|
|
41
86
|
});
|
|
42
87
|
child.stderr?.on('data', (data) => {
|
|
43
|
-
|
|
88
|
+
stderr += data;
|
|
44
89
|
hasOutput = true;
|
|
45
90
|
process.stderr.write(data);
|
|
46
91
|
});
|
|
@@ -48,7 +93,7 @@ function executeCommand(command) {
|
|
|
48
93
|
if (hasOutput) {
|
|
49
94
|
console2.printSeparator('', boxWidth);
|
|
50
95
|
}
|
|
51
|
-
resolve({ exitCode: code || 0, output });
|
|
96
|
+
resolve({ exitCode: code || 0, output: stdout + stderr, stdout, stderr });
|
|
52
97
|
});
|
|
53
98
|
child.on('error', (err) => {
|
|
54
99
|
if (!hasOutput) {
|
|
@@ -56,7 +101,7 @@ function executeCommand(command) {
|
|
|
56
101
|
}
|
|
57
102
|
console2.error(err.message);
|
|
58
103
|
console2.printSeparator('', boxWidth);
|
|
59
|
-
resolve({ exitCode: 1, output: err.message });
|
|
104
|
+
resolve({ exitCode: 1, output: err.message, stdout: '', stderr: err.message });
|
|
60
105
|
});
|
|
61
106
|
});
|
|
62
107
|
}
|
|
@@ -65,7 +110,8 @@ program
|
|
|
65
110
|
.name('pls')
|
|
66
111
|
.description('AI 驱动的命令行工具,将自然语言转换为可执行的 Shell 命令')
|
|
67
112
|
.version(packageJson.version, '-v, --version', '显示版本号')
|
|
68
|
-
.helpOption('-h, --help', '显示帮助信息')
|
|
113
|
+
.helpOption('-h, --help', '显示帮助信息')
|
|
114
|
+
.allowUnknownOption(true); // 允许未知选项(用于别名参数传递)
|
|
69
115
|
// config 子命令
|
|
70
116
|
const configCmd = program.command('config').description('管理配置');
|
|
71
117
|
configCmd
|
|
@@ -78,15 +124,16 @@ configCmd
|
|
|
78
124
|
console.log('');
|
|
79
125
|
console2.title('当前配置:');
|
|
80
126
|
console2.muted('━'.repeat(50));
|
|
81
|
-
console.log(` ${chalk.hex(
|
|
82
|
-
console.log(` ${chalk.hex(
|
|
83
|
-
console.log(` ${chalk.hex(
|
|
84
|
-
console.log(` ${chalk.hex(
|
|
85
|
-
console.log(` ${chalk.hex(
|
|
86
|
-
console.log(` ${chalk.hex(
|
|
87
|
-
console.log(` ${chalk.hex(
|
|
88
|
-
console.log(` ${chalk.hex(
|
|
89
|
-
console.log(` ${chalk.hex(
|
|
127
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('apiKey')}: ${maskApiKey(config.apiKey)}`);
|
|
128
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('baseUrl')}: ${config.baseUrl}`);
|
|
129
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('provider')}: ${config.provider}`);
|
|
130
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('model')}: ${config.model}`);
|
|
131
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('shellHook')}: ${config.shellHook ? chalk.hex(getThemeColors().success)('已启用') : chalk.gray('未启用')}`);
|
|
132
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('editMode')}: ${config.editMode === 'auto' ? chalk.hex(getThemeColors().primary)('auto (自动编辑)') : chalk.gray('manual (按E编辑)')}`);
|
|
133
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('chatHistoryLimit')}: ${config.chatHistoryLimit} 轮`);
|
|
134
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('commandHistoryLimit')}: ${config.commandHistoryLimit} 条`);
|
|
135
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('shellHistoryLimit')}: ${config.shellHistoryLimit} 条`);
|
|
136
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('theme')}: ${config.theme === 'dark' ? chalk.hex(getThemeColors().primary)('dark (深色)') : chalk.hex(getThemeColors().primary)('light (浅色)')}`);
|
|
90
137
|
console2.muted('━'.repeat(50));
|
|
91
138
|
console2.muted(`配置文件: ${CONFIG_FILE}`);
|
|
92
139
|
console.log('');
|
|
@@ -113,6 +160,67 @@ configCmd.action(async () => {
|
|
|
113
160
|
const { runConfigWizard } = await import('../src/config.js');
|
|
114
161
|
await runConfigWizard();
|
|
115
162
|
});
|
|
163
|
+
// theme 子命令
|
|
164
|
+
const themeCmd = program.command('theme').description('管理主题');
|
|
165
|
+
themeCmd
|
|
166
|
+
.command('list')
|
|
167
|
+
.description('查看所有可用主题')
|
|
168
|
+
.action(async () => {
|
|
169
|
+
const { themes } = await import('../src/ui/theme.js');
|
|
170
|
+
const config = getConfig();
|
|
171
|
+
const currentTheme = config.theme || 'dark';
|
|
172
|
+
console.log('');
|
|
173
|
+
console2.title('🎨 可用主题:');
|
|
174
|
+
console2.muted('━'.repeat(50));
|
|
175
|
+
Object.keys(themes).forEach((themeName) => {
|
|
176
|
+
const isCurrent = themeName === currentTheme;
|
|
177
|
+
const prefix = isCurrent ? '●' : '○';
|
|
178
|
+
const label = themeName === 'dark' ? 'dark (深色)' : 'light (浅色)';
|
|
179
|
+
const color = themeName === 'dark' ? '#00D9FF' : '#0284C7';
|
|
180
|
+
if (isCurrent) {
|
|
181
|
+
console.log(` ${chalk.hex(color)(prefix)} ${chalk.hex(color).bold(label)} ${chalk.gray('(当前)')}`);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
console.log(` ${chalk.gray(prefix)} ${label}`);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
console2.muted('━'.repeat(50));
|
|
188
|
+
console.log('');
|
|
189
|
+
});
|
|
190
|
+
themeCmd
|
|
191
|
+
.argument('[name]', '主题名称 (dark, light)')
|
|
192
|
+
.description('切换主题')
|
|
193
|
+
.action((name) => {
|
|
194
|
+
if (!name) {
|
|
195
|
+
// 显示当前主题
|
|
196
|
+
const config = getConfig();
|
|
197
|
+
const currentTheme = config.theme || 'dark';
|
|
198
|
+
const label = currentTheme === 'dark' ? 'dark (深色)' : 'light (浅色)';
|
|
199
|
+
const color = currentTheme === 'dark' ? '#00D9FF' : '#0284C7';
|
|
200
|
+
console.log('');
|
|
201
|
+
console.log(`当前主题: ${chalk.hex(color).bold(label)}`);
|
|
202
|
+
console.log('');
|
|
203
|
+
console2.muted('使用 pls theme list 查看所有主题');
|
|
204
|
+
console2.muted('使用 pls theme <name> 切换主题');
|
|
205
|
+
console.log('');
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
// 切换主题
|
|
209
|
+
try {
|
|
210
|
+
setConfigValue('theme', name);
|
|
211
|
+
const label = name === 'dark' ? 'dark (深色)' : 'light (浅色)';
|
|
212
|
+
const color = name === 'dark' ? '#00D9FF' : '#0284C7';
|
|
213
|
+
console.log('');
|
|
214
|
+
console2.success(`已切换到 ${chalk.hex(color).bold(label)} 主题`);
|
|
215
|
+
console.log('');
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
console.log('');
|
|
219
|
+
console2.error(error.message);
|
|
220
|
+
console.log('');
|
|
221
|
+
process.exit(1);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
116
224
|
// history 子命令
|
|
117
225
|
const historyCmd = program.command('history').description('查看或管理命令历史');
|
|
118
226
|
historyCmd
|
|
@@ -132,14 +240,14 @@ historyCmd
|
|
|
132
240
|
history.forEach((item, index) => {
|
|
133
241
|
const status = item.executed
|
|
134
242
|
? item.exitCode === 0
|
|
135
|
-
? chalk.hex(
|
|
136
|
-
: chalk.hex(
|
|
243
|
+
? chalk.hex(getThemeColors().success)('✓')
|
|
244
|
+
: chalk.hex(getThemeColors().error)(`✗ 退出码:${item.exitCode}`)
|
|
137
245
|
: chalk.gray('(未执行)');
|
|
138
|
-
console.log(`\n${chalk.gray(`${index + 1}.`)} ${chalk.hex(
|
|
246
|
+
console.log(`\n${chalk.gray(`${index + 1}.`)} ${chalk.hex(getThemeColors().primary)(item.userPrompt)}`);
|
|
139
247
|
// 显示用户修改信息
|
|
140
248
|
if (item.userModified && item.aiGeneratedCommand) {
|
|
141
249
|
console.log(` ${chalk.dim('AI 生成:')} ${chalk.gray(item.aiGeneratedCommand)}`);
|
|
142
|
-
console.log(` ${chalk.dim('用户修改为:')} ${item.command} ${status} ${chalk.
|
|
250
|
+
console.log(` ${chalk.dim('用户修改为:')} ${item.command} ${status} ${chalk.hex(getThemeColors().warning)('(已修改)')}`);
|
|
143
251
|
}
|
|
144
252
|
else {
|
|
145
253
|
console.log(` ${chalk.dim('→')} ${item.command} ${status}`);
|
|
@@ -199,14 +307,14 @@ historyCmd.action(() => {
|
|
|
199
307
|
history.forEach((item, index) => {
|
|
200
308
|
const status = item.executed
|
|
201
309
|
? item.exitCode === 0
|
|
202
|
-
? chalk.hex(
|
|
203
|
-
: chalk.hex(
|
|
310
|
+
? chalk.hex(getThemeColors().success)('✓')
|
|
311
|
+
: chalk.hex(getThemeColors().error)(`✗ 退出码:${item.exitCode}`)
|
|
204
312
|
: chalk.gray('(未执行)');
|
|
205
|
-
console.log(`\n${chalk.gray(`${index + 1}.`)} ${chalk.hex(
|
|
313
|
+
console.log(`\n${chalk.gray(`${index + 1}.`)} ${chalk.hex(getThemeColors().primary)(item.userPrompt)}`);
|
|
206
314
|
// 显示用户修改信息
|
|
207
315
|
if (item.userModified && item.aiGeneratedCommand) {
|
|
208
316
|
console.log(` ${chalk.dim('AI 生成:')} ${chalk.gray(item.aiGeneratedCommand)}`);
|
|
209
|
-
console.log(` ${chalk.dim('用户修改为:')} ${item.command} ${status} ${chalk.
|
|
317
|
+
console.log(` ${chalk.dim('用户修改为:')} ${item.command} ${status} ${chalk.hex(getThemeColors().warning)('(已修改)')}`);
|
|
210
318
|
}
|
|
211
319
|
else {
|
|
212
320
|
console.log(` ${chalk.dim('→')} ${item.command} ${status}`);
|
|
@@ -265,11 +373,11 @@ hookCmd
|
|
|
265
373
|
console.log('');
|
|
266
374
|
console2.title('📊 Shell Hook 状态');
|
|
267
375
|
console2.muted('━'.repeat(40));
|
|
268
|
-
console.log(` ${chalk.hex(
|
|
269
|
-
console.log(` ${chalk.hex(
|
|
270
|
-
console.log(` ${chalk.hex(
|
|
271
|
-
console.log(` ${chalk.hex(
|
|
272
|
-
console.log(` ${chalk.hex(
|
|
376
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('Shell 类型')}: ${status.shellType}`);
|
|
377
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('配置文件')}: ${status.configPath || '未知'}`);
|
|
378
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('已安装')}: ${status.installed ? chalk.hex(getThemeColors().success)('是') : chalk.gray('否')}`);
|
|
379
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('已启用')}: ${status.enabled ? chalk.hex(getThemeColors().success)('是') : chalk.gray('否')}`);
|
|
380
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('历史文件')}: ${status.historyFile}`);
|
|
273
381
|
console2.muted('━'.repeat(40));
|
|
274
382
|
if (!status.installed) {
|
|
275
383
|
console.log('');
|
|
@@ -283,11 +391,11 @@ hookCmd.action(() => {
|
|
|
283
391
|
console.log('');
|
|
284
392
|
console2.title('📊 Shell Hook 状态');
|
|
285
393
|
console2.muted('━'.repeat(40));
|
|
286
|
-
console.log(` ${chalk.hex(
|
|
287
|
-
console.log(` ${chalk.hex(
|
|
288
|
-
console.log(` ${chalk.hex(
|
|
289
|
-
console.log(` ${chalk.hex(
|
|
290
|
-
console.log(` ${chalk.hex(
|
|
394
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('Shell 类型')}: ${status.shellType}`);
|
|
395
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('配置文件')}: ${status.configPath || '未知'}`);
|
|
396
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('已安装')}: ${status.installed ? chalk.hex(getThemeColors().success)('是') : chalk.gray('否')}`);
|
|
397
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('已启用')}: ${status.enabled ? chalk.hex(getThemeColors().success)('是') : chalk.gray('否')}`);
|
|
398
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('历史文件')}: ${status.historyFile}`);
|
|
291
399
|
console2.muted('━'.repeat(40));
|
|
292
400
|
if (!status.installed) {
|
|
293
401
|
console.log('');
|
|
@@ -295,6 +403,390 @@ hookCmd.action(() => {
|
|
|
295
403
|
}
|
|
296
404
|
console.log('');
|
|
297
405
|
});
|
|
406
|
+
// upgrade 子命令
|
|
407
|
+
program
|
|
408
|
+
.command('upgrade')
|
|
409
|
+
.description('升级到最新版本')
|
|
410
|
+
.action(async () => {
|
|
411
|
+
const success = await performUpgrade(packageJson.version);
|
|
412
|
+
process.exit(success ? 0 : 1);
|
|
413
|
+
});
|
|
414
|
+
// alias 子命令
|
|
415
|
+
const aliasCmd = program.command('alias').description('管理命令别名');
|
|
416
|
+
// 获取所有子命令名称(用于检测冲突)
|
|
417
|
+
function getReservedCommands() {
|
|
418
|
+
return program.commands.map((cmd) => cmd.name());
|
|
419
|
+
}
|
|
420
|
+
aliasCmd
|
|
421
|
+
.command('list')
|
|
422
|
+
.description('列出所有别名')
|
|
423
|
+
.action(() => {
|
|
424
|
+
displayAliases();
|
|
425
|
+
});
|
|
426
|
+
aliasCmd
|
|
427
|
+
.command('add <name> <prompt>')
|
|
428
|
+
.description('添加别名(prompt 支持 {{param}} 或 {{param:default}} 参数模板)')
|
|
429
|
+
.option('-d, --description <desc>', '别名描述')
|
|
430
|
+
.action((name, prompt, options) => {
|
|
431
|
+
try {
|
|
432
|
+
addAlias(name, prompt, options.description, getReservedCommands());
|
|
433
|
+
console.log('');
|
|
434
|
+
console2.success(`已添加别名: ${name}`);
|
|
435
|
+
console.log(` ${chalk.gray('→')} ${prompt}`);
|
|
436
|
+
console.log('');
|
|
437
|
+
}
|
|
438
|
+
catch (error) {
|
|
439
|
+
console.log('');
|
|
440
|
+
console2.error(error.message);
|
|
441
|
+
console.log('');
|
|
442
|
+
process.exit(1);
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
aliasCmd
|
|
446
|
+
.command('remove <name>')
|
|
447
|
+
.description('删除别名')
|
|
448
|
+
.action((name) => {
|
|
449
|
+
const removed = removeAlias(name);
|
|
450
|
+
console.log('');
|
|
451
|
+
if (removed) {
|
|
452
|
+
console2.success(`已删除别名: ${name}`);
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
console2.error(`别名不存在: ${name}`);
|
|
456
|
+
}
|
|
457
|
+
console.log('');
|
|
458
|
+
});
|
|
459
|
+
// 默认 alias 命令(显示列表)
|
|
460
|
+
aliasCmd.action(() => {
|
|
461
|
+
displayAliases();
|
|
462
|
+
});
|
|
463
|
+
// remote 子命令
|
|
464
|
+
const remoteCmd = program.command('remote').description('管理远程服务器');
|
|
465
|
+
remoteCmd
|
|
466
|
+
.command('list')
|
|
467
|
+
.description('列出所有远程服务器')
|
|
468
|
+
.action(() => {
|
|
469
|
+
displayRemotes();
|
|
470
|
+
});
|
|
471
|
+
remoteCmd
|
|
472
|
+
.command('add <name> <host>')
|
|
473
|
+
.description('添加远程服务器(格式: user@host 或 user@host:port)')
|
|
474
|
+
.option('-k, --key <path>', 'SSH 私钥路径')
|
|
475
|
+
.option('-p, --password', '使用密码认证(每次执行时输入)')
|
|
476
|
+
.action((name, host, options) => {
|
|
477
|
+
try {
|
|
478
|
+
addRemote(name, host, { key: options.key, password: options.password });
|
|
479
|
+
console.log('');
|
|
480
|
+
console2.success(`已添加远程服务器: ${name}`);
|
|
481
|
+
console.log(` ${chalk.gray('→')} ${host}`);
|
|
482
|
+
if (options.key) {
|
|
483
|
+
console.log(` ${chalk.gray('密钥:')} ${options.key}`);
|
|
484
|
+
}
|
|
485
|
+
if (options.password) {
|
|
486
|
+
console.log(` ${chalk.gray('认证:')} 密码(每次执行时输入)`);
|
|
487
|
+
}
|
|
488
|
+
console.log('');
|
|
489
|
+
}
|
|
490
|
+
catch (error) {
|
|
491
|
+
console.log('');
|
|
492
|
+
console2.error(error.message);
|
|
493
|
+
console.log('');
|
|
494
|
+
process.exit(1);
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
remoteCmd
|
|
498
|
+
.command('remove <name>')
|
|
499
|
+
.description('删除远程服务器')
|
|
500
|
+
.action((name) => {
|
|
501
|
+
const removed = removeRemote(name);
|
|
502
|
+
console.log('');
|
|
503
|
+
if (removed) {
|
|
504
|
+
console2.success(`已删除远程服务器: ${name}`);
|
|
505
|
+
}
|
|
506
|
+
else {
|
|
507
|
+
console2.error(`远程服务器不存在: ${name}`);
|
|
508
|
+
}
|
|
509
|
+
console.log('');
|
|
510
|
+
});
|
|
511
|
+
remoteCmd
|
|
512
|
+
.command('test <name>')
|
|
513
|
+
.description('测试远程服务器连接')
|
|
514
|
+
.action(async (name) => {
|
|
515
|
+
const remote = getRemote(name);
|
|
516
|
+
if (!remote) {
|
|
517
|
+
console.log('');
|
|
518
|
+
console2.error(`远程服务器不存在: ${name}`);
|
|
519
|
+
console.log('');
|
|
520
|
+
process.exit(1);
|
|
521
|
+
}
|
|
522
|
+
console.log('');
|
|
523
|
+
console2.info(`正在测试连接 ${name} (${remote.user}@${remote.host}:${remote.port})...`);
|
|
524
|
+
const result = await testRemoteConnection(name);
|
|
525
|
+
console.log(` ${result.message}`);
|
|
526
|
+
if (result.success) {
|
|
527
|
+
// 采集系统信息
|
|
528
|
+
console2.info('正在采集系统信息...');
|
|
529
|
+
try {
|
|
530
|
+
const sysInfo = await collectRemoteSysInfo(name, true);
|
|
531
|
+
console.log(` ${chalk.gray('系统:')} ${sysInfo.os} ${sysInfo.osVersion}`);
|
|
532
|
+
console.log(` ${chalk.gray('Shell:')} ${sysInfo.shell}`);
|
|
533
|
+
console.log(` ${chalk.gray('主机名:')} ${sysInfo.hostname}`);
|
|
534
|
+
}
|
|
535
|
+
catch (error) {
|
|
536
|
+
console2.warning(`无法采集系统信息: ${error.message}`);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
console.log('');
|
|
540
|
+
});
|
|
541
|
+
// remote hook 子命令
|
|
542
|
+
const remoteHookCmd = remoteCmd.command('hook').description('管理远程服务器 Shell Hook');
|
|
543
|
+
remoteHookCmd
|
|
544
|
+
.command('install <name>')
|
|
545
|
+
.description('在远程服务器安装 Shell Hook')
|
|
546
|
+
.action(async (name) => {
|
|
547
|
+
const remote = getRemote(name);
|
|
548
|
+
if (!remote) {
|
|
549
|
+
console.log('');
|
|
550
|
+
console2.error(`远程服务器不存在: ${name}`);
|
|
551
|
+
console.log('');
|
|
552
|
+
process.exit(1);
|
|
553
|
+
}
|
|
554
|
+
console.log('');
|
|
555
|
+
console2.title('🔧 远程 Shell Hook 安装');
|
|
556
|
+
console2.muted('━'.repeat(40));
|
|
557
|
+
console2.info(`目标服务器: ${name} (${remote.user}@${remote.host})`);
|
|
558
|
+
try {
|
|
559
|
+
// 检测远程 shell 类型
|
|
560
|
+
const sshExecFn = async (cmd) => {
|
|
561
|
+
const result = await sshExec(name, cmd, { timeout: 30000 });
|
|
562
|
+
return { stdout: result.stdout, exitCode: result.exitCode };
|
|
563
|
+
};
|
|
564
|
+
const shellType = await detectRemoteShell(sshExecFn);
|
|
565
|
+
const configPath = getRemoteShellConfigPath(shellType);
|
|
566
|
+
console2.muted(`检测到 Shell: ${shellType}`);
|
|
567
|
+
console2.muted(`配置文件: ${configPath}`);
|
|
568
|
+
console.log('');
|
|
569
|
+
const result = await installRemoteShellHook(sshExecFn, shellType);
|
|
570
|
+
console.log(` ${result.message}`);
|
|
571
|
+
if (result.success) {
|
|
572
|
+
console.log('');
|
|
573
|
+
console2.warning('⚠️ 请在远程服务器重启终端或执行:');
|
|
574
|
+
console2.info(` source ${configPath}`);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
catch (error) {
|
|
578
|
+
console2.error(`安装失败: ${error.message}`);
|
|
579
|
+
}
|
|
580
|
+
console.log('');
|
|
581
|
+
});
|
|
582
|
+
remoteHookCmd
|
|
583
|
+
.command('uninstall <name>')
|
|
584
|
+
.description('从远程服务器卸载 Shell Hook')
|
|
585
|
+
.action(async (name) => {
|
|
586
|
+
const remote = getRemote(name);
|
|
587
|
+
if (!remote) {
|
|
588
|
+
console.log('');
|
|
589
|
+
console2.error(`远程服务器不存在: ${name}`);
|
|
590
|
+
console.log('');
|
|
591
|
+
process.exit(1);
|
|
592
|
+
}
|
|
593
|
+
console.log('');
|
|
594
|
+
console2.info(`正在从 ${name} 卸载 Shell Hook...`);
|
|
595
|
+
try {
|
|
596
|
+
const sshExecFn = async (cmd) => {
|
|
597
|
+
const result = await sshExec(name, cmd, { timeout: 30000 });
|
|
598
|
+
return { stdout: result.stdout, exitCode: result.exitCode };
|
|
599
|
+
};
|
|
600
|
+
const shellType = await detectRemoteShell(sshExecFn);
|
|
601
|
+
const result = await uninstallRemoteShellHook(sshExecFn, shellType);
|
|
602
|
+
console.log(` ${result.message}`);
|
|
603
|
+
if (result.success) {
|
|
604
|
+
console.log('');
|
|
605
|
+
console2.warning('⚠️ 请在远程服务器重启终端使其生效');
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
catch (error) {
|
|
609
|
+
console2.error(`卸载失败: ${error.message}`);
|
|
610
|
+
}
|
|
611
|
+
console.log('');
|
|
612
|
+
});
|
|
613
|
+
remoteHookCmd
|
|
614
|
+
.command('status <name>')
|
|
615
|
+
.description('查看远程服务器 Shell Hook 状态')
|
|
616
|
+
.action(async (name) => {
|
|
617
|
+
const remote = getRemote(name);
|
|
618
|
+
if (!remote) {
|
|
619
|
+
console.log('');
|
|
620
|
+
console2.error(`远程服务器不存在: ${name}`);
|
|
621
|
+
console.log('');
|
|
622
|
+
process.exit(1);
|
|
623
|
+
}
|
|
624
|
+
console.log('');
|
|
625
|
+
console2.info(`正在检查 ${name} 的 Hook 状态...`);
|
|
626
|
+
try {
|
|
627
|
+
const sshExecFn = async (cmd) => {
|
|
628
|
+
const result = await sshExec(name, cmd, { timeout: 30000 });
|
|
629
|
+
return { stdout: result.stdout, exitCode: result.exitCode };
|
|
630
|
+
};
|
|
631
|
+
const status = await getRemoteHookStatus(sshExecFn);
|
|
632
|
+
console.log('');
|
|
633
|
+
console2.title(`📊 远程 Shell Hook 状态 - ${name}`);
|
|
634
|
+
console2.muted('━'.repeat(40));
|
|
635
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('Shell 类型')}: ${status.shellType}`);
|
|
636
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('配置文件')}: ${status.configPath}`);
|
|
637
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('已安装')}: ${status.installed ? chalk.hex(getThemeColors().success)('是') : chalk.gray('否')}`);
|
|
638
|
+
console2.muted('━'.repeat(40));
|
|
639
|
+
if (!status.installed) {
|
|
640
|
+
console.log('');
|
|
641
|
+
console2.muted(`提示: 运行 pls remote hook install ${name} 安装 Shell Hook`);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
catch (error) {
|
|
645
|
+
console2.error(`检查失败: ${error.message}`);
|
|
646
|
+
}
|
|
647
|
+
console.log('');
|
|
648
|
+
});
|
|
649
|
+
// remote history 子命令
|
|
650
|
+
const remoteHistoryCmd = remoteCmd.command('history').description('管理远程服务器历史记录');
|
|
651
|
+
remoteHistoryCmd
|
|
652
|
+
.command('show <name>')
|
|
653
|
+
.description('显示远程服务器命令历史')
|
|
654
|
+
.action((name) => {
|
|
655
|
+
displayRemoteHistory(name);
|
|
656
|
+
});
|
|
657
|
+
remoteHistoryCmd
|
|
658
|
+
.command('clear <name>')
|
|
659
|
+
.description('清空远程服务器命令历史')
|
|
660
|
+
.action((name) => {
|
|
661
|
+
clearRemoteHistory(name);
|
|
662
|
+
console.log('');
|
|
663
|
+
console2.success(`已清空服务器 "${name}" 的命令历史`);
|
|
664
|
+
console.log('');
|
|
665
|
+
});
|
|
666
|
+
remoteHistoryCmd
|
|
667
|
+
.command('shell <name>')
|
|
668
|
+
.description('显示远程服务器 Shell 历史')
|
|
669
|
+
.action(async (name) => {
|
|
670
|
+
await displayRemoteShellHistory(name);
|
|
671
|
+
});
|
|
672
|
+
remoteHistoryCmd
|
|
673
|
+
.command('shell-clear <name>')
|
|
674
|
+
.description('清空远程服务器 Shell 历史')
|
|
675
|
+
.action(async (name) => {
|
|
676
|
+
await clearRemoteShellHistory(name);
|
|
677
|
+
});
|
|
678
|
+
// remote default 子命令
|
|
679
|
+
remoteCmd
|
|
680
|
+
.command('default [name]')
|
|
681
|
+
.description('设置或查看默认远程服务器')
|
|
682
|
+
.option('-c, --clear', '清除默认服务器设置')
|
|
683
|
+
.action((name, options) => {
|
|
684
|
+
const config = getConfig();
|
|
685
|
+
// 清除默认
|
|
686
|
+
if (options?.clear) {
|
|
687
|
+
if (config.defaultRemote) {
|
|
688
|
+
setConfigValue('defaultRemote', '');
|
|
689
|
+
console.log('');
|
|
690
|
+
console2.success('已清除默认远程服务器');
|
|
691
|
+
console.log('');
|
|
692
|
+
}
|
|
693
|
+
else {
|
|
694
|
+
console.log('');
|
|
695
|
+
console2.muted('当前没有设置默认远程服务器');
|
|
696
|
+
console.log('');
|
|
697
|
+
}
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
// 查看默认
|
|
701
|
+
if (!name) {
|
|
702
|
+
console.log('');
|
|
703
|
+
if (config.defaultRemote) {
|
|
704
|
+
const remote = getRemote(config.defaultRemote);
|
|
705
|
+
if (remote) {
|
|
706
|
+
console.log(`默认远程服务器: ${chalk.hex(getThemeColors().primary)(config.defaultRemote)}`);
|
|
707
|
+
console.log(` ${chalk.gray('→')} ${remote.user}@${remote.host}:${remote.port}`);
|
|
708
|
+
}
|
|
709
|
+
else {
|
|
710
|
+
console2.warning(`默认服务器 "${config.defaultRemote}" 不存在,建议清除设置`);
|
|
711
|
+
console2.muted('运行 pls remote default --clear 清除');
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
else {
|
|
715
|
+
console2.muted('当前没有设置默认远程服务器');
|
|
716
|
+
console2.muted('使用 pls remote default <name> 设置默认服务器');
|
|
717
|
+
}
|
|
718
|
+
console.log('');
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
// 设置默认
|
|
722
|
+
const remote = getRemote(name);
|
|
723
|
+
if (!remote) {
|
|
724
|
+
console.log('');
|
|
725
|
+
console2.error(`远程服务器不存在: ${name}`);
|
|
726
|
+
console2.muted('使用 pls remote list 查看所有服务器');
|
|
727
|
+
console.log('');
|
|
728
|
+
process.exit(1);
|
|
729
|
+
}
|
|
730
|
+
setConfigValue('defaultRemote', name);
|
|
731
|
+
console.log('');
|
|
732
|
+
console2.success(`已设置默认远程服务器: ${name}`);
|
|
733
|
+
console.log(` ${chalk.gray('→')} ${remote.user}@${remote.host}:${remote.port}`);
|
|
734
|
+
console2.muted('现在可以使用 pls -r <prompt> 直接在该服务器执行');
|
|
735
|
+
console.log('');
|
|
736
|
+
});
|
|
737
|
+
// remote workdir 子命令
|
|
738
|
+
remoteCmd
|
|
739
|
+
.command('workdir <name> [path]')
|
|
740
|
+
.description('设置或查看远程服务器的工作目录')
|
|
741
|
+
.option('-c, --clear', '清除工作目录设置')
|
|
742
|
+
.action((name, workdirPath, options) => {
|
|
743
|
+
const remote = getRemote(name);
|
|
744
|
+
if (!remote) {
|
|
745
|
+
console.log('');
|
|
746
|
+
console2.error(`远程服务器不存在: ${name}`);
|
|
747
|
+
console.log('');
|
|
748
|
+
process.exit(1);
|
|
749
|
+
}
|
|
750
|
+
// 清除工作目录
|
|
751
|
+
if (options?.clear) {
|
|
752
|
+
if (remote.workDir) {
|
|
753
|
+
setRemoteWorkDir(name, '-');
|
|
754
|
+
console.log('');
|
|
755
|
+
console2.success(`已清除 ${name} 的工作目录设置`);
|
|
756
|
+
console.log('');
|
|
757
|
+
}
|
|
758
|
+
else {
|
|
759
|
+
console.log('');
|
|
760
|
+
console2.muted(`${name} 没有设置工作目录`);
|
|
761
|
+
console.log('');
|
|
762
|
+
}
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
// 查看工作目录
|
|
766
|
+
if (!workdirPath) {
|
|
767
|
+
console.log('');
|
|
768
|
+
if (remote.workDir) {
|
|
769
|
+
console.log(`${chalk.hex(getThemeColors().primary)(name)} 的工作目录:`);
|
|
770
|
+
console.log(` ${chalk.gray('→')} ${remote.workDir}`);
|
|
771
|
+
}
|
|
772
|
+
else {
|
|
773
|
+
console2.muted(`${name} 没有设置工作目录`);
|
|
774
|
+
console2.muted(`使用 pls remote workdir ${name} <path> 设置工作目录`);
|
|
775
|
+
}
|
|
776
|
+
console.log('');
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
// 设置工作目录
|
|
780
|
+
setRemoteWorkDir(name, workdirPath);
|
|
781
|
+
console.log('');
|
|
782
|
+
console2.success(`已设置 ${name} 的工作目录: ${workdirPath}`);
|
|
783
|
+
console2.muted('现在在该服务器执行的命令会自动切换到此目录');
|
|
784
|
+
console.log('');
|
|
785
|
+
});
|
|
786
|
+
// 默认 remote 命令(显示列表)
|
|
787
|
+
remoteCmd.action(() => {
|
|
788
|
+
displayRemotes();
|
|
789
|
+
});
|
|
298
790
|
// chat 子命令
|
|
299
791
|
const chatCmd = program.command('chat').description('AI 对话模式,问答、讲解命令');
|
|
300
792
|
chatCmd
|
|
@@ -319,8 +811,8 @@ chatCmd
|
|
|
319
811
|
console.log('');
|
|
320
812
|
console2.title('💬 AI 对话模式');
|
|
321
813
|
console2.muted('━'.repeat(40));
|
|
322
|
-
console.log(` ${chalk.hex(
|
|
323
|
-
console.log(` ${chalk.hex(
|
|
814
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('当前对话轮数')}: ${roundCount}`);
|
|
815
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('历史文件')}: ${historyFile}`);
|
|
324
816
|
console2.muted('━'.repeat(40));
|
|
325
817
|
console.log('');
|
|
326
818
|
console2.muted('用法:');
|
|
@@ -337,19 +829,37 @@ chatCmd
|
|
|
337
829
|
console.log('');
|
|
338
830
|
process.exit(1);
|
|
339
831
|
}
|
|
340
|
-
//
|
|
341
|
-
|
|
832
|
+
// 懒加载 Chat 组件(避免启动时加载 React/Ink)
|
|
833
|
+
;
|
|
834
|
+
(async () => {
|
|
835
|
+
const React = await import('react');
|
|
836
|
+
const { render } = await import('ink');
|
|
837
|
+
const { Chat } = await import('../src/components/Chat.js');
|
|
838
|
+
render(React.createElement(Chat, {
|
|
839
|
+
prompt,
|
|
840
|
+
debug: options.debug,
|
|
841
|
+
showRoundCount: true,
|
|
842
|
+
onComplete: () => process.exit(0),
|
|
843
|
+
}));
|
|
844
|
+
})();
|
|
342
845
|
});
|
|
343
846
|
// 默认命令(执行 prompt)
|
|
344
847
|
program
|
|
345
848
|
.argument('[prompt...]', '自然语言描述你想执行的操作')
|
|
346
849
|
.option('-d, --debug', '显示调试信息(系统信息、完整 prompt 等)')
|
|
850
|
+
.option('-r, --remote [name]', '在远程服务器上执行(不指定则使用默认服务器)')
|
|
347
851
|
.action((promptArgs, options) => {
|
|
852
|
+
// 智能处理 -r 参数:如果 -r 后面的值不是已注册的服务器名,把它当作 prompt 的一部分
|
|
853
|
+
if (typeof options.remote === 'string' && !getRemote(options.remote)) {
|
|
854
|
+
// "查看当前目录" 不是服务器名,放回 prompt
|
|
855
|
+
promptArgs.unshift(options.remote);
|
|
856
|
+
options.remote = true; // 改为使用默认服务器
|
|
857
|
+
}
|
|
348
858
|
if (promptArgs.length === 0) {
|
|
349
859
|
program.help();
|
|
350
860
|
return;
|
|
351
861
|
}
|
|
352
|
-
|
|
862
|
+
let prompt = promptArgs.join(' ');
|
|
353
863
|
if (!prompt.trim()) {
|
|
354
864
|
console.log('');
|
|
355
865
|
console2.error('请提供你想执行的操作描述');
|
|
@@ -357,6 +867,23 @@ program
|
|
|
357
867
|
console.log('');
|
|
358
868
|
process.exit(1);
|
|
359
869
|
}
|
|
870
|
+
// 尝试解析别名(支持 pls disk 和 pls @disk 两种格式)
|
|
871
|
+
try {
|
|
872
|
+
const aliasResult = resolveAlias(prompt);
|
|
873
|
+
if (aliasResult.resolved) {
|
|
874
|
+
prompt = aliasResult.prompt;
|
|
875
|
+
if (options.debug) {
|
|
876
|
+
console.log('');
|
|
877
|
+
console2.muted(`别名解析: ${aliasResult.aliasName} → ${prompt}`);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
catch (error) {
|
|
882
|
+
console.log('');
|
|
883
|
+
console2.error(error.message);
|
|
884
|
+
console.log('');
|
|
885
|
+
process.exit(1);
|
|
886
|
+
}
|
|
360
887
|
// 检查配置
|
|
361
888
|
if (!isConfigValid()) {
|
|
362
889
|
console.log('');
|
|
@@ -365,19 +892,194 @@ program
|
|
|
365
892
|
console.log('');
|
|
366
893
|
process.exit(1);
|
|
367
894
|
}
|
|
368
|
-
//
|
|
895
|
+
// 解析远程服务器名称
|
|
896
|
+
// options.remote 可能是:
|
|
897
|
+
// - undefined: 没有使用 -r
|
|
898
|
+
// - true: 使用了 -r 但没有指定名称(使用默认)
|
|
899
|
+
// - string: 使用了 -r 并指定了名称(支持逗号分隔的多个服务器)
|
|
900
|
+
let remoteName;
|
|
901
|
+
let remoteNames; // 批量执行时的服务器列表
|
|
902
|
+
if (options.remote !== undefined) {
|
|
903
|
+
if (options.remote === true) {
|
|
904
|
+
// 使用默认服务器
|
|
905
|
+
const config = getConfig();
|
|
906
|
+
if (!config.defaultRemote) {
|
|
907
|
+
console.log('');
|
|
908
|
+
console2.error('未设置默认远程服务器');
|
|
909
|
+
console2.muted('使用 pls remote default <name> 设置默认服务器');
|
|
910
|
+
console2.muted('或使用 pls -r <name> <prompt> 指定服务器');
|
|
911
|
+
console.log('');
|
|
912
|
+
process.exit(1);
|
|
913
|
+
}
|
|
914
|
+
remoteName = config.defaultRemote;
|
|
915
|
+
}
|
|
916
|
+
else {
|
|
917
|
+
// 检查是否为批量执行(逗号分隔的服务器名)
|
|
918
|
+
if (options.remote.includes(',')) {
|
|
919
|
+
remoteNames = options.remote.split(',').map(s => s.trim()).filter(s => s.length > 0);
|
|
920
|
+
// 验证所有服务器是否存在
|
|
921
|
+
const invalidServers = remoteNames.filter(name => !getRemote(name));
|
|
922
|
+
if (invalidServers.length > 0) {
|
|
923
|
+
console.log('');
|
|
924
|
+
console2.error(`以下服务器不存在: ${invalidServers.join(', ')}`);
|
|
925
|
+
console2.muted('使用 pls remote list 查看所有服务器');
|
|
926
|
+
console2.muted('使用 pls remote add <name> <user@host> 添加服务器');
|
|
927
|
+
console.log('');
|
|
928
|
+
process.exit(1);
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
else {
|
|
932
|
+
remoteName = options.remote;
|
|
933
|
+
// 检查服务器是否存在
|
|
934
|
+
const remote = getRemote(remoteName);
|
|
935
|
+
if (!remote) {
|
|
936
|
+
console.log('');
|
|
937
|
+
console2.error(`远程服务器不存在: ${remoteName}`);
|
|
938
|
+
console2.muted('使用 pls remote add <name> <user@host> 添加服务器');
|
|
939
|
+
console.log('');
|
|
940
|
+
process.exit(1);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
// 懒加载 MultiStepCommandGenerator 组件(避免启动时加载 React/Ink)
|
|
369
946
|
;
|
|
370
947
|
(async () => {
|
|
948
|
+
// 批量远程执行模式
|
|
949
|
+
if (remoteNames && remoteNames.length > 0) {
|
|
950
|
+
console.log('');
|
|
951
|
+
console2.info(`正在为 ${remoteNames.length} 台服务器生成命令...`);
|
|
952
|
+
console.log('');
|
|
953
|
+
try {
|
|
954
|
+
// 1. 并发生成命令
|
|
955
|
+
const commands = await generateBatchRemoteCommands(remoteNames, prompt, { debug: options.debug });
|
|
956
|
+
// 2. 显示生成的命令
|
|
957
|
+
console2.success('✓ 命令生成完成\n');
|
|
958
|
+
const theme = getCurrentTheme();
|
|
959
|
+
commands.forEach(({ server, command, sysInfo }) => {
|
|
960
|
+
console.log(chalk.hex(theme.primary)(`${server}`) + chalk.gray(` (${sysInfo.os}):`));
|
|
961
|
+
console.log(chalk.hex(theme.secondary)(` ${command}`));
|
|
962
|
+
});
|
|
963
|
+
console.log('');
|
|
964
|
+
// 3. 询问用户确认
|
|
965
|
+
const readline = await import('readline');
|
|
966
|
+
const rl = readline.createInterface({
|
|
967
|
+
input: process.stdin,
|
|
968
|
+
output: process.stdout,
|
|
969
|
+
});
|
|
970
|
+
const confirmed = await new Promise((resolve) => {
|
|
971
|
+
console.log(chalk.gray(`将在 ${remoteNames.length} 台服务器执行以上命令`));
|
|
972
|
+
rl.question(chalk.gray('执行? [回车执行 / Ctrl+C 取消] '), (answer) => {
|
|
973
|
+
rl.close();
|
|
974
|
+
resolve(true);
|
|
975
|
+
});
|
|
976
|
+
});
|
|
977
|
+
if (!confirmed) {
|
|
978
|
+
console.log('');
|
|
979
|
+
console2.muted('已取消执行');
|
|
980
|
+
console.log('');
|
|
981
|
+
process.exit(0);
|
|
982
|
+
}
|
|
983
|
+
// 4. 并发执行
|
|
984
|
+
console.log('');
|
|
985
|
+
console2.info('正在执行...');
|
|
986
|
+
const results = await executeBatchRemoteCommands(commands);
|
|
987
|
+
// 5. 显示执行结果摘要
|
|
988
|
+
console.log('');
|
|
989
|
+
console2.info('执行完成:\n');
|
|
990
|
+
results.forEach(({ server, exitCode }) => {
|
|
991
|
+
const icon = exitCode === 0 ? '✓' : '✗';
|
|
992
|
+
const color = exitCode === 0 ? theme.success : theme.error;
|
|
993
|
+
console.log(` ${chalk.hex(color)(icon)} ${server} ${chalk.gray(`(退出码: ${exitCode})`)}`);
|
|
994
|
+
});
|
|
995
|
+
// 6. 显示每个服务器的详细输出
|
|
996
|
+
console.log('');
|
|
997
|
+
results.forEach(({ server, output }) => {
|
|
998
|
+
console.log(chalk.hex(theme.primary)(`─── ${server} ───`));
|
|
999
|
+
console.log(output || chalk.gray('(无输出)'));
|
|
1000
|
+
});
|
|
1001
|
+
// 7. 记录到历史
|
|
1002
|
+
results.forEach(({ server, command, exitCode, output }) => {
|
|
1003
|
+
addRemoteHistory(server, {
|
|
1004
|
+
userPrompt: prompt,
|
|
1005
|
+
command,
|
|
1006
|
+
aiGeneratedCommand: command, // 批量执行无编辑功能
|
|
1007
|
+
userModified: false,
|
|
1008
|
+
executed: true,
|
|
1009
|
+
exitCode,
|
|
1010
|
+
output,
|
|
1011
|
+
});
|
|
1012
|
+
});
|
|
1013
|
+
// 8. 根据结果决定退出码
|
|
1014
|
+
const allSuccess = results.every(r => r.exitCode === 0);
|
|
1015
|
+
const allFailed = results.every(r => r.exitCode !== 0);
|
|
1016
|
+
if (allFailed) {
|
|
1017
|
+
process.exit(2); // 全部失败
|
|
1018
|
+
}
|
|
1019
|
+
else if (!allSuccess) {
|
|
1020
|
+
process.exit(1); // 部分失败
|
|
1021
|
+
}
|
|
1022
|
+
process.exit(0); // 全部成功
|
|
1023
|
+
}
|
|
1024
|
+
catch (error) {
|
|
1025
|
+
console.log('');
|
|
1026
|
+
console2.error(`批量执行失败: ${error.message}`);
|
|
1027
|
+
console.log('');
|
|
1028
|
+
process.exit(1);
|
|
1029
|
+
}
|
|
1030
|
+
return;
|
|
1031
|
+
}
|
|
1032
|
+
// 单服务器执行模式
|
|
1033
|
+
const React = await import('react');
|
|
1034
|
+
const { render } = await import('ink');
|
|
1035
|
+
const { MultiStepCommandGenerator } = await import('../src/components/MultiStepCommandGenerator.js');
|
|
1036
|
+
// 如果是远程模式,先获取远程上下文
|
|
1037
|
+
let remoteContext = null;
|
|
1038
|
+
if (remoteName) {
|
|
1039
|
+
console.log('');
|
|
1040
|
+
console2.info(`正在连接远程服务器 ${remoteName}...`);
|
|
1041
|
+
try {
|
|
1042
|
+
// 采集系统信息(使用缓存)
|
|
1043
|
+
const sysInfo = await collectRemoteSysInfo(remoteName);
|
|
1044
|
+
if (options.debug) {
|
|
1045
|
+
console2.muted(`系统: ${sysInfo.os} ${sysInfo.osVersion} (${sysInfo.shell})`);
|
|
1046
|
+
}
|
|
1047
|
+
// 获取远程 shell 历史
|
|
1048
|
+
const shellHistory = await fetchRemoteShellHistory(remoteName);
|
|
1049
|
+
if (options.debug && shellHistory.length > 0) {
|
|
1050
|
+
console2.muted(`Shell 历史: ${shellHistory.length} 条`);
|
|
1051
|
+
}
|
|
1052
|
+
remoteContext = { name: remoteName, sysInfo, shellHistory };
|
|
1053
|
+
console2.success(`已连接到 ${remoteName}`);
|
|
1054
|
+
}
|
|
1055
|
+
catch (error) {
|
|
1056
|
+
console2.error(`无法连接到 ${remoteName}: ${error.message}`);
|
|
1057
|
+
console.log('');
|
|
1058
|
+
process.exit(1);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
371
1061
|
const executedSteps = [];
|
|
372
1062
|
let currentStepNumber = 1;
|
|
373
1063
|
let lastStepFailed = false; // 跟踪上一步是否失败
|
|
374
1064
|
while (true) {
|
|
375
1065
|
let stepResult = null;
|
|
376
1066
|
// 使用 Ink 渲染命令生成
|
|
377
|
-
const { waitUntilExit, unmount } = render(React.createElement(MultiStepCommandGenerator, {
|
|
1067
|
+
const { waitUntilExit, unmount } = render(React.createElement(MultiStepCommandGenerator, {
|
|
1068
|
+
prompt,
|
|
1069
|
+
debug: options.debug,
|
|
1070
|
+
previousSteps: executedSteps,
|
|
1071
|
+
currentStepNumber,
|
|
1072
|
+
remoteContext: remoteContext ? {
|
|
1073
|
+
name: remoteContext.name,
|
|
1074
|
+
sysInfo: remoteContext.sysInfo,
|
|
1075
|
+
shellHistory: remoteContext.shellHistory,
|
|
1076
|
+
} : undefined,
|
|
1077
|
+
isRemote: !!remoteName, // 远程执行时不检测 builtin
|
|
1078
|
+
onStepComplete: (res) => {
|
|
378
1079
|
stepResult = res;
|
|
379
1080
|
unmount();
|
|
380
|
-
}
|
|
1081
|
+
},
|
|
1082
|
+
}));
|
|
381
1083
|
await waitUntilExit();
|
|
382
1084
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
383
1085
|
// 处理步骤结果
|
|
@@ -388,16 +1090,31 @@ program
|
|
|
388
1090
|
process.exit(0);
|
|
389
1091
|
}
|
|
390
1092
|
if (stepResult.hasBuiltin) {
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
1093
|
+
// 远程模式记录到远程历史
|
|
1094
|
+
if (remoteName) {
|
|
1095
|
+
addRemoteHistory(remoteName, {
|
|
1096
|
+
userPrompt: currentStepNumber === 1 ? prompt : `[步骤${currentStepNumber}] ${prompt}`,
|
|
1097
|
+
command: stepResult.command,
|
|
1098
|
+
aiGeneratedCommand: stepResult.aiGeneratedCommand,
|
|
1099
|
+
userModified: stepResult.userModified || false,
|
|
1100
|
+
executed: false,
|
|
1101
|
+
exitCode: null,
|
|
1102
|
+
output: '',
|
|
1103
|
+
reason: 'builtin',
|
|
1104
|
+
});
|
|
1105
|
+
}
|
|
1106
|
+
else {
|
|
1107
|
+
addHistory({
|
|
1108
|
+
userPrompt: currentStepNumber === 1 ? prompt : `[步骤${currentStepNumber}] ${prompt}`,
|
|
1109
|
+
command: stepResult.command,
|
|
1110
|
+
aiGeneratedCommand: stepResult.aiGeneratedCommand, // AI 原始命令
|
|
1111
|
+
userModified: stepResult.userModified || false,
|
|
1112
|
+
executed: false,
|
|
1113
|
+
exitCode: null,
|
|
1114
|
+
output: '',
|
|
1115
|
+
reason: 'builtin',
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
401
1118
|
process.exit(0);
|
|
402
1119
|
}
|
|
403
1120
|
if (stepResult.confirmed) {
|
|
@@ -423,10 +1140,32 @@ program
|
|
|
423
1140
|
console.log('');
|
|
424
1141
|
process.exit(1);
|
|
425
1142
|
}
|
|
426
|
-
//
|
|
1143
|
+
// 执行命令(本地或远程)
|
|
427
1144
|
const execStart = Date.now();
|
|
428
|
-
|
|
1145
|
+
let exitCode;
|
|
1146
|
+
let output;
|
|
1147
|
+
let stdout;
|
|
1148
|
+
if (remoteName) {
|
|
1149
|
+
// 远程执行
|
|
1150
|
+
const result = await executeRemoteCommand(remoteName, stepResult.command);
|
|
1151
|
+
exitCode = result.exitCode;
|
|
1152
|
+
output = result.output;
|
|
1153
|
+
stdout = result.stdout;
|
|
1154
|
+
}
|
|
1155
|
+
else {
|
|
1156
|
+
// 本地执行
|
|
1157
|
+
const result = await executeCommand(stepResult.command);
|
|
1158
|
+
exitCode = result.exitCode;
|
|
1159
|
+
output = result.output;
|
|
1160
|
+
stdout = result.stdout;
|
|
1161
|
+
}
|
|
429
1162
|
const execDuration = Date.now() - execStart;
|
|
1163
|
+
// 判断命令是否成功
|
|
1164
|
+
// 退出码 141 = 128 + 13 (SIGPIPE),是管道正常关闭时的信号
|
|
1165
|
+
// 例如:ps aux | head -3,head 读完 3 行就关闭管道,ps 收到 SIGPIPE
|
|
1166
|
+
// 但如果退出码是 141 且没有 stdout 输出,说明可能是真正的错误
|
|
1167
|
+
const isSigpipeWithOutput = exitCode === 141 && stdout.trim().length > 0;
|
|
1168
|
+
const isSuccess = exitCode === 0 || isSigpipeWithOutput;
|
|
430
1169
|
// 保存到执行历史
|
|
431
1170
|
const executedStep = {
|
|
432
1171
|
command: stepResult.command,
|
|
@@ -437,19 +1176,32 @@ program
|
|
|
437
1176
|
output,
|
|
438
1177
|
};
|
|
439
1178
|
executedSteps.push(executedStep);
|
|
440
|
-
// 记录到 pls
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
1179
|
+
// 记录到 pls 历史(远程模式记录到远程历史)
|
|
1180
|
+
if (remoteName) {
|
|
1181
|
+
addRemoteHistory(remoteName, {
|
|
1182
|
+
userPrompt: currentStepNumber === 1 ? prompt : `[步骤${currentStepNumber}] ${stepResult.reasoning || prompt}`,
|
|
1183
|
+
command: stepResult.command,
|
|
1184
|
+
aiGeneratedCommand: stepResult.aiGeneratedCommand,
|
|
1185
|
+
userModified: stepResult.userModified || false,
|
|
1186
|
+
executed: true,
|
|
1187
|
+
exitCode,
|
|
1188
|
+
output,
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1191
|
+
else {
|
|
1192
|
+
addHistory({
|
|
1193
|
+
userPrompt: currentStepNumber === 1 ? prompt : `[步骤${currentStepNumber}] ${stepResult.reasoning || prompt}`,
|
|
1194
|
+
command: stepResult.command,
|
|
1195
|
+
aiGeneratedCommand: stepResult.aiGeneratedCommand, // AI 原始命令
|
|
1196
|
+
userModified: stepResult.userModified || false,
|
|
1197
|
+
executed: true,
|
|
1198
|
+
exitCode,
|
|
1199
|
+
output,
|
|
1200
|
+
});
|
|
1201
|
+
}
|
|
450
1202
|
// 显示结果
|
|
451
1203
|
console.log('');
|
|
452
|
-
if (
|
|
1204
|
+
if (isSuccess) {
|
|
453
1205
|
if (currentStepNumber === 1 && stepResult.needsContinue !== true) {
|
|
454
1206
|
// 单步命令
|
|
455
1207
|
console2.success(`执行完成 ${console2.formatDuration(execDuration)}`);
|
|
@@ -501,22 +1253,80 @@ program
|
|
|
501
1253
|
}
|
|
502
1254
|
})();
|
|
503
1255
|
});
|
|
1256
|
+
/**
|
|
1257
|
+
* 执行远程命令
|
|
1258
|
+
* 如果设置了工作目录,自动添加 cd 前缀
|
|
1259
|
+
*/
|
|
1260
|
+
async function executeRemoteCommand(remoteName, command) {
|
|
1261
|
+
let stdout = '';
|
|
1262
|
+
let stderr = '';
|
|
1263
|
+
// 如果有工作目录,自动添加 cd 前缀
|
|
1264
|
+
const workDir = getRemoteWorkDir(remoteName);
|
|
1265
|
+
const actualCommand = workDir ? `cd ${workDir} && ${command}` : command;
|
|
1266
|
+
console.log(''); // 空行
|
|
1267
|
+
// 计算命令框宽度,让分隔线长度一致
|
|
1268
|
+
const lines = command.split('\n');
|
|
1269
|
+
const maxContentWidth = Math.max(...lines.map(l => console2.getDisplayWidth(l)));
|
|
1270
|
+
const boxWidth = Math.max(maxContentWidth + 4, console2.getDisplayWidth('生成命令') + 6, 20);
|
|
1271
|
+
console2.printSeparator(`远程输出 (${remoteName})`, boxWidth);
|
|
1272
|
+
try {
|
|
1273
|
+
const result = await sshExec(remoteName, actualCommand, {
|
|
1274
|
+
onStdout: (data) => {
|
|
1275
|
+
stdout += data;
|
|
1276
|
+
process.stdout.write(data);
|
|
1277
|
+
},
|
|
1278
|
+
onStderr: (data) => {
|
|
1279
|
+
stderr += data;
|
|
1280
|
+
process.stderr.write(data);
|
|
1281
|
+
},
|
|
1282
|
+
});
|
|
1283
|
+
if (stdout || stderr) {
|
|
1284
|
+
console2.printSeparator('', boxWidth);
|
|
1285
|
+
}
|
|
1286
|
+
return {
|
|
1287
|
+
exitCode: result.exitCode,
|
|
1288
|
+
output: stdout + stderr,
|
|
1289
|
+
stdout,
|
|
1290
|
+
stderr,
|
|
1291
|
+
};
|
|
1292
|
+
}
|
|
1293
|
+
catch (error) {
|
|
1294
|
+
console2.printSeparator('', boxWidth);
|
|
1295
|
+
console2.error(error.message);
|
|
1296
|
+
return {
|
|
1297
|
+
exitCode: 1,
|
|
1298
|
+
output: error.message,
|
|
1299
|
+
stdout: '',
|
|
1300
|
+
stderr: error.message,
|
|
1301
|
+
};
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
504
1304
|
// 自定义帮助信息
|
|
505
1305
|
program.addHelpText('after', `
|
|
506
1306
|
${chalk.bold('示例:')}
|
|
507
|
-
${chalk.hex(
|
|
508
|
-
${chalk.hex(
|
|
509
|
-
${chalk.hex(
|
|
510
|
-
${chalk.hex(
|
|
511
|
-
${chalk.hex(
|
|
512
|
-
${chalk.hex(
|
|
513
|
-
${chalk.hex(
|
|
514
|
-
${chalk.hex(
|
|
515
|
-
${chalk.hex(
|
|
516
|
-
${chalk.hex(
|
|
517
|
-
${chalk.hex(
|
|
518
|
-
${chalk.hex(
|
|
519
|
-
${chalk.hex(
|
|
520
|
-
${chalk.hex(
|
|
1307
|
+
${chalk.hex(getThemeColors().primary)('pls 安装 git')} 让 AI 生成安装 git 的命令
|
|
1308
|
+
${chalk.hex(getThemeColors().primary)('pls 查找大于 100MB 的文件')} 查找大文件
|
|
1309
|
+
${chalk.hex(getThemeColors().primary)('pls 删除刚才创建的文件')} AI 会参考历史记录
|
|
1310
|
+
${chalk.hex(getThemeColors().primary)('pls --debug 压缩 logs 目录')} 显示调试信息
|
|
1311
|
+
${chalk.hex(getThemeColors().primary)('pls chat tar 命令怎么用')} AI 对话模式
|
|
1312
|
+
${chalk.hex(getThemeColors().primary)('pls chat clear')} 清空对话历史
|
|
1313
|
+
${chalk.hex(getThemeColors().primary)('pls history')} 查看 pls 命令历史
|
|
1314
|
+
${chalk.hex(getThemeColors().primary)('pls history clear')} 清空历史记录
|
|
1315
|
+
${chalk.hex(getThemeColors().primary)('pls alias')} 查看命令别名
|
|
1316
|
+
${chalk.hex(getThemeColors().primary)('pls alias add disk "查看磁盘"')} 添加别名
|
|
1317
|
+
${chalk.hex(getThemeColors().primary)('pls disk')} 使用别名(等同于 pls @disk)
|
|
1318
|
+
${chalk.hex(getThemeColors().primary)('pls hook')} 查看 shell hook 状态
|
|
1319
|
+
${chalk.hex(getThemeColors().primary)('pls hook install')} 安装 shell hook(增强功能)
|
|
1320
|
+
${chalk.hex(getThemeColors().primary)('pls hook uninstall')} 卸载 shell hook
|
|
1321
|
+
${chalk.hex(getThemeColors().primary)('pls upgrade')} 升级到最新版本
|
|
1322
|
+
${chalk.hex(getThemeColors().primary)('pls config')} 交互式配置
|
|
1323
|
+
${chalk.hex(getThemeColors().primary)('pls config list')} 查看当前配置
|
|
1324
|
+
|
|
1325
|
+
${chalk.bold('远程执行:')}
|
|
1326
|
+
${chalk.hex(getThemeColors().primary)('pls remote')} 查看远程服务器列表
|
|
1327
|
+
${chalk.hex(getThemeColors().primary)('pls remote add myserver root@1.2.3.4')} 添加服务器
|
|
1328
|
+
${chalk.hex(getThemeColors().primary)('pls remote test myserver')} 测试连接
|
|
1329
|
+
${chalk.hex(getThemeColors().primary)('pls -r myserver 查看磁盘')} 在远程服务器执行
|
|
1330
|
+
${chalk.hex(getThemeColors().primary)('pls remote hook install myserver')} 安装远程 Shell Hook
|
|
521
1331
|
`);
|
|
522
1332
|
program.parse();
|