kward 0.85.0 → 0.86.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +130 -0
  3. data/CONTRIBUTING.md +1 -1
  4. data/Gemfile.lock +6 -2
  5. data/README.md +3 -3
  6. data/Rakefile +3 -0
  7. data/doc/agent-tools.md +11 -0
  8. data/doc/api.md +4 -1
  9. data/doc/composer.md +4 -3
  10. data/doc/configuration.md +60 -11
  11. data/doc/extensibility.md +2 -0
  12. data/doc/getting-started.md +3 -3
  13. data/doc/git.md +1 -1
  14. data/doc/memory.md +1 -1
  15. data/doc/plugins.md +40 -5
  16. data/doc/providers.md +10 -4
  17. data/doc/rpc.md +125 -7
  18. data/doc/session-management.md +36 -1
  19. data/doc/shell.md +643 -27
  20. data/doc/tabs.md +2 -1
  21. data/doc/troubleshooting.md +1 -1
  22. data/doc/usage.md +21 -11
  23. data/examples/plugins/openrouter_proxy/README.md +103 -0
  24. data/examples/plugins/openrouter_proxy/fake_embeddings.rb +85 -0
  25. data/examples/plugins/openrouter_proxy/plugin.rb +7 -0
  26. data/examples/plugins/openrouter_proxy/protocol.rb +202 -0
  27. data/examples/plugins/openrouter_proxy/server.rb +186 -0
  28. data/examples/plugins/space_invaders.rb +0 -1
  29. data/exe/kward +2 -2
  30. data/exe/kwsh +7 -0
  31. data/kward.gemspec +3 -1
  32. data/lib/kward/agent.rb +43 -9
  33. data/lib/kward/cli/commands.rb +41 -11
  34. data/lib/kward/cli/interactive_turn.rb +10 -0
  35. data/lib/kward/cli/plugins.rb +5 -1
  36. data/lib/kward/cli/prompt_interface.rb +4 -1
  37. data/lib/kward/cli/rendering.rb +21 -28
  38. data/lib/kward/cli/runtime_helpers.rb +52 -34
  39. data/lib/kward/cli/settings/model.rb +1 -12
  40. data/lib/kward/cli/tabs.rb +91 -18
  41. data/lib/kward/cli/tool_summaries.rb +2 -0
  42. data/lib/kward/cli/transcript_formatter.rb +3 -10
  43. data/lib/kward/cli/worktrees.rb +1 -0
  44. data/lib/kward/cli.rb +56 -7
  45. data/lib/kward/config/core.rb +16 -5
  46. data/lib/kward/config/extensions.rb +3 -0
  47. data/lib/kward/config/paths.rb +11 -0
  48. data/lib/kward/config/settings.rb +7 -0
  49. data/lib/kward/conversation.rb +11 -1
  50. data/lib/kward/entrypoint.rb +14 -0
  51. data/lib/kward/events.rb +2 -0
  52. data/lib/kward/generated_images.rb +237 -0
  53. data/lib/kward/model/catalog.rb +6 -3
  54. data/lib/kward/model/client.rb +105 -36
  55. data/lib/kward/model/codex_images.rb +83 -0
  56. data/lib/kward/model/copilot_models.rb +8 -4
  57. data/lib/kward/model/model_info.rb +27 -9
  58. data/lib/kward/model/openrouter_model_cache.rb +4 -2
  59. data/lib/kward/model/payloads.rb +6 -5
  60. data/lib/kward/model/sources.rb +3 -1
  61. data/lib/kward/model/stream_parser.rb +4 -7
  62. data/lib/kward/pan/index.html.erb +8 -1
  63. data/lib/kward/pan/server.rb +13 -1
  64. data/lib/kward/plugins/codex_responses.rb +73 -0
  65. data/lib/kward/plugins/host.rb +16 -0
  66. data/lib/kward/plugins/http_server.rb +83 -0
  67. data/lib/kward/plugins/registry.rb +39 -0
  68. data/lib/kward/prompt_interface/composer_controller.rb +0 -1
  69. data/lib/kward/prompt_interface/composer_renderer.rb +159 -72
  70. data/lib/kward/prompt_interface/editor/controller.rb +1 -1
  71. data/lib/kward/prompt_interface/editor/renderer.rb +10 -7
  72. data/lib/kward/prompt_interface/file_overlay.rb +3 -3
  73. data/lib/kward/prompt_interface/generated_image_preview.rb +162 -0
  74. data/lib/kward/prompt_interface/git_prompt.rb +18 -11
  75. data/lib/kward/prompt_interface/interactive/renderer.rb +1 -1
  76. data/lib/kward/prompt_interface/interactive/state.rb +18 -20
  77. data/lib/kward/prompt_interface/key_handler.rb +108 -25
  78. data/lib/kward/prompt_interface/layout.rb +11 -1
  79. data/lib/kward/prompt_interface/overlay_renderer.rb +25 -8
  80. data/lib/kward/prompt_interface/project_browser.rb +27 -54
  81. data/lib/kward/prompt_interface/prompt_renderer.rb +27 -35
  82. data/lib/kward/prompt_interface/question_prompt.rb +19 -28
  83. data/lib/kward/prompt_interface/runtime_state.rb +31 -9
  84. data/lib/kward/prompt_interface/screen.rb +24 -21
  85. data/lib/kward/prompt_interface/selection_prompt.rb +8 -8
  86. data/lib/kward/prompt_interface/slash_overlay.rb +1 -1
  87. data/lib/kward/prompt_interface/transcript_renderer.rb +7 -6
  88. data/lib/kward/prompt_interface.rb +109 -39
  89. data/lib/kward/prompts.rb +7 -1
  90. data/lib/kward/pty/adaptive_output_sink.rb +38 -4
  91. data/lib/kward/pty/interactive_runner.rb +20 -28
  92. data/lib/kward/pty/transcript_normalizer.rb +65 -21
  93. data/lib/kward/rpc/config_manager.rb +3 -0
  94. data/lib/kward/rpc/server.rb +27 -5
  95. data/lib/kward/rpc/session_manager.rb +62 -17
  96. data/lib/kward/rpc/transcript_normalizer.rb +11 -7
  97. data/lib/kward/sessions/store.rb +30 -3
  98. data/lib/kward/shell/kwsh.rb +27 -7
  99. data/lib/kward/shell/kwshrc.rb +11 -2
  100. data/lib/kward/shell/native_arithmetic.rb +255 -0
  101. data/lib/kward/shell/native_arrays.rb +102 -0
  102. data/lib/kward/shell/native_braces.rb +96 -0
  103. data/lib/kward/shell/native_builtins.rb +476 -0
  104. data/lib/kward/shell/native_completion.rb +169 -0
  105. data/lib/kward/shell/native_condition.rb +75 -0
  106. data/lib/kward/shell/native_directory.rb +116 -0
  107. data/lib/kward/shell/native_exec_state.rb +73 -0
  108. data/lib/kward/shell/native_expansion.rb +252 -0
  109. data/lib/kward/shell/native_fields.rb +62 -0
  110. data/lib/kward/shell/native_glob.rb +96 -0
  111. data/lib/kward/shell/native_history.rb +242 -0
  112. data/lib/kward/shell/native_interactive.rb +164 -0
  113. data/lib/kward/shell/native_invocation.rb +42 -0
  114. data/lib/kward/shell/native_jobs.rb +250 -0
  115. data/lib/kward/shell/native_line_editor.rb +382 -0
  116. data/lib/kward/shell/native_line_terminal.rb +286 -0
  117. data/lib/kward/shell/native_locale.rb +109 -0
  118. data/lib/kward/shell/native_locals.rb +57 -0
  119. data/lib/kward/shell/native_pattern.rb +200 -0
  120. data/lib/kward/shell/native_process.rb +279 -0
  121. data/lib/kward/shell/native_program.rb +40 -0
  122. data/lib/kward/shell/native_programmable_completion.rb +115 -0
  123. data/lib/kward/shell/native_prompt_format.rb +89 -0
  124. data/lib/kward/shell/native_runtime.rb +964 -0
  125. data/lib/kward/shell/native_substitution.rb +110 -0
  126. data/lib/kward/shell/native_syntax.rb +946 -0
  127. data/lib/kward/shell/native_traps.rb +196 -0
  128. data/lib/kward/shell/native_vi.rb +407 -0
  129. data/lib/kward/shell/native_vi_terminal.rb +19 -0
  130. data/lib/kward/shell/native_worker.rb +82 -0
  131. data/lib/kward/shell/persistent_session.rb +184 -190
  132. data/lib/kward/shell/prompt.rb +14 -0
  133. data/lib/kward/shell/prompt_agent.rb +17 -0
  134. data/lib/kward/shell/script_runner.rb +244 -0
  135. data/lib/kward/shell/standalone_agent.rb +31 -0
  136. data/lib/kward/shell/standalone_link.rb +78 -0
  137. data/lib/kward/shell/standalone_terminal.rb +287 -0
  138. data/lib/kward/terminal/keys.rb +22 -1
  139. data/lib/kward/terminal/locale_text.rb +29 -0
  140. data/lib/kward/terminal/sequences.rb +45 -4
  141. data/lib/kward/tools/datetime.rb +85 -0
  142. data/lib/kward/tools/generate_image.rb +36 -0
  143. data/lib/kward/tools/registry.rb +35 -9
  144. data/lib/kward/tools/tool_call.rb +3 -1
  145. data/lib/kward/transcripts/markdown_transcript.rb +8 -0
  146. data/lib/kward/transcripts/turn_summary_formatter.rb +93 -0
  147. data/lib/kward/transport/gateway.rb +2 -1
  148. data/lib/kward/turn_summary.rb +68 -0
  149. data/lib/kward/version.rb +1 -1
  150. data/lib/main.rb +6 -3
  151. metadata +84 -1
