feat: Enhance placeholder system with AI context support
- Introduced `build_ai_placeholder_caption` function in `placeholder_registry.py` to generate AI context captions based on placeholder metadata. - Updated `resolve_placeholders` in `placeholder_resolver.py` to support modifiers for AI context, allowing for enhanced descriptions when placeholders are resolved. - Modified `get_placeholder_catalog` to include AI captions in the output, improving the metadata available for placeholders. - Adjusted `export_placeholder_values` to include AI captions in the exported data, enhancing the information provided to users. These changes improve the flexibility and functionality of the placeholder system, enabling richer context generation for dynamic content.
This commit is contained in:
1 parent
41bf593d4c
commit
baeddd7c13
5 files changed
+146
-27
No files matched your search
@@ -47,6 +47,20 @@ from data_layer.health_metrics import (
|
||||
get_vo2_max_data
|
||||
)
|
||||
|
||||
from placeholder_registry import build_ai_placeholder_caption, get_registry
|
||||
|
||||
# {{key}} oder {{key|d}} — Modifier d hängt KI-Kontext (ai_caption) an
|
||||
_PLACEHOLDER_TOKEN_RE = re.compile(
|
||||
r"\{\{\s*([a-zA-Z0-9_]+)(?:\s*\|\s*([a-zA-Z0-9_,\s]+))?\s*\}\}"
|
||||
)
|
||||
|
||||
|
||||
def _ai_caption_for_placeholder_key(key: str) -> Optional[str]:
|
||||
meta = get_registry().get(key)
|
||||
if meta:
|
||||
return build_ai_placeholder_caption(meta)
|
||||
return None
|
||||
|
||||
|
||||
# ── Helper Functions ──────────────────────────────────────────────────────────
|
||||
|
||||
@@ -1588,6 +1602,10 @@ def resolve_placeholders(template: str, profile_id: str) -> str:
|
||||
"""
|
||||
Replace all {{placeholders}} in template with actual user data.
|
||||
|
||||
Unterstützt Modifier wie bei der Prompt-Pipeline:
|
||||
- {{fat_avg}} — nur Wert
|
||||
- {{fat_avg|d}} — Wert plus KI-Kontext (business_meaning / semantic_contract aus Registry)
|
||||
|
||||
Args:
|
||||
template: Prompt template with placeholders
|
||||
profile_id: User profile ID
|
||||
@@ -1595,18 +1613,26 @@ def resolve_placeholders(template: str, profile_id: str) -> str:
|
||||
Returns:
|
||||
Resolved template with placeholders replaced by values
|
||||
"""
|
||||
result = template
|
||||
|
||||
for placeholder, resolver in PLACEHOLDER_MAP.items():
|
||||
if placeholder in result:
|
||||
try:
|
||||
value = resolver(profile_id)
|
||||
result = result.replace(placeholder, str(value))
|
||||
except Exception as e:
|
||||
# On error, replace with error message
|
||||
result = result.replace(placeholder, f"[Fehler: {placeholder}]")
|
||||
def _repl(match: re.Match) -> str:
|
||||
key = match.group(1)
|
||||
modifiers_raw = (match.group(2) or "").strip()
|
||||
mods = {x.strip().lower() for x in modifiers_raw.split(",") if x.strip()}
|
||||
ph = f"{{{{{key}}}}}"
|
||||
resolver = PLACEHOLDER_MAP.get(ph)
|
||||
if not resolver:
|
||||
return match.group(0)
|
||||
try:
|
||||
value = str(resolver(profile_id))
|
||||
except Exception:
|
||||
return f"[Fehler: {ph}]"
|
||||
if "d" in mods:
|
||||
cap = _ai_caption_for_placeholder_key(key)
|
||||
if cap:
|
||||
value = f"{value} — {cap}"
|
||||
return value
|
||||
|
||||
return result
|
||||
return _PLACEHOLDER_TOKEN_RE.sub(_repl, template)
|
||||
|
||||
|
||||
def get_unknown_placeholders(template: str) -> List[str]:
|
||||
@@ -1619,12 +1645,9 @@ def get_unknown_placeholders(template: str) -> List[str]:
|
||||
Returns:
|
||||
List of unknown placeholder names (without {{}})
|
||||
"""
|
||||
# Find all {{...}} patterns
|
||||
found = re.findall(r'\{\{(\w+)\}\}', template)
|
||||
|
||||
# Filter to only unknown ones
|
||||
found = _PLACEHOLDER_TOKEN_RE.findall(template)
|
||||
known_names = {p.strip('{}') for p in PLACEHOLDER_MAP.keys()}
|
||||
unknown = [p for p in found if p not in known_names]
|
||||
unknown = [key for key, _ in found if key not in known_names]
|
||||
|
||||
return list(set(unknown)) # Remove duplicates
|
||||
|
||||
@@ -1781,7 +1804,8 @@ def get_placeholder_catalog(profile_id: str) -> Dict[str, List[Dict[str, str]]]:
|
||||
catalog[category].append({
|
||||
'key': key,
|
||||
'description': metadata.description,
|
||||
'example': str(example)
|
||||
'example': str(example),
|
||||
'ai_caption': build_ai_placeholder_caption(metadata),
|
||||
})
|
||||
|
||||
# Legacy placeholders (not in registry yet)
|
||||
@@ -1810,7 +1834,8 @@ def get_placeholder_catalog(profile_id: str) -> Dict[str, List[Dict[str, str]]]:
|
||||
catalog[category].append({
|
||||
'key': key,
|
||||
'description': description,
|
||||
'example': str(example)
|
||||
'example': str(example),
|
||||
'ai_caption': description,
|
||||
})
|
||||
|
||||
# Add ALL remaining placeholders from PLACEHOLDER_MAP that aren't categorized yet
|
||||
@@ -1839,7 +1864,8 @@ def get_placeholder_catalog(profile_id: str) -> Dict[str, List[Dict[str, str]]]:
|
||||
catalog[sonstige_category].append({
|
||||
'key': key,
|
||||
'description': f'Platzhalter: {key}', # Generic description
|
||||
'example': str(example)
|
||||
'example': str(example),
|
||||
'ai_caption': f'Platzhalter {key} (noch ohne erweiterte Registry-Beschreibung).',
|
||||
})
|
||||
|
||||
return catalog
|
||||
Reference in new issue
Block a user