Class: AIA::TopicContext

Inherits:
Object
  • Object
show all
Defined in:
lib/aia/topic_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_size = 128_000) ⇒ TopicContext

Initialize topic context manager

Parameters:

  • context_size (Integer) (defaults to: 128_000)

    max allowed bytes per topic



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_sizeObject (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_conversationsHash<String, Array<Hash>>

Hash of topic => array_of_contexts

Returns:

  • (Hash<String, Array<Hash>>)


61
62
63
# File 'lib/aia/topic_context.rb', line 61

def all_conversations
  @mutex.synchronize { @storage.dup }
end

#clearObject

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

Parameters:

  • topic (String)

Returns:

  • (Array<Hash>)


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)

Parameters:

  • request (String)
  • response (String)
  • topic (String, nil) (defaults to: nil)

Returns:

  • (String)

    topic name used

Raises:

  • (ArgumentError)


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

Parameters:

  • topic (String)

Returns:

  • (Hash{Symbol => Integer})


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

#topicsArray<String>

All topic names

Returns:

  • (Array<String>)


55
56
57
# File 'lib/aia/topic_context.rb', line 55

def topics
  @mutex.synchronize { @storage.keys }
end

#total_charsInteger

Total number of characters stored across all topics

Returns:

  • (Integer)


67
68
69
# File 'lib/aia/topic_context.rb', line 67

def total_chars
  @mutex.synchronize { @total_chars }
end