|
36 | 36 | from ...agents.live_request_queue import LiveRequestQueue |
37 | 37 | from ...agents.readonly_context import ReadonlyContext |
38 | 38 | from ...agents.run_config import StreamingMode |
39 | | -from ...auth.auth_handler import AuthHandler |
40 | | -from ...auth.auth_tool import AuthConfig |
41 | 39 | from ...auth.credential_manager import CredentialManager |
42 | 40 | from ...events.event import Event |
43 | 41 | from ...models.base_llm_connection import BaseLlmConnection |
|
51 | 49 | from ...tools.tool_context import ToolContext |
52 | 50 | from ...utils.context_utils import Aclosing |
53 | 51 | from .audio_cache_manager import AudioCacheManager |
54 | | -from .functions import build_auth_request_event |
55 | | - |
56 | | -# Prefix used by toolset auth credential IDs |
57 | | -TOOLSET_AUTH_CREDENTIAL_ID_PREFIX = '_adk_toolset_auth_' |
58 | 52 |
|
59 | 53 | if TYPE_CHECKING: |
60 | 54 | from ...agents.llm_agent import LlmAgent |
@@ -115,24 +109,24 @@ def _finalize_model_response_event( |
115 | 109 | async def _resolve_toolset_auth( |
116 | 110 | invocation_context: InvocationContext, |
117 | 111 | agent: LlmAgent, |
118 | | -) -> AsyncGenerator[Event, None]: |
| 112 | +) -> None: |
119 | 113 | """Resolves authentication for toolsets before tool listing. |
120 | 114 |
|
121 | 115 | For each toolset with auth configured via get_auth_config(): |
122 | 116 | - If credential is available, populate auth_config.exchanged_auth_credential |
123 | | - - If credential is not available, yield auth request event and interrupt |
| 117 | + - If credential is not available, log and continue — auth will be handled |
| 118 | + on demand by ToolAuthHandler when a tool is actually invoked. |
| 119 | +
|
| 120 | + This avoids triggering OAuth redirects on every agent invocation, |
| 121 | + including messages that don't require any tool calls. |
124 | 122 |
|
125 | 123 | Args: |
126 | 124 | invocation_context: The invocation context. |
127 | 125 | agent: The LLM agent. |
128 | | -
|
129 | | - Yields: |
130 | | - Auth request events if any toolset needs authentication. |
131 | 126 | """ |
132 | 127 | if not agent.tools: |
133 | 128 | return |
134 | 129 |
|
135 | | - pending_auth_requests: dict[str, AuthConfig] = {} |
136 | 130 | callback_context = CallbackContext(invocation_context) |
137 | 131 |
|
138 | 132 | for tool_union in agent.tools: |
@@ -161,30 +155,11 @@ async def _resolve_toolset_auth( |
161 | 155 | # Populate in-place for toolset to use in get_tools() |
162 | 156 | auth_config.exchanged_auth_credential = credential |
163 | 157 | else: |
164 | | - # Need auth - will interrupt |
165 | | - toolset_id = ( |
166 | | - f'{TOOLSET_AUTH_CREDENTIAL_ID_PREFIX}{type(tool_union).__name__}' |
| 158 | + logger.debug( |
| 159 | + 'No credential found for toolset %s; deferring auth to tool' |
| 160 | + ' invocation.', |
| 161 | + type(tool_union).__name__, |
167 | 162 | ) |
168 | | - pending_auth_requests[toolset_id] = auth_config |
169 | | - |
170 | | - if not pending_auth_requests: |
171 | | - return |
172 | | - |
173 | | - # Build auth requests dict with generated auth requests |
174 | | - auth_requests = { |
175 | | - credential_id: AuthHandler(auth_config).generate_auth_request() |
176 | | - for credential_id, auth_config in pending_auth_requests.items() |
177 | | - } |
178 | | - |
179 | | - # Yield event with auth requests using the shared helper |
180 | | - yield build_auth_request_event( |
181 | | - invocation_context, |
182 | | - auth_requests, |
183 | | - author=agent.name, |
184 | | - ) |
185 | | - |
186 | | - # Interrupt invocation |
187 | | - invocation_context.end_invocation = True |
188 | 163 |
|
189 | 164 |
|
190 | 165 | async def _handle_before_model_callback( |
@@ -916,14 +891,7 @@ async def _preprocess_async( |
916 | 891 |
|
917 | 892 | # Resolve toolset authentication before tool listing. |
918 | 893 | # This ensures credentials are ready before get_tools() is called. |
919 | | - async with Aclosing( |
920 | | - self._resolve_toolset_auth(invocation_context, agent) |
921 | | - ) as agen: |
922 | | - async for event in agen: |
923 | | - yield event |
924 | | - |
925 | | - if invocation_context.end_invocation: |
926 | | - return |
| 894 | + await _resolve_toolset_auth(invocation_context, agent) |
927 | 895 |
|
928 | 896 | # Run processors for tools. |
929 | 897 | await _process_agent_tools(invocation_context, llm_request) |
@@ -1273,17 +1241,6 @@ def _finalize_model_response_event( |
1273 | 1241 | llm_request, llm_response, model_response_event |
1274 | 1242 | ) |
1275 | 1243 |
|
1276 | | - async def _resolve_toolset_auth( |
1277 | | - self, |
1278 | | - invocation_context: InvocationContext, |
1279 | | - agent: LlmAgent, |
1280 | | - ) -> AsyncGenerator[Event, None]: |
1281 | | - async with Aclosing( |
1282 | | - _resolve_toolset_auth(invocation_context, agent) |
1283 | | - ) as agen: |
1284 | | - async for event in agen: |
1285 | | - yield event |
1286 | | - |
1287 | 1244 | async def _handle_before_model_callback( |
1288 | 1245 | self, |
1289 | 1246 | invocation_context: InvocationContext, |
|
0 commit comments