From e2d33ffce4d4e5fe99b7b4c3d33ca1167122be63 Mon Sep 17 00:00:00 2001 From: nesquena-hermes Date: Thu, 30 Apr 2026 16:17:59 +0000 Subject: [PATCH] fix(models): persist context_length/threshold_tokens/last_prompt_tokens in Session model (#1318 split) From PR #1341. Co-authored-by: fxd-jason --- api/models.py | 9 +++++++++ api/routes.py | 3 +++ 2 files changed, 12 insertions(+) diff --git a/api/models.py b/api/models.py index 981d0ccf..bf1dda8e 100644 --- a/api/models.py +++ b/api/models.py @@ -318,6 +318,8 @@ class Session: context_messages=None, compression_anchor_visible_idx=None, compression_anchor_message_key=None, + context_length=None, threshold_tokens=None, + last_prompt_tokens=None, **kwargs): self.session_id = session_id or uuid.uuid4().hex[:12] self.title = title @@ -342,6 +344,9 @@ class Session: self.context_messages = context_messages if isinstance(context_messages, list) else [] self.compression_anchor_visible_idx = compression_anchor_visible_idx self.compression_anchor_message_key = compression_anchor_message_key + self.context_length = context_length + self.threshold_tokens = threshold_tokens + self.last_prompt_tokens = last_prompt_tokens self._metadata_message_count = None @property @@ -361,6 +366,7 @@ class Session: 'personality', 'active_stream_id', 'pending_user_message', 'pending_attachments', 'pending_started_at', 'compression_anchor_visible_idx', 'compression_anchor_message_key', + 'context_length', 'threshold_tokens', 'last_prompt_tokens', ] meta = {k: getattr(self, k, None) for k in METADATA_FIELDS} meta['messages'] = self.messages @@ -452,6 +458,9 @@ class Session: 'personality': self.personality, 'compression_anchor_visible_idx': self.compression_anchor_visible_idx, 'compression_anchor_message_key': self.compression_anchor_message_key, + 'context_length': self.context_length, + 'threshold_tokens': self.threshold_tokens, + 'last_prompt_tokens': self.last_prompt_tokens, 'active_stream_id': self.active_stream_id, 'is_streaming': _is_streaming_session( self.active_stream_id, active_stream_ids diff --git a/api/routes.py b/api/routes.py index 7ec990f5..81815204 100644 --- a/api/routes.py +++ b/api/routes.py @@ -921,6 +921,9 @@ def handle_get(handler, parsed) -> bool: "pending_user_message": getattr(s, "pending_user_message", None), "pending_attachments": getattr(s, "pending_attachments", []) if load_messages else [], "pending_started_at": getattr(s, "pending_started_at", None), + "context_length": getattr(s, "context_length", 0) or 0, + "threshold_tokens": getattr(s, "threshold_tokens", 0) or 0, + "last_prompt_tokens": getattr(s, "last_prompt_tokens", 0) or 0, } # Signal to the frontend that older messages were omitted. # For msg_before paging, compare against the filtered set,