echo_toolkit/
echo_toolkit.rs

1use std::env;
2use thiserror::Error;
3use unifai_sdk::{
4    serde::{self, Deserialize, Serialize},
5    serde_json::json,
6    tokio,
7    toolkit::{
8        Action, ActionContext, ActionDefinition, ActionParams, ActionResult, ToolkitInfo,
9        ToolkitService,
10    },
11};
12
13struct EchoSlam;
14
15#[derive(Serialize, Deserialize)]
16#[serde(crate = "serde")]
17struct EchoSlamArgs {
18    pub content: String,
19}
20
21#[derive(Debug, Error)]
22#[error("Echo error")]
23struct EchoSlamError;
24
25impl Action for EchoSlam {
26    const NAME: &'static str = "echo";
27
28    type Error = EchoSlamError;
29    type Args = EchoSlamArgs;
30    type Output = String;
31
32    async fn definition(&self) -> ActionDefinition {
33        ActionDefinition {
34            description: "Echo the message".to_string(),
35            payload: json!({
36                "content": {
37                    "type": "string",
38                    "description": "The content to echo.",
39                    "required": true
40                }
41            }),
42            payment: None,
43        }
44    }
45
46    async fn call(
47        &self,
48        ctx: ActionContext,
49        params: ActionParams<Self::Args>,
50    ) -> Result<ActionResult<Self::Output>, Self::Error> {
51        let output = format!(
52            "You are agent <${}>, you said \"{}\".",
53            ctx.agent_id, params.payload.content
54        );
55
56        Ok(ActionResult {
57            payload: output,
58            payment: None,
59        })
60    }
61}
62
63#[tokio::main]
64async fn main() {
65    tracing_subscriber::fmt().init();
66
67    let unifai_toolkit_api_key =
68        env::var("UNIFAI_TOOLKIT_API_KEY").expect("UNIFAI_TOOLKIT_API_KEY not set");
69
70    let mut service = ToolkitService::new(&unifai_toolkit_api_key);
71
72    let info = ToolkitInfo {
73        name: "Echo Slam".to_string(),
74        description: "What's in, what's out.".to_string(),
75    };
76
77    service.update_info(info).await.unwrap();
78
79    service.add_action(EchoSlam);
80
81    let runner = service.start().await.unwrap();
82    let _ = runner.await.unwrap();
83}