fix: Resize file in screen toolkit (#81)

Co-authored-by: Bradley Axen <baxen@squareup.com>
This commit is contained in:
Zaki Ali
2024-09-23 22:01:53 -07:00
committed by GitHub
parent cb6a3d7fe9
commit b9bb48eeaa

View File

@@ -1,6 +1,9 @@
import subprocess
import uuid
from rich.markdown import Markdown
from rich.panel import Panel
from goose.toolkit.base import Toolkit, tool
@@ -8,17 +11,30 @@ 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) -> 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.
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.
Return:
(str) a path to the screenshot file, in the format of image: followed by the path to the file.
Args:
display (int): Display to take the screen shot in. Default is the main display (1). Must be a value greater than 1.
""" # noqa: E501
# Generate a random tmp filename for screenshot
filename = f"/tmp/goose_screenshot_{uuid.uuid4().hex}.png"
filename = f"/tmp/goose_screenshot_{uuid.uuid4().hex}.jpg"
screen_capture_command = ["screencapture", "-x", "-D", str(display), filename, "-f", "jpg"]
subprocess.run(["screencapture", "-x", filename])
subprocess.run(screen_capture_command, check=True, capture_output=True)
resize_command = ["sips", "--resampleWidth", "768", filename, "-s", "format", "jpeg"]
subprocess.run(resize_command, check=True, capture_output=True)
self.notifier.log(
Panel.fit(
Markdown(f"```bash\n{' '.join(screen_capture_command)}"),
title="screen",
)
)
return f"image:{filename}"