Class: AIA::TopicContext
- Inherits:
-
Object
- Object
- AIA::TopicContext
- Defined in:
- lib/aia/topic_context.rb
Instance Attribute Summary collapse
-
#context_size ⇒ Object
readonly
Returns the value of attribute context_size.
Instance Method Summary collapse
-
#all_conversations ⇒ Hash<String, Array<Hash>>
Hash of topic => array_of_contexts.
-
#clear ⇒ Object
Empty the storage and reset counters.
-
#get_conversation(topic) ⇒ Array<Hash>
Return an array of contexts for the given topic.
-
#initialize(context_size = 128_000) ⇒ TopicContext
constructor
Initialize topic context manager.
-
#store_conversation(request, response, topic = nil) ⇒ String
Store a request/response pair under the given topic (or auto-generate one).
-
#topic_stats(topic) ⇒ Hash{Symbol => Integer}
Get memory usage statistics for a topic.
-
#topics ⇒ Array<String>
All topic names.
-
#total_chars ⇒ Integer
Total number of characters stored across all topics.
Constructor Details
#initialize(context_size = 128_000) ⇒ TopicContext
Initialize topic context manager
14 15 16 17 18 19 |
# File 'lib/aia/topic_context.rb', line 14 def initialize(context_size = 128_000) @storage = Hash.new { |h, k| h[k] = [] } # auto-initialize empty array @context_size = context_size @total_chars = 0 @mutex = Mutex.new # ensure thread safety end |
Instance Attribute Details
#context_size ⇒ Object (readonly)
Returns the value of attribute context_size.
10 11 12 |
# File 'lib/aia/topic_context.rb', line 10 def context_size @context_size end |
Instance Method Details
#all_conversations ⇒ Hash<String, Array<Hash>>
Hash of topic => array_of_contexts
61 62 63 |
# File 'lib/aia/topic_context.rb', line 61 def all_conversations @mutex.synchronize { @storage.dup } end |
#clear ⇒ Object
Empty the storage and reset counters
72 73 74 75 76 77 |
# File 'lib/aia/topic_context.rb', line 72 def clear @mutex.synchronize do @storage.clear @total_chars = 0 end end |
#get_conversation(topic) ⇒ Array<Hash>
Return an array of contexts for the given topic
49 50 51 |
# File 'lib/aia/topic_context.rb', line 49 def get_conversation(topic) @mutex.synchronize { @storage[topic] || [] } end |
#store_conversation(request, response, topic = nil) ⇒ String
Store a request/response pair under the given topic (or auto-generate one)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/aia/topic_context.rb', line 26 def store_conversation(request, response, topic = nil) raise ArgumentError, "request and response must be strings" unless request.is_a?(String) && response.is_a?(String) topic ||= generate_topic(request) size = request.bytesize + response.bytesize @mutex.synchronize do # Add the new context @storage[topic] << { request:, response:, size:, time: Time.now } # Update the global total @total_chars += size # Trim old entries if we exceeded the per-topic limit trim_topic(topic) end topic end |
#topic_stats(topic) ⇒ Hash{Symbol => Integer}
Get memory usage statistics for a topic
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/aia/topic_context.rb', line 82 def topic_stats(topic) @mutex.synchronize do return {} unless @storage.key?(topic) { count: @storage[topic].length, size: topic_total_size(topic), avg_size: topic_total_size(topic).fdiv(@storage[topic].length), } end end |
#topics ⇒ Array<String>
All topic names
55 56 57 |
# File 'lib/aia/topic_context.rb', line 55 def topics @mutex.synchronize { @storage.keys } end |
#total_chars ⇒ Integer
Total number of characters stored across all topics
67 68 69 |
# File 'lib/aia/topic_context.rb', line 67 def total_chars @mutex.synchronize { @total_chars } end |