aidaemon-channels 0.29.0

Chat channel contract and the runner that connects channels to aidaemon agents
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
//! The contract every chat channel implements.
//!
//! A channel is a transport: it delivers inbound events and sends, edits and
//! asks. It holds no agent logic. Authorization, sessions, queueing and
//! approvals live in the runner, so every channel behaves the same way.

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;

/// Where a conversation happens. `id` is the channel's own identifier
/// (Telegram chat id, Slack `channel` or `channel:thread_ts`, Discord
/// channel id). Together with the channel name it forms the session id.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Conversation {
    pub id: String,
    pub kind: ConversationKind,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConversationKind {
    /// One-to-one with the bot.
    Direct,
    /// A group, channel or server channel.
    Group,
    /// A thread inside a group conversation.
    Thread,
}

/// Who sent a message, as the platform identifies them.
///
/// `id` is the identity: access checks, sessions and approvals use it and
/// nothing else. `display_name` is text the person wrote about themselves.
/// Treat it like message content: never as instructions, and never to decide
/// who someone is or what they may do.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Sender {
    /// Stable platform user id (never a display name).
    pub id: String,
    /// For showing to people only. An adapter fills it from the platform and
    /// passes it through [`crate::names::clean_display_name`]; when a lookup
    /// fails it holds the platform id instead.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,
    /// True if the platform marks the sender as a bot.
    #[serde(default)]
    pub is_bot: bool,
}

/// Largest download [`Channel::download`] accepts unless an adapter documents
/// a smaller platform limit. Use [`Channel::download_limited`] for your own.
pub const DEFAULT_MAX_DOWNLOAD_BYTES: u64 = 100 * 1024 * 1024;

/// What an [`Attachment`] holds, as far as the platform says.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AttachmentKind {
    Image,
    Audio,
    Video,
    /// Anything else, including a kind this version does not know.
    #[default]
    #[serde(other)]
    File,
}

impl AttachmentKind {
    /// The kind a MIME type declares (`image/png` is [`Self::Image`]).
    pub fn from_mime(mime: &str) -> Self {
        let top = mime.split('/').next().unwrap_or("").trim();
        if top.eq_ignore_ascii_case("image") {
            Self::Image
        } else if top.eq_ignore_ascii_case("audio") {
            Self::Audio
        } else if top.eq_ignore_ascii_case("video") {
            Self::Video
        } else {
            Self::File
        }
    }

    /// The kind a file name's extension suggests, for platforms that do not
    /// always report a MIME type.
    fn from_name(name: &str) -> Self {
        let extension = name.rsplit_once('.').map_or("", |(_, ext)| ext);
        match extension.to_ascii_lowercase().as_str() {
            "png" | "jpg" | "jpeg" | "gif" | "webp" => Self::Image,
            "mp3" | "ogg" | "oga" | "wav" | "flac" | "m4a" | "aac" => Self::Audio,
            "mp4" | "mov" | "webm" | "mkv" => Self::Video,
            _ => Self::File,
        }
    }
}

/// A file or image attached to an inbound message. Adapters fill what the
/// platform provides; the bytes are fetched lazily with [`Channel::download`].
///
/// Built with [`Attachment::new`] and the `with_` methods, so a new field
/// does not break adapters written against an older version.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Attachment {
    pub name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub size_bytes: Option<u64>,
    /// Platform URL or file id to download from.
    pub source: String,
    /// What it holds. The platform's word: check the bytes before relying
    /// on it.
    #[serde(default)]
    pub kind: AttachmentKind,
}

impl Attachment {
    /// An attachment named `name`, fetched from `source`. Its kind comes
    /// from the name's extension until [`Self::with_mime_type`] or
    /// [`Self::with_kind`] says better.
    pub fn new(name: impl Into<String>, source: impl Into<String>) -> Self {
        let name = name.into();
        let kind = AttachmentKind::from_name(&name);
        Self {
            name,
            mime_type: None,
            size_bytes: None,
            source: source.into(),
            kind,
        }
    }

    /// Set the MIME type (when known); its kind replaces the one guessed
    /// from the name.
    pub fn with_mime_type(mut self, mime_type: Option<String>) -> Self {
        if let Some(mime) = mime_type.as_deref() {
            self.kind = AttachmentKind::from_mime(mime);
        }
        self.mime_type = mime_type;
        self
    }

