Skip to content
Kward Search API index

Class: Kward::Tools::RunShellCommand

Inherits:
Base
  • Object
show all
Defined in:
lib/kward/tools/run_shell_command.rb

Overview

Tool wrapper for bounded shell commands in the workspace or active shell.

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#schema

Constructor Details

#initialize(workspace: nil, shell_prompt_session: nil) ⇒ RunShellCommand

Builds the tool schema and stores the execution dependency.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kward/tools/run_shell_command.rb', line 11

def initialize(workspace: nil, shell_prompt_session: nil)
  raise ArgumentError, "RunShellCommand requires a workspace or shell session" unless workspace || shell_prompt_session
  raise ArgumentError, "RunShellCommand accepts only one execution target" if workspace && shell_prompt_session

  @workspace = workspace
  @shell_prompt_session = shell_prompt_session
  description = shell_prompt_session ?
    "Run a bounded noninteractive command in the active embedded shell session." :
    "Run a shell command from the workspace root."
  timeout_description = shell_prompt_session ?
    "Timeout seconds; defaults to the embedded shell setting." :
    "Timeout seconds; default 30."
  super(
    "run_shell_command",
    description,
    properties: {
      command: { type: "string", description: "Command to run." },
      timeout_seconds: { type: "integer", description: timeout_description }
    },
    required: ["command"]
  )
end

Instance Method Details

#call(args, _conversation, cancellation: nil) ⇒ Object

Executes the tool and returns model-facing output text.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kward/tools/run_shell_command.rb', line 35

def call(args, _conversation, cancellation: nil)
  command = argument(args, :command, "")
  timeout_seconds = argument(
    args,
    :timeout_seconds,
    @shell_prompt_session ? @shell_prompt_session.timeout_seconds : Workspace::DEFAULT_COMMAND_TIMEOUT_SECONDS
  )

  if @shell_prompt_session
    @shell_prompt_session.run(command, timeout_seconds: timeout_seconds, cancellation: cancellation)
  else
    @workspace.run_shell_command(command, timeout_seconds: timeout_seconds, cancellation: cancellation)
  end
end