@@ -292,7 +292,7 @@ module Kward
292
292
  true
293
293
  end
294
294
 
295
- def project_browser_delete_confirmation_rows(width)
295
+ def project_browser_delete_confirmation_rows(width, height:)
296
296
  confirmation = @project_browser_state[:delete_confirmation]
297
297
  path = confirmation[:path]
298
298
  lines = [overlay_text_line("Delete #{confirmation[:directory] ? "directory" : "file"}?"), overlay_blank_line]
@@ -303,7 +303,7 @@ module Kward
303
303
  lines << overlay_text_line(path)
304
304
  lines << overlay_text_line("Press Backspace again to confirm • Esc to cancel", :muted)
305
305
  end
306
- overlay_card_rows("Confirm deletion", lines, width)
306
+ overlay_card_rows("Confirm deletion", lines, width, height: height)
307
307
  end
308
308
 
309
309
  def project_browser_name_entry_active?
@@ -656,65 +656,38 @@ module Kward
656
656
  end
657
657
 
658
658
  def open_project_browser_image(path)
659
- full_path = File.expand_path(path.to_s, prompt_workspace_root)
660
- part = Kward::ImageAttachments.image_part_from_path(full_path)
661
- unless part
662
- @project_browser_state[:status] = "Cannot preview #{path}: unreadable or larger than 20 MB"
659
+ state = image_viewer_state_for(File.expand_path(path.to_s, prompt_workspace_root), path.to_s)
660
+ if state[:preview_error]
661
+ @project_browser_state[:status] = "Cannot preview #{path}: #{state[:preview_error]}"
663
662
  return false
