Skip to content

Commit

Permalink
Resize file in screen toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
zakiali committed Sep 21, 2024
1 parent 71863a1 commit 1804f72
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/goose/toolkit/screen.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,54 @@
import os
import re
import subprocess
import uuid

from rich.markdown import Markdown
from rich.panel import Panel

from goose.toolkit.base import Toolkit, tool


class Screen(Toolkit):
"""Provides an instructions on when and how to work with screenshots"""

@tool
def take_screenshot(self) -> str:
def take_screenshot(self, display: int = 1, resize: bool = False, target_size: int = 4) -> str:
"""
Take a screenshot to assist the user in debugging or designing an app. They may need help with moving a screen element, or interacting in some way where you could do with seeing the screen.
Optionally, resize the image using the `sips` cli tool. You may do this fs there is potential for error in the payload size.
Args:
display (int): Display to take the screen shot in. Default is the main display (1). Must be a value greater than 1.
resize (bool): Boolean parameter to resize the image or not. No resizing by default.
target_size (int): Target file size in MB. Default is 4.
Return:
(str) a path to the screenshot file, in the format of image: followed by the path to the file.
""" # noqa: E501
# Generate a random tmp filename for screenshot
filename = f"/tmp/goose_screenshot_{uuid.uuid4().hex}.png"

subprocess.run(["screencapture", "-x", filename])
screen_capture_command = ["screencapture", "-x", "-D", str(display), filename]
resize_command_str = ""

subprocess.run(screen_capture_command)

if resize:
# get current disk size and reduce pixels by fractional amount to target (not linear but approx)
size = os.path.getsize(filename) / (1024**2)
reduce_by = max(0, (size - target_size) / size)
output = subprocess.run(["sips", "-g", "pixelWidth", filename], stdout=subprocess.PIPE).stdout.decode()
current_pixel_width = int(re.search(r"pixelWidth:\s*(\d+)", output).group(1))

resize_command = ["sips", "--resampleWidth", str(int(current_pixel_width * reduce_by)), filename]
subprocess.run(resize_command)
resize_command_str = " ".join(resize_command)

self.notifier.log(
Panel.fit(
Markdown(f"```bash\n{' '.join(screen_capture_command)}\n{resize_command_str if resize else ''}"),
title="screen",
)
)

return f"image:{filename}"

Expand Down

0 comments on commit 1804f72

Please sign in to comment.