|
| 1 | +import asyncio |
| 2 | +import logging |
| 3 | +import os |
| 4 | + |
| 5 | +import click |
| 6 | +import uvicorn |
| 7 | + |
| 8 | +from foundry_agent_executor import create_foundry_agent_executor |
| 9 | +from dotenv import load_dotenv |
| 10 | +from starlette.applications import Starlette |
| 11 | +from starlette.requests import Request |
| 12 | +from starlette.responses import PlainTextResponse |
| 13 | +from starlette.routing import Route |
| 14 | + |
| 15 | +from a2a.server.apps import A2AStarletteApplication |
| 16 | +from a2a.server.request_handlers import DefaultRequestHandler |
| 17 | +from a2a.server.tasks import InMemoryTaskStore |
| 18 | +from a2a.types import AgentCapabilities, AgentCard, AgentSkill |
| 19 | + |
| 20 | + |
| 21 | +load_dotenv() |
| 22 | + |
| 23 | +logging.basicConfig(level=logging.INFO) |
| 24 | +logger = logging.getLogger(__name__) |
| 25 | + |
| 26 | + |
| 27 | +@click.command() |
| 28 | +@click.option('--host', 'host', default='localhost') |
| 29 | +@click.option('--port', 'port', default=10007) |
| 30 | +def main(host: str, port: int): |
| 31 | + """ |
| 32 | + Run the AI Foundry A2A demo server. |
| 33 | + """ |
| 34 | + # Verify required environment variables |
| 35 | + required_env_vars = [ |
| 36 | + 'AZURE_AI_FOUNDRY_PROJECT_ENDPOINT', |
| 37 | + 'AZURE_AI_AGENT_MODEL_DEPLOYMENT_NAME' |
| 38 | + ] |
| 39 | + |
| 40 | + missing_vars = [var for var in required_env_vars if not os.getenv(var)] |
| 41 | + if missing_vars: |
| 42 | + raise ValueError( |
| 43 | + f"Missing required environment variables: {', '.join(missing_vars)}" |
| 44 | + ) |
| 45 | + |
| 46 | + # Define agent skills |
| 47 | + skills = [ |
| 48 | + AgentSkill( |
| 49 | + id='check_availability', |
| 50 | + name='Check Calendar Availability', |
| 51 | + description="Check if a user is available at a specific time using their calendar", |
| 52 | + tags=['calendar', 'scheduling'], |
| 53 | + examples=[ |
| 54 | + 'Am I free from 10am to 11am tomorrow?', |
| 55 | + 'Check my availability for next Tuesday afternoon', |
| 56 | + 'Do I have any conflicts on Friday morning?' |
| 57 | + ], |
| 58 | + ), |
| 59 | + AgentSkill( |
| 60 | + id='get_upcoming_events', |
| 61 | + name='Get Upcoming Events', |
| 62 | + description="Retrieve upcoming calendar events for the user", |
| 63 | + tags=['calendar', 'events'], |
| 64 | + examples=[ |
| 65 | + 'What meetings do I have today?', |
| 66 | + 'Show me my schedule for this week', |
| 67 | + 'What\'s coming up in the next few hours?' |
| 68 | + ], |
| 69 | + ), |
| 70 | + AgentSkill( |
| 71 | + id='calendar_management', |
| 72 | + name='Calendar Management', |
| 73 | + description="General calendar management and scheduling assistance", |
| 74 | + tags=['calendar', 'productivity'], |
| 75 | + examples=[ |
| 76 | + 'Help me manage my calendar', |
| 77 | + 'When is the best time for a meeting?', |
| 78 | + 'Optimize my schedule for tomorrow' |
| 79 | + ], |
| 80 | + ) |
| 81 | + ] |
| 82 | + |
| 83 | + # Create agent card |
| 84 | + agent_card = AgentCard( |
| 85 | + name='AI Foundry Calendar Agent', |
| 86 | + description="An intelligent calendar management agent powered by Azure AI Foundry. " |
| 87 | + "I can help you check availability, manage events, and optimize your schedule.", |
| 88 | + url=f'http://{host}:{port}/', |
| 89 | + version='1.0.0', |
| 90 | + defaultInputModes=['text'], |
| 91 | + defaultOutputModes=['text'], |
| 92 | + capabilities=AgentCapabilities(streaming=True), |
| 93 | + skills=skills, |
| 94 | + ) |
| 95 | + |
| 96 | + # Create agent executor |
| 97 | + agent_executor = create_foundry_agent_executor(agent_card) |
| 98 | + |
| 99 | + # Create request handler |
| 100 | + request_handler = DefaultRequestHandler( |
| 101 | + agent_executor=agent_executor, |
| 102 | + task_store=InMemoryTaskStore() |
| 103 | + ) |
| 104 | + |
| 105 | + # Create A2A application |
| 106 | + a2a_app = A2AStarletteApplication( |
| 107 | + agent_card=agent_card, |
| 108 | + http_handler=request_handler |
| 109 | + ) |
| 110 | + |
| 111 | + # Get routes |
| 112 | + routes = a2a_app.routes() |
| 113 | + |
| 114 | + # Add health check endpoint |
| 115 | + async def health_check(request: Request) -> PlainTextResponse: |
| 116 | + return PlainTextResponse('AI Foundry Calendar Agent is running!') |
| 117 | + |
| 118 | + routes.append( |
| 119 | + Route( |
| 120 | + path='/health', |
| 121 | + methods=['GET'], |
| 122 | + endpoint=health_check |
| 123 | + ) |
| 124 | + ) |
| 125 | + |
| 126 | + # Create Starlette app |
| 127 | + app = Starlette(routes=routes) |
| 128 | + |
| 129 | + # Log startup information |
| 130 | + logger.info(f"Starting AI Foundry Calendar Agent on {host}:{port}") |
| 131 | + logger.info(f"Agent card: {agent_card.name}") |
| 132 | + logger.info(f"Skills: {[skill.name for skill in skills]}") |
| 133 | + logger.info(f"Health check available at: http://{host}:{port}/health") |
| 134 | + |
| 135 | + # Run the server |
| 136 | + uvicorn.run(app, host=host, port=port) |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == '__main__': |
| 140 | + main() |
0 commit comments