Class: OllamaChat::ContextUsage
- Inherits:
-
Object
- Object
- OllamaChat::ContextUsage
- Includes:
- Utils::ValueFormatter, Term::ANSIColor
- Defined in:
- lib/ollama_chat/context_usage.rb
Overview
Renders the current context-window usage as colored or plain strings.
Wraps an Chat instance and concentrates the ANSI coloring,
emoji, and token-formatting logic that was previously inlined in
Information. The color reflects how full the context is
relative to the compaction keep_recent / reserve budgets:
๐ข green โ below `keep_recent` (plenty of room)
๐ก yellow โ between `keep_recent` and `reserve` (getting tight)
๐ด red โ above `reserve` (compaction imminent)
Constant Summary collapse
- EMOJI =
{ green: '๐ข', yellow: '๐ก', red: '๐ด' }.freeze
Instance Method Summary collapse
-
#color_for(tokens, ctx) ⇒ Symbol
private
Resolves the threshold color for a given fill level.
-
#colored ⇒ String
Colored usage string, e.g.
-
#estimate ⇒ OllamaChat::TokenEstimator::Estimate
private
The compacted estimate.
-
#filled ⇒ Float
Fraction of the context window currently in use, clamped to 0.0โ1.0.
-
#initialize(chat) ⇒ ContextUsage
constructor
A new instance of ContextUsage.
-
#paint(color, string) ⇒ String
private
Paints a string with the given threshold color.
-
#percent ⇒ String
Formatted percentage string, e.g.
-
#percent_for(tokens, ctx) ⇒ String
private
The formatted percentage.
-
#plain ⇒ String
Plain (uncolored) usage string, e.g.
Methods included from Utils::ValueFormatter
Constructor Details
#initialize(chat) ⇒ ContextUsage
Returns a new instance of ContextUsage.
22 23 24 |
# File 'lib/ollama_chat/context_usage.rb', line 22 def initialize(chat) @chat = chat end |
Instance Method Details
#color_for(tokens, ctx) ⇒ Symbol (private)
Resolves the threshold color for a given fill level.
101 102 103 104 105 106 107 108 |
# File 'lib/ollama_chat/context_usage.rb', line 101 def color_for(tokens, ctx) keep_recent = @chat.compact_ratio_tokens(:keep_recent, ctx) reserve = @chat.compact_ratio_tokens(:reserve, ctx) if tokens < keep_recent then :green elsif tokens <= reserve then :yellow else :red end end |
#colored ⇒ String
Colored usage string, e.g. ๐ข 73.1 KT of 262.1 KT (27.9%).
Every value (used tokens, context limit, percentage) is painted with the
threshold color; the surrounding text is bold. Returns a bold n/a when
the context length cannot be determined.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ollama_chat/context_usage.rb', line 33 def colored ctx = @chat.current_context_length return bold { 'n/a' } unless ctx es = estimate color = color_for(es.tokens, ctx) percent = percent_for(es.tokens, ctx) [ EMOJI[color], ' ', bold { paint(color, percent) }, ' ยท ', paint(color, es.tokens_formatted), ' of ', paint(color, format_tokens(ctx)), ] * '' end |
#estimate ⇒ OllamaChat::TokenEstimator::Estimate (private)
Returns the compacted estimate.
85 86 87 |
# File 'lib/ollama_chat/context_usage.rb', line 85 def estimate @chat..compacted_estimate_tokens end |
#filled ⇒ Float
Fraction of the context window currently in use, clamped to 0.0โ1.0.
70 71 72 73 |
# File 'lib/ollama_chat/context_usage.rb', line 70 def filled es = estimate (es.tokens.to_f / @chat.current_context_length).clamp(0..1).to_f end |
#paint(color, string) ⇒ String (private)
Paints a string with the given threshold color.
115 116 117 118 119 120 121 |
# File 'lib/ollama_chat/context_usage.rb', line 115 def paint(color, string) case color when :green then green { string } when :yellow then yellow { string } else red { string } end end |
#percent ⇒ String
Formatted percentage string, e.g. 27.9%.
78 79 80 |
# File 'lib/ollama_chat/context_usage.rb', line 78 def percent format('%.1f%%', 100 * filled) end |
#percent_for(tokens, ctx) ⇒ String (private)
Returns the formatted percentage.
92 93 94 |
# File 'lib/ollama_chat/context_usage.rb', line 92 def percent_for(tokens, ctx) format('%.1f%%', 100 * (tokens.to_f / ctx).clamp(0..1)) end |
#plain ⇒ String
Plain (uncolored) usage string, e.g. 73.1 KT of 262.1 KT (27.9%).
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ollama_chat/context_usage.rb', line 55 def plain ctx = @chat.current_context_length return 'n/a' unless ctx es = estimate '%s ยท %s of %s' % [ percent_for(es.tokens, ctx), es.tokens_formatted, format_tokens(ctx), ] end |