Skip to content
Kward Search API index

Class: Kward::ShellPromptSession

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/shell/prompt_session.rb

Overview

Scoped bridge between a transient shell-agent turn and the live shell state.

Constant Summary collapse

MAX_PREPARED_COMMAND_BYTES =
32_000
UNSAFE_COMMAND_CONTROL_PATTERN =
/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F\r]/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shell) ⇒ ShellPromptSession

Returns a new instance of ShellPromptSession.



12
13
14
15
# File 'lib/kward/shell/prompt_session.rb', line 12

def initialize(shell)
  @shell = shell
  @prepared_command = nil
end

Instance Attribute Details

#shellObject (readonly)

Returns the value of attribute shell.



10
11
12
# File 'lib/kward/shell/prompt_session.rb', line 10

def shell
  @shell
end

Instance Method Details

#contextObject



17
18
19
# File 'lib/kward/shell/prompt_session.rb', line 17

def context
  @shell.context_snapshot
end

#prepare(command) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
# File 'lib/kward/shell/prompt_session.rb', line 32

def prepare(command)
  value = command.to_s
  raise ArgumentError, "A shell command is required." if value.strip.empty?
  raise ArgumentError, "Prepared shell command is too large." if value.bytesize > MAX_PREPARED_COMMAND_BYTES
  raise ArgumentError, "Prepared shell command contains unsafe control characters." if value.match?(UNSAFE_COMMAND_CONTROL_PATTERN)

  @prepared_command = value
  "Prepared the command in the shell prompt. It will not run until you press Enter."
end

#prepared_commandObject



42
43
44
# File 'lib/kward/shell/prompt_session.rb', line 42

def prepared_command
  @prepared_command&.dup
end

#run(command, timeout_seconds: nil, cancellation: nil) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
# File 'lib/kward/shell/prompt_session.rb', line 25

def run(command, timeout_seconds: nil, cancellation: nil)
  raise ArgumentError, "A shell command is required." if command.to_s.strip.empty?

  result = @shell.run_for_agent(command, timeout_seconds: timeout_seconds, cancellation: cancellation)
  format_result(result)
end

#timeout_secondsObject



21
22
23
# File 'lib/kward/shell/prompt_session.rb', line 21

def timeout_seconds
  @shell.timeout_seconds
end