664
663
  end
665
664
 
666
- protocol = terminal_image_protocol
667
- unless protocol
668
- @project_browser_state[:status] = "Cannot preview #{path}: terminal does not support inline images"
669
- return false
670
- end
671
-
672
- part = Kward::ImageAttachments.terminal_image_part(part, protocol)
673
- unless part
674
- @project_browser_state[:status] = "Cannot preview #{path}: terminal cannot render this image format"
675
- return false
676
- end
677
-
678
- image_id = next_terminal_image_id
679
- columns = image_viewer_columns(screen_width)
680
- rows = image_viewer_preview_rows(screen_height, width: screen_width)
681
- terminal_columns, terminal_rows = image_viewer_terminal_size(part, protocol, columns, rows)
682
- sequence = Kward::ImageAttachments.terminal_image_sequence(
683
- part,
684
- width: terminal_columns,
685
- height: terminal_rows,
686
- protocol: protocol,
687
- image_id: image_id,
688
- move_cursor: false,
689
- env: ENV
690
- )
691
- unless sequence
692
- @project_browser_state[:status] = "Cannot preview #{path}: terminal cannot render this image format"
693
- return false
694
- end
695
-
696
- @image_viewer_state = {
697
- path: full_path,
698
- display_path: path.to_s,
699
- protocol: protocol,
700
- image_id: image_id,
701
- part: part,
702
- zoom: 1.0,
703
- columns: columns,
704
- rows: rows,
705
- sequence: sequence
706
- }
665
+ @image_viewer_state = state
707
666
  @pending_keys.clear
708
667
  @asking = true
709
668
  true
710
669
  end
711
670
 
671
+ def image_viewer_state_for(path, display_path)
672
+ state = { path: path, display_path: display_path, zoom: 1.0 }
673
+ part = ImageAttachments.image_part_from_path(path)
674
+ return state.merge(preview_error: "unreadable or larger than 20 MB") unless part
675
+
676
+ protocol = terminal_image_protocol
677
+ return state.merge(preview_error: "terminal does not support inline images") unless protocol
678
+
679
+ part = ImageAttachments.terminal_image_part(part, protocol)
680
+ return state.merge(preview_error: "terminal cannot render this image format") unless part
681
+
682
+ state.merge(protocol: protocol, part: part, image_id: next_terminal_image_id)
683
+ end
684
+
712
685
  def close_image_viewer
713
686
  return false unless image_viewer_active?
714
687
 
715
688
  clear_image_viewer_output_locked
689
+ sync_project_browser_query_input unless @image_viewer_state[:generated]
716
690
  @image_viewer_state = nil
717
- sync_project_browser_query_input
718
691
  @asking = true
719
692
  true
720
693
  end
@@ -739,7 +712,7 @@ module Kward
739
712
  end
740
713
 
741
714
  def zoom_image_viewer(direction)
742
- return false unless image_viewer_active?
715
+ return false unless image_viewer_active? && !@image_viewer_state[:preview_error]
743
716
 
