pub struct Session { /* private fields */ }Expand description
An open session and the history it is backed by.
Debug is hand-written so the tests can use expect_err; deriving it would
try to print the open file handle.
Implementations§
Source§impl Session
impl Session
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Whether the file holds nothing yet.
Not dead weight: clippy::len_without_is_empty requires it beside len,
and a reader coming to len expects to be able to ask the empty case.
Sourcepub fn open(path: &Path) -> Result<Self>
pub fn open(path: &Path) -> Result<Self>
Open or create the session file and replay it into a history.
§Errors
yk_core::Error::Read if the file cannot be opened, and
yk_core::Error::Malformed if a line cannot be read. A corrupt line is
reported rather than skipped: silently dropping a message makes the model
look like it forgot something, which is a much harder bug to find.
pub const fn history_mut(&mut self) -> &mut Vec<Message>
pub fn append_user(&mut self, prompt: &str)
Sourcepub fn model_or<'a>(&'a self, default: &'a str) -> &'a str
pub fn model_or<'a>(&'a self, default: &'a str) -> &'a str
The model in force, or default when the session records none.
The fallback for a new conversation and for one that never switched: the caller’s configured model is the answer in both cases.
Sourcepub fn set_model(&mut self, model: &str) -> Result<()>
pub fn set_model(&mut self, model: &str) -> Result<()>
Record a model switch, so a resumed conversation picks it back up.
Appended rather than held only in memory: a client closes the process and reopens it, and a model that lived only in memory would be a picker that lies, reporting a switched model and then resuming on the configured one.
§Errors
yk_core::Error::Read if the entry cannot be written.
Sourcepub fn compact(&mut self, summary: &str) -> Result<()>
pub fn compact(&mut self, summary: &str) -> Result<()>
Replace the active branch with a summary, shadowing what came before.
The summary is written as a Compaction entry, so the branch walk stops
there and everything before it leaves the conversation without leaving the
file. The in-memory history becomes the summary alone, so a run that
continues from here sends the summary and not the shadowed turns.
The write cursor moves with the history, because the summary is already on
disk as the compaction entry and a later persist must not write it
again.
§Errors
yk_core::Error::Read if the entry cannot be written.