UNPKG

agentlang

Version:

The easiest way to build the most reliable AI agents - enterprise-grade teams of AI agents that collaborate with each other and humans

249 lines 9.47 kB
import { callPostEventOnSubscription, runPostCreateEvents, runPostDeleteEvents, runPostUpdateEvents, } from '../interpreter.js'; import { logger } from '../logger.js'; import { makeInstance, newInstanceAttributes, } from '../module.js'; import { CrudType, nameToPath } from '../util.js'; import { DefaultAuthInfo } from './authinfo.js'; const subscriptionEvents = new Map(); export function setSubscriptionEvent(fqEventName, resolverName) { subscriptionEvents.set(resolverName, fqEventName); } export function getSubscriptionEvent(resolverName) { return subscriptionEvents.get(resolverName); } export class Resolver { constructor(name) { this.authInfo = DefaultAuthInfo; this.name = 'default'; if (name) this.name = name; } setAuthInfo(authInfo) { this.authInfo = authInfo; return this; } setEnvironment(env) { this.env = env; return this; } getEnvironment() { return this.env; } suspend() { var _a; (_a = this.env) === null || _a === void 0 ? void 0 : _a.suspend(); return this; } getName() { return this.name; } notImpl(method) { logger.warn(`Method ${method} not implemented in resolver ${this.name}`); } onSetPath(moduleName, entryName) { this.notImpl(`onSetPath(${moduleName}, ${entryName})`); } async createInstance(inst) { this.notImpl(`createInstance(${inst})`); } async upsertInstance(inst) { return this.notImpl(`upsertInstance(${inst})`); } /** * @param {Instance} inst - an Instance with query and update attributes * @param {InstanceAttributes} newAttrs - updated attributes to set in instance */ async updateInstance(inst, newAttrs) { return this.notImpl(`updateInstance(${inst}, ${newAttrs})`); } /** * @param {Instance} inst - an Instance with query attributes * @param {boolean} queryAll - if this flag is set, fetch all instances */ async queryInstances(inst, queryAll, distinct = false) { return this.notImpl(`queryInstances(${inst}, ${queryAll}, ${distinct})`); } /** * Return all instances under the given parent-path. * @param {string} parentPath - path of the parent with the relevant relationship name as the last component * @param {Instance} inst - child Instance with query attributes */ async queryChildInstances(parentPath, inst) { return this.notImpl(`queryChildInstances(${parentPath}, ${inst})`); } /** * Return all instances connected to connectedInstance via the given between-relationship * @param relationship Between relationship * @param connectedInstance The instance to traveres the relationship from * @param inst Target instance with query attributes */ async queryConnectedInstances(relationship, connectedInstance, inst) { return this.notImpl(`queryConnectedInstances(${relationship}, ${connectedInstance}, ${inst})`); } async queryByJoin(inst, joinInfo, intoSpec, distinct = false, rawJoinSpec) { return this.notImpl(`queryByJoin(${inst}, ${joinInfo}, ${intoSpec}, ${distinct} ${rawJoinSpec})`); } /** * @param {Instance} inst - an Instance with query attributes */ async deleteInstance(inst, purge) { return this.notImpl(`deleteInstance(${inst}, ${purge})`); } /** * Connect instances via a between relationship * @param node1 The main node to connect * @param otherNodeOrNodes Nodes to be connected to node1 * @param relEntry Details of the repationship */ async connectInstances(node1, otherNodeOrNodes, relEntry, orUpdate) { return this.notImpl(`connectInstances(${node1}, ${otherNodeOrNodes}, ${relEntry}, ${orUpdate})`); } async fullTextSearch(entryName, moduleName, query, options) { return this.notImpl(`fullTextSearch(${entryName}, ${moduleName}, ${query}, ${options})`); } // Return a transactionId async startTransaction() { this.notImpl('startTransaction()'); return 1; } async commitTransaction(txnId) { return this.notImpl(`commitTransaction(${txnId})`); } async rollbackTransaction(txnId) { return this.notImpl(`rollbackTransaction(${txnId})`); } async subscribe() { return undefined; } async onOutOfBandCrud(inst, operation, env) { switch (operation) { case CrudType.CREATE: return await runPostCreateEvents(inst, env); case CrudType.UPDATE: return await runPostUpdateEvents(inst, undefined, env); case CrudType.DELETE: return await runPostDeleteEvents(inst, env); default: return inst; } } async onCreate(inst, env) { return this.onOutOfBandCrud(inst, CrudType.CREATE, env); } async onUpdate(inst, env) { return this.onOutOfBandCrud(inst, CrudType.UPDATE, env); } async onDelete(inst, env) { return this.onOutOfBandCrud(inst, CrudType.DELETE, env); } async onSubscription(result, callPostCrudEvent = false) { if (result !== undefined) { try { if (callPostCrudEvent) { const inst = result; return await callPostEventOnSubscription(CrudType.CREATE, inst); } else { const eventName = getSubscriptionEvent(this.name); if (eventName) { const path = nameToPath(eventName); const inst = makeInstance(path.getModuleName(), path.getEntryName(), newInstanceAttributes().set('data', result)); const { evaluate } = await import('../interpreter.js'); return await evaluate(inst); } } } catch (err) { logger.error(`Resolver ${this.name} raised error in onSubscription handler: ${err}`); return undefined; } } } } Resolver.Default = new Resolver(); export class GenericResolver extends Resolver { constructor(name, implementation) { super(name); this.implementation = implementation; } async createInstance(inst) { var _a; if ((_a = this.implementation) === null || _a === void 0 ? void 0 : _a.create) { return await this.implementation.create(this, inst); } else { return await super.createInstance(inst); } } async upsertInstance(inst) { var _a; if ((_a = this.implementation) === null || _a === void 0 ? void 0 : _a.upsert) { return await this.implementation.upsert(this, inst); } return await super.upsertInstance(inst); } async updateInstance(inst, newAttrs) { var _a; if ((_a = this.implementation) === null || _a === void 0 ? void 0 : _a.update) { return await this.implementation.update(this, inst, newAttrs); } return await super.updateInstance(inst, newAttrs); } async queryInstances(inst, queryAll) { var _a; if ((_a = this.implementation) === null || _a === void 0 ? void 0 : _a.query) { return await this.implementation.query(this, inst, queryAll); } return await super.queryInstances(inst, queryAll); } async deleteInstance(inst, purge) { var _a; if ((_a = this.implementation) === null || _a === void 0 ? void 0 : _a.delete) { return await this.implementation.delete(this, inst, purge); } return await super.deleteInstance(inst, purge); } async startTransaction() { var _a; if ((_a = this.implementation) === null || _a === void 0 ? void 0 : _a.startTransaction) { return await this.implementation.startTransaction(this); } return await super.startTransaction(); } async commitTransaction(txnId) { var _a; if ((_a = this.implementation) === null || _a === void 0 ? void 0 : _a.commitTransaction) { return await this.implementation.commitTransaction(this, txnId); } return await super.commitTransaction(txnId); } async rollbackTransaction(txnId) { var _a; if ((_a = this.implementation) === null || _a === void 0 ? void 0 : _a.rollbackTransaction) { return await this.implementation.rollbackTransaction(this, txnId); } return await super.rollbackTransaction(txnId); } async subscribe() { var _a; const MaxErrors = 3; let errCount = 0; while (true) { try { if ((_a = this.subs) === null || _a === void 0 ? void 0 : _a.subscribe) { await this.subs.subscribe(this); } await super.subscribe(); return; } catch (reason) { logger.warn(`subscribe error in resolver ${this.name}: ${reason}`); if (errCount >= MaxErrors) { logger.warn(`exiting resolver subscription after ${errCount} retries`); break; } ++errCount; } } } } //# sourceMappingURL=interface.js.map