Skip to main content

@flow-state-dev/core/capability

Interfaces​

CapabilityConfig​

Type Parameters​

TName​

TName extends string = string

TFns​

TFns extends Record<string, (...args) => any> = Record<string, (...args) => any>

TSessionStateSchema​

TSessionStateSchema extends ZodTypeAny | undefined = undefined

TResources​

TResources extends | Record<string, DeclaredResourceEntry> | undefined = undefined

TTargetSchemas​

TTargetSchemas extends Record<string, ZodTypeAny> | undefined = undefined

TSequencerStateSchema​

TSequencerStateSchema extends ZodTypeAny | undefined = undefined

TOwnStateSchema​

TOwnStateSchema extends ZodTypeAny | undefined = undefined

Properties​

config?​
config?: CapabilityConfigDef<any, InferSessionState<TSessionStateSchema>>;

Open config — a typed value the capability accepts via .config(), plus a resolver mapping it onto a block surface. Complements presets; the exact .config() argument / resolver-param types are refined by defineCapability.

fns?​
fns?: (ctx) => TFns;
Parameters​
ctx​

CapabilityFnsContext<TResources>

Returns​

TFns

itemVisibility?​
itemVisibility?:
| ItemVisibility
| readonly ItemVisibility[];

Restrict this capability to blocks with a matching itemVisibility.

Omitted (default): the capability attaches to every block that declares it via uses. Set to an ItemVisibility or array to filter to an allowlist — the capability only attaches when the consuming block's itemVisibility matches (deep equal). A block with no itemVisibility is treated as { client: true, history: true } for this check.

name​
name: TName;
orgStateSchema?​
orgStateSchema?: ZodTypeAny;
presets?​
presets?: Record<string,
| string[]
| PresetDef<InferSessionState<TSessionStateSchema>>> & object;
Type Declaration​
default?​
default?: string[];
requestStateSchema?​
requestStateSchema?: ZodTypeAny;
resources?​
resources?: TResources extends Record<string, DeclaredResourceEntry> ? TResources : Record<string, DeclaredResourceEntry>;

Flat resource map — accessor key → resource definition.

resourcesType?​
resourcesType?: TResources extends Record<string, DeclaredResourceEntry> ? unknown : never;

Type-only escape hatch for resources. See sessionStateType.

sequencerStateSchema?​
sequencerStateSchema?: TSequencerStateSchema extends ZodTypeAny ? TSequencerStateSchema : ZodTypeAny;
sequencerStateType?​
sequencerStateType?: TSequencerStateSchema extends ZodTypeAny ? unknown : never;

Type-only escape hatch for sequencer state. See sessionStateType.

sessionStateSchema?​
sessionStateSchema?: TSessionStateSchema;
sessionStateType?​
sessionStateType?: TSessionStateSchema extends ZodTypeAny ? unknown : never;

Type-only escape hatch. When the inferred type from sessionStateSchema is too loose (e.g. nested z.record or z.any), assert the exact ctx shape consumers should see here. Carries no runtime value.

Setting this without sessionStateSchema is a type error — schemas remain the source of truth for runtime validation.

stateSchema?​
stateSchema?: TOwnStateSchema extends ZodTypeAny ? TOwnStateSchema : ZodTypeAny;

Own-state contribution — see PresetDef.stateSchema.

stateSchemaType?​
stateSchemaType?: TOwnStateSchema extends ZodTypeAny ? unknown : never;

Type-only escape hatch for own state. See sessionStateType.

targetStateSchemas?​
targetStateSchemas?: TTargetSchemas extends Record<string, ZodTypeAny> ? TTargetSchemas : Record<string, ZodTypeAny>;
targetStatesType?​
targetStatesType?: TTargetSchemas extends Record<string, ZodTypeAny> ? unknown : never;

Type-only escape hatch for target state handles. See sessionStateType.

userStateSchema?​
userStateSchema?: ZodTypeAny;
uses?​
uses?: readonly (
| CapabilityRef
| ((ctx) => readonly CapabilityRef[]))[];

