Skip to main content

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.

One message, top to bottom
Four boxes stacked top to bottom with arrows between them. Your page, in the browser, calls session.sendAction with the action chat. The engine, on your server, checks the input and whose session it is; inside it sits the session, hatched because it outlives the request. The flow, help-desk, names its actions. The blocks, chatGenerator and incrementMessageCount, sit inside a dashed box because they last one action run. A downward arrow between each pair of boxes is the call, and an upward arrow is the reply streaming back as items.YOUR PAGE · IN THE BROWSERsession.sendAction("chat", { message })Send is pressed. The pagecalls the action chat.THE ENGINE · ON YOUR SERVERchecks the input and whose session it isTHE SESSIONRejects bad input, then runs theaction on the session.THE FLOWhelp-deskaction chat · state · what the page may seehelp-desk names its actions.chat starts the blocks below.THE BLOCKSONE RUN OF THE ACTIONchatGeneratorincrementMessageCountchatGenerator writes the reply.incrementMessageCount counts it.The reply streams back as items.CALLS · STREAMS BACKLASTS ONE ACTION RUNOUTLIVES THE REQUEST

An app, from the top​

One file says what the app is made of:

fsdev.config.ts
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:

src/app/send-button.tsx
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:

  1. The page sends the action to the engine.
  2. 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.
  3. The flow runs the action's block. In help-desk that is a sequencer, a block that runs blocks in order: chatGenerator calls the model, then incrementMessageCount updates the session's state.
  4. Blocks emit items, the pieces a flow streams out, and they reach the page as the model writes. session.items is 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.
  5. 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​

Where things are kept
Four columns. In your repo: fsdev.config.ts, flow.ts, the blocks folder and WORKER.md. In the process: the FlowState handle. In your store, hatched because it outlives the request: the session, highlighted as the idea to keep, then session state, items, and request state drawn with a dashed outline because it belongs to one request only. In the browser: the live item list, the snapshot, unsent text kept in localStorage, and the last view answer, which the page holds only while the screen is open. A note says that with no stores set, the store column is held in the process and a restart empties it.IN YOUR REPOIN THE PROCESSIN YOUR STOREIN THE BROWSERfsdev.config.tskits · flows · storesflow.tsactions · state · blocksblocks/one block per fileWORKER.mda worker's briefFlowStateflows · models · storesthe sessionmany requests, one logsession statemessageCountitemsmessages · tool resultssession.itemsthe live reply listsnapshotexposed state onlyunsent textkept in localStorageview answerlast read; not storedrequest stateone request only01With no stores set, the store column is held in the process and a restart empties it.LASTS ONE ACTION RUNOUTLIVES THE REQUESTTHE SESSION

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 aboutRead
KitKits and slots
FlowFlows and actions
BlockBlocks
SessionSessions, items and streaming
StateState and scopes
ViewViews

What it gets you​

  • The session survives. The session lives in your store, so with sqliteStores a 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 run runs 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.