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
@@ -1,3 +1,5 @@
1
+ require_relative "../shell/prompt_agent"
2
+
1
3
  # Namespace for the Kward CLI agent runtime.
2
4
  module Kward
3
5
  # Command-line frontend that coordinates terminal interaction, sessions, tools, and model turns.
@@ -7,6 +9,19 @@ module Kward
7
9
  MAX_TRANSIENT_TERMINAL_OUTPUT_BYTES = 1_048_576
8
10
  UNSAFE_TRANSCRIPT_CONTROL_PATTERN = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/.freeze
9
11
 
12
+ # Lets the PTY pumps resolve an ambiguous legacy Escape without reading ahead.
13
+ TerminalHandoffInput = Struct.new(:prompt, :on_tab_action) do
14
+ def call(chunk)
15
+ filtered = prompt.filter_terminal_handoff_input(chunk)
16
+ on_tab_action.call(filtered[:tab_action]) if filtered[:tab_action]
17
+ filtered
18
+ end
19
+
20
+ def flush_pending_escape
21
+ prompt.flush_terminal_handoff_escape if prompt.respond_to?(:flush_terminal_handoff_escape)
22
+ end
23
+ end
24
+
10
25
  private
11
26
 
12
27
  def new_conversation(workspace_root: current_workspace_root)
@@ -216,21 +231,13 @@ module Kward
216
231
 
217
232
  def build_shell_prompt_agent(active_agent)
218
233
  active_conversation = active_agent.conversation
