How it fits together
A Flow State app is a React page on top of a backend, and the backend is built in layers. Each layer has one job, and each is taught on its own page.
This page uses one app, a help desk. A user types "Where is my order?" and a model answers. The page follows that message down through the layers, shows where each thing is kept, and says which page to open for what.
An app, from the top
One file says what the app is made of:
import { createFlowState } from "@flow-state-dev/engine";
import { sqliteStores } from "@flow-state-dev/store-sqlite";
import { workforce } from "@flow-state-dev/workforce";
import helpDeskFlow from "./src/flows/help-desk/flow";
export default createFlowState({
kits: [workforce({ directory: "./workforce" })],
flows: [helpDeskFlow],
stores: sqliteStores({ filename: "./app.db" }),
});
createFlowState builds the engine: the part on your server that serves your flows, streams their results and stores what they keep.
A flow is a self-contained system inside your app, registered with the server for clients to call. It names its actions, the entry points a page can run, and says which of its state the browser may see. help-desk has one action, chat. See Flows and actions.
A block is a typed unit of work: a model call, a plain function, or several of them chained. An action runs one block. See Blocks.
A kit is a feature packaged so an app installs it with one entry in kits. It brings its own flows and can leave openings for other kits to fill. workforce brings your team of workers, each one a WORKER.md file in your repo whose body is its instructions. See Kits and slots.
stores is where data is kept.
What happens when you send a message
The page makes one call:
import type { ReactElement } from "react";
import { useFlow, useSession } from "@flow-state-dev/react";
export function SendButton({ message }: { message: string }): ReactElement {
const flow = useFlow({ flowKind: "help-desk", autoCreateSession: true });
const session = useSession(flow.activeSessionId, { flowKind: "help-desk" });
return (
<button
disabled={!flow.activeSessionId}
onClick={() => void session.sendAction("chat", { message })}
>
Send
</button>
);
}
useFlow finds or creates the user's session, the record that keeps this user's requests to the flow together. The button stays disabled until the id exists; the React API reference covers what sendAction does without one.
From there:
- The page sends the action to the engine.
- The engine checks the input against the action's schema and checks that the session is the caller's. A session answers only the user who owns it: send to someone else's session id and the engine refuses before any block runs, answering as if the session did not exist.
- The flow runs the action's block. In
help-deskthat is a sequencer, a block that runs blocks in order:chatGeneratorcalls the model, thenincrementMessageCountupdates the session's state. - Blocks emit items, the pieces a flow streams out, and they reach the page as the model writes.
session.itemsis the live list of them. Each block run is also recorded, with its output, as a trace item the page does not receive by default. - When the run completes, the page receives the session's new state.
If the stream drops, the page picks it up where it left off. Sessions, items and streaming has the detail.
Where things are kept
Code and WORKER.md files are in your repo. What createFlowState returns lives in the process.
The session is in your store, with its state and its items. All of that outlives the request, and on a store that persists it outlives a restart. Request state is scratch space for one action run, and only that run can see it. With no stores, the store is memory: a restart empties it, sessions included.
The browser holds what it last received: the live items and the snapshot, the state the flow chose to expose. A view is a read-only question a screen asks the server, such as a user's open orders. Its answer is computed on each read. The server doesn't keep it, and the browser holds only the last answer while the screen is open. See State and scopes, Views and the client reference for what the browser keeps locally.
Which page to read for what
Say the reply to "Where is my order?" is wrong. If a worker gave it, open its WORKER.md first: the body is its instructions, run on the built-in agent flow unless the file names a flow of yours. Otherwise, or if the file looks right, open the flow, then the blocks inside it. To run the action without the page, use the terminal. It prints each event as a JSON line:
fsdev run help-desk chat -i '{"message":"Where is my order?"}'
The CLI reference lists the flags that set the input and the session. To see each block's trace, open the DevTool with fsdev dev.
| To learn about | Read |
|---|---|
| Kit | Kits and slots |
| Flow | Flows and actions |
| Block | Blocks |
| Session | Sessions, items and streaming |
| State | State and scopes |
| View | Views |
What it gets you
- The session survives. The session lives in your store, so with
sqliteStoresa user comes back after a restart and finds it. - A dropped connection loses nothing. The stream picks up where it stopped, so a half-written reply finishes.
- The browser's client data holds only what the flow exposes. A change to another field can pass through the stream while a request runs, so keep secrets out of session state.
- You can test without the page.
fsdev runruns the same action on the same engine and stores, from a terminal.
How it fits with the others
Each layer uses only the ones beneath it, so the page never reaches into a block and a block never knows about React. Code that isn't a React page, such as a script or an edge function, calls your flows through @flow-state-dev/client, and @flow-state-dev/react wraps that client in hooks. Kits are built on the engine; you can ship a flow with none.
Next
- Flows and actions: how a flow names its actions and what runs when one is called.
- Blocks: the five kinds of block and how they nest.
- Kits and slots: installing a feature, and how kits build on each other.
- Glossary: every word on this page, in one line each.