|
15 | 15 | */ |
16 | 16 | package com.google.adk.plugins; |
17 | 17 |
|
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 | | - |
31 | 18 | /** |
32 | 19 | * Base class for creating plugins. |
33 | 20 | * |
|
40 | 27 | * <p>A plugin can implement one or more methods of callbacks, but should not implement the same |
41 | 28 | * method of callback for multiple times. |
42 | 29 | */ |
43 | | -public abstract class BasePlugin { |
| 30 | +public abstract class BasePlugin implements Plugin { |
44 | 31 | protected final String name; |
45 | 32 |
|
46 | 33 | public BasePlugin(String name) { |
47 | 34 | this.name = name; |
48 | 35 | } |
49 | 36 |
|
| 37 | + @Override |
50 | 38 | public String getName() { |
51 | 39 | return name; |
52 | 40 | } |
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 | | - } |
207 | 41 | } |
0 commit comments