llm-tool 0.10.6

Framework-agnostic Rust tool definitions for LLM agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Strongly-typed Rust tool trait and type-erasure machinery.

use alloc::{borrow::Cow, boxed::Box, format, string::ToString};
use core::{future::Future, pin::Pin};

use super::types::{
    OutputSanitization, ToolContext, ToolDefinition, ToolEffect, ToolError, ToolOutput,
    ToolProvenance,
};

/// Convenience type for tools that take no parameters.
#[derive(
    Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema,
)]
pub struct EmptyParams {}

/// A custom tool implemented entirely in Rust with strongly-typed parameters.
///
/// Define your parameters as a struct deriving [`serde::Deserialize`] and
/// `JsonSchema`, then implement this trait to provide the tool's logic.
/// The JSON Schema sent to the model is derived automatically from the
/// params struct — doc comments on fields become parameter descriptions.
///
/// Tools are async: for I/O-bound work (HTTP, filesystem, subprocess) the
/// runtime stays unblocked. Sync tools just don't `.await` anything — the
/// compiler optimizes the state machine to an immediate return.
///
/// # Example
///
/// ```rust
/// use llm_tool::{JsonSchema, RustTool, ToolContext, ToolError, ToolOutput};
/// use serde::Deserialize;
///
/// #[derive(Deserialize, JsonSchema)]
/// struct FlashParams {
///     /// Target device identifier.
///     device_id: String,
///     /// Path to the firmware image.
///     image_path: String,
/// }
///
/// struct FlashDevice;
///
/// impl RustTool for FlashDevice {
///     type Params = FlashParams;
///     const NAME: &'static str = "flash_device";
///     const DESCRIPTION: &'static str = "Flashes firmware to a connected device.";
///
///     async fn call(
///         &self,
///         params: Self::Params,
///         _ctx: &ToolContext,
///     ) -> Result<ToolOutput, ToolError> {
///         Ok(format!("Flashed {} to {}", params.image_path, params.device_id).into())
///     }
/// }
/// ```
pub trait RustTool: Send + Sync {
    /// The strongly-typed parameters struct.
    ///
    /// Derive [`serde::Deserialize`] and `JsonSchema` on your params struct.
    /// `JsonSchema` auto-generates the parameter schema sent to the model;
    /// `Deserialize` parses the model's JSON arguments into your struct.
    type Params: serde::de::DeserializeOwned + schemars::JsonSchema + Send;

    /// Unique tool name (e.g. `"flash_device"`).
    const NAME: &'static str;

    /// Human-readable description shown to the model.
    const DESCRIPTION: &'static str;

    /// Execution effect of the tool.
    const EFFECT: ToolEffect = if Self::IDEMPOTENT {
        ToolEffect::ReadOnly
    } else {
        ToolEffect::Mutating
    };

    /// Whether the tool is idempotent / read-only safe for concurrent wave execution.
    const IDEMPOTENT: bool = false;

    /// Data-provenance and trust classification of the tool.
    ///
    /// Defaults to [`ToolProvenance::Trusted`]. Tools with [`ToolProvenance::ExternalMcp`]
    /// or [`ToolProvenance::UntrustedNetwork`] automatically default [`Self::OUTPUT_SANITIZATION`]
    /// to [`OutputSanitization::Quarantine`] unless overridden.
    const PROVENANCE: ToolProvenance = ToolProvenance::Trusted;

    /// Shorthand: neutralize control tokens in this tool's output.
    ///
    /// Defaults to `true` (secure by default). Set to `false` to opt out, or prefer
    /// setting [`Self::OUTPUT_SANITIZATION`] directly.
    const SANITIZE_OUTPUT: bool = true;

    /// Shorthand: wrap this tool's output in a `<tool_output_quarantine>` envelope.
    ///
    /// Takes precedence over [`Self::SANITIZE_OUTPUT`]. Prefer setting
    /// [`Self::OUTPUT_SANITIZATION`] directly.
    const QUARANTINE_OUTPUT: bool = false;

    /// Automatic prompt-injection sanitization policy applied to this tool's output.
    ///
    /// Derived from the shorthands and provenance: [`OutputSanitization::Quarantine`] if
    /// [`Self::QUARANTINE_OUTPUT`] or [`Self::PROVENANCE`] is an untrusted external source,
    /// else [`OutputSanitization::SanitizeTokens`] if [`Self::SANITIZE_OUTPUT`] (the default),
    /// else [`OutputSanitization::None`].
    /// Override it to pick a tagged envelope, or [`OutputSanitization::None`] only for
    /// output you fully control.
    const OUTPUT_SANITIZATION: OutputSanitization =
        if Self::QUARANTINE_OUTPUT || Self::PROVENANCE.is_untrusted_source() {
            OutputSanitization::Quarantine
        } else if Self::SANITIZE_OUTPUT {
            OutputSanitization::SanitizeTokens
        } else {
            OutputSanitization::None
        };

    /// Return the execution effect of the tool.
    fn effect(&self) -> ToolEffect {
        Self::EFFECT
    }

