From 2e8fdae5ad1fb195a3f489de54a40e86733043da Mon Sep 17 00:00:00 2001 From: aaron Date: Fri, 27 Feb 2026 14:48:50 +1100 Subject: [PATCH] Update browser-agent to use create_agent factory pattern - Convert module-level agent instantiation to create_agent() function - Update main.py to pass create_agent to host() instead of agent instance - Add textual>=0.60.0 to requirements.txt (required by connectonion TUI) This follows the host.md pattern where each request gets a fresh agent instance for proper isolation. --- agent.py | 36 ++++++++++++++++++++++++------------ main.py | 4 ++-- requirements.txt | 1 + 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/agent.py b/agent.py index f47d1bb..a40d865 100644 --- a/agent.py +++ b/agent.py @@ -8,21 +8,33 @@ from connectonion import Agent from connectonion.useful_plugins import image_result_formatter, ui_stream -from tools.web_automation import web +from tools.browser import web from tools.deep_research import perform_deep_research # Load environment variables load_dotenv() -# Create the agent -system_prompt_path = Path(__file__).parent / "prompts" / "agent.md" +def create_agent(): + """Create a fresh browser agent instance. -agent = Agent( - name="browser_agent", - model=os.getenv("BROWSER_AGENT_MODEL", "co/gemini-2.5-flash"), - system_prompt=system_prompt_path, - # We pass the web instance (for direct tools) AND the deep_research function - tools=[web, perform_deep_research], - plugins=[image_result_formatter, ui_stream], - max_iterations=50 -) \ No newline at end of file + Returns a new Agent instance for each request to ensure proper isolation. + Used by host() to create agents per request. + """ + system_prompt_path = Path(__file__).parent / "prompts" / "agent.md" + + return Agent( + name="browser_agent", + model="co/gemini-2.5-pro", + system_prompt=system_prompt_path, + tools=[web, perform_deep_research], + plugins=[image_result_formatter, ui_stream], + max_iterations=200 + ) + +if __name__ == "__main__": + agent = create_agent() + agent.input("""pls use this user name and passwd to login nsw.gov.au aaronplus1996@gmail.com passwd is @HZERn5W65z$-Ez + the url is https://driver-knowledge-test.service.nsw.gov.au/ + then try to click and learning all the learning part + if any page need auth code, pls let use to use login thing to login + """) \ No newline at end of file diff --git a/main.py b/main.py index fd50347..4c808a3 100644 --- a/main.py +++ b/main.py @@ -9,11 +9,11 @@ """ import os from connectonion import host -from agent import agent +from agent import create_agent if __name__ == "__main__": trust = os.environ.get("TRUST", "open") # Default to open for local dev print(f"Starting browser-agent with trust={trust}") print("WebSocket endpoint: ws://localhost:8000/ws") print("HTTP endpoint: http://localhost:8000/input") - host(agent, trust=trust, relay_url=None) # Disable relay for local testing \ No newline at end of file + host(create_agent, trust=trust, relay_url=None) # Disable relay for local testing \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index d42b2c1..a2e1940 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ pydantic>=2.0.0 typer>=0.9.0 rich>=13.0.0 Pillow +textual>=0.60.0