Skip to main content

HistoryManager

Struct HistoryManager 

Source
pub struct HistoryManager<C: ConversationStore, S: StateStore> {
    pub thread_id: ThreadId,
    pub root_thread_id: ThreadId,
    pub history: RefCell<Vec<InfinityMessage>>,
    /* private fields */
}

Fields§

§thread_id: ThreadId§root_thread_id: ThreadId§history: RefCell<Vec<InfinityMessage>>

Implementations§

Source§

impl<C: ConversationStore, S: StateStore> HistoryManager<C, S>

Source

pub async fn new_with_history( conversation_store: C, state_store: S, thread_id: ThreadId, ) -> Result<Self, Box<dyn Error + Send + Sync>>

Source

pub fn handle_content( &self, message: InfinityMessage, message_id: String, ) -> Result<bool, Box<dyn Error + Send + Sync>>

Source

pub fn handle_completion( &self, completion: &StreamChunk, completion_id: String, display_as: Option<String>, )

Buffer a streamed assistant chunk for the current turn. The chunk is not committed to history yet — it accumulates in turn_buffer until the turn reaches a flush point (Self::flush_turn) or is discarded on failure (Self::discard_turn). This keeps partial, possibly-abandoned turns out of committed history.

Source

pub fn flush_turn(&self)

Commit the buffered current-turn content into history and pending_items. Called at a flush point: a completed turn (Final) or a turn-ending tool call. After this, the buffered messages are part of committed history and will be persisted by the next Self::sync.

Callers must have promoted any unvalidated inputs first (see Self::mark_inputs_model_validated): the buffered content is model output, and model output for a request that included unvalidated inputs is exactly the proof required to validate them.

Source

pub fn flush_turn_trimming_reasoning(&self)

Like Self::flush_turn, but first drops any trailing assistant reasoning (and empty text) from the buffer so the committed turn does not end on a reasoning block.

Source

pub fn discard_turn(&self)

Drop the buffered current-turn content without committing it. Called when a turn fails mid-stream (timeout, disconnect) and will be retried: the retry rebuilds the request from clean committed history, so the abandoned partial turn must not linger.

Source

pub fn turn_buffer_is_empty(&self) -> bool

Source

pub fn current_turn_view(&self) -> Vec<InfinityMessage>

Committed history followed by the in-flight buffered turn. Used for mid-turn subscriber replay so a client connecting while the model is streaming still sees the partial assistant message.

Source

pub fn mark_inputs_model_validated(&self)

Promote all unvalidated inputs to the known-safe persistence queue.

Called as soon as the model streams any output for a request that included them: output means the request was accepted, i.e. the context window did not overflow, so the inputs are safe to persist.

Source

pub fn unvalidated_len(&self) -> usize

Number of inputs still awaiting model validation.

Source

pub fn drop_unvalidated_user_inputs(&self) -> usize

Drop all unvalidated user inputs: items that are neither tool results nor subscription events (those are placeholder-replaceable, see Self::replace_unvalidated_tool_results, and answering a tool call is always better than stranding it). Dropped items are removed from the in-memory history and their dedup IDs forgotten so a redelivery is not silently ignored. Returns how many items were dropped.

Used when the model reports a context overflow: an oversized user input has no safe substitute, and it must not be persisted (or kept in memory) or the thread would be permanently wedged on it.

Source

pub fn replace_unvalidated_tool_results(&self, placeholder: &str) -> bool

Replace the content of every unvalidated tool result — including the bodies of subscription events, which carry a tool result — with placeholder (in both the persistence queue and the in-memory history). Returns true if at least one was replaced.

Used on context overflow: unlike user text, a tool result cannot simply be dropped without stranding its tool call (and a subscription event should still record that an event arrived), but both can be answered with a fixed placeholder. Bodies already equal to the placeholder are not counted, so a second overflow reports false and the caller falls back to dropping the inputs.

Source

pub async fn sync(&self) -> Result<(), Box<dyn Error + Send + Sync>>

Source

pub async fn update_metadata( &self, metadata: Value, ) -> Result<(), Box<dyn Error + Send + Sync>>

Source

pub fn get_metadata(&self) -> Option<Value>

Source

pub fn get_history(&self, supports_image_input: bool) -> Vec<Message>

Build the model-facing chat history. When supports_image_input is false, image tool-result content is replaced with a text placeholder so the history can be sent to models without image support.

Source

pub fn get_thread_stack(&self) -> Vec<ThreadId>

Returns the full thread stack: [root, ..ancestors, current_thread]. For the root thread this is just the root thread ID.

Source

pub fn conversation_store(&self) -> &C

Source

pub async fn apply_compaction( &self, ) -> Result<bool, Box<dyn Error + Send + Sync>>

Apply the latest compaction summary: reload from store, truncate in-memory history up to the compaction point, and prepend the summary.

Source

pub fn take_interrupted_tool_calls(&self) -> Vec<ToolCallId>

Drain and return tool call IDs that were interrupted by new user messages. Callers use this to send best-effort cancellation notifications to RAP tool servers so they can abort in-flight operations.

Source

pub async fn find_tool_call(&self, tool_call_id: &str) -> Option<ToolCall>

Find a tool call by ID, unwinding past this thread’s compaction boundary.

Compaction folds old messages into a summary in the loaded view only — the store still holds every row. A synthetic event that references a summarized-away call (e.g. a child’s close report whose spawn_thread call was compacted while the parent slept) must still resolve it, so on an in-memory miss the thread’s own persisted rows are searched as well.

Own rows suffice because every synthetic event resolves against a call the receiving thread owns: reports name the parent’s own spawn_thread call, subscription events the subscribing thread’s own call, and parent messages the copy of the spawn call seeded into the child’s store at spawn time. (Threads spawned before spawn-call seeding saw the call only through their inherited slice; for those, an event referencing a compacted-away inherited call is still dropped.)

Source

pub fn safe_spawn_point(&self) -> usize

Compute a safe spawn point that excludes trailing unanswered tool calls and any unvalidated (not yet persistable) inputs. Returns an absolute store order (accounting for prior compaction offset and ancestor prefix) suitable for use as spawn_order_override.

Source

pub async fn track_subscription( &self, tool_call_id: &ToolCallId<str>, ) -> Result<(), Box<dyn Error + Send + Sync>>

Record a subscription in the current thread’s metadata. The tool_call_id is the ID of the tool call whose result had subscription: true. Ownership is implicit — a subscription is stored in the thread that created it.

Source

pub async fn remove_subscription( &self, tool_call_id: &ToolCallId<str>, ) -> Result<(), Box<dyn Error + Send + Sync>>

Remove a subscription from the current thread’s active tracking.

Auto Trait Implementations§

§

impl<C, S> !Freeze for HistoryManager<C, S>

§

impl<C, S> !RefUnwindSafe for HistoryManager<C, S>

§

impl<C, S> !Sync for HistoryManager<C, S>

§

impl<C, S> Send for HistoryManager<C, S>

§

impl<C, S> Unpin for HistoryManager<C, S>
where C: Unpin, S: Unpin,

§

impl<C, S> UnsafeUnpin for HistoryManager<C, S>
where C: UnsafeUnpin, S: UnsafeUnpin,

§

impl<C, S> UnwindSafe for HistoryManager<C, S>
where C: UnwindSafe, S: UnwindSafe,

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: Sized + 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: Sized + 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 = !

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

fn try_from(value: U) -> Result<T, !>

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