Skip to main content

Capabilities

A capability is a named bundle of resources, state, helper functions, prompt context and tools that a block lists in uses to receive all of it. A block is the unit of work in a flow, a generator is the block that calls a model, and a resource is named, stored data a block declares.

This page lists everything a capability can declare, what a block that lists it gets, and what the build refuses. Capabilities explains what a capability is for.

The examples use the orders capability of a support desk: a user-scoped customerOrders collection of the customer's orders, a refundsEnabled session state field, a context preset and a tools preset holding lookUpOrder, a handler block the model calls as a tool to read an order's status from that collection.

src/flows/support/resources/orders.ts
import { defineResourceCollection } from "@flow-state-dev/core";
import { z } from "zod";

export const customerOrders = defineResourceCollection({
pattern: "orders/*",
scope: "user",
stateSchema: z.object({ status: z.string() }),
});
src/flows/support/tools/look-up-order.ts
import { handler } from "@flow-state-dev/core";
import { z } from "zod";
import { customerOrders } from "../resources/orders";

export const lookUpOrder = handler({
name: "lookUpOrder",
inputSchema: z.object({ orderId: z.string() }),
outputSchema: z.object({ status: z.string() }),
resources: { orders: customerOrders },
execute: async (input, ctx) => {
const order = await ctx.resources.orders.getOptional(input.orderId);
return { status: order?.state.status ?? "No order with that number." };
},
});
src/flows/support/capabilities/orders.ts
import { defineCapability } from "@flow-state-dev/core";
import { z } from "zod";
import { customerOrders } from "../resources/orders";
import { lookUpOrder } from "../tools/look-up-order";

export const orders = defineCapability({
name: "orders",
resources: { orders: customerOrders },
sessionStateSchema: z.object({ refundsEnabled: z.boolean().default(false) }),
presets: {
context: { context: "Quote the order number whenever you mention an order." },
tools: { tools: [lookUpOrder] },
default: ["context", "tools"],
},
});
src/flows/support/blocks/draft-reply.ts
import { generator } from "@flow-state-dev/core";
import { z } from "zod";
import { orders } from "../capabilities/orders";

export const draftReply = generator({
name: "draftReply",
model: "openai/gpt-6-luna",
prompt: "Answer the customer's question about their order.",
inputSchema: z.string(),
user: (input) => input,
uses: [orders],
});

Everything on this page imports from @flow-state-dev/core. A capability is code shared by every block that lists it. A capability is not a kit: a kit is installed by the app, and a capability is listed by a block.

Lookups​

QuestionAnswer
Can a dynamic uses entry install a resource?No. Declare the resource on the capability and list that capability statically. Static and dynamic entries
How are presets turned off?orders.presets({ tools: false }) or orders.with({ tools: false }). Each returns a new reference and leaves orders as it was. Which presets are on
Why did my generator lose the capability's tools?It declares tools:. Tools a generator is given
Why is ctx.cap.<name> missing?The capability has no fns, or the entry is dynamic. fns
Where does .with() send a key?To a preset, to config, or it throws. Routing
Why is a field missing from ctx's type?It came from a preset or config, or from a dependency. Type helpers

defineCapability fields​

defineCapability(config) returns the capability. The returned object is what a block lists in uses. Reuse one object across blocks. Two different defineCapability calls with one name are an error only when both reach one block's uses, directly or through a dependency. One object listed twice is one install.

Parts every block that lists the capability receives:

FieldTypeWhat it does
namestring, requiredNames the capability. Empty or whitespace-only throws defineCapability() requires a non-empty name. It is also the key of the capability's helpers on ctx.cap.
resourcesmap of accessor to defineResource(...) or collectionResources installed on the block. Scope is set on each resource, not on the capability. Resources
sessionStateSchema, userStateSchema, orgStateSchemaZod schemaFields added to that scope's state. Listed statically, they reach the flow's scope state defaults. Flows and actions
requestStateSchemaZod schemaAccepted and ignored.
sequencerStateSchemaZod objectFields the capability needs on a sequencer's state. Only a sequencer accepts it. Blocks
stateSchemaZod objectThe block's own state, ctx.self. Valid on every block kind. Blocks
targetStateSchemasmap of name to Zod schemaTyped handles to named ancestor blocks' state, ctx.targets. Valid on every block kind. Blocks

Helpers, composition and filtering:

FieldTypeWhat it does
fns(ctx: BlockContext) => Record<string, function>Helper functions at ctx.cap.<name>. Presets do not control it.
usesreadonly UsesEntry[]Capabilities this one depends on. Static entries install with it; function entries are dynamic.
itemVisibilityItemVisibility or an arrayAttaches the capability only to blocks whose itemVisibility is one of these. Default: every block. A block that sets no itemVisibility gets the default on Visibility.

