11package io .a2a .server .apps .common ;
22
3+ import java .util .concurrent .CountDownLatch ;
4+ import java .util .concurrent .atomic .AtomicReference ;
5+ import java .util .function .BiConsumer ;
6+
37import io .a2a .client .Client ;
48import io .a2a .client .ClientBuilder ;
9+ import io .a2a .client .ClientEvent ;
10+ import io .a2a .client .TaskEvent ;
11+ import io .a2a .client .TaskUpdateEvent ;
512import io .a2a .client .config .ClientConfig ;
613import io .a2a .client .transport .grpc .GrpcTransport ;
714import io .a2a .client .transport .grpc .GrpcTransportConfigBuilder ;
1118import io .a2a .client .transport .rest .RestTransportConfigBuilder ;
1219import io .a2a .spec .A2AClientException ;
1320import io .a2a .spec .AgentCard ;
21+ import io .a2a .spec .Task ;
1422import io .a2a .spec .TransportProtocol ;
1523import io .grpc .ManagedChannel ;
1624import io .grpc .ManagedChannelBuilder ;
2129 */
2230public class AgentToAgentClientFactory {
2331
32+ /**
33+ * Creates a BiConsumer that captures the final task state.
34+ * This utility method is used by both test classes and agent executors to avoid code duplication.
35+ *
36+ * @param taskRef the AtomicReference to store the final task
37+ * @param latch the CountDownLatch to signal completion
38+ * @return a BiConsumer that captures completed tasks
39+ */
40+ public static BiConsumer <ClientEvent , AgentCard > createTaskCaptureConsumer (
41+ AtomicReference <Task > taskRef , CountDownLatch latch ) {
42+ return (event , agentCard ) -> {
43+ Task task = null ;
44+ if (event instanceof TaskEvent taskEvent ) {
45+ task = taskEvent .getTask ();
46+ } else if (event instanceof TaskUpdateEvent taskUpdateEvent ) {
47+ task = taskUpdateEvent .getTask ();
48+ }
49+
50+ if (task != null && task .status ().state ().isFinal ()) {
51+ taskRef .set (task );
52+ latch .countDown ();
53+ }
54+ };
55+ }
56+
2457 /**
2558 * Creates a client for the specified transport protocol.
59+ * The agent card parameter already contains the correct local endpoint URLs
60+ * configured by the test's AgentCardProducer.
2661 *
27- * @param agentCard the agentcard of the remote server
62+ * @param agentCard the agent card with correct local endpoints
2863 * @param transportProtocol the transport protocol to use
29- * @param serverUrl the server URL (e.g., "http://localhost:8081" or "localhost:9090")
3064 * @return configured client
3165 * @throws A2AClientException if client creation fails
3266 */
33- public static Client createClient (AgentCard agentCard , TransportProtocol transportProtocol , String serverUrl )
67+ public static Client createClient (AgentCard agentCard , TransportProtocol transportProtocol )
3468 throws A2AClientException {
3569 ClientConfig clientConfig = ClientConfig .builder ()
3670 .setStreaming (false )
@@ -46,7 +80,7 @@ public static Client createClient(AgentCard agentCard, TransportProtocol transpo
4680 default -> throw new IllegalArgumentException ("Unsupported transport: " + transportProtocol );
4781 };
4882
49- enhancer .enhance (clientBuilder , serverUrl );
83+ enhancer .enhance (clientBuilder );
5084 return clientBuilder .build ();
5185 }
5286
@@ -55,12 +89,12 @@ public static Client createClient(AgentCard agentCard, TransportProtocol transpo
5589 * not on the classpath.
5690 */
5791 interface ClientTransportEnhancer {
58- void enhance (ClientBuilder clientBuilder , String serverUrl );
92+ void enhance (ClientBuilder clientBuilder );
5993 }
6094
6195 private static class GrpcClientEnhancer implements AgentToAgentClientFactory .ClientTransportEnhancer {
6296 @ Override
63- public void enhance (ClientBuilder clientBuilder , String serverUrl ) {
97+ public void enhance (ClientBuilder clientBuilder ) {
6498 clientBuilder .withTransport (GrpcTransport .class , new GrpcTransportConfigBuilder ().channelFactory (target -> {
6599 ManagedChannel channel = ManagedChannelBuilder .forTarget (target ).usePlaintext ().build ();
66100 return channel ;
@@ -70,14 +104,14 @@ public void enhance(ClientBuilder clientBuilder, String serverUrl) {
70104
71105 private static class JsonRpcClientEnhancer implements AgentToAgentClientFactory .ClientTransportEnhancer {
72106 @ Override
73- public void enhance (ClientBuilder clientBuilder , String serverUrl ) {
107+ public void enhance (ClientBuilder clientBuilder ) {
74108 clientBuilder .withTransport (JSONRPCTransport .class , new JSONRPCTransportConfigBuilder ());
75109 }
76110 }
77111
78112 private static class RestClientEnhancer implements AgentToAgentClientFactory .ClientTransportEnhancer {
79113 @ Override
80- public void enhance (ClientBuilder clientBuilder , String serverUrl ) {
114+ public void enhance (ClientBuilder clientBuilder ) {
81115 clientBuilder .withTransport (RestTransport .class , new RestTransportConfigBuilder ());
82116 }
83117 }
0 commit comments