|
| 1 | +from typing_extensions import override |
| 2 | + |
| 3 | +from a2a.server.agent_execution import AgentExecutor, RequestContext |
| 4 | +from a2a.server.events import EventQueue |
| 5 | +from a2a.utils import new_agent_text_message |
| 6 | + |
| 7 | + |
| 8 | +class HelloWorldAgent: |
| 9 | + """A simple agent that returns a static 'Hello, world!' message.""" |
| 10 | + |
| 11 | + async def invoke(self) -> str: |
| 12 | + """Invokes the agent's main logic and returns a response message. |
| 13 | +
|
| 14 | + Returns: |
| 15 | + str: The fixed message 'Hello, world!'. |
| 16 | + """ |
| 17 | + return 'Hello, world!' |
| 18 | + |
| 19 | + |
| 20 | +class HelloWorldAgentExecutor(AgentExecutor): |
| 21 | + """AgentExecutor implementation for the HelloWorldAgent. |
| 22 | +
|
| 23 | + This executor wraps a HelloWorldAgent and, when executed, sends |
| 24 | + a single text message event with the message "Hello World". |
| 25 | +
|
| 26 | + Intended for demonstration, testing, or HelloWorld scaffolding purposes. |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self) -> None: |
| 30 | + """Initializes the executor with a HelloWorldAgent instance.""" |
| 31 | + self.agent = HelloWorldAgent() |
| 32 | + |
| 33 | + @override |
| 34 | + async def execute( |
| 35 | + self, |
| 36 | + context: RequestContext, |
| 37 | + event_queue: EventQueue, |
| 38 | + ) -> None: |
| 39 | + """Executes the agent by invoking it and emitting the result as a text message event. |
| 40 | +
|
| 41 | + Args: |
| 42 | + context: The request context provided by the framework. |
| 43 | + event_queue: The event queue to which agent messages should be enqueued. |
| 44 | + """ |
| 45 | + result = await self.agent.invoke() |
| 46 | + event_queue.enqueue_event(new_agent_text_message(result)) |
| 47 | + |
| 48 | + @override |
| 49 | + async def cancel( |
| 50 | + self, context: RequestContext, event_queue: EventQueue |
| 51 | + ) -> None: |
| 52 | + """Raises an exception because cancelation is not supported for this example agent. |
| 53 | +
|
| 54 | + Args: |
| 55 | + context: The request context (not used in this method). |
| 56 | + event_queue: The event queue (not used in this method). |
| 57 | +
|
| 58 | + Raises: |
| 59 | + Exception: Always raised, indicating cancel is not supported. |
| 60 | + """ |
| 61 | + raise Exception('cancel not supported') |
| 62 | + |
| 63 | + |
| 64 | +class EchoAgent: |
| 65 | + """An agent that returns the input message as-is.""" |
| 66 | + |
| 67 | + async def invoke(self, message: str) -> str: |
| 68 | + """Invokes the agent's main logic and returns the input message unchanged. |
| 69 | +
|
| 70 | + This method simulates an echo behavior by returning |
| 71 | + the same message that was passed as input. |
| 72 | +
|
| 73 | + Args: |
| 74 | + message: The input string to echo. |
| 75 | +
|
| 76 | + Returns: |
| 77 | + The same string that was provided as input. |
| 78 | + """ |
| 79 | + return message |
| 80 | + |
| 81 | + |
| 82 | +class EchoAgentExecutor(AgentExecutor): |
| 83 | + """AgentExecutor implementation for the EchoAgent. |
| 84 | +
|
| 85 | + This executor wraps an EchoAgent and, when executed, it sends back |
| 86 | + the same message it receives, simulating a basic echo response. |
| 87 | +
|
| 88 | + Intended for demonstration, testing, or HelloWorld scaffolding purposes. |
| 89 | + """ |
| 90 | + |
| 91 | + def __init__(self) -> None: |
| 92 | + """Initializes the executor with a EchoAgent instance.""" |
| 93 | + self.agent = EchoAgent() |
| 94 | + |
| 95 | + @override |
| 96 | + async def execute( |
| 97 | + self, |
| 98 | + context: RequestContext, |
| 99 | + event_queue: EventQueue, |
| 100 | + ) -> None: |
| 101 | + """Executes the agent by invoking it and emitting the result as a text message event. |
| 102 | +
|
| 103 | + Args: |
| 104 | + context: The request context provided by the framework. |
| 105 | + event_queue: The event queue to which agent messages should be enqueued. |
| 106 | + """ |
| 107 | + message = context.get_user_input() |
| 108 | + result = await self.agent.invoke(message) |
| 109 | + event_queue.enqueue_event(new_agent_text_message(result)) |
| 110 | + |
| 111 | + @override |
| 112 | + async def cancel( |
| 113 | + self, context: RequestContext, event_queue: EventQueue |
| 114 | + ) -> None: |
| 115 | + """Raises an exception because cancelation is not supported for this example agent. |
| 116 | +
|
| 117 | + Args: |
| 118 | + context: The request context (not used in this method). |
| 119 | + event_queue: The event queue (not used in this method). |
| 120 | +
|
| 121 | + Raises: |
| 122 | + Exception: Always raised, indicating cancel is not supported. |
| 123 | + """ |
| 124 | + raise Exception('cancel not supported') |
0 commit comments