55import logging
66import os
77import shlex
8- from pathlib import Path
8+ from contextlib import suppress
99from tempfile import gettempdir
1010from typing import Literal
1111from uuid import uuid4
1212
13+ from anyio import Path
14+
1315from hud .tools .types import ContentResult
1416from hud .tools .utils import run
1517
@@ -141,9 +143,14 @@ async def execute(self, command: str, take_screenshot: bool = True) -> ContentRe
141143 # Execute command
142144 returncode , stdout , stderr = await run (full_command )
143145
146+ error = None
147+ if returncode != 0 :
148+ error = stderr or f"Command failed with exit code { returncode } "
149+
144150 # Prepare result
145151 result = ContentResult (
146- output = stdout if stdout else None , error = stderr if stderr or returncode != 0 else None
152+ output = stdout if stdout else None ,
153+ error = error ,
147154 )
148155
149156 # Take screenshot if requested
@@ -167,7 +174,7 @@ async def screenshot(self) -> str | None:
167174 # Real screenshot using scrot
168175 if OUTPUT_DIR :
169176 output_dir = Path (OUTPUT_DIR )
170- output_dir .mkdir (parents = True , exist_ok = True )
177+ await output_dir .mkdir (parents = True , exist_ok = True )
171178 screenshot_path = output_dir / f"screenshot_{ uuid4 ().hex } .png"
172179 else :
173180 # Generate a unique path in system temp dir without opening a file
@@ -177,12 +184,13 @@ async def screenshot(self) -> str | None:
177184
178185 returncode , _ , _stderr = await run (screenshot_cmd )
179186
180- if returncode == 0 and screenshot_path .exists ():
187+ if returncode == 0 and await screenshot_path .exists ():
181188 try :
182- image_data = screenshot_path .read_bytes ()
189+ image_data = await screenshot_path .read_bytes ()
183190 # Remove the file unless user requested persistence via env var
184191 if not OUTPUT_DIR :
185- screenshot_path .unlink (missing_ok = True )
192+ with suppress (FileNotFoundError ):
193+ await screenshot_path .unlink ()
186194 return base64 .b64encode (image_data ).decode ()
187195 except Exception :
188196 return None
0 commit comments