219
- config = ConfigFiles.read_config
220
- configured_provider = ConfigFiles.shell_agent_provider(config)
221
- conversation = Conversation.new(
222
- system_message: ShellPrompt.system_message,
223
- workspace_root: active_conversation.workspace_root,
224
- provider: configured_provider || active_conversation.provider || current_model_provider,
225
- model: ConfigFiles.shell_agent_model(config) || (configured_provider ? nil : active_conversation.model || current_model_id),
226
- reasoning_effort: ConfigFiles.shell_agent_reasoning_effort(config) || (configured_provider ? nil : active_conversation.reasoning_effort || current_reasoning_effort)
227
- )
228
- Agent.new(
234
+ ShellPromptAgent.build(
229
235
  client: @client,
230
- tool_registry: active_agent.tool_registry,
231
- conversation: conversation,
232
- strict_provider: !configured_provider.nil?,
233
- warning_sink: ConfigFiles.warning_sink
236
+ registry: active_agent.tool_registry,
237
+ workspace_root: active_conversation.workspace_root,
238
+ provider: active_conversation.provider || current_model_provider,
239
+ model: active_conversation.model || current_model_id,
240
+ reasoning_effort: active_conversation.reasoning_effort || current_reasoning_effort
234
241
  )
235
242
  end
236
243
 
@@ -372,13 +379,15 @@ module Kward
372
379
 
373
380
  def with_interactive_terminal_handoff
374
381
  if @prompt.respond_to?(:with_inline_terminal_handoff)
375
- with_tab_keybinding_handoff(:with_inline_terminal_handoff) do |input, output, transition|
382
+ with_tab_keybinding_handoff(:with_inline_terminal_handoff) do |input, output, transition, keep_view|
376
383
  sink = AdaptivePtyOutputSink.new(
377
384
  output: output,
378
385
  on_exclusive: transition,
379
386
  max_capture_bytes: MAX_TRANSIENT_TERMINAL_OUTPUT_BYTES
380
387
  )
381
- yield(input, sink)
388
+ result = yield(input, sink)
389
+ keep_view&.call if result && !result.tab_action && !result.background && sink.preserve_terminal_view?
390
+ result
382
391
  end
383
392
  elsif @prompt.respond_to?(:with_terminal_handoff)
384
393
  with_tab_keybinding_handoff(:with_terminal_handoff) do |input, output|
@@ -413,12 +422,7 @@ module Kward
413
422
  return unless @prompt.respond_to?(:filter_terminal_handoff_input)
414
423
  return unless respond_to?(:handle_tab_action, true)
415
424
 
416
- lambda do |chunk|
417
- filtered = @prompt.filter_terminal_handoff_input(chunk)
418
- action = filtered[:tab_action]
419
- handle_tab_action(action, @session_store) if action
420
- filtered
421
- end
425
+ TerminalHandoffInput.new(@prompt, ->(action) { handle_tab_action(action, @session_store) })
422
426
  end
423
427
 
424
428
  def record_completed_pty_output(sink, result, tab: nil, background_state: nil)
@@ -443,7 +447,7 @@ module Kward
443
447
  transcript_output
444
448
  end
445
449
 
446
- def flush_background_tab_output(tab, state, result: nil)
450
+ def flush_background_tab_output(tab, state, result: nil, complete: false)
447
451
  sink = state[:run].sink
448
452
  output = sink&.captured_output.to_s
449
453
  return if output.empty?
@@ -451,11 +455,18 @@ module Kward
451
455
  transcript_output = if state[:output_kind] == :streamed
452
456
  background_stream_transcript_output(sink)
453
457
  else
454
- transcript_result = result || InteractivePtyRunner::Result.new(exit_status: 0, input_forwarded: false)
458
+ transcript_result = result if result.respond_to?(:input_forwarded)
459
+ transcript_result ||= InteractivePtyRunner::Result.new(exit_status: 0, input_forwarded: false)
455
460
  background_shell_transcript_output(sink, transcript_result)
456
461
  end
457
462
  return unless transcript_output
458
463
 
464
+ if state[:output_kind] == :pty && !complete
465
+ # Transcript entries finish on a new line; keep a partial PTY row buffered.
466
+ newline = transcript_output.rindex("\n")
467
+ transcript_output = newline ? transcript_output[0..newline] : ""
468
+ end
469
+
459
470
  rendered_output = state[:rendered_output].to_s
460
471
  if transcript_output.start_with?(rendered_output)
461
472
  delta = transcript_output[rendered_output.length..].to_s
@@ -483,8 +494,8 @@ module Kward
483
494
  return if sink&.truncated?
484
495
 
485
496
  text = ANSI.normalize_transcript_encoding(sink&.captured_output.to_s).gsub("\r\n", "\n")
486
- sanitized = PtyTranscriptNormalizer.normalize(text)
487
- return if sanitized.empty?
497
+ sanitized = PtyTranscriptNormalizer.normalize(text, columns: TTY::Screen.width, max_bytes: MAX_TRANSIENT_TERMINAL_OUTPUT_BYTES)
498
+ return if sanitized.nil? || sanitized.empty?
488
499
  return if ANSI.strip_control_sequences(sanitized).match?(UNSAFE_TRANSCRIPT_CONTROL_PATTERN)
489
500
 
490
501
  sanitized
@@ -520,14 +531,14 @@ module Kward
520
531
  return if text.empty?
521
532
 
522
533
  if normalize_line_controls
523
- sanitized = PtyTranscriptNormalizer.normalize(text)
534
+ sanitized = PtyTranscriptNormalizer.normalize(text, columns: TTY::Screen.width, max_bytes: MAX_TRANSIENT_TERMINAL_OUTPUT_BYTES)
524
535
  else
525
536
  return if text.include?("\r")
526
537
 
527
538
  sanitized = ANSI.sanitize_transcript(text)
528
539
  return unless sanitized == text
529
540
  end
530
- return if sanitized.empty?
541
+ return if sanitized.nil? || sanitized.empty?
531
542
  return if ANSI.strip_control_sequences(sanitized).match?(UNSAFE_TRANSCRIPT_CONTROL_PATTERN)
532
543
 
533
544
  sanitized
@@ -590,9 +601,9 @@ module Kward
590
601
  pty_result = run_kwsh_interactive_pty_command(shell, result)
591
602
  return :tab_action if pty_result&.tab_action
592
603
 
593
- next
604
+ next unless shell.respond_to?(:exited?) && shell.exited?
594
605
  end
595
- if result.exit_shell
606
+ if result.exit_shell || shell.respond_to?(:exited?) && shell.exited?
596
607
  close_kwsh_session(shell)
597
608
  tab.shell = nil if tab
598
609
  tab.shell_agent = nil if tab
@@ -638,10 +649,17 @@ module Kward
638
649
 
639
650
  def ask_kwsh(shell)
640
651
  provider = ->(input, cursor) { shell.complete(input, cursor) }
641
- if @prompt.respond_to?(:with_completion_provider)
642
- @prompt.with_completion_provider(provider, slash_overlay: false) { @prompt.ask(shell.prompt_label) }
643
- else
644
- @prompt.ask(shell.prompt_label)
652
+ loop do
653
+ label = shell.respond_to?(:input_prompt_label) ? shell.input_prompt_label : shell.prompt_label
654
+ input = if @prompt.respond_to?(:with_completion_provider)
655
+ @prompt.with_completion_provider(provider, slash_overlay: false) { @prompt.ask(label) }
656
+ else
657
+ @prompt.ask(label)
658
+ end
659
+ return input unless input.is_a?(String) && shell.respond_to?(:compose_input)
660
+
661
+ source = shell.compose_input(input)
662
+ return source if source
645
663
  end
646
664
  end
647
665
 
@@ -16,7 +16,7 @@ module Kward
16
16
  show_all = false
17
17
  loop do
18
18
  visible_models = provider_filter ? models.select { |model| model[:provider] == provider_filter } : models
19
- visible_models = visible_models.select { |model| default_selectable_model?(model) } unless show_all
19
+ visible_models = visible_models.select { |model| ModelInfo.default_selectable_model?(model) } unless show_all
20
20
  actions = ["Refresh model list", "Enter model ID manually", "Show all models", "Change provider"]
21
21
  selected = @prompt.select("Default model", model_choices(visible_models, conversation) + actions, title: "Models", custom: true)
22
22
  return unless selected
@@ -156,17 +156,6 @@ module Kward
156
156
  choices.empty? ? ["#{current_provider} #{current_model} (current)"] : choices.uniq
157
157
  end
158
158
 
159
- def default_selectable_model?(model)
160
- return true if model[:current]
161
-
162
- capabilities = Array(model[:supportedParameters]).map { |value| value.to_s.downcase }
163
- return true if capabilities.empty?
164
-
165
- capabilities.any? do |capability|
166
- ["tools", "toolchoice", "functioncalling", "generatecontent", "streamgeneratecontent"].include?(capability.delete("_-"))
167
- end
168
- end
169
-
170
159
  def selected_model(selected, models)
171
160
  text = selected.to_s.sub(/ \(current\)\z/, "").strip
172
161
  known = models.find { |model| "#{model[:provider]} #{model[:id]}" == text }
@@ -45,10 +45,18 @@ module Kward
45
45
  :error_reported,
46
46
  :local_busy_activity,
47
47
  :pending_runtime_outputs,
48
+ :image_offers,
48
49
  keyword_init: true
49
50
  ) do
