@ai-digital-factory/state-flow-client
v0.2.0
Published
Browser client host for State Flow: in-memory client instances for the life of the page, and history reported to State Flow Cloud.
Readme
@ai-digital-factory/state-flow-client
The browser host for client Flows (<Flow runs="client">, ADR 0097, 0100, 0104, 0108). Client instances live in memory for the life of the page; lasting truth lives in server Flows.
import { createClientHost } from "@ai-digital-factory/state-flow-client";
import loanRequest from "@acme/client-handlers/loan-request";
const host = createClientHost({
// Optional: report this page's Flow histories to State Flow Cloud.
cloud: { url: "https://cloud.example.com", publishableKey: "sfp_…" },
});
const flow = await host.createFlow(loanRequest, {
applicationId: () => "loan:A-17",
// The application's own route to the server Flow (ADR 0108).
submitApplication: (request) => api.submitApplication(request),
});
// A Flow with an inputSchema also takes `input`, validated and kept with the instance (ADR 0102).
const form = await flow.ensureInstance({ instanceId: "loan-form:A-17" });
// Same builder as the server (ADR 0107).
const submitted = await form
.when("amount")
.transition("submit-request")
.tags({ traceId: crypto.randomUUID() }) // typed by the package's tags.ts (ADR 0108)
.payload({ amount: 25000 })
.run({
"invalid-payload": () => "Some values are not valid.",
otherwise: (refusal) =>
refusal.kind === "retry"
? "Please try again."
: "That did not go through.",
});- Typed registration:
createFlowtakes a compiled client module fromstate-flow compileand dependencies implementing its interfaces; a durable module or missing dependencies do not compile. Instances, scopes (instance.scope(...)), snapshots, data, payloads and refusal handlers keep the compiled types. - Page-lifetime instances (ADR 0104): nothing is saved in the browser.
ensureInstancecreates the instance in this page or returns the one the page holds; a reload or another tab starts fresh. Choose the start State from lasting truth (a server Flow, the application's database) inresolveStartState, through the Flow's dependencies, and give server instances a business identity (application:A-17) so a repeated submit after a reload reaches the same server instance instead of creating another. - The network is the application's (ADR 0108): an effect reaches a server Flow through whatever the application gives it as a dependency: its own route, tRPC, a queue. Effects receive
context.executionId(an idempotency key) andcontext.tagsto carry along: the server dispatches with.tags(...), so State Flow Cloud finds both histories by the same tag. When the application learns of a server change (polling, push, focus), it dispatches the browser Transition itself. Type routes withPayloadOf,ResultOfandRefusalOffrom@ai-digital-factory/state-flow-runtime, imported withimport type. - No retries: effects are plain async functions; a failure leaves the source State committed.
- Tags (ADR 0108): a package declares in
src/tags.tsthe labels its calls carry (tagsSchema, string fields).ensureInstance({ tags })and.tags(...)on a dispatch take them, typed and required as the schema says; every callback of the work the call starts receives them ascontext.tags, the history records them and Cloud finds instances by them. They are reported unredacted, so never put personal or secret values in them. - Refusal kinds: every refusal carries
kind:retry(the same request may be sent again),rejectedorfailed. - Reported history: with
cloud, every dispatch and how it ended is reported to State Flow Cloud with the web application's publishable key, each entry with a stable ID and the release of its Flow model (a digest of the model's content), so retries never duplicate or reorder history and a model is never replaced. The host sends each model with its first report and flushes small batches when the page is hidden. Reports are best effort: entries still queued when the page goes away are lost. Secret fields (secretField) are never recorded; private fields (privateField) stay in the browser and are redacted before reporting. Withoutcloud, nothing leaves the browser. A secret application key (sfk_…) is refused.
React bindings live in @ai-digital-factory/state-flow-react.
