|
| 1 | +import httpx |
| 2 | + |
| 3 | +from fastapi import FastAPI |
| 4 | + |
| 5 | +from a2a.server.agent_execution import AgentExecutor, RequestContext |
| 6 | +from a2a.server.apps import A2ARESTFastAPIApplication |
| 7 | +from a2a.server.events import EventQueue |
| 8 | +from a2a.server.request_handlers import DefaultRequestHandler |
| 9 | +from a2a.server.tasks import ( |
| 10 | + BasePushNotificationSender, |
| 11 | + InMemoryPushNotificationConfigStore, |
| 12 | + InMemoryTaskStore, |
| 13 | + TaskUpdater, |
| 14 | +) |
| 15 | +from a2a.types import ( |
| 16 | + AgentCapabilities, |
| 17 | + AgentCard, |
| 18 | + AgentSkill, |
| 19 | + InvalidParamsError, |
| 20 | + Message, |
| 21 | + Task, |
| 22 | +) |
| 23 | +from a2a.utils import ( |
| 24 | + new_agent_text_message, |
| 25 | + new_task, |
| 26 | +) |
| 27 | +from a2a.utils.errors import ServerError |
| 28 | + |
| 29 | + |
| 30 | +def test_agent_card(url: str) -> AgentCard: |
| 31 | + """Returns an agent card for the test agent.""" |
| 32 | + return AgentCard( |
| 33 | + name='Test Agent', |
| 34 | + description='Just a test agent', |
| 35 | + url=url, |
| 36 | + version='1.0.0', |
| 37 | + default_input_modes=['text'], |
| 38 | + default_output_modes=['text'], |
| 39 | + capabilities=AgentCapabilities(streaming=True, push_notifications=True), |
| 40 | + skills=[ |
| 41 | + AgentSkill( |
| 42 | + id='greeting', |
| 43 | + name='Greeting Agent', |
| 44 | + description='just greats the user', |
| 45 | + tags=['greeting'], |
| 46 | + examples=['Hello Agent!', 'How are you?'], |
| 47 | + ) |
| 48 | + ], |
| 49 | + supports_authenticated_extended_card=True, |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +class TestAgent: |
| 54 | + """Agent for push notification testing.""" |
| 55 | + |
| 56 | + async def invoke( |
| 57 | + self, updater: TaskUpdater, msg: Message, task: Task |
| 58 | + ) -> None: |
| 59 | + # Fail for unsupported messages. |
| 60 | + if ( |
| 61 | + not msg.parts |
| 62 | + or len(msg.parts) != 1 |
| 63 | + or msg.parts[0].root.kind != 'text' |
| 64 | + ): |
| 65 | + await updater.failed( |
| 66 | + new_agent_text_message( |
| 67 | + 'Unsupported mesesage.', task.context_id, task.id |
| 68 | + ) |
| 69 | + ) |
| 70 | + return |
| 71 | + text_message = msg.parts[0].root.text |
| 72 | + |
| 73 | + # Simple request-response flow. |
| 74 | + if text_message == 'Hello Agent!': |
| 75 | + await updater.complete( |
| 76 | + new_agent_text_message('Hello User!', task.context_id, task.id) |
| 77 | + ) |
| 78 | + |
| 79 | + # Flow with user input required: "How are you?" -> "Good! How are you?" -> "Good" -> "Amazing". |
| 80 | + elif text_message == 'How are you?': |
| 81 | + await updater.requires_input( |
| 82 | + new_agent_text_message( |
| 83 | + 'Good! How are you?', task.context_id, task.id |
| 84 | + ) |
| 85 | + ) |
| 86 | + elif text_message == 'Good': |
| 87 | + await updater.complete( |
| 88 | + new_agent_text_message('Amazing', task.context_id, task.id) |
| 89 | + ) |
| 90 | + |
| 91 | + # Fail for unsupported messages. |
| 92 | + else: |
| 93 | + await updater.failed( |
| 94 | + new_agent_text_message( |
| 95 | + 'Unsupported message.', task.context_id, task.id |
| 96 | + ) |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +class TestAgentExecutor(AgentExecutor): |
| 101 | + """Test AgentExecutor implementation.""" |
| 102 | + |
| 103 | + def __init__(self) -> None: |
| 104 | + self.agent = TestAgent() |
| 105 | + |
| 106 | + async def execute( |
| 107 | + self, |
| 108 | + context: RequestContext, |
| 109 | + event_queue: EventQueue, |
| 110 | + ) -> None: |
| 111 | + if not context.message: |
| 112 | + raise ServerError(error=InvalidParamsError(message='No message')) |
| 113 | + |
| 114 | + task = context.current_task |
| 115 | + if not task: |
| 116 | + task = new_task(context.message) |
| 117 | + await event_queue.enqueue_event(task) |
| 118 | + updater = TaskUpdater(event_queue, task.id, task.context_id) |
| 119 | + |
| 120 | + await self.agent.invoke(updater, context.message, task) |
| 121 | + |
| 122 | + async def cancel( |
| 123 | + self, context: RequestContext, event_queue: EventQueue |
| 124 | + ) -> None: |
| 125 | + raise Exception('cancel not supported') |
| 126 | + |
| 127 | + |
| 128 | +def create_agent_app( |
| 129 | + url: str, notification_client: httpx.AsyncClient |
| 130 | +) -> FastAPI: |
| 131 | + """Creates a new HTTP+REST FastAPI application for the test agent.""" |
| 132 | + push_config_store = InMemoryPushNotificationConfigStore() |
| 133 | + app = A2ARESTFastAPIApplication( |
| 134 | + agent_card=test_agent_card(url), |
| 135 | + http_handler=DefaultRequestHandler( |
| 136 | + agent_executor=TestAgentExecutor(), |
| 137 | + task_store=InMemoryTaskStore(), |
| 138 | + push_config_store=push_config_store, |
| 139 | + push_sender=BasePushNotificationSender( |
| 140 | + httpx_client=notification_client, |
| 141 | + config_store=push_config_store, |
| 142 | + ), |
| 143 | + ), |
| 144 | + ) |
| 145 | + return app.build() |
0 commit comments