|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +import click |
| 6 | +import uvicorn |
| 7 | + |
| 8 | +from adk_agent_executor import ADKAgentExecutor |
| 9 | +from adk_agent import create_agent |
| 10 | +from dotenv import load_dotenv |
| 11 | + |
| 12 | +from a2a.server.tasks import InMemoryTaskStore |
| 13 | +from a2a.server.apps import A2AStarletteApplication |
| 14 | +from a2a.server.request_handlers import DefaultRequestHandler |
| 15 | +from a2a.types import ( |
| 16 | + AgentAuthentication, |
| 17 | + AgentCapabilities, |
| 18 | + AgentCard, |
| 19 | + AgentSkill, |
| 20 | +) |
| 21 | +from google.adk.artifacts import InMemoryArtifactService |
| 22 | +from google.adk.memory.in_memory_memory_service import InMemoryMemoryService |
| 23 | +from google.adk.runners import Runner |
| 24 | +from google.adk.sessions import InMemorySessionService |
| 25 | +from starlette.applications import Starlette |
| 26 | +from starlette.routing import Route |
| 27 | +from starlette.requests import Request |
| 28 | +from starlette.responses import PlainTextResponse |
| 29 | + |
| 30 | +load_dotenv() |
| 31 | + |
| 32 | +logging.basicConfig() |
| 33 | + |
| 34 | + |
| 35 | +@click.command() |
| 36 | +@click.option('--host', 'host', default='localhost') |
| 37 | +@click.option('--port', 'port', default=10007) |
| 38 | +def main(host: str, port: int): |
| 39 | + # Verify an API key is set. Not required if using Vertex AI APIs, since those can use gcloud credentials. |
| 40 | + if not os.getenv('GOOGLE_GENAI_USE_VERTEXAI') == 'TRUE': |
| 41 | + if not os.getenv('GOOGLE_API_KEY'): |
| 42 | + raise Exception( |
| 43 | + 'GOOGLE_API_KEY environment variable not set and GOOGLE_GENAI_USE_VERTEXAI is not TRUE.' |
| 44 | + ) |
| 45 | + |
| 46 | + skill = AgentSkill( |
| 47 | + id='check_availability', |
| 48 | + name='Check Availability', |
| 49 | + description="Checks a user's availability for a time using their Google Calendar", |
| 50 | + tags=['calendar'], |
| 51 | + examples=['Am I free from 10am to 11am tomorrow?'], |
| 52 | + ) |
| 53 | + |
| 54 | + agent_card = AgentCard( |
| 55 | + name='Calendar Agent', |
| 56 | + description="An agent that can manage a user's calendar", |
| 57 | + url=f'http://{host}:{port}/', |
| 58 | + version='1.0.0', |
| 59 | + defaultInputModes=['text'], |
| 60 | + defaultOutputModes=['text'], |
| 61 | + capabilities=AgentCapabilities(streaming=True), |
| 62 | + skills=[skill], |
| 63 | + authentication=AgentAuthentication(schemes=['public']), |
| 64 | + ) |
| 65 | + |
| 66 | + adk_agent = create_agent( |
| 67 | + client_id=os.getenv('GOOGLE_CLIENT_ID'), |
| 68 | + client_secret=os.getenv('GOOGLE_CLIENT_SECRET'), |
| 69 | + ) |
| 70 | + runner = Runner( |
| 71 | + app_name=agent_card.name, |
| 72 | + agent=adk_agent, |
| 73 | + artifact_service=InMemoryArtifactService(), |
| 74 | + session_service=InMemorySessionService(), |
| 75 | + memory_service=InMemoryMemoryService(), |
| 76 | + ) |
| 77 | + agent_executor = ADKAgentExecutor(runner, agent_card) |
| 78 | + |
| 79 | + async def handle_auth(request: Request) -> PlainTextResponse: |
| 80 | + await agent_executor.on_auth_callback( |
| 81 | + request.query_params.get('state'), str(request.url) |
| 82 | + ) |
| 83 | + return PlainTextResponse('Authentication successful.') |
| 84 | + |
| 85 | + request_handler = DefaultRequestHandler( |
| 86 | + agent_executor=agent_executor, task_store=InMemoryTaskStore() |
| 87 | + ) |
| 88 | + |
| 89 | + a2a_app = A2AStarletteApplication( |
| 90 | + agent_card=agent_card, http_handler=request_handler |
| 91 | + ) |
| 92 | + routes = a2a_app.routes() |
| 93 | + routes.append( |
| 94 | + Route( |
| 95 | + path='/authenticate', |
| 96 | + methods=['GET'], |
| 97 | + endpoint=handle_auth, |
| 98 | + ) |
| 99 | + ) |
| 100 | + app = Starlette(routes=routes) |
| 101 | + |
| 102 | + uvicorn.run(app, host=host, port=port) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == '__main__': |
| 106 | + main() |
0 commit comments