    pub fn with_size_bytes(mut self, size_bytes: Option<u64>) -> Self {
        self.size_bytes = size_bytes;
        self
    }

    /// Set the kind directly, for a platform that says what it is.
    pub fn with_kind(mut self, kind: AttachmentKind) -> Self {
        self.kind = kind;
        self
    }
}

/// Built with [`InboundMessage::new`] and the `with_` methods, so a new field
/// does not break adapters written against an older version.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct InboundMessage {
    pub conversation: Conversation,
    pub sender: Sender,
    /// Platform message id, used for replies and edits.
    pub message_id: String,
    pub text: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub attachments: Vec<Attachment>,
    /// The bot was addressed explicitly (mention, reply to the bot, or a
    /// slash command). Always true in direct conversations.
    pub addressed: bool,
    /// Platform id of the message this one answers: a reply's target, or
    /// the root of the Slack thread it sits in. `None` for a message that
    /// answers nothing.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent_message_id: Option<String>,
    /// The platform's thread this message is in (Telegram forum topic id,
    /// Slack `thread_ts`, Discord thread channel id). `None` outside a
    /// thread, including a top-level Slack message the bot will answer in a
    /// new thread (that thread is only its [`Conversation`]).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub thread_id: Option<String>,
}

impl InboundMessage {
    /// A message with no attachments and no parent. `addressed` starts true
    /// in a direct conversation and false anywhere else.
    pub fn new(
        conversation: Conversation,
        sender: Sender,
        message_id: impl Into<String>,
        text: impl Into<String>,
    ) -> Self {
        let addressed = conversation.kind == ConversationKind::Direct;
        Self {
            conversation,
            sender,
            message_id: message_id.into(),
            text: text.into(),
            attachments: Vec::new(),
            addressed,
            parent_message_id: None,
            thread_id: None,
        }
    }

    pub fn with_attachments(mut self, attachments: Vec<Attachment>) -> Self {
        self.attachments = attachments;
        self
    }

    pub fn with_addressed(mut self, addressed: bool) -> Self {
        self.addressed = addressed;
        self
    }

    pub fn with_parent_message_id(mut self, parent: Option<String>) -> Self {
        self.parent_message_id = parent;
        self
    }

    pub fn with_thread_id(mut self, thread: Option<String>) -> Self {
        self.thread_id = thread;
        self
    }
}

/// A message to send. `text` is Markdown; adapters convert it to the
/// platform's dialect and split it to fit `max_message_len`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OutboundMessage {
    pub text: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reply_to: Option<String>,
}

impl OutboundMessage {
    pub fn text(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            reply_to: None,
        }
    }
}

/// Identifies a message the bot sent, for later edits.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct MessageRef {
    pub conversation_id: String,
    pub message_id: String,
}

/// One button on an approval prompt.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ApprovalChoice {
    /// Returned in [`ApprovalAnswer::choice_id`].
    pub id: String,
    pub label: String,
}

/// A question with buttons, used for tool approvals.
///
/// Built with [`ApprovalPrompt::new`], so a new field does not break code
/// written against an older version.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ApprovalPrompt {
    /// Opaque id chosen by the runner; echoed back in the answer.
    pub id: String,
    pub conversation: Conversation,
    /// Markdown body describing what is being approved: what a channel
    /// that draws no card of its own shows.
    pub text: String,
    pub choices: Vec<ApprovalChoice>,
    /// The same request as typed facts, for a channel that renders its own
    /// card (a web UI, a native dialog). `None` for prompts that are not
    /// about a tool call.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub details: Option<ApprovalDetails>,
}

impl ApprovalPrompt {
    pub fn new(
        id: impl Into<String>,
        conversation: Conversation,
        text: impl Into<String>,
        choices: Vec<ApprovalChoice>,
    ) -> Self {
        Self {
            id: id.into(),
            conversation,
            text: text.into(),
            choices,
            details: None,
        }
    }

    pub fn with_details(mut self, details: ApprovalDetails) -> Self {
        self.details = Some(details);
        self
    }
}

