lark-channel 0.5.0

Lark/Feishu Channel SDK for Rust
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
use std::time::{Duration, Instant};

use crate::lark_openapi::OpenApiTransport;
use crate::{CardId, Error, MessageId, Result};

use super::MarkdownStream;

/// Runtime-independent coalescing throttle for a high-level Markdown stream.
///
/// The first generated content is sent immediately. Later `append` and
/// `set_content` calls retain the latest complete content locally and send it
/// only after `min_update_interval` has elapsed. No background timer is
/// created: a later content call, `flush`, or `finish` drives delivery.
/// Applications that need buffered content to appear during a producer pause
/// can schedule `flush` using [`Self::next_flush_in`].
///
/// The configured interval applies only to automatic content update attempts.
/// Explicit [`Self::flush`] calls and [`Self::finish`] bypass it so callers can
/// force out the buffered tail and close the stream. This type therefore
/// coalesces generated content but is not an aggregate rate limiter for every
/// CardKit operation. Applications that need a hard per-entity operation budget
/// must schedule all explicit operations as part of their own policy.
#[derive(Debug)]
pub struct ThrottledMarkdownStream<'a, T> {
    stream: MarkdownStream<'a, T>,
    min_update_interval: Duration,
    desired_content: Option<String>,
    last_update_attempt_at: Option<Instant>,
}