Presets and config:

FieldTypeWhat it does
presetsmap of name to PresetDef, plus default?: string[]Named bundles a block turns on or off.
config{ schema?, resolve }A typed value a block passes with .config(value), and the resolver that maps it onto the block.

The type-only fields sessionStateType, resourcesType, targetStatesType, sequencerStateType and stateSchemaType are on Type helpers.

context, tools, controlTools, history, model, providerOptions and caching are not top-level fields. They go in a preset, or come from a config resolver.

A capability name containing a colon is valid. A factory that names its capability orders:session lands its helpers at ctx.cap["orders:session"], which is not dot-accessible.

A preset name and a config field name must not overlap. defineCapability throws at definition:

defineCapability("<name>"): preset name(s) [<names>] collide with config field name(s). .with() routes a flat bag by preset-name membership, so the two sets must be disjoint — rename the preset or the config field.

What a preset can declare​

A preset is a partial block configuration. A config resolver returns the same shape, and so does a preset override function.

Keys any block accepts:

KeyTypeWhat it does
resourcesmap of accessor to resourceInstalls resources.
sessionStateSchema, userStateSchema, orgStateSchema, stateSchema, targetStateSchemasas in the field tableSame as the top-level field, except that a scope state schema from a preset does not reach the flow's defaults.
requestStateSchemaZod schemaAccepted and ignored.
sequencerStateSchemaZod objectSame as the top-level field. Sequencer only.

Keys only a generator accepts:

KeyTypeWhat it does
contexta string, a ContextObject, a function (input, ctx) => string | ContextObject | null | undefined, or an array of theseAdded after the block's own context entries. How context is assembled
toolsGeneratorTool[] or (ctx) => GeneratorTool[] | Promise<...>Tools the model may call. Dropped when the generator declares tools:.
controlToolssame as toolsTools that reach the model whatever the generator's tools: says.
historyItemQuery or (ctx) => ItemQuery | Promise<ItemQuery>A query added to every ctx.session.items.history() read the generator makes. How it merges
model, providerOptions, cachingthe generator's own typesFill the generator's setting when the block sets none. The last capability wins. The block's own setting always wins.

A block kind that cannot take a key throws when the block is built, and Blocks lists which kinds take which. Generators covers model.

A capability's top-level fields merge first, then each active preset in declaration order, then the config resolver's output. For a setting that holds one value, such as model, config overrides a preset.

Which presets are on​

QuestionAnswer
Which are on with no override?The names in default, or every preset when default is absent. default: [] turns all of them off.
How is one turned off?cap.presets({ name: false }) or cap.with({ name: false }).
How is one turned on?cap.presets({ name: true }), or a function override.
Does turning one off remove the capability's top-level resources, state or fns?No. A preset that itself declares resources or state takes those away when it is off.
What does a preset do to ctx.cap helpers?Nothing.
What if the name is unknown?A TypeScript caller gets a compile error, because the override keys are the declared names. At runtime .presets() returns a reference, and the block that lists the capability throws when it is built: Unknown preset "<name>" on capability "<cap>".

When one block lists a capability in two places, the first reference reached supplies the presets. Dependencies are reached before their dependents, depth first. [orders.presets({ context: false }), orders] merges only tools. [orders, orders.presets({ context: false })] merges both.

Preset override forms​

FormExampleEffect
Boolean{ tools: false }Turns the preset off or on.
Function{ tools: (preset) => ({ tools: [other] }) }Receives the preset's declared object and returns a partial, spread over the preset as { ...preset, ...returned }. It also turns the preset on, including one not in default.

A function override is typed as that preset's PresetDef. It cannot take a value from the consumer or see which other presets are on. Config does that.

.presets(), .config() and .with()​

.presets() and .config() always return a new reference. .with() does too, except that .with({}) on a capability with no config returns the capability itself. None of them changes the original. They chain in either order. Two references to one capability install it once. Different .config() values throw; different .presets() take the first reached.

BuilderTakesCompile-time ruleWhat it does
.presets(overrides){ presetName: boolean | fn }Keys are the declared preset names.Records overrides.
.config(value)The config schema's input type; the resolver parameter's type when schemalessOn a capability with no config, the call fails to compile.Records the value.
.with(bag)A flat bagThe config input intersected with the preset overrides; either alone when the other is absent..presets and .config in one call.