50
51
  TRANSIENT_SHELL_OUTPUT_LIMIT = 200_000
51
52
 
53
+ attr_reader :pending_images
54
+
55
+ def initialize(**attributes)
56
+ super
57
+ @pending_images = Queue.new
58
+ end
59
+
52
60
  def running?
53
61
  %w[queued running waiting_for_question].include?(status.to_s)
54
62
  end
@@ -68,6 +76,7 @@ module Kward
68
76
 
69
77
  def record_event(event)
70
78
  event_history << event
79
+ pending_images << event.image if event.is_a?(Events::ImageReady)
71
80
  end
72
81
 
73
82
  def append_transient_shell_entry(text)
@@ -241,8 +250,9 @@ module Kward
241
250
  return switch_tab(@tabs.index(existing))
242
251
  end
243
252
 
244
- save_active_tab_state
245
253
  stop_tab_live_view
254
+ save_active_tab_state
255
+ suspend_tab_images(active_tab)
246
256
  scope_key = plugin_tab_scope_key(tab_type)
247
257
  descriptor = { "kind" => "plugin", "plugin_tab_type" => tab_type.id, "label" => tab_type.title, "scope_key" => scope_key }
248
258
  host = build_plugin_tab_host(tab_type, workspace_root: session_store.cwd, scope_key: scope_key)
