Trait Action

Source
pub trait Action:
    Sized
    + Send
    + Sync {
    type Error: Error + Send + Sync + 'static;
    type Args: for<'a> Deserialize<'a> + Send + Sync;
    type Output: Serialize;

    const NAME: &'static str;

    // Required methods
    fn definition(&self) -> impl Future<Output = ActionDefinition> + Send + Sync;
    fn call(
        &self,
        ctx: ActionContext,
        params: ActionParams<Self::Args>,
    ) -> impl Future<Output = Result<ActionResult<Self::Output>, Self::Error>> + Send + Sync;

    // Provided method
    fn name(&self) -> String { ... }
}
Expand description

Trait that represents an action of Toolkit

§Example

use serde::{Deserialize, Serialize};
use serde_json::json;
use thiserror::Error;
use unifai_sdk::{toolkit::{Action, ActionContext, ActionDefinition, ActionParams, ActionResult}};

struct EchoSlam;

#[derive(Serialize, Deserialize)]
struct EchoSlamArgs {
    pub content: String,
}

#[derive(Debug, Error)]
#[error("Echo error")]
struct EchoSlamError;

impl Action for EchoSlam {
    const NAME: &'static str = "echo";

    type Error = EchoSlamError;
    type Args = EchoSlamArgs;
    type Output = String;

    async fn definition(&self) -> ActionDefinition {
        ActionDefinition {
            description: "Echo the message".to_string(),
            payload: json!({
                "content": {
                    "type": "string",
                    "description": "The content to echo.",
                    "required": true
                }
            }),
            payment: None,
        }
    }

    async fn call(
        &self,
        ctx: ActionContext,
        params: ActionParams<Self::Args>,
    ) -> Result<ActionResult<Self::Output>, Self::Error> {
        let output = format!(
            "You are agent <${}>, you said \"{}\".",
            ctx.agent_id, params.payload.content
        );

        Ok(ActionResult {
            payload: output,
            payment: None,
        })
    }
}

Required Associated Constants§

Source

const NAME: &'static str

The name of the action. This name should be unique.

Required Associated Types§

Source

type Error: Error + Send + Sync + 'static

The error type of the action.

Source

type Args: for<'a> Deserialize<'a> + Send + Sync

The arguments type of the action.

Source

type Output: Serialize

The output type of the action.

Required Methods§

Source

fn definition(&self) -> impl Future<Output = ActionDefinition> + Send + Sync

A method returning the action definition.

Source

fn call( &self, ctx: ActionContext, params: ActionParams<Self::Args>, ) -> impl Future<Output = Result<ActionResult<Self::Output>, Self::Error>> + Send + Sync

The action execution method.

Provided Methods§

Source

fn name(&self) -> String

A method returning the name of the action.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§