Skip to main content

rig_core/conversation/
note.rs

1//! Notes in a transcript ([`Note`]).
2
3use serde::{Deserialize, Serialize};
4
5use super::{Aside, Author, MessageId};
6
7/// A note in a transcript: an [`Aside`] of kind [`Note::KIND`] that rig's
8/// terminal view draws after the entry it follows, with what it shows
9/// collapsed, in a line, and what it shows expanded. It never reaches a
10/// model, and it is saved, restored and forked with the conversation; one
11/// made [`unsaved`](Aside::unsaved) is not saved. Rig's print mode and
12/// ACP show no notes.
13///
14/// ```
15/// use rig_core::conversation::{Author, Conversation, Note};
16/// use rig_core::message::Message;
17///
18/// let mut conversation = Conversation::new();
19/// conversation.push(Message::user("Why tabs?"));
20/// let after = conversation.id(0);
21/// let note = Note::new("Why tabs?", "The project's style says so.");
22/// conversation.add_aside(note.aside(Author::plugin("btw"), after));
23/// ```
24#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
25pub struct Note {
26    /// What it shows collapsed, in a line.
27    pub title: String,
28    /// What it shows expanded.
29    pub text: String,
30}
31
32impl Note {
33    /// The kind of the asides that hold a note.
34    pub const KIND: &'static str = "note";
35
36    /// A note titled `title`, showing `text` expanded.
37    pub fn new(title: impl Into<String>, text: impl Into<String>) -> Self {
38        Self {
39            title: title.into(),
40            text: text.into(),
41        }
42    }
43
44    /// The saved aside by `author` that holds this note, following the
45    /// entry `after` (`None`: before the first).
46    pub fn aside(self, author: Author, after: Option<MessageId>) -> Aside {
47        let value = serde_json::json!({ "title": self.title, "text": self.text });
48        Aside::new(Self::KIND, author, value).after(after)
49    }
50
51    /// The note `aside` holds, when it holds one.
52    pub fn of(aside: &Aside) -> Option<Self> {
53        aside.read(Self::KIND)
54    }
55}