|
| 1 | +# Copyright 2025 © BeeAI a Series of LF Projects, LLC |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +# Based on: https://github.com/a2aproject/a2a-samples/tree/main/samples/python/hosts/cli |
| 5 | + |
| 6 | +import asyncio |
| 7 | +import base64 |
| 8 | +import os |
| 9 | +from uuid import uuid4 |
| 10 | + |
| 11 | +import a2a |
| 12 | +import a2a.client |
| 13 | +import a2a.types |
| 14 | +import anyio |
| 15 | +import asyncclick |
| 16 | +import asyncclick.exceptions |
| 17 | +import httpx |
| 18 | +import yaml |
| 19 | + |
| 20 | + |
| 21 | +@asyncclick.command() |
| 22 | +@asyncclick.option("--agent", default="http://127.0.0.1:10000") |
| 23 | +@asyncclick.option("--session", default=0) |
| 24 | +@asyncclick.option("--history", default=False) |
| 25 | +async def cli( |
| 26 | + agent, |
| 27 | + session, |
| 28 | + history, |
| 29 | +): |
| 30 | + async with httpx.AsyncClient(timeout=30) as httpx_client: |
| 31 | + card_resolver = a2a.client.A2ACardResolver(httpx_client, agent) |
| 32 | + card = await card_resolver.get_agent_card() |
| 33 | + |
| 34 | + print("======= Agent Card ========") |
| 35 | + print(yaml.dump(card.model_dump(mode="json", exclude_none=True))) |
| 36 | + |
| 37 | + client = a2a.client.A2AClient(httpx_client, agent_card=card) |
| 38 | + |
| 39 | + continue_loop = True |
| 40 | + streaming = card.capabilities.streaming |
| 41 | + context_id = session if session > 0 else uuid4().hex |
| 42 | + |
| 43 | + while continue_loop: |
| 44 | + print("\n\n========= starting a new task ======== ") |
| 45 | + continue_loop, _, task_id = await complete_task( |
| 46 | + client=client, |
| 47 | + streaming=streaming, |
| 48 | + task_id=None, |
| 49 | + context_id=context_id, |
| 50 | + ) |
| 51 | + |
| 52 | + if history and continue_loop: |
| 53 | + print("========= history ======== ") |
| 54 | + task_response = await client.get_task( |
| 55 | + a2a.types.GetTaskRequest( |
| 56 | + id=str(uuid4()), params=a2a.types.TaskQueryParams(id=task_id, history_length=10) |
| 57 | + ) |
| 58 | + ) |
| 59 | + print(task_response.model_dump_json(include={"result": {"history": True}})) |
| 60 | + |
| 61 | + |
| 62 | +async def complete_task( |
| 63 | + client: a2a.client.A2AClient, |
| 64 | + streaming, |
| 65 | + task_id, |
| 66 | + context_id, |
| 67 | +): |
| 68 | + try: |
| 69 | + prompt = asyncclick.prompt("\n👤 User (CTRL-D to cancel)") |
| 70 | + except asyncclick.exceptions.Abort: |
| 71 | + print("Exiting...") |
| 72 | + return False, context_id, task_id |
| 73 | + |
| 74 | + message = a2a.types.Message( |
| 75 | + message_id=str(uuid4()), |
| 76 | + role=a2a.types.Role.user, |
| 77 | + parts=[a2a.types.Part(root=a2a.types.TextPart(text=prompt))], |
| 78 | + task_id=task_id, |
| 79 | + context_id=context_id, |
| 80 | + ) |
| 81 | + |
| 82 | + try: |
| 83 | + file_path = asyncclick.prompt( |
| 84 | + "Select a file path to attach? (press enter to skip)", |
| 85 | + default="", |
| 86 | + show_default=False, |
| 87 | + ) |
| 88 | + except asyncclick.exceptions.Abort: |
| 89 | + print("Exiting...") |
| 90 | + return False, context_id, task_id |
| 91 | + |
| 92 | + print("🤖 Agent: ") |
| 93 | + |
| 94 | + if file_path and file_path.strip() != "": |
| 95 | + message.parts.append( |
| 96 | + a2a.types.Part( |
| 97 | + root=a2a.types.FilePart( |
| 98 | + file=a2a.types.FileWithBytes( |
| 99 | + name=os.path.basename(file_path), |
| 100 | + bytes=base64.b64encode(await anyio.Path(file_path).read_bytes()).decode("utf-8"), |
| 101 | + ) |
| 102 | + ) |
| 103 | + ) |
| 104 | + ) |
| 105 | + |
| 106 | + payload = a2a.types.MessageSendParams( |
| 107 | + message=message, |
| 108 | + configuration=a2a.types.MessageSendConfiguration( |
| 109 | + accepted_output_modes=["text"], |
| 110 | + ), |
| 111 | + ) |
| 112 | + |
| 113 | + task_result = None |
| 114 | + message = None |
| 115 | + task_completed = False |
| 116 | + if streaming: |
| 117 | + response_stream = client.send_message_streaming( |
| 118 | + a2a.types.SendStreamingMessageRequest( |
| 119 | + id=str(uuid4()), |
| 120 | + params=payload, |
| 121 | + ) |
| 122 | + ) |
| 123 | + printing_streaming_tokens = False |
| 124 | + async for result in response_stream: |
| 125 | + if isinstance(result.root, a2a.types.JSONRPCErrorResponse): |
| 126 | + if printing_streaming_tokens: |
| 127 | + print() |
| 128 | + printing_streaming_tokens = False |
| 129 | + print(f"Error: {result.root.error}, context_id: {context_id}, task_id: {task_id}") |
| 130 | + return False, context_id, task_id |
| 131 | + event = result.root.result |
| 132 | + context_id = event.context_id |
| 133 | + if isinstance(event, a2a.types.Task): |
| 134 | + task_id = event.id |
| 135 | + if printing_streaming_tokens: |
| 136 | + print() |
| 137 | + printing_streaming_tokens = False |
| 138 | + print(f"TASK => {event.model_dump_json(exclude_none=True)}") |
| 139 | + elif isinstance(event, a2a.types.TaskArtifactUpdateEvent): |
| 140 | + task_id = event.task_id |
| 141 | + if printing_streaming_tokens: |
| 142 | + print() |
| 143 | + printing_streaming_tokens = False |
| 144 | + print(f"ARTIFACT => {event.model_dump_json(exclude_none=True)}") |
| 145 | + elif isinstance(event, a2a.types.TaskStatusUpdateEvent): |
| 146 | + task_id = event.task_id |
| 147 | + if event.status.message: |
| 148 | + if not printing_streaming_tokens: |
| 149 | + print() |
| 150 | + printing_streaming_tokens = True |
| 151 | + for part in event.status.message.parts: |
| 152 | + if isinstance(part.root, a2a.types.TextPart): |
| 153 | + print(part.root.text, end="", flush=True) |
| 154 | + if event.status.state == "completed": |
| 155 | + task_completed = True |
| 156 | + elif isinstance(event, a2a.types.Message): |
| 157 | + message = event |
| 158 | + |
| 159 | + # Upon completion of the stream. Retrieve the full task if one was made. |
| 160 | + if task_id and not task_completed: |
| 161 | + task_result_response = await client.get_task( |
| 162 | + a2a.types.GetTaskRequest( |
| 163 | + id=str(uuid4()), |
| 164 | + params=a2a.types.TaskQueryParams(id=task_id), |
| 165 | + ) |
| 166 | + ) |
| 167 | + if isinstance(task_result_response.root, a2a.types.JSONRPCErrorResponse): |
| 168 | + print(f"Error: {task_result_response.root.error}, context_id: {context_id}, task_id: {task_id}") |
| 169 | + return False, context_id, task_id |
| 170 | + task_result = task_result_response.root.result |
| 171 | + else: |
| 172 | + try: |
| 173 | + # For non-streaming, assume the response is a task or message. |
| 174 | + event = ( |
| 175 | + await client.send_message( |
| 176 | + a2a.types.SendMessageRequest( |
| 177 | + id=str(uuid4()), |
| 178 | + params=payload, |
| 179 | + ) |
| 180 | + ) |
| 181 | + ).root.result |
| 182 | + if not context_id and event: |
| 183 | + context_id = event.context_id |
| 184 | + if isinstance(event, a2a.types.Task): |
| 185 | + if not task_id: |
| 186 | + task_id = event.id |
| 187 | + task_result = event |
| 188 | + elif isinstance(event, a2a.types.Message): |
| 189 | + message = event |
| 190 | + except Exception as e: |
| 191 | + print("Failed to complete the call", e) |
| 192 | + |
| 193 | + if message: |
| 194 | + print(f"\n{message.model_dump_json(exclude_none=True)}") |
| 195 | + return True, context_id, task_id |
| 196 | + if task_result: |
| 197 | + # Don't print the contents of a file. |
| 198 | + task_content = task_result.model_dump_json( |
| 199 | + exclude={ |
| 200 | + "history": { |
| 201 | + "__all__": { |
| 202 | + "parts": { |
| 203 | + "__all__": {"file"}, |
| 204 | + }, |
| 205 | + }, |
| 206 | + }, |
| 207 | + }, |
| 208 | + exclude_none=True, |
| 209 | + ) |
| 210 | + print(f"\n{task_content}") |
| 211 | + ## if the result is that more input is required, loop again. |
| 212 | + state = a2a.types.TaskState(task_result.status.state) |
| 213 | + if state.name == a2a.types.TaskState.input_required.name: |
| 214 | + return ( |
| 215 | + await complete_task( |
| 216 | + client, |
| 217 | + streaming, |
| 218 | + task_id, |
| 219 | + context_id, |
| 220 | + ), |
| 221 | + context_id, |
| 222 | + task_id, |
| 223 | + ) |
| 224 | + ## task is complete |
| 225 | + return True, context_id, task_id |
| 226 | + ## Failure case, shouldn't reach |
| 227 | + return True, context_id, task_id |
| 228 | + |
| 229 | + |
| 230 | +if __name__ == "__main__": |
| 231 | + asyncio.run(cli()) |
0 commit comments