Skip to main content

askit_std_agents/
utils.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};
7
8const CATEGORY: &str = "Std/Utils";
9
10const PIN_IN: &str = "in";
11const PIN_RESET: &str = "reset";
12const PIN_COUNT: &str = "count";
13
14const DISPLAY_COUNT: &str = "count";
15
16/// Counter
17#[askit_agent(
18    title = "Counter",
19    category = CATEGORY,
20    inputs = [PIN_IN, PIN_RESET],
21    outputs = [PIN_COUNT],
22    integer_config(
23        name = DISPLAY_COUNT,
24        readonly,
25        hide_title,
26    )
27)]
28struct CounterAgent {
29    data: AgentData,
30    count: i64,
31}
32
33#[async_trait]
34impl AsAgent for CounterAgent {
35    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
36        Ok(Self {
37            data: AgentData::new(askit, id, spec),
38            count: 0,
39        })
40    }
41
42    async fn start(&mut self) -> Result<(), AgentError> {
43        self.count = 0;
44        self.set_config(DISPLAY_COUNT.to_string(), AgentValue::integer(0))?;
45        self.emit_config_updated(DISPLAY_COUNT, AgentValue::integer(0));
46        Ok(())
47    }
48
49    async fn process(
50        &mut self,
51        ctx: AgentContext,
52        pin: String,
53        _value: AgentValue,
54    ) -> Result<(), AgentError> {
55        if pin == PIN_RESET {
56            self.count = 0;
57        } else if pin == PIN_IN {
58            self.count += 1;
59        }
60        self.set_config(DISPLAY_COUNT.to_string(), AgentValue::integer(self.count))?;
61        self.output(ctx, PIN_COUNT, AgentValue::integer(self.count))
62            .await?;
63        self.emit_config_updated(DISPLAY_COUNT, AgentValue::integer(self.count));
64
65        Ok(())
66    }
67}