Aster 的 Prompt Builder 系统提供了一个模块化、可扩展的方式来构建 Agent 的 System Prompt。它借鉴了 Claude Code 的设计理念,支持动态组合多个 Prompt 模块,提供 14+ 预定义模块和 8 个预设模板。
type PromptModule interface {
Name() string // 模块名称
Build(ctx *PromptContext) (string, error) // 构建模块内容
Priority() int // 优先级(决定注入顺序,0-100)
Condition(ctx *PromptContext) bool // 是否应该注入此模块
}
构建上下文,包含构建 Prompt 所需的所有信息:
type PromptContext struct {
Agent *Agent // Agent 实例
Template *types.AgentTemplateDefinition // 模板定义
Environment *EnvironmentInfo // 环境信息
Sandbox *SandboxInfo // 沙箱信息
Tools map[string]tools.Tool // 可用工具
Metadata map[string]interface{} // 元数据
}
metadata["show_capabilities"] == true## Your Capabilities
You can:
- Read and analyze files
- Create and modify files
- Execute shell commands
- Search the web for information
## Environment Information
- Working Directory: /Users/user/project
- Platform: darwin
- Date: 2025-11-24
- Git Repository: Yes
- Current Branch: main
ToolsManual: &types.ToolsManualConfig{
Mode: "all", // "all", "listed", "none"
Include: []string{"Read", "Write"},
Exclude: []string{"Bash"},
}
ReminderOnStart == truemetadata["agent_type"] == "code_assistant" 或 "developer"metadata["room_id"] 存在Metadata: map[string]interface{}{
"room_id": "room-123",
"room_member_count": 3,
"room_members": []string{"alice", "bob", "charlie"},
}
metadata["workflow_id"] 存在metadata["enable_security"] == truemetadata["enable_performance_hints"] == true用途: 专业代码助手 包含模块: base, capabilities, environment, sandbox, tools_manual, todo_reminder, code_reference 使用场景: 代码编写、审查、调试
用途: 研究助手 包含模块: base, capabilities, environment, tools_manual, todo_reminder 使用场景: 信息收集、文献研究、数据分析
用途: 数据分析师 包含模块: base, capabilities, environment, sandbox, tools_manual, todo_reminder, performance 使用场景: 数据分析、可视化、统计分析
用途: DevOps 工程师 包含模块: base, capabilities, environment, sandbox, tools_manual, todo_reminder, security 使用场景: 基础设施管理、CI/CD 配置
用途: 安全审计员 包含模块: base, capabilities, environment, tools_manual, todo_reminder, security, code_reference 使用场景: 安全审计、漏洞扫描、合规检查
// 使用预设模板
deps := createDependencies()
RegisterAllPresets(deps.TemplateRegistry)
agent, err := agent.Create(ctx, &types.AgentConfig{
TemplateID: "code-assistant",
}, deps)
// 获取构建后的 System Prompt
systemPrompt := agent.GetSystemPrompt()
agent, err := agent.Create(ctx, &types.AgentConfig{
TemplateID: "code-assistant",
Metadata: map[string]interface{}{
"agent_type": "code_assistant",
"show_capabilities": true,
"show_limitations": true,
"enable_security": true,
"enable_performance_hints": true,
"custom_instructions": "Always write tests first.",
},
}, deps)
agent, err := agent.Create(ctx, &types.AgentConfig{
TemplateID: "code-assistant",
Metadata: map[string]interface{}{
"room_id": "room-123",
"room_member_count": 3,
"room_members": []string{"alice", "bob", "charlie"},
},
}, deps)
deps.TemplateRegistry.Register(&types.AgentTemplateDefinition{
ID: "selective-tools",
SystemPrompt: "You are a selective assistant.",
Tools: []interface{}{"Read", "Write", "Bash"},
Runtime: &types.AgentTemplateRuntime{
ToolsManual: &types.ToolsManualConfig{
Mode: "listed",
Include: []string{"Read", "Write"}, // 只包含这两个
},
},
})
创建自定义 Prompt 模块:
type CustomModule struct{}
func (m *CustomModule) Name() string { return "custom" }
func (m *CustomModule) Priority() int { return 35 }
func (m *CustomModule) Condition(ctx *PromptContext) bool {
return ctx.Metadata["enable_custom"] == true
}
func (m *CustomModule) Build(ctx *PromptContext) (string, error) {
return "## Custom Section\n\nYour custom content here.", nil
}
在 buildSystemPrompt 方法中添加:
builder.AddModule(&CustomModule{})
optimizer := &PromptOptimizer{
MaxLength: 10000, // 最大字符数
RemoveDuplicates: true, // 移除重复空行
CompactFormat: true, // 紧凑格式
}
optimized := optimizer.Optimize(systemPrompt)
stats := AnalyzePrompt(systemPrompt)
fmt.Println(FormatStats(stats))
// 输出: Prompt Stats: 2652 chars, 87 lines, 12 sections, ~663 tokens
compressor := &PromptCompressor{
TargetLength: 5000,
PreserveSections: []string{"Tools Manual", "Security"},
}
compressed := compressor.Compress(systemPrompt)
根据 Agent 的主要用途选择预设模板:
通过 Metadata 控制模块注入:
Metadata: map[string]interface{}{
"agent_type": "code_assistant", // 触发 CodeReferenceModule
"show_capabilities": true, // 触发 CapabilitiesModule
"enable_security": true, // 触发 SecurityModule
"enable_performance_hints": true, // 触发 PerformanceModule
}
对于上下文窗口有限的模型:
Context: &types.ContextManagerOptions{
MaxTokens: 100000, // 触发 ContextWindowModule
}
减少不必要的工具说明:
ToolsManual: &types.ToolsManualConfig{
Mode: "listed",
Include: []string{"Read", "Write"}, // 只包含必要的工具
}
针对特定任务添加额外指导:
Metadata: map[string]interface{}{
"custom_instructions": "Always follow TDD. Write tests first.",
}
Mode: "listed" 只包含必要工具Exclude 排除不需要的工具stats := AnalyzePrompt(systemPrompt)
log.Printf("Prompt size: %d chars, ~%d tokens",
stats.TotalLength,
stats.EstimatedTokens)
if stats.EstimatedTokens > 5000 {
log.Println("Warning: Prompt is large, consider optimization")
}
| 特性 | Claude Code | Aster Prompt Builder |
|---|---|---|
| 构建方式 | 模块化拼凑 | 模块化拼凑 |
| 动态注入 | 支持 | 支持 |
| 条件模块 | 支持 | 支持 |
| 优先级控制 | 隐式 | 显式(Priority 方法) |
| 自定义模块 | 不支持 | 支持 |
| 配置驱动 | 部分支持 | 完全支持 |
| 预设模板 | 无 | 8 个预设 |