UNPKG

@nomyx/assistant

Version:

A powerful assistant library and cli for your AI projects. works with Vertex AI (Claude and Gemini)

55 lines (54 loc) 1.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActionLoopImpl = void 0; class ActionLoopImpl { constructor(options) { this.steps = []; this.plugins = []; this.provider = options.provider; this.tools = options.tools; this.logger = options.logger; this.options = { maxIterations: options.maxIterations || 10, timeoutMs: options.timeoutMs || 30000, }; } async execute(initialState) { let currentState = initialState; let iteration = 0; const startTime = Date.now(); while (iteration < this.options.maxIterations) { if (Date.now() - startTime > this.options.timeoutMs) { throw new Error('Action loop execution timed out'); } await this.notifyPlugins('onBeforeStep', currentState); for (const step of this.steps) { currentState = await step.execute(currentState); } await this.notifyPlugins('onAfterStep', currentState); iteration++; if (this.isLoopComplete(currentState)) { break; } } await this.notifyPlugins('onLoopComplete', currentState); return currentState; } addStep(step) { this.steps.push(step); } removeStep(stepName) { this.steps = this.steps.filter(step => step.name !== stepName); } isLoopComplete(state) { return false; } async notifyPlugins(event, state) { for (const plugin of this.plugins) { if (plugin[event]) { await plugin[event](state); } } } } exports.ActionLoopImpl = ActionLoopImpl;