pub struct ToolDispatcher { /* private fields */ }Expand description
Routes function calls to the right tool implementation.
Implementations§
Source§impl ToolDispatcher
impl ToolDispatcher
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty tool dispatcher with the default 30-second timeout.
§Examples
use gemini_adk_rs::tool::{ToolDispatcher, SimpleTool};
use serde_json::json;
let mut dispatcher = ToolDispatcher::new();
dispatcher.register(SimpleTool::new(
"echo", "Echo input", None,
|args| async move { Ok(args) },
));Sourcepub fn with_timeout(self, timeout: Duration) -> Self
pub fn with_timeout(self, timeout: Duration) -> Self
Set the default timeout for tool calls.
Sourcepub fn with_confirmation_provider(
self,
provider: Arc<dyn ConfirmationProvider>,
) -> Self
pub fn with_confirmation_provider( self, provider: Arc<dyn ConfirmationProvider>, ) -> Self
Attach a confirmation provider (builder form).
Once set, any tool reporting
requires_confirmation
— e.g. one built with T::confirm(..) — is checked against the provider
before it executes; a denied decision returns a ToolError instead of
running the tool. With no provider configured, confirmation-gated tools
run normally (enforcement is opt-in).
Sourcepub fn set_confirmation_provider(
&mut self,
provider: Arc<dyn ConfirmationProvider>,
)
pub fn set_confirmation_provider( &mut self, provider: Arc<dyn ConfirmationProvider>, )
Attach a confirmation provider in place. See
with_confirmation_provider.
Sourcepub fn has_confirmation_provider(&self) -> bool
pub fn has_confirmation_provider(&self) -> bool
Whether a confirmation provider is configured.
Sourcepub fn default_timeout(&self) -> Duration
pub fn default_timeout(&self) -> Duration
Returns the configured default timeout.
Sourcepub fn register(&mut self, tool: impl ToolFunction)
pub fn register(&mut self, tool: impl ToolFunction)
Register a tool that implements ToolFunction.
Sourcepub fn register_function(&mut self, tool: Arc<dyn ToolFunction>)
pub fn register_function(&mut self, tool: Arc<dyn ToolFunction>)
Register a regular function tool (pre-wrapped in Arc).
Sourcepub fn register_streaming(&mut self, tool: Arc<dyn StreamingTool>)
pub fn register_streaming(&mut self, tool: Arc<dyn StreamingTool>)
Register a streaming tool.
Sourcepub fn register_input_streaming(&mut self, tool: Arc<dyn InputStreamingTool>)
pub fn register_input_streaming(&mut self, tool: Arc<dyn InputStreamingTool>)
Register an input-streaming tool.
Sourcepub fn merge(&mut self, other: ToolDispatcher)
pub fn merge(&mut self, other: ToolDispatcher)
Add every tool of other whose name this dispatcher does not already
have. This dispatcher’s own tools, timeout and confirmation provider
win.
Sourcepub fn gated_tools(&self) -> impl Iterator<Item = &str>
pub fn gated_tools(&self) -> impl Iterator<Item = &str>
The function tools that ask for confirmation before they run
(T::confirm(..)), which need a confirmation provider to be gated.
Sourcepub fn names(&self) -> impl Iterator<Item = &str>
pub fn names(&self) -> impl Iterator<Item = &str>
The names of the registered tools, in declaration order.
Sourcepub fn get_tool(&self, name: &str) -> Option<&ToolKind>
pub fn get_tool(&self, name: &str) -> Option<&ToolKind>
Get a tool by name (for introspection/streaming tool spawning).
Sourcepub async fn call_function(
&self,
name: &str,
args: Value,
) -> Result<Value, ToolError>
pub async fn call_function( &self, name: &str, args: Value, ) -> Result<Value, ToolError>
Call a regular function tool by name, using the default timeout.
The tool gets a detached ToolContext (fresh
state, no call id). Inside a session use
call_function_in.
Sourcepub async fn call_function_in(
&self,
name: &str,
args: Value,
ctx: ToolContext,
) -> Result<Value, ToolError>
pub async fn call_function_in( &self, name: &str, args: Value, ctx: ToolContext, ) -> Result<Value, ToolError>
Call a regular function tool by name within a session: the tool
receives ctx (see ToolContext). The default
timeout covers confirmation and execution. Cancelling ctx.cancel
drops either wait with ToolError::Cancelled.
Sourcepub async fn call_function_with_timeout(
&self,
name: &str,
args: Value,
timeout: Duration,
) -> Result<Value, ToolError>
pub async fn call_function_with_timeout( &self, name: &str, args: Value, timeout: Duration, ) -> Result<Value, ToolError>
Call a regular function tool by name with an explicit timeout.
The duration includes confirmation and execution. When it expires,
the pending future is dropped and ToolError::Timeout is returned.
Sourcepub async fn call_function_with_cancel(
&self,
name: &str,
args: Value,
cancel: CancellationToken,
) -> Result<Value, ToolError>
pub async fn call_function_with_cancel( &self, name: &str, args: Value, cancel: CancellationToken, ) -> Result<Value, ToolError>
Call a regular function tool by name, racing against a cancellation token.
The default timeout covers confirmation and execution. If the token
is cancelled during either wait, its future is dropped and
ToolError::Cancelled is returned.
Sourcepub fn build_response(
call: &FunctionCall,
result: Result<Value, ToolError>,
) -> FunctionResponse
pub fn build_response( call: &FunctionCall, result: Result<Value, ToolError>, ) -> FunctionResponse
Build a FunctionResponse from a FunctionCall result.
Sourcepub async fn cancel_streaming(&self, name: &str)
pub async fn cancel_streaming(&self, name: &str)
Cancel a streaming tool by name.
Sourcepub async fn cancel_by_ids(&self, ids: &[String])
pub async fn cancel_by_ids(&self, ids: &[String])
Cancel streaming tools by IDs.
Sourcepub fn to_tool_declarations(&self) -> Vec<Tool>
pub fn to_tool_declarations(&self) -> Vec<Tool>
Generate Tool declarations for the setup message.
Declarations are ordered by tool name and cached until the next
register*() or merge.
Trait Implementations§
Source§impl Default for ToolDispatcher
impl Default for ToolDispatcher
Source§impl ToolProvider for ToolDispatcher
impl ToolProvider for ToolDispatcher
Source§fn declarations(&self) -> Vec<Tool>
fn declarations(&self) -> Vec<Tool>
Auto Trait Implementations§
impl !Freeze for ToolDispatcher
impl !RefUnwindSafe for ToolDispatcher
impl !UnwindSafe for ToolDispatcher
impl Send for ToolDispatcher
impl Sync for ToolDispatcher
impl Unpin for ToolDispatcher
impl UnsafeUnpin for ToolDispatcher
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
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§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.§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.§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.§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.§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.§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.§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.§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.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> 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 more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§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 more§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 more§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
§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
§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.§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.§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.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
§impl<T> Tap for T
impl<T> Tap for T
§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 more§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 more§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 more§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 more§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 more§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 more§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.§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.§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.§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.§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.§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.§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.