pub struct Thread {
pub id: String,
pub resource_id: Option<String>,
pub parent_thread_id: Option<String>,
pub messages: Vec<Arc<Message>>,
pub state: Value,
pub patches: Vec<TrackedPatch>,
pub metadata: ThreadMetadata,
}Expand description
Persisted thread state with messages and state history.
Thread uses an owned builder pattern: with_* methods consume self
and return a new Thread (e.g., thread.with_message(msg)).
Fields§
§id: StringUnique thread identifier.
resource_id: Option<String>Owner/resource identifier (e.g., user_id, org_id).
parent_thread_id: Option<String>Parent thread identifier (links child → parent for sub-agent lineage).
messages: Vec<Arc<Message>>Messages (Arc-wrapped for efficient cloning).
state: ValueInitial/snapshot state.
patches: Vec<TrackedPatch>Patches applied since the last snapshot.
metadata: ThreadMetadataMetadata.
Implementations§
Source§impl Thread
impl Thread
Sourcepub fn with_initial_state(id: impl Into<String>, state: Value) -> Self
pub fn with_initial_state(id: impl Into<String>, state: Value) -> Self
Create a new thread with initial state.
Sourcepub fn with_resource_id(self, resource_id: impl Into<String>) -> Self
pub fn with_resource_id(self, resource_id: impl Into<String>) -> Self
Set the resource_id (pure function, returns new Thread).
Sourcepub fn with_parent_thread_id(self, parent_thread_id: impl Into<String>) -> Self
pub fn with_parent_thread_id(self, parent_thread_id: impl Into<String>) -> Self
Set the parent_thread_id (pure function, returns new Thread).
Sourcepub fn with_message(self, msg: Message) -> Self
pub fn with_message(self, msg: Message) -> Self
Add a message to the thread (pure function, returns new Thread).
Messages are Arc-wrapped for efficient cloning during agent loops.
Sourcepub fn with_messages(self, msgs: impl IntoIterator<Item = Message>) -> Self
pub fn with_messages(self, msgs: impl IntoIterator<Item = Message>) -> Self
Add multiple messages (pure function, returns new Thread).
Sourcepub fn with_patch(self, patch: TrackedPatch) -> Self
pub fn with_patch(self, patch: TrackedPatch) -> Self
Add a patch to the thread (pure function, returns new Thread).
Sourcepub fn with_patches(
self,
patches: impl IntoIterator<Item = TrackedPatch>,
) -> Self
pub fn with_patches( self, patches: impl IntoIterator<Item = TrackedPatch>, ) -> Self
Add multiple patches (pure function, returns new Thread).
Sourcepub fn rebuild_state(&self) -> TireaResult<Value>
pub fn rebuild_state(&self) -> TireaResult<Value>
Rebuild the current state (base + thread patches).
Sourcepub fn replay_to(&self, patch_index: usize) -> TireaResult<Value>
pub fn replay_to(&self, patch_index: usize) -> TireaResult<Value>
Replay state to a specific patch index (0-based).
patch_index = 0: Returns state after applying the first patch onlypatch_index = n: Returns state after applying patches 0..=npatch_index >= patch_count: Returns error
This enables time-travel debugging by accessing any historical state point.
Sourcepub fn snapshot(self) -> TireaResult<Self>
pub fn snapshot(self) -> TireaResult<Self>
Create a snapshot, collapsing patches into the base state.
Returns a new Thread with the current state as base and empty patches.
Sourcepub fn needs_snapshot(&self, threshold: usize) -> bool
pub fn needs_snapshot(&self, threshold: usize) -> bool
Check if a snapshot is needed (e.g., too many patches).
Sourcepub fn message_count(&self) -> usize
pub fn message_count(&self) -> usize
Get the number of messages.
Sourcepub fn patch_count(&self) -> usize
pub fn patch_count(&self) -> usize
Get the number of patches.