@cadenza.io/core 1.4.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,8 +361,32 @@ 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;
@@ -375,6 +399,10 @@ declare class Task extends SignalParticipant implements Graph {
375
399
  readonly isSignal: 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;
@@ -392,14 +420,45 @@ declare class Task extends SignalParticipant implements Graph {
392
420
  * @param register Register via signal (default true).
393
421
  * @param isUnique
394
422
  * @param isMeta
423
+ * @param getTagCallback
424
+ * @param inputSchema
425
+ * @param validateInputContext
426
+ * @param outputSchema
427
+ * @param validateOutputContext
395
428
  * @edge Emits 'meta.task.created' with { __task: this } for seed.
396
429
  */
397
- constructor(name: string, task: TaskFunction, description?: string, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: 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);
398
431
  getTag(context?: AnyObject): string;
399
432
  setGlobalId(id: string): void;
400
433
  setTimeout(timeout: number): void;
401
434
  setConcurrency(concurrency: number): void;
402
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
+ */
403
462
  execute(context: GraphContext, progressCallback: (progress: number) => void): TaskResult;
404
463
  doAfter(...tasks: Task[]): this;
405
464
  then(...tasks: Task[]): this;
@@ -521,7 +580,7 @@ declare class GraphRunner extends SignalEmitter {
521
580
  * @edge Emits 'meta.runner.added_tasks' with metadata.
522
581
  * @edge Empty tasks warns no-op.
523
582
  */
524
- protected addTasks(tasks: Task | GraphRoutine | (Task | GraphRoutine)[], context: AnyObject): void;
583
+ protected addTasks(tasks: Task | GraphRoutine | (Task | GraphRoutine)[], context?: AnyObject): void;
525
584
  /**
526
585
  * Runs tasks/routines.
527
586
  * @param tasks Optional tasks/routines.
@@ -620,12 +679,6 @@ declare class GraphRegistry {
620
679
  reset(): void;
621
680
  }
622
681
 
623
- type ThrottleTagGetter = (context?: AnyObject, task?: Task) => string;
624
- declare class ThrottledTask extends Task {
625
- readonly throttled: boolean;
626
- constructor(name: string, task: TaskFunction, description?: string, getTagCallback?: ThrottleTagGetter, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: boolean);
627
- }
628
-
629
682
  interface DebounceOptions {
630
683
  leading?: boolean;
631
684
  trailing?: boolean;
@@ -639,7 +692,7 @@ declare class DebounceTask extends Task {
639
692
  private lastReject;
640
693
  private lastContext;
641
694
  private lastTimeout;
642
- constructor(name: string, task: TaskFunction, description?: string, debounceTime?: number, leading?: boolean, trailing?: boolean, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: 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);
643
696
  private executeFunction;
644
697
  private debouncedTrigger;
645
698
  execute(context: GraphContext, progressCallback: (progress: number) => void): TaskResult;
@@ -649,7 +702,7 @@ declare class EphemeralTask extends Task {
649
702
  private readonly once;
650
703
  private readonly condition;
651
704
  readonly isEphemeral: boolean;
652
- constructor(name: string, task: TaskFunction, description?: string, once?: boolean, condition?: (context: any) => boolean, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: 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);
653
706
  execute(context: any, progressCallback: (progress: number) => void): TaskResult;
654
707
  }
655
708
 
@@ -671,6 +724,11 @@ interface TaskOptions {
671
724
  register?: boolean;
672
725
  isUnique?: boolean;
673
726
  isMeta?: boolean;
727
+ getTagCallback?: ThrottleTagGetter;
728
+ inputSchema?: SchemaDefinition;
729
+ validateInputContext?: boolean;
730
+ outputSchema?: SchemaDefinition;
731
+ validateOutputContext?: boolean;
674
732
  }
675
733
  declare class Cadenza {
676
734
  static broker: SignalBroker;
@@ -740,7 +798,7 @@ declare class Cadenza {
740
798
  * @returns The created ThrottledTask.
741
799
  * @edge If no getter, throttles per task ID; use for resource protection.
742
800
  */
743
- 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;
744
802
  /**
745
803
  * Creates a ThrottledMetaTask for meta-layer throttling.
746
804
  * @param name Identifier.
@@ -750,7 +808,7 @@ declare class Cadenza {
750
808
  * @param options Optional task options.
751
809
  * @returns The created ThrottledMetaTask.
752
810
  */
753
- static createThrottledMetaTask(name: string, func: TaskFunction, throttledIdGetter?: ThrottleTagGetter, description?: string, options?: TaskOptions): ThrottledTask;
811
+ static createThrottledMetaTask(name: string, func: TaskFunction, throttledIdGetter: ThrottleTagGetter, description?: string, options?: TaskOptions): Task;
754
812
  /**
755
813
  * Creates a DebounceTask (delays exec until quiet period) and registers it.
756
814
  * @param name Identifier.
@@ -820,8 +878,4 @@ declare class SignalTask extends Task {
820
878
  constructor(signal: string, description?: string);
821
879
  }
822
880
 
823
- declare class UniqueTask extends Task {
824
- readonly isUnique: boolean;
825
- }
826
-
827
- export { type AnyObject, DebounceTask, EphemeralTask, GraphContext, GraphRegistry, GraphRoutine, GraphRun, SignalEmitter, SignalParticipant, SignalTask, Task, type TaskOptions, type TaskResult, ThrottledTask, 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 };
package/dist/index.d.ts CHANGED
@@ -361,8 +361,32 @@ 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;
@@ -375,6 +399,10 @@ declare class Task extends SignalParticipant implements Graph {
375
399
  readonly isSignal: 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;
@@ -392,14 +420,45 @@ declare class Task extends SignalParticipant implements Graph {
392
420
  * @param register Register via signal (default true).
393
421
  * @param isUnique
394
422
  * @param isMeta
423
+ * @param getTagCallback
424
+ * @param inputSchema
425
+ * @param validateInputContext
426
+ * @param outputSchema
427
+ * @param validateOutputContext
395
428
  * @edge Emits 'meta.task.created' with { __task: this } for seed.
396
429
  */
397
- constructor(name: string, task: TaskFunction, description?: string, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: 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);
398
431
  getTag(context?: AnyObject): string;
399
432
  setGlobalId(id: string): void;
400
433
  setTimeout(timeout: number): void;
401
434
  setConcurrency(concurrency: number): void;
402
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
+ */
403
462
  execute(context: GraphContext, progressCallback: (progress: number) => void): TaskResult;
404
463
  doAfter(...tasks: Task[]): this;
405
464
  then(...tasks: Task[]): this;
@@ -521,7 +580,7 @@ declare class GraphRunner extends SignalEmitter {
521
580
  * @edge Emits 'meta.runner.added_tasks' with metadata.
522
581
  * @edge Empty tasks warns no-op.
523
582
  */
524
- protected addTasks(tasks: Task | GraphRoutine | (Task | GraphRoutine)[], context: AnyObject): void;
583
+ protected addTasks(tasks: Task | GraphRoutine | (Task | GraphRoutine)[], context?: AnyObject): void;
525
584
  /**
526
585
  * Runs tasks/routines.
527
586
  * @param tasks Optional tasks/routines.
@@ -620,12 +679,6 @@ declare class GraphRegistry {
620
679
  reset(): void;
621
680
  }
622
681
 
623
- type ThrottleTagGetter = (context?: AnyObject, task?: Task) => string;
624
- declare class ThrottledTask extends Task {
625
- readonly throttled: boolean;
626
- constructor(name: string, task: TaskFunction, description?: string, getTagCallback?: ThrottleTagGetter, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: boolean);
627
- }
628
-
629
682
  interface DebounceOptions {
630
683
  leading?: boolean;
631
684
  trailing?: boolean;
@@ -639,7 +692,7 @@ declare class DebounceTask extends Task {
639
692
  private lastReject;
640
693
  private lastContext;
641
694
  private lastTimeout;
642
- constructor(name: string, task: TaskFunction, description?: string, debounceTime?: number, leading?: boolean, trailing?: boolean, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: 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);
643
696
  private executeFunction;
644
697
  private debouncedTrigger;
645
698
  execute(context: GraphContext, progressCallback: (progress: number) => void): TaskResult;
@@ -649,7 +702,7 @@ declare class EphemeralTask extends Task {
649
702
  private readonly once;
650
703
  private readonly condition;
651
704
  readonly isEphemeral: boolean;
652
- constructor(name: string, task: TaskFunction, description?: string, once?: boolean, condition?: (context: any) => boolean, concurrency?: number, timeout?: number, register?: boolean, isUnique?: boolean, isMeta?: 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);
653
706
  execute(context: any, progressCallback: (progress: number) => void): TaskResult;
654
707
  }
655
708
 
@@ -671,6 +724,11 @@ interface TaskOptions {
671
724
  register?: boolean;
672
725
  isUnique?: boolean;
673
726
  isMeta?: boolean;
727
+ getTagCallback?: ThrottleTagGetter;
728
+ inputSchema?: SchemaDefinition;
729
+ validateInputContext?: boolean;
730
+ outputSchema?: SchemaDefinition;
731
+ validateOutputContext?: boolean;
674
732
  }
675
733
  declare class Cadenza {
676
734
  static broker: SignalBroker;
@@ -740,7 +798,7 @@ declare class Cadenza {
740
798
  * @returns The created ThrottledTask.
741
799
  * @edge If no getter, throttles per task ID; use for resource protection.
742
800
  */
743
- 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;
744
802
  /**
745
803
  * Creates a ThrottledMetaTask for meta-layer throttling.
746
804
  * @param name Identifier.
@@ -750,7 +808,7 @@ declare class Cadenza {
750
808
  * @param options Optional task options.
751
809
  * @returns The created ThrottledMetaTask.
752
810
  */
753
- static createThrottledMetaTask(name: string, func: TaskFunction, throttledIdGetter?: ThrottleTagGetter, description?: string, options?: TaskOptions): ThrottledTask;
811
+ static createThrottledMetaTask(name: string, func: TaskFunction, throttledIdGetter: ThrottleTagGetter, description?: string, options?: TaskOptions): Task;
754
812
  /**
755
813
  * Creates a DebounceTask (delays exec until quiet period) and registers it.
756
814
  * @param name Identifier.
@@ -820,8 +878,4 @@ declare class SignalTask extends Task {
820
878
  constructor(signal: string, description?: string);
821
879
  }
822
880
 
823
- declare class UniqueTask extends Task {
824
- readonly isUnique: boolean;
825
- }
826
-
827
- export { type AnyObject, DebounceTask, EphemeralTask, GraphContext, GraphRegistry, GraphRoutine, GraphRun, SignalEmitter, SignalParticipant, SignalTask, Task, type TaskOptions, type TaskResult, ThrottledTask, 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 };