744
717
  delta = direction == :in ? IMAGE_VIEWER_ZOOM_STEP : -IMAGE_VIEWER_ZOOM_STEP
745
718
  zoom = [[image_viewer_zoom + delta, IMAGE_VIEWER_MIN_ZOOM].max, IMAGE_VIEWER_MAX_ZOOM].min
@@ -754,9 +727,8 @@ module Kward
754
727
  [desired_columns, available_columns].min
755
728
  end
756
729
 
757
- def image_viewer_sequence(width, height)
730
+ def image_viewer_sequence(width, rows)
758
731
  columns = image_viewer_columns(width)
759
- rows = image_viewer_preview_rows(height, width: width)
760
732
  state = @image_viewer_state
761
733
  return state[:sequence] if state[:columns] == columns && state[:rows] == rows
762
734
 
@@ -883,7 +855,7 @@ module Kward
883
855
 
884
856
  def project_browser_rows(width, height: screen_height)
885
857
  return [] unless project_browser_visible?
886
- return project_browser_delete_confirmation_rows(width) if project_browser_delete_confirmation_active?
858
+ return project_browser_delete_confirmation_rows(width, height: height) if project_browser_delete_confirmation_active?
887
859
 
888
860
  rows = project_browser_visible_rows
889
861
  lines = []
@@ -900,7 +872,7 @@ module Kward
900
872
  lines << overlay_text_line(@project_browser_state[:status], :muted) if @project_browser_state[:status]
901
873
  lines << overlay_blank_line
902
874
  lines << overlay_text_line(project_browser_help_text, :muted)
903
- overlay_card_rows(title, lines, width)
875
+ overlay_card_rows(title, lines, width, height: height)
904
876
  end
905
877
 
906
878
  def project_browser_title
@@ -1147,7 +1119,8 @@ module Kward
1147
1119
  composer_bottom_rows = @tabs.empty? ? 1 : tab_border_rows(width).length
1148
1120
  footer_rows = footer_text.empty? ? 0 : 1
1149
1121
  attachment_rows = attachment_badge_texts.length
1150
- [height - 7 - composer_bottom_rows - footer_rows - attachment_rows, 4].max
1122
+ status_rows = @project_browser_state&.[](:status) ? 1 : 0
1123
+ [composer_row_budget(height) - 6 - composer_bottom_rows - footer_rows - attachment_rows - status_rows, 4].max
1151
1124
  end
1152
1125
 
1153
1126
  def default_project_browser_expanded_paths(paths)
@@ -6,44 +6,36 @@ module Kward
6
6
  module PromptRenderer
7
7
  private
8
8
 
9
- def render_prompt_locked(synchronized: false)
9
+ def render_prompt_locked
10
10
  return unless @started && @asking && !terminal_owned_by_child_locked?
11
11
 
12
12
  width, height = screen_size
13
- if width != @last_width || height != @last_height
14
- with_synchronized_output_locked { render_prompt_body_locked }
15
- flush_output_locked
16
- return
13
+ unless handle_resize_locked(width: width, height: height)
14
+ rows, cursor_row, cursor_col = composer_layout(width, height)
15
+ if composer_frame_needs_sync?(rows, cursor_row)
16
+ with_synchronized_output_locked { render_prompt_layout_locked(rows, cursor_row, cursor_col, width, height) }
17
+ else
18
+ render_prompt_layout_locked(rows, cursor_row, cursor_col, width, height)
19
+ end
17
20
  end
18
-
19
- rows, cursor_row, cursor_col = composer_layout(width, height)
20
- synchronized ||= rows.length != @reserved_rows
21
- if synchronized
22
- with_synchronized_output_locked { render_prompt_layout_locked(rows, cursor_row, cursor_col, width, height) }
23
- else
24
- render_prompt_layout_locked(rows, cursor_row, cursor_col, width, height)
25
- end
26
- flush_output_locked
21
+ flush_output_locked unless @restoring_transcript || @synchronized_output_depth.positive?
27
22
  end
28
23
 
29
- def render_prompt_body_locked
30
- return if terminal_owned_by_child_locked?
24
+ def composer_frame_needs_sync?(rows, cursor_row)
25
+ return true if rows.length != @reserved_rows
31
26
 
32
- handle_resize_locked
33
- width, height = screen_size
34
- rows, cursor_row, cursor_col = composer_layout(width, height)
35
- render_prompt_layout_locked(rows, cursor_row, cursor_col, width, height)
27
+ # Keep ordinary typing fast, but don't expose cursor travel to other rows.
28
+ rows.each_index.any? { |index| index != cursor_row && rows[index] != @last_composer_rows[index] }
36
29
  end
37
30
 
38
31
  def render_prompt_layout_locked(rows, cursor_row, cursor_col, width, height)
39
32
  ensure_scroll_region_locked(rows.length, width: width, height: height)
