kward 0.84.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 (245) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +164 -0
  3. data/CONTRIBUTING.md +1 -1
  4. data/Gemfile.lock +6 -2
  5. data/README.md +5 -5
  6. data/Rakefile +3 -0
  7. data/doc/agent-tools.md +24 -1
  8. data/doc/api.md +20 -2
  9. data/doc/composer.md +5 -4
  10. data/doc/configuration.md +86 -14
  11. data/doc/extensibility.md +4 -1
  12. data/doc/getting-started.md +3 -3
  13. data/doc/git.md +4 -2
  14. data/doc/memory.md +1 -1
  15. data/doc/pan.md +6 -0
  16. data/doc/permissions.md +4 -4
  17. data/doc/plugins.md +504 -20
  18. data/doc/providers.md +10 -4
  19. data/doc/rpc.md +279 -23
  20. data/doc/sandboxing.md +11 -5
  21. data/doc/security.md +7 -1
  22. data/doc/session-management.md +41 -5
  23. data/doc/shell.md +643 -27
  24. data/doc/tabs.md +8 -3
  25. data/doc/transports.md +15 -0
  26. data/doc/troubleshooting.md +1 -1
  27. data/doc/usage.md +25 -12
  28. data/doc/workspace-tools.md +9 -0
  29. data/examples/plugins/openrouter_proxy/README.md +103 -0
  30. data/examples/plugins/openrouter_proxy/fake_embeddings.rb +85 -0
  31. data/examples/plugins/openrouter_proxy/plugin.rb +7 -0
  32. data/examples/plugins/openrouter_proxy/protocol.rb +202 -0
  33. data/examples/plugins/openrouter_proxy/server.rb +186 -0
  34. data/examples/plugins/space_invaders.rb +1 -2
  35. data/examples/plugins/stardate_footer.rb +2 -2
  36. data/examples/plugins/telegram/plugin.rb +1 -1
  37. data/exe/kward +2 -2
  38. data/exe/kwsh +7 -0
  39. data/kward.gemspec +3 -1
  40. data/lib/kward/agent.rb +66 -19
  41. data/lib/kward/cli/commands.rb +41 -11
  42. data/lib/kward/cli/compaction.rb +9 -3
  43. data/lib/kward/cli/interactive_turn.rb +10 -0
  44. data/lib/kward/cli/plugins.rb +85 -12
  45. data/lib/kward/cli/prompt_interface.rb +29 -6
  46. data/lib/kward/cli/rendering.rb +24 -28
  47. data/lib/kward/cli/runtime_helpers.rb +168 -56
  48. data/lib/kward/cli/sessions.rb +9 -5
  49. data/lib/kward/cli/settings/menus.rb +745 -0
  50. data/lib/kward/cli/settings/model.rb +316 -0
  51. data/lib/kward/cli/settings.rb +6 -1055
  52. data/lib/kward/cli/slash_commands.rb +45 -4
  53. data/lib/kward/cli/tabs.rb +257 -44
  54. data/lib/kward/cli/tool_summaries.rb +2 -0
  55. data/lib/kward/{cli_transcript_formatter.rb → cli/transcript_formatter.rb} +6 -13
  56. data/lib/kward/cli/worktrees.rb +66 -2
  57. data/lib/kward/cli.rb +82 -31
  58. data/lib/kward/compactor.rb +18 -7
  59. data/lib/kward/config/core.rb +400 -0
  60. data/lib/kward/config/extensions.rb +99 -0
  61. data/lib/kward/config/paths.rb +11 -0
  62. data/lib/kward/config/prompts.rb +313 -0
  63. data/lib/kward/config/settings.rb +257 -0
  64. data/lib/kward/config_files.rb +14 -1008
  65. data/lib/kward/conversation.rb +41 -2
  66. data/lib/kward/entrypoint.rb +14 -0
  67. data/lib/kward/events.rb +2 -0
  68. data/lib/kward/generated_images.rb +237 -0
  69. data/lib/kward/image_attachments.rb +1 -1
  70. data/lib/kward/model/catalog.rb +6 -3
  71. data/lib/kward/model/client.rb +107 -38
  72. data/lib/kward/model/codex_images.rb +83 -0
  73. data/lib/kward/model/copilot_models.rb +10 -6
  74. data/lib/kward/model/model_info.rb +42 -7
  75. data/lib/kward/{openrouter_model_cache.rb → model/openrouter_model_cache.rb} +7 -5
  76. data/lib/kward/model/payloads.rb +18 -8
  77. data/lib/kward/model/sources.rb +3 -1
  78. data/lib/kward/model/stream_parser.rb +4 -7
  79. data/lib/kward/model/typesafe_client.rb +78 -0
  80. data/lib/kward/pan/index.html.erb +8 -1
  81. data/lib/kward/pan/server.rb +23 -8
  82. data/lib/kward/permissions/policy.rb +6 -2
  83. data/lib/kward/plugin_registry.rb +2 -659
  84. data/lib/kward/plugins/actions.rb +453 -0
  85. data/lib/kward/plugins/chat_contract.rb +121 -0
  86. data/lib/kward/{plugin_chat_runtime.rb → plugins/chat_runtime.rb} +62 -18
  87. data/lib/kward/plugins/codex_responses.rb +73 -0
  88. data/lib/kward/plugins/host.rb +248 -0
  89. data/lib/kward/plugins/http_server.rb +83 -0
  90. data/lib/kward/plugins/registry.rb +1229 -0
  91. data/lib/kward/plugins/resources.rb +206 -0
  92. data/lib/kward/plugins/turn_request.rb +36 -0
  93. data/lib/kward/plugins/ui.rb +219 -0
  94. data/lib/kward/prompt_interface/composer_controller.rb +0 -1
  95. data/lib/kward/prompt_interface/composer_renderer.rb +160 -73
  96. data/lib/kward/prompt_interface/composer_state.rb +1 -1
  97. data/lib/kward/prompt_interface/editor/controller.rb +2 -2
  98. data/lib/kward/prompt_interface/editor/renderer.rb +10 -7
  99. data/lib/kward/prompt_interface/editor/runner.rb +1 -1
  100. data/lib/kward/prompt_interface/editor/state.rb +1 -1
  101. data/lib/kward/prompt_interface/file_overlay.rb +3 -3
  102. data/lib/kward/prompt_interface/generated_image_preview.rb +162 -0
  103. data/lib/kward/prompt_interface/git_prompt.rb +18 -11
  104. data/lib/kward/prompt_interface/interactive/renderer.rb +1 -1
  105. data/lib/kward/prompt_interface/interactive/state.rb +18 -20
  106. data/lib/kward/prompt_interface/key_handler.rb +108 -25
  107. data/lib/kward/prompt_interface/layout.rb +11 -1
  108. data/lib/kward/prompt_interface/overlay_renderer.rb +26 -9
  109. data/lib/kward/prompt_interface/plugin_ui_requests.rb +82 -0
  110. data/lib/kward/prompt_interface/project_browser.rb +27 -54
  111. data/lib/kward/prompt_interface/prompt_renderer.rb +27 -35
  112. data/lib/kward/prompt_interface/question_prompt.rb +19 -28
  113. data/lib/kward/prompt_interface/runtime_state.rb +37 -10
  114. data/lib/kward/prompt_interface/screen.rb +30 -20
  115. data/lib/kward/prompt_interface/selection_prompt.rb +8 -8
  116. data/lib/kward/prompt_interface/slash_overlay.rb +1 -1
  117. data/lib/kward/prompt_interface/transcript_renderer.rb +7 -6
  118. data/lib/kward/prompt_interface.rb +149 -30
  119. data/lib/kward/prompts/commands.rb +2 -1
  120. data/lib/kward/prompts.rb +7 -1
  121. data/lib/kward/{adaptive_pty_output_sink.rb → pty/adaptive_output_sink.rb} +39 -5
  122. data/lib/kward/{interactive_pty_runner.rb → pty/interactive_runner.rb} +22 -30
  123. data/lib/kward/{local_command_runner.rb → pty/local_command_runner.rb} +1 -1
  124. data/lib/kward/{local_pty_command_runner.rb → pty/local_pty_runner.rb} +3 -9
  125. data/lib/kward/{pty_output_sink.rb → pty/output_sink.rb} +9 -4
  126. data/lib/kward/pty/transcript_normalizer.rb +137 -0
  127. data/lib/kward/rpc/config_manager.rb +3 -0
  128. data/lib/kward/rpc/plugin_chat_manager.rb +30 -10
  129. data/lib/kward/rpc/prompt_bridge.rb +25 -0
  130. data/lib/kward/rpc/server.rb +118 -17
  131. data/lib/kward/rpc/session_manager.rb +208 -60
  132. data/lib/kward/rpc/session_tree_rows.rb +2 -2
  133. data/lib/kward/rpc/tool_metadata.rb +1 -1
  134. data/lib/kward/rpc/transcript_normalizer.rb +11 -7
  135. data/lib/kward/sandbox/command_runner.rb +1 -1
  136. data/lib/kward/{session_catalog.rb → sessions/catalog.rb} +1 -1
  137. data/lib/kward/{session_store.rb → sessions/store.rb} +39 -12
  138. data/lib/kward/{session_tree_nodes.rb → sessions/tree_nodes.rb} +3 -3
  139. data/lib/kward/{session_tree_renderer.rb → sessions/tree_renderer.rb} +4 -4
  140. data/lib/kward/{session_tree_tool_display.rb → sessions/tree_tool_display.rb} +1 -1
  141. data/lib/kward/{kwsh.rb → shell/kwsh.rb} +30 -10
  142. data/lib/kward/{kwshrc.rb → shell/kwshrc.rb} +11 -2
  143. data/lib/kward/shell/native_arithmetic.rb +255 -0
  144. data/lib/kward/shell/native_arrays.rb +102 -0
  145. data/lib/kward/shell/native_braces.rb +96 -0
  146. data/lib/kward/shell/native_builtins.rb +476 -0
  147. data/lib/kward/shell/native_completion.rb +169 -0
  148. data/lib/kward/shell/native_condition.rb +75 -0
  149. data/lib/kward/shell/native_directory.rb +116 -0
  150. data/lib/kward/shell/native_exec_state.rb +73 -0
  151. data/lib/kward/shell/native_expansion.rb +252 -0
  152. data/lib/kward/shell/native_fields.rb +62 -0
  153. data/lib/kward/shell/native_glob.rb +96 -0
  154. data/lib/kward/shell/native_history.rb +242 -0
  155. data/lib/kward/shell/native_interactive.rb +164 -0
  156. data/lib/kward/shell/native_invocation.rb +42 -0
  157. data/lib/kward/shell/native_jobs.rb +250 -0
  158. data/lib/kward/shell/native_line_editor.rb +382 -0
  159. data/lib/kward/shell/native_line_terminal.rb +286 -0
  160. data/lib/kward/shell/native_locale.rb +109 -0
  161. data/lib/kward/shell/native_locals.rb +57 -0
  162. data/lib/kward/shell/native_pattern.rb +200 -0
  163. data/lib/kward/shell/native_process.rb +279 -0
  164. data/lib/kward/shell/native_program.rb +40 -0
  165. data/lib/kward/shell/native_programmable_completion.rb +115 -0
  166. data/lib/kward/shell/native_prompt_format.rb +89 -0
  167. data/lib/kward/shell/native_runtime.rb +964 -0
  168. data/lib/kward/shell/native_substitution.rb +110 -0
  169. data/lib/kward/shell/native_syntax.rb +946 -0
  170. data/lib/kward/shell/native_traps.rb +196 -0
  171. data/lib/kward/shell/native_vi.rb +407 -0
  172. data/lib/kward/shell/native_vi_terminal.rb +19 -0
  173. data/lib/kward/shell/native_worker.rb +82 -0
  174. data/lib/kward/{persistent_shell_session.rb → shell/persistent_session.rb} +187 -193
  175. data/lib/kward/shell/prompt.rb +66 -0
  176. data/lib/kward/shell/prompt_agent.rb +17 -0
  177. data/lib/kward/{shell_prompt_session.rb → shell/prompt_session.rb} +1 -1
  178. data/lib/kward/shell/script_runner.rb +244 -0
  179. data/lib/kward/shell/standalone_agent.rb +31 -0
  180. data/lib/kward/shell/standalone_link.rb +78 -0
  181. data/lib/kward/shell/standalone_terminal.rb +287 -0
  182. data/lib/kward/skills/trust_store.rb +1 -1
  183. data/lib/kward/tabs/driver.rb +194 -0
  184. data/lib/kward/{tab_store.rb → tabs/store.rb} +2 -2
  185. data/lib/kward/{clipboard.rb → terminal/clipboard.rb} +1 -1
  186. data/lib/kward/{terminal_image_support.rb → terminal/image_support.rb} +1 -1
  187. data/lib/kward/{terminal_keys.rb → terminal/keys.rb} +22 -1
  188. data/lib/kward/terminal/locale_text.rb +29 -0
  189. data/lib/kward/{terminal_sequences.rb → terminal/sequences.rb} +45 -4
  190. data/lib/kward/tools/base.rb +18 -0
  191. data/lib/kward/tools/context_for_task.rb +15 -6
  192. data/lib/kward/tools/datetime.rb +85 -0
  193. data/lib/kward/tools/edit_file.rb +9 -6
  194. data/lib/kward/tools/generate_image.rb +36 -0
  195. data/lib/kward/tools/git_commit.rb +13 -7
  196. data/lib/kward/tools/list_directory.rb +4 -4
  197. data/lib/kward/tools/plugin_tool.rb +41 -0
  198. data/lib/kward/tools/prepare_shell_command.rb +1 -1
  199. data/lib/kward/tools/read_file.rb +7 -6
  200. data/lib/kward/tools/registry.rb +130 -21
  201. data/lib/kward/tools/run_shell_command.rb +10 -8
  202. data/lib/kward/tools/search/code.rb +1 -1
  203. data/lib/kward/tools/summarize_file_structure.rb +5 -5
  204. data/lib/kward/tools/tool_call.rb +4 -1
  205. data/lib/kward/tools/typesafe_evaluate.rb +81 -0
  206. data/lib/kward/tools/workspace_targets.rb +58 -0
  207. data/lib/kward/tools/write_file.rb +9 -6
  208. data/lib/kward/{export_path.rb → transcripts/export_path.rb} +1 -1
  209. data/lib/kward/{markdown_transcript.rb → transcripts/markdown_transcript.rb} +10 -2
  210. data/lib/kward/transcripts/turn_summary_formatter.rb +93 -0
  211. data/lib/kward/transport/contracts.rb +200 -0
  212. data/lib/kward/transport/gateway.rb +81 -36
  213. data/lib/kward/transport/plugin_chat_gateway.rb +3 -2
  214. data/lib/kward/transport.rb +1 -200
  215. data/lib/kward/turn_summary.rb +68 -0
  216. data/lib/kward/version.rb +1 -1
  217. data/lib/kward/{workspace_factory.rb → workspace/factory.rb} +2 -2
  218. data/lib/kward/{git_worktree_manager.rb → workspace/git_worktree_manager.rb} +28 -0
  219. data/lib/kward/{workspace.rb → workspace/workspace.rb} +3 -3
  220. data/lib/main.rb +6 -3
  221. data/templates/default/fulldoc/html/css/kward.css +125 -0
  222. data/templates/default/fulldoc/html/images/kward_screen_1.png +0 -0
  223. data/templates/default/fulldoc/html/setup.rb +1 -1
  224. data/templates/default/layout/html/layout.erb +16 -4
  225. metadata +150 -48
  226. data/lib/kward/pty_transcript_normalizer.rb +0 -93
  227. data/lib/kward/shell_prompt.rb +0 -52
  228. data/lib/kward/tab_driver.rb +0 -90
  229. data/templates/default/fulldoc/html/images/kward_workflow.svg +0 -52
  230. /data/lib/kward/{editor_prompt.rb → cli/editor_prompt.rb} +0 -0
  231. /data/lib/kward/{editor_prompt_session.rb → cli/editor_prompt_session.rb} +0 -0
  232. /data/lib/kward/{diff_view_mode.rb → prompt_interface/editor/diff_view_mode.rb} +0 -0
  233. /data/lib/kward/{editor_mode.rb → prompt_interface/editor/editor_mode.rb} +0 -0
  234. /data/lib/kward/{markdown_code_block.rb → prompt_interface/editor/markdown_code_block.rb} +0 -0
  235. /data/lib/kward/{scratchpad_languages.rb → prompt_interface/editor/scratchpad_languages.rb} +0 -0
  236. /data/lib/kward/{scratchpad_runner.rb → prompt_interface/editor/scratchpad_runner.rb} +0 -0
  237. /data/lib/kward/{detached_run.rb → pty/detached_run.rb} +0 -0
  238. /data/lib/kward/{session_diff.rb → sessions/diff.rb} +0 -0
  239. /data/lib/kward/{session_naming.rb → sessions/naming.rb} +0 -0
  240. /data/lib/kward/{session_trash.rb → sessions/trash.rb} +0 -0
  241. /data/lib/kward/{ansi.rb → terminal/ansi.rb} +0 -0
  242. /data/lib/kward/{terminal_text.rb → terminal/text.rb} +0 -0
  243. /data/lib/kward/{transcript_export.rb → transcripts/transcript_export.rb} +0 -0
  244. /data/lib/kward/{project_files.rb → workspace/files.rb} +0 -0
  245. /data/lib/kward/{path_guard.rb → workspace/path_guard.rb} +0 -0
