-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.py
More file actions
244 lines (209 loc) Β· 8.46 KB
/
Copy pathconfig.py
File metadata and controls
244 lines (209 loc) Β· 8.46 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""
LightClaw β Configuration
Flat .env-based configuration system.
"""
import os
import re
from dataclasses import dataclass, field
from dotenv import load_dotenv
load_dotenv()
LATEST_MODEL_DEFAULTS = {
"openai": "gpt-5.2",
"xai": "grok-4-latest",
"claude": "claude-opus-4-5",
"gemini": "gemini-3-flash-preview",
"deepseek": "deepseek-chat",
"zai": "glm-5",
}
_MODEL_DEFAULT_SENTINELS = {"", "latest", "auto", "default"}
DEFAULT_ANTHROPIC_BASE_URL = "https://api.anthropic.com"
def _strip_inline_comment(value: str) -> str:
"""Strip shell-style inline comments for unquoted env values."""
if not value:
return ""
cleaned = value.strip()
if not cleaned:
return ""
if cleaned.startswith("#"):
return ""
return re.sub(r"\s+#.*$", "", cleaned).strip()
def _parse_allowed_users(raw: str) -> list[str]:
"""Parse TELEGRAM_ALLOWED_USERS as comma-separated numeric user IDs."""
cleaned = _strip_inline_comment(raw)
if not cleaned:
return []
users: list[str] = []
for chunk in cleaned.split(","):
token = chunk.strip()
if not token:
continue
if token.startswith("#"):
break
token = token.split("#", 1)[0].strip()
if not token:
continue
# Telegram user IDs are numeric; ignore placeholder/comment text safely.
if token.lstrip("-").isdigit():
users.append(token)
return users
def _parse_deny_patterns(raw: str) -> list[str]:
"""Parse LOCAL_AGENT_DENY_PATTERNS into a list of regex pattern strings."""
if not raw:
return []
patterns: list[str] = []
for chunk in re.split(r"[,\n;]+", raw):
token = _strip_inline_comment(chunk)
if token:
patterns.append(token)
return patterns
def _parse_bool(raw: str, default: bool = False) -> bool:
cleaned = _strip_inline_comment(raw or "")
if not cleaned:
return default
return cleaned.strip().lower() in {"1", "true", "yes", "y", "on"}
def _parse_multi_default_agents(raw: str) -> list[str]:
alias_map = {
"codex": "codex",
"codex-cli": "codex",
"claude": "claude",
"claude-code": "claude",
}
cleaned = _strip_inline_comment(raw or "")
if not cleaned:
return ["claude", "codex"]
agents: list[str] = []
for chunk in re.split(r"[,\s;]+", cleaned):
token = chunk.strip().lower()
if not token:
continue
canonical = alias_map.get(token)
if not canonical:
continue
if canonical not in agents:
agents.append(canonical)
return agents or ["claude", "codex"]
@dataclass
class Config:
# LLM Provider
llm_provider: str = ""
llm_model: str = ""
# Provider credentials
openai_api_key: str = ""
xai_api_key: str = ""
anthropic_api_key: str = ""
anthropic_auth_token: str = ""
anthropic_base_url: str = DEFAULT_ANTHROPIC_BASE_URL
gemini_api_key: str = ""
deepseek_api_key: str = ""
zai_api_key: str = ""
# Telegram
telegram_bot_token: str = ""
telegram_allowed_users: list[str] = field(default_factory=list)
# Memory
memory_db_path: str = ".lightclaw/lightclaw.db"
memory_top_k: int = 5
# Workspace & Context
workspace_path: str = ".lightclaw/workspace"
context_window: int = 128000
max_output_tokens: int = 12000
local_agent_timeout_sec: int = 1800
local_agent_progress_interval_sec: int = 30
local_agent_safety_mode: str = "off"
local_agent_deny_patterns: list[str] = field(default_factory=list)
local_agent_multi_default_agents: list[str] = field(
default_factory=lambda: ["claude", "codex"]
)
local_agent_multi_auto_continue: bool = False
local_agent_multi_repair_attempts: int = 1
# Skills
skills_hub_base_url: str = "https://clawhub.ai"
skills_state_path: str = ".lightclaw/skills_state.json"
# Optional: Groq API key for voice transcription
groq_api_key: str = ""
def _resolve_model(provider: str, model: str) -> str:
"""Resolve empty/default model values to provider-specific latest defaults."""
provider_name = _strip_inline_comment(provider or "").lower()
requested = _strip_inline_comment(model or "")
if requested.lower() in _MODEL_DEFAULT_SENTINELS:
return LATEST_MODEL_DEFAULTS.get(provider_name, LATEST_MODEL_DEFAULTS["openai"])
return requested
def load_config() -> Config:
"""Load config from environment variables with auto-detection."""
allowed_raw = os.getenv("TELEGRAM_ALLOWED_USERS", "")
allowed = _parse_allowed_users(allowed_raw)
cfg = Config(
llm_provider=_strip_inline_comment(os.getenv("LLM_PROVIDER", "")),
llm_model=_strip_inline_comment(os.getenv("LLM_MODEL", "")),
openai_api_key=_strip_inline_comment(os.getenv("OPENAI_API_KEY", "")),
xai_api_key=_strip_inline_comment(os.getenv("XAI_API_KEY", "")),
anthropic_api_key=_strip_inline_comment(os.getenv("ANTHROPIC_API_KEY", "")),
anthropic_auth_token=_strip_inline_comment(os.getenv("ANTHROPIC_AUTH_TOKEN", "")),
anthropic_base_url=_strip_inline_comment(
os.getenv("ANTHROPIC_BASE_URL", DEFAULT_ANTHROPIC_BASE_URL)
)
or DEFAULT_ANTHROPIC_BASE_URL,
gemini_api_key=_strip_inline_comment(os.getenv("GEMINI_API_KEY", "")),
deepseek_api_key=_strip_inline_comment(os.getenv("DEEPSEEK_API_KEY", "")),
zai_api_key=_strip_inline_comment(os.getenv("ZAI_API_KEY", "")),
telegram_bot_token=_strip_inline_comment(os.getenv("TELEGRAM_BOT_TOKEN", "")),
telegram_allowed_users=allowed,
memory_db_path=os.getenv("MEMORY_DB_PATH", ".lightclaw/lightclaw.db"),
memory_top_k=int(os.getenv("MEMORY_TOP_K", "5")),
workspace_path=os.getenv("WORKSPACE_PATH", ".lightclaw/workspace"),
context_window=int(os.getenv("CONTEXT_WINDOW", "128000")),
max_output_tokens=int(os.getenv("MAX_OUTPUT_TOKENS", "12000")),
local_agent_timeout_sec=int(os.getenv("LOCAL_AGENT_TIMEOUT_SEC", "1800")),
local_agent_progress_interval_sec=int(
os.getenv("LOCAL_AGENT_PROGRESS_INTERVAL_SEC", "30")
),
local_agent_safety_mode=os.getenv("LOCAL_AGENT_SAFETY_MODE", "off"),
local_agent_deny_patterns=_parse_deny_patterns(
os.getenv("LOCAL_AGENT_DENY_PATTERNS", "")
),
local_agent_multi_default_agents=_parse_multi_default_agents(
os.getenv("LOCAL_AGENT_MULTI_DEFAULT_AGENTS", "claude,codex")
),
local_agent_multi_auto_continue=_parse_bool(
os.getenv("LOCAL_AGENT_MULTI_AUTO_CONTINUE", "no"),
default=False,
),
local_agent_multi_repair_attempts=int(
os.getenv("LOCAL_AGENT_MULTI_REPAIR_ATTEMPTS", "1")
),
skills_hub_base_url=os.getenv("SKILLS_HUB_BASE_URL", "https://clawhub.ai") or "https://clawhub.ai",
skills_state_path=os.getenv("SKILLS_STATE_PATH", ".lightclaw/skills_state.json") or ".lightclaw/skills_state.json",
groq_api_key=_strip_inline_comment(os.getenv("GROQ_API_KEY", "")),
)
# Auto-detect provider from configured credentials if not explicitly set
if not cfg.llm_provider:
if cfg.openai_api_key:
cfg.llm_provider = "openai"
elif cfg.xai_api_key:
cfg.llm_provider = "xai"
elif cfg.anthropic_api_key or cfg.anthropic_auth_token:
cfg.llm_provider = "claude"
elif cfg.gemini_api_key:
cfg.llm_provider = "gemini"
elif cfg.deepseek_api_key:
cfg.llm_provider = "deepseek"
elif cfg.zai_api_key:
cfg.llm_provider = "zai"
cfg.llm_provider = cfg.llm_provider.strip().lower()
cfg.llm_model = _resolve_model(cfg.llm_provider, cfg.llm_model)
cfg.max_output_tokens = max(512, int(cfg.max_output_tokens))
cfg.local_agent_timeout_sec = max(60, int(cfg.local_agent_timeout_sec))
cfg.local_agent_progress_interval_sec = max(
10, int(cfg.local_agent_progress_interval_sec)
)
cfg.local_agent_safety_mode = _strip_inline_comment(
cfg.local_agent_safety_mode or "off"
).lower()
if cfg.local_agent_safety_mode not in {"off", "strict"}:
cfg.local_agent_safety_mode = "off"
if not cfg.local_agent_multi_default_agents:
cfg.local_agent_multi_default_agents = ["claude", "codex"]
cfg.local_agent_multi_repair_attempts = max(
0,
min(2, int(cfg.local_agent_multi_repair_attempts)),
)
return cfg