CapabilityConfigDef​

Open, typed configuration a capability accepts via .config(value). The author supplies a resolve function that maps a validated config value onto the same partial block surface a preset contributes, merged through the same pipeline. Distinct from presets (predefined on/off bundles): config carries values, and the capability owns what they mean.

TConfigOut is the resolver's input — the parsed schema output (z.output) when schema is set, or the explicit type when schemaless.

Type Parameters​

TConfigOut​

TConfigOut = any

TSessionState​

TSessionState = any

Properties​

resolve​
resolve: (config, ctx) => Partial<PresetDef<TSessionState>>;

Maps validated config → a partial block surface, merged like a preset. Runs at block-build time, before the merged surface is block-kind validated, so it must be pure (value → surface) with no side effects.

Parameters​
config​

TConfigOut

ctx​

CapabilityConfigResolveCtx

Returns​

Partial<PresetDef<TSessionState>>

schema?​
schema?: ZodTypeAny;

Optional Zod schema. Validates the value passed to .config() and types both the .config() argument (z.input) and the resolver's config parameter (z.output). A capability meant to be usable without .config() must declare a schema that accepts undefined at the top level — e.g. z.object({ ... }).default({}); optional fields alone do not make the object optional. Omit schema for a schemaless config typed only by the resolver's config parameter; schemaless config makes .config() required.


CapabilityConfigResolveCtx​

Context handed to a capability's config resolver at block-build time. Lets the resolver see which of the capability's own presets are active for this block, so it can own override-vs-add semantics per setting, and the kind of block it is contributing to.

Properties​

blockKind​
blockKind: BlockKind;

The consuming block's kind.

presets​
presets: ReadonlySet<string>;

Names of this capability's presets active for the block being built.


FlattenResult​

Result of flattening capabilities — static refs + collected dynamic resolvers.

Properties​

dynamicResolvers​
dynamicResolvers: DynamicUsesResolver[];

Dynamic uses entries collected from all traversed capabilities.

staticRefs​
staticRefs: CapabilityRef[];

Deduplicated, dependency-ordered static capability refs.

Type Aliases​

DynamicUsesResolver​

type DynamicUsesResolver = (ctx) => readonly CapabilityRef[];

Dynamic uses entry — a function that resolves capabilities at runtime.

Parameters​

ctx​

any

Returns​

readonly CapabilityRef[]


InferCapabilityOwnState​

type InferCapabilityOwnState<TUses> = [Extract<TUses[number], CapabilityRef>] extends [never] ? object : Prettify<UnionToIntersection<InferCapabilityOwnStateEntry<Extract<TUses[number], CapabilityRef>>>>;

Infer the merged own-state shape (ctx.self) contributed by static capabilities. Same shape as session state — z.infer of stateSchema per capability, with stateSchemaType as an override. Consuming block factories intersect this with their own declared stateSchema (block-own on the LEFT — see e.g. HandlerConfig.TSelfState).

Type Parameters​

TUses​

TUses extends readonly UsesEntry[]


InferCapabilityResources​

type InferCapabilityResources<TUses> = [Extract<TUses[number], CapabilityRef>] extends [never] ? object : Prettify<UnionToIntersection<InferCapabilityResourcesEntry<Extract<TUses[number], CapabilityRef>>>>;

Infer the merged ctx.resources shape contributed by static capabilities. Each capability contributes its resources map mapped to ResourceRef / ResourceCollectionRef handles, unless its resourcesType escape hatch is set. Multi-capability arrays are intersected.

Type Parameters​

TUses​

TUses extends readonly UsesEntry[]


InferCapabilitySequencerState​

type InferCapabilitySequencerState<TUses> = [Extract<TUses[number], CapabilityRef>] extends [never] ? object : Prettify<UnionToIntersection<InferCapabilitySequencerStateEntry<Extract<TUses[number], CapabilityRef>>>>;

Infer the merged sequencer state shape contributed by static capabilities. Same shape as session state — z.infer of sequencerStateSchema per cap, with sequencerStateType as an override.

