Class: Mana::Context
- Inherits:
-
Object
- Object
- Mana::Context
- Defined in:
- lib/mana/context.rb
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#summaries ⇒ Object
readonly
Returns the value of attribute summaries.
Class Method Summary collapse
-
.current ⇒ Object
Return the current thread's context instance (lazy-initialized).
Instance Method Summary collapse
-
#clear! ⇒ Object
Clear conversation history and summaries.
-
#clear_messages! ⇒ Object
Clear conversation history and compaction summaries.
-
#estimate_tokens(text) ⇒ Object
Rough token estimate: ~4 characters per token.
-
#initialize ⇒ Context
constructor
Initialize with empty conversation context.
-
#inspect ⇒ Object
Human-readable summary: counts and token usage.
-
#token_count ⇒ Object
Estimate total token count across short-term messages and summaries.
Constructor Details
#initialize ⇒ Context
Initialize with empty conversation context
8 9 10 11 |
# File 'lib/mana/context.rb', line 8 def initialize @messages = [] @summaries = [] end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
5 6 7 |
# File 'lib/mana/context.rb', line 5 def @messages end |
#summaries ⇒ Object (readonly)
Returns the value of attribute summaries.
5 6 7 |
# File 'lib/mana/context.rb', line 5 def summaries @summaries end |
Class Method Details
.current ⇒ Object
Return the current thread's context instance (lazy-initialized). Uses config.context_class if set, otherwise Mana::Context.
18 19 20 21 22 23 |
# File 'lib/mana/context.rb', line 18 def current Thread.current[:mana_context] ||= begin klass = Mana.config.context_class || self klass.new end end |
Instance Method Details
#clear! ⇒ Object
Clear conversation history and summaries
58 59 60 |
# File 'lib/mana/context.rb', line 58 def clear! end |
#clear_messages! ⇒ Object
Clear conversation history and compaction summaries
63 64 65 66 |
# File 'lib/mana/context.rb', line 63 def @messages.clear @summaries.clear end |
#estimate_tokens(text) ⇒ Object
Rough token estimate: ~4 characters per token
49 50 51 52 53 |
# File 'lib/mana/context.rb', line 49 def estimate_tokens(text) return 0 unless text.is_a?(String) (text.length / 4.0).ceil end |
#inspect ⇒ Object
Human-readable summary: counts and token usage
71 72 73 |
# File 'lib/mana/context.rb', line 71 def inspect "#<Mana::Context messages=#{} rounds, tokens=#{token_count}/#{context_window}>" end |
#token_count ⇒ Object
Estimate total token count across short-term messages and summaries.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mana/context.rb', line 29 def token_count count = 0 @messages.each do |msg| content = msg[:content] case content when String # Plain text message count += estimate_tokens(content) when Array # Array of content blocks (tool_use, tool_result, text) content.each do |block| count += estimate_tokens(block[:text] || block[:content] || "") end end end @summaries.each { |s| count += estimate_tokens(s) } count end |