1- """Pure text helpers for the cascade: sentence splitting and history trimming .
1+ """Pure text helpers for the cascade: sentence and clause splitting .
22
33Kept Rich-free and dependency-light so the orchestration logic in ``engine`` can
4- be unit-tested without any I/O.
4+ be unit-tested without any I/O. (Conversation-history trimming used to live here as a
5+ client-side sliding window; the live brain now delegates context-window management to
6+ the deepagents ``SummarizationMiddleware`` instead — see :mod:`aai_cli.agent_cascade.brain`.)
57"""
68
79from __future__ import annotations
@@ -27,6 +29,17 @@ def _is_boundary(text: str, index: int) -> bool:
2729 return index + 1 < len (text ) and text [index + 1 ].isspace ()
2830
2931
32+ def _ends_sentence (text : str , index : int , char : str ) -> bool :
33+ """True when ``char`` at ``index`` closes a sentence: a terminator at end-of-text or
34+ followed by whitespace.
35+
36+ Unlike :func:`_is_boundary` (used for partial streamed chunks), end-of-text *does* close a
37+ sentence here: :func:`split_sentences` runs on a complete reply, so a trailing terminator is a
38+ real boundary, not a possibly-mid-token one.
39+ """
40+ return char in _TERMINATORS and (index + 1 == len (text ) or text [index + 1 ].isspace ())
41+
42+
3043def pop_clauses (buffer : str , * , min_chars : int ) -> tuple [list [str ], str ]:
3144 """Pull complete speakable clauses off the front of ``buffer`` for incremental TTS.
3245
@@ -68,7 +81,7 @@ def split_sentences(text: str) -> list[str]:
6881 sentences : list [str ] = []
6982 start = 0
7083 for index , char in enumerate (text ):
71- if char in _TERMINATORS and ( index + 1 == len ( text ) or text [ index + 1 ]. isspace () ):
84+ if _ends_sentence ( text , index , char ):
7285 # Boundary confirmed (end-of-text or a following space); the slice includes
7386 # the terminator, so it is never blank after stripping leading whitespace.
7487 sentences .append (text [start : index + 1 ].strip ())
@@ -77,13 +90,3 @@ def split_sentences(text: str) -> list[str]:
7790 if tail :
7891 sentences .append (tail )
7992 return sentences
80-
81-
82- def trim_history [T ](history : list [T ], max_messages : int ) -> None :
83- """Cap ``history`` to its most recent ``max_messages`` entries, in place.
84-
85- A sliding window over the conversation so an unbounded chat doesn't grow the
86- context (and the per-turn token cost) without limit.
87- """
88- if len (history ) > max_messages :
89- del history [: len (history ) - max_messages ]
0 commit comments