Spaces:
Sleeping
Sleeping
File size: 81,005 Bytes
d7b3d84 |
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 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 |
# pyright: reportMissingImports=false
# Check for MCP mode early to prevent logging initialization
import sys
if '--mcp' in sys.argv:
import logging
import os
os.environ['BROWSER_USE_LOGGING_LEVEL'] = 'critical'
os.environ['BROWSER_USE_SETUP_LOGGING'] = 'false'
logging.disable(logging.CRITICAL)
# Special case: install command doesn't need CLI dependencies
if len(sys.argv) > 1 and sys.argv[1] == 'install':
import platform
import subprocess
print('π¦ Installing Chromium browser + system dependencies...')
print('β³ This may take a few minutes...\n')
# Build command - only use --with-deps on Linux (it fails on Windows/macOS)
cmd = ['uvx', 'playwright', 'install', 'chromium']
if platform.system() == 'Linux':
cmd.append('--with-deps')
cmd.append('--no-shell')
result = subprocess.run(cmd)
if result.returncode == 0:
print('\nβ
Installation complete!')
print('π Ready to use! Run: uvx browser-use')
else:
print('\nβ Installation failed')
sys.exit(1)
sys.exit(0)
# Check for init subcommand early to avoid loading TUI dependencies
if 'init' in sys.argv:
from browser_use.init_cmd import INIT_TEMPLATES
from browser_use.init_cmd import main as init_main
# Check if --template or -t flag is present without a value
# If so, just remove it and let init_main handle interactive mode
if '--template' in sys.argv or '-t' in sys.argv:
try:
template_idx = sys.argv.index('--template') if '--template' in sys.argv else sys.argv.index('-t')
template = sys.argv[template_idx + 1] if template_idx + 1 < len(sys.argv) else None
# If template is not provided or is another flag, remove the flag and use interactive mode
if not template or template.startswith('-'):
if '--template' in sys.argv:
sys.argv.remove('--template')
else:
sys.argv.remove('-t')
except (ValueError, IndexError):
pass
# Remove 'init' from sys.argv so click doesn't see it as an unexpected argument
sys.argv.remove('init')
init_main()
sys.exit(0)
# Check for --template flag early to avoid loading TUI dependencies
if '--template' in sys.argv:
from pathlib import Path
import click
from browser_use.init_cmd import INIT_TEMPLATES
# Parse template and output from sys.argv
try:
template_idx = sys.argv.index('--template')
template = sys.argv[template_idx + 1] if template_idx + 1 < len(sys.argv) else None
except (ValueError, IndexError):
template = None
# If template is not provided or is another flag, use interactive mode
if not template or template.startswith('-'):
# Redirect to init command with interactive template selection
from browser_use.init_cmd import main as init_main
# Remove --template from sys.argv
sys.argv.remove('--template')
init_main()
sys.exit(0)
# Validate template name
if template not in INIT_TEMPLATES:
click.echo(f'β Invalid template. Choose from: {", ".join(INIT_TEMPLATES.keys())}', err=True)
sys.exit(1)
# Check for --output flag
output = None
if '--output' in sys.argv or '-o' in sys.argv:
try:
output_idx = sys.argv.index('--output') if '--output' in sys.argv else sys.argv.index('-o')
output = sys.argv[output_idx + 1] if output_idx + 1 < len(sys.argv) else None
except (ValueError, IndexError):
pass
# Check for --force flag
force = '--force' in sys.argv or '-f' in sys.argv
# Determine output path
output_path = Path(output) if output else Path.cwd() / f'browser_use_{template}.py'
# Read and write template
try:
templates_dir = Path(__file__).parent / 'cli_templates'
template_file = INIT_TEMPLATES[template]['file']
template_path = templates_dir / template_file
content = template_path.read_text(encoding='utf-8')
# Write file with safety checks
if output_path.exists() and not force:
click.echo(f'β οΈ File already exists: {output_path}')
if not click.confirm('Overwrite?', default=False):
click.echo('β Cancelled')
sys.exit(1)
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(content, encoding='utf-8')
click.echo(f'β
Created {output_path}')
click.echo('\nNext steps:')
click.echo(' 1. Install browser-use:')
click.echo(' uv pip install browser-use')
click.echo(' 2. Set up your API key in .env file or environment:')
click.echo(' BROWSER_USE_API_KEY=your-key')
click.echo(' (Get your key at https://cloud.browser-use.com/new-api-key)')
click.echo(' 3. Run your script:')
click.echo(f' python {output_path.name}')
except Exception as e:
click.echo(f'β Error: {e}', err=True)
sys.exit(1)
sys.exit(0)
import asyncio
import json
import logging
import os
import time
from pathlib import Path
from typing import Any
from dotenv import load_dotenv
from browser_use.llm.anthropic.chat import ChatAnthropic
from browser_use.llm.google.chat import ChatGoogle
from browser_use.llm.openai.chat import ChatOpenAI
load_dotenv()
from browser_use import Agent, Controller
from browser_use.agent.views import AgentSettings
from browser_use.browser import BrowserProfile, BrowserSession
from browser_use.logging_config import addLoggingLevel
from browser_use.telemetry import CLITelemetryEvent, ProductTelemetry
from browser_use.utils import get_browser_use_version
try:
import click
from textual import events
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.containers import Container, HorizontalGroup, VerticalScroll
from textual.widgets import Footer, Header, Input, Label, Link, RichLog, Static
except ImportError:
print('β οΈ CLI addon is not installed. Please install it with: `pip install "browser-use[cli]"` and try again.')
sys.exit(1)
try:
import readline
READLINE_AVAILABLE = True
except ImportError:
# readline not available on Windows by default
READLINE_AVAILABLE = False
os.environ['BROWSER_USE_LOGGING_LEVEL'] = 'result'
from browser_use.config import CONFIG
# Set USER_DATA_DIR now that CONFIG is imported
USER_DATA_DIR = CONFIG.BROWSER_USE_PROFILES_DIR / 'cli'
# Ensure directories exist
CONFIG.BROWSER_USE_CONFIG_FILE.parent.mkdir(parents=True, exist_ok=True)
USER_DATA_DIR.mkdir(parents=True, exist_ok=True)
# Default User settings
MAX_HISTORY_LENGTH = 100
# Directory setup will happen in functions that need CONFIG
# Logo components with styling for rich panels
BROWSER_LOGO = """
[white] ++++++ +++++++++ [/]
[white] +++ +++++ +++ [/]
[white] ++ ++++ ++ ++ [/]
[white] ++ +++ +++ ++ [/]
[white] ++++ +++ [/]
[white] +++ +++ [/]
[white] +++ +++ [/]
[white] ++ +++ +++ ++ [/]
[white] ++ ++++ ++ ++ [/]
[white] +++ ++++++ +++ [/]
[white] ++++++ +++++++ [/]
[white]βββββββ βββββββ βββββββ βββ ββββββββββββββββββββββββββ[/] [darkorange]βββ βββββββββββββββββββ[/]
[white]ββββββββββββββββββββββββββββ βββββββββββββββββββββββββββ[/] [darkorange]βββ βββββββββββββββββββ[/]
[white]βββββββββββββββββββ ββββββ ββ βββββββββββββββββ ββββββββ[/] [darkorange]βββ βββββββββββββββββ[/]
[white]βββββββββββββββββββ βββββββββββββββββββββββββββ ββββββββ[/] [darkorange]βββ βββββββββββββββββ[/]
[white]βββββββββββ βββββββββββββββββββββββββββββββββββββββββ βββ[/] [darkorange]βββββββββββββββββββββββββ[/]
[white]βββββββ βββ βββ βββββββ ββββββββ βββββββββββββββββββ βββ[/] [darkorange]βββββββ ββββββββββββββββ[/]
"""
# Common UI constants
TEXTUAL_BORDER_STYLES = {'logo': 'blue', 'info': 'blue', 'input': 'orange3', 'working': 'yellow', 'completion': 'green'}
def get_default_config() -> dict[str, Any]:
"""Return default configuration dictionary using the new config system."""
# Load config from the new config system
config_data = CONFIG.load_config()
# Extract browser profile, llm, and agent configs
browser_profile = config_data.get('browser_profile', {})
llm_config = config_data.get('llm', {})
agent_config = config_data.get('agent', {})
return {
'model': {
'name': llm_config.get('model'),
'temperature': llm_config.get('temperature', 0.0),
'api_keys': {
'OPENAI_API_KEY': llm_config.get('api_key', CONFIG.OPENAI_API_KEY),
'ANTHROPIC_API_KEY': CONFIG.ANTHROPIC_API_KEY,
'GOOGLE_API_KEY': CONFIG.GOOGLE_API_KEY,
'DEEPSEEK_API_KEY': CONFIG.DEEPSEEK_API_KEY,
'GROK_API_KEY': CONFIG.GROK_API_KEY,
},
},
'agent': agent_config,
'browser': {
'headless': browser_profile.get('headless', True),
'keep_alive': browser_profile.get('keep_alive', True),
'ignore_https_errors': browser_profile.get('ignore_https_errors', False),
'user_data_dir': browser_profile.get('user_data_dir'),
'allowed_domains': browser_profile.get('allowed_domains'),
'wait_between_actions': browser_profile.get('wait_between_actions'),
'is_mobile': browser_profile.get('is_mobile'),
'device_scale_factor': browser_profile.get('device_scale_factor'),
'disable_security': browser_profile.get('disable_security'),
},
'command_history': [],
}
def load_user_config() -> dict[str, Any]:
"""Load user configuration using the new config system."""
# Just get the default config which already loads from the new system
config = get_default_config()
# Load command history from a separate file if it exists
history_file = CONFIG.BROWSER_USE_CONFIG_DIR / 'command_history.json'
if history_file.exists():
try:
with open(history_file) as f:
config['command_history'] = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
config['command_history'] = []
return config
def save_user_config(config: dict[str, Any]) -> None:
"""Save command history only (config is saved via the new system)."""
# Only save command history to a separate file
if 'command_history' in config and isinstance(config['command_history'], list):
# Ensure command history doesn't exceed maximum length
history = config['command_history']
if len(history) > MAX_HISTORY_LENGTH:
history = history[-MAX_HISTORY_LENGTH:]
# Save to separate history file
history_file = CONFIG.BROWSER_USE_CONFIG_DIR / 'command_history.json'
with open(history_file, 'w') as f:
json.dump(history, f, indent=2)
def update_config_with_click_args(config: dict[str, Any], ctx: click.Context) -> dict[str, Any]:
"""Update configuration with command-line arguments."""
# Ensure required sections exist
if 'model' not in config:
config['model'] = {}
if 'browser' not in config:
config['browser'] = {}
# Update configuration with command-line args if provided
if ctx.params.get('model'):
config['model']['name'] = ctx.params['model']
if ctx.params.get('headless') is not None:
config['browser']['headless'] = ctx.params['headless']
if ctx.params.get('window_width'):
config['browser']['window_width'] = ctx.params['window_width']
if ctx.params.get('window_height'):
config['browser']['window_height'] = ctx.params['window_height']
if ctx.params.get('user_data_dir'):
config['browser']['user_data_dir'] = ctx.params['user_data_dir']
if ctx.params.get('profile_directory'):
config['browser']['profile_directory'] = ctx.params['profile_directory']
if ctx.params.get('cdp_url'):
config['browser']['cdp_url'] = ctx.params['cdp_url']
# Consolidated proxy dict
proxy: dict[str, str] = {}
if ctx.params.get('proxy_url'):
proxy['server'] = ctx.params['proxy_url']
if ctx.params.get('no_proxy'):
# Store as comma-separated list string to match Chrome flag
proxy['bypass'] = ','.join([p.strip() for p in ctx.params['no_proxy'].split(',') if p.strip()])
if ctx.params.get('proxy_username'):
proxy['username'] = ctx.params['proxy_username']
if ctx.params.get('proxy_password'):
proxy['password'] = ctx.params['proxy_password']
if proxy:
config['browser']['proxy'] = proxy
return config
def setup_readline_history(history: list[str]) -> None:
"""Set up readline with command history."""
if not READLINE_AVAILABLE:
return
# Add history items to readline
for item in history:
readline.add_history(item)
def get_llm(config: dict[str, Any]):
"""Get the language model based on config and available API keys."""
model_config = config.get('model', {})
model_name = model_config.get('name')
temperature = model_config.get('temperature', 0.0)
# Get API key from config or environment
api_key = model_config.get('api_keys', {}).get('OPENAI_API_KEY') or CONFIG.OPENAI_API_KEY
if model_name:
if model_name.startswith('gpt'):
if not api_key and not CONFIG.OPENAI_API_KEY:
print('β οΈ OpenAI API key not found. Please update your config or set OPENAI_API_KEY environment variable.')
sys.exit(1)
return ChatOpenAI(model=model_name, temperature=temperature, api_key=api_key or CONFIG.OPENAI_API_KEY)
elif model_name.startswith('claude'):
if not CONFIG.ANTHROPIC_API_KEY:
print('β οΈ Anthropic API key not found. Please update your config or set ANTHROPIC_API_KEY environment variable.')
sys.exit(1)
return ChatAnthropic(model=model_name, temperature=temperature)
elif model_name.startswith('gemini'):
if not CONFIG.GOOGLE_API_KEY:
print('β οΈ Google API key not found. Please update your config or set GOOGLE_API_KEY environment variable.')
sys.exit(1)
return ChatGoogle(model=model_name, temperature=temperature)
elif model_name.startswith('oci'):
# OCI models require additional configuration
print(
'β οΈ OCI models require manual configuration. Please use the ChatOCIRaw class directly with your OCI credentials.'
)
sys.exit(1)
# Auto-detect based on available API keys
if api_key or CONFIG.OPENAI_API_KEY:
return ChatOpenAI(model='gpt-5-mini', temperature=temperature, api_key=api_key or CONFIG.OPENAI_API_KEY)
elif CONFIG.ANTHROPIC_API_KEY:
return ChatAnthropic(model='claude-4-sonnet', temperature=temperature)
elif CONFIG.GOOGLE_API_KEY:
return ChatGoogle(model='gemini-2.5-pro', temperature=temperature)
else:
print(
'β οΈ No API keys found. Please update your config or set one of: OPENAI_API_KEY, ANTHROPIC_API_KEY, or GOOGLE_API_KEY.'
)
sys.exit(1)
class RichLogHandler(logging.Handler):
"""Custom logging handler that redirects logs to a RichLog widget."""
def __init__(self, rich_log: RichLog):
super().__init__()
self.rich_log = rich_log
def emit(self, record):
try:
msg = self.format(record)
self.rich_log.write(msg)
except Exception:
self.handleError(record)
class BrowserUseApp(App):
"""Browser-use TUI application."""
# Make it an inline app instead of fullscreen
# MODES = {"light"} # Ensure app is inline, not fullscreen
CSS = """
#main-container {
height: 100%;
layout: vertical;
}
#logo-panel, #links-panel, #paths-panel, #info-panels {
border: solid $primary;
margin: 0 0 0 0;
padding: 0;
}
#info-panels {
display: none;
layout: vertical;
height: auto;
min-height: 5;
margin: 0 0 1 0;
}
#top-panels {
layout: horizontal;
height: auto;
width: 100%;
}
#browser-panel, #model-panel {
width: 1fr;
height: 100%;
padding: 1;
border-right: solid $primary;
}
#model-panel {
border-right: none;
}
#tasks-panel {
height: auto;
max-height: 10;
overflow-y: scroll;
padding: 1;
border-top: solid $primary;
}
#browser-info, #model-info, #tasks-info {
height: auto;
margin: 0;
padding: 0;
background: transparent;
overflow-y: auto;
min-height: 3;
}
#three-column-container {
height: 1fr;
layout: horizontal;
width: 100%;
display: none;
}
#main-output-column {
width: 1fr;
height: 100%;
border: solid $primary;
padding: 0;
margin: 0 1 0 0;
}
#events-column {
width: 1fr;
height: 100%;
border: solid $warning;
padding: 0;
margin: 0 1 0 0;
}
#cdp-column {
width: 1fr;
height: 100%;
border: solid $accent;
padding: 0;
margin: 0;
}
#main-output-log, #events-log, #cdp-log {
height: 100%;
overflow-y: scroll;
background: $surface;
color: $text;
width: 100%;
padding: 1;
}
#events-log {
color: $warning;
}
#cdp-log {
color: $accent-lighten-2;
}
#logo-panel {
width: 100%;
height: auto;
content-align: center middle;
text-align: center;
}
#links-panel {
width: 100%;
padding: 1;
border: solid $primary;
height: auto;
}
.link-white {
color: white;
}
.link-purple {
color: purple;
}
.link-magenta {
color: magenta;
}
.link-green {
color: green;
}
HorizontalGroup {
height: auto;
}
.link-label {
width: auto;
}
.link-url {
width: auto;
}
.link-row {
width: 100%;
height: auto;
}
#paths-panel {
color: $text-muted;
}
#task-input-container {
border: solid $accent;
padding: 1;
margin-bottom: 1;
height: auto;
dock: bottom;
}
#task-label {
color: $accent;
padding-bottom: 1;
}
#task-input {
width: 100%;
}
"""
BINDINGS = [
Binding('ctrl+c', 'quit', 'Quit', priority=True, show=True),
Binding('ctrl+q', 'quit', 'Quit', priority=True),
Binding('ctrl+d', 'quit', 'Quit', priority=True),
Binding('up', 'input_history_prev', 'Previous command', show=False),
Binding('down', 'input_history_next', 'Next command', show=False),
]
def __init__(self, config: dict[str, Any], *args, **kwargs):
super().__init__(*args, **kwargs)
self.config = config
self.browser_session: BrowserSession | None = None # Will be set before app.run_async()
self.controller: Controller | None = None # Will be set before app.run_async()
self.agent: Agent | None = None
self.llm: Any | None = None # Will be set before app.run_async()
self.task_history = config.get('command_history', [])
# Track current position in history for up/down navigation
self.history_index = len(self.task_history)
# Initialize telemetry
self._telemetry = ProductTelemetry()
# Store for event bus handler
self._event_bus_handler_id = None
self._event_bus_handler_func = None
# Timer for info panel updates
self._info_panel_timer = None
def setup_richlog_logging(self) -> None:
"""Set up logging to redirect to RichLog widget instead of stdout."""
# Try to add RESULT level if it doesn't exist
try:
addLoggingLevel('RESULT', 35)
except AttributeError:
pass # Level already exists, which is fine
# Get the main output RichLog widget
rich_log = self.query_one('#main-output-log', RichLog)
# Create and set up the custom handler
log_handler = RichLogHandler(rich_log)
log_type = os.getenv('BROWSER_USE_LOGGING_LEVEL', 'result').lower()
class BrowserUseFormatter(logging.Formatter):
def format(self, record):
# if isinstance(record.name, str) and record.name.startswith('browser_use.'):
# record.name = record.name.split('.')[-2]
return super().format(record)
# Set up the formatter based on log type
if log_type == 'result':
log_handler.setLevel('RESULT')
log_handler.setFormatter(BrowserUseFormatter('%(message)s'))
else:
log_handler.setFormatter(BrowserUseFormatter('%(levelname)-8s [%(name)s] %(message)s'))
# Configure root logger - Replace ALL handlers, not just stdout handlers
root = logging.getLogger()
# Clear all existing handlers to prevent output to stdout/stderr
root.handlers = []
root.addHandler(log_handler)
# Set log level based on environment variable
if log_type == 'result':
root.setLevel('RESULT')
elif log_type == 'debug':
root.setLevel(logging.DEBUG)
else:
root.setLevel(logging.INFO)
# Configure browser_use logger and all its sub-loggers
browser_use_logger = logging.getLogger('browser_use')
browser_use_logger.propagate = False # Don't propagate to root logger
browser_use_logger.handlers = [log_handler] # Replace any existing handlers
browser_use_logger.setLevel(root.level)
# Also ensure agent loggers go to the main output
# Use a wildcard pattern to catch all agent-related loggers
for logger_name in ['browser_use.Agent', 'browser_use.controller', 'browser_use.agent', 'browser_use.agent.service']:
agent_logger = logging.getLogger(logger_name)
agent_logger.propagate = False
agent_logger.handlers = [log_handler]
agent_logger.setLevel(root.level)
# Also catch any dynamically created agent loggers with task IDs
for name, logger in logging.Logger.manager.loggerDict.items():
if isinstance(name, str) and 'browser_use.Agent' in name:
if isinstance(logger, logging.Logger):
logger.propagate = False
logger.handlers = [log_handler]
logger.setLevel(root.level)
# Silence third-party loggers but keep them using our handler
for logger_name in [
'WDM',
'httpx',
'selenium',
'playwright',
'urllib3',
'asyncio',
'openai',
'httpcore',
'charset_normalizer',
'anthropic._base_client',
'PIL.PngImagePlugin',
'trafilatura.htmlprocessing',
'trafilatura',
'groq',
'portalocker',
'portalocker.utils',
]:
third_party = logging.getLogger(logger_name)
third_party.setLevel(logging.ERROR)
third_party.propagate = False
third_party.handlers = [log_handler] # Use our handler to prevent stdout/stderr leakage
def on_mount(self) -> None:
"""Set up components when app is mounted."""
# We'll use a file logger since stdout is now controlled by Textual
logger = logging.getLogger('browser_use.on_mount')
logger.debug('on_mount() method started')
# Step 1: Set up custom logging to RichLog
logger.debug('Setting up RichLog logging...')
try:
self.setup_richlog_logging()
logger.debug('RichLog logging set up successfully')
except Exception as e:
logger.error(f'Error setting up RichLog logging: {str(e)}', exc_info=True)
raise RuntimeError(f'Failed to set up RichLog logging: {str(e)}')
# Step 2: Set up input history
logger.debug('Setting up readline history...')
try:
if READLINE_AVAILABLE and self.task_history:
for item in self.task_history:
readline.add_history(item)
logger.debug(f'Added {len(self.task_history)} items to readline history')
else:
logger.debug('No readline history to set up')
except Exception as e:
logger.error(f'Error setting up readline history: {str(e)}', exc_info=False)
# Non-critical, continue
# Step 3: Focus the input field
logger.debug('Focusing input field...')
try:
input_field = self.query_one('#task-input', Input)
input_field.focus()
logger.debug('Input field focused')
except Exception as e:
logger.error(f'Error focusing input field: {str(e)}', exc_info=True)
# Non-critical, continue
# Step 5: Setup CDP logger and event bus listener if browser session is available
logger.debug('Setting up CDP logging and event bus listener...')
try:
self.setup_cdp_logger()
if self.browser_session:
self.setup_event_bus_listener()
logger.debug('CDP logging and event bus setup complete')
except Exception as e:
logger.error(f'Error setting up CDP logging/event bus: {str(e)}', exc_info=True)
# Non-critical, continue
# Capture telemetry for CLI start
self._telemetry.capture(
CLITelemetryEvent(
version=get_browser_use_version(),
action='start',
mode='interactive',
model=self.llm.model if self.llm and hasattr(self.llm, 'model') else None,
model_provider=self.llm.provider if self.llm and hasattr(self.llm, 'provider') else None,
)
)
logger.debug('on_mount() completed successfully')
def on_input_key_up(self, event: events.Key) -> None:
"""Handle up arrow key in the input field."""
# For textual key events, we need to check focus manually
input_field = self.query_one('#task-input', Input)
if not input_field.has_focus:
return
# Only process if we have history
if not self.task_history:
return
# Move back in history if possible
if self.history_index > 0:
self.history_index -= 1
task_input = self.query_one('#task-input', Input)
task_input.value = self.task_history[self.history_index]
# Move cursor to end of text
task_input.cursor_position = len(task_input.value)
# Prevent default behavior (cursor movement)
event.prevent_default()
event.stop()
def on_input_key_down(self, event: events.Key) -> None:
"""Handle down arrow key in the input field."""
# For textual key events, we need to check focus manually
input_field = self.query_one('#task-input', Input)
if not input_field.has_focus:
return
# Only process if we have history
if not self.task_history:
return
# Move forward in history or clear input if at the end
if self.history_index < len(self.task_history) - 1:
self.history_index += 1
task_input = self.query_one('#task-input', Input)
task_input.value = self.task_history[self.history_index]
# Move cursor to end of text
task_input.cursor_position = len(task_input.value)
elif self.history_index == len(self.task_history) - 1:
# At the end of history, go to "new line" state
self.history_index += 1
self.query_one('#task-input', Input).value = ''
# Prevent default behavior (cursor movement)
event.prevent_default()
event.stop()
async def on_key(self, event: events.Key) -> None:
"""Handle key events at the app level to ensure graceful exit."""
# Handle Ctrl+C, Ctrl+D, and Ctrl+Q for app exit
if event.key == 'ctrl+c' or event.key == 'ctrl+d' or event.key == 'ctrl+q':
await self.action_quit()
event.stop()
event.prevent_default()
def on_input_submitted(self, event: Input.Submitted) -> None:
"""Handle task input submission."""
if event.input.id == 'task-input':
task = event.input.value
if not task.strip():
return
# Add to history if it's new
if task.strip() and (not self.task_history or task != self.task_history[-1]):
self.task_history.append(task)
self.config['command_history'] = self.task_history
save_user_config(self.config)
# Reset history index to point past the end of history
self.history_index = len(self.task_history)
# Hide logo, links, and paths panels
self.hide_intro_panels()
# Process the task
self.run_task(task)
# Clear the input
event.input.value = ''
def hide_intro_panels(self) -> None:
"""Hide the intro panels, show info panels and the three-column view."""
try:
# Get the panels
logo_panel = self.query_one('#logo-panel')
links_panel = self.query_one('#links-panel')
paths_panel = self.query_one('#paths-panel')
info_panels = self.query_one('#info-panels')
three_column = self.query_one('#three-column-container')
# Hide intro panels if they're visible and show info panels + three-column view
if logo_panel.display:
logging.debug('Hiding intro panels and showing info panels + three-column view')
logo_panel.display = False
links_panel.display = False
paths_panel.display = False
# Show info panels and three-column container
info_panels.display = True
three_column.display = True
# Start updating info panels
self.update_info_panels()
logging.debug('Info panels and three-column view should now be visible')
except Exception as e:
logging.error(f'Error in hide_intro_panels: {str(e)}')
def setup_event_bus_listener(self) -> None:
"""Setup listener for browser session event bus."""
if not self.browser_session or not self.browser_session.event_bus:
return
# Clean up any existing handler before registering a new one
if self._event_bus_handler_func is not None:
try:
# Remove handler from the event bus's internal handlers dict
if hasattr(self.browser_session.event_bus, 'handlers'):
# Find and remove our handler function from all event patterns
for event_type, handler_list in list(self.browser_session.event_bus.handlers.items()):
# Remove our specific handler function object
if self._event_bus_handler_func in handler_list:
handler_list.remove(self._event_bus_handler_func)
logging.debug(f'Removed old handler from event type: {event_type}')
except Exception as e:
logging.debug(f'Error cleaning up event bus handler: {e}')
self._event_bus_handler_func = None
self._event_bus_handler_id = None
try:
# Get the events log widget
events_log = self.query_one('#events-log', RichLog)
except Exception:
# Widget not ready yet
return
# Create handler to log all events
def log_event(event):
event_name = event.__class__.__name__
# Format event data nicely
try:
if hasattr(event, 'model_dump'):
event_data = event.model_dump(exclude_unset=True)
# Remove large fields
if 'screenshot' in event_data:
event_data['screenshot'] = '<bytes>'
if 'dom_state' in event_data:
event_data['dom_state'] = '<truncated>'
event_str = str(event_data) if event_data else ''
else:
event_str = str(event)
# Truncate long strings
if len(event_str) > 200:
event_str = event_str[:200] + '...'
events_log.write(f'[yellow]β {event_name}[/] {event_str}')
except Exception as e:
events_log.write(f'[red]β {event_name}[/] (error formatting: {e})')
# Store the handler function before registering it
self._event_bus_handler_func = log_event
self._event_bus_handler_id = id(log_event)
# Register wildcard handler for all events
self.browser_session.event_bus.on('*', log_event)
logging.debug(f'Registered new event bus handler with id: {self._event_bus_handler_id}')
def setup_cdp_logger(self) -> None:
"""Setup CDP message logger to capture already-transformed CDP logs."""
# No need to configure levels - setup_logging() already handles that
# We just need to capture the transformed logs and route them to the CDP pane
# Get the CDP log widget
cdp_log = self.query_one('#cdp-log', RichLog)
# Create custom handler for CDP logging
class CDPLogHandler(logging.Handler):
def __init__(self, rich_log: RichLog):
super().__init__()
self.rich_log = rich_log
def emit(self, record):
try:
msg = self.format(record)
# Truncate very long messages
if len(msg) > 300:
msg = msg[:300] + '...'
# Color code by level
if record.levelno >= logging.ERROR:
self.rich_log.write(f'[red]{msg}[/]')
elif record.levelno >= logging.WARNING:
self.rich_log.write(f'[yellow]{msg}[/]')
else:
self.rich_log.write(f'[cyan]{msg}[/]')
except Exception:
self.handleError(record)
# Setup handler for cdp_use loggers
cdp_handler = CDPLogHandler(cdp_log)
cdp_handler.setFormatter(logging.Formatter('%(message)s'))
cdp_handler.setLevel(logging.DEBUG)
# Route CDP logs to the CDP pane
# These are already transformed by cdp_use and at the right level from setup_logging
for logger_name in ['websockets.client', 'cdp_use', 'cdp_use.client', 'cdp_use.cdp', 'cdp_use.cdp.registry']:
logger = logging.getLogger(logger_name)
# Add our handler (don't replace - keep existing console handler too)
if cdp_handler not in logger.handlers:
logger.addHandler(cdp_handler)
def scroll_to_input(self) -> None:
"""Scroll to the input field to ensure it's visible."""
input_container = self.query_one('#task-input-container')
input_container.scroll_visible()
def run_task(self, task: str) -> None:
"""Launch the task in a background worker."""
# Create or update the agent
agent_settings = AgentSettings.model_validate(self.config.get('agent', {}))
# Get the logger
logger = logging.getLogger('browser_use.app')
# Make sure intro is hidden and log is ready
self.hide_intro_panels()
# Clear the main output log to start fresh
rich_log = self.query_one('#main-output-log', RichLog)
rich_log.clear()
if self.agent is None:
if not self.llm:
raise RuntimeError('LLM not initialized')
self.agent = Agent(
task=task,
llm=self.llm,
controller=self.controller if self.controller else Controller(),
browser_session=self.browser_session,
source='cli',
**agent_settings.model_dump(),
)
# Update our browser_session reference to point to the agent's
if hasattr(self.agent, 'browser_session'):
self.browser_session = self.agent.browser_session
# Set up event bus listener (will clean up any old handler first)
self.setup_event_bus_listener()
else:
self.agent.add_new_task(task)
# Let the agent run in the background
async def agent_task_worker() -> None:
logger.debug('\nπ Working on task: %s', task)
# Set flags to indicate the agent is running
if self.agent:
self.agent.running = True # type: ignore
self.agent.last_response_time = 0 # type: ignore
# Panel updates are already happening via the timer in update_info_panels
task_start_time = time.time()
error_msg = None
try:
# Capture telemetry for message sent
self._telemetry.capture(
CLITelemetryEvent(
version=get_browser_use_version(),
action='message_sent',
mode='interactive',
model=self.llm.model if self.llm and hasattr(self.llm, 'model') else None,
model_provider=self.llm.provider if self.llm and hasattr(self.llm, 'provider') else None,
)
)
# Run the agent task, redirecting output to RichLog through our handler
if self.agent:
await self.agent.run()
except Exception as e:
error_msg = str(e)
logger.error('\nError running agent: %s', str(e))
finally:
# Clear the running flag
if self.agent:
self.agent.running = False # type: ignore
# Capture telemetry for task completion
duration = time.time() - task_start_time
self._telemetry.capture(
CLITelemetryEvent(
version=get_browser_use_version(),
action='task_completed' if error_msg is None else 'error',
mode='interactive',
model=self.llm.model if self.llm and hasattr(self.llm, 'model') else None,
model_provider=self.llm.provider if self.llm and hasattr(self.llm, 'provider') else None,
duration_seconds=duration,
error_message=error_msg,
)
)
logger.debug('\nβ
Task completed!')
# Make sure the task input container is visible
task_input_container = self.query_one('#task-input-container')
task_input_container.display = True
# Refocus the input field
input_field = self.query_one('#task-input', Input)
input_field.focus()
# Ensure the input is visible by scrolling to it
self.call_after_refresh(self.scroll_to_input)
# Run the worker
self.run_worker(agent_task_worker, name='agent_task')
def action_input_history_prev(self) -> None:
"""Navigate to the previous item in command history."""
# Only process if we have history and input is focused
input_field = self.query_one('#task-input', Input)
if not input_field.has_focus or not self.task_history:
return
# Move back in history if possible
if self.history_index > 0:
self.history_index -= 1
input_field.value = self.task_history[self.history_index]
# Move cursor to end of text
input_field.cursor_position = len(input_field.value)
def action_input_history_next(self) -> None:
"""Navigate to the next item in command history or clear input."""
# Only process if we have history and input is focused
input_field = self.query_one('#task-input', Input)
if not input_field.has_focus or not self.task_history:
return
# Move forward in history or clear input if at the end
if self.history_index < len(self.task_history) - 1:
self.history_index += 1
input_field.value = self.task_history[self.history_index]
# Move cursor to end of text
input_field.cursor_position = len(input_field.value)
elif self.history_index == len(self.task_history) - 1:
# At the end of history, go to "new line" state
self.history_index += 1
input_field.value = ''
async def action_quit(self) -> None:
"""Quit the application and clean up resources."""
# Note: We don't need to close the browser session here because:
# 1. If an agent exists, it already called browser_session.stop() in its run() method
# 2. If keep_alive=True (default), we want to leave the browser running anyway
# This prevents the duplicate "stop() called" messages in the logs
# Flush telemetry before exiting
self._telemetry.flush()
# Exit the application
self.exit()
print('\nTry running tasks on our cloud: https://browser-use.com')
def compose(self) -> ComposeResult:
"""Create the UI layout."""
yield Header()
# Main container for app content
with Container(id='main-container'):
# Logo panel
yield Static(BROWSER_LOGO, id='logo-panel', markup=True)
# Links panel with URLs
with Container(id='links-panel'):
with HorizontalGroup(classes='link-row'):
yield Static('Run at scale on cloud: [blink]βοΈ[/] ', markup=True, classes='link-label')
yield Link('https://browser-use.com', url='https://browser-use.com', classes='link-white link-url')
yield Static('') # Empty line
with HorizontalGroup(classes='link-row'):
yield Static('Chat & share on Discord: π ', markup=True, classes='link-label')
yield Link(
'https://discord.gg/ESAUZAdxXY', url='https://discord.gg/ESAUZAdxXY', classes='link-purple link-url'
)
with HorizontalGroup(classes='link-row'):
yield Static('Get prompt inspiration: π¦Έ ', markup=True, classes='link-label')
yield Link(
'https://github.com/browser-use/awesome-prompts',
url='https://github.com/browser-use/awesome-prompts',
classes='link-magenta link-url',
)
with HorizontalGroup(classes='link-row'):
yield Static('[dim]Report any issues:[/] π ', markup=True, classes='link-label')
yield Link(
'https://github.com/browser-use/browser-use/issues',
url='https://github.com/browser-use/browser-use/issues',
classes='link-green link-url',
)
# Paths panel
yield Static(
f' βοΈ Settings saved to: {str(CONFIG.BROWSER_USE_CONFIG_FILE.resolve()).replace(str(Path.home()), "~")}\n'
f' π Outputs & recordings saved to: {str(Path(".").resolve()).replace(str(Path.home()), "~")}',
id='paths-panel',
markup=True,
)
# Info panels (hidden by default, shown when task starts)
with Container(id='info-panels'):
# Top row with browser and model panels side by side
with Container(id='top-panels'):
# Browser panel
with Container(id='browser-panel'):
yield RichLog(id='browser-info', markup=True, highlight=True, wrap=True)
# Model panel
with Container(id='model-panel'):
yield RichLog(id='model-info', markup=True, highlight=True, wrap=True)
# Tasks panel (full width, below browser and model)
with VerticalScroll(id='tasks-panel'):
yield RichLog(id='tasks-info', markup=True, highlight=True, wrap=True, auto_scroll=True)
# Three-column container (hidden by default)
with Container(id='three-column-container'):
# Column 1: Main output
with VerticalScroll(id='main-output-column'):
yield RichLog(highlight=True, markup=True, id='main-output-log', wrap=True, auto_scroll=True)
# Column 2: Event bus events
with VerticalScroll(id='events-column'):
yield RichLog(highlight=True, markup=True, id='events-log', wrap=True, auto_scroll=True)
# Column 3: CDP messages
with VerticalScroll(id='cdp-column'):
yield RichLog(highlight=True, markup=True, id='cdp-log', wrap=True, auto_scroll=True)
# Task input container (now at the bottom)
with Container(id='task-input-container'):
yield Label('π What would you like me to do on the web?', id='task-label')
yield Input(placeholder='Enter your task...', id='task-input')
yield Footer()
def update_info_panels(self) -> None:
"""Update all information panels with current state."""
try:
# Update actual content
self.update_browser_panel()
self.update_model_panel()
self.update_tasks_panel()
except Exception as e:
logging.error(f'Error in update_info_panels: {str(e)}')
finally:
# Always schedule the next update - will update at 1-second intervals
# This ensures continuous updates even if agent state changes
self.set_timer(1.0, self.update_info_panels)
def update_browser_panel(self) -> None:
"""Update browser information panel with details about the browser."""
browser_info = self.query_one('#browser-info', RichLog)
browser_info.clear()
# Try to use the agent's browser session if available
browser_session = self.browser_session
if hasattr(self, 'agent') and self.agent and hasattr(self.agent, 'browser_session'):
browser_session = self.agent.browser_session
if browser_session:
try:
# Check if browser session has a CDP client
if not hasattr(browser_session, 'cdp_client') or browser_session.cdp_client is None:
browser_info.write('[yellow]Browser session created, waiting for browser to launch...[/]')
return
# Update our reference if we're using the agent's session
if browser_session != self.browser_session:
self.browser_session = browser_session
# Get basic browser info from browser_profile
browser_type = 'Chromium'
headless = browser_session.browser_profile.headless
# Determine connection type based on config
connection_type = 'playwright' # Default
if browser_session.cdp_url:
connection_type = 'CDP'
elif browser_session.browser_profile.executable_path:
connection_type = 'user-provided'
# Get window size details from browser_profile
window_width = None
window_height = None
if browser_session.browser_profile.viewport:
window_width = browser_session.browser_profile.viewport.width
window_height = browser_session.browser_profile.viewport.height
# Try to get browser PID
browser_pid = 'Unknown'
connected = False
browser_status = '[red]Disconnected[/]'
try:
# Check if browser PID is available
# Check if we have a CDP client
if browser_session.cdp_client is not None:
connected = True
browser_status = '[green]Connected[/]'
browser_pid = 'N/A'
except Exception as e:
browser_pid = f'Error: {str(e)}'
# Display browser information
browser_info.write(f'[bold cyan]Chromium[/] Browser ({browser_status})')
browser_info.write(
f'Type: [yellow]{connection_type}[/] [{"green" if not headless else "red"}]{" (headless)" if headless else ""}[/]'
)
browser_info.write(f'PID: [dim]{browser_pid}[/]')
browser_info.write(f'CDP Port: {browser_session.cdp_url}')
if window_width and window_height:
browser_info.write(f'Window: [blue]{window_width}[/] Γ [blue]{window_height}[/]')
# Include additional information about the browser if needed
if connected and hasattr(self, 'agent') and self.agent:
try:
# Show when the browser was connected
timestamp = int(time.time())
current_time = time.strftime('%H:%M:%S', time.localtime(timestamp))
browser_info.write(f'Last updated: [dim]{current_time}[/]')
except Exception:
pass
# Show the agent's current page URL if available
if browser_session.agent_focus:
current_url = (
browser_session.agent_focus.url.replace('https://', '')
.replace('http://', '')
.replace('www.', '')[:36]
+ 'β¦'
)
browser_info.write(f'ποΈ [green]{current_url}[/]')
except Exception as e:
browser_info.write(f'[red]Error updating browser info: {str(e)}[/]')
else:
browser_info.write('[red]Browser not initialized[/]')
def update_model_panel(self) -> None:
"""Update model information panel with details about the LLM."""
model_info = self.query_one('#model-info', RichLog)
model_info.clear()
if self.llm:
# Get model details
model_name = 'Unknown'
if hasattr(self.llm, 'model_name'):
model_name = self.llm.model_name
elif hasattr(self.llm, 'model'):
model_name = self.llm.model
# Show model name
if self.agent:
temp_str = f'{self.llm.temperature}ΒΊC ' if self.llm.temperature else ''
vision_str = '+ vision ' if self.agent.settings.use_vision else ''
model_info.write(
f'[white]LLM:[/] [blue]{self.llm.__class__.__name__} [yellow]{model_name}[/] {temp_str}{vision_str}'
)
else:
model_info.write(f'[white]LLM:[/] [blue]{self.llm.__class__.__name__} [yellow]{model_name}[/]')
# Show token usage statistics if agent exists and has history
if self.agent and hasattr(self.agent, 'state') and hasattr(self.agent.state, 'history'):
# Calculate tokens per step
num_steps = len(self.agent.history.history)
# Get the last step metadata to show the most recent LLM response time
if num_steps > 0 and self.agent.history.history[-1].metadata:
last_step = self.agent.history.history[-1]
if last_step.metadata:
step_duration = last_step.metadata.duration_seconds
else:
step_duration = 0
# Show total duration
total_duration = self.agent.history.total_duration_seconds()
if total_duration > 0:
model_info.write(f'[white]Total Duration:[/] [magenta]{total_duration:.2f}s[/]')
# Calculate response time metrics
model_info.write(f'[white]Last Step Duration:[/] [magenta]{step_duration:.2f}s[/]')
# Add current state information
if hasattr(self.agent, 'running'):
if getattr(self.agent, 'running', False):
model_info.write('[yellow]LLM is thinking[blink]...[/][/]')
elif hasattr(self.agent, 'state') and hasattr(self.agent.state, 'paused') and self.agent.state.paused:
model_info.write('[orange]LLM paused[/]')
else:
model_info.write('[red]Model not initialized[/]')
def update_tasks_panel(self) -> None:
"""Update tasks information panel with details about the tasks and steps hierarchy."""
tasks_info = self.query_one('#tasks-info', RichLog)
tasks_info.clear()
if self.agent:
# Check if agent has tasks
task_history = []
message_history = []
# Try to extract tasks by looking at message history
if hasattr(self.agent, '_message_manager') and self.agent._message_manager:
message_history = self.agent._message_manager.state.history.get_messages()
# Extract original task(s)
original_tasks = []
for msg in message_history:
if hasattr(msg, 'content'):
content = msg.content
if isinstance(content, str) and 'Your ultimate task is:' in content:
task_text = content.split('"""')[1].strip()
original_tasks.append(task_text)
if original_tasks:
tasks_info.write('[bold green]TASK:[/]')
for i, task in enumerate(original_tasks, 1):
# Only show latest task if multiple task changes occurred
if i == len(original_tasks):
tasks_info.write(f'[white]{task}[/]')
tasks_info.write('')
# Get current state information
current_step = self.agent.state.n_steps if hasattr(self.agent, 'state') else 0
# Get all agent history items
history_items = []
if hasattr(self.agent, 'state') and hasattr(self.agent.state, 'history'):
history_items = self.agent.history.history
if history_items:
tasks_info.write('[bold yellow]STEPS:[/]')
for idx, item in enumerate(history_items, 1):
# Determine step status
step_style = '[green]β[/]'
# For the current step, show it as in progress
if idx == current_step:
step_style = '[yellow]β³[/]'
# Check if this step had an error
if item.result and any(result.error for result in item.result):
step_style = '[red]β[/]'
# Show step number
tasks_info.write(f'{step_style} Step {idx}/{current_step}')
# Show goal if available
if item.model_output and hasattr(item.model_output, 'current_state'):
# Show goal for this step
goal = item.model_output.current_state.next_goal
if goal:
# Take just the first line for display
goal_lines = goal.strip().split('\n')
goal_summary = goal_lines[0]
tasks_info.write(f' [cyan]Goal:[/] {goal_summary}')
# Show evaluation of previous goal (feedback)
eval_prev = item.model_output.current_state.evaluation_previous_goal
if eval_prev and idx > 1: # Only show for steps after the first
eval_lines = eval_prev.strip().split('\n')
eval_summary = eval_lines[0]
eval_summary = eval_summary.replace('Success', 'β
').replace('Failed', 'β ').strip()
tasks_info.write(f' [tan]Evaluation:[/] {eval_summary}')
# Show actions taken in this step
if item.model_output and item.model_output.action:
tasks_info.write(' [purple]Actions:[/]')
for action_idx, action in enumerate(item.model_output.action, 1):
action_type = action.__class__.__name__
if hasattr(action, 'model_dump'):
# For proper actions, show the action type
action_dict = action.model_dump(exclude_unset=True)
if action_dict:
action_name = list(action_dict.keys())[0]
tasks_info.write(f' {action_idx}. [blue]{action_name}[/]')
# Show results or errors from this step
if item.result:
for result in item.result:
if result.error:
error_text = result.error
tasks_info.write(f' [red]Error:[/] {error_text}')
elif result.extracted_content:
content = result.extracted_content
tasks_info.write(f' [green]Result:[/] {content}')
# Add a space between steps for readability
tasks_info.write('')
# If agent is actively running, show a status indicator
if hasattr(self.agent, 'running') and getattr(self.agent, 'running', False):
tasks_info.write('[yellow]Agent is actively working[blink]...[/][/]')
elif hasattr(self.agent, 'state') and hasattr(self.agent.state, 'paused') and self.agent.state.paused:
tasks_info.write('[orange]Agent is paused (press Enter to resume)[/]')
else:
tasks_info.write('[dim]Agent not initialized[/]')
# Force scroll to bottom
tasks_panel = self.query_one('#tasks-panel')
tasks_panel.scroll_end(animate=False)
async def run_prompt_mode(prompt: str, ctx: click.Context, debug: bool = False):
"""Run browser-use in non-interactive mode with a single prompt."""
# Import and call setup_logging to ensure proper initialization
from browser_use.logging_config import setup_logging
# Set up logging to only show results by default
os.environ['BROWSER_USE_LOGGING_LEVEL'] = 'result'
# Re-run setup_logging to apply the new log level
setup_logging()
# The logging is now properly configured by setup_logging()
# No need to manually configure handlers since setup_logging() handles it
# Initialize telemetry
telemetry = ProductTelemetry()
start_time = time.time()
error_msg = None
try:
# Load config
config = load_user_config()
config = update_config_with_click_args(config, ctx)
# Get LLM
llm = get_llm(config)
# Capture telemetry for CLI start in oneshot mode
telemetry.capture(
CLITelemetryEvent(
version=get_browser_use_version(),
action='start',
mode='oneshot',
model=llm.model if hasattr(llm, 'model') else None,
model_provider=llm.__class__.__name__ if llm else None,
)
)
# Get agent settings from config
agent_settings = AgentSettings.model_validate(config.get('agent', {}))
# Create browser session with config parameters
browser_config = config.get('browser', {})
# Remove None values from browser_config
browser_config = {k: v for k, v in browser_config.items() if v is not None}
# Create BrowserProfile with user_data_dir
profile = BrowserProfile(user_data_dir=str(USER_DATA_DIR), **browser_config)
browser_session = BrowserSession(
browser_profile=profile,
)
# Create and run agent
agent = Agent(
task=prompt,
llm=llm,
browser_session=browser_session,
source='cli',
**agent_settings.model_dump(),
)
await agent.run()
# Ensure the browser session is fully stopped
# The agent's close() method only kills the browser if keep_alive=False,
# but we need to ensure all background tasks are stopped regardless
if browser_session:
try:
# Kill the browser session to stop all background tasks
await browser_session.kill()
except Exception:
# Ignore errors during cleanup
pass
# Capture telemetry for successful completion
telemetry.capture(
CLITelemetryEvent(
version=get_browser_use_version(),
action='task_completed',
mode='oneshot',
model=llm.model if hasattr(llm, 'model') else None,
model_provider=llm.__class__.__name__ if llm else None,
duration_seconds=time.time() - start_time,
)
)
except Exception as e:
error_msg = str(e)
# Capture telemetry for error
telemetry.capture(
CLITelemetryEvent(
version=get_browser_use_version(),
action='error',
mode='oneshot',
model=llm.model if hasattr(llm, 'model') else None,
model_provider=llm.__class__.__name__ if llm and 'llm' in locals() else None,
duration_seconds=time.time() - start_time,
error_message=error_msg,
)
)
if debug:
import traceback
traceback.print_exc()
else:
print(f'Error: {str(e)}', file=sys.stderr)
sys.exit(1)
finally:
# Ensure telemetry is flushed
telemetry.flush()
# Give a brief moment for cleanup to complete
await asyncio.sleep(0.1)
# Cancel any remaining tasks to ensure clean exit
tasks = [t for t in asyncio.all_tasks() if t != asyncio.current_task()]
for task in tasks:
task.cancel()
# Wait for all tasks to be cancelled
if tasks:
await asyncio.gather(*tasks, return_exceptions=True)
async def textual_interface(config: dict[str, Any]):
"""Run the Textual interface."""
# Prevent browser_use from setting up logging at import time
os.environ['BROWSER_USE_SETUP_LOGGING'] = 'false'
logger = logging.getLogger('browser_use.startup')
# Set up logging for Textual UI - prevent any logging to stdout
def setup_textual_logging():
# Replace all handlers with null handler
root_logger = logging.getLogger()
for handler in root_logger.handlers:
root_logger.removeHandler(handler)
# Add null handler to ensure no output to stdout/stderr
null_handler = logging.NullHandler()
root_logger.addHandler(null_handler)
logger.debug('Logging configured for Textual UI')
logger.debug('Setting up Browser, Controller, and LLM...')
# Step 1: Initialize BrowserSession with config
logger.debug('Initializing BrowserSession...')
try:
# Get browser config from the config dict
browser_config = config.get('browser', {})
logger.info('Browser type: chromium') # BrowserSession only supports chromium
if browser_config.get('executable_path'):
logger.info(f'Browser binary: {browser_config["executable_path"]}')
if browser_config.get('headless'):
logger.info('Browser mode: headless')
else:
logger.info('Browser mode: visible')
# Create BrowserSession directly with config parameters
# Remove None values from browser_config
browser_config = {k: v for k, v in browser_config.items() if v is not None}
# Create BrowserProfile with user_data_dir
profile = BrowserProfile(user_data_dir=str(USER_DATA_DIR), **browser_config)
browser_session = BrowserSession(
browser_profile=profile,
)
logger.debug('BrowserSession initialized successfully')
# Set up FIFO logging pipes for streaming logs to UI
try:
from browser_use.logging_config import setup_log_pipes
setup_log_pipes(session_id=browser_session.id)
logger.debug(f'FIFO logging pipes set up for session {browser_session.id[-4:]}')
except Exception as e:
logger.debug(f'Could not set up FIFO logging pipes: {e}')
# Browser version logging not available with CDP implementation
except Exception as e:
logger.error(f'Error initializing BrowserSession: {str(e)}', exc_info=True)
raise RuntimeError(f'Failed to initialize BrowserSession: {str(e)}')
# Step 3: Initialize Controller
logger.debug('Initializing Controller...')
try:
controller = Controller()
logger.debug('Controller initialized successfully')
except Exception as e:
logger.error(f'Error initializing Controller: {str(e)}', exc_info=True)
raise RuntimeError(f'Failed to initialize Controller: {str(e)}')
# Step 4: Get LLM
logger.debug('Getting LLM...')
try:
# Ensure setup_logging is not called when importing modules
os.environ['BROWSER_USE_SETUP_LOGGING'] = 'false'
llm = get_llm(config)
# Log LLM details
model_name = getattr(llm, 'model_name', None) or getattr(llm, 'model', 'Unknown model')
provider = llm.__class__.__name__
temperature = getattr(llm, 'temperature', 0.0)
logger.info(f'LLM: {provider} ({model_name}), temperature: {temperature}')
logger.debug(f'LLM initialized successfully: {provider}')
except Exception as e:
logger.error(f'Error getting LLM: {str(e)}', exc_info=True)
raise RuntimeError(f'Failed to initialize LLM: {str(e)}')
logger.debug('Initializing BrowserUseApp instance...')
try:
app = BrowserUseApp(config)
# Pass the initialized components to the app
app.browser_session = browser_session
app.controller = controller
app.llm = llm
# Set up event bus listener now that browser session is available
# Note: This needs to be called before run_async() but after browser_session is set
# We'll defer this to on_mount() since it needs the widgets to be available
# Configure logging for Textual UI before going fullscreen
setup_textual_logging()
# Log browser and model configuration that will be used
browser_type = 'Chromium' # BrowserSession only supports Chromium
model_name = config.get('model', {}).get('name', 'auto-detected')
headless = config.get('browser', {}).get('headless', False)
headless_str = 'headless' if headless else 'visible'
logger.info(f'Preparing {browser_type} browser ({headless_str}) with {model_name} LLM')
logger.debug('Starting Textual app with run_async()...')
# No more logging after this point as we're in fullscreen mode
await app.run_async()
except Exception as e:
logger.error(f'Error in textual_interface: {str(e)}', exc_info=True)
# Note: We don't close the browser session here to avoid duplicate stop() calls
# The browser session will be cleaned up by its __del__ method if needed
raise
async def run_auth_command():
"""Run the authentication command with dummy task in UI."""
import asyncio
import os
from browser_use.sync.auth import DeviceAuthClient
print('π Browser Use Cloud Authentication')
print('=' * 40)
# Ensure cloud sync is enabled (should be default, but make sure)
os.environ['BROWSER_USE_CLOUD_SYNC'] = 'true'
auth_client = DeviceAuthClient()
print('π Debug: Checking authentication status...')
print(f' API Token: {"β
Present" if auth_client.api_token else "β Missing"}')
print(f' User ID: {auth_client.user_id}')
print(f' Is Authenticated: {auth_client.is_authenticated}')
if auth_client.auth_config.authorized_at:
print(f' Authorized at: {auth_client.auth_config.authorized_at}')
print()
# Check if already authenticated
if auth_client.is_authenticated:
print('β
Already authenticated!')
print(f' User ID: {auth_client.user_id}')
print(f' Authenticated at: {auth_client.auth_config.authorized_at}')
# Show cloud URL if possible
frontend_url = CONFIG.BROWSER_USE_CLOUD_UI_URL or auth_client.base_url.replace('//api.', '//cloud.')
print(f'\nπ View your runs at: {frontend_url}')
return
print('π Starting authentication flow...')
print(' This will open a browser window for you to sign in.')
print()
# Initialize variables for exception handling
task_id = None
sync_service = None
try:
# Create authentication flow with dummy task
from uuid_extensions import uuid7str
from browser_use.agent.cloud_events import (
CreateAgentSessionEvent,
CreateAgentStepEvent,
CreateAgentTaskEvent,
UpdateAgentTaskEvent,
)
from browser_use.sync.service import CloudSync
# IDs for our session and task
session_id = uuid7str()
task_id = uuid7str()
# Create special sync service that allows auth events
sync_service = CloudSync(allow_session_events_for_auth=True)
sync_service.set_auth_flow_active() # Explicitly enable auth flow
sync_service.session_id = session_id # Set session ID for auth context
sync_service.auth_client = auth_client # Use the same auth client instance!
# 1. Create session (like main branch does at start)
session_event = CreateAgentSessionEvent(
id=session_id,
user_id=auth_client.temp_user_id,
browser_session_id=uuid7str(),
browser_session_live_url='',
browser_session_cdp_url='',
device_id=auth_client.device_id,
browser_state={
'viewport': {'width': 1280, 'height': 720},
'user_agent': None,
'headless': True,
'initial_url': None,
'final_url': None,
'total_pages_visited': 0,
'session_duration_seconds': 0,
},
browser_session_data={
'cookies': [],
'secrets': {},
'allowed_domains': [],
},
)
await sync_service.handle_event(session_event)
# Brief delay to ensure session is created in backend before sending task
await asyncio.sleep(0.5)
# 2. Create task (like main branch does at start)
task_event = CreateAgentTaskEvent(
id=task_id,
agent_session_id=session_id,
llm_model='auth-flow',
task='π Complete authentication and join the browser-use community',
user_id=auth_client.temp_user_id,
device_id=auth_client.device_id,
done_output=None,
user_feedback_type=None,
user_comment=None,
gif_url=None,
)
await sync_service.handle_event(task_event)
# Longer delay to ensure task is created in backend before sending step event
await asyncio.sleep(1.0)
# 3. Run authentication with timeout
print('β³ Waiting for authentication... (this may take up to 2 minutes for testing)')
print(' Complete the authentication in your browser, then this will continue automatically.')
print()
try:
print('π§ Debug: Starting authentication process...')
print(f' Original auth client authenticated: {auth_client.is_authenticated}')
print(f' Sync service auth client authenticated: {sync_service.auth_client.is_authenticated}')
print(f' Same auth client? {auth_client is sync_service.auth_client}')
print(f' Session ID: {sync_service.session_id}')
# Create a task to show periodic status updates
async def show_auth_progress():
for i in range(1, 25): # Show updates every 5 seconds for 2 minutes
await asyncio.sleep(5)
fresh_check = DeviceAuthClient()
print(f'β±οΈ Waiting for authentication... ({i * 5}s elapsed)')
print(f' Status: {"β
Authenticated" if fresh_check.is_authenticated else "β³ Still waiting"}')
if fresh_check.is_authenticated:
print('π Authentication detected! Completing...')
break
# Run authentication and progress updates concurrently
auth_start_time = asyncio.get_event_loop().time()
auth_task = asyncio.create_task(sync_service.authenticate(show_instructions=True))
progress_task = asyncio.create_task(show_auth_progress())
# Wait for authentication to complete, with timeout
success = await asyncio.wait_for(auth_task, timeout=120.0) # 2 minutes for initial testing
progress_task.cancel() # Stop the progress updates
auth_duration = asyncio.get_event_loop().time() - auth_start_time
print(f'π§ Debug: Authentication returned: {success} (took {auth_duration:.1f}s)')
except TimeoutError:
print('β±οΈ Authentication timed out after 2 minutes.')
print(' Checking if authentication completed in background...')
# Create a fresh auth client to check current status
fresh_auth_client = DeviceAuthClient()
print('π§ Debug: Fresh auth client check:')
print(f' API Token: {"β
Present" if fresh_auth_client.api_token else "β Missing"}')
print(f' Is Authenticated: {fresh_auth_client.is_authenticated}')
if fresh_auth_client.is_authenticated:
print('β
Authentication was successful!')
success = True
# Update the sync service's auth client
sync_service.auth_client = fresh_auth_client
else:
print('β Authentication not completed. Please try again.')
success = False
except Exception as e:
print(f'β Authentication error: {type(e).__name__}: {e}')
import traceback
print(f'π Full traceback: {traceback.format_exc()}')
success = False
if success:
# 4. Send step event to show progress (like main branch during execution)
# Use the sync service's auth client which has the updated user_id
step_event = CreateAgentStepEvent(
# Remove explicit ID - let it auto-generate to avoid backend validation issues
user_id=auth_client.temp_user_id, # Use same temp user_id as task for consistency
device_id=auth_client.device_id, # Use consistent device_id
agent_task_id=task_id,
step=1,
actions=[
{
'click': {
'coordinate': [800, 400],
'description': 'Click on Star button',
'success': True,
},
'done': {
'success': True,
'text': 'β Starred browser-use/browser-use repository! Welcome to the community!',
},
}
],
next_goal='β Star browser-use GitHub repository to join the community',
evaluation_previous_goal='Authentication completed successfully',
memory='User authenticated with Browser Use Cloud and is now part of the community',
screenshot_url=None,
url='https://github.com/browser-use/browser-use',
)
print('π€ Sending dummy step event...')
await sync_service.handle_event(step_event)
# Small delay to ensure step is processed before completion
await asyncio.sleep(0.5)
# 5. Complete task (like main branch does at end)
completion_event = UpdateAgentTaskEvent(
id=task_id,
user_id=auth_client.temp_user_id, # Use same temp user_id as task for consistency
device_id=auth_client.device_id, # Use consistent device_id
done_output="π Welcome to Browser Use! You're now authenticated and part of our community. β Your future tasks will sync to the cloud automatically.",
user_feedback_type=None,
user_comment=None,
gif_url=None,
)
await sync_service.handle_event(completion_event)
print('π Authentication successful!')
print(' Future browser-use runs will now sync to the cloud.')
else:
# Failed - still complete the task with failure message
completion_event = UpdateAgentTaskEvent(
id=task_id,
user_id=auth_client.temp_user_id, # Still temp user since auth failed
device_id=auth_client.device_id,
done_output='β Authentication failed. Please try again.',
user_feedback_type=None,
user_comment=None,
gif_url=None,
)
await sync_service.handle_event(completion_event)
print('β Authentication failed.')
print(' Please try again or check your internet connection.')
except Exception as e:
print(f'β Authentication error: {e}')
# Still try to complete the task in UI with error message
if task_id and sync_service:
try:
from browser_use.agent.cloud_events import UpdateAgentTaskEvent
completion_event = UpdateAgentTaskEvent(
id=task_id,
user_id=auth_client.temp_user_id,
device_id=auth_client.device_id,
done_output=f'β Authentication error: {e}',
user_feedback_type=None,
user_comment=None,
gif_url=None,
)
await sync_service.handle_event(completion_event)
except Exception:
pass # Don't fail if we can't send the error event
sys.exit(1)
@click.group(invoke_without_command=True)
@click.option('--version', is_flag=True, help='Print version and exit')
@click.option(
'--template',
type=click.Choice(['default', 'advanced', 'tools'], case_sensitive=False),
help='Generate a template file (default, advanced, or tools)',
)
@click.option('--output', '-o', type=click.Path(), help='Output file path for template (default: browser_use_<template>.py)')
@click.option('--force', '-f', is_flag=True, help='Overwrite existing files without asking')
@click.option('--model', type=str, help='Model to use (e.g., gpt-5-mini, claude-4-sonnet, gemini-2.5-flash)')
@click.option('--debug', is_flag=True, help='Enable verbose startup logging')
@click.option('--headless', is_flag=True, help='Run browser in headless mode', default=None)
@click.option('--window-width', type=int, help='Browser window width')
@click.option('--window-height', type=int, help='Browser window height')
@click.option(
'--user-data-dir', type=str, help='Path to Chrome user data directory (e.g. ~/Library/Application Support/Google/Chrome)'
)
@click.option('--profile-directory', type=str, help='Chrome profile directory name (e.g. "Default", "Profile 1")')
@click.option('--cdp-url', type=str, help='Connect to existing Chrome via CDP URL (e.g. http://localhost:9222)')
@click.option('--proxy-url', type=str, help='Proxy server for Chromium traffic (e.g. http://host:8080 or socks5://host:1080)')
@click.option('--no-proxy', type=str, help='Comma-separated hosts to bypass proxy (e.g. localhost,127.0.0.1,*.internal)')
@click.option('--proxy-username', type=str, help='Proxy auth username')
@click.option('--proxy-password', type=str, help='Proxy auth password')
@click.option('-p', '--prompt', type=str, help='Run a single task without the TUI (headless mode)')
@click.option('--mcp', is_flag=True, help='Run as MCP server (exposes JSON RPC via stdin/stdout)')
@click.pass_context
def main(ctx: click.Context, debug: bool = False, **kwargs):
"""Browser Use - AI Agent for Web Automation
Run without arguments to start the interactive TUI.
Examples:
uvx browser-use --template default
uvx browser-use --template advanced --output my_script.py
"""
# Handle template generation
if kwargs.get('template'):
_run_template_generation(kwargs['template'], kwargs.get('output'), kwargs.get('force', False))
return
if ctx.invoked_subcommand is None:
# No subcommand, run the main interface
run_main_interface(ctx, debug, **kwargs)
def run_main_interface(ctx: click.Context, debug: bool = False, **kwargs):
"""Run the main browser-use interface"""
if kwargs['version']:
from importlib.metadata import version
print(version('browser-use'))
sys.exit(0)
# Check if MCP server mode is activated
if kwargs.get('mcp'):
# Capture telemetry for MCP server mode via CLI (suppress any logging from this)
try:
telemetry = ProductTelemetry()
telemetry.capture(
CLITelemetryEvent(
version=get_browser_use_version(),
action='start',
mode='mcp_server',
)
)
except Exception:
# Ignore telemetry errors in MCP mode to prevent any stdout contamination
pass
# Run as MCP server
from browser_use.mcp.server import main as mcp_main
asyncio.run(mcp_main())
return
# Check if prompt mode is activated
if kwargs.get('prompt'):
# Set environment variable for prompt mode before running
os.environ['BROWSER_USE_LOGGING_LEVEL'] = 'result'
# Run in non-interactive mode
asyncio.run(run_prompt_mode(kwargs['prompt'], ctx, debug))
return
# Configure console logging
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s', '%H:%M:%S'))
# Configure root logger
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO if not debug else logging.DEBUG)
root_logger.addHandler(console_handler)
logger = logging.getLogger('browser_use.startup')
logger.info('Starting Browser-Use initialization')
if debug:
logger.debug(f'System info: Python {sys.version.split()[0]}, Platform: {sys.platform}')
logger.debug('Loading environment variables from .env file...')
load_dotenv()
logger.debug('Environment variables loaded')
# Load user configuration
logger.debug('Loading user configuration...')
try:
config = load_user_config()
logger.debug(f'User configuration loaded from {CONFIG.BROWSER_USE_CONFIG_FILE}')
except Exception as e:
logger.error(f'Error loading user configuration: {str(e)}', exc_info=True)
print(f'Error loading configuration: {str(e)}')
sys.exit(1)
# Update config with command-line arguments
logger.debug('Updating configuration with command line arguments...')
try:
config = update_config_with_click_args(config, ctx)
logger.debug('Configuration updated')
except Exception as e:
logger.error(f'Error updating config with command line args: {str(e)}', exc_info=True)
print(f'Error updating configuration: {str(e)}')
sys.exit(1)
# Save updated config
logger.debug('Saving user configuration...')
try:
save_user_config(config)
logger.debug('Configuration saved')
except Exception as e:
logger.error(f'Error saving user configuration: {str(e)}', exc_info=True)
print(f'Error saving configuration: {str(e)}')
sys.exit(1)
# Setup handlers for console output before entering Textual UI
logger.debug('Setting up handlers for Textual UI...')
# Log browser and model configuration that will be used
browser_type = 'Chromium' # BrowserSession only supports Chromium
model_name = config.get('model', {}).get('name', 'auto-detected')
headless = config.get('browser', {}).get('headless', False)
headless_str = 'headless' if headless else 'visible'
logger.info(f'Preparing {browser_type} browser ({headless_str}) with {model_name} LLM')
try:
# Run the Textual UI interface - now all the initialization happens before we go fullscreen
logger.debug('Starting Textual UI interface...')
asyncio.run(textual_interface(config))
except Exception as e:
# Restore console logging for error reporting
root_logger.setLevel(logging.INFO)
for handler in root_logger.handlers:
root_logger.removeHandler(handler)
root_logger.addHandler(console_handler)
logger.error(f'Error initializing Browser-Use: {str(e)}', exc_info=debug)
print(f'\nError launching Browser-Use: {str(e)}')
if debug:
import traceback
traceback.print_exc()
sys.exit(1)
@main.command()
def auth():
"""Authenticate with Browser Use Cloud to sync your runs"""
asyncio.run(run_auth_command())
@main.command()
def install():
"""Install Chromium browser with system dependencies"""
import platform
import subprocess
print('π¦ Installing Chromium browser + system dependencies...')
print('β³ This may take a few minutes...\n')
# Build command - only use --with-deps on Linux (it fails on Windows/macOS)
cmd = ['uvx', 'playwright', 'install', 'chromium']
if platform.system() == 'Linux':
cmd.append('--with-deps')
cmd.append('--no-shell')
result = subprocess.run(cmd)
if result.returncode == 0:
print('\nβ
Installation complete!')
print('π Ready to use! Run: uvx browser-use')
else:
print('\nβ Installation failed')
sys.exit(1)
# ============================================================================
# Template Generation - Generate template files
# ============================================================================
# Template metadata
INIT_TEMPLATES = {
'default': {
'file': 'default_template.py',
'description': 'Simplest setup - capable of any web task with minimal configuration',
},
'advanced': {
'file': 'advanced_template.py',
'description': 'All configuration options shown with defaults',
},
'tools': {
'file': 'tools_template.py',
'description': 'Custom action examples - extend the agent with your own functions',
},
}
def _run_template_generation(template: str, output: str | None, force: bool):
"""Generate a template file (called from main CLI)."""
# Determine output path
if output:
output_path = Path(output)
else:
output_path = Path.cwd() / f'browser_use_{template}.py'
# Read template file
try:
templates_dir = Path(__file__).parent / 'cli_templates'
template_file = INIT_TEMPLATES[template]['file']
template_path = templates_dir / template_file
content = template_path.read_text(encoding='utf-8')
except Exception as e:
click.echo(f'β Error reading template: {e}', err=True)
sys.exit(1)
# Write file
if _write_init_file(output_path, content, force):
click.echo(f'β
Created {output_path}')
click.echo('\nNext steps:')
click.echo(' 1. Install browser-use:')
click.echo(' uv pip install browser-use')
click.echo(' 2. Set up your API key in .env file or environment:')
click.echo(' BROWSER_USE_API_KEY=your-key')
click.echo(' (Get your key at https://cloud.browser-use.com/new-api-key)')
click.echo(' 3. Run your script:')
click.echo(f' python {output_path.name}')
else:
sys.exit(1)
def _write_init_file(output_path: Path, content: str, force: bool = False) -> bool:
"""Write content to a file, with safety checks."""
# Check if file already exists
if output_path.exists() and not force:
click.echo(f'β οΈ File already exists: {output_path}')
if not click.confirm('Overwrite?', default=False):
click.echo('β Cancelled')
return False
# Ensure parent directory exists
output_path.parent.mkdir(parents=True, exist_ok=True)
# Write file
try:
output_path.write_text(content, encoding='utf-8')
return True
except Exception as e:
click.echo(f'β Error writing file: {e}', err=True)
return False
@main.command('init')
@click.option(
'--template',
'-t',
type=click.Choice(['default', 'advanced', 'tools'], case_sensitive=False),
help='Template to use',
)
@click.option(
'--output',
'-o',
type=click.Path(),
help='Output file path (default: browser_use_<template>.py)',
)
@click.option(
'--force',
'-f',
is_flag=True,
help='Overwrite existing files without asking',
)
@click.option(
'--list',
'-l',
'list_templates',
is_flag=True,
help='List available templates',
)
def init(
template: str | None,
output: str | None,
force: bool,
list_templates: bool,
):
"""
Generate a browser-use template file to get started quickly.
Examples:
\b
# Interactive mode - prompts for template selection
uvx browser-use init
\b
# Generate default template
uvx browser-use init --template default
\b
# Generate advanced template with custom filename
uvx browser-use init --template advanced --output my_script.py
\b
# List available templates
uvx browser-use init --list
"""
# Handle --list flag
if list_templates:
click.echo('Available templates:\n')
for name, info in INIT_TEMPLATES.items():
click.echo(f' {name:12} - {info["description"]}')
return
# Interactive template selection if not provided
if not template:
click.echo('Available templates:\n')
for name, info in INIT_TEMPLATES.items():
click.echo(f' {name:12} - {info["description"]}')
click.echo()
template = click.prompt(
'Which template would you like to use?',
type=click.Choice(['default', 'advanced', 'tools'], case_sensitive=False),
default='default',
)
# Template is guaranteed to be set at this point (either from option or prompt)
assert template is not None
# Determine output path
if output:
output_path = Path(output)
else:
output_path = Path.cwd() / f'browser_use_{template}.py'
# Read template file
try:
templates_dir = Path(__file__).parent / 'cli_templates'
template_file = INIT_TEMPLATES[template]['file']
template_path = templates_dir / template_file
content = template_path.read_text(encoding='utf-8')
except Exception as e:
click.echo(f'β Error reading template: {e}', err=True)
sys.exit(1)
# Write file
if _write_init_file(output_path, content, force):
click.echo(f'β
Created {output_path}')
click.echo('\nNext steps:')
click.echo(' 1. Install browser-use:')
click.echo(' uv pip install browser-use')
click.echo(' 2. Set up your API key in .env file or environment:')
click.echo(' BROWSER_USE_API_KEY=your-key')
click.echo(' (Get your key at https://cloud.browser-use.com/new-api-key)')
click.echo(' 3. Run your script:')
click.echo(f' python {output_path.name}')
else:
sys.exit(1)
if __name__ == '__main__':
main()
|