|
| 1 | +import asyncio |
| 2 | +import click |
| 3 | +import json |
| 4 | +import logging |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import uvicorn |
| 8 | + |
| 9 | +from dotenv import load_dotenv |
| 10 | +load_dotenv() |
| 11 | + |
| 12 | +from agent import HRAgent |
| 13 | +from agent_executor import HRAgentExecutor |
| 14 | +from api import hr_api |
| 15 | +from oauth2_middleware import OAuth2Middleware |
| 16 | + |
| 17 | +from a2a.server.apps import A2AStarletteApplication |
| 18 | +from a2a.server.request_handlers import DefaultRequestHandler |
| 19 | +from a2a.server.tasks import InMemoryTaskStore |
| 20 | +from a2a.types import ( |
| 21 | + AgentAuthentication, |
| 22 | + AgentCapabilities, |
| 23 | + AgentCard, |
| 24 | + AgentSkill, |
| 25 | + # ClientCredentialsOAuthFlow, |
| 26 | + # OAuth2SecurityScheme, |
| 27 | + # OAuthFlows, |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +logging.basicConfig(level=logging.INFO) |
| 32 | +logger = logging.getLogger() |
| 33 | + |
| 34 | + |
| 35 | +@click.command() |
| 36 | +@click.option('--host', default='0.0.0.0') |
| 37 | +@click.option('--port_agent', default=10050) |
| 38 | +@click.option('--port_api', default=10051) |
| 39 | +def main(host: str, port_agent: int, port_api: int): |
| 40 | + async def run_all(): |
| 41 | + await asyncio.gather( |
| 42 | + start_agent(host, port_agent), |
| 43 | + start_api(host, port_api), |
| 44 | + ) |
| 45 | + |
| 46 | + asyncio.run(run_all()) |
| 47 | + |
| 48 | + |
| 49 | +async def start_agent(host: str, port): |
| 50 | + agent_card = AgentCard( |
| 51 | + name='Staff0 HR Agent', |
| 52 | + description='This agent handles external verification requests about Staff0 employees made by third parties.', |
| 53 | + url=f'http://{host}:{port}/', |
| 54 | + version='0.1.0', |
| 55 | + defaultInputModes=HRAgent.SUPPORTED_CONTENT_TYPES, |
| 56 | + defaultOutputModes=HRAgent.SUPPORTED_CONTENT_TYPES, |
| 57 | + capabilities=AgentCapabilities(streaming=True), |
| 58 | + skills=[ |
| 59 | + AgentSkill( |
| 60 | + id='is_active_employee', |
| 61 | + name='Check Employment Status Tool', |
| 62 | + description='Confirm whether a person is an active employee of the company.', |
| 63 | + tags=['employment status'], |
| 64 | + examples=[ |
| 65 | + 'Does John Doe with email [email protected] work at Staff0?' |
| 66 | + ], |
| 67 | + ) |
| 68 | + ], |
| 69 | + authentication=AgentAuthentication( |
| 70 | + schemes=['oauth2'], |
| 71 | + credentials=json.dumps({ |
| 72 | + 'tokenUrl': f'https://{os.getenv("HR_AUTH0_DOMAIN")}/oauth/token', |
| 73 | + 'scopes': { |
| 74 | + 'read:employee_status': 'Allows confirming whether a person is an active employee of the company.' |
| 75 | + } |
| 76 | + }), |
| 77 | + ), |
| 78 | + # securitySchemes={ |
| 79 | + # 'oauth2_m2m_client': OAuth2SecurityScheme( |
| 80 | + # description='', |
| 81 | + # flows=OAuthFlows( |
| 82 | + # authorizationCode=ClientCredentialsOAuthFlow( |
| 83 | + # tokenUrl=f'https://{os.getenv("HR_AUTH0_DOMAIN")}/oauth/token', |
| 84 | + # scopes={ |
| 85 | + # 'read:employee_status': 'Allows confirming whether a person is an active employee of the company.', |
| 86 | + # }, |
| 87 | + # ), |
| 88 | + # ), |
| 89 | + # ), |
| 90 | + # }, |
| 91 | + # security=[{ |
| 92 | + # 'oauth2_m2m_client': [ |
| 93 | + # 'read:employee_status', |
| 94 | + # ], |
| 95 | + # }], |
| 96 | + ) |
| 97 | + |
| 98 | + request_handler = DefaultRequestHandler( |
| 99 | + agent_executor=HRAgentExecutor(), |
| 100 | + task_store=InMemoryTaskStore(), |
| 101 | + ) |
| 102 | + |
| 103 | + server = A2AStarletteApplication( |
| 104 | + agent_card=agent_card, http_handler=request_handler |
| 105 | + ) |
| 106 | + |
| 107 | + app = server.build() |
| 108 | + app.add_middleware(OAuth2Middleware, agent_card=agent_card, public_paths=['/.well-known/agent.json']) |
| 109 | + |
| 110 | + logger.info(f'Starting HR Agent server on {host}:{port}') |
| 111 | + await uvicorn.Server(uvicorn.Config(app=app, host=host, port=port)).serve() |
| 112 | + |
| 113 | + |
| 114 | +async def start_api(host: str, port): |
| 115 | + logger.info(f'Starting HR API server on {host}:{port}') |
| 116 | + await uvicorn.Server(uvicorn.Config(app=hr_api, host=host, port=port)).serve() |
| 117 | + |
| 118 | + |
| 119 | +# this ensures that `main()` runs when using `uv run .` |
| 120 | +if not hasattr(sys, '_called_from_uvicorn'): |
| 121 | + main() |
0 commit comments