Class: LLM::Repl::Buffer
- Inherits:
-
Object
- Object
- LLM::Repl::Buffer
- 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
- #visible(height) ⇒ Array<String>
- #write(chars, attrs = nil, method: :append)
- #write_message(user, content, method: :append)
-
#open
Open the buffer.
-
#close
Close the buffer.
- #scroll_up(height)
- #scroll_down
- #scroll_to_bottom
-
#width ⇒ Integer
The width of the centered content area - with 20% on each side.
- #initialize(repl) ⇒ LLM::Repl::Buffer constructor
Constructor Details
#initialize(repl) ⇒ LLM::Repl::Buffer
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>
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.
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.
49 50 51 52 53 54 55 56 57 |
# File 'lib/llm/repl/buffer.rb', line 49 def (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 |
#width ⇒ Integer
The width of the centered content area - with 20% on each side. The rest makes up the width of the buffer.
99 100 101 |
# File 'lib/llm/repl/buffer.rb', line 99 def width (Curses.cols * 0.6).floor end |