    /// Return the data-provenance tier of the tool.
    fn provenance(&self) -> ToolProvenance {
        Self::PROVENANCE
    }

    /// Return whether the tool is idempotent (read-only).
    fn is_idempotent(&self) -> bool {
        matches!(self.effect(), ToolEffect::ReadOnly)
    }

    /// Return the automatic prompt-injection sanitization policy for this tool's output.
    fn output_sanitization(&self) -> OutputSanitization {
        Self::OUTPUT_SANITIZATION
    }

    /// Return the tool description used in [`ToolDefinition`].
    ///
    /// The default returns [`Self::DESCRIPTION`] (the static string from a
    /// doc comment or template body). When using
    /// `#[llm_tool(description_file = "...", context = ...)]`, the generated
    /// implementation overrides this to render the template with runtime
    /// variables on each call (parsed once via `LazyLock`), falling back to
    /// [`Self::DESCRIPTION`] if a render ever fails.
    fn description(&self) -> Cow<'static, str> {
        Cow::Borrowed(Self::DESCRIPTION)
    }

    /// Execute the tool with typed parameters and an execution context.
    ///
    /// Async to support I/O-bound tools (HTTP, filesystem, subprocess).
    /// Sync tools just compute and return — the async wrapper is zero-cost.
    ///
    /// The `ctx` parameter provides access to conversation metadata and a
    /// shared key-value state store. Tools that don't need context can simply
    /// ignore it with `_ctx`.
    ///
    /// # Errors
    ///
    /// Returns `Err(ToolError)` if the tool execution fails.
    fn call(
        &self,
        params: Self::Params,
        ctx: &ToolContext,
    ) -> impl Future<Output = Result<ToolOutput, ToolError>> + Send;
}

/// Shared [`alloc::sync::Arc`]-backed wrapper for any [`RustTool`] implementation,
/// enabling cheap cloning across registries and subagent scopes.
#[derive(Debug)]
pub struct SharedRustTool<T>(pub alloc::sync::Arc<T>);

impl<T> Clone for SharedRustTool<T> {
    fn clone(&self) -> Self {
        Self(alloc::sync::Arc::clone(&self.0))
    }
}

impl<T> SharedRustTool<T> {
    /// Wrap `tool` in a new [`SharedRustTool`].
    #[must_use]
    pub fn new(tool: T) -> Self {
        Self(alloc::sync::Arc::new(tool))
    }

    /// Borrow the underlying tool.
    #[must_use]
    pub fn as_inner(&self) -> &T {
        &self.0
    }
}

impl<T> From<alloc::sync::Arc<T>> for SharedRustTool<T> {
    fn from(arc: alloc::sync::Arc<T>) -> Self {
        Self(arc)
    }
}

impl<T: RustTool> RustTool for alloc::sync::Arc<T> {
    type Params = T::Params;
    const NAME: &'static str = T::NAME;
    const DESCRIPTION: &'static str = T::DESCRIPTION;
    const EFFECT: ToolEffect = T::EFFECT;
    const IDEMPOTENT: bool = T::IDEMPOTENT;
    const PROVENANCE: ToolProvenance = T::PROVENANCE;
    const SANITIZE_OUTPUT: bool = T::SANITIZE_OUTPUT;
    const QUARANTINE_OUTPUT: bool = T::QUARANTINE_OUTPUT;
    const OUTPUT_SANITIZATION: OutputSanitization = T::OUTPUT_SANITIZATION;

    fn effect(&self) -> ToolEffect {
        (**self).effect()
    }

    fn provenance(&self) -> ToolProvenance {
        (**self).provenance()
    }

    fn is_idempotent(&self) -> bool {
        (**self).is_idempotent()
    }

    fn output_sanitization(&self) -> OutputSanitization {
        (**self).output_sanitization()
    }

    fn description(&self) -> Cow<'static, str> {
        (**self).description()
    }

    fn call(
        &self,
        params: Self::Params,
        ctx: &ToolContext,
    ) -> impl Future<Output = Result<ToolOutput, ToolError>> + Send {
        (**self).call(params, ctx)
    }
}

impl<T: RustTool> RustTool for SharedRustTool<T> {
    type Params = T::Params;
    const NAME: &'static str = T::NAME;
    const DESCRIPTION: &'static str = T::DESCRIPTION;
    const EFFECT: ToolEffect = T::EFFECT;
    const IDEMPOTENT: bool = T::IDEMPOTENT;
    const PROVENANCE: ToolProvenance = T::PROVENANCE;
    const SANITIZE_OUTPUT: bool = T::SANITIZE_OUTPUT;
    const QUARANTINE_OUTPUT: bool = T::QUARANTINE_OUTPUT;
    const OUTPUT_SANITIZATION: OutputSanitization = T::OUTPUT_SANITIZATION;

    fn effect(&self) -> ToolEffect {
        self.0.effect()
    }

    fn provenance(&self) -> ToolProvenance {
        self.0.provenance()
    }

    fn is_idempotent(&self) -> bool {
        self.0.is_idempotent()
    }

    fn output_sanitization(&self) -> OutputSanitization {
        self.0.output_sanitization()
    }

