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§
Required Associated Types§
Sourcetype Args: for<'a> Deserialize<'a> + Send + Sync
type Args: for<'a> Deserialize<'a> + Send + Sync
The arguments type of the action.
Required Methods§
Sourcefn definition(&self) -> impl Future<Output = ActionDefinition> + Send + Sync
fn definition(&self) -> impl Future<Output = ActionDefinition> + Send + Sync
A method returning the action definition.
Sourcefn call(
&self,
ctx: ActionContext,
params: ActionParams<Self::Args>,
) -> impl Future<Output = Result<ActionResult<Self::Output>, Self::Error>> + Send + Sync
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§
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.