pub struct SessionLog { /* private fields */ }Expand description
A session file opened for appending.
Implementations§
Source§impl SessionLog
impl SessionLog
Sourcepub fn head_id(&self) -> Result<Option<Id>>
pub fn head_id(&self) -> Result<Option<Id>>
The entry the active branch ends at.
That is the last line in the file, except when it is a rewind, in which
case it is the entry that rewind names. None is a file with nothing in
it, and only that: a file that cannot be read is an error, not an empty
session.
A rewind whose target names nothing is returned as written, because this
reads the last line rather than walking the branch. SessionLog::active_entries is
the method that reports a target that names nothing.
§Errors
yk_core::Error::Read naming the file when it cannot be read, and
yk_core::Error::Malformed when a line cannot be parsed. Reporting the
read failure rather than turning it into None is the point: a caller
that got None back could not tell a file it could not read from one
nobody had written to.
Sourcepub fn active_entries(&self) -> Result<Vec<Entry>>
pub fn active_entries(&self) -> Result<Vec<Entry>>
The entries that make up the conversation, oldest first.
Returns borrowed slices of the file read, which is why it is on &self
and why the ids inside are borrowed too. A caller that needs to mutate
the history wants SessionLog::active_messages instead.
§Errors
yk_core::Error::Read naming the file when it cannot be read, and
yk_core::Error::Malformed when a line cannot be parsed or a parent id
names no entry. A dangling parent is a corrupted file, and silently
stopping the walk there would truncate a conversation in a way that looks
like nothing happened.
Sourcepub fn active_messages(&self) -> Result<Vec<Message>>
pub fn active_messages(&self) -> Result<Vec<Message>>
Sourcepub fn all_entries(&self) -> Result<Vec<Entry>>
pub fn all_entries(&self) -> Result<Vec<Entry>>
Every entry in the file, in the order written.
The header is not an entry and is not returned. A corrupt line is reported rather than skipped, because a session that silently loses turns is worse than one that refuses to load.
§Errors
Fails if the file cannot be read, a line is not valid JSON, or the header names a version this build does not understand.
Sourcepub fn active_model(&self) -> Result<Option<String>>
pub fn active_model(&self) -> Result<Option<String>>
The model this conversation ended up on.
The last Entry::Model on the active branch, which is the one in force
at the head. None for a session that never switched, meaning the
caller’s configured default is still the answer.
§Errors
Sourcepub fn fork(&self, destination: &Path) -> Result<()>
pub fn fork(&self, destination: &Path) -> Result<()>
Copy the whole file to destination.
This is a fork. Both files share a past and diverge from it, and because nothing is ever rewritten neither can damage the other.
§Errors
yk_core::Error::Read when the destination cannot be written, and
yk_core::Error::Malformed when the source cannot be read.
Sourcepub fn rewind(&mut self, to: &Id) -> Result<()>
pub fn rewind(&mut self, to: &Id) -> Result<()>
Append a rewind, so the branch walk stops at to.
§Errors
yk_core::Error::Malformed when to names no entry in the file.
Sourcepub fn rewind_by(&mut self, messages: usize) -> Result<Id>
pub fn rewind_by(&mut self, messages: usize) -> Result<Id>
Move the head back by messages entries on the active branch.
Counting entries rather than messages is deliberate: a turn is one assistant entry plus one entry per tool result, so “go back three” means something different from “go back three turns”. Counting entries is the literal thing a user watching the transcript expects.
§Errors
yk_core::Error::Malformed when the branch is shorter than messages,
which would mean rewinding past the start.
Source§impl SessionLog
impl SessionLog
Sourcepub fn open(path: impl Into<PathBuf>) -> Result<Self>
pub fn open(path: impl Into<PathBuf>) -> Result<Self>
Open, creating the file and its parent directories if needed.
§Errors
Fails if the directories cannot be created or the file cannot be opened.