@@ -1,4 +1,4 @@
1
- require_relative "../terminal_text"
1
+ require_relative "../terminal/text"
2
2
 
3
3
  # Namespace for the Kward CLI agent runtime.
4
4
  module Kward
@@ -11,41 +11,49 @@ module Kward
11
11
  def composer_layout(width, height = screen_height)
12
12
  return interactive_layout(width, height) if interactive_active_locked?
13
13
  return editor_layout(width, height) if editor_visible?
14
- return compact_composer_layout(width) if height < 4
14
+ return compact_composer_layout(width) if width < 5 || height < 4
15
15
  return question_composer_layout(width, height) if @question_state
16
16
 
17
- content_width = [width - 4, 1].max
18
- input_layout_rows, input_cursor_row, input_cursor_col = input_layout(content_width)
19
- attachment_rows = attachment_badge_rows(content_width)
20
17
  overlay_rows = active_overlay_rows(width, height: height)
21
- footer_text = footer_text()
22
- max_input_rows = max_visible_input_rows(attachment_rows.length, overlay_rows.length, footer_text.empty? ? 0 : 1, height: height)
23
- visible_start = [[input_cursor_row - max_input_rows + 1, 0].max, [input_layout_rows.length - max_input_rows, 0].max].min
24
- visible_rows = input_layout_rows[visible_start, max_input_rows] || [""]
25
- rows = overlay_rows + [top_border(width)]
26
- rows.concat(attachment_rows)
27
- rows.concat(visible_rows.map { |row| box_content_row(row, content_width) })
28
- rows << footer_row(content_width, footer_text) unless footer_text.empty?
29
- if @tabs.empty?
30
- rows << bottom_border(width)
31
- else
32
- rows.concat(tab_border_rows(width))
33
- end
34
- cursor_row = overlay_rows.length + 1 + attachment_rows.length + input_cursor_row - visible_start
35
- cursor_col = 2 + [input_cursor_col, content_width - 1].min
36
- [rows, cursor_row, cursor_col]
18
+ available_rows = composer_row_budget(height) - overlay_rows.length
19
+ return compact_composer_layout(width, overlay_rows) if available_rows < 3
20
+
21
+ content_width = width - 4
22
+ # Reserve the borders and the draft's cursor row before adding optional chrome.
23
+ extra_rows = available_rows - 3
24
+ attachment_rows = attachment_badge_rows(content_width).first(extra_rows)
25
+ extra_rows -= attachment_rows.length
26
+ footer = footer_row(content_width) if extra_rows.positive?
27
+ extra_rows -= 1 if footer
28
+ bottom_rows = composer_bottom_rows(width, 1 + extra_rows)
29
+ input_cap = [COMPOSER_MAX_INPUT_ROWS - attachment_rows.length, 1].max
30
+ max_input_rows = [input_cap, 2 + extra_rows - bottom_rows.length].min
31
+ input_rows, cursor_row, cursor_col = visible_composer_input(content_width, max_input_rows)
32
+
33
+ rows = overlay_rows + [top_border(width)] + attachment_rows + input_rows
34
+ rows << footer if footer
35
+ rows.concat(bottom_rows)
36
+ [rows, overlay_rows.length + 1 + attachment_rows.length + cursor_row, cursor_col]
37
37
  end
