-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathcli.py
More file actions
345 lines (305 loc) · 12.2 KB
/
Copy pathcli.py
File metadata and controls
345 lines (305 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import os
import warnings
import typer
import uvicorn
from typing_extensions import Annotated
from typing import Optional
from pathlib import Path
import logging
from ..version import VERSION
from .._docker import (
check_docker_running,
check_browser_image,
check_python_image,
build_browser_image,
build_python_image,
)
# Configure basic logging to show only errors
logging.basicConfig(level=logging.ERROR)
# Create a Typer application instance with a descriptive help message
# This is the main entry point for CLI commands
app = typer.Typer(help="Magentic-UI: A human-centered interface for web agents.")
# Ignore deprecation warnings from websockets
warnings.filterwarnings("ignore", message="websockets.legacy is deprecated*")
warnings.filterwarnings(
"ignore", message="websockets.server.WebSocketServerProtocol is deprecated*"
)
# Ignore warnings about ffmpeg or avconv not being found
# Audio is not used in the UI, so we can ignore this warning
warnings.filterwarnings("ignore", message="Couldn't find ffmpeg or avconv*")
def get_env_file_path():
"""
Create a temporary environment file path in the user's home directory.
Used to pass environment variables to Uvicorn workers.
Returns:
str: The full path to the temporary environment file
"""
app_dir = os.path.join(os.path.expanduser("~"), ".magentic_ui")
if not os.path.exists(app_dir):
os.makedirs(app_dir, exist_ok=True)
return os.path.join(app_dir, "temp_env_vars.env")
# This decorator makes this function the default action when no subcommand is provided
# invoke_without_command=True means this function runs automatically when only 'magentic-ui' is typed
@app.callback(invoke_without_command=True)
def main(
ctx: typer.Context, # Typer context provides information about the command invocation
host: str = typer.Option("127.0.0.1", help="Host to run the UI on."),
port: int = typer.Option(8081, help="Port to run the UI on."),
workers: int = typer.Option(1, help="Number of workers to run the UI with."),
reload: Annotated[
bool, typer.Option("--reload", help="Reload the UI on code changes.")
] = False,
docs: bool = typer.Option(True, help="Whether to generate API docs."),
appdir: str = typer.Option(
str(Path.home() / ".magentic_ui"),
help="Path to the app directory where files are stored.",
),
database_uri: Optional[str] = typer.Option(
None, "--database-uri", help="Database URI to connect to."
),
upgrade_database: bool = typer.Option(
False, "--upgrade-database", help="Upgrade the database schema on startup."
),
config: Optional[str] = typer.Option(
None, "--config", help="Path to the config file."
),
rebuild_docker: Optional[bool] = typer.Option(
False, "--rebuild-docker", help="Rebuild the docker images before starting."
),
version: bool = typer.Option(
False, "--version", help="Print the version of Magentic-UI and exit."
),
run_without_docker: Annotated[
bool,
typer.Option(
"--run-without-docker",
help="Run without docker. This will remove coder and filesurfer agents and disable live browser view.",
),
] = False,
):
"""
Magentic-UI: A human-centered interface for web agents.
Run `magentic-ui` to start the application.
"""
# Check if version flag was provided
if version:
typer.echo(f"Magentic-UI version: {VERSION}")
raise typer.Exit()
# This conditional checks if a subcommand was provided
# If no subcommand was specified (e.g., just 'magentic-ui'), run the UI
if ctx.invoked_subcommand is None:
run_ui(
host=host,
port=port,
workers=workers,
reload=reload,
docs=docs,
appdir=appdir,
database_uri=database_uri,
upgrade_database=upgrade_database,
config=config,
rebuild_docker=rebuild_docker,
run_without_docker=run_without_docker,
)
def run_ui(
host: str,
port: int,
workers: int,
reload: bool,
docs: bool,
appdir: str,
database_uri: Optional[str],
upgrade_database: bool,
config: Optional[str],
rebuild_docker: Optional[bool],
run_without_docker: bool,
):
"""
Core logic to run the Magentic-UI web application.
This function is used by both the main entry point and the legacy 'ui' command.
Args:
host (str, optional): Host to run the UI on. Defaults to 127.0.0.1 (localhost).
port (int, optional): Port to run the UI on. Defaults to 8081.
workers (int, optional): Number of workers to run the UI with. Defaults to 1.
reload (bool, optional): Whether to reload the UI on code changes. Defaults to False.
docs (bool, optional): Whether to generate API docs. Defaults to True.
appdir (str, optional): Path to the app directory where files are stored. Defaults to ~/.magentic_ui.
database_uri (str, optional): Database URI to connect to. Defaults to None.
upgrade_database (bool, optional): Whether to upgrade the database schema. Defaults to False.
config (str, optional): Path to the config file. Defaults to config.yaml if present.
rebuild_docker (bool, optional): Rebuild the docker images. Defaults to False.
run_without_docker (bool, optional): Run without docker. This will remove coder and filesurfer agents and disale live browser view. Defaults to False.
"""
# Display a green, bold "Starting Magentic-UI" message
typer.echo(typer.style("Starting Magentic-UI", fg=typer.colors.GREEN, bold=True))
# === Docker Setup ===
# Check if Docker is running and prepare required images
if not run_without_docker:
typer.echo("Checking if Docker is running...", nl=False)
if not check_docker_running():
typer.echo(typer.style("Failed\n", fg=typer.colors.RED, bold=True))
typer.echo("Docker is not running. Please start Docker and try again.")
raise typer.Exit(1) # Exit with error code 1
else:
typer.echo(typer.style("OK", fg=typer.colors.GREEN, bold=True))
# Check and build Docker images if needed
typer.echo("Checking Docker vnc browser image...", nl=False)
if not check_browser_image() or rebuild_docker:
typer.echo(typer.style("Update\n", fg=typer.colors.YELLOW, bold=True))
typer.echo("Building Docker vnc image (this WILL take a few minutes)")
build_browser_image()
typer.echo("\n")
else:
typer.echo(typer.style("OK", fg=typer.colors.GREEN, bold=True))
typer.echo("Checking Docker python image...", nl=False)
if not check_python_image() or rebuild_docker:
typer.echo(typer.style("Update\n", fg=typer.colors.YELLOW, bold=True))
typer.echo("Building Docker python image (this WILL take a few minutes)")
build_python_image()
typer.echo("\n")
else:
typer.echo(typer.style("OK", fg=typer.colors.GREEN, bold=True))
# Verify Docker images exist after attempted build
if not check_browser_image() or not check_python_image():
typer.echo(typer.style("Failed\n", fg=typer.colors.RED, bold=True))
typer.echo(
"Docker images not found. Please build the images and try again."
)
raise typer.Exit(1)
else:
typer.echo(
typer.style(
"Running without docker... This will remove the live browser view and will disable code and file manipulation.",
fg=typer.colors.YELLOW,
bold=True,
)
)
typer.echo(
typer.style(
"For the full experience of Magentic-UI please use docker.",
fg=typer.colors.YELLOW,
bold=True,
)
)
typer.echo("Launching Web Application...")
# === Environment Setup ===
# Create environment variables to pass to the web application
env_vars = {
"_HOST": host,
"_PORT": port,
"_API_DOCS": str(docs),
}
# Add optional environment variables
if appdir:
env_vars["_APPDIR"] = appdir
if database_uri:
env_vars["DATABASE_URI"] = database_uri
if upgrade_database:
env_vars["_UPGRADE_DATABASE"] = "1"
# Set Docker-related environment variables
env_vars["INSIDE_DOCKER"] = "0"
env_vars["EXTERNAL_WORKSPACE_ROOT"] = appdir
env_vars["INTERNAL_WORKSPACE_ROOT"] = appdir
env_vars["RUN_WITHOUT_DOCKER"] = str(run_without_docker)
# Handle configuration file path
if not config:
# Look for config.yaml in the current directory if not specified
if os.path.isfile("config.yaml"):
config = "config.yaml"
else:
typer.echo("Config file not provided. Using default settings.")
if config:
env_vars["_CONFIG"] = config
# Create a temporary environment file to share with Uvicorn workers
env_file_path = get_env_file_path()
with open(env_file_path, "w") as temp_env:
for key, value in env_vars.items():
temp_env.write(f"{key}={value}\n")
# Start the Uvicorn server with the configured settings
uvicorn.run(
"magentic_ui.backend.web.app:app", # Path to the ASGI application
host=host,
port=port,
workers=workers,
reload=reload,
reload_excludes=["**/alembic/*", "**/alembic.ini", "**/versions/*"]
if reload
else None,
env_file=env_file_path, # Pass environment variables via file
)
# This command is hidden from help to encourage using the new syntax
# but kept for backward compatibility with existing scripts and documentation
@app.command(hidden=True)
def ui(
host: str = "127.0.0.1",
port: int = 8081,
workers: int = 1,
reload: Annotated[bool, typer.Option("--reload")] = False,
docs: bool = True,
appdir: str = str(Path.home() / ".magentic_ui"),
database_uri: Optional[str] = None,
upgrade_database: bool = False,
config: Optional[str] = None,
rebuild_docker: Optional[bool] = False,
run_without_docker: Annotated[
bool,
typer.Option(
"--run-without-docker",
help="Run without docker. This will remove coder and filesurfer agents and disale live browser view.",
),
] = False,
):
"""
[Deprecated] Run Magentic-UI.
This command is kept for backward compatibility.
"""
# Simply delegate to the main run_ui function with the same parameters
run_ui(
host=host,
port=port,
workers=workers,
reload=reload,
docs=docs,
appdir=appdir,
database_uri=database_uri,
upgrade_database=upgrade_database,
config=config,
rebuild_docker=rebuild_docker,
run_without_docker=run_without_docker,
)
# Keep the version command for backward compatibility but hide it from help
@app.command(hidden=True)
def version():
"""
Print the version of the Magentic-UI backend CLI.
"""
typer.echo(f"Magentic-UI version: {VERSION}")
@app.command(hidden=True)
def help():
"""
Show help information about available commands and options.
"""
# Use a system call to run the command with --help
import subprocess
import sys
import os
# Get the command that was used to run this script
command = os.path.basename(sys.argv[0])
# If running directly as a module, use the appropriate command name
if command == "python" or command == "python3":
command = "magentic-ui"
# Run the command with --help
try:
subprocess.run([command, "--help"])
except FileNotFoundError:
# Fallback if the command isn't found in PATH
typer.echo(f"Error: Command '{command}' not found in PATH.")
typer.echo(f"For more information, run `{command} --help`")
def run():
"""
Main entry point called by the 'magentic' and 'magentic-ui' commands.
This function is referenced in pyproject.toml's [project.scripts] section.
"""
app() # Hand control to the Typer application
if __name__ == "__main__":
app() # Allow running this file directly with 'python -m magentic_ui.backend.cli'