Skip to main content

ToolContext

Struct ToolContext 

Source
pub struct ToolContext<'a> {
    pub tool_call_id: &'a str,
    pub args: Value,
    pub llm: Option<&'a AgentLlmConfig>,
    pub provider: Option<Arc<dyn LlmProvider>>,
    pub working_dir: Option<&'a PathBuf>,
    /* private fields */
}
Expand description

Per-invocation context passed to Tool::execute methods.

Carries everything a tool needs: the call ID for building results, raw JSON arguments, cancellation support, and the working directory for path resolution.

§Key fields

FieldPurpose
tool_call_idEcho into ToolResult::tool_call_id so results match calls
argsRaw JSON arguments sent by the LLM — parse with parse_args
working_dirChat workspace root; relative paths resolve against this

Fields§

§tool_call_id: &'a str

The unique ID for this tool call. Echo into ToolResult::tool_call_id when returning.

§args: Value

Raw JSON arguments from the LLM. Use parse_args to deserialize.

§llm: Option<&'a AgentLlmConfig>

Current parent configuration, including its cache routing identity.

§provider: Option<Arc<dyn LlmProvider>>

Current provider for tools that delegate model requests.

§working_dir: Option<&'a PathBuf>

Working directory for the current agent (typically chat workspace). Relative paths resolve here.

Implementations§

Source§

impl<'a> ToolContext<'a>

Source

pub fn new( tool_call_id: &'a str, args: Value, working_dir: Option<&'a PathBuf>, cancel_token: &'a CancellationToken, ) -> Self

Create a new tool execution context.

Source

pub fn with_llm(self, llm: &'a AgentLlmConfig) -> Self

Pass the current agent configuration to the tool.

Source

pub fn with_provider(self, provider: Arc<dyn LlmProvider>) -> Self

Pass the current provider to tools that delegate model requests.

Source

pub fn with_progress(self, tx: UnboundedSender<String>) -> Self

Attach a progress channel for streaming incremental output.

Source

pub fn with_background_request(self, request: &'a BackgroundRequest) -> Self

Attach the user’s control for backgrounding this running call.

Source

pub fn background_request(&self) -> Option<&BackgroundRequest>

Source

pub fn progress_sender(&self) -> Option<UnboundedSender<String>>

Clone the live output destination when execution moves to an owned task.

Source

pub fn progress(&self, line: &str)

Send a progress line through the channel (no-op if no channel is set).

Source

pub fn parse_args<T: DeserializeOwned>(&self, tool_name: &str) -> Result<T>

Parse the raw JSON arguments into a typed value without consuming them.

The tool name is included in errors, along with the deserialization error. This does not validate the tool’s JSON schema, so keep the schema in sync with the argument type.

use std::path::PathBuf;

use flashmind_types::tool::ToolContext;
use serde::Deserialize;
use serde_json::json;
use tokio_util::sync::CancellationToken;

#[derive(Deserialize)]
struct ReadArgs {
    path: PathBuf,
}

let cancel = CancellationToken::new();
let context = ToolContext::new("call", json!({"path": "src/main.rs"}), None, &cancel);
let args: ReadArgs = context.parse_args("read")?;
assert_eq!(args.path, PathBuf::from("src/main.rs"));
Source

pub fn cancel_token(&self) -> &CancellationToken

Access the cancellation token for cooperative cancellation checks.

For long-running operations, prefer child_token to get a derived token that can be passed to async sub-operations.

Source

pub fn child_token(&self) -> CancellationToken

Create a child cancellation token. Cancels when the parent turn is cancelled. Use this for spawned tasks so they’re automatically cancelled when the user aborts.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for ToolContext<'a>

§

impl<'a> !UnwindSafe for ToolContext<'a>

§

impl<'a> Forget for ToolContext<'a>

§

impl<'a> Freeze for ToolContext<'a>

§

impl<'a> Move for ToolContext<'a>

§

impl<'a> Send for ToolContext<'a>

§

impl<'a> Sync for ToolContext<'a>

§

impl<'a> Unpin for ToolContext<'a>

§

impl<'a> UnsafeUnpin for ToolContext<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more