38
38
 
39
- def compact_composer_layout(width)
39
+ def compact_composer_layout(width, overlay_rows = [])
40
40
  lines, cursor_line, cursor_col = @composer.lines_and_cursor
41
41
  prefix = "#{@prompt_label} "
42
42
  prefix_width = TerminalText.width(prefix)
43
+ prefix, prefix_width = "", 0 if prefix_width >= width
43
44
  line = lines[cursor_line] || ""
44
45
  input_width = [width - prefix_width, 1].max
45
46
  layout = TerminalText.wrap(line, width: input_width, cursor: cursor_col)
46
47
  visible = layout[:rows][layout[:cursor_row]].to_s
47
48
  row = visible_ljust(TerminalText.truncate("#{prefix}#{visible}", width), width)
48
- [[row], 0, [prefix_width + layout[:cursor_col], width - 1].min]
49
+ [overlay_rows + [row], overlay_rows.length, [prefix_width + layout[:cursor_col], width - 1].min]
50
+ end
51
+
52
+ def visible_composer_input(content_width, max_rows)
53
+ rows, cursor_row, cursor_col = input_layout(content_width)
54
+ start = [[cursor_row - max_rows + 1, 0].max, [rows.length - max_rows, 0].max].min
55
+ visible = rows[start, max_rows].map { |row| box_content_row(row, content_width) }
56
+ [visible, cursor_row - start, 2 + [cursor_col, content_width - 1].min]
49
57
  end
