yuangs 3.16.0 → 3.17.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.
|
@@ -17,6 +17,7 @@ const governance_1 = require("./governance");
|
|
|
17
17
|
const executor_1 = require("./executor");
|
|
18
18
|
const contextManager_1 = require("./contextManager");
|
|
19
19
|
const core_1 = require("./governance/core");
|
|
20
|
+
const dynamicPrompt_1 = require("./dynamicPrompt");
|
|
20
21
|
class AgentRuntime {
|
|
21
22
|
context;
|
|
22
23
|
executionId;
|
|
@@ -27,6 +28,9 @@ class AgentRuntime {
|
|
|
27
28
|
async run(userInput, mode = "chat", onChunk, model) {
|
|
28
29
|
let turnCount = 0;
|
|
29
30
|
const maxTurns = 10;
|
|
31
|
+
let lastError;
|
|
32
|
+
// 构建初始动态上下文
|
|
33
|
+
const initialDynamicContext = await (0, dynamicPrompt_1.buildDynamicContext)();
|
|
30
34
|
if (userInput) {
|
|
31
35
|
this.context.addMessage("user", userInput);
|
|
32
36
|
}
|
|
@@ -35,11 +39,17 @@ class AgentRuntime {
|
|
|
35
39
|
if (currentTurn > 1) {
|
|
36
40
|
console.log(chalk_1.default.blue(`\n--- Turn ${currentTurn} ---`));
|
|
37
41
|
}
|
|
42
|
+
// 构建动态上下文(如果上一步有错误)
|
|
43
|
+
const dynamicContext = await (0, dynamicPrompt_1.buildDynamicContext)(lastError);
|
|
38
44
|
const messages = this.context.getMessages().map((msg) => ({
|
|
39
45
|
role: (msg.role === "tool" ? "system" : msg.role),
|
|
40
46
|
content: msg.content,
|
|
41
47
|
}));
|
|
42
|
-
|
|
48
|
+
// 构建基础prompt(包括治理策略)
|
|
49
|
+
const basePrompt = governance_1.GovernanceService.getPolicyManual();
|
|
50
|
+
// 注入动态上下文
|
|
51
|
+
const enhancedPrompt = (0, dynamicPrompt_1.injectDynamicContext)(basePrompt, dynamicContext);
|
|
52
|
+
const thought = await llmAdapter_1.LLMAdapter.think(messages, mode, onChunk, model, enhancedPrompt);
|
|
43
53
|
const action = {
|
|
44
54
|
id: (0, crypto_1.randomUUID)(),
|
|
45
55
|
type: thought.type || "answer",
|
|
@@ -78,6 +88,8 @@ class AgentRuntime {
|
|
|
78
88
|
console.log(chalk_1.default.yellow(`[EXECUTING] ⚙️ ${action.type}...`));
|
|
79
89
|
const result = await executor_1.ToolExecutor.execute(action);
|
|
80
90
|
if (result.success) {
|
|
91
|
+
// 成功时清除错误状态
|
|
92
|
+
lastError = undefined;
|
|
81
93
|
this.context.addToolResult(action.type, result.output);
|
|
82
94
|
const preview = result.output.length > 300
|
|
83
95
|
? result.output.substring(0, 300) + '...'
|
|
@@ -85,6 +97,8 @@ class AgentRuntime {
|
|
|
85
97
|
console.log(chalk_1.default.green(`[SUCCESS] Result:\n${preview}`));
|
|
86
98
|
}
|
|
87
99
|
else {
|
|
100
|
+
// 失败时记录错误,下次循环会注入错误恢复指导
|
|
101
|
+
lastError = result.error;
|
|
88
102
|
this.context.addToolResult(action.type, `Error: ${result.error}`);
|
|
89
103
|
console.log(chalk_1.default.red(`[ERROR] ${result.error}`));
|
|
90
104
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AgentRuntime.js","sourceRoot":"","sources":["../../src/agent/AgentRuntime.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAC1B,mCAAoC;AACpC,mCAAgC;AAChC,sEAA+C;AAE/C,mBAAmB;AACnB,eAAM,CAAC,UAAU,CAAC;IAChB,QAAQ,EAAE,IAAI,yBAAgB,EAAE;CACjC,CAAC,CAAC;AACH,6CAA0C;AAC1C,6CAAiD;AACjD,yCAA0C;AAC1C,qDAAkD;AAClD,4CAAqD;
|
|
1
|
+
{"version":3,"file":"AgentRuntime.js","sourceRoot":"","sources":["../../src/agent/AgentRuntime.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAC1B,mCAAoC;AACpC,mCAAgC;AAChC,sEAA+C;AAE/C,mBAAmB;AACnB,eAAM,CAAC,UAAU,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AAC,aAAa,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;gBACvD,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,GAAG;oBACxC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,GAAG,KAAK;oBACzC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC;gBAClB,OAAO,CAAC,GAAG,CAAC,eAAK,CAAC,KAAK,CAAC,sBAAsB,OAAO,EAAE,CAAC,CAAC,CAAC;YAC5D,CAAC;iBAAM,CAAC;gBACN,wBAAwB;gBACxB,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC;gBACzB,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,EAAE,UAAU,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;gBAClE,OAAO,CAAC,GAAG,CAAC,eAAK,CAAC,GAAG,CAAC,WAAW,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;YACpD,CAAC;QACH,CAAC;QAED,IAAI,SAAS,IAAI,QAAQ,EAAE,CAAC;YAC1B,OAAO,CAAC,GAAG,CAAC,eAAK,CAAC,GAAG,CAAC,mBAAmB,QAAQ,YAAY,CAAC,CAAC,CAAC;QAClE,CAAC;IACH,CAAC;CACF;AApID,oCAoIC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 动态Prompt生成器
|
|
3
|
+
* 根据运行时状态动态注入Prompt片段
|
|
4
|
+
*/
|
|
5
|
+
export interface DynamicContext {
|
|
6
|
+
gitContext?: string;
|
|
7
|
+
techStack?: string[];
|
|
8
|
+
lastError?: string;
|
|
9
|
+
errorRecovery?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 检测Git上下文(增强版,支持子目录检测)
|
|
13
|
+
*/
|
|
14
|
+
export declare function detectGitContext(): Promise<string | null>;
|
|
15
|
+
/**
|
|
16
|
+
* 检测技术栈(使用Promise.all并发检测,提升性能)
|
|
17
|
+
*/
|
|
18
|
+
export declare function detectTechStack(): Promise<string[]>;
|
|
19
|
+
/**
|
|
20
|
+
* 生成技术栈指导
|
|
21
|
+
*/
|
|
22
|
+
export declare function generateTechStackGuidance(stacks: string[]): string;
|
|
23
|
+
/**
|
|
24
|
+
* 生成错误恢复指导
|
|
25
|
+
*/
|
|
26
|
+
export declare function generateErrorRecovery(lastError: string): string;
|
|
27
|
+
/**
|
|
28
|
+
* 构建动态上下文
|
|
29
|
+
*/
|
|
30
|
+
export declare function buildDynamicContext(lastError?: string, includeTechStack?: boolean): Promise<DynamicContext>;
|
|
31
|
+
/**
|
|
32
|
+
* 将动态上下文注入到Prompt
|
|
33
|
+
*/
|
|
34
|
+
export declare function injectDynamicContext(basePrompt: string, context: DynamicContext): string;
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.detectGitContext = detectGitContext;
|
|
37
|
+
exports.detectTechStack = detectTechStack;
|
|
38
|
+
exports.generateTechStackGuidance = generateTechStackGuidance;
|
|
39
|
+
exports.generateErrorRecovery = generateErrorRecovery;
|
|
40
|
+
exports.buildDynamicContext = buildDynamicContext;
|
|
41
|
+
exports.injectDynamicContext = injectDynamicContext;
|
|
42
|
+
const fs = __importStar(require("fs/promises"));
|
|
43
|
+
const child_process_1 = require("child_process");
|
|
44
|
+
const util_1 = require("util");
|
|
45
|
+
const execAsync = (0, util_1.promisify)(child_process_1.exec);
|
|
46
|
+
/**
|
|
47
|
+
* 缓存检测结果,避免重复IO操作
|
|
48
|
+
*/
|
|
49
|
+
let cachedGitContext = null;
|
|
50
|
+
let cachedTechStack = null;
|
|
51
|
+
let lastCheckTimestamp = 0;
|
|
52
|
+
const CACHE_TTL = 5000; // 5秒缓存
|
|
53
|
+
/**
|
|
54
|
+
* 检测Git上下文(增强版,支持子目录检测)
|
|
55
|
+
*/
|
|
56
|
+
async function detectGitContext() {
|
|
57
|
+
// 使用git命令检测,支持在项目子目录中运行
|
|
58
|
+
try {
|
|
59
|
+
const { stdout } = await execAsync('git rev-parse --is-inside-work-tree', {
|
|
60
|
+
cwd: process.cwd(),
|
|
61
|
+
timeout: 2000
|
|
62
|
+
});
|
|
63
|
+
if (stdout.trim() === 'true') {
|
|
64
|
+
return `
|
|
65
|
+
[GIT CONTEXT]
|
|
66
|
+
当前目录在一个Git仓库中。
|
|
67
|
+
- 优先使用 \`git ls-files\` 列出文件(遵守.gitignore)
|
|
68
|
+
- 使用 \`git diff\` 查看未提交的更改
|
|
69
|
+
- 使用 \`git log\` 查看最近历史
|
|
70
|
+
- 谨慎操作版本控制文件
|
|
71
|
+
`;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
// git命令失败,回退到文件系统检测
|
|
76
|
+
try {
|
|
77
|
+
await fs.access('.git');
|
|
78
|
+
return `
|
|
79
|
+
[GIT CONTEXT]
|
|
80
|
+
当前目录是一个Git仓库。
|
|
81
|
+
- 优先使用 \`git ls-files\` 列出文件(遵守.gitignore)
|
|
82
|
+
- 使用 \`git diff\` 查看未提交的更改
|
|
83
|
+
- 使用 \`git log\` 查看最近历史
|
|
84
|
+
- 谨慎操作版本控制文件
|
|
85
|
+
`;
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 检测技术栈(使用Promise.all并发检测,提升性能)
|
|
95
|
+
*/
|
|
96
|
+
async function detectTechStack() {
|
|
97
|
+
const filesToCheck = [
|
|
98
|
+
{ file: 'package.json', stack: 'Node.js' },
|
|
99
|
+
{ file: 'Cargo.toml', stack: 'Rust' },
|
|
100
|
+
{ file: 'go.mod', stack: 'Go' },
|
|
101
|
+
{ file: 'requirements.txt', stack: 'Python' },
|
|
102
|
+
{ file: 'pom.xml', stack: 'Java/Maven' },
|
|
103
|
+
{ file: 'build.gradle', stack: 'Java/Gradle' },
|
|
104
|
+
{ file: 'Gemfile', stack: 'Ruby' },
|
|
105
|
+
{ file: 'composer.json', stack: 'PHP' },
|
|
106
|
+
{ file: 'Dockerfile', stack: 'Docker' },
|
|
107
|
+
];
|
|
108
|
+
// 并发检测所有文件,提升性能
|
|
109
|
+
const results = await Promise.all(filesToCheck.map(async ({ file, stack }) => {
|
|
110
|
+
try {
|
|
111
|
+
await fs.access(file);
|
|
112
|
+
return stack;
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
}));
|
|
118
|
+
// 过滤掉null值并去重
|
|
119
|
+
return results.filter((stack) => stack !== null);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 生成技术栈指导
|
|
123
|
+
*/
|
|
124
|
+
function generateTechStackGuidance(stacks) {
|
|
125
|
+
if (stacks.length === 0) {
|
|
126
|
+
return '';
|
|
127
|
+
}
|
|
128
|
+
const guidance = [];
|
|
129
|
+
if (stacks.includes('Node.js')) {
|
|
130
|
+
guidance.push(`
|
|
131
|
+
[TECH STACK: Node.js]
|
|
132
|
+
- 使用 \`npm\` 或 \`yarn\` 进行包管理
|
|
133
|
+
- 检查 package.json 可用的脚本命令
|
|
134
|
+
- 生成代码时使用TypeScript严格模式
|
|
135
|
+
- 使用ESLint和Prettier进行代码格式化`);
|
|
136
|
+
}
|
|
137
|
+
if (stacks.includes('Python')) {
|
|
138
|
+
guidance.push(`
|
|
139
|
+
[TECH STACK: Python]
|
|
140
|
+
- 使用 \`pip\` 或 \`poetry\` 进行包管理
|
|
141
|
+
- 检查 requirements.txt 或 pyproject.toml
|
|
142
|
+
- 遵循PEP 8代码风格指南
|
|
143
|
+
- 使用虚拟环境隔离依赖`);
|
|
144
|
+
}
|
|
145
|
+
if (stacks.includes('Go')) {
|
|
146
|
+
guidance.push(`
|
|
147
|
+
[TECH STACK: Go]
|
|
148
|
+
- 使用 \`go mod\` 进行模块管理
|
|
149
|
+
- 检查 go.mod 文件了解依赖
|
|
150
|
+
- 遵循Go惯用模式和错误处理
|
|
151
|
+
- 使用 \`go test\` 运行测试`);
|
|
152
|
+
}
|
|
153
|
+
if (stacks.includes('Rust')) {
|
|
154
|
+
guidance.push(`
|
|
155
|
+
[TECH STACK: Rust]
|
|
156
|
+
- 使用 \`cargo\` 进行包管理
|
|
157
|
+
- 检查 Cargo.toml 了解依赖
|
|
158
|
+
- 遵循Rust所有权和借用规则
|
|
159
|
+
- 使用 \`cargo clippy\` 进行代码检查`);
|
|
160
|
+
}
|
|
161
|
+
if (stacks.includes('Docker')) {
|
|
162
|
+
guidance.push(`
|
|
163
|
+
[TECH STACK: Docker]
|
|
164
|
+
- 检查 Dockerfile 和 docker-compose.yml
|
|
165
|
+
- 容器化运行和测试命令
|
|
166
|
+
- 注意多阶段构建优化
|
|
167
|
+
- 管理容器网络和卷`);
|
|
168
|
+
}
|
|
169
|
+
return guidance.join('\n');
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* 生成错误恢复指导
|
|
173
|
+
*/
|
|
174
|
+
function generateErrorRecovery(lastError) {
|
|
175
|
+
return `
|
|
176
|
+
[ERROR RECOVERY]
|
|
177
|
+
上一步操作失败: ${lastError}
|
|
178
|
+
你必须尝试不同的方法或验证前置条件。
|
|
179
|
+
|
|
180
|
+
考虑以下选项:
|
|
181
|
+
- 检查命令语法是否正确
|
|
182
|
+
- 验证文件/路径是否存在
|
|
183
|
+
- 使用不同的标志或工具
|
|
184
|
+
- 检查依赖是否已安装
|
|
185
|
+
- 查看错误日志获取更多信息
|
|
186
|
+
|
|
187
|
+
如果仍然失败,切换到 "answer" 模式向用户说明问题`;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* 构建动态上下文
|
|
191
|
+
*/
|
|
192
|
+
async function buildDynamicContext(lastError, includeTechStack = true) {
|
|
193
|
+
const context = {};
|
|
194
|
+
// 检测Git上下文
|
|
195
|
+
const gitContext = await detectGitContext();
|
|
196
|
+
if (gitContext) {
|
|
197
|
+
context.gitContext = gitContext;
|
|
198
|
+
}
|
|
199
|
+
// 检测技术栈
|
|
200
|
+
if (includeTechStack) {
|
|
201
|
+
const techStack = await detectTechStack();
|
|
202
|
+
if (techStack.length > 0) {
|
|
203
|
+
context.techStack = techStack;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
// 错误恢复
|
|
207
|
+
if (lastError) {
|
|
208
|
+
context.lastError = lastError;
|
|
209
|
+
context.errorRecovery = generateErrorRecovery(lastError);
|
|
210
|
+
}
|
|
211
|
+
return context;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* 将动态上下文注入到Prompt
|
|
215
|
+
*/
|
|
216
|
+
function injectDynamicContext(basePrompt, context) {
|
|
217
|
+
let prompt = basePrompt;
|
|
218
|
+
// 注入Git上下文
|
|
219
|
+
if (context.gitContext) {
|
|
220
|
+
prompt += `\n${context.gitContext}`;
|
|
221
|
+
}
|
|
222
|
+
// 注入技术栈指导
|
|
223
|
+
if (context.techStack && context.techStack.length > 0) {
|
|
224
|
+
const guidance = generateTechStackGuidance(context.techStack);
|
|
225
|
+
prompt += `\n${guidance}`;
|
|
226
|
+
}
|
|
227
|
+
// 注入错误恢复
|
|
228
|
+
if (context.errorRecovery) {
|
|
229
|
+
prompt += `\n${context.errorRecovery}`;
|
|
230
|
+
}
|
|
231
|
+
return prompt;
|
|
232
|
+
}
|
|
233
|
+
//# sourceMappingURL=dynamicPrompt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dynamicPrompt.js","sourceRoot":"","sources":["../../src/agent/dynamicPrompt.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,4CAoCC;AAKD,0CA2BC;AAKD,8DAqDC;AAKD,sDAcC;AAKD,kDA2BC;AAKD,oDAuBC;AA3OD,gDAAkC;AAElC,iDAAqC;AACrC,+BAAiC;AAEjC,MAAM,SAAS,GAAG,IAAA,gBAAS,EAAC,oBAAI,CAAC,CAAC;AAElC;;GAEG;AACH,IAAI,gBAAgB,GAAkB,IAAI,CA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