Class: Ask::Sandbox::Daytona

Inherits:
Base
  • Object
show all
Defined in:
lib/ask/sandbox/daytona.rb

Overview

Executes commands in a Daytona sandbox via the official daytona gem.

The daytona gem is loaded lazily (inside #call). If the gem is not installed, a clear LoadError is raised with installation instructions.

Examples:

sandbox = Ask::Sandbox::Daytona.new(
  api_key: "dta_xxxx",
  server_url: "https://api.daytona.io"
)
sandbox.call(["ruby", "-e", "puts 1+1"])

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, server_url: nil, image: nil, timeout: 120) ⇒ Daytona

Returns a new instance of Daytona.

Parameters:

  • api_key (String, nil) (defaults to: nil)

    Daytona API key. If nil, tries to resolve via Auth.lookup(“DAYTONA_API_KEY”) or ENV.

  • server_url (String, nil) (defaults to: nil)

    Daytona server URL. Falls back to the daytona gem’s default.

  • image (String, nil) (defaults to: nil)

    sandbox image (e.g. “ruby:3.4”)

  • timeout (Integer) (defaults to: 120)

    default timeout in seconds (default: 120 —Daytona sandboxes may take time to boot)



25
26
27
28
29
30
# File 'lib/ask/sandbox/daytona.rb', line 25

def initialize(api_key: nil, server_url: nil, image: nil, timeout: 120)
  @api_key = api_key
  @server_url = server_url
  @image = image
  @default_timeout = timeout
end

Instance Method Details

#call(command, timeout: @default_timeout, workdir: nil, env: {}, stdin: nil) ⇒ Ask::Sandbox::Result

Execute a command in the sandbox.

Lazily loads the daytona gem on first call.

Parameters:

  • command (String, Array<String>)

    When a String, executed via a shell (bash -c). When an Array, executed directly with no shell interpretation.

  • timeout (Integer) (defaults to: @default_timeout)

    max execution time in seconds (default: 30)

  • workdir (String, nil) (defaults to: nil)

    working directory inside the sandbox

  • env (Hash{String => String}) (defaults to: {})

    extra environment variables

  • stdin (String, nil) (defaults to: nil)

    data to pipe to the command’s stdin

Returns:

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
# File 'lib/ask/sandbox/daytona.rb', line 35

def call(command, timeout: @default_timeout, workdir: nil, env: {}, stdin: nil)
  raise ArgumentError, "command must not be nil" if command.nil?
  raise ArgumentError, "command must not be empty" if command.respond_to?(:empty?) && command.empty?

  api_key = resolve_api_key
  ensure_daytona_gem!
  execute_on_daytona(command, api_key, timeout)
end