-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrun.py
More file actions
1114 lines (968 loc) · 39 KB
/
run.py
File metadata and controls
1114 lines (968 loc) · 39 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
CraftBot Run Script
Usage:
python run.py # Run the agent (browser interface - default)
python run.py --tui # Run in TUI mode
python run.py --cli # Run in CLI mode
python run.py --gui # Run with GUI mode enabled (AI can control VM)
Options:
--gui Enable GUI mode (optional, requires: python install.py --gui)
--tui Use TUI (terminal UI) interface instead of browser
--cli Use CLI (command line) interface
--conda Use conda environment (overrides config setting)
--no-conda Don't use conda (overrides config setting)
--frontend-port PORT Set frontend port (default: 7925)
--backend-port PORT Set backend port (default: 7926)
Note: The installation method (conda/pip) is saved from install.py and reused here.
"""
import multiprocessing
import os
import sys
import json
import subprocess
import shutil
import time
import urllib.request
import urllib.error
import webbrowser
import atexit
from typing import Tuple, Optional, Dict, Any, List
multiprocessing.freeze_support()
from dotenv import load_dotenv
load_dotenv()
# --- Base directory ---
# In a PyInstaller --onefile binary, bundled data is extracted to sys._MEIPASS
if getattr(sys, 'frozen', False):
BASE_DIR = sys._MEIPASS
else:
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
def _bootstrap_frozen():
"""Copy bundled config/data from _MEIPASS to CWD on first run.
PyInstaller extracts bundled files into a temp directory (sys._MEIPASS)
which is read-only and deleted on exit. The app expects mutable config
and data directories under CWD so they persist between runs.
"""
if not getattr(sys, 'frozen', False):
return
import shutil as _shutil
meipass = sys._MEIPASS
cwd = os.getcwd()
# Directories to bootstrap (source relative to _MEIPASS)
dirs_to_copy = [
"app/config",
"app/data",
"agents",
"assets",
"skills",
]
# Individual files to bootstrap
files_to_copy = [
"config.json",
".env.example",
]
for rel_dir in dirs_to_copy:
src = os.path.join(meipass, rel_dir)
dst = os.path.join(cwd, rel_dir)
if os.path.isdir(src) and not os.path.isdir(dst):
print(f" Bootstrapping {rel_dir}/...")
os.makedirs(os.path.dirname(dst), exist_ok=True)
_shutil.copytree(src, dst)
for rel_file in files_to_copy:
src = os.path.join(meipass, rel_file)
dst = os.path.join(cwd, rel_file)
if os.path.isfile(src) and not os.path.isfile(dst):
print(f" Bootstrapping {rel_file}...")
_shutil.copy2(src, dst)
_bootstrap_frozen()
# --- Configuration ---
CONFIG_FILE = os.path.join(BASE_DIR, "config.json")
MAIN_APP_SCRIPT = os.path.join(BASE_DIR, "main.py")
YML_FILE = os.path.join(BASE_DIR, "environment.yml")
OMNIPARSER_ENV_NAME = "omni"
OMNIPARSER_SERVER_URL = os.getenv("OMNIPARSER_BASE_URL", "http://localhost:7861")
# ==========================================
# HELPER FUNCTIONS
# ==========================================
def parse_port_arg(args: list, flag: str, default: int) -> int:
"""Parse a port argument from command line args.
Args:
args: List of command line arguments
flag: The flag to look for (e.g., '--frontend-port')
default: Default port value if flag not found
Returns:
The port number (either from args or default)
"""
for i, arg in enumerate(args):
if arg == flag and i + 1 < len(args):
try:
return int(args[i + 1])
except ValueError:
print(f"Warning: Invalid port value for {flag}, using default {default}")
return default
elif arg.startswith(f"{flag}="):
try:
return int(arg.split("=", 1)[1])
except ValueError:
print(f"Warning: Invalid port value for {flag}, using default {default}")
return default
return default
def _wrap_windows_bat(cmd_list: list[str]) -> list[str]:
if sys.platform != "win32":
return cmd_list
exe = shutil.which(cmd_list[0])
if exe and exe.lower().endswith((".bat", ".cmd")):
return ["cmd.exe", "/d", "/c", exe] + cmd_list[1:]
return cmd_list
def load_config() -> Dict[str, Any]:
"""
Load configuration from file safely.
SECURITY FIX: Use try-except instead of check-then-use to prevent TOCTOU race conditions.
"""
try:
with open(CONFIG_FILE, 'r') as f:
return json.load(f)
except FileNotFoundError:
return {}
except json.JSONDecodeError:
return {}
except IOError:
return {}
def save_config_value(key: str, value: Any) -> None:
config = load_config()
config[key] = value
try:
with open(CONFIG_FILE, 'w') as f:
json.dump(config, f, indent=4)
except IOError:
pass
def run_command(cmd_list: list[str], cwd: Optional[str] = None, check: bool = True, capture: bool = False, env_extras: Dict[str, str] = None) -> subprocess.CompletedProcess:
cmd_list = _wrap_windows_bat(cmd_list)
my_env = os.environ.copy()
if env_extras:
my_env.update(env_extras)
my_env["PYTHONUNBUFFERED"] = "1"
kwargs = {}
if capture:
kwargs['capture_output'] = True
kwargs['text'] = True
else:
kwargs['stdout'] = sys.stdout
kwargs['stderr'] = sys.stderr
try:
return subprocess.run(cmd_list, cwd=cwd, check=check, env=my_env, **kwargs)
except subprocess.CalledProcessError:
sys.exit(1)
except FileNotFoundError as e:
print(f"Executable not found: {e.filename}")
sys.exit(1)
def launch_background_command(cmd_list: list[str], cwd: Optional[str] = None, env_extras: Dict[str, str] = None) -> Optional[subprocess.Popen]:
cmd_list = _wrap_windows_bat(cmd_list)
my_env = os.environ.copy()
if env_extras:
my_env.update(env_extras)
my_env["PYTHONUNBUFFERED"] = "1"
print(f"Starting: {' '.join(cmd_list[:3])}...", flush=True)
kwargs = {}
if sys.platform != "win32":
kwargs['start_new_session'] = True
try:
process = subprocess.Popen(
cmd_list,
cwd=cwd,
env=my_env,
stdout=sys.stdout,
stderr=sys.stderr,
**kwargs
)
return process
except Exception as e:
print(f"Error: {e}")
return None
def wait_for_server(url: str, timeout: int = 180) -> bool:
print(f"Waiting for {url}...", end="", flush=True)
start = time.time()
while time.time() - start < timeout:
try:
with urllib.request.urlopen(url, timeout=3) as r:
if r.status < 400:
print(" Ready!")
return True
except urllib.error.HTTPError as e:
if e.code < 500:
print(" Ready!")
return True
except:
pass
print(".", end="", flush=True)
time.sleep(1)
print(f" Timeout!")
return False
# ==========================================
# BROWSER FRONTEND
# ==========================================
FRONTEND_DIR = os.path.join(BASE_DIR, "app", "ui_layer", "browser", "frontend")
FRONTEND_PORT = 7925
FRONTEND_URL = f"http://localhost:{FRONTEND_PORT}"
# Global list to track background processes for cleanup
_background_processes: List[subprocess.Popen] = []
def cleanup_background_processes():
"""Clean up all background processes on exit."""
for proc in _background_processes:
if proc and proc.poll() is None:
try:
proc.terminate()
proc.wait(timeout=5)
except:
try:
proc.kill()
except:
pass
# Register cleanup on exit
atexit.register(cleanup_background_processes)
def _try_install_nodejs_linux(silent: bool = False) -> bool:
"""
Attempt to auto-install Node.js on Linux systems (including Kali).
Returns True if successful, False otherwise.
"""
if sys.platform == "win32":
return False
# Check if node is already installed
if shutil.which("node") and shutil.which("npm"):
return True
if not silent:
print("\n🔧 Attempting to install Node.js...")
# Detect package manager and prepare commands
package_managers = [
("apt-get", ["sudo", "apt-get", "update"], ["sudo", "apt-get", "install", "-y", "nodejs", "npm"]),
("apt", ["sudo", "apt", "update"], ["sudo", "apt", "install", "-y", "nodejs", "npm"]),
("dnf", None, ["sudo", "dnf", "install", "-y", "nodejs", "npm"]),
("yum", None, ["sudo", "yum", "install", "-y", "nodejs", "npm"]),
("pacman", None, ["sudo", "pacman", "-Sy", "nodejs", "npm"]),
("zypper", None, ["sudo", "zypper", "install", "-y", "nodejs", "npm"]),
]
for pm_name, update_cmd, install_cmd in package_managers.items():
if shutil.which(pm_name.split()[0]):
if not silent:
print(f" Found {pm_name}, installing Node.js...")
try:
# Run update command if available
if update_cmd:
try:
result = subprocess.run(update_cmd, capture_output=True, text=True, timeout=300)
except Exception:
pass # Update failed, but continue with install
# Run install command
result = subprocess.run(install_cmd, capture_output=True, text=True, timeout=300)
if result.returncode == 0:
if not silent:
print("✓ Node.js installed successfully")
# Small delay to ensure PATH is updated
time.sleep(1)
return True
else:
if not silent:
print(f" ⚠ {pm_name} installation failed, trying next...")
except Exception as e:
if not silent:
print(f" ⚠ Error with {pm_name}: {str(e)[:100]}, trying next...")
return False
def _launch_static_frontend(silent: bool = False) -> Optional[subprocess.Popen]:
"""Serve pre-built frontend static files with proxy support.
Used when running as a PyInstaller binary where npm/node aren't available
but the built dist/ folder is bundled. Proxies /ws and /api requests to
the backend server, mirroring the Vite dev server proxy config.
"""
import http.server
import threading
import urllib.request
dist_dir = os.path.join(FRONTEND_DIR, "dist")
backend_port = int(os.environ.get("VITE_BACKEND_PORT", BACKEND_PORT))
backend_url = f"http://localhost:{backend_port}"
class FrontendHandler(http.server.SimpleHTTPRequestHandler):
"""Serves static files and proxies /api and /ws to the backend."""
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=dist_dir, **kwargs)
def do_GET(self):
if self.path.startswith("/api/") or self.path.startswith("/api?"):
self._proxy_request()
elif self.path.startswith("/ws"):
# WebSocket upgrade can't be proxied via HTTP; the frontend
# will connect directly if we return 426
self.send_error(426, "WebSocket connections not proxied - connect directly to backend")
else:
# Serve static files; fall back to index.html for SPA routing
# Check if file exists, otherwise serve index.html
file_path = os.path.join(dist_dir, self.path.lstrip("/"))
if not os.path.exists(file_path) or os.path.isdir(file_path):
if not os.path.exists(file_path + "/index.html") and "." not in os.path.basename(self.path):
self.path = "/index.html"
super().do_GET()
def do_POST(self):
if self.path.startswith("/api/"):
self._proxy_request()
else:
self.send_error(404)
def do_PUT(self):
if self.path.startswith("/api/"):
self._proxy_request()
else:
self.send_error(404)
def do_DELETE(self):
if self.path.startswith("/api/"):
self._proxy_request()
else:
self.send_error(404)
def _proxy_request(self):
"""Forward request to the backend server."""
target_url = f"{backend_url}{self.path}"
try:
# Read request body if present
content_length = int(self.headers.get("Content-Length", 0))
body = self.rfile.read(content_length) if content_length > 0 else None
# Build proxy request
req = urllib.request.Request(target_url, data=body, method=self.command)
# Forward relevant headers
for header in ("Content-Type", "Authorization", "Accept"):
if self.headers.get(header):
req.add_header(header, self.headers[header])
with urllib.request.urlopen(req, timeout=120) as resp:
self.send_response(resp.status)
for key, val in resp.getheaders():
if key.lower() not in ("transfer-encoding", "connection"):
self.send_header(key, val)
self.end_headers()
self.wfile.write(resp.read())
except urllib.error.HTTPError as e:
self.send_response(e.code)
self.end_headers()
self.wfile.write(e.read())
except Exception as e:
self.send_error(502, f"Backend proxy error: {e}")
def log_message(self, format, *args):
pass # Suppress request logging
try:
httpd = http.server.HTTPServer(("localhost", FRONTEND_PORT), FrontendHandler)
except OSError as e:
if not silent:
print(f"Error: Could not start static frontend server: {e}")
return None
thread = threading.Thread(target=httpd.serve_forever, daemon=True)
thread.start()
# Return a dummy Popen-like object so callers can treat it uniformly
class _StaticServer:
def __init__(self, server):
self._server = server
self.returncode = None
def poll(self):
return None # always running
def terminate(self):
self._server.shutdown()
def kill(self):
self._server.shutdown()
dummy = _StaticServer(httpd)
_background_processes.append(dummy)
return dummy
def launch_frontend(silent: bool = False) -> Optional[subprocess.Popen]:
"""Launch the frontend dev server for browser mode."""
# If running as a PyInstaller binary, serve pre-built static files
# instead of launching npm dev server (node/npm won't be available)
dist_dir = os.path.join(FRONTEND_DIR, "dist")
is_frozen = getattr(sys, 'frozen', False)
if is_frozen:
if os.path.exists(dist_dir):
return _launch_static_frontend(silent)
else:
# Binary mode but no dist folder bundled — can't start frontend
if not silent:
print(f"Error: Frontend dist not found at {dist_dir}")
print(f" BASE_DIR: {BASE_DIR}")
print(f" FRONTEND_DIR: {FRONTEND_DIR}")
return None
if not os.path.exists(FRONTEND_DIR):
if not silent:
print(f"Error: Frontend directory not found at {FRONTEND_DIR}")
print("Make sure the browser frontend is installed.")
return None
# Check if node_modules exists
node_modules = os.path.join(FRONTEND_DIR, "node_modules")
if not os.path.exists(node_modules):
if not silent:
print("Error: Frontend dependencies not installed.")
print("\nTo fix this, run: python install.py")
print("\nOr manually install with:")
print(" cd app/ui_layer/browser/frontend")
print(" npm install")
return None
# Find npm command
npm_cmd = shutil.which("npm")
if not npm_cmd:
# Try to auto-install Node.js on Linux
if sys.platform != "win32":
if not silent:
print("Node.js not found. Attempting auto-install on Linux...")
if _try_install_nodejs_linux(silent=silent):
npm_cmd = shutil.which("npm")
if not npm_cmd:
if not silent:
print("Error: npm not found in PATH")
print("\nNode.js is required for browser mode.")
print("Install from: https://nodejs.org/ (choose LTS version)")
print("\nAfter installation:")
print(" 1. Restart your terminal")
print(" 2. Run: python run.py")
return None
# Build command for npm run dev
if sys.platform == "win32":
# On Windows, use cmd.exe to run npm
cmd = ["cmd.exe", "/c", "npm", "run", "dev"]
else:
cmd = [npm_cmd, "run", "dev"]
try:
# Start frontend in background
# Redirect output to DEVNULL to prevent blocking when buffer fills
process = subprocess.Popen(
cmd,
cwd=FRONTEND_DIR,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
env=os.environ.copy(),
)
_background_processes.append(process)
return process
except FileNotFoundError:
if not silent:
print("Error: npm command not found")
print("Install Node.js from: https://nodejs.org/")
return None
except Exception as e:
if not silent:
print(f"Error starting frontend: {e}")
return None
def wait_for_frontend(timeout: int = 30) -> bool:
"""Wait for the frontend dev server to be ready."""
print(f"Waiting for frontend at {FRONTEND_URL}...", end="", flush=True)
start = time.time()
while time.time() - start < timeout:
try:
with urllib.request.urlopen(FRONTEND_URL, timeout=2) as r:
if r.status < 400:
print(" Ready!")
return True
except urllib.error.HTTPError as e:
if e.code < 500:
print(" Ready!")
return True
except:
pass
print(".", end="", flush=True)
time.sleep(0.5)
print(" Timeout!")
return False
def open_browser(url: str):
"""Open the default web browser to the given URL."""
print(f"Opening browser at {url}...")
try:
webbrowser.open(url)
except Exception as e:
print(f"Could not open browser automatically: {e}")
print(f"Please open {url} manually in your browser.")
BACKEND_PORT = 7926
BACKEND_URL = f"http://localhost:{BACKEND_PORT}"
# ==========================================
# BROWSER MODE STARTUP UI
# ==========================================
STEP_WIDTH = 45 # Width for step text alignment
def print_browser_header():
"""Print the browser mode startup header."""
print("\n🤖 CraftBot")
print("━" * 52)
print("\nMode: Browser\n")
def print_step(step_num: int, total: int, message: str, done: bool = False):
"""Print a formatted step line."""
prefix = f" [{step_num:>2}/{total}]"
# Pad message to align checkmarks
padded_msg = f"{message}...".ljust(STEP_WIDTH - len(prefix))
if done:
print(f"{prefix} {padded_msg}✓", flush=True)
else:
print(f"{prefix} {padded_msg}", end="", flush=True)
def print_step_done():
"""Print checkmark for current step."""
print("✓", flush=True)
def print_progress_bar(percent: int, width: int = 40):
"""Print a progress bar from 0-100%."""
filled = int(width * percent / 100)
bar = "█" * filled + "░" * (width - filled)
sys.stdout.write(f"\r [{bar}] {percent:3d}%")
sys.stdout.flush()
def print_ready_banner(url: str):
"""Print the final ready banner."""
print("\n" + "━" * 52)
print(f"✓ Ready → CraftBot Browser Interface running at {url}")
print("━" * 52 + "\n")
def wait_for_backend_silent(timeout: int = 60) -> bool:
"""Wait for the agent backend WebSocket server to be ready (silent)."""
start = time.time()
while time.time() - start < timeout:
try:
with urllib.request.urlopen(BACKEND_URL, timeout=2) as r:
if r.status < 400:
return True
except urllib.error.HTTPError as e:
if e.code < 500:
return True
except urllib.error.URLError:
pass
except:
pass
time.sleep(0.5)
return False
def wait_for_frontend_silent(timeout: int = 30) -> bool:
"""Wait for the frontend dev server to be ready (silent)."""
start = time.time()
while time.time() - start < timeout:
try:
with urllib.request.urlopen(FRONTEND_URL, timeout=2) as r:
if r.status < 400:
return True
except urllib.error.HTTPError as e:
if e.code < 500:
return True
except:
pass
time.sleep(0.5)
return False
def wait_for_backend(timeout: int = 60) -> bool:
"""Wait for the agent backend WebSocket server to be ready."""
print(f"Waiting for agent backend at {BACKEND_URL}...", end="", flush=True)
start = time.time()
while time.time() - start < timeout:
try:
with urllib.request.urlopen(BACKEND_URL, timeout=2) as r:
if r.status < 400:
print(" Ready!")
return True
except urllib.error.HTTPError as e:
# Any HTTP response means server is up
if e.code < 500:
print(" Ready!")
return True
except urllib.error.URLError:
pass
except:
pass
print(".", end="", flush=True)
time.sleep(0.5)
print(" Timeout!")
return False
def launch_agent_background(env_name: Optional[str], use_conda: bool, silent: bool = False) -> Optional[subprocess.Popen]:
"""Launch main.py in the background for browser mode."""
main_script = os.path.abspath(MAIN_APP_SCRIPT)
if not os.path.exists(main_script):
if not silent:
print(f"Error: {main_script} not found.")
return None
# Filter flags (--browser passes through to agent)
skip_flags = {"--gui", "--conda", "--no-conda", "--tui"}
# Also skip port flags and their values
pass_args = []
skip_next = False
for a in sys.argv[1:]:
if skip_next:
skip_next = False
continue
if a in skip_flags:
continue
if a in ("--frontend-port", "--backend-port"):
skip_next = True
continue
if a.startswith("--frontend-port=") or a.startswith("--backend-port="):
continue
pass_args.append(a)
# Ensure --browser is in args (for default mode when no flags given)
if "--browser" not in pass_args:
pass_args.append("--browser")
# Set environment variable for browser startup UI formatting and warning suppression
agent_env = os.environ.copy()
agent_env["BROWSER_STARTUP_UI"] = "1"
agent_env["PYTHONWARNINGS"] = "ignore"
# When running as a PyInstaller frozen binary, run main() in a thread
# instead of spawning a subprocess (sys.executable is the binary itself)
if getattr(sys, 'frozen', False):
import threading
sys.argv = [sys.argv[0]] + pass_args
for k, v in agent_env.items():
os.environ[k] = v
def _run_agent():
try:
from main import main as main_entry
main_entry()
except Exception as e:
print(f"Agent error: {e}")
thread = threading.Thread(target=_run_agent, daemon=True)
thread.start()
# Return a dummy Popen-like object
class _AgentThread:
def __init__(self):
self.returncode = None
def poll(self):
return None if thread.is_alive() else 0
def wait(self):
thread.join()
def terminate(self):
pass # Thread will exit when main process exits (daemon=True)
def kill(self):
pass
dummy = _AgentThread()
_background_processes.append(dummy)
return dummy
# Build command
if use_conda and env_name:
conda_exe = get_conda_command()
cmd = [conda_exe, "run", "--no-capture-output", "-n", env_name, "python", "-u", main_script] + pass_args
# On Windows, wrap .bat files with cmd.exe
if sys.platform == "win32" and conda_exe.lower().endswith((".bat", ".cmd")):
cmd = ["cmd.exe", "/d", "/c"] + cmd
else:
cmd = [sys.executable, "-u", main_script] + pass_args
try:
process = subprocess.Popen(
cmd,
cwd=os.path.dirname(main_script),
env=agent_env,
stdout=sys.stdout,
stderr=sys.stderr,
)
_background_processes.append(process)
return process
except Exception as e:
if not silent:
print(f"Error starting agent: {e}")
return None
# ==========================================
# ENVIRONMENT DETECTION
# ==========================================
def is_conda_installed() -> Tuple[bool, str, Optional[str]]:
conda_exe = shutil.which("conda")
if conda_exe:
return True, conda_exe, os.path.dirname(os.path.dirname(conda_exe))
if sys.platform == "win32":
# Check common Miniconda/Anaconda installation paths
common_paths = [
os.path.join(os.path.expanduser("~"), "miniconda3"),
os.path.join(os.path.expanduser("~"), "Miniconda3"),
os.path.join(os.path.expanduser("~"), "anaconda3"),
os.path.join(os.path.expanduser("~"), "Anaconda3"),
"C:\\miniconda3",
"C:\\Miniconda3",
"C:\\anaconda3",
"C:\\Anaconda3",
]
for base_path in common_paths:
conda_bat = os.path.join(base_path, "condabin", "conda.bat")
if os.path.exists(conda_bat):
return True, conda_bat, base_path
# Also check current Python directory
for base in [os.path.dirname(os.path.dirname(sys.executable))]:
if os.path.exists(os.path.join(base, "condabin", "conda.bat")):
return True, base, base
return False, "", None
def get_env_name_from_yml() -> str:
try:
with open(YML_FILE, 'r') as f:
for line in f:
if line.strip().startswith("name:"):
return line.split(":", 1)[1].strip().strip("'\"")
except:
pass
return "craftbot"
def get_conda_command() -> str:
"""Return conda command. Use full path on Windows if conda not in PATH."""
# First try to find conda in PATH
conda_exe = shutil.which("conda")
if conda_exe:
return conda_exe
# On Windows, check common installation paths
if sys.platform == "win32":
common_paths = [
os.path.join(os.path.expanduser("~"), "miniconda3"),
os.path.join(os.path.expanduser("~"), "Miniconda3"),
os.path.join(os.path.expanduser("~"), "anaconda3"),
os.path.join(os.path.expanduser("~"), "Anaconda3"),
"C:\\miniconda3",
"C:\\Miniconda3",
"C:\\anaconda3",
"C:\\Anaconda3",
]
for base_path in common_paths:
conda_bat = os.path.join(base_path, "condabin", "conda.bat")
if os.path.exists(conda_bat):
return conda_bat
# Fallback to just "conda" (will work if it's in PATH)
return "conda"
def verify_env(env_name: str) -> bool:
try:
conda_cmd = get_conda_command()
cmd = [conda_cmd, "run", "-n", env_name, "python", "-c", "print('ok')"]
run_command(cmd, capture=True)
return True
except:
return False
# ==========================================
# OMNIPARSER SERVER
# ==========================================
def launch_omniparser(use_conda: bool) -> bool:
"""Launch OmniParser server for GUI mode."""
print("Starting GUI components (OmniParser)...")
config = load_config()
repo_path = config.get("omniparser_repo_path", os.path.abspath("OmniParser_CraftOS"))
if not os.path.exists(repo_path):
print(f"Error: GUI components not installed.")
print("Run 'python install.py --gui --conda' first.")
return False
if use_conda:
conda_cmd = get_conda_command()
cmd = [conda_cmd, "run", "-n", OMNIPARSER_ENV_NAME, "python", "-u", "-m", "gradio_demo"]
else:
cmd = [sys.executable, "-u", "-m", "gradio_demo"]
launch_background_command(cmd, cwd=repo_path)
if wait_for_server(OMNIPARSER_SERVER_URL, timeout=180):
os.environ["OMNIPARSER_BASE_URL"] = OMNIPARSER_SERVER_URL
return True
print("Failed to start GUI components.")
return False
# ==========================================
# MAIN LAUNCHER
# ==========================================
def launch_agent(env_name: Optional[str], conda_base: Optional[str], use_conda: bool):
"""Launch main.py in the current terminal."""
main_script = os.path.abspath(MAIN_APP_SCRIPT)
if not os.path.exists(main_script):
print(f"Error: {main_script} not found.")
sys.exit(1)
# Filter flags (--cli and --tui pass through to agent)
skip_flags = {"--gui", "--conda", "--no-conda", "--browser"}
# Also skip port flags and their values
pass_args = []
skip_next = False
for a in sys.argv[1:]:
if skip_next:
skip_next = False
continue
if a in skip_flags:
continue
if a in ("--frontend-port", "--backend-port"):
skip_next = True
continue
if a.startswith("--frontend-port=") or a.startswith("--backend-port="):
continue
pass_args.append(a)
print(f"Starting CraftBot...\n")
# When running as a PyInstaller frozen binary, sys.executable points to
# the binary itself, so spawning "python main.py" would re-run run.py
# in an infinite loop. Instead, import and call main() directly.
if getattr(sys, 'frozen', False):
try:
sys.argv = [sys.argv[0]] + pass_args
from main import main as main_entry
main_entry()
except KeyboardInterrupt:
print("\nInterrupted.")
sys.exit(0)
return
# Build command
if use_conda and env_name:
conda_exe = get_conda_command()
cmd = [conda_exe, "run", "--no-capture-output", "-n", env_name, "python", "-u", main_script] + pass_args
# On Windows, wrap .bat files with cmd.exe
if sys.platform == "win32" and conda_exe.lower().endswith((".bat", ".cmd")):
cmd = ["cmd.exe", "/d", "/c"] + cmd
else:
cmd = [sys.executable, "-u", main_script] + pass_args
# Run in current terminal with all environment variables
try:
result = subprocess.run(cmd, cwd=os.path.dirname(main_script), env=os.environ.copy())
sys.exit(result.returncode)
except KeyboardInterrupt:
print("\nInterrupted.")
sys.exit(0)
# ==========================================
# MAIN
# ==========================================
if __name__ == "__main__":
args_list = sys.argv[1:]
args = set(args_list)
# Parse flags
gui_mode = "--gui" in args
tui_mode = "--tui" in args
cli_mode = "--cli" in args
conda_flag = "--conda" in args
no_conda_flag = "--no-conda" in args
# Parse port arguments (override defaults)
FRONTEND_PORT = parse_port_arg(args_list, "--frontend-port", FRONTEND_PORT)
BACKEND_PORT = parse_port_arg(args_list, "--backend-port", BACKEND_PORT)
FRONTEND_URL = f"http://localhost:{FRONTEND_PORT}"
BACKEND_URL = f"http://localhost:{BACKEND_PORT}"
# Browser mode is default (unless --tui or --cli specified)
browser_mode = not tui_mode and not cli_mode
# Load saved config to check what was actually installed
config = load_config()
use_conda = config.get("use_conda", False) # Use config instead of defaulting to True
# Override with command-line flags if provided
if conda_flag:
use_conda = True
elif no_conda_flag:
use_conda = False
gui_installed = config.get("gui_mode_enabled", False)
# Set environment variables
os.environ["USE_CONDA"] = str(use_conda)
os.environ["GUI_MODE_ENABLED"] = str(gui_mode)
os.environ["USE_OMNIPARSER"] = str(gui_mode and gui_installed)
# Set port environment variables for frontend (Vite) and backend
os.environ["VITE_PORT"] = str(FRONTEND_PORT)
os.environ["VITE_BACKEND_PORT"] = str(BACKEND_PORT)
os.environ["BROWSER_PORT"] = str(BACKEND_PORT)
# Determine mode string for display (only print for non-browser modes)
if not browser_mode:
if cli_mode:
mode_str = "CLI"
elif gui_mode:
mode_str = "GUI + TUI"
else:
mode_str = "TUI"
print(f"\nMode: {mode_str}")
# Check conda only if it was installed earlier
conda_base = None
env_name = None
if use_conda:
found, path, conda_base = is_conda_installed()
if not found:
print("Error: Conda not found.")
print("If you want to use conda, run: python install.py --conda")
print("Or run without conda: python run.py (global pip only)\n")
sys.exit(1)
env_name = get_env_name_from_yml()
if not verify_env(env_name):
print(f"\nEnvironment '{env_name}' not ready.")
print("Run 'python install.py' or 'python install.py --conda' first.\n")
sys.exit(1)
# Start OmniParser only if GUI mode and it was installed
if gui_mode and gui_installed:
if not launch_omniparser(use_conda):
print("Warning: Continuing without OmniParser.")
os.environ["USE_OMNIPARSER"] = "False"
elif gui_mode and not gui_installed:
print("\nGUI mode requested but components not installed.")
print("Run: python install.py --gui --conda\n")
sys.exit(1)
# Browser mode: start frontend + agent, wait for both, then open browser
if browser_mode:
# Print browser mode header
print_browser_header()
# Step 1: Start frontend server (0% -> 10%)
# Step 1: Start frontend server