Skip to content

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

feat(channels): add host-owned channel handle for module and relay dispatch

Steven Enamakelb0203be07e4d
diff --git a/crates/openhuman-core/src/channels/runtime/startup.rs b/crates/openhuman-core/src/channels/runtime/startup.rs
index 28b14c2448..8cbd1235e6 100644
--- a/crates/openhuman-core/src/channels/runtime/startup.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup.rs
@@ -15,6 +15,103 @@ mod turn_parts;
use anyhow::Result;
use tinybus::module::{ModuleCallError, ModuleClient};
+#[cfg(test)]
+pub(crate) type ChannelHandle = dyn tinychannels_runtime::Channel;
+
+/// Host-owned handle to a configured channel instance. Production operations
+/// are dispatched through the loaded TinyChannels module's bus object.
+#[cfg(not(test))]
+#[derive(Clone)]
+pub(crate) enum ChannelHandle {
+ Module(module_providers::ModuleChannel),
+ Relay(std::sync::Arc<crate::channels::providers::relay::RelayChannel>),
+}
+
+#[cfg(not(test))]
+impl ChannelHandle {
+ pub(crate) fn name(&self) -> &str {
+ match self {
+ Self::Module(channel) => &channel.name,
+ Self::Relay(channel) => channel.name(),
+ }
+ }
+
+ pub(crate) fn proactive_target(&self) -> Option<String> {
+ match self {
+ Self::Module(channel) => channel.proactive_target(),
+ Self::Relay(_) => None,
+ }
+ }
+
+ pub(crate) fn capabilities(&self) -> tinychannels_bus::ChannelCapabilities {
+ match self {
+ Self::Module(channel) => channel.capabilities(),
+ Self::Relay(_) => tinychannels_bus::ChannelCapabilities::unknown(),
+ }
+ }
+
+ pub(crate) fn supports_reactions(&self) -> bool {
+ matches!(self, Self::Module(channel) if channel.interaction.supports_reactions)
+ }
+
+ pub(crate) async fn send(
+ &self,
+ message: &tinychannels_bus::SendMessage,
+ ) -> anyhow::Result<()> {
+ match self {
+ Self::Module(channel) => channel.send_prepared(message).await,
+ Self::Relay(channel) => channel.send(message).await,
+ }
+ }
+
+ pub(crate) async fn send_with_outbound_intent(
+ &self,
+ message: &tinychannels_bus::SendMessage,
+ ) -> anyhow::Result<()> {
+ self.send(message).await
+ }
+
+ pub(crate) async fn start_typing(&self, recipient: &str) -> anyhow::Result<()> {
+ match self {
+ Self::Module(channel) => channel
+ .interact(tinychannels_bus::ChannelInteraction::StartTyping {
+ recipient: recipient.to_owned(),
+ })
+ .await,
+ Self::Relay(_) => Ok(()),
+ }
+ }
+
+ pub(crate) async fn stop_typing(&self, recipient: &str) -> anyhow::Result<()> {
+ match self {
+ Self::Module(channel) => channel
+ .interact(tinychannels_bus::ChannelInteraction::StopTyping {
+ recipient: recipient.to_owned(),
+ })
+ .await,
+ Self::Relay(_) => Ok(()),
+ }
+ }
+
+ pub(crate) async fn begin_typing_refresh(
+ &self,
+ recipient: &str,
+ interval_secs: u64,
+ ) -> anyhow::Result<Option<String>> {
+ match self {
+ Self::Module(channel) => channel.begin_typing_refresh(recipient, interval_secs).await,
+ Self::Relay(_) => Ok(None),
+ }
+ }
+
+ pub(crate) async fn end_typing_refresh(&self, handle: &str) -> anyhow::Result<()> {
+ match self {
+ Self::Module(channel) => channel.end_typing_refresh(handle).await,
+ Self::Relay(_) => Ok(()),
+ }
+ }
+}
+
/// Ask the owning module to prepare effective execution limits for this host
/// runtime. A malformed response fails closed and reports only the module ID.
pub(crate) async fn prepare_runtime_limits(
diff --git a/crates/openhuman-core/src/channels/runtime/startup/module_providers.rs b/crates/openhuman-core/src/channels/runtime/startup/module_providers.rs
index 200ff5bf51..bd8f597e20 100644
--- a/crates/openhuman-core/src/channels/runtime/startup/module_providers.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/module_providers.rs
@@ -16,6 +16,7 @@ use tinychannels_bus::module_runtime::{
use tinychannels_bus::module_supervision::ChannelSupervisionPolicy;
use tinychannels_bus::{ChannelInteraction, ChannelInteractionResult};
use tinychannels_bus::{ChannelMessage, SendMessage};
+#[cfg(test)]
use tinychannels_runtime::Channel;
use tokio::sync::mpsc;
@@ -27,7 +28,10 @@ pub(super) struct ModuleChannelRuntime {
proxy: tinychannels_bus::HttpProxyPolicy,
infos: Vec<ConfiguredChannelInfo>,
relay: Option<tinychannels_bus::module_relay::RelayReservation>,
+ #[cfg(test)]
channels: Arc<HashMap<String, Arc<dyn Channel>>>,
+ #[cfg(not(test))]
+ channels: Arc<HashMap<String, Arc<super::ChannelHandle>>>,
closed: bool,
}
@@ -35,7 +39,7 @@ pub(super) struct ModuleChannelRuntime {
pub(super) struct ModuleChannel {
pub(super) name: String,
pub(super) object: ModuleObjectClient,
- interaction: tinychannels_bus::ChannelInteractionInfo,
+ pub(super) interaction: tinychannels_bus::ChannelInteractionInfo,
relay: Option<tinychannels_bus::module_relay::RelayReservation>,
relay_fronted: bool,
}
@@ -137,7 +141,10 @@ impl ModuleChannelRuntime {
.any(|identity| identity.platform == channel)
})
};
- let mut channels = HashMap::new();
+ #[cfg(test)]
+ let mut channels: HashMap<String, Arc<dyn Channel>> = HashMap::new();
+ #[cfg(not(test))]
+ let mut channels: HashMap<String, Arc<super::ChannelHandle>> = HashMap::new();
let mut progressive = HashMap::new();
for info in &infos {
let channel = ModuleChannel {
@@ -148,7 +155,13 @@ impl ModuleChannelRuntime {
relay_fronted: relay_fronts(&info.name),
};
let channel = Arc::new(channel);
+ #[cfg(test)]
channels.insert(info.name.clone(), channel.clone() as Arc<dyn Channel>);
+ #[cfg(not(test))]
+ channels.insert(
+ info.name.clone(),
+ Arc::new(super::ChannelHandle::Module((*channel).clone())),
+ );
let progressive_channel: Arc<dyn ProgressiveChannelTransport> = channel.clone();
progressive.insert(info.name.clone(), Arc::downgrade(&progressive_channel));
}
@@ -169,10 +182,16 @@ impl ModuleChannelRuntime {
})
}
+ #[cfg(test)]
pub(super) fn channels(&self) -> Arc<HashMap<String, Arc<dyn Channel>>> {
Arc::clone(&self.channels)
}
+ #[cfg(not(test))]
+ pub(super) fn channels(&self) -> Arc<HashMap<String, Arc<super::ChannelHandle>>> {
+ Arc::clone(&self.channels)
+ }
+
pub(super) fn module_objects(&self) -> Arc<HashMap<String, ModuleObjectClient>> {
Arc::new(
self.infos
@@ -333,8 +352,7 @@ fn channel_proxy(config: &Config) -> tinychannels_bus::HttpProxyPolicy {
}
}
-#[async_trait]
-impl Channel for ModuleChannel {
+impl ModuleChannel {
fn name(&self) -> &str {
&self.name
}
@@ -343,7 +361,7 @@ impl Channel for ModuleChannel {
self.interaction.proactive_target.clone()
}
- async fn send(&self, message: &SendMessage) -> anyhow::Result<()> {
+ pub(super) async fn send_prepared(&self, message: &SendMessage) -> anyhow::Result<()> {
self.send_prepared(message).await
}