Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /MarkinHaus_ToolBoxV2 /toolboxv2 /__main__.py
"""Console script for toolboxv2."""
import argparse
import asyncio
import pprint
# Import default Pages
import textwrap
import time
from contextlib import redirect_stdout
from functools import wraps
from pathlib import Path
from platform import node, system
from typing import List, Tuple
from dotenv import load_dotenv
load_dotenv()
import os
import sys
# ponytail: Nuitka no_site (standalone default) removes builtin exit()/quit().
# Remove this shim when --python-flag=no_site is disabled in build config.
exit = sys.exit
from toolboxv2 import tb_root_dir, _feature_enabled
from toolboxv2.utils.system.feature_manager import FeatureManager
from toolboxv2.flows import flows_dict as flows_dict_func
try:
from toolboxv2.setup_helper import run_command
except ImportError as e:
run_command = lambda x:print(x, f"run_command error {e}")
from toolboxv2.utils import get_app
from toolboxv2.utils.clis.db_cli_manager import cli_db_runner
from toolboxv2.utils.clis.user_manager import main as user_manager_main
from toolboxv2.utils.clis.tcm_p2p_cli import cli_tcm_runner
from toolboxv2.utils.extras.Style import Spinner, Style
from toolboxv2.utils.system import CallingObject, get_state_from_app
from toolboxv2.utils.system.main_tool import MainTool, get_version_from_pyproject
from toolboxv2.utils.system.getting_and_closing_app import a_save_closing_app
from toolboxv2.utils.system.exe_bg import run_executable_in_background
from toolboxv2.utils.clis.cli_input import menu_select, menu_select_async
from .utils.toolbox import App as TbApp
# ── WEB-Feature: workers, proxy, dashboard ────────────────────────────────────
_WEB_AVAILABLE = False
cli_worker_manager = None
interactive_user_dashboard = None
DaemonApp = None
ProxyApp = None
a_get_proxy_app = None
cli_event = cli_http_worker = cli_session = cli_ws_worker = None
if _feature_enabled("web"):
try:
from toolboxv2.utils.clis.cli_worker_manager import main as cli_worker_manager
from toolboxv2.utils.clis.user_dashboard import interactive_user_dashboard
from toolboxv2.utils.daemon import DaemonApp
from toolboxv2.utils.proxy import ProxyApp
from toolboxv2.utils.system.getting_and_closing_app import a_get_proxy_app
from toolboxv2.utils.workers import (
cli_event, cli_http_worker, cli_session, cli_ws_worker,
)
_WEB_AVAILABLE = True
except ImportError as _e:
import sys
import traceback
traceback.print_exc()
print(f"[web] Import failed: {_e}", file=sys.stderr)
# ── ISAA-Feature: MCP server ──────────────────────────────────────────────────
cli_mcp_server = None
if _feature_enabled("isaa"):
try:
from .mcp_server.__main__ import main as cli_mcp_server
except ImportError:
cli_mcp_server = lambda: None
else:
cli_mcp_server = lambda: None
# Set UTF-8 encoding for Windows console (place at top of your script)
if sys.platform == "win32":
import ctypes
ctypes.windll.kernel32.SetConsoleOutputCP(65001)
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8")
sys.stderr.reconfigure(encoding="utf-8")
DEFAULT_MODI = "cli"
_hook = [None]
try:
import hmr
HOT_RELOADER = True
except ImportError:
HOT_RELOADER = False
try:
import cProfile
import io
import pstats
def profile_execute_all_functions(app=None, m_query="", f_query=""):
# Erstellen Sie eine Instanz Ihrer Klasse
instance = app if app is not None else get_app(from_="Profiler")
# Erstellen eines Profilers
profiler = cProfile.Profile()
def timeit(func_):
@wraps(func_)
def timeit_wrapper(*args, **kwargs):
profiler.enable()
start_time = time.perf_counter()
result = func_(*args, **kwargs)
end_time = time.perf_counter()
profiler.disable()
total_time_ = end_time - start_time
print(
f"Function {func_.__name__}{args} {kwargs} Took {total_time_:.4f} seconds"
)
return result
return timeit_wrapper
items = list(instance.functions.items()).copy()
for module_name, functions in items:
if not module_name.startswith(m_query):
continue
f_items = list(functions.items()).copy()
for function_name, function_data in f_items:
if not isinstance(function_data, dict):
continue
if not function_name.startswith(f_query):
continue
test: list = function_data.get("do_test")
print(test, module_name, function_name, function_data)
if test is False:
continue
instance.functions[module_name][function_name]["func"] = timeit(
function_data.get("func")
)
# Starten des Profilers und Ausführen der Funktion
instance.execute_all_functions(m_query=m_query, f_query=f_query)
s = io.StringIO()
sortby = "cumulative"
ps = pstats.Stats(profiler, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())
print("\n================================" * 12)
s = io.StringIO()
sortby = (
"time" # Sortierung nach der Gesamtzeit, die in jeder Funktion verbracht wird
)
# Erstellen eines pstats-Objekts und Ausgabe der Top-Funktionen
ps = pstats.Stats(profiler, stream=s).sort_stats(sortby)
ps.print_stats()
# Ausgabe der Ergebnisse
print(s.getvalue())
# Erstellen eines Streams für die Profilergebnisse
except ImportError:
def profile_execute_all_functions(*args):
return print(args)
raise ValueError("Failed to import function for profiling")
try:
from toolboxv2.utils.system.tb_logger import (
edit_log_files,
loggerNameOfToolboxv2,
unstyle_log_files,
)
except ModuleNotFoundError:
from toolboxv2.utils.system.tb_logger import (
edit_log_files,
loggerNameOfToolboxv2,
unstyle_log_files,
)
import os
import subprocess
def _get_profile() -> str | None:
"""
Lese app.profile aus dem Manifest.
None = Manifest existiert nicht oder profile nicht gesetzt → First-Run.
"""
try:
from toolboxv2 import tb_root_dir
from toolboxv2.utils.manifest import ManifestLoader
loader = ManifestLoader(tb_root_dir)
if not loader.exists():
return None
manifest = loader.load()
return manifest.app.profile.value if manifest.app.profile else None
except Exception:
return None
def _run_server_overview():
"""ASCII-Übersicht für profile=server."""
import datetime
try:
from toolboxv2.utils.clis.service_manager import ServiceManager
sm = ServiceManager()
status = sm.get_all_status(include_registry=True)
except Exception:
status = {}
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
running = sum(1 for v in status.values() if v.get("running"))
total = len(status)
print(f"\n{'═'*52}")
print(f" ToolBoxV2 Server Overview • {now}")
print(f"{'═'*52}")
print(f" Services : {running}/{total} running")
for name, info in status.items():
state = "▶ running" if info.get("running") else "■ stopped"
pid = f"pid={info['pid']}" if info.get("pid") else " "
print(f" {'✅' if info.get('running') else '🔴'} {name:<18} {state:<12} {pid}")
print(f"{'═'*52}\n")
def _run_business_overview():
"""3-Zeilen Health-Summary für profile=business."""
try:
from toolboxv2.utils.clis.service_manager import ServiceManager
status = ServiceManager().get_all_status(include_registry=True)
running = sum(1 for v in status.values() if v.get("running"))
total = len(status)
health = "✅ Healthy" if running == total and total > 0 else ("⚠️ Degraded" if running > 0 else "🔴 Down")
except Exception:
health, running, total = "❓ Unknown", 0, 0
print(f"\n ToolBoxV2 Status: {health} ({running}/{total} services) — run 'tb status' for details\n")
import os
import sys
import subprocess
from platform import system
def start(pidname, args, filename):
caller = args[0]
sub_args = args[1:]
sub_args = ["-bgr" if arg == "-bg" else arg for arg in sub_args]
# ponytail: In Nuitka standalone/onefile, sys.executable IS the compiled binary.
# Skip python/pythonw swap — spawn self directly without "-m toolboxv2".
try:
# __compiled__
p = run_executable_in_background(sys.executable, sub_args)
pid = p.pid
with open(filename, "w", encoding="utf8") as f:
f.write(str(pid))
print(f"Service {pidname} started (PID {pid})")
return
except NameError:
pass
if system() == "Windows":
# Ersetze python.exe durch pythonw.exe, um Fenster vollständig zu unterdrücken
exe_name = os.path.basename(sys.executable)
if exe_name.lower().startswith("python") and not exe_name.lower().startswith("pythonw"):
new_exe = exe_name.lower().replace("python", "pythonw")
executable = os.path.join(os.path.dirname(sys.executable), new_exe)
if not os.path.exists(executable):
executable = sys.executable
else:
executable = sys.executable
# Nutze direkt das Python-Modul, um den tb.exe Console-Wrapper zu umgehen
run_args = ["-m", "toolboxv2"] + sub_args
else:
executable = sys.executable
run_args = ["-m", "toolboxv2"] + sub_args
# Plattformspezifisches, lautloses Starten ohne Terminalfenster
p = run_executable_in_background(executable, run_args)
pid = p.pid
with open(filename, "w", encoding="utf8") as f:
f.write(str(pid))
print(f"Service {pidname} started (PID {pid})")
def stop(pidfile, pidname):
try:
with open(pidfile, encoding="utf8") as f:
procID = f.readline().strip()
except OSError:
get_app().logger.error("Process file does not exist")
return
if procID:
if system() == "Windows":
subprocess.Popen(["taskkill", "/PID", procID, "/F"],
creationflags=0x08000000 if system() == "Windows" else 0)
else:
subprocess.Popen(["kill", "-SIGTERM", procID],
creationflags=0x08000000 if system() == "Windows" else 0)
get_app().logger.info(f"Service {pidname} {procID} stopped")
os.remove(pidfile)
def create_service_file(user, group, working_dir, runner):
service_content = f"""[Unit]
Description=ToolBoxService
After=network.target
[Service]
Type=oneshot
User={user}
Group={group}
WorkingDirectory={working_dir}
ExecStart=tb --sm
[Install]
WantedBy=multi-user.target
"""
with open("tb.service", "w", encoding="utf8") as f:
f.write(service_content)
def init_service():
user = input("Enter the user name: ")
group = input("Enter the group name: ")
runner = "bg"
if runner_ := input("enter a runner default bg: ").strip():
runner = runner_
working_dir = get_app().start_dir
create_service_file(user, group, working_dir, runner)
subprocess.run(["sudo", "mv", "tb.service", "/etc/systemd/system/"])
subprocess.run(["sudo", "systemctl", "daemon-reload"])
def manage_service(action):
subprocess.run(["sudo", "systemctl", action, "tb.service"])
def show_service_status():
subprocess.run(["sudo", "systemctl", "status", "tb.service"])
def uninstall_service():
subprocess.run(["sudo", "systemctl", "disable", "tb.service"])
subprocess.run(["sudo", "systemctl", "stop", "tb.service"])
subprocess.run(["sudo", "rm", "/etc/systemd/system/tb.service"])
subprocess.run(["sudo", "systemctl", "daemon-reload"])
async def setup_service_windows():
path = "C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Startup"
# C:\Users\Markin\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
mode = await menu_select_async(
[
("init", "Init (first-time setup)"),
("uninstall", "Uninstall"),
("show", "Show window"),
("hide", "hide window"),
("exit", "Exit")
],
title="Select mode:",
hint="↑/↓ or W/S · Enter · q to back"
)
if mode in (None, "exit"):
return
if not os.path.exists(path):
print("pleas press win + r and enter")
print("1. for system -> shell:common startup")
print("2. for user -> shell:startup")
path = input("Enter the path that opened: ")
res = None
if mode == "init":
if os.path.exists(path + "/tb_start.bat"):
os.remove(path + "/tb_start.bat")
try:
with open(path + "/tb_start.bat", "a", encoding="utf8") as f:
command = f"-m toolboxv2.__main__ --sm"
f.write(f"""{sys.executable} {command}""")
print(f"Init Service in {path}")
except PermissionError:
print("Pleas run as Admin")
elif mode == "show":
res = get_app().show_console()
elif mode == "hide":
res = get_app().hide_console()
elif mode == "uninstall":
try:
os.remove(path + "/tb_start.bat")
print(f"Removed Service from {path}")
except FileNotFoundError:
print("Service file not found.")
if res is not None:
await res
def setup_service_linux():
mode = menu_select(
[
("init", "Init (first-time setup)"),
("manage", "Start / Stop / Restart"),
("status", "Status"),
("uninstall", "Uninstall"),
("show", "Show window"),
("hide", "hide window")
],
title="Select mode:",
hint="↑/↓ or W/S · Enter · q to back"
)
if mode is None:
return
if mode == "init":
init_service()
elif mode == "manage":
action = input("Enter 'start', 'stop', or 'restart': ")
manage_service(action)
elif mode == "status":
show_service_status()
elif mode == "uninstall":
uninstall_service()
elif mode == "show":
get_app().show_console()
elif mode == "hide":
get_app().hide_console()
# =================== Constants ===================
RUNNER_KEYS = [
"help",
"!",
"version",
"venv",
"db",
"gui",
"p2p",
"default",
"status",
"browser",
"mcp",
"login",
"logout",
"run",
"mods",
"flow",
"user",
"workers",
"session",
"event",
"broker",
"build",
"http_worker",
"obs",
"ws_worker",
"access",
"services",
"registry",
"manifest",
"llm-gateway",
"mkdocs",
"build",
"analyze",
"docksh",
"docker-image",
"fl",
"jsx",
"ytss",
"LiveSync",
"tray",
"local-cli"
]
DEFAULT_MODI = "local-cli"
# =================== Helper Functions ===================
def split_args_by_runner(
args: List[str], runner_keys: List[str]
) -> Tuple[List[str], str, List[str]]:
"""Split arguments into main args, runner name, and runner args."""
for i, arg in enumerate(args):
# ponytail: -init takes a value that can equal a runner key (e.g.
# 'manifest'). The value is not a runner -- skip it.
if i > 0 and args[i - 1] == "-init":
continue
if arg in runner_keys:
return args[:i], arg, args[i + 1 :]
return args, None, []
def parse_kwargs(kwargs_list: List[str]) -> dict:
"""Parse key=value pairs into dictionary."""
kwargs = {}
for item in kwargs_list:
if "=" in item:
key, value = item.split("=", 1)
kwargs[key.strip()] = value.strip()
elif ":" in item:
key, value = item.split(":", 1)
kwargs[key.strip()] = value.strip()
return kwargs
# =================== Modern Help Formatter ===================
class ModernHelpFormatter(argparse.RawDescriptionHelpFormatter):
"""Modern help formatter with better spacing and alignment."""
def __init__(self, prog, indent_increment=2, max_help_position=40, width=None):
super().__init__(prog, indent_increment, max_help_position, width)
def _format_action(self, action):
# Get the default formatting
result = super()._format_action(action)
# Add spacing for subcommands
if action.nargs == 0 and action.option_strings:
return result
return result
# =================== Guide System ===================
def show_interactive_guide():
"""Show interactive guide with examples and tips."""
guide = textwrap.dedent("""
╔════════════════════════════════════════════════════════════════════════════╗
║ 🧰 ToolBoxV2 — Interactive Guide 🧰 ║
╚════════════════════════════════════════════════════════════════════════════╝
┌─ FIRST RUN & PROFILES ─────────────────────────────────────────────────────┐
│ │
│ On first launch ToolBoxV2 asks you to pick a profile that tailors the │
│ default experience: │
│ │
│ consumer — opens the GUI automatically │
│ homelab — opens the interactive CLI dashboard │
│ developer — opens the interactive CLI dashboard │
│ server — prints a service overview, then exits │
│ business — prints a health summary, then exits │
│ │
│ Setup commands: │
│ $ tb -init main # Bootstrap ToolBoxV2 for first use │
│ $ tb -init config # Run the interactive config wizard │
│ $ tb manifest # View / edit the manifest directly │
│ │
│ You can re-run the config wizard at any time with `tb -init config`. │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ QUICK START ──────────────────────────────────────────────────────────────┐
│ │
│ Start ToolBoxV2: │
│ $ tb # Launch default for your profile │
│ $ tb --sm # Start all services from manifest │
│ $ tb gui # Start the desktop GUI │
│ $ tb workers start # Start the API worker system │
│ $ tb status # Full system health check │
│ │
│ Update: │
│ $ tb -u main # Update core to the latest version │
│ $ tb -u [MODULE] # Update a single module │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ FEATURE FLAGS ────────────────────────────────────────────────────────────┐
│ │
│ Some runners require optional feature groups to be enabled: │
│ │
│ web — workers, http_worker, ws_worker │
│ pip install toolboxv2[web] │
│ isaa — mcp (MCP server for AI agents) │
│ pip install toolboxv2[isaa] │
│ desktop — gui (Tauri desktop app) │
│ pip install toolboxv2[desktop] │
│ │
│ Enable / disable in the manifest: │
│ $ tb manifest enable web # Enable feature group │
│ $ tb manifest disable isaa # Disable feature group │
│ │
│ If a runner's feature is missing, ToolBoxV2 tells you exactly which │
│ feature to enable and which extras to install. │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ AUTHENTICATION ───────────────────────────────────────────────────────────┐
│ │
│ $ tb login # Login (browser-based OAuth) │
│ $ tb login --status # Show current session info │
│ $ tb logout # End current session │
│ $ tb user # Interactive user management │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ MODULE MANAGEMENT ────────────────────────────────────────────────────────┐
│ │
│ Quick install / update / remove: │
│ $ tb -i [MODULE] # Install module │
│ $ tb -u [MODULE] # Update module │
│ $ tb -r [MODULE] # Remove module │
│ │
│ Module manager (interactive & scriptable): │
│ $ tb mods # Open interactive module manager │
│ $ tb mods list # List all available modules │
│ $ tb mods create MyMod # Create a new module scaffold │
│ $ tb mods build MyMod # Build module for distribution │
│ $ tb mods create MyMod --external --type tool │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ SERVICES & WORKERS ───────────────────────────────────────────────────────┐
│ │
│ Worker system (requires feature: web): │
│ $ tb workers start # Start all workers │
│ $ tb workers stop # Stop all workers │
│ $ tb workers status # Show worker status │
│ $ tb http_worker # Start a single HTTP worker │
│ $ tb ws_worker # Start a single WebSocket worker │
│ │
│ Infrastructure: │
│ $ tb broker # Start ZMQ event broker │
│ $ tb session generate-secret # Generate session secret │
│ $ tb session test # Test session configuration │
│ │
│ Service manager: │
│ $ tb services # Interactive service management │
│ $ tb --sm # Auto-start services from manifest │
│ $ tb --init-sm # Install as system service │
│ │
│ Global CLI access: │
│ $ tb access install # Make `tb` available system-wide │
│ $ tb access status # Check global access status │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ INTERFACES ───────────────────────────────────────────────────────────────┐
│ │
│ $ tb gui # Desktop GUI (feature: desktop) │
│ $ tb mcp # MCP server for AI agents (isaa) │
│ $ tb p2p [start|stop|status] # P2P mesh client │
│ $ tb jsx # JSX dev server (live preview) │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ DATABASE & REGISTRY ──────────────────────────────────────────────────────┐
│ │
│ $ tb db # Interactive DB management │
│ $ tb db status # Show DB connection status │
│ $ tb registry # Package registry management │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ DOCKER & CONTAINERS ──────────────────────────────────────────────────────┐
│ │
│ $ tb --docker -m [test|live|dev] -p 8000 # Run in Docker │
│ $ tb --build # Build Docker image from source │
│ $ tb docker-image # Manage Docker images │
│ $ tb docksh # Open interactive Docker shell │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ FLOW EXECUTION ───────────────────────────────────────────────────────────┐
│ │
│ $ tb run # Execute .tbx files (TBX Lang) │
│ $ tb -m [flow] # Run a flow from the default folder │
│ $ tb flow --flow [file] # Run flow from a specific file │
│ $ tb flow --remote # Run flow from a remote .gist │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ MANIFEST & CONFIGURATION ─────────────────────────────────────────────────┐
│ │
│ $ tb manifest # View / edit manifest interactively │
│ $ tb manifest enable [feat] # Enable a feature group │
│ $ tb manifest disable [feat] # Disable a feature group │
│ $ tb -init config # Re-run the config wizard │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ OBSERVABILITY & MONITORING ───────────────────────────────────────────────┐
│ │
│ $ tb obs # Observability layer (metrics, logs) │
│ $ tb --lm # Log manager (view / remove / edit) │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ MEDIA & STREAMING ────────────────────────────────────────────────────────┐
│ │
│ $ tb ytss # YouTube streaming service │
│ $ tb LiveSync # Real-time file synchronization │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ BUILD & DISTRIBUTION ─────────────────────────────────────────────────────┐
│ │
│ $ tb build # Build ToolBoxV2 + features + upload │
│ $ tb browser build # Build browser extension │
│ $ tb browser install # Install browser extension │
│ $ tb fl # Feature loader (load feature packs) │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ LLM GATEWAY ─────────────────────────────────────────────────────────────┐
│ │
│ $ tb llm-gateway # Start the LLM gateway proxy │
│ # (routes to local & remote models) │
│ │
└───────────────────────────────────────────────────────────────────────────┘
┌─ COMMAND EXECUTION ────────────────────────────────────────────────────────┐
│ │
│ Basic Syntax: │
│ $ tb -c [MODULE] [FUNCTION] [ARGS...] │
│ │
│ Show Module Info: │
│ $ tb -c # List all modules and functions │
│ $ tb -c CloudM # List all CloudM functions │
│ │
│ Execute Functions: │
│ $ tb -c CloudM Version # Get CloudM version │
│ $ tb -c CloudM get_mod_snapshot CloudM │
│ $ tb -c MyMod my_func arg1 arg2 │
│ │
│ With Kwargs: │
│ $ tb -c CloudM get_mod_snapshot --kwargs mod_name=CloudM │
│ $ tb -c MyMod my_func --kwargs key1:value1 key2:value2 │
│ │
│ Multiple Commands: │
│ $ tb -c CloudM Version -c CloudM get_mod_snapshot CloudM │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ USER MANAGEMENT ──────────────────────────────────────────────────────────┐
│ │
│ $ tb user list # List all users │
│ $ tb user info --username john # Show user info │
│ $ tb user set-level john admin # Set user level │
│ $ tb user rotate-minio john # Rotate MinIO credentials │
│ $ tb user revoke-minio john # Revoke MinIO access │
│ $ tb user delete --username jo # Delete user │
│ │
│ Levels: guest, user, moderator, admin, root │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ VIRTUAL ENVIRONMENT ──────────────────────────────────────────────────────┐
│ │
│ $ tb venv # Manage Python virtual environments │
│ $ tb venv -h # Show venv subcommand help │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ ADVANCED USAGE ───────────────────────────────────────────────────────────┐
│ │
│ Remote Mode: │
│ $ tb --remote -w 0.0.0.0 -p 5000 │
│ │
│ Debug Mode: │
│ $ tb --debug --sysPrint # Enable verbose output + hot-reload │
│ │
│ Instance Management: │
│ $ tb -n myinstance # Start with custom instance name │
│ $ tb --kill # Kill running instance │
│ │
│ Background Mode: │
│ $ tb -bg # Run interface in background │
│ $ tb -fg # Run proxy interface in foreground │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ DATA OPERATIONS (⚠️ DANGER ZONE) ────────────────────────────────────────┐
│ │
│ ⚠️ WARNING: These operations cause DATA LOSS! │
│ │
│ $ tb --delete-config # Delete named config folder │
│ $ tb --delete-data # Delete named data folder │
│ $ tb --delete-config-all # Delete ALL configs (!) │
│ $ tb --delete-data-all # Delete ALL data (!) │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ DEVELOPMENT & TESTING ────────────────────────────────────────────────────┐
│ │
│ $ tb --test # Run complete test suite │
│ $ tb --test --kwargs registry # Run only registry tests │
│ $ tb --profiler # Profile all registered functions │
│ $ tb -l # Load all modules from mod directory │
│ $ tb -sfe -l # Generate all_function_enums.py │
│ $ tb -v -l # Show all module versions │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ PRACTICAL EXAMPLES ───────────────────────────────────────────────────────┐
│ │
│ Install and run a module: │
│ $ tb -i MyModule │
│ $ tb -c MyModule my_function │
│ │
│ Full status check: │
│ $ tb status # DB, API, P2P, workers, services │
│ │
│ Create user and manage access: │
│ $ tb user list │
│ $ tb user info --username alice │
│ $ tb user set-level alice admin │
│ │
│ Run in Docker: │
│ $ tb --docker -m dev -p 8000 -w 0.0.0.0 │
│ │
│ Start MCP server for AI agents: │
│ $ tb mcp │
│ │
│ Start LLM gateway: │
│ $ tb llm-gateway │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ ALL RUNNER COMMANDS (A–Z) ────────────────────────────────────────────────┐
│ │
│ access Global CLI access management │
│ analyze Anylse python & JS type code static and runtime │
│ broker ZMQ event broker │
│ browser Browser extension build / install │
│ build Build ToolBoxV2 + features + upload │
│ depsy ToolBoxV2 all in on dependency manager cros platform │
│ db Database management │
│ docksh Interactive Docker shell │
│ docker-image Docker image management │
│ fbuild Build TB Feature for pip │
│ fl Feature loader │
│ flow Execute flows from file / directory │
│ gui Desktop GUI (feature: desktop) │
│ http_worker Single HTTP worker (feature: web) │
│ jsx JSX dev server │
│ local-cli Main tb local cli enty point │
│ LiveSync Real-time file synchronization │
│ llm-gateway LLM gateway proxy │
│ login Login to ToolBoxV2 │
│ logout Logout from ToolBoxV2 │
│ manifest Manifest configuration │
│ mcp MCP server for agents (feature: isaa) │
│ mkdocs ToolboxV2 Docs system for internal and external usage │
│ mods Module manager │
│ obs Observability layer │
│ p2p P2P mesh client │
│ registry Package registry management │
│ run Execute .tbx files (TBX Lang) │
│ services Service manager │
│ session Session management │
│ status System health check │
│ user User management │
│ version TB Version Manager │
│ venv Virtual environment management │
│ workers Worker system management (feature: web) │
│ ws_worker Single WebSocket worker (feature: web) │
│ ytss YouTube streaming service │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ TIPS & TRICKS ────────────────────────────────────────────────────────────┐
│ │
│ • Use `tb [command] -h` for detailed help on any runner command │
│ • Use `--sysPrint` for verbose output when debugging │
│ • Runner commands can take extra flags: `tb workers start --port 8080` │
│ • Use `-n` to run multiple instances with different names │
│ • Module functions are auto-discovered when using `-l` │
│ • Feature groups control which runners are available — check with │
│ `tb manifest` and enable what you need │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ GETTING HELP ─────────────────────────────────────────────────────────────┐
│ │
│ General Help: │
│ $ tb -h # Show main help │
│ $ tb --guide # Show this guide │
│ │
│ Command-Specific Help: │
│ $ tb workers -h # Worker system help │
│ $ tb mods -h # Module manager help │
│ $ tb db -h # Database help │
│ $ tb venv -h # Virtual environment help │
│ │
│ Module Information: │
│ $ tb -c [MODULE] # List module functions │
│ $ tb -v -l # Show all module versions │
│ │
└────────────────────────────────────────────────────────────────────────────┘
╔════════════════════════════════════════════════════════════════════════════╗
║ For more information visit: https://markinhaus.github.io/ToolBoxV2/ ║
╚════════════════════════════════════════════════════════════════════════════╝
""")
print(guide)
# =================== Modern Parser ===================
def parse_args():
"""Create modern argument parser with improved help text."""
# Handle runner commands early
runner_keys = RUNNER_KEYS
main_args, runner_name, runner_args = split_args_by_runner(sys.argv[1:], runner_keys)
# Temporarily adjust sys.argv for main parsing
if runner_name:
original_argv = sys.argv.copy()
sys.argv = [sys.argv[0]] + main_args
# Create parser with modern help
parser = argparse.ArgumentParser(
prog="tb",
description=textwrap.dedent("""
╔════════════════════════════════════════════════════════════════════════╗
║ ToolBoxV2 - CLI Interface ║
╚════════════════════════════════════════════════════════════════════════╝
A powerful, modular Python framework for building and managing tools.
Quick Start:
$ tb # Launch default for your profile
$ tb gui # Launch GUI
$ tb --guide # Show guide
$ tb -c [MOD] [FUNC] # Execute module function
"""),
epilog=textwrap.dedent("""
┌─ EXAMPLES ─────────────────────────────────────────────────────────────┐
│ │
│ Basic Usage: │
│ $ tb gui # Start GUI │
│ $ tb workers start # Start API server │
│ $ tb status # Check status │
│ │
│ Module Commands: │
│ $ tb -c helper # List helper functions │
│ $ tb -c CloudM Version # Get version │
│ $ tb -c CloudM get_mod_snapshot CloudM │
│ │
│ Advanced: │
│ $ tb --docker -m dev -p 8000 # Docker mode │
│ $ tb -c helper create-user bob bob@mail.com │
│ $ tb -c MyMod func --kwargs key=val # With kwargs │
│ │
│ Services: │
│ $ tb services # Manage services │
│ $ tb manifest # Edit manifest │
│ $ tb obs # Observability │
│ $ tb llm-gateway # LLM proxy │
│ │
└────────────────────────────────────────────────────────────────────────┘
For detailed guide: tb --guide
For command help: tb [command] -h
"""),
formatter_class=ModernHelpFormatter,
add_help=False, # We'll add custom help
)
# =================== EXTENSION COMMANDS ===================
extensions = parser.add_argument_group("🚀 Extension Commands")
extensions.add_argument(
"gui",
help="Launch graphical user interface",
nargs="?",
const=True,
default=False,
)
extensions.add_argument(
"mods",
help="Open interactive module manager",
nargs="?",
const=True,
default=False,
)
# extensions.add_argument("p2p",
# help="Manage P2P client (use: tb p2p -h)",
# nargs='?',
# const=True,
# default=False)
extensions.add_argument(
"db",
help="Database commands (use: tb db -h)",
nargs="?",
const=True,
default=False,
)
extensions.add_argument(
"venv",
help="Conda environment commands (use: tb venv -h)",
nargs="?",
const=True,
default=False,
)
extensions.add_argument(
"mcp", help="Start MCP server for agents", nargs="?", const=True, default=False
)
extensions.add_argument(
"browser",
help="Browser extension installer",
nargs="?",
const=True,
default=False,
)
extensions.add_argument(
"flow",
help="Execute flows/mod from file or directory",
nargs="?",
const=True,
default=False,
)
extensions.add_argument(
"run", help="Execute .tbx file and setup", nargs="?", const=True, default=False
)
extensions.add_argument(
"login", help="Login to ToolBoxV2", nargs="?", const=True, default=False
)
extensions.add_argument(
"logout", help="Logout from ToolBoxV2", nargs="?", const=True, default=False
)
extensions.add_argument(
"obs", help="ToolBoxV2 Observability layer", nargs="?", const=True, default=False
)
extensions.add_argument(
"status",
help="Display system status (DB, API, P2P, Workers)",
nargs="?",
const=True,
default=False,
)
extensions.add_argument(
"user", help="User management", nargs="?", const=True, default=False
)
extensions.add_argument(
"workers",
help="Worker management (use: tb workers -h)",
nargs="?",
const=True,
default=False,
)
extensions.add_argument(
"session",
help="Session management for workers (use: tb session -h)",
nargs="?",
const=True,
default=False,
)
extensions.add_argument(
"broker",
help="ZMQ event broker (use: tb broker -h)",
nargs="?",
const=True,
default=False,
)
extensions.add_argument(
"build",
help="Build tb and features + uploader (use: tb build -h)",
nargs="?",
const=True,
default=False,
)
extensions.add_argument(
"http_worker", help="HTTP worker", nargs="?", const=True, default=False
)
extensions.add_argument(
"ws_worker", help="WebSocket worker", nargs="?", const=True, default=False
)
extensions.add_argument(
"docksh", help="Start docker cli", nargs="?", const=True, default=False
)
extensions.add_argument(
"access", help="Access the tb cli from anywhere", nargs="?", const=True, default=False
)
# =================== CORE OPTIONS ===================
core = parser.add_argument_group("⚙️ Core Options")
core.add_argument("-h", "--help", action="store_true", help="Show this help message")
core.add_argument("--guide", action="store_true", help="Show interactive usage guide")
core.add_argument(
"-v",
"--get-version",
"--version",
action="store_true",
help="Display ToolBox version and modules (use with -l for all)",
)
core.add_argument(
"-init",
type=str,
metavar="TYPE",
help="Initialize ToolBoxV2 [main|config|manifest]",
)
core.add_argument(
"-l",
"--load-all-mod-in-files",
action="store_true",
help="Load all modules from mod directory",
)
core.add_argument(
"-c",
"--command",
nargs="*",
action="append",
metavar=("MODULE", "FUNCTION"),
help="Execute module command: -c MODULE FUNCTION [ARGS...]",
)
# =================== MODULE MANAGEMENT ===================
modules = parser.add_argument_group("📦 Module Management")
modules.add_argument(
"-i",
"--install",
type=str,
metavar="MODULE",
help="Install module or interface by name",
)
modules.add_argument(
"-u",
"--update",
type=str,
metavar="MODULE",
help="Update module or interface by name",
)
modules.add_argument(
"-r",
"--remove",
type=str,
metavar="MODULE",
help="Uninstall module or interface by name",
)
modules.add_argument(
"-m",
"--modi",
type=str,
metavar="MODE",
default=DEFAULT_MODI,
help=f"Interface mode (default: {DEFAULT_MODI})",
)
# =================== RUNTIME CONTROL ===================
runtime = parser.add_argument_group("🎮 Runtime Control")
runtime.add_argument(
"--kill", action="store_true", help="Terminate running ToolBox instance"
)
runtime.add_argument(
"-bg",
"--background-application",
action="store_true",
help="Run interface in background mode",
)
runtime.add_argument(
"-bgr",
"--background-application-runner",
action="store_true",
help="Background runner flag for current process",
)
runtime.add_argument(
"-fg",
"--live-application",
action="store_true",
help="Run proxy interface in foreground",
)
runtime.add_argument(
"--remote", action="store_true", help="Enable remote access mode"
)
runtime.add_argument(
"--debug", action="store_true", help="Enable debug mode with hot-reload"
)
# =================== DOCKER ===================
docker = parser.add_argument_group("🐳 Docker Options")
docker.add_argument(
"--docker", action="store_true", help="Run in Docker container [test|live|dev]"
)
docker.add_argument(
"--build", action="store_true", help="Build Docker image from local source"
)
# =================== NETWORKING ===================
network = parser.add_argument_group("🌐 Network Configuration")
network.add_argument(
"-n",
"--name",
type=str,
metavar="ID",
default="main",
help="Instance identifier (default: main)",
)
network.add_argument(
"-p",
"--port",
type=int,
metavar="PORT",
default=5000,
help="Interface port number (default: 5000)",
)
network.add_argument(
"-w",
"--host",
type=str,
metavar="HOST",
default="0.0.0.0",
help="Interface host address (default: 0.0.0.0)",
)
# =================== SERVICE MANAGEMENT ===================
services = parser.add_argument_group("🔧 Service Management")
services.add_argument(
"--init-sm",
action="store_true",
help=f"Service Manager for '{system()}' (auto-start/restart)",
)
services.add_argument(
"--sm",
action="store_true",
help=f"Service Manager for '{system()}' (auto-start/restart)",
)
services.add_argument(
"--lm", action="store_true", help="Log Manager (view/remove/edit logs)"
)
# =================== DATA OPERATIONS ===================
data_ops = parser.add_argument_group("💾 Data Operations (⚠️ Use with caution!)")
data_ops.add_argument(
"--delete-config", action="store_true", help="⚠️ Delete named config folder"
)
data_ops.add_argument(
"--delete-data", action="store_true", help="⚠️ Delete named data folder"
)
data_ops.add_argument(
"--delete-config-all",
action="store_true",
help="🚨 DANGER: Delete ALL config files (DATA LOSS!)",
)
data_ops.add_argument(
"--delete-data-all",
action="store_true",
help="🚨 DANGER: Delete ALL data folders (DATA LOSS!)",
)
# =================== DEVELOPMENT ===================
dev = parser.add_argument_group("🔬 Development & Testing")
dev.add_argument("--test", action="store_true", help="Run complete test suite")
dev.add_argument(
"--profiler", action="store_true", help="Profile all registered functions"
)
dev.add_argument(
"-sfe",
"--save-function-enums-in-file",
action="store_true",
help="Generate all_function_enums.py (requires -l)",
)
dev.add_argument(
"--sysPrint", action="store_true", help="Enable verbose system output"
)
# =================== ADVANCED ===================
advanced = parser.add_argument_group("🎯 Advanced Options")
advanced.add_argument(
"--kwargs",
nargs="*",
action="append",
default=[],
metavar="KEY=VALUE",
help="Pass key-value pairs: --kwargs key1=value1 key2=value2",
)
# Parse arguments
args = parser.parse_args()
# Handle custom help
if args.help:
parser.print_help()
os._exit(0)
# Handle guide
if args.guide:
show_interactive_guide()
os._exit(0)
# Add runner information
if runner_name:
args.runner_name = runner_name
args.runner_args = runner_args
sys.argv = original_argv
else:
args.runner_name = None
args.runner_args = []
# Parse kwargs
if args.kwargs:
kwargs_list = args.kwargs.copy()
args.kwargs = []
for k in kwargs_list:
args.kwargs.append(parse_kwargs(k))
if not args.kwargs or len(args.kwargs) == 0:
args.kwargs = [{}]
return args
def edit_logs():
name = input(f"Name of logger \ndefault {loggerNameOfToolboxv2}\n:")
name = name if name else loggerNameOfToolboxv2
def date_in_format(_date):
ymd = _date.split("-")
if len(ymd) != 3:
print("Not enough segments")
return False
if len(ymd[1]) != 2:
print("incorrect format")
return False
if len(ymd[2]) != 2:
print("incorrect format")
return False
return True
def level_in_format(_level):
if _level in ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"]:
_level = [50, 40, 30, 20, 10, 0][
["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"].index(_level)
]
return True, _level
try:
_level = int(_level)
except ValueError:
print("incorrect format pleas enter integer 50, 40, 30, 20, 10, 0")
return False, -1
return _level in [50, 40, 30, 20, 10, 0], _level
date = input(
"Date of log format : YYYY-MM-DD replace M||D with xx for multiple editing\n:"
)
while not date_in_format(date):
date = input("Date of log format : YYYY-MM-DD :")
level = input(
f"Level : {list(zip(['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET'], [50, 40, 30, 20, 10, 0], strict=False))}"
f" : enter number\n:"
)
while not level_in_format(level)[0]:
level = input("Level : ")
level = level_in_format(level)[1]
do = input("Do function : default remove (r) or uncoler (uc)")
if do == "uc":
edit_log_files(name=name, date=date, level=level, n=0, do=unstyle_log_files)
else:
edit_log_files(name=name, date=date, level=level, n=0)
def run_tests(test_path, args=None, cwd=None, venv=None):
if args is None:
args = ["-n", "auto"]
# Eigene venv nutzen wenn angegeben
if venv:
scripts_dir = "Scripts" if os.name == "nt" else "bin"
python = str(Path(venv) / scripts_dir / Path(sys.executable).name)
else:
python = sys.executable
command = [python, "-m", "pytest", test_path] + args
try:
result = subprocess.run(
command,
check=True,
encoding="cp850",
cwd=cwd # ins Subprojekt wechseln
)
return result.returncode == 0
except subprocess.CalledProcessError as e:
get_app().logger.error(f"Fehler beim Ausführen der Unittests: {e}")
return False
# try:
# from . import tb_root_dir
# command = ["npm", "test", "--prefix", tb_root_dir.as_posix()]
# result = subprocess.run(command, check=True, encoding='cp850', cwd=tb_root_dir)
# return result.returncode == 0
# except subprocess.CalledProcessError as e:
# print(f"Fehler beim Ausführen der npm-Tests: {e}")
# return False
# except Exception as e:
# print(f"Fehler beim Ausführen der npm-Tests:{e}")
# return False
async def setup_app(ov_name=None, App=TbApp):
args = parse_args()
if ov_name:
args.name = ov_name
abspath = os.path.dirname(os.path.abspath(__file__))
identification = args.name + "-" + node() + "\\"
data_folder = abspath + "\\.data\\"
config_folder = abspath + "\\.config\\"
info_folder = abspath + "\\.info\\"
os.makedirs(info_folder, exist_ok=True)
app_config_file = config_folder + identification
app_data_folder = data_folder + identification
if args.delete_config_all:
os.remove(config_folder)
if args.delete_data_all:
os.remove(data_folder)
if args.delete_config:
os.remove(app_config_file)
if args.delete_data:
os.remove(app_data_folder)
if args.test:
_reg_dir = tb_root_dir.parent / "tb-registry"
if not _reg_dir.is_dir():
print(f"tb --test needs the tb-registry checkout at {_reg_dir} (dev layout; not bundled in wheel installs)")
exit(1)
get_app().logger.info(f"Testing in {tb_root_dir}")
args_ = [w for w in args.kwargs[0].values()]
if args.name == "test":
args_.append('-x')
registry = False
if "registry" in args_:
registry = True
args_.remove("registry")
if registry:
exit(0 if run_tests("tests", args = args_.extend(["--cov=registry","--cov-report=html"]),cwd=tb_root_dir.parent/"tb-registry",
venv=tb_root_dir.parent/"tb-registry"/".venv") else 1)
exit(0
if
all([
run_tests('tests', args = args_.extend(["--cov=toolboxv2", "--cov-report=term-missing", "--cov-report=html"]), cwd=tb_root_dir),
run_tests("tests", args = args_.extend(["--cov=registry", "--cov-report=term-missing", "--cov-report=html"]),cwd=tb_root_dir.parent/"tb-registry",
venv=tb_root_dir.parent/"tb-registry"/".venv")
])
else 1
)
abspath = os.path.dirname(os.path.abspath(__file__))
info_folder = abspath + "\\.info\\pids\\"
os.makedirs(info_folder, exist_ok=True)
if not args.sysPrint and not (
args.debug or args.background_application_runner or args.install or args.kill
):
TbApp.sprint = lambda text, *_args, **kwargs: False
tb_app = get_app(from_="InitialStartUp", name=args.name, args=args, app_con=TbApp)
# Initialize FeatureManager with app reference
features_dir = tb_root_dir / "toolboxv2" / "features"
feature_manager = FeatureManager(app=tb_app, features_dir=str(features_dir))
tb_app.feature_manager = feature_manager
tb_app.loop = asyncio.get_running_loop()
if args.load_all_mod_in_files:
_min_info = await tb_app.load_all_mods_in_file()
with Spinner("Crating State"):
import threading
st = threading.Thread(
target=get_state_from_app,
args=(
tb_app,
os.getenv("TOOLBOXV2_REMOTE_BASE", "https://simplecore.app"),
"https://github.com/MarkinHaus/ToolBoxV2/tree/master/toolboxv2/",
),
daemon=True,
)
st.start()
# tb_app.print_functions()
if _min_info:
tb_app.print(_min_info)
tb_app.print(await tb_app.load_external_mods())
if args.update:
if args.update == "main":
await tb_app.save_load("CloudM")
tb_app.run_any("CloudM", "update_core")
run_command("npm run build:tbjs && npm run build:web")
else:
res = await tb_app.a_run_any(
"CloudM", "install", module_name=args.update, get_results=True
)
res.print()
if args.background_application_runner:
# Headless: no GUI notification. Log instead.
import logging as _bg_log
_bg_log.getLogger("bg_runner").info(
f"Starting background application tb # {' '.join(sys.argv[1:])}")
daemon_app = await DaemonApp(
tb_app, args.host, args.port if args.port != 5000 else 6587, t=False
)
tb_app.daemon_app = daemon_app
args.live_application = False
# A2: Report daemon presence to tray API
_tray_port = args.port if args.port != 5000 else 6587
_tray_url = f"http://{args.host}:{_tray_port}"
# Single source of truth for the pystray fallback and the Tauri SSE
# subscriber; without it they poll different ports.
os.environ["TB_TRAY_URL"] = _tray_url
_tray_client = None
try:
from toolboxv2.utils.workers.fast.tray_api import TrayClient
_tray_client = TrayClient("daemon", label="Daemon App")
_tray_client.report(running=True, pid=os.getpid(), url=_tray_url)
except Exception:
pass
async def check_and_start_fallback():
await asyncio.sleep(2.0) # Dem Tauri-Client Zeit zum Verbinden geben
from toolboxv2.utils.workers.fast.tray_api import has_active_subscribers
_subs = has_active_subscribers()
import logging as _lg
_lg.basicConfig(level=_lg.DEBUG)
_lg.info(f"check_and_start_fallback: has_active_subscribers={_subs}")
if not _subs:
# Guard: skip if tray service is already running via tb services
try:
from pathlib import Path as _P
from toolboxv2 import tb_root_dir as _root
_tray_pid_file = _P(_root) / ".info" / "pids" / "tray.pid"
if _tray_pid_file.exists():
_tpid = int(_tray_pid_file.read_text(encoding="utf-8").strip())
from toolboxv2.utils.extras.fallback_tray import _pid_alive
if _pid_alive(_tpid):
tb_app.sprint("Tray service already running, skipping fallback")
return
except Exception:
pass
from toolboxv2.utils.extras.fallback_tray import run_fallback_tray
tb_app.sprint("No Tauri listener detected. Launching Fallback Tray...")
# icon.run() is BLOCKING (Win32 GetMessage loop).
# Must run in a dedicated thread, NOT on the asyncio event loop.
import threading
tray_thread = threading.Thread(
target=run_fallback_tray,
args=(tb_app,),
name="FallbackTray",
daemon=False
)
tray_thread.start()
_lg.info(f"Tray thread started: {tray_thread.is_alive()}")
# B: Tray Auto-Start — einkommentiert
tb_app.run_bg_task_advanced(check_and_start_fallback)
elif args.background_application:
tb_app.alive = False
if not args.kill:
tb_app.sprint("Starting background application")
if args.background_application_runner:
tb_app.sprint("Already in background runner mode, not spawning again")
else:
print(f"Spawning background process...")
start(args.name, sys.argv, filename=f"{info_folder}bg-{args.name}.pid")
# NEU: Parent-Prozess sollte hier beenden
print(f"Background process spawned. Exiting parent.")
os._exit(0)
return tb_app, args
else:
pid_file = f"{info_folder}bg-{args.name}.pid"
if not os.path.exists(pid_file):
print(f"NO background process running for {args.name}")
else:
try:
_ = await ProxyApp(
tb_app,
args.host if args.host != "0.0.0.0" else "localhost",
args.port if args.port != 5000 else 6587,
timeout=4,
)
res = await _.verify()
if await _.exit_main() != "No data look later":
stop(pid_file, args.name)
except Exception as e:
print("Auto Stopping background application")
stop(pid_file, args.name)
# tb_app.debug_rains(e)
elif args.live_application:
try:
tb_app = await a_get_proxy_app(
tb_app,
host=args.host if args.host != "0.0.0.0" else "localhost",
port=args.port if args.port != 5000 else 6587,
key=os.getenv("TB_R_KEY", "user@phfrase"),
)
except:
import traceback
print(traceback.format_exc())
tb_app.sprint(
"No bg instance found starting local, to run in the background use -bg"
)
return tb_app, args
async def command_runner(tb_app, command, **kwargs):
if len(command) < 1:
tb_app.print_functions()
tb_app.print(
"minimum command length is 2 {module_name} {function_name} optional args... Com^C to exit"
)
return await tb_app.a_idle()
tb_app.print(f"Running command: {' '.join(command)} {kwargs}")
call = CallingObject().empty()
mod = tb_app.get_mod(command[0], spec="app")
if hasattr(mod, "async_initialized") and not mod.async_initialized:
await mod
call.module_name = command[0]
if len(command) < 2:
tb_app.print_functions(command[0])
tb_app.print(
"minimum command length is 2 {module_name} {function_name} optional args..."
)
return
call.function_name = command[1]
call.args = command[2:]
call.kwargs = kwargs
if (
"help" in call.kwargs
and call.kwargs.get("help", False)
or "h" in call.kwargs
and call.kwargs.get("h", False)
):
data = tb_app.get_function((call.module_name, call.function_name), metadata=True)
pprint.pprint(data)
return data
spec = "app" # if not args.live_application else tb_app.id
r = await tb_app.a_run_any(
(call.module_name, call.function_name),
tb_run_with_specification=spec,
args_=call.args,
get_results=True,
)
if asyncio.iscoroutine(r):
r = await r
if isinstance(r, asyncio.Task):
r = await r
tb_app.print("Running in", spec)
if hasattr(r, "print"):
r.print(full_data=True)
else:
tb_app.print(r)
return r
async def main(App=TbApp, do_exit=True):
"""Console script for toolboxv2."""
tb_app, args = await setup_app(App=App)
__version__ = get_version_from_pyproject()
abspath = os.path.dirname(os.path.abspath(__file__))
info_folder = abspath + "\\.info\\pids\\"
pid_file = f"{info_folder}{args.modi}-{args.name}.pid"
# =================== FIX: Manifest-Aware Auto-Login ===================
from toolboxv2.mods.CloudM.LogInSystem import auto_login_from_blob
tb_app.run_bg_task_advanced(auto_login_from_blob, tb_app)
# ======================================================================
if args.install:
report = await tb_app.a_run_any(
"CloudM", "install", module_name=args.install, get_results=True
)
report.print()
if args.init == "main":
from .setup_helper import setup_main
setup_main()
elif args.init == "config":
from .utils.clis.config_wizard import run_config_wizard
exit_code = run_config_wizard()
await a_save_closing_app()
exit(exit_code)
elif args.init == "manifest":
from toolboxv2 import build_manifest
from toolboxv2 import tb_root_dir
m = build_manifest("mini", save=True)
tb_app.print(f"Created manifest at {tb_root_dir}/tb-manifest.yaml (profile={m.app.profile.value})")
await a_save_closing_app()
exit(0)
elif args.init is not None:
tb_app.print(
"No init action specified valid options are ['main', 'config', 'manifest']"
)
await a_save_closing_app()
exit(1)
if args.lm:
edit_logs()
await a_save_closing_app()
exit(0)
if (
args.load_all_mod_in_files
or args.save_function_enums_in_file
or args.get_version
or args.profiler
or args.background_application_runner
or args.test
):
if args.save_function_enums_in_file:
tb_app.save_registry_as_enums("utils\\system", "all_functions_enums.py")
await a_save_closing_app()
return tb_app
if args.get_version:
print(
f"\n{' Version ':-^45}\n\n{Style.Bold(Style.CYAN(Style.ITALIC('RE'))) + Style.ITALIC('Simple') + 'ToolBox':<35}:{__version__:^10}\n"
)
for mod_name in tb_app.functions:
if isinstance(tb_app.functions[mod_name].get("app_instance"), MainTool):
print(
f"{mod_name:^35}:{tb_app.functions[mod_name]['app_instance'].version:^10}"
)
else:
try:
v = (
tb_app.functions[mod_name]
.get(list(tb_app.functions[mod_name].keys())[0])
.get("version", "unknown (functions only)")
.replace(f"{__version__}:", "")
)
except AttributeError:
v = "unknown"
print(f"{mod_name:^35}:{v:^10}")
print("\n")
await a_save_closing_app()
return tb_app
if _hook[0]:
if asyncio.iscoroutinefunction(_hook[0]):
await _hook[0](tb_app)
elif callable(_hook[0]):
_hook[0](tb_app)
if args.profiler:
profile_execute_all_functions(tb_app)
await a_save_closing_app()
return tb_app
if (
not args.kill
and not args.docker
and tb_app.alive
and not args.background_application
and "-m" in sys.argv
):
tb_app.save_autocompletion_dict()
if (
args.background_application_runner
and args.modi == "bg"
and hasattr(tb_app, "daemon_app")
):
await tb_app.daemon_app.online
if args.remote:
await tb_app.rrun_flows(args.modi, **args.kwargs[0])
flows_dict = flows_dict_func(s=args.modi, remote=False, flows_dict_=tb_app.flows)
if args.modi not in flows_dict:
flows_dict = {
**flows_dict,
**flows_dict_func(s=args.modi, remote=True, flows_dict_=tb_app.flows),
}
tb_app.set_flows(flows_dict)
if args.modi not in flows_dict:
print(
f"Modi : [{args.modi}] not found on device installed modi : {list(flows_dict.keys())}"
)
exit(1)
# open(f"./config/{args.modi}.pid", "w").write(app_pid)
await tb_app.run_flows(args.modi, **args.kwargs[0])
elif args.docker:
flows_dict = flows_dict_func("docker")
if "docker" not in flows_dict:
print("No docker")
return tb_app
flows_dict["docker"](tb_app, args)
elif args.kill and not args.background_application:
if not os.path.exists(pid_file):
print("You must first run the mode")
else:
try:
from toolboxv2.utils.clis.cli_worker_manager import WorkerManager
from toolboxv2.utils.workers.config import load_config
config = load_config(None)
WorkerManager(config).stop_all()
except Exception as e:
print(Style.YELLOW(f"Error stopping workers manager: {e}"))
with open(pid_file, encoding="utf8") as f:
app_pid = f.read()
print(f"Exit app {app_pid}")
if system() == "Windows":
os.system(f"taskkill /pid {app_pid} /F")
else:
os.system(f"kill -9 {app_pid}")
if args.command and not args.background_application:
for command in args.command:
await command_runner(
tb_app,
command,
**args.kwargs[
args.command.index(command)
if args.command.index(command) < len(args.kwargs) - 1
else 0
],
)
if args.live_application and args.debug:
hide = tb_app.hide_console()
if hide is not None:
await hide
if os.path.exists(pid_file):
os.remove(pid_file)
if do_exit and not tb_app.called_exit[0]:
await a_save_closing_app()
return tb_app
return tb_app
CATEGORIES = {
"core": {
"title": "📦 Core Operations",
"runners": {
"user": "User management (levels, MinIO credentials, key rotation)",
"run": "Execute .tbx files using TBX Language",
"db": "Database management (standalone, desktop, mobile SQLite, active-active sync)",
"workers": "Orchestrate workers (HTTP, WS, Broker) [feature: web]",
"services": "Service manager for auto-start/restart and system service integration",
"registry": "Manage packages & mods on SimpleCore Registry (login, search, publish)",
}
},
"utility": {
"title": "⚙️ Utility Commands",
"runners": {
"login": "Browser-based login to SimpleCore Hub",
"logout": "End current authentication session",
"status": "Check complete system status & health (DB, API, P2P, Workers)",
"session": "Manage JWT, cookie, cluster and worker secrets",
"manifest": "Interactive manifest manager (enable/disable feature groups)",
}
},
"dev": {
"title": "🔬 Developer Tools & Interfaces",
"runners": {
"venv": "Manage Python environments (conda, uv, native)",
"mcp": "Start Model Context Protocol server for AI agents [feature: isaa]",
"gui": "Launch Tauri-based graphical desktop app [feature: desktop]",
"browser": "Compile, install and package browser extension",
"jsx": "Start JSX development server with live preview",
"p2p": "P2P client chat, file transfer & voice support",
"analyze": "Analyze python & JS code statically and at runtime",
}
},
"infra": {
"title": "🌐 Infrastructure & Pipelines",
"runners": {
"broker": "ZMQ event broker for worker orchestration",
"http_worker": "Start a single HTTP worker instance [feature: web]",
"ws_worker": "Start a single WebSocket worker instance [feature: web]",
"docksh": "Launch interactive Docker CLI shell",
"access": "Manage global system-wide 'tb' CLI access path",
"build": "Build and package ToolBoxV2 + features for pip/upload",
"fl": "Unpack and load feature packs",
"docker-image": "Build ToolBoxV2 Docker images & compose stacks",
"obs": "E2E observability layer (MinIO/S3 + OpenObserve integration)",
"mkdocs": "Scan codebase and compile MkDocs documentation with DocMap",
"ytss": "YouTube streaming & broadcast service",
"LiveSync": "Real-time file synchronization client",
}
}
}
async def run_interactive_help():
"""Launches a 2-tiered interactive keypad help menu to browse runners and view docstrings."""
from .utils.clis.cli_input import menu_select_async
import importlib
import textwrap
while True:
cat_options = [
(cat_key, f"{cat_data['title']} ({len(cat_data['runners'])} commands)")
for cat_key, cat_data in CATEGORIES.items()
]
cat_options.append(("exit", "❌ Exit help"))
selected_cat = await menu_select_async(
cat_options,
title="🌩️ Select Help Category:",
hint="↑/↓ or W/S · Enter · q to exit"
)
if selected_cat in (None, "exit"):
break
while True:
cat_data = CATEGORIES[selected_cat]
runner_options = [
(run_key, f"{run_key:<15} - {desc}")
for run_key, desc in cat_data["runners"].items()
]
runner_options.append(("back", "↩ Back to Categories"))
selected_runner = await menu_select_async(
runner_options,
title=f"{cat_data['title']} - Select command to view help or run:",
hint="↑/↓ or W/S · Enter · q to back"
)
if selected_runner in (None, "back"):
break
action_options = [
("info", "ℹ️ Show detailed help / docstring"),
("run", "🚀 Run command directly"),
("back", "↩ Back")
]
action = await menu_select_async(
action_options,
title=f"Command '{selected_runner}': What would you like to do?",
hint="↑/↓ or W/S · Enter · q to back"
)
if action == "info":
desc = cat_data["runners"][selected_runner]
doc = None
try:
# Resilient module-level docstring loader for lazy-loaded lambdas
if selected_runner == "venv":
mod = importlib.import_module("toolboxv2.utils.system.venv_runner")
doc = mod.__doc__
elif selected_runner == "db":
mod = importlib.import_module("toolboxv2.utils.clis.db_cli_manager")
doc = mod.__doc__
elif selected_runner == "p2p":
mod = importlib.import_module("toolboxv2.utils.clis.tcm_p2p_cli")
doc = mod.__doc__
elif selected_runner == "registry":
mod = importlib.import_module("toolboxv2.utils.clis.cli_registry")
doc = mod.__doc__
elif selected_runner == "services":
mod = importlib.import_module("toolboxv2.utils.clis.service_manager")
doc = mod.__doc__
elif selected_runner == "manifest":
mod = importlib.import_module("toolboxv2.utils.clis.manifest_cli")
doc = mod.__doc__
elif selected_runner == "llm-gateway":
mod = importlib.import_module("toolboxv2.utils.clis.llm_gateway_cli")
doc = mod.__doc__
elif selected_runner == "docker-image":
mod = importlib.import_module("toolboxv2.utils.clis.docker_image_cli")
doc = mod.__doc__
elif selected_runner == "obs":
mod = importlib.import_module("toolboxv2.utils.clis.observability_helper")
doc = mod.__doc__
elif selected_runner == "mkdocs":
mod = importlib.import_module("toolboxv2.utils.clis.cli_mkdocs")
doc = mod.__doc__
elif selected_runner == "gui":
mod = importlib.import_module("toolboxv2.utils.clis.tauri_cli")
doc = mod.__doc__
else:
runners_dict = runner_setup()
func = runners_dict.get(selected_runner)
if func:
doc = func.__doc__
except Exception:
pass
detail_text = textwrap.dedent(doc).strip() if doc else "No detailed docstring available."
print("\033[2J\033[H")
print("═" * 70)
print(f" Command Help: {selected_runner}")
print("═" * 70)
print(f"Summary: {desc}\n")
print("Details / Docstring:")
print("-" * 70)
print(detail_text)
print("-" * 70)
input("\nPress Enter to return...")
elif action == "run":
runners_dict = runner_setup()
func = runners_dict.get(selected_runner)
if func:
print("\033[2J\033[H")
print(f"🚀 Launching runner: {selected_runner}...\n")
import sys
sys.argv = [sys.argv[0]]
res = func()
if asyncio.iscoroutine(res):
await res
sys.exit(0)
def runner_setup():
def helper_gui():
__import__("toolboxv2.utils.clis.tauri_cli", fromlist=["main"]).main()
async def status_helper():
"""Fixed status: nutzt _find_cli_session statt kaputtes list_blobs."""
import sys
print("🔍 ToolBoxV2 System Status")
print("═" * 40)
try:
from toolboxv2.mods.CloudM.LogInSystem import _find_cli_session, cli_status
app = get_app("status_check")
result = _find_cli_session(app)
if result:
sd = result["session_data"]
username = result["username"]
auth_time = sd.get("authenticated_at", 0)
time_str = "Unknown"
if auth_time:
elapsed = time.time() - auth_time
h, m = int(elapsed // 3600), int((elapsed % 3600) // 60)
time_str = f"{h}h {m}m ago"
print(f"🔐 Authentication: ✅ Logged in as {username}")
print(f" Provider: {sd.get('provider', 'unknown')}")
print(f" Session: {time_str}")
else:
print("🔐 Authentication: ❌ Not logged in")
await cli_status(app)
from toolboxv2.mods.CloudM.mini import get_service_status
print(get_service_status(app.info_dir.replace(app.id, "")))
except Exception as e:
print(f"🔐 Authentication: ❌ Status check failed: {e}")
_run_server_overview()
_run_business_overview()
print()
sys.argv = ["db", "status"]
await cli_db_runner()
print()
from toolboxv2.utils.clis.cli_printing import Style
print(Style.GREY("─" * 25))
sys.argv = ["workers", "status"]
if callable(cli_worker_manager):
cli_worker_manager()
else:
print("workers: n/a (web feature disabled)")
sys.argv = ["p2p", "status"]
if callable(cli_tcm_runner):
cli_tcm_runner()
else:
print("p2p: n/a")
async def cli_web_login():
"""Enhanced CLI web login entry point with modern visual feedback"""
import argparse
# Clear screen for clean start
print("\033[2J\033[H")
parser = argparse.ArgumentParser(
prog="tb login",
description="🔐 Login to ToolBoxV2",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
╔════════════════════════════════════════════════════════════════════════════╗
║ Login Options ║
╠════════════════════════════════════════════════════════════════════════════╣
║ ║
║ $ tb login ║
║ $ tb login --status ║
║ ║
╚════════════════════════════════════════════════════════════════════════════╝
""",
)
parser.add_argument(
"--status",
help="Force remote login to SimpleCore Hub",
action="store_true",
default=False,
)
args = parser.parse_args()
# Visual feedback
print(
"╔════════════════════════════════════════════════════════════════════════════╗"
)
print(
"║ 🔐 ToolBoxV2 Authentication ║"
)
print(
"╚════════════════════════════════════════════════════════════════════════════╝\n"
)
async def helper():
app = get_app("CloudM.cli_web_login")
if args.status:
return await app.a_run_any("CloudM", "cli_status")
res = await app.a_run_any(
"CloudM",
"cli_login",
)
return res
return await helper()
async def logout():
app = get_app("CloudM.cli_web_login")
return await app.a_run_any("CloudM", "cli_logout")
async def run_flow_from_file_or_load_all_flows_and_mods_from_dir(app=None):
from toolboxv2 import init_cwd
if app is None:
app = get_app("app.Flows")
parser = argparse.ArgumentParser(
prog="tb flow",
description="Run flow from file or load all flows and mods from dir",
)
parser.add_argument("--flow", help="Run flow from file", default=None)
parser.add_argument(
"--remote", help="Force remote login", action="store_true", default=False
)
args = parser.parse_args()
app.print(Style.VIOLET2(f"Loading externals from {init_cwd}"))
await app.load_all_mods_in_file(init_cwd)
app.flows = flows_dict_func(args.flow or ".py", args.remote, init_cwd, app.flows)
if args.flow:
await app.run_flows(args.flow)
run_c = run_flow_from_file_or_load_all_flows_and_mods_from_dir
async def mods_manager():
import argparse
import sys
# App laden
app = get_app("CloudM.ModManager")
# Parser konfigurieren
parser = argparse.ArgumentParser(
prog="tb mods",
description="Manage ToolBox modules or start Dev Runner",
)
# 1. Das erste Argument: Entweder ein Befehl (list, create...) ODER ein Modulname
parser.add_argument(
"command",
nargs="?",
default="",
help="Command (list, create, manager...) OR Module Name for Dev Runner"
)
# 2. Das zweite Argument: Ziel-Modul (nur nötig für build, install, create)
parser.add_argument(
"module_name",
nargs="?",
default="",
help="Target module name (for build, install, create)"
)
# 3. Flags für 'create' und 'gen-configs'
# Wir fügen hier alle möglichen Flags hinzu, damit sie in **kwargs landen
parser.add_argument("--type", help="Module type (for create)", default=None)
parser.add_argument("--desc", help="Description", default=None)
parser.add_argument("--version", help="Version", default=None)
parser.add_argument("--location", help="Custom path", default=None)
parser.add_argument("--author", help="Author name", default=None)
# Boolean Flags (store_true)
parser.add_argument("--external", action="store_true", help="Create external module")
parser.add_argument("--no-config", action="store_true", help="Skip tbConfig creation")
parser.add_argument("--force", action="store_true", help="Force overwrite")
parser.add_argument("--non-interactive", action="store_true", help="Skip confirmations")
parser.add_argument("--no-backup", action="store_true", help="Skip backups")
parser.add_argument(
"-p",
"--port",
type=int,
metavar="PORT",
default=8080,
help="Interface port number (default: 8080)",
)
# Argumente parsen
args = parser.parse_args()
# Wir bauen die kwargs zusammen.
# Wichtig: Wir filtern 'command' und 'module_name' heraus, da diese positionsabhängig übergeben werden.
# Wir filtern None-Werte heraus, damit Defaults der Zielfunktion greifen.
clean_kwargs = {
k: v for k, v in vars(args).items()
if k not in ["command", "module_name"] and v is not None and v is not False
}
# Boolean Flags müssen explizit übergeben werden, wenn sie True sind
for flag in ["external", "no_config", "force", "non_interactive", "no_backup"]:
if getattr(args, flag, False):
# argparse macht aus "no-config" -> "no_config", wir müssen das mappen wenn nötig
# In main() prüfst du "if '--external' in kwargs", daher müssen wir den Key anpassen
# oder die Logik in main() auf kwargs.get('external') ändern.
# Um kompatibel zu deiner main() Logik ("--flag" in kwargs) zu bleiben:
key_name = "--" + flag.replace("_", "-")
clean_kwargs[key_name] = True
# Ausführung an CloudM -> mods übergeben
await app.a_run_any(
"CloudM",
"mods",
command=args.command,
module_name=args.module_name,
**clean_kwargs
)
async def registry_server():
app = get_app("CloudM.RegistryServer")
await app.a_run_any("CloudM.RegistryServer", "start")
async def _run_docksh():
from toolboxv2.mods.ContainerManager.cli import main as d_cli
await d_cli()
runner = {
"help": run_interactive_help,
"!": run_interactive_help,
"venv": lambda: __import__(
"toolboxv2.utils.system.venv_runner", fromlist=["main"]
).main(),
"db": cli_db_runner,
"gui": helper_gui,
"local-cli": lambda: __import__(
"toolboxv2.utils.clis.local_cli", fromlist=["main"]
).main(),
"p2p": cli_tcm_runner,
"status": status_helper,
"analyze": lambda: __import__(
"toolboxv2.utils.extras.code_analyzer.tb_analyze", fromlist=["_cli"]
)._cli(),
"depsy": lambda: __import__(
"toolboxv2.utils.extras.depsy.depmanager", fromlist=["_cli"]
)._cli(),
"browser": lambda: __import__(
"toolboxv2.tb_browser.install", fromlist=["main"]
).main(),
"mcp": cli_mcp_server,
"login": cli_web_login,
"logout": logout,
"flow": run_c,
"mods": mods_manager,
"registry": lambda: __import__(
"toolboxv2.utils.clis.cli_registry", fromlist=["registry"]
).registry(),
"run": lambda: __import__(
"toolboxv2.utils.clis.tb_lang_cli", fromlist=["cli_tbx_main"]).cli_tbx_main(),
"user": user_manager_main,
"default": interactive_user_dashboard,
"workers": cli_worker_manager,
"session": cli_session,
"broker": cli_event,
"mkdocs": lambda: __import__(
"toolboxv2.utils.clis.cli_mkdocs", fromlist=["run"]
).run(get_app("app.mkdocs"), sys.argv),
"http_worker": cli_http_worker,
"obs": lambda: __import__(
"toolboxv2.utils.clis.observability_helper", fromlist=["main"]
).main(),
"access": lambda: __import__(
"toolboxv2.__genv__", fromlist=["main"]
).main(),
"ws_worker": cli_ws_worker,
"services": lambda: __import__(
"toolboxv2.utils.clis.service_manager", fromlist=["cli_services"]
).cli_services(),
"manifest": lambda: __import__(
"toolboxv2.utils.clis.manifest_cli", fromlist=["cli_manifest_main"]
).cli_manifest_main(),
"llm-gateway": lambda: __import__(
"toolboxv2.utils.clis.llm_gateway_cli", fromlist=["cli_llm_gateway"]
).cli_llm_gateway(),
"build": lambda: __import__(
"toolboxv2.utils.system.ci.ci_build", fromlist=["main"]
).main(),
"version": lambda: __import__(
"toolboxv2.utils.system.ci.ci_version", fromlist=["main"]
).main(),
"docksh": _run_docksh,
"docker-image": lambda: __import__(
"toolboxv2.utils.clis.docker_image_cli", fromlist=["main"]
).main(),
"jsx": lambda: __import__(
"toolboxv2.utils.clis.cli_jsx_server", fromlist=["main"]
).main(),
"fl": lambda: __import__(
"toolboxv2.feature_loader", fromlist=["main"]
).main(),
"ytss": lambda: __import__(
"toolboxv2.ytss", fromlist=["main"]
).main(),
"LiveSync": lambda: __import__(
"toolboxv2.mods.CloudM.LiveSync.cli", fromlist=["main"]
).main(),
"tray": lambda: __import__(
"toolboxv2.utils.extras.fallback_tray", fromlist=["run_tray_service"]
).run_tray_service(),
}
runner = _build_guarded_runners(runner)
return runner
def _build_guarded_runners(runner: dict) -> dict:
"""
Ersetze None-Runner durch hilfreiche Fehlermeldungen.
Wird am Ende von runner_setup() aufgerufen.
"""
def _missing_feature(name: str):
def _handler():
print(f"\n❌ Feature '{name}' is not enabled.")
print(f" Enable with: tb manifest enable {name}")
print(f" Install deps: pip install toolboxv2[{name}]\n")
import sys; os._exit(1)
return _handler
# web-abhängige Runner
if not _WEB_AVAILABLE:
for key in ("workers", "http_worker", "ws_worker"):
runner[key] = _missing_feature("web")
# isaa-abhängige Runner
if not _feature_enabled("isaa"):
runner["mcp"] = _missing_feature("isaa")
# desktop-abhängige Runner
if not _feature_enabled("desktop"):
runner["gui"] = _missing_feature("desktop")
return runner
from toolboxv2.utils.extras.code_analyzer.profiler import profile_code
@profile_code(
sort_by="cumulative",
top_n=30,
graph=True,
graph_file="import_graph.html", # → Obsidian
group_depth=5,min_time=0.01,
)
def main_runner():
# The fuck is uv not PyO3 compatible
sys.excepthook = sys.__excepthook__
# Windows: Use SelectorEventLoop for ZMQ compatibility
if sys.platform == "win32":
# asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
# IPython special case -> refactor to MocIpy new flow
# Service Manager special case - start all auto-start services and exit
if "--sm" in sys.argv:
from toolboxv2.utils.clis.service_manager import run_service_manager_startup
if 'init' in sys.argv:
if system() == "Linux":
setup_service_linux()
elif system() == "Windows":
asyncio.run(setup_service_windows())
else:
print(
f"Service manager not supported on this platform {system()}"
)
else:
rc = run_service_manager_startup()
os._exit(rc if isinstance(rc, int) else 0)
elif "--print-root" in sys.argv:
from toolboxv2 import tb_root_dir
print(str(tb_root_dir.parent))
os._exit(0)
# Normale Main-App
else:
# Clear screen for clean start
runner = runner_setup()
runner_keys = list(RUNNER_KEYS)
main_args, runner_name, runner_args = split_args_by_runner(
sys.argv[1:], runner_keys
)
# Check for unknown runner (argument that looks like a runner but isn't in RUNNER_KEYS)
# This catches cases like `tb xyz` where xyz is not a valid runner
unknown_runner = None
if runner_name is None and len(sys.argv) > 1:
# Check if first non-flag argument could be an unknown runner
continue_flag = False
for arg in sys.argv[1:]:
if arg in ["-c", "-v", "-m"]:
runner_name = ""
break
if continue_flag:
continue_flag = False
continue
if not arg.startswith("-") and "=" not in arg:
# This looks like a runner name but wasn't found in RUNNER_KEYS
unknown_runner = arg
break
else:
continue_flag = True
if unknown_runner:
print(f"\n\033[1;31m❌ Unknown command: '{unknown_runner}'\033[0m")
print(f"\n\033[1mAvailable commands:\033[0m")
# Group runners by category for better display
core_runners = ["user", "run", "db", "workers", "services", "registry"]
util_runners = ["login", "logout", "status", "session", "manifest"]
dev_runners = ["venv", "mcp", "gui", "browser"]
other_runners = [
r
for r in runner_keys
if r not in core_runners + util_runners + dev_runners
]
print(f" \033[36mCore:\033[0m {', '.join(core_runners)}")
print(f" \033[36mUtility:\033[0m {', '.join(util_runners)}")
print(f" \033[36mDev:\033[0m {', '.join(dev_runners)}")
if other_runners:
print(f" \033[36mOther:\033[0m {', '.join(other_runners)}")
print(f"\n\033[2mUse 'tb <command> --help' for more information.\033[0m\n")
os._exit(1)
try:
loop = asyncio.new_event_loop()
if runner_name == "flows":
_hook[0] = runner["flows"]
runner["flows"] = lambda: None
if runner_name == "mcp":
TbApp.print = lambda *a, **k: None
async def main_helper(runner_name):
# Default to interactive dashboard if no runner specified
# This applies to: `tb`, `tb -l`, `tb --debug`, etc.
if runner_name is None and not '--test' in sys.argv:
profile = _get_profile()
if profile is None:
# Silent auto-init: create minimal manifest, no interaction
from toolboxv2.utils.manifest import ManifestLoader
from toolboxv2 import tb_root_dir
_m = ManifestLoader(tb_root_dir).load_or_create_default()
profile = _m.app.profile.value if _m.app.profile else "local"
if profile == "local":
runner_name = "local-cli" # zero-friction default (F1)
elif profile == "consumer":
runner_name = "gui" # web UI in Tauri
elif profile == "homelab":
runner_name = "gui" # web UI in Tauri/browser — same surface
elif profile == "developer":
runner_name = "local-cli" # terminal version of the same screens
elif profile == "server":
_run_server_overview()
if len(sys.argv) < 2:
return
elif profile == "business":
_run_business_overview()
if len(sys.argv) < 2:
return
else:
runner_name = "default" # fallback to legacy dashboard
app = await main(TbApp, runner_name == "default")
# Wenn Runner angegeben
if app.alive and runner_name:
if not app.manifest.observability.slow_on_init:
app.run_bg_task_advanced(app.observability_health_check_and_anabel)
# app.run_bg_task_advanced(app._initialize_network)
# if app.ping is None:
# await asyncio.sleep(1)
# Setze sys.argv für Runner
sys.argv = [sys.argv[0]] + runner_args
try:
res = runner[runner_name]()
if asyncio.iscoroutine(res):
await res
except KeyboardInterrupt:
return
elif runner_name and not app.alive and runner_name != "default":
if not '-bg' in sys.argv:
raise ValueError(f"FIX DAS SOFORT WENN RUNNER {runner_name} muss {app.alive=} == TRUE sein")
await app.a_exit()
loop.run_until_complete(main_helper(runner_name))
except KeyboardInterrupt:
return
def get_real_python_executable():
try:
import ctypes
# Set the return type for the function call
ctypes.pythonapi.Py_GetProgramFullPath.restype = ctypes.c_char_p
exe_path = ctypes.pythonapi.Py_GetProgramFullPath()
print(exe_path)
if exe_path:
return exe_path.decode("utf-8")
except Exception as e:
# If anything goes wrong, fall back to sys.executable
print(f"Error detecting real executable: {e}")
return sys.executable
def server_helper(instance_id: str = "main", db_mode=None):
# real_exe = get_real_python_executable()
from pathlib import Path
sys.executable = str(Path(os.getenv("PYTHON_EXECUTABLE", sys.executable)))
print("Using Python executable env:", sys.executable)
loop = asyncio.new_event_loop()
sys.argv.append("-l")
sys.argv.append("--debug")
app, _ = loop.run_until_complete(setup_app(instance_id))
app.loop = loop
if db_mode is None:
db_mode = os.getenv("DB_MODE_KEY", "LC")
app.is_server = True
# db = app.get_mod("DB")
# db.edit_cli(db_mode)
# db.initialize_database()
# execute all flows starting with server as bg tasks
def task():
flows_dict = flows_dict_func(remote=False)
app.set_flows(flows_dict)
for flow in flows_dict:
if flow.startswith("server"):
print(f"Starting server flow: {flow}")
app.run_bg_task_advanced(app.run_flows, flow)
# app.run_bg_task_advanced(task)
return app
if __name__ == "__main__":
# print("STARTED START FROM __main__")
main_runner()
os._exit(0)
"""Console script for toolboxv2."""
import argparse
import asyncio
import pprint
# Import default Pages
import textwrap
import time
from contextlib import redirect_stdout
from functools import wraps
from pathlib import Path
from platform import node, system
from typing import List, Tuple
from dotenv import load_dotenv
load_dotenv()
import os
import sys
# ponytail: Nuitka no_site (standalone default) removes builtin exit()/quit().
# Remove this shim when --python-flag=no_site is disabled in build config.
exit = sys.exit
from toolboxv2 import tb_root_dir, _feature_enabled
from toolboxv2.utils.system.feature_manager import FeatureManager
from toolboxv2.flows import flows_dict as flows_dict_func
try:
from toolboxv2.setup_helper import run_command
except ImportError as e:
run_command = lambda x:print(x, f"run_command error {e}")
from toolboxv2.utils import get_app
from toolboxv2.utils.clis.db_cli_manager import cli_db_runner
from toolboxv2.utils.clis.user_manager import main as user_manager_main
from toolboxv2.utils.clis.tcm_p2p_cli import cli_tcm_runner
from toolboxv2.utils.extras.Style import Spinner, Style
from toolboxv2.utils.system import CallingObject, get_state_from_app
from toolboxv2.utils.system.main_tool import MainTool, get_version_from_pyproject
from toolboxv2.utils.system.getting_and_closing_app import a_save_closing_app
from toolboxv2.utils.system.exe_bg import run_executable_in_background
from toolboxv2.utils.clis.cli_input import menu_select, menu_select_async
from .utils.toolbox import App as TbApp
# ── WEB-Feature: workers, proxy, dashboard ────────────────────────────────────
_WEB_AVAILABLE = False
cli_worker_manager = None
interactive_user_dashboard = None
DaemonApp = None
ProxyApp = None
a_get_proxy_app = None
cli_event = cli_http_worker = cli_session = cli_ws_worker = None
if _feature_enabled("web"):
try:
from toolboxv2.utils.clis.cli_worker_manager import main as cli_worker_manager
from toolboxv2.utils.clis.user_dashboard import interactive_user_dashboard
from toolboxv2.utils.daemon import DaemonApp
from toolboxv2.utils.proxy import ProxyApp
from toolboxv2.utils.system.getting_and_closing_app import a_get_proxy_app
from toolboxv2.utils.workers import (
cli_event, cli_http_worker, cli_session, cli_ws_worker,
)
_WEB_AVAILABLE = True
except ImportError as _e:
import sys
import traceback
traceback.print_exc()
print(f"[web] Import failed: {_e}", file=sys.stderr)
# ── ISAA-Feature: MCP server ──────────────────────────────────────────────────
cli_mcp_server = None
if _feature_enabled("isaa"):
try:
from .mcp_server.__main__ import main as cli_mcp_server
except ImportError:
cli_mcp_server = lambda: None
else:
cli_mcp_server = lambda: None
# Set UTF-8 encoding for Windows console (place at top of your script)
if sys.platform == "win32":
import ctypes
ctypes.windll.kernel32.SetConsoleOutputCP(65001)
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8")
sys.stderr.reconfigure(encoding="utf-8")
DEFAULT_MODI = "cli"
_hook = [None]
try:
import hmr
HOT_RELOADER = True
except ImportError:
Xet Storage Details
- Size:
- 119 kB
- Xet hash:
- beec9bfcd9a39454ec88395c723da32b1a4cade94dfed5ab50cc5cce9c704216
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.