askit_std_agents/
display.rs

1use 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/Display";
9static DISPLAY_VALUE: &str = "value";
10
11// Display Value
12#[askit_agent(
13    title = "Display Value",
14    category = CATEGORY,
15    inputs = ["*"],
16    any_display(name = DISPLAY_VALUE, hide_title)
17)]
18struct DisplayValueAgent {
19    data: AgentData,
20}
21
22#[async_trait]
23impl AsAgent for DisplayValueAgent {
24    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
25        Ok(Self {
26            data: AgentData::new(askit, id, spec),
27        })
28    }
29
30    async fn start(&mut self) -> Result<(), AgentError> {
31        Ok(())
32    }
33
34    async fn process(
35        &mut self,
36        _ctx: AgentContext,
37        _pin: String,
38        value: AgentValue,
39    ) -> Result<(), AgentError> {
40        self.emit_display(DISPLAY_VALUE, value);
41        Ok(())
42    }
43}
44
45// Debug Value
46#[askit_agent(
47    title = "Debug Value",
48    category = CATEGORY,
49    inputs = ["*"],
50    object_display(name = DISPLAY_VALUE, hide_title)
51)]
52struct DebugValueAgent {
53    data: AgentData,
54}
55
56#[async_trait]
57impl AsAgent for DebugValueAgent {
58    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
59        Ok(Self {
60            data: AgentData::new(askit, id, spec),
61        })
62    }
63
64    async fn process(
65        &mut self,
66        ctx: AgentContext,
67        _pin: String,
68        value: AgentValue,
69    ) -> Result<(), AgentError> {
70        let ctx_json =
71            serde_json::to_value(&ctx).map_err(|e| AgentError::InvalidValue(e.to_string()))?;
72        let ctx = AgentValue::from_json(ctx_json)?;
73        let debug_value =
74            AgentValue::object([("ctx".to_string(), ctx), ("value".to_string(), value)].into());
75        self.emit_display(DISPLAY_VALUE, debug_value);
76        Ok(())
77    }
78}