@fabricorg/experience-contracts
v1.4.0
Published
Portable experience metadata contracts for capability-driven interfaces.
Readme
@fabricorg/experience-contracts
Typed, versioned experience metadata carried through CapabilityMetadata.extensions. Platform preserves
the namespaced JSON without interpreting roles, slots, action intents, screens, layout, or rendering.
Display renderers
createDisplayRegistry() maps an event type to a DisplayRenderer, which turns an event payload into a
DisplayDescriptor. render(eventType, payload, context) passes context to the renderer unchanged.
DisplayContext.event is an optional DisplayEventIdentity copied from the event envelope: eventId,
subjectType, subjectId, occurredAt, and actionInvocationId when an invocation emitted the event.
Some payloads identify their invocation only in the envelope. Platform Host's ApprovalRequested,
ApprovalDecided and ApprovalExpired payloads omit the command and proposal, for example. To render
one, read the invocation with a lookup scoped to context.tenantId and context.spaceId and keyed by
context.event.actionInvocationId. Renderers that use only the payload can ignore the field.
Command review renderers
createCommandReviewRegistry() maps an action ID to a renderer for the proposed command.
Register one renderer per action ID, using the same action ID syntax as experience action intents.
Renderers return a CommandReviewResult synchronously or asynchronously.
import { createCommandReviewRegistry } from "@fabricorg/experience-contracts";
const reviews = createCommandReviewRegistry();
reviews.register("jobs.start", (parameters) => {
if (!parameters || typeof parameters !== "object" || !("priority" in parameters)
|| typeof parameters.priority !== "string") {
return { status: "unavailable", reason: "required_data_unavailable" };
}
return {
status: "available",
descriptor: {
title: "Start processing",
effect: "external_start",
approvalStatement: "Start a processing job in the external scheduler.",
fields: [{ label: "Priority", value: parameters.priority, source: "agent_proposed" }],
},
};
});
// The server supplies the command and its trusted tenant, space, db, and reader context.
const review = await reviews.render("jobs.start", proposedParameters, trustedContext);The effect is external_party for a command that reaches an outside party, external_start
for one that starts external work, or internal_change for an internal change. Fields appear in
the supplied order. Each string value is marked server_verified when checked against a trusted
source, server_derived when computed by the server, or agent_proposed when supplied by the
agent. Optional format hints are text, number, currency, date, datetime, and identifier.
Renderers own field provenance and should select only values appropriate for the reviewer to see.
render is member-only. The caller must establish membership and supply the reader's
actorType from trusted server context. Do not take it from the request body. The registry
checks for natural_person before looking up or invoking a renderer. Missing or malformed context
also returns reader_not_permitted.
An unregistered action returns renderer_not_registered. A throw or rejection returns
renderer_failed, and malformed output returns invalid_descriptor. An available descriptor
requires a non-blank title and approval statement, a known effect, and an array of review rows.
A flat row has an omitted or undefined kind, a non-blank label, a string value, a known
source, and an optional format from the list above.
A comparison row has kind: "comparison", a non-blank label, a renderer-supplied state
of match, mismatch, or missing, and exactly two entries in its values array. Each entry
has a non-blank label, a known source, and an optional value. An omitted or undefined value
counts as absent. A present value must be a non-blank string. The state is missing exactly when
at least one value is absent. The renderer applies its domain rules to choose match or
mismatch. The reader validates presence and accepts the renderer's comparison of two present
strings. A comparison row can include one format from the list above and a non-blank note.
CommandReviewDescriptor.fields is a union of flat and comparison rows. Code reading
fields[].value must narrow on kind. Other kind values make the descriptor invalid.
A renderer can return
{ status: "unavailable", reason: "required_data_unavailable" } when a trusted read is missing or the stored parameters lack a value the review needs.
Unavailable results contain only a status and a fixed reason code.
Command review digests
digestCommandReview({ tenantId, spaceId, actionInvocationId, actionId, descriptor })
normalizes the descriptor with the same reader as CommandReviewRegistry.render. It hashes
the UTF-8 bytes of the JSON envelope with kind: "command-review" and version: 2, along with
the five supplied members. Object keys are sorted recursively in UTF-16 code-unit order. The hash
preserves the order of rows and the order of entries within a comparison. The result is a
lowercase, zero-padded SHA-256 hex string.
Blank or non-string identifiers and invalid descriptors throw. The runtime needs Web Crypto
(crypto.subtle) and TextEncoder. Changes to the envelope or descriptor shape require a new
COMMAND_REVIEW_DIGEST_RULE identifier. The current identifier is
fabric-command-review-sha256-v2. Version 2 changes every digest, including flat-only reviews.
The digest covers only the normalized descriptor and its envelope. readCommandReviewDescriptor
builds fresh rows and comparison entries and omits unknown properties and undefined optional
properties. The UI must render
the descriptor returned by render without separately displaying raw renderer properties as
part of that review. Send the descriptor and digest together and echo the matching digest on
the decision. On review_changed, show a fresh review and ask for a new explicit decision.
A digest identifies the review named by the request. It is unkeyed and any caller can compute
it. It gives no evidence of what the approver saw, read, displayed or attested to, and excludes
editsReference. Applications can use the
Platform Host approval-hook pattern
to check it against the server render at decision time.
