openclaw_channels/
traits.rs1use async_trait::async_trait;
4use thiserror::Error;
5
6use openclaw_core::types::{Attachment, DeliveryResult, Message};
7
8#[derive(Error, Debug)]
10pub enum ChannelError {
11 #[error("Channel not connected")]
13 NotConnected,
14
15 #[error("Authentication failed: {0}")]
17 AuthFailed(String),
18
19 #[error("Delivery failed: {0}")]
21 DeliveryFailed(String),
22
23 #[error("Rate limited")]
25 RateLimited,
26
27 #[error("Network error: {0}")]
29 Network(String),
30
31 #[error("Configuration error: {0}")]
33 Config(String),
34}
35
36#[derive(Debug, Clone, Default, serde::Serialize)]
38pub struct ChannelCapabilities {
39 pub text: bool,
41 pub images: bool,
43 pub videos: bool,
45 pub voice: bool,
47 pub files: bool,
49 pub threads: bool,
51 pub reactions: bool,
53 pub editing: bool,
55 pub deletion: bool,
57}
58
59#[derive(Debug, Clone)]
61pub struct ChannelProbe {
62 pub connected: bool,
64 pub account_id: Option<String>,
66 pub display_name: Option<String>,
68 pub error: Option<String>,
70}
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq)]
74pub enum DeliveryMode {
75 Immediate,
77 Batched,
79 Webhook,
81}
82
83#[derive(Debug, Clone)]
85pub struct OutboundContext {
86 pub chat_id: String,
88 pub reply_to: Option<String>,
90 pub thread_id: Option<String>,
92}
93
94#[derive(Debug, Clone)]
96pub struct ChannelContext {
97 pub agent_id: String,
99 pub account_id: String,
101}
102
103#[async_trait]
105pub trait Channel: Send + Sync {
106 fn id(&self) -> &str;
108
109 fn label(&self) -> &str;
111
112 fn capabilities(&self) -> ChannelCapabilities;
114
115 async fn start(&self, ctx: ChannelContext) -> Result<(), ChannelError>;
117
118 async fn stop(&self) -> Result<(), ChannelError>;
120
121 async fn probe(&self) -> Result<ChannelProbe, ChannelError>;
123}
124
125#[async_trait]
127pub trait ChannelOutbound: Channel {
128 async fn send_text(
130 &self,
131 ctx: OutboundContext,
132 text: &str,
133 ) -> Result<DeliveryResult, ChannelError>;
134
135 async fn send_media(
137 &self,
138 ctx: OutboundContext,
139 media: &[Attachment],
140 ) -> Result<DeliveryResult, ChannelError>;
141
142 fn text_chunk_limit(&self) -> usize;
144
145 fn delivery_mode(&self) -> DeliveryMode;
147}
148
149#[async_trait]
151pub trait ChannelInbound: Channel {
152 type RawMessage;
154
155 fn normalize(&self, raw: Self::RawMessage) -> Result<Message, ChannelError>;
157
158 async fn acknowledge(&self, message_id: &str) -> Result<(), ChannelError>;
160}