Type Parameters​

TUses​

TUses extends readonly UsesEntry[]


InferCapabilitySessionState​

type InferCapabilitySessionState<TUses> = [Extract<TUses[number], CapabilityRef>] extends [never] ? object : Prettify<UnionToIntersection<InferCapabilitySessionStateEntry<Extract<TUses[number], CapabilityRef>>>>;

Infer the merged ctx.session.state shape contributed by static capabilities in a uses array. Each capability contributes the z.infer of its sessionStateSchema, unless its sessionStateType escape hatch is set, in which case that exact type is used instead. Multi-capability arrays are intersected. Empty arrays and arrays with no static refs resolve to {}.

Type Parameters​

TUses​

TUses extends readonly UsesEntry[]


InferCapabilityTargets​

type InferCapabilityTargets<TUses> = [Extract<TUses[number], CapabilityRef>] extends [never] ? object : Prettify<UnionToIntersection<InferCapabilityTargetsEntry<Extract<TUses[number], CapabilityRef>>>>;

Infer the merged ctx.targets shape contributed by static capabilities. Each capability contributes typed StateRef handles for each declared target, unless its targetStatesType escape hatch is set.

Type Parameters​

TUses​

TUses extends readonly UsesEntry[]


MergedCapabilitySurface​

type MergedCapabilitySurface = object;

Accumulator for merged capability surfaces.

Properties​

caching​
caching: ResolvableCachingConfig<any, any> | undefined;
contextEntries​
contextEntries: PresetContextEntry[];
controlToolEntries​
controlToolEntries: (
| GeneratorTool[]
| ((ctx) =>
| GeneratorTool[]
| Promise<GeneratorTool[]>))[];

Fence-exempt control tools — see PresetDef.controlTools.

historyEntries​
historyEntries: NonNullable<PresetDef["history"]>[];

History query entries — see PresetDef.history. In merge order.

model​
model: ResolvableModel<any, any> | undefined;
orgStateSchema​
orgStateSchema: ZodTypeAny | undefined;
providerOptions​
providerOptions: ResolvableProviderOptions<any, any> | undefined;
requestStateSchema​
requestStateSchema: ZodTypeAny | undefined;
resources​
resources:
| Record<string, DeclaredResourceEntry>
| undefined;
sequencerStateSchema​
sequencerStateSchema: ZodTypeAny | undefined;
sessionStateSchema​
sessionStateSchema: ZodTypeAny | undefined;
stateSchema​
stateSchema: ZodTypeAny | undefined;

Own-state contribution, merged across capabilities.

targetStateSchemas​
targetStateSchemas: Record<string, ZodTypeAny> | undefined;
toolEntries​
toolEntries: (
| GeneratorTool[]
| ((ctx) =>
| GeneratorTool[]
| Promise<GeneratorTool[]>))[];
userStateSchema​
userStateSchema: ZodTypeAny | undefined;

PresetOverrideFn​

type PresetOverrideFn<TPreset> = (preset) => Partial<TPreset>;

Function-form override receives the preset's declared object and returns a possibly-modified version. Runs at block factory time.

Type Parameters​

TPreset​

TPreset extends PresetDef

Parameters​

preset​

TPreset

Returns​

Partial<TPreset>

Functions​

extractMergedResources()​

function extractMergedResources(merged): DeclaredResources | undefined;

Extract a DeclaredResources object from the merged surface, suitable for passing to buildBlock().

Parameters​

merged​

MergedCapabilitySurface

Returns​

DeclaredResources | undefined


flattenCapabilities()​

Call Signature​

function flattenCapabilities(refs): CapabilityRef[];

Flatten a list of CapabilityRef entries into a deduplicated, transitively resolved list. Detects cycles. Returns capabilities in dependency order (dependencies before dependents).

Dynamic entries (functions) encountered during traversal — either at the top level or nested inside a capability's uses — are collected separately. They contribute context, tools and control tools at runtime, not resources at build time.

Parameters​
refs​

readonly CapabilityRef[]

Returns​

CapabilityRef[]

