Skip to main content

askit_std_agents/
display.rs

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