1use std::collections::BTreeMap;
17use std::sync::Arc;
18
19use rig_core::catalog::ModelSpec;
20use rig_core::completion::Message;
21use rig_core::conversation::{Author, Change, Conversation, Entry, MessageId, Spliced};
22
23use crate::session::replay;
24use crate::{SessionError, Store};
25
26mod pi;
27
28pub use pi::{
29 CompactionPreparation, CompactionSettings, FileLists, SUMMARIZATION_SYSTEM_PROMPT,
30 SummaryFailure, clamp_max_tokens, estimate_message_tokens, estimate_tokens, find_cut_point,
31 merge_split_turn, no_history, prepare_compaction, prepare_compaction_at, request_tokens,
32 serialize_conversation, summarization_request, summary_request, summary_text,
33 turn_prefix_request,
34};
35
36const OPENS: &str = "The conversation history before this point was compacted into the following summary:\n\n<summary>\n";
38
39const CLOSES: &str = "\n</summary>";
41
42#[derive(Clone, Debug, Default, PartialEq, Eq)]
47pub struct SummaryState {
48 pub summary: String,
51 pub read_files: Vec<String>,
53 pub modified_files: Vec<String>,
55}
56
57impl SummaryState {
58 pub fn previous_summary(&self) -> Option<String> {
62 Some(self.with_files()).filter(|text| !text.is_empty())
63 }
64
65 pub fn message(&self) -> Option<String> {
68 self.previous_summary().map(|_| self.wrapped())
69 }
70
71 pub fn read(text: &str) -> Self {
74 let framed = text.strip_prefix(OPENS);
75 let Some(body) = framed.and_then(|text| text.strip_suffix(CLOSES)) else {
76 return Self {
77 summary: text.to_owned(),
78 ..Self::default()
79 };
80 };
81 let (body, modified_files) = pi::take_file_list(body, "modified-files");
82 let (summary, read_files) = pi::take_file_list(body, "read-files");
83 Self {
84 summary: summary.to_owned(),
85 read_files,
86 modified_files,
87 }
88 }
89
90 fn with_files(&self) -> String {
92 let files = pi::format_file_operations(&self.read_files, &self.modified_files);
93 format!("{}{files}", pi::without_file_lists(&self.summary))
94 }
95
96 fn wrapped(&self) -> String {
98 format!("{OPENS}{}{CLOSES}", self.with_files())
99 }
100}
101
102pub fn fit_to_model<'t>(
110 text: &'t str,
111 spec: &ModelSpec,
112 summary_tokens: u64,
113) -> (std::borrow::Cow<'t, str>, Option<u64>) {
114 let chars = |tokens: u64| usize::try_from(tokens.saturating_mul(3)).unwrap_or(usize::MAX);
115 let most = spec
116 .output_limit()
117 .map_or(summary_tokens, |limit| summary_tokens.min(u64::from(limit)));
118 let (most, budget) = match spec.input_limit().map(u64::from) {
119 None => (most, 400_000),
120 Some(limit) => {
121 let room = limit.saturating_sub(16_384).max(limit / 2);
122 let most = most.min(room / 2);
123 let least = chars(16_384u64.min(limit / 8));
124 (most, chars(room.saturating_sub(most)).max(least))
125 }
126 };
127 let known = spec.max_output_tokens.is_some() || spec.context_window.is_some();
128 let most = known.then_some(most);
129 let left_out = text.chars().count().saturating_sub(budget);
130 if left_out == 0 {
131 return (std::borrow::Cow::Borrowed(text), most);
132 }
133 let start = text
134 .char_indices()
135 .nth(left_out)
136 .map_or(text.len(), |(start, _)| start);
137 let rest = text.get(start..).unwrap_or_default();
138 let cut = format!("[\u{2026} {left_out} earlier characters left out]\n{rest}");
139 (std::borrow::Cow::Owned(cut), most)
140}
141
142pub fn summary_author() -> Author {
144 Author::plugin("compaction")
145}
146
147pub fn summary_in(change: &Change) -> Option<MessageId> {
152 let Change::Splice { removed, with, .. } = change else {
153 return None;
154 };
155 let kept = |id: &MessageId| with.iter().any(|entry| entry.id == Some(*id));
156 if removed.iter().all(kept) {
157 return None;
158 }
159 let summary = summary_author();
160 with.iter()
161 .find(|entry| entry.author.as_ref() == Some(&summary))
162 .and_then(|entry| entry.id)
163}
164
165pub fn summarized_prefix(conversation: &Conversation) -> usize {
170 let author = summary_author();
171 let summaries = conversation.entries();
172 summaries
173 .take_while(|entry| entry.author == Some(&author))
174 .count()
175}
176
177pub fn already_compacted(conversation: &Conversation) -> bool {
180 let author = summary_author();
181 let mut summaries = conversation.entries();
182 let summary = summaries
183 .find(|entry| entry.author == Some(&author))
184 .map(|entry| entry.id);
185 summary.is_some_and(|summary| conversation.entries().all(|entry| entry.id <= summary))
186}
187
188pub fn apply_summary(
198 conversation: &mut Conversation,
199 replaces: &[Arc<Message>],
200 state: &SummaryState,
201) -> Option<Spliced> {
202 let end = replaces.len();
203 let now = conversation.messages();
204 let read = now.get(..end).filter(|now| !now.is_empty())?;
205 let same = read
206 .iter()
207 .zip(replaces)
208 .all(|(now, read)| Arc::ptr_eq(now, read));
209 if !same || !pi::may_keep_from(now, end) {
210 return None;
211 }
212 let summary = Entry::new(Message::user(state.wrapped())).by(summary_author());
213 Some(conversation.splice(..end, [summary], summary_author()))
214}
215
216pub fn summary_state(conversation: &Conversation) -> SummaryState {
220 let newest = summarized_prefix(conversation).checked_sub(1);
221 let newest = newest.and_then(|at| conversation.messages().get(at)?.rag_text());
222 newest
223 .map(|text| SummaryState::read(&text))
224 .unwrap_or_default()
225}
226
227pub fn summarized_in_log(
238 store: &dyn Store,
239 log: &str,
240) -> Result<BTreeMap<MessageId, Vec<Entry>>, SessionError> {
241 let mut replaced = BTreeMap::new();
242 replay(store, log, |before, recorded| {
243 let change = &recorded.change;
244 let (Some(summary), Change::Splice { removed, with, .. }) = (summary_in(change), change)
245 else {
246 if matches!(change, Change::Reset { .. }) {
247 replaced.clear();
248 }
249 return;
250 };
251 let kept = |id: &&MessageId| with.iter().any(|entry| entry.id == Some(**id));
252 let taken = removed.iter().filter(|id| !kept(id));
253 let taken = taken.filter_map(|id| before.entry(before.index_of(*id)?));
254 replaced.entry(summary).or_insert_with(|| taken.collect());
255 })?;
256 Ok(replaced)
257}
258
259#[cfg(test)]
260mod tests;