|
| 1 | +//! Rendering for context window information |
| 2 | +
|
| 3 | +use crossterm::style::Attribute; |
| 4 | +use crossterm::{ |
| 5 | + execute, |
| 6 | + queue, |
| 7 | + style, |
| 8 | +}; |
| 9 | + |
| 10 | +use super::context_data_provider::ContextWindowData; |
| 11 | +use crate::cli::chat::token_counter::TokenCount; |
| 12 | +use crate::cli::chat::{ |
| 13 | + ChatError, |
| 14 | + ChatSession, |
| 15 | +}; |
| 16 | +use crate::theme::StyledText; |
| 17 | + |
| 18 | +/// Calculate context percentage from token counts (private utility) |
| 19 | +fn calculate_context_percentage(tokens: TokenCount, context_window_size: usize) -> f32 { |
| 20 | + (tokens.value() as f32 / context_window_size as f32) * 100.0 |
| 21 | +} |
| 22 | + |
| 23 | +/// Render context window information section |
| 24 | +pub async fn render_context_window( |
| 25 | + context_data: &ContextWindowData, |
| 26 | + session: &mut ChatSession, |
| 27 | +) -> Result<(), ChatError> { |
| 28 | + if !context_data.dropped_context_files.is_empty() { |
| 29 | + execute!( |
| 30 | + session.stderr, |
| 31 | + StyledText::warning_fg(), |
| 32 | + style::Print("\nSome context files are dropped due to size limit, please run "), |
| 33 | + StyledText::success_fg(), |
| 34 | + style::Print("/context show "), |
| 35 | + StyledText::warning_fg(), |
| 36 | + style::Print("to learn more.\n"), |
| 37 | + StyledText::reset(), |
| 38 | + )?; |
| 39 | + } |
| 40 | + |
| 41 | + let window_width = session.terminal_width(); |
| 42 | + // set a max width for the progress bar for better aesthetic |
| 43 | + let progress_bar_width = std::cmp::min(window_width, 80); |
| 44 | + |
| 45 | + let context_width = ((context_data.context_tokens.value() as f64 / context_data.context_window_size as f64) |
| 46 | + * progress_bar_width as f64) as usize; |
| 47 | + let assistant_width = ((context_data.assistant_tokens.value() as f64 / context_data.context_window_size as f64) |
| 48 | + * progress_bar_width as f64) as usize; |
| 49 | + let tools_width = ((context_data.tools_tokens.value() as f64 / context_data.context_window_size as f64) |
| 50 | + * progress_bar_width as f64) as usize; |
| 51 | + let user_width = ((context_data.user_tokens.value() as f64 / context_data.context_window_size as f64) |
| 52 | + * progress_bar_width as f64) as usize; |
| 53 | + |
| 54 | + let left_over_width = progress_bar_width |
| 55 | + - std::cmp::min( |
| 56 | + context_width + assistant_width + user_width + tools_width, |
| 57 | + progress_bar_width, |
| 58 | + ); |
| 59 | + |
| 60 | + let is_overflow = (context_width + assistant_width + user_width + tools_width) > progress_bar_width; |
| 61 | + |
| 62 | + let total_percentage = calculate_context_percentage(context_data.total_tokens, context_data.context_window_size); |
| 63 | + |
| 64 | + if is_overflow { |
| 65 | + queue!( |
| 66 | + session.stderr, |
| 67 | + style::Print(format!( |
| 68 | + "\nCurrent context window ({} of {}k tokens used)\n", |
| 69 | + context_data.total_tokens, |
| 70 | + context_data.context_window_size / 1000 |
| 71 | + )), |
| 72 | + StyledText::error_fg(), |
| 73 | + style::Print("█".repeat(progress_bar_width)), |
| 74 | + StyledText::reset(), |
| 75 | + style::Print(" "), |
| 76 | + style::Print(format!("{total_percentage:.2}%")), |
| 77 | + )?; |
| 78 | + } else { |
| 79 | + queue!( |
| 80 | + session.stderr, |
| 81 | + style::Print(format!( |
| 82 | + "\nCurrent context window ({} of {}k tokens used)\n", |
| 83 | + context_data.total_tokens, |
| 84 | + context_data.context_window_size / 1000 |
| 85 | + )), |
| 86 | + // Context files |
| 87 | + StyledText::brand_fg(), |
| 88 | + // add a nice visual to mimic "tiny" progress, so the overrall progress bar doesn't look too |
| 89 | + // empty |
| 90 | + style::Print( |
| 91 | + "|".repeat(if context_width == 0 && context_data.context_tokens.value() > 0 { |
| 92 | + 1 |
| 93 | + } else { |
| 94 | + 0 |
| 95 | + }) |
| 96 | + ), |
| 97 | + style::Print("█".repeat(context_width)), |
| 98 | + // Tools |
| 99 | + StyledText::error_fg(), |
| 100 | + style::Print( |
| 101 | + "|".repeat(if tools_width == 0 && context_data.tools_tokens.value() > 0 { |
| 102 | + 1 |
| 103 | + } else { |
| 104 | + 0 |
| 105 | + }) |
| 106 | + ), |
| 107 | + style::Print("█".repeat(tools_width)), |
| 108 | + // Assistant responses |
| 109 | + StyledText::info_fg(), |
| 110 | + style::Print( |
| 111 | + "|".repeat(if assistant_width == 0 && context_data.assistant_tokens.value() > 0 { |
| 112 | + 1 |
| 113 | + } else { |
| 114 | + 0 |
| 115 | + }) |
| 116 | + ), |
| 117 | + style::Print("█".repeat(assistant_width)), |
| 118 | + // User prompts |
| 119 | + StyledText::emphasis_fg(), |
| 120 | + style::Print("|".repeat(if user_width == 0 && context_data.user_tokens.value() > 0 { |
| 121 | + 1 |
| 122 | + } else { |
| 123 | + 0 |
| 124 | + })), |
| 125 | + style::Print("█".repeat(user_width)), |
| 126 | + StyledText::secondary_fg(), |
| 127 | + style::Print("█".repeat(left_over_width)), |
| 128 | + style::Print(" "), |
| 129 | + StyledText::reset(), |
| 130 | + style::Print(format!("{total_percentage:.2}%")), |
| 131 | + )?; |
| 132 | + } |
| 133 | + |
| 134 | + execute!(session.stderr, style::Print("\n\n"))?; |
| 135 | + |
| 136 | + queue!( |
| 137 | + session.stderr, |
| 138 | + StyledText::brand_fg(), |
| 139 | + style::Print("█ Context files: "), |
| 140 | + StyledText::reset(), |
| 141 | + style::Print(format!( |
| 142 | + "~{} tokens ({:.2}%)\n", |
| 143 | + context_data.context_tokens, |
| 144 | + calculate_context_percentage(context_data.context_tokens, context_data.context_window_size) |
| 145 | + )), |
| 146 | + StyledText::error_fg(), |
| 147 | + style::Print("█ Tools: "), |
| 148 | + StyledText::reset(), |
| 149 | + style::Print(format!( |
| 150 | + " ~{} tokens ({:.2}%)\n", |
| 151 | + context_data.tools_tokens, |
| 152 | + calculate_context_percentage(context_data.tools_tokens, context_data.context_window_size) |
| 153 | + )), |
| 154 | + StyledText::info_fg(), |
| 155 | + style::Print("█ Kiro responses: "), |
| 156 | + StyledText::reset(), |
| 157 | + style::Print(format!( |
| 158 | + " ~{} tokens ({:.2}%)\n", |
| 159 | + context_data.assistant_tokens, |
| 160 | + calculate_context_percentage(context_data.assistant_tokens, context_data.context_window_size) |
| 161 | + )), |
| 162 | + StyledText::emphasis_fg(), |
| 163 | + style::Print("█ Your prompts: "), |
| 164 | + StyledText::reset(), |
| 165 | + style::Print(format!( |
| 166 | + " ~{} tokens ({:.2}%)\n\n", |
| 167 | + context_data.user_tokens, |
| 168 | + calculate_context_percentage(context_data.user_tokens, context_data.context_window_size) |
| 169 | + )), |
| 170 | + )?; |
| 171 | + |
| 172 | + queue!( |
| 173 | + session.stderr, |
| 174 | + style::SetAttribute(Attribute::Bold), |
| 175 | + style::Print("\n💡 Pro Tips:\n"), |
| 176 | + StyledText::reset_attributes(), |
| 177 | + StyledText::secondary_fg(), |
| 178 | + style::Print("Run "), |
| 179 | + StyledText::success_fg(), |
| 180 | + style::Print("/compact"), |
| 181 | + StyledText::secondary_fg(), |
| 182 | + style::Print(" to replace the conversation history with its summary\n"), |
| 183 | + style::Print("Run "), |
| 184 | + StyledText::success_fg(), |
| 185 | + style::Print("/clear"), |
| 186 | + StyledText::secondary_fg(), |
| 187 | + style::Print(" to erase the entire chat history\n"), |
| 188 | + style::Print("Run "), |
| 189 | + StyledText::success_fg(), |
| 190 | + style::Print("/context show"), |
| 191 | + StyledText::secondary_fg(), |
| 192 | + style::Print(" to see tokens per context file\n\n"), |
| 193 | + StyledText::reset(), |
| 194 | + )?; |
| 195 | + |
| 196 | + Ok(()) |
| 197 | +} |
0 commit comments