pub struct ChatState { /* private fields */ }Expand description
The transcript reducer.
This is plain data: construct it in a host or wrap it in an entity. The rules are deliberately small:
- only
MessageState::Streamingmessages acceptappend; finishonly moves a streaming message to complete;stopkeeps the partial body;failkeeps the partial body and records the reason.
Implementations§
Source§impl ChatState
impl ChatState
Sourcepub fn messages(&self) -> &[ChatMessage]
pub fn messages(&self) -> &[ChatMessage]
All messages in display order.
Sourcepub fn get(&self, id: MessageId) -> Option<&ChatMessage>
pub fn get(&self, id: MessageId) -> Option<&ChatMessage>
Look up a message by identity.
Sourcepub fn last(&self) -> Option<&ChatMessage>
pub fn last(&self) -> Option<&ChatMessage>
The most recent message.
Sourcepub fn last_assistant(&self) -> Option<&ChatMessage>
pub fn last_assistant(&self) -> Option<&ChatMessage>
The most recent assistant message.
Sourcepub fn streaming_id(&self) -> Option<MessageId>
pub fn streaming_id(&self) -> Option<MessageId>
The message currently streaming, if any.
Sourcepub fn is_streaming(&self) -> bool
pub fn is_streaming(&self) -> bool
Returns whether any message is still streaming.
Sourcepub fn push(&mut self, message: NewMessage) -> MessageId
pub fn push(&mut self, message: NewMessage) -> MessageId
Append a message and assign it a fresh identity.
Sourcepub fn insert_before(
&mut self,
before: MessageId,
message: NewMessage,
) -> Option<MessageId>
pub fn insert_before( &mut self, before: MessageId, message: NewMessage, ) -> Option<MessageId>
Insert a message immediately before before.
Returns None when before is unknown; the transcript is unchanged in
that case.
Sourcepub fn push_user(&mut self, text: impl Into<String>) -> MessageId
pub fn push_user(&mut self, text: impl Into<String>) -> MessageId
Append a complete user message.
Sourcepub fn push_system(&mut self, text: impl Into<String>) -> MessageId
pub fn push_system(&mut self, text: impl Into<String>) -> MessageId
Append a complete system notice.
Sourcepub fn begin_assistant(&mut self) -> MessageId
pub fn begin_assistant(&mut self) -> MessageId
Append an empty streaming assistant message.
Sourcepub fn append(&mut self, id: MessageId, delta: &str) -> bool
pub fn append(&mut self, id: MessageId, delta: &str) -> bool
Append delta to a streaming message.
Returns false for unknown messages and for messages that are no
longer streaming, so late tokens can be dropped safely.
Sourcepub fn fail(&mut self, id: MessageId, error: impl Into<String>) -> bool
pub fn fail(&mut self, id: MessageId, error: impl Into<String>) -> bool
Mark a message as failed, keeping any partial body.
Sourcepub fn set_meta(&mut self, id: MessageId, meta: MessageMeta) -> bool
pub fn set_meta(&mut self, id: MessageId, meta: MessageMeta) -> bool
Replace a message’s footer metadata.
Sourcepub fn add_source(&mut self, id: MessageId, source: SourceRef) -> bool
pub fn add_source(&mut self, id: MessageId, source: SourceRef) -> bool
Add one source to a message’s footer, deduplicating by source id.
Returns whether the source was added.
Sourcepub fn remove(&mut self, id: MessageId) -> bool
pub fn remove(&mut self, id: MessageId) -> bool
Remove one message, keeping the relative order of the rest.
Returns whether the message existed.
Sourcepub fn replace(
&mut self,
messages: impl IntoIterator<Item = NewMessage>,
) -> Vec<MessageId>
pub fn replace( &mut self, messages: impl IntoIterator<Item = NewMessage>, ) -> Vec<MessageId>
Replace the transcript, assigning fresh identities.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ChatState
impl RefUnwindSafe for ChatState
impl Send for ChatState
impl Sync for ChatState
impl Unpin for ChatState
impl UnsafeUnpin for ChatState
impl UnwindSafe for ChatState
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for 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> 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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().