Class: LLM::Console::Input Private
- Inherits:
-
Object
- Object
- LLM::Console::Input
- Defined in:
- lib/llm/console/input.rb,
lib/llm/console/input/row.rb,
lib/llm/console/input/char.rb,
lib/llm/console/input/cache.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
The LLM::Console::Input class manages the editable input line shown at the bottom of the REPL.
Defined Under Namespace
Constant Summary collapse
- CTRL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
{ A: Curses::KEY_CTRL_A, E: Curses::KEY_CTRL_E, F: Curses::KEY_CTRL_F, K: Curses::KEY_CTRL_K, Y: Curses::KEY_CTRL_Y, D: Curses::KEY_CTRL_D, P: Curses::KEY_CTRL_P, N: Curses::KEY_CTRL_N }
- REPEATS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
This hash tracks how many times a given key was pressed repeatedly without being interrupted by another key. The previous key is reset to 0 when a different key is pressed.
{}
- UP =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Curses::Key::UP
- DOWN =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Curses::Key::DOWN
- LEFT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Curses::Key::LEFT
- RIGHT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Curses::Key::RIGHT
- PGUP =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Curses::KEY_PPAGE
- PGDOWN =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Curses::KEY_NPAGE
- KEY_RESIZE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Curses::KEY_RESIZE
- TAB =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
9- ESC =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
27- ENTER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
10- BACKSPACE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The codes that arrive for the backspace key.
There is not one answer. A terminal that follows the Linux convention sends DEL (127), and the OpenBSD console sends ^H (8), because its erase character is still ^H. And because the window enables keypad, curses answers with its own KEY_BACKSPACE when a terminal's terminfo describes the key. All three erase, so all three are accepted.
[8, 127, Curses::KEY_BACKSPACE].freeze
- PASTE_THRESHOLD =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Threshold in seconds. If characters arrive faster than this, we assume the user is pasting multi-line text. Human typing is ~150–300ms per key, so 50ms reliably distinguishes a paste from manual typing.
0.05
Instance Attribute Summary collapse
- #paste writeonly private
Instance Method Summary collapse
- #paste? ⇒ Boolean private
-
#buffer ⇒ String
private
The input buffer as an ordinary string.
- #on_char(window, char, now) ⇒ Symbol? private
- #to_s ⇒ String private
- #height ⇒ Integer private
-
#lines ⇒ Array<String>
private
Returns the visible lines of the input buffer.
-
#cursor_pos ⇒ Array(Integer, Integer)
private
Returns the cursor position as [line, column] within the visible viewport.
- #move_start private
- #move_end private
- #move_left private
- #move_right private
- #move_forward private
- #autocomplete private
- #kill private
-
#delete
private
Deletes the character at the cursor, or consumes the break and pulls up the next row at the end of a row.
- #restore private
- #take ⇒ String private
- #initialize(repl, options = {}) ⇒ LLM::Console::Input constructor private
Constructor Details
#initialize(repl, options = {}) ⇒ LLM::Console::Input
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/llm/console/input.rb', line 70 def initialize(repl, = {}) @repl = repl @name = repl.name @agent = repl.agent @llm = @agent.llm.name @rows = [Row.new] @cursor = [0, 0] @scroll = 0 @height = .fetch(:height, 3) @last_char_at = nil @memory = @agent..select(&:user?).map(&:content) @walker = Walker.new(@memory) @paste = false end |
Instance Attribute Details
#paste=(value) (writeonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
65 66 67 |
# File 'lib/llm/console/input.rb', line 65 def paste=(value) @paste = value end |
Instance Method Details
#paste? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
355 356 357 |
# File 'lib/llm/console/input.rb', line 355 def paste? @paste end |
#buffer ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The input buffer as an ordinary string.
88 89 90 |
# File 'lib/llm/console/input.rb', line 88 def buffer text end |
#on_char(window, char, now) ⇒ Symbol?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/llm/console/input.rb', line 96 def on_char(window, char, now) is_paste = lambda { @last_char_at and (now - @last_char_at) < PASTE_THRESHOLD } if char and @char != char REPEATS[@char] = 0 end if PGUP == char (window.rows - 3).times { window.scroll_up } :pageup elsif PGDOWN == char (window.rows - 3).times { window.scroll_down } :pagedown elsif TAB == char autocomplete :tab elsif ESC == char @agent.cancel! ## # '@agent.cancel!' might do this for us already # but we can't rely on it alone. There are moments # in time where '@agent.cancel!' is essentially a # noop. thread = @repl.thread thread&.raise(LLM::Interrupt) if thread&.alive? elsif CTRL[:P] == char set(text: @walker.prev.dup) :ctrl_p elsif CTRL[:N] == char set(text: @walker.next.dup) :ctrl_n elsif CTRL[:D] == char delete :ctrl_d elsif CTRL[:A] == char move_start :ctrl_a elsif CTRL[:E] == char move_end :ctrl_e elsif CTRL[:F] == char move_forward :ctrl_f elsif CTRL[:Y] == char restore :ctrl_y elsif CTRL[:K] == char kill :ctrl_k elsif char == LEFT move_left :left elsif char == RIGHT move_right :right elsif BACKSPACE.include?(char) backspace :backspace elsif char == ENTER if @paste = is_paste.() insert("\n") :char else @memory.push(text) @walker.cursor = @memory.size :submit end elsif char == KEY_RESIZE window.resize :resize elsif char == UP window.scroll_up :up elsif char == DOWN window.scroll_down :down elsif String === char ## # A paste arrives as a burst of fast characters. Flag it so the # next loop iteration drains the rest through read_paste in a # single shot, instead of inserting one character at a time and # redrawing after each. A string that is already the product of # read_paste (multi-character) is inserted as-is, one char at a # time, so every newline creates a row and every Char stays a # single character. @paste = true if char.length == 1 && is_paste.() char.each_char { |c| insert(c) } :char else nil end ensure if char REPEATS[char] += 1 @last_char_at = now @char = char end end |
#to_s ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
195 196 197 198 199 200 |
# File 'lib/llm/console/input.rb', line 195 def to_s @rows .each_with_index .map { |row, i| (i.zero? ? prompt : "") + row.to_s } .join("\n") end |
#height ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
204 205 206 |
# File 'lib/llm/console/input.rb', line 204 def height @height end |
#lines ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the visible lines of the input buffer. The viewport follows the cursor so the cursor line is always visible.
212 213 214 215 216 217 218 |
# File 'lib/llm/console/input.rb', line 212 def lines scroll! visible = @rows[@scroll, height] || [] visible.each_with_index.map do |row, i| (i.zero? && @scroll.zero? ? prompt : "") + row.to_s end end |
#cursor_pos ⇒ Array(Integer, Integer)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the cursor position as [line, column] within the visible viewport.
224 225 226 227 228 229 230 |
# File 'lib/llm/console/input.rb', line 224 def cursor_pos scroll! row, col = @cursor col = Node.width(@rows[row].chars[0...col].map(&:to_s).join) col += Node.width(prompt) if row.zero? [row - @scroll, col] end |
#move_start
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
234 235 236 |
# File 'lib/llm/console/input.rb', line 234 def move_start @cursor = [0, 0] end |
#move_end
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
240 241 242 |
# File 'lib/llm/console/input.rb', line 240 def move_end @cursor = [@rows.size - 1, @rows.last.chars.size] end |
#move_left
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
246 247 248 249 250 251 252 253 |
# File 'lib/llm/console/input.rb', line 246 def move_left row, col = @cursor @cursor = if col > 0 then [row, col - 1] elsif row > 0 then [row - 1, @rows[row - 1].chars.size] else [0, 0] end end |
#move_right
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
257 258 259 260 261 262 263 264 |
# File 'lib/llm/console/input.rb', line 257 def move_right row, col = @cursor @cursor = if col < @rows[row].chars.size then [row, col + 1] elsif row < @rows.size - 1 then [row + 1, 0] else [row, col] end end |
#move_forward
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
268 269 270 |
# File 'lib/llm/console/input.rb', line 268 def move_forward move_right end |
#autocomplete
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/llm/console/input.rb', line 274 def autocomplete return unless text[0] == "/" ## # There are two kinds of an autocomplete # that can take place. The first kind # autocompletes command names, and the # second kind autocompletes command # arguments. # # Command arguments are given to the # `Command#complete(...)` method and # returns an array of potential candidates. if text.include?(" ") complete(:argument) else complete(:command) end end |
#kill
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
295 296 297 298 299 300 301 302 303 |
# File 'lib/llm/console/input.rb', line 295 def kill row, col = @cursor tail = @rows[row].chars[col..].map(&:to_s).join tail += @rows[(row + 1)..].map(&:to_s).join("\n") @copy = tail @rows[row].chars.slice!(col..) @rows = @rows[0..row] @cursor = [row, col] end |
#delete
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Deletes the character at the cursor, or consumes the break and pulls up the next row at the end of a row.
310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/llm/console/input.rb', line 310 def delete row, col = @cursor if col < @rows[row].chars.size @rows[row].chars.delete_at(col) elsif row < @rows.size - 1 nxt = @rows[row + 1] ## # A wrapped row was split at a space; restore it so # the merged words don't run together. @rows[row].chars << Char.new(" ") if nxt.break_type == :space @rows[row].chars.concat(nxt.chars) @rows.delete_at(row + 1) end end |
#restore
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/llm/console/input.rb', line 327 def restore return unless @copy row, col = @cursor @copy.each_char do |char| if char == "\n" @rows.insert(row + 1, Row.new(:newline)) row += 1 col = 0 else @rows[row].chars.insert(col, Char.new(char)) col += 1 end end @cursor = [row, col] end |