use assistant_context_editor::SavedContextMetadata; use chrono::{DateTime, Utc}; use gpui::{prelude::*, Entity}; use crate::thread_store::{SerializedThreadMetadata, ThreadStore}; pub enum HistoryEntry { Thread(SerializedThreadMetadata), Context(SavedContextMetadata), } impl HistoryEntry { pub fn updated_at(&self) -> DateTime { match self { HistoryEntry::Thread(thread) => thread.updated_at, HistoryEntry::Context(context) => context.mtime.to_utc(), } } } pub struct HistoryStore { thread_store: Entity, context_store: Entity, } impl HistoryStore { pub fn new( thread_store: Entity, context_store: Entity, _cx: &mut Context, ) -> Self { Self { thread_store, context_store, } } /// Returns the number of history entries. pub fn entry_count(&self, cx: &mut Context) -> usize { self.entries(cx).len() } pub fn entries(&self, cx: &mut Context) -> Vec { let mut history_entries = Vec::new(); for thread in self.thread_store.update(cx, |this, _cx| this.threads()) { history_entries.push(HistoryEntry::Thread(thread)); } for context in self.context_store.update(cx, |this, _cx| this.contexts()) { history_entries.push(HistoryEntry::Context(context)); } history_entries.sort_unstable_by_key(|entry| std::cmp::Reverse(entry.updated_at())); history_entries } pub fn recent_entries(&self, limit: usize, cx: &mut Context) -> Vec { self.entries(cx).into_iter().take(limit).collect() } }