40
33
  @rendered_rows = rows.length
41
34
  render_composer_rows_locked(rows, height: height)
42
- @cursor_rendered_row = cursor_row
43
35
  @last_width = width
44
36
  @last_height = height
45
- move_to_screen(composer_top_row(height) + cursor_row, cursor_col + 1)
46
- render_cursor_visibility_locked
37
+ @rendered_composer_cursor = [composer_top_row(height) + cursor_row, cursor_col + 1]
38
+ restore_composer_cursor_locked
47
39
  end
48
40
 
49
41
  def render_prompt_after_output_locked
@@ -57,7 +49,6 @@ module Kward
57
49
  width, height = screen_size
58
50
  clear_composer_region_locked(height: height)
59
51
  @rendered_rows = 0
60
- @cursor_rendered_row = 0
61
52
  redraw_transcript_locked(width: width, height: height)
62
53
  end
63
54
 
@@ -69,7 +60,6 @@ module Kward
69
60
  reserve_composer_region_locked(width: width, height: height) if @started && @asking
70
61
  clear_composer_region_locked(height: height)
71
62
  @rendered_rows = 0
72
- @cursor_rendered_row = 0
73
63
  move_to_transcript_cursor_locked(width: width, height: height) if @started
74
64
  end
75
65
 
@@ -78,17 +68,22 @@ module Kward
78
68
 
79
69
  handle_resize_locked
80
70
  width, height = screen_size
71
+ # Stream against the painted frame, not a layout that may have unpainted status changes.
72
+ if @started && @asking
73
+ render_prompt_locked if @last_composer_rows.empty?
74
+ elsif @reserved_rows.zero?
75
+ reserve_composer_region_locked(width: width, height: height)
76
+ end
77
+ return if transcript_region_hidden?(height)
78
+
81
79
  hide_cursor_for_transcript_output_locked
82
- reserve_composer_region_locked(width: width, height: height)
83
80
  move_to_transcript_cursor_locked(width: width, height: height)
84
81
  end
85
82
 
86
83
  def restore_composer_cursor_locked
87
- return unless @started && @asking && !terminal_owned_by_child_locked?
84
+ return unless @started && @asking && @rendered_composer_cursor && !terminal_owned_by_child_locked?
88
85
 
89
- width, height = screen_size
90
- _rows, cursor_row, cursor_col = composer_layout(width, height)
91
- move_to_screen(composer_top_row(height) + cursor_row, cursor_col + 1)
86
+ move_to_screen(*@rendered_composer_cursor)
92
87
  render_cursor_visibility_locked
93
88
  end
94
89
 
@@ -106,14 +101,11 @@ module Kward
106
101
  redraw_transcript_locked(width: width, height: height)
107
102
  @rendered_rows = @asking ? rows.length : 0
108
103
  render_composer_rows_locked(rows, height: height) if @asking
109
- @cursor_rendered_row = @asking ? cursor_row : 0
110
104
  @last_width = width
111
105
  @last_height = height
112
106
  reset_stream_position_from_transcript_locked(width)
113
- if @asking
114
- move_to_screen(composer_top_row(height) + cursor_row, cursor_col + 1)
115
- render_cursor_visibility_locked
116
- end
107
+ @rendered_composer_cursor = @asking ? [composer_top_row(height) + cursor_row, cursor_col + 1] : nil
108
+ restore_composer_cursor_locked
117
109
  end
118
110
 
119
111
  end
@@ -26,8 +26,8 @@ module Kward
26
26
  @mutex.synchronize do
27
27
  if key.nil?
28
28
  resized = handle_resize_locked
29
- footer_refreshed = tick_footer_locked
30
- render_prompt_locked if resized || footer_refreshed
29
+ chrome_changed = tick_composer_chrome_locked
30
+ render_prompt_locked if resized || chrome_changed
31
31
  else
32
32
  result = handle_question_key(key)
33
33
  result = drain_pending_question_keys_locked(result)
@@ -309,33 +309,24 @@ module Kward
309
309
  end
310
310
 
311
311
  def question_composer_layout(width, height = screen_height)
