MCPcopy Create free account
hub / github.com/RawdogReverend/TermNet / handle_single_command

Function handle_single_command

backend/extensions/browser_server.py:225–438  ·  view source on GitHub ↗

Handle a single command from a WebSocket connection

(websocket)

Source from the content-addressed store, hash-verified

223 }
224
225async def handle_single_command(websocket):
226 """Handle a single command from a WebSocket connection"""
227 global global_history, global_current_url, global_forms
228
229 async with browser_lock:
230 await initialize_browser()
231
232 try:
233 # Wait for the command
234 command = await asyncio.wait_for(websocket.recv(), timeout=30.0)
235 command = command.strip()
236 response = {}
237
238 logger.info(f"Processing command: {command}")
239
240 # Navigate to URL
241 if command.startswith(("url:", "go:", "open:")):
242 url = command.split(":", 1)[1].strip()
243 if not url.startswith(('http://', 'https://')):
244 url = "https://" + url
245
246 try:
247 await global_page.goto(url, wait_until='domcontentloaded', timeout=15000)
248 content = await get_structured_data(global_page, global_page.url)
249 global_current_url = global_page.url
250 global_forms = content["forms"]
251 global_history.append(global_current_url)
252 response = content
253 except Exception as e:
254 response = {"error": f"Failed to load {url}: {str(e)}"}
255
256 # Follow link by index
257 elif command.startswith("link:"):
258 try:
259 idx = int(command.split(":", 1)[1].strip())
260 current_content = await get_structured_data(global_page, global_page.url)
261 if 0 <= idx < len(current_content["links"]):
262 target_url = current_content["links"][idx]["url"]
263 await global_page.goto(target_url, wait_until='domcontentloaded', timeout=15000)
264 content = await get_structured_data(global_page, global_page.url)
265 global_current_url = global_page.url
266 global_forms = content["forms"]
267 global_history.append(global_current_url)
268 response = content
269 else:
270 response = {"error": f"Link index {idx} out of range"}
271 except (ValueError, IndexError) as e:
272 response = {"error": f"Invalid link command: {str(e)}"}
273
274 # Search on page
275 elif command.startswith("search:"):
276 search_term = command.split(":", 1)[1].strip()
277 html = await global_page.content()
278 soup = BeautifulSoup(html, "html.parser")
279 text = soup.get_text().lower()
280
281 if search_term.lower() in text:
282 # Find context around the search term

Callers 1

websocket_handlerFunction · 0.85

Calls 3

initialize_browserFunction · 0.85
get_structured_dataFunction · 0.85
clean_textFunction · 0.85

Tested by

no test coverage detected