@yivan-lab/pretty-please 1.2.0 → 1.3.1

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.
@@ -1,26 +1,18 @@
1
1
  import { Agent } from '@mastra/core';
2
2
  import { getConfig } from './config.js';
3
- import { buildCommandSystemPrompt } from './prompts.js';
4
- import { formatSystemInfo } from './sysinfo.js';
5
- import { formatHistoryForAI } from './history.js';
6
- import { formatShellHistoryForAI, getShellHistory } from './shell-hook.js';
3
+ import { SHELL_COMMAND_SYSTEM_PROMPT } from './prompts.js';
7
4
  /**
8
5
  * 创建 Mastra Shell Agent
9
6
  * 根据用户配置的 API Key、Base URL、Provider 和 Model
7
+ * 使用静态的 System Prompt(不包含动态数据)
10
8
  */
11
9
  export function createShellAgent() {
12
10
  const config = getConfig();
13
11
  // 组合 provider/model 格式(Mastra 要求)
14
12
  const modelId = `${config.provider}/${config.model}`;
15
- // 构建系统提示词
16
- const sysinfo = formatSystemInfo();
17
- const plsHistory = formatHistoryForAI();
18
- const shellHistory = formatShellHistoryForAI();
19
- const shellHookEnabled = config.shellHook && getShellHistory().length > 0;
20
- const systemPrompt = buildCommandSystemPrompt(sysinfo, plsHistory, shellHistory, shellHookEnabled);
21
13
  return new Agent({
22
14
  name: 'shell-commander',
23
- instructions: systemPrompt,
15
+ instructions: SHELL_COMMAND_SYSTEM_PROMPT,
24
16
  model: {
25
17
  url: config.baseUrl,
26
18
  id: modelId,
@@ -1,14 +1,21 @@
1
1
  import { Agent } from '@mastra/core';
2
2
  /**
3
- * 创建 Mastra Chat Agent
3
+ * 创建 Mastra Chat Agent(使用静态系统提示词)
4
4
  */
5
5
  export declare function createChatAgent(): Agent<"chat-assistant", Record<string, import("@mastra/core").ToolAction<any, any, any, any, import("@mastra/core").ToolExecutionContext<any, any, any>>>, Record<string, import("@mastra/core").Metric>>;
6
- /**
7
- * 获取完整的系统提示词(用于调试)
8
- */
9
- export declare function getChatSystemPrompt(): string;
10
6
  /**
11
7
  * 使用 Mastra 进行 AI 对话(支持流式输出)
8
+ *
9
+ * 消息结构:
10
+ * [
11
+ * "历史问题1", // user (纯粹的问题)
12
+ * "历史回答1", // assistant
13
+ * "历史问题2", // user
14
+ * "历史回答2", // assistant
15
+ * "<system_info>...\n // user (最新消息,包含完整上下文)
16
+ * <command_history>...\n
17
+ * <user_question>最新问题</user_question>"
18
+ * ]
12
19
  */
13
20
  export declare function chatWithMastra(prompt: string, options?: {
14
21
  debug?: boolean;
@@ -23,6 +30,6 @@ export declare function chatWithMastra(prompt: string, options?: {
23
30
  role: string;
24
31
  content: string;
25
32
  }>;
26
- userPrompt: string;
33
+ userContext: string;
27
34
  };
28
35
  }>;
@@ -1,26 +1,20 @@
1
1
  import { Agent } from '@mastra/core';
2
2
  import { getConfig } from './config.js';
3
- import { buildChatSystemPrompt } from './prompts.js';
3
+ import { CHAT_SYSTEM_PROMPT, buildChatUserContext } from './prompts.js';
4
4
  import { formatSystemInfo } from './sysinfo.js';
5
5
  import { formatHistoryForAI } from './history.js';
6
6
  import { formatShellHistoryForAI, getShellHistory } from './shell-hook.js';
7
7
  import { getChatHistory, addChatMessage } from './chat-history.js';
8
8
  /**
9
- * 创建 Mastra Chat Agent
9
+ * 创建 Mastra Chat Agent(使用静态系统提示词)
10
10
  */
11
11
  export function createChatAgent() {
12
12
  const config = getConfig();
13
13
  // 组合 provider/model 格式(Mastra 要求)
14
14
  const modelId = `${config.provider}/${config.model}`;
15
- // 构建系统提示词
16
- const sysinfo = formatSystemInfo();
17
- const plsHistory = formatHistoryForAI();
18
- const shellHistory = formatShellHistoryForAI();
19
- const shellHookEnabled = config.shellHook && getShellHistory().length > 0;
20
- const systemPrompt = buildChatSystemPrompt(sysinfo, plsHistory, shellHistory, shellHookEnabled);
21
15
  return new Agent({
22
16
  name: 'chat-assistant',
23
- instructions: systemPrompt,
17
+ instructions: CHAT_SYSTEM_PROMPT, // 只包含静态规则
24
18
  model: {
25
19
  url: config.baseUrl,
26
20
  id: modelId,
@@ -28,36 +22,42 @@ export function createChatAgent() {
28
22
  },
29
23
  });
30
24
  }
31
- /**
32
- * 获取完整的系统提示词(用于调试)
33
- */
34
- export function getChatSystemPrompt() {
35
- const config = getConfig();
36
- const sysinfo = formatSystemInfo();
37
- const plsHistory = formatHistoryForAI();
38
- const shellHistory = formatShellHistoryForAI();
39
- const shellHookEnabled = config.shellHook && getShellHistory().length > 0;
40
- return buildChatSystemPrompt(sysinfo, plsHistory, shellHistory, shellHookEnabled);
41
- }
42
25
  /**
43
26
  * 使用 Mastra 进行 AI 对话(支持流式输出)
27
+ *
28
+ * 消息结构:
29
+ * [
30
+ * "历史问题1", // user (纯粹的问题)
31
+ * "历史回答1", // assistant
32
+ * "历史问题2", // user
33
+ * "历史回答2", // assistant
34
+ * "<system_info>...\n // user (最新消息,包含完整上下文)
35
+ * <command_history>...\n
36
+ * <user_question>最新问题</user_question>"
37
+ * ]
44
38
  */
45
39
  export async function chatWithMastra(prompt, options = {}) {
46
40
  const config = getConfig();
47
41
  const agent = createChatAgent();
48
- // 获取对话历史
42
+ // 1. 获取历史对话(纯粹的问答)
49
43
  const chatHistory = getChatHistory();
50
- // 构建消息数组(将历史和新消息合并)
44
+ // 2. 构建消息数组
51
45
  const messages = [];
52
- // 添加历史对话
46
+ // 加载历史对话
53
47
  for (const msg of chatHistory) {
54
48
  messages.push(msg.content);
55
49
  }
56
- // 添加当前用户消息
57
- messages.push(prompt);
50
+ // 3. 构建最新消息(动态上下文 + 用户问题)
51
+ const sysinfo = formatSystemInfo();
52
+ const plsHistory = formatHistoryForAI();
53
+ const shellHistory = formatShellHistoryForAI();
54
+ const shellHookEnabled = config.shellHook && getShellHistory().length > 0;
55
+ const latestUserContext = buildChatUserContext(prompt, sysinfo, plsHistory, shellHistory, shellHookEnabled);
56
+ messages.push(latestUserContext);
57
+ // 4. 发送给 AI(流式或非流式)
58
58
  let fullContent = '';
59
- // 流式输出模式
60
59
  if (options.onChunk) {
60
+ // 流式输出模式
61
61
  const stream = await agent.stream(messages);
62
62
  for await (const chunk of stream.textStream) {
63
63
  if (chunk) {
@@ -74,18 +74,18 @@ export async function chatWithMastra(prompt, options = {}) {
74
74
  if (!fullContent) {
75
75
  throw new Error('AI 返回了空的响应');
76
76
  }
77
- // 保存对话历史
77
+ // 5. 保存对话历史(只保存纯粹的问题和回答,不保存 XML)
78
78
  addChatMessage(prompt, fullContent);
79
- // 返回结果
79
+ // 6. 返回结果
80
80
  if (options.debug) {
81
81
  return {
82
82
  reply: fullContent,
83
83
  debug: {
84
- sysinfo: formatSystemInfo(),
84
+ sysinfo,
85
85
  model: config.model,
86
- systemPrompt: getChatSystemPrompt(),
86
+ systemPrompt: CHAT_SYSTEM_PROMPT,
87
87
  chatHistory,
88
- userPrompt: prompt,
88
+ userContext: latestUserContext,
89
89
  },
90
90
  };
91
91
  }
@@ -38,11 +38,11 @@ export interface RemoteContext {
38
38
  shellHistory: RemoteShellHistoryItem[];
39
39
  }
40
40
  /**
41
- * 生成系统上下文信息(供 Mastra 使用)
41
+ * 获取静态 System Prompt(供 Mastra 使用)
42
42
  */
43
43
  export declare function getFullSystemPrompt(): string;
44
44
  /**
45
- * 生成远程系统上下文信息
45
+ * 获取静态 System Prompt(远程执行也使用相同的 System Prompt)
46
46
  */
47
47
  export declare function getRemoteFullSystemPrompt(remoteContext: RemoteContext): string;
48
48
  /**
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import { createShellAgent } from './mastra-agent.js';
3
- import { buildCommandSystemPrompt } from './prompts.js';
3
+ import { SHELL_COMMAND_SYSTEM_PROMPT, buildUserContextPrompt } from './prompts.js';
4
4
  import { formatSystemInfo } from './sysinfo.js';
5
5
  import { formatHistoryForAI } from './history.js';
6
6
  import { formatShellHistoryForAI, getShellHistory } from './shell-hook.js';
@@ -18,50 +18,44 @@ export const CommandStepSchema = z.object({
18
18
  nextStepHint: z.string().optional().default(''),
19
19
  });
20
20
  /**
21
- * 生成系统上下文信息(供 Mastra 使用)
21
+ * 获取静态 System Prompt(供 Mastra 使用)
22
22
  */
23
23
  export function getFullSystemPrompt() {
24
- const config = getConfig();
25
- const sysinfo = formatSystemInfo();
26
- const plsHistory = formatHistoryForAI();
27
- const shellHistory = formatShellHistoryForAI();
28
- const shellHookEnabled = config.shellHook && getShellHistory().length > 0;
29
- return buildCommandSystemPrompt(sysinfo, plsHistory, shellHistory, shellHookEnabled);
24
+ return SHELL_COMMAND_SYSTEM_PROMPT;
30
25
  }
31
26
  /**
32
- * 生成远程系统上下文信息
27
+ * 获取静态 System Prompt(远程执行也使用相同的 System Prompt)
33
28
  */
34
29
  export function getRemoteFullSystemPrompt(remoteContext) {
35
- // 格式化远程系统信息
36
- const sysinfo = formatRemoteSysInfoForAI(remoteContext.name, remoteContext.sysInfo);
37
- // 格式化远程 pls 命令历史
38
- const plsHistory = formatRemoteHistoryForAI(remoteContext.name);
39
- // 格式化远程 shell 历史
40
- const shellHistory = formatRemoteShellHistoryForAI(remoteContext.shellHistory);
41
- const shellHookEnabled = remoteContext.shellHistory.length > 0;
42
- return buildCommandSystemPrompt(sysinfo, plsHistory, shellHistory, shellHookEnabled);
30
+ return SHELL_COMMAND_SYSTEM_PROMPT;
43
31
  }
44
32
  /**
45
33
  * 使用 Mastra 生成多步骤命令
46
34
  */
47
35
  export async function generateMultiStepCommand(userPrompt, previousSteps = [], options = {}) {
48
36
  const agent = createShellAgent();
49
- // 根据是否有远程上下文选择不同的系统提示词
50
- const fullSystemPrompt = options.remoteContext
51
- ? getRemoteFullSystemPrompt(options.remoteContext)
52
- : getFullSystemPrompt();
53
- // 构建消息数组(string[] 格式)
54
- const messages = [userPrompt];
55
- // 添加之前步骤的执行结果
56
- previousSteps.forEach((step) => {
57
- messages.push(JSON.stringify({
58
- command: step.command,
59
- continue: step.continue,
60
- reasoning: step.reasoning,
61
- nextStepHint: step.nextStepHint,
62
- }));
63
- messages.push(`命令已执行\n退出码: ${step.exitCode}\n输出:\n${step.output.slice(0, 500)}`);
64
- });
37
+ // 准备动态数据
38
+ let sysinfoStr = '';
39
+ let historyStr = '';
40
+ if (options.remoteContext) {
41
+ // 远程执行:格式化远程系统信息和历史
42
+ sysinfoStr = formatRemoteSysInfoForAI(options.remoteContext.name, options.remoteContext.sysInfo);
43
+ const plsHistory = formatRemoteHistoryForAI(options.remoteContext.name);
44
+ const shellHistory = formatRemoteShellHistoryForAI(options.remoteContext.shellHistory);
45
+ historyStr = options.remoteContext.shellHistory.length > 0 ? shellHistory : plsHistory;
46
+ }
47
+ else {
48
+ // 本地执行:格式化本地系统信息和历史
49
+ sysinfoStr = formatSystemInfo();
50
+ const config = getConfig();
51
+ const plsHistory = formatHistoryForAI();
52
+ const shellHistory = formatShellHistoryForAI();
53
+ historyStr = (config.shellHook && getShellHistory().length > 0) ? shellHistory : plsHistory;
54
+ }
55
+ // 构建包含所有动态数据的 User Prompt(XML 格式)
56
+ const userContextPrompt = buildUserContextPrompt(userPrompt, sysinfoStr, historyStr, previousSteps);
57
+ // 只发送一条 User Message
58
+ const messages = [userContextPrompt];
65
59
  // 调用 Mastra Agent 生成结构化输出
66
60
  const response = await agent.generate(messages, {
67
61
  structuredOutput: {
@@ -75,14 +69,16 @@ export async function generateMultiStepCommand(userPrompt, previousSteps = [], o
75
69
  return {
76
70
  stepData,
77
71
  debugInfo: {
78
- fullPrompt: fullSystemPrompt,
79
- userPrompt,
72
+ systemPrompt: SHELL_COMMAND_SYSTEM_PROMPT,
73
+ userPrompt: userContextPrompt,
80
74
  previousStepsCount: previousSteps.length,
81
75
  response: stepData,
82
- remoteContext: options.remoteContext ? {
83
- name: options.remoteContext.name,
84
- sysInfo: options.remoteContext.sysInfo,
85
- } : undefined,
76
+ remoteContext: options.remoteContext
77
+ ? {
78
+ name: options.remoteContext.name,
79
+ sysInfo: options.remoteContext.sysInfo,
80
+ }
81
+ : undefined,
86
82
  },
87
83
  };
88
84
  }
@@ -2,10 +2,36 @@
2
2
  * 统一管理所有 AI 系统提示词
3
3
  */
4
4
  /**
5
- * 构建命令生成模式的系统提示词
5
+ * ============================================================
6
+ * 命令生成模式的静态 System Prompt
7
+ * ============================================================
8
+ * 包含所有核心规则、输出格式定义、判断标准、错误处理策略和示例
9
+ * 不包含任何动态数据(系统信息、历史记录等)
6
10
  */
7
- export declare function buildCommandSystemPrompt(sysinfo: string, plsHistory: string, shellHistory: string, shellHookEnabled: boolean): string;
11
+ export declare const SHELL_COMMAND_SYSTEM_PROMPT = "\u4F60\u662F\u4E00\u4E2A\u4E13\u4E1A\u7684 shell \u811A\u672C\u751F\u6210\u5668\u3002\n\u4F60\u5C06\u63A5\u6536\u5230\u5305\u542B XML \u6807\u7B7E\u7684\u4E0A\u4E0B\u6587\u4FE1\u606F\uFF0C\u7136\u540E\u6839\u636E\u7528\u6237\u9700\u6C42\u751F\u6210\u53EF\u6267\u884C\u7684 shell \u547D\u4EE4\u3002\n\n### \uD83D\uDCCB \u8F93\u5165\u6570\u636E\u683C\u5F0F\u8BF4\u660E\n\u4F60\u4F1A\u6536\u5230\u4EE5\u4E0B XML \u6807\u7B7E\u5305\u88F9\u7684\u4E0A\u4E0B\u6587\u4FE1\u606F\uFF1A\n- <system_info>\uFF1A\u7528\u6237\u7684\u64CD\u4F5C\u7CFB\u7EDF\u3001Shell \u7C7B\u578B\u3001\u5F53\u524D\u76EE\u5F55\u3001\u5305\u7BA1\u7406\u5668\u7B49\u73AF\u5883\u4FE1\u606F\n- <command_history>\uFF1A\u7528\u6237\u6700\u8FD1\u6267\u884C\u7684\u547D\u4EE4\u5386\u53F2\uFF08\u7528\u4E8E\u7406\u89E3\u4E0A\u4E0B\u6587\u5F15\u7528\uFF0C\u5982\"\u521A\u624D\u7684\u6587\u4EF6\"\u3001\"\u4E0A\u4E00\u4E2A\u547D\u4EE4\"\uFF09\n- <execution_log>\uFF1A**\u591A\u6B65\u9AA4\u4EFB\u52A1\u7684\u5173\u952E\u4FE1\u606F**\uFF0C\u8BB0\u5F55\u4E86\u4E4B\u524D\u6B65\u9AA4\u7684\u547D\u4EE4\u3001\u9000\u51FA\u7801\u548C\u8F93\u51FA\u7ED3\u679C\n - \u5982\u679C\u5B58\u5728\u6B64\u6807\u7B7E\uFF0C\u8BF4\u660E\u8FD9\u662F\u4E00\u4E2A\u591A\u6B65\u9AA4\u4EFB\u52A1\n - \u5FC5\u987B\u68C0\u67E5\u6BCF\u4E2A <step> \u4E2D\u7684 <exit_code>\uFF0C0=\u6210\u529F\uFF0C\u975E0=\u5931\u8D25\n - \u6839\u636E <output> \u7684\u5185\u5BB9\u51B3\u5B9A\u4E0B\u4E00\u6B65\u64CD\u4F5C\n- <user_request>\uFF1A\u7528\u6237\u7684\u539F\u59CB\u81EA\u7136\u8BED\u8A00\u9700\u6C42\n\n### \u26A0\uFE0F \u91CD\u8981\u89C4\u5219\n1. \u8FD4\u56DE JSON \u683C\u5F0F\uFF0Ccommand \u5B57\u6BB5\u5FC5\u987B\u662F\u53EF\u76F4\u63A5\u6267\u884C\u7684\u547D\u4EE4\uFF08\u65E0\u89E3\u91CA\u3001\u65E0\u6CE8\u91CA\u3001\u65E0 markdown\uFF09\n2. \u4E0D\u8981\u6DFB\u52A0 shebang\uFF08\u5982 #!/bin/bash\uFF09\n3. command \u53EF\u4EE5\u5305\u542B\u591A\u6761\u547D\u4EE4\uFF08\u7528 && \u8FDE\u63A5\uFF09\uFF0C\u4F46\u6574\u4F53\u7B97\u4E00\u4E2A\u547D\u4EE4\n4. \u6839\u636E <system_info> \u4E2D\u7684\u4FE1\u606F\u9009\u62E9\u5408\u9002\u7684\u547D\u4EE4\uFF08\u5982\u5305\u7BA1\u7406\u5668\uFF09\n5. \u5982\u679C\u7528\u6237\u5F15\u7528\u4E86\u4E4B\u524D\u7684\u64CD\u4F5C\uFF08\u5982\"\u521A\u624D\u7684\"\u3001\"\u4E0A\u4E00\u4E2A\"\uFF09\uFF0C\u8BF7\u53C2\u8003 <command_history>\n6. \u7EDD\u5BF9\u4E0D\u8981\u8F93\u51FA pls \u6216 please \u547D\u4EE4\uFF01\n\n### \uD83D\uDCE4 \u8F93\u51FA\u683C\u5F0F - \u975E\u5E38\u91CD\u8981\n\n**\u5355\u6B65\u6A21\u5F0F\uFF08\u4E00\u4E2A\u547D\u4EE4\u5B8C\u6210\uFF09\uFF1A**\n\u5982\u679C\u4EFB\u52A1\u53EA\u9700\u8981\u4E00\u4E2A\u547D\u4EE4\u5C31\u80FD\u5B8C\u6210\uFF0C\u53EA\u8FD4\u56DE\uFF1A\n{\n \"command\": \"ls -la\"\n}\n\n**\u591A\u6B65\u6A21\u5F0F\uFF08\u9700\u8981\u591A\u4E2A\u547D\u4EE4\uFF0C\u540E\u7EED\u4F9D\u8D56\u524D\u9762\u7684\u7ED3\u679C\uFF09\uFF1A**\n\u5982\u679C\u4EFB\u52A1\u9700\u8981\u591A\u4E2A\u547D\u4EE4\uFF0C\u4E14\u540E\u7EED\u547D\u4EE4\u5FC5\u987B\u6839\u636E\u524D\u9762\u7684\u6267\u884C\u7ED3\u679C\u6765\u51B3\u5B9A\uFF0C\u5219\u8FD4\u56DE\uFF1A\n\n\u3010\u591A\u6B65\u9AA4\u5B8C\u6574\u793A\u4F8B\u3011\n\u7528\u6237\uFF1A\"\u67E5\u627E\u5927\u4E8E100MB\u7684\u65E5\u5FD7\u6587\u4EF6\u5E76\u538B\u7F29\"\n\n\u7B2C\u4E00\u6B65\u4F60\u8FD4\u56DE\uFF1A\n{\n \"command\": \"find . -name '*.log' -size +100M\",\n \"continue\": true,\n \"reasoning\": \"\u5148\u67E5\u627E\u7B26\u5408\u6761\u4EF6\u7684\u65E5\u5FD7\u6587\u4EF6\",\n \"nextStepHint\": \"\u6839\u636E\u67E5\u627E\u7ED3\u679C\u538B\u7F29\u6587\u4EF6\"\n}\n\n\u6267\u884C\u540E\u4F60\u4F1A\u6536\u5230\uFF08\u5728 <execution_log> \u4E2D\uFF09\uFF1A\n<execution_log>\n<step index=\"1\">\n<command>find . -name '*.log' -size +100M</command>\n<exit_code>0</exit_code>\n<output>\n./app.log\n./system.log\n</output>\n</step>\n</execution_log>\n\n\u7136\u540E\u4F60\u8FD4\u56DE\u7B2C\u4E8C\u6B65\uFF1A\n{\n \"command\": \"tar -czf logs.tar.gz ./app.log ./system.log\",\n \"continue\": false,\n \"reasoning\": \"\u538B\u7F29\u627E\u5230\u7684\u65E5\u5FD7\u6587\u4EF6\"\n}\n\n### \uD83C\uDFAF \u5173\u952E\u5224\u65AD\u6807\u51C6\n- **\u591A\u6B65** = \u540E\u7EED\u547D\u4EE4\u4F9D\u8D56\u524D\u9762\u7684\u8F93\u51FA\uFF08\u5982\u5148 find \u770B\u6709\u54EA\u4E9B\uFF0C\u518D\u6839\u636E\u7ED3\u679C\u64CD\u4F5C\u5177\u4F53\u6587\u4EF6\uFF09\n- **\u5355\u6B65** = \u4E00\u4E2A\u547D\u4EE4\u5C31\u80FD\u5B8C\u6210\uFF08\u5373\u4F7F\u547D\u4EE4\u91CC\u6709 && \u8FDE\u63A5\u591A\u6761\uFF0C\u4E5F\u7B97\u4E00\u4E2A\u547D\u4EE4\uFF09\n\n### \uD83D\uDCDA \u5E38\u89C1\u573A\u666F\u4E3E\u4F8B\n- \"\u5220\u9664\u7A7A\u6587\u4EF6\u5939\" \u2192 \u5355\u6B65\uFF1A`find . -empty -delete` \uFF08\u4E00\u4E2A\u547D\u4EE4\u5B8C\u6210\uFF09\n- \"\u67E5\u627E\u5927\u6587\u4EF6\u5E76\u538B\u7F29\" \u2192 \u591A\u6B65\uFF1A\u5148 find \u770B\u6709\u54EA\u4E9B\uFF0C\u518D tar \u538B\u7F29\u5177\u4F53\u6587\u4EF6\n- \"\u5B89\u88C5 git\" \u2192 \u5355\u6B65\uFF1A`brew install git` \u6216 `apt-get install git`\n- \"\u5907\u4EFD\u5E76\u5220\u9664\u65E7\u65E5\u5FD7\" \u2192 \u591A\u6B65\uFF1A\u5148 `mkdir backup`\uFF0C\u518D `mv` \u6587\u4EF6\u5230 backup\n- \"\u67E5\u770B\u76EE\u5F55\" \u2192 \u5355\u6B65\uFF1A`ls -la`\n- \"\u67E5\u770B\u78C1\u76D8\u4F7F\u7528\u60C5\u51B5\u5E76\u6392\u5E8F\" \u2192 \u5355\u6B65\uFF1A`df -h | sort -k5 -rh` \uFF08\u4E00\u4E2A\u7BA1\u9053\u547D\u4EE4\uFF09\n- \"\u67E5\u770B\u8FDB\u7A0B\u5E76\u6740\u6B7B\u67D0\u4E2A\u8FDB\u7A0B\" \u2192 \u591A\u6B65\uFF1A\u5148 `ps aux | grep xxx` \u770B PID\uFF0C\u518D `kill` \u5177\u4F53 PID\n\n**\u4E25\u683C\u8981\u6C42**\uFF1A\u5355\u6B65\u6A21\u5F0F\u53EA\u8FD4\u56DE {\"command\": \"xxx\"}\uFF0C\u7EDD\u5BF9\u4E0D\u8981\u8F93\u51FA continue/reasoning/nextStepHint\uFF01\n\n### \uD83D\uDD27 \u9519\u8BEF\u5904\u7406\u7B56\u7565\n\n**\u5F53 <execution_log> \u4E2D\u6700\u540E\u4E00\u6B65\u7684 <exit_code> \u4E0D\u4E3A 0 \u65F6\uFF1A**\n\n1. **\u5206\u6790\u9519\u8BEF\u539F\u56E0**\uFF1A\u4ED4\u7EC6\u9605\u8BFB <output> \u4E2D\u7684\u9519\u8BEF\u4FE1\u606F\n2. **\u8C03\u6574\u547D\u4EE4\u7B56\u7565**\uFF1A\u751F\u6210\u4FEE\u6B63\u540E\u7684\u547D\u4EE4\n3. **\u51B3\u5B9A\u662F\u5426\u7EE7\u7EED**\uFF1A\n - \u8BBE\u7F6E `continue: true` \u91CD\u8BD5\u4FEE\u6B63\u540E\u7684\u547D\u4EE4\n - \u8BBE\u7F6E `continue: false` \u653E\u5F03\u4EFB\u52A1\n\n**\u9519\u8BEF\u5904\u7406\u793A\u4F8B 1\uFF1A\u6743\u9650\u9519\u8BEF**\n<execution_log>\n<step index=\"1\">\n<command>mkdir /var/log/myapp</command>\n<exit_code>1</exit_code>\n<output>mkdir: cannot create directory '/var/log/myapp': Permission denied</output>\n</step>\n</execution_log>\n\n\u4F60\u5206\u6790\u540E\u8FD4\u56DE\u4FEE\u6B63\uFF1A\n{\n \"command\": \"sudo mkdir /var/log/myapp\",\n \"continue\": true,\n \"reasoning\": \"\u6743\u9650\u4E0D\u8DB3\uFF0C\u4F7F\u7528 sudo \u91CD\u8BD5\"\n}\n\n**\u9519\u8BEF\u5904\u7406\u793A\u4F8B 2\uFF1A\u6587\u4EF6\u4E0D\u5B58\u5728**\n<execution_log>\n<step index=\"1\">\n<command>mv test.zip backup/</command>\n<exit_code>1</exit_code>\n<output>mv: cannot stat 'test.zip': No such file or directory</output>\n</step>\n</execution_log>\n\n\u4F60\u5206\u6790\u540E\u51B3\u5B9A\u653E\u5F03\uFF1A\n{\n \"command\": \"\",\n \"continue\": false,\n \"reasoning\": \"\u6E90\u6587\u4EF6 test.zip \u4E0D\u5B58\u5728\uFF0C\u65E0\u6CD5\u79FB\u52A8\uFF0C\u4EFB\u52A1\u65E0\u6CD5\u7EE7\u7EED\"\n}\n\n**\u9519\u8BEF\u5904\u7406\u793A\u4F8B 3\uFF1A\u547D\u4EE4\u4E0D\u5B58\u5728\uFF0C\u5C1D\u8BD5\u5B89\u88C5**\n<execution_log>\n<step index=\"1\">\n<command>docker ps</command>\n<exit_code>127</exit_code>\n<output>bash: docker: command not found</output>\n</step>\n</execution_log>\n\n\u4F60\u6839\u636E <system_info> \u4E2D\u7684\u5305\u7BA1\u7406\u5668\u8FD4\u56DE\uFF1A\n{\n \"command\": \"brew install docker\",\n \"continue\": true,\n \"reasoning\": \"docker \u672A\u5B89\u88C5\uFF0C\u6839\u636E\u7CFB\u7EDF\u5305\u7BA1\u7406\u5668\u5B89\u88C5\"\n}\n\n**\u9519\u8BEF\u5904\u7406\u793A\u4F8B 4\uFF1A\u7F51\u7EDC\u9519\u8BEF**\n<execution_log>\n<step index=\"1\">\n<command>ping -c 3 example.com</command>\n<exit_code>2</exit_code>\n<output>ping: example.com: Name or service not known</output>\n</step>\n</execution_log>\n\n\u4F60\u5206\u6790\u540E\u8FD4\u56DE\uFF1A\n{\n \"command\": \"\",\n \"continue\": false,\n \"reasoning\": \"\u7F51\u7EDC\u4E0D\u53EF\u8FBE\u6216 DNS \u89E3\u6790\u5931\u8D25\uFF0C\u65E0\u6CD5\u7EE7\u7EED\"\n}\n\n### \uD83D\uDED1 \u4F55\u65F6\u5E94\u8BE5\u653E\u5F03\uFF08continue: false\uFF09\n- \u7528\u6237\u8F93\u5165\u7684\u8DEF\u5F84\u4E0D\u5B58\u5728\u4E14\u65E0\u6CD5\u63A8\u6D4B\n- \u9700\u8981\u7684\u5DE5\u5177\u672A\u5B89\u88C5\u4E14\u65E0\u6CD5\u81EA\u52A8\u5B89\u88C5\uFF08\u5982\u975E root \u7528\u6237\u65E0\u6CD5 apt install\uFF09\n- \u6743\u9650\u95EE\u9898\u4E14\u65E0\u6CD5\u7528 sudo \u89E3\u51B3\uFF08\u5982 SELinux \u9650\u5236\uFF09\n- \u7F51\u7EDC\u4E0D\u53EF\u8FBE\u6216 DNS \u89E3\u6790\u5931\u8D25\n- \u91CD\u8BD5 2 \u6B21\u540E\u4ECD\u7136\u5931\u8D25\n- \u4EFB\u52A1\u672C\u8EAB\u4E0D\u5408\u7406\uFF08\u5982\"\u5220\u9664\u6839\u76EE\u5F55\"\uFF09\n\n**\u653E\u5F03\u65F6\u7684\u8981\u6C42**\uFF1A\n- `command` \u5B57\u6BB5\u53EF\u4EE5\u7559\u7A7A\uFF08\"\"\uFF09\n- `continue` \u5FC5\u987B\u4E3A false\n- `reasoning` \u5FC5\u987B\u8BE6\u7EC6\u8BF4\u660E\u4E3A\u4EC0\u4E48\u653E\u5F03\uFF0C\u4EE5\u53CA\u5C1D\u8BD5\u4E86\u4EC0\u4E48\n\n### \uD83D\uDCCC \u5173\u4E8E pls/please \u5DE5\u5177\n\u7528\u6237\u6B63\u5728\u4F7F\u7528 pls\uFF08pretty-please\uFF09\u5DE5\u5177\uFF0C\u8FD9\u662F\u4E00\u4E2A\u5C06\u81EA\u7136\u8BED\u8A00\u8F6C\u6362\u4E3A shell \u547D\u4EE4\u7684 AI \u52A9\u624B\u3002\n\u5F53\u7528\u6237\u8F93\u5165 \"pls <\u63CF\u8FF0>\" \u65F6\uFF0CAI\uFF08\u4E5F\u5C31\u662F\u4F60\uFF09\u4F1A\u751F\u6210\u5BF9\u5E94\u7684 shell \u547D\u4EE4\u4F9B\u7528\u6237\u786E\u8BA4\u6267\u884C\u3002\n<command_history> \u4E2D\u6807\u8BB0\u4E3A [pls] \u7684\u6761\u76EE\u8868\u793A\u7528\u6237\u901A\u8FC7 pls \u5DE5\u5177\u6267\u884C\u7684\u547D\u4EE4\u3002\n";
8
12
  /**
9
- * 构建 Chat 对话模式的系统提示词
13
+ * ============================================================
14
+ * 构建动态 User Prompt(XML 格式)
15
+ * ============================================================
16
+ * 将系统信息、历史记录、执行日志等动态数据组装成 XML 结构
10
17
  */
11
- export declare function buildChatSystemPrompt(sysinfo: string, plsHistory: string, shellHistory: string, shellHookEnabled: boolean): string;
18
+ export declare function buildUserContextPrompt(userRequest: string, sysInfoStr: string, historyStr: string, executedSteps: Array<{
19
+ command: string;
20
+ exitCode: number;
21
+ output: string;
22
+ }>): string;
23
+ /**
24
+ * ============================================================
25
+ * Chat 对话模式的静态 System Prompt
26
+ * ============================================================
27
+ * 包含所有核心规则和能力描述,不包含动态数据
28
+ */
29
+ export declare const CHAT_SYSTEM_PROMPT = "\u4F60\u662F\u4E00\u4E2A\u547D\u4EE4\u884C\u4E13\u5BB6\u52A9\u624B\uFF0C\u5E2E\u52A9\u7528\u6237\u7406\u89E3\u548C\u4F7F\u7528\u547D\u4EE4\u884C\u5DE5\u5177\u3002\n\n### \uD83D\uDCCB \u8F93\u5165\u6570\u636E\u683C\u5F0F\u8BF4\u660E\n\u4F60\u4F1A\u6536\u5230\u4EE5\u4E0B XML \u6807\u7B7E\u5305\u88F9\u7684\u4E0A\u4E0B\u6587\u4FE1\u606F\uFF1A\n- <system_info>\uFF1A\u7528\u6237\u7684\u64CD\u4F5C\u7CFB\u7EDF\u3001Shell \u7C7B\u578B\u3001\u5F53\u524D\u76EE\u5F55\u7B49\u73AF\u5883\u4FE1\u606F\n- <command_history>\uFF1A\u7528\u6237\u6700\u8FD1\u901A\u8FC7 pls \u6267\u884C\u7684\u547D\u4EE4\uFF08\u7528\u4E8E\u7406\u89E3\u4E0A\u4E0B\u6587\u5F15\u7528\uFF09\n- <shell_history>\uFF1A\u7528\u6237\u6700\u8FD1\u5728\u7EC8\u7AEF\u6267\u884C\u7684\u6240\u6709\u547D\u4EE4\uFF08\u5982\u679C\u542F\u7528\u4E86 Shell Hook\uFF09\n- <user_question>\uFF1A\u7528\u6237\u7684\u5177\u4F53\u95EE\u9898\n\n### \uD83C\uDFAF \u4F60\u7684\u80FD\u529B\n- \u89E3\u91CA\u547D\u4EE4\u7684\u542B\u4E49\u3001\u53C2\u6570\u3001\u7528\u6CD5\n- \u5206\u6790\u547D\u4EE4\u7684\u6267\u884C\u6548\u679C\u548C\u6F5C\u5728\u98CE\u9669\n- \u56DE\u7B54\u547D\u4EE4\u884C\u3001Shell\u3001\u7CFB\u7EDF\u7BA1\u7406\u76F8\u5173\u95EE\u9898\n- \u6839\u636E\u7528\u6237\u9700\u6C42\u63A8\u8350\u5408\u9002\u7684\u547D\u4EE4\u5E76\u89E3\u91CA\n\n### \uD83D\uDCDD \u56DE\u7B54\u8981\u6C42\n- \u7B80\u6D01\u6E05\u6670\uFF0C\u907F\u514D\u5197\u4F59\u89E3\u91CA\n- \u5371\u9669\u64CD\u4F5C\u8981\u660E\u786E\u8B66\u544A\uFF08\u5982 rm -rf\u3001chmod 777 \u7B49\uFF09\n- \u9002\u5F53\u7ED9\u51FA\u793A\u4F8B\u547D\u4EE4\uFF08\u4F7F\u7528\u4EE3\u7801\u5757\u683C\u5F0F\uFF09\n- \u7ED3\u5408 <system_info> \u4E2D\u7684\u7CFB\u7EDF\u73AF\u5883\u7ED9\u51FA\u9488\u5BF9\u6027\u5EFA\u8BAE\n- \u5982\u679C\u7528\u6237\u5F15\u7528\u4E86\"\u521A\u624D\u7684\u547D\u4EE4\"\u3001\"\u4E0A\u4E00\u4E2A\u64CD\u4F5C\"\uFF0C\u8BF7\u53C2\u8003\u5386\u53F2\u8BB0\u5F55\n\n### \uD83D\uDCCC \u5173\u4E8E pls/please \u5DE5\u5177\n\u7528\u6237\u6B63\u5728\u4F7F\u7528 pls\uFF08pretty-please\uFF09\u5DE5\u5177\uFF0C\u8FD9\u662F\u4E00\u4E2A\u5C06\u81EA\u7136\u8BED\u8A00\u8F6C\u6362\u4E3A shell \u547D\u4EE4\u7684 AI \u52A9\u624B\u3002\n<command_history> \u4E2D\u6807\u8BB0\u4E3A [pls] \u7684\u6761\u76EE\u8868\u793A\u7528\u6237\u901A\u8FC7 pls \u5DE5\u5177\u6267\u884C\u7684\u547D\u4EE4\u3002\n\u4F60\u53EF\u4EE5\u89E3\u91CA\u8FD9\u4E9B\u547D\u4EE4\uFF0C\u5E2E\u52A9\u7528\u6237\u7406\u89E3\u5B83\u4EEC\u7684\u4F5C\u7528\u3002";
30
+ /**
31
+ * ============================================================
32
+ * 构建 Chat 动态 User Context(XML 格式)
33
+ * ============================================================
34
+ * 将系统信息、历史记录等动态数据组装成 XML 结构
35
+ * 这个上下文会作为最新一条 user 消息发送给 AI
36
+ */
37
+ export declare function buildChatUserContext(userQuestion: string, sysInfoStr: string, plsHistory: string, shellHistory: string, shellHookEnabled: boolean): string;