Let the host run the workflow
Many hosts already have a workflow: a case that moves through steps, rules that decide what to ask next, systems that approve or refuse. Mica fits in by rendering each step while the host keeps the decisions. The document is static JSON, the same for every person. It declares the layout, every question it might ask, and the business object that carries the host’s content. The host decides what that content says, which questions are shown, which page is active, and which document comes next.
This guide builds that split step by step. The
business loan application example runs all of
it live, and its host code is in the Mica repository:
site/src/components/LoanWorkflowExample.tsx (the host shell) and
createLoanDesk in site/src/components/exampleHosts.ts (the bank’s rules).
It assumes the setup from
Embed Mica in a host application and
Implement business objects in a host.
1. Put the host’s content in a record
Section titled “1. Put the host’s content in a record”Declare one business object for the case the person is working on. Give it a field for each piece of content the host writes, and a field for each decision the document has to follow:
"application": { "type": "lending.loan_application", "cardinality": "one", "identity": "id", "fields": { "id": { "kind": "string", "required": true }, "stage": { "kind": "string", "required": true, "enum": ["details", "follow-up", "decided"] }, "headline": { "kind": "string", "required": true }, "intro": { "kind": "string", "required": true }, "decisionId": { "kind": "string", "nullable": true }, "followUp": { "kind": "object", "required": true, "fields": { "collateral": { "kind": "object", "required": true, "fields": { "asked": { "kind": "boolean", "required": true }, "reason": { "kind": "string", "required": true } } } } } }}Add an update capability whose inputs are the person’s answers; step 4 uses
it. The document reads the content like any state, with
{ "$state": "/objects/application/data/headline" }. Send money, dates and
rates as raw values and let the document show them with
$format in the host’s locale, such as
{ "$format": "currency", "value": { "$state": "/objects/application/data/limit" }, "currency": "EUR" };
send finished text only for sentences the host writes. A figure derived from
those values for display, such as an order total, need not be a field: the
document can show it with a display-only
$calc,
while the host keeps computing the figures it decides on or discloses, such as
a loan’s total to repay.
2. Seed the record before the first render
Section titled “2. Seed the record before the first render”A host that already knows the case does not need a round trip. Pass the record
as data when you seed the store, and the document renders with it from the
first frame:
const record = await lending.applicationFor(session.customerId);
const store = createStateStore( seedRuntimeObjectState(structuredClone(document.state ?? {}), document.objects, { binding, data: { application: record }, }),);The seed is held to the declaration like a fetch answer: only declared fields reach the store. Every person gets the same layout; what differs is the record the host seeds.
3. Declare every question, show the ones the host asks
Section titled “3. Declare every question, show the ones the host asks”The document holds every question the host might ask. Each group is shown only while the host’s record asks it:
"ask-collateral": { "type": "Section", "props": { "eyebrow": "SECURITY", "title": "What can secure the loan?", "tone": "outlined", "padding": "lg" }, "children": ["ask-collateral-reason", "collateral-description", "collateral-value"], "visible": { "$state": "/objects/application/data/followUp/collateral/asked" }}Make each question’s input required only while the same flag holds, so a question nobody sees never blocks the button:
"collateralValue": { "kind": "number", "requiredWhen": { "$state": "/objects/application/data/followUp/collateral/asked" }, "minimum": 0}The host cannot add a question at run time: it chooses among the ones the
document declares. An unanswered question is not sent as a value: an empty
number input is left out, and an empty text input arrives as "" (a
nullable input arrives as null). Ignore the answers to questions your rules
did not ask.
4. Decide in the operation, answer with the next record
Section titled “4. Decide in the operation, answer with the next record”One update can carry every answer, from every page. The host’s
implementation is where the workflow runs: hand the answers to your own rules
or workflow engine, and answer with the record as it now stands:
const binding: HostObjectBinding = { application: { type: "lending.loan_application", update: async (applicationId, answers) => { const outcome = await lending.submitAnswers(applicationId, answers); if (outcome.refused) { throw hostObjectError({ category: "validation", code: "application.refused", message: "Some answers need another look. Check the highlighted fields.", retryable: false, fieldErrors: outcome.refused, // { collateralValue: "Security must be worth at least €15,000…" } }); } // The next step's content, a new stage, and the questions it asks. return outcome.application; }, },};What holds for the answer:
- It replaces the record in the store only after the host answers, with the
same identity; an answer for a different
idis a host-contract error. - Mica checks the declared input rules first, so the host is never asked with a missing, out-of-range or ill-typed answer. Your own rules still run in the host: Mica’s checks are the declaration’s, not your business’s.
- A refusal leaves the record as it was.
fieldErrorskeys must be declared input names; Mica drops any other key. The document shows each message where it reads/meta/objects/application/operations/update/error/fieldErrors/{input}. - A refusal rejects the button’s action, so any action after it in the same event does not run.
5. Choose the page from the record
Section titled “5. Choose the page from the record”Nothing in the document needs to link to the next page. The host reads the
record’s stage and passes the page key it wants to PageProvider:
const application = useSyncExternalStore(store.subscribe, () => store.get(objectDataPath("application")),) as { stage?: string } | null;
const page = application?.stage === "follow-up" ? "page-follow-up" : "page-details";
return ( <PageProvider spec={document} activePageKey={page}> {/* JSONUIProvider, PackSurface and Renderer as usual */} </PageProvider>);Deriving the page from the record keeps the two in step: the page key changes in the same render as the content that goes with it, and a refusal, which leaves the record alone, leaves the page alone too.
The page keys come from the document. The host chooses among them, and an
unknown key renders an in-place message listing the valid ones. Mica does not
stop a document from linking to any of its pages with goToPage; keeping a
step host-only is a choice of the document’s author, and goToPageHandler
hands any link the document does have to the host’s onNavigate.
6. Render a different document
Section titled “6. Render a different document”When the workflow moves on to a step another document handles, render that document instead. Give it its own store and its own object runtime, and pass it what it needs to load:
if (application?.stage === "decided" && application.decisionId) { return <DecisionApp key={application.decisionId} decisionId={application.decisionId} />;}
function DecisionApp({ decisionId }: { decisionId: string }) { const [store] = useState(() => createStateStore( seedRuntimeObjectState({ ...structuredClone(decision.state ?? {}), decisionId }, decision.objects, { binding }), ), ); // createObjectRuntime, runStartup, handlers and PageProvider as in the first document.}The second document loads its own record at startup, with the id the host put in its state:
"lifecycle": { "startup": [ { "action": "fetch", "params": { "object": "decision", "inputs": { "decisionId": { "$state": "/decisionId" } } } } ]}Nothing carries over between the two documents except what the host passes. The first document’s session ends when it unmounts: dispose its object runtime then, as in step 4 of the host objects guide, so a late answer is dropped instead of landing in a store no one shows. If the person may come back to it, keep its store in the host.
What the document still decides
Section titled “What the document still decides”The host does not have to own every step. Inside the decision document,
Accept and sign runs update and then goToPage to its own Signed
page, because that step belongs to the document rather than the bank’s
workflow. Use host routing for steps the host’s rules decide, and the
document’s own navigation for the rest.
See also
Section titled “See also”- Business loan application: the whole workflow, live, with the host’s log beside it.
- Business objects: the declaration and the state the runtime projects.
- Host contract: seeding, the error envelope and the operation rules.