@cadenza.io/core 1.3.0 → 1.5.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
@@ -361,20 +361,48 @@ declare class SignalParticipant extends SignalEmitter {
361
361
  destroy(): void;
362
362
  }
363
363
 
364
+ type SchemaType = "string" | "number" | "boolean" | "array" | "object" | "null" | "any";
365
+ type SchemaConstraints = {
366
+ min?: number;
367
+ max?: number;
368
+ minLength?: number;
369
+ maxLength?: number;
370
+ pattern?: string;
371
+ enum?: any[];
372
+ multipleOf?: number;
373
+ format?: "email" | "url" | "date-time" | "uuid" | "custom";
374
+ oneOf?: any[];
375
+ };
376
+ type SchemaDefinition = {
377
+ type: SchemaType;
378
+ required?: string[];
379
+ properties?: {
380
+ [key: string]: SchemaDefinition;
381
+ };
382
+ items?: SchemaDefinition;
383
+ constraints?: SchemaConstraints;
384
+ description?: string;
385
+ };
386
+
364
387
  type TaskFunction = (context: AnyObject, progressCallback: (progress: number) => void) => TaskResult;
365
388
  type TaskResult = boolean | object | Generator | Promise<any> | void;
389
+ type ThrottleTagGetter = (context?: AnyObject, task?: Task) => string;
366
390
  declare class Task extends SignalParticipant implements Graph {
367
391
  id: string;
368
392
  readonly name: string;
369
393
  readonly description: string;
370
394
  concurrency: number;
371
395
  timeout: number;
396
+ readonly isMeta: boolean;
372
397
  readonly isUnique: boolean;
373
398
  readonly throttled: boolean;
374
399
  readonly isSignal: boolean;
375
- readonly isMeta: boolean;
376
400
  readonly isDeputy: boolean;
377
401
  readonly isEphemeral: boolean;
402
+ protected inputContextSchema: SchemaDefinition | undefined;
403
+ protected validateInputContext: boolean;
404
+ protected outputContextSchema: SchemaDefinition | undefined;
405
+ protected validateOutputContext: boolean;
378
406
  layerIndex: number;
379
407
  progressWeight: number;
380
408
  private nextTasks;
@@ -390,14 +418,47 @@ declare class Task extends SignalParticipant implements Graph {
390
418
  * @param concurrency Limit.
391
419
  * @param timeout ms.
392
420
  * @param register Register via signal (default true).
421
+ * @param isUnique
422
+ * @param isMeta
423
+ * @param getTagCallback
424
+ * @param inputSchema
425
+ * @param validateInputContext
426
+ * @param outputSchema
427
+ * @param validateOutputContext
393
428
  * @edge Emits 'meta.task.created' with { __task: this } for seed.
394
429
  */
395
- constructor(name: string, task: TaskFunction, description?: string, concurrency?: number, timeout?: number, register?: boolean);
430
+ constructor(name: string, task: TaskFunction, description?: string, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: boolean, getTagCallback?: ThrottleTagGetter | undefined, inputSchema?: SchemaDefinition | undefined, validateInputContext?: boolean, outputSchema?: SchemaDefinition | undefined, validateOutputContext?: boolean);
396
431
  getTag(context?: AnyObject): string;
397
432
  setGlobalId(id: string): void;
398
433
  setTimeout(timeout: number): void;
399
434
  setConcurrency(concurrency: number): void;
400
435
  setProgressWeight(weight: number): void;
436
+ setInputContextSchema(schema: SchemaDefinition): void;
437
+ setOutputContextSchema(schema: SchemaDefinition): void;
438
+ setValidateInputContext(value: boolean): void;
439
+ setValidateOutputContext(value: boolean): void;
440
+ /**
441
+ * Validates a context deeply against a schema.
442
+ * @param data - The data to validate (input context or output result).
443
+ * @param schema - The schema definition.
444
+ * @param path - The current path for error reporting (default: 'root').
445
+ * @returns { { valid: boolean, errors: Record<string, string> } } - Validation result with detailed errors if invalid.
446
+ * @description Recursively checks types, required fields, and constraints; allows extra properties not in schema.
447
+ */
448
+ protected validateSchema(data: any, schema: SchemaDefinition | undefined, path?: string): {
449
+ valid: boolean;
450
+ errors: Record<string, string>;
451
+ };
452
+ validateInput(context: AnyObject): true | AnyObject;
453
+ validateOutput(context: AnyObject): true | AnyObject;
454
+ /**
455
+ * Executes the task function after optional input validation.
456
+ * @param context - The GraphContext to validate and execute.
457
+ * @param progressCallback - Callback for progress updates.
458
+ * @returns TaskResult from the taskFunction or error object on validation failure.
459
+ * @edge If validateInputContext is true, validates context; on failure, emits 'meta.task.validationFailed' with detailed errors.
460
+ * @edge If validateOutputContext is true, validates output; on failure, emits 'meta.task.outputValidationFailed' with detailed errors.
461
+ */
401
462
  execute(context: GraphContext, progressCallback: (progress: number) => void): TaskResult;
402
463
  doAfter(...tasks: Task[]): this;
403
464
  then(...tasks: Task[]): this;
@@ -479,7 +540,7 @@ declare class GraphRoutine extends SignalParticipant {
479
540
  readonly description: string;
480
541
  readonly isMeta: boolean;
481
542
  private tasks;
482
- constructor(name: string, tasks: Task[], description: string);
543
+ constructor(name: string, tasks: Task[], description: string, isMeta?: boolean);
483
544
  /**
484
545
  * Applies callback to starting tasks.
485
546
  * @param callBack The callback.
@@ -519,7 +580,7 @@ declare class GraphRunner extends SignalEmitter {
519
580
  * @edge Emits 'meta.runner.added_tasks' with metadata.
520
581
  * @edge Empty tasks warns no-op.
521
582
  */
522
- protected addTasks(tasks: Task | GraphRoutine | (Task | GraphRoutine)[], context: AnyObject): void;
583
+ protected addTasks(tasks: Task | GraphRoutine | (Task | GraphRoutine)[], context?: AnyObject): void;
523
584
  /**
524
585
  * Runs tasks/routines.
525
586
  * @param tasks Optional tasks/routines.
@@ -545,18 +606,20 @@ declare class SignalBroker {
545
606
  static get instance(): SignalBroker;
546
607
  protected runner: GraphRunner | undefined;
547
608
  protected metaRunner: GraphRunner | undefined;
609
+ getSignalsTask: Task | undefined;
548
610
  protected signalObservers: Map<string, {
549
611
  fn: (runner: GraphRunner, tasks: (Task | GraphRoutine)[], context: AnyObject) => void;
550
612
  tasks: Set<Task | GraphRoutine>;
551
613
  }>;
552
- protected emitStacks: Map<string, Set<string>>;
614
+ protected emitStacks: Map<string, Map<string, AnyObject>>;
553
615
  protected constructor();
554
616
  /**
555
617
  * Initializes with runners.
556
618
  * @param runner Standard runner for user signals.
557
619
  * @param metaRunner Meta runner for 'meta.' signals (suppresses further meta-emits).
558
620
  */
559
- init(runner: GraphRunner, metaRunner: GraphRunner): void;
621
+ bootstrap(runner: GraphRunner, metaRunner: GraphRunner): void;
622
+ init(): void;
560
623
  /**
561
624
  * Observes a signal with a routine/task.
562
625
  * @param signal The signal (e.g., 'domain.action', 'domain.*' for wildcards).
@@ -580,6 +643,7 @@ declare class SignalBroker {
580
643
  * @throws Error on detected loop.
581
644
  */
582
645
  emit(signal: string, context?: AnyObject): void;
646
+ execute(signal: string, context: AnyObject): boolean;
583
647
  private executeListener;
584
648
  private addSignal;
585
649
  /**
@@ -591,55 +655,30 @@ declare class SignalBroker {
591
655
  reset(): void;
592
656
  }
593
657
 
594
- declare class MetaTask extends Task {
595
- readonly isMeta: boolean;
596
- }
597
-
598
658
  declare class GraphRegistry {
599
659
  private static _instance;
600
660
  static get instance(): GraphRegistry;
601
661
  private tasks;
602
662
  private routines;
603
- registerTask: MetaTask;
604
- updateTaskId: MetaTask;
605
- getTaskById: MetaTask;
606
- getTaskByName: MetaTask;
607
- getTasksByLayer: MetaTask;
608
- getAllTasks: MetaTask;
609
- doForEachTask: MetaTask;
610
- deleteTask: MetaTask;
611
- registerRoutine: MetaTask;
612
- updateRoutineId: MetaTask;
613
- getRoutineById: MetaTask;
614
- getRoutineByName: MetaTask;
615
- getAllRoutines: MetaTask;
616
- doForEachRoutine: MetaTask;
617
- deleteRoutine: MetaTask;
663
+ registerTask: Task;
664
+ updateTaskId: Task;
665
+ getTaskById: Task;
666
+ getTaskByName: Task;
667
+ getTasksByLayer: Task;
668
+ getAllTasks: Task;
669
+ doForEachTask: Task;
670
+ deleteTask: Task;
671
+ registerRoutine: Task;
672
+ updateRoutineId: Task;
673
+ getRoutineById: Task;
674
+ getRoutineByName: Task;
675
+ getAllRoutines: Task;
676
+ doForEachRoutine: Task;
677
+ deleteRoutine: Task;
618
678
  private constructor();
619
679
  reset(): void;
620
680
  }
621
681
 
622
- declare class UniqueTask extends Task {
623
- readonly isUnique: boolean;
624
- }
625
-
626
- declare class UniqueMetaTask extends UniqueTask {
627
- readonly isMeta: boolean;
628
- }
629
-
630
- type ThrottleTagGetter = (context?: AnyObject, task?: Task) => string;
631
- declare class ThrottledTask extends Task {
632
- readonly throttled: boolean;
633
- constructor(name: string, task: TaskFunction, description?: string, getTagCallback?: ThrottleTagGetter, concurrency?: number, timeout?: number, register?: boolean);
634
- export(): {
635
- __getTagCallback: string;
636
- };
637
- }
638
-
639
- declare class ThrottledMetaTask extends ThrottledTask {
640
- readonly isMeta: boolean;
641
- }
642
-
643
682
  interface DebounceOptions {
644
683
  leading?: boolean;
645
684
  trailing?: boolean;
@@ -653,32 +692,20 @@ declare class DebounceTask extends Task {
653
692
  private lastReject;
654
693
  private lastContext;
655
694
  private lastTimeout;
656
- constructor(name: string, task: TaskFunction, description?: string, debounceTime?: number, leading?: boolean, trailing?: boolean, concurrency?: number, timeout?: number, register?: boolean);
695
+ constructor(name: string, task: TaskFunction, description?: string, debounceTime?: number, leading?: boolean, trailing?: boolean, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: boolean, inputSchema?: SchemaDefinition | undefined, validateInputSchema?: boolean, outputSchema?: SchemaDefinition | undefined, validateOutputSchema?: boolean);
657
696
  private executeFunction;
658
697
  private debouncedTrigger;
659
698
  execute(context: GraphContext, progressCallback: (progress: number) => void): TaskResult;
660
699
  }
661
700
 
662
- declare class DebouncedMetaTask extends DebounceTask {
663
- readonly isMeta: boolean;
664
- }
665
-
666
701
  declare class EphemeralTask extends Task {
667
702
  private readonly once;
668
703
  private readonly condition;
669
704
  readonly isEphemeral: boolean;
670
- constructor(name: string, task: TaskFunction, description?: string, once?: boolean, condition?: (context: any) => boolean, concurrency?: number, timeout?: number, register?: boolean);
705
+ 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);
671
706
  execute(context: any, progressCallback: (progress: number) => void): TaskResult;
672
707
  }
673
708
 
674
- declare class EphemeralMetaTask extends EphemeralTask {
675
- readonly isMeta: boolean;
676
- }
677
-
678
- declare class MetaRoutine extends GraphRoutine {
679
- readonly isMeta: boolean;
680
- }
681
-
682
709
  declare class GraphAsyncRun extends GraphRunStrategy {
683
710
  constructor();
684
711
  run(): Promise<void>;
@@ -695,14 +722,21 @@ interface TaskOptions {
695
722
  concurrency?: number;
696
723
  timeout?: number;
697
724
  register?: boolean;
725
+ isUnique?: boolean;
726
+ isMeta?: boolean;
727
+ getTagCallback?: ThrottleTagGetter;
728
+ inputSchema?: SchemaDefinition;
729
+ validateInputContext?: boolean;
730
+ outputSchema?: SchemaDefinition;
731
+ validateOutputContext?: boolean;
698
732
  }
699
733
  declare class Cadenza {
700
734
  static broker: SignalBroker;
701
735
  static runner: GraphRunner;
702
736
  static metaRunner: GraphRunner;
703
737
  static registry: GraphRegistry;
704
- private static isBootstrapped;
705
- private static bootstrap;
738
+ protected static isBootstrapped: boolean;
739
+ protected static bootstrap(): void;
706
740
  static get runStrategy(): {
707
741
  PARALLEL: GraphAsyncRun;
708
742
  SEQUENTIAL: GraphStandardRun;
@@ -712,7 +746,7 @@ declare class Cadenza {
712
746
  * @param name The name to validate.
713
747
  * @throws Error if invalid.
714
748
  */
715
- private static validateName;
749
+ protected static validateName(name: string): void;
716
750
  /**
717
751
  * Creates a standard Task and registers it in the GraphRegistry.
718
752
  * @param name Unique identifier for the task.
@@ -733,7 +767,7 @@ declare class Cadenza {
733
767
  * @returns The created MetaTask instance.
734
768
  * @throws Error if name invalid or duplicate.
735
769
  */
736
- static createMetaTask(name: string, func: TaskFunction, description?: string, options?: TaskOptions): MetaTask;
770
+ static createMetaTask(name: string, func: TaskFunction, description?: string, options?: TaskOptions): Task;
737
771
  /**
738
772
  * Creates a UniqueTask (executes once per execution ID, merging parents) and registers it.
739
773
  * Use for fan-in/joins after parallel branches.
@@ -744,7 +778,7 @@ declare class Cadenza {
744
778
  * @returns The created UniqueTask.
745
779
  * @throws Error if invalid.
746
780
  */
747
- static createUniqueTask(name: string, func: TaskFunction, description?: string, options?: TaskOptions): UniqueTask;
781
+ static createUniqueTask(name: string, func: TaskFunction, description?: string, options?: TaskOptions): Task;
748
782
  /**
749
783
  * Creates a UniqueMetaTask for meta-layer joins.
750
784
  * @param name Unique identifier.
@@ -753,7 +787,7 @@ declare class Cadenza {
753
787
  * @param options Optional task options.
754
788
  * @returns The created UniqueMetaTask.
755
789
  */
756
- static createUniqueMetaTask(name: string, func: TaskFunction, description?: string, options?: TaskOptions): UniqueMetaTask;
790
+ static createUniqueMetaTask(name: string, func: TaskFunction, description?: string, options?: TaskOptions): Task;
757
791
  /**
758
792
  * Creates a ThrottledTask (rate-limited by concurrency or custom groups) and registers it.
759
793
  * @param name Unique identifier.
@@ -764,7 +798,7 @@ declare class Cadenza {
764
798
  * @returns The created ThrottledTask.
765
799
  * @edge If no getter, throttles per task ID; use for resource protection.
766
800
  */
767
- static createThrottledTask(name: string, func: TaskFunction, throttledIdGetter?: ThrottleTagGetter, description?: string, options?: TaskOptions): ThrottledTask;
801
+ static createThrottledTask(name: string, func: TaskFunction, throttledIdGetter?: ThrottleTagGetter, description?: string, options?: TaskOptions): Task;
768
802
  /**
769
803
  * Creates a ThrottledMetaTask for meta-layer throttling.
770
804
  * @param name Identifier.
@@ -774,7 +808,7 @@ declare class Cadenza {
774
808
  * @param options Optional task options.
775
809
  * @returns The created ThrottledMetaTask.
776
810
  */
777
- static createThrottledMetaTask(name: string, func: TaskFunction, throttledIdGetter?: ThrottleTagGetter, description?: string, options?: TaskOptions): ThrottledMetaTask;
811
+ static createThrottledMetaTask(name: string, func: TaskFunction, throttledIdGetter: ThrottleTagGetter, description?: string, options?: TaskOptions): Task;
778
812
  /**
779
813
  * Creates a DebounceTask (delays exec until quiet period) and registers it.
780
814
  * @param name Identifier.
@@ -795,7 +829,7 @@ declare class Cadenza {
795
829
  * @param options Optional task options plus optional debounce config (e.g., leading/trailing).
796
830
  * @returns The created DebouncedMetaTask.
797
831
  */
798
- static createDebounceMetaTask(name: string, func: TaskFunction, description?: string, debounceTime?: number, options?: TaskOptions & DebounceOptions): DebouncedMetaTask;
832
+ static createDebounceMetaTask(name: string, func: TaskFunction, description?: string, debounceTime?: number, options?: TaskOptions & DebounceOptions): DebounceTask;
799
833
  /**
800
834
  * Creates an EphemeralTask (self-destructs after exec or condition) without default registration.
801
835
  * Useful for transients; optionally register if needed.
@@ -819,7 +853,7 @@ declare class Cadenza {
819
853
  * @param options Optional task options.
820
854
  * @returns The created EphemeralMetaTask.
821
855
  */
822
- static createEphemeralMetaTask(name: string, func: TaskFunction, description?: string, once?: boolean, destroyCondition?: (context: any) => boolean, options?: TaskOptions): EphemeralMetaTask;
856
+ static createEphemeralMetaTask(name: string, func: TaskFunction, description?: string, once?: boolean, destroyCondition?: (context: any) => boolean, options?: TaskOptions): EphemeralTask;
823
857
  /**
824
858
  * Creates a GraphRoutine (named entry to starting tasks) and registers it.
825
859
  * @param name Unique identifier.
@@ -836,7 +870,7 @@ declare class Cadenza {
836
870
  * @param description Optional.
837
871
  * @returns The created MetaRoutine.
838
872
  */
839
- static createMetaRoutine(name: string, tasks: Task[], description?: string): MetaRoutine;
873
+ static createMetaRoutine(name: string, tasks: Task[], description?: string): GraphRoutine;
840
874
  static reset(): void;
841
875
  }
842
876
 
@@ -844,8 +878,4 @@ declare class SignalTask extends Task {
844
878
  constructor(signal: string, description?: string);
845
879
  }
846
880
 
847
- declare class SignalMetaTask extends SignalTask {
848
- readonly isMeta = true;
849
- }
850
-
851
- export { type AnyObject, DebounceTask, DebouncedMetaTask, EphemeralMetaTask, EphemeralTask, GraphContext, GraphRegistry, GraphRoutine, GraphRun, MetaRoutine, MetaTask, SignalEmitter, SignalMetaTask, SignalParticipant, SignalTask, Task, type TaskResult, ThrottledMetaTask, ThrottledTask, UniqueMetaTask, UniqueTask, Cadenza as default };
881
+ export { type AnyObject, DebounceTask, EphemeralTask, GraphContext, GraphRegistry, GraphRoutine, GraphRun, SignalEmitter, SignalParticipant, SignalTask, Task, type TaskOptions, type TaskResult, Cadenza as default };