Class: BoltRb::Assistant::MemoryThreadContextStore

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt_rb/assistant/memory_thread_context_store.rb

Overview

In-memory store for assistant thread context.

Slack sends the user's active channel with assistant_thread_started and assistant_thread_context_changed. This store keeps that context so later user messages in the same thread can read it.

Data lives only in the current process. Give BoltRb.configuration.assistant_thread_context_store an object with the same get and save methods to persist across processes.

Examples:

Custom store

class RedisThreadContextStore
  def get(channel_id:, thread_ts:)
    json = redis.get("assistant:#{channel_id}:#{thread_ts}")
    json && JSON.parse(json)
  end

  def save(channel_id:, thread_ts:, context:)
    redis.set("assistant:#{channel_id}:#{thread_ts}", context.to_json)
  end
end

Instance Method Summary collapse

Constructor Details

#initializeMemoryThreadContextStore

Returns a new instance of MemoryThreadContextStore.



27
28
29
30
# File 'lib/bolt_rb/assistant/memory_thread_context_store.rb', line 27

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

Instance Method Details

#get(channel_id:, thread_ts:) ⇒ Hash?

Read the saved context for a thread

Parameters:

  • channel_id (String)

    The DM channel ID

  • thread_ts (String)

    The thread timestamp

Returns:

  • (Hash, nil)

    The saved context or nil



37
38
39
# File 'lib/bolt_rb/assistant/memory_thread_context_store.rb', line 37

def get(channel_id:, thread_ts:)
  @mutex.synchronize { @contexts[key(channel_id, thread_ts)] }
end

#save(channel_id:, thread_ts:, context:) ⇒ void

This method returns an undefined value.

Save the context for a thread

Parameters:

  • channel_id (String)

    The DM channel ID

  • thread_ts (String)

    The thread timestamp

  • context (Hash)

    The context to save



47
48
49
# File 'lib/bolt_rb/assistant/memory_thread_context_store.rb', line 47

def save(channel_id:, thread_ts:, context:)
  @mutex.synchronize { @contexts[key(channel_id, thread_ts)] = context }
end