File size: 2,904 Bytes
be3cb04
9fa39cc
 
 
 
be3cb04
 
 
583face
be3cb04
 
 
 
 
 
 
 
 
 
 
583face
be3cb04
9fa39cc
be3cb04
 
 
 
 
 
 
 
 
583face
 
be3cb04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
583face
 
 
9fa39cc
 
 
583face
 
 
be3cb04
 
 
583face
 
9fa39cc
be3cb04
 
 
 
 
583face
be3cb04
 
 
 
 
583face
be3cb04
 
 
 
 
 
 
 
 
 
9fa39cc
be3cb04
 
9fa39cc
be3cb04
 
 
 
583face
 
 
 
 
9fa39cc
be3cb04
 
 
 
9fa39cc
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
Stealth Browser Stack - FastAPI on Port 8888
=============================================
This handles healthchecks and API tasks.
noVNC moved to 6080 to avoid conflict.
"""

import os
import asyncio
import logging
from datetime import datetime
from typing import Dict, Any, Optional

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

from agent import BrowserSession, validate_order

app = FastAPI(title="Stealth Browser Worker", version="2.5.0")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

browser_session: Optional[BrowserSession] = None


class TaskRequest(BaseModel):
    task_id: str
    task_type: str
    data: Dict[str, Any]


class TaskResponse(BaseModel):
    task_id: str
    status: str
    result: Optional[Dict[str, Any]] = None
    error: Optional[str] = None
    logs: Optional[list] = None
    execution_time_ms: int = 0


@app.get("/")
async def root():
    return {
        "service": "AI System Bridge",
        "status": "active",
        "timestamp": datetime.now().isoformat()
    }


@app.get("/health")
async def health():
    return {
        "status": "healthy",
        "display": os.environ.get("DISPLAY", ":99"),
        "browser": browser_session is not None and browser_session.is_active
    }


@app.post("/run-task", response_model=TaskResponse)
async def run_task(request: TaskRequest):
    global browser_session
    start_time = datetime.now()
    
    try:
        if request.task_type in ["validate_order", "validate_email"]:
            order_data = {**request.data, "task_id": request.task_id}
            result = await validate_order(order_data, browser_session)
            execution_time = int((datetime.now() - start_time).total_seconds() * 1000)
            
            return TaskResponse(
                task_id=request.task_id,
                status="success",
                result=result,
                logs=result.get("logs", []),
                execution_time_ms=execution_time
            )
        else:
            raise HTTPException(status_code=400, detail="Invalid task_type")
            
    except Exception as e:
        return TaskResponse(task_id=request.task_id, status="error", error=str(e))


@app.on_event("startup")
async def startup():
    global browser_session
    try:
        browser_session = await BrowserSession.create()
        logger.info("✅ Persistent browser session created!")
    except Exception as e:
        logger.error(f"⚠️ Browser fail: {e}")


if __name__ == "__main__":
    import uvicorn
    # Port 8888 for HF healthcheck
    uvicorn.run(app, host="0.0.0.0", port=8888)