Skip to content

Commit 4fcb1cc

Browse files
Jacksunweicopybara-github
authored andcommitted
refactor: Renames AgentProvider to AgentLoader and renames getAgent to loadAgent
This is consistent with the interface in python. https://github.com/google/adk-python/blob/main/src/google/adk/cli/utils/agent_loader.py PiperOrigin-RevId: 793964389
1 parent 85b06da commit 4fcb1cc

File tree

6 files changed

+122
-92
lines changed

6 files changed

+122
-92
lines changed

maven_plugin/README.md

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ This Maven plugin provides a convenient way to start the ADK Web Server with you
77
### Basic Usage
88

99
```bash
10-
mvn google-adk:web -Dagents=com.example.MyAgentProvider.INSTANCE
10+
mvn google-adk:web -Dagents=com.example.MyAgentLoader.INSTANCE
1111
```
1212

1313
### Parameters
1414

15-
- **`agents`** (required): Full class path to your AgentProvider implementation
15+
- **`agents`** (required): Full class path to your AgentLoader implementation
1616
- **`port`** (optional, default: 8000): Port for the web server
1717
- **`host`** (optional, default: localhost): Host address to bind to
1818
- **`hotReloading`** (optional, default: true): Whether to enable hot reloading
@@ -21,36 +21,43 @@ mvn google-adk:web -Dagents=com.example.MyAgentProvider.INSTANCE
2121

2222
```bash
2323
mvn google-adk:web \
24-
-Dagents=com.example.MyAgentProvider.INSTANCE \
24+
-Dagents=com.example.MyAgentLoader.INSTANCE \
2525
-Dhost=0.0.0.0 \
2626
-Dport=9090 \
2727
-DhotReloading=false
2828
```
2929

30-
## Creating an AgentProvider
30+
## Creating an AgentLoader
3131

32-
### 1. Implement the AgentProvider Interface
32+
### 1. Implement the AgentLoader Interface
3333

34-
Create a class that implements `com.google.adk.maven.AgentProvider`:
34+
Create a class that implements `com.google.adk.maven.AgentLoader`:
3535

