Class: OllamaChat::ContextUsage

Inherits:
Object
  • Object
show all
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

Methods included from Utils::ValueFormatter

#format_bytes, #format_tokens

Constructor Details

#initialize(chat) ⇒ ContextUsage

Returns a new instance of ContextUsage.

Parameters:



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.

Parameters:

  • tokens (Integer) —

    used tokens

  • ctx (Integer) —

    context length

Returns:

  • (Symbol) —

    one of :green, :yellow, :red



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.

Returns:

  • (String) —

    the ANSI-colored usage string



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.

Returns:



85
86
87
# File 'lib/ollama_chat/context_usage.rb', line 85

def estimate
  @chat.messages.compacted_estimate_tokens
end

#filled ⇒ Float

Fraction of the context window currently in use, clamped to 0.0โ€“1.0.

Returns:

  • (Float) —

    the fill ratio (e.g. 0.279)



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.

Parameters:

  • color (Symbol) —

    one of :green, :yellow, :red

  • string (String) —

    the text to colorize

Returns:

  • (String) —

    the ANSI-colored string



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%.

Returns:

  • (String) —

    the percentage



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.

Parameters:

  • tokens (Integer) —

    used tokens

  • ctx (Integer) —

    context length

Returns:

  • (String) —

    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%).

Returns:

  • (String) —

    the usage string, or 'n/a' when the context length is unknown.



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