seabird 0.4.0-alpha.3

A simple client library for the seabird-chat ecosystem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
use anyhow::Context;
use std::collections::HashMap;

use http::Uri;
use tonic::{
    metadata::{Ascii, MetadataValue},
    transport::{Channel, ClientTlsConfig},
};

use crate::error::Result;
use crate::proto;

/// MessageContent represents either plain text or structured blocks for messages.
#[derive(Debug)]
pub enum MessageContent {
    Text(String),
    Blocks(proto::Block),
}

impl MessageContent {
    /// Converts the message content into its internal representation.
    ///
    /// Returns a tuple of (text, optional block), where text-only messages
    /// return the text with None, and block messages return empty text with Some(block).
    fn into_inner(self) -> (String, Option<proto::Block>) {
        match self {
            MessageContent::Text(text) => (text, None),
            MessageContent::Blocks(block) => (String::from(""), Some(block)),
        }
    }
}

impl From<String> for MessageContent {
    fn from(text: String) -> Self {
        MessageContent::Text(text)
    }
}

impl From<&str> for MessageContent {
    fn from(text: &str) -> Self {
        MessageContent::Text(text.to_string())
    }
}

impl From<proto::Block> for MessageContent {
    fn from(block: proto::Block) -> Self {
        MessageContent::Blocks(block)
    }
}

#[cfg(feature = "seabird-client")]
use crate::proto::seabird::seabird_client::SeabirdClient as SeabirdProtoClient;

#[cfg(feature = "chat-ingest-client")]
use crate::proto::seabird::chat_ingest_client::ChatIngestClient as ChatIngestProtoClient;

/// Configuration for connecting to a seabird instance.
///
/// # Examples
///
/// ```rust
/// use seabird::ClientConfig;
///
/// let config = ClientConfig {
///     url: "https://seabird.example.com".to_string(),
///     token: "your-bot-token".to_string(),
/// };
/// ```
#[derive(Clone, Debug)]
pub struct ClientConfig {
    /// The URL of the seabird instance (e.g., "https://seabird.example.com" or "http://localhost:11235")
    pub url: String,
    /// The auth token for the bot
    pub token: String,
}

/// A convenience wrapper around the raw gRPC client type with authentication added.
///
/// This type alias represents a tonic channel with an authentication interceptor
/// that automatically adds authorization headers to all requests.
pub type InnerClient =
    tonic::codegen::InterceptedService<tonic::transport::Channel, AuthHeaderInterceptor>;

/// A tonic interceptor that adds authentication headers to gRPC requests.
///
/// Most users should not need to use this directly. This interceptor
/// automatically adds the "authorization" header with a Bearer token to every
/// outgoing request.
#[derive(Debug)]
pub struct AuthHeaderInterceptor {
    auth_header: MetadataValue<Ascii>,
}

impl tonic::service::Interceptor for AuthHeaderInterceptor {
    fn call(
        &mut self,
        mut req: tonic::Request<()>,
    ) -> std::result::Result<tonic::Request<()>, tonic::Status> {
        req.metadata_mut()
            .insert("authorization", self.auth_header.clone());
        Ok(req)
    }
}

/// Client for interacting with a seabird instance, generally as a bot.
///
/// This client provides methods for sending messages and performing actions in
/// channels and private conversations. It requires the `seabird-client` feature
/// to be enabled.
///
/// # Examples
///
/// ```rust,no_run
/// use seabird::{ClientConfig, SeabirdClient};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let config = ClientConfig {
///         url: "https://seabird.example.com".to_string(),
///         token: "your-token-here".to_string(),
///     };
///
///     let mut client = SeabirdClient::new(config).await?;
///     client.send_message("channel-id", "Hello!", None).await?;
///
///     Ok(())
/// }
/// ```
#[cfg(feature = "seabird-client")]
#[derive(Debug)]
pub struct SeabirdClient {
    inner: SeabirdProtoClient<InnerClient>,
}

