Skip to content

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

refactor(channels): use ChannelHandle instead of dyn Channel

Steven Enamakelce80d00367b5
diff --git a/crates/openhuman-core/src/channels/context.rs b/crates/openhuman-core/src/channels/context.rs
index cf18bb176f..4356e68409 100644
--- a/crates/openhuman-core/src/channels/context.rs
+++ b/crates/openhuman-core/src/channels/context.rs
@@ -28,7 +28,7 @@ pub(crate) struct ChannelRuntimeContext {
/// Explicit durable conversation for a hosted relay; ordinary channels
/// use their existing per-sender history and have no relay thread.
pub(crate) relay_thread: Option<String>,
- pub(crate) channels_by_name: Arc<HashMap<String, Arc<dyn super::Channel>>>,
+ pub(crate) channels_by_name: Arc<HashMap<String, Arc<super::runtime::ChannelHandle>>>,
/// Compiled TinyChannels objects for configured providers. Host channel
/// runners use these objects for module-owned progressive output.
pub(crate) module_objects: Arc<HashMap<String, tinybus::module::ModuleObjectClient>>,
diff --git a/crates/openhuman-core/src/channels/host/channel_events.rs b/crates/openhuman-core/src/channels/host/channel_events.rs
index 826f71ed79..d007da6036 100644
--- a/crates/openhuman-core/src/channels/host/channel_events.rs
+++ b/crates/openhuman-core/src/channels/host/channel_events.rs
@@ -1,6 +1,9 @@
//! Host-owned approval routing and turn events over the channel module.
use crate::channels::capabilities::capabilities_for;
-use crate::channels::{Channel, ChannelSendExt, SendMessage};
+use crate::channels::runtime::ChannelHandle;
+use crate::channels::SendMessage;
+#[cfg(test)]
+use crate::channels::ChannelSendExt;
use crate::core::events::DomainEvent;
use async_trait::async_trait;
use std::collections::HashMap;
@@ -12,14 +15,14 @@ use tinychannels_bus::{approvals::ReplyContext, ChannelMessage};
/// Captures the account configuration and reply addresses used by approvals.
pub struct ChannelApprovalSurfaceSubscriber {
- channels: Arc<HashMap<String, Arc<dyn Channel>>>,
+ channels: Arc<HashMap<String, Arc<ChannelHandle>>>,
replies: Mutex<HashMap<String, ReplyContext>>,
config: Option<Arc<crate::config::Config>>,
cancellation: tokio_util::sync::CancellationToken,
}
impl ChannelApprovalSurfaceSubscriber {
pub fn with_config(
- channels: Arc<HashMap<String, Arc<dyn Channel>>>,
+ channels: Arc<HashMap<String, Arc<ChannelHandle>>>,
config: Arc<crate::config::Config>,
) -> Self {
Self {
@@ -30,7 +33,7 @@ impl ChannelApprovalSurfaceSubscriber {
}
}
#[cfg(test)]
- pub fn new(channels: Arc<HashMap<String, Arc<dyn Channel>>>) -> Self {
+ pub fn new(channels: Arc<HashMap<String, Arc<ChannelHandle>>>) -> Self {
Self {
channels,
replies: Mutex::new(HashMap::new()),
diff --git a/crates/openhuman-core/src/channels/mod.rs b/crates/openhuman-core/src/channels/mod.rs
index 3e777de654..749faf9e10 100644
--- a/crates/openhuman-core/src/channels/mod.rs
+++ b/crates/openhuman-core/src/channels/mod.rs
@@ -11,6 +11,7 @@ pub mod contract_schema;
pub(crate) mod credentials;
pub use tinychannels_bus::SendMessage;
+#[cfg(test)]
pub use tinychannels_runtime::{Channel, ChannelSendExt};
#[cfg(feature = "channels")]
diff --git a/crates/openhuman-core/src/channels/proactive.rs b/crates/openhuman-core/src/channels/proactive.rs
index 317b006d72..4683a96cf3 100644
--- a/crates/openhuman-core/src/channels/proactive.rs
+++ b/crates/openhuman-core/src/channels/proactive.rs
@@ -14,7 +14,10 @@
//! - The independent `web_chat::proactive` subscriber always delivers the web
//! destination, including when the external channels feature is disabled.
-use crate::channels::{Channel, ChannelSendExt, SendMessage};
+use crate::channels::runtime::ChannelHandle;
+use crate::channels::SendMessage;
+#[cfg(test)]
+use crate::channels::ChannelSendExt;
use crate::core::events::DomainEvent;
use async_trait::async_trait;
use std::collections::HashMap;
@@ -35,7 +38,7 @@ fn proactive_approval_gate() -> Option<Arc<crate::security::approval::ApprovalGa
pub struct ProactiveMessageSubscriber {
/// External channels (Telegram, Discord, etc.) keyed by name.
/// Empty in the desktop/web-only runtime.
- channels_by_name: Arc<HashMap<String, Arc<dyn Channel>>>,
+ channels_by_name: Arc<HashMap<String, Arc<ChannelHandle>>>,
/// The user's preferred channel for proactive messages. Read from
/// config at construction; can be updated at runtime.
@@ -46,7 +49,7 @@ impl ProactiveMessageSubscriber {
/// Construct with access to the external channels map and a
/// preferred channel name (from `channels_config.active_channel`).
pub fn new(
- channels_by_name: Arc<HashMap<String, Arc<dyn Channel>>>,
+ channels_by_name: Arc<HashMap<String, Arc<ChannelHandle>>>,
active_channel: Option<String>,
) -> Self {
Self {
diff --git a/crates/openhuman-core/src/channels/routes.rs b/crates/openhuman-core/src/channels/routes.rs
index 33721d133c..20c6880549 100644
--- a/crates/openhuman-core/src/channels/routes.rs
+++ b/crates/openhuman-core/src/channels/routes.rs
@@ -1,7 +1,10 @@
//! Per-sender routing and runtime command handling.
use super::context::{ChannelRouteSelection, ChannelRuntimeContext};
-use super::{Channel, ChannelSendExt, SendMessage};
+use super::SendMessage;
+use super::runtime::ChannelHandle;
+#[cfg(test)]
+use super::ChannelSendExt;
use crate::inference::provider;
use std::sync::Arc;
use tinychannels_bus as traits;
@@ -150,7 +153,7 @@ impl RouteCallbacks {
pub(crate) async fn handle_runtime_command_with_reset<F, Fut>(
ctx: &ChannelRuntimeContext,
msg: &traits::ChannelMessage,
- target_channel: Option<&Arc<dyn Channel>>,
+ target_channel: Option<&Arc<ChannelHandle>>,
sender_key: &str,
reset_scope: Option<tinychannels_bus::module_remote::RemoteSessionScope>,
durable_session_key: Option<String>,
@@ -346,7 +349,7 @@ async fn durable_channel_history_len(
pub(crate) async fn handle_runtime_command_if_needed(
ctx: &ChannelRuntimeContext,
msg: &traits::ChannelMessage,
- target_channel: Option<&Arc<dyn Channel>>,
+ target_channel: Option<&Arc<ChannelHandle>>,
sender_key: &str,
) -> bool {
handle_runtime_command_with_reset(
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs b/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs
index 21e55f839d..03e50f4beb 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs
@@ -21,7 +21,7 @@ pub(super) fn log_worker_join_result(result: Result<(), tokio::task::JoinError>)
/// Start the portable typing lifecycle with OpenHuman's configured interval.
pub(super) fn spawn_typing_task(
dispatch: &crate::core::runtime::DispatchCtx,
- channel: std::sync::Arc<dyn crate::channels::Channel>,
+ channel: std::sync::Arc<crate::channels::runtime::ChannelHandle>,
recipient: String,
cancellation_token: tokio_util::sync::CancellationToken,
) -> tokio::task::JoinHandle<()> {
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs
index dd089b10c1..615b215c94 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs
@@ -21,7 +21,9 @@ use std::sync::Arc;
use crate::agent::host_agents::HostAgent;
use crate::channels::context::ChannelRuntimeContext;
-use crate::channels::{ChannelSendExt, SendMessage};
+use crate::channels::SendMessage;
+#[cfg(test)]
+use crate::channels::ChannelSendExt;
use crate::config::Config;
use crate::core::bus::BUS;
use crate::core::events::DomainEvent;
@@ -142,7 +144,7 @@ pub(crate) const AGENT_UNAVAILABLE_REPLY: &str =
pub(crate) async fn refuse_missing_agent(
ctx: &ChannelRuntimeContext,
msg: &traits::ChannelMessage,
- target_channel: Option<&Arc<dyn crate::channels::Channel>>,
+ target_channel: Option<&Arc<crate::channels::runtime::ChannelHandle>>,
agent_id: &str,
) {
if let Some(channel) = target_channel {
diff --git a/crates/openhuman-core/src/channels/runtime/mod.rs b/crates/openhuman-core/src/channels/runtime/mod.rs
index 38f5ece3b9..0bd7807fe3 100644
--- a/crates/openhuman-core/src/channels/runtime/mod.rs
+++ b/crates/openhuman-core/src/channels/runtime/mod.rs
@@ -8,6 +8,7 @@ mod supervision;
pub(crate) use startup::hydrate_channel_credentials;
pub(crate) use startup::prepare_runtime_limits;
+pub(crate) use startup::ChannelHandle;
pub use startup::start_channels;
pub(crate) use startup::start_channels_with_session;
// The hosted-channel relay (`channels::providers::relay`) runs one message
diff --git a/crates/openhuman-core/src/channels/runtime/startup/turn_parts.rs b/crates/openhuman-core/src/channels/runtime/startup/turn_parts.rs
index 640d76af9a..29e21c6cf9 100644
--- a/crates/openhuman-core/src/channels/runtime/startup/turn_parts.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/turn_parts.rs
@@ -236,7 +236,7 @@ pub(crate) fn runtime_context(
dispatch: &DispatchCtx,
config: &Config,
parts: ChannelTurnParts,
- channels_by_name: Arc<HashMap<String, Arc<dyn crate::channels::Channel>>>,
+ channels_by_name: Arc<HashMap<String, Arc<crate::channels::runtime::ChannelHandle>>>,
module_objects: Arc<HashMap<String, tinybus::module::ModuleObjectClient>>,
message_timeout_secs: u64,
) -> ChannelRuntimeContext {
diff --git a/crates/openhuman-core/src/cron/bus.rs b/crates/openhuman-core/src/cron/bus.rs
index 7fac646ddb..77f22fe31d 100644
--- a/crates/openhuman-core/src/cron/bus.rs
+++ b/crates/openhuman-core/src/cron/bus.rs
@@ -11,16 +11,16 @@ use std::collections::HashMap;
use std::sync::Arc;
use tinybus::EventHandler;
use tinychannels_bus::SendMessage;
-use tinychannels_runtime::Channel;
+use crate::channels::runtime::ChannelHandle;
/// Subscribes to `CronDeliveryRequested` events and dispatches
/// the output to the named channel.
pub struct CronDeliverySubscriber {
- channels_by_name: Arc<HashMap<String, Arc<dyn Channel>>>,
+ channels_by_name: Arc<HashMap<String, Arc<ChannelHandle>>>,
}
impl CronDeliverySubscriber {
- pub fn new(channels_by_name: Arc<HashMap<String, Arc<dyn Channel>>>) -> Self {
+ pub fn new(channels_by_name: Arc<HashMap<String, Arc<ChannelHandle>>>) -> Self {
Self { channels_by_name }
}
}