pub struct PluginBuilder { /* private fields */ }plugin only.Expand description
Builder for creating plugins with a fluent API.
PluginBuilder provides a chainable interface for constructing Plugin instances
without manually filling out a PluginConfig struct. Each method configures a
single lifecycle callback, and build produces the final Plugin.
§End-to-End Example
use adk_plugin::PluginBuilder;
use std::sync::Arc;
let plugin = PluginBuilder::new("audit-trail")
.before_agent(Box::new(|ctx| {
Box::pin(async move {
tracing::info!("agent starting");
Ok(())
})
}))
.after_agent(Box::new(|ctx| {
Box::pin(async move {
tracing::info!("agent finished");
Ok(())
})
}))
.before_tool(Box::new(|ctx| {
Box::pin(async move {
if let Some(name) = ctx.tool_name() {
tracing::info!(tool = name, "tool invoked");
}
Ok(())
})
}))
.after_tool(Box::new(|ctx| {
Box::pin(async move {
tracing::info!("tool completed");
Ok(())
})
}))
.before_model(Box::new(|ctx, request| {
Box::pin(async move {
tracing::debug!(model = ?request.model, "sending request to model");
Ok(request)
})
}))
.after_model(Box::new(|ctx, response| {
Box::pin(async move {
tracing::debug!("model responded");
Ok(None)
})
}))
.close_fn(|| Box::pin(async { tracing::info!("plugin closed") }))
.build();
assert_eq!(plugin.name(), "audit-trail");Implementations§
Source§impl PluginBuilder
impl PluginBuilder
Sourcepub fn new(name: impl Into<String>) -> PluginBuilder
pub fn new(name: impl Into<String>) -> PluginBuilder
Create a new plugin builder with the given name.
The name uniquely identifies this plugin within a PluginManager.
let builder = PluginBuilder::new("my-plugin");Sourcepub fn on_user_message(
self,
callback: Box<dyn Fn(Arc<dyn InvocationContext>, Content) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Sync + Send>,
) -> PluginBuilder
pub fn on_user_message( self, callback: Box<dyn Fn(Arc<dyn InvocationContext>, Content) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Sync + Send>, ) -> PluginBuilder
Set the callback invoked when a user message is received.
The callback can inspect or modify the incoming Content.
Return Ok(Some(content)) to replace the message, or Ok(None) to keep the original.
builder.on_user_message(Box::new(|ctx, content| {
Box::pin(async move {
tracing::info!(role = %content.role, "user message received");
Ok(None) // pass through unchanged
})
}))Sourcepub fn on_event(
self,
callback: Box<dyn Fn(Arc<dyn InvocationContext>, Event) -> Pin<Box<dyn Future<Output = Result<Option<Event>, AdkError>> + Send>> + Sync + Send>,
) -> PluginBuilder
pub fn on_event( self, callback: Box<dyn Fn(Arc<dyn InvocationContext>, Event) -> Pin<Box<dyn Future<Output = Result<Option<Event>, AdkError>> + Send>> + Sync + Send>, ) -> PluginBuilder
Set the callback invoked for each event generated by the agent.
The callback can inspect or modify Event objects before they
are yielded to the caller. Return Ok(Some(event)) to replace the event, or
Ok(None) to keep the original.
builder.on_event(Box::new(|ctx, event| {
Box::pin(async move {
tracing::debug!(id = %event.id, "event emitted");
Ok(None)
})
}))Sourcepub fn before_run(
self,
callback: Box<dyn Fn(Arc<dyn InvocationContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Sync + Send>,
) -> PluginBuilder
pub fn before_run( self, callback: Box<dyn Fn(Arc<dyn InvocationContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Sync + Send>, ) -> PluginBuilder
Set the callback invoked before the agent run starts.
Use this for setup, validation, or early exit. Return Ok(Some(content)) to
skip the run entirely and return that content, or Ok(None) to proceed normally.
builder.before_run(Box::new(|ctx| {
Box::pin(async move {
tracing::info!("run starting");
Ok(None) // continue with the run
})
}))Sourcepub fn after_run(
self,
callback: Box<dyn Fn(Arc<dyn InvocationContext>) -> Pin<Box<dyn Future<Output = ()> + Send>> + Sync + Send>,
) -> PluginBuilder
pub fn after_run( self, callback: Box<dyn Fn(Arc<dyn InvocationContext>) -> Pin<Box<dyn Future<Output = ()> + Send>> + Sync + Send>, ) -> PluginBuilder
Set the callback invoked after the agent run completes.
Use this for cleanup, logging, or metrics collection. This callback is for side effects only and does not emit events.
builder.after_run(Box::new(|ctx| {
Box::pin(async move {
tracing::info!("run completed");
})
}))Sourcepub fn before_agent(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Sync + Send>,
) -> PluginBuilder
pub fn before_agent( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Sync + Send>, ) -> PluginBuilder
Set the callback invoked before agent execution.
Runs each time an agent is about to execute. Useful for tracing, permission checks, or injecting state.
builder.before_agent(Box::new(|ctx| {
Box::pin(async move {
tracing::info!("agent starting");
Ok(())
})
}))Sourcepub fn after_agent(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Sync + Send>,
) -> PluginBuilder
pub fn after_agent( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Sync + Send>, ) -> PluginBuilder
Set the callback invoked after agent execution.
Runs each time an agent finishes. Useful for logging duration, collecting metrics, or post-processing results.
builder.after_agent(Box::new(|ctx| {
Box::pin(async move {
tracing::info!("agent finished");
Ok(())
})
}))Sourcepub fn before_model(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>, LlmRequest) -> Pin<Box<dyn Future<Output = Result<BeforeModelResult, AdkError>> + Send>> + Sync + Send>,
) -> PluginBuilder
pub fn before_model( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>, LlmRequest) -> Pin<Box<dyn Future<Output = Result<BeforeModelResult, AdkError>> + Send>> + Sync + Send>, ) -> PluginBuilder
Set the callback invoked before an LLM call.
The callback receives the LlmRequest and can modify it
or skip the call entirely by returning a pre-built response.
builder.before_model(Box::new(|ctx, request| {
Box::pin(async move {
tracing::debug!(model = ?request.model, "calling model");
Ok(request) // pass through unchanged
})
}))Sourcepub fn after_model(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>, LlmResponse) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AdkError>> + Send>> + Sync + Send>,
) -> PluginBuilder
pub fn after_model( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>, LlmResponse) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AdkError>> + Send>> + Sync + Send>, ) -> PluginBuilder
Set the callback invoked after an LLM call.
The callback receives the LlmResponse and can modify
or replace it. Return Ok(Some(response)) to replace, or Ok(None) to keep
the original.
builder.after_model(Box::new(|ctx, response| {
Box::pin(async move {
tracing::debug!("model responded");
Ok(None) // keep original response
})
}))Sourcepub fn on_model_error(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>, LlmRequest, String) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AdkError>> + Send>> + Sync + Send>,
) -> PluginBuilder
pub fn on_model_error( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>, LlmRequest, String) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AdkError>> + Send>> + Sync + Send>, ) -> PluginBuilder
Set the callback invoked when an LLM call returns an error.
The callback receives the original LlmRequest and the
error message. Return Ok(Some(response)) to provide a fallback response, or
Ok(None) to propagate the error.
builder.on_model_error(Box::new(|ctx, request, error| {
Box::pin(async move {
tracing::warn!(error = %error, "model error, no fallback");
Ok(None) // propagate the error
})
}))Sourcepub fn before_tool(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Sync + Send>,
) -> PluginBuilder
pub fn before_tool( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Sync + Send>, ) -> PluginBuilder
Set the callback invoked before tool execution.
Runs each time a tool is about to execute. The CallbackContext
provides tool_name() and tool_input() for tool-specific logic.
builder.before_tool(Box::new(|ctx| {
Box::pin(async move {
if let Some(name) = ctx.tool_name() {
tracing::info!(tool = name, "tool starting");
}
Ok(())
})
}))Sourcepub fn after_tool(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Sync + Send>,
) -> PluginBuilder
pub fn after_tool( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Sync + Send>, ) -> PluginBuilder
Set the callback invoked after tool execution.
Runs each time a tool finishes. Useful for logging results, collecting metrics, or auditing tool usage.
builder.after_tool(Box::new(|ctx| {
Box::pin(async move {
tracing::info!("tool completed");
Ok(())
})
}))Sourcepub fn on_tool_error(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>, Arc<dyn Tool>, Value, String) -> Pin<Box<dyn Future<Output = Result<Option<Value>, AdkError>> + Send>> + Sync + Send>,
) -> PluginBuilder
pub fn on_tool_error( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>, Arc<dyn Tool>, Value, String) -> Pin<Box<dyn Future<Output = Result<Option<Value>, AdkError>> + Send>> + Sync + Send>, ) -> PluginBuilder
Set the callback invoked when a tool returns an error.
The callback receives the tool name, the error, and the
CallbackContext. Use this to log failures or
provide fallback values.
builder.on_tool_error(Box::new(|ctx, tool_name, error| {
Box::pin(async move {
tracing::warn!(tool = %tool_name, error = %error, "tool failed");
Ok(None) // propagate the error
})
}))Sourcepub fn close_fn(
self,
f: impl Fn() -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync + 'static,
) -> PluginBuilder
pub fn close_fn( self, f: impl Fn() -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync + 'static, ) -> PluginBuilder
Set the async cleanup function called when the plugin is closed.
Use this to release resources, flush buffers, or perform final cleanup.
builder.close_fn(|| Box::pin(async {
tracing::info!("plugin shutting down");
}))Auto Trait Implementations§
impl !RefUnwindSafe for PluginBuilder
impl !UnwindSafe for PluginBuilder
impl Freeze for PluginBuilder
impl Send for PluginBuilder
impl Sync for PluginBuilder
impl Unpin for PluginBuilder
impl UnsafeUnpin for PluginBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreimpl<T> MaybeSend for Twhere
T: Send,
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.