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>>.
§⚠ Caller responsibilities
Unlike send_once, this method is intentionally
minimal: it does not append the assistant reply to history, does not
run summarization, and does not set stream: true on the request for
you (the underlying ApiClient::stream_text handles that).
If you want the conversation to remember this turn you must collect the full text and push it yourself:
use futures::StreamExt;
use ds_api::{ApiClient, conversation::Conversation};
use ds_api::raw::request::message::{Message, Role};
let mut conv = Conversation::new(ApiClient::new("sk-..."));
conv.push_user_input("Tell me a joke.");
let mut text = String::new();
let mut stream = conv.stream_text().await?;
while let Some(fragment) = stream.next().await {
text.push_str(&fragment?);
}
drop(stream); // release the borrow on `conv`
// Manually record the assistant turn so the next call sees it.
conv.add_message(Message::new(Role::Assistant, &text));Skipping this step means the assistant’s reply is silently absent from
history on the next turn. send_once does
all of this automatically and should be preferred unless you specifically
need incremental token delivery.