Class: LLM::Repl::Buffer

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

Overview

This class maintains conversation state that includes the conversation itself, and metadata associated with the conversation.

Internally it maintains an array where each element represents a row, and each element in a row is a Hash that describes a piece of text and any styles that might be applied to it by the UI thread.

It also maintains a cursor that tracks the active row by its index number. The streaming path reuses a single row by overwriting its contents repeatedly.

Instance Method Summary collapse

Constructor Details

#initialize(repl) ⇒ LLM::Repl::Buffer

Parameters:



22
23
24
25
26
27
28
# File 'lib/llm/repl/buffer.rb', line 22

def initialize(repl)
  @repl = repl
  @rows = [[]]
  @cursor = nil
  @snapshot = nil
  @offset = 0
end

Instance Method Details

#closevoid

This method returns an undefined value.

Close the buffer.



69
70
71
72
# File 'lib/llm/repl/buffer.rb', line 69

def close
  @cursor = nil
  @snapshot = nil
end

#openvoid

This method returns an undefined value.

Open the buffer.



61
62
63
64
# File 'lib/llm/repl/buffer.rb', line 61

def open
  @cursor = @rows.size - 1
  @snapshot = @rows.map(&:dup)
end

#scroll_downvoid

This method returns an undefined value.



83
84
85
# File 'lib/llm/repl/buffer.rb', line 83

def scroll_down
  @offset = [@offset - 1, 0].max
end

#scroll_to_bottomvoid

This method returns an undefined value.



89
90
91
# File 'lib/llm/repl/buffer.rb', line 89

def scroll_to_bottom
  @offset = 0
end

#scroll_up(height) ⇒ void

This method returns an undefined value.



76
77
78
79
# File 'lib/llm/repl/buffer.rb', line 76

def scroll_up(height)
  max = [rows.size - height, 0].max
  @offset = [@offset + 1, max].min
end

#visible(height) ⇒ Array<String>

Parameters:

  • height (Integer)

Returns:

  • (Array<String>)


96
97
98
99
100
101
# File 'lib/llm/repl/buffer.rb', line 96

def visible(height)
  all = rows
  last = all.size - 1 - @offset
  first = [last - height + 1, 0].max
  all[first..last] || []
end

#write(chars, attrs = nil, method: :append) ⇒ void

This method returns an undefined value.

Parameters:

  • chars (String, Array)
  • attrs (Object) (defaults to: nil)
  • method (Symbol) (defaults to: :append)


35
36
37
38
39
40
41
# File 'lib/llm/repl/buffer.rb', line 35

def write(chars, attrs = nil, method: :append)
  case chars
  when Array then chunks = chars
  else chunks = [Node.new(chars.to_s, attrs)]
  end
  self.method(method).call(chunks)
end

#write_message(user, content, method: :append) ⇒ void

This method returns an undefined value.

Parameters:

  • user (String)
  • content (String, Array)
  • method (Symbol) (defaults to: :append)


48
49
50
51
52
53
54
55
56
# File 'lib/llm/repl/buffer.rb', line 48

def write_message(user, content, method: :append)
  chunks = [Node.new("#{user}: ", Curses::A_BOLD)]
  case content
  when Array then chunks.concat(content)
  else chunks.push(Node.new(content))
  end
  chunks.push(Node.new("\n"))
  write(chunks, method:)
end