Skip to content

Commit 97a59f4

Browse files
google-genai-botcopybara-github
authored andcommitted
refactor: PluginManager is used as a Plugin interface where possible
InvocationContext -> PluginManager causes some interesting circular dependencies. Next step should probably be an interface for InvocationContext that can be used by Plugin. PiperOrigin-RevId: 852964429
1 parent 9e98611 commit 97a59f4

File tree

9 files changed

+338
-232
lines changed

9 files changed

+338
-232
lines changed

core/src/main/java/com/google/adk/agents/BaseAgent.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import com.google.adk.agents.Callbacks.AfterAgentCallback;
2323
import com.google.adk.agents.Callbacks.BeforeAgentCallback;
2424
import com.google.adk.events.Event;
25-
import com.google.adk.plugins.PluginManager;
25+
import com.google.adk.plugins.Plugin;
2626
import com.google.common.collect.ImmutableList;
2727
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2828
import com.google.errorprone.annotations.DoNotCall;
@@ -255,9 +255,9 @@ public Flowable<Event> runAsync(InvocationContext parentContext) {
255255
* @return callback functions.
256256
*/
257257
private ImmutableList<Function<CallbackContext, Maybe<Content>>> beforeCallbacksToFunctions(
258-
PluginManager pluginManager, List<? extends BeforeAgentCallback> callbacks) {
258+
Plugin pluginManager, List<? extends BeforeAgentCallback> callbacks) {
259259
return Stream.concat(
260-
Stream.of(ctx -> pluginManager.runBeforeAgentCallback(this, ctx)),
260+
Stream.of(ctx -> pluginManager.beforeAgentCallback(this, ctx)),
261261
callbacks.stream()
262262
.map(callback -> (Function<CallbackContext, Maybe<Content>>) callback::call))
263263
.collect(toImmutableList());
@@ -270,9 +270,9 @@ private ImmutableList<Function<CallbackContext, Maybe<Content>>> beforeCallbacks
270270
* @return callback functions.
271271
*/
272272
private ImmutableList<Function<CallbackContext, Maybe<Content>>> afterCallbacksToFunctions(
273-
PluginManager pluginManager, List<? extends AfterAgentCallback> callbacks) {
273+
Plugin pluginManager, List<? extends AfterAgentCallback> callbacks) {
274274
return Stream.concat(
275-
Stream.of(ctx -> pluginManager.runAfterAgentCallback(this, ctx)),
275+
Stream.of(ctx -> pluginManager.afterAgentCallback(this, ctx)),
276276
callbacks.stream()
277277
.map(callback -> (Function<CallbackContext, Maybe<Content>>) callback::call))
278278
.collect(toImmutableList());

core/src/main/java/com/google/adk/agents/InvocationContext.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.adk.flows.llmflows.ResumabilityConfig;
2222
import com.google.adk.memory.BaseMemoryService;
2323
import com.google.adk.models.LlmCallsLimitExceededException;
24+
import com.google.adk.plugins.Plugin;
2425
import com.google.adk.plugins.PluginManager;
2526
import com.google.adk.sessions.BaseSessionService;
2627
import com.google.adk.sessions.Session;
@@ -42,7 +43,7 @@ public class InvocationContext {
4243
private final BaseSessionService sessionService;
4344
private final BaseArtifactService artifactService;
4445
private final BaseMemoryService memoryService;
45-
private final PluginManager pluginManager;
46+
private final Plugin pluginManager;
4647
private final Optional<LiveRequestQueue> liveRequestQueue;
4748
private final Map<String, ActiveStreamingTool> activeStreamingTools = new ConcurrentHashMap<>();
4849
private final String invocationId;
@@ -80,7 +81,7 @@ public InvocationContext(
8081
BaseSessionService sessionService,
8182
BaseArtifactService artifactService,
8283
BaseMemoryService memoryService,
83-
PluginManager pluginManager,
84+
Plugin pluginManager,
8485
Optional<LiveRequestQueue> liveRequestQueue,
8586
Optional<String> branch,
8687
String invocationId,
@@ -235,7 +236,7 @@ public BaseMemoryService memoryService() {
235236
}
236237

237238
/** Returns the plugin manager for accessing tools and plugins. */
238-
public PluginManager pluginManager() {
239+
public Plugin pluginManager() {
239240
return pluginManager;
240241
}
241242

@@ -376,7 +377,7 @@ public static class Builder {
376377
private BaseSessionService sessionService;
377378
private BaseArtifactService artifactService;
378379
private BaseMemoryService memoryService;
379-
private PluginManager pluginManager = new PluginManager();
380+
private Plugin pluginManager = new PluginManager();
380381
private Optional<LiveRequestQueue> liveRequestQueue = Optional.empty();
381382
private Optional<String> branch = Optional.empty();
382383
private String invocationId = newInvocationContextId();
@@ -430,7 +431,7 @@ public Builder memoryService(BaseMemoryService memoryService) {
430431
* @return this builder instance for chaining.
431432
*/
432433
@CanIgnoreReturnValue
433-
public Builder pluginManager(PluginManager pluginManager) {
434+
public Builder pluginManager(Plugin pluginManager) {
434435
this.pluginManager = pluginManager;
435436
return this;
436437
}

core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private Flowable<LlmResponse> callLlm(
200200
exception ->
201201
context
202202
.pluginManager()
203-
.runOnModelErrorCallback(
203+
.onModelErrorCallback(
204204
new CallbackContext(
205205
context, eventForCallbackUsage.actions()),
206206
llmRequestBuilder,
@@ -244,7 +244,7 @@ private Single<Optional<LlmResponse>> handleBeforeModelCallback(
244244
CallbackContext callbackContext = new CallbackContext(context, callbackEvent.actions());
245245

246246
Maybe<LlmResponse> pluginResult =
247-
context.pluginManager().runBeforeModelCallback(callbackContext, llmRequestBuilder);
247+
context.pluginManager().beforeModelCallback(callbackContext, llmRequestBuilder);
248248

249249
LlmAgent agent = (LlmAgent) context.agent();
250250

@@ -280,7 +280,7 @@ private Single<LlmResponse> handleAfterModelCallback(
280280
CallbackContext callbackContext = new CallbackContext(context, callbackEvent.actions());
281281

282282
Maybe<LlmResponse> pluginResult =
283-
context.pluginManager().runAfterModelCallback(callbackContext, llmResponse);
283+
context.pluginManager().afterModelCallback(callbackContext, llmResponse);
284284

285285
LlmAgent agent = (LlmAgent) context.agent();
286286
Optional<List<? extends AfterModelCallback>> callbacksOpt = agent.afterModelCallback();

core/src/main/java/com/google/adk/flows/llmflows/Functions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ private static Maybe<Event> postProcessFunctionResult(
390390
t ->
391391
invocationContext
392392
.pluginManager()
393-
.runOnToolErrorCallback(tool, functionArgs, toolContext, t)
393+
.onToolErrorCallback(tool, functionArgs, toolContext, t)
394394
.map(isLive ? Optional::ofNullable : Optional::of)
395395
.switchIfEmpty(Single.error(t)))
396396
.flatMapMaybe(
@@ -462,7 +462,7 @@ private static Maybe<Map<String, Object>> maybeInvokeBeforeToolCall(
462462
LlmAgent agent = (LlmAgent) invocationContext.agent();
463463

464464
Maybe<Map<String, Object>> pluginResult =
465-
invocationContext.pluginManager().runBeforeToolCallback(tool, functionArgs, toolContext);
465+
invocationContext.pluginManager().beforeToolCallback(tool, functionArgs, toolContext);
466466

467467
Optional<List<? extends BeforeToolCallback>> callbacksOpt = agent.beforeToolCallback();
468468
if (callbacksOpt.isEmpty() || callbacksOpt.get().isEmpty()) {
@@ -496,7 +496,7 @@ private static Maybe<Map<String, Object>> maybeInvokeAfterToolCall(
496496
Maybe<Map<String, Object>> pluginResult =
497497
invocationContext
498498
.pluginManager()
499-
.runAfterToolCallback(tool, functionArgs, toolContext, functionResult);
499+
.afterToolCallback(tool, functionArgs, toolContext, functionResult);
500500

501501
Optional<List<? extends AfterToolCallback>> callbacksOpt = agent.afterToolCallback();
502502
if (callbacksOpt.isEmpty() || callbacksOpt.get().isEmpty()) {

core/src/main/java/com/google/adk/plugins/BasePlugin.java

Lines changed: 2 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@
1515
*/
1616
package com.google.adk.plugins;
1717

18-
import com.google.adk.agents.BaseAgent;
19-
import com.google.adk.agents.CallbackContext;
20-
import com.google.adk.agents.InvocationContext;
21-
import com.google.adk.events.Event;
22-
import com.google.adk.models.LlmRequest;
23-
import com.google.adk.models.LlmResponse;
24-
import com.google.adk.tools.BaseTool;
25-
import com.google.adk.tools.ToolContext;
26-
import com.google.genai.types.Content;
27-
import io.reactivex.rxjava3.core.Completable;
28-
import io.reactivex.rxjava3.core.Maybe;
29-
import java.util.Map;
30-
3118
/**
3219
* Base class for creating plugins.
3320
*
@@ -40,168 +27,15 @@
4027
* <p>A plugin can implement one or more methods of callbacks, but should not implement the same
4128
* method of callback for multiple times.
4229
*/
43-
public abstract class BasePlugin {
30+
public abstract class BasePlugin implements Plugin {
4431
protected final String name;
4532

4633
public BasePlugin(String name) {
4734
this.name = name;
4835
}
4936

37+
@Override
5038
public String getName() {
5139
return name;
5240
}
53-
54-
/**
55-
* Callback executed when a user message is received before an invocation starts.
56-
*
57-
* @param invocationContext The context for the entire invocation.
58-
* @param userMessage The message content input by user.
59-
* @return An optional Content to replace the user message. Returning Empty to proceed normally.
60-
*/
61-
public Maybe<Content> onUserMessageCallback(
62-
InvocationContext invocationContext, Content userMessage) {
63-
return Maybe.empty();
64-
}
65-
66-
/**
67-
* Callback executed before the ADK runner runs.
68-
*
69-
* @param invocationContext The context for the entire invocation.
70-
* @return An optional Content to halt execution. Returning Empty to proceed normally.
71-
*/
72-
public Maybe<Content> beforeRunCallback(InvocationContext invocationContext) {
73-
return Maybe.empty();
74-
}
75-
76-
/**
77-
* Callback executed after an event is yielded from runner.
78-
*
79-
* @param invocationContext The context for the entire invocation.
80-
* @param event The event raised by the runner.
81-
* @return An optional Event to modify or replace the response. Returning Empty to proceed
82-
* normally.
83-
*/
84-
public Maybe<Event> onEventCallback(InvocationContext invocationContext, Event event) {
85-
return Maybe.empty();
86-
}
87-
88-
/**
89-
* Callback executed after an ADK runner run has completed.
90-
*
91-
* @param invocationContext The context for the entire invocation.
92-
*/
93-
public Completable afterRunCallback(InvocationContext invocationContext) {
94-
return Completable.complete();
95-
}
96-
97-
/**
98-
* Callback executed before an agent's primary logic is invoked.
99-
*
100-
* @param agent The agent that is about to run.
101-
* @param callbackContext The context for the agent invocation.
102-
* @return An optional Content object to bypass the agent's execution. Returning Empty to proceed
103-
* normally.
104-
*/
105-
public Maybe<Content> beforeAgentCallback(BaseAgent agent, CallbackContext callbackContext) {
106-
return Maybe.empty();
107-
}
108-
109-
/**
110-
* Callback executed after an agent's primary logic has completed.
111-
*
112-
* @param agent The agent that has just run.
113-
* @param callbackContext The context for the agent invocation.
114-
* @return An optional Content object to replace the agent's original result. Returning Empty to
115-
* use the original result.
116-
*/
117-
public Maybe<Content> afterAgentCallback(BaseAgent agent, CallbackContext callbackContext) {
118-
return Maybe.empty();
119-
}
120-
121-
/**
122-
* Callback executed before a request is sent to the model.
123-
*
124-
* @param callbackContext The context for the current agent call.
125-
* @param llmRequest The mutable request builder, allowing modification of the request before it
126-
* is sent to the model.
127-
* @return An optional LlmResponse to trigger an early exit. Returning Empty to proceed normally.
128-
*/
129-
public Maybe<LlmResponse> beforeModelCallback(
130-
CallbackContext callbackContext, LlmRequest.Builder llmRequest) {
131-
return Maybe.empty();
132-
}
133-
134-
/**
135-
* Callback executed after a response is received from the model.
136-
*
137-
* @param callbackContext The context for the current agent call.
138-
* @param llmResponse The response object received from the model.
139-
* @return An optional LlmResponse to modify or replace the response. Returning Empty to use the
140-
* original response.
141-
*/
142-
public Maybe<LlmResponse> afterModelCallback(
143-
CallbackContext callbackContext, LlmResponse llmResponse) {
144-
return Maybe.empty();
145-
}
146-
147-
/**
148-
* Callback executed when a model call encounters an error.
149-
*
150-
* @param callbackContext The context for the current agent call.
151-
* @param llmRequest The mutable request builder for the request that failed.
152-
* @param error The exception that was raised.
153-
* @return An optional LlmResponse to use instead of propagating the error. Returning Empty to
154-
* allow the original error to be raised.
155-
*/
156-
public Maybe<LlmResponse> onModelErrorCallback(
157-
CallbackContext callbackContext, LlmRequest.Builder llmRequest, Throwable error) {
158-
return Maybe.empty();
159-
}
160-
161-
/**
162-
* Callback executed before a tool is called.
163-
*
164-
* @param tool The tool instance that is about to be executed.
165-
* @param toolArgs The dictionary of arguments to be used for invoking the tool.
166-
* @param toolContext The context specific to the tool execution.
167-
* @return An optional Map to stop the tool execution and return this response immediately.
168-
* Returning Empty to proceed normally.
169-
*/
170-
public Maybe<Map<String, Object>> beforeToolCallback(
171-
BaseTool tool, Map<String, Object> toolArgs, ToolContext toolContext) {
172-
return Maybe.empty();
173-
}
174-
175-
/**
176-
* Callback executed after a tool has been called.
177-
*
178-
* @param tool The tool instance that has just been executed.
179-
* @param toolArgs The original arguments that were passed to the tool.
180-
* @param toolContext The context specific to the tool execution.
181-
* @param result The dictionary returned by the tool invocation.
182-
* @return An optional Map to replace the original result from the tool. Returning Empty to use
183-
* the original result.
184-
*/
185-
public Maybe<Map<String, Object>> afterToolCallback(
186-
BaseTool tool,
187-
Map<String, Object> toolArgs,
188-
ToolContext toolContext,
189-
Map<String, Object> result) {
190-
return Maybe.empty();
191-
}
192-
193-
/**
194-
* Callback executed when a tool call encounters an error.
195-
*
196-
* @param tool The tool instance that encountered an error.
197-
* @param toolArgs The arguments that were passed to the tool.
198-
* @param toolContext The context specific to the tool execution.
199-
* @param error The exception that was raised during tool execution.
200-
* @return An optional Map to be used as the tool response instead of propagating the error.
201-
* Returning Empty to allow the original error to be raised.
202-
*/
203-
public Maybe<Map<String, Object>> onToolErrorCallback(
204-
BaseTool tool, Map<String, Object> toolArgs, ToolContext toolContext, Throwable error) {
205-
return Maybe.empty();
206-
}
20741
}

0 commit comments

Comments
 (0)