@@ -304,7 +314,9 @@ module Kward
304
314
  hook_context: hook_context,
305
315
  mcp_clients: strict ? [] : nil,
306
316
  plugin_tools: strict ? [] : plugin_registry.tools,
307
- git_committer: git_committer
317
+ git_committer: git_committer,
318
+ image_client: @client,
319
+ generated_images: (@generated_images ||= GeneratedImages.new)
308
320
  )
309
321
  @footer_conversation = conversation
310
322
  @footer_tool_registry = tool_registry
@@ -432,6 +444,8 @@ module Kward
432
444
  case action[:tab_action]
433
445
  when :new
434
446
  open_new_tab(session_store)
447
+ when :new_after_current
448
+ open_new_tab(session_store, after_current: true)
435
449
  when :close
436
450
  close_active_tab
437
451
  when :next
@@ -443,18 +457,21 @@ module Kward
443
457
  end
444
458
  end
445
459
 
446
- def open_new_tab(session_store)
460
+ def open_new_tab(session_store, after_current: false)
447
461
  return say_sessions_unavailable unless session_store
448
462
 
449
- save_active_tab_state
450
463
  stop_tab_live_view
464
+ save_active_tab_state
465
+ suspend_tab_images(active_tab)
451
466
  session = track_session(session_store.create(provider: current_model_provider, model: current_model_id, reasoning_effort: current_reasoning_effort))
452
467
  conversation = new_conversation(workspace_root: session_store.cwd)
453
468
  session.attach(conversation)
454
469
  agent = build_tab_agent(conversation, session)
455
- @tabs << build_tab(session, agent, driver: SessionTabDriver.new(session: session, agent: agent), label: default_tab_label(@tabs.length))
456
- @active_tab_index = @tabs.length - 1
457
- activate_tab(@active_tab_index)
470
+ tab = build_tab(session, agent, driver: SessionTabDriver.new(session: session, agent: agent), label: default_tab_label(@tabs.length))
471
+ index = after_current ? @active_tab_index + 1 : @tabs.length
472
+ @tabs.insert(index, tab)
473
+ @active_tab_index = index
474
+ activate_tab(index)
458
475
  end
459
476
 
460
477
  def close_active_tab
@@ -464,6 +481,8 @@ module Kward
464
481
  return nil
465
482
  end
466
483
 
484
+ stop_tab_live_view
485
+ discard_tab_images(tab)
467
486
  if @tabs.length <= 1
468
487
  close_kwsh_session(tab.shell) if tab&.shell && respond_to?(:close_kwsh_session, true)
469
488
  close_plugin_tab(tab)
@@ -472,7 +491,6 @@ module Kward
472
491
  return PromptInterface::EXIT_INPUT
473
492
  end
474
493
 
475
- stop_tab_live_view
476
494
  close_kwsh_session(tab.shell) if tab&.shell && respond_to?(:close_kwsh_session, true)
477
495
  tab.session&.delete_if_unused if tab&.session.respond_to?(:delete_if_unused)
478
496
  close_plugin_tab(tab)
@@ -486,8 +504,9 @@ module Kward
486
504
  return if index == @active_tab_index
487
505
  return unless index.between?(0, @tabs.length - 1)
488
506
 
489
- save_active_tab_state
490
507
  stop_tab_live_view
508
+ save_active_tab_state
509
+ suspend_tab_images(active_tab)
491
510
  @active_tab_index = index
492
511
  activate_tab(index)
493
512
  end
@@ -496,6 +515,7 @@ module Kward
496
515
  tab = active_tab
497
516
  return agent unless tab
498
517
 
518
+ discard_tab_images(tab) unless tab.agent&.conversation.equal?(agent.conversation)
499
519
  tab.session = @active_session
