Skip to content

Commit 3e7ebc9

Browse files
committed
fix: Update the helloworld client example to use the ClientFactory
1 parent e077233 commit 3e7ebc9

File tree

5 files changed

+63
-21
lines changed

5 files changed

+63
-21
lines changed

client/base/src/main/java/io/a2a/client/ClientFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ public Client create(AgentCard agentCard, List<BiConsumer<ClientEvent, AgentCard
8080
private static LinkedHashMap<String, String> getServerPreferredTransports(AgentCard agentCard) {
8181
LinkedHashMap<String, String> serverPreferredTransports = new LinkedHashMap<>();
8282
serverPreferredTransports.put(agentCard.preferredTransport(), agentCard.url());
83-
for (AgentInterface agentInterface : agentCard.additionalInterfaces()) {
84-
serverPreferredTransports.putIfAbsent(agentInterface.transport(), agentInterface.url());
83+
if (agentCard.additionalInterfaces() != null) {
84+
for (AgentInterface agentInterface : agentCard.additionalInterfaces()) {
85+
serverPreferredTransports.putIfAbsent(agentInterface.transport(), agentInterface.url());
86+
}
8587
}
8688
return serverPreferredTransports;
8789
}

examples/helloworld/client/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<description>Examples for the Java SDK for the Agent2Agent Protocol (A2A)</description>
1717

1818
<dependencies>
19+
<dependency>
20+
<groupId>io.github.a2asdk</groupId>
21+
<artifactId>a2a-java-sdk-client</artifactId>
22+
</dependency>
1923
<dependency>
2024
<groupId>io.github.a2asdk</groupId>
2125
<artifactId>a2a-java-sdk-client-transport-jsonrpc</artifactId>

examples/helloworld/client/src/main/java/io/a2a/examples/helloworld/HelloWorldClient.java

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package io.a2a.examples.helloworld;
22

3+
import java.util.ArrayList;
34
import java.util.HashMap;
5+
import java.util.List;
46
import java.util.Map;
7+
import java.util.concurrent.CompletableFuture;
8+
import java.util.function.BiConsumer;
9+
import java.util.function.Consumer;
510

611
import com.fasterxml.jackson.databind.ObjectMapper;
712
import io.a2a.A2A;
13+
import io.a2a.client.A2ACardResolver;
14+
import io.a2a.client.Client;
15+
import io.a2a.client.ClientEvent;
816
import io.a2a.client.ClientFactory;
9-
import io.a2a.client.transport.jsonrpc.JSONRPCTransport;
17+
import io.a2a.client.MessageEvent;
18+
import io.a2a.client.config.ClientConfig;
1019
import io.a2a.spec.AgentCard;
11-
import io.a2a.spec.EventKind;
1220
import io.a2a.spec.Message;
13-
import io.a2a.spec.MessageSendParams;
14-
import io.a2a.spec.SendMessageResponse;
1521
import io.a2a.spec.Part;
1622
import io.a2a.spec.TextPart;
1723

@@ -28,7 +34,7 @@ public class HelloWorldClient {
2834
public static void main(String[] args) {
2935
try {
3036
AgentCard finalAgentCard = null;
31-
AgentCard publicAgentCard = A2A.getAgentCard("http://localhost:9999");
37+
AgentCard publicAgentCard = new A2ACardResolver("http://localhost:9999").getAgentCard();
3238
System.out.println("Successfully fetched public agent card:");
3339
System.out.println(OBJECT_MAPPER.writeValueAsString(publicAgentCard));
3440
System.out.println("Using public agent card for client initialization (default).");
@@ -47,23 +53,47 @@ public static void main(String[] args) {
4753
System.out.println("Public card does not indicate support for an extended card. Using public card.");
4854
}
4955

50-
ClientFactory clientFactory = new ClientFactory();
51-
JSONRPCTransport client = new JSONRPCTransport(finalAgentCard);
52-
Message message = A2A.toUserMessage(MESSAGE_TEXT); // the message ID will be automatically generated for you
53-
MessageSendParams params = new MessageSendParams.Builder()
54-
.message(message)
55-
.build();
56-
EventKind result = client.sendMessage(params, null);
57-
if (result instanceof Message responseMessage) {
58-
StringBuilder textBuilder = new StringBuilder();
59-
if (responseMessage.getParts() != null) {
60-
for (Part<?> part : responseMessage.getParts()) {
61-
if (part instanceof TextPart textPart) {
62-
textBuilder.append(textPart.getText());
56+
final CompletableFuture<String> messageResponse = new CompletableFuture<>();
57+
58+
// Create consumers list for handling client events
59+
List<BiConsumer<ClientEvent, AgentCard>> consumers = new ArrayList<>();
60+
consumers.add((event, agentCard) -> {
61+
if (event instanceof MessageEvent messageEvent) {
62+
Message responseMessage = messageEvent.getMessage();
63+
StringBuilder textBuilder = new StringBuilder();
64+
if (responseMessage.getParts() != null) {
65+
for (Part<?> part : responseMessage.getParts()) {
66+
if (part instanceof TextPart textPart) {
67+
textBuilder.append(textPart.getText());
68+
}
6369
}
6470
}
71+
messageResponse.complete(textBuilder.toString());
72+
} else {
73+
System.out.println("Received client event: " + event.getClass().getSimpleName());
6574
}
66-
System.out.println("Response: " + textBuilder.toString());
75+
});
76+
77+
// Create error handler for streaming errors
78+
Consumer<Throwable> streamingErrorHandler = (error) -> {
79+
System.err.println("Streaming error occurred: " + error.getMessage());
80+
error.printStackTrace();
81+
messageResponse.completeExceptionally(error);
82+
};
83+
84+
ClientFactory clientFactory = new ClientFactory(new ClientConfig.Builder().build());
85+
Client client = clientFactory.create(finalAgentCard, consumers, streamingErrorHandler);
86+
Message message = A2A.toUserMessage(MESSAGE_TEXT); // the message ID will be automatically generated for you
87+
88+
System.out.println("Sending message: " + MESSAGE_TEXT);
89+
client.sendMessage(message);
90+
System.out.println("Message sent successfully. Responses will be handled by the configured consumers.");
91+
92+
try {
93+
String responseText = messageResponse.get();
94+
System.out.println("Response: " + responseText);
95+
} catch (Exception e) {
96+
System.err.println("Failed to get response: " + e.getMessage());
6797
}
6898
} catch (Exception e) {
6999
System.err.println("An error occurred: " + e.getMessage());

examples/helloworld/client/src/main/java/io/a2a/examples/helloworld/HelloWorldRunner.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
///usr/bin/env jbang "$0" "$@" ; exit $?
22
//DEPS io.github.a2asdk:a2a-java-sdk-client:0.3.0.Beta1-SNAPSHOT
3+
//DEPS io.github.a2asdk:a2a-java-sdk-client-transport-jsonrpc:0.3.0.Beta1-SNAPSHOT
34
//SOURCES HelloWorldClient.java
45

56
/**

examples/helloworld/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
<type>pom</type>
2727
<scope>import</scope>
2828
</dependency>
29+
<dependency>
30+
<groupId>io.github.a2asdk</groupId>
31+
<artifactId>a2a-java-sdk-client</artifactId>
32+
<version>${project.version}</version>
33+
</dependency>
2934
<dependency>
3035
<groupId>io.github.a2asdk</groupId>
3136
<artifactId>a2a-java-sdk-client-transport-jsonrpc</artifactId>

0 commit comments

Comments
 (0)