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: StringUnique identifier for this run.
start_time: DateTime<Utc>When this run started.
retry_count: u32Current retry count for tool call.
max_retries: u32Maximum 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: StringName of the model being used.
model_settings: ModelSettingsCurrent model settings.
usage: RunUsageUsage statistics so far.
metadata: Option<Value>Custom metadata.
partial_output: boolWhether partial output is being generated.
Implementations§
Source§impl<Deps> RunContext<Deps>
impl<Deps> RunContext<Deps>
Sourcepub fn from_arc(deps: Arc<Deps>, model_name: impl Into<String>) -> Self
pub fn from_arc(deps: Arc<Deps>, model_name: impl Into<String>) -> Self
Create a context from existing Arc’d deps.
Sourcepub fn with_run_id(self, run_id: impl Into<String>) -> Self
pub fn with_run_id(self, run_id: impl Into<String>) -> Self
Set the run ID.
Sourcepub fn with_max_retries(self, max_retries: u32) -> Self
pub fn with_max_retries(self, max_retries: u32) -> Self
Set max retries.
Sourcepub fn with_model_settings(self, settings: ModelSettings) -> Self
pub fn with_model_settings(self, settings: ModelSettings) -> Self
Set model settings.
Sourcepub fn with_metadata(self, metadata: Value) -> Self
pub fn with_metadata(self, metadata: Value) -> Self
Set metadata.
Sourcepub fn with_tool_context(
self,
tool_name: impl Into<String>,
tool_call_id: Option<String>,
) -> Self
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.
Sourcepub fn with_partial_output(self, partial: bool) -> Self
pub fn with_partial_output(self, partial: bool) -> Self
Set partial output mode.
Sourcepub fn increment_retry(&mut self)
pub fn increment_retry(&mut self)
Increment the retry count.
Sourcepub fn elapsed_secs(&self) -> f64
pub fn elapsed_secs(&self) -> f64
Get elapsed time in seconds.
Sourcepub fn in_tool_call(&self) -> bool
pub fn in_tool_call(&self) -> bool
Check if we’re currently in a tool call.
Sourcepub fn for_tool(
&self,
tool_name: impl Into<String>,
tool_call_id: Option<String>,
) -> Self
pub fn for_tool( &self, tool_name: impl Into<String>, tool_call_id: Option<String>, ) -> Self
Create a child context for a tool call.
Sourcepub fn with_usage(self, usage: RunUsage) -> Self
pub fn with_usage(self, usage: RunUsage) -> Self
Create a copy with updated usage.
Sourcepub fn with_deps<NewDeps>(self, new_deps: NewDeps) -> RunContext<NewDeps>
pub fn with_deps<NewDeps>(self, new_deps: NewDeps) -> RunContext<NewDeps>
Replace dependencies (for testing).
Trait Implementations§
Source§impl<Deps: Clone> Clone for RunContext<Deps>
impl<Deps: Clone> Clone for RunContext<Deps>
Source§fn clone(&self) -> RunContext<Deps>
fn clone(&self) -> RunContext<Deps>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more