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
75 lines (74 loc) • 3.69 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createArvoOrchestrator = void 0;
var _1 = require(".");
var MachineExecutionEngine_1 = require("../MachineExecutionEngine");
var MachineRegistry_1 = require("../MachineRegistry");
var errors_1 = require("../errors");
/**
* Creates a new Arvo orchestrator instance with default components.
*
* Factory function that constructs an orchestrator with standard execution engine
* and registry implementations. Validates that all machines share the same source
* identifier and have unique versions.
*
* @param params - Configuration parameters for the orchestrator
* @returns Configured ArvoOrchestrator instance ready for event handling
*
* @throws {Error} When no machines are provided
* @throws {ConfigViolation} When machines have different source identifiers
* @throws {ConfigViolation} When machines have duplicate versions
*
* @example
* ```typescript
* const orchestrator = createArvoOrchestrator({
* memory: new SimpleMachineMemory(),
* machines: [userOnboardingMachine, paymentMachine]
* });
*
* // Process events
* const result = await orchestrator.execute(event);
* ```
*
* @see {@link setupArvoMachine} for creating machine definitions
* @see {@link ArvoOrchestrator} for direct instantiation with custom components
*/
var createArvoOrchestrator = function (_a) {
var executionunits = _a.executionunits, memory = _a.memory, machines = _a.machines, defaultEventEmissionDomains = _a.defaultEventEmissionDomains, spanOptions = _a.spanOptions, _locking = _a.requiresResourceLocking;
if (!(machines === null || machines === void 0 ? void 0 : machines.length)) {
throw new Error('At least one machine must be provided');
}
var registry = new (MachineRegistry_1.MachineRegistry.bind.apply(MachineRegistry_1.MachineRegistry, __spreadArray([void 0], machines, false)))();
var requiresResourceLocking = _locking !== null && _locking !== void 0 ? _locking : machines.some(function (machine) { return machine.requiresResourceLocking; });
var representativeMachine = registry.machines[0];
var lastSeenVersions = new Set();
for (var _i = 0, _b = registry.machines; _i < _b.length; _i++) {
var machine = _b[_i];
if (representativeMachine.source !== machine.source) {
throw new errors_1.ConfigViolation("All the machines in the orchestrator must have source '".concat(representativeMachine.source, "'. Make sure all machine are implementing versions of the same self contract"));
}
if (lastSeenVersions.has(machine.version)) {
throw new errors_1.ConfigViolation("An orchestrator must have unique machine versions. Machine ID:".concat(machine.id, " has duplicate version ").concat(machine.version, "."));
}
lastSeenVersions.add(machine.version);
}
return new _1.ArvoOrchestrator({
executionunits: executionunits,
memory: memory,
registry: registry,
executionEngine: new MachineExecutionEngine_1.MachineExecutionEngine(),
requiresResourceLocking: requiresResourceLocking,
defaultEventEmissionDomains: defaultEventEmissionDomains,
spanOptions: spanOptions,
});
};
exports.createArvoOrchestrator = createArvoOrchestrator;