@cadenza.io/core 3.21.0 → 3.23.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
@@ -1805,34 +1805,77 @@ declare class GraphStandardRun extends GraphRunStrategy {
1805
1805
  export(): any;
1806
1806
  }
1807
1807
 
1808
+ /**
1809
+ * Determines when an actor key should be materialized in memory.
1810
+ * - `eager`: creates the default key record immediately.
1811
+ * - `lazy`: creates key records on first access.
1812
+ */
1808
1813
  type ActorLoadPolicy = "eager" | "lazy";
1814
+ /**
1815
+ * Defines how durable writes should be interpreted by actor tasks.
1816
+ * - `overwrite`: replace full durable state.
1817
+ * - `patch`: shallow-merge object fields.
1818
+ * - `reducer`: allow reducer-return handlers.
1819
+ */
1809
1820
  type ActorWriteContract = "overwrite" | "patch" | "reducer";
1821
+ /**
1822
+ * Declares the intent of an actor task.
1823
+ * - `read`: no state writes allowed.
1824
+ * - `write`: durable/runtime writes allowed.
1825
+ * - `meta`: write semantics with forced meta task registration.
1826
+ */
1810
1827
  type ActorTaskMode = "read" | "write" | "meta";
1828
+ /**
1829
+ * Actor classification.
1830
+ * Meta actors are internal/framework-level actors and force bound tasks to meta.
1831
+ */
1811
1832
  type ActorKind = "standard" | "meta";
1833
+ /**
1834
+ * Optional runtime read protection policy.
1835
+ */
1812
1836
  type ActorRuntimeReadGuard = "none" | "freeze-shallow";
1837
+ /**
1838
+ * Consistency profile hint used primarily by distributed/service extensions.
1839
+ */
1813
1840
  type ActorConsistencyProfileName = "strict" | "balanced" | "cached" | "async";
1841
+ /**
1842
+ * Per-invocation actor options accepted via `context.__actorOptions`.
1843
+ * Only key targeting and idempotency are overridable at invocation level.
1844
+ */
1814
1845
  interface ActorInvocationOptions {
1815
1846
  actorKey?: string;
1816
1847
  idempotencyKey?: string;
1817
1848
  }
1849
+ /**
1850
+ * Session expiry/touch behavior for actor keys.
1851
+ */
1818
1852
  interface SessionPolicy {
1819
1853
  enabled?: boolean;
1820
1854
  idleTtlMs?: number;
1821
1855
  absoluteTtlMs?: number;
1822
1856
  extendIdleTtlOnRead?: boolean;
1823
1857
  }
1858
+ /**
1859
+ * Task retry policy metadata for actor definitions/specs.
1860
+ */
1824
1861
  interface RetryPolicy {
1825
1862
  attempts?: number;
1826
1863
  delayMs?: number;
1827
1864
  maxDelayMs?: number;
1828
1865
  factor?: number;
1829
1866
  }
1867
+ /**
1868
+ * Idempotency policy for actor task execution.
1869
+ */
1830
1870
  interface IdempotencyPolicy {
1831
1871
  enabled?: boolean;
1832
1872
  mode?: "required" | "optional";
1833
1873
  rerunOnFailedDuplicate?: boolean;
1834
1874
  ttlMs?: number;
1835
1875
  }
