"""Keep a full page of device identities visible within the inline response budget.""" import json from typing import Any def compact_scada_catalog(output: dict[str, Any], budget: int) -> dict[str, Any] | None: """Project attributes, never rows; full task evidence is persisted before this step.""" rows = output.get("data") if output.get("source") != "list_scada_devices" or not isinstance(rows, list): return None if not all(isinstance(row, dict) and "device_id" in row for row in rows): return None fields = ("device_id", "device_type", "measurement_unit") preview = { **output, "data": [{key: row[key] for key in fields if key in row} for row in rows], "truncated": True, "data_gaps": [ *output.get("data_gaps", []), "All devices in this page are listed with IDs, types and units; " "other attributes are omitted. Use get_scada_device for model mapping, " "coordinates and other device details. Use page.has_more to check for more devices.", ], } if len(json.dumps(preview, ensure_ascii=False).encode()) > budget: return None return preview