|
4 | 4 |
|
5 | 5 | # @Project : browser-use-webui
|
6 | 6 | # @FileName: utils.py
|
7 |
| - |
8 | 7 | import base64
|
9 | 8 | import os
|
| 9 | +import time |
| 10 | +from pathlib import Path |
| 11 | +from typing import Dict, Optional |
10 | 12 |
|
11 | 13 | from langchain_anthropic import ChatAnthropic
|
12 | 14 | from langchain_google_genai import ChatGoogleGenerativeAI
|
@@ -140,3 +142,60 @@ def encode_image(img_path):
|
140 | 142 | with open(img_path, "rb") as fin:
|
141 | 143 | image_data = base64.b64encode(fin.read()).decode("utf-8")
|
142 | 144 | return image_data
|
| 145 | + |
| 146 | + |
| 147 | +def get_latest_files(directory: str, file_types: list = ['.webm', '.zip']) -> Dict[str, Optional[str]]: |
| 148 | + """Get the latest recording and trace files""" |
| 149 | + latest_files: Dict[str, Optional[str]] = {ext: None for ext in file_types} |
| 150 | + |
| 151 | + if not os.path.exists(directory): |
| 152 | + os.makedirs(directory, exist_ok=True) |
| 153 | + return latest_files |
| 154 | + |
| 155 | + for file_type in file_types: |
| 156 | + try: |
| 157 | + matches = list(Path(directory).rglob(f"*{file_type}")) |
| 158 | + if matches: |
| 159 | + latest = max(matches, key=lambda p: p.stat().st_mtime) |
| 160 | + # Only return files that are complete (not being written) |
| 161 | + if time.time() - latest.stat().st_mtime > 1.0: |
| 162 | + latest_files[file_type] = str(latest) |
| 163 | + except Exception as e: |
| 164 | + print(f"Error getting latest {file_type} file: {e}") |
| 165 | + |
| 166 | + return latest_files |
| 167 | +async def capture_screenshot(browser_context) -> str: |
| 168 | + """Capture and encode a screenshot""" |
| 169 | + try: |
| 170 | + # Extract the Playwright browser instance |
| 171 | + playwright_browser = browser_context.browser.playwright_browser # Ensure this is correct. |
| 172 | + |
| 173 | + # Check if the browser instance is valid and if an existing context can be reused |
| 174 | + if playwright_browser and playwright_browser.contexts: |
| 175 | + playwright_context = playwright_browser.contexts[0] |
| 176 | + |
| 177 | + # Access pages in the context |
| 178 | + if playwright_context: |
| 179 | + pages = playwright_context.pages |
| 180 | + |
| 181 | + # Use an existing page or create a new one if none exist |
| 182 | + if pages: |
| 183 | + active_page = pages[0] |
| 184 | + for page in pages: |
| 185 | + if page.url != "about:blank": |
| 186 | + active_page = page |
| 187 | + |
| 188 | + # Take screenshot |
| 189 | + try: |
| 190 | + screenshot = await active_page.screenshot( |
| 191 | + type='jpeg', |
| 192 | + quality=75, |
| 193 | + scale="css" |
| 194 | + ) |
| 195 | + encoded = base64.b64encode(screenshot).decode('utf-8') |
| 196 | + return f'<img src="data:image/jpeg;base64,{encoded}" style="width:80vw; height:90vh ; border:1px solid #ccc;">' |
| 197 | + except Exception as e: |
| 198 | + return f"<h1 class='error' style='width:80vw; height:90vh'>Waiting for browser session...</h1>" |
| 199 | + |
| 200 | + except Exception as e: |
| 201 | + return f"<h1 class='error' style='width:80vw; height:90vh'>Waiting for browser session...</h1>" |
0 commit comments