UNPKG

aiwrapper

Version:

A Universal AI Wrapper for JavaScript & TypeScript

65 lines (64 loc) 1.93 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); class Agent { constructor() { __publicField(this, "listeners", []); __publicField(this, "_state", "idle"); } // Get current agent state get state() { return this._state; } // Subscribe to agent events subscribe(listener) { this.listeners.push(listener); return () => { const index = this.listeners.indexOf(listener); if (index > -1) { this.listeners.splice(index, 1); } }; } // Run the agent either with a new input or the provided in this.lastInput async run(input, options) { this.setState("running"); try { const result = await this.runInternal(input, options); this.setState("idle"); return result; } catch (error) { if ((error == null ? void 0 : error.name) === "AbortError") { const partial = error == null ? void 0 : error.partialResult; this.emit({ type: "aborted", error, partial }); this.setState("idle"); if (partial !== void 0) return partial; throw error; } this.emit({ type: "error", error }); this.setState("idle"); throw error; } } // Set state and emit state change event setState(newState) { if (this._state !== newState) { this._state = newState; this.emit({ type: "state", state: newState }); } } // Emit events to all listeners emit(event) { this.listeners.forEach((listener) => { try { listener(event); } catch (error) { console.error("Error in agent event listener:", error); } }); } } export { Agent }; //# sourceMappingURL=agent.js.map