Tree-structured conversation state manager for branching chats.
convo-tree models a conversation as a rooted tree where each node holds a message (system, user, assistant, or tool), children represent alternative continuations from the same point, and any root-to-leaf path is one complete linear conversation. The core metaphor is git: fork() is git branch, switchTo() is git checkout, getActivePath() is git log --first-parent, and prune() is git branch -D.
The package is a pure data structure with zero runtime dependencies and no network I/O. It manages the tree; the caller manages LLM interactions. Extract the active path with getActivePath(), send it to any LLM provider, and add the response back with addMessage().
npm install convo-treeRequires Node.js 18 or later.
import{createConversationTree}from'convo-tree';// Create a tree with an automatic system prompt root nodeconsttree=createConversationTree({systemPrompt: 'You are a helpful assistant.',});// Build a conversation by appending messagestree.addMessage('user','Hello!');tree.addMessage('assistant','Hi there! How can I help?');tree.addMessage('user','Tell me a joke.');tree.addMessage('assistant','Why did the chicken cross the road?');// Extract the active path as a flat message array for any LLM APIconstmessages=tree.getActivePath();// [// { role: 'system', content: 'You are a helpful assistant.' },// { role: 'user', content: 'Hello!' },// { role: 'assistant', content: 'Hi there! How can I help?' },// { role: 'user', content: 'Tell me a joke.' },// { role: 'assistant', content: 'Why did the chicken cross the road?' }// ]- Branching conversations -- Fork at any point to explore alternative continuations. Multiple branches coexist in a single tree structure.
- HEAD tracking -- A HEAD pointer tracks the current position. New messages append as children of HEAD, and HEAD advances automatically.
- Active path extraction --
getActivePath()returns a flatMessage[]from root to HEAD, ready to send to any LLM API. - Undo/redo -- Navigate backward and forward along the active path without losing history. Adding a new message after undo implicitly creates a new branch.
- Subtree pruning -- Remove a node and all its descendants in one operation. HEAD relocates automatically if it falls within the pruned subtree.
- Branch labels -- Assign human-readable labels to branches for organization (e.g., "creative approach", "model: GPT-4o").
- Node metadata -- Attach arbitrary key-value data to any node (model name, temperature, latency, token count).
- Event system -- Subscribe to
message,fork,switch, andpruneevents for reactive UI updates and logging. - Serialization -- Export the full tree state as a JSON-serializable object for persistence and restoration.
- Zero dependencies -- Pure data structure using only built-in Node.js APIs (
crypto.randomUUID,Date.now). - Full TypeScript support -- Written in TypeScript with exported type declarations.
Factory function that creates and returns a ConversationTree instance.
import{createConversationTree}from'convo-tree';consttree=createConversationTree({systemPrompt: 'You are a helpful assistant.',now: ()=>Date.now(),generateId: ()=>crypto.randomUUID(),});| Option | Type | Default | Description |
|---|---|---|---|
systemPrompt | string | undefined | If provided, a system-role node is created automatically as the root. |
treeMeta | Record<string, unknown> | undefined | Arbitrary metadata to associate with the tree itself. |
now | () => number | Date.now | Custom timestamp function used for createdAt on every new node. |
generateId | () => string | crypto.randomUUID | Custom ID generator for node IDs. |
Appends a new message node as a child of the current HEAD and advances HEAD to the new node. Clears the redo stack.
Parameters:
| Parameter | Type | Description |
|---|---|---|
role | 'system' | 'user' | 'assistant' | 'tool' | The message role. |
content | string | The message content. |
metadata | Record<string, unknown> | Optional metadata to attach to the node. Defaults to {}. |
Returns:ConversationNode -- the newly created node.
constnode=tree.addMessage('user','Hello!',{tokens: 3});// node.id -> unique UUID// node.role -> 'user'// node.content -> 'Hello!'// node.parentId -> ID of the previous HEAD node (or null if first node)// node.children -> []// node.metadata -> { tokens: 3 }// node.createdAt -> timestamp from now()When called on a node that already has children, the new message becomes a sibling, creating an implicit fork without requiring an explicit fork() call.
Marks a fork point in the tree. Does not create a new node. If nodeId is provided, that node becomes the fork point; otherwise the current HEAD is used. Optionally assigns a branch label to the fork point node.
Parameters:
| Parameter | Type | Description |
|---|---|---|
nodeId | string | Optional. The node ID to fork from. Defaults to the current HEAD. |
label | string | Optional. A human-readable label to assign to the fork point node. |
Returns:Branch -- an object with forkPointId and optional label.
Throws:InvalidOperationError if the tree is empty. NodeNotFoundError if nodeId does not exist.
constbranch=tree.fork(someNode.id,'alternate-response');// branch.forkPointId -> someNode.id// branch.label -> 'alternate-response'After calling fork(), use switchTo() to move HEAD to the fork point, then call addMessage() to diverge from the original path.
Moves HEAD to any existing node in the tree, changing the active path to the root-to-node path.
Parameters:
| Parameter | Type | Description |
|---|---|---|
nodeId | string | The ID of the node to switch to. |
Returns:void
Throws:NodeNotFoundError if the node does not exist.
tree.switchTo(earlierNode.id);// HEAD is now at earlierNode// getActivePath() returns root -> ... -> earlierNodeReturns the linear message array from root to the current HEAD. The returned array is suitable for direct use with any LLM chat completion API.
Returns:Message[] -- an array of { role, content, ...metadata } objects. Returns an empty array if the tree is empty.
constmessages=tree.getActivePath();// messages[0].role -> 'system' (if systemPrompt was set)// messages[0].content -> 'You are a helpful assistant.'Metadata fields are spread into the message object. For example, if a node has metadata: { tokens: 5 }, the corresponding message will include tokens: 5 alongside role and content.
Returns the linear message array from root to the specified node, without changing HEAD.
Parameters:
| Parameter | Type | Description |
|---|---|---|
nodeId | string | The ID of the target node. |
Returns:Message[]
Throws:NodeNotFoundError if the node does not exist.
constpathA=tree.getPathTo(responseA.id);constpathB=tree.getPathTo(responseB.id);// Compare two branch paths without switching HEADMoves HEAD to its parent node, pushing the current HEAD onto the redo stack. Returns the new HEAD node, or null if HEAD is already at the root or the tree is empty.
Returns:ConversationNode | null
tree.addMessage('user','First');tree.addMessage('assistant','Second');constprevious=tree.undo();// previous.content -> 'First'// tree.getHead().content -> 'First'Restores the most recently undone node by popping the redo stack and advancing HEAD. Returns the restored node, or null if the redo stack is empty or invalid.
The redo stack is validated: the node to redo must be a child of the current HEAD. If the tree structure has changed (e.g., via addMessage() or prune()), the redo stack is cleared.
Returns:ConversationNode | null
tree.undo();constrestored=tree.redo();// HEAD is back at the node that was undoneAdding a new message after undo() clears the redo stack, creating an implicit new branch from the undo point.
Returns the current HEAD node, or null if the tree is empty.
Returns:ConversationNode | null
consthead=tree.getHead();if(head){console.log(head.role,head.content);}Retrieves any node in the tree by its ID.
Parameters:
| Parameter | Type | Description |
|---|---|---|
nodeId | string | The ID of the node to retrieve. |
Returns:ConversationNode | undefined
constnode=tree.getNode('some-uuid');if(node){console.log(node.children.length,'children');}Removes the specified node and all of its descendants from the tree. Updates the parent's children array. If HEAD falls within the pruned subtree, HEAD is moved to the pruned node's parent. If the root is pruned, the tree is fully cleared.
Parameters:
| Parameter | Type | Description |
|---|---|---|
nodeId | string | The ID of the node to prune. |
Returns:number -- the count of nodes removed (including the target node and all descendants).
Throws:NodeNotFoundError if the node does not exist.
constn1=tree.addMessage('user','Root');constn2=tree.addMessage('assistant','Child');tree.addMessage('user','Grandchild');constremoved=tree.prune(n2.id);// removed -> 2 (Child + Grandchild)// HEAD automatically moves to n1Entries in the redo stack that reference pruned nodes are also removed.
Sets or updates the branch label on a node.
Parameters:
| Parameter | Type | Description |
|---|---|---|
nodeId | string | The ID of the node to label. |
label | string | The label to assign. |
Returns:void
Throws:NodeNotFoundError if the node does not exist.
tree.setLabel(node.id,'creative-approach');// tree.getNode(node.id).branchLabel -> 'creative-approach'Resets the tree to an empty state. All nodes, the root, HEAD, and the redo stack are cleared.
Returns:void
tree.clear();// tree.nodeCount -> 0// tree.getHead() -> null// tree.getActivePath() -> []Exports the full tree state as a plain JSON-serializable object.
Returns:TreeState
conststate=tree.serialize();// {// version: 1,// nodes: { 'uuid-1': { ... }, 'uuid-2': { ... } },// rootId: 'uuid-1',// headId: 'uuid-2',// redoStack: []// }// Persist to disk, database, or transmit over the networkconstjson=JSON.stringify(state);A readonly property returning the total number of nodes in the tree.
Type:number
console.log(tree.nodeCount);// 5Subscribes to tree events. Returns an unsubscribe function.
Parameters:
| Parameter | Type | Description |
|---|---|---|
event | string | The event name: 'message', 'fork', 'switch', or 'prune'. |
handler | Function | The callback invoked when the event fires. |
Returns:() => void -- call this function to unsubscribe.
| Event | Payload | Fires when |
|---|---|---|
message | ConversationNode | addMessage() creates a new node. |
fork | Branch | fork() is called. |
switch | string (nodeId) | switchTo() moves HEAD. |
prune | { nodeId: string, count: number } | prune() removes nodes. |
constunsub=tree.on('message',(node)=>{console.log('New message:',node.role,node.content);});tree.addMessage('user','Hello');// triggers handlerunsub();// stop listeningtree.addMessage('user','World');// handler is NOT calledAll types are exported from the package entry point.
importtype{ConversationNode,ConversationTree,ConversationTreeOptions,Branch,Message,TreeState,}from'convo-tree';interfaceConversationNode{id: string;role: 'system'|'user'|'assistant'|'tool';content: string;parentId: string|null;children: string[];createdAt: number;metadata: Record<string,unknown>;branchLabel?: string;}interfaceBranch{forkPointId: string;label?: string;}interfaceMessage{role: string;content: string;[k: string]: unknown;}interfaceTreeState{nodes: Record<string,ConversationNode>;rootId: string|null;headId: string|null;redoStack: string[];version: 1;}interfaceConversationTreeOptions{systemPrompt?: string;treeMeta?: Record<string,unknown>;now?: ()=>number;generateId?: ()=>string;}Supply a deterministic ID generator for reproducible tests or when UUIDs are not desired.
letcounter=0;consttree=createConversationTree({generateId: ()=>`msg-${++counter}`,});constn1=tree.addMessage('user','Hello');// n1.id -> 'msg-1'Supply a custom clock for deterministic timestamps in tests or when using a different time source.
consttree=createConversationTree({now: ()=>1700000000000,});constnode=tree.addMessage('user','Hello');// node.createdAt -> 1700000000000convo-tree exports three error classes, all extending from ConvoTreeError.
import{ConvoTreeError,NodeNotFoundError,InvalidOperationError,}from'convo-tree';Base error class. Has a code property (string) for programmatic error handling.
try{tree.switchTo('nonexistent');}catch(err){if(errinstanceofConvoTreeError){console.log(err.code);// 'NODE_NOT_FOUND'}}Thrown when an operation references a node ID that does not exist in the tree. Has a nodeId property indicating which ID was not found.
- Code:
'NODE_NOT_FOUND' - Thrown by:
switchTo(),getPathTo(),prune(),setLabel(),fork()(whennodeIdis provided)
try{tree.getPathTo('does-not-exist');}catch(err){if(errinstanceofNodeNotFoundError){console.log(err.nodeId);// 'does-not-exist'}}Thrown when an operation is structurally invalid given the current tree state.
- Code:
'INVALID_OPERATION' - Thrown by:
fork()when called on an empty tree
constemptyTree=createConversationTree();try{emptyTree.fork();}catch(err){if(errinstanceofInvalidOperationError){console.log(err.message);// 'Cannot fork an empty tree'}}Fork at any point to explore alternative continuations, then switch between branches.
consttree=createConversationTree();constquestion=tree.addMessage('user','What is the capital of France?');constresponseA=tree.addMessage('assistant','Paris.');// Fork back to the question and try a different responsetree.fork(question.id,'detailed-response');tree.switchTo(question.id);constresponseB=tree.addMessage('assistant','The capital of France is Paris.');// Extract each branch independentlyconstpathA=tree.getPathTo(responseA.id);// [{ role: 'user', content: 'What is the capital of France?' },// { role: 'assistant', content: 'Paris.' }]constpathB=tree.getPathTo(responseB.id);// [{ role: 'user', content: 'What is the capital of France?' },// { role: 'assistant', content: 'The capital of France is Paris.' }]Calling addMessage() after undo() creates a new branch from the undo point and clears the redo stack.
consttree=createConversationTree();tree.addMessage('user','First');tree.addMessage('assistant','Second');tree.addMessage('user','Third');tree.undo();// HEAD at 'Second'tree.undo();// HEAD at 'First'// New message creates a branch from 'First'tree.addMessage('assistant','Alternative second');// redo() now returns null -- redo stack was clearedSerialize the tree for storage and reconstruct later.
// Saveconststate=tree.serialize();constjson=JSON.stringify(state);fs.writeFileSync('conversation.json',json);// Loadconstloaded=JSON.parse(fs.readFileSync('conversation.json','utf-8'));// Reconstruct by creating a new tree and replaying messages// from loaded.nodes in createdAt orderUse the event system for reactive UI updates, logging, or analytics.
consttree=createConversationTree();// Log all new messagestree.on('message',(node)=>{console.log(`[${node.role}] ${node.content}`);});// Track branch creationtree.on('fork',(branch)=>{console.log(`Forked at ${branch.forkPointId}: ${branch.label??'unlabeled'}`);});// Monitor pruningtree.on('prune',({ nodeId, count })=>{console.log(`Pruned ${count} nodes starting from ${nodeId}`);});// React to navigationtree.on('switch',(nodeId)=>{console.log(`Switched HEAD to ${nodeId}`);});Store per-message provenance data such as model, latency, and token counts.
constnode=tree.addMessage('assistant','Hello!',{model: 'gpt-4o',temperature: 0.7,latencyMs: 450,promptTokens: 128,completionTokens: 12,});// Metadata is included in getActivePath() outputconstmessages=tree.getActivePath();// Last message: { role: 'assistant', content: 'Hello!',// model: 'gpt-4o', temperature: 0.7, latencyMs: 450, ... }Fork at the same point to compare responses from different models or prompt configurations.
consttree=createConversationTree({systemPrompt: 'You are a writing assistant.',});constprompt=tree.addMessage('user','Write a haiku about rain.');constresponseA=tree.addMessage('assistant','Gentle drops descend...');// Fork for a second attempttree.fork(prompt.id,'attempt-2');tree.switchTo(prompt.id);constresponseB=tree.addMessage('assistant','Silver threads of rain...');// Fork for a third attempttree.fork(prompt.id,'attempt-3');tree.switchTo(prompt.id);constresponseC=tree.addMessage('assistant','Clouds weep softly now...');// Compare all three pathsconstpaths=[responseA,responseB,responseC].map((r)=>tree.getPathTo(r.id));convo-tree is written in TypeScript and ships type declarations alongside the compiled JavaScript. All public types are exported from the package entry point.
import{createConversationTree}from'convo-tree';importtype{ConversationNode,ConversationTree,ConversationTreeOptions,Branch,Message,TreeState,}from'convo-tree';The ConversationTree interface defines the full shape of the tree object returned by createConversationTree(). Use it for explicit typing when passing tree instances between functions.
functionanalyzeTree(tree: ConversationTree): void{constpath=tree.getActivePath();consthead=tree.getHead();console.log(`${tree.nodeCount} nodes, head at ${head?.id??'empty'}`);}MIT