    fn description(&self) -> Cow<'static, str> {
        self.0.description()
    }

    fn call(
        &self,
        params: Self::Params,
        ctx: &ToolContext,
    ) -> impl Future<Output = Result<ToolOutput, ToolError>> + Send {
        self.0.call(params, ctx)
    }
}

/// Build a [`ToolDefinition`] from any [`RustTool`] implementor.
///
/// The generated schema is sanitized for broad compatibility with LLM
/// SDKs that expect `"type"` to always be a single string
/// (not the array form `["string", "null"]` that schemars emits for
/// `Option<T>` fields).
///
/// # Errors
///
/// Returns `Err` if the JSON schema serialization fails.
pub fn definition_of<T: RustTool>(tool: &T) -> Result<ToolDefinition, ToolError> {
    let schema = schemars::schema_for!(T::Params);
    let mut parameter_schema = serde_json::to_value(schema).map_err(|e| {
        ToolError::new(format!(
            "Failed to serialize schema for tool '{}': {e}",
            T::NAME
        ))
    })?;
    sanitize_schema_types(&mut parameter_schema);
    Ok(ToolDefinition::with_effect(
        T::NAME,
        tool.description().into_owned(),
        parameter_schema,
        tool.effect(),
    )
    .with_provenance(tool.provenance())
    .with_output_sanitization(tool.output_sanitization()))
}

/// Recursively sanitize JSON Schema `"type"` fields for Go genai compatibility.
///
/// `schemars` emits `"type": ["string", "null"]` for `Option<String>` fields
/// (the nullable type-array form). The Go genai SDK's `Schema.Type`
/// is a single `genai.Type` enum, so it can't unmarshal an array.
///
/// This function walks the schema tree and replaces any array `type` with the
/// first non-`"null"` element. For example:
/// - `["string", "null"]` → `"string"`
/// - `["integer", "null"]` → `"integer"`
const SCHEMA_TYPE_KEY: &str = "type";
const SCHEMA_NULL_TYPE: &str = "null";

fn sanitize_schema_types(value: &mut serde_json::Value) {
    match value {
        serde_json::Value::Object(map) => {
            // If "type" is an array (e.g. ["string", "null"]), pick the first
            // non-"null" element and replace with it as a scalar type.
            let replacement = match map.get(SCHEMA_TYPE_KEY) {
                Some(serde_json::Value::Array(arr)) => {
                    let non_null = arr
                        .iter()
                        .find(
                            |v| !matches!(v, serde_json::Value::String(s) if s == SCHEMA_NULL_TYPE),
                        )
                        .cloned();
                    non_null.or_else(|| arr.first().cloned())
                }
                _ => None,
            };
            if let Some(val) = replacement {
                map.insert(SCHEMA_TYPE_KEY.to_string(), val);
            }
            for val in map.values_mut() {
                sanitize_schema_types(val);
            }
        }
        serde_json::Value::Array(arr) => {
            for item in arr {
                sanitize_schema_types(item);
            }
        }
        _ => {}
    }
}

/// Type-erased future returned when dispatching a boxed tool.
///
/// Produced by the crate-internal type-erasure layer that lets heterogeneous
/// [`RustTool`] implementations share one registry.
pub type BoxToolFuture<'a> =
    Pin<Box<dyn Future<Output = Result<ToolOutput, ToolError>> + Send + 'a>>;

/// Type-erased wrapper enabling heterogeneous tool storage.
///
/// Boxes the future from [`RustTool::call`] so we can store different tool
/// types in the same `HashMap<String, Box<dyn ErasedTool>>`.
pub trait ErasedTool: Send + Sync {
    /// Deserialize `args` and call the handler, returning a boxed future.
    fn call_erased<'a>(
        &'a self,
        args: serde_json::Value,
        ctx: &'a ToolContext,
    ) -> BoxToolFuture<'a>;

    /// Deserialize `args_json` directly from a JSON string and call the handler,
    /// returning a boxed future without intermediate DOM allocation.
    fn call_erased_str<'a>(&'a self, args_json: &'a str, ctx: &'a ToolContext)
    -> BoxToolFuture<'a>;
}

impl<T: RustTool> ErasedTool for T {
    fn call_erased<'a>(
        &'a self,
        args: serde_json::Value,
        ctx: &'a ToolContext,
    ) -> BoxToolFuture<'a> {
        Box::pin(async move {
            let params: T::Params = serde_json::from_value(args).map_err(|e| {
                ToolError::new(format!("Failed to deserialize tool parameters: {e}"))
            })?;
            let output = self.call(params, ctx).await?;
            Ok(self.output_sanitization().apply(output))
        })
    }

    fn call_erased_str<'a>(
        &'a self,
        args_json: &'a str,
        ctx: &'a ToolContext,
    ) -> BoxToolFuture<'a> {
        Box::pin(async move {
            let params: T::Params = serde_json::from_str(args_json).map_err(|e| {
                ToolError::new(format!("Failed to deserialize tool parameters: {e}"))
            })?;
            let output = self.call(params, ctx).await?;
            Ok(self.output_sanitization().apply(output))
        })
    }
}