/// What an approval prompt asks about, as data: the call waiting, why the
/// run paused and, when an approval policy asked, its typed request (a
/// summary, risk, effects and the card's facts: [`aidaemon_core::ApprovalCard`]).
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ApprovalDetails {
    /// The tool the call would run.
    pub tool: String,
    /// Its arguments, as the model sent them.
    pub arguments: serde_json::Value,
    /// Why the run paused, in the words of the hook that paused it.
    pub reason: String,
    /// The owner request, when the pause asks the owner.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub request: Option<aidaemon_core::OwnerApprovalRequest>,
}

impl ApprovalDetails {
    pub fn from_interruption(interruption: &aidaemon_core::Interruption) -> Self {
        Self {
            tool: interruption.call.name.clone(),
            arguments: interruption.call.arguments.clone(),
            reason: interruption.reason.clone(),
            request: match &interruption.kind {
                aidaemon_core::InterruptKind::OwnerApproval(request) => Some(request.clone()),
                _ => None,
            },
        }
    }
}

/// A run's live status as data, rebuilt by the runner from the run's
/// events. The runner shows it with [`Channel::send_status`] and
/// [`Channel::update_status`]; their defaults draw it as Markdown
/// ([`RunProgress::to_markdown`]), and a channel with its own status UI
/// overrides them to render the fields.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RunProgress {
    pub phase: ProgressPhase,
    /// The run's latest tool calls (at most eight), oldest first.
    pub tools: Vec<ToolProgress>,
    /// The model's text streamed so far in the current turn, while
    /// working.
    pub streamed: String,
}

/// Whether the run is still going, and how it ended.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProgressPhase {
    Working,
    Ended(RunEnding),
}

/// How a run ended, as its status line says it.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RunEnding {
    Done,
    /// It finished with something left undone.
    PartlyDone,
    /// It paused for an approval (an [`ApprovalPrompt`] follows).
    WaitingForApproval,
    /// It was stopped (cancelled).
    Stopped,
    /// A tool finished it as blocked.
    Blocked,
    /// The run could not be run at all.
    Failed,
    /// Any other end: a limit, a provider failure, no progress.
    Ended,
}

impl RunEnding {
    /// The word the default status line uses.
    pub fn word(self) -> &'static str {
        match self {
            Self::Done => "Done",
            Self::PartlyDone => "Partly done",
            Self::WaitingForApproval => "Waiting for approval",
            Self::Stopped => "Stopped",
            Self::Blocked => "Blocked",
            Self::Failed => "Failed",
            Self::Ended => "Ended",
        }
    }
}

/// One tool call on a status.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ToolProgress {
    pub call_id: String,
    pub tool: String,
    pub state: ToolState,
}

/// Where one tool call stands.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolState {
    Running,
    /// It ended, with its receipt's status (a refused call never ran).
    Ended(aidaemon_core::ReceiptStatus),
}

impl RunProgress {
    /// A run that has just started.
    pub fn working() -> Self {
        Self {
            phase: ProgressPhase::Working,
            tools: Vec::new(),
            streamed: String::new(),
        }
    }

    /// The default rendering: an italic status word, a line per tool call
    /// and, while working, the streamed text, cut to `max_chars`.
    pub fn to_markdown(&self, max_chars: usize) -> String {
        let mut out = match self.phase {
            ProgressPhase::Working => "_Working..._".to_string(),
            ProgressPhase::Ended(ending) => format!("_{}_", ending.word()),
        };
        for tool in &self.tools {
            out.push('\n');
            out.push_str(&match tool.state {
                ToolState::Running => format!("- Running `{}`", tool.tool),
                ToolState::Ended(status) => {
                    format!("- `{}`: {}", tool.tool, crate::render::receipt_word(status))
                }
            });
        }
        let stream = self.streamed.trim();
        if self.phase == ProgressPhase::Working && !stream.is_empty() {
            out.push_str("\n\n");
            let room = max_chars.saturating_sub(out.chars().count() + 4);
            out.push_str(&crate::render::tail_chars(stream, room));
        }
        crate::render::truncate_chars(&out, max_chars)
    }
}

/// A button press (or the text-command fallback) answering a prompt.
///
/// Built with [`ApprovalAnswer::new`], so a new field does not break adapters
/// written against an older version.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ApprovalAnswer {
    pub prompt_id: String,
    pub choice_id: String,
    pub sender: Sender,
    pub conversation: Conversation,
    /// The message whose button was pressed, so its buttons can be removed
    /// with [`Channel::remove_buttons`] even when the prompt is no longer
    /// known (it expired, or the process restarted). `None` when the
    /// platform did not say, or the answer was typed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub message: Option<MessageRef>,
}

