askit_std_agents/
input.rs

1use std::vec;
2
3use agent_stream_kit::{
4    ASKit, Agent, AgentContext, AgentData, AgentError, AgentOutput, AgentSpec, AgentStatus,
5    AgentValue, AsAgent, askit_agent, async_trait,
6};
7
8static CATEGORY: &str = "Std/Input";
9
10static UNIT: &str = "unit";
11static BOOLEAN: &str = "boolean";
12static INTEGER: &str = "integer";
13static NUMBER: &str = "number";
14static STRING: &str = "string";
15static TEXT: &str = "text";
16static OBJECT: &str = "object";
17
18/// Unit Input
19#[askit_agent(
20    title = "Unit Input",
21    category = CATEGORY,
22    outputs = [UNIT],
23    unit_config(name = UNIT, hide_title)
24)]
25struct UnitInputAgent {
26    data: AgentData,
27}
28
29impl AsAgent for UnitInputAgent {
30    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
31        Ok(Self {
32            data: AgentData::new(askit, id, spec),
33        })
34    }
35
36    fn configs_changed(&mut self) -> Result<(), AgentError> {
37        // Since set_config is called even when the agent is not running,
38        // we need to check the status before outputting the value.
39        if *self.status() == AgentStatus::Start {
40            self.try_output(AgentContext::new(), UNIT, AgentValue::unit())?;
41        }
42
43        Ok(())
44    }
45}
46
47// Boolean Input
48#[askit_agent(
49    title = "Boolean Input",
50    category = CATEGORY,
51    inputs = [UNIT],
52    outputs = [BOOLEAN],
53    boolean_config(name = BOOLEAN, hide_title),
54)]
55struct BooleanInputAgent {
56    data: AgentData,
57}
58
59#[async_trait]
60impl AsAgent for BooleanInputAgent {
61    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
62        Ok(Self {
63            data: AgentData::new(askit, id, spec),
64        })
65    }
66
67    fn configs_changed(&mut self) -> Result<(), AgentError> {
68        if *self.status() == AgentStatus::Start {
69            let value = self.configs()?.get(BOOLEAN)?;
70            self.try_output(AgentContext::new(), BOOLEAN, value.clone())?;
71        }
72        Ok(())
73    }
74
75    async fn process(
76        &mut self,
77        ctx: AgentContext,
78        _pin: String,
79        _value: AgentValue,
80    ) -> Result<(), AgentError> {
81        let value = self.configs()?.get(BOOLEAN)?;
82        self.try_output(ctx, BOOLEAN, value.clone())
83    }
84}
85
86// Integer Input
87#[askit_agent(
88    title = "Integer Input",
89    category = CATEGORY,
90    inputs = [UNIT],
91    outputs = [INTEGER],
92    integer_config(name = INTEGER, hide_title)
93)]
94struct IntegerInputAgent {
95    data: AgentData,
96}
97
98#[async_trait]
99impl AsAgent for IntegerInputAgent {
100    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
101        Ok(Self {
102            data: AgentData::new(askit, id, spec),
103        })
104    }
105
106    fn configs_changed(&mut self) -> Result<(), AgentError> {
107        if *self.status() == AgentStatus::Start {
108            let value = self.configs()?.get(INTEGER)?;
109            self.try_output(AgentContext::new(), INTEGER, value.clone())?;
110        }
111        Ok(())
112    }
113
114    async fn process(
115        &mut self,
116        ctx: AgentContext,
117        _pin: String,
118        _value: AgentValue,
119    ) -> Result<(), AgentError> {
120        let value = self.configs()?.get(INTEGER)?;
121        self.try_output(ctx, INTEGER, value.clone())
122    }
123}
124
125// Number Input
126#[askit_agent(
127    title = "Number Input",
128    category = CATEGORY,
129    inputs = [UNIT],
130    outputs = [NUMBER],
131    number_config(name = NUMBER, hide_title)
132)]
133struct NumberInputAgent {
134    data: AgentData,
135}
136
137#[async_trait]
138impl AsAgent for NumberInputAgent {
139    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
140        Ok(Self {
141            data: AgentData::new(askit, id, spec),
142        })
143    }
144
145    fn configs_changed(&mut self) -> Result<(), AgentError> {
146        if *self.status() == AgentStatus::Start {
147            let value = self.configs()?.get(NUMBER)?;
148            self.try_output(AgentContext::new(), NUMBER, value.clone())?;
149        }
150        Ok(())
151    }
152
153    async fn process(
154        &mut self,
155        ctx: AgentContext,
156        _pin: String,
157        _value: AgentValue,
158    ) -> Result<(), AgentError> {
159        let value = self.configs()?.get(NUMBER)?;
160        self.try_output(ctx, NUMBER, value.clone())
161    }
162}
163
164// String Input
165#[askit_agent(
166    title = "String Input",
167    category = CATEGORY,
168    inputs = [UNIT],
169    outputs = [STRING],
170    string_config(name = STRING, hide_title)
171)]
172struct StringInputAgent {
173    data: AgentData,
174}
175
176#[async_trait]
177impl AsAgent for StringInputAgent {
178    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
179        Ok(Self {
180            data: AgentData::new(askit, id, spec),
181        })
182    }
183
184    fn configs_changed(&mut self) -> Result<(), AgentError> {
185        if *self.status() == AgentStatus::Start {
186            let value = self.configs()?.get(STRING)?;
187            self.try_output(AgentContext::new(), STRING, value.clone())?;
188        }
189        Ok(())
190    }
191
192    async fn process(
193        &mut self,
194        ctx: AgentContext,
195        _pin: String,
196        _value: AgentValue,
197    ) -> Result<(), AgentError> {
198        let value = self.configs()?.get(STRING)?;
199        self.try_output(ctx, STRING, value.clone())
200    }
201}
202
203// Text Input
204#[askit_agent(
205    title = "Text Input",
206    category = CATEGORY,
207    inputs = [UNIT],
208    outputs = [TEXT],
209    text_config(name = TEXT, hide_title)
210)]
211struct TextInputAgent {
212    data: AgentData,
213}
214
215#[async_trait]
216impl AsAgent for TextInputAgent {
217    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
218        Ok(Self {
219            data: AgentData::new(askit, id, spec),
220        })
221    }
222
223    fn configs_changed(&mut self) -> Result<(), AgentError> {
224        if *self.status() == AgentStatus::Start {
225            let value = self.configs()?.get(TEXT)?;
226            self.try_output(AgentContext::new(), TEXT, value.clone())?;
227        }
228        Ok(())
229    }
230
231    async fn process(
232        &mut self,
233        ctx: AgentContext,
234        _pin: String,
235        _value: AgentValue,
236    ) -> Result<(), AgentError> {
237        let value = self.configs()?.get(TEXT)?;
238        self.try_output(ctx, TEXT, value.clone())
239    }
240}
241
242// Object Input
243#[askit_agent(
244    title = "Object Input",
245    category = CATEGORY,
246    inputs = [UNIT],
247    outputs = [OBJECT],
248    object_config(name = OBJECT, hide_title)
249)]
250struct ObjectInputAgent {
251    data: AgentData,
252}
253
254#[async_trait]
255impl AsAgent for ObjectInputAgent {
256    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
257        Ok(Self {
258            data: AgentData::new(askit, id, spec),
259        })
260    }
261
262    fn configs_changed(&mut self) -> Result<(), AgentError> {
263        if *self.status() == AgentStatus::Start {
264            let value = self.configs()?.get(OBJECT)?;
265            self.try_output(AgentContext::new(), OBJECT, value.clone())?;
266        }
267        Ok(())
268    }
269
270    async fn process(
271        &mut self,
272        ctx: AgentContext,
273        _pin: String,
274        _value: AgentValue,
275    ) -> Result<(), AgentError> {
276        let value = self.configs()?.get(OBJECT)?;
277        self.try_output(ctx, OBJECT, value.clone())
278    }
279}