1876
+ /**
1877
+ * Declarative actor-key resolution configuration.
1878
+ */
1836
1879
  type ActorKeyDefinition = {
1837
1880
  source: "path";
1838
1881
  path: string;
@@ -1843,30 +1886,38 @@ type ActorKeyDefinition = {
1843
1886
  source: "template";
1844
1887
  template: string;
1845
1888
  };
1889
+ /**
1890
+ * Serializable metadata describing a task bound to an actor.
1891
+ */
1846
1892
  interface ActorTaskBindingDefinition {
1847
1893
  taskName: string;
1848
1894
  mode?: ActorTaskMode;
1849
1895
  description?: string;
1850
1896
  }
1897
+ /**
1898
+ * Declarative state schema/configuration for an actor.
1899
+ *
1900
+ * Durable state uses `initState` for bootstrap defaults.
1901
+ * Runtime state is intentionally task-driven (no actor-level init lifecycle hook).
1902
+ */
1851
1903
  interface ActorStateDefinition<D extends Record<string, any>, R = AnyObject> {
1852
1904
  durable?: {
1853
- initialState?: D | (() => D);
1905
+ /**
1906
+ * Initial durable state. Prefer static values for simple cases.
1907
+ * Function form is intended for advanced computed initialization.
1908
+ */
1909
+ initState?: D | (() => D);
1854
1910
  schema?: AnyObject;
1855
1911
  description?: string;
1856
1912
  };
1857
1913
  runtime?: {
1858
- initialState?: R | (() => R);
1859
1914
  schema?: AnyObject;
1860
1915
  description?: string;
1861
- factoryToken?: string;
1862
- factoryConfig?: AnyObject;
1863
1916
  };
1864
1917
  }
1865
- interface ActorLifecycleDefinition {
1866
- initTaskName?: string;
1867
- initRoutineName?: string;
1868
- initHandlerToken?: string;
1869
- }
1918
+ /**
1919
+ * Serializable actor definition used for persistence/round-tripping.
1920
+ */
1870
1921
  interface ActorDefinition<D extends Record<string, any>, R = AnyObject> {
1871
1922
  name: string;
1872
1923
  description: string;
@@ -1881,18 +1932,20 @@ interface ActorDefinition<D extends Record<string, any>, R = AnyObject> {
1881
1932
  runtimeReadGuard?: ActorRuntimeReadGuard;
1882
1933
  key?: ActorKeyDefinition;
1883
1934
  state?: ActorStateDefinition<D, R>;
1884
- lifecycle?: ActorLifecycleDefinition;
1885
1935
  tasks?: ActorTaskBindingDefinition[];
1886
1936
  }
1937
+ /**
1938
+ * Runtime actor specification used by `Cadenza.createActor(...)`.
1939
+ */
1887
1940
  interface ActorSpec<D extends Record<string, any>, R = AnyObject> {
1888
1941
  name: string;
1889
1942
  description?: string;
1890
1943
  state?: ActorStateDefinition<D, R>;
1891
- initialState?: D | (() => D);
1892
- initialDurableState?: D | (() => D);
1893
- initialRuntimeState?: R | (() => R);
1944
+ /**
1945
+ * Shortcut durable bootstrap state when `state.durable.initState` is not supplied.
1946
+ */
1947
+ initState?: D | (() => D);
1894
1948
  defaultKey: string;
1895
- init?: ActorInitHandler<D, R>;
1896
1949
  key?: ActorKeyDefinition;
1897
1950
  keyResolver?: (input: Record<string, any>) => string | undefined;
1898
1951
  loadPolicy?: ActorLoadPolicy;
@@ -1904,17 +1957,28 @@ interface ActorSpec<D extends Record<string, any>, R = AnyObject> {
1904
1957
  consistencyProfile?: ActorConsistencyProfileName;
1905
1958
  runtimeReadGuard?: ActorRuntimeReadGuard;
1906
1959
  taskBindings?: ActorTaskBindingDefinition[];
1907
- lifecycle?: ActorLifecycleDefinition;
1908
1960
  }
1961
+ /**
1962
+ * Optional actor creation flags.
1963
+ */
1909
1964
  interface ActorFactoryOptions<D extends Record<string, any> = Record<string, any>, R = AnyObject> {
1910
1965
  isMeta?: boolean;
1911
1966
  definitionSource?: ActorDefinition<D, R>;
1912
1967
  }
1968
+ /**
1969
+ * Optional per-binding behavior when wrapping actor handlers.
1970
+ */
1913
1971
  interface ActorTaskBindingOptions {
1914
1972
  mode?: ActorTaskMode;
1915
1973
  touchSession?: boolean;
1916
1974
  }
1975
+ /**
1976
+ * Reducer function used for state transitions.
1977
+ */
1917
1978
  type ActorStateReducer<S> = (state: S, input: AnyObject) => S;
1979
+ /**
1980
+ * Fully resolved invocation options used during actor task execution.
1981
+ */
1918
1982
  interface ActorResolvedInvocationOptions {
1919
1983
  actorKey?: string;
1920
1984
  loadPolicy: ActorLoadPolicy;
@@ -1923,6 +1987,9 @@ interface ActorResolvedInvocationOptions {
1923
1987
  idempotencyKey?: string;
1924
1988
  touchSession: boolean;
1925
1989
  }
1990
+ /**
1991
+ * Durable/runtime mutator helpers exposed to actor handlers.
1992
+ */
1926
1993
  interface ActorStateMutators<D extends Record<string, any>, R = AnyObject> {
1927
1994
  setDurable: (next: D | ActorStateReducer<D>) => void;
1928
1995
  patchDurable: (partial: Partial<D>) => void;
@@ -1931,6 +1998,9 @@ interface ActorStateMutators<D extends Record<string, any>, R = AnyObject> {
1931
1998
  patchRuntime: (partial: Partial<R>) => void;
1932
1999
  reduceRuntime: (reducer: ActorStateReducer<R>) => void;
1933
2000
  }
2001
+ /**
2002
+ * Combined actor state snapshot and mutators exposed to handlers.
2003
+ */
1934
2004
  interface ActorStateStore<D extends Record<string, any>, R = AnyObject> extends ActorStateMutators<D, R> {
1935
2005
  durable: D;
1936
2006
  runtime: R;
@@ -1938,11 +2008,13 @@ interface ActorStateStore<D extends Record<string, any>, R = AnyObject> extends
1938
2008
  durableVersion: number;
1939
2009
  runtimeVersion: number;
1940
2010
  }
1941
- interface ActorInitStateStore<D extends Record<string, any>, R = AnyObject> extends ActorStateMutators<D, R> {
1942
- durable: D;
1943
- runtime: R;
1944
- }
2011
+ /**
2012
+ * Context injected into `actor.task(...)` handlers.
2013
+ */
1945
2014
  interface ActorTaskContext<D extends Record<string, any>, R = AnyObject> {
2015
+ /**
2016
+ * Durable state alias retained for backwards ergonomics.
2017
+ */
1946
2018
  state: D;
1947
2019
  durableState: D;
1948
2020
  runtimeState: R;
@@ -1970,52 +2042,31 @@ interface ActorTaskContext<D extends Record<string, any>, R = AnyObject> {
1970
2042
  emit: (signal: string, payload?: AnyObject) => void;
1971
2043
  inquire: (inquiry: string, context?: AnyObject, options?: InquiryOptions) => Promise<AnyObject>;
1972
2044
  }
2045
+ /**
2046
+ * Handler signature used by `actor.task(...)`.
2047
+ */
1973
2048
  type ActorTaskHandler<D extends Record<string, any>, R = AnyObject> = (context: ActorTaskContext<D, R>) => TaskResult | ActorStateReducer<D> | Promise<TaskResult | ActorStateReducer<D>>;
1974
- interface ActorInitResult<D extends Record<string, any>, R = AnyObject> {
1975
- state?: D;
1976
- durableState?: D;
1977
- runtimeState?: R;
1978
- }
1979
- interface ActorInitContext<D extends Record<string, any>, R = AnyObject> {
1980
- baseState: D;
1981
- durableBaseState: D;
1982
- runtimeBaseState: R;
1983
- store: ActorInitStateStore<D, R>;
1984
- input: AnyObject;
1985
- actor: {
1986
- name: string;
1987
- description?: string;
1988
- key: string;
1989
- kind: ActorKind;
1990
- };
1991
- options: ActorResolvedInvocationOptions;
1992
- setState: (next: D | ActorStateReducer<D>) => void;
1993
- patchState: (partial: Partial<D>) => void;
1994
- reduceState: (reducer: ActorStateReducer<D>) => void;
1995
- setDurableState: (next: D | ActorStateReducer<D>) => void;
1996
- patchDurableState: (partial: Partial<D>) => void;
1997
- reduceDurableState: (reducer: ActorStateReducer<D>) => void;
1998
- setRuntimeState: (next: R | ActorStateReducer<R>) => void;
1999
- patchRuntimeState: (partial: Partial<R>) => void;
2000
- reduceRuntimeState: (reducer: ActorStateReducer<R>) => void;
2001
- }
2002
- type ActorInitHandler<D extends Record<string, any>, R = AnyObject> = (context: ActorInitContext<D, R>) => D | ActorInitResult<D, R> | void | Promise<D | ActorInitResult<D, R> | void>;
2003
- interface ActorRuntimeFactoryContext<D extends Record<string, any> = Record<string, any>, R = AnyObject> {
2004
- definition: ActorDefinition<D, R>;
2005
- actor: {
2006
- name: string;
2007
- key: string;
2008
- kind: ActorKind;
2009
- };
2010
- input: AnyObject;
2011
- config?: AnyObject;
2012
- }
2013
- type ActorRuntimeFactory<R = AnyObject, D extends Record<string, any> = Record<string, any>> = (context: ActorRuntimeFactoryContext<D, R>) => R | Promise<R>;
2014
- interface ActorDefinitionFactoryOptions<D extends Record<string, any> = Record<string, any>, R = AnyObject> {
2015
- isMeta?: boolean;
2016
- runtimeFactories?: Record<string, ActorRuntimeFactory<R, D>>;
2017
- initHandlers?: Record<string, ActorInitHandler<D, R>>;
2049
+ /**
2050
+ * Metadata attached to wrapped actor task functions.
2051
+ */
2052
+ interface ActorTaskRuntimeMetadata {
2053
+ actorName: string;
2054
+ actorDescription?: string;
2055
+ actorKind: ActorKind;
2056
+ mode: ActorTaskMode;
2057
+ forceMeta: boolean;
2018
2058
  }
2059
+ /**
2060
+ * Reads actor metadata from a wrapped task function if available.
2061
+ */
2062
+ declare function getActorTaskRuntimeMetadata(taskFunction: TaskFunction): ActorTaskRuntimeMetadata | undefined;
2063
+ /**
2064
+ * In-memory actor runtime.
2065
+ *
2066
+ * Actors keep durable and runtime state per resolved actor key.
2067
+ * Durable defaults are loaded from `initState`.
2068
+ * Runtime state is intentionally initialized/updated by normal actor write tasks.
2069
+ */
2019
2070
  declare class Actor<D extends Record<string, any> = AnyObject, R = AnyObject> {
2020
2071
  readonly spec: ActorSpec<D, R>;
2021
2072
  readonly kind: ActorKind;
@@ -2023,22 +2074,48 @@ declare class Actor<D extends Record<string, any> = AnyObject, R = AnyObject> {
2023
2074
  private readonly stateByKey;
2024
2075
  private readonly sessionByKey;
2025
2076
  private readonly idempotencyByKey;
2026
- private readonly initializedKeys;
2027
- private readonly initializationByKey;
2028
2077
  private nextTaskBindingIndex;
2078
+ /**
2079
+ * Creates an actor instance and optionally materializes the default key.
2080
+ */
2029
2081
  constructor(spec: ActorSpec<D, R>, options?: ActorFactoryOptions<D, R>);
2082
+ /**
2083
+ * Wraps an actor-aware handler into a standard Cadenza task function.
2084
+ */
2030
2085
  task(handler: ActorTaskHandler<D, R>, bindingOptions?: ActorTaskBindingOptions): TaskFunction;
2086
+ /**
2087
+ * Returns durable state snapshot for the resolved key.
2088
+ */
2031
2089
  getState(actorKey?: string): D;
2090
+ /**
2091
+ * Returns durable state snapshot for the resolved key.
2092
+ */
2032
2093
  getDurableState(actorKey?: string): D;
2094
+ /**
2095
+ * Returns runtime state reference for the resolved key.
2096
+ */
2033
2097
  getRuntimeState(actorKey?: string): R;
2098
+ /**
2099
+ * Alias of `getDurableVersion`.
2100
+ */
2034
2101
  getVersion(actorKey?: string): number;
2102
+ /**
2103
+ * Returns durable state version for the resolved key.
2104
+ */
2035
2105
  getDurableVersion(actorKey?: string): number;
2106
+ /**
2107
+ * Returns runtime state version for the resolved key.
2108
+ */
2036
2109
  getRuntimeVersion(actorKey?: string): number;
2110
+ /**
2111
+ * Exports this actor as a serializable definition.
2112
+ */
2037
2113
  toDefinition(): ActorDefinition<D, R>;
2114
+ /**
2115
+ * Resets one actor key or all keys.
2116
+ */
2038
2117
  reset(actorKey?: string): void;
2039
2118
  private applyRuntimeReadGuard;
2040
- private buildDefaultInvocationOptions;
2041
- private primeInitialization;
2042
2119
  private normalizeInputContext;
2043
2120
  private resolveInvocationOptions;
2044
2121
  private resolveActorKey;
@@ -2046,10 +2123,10 @@ declare class Actor<D extends Record<string, any> = AnyObject, R = AnyObject> {
2046
2123
  private resolveInitialDurableState;
2047
2124
  private resolveInitialRuntimeState;
2048
2125
  private ensureStateRecord;
2049
- private ensureInitialized;
2050
2126
  private touchSession;
2051
2127
  private runWithOptionalIdempotency;
2052
2128
  private getActiveIdempotencyRecord;
2129
+ private emitActorCreatedSignal;
2053
2130
  }
2054
2131
 
2055
2132
  interface TaskOptions {
@@ -2083,6 +2160,7 @@ declare class Cadenza {
2083
2160
  static metaRunner: GraphRunner;
2084
2161
  static registry: GraphRegistry;
2085
2162
  private static taskCache;
2163
+ private static actorCache;
2086
2164
  static isBootstrapped: boolean;
2087
2165
  static mode: CadenzaMode;
2088
2166
  /**
@@ -2123,8 +2201,7 @@ declare class Cadenza {
2123
2201
  */
2124
2202
  static validateName(name: string): void;
2125
2203
  private static resolveTaskOptionsForActorTask;
2126
- private static applyActorInitResult;
2127
- private static createActorInitHandlerFromDefinition;
2204
+ private static registerActor;
2128
2205
  /**
2129
2206
  * Executes the specified task or GraphRoutine with the given context using an internal runner.
2130
2207
  *
@@ -2166,11 +2243,25 @@ declare class Cadenza {
2166
2243
  static interval(taskName: string, context: AnyObject, intervalMs: number, leading?: boolean, startDateTime?: Date): void;
2167
2244
  static debounce(signalName: string, context: any, delayMs: number): void;
2168
2245
  static get(taskName: string): Task | undefined;
2246
+ static getActor<D extends Record<string, any> = AnyObject, R = AnyObject>(actorName: string): Actor<D, R> | undefined;
2247
+ static getAllActors<D extends Record<string, any> = AnyObject, R = AnyObject>(): Actor<D, R>[];
2169
2248
  static getRoutine(routineName: string): GraphRoutine | undefined;
2170
2249
  static defineIntent(intent: Intent): Intent;
2171
2250
  static inquire(inquiry: string, context: AnyObject, options?: InquiryOptions): Promise<any>;
2251
+ /**
2252
+ * Creates an in-memory actor runtime instance.
2253
+ *
2254
+ * Actors are not graph nodes. Use `actor.task(...)` to produce standard tasks that
2255
+ * participate in the graph like any other task.
2256
+ */
2172
2257
  static createActor<D extends Record<string, any> = AnyObject, R = AnyObject>(spec: ActorSpec<D, R>, options?: ActorFactoryOptions): Actor<D, R>;
2173
- static createActorFromDefinition<D extends Record<string, any> = AnyObject, R = AnyObject>(definition: ActorDefinition<D, R>, options?: ActorDefinitionFactoryOptions<D, R>): Actor<D, R>;
2258
+ /**
2259
+ * Creates an actor from a serializable actor definition.
2260
+ *
2261
+ * Durable bootstrap state is resolved from `state.durable.initState`.
2262
+ * For backwards compatibility, legacy `state.durable.initialState` is also accepted.
2263
+ */
2264
+ static createActorFromDefinition<D extends Record<string, any> = AnyObject, R = AnyObject>(definition: ActorDefinition<D, R>, options?: ActorFactoryOptions<D, R>): Actor<D, R>;
2174
2265
  /**
2175
2266
  * Creates and registers a new task with the specified parameters and options.
2176
2267
  * Tasks are the basic building blocks of Cadenza graphs and are responsible for executing logic.
@@ -2522,4 +2613,4 @@ declare class Cadenza {
2522
2613
  static reset(): void;
2523
2614
  }
2524
2615
 
2525
- export { Actor, type ActorConsistencyProfileName, type ActorDefinition, type ActorDefinitionFactoryOptions, type ActorFactoryOptions, type ActorInitContext, type ActorInitHandler, type ActorInitResult, type ActorInitStateStore, type ActorInvocationOptions, type ActorKeyDefinition, type ActorKind, type ActorLifecycleDefinition, type ActorLoadPolicy, type ActorRuntimeFactory, type ActorRuntimeFactoryContext, type ActorRuntimeReadGuard, type ActorSpec, type ActorStateDefinition, type ActorStateReducer, type ActorStateStore, type ActorTaskBindingDefinition, type ActorTaskBindingOptions, type ActorTaskContext, type ActorTaskHandler, type ActorTaskMode, type ActorWriteContract, type AnyObject, type CadenzaMode, type DebounceOptions, DebounceTask, type EmitOptions, EphemeralTask, type EphemeralTaskOptions, GraphContext, GraphRegistry, GraphRoutine, GraphRun, GraphRunner, type IdempotencyPolicy, InquiryBroker, type InquiryOptions, type Intent, type RetryPolicy, type Schema, type SchemaConstraints, type SchemaDefinition, type SchemaType, type SessionPolicy, SignalBroker, SignalEmitter, Task, type TaskFunction, type TaskOptions, type TaskResult, type ThrottleTagGetter, Cadenza as default };
2616
+ export { Actor, type ActorConsistencyProfileName, type ActorDefinition, type ActorFactoryOptions, type ActorInvocationOptions, type ActorKeyDefinition, type ActorKind, type ActorLoadPolicy, type ActorRuntimeReadGuard, type ActorSpec, type ActorStateDefinition, type ActorStateReducer, type ActorStateStore, type ActorTaskBindingDefinition, type ActorTaskBindingOptions, type ActorTaskContext, type ActorTaskHandler, type ActorTaskMode, type ActorTaskRuntimeMetadata, type ActorWriteContract, type AnyObject, type CadenzaMode, type DebounceOptions, DebounceTask, type EmitOptions, EphemeralTask, type EphemeralTaskOptions, GraphContext, GraphRegistry, GraphRoutine, GraphRun, GraphRunner, type IdempotencyPolicy, InquiryBroker, type InquiryOptions, type Intent, type RetryPolicy, type Schema, type SchemaConstraints, type SchemaDefinition, type SchemaType, type SessionPolicy, SignalBroker, SignalEmitter, Task, type TaskFunction, type TaskOptions, type TaskResult, type ThrottleTagGetter, Cadenza as default, getActorTaskRuntimeMetadata };