impl ApprovalAnswer {
    pub fn new(
        prompt_id: impl Into<String>,
        choice_id: impl Into<String>,
        sender: Sender,
        conversation: Conversation,
    ) -> Self {
        Self {
            prompt_id: prompt_id.into(),
            choice_id: choice_id.into(),
            sender,
            conversation,
            message: None,
        }
    }

    /// Record the message whose button was pressed: the [`MessageRef`]
    /// that [`Channel::ask`] returned for this prompt. A channel with
    /// buttons should always set it when the platform says which message
    /// was pressed (keep the ref from `ask` by prompt id if it does not):
    /// the runner then closes that exact message, by
    /// [`edit`](Channel::edit) or [`remove_buttons`](Channel::remove_buttons),
    /// even when the prompt is no longer known (it expired, or the process
    /// restarted), so stale buttons never stay pressable. The conformance
    /// kit checks it (`channel.answer_message_removes_buttons`). A typed
    /// `/approve` answer has no message and leaves it unset.
    ///
    /// ```
    /// use aidaemon_channels::{ApprovalAnswer, Conversation, ConversationKind, MessageRef, Sender};
    ///
    /// // `pressed` is the ref `ask` returned when it showed prompt `p1`.
    /// let pressed = MessageRef { conversation_id: "c1".into(), message_id: "m7".into() };
    /// let answer = ApprovalAnswer::new(
    ///     "p1",
    ///     "approve",
    ///     Sender { id: "synthetic-user-1".into(), display_name: None, is_bot: false },
    ///     Conversation { id: "c1".into(), kind: ConversationKind::Direct },
    /// )
    /// .with_message(pressed.clone());
    /// assert_eq!(answer.message, Some(pressed));
    /// ```
    pub fn with_message(mut self, message: MessageRef) -> Self {
        self.message = Some(message);
        self
    }
}

/// A command the app offers in the platform's own command menu (Discord and
/// Slack slash commands, Telegram's `/` menu).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommandSpec {
    /// Without the leading `/`; lowercase letters, digits and `_`.
    pub name: String,
    pub description: String,
}

impl CommandSpec {
    pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
        }
    }
}

/// A command invoked through the platform's command UI rather than typed
/// as text (a typed `/command` arrives as an [`InboundMessage`]). Answer it
/// with [`Channel::answer_command`]: some platforms show the invocation as
/// pending until it is answered.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommandInvocation {
    pub conversation: Conversation,
    pub sender: Sender,
    /// Without the leading `/`.
    pub name: String,
    /// Everything after the command name, trimmed.
    pub args: String,
    /// Adapter-defined token for answering this invocation.
    pub handle: String,
}

impl CommandInvocation {
    /// The invocation as the text a user would have typed.
    pub fn as_text(&self) -> String {
        if self.args.is_empty() {
            format!("/{}", self.name)
        } else {
            format!("/{} {}", self.name, self.args)
        }
    }
}

/// Everything a channel reports to the runner.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum ChannelEvent {
    Message(InboundMessage),
    Approval(ApprovalAnswer),
    Command(CommandInvocation),
}

/// A file to send.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OutboundFile {
    /// File name shown to the recipient.
    pub name: String,
    pub bytes: Vec<u8>,
    pub mime_type: Option<String>,
    /// Markdown caption sent with the file.
    pub caption: Option<String>,
}

/// What a channel supports; the runner adapts to it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChannelCapabilities {
    pub edit_messages: bool,
    pub buttons: bool,
    pub typing_indicator: bool,
    pub threads: bool,
    /// [`Channel::delete`] works.
    pub delete_messages: bool,
    /// [`Channel::send_file`], [`Channel::download`] and
    /// [`Channel::download_limited`] work.
    pub files: bool,
    /// The platform has a command menu ([`Channel::register_commands`]).
    pub commands: bool,
    /// Longest message the platform accepts, in characters.
    pub max_message_len: usize,
}

