Skip to content

fix: Resize file in screen toolkit #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/goose/toolkit/screen.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
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) -> 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(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)

subprocess.run(["screencapture", "-x", filename])
self.notifier.log(
Panel.fit(
Markdown(f"```bash\n{' '.join(screen_capture_command)}"),
title="screen",
)
)

return f"image:{filename}"

Expand Down