Class: ThreadTree
@providerprotocol/agents / thread-tree / ThreadTree
Class: ThreadTree
Section titled “Class: ThreadTree”Defined in: src/thread-tree/index.ts:172
A tree-structured collection of conversation threads with branching support.
ThreadTree provides a git-like branching model for conversations, where each node represents a snapshot of the conversation state. Users can create branches to explore alternative conversation paths, switch between branches, and maintain multiple concurrent conversation histories.
Remarks
Section titled “Remarks”Key concepts:
- Root: The initial node created when the tree is instantiated
- Current: The active node where new state updates are applied
- Branch: Creating a new child node from any existing node
- Checkout: Switching the active node to a different position in the tree
The tree automatically manages node relationships and provides efficient lookup via an internal Map structure.
ThreadNode - The node class used within the tree
Example
Section titled “Example”// Create a new thread treeconst tree = new ThreadTree();
// Work with the current stateconst state = tree.history();
// Create a branch for experimentationconst branchId = tree.branch(tree.current.id, 'experiment');tree.checkout(branchId);
// Switch back to the original branchtree.checkout(tree.root.id);
// Persist and restoreconst json = tree.toJSON();const restored = ThreadTree.fromJSON(json);Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new ThreadTree(
root?):ThreadTree
Defined in: src/thread-tree/index.ts:210
Creates a new ThreadTree instance.
If no root node is provided, creates a fresh tree with a new root node containing an initial (empty) agent state.
Parameters
Section titled “Parameters”Optional existing root node to use. If omitted, a new root with initial state is created automatically.
Returns
Section titled “Returns”ThreadTree
Example
Section titled “Example”// Create a fresh treeconst tree = new ThreadTree();
// Create from an existing root (used by fromJSON)const existingRoot = new ThreadNode(id, null, state, 'root');const tree = new ThreadTree(existingRoot);Properties
Section titled “Properties”
readonlynodes:Map<string,ThreadNode>
Defined in: src/thread-tree/index.ts:189
Map of all nodes in the tree, keyed by node ID. Provides O(1) lookup for branch and checkout operations.
readonlyroot:ThreadNode
Defined in: src/thread-tree/index.ts:177
The root node of the tree, representing the initial conversation state. This node has no parent (parentId is null).
Accessors
Section titled “Accessors”current
Section titled “current”Get Signature
Section titled “Get Signature”get current():
ThreadNode
Defined in: src/thread-tree/index.ts:242
Gets the currently active node in the tree.
The current node is where state updates are applied and serves as the default parent for new branches.
Example
Section titled “Example”const currentState = tree.current.state;const currentId = tree.current.id;Returns
Section titled “Returns”The currently active ThreadNode
Methods
Section titled “Methods”branch()
Section titled “branch()”branch(
fromId,name?):string
Defined in: src/thread-tree/index.ts:274
Creates a new branch from an existing node.
The new node inherits a copy of the parent node’s state and is automatically registered in the tree. The parent’s children array is updated to include the new node.
Parameters
Section titled “Parameters”fromId
Section titled “fromId”string
ID of the node to branch from
string
Optional human-readable name for the new branch
Returns
Section titled “Returns”string
The ID of the newly created node
Throws
Section titled “Throws”Error if the specified parent node does not exist
Remarks
Section titled “Remarks”This method does not automatically checkout the new branch. Call checkout with the returned ID to switch to the new branch.
Example
Section titled “Example”// Create a named branch from current positionconst branchId = tree.branch(tree.current.id, 'try-different-approach');
// Switch to the new branchtree.checkout(branchId);
// Create an unnamed branch from rootconst altBranchId = tree.branch(tree.root.id);checkout()
Section titled “checkout()”checkout(
nodeId):void
Defined in: src/thread-tree/index.ts:315
Switches the active node to a different position in the tree.
After checkout, the tree’s current property will point to the specified node, and subsequent state operations will apply to that node.
Parameters
Section titled “Parameters”nodeId
Section titled “nodeId”string
ID of the node to switch to
Returns
Section titled “Returns”void
Throws
Section titled “Throws”Error if the specified node does not exist
Example
Section titled “Example”// Switch to a specific branchtree.checkout(branchId);
// Switch back to roottree.checkout(tree.root.id);
// Switch to a leaf nodeconst leaves = tree.getLeaves();tree.checkout(leaves[0]);getBranches()
Section titled “getBranches()”getBranches():
Map<string,string|undefined>
Defined in: src/thread-tree/index.ts:395
Gets all named branches in the tree.
Returns a map of node IDs to their branch names for nodes that have been given explicit names. Useful for displaying a list of available branches to users.
Returns
Section titled “Returns”Map<string, string | undefined>
Map of node IDs to branch names (undefined values excluded from result)
Example
Section titled “Example”const branches = tree.getBranches();for (const [id, name] of branches) { console.log(`Branch "${name}": ${id}`);}getLeaves()
Section titled “getLeaves()”getLeaves():
string[]
Defined in: src/thread-tree/index.ts:366
Gets all leaf nodes in the tree.
Leaf nodes are nodes with no children, representing the endpoints of various conversation branches. These are typically the most recent points in each conversation path.
Returns
Section titled “Returns”string[]
Array of node IDs for all leaf nodes
Example
Section titled “Example”const leaves = tree.getLeaves();console.log(`Tree has ${leaves.length} active branches`);
// Visit each leaffor (const leafId of leaves) { tree.checkout(leafId); console.log(tree.current.name);}history()
Section titled “history()”history():
AgentState
Defined in: src/thread-tree/index.ts:340
Gets the agent state for the current node with tree metadata attached.
Since nodes store complete state snapshots, this simply returns the current node’s state with an additional metadata field containing the current node’s ID for reference.
Returns
Section titled “Returns”The current node’s AgentState with _threadTreeNodeId metadata
Example
Section titled “Example”const state = tree.history();const messages = state.messages;const nodeId = state.metadata['_threadTreeNodeId'];toJSON()
Section titled “toJSON()”toJSON():
ThreadTreeJSON
Defined in: src/thread-tree/index.ts:421
Serializes the entire tree to a JSON-compatible object.
The serialized form includes all nodes, the root ID, and the current node ID, allowing complete tree reconstruction via fromJSON().
Returns
Section titled “Returns”A plain object representation suitable for JSON.stringify()
Example
Section titled “Example”const json = tree.toJSON();await Bun.write('conversation-tree.json', JSON.stringify(json, null, 2));fromJSON()
Section titled “fromJSON()”
staticfromJSON(json):ThreadTree
Defined in: src/thread-tree/index.ts:455
Deserializes a ThreadTree from a JSON object.
Reconstructs the complete tree structure including all nodes, parent-child relationships, and restores the current node position.
Parameters
Section titled “Parameters”The serialized tree data
Returns
Section titled “Returns”ThreadTree
A new ThreadTree instance with the restored structure
Throws
Section titled “Throws”Error if the root node ID is not found in the nodes array
Throws
Section titled “Throws”Error if the current node ID is not found in the nodes array
Example
Section titled “Example”const data = await Bun.file('conversation-tree.json').json();const tree = ThreadTree.fromJSON(data as ThreadTreeJSON);
// Tree is fully restored with same current positionconsole.log(tree.current.id);