open-swarm 0.1.1744937124__py3-none-any.whl → 0.1.1744942852__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {open_swarm-0.1.1744937124.dist-info → open_swarm-0.1.1744942852.dist-info}/METADATA +1 -1
- {open_swarm-0.1.1744937124.dist-info → open_swarm-0.1.1744942852.dist-info}/RECORD +11 -29
- swarm/blueprints/chatbot/blueprint_chatbot.py +13 -3
- swarm/blueprints/codey/blueprint_codey.py +188 -0
- swarm/blueprints/gaggle/blueprint_gaggle.py +21 -4
- swarm/blueprints/whiskeytango_foxtrot/blueprint_whiskeytango_foxtrot.py +33 -3
- swarm/extensions/blueprint/__init__.py +1 -2
- swarm/extensions/blueprint/cli_handler.py +20 -4
- swarm/extensions/blueprint/agent_utils.py +0 -21
- swarm/extensions/blueprint/blueprint_base.py +0 -333
- swarm/extensions/blueprint/blueprint_discovery.py +0 -128
- swarm/extensions/blueprint/blueprint_utils.py +0 -17
- swarm/extensions/blueprint/common_utils.py +0 -12
- swarm/extensions/blueprint/config_loader.py +0 -122
- swarm/extensions/blueprint/output_utils.py +0 -173
- swarm/extensions/blueprint/slash_commands.py +0 -17
- swarm/extensions/blueprint/spinner.py +0 -100
- swarm/extensions/config/config_manager.py +0 -258
- swarm/extensions/config/server_config.py +0 -49
- swarm/extensions/config/setup_wizard.py +0 -103
- swarm/extensions/config/utils/__init__.py +0 -0
- swarm/extensions/config/utils/logger.py +0 -36
- swarm/extensions/launchers/build_launchers.py +0 -14
- swarm/extensions/launchers/build_swarm_wrapper.py +0 -12
- swarm/extensions/launchers/swarm_api.py +0 -68
- swarm/extensions/launchers/swarm_cli.py +0 -216
- swarm/extensions/launchers/swarm_wrapper.py +0 -29
- {open_swarm-0.1.1744937124.dist-info → open_swarm-0.1.1744942852.dist-info}/WHEEL +0 -0
- {open_swarm-0.1.1744937124.dist-info → open_swarm-0.1.1744942852.dist-info}/entry_points.txt +0 -0
- {open_swarm-0.1.1744937124.dist-info → open_swarm-0.1.1744942852.dist-info}/licenses/LICENSE +0 -0
@@ -1,333 +0,0 @@
|
|
1
|
-
# --- REMOVE noisy debug/framework prints unless SWARM_DEBUG=1 ---
|
2
|
-
import os
|
3
|
-
|
4
|
-
def _should_debug():
|
5
|
-
return os.environ.get("SWARM_DEBUG") == "1"
|
6
|
-
|
7
|
-
def _debug_print(*args, **kwargs):
|
8
|
-
if _should_debug():
|
9
|
-
print(*args, **kwargs)
|
10
|
-
|
11
|
-
def _framework_print(*args, **kwargs):
|
12
|
-
if _should_debug():
|
13
|
-
print(*args, **kwargs)
|
14
|
-
|
15
|
-
# --- Content for src/swarm/extensions/blueprint/blueprint_base.py ---
|
16
|
-
import logging
|
17
|
-
import json
|
18
|
-
from abc import ABC, abstractmethod
|
19
|
-
from typing import Dict, Any, Optional, List, AsyncGenerator
|
20
|
-
from pathlib import Path
|
21
|
-
from django.apps import apps # Import Django apps registry
|
22
|
-
|
23
|
-
# Keep the function import
|
24
|
-
from swarm.extensions.config.config_loader import get_profile_from_config, _substitute_env_vars
|
25
|
-
|
26
|
-
from openai import AsyncOpenAI
|
27
|
-
from agents import set_default_openai_client
|
28
|
-
from .slash_commands import slash_registry, SlashCommandRegistry
|
29
|
-
from blueprint_agents import * # Import all from blueprint_agents
|
30
|
-
|
31
|
-
logger = logging.getLogger(__name__)
|
32
|
-
from rich.console import Console
|
33
|
-
import traceback
|
34
|
-
|
35
|
-
# --- PATCH: Suppress OpenAI tracing/telemetry errors if using LiteLLM/custom endpoint ---
|
36
|
-
import logging
|
37
|
-
import os
|
38
|
-
if os.environ.get("LITELLM_BASE_URL") or os.environ.get("OPENAI_BASE_URL"):
|
39
|
-
# Silence openai.agents tracing/telemetry errors
|
40
|
-
logging.getLogger("openai.agents").setLevel(logging.CRITICAL)
|
41
|
-
try:
|
42
|
-
import openai.agents.tracing
|
43
|
-
openai.agents.tracing.TracingClient = lambda *a, **kw: None
|
44
|
-
except Exception:
|
45
|
-
pass
|
46
|
-
|
47
|
-
# --- Spinner/Status Message Enhancements ---
|
48
|
-
# To be used by all blueprints for consistent UX
|
49
|
-
import itertools
|
50
|
-
import sys
|
51
|
-
import threading
|
52
|
-
import time
|
53
|
-
|
54
|
-
class Spinner:
|
55
|
-
def __init__(self, message_sequence=None, interval=0.3, slow_threshold=10):
|
56
|
-
self.message_sequence = message_sequence or ['Generating.', 'Generating..', 'Generating...', 'Running...']
|
57
|
-
self.interval = interval
|
58
|
-
self.slow_threshold = slow_threshold # seconds before 'Taking longer than expected'
|
59
|
-
self._stop_event = threading.Event()
|
60
|
-
self._thread = None
|
61
|
-
self._start_time = None
|
62
|
-
|
63
|
-
def start(self):
|
64
|
-
self._stop_event.clear()
|
65
|
-
self._start_time = time.time()
|
66
|
-
self._thread = threading.Thread(target=self._spin)
|
67
|
-
self._thread.start()
|
68
|
-
|
69
|
-
def _spin(self):
|
70
|
-
for msg in itertools.cycle(self.message_sequence):
|
71
|
-
if self._stop_event.is_set():
|
72
|
-
break
|
73
|
-
elapsed = time.time() - self._start_time
|
74
|
-
if elapsed > self.slow_threshold:
|
75
|
-
sys.stdout.write('\rGenerating... Taking longer than expected ')
|
76
|
-
else:
|
77
|
-
sys.stdout.write(f'\r{msg} ')
|
78
|
-
sys.stdout.flush()
|
79
|
-
time.sleep(self.interval)
|
80
|
-
sys.stdout.write('\r')
|
81
|
-
sys.stdout.flush()
|
82
|
-
|
83
|
-
def stop(self, final_message=''):
|
84
|
-
self._stop_event.set()
|
85
|
-
if self._thread:
|
86
|
-
self._thread.join()
|
87
|
-
if final_message:
|
88
|
-
sys.stdout.write(f'\r{final_message}\n')
|
89
|
-
sys.stdout.flush()
|
90
|
-
|
91
|
-
# Usage Example (to be called in blueprints):
|
92
|
-
# spinner = Spinner()
|
93
|
-
# spinner.start()
|
94
|
-
# ... do work ...
|
95
|
-
# spinner.stop('Done!')
|
96
|
-
|
97
|
-
def configure_openai_client_from_env():
|
98
|
-
"""
|
99
|
-
Framework-level function: Always instantiate and set the default OpenAI client.
|
100
|
-
Prints out the config being used for debug.
|
101
|
-
"""
|
102
|
-
import os
|
103
|
-
from agents import set_default_openai_client
|
104
|
-
from openai import AsyncOpenAI
|
105
|
-
base_url = os.environ.get("LITELLM_BASE_URL") or os.environ.get("OPENAI_BASE_URL")
|
106
|
-
api_key = os.environ.get("LITELLM_API_KEY") or os.environ.get("OPENAI_API_KEY")
|
107
|
-
_debug_print(f"[DEBUG] Using OpenAI client config: base_url={base_url}, api_key={'set' if api_key else 'NOT SET'}")
|
108
|
-
if base_url and api_key:
|
109
|
-
client = AsyncOpenAI(base_url=base_url, api_key=api_key)
|
110
|
-
set_default_openai_client(client)
|
111
|
-
_framework_print(f"[FRAMEWORK] Set default OpenAI client: base_url={base_url}, api_key={'set' if api_key else 'NOT SET'}")
|
112
|
-
else:
|
113
|
-
_framework_print("[FRAMEWORK] WARNING: base_url or api_key missing, OpenAI client not set!")
|
114
|
-
|
115
|
-
configure_openai_client_from_env()
|
116
|
-
|
117
|
-
class BlueprintBase(ABC):
|
118
|
-
"""
|
119
|
-
Abstract base class for all Swarm blueprints.
|
120
|
-
|
121
|
-
Defines the core interface for blueprint initialization and execution.
|
122
|
-
"""
|
123
|
-
enable_terminal_commands: bool = False # By default, terminal command execution is disabled
|
124
|
-
|
125
|
-
@classmethod
|
126
|
-
def main(cls):
|
127
|
-
"""
|
128
|
-
Standard CLI entry point for all blueprints.
|
129
|
-
Subclasses can override metadata/config_path if needed.
|
130
|
-
"""
|
131
|
-
from swarm.extensions.blueprint.cli_handler import run_blueprint_cli
|
132
|
-
from pathlib import Path
|
133
|
-
swarm_version = getattr(cls, "SWARM_VERSION", "1.0.0")
|
134
|
-
config_path = getattr(cls, "DEFAULT_CONFIG_PATH", Path(__file__).parent / "swarm_config.json")
|
135
|
-
run_blueprint_cli(cls, swarm_version=swarm_version, default_config_path=config_path)
|
136
|
-
|
137
|
-
def display_splash_screen(self, animated: bool = False):
|
138
|
-
"""Default splash screen. Subclasses can override for custom CLI/API branding."""
|
139
|
-
console = Console()
|
140
|
-
console.print(f"[bold cyan]Welcome to {self.__class__.__name__}![/]", style="bold")
|
141
|
-
|
142
|
-
def __init__(self, blueprint_id: str, config_path: Optional[Path] = None, enable_terminal_commands: Optional[bool] = None):
|
143
|
-
try:
|
144
|
-
if not blueprint_id:
|
145
|
-
raise ValueError("blueprint_id cannot be empty or None")
|
146
|
-
self.blueprint_id = blueprint_id
|
147
|
-
self.config_path = config_path # Note: config_path is currently unused if we rely on AppConfig
|
148
|
-
self._config: Optional[Dict[str, Any]] = None
|
149
|
-
self._llm_profile_name: Optional[str] = None
|
150
|
-
self._llm_profile_data: Optional[Dict[str, Any]] = None
|
151
|
-
self._markdown_output: bool = True # Default
|
152
|
-
# Allow per-instance override
|
153
|
-
if enable_terminal_commands is not None:
|
154
|
-
self.enable_terminal_commands = enable_terminal_commands
|
155
|
-
# Else: use class attribute (default False or set by subclass)
|
156
|
-
|
157
|
-
logger.info(f"Initializing blueprint '{self.blueprint_id}' (Type: {self.__class__.__name__})")
|
158
|
-
|
159
|
-
# --- Ensure custom OpenAI client for custom LLM providers ---
|
160
|
-
import os
|
161
|
-
|
162
|
-
# Remove monkey patching and envvar hacks. Always pass config values directly.
|
163
|
-
# (Retain only explicit AsyncOpenAI client instantiation in blueprints)
|
164
|
-
# (No changes needed here for direct client pattern)
|
165
|
-
|
166
|
-
self._load_and_process_config()
|
167
|
-
except AttributeError as e:
|
168
|
-
logger.debug(f"[BlueprintBase.__init__] AttributeError: {e}")
|
169
|
-
traceback.print_exc()
|
170
|
-
raise
|
171
|
-
|
172
|
-
def _load_and_process_config(self):
|
173
|
-
"""Loads the main Swarm config and extracts relevant settings. Falls back to empty config if Django unavailable or not found."""
|
174
|
-
import os
|
175
|
-
import json
|
176
|
-
from pathlib import Path
|
177
|
-
def redact(val):
|
178
|
-
if not isinstance(val, str) or len(val) <= 4:
|
179
|
-
return "****"
|
180
|
-
return val[:2] + "*" * (len(val)-4) + val[-2:]
|
181
|
-
def redact_dict(d):
|
182
|
-
if isinstance(d, dict):
|
183
|
-
return {k: (redact_dict(v) if not (isinstance(v, str) and ("key" in k.lower() or "token" in k.lower() or "secret" in k.lower())) else redact(v)) for k, v in d.items()}
|
184
|
-
elif isinstance(d, list):
|
185
|
-
return [redact_dict(item) for item in d]
|
186
|
-
return d
|
187
|
-
try:
|
188
|
-
try:
|
189
|
-
# --- Get config from the AppConfig instance (Django) ---
|
190
|
-
app_config_instance = apps.get_app_config('swarm')
|
191
|
-
if not hasattr(app_config_instance, 'config') or not app_config_instance.config:
|
192
|
-
raise ValueError("AppConfig for 'swarm' does not have a valid 'config' attribute.")
|
193
|
-
config = app_config_instance.config
|
194
|
-
logger.debug("Loaded config from Django AppConfig.")
|
195
|
-
except Exception as e:
|
196
|
-
if _should_debug():
|
197
|
-
logger.warning(f"Falling back to CLI/home config due to error: {e}")
|
198
|
-
config = None
|
199
|
-
# 1. CLI argument (not handled here, handled in cli_handler)
|
200
|
-
# 2. Current working directory
|
201
|
-
cwd_config = Path.cwd() / "swarm_config.json"
|
202
|
-
if cwd_config.exists():
|
203
|
-
with open(cwd_config, 'r') as f:
|
204
|
-
config = json.load(f)
|
205
|
-
# 3. XDG_CONFIG_HOME or ~/.config/swarm/swarm_config.json
|
206
|
-
elif os.environ.get("XDG_CONFIG_HOME"):
|
207
|
-
xdg_config = Path(os.environ["XDG_CONFIG_HOME"]) / "swarm" / "swarm_config.json"
|
208
|
-
if xdg_config.exists():
|
209
|
-
with open(xdg_config, 'r') as f:
|
210
|
-
config = json.load(f)
|
211
|
-
elif (Path.home() / ".config/swarm/swarm_config.json").exists():
|
212
|
-
with open(Path.home() / ".config/swarm/swarm_config.json", 'r') as f:
|
213
|
-
config = json.load(f)
|
214
|
-
# 4. Legacy fallback: ~/.swarm/swarm_config.json
|
215
|
-
elif (Path.home() / ".swarm/swarm_config.json").exists():
|
216
|
-
with open(Path.home() / ".swarm/swarm_config.json", 'r') as f:
|
217
|
-
config = json.load(f)
|
218
|
-
# 5. Fallback: OPENAI_API_KEY envvar
|
219
|
-
elif os.environ.get("OPENAI_API_KEY"):
|
220
|
-
config = {
|
221
|
-
"llm": {"default": {"provider": "openai", "model": "gpt-3.5-turbo", "api_key": os.environ["OPENAI_API_KEY"]}},
|
222
|
-
"settings": {"default_llm_profile": "default", "default_markdown_output": True},
|
223
|
-
"blueprints": {},
|
224
|
-
"llm_profile": "default",
|
225
|
-
"mcpServers": {}
|
226
|
-
}
|
227
|
-
logger.info("No config file found, using default config with OPENAI_API_KEY for CLI mode.")
|
228
|
-
else:
|
229
|
-
config = {}
|
230
|
-
logger.warning("No config file found and OPENAI_API_KEY is not set. Using empty config. CLI blueprints may fail if LLM config is required.")
|
231
|
-
if config is not None:
|
232
|
-
config = _substitute_env_vars(config)
|
233
|
-
self._config = config or {}
|
234
|
-
|
235
|
-
# --- After config is loaded, set OpenAI client from config if possible ---
|
236
|
-
try:
|
237
|
-
llm_profiles = self._config.get("llm", {})
|
238
|
-
default_profile = llm_profiles.get("default", {})
|
239
|
-
base_url = default_profile.get("base_url")
|
240
|
-
api_key = default_profile.get("api_key")
|
241
|
-
# Expand env vars if present
|
242
|
-
import os
|
243
|
-
if base_url and base_url.startswith("${"):
|
244
|
-
var = base_url[2:-1]
|
245
|
-
base_url = os.environ.get(var, base_url)
|
246
|
-
if api_key and api_key.startswith("${"):
|
247
|
-
var = api_key[2:-1]
|
248
|
-
api_key = os.environ.get(var, api_key)
|
249
|
-
if base_url and api_key:
|
250
|
-
from openai import AsyncOpenAI
|
251
|
-
from agents import set_default_openai_client
|
252
|
-
_debug_print(f"[DEBUG] (config) Setting OpenAI client: base_url={base_url}, api_key={'set' if api_key else 'NOT SET'}")
|
253
|
-
client = AsyncOpenAI(base_url=base_url, api_key=api_key)
|
254
|
-
set_default_openai_client(client)
|
255
|
-
except Exception as e:
|
256
|
-
_debug_print(f"[DEBUG] Failed to set OpenAI client from config: {e}")
|
257
|
-
|
258
|
-
# --- Debug: Print and log redacted config ---
|
259
|
-
redacted_config = redact_dict(self._config)
|
260
|
-
logger.debug(f"Loaded config (redacted): {json.dumps(redacted_config, indent=2)}")
|
261
|
-
|
262
|
-
# --- Process LLM profile name and data ---
|
263
|
-
settings_section = self._config.get("settings", {})
|
264
|
-
llm_section = self._config.get("llm", {})
|
265
|
-
default_profile = settings_section.get("default_llm_profile") or "default"
|
266
|
-
self._llm_profile_name = self._config.get("llm_profile") or default_profile
|
267
|
-
if "profiles" in llm_section:
|
268
|
-
self._llm_profile_data = llm_section["profiles"].get(self._llm_profile_name, {})
|
269
|
-
else:
|
270
|
-
self._llm_profile_data = llm_section.get(self._llm_profile_name, {})
|
271
|
-
|
272
|
-
blueprint_specific_settings = self._config.get("blueprints", {}).get(self.blueprint_id, {})
|
273
|
-
global_markdown_setting = settings_section.get("default_markdown_output", True)
|
274
|
-
self._markdown_output = blueprint_specific_settings.get("markdown_output", global_markdown_setting)
|
275
|
-
logger.debug(f"Markdown output for '{self.blueprint_id}': {self._markdown_output}")
|
276
|
-
|
277
|
-
except ValueError as e:
|
278
|
-
logger.error(f"Configuration error for blueprint '{self.blueprint_id}': {e}", exc_info=True)
|
279
|
-
raise
|
280
|
-
except Exception as e:
|
281
|
-
logger.error(f"Unexpected error loading config for blueprint '{self.blueprint_id}': {e}", exc_info=True)
|
282
|
-
raise
|
283
|
-
|
284
|
-
@property
|
285
|
-
def config(self) -> Dict[str, Any]:
|
286
|
-
"""Returns the loaded and processed Swarm configuration."""
|
287
|
-
if self._config is None:
|
288
|
-
raise RuntimeError("Configuration accessed before initialization or after failure.")
|
289
|
-
return self._config
|
290
|
-
|
291
|
-
@property
|
292
|
-
def llm_profile(self) -> Dict[str, Any]:
|
293
|
-
"""Returns the loaded and processed LLM profile data for this blueprint."""
|
294
|
-
if self._llm_profile_data is None:
|
295
|
-
raise RuntimeError("LLM profile accessed before initialization or after failure.")
|
296
|
-
return self._llm_profile_data
|
297
|
-
|
298
|
-
@property
|
299
|
-
def llm_profile_name(self) -> str:
|
300
|
-
"""Returns the name of the LLM profile being used."""
|
301
|
-
if self._llm_profile_name is None:
|
302
|
-
raise RuntimeError("LLM profile name accessed before initialization or after failure.")
|
303
|
-
return self._llm_profile_name
|
304
|
-
|
305
|
-
@property
|
306
|
-
def slash_commands(self) -> SlashCommandRegistry:
|
307
|
-
"""Access the global slash command registry. Blueprints can register new commands here."""
|
308
|
-
return slash_registry
|
309
|
-
|
310
|
-
def get_llm_profile(self, profile_name: str) -> dict:
|
311
|
-
"""Returns the LLM profile dict for the given profile name from config, or empty dict if not found.
|
312
|
-
Supports both llm.profiles and direct llm keys for backward compatibility."""
|
313
|
-
llm_section = self.config.get("llm", {})
|
314
|
-
if "profiles" in llm_section:
|
315
|
-
return llm_section["profiles"].get(profile_name, {})
|
316
|
-
return llm_section.get(profile_name, {})
|
317
|
-
|
318
|
-
@property
|
319
|
-
def should_output_markdown(self) -> bool:
|
320
|
-
"""Returns whether the blueprint should format output as Markdown."""
|
321
|
-
return self._markdown_output
|
322
|
-
|
323
|
-
@abstractmethod
|
324
|
-
async def run(self, messages: List[Dict[str, Any]], **kwargs: Any) -> AsyncGenerator[Dict[str, Any], None]:
|
325
|
-
"""
|
326
|
-
The main execution method for the blueprint.
|
327
|
-
"""
|
328
|
-
import os
|
329
|
-
import pprint
|
330
|
-
logger.debug("ENVIRONMENT DUMP BEFORE MODEL CALL:")
|
331
|
-
pprint.pprint(dict(os.environ))
|
332
|
-
raise NotImplementedError("Subclasses must implement the 'run' method.")
|
333
|
-
yield {}
|
@@ -1,128 +0,0 @@
|
|
1
|
-
import os
|
2
|
-
import importlib
|
3
|
-
import importlib.util
|
4
|
-
import inspect
|
5
|
-
import logging # Ensure logging is imported
|
6
|
-
import sys
|
7
|
-
from typing import Dict, Type, Any
|
8
|
-
from pathlib import Path
|
9
|
-
|
10
|
-
# *** Define logger EARLIER ***
|
11
|
-
logger = logging.getLogger(__name__)
|
12
|
-
|
13
|
-
# *** Import the ACTUAL BlueprintBase from the likely correct path ***
|
14
|
-
try:
|
15
|
-
# Adjust this path if BlueprintBase lives elsewhere
|
16
|
-
from swarm.extensions.blueprint.blueprint_base import BlueprintBase
|
17
|
-
except ImportError:
|
18
|
-
# This logger call is now safe
|
19
|
-
logger.error("Failed to import BlueprintBase from swarm.extensions.blueprint.blueprint_base. Using placeholder.", exc_info=True)
|
20
|
-
class BlueprintBase: # Fallback placeholder
|
21
|
-
metadata: Dict[str, Any] = {}
|
22
|
-
def __init__(self, *args, **kwargs): pass
|
23
|
-
async def run(self, *args, **kwargs): pass
|
24
|
-
|
25
|
-
|
26
|
-
class BlueprintLoadError(Exception):
|
27
|
-
"""Custom exception for errors during blueprint loading."""
|
28
|
-
pass
|
29
|
-
|
30
|
-
def _get_blueprint_name_from_dir(dir_name: str) -> str:
|
31
|
-
"""Converts directory name (e.g., 'blueprint_my_agent') to blueprint name (e.g., 'my_agent')."""
|
32
|
-
prefix = "blueprint_"
|
33
|
-
if dir_name.startswith(prefix):
|
34
|
-
return dir_name[len(prefix):]
|
35
|
-
return dir_name
|
36
|
-
|
37
|
-
def discover_blueprints(blueprint_dir: str) -> Dict[str, Type[BlueprintBase]]:
|
38
|
-
"""
|
39
|
-
Discovers blueprints (subclasses of BlueprintBase) by looking for
|
40
|
-
'blueprint_{name}.py' files within subdirectories of the given blueprint directory.
|
41
|
-
|
42
|
-
Args:
|
43
|
-
blueprint_dir: The path to the directory containing blueprint subdirectories.
|
44
|
-
|
45
|
-
Returns:
|
46
|
-
A dictionary mapping blueprint names to their corresponding class objects.
|
47
|
-
"""
|
48
|
-
logger.info(f"Starting blueprint discovery in directory: {blueprint_dir}")
|
49
|
-
blueprints: Dict[str, Type[BlueprintBase]] = {}
|
50
|
-
base_dir = Path(blueprint_dir).resolve()
|
51
|
-
|
52
|
-
if not base_dir.is_dir():
|
53
|
-
logger.error(f"Blueprint directory not found or is not a directory: {base_dir}")
|
54
|
-
return blueprints
|
55
|
-
|
56
|
-
# Iterate over items inside the base blueprint directory
|
57
|
-
for subdir in base_dir.iterdir():
|
58
|
-
if not subdir.is_dir():
|
59
|
-
continue # Skip files directly under blueprints/
|
60
|
-
|
61
|
-
# Use directory name as blueprint name (e.g., 'echocraft')
|
62
|
-
blueprint_name = subdir.name
|
63
|
-
logger.debug(f"Processing potential blueprint '{blueprint_name}' in directory: {subdir.name}")
|
64
|
-
|
65
|
-
# Look for the specific .py file, e.g., blueprint_echocraft.py
|
66
|
-
py_file_name = f"blueprint_{blueprint_name}.py"
|
67
|
-
py_file_path = subdir / py_file_name
|
68
|
-
|
69
|
-
if not py_file_path.is_file():
|
70
|
-
# Also check for just {blueprint_name}.py if that's a convention
|
71
|
-
alt_py_file_name = f"{blueprint_name}.py"
|
72
|
-
alt_py_file_path = subdir / alt_py_file_name
|
73
|
-
if alt_py_file_path.is_file():
|
74
|
-
py_file_path = alt_py_file_path # Use the alternative path
|
75
|
-
py_file_name = alt_py_file_name
|
76
|
-
logger.debug(f"Found alternative blueprint file: {py_file_name}")
|
77
|
-
else:
|
78
|
-
logger.warning(f"Skipping directory '{subdir.name}': Neither '{py_file_name}' nor '{alt_py_file_name}' found.")
|
79
|
-
continue
|
80
|
-
|
81
|
-
|
82
|
-
# Construct module import path, e.g., blueprints.echocraft.blueprint_echocraft
|
83
|
-
if py_file_path.name.startswith('blueprint_gatcha'):
|
84
|
-
module_import_path = f"swarm.blueprints.gatcha.{py_file_path.stem}"
|
85
|
-
elif py_file_path.name.startswith('blueprint_'):
|
86
|
-
module_import_path = f"swarm.blueprints.{subdir.name}.{py_file_path.stem}"
|
87
|
-
else:
|
88
|
-
continue
|
89
|
-
|
90
|
-
try:
|
91
|
-
# Ensure parent directory is in path
|
92
|
-
parent_dir = str(base_dir.parent)
|
93
|
-
if parent_dir not in sys.path:
|
94
|
-
logger.debug(f"Adding '{parent_dir}' to sys.path for blueprint discovery.")
|
95
|
-
sys.path.insert(0, parent_dir)
|
96
|
-
|
97
|
-
# Create module spec from file path
|
98
|
-
module_spec = importlib.util.spec_from_file_location(module_import_path, py_file_path)
|
99
|
-
|
100
|
-
if module_spec and module_spec.loader:
|
101
|
-
module = importlib.util.module_from_spec(module_spec)
|
102
|
-
sys.modules[module_import_path] = module
|
103
|
-
module_spec.loader.exec_module(module)
|
104
|
-
logger.debug(f"Successfully loaded module: {module_import_path}")
|
105
|
-
|
106
|
-
found_bp_class = None
|
107
|
-
for name, obj in inspect.getmembers(module):
|
108
|
-
if inspect.isclass(obj) and obj.__module__ == module_import_path and issubclass(obj, BlueprintBase) and obj is not BlueprintBase:
|
109
|
-
if found_bp_class:
|
110
|
-
logger.warning(f"Multiple BlueprintBase subclasses found in {py_file_name}. Using the first: {found_bp_class.__name__}.")
|
111
|
-
else:
|
112
|
-
logger.debug(f"Found Blueprint class '{name}' in module '{module_import_path}'")
|
113
|
-
found_bp_class = obj
|
114
|
-
blueprints[blueprint_name] = found_bp_class
|
115
|
-
# break
|
116
|
-
|
117
|
-
if not found_bp_class:
|
118
|
-
logger.warning(f"No BlueprintBase subclass found directly defined in module: {module_import_path}")
|
119
|
-
else:
|
120
|
-
logger.warning(f"Could not create module spec for {py_file_path}")
|
121
|
-
|
122
|
-
except Exception as e:
|
123
|
-
logger.error(f"Error processing blueprint file '{py_file_path}': {e}", exc_info=True)
|
124
|
-
if module_import_path in sys.modules:
|
125
|
-
del sys.modules[module_import_path]
|
126
|
-
|
127
|
-
logger.info(f"Blueprint discovery complete. Found: {list(blueprints.keys())}")
|
128
|
-
return blueprints
|
@@ -1,17 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
Utility functions for blueprint management.
|
3
|
-
"""
|
4
|
-
|
5
|
-
def filter_blueprints(all_blueprints: dict, allowed_blueprints_str: str) -> dict:
|
6
|
-
"""
|
7
|
-
Filters the given blueprints dictionary using a comma-separated string of allowed blueprint keys.
|
8
|
-
|
9
|
-
Args:
|
10
|
-
all_blueprints (dict): A dictionary containing all discovered blueprints.
|
11
|
-
allowed_blueprints_str (str): A comma-separated string of allowed blueprint keys.
|
12
|
-
|
13
|
-
Returns:
|
14
|
-
dict: A dictionary containing only the blueprints whose keys are present in the allowed list.
|
15
|
-
"""
|
16
|
-
allowed_list = [bp.strip() for bp in allowed_blueprints_str.split(",")]
|
17
|
-
return {k: v for k, v in all_blueprints.items() if k in allowed_list}
|
@@ -1,12 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
Common utilities potentially shared across blueprint extensions.
|
3
|
-
"""
|
4
|
-
|
5
|
-
from typing import Any # Removed Dict, List as they weren't used
|
6
|
-
|
7
|
-
def get_agent_name(agent: Any) -> str:
|
8
|
-
"""Return the name of an agent from its attributes ('name' or '__name__')."""
|
9
|
-
return getattr(agent, "name", getattr(agent, "__name__", "<unknown>"))
|
10
|
-
|
11
|
-
# get_token_count has been moved to swarm.utils.context_utils
|
12
|
-
# Ensure imports in other files point to the correct location.
|
@@ -1,122 +0,0 @@
|
|
1
|
-
import json
|
2
|
-
import logging
|
3
|
-
import os
|
4
|
-
from pathlib import Path
|
5
|
-
from typing import Any, Dict, Optional, Union
|
6
|
-
|
7
|
-
from dotenv import load_dotenv
|
8
|
-
|
9
|
-
logger = logging.getLogger("swarm.config")
|
10
|
-
|
11
|
-
def _substitute_env_vars(value: Any) -> Any:
|
12
|
-
"""Recursively substitute environment variables in strings, lists, and dicts."""
|
13
|
-
if isinstance(value, str):
|
14
|
-
return os.path.expandvars(value)
|
15
|
-
elif isinstance(value, list):
|
16
|
-
return [_substitute_env_vars(item) for item in value]
|
17
|
-
elif isinstance(value, dict):
|
18
|
-
return {k: _substitute_env_vars(v) for k, v in value.items()}
|
19
|
-
else:
|
20
|
-
return value
|
21
|
-
|
22
|
-
def load_environment(project_root: Path):
|
23
|
-
"""Loads environment variables from a `.env` file located at the project root."""
|
24
|
-
dotenv_path = project_root / ".env"
|
25
|
-
logger.debug(f"Checking for .env file at: {dotenv_path}")
|
26
|
-
try:
|
27
|
-
if dotenv_path.is_file():
|
28
|
-
loaded = load_dotenv(dotenv_path=dotenv_path, override=True)
|
29
|
-
if loaded:
|
30
|
-
logger.debug(f".env file Loaded/Overridden at: {dotenv_path}")
|
31
|
-
else:
|
32
|
-
logger.debug(f"No .env file found at {dotenv_path}.")
|
33
|
-
except Exception as e:
|
34
|
-
logger.error(f"Error loading .env file '{dotenv_path}': {e}", exc_info=logger.level <= logging.DEBUG)
|
35
|
-
|
36
|
-
def load_full_configuration(
|
37
|
-
blueprint_class_name: str,
|
38
|
-
default_config_path: Path,
|
39
|
-
config_path_override: Optional[Union[str, Path]] = None,
|
40
|
-
profile_override: Optional[str] = None,
|
41
|
-
cli_config_overrides: Optional[Dict[str, Any]] = None,
|
42
|
-
) -> Dict[str, Any]:
|
43
|
-
"""
|
44
|
-
Loads and merges configuration settings from base file, blueprint specifics, profiles, and CLI overrides.
|
45
|
-
|
46
|
-
Args:
|
47
|
-
blueprint_class_name (str): The name of the blueprint class (e.g., "MyBlueprint").
|
48
|
-
default_config_path (Path): The default path to the swarm_config.json file.
|
49
|
-
config_path_override (Optional[Union[str, Path]]): Path specified via CLI argument.
|
50
|
-
profile_override (Optional[str]): Profile specified via CLI argument.
|
51
|
-
cli_config_overrides (Optional[Dict[str, Any]]): Overrides provided via CLI argument.
|
52
|
-
|
53
|
-
Returns:
|
54
|
-
Dict[str, Any]: The final, merged configuration dictionary.
|
55
|
-
|
56
|
-
Raises:
|
57
|
-
ValueError: If the configuration file has JSON errors or cannot be read.
|
58
|
-
FileNotFoundError: If a specific config_path_override is given but the file doesn't exist.
|
59
|
-
"""
|
60
|
-
config_path = Path(config_path_override) if config_path_override else default_config_path
|
61
|
-
logger.debug(f"Attempting to load base configuration from: {config_path}")
|
62
|
-
base_config = {}
|
63
|
-
if config_path.is_file():
|
64
|
-
try:
|
65
|
-
with open(config_path, "r", encoding="utf-8") as f:
|
66
|
-
base_config = json.load(f)
|
67
|
-
logger.debug(f"Successfully loaded base configuration from: {config_path}")
|
68
|
-
except json.JSONDecodeError as e:
|
69
|
-
raise ValueError(f"Config Error: Failed to parse JSON in {config_path}: {e}") from e
|
70
|
-
except Exception as e:
|
71
|
-
raise ValueError(f"Config Error: Failed to read {config_path}: {e}") from e
|
72
|
-
else:
|
73
|
-
if config_path_override:
|
74
|
-
raise FileNotFoundError(f"Configuration Error: Specified config file not found: {config_path}")
|
75
|
-
else:
|
76
|
-
logger.warning(f"Default configuration file not found at {config_path}. Proceeding without base configuration.")
|
77
|
-
|
78
|
-
# 1. Start with base defaults
|
79
|
-
final_config = base_config.get("defaults", {}).copy()
|
80
|
-
logger.debug(f"Applied base defaults. Keys: {list(final_config.keys())}")
|
81
|
-
|
82
|
-
# 2. Merge base llm and mcpServers sections
|
83
|
-
if "llm" in base_config:
|
84
|
-
final_config.setdefault("llm", {}).update(base_config["llm"])
|
85
|
-
logger.debug("Merged base 'llm'.")
|
86
|
-
if "mcpServers" in base_config:
|
87
|
-
final_config.setdefault("mcpServers", {}).update(base_config["mcpServers"])
|
88
|
-
logger.debug("Merged base 'mcpServers'.")
|
89
|
-
|
90
|
-
# 3. Merge blueprint-specific settings
|
91
|
-
blueprint_settings = base_config.get("blueprints", {}).get(blueprint_class_name, {})
|
92
|
-
if blueprint_settings:
|
93
|
-
final_config.update(blueprint_settings)
|
94
|
-
logger.debug(f"Merged BP '{blueprint_class_name}' settings. Keys: {list(blueprint_settings.keys())}")
|
95
|
-
|
96
|
-
# 4. Determine and merge profile settings
|
97
|
-
# Priority: CLI > Blueprint Specific > Base Defaults > "default"
|
98
|
-
profile_in_bp_settings = blueprint_settings.get("default_profile")
|
99
|
-
profile_in_base_defaults = base_config.get("defaults", {}).get("default_profile")
|
100
|
-
profile_to_use = profile_override or profile_in_bp_settings or profile_in_base_defaults or "default"
|
101
|
-
logger.debug(f"Using profile: '{profile_to_use}'")
|
102
|
-
profile_settings = base_config.get("profiles", {}).get(profile_to_use, {})
|
103
|
-
if profile_settings:
|
104
|
-
final_config.update(profile_settings)
|
105
|
-
logger.debug(f"Merged profile '{profile_to_use}'. Keys: {list(profile_settings.keys())}")
|
106
|
-
elif profile_to_use != "default" and (profile_override or profile_in_bp_settings or profile_in_base_defaults):
|
107
|
-
logger.warning(f"Profile '{profile_to_use}' requested but not found.")
|
108
|
-
|
109
|
-
# 5. Merge CLI overrides (highest priority)
|
110
|
-
if cli_config_overrides:
|
111
|
-
final_config.update(cli_config_overrides)
|
112
|
-
logger.debug(f"Merged CLI overrides. Keys: {list(cli_config_overrides.keys())}")
|
113
|
-
|
114
|
-
# Ensure top-level keys exist
|
115
|
-
final_config.setdefault("llm", {})
|
116
|
-
final_config.setdefault("mcpServers", {})
|
117
|
-
|
118
|
-
# 6. Substitute environment variables in the final config
|
119
|
-
final_config = _substitute_env_vars(final_config)
|
120
|
-
logger.debug("Applied final env var substitution.")
|
121
|
-
|
122
|
-
return final_config
|