Spaces:
Running
Running
File size: 49,304 Bytes
28a08e7 | 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 | """
browser.py β Playwright browser automation.
S65 β /screenshot + /navigate stateless (mantenuti per retrocompat)
S_NEW β /open + /act + /close con sessioni persistenti + DOM Intelligence
S174 β GET /screenshot/{session_id} snapshot sessione attiva senza azioni
W-NAV aggiornamenti:
- MAX_TEXT 3000β6000: piΓΉ contesto per l'LLM senza rischiare OOM
- _goto_with_networkidle(): networkidle per SPA/React/Vue, fallback domcontentloaded
- _dismiss_cookie_banner(): auto-dismiss CSS+JS prima dell'estrazione DOM (W-NAV2)
- _extract_text_trafilatura(): estrazione mainbody Readability-quality (W-NAV)
- /navigate: usa trafilatura per text_content (da 2000β5000 chars utili)
- /open: aggiunto text_content via trafilatura nella risposta
Vincoli HF free tier:
- Max SESSION_LIMIT sessioni vive (OOM guard: Chromium ~300 MB/sessione)
- Timeout sessione: SESSION_TTL_S secondi di inattivitΓ
- Un solo _browser_lock per aprire nuove sessioni (evita race condition)
Problematiche W-NAV anticipate:
- networkidle timeout: pagine con polling infinito (ads/analytics/WebSocket)
non raggiungono mai networkidle β timeout catturato, flusso continua
(domcontentloaded Γ¨ giΓ avvenuto come prerequisito β DOM accessibile)
- Cookie banner loop: _dismiss_cookie_banner() Γ¨ idempotente e silenziosa β
se fallisce il flusso continua normalmente (banner nella DOM, LLM lo vede
e puΓ² istruire browser_act per gestirlo manualmente)
- trafilatura su pagine non-article (login, dashboard, SPA vuota): restituisce
None β fallback a DOM innerText evaluation (pre-esistente, sempre funziona)
- get_by_text Playwright API: usiamo page.evaluate() JS per text-matching invece
di Locator API (piΓΉ stabile tra versioni playwright, zero versioning issues)
"""
import os
import asyncio, base64, hashlib, os, time, uuid, logging
from typing import Optional, Any
from fastapi import APIRouter, Depends, HTTPException, Request
from pydantic import BaseModel
from .auth_guard import require_role, AuthRole
router = APIRouter(prefix="/api/browser", tags=["browser"])
_logger = logging.getLogger("browser")
# βββ Costanti βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
SESSION_LIMIT = 2 # max sessioni vive (OOM guard HF free)
SESSION_TTL_S = 300 # QF-4: 2β5 min inattivitΓ β supporta navigazione multi-step piΓΉ lunga
GOTO_TIMEOUT = 25_000 # QF-4: 15β25s goto β SPA pesanti (React/Next.js) servono piΓΉ tempo
ACTION_TIMEOUT = 5_000 # ms per singola azione
MAX_LINKS = 25
MAX_INPUTS = 20
MAX_TEXT = 6000 # W-NAV: alzato da 3000β6000 (piΓΉ contesto per LLM)
# βββ Lock + registry sessioni βββββββββββββββββββββββββββββββββββββββββββββββββ
_browser_lock = asyncio.Lock()
_sessions: dict[str, dict[str, Any]] = {}
# βββ Launch args ottimizzati HF Spaces free βββββββββββββββββββββββββββββββββββ
_LAUNCH_ARGS = [
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
"--disable-gpu",
"--single-process",
"--no-zygote",
"--disable-extensions",
"--disable-background-networking",
"--disable-default-apps",
"--mute-audio",
# S274-SEC5: rimosso --disable-web-security β disabilita Same-Origin Policy β SSRF via siti visitati
# "--disable-web-security", # RIMOSSO per sicurezza
# ARCH-3: ulteriori flag riduzione memoria (HF free tier ~1GB RAM)
"--disable-accelerated-2d-canvas", # disabilita canvas GPU (non usato in headless)
"--disable-renderer-backgrounding", # previene throttling renderer in background
"--renderer-process-limit=1", # max 1 renderer process (headless, no tabs visibili)
"--js-flags=--max-old-space-size=200",# limita heap V8 a 200MB per renderer
]
_UA_MOBILE = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15"
_UA_DESKTOP = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/124.0.0.0"
_BLOCKED = [
"localhost", "127.0.0.1", "0.0.0.0",
"192.168.", "10.0.", "172.16.", "172.17.", "172.18.",
"metadata.google", "169.254",
]
# βββ W-NAV2: Cookie dismiss β selettori CSS prioritizzati ββββββββββββββββββββ
# Ordine: vendor-specifici (piΓΉ precisi) β pattern generici (piΓΉ ampi)
# Problematica: selettori troppo generici (es. "button") catturano azioni non-cookie
# β usiamo pattern specifici per namespace (id/class con "cookie/consent/gdpr")
_COOKIE_DISMISS_SELECTORS = [
"#onetrust-accept-btn-handler", # OneTrust (ubiquo)
"#CybotCookiebotDialogBodyButtonAccept", # Cookiebot
"#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll",
"[data-cookiebanner='accept_button']",
".cc-btn.cc-allow", # CookieConsent.js
".cc-accept-all",
"#cookie_action_close_header",
"#accept-cookies",
"#acceptAllCookies",
"#acceptCookies",
".cookie-accept-all",
".cookie__btn--accept",
"#gdpr-cookie-accept",
".gdpr-accept-all",
".gdpr__btn",
"[aria-label='Accept all cookies']",
"[aria-label='Accetta tutti i cookie']",
"[aria-label='Consenti tutti i cookie']",
"[aria-label='Allow all cookies']",
"button[id*='cookie'][id*='accept']",
"button[id*='accept'][id*='cookie']",
"button[class*='cookie'][class*='accept']",
".cookie-consent__accept",
".cookie-notice__accept",
".cookie-banner__accept",
"#cookie-accept",
".js-accept-cookies",
]
# JS fallback: testo-matching su pulsanti visibili
# Problematica: .innerText puΓ² essere "" su elementi visibili ma con solo icone
# β usiamo .textContent come fallback per innerText
# Problematica: false positive su "OK" generico (es. dialog di conferma)
# β "ok" solo se il genitore contiene "cookie/consent/gdpr" nel className/id
_COOKIE_DISMISS_JS = """() => {
const EXACT = [
'accetta tutto', 'accetta tutti', 'accetta tutti i cookie',
'accept all', 'accept all cookies', 'allow all', 'allow all cookies',
'tout accepter', 'alle akzeptieren', 'aceitar tudo', 'aceptar todo',
'i accept all', 'i agree to all',
];
const PARTIAL = [
'accetta', 'accept cookies', 'allow cookies',
'consenti tutto', 'ho capito', 'i accept', 'i agree',
];
const isCookieCtx = (el) => {
const ctx = (el.id + ' ' + el.className + ' ' +
(el.closest('[class*=cookie],[class*=consent],[class*=gdpr],[id*=cookie],[id*=consent],[id*=gdpr]')?.className || '')
).toLowerCase();
return ctx.includes('cookie') || ctx.includes('consent') || ctx.includes('gdpr') || ctx.includes('privacy');
};
const btns = Array.from(document.querySelectorAll(
'button,a,[role=button],[class*=cookie] *,[class*=consent] *,[id*=cookie] *,[id*=gdpr] *'
));
for (const el of btns) {
const txt = (el.innerText || el.textContent || '').trim().toLowerCase().replace(/\\s+/g, ' ');
if (!txt || txt.length > 60) continue;
const s = window.getComputedStyle(el);
if (s.display === 'none' || s.visibility === 'hidden' || s.opacity === '0') continue;
if (EXACT.includes(txt) || (PARTIAL.some(p => txt.includes(p)) && isCookieCtx(el))) {
el.click();
return txt.slice(0, 40);
}
}
return null;
}"""
# βββ DOM Intelligence script βββββββββββββββββββββββββββββββββββββββββββββββββ
_DOM_SCRIPT = """() => {
const links = [...document.querySelectorAll('a[href]')]
.filter(a => a.href.startsWith('http') && a.innerText.trim())
.slice(0, %d)
.map(a => ({
text: a.innerText.trim().replace(/\\s+/g, ' ').slice(0, 80),
href: a.href,
selector: a.id ? '#' + a.id : (a.getAttribute('aria-label')
? '[aria-label="' + a.getAttribute('aria-label') + '"]'
: (a.className ? '.' + a.className.split(' ').filter(c=>c&&!c.match(/^[a-z]{1,2}$/)).slice(0,2).join('.') : 'a'))
}));
const inputs = [...document.querySelectorAll(
'input:not([type=hidden]),textarea,select,button,[role=button],[role=checkbox],[role=radio],[role=switch],[role=combobox]'
)]
.filter(el => {
const s = window.getComputedStyle(el);
return s.display !== 'none' && s.visibility !== 'hidden' && s.opacity !== '0';
})
.slice(0, %d)
.map(el => {
let selector = null;
const role = el.getAttribute('role') || el.tagName.toLowerCase();
if (el.id) selector = '#' + el.id;
else if (el.getAttribute('name')) selector = '[name="' + el.getAttribute('name') + '"]';
else if (el.getAttribute('aria-label')) selector = '[aria-label="' + el.getAttribute('aria-label') + '"]';
else if (el.placeholder) selector = '[placeholder="' + el.placeholder + '"]';
else if (el.getAttribute('data-testid')) selector = '[data-testid="' + el.getAttribute('data-testid') + '"]';
const tag = el.tagName.toLowerCase();
const isChecked = el.checked !== undefined ? el.checked : null;
const currentVal = (tag === 'input' || tag === 'textarea') ? (el.value || '') : null;
const isDisabled = el.disabled || el.getAttribute('aria-disabled') === 'true';
let label = el.getAttribute('aria-label') || el.placeholder || el.getAttribute('name') || el.id;
if (!label) {
const lbl = el.id ? document.querySelector('label[for="'+el.id+'"]') : el.closest('label');
if (lbl) label = lbl.innerText.trim().slice(0, 40);
}
if (!label) label = el.innerText?.trim().slice(0, 40) || null;
return { tag, role, type: el.type || null, label, selector,
value: currentVal, checked: isChecked, disabled: isDisabled || false };
})
.filter(el => el.label || el.selector);
const headings = [...document.querySelectorAll('h1,h2,h3')]
.slice(0, 8)
.map(h => ({ level: h.tagName.toLowerCase(), text: h.innerText.trim().slice(0, 80) }));
const modals = [...document.querySelectorAll(
'[role=dialog],[role=alertdialog],[role=modal],.modal,.dialog,[aria-modal=true]'
)]
.filter(el => {
const s = window.getComputedStyle(el);
return s.display !== 'none' && s.visibility !== 'hidden';
})
.slice(0, 3)
.map(el => ({
role: el.getAttribute('role') || 'modal',
title: el.querySelector('h1,h2,h3,[role=heading]')?.innerText.trim().slice(0,60) || null,
selector: el.id ? '#' + el.id : (el.getAttribute('aria-label') ? '[aria-label="'+el.getAttribute('aria-label')+'"]' : '[role="'+(el.getAttribute('role')||'dialog')+'"]'),
}));
const title = document.title;
const desc = document.querySelector('meta[name=description]')?.content?.slice(0, 200) || null;
const mainEl = document.querySelector('main,[role=main],article,.content,#content') || document.body;
const text = mainEl.innerText.replace(/\\s+/g, ' ').trim().slice(0, %d);
return { title, desc, text, links, inputs, headings, modals };
}"""
# βββ Helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async def _get_ax_tree(page: Any, max_depth: int = 5) -> Optional[dict]:
"""GAP-AX: Playwright Accessibility Tree β 'screenshot testuale' MCP-style.
Restituisce una struttura ad albero ARIA che descrive la pagina in modo
semantico: ruoli, nomi, stati, relazioni. Usato dall'LLM per interazioni
precise senza ambiguitΓ visiva (stile Manus / browser-use).
Limitazioni sicure:
- max_depth=5: alberi profondi generano payload enormi. 5 livelli coprono
il 95% delle pagine senza superare ~8KB di JSON.
- Timeout 3s: mai blocca il flusso principale (snapshot Γ¨ sincrono ma puΓ²
bloccarsi su pagine con ARIA dinamici in aggiornamento continuo).
- Ritorna None su qualsiasi errore: campo opzionale, zero impatto.
"""
try:
# interesting=True: esclude nodi ARIA nascosti (display:none, aria-hidden)
snapshot = await asyncio.wait_for(
page.accessibility.snapshot(interesting_only=True),
timeout=3.0,
)
if not snapshot:
return None
return _trim_ax_tree(snapshot, max_depth)
except Exception:
return None
def _trim_ax_tree(node: dict, depth: int) -> dict:
"""Riduce ricorsivamente l'albero AX a max_depth livelli.
Mantiene: role, name, description, value, checked, expanded, required.
Scarta: proprietΓ interne Playwright (nodeId, backendDOMNodeId, ignoredReasons).
"""
KEEP = frozenset({role, name, description, value, checked,
expanded, required, haspopup, level, pressed,
selected, multiselectable, orientation})
result: dict = {k: v for k, v in node.items() if k in KEEP and v not in (None, False, )}
if depth > 0 and node.get(children):
trimmed = [_trim_ax_tree(c, depth - 1) for c in node[children]]
# Filtra nodi completamente vuoti (solo role senza nome nΓ© figli)
trimmed = [c for c in trimmed if len(c) > 1 or c.get(children)]
if trimmed:
result[children] = trimmed
return result
def _safe_url(url: str) -> bool:
low = url.lower()
return url.startswith(("http://", "https://")) and not any(b in low for b in _BLOCKED)
async def _goto_with_networkidle(page: Any, url: str, timeout: int = GOTO_TIMEOUT) -> None:
"""
Naviga all'URL con wait_until='networkidle' per SPA/React/Vue/Next.js.
W-NAV3: never use only domcontentloaded for SPAs β may miss JS-rendered content.
Comportamento:
- networkidle: aspetta 500ms senza richieste HTTP attive (standard per SPA)
- Timeout: se la pagina ha polling infinito (ads, analytics, WebSocket keepalive),
il timeout scatta DOPO che domcontentloaded Γ¨ giΓ avvenuto β DOM accessibile.
Catturiamo silenziosamente e continuiamo.
- wait_for_load_state fallback: su timeout, forza attesa domcontentloaded
(giΓ avvenuto, ritorna subito β Γ¨ solo un safety net)
Nota: timeout di goto con networkidle NON significa pagina non caricata.
Significa che ci sono richieste background attive dopo il caricamento visibile.
"""
try:
await page.goto(url, wait_until="networkidle", timeout=timeout)
except Exception:
# Timeout o navigazione interrotta β il DOM Γ¨ comunque disponibile
try:
await page.wait_for_load_state("domcontentloaded", timeout=3000)
except Exception:
pass # Anche domcontentloaded fallisce? Procediamo β page.content() funziona comunque
async def _dismiss_cookie_banner(page: Any) -> bool:
"""
Auto-dismiss banner cookie prima dell'estrazione DOM.
W-NAV2: eseguita dopo ogni _goto_with_networkidle in /open, /navigate, /screenshot.
Strategia a 2 fasi:
Fase 1: CSS selectors vendor-specifici (precisi, zero false positive)
Fase 2: JS text-matching su pulsanti visibili (copre CMP custom e traduzioni)
Silenziosa: non lancia mai, timeout brevi (1.5s per selettore) per non
bloccare il flusso. Se fallisce, il banner rimane nel DOM e l'LLM lo vede
nei dom.modals β puΓ² istruire browser_act per gestirlo manualmente.
"""
# Fase 1: CSS selectors diretti
for sel in _COOKIE_DISMISS_SELECTORS:
try:
el = await page.query_selector(sel)
if el:
visible = await el.is_visible()
if visible:
await el.click(timeout=1500)
await page.wait_for_timeout(300)
_logger.debug("Cookie banner dismissed via CSS: %s", sel)
return True
except Exception:
continue
# Fase 2: JS text-matching (CMP custom, traduzioni non standard)
try:
clicked = await page.evaluate(_COOKIE_DISMISS_JS)
if clicked:
await page.wait_for_timeout(300)
_logger.debug("Cookie banner dismissed via JS text-match: '%s'", clicked)
return True
except Exception as _exc:
_logger.debug("[browser] silenced %s", type(_exc).__name__) # noqa: BLE001
return False
async def _extract_text_trafilatura(page: Any, url: str = "", max_chars: int = MAX_TEXT) -> str:
"""
Estrae il testo mainbody via trafilatura (Readability-quality).
Fallback: DOM innerText evaluation se trafilatura non disponibile o restituisce poco.
Problematiche:
- trafilatura su SPA: l'HTML di page.content() include il DOM post-JS β
trafilatura puΓ² estrarre piΓΉ testo rispetto all'HTML statico iniziale
- trafilatura su pagine non-article (login, 404): restituisce None β
fallback a DOM innerText (sempre disponibile)
- max_chars applicato sia a trafilatura che al fallback
"""
try:
import trafilatura # type: ignore[import-untyped]
html = await page.content()
extracted = trafilatura.extract(
html,
url=url or None,
include_comments=False,
include_tables=True,
include_images=False,
deduplicate=True,
favor_recall=True,
)
if extracted and len(extracted.strip()) > 200:
return extracted[:max_chars]
except Exception as _exc:
_logger.debug("[browser] silenced %s", type(_exc).__name__) # noqa: BLE001
# Fallback: DOM evaluation (pre-esistente, sempre funziona)
try:
text = await page.evaluate(
"() => (document.querySelector('main,[role=main],article,.content,#content') || document.body)"
f".innerText.replace(/\\s+/g,' ').trim().slice(0,{max_chars})"
)
return str(text)
except Exception:
return ""
async def _try_persist_screenshot(url: str, png_b64: str, title: str) -> None:
try:
sb_url = os.getenv("SUPABASE_URL", "")
sb_key = os.getenv("SUPABASE_ANON_KEY") or os.getenv("SUPABASE_KEY", "")
if not (sb_url and sb_key):
return
from supabase import create_client
sb = create_client(sb_url, sb_key)
fid = hashlib.sha256(url.encode()).hexdigest()[:32]
now = int(time.time() * 1000)
sb.table("vfs_files").upsert({
"id": fid, "path": f"/browser-screenshots/{fid}.png",
"language": "image", "content": png_b64, "conversation_id": None,
"created_at": now, "updated_at": now,
"metadata": {"source_url": url, "title": title},
}).execute()
except Exception as _e:
_logger.warning("_try_persist_screenshot: Supabase upsert failed: %s", _e)
async def _make_context(browser: Any, width: int, height: int, mobile: bool) -> Any:
return await browser.new_context(
viewport={"width": 390 if mobile else width, "height": height},
is_mobile=mobile,
user_agent=_UA_MOBILE if mobile else _UA_DESKTOP,
)
async def _execute_actions(page: Any, actions: list) -> None:
for action in actions:
sel = action.selector or ""
try:
if action.type == "click" and sel:
await page.click(sel, timeout=ACTION_TIMEOUT)
await page.wait_for_load_state("domcontentloaded", timeout=5000)
elif action.type == "click" and action.x is not None and action.y is not None:
# GAP-CLICK: fallback coordinate per elementi senza selector DOM
await page.mouse.click(action.x, action.y)
await page.wait_for_load_state("domcontentloaded", timeout=5000)
elif action.type == "fill" and sel:
await page.fill(sel, action.value or "", timeout=ACTION_TIMEOUT)
elif action.type == "select" and sel:
await page.select_option(sel, action.value or "", timeout=ACTION_TIMEOUT)
elif action.type == "press" and sel:
await page.press(sel, action.key or "Enter", timeout=ACTION_TIMEOUT)
elif action.type == "hover" and sel:
await page.hover(sel, timeout=ACTION_TIMEOUT)
elif action.type == "wait_for" and sel:
await page.wait_for_selector(sel, timeout=ACTION_TIMEOUT)
elif action.type == "wait":
await page.wait_for_timeout(min(action.ms or 500, 5000))
elif action.type == "scroll":
pct = float(action.value or "50")
await page.evaluate(f"window.scrollTo(0, document.body.scrollHeight * {pct / 100})")
except Exception as e:
_logger.debug("Action %s on '%s' failed: %s", action.type, sel, e)
# βββ ARCH-7: Browserless.io CDP fallback βββββββββββββββββββββββββββββββββββββ
# Se BROWSERLESS_TOKEN Γ¨ impostato su Railway, usa CDP remoto (zero RAM locale).
# Fallback automatico a Chromium locale se token assente o connessione fallisce.
_BROWSERLESS_WS = "wss://chrome.browserless.io"
async def _get_browser_instance():
"""
Ritorna (pw, browser, is_remote).
is_remote=True β CDP remoto: non chiamare browser.close() nΓ© pw.stop().
is_remote=False β Chromium locale: cleanup normale.
"""
from playwright.async_api import async_playwright
pw = await async_playwright().start()
token = os.getenv("BROWSERLESS_TOKEN", "").strip()
if token:
try:
ws = f"{_BROWSERLESS_WS}?token={token}"
browser = await pw.chromium.connect_over_cdp(ws, timeout=10_000)
_logger.info("Browser: Browserless.io CDP β (zero RAM locale)")
return pw, browser, True
except Exception as _e:
_logger.warning("Browser: CDP non disponibile (%s) β fallback locale", _e)
browser = await pw.chromium.launch(headless=True, args=_LAUNCH_ARGS)
return pw, browser, False
async def _close_session(sid: str, reason: str = "explicit") -> None:
sess = _sessions.pop(sid, None)
if not sess:
return
is_remote = sess.get("is_remote", False)
try:
await sess["context"].close()
if not is_remote:
await sess["browser"].close()
# pw.stop() sempre: chiude la connessione playwright (locale: termina processo; CDP: disconnette)
await sess["pw"].stop()
_logger.info("Session %s closed (%s, remote=%s)", sid, reason, is_remote)
except Exception as e:
_logger.warning("Session %s close error: %s", sid, e)
async def _session_cleanup_loop() -> None:
while True:
await asyncio.sleep(30) # ARCH-3: check piΓΉ frequente (era 60s)
now = time.time()
expired = [sid for sid, s in list(_sessions.items())
if now - s["last_used"] > SESSION_TTL_S]
for sid in expired:
await _close_session(sid, "TTL expired")
def _log_browser_bg_exc(t): # BUGFIX: log eccezioni da create_task fire-and-forget
if not t.cancelled() and t.exception():
_logger.warning("[browser] bg task raised: %s", t.exception())
def _start_cleanup() -> None:
try:
loop = asyncio.get_event_loop()
if loop.is_running():
asyncio.create_task(_session_cleanup_loop()).add_done_callback(_log_browser_bg_exc) # BUGFIX
except Exception as _e:
_logger.warning("_start_cleanup: create_task failed β cleanup loop not running: %s", _e)
# βββ Models βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class BrowserAction(BaseModel):
type: str
selector: Optional[str] = None
value: Optional[str] = None
key: Optional[str] = None
ms: Optional[int] = None
x: Optional[float] = None # GAP-CLICK: coordinata X per click-by-position
y: Optional[float] = None # GAP-CLICK: coordinata Y per click-by-position
class ScreenshotRequest(BaseModel):
url: str
width: int = 1280
height: int = 800
mobile: bool = False
wait_ms: int = 2000
class NavigateRequest(BaseModel):
url: str
actions: list[BrowserAction] = []
width: int = 1280
height: int = 800
mobile: bool = False
wait_ms: int = 2000
class BrowserOpenRequest(BaseModel):
url: str
actions: list[BrowserAction] = []
mobile: bool = False
width: int = 1280
wait_ms: int = 1500
class BrowserActRequest(BaseModel):
session_id: str
actions: list[BrowserAction]
wait_ms: int = 1000
take_screenshot: bool = True
class BrowserCloseRequest(BaseModel):
session_id: str
class DomSnapshot(BaseModel):
title: Optional[str] = None
desc: Optional[str] = None
text: Optional[str] = None
links: list[dict] = []
inputs: list[dict] = []
headings: list[dict] = []
modals: list[dict] = []
class BrowserResult(BaseModel):
ok: bool
session_id: Optional[str] = None
screenshot_b64: Optional[str] = None
title: Optional[str] = None
url: Optional[str] = None
text_content: Optional[str] = None
dom: Optional[DomSnapshot] = None
ax_tree: Optional[dict] = None # GAP-AX: Playwright Accessibility Tree (MCP-style)
error: Optional[str] = None
warnings: list[str] = []
# βββ verify_goal_browser ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Sprint 3b ITEM 8
async def verify_goal_browser(
goal: str,
url: str,
requirements: "list | None" = None,
timeout_s: float = 30.0,
) -> dict:
if not _safe_url(url):
return {"ok": False, "overall": "UNKNOWN", "per_criterion": {}, "error": "URL non consentita"}
# S701: se requirements=None, fallback a basic DOM check (era UNKNOWN immediato)
# Prima: 90%+ dei casi usciva subito senza verificare nulla.
# Ora: check DOM non-empty + zero white screen + JS error interception.
if not requirements:
try:
import playwright # noqa: F401
except ImportError:
return {"ok": True, "overall": "UNKNOWN", "per_criterion": {}, "error": "playwright non installato"}
try:
from playwright.async_api import async_playwright
_js_errs: list[str] = []
async with async_playwright() as _pw2:
_b2 = await asyncio.wait_for(
_pw2.chromium.launch(headless=True, args=_LAUNCH_ARGS), timeout=10.0)
_ctx2 = await _b2.new_context(viewport={"width": 1280, "height": 800}, user_agent=_UA_DESKTOP)
_pg2 = await _ctx2.new_page()
_pg2.on("pageerror", lambda e: _js_errs.append(str(e)))
try:
await asyncio.wait_for(
_goto_with_networkidle(_pg2, url, GOTO_TIMEOUT),
timeout=min(timeout_s, 15.0),
)
await _pg2.wait_for_timeout(600)
_body_txt = await _pg2.evaluate("document.body ? document.body.innerText.trim() : ''")
_body_html = await _pg2.evaluate("document.body ? document.body.innerHTML.trim() : ''")
_title2 = await _pg2.title()
finally:
await _ctx2.close()
await _b2.close()
_is_white = len(_body_txt) < 5 and len(_body_html) < 30
_has_js_err = bool(_js_errs)
if _is_white:
_overall2 = "FAIL"
_crit2 = {"dom_not_empty": "FAIL", "js_errors": "PASS" if not _has_js_err else "FAIL"}
elif _has_js_err:
_overall2 = "FAIL"
_crit2 = {"dom_not_empty": "PASS", "js_errors": "FAIL"}
else:
_overall2 = "PASS"
_crit2 = {"dom_not_empty": "PASS", "js_errors": "PASS"}
# S701 R5: telemetria DOM check
try:
from api.state import increment_stat as _inc_dom
_inc_dom("browser_dom_check_pass" if _overall2 == "PASS" else "browser_dom_check_fail")
except Exception as _exc:
_logger.debug("[browser] silenced %s", type(_exc).__name__) # noqa: BLE001
return {"ok": True, "overall": _overall2, "per_criterion": _crit2,
"error": None, "title": _title2, "js_errors": _js_errs[:3]}
except asyncio.TimeoutError:
return {"ok": True, "overall": "UNKNOWN", "per_criterion": {}, "error": "timeout"}
except Exception as _be:
return {"ok": True, "overall": "UNKNOWN", "per_criterion": {}, "error": str(_be)[:200]}
try:
import playwright # noqa: F401
except ImportError:
return {
"ok": True, "overall": "UNKNOWN", "per_criterion": {},
"error": "playwright non installato β browser verify disabilitato",
}
per_criterion: dict[str, str] = {}
try:
from playwright.async_api import async_playwright
async with async_playwright() as _pw:
_browser = await _pw.chromium.launch(headless=True, args=_LAUNCH_ARGS)
_ctx = await _browser.new_context(
viewport={"width": 1280, "height": 800},
user_agent=_UA_DESKTOP,
)
_page = await _ctx.new_page()
try:
# W-NAV3: usa networkidle anche per verify_goal_browser
await asyncio.wait_for(
_goto_with_networkidle(_page, url, GOTO_TIMEOUT),
timeout=min(timeout_s, GOTO_TIMEOUT / 1000),
)
await _page.wait_for_timeout(1000)
_criteria: list[str] = []
for _req in (requirements or [])[:5]:
_ac = getattr(_req, "acceptance_criteria", None) or []
_criteria.extend(str(c) for c in _ac[:2])
_criteria = _criteria[:5]
for _crit in _criteria:
_low = _crit.lower()
_verdict = "UNKNOWN"
try:
if any(k in _low for k in ["login", "autenti", "sessione", "http 200", "200 ok"]):
_el = await _page.query_selector("form, input[type=password], input[type=email]")
_verdict = "PASS" if _el else "FAIL"
elif any(k in _low for k in ["crud", "lista", "record", "endpoint", "array", "json"]):
_body = await _page.evaluate("() => document.body.innerText.slice(0, 3000)")
_verdict = "PASS" if len(str(_body)) > 100 else "FAIL"
elif any(k in _low for k in ["dashboard", "dati", "metriche", "renderizza", "mostra"]):
_el = await _page.query_selector("main, [role=main], .dashboard, #app, #root, table, chart")
_verdict = "PASS" if _el else "FAIL"
elif any(k in _low for k in ["form", "validaz", "campo", "submit", "bottone"]):
_el = await _page.query_selector("form, input, textarea, button[type=submit]")
_verdict = "PASS" if _el else "FAIL"
elif any(k in _low for k in ["errore", "error", "400", "401", "403", "fallisce"]):
_verdict = "UNKNOWN"
else:
_title = await _page.title()
_verdict = "PASS" if _title else "FAIL"
except Exception:
_verdict = "UNKNOWN"
per_criterion[_crit[:80]] = _verdict
finally:
await _ctx.close()
await _browser.close()
_pass_n = sum(1 for v in per_criterion.values() if v == "PASS")
_total = len(per_criterion)
if _total == 0:
_overall = "UNKNOWN"
elif _pass_n / _total >= 0.5:
_overall = "PASS"
else:
_overall = "FAIL"
return {"ok": True, "overall": _overall, "per_criterion": per_criterion, "error": None}
except ImportError:
return {"ok": False, "overall": "UNKNOWN", "per_criterion": {}, "error": "Playwright non installato"}
except Exception as _e:
return {"ok": False, "overall": "UNKNOWN", "per_criterion": per_criterion, "error": str(_e)[:300]} # S588
# βββ _take_screenshot (internal helper) ββββββββββββββββββββββββββββββββββββββ
async def _take_screenshot(
url: str,
mobile: bool = False,
width: int = 1280,
height: int = 800,
wait_ms: int = 1500,
) -> dict:
"""
Wrapper interno per screenshot Playwright headless. (GAP-6-fix)
Usato da gemini_vision.py senza passare per la route HTTP.
Ritorna: {"ok": bool, "screenshot_b64": str, "title": str, "url": str}
"""
if not _safe_url(url):
return {"ok": False, "error": "URL non consentita", "screenshot_b64": "", "title": url, "url": url}
async with _browser_lock:
try:
from playwright.async_api import async_playwright
async with async_playwright() as pw:
browser = await pw.chromium.launch(headless=True, args=_LAUNCH_ARGS)
ctx = await _make_context(browser, width, height, mobile)
page = await ctx.new_page()
try:
await _goto_with_networkidle(page, url, GOTO_TIMEOUT)
await _dismiss_cookie_banner(page)
await page.wait_for_timeout(wait_ms)
png = await page.screenshot(type="png", full_page=False)
title = await page.title()
png_b64 = base64.b64encode(png).decode()
asyncio.create_task(_try_persist_screenshot(url, png_b64, title))
return {"ok": True, "screenshot_b64": png_b64, "title": title, "url": page.url}
except Exception as _e:
return {"ok": False, "error": str(_e)[:500], "screenshot_b64": "", "title": url, "url": url}
finally:
await ctx.close()
await browser.close()
except Exception as _e:
return {"ok": False, "error": str(_e)[:500], "screenshot_b64": "", "title": url, "url": url}
# βββ /screenshot βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@router.post("/screenshot", response_model=BrowserResult)
async def browser_screenshot(req: ScreenshotRequest, role: AuthRole = Depends(require_role(AuthRole.MACHINE))): # GAP-1-fix
"""Screenshot headless di una pagina web (stateless)."""
if not _safe_url(req.url):
raise HTTPException(400, "URL non consentita")
async with _browser_lock:
try:
from playwright.async_api import async_playwright
async with async_playwright() as pw:
browser = await pw.chromium.launch(headless=True, args=_LAUNCH_ARGS)
ctx = await _make_context(browser, req.width, req.height, req.mobile)
page = await ctx.new_page()
try:
# W-NAV3: networkidle per SPA
await _goto_with_networkidle(page, req.url, GOTO_TIMEOUT)
# W-NAV2: dismiss cookie banner prima dello screenshot
await _dismiss_cookie_banner(page)
await page.wait_for_timeout(req.wait_ms)
png = await page.screenshot(type="png", full_page=False)
title = await page.title()
png_b64 = base64.b64encode(png).decode()
asyncio.create_task(_try_persist_screenshot(req.url, png_b64, title)).add_done_callback(_log_browser_bg_exc) # BUGFIX
return BrowserResult(ok=True, screenshot_b64=png_b64, title=title, url=page.url)
except Exception as e:
return BrowserResult(ok=False, error=str(e)[:500]) # S599: 300β500
finally:
await ctx.close()
await browser.close()
except Exception as e:
return BrowserResult(ok=False, error=str(e)[:500])
# βββ /navigate ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@router.post("/navigate", response_model=BrowserResult)
async def browser_navigate(req: NavigateRequest, role: AuthRole = Depends(require_role(AuthRole.MACHINE))): # GAP-1-fix
"""
Naviga, esegui azioni, restituisce screenshot + testo (stateless).
W-NAV: text_content ora estratto via trafilatura (da 2000β5000 chars utili).
"""
if not _safe_url(req.url):
raise HTTPException(400, "URL non consentita")
async with _browser_lock:
try:
from playwright.async_api import async_playwright
async with async_playwright() as pw:
browser = await pw.chromium.launch(headless=True, args=_LAUNCH_ARGS)
ctx = await _make_context(browser, req.width, req.height, req.mobile)
page = await ctx.new_page()
try:
# W-NAV3: networkidle per SPA
await _goto_with_networkidle(page, req.url, GOTO_TIMEOUT)
# W-NAV2: dismiss cookie prima delle azioni
await _dismiss_cookie_banner(page)
await page.wait_for_timeout(500)
await _execute_actions(page, req.actions)
await page.wait_for_timeout(req.wait_ms)
png = await page.screenshot(type="png", full_page=False)
title = await page.title()
# W-NAV: trafilatura estrae mainbody, molto piΓΉ testo utile
text = await _extract_text_trafilatura(page, req.url, max_chars=5000)
png_b64 = base64.b64encode(png).decode()
asyncio.create_task(_try_persist_screenshot(page.url, png_b64, title)).add_done_callback(_log_browser_bg_exc) # BUGFIX
ax_tree = await _get_ax_tree(page) # GAP-AX
return BrowserResult(
ok=True, screenshot_b64=png_b64, title=title,
url=page.url, text_content=text[:5000] if text else None,
ax_tree=ax_tree,
)
except Exception as e:
return BrowserResult(ok=False, error=str(e)[:500])
finally:
await ctx.close()
await browser.close()
except Exception as e:
return BrowserResult(ok=False, error=str(e)[:500])
# βββ /open ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@router.post("/open", response_model=BrowserResult)
async def browser_open(
req: BrowserOpenRequest, request: Request,
role: AuthRole = Depends(require_role(AuthRole.MACHINE)), # P19-SEC2-1: era fail-open
):
"""
Apre una sessione Playwright persistente, naviga all'URL, restituisce
session_id + screenshot + mappa DOM + text_content (trafilatura).
"""
if not _safe_url(req.url):
raise HTTPException(400, "URL non consentita")
if len(_sessions) >= SESSION_LIMIT:
oldest = min(_sessions, key=lambda sid: _sessions[sid]["last_used"])
await _close_session(oldest, "OOM guard")
async with _browser_lock:
try:
# ARCH-7: CDP remoto (Browserless) se BROWSERLESS_TOKEN, else locale
pw, browser, _is_remote = await _get_browser_instance()
ctx = await _make_context(browser, req.width, 800, req.mobile)
page = await ctx.new_page()
# W-NAV3: networkidle per SPA
await _goto_with_networkidle(page, req.url, GOTO_TIMEOUT)
# W-NAV2: dismiss cookie prima dell'estrazione DOM
await _dismiss_cookie_banner(page)
await page.wait_for_timeout(500)
await _execute_actions(page, req.actions)
await page.wait_for_timeout(req.wait_ms)
png = await page.screenshot(type="png", full_page=False)
title = await page.title()
png_b64 = base64.b64encode(png).decode()
dom_raw = await page.evaluate(_DOM_SCRIPT % (MAX_LINKS, MAX_INPUTS, MAX_TEXT))
# W-NAV: text_content via trafilatura (aggiunto β prima non era nella risposta /open)
text = await _extract_text_trafilatura(page, req.url, max_chars=MAX_TEXT)
sid = uuid.uuid4().hex[:16]
_sessions[sid] = {
"pw": pw, "browser": browser, "context": ctx, "page": page,
"created_at": time.time(), "last_used": time.time(), "url": page.url,
"click_history": [],
"visited_urls": [page.url],
"action_log": [],
"is_remote": _is_remote, # ARCH-7: True=CDP, False=locale
}
asyncio.create_task(_try_persist_screenshot(page.url, png_b64, title)).add_done_callback(_log_browser_bg_exc) # BUGFIX
dom = DomSnapshot(**dom_raw) if isinstance(dom_raw, dict) else None
ax_tree = await _get_ax_tree(page) # GAP-AX: Accessibility Tree MCP-style
return BrowserResult(
ok=True, session_id=sid,
screenshot_b64=png_b64, title=title, url=page.url,
dom=dom,
text_content=text[:MAX_TEXT] if text else None,
ax_tree=ax_tree,
)
except Exception as e:
return BrowserResult(ok=False, error=str(e)[:500]) # S603: 400β500
# βββ /act βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@router.post("/act", response_model=BrowserResult)
async def browser_act(
req: BrowserActRequest, request: Request,
role: AuthRole = Depends(require_role(AuthRole.MACHINE)), # P19-SEC2-1: era fail-open
):
"""
Esegue azioni su una sessione aperta.
Restituisce screenshot + mappa DOM + warnings anti-loop.
"""
sess = _sessions.get(req.session_id)
if not sess:
raise HTTPException(404, f"Sessione {req.session_id} non trovata o scaduta")
sess["last_used"] = time.time()
page = sess["page"]
warnings: list[str] = []
CLICK_HISTORY_MAX = 20
LOOP_THRESHOLD = 3
for action in req.actions:
if action.type == "click" and action.selector:
history: list[str] = sess.get("click_history", [])
repeat_count = history.count(action.selector)
if repeat_count >= LOOP_THRESHOLD:
warnings.append(
f"β οΈ ANTI-LOOP: selettore '{action.selector}' giΓ cliccato "
f"{repeat_count}x β cambia strategia (prova altro selettore, scroll, modal, ecc.)"
)
history.append(action.selector)
sess["click_history"] = history[-CLICK_HISTORY_MAX:]
try:
url_before = page.url
await _execute_actions(page, req.actions)
await page.wait_for_timeout(req.wait_ms)
url_after = page.url
sess["url"] = url_after
visited: list[str] = sess.get("visited_urls", [])
if url_after not in visited:
visited.append(url_after)
sess["visited_urls"] = visited[-30:]
action_log: list[dict] = sess.get("action_log", [])
for a in req.actions:
action_log.append({
"type": a.type, "selector": a.selector,
"value": a.value, "url_after": url_after,
})
sess["action_log"] = action_log[-50:]
title = await page.title()
dom_raw = await page.evaluate(_DOM_SCRIPT % (MAX_LINKS, MAX_INPUTS, MAX_TEXT))
dom = DomSnapshot(**dom_raw) if isinstance(dom_raw, dict) else None
if dom and dom.modals:
modal_titles = [m.get("title") or m.get("role", "modal") for m in dom.modals]
warnings.append(
f"π MODAL RILEVATO: {', '.join(str(t) for t in modal_titles)} "
"β potrebbe bloccare le azioni. Chiudilo prima di continuare."
)
ax_tree = await _get_ax_tree(page) if req.take_screenshot else None # GAP-AX
result = BrowserResult(
ok=True, session_id=req.session_id,
url=page.url, title=title, dom=dom, warnings=warnings,
ax_tree=ax_tree,
)
if req.take_screenshot:
png = await page.screenshot(type="png", full_page=False)
png_b64 = base64.b64encode(png).decode()
result.screenshot_b64 = png_b64
asyncio.create_task(_try_persist_screenshot(page.url, png_b64, title)).add_done_callback(_log_browser_bg_exc) # BUGFIX
return result
except Exception as e:
return BrowserResult(ok=False, session_id=req.session_id, error=str(e)[:500], warnings=warnings) # S603
# βββ /close βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@router.post("/close", response_model=BrowserResult)
async def browser_close(req: BrowserCloseRequest, role: AuthRole = Depends(require_role(AuthRole.MACHINE))): # GAP-1-fix
"""Chiude esplicitamente una sessione persistente e libera risorse."""
if req.session_id not in _sessions:
return BrowserResult(ok=True, session_id=req.session_id)
await _close_session(req.session_id, "explicit")
return BrowserResult(ok=True, session_id=req.session_id)
# βββ GET /screenshot/{session_id} βββββββββββββββββββββββββββββββββββββββββββββ
@router.get("/screenshot/{session_id}", response_model=BrowserResult)
async def browser_session_screenshot(session_id: str, full_page: bool = False, role: AuthRole = Depends(require_role(AuthRole.MACHINE))): # GAP-1-fix
"""Snapshot della pagina corrente senza azioni. Aggiorna last_used."""
sess = _sessions.get(session_id)
if not sess:
raise HTTPException(404, f"Sessione {session_id} non trovata o scaduta")
sess["last_used"] = time.time()
page = sess["page"]
try:
png = await page.screenshot(type="png", full_page=full_page)
title = await page.title()
png_b64 = base64.b64encode(png).decode()
asyncio.create_task(_try_persist_screenshot(page.url, png_b64, title)).add_done_callback(_log_browser_bg_exc) # BUGFIX
return BrowserResult(
ok=True, session_id=session_id,
screenshot_b64=png_b64, title=title, url=page.url,
)
except Exception as e:
return BrowserResult(ok=False, session_id=session_id, error=str(e)[:500]) # S603
# βββ /sessions ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@router.get("/sessions")
async def list_sessions(role: AuthRole = Depends(require_role(AuthRole.MACHINE))): # GAP-1-fix
now = time.time()
return {
sid: {
"url": s["url"],
"idle_s": int(now - s["last_used"]),
"age_s": int(now - s["created_at"]),
}
for sid, s in _sessions.items()
}
_start_cleanup()
|