Call Signature​

function flattenCapabilities(refs, options): FlattenResult;

Flatten a list of CapabilityRef entries into a deduplicated, transitively resolved list. Detects cycles. Returns capabilities in dependency order (dependencies before dependents).

Dynamic entries (functions) encountered during traversal — either at the top level or nested inside a capability's uses — are collected separately. They contribute context, tools and control tools at runtime, not resources at build time.

Parameters​
refs​

readonly CapabilityRef[]

options​
collectDynamic​

true

Returns​

FlattenResult


getBaseCapability()​

function getBaseCapability(ref): DefinedCapability;

Recover the base defineCapability() reference from a potentially configured capability — one that had .presets() and/or .config() called on it. Both builders produce a clone exactly one hop from the base, so a single Object.getPrototypeOf recovers it. A ref is configured iff it carries an own __presetOverrides or __config.

Parameters​

ref​

CapabilityRef

Returns​

DefinedCapability


mergeCapabilities()​

function mergeCapabilities(
caps,
blockKind,
blockName?
): MergedCapabilitySurface;

Merge all flattened capabilities (required + active presets + open config) into a MergedCapabilitySurface. The caller is responsible for flattening first.

Parameters​

caps​

readonly CapabilityRef[]

blockKind​

BlockKind

blockName?​

string

The consuming block, for the sequencer-state conflict error.

Returns​

MergedCapabilitySurface


mergeCapabilityOwnStateWithBlock()​

function mergeCapabilityOwnStateWithBlock(
capStateSchema,
blockStateSchema,
blockName
): ZodTypeAny | undefined;

Merge the capability-contributed own-state schema (from mergeCapabilities) with a block's own declared stateSchema. A same-name field declared by both must be the same schema reference or this throws — see mergeOwnStateShapes. Mirrors mergeWithBlockResources's capability-then- block shape (and its reference-equality collision rule).

Parameters​

capStateSchema​

ZodTypeAny | undefined

blockStateSchema​

ZodTypeAny | undefined

blockName​

string

Returns​

ZodTypeAny | undefined


mergeSurfaceInto()​

function mergeSurfaceInto(
acc,
surface,
blockKind,
capName,
presetName,
sourceKind?,
blockName?
): void;

Merge a single surface (required or preset) into the accumulator. Validates block-kind compatibility and throws clear errors.

Parameters​

acc​

MergedCapabilitySurface

surface​

Partial<PresetDef>

blockKind​

BlockKind

capName​

string

presetName​

string

sourceKind?​

"config" | "preset"

blockName?​

string

The consuming block, named when two capabilities' sequencerStateSchema disagree on a field's kind.

Returns​

void


mergeWithBlockResources()​

function mergeWithBlockResources(capResources, blockResources): DeclaredResources | undefined;

Merge the capability surface's resources with the block's own declared resources. Block-level wins on dedup (same reference = ok).

Parameters​

capResources​

DeclaredResources | undefined

blockResources​

DeclaredResources | undefined

Returns​

DeclaredResources | undefined


resolveActivePresets()​

function resolveActivePresets(cap): object[];

Resolve which presets are active for a capability, given any overrides applied via .presets(). Returns the final list of preset definitions (with function-form overrides applied) ready for surface merging.

Parameters​

cap​

CapabilityRef

Returns​

object[]

References​

CapabilityPresetCtx​

Re-exports CapabilityPresetCtx


CapabilityRef​

Re-exports CapabilityRef


ConfigArgOf​

Re-exports ConfigArgOf


ConfiguredCapability​

Re-exports ConfiguredCapability


defineCapability​

Re-exports defineCapability


DefinedCapability​

Re-exports DefinedCapability


InferCapabilities​

Re-exports InferCapabilities


PresetContextEntry​

Re-exports PresetContextEntry


PresetDef​

Re-exports PresetDef


PresetOverrides​

Re-exports PresetOverrides


UsesEntry​

Re-exports UsesEntry


UsesSlot​

Re-exports UsesSlot


WithArg​

Re-exports WithArg