impl Default for ChannelCapabilities {
    fn default() -> Self {
        Self {
            edit_messages: false,
            buttons: false,
            typing_indicator: false,
            threads: false,
            delete_messages: false,
            files: false,
            commands: false,
            max_message_len: 4000,
        }
    }
}

#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum ChannelError {
    #[error("not supported by this channel: {0}")]
    Unsupported(&'static str),
    #[error("rate limited; retry after {retry_after_secs:?}s")]
    RateLimited { retry_after_secs: Option<u64> },
    #[error("authentication failed: {0}")]
    Auth(String),
    #[error("transport error: {0}")]
    Transport(String),
    /// A download was over the size it was allowed.
    #[error("file is over the {limit_bytes} byte limit")]
    TooLarge { limit_bytes: u64 },
    #[error("{0}")]
    Other(String),
}

/// A chat transport. Implementations translate platform updates into
/// [`ChannelEvent`]s and runner requests into platform API calls.
#[async_trait]
pub trait Channel: Send + Sync {
    /// Short stable name, used as the session-id prefix (`telegram`,
    /// `slack`, `discord`, or a bot-specific name for multi-bot setups).
    fn name(&self) -> &str;

    fn capabilities(&self) -> ChannelCapabilities;

    /// Receive updates and forward them until `shutdown` is cancelled or the
    /// receiver is dropped. Reconnects on transient failures; returns an
    /// error only for unrecoverable ones (for example a revoked token).
    ///
    /// It must be re-runnable: the runner (and the conformance kit) may
    /// call `run` again after it returned, with a new sender, and updates
    /// arriving then go to the new one. A push-style channel (a webhook
    /// server) keeps its listener across runs, or starts a new one, and
    /// drops what arrives while no `run` is going.
    async fn run(
        &self,
        events: mpsc::Sender<ChannelEvent>,
        shutdown: CancellationToken,
    ) -> Result<(), ChannelError>;

    /// Send a message, splitting it if needed. Returns the last part's ref.
    async fn send(
        &self,
        conversation: &Conversation,
        message: &OutboundMessage,
    ) -> Result<MessageRef, ChannelError>;

    /// Replace the text of a message the bot sent, and remove any buttons
    /// on it. An edit always stays one message: text too long for the
    /// platform is cut at the end and marked with
    /// [`crate::split::TRUNCATION_MARK`] (see
    /// [`crate::split::truncate_for_edit`]), never split into further
    /// messages and never an error.
    async fn edit(&self, _message: &MessageRef, _text: &str) -> Result<(), ChannelError> {
        Err(ChannelError::Unsupported("edit"))
    }

    /// Show an approval prompt with buttons. The runner calls it only
    /// when [`ChannelCapabilities::buttons`] is true; otherwise, or when
    /// this returns an error, it sends the prompt's text with
    /// `/approve <id>` / `/deny <id>` commands instead. A channel without
    /// buttons leaves the default.
    ///
    /// When the prompt is answered (or expires), the runner
    /// [`edit`](Channel::edit)s the prompt's message to say how it ended,
    /// which removes the buttons; it calls
    /// [`remove_buttons`](Channel::remove_buttons) only where it cannot
    /// edit (the channel cannot, or the prompt is no longer known).
    async fn ask(&self, _prompt: &ApprovalPrompt) -> Result<MessageRef, ChannelError> {
        Err(ChannelError::Unsupported("buttons"))
    }

    /// Remove the buttons from a message, leaving its text. Best effort:
    /// the default does nothing, for channels without buttons. See
    /// [`Channel::ask`] for when the runner calls it.
    async fn remove_buttons(&self, _message: &MessageRef) -> Result<(), ChannelError> {
        Ok(())
    }

    /// Post a run's live status ([`RunProgress`]). The runner calls it
    /// once per run, when [`ChannelCapabilities::edit_messages`] is true,
    /// and then [`Channel::update_status`] as the run goes and when it
    /// ends. The default sends [`RunProgress::to_markdown`]; override both
    /// to render the status natively.
    async fn send_status(
        &self,
        conversation: &Conversation,
        progress: &RunProgress,
    ) -> Result<MessageRef, ChannelError> {
        let text = self.status_text(progress);
        self.send(conversation, &OutboundMessage::text(text)).await
    }