312
- content_width = [width - 4, 1].max
313
- overlay_rows = active_overlay_rows(width, height: height)
314
- return question_custom_composer_layout(width, height, overlay_rows, content_width) if selected_question_choice&.fetch(:custom, false)
315
-
316
- rows = overlay_rows + [top_border(width), box_content_row("", content_width)]
317
- rows.concat(question_bottom_border_rows(width))
318
- [rows, overlay_rows.length + 1, 2]
319
- end
320
-
321
- def question_custom_composer_layout(width, height, overlay_rows, content_width)
322
- input_layout_rows, input_cursor_row, input_cursor_col = input_layout(content_width)
323
- max_input_rows = max_visible_input_rows(0, overlay_rows.length, 0, height: height)
324
- visible_start = [[input_cursor_row - max_input_rows + 1, 0].max, [input_layout_rows.length - max_input_rows, 0].max].min
325
- visible_rows = input_layout_rows[visible_start, max_input_rows] || [""]
326
- rows = overlay_rows + [top_border(width)]
327
- rows.concat(visible_rows.map { |row| box_content_row(row, content_width) })
328
- rows.concat(question_bottom_border_rows(width))
329
- cursor_row = overlay_rows.length + 1 + input_cursor_row - visible_start
330
- cursor_col = 2 + [input_cursor_col, content_width - 1].min
331
- [rows, cursor_row, cursor_col]
332
- end
333
-
334
- def question_bottom_border_rows(width)
335
- @tabs.empty? ? [bottom_border(width)] : tab_border_rows(width)
312
+ overlay_rows = question_overlay_rows(width, height: height)
313
+ available_rows = composer_row_budget(height) - overlay_rows.length
314
+ return compact_composer_layout(width, overlay_rows) if available_rows < 3
315
+
316
+ content_width = width - 4
317
+ bottom_rows = composer_bottom_rows(width, available_rows - 2)
318
+ if selected_question_choice&.fetch(:custom, false)
319
+ max_input_rows = [COMPOSER_MAX_INPUT_ROWS, available_rows - 1 - bottom_rows.length].min
320
+ input_rows, cursor_row, cursor_col = visible_composer_input(content_width, max_input_rows)
321
+ else
322
+ input_rows = [box_content_row("", content_width)]
323
+ cursor_row, cursor_col = 0, 2
324
+ end
325
+ rows = overlay_rows + [top_border(width)] + input_rows + bottom_rows
326
+ [rows, overlay_rows.length + 1 + cursor_row, cursor_col]
336
327
  end
337
328
 
338
- def question_overlay_rows(width)
329
+ def question_overlay_rows(width, height: screen_height)
339
330
  title = "Question #{@question_state[:index]}/#{@question_state[:total]} · #{@question_state[:header]}"
340
331
  content_width = [overlay_card_width(width) - 4, 1].max
341
332
  lines = question_text_rows(@question_state[:question], content_width, :bold)
@@ -347,7 +338,7 @@ module Kward
347
338
  selected = index == question_selection_index
348
339
  lines << overlay_choice_line(choice_text(choice, selected: selected), selected: selected)
349
340
  end
350
- overlay_card_rows(title, lines, width)
341
+ overlay_card_rows(title, lines, width, height: height)
351
342
  end
352
343
 
353
344
  def question_text_rows(text, width, style)
@@ -35,7 +35,7 @@ module Kward
35
35
 
36
36
  def spinner_active?
37
37
  busy = @busy || (@select_state && @select_state[:busy_activity])
38
- busy && @queued_count.zero? && @started && @asking
38
+ busy && (@queued_count.zero? || @busy_activity == "Generating image…") && @started && @asking
39
39
  end
40
40
 
41
41
  def completion_status(status)
@@ -87,8 +87,16 @@ module Kward
87
87
  true
88
88
  end
89
89
 
90
+ def tick_composer_chrome_locked
91
+ return false unless @started && @asking && !terminal_owned_by_child_locked?
92
+
93
+ footer_changed = tick_footer_locked
94
+ status_changed = tick_composer_status_locked
95
+ footer_changed || status_changed
96
+ end
97
+
90
98
  def tick_footer_locked
91
- return false unless @footer && @started && @asking
99
+ return false unless @footer
92
100
  return false unless footer_refresh_due?
93
101
 
94
102
  previous_text = @cached_footer_text
@@ -120,19 +128,33 @@ module Kward
120
128
  arity.zero? ? @footer.call : @footer.call(max_width)
121
129
  end
122
130
 
131
+ def tick_composer_status_locked
132
+ return false unless @composer_status && composer_status_refresh_due?
133
+ return false if editor_visible? || screen_height < 4
134
+
135
+ previous_text = @cached_composer_status_text
136
+ refresh_composer_status_text_locked
137
+ previous_text != @cached_composer_status_text
138
+ end
139
+
123
140
  def cached_composer_status_text
124
141
  return nil unless @composer_status
125
142
 
126
- now = monotonic_now
127
- elapsed = now - @last_composer_status_refresh.to_f
128
- if @cached_composer_status_text.nil? || elapsed >= COMPOSER_STATUS_REFRESH_INTERVAL
129
- text = @composer_status.call.to_s
130
- @cached_composer_status_text = text.empty? ? nil : status_composer_text(text)
131
- @last_composer_status_refresh = now
132
- end
143
+ refresh_composer_status_text_locked if composer_status_refresh_due?
133
144
  @cached_composer_status_text
