feat: enhance formatting and normalization of activity metrics
- Introduced `format_scalar_for_prompt_text` function to standardize the representation of scalar values in activity summaries and details. - Updated `get_activity_summary` and `get_activity_detail` functions to utilize the new formatting for improved readability. - Added normalization for float values in session metrics to prevent excessively long representations. - Enhanced unit tests to verify the new formatting and normalization behavior.
This commit is contained in:
1 parent
6756dc60f3
commit
178534e9eb
5 files changed
+103
-4
No files matched your search
@@ -13,9 +13,31 @@ from data_layer.activity_data_canon import (
|
||||
ACTIVITY_LOG_LEGACY_COLUMN_FOR_EAV_PRIMARY_PARAM,
|
||||
ACTIVITY_MODULE_REGISTRY_FIELD_KEYS,
|
||||
)
|
||||
from data_layer.prompt_output_compact import normalize_prompt_number
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _normalize_metric_value_for_read(data_type: str, val: Any) -> Any:
|
||||
"""Lesepfad (Layer 1): keine unnötig langen Float-Strings für KI/UI (Issue 53 / Platzhalter)."""
|
||||
if val is None:
|
||||
return None
|
||||
dt = (data_type or "").strip().lower()
|
||||
if dt == "string":
|
||||
return val
|
||||
if dt == "boolean":
|
||||
return bool(val)
|
||||
if dt == "integer":
|
||||
try:
|
||||
if isinstance(val, bool):
|
||||
return int(val)
|
||||
return int(val)
|
||||
except (TypeError, ValueError):
|
||||
return normalize_prompt_number(val)
|
||||
if dt == "float":
|
||||
return normalize_prompt_number(val)
|
||||
return normalize_prompt_number(val)
|
||||
|
||||
# Diese Spalten nicht aus CSV-Parameter-Zuordnung überschreiben (kommen aus Typ-Mapping / System).
|
||||
ACTIVITY_LOG_PATCH_FORBIDDEN = frozenset(
|
||||
{
|
||||
@@ -430,6 +452,8 @@ def merge_column_backed_and_eav_metrics(
|
||||
keys_handled.add(k)
|
||||
|
||||
merged.sort(key=lambda x: x["key"])
|
||||
for m in merged:
|
||||
m["value"] = _normalize_metric_value_for_read(m.get("data_type") or "", m.get("value"))
|
||||
return merged
|
||||
|
||||
|
||||
|
||||
@@ -70,6 +70,31 @@ def compact_json_payload_for_prompts(obj: Any) -> Any:
|
||||
return normalize_prompt_number(obj)
|
||||
|
||||
|
||||
def format_scalar_for_prompt_text(x: Any) -> str:
|
||||
"""
|
||||
Kurzdarstellung für Text-Platzhalter (activity_detail, Tabellen, …).
|
||||
Nutzt dieselbe Komprimierung wie JSON (normalize_prompt_number).
|
||||
"""
|
||||
if x is None:
|
||||
return "—"
|
||||
if isinstance(x, bool):
|
||||
return "ja" if x else "nein"
|
||||
if isinstance(x, str):
|
||||
return x
|
||||
n = normalize_prompt_number(x)
|
||||
if isinstance(n, bool):
|
||||
return "ja" if n else "nein"
|
||||
if isinstance(n, int) and not isinstance(n, bool):
|
||||
return str(n)
|
||||
if isinstance(n, float):
|
||||
if not math.isfinite(n):
|
||||
return str(n)
|
||||
if abs(n - round(n)) < 1e-9:
|
||||
return str(int(round(n)))
|
||||
return str(n)
|
||||
return str(n)
|
||||
|
||||
|
||||
def session_metrics_list_to_key_value_compact(metrics: list[Any] | None) -> dict[str, Any]:
|
||||
"""
|
||||
Session-Metriken für KI-JSON: nur key → Wert (keine wiederholten Namen/Beschreibungen).
|
||||
|
||||
Reference in new issue
Block a user