Skip to content

Commit 7d3df0b

Browse files
fjumaehsavoie
authored andcommitted
feat: Add a base ClientTransport class and create a new JSONRPCTransport client implementation based off the previous A2AClient
1 parent f5643d2 commit 7d3df0b

File tree

9 files changed

+1345
-4
lines changed

9 files changed

+1345
-4
lines changed

client/src/main/java/io/a2a/client/A2AClient.java

Lines changed: 716 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.a2a.client;
2+
3+
import java.util.Map;
4+
5+
public class ClientCallContext {
6+
7+
private final Map<String, Object> state;
8+
private final Map<String, String> httpHeaders;
9+
10+
public ClientCallContext(Map<String, Object> state, Map<String, String> httpHeaders) {
11+
this.state = state;
12+
this.httpHeaders = httpHeaders;
13+
}
14+
15+
public Map<String, Object> getState() {
16+
return state;
17+
}
18+
19+
public Map<String, String> getHttpHeaders() {
20+
return httpHeaders;
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.a2a.client;
2+
3+
import java.util.Map;
4+
5+
import io.a2a.spec.AgentCard;
6+
7+
/**
8+
* An abstract base class for client-side call interceptors.
9+
* Interceptors can inspect and modify requests before they are sent,
10+
* which is ideal for concerns like authentication, logging, or tracing.
11+
*/
12+
public abstract class ClientCallInterceptor {
13+
14+
/**
15+
* Intercept a client call before the request is sent.
16+
*
17+
* @param methodName the name of the protocol method (e.g., 'message/send')
18+
* @param payload the request payload
19+
* @param httpHeaders the http headers to use
20+
* @param agentCard the agent card (may be {@code null})
21+
* @param clientCallContext the {@code ClientCallContext} for this call (may be {@code null})
22+
* @return the potentially modified payload and HTTP headers
23+
*/
24+
public abstract PayloadAndHeaders intercept(String methodName, Object payload, Map<String, String> httpHeaders,
25+
AgentCard agentCard, ClientCallContext clientCallContext);
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.a2a.client;
2+
3+
import java.util.Map;
4+
5+
public class PayloadAndHeaders {
6+
7+
private final Object payload;
8+
private final Map<String, String> httpHeaders;
9+
10+
public PayloadAndHeaders(Object payload, Map<String, String> httpHeaders) {
11+
this.payload = payload;
12+
this.httpHeaders = httpHeaders;
13+
}
14+
15+
public Object getPayload() {
16+
return payload;
17+
}
18+
19+
public Map<String, String> getHttpHeaders() {
20+
return httpHeaders;
21+
}
22+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package io.a2a.client.transport;
2+
3+
import java.util.List;
4+
import java.util.function.Consumer;
5+
6+
import io.a2a.client.ClientCallContext;
7+
import io.a2a.spec.A2AClientException;
8+
import io.a2a.spec.A2AServerException;
9+
import io.a2a.spec.AgentCard;
10+
import io.a2a.spec.DeleteTaskPushNotificationConfigParams;
11+
import io.a2a.spec.EventKind;
12+
import io.a2a.spec.GetTaskPushNotificationConfigParams;
13+
import io.a2a.spec.ListTaskPushNotificationConfigParams;
14+
import io.a2a.spec.MessageSendParams;
15+
import io.a2a.spec.StreamingEventKind;
16+
import io.a2a.spec.Task;
17+
import io.a2a.spec.TaskIdParams;
18+
import io.a2a.spec.TaskPushNotificationConfig;
19+
import io.a2a.spec.TaskQueryParams;
20+
21+
/**
22+
* Abstract base class for a client transport.
23+
*/
24+
public abstract class ClientTransport {
25+
26+
/**
27+
* Send a non-streaming message request to the agent.
28+
*
29+
* @param request the message send parameters
30+
* @param context optional client call context for the request (may be {@code null})
31+
* @return the response, either a Task or Message
32+
* @throws A2AClientException if sending the message fails for any reason
33+
*/
34+
public abstract EventKind sendMessage(MessageSendParams request, ClientCallContext context)
35+
throws A2AClientException;
36+
37+
/**
38+
* Send a streaming message request to the agent and receive responses as they arrive.
39+
*
40+
* @param request the message send parameters
41+
* @param eventConsumer consumer that will receive streaming events as they arrive
42+
* @param errorConsumer consumer that will be called if an error occurs during streaming
43+
* @param context optional client call context for the request (may be {@code null})
44+
* @throws A2AClientException if setting up the streaming connection fails
45+
*/
46+
public abstract void sendMessageStreaming(MessageSendParams request, Consumer<StreamingEventKind> eventConsumer,
47+
Consumer<Throwable> errorConsumer, ClientCallContext context) throws A2AClientException;
48+
49+
/**
50+
* Retrieve the current state and history of a specific task.
51+
*
52+
* @param request the task query parameters specifying which task to retrieve
53+
* @param context optional client call context for the request (may be {@code null})
54+
* @return the task
55+
* @throws A2AClientException if retrieving the task fails for any reason
56+
*/
57+
public abstract Task getTask(TaskQueryParams request, ClientCallContext context) throws A2AClientException;
58+
59+
/**
60+
* Request the agent to cancel a specific task.
61+
*
62+
* @param request the task ID parameters specifying which task to cancel
63+
* @param context optional client call context for the request (may be {@code null})
64+
* @return the cancelled task
65+
* @throws A2AClientException if cancelling the task fails for any reason
66+
*/
67+
public abstract Task cancelTask(TaskIdParams request, ClientCallContext context) throws A2AClientException;
68+
69+
/**
70+
* Set or update the push notification configuration for a specific task.
71+
*
72+
* @param request the push notification configuration to set for the task
73+
* @param context optional client call context for the request (may be {@code null})
74+
* @return the configured TaskPushNotificationConfig
75+
* @throws A2AClientException if setting the task push notification configuration fails for any reason
76+
*/
77+
public abstract TaskPushNotificationConfig setTaskPushNotificationConfiguration(TaskPushNotificationConfig request,
78+
ClientCallContext context) throws A2AClientException;
79+
80+
/**
81+
* Retrieve the push notification configuration for a specific task.
82+
*
83+
* @param request the parameters specifying which task's notification config to retrieve
84+
* @param context optional client call context for the request (may be {@code null})
85+
* @return the task push notification config
86+
* @throws A2AClientException if getting the task push notification config fails for any reason
87+
*/
88+
public abstract TaskPushNotificationConfig getTaskPushNotificationConfiguration(
89+
GetTaskPushNotificationConfigParams request,
90+
ClientCallContext context) throws A2AClientException;
91+
92+
/**
93+
* Retrieve the list of push notification configurations for a specific task.
94+
*
95+
* @param request the parameters specifying which task's notification configs to retrieve
96+
* @param context optional client call context for the request (may be {@code null})
97+
* @return the list of task push notification configs
98+
* @throws A2AClientException if getting the task push notification configs fails for any reason
99+
*/
100+
public abstract List<TaskPushNotificationConfig> listTaskPushNotificationConfigurations(
101+
ListTaskPushNotificationConfigParams request,
102+
ClientCallContext context) throws A2AClientException;
103+
104+
/**
105+
* Delete the list of push notification configurations for a specific task.
106+
*
107+
* @param request the parameters specifying which task's notification configs to delete
108+
* @param context optional client call context for the request (may be {@code null})
109+
* @throws A2AClientException if deleting the task push notification configs fails for any reason
110+
*/
111+
public abstract void deleteTaskPushNotificationConfigurations(
112+
DeleteTaskPushNotificationConfigParams request,
113+
ClientCallContext context) throws A2AClientException;
114+
115+
/**
116+
* Reconnect to get task updates for an existing task.
117+
*
118+
* @param request the task ID parameters specifying which task to resubscribe to
119+
* @param eventConsumer consumer that will receive streaming events as they arrive
120+
* @param errorConsumer consumer that will be called if an error occurs during streaming
121+
* @param context optional client call context for the request (may be {@code null})
122+
* @throws A2AClientException if resubscribing to the task fails for any reason
123+
*/
124+
public abstract void resubscribe(TaskIdParams request, Consumer<StreamingEventKind> eventConsumer,
125+
Consumer<Throwable> errorConsumer, ClientCallContext context) throws A2AClientException;
126+
127+
/**
128+
* Retrieve the AgentCard.
129+
*
130+
* @param context optional client call context for the request (may be {@code null})
131+
* @return the AgentCard
132+
* @throws A2AClientException if retrieving the agent card fails for any reason
133+
*/
134+
public abstract AgentCard getAgentCard(ClientCallContext context) throws A2AClientException;
135+
136+
/**
137+
* Close the transport and release any associated resources.
138+
*/
139+
public abstract void close();
140+
}

0 commit comments

Comments
 (0)