Skip to main content

Session

Struct Session 

Source
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

Source

pub fn path(&self) -> &Path

Where the session file is, for diagnostics.

Source

pub const fn len(&self) -> usize

How many entries the file holds.

Source

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.

Source

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.

Source

pub fn history(&self) -> &[Message]

The conversation as it currently stands.

Source

pub const fn history_mut(&mut self) -> &mut Vec<Message>

Source

pub fn append_user(&mut self, prompt: &str)

Source

pub fn model(&self) -> Option<&str>

The model this conversation is on, if the file says.

Source

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.

Source

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.

Source

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.

Source

pub fn persist(&mut self) -> Result<()>

Append whatever the loop produced since the last call.

§Errors

yk_core::Error::Read if the file cannot be written. The cursor advances only for entries that were actually written, so a retry after a failure resumes rather than skipping.

Trait Implementations§

Source§

impl Debug for Session

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for Session

§

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

§

impl Move for Session

§

impl RefUnwindSafe for Session
where I32NotAllOnes: RefUnwindSafe,

§

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

§

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

§

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

§

impl UnsafeUnpin for Session

§

impl UnwindSafe for Session
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.