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 ,
6766 ENTITY_ICON_PROCESSING ,
6867 DEFAULT_NAME_PREFIX ,
6968 CONF_MAX_HISTORY_SIZE ,
70- MAX_ATTRIBUTE_SIZE ,
7169 VERSION ,
7270)
7371
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
8083async 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