forked from yoheinakajima/babyagi3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
158 lines (127 loc) · 4.61 KB
/
config.py
File metadata and controls
158 lines (127 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
Configuration loader for BabyAGI.
Loads configuration from YAML file with environment variable substitution.
"""
import logging
import os
import re
from pathlib import Path
logger = logging.getLogger(__name__)
def load_config(config_path: str = None) -> dict:
"""Load configuration from YAML file.
Args:
config_path: Path to config file. Defaults to config.yaml in current dir.
Returns:
Configuration dict with env vars substituted.
"""
# Find config file
if config_path is None:
config_path = os.environ.get("BABYAGI_CONFIG", "config.yaml")
path = Path(config_path)
if not path.exists():
# Return minimal default config
return _default_config()
# Load YAML
try:
import yaml
except ImportError:
logger.warning("PyYAML not installed. Using default config. Install with: pip install pyyaml")
return _default_config()
with open(path) as f:
content = f.read()
# Substitute environment variables: ${VAR_NAME} or ${VAR_NAME:default}
content = _substitute_env_vars(content)
config = yaml.safe_load(content) or {}
# Merge with defaults
return _merge_with_defaults(config)
def _substitute_env_vars(content: str) -> str:
"""Replace ${VAR} and ${VAR:default} with environment values."""
def replace(match):
var_expr = match.group(1)
if ":" in var_expr:
var_name, default = var_expr.split(":", 1)
else:
var_name, default = var_expr, ""
return os.environ.get(var_name, default)
# Match ${VAR} or ${VAR:default}
pattern = r"\$\{([^}]+)\}"
return re.sub(pattern, replace, content)
def _default_config() -> dict:
"""Return minimal default configuration."""
return {
"owner": {
"id": os.environ.get("OWNER_ID", "owner"),
"name": os.environ.get("OWNER_NAME", ""),
"email": os.environ.get("OWNER_EMAIL", ""),
"bio": os.environ.get("OWNER_BIO", ""),
"goal": os.environ.get("OWNER_GOAL", ""),
"phone": os.environ.get("OWNER_PHONE", ""),
"timezone": os.environ.get("OWNER_TIMEZONE", ""),
"contacts": {
"email": os.environ.get("OWNER_EMAIL", ""),
}
},
"channels": {
"cli": {"enabled": True},
"email": {
"enabled": bool(os.environ.get("AGENTMAIL_API_KEY")),
"poll_interval": 60,
},
"voice": {"enabled": False},
},
"agent": {
"model": "claude-sonnet-4-20250514",
"name": os.environ.get("AGENT_NAME", "Assistant"),
"description": os.environ.get("AGENT_DESCRIPTION", "a helpful AI assistant"),
"objective": os.environ.get(
"AGENT_OBJECTIVE",
"Help my owner with tasks, manage their digital presence, and handle communications on their behalf."
),
"behavior": {
"spending": {
"require_approval": True,
"auto_approve_limit": 0.0,
},
"external_policy": {
"respond_to_unknown": True,
"consult_owner_threshold": "medium",
},
"accounts": {
"use_agent_email": True,
"check_existing_first": True,
},
},
},
}
def _merge_with_defaults(config: dict) -> dict:
"""Merge user config with defaults."""
defaults = _default_config()
# Deep merge
def merge(base, override):
result = base.copy()
for key, value in override.items():
if key in result and isinstance(result[key], dict) and isinstance(value, dict):
result[key] = merge(result[key], value)
else:
result[key] = value
return result
return merge(defaults, config)
def get_channel_config(config: dict, channel: str) -> dict:
"""Get configuration for a specific channel.
Args:
config: Full configuration dict
channel: Channel name (cli, email, voice, etc.)
Returns:
Channel configuration dict, or empty dict if not found.
"""
return config.get("channels", {}).get(channel, {})
def is_channel_enabled(config: dict, channel: str) -> bool:
"""Check if a channel is enabled.
Args:
config: Full configuration dict
channel: Channel name
Returns:
True if channel is enabled.
"""
channel_config = get_channel_config(config, channel)
return channel_config.get("enabled", False)