impl<'a, T> ThrottledMarkdownStream<'a, T>
where
    T: OpenApiTransport,
{
    pub(super) fn new(stream: MarkdownStream<'a, T>, min_update_interval: Duration) -> Self {
        let desired_content = stream
            .pending_content()
            .or_else(|| stream.content())
            .map(str::to_owned);
        let last_update_attempt_at =
            (stream.content().is_some() || stream.has_pending_operation()).then(Instant::now);
        Self {
            stream,
            min_update_interval,
            desired_content,
            last_update_attempt_at,
        }
    }

    /// Returns the message that displays this stream.
    pub fn message_id(&self) -> &MessageId {
        self.stream.message_id()
    }

    /// Returns the CardKit entity updated by this stream.
    pub fn card_id(&self) -> &CardId {
        self.stream.card_id()
    }

    /// Returns the complete locally accumulated content, including data that
    /// may still be buffered.
    pub fn content(&self) -> Option<&str> {
        self.desired_content.as_deref()
    }

    /// Returns the latest content acknowledged by the OpenAPI client.
    pub fn flushed_content(&self) -> Option<&str> {
        self.stream.content()
    }

    /// Returns the configured minimum interval between automatic update
    /// attempts.
    pub fn min_update_interval(&self) -> Duration {
        self.min_update_interval
    }

    pub(super) fn set_min_update_interval(&mut self, min_update_interval: Duration) {
        self.min_update_interval = min_update_interval;
    }

    pub(super) fn has_pending_finish(&self) -> bool {
        self.stream.has_pending_finish()
    }

    /// Returns the sequence that will be used by the next CardKit operation.
    pub fn next_sequence(&self) -> u32 {
        self.stream.next_sequence()
    }

    /// Returns whether the stream completed successfully.
    pub fn is_finished(&self) -> bool {
        self.stream.is_finished()
    }

    /// Returns whether an ambiguous remote failure retained an operation for replay.
    pub fn has_pending_operation(&self) -> bool {
        self.stream.has_pending_operation()
    }

    /// Returns whether locally accumulated content has not been acknowledged
    /// by the OpenAPI client yet.
    pub fn has_buffered_content(&self) -> bool {
        self.desired_content.as_deref() != self.stream.content()
    }

    /// Returns how long the caller should wait before an automatic flush is
    /// due, or `None` when there is no buffered or pending content operation.
    pub fn next_flush_in(&self) -> Option<Duration> {
        if self.stream.has_pending_finish()
            || (!self.has_buffered_content() && !self.stream.has_pending_operation())
        {
            return None;
        }

        Some(match self.last_update_attempt_at {
            Some(last_attempt) => self
                .min_update_interval
                .saturating_sub(last_attempt.elapsed()),
            None => Duration::ZERO,
        })
    }

    /// Appends one token delta to the local content and flushes it when the
    /// throttle window is due.
    ///
    /// Empty chunks are a no-op. Validation runs before the local buffer is
    /// changed, so rejected content does not poison a later flush.
    pub async fn append(&mut self, chunk: impl AsRef<str>) -> Result<()> {
        self.ensure_content_mutable()?;
        let chunk = chunk.as_ref();
        if chunk.is_empty() {
            return Ok(());
        }

        let mut content = self.content().unwrap_or_default().to_owned();
        content.push_str(chunk);
        self.stream.validate_content(&content)?;
        self.desired_content = Some(content);
        self.flush_if_due().await.map(|_| ())
    }

    /// Replaces the local complete content snapshot and flushes it when the
    /// throttle window is due.
    pub async fn set_content(&mut self, content: impl Into<String>) -> Result<()> {
        self.ensure_content_mutable()?;
        let content = content.into();
        self.stream.validate_content(&content)?;
        self.desired_content = Some(content);
        self.flush_if_due().await.map(|_| ())
    }

    /// Flushes one retained operation or buffered content update when the
    /// throttle interval has elapsed.
    ///
    /// Returns `true` when an OpenAPI operation was acknowledged and `false`
    /// when there was no due work. A retained ambiguous operation is replayed
    /// before newer buffered content and keeps its original sequence and UUID.
    pub async fn flush_if_due(&mut self) -> Result<bool> {
        if self.stream.is_finished() || self.stream.has_pending_finish() {
            return Ok(false);
        }
        let now = Instant::now();
        if !self.is_due_at(now) {
            return Ok(false);
        }

        if self.stream.has_pending_operation() {
            self.last_update_attempt_at = Some(now);
            self.stream.retry_pending().await?;
            return Ok(true);
        }

        self.flush_buffered_at(now).await
    }

    /// Immediately replays retained work and sends the latest buffered
    /// content, bypassing the throttle interval.
    pub async fn flush(&mut self) -> Result<()> {
        if self.stream.is_finished() {
            return Ok(());
        }
        if self.stream.has_pending_operation() {
            self.last_update_attempt_at = Some(Instant::now());
            self.stream.retry_pending().await?;
            if self.stream.is_finished() {
                self.sync_content_from_stream();
                return Ok(());
            }
        }
        self.flush_buffered_at(Instant::now()).await.map(|_| ())
    }

    /// Flushes the latest content and disables CardKit streaming mode.
    ///
    /// Repeating `finish` after an ambiguous finish failure replays the
    /// retained settings operation through the underlying stream.
    pub async fn finish(&mut self) -> Result<()> {
        if self.stream.is_finished() {
            self.sync_content_from_stream();
            return Ok(());
        }
        if self.stream.has_pending_finish() {
            let result = self.stream.retry_pending().await;
            self.sync_content_from_stream();
            return result;
        }

        self.flush().await?;
        if self.stream.is_finished() {
            return Ok(());
        }
        let result = self.stream.finish().await;
        self.sync_content_from_stream();
        result
    }

    fn is_due_at(&self, now: Instant) -> bool {
        if !self.has_buffered_content() && !self.stream.has_pending_operation() {
            return false;
        }
        self.last_update_attempt_at.is_none_or(|last_attempt| {
            now.saturating_duration_since(last_attempt) >= self.min_update_interval
        })
    }

    async fn flush_buffered_at(&mut self, now: Instant) -> Result<bool> {
        if !self.has_buffered_content() {
            return Ok(false);
        }
        let Some(content) = self.desired_content.clone() else {
            return Ok(false);
        };

        self.last_update_attempt_at = Some(now);
        self.stream.set_content(content).await?;
        Ok(true)
    }

    fn ensure_content_mutable(&self) -> Result<()> {
        if self.stream.is_finished() {
            return Err(Error::Validation(
                "markdown stream is already finished".to_owned(),
            ));
        }
        if self.stream.has_pending_finish() {
            return Err(Error::Validation(
                "retry the pending markdown stream finish before changing content".to_owned(),
            ));
        }
        Ok(())
    }

    fn sync_content_from_stream(&mut self) {
        self.desired_content = self
            .stream
            .pending_content()
            .or_else(|| self.stream.content())
            .map(str::to_owned);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lark_openapi::HttpMethod;
    use crate::message::streaming::test_support::{
        FakeTransport, assert_card_content_update, block_on, card_response, message_response,
        ok_response, token_response,
    };
    use crate::{ChannelConfig, MessageSender, Recipient};

    const INTERVAL: Duration = Duration::from_millis(100);

    #[test]
    fn coalesces_updates_and_flushes_the_tail_before_finish() {
        let transport = FakeTransport::http(vec![
            token_response(),
            card_response("7355372766134157401"),
            message_response("om_throttled"),
            ok_response(),
            ok_response(),
            ok_response(),
            ok_response(),
        ]);
        let sender = sender(&transport);
        let mut stream = started_stream(&sender).throttle(INTERVAL);

        block_on(stream.append("A")).expect("first update");
        block_on(stream.append("B")).expect("buffered second update");
        assert_eq!(transport.calls().len(), 4);
        assert_eq!(stream.content(), Some("AB"));
        assert_eq!(stream.flushed_content(), Some("A"));
        assert!(stream.has_buffered_content());
        assert!(stream.next_flush_in().is_some_and(|wait| wait <= INTERVAL));

        make_due(&mut stream);
        block_on(stream.append("C")).expect("coalesced update");
        block_on(stream.append("D")).expect("buffered tail");
        block_on(stream.finish()).expect("flushed and finished");

        assert_eq!(stream.content(), Some("ABCD"));
        assert_eq!(stream.flushed_content(), Some("ABCD"));
        assert!(!stream.has_buffered_content());
        assert!(stream.is_finished());
        assert_eq!(stream.next_sequence(), 5);
        assert_eq!(stream.next_flush_in(), None);

        let calls = transport.calls();
        assert_eq!(calls.len(), 7);
        assert_card_content_update(&calls[3], 1, "A");
        assert_card_content_update(&calls[4], 2, "ABC");
        assert_card_content_update(&calls[5], 3, "ABCD");
        assert_eq!(calls[6].method, HttpMethod::Patch);
        assert_eq!(calls[6].body["sequence"], 4);
    }

    #[test]
    fn explicit_flush_bypasses_the_interval_without_duplicate_updates() {
        let transport = FakeTransport::http(vec![
            token_response(),
            card_response("7355372766134157402"),
            message_response("om_flush"),
            ok_response(),
            ok_response(),
        ]);
        let sender = sender(&transport);
        let mut stream = started_stream(&sender).throttle(INTERVAL);

        block_on(stream.append("A")).expect("first update");
        block_on(stream.append("B")).expect("buffered update");
        block_on(stream.flush()).expect("explicit flush");
        let calls_after_flush = transport.calls().len();
        block_on(stream.flush()).expect("duplicate flush is a no-op");

        assert_eq!(calls_after_flush, 5);
        assert_eq!(transport.calls().len(), calls_after_flush);
        assert_eq!(stream.flushed_content(), Some("AB"));
    }

    #[test]
    fn finish_bypasses_the_interval_for_the_tail_and_settings_update() {
        let transport = FakeTransport::http(vec![
            token_response(),
            card_response("7355372766134157410"),
            message_response("om_finish_bypass"),
            ok_response(),
            ok_response(),
            ok_response(),
        ]);
        let sender = sender(&transport);
        let mut stream = started_stream(&sender).throttle(Duration::from_secs(60));

        block_on(stream.append("A")).expect("first update");
        block_on(stream.append("B")).expect("buffered tail");
        assert!(stream.next_flush_in().is_some_and(|wait| !wait.is_zero()));

        block_on(stream.finish()).expect("tail flushed and stream closed");

        let calls = transport.calls();
        assert_eq!(calls.len(), 6);
        assert_card_content_update(&calls[3], 1, "A");
        assert_card_content_update(&calls[4], 2, "AB");
        assert_eq!(calls[5].method, HttpMethod::Patch);
        assert_eq!(calls[5].body["sequence"], 3);
        assert!(stream.is_finished());
    }

    #[test]
    fn replays_pending_content_before_flushing_newer_buffered_content() {
        let transport = FakeTransport::new(vec![
            Ok(token_response()),
            Ok(card_response("7355372766134157403")),
            Ok(message_response("om_pending")),
            Err(Error::Transport("content response lost".to_owned())),
            Ok(ok_response()),
            Ok(ok_response()),
        ]);
        let sender = sender(&transport);
        let mut stream = started_stream(&sender).throttle(INTERVAL);

        let error = block_on(stream.append("A")).expect_err("ambiguous first update");
        assert!(matches!(error, Error::Transport(_)));
        assert!(stream.has_pending_operation());
        assert_eq!(stream.content(), Some("A"));
        assert_eq!(stream.flushed_content(), None);

        block_on(stream.append("B")).expect("new content stays buffered");
        assert_eq!(transport.calls().len(), 4);
        make_due(&mut stream);
        assert!(block_on(stream.flush_if_due()).expect("pending replay"));
        assert_eq!(stream.flushed_content(), Some("A"));
        assert_eq!(stream.content(), Some("AB"));
        assert!(stream.has_buffered_content());

        block_on(stream.flush()).expect("newer buffer flushed");
        assert_eq!(stream.flushed_content(), Some("AB"));
        assert!(!stream.has_pending_operation());

        let calls = transport.calls();
        assert_eq!(calls.len(), 6);
        assert_eq!(calls[3].body["sequence"], 1);
        assert_eq!(calls[4].body["sequence"], 1);
        assert_eq!(calls[3].body["uuid"], calls[4].body["uuid"]);
        assert_card_content_update(&calls[5], 2, "AB");
    }

    #[test]
    fn invalid_buffered_content_does_not_replace_the_previous_snapshot() {
        let transport = FakeTransport::http(vec![
            token_response(),
            card_response("7355372766134157404"),
            message_response("om_validation"),
            ok_response(),
        ]);
        let sender = sender(&transport);
        let mut stream = started_stream(&sender).throttle(INTERVAL);

        block_on(stream.append("valid")).expect("first update");
        let error = block_on(stream.set_content(String::new())).expect_err("empty content");
        assert!(matches!(error, Error::Validation(_)));
        assert_eq!(stream.content(), Some("valid"));
        assert_eq!(stream.flushed_content(), Some("valid"));
        assert!(!stream.has_buffered_content());
        assert_eq!(transport.calls().len(), 4);
    }

    #[test]
    fn empty_finish_synchronizes_the_fallback_content() {
        let transport = FakeTransport::http(vec![
            token_response(),
            card_response("7355372766134157405"),
            message_response("om_empty"),
            ok_response(),
            ok_response(),
        ]);
        let sender = sender(&transport);
        let mut stream = started_stream(&sender).throttle(INTERVAL);

        block_on(stream.finish()).expect("empty stream finished");

        assert_eq!(stream.content(), Some("(no content)"));
        assert_eq!(stream.flushed_content(), Some("(no content)"));
        assert!(!stream.has_buffered_content());
        assert!(stream.is_finished());
    }

    #[test]
    fn wrapping_an_updated_stream_starts_a_new_throttle_window() {
        let transport = FakeTransport::http(vec![
            token_response(),
            card_response("7355372766134157406"),
            message_response("om_existing"),
            ok_response(),
        ]);
        let sender = sender(&transport);
        let mut immediate = started_stream(&sender);
        block_on(immediate.append("A")).expect("immediate update");

        let mut stream = immediate.throttle(INTERVAL);
        block_on(stream.append("B")).expect("buffered after wrapping");

        assert_eq!(transport.calls().len(), 4);
        assert_eq!(stream.content(), Some("AB"));
        assert_eq!(stream.flushed_content(), Some("A"));
        assert!(stream.has_buffered_content());
    }

    #[test]
    fn wrapping_a_pending_stream_preserves_the_attempted_content() {
        let transport = FakeTransport::new(vec![
            Ok(token_response()),
            Ok(card_response("7355372766134157407")),
            Ok(message_response("om_existing_pending")),
            Err(Error::Transport("content response lost".to_owned())),
            Ok(ok_response()),
        ]);
        let sender = sender(&transport);
        let mut immediate = started_stream(&sender);
        let error = block_on(immediate.append("A")).expect_err("ambiguous update");
        assert!(matches!(error, Error::Transport(_)));

        let mut stream = immediate.throttle(INTERVAL);
        assert_eq!(stream.content(), Some("A"));
        assert_eq!(stream.flushed_content(), None);
        assert!(stream.has_pending_operation());
        make_due(&mut stream);
        assert!(block_on(stream.flush_if_due()).expect("pending replay"));

        assert_eq!(stream.content(), Some("A"));
        assert_eq!(stream.flushed_content(), Some("A"));
        assert!(!stream.has_buffered_content());
        assert!(!stream.has_pending_operation());
    }

    #[test]
    fn pending_empty_finish_keeps_fallback_content_and_replays_settings() {
        let transport = FakeTransport::new(vec![
            Ok(token_response()),
            Ok(card_response("7355372766134157408")),
            Ok(message_response("om_pending_finish")),
            Ok(ok_response()),
            Err(Error::Transport("finish response lost".to_owned())),
            Ok(ok_response()),
        ]);
        let sender = sender(&transport);
        let mut stream = started_stream(&sender).throttle(INTERVAL);

        let error = block_on(stream.finish()).expect_err("ambiguous finish");
        assert!(matches!(error, Error::Transport(_)));
        assert_eq!(stream.content(), Some("(no content)"));
        assert_eq!(stream.flushed_content(), Some("(no content)"));
        assert!(!stream.has_buffered_content());
        assert!(stream.has_pending_operation());

        let calls_before_append = transport.calls().len();
        let error = block_on(stream.append("late")).expect_err("finish remains pending");
        assert!(matches!(error, Error::Validation(_)));
        assert_eq!(transport.calls().len(), calls_before_append);

        block_on(stream.finish()).expect("finish replayed");
        assert!(stream.is_finished());
        let calls = transport.calls();
        assert_eq!(calls[4].body["sequence"], 2);
        assert_eq!(calls[5].body["sequence"], 2);
        assert_eq!(calls[4].body["uuid"], calls[5].body["uuid"]);
    }

    #[test]
    fn pending_empty_fallback_is_preserved_before_finish_can_start() {
        let transport = FakeTransport::new(vec![
            Ok(token_response()),
            Ok(card_response("7355372766134157409")),
            Ok(message_response("om_pending_fallback")),
            Err(Error::Transport("fallback response lost".to_owned())),
            Ok(ok_response()),
            Ok(ok_response()),
        ]);
        let sender = sender(&transport);
        let mut stream = started_stream(&sender).throttle(INTERVAL);

        let error = block_on(stream.finish()).expect_err("ambiguous fallback update");
        assert!(matches!(error, Error::Transport(_)));
        assert_eq!(stream.content(), Some("(no content)"));
        assert_eq!(stream.flushed_content(), None);
        assert!(stream.has_buffered_content());
        assert!(stream.has_pending_operation());

        make_due(&mut stream);
        assert!(block_on(stream.flush_if_due()).expect("fallback replay"));
        assert_eq!(stream.content(), Some("(no content)"));
        assert_eq!(stream.flushed_content(), Some("(no content)"));
        assert!(!stream.has_buffered_content());
        assert!(!stream.has_pending_operation());

        block_on(stream.finish()).expect("settings update");
        assert!(stream.is_finished());
        let calls = transport.calls();
        assert_eq!(calls[3].body["sequence"], 1);
        assert_eq!(calls[4].body["sequence"], 1);
        assert_eq!(calls[3].body["uuid"], calls[4].body["uuid"]);
        assert_eq!(calls[5].body["sequence"], 2);
    }

    fn sender(transport: &FakeTransport) -> MessageSender<FakeTransport> {
        let client = crate::lark_openapi::OpenApiClient::new(
            ChannelConfig::new("cli_a", "secret"),
            transport.clone(),
        );
        MessageSender::new(client)
    }

    fn started_stream<'a>(
        sender: &'a MessageSender<FakeTransport>,
    ) -> MarkdownStream<'a, FakeTransport> {
        block_on(
            sender
                .markdown_stream_message(Recipient::Chat("oc_123".to_owned()))
                .max_attempts(1)
                .start(),
        )
        .expect("started stream")
    }

    fn make_due<T>(stream: &mut ThrottledMarkdownStream<'_, T>) {
        stream.last_update_attempt_at = Some(
            Instant::now()
                .checked_sub(stream.min_update_interval)
                .expect("past instant"),
        );
    }
}