pub struct Database { /* private fields */ }Expand description
Core database wrapper
Implementations§
Source§impl Database
impl Database
pub fn open(path: &str) -> Result<Self>
Sourcepub fn open_existing_readonly(path: &str) -> Result<Option<Self>>
pub fn open_existing_readonly(path: &str) -> Result<Option<Self>>
Open an existing database read-only. Returns Ok(None) when the file
is missing so completion / debug paths never create whycodes.db.
pub fn open_in_memory() -> Result<Self>
Sourcepub fn upsert_session(
&self,
id: &str,
title: &str,
project_path: &str,
created_at: &str,
updated_at: &str,
usage: &Usage,
) -> Result<()>
pub fn upsert_session( &self, id: &str, title: &str, project_path: &str, created_at: &str, updated_at: &str, usage: &Usage, ) -> Result<()>
Insert or update a session row, preserving created_at on conflict.
Sourcepub fn create_session(
&self,
id: &str,
title: &str,
project_path: &str,
) -> Result<()>
pub fn create_session( &self, id: &str, title: &str, project_path: &str, ) -> Result<()>
Convenience for tests / simple creates (zero usage, now timestamps).
pub fn get_session(&self, id: &str) -> Result<Option<SessionRow>>
pub fn list_sessions(&self) -> Result<Vec<SessionRow>>
Sourcepub fn search_sessions_text(
&self,
terms: &[String],
limit: usize,
) -> Result<Vec<SessionTextHit>>
pub fn search_sessions_text( &self, terms: &[String], limit: usize, ) -> Result<Vec<SessionTextHit>>
Sessions with a message containing every term (ASCII case-insensitive), most recently updated first.
A scan rather than an FTS index: every save rewrites a session’s messages, and an index made a 500-message save ~15x slower, while a scan of a 120k-message database takes ~0.3 s at search time only.
Sourcepub fn usage_totals(&self) -> Result<UsageTotals>
pub fn usage_totals(&self) -> Result<UsageTotals>
Sum provider-reported usage and message counts across all sessions.
pub fn update_title(&self, id: &str, title: &str) -> Result<()>
pub fn delete_session(&self, id: &str) -> Result<()>
pub fn insert_message( &self, msg_id: &str, session_id: &str, role: &str, content: &str, tool_call_id: Option<&str>, name: Option<&str>, ) -> Result<()>
Sourcepub fn delete_messages(&self, session_id: &str) -> Result<()>
pub fn delete_messages(&self, session_id: &str) -> Result<()>
Drop all messages for a session (used before full re-persist).
Sourcepub fn replace_messages(
&self,
session_id: &str,
messages: &[MessageInsert],
) -> Result<()>
pub fn replace_messages( &self, session_id: &str, messages: &[MessageInsert], ) -> Result<()>
Replace every message for a session in one transaction.
Full rewrite keeps the table honest across multi-turn persists; the transaction avoids half-written transcripts if a process dies mid-loop.
Sourcepub fn replace_transcript(
&self,
session_id: &str,
messages: &[MessageInsert],
branches: &[BranchInsert],
) -> Result<()>
pub fn replace_transcript( &self, session_id: &str, messages: &[MessageInsert], branches: &[BranchInsert], ) -> Result<()>
Replace the active path and every stored branch of a session in one transaction, so a crash mid-save never leaves a switched-away path unsaved: the old rows stay until the new ones commit.
Sourcepub fn get_branches(&self, session_id: &str) -> Result<Vec<BranchRow>>
pub fn get_branches(&self, session_id: &str) -> Result<Vec<BranchRow>>
Stored branches of a session in save order. A session saved before branches existed has none. Message rows whose branch header is missing still come back, as an unnamed branch, rather than being dropped.
Sourcepub fn message_counts_by_session(&self) -> Result<HashMap<String, usize>>
pub fn message_counts_by_session(&self) -> Result<HashMap<String, usize>>
Message counts for every session in one query (session picker).
pub fn get_messages(&self, session_id: &str) -> Result<Vec<MessageRow>>
pub fn message_count(&self, session_id: &str) -> Result<usize>
pub fn get_state(&self, key: &str) -> Result<Option<String>>
pub fn set_state(&self, key: &str, value: &str) -> Result<()>
pub fn delete_state(&self, key: &str) -> Result<()>
pub fn insert_memory( &self, id: &str, project_key: &str, text: &str, embedding: &[u8], source_session: Option<&str>, ) -> Result<()>
pub fn get_memory(&self, id: &str) -> Result<Option<MemoryRow>>
pub fn list_memories( &self, project_key: &str, limit: usize, ) -> Result<Vec<MemoryRow>>
pub fn delete_memory(&self, id: &str) -> Result<bool>
pub fn clear_memories(&self, project_key: &str) -> Result<usize>
pub fn touch_memory_recall(&self, id: &str) -> Result<()>
pub fn count_memories(&self, project_key: &str) -> Result<usize>
pub fn insert_code_chunk( &self, id: &str, project_key: &str, path: &str, start_line: i64, end_line: i64, text: &str, embedding: &[u8], ) -> Result<()>
pub fn list_code_chunks( &self, project_key: &str, limit: usize, ) -> Result<Vec<CodeChunkRow>>
pub fn clear_code_chunks(&self, project_key: &str) -> Result<usize>
pub fn insert_session_chunk( &self, id: &str, project_key: &str, session_id: &str, turn_index: i64, text: &str, embedding: &[u8], ) -> Result<()>
pub fn list_session_chunks( &self, project_key: &str, limit: usize, ) -> Result<Vec<SessionChunkRow>>
Sourcepub fn search_memories(
&self,
project_key: &str,
query: &str,
limit: usize,
) -> Result<FtsPage<MemoryRow>>
pub fn search_memories( &self, project_key: &str, query: &str, limit: usize, ) -> Result<FtsPage<MemoryRow>>
Up to limit facts that share a word with query, best BM25 first.
Sourcepub fn search_code_chunks(
&self,
project_key: &str,
query: &str,
limit: usize,
) -> Result<FtsPage<CodeChunkRow>>
pub fn search_code_chunks( &self, project_key: &str, query: &str, limit: usize, ) -> Result<FtsPage<CodeChunkRow>>
Up to limit code chunks that share a word with query.
Sourcepub fn search_session_chunks(
&self,
project_key: &str,
query: &str,
limit: usize,
) -> Result<FtsPage<SessionChunkRow>>
pub fn search_session_chunks( &self, project_key: &str, query: &str, limit: usize, ) -> Result<FtsPage<SessionChunkRow>>
Up to limit past session turns that share a word with query.
Auto Trait Implementations§
impl !Freeze for Database
impl !RefUnwindSafe for Database
impl !Sync for Database
impl !UnwindSafe for Database
impl Send for Database
impl Unpin for Database
impl UnsafeUnpin for Database
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more