500
520
  worktree = reconcile_worktree_binding(tab, agent.conversation)
501
521
  tab.agent = build_tab_agent(agent.conversation, tab.session, worktree: worktree)
@@ -572,8 +592,14 @@ module Kward
572
592
  update_plugin_tab_slash_commands(tab)
573
593
  update_prompt_tabs
574
594
  render_tab(tab) if render
595
+ offer_tab_images(tab)
575
596
  start_tab_live_view(tab) if tab.running?
576
- @prompt.begin_busy_input("You>", activity: tab.local_busy_activity) if tab.local_busy? && @prompt.respond_to?(:begin_busy_input)
597
+ if tab.local_busy? && tab.shell
598
+ @prompt.finish_busy_input if @prompt.respond_to?(:finish_busy_input)
599
+ @prompt.update_prompt_label(tab.shell.prompt_label) if @prompt.respond_to?(:update_prompt_label)
600
+ elsif tab.local_busy? && @prompt.respond_to?(:begin_busy_input)
601
+ @prompt.begin_busy_input("You>", activity: tab.local_busy_activity)
602
+ end
577
603
  persist_tabs
578
604
  service_active_tab_question
579
605
  tab.agent
@@ -702,7 +728,7 @@ module Kward
702
728
  complete = state[:run].complete?
703
729
  result = state[:run].result if complete
704
730
  if state[:output_kind] && respond_to?(:flush_background_tab_output, true)
705
- flush_background_tab_output(tab, state, result: result)
731
+ flush_background_tab_output(tab, state, result: result, complete: complete)
706
732
  end
707
733
  next unless complete
708
734
 
@@ -782,6 +808,7 @@ module Kward
782
808
  update_prompt_tabs
783
809
  tab.answer = tab.driver.submit(input, display_input: display_input, cancellation: tab.cancellation, steering: tab.steering) do |event|
784
810
  tab.record_event(event)
811
+ update_prompt_tabs if event.is_a?(Events::ImageReady) && !tab.equal?(active_tab)
785
812
  end
786
813
  tab.status = "ready"
787
814
  tab.unread = tab != active_tab
@@ -814,13 +841,20 @@ module Kward
814
841
  service_background_tab_runs
815
842
  tab = active_tab
816
843
  unless tab&.running? || tab&.local_busy?
844
+ offer_tab_images(tab) if tab
817
845
  input = @prompt.ask("You>")
818
846
  return { tab_action: :close } if input.nil? && tab && @tabs.length > 1
819
847
 
820
848
  return input
821
849
  end
822
850
 
823
- @prompt.begin_busy_input("You>") if @prompt.respond_to?(:begin_busy_input)
851
+ if @prompt.respond_to?(:begin_busy_input)
852
+ if tab.shell
853
+ @prompt.begin_busy_input(tab.shell.prompt_label, activity: "running", show_activity: false)
854
+ else
855
+ @prompt.begin_busy_input("You>")
856
+ end
857
+ end
824
858
  loop do
825
859
  service_background_tab_runs
826
860
  refresh_active_tab
@@ -834,6 +868,7 @@ module Kward
834
868
  sleep 0.01
835
869
  next
836
870
  end
871
+ offer_tab_images(tab)
837
872
  poll_result = @prompt.poll_input
838
873
  case poll_result
839
874
  when Hash
@@ -957,6 +992,8 @@ module Kward
957
992
  break unless @tab_live_view_token.equal?(token)
958
993
 
959
994
  if tab.idle?
995
+ next if tab.seen_events < tab.event_history.length
996
+
960
997
  renderer.call(:flush, tab.driver)
961
998
  break
962
999
  end
@@ -988,16 +1025,43 @@ module Kward
988
1025
  end
989
1026
  end
990
1027
 
