@@ -89,16 +89,10 @@ async def _process_request(
8989 # 2. The function call required authorization.
9090 # Ideally we'd have a way to interpret whether the response is a completion for the
9191 # task or requires follow-up, but I'm not going to bother just yet.
92- if auth_request_function_call := get_auth_request_function_call (
93- event
94- ):
92+ if auth_request_function_call := get_auth_request_function_call (event ):
9593 # Gather details, then suspend.
96- auth_details = self ._prepare_auth_request (
97- auth_request_function_call
98- )
99- logger .debug (
100- 'Yielding auth required response: %s' , auth_details .uri
101- )
94+ auth_details = self ._prepare_auth_request (auth_request_function_call )
95+ logger .debug ('Yielding auth required response: %s' , auth_details .uri )
10296 await task_updater .update_status (
10397 TaskState .auth_required ,
10498 message = new_agent_text_message (
@@ -127,9 +121,7 @@ async def _process_request(
127121
128122 if auth_details :
129123 # After auth is received, we can continue processing this request.
130- await self ._complete_auth_processing (
131- context , auth_details , task_updater
132- )
124+ await self ._complete_auth_processing (context , auth_details , task_updater )
133125
134126 def _prepare_auth_request (
135127 self , auth_request_function_call : types .FunctionCall
@@ -148,9 +140,7 @@ def _prepare_auth_request(
148140 oauth2_config = auth_config .exchanged_auth_credential .oauth2
149141 base_auth_uri = oauth2_config .auth_uri
150142 if not base_auth_uri :
151- raise ValueError (
152- f'Cannot get auth uri from auth config: { auth_config } '
153- )
143+ raise ValueError (f'Cannot get auth uri from auth config: { auth_config } ' )
154144 redirect_uri = f'{ self ._card .url } authenticate'
155145 oauth2_config .redirect_uri = redirect_uri
156146 state_token = oauth2_config .state
@@ -194,9 +184,7 @@ async def _complete_auth_processing(
194184 ),
195185 )
196186 del self ._awaiting_auth [auth_details .state ]
197- oauth2_config = (
198- auth_details .auth_config .exchanged_auth_credential .oauth2
199- )
187+ oauth2_config = auth_details .auth_config .exchanged_auth_credential .oauth2
200188 oauth2_config .auth_response_uri = auth_uri
201189 auth_content = types .UserContent (
202190 parts = [
@@ -210,20 +198,20 @@ async def _complete_auth_processing(
210198 ]
211199 )
212200 await self ._process_request (auth_content , context , task_updater )
213- # Extract the stored credential.
214- if context . call_context and context . call_context . user . is_authenticated :
215- await self ._store_user_auth (
216- context ,
217- auth_details .auth_config .auth_scheme ,
218- auth_details .auth_config .raw_auth_credential ,
219- )
201+ # Always hoist the session credential. The documented OAuth redirect
202+ # has no JWT, so call_context stays unauthenticated.
203+ await self ._store_user_auth (
204+ context ,
205+ auth_details .auth_config .auth_scheme ,
206+ auth_details .auth_config .raw_auth_credential ,
207+ )
220208
221209 async def execute (
222210 self ,
223211 context : RequestContext ,
224212 event_queue : EventQueue ,
225- ):
226- # Run the agent until either complete or the task is suspended.
213+ ) -> None :
214+ """ Run the agent until the task completes or is suspended."""
227215 updater = TaskUpdater (event_queue , context .task_id , context .context_id )
228216 # Immediately notify that the task is submitted.
229217 if not context .current_task :
@@ -238,11 +226,12 @@ async def execute(
238226 )
239227 logger .debug ('[Calendar] execute exiting' )
240228
241- async def cancel (self , context : RequestContext , event_queue : EventQueue ):
242- # Ideally: kill any ongoing tasks.
229+ async def cancel (self , context : RequestContext , event_queue : EventQueue ) -> None :
230+ """Cancel is not supported for the calendar agent."""
243231 raise ServerError (error = UnsupportedOperationError ())
244232
245- async def on_auth_callback (self , state : str , uri : str ):
233+ async def on_auth_callback (self , state : str , uri : str ) -> None :
234+ """Resume an in-flight OAuth callback."""
246235 self ._awaiting_auth [state ].set_result (uri )
247236
248237 async def _upsert_session (self , context : RequestContext ) -> Session :
@@ -262,9 +251,9 @@ async def _upsert_session(self, context: RequestContext) -> Session:
262251 return await self ._ensure_auth (session )
263252
264253 async def _ensure_auth (self , session : Session ) -> Session :
265- if (
266- stored_cred := self . _credentials . get ( session . user_id )
267- ) and not session . state . get ( stored_cred . key ) :
254+ if (stored_cred := self . _credentials . get ( session . user_id )) and not session . state . get (
255+ stored_cred . key
256+ ):
268257 event_action = EventActions (
269258 state_delta = {
270259 stored_cred .key : stored_cred .credential ,
@@ -301,10 +290,8 @@ async def _store_user_auth(
301290 )
302291 stored_credential = session .state .get (credential_key )
303292 if stored_credential :
304- self ._credentials [context .call_context .user .user_name ] = (
305- StoredCredential (
306- key = credential_key , credential = stored_credential
307- )
293+ self ._credentials [session .user_id ] = StoredCredential (
294+ key = credential_key , credential = stored_credential
308295 )
309296
310297
@@ -321,15 +308,11 @@ def convert_a2a_part_to_genai(part: Part) -> types.Part:
321308 if isinstance (part , FilePart ):
322309 if isinstance (part .file , FileWithUri ):
323310 return types .Part (
324- file_data = types .FileData (
325- file_uri = part .file .uri , mime_type = part .file .mime_type
326- )
311+ file_data = types .FileData (file_uri = part .file .uri , mime_type = part .file .mime_type )
327312 )
328313 if isinstance (part .file , FileWithBytes ):
329314 return types .Part (
330- inline_data = types .Blob (
331- data = part .file .bytes , mime_type = part .file .mime_type
332- )
315+ inline_data = types .Blob (data = part .file .bytes , mime_type = part .file .mime_type )
333316 )
334317 raise ValueError (f'Unsupported file type: { type (part .file )} ' )
335318 raise ValueError (f'Unsupported part type: { type (part )} ' )
@@ -390,7 +373,5 @@ def get_auth_config(
390373 if not auth_request_function_call .args or not (
391374 auth_config := auth_request_function_call .args .get ('authConfig' )
392375 ):
393- raise ValueError (
394- f'Cannot get auth config from function call: { auth_request_function_call } '
395- )
376+ raise ValueError (f'Cannot get auth config from function call: { auth_request_function_call } ' )
396377 return AuthConfig .model_validate (auth_config )
0 commit comments