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