mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
feat: migrate invocation_context to callback_context
Update plugin manager and built-in plugins to prioritize CallbackContext. Keep InvocationContext access for legacy plugins with adapter. Change callback docs/tests to cover the new context. PiperOrigin-RevId: 818798087
This commit is contained in:
committed by
Copybara-Service
parent
fa84bcb575
commit
e2072af69f
@@ -103,19 +103,15 @@ async def test_base_plugin_default_callbacks_return_none():
|
||||
assert (
|
||||
await plugin.on_user_message_callback(
|
||||
user_message=mock_user_message,
|
||||
invocation_context=mock_context,
|
||||
callback_context=mock_context,
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert (
|
||||
await plugin.before_run_callback(invocation_context=mock_context) is None
|
||||
)
|
||||
assert (
|
||||
await plugin.after_run_callback(invocation_context=mock_context) is None
|
||||
)
|
||||
assert await plugin.before_run_callback(callback_context=mock_context) is None
|
||||
assert await plugin.after_run_callback(callback_context=mock_context) is None
|
||||
assert (
|
||||
await plugin.on_event_callback(
|
||||
invocation_context=mock_context, event=mock_context
|
||||
callback_context=mock_context, event=mock_context
|
||||
)
|
||||
is None
|
||||
)
|
||||
@@ -200,25 +196,21 @@ async def test_base_plugin_all_callbacks_can_be_overridden():
|
||||
assert (
|
||||
await plugin.on_user_message_callback(
|
||||
user_message=mock_user_message,
|
||||
invocation_context=mock_invocation_context,
|
||||
callback_context=mock_callback_context,
|
||||
)
|
||||
== "overridden_on_user_message"
|
||||
)
|
||||
assert (
|
||||
await plugin.before_run_callback(
|
||||
invocation_context=mock_invocation_context
|
||||
)
|
||||
await plugin.before_run_callback(callback_context=mock_callback_context)
|
||||
== "overridden_before_run"
|
||||
)
|
||||
assert (
|
||||
await plugin.after_run_callback(
|
||||
invocation_context=mock_invocation_context
|
||||
)
|
||||
await plugin.after_run_callback(callback_context=mock_callback_context)
|
||||
== "overridden_after_run"
|
||||
)
|
||||
assert (
|
||||
await plugin.on_event_callback(
|
||||
invocation_context=mock_invocation_context, event=mock_event
|
||||
callback_context=mock_callback_context, event=mock_event
|
||||
)
|
||||
== "overridden_on_event"
|
||||
)
|
||||
|
||||
@@ -43,7 +43,7 @@ async def test_global_instruction_plugin_with_string():
|
||||
mock_invocation_context.session = mock_session
|
||||
|
||||
mock_callback_context = Mock(spec=CallbackContext)
|
||||
mock_callback_context.invocation_context = mock_invocation_context
|
||||
mock_callback_context._invocation_context = mock_invocation_context
|
||||
|
||||
llm_request = LlmRequest(
|
||||
model="gemini-1.5-flash",
|
||||
@@ -70,7 +70,7 @@ async def test_global_instruction_plugin_with_instruction_provider():
|
||||
"""Test GlobalInstructionPlugin with an InstructionProvider function."""
|
||||
|
||||
async def build_global_instruction(readonly_context: ReadonlyContext) -> str:
|
||||
return f"You are assistant for user {readonly_context.session.user_id}."
|
||||
return f"You are assistant for user {readonly_context.user_id}."
|
||||
|
||||
plugin = GlobalInstructionPlugin(global_instruction=build_global_instruction)
|
||||
|
||||
@@ -83,7 +83,8 @@ async def test_global_instruction_plugin_with_instruction_provider():
|
||||
mock_invocation_context.session = mock_session
|
||||
|
||||
mock_callback_context = Mock(spec=CallbackContext)
|
||||
mock_callback_context.invocation_context = mock_invocation_context
|
||||
mock_callback_context._invocation_context = mock_invocation_context
|
||||
mock_callback_context.user_id = "alice"
|
||||
|
||||
llm_request = LlmRequest(
|
||||
model="gemini-1.5-flash",
|
||||
@@ -119,7 +120,7 @@ async def test_global_instruction_plugin_empty_instruction():
|
||||
mock_invocation_context.session = mock_session
|
||||
|
||||
mock_callback_context = Mock(spec=CallbackContext)
|
||||
mock_callback_context.invocation_context = mock_invocation_context
|
||||
mock_callback_context._invocation_context = mock_invocation_context
|
||||
|
||||
llm_request = LlmRequest(
|
||||
model="gemini-1.5-flash",
|
||||
@@ -156,7 +157,7 @@ async def test_global_instruction_plugin_leads_existing():
|
||||
mock_invocation_context.session = mock_session
|
||||
|
||||
mock_callback_context = Mock(spec=CallbackContext)
|
||||
mock_callback_context.invocation_context = mock_invocation_context
|
||||
mock_callback_context._invocation_context = mock_invocation_context
|
||||
|
||||
llm_request = LlmRequest(
|
||||
model="gemini-1.5-flash",
|
||||
@@ -191,7 +192,7 @@ async def test_global_instruction_plugin_prepends_to_list():
|
||||
mock_invocation_context.session = mock_session
|
||||
|
||||
mock_callback_context = Mock(spec=CallbackContext)
|
||||
mock_callback_context.invocation_context = mock_invocation_context
|
||||
mock_callback_context._invocation_context = mock_invocation_context
|
||||
|
||||
llm_request = LlmRequest(
|
||||
model="gemini-1.5-flash",
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
from typing import Optional
|
||||
|
||||
from google.adk.agents.base_agent import BaseAgent
|
||||
from google.adk.agents.callback_context import CallbackContext
|
||||
from google.adk.agents.context_cache_config import ContextCacheConfig
|
||||
from google.adk.agents.invocation_context import InvocationContext
|
||||
from google.adk.agents.llm_agent import LlmAgent
|
||||
from google.adk.apps.app import App
|
||||
from google.adk.apps.app import ResumabilityConfig
|
||||
@@ -98,7 +98,7 @@ class MockPlugin(BasePlugin):
|
||||
async def on_user_message_callback(
|
||||
self,
|
||||
*,
|
||||
invocation_context: InvocationContext,
|
||||
callback_context: CallbackContext,
|
||||
user_message: types.Content,
|
||||
) -> Optional[types.Content]:
|
||||
if not self.enable_user_message_callback:
|
||||
@@ -109,7 +109,7 @@ class MockPlugin(BasePlugin):
|
||||
)
|
||||
|
||||
async def on_event_callback(
|
||||
self, *, invocation_context: InvocationContext, event: Event
|
||||
self, *, callback_context: CallbackContext, event: Event
|
||||
) -> Optional[Event]:
|
||||
if not self.enable_event_callback:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user