145
+ end
146
+
147
+ def composer_status_refresh_due?
148
+ @last_composer_status_refresh.nil? || monotonic_now - @last_composer_status_refresh >= COMPOSER_STATUS_REFRESH_INTERVAL
149
+ end
150
+
151
+ def refresh_composer_status_text_locked
152
+ text = @composer_status.call.to_s
153
+ @cached_composer_status_text = text.empty? ? nil : status_composer_text(text)
134
154
  rescue StandardError
135
155
  @cached_composer_status_text = nil
156
+ ensure
157
+ @last_composer_status_refresh = monotonic_now
136
158
  end
137
159
 
138
160
  def monotonic_now
@@ -111,7 +111,8 @@ module Kward
111
111
  end
112
112
 
113
113
  def ensure_scroll_region_locked(row_count, redraw_transcript: true, width: screen_width, height: screen_height)
114
- new_reserved_rows = [[row_count, 1].max, [height - 1, 1].max].min
114
+ row_limit = editor_visible? ? height : height - 1
115
+ new_reserved_rows = row_count.clamp(1, row_limit)
115
116
  return if @reserved_rows == new_reserved_rows && @last_height == height
116
117
 
117
118
  old_reserved_rows = @reserved_rows
@@ -119,37 +120,38 @@ module Kward
119
120
  @reserved_rows = new_reserved_rows
120
121
  new_top = composer_top_row(height)
121
122
  new_bottom = transcript_bottom_row(height)
122
- print_output_locked(TerminalSequences.scroll_region(1, new_bottom))
123
+ if transcript_region_hidden?(height)
124
+ @transcript_viewport_rows = 0
125
+ print_output_locked(TerminalSequences.restore_scroll_region)
126
+ else
127
+ print_output_locked(TerminalSequences.scroll_region(1, new_bottom))
128
+ end
123
129
  clear_screen_rows_locked(old_top, new_top - 1) if new_top > old_top
124
130
  clear_screen_rows_locked(new_bottom + 1, new_top - 1) if old_reserved_rows.positive? && new_bottom < new_top - 1
125
131
  @last_composer_rows = []
126
- if redraw_transcript && @stream_state.block && new_reserved_rows > old_reserved_rows
132
+ if redraw_transcript && @stream_state.block && new_reserved_rows > old_reserved_rows && !transcript_region_hidden?(height)
127
133
  @transcript_viewport_rows = [@transcript_viewport_rows, new_bottom].min
128
134
  end
129
135
  should_redraw_transcript = redraw_transcript && (new_reserved_rows < old_reserved_rows || @stream_state.block)
130
136
  redraw_transcript_locked(width: width, height: height) if should_redraw_transcript && new_reserved_rows != old_reserved_rows
131
137
  end
132
138
 
133
- def handle_resize_locked
139
+ def handle_resize_locked(width: screen_width, height: screen_height)
134
140
  return false if terminal_owned_by_child_locked?
135
-
136
- current_width, current_height = screen_size
137
- return false if current_width == @last_width && current_height == @last_height
138
-
139
- old_width = @last_width
140
- old_height = @last_height
141
- old_reserved_rows = @reserved_rows
142
- restore_scroll_region_locked
143
- rows_to_clear = resize_prompt_clear_rows(old_width, current_width, old_reserved_rows)
144
- clear_resized_composer_region_locked(old_height, current_height, rows_to_clear)
145
- @reserved_rows = 0
146
- @last_width = current_width
147
- @last_height = current_height
148
- @last_footer_refresh = nil if current_width != old_width
149
- if interactive_active_locked?
150
- @interactive_state[:controller].resize(width: interactive_canvas_width(current_width))
141
+ return false if width == @last_width && height == @last_height
142
+
143
+ with_synchronized_output_locked do
144
+ rows_to_clear = resize_prompt_clear_rows(@last_width, width, @reserved_rows)
145
+ restore_scroll_region_locked
146
+ clear_resized_composer_region_locked(@last_height, height, rows_to_clear)
147
+ @last_footer_refresh = nil if width != @last_width
148
+ @last_width = width
149
+ @last_height = height
150
+ if interactive_active_locked?
151
+ @interactive_state[:controller].resize(width: interactive_canvas_width(width))
152
+ end
153
+ redraw_screen_locked(width: width, height: height)
151
154
  end
152
- redraw_screen_locked(width: current_width, height: current_height)
153
155
  true
154
156
  end
155
157
 
@@ -188,6 +190,7 @@ module Kward
188
190
  rows_to_clear ||= [@reserved_rows, @rendered_rows].max
189
191
  clear_bottom_rows_locked(height, rows_to_clear)
190
192
  @last_composer_rows = []
193
+ @rendered_composer_cursor = nil
191
194
  end
192
195
 
193
196
  def resize_prompt_clear_rows(old_width, current_width, old_reserved_rows)
@@ -362,9 +362,9 @@ module Kward
362
362
  @mutex.synchronize do
363
363
  resized = handle_resize_locked