1028
+ def offer_tab_images(tab)
1029
+ return unless tab.equal?(active_tab) && tab.agent && @prompt.respond_to?(:offer_generated_images)
1030
+ return unless tab_image_waiting?(tab)
1031
+
1032
+ offers = tab.image_offers || []
1033
+ tab.image_offers = nil
1034
+ # Only the input thread drains the queue; the turn worker can only append.
1035
+ tab.pending_images.size.times do
1036
+ offers << { store: @generated_images, owner: tab.agent.conversation, image: tab.pending_images.pop }
1037
+ end
1038
+ @prompt.offer_generated_images(offers)
1039
+ end
1040
+
1041
+ def suspend_tab_images(tab)
1042
+ return unless tab && @prompt.respond_to?(:take_generated_image_offers)
1043
+
1044
+ tab.image_offers = @prompt.take_generated_image_offers
1045
+ end
1046
+
1047
+ def discard_tab_images(tab)
1048
+ return unless tab&.agent && @generated_images
1049
+
1050
+ suspend_tab_images(tab)
1051
+ tab.image_offers = nil
1052
+ tab.pending_images.clear
1053
+ @generated_images.discard_owner(tab.agent.conversation)
1054
+ end
1055
+
991
1056
  def plugin_tab_type_for(driver)
992
1057
  plugin_registry.tab_type_for_id(driver.descriptor["plugin_tab_type"])
993
1058
  end
994
1059
 
995
1060
  def render_tab_answer(tab)
996
- return unless tab.status == "ready"
997
- return if tab.stream_state[:streamed]
998
- return if tab.answer.to_s.empty?
999
-
1000
- @prompt.say("\n#{colored(transcript_marker("Assistant"), :activity, :bold)} #{render_markdown_transcript(tab.answer)}\n")
1061
+ if tab.status == "ready" && !tab.stream_state[:streamed] && !tab.answer.to_s.empty?
1062
+ @prompt.say("\n#{colored(transcript_marker("Assistant"), :activity, :bold)} #{render_markdown_transcript(tab.answer)}\n")
1063
+ end
1064
+ print_turn_summary(tab.stream_state.delete(:turn_summary))
1001
1065
  end
1002
1066
 
1003
1067
  def new_tab_stream_state(driver)
@@ -1017,10 +1081,15 @@ module Kward
1017
1081
  label = tab.label.to_s.empty? ? default_tab_label(index) : tab.label.to_s
1018
1082
  binding = worktree_binding_for(tab)
1019
1083
  label = "#{label} · #{binding.branch}" if binding&.active?
1084
+ label = "#{label} · image" if tab != active_tab && tab_image_waiting?(tab)
1020
1085
  { name: label, color: tab_label_color(tab) }
1021
1086
  end
1022
1087
  end
1023
1088
 
1089
+ def tab_image_waiting?(tab)
1090
+ !tab.pending_images.empty? || (tab.image_offers && !tab.image_offers.empty?)
1091
+ end
1092
+
1024
1093
  def tab_label_color(tab)
1025
1094
  return :success if tab.status.to_s == "waiting_for_question"
1026
1095
  return :caution if tab.running? || tab.local_busy?
@@ -1102,8 +1171,9 @@ module Kward
1102
1171
  return runtime_output("Tab #{value} does not exist.") unless target_index.between?(0, @tabs.length - 1)
1103
1172
  return if target_index == @active_tab_index
1104
1173
 
1105
- save_active_tab_state
1106
1174
  stop_tab_live_view
1175
+ save_active_tab_state
1176
+ suspend_tab_images(active_tab)
1107
1177
  tab = @tabs.delete_at(@active_tab_index)
1108
1178
  @tabs.insert(target_index, tab)
1109
1179
  @active_tab_index = target_index
@@ -1150,6 +1220,9 @@ module Kward
1150
1220
  rescue StandardError
1151
1221
  nil
1152
1222
  end
1223
+ ensure
1224
+ @generated_images&.close
1225
+ @generated_images = nil
1153
1226
  end
1154
1227
 
1155
1228
  def build_plugin_tab_host(tab_type, workspace_root:, scope_key:)
@@ -7,6 +7,8 @@ module Kward
7
7
  private
8
8
 
9
9
  def tool_activity(tool_call)
