pub struct ConversationManager { /* private fields */ }Expand description
Shared, async conversation history manager.
Thread-safe: clone the Arc to share across tasks. All operations are
O(1) amortised apart from build_prompt which
is O(turns).
Implementations§
Source§impl ConversationManager
impl ConversationManager
Sourcepub fn new(config: ConversationConfig) -> Self
pub fn new(config: ConversationConfig) -> Self
Create a new manager with the given configuration.
Sourcepub async fn push_user(&self, session: &SessionId, content: impl Into<String>)
pub async fn push_user(&self, session: &SessionId, content: impl Into<String>)
Append a user turn to the conversation for session.
Sourcepub async fn push_assistant(
&self,
session: &SessionId,
content: impl Into<String>,
)
pub async fn push_assistant( &self, session: &SessionId, content: impl Into<String>, )
Append an assistant turn to the conversation for session.
Sourcepub async fn push_tool(&self, session: &SessionId, content: impl Into<String>)
pub async fn push_tool(&self, session: &SessionId, content: impl Into<String>)
Append a tool-result turn.
Sourcepub async fn push_system(&self, session: &SessionId, content: impl Into<String>)
pub async fn push_system(&self, session: &SessionId, content: impl Into<String>)
Append a system turn (overrides the global system prompt for this turn).
Sourcepub async fn build_prompt(&self, session: &SessionId, new_input: &str) -> String
pub async fn build_prompt(&self, session: &SessionId, new_input: &str) -> String
Build a full prompt string that includes prior conversation history
followed by new_input as the next user message.
The returned string is ready to pass directly to a crate::ModelWorker.
Sourcepub async fn get(&self, session: &SessionId) -> Option<Conversation>
pub async fn get(&self, session: &SessionId) -> Option<Conversation>
Return a snapshot of the conversation for session, or None if no
history exists.
Sourcepub async fn turn_count(&self, session: &SessionId) -> usize
pub async fn turn_count(&self, session: &SessionId) -> usize
Return the number of turns currently stored for session.
Sourcepub async fn token_count(&self, session: &SessionId) -> usize
pub async fn token_count(&self, session: &SessionId) -> usize
Return the estimated token count for session.
Sourcepub async fn set_meta(
&self,
session: &SessionId,
key: impl Into<String>,
value: impl Into<String>,
)
pub async fn set_meta( &self, session: &SessionId, key: impl Into<String>, value: impl Into<String>, )
Attach arbitrary metadata to a session’s conversation.
Sourcepub async fn evict_stale(&self)
pub async fn evict_stale(&self)
Evict all conversations that have been inactive longer than the configured TTL. Call this periodically (e.g. every hour) to reclaim memory.
Sourcepub async fn export_json(&self, session: &SessionId) -> Option<String>
pub async fn export_json(&self, session: &SessionId) -> Option<String>
Export a conversation as a JSON string for persistence or debugging.
Sourcepub async fn active_sessions(&self) -> Vec<String>
pub async fn active_sessions(&self) -> Vec<String>
Return all active session IDs.
Trait Implementations§
Source§impl Clone for ConversationManager
impl Clone for ConversationManager
Source§fn clone(&self) -> ConversationManager
fn clone(&self) -> ConversationManager
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for ConversationManager
impl !UnwindSafe for ConversationManager
impl Freeze for ConversationManager
impl Send for ConversationManager
impl Sync for ConversationManager
impl Unpin for ConversationManager
impl UnsafeUnpin for ConversationManager
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request