@@ -54,7 +54,7 @@ async def test_maybe_compact_events_success(self):
5454 self ._create_event (1.0 , 'Hello' , 'user' ),
5555 self ._create_event (2.0 , 'Hi there!' , 'model' ),
5656 ]
57- expected_conversation_history = 'user: Hello\\ nmodel: Hi there!'
57+ expected_conversation_history = 'user: Hello\n model: Hi there!'
5858 expected_prompt = self .compactor ._DEFAULT_PROMPT_TEMPLATE .format (
5959 conversation_history = expected_conversation_history
6060 )
@@ -162,7 +162,7 @@ def test_format_events_for_prompt(self):
162162 parts = [
163163 Part (
164164 function_call = FunctionCall (
165- id = 'call_1' , name = 'tool' , args = {}
165+ id = 'call_1' , name = 'tool' , args = {'q' : 'x' }
166166 )
167167 )
168168 ]
@@ -186,8 +186,80 @@ def test_format_events_for_prompt(self):
186186 ),
187187 ]
188188 expected_formatted_history = (
189- 'user: User says...\\ nmodel: Model replies...\\ nuser: Another user'
190- ' input\\ nmodel: More model text'
189+ 'user: User says...\n model: Model replies...\n user: Another user'
190+ ' input\n model: More model text\n model called tool:'
191+ " tool({'q': 'x'})\n Tool response from tool: {'result': 'done'}"
191192 )
192193 formatted_history = self .compactor ._format_events_for_prompt (events )
193194 self .assertEqual (formatted_history , expected_formatted_history )
195+
196+ def test_format_events_for_prompt_includes_thoughts (self ):
197+ events = [
198+ self ._create_event (1.0 , 'What is the weather?' , 'user' ),
199+ Event (
200+ timestamp = 2.0 ,
201+ author = 'model' ,
202+ content = Content (
203+ parts = [
204+ Part (text = 'Let me check the tool output.' , thought = True ),
205+ Part (text = 'It is sunny.' ),
206+ ]
207+ ),
208+ ),
209+ ]
210+ expected_formatted_history = (
211+ 'user: What is the weather?\n model (thought): Let me check the tool'
212+ ' output.\n model: It is sunny.'
213+ )
214+ formatted_history = self .compactor ._format_events_for_prompt (events )
215+ self .assertEqual (formatted_history , expected_formatted_history )
216+
217+ def test_format_events_for_prompt_skips_compaction_event_thought (self ):
218+ events = [
219+ Event (
220+ timestamp = 1.0 ,
221+ author = 'model' ,
222+ content = Content (
223+ parts = [
224+ Part (text = 'Stale summarizer reasoning.' , thought = True ),
225+ Part (text = 'Prior summary.' ),
226+ ]
227+ ),
228+ actions = EventActions (
229+ compaction = EventCompaction (
230+ start_timestamp = 0.0 ,
231+ end_timestamp = 1.0 ,
232+ compacted_content = Content (parts = [Part (text = 'Prior' )]),
233+ )
234+ ),
235+ ),
236+ self ._create_event (2.0 , 'New user input' , 'user' ),
237+ ]
238+ expected_formatted_history = 'model: Prior summary.\n user: New user input'
239+ formatted_history = self .compactor ._format_events_for_prompt (events )
240+ self .assertEqual (formatted_history , expected_formatted_history )
241+
242+ def test_format_events_for_prompt_truncates_large_tool_response (self ):
243+ limit = self .compactor ._MAX_TOOL_CONTENT_CHARS
244+ large_value = 'x' * (limit + 500 )
245+ events = [
246+ Event (
247+ timestamp = 1.0 ,
248+ author = 'model' ,
249+ content = Content (
250+ parts = [
251+ Part (
252+ function_response = FunctionResponse (
253+ id = 'call_1' ,
254+ name = 'search' ,
255+ response = {'data' : large_value },
256+ )
257+ )
258+ ]
259+ ),
260+ ),
261+ ]
262+ formatted_history = self .compactor ._format_events_for_prompt (events )
263+ self .assertIn ('Tool response from search:' , formatted_history )
264+ self .assertIn ('... [truncated' , formatted_history )
265+ self .assertLess (len (formatted_history ), len (large_value ))
0 commit comments