Skip to content

Commit 84f2f41

Browse files
hangfeicopybara-github
authored andcommitted
fix: Rollback compaction handling from _get_contents
The sorting may cause problems in complex cases so rolling it back. Will implement it with a non-sorting approach. PiperOrigin-RevId: 816420710
1 parent 8110e41 commit 84f2f41

File tree

1 file changed

+17
-78
lines changed

1 file changed

+17
-78
lines changed

src/google/adk/flows/llm_flows/contents.py

Lines changed: 17 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -236,40 +236,17 @@ def _contains_empty_content(event: Event) -> bool:
236236
def _get_contents(
237237
current_branch: Optional[str], events: list[Event], agent_name: str = ''
238238
) -> list[types.Content]:
239-
"""Retrieves and processes events into a list of Contents for the LLM request.
240-
241-
This function prepares the conversation history for the LLM by applying
242-
several transformations:
243-
1. **Initial Filtering**: Removes events that are empty, do not belong
244-
to the current invocation branch, or are related to authentication
245-
or request confirmation.
246-
2. **Compaction Handling**: Identifies the latest compaction event. If found,
247-
it replaces all events covered by the compaction range with the compacted
248-
summary content. Only events *after* the compaction's end timestamp
249-
are included in addition to the summary. If no compaction event exists,
250-
all filtered events are considered.
251-
3. **Transcription Aggregation**: Combines consecutive
252-
`input_transcription` and `output_transcription` events into single
253-
'user' and 'model' role `types.Content` events, respectively.
254-
4. **Multi-Agent Presentation**: Reformats messages from other agents
255-
(i.e., not the current `agent_name` and not 'user') to be presented
256-
as user-role context, prefixed with `[agent_name] said:`. Compactor
257-
events are included directly without reformatting.
258-
5. **Function Call/Response Rearrangement**: Ensures proper pairing of
259-
asynchronous function calls and responses within the event history.
260-
6. **Content Conversion**: Converts the final list of processed events
261-
into `types.Content` objects, removing any client-side function call IDs.
239+
"""Get the contents for the LLM request.
240+
241+
Applies filtering, rearrangement, and content processing to events.
262242
263243
Args:
264-
current_branch: The current invocation branch ID. Events outside this branch
265-
will be filtered out.
266-
events: A list of session events to process.
267-
agent_name: The name of the agent currently running. Used to distinguish
268-
between events from the current agent, other agents, and the user.
244+
current_branch: The current branch of the agent.
245+
events: Events to process.
246+
agent_name: The name of the agent.
269247
270248
Returns:
271-
A list of `types.Content` objects representing the conversation history
272-
to be sent to the LLM.
249+
A list of processed contents.
273250
"""
274251
accumulated_input_transcription = ''
275252
accumulated_output_transcription = ''
@@ -292,55 +269,18 @@ def _get_contents(
292269

293270
raw_filtered_events.append(event)
294271

295-
# Find the latest compaction event.
296-
latest_compaction_event = None
297-
for event in reversed(raw_filtered_events):
298-
if event.actions and event.actions.compaction:
299-
latest_compaction_event = event
300-
break
301-
302-
events_to_process = []
303-
if latest_compaction_event:
304-
compaction = latest_compaction_event.actions.compaction
305-
if (
306-
compaction.start_timestamp is not None
307-
and compaction.end_timestamp is not None
308-
):
309-
# Add the compacted event itself.
310-
new_event = Event(
311-
timestamp=compaction.end_timestamp,
312-
author='compactor',
313-
content=compaction.compacted_content,
314-
branch=latest_compaction_event.branch,
315-
invocation_id=latest_compaction_event.invocation_id,
316-
actions=latest_compaction_event.actions,
317-
)
318-
events_to_process.append(new_event)
319-
320-
# Add events from raw_filtered_events that are *after* the
321-
# latest compaction's end timestamp.
322-
for event in raw_filtered_events:
323-
if event.timestamp > compaction.end_timestamp:
324-
events_to_process.append(event)
325-
else:
326-
# No compaction events, process all raw filtered events.
327-
events_to_process = raw_filtered_events
328-
329-
# Sort by timestamp to ensure chronological order.
330-
events_to_process.sort(key=lambda x: x.timestamp)
331-
332272
filtered_events = []
333273
# aggregate transcription events
334-
for i in range(len(events_to_process)):
335-
event = events_to_process[i]
274+
for i in range(len(raw_filtered_events)):
275+
event = raw_filtered_events[i]
336276
if not event.content:
337277
# Convert transcription into normal event
338278
if event.input_transcription and event.input_transcription.text:
339279
accumulated_input_transcription += event.input_transcription.text
340280
if (
341-
i != len(events_to_process) - 1
342-
and events_to_process[i + 1].input_transcription
343-
and events_to_process[i + 1].input_transcription.text
281+
i != len(raw_filtered_events) - 1
282+
and raw_filtered_events[i + 1].input_transcription
283+
and raw_filtered_events[i + 1].input_transcription.text
344284
):
345285
continue
346286
event = event.model_copy(deep=True)
@@ -353,9 +293,9 @@ def _get_contents(
353293
elif event.output_transcription and event.output_transcription.text:
354294
accumulated_output_transcription += event.output_transcription.text
355295
if (
356-
i != len(events_to_process) - 1
357-
and events_to_process[i + 1].output_transcription
358-
and events_to_process[i + 1].output_transcription.text
296+
i != len(raw_filtered_events) - 1
297+
and raw_filtered_events[i + 1].output_transcription
298+
and raw_filtered_events[i + 1].output_transcription.text
359299
):
360300
continue
361301
event = event.model_copy(deep=True)
@@ -384,9 +324,8 @@ def _get_contents(
384324
contents = []
385325
for event in result_events:
386326
content = copy.deepcopy(event.content)
387-
if content:
388-
remove_client_function_call_id(content)
389-
contents.append(content)
327+
remove_client_function_call_id(content)
328+
contents.append(content)
390329
return contents
391330

392331

0 commit comments

Comments
 (0)