@@ -50,6 +50,14 @@ async def run_async(
50
50
invocation_context .session .events ,
51
51
agent .name ,
52
52
)
53
+ elif agent .include_contents == 'windowed' :
54
+ llm_request .contents = _get_windowed_turn_contents (
55
+ ## Includes windowed turn contexts (windowed conversation history)
56
+ invocation_context .branch ,
57
+ invocation_context .session .events ,
58
+ agent .name ,
59
+ history_window = agent .history_window_size
60
+ )
53
61
else :
54
62
# Include current turn context only (no conversation history)
55
63
llm_request .contents = _get_current_turn_contents (
@@ -282,6 +290,39 @@ def _get_current_turn_contents(
282
290
283
291
return []
284
292
293
+ def _get_windowed_turn_contents (
294
+ current_branch : Optional [str ],
295
+ events : list [Event ],
296
+ agent_name : str = '' ,
297
+ history_window : int = 5 # New argument
298
+ ) -> list [types .Content ]:
299
+ """Get contents for the current turn, optionally with limited conversation history.
300
+
301
+ Args:
302
+ current_branch: The current branch of the agent.
303
+ events: A list of all session events.
304
+ agent_name: The name of the agent.
305
+ history_window: Number of previous contents to include from conversation history.
306
+
307
+ Returns:
308
+ A list of contents for the current turn and optional recent history.
309
+ """
310
+ # Find the start of the current turn
311
+ turn_start_index = 0
312
+ for i in range (len (events ) - 1 , - 1 , - 1 ):
313
+ event = events [i ]
314
+ if event .author == 'user' or _is_other_agent_reply (agent_name , event ):
315
+ turn_start_index = i
316
+ break
317
+
318
+ # Determine the starting index for history window
319
+ if history_window > 0 :
320
+ history_start_index = max (0 , turn_start_index - history_window )
321
+ else :
322
+ history_start_index = turn_start_index
323
+
324
+ return _get_contents (current_branch , events [history_start_index :], agent_name )
325
+
285
326
286
327
def _is_other_agent_reply (current_agent_name : str , event : Event ) -> bool :
287
328
"""Whether the event is a reply from another agent."""
0 commit comments