Class: LLM::Buffer

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/llm/buffer.rb

Overview

LLM::Buffer provides an Enumerable object that yields each message in a conversation on-demand, and only sends a request to the LLM when a response is needed.

Instance Method Summary collapse

Constructor Details

#initialize(provider) ⇒ LLM::Buffer

Parameters:



14
15
16
17
18
# File 'lib/llm/buffer.rb', line 14

def initialize(provider)
  @provider = provider
  @pending = []
  @completed = []
end

Instance Method Details

#findLLM::Message?

Find a message (in descending order)

Returns:



46
47
48
# File 'lib/llm/buffer.rb', line 46

def find(...)
  reverse_each.find(...)
end

#each {|LLM::Message| ... } ⇒ void

This method returns an undefined value.

Yields:

  • (LLM::Message)

    Yields each message in the conversation thread

Raises:

  • (NotImplementedError)

    When the method is not implemented by a subclass



25
26
27
28
29
30
31
32
# File 'lib/llm/buffer.rb', line 25

def each(...)
  if block_given?
    empty! unless @pending.empty?
    @completed.each { yield(_1) }
  else
    enum_for(:each, ...)
  end
end

#unreadArray<LLM::Message>

Returns an array of unread messages

Returns:

See Also:



39
40
41
# File 'lib/llm/buffer.rb', line 39

def unread
  reject(&:read?)
end

#<<(item) ⇒ void Also known as: push

This method returns an undefined value.

Parameters:

  • item ([LLM::Message, Hash])

    A message and its parameters



54
55
56
57
# File 'lib/llm/buffer.rb', line 54

def <<(item)
  @pending << item
  self
end

#[](index) ⇒ LLM::Message?

Returns a message, or nil

Parameters:

  • index (Integer, #to_i)

    The message index

Returns:



65
66
67
# File 'lib/llm/buffer.rb', line 65

def [](index)
  @completed[index.to_i] || to_a[index.to_i]
end

#inspectString

Returns:

  • (String)


71
72
73
74
# File 'lib/llm/buffer.rb', line 71

def inspect
  "#<#{self.class.name}:0x#{object_id.to_s(16)} " \
  "completed_count=#{@completed.size} pending_count=#{@pending.size}>"
end