1pub fn format_token_count(tokens: u32) -> String {
2 if tokens >= 1_000_000 {
3 format!("{:.1}M", tokens as f64 / 1_000_000.0)
4 } else if tokens >= 1_000 {
5 format!("{:.1}K", tokens as f64 / 1_000.0)
6 } else {
7 tokens.to_string()
8 }
9}
10
11#[derive(Debug, Clone, Copy)]
12pub enum ContextWindowSize {
13 Small8K,
14 Medium32K,
15 Large128K,
16 Huge256K,
17 Custom(u32),
18}
19
20impl ContextWindowSize {
21 pub fn tokens(&self) -> u32 {
22 match self {
23 ContextWindowSize::Small8K => 8192,
24 ContextWindowSize::Medium32K => 32768,
25 ContextWindowSize::Large128K => 131072,
26 ContextWindowSize::Huge256K => 262144,
27 ContextWindowSize::Custom(n) => *n,
28 }
29 }
30
31 pub fn default_size() -> Self {
32 ContextWindowSize::Small8K
33 }
34
35 pub fn usage_percentage(&self, used_tokens: u32) -> f64 {
36 let max = self.tokens() as f64;
37 let used = used_tokens as f64;
38 (used / max * 100.0).min(100.0)
39 }
40}
41
42pub const WARNING_THRESHOLD_PERCENT: f64 = 70.0;
43pub const CRITICAL_THRESHOLD_PERCENT: f64 = 90.0;
44
45pub fn check_context_warning(
46 used_tokens: u32,
47 context_size: ContextWindowSize,
48) -> Option<ContextWarning> {
49 let percentage = context_size.usage_percentage(used_tokens);
50
51 if percentage >= CRITICAL_THRESHOLD_PERCENT {
52 Some(ContextWarning::Critical(percentage))
53 } else if percentage >= WARNING_THRESHOLD_PERCENT {
54 Some(ContextWarning::Warning(percentage))
55 } else {
56 None
57 }
58}
59
60#[derive(Debug, Clone, Copy)]
61pub enum ContextWarning {
62 Warning(f64),
63 Critical(f64),
64}
65
66impl ContextWarning {
67 pub fn percentage(&self) -> f64 {
68 match self {
69 ContextWarning::Warning(p) | ContextWarning::Critical(p) => *p,
70 }
71 }
72
73 pub fn is_critical(&self) -> bool {
74 matches!(self, ContextWarning::Critical(_))
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
83 fn test_format_token_count() {
84 assert_eq!(format_token_count(0), "0");
85 assert_eq!(format_token_count(100), "100");
86 assert_eq!(format_token_count(1000), "1.0K");
87 assert_eq!(format_token_count(1500), "1.5K");
88 assert_eq!(format_token_count(1000000), "1.0M");
89 assert_eq!(format_token_count(1500000), "1.5M");
90 }
91
92 #[test]
93 fn test_context_window_default() {
94 assert!(matches!(
96 ContextWindowSize::default_size(),
97 ContextWindowSize::Small8K
98 ));
99 }
100
101 #[test]
102 fn test_context_window_usage() {
103 let ctx = ContextWindowSize::Small8K;
104 assert!((ctx.usage_percentage(4096) - 50.0).abs() < 1.0);
105 assert!((ctx.usage_percentage(8192) - 100.0).abs() < 1.0);
106 }
107
108 #[test]
109 fn test_context_warning() {
110 let ctx = ContextWindowSize::Small8K;
111
112 assert!(check_context_warning(4096, ctx).is_none());
114
115 let warning = check_context_warning(5735, ctx);
117 assert!(warning.is_some());
118 assert!(!warning.unwrap().is_critical());
119
120 let critical = check_context_warning(7373, ctx);
122 assert!(critical.is_some());
123 assert!(critical.unwrap().is_critical());
124 }
125}