Class: Daytona::ComputerUse::Screenshot

Inherits:
Object
  • Object
show all
Includes:
Instrumentation
Defined in:
lib/daytona/computer_use.rb

Overview

Screenshot operations for computer use functionality.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

included

Constructor Details

#initialize(sandbox_id:, toolbox_api:, otel_state: nil) ⇒ Screenshot

Returns a new instance of Screenshot.

Parameters:

  • sandbox_id (String)

    The ID of the sandbox

  • toolbox_api (DaytonaToolboxApiClient::ComputerUseApi)

    API client for sandbox operations

  • otel_state (Daytona::OtelState, nil) (defaults to: nil)


234
235
236
237
238
# File 'lib/daytona/computer_use.rb', line 234

def initialize(sandbox_id:, toolbox_api:, otel_state: nil)
  @sandbox_id = sandbox_id
  @toolbox_api = toolbox_api
  @otel_state = otel_state
end

Instance Attribute Details

#sandbox_idString (readonly)

Returns The ID of the sandbox.

Returns:

  • (String)

    The ID of the sandbox



226
227
228
# File 'lib/daytona/computer_use.rb', line 226

def sandbox_id
  @sandbox_id
end

#toolbox_apiDaytonaToolboxApiClient::ComputerUseApi (readonly)

Returns API client for sandbox operations.

Returns:

  • (DaytonaToolboxApiClient::ComputerUseApi)

    API client for sandbox operations



229
230
231
# File 'lib/daytona/computer_use.rb', line 229

def toolbox_api
  @toolbox_api
end

Instance Method Details

#take_compressed(options: nil) ⇒ DaytonaApiClient::CompressedScreenshotResponse

Takes a compressed screenshot of the entire screen.

Examples:

# Default compression
screenshot = sandbox.computer_use.screenshot.take_compressed

# High quality JPEG
jpeg = sandbox.computer_use.screenshot.take_compressed(
  options: ScreenshotOptions.new(format: "jpeg", quality: 95, show_cursor: true)
)

# Scaled down PNG
scaled = sandbox.computer_use.screenshot.take_compressed(
  options: ScreenshotOptions.new(format: "png", scale: 0.5)
)

Parameters:

  • options (ScreenshotOptions, nil) (defaults to: nil)

    Compression and display options

Returns:

  • (DaytonaApiClient::CompressedScreenshotResponse)

    Compressed screenshot data

Raises:



294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/daytona/computer_use.rb', line 294

def take_compressed(options: nil)
  options ||= ScreenshotOptions.new
  toolbox_api.take_compressed_screenshot(
    sandbox_id,
    scale: options.scale,
    quality: options.quality,
    format: options.fmt,
    show_cursor: options.show_cursor
  )
rescue StandardError => e
  raise Sdk::Error, "Failed to take compressed screenshot: #{e.message}"
end

#take_compressed_region(region:, options: nil) ⇒ DaytonaApiClient::CompressedScreenshotResponse

Takes a compressed screenshot of a specific region.

Examples:

region = ScreenshotRegion.new(x: 0, y: 0, width: 800, height: 600)
screenshot = sandbox.computer_use.screenshot.take_compressed_region(
  region,
  options: ScreenshotOptions.new(format: "webp", quality: 80, show_cursor: true)
)
puts "Compressed size: #{screenshot.size_bytes} bytes"

Parameters:

Returns:

  • (DaytonaApiClient::CompressedScreenshotResponse)

    Compressed screenshot data

Raises:



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/daytona/computer_use.rb', line 321

def take_compressed_region(region:, options: nil)
  options ||= ScreenshotOptions.new
  toolbox_api.take_compressed_region_screenshot(
    sandbox_id,
    region.height,
    region.width,
    region.y,
    region.x,
    scale: options.scale,
    quality: options.quality,
    format: options.fmt,
    show_cursor: options.show_cursor
  )
rescue StandardError => e
  raise Sdk::Error, "Failed to take compressed region screenshot: #{e.message}"
end

#take_full_screen(show_cursor: false) ⇒ DaytonaApiClient::ScreenshotResponse

Takes a screenshot of the entire screen.

Examples:

screenshot = sandbox.computer_use.screenshot.take_full_screen
puts "Screenshot size: #{screenshot.width}x#{screenshot.height}"

# With cursor visible
with_cursor = sandbox.computer_use.screenshot.take_full_screen(show_cursor: true)

Parameters:

  • show_cursor (Boolean) (defaults to: false)

    Whether to show the cursor in the screenshot. Defaults to false

Returns:

  • (DaytonaApiClient::ScreenshotResponse)

    Screenshot data with base64 encoded image

Raises:



252
253
254
255
256
# File 'lib/daytona/computer_use.rb', line 252

def take_full_screen(show_cursor: false)
  toolbox_api.take_screenshot(show_cursor:)
rescue StandardError => e
  raise Sdk::Error, "Failed to take screenshot: #{e.message}"
end

#take_region(region:, show_cursor: false) ⇒ DaytonaApiClient::RegionScreenshotResponse

Takes a screenshot of a specific region.

Examples:

region = ScreenshotRegion.new(x: 100, y: 100, width: 300, height: 200)
screenshot = sandbox.computer_use.screenshot.take_region(region)
puts "Captured region: #{screenshot.region.width}x#{screenshot.region.height}"

Parameters:

  • region (ScreenshotRegion)

    The region to capture

  • show_cursor (Boolean) (defaults to: false)

    Whether to show the cursor in the screenshot. Defaults to false

Returns:

  • (DaytonaApiClient::RegionScreenshotResponse)

    Screenshot data with base64 encoded image

Raises:



269
270
271
272
273
# File 'lib/daytona/computer_use.rb', line 269

def take_region(region:, show_cursor: false)
  toolbox_api.take_region_screenshot(region.height, region.width, region.y, region.x, show_cursor:)
rescue StandardError => e
  raise Sdk::Error, "Failed to take region screenshot: #{e.message}"
end