1use crate::callbacks::*;
6use adk_core::{
7 AfterAgentCallback, AfterModelCallback, AfterToolCallback, BeforeAgentCallback,
8 BeforeModelCallback, BeforeToolCallback,
9};
10use std::future::Future;
11use std::pin::Pin;
12
13pub type CloseFn = Box<dyn Fn() -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>;
15
16pub struct PluginConfig {
35 pub name: String,
37
38 pub on_user_message: Option<OnUserMessageCallback>,
41 pub on_event: Option<OnEventCallback>,
43 pub before_run: Option<BeforeRunCallback>,
45 pub after_run: Option<AfterRunCallback>,
47
48 pub before_agent: Option<BeforeAgentCallback>,
51 pub after_agent: Option<AfterAgentCallback>,
53
54 pub before_model: Option<BeforeModelCallback>,
57 pub after_model: Option<AfterModelCallback>,
59 pub on_model_error: Option<OnModelErrorCallback>,
61
62 pub before_tool: Option<BeforeToolCallback>,
65 pub after_tool: Option<AfterToolCallback>,
67 pub on_tool_error: Option<OnToolErrorCallback>,
69
70 pub close_fn: Option<CloseFn>,
72}
73
74impl Default for PluginConfig {
75 fn default() -> Self {
76 Self {
77 name: "unnamed".to_string(),
78 on_user_message: None,
79 on_event: None,
80 before_run: None,
81 after_run: None,
82 before_agent: None,
83 after_agent: None,
84 before_model: None,
85 after_model: None,
86 on_model_error: None,
87 before_tool: None,
88 after_tool: None,
89 on_tool_error: None,
90 close_fn: None,
91 }
92 }
93}
94
95pub struct Plugin {
128 config: PluginConfig,
129}
130
131impl Plugin {
132 pub fn new(config: PluginConfig) -> Self {
134 Self { config }
135 }
136
137 pub fn name(&self) -> &str {
139 &self.config.name
140 }
141
142 pub fn on_user_message(&self) -> Option<&OnUserMessageCallback> {
144 self.config.on_user_message.as_ref()
145 }
146
147 pub fn on_event(&self) -> Option<&OnEventCallback> {
149 self.config.on_event.as_ref()
150 }
151
152 pub fn before_run(&self) -> Option<&BeforeRunCallback> {
154 self.config.before_run.as_ref()
155 }
156
157 pub fn after_run(&self) -> Option<&AfterRunCallback> {
159 self.config.after_run.as_ref()
160 }
161
162 pub fn before_agent(&self) -> Option<&BeforeAgentCallback> {
164 self.config.before_agent.as_ref()
165 }
166
167 pub fn after_agent(&self) -> Option<&AfterAgentCallback> {
169 self.config.after_agent.as_ref()
170 }
171
172 pub fn before_model(&self) -> Option<&BeforeModelCallback> {
174 self.config.before_model.as_ref()
175 }
176
177 pub fn after_model(&self) -> Option<&AfterModelCallback> {
179 self.config.after_model.as_ref()
180 }
181
182 pub fn on_model_error(&self) -> Option<&OnModelErrorCallback> {
184 self.config.on_model_error.as_ref()
185 }
186
187 pub fn before_tool(&self) -> Option<&BeforeToolCallback> {
189 self.config.before_tool.as_ref()
190 }
191
192 pub fn after_tool(&self) -> Option<&AfterToolCallback> {
194 self.config.after_tool.as_ref()
195 }
196
197 pub fn on_tool_error(&self) -> Option<&OnToolErrorCallback> {
199 self.config.on_tool_error.as_ref()
200 }
201
202 pub async fn close(&self) {
204 if let Some(ref close_fn) = self.config.close_fn {
205 close_fn().await;
206 }
207 }
208}
209
210impl std::fmt::Debug for Plugin {
211 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
212 f.debug_struct("Plugin")
213 .field("name", &self.config.name)
214 .field("has_on_user_message", &self.config.on_user_message.is_some())
215 .field("has_on_event", &self.config.on_event.is_some())
216 .field("has_before_run", &self.config.before_run.is_some())
217 .field("has_after_run", &self.config.after_run.is_some())
218 .field("has_before_agent", &self.config.before_agent.is_some())
219 .field("has_after_agent", &self.config.after_agent.is_some())
220 .field("has_before_model", &self.config.before_model.is_some())
221 .field("has_after_model", &self.config.after_model.is_some())
222 .field("has_before_tool", &self.config.before_tool.is_some())
223 .field("has_after_tool", &self.config.after_tool.is_some())
224 .finish()
225 }
226}
227
228pub struct PluginBuilder {
239 config: PluginConfig,
240}
241
242impl PluginBuilder {
243 pub fn new(name: impl Into<String>) -> Self {
245 Self { config: PluginConfig { name: name.into(), ..Default::default() } }
246 }
247
248 pub fn on_user_message(mut self, callback: OnUserMessageCallback) -> Self {
250 self.config.on_user_message = Some(callback);
251 self
252 }
253
254 pub fn on_event(mut self, callback: OnEventCallback) -> Self {
256 self.config.on_event = Some(callback);
257 self
258 }
259
260 pub fn before_run(mut self, callback: BeforeRunCallback) -> Self {
262 self.config.before_run = Some(callback);
263 self
264 }
265
266 pub fn after_run(mut self, callback: AfterRunCallback) -> Self {
268 self.config.after_run = Some(callback);
269 self
270 }
271
272 pub fn before_agent(mut self, callback: BeforeAgentCallback) -> Self {
274 self.config.before_agent = Some(callback);
275 self
276 }
277
278 pub fn after_agent(mut self, callback: AfterAgentCallback) -> Self {
280 self.config.after_agent = Some(callback);
281 self
282 }
283
284 pub fn before_model(mut self, callback: BeforeModelCallback) -> Self {
286 self.config.before_model = Some(callback);
287 self
288 }
289
290 pub fn after_model(mut self, callback: AfterModelCallback) -> Self {
292 self.config.after_model = Some(callback);
293 self
294 }
295
296 pub fn on_model_error(mut self, callback: OnModelErrorCallback) -> Self {
298 self.config.on_model_error = Some(callback);
299 self
300 }
301
302 pub fn before_tool(mut self, callback: BeforeToolCallback) -> Self {
304 self.config.before_tool = Some(callback);
305 self
306 }
307
308 pub fn after_tool(mut self, callback: AfterToolCallback) -> Self {
310 self.config.after_tool = Some(callback);
311 self
312 }
313
314 pub fn on_tool_error(mut self, callback: OnToolErrorCallback) -> Self {
316 self.config.on_tool_error = Some(callback);
317 self
318 }
319
320 pub fn close_fn(
322 mut self,
323 f: impl Fn() -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync + 'static,
324 ) -> Self {
325 self.config.close_fn = Some(Box::new(f));
326 self
327 }
328
329 pub fn build(self) -> Plugin {
331 Plugin::new(self.config)
332 }
333}