|
| 1 | +import asyncio |
| 2 | +import contextlib |
| 3 | +import logging |
| 4 | + |
| 5 | +import grpc |
| 6 | +import uvicorn |
| 7 | + |
| 8 | +from fastapi import FastAPI |
| 9 | + |
| 10 | +from a2a.compat.v0_3 import a2a_v0_3_pb2_grpc |
| 11 | +from a2a.compat.v0_3.grpc_handler import CompatGrpcHandler |
| 12 | +from a2a.server.agent_execution.agent_executor import AgentExecutor |
| 13 | +from a2a.server.agent_execution.context import RequestContext |
| 14 | +from a2a.server.apps import A2AFastAPIApplication, A2ARESTFastAPIApplication |
| 15 | +from a2a.server.events.event_queue import EventQueue |
| 16 | +from a2a.server.request_handlers import GrpcHandler |
| 17 | +from a2a.server.request_handlers.default_request_handler import ( |
| 18 | + DefaultRequestHandler, |
| 19 | +) |
| 20 | +from a2a.server.tasks.inmemory_task_store import InMemoryTaskStore |
| 21 | +from a2a.server.tasks.task_updater import TaskUpdater |
| 22 | +from a2a.types import ( |
| 23 | + AgentCapabilities, |
| 24 | + AgentCard, |
| 25 | + AgentInterface, |
| 26 | + AgentProvider, |
| 27 | + AgentSkill, |
| 28 | + Part, |
| 29 | + a2a_pb2_grpc, |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +logger = logging.getLogger(__name__) |
| 34 | + |
| 35 | + |
| 36 | +class SampleAgentExecutor(AgentExecutor): |
| 37 | + """Sample agent executor logic similar to the a2a-js sample.""" |
| 38 | + |
| 39 | + def __init__(self) -> None: |
| 40 | + self.running_tasks: set[str] = set() |
| 41 | + |
| 42 | + async def cancel( |
| 43 | + self, context: RequestContext, event_queue: EventQueue |
| 44 | + ) -> None: |
| 45 | + """Cancels a task.""" |
| 46 | + task_id = context.task_id |
| 47 | + if task_id in self.running_tasks: |
| 48 | + self.running_tasks.remove(task_id) |
| 49 | + |
| 50 | + updater = TaskUpdater( |
| 51 | + event_queue=event_queue, |
| 52 | + task_id=task_id or '', |
| 53 | + context_id=context.context_id or '', |
| 54 | + ) |
| 55 | + await updater.cancel() |
| 56 | + |
| 57 | + async def execute( |
| 58 | + self, context: RequestContext, event_queue: EventQueue |
| 59 | + ) -> None: |
| 60 | + """Executes a task inline.""" |
| 61 | + user_message = context.message |
| 62 | + task_id = context.task_id |
| 63 | + context_id = context.context_id |
| 64 | + |
| 65 | + if not user_message or not task_id or not context_id: |
| 66 | + return |
| 67 | + |
| 68 | + self.running_tasks.add(task_id) |
| 69 | + |
| 70 | + logger.info( |
| 71 | + '[SampleAgentExecutor] Processing message %s for task %s (context: %s)', |
| 72 | + user_message.message_id, |
| 73 | + task_id, |
| 74 | + context_id, |
| 75 | + ) |
| 76 | + |
| 77 | + updater = TaskUpdater( |
| 78 | + event_queue=event_queue, |
| 79 | + task_id=task_id, |
| 80 | + context_id=context_id, |
| 81 | + ) |
| 82 | + |
| 83 | + working_message = updater.new_agent_message( |
| 84 | + parts=[Part(text='Processing your question...')] |
| 85 | + ) |
| 86 | + await updater.start_work(message=working_message) |
| 87 | + |
| 88 | + query = context.get_user_input() |
| 89 | + |
| 90 | + agent_reply_text = self._parse_input(query) |
| 91 | + await asyncio.sleep(1) |
| 92 | + |
| 93 | + if task_id not in self.running_tasks: |
| 94 | + return |
| 95 | + |
| 96 | + await updater.add_artifact( |
| 97 | + parts=[Part(text=agent_reply_text)], |
| 98 | + name='response', |
| 99 | + last_chunk=True, |
| 100 | + ) |
| 101 | + await updater.complete() |
| 102 | + |
| 103 | + logger.info( |
| 104 | + '[SampleAgentExecutor] Task %s finished with state: completed', |
| 105 | + task_id, |
| 106 | + ) |
| 107 | + |
| 108 | + def _parse_input(self, query: str) -> str: |
| 109 | + if not query: |
| 110 | + return 'Hello! Please provide a message for me to respond to.' |
| 111 | + |
| 112 | + ql = query.lower() |
| 113 | + if 'hello' in ql or 'hi' in ql: |
| 114 | + return 'Hello World! Nice to meet you!' |
| 115 | + if 'how are you' in ql: |
| 116 | + return ( |
| 117 | + "I'm doing great! Thanks for asking. How can I help you today?" |
| 118 | + ) |
| 119 | + if 'goodbye' in ql or 'bye' in ql: |
| 120 | + return 'Goodbye! Have a wonderful day!' |
| 121 | + return f"Hello World! You said: '{query}'. Thanks for your message!" |
| 122 | + |
| 123 | + |
| 124 | +async def serve( |
| 125 | + host: str = '127.0.0.1', |
| 126 | + port: int = 41241, |
| 127 | + grpc_port: int = 50051, |
| 128 | + compat_grpc_port: int = 50052, |
| 129 | +) -> None: |
| 130 | + """Run the Sample Agent server with mounted JSON-RPC, HTTP+JSON and gRPC transports.""" |
| 131 | + agent_card = AgentCard( |
| 132 | + name='Sample Agent', |
| 133 | + description='A sample agent to test the stream functionality.', |
| 134 | + provider=AgentProvider( |
| 135 | + organization='A2A Samples', url='https://example.com' |
| 136 | + ), |
| 137 | + version='1.0.0', |
| 138 | + capabilities=AgentCapabilities( |
| 139 | + streaming=True, push_notifications=False |
| 140 | + ), |
| 141 | + default_input_modes=['text'], |
| 142 | + default_output_modes=['text', 'task-status'], |
| 143 | + skills=[ |
| 144 | + AgentSkill( |
| 145 | + id='sample_agent', |
| 146 | + name='Sample Agent', |
| 147 | + description='Say hi.', |
| 148 | + tags=['sample'], |
| 149 | + examples=['hi'], |
| 150 | + input_modes=['text'], |
| 151 | + output_modes=['text', 'task-status'], |
| 152 | + ) |
| 153 | + ], |
| 154 | + supported_interfaces=[ |
| 155 | + AgentInterface( |
| 156 | + protocol_binding='GRPC', |
| 157 | + protocol_version='1.0', |
| 158 | + url=f'{host}:{grpc_port}', |
| 159 | + ), |
| 160 | + AgentInterface( |
| 161 | + protocol_binding='GRPC', |
| 162 | + protocol_version='0.3', |
| 163 | + url=f'{host}:{compat_grpc_port}', |
| 164 | + ), |
| 165 | + AgentInterface( |
| 166 | + protocol_binding='JSONRPC', |
| 167 | + protocol_version='1.0', |
| 168 | + url=f'http://{host}:{port}/a2a/jsonrpc/', |
| 169 | + ), |
| 170 | + AgentInterface( |
| 171 | + protocol_binding='JSONRPC', |
| 172 | + protocol_version='0.3', |
| 173 | + url=f'http://{host}:{port}/a2a/jsonrpc/', |
| 174 | + ), |
| 175 | + AgentInterface( |
| 176 | + protocol_binding='HTTP+JSON', |
| 177 | + protocol_version='1.0', |
| 178 | + url=f'http://{host}:{port}/a2a/rest/', |
| 179 | + ), |
| 180 | + AgentInterface( |
| 181 | + protocol_binding='HTTP+JSON', |
| 182 | + protocol_version='0.3', |
| 183 | + url=f'http://{host}:{port}/a2a/rest/', |
| 184 | + ), |
| 185 | + ], |
| 186 | + ) |
| 187 | + |
| 188 | + task_store = InMemoryTaskStore() |
| 189 | + request_handler = DefaultRequestHandler( |
| 190 | + agent_executor=SampleAgentExecutor(), task_store=task_store |
| 191 | + ) |
| 192 | + |
| 193 | + rest_app_builder = A2ARESTFastAPIApplication( |
| 194 | + agent_card=agent_card, |
| 195 | + http_handler=request_handler, |
| 196 | + enable_v0_3_compat=True, |
| 197 | + ) |
| 198 | + rest_app = rest_app_builder.build() |
| 199 | + |
| 200 | + jsonrpc_app_builder = A2AFastAPIApplication( |
| 201 | + agent_card=agent_card, |
| 202 | + http_handler=request_handler, |
| 203 | + enable_v0_3_compat=True, |
| 204 | + ) |
| 205 | + |
| 206 | + app = FastAPI() |
| 207 | + jsonrpc_app_builder.add_routes_to_app(app, rpc_url='/a2a/jsonrpc/') |
| 208 | + app.mount('/a2a/rest', rest_app) |
| 209 | + |
| 210 | + grpc_server = grpc.aio.server() |
| 211 | + grpc_server.add_insecure_port(f'{host}:{grpc_port}') |
| 212 | + servicer = GrpcHandler(agent_card, request_handler) |
| 213 | + a2a_pb2_grpc.add_A2AServiceServicer_to_server(servicer, grpc_server) |
| 214 | + |
| 215 | + compat_grpc_server = grpc.aio.server() |
| 216 | + compat_grpc_server.add_insecure_port(f'{host}:{compat_grpc_port}') |
| 217 | + compat_servicer = CompatGrpcHandler(agent_card, request_handler) |
| 218 | + a2a_v0_3_pb2_grpc.add_A2AServiceServicer_to_server( |
| 219 | + compat_servicer, compat_grpc_server |
| 220 | + ) |
| 221 | + |
| 222 | + config = uvicorn.Config(app, host=host, port=port) |
| 223 | + uvicorn_server = uvicorn.Server(config) |
| 224 | + |
| 225 | + logger.info('Starting Sample Agent servers:') |
| 226 | + logger.info(' - HTTP on http://%s:%s', host, port) |
| 227 | + logger.info(' - gRPC on %s:%s', host, grpc_port) |
| 228 | + logger.info(' - gRPC (v0.3 compat) on %s:%s', host, compat_grpc_port) |
| 229 | + logger.info( |
| 230 | + 'Agent Card available at http://%s:%s/.well-known/agent-card.json', |
| 231 | + host, |
| 232 | + port, |
| 233 | + ) |
| 234 | + |
| 235 | + await asyncio.gather( |
| 236 | + grpc_server.start(), |
| 237 | + compat_grpc_server.start(), |
| 238 | + uvicorn_server.serve(), |
| 239 | + ) |
| 240 | + |
| 241 | + |
| 242 | +if __name__ == '__main__': |
| 243 | + logging.basicConfig(level=logging.INFO) |
| 244 | + with contextlib.suppress(KeyboardInterrupt): |
| 245 | + asyncio.run(serve()) |
0 commit comments