adk-gateway 1.0.0

Multi-channel AI gateway for adk-rust agents — Telegram, Slack, WhatsApp, Discord, Matrix + control panel
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
502
503
504
505
506
507
508
509
//! Delivery strategies for streaming vs batch response delivery.
//!
//! `DeliveryStrategy` abstracts how partial and final response text is
//! delivered to the user through a channel. Two implementations:
//!
//! - `StreamingDelivery`: sends an initial placeholder, then edits in-place
//!   as partial events arrive, rate-limited to 1 edit/sec (R2.1, R2.2, R2.3).
//! - `BatchDelivery`: ignores partials, sends a single message on completion (R2.4).
//!
//! A factory function `select_strategy` picks the right one based on channel
//! capabilities and the configured `streamMode` (R2.5).

use crate::channel::{Channel, ChannelType, EditMessage, OutboundMessage};

use async_trait::async_trait;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::time::Instant;

/// Reference to a message being delivered — carries the context needed
/// to send or edit messages on a channel.
#[derive(Debug, Clone)]
pub struct MessageRef {
    /// The channel type (for building edit/send messages)
    pub channel_type: ChannelType,
    /// Account identifier for multi-account support
    pub account_id: String,
    /// Recipient / chat ID
    pub recipient_id: String,
    /// Platform message ID of the in-flight message (set after first send)
    pub message_id: Option<String>,
    /// Platform message ID to reply to (the user's original message)
    pub reply_to: Option<String>,
}

/// Abstracts streaming vs batch response delivery.
#[async_trait]
pub trait DeliveryStrategy: Send + Sync {
    /// Called on each partial event (streaming mode uses this to edit in-place).
    async fn on_partial(&self, text: &str, msg_ref: &MessageRef) -> anyhow::Result<()>;

    /// Called with the final complete text.
    async fn on_complete(&self, text: &str, msg_ref: &MessageRef) -> anyhow::Result<()>;
}

/// Internal mutable state for `StreamingDelivery`.
struct StreamingState {
    /// The platform message ID of the placeholder we sent.
    sent_message_id: Option<String>,
    /// When we last successfully sent an edit.
    last_edit: Option<Instant>,
    /// The most recent accumulated text (for retry on edit failure).
    latest_text: String,
}

/// Streaming delivery: send placeholder, edit in-place, rate-limit edits.
pub struct StreamingDelivery {
    channel: Arc<dyn Channel>,
    state: Mutex<StreamingState>,
    /// Minimum interval between edits (default 1 second).
    edit_interval: std::time::Duration,
}

impl StreamingDelivery {
    /// Create a new streaming delivery strategy for the given channel.
    pub fn new(channel: Arc<dyn Channel>) -> Self {
        Self {
            channel,
            state: Mutex::new(StreamingState {
                sent_message_id: None,
                last_edit: None,
                latest_text: String::new(),
            }),
            edit_interval: std::time::Duration::from_secs(1),
        }
    }

    /// Try to edit the in-flight message. On rate-limit failure, wait and retry
    /// with the latest accumulated text (R2.5).
    async fn try_edit(
        &self,
        msg_ref: &MessageRef,
        text: &str,
        message_id: &str,
    ) -> anyhow::Result<()> {
        let edit_msg = EditMessage {
            channel_type: msg_ref.channel_type,
            account_id: msg_ref.account_id.clone(),
            message_id: message_id.to_string(),
            recipient_id: msg_ref.recipient_id.clone(),
            text: text.to_string(),
        };

        match self.channel.edit(edit_msg).await {
            Ok(()) => Ok(()),
            Err(e) => {
                let err_str = e.to_string();
                // "message is not modified" is benign — content hasn't changed
                if err_str.contains("message is not modified") {
                    tracing::debug!("edit skipped: message content unchanged");
                    return Ok(());
                }
                tracing::warn!(error = %e, "edit failed, retrying after rate-limit window");
                // Wait for the rate-limit window then retry with latest text
                tokio::time::sleep(self.edit_interval).await;

                let state = self.state.lock().await;
                let retry_text = if state.latest_text.is_empty() {
                    text.to_string()
                } else {
                    state.latest_text.clone()
                };
                drop(state);

                let retry_msg = EditMessage {
                    channel_type: msg_ref.channel_type,
                    account_id: msg_ref.account_id.clone(),
                    message_id: message_id.to_string(),
                    recipient_id: msg_ref.recipient_id.clone(),
                    text: retry_text,
                };
                self.channel.edit(retry_msg).await
            }
        }
    }
}

