Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(inbound): add host and ops modules for inbound handling

Steven Enamakel38e6a095e988
diff --git a/crates/openhuman-core/src/inbound/host.rs b/crates/openhuman-core/src/inbound/host.rs
new file mode 100644
index 0000000000..ddf0ff9271
--- /dev/null
+++ b/crates/openhuman-core/src/inbound/host.rs
@@ -0,0 +1,82 @@
+//! The host callbacks the `tinyeventqueues` module calls:
+//! `ai.tinyhumans.tinyeventqueues.Host` (`ClassifySalience`, `Deliver`).
+//!
+//! Served on the module runtime's connection before any module loads
+//! (`modules::runtime::install_callbacks`), when the `eventqueues` component
+//! is on. The module declares the interface optional: without it, deliveries
+//! back off until it is served.
+
+use std::sync::Arc;
+use std::time::Duration;
+
+use tinybus::ObjectPath;
+use tinyeventqueues_bus::names::host::{INTERFACE as NAME, OBJECT_PATH as PATH};
+use tinyeventqueues_bus::{
+ ClassifySalienceRequest, ClassifySalienceResponse, DeliverRequest, DeliverResponse,
+};
+
+use super::deliver::Deliverer;
+use crate::core::runtime::DispatchCtx;
+use crate::triage::LiveTriage;
+
+/// The served callback object.
+#[derive(Clone)]
+pub(crate) struct InboundHost {
+ deliverer: Arc<Deliverer>,
+}
+
+impl InboundHost {
+ pub(crate) fn new(deliverer: Arc<Deliverer>) -> Self {
+ Self { deliverer }
+ }
+}
+
+#[tinybus::interface(name = "ai.tinyhumans.tinyeventqueues.Host")]
+impl InboundHost {
+ /// Classify a borderline event by triaging it. A failure keeps the class
+ /// the module's score gave the event.
+ async fn classify_salience(
+ &self,
+ request: ClassifySalienceRequest,
+ ) -> tinybus::Result<ClassifySalienceResponse> {
+ self.deliverer
+ .classify(request)
+ .await
+ .map_err(|message| tinybus::Error::MethodFailed {
+ name: "ai.tinyhumans.tinyeventqueues.Error.Host".to_string(),
+ message,
+ })
+ }
+
+ /// Deliver one due turn: triage it and answer with the ack semantics.
+ async fn deliver(&self, request: DeliverRequest) -> tinybus::Result<DeliverResponse> {
+ Ok(self.deliverer.deliver(request).await)
+ }
+}
+
+/// Serve the callbacks on `connection`, triaging under the host's own context.
+pub(crate) async fn install(connection: &tinybus::Connection) -> tinybus::Result<()> {
+ let ctx = DispatchCtx::host_default();
+ let timeout_ms = match ctx.load_config().await {
+ Ok(config) => config.inbound.triage_timeout_ms,
+ Err(error) => {
+ tracing::debug!(error = %error, "[inbound] config unavailable; default triage timeout");
+ crate::config::schema::InboundConfig::default().triage_timeout_ms
+ }
+ };
+ let deliverer = Deliverer::new(
+ Arc::new(LiveTriage),
+ ctx,
+ Duration::from_millis(timeout_ms),
+ );
+ connection
+ .serve_at(ObjectPath::new(PATH)?, InboundHost::new(Arc::new(deliverer)))
+ .await?;
+ connection.request_name(NAME).await?;
+ tracing::debug!("[inbound] serving the tinyeventqueues host callbacks");
+ Ok(())
+}
+
+#[cfg(test)]
+#[path = "host_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/inbound/ops.rs b/crates/openhuman-core/src/inbound/ops.rs
new file mode 100644
index 0000000000..c98a23a3cd
--- /dev/null
+++ b/crates/openhuman-core/src/inbound/ops.rs
@@ -0,0 +1,99 @@
+//! Operator surface of the inbound event queues: depths and counts, the
+//! dead-letter queue, and redriving one item.
+
+use serde::Serialize;
+use tinybus::module::ModuleClient;
+use tinyeventqueues_bus::{DeadItem, StatsResponse};
+
+use crate::config::Config;
+use crate::core::Outcome;
+use crate::modules::event_queues;
+
+/// The most dead letters one `inbound.dead` call returns.
+pub const MAX_DEAD_LIMIT: u32 = 500;
+
+/// The dead letters `inbound.dead` returns.
+#[derive(Debug, Clone, Serialize)]
+pub struct DeadLetters {
+ /// Parked items, oldest first.
+ pub items: Vec<DeadItem>,
+}
+
+/// The result of `inbound.redrive`.
+#[derive(Debug, Clone, Serialize)]
+pub struct Redriven {
+ /// The redriven item's id.
+ pub id: String,
+ /// `false` when the item was already gone.
+ pub redriven: bool,
+}
+
+fn require(config: &Config) -> Result<(), String> {
+ crate::core::runtime::components::require_config(config, super::settings::COMPONENT)
+}
+
+/// Queue depths and outcome counts.
+///
+/// # Errors
+///
+/// The component is off, or the module is unavailable or failed the call.
+pub async fn stats(config: &Config, client: &ModuleClient) -> Result<Outcome<StatsResponse>, String> {
+ require(config)?;
+ let stats = event_queues::stats(client)
+ .await
+ .map_err(|error| format!("inbound.stats: {error}"))?;
+ Ok(Outcome::single_log(stats, "inbound queue stats read"))
+}
+
+/// Up to `limit` dead letters (capped at [`MAX_DEAD_LIMIT`]).
+///
+/// # Errors
+///
+/// As [`stats`].
+pub async fn dead(
+ config: &Config,
+ client: &ModuleClient,
+ limit: Option<u32>,
+) -> Result<Outcome<DeadLetters>, String> {
+ require(config)?;
+ let limit = limit.unwrap_or(50).min(MAX_DEAD_LIMIT);
+ let items = event_queues::list_dead(client, limit)
+ .await
+ .map_err(|error| format!("inbound.dead: {error}"))?;
+ let count = items.len();
+ Ok(Outcome::single_log(
+ DeadLetters { items },
+ format!("{count} dead letter(s)"),
+ ))
+}
+
+/// Put dead letter `id` back into the pipeline.
+///
+/// # Errors
+///
+/// As [`stats`]; an empty or unknown id is refused.
+pub async fn redrive(
+ config: &Config,
+ client: &ModuleClient,
+ id: &str,
+) -> Result<Outcome<Redriven>, String> {
+ require(config)?;
+ if id.trim().is_empty() {
+ return Err("inbound.redrive: id is required".to_owned());
+ }
+ let redriven = event_queues::redrive(client, id)
+ .await
+ .map_err(|error| format!("inbound.redrive: {error}"))?;
+ tracing::info!(id = %id, redriven, "[inbound] redrive");
+ Ok(Outcome::single_log(
+ Redriven {
+ id: id.to_owned(),
+ redriven,
+ },
+ if redriven { "redriven" } else { "already gone" },
+ ))
+}
+
+#[cfg(test)]
+#[path = "ops_tests.rs"]
+mod tests;