41
41
ABSOLUTE_MAX_HISTORY_SIZE ,
42
42
MAX_ATTRIBUTE_SIZE ,
43
43
MAX_HISTORY_FILE_SIZE ,
44
+ TRUNCATION_INDICATOR ,
44
45
)
45
46
46
47
_LOGGER = logging .getLogger (__name__ )
@@ -647,15 +648,14 @@ async def _rotate_history_files(self) -> None:
647
648
648
649
async def _async_update_data (self ) -> Dict [str , Any ]:
649
650
"""Update coordinator data with improved error handling and performance."""
650
-
651
651
try :
652
652
async with asyncio .Semaphore (1 ):
653
653
current_state = self ._get_current_state ()
654
654
655
- limited_history = await self ._get_limited_history ()
655
+ # Get limited history with info
656
+ history_data = await self ._get_limited_history ()
656
657
657
658
metrics = await self ._get_current_metrics ()
658
-
659
659
if metrics is None :
660
660
metrics = {}
661
661
@@ -670,47 +670,68 @@ async def _async_update_data(self) -> Dict[str, Any]:
670
670
"uptime" : self ._calculate_uptime (),
671
671
"system_prompt" : self ._get_truncated_system_prompt (),
672
672
"history_size" : len (self ._conversation_history ),
673
- "conversation_history" : limited_history ,
673
+ "conversation_history" : history_data ["entries" ],
674
+ "history_info" : history_data ["info" ],
674
675
"normalized_name" : self .normalized_name ,
675
676
}
676
677
677
678
await self ._validate_update_data (data )
678
-
679
679
return data
680
680
681
- except asyncio .CancelledError :
682
- _LOGGER .warning ("Update was cancelled" )
683
- raise
684
681
except Exception as err :
685
682
_LOGGER .error (f"Error updating data: { err } " , exc_info = True )
686
683
return self ._get_safe_initial_state ()
687
684
688
- async def _get_limited_history (self ) -> List [ Dict [str , Any ] ]:
689
- """Get limited conversation history with size constraints ."""
685
+ async def _get_limited_history (self ) -> Dict [str , Any ]:
686
+ """Get limited conversation history showing only last Q&A ."""
690
687
limited_history = []
688
+
691
689
if self ._conversation_history :
692
- for entry in self ._conversation_history [- 3 :]:
693
- limited_entry = {
694
- "timestamp" : entry ["timestamp" ],
695
- "question" : self ._truncate_text (entry ["question" ]),
696
- "response" : self ._truncate_text (entry ["response" ]),
697
- }
698
- limited_history .append (limited_entry )
699
- return limited_history
690
+ last_entry = self ._conversation_history [- 1 ]
691
+ limited_entry = {
692
+ "timestamp" : last_entry ["timestamp" ],
693
+ "question" : last_entry ["question" ][:4096 ] + (TRUNCATION_INDICATOR if len (last_entry ["question" ]) > 4096 else "" ),
694
+ "response" : last_entry ["response" ][:4096 ] + (TRUNCATION_INDICATOR if len (last_entry ["response" ]) > 4096 else "" ),
695
+ }
696
+ limited_history .append (limited_entry )
700
697
701
- def _truncate_text (self , text : str , max_length : int = MAX_ATTRIBUTE_SIZE ) -> str :
702
- """Safely truncate text to maximum length."""
703
- return text [:max_length ] if text else ""
698
+ history_info = {
699
+ "total_entries" : len (self ._conversation_history ),
700
+ "displayed_entries" : len (limited_history ),
701
+ "full_history_available" : True ,
702
+ "history_path" : self ._history_file ,
703
+ }
704
+
705
+ return {
706
+ "entries" : limited_history ,
707
+ "full_history" : self ._conversation_history ,
708
+ "info" : history_info
709
+ }
704
710
705
711
async def _get_sanitized_last_response (self ) -> Dict [str , Any ]:
706
- """Get sanitized version of last response."""
712
+ """Get sanitized version of last response with truncation indicators ."""
707
713
response = self .last_response .copy ()
714
+
708
715
if "response" in response :
709
- response ["response" ] = self ._truncate_text (response ["response" ])
716
+ original_response = response ["response" ]
717
+ is_response_truncated = len (original_response ) > 4096
718
+ response ["response" ] = original_response [:4096 ] + (TRUNCATION_INDICATOR if is_response_truncated else "" )
719
+ response ["is_response_truncated" ] = is_response_truncated
720
+ response ["full_response_length" ] = len (original_response )
721
+
710
722
if "question" in response :
711
- response ["question" ] = self ._truncate_text (response ["question" ])
723
+ original_question = response ["question" ]
724
+ is_question_truncated = len (original_question ) > 4096
725
+ response ["question" ] = original_question [:4096 ] + (TRUNCATION_INDICATOR if is_question_truncated else "" )
726
+ response ["is_question_truncated" ] = is_question_truncated
727
+ response ["full_question_length" ] = len (original_question )
728
+
712
729
return response
713
730
731
+ def _truncate_text (self , text : str , max_length : int = MAX_ATTRIBUTE_SIZE ) -> str :
732
+ """Safely truncate text to maximum length."""
733
+ return text [:max_length ] if text else ""
734
+
714
735
def _calculate_uptime (self ) -> float :
715
736
"""Calculate current uptime in seconds."""
716
737
return (dt_util .utcnow () - self ._start_time ).total_seconds ()
0 commit comments