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
29
# File 'lib/llm/repl/buffer.rb', line 22

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

Instance Method Details

#visible(height) ⇒ Array<String>

Parameters:

  • height (Integer)

Returns:

  • (Array<String>)


106
107
108
109
110
111
# File 'lib/llm/repl/buffer.rb', line 106

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)

This method returns an undefined value.

Parameters:

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


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

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)

This method returns an undefined value.

Parameters:

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


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

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

#open

This method returns an undefined value.

Open the buffer.



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

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

#close

This method returns an undefined value.

Close the buffer.



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

def close
  @cursor = nil
  @snapshot = nil
end

#scroll_up(height)

This method returns an undefined value.



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

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

#scroll_down

This method returns an undefined value.



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

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

#scroll_to_bottom

This method returns an undefined value.



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

def scroll_to_bottom
  @offset = 0
end

#widthInteger

The width of the centered content area - with 20% on each side. The rest makes up the width of the buffer.

Returns:

  • (Integer)


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

def width
  (Curses.cols * 0.6).floor
end