364
364
  spun = tick_spinner_locked
365
- footer_refreshed = tick_footer_locked
366
- render_prompt_locked if resized || spun || footer_refreshed
367
- flush_output_locked if resized || spun || footer_refreshed
365
+ chrome_changed = tick_composer_chrome_locked
366
+ render_prompt_locked if resized || spun || chrome_changed
367
+ flush_output_locked if resized || spun || chrome_changed
368
368
  end
369
369
  end
370
370
 
@@ -667,7 +667,7 @@ module Kward
667
667
  end
668
668
 
669
669
  def selection_overlay_rows(width, height: screen_height)
670
- return selection_confirmation_rows(width) if select_confirmation_active?
670
+ return selection_confirmation_rows(width, height: height) if select_confirmation_active?
671
671
 
672
672
  matches = selection_matches
673
673
  lines = [overlay_text_line(selection_overlay_help_text, :muted), overlay_blank_line]
@@ -677,7 +677,7 @@ module Kward
677
677
  else
678
678
  lines << overlay_text_line("No matches", :muted)
679
679
  end
680
- return overlay_card_rows(selection_overlay_title, lines, width)
680
+ return overlay_card_rows(selection_overlay_title, lines, width, height: height)
681
681
  end
682
682
 
683
683
  visible = visible_selection_matches(matches, height: height)
@@ -686,7 +686,7 @@ module Kward
686
686
  index = start_index + offset
687
687
  lines << overlay_choice_line(choice, selected: index == selection_index)
688
688
  end
689
- overlay_card_rows(selection_overlay_title, lines, width)
689
+ overlay_card_rows(selection_overlay_title, lines, width, height: height)
690
690
  end
691
691
 
692
692
  def selection_overlay_help_text
@@ -706,13 +706,13 @@ module Kward
706
706
  title && !title.empty? ? title : "Sessions"
707
707
  end
708
708
 
709
- def selection_confirmation_rows(width)
709
+ def selection_confirmation_rows(width, height:)
710
710
  title = @select_state[:confirm_title].to_s
711
711
  title = "Confirm" if title.empty?
712
712
  text = @select_state[:confirm_text].to_s
713
713
  text = "Press #{@select_state[:confirm_key]} again to confirm, Esc to cancel." if text.empty?
714
714
  lines = [overlay_text_line(text, :muted)]
715
- overlay_card_rows(title, lines, width)
715
+ overlay_card_rows(title, lines, width, height: height)
716
716
  end
717
717
 
718
718
  def visible_selection_matches(matches, height: screen_height)
@@ -105,7 +105,7 @@ module Kward
105
105
  description = command[:description].empty? ? "" : " — #{command[:description]}"
106
106
  overlay_choice_line("/#{command[:name]}#{hint}#{description}", selected: index == @slash_selection_index)
107
107
  end
108
- overlay_card_rows("Slash commands", lines, width)
108
+ overlay_card_rows("Slash commands", lines, width, height: height)
109
109
  end
110
110
 
111
111
  def visible_slash_overlay_matches(matches, height: screen_height)
@@ -31,8 +31,10 @@ module Kward
31
31
  def write_visual_transcript_text_locked(text)
32
32
  width, height = screen_size
33
33
  output_text = terminal_newlines(text.to_s)
34
- advance_pending_stream_wrap_locked(output_text, width: width, height: height)
35
- print_output_locked(output_text)
34
+ unless transcript_region_hidden?(height)
35
+ advance_pending_stream_wrap_locked(output_text, width: width, height: height)
36
+ print_output_locked(output_text)
37
+ end
36
38
  update_stream_position(output_text, width: width)
37
39
  end
38
40
 
@@ -51,7 +53,7 @@ module Kward
51
53
  end
52
54
 
53
55
  def redraw_transcript_locked(width: screen_width, height: screen_height)
54
- return unless transcript_renderable?
56
+ return if transcript_region_hidden?(height) || !transcript_renderable?
55
57
 
56
58
  rows = transcript_viewport_rows(transcript_redraw_row_count(height), width)
57
59
  clear_screen_rows_locked(1, rows.length)
@@ -70,7 +72,7 @@ module Kward
70
72
  end
71
73
 
72
74
  def remember_transcript_viewport_locked(height = screen_height)
73
- @transcript_viewport_rows = transcript_bottom_row(height)
75
+ @transcript_viewport_rows = transcript_bottom_row(height) unless transcript_region_hidden?(height)
74
76
  end
75
77
 
76
78
  def transcript_renderable?
@@ -118,8 +120,7 @@ module Kward
118
120
  end
119
121
 
120
122
  def transcript_marker(label)
121
- marker = "#{transcript_label(label)}>"
122
- label == "Assistant" ? "✦ #{marker}" : marker
123
+ "#{transcript_label(label)}>"
123
124
  end
124
125
 
125
126
  def label_styles(label)