1use std::collections::{HashMap, HashSet};
29use std::fmt;
30use std::ops::{Bound, Range, RangeBounds};
31use std::str::FromStr;
32use std::sync::Arc;
33
34use serde::{Deserialize, Serialize, Serializer};
35
36use crate::completion::Message;
37use crate::message::{StopReason, ToolCall, UserContent};
38
39mod aside;
40mod author;
41mod facts;
42mod lineage;
43mod note;
44pub mod pairing;
45mod turn;
46
47pub use aside::{Aside, AsideId};
48pub use author::{Author, AuthorKind, RequestId};
49pub use facts::ResultFacts;
50pub use lineage::Lineage;
51pub use note::Note;
52pub use pairing::CallRef;
53pub use turn::{StopCause, TURN, TurnEnd, TurnRecord};
54
55use pairing::{Normalized, PairingError};
56
57pub const FILE_OPEN: &str = "<file path=\"";
60
61pub const FILE_CLOSE: &str = "</file>";
63
64pub fn attached_file(text: &str) -> Option<(&str, &str)> {
67 let rest = text.strip_prefix(FILE_OPEN)?.strip_suffix(FILE_CLOSE)?;
68 rest.split_once("\">\n")
69}
70
71pub const UNATTACHED_OPEN: &str = "<unattached path=\"";
74
75pub const UNATTACHED_CLOSE: &str = "</unattached>";
77
78pub fn unattached(text: &str) -> Option<(&str, &str)> {
81 let rest = text
82 .strip_prefix(UNATTACHED_OPEN)?
83 .strip_suffix(UNATTACHED_CLOSE)?;
84 rest.split_once("\">\n")
85}
86
87pub fn is_attachment_text(text: &str) -> bool {
91 attached_file(text).is_some() || unattached(text).is_some()
92}
93
94#[derive(Clone, Debug, Default, Serialize, Deserialize)]
110#[serde(try_from = "State")]
111pub struct Conversation {
112 messages: Vec<Arc<Message>>,
115 meta: Vec<Meta>,
117 #[serde(serialize_with = "saved_asides")]
119 asides: Vec<(AsideId, Aside)>,
120 #[serde(skip_serializing_if = "Option::is_none")]
121 halted: Option<Halt>,
122 next_id: u64,
124 next_aside: u64,
126 #[serde(skip)]
129 revision: u64,
130 #[serde(skip)]
132 lineage: Lineage,
133 #[serde(skip)]
134 unsaved: Vec<ChangeRecord>,
135 #[serde(skip)]
138 clock: Option<u64>,
139}
140
141#[derive(Clone, Debug, Serialize, Deserialize)]
145struct Meta {
146 id: MessageId,
147 #[serde(default, skip_serializing_if = "Option::is_none")]
148 author: Option<Author>,
149 #[serde(default)]
151 made: u64,
152 #[serde(default, skip_serializing_if = "ResultFacts::is_empty")]
154 facts: ResultFacts,
155}
156
157#[derive(Deserialize)]
159struct State {
160 #[serde(default)]
161 messages: Vec<Arc<Message>>,
162 #[serde(default)]
163 meta: Vec<Meta>,
164 #[serde(default)]
165 asides: Vec<(AsideId, Aside)>,
166 #[serde(default)]
167 halted: Option<Halt>,
168 #[serde(default)]
169 next_id: u64,
170 #[serde(default)]
171 next_aside: u64,
172}
173
174impl TryFrom<State> for Conversation {
175 type Error = ApplyError;
176
177 fn try_from(state: State) -> Result<Self, ApplyError> {
178 if state.meta.len() != state.messages.len() {
179 return Err(mismatch(format!(
180 "{} messages but {} ids",
181 state.messages.len(),
182 state.meta.len()
183 )));
184 }
185 let mut ids = HashSet::new();
186 if let Some(meta) = state
187 .meta
188 .iter()
189 .find(|meta| meta.id.0 >= state.next_id || !ids.insert(meta.id))
190 {
191 return Err(mismatch(format!("message id {} reused", meta.id.0)));
192 }
193 let mut asides = HashSet::new();
194 if let Some((id, _)) = state
195 .asides
196 .iter()
197 .find(|(id, _)| id.0 >= state.next_aside || !asides.insert(*id))
198 {
199 return Err(mismatch(format!("aside id {} reused", id.0)));
200 }
201 Ok(Self {
202 messages: state.messages,
203 meta: state.meta,
204 asides: state.asides,
205 halted: state.halted,
206 next_id: state.next_id,
207 next_aside: state.next_aside,
208 revision: 0,
209 lineage: Lineage::DETACHED,
210 unsaved: Vec::new(),
211 clock: None,
212 })
213 }
214}
215
216fn saved_asides<S: Serializer>(
217 asides: &[(AsideId, Aside)],
218 serializer: S,
219) -> Result<S::Ok, S::Error> {
220 serializer.collect_seq(asides.iter().filter(|(_, aside)| aside.saved))
221}
222
223impl PartialEq for Conversation {
224 fn eq(&self, other: &Self) -> bool {
225 let saved = |(_, aside): &&(AsideId, Aside)| aside.saved;
226 let same = |a: &Meta, b: &Meta| a.id == b.id && a.author == b.author && a.facts == b.facts;
227 self.messages == other.messages
228 && self.meta.len() == other.meta.len()
229 && self.meta.iter().zip(&other.meta).all(|(a, b)| same(a, b))
230 && self.halted == other.halted
231 && self
232 .asides
233 .iter()
234 .filter(saved)
235 .eq(other.asides.iter().filter(saved))
236 }
237}
238
239#[derive(
243 Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize,
244)]
245#[serde(transparent)]
246pub struct MessageId(u64);
247
248impl MessageId {
249 pub const fn get(self) -> u64 {
251 self.0
252 }
253}
254
255impl fmt::Display for MessageId {
257 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
258 write!(f, "m{}", self.0)
259 }
260}
261
262impl FromStr for MessageId {
264 type Err = ParseIdError;
265
266 fn from_str(text: &str) -> Result<Self, ParseIdError> {
267 parse_id(text, 'm').map(Self)
268 }
269}
270
271#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
273#[error("{0:?} is not an id")]
274pub struct ParseIdError(pub String);
275
276fn parse_id(text: &str, prefix: char) -> Result<u64, ParseIdError> {
278 let number = text.strip_prefix(prefix).unwrap_or(text);
279 number.parse().map_err(|_| ParseIdError(text.to_owned()))
280}
281
282#[derive(
287 Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize,
288)]
289pub struct MessageKey {
290 pub made: u64,
292 pub id: MessageId,
294}
295
296#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
301pub struct Entry {
302 pub message: Arc<Message>,
304 #[serde(default, skip_serializing_if = "Option::is_none")]
307 pub author: Option<Author>,
308 #[serde(default, skip_serializing_if = "Option::is_none")]
310 pub id: Option<MessageId>,
311 #[serde(default, skip_serializing_if = "Option::is_none")]
315 pub made: Option<u64>,
316 #[serde(default, skip_serializing_if = "ResultFacts::is_empty")]
319 pub facts: ResultFacts,
320}
321
322impl Entry {
323 pub fn new(message: impl Into<Arc<Message>>) -> Self {
325 Self {
326 message: message.into(),
327 author: None,
328 id: None,
329 made: None,
330 facts: ResultFacts::default(),
331 }
332 }
333
334 #[must_use]
336 pub fn with_facts(self, facts: ResultFacts) -> Self {
337 Self { facts, ..self }
338 }
339
340 pub fn is_stop_marker(&self) -> bool {
343 is_stop_marker(&self.message, self.author.as_ref())
344 }
345
346 #[must_use]
348 pub fn by(self, author: Author) -> Self {
349 Self {
350 author: Some(author),
351 ..self
352 }
353 }
354}
355
356impl From<Message> for Entry {
357 fn from(message: Message) -> Self {
358 Self::new(message)
359 }
360}
361
362impl From<Arc<Message>> for Entry {
363 fn from(message: Arc<Message>) -> Self {
364 Self::new(message)
365 }
366}
367
368#[derive(Clone, Copy, Debug)]
370pub struct EntryRef<'c> {
371 pub index: usize,
373 pub id: MessageId,
375 pub message: &'c Message,
377 pub author: Option<&'c Author>,
379 pub made: u64,
381 pub facts: &'c ResultFacts,
383}
384
385impl EntryRef<'_> {
386 pub fn key(&self) -> MessageKey {
388 MessageKey {
389 made: self.made,
390 id: self.id,
391 }
392 }
393
394 pub fn is_users_own(&self) -> bool {
398 self.says_something()
399 && self
400 .author
401 .is_none_or(|author| author.kind == AuthorKind::User && author.title.is_none())
402 }
403
404 pub fn is_request(&self) -> bool {
408 self.says_something()
409 && self
410 .author
411 .is_none_or(|author| author.kind != AuthorKind::Rig)
412 }
413
414 fn says_something(&self) -> bool {
417 match self.message {
418 Message::User { content } => content.iter().any(|item| !pairing::is_result(item)),
419 _ => false,
420 }
421 }
422
423 pub fn is_stop_marker(&self) -> bool {
426 is_stop_marker(self.message, self.author)
427 }
428}
429
430fn is_stop_marker(message: &Message, author: Option<&Author>) -> bool {
432 let by_rig = author.is_some_and(|author| author.kind == AuthorKind::Rig);
433 by_rig
434 && matches!(message, Message::User { content }
435 if matches!(content.as_slice(), [UserContent::Text(text)] if text.text == STOPPED))
436}
437
438pub const STOPPED: &str = "[The user stopped the request above before it was fully answered; \
442 any reply to it above was cut off there. \
443 Do not carry it out unless asked again.]";
444
445#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
448#[serde(rename_all = "snake_case")]
449pub enum Halt {
450 Stopped,
453 #[default]
456 Kept,
457}
458
459#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
462#[serde(tag = "type", rename_all = "snake_case")]
463pub enum Change {
464 Push {
466 entries: Vec<Entry>,
468 },
469 Splice {
474 from: usize,
476 to: usize,
478 removed: Vec<MessageId>,
480 with: Vec<Entry>,
482 },
483 Halt {
485 halt: Option<Halt>,
487 },
488 Aside {
490 id: AsideId,
492 aside: Option<Aside>,
494 },
495 Reset {
499 state: Box<Conversation>,
501 },
502}
503
504#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
506pub struct ChangeRecord {
507 #[serde(flatten)]
509 pub change: Change,
510 #[serde(default, skip_serializing_if = "Option::is_none")]
513 pub by: Option<Author>,
514 #[serde(default, skip_serializing_if = "Option::is_none")]
516 pub at: Option<u64>,
517}
518
519#[derive(Clone, Debug, Default, PartialEq, Eq)]
521pub struct Spliced {
522 pub removed: Vec<MessageId>,
524 pub changed: Vec<MessageId>,
526 pub restored: Vec<MessageId>,
528}
529
530#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
533pub enum ApplyError {
534 #[error("{0}")]
536 Mismatch(String),
537}
538
539fn mismatch(why: String) -> ApplyError {
540 ApplyError::Mismatch(why)
541}
542
543pub fn short_id(id: &str) -> &str {
545 id.get(..8).unwrap_or(id)
546}
547
548impl Conversation {
549 pub fn new() -> Self {
551 Self::default()
552 }
553
554 pub fn from_messages(messages: impl IntoIterator<Item = impl Into<Arc<Message>>>) -> Self {
557 let mut conversation = Self::new();
558 conversation.extend(messages);
559 conversation
560 }
561
562 pub fn messages(&self) -> &[Arc<Message>] {
566 &self.messages
567 }
568
569 pub fn into_messages(self) -> Vec<Arc<Message>> {
572 self.messages
573 }
574
575 pub fn entries(&self) -> impl DoubleEndedIterator<Item = EntryRef<'_>> + ExactSizeIterator {
577 let entries = self.messages.iter().zip(&self.meta).enumerate();
578 entries.map(|(index, (message, meta))| EntryRef {
579 index,
580 id: meta.id,
581 message,
582 author: meta.author.as_ref(),
583 made: meta.made,
584 facts: &meta.facts,
585 })
586 }
587
588 pub fn entry(&self, at: usize) -> Option<Entry> {
591 let meta = self.meta.get(at)?;
592 Some(Entry {
593 message: self.messages.get(at)?.clone(),
594 author: meta.author.clone(),
595 id: Some(meta.id),
596 made: Some(meta.made),
597 facts: meta.facts.clone(),
598 })
599 }
600
601 pub fn id(&self, at: usize) -> Option<MessageId> {
603 self.meta.get(at).map(|meta| meta.id)
604 }
605
606 pub fn key(&self, at: usize) -> Option<MessageKey> {
608 let meta = self.meta.get(at)?;
609 Some(MessageKey {
610 made: meta.made,
611 id: meta.id,
612 })
613 }
614
615 pub fn index_of_key(&self, key: MessageKey) -> Option<usize> {
618 let at = self.index_of(key.id)?;
619 (self.meta.get(at)?.made == key.made).then_some(at)
620 }
621
622 pub fn author(&self, at: usize) -> Option<&Author> {
625 self.meta.get(at)?.author.as_ref()
626 }
627
628 pub fn index_of(&self, id: MessageId) -> Option<usize> {
630 self.meta.iter().position(|meta| meta.id == id)
631 }
632
633 pub fn next_id(&self) -> MessageId {
636 MessageId(self.next_id)
637 }
638
639 pub fn start_of(&self, id: MessageId) -> usize {
644 self.index_of(id).unwrap_or(if id.0 >= self.next_id {
645 self.messages.len()
646 } else {
647 0
648 })
649 }
650
651 pub fn turn_start(&self) -> MessageId {
654 let trailing = self.messages.iter().rev();
655 let trailing = trailing
656 .take_while(|message| matches!(&***message, Message::User { .. }))
657 .count();
658 let first = self.messages.len().saturating_sub(trailing);
659 let answered = (trailing > 0).then(|| self.id(first)).flatten();
660 answered.unwrap_or(MessageId(self.next_id))
661 }
662
663 pub fn asked(&self, start: MessageId) -> impl Iterator<Item = EntryRef<'_>> {
669 let entries = self.entries().skip(self.start_of(start));
670 entries.take_while(|entry| matches!(entry.message, Message::User { .. }))
671 }
672
673 pub fn request(&self, start: MessageId) -> Option<EntryRef<'_>> {
677 self.asked(start).find(EntryRef::is_request)
678 }
679
680 pub fn halted(&self) -> Option<Halt> {
683 self.halted
684 }
685
686 pub fn awaits_model(&self) -> bool {
691 let read = self.messages.iter().rev().map(Arc::as_ref).find(|message| {
692 !matches!(message, Message::Assistant(turn)
693 if turn.stop.as_ref().is_some_and(StopReason::is_failure))
694 });
695 self.halted.is_none() && matches!(read, Some(Message::User { .. }))
696 }
697
698 pub fn pending_calls(&self) -> Vec<(CallRef, ToolCall)> {
701 let Some((at, calls)) = pairing::pending(&self.messages) else {
702 return Vec::new();
703 };
704 let Some(reply) = self.id(at) else {
705 return Vec::new();
706 };
707 let calls = calls.into_iter();
708 calls
709 .map(|(index, call)| (CallRef { reply, index }, call.clone()))
710 .collect()
711 }
712
713 pub fn validate(&self) -> Result<(), PairingError> {
716 pairing::validate(&self.messages)
717 }
718
719 pub fn asides(&self) -> impl DoubleEndedIterator<Item = (AsideId, &Aside)> {
721 self.asides.iter().map(|(id, aside)| (*id, aside))
722 }
723
724 pub fn aside(&self, id: AsideId) -> Option<&Aside> {
726 let mut asides = self.asides.iter();
727 asides
728 .find(|(known, _)| *known == id)
729 .map(|(_, aside)| aside)
730 }
731
732 pub fn push(&mut self, entry: impl Into<Entry>) -> MessageId {
739 let entry = entry.into();
740 let by = entry.author.clone();
741 let mut ids = self.push_entries(vec![entry], by);
742 ids.pop().unwrap_or(MessageId(self.next_id))
743 }
744
745 pub fn extend(&mut self, messages: impl IntoIterator<Item = impl Into<Arc<Message>>>) {
748 let entries = messages.into_iter().map(Entry::new).collect();
749 self.push_entries(entries, None);
750 }
751
752 fn push_entries(&mut self, entries: Vec<Entry>, by: Option<Author>) -> Vec<MessageId> {
753 if entries.is_empty() {
754 return Vec::new();
755 }
756 let stop = (self.halted == Some(Halt::Stopped))
757 .then(|| Entry::new(Message::user(STOPPED)).by(Author::rig()));
758 let mut next = self.next_id;
759 let mut ids = Vec::with_capacity(entries.len());
760 let mut fill = |mut entry: Entry| {
761 let id = MessageId(next);
762 next = next.saturating_add(1);
763 entry.id = Some(id);
764 entry.made = None;
765 (entry, id)
766 };
767 let stop = stop.map(|entry| fill(entry).0);
768 let entries = entries.into_iter().map(|entry| {
769 let (entry, id) = fill(entry);
770 ids.push(id);
771 entry
772 });
773 let entries = stop.into_iter().chain(entries).collect();
774 self.record(Change::Push { entries }, by);
775 ids
776 }
777
778 pub fn splice(
787 &mut self,
788 range: impl RangeBounds<usize>,
789 with: impl IntoIterator<Item = Entry>,
790 by: Author,
791 ) -> Spliced {
792 self.replace(range, with.into_iter().collect(), by, false)
793 }
794
795 pub fn restore(
820 &mut self,
821 range: impl RangeBounds<usize>,
822 with: impl IntoIterator<Item = Entry>,
823 by: Author,
824 ) -> Spliced {
825 self.replace(range, with.into_iter().collect(), by, true)
826 }
827
828 pub fn set_entries(&mut self, entries: Vec<Entry>, by: Author) -> Spliced {
833 let same = |entry: &Entry, at: usize| {
834 entry.id.is_some()
835 && entry.id == self.id(at)
836 && entry.author.as_ref() == self.author(at)
837 && Some(&entry.message) == self.messages.get(at)
838 && Some(&entry.facts) == self.meta.get(at).map(|meta| &meta.facts)
839 };
840 let (len, count) = (self.messages.len(), entries.len());
841 let start = entries
842 .iter()
843 .enumerate()
844 .take_while(|(at, entry)| same(entry, *at))
845 .count();
846 let end = entries
847 .iter()
848 .rev()
849 .zip((0..len).rev())
850 .take(len.min(count).saturating_sub(start))
851 .take_while(|(entry, at)| same(entry, *at))
852 .count();
853 if start == len && start == count {
854 return Spliced::default();
855 }
856 let with = entries
857 .into_iter()
858 .skip(start)
859 .take(count.saturating_sub(start).saturating_sub(end));
860 self.restore(start..len.saturating_sub(end), with, by)
861 }
862
863 fn replace(
866 &mut self,
867 range: impl RangeBounds<usize>,
868 mut with: Vec<Entry>,
869 by: Author,
870 restores: bool,
871 ) -> Spliced {
872 let len = self.messages.len();
873 let to = match range.end_bound() {
874 Bound::Included(&end) => end.saturating_add(1),
875 Bound::Excluded(&end) => end,
876 Bound::Unbounded => len,
877 }
878 .min(len);
879 let from = match range.start_bound() {
880 Bound::Included(&start) => start,
881 Bound::Excluded(&start) => start.saturating_add(1),
882 Bound::Unbounded => 0,
883 }
884 .min(to);
885 if from == to && with.is_empty() {
886 return Spliced::default();
887 }
888 let replaced = self.meta.get(from..to).unwrap_or_default();
889 let removed: Vec<MessageId> = replaced.iter().map(|meta| meta.id).collect();
890 let in_range: HashSet<MessageId> = removed.iter().copied().collect();
891 let mut outside = None;
892 let mut taken = HashSet::new();
893 let mut restored = Vec::new();
894 let mut next = self.next_id;
895 for entry in &mut with {
896 let id = entry.id.filter(|id| !taken.contains(id));
897 if let Some(id) = id.filter(|id| in_range.contains(id)) {
898 taken.insert(id);
899 entry.made = None;
900 continue;
901 }
902 let back = id.filter(|id| {
903 restores
904 && id.0 < self.next_id
905 && !self.outside(from..to, &mut outside).contains(id)
906 });
907 if let Some(id) = back {
908 taken.insert(id);
909 restored.push(id);
910 continue;
911 }
912 entry.id = Some(MessageId(next));
913 entry.made = None;
914 next = next.saturating_add(1);
915 }
916 let changed = with.iter().filter_map(|entry| entry.id).collect();
917 let gone = removed.iter().filter(|id| !taken.contains(id)).copied();
918 let spliced = Spliced {
919 removed: gone.collect(),
920 changed,
921 restored,
922 };
923 let change = Change::Splice {
924 from,
925 to,
926 removed,
927 with,
928 };
929 self.record(change, Some(by));
930 spliced
931 }
932
933 fn outside<'s>(
936 &self,
937 range: Range<usize>,
938 outside: &'s mut Option<HashSet<MessageId>>,
939 ) -> &'s HashSet<MessageId> {
940 outside.get_or_insert_with(|| {
941 let before = self.meta.get(..range.start).unwrap_or_default();
942 let after = self.meta.get(range.end..).unwrap_or_default();
943 before.iter().chain(after).map(|meta| meta.id).collect()
944 })
945 }
946
947 pub fn edit_each(
972 &mut self,
973 by: Author,
974 mut edit: impl FnMut(EntryRef<'_>) -> Option<Message>,
975 ) -> Vec<MessageId> {
976 let rewritten: Vec<(usize, MessageId, Message)> = self
977 .entries()
978 .filter_map(|entry| {
979 let (at, id) = (entry.index, entry.id);
980 edit(entry).map(|message| (at, id, message))
981 })
982 .collect();
983 let (Some(&(first, ..)), Some(&(last, ..))) = (rewritten.first(), rewritten.last()) else {
984 return Vec::new();
985 };
986 let ids = rewritten.iter().map(|(_, id, _)| *id).collect();
987 let mut rewritten = rewritten.into_iter().peekable();
988 let entries: Vec<Entry> = (first..=last)
989 .filter_map(|at| match rewritten.next_if(|(next, ..)| *next == at) {
990 Some((_, id, message)) => Some(Entry {
991 message: Arc::new(message),
992 author: self.author(at).cloned(),
993 id: Some(id),
994 made: None,
995 facts: self
996 .meta
997 .get(at)
998 .map(|meta| meta.facts.clone())
999 .unwrap_or_default(),
1000 }),
1001 None => self.entry(at),
1002 })
1003 .collect();
1004 self.splice(first..=last, entries, by);
1005 ids
1006 }
1007
1008 pub fn normalize(&mut self, by: Author) -> Normalized {
1015 let entries = (0..self.messages.len()).filter_map(|at| self.entry(at));
1016 let normalized = pairing::normalize(entries.collect());
1017 let into: HashMap<MessageId, MessageId> = normalized
1018 .merged
1019 .iter()
1020 .map(|(kept, gone)| (*gone, *kept))
1021 .collect();
1022 let dropped: HashSet<MessageId> = normalized.dropped.iter().copied().collect();
1023 let anchor = |id: MessageId| -> Option<MessageId> {
1026 let before = self.meta.iter().rev().skip_while(|meta| meta.id != id);
1027 before
1028 .map(|meta| meta.id)
1029 .find_map(|id| match into.get(&id) {
1030 Some(kept) => Some(*kept),
1031 None => (!dropped.contains(&id)).then_some(id),
1032 })
1033 };
1034 let moves: Vec<(AsideId, Option<MessageId>)> = self
1035 .asides
1036 .iter()
1037 .filter_map(|(at, aside)| {
1038 let after = aside.after?;
1039 let moves = into.contains_key(&after) || dropped.contains(&after);
1040 moves.then(|| (*at, anchor(after)))
1041 })
1042 .collect();
1043 self.set_entries(normalized.entries.clone(), by.clone());
1044 for (id, after) in moves {
1045 self.move_aside(id, after, &by);
1046 }
1047 normalized
1048 }
1049
1050 pub fn halt(&mut self, halt: Halt) -> bool {
1053 let halts = self.awaits_model();
1054 if halts {
1055 self.record(Change::Halt { halt: Some(halt) }, None);
1056 }
1057 halts
1058 }
1059
1060 pub fn add_aside(&mut self, aside: Aside) -> AsideId {
1063 let id = AsideId(self.next_aside);
1064 if aside.saved {
1065 let by = Some(aside.author.clone());
1066 let aside = Some(aside);
1067 self.record(Change::Aside { id, aside }, by);
1068 } else {
1069 self.place_aside(id, aside);
1070 }
1071 id
1072 }
1073
1074 pub fn replace_aside(&mut self, id: AsideId, aside: Aside) -> bool {
1078 let Some(was_saved) = self.aside(id).map(|old| old.saved) else {
1079 return false;
1080 };
1081 if aside.saved {
1082 let by = Some(aside.author.clone());
1083 let aside = Some(aside);
1084 self.record(Change::Aside { id, aside }, by);
1085 } else {
1086 self.place_aside(id, aside);
1088 if was_saved {
1089 self.log(Change::Aside { id, aside: None }, None);
1090 }
1091 }
1092 true
1093 }
1094
1095 pub fn remove_aside(&mut self, id: AsideId) -> Option<Aside> {
1097 let at = self.asides.iter().position(|(known, _)| *known == id)?;
1098 let (_, aside) = self.asides.remove(at);
1099 if aside.saved {
1100 self.log(Change::Aside { id, aside: None }, None);
1101 }
1102 Some(aside)
1103 }
1104
1105 fn move_aside(&mut self, id: AsideId, after: Option<MessageId>, by: &Author) {
1108 let Some(aside) = self.aside(id).cloned() else {
1109 return;
1110 };
1111 let aside = aside.after(after);
1112 if aside.saved {
1113 let aside = Some(aside);
1114 self.record(Change::Aside { id, aside }, Some(by.clone()));
1115 } else {
1116 self.place_aside(id, aside);
1117 }
1118 }
1119
1120 fn place_aside(&mut self, id: AsideId, aside: Aside) {
1123 self.next_aside = self.next_aside.max(id.0.saturating_add(1));
1124 match self.asides.iter_mut().find(|(known, _)| *known == id) {
1125 Some((_, slot)) => *slot = aside,
1126 None => self.asides.push((id, aside)),
1127 }
1128 }
1129
1130 pub fn apply(&mut self, change: &ChangeRecord) -> Result<(), ApplyError> {
1136 let at = change.at.unwrap_or_else(|| self.now());
1137 self.change(&change.change, at)?;
1138 self.revision = self.revision.saturating_add(1);
1139 Ok(())
1140 }
1141
1142 pub fn dated<R>(&mut self, at: u64, edit: impl FnOnce(&mut Self) -> R) -> R {
1160 let outer = self.clock.replace(at);
1161 let made = edit(self);
1162 self.clock = outer;
1163 made
1164 }
1165
1166 fn now(&self) -> u64 {
1169 self.clock.unwrap_or_else(crate::now_ms)
1170 }
1171
1172 pub fn unsaved(&self) -> &[ChangeRecord] {
1174 &self.unsaved
1175 }
1176
1177 pub fn take_unsaved(&mut self) -> Vec<ChangeRecord> {
1179 std::mem::take(&mut self.unsaved)
1180 }
1181
1182 pub fn revision(&self) -> u64 {
1187 self.revision
1188 }
1189
1190 pub fn lineage(&self) -> Lineage {
1194 self.lineage
1195 }
1196
1197 pub fn continues(&self, lineage: Lineage, changes: u64) -> bool {
1203 let base = self.revision.saturating_sub(self.unsaved.len() as u64);
1204 base == changes
1205 && (self.lineage == lineage || (changes == 0 && self.lineage == Lineage::EMPTY))
1206 }
1207
1208 pub fn continue_log(&mut self, lineage: Lineage, changes: u64) {
1212 self.lineage = lineage;
1213 self.revision = changes.saturating_add(self.unsaved.len() as u64);
1214 }
1215
1216 pub fn view(&self) -> Self {
1219 Self {
1220 asides: self.asides.clone(),
1221 ..self.request_view()
1222 }
1223 }
1224
1225 pub fn request_view(&self) -> Self {
1228 Self {
1229 messages: self.messages.clone(),
1230 meta: self.meta.clone(),
1231 asides: Vec::new(),
1232 halted: self.halted,
1233 next_id: self.next_id,
1234 next_aside: self.next_aside,
1235 revision: self.revision,
1236 lineage: self.lineage,
1237 unsaved: Vec::new(),
1238 clock: None,
1239 }
1240 }
1241
1242 fn record(&mut self, change: Change, by: Option<Author>) {
1244 let at = self.now();
1245 match self.change(&change, at) {
1246 Ok(()) => self.log_at(change, by, at),
1247 Err(error) => tracing::error!(%error, "a conversation edit did not fit; dropped"),
1248 }
1249 }
1250
1251 fn log(&mut self, change: Change, by: Option<Author>) {
1253 let at = self.now();
1254 self.log_at(change, by, at);
1255 }
1256
1257 fn log_at(&mut self, change: Change, by: Option<Author>, at: u64) {
1258 self.revision = self.revision.saturating_add(1);
1259 self.unsaved.push(ChangeRecord {
1260 change,
1261 by,
1262 at: Some(at),
1263 });
1264 }
1265
1266 fn change(&mut self, change: &Change, at: u64) -> Result<(), ApplyError> {
1270 let meta = |entry: &Entry, made: u64| Meta {
1271 id: entry.id.unwrap_or_default(),
1272 author: entry.author.clone(),
1273 made,
1274 facts: entry.facts.clone(),
1275 };
1276 match change {
1277 Change::Push { entries } => {
1278 let end = self.meta.len();
1279 let next = self.new_ids(entries, &HashSet::new(), end..end)?;
1280 for entry in entries {
1281 self.messages.push(entry.message.clone());
1282 self.meta.push(meta(entry, entry.made.unwrap_or(at)));
1283 }
1284 self.next_id = next;
1285 self.halted = None;
1286 }
1287 Change::Splice {
1288 from,
1289 to,
1290 removed,
1291 with,
1292 } => {
1293 let (from, to) = (*from, *to);
1294 let reaches_end = to == self.meta.len();
1295 let replaced = self.meta.get(from..to).ok_or_else(|| {
1296 mismatch(format!(
1297 "splice of {from}..{to} in {} entries",
1298 self.meta.len()
1299 ))
1300 })?;
1301 if !replaced.iter().map(|meta| &meta.id).eq(removed) {
1302 return Err(mismatch(format!("splice of {from}..{to} names other ids")));
1303 }
1304 let kept: HashMap<MessageId, u64> =
1305 replaced.iter().map(|meta| (meta.id, meta.made)).collect();
1306 let next = self.new_ids(with, &removed.iter().copied().collect(), from..to)?;
1307 let metas: Vec<Meta> = with
1308 .iter()
1309 .map(|entry| {
1310 let made = entry.id.and_then(|id| kept.get(&id).copied());
1311 meta(entry, made.or(entry.made).unwrap_or(at))
1312 })
1313 .collect();
1314 self.messages
1315 .splice(from..to, with.iter().map(|entry| entry.message.clone()));
1316 self.meta.splice(from..to, metas);
1317 self.next_id = next;
1318 if reaches_end {
1319 self.halted = None;
1320 }
1321 }
1322 Change::Halt { halt } => self.halted = *halt,
1323 Change::Aside {
1324 id,
1325 aside: Some(aside),
1326 } => self.place_aside(*id, aside.clone()),
1327 Change::Aside { id, aside: None } => {
1328 let at = self.asides.iter().position(|(known, _)| known == id);
1329 let at = at.ok_or_else(|| mismatch(format!("no aside {}", id.0)))?;
1330 self.asides.remove(at);
1331 }
1332 Change::Reset { state } => {
1335 let (unsaved, revision) = (std::mem::take(&mut self.unsaved), self.revision);
1336 let lineage = self.lineage;
1337 *self = state.view();
1338 self.unsaved = unsaved;
1339 self.revision = revision;
1340 self.lineage = lineage;
1341 }
1342 }
1343 Ok(())
1344 }
1345
1346 fn new_ids(
1351 &self,
1352 entries: &[Entry],
1353 reused: &HashSet<MessageId>,
1354 range: Range<usize>,
1355 ) -> Result<u64, ApplyError> {
1356 let mut next = self.next_id;
1357 let mut seen = HashSet::new();
1358 let mut outside = None;
1359 for entry in entries {
1360 let id = entry
1361 .id
1362 .ok_or_else(|| mismatch("a recorded entry without an id".to_owned()))?;
1363 if id.0 >= next {
1364 next = id.0.saturating_add(1);
1365 continue;
1366 }
1367 let fits = reused.contains(&id)
1368 || (id.0 < self.next_id
1369 && !self.outside(range.clone(), &mut outside).contains(&id));
1370 if !fits || !seen.insert(id) {
1371 return Err(mismatch(format!("message id {} reused", id.0)));
1372 }
1373 }
1374 Ok(next)
1375 }
1376}
1377
1378#[cfg(test)]
1379mod tests;