|
| 1 | +import json |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | +from typing import List |
| 5 | + |
| 6 | +from .base import CLITool |
| 7 | + |
| 8 | + |
| 9 | +class BlackboxTool(CLITool): |
| 10 | + """Blackbox AI CLI wrapper.""" |
| 11 | + |
| 12 | + command_name = "blackbox" |
| 13 | + tool_key = "blackbox" |
| 14 | + install_description = "Blackbox AI CLI" |
| 15 | + |
| 16 | + def _write_blackbox_config( |
| 17 | + self, endpoint_config: dict, model: str, endpoint_name: str |
| 18 | + ) -> Path: |
| 19 | + """ |
| 20 | + Write Blackbox CLI configuration to ~/.blackboxcli/settings.json. |
| 21 | +
|
| 22 | + Args: |
| 23 | + endpoint_config: Endpoint configuration dictionary |
| 24 | + model: Selected model name |
| 25 | + endpoint_name: Name of the endpoint |
| 26 | +
|
| 27 | + Returns: |
| 28 | + Path to the written settings file |
| 29 | + """ |
| 30 | + config_dir = Path.home() / ".blackboxcli" |
| 31 | + config_file = config_dir / "settings.json" |
| 32 | + |
| 33 | + # Preserve existing configuration if it exists |
| 34 | + existing_config = {} |
| 35 | + if config_file.exists(): |
| 36 | + try: |
| 37 | + existing_config = json.loads(config_file.read_text(encoding="utf-8")) |
| 38 | + except Exception: |
| 39 | + pass |
| 40 | + |
| 41 | + # Build the Blackbox configuration |
| 42 | + blackbox_config = { |
| 43 | + "security": { |
| 44 | + "auth": { |
| 45 | + "selectedType": "blackbox-api", |
| 46 | + "selectedProvider": "blackbox", |
| 47 | + "blackbox": { |
| 48 | + "apiKey": endpoint_config.get("actual_api_key", ""), |
| 49 | + "baseUrl": endpoint_config.get("endpoint", "https://api.blackbox.ai"), |
| 50 | + "model": model, |
| 51 | + }, |
| 52 | + } |
| 53 | + }, |
| 54 | + "model": {"name": model}, |
| 55 | + } |
| 56 | + |
| 57 | + # Merge with existing configuration to preserve other settings |
| 58 | + if isinstance(existing_config, dict): |
| 59 | + # Deep merge security.auth settings |
| 60 | + if "security" not in existing_config: |
| 61 | + existing_config["security"] = {} |
| 62 | + if "auth" not in existing_config["security"]: |
| 63 | + existing_config["security"]["auth"] = {} |
| 64 | + |
| 65 | + existing_config["security"]["auth"].update(blackbox_config["security"]["auth"]) |
| 66 | + existing_config["model"] = blackbox_config["model"] |
| 67 | + final_config = existing_config |
| 68 | + else: |
| 69 | + final_config = blackbox_config |
| 70 | + |
| 71 | + # Write the configuration |
| 72 | + config_dir.mkdir(parents=True, exist_ok=True) |
| 73 | + with open(config_file, "w", encoding="utf-8") as f: |
| 74 | + json.dump(final_config, f, indent=2, ensure_ascii=False) |
| 75 | + |
| 76 | + return config_file |
| 77 | + |
| 78 | + def run(self, args: List[str] = None) -> int: |
| 79 | + """ |
| 80 | + Configure and launch the Blackbox AI CLI. |
| 81 | +
|
| 82 | + Args: |
| 83 | + args: List of arguments to pass to the Blackbox CLI |
| 84 | +
|
| 85 | + Returns: |
| 86 | + Exit code of the Blackbox CLI process |
| 87 | + """ |
| 88 | + args = args or [] |
| 89 | + |
| 90 | + # Set up endpoint and model for Blackbox |
| 91 | + success, result = self._validate_and_setup_tool("blackbox", select_multiple=False) |
| 92 | + if not success: |
| 93 | + return 1 |
| 94 | + |
| 95 | + # Extract endpoint configuration and selected model |
| 96 | + endpoint_config, endpoint_name, model = result |
| 97 | + |
| 98 | + # Write Blackbox configuration file |
| 99 | + config_file = self._write_blackbox_config(endpoint_config, model, endpoint_name) |
| 100 | + print(f"Blackbox configuration written to {config_file}") |
| 101 | + |
| 102 | + # Set up environment variables for Blackbox based on tools.yaml configuration |
| 103 | + env = os.environ.copy() |
| 104 | + env["BLACKBOX_API_KEY"] = endpoint_config["actual_api_key"] |
| 105 | + env["BLACKBOX_API_BASE_URL"] = endpoint_config["endpoint"] |
| 106 | + env["BLACKBOX_API_MODEL"] = model |
| 107 | + self._set_node_tls_env(env) |
| 108 | + |
| 109 | + # Execute the Blackbox CLI with the configured environment |
| 110 | + command = ["blackbox"] + args |
| 111 | + |
| 112 | + # Display the complete command that will be executed |
| 113 | + args_str = " ".join(args) if args else "" |
| 114 | + command_str = f"blackbox {args_str}".strip() |
| 115 | + print("") |
| 116 | + print("Complete command to execute:") |
| 117 | + print( |
| 118 | + f"BLACKBOX_API_BASE_URL={env['BLACKBOX_API_BASE_URL']} " |
| 119 | + f"BLACKBOX_API_KEY=*** " |
| 120 | + f"BLACKBOX_API_MODEL={model} {command_str}" |
| 121 | + ) |
| 122 | + print("") |
| 123 | + |
| 124 | + return self._run_tool_with_env(command, env, "blackbox", interactive=True) |
0 commit comments