UNPKG

loom-agents

Version:

A lightweight, composable framework for building hierarchical AI agent systems using OpenAI's API.

55 lines 1.62 kB
import { v4 as uuidv4 } from "uuid"; export class TraceSession { root; currentPath; constructor(name, data = {}) { this.root = { id: `trace.${uuidv4()}`, name, data, startTime: Date.now(), children: [], }; this.currentPath = [this.root]; } start(name, data = {}) { const newNode = { id: `trace.${uuidv4()}`, name, data, startTime: Date.now(), children: [], }; const current = this.currentPath[this.currentPath.length - 1]; current.children.push(newNode); this.currentPath.push(newNode); return newNode; } end() { const current = this.currentPath.pop(); if (!current) { throw new Error("No active trace node to end"); } current.endTime = Date.now(); return current.endTime - current.startTime; } getTraceTree() { return this.root; } render(node = this.root, indent = "") { const duration = node.endTime ? ` (${node.endTime - node.startTime} ms)` : ""; let output = `${indent}[${node.id}] ${node.name}${duration}`; if (node.data && Object.keys(node.data).length > 0) { output += ` - ${JSON.stringify(node.data)}`; } output += "\n"; const newIndent = indent + " "; for (const child of node.children) { output += this.render(child, newIndent); } return output; } } //# sourceMappingURL=TraceSession.js.map