agent-stream-kit 0.19.0

Agent Stream Kit
Documentation
use crate::agent::Agent;
use crate::context::AgentContext;
use crate::error::AgentError;
use crate::value::AgentValue;
use std::future::Future;
use std::pin::Pin;

pub trait AgentOutput {
    fn output_raw(
        &self,
        ctx: AgentContext,
        pin: String,
        value: AgentValue,
    ) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + '_>>;

    fn output<S: Into<String>>(
        &self,
        ctx: AgentContext,
        pin: S,
        value: AgentValue,
    ) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + '_>> {
        self.output_raw(ctx, pin.into(), value)
    }

    fn try_output_raw(
        &self,
        ctx: AgentContext,
        pin: String,
        value: AgentValue,
    ) -> Result<(), AgentError>;

    fn try_output<S: Into<String>>(
        &self,
        ctx: AgentContext,
        pin: S,
        value: AgentValue,
    ) -> Result<(), AgentError> {
        self.try_output_raw(ctx, pin.into(), value)
    }

    fn emit_config_updated_raw(&self, key: String, value: AgentValue);

    fn emit_config_updated<S: Into<String>>(&self, key: S, value: AgentValue) {
        self.emit_config_updated_raw(key.into(), value);
    }

    fn emit_agent_spec_updated_raw(&self);

    fn emit_agent_spec_updated(&self) {
        self.emit_agent_spec_updated_raw();
    }

    fn emit_error_raw(&self, message: String);

    #[allow(unused)]
    fn emit_error<S: Into<String>>(&self, message: S) {
        self.emit_error_raw(message.into());
    }
}

impl<T: Agent> AgentOutput for T {
    fn output_raw(
        &self,
        ctx: AgentContext,
        pin: String,
        value: AgentValue,
    ) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + '_>> {
        Box::pin(async move {
            self.askit()
                .send_agent_out(self.id().into(), ctx, pin, value)
                .await
        })
    }

    fn try_output_raw(
        &self,
        ctx: AgentContext,
        pin: String,
        value: AgentValue,
    ) -> Result<(), AgentError> {
        self.askit()
            .try_send_agent_out(self.id().into(), ctx, pin, value)
    }

    fn emit_config_updated_raw(&self, key: String, value: AgentValue) {
        self.askit()
            .emit_agent_config_updated(self.id().to_string(), key, value);
    }

    fn emit_agent_spec_updated_raw(&self) {
        self.askit().emit_agent_spec_updated(self.id().to_string());
    }

    fn emit_error_raw(&self, message: String) {
        self.askit()
            .emit_agent_error(self.id().to_string(), message);
    }
}