    /// Update the status [`Channel::send_status`] posted. The default
    /// [`edit`](Channel::edit)s it to [`RunProgress::to_markdown`].
    async fn update_status(
        &self,
        message: &MessageRef,
        progress: &RunProgress,
    ) -> Result<(), ChannelError> {
        let text = self.status_text(progress);
        self.edit(message, &text).await
    }

    /// The Markdown the default status methods send: the progress cut to
    /// this channel's message length, LaTeX rewritten as for any text
    /// ([`Channel::strips_latex`]).
    fn status_text(&self, progress: &RunProgress) -> String {
        let text = progress.to_markdown(self.capabilities().max_message_len);
        if self.strips_latex() {
            crate::latex::strip_latex_keeping_code(&text)
        } else {
            text
        }
    }

    /// Show a "typing" indicator. Best effort.
    async fn typing(&self, _conversation: &Conversation) -> Result<(), ChannelError> {
        Ok(())
    }

    /// Delete a message the bot sent.
    async fn delete(&self, _message: &MessageRef) -> Result<(), ChannelError> {
        Err(ChannelError::Unsupported("delete"))
    }

    /// Send a file, with an optional caption.
    async fn send_file(
        &self,
        _conversation: &Conversation,
        _file: &OutboundFile,
    ) -> Result<MessageRef, ChannelError> {
        Err(ChannelError::Unsupported("files"))
    }

    /// Fetch the bytes of an inbound attachment, up to the adapter's
    /// platform limit or else [`DEFAULT_MAX_DOWNLOAD_BYTES`]. An adapter
    /// keeps its token to the platform's own hosts and out of its errors
    /// and logs.
    async fn download(&self, _attachment: &Attachment) -> Result<Vec<u8>, ChannelError> {
        Err(ChannelError::Unsupported("files"))
    }

    /// [`Self::download`], refused with [`ChannelError::TooLarge`] over
    /// `max_bytes`.
    ///
    /// The default holds any adapter to the limit, but learns the true size
    /// only after [`Self::download`] returns. An adapter that can stop
    /// reading part way through should override it.
    async fn download_limited(
        &self,
        attachment: &Attachment,
        max_bytes: u64,
    ) -> Result<Vec<u8>, ChannelError> {
        let too_large = ChannelError::TooLarge {
            limit_bytes: max_bytes,
        };
        if attachment.size_bytes.is_some_and(|size| size > max_bytes) {
            return Err(too_large);
        }
        let bytes = self.download(attachment).await?;
        if bytes.len() as u64 > max_bytes {
            return Err(too_large);
        }
        Ok(bytes)
    }

    /// Offer `commands` in the platform's command menu, replacing what was
    /// offered before. A platform whose commands are configured outside the
    /// API (Slack's app manifest) accepts the call and does nothing.
    async fn register_commands(&self, _commands: &[CommandSpec]) -> Result<(), ChannelError> {
        Ok(())
    }

    /// Whether the runner rewrites LaTeX in the model's text as plain text
    /// before sending it here ([`crate::latex::strip_latex_keeping_code`]).
    /// On by default, since chat platforms show it as raw markup; override
    /// it to return `false` for one that renders math.
    fn strips_latex(&self) -> bool {
        true
    }

    /// The name a person shows on the platform, by their platform user id
    /// (`None` when the platform has none to give). It is the person's own
    /// text: see [`Sender`].
    async fn display_name(&self, _user_id: &str) -> Result<Option<String>, ChannelError> {
        Ok(None)
    }

