fix: State command now outputs LLM representation, add CLI aliases

- State command outputs same format that browser-use agents see
- Added aliases: bu, browser (all work the same)
- Headed mode now correctly shows browser window
This commit is contained in:
Gregor Žunič
2026-01-19 09:32:26 -08:00
parent 0f9e955276
commit 4716ae50ce
3 changed files with 14 additions and 27 deletions

View File

@@ -119,28 +119,9 @@ async def handle(action: str, session: SessionInfo, params: dict[str, Any]) -> A
return {'screenshot': base64.b64encode(data).decode(), 'size': len(data)}
elif action == 'state':
state = await bs.get_browser_state_summary(include_screenshot=False)
if state:
# Build element list with indices from DOM state
elements = []
if state.dom_state and state.dom_state.selector_map:
for idx, elem in state.dom_state.selector_map.items():
elements.append(
{
'index': idx,
'tag': getattr(elem, 'tag_name', ''),
'text': (getattr(elem, 'text', '') or '')[:50],
'role': getattr(elem, 'role', ''),
}
)
return {
'url': state.url,
'title': state.title,
'element_count': len(elements),
'elements': elements[:20], # Return first 20 elements
}
return {'url': '', 'title': '', 'elements': []}
# Return the same LLM representation that browser-use agents see
state_text = await bs.get_state_as_text()
return {'_raw_text': state_text}
elif action == 'switch':
from browser_use.browser.events import SwitchTabEvent

View File

@@ -390,11 +390,15 @@ def main() -> int:
data = response.get('data')
if data is not None:
if isinstance(data, dict):
for key, value in data.items():
if key == 'screenshot' and len(str(value)) > 100:
print(f'{key}: <{len(value)} bytes>')
else:
print(f'{key}: {value}')
# Special case: raw text output (e.g., state command)
if '_raw_text' in data:
print(data['_raw_text'])
else:
for key, value in data.items():
if key == 'screenshot' and len(str(value)) > 100:
print(f'{key}: <{len(value)} bytes>')
else:
print(f'{key}: {value}')
elif isinstance(data, str):
print(data)
else:

View File

@@ -93,6 +93,8 @@ Repository = "https://github.com/browser-use/browser-use"
[project.scripts]
browser-use = "browser_use.skill_cli.main:main" # Fast CLI for browser automation
bu = "browser_use.skill_cli.main:main" # Alias for browser-use
browser = "browser_use.skill_cli.main:main" # Alias for browser-use
browser-use-tui = "browser_use.cli:main" # Legacy TUI interface
[build-system]