-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig.py
More file actions
executable file
·316 lines (271 loc) · 11.6 KB
/
Copy pathconfig.py
File metadata and controls
executable file
·316 lines (271 loc) · 11.6 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import os
import json
import time
import shutil
from pathlib import Path
from threading import RLock
# Thread safety for config operations
_config_lock = RLock()
# Default configuration - UNIFIED
DEFAULT_CONFIG = {
# API Configuration
"api_key": "",
"gemini_api_key": "",
"api_base": "gemini", # "openrouter" or "gemini"
"model": "gemini-flash-latest",
"ai_enabled": False,
# UI Configuration
"theme": "matrix",
"prompt_style": "hacker",
# Banner / MOTD Configuration
# banner_id: string identifier of the selected banner ("1".."25")
# banner_random: when True, a random banner is chosen on startup
# banner_sync: when True, banners use theme-based (Rich) colors;
# when False, use hardcoded ANSI-colored templates.
"banner_id": "1",
"banner_random": False,
"banner_sync": True,
# System Configuration
"safe_mode": True,
"auto_backup": True,
"log_commands": True,
"command_prefix": "",
"paranoid_mode": False,
"model_profile": "quality",
"offline_mode": False,
"auto_model_switch": True,
# Feedback / telemetry
"feedback_worker_url": "https://feedback-n-review.vritrasec.workers.dev/",
# Persistence metadata
"_config_version": None, # Will be synced with VRITRA_VERSION on first load/save
"_last_updated": None
}
# Professional config directory structure with better Windows support
CONFIG_DIR = os.path.expanduser("~/.config-vritrasecz/vritraai")
def ensure_config_directory():
"""Ensure config directory exists with proper permissions."""
try:
os.makedirs(CONFIG_DIR, exist_ok=True)
# Test write permissions
test_file = os.path.join(CONFIG_DIR, ".test_write")
with open(test_file, 'w') as f:
f.write("test")
os.remove(test_file)
return True
except PermissionError:
print(f"⚠️ Warning: No write permission to config directory: {CONFIG_DIR}")
return False
except Exception as e:
print(f"⚠️ Warning: Could not create config directory: {e}")
return False
ensure_config_directory()
CONFIG_FILE = os.path.join(CONFIG_DIR, "config.json")
CONFIG_BACKUP_FILE = os.path.join(CONFIG_DIR, "config.json.backup")
def create_config_backup():
"""Create a backup of the config file."""
try:
if os.path.exists(CONFIG_FILE):
shutil.copy2(CONFIG_FILE, CONFIG_BACKUP_FILE)
return True
except Exception:
pass
return False
def restore_config_from_backup():
"""Restore config from backup if main config is corrupted."""
try:
if os.path.exists(CONFIG_BACKUP_FILE):
shutil.copy2(CONFIG_BACKUP_FILE, CONFIG_FILE)
return True
except Exception:
pass
return False
def validate_config(config):
"""Validate configuration data."""
if not isinstance(config, dict):
return False
# Check required keys exist
required_keys = ['api_key', 'theme', 'prompt_style']
for key in required_keys:
if key not in config:
return False
# Validate theme exists
valid_themes = [
'dark','light','retro','cyberpunk','matrix','hacker_green',
'terminal_green','neon','rainbow','purple','cherry','mint',
'ocean','sunset','forest','winter','spring','summer',
'grayscale','royal','coffee','autumn','pastel','toxic',
'volcano','galaxy','deep_sea','candy','lava','ice',
'electric','forest_night','synthwave','desert_sunset',
'midnight','sunrise','lavender'
]
if config.get('theme') not in valid_themes:
config['theme'] = 'matrix'
return True
def load_config():
"""Load configuration from file with robust error handling."""
with _config_lock:
attempts = 0
max_attempts = 3
while attempts < max_attempts:
try:
if os.path.exists(CONFIG_FILE):
# Try to read config
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
content = f.read().strip()
if not content:
raise ValueError("Empty config file")
config = json.loads(content)
# Validate config structure
if not validate_config(config):
raise ValueError("Invalid config structure")
# Merge with defaults for new keys
merged_config = DEFAULT_CONFIG.copy()
merged_config.update(config)
merged_config["_last_updated"] = time.time()
# Sync _config_version if None or missing
if not merged_config.get("_config_version"):
try:
import sys
if 'vritraai' in sys.modules:
vritraai_module = sys.modules['vritraai']
if hasattr(vritraai_module, 'VRITRA_VERSION'):
merged_config["_config_version"] = vritraai_module.VRITRA_VERSION
except Exception:
pass
# Save merged config back (adds any new default keys)
_save_config_unsafe(merged_config)
print(f"✅ Configuration loaded successfully from {CONFIG_FILE}")
return merged_config
else:
# No config file exists, create default
print(f"📝 Creating default configuration at {CONFIG_FILE}")
default_config = DEFAULT_CONFIG.copy()
default_config["_last_updated"] = time.time()
_save_config_unsafe(default_config)
return default_config
except (json.JSONDecodeError, ValueError) as e:
print(f"⚠️ Config file corrupted (attempt {attempts + 1}): {e}")
# Try to restore from backup
if attempts == 0 and restore_config_from_backup():
print("🔄 Restored config from backup")
attempts += 1
continue
# If still failing, recreate config
if attempts >= max_attempts - 1:
print("🆕 Creating fresh configuration")
try:
os.remove(CONFIG_FILE)
except:
pass
default_config = DEFAULT_CONFIG.copy()
default_config["_last_updated"] = time.time()
_save_config_unsafe(default_config)
return default_config
except Exception as e:
print(f"❌ Unexpected error loading config (attempt {attempts + 1}): {e}")
attempts += 1
# Fallback to defaults if all attempts failed
print("⚠️ Using default configuration (file operations failed)")
fallback_config = DEFAULT_CONFIG.copy()
# Try to sync version even in fallback
try:
import sys
if 'vritraai' in sys.modules:
vritraai_module = sys.modules['vritraai']
if hasattr(vritraai_module, 'VRITRA_VERSION'):
fallback_config["_config_version"] = vritraai_module.VRITRA_VERSION
except Exception:
pass # Silently fail if version sync not possible
return fallback_config
def _save_config_unsafe(config):
"""Save configuration without acquiring lock (internal use only)."""
if not ensure_config_directory():
print("❌ Cannot save config: directory not accessible")
return False
try:
# Validate config before saving
if not validate_config(config):
print("❌ Cannot save invalid config")
return False
# Create backup before saving
create_config_backup()
# Sync _config_version from vritraai if available (avoid circular import)
try:
import sys
if 'vritraai' in sys.modules:
vritraai_module = sys.modules['vritraai']
if hasattr(vritraai_module, 'VRITRA_VERSION'):
config["_config_version"] = vritraai_module.VRITRA_VERSION
except Exception:
pass # Silently fail if version sync not possible
# Update timestamp
config["_last_updated"] = time.time()
# Write to temporary file first, then rename (atomic operation)
temp_file = CONFIG_FILE + ".tmp"
with open(temp_file, 'w', encoding='utf-8') as f:
json.dump(config, f, indent=2, ensure_ascii=False)
f.flush() # Ensure data is written to disk
os.fsync(f.fileno()) # Force OS to write to disk
# Atomic rename
if os.path.exists(CONFIG_FILE):
os.replace(temp_file, CONFIG_FILE)
else:
os.rename(temp_file, CONFIG_FILE)
print(f"💾 Configuration saved successfully to {CONFIG_FILE}")
return True
except PermissionError:
print(f"❌ Permission denied saving config to {CONFIG_FILE}")
return False
except Exception as e:
print(f"❌ Error saving config: {e}")
# Clean up temp file if it exists
try:
temp_file = CONFIG_FILE + ".tmp"
if os.path.exists(temp_file):
os.remove(temp_file)
except:
pass
return False
def save_config(config):
"""Save configuration to file with robust error handling."""
with _config_lock:
return _save_config_unsafe(config)
def get_config_value(key, default=None):
"""Get a specific configuration value."""
config = load_config()
return config.get(key, default)
def set_config_value(key, value):
"""Set a specific configuration value."""
config = load_config()
config[key] = value
return save_config(config)
def reset_config():
"""Reset configuration to defaults."""
try:
create_config_backup()
default_config = DEFAULT_CONFIG.copy()
default_config["_last_updated"] = time.time()
# Sync _config_version with VRITRA_VERSION when resetting
try:
import sys
if 'vritraai' in sys.modules:
vritraai_module = sys.modules['vritraai']
if hasattr(vritraai_module, 'VRITRA_VERSION'):
default_config["_config_version"] = vritraai_module.VRITRA_VERSION
except Exception:
pass # Silently fail if version sync not possible
if save_config(default_config):
print("🔄 Configuration reset to defaults")
return True
except Exception as e:
print(f"❌ Error resetting config: {e}")
return False
# Load configuration with new robust system
config = load_config()
API_KEY = config.get("api_key", "")
GEMINI_API_KEY = config.get("gemini_api_key", "")
API_BASE = config.get("api_base", "gemini")
MODEL = config.get("model", "gemini-flash-latest")
# Export main functions for external use
__all__ = ['load_config', 'save_config', 'get_config_value', 'set_config_value', 'reset_config',
'API_KEY', 'GEMINI_API_KEY', 'API_BASE', 'MODEL', 'CONFIG_FILE', 'CONFIG_DIR']