10
+ return "Generating image…" if tool_call_name(tool_call) == "generate_image"
11
+
10
12
  "running #{tool_call_name(tool_call).tr("_", " ")}"
11
13
  end
12
14
 
@@ -19,10 +19,6 @@ module Kward
19
19
  when "reasoning"
20
20
  summary = response_item_text_parts(MessageAccess.value(item, :summary))
21
21
  summary.empty? ? response_item_text_parts(MessageAccess.value(item, :content)) : summary
22
- when "message"
23
- next [] unless MessageAccess.value(item, :phase).to_s == "commentary"
24
-
25
- response_item_text_parts(MessageAccess.value(item, :content))
26
22
  else
27
23
  []
28
24
  end
@@ -54,15 +50,12 @@ module Kward
54
50
  end
55
51
 
56
52
  def assistant_content_text(message)
57
- text = content_text(MessageAccess.content(message))
58
- return text unless text.empty?
59
-
60
- response_items(message).filter_map do |item|
53
+ text = response_items(message).filter_map do |item|
61
54
  next unless MessageAccess.value(item, :type) == "message"
62
- next if MessageAccess.value(item, :phase).to_s == "commentary"
63
55
 
64
56
  response_item_text(MessageAccess.value(item, :content))
65
- end.join
57
+ end.reject(&:empty?).join("\n\n")
58
+ text.empty? ? content_text(MessageAccess.content(message)) : text
66
59
  end
67
60
 
68
61
  def user_display_text(message)
@@ -61,6 +61,7 @@ module Kward
61
61
  reasoning_effort: current_reasoning_effort
62
62
  )
63
63
  agent = build_tab_agent(conversation, session, worktree: worktree)
64
+ discard_tab_images(tab)
64
65
  tab.session = track_session(session)
65
66
  tab.agent = agent
66
67
  tab.driver = SessionTabDriver.new(session: tab.session, agent: agent, worktree: worktree)
data/lib/kward/cli.rb CHANGED
@@ -4,6 +4,7 @@ require "thread"
4
4
  require "tty-prompt"
5
5
  require_relative "agent"
6
6
  require_relative "terminal/ansi"
7
+ require_relative "terminal/sequences"
7
8
  require_relative "version"
8
9
  require_relative "model/client"
9
10
  require_relative "compactor"
@@ -135,6 +136,7 @@ module Kward
135
136
  @working_directory = nil
136
137
  @prompt_delimited = false
137
138
  @requested_mode = "auto"
139
+ @startup_option_name = nil
138
140
  @skip_config = false
139
141
  @pending_reasoning_config = nil
140
142
  @pending_reasoning_config_mutex = Mutex.new
@@ -147,7 +149,11 @@ module Kward
147
149
  # @return [void]
148
150
  def run
149
151
  @argv = extract_global_options(@argv)
152
+ if @startup_option_name && (!@argv.empty? || @prompt_delimited)
153
+ raise ArgumentError, "Plugin startup option #{@startup_option_name} cannot be combined with a prompt or --"
154
+ end
150
155
  ConfigFiles.skip_config = @skip_config
156
+ ConfigFiles.ignore_local_instructions = @ignore_local_instructions
151
157
  with_working_directory { dispatch }
152
158
  rescue ConfigFiles::ConfigError => e
153
159
  warn config_error_message(e)
@@ -169,6 +175,7 @@ module Kward
169
175
  ensure
170
176
  shutdown_plugins if respond_to?(:shutdown_plugins, true)
171
177
  ConfigFiles.skip_config = false
178
+ ConfigFiles.ignore_local_instructions = false
172
179
  end
173
180
 
174
181
  def ensure_client!
@@ -220,9 +227,8 @@ module Kward
220
227
  end
221
228
 
222
229
  def dispatch
223
- if @prompt_delimited
224
- ConfigFiles.ensure_default_config!
225
- run_prompt_or_interactive
230
+ if @startup_option_name
231
+ discover_plugin_registry.run_startup_option(@startup_option_name)
226
232
  return
227
233
  end
228
234
 
