Skip to content

Commit 3b922a2

Browse files
seanzhougooglecopybara-github
authored andcommitted
fix: Only process the auth responses in the last event with content (if applicable i.e. it's authored by user)
fixes : #1944 PiperOrigin-RevId: 800560805
1 parent 7975e8e commit 3b922a2

File tree

2 files changed

+567
-19
lines changed

2 files changed

+567
-19
lines changed

src/google/adk/auth/auth_preprocessor.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,33 @@ async def run_async(
5151
return
5252

5353
request_euc_function_call_ids = set()
54-
for k in range(len(events) - 1, -1, -1):
55-
event = events[k]
56-
# look for first event authored by user
57-
if not event.author or event.author != 'user':
58-
continue
59-
responses = event.get_function_responses()
60-
if not responses:
61-
return
54+
# find the last event with non-None content
55+
last_event_with_content = None
56+
for i in range(len(events) - 1, -1, -1):
57+
event = events[i]
58+
if event.content is not None:
59+
last_event_with_content = event
60+
break
6261

63-
for function_call_response in responses:
64-
if function_call_response.name != REQUEST_EUC_FUNCTION_CALL_NAME:
65-
continue
66-
# found the function call response for the system long running request euc
67-
# function call
68-
request_euc_function_call_ids.add(function_call_response.id)
69-
auth_config = AuthConfig.model_validate(function_call_response.response)
70-
await AuthHandler(
71-
auth_config=auth_config
72-
).parse_and_store_auth_response(state=invocation_context.session.state)
73-
break
62+
# check if the last event with content is authored by user
63+
if not last_event_with_content or last_event_with_content.author != 'user':
64+
return
65+
66+
responses = last_event_with_content.get_function_responses()
67+
if not responses:
68+
return
69+
70+
# look for auth response
71+
for function_call_response in responses:
72+
if function_call_response.name != REQUEST_EUC_FUNCTION_CALL_NAME:
73+
continue
74+
# found the function call response for the system long running request euc
75+
# function call
76+
request_euc_function_call_ids.add(function_call_response.id)
77+
auth_config = AuthConfig.model_validate(function_call_response.response)
78+
await AuthHandler(auth_config=auth_config).parse_and_store_auth_response(
79+
state=invocation_context.session.state
80+
)
7481

7582
if not request_euc_function_call_ids:
7683
return

0 commit comments

Comments
 (0)