arvo-event-handler 3.0.7 → 3.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/ArvoEventHandler/index.d.ts +1 -1
  2. package/dist/ArvoEventHandler/index.js +1 -1
  3. package/dist/ArvoMachine/createMachine.d.ts +1 -1
  4. package/dist/ArvoMachine/createMachine.js +1 -1
  5. package/dist/ArvoOrchestrationUtils/createEmitableEvent.d.ts +30 -0
  6. package/dist/ArvoOrchestrationUtils/createEmitableEvent.js +160 -0
  7. package/dist/ArvoOrchestrationUtils/error.d.ts +18 -0
  8. package/dist/{ArvoOrchestrator → ArvoOrchestrationUtils}/error.js +14 -9
  9. package/dist/ArvoOrchestrationUtils/handlerErrors.d.ts +35 -0
  10. package/dist/ArvoOrchestrationUtils/handlerErrors.js +184 -0
  11. package/dist/ArvoOrchestrationUtils/orchestrationExecutionState.d.ts +11 -0
  12. package/dist/ArvoOrchestrationUtils/orchestrationExecutionState.js +7 -0
  13. package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/acquireLockWithValidation.d.ts +8 -0
  14. package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/acquireLockWithValidation.js +69 -0
  15. package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/index.d.ts +40 -0
  16. package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/index.js +228 -0
  17. package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/validateAndParseSubject.d.ts +7 -0
  18. package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/validateAndParseSubject.js +25 -0
  19. package/dist/ArvoOrchestrationUtils/types.d.ts +1 -0
  20. package/dist/ArvoOrchestrationUtils/types.js +2 -0
  21. package/dist/ArvoOrchestrator/factory.js +13 -0
  22. package/dist/ArvoOrchestrator/index.d.ts +3 -19
  23. package/dist/ArvoOrchestrator/index.js +85 -477
  24. package/dist/ArvoOrchestrator/types.d.ts +3 -2
  25. package/dist/ArvoResumable/factory.d.ts +2 -2
  26. package/dist/ArvoResumable/factory.js +1 -1
  27. package/dist/ArvoResumable/index.d.ts +5 -19
  28. package/dist/ArvoResumable/index.js +76 -433
  29. package/dist/ArvoResumable/types.d.ts +5 -4
  30. package/dist/IArvoEventHandler/index.d.ts +0 -1
  31. package/dist/MachineExecutionEngine/index.d.ts +1 -1
  32. package/dist/MachineRegistry/index.d.ts +1 -1
  33. package/dist/SyncEventResource/index.d.ts +1 -1
  34. package/dist/SyncEventResource/index.js +1 -1
  35. package/dist/index.d.ts +12 -11
  36. package/dist/index.js +24 -21
  37. package/package.json +2 -2
  38. package/dist/ArvoOrchestrator/error.d.ts +0 -16
