Skip to main content

RunContext

Struct RunContext 

Source
pub struct RunContext<Deps = ()> {
    pub deps: Arc<Deps>,
    pub run_id: String,
    pub start_time: DateTime<Utc>,
    pub retry_count: u32,
    pub max_retries: u32,
    pub tool_name: Option<String>,
    pub tool_call_id: Option<String>,
    pub model_name: String,
    pub model_settings: ModelSettings,
    pub usage: RunUsage,
    pub metadata: Option<Value>,
    pub partial_output: bool,
}
Expand description

Context passed to tools during execution.

The RunContext provides tools with access to:

  • User-provided dependencies (database connections, API clients, etc.)
  • Run metadata (ID, start time, model name)
  • Retry information
  • Current usage statistics

§Type Parameters

  • Deps: The type of dependencies available to tools. Defaults to ().

§Example

use serdes_ai_tools::RunContext;
use std::sync::Arc;

struct MyDeps {
    api_key: String,
}

let deps = MyDeps { api_key: "secret".into() };
let ctx = RunContext::new(deps, "gpt-4");

// Access deps in a tool
assert_eq!(ctx.deps.api_key, "secret");

Fields§

§deps: Arc<Deps>

User-provided dependencies.

§run_id: String

Unique identifier for this run.

§start_time: DateTime<Utc>

When this run started.

§retry_count: u32

Current retry count for tool call.

§max_retries: u32

Maximum retries allowed.

§tool_name: Option<String>

Name of the tool being called (if in a tool call).

§tool_call_id: Option<String>

Tool call ID (if in a tool call).

§model_name: String

Name of the model being used.

§model_settings: ModelSettings

Current model settings.

§usage: RunUsage

Usage statistics so far.

§metadata: Option<Value>

Custom metadata.

§partial_output: bool

Whether partial output is being generated.

Implementations§

Source§

impl<Deps> RunContext<Deps>

Source

pub fn new(deps: Deps, model_name: impl Into<String>) -> Self

Create a new run context.

Source

pub fn from_arc(deps: Arc<Deps>, model_name: impl Into<String>) -> Self

Create a context from existing Arc’d deps.

Source

pub fn with_run_id(self, run_id: impl Into<String>) -> Self

Set the run ID.

Source

pub fn with_max_retries(self, max_retries: u32) -> Self

Set max retries.

Source

pub fn with_model_settings(self, settings: ModelSettings) -> Self

Set model settings.

Source

pub fn with_metadata(self, metadata: Value) -> Self

Set metadata.

Source

pub fn with_tool_context( self, tool_name: impl Into<String>, tool_call_id: Option<String>, ) -> Self

Set the tool context for a tool call.

Source

pub fn with_partial_output(self, partial: bool) -> Self

Set partial output mode.

Source

pub fn increment_retry(&mut self)

Increment the retry count.

Source

pub fn can_retry(&self) -> bool

Check if we can retry.

Source

pub fn elapsed(&self) -> Duration

Get elapsed time since start.

Source

pub fn elapsed_secs(&self) -> f64

Get elapsed time in seconds.

Source

pub fn in_tool_call(&self) -> bool

Check if we’re currently in a tool call.

Source

pub fn for_tool( &self, tool_name: impl Into<String>, tool_call_id: Option<String>, ) -> Self

Create a child context for a tool call.

Source

pub fn with_usage(self, usage: RunUsage) -> Self

Create a copy with updated usage.

Source

pub fn with_deps<NewDeps>(self, new_deps: NewDeps) -> RunContext<NewDeps>

Replace dependencies (for testing).

Source§

impl RunContext<()>

Source

pub fn minimal(model_name: impl Into<String>) -> Self

Create a minimal context without dependencies.

Trait Implementations§

Source§

impl<Deps: Clone> Clone for RunContext<Deps>

Source§

fn clone(&self) -> RunContext<Deps>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Deps: Debug> Debug for RunContext<Deps>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Deps: Default> Default for RunContext<Deps>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Deps> Freeze for RunContext<Deps>

§

impl<Deps> RefUnwindSafe for RunContext<Deps>
where Deps: RefUnwindSafe,

§

impl<Deps> Send for RunContext<Deps>
where Deps: Sync + Send,

§

impl<Deps> Sync for RunContext<Deps>
where Deps: Sync + Send,

§

impl<Deps> Unpin for RunContext<Deps>

§

impl<Deps> UnwindSafe for RunContext<Deps>
where Deps: RefUnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.