Skip to content

Commit 4e3b4cf

Browse files
committed
fix: Resolve production issues — history dir creation and sensor attributes > 16KB
History: - Add defensive os.makedirs in _sync_test_directory_write to ensure directory exists before write test (fixes ERRNO 2 on fresh install) Sensor attributes (fixes Recorder 16KB limit violation): - Reduce conversation_history preview: 3 entries × 256 chars (was 5 × 4096) - Reduce last response/question truncation to 2048 chars (was 4096) - Reduce system_prompt in attributes to 512 chars (was 4096) - Full data remains accessible via ha_text_ai.get_history service
1 parent c458289 commit 4e3b4cf

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

custom_components/ha_text_ai/history.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ async def _check_history_directory(self) -> None:
123123
@staticmethod
124124
def _sync_test_directory_write(test_file_path: str) -> None:
125125
try:
126+
os.makedirs(os.path.dirname(test_file_path), mode=0o755, exist_ok=True)
126127
with open(test_file_path, "w") as f:
127128
f.write("Permission test")
128129
os.remove(test_file_path)

custom_components/ha_text_ai/sensor.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
ATTR_SYSTEM_PROMPT,
4747
ATTR_RESPONSE,
4848
ATTR_QUESTION,
49-
ATTR_CONVERSATION_HISTORY,
5049
METRIC_TOTAL_TOKENS,
5150
METRIC_PROMPT_TOKENS,
5251
METRIC_COMPLETION_TOKENS,
@@ -67,7 +66,6 @@
6766
ENTITY_ICON_PROCESSING,
6867
DEFAULT_NAME_PREFIX,
6968
CONF_MAX_HISTORY_SIZE,
70-
MAX_ATTRIBUTE_SIZE,
7169
VERSION,
7270
)
7371

@@ -76,6 +74,11 @@
7674

7775
_LOGGER = logging.getLogger(__name__)
7876

77+
# HA Recorder limit is 16384 bytes for state_attributes.
78+
# Budget per field to stay well within the limit.
79+
_ATTR_TEXT_LIMIT = 2048
80+
_ATTR_PROMPT_LIMIT = 512
81+
7982

8083
async def async_setup_entry(
8184
hass: HomeAssistant,
@@ -243,7 +246,7 @@ def extra_state_attributes(self) -> Dict[str, Any]:
243246
ATTR_TOTAL_ERRORS: metrics.get("total_errors", 0),
244247
"instance_name": self._instance_name,
245248
"normalized_name": self._normalized_name,
246-
ATTR_SYSTEM_PROMPT: (data.get("system_prompt", "")[:MAX_ATTRIBUTE_SIZE]
249+
ATTR_SYSTEM_PROMPT: (data.get("system_prompt", "")[:_ATTR_PROMPT_LIMIT]
247250
if data.get("system_prompt") else None),
248251
ATTR_IS_PROCESSING: data.get("is_processing", False),
249252
ATTR_IS_RATE_LIMITED: data.get("is_rate_limited", False),
@@ -253,18 +256,19 @@ def extra_state_attributes(self) -> Dict[str, Any]:
253256
ATTR_HISTORY_SIZE: data.get("history_size", 0),
254257
}
255258

256-
# History limit
259+
# Conversation history preview (compact: last 3, truncated to 256 chars).
260+
# Full history is available via ha_text_ai.get_history service.
257261
conversation_history = data.get("conversation_history", [])
258262
if conversation_history:
259-
limited_history = []
260-
for entry in conversation_history:
261-
limited_entry = {
263+
preview = conversation_history[-3:]
264+
attributes["conversation_history"] = [
265+
{
262266
"timestamp": entry["timestamp"],
263-
"question": entry["question"][:MAX_ATTRIBUTE_SIZE],
264-
"response": entry["response"][:MAX_ATTRIBUTE_SIZE]
267+
"question": entry["question"][:256],
268+
"response": entry["response"][:256],
265269
}
266-
limited_history.append(limited_entry)
267-
attributes[ATTR_CONVERSATION_HISTORY] = limited_history
270+
for entry in preview
271+
]
268272

269273
# Metrics
270274
if isinstance(metrics, dict):
@@ -283,11 +287,11 @@ def extra_state_attributes(self) -> Dict[str, Any]:
283287
last_response = data.get("last_response", {})
284288
if isinstance(last_response, dict):
285289
attributes.update({
286-
ATTR_RESPONSE: last_response.get("response", "")[:MAX_ATTRIBUTE_SIZE],
287-
ATTR_QUESTION: last_response.get("question", "")[:MAX_ATTRIBUTE_SIZE],
290+
ATTR_RESPONSE: last_response.get("response", "")[:_ATTR_TEXT_LIMIT],
291+
ATTR_QUESTION: last_response.get("question", "")[:_ATTR_TEXT_LIMIT],
288292
"last_model": last_response.get("model", ""),
289293
"last_timestamp": last_response.get("timestamp", ""),
290-
"last_error": (last_response.get("error", "")[:MAX_ATTRIBUTE_SIZE]
294+
"last_error": (last_response.get("error", "")[:_ATTR_TEXT_LIMIT]
291295
if last_response.get("error") else None),
292296
})
293297

0 commit comments

Comments
 (0)