askit_std_agents/
utils.rs1use std::vec;
2
3use agent_stream_kit::{
4 ASKit, AgentContext, AgentData, AgentError, AgentOutput, AgentSpec, AgentValue, AsAgent,
5 askit_agent, async_trait,
6};
7
8static CATEGORY: &str = "Std/Utils";
9
10static PIN_IN: &str = "in";
11static PIN_RESET: &str = "reset";
12static PIN_COUNT: &str = "count";
13
14static DISPLAY_COUNT: &str = "count";
15
16#[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.emit_config_updated(DISPLAY_COUNT, AgentValue::integer(0));
45 Ok(())
46 }
47
48 async fn process(
49 &mut self,
50 ctx: AgentContext,
51 pin: String,
52 _value: AgentValue,
53 ) -> Result<(), AgentError> {
54 if pin == PIN_RESET {
55 self.count = 0;
56 } else if pin == PIN_IN {
57 self.count += 1;
58 }
59 self.try_output(ctx, PIN_COUNT, AgentValue::integer(self.count))?;
60 self.emit_config_updated(DISPLAY_COUNT, AgentValue::integer(self.count));
61
62 Ok(())
63 }
64}