agent_stream_kit/
output.rs

1use crate::agent::Agent;
2use crate::context::AgentContext;
3use crate::error::AgentError;
4use crate::value::AgentValue;
5use std::future::Future;
6use std::pin::Pin;
7
8pub trait AgentOutput {
9    fn output_raw(
10        &self,
11        ctx: AgentContext,
12        pin: String,
13        value: AgentValue,
14    ) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + '_>>;
15
16    fn output<S: Into<String>>(
17        &self,
18        ctx: AgentContext,
19        pin: S,
20        value: AgentValue,
21    ) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + '_>> {
22        self.output_raw(ctx, pin.into(), value)
23    }
24
25    fn try_output_raw(
26        &self,
27        ctx: AgentContext,
28        pin: String,
29        value: AgentValue,
30    ) -> Result<(), AgentError>;
31
32    fn try_output<S: Into<String>>(
33        &self,
34        ctx: AgentContext,
35        pin: S,
36        value: AgentValue,
37    ) -> Result<(), AgentError> {
38        self.try_output_raw(ctx, pin.into(), value)
39    }
40
41    fn emit_config_updated_raw(&self, key: String, value: AgentValue);
42
43    fn emit_config_updated<S: Into<String>>(&self, key: S, value: AgentValue) {
44        self.emit_config_updated_raw(key.into(), value);
45    }
46
47    fn emit_agent_spec_updated_raw(&self);
48
49    fn emit_agent_spec_updated(&self) {
50        self.emit_agent_spec_updated_raw();
51    }
52
53    fn emit_error_raw(&self, message: String);
54
55    #[allow(unused)]
56    fn emit_error<S: Into<String>>(&self, message: S) {
57        self.emit_error_raw(message.into());
58    }
59}
60
61impl<T: Agent> AgentOutput for T {
62    fn output_raw(
63        &self,
64        ctx: AgentContext,
65        pin: String,
66        value: AgentValue,
67    ) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + '_>> {
68        Box::pin(async move {
69            self.askit()
70                .send_agent_out(self.id().into(), ctx, pin, value)
71                .await
72        })
73    }
74
75    fn try_output_raw(
76        &self,
77        ctx: AgentContext,
78        pin: String,
79        value: AgentValue,
80    ) -> Result<(), AgentError> {
81        self.askit()
82            .try_send_agent_out(self.id().into(), ctx, pin, value)
83    }
84
85    fn emit_config_updated_raw(&self, key: String, value: AgentValue) {
86        self.askit()
87            .emit_agent_config_updated(self.id().to_string(), key, value);
88    }
89
90    fn emit_agent_spec_updated_raw(&self) {
91        self.askit().emit_agent_spec_updated(self.id().to_string());
92    }
93
94    fn emit_error_raw(&self, message: String) {
95        self.askit()
96            .emit_agent_error(self.id().to_string(), message);
97    }
98}