def app_open(app_name: str) -> Dict[str, Any]:
"""
Open an app in the default web browser. If the app is not currently being served,
it will be served first.
Can only open one app at a time.
Args:
app_name: Name of the application to open
Returns:
A dictionary containing the result of the operation
"""
global http_server
try:
# Find the app directory
app_path = os.path.join(APP_DIR, app_name)
if not os.path.exists(app_path):
return {
"success": False,
"error": f"App '{app_name}' not found at {app_path}"
}
# If the server is not running, start it
if not http_server:
serve_result = app_serve(app_name)
if not serve_result["success"]:
return serve_result
# Get the URL from the serve result
url = serve_result["url"]
else:
# Use the current server port
url = f"http://localhost:{server_port}"
# Check if we're on macOS
if os.uname().sysname == "Darwin": # macOS
# Use Chrome in app mode
chrome_path = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
if os.path.exists(chrome_path):
logger.info(f"Opening app in Chrome app mode: {url}")
# Use Popen instead of run to avoid blocking
subprocess.Popen([chrome_path, f"--app={url}"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
else:
# Fallback to default browser if Chrome is not installed
logger.info(f"Chrome not found, opening in default browser: {url}")
# Use Popen instead of run to avoid blocking
subprocess.Popen(["open", url],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
else:
# For non-macOS systems, use the default browser
# Use Popen instead of run to avoid blocking
subprocess.Popen(["open", url],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
return {
"success": True,
"app_name": app_name,
"url": url,
"message": f"App '{app_name}' opened in browser at {url}"
}
except Exception as e:
logger.error(f"Error opening app: {e}")
return {"success": False, "error": f"Failed to open app: {str(e)}"}