feat(core): 实现动态提示词模板系统

- 新增 prompt-template 模块,支持运行时变量替换
- 支持 ${variable}、${obj.prop}、${cond ? "a" : "b"} 语法
- AgentInfo 新增 promptTemplate 字段标记动态模板
- Plan Agent 提示词改用模板语法,支持动态工具名和计划文件路径
- AgentExecutor.buildSystemPrompt 集成模板渲染
- 新增 27 个单元测试验证模板功能
This commit is contained in:
2025-12-16 14:15:10 +08:00
parent a32c83480d
commit 58f1bc8718
10 changed files with 968 additions and 13 deletions
+11 -1
View File
@@ -17,6 +17,7 @@ import type {
} from './types.js';
import { checkBashPermission, isPathInAllowedWritePaths } from './permission-merger.js';
import { getProviderRegistry } from '../provider/index.js';
import { renderTemplate, createPlanContext } from './prompt-template/index.js';
/**
* Agent 执行器
@@ -286,10 +287,19 @@ export class AgentExecutor {
/**
* 构建系统提示词
* 如果 Agent 启用了 promptTemplate,则动态渲染模板变量
*/
private buildSystemPrompt(): string {
// 如果 Agent 有自定义 prompt,使用它
// 如果 Agent 有自定义 prompt
if (this.agentInfo.prompt) {
// 如果启用了模板渲染,动态解析变量
if (this.agentInfo.promptTemplate) {
const context = createPlanContext({
workdir: process.cwd(),
isSubagent: this.agentInfo.mode === 'subagent',
});
return renderTemplate(this.agentInfo.prompt, context);
}
return this.agentInfo.prompt;
}