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