Class: Kward::PromptInterface
- Inherits:
-
Object
- Object
- Kward::PromptInterface
show all
- Includes:
- ComposerController, ComposerRenderer, KeyHandler, Layout, OverlayRenderer, PromptRenderer, QuestionPrompt, RuntimeState, Screen, SelectionPrompt, SlashOverlay, TranscriptRenderer
- Defined in:
- lib/kward/prompt_interface.rb,
lib/kward/prompt_interface/banner.rb,
lib/kward/prompt_interface/layout.rb,
lib/kward/prompt_interface/screen.rb,
lib/kward/prompt_interface/key_handler.rb,
lib/kward/prompt_interface/stream_state.rb,
lib/kward/prompt_interface/runtime_state.rb,
lib/kward/prompt_interface/slash_overlay.rb,
lib/kward/prompt_interface/composer_state.rb,
lib/kward/prompt_interface/prompt_renderer.rb,
lib/kward/prompt_interface/question_prompt.rb,
lib/kward/prompt_interface/overlay_renderer.rb,
lib/kward/prompt_interface/selection_prompt.rb,
lib/kward/prompt_interface/composer_renderer.rb,
lib/kward/prompt_interface/transcript_buffer.rb,
lib/kward/prompt_interface/composer_controller.rb,
lib/kward/prompt_interface/transcript_renderer.rb
Overview
Terminal transcript rendering helpers.
Defined Under Namespace
Modules: ComposerController, ComposerRenderer, KeyHandler, Layout, OverlayRenderer, PromptRenderer, QuestionPrompt, RuntimeState, Screen, SelectionPrompt, SlashOverlay, TranscriptRenderer
Classes: Banner, ComposerState, StreamState, SubmittedInput, TranscriptBuffer
Constant Summary
collapse
- HELP_TEXT =
"Enter sends • Shift+Enter inserts newline • ↑/↓ history • Ctrl+D exits empty prompt".freeze
- BUSY_HELP_TEXT =
"Ctrl+C cancels".freeze
- SPINNER_FRAMES =
%w[⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏].freeze
- SPINNER_INTERVAL =
0.1
1.0
- COMPOSER_MAX_INPUT_ROWS =
6
- TRANSCRIPT_BUFFER_LIMIT =
200_000
- BANNER_LOGO_PIXELS =
Banner::LOGO_PIXELS
- BANNER_MESSAGE =
Banner::MESSAGE
- KEYBOARD_PROTOCOL_ENABLE =
"\e[>1u".freeze
- KEYBOARD_PROTOCOL_RESTORE =
"\e[<u".freeze
- BRACKETED_PASTE_ENABLE =
"\e[?2004h".freeze
- BRACKETED_PASTE_RESTORE =
"\e[?2004l".freeze
- BRACKETED_PASTE_START =
"\e[200~".freeze
- BRACKETED_PASTE_END =
"\e[201~".freeze
- SYNCHRONIZED_OUTPUT_ENABLE =
"\e[?2026h".freeze
- SYNCHRONIZED_OUTPUT_DISABLE =
"\e[?2026l".freeze
- CURSOR_SHOW =
"\e[?25h".freeze
- CURSOR_HIDE =
"\e[?25l".freeze
- SHIFT_ENTER_SEQUENCES =
["\e[13;2u", "\e[13;2~", "\e[27;2;13~", "\e\r", "\e\n"].freeze
- EXIT_INPUT =
:exit_input
- CANCEL_INPUT =
:cancel_input
- SELECT_CANCEL =
:select_cancel
Instance Method Summary
collapse
-
#ask(message = "You>") ⇒ Object
-
#ask_user_question(questions) ⇒ Object
-
#begin_busy_input(message = "You>", activity: "streaming") ⇒ Object
-
#clear_steered_count ⇒ Object
-
#clear_transcript ⇒ Object
-
#close ⇒ Object
-
#finish_busy_input ⇒ Object
-
#finish_stream_block ⇒ Object
-
#initialize(input: $stdin, output: $stdout, slash_commands: [], overlay_settings: nil, footer: nil, composer_status: nil, busy_help: true, attachment_badges: nil, attachment_parser: nil, banner_pixels: nil, banner_message: nil) ⇒ PromptInterface
constructor
A new instance of PromptInterface.
-
#modal_active? ⇒ Boolean
-
#poll_input ⇒ Object
-
#print_visual_banner ⇒ Object
-
#redraw ⇒ Object
-
#restore_transcript ⇒ Object
-
#say(message) ⇒ Object
-
#say_visual(message) ⇒ Object
-
#select(message, choices, title: "Sessions", custom: false, initial_index: 0) ⇒ Object
-
#set_queued_count(count) ⇒ Object
-
#set_steered_count(count) ⇒ Object
-
#start ⇒ Object
-
#start_stream_block(label) ⇒ Object
-
#update_assistant_label(label) ⇒ Object
-
#update_overlay_settings(settings) ⇒ Object
-
#write_delta(delta) ⇒ Object
-
#write_stream_block(label, delta, finish: false) ⇒ Object
-
#yes?(message, default: false) ⇒ Boolean
Constructor Details
#initialize(input: $stdin, output: $stdout, slash_commands: [], overlay_settings: nil, footer: nil, composer_status: nil, busy_help: true, attachment_badges: nil, attachment_parser: nil, banner_pixels: nil, banner_message: nil) ⇒ PromptInterface
Returns a new instance of PromptInterface.
84
85
86
87
88
89
90
91
92
93
94
95
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
|
# File 'lib/kward/prompt_interface.rb', line 84
def initialize(input: $stdin, output: $stdout, slash_commands: [], overlay_settings: nil, footer: nil, composer_status: nil, busy_help: true, attachment_badges: nil, attachment_parser: nil, banner_pixels: nil, banner_message: nil)
@input_io = input
@output_io = output
@reader = TTY::Reader.new(input: input, output: output, interrupt: :error)
@mutex = Mutex.new
@composer = ComposerState.new
self.composer_input = @composer.input
self.composer_cursor = @composer.cursor
@started = false
@asking = false
@busy = false
@busy_activity = "streaming"
@queued_count = 0
@steered_count = 0
@spinner_frame_index = 0
@last_spinner_tick = monotonic_now
@last_footer_refresh = monotonic_now
@prompt_label = "You>"
@assistant_label = "Assistant"
@stream_state = StreamState.new
@rendered_rows = 0
@last_composer_rows = []
@cursor_rendered_row = 0
@transcript_buffer = TranscriptBuffer.new(limit: TRANSCRIPT_BUFFER_LIMIT)
@visual_banner_count = 0
@transcript_viewport_rows = 0
@restoring_transcript = false
@pending_keys = []
@original_console_mode = nil
@raw_mode_active = false
@slash_commands = normalize_slash_commands(slash_commands)
@slash_selection_index = 0
@slash_overlay_dismissed_input = nil
@select_state = nil
@question_state = nil
@last_width = screen_width
@last_height = screen_height
@reserved_rows = 0
@color_enabled = ANSI.enabled?(output)
@cursor_visible = true
@synchronized_output_depth = 0
@overlay_settings = normalize_overlay_settings(overlay_settings)
@footer =
@composer_status = composer_status
@busy_help = busy_help
@attachment_badges = attachment_badges
@attachment_parser = attachment_parser
@banner = Banner.new(message: banner_message, pixels: banner_pixels, screen_height: method(:screen_height))
end
|
Instance Method Details
#ask(message = "You>") ⇒ Object
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/kward/prompt_interface.rb', line 223
def ask(message = "You>")
was_composing = @started && @asking
start
@mutex.synchronize do
preserve_input = was_composing && !@busy && !composer_input.empty?
@prompt_label = message.to_s
unless preserve_input
self.composer_input = @composer.prefill_input.to_s
@composer.prefill_input = nil
self.composer_cursor = composer_input.length
@composer.clear_attachments
reset_history_navigation
end
@pending_keys.clear
@asking = true
@busy = false
@queued_count = 0
render_prompt_locked
end
loop do
key = read_key(nonblock: true)
result = nil
@mutex.synchronize do
if key.nil?
resized = handle_resize_locked
=
render_prompt_locked if resized ||
else
result = handle_key(key)
render_prompt_locked unless result.is_a?(String) || result == EXIT_INPUT
end
end
return result if result.is_a?(String)
return nil if result == EXIT_INPUT
sleep 0.02 if key.nil?
end
end
|
#ask_user_question(questions) ⇒ Object
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
# File 'lib/kward/prompt_interface.rb', line 316
def ask_user_question(questions)
return [] if questions.empty?
start
saved_state = nil
answers = []
@mutex.synchronize { saved_state = begin_question_prompt_state }
questions.each_with_index do |question, index|
answer = ask_single_user_question(question, index + 1, questions.length)
if answer == SELECT_CANCEL
finish_question_prompt(saved_state)
return nil
end
answers << answer
end
finish_question_prompt(saved_state)
answers
rescue StandardError
finish_question_prompt(saved_state) if saved_state
raise
end
|
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
# File 'lib/kward/prompt_interface.rb', line 351
def begin_busy_input(message = "You>", activity: "streaming")
start
@mutex.synchronize do
@prompt_label = message.to_s
@busy_activity = normalize_busy_activity(activity)
self.composer_input = ""
self.composer_cursor = 0
@composer.clear_attachments
@pending_keys.clear
@asking = true
@busy = true
@queued_count = 0
@steered_count = 0
reset_spinner_locked
reset_history_navigation
render_prompt_locked
end
end
|
#clear_steered_count ⇒ Object
386
387
388
389
390
391
392
|
# File 'lib/kward/prompt_interface.rb', line 386
def clear_steered_count
@mutex.synchronize do
@steered_count = 0
@busy_activity = "streaming"
render_prompt_locked if @asking
end
end
|
#clear_transcript ⇒ Object
482
483
484
485
486
487
488
489
490
491
492
493
|
# File 'lib/kward/prompt_interface.rb', line 482
def clear_transcript
@mutex.synchronize do
@transcript_buffer.clear
@visual_banner_count = 0
@transcript_viewport_rows = 0
@stream_state.finish_block
@stream_state.reset
width, height = screen_size
with_synchronized_output_locked { redraw_screen_locked(width: width, height: height) }
@output_io.flush
end
end
|
#close ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/kward/prompt_interface.rb', line 147
def close
@mutex.synchronize do
return unless @started
clear_prompt_for_output_locked
restore_scroll_region_locked
@output_io.print(BRACKETED_PASTE_RESTORE)
@output_io.print(KEYBOARD_PROTOCOL_RESTORE)
set_cursor_visible_locked(true, force: true)
@output_io.puts
@output_io.flush
@started = false
restore_console_mode_locked
end
end
|
394
395
396
397
398
399
400
401
402
403
|
# File 'lib/kward/prompt_interface.rb', line 394
def finish_busy_input
@mutex.synchronize do
@busy = false
@busy_activity = "streaming"
@queued_count = 0
@steered_count = 0
@asking = true
render_prompt_locked
end
end
|
#finish_stream_block ⇒ Object
462
463
464
465
466
|
# File 'lib/kward/prompt_interface.rb', line 462
def finish_stream_block
@mutex.synchronize do
write_stream_block_locked(nil, "", finish: true)
end
end
|
#modal_active? ⇒ Boolean
340
341
342
|
# File 'lib/kward/prompt_interface.rb', line 340
def modal_active?
@mutex.synchronize { !@question_state.nil? || !@select_state.nil? }
end
|
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
# File 'lib/kward/prompt_interface.rb', line 405
def poll_input
key = read_key(nonblock: true)
@mutex.synchronize do
if key.nil?
resized = handle_resize_locked
spun = tick_spinner_locked
=
render_prompt_locked if resized || spun ||
return nil
end
result = handle_key(key)
render_prompt_locked unless [EXIT_INPUT, CANCEL_INPUT].include?(result)
[EXIT_INPUT, CANCEL_INPUT].include?(result) ? result : result
end
end
|
#print_visual_banner ⇒ Object
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
# File 'lib/kward/prompt_interface.rb', line 428
def print_visual_banner
@mutex.synchronize do
width, height = screen_size
rows = banner_rows(width)
return if rows.empty?
with_synchronized_output_locked do
prepare_transcript_output_locked
rows.each do |row|
write_visual_transcript_text_locked(row)
write_visual_transcript_text_locked("\n")
end
@visual_banner_count += 1
invalidate_transcript_display_rows_cache
remember_transcript_viewport_locked(height)
@stream_state.finish_block
restore_composer_cursor_locked
end
@output_io.flush
end
end
|
#redraw ⇒ Object
474
475
476
477
478
479
480
|
# File 'lib/kward/prompt_interface.rb', line 474
def redraw
@mutex.synchronize do
width, height = screen_size
with_synchronized_output_locked { redraw_screen_locked(width: width, height: height) }
@output_io.flush
end
end
|
#restore_transcript ⇒ Object
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
# File 'lib/kward/prompt_interface.rb', line 200
def restore_transcript
@mutex.synchronize do
clear_prompt_for_output_locked
@output_io.print(SYNCHRONIZED_OUTPUT_ENABLE)
@transcript_buffer.clear
@visual_banner_count = 0
@transcript_viewport_rows = 0
@stream_state.finish_block
@stream_state.reset
@restoring_transcript = true
end
yield
ensure
@mutex.synchronize do
@restoring_transcript = false
@output_io.print(SYNCHRONIZED_OUTPUT_DISABLE)
width, height = screen_size
redraw_screen_locked(width: width, height: height)
@output_io.flush
end
end
|
#say(message) ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/kward/prompt_interface.rb', line 163
def say(message)
@mutex.synchronize do
text = message.to_s
if @restoring_transcript
write_transcript_text_locked(text)
write_transcript_text_locked("\n") unless text.end_with?("\n")
@stream_state.finish_block
next
end
with_synchronized_output_locked do
clear_prompt_for_output_locked
write_transcript_text_locked(text)
write_transcript_text_locked("\n") unless text.end_with?("\n")
@stream_state.finish_block
render_prompt_after_output_locked
end
@output_io.flush
end
end
|
#say_visual(message) ⇒ Object
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/kward/prompt_interface.rb', line 184
def say_visual(message)
@mutex.synchronize do
return if @restoring_transcript
with_synchronized_output_locked do
clear_prompt_for_output_locked
text = message.to_s
write_visual_transcript_text_locked(text)
write_visual_transcript_text_locked("\n") unless text.end_with?("\n")
@stream_state.finish_block
render_prompt_after_output_locked
end
@output_io.flush
end
end
|
#select(message, choices, title: "Sessions", custom: false, initial_index: 0) ⇒ Object
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
# File 'lib/kward/prompt_interface.rb', line 273
def select(message, choices, title: "Sessions", custom: false, initial_index: 0)
return nil if choices.empty? && !custom
start
@mutex.synchronize do
@prompt_label = message.to_s
self.composer_input = ""
self.composer_cursor = 0
@composer.clear_attachments
@pending_keys.clear
@asking = true
@busy = false
@queued_count = 0
choice_labels = choices.map(&:to_s)
selection_index = choice_labels.empty? ? 0 : [[initial_index.to_i, 0].max, choice_labels.length - 1].min
@select_state = { choices: choice_labels, selection_index: selection_index, title: title.to_s, custom: custom }
reset_history_navigation
render_prompt_locked
end
loop do
key = read_key(nonblock: true)
result = nil
@mutex.synchronize do
if key.nil?
resized = handle_resize_locked
=
render_prompt_locked if resized ||
else
result = handle_select_key(key)
render_prompt_locked unless result.is_a?(String) || result == SELECT_CANCEL
end
end
if result.is_a?(String) || result == SELECT_CANCEL
finish_select_prompt
return result == SELECT_CANCEL ? nil : result
end
sleep 0.02 if key.nil?
end
end
|
#set_queued_count(count) ⇒ Object
370
371
372
373
374
375
376
|
# File 'lib/kward/prompt_interface.rb', line 370
def set_queued_count(count)
@mutex.synchronize do
@queued_count = count.to_i
@steered_count = 0 if @queued_count.positive?
render_prompt_locked if @asking
end
end
|
#set_steered_count(count) ⇒ Object
378
379
380
381
382
383
384
|
# File 'lib/kward/prompt_interface.rb', line 378
def set_steered_count(count)
@mutex.synchronize do
@steered_count = count.to_i
@queued_count = 0 if @steered_count.positive?
render_prompt_locked if @asking
end
end
|
#start ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/kward/prompt_interface.rb', line 134
def start
@mutex.synchronize do
return if @started
enter_raw_mode_locked
@started = true
@asking = true
@output_io.print(KEYBOARD_PROTOCOL_ENABLE)
@output_io.print(BRACKETED_PASTE_ENABLE)
render_prompt_locked
end
end
|
#start_stream_block(label) ⇒ Object
450
451
452
453
454
|
# File 'lib/kward/prompt_interface.rb', line 450
def start_stream_block(label)
@mutex.synchronize do
write_stream_block_locked(label, "", finish: false)
end
end
|
#update_assistant_label(label) ⇒ Object
422
423
424
425
426
|
# File 'lib/kward/prompt_interface.rb', line 422
def update_assistant_label(label)
@mutex.synchronize do
@assistant_label = label.to_s.empty? ? "Assistant" : label.to_s
end
end
|
#update_overlay_settings(settings) ⇒ Object
344
345
346
347
348
349
|
# File 'lib/kward/prompt_interface.rb', line 344
def update_overlay_settings(settings)
@mutex.synchronize do
@overlay_settings = normalize_overlay_settings(settings)
render_prompt_locked if @started && @asking
end
end
|
#write_delta(delta) ⇒ Object
456
457
458
459
460
|
# File 'lib/kward/prompt_interface.rb', line 456
def write_delta(delta)
@mutex.synchronize do
write_stream_block_locked(nil, delta.to_s, finish: false)
end
end
|
#write_stream_block(label, delta, finish: false) ⇒ Object
468
469
470
471
472
|
# File 'lib/kward/prompt_interface.rb', line 468
def write_stream_block(label, delta, finish: false)
@mutex.synchronize do
write_stream_block_locked(label, delta.to_s, finish: finish)
end
end
|
#yes?(message, default: false) ⇒ Boolean
263
264
265
266
267
268
269
270
271
|
# File 'lib/kward/prompt_interface.rb', line 263
def yes?(message, default: false)
answer = ask("#{message} #{default ? "[Y/n]" : "[y/N]"}")
return default if answer.nil?
answer = answer.strip.downcase
return default if answer.empty?
answer.start_with?("y")
end
|