    /// Answer a [`CommandInvocation`]. The default sends `text` to its
    /// conversation.
    async fn answer_command(
        &self,
        command: &CommandInvocation,
        text: &str,
    ) -> Result<(), ChannelError> {
        self.send(&command.conversation, &OutboundMessage::text(text))
            .await
            .map(|_| ())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_mime_type_outranks_the_name() {
        assert_eq!(Attachment::new("a.png", "s").kind, AttachmentKind::Image);
        assert_eq!(Attachment::new("a.MP3", "s").kind, AttachmentKind::Audio);
        assert_eq!(Attachment::new("a.mov", "s").kind, AttachmentKind::Video);
        assert_eq!(Attachment::new("a.pdf", "s").kind, AttachmentKind::File);
        assert_eq!(Attachment::new("noext", "s").kind, AttachmentKind::File);
        let relabelled = Attachment::new("a.png", "s").with_mime_type(Some("text/plain".into()));
        assert_eq!(relabelled.kind, AttachmentKind::File);
        let unlabelled = Attachment::new("a.png", "s").with_mime_type(None);
        assert_eq!(unlabelled.kind, AttachmentKind::Image);
    }

    #[test]
    fn older_or_newer_json_still_reads() {
        // Written before `kind` and the links existed.
        let old: InboundMessage = serde_json::from_value(serde_json::json!({
            "conversation": {"id": "c", "kind": "direct"},
            "sender": {"id": "synthetic-user-1"},
            "message_id": "m1",
            "text": "hi",
            "attachments": [{"name": "a.bin", "source": "s"}],
            "addressed": true,
        }))
        .unwrap();
        assert_eq!(old.attachments[0].kind, AttachmentKind::File);
        assert_eq!(old.parent_message_id, None);
        assert_eq!(old.thread_id, None);
        // A kind this version has never heard of.
        let unknown: Attachment = serde_json::from_value(
            serde_json::json!({"name": "a", "source": "s", "kind": "hologram"}),
        )
        .unwrap();
        assert_eq!(unknown.kind, AttachmentKind::File);
        // An unset link is not written out.
        let written = serde_json::to_value(&old).unwrap();
        assert!(written.get("parent_message_id").is_none());
    }

    struct Bare;

    #[async_trait]
    impl Channel for Bare {
        fn name(&self) -> &str {
            "bare"
        }
        fn capabilities(&self) -> ChannelCapabilities {
            ChannelCapabilities::default()
        }
        async fn run(
            &self,
            _events: mpsc::Sender<ChannelEvent>,
            _shutdown: CancellationToken,
        ) -> Result<(), ChannelError> {
            Ok(())
        }
        async fn send(
            &self,
            conversation: &Conversation,
            _message: &OutboundMessage,
        ) -> Result<MessageRef, ChannelError> {
            Ok(MessageRef {
                conversation_id: conversation.id.clone(),
                message_id: "1".into(),
            })
        }
    }

    /// Overrides only the old method, the way an older adapter would.
    struct OldAdapter(Vec<u8>);

    #[async_trait]
    impl Channel for OldAdapter {
        fn name(&self) -> &str {
            "old"
        }
        fn capabilities(&self) -> ChannelCapabilities {
            ChannelCapabilities::default()
        }
        async fn run(
            &self,
            _events: mpsc::Sender<ChannelEvent>,
            _shutdown: CancellationToken,
        ) -> Result<(), ChannelError> {
            Ok(())
        }
        async fn send(
            &self,
            conversation: &Conversation,
            _message: &OutboundMessage,
        ) -> Result<MessageRef, ChannelError> {
            Ok(MessageRef {
                conversation_id: conversation.id.clone(),
                message_id: "1".into(),
            })
        }
        async fn download(&self, _attachment: &Attachment) -> Result<Vec<u8>, ChannelError> {
            Ok(self.0.clone())
        }
    }

    #[tokio::test]
    async fn the_defaults_are_safe() {
        let attachment = Attachment::new("a", "s");
        assert!(matches!(
            Bare.download(&attachment).await,
            Err(ChannelError::Unsupported(_))
        ));
        assert!(matches!(
            Bare.download_limited(&attachment, 10).await,
            Err(ChannelError::Unsupported(_))
        ));
        let message = MessageRef {
            conversation_id: "c".into(),
            message_id: "m".into(),
        };
        assert!(Bare.remove_buttons(&message).await.is_ok());
    }

    #[tokio::test]
    async fn an_older_adapter_is_held_to_the_limit() {
        let adapter = OldAdapter(vec![0; 11]);
        let attachment = Attachment::new("a", "s");
        assert_eq!(
            adapter
                .download_limited(&attachment, 11)
                .await
                .unwrap()
                .len(),
            11
        );
        assert!(matches!(
            adapter.download_limited(&attachment, 10).await,
            Err(ChannelError::TooLarge { limit_bytes: 10 })
        ));
        // A declared size over the limit is refused without fetching.
        let declared = attachment.with_size_bytes(Some(99));
        assert!(matches!(
            OldAdapter(Vec::new()).download_limited(&declared, 10).await,
            Err(ChannelError::TooLarge { .. })
        ));
    }
}