Module: LLM::Tool::Utils::Process

Defined in:
lib/llm/tools/utils/process.rb

Instance Method Summary collapse

Instance Method Details

#wait(command:, timeout:)

Note:

When the timeout expires this method will throw :kill rather than raise an exception.

This method returns an undefined value.

This method waits for a command to finish but within timeout. A timeout that expires will throw back to the caller.

Parameters:

Raises:

  • (UncaughtThrowError) —

    When the throw does not have a catch.



18
19
20
21
22
23
24
25
26
27
# File 'lib/llm/tools/utils/process.rb', line 18

def wait(command:, timeout:)
  start = now
  while command.running? || status_unavailable?(command)
    if now - start > timeout
      command.kill!
      throw(:kill, true)
    end
    sleep 0.01
  end
end

#spawn(command:, arguments:, env: {}, max_bytes: self.class.max_bytes) ⇒ LLM::Command

Spawn a command from a command and arguments, without going through a shell. The command's stdout and stderr are each capped at max_bytes.

Parameters:

  • command (String) —

    The command to run

  • arguments (Array<String>) —

    One or more arguments

  • env (Hash) (defaults to: {}) —

    Extra environment variables to set for the command

  • max_bytes (Integer) (defaults to: self.class.max_bytes) —

    The max number of bytes to keep per stream. Defaults to the including tool's self.class.max_bytes.

Returns:

Raises:

  • (ArgumentError) —

    When max_bytes is nil



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/llm/tools/utils/process.rb', line 45

def spawn(command:, arguments:, env: {}, max_bytes: self.class.max_bytes)
  if Integer(max_bytes, exception: false).nil?
    raise ArgumentError, "max_bytes cannot be nil"
  end
  LLM::Command
    .new(command)
    .env(env)
    .arguments(*[*arguments])
    .limit(stdout: max_bytes, stderr: max_bytes)
    .spawn
end

#now ⇒ Numeric

Returns:

  • (Numeric)


59
60
61
# File 'lib/llm/tools/utils/process.rb', line 59

def now
  ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
end