| 1 | import type { ChatHistoryItem, Session } from "core/index.js"; |
| 2 | |
| 3 | import { compactChatHistory } from "../../compaction.js"; |
| 4 | import { updateSessionHistory } from "../../session.js"; |
| 5 | import { formatError } from "../../util/formatError.js"; |
| 6 | import { logger } from "../../util/logger.js"; |
| 7 | import { shouldAutoCompact } from "../../util/tokenizer.js"; |
| 8 | |
| 9 | interface HandleCompactCommandOptions { |
| 10 | chatHistory: ChatHistoryItem[]; |
| 11 | model: any; |
| 12 | llmApi: any; |
| 13 | setChatHistory: React.Dispatch<React.SetStateAction<ChatHistoryItem[]>>; |
| 14 | setCompactionIndex: React.Dispatch<React.SetStateAction<number | null>>; |
| 15 | currentSession: Session; |
| 16 | setCurrentSession: React.Dispatch<React.SetStateAction<Session>>; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Handle compaction command |
| 21 | */ |
| 22 | export async function handleCompactCommand({ |
| 23 | chatHistory, |
| 24 | model, |
| 25 | llmApi, |
| 26 | setChatHistory, |
| 27 | setCompactionIndex, |
| 28 | currentSession, |
| 29 | setCurrentSession, |
| 30 | }: HandleCompactCommandOptions): Promise<void> { |
| 31 | // Add compacting message |
| 32 | setChatHistory((prev) => [ |
| 33 | ...prev, |
| 34 | { |
| 35 | message: { |
| 36 | role: "system", |
| 37 | content: "Compacting chat history...", |
| 38 | }, |
| 39 | contextItems: [], |
| 40 | }, |
| 41 | ]); |
| 42 | |
| 43 | try { |
| 44 | // Compact the chat history directly (already in unified format) |
| 45 | const result = await compactChatHistory(chatHistory, model, llmApi); |
| 46 | |
| 47 | // Replace chat history with compacted version |
| 48 | setChatHistory(result.compactedHistory); |
| 49 | setCompactionIndex(result.compactionIndex); |
| 50 | |
| 51 | // Save the compacted session |
| 52 | const updatedSession: Session = { |
| 53 | ...currentSession, |
| 54 | history: result.compactedHistory, |
| 55 | }; |
| 56 | updateSessionHistory(result.compactedHistory); |
| 57 | setCurrentSession(updatedSession); |
| 58 | |
| 59 | setChatHistory((prev) => [ |
| 60 | ...prev, |
| 61 | { |
| 62 | message: { |
| 63 | role: "system", |
| 64 | content: "Chat history compacted successfully.", |
| 65 | }, |
| 66 | contextItems: [], |
| 67 | }, |
| 68 | ]); |
| 69 | } catch (error) { |
| 70 | logger.error("Compaction failed:", error); |
| 71 | setChatHistory((prev) => [ |
| 72 | ...prev, |
| 73 | { |
| 74 | message: { |
| 75 | role: "system", |
| 76 | content: `Compaction failed: ${formatError(error)}`, |
| 77 | }, |
| 78 | contextItems: [], |
| 79 | }, |
| 80 | ]); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | interface HandleAutoCompactionOptions { |
| 85 | chatHistory: ChatHistoryItem[]; |
| 86 | model: any; |
| 87 | llmApi: any; |
| 88 | compactionIndex: number | null; |
| 89 | setChatHistory: React.Dispatch<React.SetStateAction<ChatHistoryItem[]>>; |
| 90 | setCompactionIndex: React.Dispatch<React.SetStateAction<number | null>>; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Handle auto-compaction when approaching context limit - TUI wrapper |
| 95 | */ |
| 96 | export async function handleAutoCompaction({ |
| 97 | chatHistory, |
| 98 | model, |
| 99 | llmApi, |
| 100 | compactionIndex: _compactionIndex, |
| 101 | setChatHistory, |
| 102 | setCompactionIndex, |
| 103 | }: HandleAutoCompactionOptions): Promise<{ |
| 104 | currentChatHistory: ChatHistoryItem[]; |
| 105 | currentCompactionIndex: number | null; |
| 106 | }> { |
| 107 | try { |
| 108 | // Check if auto-compaction is needed |
| 109 | // Check if auto-compaction is needed using ChatHistoryItem directly |
| 110 | if (!model || !shouldAutoCompact(chatHistory, model)) { |
| 111 | return { |
| 112 | currentChatHistory: chatHistory, |
| 113 | currentCompactionIndex: _compactionIndex, |
| 114 | }; |
| 115 | } |
| 116 | |
| 117 | logger.info("Auto-compaction triggered for TUI mode"); |
| 118 | |
| 119 | // Add compacting message |
| 120 | setChatHistory((prev) => [ |
| 121 | ...prev, |
| 122 | { |
| 123 | message: { |
| 124 | role: "system", |
| 125 | content: "Auto-compacting chat history...", |
| 126 | }, |
| 127 | contextItems: [], |
| 128 | }, |
| 129 | ]); |
| 130 | |
| 131 | // Compact the unified history |
| 132 | const result = await compactChatHistory(chatHistory, model, llmApi); |
| 133 | |
| 134 | // Keep the system message and append the compaction summary |
| 135 | // This replaces the old messages with a summary to reduce context size |
| 136 | const systemMessage = chatHistory.find( |
| 137 | (item) => item.message.role === "system", |
| 138 | ); |
| 139 | |
| 140 | const compactedMessage: ChatHistoryItem = { |
| 141 | message: { |
| 142 | role: "assistant" as const, |
| 143 | content: result.compactionContent, |
| 144 | }, |
| 145 | contextItems: [], |
| 146 | conversationSummary: result.compactionContent, // Mark this as a summary |
| 147 | }; |
| 148 | |
| 149 | // Create new history with system message (if exists) and compaction summary |
| 150 | const updatedHistory: ChatHistoryItem[] = systemMessage |
| 151 | ? [systemMessage, compactedMessage] |
| 152 | : [compactedMessage]; |
| 153 | |
| 154 | setChatHistory(updatedHistory); |
| 155 | setCompactionIndex(updatedHistory.length - 1); |
| 156 | |
| 157 | // Add success message |
| 158 | setChatHistory((prev) => [ |
| 159 | ...prev, |
| 160 | { |
| 161 | message: { |
| 162 | role: "system", |
| 163 | content: "✓ Chat history auto-compacted successfully.", |
| 164 | }, |
| 165 | contextItems: [], |
| 166 | }, |
| 167 | ]); |
| 168 | |
| 169 | return { |
| 170 | currentChatHistory: updatedHistory, |
| 171 | currentCompactionIndex: updatedHistory.length - 1, |
| 172 | }; |
| 173 | } catch (error) { |
| 174 | logger.error("Auto-compaction failed:", error); |
| 175 | |
| 176 | // Add error message |
| 177 | setChatHistory((prev) => [ |
| 178 | ...prev, |
| 179 | { |
| 180 | message: { |
| 181 | role: "system", |
| 182 | content: `Auto-compaction failed: ${formatError(error)}. Continuing without compaction...`, |
| 183 | }, |
| 184 | contextItems: [], |
| 185 | }, |
| 186 | ]); |
| 187 | |
| 188 | return { |
| 189 | currentChatHistory: chatHistory, |
| 190 | currentCompactionIndex: null, |
| 191 | }; |
| 192 | } |
| 193 | } |