Build 构建完整的 System Prompt
(ctx *PromptContext)
| 91 | |
| 92 | // Build 构建完整的 System Prompt |
| 93 | func (pb *PromptBuilder) Build(ctx *PromptContext) (string, error) { |
| 94 | // 按优先级排序 |
| 95 | sort.Slice(pb.modules, func(i, j int) bool { |
| 96 | return pb.modules[i].Priority() < pb.modules[j].Priority() |
| 97 | }) |
| 98 | |
| 99 | var sections []string |
| 100 | |
| 101 | // 获取禁用的模块列表 |
| 102 | var disabledModules []string |
| 103 | if ctx.Template != nil && ctx.Template.Runtime != nil { |
| 104 | disabledModules = ctx.Template.Runtime.DisabledPromptModules |
| 105 | } |
| 106 | |
| 107 | for _, module := range pb.modules { |
| 108 | // 检查是否被禁用 |
| 109 | moduleName := module.Name() |
| 110 | isDisabled := slices.Contains(disabledModules, moduleName) |
| 111 | if isDisabled { |
| 112 | continue |
| 113 | } |
| 114 | |
| 115 | // 检查条件 |
| 116 | if !module.Condition(ctx) { |
| 117 | continue |
| 118 | } |
| 119 | |
| 120 | // 构建模块内容 |
| 121 | content, err := module.Build(ctx) |
| 122 | if err != nil { |
| 123 | return "", fmt.Errorf("build module %s: %w", module.Name(), err) |
| 124 | } |
| 125 | |
| 126 | if content != "" { |
| 127 | sections = append(sections, content) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | systemPrompt := strings.Join(sections, "\n\n") |
| 132 | |
| 133 | // 检查是否需要压缩 |
| 134 | if pb.shouldCompress(systemPrompt, ctx) { |
| 135 | fmt.Printf("[PromptBuilder] 🔄 Compression triggered: prompt length=%d chars, threshold=%d\n", |
| 136 | len(systemPrompt), ctx.Template.Runtime.PromptCompression.MaxLength) |
| 137 | |
| 138 | // 输出原始内容(截取前 1000 字符) |
| 139 | originalPreview := systemPrompt |
| 140 | if len(originalPreview) > 1000 { |
| 141 | originalPreview = originalPreview[:1000] + "\n... (truncated)" |
| 142 | } |
| 143 | fmt.Printf("[PromptBuilder] 📄 ORIGINAL PROMPT:\n%s\n", originalPreview) |
| 144 | fmt.Println("------- END ORIGINAL -------") |
| 145 | |
| 146 | compressed, err := pb.compress(context.Background(), systemPrompt, ctx) |
| 147 | if err != nil { |
| 148 | // 压缩失败,使用原始内容 |
| 149 | fmt.Printf("[PromptBuilder] ❌ Compression failed: %v, using original\n", err) |
| 150 | return systemPrompt, nil |
no test coverage detected