forked from a2aproject/a2a-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA2A.java
More file actions
130 lines (116 loc) · 4.98 KB
/
A2A.java
File metadata and controls
130 lines (116 loc) · 4.98 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
package io.a2a;
import java.util.Collections;
import java.util.Map;
import io.a2a.client.A2ACardResolver;
import io.a2a.transport.jsonrpc.client.A2AHttpClient;
import io.a2a.transport.jsonrpc.client.JdkA2AHttpClient;
import io.a2a.spec.A2AClientError;
import io.a2a.spec.A2AClientJSONError;
import io.a2a.spec.AgentCard;
import io.a2a.spec.Message;
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);
}
private static Message toMessage(String text, Message.Role role, String messageId) {
Message.Builder messageBuilder = new Message.Builder()
.role(role)
.parts(Collections.singletonList(new TextPart(text)));
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(new JdkA2AHttpClient(), agentUrl);
}
/**
* Get the agent card for an A2A agent.
*
* @param httpClient the http client to use
* @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(A2AHttpClient httpClient, String agentUrl) throws A2AClientError, A2AClientJSONError {
return getAgentCard(httpClient, 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.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(new JdkA2AHttpClient(), agentUrl, relativeCardPath, authHeaders);
}
/**
* Get the agent card for an A2A agent.
*
* @param httpClient the http client to use
* @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.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(A2AHttpClient httpClient, String agentUrl, String relativeCardPath, Map<String, String> authHeaders) throws A2AClientError, A2AClientJSONError {
A2ACardResolver resolver = new A2ACardResolver(httpClient, agentUrl, relativeCardPath, authHeaders);
return resolver.getAgentCard();
}
}