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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +130 -0
- data/CONTRIBUTING.md +1 -1
- data/Gemfile.lock +6 -2
- data/README.md +3 -3
- data/Rakefile +3 -0
- data/doc/agent-tools.md +11 -0
- data/doc/api.md +4 -1
- data/doc/composer.md +4 -3
- data/doc/configuration.md +60 -11
- data/doc/extensibility.md +2 -0
- data/doc/getting-started.md +3 -3
- data/doc/git.md +1 -1
- data/doc/memory.md +1 -1
- data/doc/plugins.md +40 -5
- data/doc/providers.md +10 -4
- data/doc/rpc.md +125 -7
- data/doc/session-management.md +36 -1
- data/doc/shell.md +643 -27
- data/doc/tabs.md +2 -1
- data/doc/troubleshooting.md +1 -1
- data/doc/usage.md +21 -11
- data/examples/plugins/openrouter_proxy/README.md +103 -0
- data/examples/plugins/openrouter_proxy/fake_embeddings.rb +85 -0
- data/examples/plugins/openrouter_proxy/plugin.rb +7 -0
- data/examples/plugins/openrouter_proxy/protocol.rb +202 -0
- data/examples/plugins/openrouter_proxy/server.rb +186 -0
- data/examples/plugins/space_invaders.rb +0 -1
- data/exe/kward +2 -2
- data/exe/kwsh +7 -0
- data/kward.gemspec +3 -1
- data/lib/kward/agent.rb +43 -9
- data/lib/kward/cli/commands.rb +41 -11
- data/lib/kward/cli/interactive_turn.rb +10 -0
- data/lib/kward/cli/plugins.rb +5 -1
- data/lib/kward/cli/prompt_interface.rb +4 -1
- data/lib/kward/cli/rendering.rb +21 -28
- data/lib/kward/cli/runtime_helpers.rb +52 -34
- data/lib/kward/cli/settings/model.rb +1 -12
- data/lib/kward/cli/tabs.rb +91 -18
- data/lib/kward/cli/tool_summaries.rb +2 -0
- data/lib/kward/cli/transcript_formatter.rb +3 -10
- data/lib/kward/cli/worktrees.rb +1 -0
- data/lib/kward/cli.rb +56 -7
- data/lib/kward/config/core.rb +16 -5
- data/lib/kward/config/extensions.rb +3 -0
- data/lib/kward/config/paths.rb +11 -0
- data/lib/kward/config/settings.rb +7 -0
- data/lib/kward/conversation.rb +11 -1
- data/lib/kward/entrypoint.rb +14 -0
- data/lib/kward/events.rb +2 -0
- data/lib/kward/generated_images.rb +237 -0
- data/lib/kward/model/catalog.rb +6 -3
- data/lib/kward/model/client.rb +105 -36
- data/lib/kward/model/codex_images.rb +83 -0
- data/lib/kward/model/copilot_models.rb +8 -4
- data/lib/kward/model/model_info.rb +27 -9
- data/lib/kward/model/openrouter_model_cache.rb +4 -2
- data/lib/kward/model/payloads.rb +6 -5
- data/lib/kward/model/sources.rb +3 -1
- data/lib/kward/model/stream_parser.rb +4 -7
- data/lib/kward/pan/index.html.erb +8 -1
- data/lib/kward/pan/server.rb +13 -1
- data/lib/kward/plugins/codex_responses.rb +73 -0
- data/lib/kward/plugins/host.rb +16 -0
- data/lib/kward/plugins/http_server.rb +83 -0
- data/lib/kward/plugins/registry.rb +39 -0
- data/lib/kward/prompt_interface/composer_controller.rb +0 -1
- data/lib/kward/prompt_interface/composer_renderer.rb +159 -72
- data/lib/kward/prompt_interface/editor/controller.rb +1 -1
- data/lib/kward/prompt_interface/editor/renderer.rb +10 -7
- data/lib/kward/prompt_interface/file_overlay.rb +3 -3
- data/lib/kward/prompt_interface/generated_image_preview.rb +162 -0
- data/lib/kward/prompt_interface/git_prompt.rb +18 -11
- data/lib/kward/prompt_interface/interactive/renderer.rb +1 -1
- data/lib/kward/prompt_interface/interactive/state.rb +18 -20
- data/lib/kward/prompt_interface/key_handler.rb +108 -25
- data/lib/kward/prompt_interface/layout.rb +11 -1
- data/lib/kward/prompt_interface/overlay_renderer.rb +25 -8
- data/lib/kward/prompt_interface/project_browser.rb +27 -54
- data/lib/kward/prompt_interface/prompt_renderer.rb +27 -35
- data/lib/kward/prompt_interface/question_prompt.rb +19 -28
- data/lib/kward/prompt_interface/runtime_state.rb +31 -9
- data/lib/kward/prompt_interface/screen.rb +24 -21
- data/lib/kward/prompt_interface/selection_prompt.rb +8 -8
- data/lib/kward/prompt_interface/slash_overlay.rb +1 -1
- data/lib/kward/prompt_interface/transcript_renderer.rb +7 -6
- data/lib/kward/prompt_interface.rb +109 -39
- data/lib/kward/prompts.rb +7 -1
- data/lib/kward/pty/adaptive_output_sink.rb +38 -4
- data/lib/kward/pty/interactive_runner.rb +20 -28
- data/lib/kward/pty/transcript_normalizer.rb +65 -21
- data/lib/kward/rpc/config_manager.rb +3 -0
- data/lib/kward/rpc/server.rb +27 -5
- data/lib/kward/rpc/session_manager.rb +62 -17
- data/lib/kward/rpc/transcript_normalizer.rb +11 -7
- data/lib/kward/sessions/store.rb +30 -3
- data/lib/kward/shell/kwsh.rb +27 -7
- data/lib/kward/shell/kwshrc.rb +11 -2
- data/lib/kward/shell/native_arithmetic.rb +255 -0
- data/lib/kward/shell/native_arrays.rb +102 -0
- data/lib/kward/shell/native_braces.rb +96 -0
- data/lib/kward/shell/native_builtins.rb +476 -0
- data/lib/kward/shell/native_completion.rb +169 -0
- data/lib/kward/shell/native_condition.rb +75 -0
- data/lib/kward/shell/native_directory.rb +116 -0
- data/lib/kward/shell/native_exec_state.rb +73 -0
- data/lib/kward/shell/native_expansion.rb +252 -0
- data/lib/kward/shell/native_fields.rb +62 -0
- data/lib/kward/shell/native_glob.rb +96 -0
- data/lib/kward/shell/native_history.rb +242 -0
- data/lib/kward/shell/native_interactive.rb +164 -0
- data/lib/kward/shell/native_invocation.rb +42 -0
- data/lib/kward/shell/native_jobs.rb +250 -0
- data/lib/kward/shell/native_line_editor.rb +382 -0
- data/lib/kward/shell/native_line_terminal.rb +286 -0
- data/lib/kward/shell/native_locale.rb +109 -0
- data/lib/kward/shell/native_locals.rb +57 -0
- data/lib/kward/shell/native_pattern.rb +200 -0
- data/lib/kward/shell/native_process.rb +279 -0
- data/lib/kward/shell/native_program.rb +40 -0
- data/lib/kward/shell/native_programmable_completion.rb +115 -0
- data/lib/kward/shell/native_prompt_format.rb +89 -0
- data/lib/kward/shell/native_runtime.rb +964 -0
- data/lib/kward/shell/native_substitution.rb +110 -0
- data/lib/kward/shell/native_syntax.rb +946 -0
- data/lib/kward/shell/native_traps.rb +196 -0
- data/lib/kward/shell/native_vi.rb +407 -0
- data/lib/kward/shell/native_vi_terminal.rb +19 -0
- data/lib/kward/shell/native_worker.rb +82 -0
- data/lib/kward/shell/persistent_session.rb +184 -190
- data/lib/kward/shell/prompt.rb +14 -0
- data/lib/kward/shell/prompt_agent.rb +17 -0
- data/lib/kward/shell/script_runner.rb +244 -0
- data/lib/kward/shell/standalone_agent.rb +31 -0
- data/lib/kward/shell/standalone_link.rb +78 -0
- data/lib/kward/shell/standalone_terminal.rb +287 -0
- data/lib/kward/terminal/keys.rb +22 -1
- data/lib/kward/terminal/locale_text.rb +29 -0
- data/lib/kward/terminal/sequences.rb +45 -4
- data/lib/kward/tools/datetime.rb +85 -0
- data/lib/kward/tools/generate_image.rb +36 -0
- data/lib/kward/tools/registry.rb +35 -9
- data/lib/kward/tools/tool_call.rb +3 -1
- data/lib/kward/transcripts/markdown_transcript.rb +8 -0
- data/lib/kward/transcripts/turn_summary_formatter.rb +93 -0
- data/lib/kward/transport/gateway.rb +2 -1
- data/lib/kward/turn_summary.rb +68 -0
- data/lib/kward/version.rb +1 -1
- data/lib/main.rb +6 -3
- metadata +84 -1
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
require "securerandom"
|
|
2
|
+
require_relative "../model/client"
|
|
3
|
+
|
|
4
|
+
module Kward
|
|
5
|
+
# Adapts Kward's normal Codex provider stream to the plugin Responses event
|
|
6
|
+
# contract without exposing the configured credentials.
|
|
7
|
+
class PluginCodexResponses
|
|
8
|
+
class AuthenticationError < StandardError; end
|
|
9
|
+
|
|
10
|
+
class RequestError < StandardError
|
|
11
|
+
attr_reader :status
|
|
12
|
+
|
|
13
|
+
def initialize(status)
|
|
14
|
+
@status = status.to_i
|
|
15
|
+
super("Codex request failed (HTTP #{@status})")
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def initialize(client: Kward::Client.new)
|
|
20
|
+
@client = client
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Streams Kward Codex events using the normal configured Codex credentials
|
|
24
|
+
# and endpoint, translating them to Responses-shaped events for plugins.
|
|
25
|
+
def stream(model:, input:, instructions: nil, text: nil, max_output_tokens: nil, cancellation: nil)
|
|
26
|
+
messages = input_messages(input, instructions)
|
|
27
|
+
output_text = +""
|
|
28
|
+
emit_text = lambda do |delta|
|
|
29
|
+
next unless delta.is_a?(String) && !delta.empty?
|
|
30
|
+
|
|
31
|
+
output_text << delta
|
|
32
|
+
yield "type" => "response.output_text.delta", "delta" => delta
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
result = @client.chat(
|
|
36
|
+
messages,
|
|
37
|
+
provider: "codex",
|
|
38
|
+
provider_required: true,
|
|
39
|
+
model: model,
|
|
40
|
+
max_tokens: max_output_tokens,
|
|
41
|
+
text_format: text,
|
|
42
|
+
cancellation: cancellation,
|
|
43
|
+
on_assistant_delta: emit_text
|
|
44
|
+
)
|
|
45
|
+
emit_text.call(result["content"]) if output_text.empty? && result["content"].is_a?(String)
|
|
46
|
+
yield "type" => "response.completed", "response" => { "id" => "codex-#{SecureRandom.hex(12)}", "usage" => result["usage"] }
|
|
47
|
+
nil
|
|
48
|
+
rescue Kward::Client::AuthenticationError => error
|
|
49
|
+
raise AuthenticationError, error.message
|
|
50
|
+
rescue Kward::Client::RequestError => error
|
|
51
|
+
raise RequestError, error.code
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def input_messages(input, instructions)
|
|
57
|
+
raise ArgumentError, "Codex input must be an array of messages" unless input.is_a?(Array)
|
|
58
|
+
|
|
59
|
+
messages = []
|
|
60
|
+
messages << { role: "system", content: instructions } if instructions.is_a?(String) && !instructions.empty?
|
|
61
|
+
input.each_with_index do |item, index|
|
|
62
|
+
role = item["role"] || item[:role] if item.is_a?(Hash)
|
|
63
|
+
content = item.key?("content") ? item["content"] : item[:content] if item.is_a?(Hash)
|
|
64
|
+
unless %w[user assistant].include?(role) && content.is_a?(String)
|
|
65
|
+
raise ArgumentError, "Codex input message #{index} must have a supported role and string content"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
messages << { role: role, content: content }
|
|
69
|
+
end
|
|
70
|
+
messages
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
data/lib/kward/plugins/host.rb
CHANGED
|
@@ -6,6 +6,8 @@ require_relative "../config_files"
|
|
|
6
6
|
require_relative "../deep_copy"
|
|
7
7
|
require_relative "../private_file"
|
|
8
8
|
require_relative "resources"
|
|
9
|
+
require_relative "http_server"
|
|
10
|
+
require_relative "codex_responses"
|
|
9
11
|
|
|
10
12
|
# Namespace for the Kward CLI agent runtime.
|
|
11
13
|
module Kward
|
|
@@ -154,6 +156,20 @@ module Kward
|
|
|
154
156
|
@resources.background(name: name, cancellation: cancellation, &block)
|
|
155
157
|
end
|
|
156
158
|
|
|
159
|
+
# Streams Responses-shaped events through Kward's normal Codex provider
|
|
160
|
+
# client without exposing the configured credentials to plugin code.
|
|
161
|
+
def codex_responses
|
|
162
|
+
@codex_responses ||= PluginCodexResponses.new
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Creates a loopback-only HTTP listener owned by this plugin. The listener
|
|
166
|
+
# is stopped automatically during plugin reload or shutdown.
|
|
167
|
+
def http_server(host: "127.0.0.1", port: 0, max_body_bytes: PluginHTTPServer::DEFAULT_MAX_BODY_BYTES, request_timeout: PluginHTTPServer::DEFAULT_REQUEST_TIMEOUT, max_clients: 8, &handler)
|
|
168
|
+
server = PluginHTTPServer.new(host: host, port: port, max_body_bytes: max_body_bytes, request_timeout: request_timeout, max_clients: max_clients, &handler)
|
|
169
|
+
on_cleanup(server) { |resource| resource.stop }
|
|
170
|
+
server
|
|
171
|
+
end
|
|
172
|
+
|
|
157
173
|
# Registers idempotent cleanup for a subscription or other plugin resource.
|
|
158
174
|
# The returned disposable may be invoked early; otherwise Kward invokes it
|
|
159
175
|
# during plugin reload or shutdown.
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
require "ipaddr"
|
|
2
|
+
require "webrick"
|
|
3
|
+
|
|
4
|
+
module Kward
|
|
5
|
+
# Small, plugin-owned HTTP listener constrained to loopback.
|
|
6
|
+
class PluginHTTPServer
|
|
7
|
+
DEFAULT_MAX_BODY_BYTES = 1_048_576
|
|
8
|
+
DEFAULT_REQUEST_TIMEOUT = 30
|
|
9
|
+
|
|
10
|
+
attr_reader :host, :requested_port
|
|
11
|
+
|
|
12
|
+
def initialize(host: "127.0.0.1", port: 0, max_body_bytes: DEFAULT_MAX_BODY_BYTES, request_timeout: DEFAULT_REQUEST_TIMEOUT, max_clients: 8, &handler)
|
|
13
|
+
raise ArgumentError, "Plugin HTTP server requires a handler" unless handler
|
|
14
|
+
raise ArgumentError, "Plugin HTTP server must bind to loopback" unless IPAddr.new(host).loopback?
|
|
15
|
+
raise ArgumentError, "Plugin HTTP server port must be between 0 and 65535" unless Integer(port).between?(0, 65_535)
|
|
16
|
+
raise ArgumentError, "Plugin HTTP server body limit must be positive" unless Integer(max_body_bytes).positive?
|
|
17
|
+
raise ArgumentError, "Plugin HTTP server request timeout must be positive" unless Float(request_timeout).positive?
|
|
18
|
+
raise ArgumentError, "Plugin HTTP server client limit must be positive" unless Integer(max_clients).positive?
|
|
19
|
+
|
|
20
|
+
@host = host
|
|
21
|
+
@requested_port = Integer(port)
|
|
22
|
+
@handler = handler
|
|
23
|
+
@server = WEBrick::HTTPServer.new(
|
|
24
|
+
BindAddress: host,
|
|
25
|
+
Port: @requested_port,
|
|
26
|
+
MaxReqBytes: Integer(max_body_bytes),
|
|
27
|
+
RequestTimeout: Float(request_timeout),
|
|
28
|
+
MaxClients: Integer(max_clients),
|
|
29
|
+
AccessLog: [],
|
|
30
|
+
Logger: WEBrick::Log.new(File::NULL)
|
|
31
|
+
)
|
|
32
|
+
@thread = nil
|
|
33
|
+
@mutex = Mutex.new
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def start
|
|
37
|
+
@mutex.synchronize do
|
|
38
|
+
return self if @thread&.alive?
|
|
39
|
+
|
|
40
|
+
@server.mount_proc("/") { |request, response| @handler.call(request, response) }
|
|
41
|
+
@thread = Thread.new { @server.start }
|
|
42
|
+
@thread.report_on_exception = false
|
|
43
|
+
end
|
|
44
|
+
wait_until_ready
|
|
45
|
+
self
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def wait
|
|
49
|
+
@thread&.join
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def stop
|
|
53
|
+
thread = @mutex.synchronize do
|
|
54
|
+
current = @thread
|
|
55
|
+
@server.shutdown if current&.alive?
|
|
56
|
+
@thread = nil
|
|
57
|
+
current
|
|
58
|
+
end
|
|
59
|
+
thread&.join(2)
|
|
60
|
+
self
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def port
|
|
64
|
+
@port || requested_port
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
|
|
69
|
+
def wait_until_ready
|
|
70
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 2
|
|
71
|
+
loop do
|
|
72
|
+
if @server.status == :Running
|
|
73
|
+
@port = @server.listeners.first&.addr&.[](1) || requested_port
|
|
74
|
+
return
|
|
75
|
+
end
|
|
76
|
+
raise @thread.value unless @thread.alive?
|
|
77
|
+
raise "Plugin HTTP server did not start" if Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline
|
|
78
|
+
|
|
79
|
+
sleep 0.01
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -33,6 +33,9 @@ module Kward
|
|
|
33
33
|
# registry. The handler receives parsed arguments and a runtime context.
|
|
34
34
|
Tool = Struct.new(:name, :description, :schema, :path, :handler, keyword_init: true)
|
|
35
35
|
|
|
36
|
+
# A plugin-owned top-level mode selected by a declared CLI switch.
|
|
37
|
+
StartupOption = Struct.new(:name, :description, :plugin_id, :path, :handler, keyword_init: true)
|
|
38
|
+
|
|
36
39
|
STATUS_PRIORITIES = { low: 0, normal: 1, high: 2 }.freeze
|
|
37
40
|
STATUS_SEPARATOR = " · "
|
|
38
41
|
|
|
@@ -505,6 +508,14 @@ module Kward
|
|
|
505
508
|
@registry.register_transport(name, id: id, capabilities: capabilities, execution_profile: execution_profile, path: @path, &block)
|
|
506
509
|
end
|
|
507
510
|
|
|
511
|
+
# Registers a no-argument top-level CLI mode for an identified plugin.
|
|
512
|
+
# The handler runs only when the user selects this option.
|
|
513
|
+
def startup_option(name, description:, &block)
|
|
514
|
+
raise ArgumentError, "Plugin startup options require stable plugin identity" unless @host
|
|
515
|
+
|
|
516
|
+
@registry.register_startup_option(name, description: description, plugin_id: @host.id, path: @path, &block)
|
|
517
|
+
end
|
|
518
|
+
|
|
508
519
|
private
|
|
509
520
|
|
|
510
521
|
def register_lifecycle(event, &block)
|
|
@@ -540,6 +551,7 @@ module Kward
|
|
|
540
551
|
@tab_types_by_id = {}
|
|
541
552
|
@transports = {}
|
|
542
553
|
@transports_by_id = {}
|
|
554
|
+
@startup_options = {}
|
|
543
555
|
@statuses = {}
|
|
544
556
|
@status_sequence = 0
|
|
545
557
|
@footer_path = nil
|
|
@@ -627,6 +639,20 @@ module Kward
|
|
|
627
639
|
@transports_by_id[id.to_s]
|
|
628
640
|
end
|
|
629
641
|
|
|
642
|
+
def startup_options
|
|
643
|
+
@startup_options.values.dup
|
|
644
|
+
end
|
|
645
|
+
|
|
646
|
+
def startup_option_for(name)
|
|
647
|
+
@startup_options[name.to_s]
|
|
648
|
+
end
|
|
649
|
+
|
|
650
|
+
def run_startup_option(name)
|
|
651
|
+
option = startup_option_for(name) || raise(ArgumentError, "Unknown plugin startup option: #{name}")
|
|
652
|
+
start!
|
|
653
|
+
option.handler.call(plugin_for(option.plugin_id))
|
|
654
|
+
end
|
|
655
|
+
|
|
630
656
|
def status?
|
|
631
657
|
!@statuses.empty?
|
|
632
658
|
end
|
|
@@ -894,6 +920,19 @@ module Kward
|
|
|
894
920
|
@tab_types_by_id[id] = tab_type
|
|
895
921
|
end
|
|
896
922
|
|
|
923
|
+
def register_startup_option(name, description:, plugin_id:, path: nil, &handler)
|
|
924
|
+
name = name.to_s
|
|
925
|
+
raise "Plugin startup option must look like --name: #{name}" unless name.match?(/\A--[a-z][a-z0-9-]*\z/)
|
|
926
|
+
raise "Plugin startup option requires a handler: #{name}" unless handler
|
|
927
|
+
|
|
928
|
+
if @startup_options.key?(name)
|
|
929
|
+
emit_warning "Warning: skipping duplicate Kward plugin startup option #{name}: #{path}"
|
|
930
|
+
return nil
|
|
931
|
+
end
|
|
932
|
+
|
|
933
|
+
@startup_options[name] = StartupOption.new(name: name, description: description.to_s, plugin_id: plugin_id, path: path, handler: handler)
|
|
934
|
+
end
|
|
935
|
+
|
|
897
936
|
def register_transport(name, id:, capabilities: nil, execution_profile: nil, path: nil, &handler)
|
|
898
937
|
name = name.to_s
|
|
899
938
|
id = id.to_s
|
|
@@ -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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
cursor_row =
|
|
35
|
-
|
|
36
|
-
|
|
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],
|
|
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
|
-
|
|
84
|
-
if
|
|
85
|
-
|
|
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
|
-
|
|
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 && @
|
|
95
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
-
|
|
155
|
-
|
|
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
|
|
264
|
-
|
|
265
|
-
|
|
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
|
-
|
|
281
|
-
title =
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
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)
|
|
@@ -7,8 +7,15 @@ module Kward
|
|
|
7
7
|
private
|
|
8
8
|
|
|
9
9
|
def editor_layout(width, height = screen_height)
|
|
10
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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)
|
|
@@ -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)
|