Skip to main content

Conversation

Struct Conversation 

Source
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

Source

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).

Source

pub fn with_summarizer(self, s: impl Summarizer + 'static) -> Self

Replace the summarizer.

Source

pub fn enable_auto_summary(self, v: bool) -> Self

Enable or disable automatic summarization (enabled by default).

Source

pub fn with_history(self, history: Vec<Message>) -> Self

Seed the conversation with an existing message history.

Source

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

Read-only view of the current history.

Source

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

Mutable access to the raw history (advanced use).

Source

pub fn add_message(&mut self, message: Message)

Append an arbitrary message (any role) to the history.

Source

pub fn push_user_input(&mut self, text: impl Into<String>)

Append a Role::User message to the history.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more