pub struct FnTool { /* private fields */ }Expand description
A Tool built from a plain async function.
Use this to adapt an Autumn handler’s core logic:
async fn get_weather(input: Value) -> Result<Value, AgentError> {
let city = input["city"].as_str().unwrap_or("unknown");
Ok(json!({"city": city, "temp_f": 72}))
}
let tool = FnTool::new(
"get_weather",
"Current weather for a city.",
serde_json::json!({
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
}),
get_weather,
);Implementations§
Source§impl FnTool
impl FnTool
Sourcepub fn new<F, Fut>(
name: impl Into<String>,
description: impl Into<String>,
schema: Value,
run: F,
) -> Self
pub fn new<F, Fut>( name: impl Into<String>, description: impl Into<String>, schema: Value, run: F, ) -> Self
Wrap an async function as a tool.
The function receives the decoded JSON arguments and returns JSON.
Return ErrorKind::Tool errors for domain failures so the agent
loop reports them back to the model instead of aborting the run.
Sourcepub fn with_context<F, Fut>(
name: impl Into<String>,
description: impl Into<String>,
schema: Value,
run: F,
) -> Selfwhere
F: Fn(Value, ToolContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<Value, AgentError>> + Send + 'static,
pub fn with_context<F, Fut>(
name: impl Into<String>,
description: impl Into<String>,
schema: Value,
run: F,
) -> Selfwhere
F: Fn(Value, ToolContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<Value, AgentError>> + Send + 'static,
Wrap an async function that also wants the ToolContext.
Use it when the tool needs the run id or call id, for example as an idempotency key.
Sourcepub const fn effect(self, effect: ToolEffect) -> Self
pub const fn effect(self, effect: ToolEffect) -> Self
Declare what the tool can change. The default is
ToolEffect::Write.
Share the tool between the plugin registry and direct use.
Trait Implementations§
Source§impl Tool for FnTool
impl Tool for FnTool
Source§fn description(&self) -> &str
fn description(&self) -> &str
What the tool does, shown to the model.
Source§fn input_schema(&self) -> Value
fn input_schema(&self) -> Value
JSON Schema for the tool’s input object.
Source§fn execute<'a>(
&'a self,
input: Value,
ctx: &'a ToolContext,
) -> Pin<Box<dyn Future<Output = Result<Value, AgentError>> + Send + 'a>>
fn execute<'a>( &'a self, input: Value, ctx: &'a ToolContext, ) -> Pin<Box<dyn Future<Output = Result<Value, AgentError>> + Send + 'a>>
Run the tool against decoded JSON arguments.
Source§fn effect(&self) -> ToolEffect
fn effect(&self) -> ToolEffect
What the tool can change. Defaults to
ToolEffect::Write: a tool
is trusted with less only when it says so.Source§fn definition(&self) -> ToolDefinition
fn definition(&self) -> ToolDefinition
Bundle the metadata the model sees into a
ToolDefinition.Auto Trait Implementations§
impl !RefUnwindSafe for FnTool
impl !UnwindSafe for FnTool
impl Forget for FnTool
impl Freeze for FnTool
impl Send for FnTool
impl Sync for FnTool
impl Unpin for FnTool
impl UnsafeUnpin for FnTool
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
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> Scoped for T
impl<T> Scoped for T
Source§fn scope(ctx: &PolicyContext) -> ScopeQuery<'_, Self>
fn scope(ctx: &PolicyContext) -> ScopeQuery<'_, Self>
Open a deferred
ScopeQuery for this type. Resolves the
registered scope at .load() time, not here.