Skip to content

Commit 7cb5ebe

Browse files
DeanChensjcopybara-github
authored andcommitted
test: Add test for finding matching function call when response is not last event
- Add unit test to verify that `find_matching_function_call_in_history` correctly finds the matching function call event even if the function response is not the immediate next event. Co-authored-by: Shangjie Chen <deanchen@google.com> PiperOrigin-RevId: 944560257
1 parent ab839d5 commit 7cb5ebe

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

tests/unittests/agents/test_invocation_context.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,3 +655,40 @@ def test_stamp_event_branch_context_does_not_overwrite_existing_scope(
655655
invocation_context.stamp_event_branch_context(fr_event)
656656
assert fr_event.branch == 'root@1'
657657
assert fr_event.isolation_scope == 'task_123'
658+
659+
def test_find_matching_function_call_when_response_is_not_last_event(
660+
self, test_invocation_context
661+
):
662+
"""Tests that matching function call is found even when response is not the last event in history."""
663+
fc = Part.from_function_call(name='some_tool', args={})
664+
fc.function_call.id = 'test_function_call_id'
665+
fc_event = Event(
666+
invocation_id='inv_1',
667+
author='agent',
668+
content=testing_utils.ModelContent([fc]),
669+
)
670+
fr = Part.from_function_response(
671+
name='some_tool', response={'result': 'ok'}
672+
)
673+
fr.function_response.id = 'test_function_call_id'
674+
fr_event = Event(
675+
invocation_id='inv_1',
676+
author='agent',
677+
content=Content(role='user', parts=[fr]),
678+
)
679+
# Add a subsequent event to the history so that fr_event is NOT the last one
680+
subsequent_event = Event(
681+
invocation_id='inv_1',
682+
author='user',
683+
content=Content(role='user', parts=[Part(text='next user message')]),
684+
)
685+
invocation_context = test_invocation_context(
686+
[fc_event, fr_event, subsequent_event]
687+
)
688+
689+
matching_fc_event = invocation_context._find_matching_function_call(
690+
fr_event
691+
)
692+
assert testing_utils.simplify_content(
693+
matching_fc_event.content
694+
) == testing_utils.simplify_content(fc_event.content)

0 commit comments

Comments
 (0)