|
1 | 1 | """ |
2 | 2 | Recordings API routes for managing saved recordings. |
3 | 3 | """ |
| 4 | +import asyncio |
4 | 5 | import json |
| 6 | +import logging |
5 | 7 | import uuid |
6 | 8 | from datetime import datetime |
7 | 9 | from pathlib import Path |
8 | 10 | from typing import Optional |
9 | 11 |
|
10 | | -from fastapi import APIRouter, HTTPException |
| 12 | +from fastapi import APIRouter, HTTPException, BackgroundTasks |
11 | 13 | from pydantic import BaseModel |
12 | 14 |
|
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
13 | 17 | router = APIRouter(prefix="/api/recordings", tags=["recordings"]) |
14 | 18 |
|
15 | 19 | # Storage file path |
16 | 20 | RECORDINGS_FILE = Path("recordings.json") |
17 | 21 |
|
| 22 | +# Active recording session |
| 23 | +_active_recording = None |
| 24 | + |
18 | 25 |
|
19 | 26 | def _load_recordings() -> list: |
20 | 27 | """Load recordings from JSON file.""" |
@@ -109,28 +116,146 @@ async def delete_recording(recording_id: str): |
109 | 116 |
|
110 | 117 |
|
111 | 118 | @router.post("/{recording_id}/run") |
112 | | -async def run_recording(recording_id: str): |
| 119 | +async def run_recording(recording_id: str, background_tasks: BackgroundTasks): |
113 | 120 | """Run a saved recording.""" |
114 | 121 | recordings = _load_recordings() |
115 | 122 |
|
116 | 123 | for rec in recordings: |
117 | 124 | if rec["id"] == recording_id: |
118 | | - # In a real implementation, this would: |
119 | | - # 1. Generate a script from the recording actions |
120 | | - # 2. Execute it via the replay system |
121 | | - # For now, return success |
122 | | - return {"message": f"Recording '{rec['name']}' started", "recording": rec} |
| 125 | + # Run the recording in background |
| 126 | + background_tasks.add_task(_run_recording_task, rec) |
| 127 | + return {"message": f"Recording '{rec['name']}' started", "recording_id": recording_id} |
123 | 128 |
|
124 | 129 | raise HTTPException(status_code=404, detail="Recording not found") |
125 | 130 |
|
126 | 131 |
|
| 132 | +async def _run_recording_task(recording: dict): |
| 133 | + """Background task to run a recording.""" |
| 134 | + try: |
| 135 | + from llm_web_agent.recorder.script_generator import ScriptGenerator |
| 136 | + from llm_web_agent.recorder.recorder import RecordedAction, RecordingSession, ActionType |
| 137 | + |
| 138 | + # Convert stored actions to RecordedAction objects |
| 139 | + actions = [] |
| 140 | + for action_data in recording.get("actions", []): |
| 141 | + action = RecordedAction( |
| 142 | + action_type=ActionType(action_data.get("action_type", "navigate")), |
| 143 | + timestamp_ms=action_data.get("timestamp_ms", 0), |
| 144 | + selector=action_data.get("selector"), |
| 145 | + value=action_data.get("value"), |
| 146 | + url=action_data.get("url"), |
| 147 | + key=action_data.get("key"), |
| 148 | + x=action_data.get("x"), |
| 149 | + y=action_data.get("y"), |
| 150 | + element_info=action_data.get("element_info", {}), |
| 151 | + selectors=action_data.get("selectors", []), |
| 152 | + ) |
| 153 | + actions.append(action) |
| 154 | + |
| 155 | + # Create session and generate script |
| 156 | + session = RecordingSession( |
| 157 | + name=recording["name"], |
| 158 | + start_url=recording.get("start_url", ""), |
| 159 | + started_at=datetime.now(), |
| 160 | + actions=actions, |
| 161 | + ) |
| 162 | + |
| 163 | + generator = ScriptGenerator(timing=True, comments=True) |
| 164 | + script = generator.generate(session) |
| 165 | + |
| 166 | + # Execute the script |
| 167 | + exec_globals = {} |
| 168 | + exec(script, exec_globals) |
| 169 | + await exec_globals.get("main", lambda: None)() |
| 170 | + |
| 171 | + logger.info(f"Recording '{recording['name']}' completed successfully") |
| 172 | + except Exception as e: |
| 173 | + logger.error(f"Failed to run recording: {e}") |
| 174 | + |
| 175 | + |
127 | 176 | @router.post("/start") |
128 | | -async def start_recording(data: NewRecordingRequest): |
| 177 | +async def start_recording(data: NewRecordingRequest, background_tasks: BackgroundTasks): |
129 | 178 | """Start a new browser recording session.""" |
130 | | - # This would integrate with the BrowserRecorder |
131 | | - # For now, return a placeholder response |
| 179 | + global _active_recording |
| 180 | + |
| 181 | + recording_id = str(uuid.uuid4()) |
| 182 | + _active_recording = { |
| 183 | + "id": recording_id, |
| 184 | + "name": data.name, |
| 185 | + "url": data.url, |
| 186 | + "status": "starting" |
| 187 | + } |
| 188 | + |
| 189 | + # Start recording in background |
| 190 | + background_tasks.add_task(_start_recording_task, recording_id, data.name, data.url) |
| 191 | + |
132 | 192 | return { |
133 | | - "message": f"Recording '{data.name}' session started", |
| 193 | + "message": f"Recording '{data.name}' started - browser opening...", |
| 194 | + "recording_id": recording_id, |
134 | 195 | "url": data.url, |
135 | 196 | "status": "recording" |
136 | 197 | } |
| 198 | + |
| 199 | + |
| 200 | +async def _start_recording_task(recording_id: str, name: str, url: str): |
| 201 | + """Background task to run the browser recorder.""" |
| 202 | + global _active_recording |
| 203 | + |
| 204 | + try: |
| 205 | + from llm_web_agent.recorder.recorder import BrowserRecorder |
| 206 | + |
| 207 | + logger.info(f"Starting recording: {name} at {url}") |
| 208 | + |
| 209 | + recorder = BrowserRecorder( |
| 210 | + output_path=None, # We'll save to our recordings.json |
| 211 | + headless=False, |
| 212 | + session_name=name, |
| 213 | + ) |
| 214 | + |
| 215 | + session = await recorder.start(url) |
| 216 | + |
| 217 | + if session: |
| 218 | + # Convert session to storable format |
| 219 | + actions = [] |
| 220 | + for action in session.actions: |
| 221 | + actions.append({ |
| 222 | + "action_type": action.action_type.value, |
| 223 | + "timestamp_ms": action.timestamp_ms, |
| 224 | + "selector": action.selector, |
| 225 | + "value": action.value, |
| 226 | + "url": action.url, |
| 227 | + "key": action.key, |
| 228 | + "x": action.x, |
| 229 | + "y": action.y, |
| 230 | + "element_info": action.element_info, |
| 231 | + "selectors": action.selectors, |
| 232 | + }) |
| 233 | + |
| 234 | + # Save to recordings |
| 235 | + recordings = _load_recordings() |
| 236 | + recordings.append({ |
| 237 | + "id": recording_id, |
| 238 | + "name": name, |
| 239 | + "start_url": url, |
| 240 | + "actions": actions, |
| 241 | + "created_at": datetime.now().isoformat(), |
| 242 | + "updated_at": datetime.now().isoformat(), |
| 243 | + }) |
| 244 | + _save_recordings(recordings) |
| 245 | + |
| 246 | + logger.info(f"Recording '{name}' saved with {len(actions)} actions") |
| 247 | + |
| 248 | + _active_recording = None |
| 249 | + |
| 250 | + except Exception as e: |
| 251 | + logger.error(f"Recording failed: {e}") |
| 252 | + _active_recording = None |
| 253 | + |
| 254 | + |
| 255 | +@router.get("/status") |
| 256 | +async def get_recording_status(): |
| 257 | + """Get the status of any active recording.""" |
| 258 | + if _active_recording: |
| 259 | + return {"active": True, "recording": _active_recording} |
| 260 | + return {"active": False} |
| 261 | + |
0 commit comments