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: 818822267
This commit is contained in:
Google Team Member
2025-10-13 14:05:44 -07:00
committed by Copybara-Service
parent 2158b3c915
commit df05ed6b3b
10 changed files with 110 additions and 240 deletions
+16 -8
View File
@@ -103,15 +103,19 @@ async def test_base_plugin_default_callbacks_return_none():
assert (
await plugin.on_user_message_callback(
user_message=mock_user_message,
callback_context=mock_context,
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.before_run_callback(invocation_context=mock_context) is None
)
assert (
await plugin.after_run_callback(invocation_context=mock_context) is None
)
assert (
await plugin.on_event_callback(
callback_context=mock_context, event=mock_context
invocation_context=mock_context, event=mock_context
)
is None
)
@@ -196,21 +200,25 @@ async def test_base_plugin_all_callbacks_can_be_overridden():
assert (
await plugin.on_user_message_callback(
user_message=mock_user_message,
callback_context=mock_callback_context,
invocation_context=mock_invocation_context,
)
== "overridden_on_user_message"
)
assert (
await plugin.before_run_callback(callback_context=mock_callback_context)
await plugin.before_run_callback(
invocation_context=mock_invocation_context
)
== "overridden_before_run"
)
assert (
await plugin.after_run_callback(callback_context=mock_callback_context)
await plugin.after_run_callback(
invocation_context=mock_invocation_context
)
== "overridden_after_run"
)
assert (
await plugin.on_event_callback(
callback_context=mock_callback_context, event=mock_event
invocation_context=mock_invocation_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.user_id}."
return f"You are assistant for user {readonly_context.session.user_id}."
plugin = GlobalInstructionPlugin(global_instruction=build_global_instruction)
@@ -83,8 +83,7 @@ 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.user_id = "alice"
mock_callback_context.invocation_context = mock_invocation_context
llm_request = LlmRequest(
model="gemini-1.5-flash",
@@ -120,7 +119,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",
@@ -157,7 +156,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",
@@ -192,7 +191,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",