#[cfg(feature = "seabird-client")]
impl SeabirdClient {
    /// Creates a new SeabirdClient from the given configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - The client configuration containing the server URL and authentication token
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The URL cannot be parsed
    /// - TLS configuration fails (for https URLs)
    /// - Connection to the server fails
    /// - The authentication token is an invalid format
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use seabird::{ClientConfig, SeabirdClient};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let config = ClientConfig {
    ///     url: "https://seabird.example.com".to_string(),
    ///     token: "your-token".to_string(),
    /// };
    ///
    /// let client = SeabirdClient::new(config).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn new(config: ClientConfig) -> Result<Self> {
        let uri: Uri = config.url.parse().context("failed to parse seabird URL")?;
        let mut channel_builder = Channel::builder(uri.clone());

        match uri.scheme_str() {
            None | Some("https") => {
                channel_builder = channel_builder.tls_config(ClientTlsConfig::new())?;
            }
            _ => {}
        }

        let channel = channel_builder
            .connect()
            .await
            .context("Failed to connect to seabird")?;

        let auth_header: MetadataValue<Ascii> = format!("Bearer {}", config.token).parse()?;

        let seabird_client =
            SeabirdProtoClient::with_interceptor(channel, AuthHeaderInterceptor { auth_header });

        Ok(Self {
            inner: seabird_client,
        })
    }

    /// Performs an action in a private conversation with a user.
    ///
    /// Actions are typically displayed differently than regular messages (e.g., "/me waves").
    ///
    /// # Arguments
    ///
    /// * `user_id` - The ID of the user to send the action to
    /// * `content` - The message content (text or formatted blocks)
    /// * `tags` - Optional metadata tags for the message
    ///
    /// # Errors
    ///
    /// Returns an error if the gRPC request fails.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use seabird::{ClientConfig, SeabirdClient};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let mut client = SeabirdClient::new(ClientConfig {
    /// #     url: "https://example.com".to_string(),
    /// #     token: "token".to_string(),
    /// # }).await?;
    /// client.perform_private_action("user-id", "waves", None).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn perform_private_action(
        &mut self,
        user_id: impl Into<String>,
        content: impl Into<MessageContent>,
        tags: Option<HashMap<String, String>>,
    ) -> Result<()> {
        let (text, root_block) = content.into().into_inner();

        self.inner
            .perform_private_action(proto::PerformPrivateActionRequest {
                user_id: user_id.into(),
                text,
                root_block,
                tags: tags.unwrap_or_default(),
            })
            .await?;
        Ok(())
    }

    /// Performs an action in a channel.
    ///
    /// Actions are typically displayed differently than regular messages (e.g., "/me waves").
    ///
    /// # Arguments
    ///
    /// * `channel_id` - The ID of the channel to send the action to
    /// * `content` - The message content (text or formatted blocks)
    /// * `tags` - Optional metadata tags for the message
    ///
    /// # Errors
    ///
    /// Returns an error if the gRPC request fails.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use seabird::{ClientConfig, SeabirdClient};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let mut client = SeabirdClient::new(ClientConfig {
    /// #     url: "https://example.com".to_string(),
    /// #     token: "token".to_string(),
    /// # }).await?;
    /// client.perform_action("channel-id", "dances", None).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn perform_action(
        &mut self,
        channel_id: impl Into<String>,
        content: impl Into<MessageContent>,
        tags: Option<HashMap<String, String>>,
    ) -> Result<()> {
        let (text, root_block) = content.into().into_inner();

        self.inner
            .perform_action(proto::PerformActionRequest {
                channel_id: channel_id.into(),
                text,
                root_block,
                tags: tags.unwrap_or_default(),
            })
            .await?;
        Ok(())
    }

    /// Sends a message to a channel.
    ///
    /// # Arguments
    ///
    /// * `channel_id` - The ID of the channel to send the message to
    /// * `content` - The message content (text or formatted blocks)
    /// * `tags` - Optional metadata tags for the message
    ///
    /// # Errors
    ///
    /// Returns an error if the gRPC request fails.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use seabird::{ClientConfig, SeabirdClient, Block};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let mut client = SeabirdClient::new(ClientConfig {
    /// #     url: "https://example.com".to_string(),
    /// #     token: "token".to_string(),
    /// # }).await?;
    /// // Send plain text
    /// client.send_message("channel-id", "Hello!", None).await?;
    ///
    /// // Send formatted blocks
    /// let formatted = Block::new().text("Hello ").bold("world");
    /// client.send_message("channel-id", formatted, None).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn send_message(
        &mut self,
        channel_id: impl Into<String>,
        content: impl Into<MessageContent>,
        tags: Option<HashMap<String, String>>,
    ) -> Result<()> {
        let (text, root_block) = content.into().into_inner();

        self.inner
            .send_message(proto::SendMessageRequest {
                channel_id: channel_id.into(),
                text,
                root_block,
                tags: tags.unwrap_or_default(),
            })
            .await?;
        Ok(())
    }

    /// Sends a private message to a user.
    ///
    /// # Arguments
    ///
    /// * `user_id` - The ID of the user to send the message to
    /// * `content` - The message content (text or formatted blocks)
    /// * `tags` - Optional metadata tags for the message
    ///
    /// # Errors
    ///
    /// Returns an error if the gRPC request fails.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use seabird::{ClientConfig, SeabirdClient};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let mut client = SeabirdClient::new(ClientConfig {
    /// #     url: "https://example.com".to_string(),
    /// #     token: "token".to_string(),
    /// # }).await?;
    /// client.send_private_message("user-id", "Hello!", None).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn send_private_message(
        &mut self,
        user_id: impl Into<String>,
        content: impl Into<MessageContent>,
        tags: Option<HashMap<String, String>>,
    ) -> Result<()> {
        let (text, root_block) = content.into().into_inner();

        self.inner
            .send_private_message(proto::SendPrivateMessageRequest {
                user_id: user_id.into(),
                text,
                root_block,
                tags: tags.unwrap_or_default(),
            })
            .await?;
        Ok(())
    }

    /// Returns a reference to the inner gRPC client.
    ///
    /// This provides access to the underlying tonic-generated client for
    /// advanced use cases not covered by the high-level API.
    pub fn inner_ref(&self) -> &'_ SeabirdProtoClient<InnerClient> {
        &self.inner
    }

    /// Returns a mutable reference to the inner gRPC client.
    ///
    /// This provides mutable access to the underlying tonic-generated client
    /// for advanced use cases not covered by the high-level API.
    pub fn inner_mut_ref(&mut self) -> &'_ mut SeabirdProtoClient<InnerClient> {
        &mut self.inner
    }
}

