Spaces:
Running
Running
Fix portal persistence - restore state on page refresh
Browse filesChanges:
- Added portal.is_running() method to check if browser is still connected
- Added GET /qaz/copilot/portal/status endpoint
- Updated HTML to check portal status on page load
- If portal is already running when page loads, it automatically restores the UI
- Portal stays running until you click 'Close' or restart the server
The portal now persists across page refreshes - you can start it, solve CAPTCHA,
refresh the page, and it will still be there!
- admin_router.py +21 -0
- copilot_portal.py +13 -0
- static/qaz.html +25 -0
admin_router.py
CHANGED
|
@@ -368,6 +368,27 @@ async def close_copilot_portal():
|
|
| 368 |
raise HTTPException(status_code=500, detail=str(e))
|
| 369 |
|
| 370 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
class PortalClick(BaseModel):
|
| 372 |
x: float
|
| 373 |
y: float
|
|
|
|
| 368 |
raise HTTPException(status_code=500, detail=str(e))
|
| 369 |
|
| 370 |
|
| 371 |
+
@router.get("/copilot/portal/status")
|
| 372 |
+
async def get_portal_status():
|
| 373 |
+
"""Check if the portal is currently running."""
|
| 374 |
+
try:
|
| 375 |
+
from copilot_portal import get_portal
|
| 376 |
+
|
| 377 |
+
portal = get_portal()
|
| 378 |
+
is_running = portal.is_running()
|
| 379 |
+
|
| 380 |
+
return {
|
| 381 |
+
"is_running": is_running,
|
| 382 |
+
"is_initialized": portal.is_initialized
|
| 383 |
+
}
|
| 384 |
+
except Exception as e:
|
| 385 |
+
return {
|
| 386 |
+
"is_running": False,
|
| 387 |
+
"is_initialized": False,
|
| 388 |
+
"error": str(e)
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
|
| 392 |
class PortalClick(BaseModel):
|
| 393 |
x: float
|
| 394 |
y: float
|
copilot_portal.py
CHANGED
|
@@ -22,6 +22,19 @@ class CopilotPortal:
|
|
| 22 |
self.is_initialized = False
|
| 23 |
self.last_screenshot = "/tmp/copilot_portal.png"
|
| 24 |
self.message_queue = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
async def initialize(self):
|
| 27 |
"""Initialize the browser and navigate to Copilot."""
|
|
|
|
| 22 |
self.is_initialized = False
|
| 23 |
self.last_screenshot = "/tmp/copilot_portal.png"
|
| 24 |
self.message_queue = []
|
| 25 |
+
self.last_activity = None
|
| 26 |
+
|
| 27 |
+
def is_running(self) -> bool:
|
| 28 |
+
"""Check if the portal is currently running."""
|
| 29 |
+
if not self.is_initialized:
|
| 30 |
+
return False
|
| 31 |
+
if not self.browser:
|
| 32 |
+
return False
|
| 33 |
+
try:
|
| 34 |
+
# Check if browser is still connected
|
| 35 |
+
return self.browser.is_connected()
|
| 36 |
+
except:
|
| 37 |
+
return False
|
| 38 |
|
| 39 |
async def initialize(self):
|
| 40 |
"""Initialize the browser and navigate to Copilot."""
|
static/qaz.html
CHANGED
|
@@ -525,6 +525,31 @@
|
|
| 525 |
// Copilot Interactive Portal
|
| 526 |
let portalRefreshInterval = null;
|
| 527 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
async function startCopilotPortal() {
|
| 529 |
const btn = document.getElementById('portal-start-btn');
|
| 530 |
btn.disabled = true;
|
|
|
|
| 525 |
// Copilot Interactive Portal
|
| 526 |
let portalRefreshInterval = null;
|
| 527 |
|
| 528 |
+
// Check portal status on page load
|
| 529 |
+
async function checkPortalStatus() {
|
| 530 |
+
try {
|
| 531 |
+
const res = await fetch('/qaz/copilot/portal/status');
|
| 532 |
+
const data = await res.json();
|
| 533 |
+
|
| 534 |
+
if (data.is_running) {
|
| 535 |
+
// Portal is already running, restore the UI
|
| 536 |
+
document.getElementById('portal-controls').style.display = 'block';
|
| 537 |
+
document.getElementById('portal-status').textContent = '✅ Portal Active (Running)';
|
| 538 |
+
document.getElementById('portal-status').style.color = 'var(--success)';
|
| 539 |
+
document.getElementById('portal-start-btn').style.display = 'none';
|
| 540 |
+
|
| 541 |
+
// Start auto-refresh
|
| 542 |
+
refreshPortalScreenshot();
|
| 543 |
+
portalRefreshInterval = setInterval(refreshPortalScreenshot, 3000);
|
| 544 |
+
}
|
| 545 |
+
} catch (e) {
|
| 546 |
+
console.log('Portal not running or error:', e);
|
| 547 |
+
}
|
| 548 |
+
}
|
| 549 |
+
|
| 550 |
+
// Check status when page loads
|
| 551 |
+
checkPortalStatus();
|
| 552 |
+
|
| 553 |
async function startCopilotPortal() {
|
| 554 |
const btn = document.getElementById('portal-start-btn');
|
| 555 |
btn.disabled = true;
|