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>
impl<C: ConversationStore, S: StateStore> HistoryManager<C, S>
pub async fn new_with_history( conversation_store: C, state_store: S, thread_id: ThreadId, ) -> Result<Self, Box<dyn Error + Send + Sync>>
pub fn handle_content( &self, message: InfinityMessage, message_id: String, ) -> Result<bool, Box<dyn Error + Send + Sync>>
Sourcepub fn handle_completion(
&self,
completion: &StreamChunk,
completion_id: String,
display_as: Option<String>,
)
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.
Sourcepub fn flush_turn(&self)
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.
Sourcepub fn flush_turn_trimming_reasoning(&self)
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.
Sourcepub fn discard_turn(&self)
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.
pub fn turn_buffer_is_empty(&self) -> bool
Sourcepub fn current_turn_view(&self) -> Vec<InfinityMessage>
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.
Sourcepub fn mark_inputs_model_validated(&self)
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.
Sourcepub fn unvalidated_len(&self) -> usize
pub fn unvalidated_len(&self) -> usize
Number of inputs still awaiting model validation.
Sourcepub fn drop_unvalidated_user_inputs(&self) -> usize
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.
Sourcepub fn replace_unvalidated_tool_results(&self, placeholder: &str) -> bool
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.
pub async fn sync(&self) -> Result<(), Box<dyn Error + Send + Sync>>
pub async fn update_metadata( &self, metadata: Value, ) -> Result<(), Box<dyn Error + Send + Sync>>
pub fn get_metadata(&self) -> Option<Value>
Sourcepub fn get_history(&self, supports_image_input: bool) -> Vec<Message>
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.
Sourcepub fn get_thread_stack(&self) -> Vec<ThreadId>
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.
pub fn conversation_store(&self) -> &C
Sourcepub async fn apply_compaction(
&self,
) -> Result<bool, Box<dyn Error + Send + Sync>>
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.
Sourcepub fn take_interrupted_tool_calls(&self) -> Vec<ToolCallId>
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.
Sourcepub async fn find_tool_call(&self, tool_call_id: &str) -> Option<ToolCall>
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.)
Sourcepub fn safe_spawn_point(&self) -> usize
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.
Sourcepub async fn track_subscription(
&self,
tool_call_id: &ToolCallId<str>,
) -> Result<(), Box<dyn Error + Send + Sync>>
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.
Sourcepub async fn remove_subscription(
&self,
tool_call_id: &ToolCallId<str>,
) -> Result<(), Box<dyn Error + Send + Sync>>
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.