| 53 | parser.set_defaults(func=subparser_func) |
| 54 | |
| 55 | def execute(self): |
| 56 | current_file = Path(__file__).resolve() |
| 57 | project_root = current_file.parent.parent.parent.parent |
| 58 | webui_dir = project_root / 'webui' |
| 59 | |
| 60 | if not webui_dir.exists(): |
| 61 | import ms_agent |
| 62 | ms_agent_path = Path(ms_agent.__file__).parent |
| 63 | webui_dir = ms_agent_path / 'webui' |
| 64 | |
| 65 | if not webui_dir.exists(): |
| 66 | webui_dir = Path.cwd() / 'webui' |
| 67 | |
| 68 | backend_dir = webui_dir / 'backend' |
| 69 | frontend_dir = webui_dir / 'frontend' |
| 70 | |
| 71 | if not webui_dir.exists() or not backend_dir.exists(): |
| 72 | print('Error: WebUI directory not found.') |
| 73 | sys.exit(1) |
| 74 | |
| 75 | frontend_dist = frontend_dir / 'dist' |
| 76 | frontend_built = frontend_dist.exists() and (frontend_dist |
| 77 | / 'index.html').exists() |
| 78 | |
| 79 | if self.args.production and not frontend_built: |
| 80 | print( |
| 81 | 'Error: Frontend not built. Please run "npm run build" in webui/frontend first.' |
| 82 | ) |
| 83 | sys.exit(1) |
| 84 | |
| 85 | if not self.args.production and not frontend_built: |
| 86 | if self._build_frontend(frontend_dir): |
| 87 | frontend_built = True |
| 88 | |
| 89 | browser_host = 'localhost' if self.args.host == '0.0.0.0' else self.args.host |
| 90 | browser_url = f'http://{browser_host}:{self.args.port}' |
| 91 | |
| 92 | backend_str = str(backend_dir) |
| 93 | if backend_str not in sys.path: |
| 94 | sys.path.insert(0, backend_str) |
| 95 | |
| 96 | original_argv = sys.argv |
| 97 | original_cwd = os.getcwd() |
| 98 | try: |
| 99 | os.chdir(backend_dir) |
| 100 | from main import main |
| 101 | |
| 102 | sys.argv = [ |
| 103 | 'main.py', |
| 104 | '--host', |
| 105 | self.args.host, |
| 106 | '--port', |
| 107 | str(self.args.port), |
| 108 | ] |
| 109 | if self.args.reload: |
| 110 | sys.argv.append('--reload') |
| 111 | |
| 112 | if not self.args.no_browser and frontend_built: |