| 1 | import type { ChatHistoryItem } from "core"; |
| 2 | |
| 3 | import { findLastSafeSplitPoint } from "./messageSplitting.js"; |
| 4 | |
| 5 | const MAX_HEIGHT_BEFORE_SPLIT = 20; |
| 6 | |
| 7 | /** |
| 8 | * Splits a chat history item into multiple items if its content is too large. |
| 9 | * |
| 10 | * This function prevents UI flickering by breaking large messages into manageable chunks. |
| 11 | * It only splits at safe markdown boundaries (paragraph breaks: \n\n) to preserve formatting. |
| 12 | * |
| 13 | * Splitting behavior: |
| 14 | * - User messages: Split only when they have natural paragraph breaks (\n\n) |
| 15 | * - Assistant messages: Split when content exceeds height threshold |
| 16 | * - System messages: Never split |
| 17 | * - Messages with tool calls: Never split |
| 18 | * |
| 19 | * Common use cases: |
| 20 | * - User pastes multiple paragraphs of text -> Split at paragraph boundaries |
| 21 | * - User writes one long paragraph -> Keep as single message |
| 22 | * - Assistant generates long response -> Split at paragraph breaks (\n\n) |
| 23 | */ |
| 24 | function splitChatHistoryItem( |
| 25 | item: ChatHistoryItem, |
| 26 | terminalWidth: number, |
| 27 | ): ChatHistoryItem[] { |
| 28 | // Don't split non-text messages or messages with tool calls |
| 29 | if (item.toolCallStates?.length || typeof item.message.content !== "string") { |
| 30 | return [item]; |
| 31 | } |
| 32 | |
| 33 | // Don't split system messages |
| 34 | if (item.message.role === "system") { |
| 35 | return [item]; |
| 36 | } |
| 37 | |
| 38 | const content = item.message.content; |
| 39 | |
| 40 | if (!shouldSplitContent(content, terminalWidth)) { |
| 41 | return [item]; |
| 42 | } |
| 43 | |
| 44 | return splitContentIntoHistoryItems(item, content, terminalWidth); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Splits content into multiple chat history items at safe markdown boundaries. |
| 49 | */ |
| 50 | function splitContentIntoHistoryItems( |
| 51 | originalItem: ChatHistoryItem, |
| 52 | content: string, |
| 53 | terminalWidth: number, |
| 54 | ): ChatHistoryItem[] { |
| 55 | const items: ChatHistoryItem[] = []; |
| 56 | let remainingContent = content; |
| 57 | let itemIndex = 0; |
| 58 | |
| 59 | while (remainingContent.length > 0) { |
| 60 | if (!shouldSplitContent(remainingContent, terminalWidth)) { |
| 61 | // Remaining content is small enough, add it as the final item |
| 62 | if (remainingContent.trim()) { |
| 63 | items.push({ |
| 64 | ...originalItem, |
| 65 | message: { |
| 66 | ...originalItem.message, |
| 67 | content: remainingContent, |
| 68 | }, |
| 69 | }); |
| 70 | } |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | // Find safe split point |
| 75 | const splitPoint = findLastSafeSplitPoint(remainingContent); |
| 76 | |
| 77 | if (splitPoint === remainingContent.length) { |
| 78 | // No safe split point found, add as single large item |
| 79 | if (remainingContent.trim()) { |
| 80 | items.push({ |
| 81 | ...originalItem, |
| 82 | message: { |
| 83 | ...originalItem.message, |
| 84 | content: remainingContent, |
| 85 | }, |
| 86 | }); |
| 87 | } |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | // Split at the safe point |
| 92 | const beforeText = remainingContent.slice(0, splitPoint).trim(); |
| 93 | remainingContent = remainingContent.slice(splitPoint).trim(); |
| 94 | |
| 95 | if (beforeText) { |
| 96 | items.push({ |
| 97 | ...originalItem, |
| 98 | message: { |
| 99 | ...originalItem.message, |
| 100 | content: beforeText, |
| 101 | }, |
| 102 | }); |
| 103 | } |
| 104 | |
| 105 | itemIndex++; |
| 106 | |
| 107 | // Prevent infinite loops |
| 108 | if (itemIndex > 50) { |
| 109 | console.warn("Message splitting exceeded maximum iterations, stopping"); |
| 110 | if (remainingContent.trim()) { |
| 111 | items.push({ |
| 112 | ...originalItem, |
| 113 | message: { |
| 114 | ...originalItem.message, |
| 115 | content: remainingContent, |
| 116 | }, |
| 117 | }); |
| 118 | } |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return items; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Processes an entire chat history array, splitting large items into multiple items. |
| 128 | */ |
| 129 | export function splitChatHistory( |
| 130 | chatHistory: ChatHistoryItem[], |
| 131 | terminalWidth: number, |
| 132 | ): ChatHistoryItem[] { |
| 133 | const result: ChatHistoryItem[] = []; |
| 134 | |
| 135 | for (const item of chatHistory) { |
| 136 | const splitItems = splitChatHistoryItem(item, terminalWidth); |
| 137 | result.push(...splitItems); |
| 138 | } |
| 139 | |
| 140 | return result; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Estimates the height in terminal rows that content will take when rendered. |
| 145 | */ |
| 146 | function estimateContentHeight(content: string, terminalWidth: number): number { |
| 147 | if (!content) return 0; |
| 148 | |
| 149 | const lines = content.split("\n"); |
| 150 | let totalHeight = 0; |
| 151 | |
| 152 | for (const line of lines) { |
| 153 | if (line.length === 0) { |
| 154 | totalHeight += 1; // Empty line |
| 155 | } else { |
| 156 | // Account for line wrapping |
| 157 | totalHeight += Math.ceil(line.length / Math.max(terminalWidth - 2, 40)); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return totalHeight; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Determines if content should be split based on its estimated height. |
| 166 | */ |
| 167 | function shouldSplitContent(content: string, terminalWidth: number): boolean { |
| 168 | const estimatedHeight = estimateContentHeight(content, terminalWidth); |
| 169 | return estimatedHeight > MAX_HEIGHT_BEFORE_SPLIT; |
| 170 | } |