-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathxml_config.py
More file actions
286 lines (223 loc) · 8.69 KB
/
xml_config.py
File metadata and controls
286 lines (223 loc) · 8.69 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
"""Configuration system for XML-enhanced prompting features.
Provides feature flags and settings for all 6 XML enhancement options:
1. Workflow migration settings
2. XML schema validation
3. Prompt metrics tracking
4. Context window optimization
5. Dynamic prompt adaptation
6. Multi-language support
Copyright 2026 Smart-AI-Memory
Licensed under Fair Source License 0.9
"""
import json
import os
from dataclasses import asdict, dataclass, field
from pathlib import Path
def _validate_file_path(path: str, allowed_dir: str | None = None) -> Path:
"""Validate file path to prevent path traversal and arbitrary writes.
Args:
path: File path to validate
allowed_dir: Optional directory to restrict writes to
Returns:
Validated Path object
Raises:
ValueError: If path is invalid or unsafe
"""
if not path or not isinstance(path, str):
raise ValueError("path must be a non-empty string")
# Check for null bytes
if "\x00" in path:
raise ValueError("path contains null bytes")
try:
resolved = Path(path).resolve()
except (OSError, RuntimeError) as e:
raise ValueError(f"Invalid path: {e}")
# Check if within allowed directory
if allowed_dir:
try:
allowed = Path(allowed_dir).resolve()
resolved.relative_to(allowed)
except ValueError:
raise ValueError(f"path must be within {allowed_dir}")
# Check for dangerous system paths
dangerous_paths = ["/etc", "/sys", "/proc", "/dev"]
for dangerous in dangerous_paths:
if str(resolved).startswith(dangerous):
raise ValueError(f"Cannot write to system directory: {dangerous}")
return resolved
@dataclass
class XMLConfig:
"""XML prompting configuration.
Controls XML-enhanced prompt behavior and validation.
"""
use_xml_structure: bool = True # Default to XML prompts
validate_schemas: bool = False # Feature flag for validation
schema_dir: str = ".empathy/schemas"
strict_validation: bool = False # Fail on validation errors
@dataclass
class OptimizationConfig:
"""Context window optimization configuration.
Controls prompt compression and token reduction strategies.
"""
compression_level: str = "moderate" # none, light, moderate, aggressive
use_short_tags: bool = True
strip_whitespace: bool = True
cache_system_prompts: bool = True
max_context_tokens: int = 8000
@dataclass
class AdaptiveConfig:
"""Adaptive prompting configuration.
Controls dynamic model tier and compression selection based on task complexity.
"""
enable_adaptation: bool = True
model_tier_mapping: dict[str, str] = field(
default_factory=lambda: {
"simple": "gpt-3.5-turbo",
"moderate": "gpt-4",
"complex": "gpt-4",
"very_complex": "gpt-4-turbo-preview",
}
)
complexity_thresholds: dict[str, int] = field(
default_factory=lambda: {
"simple_tokens": 100,
"moderate_tokens": 500,
"complex_tokens": 2000,
}
)
@dataclass
class I18nConfig:
"""Internationalization configuration.
Controls multi-language support for XML prompts.
"""
default_language: str = "en"
translate_tags: bool = False # Keep tags in English by default
translate_content: bool = True
fallback_to_english: bool = True
translation_dir: str = ".empathy/translations"
@dataclass
class MetricsConfig:
"""Metrics tracking configuration.
Controls prompt performance metrics collection and storage.
"""
enable_tracking: bool = True
metrics_file: str = ".empathy/prompt_metrics.json"
track_token_usage: bool = True
track_latency: bool = True
track_retries: bool = True
track_parsing_success: bool = True
@dataclass
class EmpathyXMLConfig:
"""Main Empathy XML enhancement configuration.
Combines all feature configurations with centralized management.
Usage:
config = EmpathyXMLConfig.load_from_file()
if config.xml.use_xml_structure:
use_xml_prompts()
# Or create custom config
config = EmpathyXMLConfig(
xml=XMLConfig(validate_schemas=True),
metrics=MetricsConfig(enable_tracking=True)
)
config.save_to_file()
"""
xml: XMLConfig = field(default_factory=XMLConfig)
optimization: OptimizationConfig = field(default_factory=OptimizationConfig)
adaptive: AdaptiveConfig = field(default_factory=AdaptiveConfig)
i18n: I18nConfig = field(default_factory=I18nConfig)
metrics: MetricsConfig = field(default_factory=MetricsConfig)
@classmethod
def load_from_file(cls, config_file: str = ".empathy/config.json") -> "EmpathyXMLConfig":
"""Load configuration from JSON file.
Args:
config_file: Path to config file (default: .empathy/config.json)
Returns:
EmpathyXMLConfig instance loaded from file, or default config if file doesn't exist
"""
# Validate path to prevent path traversal attacks
try:
validated_path = _validate_file_path(config_file)
except ValueError:
# Return default config if path is invalid
return cls()
if not validated_path.exists():
# Return default config if file doesn't exist
return cls()
try:
with open(validated_path) as f:
data = json.load(f)
# Reconstruct nested dataclasses
return cls(
xml=XMLConfig(**data.get("xml", {})),
optimization=OptimizationConfig(**data.get("optimization", {})),
adaptive=AdaptiveConfig(**data.get("adaptive", {})),
i18n=I18nConfig(**data.get("i18n", {})),
metrics=MetricsConfig(**data.get("metrics", {})),
)
except Exception as e:
# Return default config on error
print(f"Warning: Failed to load config from {config_file}: {e}")
return cls()
def save_to_file(self, config_file: str = ".empathy/config.json") -> None:
"""Save configuration to JSON file.
Args:
config_file: Path to save config (default: .empathy/config.json)
"""
validated_path = _validate_file_path(config_file)
validated_path.parent.mkdir(parents=True, exist_ok=True)
data = {
"xml": asdict(self.xml),
"optimization": asdict(self.optimization),
"adaptive": asdict(self.adaptive),
"i18n": asdict(self.i18n),
"metrics": asdict(self.metrics),
}
with open(validated_path, "w") as f:
json.dump(data, f, indent=2)
@classmethod
def from_env(cls) -> "EmpathyXMLConfig":
"""Load configuration from environment variables.
Environment variables:
EMPATHY_XML_ENABLED: Enable XML prompts (default: true)
EMPATHY_VALIDATION_ENABLED: Enable schema validation (default: false)
EMPATHY_METRICS_ENABLED: Enable metrics tracking (default: true)
EMPATHY_OPTIMIZATION_LEVEL: Compression level (default: moderate)
EMPATHY_ADAPTIVE_ENABLED: Enable adaptive prompts (default: true)
Returns:
EmpathyXMLConfig with settings from environment variables
"""
return cls(
xml=XMLConfig(
use_xml_structure=os.getenv("EMPATHY_XML_ENABLED", "true").lower() == "true",
validate_schemas=os.getenv("EMPATHY_VALIDATION_ENABLED", "false").lower() == "true",
),
optimization=OptimizationConfig(
compression_level=os.getenv("EMPATHY_OPTIMIZATION_LEVEL", "moderate"),
),
adaptive=AdaptiveConfig(
enable_adaptation=os.getenv("EMPATHY_ADAPTIVE_ENABLED", "true").lower() == "true",
),
metrics=MetricsConfig(
enable_tracking=os.getenv("EMPATHY_METRICS_ENABLED", "true").lower() == "true",
),
)
# Global default configuration instance
_global_config: EmpathyXMLConfig | None = None
def get_config() -> EmpathyXMLConfig:
"""Get global configuration instance.
Returns cached config or loads from file if not yet loaded.
Returns:
Global EmpathyXMLConfig instance
"""
global _global_config
if _global_config is None:
# Try to load from file, fall back to defaults
_global_config = EmpathyXMLConfig.load_from_file()
return _global_config
def set_config(config: EmpathyXMLConfig) -> None:
"""Set global configuration instance.
Args:
config: EmpathyXMLConfig to use globally
"""
global _global_config
_global_config = config