-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlaunch.py
More file actions
94 lines (77 loc) · 3.02 KB
/
Copy pathlaunch.py
File metadata and controls
94 lines (77 loc) · 3.02 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
#!/usr/bin/env python3
"""One-command local launcher for OpenAgentForce.
Uses only the Python standard library until the isolated virtual environment is
created, the package is installed, and secure first-run configuration exists.
"""
from __future__ import annotations
import argparse
import os
import subprocess
import sys
import time
import urllib.request
import venv
import webbrowser
from pathlib import Path
ROOT = Path(__file__).resolve().parent
VENV = ROOT / ".venv"
def executable(name: str) -> Path:
if os.name == "nt":
return VENV / "Scripts" / (name + ".exe")
return VENV / "bin" / name
def run(command: list[str]) -> None:
subprocess.run(command, cwd=ROOT, check=True)
def wait_for(url: str, timeout: float = 45.0) -> bool:
deadline = time.time() + timeout
while time.time() < deadline:
try:
with urllib.request.urlopen(url, timeout=1.0) as response:
return response.status < 500
except Exception:
time.sleep(0.25)
return False
def main() -> int:
parser = argparse.ArgumentParser(description="Install, initialize, and launch OpenAgentForce")
parser.add_argument("--host", default="127.0.0.1")
parser.add_argument("--port", type=int, default=8000)
parser.add_argument("--no-browser", action="store_true")
parser.add_argument("--reinstall", action="store_true")
args = parser.parse_args()
if not VENV.exists():
print("Creating isolated Python environment...")
venv.EnvBuilder(with_pip=True).create(VENV)
python = executable("python")
command = executable("openagentforce")
marker = VENV / ".openagentforce-installed"
if args.reinstall or not marker.exists():
print("Installing OpenAgentForce...")
run([str(python), "-m", "pip", "install", "--upgrade", "pip"])
run([str(python), "-m", "pip", "install", "-e", str(ROOT)])
marker.write_text("installed\n")
if not (ROOT / ".env").exists():
print("Generating secure first-run configuration...")
run([str(command), "init", "--root", str(ROOT)])
url = f"http://{args.host}:{args.port}/setup"
print(f"Starting OpenAgentForce at {url}")
process = subprocess.Popen([str(command), "serve", "--host", args.host, "--port", str(args.port)], cwd=ROOT)
try:
if wait_for(url):
if not args.no_browser:
webbrowser.open(url)
key_file = ROOT / ".cap-admin-key"
print("\nSetup page is ready.")
if key_file.exists():
print(f"Administrator key: {key_file.read_text().strip()}")
print("Press Ctrl+C to stop the server.")
else:
print("The server did not become ready; inspect the process output above.", file=sys.stderr)
return process.wait()
except KeyboardInterrupt:
process.terminate()
try:
process.wait(timeout=5)
except subprocess.TimeoutExpired:
process.kill()
return 0
if __name__ == "__main__":
raise SystemExit(main())