#[async_trait]
impl DeliveryStrategy for StreamingDelivery {
    async fn on_partial(&self, text: &str, msg_ref: &MessageRef) -> anyhow::Result<()> {
        let mut state = self.state.lock().await;
        // Always track the latest text for retry purposes
        state.latest_text = text.to_string();

        if state.sent_message_id.is_none() {
            // First partial: send initial placeholder message
            let outbound = OutboundMessage {
                channel_type: msg_ref.channel_type,
                account_id: msg_ref.account_id.clone(),
                recipient_id: msg_ref.recipient_id.clone(),
                text: text.to_string(),
                reply_to: msg_ref.reply_to.clone(),
                is_partial: true,
            };
            let sent_id = self.channel.send(outbound).await?;
            // Use the platform message ID returned by send() for subsequent edits
            state.sent_message_id = sent_id.or_else(|| msg_ref.message_id.clone());
            state.last_edit = Some(Instant::now());
            return Ok(());
        }

        // Subsequent partials: rate-limit to 1 edit/sec (R2.2)
        if let Some(last) = state.last_edit {
            if last.elapsed() < self.edit_interval {
                // Throttled — skip this edit, latest_text is already stored for next time
                return Ok(());
            }
        }

        let message_id = state.sent_message_id.clone().unwrap();
        state.last_edit = Some(Instant::now());
        drop(state);

        self.try_edit(msg_ref, text, &message_id).await
    }

    async fn on_complete(&self, text: &str, msg_ref: &MessageRef) -> anyhow::Result<()> {
        let mut state = self.state.lock().await;
        state.latest_text = text.to_string();

        if let Some(ref message_id) = state.sent_message_id {
            let message_id = message_id.clone();
            drop(state);
            // Final edit with complete text (R2.3)
            self.try_edit(msg_ref, text, &message_id).await
        } else {
            drop(state);
            // No placeholder was ever sent (e.g. response arrived instantly),
            // just send the full message.
            let outbound = OutboundMessage {
                channel_type: msg_ref.channel_type,
                account_id: msg_ref.account_id.clone(),
                recipient_id: msg_ref.recipient_id.clone(),
                text: text.to_string(),
                reply_to: msg_ref.reply_to.clone(),
                is_partial: false,
            };
            self.channel.send(outbound).await?;
            Ok(())
        }
    }
}

/// Batch delivery: ignore partials, send a single message on completion (R2.4).
pub struct BatchDelivery {
    channel: Arc<dyn Channel>,
}

impl BatchDelivery {
    /// Create a new batch delivery strategy for the given channel.
    pub fn new(channel: Arc<dyn Channel>) -> Self {
        Self { channel }
    }
}

#[async_trait]
impl DeliveryStrategy for BatchDelivery {
    async fn on_partial(&self, _text: &str, _msg_ref: &MessageRef) -> anyhow::Result<()> {
        // Batch mode ignores partial events
        Ok(())
    }

    async fn on_complete(&self, text: &str, msg_ref: &MessageRef) -> anyhow::Result<()> {
        let outbound = OutboundMessage {
            channel_type: msg_ref.channel_type,
            account_id: msg_ref.account_id.clone(),
            recipient_id: msg_ref.recipient_id.clone(),
            text: text.to_string(),
            reply_to: msg_ref.reply_to.clone(),
            is_partial: false,
        };
        self.channel.send(outbound).await?;
        Ok(())
    }
}

/// Select the appropriate delivery strategy based on channel capabilities
/// and the configured stream mode.
///
/// Uses `StreamingDelivery` when the channel supports editing AND the
/// stream mode is not `"complete"`. Otherwise falls back to `BatchDelivery`.
pub fn select_strategy(
    channel: Arc<dyn Channel>,
    stream_mode: Option<&str>,
) -> Arc<dyn DeliveryStrategy> {
    let use_streaming =
        channel.supports_editing() && stream_mode.map(|m| m != "complete").unwrap_or(true);

    if use_streaming {
        tracing::debug!(
            channel = %channel.channel_type(),
            "using streaming delivery (edit-in-place)"
        );
        Arc::new(StreamingDelivery::new(channel))
    } else {
        tracing::debug!(
            channel = %channel.channel_type(),
            "using batch delivery (single message)"
        );
        Arc::new(BatchDelivery::new(channel))
    }
}

