Skip to main content

Runtime

Struct Runtime 

Source
pub struct Runtime {
    pub model: Option<String>,
    pub api_key: Option<String>,
    pub used: bool,
    /* private fields */
}
Expand description

Runtime - Main execution environment for agentic functions

Fields§

§model: Option<String>§api_key: Option<String>§used: bool

Implementations§

Source§

impl Runtime

Source

pub fn new() -> Self

Create a new Runtime with default memory storage

Source

pub fn with_config(model: Option<String>, api_key: Option<String>) -> Self

Create a Runtime with specific configuration

Source

pub async fn register_tool( &self, name: impl Into<String>, tool_fn: ToolFunction, tool_type: Option<String>, ) -> Result<()>

Register a tool function

§Arguments
  • name - Tool name for LLM
  • tool_fn - Sync or async function
  • tool_type - Optional type name for marshalling
Source

pub async fn register_tool_with_schema( &self, name: impl Into<String>, description: impl Into<String>, schema: Value, tool_fn: ToolFunction, ) -> Result<()>

Register a tool function with schema information

This method is useful when you have a tool struct that provides its schema via #[derive(Tool)], and you want to register it with a handler function.

§Arguments
  • name - Tool name for LLM
  • description - Tool description
  • schema - JSON schema for the tool parameters (from Tool::schema())
  • tool_fn - Sync or async function to handle the tool call
§Example
ⓘ
runtime.register_tool_with_schema(
    MyTool::tool_name(),
    MyTool::description(),
    MyTool::schema(),
    ToolFunction::Sync(Box::new(|args| {
        // Handle the tool call
        Ok("result".to_string())
    })),
).await?;
Source

pub async fn tool<T, F>(&self, handler: F, name: Option<&str>) -> Result<()>
where T: Deserializable + Serialize + 'static, F: Fn(T) -> BoxFuture<'static, Result<String>> + Send + Sync + 'static,

Register a tool with type and handler (Python: runtime.tool(fn, name=…, tool_type=…))

This is the primary API for registering tools for native tool calling. The type parameter T must implement Deserializable. The handler receives the typed struct directly - deserialization is handled automatically.

§Arguments
  • handler - Async function that receives the typed struct T
  • name - Optional tool name (defaults to type name if None)
§Example
ⓘ
#[derive(Deserialize, Serialize)]
struct WeatherQuery {
    city: String,
    units: Option<String>,
}
impl Deserializable for WeatherQuery { ... }

// Handler receives WeatherQuery directly, not raw JSON
runtime.tool::<WeatherQuery, _>(|query| Box::pin(async move {
    Ok(format!("Weather in {}: Sunny", query.city))
}), Some("get_weather")).await?;
Source

pub async fn get_parser(&self) -> NativeToolParser

Get a NativeToolParser instance with all registered tool constructors

This parser can be used during streaming to dynamically construct ParsedTool instances from tool call deltas.

Source

pub async fn get_tool_schemas(&self) -> HashMap<String, ToolSchemaInfo>

Get tool schema information for all registered tools

Source

pub async fn run(&mut self, params: RunParams) -> Result<Value>

Execute a non-streaming LLM call

Source

pub async fn run_stream( &mut self, params: RunParams, ) -> Result<impl Stream<Item = Result<(String, Value)>>>

Execute a streaming LLM call

Returns an async stream of (chunk_type, chunk_value) tuples

Source

pub fn is_tool_call(&self, result: &Value) -> bool

Check if a result is a tool call

Source

pub fn get_tool_name(&self, result: &Value) -> Option<String>

Get tool name from result

Source

pub async fn execute_tool(&self, tool_result: &Value) -> Result<String>

Execute a tool call

Source

pub async fn raw_response(&self) -> String

Get accumulated raw response

Source

pub async fn reasoning(&self) -> String

Get accumulated reasoning

Source

pub async fn reasoning_segments(&self) -> Vec<ReasoningSegment>

Get reasoning segments

Source

pub async fn clear_raw_response(&self)

Clear raw response accumulator

Source

pub async fn clear_reasoning(&self)

Clear reasoning accumulator

Source

pub async fn clear_reasoning_segments(&self)

Clear reasoning segments

Source

pub async fn set_default_prompt(&self, prompt: impl Into<String>)

Set default prompt (from function docstring)

Source

pub async fn set_return_type(&self, type_name: Option<String>)

Set return type

Source

pub async fn set_current_call_args(&self, args: Option<HashMap<String, Value>>)

Set current call arguments (for prompt enhancement)

Trait Implementations§

Source§

impl Default for Runtime

Source§

fn default() -> Self

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

Auto Trait Implementations§

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> ErasedDestructor for T
where T: 'static,

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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