50
58
 
51
59
  def input_layout(content_width)
@@ -77,34 +85,52 @@ module Kward
77
85
  end
78
86
 
79
87
  def top_border(width)
80
- title = composer_title
88
+ title = composer_title(width - 2)
89
+ gap = width - 2 - TerminalText.width(ANSI.strip(title))
81
90
  status = cached_composer_status_text
82
91
  if status
83
- gap = width - 2 - TerminalText.width(ANSI.strip(title)) - TerminalText.width(ANSI.strip(status))
84
- if gap >= 0
85
- return colored("╭", :primary_green) + title + colored("─" * gap, :primary_green) + status + colored("╮", :primary_green)
92
+ status_width = TerminalText.width(ANSI.strip(status))
93
+ if status_width <= gap
94
+ gap -= status_width
95
+ else
96
+ status = nil
86
97
  end
87
98
  end
88
- plain_title = ANSI.strip(title)
89
- "#{colored("╭", :primary_green)}#{title}#{colored("─" * [width - TerminalText.width(plain_title) - 2, 0].max, :primary_green)}#{colored("╮", :primary_green)}"
99
+ "#{colored("╭", :primary_green)}#{title}#{colored("─" * gap, :primary_green)}#{status}#{colored("╮", :primary_green)}"
90
100
  end
91
101
 
92
- def composer_title
102
+ def composer_title(max_width = nil)
93
103
  label = composer_title_label