/// Split a message into chunks that fit within `max_len`.
///
/// Tries to split at newlines or spaces to avoid breaking words.
/// Each chunk is guaranteed to have length ≤ `max_len`.
pub fn split_message(text: &str, max_len: usize) -> Vec<String> {
    if max_len == 0 {
        return vec![text.to_string()];
    }
    if text.len() <= max_len {
        return vec![text.to_string()];
    }
    let mut chunks = Vec::new();
    let mut remaining = text;
    while !remaining.is_empty() {
        if remaining.len() <= max_len {
            chunks.push(remaining.to_string());
            break;
        }
        let slice = &remaining[..max_len];
        let at = slice
            .rfind('\n')
            .or_else(|| slice.rfind(' '))
            .unwrap_or(max_len);
        let at = if at == 0 { max_len } else { at };
        chunks.push(remaining[..at].to_string());
        remaining = remaining[at..].trim_start();
    }
    chunks
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::channel::{Channel, ChannelType, EditMessage, InboundMessage, OutboundMessage};
    use async_trait::async_trait;
    use std::sync::atomic::{AtomicU32, Ordering};
    use tokio::sync::mpsc;

    /// A mock channel that tracks send/edit calls for testing.
    struct MockChannel {
        editing_supported: bool,
        send_count: AtomicU32,
        edit_count: AtomicU32,
        /// If set, the first N edits will fail to simulate rate limiting.
        fail_first_n_edits: AtomicU32,
    }

    impl MockChannel {
        fn new(editing_supported: bool) -> Self {
            Self {
                editing_supported,
                send_count: AtomicU32::new(0),
                edit_count: AtomicU32::new(0),
                fail_first_n_edits: AtomicU32::new(0),
            }
        }

        fn with_failing_edits(mut self, n: u32) -> Self {
            self.fail_first_n_edits = AtomicU32::new(n);
            self
        }

        fn sends(&self) -> u32 {
            self.send_count.load(Ordering::SeqCst)
        }

        fn edits(&self) -> u32 {
            self.edit_count.load(Ordering::SeqCst)
        }
    }

    #[async_trait]
    impl Channel for MockChannel {
        fn channel_type(&self) -> ChannelType {
            ChannelType::Telegram
        }

        async fn start(&self, _tx: mpsc::Sender<InboundMessage>) -> anyhow::Result<()> {
            Ok(())
        }

        async fn send(&self, _msg: OutboundMessage) -> anyhow::Result<Option<String>> {
            self.send_count.fetch_add(1, Ordering::SeqCst);
            Ok(Some("mock-msg-123".to_string()))
        }

        async fn edit(&self, _msg: EditMessage) -> anyhow::Result<()> {
            let remaining = self.fail_first_n_edits.load(Ordering::SeqCst);
            if remaining > 0 {
                self.fail_first_n_edits.fetch_sub(1, Ordering::SeqCst);
                return Err(anyhow::anyhow!("rate limited"));
            }
            self.edit_count.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }

        fn supports_editing(&self) -> bool {
            self.editing_supported
        }

        async fn shutdown(&self) -> anyhow::Result<()> {
            Ok(())
        }
    }

    fn test_msg_ref() -> MessageRef {
        MessageRef {
            channel_type: ChannelType::Telegram,
            account_id: "default".to_string(),
            recipient_id: "user123".to_string(),
            message_id: Some("msg_1".to_string()),
            reply_to: Some("orig_msg".to_string()),
        }
    }

    #[tokio::test]
    async fn test_batch_ignores_partials() {
        let ch = Arc::new(MockChannel::new(false));
        let strategy = BatchDelivery::new(ch.clone());
        let msg_ref = test_msg_ref();

        strategy.on_partial("hello", &msg_ref).await.unwrap();
        strategy.on_partial("hello world", &msg_ref).await.unwrap();
        assert_eq!(ch.sends(), 0);
        assert_eq!(ch.edits(), 0);
    }

    #[tokio::test]
    async fn test_batch_sends_on_complete() {
        let ch = Arc::new(MockChannel::new(false));
        let strategy = BatchDelivery::new(ch.clone());
        let msg_ref = test_msg_ref();

        strategy.on_complete("final text", &msg_ref).await.unwrap();
        assert_eq!(ch.sends(), 1);
        assert_eq!(ch.edits(), 0);
    }

    #[tokio::test]
    async fn test_streaming_sends_placeholder_on_first_partial() {
        let ch = Arc::new(MockChannel::new(true));
        let strategy = StreamingDelivery::new(ch.clone());
        let msg_ref = test_msg_ref();

        strategy.on_partial("hel", &msg_ref).await.unwrap();
        assert_eq!(ch.sends(), 1, "should send placeholder on first partial");
        assert_eq!(ch.edits(), 0, "no edits yet on first partial");
    }

    #[tokio::test]
    async fn test_streaming_rate_limits_edits() {
        let ch = Arc::new(MockChannel::new(true));
        let strategy = StreamingDelivery::new(ch.clone());
        let msg_ref = test_msg_ref();

        // First partial sends placeholder
        strategy.on_partial("a", &msg_ref).await.unwrap();
        // Immediate second partial should be throttled
        strategy.on_partial("ab", &msg_ref).await.unwrap();
        // Immediate third partial should also be throttled
        strategy.on_partial("abc", &msg_ref).await.unwrap();

        assert_eq!(ch.sends(), 1, "only one send (placeholder)");
        assert_eq!(ch.edits(), 0, "edits throttled within 1 sec");
    }

    #[tokio::test]
    async fn test_streaming_complete_performs_final_edit() {
        let ch = Arc::new(MockChannel::new(true));
        let strategy = StreamingDelivery::new(ch.clone());
        let msg_ref = test_msg_ref();

        strategy.on_partial("partial", &msg_ref).await.unwrap();
        strategy.on_complete("final", &msg_ref).await.unwrap();

        assert_eq!(ch.sends(), 1, "placeholder send");
        assert_eq!(ch.edits(), 1, "final edit");
    }

    #[tokio::test]
    async fn test_streaming_complete_without_partial_sends_message() {
        let ch = Arc::new(MockChannel::new(true));
        let strategy = StreamingDelivery::new(ch.clone());
        let msg_ref = test_msg_ref();

        // No partials, just complete
        strategy
            .on_complete("instant response", &msg_ref)
            .await
            .unwrap();
        assert_eq!(ch.sends(), 1, "should send as regular message");
        assert_eq!(ch.edits(), 0, "no edits needed");
    }

    #[tokio::test]
    async fn test_select_strategy_streaming_when_editing_supported() {
        let ch = Arc::new(MockChannel::new(true));
        let strategy = select_strategy(ch, None);
        // We can't downcast easily, but we can test behavior
        let msg_ref = test_msg_ref();
        // on_partial should not error
        strategy.on_partial("test", &msg_ref).await.unwrap();
    }

    #[tokio::test]
    async fn test_select_strategy_batch_when_complete_mode() {
        let ch = Arc::new(MockChannel::new(true));
        let strategy = select_strategy(ch.clone(), Some("complete"));
        let msg_ref = test_msg_ref();

        // Partials should be ignored (batch mode)
        strategy.on_partial("test", &msg_ref).await.unwrap();
        assert_eq!(ch.sends(), 0, "batch ignores partials");
    }

    #[tokio::test]
    async fn test_select_strategy_batch_when_no_editing() {
        let ch = Arc::new(MockChannel::new(false));
        let strategy = select_strategy(ch.clone(), Some("partial"));
        let msg_ref = test_msg_ref();

        strategy.on_partial("test", &msg_ref).await.unwrap();
        assert_eq!(
            ch.sends(),
            0,
            "batch ignores partials even with partial mode"
        );
    }

    #[tokio::test]
    async fn test_streaming_edit_failure_retries() {
        let ch = Arc::new(MockChannel::new(true).with_failing_edits(1));
        let strategy = StreamingDelivery {
            channel: ch.clone(),
            state: Mutex::new(StreamingState {
                sent_message_id: None,
                last_edit: None,
                latest_text: String::new(),
            }),
            // Use a very short interval for testing
            edit_interval: std::time::Duration::from_millis(10),
        };
        let msg_ref = test_msg_ref();

        // Send placeholder
        strategy.on_partial("a", &msg_ref).await.unwrap();
        assert_eq!(ch.sends(), 1);

        // Wait past rate limit
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;

        // This edit will fail once, then retry successfully
        strategy.on_partial("ab", &msg_ref).await.unwrap();
        assert_eq!(ch.edits(), 1, "retry should succeed");
    }
}