Skip to main content

Module local

Module local 

Source
Expand description

In-process tool registration and interaction context. Local executable registrations and declared caller interactions.

ToolRegistration::function accepts normalized invocation values; ToolRegistration::typed derives schemas for typed arguments and results. Interactive handlers use their ToolInvocationContext and resume through the same unique handler. Construct a source with crate::Toolset::local.

§Caller interaction and cancellation

The crate::local::ToolInvocationContext::request_text and crate::local::ToolInvocationContext::request_form examples below execute the complete submit/resume path and inspect the result. Cancellation uses the same unique handler and exact pending interaction id. It is an outer cancellation outcome, not a completed model-visible result.

use tools_kit::{Toolset, ToolsetBackend, ToolHandlerBackend, ToolError};
use tools_kit::declaration::{ToolInfo, ToolName};
use tools_kit::execution::{ToolHandlerInput, ToolHandlerOutput, ToolInvocation, ToolResult};
use tools_kit::interaction::{ToolInteractionCapabilities, ToolInteractionResponse};
use tools_kit::local::ToolRegistration;
use tools_kit::schema::JsonSchema;
let name = ToolName::new("ask")?;
let mut info = ToolInfo::new(
    name.clone(),
    JsonSchema::strict(serde_json::json!({"type":"object"})),
);
info.interactions = ToolInteractionCapabilities { text: true, ..Default::default() };
let tools = Toolset::local(vec![ToolRegistration::interactive(info, |_input, context| async move {
    let answer = context.request_text("Which city?", false).await?;
    Ok(ToolResult::text(answer))
})])?;
let work = async {
    let mut handler = tools.tool_handler(name.clone()).await?;
    let output = handler.step(ToolHandlerInput::Invoke(ToolInvocation {
        call_id: Some("city-call".into()), arguments: serde_json::json!({}),
    })).await?;
    let ToolHandlerOutput::Interaction(request) = output else { panic!("interaction expected") };
    assert_eq!(request.tool_name, name);
    assert_eq!(request.call_id.as_deref(), Some("city-call"));
    let reply = ToolInteractionResponse::cancel(request.id, Some("caller stopped".into()));
    let cancelled = handler.step(ToolHandlerInput::Interaction(reply)).await;
    assert!(matches!(cancelled, Err(ToolError::Cancelled { .. })));
    assert!(handler.step(ToolHandlerInput::Cancel { reason: None }).await.is_err());
    Ok::<(), ToolError>(())
}.await;
let cleanup = tools.shutdown().await;
if let Err(error) = &cleanup {
    eprintln!("Cleanup failed: {error}");
}
work?;
cleanup?;

Structs§

ToolInvocationContext
Context available to local tools that declare caller interaction support.
ToolRegistration
One complete local tool declaration plus its private executable handler. Registration constructs passive declarations and closures; execution begins through the selected unique handler. Cloning a registration shares its immutable handler.