/// Client for ingesting chat data into seabird.
///
/// This client is used to send chat data from external sources into the seabird
/// chat ingest service. It requires the `chat-ingest-client` feature to be enabled.
///
/// # Examples
///
/// ```rust,no_run
/// use seabird::{ClientConfig, ChatIngestClient};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let config = ClientConfig {
///         url: "https://seabird.example.com".to_string(),
///         token: "your-token-here".to_string(),
///     };
///
///     let client = ChatIngestClient::new(config).await?;
///     // Use the inner client to access chat ingest methods
///
///     Ok(())
/// }
/// ```
#[cfg(feature = "chat-ingest-client")]
#[derive(Debug)]
pub struct ChatIngestClient {
    inner: ChatIngestProtoClient<InnerClient>,
}

#[cfg(feature = "chat-ingest-client")]
impl ChatIngestClient {
    /// Creates a new ChatIngestClient from the given configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - The client configuration containing the server URL and authentication token
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The URL cannot be parsed
    /// - TLS configuration fails (for https URLs)
    /// - Connection to the server fails
    /// - The authentication token is invalid
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use seabird::{ClientConfig, ChatIngestClient};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let config = ClientConfig {
    ///     url: "https://seabird.example.com".to_string(),
    ///     token: "your-token".to_string(),
    /// };
    ///
    /// let client = ChatIngestClient::new(config).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn new(config: ClientConfig) -> Result<Self> {
        let uri: Uri = config.url.parse().context("failed to parse seabird URL")?;
        let mut channel_builder = Channel::builder(uri.clone());

        match uri.scheme_str() {
            None | Some("https") => {
                channel_builder = channel_builder.tls_config(ClientTlsConfig::new())?;
            }
            _ => {}
        }

        let channel = channel_builder
            .connect()
            .await
            .context("Failed to connect to seabird")?;

        let auth_header: MetadataValue<Ascii> = format!("Bearer {}", config.token).parse()?;

        let chat_ingest_client =
            ChatIngestProtoClient::with_interceptor(channel, AuthHeaderInterceptor { auth_header });

        Ok(Self {
            inner: chat_ingest_client,
        })
    }

    /// Returns a reference to the inner gRPC client.
    ///
    /// This provides access to the underlying tonic-generated client for
    /// accessing chat ingest protocol methods.
    pub fn inner_ref(&self) -> &'_ ChatIngestProtoClient<InnerClient> {
        &self.inner
    }

    /// Returns a mutable reference to the inner gRPC client.
    ///
    /// This provides mutable access to the underlying tonic-generated client
    /// for accessing chat ingest protocol methods.
    pub fn inner_mut_ref(&mut self) -> &'_ mut ChatIngestProtoClient<InnerClient> {
        &mut self.inner
    }
}