TreeBase is an ideal structure for managing the hierarchical data often encountered in AI agent workflows, such as conversation branching, thought tracing, and complex tool output organization.
When building agents that support "What-if" scenarios or multi-path dialogues, you can use TreeBase to manage the conversation history.
// Branching a conversation
const chatStore = new TreeBase({ data: initialChat });
// User asks a different question from a previous turn
chatStore.add({
id: 'branch-1',
pid: 'turn-3',
role: 'user',
content: 'What if we try Option B instead?'
});
// The agent can now navigate different "timelines" of the conversation
const timeline = chatStore.getParents('branch-1');For complex tasks, agents often need to explore multiple reasoning paths simultaneously.
- Nodes: Represent a reasoning step or a hypothesis.
- Children: Represent refinements or alternative paths based on the parent step.
- Ordering: Use the
indexproperty to rank paths by confidence.
When an agent calls a tool that triggers sub-agents or sub-tasks, TreeBase organizes these results semantically.
// Organising a multi-step research task
tb.add({ id: 'research-task', title: 'Find 2024 GDP' });
tb.add({ id: 'step-1', pid: 'research-task', title: 'Search IMF API' });
tb.add({ id: 'step-2', pid: 'research-task', title: 'Cross-reference with World Bank' });Instead of sending a flat history to an LLM, use getParents(childId) to reconstruct only the relevant path (the "current timeline") for the agent. This saves tokens and reduces noise from discarded branches.
Use custom properties in your nodes to store metadata like token_count, latency, or model_version. Use the search() method to find specific reasoning steps across a massive trace.
When an agent is streaming updates to a node's content in a reactive environment (Vue/Pinia, React/Valtio, MobX):
- Update the
dictionarydirectly for performance during the stream. - Call
refresh()once the stream completes or at throttled intervals to update the UI tree.
// Throttled update pattern for agents
function onStreamChunk(chunk) {
state.dictionary[currentNodeId].content += chunk;
// Throttled refresh to keep UI smooth but updated
throttledRefresh();
}When an AI Agent is modifying the tree, it MUST follow these rules to maintain internal state integrity:
- API Preference: Always prefer using
tb.add(),tb.update(),tb.move(), ortb.delete(). These methods manage internal maps and cache automatically. - External Mutations: If you mutate the
dictionarydirectly (e.g.,dictionary[id].prop = val), you MUST calltb.refresh()immediately after. - Bulk Updates: When replacing the entire tree, use
tb.setData(newData)instead of manual assignment totb.dictionary. - UI Sync: In reactive environments (Vue/Pinia, React/Valtio), remember that
TreeBaseinternal Maps (childrenMap,treeCache) are only rebuilt during arefresh(). If you omit this, the UI tree will become stale even if the dictionary appears updated.
| Method | Agentic Use Case |
|---|---|
getTree(root) |
Visualizing the full reasoning/dialogue tree. |
getParents(id) |
Extracting the current "Context Path" for LLM prompts. |
move(id, {pid}) |
Refactoring thought structures or re-categorizing information. |
search(path, val) |
Finding past memories or tool outputs by semantic keys. |
refresh() |
Syncing state after high-frequency streaming updates. |