Class: Kward::Kwsh
- Inherits:
-
Object
- Object
- Kward::Kwsh
- Defined in:
- lib/kward/shell/kwsh.rb
Overview
Kward-native embedded shell command runner.
Defined Under Namespace
Classes: Completion, Result
Constant Summary collapse
- BUILTINS =
Kwshrc::PROMPT_BUILTINS
- DEFAULT_SHELL =
"/bin/sh"- DEFAULT_TIMEOUT_SECONDS =
300- DEFAULT_MAX_OUTPUT_BYTES =
1_048_576- DEFAULT_HISTORY_LIMIT =
1_000- CONTEXT_OUTPUT_BYTES =
32_000
Instance Attribute Summary collapse
-
#cwd ⇒ Object
readonly
Returns the value of attribute cwd.
-
#last_command ⇒ Object
readonly
Returns the value of attribute last_command.
-
#timeout_seconds ⇒ Object
readonly
Returns the value of attribute timeout_seconds.
Class Method Summary collapse
Instance Method Summary collapse
- #child_env(interactive: false) ⇒ Object
- #command_shell ⇒ Object
- #complete(input, cursor) ⇒ Object
-
#complete_directory(input, cursor, root: @cwd) ⇒ Object
Directory field completion uses shell matching but no shell quoting or expansion.
-
#context_snapshot(max_output_bytes: CONTEXT_OUTPUT_BYTES) ⇒ Object
Returns bounded shell state for an explicit shell-agent prompt.
- #editor_command_result(command, display_command: command) ⇒ Object
- #expand_alias(command, interactive: false) ⇒ Object
-
#initialize(cwd: Dir.pwd, env: ENV.to_h, shell: DEFAULT_SHELL, configured_env: {}, aliases: {}, timeout_seconds: DEFAULT_TIMEOUT_SECONDS, max_output_bytes: DEFAULT_MAX_OUTPUT_BYTES) ⇒ Kwsh
constructor
A new instance of Kwsh.
- #prompt_label ⇒ Object
-
#record_interactive_result(output:, exit_status:) ⇒ Object
Records the output of an interactive command after terminal handoff.
- #run(input, cancellation: nil, &block) ⇒ Object
-
#run_for_agent(input, timeout_seconds: nil, cancellation: nil) ⇒ Object
Runs a command for the shell assistant without handing it the terminal.
Constructor Details
#initialize(cwd: Dir.pwd, env: ENV.to_h, shell: DEFAULT_SHELL, configured_env: {}, aliases: {}, timeout_seconds: DEFAULT_TIMEOUT_SECONDS, max_output_bytes: DEFAULT_MAX_OUTPUT_BYTES) ⇒ Kwsh
Returns a new instance of Kwsh.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/kward/shell/kwsh.rb', line 34 def initialize(cwd: Dir.pwd, env: ENV.to_h, shell: DEFAULT_SHELL, configured_env: {}, aliases: {}, timeout_seconds: DEFAULT_TIMEOUT_SECONDS, max_output_bytes: DEFAULT_MAX_OUTPUT_BYTES) @cwd = File.(cwd.to_s.empty? ? Dir.pwd : cwd.to_s) @previous_cwd = nil @env = env.to_h.transform_keys(&:to_s).transform_values(&:to_s) @env.merge!(configured_env.to_h.transform_keys(&:to_s).transform_values(&:to_s)) @env["PWD"] = @cwd configure_rbenv_environment configure_color_environment @aliases = aliases.to_h.transform_keys(&:to_s).transform_values(&:to_s) @shell = shell.to_s.empty? ? DEFAULT_SHELL : shell.to_s @timeout_seconds = timeout_seconds.to_i.positive? ? timeout_seconds.to_i : DEFAULT_TIMEOUT_SECONDS @max_output_bytes = max_output_bytes.to_i.positive? ? max_output_bytes.to_i : DEFAULT_MAX_OUTPUT_BYTES @last_command = nil @last_result = nil end |
Instance Attribute Details
#cwd ⇒ Object (readonly)
Returns the value of attribute cwd.
24 25 26 |
# File 'lib/kward/shell/kwsh.rb', line 24 def cwd @cwd end |
#last_command ⇒ Object (readonly)
Returns the value of attribute last_command.
24 25 26 |
# File 'lib/kward/shell/kwsh.rb', line 24 def last_command @last_command end |
#timeout_seconds ⇒ Object (readonly)
Returns the value of attribute timeout_seconds.
24 25 26 |
# File 'lib/kward/shell/kwsh.rb', line 24 def timeout_seconds @timeout_seconds end |
Class Method Details
.valid_alias_name?(name) ⇒ Boolean
452 453 454 |
# File 'lib/kward/shell/kwsh.rb', line 452 def self.valid_alias_name?(name) Kwshrc.valid_alias_name?(name) end |
Instance Method Details
#child_env(interactive: false) ⇒ Object
30 31 32 |
# File 'lib/kward/shell/kwsh.rb', line 30 def child_env(interactive: false) @env.dup end |
#command_shell ⇒ Object
26 27 28 |
# File 'lib/kward/shell/kwsh.rb', line 26 def command_shell @shell end |
#complete(input, cursor) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/kward/shell/kwsh.rb', line 99 def complete(input, cursor) token = completion_token(input.to_s, cursor.to_i) return nil if token[:command] && token[:text].empty? completion_text = token[:path_text] || token[:text] candidates = if token[:command] && !path_like_token?(completion_text) && !token[:quote] command_candidates(completion_text) else path_candidates(completion_text, directories_only: cd_completion?(input, token), quote: token[:quote]) end return nil if candidates.empty? replacement = completion_replacement(token[:text], candidates) Completion.new(range: token[:range], replacement: replacement, candidates: candidates) end |
#complete_directory(input, cursor, root: @cwd) ⇒ Object
Directory field completion uses shell matching but no shell quoting or expansion.
116 117 118 119 120 121 122 |
# File 'lib/kward/shell/kwsh.rb', line 116 def complete_directory(input, cursor, root: @cwd) prefix = input[0...cursor] candidates = path_candidates(prefix, directories_only: true, literal: true, root: root) return if candidates.empty? Completion.new(range: 0...input.length, replacement: completion_replacement(prefix, candidates), candidates: candidates) end |
#context_snapshot(max_output_bytes: CONTEXT_OUTPUT_BYTES) ⇒ Object
Returns bounded shell state for an explicit shell-agent prompt.
90 91 92 93 94 95 96 97 |
# File 'lib/kward/shell/kwsh.rb', line 90 def context_snapshot(max_output_bytes: CONTEXT_OUTPUT_BYTES) { cwd: @cwd, last_command: @last_command, exit_status: @last_result&.exit_status, last_output: context_output(@last_result&.output, max_output_bytes) } end |
#editor_command_result(command, display_command: command) ⇒ Object
136 137 138 139 |
# File 'lib/kward/shell/kwsh.rb', line 136 def editor_command_result(command, display_command: command) = (command, interactive: true) kward_command_result(, display_command: display_command) end |
#expand_alias(command, interactive: false) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/kward/shell/kwsh.rb', line 124 def (command, interactive: false) words = shell_words(command) return command if words.empty? || BUILTINS.include?(words.first) return command unless @aliases[words.first] rest = command.sub(/\A\s*#{Regexp.escape(words.first)}\b\s*/, "") = [@aliases.fetch(words.first), rest].reject(&:empty?).join(" ") interactive ? .sub(/\A\s*(?:capture|pty)(?:\s+|\z)/, "") : rescue ArgumentError command end |
#prompt_label ⇒ Object
50 51 52 |
# File 'lib/kward/shell/kwsh.rb', line 50 def prompt_label "Shell #{display_cwd} $" end |
#record_interactive_result(output:, exit_status:) ⇒ Object
Records the output of an interactive command after terminal handoff.
83 84 85 86 87 |
# File 'lib/kward/shell/kwsh.rb', line 83 def record_interactive_result(output:, exit_status:) return unless @last_command @last_result = Result.new(output: ANSI.sanitize_transcript(output.to_s), exit_status: exit_status) end |
#run(input, cancellation: nil, &block) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/kward/shell/kwsh.rb', line 54 def run(input, cancellation: nil, &block) command = input.to_s.strip return Result.new(output: "", exit_status: 0) if command.empty? result = (command, cancellation: cancellation, &block) remember_result(command, result) result end |
#run_for_agent(input, timeout_seconds: nil, cancellation: nil) ⇒ Object
Runs a command for the shell assistant without handing it the terminal. Built-ins still execute in this Kwsh instance so their state persists.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/kward/shell/kwsh.rb', line 65 def run_for_agent(input, timeout_seconds: nil, cancellation: nil) command = input.to_s.strip return run(command, cancellation: cancellation) if command.empty? result = run(command, cancellation: cancellation) return result unless result.interactive_command result = execute( result.interactive_command, display_command: command, timeout_seconds: timeout_seconds, cancellation: cancellation ) remember_result(command, result) result end |