UNPKG

donobu

Version:

Create browser automations with an LLM agent and replay them as Playwright scripts.

88 lines 3.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FlowsPersistenceRegistryImpl = void 0; const DonobuSqliteDb_1 = require("../DonobuSqliteDb"); const PersistencePlugin_1 = require("../PersistencePlugin"); const FlowsPersistenceDonobuApi_1 = require("./FlowsPersistenceDonobuApi"); const FlowsPersistenceSqlite_1 = require("./FlowsPersistenceSqlite"); const FlowsPersistenceVolatile_1 = require("./FlowsPersistenceVolatile"); /** * A factory class for creating FlowsPersistence instances. Persistence layers are constructed * eagerly at creation time and reused across all subsequent calls. */ class FlowsPersistenceRegistryImpl { /** * Creates an instance with pre-built persistence layers. */ constructor(layers) { this.layers = layers; if (layers.length === 0) { throw new Error('No valid flow persistence implementation available!'); } } /** * Creates an instance by reading environment variables and eagerly constructing * all applicable persistence layers. */ static async fromEnvironment(environ, persistencePlugins = new PersistencePlugin_1.PersistencePluginRegistry()) { const donobuApiBaseUrl = environ.data.DONOBU_API_BASE_URL; // Persistence credential resolution: prefer the primary DONOBU_API_KEY // (which also drives the inference fallback chain in // DonobuFlowsManager.createGptClient), then fall back to the // persistence-only DONOBU_PERSISTENCE_API_KEY for hosts that route // inference through an explicit gpt-config and don't want their // persistence credential to short-circuit the agent lookup. const donobuApiKey = environ.data.DONOBU_API_KEY ?? environ.data.DONOBU_PERSISTENCE_API_KEY; const layers = []; for (const key of environ.data.PERSISTENCE_PRIORITY) { switch (key) { case 'DONOBU': if (donobuApiKey && donobuApiBaseUrl) { layers.push({ key, persistence: new FlowsPersistenceDonobuApi_1.FlowsPersistenceDonobuApi(donobuApiBaseUrl, donobuApiKey), }); } break; case 'LOCAL': layers.push({ key, persistence: await FlowsPersistenceSqlite_1.FlowsPersistenceSqlite.create(await (0, DonobuSqliteDb_1.getDonobuSqliteDatabase)()), }); break; case 'RAM': layers.push({ key, persistence: new FlowsPersistenceVolatile_1.FlowsPersistenceVolatile() }); break; default: { const plugin = persistencePlugins.get(key); if (plugin) { const impl = await plugin.createFlowsPersistence(); if (impl) { layers.push({ key, persistence: impl }); } } break; } } } return new FlowsPersistenceRegistryImpl(layers); } /** * Returns the primary persistence layer. */ async get() { return this.layers[0].persistence; } /** Returns all persistence layers. Guaranteed to be non-empty. */ async getAll() { return this.layers.map((layer) => layer.persistence); } async getEntries() { return this.layers; } async getByKey(key) { return this.layers.find((layer) => layer.key === key)?.persistence; } } exports.FlowsPersistenceRegistryImpl = FlowsPersistenceRegistryImpl; //# sourceMappingURL=FlowsPersistenceRegistry.js.map