3636
```java
3737
package com.example;
3838

3939
import com.google.adk.agents.BaseAgent;
4040
import com.google.adk.agents.LlmAgent;
41-
import com.google.adk.maven.AgentProvider;
42-
import java.util.Map;
41+
import com.google.adk.maven.AgentLoader;
42+
import com.google.common.collect.ImmutableList;
43+
import java.util.NoSuchElementException;
4344

44-
public class MyAgentProvider implements AgentProvider {
45+
public class MyAgentLoader implements AgentLoader {
4546

46-
public static final MyAgentProvider INSTANCE = new MyAgentProvider();
47+
public static final MyAgentLoader INSTANCE = new MyAgentLoader();
4748

4849
@Override
49-
public Map<String, BaseAgent> getAgents() {
50-
return Map.of(
51-
"chat_bot", createChatBot(),
52-
"code_assistant", createCodeAssistant()
53-
);
50+
public ImmutableList<String> listAgents() {
51+
return ImmutableList.of("chat_bot", "code_assistant");
52+
}
53+
54+
@Override
55+
public BaseAgent loadAgent(String name) {
56+
switch (name) {
57+
case "chat_bot": return createChatBot();
58+
case "code_assistant": return createCodeAssistant();
59+
default: throw new NoSuchElementException("Agent not found: " + name);
60+
}
5461
}
5562

5663
private BaseAgent createChatBot() {
@@ -92,19 +99,27 @@ Add the plugin to your `pom.xml`:
9299
### 3. Run the Web Server
93100

94101
```bash
95-
mvn google-adk:web -Dagents=com.example.MyAgentProvider.INSTANCE
102+
mvn google-adk:web -Dagents=com.example.MyAgentLoader.INSTANCE
96103
```
97104

98-
## Alternative AgentProvider Patterns
105+
## Alternative AgentLoader Patterns
99106

100107
### Using Default Constructor
101108

102109
```java
103-
public class SimpleAgentProvider implements AgentProvider {
110+
public class SimpleAgentLoader implements AgentLoader {
104111
// No static field needed, uses default constructor
105112
@Override
106-
public Map<String, BaseAgent> getAgents() {
107-
return Map.of("simple_agent", createSimpleAgent());
113+
public ImmutableList<String> listAgents() {
114+
return ImmutableList.of("simple_agent");
115+
}
116+
117+
@Override
118+
public BaseAgent loadAgent(String name) {
119+
if ("simple_agent".equals(name)) {
120+
return createSimpleAgent();
121+
}
122+
throw new NoSuchElementException("Agent not found: " + name);
108123
}
109124

110125
private BaseAgent createSimpleAgent() {
@@ -119,28 +134,39 @@ public class SimpleAgentProvider implements AgentProvider {
119134
```
120135

121136
Usage:
137+
122138
```bash
123-
mvn google-adk:web -Dagents=com.example.SimpleAgentProvider
139+
mvn google-adk:web -Dagents=com.example.SimpleAgentLoader
124140
```
125141

126142
### Using Other Static Fields
127143

128144
```java
129-
public class MultipleProviders implements AgentProvider {
130-
public static final MultipleProviders DEFAULT = new MultipleProviders();
131-
public static final MultipleProviders ADVANCED = new MultipleProviders(true);
145+
public class MultipleLoaders implements AgentLoader {
146+
public static final MultipleLoaders DEFAULT = new MultipleLoaders();
147+
public static final MultipleLoaders ADVANCED = new MultipleLoaders(true);
132148

133149
private final boolean advanced;
134150

135-
public MultipleProviders() { this(false); }
136-
public MultipleProviders(boolean advanced) { this.advanced = advanced; }
151+
public MultipleLoaders() { this(false); }
152+
public MultipleLoaders(boolean advanced) { this.advanced = advanced; }
137153

138154
@Override
139-
public Map<String, BaseAgent> getAgents() {
155+
public ImmutableList<String> listAgents() {
140156
if (advanced) {
141-
return Map.of("advanced_agent", createAdvancedAgent());
157+
return ImmutableList.of("advanced_agent");
158+
}
159+
return ImmutableList.of("basic_agent");
160+
}
161+
162+
@Override
163+
public BaseAgent loadAgent(String name) {
164+
if (advanced && "advanced_agent".equals(name)) {
165+
return createAdvancedAgent();
166+
} else if (!advanced && "basic_agent".equals(name)) {
167+
return createBasicAgent();
142168
}
143-
return Map.of("basic_agent", createBasicAgent());
169+
throw new NoSuchElementException("Agent not found: " + name);
144170
}
145171

146172
private BaseAgent createBasicAgent() {
@@ -164,8 +190,9 @@ public class MultipleProviders implements AgentProvider {
164190
```
165191

166192
Usage:
193+
167194
```bash
168-
mvn google-adk:web -Dagents=com.example.MultipleProviders.ADVANCED
195+
mvn google-adk:web -Dagents=com.example.MultipleLoaders.ADVANCED
169196
```
170197

171198
## Web UI
@@ -194,7 +221,7 @@ Make sure your project has the necessary ADK dependencies:
194221
<version>0.2.1-SNAPSHOT</version>
195222
</dependency>
196223

197-
<!-- Maven plugin dependency for AgentProvider interface -->
224+
<!-- Maven plugin dependency for AgentLoader interface -->
198225
<dependency>
199226
<groupId>com.google.adk</groupId>
200227
<artifactId>google-adk-maven-plugin</artifactId>
@@ -223,15 +250,17 @@ Add the plugin to your `pom.xml` for convenience:
223250
```
224251

225252
Then use the short command:
253+
226254
```bash
227-
mvn google-adk:web -Dagents=com.example.MyAgentProvider.INSTANCE
255+
mvn google-adk:web -Dagents=com.example.MyAgentLoader.INSTANCE
228256
```
229257

230258
**Option 2: Without plugin declaration (no pom.xml changes)**
231259

232260
Use the full coordinates directly:
261+
233262
```bash
234-
mvn com.google.adk:google-adk-maven-plugin:web -Dagents=com.example.MyAgentProvider.INSTANCE
263+
mvn com.google.adk:google-adk-maven-plugin:web -Dagents=com.example.MyAgentLoader.INSTANCE
235264
```
236265

237266
## Stopping the Server

maven_plugin/examples/simple-agent/README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ This example demonstrates how to create and run custom ADK agents using the Mave
44

55
## What's Included
66

7-
- **SimpleAgentProvider**: An implementation of `AgentProvider` that creates three example agents:
7+
- **SimpleAgentLoader**: An implementation of `AgentLoader` that creates three example agents:
88
- `chat_assistant`: A friendly general-purpose assistant
99
- `search_agent`: An agent with Google Search capabilities
1010
- `code_helper`: A coding assistant
1111

1212
## How to Run
1313

1414
1. **Compile the project:**
15+
1516
```bash
1617
mvn compile
1718
```
1819

1920
2. **Start the ADK Web Server:**
21+
2022
```bash
21-
mvn google-adk:web -Dagents=com.example.SimpleAgentProvider.INSTANCE
23+
mvn google-adk:web -Dagents=com.example.SimpleAgentLoader.INSTANCE
2224
```
2325

2426
3. **Open your browser:**
@@ -31,7 +33,7 @@ This example demonstrates how to create and run custom ADK agents using the Mave
3133

3234
## Customizing
3335

34-
You can modify `SimpleAgentProvider.java` to:
36+
You can modify `SimpleAgentLoader.java` to:
3537
- Add more agents
3638
- Change agent instructions
3739
- Add different tools
@@ -41,6 +43,7 @@ You can modify `SimpleAgentProvider.java` to:
4143
Example modifications:
4244

4345
### Add a New Agent
46+
4447
```java
4548
private BaseAgent createMathTutor() {
4649
return LlmAgent.builder()
@@ -54,7 +57,8 @@ private BaseAgent createMathTutor() {
5457
}
5558
```
5659

57-
Then add it to the `listAgents()` method and `getAgent()` switch statement:
60+
Then add it to the `listAgents()` method and `loadAgent()` switch statement:
61+
5862
```java
5963
@Override
6064
@Nonnull
@@ -63,7 +67,7 @@ public ImmutableList<String> listAgents() {
6367
}
6468

6569
@Override
66-
public BaseAgent getAgent(String name) {
70+
public BaseAgent loadAgent(String name) {
6771
switch (name) {
6872
case "chat_assistant":
6973
return createChatAssistant();
@@ -80,8 +84,9 @@ public BaseAgent getAgent(String name) {
8084
```
8185

8286
### Configure Different Ports
87+
8388
```bash
84-
mvn google-adk:web -Dagents=com.example.SimpleAgentProvider.INSTANCE -Dport=9090
89+
mvn google-adk:web -Dagents=com.example.SimpleAgentLoader.INSTANCE -Dport=9090
8590
```
8691

8792
## Next Steps

maven_plugin/examples/simple-agent/src/main/java/com/example/SimpleAgentProvider.java renamed to maven_plugin/examples/simple-agent/src/main/java/com/example/SimpleAgentLoader.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import com.google.adk.agents.BaseAgent;
2020
import com.google.adk.agents.LlmAgent;
21-
import com.google.adk.maven.AgentProvider;
21+
import com.google.adk.maven.AgentLoader;
2222
import com.google.adk.tools.GoogleSearchTool;
2323
import com.google.common.base.Suppliers;
2424
import com.google.common.collect.ImmutableList;
@@ -28,15 +28,15 @@
2828
import javax.annotation.Nonnull;
2929
import javax.annotation.concurrent.ThreadSafe;
3030

31-
/** Example AgentProvider that creates simple agents for demonstration. */
31+
/** Example AgentLoader that creates simple agents for demonstration. */
3232
@ThreadSafe
33-
public class SimpleAgentProvider implements AgentProvider {
33+
public class SimpleAgentLoader implements AgentLoader {
3434

35-
public static final SimpleAgentProvider INSTANCE = new SimpleAgentProvider();
35+
public static final SimpleAgentLoader INSTANCE = new SimpleAgentLoader();
3636

3737
private final ImmutableMap<String, Supplier<BaseAgent>> agentSuppliers;
3838

39-
public SimpleAgentProvider() {
39+
public SimpleAgentLoader() {
4040
this.agentSuppliers =
4141
ImmutableMap.of(
4242
"chat_assistant", Suppliers.memoize(this::createChatAssistant),
@@ -51,7 +51,7 @@ public ImmutableList<String> listAgents() {
5151
}
5252

5353
@Override
54-
public BaseAgent getAgent(String name) {
54+
public BaseAgent loadAgent(String name) {
5555
Supplier<BaseAgent> supplier = agentSuppliers.get(name);
5656
if (supplier == null) {
5757
throw new NoSuchElementException("Agent not found: " + name);

maven_plugin/src/main/java/com/google/adk/maven/AgentProvider.java renamed to maven_plugin/src/main/java/com/google/adk/maven/AgentLoader.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import javax.annotation.concurrent.ThreadSafe;
2121

2222
/**
23-
* Interface for providing agents to the ADK Web Server.
23+
* Interface for loading agents to the ADK Web Server.
2424
*
2525
* <p>Users implement this interface to register their agents with ADK Web Server.
2626
*
@@ -30,14 +30,14 @@
3030
* <p>Example usage:
3131
*
3232
* <pre>{@code
33-
* public class MyAgentProvider implements AgentProvider {
33+
* public class MyAgentLoader implements AgentLoader {
3434
* @Override
3535
* public ImmutableList<String> listAgents() {
3636
* return ImmutableList.of("chat_bot", "code_assistant");
3737
* }
3838
*
3939
* @Override
40-
* public BaseAgent getAgent(String name) {
40+
* public BaseAgent loadAgent(String name) {
4141
* switch (name) {
4242
* case "chat_bot": return createChatBot();
4343
* case "code_assistant": return createCodeAssistant();
@@ -50,13 +50,13 @@
5050
* <p>Then use with Maven plugin:
5151
*
5252
* <pre>{@code
53-
* mvn google-adk:web -Dagents=com.acme.MyAgentProvider
53+
* mvn google-adk:web -Dagents=com.acme.MyAgentLoader
5454
* }</pre>
5555
*
5656
* TODO: Add config-based agent registration in the future.
5757
*/
5858
@ThreadSafe
59-
public interface AgentProvider {
59+
public interface AgentLoader {
6060

6161
/**
6262
* Returns a list of available agent names.
@@ -68,12 +68,12 @@ public interface AgentProvider {
6868
ImmutableList<String> listAgents();
6969

7070
/**
71-
* Returns the BaseAgent instance for the specified agent name.
71+
* Loads the BaseAgent instance for the specified agent name.
7272
*
73-
* @param name the name of the agent to retrieve
73+
* @param name the name of the agent to load
7474
* @return BaseAgent instance for the given name
7575
* @throws java.util.NoSuchElementException if the agent doesn't exist
7676
* @throws IllegalStateException if the agent exists but fails to load
7777
*/
78-
BaseAgent getAgent(String name);
78+
BaseAgent loadAgent(String name);
7979
}

0 commit comments

Comments
 (0)