@@ -0,0 +1,228 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
24
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.executeWithOrchestrationWrapper = exports.returnEventsWithLogging = void 0;
51
+ var api_1 = require("@opentelemetry/api");
52
+ var arvo_core_1 = require("arvo-core");
53
+ var handlerErrors_1 = require("../handlerErrors");
54
+ var acquireLockWithValidation_1 = require("./acquireLockWithValidation");
55
+ var validateAndParseSubject_1 = require("./validateAndParseSubject");
56
+ var returnEventsWithLogging = function (param, span) {
57
+ var _a, _b;
58
+ (0, arvo_core_1.logToSpan)({
59
+ level: 'INFO',
60
+ message: "Execution completed with issues and emitted ".concat((_b = (_a = param.events) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0, " events"),
61
+ }, span);
62
+ return param;
63
+ };
64
+ exports.returnEventsWithLogging = returnEventsWithLogging;
65
+ /**
66
+ * Wraps orchestration execution with common infrastructure:
67
+ * - OpenTelemetry span management
68
+ * - Lock acquisition and release
69
+ * - State management
70
+ * - Error handling and system error generation
71
+ */
72
+ var executeWithOrchestrationWrapper = function (_a, coreExecutionFn_1) { return __awaiter(void 0, [_a, coreExecutionFn_1], void 0, function (_b, coreExecutionFn) {
73
+ var _c;
74
+ var event = _b.event, opentelemetry = _b.opentelemetry, spanName = _b.spanName, source = _b.source, syncEventResource = _b.syncEventResource, executionunits = _b.executionunits, systemErrorDomain = _b.systemErrorDomain, selfContract = _b.selfContract, domain = _b.domain, _handlerType = _b._handlerType;
75
+ return __generator(this, function (_d) {
76
+ switch (_d.label) {
77
+ case 0: return [4 /*yield*/, arvo_core_1.ArvoOpenTelemetry.getInstance().startActiveSpan({
78
+ name: spanName,
79
+ spanOptions: {
80
+ kind: api_1.SpanKind.PRODUCER,
81
+ attributes: __assign((_c = {}, _c[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = arvo_core_1.ArvoExecutionSpanKind.ORCHESTRATOR, _c[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = arvo_core_1.OpenInferenceSpanKind.CHAIN, _c), Object.fromEntries(Object.entries(event.otelAttributes).map(function (_a) {
82
+ var key = _a[0], value = _a[1];
83
+ return ["to_process.0.".concat(key), value];
84
+ }))),
85
+ },
86
+ context: opentelemetry.inheritFrom === 'EVENT'
87
+ ? {
88
+ inheritFrom: 'TRACE_HEADERS',
89
+ traceHeaders: {
90
+ traceparent: event.traceparent,
91
+ tracestate: event.tracestate,
92
+ },
93
+ }
94
+ : {
95
+ inheritFrom: 'CONTEXT',
96
+ context: api_1.context.active(),
97
+ },
98
+ disableSpanManagement: true,
99
+ fn: function (span) { return __awaiter(void 0, void 0, void 0, function () {
100
+ var otelHeaders, orchestrationParentSubject, initEventId, acquiredLock, parsedEventSubject, state, _a, emittables, newState, i, _i, _b, _c, key, value, error_1, _d, errorToThrow, errorEvents;
101
+ var _e, _f, _g, _h;
102
+ return __generator(this, function (_j) {
103
+ switch (_j.label) {
104
+ case 0:
105
+ (0, arvo_core_1.logToSpan)({
106
+ level: 'INFO',
107
+ message: "Starting execution for ".concat(event.type, " on subject ").concat(event.subject),
108
+ }, span);
109
+ otelHeaders = (0, arvo_core_1.currentOpenTelemetryHeaders)();
110
+ orchestrationParentSubject = null;
111
+ initEventId = null;
112
+ acquiredLock = null;
113
+ _j.label = 1;
114
+ case 1:
115
+ _j.trys.push([1, 6, 8, 10]);
116
+ parsedEventSubject = (0, validateAndParseSubject_1.validateAndParseSubject)(event, source, syncEventResource, span, 'orchestrator');
117
+ if (!parsedEventSubject) {
118
+ return [2 /*return*/, (0, exports.returnEventsWithLogging)({ events: [] }, span)];
119
+ }
120
+ return [4 /*yield*/, (0, acquireLockWithValidation_1.acquireLockWithValidation)(syncEventResource, event, span)];
121
+ case 2:
122
+ // Lock acquisition
123
+ acquiredLock = _j.sent();
124
+ return [4 /*yield*/, syncEventResource.acquireState(event, span)];
125
+ case 3:
126
+ state = _j.sent();
127
+ if ((state === null || state === void 0 ? void 0 : state.executionStatus) === 'failure') {
128
+ (0, arvo_core_1.logToSpan)({
129
+ level: 'WARNING',
130
+ message: "The orchestration has failed in a previous event. Ignoring event id: ".concat(event.id, " with event subject: ").concat(event.subject),
131
+ }, span);
132
+ return [2 /*return*/, (0, exports.returnEventsWithLogging)({ events: [] }, span)];
133
+ }
134
+ orchestrationParentSubject = (_e = state === null || state === void 0 ? void 0 : state.parentSubject) !== null && _e !== void 0 ? _e : null;
135
+ initEventId = (_f = state === null || state === void 0 ? void 0 : state.initEventId) !== null && _f !== void 0 ? _f : null;
136
+ if (!state) {
137
+ (0, arvo_core_1.logToSpan)({
138
+ level: 'INFO',
139
+ message: "Initializing new execution state for subject: ".concat(event.subject),
140
+ });
141
+ if (event.type !== source) {
142
+ (0, arvo_core_1.logToSpan)({
143
+ level: 'WARNING',
144
+ message: "Invalid initialization event detected. Expected type '".concat(source, "' but received '").concat(event.type, "'."),
145
+ });
146
+ return [2 /*return*/, (0, exports.returnEventsWithLogging)({ events: [] }, span)];
147
+ }
148
+ }
149
+ else {
150
+ (0, arvo_core_1.logToSpan)({
151
+ level: 'INFO',
152
+ message: "Resuming execution with existing state for subject: ".concat(event.subject),
153
+ });
154
+ }
155
+ // Extract parent subject from init event if applicable
156
+ if (event.type === source) {
157
+ orchestrationParentSubject = (_h = (_g = event === null || event === void 0 ? void 0 : event.data) === null || _g === void 0 ? void 0 : _g.parentSubject$$) !== null && _h !== void 0 ? _h : null;
158
+ }
159
+ return [4 /*yield*/, coreExecutionFn({
160
+ span: span,
161
+ otelHeaders: otelHeaders,
162
+ orchestrationParentSubject: orchestrationParentSubject,
163
+ initEventId: initEventId !== null && initEventId !== void 0 ? initEventId : event.id,
164
+ parsedEventSubject: parsedEventSubject,
165
+ state: state,
166
+ _handlerType: _handlerType,
167
+ })];
168
+ case 4:
169
+ _a = _j.sent(), emittables = _a.emittables, newState = _a.newState;
170
+ span.setAttribute("arvo.".concat(_handlerType, ".execution.status"), newState.executionStatus);
171
+ // Add OpenTelemetry attributes for emitted events
172
+ for (i = 0; i < emittables.length; i++) {
173
+ for (_i = 0, _b = Object.entries(emittables[i].otelAttributes); _i < _b.length; _i++) {
174
+ _c = _b[_i], key = _c[0], value = _c[1];
175
+ span.setAttribute("to_emit.".concat(i, ".").concat(key), value);
176
+ }
177
+ }
178
+ // Persist state
179
+ return [4 /*yield*/, syncEventResource.persistState(event, newState, state, span)];
180
+ case 5:
181
+ // Persist state
182
+ _j.sent();
183
+ (0, arvo_core_1.logToSpan)({
184
+ level: 'INFO',
185
+ message: "State update persisted in memory for subject ".concat(event.subject),
186
+ });
187
+ (0, arvo_core_1.logToSpan)({
188
+ level: 'INFO',
189
+ message: "Execution successfully completed and emitted ".concat(emittables.length, " events"),
190
+ });
191
+ return [2 /*return*/, (0, exports.returnEventsWithLogging)({ events: emittables }, span)];
192
+ case 6:
193
+ error_1 = _j.sent();
194
+ return [4 /*yield*/, (0, handlerErrors_1.handleOrchestrationErrors)(_handlerType, {
195
+ error: error_1,
196
+ event: event,
197
+ otelHeaders: otelHeaders,
198
+ orchestrationParentSubject: orchestrationParentSubject,
199
+ initEventId: initEventId,
200
+ selfContract: selfContract,
201
+ systemErrorDomain: systemErrorDomain,
202
+ executionunits: executionunits,
203
+ source: source,
204
+ domain: domain,
205
+ syncEventResource: syncEventResource,
206
+ }, span)];
207
+ case 7:
208
+ _d = _j.sent(), errorToThrow = _d.errorToThrow, errorEvents = _d.events;
209
+ if (errorToThrow)
210
+ throw errorToThrow;
211
+ return [2 /*return*/, {
212
+ events: errorEvents,
213
+ }];
214
+ case 8: return [4 /*yield*/, syncEventResource.releaseLock(event, acquiredLock, span)];
215
+ case 9:
216
+ _j.sent();
217
+ span.end();
218
+ return [7 /*endfinally*/];
219
+ case 10: return [2 /*return*/];
220
+ }
221
+ });
222
+ }); },
223
+ })];
224
+ case 1: return [2 /*return*/, _d.sent()];
225
+ }
226
+ });
227
+ }); };
228
+ exports.executeWithOrchestrationWrapper = executeWithOrchestrationWrapper;
@@ -0,0 +1,7 @@
1
+ import type { Span } from '@opentelemetry/api';
2
+ import { type ArvoEvent, type ArvoOrchestrationSubjectContent } from 'arvo-core';
3
+ import type { SyncEventResource } from '../../SyncEventResource';
4
+ /**
5
+ * Validates and parses orchestration subject
6
+ */
7
+ export declare const validateAndParseSubject: (event: ArvoEvent, expectedSource: string, syncEventResource: SyncEventResource<any>, span: Span, handlerType: "orchestrator" | "resumable") => ArvoOrchestrationSubjectContent | null;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateAndParseSubject = void 0;
4
+ var arvo_core_1 = require("arvo-core");
5
+ /**
6
+ * Validates and parses orchestration subject
7
+ */
8
+ var validateAndParseSubject = function (event, expectedSource, syncEventResource, span, handlerType) {
9
+ var _a;
10
+ syncEventResource.validateEventSubject(event, span);
11
+ var parsedEventSubject = arvo_core_1.ArvoOrchestrationSubject.parse(event.subject);
12
+ span.setAttributes((_a = {},
13
+ _a["arvo.parsed.subject.".concat(handlerType, ".name")] = parsedEventSubject.orchestrator.name,
14
+ _a["arvo.parsed.subject.".concat(handlerType, ".version")] = parsedEventSubject.orchestrator.version,
15
+ _a));
16
+ if (parsedEventSubject.orchestrator.name !== expectedSource) {
17
+ (0, arvo_core_1.logToSpan)({
18
+ level: 'WARNING',
19
+ message: "Event subject mismatch - expected '".concat(expectedSource, "' but got '").concat(parsedEventSubject.orchestrator.name, "'"),
20
+ }, span);
21
+ return null;
22
+ }
23
+ return parsedEventSubject;
24
+ };
25
+ exports.validateAndParseSubject = validateAndParseSubject;
@@ -0,0 +1 @@
1
+ export type ArvoOrchestrationHandlerType = 'orchestrator' | 'resumable';
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -13,6 +13,7 @@ exports.createArvoOrchestrator = void 0;
13
13
  var _1 = require(".");
14
14
  var MachineExecutionEngine_1 = require("../MachineExecutionEngine");
15
15
  var MachineRegistry_1 = require("../MachineRegistry");
16
+ var errors_1 = require("../errors");
16
17
  /**
17
18
  * Creates a new Arvo orchestrator instance with default components.
18
19
  * For custom components, use ArvoOrchestrator constructor directly.
@@ -46,6 +47,18 @@ var createArvoOrchestrator = function (_a) {
46
47
  }
47
48
  var registry = new (MachineRegistry_1.MachineRegistry.bind.apply(MachineRegistry_1.MachineRegistry, __spreadArray([void 0], machines, false)))();
48
49
  var requiresResourceLocking = machines.some(function (machine) { return machine.requiresResourceLocking; });
50
+ var representativeMachine = registry.machines[0];
51
+ var lastSeenVersions = [];
52
+ for (var _i = 0, _b = registry.machines; _i < _b.length; _i++) {
53
+ var machine = _b[_i];
54
+ if (representativeMachine.source !== machine.source) {
55
+ throw new errors_1.ConfigViolation("All the machines in the orchestrator must have type '".concat(representativeMachine.source, "'"));
56
+ }
57
+ if (lastSeenVersions.includes(machine.version)) {
58
+ throw new errors_1.ConfigViolation("An orchestrator must have unique machine versions. Machine ID:".concat(machine.id, " has duplicate version ").concat(machine.version, "."));
59
+ }
60
+ lastSeenVersions.push(machine.version);
61
+ }
49
62
  return new _1.ArvoOrchestrator({
50
63
  executionunits: executionunits,
51
64
  memory: memory,
@@ -1,13 +1,11 @@
1
- import { type ArvoContractRecord, type ArvoEvent, type OpenTelemetryHeaders } from 'arvo-core';
2
- import type ArvoMachine from '../ArvoMachine';
3
- import type { EnqueueArvoEventActionParam } from '../ArvoMachine/types';
1
+ import { type ArvoContractRecord, type ArvoEvent } from 'arvo-core';
2
+ import type IArvoEventHandler from '../IArvoEventHandler';
4
3
  import type { IMachineExectionEngine } from '../MachineExecutionEngine/interface';
5
4
  import type { IMachineMemory } from '../MachineMemory/interface';
6
5
  import type { IMachineRegistry } from '../MachineRegistry/interface';
7
- import type { ArvoOrchestratorParam, MachineMemoryRecord } from './types';
8
6
  import { SyncEventResource } from '../SyncEventResource';
9
- import IArvoEventHandler from '../IArvoEventHandler';
10
7
  import type { ArvoEventHandlerOpenTelemetryOptions } from '../types';
8
+ import type { ArvoOrchestratorParam, MachineMemoryRecord } from './types';
11
9
  /**
12
10
  * Orchestrates state machine execution and lifecycle management.
13
11
  * Handles machine resolution, state management, event processing and error handling.
@@ -28,20 +26,6 @@ export declare class ArvoOrchestrator implements IArvoEventHandler {
28
26
  * @throws Error if machines in registry have different sources
29
27
  */
30
28
  constructor({ executionunits, memory, registry, executionEngine, requiresResourceLocking, systemErrorDomain, }: ArvoOrchestratorParam);
31
- /**
32
- * Creates emittable event from execution result
33
- * @param event - Source event to emit
34
- * @param machine - Machine that generated event
35
- * @param otelHeaders - OpenTelemetry headers
36
- * @param orchestrationParentSubject - Parent orchestration subject
37
- * @param sourceEvent - Original triggering event
38
- * @param initEventId - The id of the event which initiated the orchestration in the first place
39
- * @param _domain - The domain of the event.
40
- *
41
- * @throws {ContractViolation} On schema/contract mismatch
42
- * @throws {ExecutionViolation} On invalid parentSubject$$ format
43
- */
44
- protected createEmittableEvent(event: EnqueueArvoEventActionParam, machine: ArvoMachine<any, any, any, any, any>, otelHeaders: OpenTelemetryHeaders, orchestrationParentSubject: string | null, sourceEvent: ArvoEvent, initEventId: string, _domain: string | null): ArvoEvent;
45
29
  /**
46
30
  * Core orchestration method that executes state machines in response to events.
47
31
  *