@yivan-lab/pretty-please 1.1.0 → 1.3.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 +390 -1
- package/bin/pls.tsx +1255 -123
- package/dist/bin/pls.js +1098 -103
- 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 +54 -26
- package/dist/src/components/CodeColorizer.js +26 -20
- package/dist/src/components/CommandBox.js +19 -8
- 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 +33 -3
- package/dist/src/config.js +83 -34
- package/dist/src/mastra-agent.d.ts +1 -0
- package/dist/src/mastra-agent.js +3 -11
- package/dist/src/mastra-chat.d.ts +13 -6
- package/dist/src/mastra-chat.js +31 -31
- package/dist/src/multi-step.d.ts +23 -7
- package/dist/src/multi-step.js +45 -26
- package/dist/src/prompts.d.ts +30 -4
- package/dist/src/prompts.js +218 -70
- 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 +58 -0
- package/dist/src/shell-hook.js +295 -26
- package/dist/src/ui/theme.d.ts +60 -23
- package/dist/src/ui/theme.js +544 -22
- package/dist/src/upgrade.d.ts +41 -0
- package/dist/src/upgrade.js +348 -0
- package/dist/src/utils/console.d.ts +4 -0
- package/dist/src/utils/console.js +89 -17
- package/package.json +4 -4
- package/src/alias.ts +301 -0
- package/src/chat-history.ts +11 -1
- package/src/components/Chat.tsx +71 -26
- package/src/components/CodeColorizer.tsx +27 -19
- package/src/components/CommandBox.tsx +26 -8
- 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 +126 -35
- package/src/mastra-agent.ts +3 -12
- package/src/mastra-chat.ts +40 -34
- package/src/multi-step.ts +62 -30
- package/src/prompts.ts +236 -78
- package/src/remote-history.ts +390 -0
- package/src/remote.ts +800 -0
- package/src/shell-hook.ts +339 -26
- package/src/ui/theme.ts +632 -23
- package/src/upgrade.ts +397 -0
- package/src/utils/console.ts +99 -17
package/dist/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yivan-lab/pretty-please",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "AI 驱动的命令行工具,将自然语言转换为可执行的 Shell 命令",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -31,12 +31,12 @@
|
|
|
31
31
|
"license": "MIT",
|
|
32
32
|
"repository": {
|
|
33
33
|
"type": "git",
|
|
34
|
-
"url": "https://github.com/
|
|
34
|
+
"url": "https://github.com/IvanLark/pretty-please.git"
|
|
35
35
|
},
|
|
36
36
|
"bugs": {
|
|
37
|
-
"url": "https://github.com/
|
|
37
|
+
"url": "https://github.com/IvanLark/pretty-please/issues"
|
|
38
38
|
},
|
|
39
|
-
"homepage": "https://github.com/
|
|
39
|
+
"homepage": "https://github.com/IvanLark/pretty-please#readme",
|
|
40
40
|
"publishConfig": {
|
|
41
41
|
"access": "public"
|
|
42
42
|
},
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type AliasConfig } from './config.js';
|
|
2
|
+
/**
|
|
3
|
+
* 别名解析结果
|
|
4
|
+
*/
|
|
5
|
+
export interface AliasResolveResult {
|
|
6
|
+
resolved: boolean;
|
|
7
|
+
prompt: string;
|
|
8
|
+
aliasName?: string;
|
|
9
|
+
originalInput?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 获取所有别名
|
|
13
|
+
*/
|
|
14
|
+
export declare function getAliases(): Record<string, AliasConfig>;
|
|
15
|
+
/**
|
|
16
|
+
* 添加别名
|
|
17
|
+
* @param name 别名名称
|
|
18
|
+
* @param prompt 对应的 prompt
|
|
19
|
+
* @param description 可选描述
|
|
20
|
+
* @param reservedCommands 保留的子命令列表(动态传入)
|
|
21
|
+
*/
|
|
22
|
+
export declare function addAlias(name: string, prompt: string, description?: string, reservedCommands?: string[]): void;
|
|
23
|
+
/**
|
|
24
|
+
* 删除别名
|
|
25
|
+
*/
|
|
26
|
+
export declare function removeAlias(name: string): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* 解析别名
|
|
29
|
+
* 支持 `pls disk` 和 `pls @disk` 两种格式
|
|
30
|
+
* @param input 用户输入(可能是别名或普通 prompt)
|
|
31
|
+
* @returns 解析结果
|
|
32
|
+
*/
|
|
33
|
+
export declare function resolveAlias(input: string): AliasResolveResult;
|
|
34
|
+
/**
|
|
35
|
+
* 显示所有别名
|
|
36
|
+
*/
|
|
37
|
+
export declare function displayAliases(): void;
|
|
38
|
+
/**
|
|
39
|
+
* 获取别名的参数信息(用于帮助显示)
|
|
40
|
+
*/
|
|
41
|
+
export declare function getAliasParams(aliasName: string): string[];
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { getConfig, saveConfig } from './config.js';
|
|
3
|
+
import { getCurrentTheme } from './ui/theme.js';
|
|
4
|
+
// 获取主题颜色
|
|
5
|
+
function getColors() {
|
|
6
|
+
const theme = getCurrentTheme();
|
|
7
|
+
return {
|
|
8
|
+
primary: theme.primary,
|
|
9
|
+
secondary: theme.secondary,
|
|
10
|
+
success: theme.success,
|
|
11
|
+
error: theme.error,
|
|
12
|
+
warning: theme.warning,
|
|
13
|
+
muted: theme.text.muted,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 获取所有别名
|
|
18
|
+
*/
|
|
19
|
+
export function getAliases() {
|
|
20
|
+
const config = getConfig();
|
|
21
|
+
return config.aliases || {};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 添加别名
|
|
25
|
+
* @param name 别名名称
|
|
26
|
+
* @param prompt 对应的 prompt
|
|
27
|
+
* @param description 可选描述
|
|
28
|
+
* @param reservedCommands 保留的子命令列表(动态传入)
|
|
29
|
+
*/
|
|
30
|
+
export function addAlias(name, prompt, description, reservedCommands = []) {
|
|
31
|
+
// 验证别名名称
|
|
32
|
+
if (!name || !name.trim()) {
|
|
33
|
+
throw new Error('别名名称不能为空');
|
|
34
|
+
}
|
|
35
|
+
// 移除可能的 @ 前缀
|
|
36
|
+
const aliasName = name.startsWith('@') ? name.slice(1) : name;
|
|
37
|
+
// 验证别名名称格式(只允许字母、数字、下划线、连字符)
|
|
38
|
+
if (!/^[a-zA-Z0-9_-]+$/.test(aliasName)) {
|
|
39
|
+
throw new Error('别名名称只能包含字母、数字、下划线和连字符');
|
|
40
|
+
}
|
|
41
|
+
// 检查是否与保留命令冲突
|
|
42
|
+
if (reservedCommands.includes(aliasName)) {
|
|
43
|
+
throw new Error(`"${aliasName}" 是保留的子命令,不能用作别名`);
|
|
44
|
+
}
|
|
45
|
+
// 验证 prompt
|
|
46
|
+
if (!prompt || !prompt.trim()) {
|
|
47
|
+
throw new Error('prompt 不能为空');
|
|
48
|
+
}
|
|
49
|
+
const config = getConfig();
|
|
50
|
+
if (!config.aliases) {
|
|
51
|
+
config.aliases = {};
|
|
52
|
+
}
|
|
53
|
+
config.aliases[aliasName] = {
|
|
54
|
+
prompt: prompt.trim(),
|
|
55
|
+
description: description?.trim(),
|
|
56
|
+
};
|
|
57
|
+
saveConfig(config);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 删除别名
|
|
61
|
+
*/
|
|
62
|
+
export function removeAlias(name) {
|
|
63
|
+
// 移除可能的 @ 前缀
|
|
64
|
+
const aliasName = name.startsWith('@') ? name.slice(1) : name;
|
|
65
|
+
const config = getConfig();
|
|
66
|
+
if (!config.aliases || !config.aliases[aliasName]) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
delete config.aliases[aliasName];
|
|
70
|
+
saveConfig(config);
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* 解析参数模板
|
|
75
|
+
* 支持格式:{{param}} 或 {{param:default}}
|
|
76
|
+
*/
|
|
77
|
+
function parseTemplateParams(prompt) {
|
|
78
|
+
const regex = /\{\{([^}:]+)(?::[^}]*)?\}\}/g;
|
|
79
|
+
const params = [];
|
|
80
|
+
let match;
|
|
81
|
+
while ((match = regex.exec(prompt)) !== null) {
|
|
82
|
+
if (!params.includes(match[1])) {
|
|
83
|
+
params.push(match[1]);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return params;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 替换模板参数
|
|
90
|
+
* @param prompt 原始 prompt(可能包含模板参数)
|
|
91
|
+
* @param args 用户提供的参数(key=value 或 --key=value 格式)
|
|
92
|
+
*/
|
|
93
|
+
function replaceTemplateParams(prompt, args) {
|
|
94
|
+
// 解析用户参数
|
|
95
|
+
const userParams = {};
|
|
96
|
+
for (const arg of args) {
|
|
97
|
+
// 支持 --key=value 或 key=value 格式
|
|
98
|
+
const cleanArg = arg.startsWith('--') ? arg.slice(2) : arg;
|
|
99
|
+
const eqIndex = cleanArg.indexOf('=');
|
|
100
|
+
if (eqIndex > 0) {
|
|
101
|
+
const key = cleanArg.slice(0, eqIndex);
|
|
102
|
+
const value = cleanArg.slice(eqIndex + 1);
|
|
103
|
+
userParams[key] = value;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// 替换模板参数
|
|
107
|
+
let result = prompt;
|
|
108
|
+
// 匹配 {{param}} 或 {{param:default}}
|
|
109
|
+
result = result.replace(/\{\{([^}:]+)(?::([^}]*))?\}\}/g, (match, param, defaultValue) => {
|
|
110
|
+
if (userParams[param] !== undefined) {
|
|
111
|
+
return userParams[param];
|
|
112
|
+
}
|
|
113
|
+
if (defaultValue !== undefined) {
|
|
114
|
+
return defaultValue;
|
|
115
|
+
}
|
|
116
|
+
// 没有提供值也没有默认值,保留原样(后面会报错或让用户补充)
|
|
117
|
+
return match;
|
|
118
|
+
});
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 检查是否还有未替换的模板参数
|
|
123
|
+
*/
|
|
124
|
+
function hasUnresolvedParams(prompt) {
|
|
125
|
+
const regex = /\{\{([^}:]+)\}\}/g;
|
|
126
|
+
const unresolved = [];
|
|
127
|
+
let match;
|
|
128
|
+
while ((match = regex.exec(prompt)) !== null) {
|
|
129
|
+
unresolved.push(match[1]);
|
|
130
|
+
}
|
|
131
|
+
return unresolved;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* 解析别名
|
|
135
|
+
* 支持 `pls disk` 和 `pls @disk` 两种格式
|
|
136
|
+
* @param input 用户输入(可能是别名或普通 prompt)
|
|
137
|
+
* @returns 解析结果
|
|
138
|
+
*/
|
|
139
|
+
export function resolveAlias(input) {
|
|
140
|
+
const parts = input.trim().split(/\s+/);
|
|
141
|
+
if (parts.length === 0) {
|
|
142
|
+
return { resolved: false, prompt: input };
|
|
143
|
+
}
|
|
144
|
+
let aliasName = parts[0];
|
|
145
|
+
const restArgs = parts.slice(1);
|
|
146
|
+
// 支持 @ 前缀
|
|
147
|
+
if (aliasName.startsWith('@')) {
|
|
148
|
+
aliasName = aliasName.slice(1);
|
|
149
|
+
}
|
|
150
|
+
const aliases = getAliases();
|
|
151
|
+
const aliasConfig = aliases[aliasName];
|
|
152
|
+
if (!aliasConfig) {
|
|
153
|
+
return { resolved: false, prompt: input };
|
|
154
|
+
}
|
|
155
|
+
// 检查是否有模板参数
|
|
156
|
+
const templateParams = parseTemplateParams(aliasConfig.prompt);
|
|
157
|
+
let resolvedPrompt;
|
|
158
|
+
if (templateParams.length > 0) {
|
|
159
|
+
// 有模板参数,进行替换
|
|
160
|
+
resolvedPrompt = replaceTemplateParams(aliasConfig.prompt, restArgs);
|
|
161
|
+
// 检查是否还有未替换的必填参数
|
|
162
|
+
const unresolved = hasUnresolvedParams(resolvedPrompt);
|
|
163
|
+
if (unresolved.length > 0) {
|
|
164
|
+
throw new Error(`别名 "${aliasName}" 缺少必填参数: ${unresolved.join(', ')}`);
|
|
165
|
+
}
|
|
166
|
+
// 过滤掉已用于参数替换的 args,剩余的追加到 prompt
|
|
167
|
+
const usedArgs = restArgs.filter((arg) => {
|
|
168
|
+
const cleanArg = arg.startsWith('--') ? arg.slice(2) : arg;
|
|
169
|
+
return cleanArg.includes('=');
|
|
170
|
+
});
|
|
171
|
+
const extraArgs = restArgs.filter((arg) => !usedArgs.includes(arg));
|
|
172
|
+
if (extraArgs.length > 0) {
|
|
173
|
+
resolvedPrompt = `${resolvedPrompt} ${extraArgs.join(' ')}`;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
// 没有模板参数,直接追加额外内容
|
|
178
|
+
if (restArgs.length > 0) {
|
|
179
|
+
resolvedPrompt = `${aliasConfig.prompt} ${restArgs.join(' ')}`;
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
resolvedPrompt = aliasConfig.prompt;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
resolved: true,
|
|
187
|
+
prompt: resolvedPrompt,
|
|
188
|
+
aliasName,
|
|
189
|
+
originalInput: input,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* 显示所有别名
|
|
194
|
+
*/
|
|
195
|
+
export function displayAliases() {
|
|
196
|
+
const aliases = getAliases();
|
|
197
|
+
const colors = getColors();
|
|
198
|
+
const aliasNames = Object.keys(aliases);
|
|
199
|
+
console.log('');
|
|
200
|
+
if (aliasNames.length === 0) {
|
|
201
|
+
console.log(chalk.gray(' 暂无别名'));
|
|
202
|
+
console.log('');
|
|
203
|
+
console.log(chalk.gray(' 使用 pls alias add <name> "<prompt>" 添加别名'));
|
|
204
|
+
console.log('');
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
console.log(chalk.bold('命令别名:'));
|
|
208
|
+
console.log(chalk.gray('━'.repeat(50)));
|
|
209
|
+
for (const name of aliasNames) {
|
|
210
|
+
const alias = aliases[name];
|
|
211
|
+
const params = parseTemplateParams(alias.prompt);
|
|
212
|
+
// 别名名称
|
|
213
|
+
let line = ` ${chalk.hex(colors.primary)(name)}`;
|
|
214
|
+
// 如果有参数,显示参数
|
|
215
|
+
if (params.length > 0) {
|
|
216
|
+
line += chalk.gray(` <${params.join('> <')}>`);
|
|
217
|
+
}
|
|
218
|
+
console.log(line);
|
|
219
|
+
// prompt 内容
|
|
220
|
+
console.log(` ${chalk.gray('→')} ${alias.prompt}`);
|
|
221
|
+
// 描述
|
|
222
|
+
if (alias.description) {
|
|
223
|
+
console.log(` ${chalk.gray(alias.description)}`);
|
|
224
|
+
}
|
|
225
|
+
console.log('');
|
|
226
|
+
}
|
|
227
|
+
console.log(chalk.gray('━'.repeat(50)));
|
|
228
|
+
console.log(chalk.gray('使用: pls <alias> 或 pls @<alias>'));
|
|
229
|
+
console.log('');
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* 获取别名的参数信息(用于帮助显示)
|
|
233
|
+
*/
|
|
234
|
+
export function getAliasParams(aliasName) {
|
|
235
|
+
const aliases = getAliases();
|
|
236
|
+
const alias = aliases[aliasName];
|
|
237
|
+
if (!alias)
|
|
238
|
+
return [];
|
|
239
|
+
return parseTemplateParams(alias.prompt);
|
|
240
|
+
}
|
package/dist/src/chat-history.js
CHANGED
|
@@ -3,6 +3,14 @@ import path from 'path';
|
|
|
3
3
|
import os from 'os';
|
|
4
4
|
import chalk from 'chalk';
|
|
5
5
|
import { getConfig } from './config.js';
|
|
6
|
+
import { getCurrentTheme } from './ui/theme.js';
|
|
7
|
+
// 获取主题颜色
|
|
8
|
+
function getColors() {
|
|
9
|
+
const theme = getCurrentTheme();
|
|
10
|
+
return {
|
|
11
|
+
primary: theme.primary,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
6
14
|
const CONFIG_DIR = path.join(os.homedir(), '.please');
|
|
7
15
|
const CHAT_HISTORY_FILE = path.join(CONFIG_DIR, 'chat_history.json');
|
|
8
16
|
/**
|
|
@@ -82,6 +90,7 @@ export function getChatRoundCount() {
|
|
|
82
90
|
export function displayChatHistory() {
|
|
83
91
|
const history = getChatHistory();
|
|
84
92
|
const config = getConfig();
|
|
93
|
+
const colors = getColors();
|
|
85
94
|
if (history.length === 0) {
|
|
86
95
|
console.log('\n' + chalk.gray('暂无对话历史'));
|
|
87
96
|
console.log('');
|
|
@@ -94,7 +103,7 @@ export function displayChatHistory() {
|
|
|
94
103
|
console.log(chalk.gray('━'.repeat(50)));
|
|
95
104
|
userMessages.forEach((msg, index) => {
|
|
96
105
|
const num = index + 1;
|
|
97
|
-
console.log(` ${chalk.
|
|
106
|
+
console.log(` ${chalk.hex(colors.primary)(num.toString().padStart(2, ' '))}. ${msg.content}`);
|
|
98
107
|
});
|
|
99
108
|
console.log(chalk.gray('━'.repeat(50)));
|
|
100
109
|
console.log(chalk.gray(`配置: 保留最近 ${config.chatHistoryLimit} 轮对话`));
|
|
@@ -3,18 +3,42 @@ import { Box, Text } from 'ink';
|
|
|
3
3
|
import Spinner from 'ink-spinner';
|
|
4
4
|
import { MarkdownDisplay } from './MarkdownDisplay.js';
|
|
5
5
|
import { chatWithMastra } from '../mastra-chat.js';
|
|
6
|
-
import { getChatRoundCount } from '../chat-history.js';
|
|
7
|
-
import {
|
|
6
|
+
import { getChatRoundCount, getChatHistory } from '../chat-history.js';
|
|
7
|
+
import { getCurrentTheme } from '../ui/theme.js';
|
|
8
|
+
import { formatSystemInfo } from '../sysinfo.js';
|
|
9
|
+
import { formatHistoryForAI } from '../history.js';
|
|
10
|
+
import { formatShellHistoryForAI, getShellHistory } from '../shell-hook.js';
|
|
11
|
+
import { getConfig } from '../config.js';
|
|
12
|
+
import { CHAT_SYSTEM_PROMPT, buildChatUserContext } from '../prompts.js';
|
|
8
13
|
/**
|
|
9
14
|
* Chat 组件 - AI 对话模式
|
|
10
15
|
* 使用正常渲染,完成后保持最后一帧在终端
|
|
11
16
|
*/
|
|
12
17
|
export function Chat({ prompt, debug, showRoundCount, onComplete }) {
|
|
18
|
+
const theme = getCurrentTheme();
|
|
13
19
|
const [status, setStatus] = useState('thinking');
|
|
14
20
|
const [content, setContent] = useState('');
|
|
15
21
|
const [duration, setDuration] = useState(0);
|
|
16
|
-
const [debugInfo, setDebugInfo] = useState(null);
|
|
17
22
|
const [roundCount] = useState(getChatRoundCount());
|
|
23
|
+
// Debug 信息:直接在 useState 初始化时计算(同步)
|
|
24
|
+
const [debugInfo] = useState(() => {
|
|
25
|
+
if (!debug)
|
|
26
|
+
return null;
|
|
27
|
+
const config = getConfig();
|
|
28
|
+
const sysinfo = formatSystemInfo();
|
|
29
|
+
const plsHistory = formatHistoryForAI();
|
|
30
|
+
const shellHistory = formatShellHistoryForAI();
|
|
31
|
+
const shellHookEnabled = config.shellHook && getShellHistory().length > 0;
|
|
32
|
+
const chatHistory = getChatHistory();
|
|
33
|
+
const userContext = buildChatUserContext(prompt, sysinfo, plsHistory, shellHistory, shellHookEnabled);
|
|
34
|
+
return {
|
|
35
|
+
sysinfo,
|
|
36
|
+
model: config.model,
|
|
37
|
+
systemPrompt: CHAT_SYSTEM_PROMPT,
|
|
38
|
+
userContext,
|
|
39
|
+
chatHistory,
|
|
40
|
+
};
|
|
41
|
+
});
|
|
18
42
|
useEffect(() => {
|
|
19
43
|
const startTime = Date.now();
|
|
20
44
|
// 流式输出回调
|
|
@@ -23,15 +47,12 @@ export function Chat({ prompt, debug, showRoundCount, onComplete }) {
|
|
|
23
47
|
setContent((prev) => prev + chunk);
|
|
24
48
|
};
|
|
25
49
|
// 调用 AI
|
|
26
|
-
chatWithMastra(prompt, { debug:
|
|
50
|
+
chatWithMastra(prompt, { debug: false, onChunk }) // 不需要 AI 返回 debug
|
|
27
51
|
.then((result) => {
|
|
28
52
|
const endTime = Date.now();
|
|
29
53
|
setDuration(endTime - startTime);
|
|
30
54
|
setStatus('done');
|
|
31
|
-
|
|
32
|
-
setDebugInfo(result.debug);
|
|
33
|
-
}
|
|
34
|
-
setTimeout(onComplete, 100);
|
|
55
|
+
setTimeout(onComplete, debug ? 500 : 100);
|
|
35
56
|
})
|
|
36
57
|
.catch((error) => {
|
|
37
58
|
setStatus('error');
|
|
@@ -40,6 +61,30 @@ export function Chat({ prompt, debug, showRoundCount, onComplete }) {
|
|
|
40
61
|
});
|
|
41
62
|
}, [prompt, debug, onComplete]);
|
|
42
63
|
return (React.createElement(Box, { flexDirection: "column" },
|
|
64
|
+
debugInfo && (React.createElement(Box, { flexDirection: "column", marginBottom: 1 },
|
|
65
|
+
React.createElement(Text, { color: theme.accent, bold: true }, "\u2501\u2501\u2501 \u8C03\u8BD5\u4FE1\u606F \u2501\u2501\u2501"),
|
|
66
|
+
React.createElement(Text, { color: theme.text.secondary },
|
|
67
|
+
"\u6A21\u578B: ",
|
|
68
|
+
debugInfo.model),
|
|
69
|
+
React.createElement(Text, { color: theme.text.secondary },
|
|
70
|
+
"\u5BF9\u8BDD\u5386\u53F2\u8F6E\u6570: ",
|
|
71
|
+
Math.floor(debugInfo.chatHistory.length / 2)),
|
|
72
|
+
debugInfo.chatHistory.length > 0 && (React.createElement(Box, { flexDirection: "column", marginTop: 1 },
|
|
73
|
+
React.createElement(Text, { color: theme.text.secondary }, "\u5386\u53F2\u5BF9\u8BDD\uFF08\u7528\u6237\u95EE\u9898\uFF09:"),
|
|
74
|
+
debugInfo.chatHistory
|
|
75
|
+
.filter((msg) => msg.role === 'user')
|
|
76
|
+
.slice(-5) // 最多显示最近 5 条
|
|
77
|
+
.map((msg, idx) => (React.createElement(Text, { key: idx, color: theme.text.muted },
|
|
78
|
+
idx + 1,
|
|
79
|
+
". ",
|
|
80
|
+
msg.content.substring(0, 50),
|
|
81
|
+
msg.content.length > 50 ? '...' : ''))))),
|
|
82
|
+
React.createElement(Box, { flexDirection: "column", marginTop: 1 },
|
|
83
|
+
React.createElement(Text, { color: theme.text.secondary }, "User Context (\u6700\u65B0\u6D88\u606F):"),
|
|
84
|
+
React.createElement(Text, { color: theme.text.muted },
|
|
85
|
+
debugInfo.userContext.substring(0, 500),
|
|
86
|
+
"...")),
|
|
87
|
+
React.createElement(Text, { color: theme.accent }, "\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501"))),
|
|
43
88
|
showRoundCount && roundCount > 0 && (React.createElement(Box, { marginBottom: 1 },
|
|
44
89
|
React.createElement(Text, { color: theme.text.secondary },
|
|
45
90
|
"(\u5BF9\u8BDD\u8F6E\u6570: ",
|
|
@@ -59,22 +104,5 @@ export function Chat({ prompt, debug, showRoundCount, onComplete }) {
|
|
|
59
104
|
React.createElement(Text, { color: theme.text.secondary },
|
|
60
105
|
"(",
|
|
61
106
|
(duration / 1000).toFixed(2),
|
|
62
|
-
"s)")))
|
|
63
|
-
debugInfo && (React.createElement(Box, { flexDirection: "column", marginY: 1 },
|
|
64
|
-
React.createElement(Text, { color: theme.accent }, "\u2501\u2501\u2501 \u8C03\u8BD5\u4FE1\u606F \u2501\u2501\u2501"),
|
|
65
|
-
React.createElement(Text, { color: theme.text.secondary },
|
|
66
|
-
"\u7CFB\u7EDF\u4FE1\u606F: ",
|
|
67
|
-
debugInfo.sysinfo),
|
|
68
|
-
React.createElement(Text, { color: theme.text.secondary },
|
|
69
|
-
"\u6A21\u578B: ",
|
|
70
|
-
debugInfo.model),
|
|
71
|
-
React.createElement(Text, { color: theme.text.secondary },
|
|
72
|
-
"\u5BF9\u8BDD\u5386\u53F2\u8F6E\u6570: ",
|
|
73
|
-
Math.floor(debugInfo.chatHistory.length / 2)),
|
|
74
|
-
React.createElement(Text, { color: theme.text.secondary }, "System Prompt:"),
|
|
75
|
-
React.createElement(Text, { dimColor: true }, debugInfo.systemPrompt),
|
|
76
|
-
React.createElement(Text, { color: theme.text.secondary },
|
|
77
|
-
"User Prompt: ",
|
|
78
|
-
debugInfo.userPrompt),
|
|
79
|
-
React.createElement(Text, { color: theme.accent }, "\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501")))));
|
|
107
|
+
"s)")))));
|
|
80
108
|
}
|
|
@@ -1,26 +1,30 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { Text, Box } from 'ink';
|
|
3
3
|
import { common, createLowlight } from 'lowlight';
|
|
4
|
-
import {
|
|
4
|
+
import { getCurrentTheme } from '../ui/theme.js';
|
|
5
5
|
// 创建 lowlight 实例
|
|
6
6
|
const lowlight = createLowlight(common);
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
7
|
+
/**
|
|
8
|
+
* 获取语法高亮颜色映射
|
|
9
|
+
*/
|
|
10
|
+
function getSyntaxColors(theme) {
|
|
11
|
+
return {
|
|
12
|
+
'hljs-keyword': theme.code.keyword,
|
|
13
|
+
'hljs-string': theme.code.string,
|
|
14
|
+
'hljs-function': theme.code.function,
|
|
15
|
+
'hljs-comment': theme.code.comment,
|
|
16
|
+
'hljs-number': theme.primary,
|
|
17
|
+
'hljs-built_in': theme.secondary,
|
|
18
|
+
'hljs-title': theme.accent,
|
|
19
|
+
'hljs-variable': theme.text.primary,
|
|
20
|
+
'hljs-type': theme.info,
|
|
21
|
+
'hljs-operator': theme.text.secondary,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
20
24
|
/**
|
|
21
25
|
* 渲染 HAST 语法树节点
|
|
22
26
|
*/
|
|
23
|
-
function renderHastNode(node, inheritedColor) {
|
|
27
|
+
function renderHastNode(node, inheritedColor, syntaxColors, theme) {
|
|
24
28
|
if (node.type === 'text') {
|
|
25
29
|
const color = inheritedColor || theme.code.text;
|
|
26
30
|
return React.createElement(Text, { color: color }, node.value);
|
|
@@ -38,26 +42,26 @@ function renderHastNode(node, inheritedColor) {
|
|
|
38
42
|
}
|
|
39
43
|
const colorToPassDown = elementColor || inheritedColor;
|
|
40
44
|
// 递归渲染子节点
|
|
41
|
-
const children = node.children?.map((child, index) => (React.createElement(React.Fragment, { key: index }, renderHastNode(child, colorToPassDown))));
|
|
45
|
+
const children = node.children?.map((child, index) => (React.createElement(React.Fragment, { key: index }, renderHastNode(child, colorToPassDown, syntaxColors, theme))));
|
|
42
46
|
return React.createElement(React.Fragment, null, children);
|
|
43
47
|
}
|
|
44
48
|
if (node.type === 'root') {
|
|
45
49
|
if (!node.children || node.children.length === 0) {
|
|
46
50
|
return null;
|
|
47
51
|
}
|
|
48
|
-
return node.children?.map((child, index) => (React.createElement(React.Fragment, { key: index }, renderHastNode(child, inheritedColor))));
|
|
52
|
+
return node.children?.map((child, index) => (React.createElement(React.Fragment, { key: index }, renderHastNode(child, inheritedColor, syntaxColors, theme))));
|
|
49
53
|
}
|
|
50
54
|
return null;
|
|
51
55
|
}
|
|
52
56
|
/**
|
|
53
57
|
* 高亮并渲染一行代码
|
|
54
58
|
*/
|
|
55
|
-
function highlightLine(line, language) {
|
|
59
|
+
function highlightLine(line, language, syntaxColors, theme) {
|
|
56
60
|
try {
|
|
57
61
|
const highlighted = !language || !lowlight.registered(language)
|
|
58
62
|
? lowlight.highlightAuto(line)
|
|
59
63
|
: lowlight.highlight(language, line);
|
|
60
|
-
const rendered = renderHastNode(highlighted, undefined);
|
|
64
|
+
const rendered = renderHastNode(highlighted, undefined, syntaxColors, theme);
|
|
61
65
|
return rendered !== null ? rendered : line;
|
|
62
66
|
}
|
|
63
67
|
catch {
|
|
@@ -68,11 +72,13 @@ function highlightLine(line, language) {
|
|
|
68
72
|
* 代码高亮组件
|
|
69
73
|
*/
|
|
70
74
|
function ColorizeCodeInternal({ code, language = null, showLineNumbers = false }) {
|
|
75
|
+
const theme = getCurrentTheme();
|
|
76
|
+
const syntaxColors = getSyntaxColors(theme);
|
|
71
77
|
const codeToHighlight = code.replace(/\n$/, '');
|
|
72
78
|
const lines = codeToHighlight.split('\n');
|
|
73
79
|
const padWidth = String(lines.length).length;
|
|
74
80
|
const renderedLines = lines.map((line, index) => {
|
|
75
|
-
const contentToRender = highlightLine(line, language);
|
|
81
|
+
const contentToRender = highlightLine(line, language, syntaxColors, theme);
|
|
76
82
|
return (React.createElement(Box, { key: index, minHeight: 1 },
|
|
77
83
|
showLineNumbers && (React.createElement(Text, { color: theme.text.dim }, `${String(index + 1).padStart(padWidth, ' ')} `)),
|
|
78
84
|
React.createElement(Text, { color: theme.code.text }, contentToRender)));
|
|
@@ -1,25 +1,36 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { Box, Text } from 'ink';
|
|
3
|
-
import {
|
|
4
|
-
import { getDisplayWidth } from '../utils/console.js';
|
|
3
|
+
import { getCurrentTheme } from '../ui/theme.js';
|
|
4
|
+
import { getDisplayWidth, wrapText } from '../utils/console.js';
|
|
5
5
|
/**
|
|
6
6
|
* CommandBox 组件 - 显示带边框和标题的命令框
|
|
7
7
|
*/
|
|
8
8
|
export const CommandBox = ({ command, title = '生成命令' }) => {
|
|
9
|
-
const
|
|
9
|
+
const theme = getCurrentTheme();
|
|
10
|
+
// 获取终端宽度,限制最大宽度
|
|
11
|
+
const termWidth = process.stdout.columns || 80;
|
|
10
12
|
const titleWidth = getDisplayWidth(title);
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
+
// 计算最大内容宽度(终端宽度 - 边框和内边距)
|
|
14
|
+
const maxContentWidth = termWidth - 6; // 减去 '│ ' 和 ' │' 以及一些余量
|
|
15
|
+
// 处理命令换行
|
|
16
|
+
const originalLines = command.split('\n');
|
|
17
|
+
const wrappedLines = [];
|
|
18
|
+
for (const line of originalLines) {
|
|
19
|
+
wrappedLines.push(...wrapText(line, maxContentWidth));
|
|
20
|
+
}
|
|
21
|
+
// 计算实际使用的宽度
|
|
22
|
+
const actualMaxWidth = Math.max(...wrappedLines.map((l) => getDisplayWidth(l)), titleWidth);
|
|
23
|
+
const boxWidth = Math.min(actualMaxWidth + 4, termWidth - 2);
|
|
13
24
|
// 顶部边框:┌─ 生成命令 ─────┐
|
|
14
25
|
const topPadding = boxWidth - titleWidth - 5;
|
|
15
|
-
const topBorder = '┌─ ' + title + ' ' + '─'.repeat(topPadding) + '┐';
|
|
26
|
+
const topBorder = '┌─ ' + title + ' ' + '─'.repeat(Math.max(0, topPadding)) + '┐';
|
|
16
27
|
// 底部边框
|
|
17
28
|
const bottomBorder = '└' + '─'.repeat(boxWidth - 2) + '┘';
|
|
18
29
|
return (React.createElement(Box, { flexDirection: "column", marginY: 1 },
|
|
19
30
|
React.createElement(Text, { color: theme.warning }, topBorder),
|
|
20
|
-
|
|
31
|
+
wrappedLines.map((line, index) => {
|
|
21
32
|
const lineWidth = getDisplayWidth(line);
|
|
22
|
-
const padding = ' '.repeat(boxWidth - lineWidth - 4);
|
|
33
|
+
const padding = ' '.repeat(Math.max(0, boxWidth - lineWidth - 4));
|
|
23
34
|
return (React.createElement(Text, { key: index },
|
|
24
35
|
React.createElement(Text, { color: theme.warning }, "\u2502 "),
|
|
25
36
|
React.createElement(Text, { color: theme.primary }, line),
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { Text, useInput } from 'ink';
|
|
3
|
-
import {
|
|
3
|
+
import { getCurrentTheme } from '../ui/theme.js';
|
|
4
4
|
/**
|
|
5
5
|
* ConfirmationPrompt 组件 - 单键确认提示
|
|
6
6
|
* 回车 = 确认,E = 编辑,Esc = 取消,Ctrl+C = 退出
|
|
7
7
|
*/
|
|
8
8
|
export const ConfirmationPrompt = ({ prompt, onConfirm, onCancel, onEdit, }) => {
|
|
9
|
+
const theme = getCurrentTheme();
|
|
9
10
|
useInput((input, key) => {
|
|
10
11
|
if (key.return) {
|
|
11
12
|
// 回车键
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { Text } from 'ink';
|
|
3
|
-
import {
|
|
3
|
+
import { getCurrentTheme } from '../ui/theme.js';
|
|
4
4
|
/**
|
|
5
5
|
* 格式化耗时
|
|
6
6
|
*/
|
|
@@ -14,6 +14,7 @@ function formatDuration(ms) {
|
|
|
14
14
|
* Duration 组件 - 显示耗时
|
|
15
15
|
*/
|
|
16
16
|
export const Duration = ({ ms }) => {
|
|
17
|
+
const theme = getCurrentTheme();
|
|
17
18
|
return React.createElement(Text, { color: theme.text.secondary },
|
|
18
19
|
"(",
|
|
19
20
|
formatDuration(ms),
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { Text } from 'ink';
|
|
3
|
-
import {
|
|
3
|
+
import { getCurrentTheme } from '../ui/theme.js';
|
|
4
4
|
/**
|
|
5
5
|
* 行内 Markdown 渲染器
|
|
6
6
|
* 处理 **粗体**、*斜体*、`代码`、~~删除线~~、<u>下划线</u>、链接
|
|
7
7
|
*/
|
|
8
8
|
function RenderInlineInternal({ text, defaultColor }) {
|
|
9
|
+
const theme = getCurrentTheme();
|
|
9
10
|
const baseColor = defaultColor || theme.text.primary;
|
|
10
11
|
// 快速路径:纯文本无 markdown
|
|
11
12
|
if (!/[*_~`<[https?:]/.test(text)) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { Text, Box } from 'ink';
|
|
3
|
-
import {
|
|
3
|
+
import { getCurrentTheme } from '../ui/theme.js';
|
|
4
4
|
import { ColorizeCode } from './CodeColorizer.js';
|
|
5
5
|
import { TableRenderer } from './TableRenderer.js';
|
|
6
6
|
import { RenderInline } from './InlineRenderer.js';
|
|
@@ -12,6 +12,7 @@ import { RenderInline } from './InlineRenderer.js';
|
|
|
12
12
|
function MarkdownDisplayInternal({ text, terminalWidth = 80 }) {
|
|
13
13
|
if (!text)
|
|
14
14
|
return React.createElement(React.Fragment, null);
|
|
15
|
+
const theme = getCurrentTheme();
|
|
15
16
|
const lines = text.split(/\r?\n/);
|
|
16
17
|
// 正则表达式
|
|
17
18
|
const headerRegex = /^ *(#{1,4}) +(.*)/;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { type ExecutedStep } from '../multi-step.js';
|
|
2
|
+
import { type ExecutedStep, type RemoteContext } from '../multi-step.js';
|
|
3
3
|
interface MultiStepCommandGeneratorProps {
|
|
4
4
|
prompt: string;
|
|
5
5
|
debug?: boolean;
|
|
@@ -18,6 +18,8 @@ interface MultiStepCommandGeneratorProps {
|
|
|
18
18
|
}) => void;
|
|
19
19
|
previousSteps?: ExecutedStep[];
|
|
20
20
|
currentStepNumber?: number;
|
|
21
|
+
remoteContext?: RemoteContext;
|
|
22
|
+
isRemote?: boolean;
|
|
21
23
|
}
|
|
22
24
|
/**
|
|
23
25
|
* MultiStepCommandGenerator 组件 - 多步骤命令生成
|