@yivan-lab/pretty-please 1.3.1 → 1.5.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 +250 -620
- package/bin/pls.tsx +178 -40
- package/dist/bin/pls.js +149 -27
- package/dist/package.json +10 -2
- package/dist/src/__integration__/command-generation.test.d.ts +5 -0
- package/dist/src/__integration__/command-generation.test.js +508 -0
- package/dist/src/__integration__/error-recovery.test.d.ts +5 -0
- package/dist/src/__integration__/error-recovery.test.js +511 -0
- package/dist/src/__integration__/shell-hook-workflow.test.d.ts +5 -0
- package/dist/src/__integration__/shell-hook-workflow.test.js +375 -0
- package/dist/src/__tests__/alias.test.d.ts +5 -0
- package/dist/src/__tests__/alias.test.js +421 -0
- package/dist/src/__tests__/chat-history.test.d.ts +5 -0
- package/dist/src/__tests__/chat-history.test.js +372 -0
- package/dist/src/__tests__/config.test.d.ts +5 -0
- package/dist/src/__tests__/config.test.js +822 -0
- package/dist/src/__tests__/history.test.d.ts +5 -0
- package/dist/src/__tests__/history.test.js +439 -0
- package/dist/src/__tests__/remote-history.test.d.ts +5 -0
- package/dist/src/__tests__/remote-history.test.js +641 -0
- package/dist/src/__tests__/remote.test.d.ts +5 -0
- package/dist/src/__tests__/remote.test.js +689 -0
- package/dist/src/__tests__/shell-hook-install.test.d.ts +5 -0
- package/dist/src/__tests__/shell-hook-install.test.js +413 -0
- package/dist/src/__tests__/shell-hook-remote.test.d.ts +5 -0
- package/dist/src/__tests__/shell-hook-remote.test.js +507 -0
- package/dist/src/__tests__/shell-hook.test.d.ts +5 -0
- package/dist/src/__tests__/shell-hook.test.js +440 -0
- package/dist/src/__tests__/sysinfo.test.d.ts +5 -0
- package/dist/src/__tests__/sysinfo.test.js +572 -0
- package/dist/src/__tests__/system-history.test.d.ts +5 -0
- package/dist/src/__tests__/system-history.test.js +457 -0
- package/dist/src/components/Chat.js +9 -28
- package/dist/src/config.d.ts +2 -0
- package/dist/src/config.js +30 -2
- package/dist/src/mastra-chat.js +10 -6
- package/dist/src/multi-step.js +10 -8
- package/dist/src/project-context.d.ts +22 -0
- package/dist/src/project-context.js +168 -0
- package/dist/src/prompts.d.ts +4 -4
- package/dist/src/prompts.js +23 -6
- package/dist/src/shell-hook.d.ts +32 -0
- package/dist/src/shell-hook.js +226 -33
- package/dist/src/sysinfo.d.ts +38 -9
- package/dist/src/sysinfo.js +245 -21
- package/dist/src/system-history.d.ts +18 -0
- package/dist/src/system-history.js +151 -0
- package/dist/src/ui/__tests__/theme.test.d.ts +5 -0
- package/dist/src/ui/__tests__/theme.test.js +688 -0
- package/dist/src/upgrade.js +3 -0
- package/dist/src/user-preferences.d.ts +44 -0
- package/dist/src/user-preferences.js +147 -0
- package/dist/src/utils/__tests__/platform-capabilities.test.d.ts +5 -0
- package/dist/src/utils/__tests__/platform-capabilities.test.js +214 -0
- package/dist/src/utils/__tests__/platform-exec.test.d.ts +5 -0
- package/dist/src/utils/__tests__/platform-exec.test.js +212 -0
- package/dist/src/utils/__tests__/platform-shell.test.d.ts +5 -0
- package/dist/src/utils/__tests__/platform-shell.test.js +300 -0
- package/dist/src/utils/__tests__/platform.test.d.ts +5 -0
- package/dist/src/utils/__tests__/platform.test.js +137 -0
- package/dist/src/utils/platform.d.ts +88 -0
- package/dist/src/utils/platform.js +331 -0
- package/package.json +10 -2
- package/src/__integration__/command-generation.test.ts +602 -0
- package/src/__integration__/error-recovery.test.ts +620 -0
- package/src/__integration__/shell-hook-workflow.test.ts +457 -0
- package/src/__tests__/alias.test.ts +545 -0
- package/src/__tests__/chat-history.test.ts +462 -0
- package/src/__tests__/config.test.ts +1043 -0
- package/src/__tests__/history.test.ts +538 -0
- package/src/__tests__/remote-history.test.ts +791 -0
- package/src/__tests__/remote.test.ts +866 -0
- package/src/__tests__/shell-hook-install.test.ts +510 -0
- package/src/__tests__/shell-hook-remote.test.ts +679 -0
- package/src/__tests__/shell-hook.test.ts +564 -0
- package/src/__tests__/sysinfo.test.ts +718 -0
- package/src/__tests__/system-history.test.ts +608 -0
- package/src/components/Chat.tsx +10 -37
- package/src/config.ts +29 -2
- package/src/mastra-chat.ts +12 -5
- package/src/multi-step.ts +11 -5
- package/src/project-context.ts +191 -0
- package/src/prompts.ts +26 -5
- package/src/shell-hook.ts +254 -32
- package/src/sysinfo.ts +326 -25
- package/src/system-history.ts +170 -0
- package/src/ui/__tests__/theme.test.ts +869 -0
- package/src/upgrade.ts +5 -0
- package/src/user-preferences.ts +178 -0
- package/src/utils/__tests__/platform-capabilities.test.ts +265 -0
- package/src/utils/__tests__/platform-exec.test.ts +278 -0
- package/src/utils/__tests__/platform-shell.test.ts +353 -0
- package/src/utils/__tests__/platform.test.ts +170 -0
- package/src/utils/platform.ts +431 -0
package/dist/src/shell-hook.js
CHANGED
|
@@ -5,6 +5,7 @@ import chalk from 'chalk';
|
|
|
5
5
|
import { CONFIG_DIR, getConfig, setConfigValue } from './config.js';
|
|
6
6
|
import { getHistory } from './history.js';
|
|
7
7
|
import { getCurrentTheme } from './ui/theme.js';
|
|
8
|
+
import { detectShell as platformDetectShell, } from './utils/platform.js';
|
|
8
9
|
// 获取主题颜色
|
|
9
10
|
function getColors() {
|
|
10
11
|
const theme = getCurrentTheme();
|
|
@@ -20,19 +21,36 @@ const SHELL_HISTORY_FILE = path.join(CONFIG_DIR, 'shell_history.jsonl');
|
|
|
20
21
|
// Hook 标记,用于识别我们添加的内容
|
|
21
22
|
const HOOK_START_MARKER = '# >>> pretty-please shell hook >>>';
|
|
22
23
|
const HOOK_END_MARKER = '# <<< pretty-please shell hook <<<';
|
|
24
|
+
/**
|
|
25
|
+
* 将 platform 模块的 ShellType 转换为本地 ShellType
|
|
26
|
+
*/
|
|
27
|
+
function toLocalShellType(platformShell) {
|
|
28
|
+
switch (platformShell) {
|
|
29
|
+
case 'zsh':
|
|
30
|
+
return 'zsh';
|
|
31
|
+
case 'bash':
|
|
32
|
+
return 'bash';
|
|
33
|
+
case 'powershell5':
|
|
34
|
+
case 'powershell7':
|
|
35
|
+
return 'powershell';
|
|
36
|
+
case 'cmd':
|
|
37
|
+
case 'fish':
|
|
38
|
+
case 'unknown':
|
|
39
|
+
default:
|
|
40
|
+
return 'unknown';
|
|
41
|
+
}
|
|
42
|
+
}
|
|
23
43
|
/**
|
|
24
44
|
* 检测当前 shell 类型
|
|
45
|
+
* 使用 platform 模块进行跨平台检测
|
|
25
46
|
*/
|
|
26
47
|
export function detectShell() {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (process.platform === 'win32')
|
|
34
|
-
return 'powershell';
|
|
35
|
-
return 'unknown';
|
|
48
|
+
const platformShell = platformDetectShell();
|
|
49
|
+
// CMD 不支持 Hook,提示用户
|
|
50
|
+
if (platformShell === 'cmd') {
|
|
51
|
+
return 'unknown';
|
|
52
|
+
}
|
|
53
|
+
return toLocalShellType(platformShell);
|
|
36
54
|
}
|
|
37
55
|
/**
|
|
38
56
|
* 获取 shell 配置文件路径
|
|
@@ -55,6 +73,35 @@ export function getShellConfigPath(shellType) {
|
|
|
55
73
|
return null;
|
|
56
74
|
}
|
|
57
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* 生成命令统计的公共 shell 函数
|
|
78
|
+
* 用于 zsh 和 bash,避免代码重复
|
|
79
|
+
*/
|
|
80
|
+
function generateStatFunction() {
|
|
81
|
+
return `
|
|
82
|
+
# 统计命令频率(公共函数)
|
|
83
|
+
__pls_record_stat() {
|
|
84
|
+
local cmd_name="$1"
|
|
85
|
+
local stats_file="${CONFIG_DIR}/command_stats.txt"
|
|
86
|
+
|
|
87
|
+
# 确保文件存在
|
|
88
|
+
touch "$stats_file"
|
|
89
|
+
|
|
90
|
+
# 更新统计(纯 shell 实现,不依赖 jq)
|
|
91
|
+
if grep -q "^$cmd_name=" "$stats_file" 2>/dev/null; then
|
|
92
|
+
# 命令已存在,次数 +1
|
|
93
|
+
local count=$(grep "^$cmd_name=" "$stats_file" | cut -d= -f2)
|
|
94
|
+
count=$((count + 1))
|
|
95
|
+
# macOS 和 Linux 的 sed -i 不同,使用临时文件
|
|
96
|
+
sed "s/^$cmd_name=.*/$cmd_name=$count/" "$stats_file" > "$stats_file.tmp" 2>/dev/null
|
|
97
|
+
mv "$stats_file.tmp" "$stats_file" 2>/dev/null
|
|
98
|
+
else
|
|
99
|
+
# 新命令,追加
|
|
100
|
+
echo "$cmd_name=1" >> "$stats_file"
|
|
101
|
+
fi
|
|
102
|
+
}
|
|
103
|
+
`;
|
|
104
|
+
}
|
|
58
105
|
/**
|
|
59
106
|
* 生成 zsh hook 脚本
|
|
60
107
|
*/
|
|
@@ -64,6 +111,7 @@ function generateZshHook() {
|
|
|
64
111
|
return `
|
|
65
112
|
${HOOK_START_MARKER}
|
|
66
113
|
# 记录命令到 pretty-please 历史
|
|
114
|
+
${generateStatFunction()}
|
|
67
115
|
__pls_preexec() {
|
|
68
116
|
__PLS_LAST_CMD="$1"
|
|
69
117
|
__PLS_CMD_START=$(date +%s)
|
|
@@ -79,6 +127,11 @@ __pls_precmd() {
|
|
|
79
127
|
echo "{\\"cmd\\":\\"$escaped_cmd\\",\\"exit\\":$exit_code,\\"time\\":\\"$timestamp\\"}" >> "${CONFIG_DIR}/shell_history.jsonl"
|
|
80
128
|
# 保持文件不超过 ${limit} 行(从配置读取)
|
|
81
129
|
tail -n ${limit} "${CONFIG_DIR}/shell_history.jsonl" > "${CONFIG_DIR}/shell_history.jsonl.tmp" && mv "${CONFIG_DIR}/shell_history.jsonl.tmp" "${CONFIG_DIR}/shell_history.jsonl"
|
|
130
|
+
|
|
131
|
+
# 统计命令频率
|
|
132
|
+
local cmd_name=$(echo "$__PLS_LAST_CMD" | awk '{print $1}')
|
|
133
|
+
__pls_record_stat "$cmd_name"
|
|
134
|
+
|
|
82
135
|
unset __PLS_LAST_CMD
|
|
83
136
|
fi
|
|
84
137
|
}
|
|
@@ -98,6 +151,7 @@ function generateBashHook() {
|
|
|
98
151
|
return `
|
|
99
152
|
${HOOK_START_MARKER}
|
|
100
153
|
# 记录命令到 pretty-please 历史
|
|
154
|
+
${generateStatFunction()}
|
|
101
155
|
__pls_prompt_command() {
|
|
102
156
|
local exit_code=$?
|
|
103
157
|
local last_cmd=$(history 1 | sed 's/^ *[0-9]* *//')
|
|
@@ -107,6 +161,10 @@ __pls_prompt_command() {
|
|
|
107
161
|
local escaped_cmd=$(echo "$last_cmd" | sed 's/\\\\/\\\\\\\\/g; s/"/\\\\"/g')
|
|
108
162
|
echo "{\\"cmd\\":\\"$escaped_cmd\\",\\"exit\\":$exit_code,\\"time\\":\\"$timestamp\\"}" >> "${CONFIG_DIR}/shell_history.jsonl"
|
|
109
163
|
tail -n ${limit} "${CONFIG_DIR}/shell_history.jsonl" > "${CONFIG_DIR}/shell_history.jsonl.tmp" && mv "${CONFIG_DIR}/shell_history.jsonl.tmp" "${CONFIG_DIR}/shell_history.jsonl"
|
|
164
|
+
|
|
165
|
+
# 统计命令频率
|
|
166
|
+
local cmd_name=$(echo "$last_cmd" | awk '{print $1}')
|
|
167
|
+
__pls_record_stat "$cmd_name"
|
|
110
168
|
fi
|
|
111
169
|
}
|
|
112
170
|
|
|
@@ -118,15 +176,26 @@ ${HOOK_END_MARKER}
|
|
|
118
176
|
}
|
|
119
177
|
/**
|
|
120
178
|
* 生成 PowerShell hook 脚本
|
|
179
|
+
* 使用 PowerShell 原生路径处理,避免跨平台路径问题
|
|
121
180
|
*/
|
|
122
181
|
function generatePowerShellHook() {
|
|
123
182
|
const config = getConfig();
|
|
124
183
|
const limit = config.shellHistoryLimit || 10; // 从配置读取
|
|
184
|
+
// 使用 PowerShell 原生路径变量,而不是嵌入 Node.js 路径
|
|
125
185
|
return `
|
|
126
186
|
${HOOK_START_MARKER}
|
|
127
187
|
# 记录命令到 pretty-please 历史
|
|
188
|
+
# 使用 PowerShell 原生路径
|
|
189
|
+
$Global:__PlsDir = Join-Path $env:USERPROFILE ".please"
|
|
190
|
+
$Global:__PlsHistoryFile = Join-Path $Global:__PlsDir "shell_history.jsonl"
|
|
191
|
+
$Global:__PlsStatsFile = Join-Path $Global:__PlsDir "command_stats.txt"
|
|
128
192
|
$Global:__PlsLastCmd = ""
|
|
129
193
|
|
|
194
|
+
# 确保目录存在
|
|
195
|
+
if (-not (Test-Path $Global:__PlsDir)) {
|
|
196
|
+
New-Item -Path $Global:__PlsDir -ItemType Directory -Force | Out-Null
|
|
197
|
+
}
|
|
198
|
+
|
|
130
199
|
function __Pls_RecordCommand {
|
|
131
200
|
$lastCmd = (Get-History -Count 1).CommandLine
|
|
132
201
|
if ($lastCmd -and $lastCmd -ne $Global:__PlsLastCmd) {
|
|
@@ -136,10 +205,38 @@ function __Pls_RecordCommand {
|
|
|
136
205
|
$timestamp = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
|
|
137
206
|
$escapedCmd = $lastCmd -replace '\\\\', '\\\\\\\\' -replace '"', '\\\\"'
|
|
138
207
|
$json = "{\`"cmd\`":\`"$escapedCmd\`",\`"exit\`":$exitCode,\`"time\`":\`"$timestamp\`"}"
|
|
139
|
-
Add-Content -Path
|
|
208
|
+
Add-Content -Path $Global:__PlsHistoryFile -Value $json
|
|
140
209
|
# 保持文件不超过 ${limit} 行(从配置读取)
|
|
141
|
-
$content = Get-Content
|
|
142
|
-
$content
|
|
210
|
+
$content = Get-Content $Global:__PlsHistoryFile -Tail ${limit} -ErrorAction SilentlyContinue
|
|
211
|
+
if ($content) {
|
|
212
|
+
$content | Set-Content $Global:__PlsHistoryFile
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
# 统计命令频率
|
|
216
|
+
$cmdName = $lastCmd -split ' ' | Select-Object -First 1
|
|
217
|
+
|
|
218
|
+
# 确保统计文件存在
|
|
219
|
+
if (-not (Test-Path $Global:__PlsStatsFile)) {
|
|
220
|
+
New-Item -Path $Global:__PlsStatsFile -ItemType File -Force | Out-Null
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
# 更新统计
|
|
224
|
+
$stats = Get-Content $Global:__PlsStatsFile -ErrorAction SilentlyContinue
|
|
225
|
+
$found = $false
|
|
226
|
+
$newStats = @()
|
|
227
|
+
foreach ($line in $stats) {
|
|
228
|
+
if ($line -match "^$cmdName=(\\d+)$") {
|
|
229
|
+
$count = [int]$matches[1] + 1
|
|
230
|
+
$newStats += "$cmdName=$count"
|
|
231
|
+
$found = $true
|
|
232
|
+
} else {
|
|
233
|
+
$newStats += $line
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (-not $found) {
|
|
237
|
+
$newStats += "$cmdName=1"
|
|
238
|
+
}
|
|
239
|
+
$newStats | Set-Content $Global:__PlsStatsFile
|
|
143
240
|
}
|
|
144
241
|
}
|
|
145
242
|
|
|
@@ -177,6 +274,18 @@ export async function installShellHook() {
|
|
|
177
274
|
const colors = getColors();
|
|
178
275
|
if (!configPath) {
|
|
179
276
|
console.log(chalk.hex(colors.error)(`❌ 不支持的 shell 类型: ${shellType}`));
|
|
277
|
+
// CMD 特殊提示
|
|
278
|
+
if (shellType === 'unknown') {
|
|
279
|
+
const platformShell = platformDetectShell();
|
|
280
|
+
if (platformShell === 'cmd') {
|
|
281
|
+
console.log('');
|
|
282
|
+
console.log(chalk.hex(colors.warning)('⚠️ CMD 不支持 Shell Hook 功能'));
|
|
283
|
+
console.log(chalk.hex(colors.secondary)('建议使用 PowerShell 获得完整体验:'));
|
|
284
|
+
console.log(chalk.hex(colors.secondary)(' 1. 按 Win 键搜索 "PowerShell"'));
|
|
285
|
+
console.log(chalk.hex(colors.secondary)(' 2. 在 PowerShell 中运行 pls hook install'));
|
|
286
|
+
console.log('');
|
|
287
|
+
}
|
|
288
|
+
}
|
|
180
289
|
return false;
|
|
181
290
|
}
|
|
182
291
|
const hookScript = generateHookScript(shellType);
|
|
@@ -434,39 +543,60 @@ export function displayShellHistory() {
|
|
|
434
543
|
console.log('');
|
|
435
544
|
}
|
|
436
545
|
/**
|
|
437
|
-
*
|
|
438
|
-
*
|
|
546
|
+
* 重新安装 Shell Hook(通用函数)
|
|
547
|
+
* 用于版本升级、配置变更等场景
|
|
548
|
+
*
|
|
549
|
+
* @param options.silent 是否静默模式(不输出日志)
|
|
550
|
+
* @param options.reason 重装原因(用于日志显示)
|
|
551
|
+
* @returns 是否成功重装
|
|
439
552
|
*/
|
|
440
|
-
export async function
|
|
553
|
+
export async function reinstallShellHook(options) {
|
|
441
554
|
const config = getConfig();
|
|
442
555
|
// 只有在 hook 已启用时才重装
|
|
443
556
|
if (!config.shellHook) {
|
|
444
557
|
return false;
|
|
445
558
|
}
|
|
446
|
-
// 值没有变化,不需要重装
|
|
447
|
-
if (oldLimit === newLimit) {
|
|
448
|
-
return false;
|
|
449
|
-
}
|
|
450
559
|
const colors = getColors();
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
560
|
+
const { silent = false, reason } = options || {};
|
|
561
|
+
if (!silent) {
|
|
562
|
+
console.log('');
|
|
563
|
+
if (reason) {
|
|
564
|
+
console.log(chalk.hex(colors.primary)(reason));
|
|
565
|
+
}
|
|
566
|
+
console.log(chalk.hex(colors.primary)('正在更新 Shell Hook...'));
|
|
567
|
+
}
|
|
568
|
+
// 卸载旧版本,安装新版本
|
|
454
569
|
uninstallShellHook();
|
|
455
570
|
await installShellHook();
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
571
|
+
if (!silent) {
|
|
572
|
+
console.log('');
|
|
573
|
+
console.log(chalk.hex(colors.warning)('⚠️ 请重启终端或运行以下命令使新配置生效:'));
|
|
574
|
+
const shellType = detectShell();
|
|
575
|
+
let configFile = '~/.zshrc';
|
|
576
|
+
if (shellType === 'bash') {
|
|
577
|
+
configFile = process.platform === 'darwin' ? '~/.bash_profile' : '~/.bashrc';
|
|
578
|
+
}
|
|
579
|
+
else if (shellType === 'powershell') {
|
|
580
|
+
configFile = '~/Documents/PowerShell/Microsoft.PowerShell_profile.ps1';
|
|
581
|
+
}
|
|
582
|
+
console.log(chalk.gray(` source ${configFile}`));
|
|
583
|
+
console.log('');
|
|
465
584
|
}
|
|
466
|
-
console.log(chalk.gray(` source ${configFile}`));
|
|
467
|
-
console.log('');
|
|
468
585
|
return true;
|
|
469
586
|
}
|
|
587
|
+
/**
|
|
588
|
+
* 当 shellHistoryLimit 变化时,自动重装 Hook
|
|
589
|
+
* 返回是否成功重装
|
|
590
|
+
*/
|
|
591
|
+
export async function reinstallHookForLimitChange(oldLimit, newLimit) {
|
|
592
|
+
// 值没有变化,不需要重装
|
|
593
|
+
if (oldLimit === newLimit) {
|
|
594
|
+
return false;
|
|
595
|
+
}
|
|
596
|
+
return reinstallShellHook({
|
|
597
|
+
reason: `检测到 shellHistoryLimit 变化 (${oldLimit} → ${newLimit})`,
|
|
598
|
+
});
|
|
599
|
+
}
|
|
470
600
|
/**
|
|
471
601
|
* 清空 shell 历史
|
|
472
602
|
*/
|
|
@@ -479,6 +609,69 @@ export function clearShellHistory() {
|
|
|
479
609
|
console.log(chalk.hex(colors.success)('✓ Shell 历史已清空'));
|
|
480
610
|
console.log('');
|
|
481
611
|
}
|
|
612
|
+
// ================== 统一历史获取 ==================
|
|
613
|
+
/**
|
|
614
|
+
* 获取 shell 历史(统一接口)
|
|
615
|
+
* 优先使用 shell hook,降级到系统历史文件
|
|
616
|
+
*
|
|
617
|
+
* 这是推荐的历史获取方式,会自动选择最佳来源:
|
|
618
|
+
* 1. 优先:shell hook(有退出码,最准确)
|
|
619
|
+
* 2. 降级:系统历史文件(无退出码,兼容未安装 hook 的情况)
|
|
620
|
+
*/
|
|
621
|
+
export function getShellHistoryWithFallback() {
|
|
622
|
+
const config = getConfig();
|
|
623
|
+
// 优先使用 shell hook
|
|
624
|
+
if (config.shellHook) {
|
|
625
|
+
const history = getShellHistory();
|
|
626
|
+
if (history.length > 0) {
|
|
627
|
+
return history;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
// 降级到系统历史文件
|
|
631
|
+
const { getSystemShellHistory } = require('./system-history.js');
|
|
632
|
+
return getSystemShellHistory();
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* 获取最近一条非 pls 的命令(统一接口)
|
|
636
|
+
* 自动选择最佳历史来源
|
|
637
|
+
*/
|
|
638
|
+
export function getLastNonPlsCommand() {
|
|
639
|
+
const history = getShellHistoryWithFallback();
|
|
640
|
+
// 从后往前找第一条非 pls 命令
|
|
641
|
+
for (let i = history.length - 1; i >= 0; i--) {
|
|
642
|
+
const item = history[i];
|
|
643
|
+
if (!item.cmd.startsWith('pls') && !item.cmd.startsWith('please')) {
|
|
644
|
+
return item;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
return null;
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* 格式化 shell 历史供 AI 使用(统一接口)
|
|
651
|
+
* 自动选择最佳历史来源
|
|
652
|
+
*/
|
|
653
|
+
export function formatShellHistoryForAIWithFallback() {
|
|
654
|
+
const config = getConfig();
|
|
655
|
+
// 如果启用了 shell hook 且有记录,使用 hook 历史(包含详细信息)
|
|
656
|
+
if (config.shellHook) {
|
|
657
|
+
const hookHistory = getShellHistory();
|
|
658
|
+
if (hookHistory.length > 0) {
|
|
659
|
+
return formatShellHistoryForAI();
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
// 降级到系统历史
|
|
663
|
+
const { getSystemShellHistory } = require('./system-history.js');
|
|
664
|
+
const history = getSystemShellHistory();
|
|
665
|
+
if (history.length === 0) {
|
|
666
|
+
return '';
|
|
667
|
+
}
|
|
668
|
+
// 格式化系统历史(简单格式,无详细信息)
|
|
669
|
+
const lines = history.map((item, index) => {
|
|
670
|
+
const status = item.exit === 0 ? '✓' : `✗ 退出码:${item.exit}`;
|
|
671
|
+
return `${index + 1}. ${item.cmd} ${status}`;
|
|
672
|
+
});
|
|
673
|
+
return `【用户终端最近执行的命令(来自系统历史)】\n${lines.join('\n')}`;
|
|
674
|
+
}
|
|
482
675
|
// ================== 远程 Shell Hook ==================
|
|
483
676
|
/**
|
|
484
677
|
* 生成远程 zsh hook 脚本
|
package/dist/src/sysinfo.d.ts
CHANGED
|
@@ -1,19 +1,48 @@
|
|
|
1
|
+
import { type ProjectContext } from './project-context.js';
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
+
* 静态系统信息(缓存 7 天)
|
|
3
4
|
*/
|
|
4
|
-
export interface
|
|
5
|
-
os:
|
|
5
|
+
export interface StaticSystemInfo {
|
|
6
|
+
os: string;
|
|
6
7
|
arch: string;
|
|
7
8
|
shell: string;
|
|
8
|
-
packageManager: string;
|
|
9
|
-
cwd: string;
|
|
10
9
|
user: string;
|
|
10
|
+
systemPackageManager: string;
|
|
11
|
+
availableCommands: string[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 动态系统信息(每次实时获取)
|
|
15
|
+
*/
|
|
16
|
+
export interface DynamicSystemInfo {
|
|
17
|
+
cwd: string;
|
|
18
|
+
project: ProjectContext | null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 完整系统信息
|
|
22
|
+
*/
|
|
23
|
+
export interface SystemInfo extends StaticSystemInfo, DynamicSystemInfo {
|
|
11
24
|
}
|
|
12
25
|
/**
|
|
13
|
-
*
|
|
26
|
+
* 获取静态系统信息(带缓存)
|
|
27
|
+
*/
|
|
28
|
+
export declare function getStaticSystemInfo(): StaticSystemInfo;
|
|
29
|
+
/**
|
|
30
|
+
* 获取动态系统信息(每次实时)
|
|
31
|
+
*/
|
|
32
|
+
export declare function getDynamicSystemInfo(): Promise<DynamicSystemInfo>;
|
|
33
|
+
/**
|
|
34
|
+
* 获取完整系统信息(主接口)
|
|
35
|
+
*/
|
|
36
|
+
export declare function getSystemInfo(): Promise<SystemInfo>;
|
|
37
|
+
/**
|
|
38
|
+
* 格式化系统信息为字符串(供 AI 使用)
|
|
39
|
+
*/
|
|
40
|
+
export declare function formatSystemInfo(info: SystemInfo): string;
|
|
41
|
+
/**
|
|
42
|
+
* 显示系统信息(CLI 美化版)
|
|
14
43
|
*/
|
|
15
|
-
export declare function
|
|
44
|
+
export declare function displaySystemInfo(info: SystemInfo): void;
|
|
16
45
|
/**
|
|
17
|
-
*
|
|
46
|
+
* 强制刷新缓存
|
|
18
47
|
*/
|
|
19
|
-
export declare function
|
|
48
|
+
export declare function refreshSystemCache(): void;
|