guidedCompact uses the LLM to summarize conversation history while preserving what the user explicitly requests.
(ctx context.Context, instruction string)
| 89 | // guidedCompact uses the LLM to summarize conversation history while |
| 90 | // preserving what the user explicitly requests. |
| 91 | func (cli *ChatCLI) guidedCompact(ctx context.Context, instruction string) { |
| 92 | // Find boundaries: keep system messages and a few recent messages verbatim |
| 93 | systemEnd := 0 |
| 94 | for i, msg := range cli.history { |
| 95 | if msg.Role == "system" && i == systemEnd { |
| 96 | systemEnd = i + 1 |
| 97 | } else { |
| 98 | break |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | minKeepRecent := 4 |
| 103 | recentStart := len(cli.history) - minKeepRecent |
| 104 | if recentStart <= systemEnd { |
| 105 | fmt.Println(colorize(" "+i18n.T("compact.error.not_enough_with_instruction"), ColorGray)) |
| 106 | return |
| 107 | } |
| 108 | |
| 109 | middleMessages := cli.history[systemEnd:recentStart] |
| 110 | if len(middleMessages) < 2 { |
| 111 | fmt.Println(colorize(" "+i18n.T("compact.error.not_enough_middle"), ColorGray)) |
| 112 | return |
| 113 | } |
| 114 | |
| 115 | // Build conversation text for the summarizer |
| 116 | var sb strings.Builder |
| 117 | for _, msg := range middleMessages { |
| 118 | content := msg.Content |
| 119 | if len(content) > 2000 { |
| 120 | content = content[:1500] + "\n... [truncated] ...\n" + content[len(content)-300:] |
| 121 | } |
| 122 | sb.WriteString(fmt.Sprintf("[%s]: %s\n\n", msg.Role, content)) |
| 123 | } |
| 124 | |
| 125 | prompt := fmt.Sprintf(`You are a conversation compactor. Summarize the conversation below into a concise structured note. |
| 126 | |
| 127 | USER INSTRUCTION: %s |
| 128 | |
| 129 | Follow the user's instruction about what to preserve. Everything the user says to keep MUST appear in your output. |
| 130 | Drop everything else. Be concise but complete for the preserved topics. |
| 131 | |
| 132 | OUTPUT FORMAT: |
| 133 | - Use markdown headers for major topics |
| 134 | - Use bullet points for details |
| 135 | - Include exact file paths, code references, and technical specifics when relevant |
| 136 | - Do NOT add information that is not in the conversation |
| 137 | |
| 138 | CONVERSATION TO COMPACT: |
| 139 | |
| 140 | %s`, instruction, sb.String()) |
| 141 | |
| 142 | summaryHistory := []models.Message{ |
| 143 | {Role: "user", Content: prompt}, |
| 144 | } |
| 145 | |
| 146 | fmt.Printf(" %s %s %s\n", |
| 147 | colorize("📦", ""), |
| 148 | i18n.T("compact.compacting_with_instruction"), |
no test coverage detected