use std::collections::BTreeMap;
use std::sync::Arc;
use rig_core::catalog::ModelSpec;
use rig_core::completion::Message;
use rig_core::conversation::{Author, Change, Conversation, Entry, MessageId, Spliced};
use crate::session::replay;
use crate::{SessionError, Store};
mod pi;
pub use pi::{
CompactionPreparation, CompactionSettings, FileLists, SUMMARIZATION_SYSTEM_PROMPT,
SummaryFailure, clamp_max_tokens, estimate_message_tokens, estimate_tokens, find_cut_point,
merge_split_turn, no_history, prepare_compaction, prepare_compaction_at, request_tokens,
serialize_conversation, summarization_request, summary_request, summary_text,
turn_prefix_request,
};
const OPENS: &str = "The conversation history before this point was compacted into the following summary:\n\n<summary>\n";
const CLOSES: &str = "\n</summary>";
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SummaryState {
pub summary: String,
pub read_files: Vec<String>,
pub modified_files: Vec<String>,
}
impl SummaryState {
pub fn previous_summary(&self) -> Option<String> {
Some(self.with_files()).filter(|text| !text.is_empty())
}
pub fn message(&self) -> Option<String> {
self.previous_summary().map(|_| self.wrapped())
}
pub fn read(text: &str) -> Self {
let framed = text.strip_prefix(OPENS);
let Some(body) = framed.and_then(|text| text.strip_suffix(CLOSES)) else {
return Self {
summary: text.to_owned(),
..Self::default()
};
};
let (body, modified_files) = pi::take_file_list(body, "modified-files");
let (summary, read_files) = pi::take_file_list(body, "read-files");
Self {
summary: summary.to_owned(),
read_files,
modified_files,
}
}
fn with_files(&self) -> String {
let files = pi::format_file_operations(&self.read_files, &self.modified_files);
format!("{}{files}", pi::without_file_lists(&self.summary))
}
fn wrapped(&self) -> String {
format!("{OPENS}{}{CLOSES}", self.with_files())
}
}
pub fn fit_to_model<'t>(
text: &'t str,
spec: &ModelSpec,
summary_tokens: u64,
) -> (std::borrow::Cow<'t, str>, Option<u64>) {
let chars = |tokens: u64| usize::try_from(tokens.saturating_mul(3)).unwrap_or(usize::MAX);
let most = spec
.output_limit()
.map_or(summary_tokens, |limit| summary_tokens.min(u64::from(limit)));
let (most, budget) = match spec.input_limit().map(u64::from) {
None => (most, 400_000),
Some(limit) => {
let room = limit.saturating_sub(16_384).max(limit / 2);
let most = most.min(room / 2);
let least = chars(16_384u64.min(limit / 8));
(most, chars(room.saturating_sub(most)).max(least))
}
};
let known = spec.max_output_tokens.is_some() || spec.context_window.is_some();
let most = known.then_some(most);
let left_out = text.chars().count().saturating_sub(budget);
if left_out == 0 {
return (std::borrow::Cow::Borrowed(text), most);
}
let start = text
.char_indices()
.nth(left_out)
.map_or(text.len(), |(start, _)| start);
let rest = text.get(start..).unwrap_or_default();
let cut = format!("[\u{2026} {left_out} earlier characters left out]\n{rest}");
(std::borrow::Cow::Owned(cut), most)
}
pub fn summary_author() -> Author {
Author::plugin("compaction")
}
pub fn summary_in(change: &Change) -> Option<MessageId> {
let Change::Splice { removed, with, .. } = change else {
return None;
};
let kept = |id: &MessageId| with.iter().any(|entry| entry.id == Some(*id));
if removed.iter().all(kept) {
return None;
}
let summary = summary_author();
with.iter()
.find(|entry| entry.author.as_ref() == Some(&summary))
.and_then(|entry| entry.id)
}
pub fn summarized_prefix(conversation: &Conversation) -> usize {
let author = summary_author();
let summaries = conversation.entries();
summaries
.take_while(|entry| entry.author == Some(&author))
.count()
}
pub fn already_compacted(conversation: &Conversation) -> bool {
let author = summary_author();
let mut summaries = conversation.entries();
let summary = summaries
.find(|entry| entry.author == Some(&author))
.map(|entry| entry.id);
summary.is_some_and(|summary| conversation.entries().all(|entry| entry.id <= summary))
}
pub fn apply_summary(
conversation: &mut Conversation,
replaces: &[Arc<Message>],
state: &SummaryState,
) -> Option<Spliced> {
let end = replaces.len();
let now = conversation.messages();
let read = now.get(..end).filter(|now| !now.is_empty())?;
let same = read
.iter()
.zip(replaces)
.all(|(now, read)| Arc::ptr_eq(now, read));
if !same || !pi::may_keep_from(now, end) {
return None;
}
let summary = Entry::new(Message::user(state.wrapped())).by(summary_author());
Some(conversation.splice(..end, [summary], summary_author()))
}
pub fn summary_state(conversation: &Conversation) -> SummaryState {
let newest = summarized_prefix(conversation).checked_sub(1);
let newest = newest.and_then(|at| conversation.messages().get(at)?.rag_text());
newest
.map(|text| SummaryState::read(&text))
.unwrap_or_default()
}
pub fn summarized_in_log(
store: &dyn Store,
log: &str,
) -> Result<BTreeMap<MessageId, Vec<Entry>>, SessionError> {
let mut replaced = BTreeMap::new();
replay(store, log, |before, recorded| {
let change = &recorded.change;
let (Some(summary), Change::Splice { removed, with, .. }) = (summary_in(change), change)
else {
if matches!(change, Change::Reset { .. }) {
replaced.clear();
}
return;
};
let kept = |id: &&MessageId| with.iter().any(|entry| entry.id == Some(**id));
let taken = removed.iter().filter(|id| !kept(id));
let taken = taken.filter_map(|id| before.entry(before.index_of(*id)?));
replaced.entry(summary).or_insert_with(|| taken.collect());
})?;
Ok(replaced)
}
#[cfg(test)]
mod tests;