Resources
A resource is named, stored data, a bit like a file. It can hold a body of content, and it can also carry typed state of its own. Each write to that state is atomic, and incState and updateState apply a change to whatever is stored, so two requests adding to it at once both land. It is for things that have a name of their own, like a refund policy every reply should follow, or the notes piling up on a support ticket. A collection is a set of them.
A block is one piece of work in a flow, one of the steps your app calls through its actions. Declare a resource on the block that needs it, and the framework stores it and hands the block a handle to it, kept apart from session state and from other resources. When any block in the flow should reach it, rather than one block in particular, declare it on the flow instead. The examples are a support desk. refundPolicy is a document, draftReply is the block that drafts each reply, and ticketNotes is a collection.
A flow also keeps session state, the typed fields on each session's record. The figure sets the two side by side.
A resource a block declares
defineResource takes a scope, a schema for the resource's typed fields, and its starting content. The scope says whose it is: one session, one user, or one org. The refund policy is the same for everyone, so it is org-scoped.
import { defineResource } from "@flow-state-dev/core";
import { z } from "zod";
export const refundPolicy = defineResource({
scope: "org",
stateSchema: z.object({ version: z.string().default("v1") }),
content: "Refunds are issued within 30 days of purchase. After that, offer store credit.",
});
A block declares it in its resources map, and reads it from ctx.resources.
import { generator } from "@flow-state-dev/core";
import { z } from "zod";
import { refundPolicy } from "../resources/refund-policy";
export const draftReply = generator({
name: "draft-reply",
model: "openai/gpt-6-luna",
prompt: "You answer customer support messages. Follow the refund policy you are given.",
inputSchema: z.string(),
resources: { refundPolicy },
user: async (input, ctx) => {
const policy = await ctx.resources.refundPolicy.readContent();
return `Refund policy:\n${policy ?? ""}\n\nCustomer message:\n${input}`;
},
});
The key in resources is the name on ctx.resources, and a name the block did not declare is a type error. readContent() returns the body. The resources reference lists every method on the handle.
State or a resource?
Session state is for flags, counters and small settings. Every block in the flow reads its fields by name from one shared set. The reference covers what happens when two blocks declare the same field differently.
A resource has its own name and its own handle, so it cannot collide with another resource's fields. Reach for one when the data has a body of content, needs an identity of its own, or has a nested structure.
Both live on the same three scopes. The refund policy is a resource, and org scope fits: it is one document for every user. User scope would give each user their own, and session scope would give each session its own. At user and org scope, flows that serve the same user or org share a resource by default; the reference covers keeping one flow's copy apart.
Collections
Ticket notes pile up, and you cannot say how many. A collection is a typed set of resources created while the app runs.
import { defineResourceCollection } from "@flow-state-dev/core";
import { z } from "zod";
export const ticketNotes = defineResourceCollection({
pattern: "ticketNotes/**",
scope: "session",
stateSchema: z.object({ author: z.string(), pinned: z.boolean().default(false) }),
});
import { handler } from "@flow-state-dev/core";
import { z } from "zod";
import { ticketNotes } from "../resources/ticket-notes";
export const addNote = handler({
name: "add-note",
inputSchema: z.object({ key: z.string(), author: z.string(), text: z.string() }),
outputSchema: z.object({ path: z.string() }),
resources: { notes: ticketNotes },
execute: async (input, ctx) => {
const note = await ctx.resources.notes.create(input.key, { author: input.author });
await note.writeContent(input.text);
return { path: note.path };
},
});
The pattern says every note's key sits under ticketNotes, at any depth. The name you give in resources, here notes, is how the block reaches the collection. create makes a note and returns its handle, which has its own state and content like any resource. It throws if a note with that key already exists, so a second message on ticket T-1042 would fail here. When the note may already be there, call getOrCreate or upsert instead. get(key) rejects when the key is missing, and getOptional(key) returns undefined. The reference covers reading notes back and the rest of the collection methods.
If the set is small and fixed, say three attachments per ticket, one resource with an array in its state is simpler.
Who can read it: the model, your code, the browser
Your code reads a resource through ctx.resources, in any block that declares it.
The model reads nothing by default. draftReply passed the policy into its prompt itself. Set llmReadable or llmWritable on a resource, and give the generator the resource tools, readResourceContentTool() and writeResourceContentTool(), and its model can read or write it. Set them on a collection and they apply to every note in it. The reference has the options.
The browser reads nothing by default either. A client config on the resource opens the body, and expose picks which state fields the page sees. The reference lists the options.
It travels with the block
A block's declaration goes wherever the block goes. A sequencer, a block that runs other blocks in order, collects the declarations of its steps, and a flow collects them from its blocks. Put draftReply in the billing flow and refundPolicy is there too, with no entry in the flow itself.
What comes along is the definition. The stored document is keyed by scope, and an org-scoped one is shared, so billing reads the same policy support does.
What a request loads
A request loads only the resources its flow declares, directly or through its blocks. Say the org also stores a serviceHours document that no block and no flow declares. It stays stored, but no request loads it, so no block can read it.
Resources declared on the flow itself load on every request. Resources declared in an action's blocks load when that action runs, and a sibling action's do not. The reference covers deferring a load further.
What it gets you
- A name of its own.
refundPolicycannot collide with a field in session state. - The block brings what it needs. Drop
draftReplyinto another flow and its policy installs with it. - It lasts as long as its scope and store. A user-scoped resource is there in the user's next session, and on a store that persists, after a restart.
- Private until you open it. The model and the browser see a resource only when its definition says so.
How it fits with the others
What a session records, the messages and results, is kept as items, which the sessions page covers. Resources are named records read by key, not a database: there is no query language over them.
Next
- Search a collection finds notes by path and by content.
- Link records with edges connects one resource to another.
- Run a block when a resource changes reacts to a write.
- Resources reference lists every option, the scopes and storage, and the browser and HTTP access.