Skip to content

Commit c0d7d5e

Browse files
committed
Add compat client modules
1 parent c421418 commit c0d7d5e

72 files changed

Lines changed: 8619 additions & 3 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compat-0.3/client/base/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependencies>
2121
<dependency>
2222
<groupId>${project.groupId}</groupId>
23-
<artifactId>a2a-java-sdk-http-client</artifactId>
23+
<artifactId>a2a-java-sdk-compat-0.3-http-client</artifactId>
2424
</dependency>
2525
<dependency>
2626
<groupId>${project.groupId}</groupId>
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package org.a2aproject.sdk.compat03;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.UUID;
7+
8+
import org.a2aproject.sdk.compat03.client.http.A2ACardResolver;
9+
import org.a2aproject.sdk.compat03.client.http.A2AHttpClient;
10+
import org.a2aproject.sdk.compat03.client.http.JdkA2AHttpClient;
11+
import org.a2aproject.sdk.compat03.spec.A2AClientError;
12+
import org.a2aproject.sdk.compat03.spec.A2AClientJSONError;
13+
import org.a2aproject.sdk.compat03.spec.AgentCard;
14+
import org.a2aproject.sdk.compat03.spec.Message;
15+
import org.a2aproject.sdk.compat03.spec.Part;
16+
import org.a2aproject.sdk.compat03.spec.TextPart;
17+
18+
19+
/**
20+
* Constants and utility methods related to the A2A protocol.
21+
*/
22+
public class A2A {
23+
24+
/**
25+
* Convert the given text to a user message.
26+
*
27+
* @param text the message text
28+
* @return the user message
29+
*/
30+
public static Message toUserMessage(String text) {
31+
return toMessage(text, Message.Role.USER, null);
32+
}
33+
34+
/**
35+
* Convert the given text to a user message.
36+
*
37+
* @param text the message text
38+
* @param messageId the message ID to use
39+
* @return the user message
40+
*/
41+
public static Message toUserMessage(String text, String messageId) {
42+
return toMessage(text, Message.Role.USER, messageId);
43+
}
44+
45+
/**
46+
* Convert the given text to an agent message.
47+
*
48+
* @param text the message text
49+
* @return the agent message
50+
*/
51+
public static Message toAgentMessage(String text) {
52+
return toMessage(text, Message.Role.AGENT, null);
53+
}
54+
55+
/**
56+
* Convert the given text to an agent message.
57+
*
58+
* @param text the message text
59+
* @param messageId the message ID to use
60+
* @return the agent message
61+
*/
62+
public static Message toAgentMessage(String text, String messageId) {
63+
return toMessage(text, Message.Role.AGENT, messageId);
64+
}
65+
66+
/**
67+
* Create a user message with text content and optional context and task IDs.
68+
*
69+
* @param text the message text (required)
70+
* @param contextId the context ID to use (optional)
71+
* @param taskId the task ID to use (optional)
72+
* @return the user message
73+
*/
74+
public static Message createUserTextMessage(String text, String contextId, String taskId) {
75+
return toMessage(text, Message.Role.USER, null, contextId, taskId);
76+
}
77+
78+
/**
79+
* Create an agent message with text content and optional context and task IDs.
80+
*
81+
* @param text the message text (required)
82+
* @param contextId the context ID to use (optional)
83+
* @param taskId the task ID to use (optional)
84+
* @return the agent message
85+
*/
86+
public static Message createAgentTextMessage(String text, String contextId, String taskId) {
87+
return toMessage(text, Message.Role.AGENT, null, contextId, taskId);
88+
}
89+
90+
/**
91+
* Create an agent message with custom parts and optional context and task IDs.
92+
*
93+
* @param parts the message parts (required)
94+
* @param contextId the context ID to use (optional)
95+
* @param taskId the task ID to use (optional)
96+
* @return the agent message
97+
*/
98+
public static Message createAgentPartsMessage(List<Part<?>> parts, String contextId, String taskId) {
99+
if (parts == null || parts.isEmpty()) {
100+
throw new IllegalArgumentException("Parts cannot be null or empty");
101+
}
102+
return toMessage(parts, Message.Role.AGENT, null, contextId, taskId);
103+
}
104+
105+
private static Message toMessage(String text, Message.Role role, String messageId) {
106+
return toMessage(text, role, messageId, null, null);
107+
}
108+
109+
private static Message toMessage(String text, Message.Role role, String messageId, String contextId, String taskId) {
110+
Message.Builder messageBuilder = new Message.Builder()
111+
.role(role)
112+
.parts(Collections.singletonList(new TextPart(text)))
113+
.contextId(contextId)
114+
.taskId(taskId);
115+
if (messageId != null) {
116+
messageBuilder.messageId(messageId);
117+
}
118+
return messageBuilder.build();
119+
}
120+
121+
private static Message toMessage(List<Part<?>> parts, Message.Role role, String messageId, String contextId, String taskId) {
122+
Message.Builder messageBuilder = new Message.Builder()
123+
.role(role)
124+
.parts(parts)
125+
.contextId(contextId)
126+
.taskId(taskId);
127+
if (messageId != null) {
128+
messageBuilder.messageId(messageId);
129+
}
130+
return messageBuilder.build();
131+
}
132+
133+
/**
134+
* Get the agent card for an A2A agent.
135+
*
136+
* @param agentUrl the base URL for the agent whose agent card we want to retrieve
137+
* @return the agent card
138+
* @throws A2AClientError If an HTTP error occurs fetching the card
139+
* @throws A2AClientJSONError If the response body cannot be decoded as JSON or validated against the AgentCard schema
140+
*/
141+
public static AgentCard getAgentCard(String agentUrl) throws A2AClientError, A2AClientJSONError {
142+
return getAgentCard(new JdkA2AHttpClient(), agentUrl);
143+
}
144+
145+
/**
146+
* Get the agent card for an A2A agent.
147+
*
148+
* @param httpClient the http client to use
149+
* @param agentUrl the base URL for the agent whose agent card we want to retrieve
150+
* @return the agent card
151+
* @throws A2AClientError If an HTTP error occurs fetching the card
152+
* @throws A2AClientJSONError If the response body cannot be decoded as JSON or validated against the AgentCard schema
153+
*/
154+
public static AgentCard getAgentCard(A2AHttpClient httpClient, String agentUrl) throws A2AClientError, A2AClientJSONError {
155+
return getAgentCard(httpClient, agentUrl, null, null);
156+
}
157+
158+
/**
159+
* Get the agent card for an A2A agent.
160+
*
161+
* @param agentUrl the base URL for the agent whose agent card we want to retrieve
162+
* @param relativeCardPath optional path to the agent card endpoint relative to the base
163+
* agent URL, defaults to ".well-known/agent-card.json"
164+
* @param authHeaders the HTTP authentication headers to use
165+
* @return the agent card
166+
* @throws A2AClientError If an HTTP error occurs fetching the card
167+
* @throws A2AClientJSONError If the response body cannot be decoded as JSON or validated against the AgentCard schema
168+
*/
169+
public static AgentCard getAgentCard(String agentUrl, String relativeCardPath, Map<String, String> authHeaders) throws A2AClientError, A2AClientJSONError {
170+
return getAgentCard(new JdkA2AHttpClient(), agentUrl, relativeCardPath, authHeaders);
171+
}
172+
173+
/**
174+
* Get the agent card for an A2A agent.
175+
*
176+
* @param httpClient the http client to use
177+
* @param agentUrl the base URL for the agent whose agent card we want to retrieve
178+
* @param relativeCardPath optional path to the agent card endpoint relative to the base
179+
* agent URL, defaults to ".well-known/agent-card.json"
180+
* @param authHeaders the HTTP authentication headers to use
181+
* @return the agent card
182+
* @throws A2AClientError If an HTTP error occurs fetching the card
183+
* @throws A2AClientJSONError If the response body cannot be decoded as JSON or validated against the AgentCard schema
184+
*/
185+
public static AgentCard getAgentCard(A2AHttpClient httpClient, String agentUrl, String relativeCardPath, Map<String, String> authHeaders) throws A2AClientError, A2AClientJSONError {
186+
A2ACardResolver resolver = new A2ACardResolver(httpClient, agentUrl, relativeCardPath, authHeaders);
187+
return resolver.getAgentCard();
188+
}
189+
}

0 commit comments

Comments
 (0)