pub struct Conversation { /* private fields */ }Expand description
Maintains a conversation history and handles context-window compression.
This is the primary building block used by DeepseekAgent.
You can also use it directly for simple back-and-forth conversations that do not need tools.
§Context management
By default the conversation uses LlmSummarizer, which calls DeepSeek to write
a concise summary of older turns once the estimated token count exceeds a threshold.
Swap it out via with_summarizer:
use ds_api::{ApiClient, conversation::Conversation, conversation::SlidingWindowSummarizer};
let conv = Conversation::new(ApiClient::new("sk-..."))
.with_summarizer(SlidingWindowSummarizer::new(20));Implementations§
Source§impl Conversation
impl Conversation
Sourcepub fn new(client: ApiClient) -> Self
pub fn new(client: ApiClient) -> Self
Create a new conversation backed by client.
The default summarizer is LlmSummarizer with sensible defaults
(~60 000 estimated tokens trigger, retain last 10 turns).
Sourcepub fn with_summarizer(self, s: impl Summarizer + 'static) -> Self
pub fn with_summarizer(self, s: impl Summarizer + 'static) -> Self
Replace the summarizer.
Sourcepub fn enable_auto_summary(self, v: bool) -> Self
pub fn enable_auto_summary(self, v: bool) -> Self
Enable or disable automatic summarization (enabled by default).
Sourcepub fn with_history(self, history: Vec<Message>) -> Self
pub fn with_history(self, history: Vec<Message>) -> Self
Seed the conversation with an existing message history.
Sourcepub fn history_mut(&mut self) -> &mut Vec<Message>
pub fn history_mut(&mut self) -> &mut Vec<Message>
Mutable access to the raw history (advanced use).
Sourcepub fn add_message(&mut self, message: Message)
pub fn add_message(&mut self, message: Message)
Append an arbitrary message (any role) to the history.
Sourcepub fn push_user_input(&mut self, text: impl Into<String>)
pub fn push_user_input(&mut self, text: impl Into<String>)
Append a Role::User message to the history.
Sourcepub async fn maybe_summarize(&mut self)
pub async fn maybe_summarize(&mut self)
Run the summarizer if the current history warrants it.
Errors from the summarizer are silently swallowed so that a transient API failure during summarization does not abort an ongoing conversation turn.
Sourcepub async fn send_once(&mut self) -> Result<Option<String>>
pub async fn send_once(&mut self) -> Result<Option<String>>
Send the current history to the API as a single (non-streaming) request and return the assistant’s text content (if any).
The assistant reply is automatically appended to the history. Summarization is run both before the request and after the reply is received.
Sourcepub async fn stream_text(
&mut self,
) -> Result<BoxStream<'_, Result<String, ApiError>>>
pub async fn stream_text( &mut self, ) -> Result<BoxStream<'_, Result<String, ApiError>>>
Stream text fragments (delta.content) from the API as a
BoxStream<Result<String, ApiError>>.
Unlike send_once, this method does not
automatically append the assistant reply or run summarization — the caller
is responsible for collecting the stream and updating history if needed.