loom-agents
Version:
A lightweight, composable framework for building hierarchical AI agent systems using OpenAI's API.
51 lines • 1.6 kB
JavaScript
import { v4 } from "uuid";
export class Trace {
details;
children = [];
parent;
constructor(name, data, parent) {
this.details = {
name,
uuid: `trace.${v4()}`,
data,
startTime: Date.now(),
children: [],
};
this.parent = parent;
}
start(name, data) {
const subTrace = new Trace(name, data, this);
this.children.push(subTrace);
return subTrace;
}
end() {
this.details.endTime = Date.now();
return this.details.endTime - this.details.startTime;
}
getDetails() {
return {
...this.details,
children: this.children.map((child) => child.getDetails()),
};
}
render(indent = "", last = true) {
const details = this.getDetails();
const connector = indent ? (last ? "└─ " : "├─ ") : "";
let line = `${indent}${connector}[${details.uuid}] ${details.name}`;
if (typeof details.endTime === "number") {
const duration = details.endTime - details.startTime;
line += ` (${duration} ms)`;
}
if (details.data) {
line += ` - ${JSON.stringify(details.data)}`;
}
let output = line + "\n";
const newIndent = indent + (last ? " " : "│ ");
this.children.forEach((child, index) => {
const childIsLast = index === this.children.length - 1;
output += child.render(newIndent, childIsLast);
});
return output;
}
}
//# sourceMappingURL=Trace.js.map