|
| 1 | +package io.a2a.client; |
| 2 | + |
| 3 | +import static io.a2a.util.Assert.checkNotNullParam; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.function.BiConsumer; |
| 8 | +import java.util.function.Consumer; |
| 9 | + |
| 10 | +import io.a2a.spec.A2AClientException; |
| 11 | +import io.a2a.spec.AgentCard; |
| 12 | +import io.a2a.spec.DeleteTaskPushNotificationConfigParams; |
| 13 | +import io.a2a.spec.GetTaskPushNotificationConfigParams; |
| 14 | +import io.a2a.spec.ListTaskPushNotificationConfigParams; |
| 15 | +import io.a2a.spec.Message; |
| 16 | +import io.a2a.spec.PushNotificationConfig; |
| 17 | +import io.a2a.spec.Task; |
| 18 | +import io.a2a.spec.TaskIdParams; |
| 19 | +import io.a2a.spec.TaskPushNotificationConfig; |
| 20 | +import io.a2a.spec.TaskQueryParams; |
| 21 | + |
| 22 | +/** |
| 23 | + * Abstract class representing an A2A client. Provides a standard set |
| 24 | + * of methods for interacting with an A2A agent, regardless of the underlying |
| 25 | + * transport protocol. It supports sending messages, managing tasks, and |
| 26 | + * handling event streams. |
| 27 | + */ |
| 28 | +public abstract class AbstractClient { |
| 29 | + |
| 30 | + private final List<BiConsumer<ClientEvent, AgentCard>> consumers; |
| 31 | + private final Consumer<Throwable> streamingErrorHandler; |
| 32 | + |
| 33 | + public AbstractClient(List<BiConsumer<ClientEvent, AgentCard>> consumers) { |
| 34 | + this(consumers, null); |
| 35 | + } |
| 36 | + |
| 37 | + public AbstractClient(List<BiConsumer<ClientEvent, AgentCard>> consumers, Consumer<Throwable> streamingErrorHandler) { |
| 38 | + checkNotNullParam("consumers", consumers); |
| 39 | + this.consumers = consumers; |
| 40 | + this.streamingErrorHandler = streamingErrorHandler; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Send a message to the remote agent. This method will automatically use |
| 45 | + * the streaming or non-streaming approach as determined by the server's |
| 46 | + * agent card and the client configuration. The configured client consumers |
| 47 | + * and will be used to handle messages, tasks, and update events received |
| 48 | + * from the remote agent. The configured streaming error handler will be used |
| 49 | + * if an error occurs during streaming. The configured client push notification |
| 50 | + * configuration will get used for streaming. |
| 51 | + * |
| 52 | + * @param request the message |
| 53 | + * @param context optional client call context for the request (may be {@code null}) |
| 54 | + * @throws A2AClientException if sending the message fails for any reason |
| 55 | + */ |
| 56 | + public abstract void sendMessage(Message request, ClientCallContext context) throws A2AClientException; |
| 57 | + |
| 58 | + /** |
| 59 | + * Send a message to the remote agent. This method will automatically use |
| 60 | + * the streaming or non-streaming approach as determined by the server's |
| 61 | + * agent card and the client configuration. The configured client consumers |
| 62 | + * will be used to handle messages, tasks, and update events received from |
| 63 | + * the remote agent. The configured streaming error handler will be used |
| 64 | + * if an error occurs during streaming. |
| 65 | + * |
| 66 | + * @param request the message |
| 67 | + * @param pushNotificationConfiguration the push notification configuration that should be |
| 68 | + * used if the streaming approach is used |
| 69 | + * @param metadata the optional metadata to include when sending the message |
| 70 | + * @throws A2AClientException if sending the message fails for any reason |
| 71 | + */ |
| 72 | + public abstract void sendMessage(Message request, PushNotificationConfig pushNotificationConfiguration, |
| 73 | + Map<String, Object> metadata, ClientCallContext context) throws A2AClientException; |
| 74 | + |
| 75 | + /** |
| 76 | + * Retrieve the current state and history of a specific task. |
| 77 | + * |
| 78 | + * @param request the task query parameters specifying which task to retrieve |
| 79 | + * @param context optional client call context for the request (may be {@code null}) |
| 80 | + * @return the task |
| 81 | + * @throws A2AClientException if retrieving the task fails for any reason |
| 82 | + */ |
| 83 | + public abstract Task getTask(TaskQueryParams request, ClientCallContext context) throws A2AClientException; |
| 84 | + |
| 85 | + /** |
| 86 | + * Request the agent to cancel a specific task. |
| 87 | + * |
| 88 | + * @param request the task ID parameters specifying which task to cancel |
| 89 | + * @param context optional client call context for the request (may be {@code null}) |
| 90 | + * @return the cancelled task |
| 91 | + * @throws A2AClientException if cancelling the task fails for any reason |
| 92 | + */ |
| 93 | + public abstract Task cancelTask(TaskIdParams request, ClientCallContext context) throws A2AClientException; |
| 94 | + |
| 95 | + /** |
| 96 | + * Set or update the push notification configuration for a specific task. |
| 97 | + * |
| 98 | + * @param request the push notification configuration to set for the task |
| 99 | + * @param context optional client call context for the request (may be {@code null}) |
| 100 | + * @return the configured TaskPushNotificationConfig |
| 101 | + * @throws A2AClientException if setting the task push notification configuration fails for any reason |
| 102 | + */ |
| 103 | + public abstract TaskPushNotificationConfig setTaskPushNotificationConfiguration( |
| 104 | + TaskPushNotificationConfig request, |
| 105 | + ClientCallContext context) throws A2AClientException; |
| 106 | + |
| 107 | + /** |
| 108 | + * Retrieve the push notification configuration for a specific task. |
| 109 | + * |
| 110 | + * @param request the parameters specifying which task's notification config to retrieve |
| 111 | + * @param context optional client call context for the request (may be {@code null}) |
| 112 | + * @return the task push notification config |
| 113 | + * @throws A2AClientException if getting the task push notification config fails for any reason |
| 114 | + */ |
| 115 | + public abstract TaskPushNotificationConfig getTaskPushNotificationConfiguration( |
| 116 | + GetTaskPushNotificationConfigParams request, |
| 117 | + ClientCallContext context) throws A2AClientException; |
| 118 | + |
| 119 | + /** |
| 120 | + * Retrieve the list of push notification configurations for a specific task. |
| 121 | + * |
| 122 | + * @param request the parameters specifying which task's notification configs to retrieve |
| 123 | + * @param context optional client call context for the request (may be {@code null}) |
| 124 | + * @return the list of task push notification configs |
| 125 | + * @throws A2AClientException if getting the task push notification configs fails for any reason |
| 126 | + */ |
| 127 | + public abstract List<TaskPushNotificationConfig> listTaskPushNotificationConfigurations( |
| 128 | + ListTaskPushNotificationConfigParams request, |
| 129 | + ClientCallContext context) throws A2AClientException; |
| 130 | + |
| 131 | + /** |
| 132 | + * Delete the list of push notification configurations for a specific task. |
| 133 | + * |
| 134 | + * @param request the parameters specifying which task's notification configs to delete |
| 135 | + * @param context optional client call context for the request (may be {@code null}) |
| 136 | + * @throws A2AClientException if deleting the task push notification configs fails for any reason |
| 137 | + */ |
| 138 | + public abstract void deleteTaskPushNotificationConfigurations( |
| 139 | + DeleteTaskPushNotificationConfigParams request, |
| 140 | + ClientCallContext context) throws A2AClientException; |
| 141 | + |
| 142 | + /** |
| 143 | + * Resubscribe to a task's event stream. |
| 144 | + * This is only available if both the client and server support streaming. |
| 145 | + * |
| 146 | + * @param request the parameters specifying which task's notification configs to delete |
| 147 | + * @param context optional client call context for the request (may be {@code null}) |
| 148 | + * @throws A2AClientException if resubscribing fails for any reason |
| 149 | + */ |
| 150 | + public abstract void resubscribe(TaskIdParams request, ClientCallContext context) throws A2AClientException; |
| 151 | + |
| 152 | + /** |
| 153 | + * Retrieve the AgentCard. |
| 154 | + * |
| 155 | + * @param context optional client call context for the request (may be {@code null}) |
| 156 | + * @return the AgentCard |
| 157 | + * @throws A2AClientException if retrieving the agent card fails for any reason |
| 158 | + */ |
| 159 | + public abstract AgentCard getAgentCard(ClientCallContext context) throws A2AClientException; |
| 160 | + |
| 161 | + /** |
| 162 | + * Close the transport and release any associated resources. |
| 163 | + */ |
| 164 | + public abstract void close(); |
| 165 | + |
| 166 | + /** |
| 167 | + * Process the event using all configured consumers. |
| 168 | + */ |
| 169 | + public void consume(ClientEvent clientEventOrMessage, AgentCard agentCard) { |
| 170 | + for (BiConsumer<ClientEvent, AgentCard> consumer : consumers) { |
| 171 | + consumer.accept(clientEventOrMessage, agentCard); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Get the error handler that should be used during streaming. |
| 177 | + * |
| 178 | + * @return the streaming error handler |
| 179 | + */ |
| 180 | + public Consumer<Throwable> getStreamingErrorHandler() { |
| 181 | + return streamingErrorHandler; |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments