arvo-event-handler 3.0.7 → 3.0.10

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 (43) hide show
  1. package/dist/ArvoEventHandler/index.d.ts +3 -4
  2. package/dist/ArvoEventHandler/index.js +53 -71
  3. package/dist/ArvoEventHandler/types.d.ts +3 -2
  4. package/dist/ArvoMachine/createMachine.d.ts +1 -1
  5. package/dist/ArvoMachine/createMachine.js +1 -1
  6. package/dist/ArvoOrchestrationUtils/createEmitableEvent.d.ts +30 -0
  7. package/dist/ArvoOrchestrationUtils/createEmitableEvent.js +160 -0
  8. package/dist/ArvoOrchestrationUtils/error.d.ts +18 -0
  9. package/dist/{ArvoOrchestrator → ArvoOrchestrationUtils}/error.js +14 -9
  10. package/dist/ArvoOrchestrationUtils/handlerErrors.d.ts +36 -0
  11. package/dist/ArvoOrchestrationUtils/handlerErrors.js +183 -0
  12. package/dist/ArvoOrchestrationUtils/orchestrationExecutionState.d.ts +11 -0
  13. package/dist/ArvoOrchestrationUtils/orchestrationExecutionState.js +7 -0
  14. package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/acquireLockWithValidation.d.ts +8 -0
  15. package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/acquireLockWithValidation.js +69 -0
  16. package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/index.d.ts +42 -0
  17. package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/index.js +221 -0
  18. package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/validateAndParseSubject.d.ts +7 -0
  19. package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/validateAndParseSubject.js +25 -0
  20. package/dist/ArvoOrchestrationUtils/servicesValidation.d.ts +22 -0
  21. package/dist/ArvoOrchestrationUtils/servicesValidation.js +56 -0
  22. package/dist/ArvoOrchestrationUtils/types.d.ts +7 -0
  23. package/dist/ArvoOrchestrationUtils/types.js +9 -0
  24. package/dist/ArvoOrchestrator/factory.d.ts +2 -2
  25. package/dist/ArvoOrchestrator/factory.js +15 -1
  26. package/dist/ArvoOrchestrator/index.d.ts +5 -20
  27. package/dist/ArvoOrchestrator/index.js +94 -471
  28. package/dist/ArvoOrchestrator/types.d.ts +19 -21
  29. package/dist/ArvoResumable/factory.d.ts +4 -2
  30. package/dist/ArvoResumable/factory.js +6 -25
  31. package/dist/ArvoResumable/index.d.ts +9 -21
  32. package/dist/ArvoResumable/index.js +82 -422
  33. package/dist/ArvoResumable/types.d.ts +5 -4
  34. package/dist/IArvoEventHandler/index.d.ts +1 -2
  35. package/dist/MachineExecutionEngine/index.d.ts +1 -1
  36. package/dist/MachineRegistry/index.d.ts +1 -1
  37. package/dist/SyncEventResource/index.d.ts +1 -1
  38. package/dist/SyncEventResource/index.js +1 -1
  39. package/dist/index.d.ts +12 -11
  40. package/dist/index.js +24 -21
  41. package/dist/types.d.ts +8 -0
  42. package/package.json +2 -2
  43. package/dist/ArvoOrchestrator/error.d.ts +0 -16
@@ -1,7 +1,6 @@
1
- import { type SpanOptions } from '@opentelemetry/api';
2
1
  import { type ArvoContract, type ArvoEvent } from 'arvo-core';
3
- import IArvoEventHandler from '../IArvoEventHandler';
4
- import type { ArvoEventHandlerOpenTelemetryOptions } from '../types';
2
+ import type IArvoEventHandler from '../IArvoEventHandler';
3
+ import type { ArvoEventHandlerOpenTelemetryOptions, ArvoEventHandlerOtelSpanOptions } from '../types';
5
4
  import type { ArvoEventHandlerFunction, ArvoEventHandlerParam } from './types';
6
5
  /**
7
6
  * `ArvoEventHandler` is the foundational component for building stateless,
@@ -89,7 +88,7 @@ export default class ArvoEventHandler<TContract extends ArvoContract> implements
89
88
  /** Computational cost metric associated with event handling operations */
90
89
  readonly executionunits: number;
91
90
  /** OpenTelemetry configuration for event handling spans */
92
- readonly spanOptions: SpanOptions;
91
+ readonly spanOptions: ArvoEventHandlerOtelSpanOptions;
93
92
  /** Version-specific event handler implementation map */
94
93
  readonly handler: ArvoEventHandlerFunction<TContract>;
95
94
  /** The source identifier for events produced by this handler */
