mirror of
https://github.com/google/adk-docs.git
synced 2026-09-14 16:16:59 +08:00
87019788e0
* callback tests fix * ADK Dev Skills hearder link fix * Update README.md
180 lines
6.2 KiB
Python
180 lines
6.2 KiB
Python
#!/usr/bin/env python3
|
|
# Copyright 2025 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.
|
|
|
|
"""Integration test for callback snippets - actually runs them against ADK.
|
|
|
|
This test:
|
|
1. Creates a minimal agent with each callback type
|
|
2. Actually runs the agent to verify callbacks work
|
|
3. Confirms the callback signatures are accepted by ADK runtime
|
|
|
|
Usage:
|
|
python test_callback_snippets.py
|
|
"""
|
|
|
|
import sys
|
|
import asyncio
|
|
from typing import Optional, Dict, Any
|
|
|
|
try:
|
|
from google.adk.agents import Agent
|
|
from google.adk.agents.callback_context import CallbackContext
|
|
from google.adk.models import LlmResponse, LlmRequest
|
|
from google.adk.tools.tool_context import ToolContext
|
|
from google.adk.tools.base_tool import BaseTool
|
|
from google.adk.runners import Runner
|
|
from google.adk.sessions import InMemorySessionService
|
|
from google.genai import types as genai_types
|
|
ADK_AVAILABLE = True
|
|
except ImportError as e:
|
|
ADK_AVAILABLE = False
|
|
print(f"[SKIP] google-adk not installed: {e}")
|
|
|
|
|
|
# Test callback implementations with CORRECT signatures
|
|
def before_agent_callback_test(callback_context: CallbackContext) -> None:
|
|
"""Test before_agent_callback with correct signature."""
|
|
callback_context.state["before_agent_called"] = True
|
|
|
|
|
|
def after_agent_callback_test(callback_context: CallbackContext) -> Optional[genai_types.Content]:
|
|
"""Test after_agent_callback with correct signature."""
|
|
callback_context.state["after_agent_called"] = True
|
|
return None
|
|
|
|
|
|
def before_model_callback_test(
|
|
callback_context: CallbackContext, llm_request: LlmRequest
|
|
) -> Optional[LlmResponse]:
|
|
"""Test before_model_callback with correct signature."""
|
|
callback_context.state["before_model_called"] = True
|
|
# Return None to proceed with the request
|
|
return None
|
|
|
|
|
|
def after_model_callback_test(
|
|
callback_context: CallbackContext, llm_response: LlmResponse
|
|
) -> Optional[LlmResponse]:
|
|
"""Test after_model_callback with correct signature."""
|
|
callback_context.state["after_model_called"] = True
|
|
return None
|
|
|
|
|
|
def before_tool_callback_test(
|
|
tool: BaseTool, args: Dict[str, Any], tool_context: ToolContext
|
|
) -> Optional[Dict]:
|
|
"""Test before_tool_callback with correct signature."""
|
|
tool_context.state["before_tool_called"] = True
|
|
tool_context.state["tool_name"] = tool.name
|
|
return None
|
|
|
|
|
|
def after_tool_callback_test(
|
|
tool: BaseTool, args: Dict[str, Any], tool_context: ToolContext, tool_response: Dict
|
|
) -> Optional[Dict]:
|
|
"""Test after_tool_callback with correct signature."""
|
|
tool_context.state["after_tool_called"] = True
|
|
tool_context.state["tool_name"] = tool.name
|
|
return None
|
|
|
|
|
|
def simple_tool(query: str) -> dict:
|
|
"""A simple test tool."""
|
|
return {"result": f"Processed: {query}"}
|
|
|
|
|
|
async def test_callbacks():
|
|
"""Run an actual agent with all callbacks to verify signatures work."""
|
|
if not ADK_AVAILABLE:
|
|
return False
|
|
|
|
try:
|
|
# Create agent with all callbacks
|
|
agent = Agent(
|
|
name="test_agent",
|
|
model="gemini-2.0-flash-exp",
|
|
instruction="You are a test agent. When asked to test, use the simple_tool.",
|
|
tools=[simple_tool],
|
|
before_agent_callback=before_agent_callback_test,
|
|
after_agent_callback=after_agent_callback_test,
|
|
before_model_callback=before_model_callback_test,
|
|
after_model_callback=after_model_callback_test,
|
|
before_tool_callback=before_tool_callback_test,
|
|
after_tool_callback=after_tool_callback_test,
|
|
)
|
|
|
|
# Set up session
|
|
session_service = InMemorySessionService()
|
|
session = await session_service.create_session(
|
|
app_name="test_app", user_id="test_user", session_id="test_session"
|
|
)
|
|
|
|
# Create runner
|
|
runner = Runner(agent=agent, app_name="test_app", session_service=session_service)
|
|
|
|
# Run agent with a simple query
|
|
user_message = genai_types.Content(
|
|
role="user", parts=[genai_types.Part.from_text(text="Please test the simple_tool with query 'hello'")]
|
|
)
|
|
|
|
# Execute
|
|
final_response = None
|
|
async for event in runner.run_async(
|
|
user_id="test_user", session_id="test_session", new_message=user_message
|
|
):
|
|
if event.is_final_response():
|
|
if event.content and event.content.parts:
|
|
final_response = event.content.parts[0].text
|
|
break
|
|
|
|
# Verify callbacks were called by checking state
|
|
# Note: We can't directly check state here without modifying the test,
|
|
# but if the agent runs without signature errors, the test passes
|
|
print("[PASS] All callbacks executed without signature errors")
|
|
if final_response:
|
|
print(f"[INFO] Agent responded: {final_response[:100]}...")
|
|
return True
|
|
|
|
except TypeError as e:
|
|
# This would catch signature mismatches
|
|
print(f"[FAIL] Callback signature error: {e}")
|
|
return False
|
|
except Exception as e:
|
|
# Check if it's an API key issue (expected in CI)
|
|
if "API_KEY" in str(e) or "credentials" in str(e).lower():
|
|
print(f"[SKIP] API credentials not available (expected in CI): {e}")
|
|
return True
|
|
print(f"[FAIL] Unexpected error: {e}")
|
|
return False
|
|
|
|
|
|
def main():
|
|
if not ADK_AVAILABLE:
|
|
print("[SKIP] Integration test skipped (google-adk not installed)")
|
|
return 0
|
|
|
|
print("[INFO] Testing callback signatures with actual ADK runtime...")
|
|
|
|
try:
|
|
success = asyncio.run(test_callbacks())
|
|
return 0 if success else 1
|
|
except Exception as e:
|
|
print(f"[FAIL] Test execution failed: {e}")
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|