UNPKG

arvo-event-handler

Version:

A complete set of orthogonal event handler and orchestration primitives for Arvo based applications, featuring declarative state machines (XState), imperative resumables for agentic workflows, contract-based routing, OpenTelemetry observability, and in-me

88 lines (87 loc) 3.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MachineRegistry = void 0; var api_1 = require("@opentelemetry/api"); var arvo_core_1 = require("arvo-core"); /** * Registry for managing and resolving ArvoMachine instances. * Provides functionality to store multiple machine instances and resolve them based on events. * * @remarks * The registry must contain at least one machine upon initialization. * Each machine in the registry should have a unique combination of version and source. */ var MachineRegistry = /** @class */ (function () { /** * Creates a new MachineRegistry instance with the provided machines. * * @param args - Variable number of ArvoMachine instances to register * @throws {Error} When no machines are provided during initialization */ function MachineRegistry() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } this.machines = args; if (!this.machines.length) { throw new Error('Machine registry initialization failed: No machines provided. At least one machine must be registered.'); } } /** * Resolves and returns a machine instance based on the provided event. * The resolution is performed using the orchestrator information in the event's subject. * * @param event - The event containing orchestration subject information * @param opentelemetry Telemetry configuration for tracing * @returns The matching ArvoMachine instance or null if not found * * @example * ```typescript * const machine = registry.resolve(incomingEvent); * // Use resolved machine for event processing * ``` */ MachineRegistry.prototype.resolve = function (event, opentelemetry) { var _this = this; if (opentelemetry === void 0) { opentelemetry = { inheritFrom: 'CONTEXT', }; } return arvo_core_1.ArvoOpenTelemetry.getInstance().startActiveSpan({ name: 'Resolve Machine', spanOptions: { kind: api_1.SpanKind.INTERNAL, attributes: event.otelAttributes, }, context: opentelemetry.inheritFrom === 'EVENT' ? { inheritFrom: 'TRACE_HEADERS', traceHeaders: { traceparent: event.traceparent, tracestate: event.tracestate, }, } : { inheritFrom: 'CONTEXT', context: api_1.context.active(), }, fn: function (span) { var _a; var subject = arvo_core_1.ArvoOrchestrationSubject.parse(event.subject); var _b = subject.orchestrator, name = _b.name, version = _b.version; span.setAttributes({ 'arvo.parsed.subject.orchestrator.name': name, 'arvo.parsed.subject.orchestrator.version': version, }); var machine = (_a = _this.machines.find(function (item) { return item.version === version && item.source === name; })) !== null && _a !== void 0 ? _a : null; (0, arvo_core_1.logToSpan)({ level: machine ? 'INFO' : 'WARNING', message: machine ? "Resolved machine for type ".concat(name, "@").concat(version) : "No machine found for ".concat(name, "@").concat(version), }); return machine; }, }); }; return MachineRegistry; }()); exports.MachineRegistry = MachineRegistry;