@@ -60,9 +60,11 @@ var __rest = (this && this.__rest) || function (s, e) {
60
60
  Object.defineProperty(exports, "__esModule", { value: true });
61
61
  var api_1 = require("@opentelemetry/api");
62
62
  var arvo_core_1 = require("arvo-core");
63
+ var ArvoDomain_1 = require("../ArvoDomain");
64
+ var handlerErrors_1 = require("../ArvoOrchestrationUtils/handlerErrors");
65
+ var orchestrationExecutionWrapper_1 = require("../ArvoOrchestrationUtils/orchestrationExecutionWrapper");
63
66
  var errors_1 = require("../errors");
64
67
  var utils_1 = require("../utils");
65
- var ArvoDomain_1 = require("../ArvoDomain");
66
68
  /**
67
69
  * `ArvoEventHandler` is the foundational component for building stateless,
68
70
  * contract-bound services in the Arvo system.
@@ -208,32 +210,32 @@ var ArvoEventHandler = /** @class */ (function () {
208
210
  * in handler configuration, or when contract URI mismatch occurs
209
211
  * @throws {ExecutionViolation} for explicitly handled runtime errors that should bubble up
210
212
  */
211
- ArvoEventHandler.prototype.execute = function (event_1) {
212
- return __awaiter(this, arguments, void 0, function (event, opentelemetry) {
213
+ ArvoEventHandler.prototype.execute = function (event, opentelemetry) {
214
+ return __awaiter(this, void 0, void 0, function () {
213
215
  var otelConfig;
214
216
  var _this = this;
215
- if (opentelemetry === void 0) { opentelemetry = {
216
- inheritFrom: 'EVENT',
217
- }; }
218
- return __generator(this, function (_a) {
219
- switch (_a.label) {
217
+ var _a, _b;
218
+ return __generator(this, function (_c) {
219
+ switch (_c.label) {
220
220
  case 0:
221
- otelConfig = (0, utils_1.createEventHandlerTelemetryConfig)("Handler<".concat(this.contract.uri, ">"), this.spanOptions, opentelemetry, event);
221
+ otelConfig = (0, utils_1.createEventHandlerTelemetryConfig)(((_b = (_a = this.spanOptions).spanName) === null || _b === void 0 ? void 0 : _b.call(_a, { selfContractUri: this.contract.uri, consumedEvent: event })) ||
222
+ "Handler<".concat(this.contract.uri, ">"), this.spanOptions, opentelemetry !== null && opentelemetry !== void 0 ? opentelemetry : { inheritFrom: 'EVENT' }, event);
222
223
  return [4 /*yield*/, arvo_core_1.ArvoOpenTelemetry.getInstance().startActiveSpan(__assign(__assign({}, otelConfig), { fn: function (span) { return __awaiter(_this, void 0, void 0, function () {
223
- var otelSpanHeaders, _i, _a, _b, key, value, parsedDataSchema, handlerContract_1, inputEventValidation, _handleOutput, outputs, result, _c, outputs_1, item, __extensions, handlerResult, domains, _d, _e, _dom, _f, _g, _h, key, value, error_1, result, _j, _k, _dom, _l, _m, _o, key, value;
224
- var _this = this;
225
- var _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
226
- return __generator(this, function (_z) {
227
- switch (_z.label) {
224
+ var otelSpanHeaders, _i, _a, _b, key, value, parsedDataSchema, handlerContract_1, inputEventValidation, _handleOutput, outputs, result, _c, outputs_1, item, __extensions, handlerResult, domains, _d, _e, _dom, _f, _g, _h, key, value, error_1, errorEvents, _j, _k, _l, errEvtIdx, errEvt, _m, _o, _p, key, value;
225
+ var _q, _r, _s, _t, _u, _v;
226
+ return __generator(this, function (_w) {
227
+ switch (_w.label) {
228
228
  case 0:
229
229
  otelSpanHeaders = (0, arvo_core_1.currentOpenTelemetryHeaders)();
230
- _z.label = 1;
230
+ _w.label = 1;
231
231
  case 1:
232
- _z.trys.push([1, 3, 4, 5]);
232
+ _w.trys.push([1, 3, 4, 5]);
233
+ span.setAttribute('arvo.handler.execution.status', 'normal');
234
+ span.setAttribute('arvo.handler.execution.type', 'handler');
233
235
  span.setStatus({ code: api_1.SpanStatusCode.OK });
234
236
  for (_i = 0, _a = Object.entries(event.otelAttributes); _i < _a.length; _i++) {
235
237
  _b = _a[_i], key = _b[0], value = _b[1];
236
- span.setAttribute("to_process.0.".concat(key), value);
238
+ span.setAttribute("consumable.0.".concat(key), value);
237
239
  }
238
240
  if (this.contract.type !== event.type) {
239
241
  throw new errors_1.ConfigViolation("Event type mismatch: Received '".concat(event.type, "', expected '").concat(this.contract.type, "'"));
@@ -256,9 +258,9 @@ var ArvoEventHandler = /** @class */ (function () {
256
258
  });
257
259
  }
258
260
  try {
259
- handlerContract_1 = this.contract.version((_p = parsedDataSchema === null || parsedDataSchema === void 0 ? void 0 : parsedDataSchema.version) !== null && _p !== void 0 ? _p : 'latest');
261
+ handlerContract_1 = this.contract.version((_q = parsedDataSchema === null || parsedDataSchema === void 0 ? void 0 : parsedDataSchema.version) !== null && _q !== void 0 ? _q : 'latest');
260
262
  }
261
- catch (_0) {
263
+ catch (_x) {
262
264
  throw new errors_1.ConfigViolation("Invalid contract version: ".concat(parsedDataSchema === null || parsedDataSchema === void 0 ? void 0 : parsedDataSchema.version, ". Available versions: ").concat(Object.keys(this.contract.versions).join(', ')));
263
265
  }
264
266
  (0, arvo_core_1.logToSpan)({
@@ -289,7 +291,7 @@ var ArvoEventHandler = /** @class */ (function () {
289
291
  spanHeaders: otelSpanHeaders,
290
292
  })];
291
293
  case 2:
292
- _handleOutput = _z.sent();
294
+ _handleOutput = _w.sent();
293
295
  if (!_handleOutput)
294
296
  return [2 /*return*/, {
295
297
  events: [],
@@ -306,85 +308,65 @@ var ArvoEventHandler = /** @class */ (function () {
306
308
  item = outputs_1[_c];
307
309
  try {
308
310
  __extensions = item.__extensions, handlerResult = __rest(item, ["__extensions"]);
309
- domains = (_r = (_q = handlerResult.domain) === null || _q === void 0 ? void 0 : _q.map(function (item) {
311
+ domains = (_s = (_r = handlerResult.domain) === null || _r === void 0 ? void 0 : _r.map(function (item) {
310
312
  return (0, ArvoDomain_1.resolveEventDomain)({
311
313
  domainToResolve: item,
312
314
  handlerSelfContract: handlerContract_1,
313
315
  eventContract: handlerContract_1,
314
316
  triggeringEvent: event,
315
317
  });
316
- })) !== null && _r !== void 0 ? _r : [null];
318
+ })) !== null && _s !== void 0 ? _s : [null];
317
319
  for (_d = 0, _e = Array.from(new Set(domains)); _d < _e.length; _d++) {
318
320
  _dom = _e[_d];
319
321
  result.push((0, arvo_core_1.createArvoEventFactory)(handlerContract_1).emits(__assign(__assign({}, handlerResult), { traceparent: otelSpanHeaders.traceparent || undefined, tracestate: otelSpanHeaders.tracestate || undefined, source: this.source, subject: event.subject,
320
322
  // 'source'
321
323
  // prioritise returned 'to', 'redirectto' and then
322
- to: (0, utils_1.coalesceOrDefault)([handlerResult.to, event.redirectto], event.source), executionunits: (0, utils_1.coalesce)(handlerResult.executionunits, this.executionunits), accesscontrol: (_t = (_s = handlerResult.accesscontrol) !== null && _s !== void 0 ? _s : event.accesscontrol) !== null && _t !== void 0 ? _t : undefined, parentid: event.id, domain: _dom }), __extensions));
324
+ to: (0, utils_1.coalesceOrDefault)([handlerResult.to, event.redirectto], event.source), executionunits: (0, utils_1.coalesce)(handlerResult.executionunits, this.executionunits), accesscontrol: (_u = (_t = handlerResult.accesscontrol) !== null && _t !== void 0 ? _t : event.accesscontrol) !== null && _u !== void 0 ? _u : undefined, parentid: event.id, domain: _dom }), __extensions));
323
325
  for (_f = 0, _g = Object.entries(result[result.length - 1].otelAttributes); _f < _g.length; _f++) {
324
326
  _h = _g[_f], key = _h[0], value = _h[1];
325
- span.setAttribute("to_emit.".concat(result.length - 1, ".").concat(key), value);
327
+ span.setAttribute("emittables.".concat(result.length - 1, ".").concat(key), value);
326
328
  }
327
329
  }
328
330
  }
329
331
  catch (e) {
330
- throw new errors_1.ContractViolation((_u = e === null || e === void 0 ? void 0 : e.message) !== null && _u !== void 0 ? _u : 'Invalid data');
332
+ throw new errors_1.ContractViolation((_v = e === null || e === void 0 ? void 0 : e.message) !== null && _v !== void 0 ? _v : 'Invalid data');
331
333
  }
332
334
  }
333
- (0, arvo_core_1.logToSpan)({
334
- level: 'INFO',
335
- message: "Event processing completed successfully. Generated ".concat(result.length, " event(s)"),
336
- });
337
- (0, arvo_core_1.logToSpan)({
338
- level: 'INFO',
339
- message: 'Event handled successfully',
340
- });
341
- return [2 /*return*/, {
342
- events: result,
343
- }];
335
+ return [2 /*return*/, (0, orchestrationExecutionWrapper_1.returnEventsWithLogging)({ events: result }, span)];
344
336
  case 3:
345
- error_1 = _z.sent();
337
+ error_1 = _w.sent();
338
+ span.setAttribute('arvo.handler.execution.status', 'failure');
346
339
  (0, arvo_core_1.exceptionToSpan)(error_1);
347
340
  span.setStatus({
348
341
  code: api_1.SpanStatusCode.ERROR,
349
342
  message: "Event processing failed: ".concat(error_1.message),
350
343
  });
351
- if (error_1.name.includes('ViolationError')) {
344
+ if ((0, arvo_core_1.isViolationError)(error_1)) {
352
345
  throw error_1;
353
346
  }
354
- result = [];
355
- for (_j = 0, _k = Array.from(new Set(this.systemErrorDomain
356
- ? this.systemErrorDomain.map(function (item) {
357
- return (0, ArvoDomain_1.resolveEventDomain)({
358
- domainToResolve: item,
359
- handlerSelfContract: _this.contract.version('latest'),
360
- eventContract: _this.contract.version('latest'),
361
- triggeringEvent: event,
362
- });
363
- })
364
- : [event.domain, this.domain, null])); _j < _k.length; _j++) {
365
- _dom = _k[_j];
366
- result.push((0, arvo_core_1.createArvoEventFactory)(this.contract.version('latest')).systemError({
367
- source: this.source,
368
- subject: event.subject,
369
- // The system error must always got back to
370
- // the source
371
- to: event.source,
372
- error: error_1,
373
- executionunits: this.executionunits,
374
- traceparent: (_v = otelSpanHeaders.traceparent) !== null && _v !== void 0 ? _v : undefined,
375
- tracestate: (_w = otelSpanHeaders.tracestate) !== null && _w !== void 0 ? _w : undefined,
376
- accesscontrol: (_x = event.accesscontrol) !== null && _x !== void 0 ? _x : undefined,
377
- parentid: (_y = event.id) !== null && _y !== void 0 ? _y : undefined,
378
- domain: _dom,
379
- }));
380
- for (_l = 0, _m = Object.entries(result[result.length - 1].otelAttributes); _l < _m.length; _l++) {
381
- _o = _m[_l], key = _o[0], value = _o[1];
382
- span.setAttribute("to_emit.".concat(result.length - 1, ".").concat(key), value);
347
+ errorEvents = (0, handlerErrors_1.createSystemErrorEvents)({
348
+ error: error_1,
349
+ event: event,
350
+ otelHeaders: otelSpanHeaders,
351
+ orchestrationParentSubject: null,
352
+ initEventId: event.id,
353
+ selfContract: this.contract.version('any'),
354
+ systemErrorDomain: undefined,
355
+ executionunits: this.executionunits,
356
+ source: this.source,
357
+ domain: this.domain,
358
+ handlerType: 'handler',
359
+ });
360
+ for (_j = 0, _k = Object.entries(errorEvents); _j < _k.length; _j++) {
361
+ _l = _k[_j], errEvtIdx = _l[0], errEvt = _l[1];
362
+ for (_m = 0, _o = Object.entries(errEvt.otelAttributes); _m < _o.length; _m++) {
363
+ _p = _o[_m], key = _p[0], value = _p[1];
364
+ span.setAttribute("emittables.".concat(errEvtIdx, ".").concat(key), value);
383
365
  }
384
366
  }
385
- return [2 /*return*/, {
386
- events: result,
387
- }];
367
+ return [2 /*return*/, (0, orchestrationExecutionWrapper_1.returnEventsWithLogging)({
368
+ events: errorEvents,
369
+ }, span)];
388
370
  case 4:
389
371
  span.end();
390
372
  return [7 /*endfinally*/];
@@ -392,7 +374,7 @@ var ArvoEventHandler = /** @class */ (function () {
392
374
  }
393
375
  });
394
376
  }); } }))];
395
- case 1: return [2 /*return*/, _a.sent()];
377
+ case 1: return [2 /*return*/, _c.sent()];
396
378
  }
397
379
  });
398
380
  });
@@ -1,6 +1,7 @@
1
- import type { Span, SpanOptions } from '@opentelemetry/api';
1
+ import type { Span } from '@opentelemetry/api';
2
2
  import type { ArvoContract, ArvoEvent, ArvoSemanticVersion, CreateArvoEvent, InferArvoEvent, OpenTelemetryHeaders, VersionedArvoContract } from 'arvo-core';
3
3
  import type { z } from 'zod';
4
+ import type { ArvoEventHandlerOtelSpanOptions } from '../types';
4
5
  /**
5
6
  * Represents the input for an ArvoEvent handler function.
6
7
  */
@@ -90,7 +91,7 @@ export type ArvoEventHandlerParam<TContract extends ArvoContract> = {
90
91
  /**
91
92
  * The OpenTelemetry span options
92
93
  */
93
- spanOptions?: SpanOptions;
94
+ spanOptions?: ArvoEventHandlerOtelSpanOptions;
94
95
  /**
95
96
  * Optional configuration to customize where system error events are emitted.
96
97
  *
@@ -1,4 +1,4 @@
1
- import { type ArvoOrchestratorEventTypeGen, CreateArvoEvent, type InferVersionedArvoContract, type VersionedArvoContract } from 'arvo-core';
1
+ import { type ArvoOrchestratorEventTypeGen, type CreateArvoEvent, type InferVersionedArvoContract, type VersionedArvoContract } from 'arvo-core';
2
2
  import { type ActionFunction, type MachineConfig, type MachineContext, type MetaObject, type ParameterizedObject, type SetupTypes } from 'xstate';
3
3
  import type { z } from 'zod';
4
4
  import ArvoMachine from '.';
@@ -25,11 +25,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.setupArvoMachine = setupArvoMachine;
27
27
  var arvo_core_1 = require("arvo-core");
28
+ var uuid_1 = require("uuid");
28
29
  var xstate_1 = require("xstate");
29
30
  var _1 = __importDefault(require("."));
30
31
  var object_1 = require("../utils/object");
31
32
  var utils_1 = require("./utils");
32
- var uuid_1 = require("uuid");
33
33
  /**
34
34
  * Establishes the foundation for creating Arvo-compatible state machines.
35
35
  *
@@ -0,0 +1,30 @@
1
+ import type { Span } from '@opentelemetry/api';
2
+ import { type ArvoEvent, type ArvoOrchestratorContract, type ArvoSemanticVersion, type OpenTelemetryHeaders, type VersionedArvoContract } from 'arvo-core';
3
+ import type { EnqueueArvoEventActionParam } from '../ArvoMachine/types';
4
+ export type CreateEmittableEventParams = {
5
+ event: EnqueueArvoEventActionParam;
6
+ otelHeaders: OpenTelemetryHeaders;
7
+ orchestrationParentSubject: string | null;
8
+ sourceEvent: ArvoEvent;
9
+ selfContract: VersionedArvoContract<ArvoOrchestratorContract, ArvoSemanticVersion>;
10
+ serviceContracts: Record<string, VersionedArvoContract<any, any>>;
11
+ initEventId: string;
12
+ domain: string | null;
13
+ executionunits: number;
14
+ source: string;
15
+ };
16
+ export declare const createEmittableEvent: ({ event, otelHeaders, orchestrationParentSubject, sourceEvent, selfContract, serviceContracts, initEventId, domain: _domain, executionunits, source, }: CreateEmittableEventParams, span: Span) => ArvoEvent;
17
+ /**
18
+ * Processes raw events into emittable events with domain resolution
19
+ */
20
+ export declare const processRawEventsIntoEmittables: (params: {
21
+ rawEvents: EnqueueArvoEventActionParam[];
22
+ otelHeaders: OpenTelemetryHeaders;
23
+ orchestrationParentSubject: string | null;
24
+ sourceEvent: ArvoEvent;
25
+ selfContract: VersionedArvoContract<ArvoOrchestratorContract, ArvoSemanticVersion>;
26
+ serviceContracts: Record<string, VersionedArvoContract<any, any>>;
27
+ initEventId: string;
28
+ executionunits: number;
29
+ source: string;
30
+ }, span: Span) => ArvoEvent[];
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.processRawEventsIntoEmittables = exports.createEmittableEvent = void 0;
4
+ var arvo_core_1 = require("arvo-core");
5
+ var ArvoDomain_1 = require("../ArvoDomain");
6
+ var errors_1 = require("../errors");
7
+ var createEmittableEvent = function (_a, span) {
8
+ var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
9
+ var event = _a.event, otelHeaders = _a.otelHeaders, orchestrationParentSubject = _a.orchestrationParentSubject, sourceEvent = _a.sourceEvent, selfContract = _a.selfContract, serviceContracts = _a.serviceContracts, initEventId = _a.initEventId, _domain = _a.domain, executionunits = _a.executionunits, source = _a.source;
10
+ (0, arvo_core_1.logToSpan)({
11
+ level: 'INFO',
12
+ message: "Creating emittable event: ".concat(event.type),
13
+ }, span);
14
+ var serviceContractMap = Object.fromEntries(Object.values(serviceContracts).map(function (item) { return [
15
+ item.accepts.type,
16
+ item,
17
+ ]; }));
18
+ var schema = null;
19
+ var contract = null;
20
+ var subject = sourceEvent.subject;
21
+ var parentId = sourceEvent.id;
22
+ var domain = (0, ArvoDomain_1.resolveEventDomain)({
23
+ domainToResolve: _domain,
24
+ handlerSelfContract: selfContract,
25
+ eventContract: null,
26
+ triggeringEvent: sourceEvent,
27
+ });
28
+ if (event.type === selfContract.metadata.completeEventType) {
29
+ (0, arvo_core_1.logToSpan)({
30
+ level: 'INFO',
31
+ message: "Creating event for workflow completion: ".concat(event.type),
32
+ }, span);
33
+ contract = selfContract;
34
+ schema = selfContract.emits[selfContract.metadata.completeEventType];
35
+ subject = orchestrationParentSubject !== null && orchestrationParentSubject !== void 0 ? orchestrationParentSubject : sourceEvent.subject;
36
+ parentId = initEventId;
37
+ domain = (0, ArvoDomain_1.resolveEventDomain)({
38
+ domainToResolve: _domain,
39
+ handlerSelfContract: selfContract,
40
+ eventContract: selfContract,
41
+ triggeringEvent: sourceEvent,
42
+ });
43
+ }
44
+ else if (serviceContractMap[event.type]) {
45
+ (0, arvo_core_1.logToSpan)({
46
+ level: 'INFO',
47
+ message: "Creating service event for external system: ".concat(event.type),
48
+ }, span);
49
+ contract = serviceContractMap[event.type];
50
+ schema = serviceContractMap[event.type].accepts.schema;
51
+ domain = (0, ArvoDomain_1.resolveEventDomain)({
52
+ domainToResolve: _domain,
53
+ handlerSelfContract: selfContract,
54
+ eventContract: contract,
55
+ triggeringEvent: sourceEvent,
56
+ });
57
+ if (contract.metadata.contractType === 'ArvoOrchestratorContract') {
58
+ if (event.data.parentSubject$$) {
59
+ try {
60
+ arvo_core_1.ArvoOrchestrationSubject.parse(event.data.parentSubject$$);
61
+ }
62
+ catch (_o) {
63
+ throw new errors_1.ExecutionViolation("Invalid parentSubject$$ for the event(type='".concat(event.type, "', uri='").concat((_b = event.dataschema) !== null && _b !== void 0 ? _b : arvo_core_1.EventDataschemaUtil.create(contract), "'). It must be follow the ArvoOrchestrationSubject schema. The easiest way is to use the current orchestration subject by storing the subject via the context block in the machine definition."));
64
+ }
65
+ }
66
+ try {
67
+ if (event.data.parentSubject$$) {
68
+ subject = arvo_core_1.ArvoOrchestrationSubject.from({
69
+ orchestator: contract.accepts.type,
70
+ version: contract.version,
71
+ subject: event.data.parentSubject$$,
72
+ domain: domain !== null && domain !== void 0 ? domain : null,
73
+ meta: {
74
+ redirectto: (_c = event.redirectto) !== null && _c !== void 0 ? _c : source,
75
+ },
76
+ });
77
+ }
78
+ else {
79
+ subject = arvo_core_1.ArvoOrchestrationSubject.new({
80
+ version: contract.version,
81
+ orchestator: contract.accepts.type,
82
+ initiator: source,
83
+ domain: domain !== null && domain !== void 0 ? domain : undefined,
84
+ meta: {
85
+ redirectto: (_d = event.redirectto) !== null && _d !== void 0 ? _d : source,
86
+ },
87
+ });
88
+ }
89
+ }
90
+ catch (error) {
91
+ throw new errors_1.ExecutionViolation("Orchestration subject creation failed due to invalid parameters - Event: ".concat(event.type, " - ").concat(error === null || error === void 0 ? void 0 : error.message));
92
+ }
93
+ }
94
+ }
95
+ var finalDataschema = event.dataschema;
96
+ var finalData = event.data;
97
+ if (contract && schema) {
98
+ try {
99
+ finalData = schema.parse(event.data);
100
+ finalDataschema = arvo_core_1.EventDataschemaUtil.create(contract);
101
+ }
102
+ catch (error) {
103
+ throw new errors_1.ContractViolation("Invalid event data: Schema validation failed.\nEvent type: ".concat(event.type, "\nDetails: ").concat(error.message));
104
+ }
105
+ }
106
+ var emittableEvent = (0, arvo_core_1.createArvoEvent)({
107
+ id: event.id,
108
+ source: source,
109
+ type: event.type,
110
+ subject: subject,
111
+ dataschema: finalDataschema !== null && finalDataschema !== void 0 ? finalDataschema : undefined,
112
+ data: finalData,
113
+ to: (_e = event.to) !== null && _e !== void 0 ? _e : event.type,
114
+ accesscontrol: (_g = (_f = event.accesscontrol) !== null && _f !== void 0 ? _f : sourceEvent.accesscontrol) !== null && _g !== void 0 ? _g : undefined,
115
+ redirectto: (_h = event.redirectto) !== null && _h !== void 0 ? _h : source,
116
+ executionunits: (_j = event.executionunits) !== null && _j !== void 0 ? _j : executionunits,
117
+ traceparent: (_k = otelHeaders.traceparent) !== null && _k !== void 0 ? _k : undefined,
118
+ tracestate: (_l = otelHeaders.tracestate) !== null && _l !== void 0 ? _l : undefined,
119
+ parentid: parentId,
120
+ domain: domain !== null && domain !== void 0 ? domain : undefined,
121
+ }, (_m = event.__extensions) !== null && _m !== void 0 ? _m : {});
122
+ (0, arvo_core_1.logToSpan)({
123
+ level: 'INFO',
124
+ message: "Event created successfully: ".concat(emittableEvent.type),
125
+ }, span);
126
+ return emittableEvent;
127
+ };
128
+ exports.createEmittableEvent = createEmittableEvent;
129
+ /**
130
+ * Processes raw events into emittable events with domain resolution
131
+ */
132
+ var processRawEventsIntoEmittables = function (params, span) {
133
+ var _a;
134
+ var emittables = [];
135
+ for (var _i = 0, _b = params.rawEvents; _i < _b.length; _i++) {
136
+ var item = _b[_i];
137
+ for (var _c = 0, _d = Array.from(new Set((_a = item.domain) !== null && _a !== void 0 ? _a : [null])); _c < _d.length; _c++) {
138
+ var _dom = _d[_c];
139
+ var evt = (0, exports.createEmittableEvent)({
140
+ event: item,
141
+ otelHeaders: params.otelHeaders,
142
+ orchestrationParentSubject: params.orchestrationParentSubject,
143
+ sourceEvent: params.sourceEvent,
144
+ selfContract: params.selfContract,
145
+ serviceContracts: params.serviceContracts,
146
+ initEventId: params.initEventId,
147
+ domain: _dom,
148
+ executionunits: params.executionunits,
149
+ source: params.source,
150
+ }, span);
151
+ emittables.push(evt);
152
+ for (var _e = 0, _f = Object.entries(emittables[emittables.length - 1].otelAttributes); _e < _f.length; _e++) {
153
+ var _g = _f[_e], key = _g[0], value = _g[1];
154
+ span.setAttribute("emittables.".concat(emittables.length - 1, ".").concat(key), value);
155
+ }
156
+ }
157
+ }
158
+ return emittables;
159
+ };
160
+ exports.processRawEventsIntoEmittables = processRawEventsIntoEmittables;
@@ -0,0 +1,18 @@
1
+ import { type ArvoEvent, ViolationError } from 'arvo-core';
2
+ export declare const TransactionViolationCause: {
3
+ readonly READ_FAILURE: "READ_MACHINE_MEMORY_FAILURE";
4
+ readonly LOCK_FAILURE: "LOCK_MACHINE_MEMORY_FAILURE";
5
+ readonly WRITE_FAILURE: "WRITE_MACHINE_MEMORY_FAILURE";
6
+ readonly LOCK_UNACQUIRED: "LOCK_UNACQUIRED";
7
+ readonly INVALID_SUBJECT: "INVALID_SUBJECT";
8
+ };
9
+ export type TransactionViolationCauseType = (typeof TransactionViolationCause)[keyof typeof TransactionViolationCause];
10
+ export declare class TransactionViolation extends ViolationError<'OrchestratorTransaction'> {
11
+ readonly cause: TransactionViolationCauseType;
12
+ constructor(param: {
13
+ cause: TransactionViolationCauseType;
14
+ message: string;
15
+ initiatingEvent: ArvoEvent;
16
+ });
17
+ }
18
+ export declare const isTransactionViolationError: (error: unknown, cause?: TransactionViolationCauseType) => boolean;
@@ -15,16 +15,15 @@ var __extends = (this && this.__extends) || (function () {
15
15
  };
16
16
  })();
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.TransactionViolation = exports.TransactionViolationCause = void 0;
18
+ exports.isTransactionViolationError = exports.TransactionViolation = exports.TransactionViolationCause = void 0;
19
19
  var arvo_core_1 = require("arvo-core");
20
- var TransactionViolationCause;
21
- (function (TransactionViolationCause) {
22
- TransactionViolationCause["READ_FAILURE"] = "READ_MACHINE_MEMORY_FAILURE";
23
- TransactionViolationCause["LOCK_FAILURE"] = "LOCK_MACHINE_MEMORY_FAILURE";
24
- TransactionViolationCause["WRITE_FAILURE"] = "WRITE_MACHINE_MEMORY_FAILURE";
25
- TransactionViolationCause["LOCK_UNACQUIRED"] = "LOCK_UNACQUIRED";
26
- TransactionViolationCause["INVALID_SUBJECT"] = "INVALID_SUBJECT";
27
- })(TransactionViolationCause || (exports.TransactionViolationCause = TransactionViolationCause = {}));
20
+ exports.TransactionViolationCause = {
21
+ READ_FAILURE: 'READ_MACHINE_MEMORY_FAILURE',
22
+ LOCK_FAILURE: 'LOCK_MACHINE_MEMORY_FAILURE',
23
+ WRITE_FAILURE: 'WRITE_MACHINE_MEMORY_FAILURE',
24
+ LOCK_UNACQUIRED: 'LOCK_UNACQUIRED',
25
+ INVALID_SUBJECT: 'INVALID_SUBJECT',
26
+ };
28
27
  var TransactionViolation = /** @class */ (function (_super) {
29
28
  __extends(TransactionViolation, _super);
30
29
  function TransactionViolation(param) {
@@ -41,3 +40,9 @@ var TransactionViolation = /** @class */ (function (_super) {
41
40
  return TransactionViolation;
42
41
  }(arvo_core_1.ViolationError));
43
42
  exports.TransactionViolation = TransactionViolation;
43
+ var isTransactionViolationError = function (error, cause) {
44
+ return ((0, arvo_core_1.isViolationError)(error) &&
45
+ error.type === 'OrchestratorTransaction' &&
46
+ (cause ? error.cause === cause : true));
47
+ };
48
+ exports.isTransactionViolationError = isTransactionViolationError;
@@ -0,0 +1,36 @@
1
+ import { type Span } from '@opentelemetry/api';
2
+ import { type ArvoEvent, type ArvoOrchestratorContract, type ArvoSemanticVersion, type OpenTelemetryHeaders, type VersionedArvoContract, type ViolationError } from 'arvo-core';
3
+ import type { SyncEventResource } from '../SyncEventResource';
4
+ import type { OrchestrationExecutionMemoryRecord } from './orchestrationExecutionState';
5
+ import { type ArvoOrchestrationHandlerType } from './types';
6
+ /**
7
+ * Parameters for system error event creation
8
+ */
9
+ export type CreateSystemErrorEventsParams = {
10
+ error: unknown;
11
+ event: ArvoEvent;
12
+ otelHeaders: OpenTelemetryHeaders;
13
+ orchestrationParentSubject: string | null;
14
+ initEventId: string | null;
15
+ selfContract: VersionedArvoContract<ArvoOrchestratorContract, ArvoSemanticVersion>;
16
+ systemErrorDomain?: (string | null)[];
17
+ executionunits: number;
18
+ source: string;
19
+ domain: string | null;
20
+ handlerType: ArvoOrchestrationHandlerType;
21
+ };
22
+ /**
23
+ * Creates system error events
24
+ */
25
+ export declare const createSystemErrorEvents: ({ error, event, otelHeaders, orchestrationParentSubject, initEventId, selfContract, systemErrorDomain, executionunits, source, domain, handlerType, }: CreateSystemErrorEventsParams & {
26
+ error: Error;
27
+ }) => ArvoEvent[];
28
+ export declare const handleOrchestrationErrors: (_handlerType: ArvoOrchestrationHandlerType, param: CreateSystemErrorEventsParams & {
29
+ syncEventResource: SyncEventResource<OrchestrationExecutionMemoryRecord<Record<string, any>>>;
30
+ }, span: Span) => Promise<{
31
+ errorToThrow: ViolationError;
32
+ events: null;
33
+ } | {
34
+ errorToThrow: null;
35
+ events: ArvoEvent[];
36
+ }>;