94
- if @busy && @queued_count.positive?
95
- status_composer_text(busy_title("#{label} · #{colored("#{@queued_count} queued", :metadata)}"))
96
- elsif @busy && @steered_count.to_i.positive?
97
- status_composer_text(busy_title("#{label} · #{colored(spinner_frame, :activity, :bold)} steering"))
98
- elsif @busy && response_arriving?
99
- status_composer_text(busy_title("#{label} · #{colored("✦", :activity, :bold)} responding"))
100
- elsif @busy
101
- status_composer_text(busy_title("#{label} · #{colored(spinner_frame, :activity, :bold)} #{@busy_activity}"))
104
+ text = if @busy && @busy_activity_visible
105
+ busy_composer_title(label, max_width)
102
106
  elsif @completion_status
103
107
  completion = colored(@completion_status[:text], @completion_status[:style], :bold)
104
- status_composer_text("#{label} · #{completion}")
108
+ "#{label} · #{completion}"
109
+ else
110
+ label
111
+ end
112
+ title = status_composer_text(text)
113
+ max_width ? visible_truncate(title, max_width) : title
114
+ end
115
+
116
+ def busy_composer_title(label, max_width)
117
+ if @busy_activity == "Generating image…"
118
+ track = Array.new(7, "░")
119
+ track[@spinner_frame_index % track.length] = "█"
120
+ elapsed = formatted_elapsed(monotonic_now - (@image_generation_started_at || @busy_started_at || monotonic_now))
121
+ return busy_title("#{label} · #{colored(track.join, :activity)} #{@busy_activity}", max_width, elapsed: elapsed)
122
+ end
123
+
124
+ activity = if @queued_count.positive?
125
+ colored("#{@queued_count} queued", :metadata)
126
+ elsif @steered_count.to_i.positive?
127
+ "#{colored(spinner_frame, :activity, :bold)} steering"
128
+ elsif response_arriving?
129
+ "#{colored("✦", :activity, :bold)} responding"
105
130
  else
106
- status_composer_text(label)
131
+ "#{colored(spinner_frame, :activity, :bold)} #{@busy_activity}"
107
132
  end
133
+ busy_title("#{label} · #{activity}", max_width)
108
134
  end
109
135
 
110
136
  def composer_title_label
@@ -113,10 +139,17 @@ module Kward
113
139
  @prompt_label.delete_suffix(">")
114
140
  end
115
141
 
116
- def busy_title(text)
117
- elapsed = busy_elapsed_text
118
- text = "#{text} · #{colored(elapsed, :metadata)}" if elapsed
119
- @busy_help ? "#{text} · #{BUSY_HELP_TEXT}" : text
142
+ def busy_title(text, max_width, elapsed: busy_elapsed_text)
143
+ suffixes = []
144
+ suffixes << colored(elapsed, :metadata) if elapsed
145
+ suffixes << BUSY_HELP_TEXT if @busy_help
146
+ suffixes.each do |suffix|
147
+ expanded = "#{text} · #{suffix}"
148
+ break if max_width && TerminalText.width(ANSI.strip(expanded)) + 2 > max_width
149
+
150
+ text = expanded
151
+ end
152
+ text
120
153
  end
121
154
 
122
155
  def status_composer_text(text)
@@ -130,10 +163,9 @@ module Kward
130
163
  def tab_border_rows(width)
131
164
  return [bottom_border(width)] if width < 10
132
165
 
133
- slots = tab_slots
134
- active_slot = slots[@active_tab_index]
166
+ slots = visible_tab_slots(width, tab_slots(width))
167
+ active_slot = slots.find { |slot| slot[:index] == @active_tab_index }
135
168
  return [bottom_border(width)] unless active_slot
136
- return [bottom_border(width)] if active_slot[:left] + active_slot[:width] > width
137
169
 
138
170
  [
139
171
  color_tab_border(bottom_tab_border_row(width, active_slot)),
@@ -142,23 +174,57 @@ module Kward
142
174
  ]
143
175
  end
144
176
 
145
- def tab_slots
146
- label_left = 4
177
+ def tab_slots(terminal_width)
147
178
  @tabs.each_with_index.map do |label, index|
148
- text = tab_label(label, index)
149
- width = TerminalText.width(ANSI.strip(text))
150
- slot = {
151
- left: label_left - 2,
152
- label_left: label_left,
179
+ text = tab_label(label, index, max_width: [terminal_width - 10, 1].max)
180
+ label_width = TerminalText.width(ANSI.strip(text))
181
+ {
182
+ index: index,
153
183
  label: text,
154
- inner_width: width + 2,
155
- width: width + 4
184
+ label_width: label_width,
185
+ inner_width: label_width + 2,
186
+ width: label_width + 4
156
187
  }
157
- label_left += width + 3
158
- slot
159
188
  end
160
189
  end
161
190
 
191
+ def visible_tab_slots(width, slots)
192
+ active_index = @active_tab_index
193
+ return [] unless slots[active_index]
194
+
195
+ first = last = active_index
196
+ return [] if tab_window_width(slots, first, last) > width
197
+
198
+ loop do
199
+ candidates = [first.positive? ? first - 1 : nil, last < slots.length - 1 ? last + 1 : nil].compact
200
+ fitting = candidates.select do |index|
201
+ candidate_first = [first, index].min
202
+ candidate_last = [last, index].max
203
+ tab_window_width(slots, candidate_first, candidate_last) <= width
204
+ end
205
+ break if fitting.empty?
206
+
207
+ candidate = fitting.min_by { |index| (index - active_index).abs }
208
+ first = [first, candidate].min
209
+ last = [last, candidate].max
210
+ end
211
+
212
+ label_left = 4 + (first.positive? ? 1 : 0)
213
+ slots[first..last].map do |slot|
214
+ slot.merge(left: label_left - 2, label_left: label_left).tap do
215
+ label_left += slot[:label_width] + 3
216
+ end
217
+ end
218
+ end
219
+
220
+ def tab_window_width(slots, first, last)
221
+ label_width = slots[first..last].sum { |slot| slot[:label_width] }
222
+ gaps = (last - first) * 3
223
+ left_indicator = first.positive? ? 1 : 0
224
+ right_indicator = last < slots.length - 1 ? 3 : 0
225
+ 6 + label_width + gaps + left_indicator + right_indicator
226
+ end
227
+
162
228
  def bottom_tab_border_row(width, active_slot)
163
229
  row = Array.new(width, " ")
164
230
  place_string(row, 0, "╰#{"─" * [active_slot[:left] - 1, 0].max}╮")
@@ -170,6 +236,8 @@ module Kward
170
236
 
171
237
  def tab_bar_row(width, slots, active_slot)
172
238
  row = Array.new(width, " ")
239
+ place_string(row, 2, "‹") if slots.first[:index].positive?
240
+ place_string(row, width - 3, "›") if slots.last[:index] < @tabs.length - 1
173
241
  slots.each do |slot|
174
242
  if slot == active_slot
175
243
  place_string(row, slot[:left], "│ #{slot[:label]} │")
@@ -213,9 +281,13 @@ module Kward
213
281
  end
214
282
  end
215
283
 
216
- def tab_label(label, index)
284
+ def tab_label(label, index, max_width: nil)
217
285
  tab = normalize_tab_label(label)
218
286
  name = tab[:name].empty? ? "Tab" : tab[:name]
287
+ if max_width
288
+ name_width = [max_width - TerminalText.width("#{index + 1} "), 1].max
289
+ name = TerminalText.truncate(name, name_width)
290
+ end
219
291
  color = tab[:color]
220
292
  name = colored(name, color) if color
221
293
  "#{index + 1} #{name}"
@@ -260,9 +332,10 @@ module Kward
260
332
  []
261
333
  end
262
334
 
263
- def max_visible_input_rows(attachment_count, overlay_count, footer_count, height: screen_height)
264
- input_cap = [COMPOSER_MAX_INPUT_ROWS - attachment_count, 1].max
265
- [[input_cap, height - 3 - overlay_count - footer_count - attachment_count].min, 1].max
335
+ def composer_bottom_rows(width, max_rows)
336
+ return [bottom_border(width)] if @tabs.empty? || max_rows < 3
337
+
338
+ tab_border_rows(width)
266
339
  end
267
340
 
268
341
  def input_prefix(_index)
@@ -277,17 +350,31 @@ module Kward
277
350
 
278
351
  def image_viewer_rows(width, height: screen_height)
279
352
  content_width = [width - 4, 1].max
280
- preview_rows = image_viewer_preview_rows(height, width: width)
281
- title = visible_truncate(" Image: #{@image_viewer_state[:display_path]} ", content_width)
282
- rows = [
283
- colored("╭", :primary_green) + title + colored("─" * [width - TerminalText.width(ANSI.strip(title)) - 2, 0].max, :primary_green) + colored("╮", :primary_green),
284
- image_viewer_graphic_row(width, image_viewer_sequence(width, height))
285
- ]
286
- rows.concat(Array.new(preview_rows - 1) { box_content_row("", content_width) })
287
- zoom = (image_viewer_zoom * 100).round
288
- rows << footer_row(content_width, "Read-only image preview · #{zoom}% · +/- zoom · Esc/Q close")
289
- rows << bottom_border(width)
290
- rows
353
+ state = @image_viewer_state
354
+ title = "Image: #{state[:display_path]}"
355
+ footer_rows = if state[:generated]
356
+ generated_image_footer_rows(content_width)
357
+ else
358
+ zoom = (image_viewer_zoom * 100).round
359
+ [footer_row(content_width, "Read-only image preview · #{zoom}% · +/- zoom · Esc/Q close")]
360
+ end
361
+ preview_budget = composer_overlay_row_budget(height) - 2 - footer_rows.length
362
+ if preview_budget < (state[:directory_picker] ? 0 : 1)
363
+ message = overlay_text_line("Pane too short for image preview · Esc back", :muted)
364
+ return overlay_card_rows(title, [message], width, height: height)
365
+ end
366
+
367
+ rows = [overlay_top_border(title, width)]
368
+ if state[:directory_picker]
369
+ # The picker owns the overlay while active; don't draw under its input.
370
+ elsif state[:preview_error]
371
+ rows << box_content_row("Preview unavailable: #{state[:preview_error]}", content_width)
372
+ else
373
+ preview_rows = [image_viewer_preview_rows(height, width: width), preview_budget].min
374
+ rows << image_viewer_graphic_row(width, image_viewer_sequence(width, preview_rows))
375
+ rows.concat(Array.new(preview_rows - 1) { box_content_row("", content_width) })
376
+ end
377
+ rows.concat(footer_rows) << bottom_border(width)
291
378
  end
292
379
 
293
380
  def image_viewer_graphic_row(width, sequence)
@@ -1,6 +1,6 @@
1
1
  require_relative "../text_boundary"
2
2
  require_relative "../text_matcher"
3
- require_relative "../terminal_text"
3
+ require_relative "../terminal/text"
4
4
 
5
5
  # Namespace for the Kward CLI agent runtime.
6
6
  module Kward
@@ -1,5 +1,5 @@
1
1
  require "fileutils"
2
- require_relative "../../scratchpad_languages"
2
+ require_relative "scratchpad_languages"
3
3
  require_relative "runner"
4
4
 
5
5
  # Namespace for the Kward CLI agent runtime.
@@ -666,7 +666,7 @@ module Kward
666
666
  end
667
667
 
668
668
  def editor_mouse_content_top_row
669
- 3
669
+ 2
670
670
  end
671
671
 
672
672
  def handle_editor_shift_navigation_key(key)
@@ -7,8 +7,15 @@ module Kward
7
7
  private
8
8
 
9
9
  def editor_layout(width, height = screen_height)
10
- return editor_split_layout(width, height) if editor_runner_output_visible?
10
+ rows, cursor_row, cursor_col = if editor_runner_output_visible?
11
+ editor_split_layout(width, height)
12
+ else
13
+ editor_source_layout(width, height)
14
+ end
15
+ [rows.first(height), cursor_row, cursor_col]
16
+ end
11
17
 
18
+ def editor_source_layout(width, height)
12
19
  content_width = [width - 4, 1].max
13
20
  visible_count = editor_visible_line_count(height: height, width: width)
14
21
  line_index, column = @editor_state.cursor_line_and_column
@@ -41,9 +48,7 @@ module Kward
41
48
  end
42
49
 
43
50
  def editor_split_visible_counts(height:, width:)
44
- # Keep one row available above the composer, matching the normal editor layout.
45
- available = height - editor_bottom_rows(width).length - 5
46
- available = 2 if available < 2
51
+ available = [height - editor_bottom_rows(width).length - 4, 2].max
47
52
  source_count = (available + 1) / 2
48
53
  [source_count, available - source_count]
49
54
  end
@@ -177,9 +182,7 @@ module Kward
177
182
  end
178
183
 
179
184
  def editor_visible_line_count(height: screen_height, width: screen_width)
180
- visible_count = [[height - 3 - editor_bottom_rows(width).length, 1].max, 1].max
181
- visible_count = [visible_count, height - 4].min if height > 4
182
- [visible_count, 1].max
185
+ [height - editor_bottom_rows(width).length - 2, 1].max
183
186
  end
184
187
 
185
188
  def editor_bottom_rows(width)
@@ -1,4 +1,4 @@
1
- require_relative "../../scratchpad_runner"
1
+ require_relative "scratchpad_runner"
2
2
  require_relative "runner_state"
3
3
 
4
4
  # Namespace for the Kward CLI agent runtime.
@@ -1,4 +1,4 @@
1
- require_relative "../../editor_mode"
1
+ require_relative "editor_mode"
2
2
  require_relative "../../text_boundary"
3
3
  require_relative "buffer"
4
4
  require_relative "file_marker"
@@ -254,10 +254,10 @@ module Kward
254
254
 
255
255
  matches = file_overlay_matches
256
256
  if file_mention_discovery_loading?
257
- return overlay_card_rows("Files", [overlay_text_line("Loading project files…", :muted)], width)
257
+ return overlay_card_rows("Files", [overlay_text_line("Loading project files…", :muted)], width, height: height)
258
258
  end
259
259
  if matches.empty?
260
- return overlay_card_rows("Files", [overlay_text_line("No matching files", :muted)], width)
260
+ return overlay_card_rows("Files", [overlay_text_line("No matching files", :muted)], width, height: height)
261
261
  end
262
262
 
263
263
  visible = visible_file_overlay_matches(matches, height: height)
@@ -268,7 +268,7 @@ module Kward
268
268
  index = start_index + offset
269
269
  overlay_choice_line(path, selected: index == @file_selection_index)
270
270
  end)
271
- overlay_card_rows(file_open_overlay_visible? ? "Open file" : "Files", lines, width)
271
+ overlay_card_rows(file_open_overlay_visible? ? "Open file" : "Files", lines, width, height: height)
272
272
  end
273
273
 
274
274
  def visible_file_overlay_matches(matches, height: screen_height)
@@ -0,0 +1,162 @@
1
+ require_relative "../shell/kwsh"
2
+
3
+ module Kward
4
+ class PromptInterface
5
+ # Generated previews share the /files renderer but are not project files.
6
+ module GeneratedImagePreview
7
+ def offer_generated_images(offers)
8
+ # Rendering can probe terminal capabilities, so let the input loop open it.
9
+ @mutex.synchronize { (@generated_image_offers ||= []).concat(offers) }
10
+ end
11
+
12
+ # Tab switches suspend the overlay; they do not discard the image.
13
+ def take_generated_image_offers
14
+ @mutex.synchronize do
15
+ offers = @generated_image_offers || []
16
+ if (image = @image_viewer_state&.fetch(:generated, nil))
17
+ offers.unshift(image)
18
+ close_image_viewer
19
+ end
20
+ @generated_image_offers = []
21
+ offers
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def open_pending_generated_image_locked
28
+ return false if @generated_image_offers.nil? || @generated_image_offers.empty?
29
+ return false if image_viewer_active? || editor_active? || project_browser_visible? || modal_active_locked? || interactive_active_locked?
30
+
31
+ while (offer = @generated_image_offers.shift)
32
+ begin
33
+ path = offer[:store].path(offer[:owner], offer[:image].id)
34
+ @image_viewer_state = image_viewer_state_for(path, offer[:image].filename).merge(generated: offer)
35
+ return true
36
+ rescue GeneratedImages::Error
37
+ next
38
+ end
39
+ end
40
+ false
41
+ end
42
+
43
+ def handle_generated_image_key(key)
44
+ state = @image_viewer_state
45
+ if (picker = state[:directory_picker])
46
+ return handle_generated_image_directory_key(key, state, picker)
47
+ end
48
+
49
+ case key
50
+ when "d", "D" then save_generated_preview(:downloads)
51
+ when "p", "P"
52
+ clear_image_viewer_output_locked
53
+ state.delete(:status)
54
+ state[:directory_picker] = { text: +"", cursor: 0 }
55
+ true
56
+ when "\e", "q", "Q" then discard_generated_preview
57
+ else false
58
+ end
59
+ end
60
+
61
+ def handle_generated_image_directory_key(key, state, picker)
62
+ case key
63
+ when "\e"
64
+ state.delete(:directory_picker)
65
+ state.delete(:status)
66
+ when "\r", "\n"
67
+ save_generated_preview(:project, picker[:text].empty? ? "." : picker[:text])
68
+ when "\t" then complete_generated_image_directory(state, picker)
69
+ when "\x7f", "\b"
70
+ if picker[:cursor].positive?
71
+ picker[:cursor] -= 1
72
+ picker[:text].slice!(picker[:cursor])
73
+ end
74
+ picker.delete(:cycle)
75
+ state.delete(:status)
76
+ else
77
+ case key_name_for(key)
78
+ when :left then picker[:cursor] = [picker[:cursor] - 1, 0].max
79
+ when :right then picker[:cursor] = [picker[:cursor] + 1, picker[:text].length].min
80
+ else
81
+ if printable_key?(key) && picker[:text].bytesize + key.bytesize <= 4096
82
+ picker[:text].insert(picker[:cursor], key)
83
+ picker[:cursor] += key.length
84
+ state.delete(:status)
85
+ end
86
+ end
87
+ picker.delete(:cycle)
88
+ end
89
+ true
90
+ end
91
+
92
+ def complete_generated_image_directory(state, picker)
93
+ if (cycle = picker[:cycle])
94
+ cycle[:index] = (cycle[:index] + 1) % cycle[:candidates].length
95
+ text = cycle[:candidates][cycle[:index]]
96
+ else
97
+ root = state[:generated][:image].root
98
+ result = Kwsh.new(cwd: root).complete_directory(picker[:text], picker[:cursor], root: root)
99
+ return unless result
100
+
101
+ text = result.replacement
102
+ picker[:cycle] = { candidates: result.candidates, index: result.candidates.index(text) || -1 } if result.candidates.size > 1
103
+ end
104
+ picker[:text] = text.dup
105
+ picker[:cursor] = text.length
106
+ state.delete(:status)
107
+ end
108
+
109
+ def save_generated_preview(destination, directory = nil)
110
+ state = @image_viewer_state
111
+ offer = state[:generated]
112
+ store, owner, image = offer.values_at(:store, :owner, :image)
113
+ path = if destination == :downloads
114
+ store.save_to_downloads(owner, image.id)
115
+ else
116
+ store.save_to_project(owner, image.id, directory)
117
+ end
118
+ offer[:saved] = true
119
+ state.delete(:directory_picker)
120
+ state[:status] = "Saved to #{path.gsub(/[[:cntrl:]]/, "?")}"
121
+ record_generated_image_status(state[:status])
122
+ true
123
+ rescue GeneratedImages::Error => e
124
+ state[:status] = e.message
125
+ true
126
+ end
127
+
128
+ def discard_generated_preview
129
+ offer = @image_viewer_state[:generated]
130
+ offer[:store].discard(offer[:owner], offer[:image].id)
131
+ record_generated_image_status("Image discarded") unless offer[:saved]
132
+ close_image_viewer
133
+ open_pending_generated_image_locked
134
+ true
135
+ rescue GeneratedImages::Error => e
136
+ @image_viewer_state[:status] = e.message
137
+ true
138
+ end
139
+
140
+ def record_generated_image_status(text)
141
+ append_transcript_buffer("\n#{text.gsub(/[[:cntrl:]]/, "?")}\n")
142
+ remember_transcript_viewport_locked
143
+ with_synchronized_output_locked { redraw_screen_locked } if @started
144
+ end
145
+
146
+ def generated_image_footer_rows(content_width)
147
+ state = @image_viewer_state
148
+ picker = state[:directory_picker]
149
+ rows = if picker
150
+ text = picker[:text].dup.insert(picker[:cursor], "│")
151
+ [footer_row(content_width, "Project directory: #{text}"), footer_row(content_width, "Tab completes · Enter saves · Esc back")]
152
+ else
153
+ actions = ["D Save to Downloads · P Save to Project…", "Esc Discard"]
154
+ actions = [actions.join(" · ")] if content_width >= 53
155
+ actions.map { |text| footer_row(content_width, text) }
156
+ end
157
+ rows << footer_row(content_width, state[:status]) if state[:status]
158
+ rows
159
+ end
160
+ end
161
+ end
162
+ end
@@ -18,8 +18,8 @@ module Kward
18
18
  @mutex.synchronize do
19
19
  if key.nil?
20
20
  resized = handle_resize_locked
21
- footer_refreshed = tick_footer_locked
22
- render_prompt_locked if resized || footer_refreshed
21
+ chrome_changed = tick_composer_chrome_locked
22
+ render_prompt_locked if resized || chrome_changed
23
23
  else
24
24
  result = handle_git_key(key)
25
25
  render_prompt_locked unless result.is_a?(String) || result == SELECT_CANCEL || git_action?(result)
@@ -210,8 +210,8 @@ module Kward
210
210
  @mutex.synchronize do
211
211
  if key.nil?
212
212
  resized = handle_resize_locked
213
- footer_refreshed = tick_footer_locked
214
- render_prompt_locked if resized || footer_refreshed
213
+ chrome_changed = tick_composer_chrome_locked
214
+ render_prompt_locked if resized || chrome_changed
215
215
  else
216
216
  handle_editor_key(key)
217
217
  render_prompt_locked if editor_active?
@@ -270,22 +270,29 @@ module Kward
270
270
  return [] unless @git_state
271
271
 
272
272
  help = git_composing? ? "Type commit message · Enter commit · Tab overlay · Esc cancel" : "↑/↓/j/k select · Enter diff · s stage/unstage · Tab message · Esc cancel"
273
- lines = [overlay_text_line(help, :muted), overlay_blank_line]
274
273
  status_lines = @git_state[:status_lines]
275
274
  status_lines = ["No uncommitted changes."] if status_lines.empty?
276
- max_status_rows = [max_overlay_list_rows(height), 1].max
275
+ content_budget = [composer_overlay_row_budget(height) - 2, 1].max
276
+ max_status_rows = [max_overlay_list_rows(height), status_lines.length, content_budget].min
277
+ show_overflow = max_status_rows < status_lines.length && content_budget >= 3
278
+ indicator_rows = show_overflow ? 2 : 0
279
+ max_status_rows = [max_status_rows, content_budget - indicator_rows].min
280
+ lines = if max_status_rows + indicator_rows + 2 <= content_budget
281
+ [overlay_text_line(help, :muted), overlay_blank_line]
282
+ else
283
+ []
284
+ end
277
285
  selected_index = @git_state[:selected_index].to_i
278
286
  start_index = centered_list_window_start(selected_index, status_lines.length, max_status_rows)
279
287
  visible_status_lines = status_lines[start_index, max_status_rows] || []
280
- lines << overlay_text_line("… #{start_index} above", :muted) if start_index.positive?
288
+ lines << overlay_text_line("… #{start_index} above", :muted) if show_overflow && start_index.positive?
281
289
  visible_status_lines.each_with_index do |line, offset|
282
290
  index = start_index + offset
283
- marker = index == selected_index ? "› " : " "
284
- lines << overlay_text_line("#{marker}#{line}")
291
+ lines << overlay_choice_line(line, selected: index == selected_index)
285
292
  end
286
293
  hidden_below = status_lines.length - start_index - visible_status_lines.length
287
- lines << overlay_text_line("… #{hidden_below} more", :muted) if hidden_below.positive?
288
- overlay_card_rows("Git", lines, width)
294
+ lines << overlay_text_line("… #{hidden_below} more", :muted) if show_overflow && hidden_below.positive?
295
+ overlay_card_rows("Git", lines, width, height: height)
289
296
  end
290
297
  end
291
298
  end
@@ -42,7 +42,7 @@ module Kward
42
42
 
43
43
  def interactive_status_text(state)
44
44
  fps = state[:controller].fps
45
- "Interactive · #{fps.to_i}fps · Ctrl+C exits"
45
+ "Interactive · #{fps.to_i}fps · Esc exits"
46
46
  end
47
47
 
48
48
  def interactive_bottom_rows(width)