@cadenza.io/core 1.8.0 → 1.9.0

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.
package/dist/index.d.mts CHANGED
@@ -119,13 +119,13 @@ declare abstract class SignalEmitter {
119
119
  * @param data Optional payload (defaults to empty object).
120
120
  * @edge No emission if silent; for metrics in silent mode, consider override or separate method.
121
121
  */
122
- emit(signal: string, data?: any): void;
122
+ protected emit(signal: string, data?: AnyObject): void;
123
123
  /**
124
124
  * Emits a signal via the broker even if silent.
125
125
  * @param signal The signal name.
126
126
  * @param data Optional payload (defaults to empty object).
127
127
  */
128
- emitMetric(signal: string, data?: any): void;
128
+ protected emitMetric(signal: string, data?: AnyObject): void;
129
129
  }
130
130
 
131
131
  declare abstract class ExecutionChain {
@@ -228,6 +228,7 @@ declare class GraphNode extends SignalEmitter implements Graph {
228
228
  private workAsync;
229
229
  private executeAsync;
230
230
  private work;
231
+ protected emitWithMetadata(signal: string, context: AnyObject): void;
231
232
  private onProgress;
232
233
  private postProcess;
233
234
  private onError;
@@ -315,7 +316,8 @@ declare class TaskIterator implements Iterator {
315
316
  }
316
317
 
317
318
  declare class SignalParticipant extends SignalEmitter {
318
- protected signalsToEmit: Set<string>;
319
+ protected emitsSignals: Set<string>;
320
+ protected signalsToEmitAfter: Set<string>;
319
321
  protected signalsToEmitOnFail: Set<string>;
320
322
  protected observedSignals: Set<string>;
321
323
  /**
@@ -330,7 +332,7 @@ declare class SignalParticipant extends SignalEmitter {
330
332
  * @param signals The signal names.
331
333
  * @returns This for chaining.
332
334
  */
333
- emits(...signals: string[]): this;
335
+ emitsAfter(...signals: string[]): this;
334
336
  emitsOnFail(...signals: string[]): this;
335
337
  /**
336
338
  * Unsubscribes from all observed signals.
@@ -355,6 +357,8 @@ declare class SignalParticipant extends SignalEmitter {
355
357
  * @returns This for chaining.
356
358
  */
357
359
  detachAllSignals(): this;
360
+ mapSignals(callback: (signal: string) => void): void[];
361
+ mapOnFailSignals(callback: (signal: string) => void): void[];
358
362
  /**
359
363
  * Emits attached signals.
360
364
  * @param context The context for emission.
@@ -397,7 +401,7 @@ type SchemaDefinition = {
397
401
  strict?: boolean;
398
402
  };
399
403
 
400
- type TaskFunction = (context: AnyObject, progressCallback: (progress: number) => void) => TaskResult;
404
+ type TaskFunction = (context: AnyObject, emit: (signal: string, context: AnyObject) => void, progressCallback: (progress: number) => void) => TaskResult;
401
405
  type TaskResult = boolean | AnyObject | Generator | Promise<any> | void;
402
406
  type ThrottleTagGetter = (context?: AnyObject, task?: Task) => string;
403
407
  declare class Task extends SignalParticipant implements Graph {
@@ -475,12 +479,13 @@ declare class Task extends SignalParticipant implements Graph {
475
479
  /**
476
480
  * Executes the task function after optional input validation.
477
481
  * @param context - The GraphContext to validate and execute.
482
+ * @param emit
478
483
  * @param progressCallback - Callback for progress updates.
479
484
  * @returns TaskResult from the taskFunction or error object on validation failure.
480
485
  * @edge If validateInputContext is true, validates context; on failure, emits 'meta.task.validationFailed' with detailed errors.
481
486
  * @edge If validateOutputContext is true, validates output; on failure, emits 'meta.task.outputValidationFailed' with detailed errors.
482
487
  */
483
- execute(context: GraphContext, progressCallback: (progress: number) => void): TaskResult;
488
+ execute(context: GraphContext, emit: (signal: string, context: AnyObject) => void, progressCallback: (progress: number) => void): TaskResult;
484
489
  doAfter(...tasks: Task[]): this;
485
490
  then(...tasks: Task[]): this;
486
491
  decouple(task: Task): void;
@@ -723,10 +728,11 @@ declare class DebounceTask extends Task {
723
728
  private lastContext;
724
729
  private lastTimeout;
725
730
  private lastProgressCallback;
731
+ private lastEmitFunction;
726
732
  constructor(name: string, task: TaskFunction, description?: string, debounceTime?: number, leading?: boolean, trailing?: boolean, maxWait?: number, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: boolean, inputSchema?: SchemaDefinition | undefined, validateInputSchema?: boolean, outputSchema?: SchemaDefinition | undefined, validateOutputSchema?: boolean);
727
733
  private executeFunction;
728
734
  private debouncedTrigger;
729
- execute(context: GraphContext, progressCallback: (progress: number) => void): TaskResult;
735
+ execute(context: GraphContext, emit: (signal: string, context: any) => void, progressCallback: (progress: number) => void): TaskResult;
730
736
  }
731
737
 
732
738
  declare class EphemeralTask extends Task {
@@ -734,7 +740,7 @@ declare class EphemeralTask extends Task {
734
740
  private readonly condition;
735
741
  readonly isEphemeral: boolean;
736
742
  constructor(name: string, task: TaskFunction, description?: string, once?: boolean, condition?: (context: any) => boolean, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: boolean, getTagCallback?: ThrottleTagGetter | undefined, inputSchema?: SchemaDefinition | undefined, validateInputContext?: boolean, outputSchema?: SchemaDefinition | undefined, validateOutputContext?: boolean, retryCount?: number, retryDelay?: number, retryDelayMax?: number, retryDelayFactor?: number);
737
- execute(context: any, progressCallback: (progress: number) => void): TaskResult;
743
+ execute(context: any, emit: (signal: string, context: AnyObject) => void, progressCallback: (progress: number) => void): TaskResult;
738
744
  }
739
745
 
740
746
  declare class GraphAsyncRun extends GraphRunStrategy {
package/dist/index.d.ts CHANGED
@@ -119,13 +119,13 @@ declare abstract class SignalEmitter {
119
119
  * @param data Optional payload (defaults to empty object).
120
120
  * @edge No emission if silent; for metrics in silent mode, consider override or separate method.
121
121
  */
122
- emit(signal: string, data?: any): void;
122
+ protected emit(signal: string, data?: AnyObject): void;
123
123
  /**
124
124
  * Emits a signal via the broker even if silent.
125
125
  * @param signal The signal name.
126
126
  * @param data Optional payload (defaults to empty object).
127
127
  */
128
- emitMetric(signal: string, data?: any): void;
128
+ protected emitMetric(signal: string, data?: AnyObject): void;
129
129
  }
130
130
 
131
131
  declare abstract class ExecutionChain {
@@ -228,6 +228,7 @@ declare class GraphNode extends SignalEmitter implements Graph {
228
228
  private workAsync;
229
229
  private executeAsync;
230
230
  private work;
231
+ protected emitWithMetadata(signal: string, context: AnyObject): void;
231
232
  private onProgress;
232
233
  private postProcess;
233
234
  private onError;
@@ -315,7 +316,8 @@ declare class TaskIterator implements Iterator {
315
316
  }
316
317
 
317
318
  declare class SignalParticipant extends SignalEmitter {
318
- protected signalsToEmit: Set<string>;
319
+ protected emitsSignals: Set<string>;
320
+ protected signalsToEmitAfter: Set<string>;
319
321
  protected signalsToEmitOnFail: Set<string>;
320
322
  protected observedSignals: Set<string>;
321
323
  /**
@@ -330,7 +332,7 @@ declare class SignalParticipant extends SignalEmitter {
330
332
  * @param signals The signal names.
331
333
  * @returns This for chaining.
332
334
  */
333
- emits(...signals: string[]): this;
335
+ emitsAfter(...signals: string[]): this;
334
336
  emitsOnFail(...signals: string[]): this;
335
337
  /**
336
338
  * Unsubscribes from all observed signals.
@@ -355,6 +357,8 @@ declare class SignalParticipant extends SignalEmitter {
355
357
  * @returns This for chaining.
356
358
  */
357
359
  detachAllSignals(): this;
360
+ mapSignals(callback: (signal: string) => void): void[];
361
+ mapOnFailSignals(callback: (signal: string) => void): void[];
358
362
  /**
359
363
  * Emits attached signals.
360
364
  * @param context The context for emission.
@@ -397,7 +401,7 @@ type SchemaDefinition = {
397
401
  strict?: boolean;
398
402
  };
399
403
 
400
- type TaskFunction = (context: AnyObject, progressCallback: (progress: number) => void) => TaskResult;
404
+ type TaskFunction = (context: AnyObject, emit: (signal: string, context: AnyObject) => void, progressCallback: (progress: number) => void) => TaskResult;
401
405
  type TaskResult = boolean | AnyObject | Generator | Promise<any> | void;
402
406
  type ThrottleTagGetter = (context?: AnyObject, task?: Task) => string;
403
407
  declare class Task extends SignalParticipant implements Graph {
@@ -475,12 +479,13 @@ declare class Task extends SignalParticipant implements Graph {
475
479
  /**
476
480
  * Executes the task function after optional input validation.
477
481
  * @param context - The GraphContext to validate and execute.
482
+ * @param emit
478
483
  * @param progressCallback - Callback for progress updates.
479
484
  * @returns TaskResult from the taskFunction or error object on validation failure.
480
485
  * @edge If validateInputContext is true, validates context; on failure, emits 'meta.task.validationFailed' with detailed errors.
481
486
  * @edge If validateOutputContext is true, validates output; on failure, emits 'meta.task.outputValidationFailed' with detailed errors.
482
487
  */
483
- execute(context: GraphContext, progressCallback: (progress: number) => void): TaskResult;
488
+ execute(context: GraphContext, emit: (signal: string, context: AnyObject) => void, progressCallback: (progress: number) => void): TaskResult;
484
489
  doAfter(...tasks: Task[]): this;
485
490
  then(...tasks: Task[]): this;
486
491
  decouple(task: Task): void;
@@ -723,10 +728,11 @@ declare class DebounceTask extends Task {
723
728
  private lastContext;
724
729
  private lastTimeout;
725
730
  private lastProgressCallback;
731
+ private lastEmitFunction;
726
732
  constructor(name: string, task: TaskFunction, description?: string, debounceTime?: number, leading?: boolean, trailing?: boolean, maxWait?: number, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: boolean, inputSchema?: SchemaDefinition | undefined, validateInputSchema?: boolean, outputSchema?: SchemaDefinition | undefined, validateOutputSchema?: boolean);
727
733
  private executeFunction;
728
734
  private debouncedTrigger;
729
- execute(context: GraphContext, progressCallback: (progress: number) => void): TaskResult;
735
+ execute(context: GraphContext, emit: (signal: string, context: any) => void, progressCallback: (progress: number) => void): TaskResult;
730
736
  }
731
737
 
732
738
  declare class EphemeralTask extends Task {
@@ -734,7 +740,7 @@ declare class EphemeralTask extends Task {
734
740
  private readonly condition;
735
741
  readonly isEphemeral: boolean;
736
742
  constructor(name: string, task: TaskFunction, description?: string, once?: boolean, condition?: (context: any) => boolean, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: boolean, getTagCallback?: ThrottleTagGetter | undefined, inputSchema?: SchemaDefinition | undefined, validateInputContext?: boolean, outputSchema?: SchemaDefinition | undefined, validateOutputContext?: boolean, retryCount?: number, retryDelay?: number, retryDelayMax?: number, retryDelayFactor?: number);
737
- execute(context: any, progressCallback: (progress: number) => void): TaskResult;
743
+ execute(context: any, emit: (signal: string, context: AnyObject) => void, progressCallback: (progress: number) => void): TaskResult;
738
744
  }
739
745
 
740
746
  declare class GraphAsyncRun extends GraphRunStrategy {
package/dist/index.js CHANGED
@@ -745,6 +745,20 @@ var GraphNode = class _GraphNode extends SignalEmitter {
745
745
  ...this.lightExport(),
746
746
  __scheduled: Date.now()
747
747
  });
748
+ const context = this.context.getFullContext();
749
+ if (context.__signalName !== void 0 && !context.__signalName.includes("meta.")) {
750
+ this.emit("meta.node.consumed_signal", {
751
+ __signal_log: {
752
+ signal_name: context.__signalName,
753
+ log_type: "consume",
754
+ consumed_by_task_id: this.task.id,
755
+ task_execution_id: this.id,
756
+ relation_type: "listener",
757
+ metadata: {},
758
+ is_meta: false
759
+ }
760
+ });
761
+ }
748
762
  }
749
763
  }
750
764
  start() {
@@ -819,6 +833,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
819
833
  try {
820
834
  const result = this.task.execute(
821
835
  this.context,
836
+ this.emitWithMetadata.bind(this),
822
837
  this.onProgress.bind(this)
823
838
  );
824
839
  if (result.errored || result.failed) {
@@ -836,6 +851,26 @@ var GraphNode = class _GraphNode extends SignalEmitter {
836
851
  });
837
852
  }
838
853
  }
854
+ emitWithMetadata(signal, context) {
855
+ this.emit(signal, {
856
+ ...context,
857
+ __emittedSignal: signal,
858
+ __emittedByNode: this.id
859
+ });
860
+ if (!signal.includes(".meta")) {
861
+ this.emit("meta.node.emitted_signal", {
862
+ __signal_log: {
863
+ signal_name: signal,
864
+ log_type: "emit",
865
+ emitted_by_task_id: this.task.id,
866
+ task_execution_id: this.id,
867
+ relation_type: "emitter",
868
+ metadata: {},
869
+ is_meta: false
870
+ }
871
+ });
872
+ }
873
+ }
839
874
  onProgress(progress) {
840
875
  var _a, _b, _c;
841
876
  progress = Math.min(Math.max(0, progress), 1);
@@ -860,9 +895,13 @@ var GraphNode = class _GraphNode extends SignalEmitter {
860
895
  this.completeSubgraph();
861
896
  }
862
897
  if (this.errored || this.failed) {
863
- this.task.emitOnFailSignals(this.context);
898
+ this.task.mapOnFailSignals(
899
+ (signal) => this.emitWithMetadata(signal, this.context)
900
+ );
864
901
  } else {
865
- this.task.emitSignals(this.context);
902
+ this.task.mapSignals(
903
+ (signal) => this.emitWithMetadata(signal, this.context)
904
+ );
866
905
  }
867
906
  this.end();
868
907
  }
@@ -1124,8 +1163,8 @@ var import_uuid4 = require("uuid");
1124
1163
  var SignalParticipant = class extends SignalEmitter {
1125
1164
  constructor() {
1126
1165
  super(...arguments);
1127
- this.signalsToEmit = /* @__PURE__ */ new Set();
1128
- // Use Set to prevent duplicates
1166
+ this.emitsSignals = /* @__PURE__ */ new Set();
1167
+ this.signalsToEmitAfter = /* @__PURE__ */ new Set();
1129
1168
  this.signalsToEmitOnFail = /* @__PURE__ */ new Set();
1130
1169
  this.observedSignals = /* @__PURE__ */ new Set();
1131
1170
  }
@@ -1148,12 +1187,18 @@ var SignalParticipant = class extends SignalEmitter {
1148
1187
  * @param signals The signal names.
1149
1188
  * @returns This for chaining.
1150
1189
  */
1151
- emits(...signals) {
1152
- signals.forEach((signal) => this.signalsToEmit.add(signal));
1190
+ emitsAfter(...signals) {
1191
+ signals.forEach((signal) => {
1192
+ this.signalsToEmitAfter.add(signal);
1193
+ this.emitsSignals.add(signal);
1194
+ });
1153
1195
  return this;
1154
1196
  }
1155
1197
  emitsOnFail(...signals) {
1156
- signals.forEach((signal) => this.signalsToEmitOnFail.add(signal));
1198
+ signals.forEach((signal) => {
1199
+ this.signalsToEmitOnFail.add(signal);
1200
+ this.emitsSignals.add(signal);
1201
+ });
1157
1202
  return this;
1158
1203
  }
1159
1204
  /**
@@ -1188,7 +1233,7 @@ var SignalParticipant = class extends SignalEmitter {
1188
1233
  * @returns This for chaining.
1189
1234
  */
1190
1235
  detachSignals(...signals) {
1191
- signals.forEach((signal) => this.signalsToEmit.delete(signal));
1236
+ signals.forEach((signal) => this.signalsToEmitAfter.delete(signal));
1192
1237
  return this;
1193
1238
  }
1194
1239
  /**
@@ -1196,16 +1241,22 @@ var SignalParticipant = class extends SignalEmitter {
1196
1241
  * @returns This for chaining.
1197
1242
  */
1198
1243
  detachAllSignals() {
1199
- this.signalsToEmit.clear();
1244
+ this.signalsToEmitAfter.clear();
1200
1245
  return this;
1201
1246
  }
1247
+ mapSignals(callback) {
1248
+ return Array.from(this.signalsToEmitAfter).map(callback);
1249
+ }
1250
+ mapOnFailSignals(callback) {
1251
+ return Array.from(this.signalsToEmitOnFail).map(callback);
1252
+ }
1202
1253
  /**
1203
1254
  * Emits attached signals.
1204
1255
  * @param context The context for emission.
1205
1256
  * @edge If isMeta (from Task), suppresses further "meta.*" to prevent loops.
1206
1257
  */
1207
1258
  emitSignals(context) {
1208
- this.signalsToEmit.forEach((signal) => {
1259
+ this.signalsToEmitAfter.forEach((signal) => {
1209
1260
  this.emit(signal, context.getFullContext());
1210
1261
  });
1211
1262
  }
@@ -1560,14 +1611,16 @@ var Task = class extends SignalParticipant {
1560
1611
  /**
1561
1612
  * Executes the task function after optional input validation.
1562
1613
  * @param context - The GraphContext to validate and execute.
1614
+ * @param emit
1563
1615
  * @param progressCallback - Callback for progress updates.
1564
1616
  * @returns TaskResult from the taskFunction or error object on validation failure.
1565
1617
  * @edge If validateInputContext is true, validates context; on failure, emits 'meta.task.validationFailed' with detailed errors.
1566
1618
  * @edge If validateOutputContext is true, validates output; on failure, emits 'meta.task.outputValidationFailed' with detailed errors.
1567
1619
  */
1568
- execute(context, progressCallback) {
1620
+ execute(context, emit, progressCallback) {
1569
1621
  return this.taskFunction(
1570
1622
  this.isMeta ? context.getClonedFullContext() : context.getClonedContext(),
1623
+ emit,
1571
1624
  progressCallback
1572
1625
  );
1573
1626
  }
@@ -1707,7 +1760,7 @@ var Task = class extends SignalParticipant {
1707
1760
  __isMeta: this.isMeta,
1708
1761
  __isSignal: this.isSignal,
1709
1762
  __eventTriggers: this.observedSignals,
1710
- __attachedEvents: this.signalsToEmit,
1763
+ __attachedEvents: this.signalsToEmitAfter,
1711
1764
  __isDeputy: this.isDeputy,
1712
1765
  __throttled: this.throttled,
1713
1766
  __isEphemeral: this.isEphemeral,
@@ -2108,6 +2161,7 @@ var DebounceTask = class extends Task {
2108
2161
  this.lastContext = null;
2109
2162
  this.lastTimeout = null;
2110
2163
  this.lastProgressCallback = null;
2164
+ this.lastEmitFunction = null;
2111
2165
  this.debounceTime = debounceTime;
2112
2166
  this.leading = leading;
2113
2167
  this.trailing = trailing;
@@ -2121,6 +2175,7 @@ var DebounceTask = class extends Task {
2121
2175
  try {
2122
2176
  result = this.taskFunction(
2123
2177
  this.lastContext.getClonedContext(),
2178
+ this.lastEmitFunction,
2124
2179
  this.lastProgressCallback
2125
2180
  );
2126
2181
  } catch (error) {
@@ -2137,7 +2192,7 @@ var DebounceTask = class extends Task {
2137
2192
  }
2138
2193
  }
2139
2194
  }
2140
- debouncedTrigger(resolve, reject, context, timeout, progressCallback) {
2195
+ debouncedTrigger(resolve, reject, context, timeout, emit, progressCallback) {
2141
2196
  const callNow = this.leading && this.timer === null;
2142
2197
  const isNewBurst = this.timer === null;
2143
2198
  if (this.timer !== null) {
@@ -2149,6 +2204,7 @@ var DebounceTask = class extends Task {
2149
2204
  this.lastContext = context;
2150
2205
  this.lastTimeout = timeout;
2151
2206
  this.lastProgressCallback = progressCallback;
2207
+ this.lastEmitFunction = emit;
2152
2208
  if (!callNow) {
2153
2209
  this.hasLaterCall = true;
2154
2210
  }
@@ -2181,7 +2237,7 @@ var DebounceTask = class extends Task {
2181
2237
  }, this.maxWait);
2182
2238
  }
2183
2239
  }
2184
- execute(context, progressCallback) {
2240
+ execute(context, emit, progressCallback) {
2185
2241
  return new Promise((resolve, reject) => {
2186
2242
  const timeout = setTimeout(() => {
2187
2243
  resolve(false);
@@ -2191,6 +2247,7 @@ var DebounceTask = class extends Task {
2191
2247
  reject,
2192
2248
  context,
2193
2249
  timeout,
2250
+ emit,
2194
2251
  progressCallback
2195
2252
  );
2196
2253
  });
@@ -2223,9 +2280,8 @@ var EphemeralTask = class extends Task {
2223
2280
  this.once = once;
2224
2281
  this.condition = condition;
2225
2282
  }
2226
- execute(context, progressCallback) {
2227
- const result = super.execute(context, progressCallback);
2228
- this.emit("meta.ephemeral.executed", result);
2283
+ execute(context, emit, progressCallback) {
2284
+ const result = super.execute(context, emit, progressCallback);
2229
2285
  if (this.once || !this.condition(result)) {
2230
2286
  this.destroy();
2231
2287
  return result;
@@ -3194,7 +3250,7 @@ Cadenza.mode = "production";
3194
3250
  var SignalTask = class extends Task {
3195
3251
  constructor(signal, description = "") {
3196
3252
  super(signal, () => true, description);
3197
- this.signalsToEmit.add(signal);
3253
+ this.signalsToEmitAfter.add(signal);
3198
3254
  }
3199
3255
  };
3200
3256