Skip to main content

SessionLog

Struct SessionLog 

Source
pub struct SessionLog { /* private fields */ }
Expand description

A session file opened for appending.

Implementations§

Source§

impl SessionLog

Source

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.

Source

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.

Source

pub fn active_messages(&self) -> Result<Vec<Message>>

The conversation as messages, oldest first.

§Errors

As SessionLog::active_entries.

Source

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.

Source

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

As SessionLog::active_entries.

Source

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.

Source

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.

Source

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

pub fn compact(&mut self, summary: &str, parent: Option<&Id>) -> Result<Id>

Append a compaction, shadowing everything before it.

§Errors

yk_core::Error::Read when the entry cannot be written.

Source§

impl SessionLog

Source

pub fn path(&self) -> &Path

Where the session file lives, for diagnostics.

Source

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.

Source

pub fn append(&mut self, entry: &Entry) -> Result<()>

Append one entry and flush it.

Flushing per entry is deliberate. A session log that loses its last turn to a crash is a log of lies, and the cost is negligible next to a model round trip.

§Errors

Fails if the entry cannot be written.

Source

pub fn header(&self) -> Result<Option<Header>>

The header, if the file has one.

None for a file written before the header existed. There is one version, so reading such a file as the current one is the only answer available and the right one.

§Errors

As SessionLog::all_entries.

Auto Trait Implementations§

§

impl Forget for SessionLog

§

impl Freeze for SessionLog
where *const u8 is !null: Freeze, PhantomData<u8>: Freeze, I32NotAllOnes: Freeze,

§

impl Move for SessionLog

§

impl RefUnwindSafe for SessionLog
where I32NotAllOnes: RefUnwindSafe,

§

impl Send for SessionLog
where RawVecInner: Send, I32NotAllOnes: Send,

§

impl Sync for SessionLog
where RawVecInner: Sync, I32NotAllOnes: Sync,

§

impl Unpin for SessionLog
where *const u8 is !null: Unpin, I32NotAllOnes: Unpin,

§

impl UnsafeUnpin for SessionLog
where *const u8 is !null: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin, I32NotAllOnes: UnsafeUnpin,

§

impl UnwindSafe for SessionLog
where RawVecInner: UnwindSafe, I32NotAllOnes: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.