Skip to main content

HostFunction

Trait HostFunction 

Source
pub trait HostFunction: Send + Sync {
    // Required methods
    fn name(&self) -> &str;
    fn description(&self) -> &str;
    fn call<'life0, 'async_trait>(
        &'life0 self,
        args: Vec<Value>,
        kwargs: Map<String, Value>,
    ) -> Pin<Box<dyn Future<Output = Result<Value, HostFunctionError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: 'async_trait;

    // Provided method
    fn signature(&self) -> String { ... }
}
Available on crate features code and embedded-python only.
Expand description

A Rust function callable from Python scripts executed by a Monty executor.

Register implementations with MontyExecutorBuilder::function, or use MontyExecutorBuilder::function_fn for the closure-based common case.

§Example

use adk_code::{HostFunction, HostFunctionError};
use async_trait::async_trait;
use serde_json::{Map, Value, json};

struct GetWeather;

#[async_trait]
impl HostFunction for GetWeather {
    fn name(&self) -> &str {
        "get_weather"
    }

    fn description(&self) -> &str {
        "Current weather for a city."
    }

    fn signature(&self) -> String {
        "get_weather(city: str) -> dict".to_string()
    }

    async fn call(
        &self,
        args: Vec<Value>,
        _kwargs: Map<String, Value>,
    ) -> Result<Value, HostFunctionError> {
        let city = args
            .first()
            .and_then(Value::as_str)
            .ok_or_else(|| HostFunctionError::new("pass the city as the first argument"))?;
        Ok(json!({ "city": city, "temp_c": 21 }))
    }
}

Required Methods§

Source

fn name(&self) -> &str

Python-visible function name (must be a valid Python identifier).

Source

fn description(&self) -> &str

One-line description — becomes the Python function’s docstring and is surfaced to the LLM through the executor’s prompt snippet.

Source

fn call<'life0, 'async_trait>( &'life0 self, args: Vec<Value>, kwargs: Map<String, Value>, ) -> Pin<Box<dyn Future<Output = Result<Value, HostFunctionError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Invoke with positional and keyword arguments (JSON-converted).

§Errors

An Err becomes a catchable Python exception carrying the message.

Provided Methods§

Source

fn signature(&self) -> String

Optional signature rendering for the LLM prompt, e.g. "get_weather(city: str, unit: str = \"C\") -> dict". Defaults to "{name}(...)".

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§