.with({ allowed, dynamicActivation: true }) equals .config({ allowed }).presets({ dynamicActivation: true }).

.with() routing​

  1. A bag that is not a plain object (a scalar, an array, null) is the whole config value. No presets ride with it.
  2. A key naming a preset, any key of presets except default, is a preset override.
  3. Any other key is config, when the capability declares config and the key is a field of its closed object schema, or when the schema is open, meaning it has no fixed set of keys: passthrough, scalar, array or schemaless.
  4. A key that is neither throws at the .with() call, before any schema parses the value, naming the unmatched keys, the presets and the config fields. It throws when the capability has no config, and when config.schema is a strip or strict object and the key is not one of its fields. An open schema passes the key through.
  5. A bag that holds only preset keys writes no config. The exception is {} on a capability with config, which writes an empty config, so .with({}) equals .config({}).

Config​

Config lets a block pass a typed value to a capability. The capability decides what the value means.

src/flows/support/capabilities/order-policy.ts
import { defineCapability } from "@flow-state-dev/core";
import { z } from "zod";

export const orderPolicy = defineCapability({
name: "orderPolicy",
config: {
schema: z.object({ refundLimitCents: z.number().default(5000) }).default({}),
resolve: (config) => ({
context: `Refunds above ${config.refundLimitCents} cents need a manager.`,
}),
},
});

A block lists orderPolicy.config({ refundLimitCents: 2000 }), or orderPolicy alone for the default.

PieceWhat it is
schemaOptional Zod schema. It validates the value given to .config() and types it: the .config() argument is z.input, the resolver's first parameter is z.output. A .default() field is optional at the call and present in the resolver.
resolve(config, ctx)Maps the validated value onto a partial block surface, the keys of a preset, merged like one. It runs when a block that lists the capability is built, so keep it pure. A throw becomes Config resolver for capability "<name>" threw: <message>. It obeys the block-kind rules of a preset, and the error names config as the source.
ctx.presetsReadonlySet<string>: the capability's presets that are on for this block. The resolver decides whether a value replaces or adds to what a preset contributes.
ctx.blockKindThe kind of the block being built.
SituationResult
No .config() call, and the schema accepts undefinedAllowed. Use z.object({ ... }).default({}): a plain z.object rejects undefined even when every field is optional.
No .config() call, and the schema rejects undefined or there is noneThrows at build: Capability "<name>" requires configuration: call .config(...) on it (its config schema does not accept an absent value). Without a schema: Capability "<name>" declares schemaless config and must be used with .config(...).
A value of the wrong shapeThrows at build: Invalid config for capability "<name>": <zod message>
One capability listed twice with different configThrows Conflicting .config() for capability "<name>": .... Identical config is allowed. Configs compare by what the schema parses to, so omitting a defaulted field is not a conflict. With no schema, a bare reference against a configured one is a conflict, and two configured ones compare by value.
Reached through a dynamic uses function, directly or inside a dynamically returned capability's own usesThrows when the function runs, at request time and not at build: Capability "<name>" declares open config but was reached through a dynamic uses resolver, which is not supported. Use it on a static uses entry.

Tuning a capability​

WayChangesTakes a value from the consumer
Function preset overrideOne named presetNo
ConfigWhatever the resolver mapsYes, typed
A factory, createXCapability(options)The capability's structure, such as a resource's scope, when it is builtYes, at build

Static and dynamic uses entries​

uses on a block or a capability is readonly UsesEntry[]. An entry is a capability (static) or (ctx: BlockContext) => readonly CapabilityRef[] (dynamic).

src/flows/support/capabilities/refund-rules.ts
import { defineCapability } from "@flow-state-dev/core";

export const refundRules = defineCapability({
name: "refundRules",
presets: {
context: { context: "Refunds above 5000 cents need a manager." },
default: ["context"],
},
});
src/flows/support/blocks/draft-reply.ts
import { generator, type BlockContext, type CapabilityRef } from "@flow-state-dev/core";
import { z } from "zod";
import { orders } from "../capabilities/orders";
import { refundRules } from "../capabilities/refund-rules";

export const draftReply = generator({
name: "draftReply",
model: "openai/gpt-6-luna",
prompt: "Answer the customer's question about their order.",
inputSchema: z.string(),
user: (input) => input,
uses: [
orders,
(ctx: BlockContext): CapabilityRef[] => (ctx.session.state.refundsEnabled === true ? [refundRules] : []),
],
});

refundRules is a capability with a context preset. The dynamic entry runs on every run of draftReply and adds it only while the session's refundsEnabled field is true.