@@ -240,6 +246,30 @@ module Kward
240
246
 
241
247
  ConfigFiles.ensure_default_config!
242
248
 
249
+ if @argv.first == "filter"
250
+ if help_option_arguments?(@argv[1..] || [])
251
+ print_command_help("filter")
252
+ return
253
+ end
254
+ raise ArgumentError, command_usage("filter") if @argv.length < 2
255
+
256
+ @requested_mode = "filter"
257
+ @argv = ["prompt", *(@argv[1..] || [])]
258
+ run_prompt_or_interactive
259
+ return
260
+ end
261
+
262
+ if @argv.first == "prompt"
263
+ if help_option_arguments?(@argv[1..] || [])
264
+ print_command_help("prompt")
265
+ return
266
+ end
267
+ raise ArgumentError, command_usage("prompt") if @argv.length < 2
268
+
269
+ run_prompt_or_interactive
270
+ return
271
+ end
272
+
243
273
  if @argv.first == "init"
244
274
  if help_option_arguments?(@argv[1..] || [])
245
275
  print_command_help("init")
@@ -377,10 +407,16 @@ module Kward
377
407
  return
378
408
  end
379
409
 
380
- run_prompt_or_interactive
410
+ raise ArgumentError, "Unknown command: #{@argv.first}" unless @argv.empty?
411
+
412
+ ensure_client!
413
+ interactive_loop
381
414
  end
382
415
 
383
416
  def edit_file_command(path)
417
+ terminal = $stdout if @stdin.tty? && $stdout.tty? && ENV["TERM"] != "dumb"
418
+ terminal&.write(TerminalSequences::ALTERNATE_SCREEN_ENTER)
419
+ terminal&.flush
384
420
  setup_interactive_prompt
385
421
  unless @prompt.respond_to?(:edit_file)
386
422
  raise ArgumentError, "The integrated editor requires an interactive terminal."
@@ -388,8 +424,13 @@ module Kward
388
424
 
389
425
  @prompt.edit_file(path, base_dir: Dir.pwd, allow_new: true)
390
426
  ensure
391
- @prompt.close if @prompt.respond_to?(:close) && prompt_interface?
392
- clear_interactive_warning_sink if respond_to?(:clear_interactive_warning_sink, true)
427
+ begin
428
+ @prompt.close if @prompt.respond_to?(:close) && prompt_interface?
429
+ ensure
430
+ terminal&.write(TerminalSequences::ALTERNATE_SCREEN_LEAVE)
431
+ terminal&.flush
432
+ clear_interactive_warning_sink if respond_to?(:clear_interactive_warning_sink, true)
433
+ end
393
434
  end
394
435
 
395
436
  def run_prompt_or_interactive
@@ -433,10 +474,12 @@ module Kward
433
474
  hook_manager: hook_manager,
434
475
  hook_context: hook_context
435
476
  )
477
+ summary = nil
436
478
  answer = if filter
437
479
  agent.ask(input)
438
480
  else
439
481
  agent.ask(input) do |event|
482
+ summary = event.summary if event.is_a?(Events::TurnSummary)
440
483
  result = render_blocking_turn_event(event, markdown_chunks)
441
484
  streamed = true if result
442
485
  assistant_streamed = true if result == :assistant_streamed
@@ -445,7 +488,13 @@ module Kward
445
488
  flush_markdown_deltas(markdown_chunks) if streamed
446
489
  return answer if filter
447
490
 
448
- assistant_streamed ? "" : render_markdown_transcript(answer)
491
+ text = assistant_streamed ? "" : render_markdown_transcript(answer)
492
+ stats = turn_summary_text(summary)
493
+ [text, stats.empty? ? nil : colored(stats, :metadata)].compact.reject(&:empty?).join("\n\n")
494
+ rescue StandardError
495
+ flush_markdown_deltas(markdown_chunks) unless filter
496
+ print_turn_summary(summary) unless filter
497
+ raise
449
498
  end
450
499
 
451
500
  def resolved_execution_mode(first_prompt:, stdin_input:)