feat: Refine placeholder resolution with enhanced modifiers support
Deploy Development / deploy (push) Successful in 55s
Build Test / pytest-backend (push) Successful in 4s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 17s

- Updated `resolve_placeholders` in `prompt_executor.py` to support combined modifiers for placeholders, allowing for more flexible output formats.
- Enhanced `build_ai_placeholder_caption` in `placeholder_registry.py` to clarify the generation of AI context captions, focusing on descriptions and explanations.
- Introduced new helper functions in `placeholder_resolver.py` to streamline the retrieval of descriptions and explanations for placeholders.
- Modified tests to cover new functionality, ensuring accurate behavior for combined modifiers and improved placeholder resolution.

These changes enhance the usability and clarity of placeholder outputs, providing users with richer contextual information.
This commit is contained in:
Lars committed 2026-04-11 21:58:29 +02:00
1 parent a9a414b956
commit 4868e44882
5 files changed
+151 -91

No files matched your search

+22 -10
View File
@@ -1,4 +1,4 @@
"""Tests für {{key|d}}, ai_caption und Unbekannt-Erkennung."""
"""Tests für {{key|d}}, {{key|x}}, ai_caption und Unbekannt-Erkennung."""
from placeholder_registry import (
PlaceholderMetadata,
PlaceholderType,
@@ -7,9 +7,10 @@ from placeholder_registry import (
)
import placeholder_resolver as pr
from placeholder_resolver import format_value_with_d_modifier
from prompt_executor import resolve_placeholders
def test_build_ai_caption_prefers_business_meaning():
def test_build_ai_caption_is_explanation_only():
m = PlaceholderMetadata(
key="test_x",
category="Test",
@@ -23,11 +24,11 @@ def test_build_ai_caption_prefers_business_meaning():
output_type=OutputType.NUMERIC,
)
cap = build_ai_placeholder_caption(m)
assert cap.startswith("Kurzbeschreibung")
assert "Kernbedeutung" in cap
assert "Kurzbeschreibung" not in cap
def test_build_ai_caption_description_then_meaning_like_protein_avg():
def test_build_ai_caption_protein_avg_no_description_prefix():
m = PlaceholderMetadata(
key="protein_avg",
category="Ernährung",
@@ -40,8 +41,8 @@ def test_build_ai_caption_description_then_meaning_like_protein_avg():
output_type=OutputType.NUMERIC,
)
cap = build_ai_placeholder_caption(m)
assert cap.startswith("Durchschn. Protein in g (30d)")
assert "Muskelerhalt" in cap
assert cap.startswith("Zentraler Placeholder")
assert "Durchschn. Protein" not in cap
assert "Technischer Bezug" not in cap
@@ -69,6 +70,8 @@ def test_placeholder_token_regex_optional_modifier():
assert m1 and m1.group(1) == "fat_avg" and m1.group(2).strip() == "d"
m2 = pr._PLACEHOLDER_TOKEN_RE.search("{{ protein_avg | d }}")
assert m2 and m2.group(1) == "protein_avg" and m2.group(2).strip() == "d"
m3 = pr._PLACEHOLDER_TOKEN_RE.search("{{k|d,x}}")
assert m3 and m3.group(1) == "k" and m3.group(2).strip() == "d,x"
def test_get_unknown_placeholders_strips_modifier():
@@ -76,17 +79,26 @@ def test_get_unknown_placeholders_strips_modifier():
assert set(unk) == {"not_a_real_key"}
def test_format_value_with_d_modifier_matches_prompt_executor():
def test_format_value_with_d_modifier_uses_description_only():
row = {
"key": "protein_avg",
"description": "Durchschn. Protein in g (30d)",
"example": "119g/Tag",
"ai_caption": "Durchschn. Protein in g (30d). Zentral für Muskelerhalt.",
"ai_caption": "Nur für |x",
}
out = format_value_with_d_modifier("119g/Tag", row)
assert out == "119g/Tag — Durchschn. Protein in g (30d). Zentral für Muskelerhalt."
assert out == "119g/Tag — Durchschn. Protein in g (30d)"
assert "Nur für |x" not in out
def test_format_value_with_d_modifier_falls_back_to_description():
row = {"description": "Nur Beschreibung", "key": "x"}
assert format_value_with_d_modifier("42", row) == "42 — Nur Beschreibung"
def test_prompt_executor_modifiers_d_x_combined():
catalog = {"E": [{"key": "p", "description": "Desc", "ai_caption": "Expl"}]}
v = {"p": "99"}
assert resolve_placeholders("{{p|d}}", v, None, catalog) == "99 — Desc"
assert resolve_placeholders("{{p|x}}", v, None, catalog) == "Expl"
assert resolve_placeholders("{{p|d,x}}", v, None, catalog) == "99 — Desc — Expl"
assert resolve_placeholders("{{p}}", v, None, catalog) == "99"