Static entryDynamic entry
When it is readOnce, when the block is builtEvery run of the block, with that run's ctx
resourcesInstalled, and bubbled up to the flowNot installed
State schemas, stateSchema, targetStateSchemas, sequencerStateSchemaInstalledNot installed
fns at ctx.cap.<name>BuiltNot built
configAllowedThrows when the function runs
Static entryDynamic entry
context, tools, controlTools, history presetsMerged at buildResolved each run from the capability's active presets, and those of its static uses dependencies
model, providerOptions, cachingMergedNot read
itemVisibility filterAppliedApplied to each returned capability
Types on ctxReflectedContribute nothing

List a resource-bearing capability statically beside the dynamic entry.

A dynamic entry on a handler, router, sequencer or evaluator does nothing: only a generator collects dynamic context, history and tools. A dynamic function inside a capability's own uses is collected on a generator and follows the same rules.

What each part installs​

"At build" means when the block that lists the capability is constructed, before any request. A refusal there is a thrown error at the line that builds the block.

PartLands onWhenIf two sources declare the same thing
resourcesThe block's declared resources, which bubble through sequencers and routers to the flow, so a flow that includes the block declares nothing.At buildA conflict throws. Resources
sessionStateSchema, userStateSchema, orgStateSchemaThe flow's scope state defaults. A schema from a preset, a config resolver or a dynamic entry does not reach them.At defineFlowTwo declarations of a field that disagree on type or default are refused, naming both. Flows and actions
stateSchemaThe block's own state, ctx.selfAt buildA conflict throws. Blocks
targetStateSchemasctx.targetsAt buildA conflict throws. Blocks
sequencerStateSchemaThe sequencer's state, alongside its own stateSchemaAt buildA mismatch throws. Blocks
fnsctx.cap.<name>, built from fns(ctx) with the running block's ctxWhen the block runs, once per run. A block inside another block that lists the same capability receives the outer block's helper object, built with the outer block's ctx.Two capabilities with one name: an error.
PartLands onWhenIf two sources declare the same thing
contextThe generator's context, after the block's own entriesAt build when static, each run when dynamicEntries concatenate. Generators
toolsThe generator's tool list, unless it declares tools:Each runAll are included. Generators
controlToolsThe generator's tool list, alwaysEach runAll are included.
historyThe generator's history readsEach runQueries merge. Generators
model, providerOptions, cachingThe generator, when the block sets noneAt buildThe last capability wins. The block's own wins.

The block's own declaration wins only for model, providerOptions, caching and the keys of a history read. A resource accessor and an own-state field declared by both a capability and the block must share one reference, or the build throws. A target declared by two capabilities must too. A target a capability and the block both declare is not checked: the block's own schema wins in the type, and conflicting types collapse to never.

Composition and refusals​

SituationResult
A capability lists dependencies in usesThey install first, transitively. A dependency reached by two paths installs once, with the presets of the first reference reached.
A cycleThrows at build: Capability cycle detected: "<name>" depends on itself (transitively).
Two defineCapability() calls with one nameThrows: Capability name collision: "<name>" is declared by two different defineCapability() calls.
One reference listed twice with different .config()Throws.
One reference listed twice with different .presets()The first reached wins.
A capability's itemVisibility does not match the block'sThe capability is dropped for that block, with all its parts including tools. A dependency in its uses is checked against its own itemVisibility, not dropped with it.

When each refusal fires​

WhereWhat throws
The defineCapability callAn empty name, and a preset name that overlaps a config field. The overlap check covers object config schemas only.
The .with() callA key that matches neither a preset nor a config field.
The build of a blockUnknown preset, block-kind mismatch, missing or invalid config, conflicting config, cycle, name collision, and every merge conflict above.
defineFlowScope state fields two declarations disagree on.
A dynamic function runningA capability with config reached through it.

What a capability stores​

A capability keeps no data. It declares shapes, and the data lives where each part's scope puts it.

PartWhere its data lives
resourcesThe store the resource's scope names. Resources
Session, user and org state fieldsThe scope's record, one per session, user or org. Flows and actions
stateSchema, sequencerStateSchemaThe block's or the sequencer's own state. Blocks
targetStateSchemas, fns, .config(value)Nowhere. Targets point at an ancestor block's state, helpers are built per block run, and a config value travels with the .config() reference in app code.

Next​

  • Capabilities, the idea and a worked example.
  • Write a capability, a step by step guide to making one.
  • Generators, the tools: option and how context and history are assembled.
  • Type helpers, what uses reflects into a block's types.
  • Resources, the scope and options of the data a capability installs.
  • Memory, the capability that ships with the framework.