@yivan-lab/pretty-please 1.0.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 +381 -28
- package/bin/pls.tsx +1138 -109
- package/dist/bin/pls.d.ts +1 -1
- package/dist/bin/pls.js +994 -91
- package/dist/package.json +80 -0
- package/dist/src/ai.d.ts +1 -41
- package/dist/src/ai.js +9 -190
- package/dist/src/alias.d.ts +41 -0
- package/dist/src/alias.js +240 -0
- package/dist/src/builtin-detector.d.ts +14 -8
- package/dist/src/builtin-detector.js +36 -16
- package/dist/src/chat-history.d.ts +16 -11
- package/dist/src/chat-history.js +35 -4
- package/dist/src/components/Chat.js +5 -4
- package/dist/src/components/CodeColorizer.js +26 -20
- package/dist/src/components/CommandBox.js +3 -17
- package/dist/src/components/ConfirmationPrompt.d.ts +2 -1
- package/dist/src/components/ConfirmationPrompt.js +9 -4
- 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 +5 -1
- package/dist/src/components/MultiStepCommandGenerator.js +127 -14
- package/dist/src/components/TableRenderer.js +2 -1
- package/dist/src/config.d.ts +59 -9
- package/dist/src/config.js +147 -48
- package/dist/src/history.d.ts +19 -5
- package/dist/src/history.js +26 -11
- package/dist/src/mastra-agent.d.ts +0 -1
- package/dist/src/mastra-agent.js +3 -4
- package/dist/src/mastra-chat.d.ts +28 -0
- package/dist/src/mastra-chat.js +93 -0
- package/dist/src/multi-step.d.ts +23 -7
- package/dist/src/multi-step.js +29 -6
- package/dist/src/prompts.d.ts +11 -0
- package/dist/src/prompts.js +140 -0
- 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 +87 -12
- package/dist/src/shell-hook.js +315 -17
- package/dist/src/sysinfo.d.ts +9 -5
- package/dist/src/sysinfo.js +2 -2
- 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.d.ts +11 -11
- package/dist/src/utils/console.js +26 -17
- package/package.json +11 -9
- package/src/alias.ts +301 -0
- package/src/builtin-detector.ts +126 -0
- package/src/chat-history.ts +140 -0
- package/src/components/Chat.tsx +6 -5
- package/src/components/CodeColorizer.tsx +27 -19
- package/src/components/CommandBox.tsx +3 -17
- package/src/components/ConfirmationPrompt.tsx +11 -3
- 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 +167 -16
- package/src/components/TableRenderer.tsx +2 -1
- package/src/config.ts +394 -0
- package/src/history.ts +160 -0
- package/src/mastra-agent.ts +3 -4
- package/src/mastra-chat.ts +124 -0
- package/src/multi-step.ts +45 -8
- package/src/prompts.ts +154 -0
- package/src/remote-history.ts +390 -0
- package/src/remote.ts +800 -0
- package/src/shell-hook.ts +754 -0
- package/src/{sysinfo.js → sysinfo.ts} +28 -16
- package/src/ui/theme.ts +101 -24
- package/src/upgrade.ts +397 -0
- package/src/utils/{console.js → console.ts} +36 -27
- package/bin/pls.js +0 -681
- package/src/ai.js +0 -324
- package/src/builtin-detector.js +0 -98
- package/src/chat-history.js +0 -94
- package/src/components/ChatStatus.tsx +0 -53
- package/src/components/CommandGenerator.tsx +0 -184
- package/src/components/ConfigDisplay.tsx +0 -64
- package/src/components/ConfigWizard.tsx +0 -101
- package/src/components/HistoryDisplay.tsx +0 -69
- package/src/components/HookManager.tsx +0 -150
- package/src/config.js +0 -221
- package/src/history.js +0 -131
- package/src/shell-hook.js +0 -393
package/dist/bin/pls.js
CHANGED
|
@@ -1,58 +1,107 @@
|
|
|
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
|
-
import fs from 'fs';
|
|
9
6
|
import os from 'os';
|
|
10
7
|
import chalk from 'chalk';
|
|
11
|
-
|
|
12
|
-
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'
|
|
13
13
|
import { isConfigValid, setConfigValue, getConfig, maskApiKey } from '../src/config.js';
|
|
14
14
|
import { clearHistory, addHistory, getHistory, getHistoryFilePath } from '../src/history.js';
|
|
15
|
-
import { clearChatHistory, getChatRoundCount, getChatHistoryFilePath } from '../src/chat-history.js';
|
|
16
|
-
import { installShellHook, uninstallShellHook, getHookStatus, detectShell, getShellConfigPath, } from '../src/shell-hook.js';
|
|
15
|
+
import { clearChatHistory, getChatRoundCount, getChatHistoryFilePath, displayChatHistory } from '../src/chat-history.js';
|
|
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
|
+
}
|
|
17
36
|
import * as console2 from '../src/utils/console.js';
|
|
18
|
-
//
|
|
37
|
+
// 导入 package.json(Bun 会自动打包进二进制)
|
|
38
|
+
import packageJson from '../package.json' with { type: 'json' };
|
|
39
|
+
// 保留这些用于其他可能的用途
|
|
19
40
|
const __filename = fileURLToPath(import.meta.url);
|
|
20
41
|
const __dirname = dirname(__filename);
|
|
21
|
-
const packageJson = JSON.parse(fs.readFileSync(join(__dirname, '../package.json'), 'utf-8'));
|
|
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
|
-
function executeCommand(command
|
|
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
|
-
|
|
32
|
-
const
|
|
75
|
+
// 计算命令框宽度,让分隔线长度一致
|
|
76
|
+
const lines = command.split('\n');
|
|
77
|
+
const maxContentWidth = Math.max(...lines.map(l => console2.getDisplayWidth(l)));
|
|
78
|
+
const boxWidth = Math.max(maxContentWidth + 4, console2.getDisplayWidth('生成命令') + 6, 20);
|
|
79
|
+
console2.printSeparator('输出', boxWidth);
|
|
80
|
+
// 使用 bash 并启用 pipefail,确保管道中任何命令失败都能正确返回非零退出码
|
|
81
|
+
const child = exec(`set -o pipefail; ${command}`, { shell: '/bin/bash' });
|
|
33
82
|
child.stdout?.on('data', (data) => {
|
|
34
|
-
|
|
83
|
+
stdout += data;
|
|
35
84
|
hasOutput = true;
|
|
36
85
|
process.stdout.write(data);
|
|
37
86
|
});
|
|
38
87
|
child.stderr?.on('data', (data) => {
|
|
39
|
-
|
|
88
|
+
stderr += data;
|
|
40
89
|
hasOutput = true;
|
|
41
90
|
process.stderr.write(data);
|
|
42
91
|
});
|
|
43
92
|
child.on('close', (code) => {
|
|
44
93
|
if (hasOutput) {
|
|
45
|
-
console2.printSeparator('');
|
|
94
|
+
console2.printSeparator('', boxWidth);
|
|
46
95
|
}
|
|
47
|
-
resolve({ exitCode: code || 0, output });
|
|
96
|
+
resolve({ exitCode: code || 0, output: stdout + stderr, stdout, stderr });
|
|
48
97
|
});
|
|
49
98
|
child.on('error', (err) => {
|
|
50
99
|
if (!hasOutput) {
|
|
51
|
-
console2.printSeparator('');
|
|
100
|
+
console2.printSeparator('', boxWidth);
|
|
52
101
|
}
|
|
53
102
|
console2.error(err.message);
|
|
54
|
-
console2.printSeparator('');
|
|
55
|
-
resolve({ exitCode: 1, output: err.message });
|
|
103
|
+
console2.printSeparator('', boxWidth);
|
|
104
|
+
resolve({ exitCode: 1, output: err.message, stdout: '', stderr: err.message });
|
|
56
105
|
});
|
|
57
106
|
});
|
|
58
107
|
}
|
|
@@ -61,7 +110,8 @@ program
|
|
|
61
110
|
.name('pls')
|
|
62
111
|
.description('AI 驱动的命令行工具,将自然语言转换为可执行的 Shell 命令')
|
|
63
112
|
.version(packageJson.version, '-v, --version', '显示版本号')
|
|
64
|
-
.helpOption('-h, --help', '显示帮助信息')
|
|
113
|
+
.helpOption('-h, --help', '显示帮助信息')
|
|
114
|
+
.allowUnknownOption(true); // 允许未知选项(用于别名参数传递)
|
|
65
115
|
// config 子命令
|
|
66
116
|
const configCmd = program.command('config').description('管理配置');
|
|
67
117
|
configCmd
|
|
@@ -73,14 +123,18 @@ configCmd
|
|
|
73
123
|
const CONFIG_FILE = join(os.homedir(), '.please', 'config.json');
|
|
74
124
|
console.log('');
|
|
75
125
|
console2.title('当前配置:');
|
|
76
|
-
console2.muted('━'.repeat(
|
|
77
|
-
console.log(` ${chalk.hex(
|
|
78
|
-
console.log(` ${chalk.hex(
|
|
79
|
-
console.log(` ${chalk.hex(
|
|
80
|
-
console.log(` ${chalk.hex(
|
|
81
|
-
console.log(` ${chalk.hex(
|
|
82
|
-
console.log(` ${chalk.hex(
|
|
83
|
-
|
|
126
|
+
console2.muted('━'.repeat(50));
|
|
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 (浅色)')}`);
|
|
137
|
+
console2.muted('━'.repeat(50));
|
|
84
138
|
console2.muted(`配置文件: ${CONFIG_FILE}`);
|
|
85
139
|
console.log('');
|
|
86
140
|
});
|
|
@@ -106,6 +160,67 @@ configCmd.action(async () => {
|
|
|
106
160
|
const { runConfigWizard } = await import('../src/config.js');
|
|
107
161
|
await runConfigWizard();
|
|
108
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
|
+
});
|
|
109
224
|
// history 子命令
|
|
110
225
|
const historyCmd = program.command('history').description('查看或管理命令历史');
|
|
111
226
|
historyCmd
|
|
@@ -125,11 +240,18 @@ historyCmd
|
|
|
125
240
|
history.forEach((item, index) => {
|
|
126
241
|
const status = item.executed
|
|
127
242
|
? item.exitCode === 0
|
|
128
|
-
? chalk.hex(
|
|
129
|
-
: chalk.hex(
|
|
243
|
+
? chalk.hex(getThemeColors().success)('✓')
|
|
244
|
+
: chalk.hex(getThemeColors().error)(`✗ 退出码:${item.exitCode}`)
|
|
130
245
|
: chalk.gray('(未执行)');
|
|
131
|
-
console.log(`\n${chalk.gray(`${index + 1}.`)} ${chalk.hex(
|
|
132
|
-
|
|
246
|
+
console.log(`\n${chalk.gray(`${index + 1}.`)} ${chalk.hex(getThemeColors().primary)(item.userPrompt)}`);
|
|
247
|
+
// 显示用户修改信息
|
|
248
|
+
if (item.userModified && item.aiGeneratedCommand) {
|
|
249
|
+
console.log(` ${chalk.dim('AI 生成:')} ${chalk.gray(item.aiGeneratedCommand)}`);
|
|
250
|
+
console.log(` ${chalk.dim('用户修改为:')} ${item.command} ${status} ${chalk.hex(getThemeColors().warning)('(已修改)')}`);
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
console.log(` ${chalk.dim('→')} ${item.command} ${status}`);
|
|
254
|
+
}
|
|
133
255
|
console.log(` ${chalk.gray(item.timestamp)}`);
|
|
134
256
|
});
|
|
135
257
|
console.log('');
|
|
@@ -145,7 +267,32 @@ historyCmd
|
|
|
145
267
|
console2.success('历史记录已清空');
|
|
146
268
|
console.log('');
|
|
147
269
|
});
|
|
148
|
-
//
|
|
270
|
+
// history chat 子命令
|
|
271
|
+
const historyChatCmd = historyCmd.command('chat').description('查看或管理对话历史');
|
|
272
|
+
historyChatCmd.action(() => {
|
|
273
|
+
displayChatHistory();
|
|
274
|
+
});
|
|
275
|
+
historyChatCmd
|
|
276
|
+
.command('clear')
|
|
277
|
+
.description('清空对话历史')
|
|
278
|
+
.action(() => {
|
|
279
|
+
clearChatHistory();
|
|
280
|
+
console.log('');
|
|
281
|
+
console2.success('对话历史已清空');
|
|
282
|
+
console.log('');
|
|
283
|
+
});
|
|
284
|
+
// history shell 子命令
|
|
285
|
+
const historyShellCmd = historyCmd.command('shell').description('查看或管理 Shell 历史');
|
|
286
|
+
historyShellCmd.action(() => {
|
|
287
|
+
displayShellHistory();
|
|
288
|
+
});
|
|
289
|
+
historyShellCmd
|
|
290
|
+
.command('clear')
|
|
291
|
+
.description('清空 Shell 历史')
|
|
292
|
+
.action(() => {
|
|
293
|
+
clearShellHistory();
|
|
294
|
+
});
|
|
295
|
+
// 默认 history 命令(显示命令历史)
|
|
149
296
|
historyCmd.action(() => {
|
|
150
297
|
const history = getHistory();
|
|
151
298
|
if (history.length === 0) {
|
|
@@ -160,11 +307,18 @@ historyCmd.action(() => {
|
|
|
160
307
|
history.forEach((item, index) => {
|
|
161
308
|
const status = item.executed
|
|
162
309
|
? item.exitCode === 0
|
|
163
|
-
? chalk.hex(
|
|
164
|
-
: chalk.hex(
|
|
310
|
+
? chalk.hex(getThemeColors().success)('✓')
|
|
311
|
+
: chalk.hex(getThemeColors().error)(`✗ 退出码:${item.exitCode}`)
|
|
165
312
|
: chalk.gray('(未执行)');
|
|
166
|
-
console.log(`\n${chalk.gray(`${index + 1}.`)} ${chalk.hex(
|
|
167
|
-
|
|
313
|
+
console.log(`\n${chalk.gray(`${index + 1}.`)} ${chalk.hex(getThemeColors().primary)(item.userPrompt)}`);
|
|
314
|
+
// 显示用户修改信息
|
|
315
|
+
if (item.userModified && item.aiGeneratedCommand) {
|
|
316
|
+
console.log(` ${chalk.dim('AI 生成:')} ${chalk.gray(item.aiGeneratedCommand)}`);
|
|
317
|
+
console.log(` ${chalk.dim('用户修改为:')} ${item.command} ${status} ${chalk.hex(getThemeColors().warning)('(已修改)')}`);
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
console.log(` ${chalk.dim('→')} ${item.command} ${status}`);
|
|
321
|
+
}
|
|
168
322
|
console.log(` ${chalk.gray(item.timestamp)}`);
|
|
169
323
|
});
|
|
170
324
|
console.log('');
|
|
@@ -219,11 +373,11 @@ hookCmd
|
|
|
219
373
|
console.log('');
|
|
220
374
|
console2.title('📊 Shell Hook 状态');
|
|
221
375
|
console2.muted('━'.repeat(40));
|
|
222
|
-
console.log(` ${chalk.hex(
|
|
223
|
-
console.log(` ${chalk.hex(
|
|
224
|
-
console.log(` ${chalk.hex(
|
|
225
|
-
console.log(` ${chalk.hex(
|
|
226
|
-
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}`);
|
|
227
381
|
console2.muted('━'.repeat(40));
|
|
228
382
|
if (!status.installed) {
|
|
229
383
|
console.log('');
|
|
@@ -237,11 +391,11 @@ hookCmd.action(() => {
|
|
|
237
391
|
console.log('');
|
|
238
392
|
console2.title('📊 Shell Hook 状态');
|
|
239
393
|
console2.muted('━'.repeat(40));
|
|
240
|
-
console.log(` ${chalk.hex(
|
|
241
|
-
console.log(` ${chalk.hex(
|
|
242
|
-
console.log(` ${chalk.hex(
|
|
243
|
-
console.log(` ${chalk.hex(
|
|
244
|
-
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}`);
|
|
245
399
|
console2.muted('━'.repeat(40));
|
|
246
400
|
if (!status.installed) {
|
|
247
401
|
console.log('');
|
|
@@ -249,6 +403,390 @@ hookCmd.action(() => {
|
|
|
249
403
|
}
|
|
250
404
|
console.log('');
|
|
251
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
|
+
});
|
|
252
790
|
// chat 子命令
|
|
253
791
|
const chatCmd = program.command('chat').description('AI 对话模式,问答、讲解命令');
|
|
254
792
|
chatCmd
|
|
@@ -273,8 +811,8 @@ chatCmd
|
|
|
273
811
|
console.log('');
|
|
274
812
|
console2.title('💬 AI 对话模式');
|
|
275
813
|
console2.muted('━'.repeat(40));
|
|
276
|
-
console.log(` ${chalk.hex(
|
|
277
|
-
console.log(` ${chalk.hex(
|
|
814
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('当前对话轮数')}: ${roundCount}`);
|
|
815
|
+
console.log(` ${chalk.hex(getThemeColors().primary)('历史文件')}: ${historyFile}`);
|
|
278
816
|
console2.muted('━'.repeat(40));
|
|
279
817
|
console.log('');
|
|
280
818
|
console2.muted('用法:');
|
|
@@ -291,19 +829,37 @@ chatCmd
|
|
|
291
829
|
console.log('');
|
|
292
830
|
process.exit(1);
|
|
293
831
|
}
|
|
294
|
-
//
|
|
295
|
-
|
|
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
|
+
})();
|
|
296
845
|
});
|
|
297
846
|
// 默认命令(执行 prompt)
|
|
298
847
|
program
|
|
299
848
|
.argument('[prompt...]', '自然语言描述你想执行的操作')
|
|
300
849
|
.option('-d, --debug', '显示调试信息(系统信息、完整 prompt 等)')
|
|
850
|
+
.option('-r, --remote [name]', '在远程服务器上执行(不指定则使用默认服务器)')
|
|
301
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
|
+
}
|
|
302
858
|
if (promptArgs.length === 0) {
|
|
303
859
|
program.help();
|
|
304
860
|
return;
|
|
305
861
|
}
|
|
306
|
-
|
|
862
|
+
let prompt = promptArgs.join(' ');
|
|
307
863
|
if (!prompt.trim()) {
|
|
308
864
|
console.log('');
|
|
309
865
|
console2.error('请提供你想执行的操作描述');
|
|
@@ -311,6 +867,23 @@ program
|
|
|
311
867
|
console.log('');
|
|
312
868
|
process.exit(1);
|
|
313
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
|
+
}
|
|
314
887
|
// 检查配置
|
|
315
888
|
if (!isConfigValid()) {
|
|
316
889
|
console.log('');
|
|
@@ -319,18 +892,194 @@ program
|
|
|
319
892
|
console.log('');
|
|
320
893
|
process.exit(1);
|
|
321
894
|
}
|
|
322
|
-
//
|
|
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)
|
|
323
946
|
;
|
|
324
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
|
+
}
|
|
325
1061
|
const executedSteps = [];
|
|
326
1062
|
let currentStepNumber = 1;
|
|
1063
|
+
let lastStepFailed = false; // 跟踪上一步是否失败
|
|
327
1064
|
while (true) {
|
|
328
1065
|
let stepResult = null;
|
|
329
1066
|
// 使用 Ink 渲染命令生成
|
|
330
|
-
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) => {
|
|
331
1079
|
stepResult = res;
|
|
332
1080
|
unmount();
|
|
333
|
-
}
|
|
1081
|
+
},
|
|
1082
|
+
}));
|
|
334
1083
|
await waitUntilExit();
|
|
335
1084
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
336
1085
|
// 处理步骤结果
|
|
@@ -341,21 +1090,82 @@ program
|
|
|
341
1090
|
process.exit(0);
|
|
342
1091
|
}
|
|
343
1092
|
if (stepResult.hasBuiltin) {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
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
|
+
}
|
|
352
1118
|
process.exit(0);
|
|
353
1119
|
}
|
|
354
1120
|
if (stepResult.confirmed) {
|
|
355
|
-
//
|
|
1121
|
+
// 如果命令为空,说明 AI 决定放弃
|
|
1122
|
+
if (!stepResult.command || stepResult.command.trim() === '') {
|
|
1123
|
+
console.log('');
|
|
1124
|
+
if (stepResult.reasoning) {
|
|
1125
|
+
console2.info(`💡 AI 分析: ${stepResult.reasoning}`);
|
|
1126
|
+
}
|
|
1127
|
+
console2.muted('❌ AI 决定停止尝试,任务失败');
|
|
1128
|
+
console.log('');
|
|
1129
|
+
process.exit(1);
|
|
1130
|
+
}
|
|
1131
|
+
// 特殊处理:如果上一步失败,且 AI 决定放弃(continue: false),直接显示原因并退出
|
|
1132
|
+
if (lastStepFailed &&
|
|
1133
|
+
stepResult.needsContinue === false &&
|
|
1134
|
+
stepResult.command.startsWith('echo')) {
|
|
1135
|
+
console.log('');
|
|
1136
|
+
if (stepResult.reasoning) {
|
|
1137
|
+
console2.info(`💡 AI 分析: ${stepResult.reasoning}`);
|
|
1138
|
+
}
|
|
1139
|
+
console2.muted('❌ AI 决定停止尝试,任务失败');
|
|
1140
|
+
console.log('');
|
|
1141
|
+
process.exit(1);
|
|
1142
|
+
}
|
|
1143
|
+
// 执行命令(本地或远程)
|
|
356
1144
|
const execStart = Date.now();
|
|
357
|
-
|
|
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
|
+
}
|
|
358
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;
|
|
359
1169
|
// 保存到执行历史
|
|
360
1170
|
const executedStep = {
|
|
361
1171
|
command: stepResult.command,
|
|
@@ -366,17 +1176,32 @@ program
|
|
|
366
1176
|
output,
|
|
367
1177
|
};
|
|
368
1178
|
executedSteps.push(executedStep);
|
|
369
|
-
// 记录到 pls
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
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
|
+
}
|
|
377
1202
|
// 显示结果
|
|
378
1203
|
console.log('');
|
|
379
|
-
if (
|
|
1204
|
+
if (isSuccess) {
|
|
380
1205
|
if (currentStepNumber === 1 && stepResult.needsContinue !== true) {
|
|
381
1206
|
// 单步命令
|
|
382
1207
|
console2.success(`执行完成 ${console2.formatDuration(execDuration)}`);
|
|
@@ -385,13 +1210,18 @@ program
|
|
|
385
1210
|
// 多步命令
|
|
386
1211
|
console2.success(`步骤 ${currentStepNumber} 执行完成 ${console2.formatDuration(execDuration)}`);
|
|
387
1212
|
}
|
|
1213
|
+
lastStepFailed = false;
|
|
388
1214
|
}
|
|
389
1215
|
else {
|
|
390
|
-
//
|
|
1216
|
+
// 执行失败,标记状态
|
|
391
1217
|
console2.error(`步骤 ${currentStepNumber} 执行失败,退出码: ${exitCode} ${console2.formatDuration(execDuration)}`);
|
|
392
1218
|
console.log('');
|
|
393
1219
|
console2.warning('正在请 AI 分析错误并调整策略...');
|
|
394
|
-
|
|
1220
|
+
lastStepFailed = true;
|
|
1221
|
+
// 继续循环,让 AI 分析错误
|
|
1222
|
+
console.log('');
|
|
1223
|
+
currentStepNumber++;
|
|
1224
|
+
continue;
|
|
395
1225
|
}
|
|
396
1226
|
// 判断是否继续
|
|
397
1227
|
if (stepResult.needsContinue !== true) {
|
|
@@ -405,25 +1235,98 @@ program
|
|
|
405
1235
|
console.log('');
|
|
406
1236
|
currentStepNumber++;
|
|
407
1237
|
}
|
|
1238
|
+
else if (!stepResult.confirmed && !stepResult.cancelled) {
|
|
1239
|
+
// AI 返回了结果但没有确认(空命令的情况)
|
|
1240
|
+
if (lastStepFailed && stepResult.reasoning) {
|
|
1241
|
+
console.log('');
|
|
1242
|
+
console2.info(`💡 AI 分析: ${stepResult.reasoning}`);
|
|
1243
|
+
console2.muted('❌ AI 决定停止尝试,任务失败');
|
|
1244
|
+
console.log('');
|
|
1245
|
+
process.exit(1);
|
|
1246
|
+
}
|
|
1247
|
+
// 其他情况也退出
|
|
1248
|
+
console.log('');
|
|
1249
|
+
console2.muted('任务结束');
|
|
1250
|
+
console.log('');
|
|
1251
|
+
process.exit(0);
|
|
1252
|
+
}
|
|
408
1253
|
}
|
|
409
1254
|
})();
|
|
410
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
|
+
}
|
|
411
1304
|
// 自定义帮助信息
|
|
412
1305
|
program.addHelpText('after', `
|
|
413
1306
|
${chalk.bold('示例:')}
|
|
414
|
-
${chalk.hex(
|
|
415
|
-
${chalk.hex(
|
|
416
|
-
${chalk.hex(
|
|
417
|
-
${chalk.hex(
|
|
418
|
-
${chalk.hex(
|
|
419
|
-
${chalk.hex(
|
|
420
|
-
${chalk.hex(
|
|
421
|
-
${chalk.hex(
|
|
422
|
-
${chalk.hex(
|
|
423
|
-
${chalk.hex(
|
|
424
|
-
${chalk.hex(
|
|
425
|
-
${chalk.hex(
|
|
426
|
-
${chalk.hex(
|
|
427
|
-
${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
|
|
428
1331
|
`);
|
|
429
1332
|
program.parse();
|