Jan Tomášek commited on
Commit
df054ff
·
unverified ·
2 Parent(s): fd3f24c521408c

Merge pull request #788 from linuztx/subordinate-agents-settings-override

Browse files
initialize.py CHANGED
@@ -4,8 +4,10 @@ from python.helpers import runtime, settings, defer
4
  from python.helpers.print_style import PrintStyle
5
 
6
 
7
- def initialize_agent():
8
  current_settings = settings.get_settings()
 
 
9
 
10
  def _normalize_model_kwargs(kwargs: dict) -> dict:
11
  # convert string values that represent valid Python numbers to numeric types
 
4
  from python.helpers.print_style import PrintStyle
5
 
6
 
7
+ def initialize_agent(override_settings: dict | None = None):
8
  current_settings = settings.get_settings()
9
+ if override_settings:
10
+ current_settings = settings.merge_settings(current_settings, override_settings)
11
 
12
  def _normalize_model_kwargs(kwargs: dict) -> dict:
13
  # convert string values that represent valid Python numbers to numeric types
python/extensions/agent_init/_15_load_profile_settings.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from initialize import initialize_agent
2
+ from python.helpers import dirty_json, files
3
+ from python.helpers.extension import Extension
4
+
5
+
6
+ class LoadProfileSettings(Extension):
7
+
8
+ async def execute(self, **kwargs) -> None:
9
+
10
+ if not self.agent or not self.agent.config.profile:
11
+ return
12
+
13
+ settings_path = files.get_abs_path("agents", self.agent.config.profile, "settings.json")
14
+ if files.exists(settings_path):
15
+ try:
16
+ override_settings_str = files.read_file(settings_path)
17
+ override_settings = dirty_json.parse(override_settings_str)
18
+
19
+ if isinstance(override_settings, dict):
20
+ # Preserve the original memory_subdir unless it's explicitly overridden
21
+ current_memory_subdir = self.agent.config.memory_subdir
22
+
23
+ new_config = initialize_agent(override_settings=override_settings)
24
+
25
+ if (
26
+ "agent_memory_subdir" not in override_settings
27
+ and current_memory_subdir != "default"
28
+ ):
29
+ new_config.memory_subdir = current_memory_subdir
30
+
31
+ self.agent.config = new_config
32
+
33
+ self.agent.context.log.log(
34
+ type="info",
35
+ content=(
36
+ "Loaded custom settings for agent "
37
+ f"{self.agent.number} with profile '{self.agent.config.profile}'."
38
+ ),
39
+ )
40
+ else:
41
+ raise Exception(
42
+ f"Subordinate settings in {settings_path} "
43
+ "must be a JSON object."
44
+ )
45
+
46
+ except Exception as e:
47
+ self.agent.context.log.log(
48
+ type="error",
49
+ content=(
50
+ "Error loading subordinate settings for "
51
+ f"profile '{self.agent.config.profile}': {e}"
52
+ ),
53
+ )
python/helpers/settings.py CHANGED
@@ -1358,6 +1358,12 @@ def set_settings_delta(delta: dict, apply: bool = True):
1358
  set_settings(new, apply) # type: ignore
1359
 
1360
 
 
 
 
 
 
 
1361
  def normalize_settings(settings: Settings) -> Settings:
1362
  copy = settings.copy()
1363
  default = get_default_settings()
 
1358
  set_settings(new, apply) # type: ignore
1359
 
1360
 
1361
+ def merge_settings(original: Settings, delta: dict) -> Settings:
1362
+ merged = original.copy()
1363
+ merged.update(delta)
1364
+ return merged
1365
+
1366
+
1367
  def normalize_settings(settings: Settings) -> Settings:
1368
  copy = settings.copy()
1369
  default = get_default_settings()