forked from google/adk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA2AAgent.java
More file actions
86 lines (76 loc) · 3.33 KB
/
A2AAgent.java
File metadata and controls
86 lines (76 loc) · 3.33 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
package com.example.a2a_basic;
import com.google.adk.a2a.agent.RemoteA2AAgent;
import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.LlmAgent;
import com.google.adk.tools.FunctionTool;
import com.google.adk.tools.ToolContext;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.a2a.client.Client;
import io.a2a.client.config.ClientConfig;
import io.a2a.client.http.A2ACardResolver;
import io.a2a.client.http.JdkA2AHttpClient;
import io.a2a.client.transport.jsonrpc.JSONRPCTransport;
import io.a2a.client.transport.jsonrpc.JSONRPCTransportConfig;
import io.a2a.spec.AgentCard;
import java.util.ArrayList;
import java.util.Random;
/** Provides local roll logic plus a remote A2A agent for the demo. */
public final class A2AAgent {
private static final Random RANDOM = new Random();
@SuppressWarnings("unchecked")
public static ImmutableMap<String, Object> rollDie(int sides, ToolContext toolContext) {
ArrayList<Integer> rolls =
(ArrayList<Integer>) toolContext.state().computeIfAbsent("rolls", k -> new ArrayList<>());
int result = RANDOM.nextInt(Math.max(sides, 1)) + 1;
rolls.add(result);
return ImmutableMap.of("result", result);
}
public static final LlmAgent ROLL_AGENT =
LlmAgent.builder()
.name("roll_agent")
.model("gemini-2.0-flash")
.description("Handles rolling dice of different sizes.")
.instruction(
"""
When asked to roll a die, always call the roll_die tool with the requested number of
sides (default to 6 if unspecified). Do not fabricate results.
""")
.tools(ImmutableList.of(FunctionTool.create(A2AAgent.class, "rollDie")))
.build();
public static LlmAgent createRootAgent(String primeAgentBaseUrl) {
BaseAgent primeAgent = createRemoteAgent(primeAgentBaseUrl);
return LlmAgent.builder()
.name("root_agent")
.model("gemini-2.0-flash")
.instruction(
"""
You can roll dice locally and delegate prime-checking to the remote prime_agent.
1. When the user asks to roll a die, route the request to roll_agent.
2. When the user asks to check primes, delegate to prime_agent.
3. If the user asks to roll and then check, roll_agent first, then prime_agent with the result.
Always recap the die result before discussing primality.
""")
.subAgents(ImmutableList.of(ROLL_AGENT, primeAgent))
.build();
}
private static BaseAgent createRemoteAgent(String primeAgentBaseUrl) {
String agentCardUrl = primeAgentBaseUrl + "/.well-known/agent-card.json";
AgentCard publicAgentCard =
new A2ACardResolver(new JdkA2AHttpClient(), primeAgentBaseUrl, agentCardUrl).getAgentCard();
Client a2aClient =
Client.builder(publicAgentCard)
.withTransport(JSONRPCTransport.class, new JSONRPCTransportConfig())
.clientConfig(
new ClientConfig.Builder()
.setStreaming(publicAgentCard.capabilities().streaming())
.build())
.build();
return RemoteA2AAgent.builder()
.name(publicAgentCard.name())
.a2aClient(a2aClient)
.agentCard(publicAgentCard)
.build();
}
private A2AAgent() {}
}