forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_manager.py
More file actions
361 lines (319 loc) · 11.7 KB
/
plugin_manager.py
File metadata and controls
361 lines (319 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import asyncio
import logging
import sys
from typing import Any
from typing import List
from typing import Literal
from typing import Optional
from typing import TYPE_CHECKING
from google.genai import types
from .base_plugin import BasePlugin
if TYPE_CHECKING:
from ..agents.base_agent import BaseAgent
from ..agents.callback_context import CallbackContext
from ..agents.invocation_context import InvocationContext
from ..events.event import Event
from ..models.llm_request import LlmRequest
from ..models.llm_response import LlmResponse
from ..tools.base_tool import BaseTool
from ..tools.tool_context import ToolContext
# A type alias for the names of the available plugin callbacks.
# This helps with static analysis and prevents typos when calling run_callbacks.
PluginCallbackName = Literal[
"on_user_message_callback",
"before_run_callback",
"after_run_callback",
"on_event_callback",
"before_agent_callback",
"after_agent_callback",
"before_tool_callback",
"after_tool_callback",
"before_model_callback",
"after_model_callback",
"on_tool_error_callback",
"on_model_error_callback",
"on_state_change_callback",
]
logger = logging.getLogger("google_adk." + __name__)
class PluginManager:
"""Manages the registration and execution of plugins.
The PluginManager is an internal class that orchestrates the invocation of
plugin callbacks at key points in the SDK's execution lifecycle. It maintains
a list of registered plugins and ensures they are called in the order they
were registered.
The core execution logic implements an "early exit" strategy: if any plugin
callback returns a non-`None` value, the execution of subsequent plugins for
that specific event is halted, and the returned value is propagated up the
call stack. This allows plugins to short-circuit operations like agent runs,
tool calls, or model requests.
"""
def __init__(
self,
plugins: Optional[List[BasePlugin]] = None,
close_timeout: float = 5.0,
):
"""Initializes the plugin service.
Args:
plugins: An optional list of plugins to register upon initialization.
close_timeout: The timeout in seconds for each plugin's close method.
"""
self.plugins: List[BasePlugin] = []
self._close_timeout = close_timeout
if plugins:
for plugin in plugins:
self.register_plugin(plugin)
def register_plugin(self, plugin: BasePlugin) -> None:
"""Registers a new plugin.
Args:
plugin: The plugin instance to register.
Raises:
ValueError: If a plugin with the same name is already registered.
"""
if any(p.name == plugin.name for p in self.plugins):
raise ValueError(f"Plugin with name '{plugin.name}' already registered.")
self.plugins.append(plugin)
logger.info("Plugin '%s' registered.", plugin.name)
def get_plugin(self, plugin_name: str) -> Optional[BasePlugin]:
"""Retrieves a registered plugin by its name.
Args:
plugin_name: The name of the plugin to retrieve.
Returns:
The plugin instance if found; otherwise, `None`.
"""
return next((p for p in self.plugins if p.name == plugin_name), None)
async def run_on_user_message_callback(
self,
*,
user_message: types.Content,
invocation_context: InvocationContext,
) -> Optional[types.Content]:
"""Runs the `on_user_message_callback` for all plugins."""
return await self._run_callbacks(
"on_user_message_callback",
user_message=user_message,
invocation_context=invocation_context,
)
async def run_before_run_callback(
self, *, invocation_context: InvocationContext
) -> Optional[types.Content]:
"""Runs the `before_run_callback` for all plugins."""
return await self._run_callbacks(
"before_run_callback", invocation_context=invocation_context
)
async def run_after_run_callback(
self, *, invocation_context: InvocationContext
) -> Optional[None]:
"""Runs the `after_run_callback` for all plugins."""
return await self._run_callbacks(
"after_run_callback", invocation_context=invocation_context
)
async def run_on_event_callback(
self, *, invocation_context: InvocationContext, event: Event
) -> Optional[Event]:
"""Runs the `on_event_callback` for all plugins."""
return await self._run_callbacks(
"on_event_callback",
invocation_context=invocation_context,
event=event,
)
async def run_before_agent_callback(
self, *, agent: BaseAgent, callback_context: CallbackContext
) -> Optional[types.Content]:
"""Runs the `before_agent_callback` for all plugins."""
return await self._run_callbacks(
"before_agent_callback",
agent=agent,
callback_context=callback_context,
)
async def run_after_agent_callback(
self, *, agent: BaseAgent, callback_context: CallbackContext
) -> Optional[types.Content]:
"""Runs the `after_agent_callback` for all plugins."""
return await self._run_callbacks(
"after_agent_callback",
agent=agent,
callback_context=callback_context,
)
async def run_before_tool_callback(
self,
*,
tool: BaseTool,
tool_args: dict[str, Any],
tool_context: ToolContext,
) -> Optional[dict]:
"""Runs the `before_tool_callback` for all plugins."""
return await self._run_callbacks(
"before_tool_callback",
tool=tool,
tool_args=tool_args,
tool_context=tool_context,
)
async def run_after_tool_callback(
self,
*,
tool: BaseTool,
tool_args: dict[str, Any],
tool_context: ToolContext,
result: dict,
) -> Optional[dict]:
"""Runs the `after_tool_callback` for all plugins."""
return await self._run_callbacks(
"after_tool_callback",
tool=tool,
tool_args=tool_args,
tool_context=tool_context,
result=result,
)
async def run_on_model_error_callback(
self,
*,
callback_context: CallbackContext,
llm_request: LlmRequest,
error: Exception,
) -> Optional[LlmResponse]:
"""Runs the `on_model_error_callback` for all plugins."""
return await self._run_callbacks(
"on_model_error_callback",
callback_context=callback_context,
llm_request=llm_request,
error=error,
)
async def run_before_model_callback(
self, *, callback_context: CallbackContext, llm_request: LlmRequest
) -> Optional[LlmResponse]:
"""Runs the `before_model_callback` for all plugins."""
return await self._run_callbacks(
"before_model_callback",
callback_context=callback_context,
llm_request=llm_request,
)
async def run_after_model_callback(
self, *, callback_context: CallbackContext, llm_response: LlmResponse
) -> Optional[LlmResponse]:
"""Runs the `after_model_callback` for all plugins."""
return await self._run_callbacks(
"after_model_callback",
callback_context=callback_context,
llm_response=llm_response,
)
async def run_on_tool_error_callback(
self,
*,
tool: BaseTool,
tool_args: dict[str, Any],
tool_context: ToolContext,
error: Exception,
) -> Optional[dict]:
"""Runs the `on_tool_error_callback` for all plugins."""
return await self._run_callbacks(
"on_tool_error_callback",
tool=tool,
tool_args=tool_args,
tool_context=tool_context,
error=error,
)
async def run_on_state_change_callback(
self,
*,
callback_context: CallbackContext,
state_delta: dict[str, Any],
) -> Optional[Any]:
"""Runs the `on_state_change_callback` for all plugins."""
return await self._run_callbacks(
"on_state_change_callback",
callback_context=callback_context,
state_delta=state_delta,
)
async def _run_callbacks(
self, callback_name: PluginCallbackName, **kwargs: Any
) -> Optional[Any]:
"""Executes a specific callback for all registered plugins.
This private method iterates through the plugins and calls the specified
callback method on each one, passing the provided keyword arguments.
The execution stops as soon as a plugin's callback returns a non-`None`
value. This "early exit" value is then returned by this method. If all
plugins are executed and all return `None`, this method also returns `None`.
Args:
callback_name: The name of the callback method to execute.
**kwargs: Keyword arguments to be passed to the callback method.
Returns:
The first non-`None` value returned by a plugin callback, or `None` if
all callbacks return `None`.
Raises:
RuntimeError: If a plugin encounters an unhandled exception during
execution. The original exception is chained.
"""
for plugin in self.plugins:
# Each plugin might not implement all callbacks. The base class provides
# default `pass` implementations, so `getattr` will always succeed.
callback_method = getattr(plugin, callback_name)
try:
result = await callback_method(**kwargs)
if result is not None:
# Early exit: A plugin has returned a value. We stop
# processing further plugins and return this value immediately.
logger.debug(
"Plugin '%s' returned a value for callback '%s', exiting early.",
plugin.name,
callback_name,
)
return result
except Exception as e:
error_message = (
f"Error in plugin '{plugin.name}' during '{callback_name}'"
f" callback: {e}"
)
logger.error(error_message, exc_info=True)
raise RuntimeError(error_message) from e
return None
async def close(self) -> None:
"""Calls the close method on all registered plugins concurrently.
Raises:
RuntimeError: If one or more plugins failed to close, containing
details of all failures.
"""
exceptions = {}
# We iterate sequentially to avoid creating new tasks which can cause issues
# with some libraries (like anyio/mcp) that rely on task-local context.
for plugin in self.plugins:
try:
if sys.version_info >= (3, 11):
async with asyncio.timeout(self._close_timeout):
await plugin.close()
else:
# For Python < 3.11, we use wait_for which creates a new task.
# This might still cause issues with task-local contexts, but
# asyncio.timeout is not available.
await asyncio.wait_for(plugin.close(), timeout=self._close_timeout)
except Exception as e:
exceptions[plugin.name] = e
if isinstance(e, (asyncio.TimeoutError, asyncio.CancelledError)):
logger.warning(
"Timeout/Cancelled while closing plugin: %s", plugin.name
)
else:
logger.error(
"Error during close of plugin %s: %s",
plugin.name,
e,
exc_info=e,
)
if exceptions:
error_summary = ", ".join(
f"'{name}': {type(exc).__name__}" for name, exc in exceptions.items()
)
raise RuntimeError(f"Failed to close plugins: {error_summary}")