Class: Clacky::Channel::GroupMessageBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/clacky/server/channel/group_message_buffer.rb

Overview

Stores recent group chat messages per chat_id so that when the bot is @-mentioned it can inject prior conversation context into the agent prompt. Thread-safe; bounded to MAX_MESSAGES per chat to limit memory growth.

Defined Under Namespace

Classes: Entry

Constant Summary collapse

MAX_MESSAGES =
15
PROMPT_LIMIT =
5

Instance Method Summary collapse

Constructor Details

#initializeGroupMessageBuffer

Returns a new instance of GroupMessageBuffer.



14
15
16
17
# File 'lib/clacky/server/channel/group_message_buffer.rb', line 14

def initialize
  @buffers = {}
  @mutex   = Mutex.new
end

Instance Method Details

#peek(chat_id, limit: nil) ⇒ Array<Entry>

Return the most recent ‘limit` entries without clearing the buffer.

Parameters:

  • chat_id (String)
  • limit (Integer, nil) (defaults to: nil)

    max entries to return; nil = all

Returns:



37
38
39
40
41
42
# File 'lib/clacky/server/channel/group_message_buffer.rb', line 37

def peek(chat_id, limit: nil)
  @mutex.synchronize do
    buf = @buffers[chat_id] || []
    limit ? buf.last(limit) : buf.dup
  end
end

#push(chat_id, user_id:, text:, user_name: nil) ⇒ Object

Parameters:

  • chat_id (String)
  • user_id (String)
  • user_name (String, nil) (defaults to: nil)
  • text (String)


23
24
25
26
27
28
29
30
31
# File 'lib/clacky/server/channel/group_message_buffer.rb', line 23

def push(chat_id, user_id:, text:, user_name: nil)
  return if text.nil? || text.strip.empty?

  @mutex.synchronize do
    buf = (@buffers[chat_id] ||= [])
    buf << Entry.new(user_id: user_id, user_name: user_name, text: text.strip)
    buf.shift if buf.size > MAX_MESSAGES
  end
end

#take(chat_id) ⇒ Array<Entry>

Return buffered entries for a chat and clear them atomically. Returns an empty array when there is no history.

Parameters:

  • chat_id (String)

Returns:



48
49
50
# File 'lib/clacky/server/channel/group_message_buffer.rb', line 48

def take(chat_id)
  @mutex.synchronize { @buffers.delete(chat_id) || [] }
end