modular_agent_core/
output.rs1use 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 {
14 fn output_raw(
19 &self,
20 ctx: AgentContext,
21 port: String,
22 value: AgentValue,
23 ) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + '_>>;
24
25 fn output<S: Into<String>>(
30 &self,
31 ctx: AgentContext,
32 port: S,
33 value: AgentValue,
34 ) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + '_>> {
35 self.output_raw(ctx, port.into(), value)
36 }
37
38 fn try_output_raw(
42 &self,
43 ctx: AgentContext,
44 port: String,
45 value: AgentValue,
46 ) -> Result<(), AgentError>;
47
48 fn try_output<S: Into<String>>(
50 &self,
51 ctx: AgentContext,
52 port: S,
53 value: AgentValue,
54 ) -> Result<(), AgentError> {
55 self.try_output_raw(ctx, port.into(), value)
56 }
57
58 fn emit_config_updated_raw(&self, key: String, value: AgentValue);
60
61 fn emit_config_updated<S: Into<String>>(&self, key: S, value: AgentValue) {
66 self.emit_config_updated_raw(key.into(), value);
67 }
68
69 fn emit_agent_spec_updated_raw(&self);
71
72 fn emit_agent_spec_updated(&self) {
77 self.emit_agent_spec_updated_raw();
78 }
79
80 fn emit_error_raw(&self, message: String);
82
83 #[allow(unused)]
87 fn emit_error<S: Into<String>>(&self, message: S) {
88 self.emit_error_raw(message.into());
89 }
90}
91
92impl<T: Agent> AgentOutput for T {
93 fn output_raw(
94 &self,
95 ctx: AgentContext,
96 port: String,
97 value: AgentValue,
98 ) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + '_>> {
99 Box::pin(async move {
100 self.ma()
101 .send_agent_out(self.id().into(), ctx, port, value)
102 .await
103 })
104 }
105
106 fn try_output_raw(
107 &self,
108 ctx: AgentContext,
109 port: String,
110 value: AgentValue,
111 ) -> Result<(), AgentError> {
112 self.ma()
113 .try_send_agent_out(self.id().into(), ctx, port, value)
114 }
115
116 fn emit_config_updated_raw(&self, key: String, value: AgentValue) {
117 self.ma()
118 .emit_agent_config_updated(self.id().to_string(), key, value);
119 }
120
121 fn emit_agent_spec_updated_raw(&self) {
122 self.ma().emit_agent_spec_updated(self.id().to_string());
123 }
124
125 fn emit_error_raw(&self, message: String) {
126 self.ma()
127 .emit_agent_error(self.id().to_string(), message);
128 }
129}