Module context

Module context 

Source
Expand description

What a tool knows about the call it serves.

A tool is usually a pure function of its arguments. Some need more: the session State (the caller’s account, a slot captured earlier), the call’s id (for logs and idempotency), or a way to notice the user barged in and stop early. The runtime hands every call a ToolContext. A tool that wants it asks for it:

  • #[tool]: add a ctx: ToolContext parameter. It is filled by the runtime and never shown to the model.
  • A closure: ContextTool, or T::contextual in the fluent crate.
  • A hand-written ToolFunction: override call_with_context.
use gemini_adk_rs::tool::{ContextTool, ToolContext, ToolFunction};
use gemini_adk_rs::State;
use serde_json::json;

let balance = ContextTool::new("balance", "The caller's balance", None, |_args, ctx: ToolContext| async move {
    let account: String = ctx.state.get("account_id").unwrap_or_default();
    Ok(json!({ "account": account, "cents": 1200 }))
});

let state = State::new();
state.set("account_id", "A-17").unwrap();
let out = balance
    .call_with_context(json!({}), ToolContext::new(state).with_call_id("call-1"))
    .await
    .unwrap();
assert_eq!(out["account"], "A-17");

Structs§

ContextTool
A tool from a closure that receives the ToolContext.
ToolContext
The session a tool call runs in.