|
| 1 | +"""Server subcommand for starting the Code Execution Server.""" |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +import click |
| 6 | + |
| 7 | +from taskweaver.cli.util import CliContext, require_workspace |
| 8 | + |
| 9 | + |
| 10 | +@click.command() |
| 11 | +@require_workspace() |
| 12 | +@click.pass_context |
| 13 | +@click.option( |
| 14 | + "--host", |
| 15 | + type=str, |
| 16 | + default=None, |
| 17 | + help="Host to bind to (default: localhost)", |
| 18 | +) |
| 19 | +@click.option( |
| 20 | + "--port", |
| 21 | + type=int, |
| 22 | + default=None, |
| 23 | + help="Port to bind to (default: 8000)", |
| 24 | +) |
| 25 | +@click.option( |
| 26 | + "--api-key", |
| 27 | + type=str, |
| 28 | + default=None, |
| 29 | + help="API key for authentication (optional for localhost)", |
| 30 | +) |
| 31 | +@click.option( |
| 32 | + "--log-level", |
| 33 | + type=click.Choice(["debug", "info", "warning", "error", "critical"]), |
| 34 | + default="info", |
| 35 | + help="Log level (default: info)", |
| 36 | +) |
| 37 | +@click.option( |
| 38 | + "--reload", |
| 39 | + is_flag=True, |
| 40 | + default=False, |
| 41 | + help="Enable auto-reload for development", |
| 42 | +) |
| 43 | +def server( |
| 44 | + ctx: click.Context, |
| 45 | + host: str, |
| 46 | + port: int, |
| 47 | + api_key: str, |
| 48 | + log_level: str, |
| 49 | + reload: bool, |
| 50 | +): |
| 51 | + """Start the Code Execution Server. |
| 52 | +
|
| 53 | + The server handles code execution requests from TaskWeaver sessions. |
| 54 | + Start this server first, then run 'taskweaver chat' in another terminal. |
| 55 | +
|
| 56 | + \b |
| 57 | + Example: |
| 58 | + taskweaver -p ./project server --port 8000 |
| 59 | + taskweaver -p ./project chat --server-url http://localhost:8000 |
| 60 | + """ |
| 61 | + ctx_obj: CliContext = ctx.obj |
| 62 | + workspace = ctx_obj.workspace |
| 63 | + assert workspace is not None |
| 64 | + |
| 65 | + from taskweaver.config.config_mgt import AppConfigSource |
| 66 | + |
| 67 | + app_config_file = os.path.join(workspace, "taskweaver_config.json") |
| 68 | + config_src = AppConfigSource( |
| 69 | + config_file_path=app_config_file if os.path.exists(app_config_file) else None, |
| 70 | + config={}, |
| 71 | + app_base_path=workspace, |
| 72 | + ) |
| 73 | + |
| 74 | + def get_config(key: str, default): |
| 75 | + return config_src.json_file_store.get(key, default) |
| 76 | + |
| 77 | + effective_host = host or get_config("execution_service.server.host", "localhost") |
| 78 | + effective_port = port or get_config("execution_service.server.port", 8000) |
| 79 | + effective_api_key = api_key or get_config("execution_service.server.api_key", None) |
| 80 | + work_dir = get_config( |
| 81 | + "execution_service.env_dir", |
| 82 | + os.path.join(workspace, "env"), |
| 83 | + ) |
| 84 | + |
| 85 | + os.makedirs(work_dir, exist_ok=True) |
| 86 | + |
| 87 | + os.environ["TASKWEAVER_SERVER_HOST"] = effective_host |
| 88 | + os.environ["TASKWEAVER_SERVER_PORT"] = str(effective_port) |
| 89 | + os.environ["TASKWEAVER_SERVER_WORK_DIR"] = work_dir |
| 90 | + if effective_api_key: |
| 91 | + os.environ["TASKWEAVER_SERVER_API_KEY"] = effective_api_key |
| 92 | + |
| 93 | + click.echo() |
| 94 | + click.echo("=" * 60) |
| 95 | + click.echo(" TaskWeaver Code Execution Server") |
| 96 | + click.echo("=" * 60) |
| 97 | + click.echo(f" Project: {ctx_obj.workspace}") |
| 98 | + click.echo(f" Host: {effective_host}") |
| 99 | + click.echo(f" Port: {effective_port}") |
| 100 | + click.echo(f" URL: http://{effective_host}:{effective_port}") |
| 101 | + click.echo(f" Health: http://{effective_host}:{effective_port}/api/v1/health") |
| 102 | + click.echo(f" Work Dir: {work_dir}") |
| 103 | + click.echo(f" API Key: {'configured' if effective_api_key else 'not required (localhost)'}") |
| 104 | + click.echo("=" * 60) |
| 105 | + click.echo() |
| 106 | + click.echo("To connect a chat session, run in another terminal:") |
| 107 | + click.echo(f" taskweaver -p {ctx_obj.workspace} chat --server-url http://{effective_host}:{effective_port}") |
| 108 | + click.echo() |
| 109 | + |
| 110 | + try: |
| 111 | + import uvicorn |
| 112 | + except ImportError: |
| 113 | + click.secho( |
| 114 | + "Error: uvicorn is required to run the server. " "Please install it with: pip install uvicorn", |
| 115 | + fg="red", |
| 116 | + ) |
| 117 | + raise SystemExit(1) |
| 118 | + |
| 119 | + try: |
| 120 | + import fastapi # noqa: F401 |
| 121 | + except ImportError: |
| 122 | + click.secho( |
| 123 | + "Error: fastapi is required to run the server. " "Please install it with: pip install fastapi", |
| 124 | + fg="red", |
| 125 | + ) |
| 126 | + raise SystemExit(1) |
| 127 | + |
| 128 | + uvicorn.run( |
| 129 | + "taskweaver.ces.server.app:app", |
| 130 | + host=effective_host, |
| 131 | + port=effective_port, |
| 132 | + reload=reload, |
| 133 | + log_level=log_level, |
| 134 | + ) |
0 commit comments