askit_std_agents/
display.rs

1use std::vec;
2
3use agent_stream_kit::{
4    ASKit, AgentConfigs, AgentContext, AgentError, AgentOutput, AgentValue, AsAgent, AgentData,
5    async_trait,
6};
7use askit_macros::askit_agent;
8
9static CATEGORY: &str = "Std/Display";
10static DISPLAY_VALUE: &str = "value";
11
12// Display Value
13#[askit_agent(
14    title = "Display Value",
15    category = CATEGORY,
16    inputs = ["*"],
17    any_display(name = DISPLAY_VALUE, hide_title)
18)]
19struct DisplayValueAgent {
20    data: AgentData,
21}
22
23#[async_trait]
24impl AsAgent for DisplayValueAgent {
25    fn new(
26        askit: ASKit,
27        id: String,
28        def_name: String,
29        config: Option<AgentConfigs>,
30    ) -> Result<Self, AgentError> {
31        Ok(Self {
32            data: AgentData::new(askit, id, def_name, config),
33        })
34    }
35
36    async fn start(&mut self) -> Result<(), AgentError> {
37        Ok(())
38    }
39
40    async fn process(
41        &mut self,
42        _ctx: AgentContext,
43        _pin: String,
44        value: AgentValue,
45    ) -> Result<(), AgentError> {
46        self.emit_display(DISPLAY_VALUE, value);
47        Ok(())
48    }
49}
50
51// Debug Value
52#[askit_agent(
53    title = "Debug Value",
54    category = CATEGORY,
55    inputs = ["*"],
56    object_display(name = DISPLAY_VALUE, hide_title)
57)]
58struct DebugValueAgent {
59    data: AgentData,
60}
61
62#[async_trait]
63impl AsAgent for DebugValueAgent {
64    fn new(
65        askit: ASKit,
66        id: String,
67        def_name: String,
68        config: Option<AgentConfigs>,
69    ) -> Result<Self, AgentError> {
70        Ok(Self {
71            data: AgentData::new(askit, id, def_name, config),
72        })
73    }
74
75    async fn process(
76        &mut self,
77        ctx: AgentContext,
78        _pin: String,
79        value: AgentValue,
80    ) -> Result<(), AgentError> {
81        let value = AgentValue::object([("value".to_string(), value)].into());
82        let ctx_json =
83            serde_json::to_value(&ctx).map_err(|e| AgentError::InvalidValue(e.to_string()))?;
84        let ctx = AgentValue::from_json(ctx_json)?;
85        let debug_value =
86            AgentValue::object([("ctx".to_string(), ctx), ("value".to_string(), value)].into());
87        self.emit_display(DISPLAY_VALUE, debug_value);
88        Ok(())
89    }
90}