UNPKG

loom-agents

Version:

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

51 lines 1.81 kB
import { TraceSession } from "../TraceSession/TraceSession.js"; export class Runner { config; agent; traceSession; constructor(agent, config = {}) { if (!agent) { throw new Error("Agent is required"); } this.config = { name: config.name || "Runner", id: config.id || "", maxDepth: config.maxDepth || 10, context: config.context || {}, }; this.agent = agent; // Create a new TraceSession for this run. this.traceSession = new TraceSession("RunnerSession", { agent: this.agent.uuid, config: this.config, }); } /** * Runs the agent through potentially multiple turns, * ensuring each turn is traced and ultimately returning the full trace tree. */ async run(input) { // Start a top-level trace for the run. this.traceSession.start("runner.run", { input }); let depth = 0; let result = await this.agent.run(input, this.traceSession); // For multi-turn interactions, wrap each turn in its own trace. while (depth < (this.config.maxDepth || 10) && result.status !== "completed") { depth++; this.traceSession.start(`turn-${depth}`, {}); result = await this.agent.run({ context: result.context }, this.traceSession); this.traceSession.end(); // End current turn trace. } // End the top-level run trace. this.traceSession.end(); return { ...result, traceTree: this.traceSession.getTraceTree() }; } /** * Renders the entire trace tree as a formatted string. */ renderTraces() { return this.traceSession.render(); } } //# sourceMappingURL=Runner.js.map