Skip to content

Agent Zero AI framework

Allow subordinate agents to override settings

linuztxa2a5a4c00cb4
diff --git a/initialize.py b/initialize.py
index 3c42c952e..ec26227fa 100644
--- a/initialize.py
+++ b/initialize.py
@@ -4,8 +4,10 @@ from python.helpers import runtime, settings, defer
from python.helpers.print_style import PrintStyle
-def initialize_agent():
+def initialize_agent(override_settings: dict | None = None):
current_settings = settings.get_settings()
+ if override_settings:
+ current_settings = settings.merge_settings(current_settings, override_settings)
def _normalize_model_kwargs(kwargs: dict) -> dict:
# convert string values that represent valid Python numbers to numeric types
diff --git a/python/extensions/agent_init/_15_load_profile_settings.py b/python/extensions/agent_init/_15_load_profile_settings.py
new file mode 100644
index 000000000..342639535
--- /dev/null
+++ b/python/extensions/agent_init/_15_load_profile_settings.py
@@ -0,0 +1,53 @@
+from initialize import initialize_agent
+from python.helpers import dirty_json, files
+from python.helpers.extension import Extension
+
+
+class LoadProfileSettings(Extension):
+
+ async def execute(self, **kwargs) -> None:
+
+ if not self.agent or not self.agent.config.profile:
+ return
+
+ settings_path = files.get_abs_path("agents", self.agent.config.profile, "settings.json")
+ if files.exists(settings_path):
+ try:
+ override_settings_str = files.read_file(settings_path)
+ override_settings = dirty_json.parse(override_settings_str)
+
+ if isinstance(override_settings, dict):
+ # Preserve the original memory_subdir unless it's explicitly overridden
+ current_memory_subdir = self.agent.config.memory_subdir
+
+ new_config = initialize_agent(override_settings=override_settings)
+
+ if (
+ "agent_memory_subdir" not in override_settings
+ and current_memory_subdir != "default"
+ ):
+ new_config.memory_subdir = current_memory_subdir
+
+ self.agent.config = new_config
+
+ self.agent.context.log.log(
+ type="info",
+ content=(
+ "Loaded custom settings for agent "
+ f"{self.agent.number} with profile '{self.agent.config.profile}'."
+ ),
+ )
+ else:
+ raise Exception(
+ f"Subordinate settings in {settings_path} "
+ "must be a JSON object."
+ )
+
+ except Exception as e:
+ self.agent.context.log.log(
+ type="error",
+ content=(
+ "Error loading subordinate settings for "
+ f"profile '{self.agent.config.profile}': {e}"
+ ),
+ )
diff --git a/python/helpers/settings.py b/python/helpers/settings.py
index 3c6d826b8..b4c03e45e 100644
--- a/python/helpers/settings.py
+++ b/python/helpers/settings.py
@@ -1334,6 +1334,12 @@ def set_settings_delta(delta: dict, apply: bool = True):
set_settings(new, apply) # type: ignore
+def merge_settings(original: Settings, delta: dict) -> Settings:
+ merged = original.copy()
+ merged.update(delta)
+ return merged
+
+
def normalize_settings(settings: Settings) -> Settings:
copy = settings.copy()
default = get_default_settings()