# Powerhouse Academy - Complete Documentation > Generated: 2026-11-22T13:36:05.171Z > Total Documents: 124 > Source: https://academy.vetra.io/llms-full.txt # Get Started ## Vetra Studio > Source: https://academy.vetra.io/academy/GetStarted/VetraStudio Vetra Studio is the browser UI for the Vetra agent. You chat with the agent, watch it work across the four-phase product cycle (Ideate → Specify → Build → Deploy), and see a live preview of the product it builds. Each Studio runs as a **dedicated cloud agent** tied to your identity. Vetra provisions an isolated environment that hosts the agent, its workspace, and the preview services. ## What you can build With the Vetra agent you build **local-first, AI-ready, specification-driven** products and platforms: - Open-source, decentralized back-ends. - SaaS, ERP, CMS, or CRM applications. - Structured-data, document-centric systems with a built-in GraphQL API. --- ## Getting started Vetra Studio is in **pre-alpha**. You reach it with an early-access code from Powerhouse. 1. Open **[vetra.io](https://vetra.io)** and log in with your wallet (via Renown). 2. Click **Vetra Studio** in the top-right header. This opens your **Products** dashboard at `/user`. If your account is not yet enabled, `/user` shows the **early-access gate** ("Vetra Studio — Early access — choose how you'd like to get started") with three paths: - **I have an invite code** — paste the code and click **Get Access**. - **I don't have a code** — join the waitlist by email, or **Request a code on Discord**. - **Run it locally** — no code needed; run Vetra Studio yourself with the `vetra` CLI (see [Running Vetra Studio locally](#running-vetra-studio-locally) below). The first time you enter, a notice reminds you the product is early:
Vetra Studio pre-alpha warning dialog
Vetra Studio is in pre-alpha. Download your documents or push them to GitHub as a back-up at the end of a cycle.
### Create a studio On the Products dashboard, click **Create new product**. With an invite code the studio provisions right away — the API key that powers the agent is supplied for you behind the code, so there is no key to enter. The product card moves through **Provisioning…** (not yet openable) to **Ready**. Click **Open →** to launch the studio in a new tab.
Vetra Studio Products dashboard with a Ready product
The Products dashboard lists your studios. Each is a dedicated cloud agent on its own subdomain, shown as Ready once provisioned.
Each product is one Vetra Studio instance running on its own subdomain (`.vetra.io`). Opening it lands you in Powerhouse Connect at `.vetra.io/d/`. --- ## The layout A studio opens in Powerhouse Connect with two panes: - **Chat pane** (left): lists chat sessions, and renders the active session's message history when one is open. - **Resize handle** (vertical strip): drag to adjust pane width. - **Main pane** (right): shows the phase-cycle cards by default, and opens a phase section, a document, or the build preview when you navigate. - **Toolbar** (far left): **Home**, **Open Account** (Renown), and **Settings**. - **Main-pane top bar**: a **Version info** button, an **Authorize agent** button (see [Authorizing the agent](#authorizing-the-agent)), and the **Auto-follow agent** checkbox. - **Auto-follow agent** (on by default): when checked, the main pane follows the agent and opens documents as the agent creates them.
Vetra Studio home with empty session list and phase cards
A fresh studio: empty session list on the left, the four phase cards on the right under "Product development cycle."
The main pane opens on **Product development cycle**. Alongside the four phase cards it offers a **Take a tour** button and an **example-prompt carousel** — cycle through the sample prompts and **Copy example prompt** to seed your first session. --- ## Creating a session 1. In the chat pane, click **New** (top-right) or **New session** (empty state). 2. A new chat session document is created in the drive and opened immediately. 3. Type your first message and press Enter (or click the send button). Sessions persist across restarts. They are CRDT documents stored in the studio's reactor. Sessions are scoped to your identity: signing in with a different identity shows a different session list. --- ## Talking to the agent The chat session is how you direct the agent. Type a message; the agent responds and runs tools. Each tool call appears inline as an expandable card showing its name, status, and a result preview. While the agent works, the send button becomes **Stop**.
An active Vetra Studio session with agent tool cards and an auto-followed document
An active session. Tool cards stream inline while the main pane auto-follows the document the agent is writing.
Tools you will see include: - `Using skill: ` / `Reading skill: ` — the agent loads a named skill (such as `document-modeling`, `document-editor-creation`, `drive-app-creation`) and reads its reference docs. - `spec-create`, `spec-generate`, `spec-update`, `spec-list`, `spec-schema` — create, generate, and edit specification documents. Create and update cards name the document, for example `spec-create · Hotel Breakfast Problem Sheet`. - `reactor-project-init`, `reactor-project-start`, `reactor-project-check`, `reactor-project-ls`, `reactor-project-ps` — scaffold, run, and inspect the project that hosts the preview. - Workspace actions in the agent's project appear as **Read**, **Write**, **Edit File**, **List**, and **Grep** cards with the file path. - `spec-preview-show` — surface a running app in the BUILD pane. A status bar at the bottom of the chat shows the session state: **Active**, start time, message count, token count, and tool-call count. The agent can: - Create and iterate on **spec documents** (problem, audience, and brand sheets, a feature, and a work breakdown structure). - Design **document models** and generate their reducers, editors, and apps. - Scaffold a **reactor project** that hosts a live editor preview. - Surface a **preview** in the BUILD pane and, when you're ready, publish to the cloud. --- ## The phase cycle The main pane shows four numbered phase cards when no section is open. Click a card to open its section, or navigate with the breadcrumb (`Home › `). | Phase | What happens there | | ---------------------------------------- | -------------------------------------------------------------------------------------- | | **1 · Ideate** — Problem Definition | Frame the problem and audience — problem, audience, and brand sheets, a feature, a WBS | | **2 · Specify** — Solution Design | Pin down the data model, workflow, and states — document models grouped by project | | **3 · Build** — Implementation & Testing | Watch the agent generate and test code, with a live preview of the running app | | **4 · Deploy** — Delivery | Publish the package and run it in your cloud environments | A card's availability depends on progress: BUILD shows a preview only after the agent scaffolds a project, and DEPLOY needs you to sign in with Renown. --- ## Ideate and Specify The Ideate and Specify sections hold the documents the agent writes. In Ideate the agent frames the product — a problem sheet, an audience sheet, a brand sheet, a feature, and a work breakdown structure. Specify then groups the **document models** into **projects**: each project is a folder of specs and models.
Specify section grouping documents into projects
Specify groups documents into projects. Each project is a folder of specs and document models.
With **Auto-follow agent** on, a document opens in the main pane as the agent creates it, so you watch each sheet and model take shape without interrupting the run. A document editor has a **Download** action and a revision-history view. For a **document model** you edit its Document Type, Description, Author, Website, and Model Extension, and its state schema under **Global** / **Local** tabs (with **Show standard library** for reference types).
Document-model editor in Vetra Studio
A document-model editor: Global and Local state-schema tabs, initial value, and operations.
--- ## The BUILD preview The BUILD pane deep-links into the reactor project the agent spawned for the session. The sequence: 1. The agent calls `reactor-project-init` and `reactor-project-start` — visible as tool cards in the chat. 2. The dev server warms up (typically 10–30 seconds) and the project reports ready. 3. The agent calls `spec-preview-show` with a project, drive, and app reference to surface the app in the BUILD pane. 4. Any code the agent generates after that is picked up by Vite HMR — the preview updates without a full reload. Until the agent surfaces a preview, the pane reads **"No preview yet — The BUILD preview appears once the agent scaffolds a project for this session."** A running reactor project is not enough on its own; the agent has to surface a preview.
BUILD pane rendering a generated app live
The BUILD pane renders the generated app live. Here the agent built a hotel breakfast-order form.
**WARNING:** The BUILD preview runs the editors and apps in a local dev reactor. **Processors do not run here** — they execute server-side in Switchboard once the studio is deployed. Preview drives are also ephemeral and reset between sessions. So an action wired through a processor (for example, syncing a guest order to a staff board) only takes effect after you deploy. Because it is pre-alpha, the preview does not always mount; if it stays on "No preview yet," deploy the app to see it running end to end. Use the breadcrumb at the top of the pane to navigate back to the phase-cycle home. --- ## Deploy The Deploy section publishes your project's package to **Vetra Cloud**. Sign in with **Renown** first (the top bar then shows your address with a **Disconnect** button). Deploy opens on a **Projects** list. Each project shows a status (**Not deployed**, **Needs release**) and a **Set up deploy** button. Click it to open the deploy detail: 1. Under **Available environments**, the project version (for example `morning-order@1.0.0`) is listed. A new project isn't running anywhere yet. 2. Click **New environment**, give it a name, and click **Create & deploy**. A `vetra.io` subdomain is assigned automatically. 3. The environment rolls out: **Approved → Deploying → Ready**. When it reports **Up to date · v1.0.0**, the deploy is live.
A deployed Vetra Studio environment showing Ready status and service links
A live environment: Ready status, its subdomain, and Open / Connect / Switchboard actions.
Each environment runs **Powerhouse Connect** at `-connect.vetra.io` and **Switchboard** (the GraphQL API) at `-switchboard.vetra.io/graphql`. Use **Open**, **Connect**, and **Switchboard** on the environment card to reach them. For environment management, services, sizing, and versions, see [Vetra Cloud](../docs/docs/01-VetraCloud.md). --- ## Authorizing the agent To let the agent sign actions with your identity, click **Authorize agent**. This runs the Renown flow that authorizes the studio's subdomain to sign actions on your behalf. Once confirmed, the top bar shows **Renown** with your address and a **Disconnect** button. Authorization is independent of chat sessions, and it switches the studio's active identity from a generated `did:key` to your wallet identity (`did:pkh`). Because sessions are scoped per identity, switching identity changes which sessions the list shows. --- ## Session URLs The studio keeps its state in the URL, under `.vetra.io/d/`: - The selected session id is the `?session=` query parameter. Sharing the URL opens the same session in another tab or browser; browser back/forward navigates between sessions; reloading restores the selection. - When the agent opens a document, the URL also carries a `doc=` parameter. --- ## Glossary **Chat session** — a CRDT document that stores message history, agent tool calls, and tool results for one conversation thread. **Phase cycle** — the four-card home view (Ideate / Specify / Build / Deploy). Each card is a stage of the product-development workflow. **Project** — a folder of related specs and document models, shown in the Specify section. **Reactor project** — a dev-mode process the agent spawns to host a live editor preview. One per active build in a chat session. **Preview drive** — a drive inside a reactor project, used for ephemeral preview document instances. **Renown** — the identity layer that lets the studio sign actions on behalf of your wallet without exposing your private key. --- ## Running Vetra Studio locally Prefer to run everything on your own machine? Vetra Studio ships as a CLI you run yourself — no invite code needed. You bring your own **Anthropic API key** (the agent runs on Claude). **Prerequisites:** Node ≥ 24 on macOS or Linux. pnpm is optional — the installer uses it if present, otherwise offers to install it and falls back to npm. ### Quick install **Option A — installer script** Downloads and runs the official installer, which sets up the `ph` and `vetra` CLIs and offers to launch the agent: ```bash curl -fsSL https://get.vetra.io | sh ``` If you want to review the script before running it, download it first: ```bash curl -fsSL https://get.vetra.io -o install-vetra.sh cat install-vetra.sh # inspect sh install-vetra.sh ``` **Option B — npm / pnpm (no shell script)** Install the CLIs directly from the Vetra registry — nothing to pipe or inspect: ```bash npm install -g ph-cmd vetra-cli --registry=https://registry.vetra.io ``` ```bash pnpm add -g ph-cmd vetra-cli --registry=https://registry.vetra.io ``` --- The first launch sets up Claude auth (bring your own Anthropic API key), then prints the Studio URL — open **[http://localhost:8090/](http://localhost:8090/)** in a browser. Start the agent again any time with `vetra`. ### From source Clone [vetra-cli](https://github.com/powerhouse-inc/vetra-cli), then install and start the agent: ```bash pnpm install pnpm dev # interactive agent REPL + embedded Reactor and Switchboard ``` Authenticate the agent once: ```bash export ANTHROPIC_API_KEY=sk-ant-... ``` When the agent starts a build, the logs print `Vetra Studio: http://localhost:8090/d/` — open that URL to see your work live, and iterate by continuing the conversation. To wipe the local dev workspace and start clean, run `pnpm dev:reset`. ### Service ports A single `vetra` process runs the REPL, an embedded Reactor, and an embedded Switchboard. It reserves three ports with no fallback, so free them first if they are already in use: | Service | Port | Purpose | | -------------------- | ----- | -------------------------------------- | | Vetra Studio (proxy) | 8090 | Browser entry point (the URL you open) | | Connect | 27370 | Editor front-end | | Switchboard | 59220 | GraphQL API | --- ## Vetra Cloud > Source: https://academy.vetra.io/academy/GetStarted/VetraCloud Vetra Cloud is the hosted infrastructure layer for Powerhouse applications. It lets you spin up a personal cloud environment that runs **Powerhouse Connect** (the user-facing document editor) and **Powerhouse Switchboard** (the backend GraphQL server) — and extend them with packages from the Vetra registry. --- ## Getting Started ### 1. Connect Your Wallet Navigate to the **Cloud** section in Vetra and authenticate with your Ethereum wallet via **Renown**. Renown lets Connect sign operations on behalf of your on-chain identity without exposing your private key.
Connect Wallet via Renown
Connect your Ethereum wallet via Renown to authenticate with Vetra.
--- ### 2. Create an Environment After authenticating, create a new environment by giving it a name. Each environment gets its own subdomain (e.g. `my-env.vetra.io`) and runs its own isolated set of services.
Create Environment
Give your environment a name to get a dedicated subdomain and isolated set of services.
--- ### 3. Configure Services Inside your environment you can enable and configure three services: | Service | Description | | -------------------------- | -------------------------------------------------------------- | | **Powerhouse Connect** | The document-editor UI served at `connect..vetra.io` | | **Powerhouse Switchboard** | The GraphQL API server at `switchboard..vetra.io/graphql` | | **Powerhouse Fusion** | Optional data-fusion layer (disabled by default) | Use the **Size** dropdown to pick the compute tier for each service and toggle it on or off with the switch on the right.
Services overview
Enable and configure Connect, Switchboard, and Fusion services for your environment.
--- ### 4. Select a Version Each service shows its currently pinned version. Click **Change version** (or **Update All** when updates are available) to pin Connect and Switchboard to a specific release.
Available updates
Pin each service to a specific version or apply all available updates at once.
--- ### 5. Install Packages Packages extend Connect and Switchboard with additional document models, editors, and reactor modules. Click **+ Add package** in the **Installed Packages** section, then search for a package by name. Available packages include community and official Powerhouse packages such as: - `@powerhousedao/builder-profile` - `@powerhousedao/contributor-billing` - `@powerhousedao/knowledge-note` - `@arbitrum/arbgrants`
Add Package
Search and select a package from the Vetra registry to install into your environment.
Once you select a package, a **Pending change** banner appears at the bottom. Click **Approve** to apply the change and redeploy your environment. --- ## Next Steps Once your environment is running you can point your local `ph` CLI at it, or share the Connect URL with collaborators so they can start working with your document models right away. To publish your own packages to the registry, see the [Publishing Packages](/academy/Build/Launch/PublishYourProject) guide. --- # Learn ## Learn the Powerhouse stack > Source: https://academy.vetra.io/learn Powerhouse is a framework for building applications whose state evolves through **operations** rather than direct mutations. Instead of storing the current state, it records the ordered operations that produced it — giving every document a complete, auditable history. If you've worked with Git, event sourcing, or CRDTs, much of this will feel familiar. This course explains how the stack is structured and how its pieces fit together. It's **explanation, not step-by-step instructions** — the goal is to understand _what_ the agent builds for you and _why_. To ship with the agent, start at [Get started](/academy/GetStarted/VetraStudio); to build by hand, see [Build](/academy/Build/ManualTodoTutorial/CreateNewPowerhouseProject). ## Why use Powerhouse? Because state is an operation log, teams get a set of hard problems solved by default: - **Track every change** — who did what, when, addressable by index and hash. - **Sync across peers** — offline edits merge deterministically, no last-write-wins. - **Build collaborative apps** — concurrent editing without conflict resolution code. - **Keep auditable histories** — the log _is_ the audit trail. - **Separate business logic from UI** — reducers hold the rules; editors just dispatch. That makes it well suited to operational systems, governance tools, financial workflows, collaborative editors, and other distributed applications. ## What you'll learn By the end of this course you'll understand: - The Powerhouse mental model — documents, state, and operations - Actions and reducers — how state changes are described and applied - Editors and host applications - Connect and distributed synchronization - Switchboard, subgraphs, and read models - How a complete Powerhouse application fits together ## The path The chapters are designed to be read in order — each builds on the last: **Foundations → Architecture → Document models → Editors → Connect → Switchboard → Sync → Going further** Each chapter is a handful of short lessons that end with a quiz to check your understanding. After the course you'll be able to navigate the Powerhouse architecture, reason about how a change flows through the stack, and read what the agent generates with confidence. New to Powerhouse? Begin with [What is Powerhouse?](/learn/foundations/what-is-powerhouse) — every later lesson builds on the operation-log model it establishes. --- ## What is Powerhouse? > Source: https://academy.vetra.io/learn/foundations/what-is-powerhouse Powerhouse is a framework for building apps whose state lives in **documents** that evolve through **operations**. If you've built with CRDTs, Event Sourcing, or Git, a lot of this will feel familiar — with one twist: Powerhouse makes the _type_ of each document explicit, so actions are always validated before they turn into operations. By the end of this lesson, you should be able to answer: what does Powerhouse store, and how does an app make changes to it? ## The core loop Everything in Powerhouse flows through the same cycle: 1. **You dispatch an action** — just data describing what you want to happen. 2. **A reducer runs** — a pure function that returns the next state and emits an operation. 3. **The operation is appended** — to the document's immutable history, and eventually synced to peers. ```ts // Dispatch const action = addTodo({ id: "t-1", title: "Learn Powerhouse" }); // The reducer produces the next state const nextDoc = reducer(currentDoc, action); ``` This loop never changes, whether the document is a to-do list, a finance ledger, or a 10,000-line design doc. ## Why operations, not snapshots? Storing _what changed_ instead of _what is_ unlocks a few things: - **Deterministic replay** — hand the operation log to any machine and it rebuilds the same state. - **Conflict-free sync** — two offline clients can both edit, then reconcile by merging operations in order. - **Auditable history** — every change is addressable by index and hash. - **Derived read models** — processors fold operations into search indexes, dashboards, or analytics without re-querying the source. ## The pieces you'll meet These are the terms that show up everywhere in the docs and in the rest of these lessons: | Term | What it is | | ------------------ | ------------------------------------------------------------------------------ | | **Document Model** | A typed schema + the set of actions allowed on it. | | **Document** | An instance of a model, plus its operation history. | | **Action** | Pure data — a name and inputs — describing an intended change. | | **Operation** | An action after it ran through a reducer: stamped with index, timestamp, hash. | | **Reducer** | A pure function `(state, action) → nextState`. | | **Editor** | A React UI that dispatches actions. | | **Processor** | An event-driven handler that derives read models from operations. | | **Reactor** | The engine that runs reducers, persists operations, and syncs them. | --- ## Thinking in operations > Source: https://academy.vetra.io/learn/foundations/thinking-in-operations If you've built CRUD apps before, you probably think in terms of _rows_ — the current value of a record in a database. Powerhouse asks you to flip this: think in terms of the _sequence of changes_ that produced that row. This lesson is short on code and heavy on mental model. Once it clicks, the rest of the stack falls into place. ## The snapshot trap Here's a SQL-shaped way to handle "mark todo done": ```sql UPDATE todos SET done = true WHERE id = 't-1'; ``` That statement destroys information. Afterwards, you can't answer: - _When_ did it get marked done? - _Who_ marked it? - _What was it before?_ - Can we undo just this change without rolling back everything since? You can rebuild some of that with audit tables, triggers, or soft deletes — but you're reintroducing history on top of a model that actively throws it away. ## The operation-shaped answer In Powerhouse, the same change is: ```ts dispatch(completeTodo({ id: "t-1" })); ``` Which becomes an operation appended to the document's log: ```json { "index": 42, "timestamp": "2026-04-15T10:12:33Z", "actor": "user:alice", "name": "completeTodo", "input": { "id": "t-1" }, "hash": "…" } ``` State is _derived_. If you want "the current list of done todos," you fold the log. If you want "the state at index 30," you fold up to there. If two peers edit offline, you merge their logs in order — no last-write-wins guesswork. ## What you get for free Once the log exists, a lot of hard problems become easy: - **Undo / redo** — append an operation that retracts the last one. - **Time Travel Debugging** — fold to any index to see the document as it was. - **Audit trails** — every change has an actor, a timestamp, and a hash. - **Derived read models** — a processor subscribes to the stream and maintains a search index, or a counter, or a cache. - **Replays** — reconstruct bugs by replaying the exact operations the user produced. --- ## The big picture > Source: https://academy.vetra.io/learn/architecture/the-big-picture The previous lesson showed how every change becomes an operation appended to an immutable log. Now let's look at _where_ that all actually runs. By the end of this lesson, you'll be able to point at any concept in the course — a reducer, a processor, a React hook — and say confidently: "that runs in the Reactor" or "that lives in Switchboard." ## One engine, two faces The **Reactor** is the core engine. It runs reducers, appends operations to storage, drives processors, and syncs with other Reactors over the DocSync protocol. Every instance of Powerhouse — on your laptop, on a server, on a peer's machine — has a Reactor at its heart. The Reactor doesn't have a UI, and it doesn't have a public API by default. That's the job of **host applications**. The two you'll meet most often are: - **Connect** — the browser-based collaboration workspace where end users edit documents. It embeds a Reactor in the browser, so it works offline and syncs when it reconnects. - **Switchboard** — the server-side host that exposes the Reactor over GraphQL and MCP. This is where processors run in the background, where read models are maintained, and where agents and integrations talk to your data. ``` Your browser Your server ┌──────────────────────┐ ┌──────────────────────────┐ │ Connect │ │ Switchboard │ │ ┌────────────────┐ │ │ ┌────────────────────┐ │ │ │ Reactor │ │◄────►│ │ Reactor │ │ │ │ (browser) │ │ sync │ │ + processors │ │ │ └────────────────┘ │ │ └────────────────────┘ │ └──────────────────────┘ └──────────────────────────┘ ``` Both Reactors speak the same protocol. Edits made offline in Connect flow to Switchboard the moment a connection is available — no polling, no special handling. ## Your code plugs into all three When you build on Powerhouse, you ship a **package** — a bundle containing your document models (schemas + reducers), editor components, and processors. That one package plugs into the whole stack: - The **Reactor** picks up your reducers and runs them whenever an action is dispatched. - **Connect** renders your editor components so users can interact with documents. - **Switchboard** runs your processors, which fold the operation stream into read models and expose them over GraphQL. You write the logic once; the stack decides where it executes. ## What syncs between them The Reactor doesn't sync state — it syncs **operations**. When you edit a document in Connect offline and then reconnect, the Reactor sends the operations you produced to the remote Reactor in Switchboard. The remote Reactor replays them, and both ends land on the same state. No last-write-wins, no manual merge step. This is what the overview doc means by "local-first, built to scale": you get a real app experience on-device, and the operation log keeps everything consistent across the network. ## Host applications — the bigger picture Connect and Switchboard are two of four host applications in the ecosystem. The next lesson covers all of them in depth, including how Fusion and Renown fit in. For now, the three-piece map — Reactor, Connect, Switchboard — is enough to orient everything else you'll learn. --- ## Host applications > Source: https://academy.vetra.io/learn/architecture/host-applications The previous lesson named the three pieces: document models, a Reactor, and a host application. Now let's make the last one concrete. Because "embed a Reactor" sounds abstract — until you see that a Reactor is just a library, and the host is the thing that gives it a place to live. ## The Reactor is a library, not a server If you've deployed a Node service before, you might picture the Reactor as a long-running process you start and point your app at. It isn't. The Reactor is a package — `@powerhousedao/reactor`. It has no `main()`. It doesn't open a port. It sits inert until something boots it up, hands it a storage backend, and starts sending it actions. That "something" is the host application. The host is what runs, and the Reactor is what the host runs. ## What a host provides A Reactor needs four things it can't supply itself: - **Storage** — where operations are persisted. Connect uses PGlite running in IndexedDB in the browser. Switchboard can point at a real Postgres instance. Your custom host could use anything that implements the storage interface. - **Network** — which other Reactors to sync with, over what transport, with what credentials. - **UI surfaces** — the windows, panels, and modals where editors render. The Reactor has no concept of a DOM. - **Package registry** — which document models, editors, and processors are loaded. The host discovers and registers them with the Reactor at startup. The Reactor's job is to apply reducers, order operations into a deterministic log, and route them to processors and sync channels. Everything else is the host's problem. ## The two ready-made hosts **Connect** is the browser and desktop host. When it boots, it initialises a Reactor with PGlite storage in IndexedDB, loads your installed packages, and exposes the Reactor through React hooks. Every editor you build reaches the Reactor through those hooks — you never construct a Reactor yourself inside an editor. **Switchboard** is the server host. It wraps a Reactor in a GraphQL and MCP API so agents, back-ends, and integrations can reach documents without a browser. A Switchboard instance can sync with one or more Connect clients through the DocSync protocol, so the same operation log flows between browser and server. ## Why the separation matters Because the host owns the runtime, the same document model — the same reducer, the same business logic — can run in a browser and a headless server without any changes. Connect renders the editor and signs actions with the user's identity. Switchboard exposes the same document over an API and runs processors server-side. The document model doesn't know which host it's in, and it doesn't need to. This is the practical payoff of keeping the Reactor a plain library: you get to choose the runtime, and you can have more than one. --- ## Documents and drives > Source: https://academy.vetra.io/learn/document-models/documents-and-drives You already know that Powerhouse stores _operations_, not snapshots. Now let's look at what those operations live _inside_ — and why the container for your files is just as much a document as the files themselves. ## A document is one typed thing Every piece of content in Powerhouse is a **document**: an instance of a document model that carries both its current state and the append-only operation log that produced it. Think of a Notion page. It has a type (the template), a body (the state), and an edit history. In Powerhouse, that last part is the canonical source — state is derived by replaying the log, not stored directly. A to-do list, a budget ledger, an invoice — each is its own document with its own ID, its own history, and its own model type that constrains which actions are valid. ## A drive is a document too Here's the twist. A **drive** is a folder of documents — and it is _itself_ a document. Compare it to your filesystem: a folder contains files, but the folder is also an entry in the parent directory. In Powerhouse, that analogy goes one level deeper: the folder has its own full operation log. When you add a file, rename a folder, move something, or remove it, that change is an operation on the drive document — not on the file inside it. This matters when you're reasoning about sync. Two collaborators can both add files to the same drive while offline. When they reconnect, Powerhouse merges their drive operation logs exactly as it would merge edits to any other document: no overwrites, no conflicts, just two interlaced sequences of operations. The drive's model type is `powerhouse/document-drive` and its actions are structural: `ADD_FILE`, `ADD_FOLDER`, `MOVE_NODE`, `DELETE_NODE`. Every one of them is an operation appended to the drive's log. ## Files live on drives When you create a document, it gets added to a drive via an `ADD_FILE` action — dispatched against the _drive_, not conjured from thin air: ```ts // This action is dispatched on the drive, not on the new document addActions({ documentId: "", actions: [ { type: "ADD_FILE", input: { documentType: "my-org/todo-list", name: "Shopping list", parentFolder: null, }, }, ], }); ``` The new document comes into existence because the drive's reducer processed that action and appended the operation. The document's own log starts empty. --- ## State and scopes > Source: https://academy.vetra.io/learn/document-models/state-and-scopes A Powerhouse document doesn't have one monolithic state — it has **scopes**. Each scope is its own state shape, its own operation log, and its own sync rules. Once you see why that separation exists, a lot of tricky design decisions become obvious. ## Two scopes, two purposes Every document starts with two scopes: - **`global`** — the state every collaborator sees. Operations land here, get appended to the global log, and replicate to all peers. - **`local`** — state that belongs only to your device. Operations here stay local; they never sync anywhere. ```ts doc.state.global; // shared with everyone doc.state.local; // yours alone doc.operations.global; // replicated log doc.operations.local; // local-only log ``` Think of a shared todo list. The `global` scope holds the items everyone cares about — titles, completion status, assignees. The `local` scope holds things like whether _you_ have the "show completed" filter on, or which item your cursor is sitting on. Those are yours. Nobody else needs them. ## Scopes stay independent Global and local are separate streams. The operation at index 5 in `global` has nothing to do with index 5 in `local`. The Reactor's sync engine replicates `global` across peers and leaves `local` completely alone. This means you can dispatch an action that only updates your filter preference — it appends to the local log, runs the local reducer, and stops there. Your teammates never see it. Meanwhile, when you add a new todo, that action appends to the global log and propagates to everyone. ## Declaring scopes in the model When you define a document model, each scope gets its own GraphQL state schema. The local schema is optional — omit it and the document only has a global scope. ```graphql type TodoListState { # global scope todos: [Todo!]! } type TodoListLocalState { # local scope (optional) showCompleted: Boolean! activeTodoId: OID } ``` When you dispatch an action, you declare which scope it targets. The reducer for that scope runs against that scope's state — the other scope is untouched. ## The one-question rule Not sure which scope to use? Ask: **should every collaborator see this change?** - Yes → `global` - No, this is mine alone → `local` That's it. The boundary isn't about importance or size — it's about audience. --- ## Actions and reducers > Source: https://academy.vetra.io/learn/document-models/actions-and-reducers You've heard the words — action, reducer, operation. This lesson makes the contract precise, so you can reason clearly about what's allowed inside each one. ## Actions are just intent An action is plain data. Nothing more. ```ts { type: "ADD_TODO", input: { id: "t-1", title: "Buy milk" } } ``` No timestamp. No hash. No index. An action describes _what you want to happen_ — it's a request, not a fact. The action alone changes nothing. That's intentional. Separating intent from result means you can validate, replay, or reject a change before it ever touches state. ## Reducers define the only legal transitions A reducer is a pure function: `(state, action) => nextState`. It's the single place where state is allowed to change, and it has strict rules: - Same inputs must always produce the same output. - No network calls, no randomness, no wall-clock time. - Synchronous only — no `async`, no promises. ```ts // Valid reducer — pure transformation addTodoOperation(state, action) { state.todos.push({ id: action.input.id, title: action.input.title, done: false, }); } ``` Why so strict? Because peers reconstruct state by _replaying_ the operation log. If a reducer reads `Date.now()` or calls `Math.random()`, two machines replaying the same log end up with different state. The whole system breaks. Non-deterministic values belong in the **action input** — generated before `dispatch`, outside the reducer: ```ts // In the editor — generate the ID before dispatching dispatch( addTodo({ id: generateId(), createdAt: new Date().toISOString(), title: input.title, }), ); ``` r.json()); state.owner = user.id", explanation: "Reducers must be synchronous and have no I/O. A network call makes the reducer async and unreliable. The owner ID should be included in the action input.", }, { label: "const t = state.todos.find(x => x.id === action.input.id); if (!t) throw new TodoNotFoundError('not found'); t.done = !t.done;", correct: true, explanation: "Pure, synchronous, deterministic — this is exactly what a valid reducer looks like. Same state + same action always produces the same result.", }, ]} /> ## Operations are what gets recorded Once the reducer runs without throwing, the Reactor stamps the action with metadata and appends it to the document log. That record is an **operation**: ```ts { index: 42, timestamp: "2026-04-15T10:00:00.000Z", hash: "sha256-...", action: { type: "ADD_TODO", input: { id: "t-1", title: "Buy milk" } }, error: null, } ``` The operation is immutable history. The action was intent; the operation is fact. If the reducer throws, the operation is still appended — but with `error` set to the message and state left unchanged. Failure is also part of the record. ## Intent vs. result The action/reducer/operation triangle gives you a clean separation: | | Carries | | ------------- | -------------------------------------------------------------- | | **Action** | Intent — a name and inputs, nothing else | | **Reducer** | Logic — the only legal way state can change | | **Operation** | Result — the action plus proof it ran (index, hash, timestamp) | A caller can't sneak state changes past the reducer. A reducer can't introduce uncertainty. An operation can't be revised after the fact. Each piece does exactly one job. --- ## Your first document model > Source: https://academy.vetra.io/learn/document-models/your-first-document-model In this lesson you'll build a minimal **Todo** document model from scratch. You'll define the schema in Vetra Studio, let codegen scaffold the types, and write the reducer that drives every state transition. You'll need: - Node.js ≥ 24 - The `ph` CLI (`npm i -g @powerhousedao/ph-cmd` or see [Installation](/academy/Build/GettingStartedBuilding/Prerequisites)) ## Set up the project From an empty directory: ```bash ph init todo-app cd todo-app pnpm install ph vetra ``` `ph vetra` launches Vetra Studio at `http://localhost:3000` and starts the file watchers. Leave it running in its own terminal throughout this lesson. ## Define the schema in Vetra Studio Open the browser and go to `http://localhost:3000`. You'll see two drives — open the **`vetra-`** drive (the source drive for your package). Click **Document Model** to create a new model specification. Give it: - **Name**: `Todo` (PascalCase, no spaces) - **Document type**: `powerhouse/todo` In the GraphQL editor, author the state schema: ```graphql type TodoState { items: [TodoItem!]! } type TodoItem { id: OID! title: String! done: Boolean! } ``` Add a module called `todos`, then add three operations: `ADD_TODO`, `COMPLETE_TODO`, and `REMOVE_TODO`. Vetra Studio generates the input types automatically. Once the schema is valid, codegen runs and emits files into your project. ## Understand what codegen produces After codegen finishes, your project contains: ``` document-models/todo/ ├── gen/ # auto-generated — never edit │ ├── actions.ts # action type unions │ ├── creators.ts # typed action creators │ ├── types.ts # TypeScript interfaces │ └── reducer.ts # reducer dispatcher ├── src/ │ └── reducers/ │ └── todos.ts # yours to implement ├── schema.graphql └── todo.json ``` Files inside `gen/` are regenerated every time the schema changes — never edit them. Your work lives entirely in `src/reducers/`. ## Write the reducer Open `document-models/todo/src/reducers/todos.ts`. Codegen scaffolds a stub that throws "not implemented" for each operation. Replace those with real logic: ```ts title="document-models/todo/src/reducers/todos.ts" export const todoTodosOperations: TodoTodosOperations = { addTodoOperation(state, action) { state.items.push({ id: action.input.id, title: action.input.title, done: false, }); }, completeTodoOperation(state, action) { const item = state.items.find((t) => t.id === action.input.id); if (item) item.done = true; }, removeTodoOperation(state, action) { state.items = state.items.filter((t) => t.id !== action.input.id); }, }; ``` Powerhouse wraps reducers in [Mutative](https://mutative.js.org/), so you write direct mutations — no need to return a new object. The framework produces an immutable snapshot from each mutation. The `state` argument the operation receives is already the scoped state (`global` or `local`). From outside the reducer, you access items as `doc.state.global.items`. ## Test it Because reducers are pure (deterministic, no I/O), you can test them without a server, a database, or a running Vetra instance: ```ts title="document-models/todo/src/tests/todos.test.ts" describe("todo reducer", () => { it("adds and completes a todo", () => { let doc = utils.createDocument(); doc = reducer(doc, addTodo({ id: "t-1", title: "Write the tutorial" })); doc = reducer(doc, addTodo({ id: "t-2", title: "Review it" })); doc = reducer(doc, completeTodo({ id: "t-1" })); expect(doc.state.global.items).toHaveLength(2); expect(doc.state.global.items[0].done).toBe(true); expect(doc.state.global.items[1].done).toBe(false); }); }); ``` Run it with `pnpm test`. The same three operations can be replayed on any peer to reconstruct exactly this state. --- ## What is an editor? > Source: https://academy.vetra.io/learn/editors/what-is-an-editor You've seen how documents store their history as operations, and how reducers produce the next state from each action. But none of that matters to a user until something renders on screen. That's the editor's job — and only that. ## An editor is a React component, nothing more An editor is a React component bound to one document type. It receives two things: the current document state, and a way to dispatch actions. Everything else — running reducers, persisting operations, syncing across peers — is handled by the Reactor. The editor doesn't know any of that exists. When a user opens a document in Connect, the host looks up which editor is registered for that document type and mounts it. If no editor is registered, there's no UI. That registration step is what links a visual component to a document model. One model can have more than one editor. A budget document might have a simple viewer and a full spreadsheet-style editor. Both are separate React components registered against the same model. ## The dividing line: editors vs reducers This is worth being explicit about, because it's easy to blur: | Job | Belongs in | | ------------------------------------------ | ---------- | | Rendering state on screen | Editor | | Dispatching an action when the user clicks | Editor | | Showing a toast notification | Editor | | Computing the next state from an action | Reducer | | Validating that a field is non-empty | Reducer | | Generating the new document history | Reducer | The pattern is simple: **reducers are pure, editors are impure.** Reducers have no access to the DOM, the network, or the clock. Editors can do all of those things freely — because they're just React components. The editor dispatches. The reducer computes. Never the other way around. ## Registration: how Connect finds your editor An editor isn't just a component — it's a component paired with metadata that says which document type it handles. That pairing is called an editor module, and it lives in `editors/editors.ts`. Connect reads this registry at startup to know which UI to mount for each document type. The registration is generated for you during the codegen phase — you don't write it by hand. But it's worth knowing it exists, because a missing registration is the most common reason an editor doesn't appear in Connect. --- ## Your first editor > Source: https://academy.vetra.io/learn/editors/your-first-editor An **editor** in Powerhouse is a React component tied to one Document Model. It has one job: turn user input into actions and render the document's state. This lesson builds a minimal editor for the Todo model from the previous lesson. You'll need `ph vetra` running and the Todo model from the previous step already in place. ## Scaffold the editor In Vetra Studio, open the **`vetra-`** drive, then click **Document Editor** to create a new editor specification. Give it: - **Name**: `todo-editor` - **Document type**: `powerhouse/todo` Once you confirm (not just save as draft — the editor must be **confirmed** for codegen to run), codegen scaffolds the editor files: ``` editors/ └── todo-editor/ ├── editor.tsx # yours to implement ├── module.ts # auto-generated, registers the editor └── index.ts ``` You can also scaffold the editor manually with: ```bash ph generate editor --name todo-editor --document-type powerhouse/todo ``` ## Implement the editor The generated `editor.tsx` takes the open document and a `dispatch` function as props. Connect passes both, and `EditorProps` types them for the Todo model. ```tsx title="editors/todo-editor/editor.tsx" addTodo, completeTodo, removeTodo, } from "todo-app/document-models/todo"; export default function Editor({ document, dispatch, }: EditorProps) { const [title, setTitle] = useState(""); return (
{ e.preventDefault(); if (!title.trim()) return; dispatch(addTodo({ id: crypto.randomUUID(), title })); setTitle(""); }} > setTitle(e.target.value)} placeholder="What needs doing?" />
    {document.state.global.items.map((t) => (
  • ))}
); } ``` Notice what the editor does _not_ do: - It never mutates state directly. - It never talks to the network. - It never generates timestamps or hashes — the Reactor handles that. All the editor does is dispatch. The reducer handles the rest. ## How editors are registered The generated `module.ts` exports an `EditorModule` object that links the component to the document type: ```ts title="editors/todo-editor/module.ts" export const TodoEditor: EditorModule = { Component: lazy(() => import("./editor.js")), documentTypes: ["powerhouse/todo"], config: { id: "todo-editor", name: "Todo Editor", }, }; ``` Your package's `editors/editors.ts` collects all `EditorModule` exports: ```ts title="editors/editors.ts" export const editors: EditorModule[] = [TodoEditor]; ``` Powerhouse reads this file to know which UI to render for which document type. ## Preview in Vetra Studio With `ph vetra` running, open the **`preview-`** drive (labelled "Vetra Preview") in the browser. Create a new Todo document there — your editor renders immediately. Any change you make to `editor.tsx` triggers a hot reload. ## What you have now You've got a full vertical slice: - A typed **document model** describing state and actions. - A pure **reducer** that defines every state transition. - A React **editor** that dispatches actions and reads `doc.state.global.items`. That's already enough to run offline, with replay, undo, and audit available. The next lesson turns the final key — sync. --- ## Editor hooks and state > Source: https://academy.vetra.io/learn/editors/editor-hooks-and-state You've written an editor shell. Now you need to actually _read_ the document and _change_ it. Powerhouse gives you a small set of hooks from `@powerhousedao/reactor-browser` to do exactly that — no manual subscriptions, no wiring. ## The editor's own document Connect renders your editor component with the open `document` and a `dispatch` bound to it as props, typed by `EditorProps` from `document-model`. The component at the top of your editor reads them there. ## The hook you'll use 90% of the time Components inside the editor don't need those props passed down. Codegen produces a typed hook for your document model. For a TodoList model, that's `useSelectedTodoListDocument`. Inside the editor it returns the document the editor was rendered with: ```tsx const [doc, dispatch] = useSelectedTodoListDocument(); ``` `doc` is a fully-typed `PHDocument`. Read state off `doc.state.global` or `doc.state.local`. The component re-renders automatically whenever an operation lands — you never poll. `dispatch` sends actions into the Reactor: ```tsx dispatch(addTodo({ id: generateId(), title: "Buy milk" })); ``` Always generate IDs _before_ calling dispatch — reducers must stay pure. ## When the document type isn't known at compile time Sometimes you're building a generic component that works across models. Two hooks cover that: - `useSelectedDocument()` — returns `[PHDocument, DispatchFn]`, untyped. Throws if nothing is selected. - `useSelectedDocumentOfType(type)` — same shape, but narrows to a specific type string. Throws if the selected document is a different type. Use the generated typed hook whenever you can. Fall back to these only when generics genuinely prevent it. ## Loading a document by ID Need a document that isn't the currently selected one? Two options: - `useDocumentById(id)` — returns `[PHDocument | undefined, DispatchFn]`. The doc is `undefined` while loading; render a spinner in that case. - `useDocument(id)` — Suspense-based. It never returns `undefined`; instead it suspends until the document is ready. Wrap the caller in ``. ## The dispatch signature Every hook's dispatch has the same shape: ```ts dispatch( actionOrActions: TAction | TAction[] | undefined, onErrors?: (errors: Error[]) => void, onSuccess?: (result: PHDocument) => void, ): void ``` Pass a single action or an array to batch. Use `onErrors` to surface validation failures in your UI. Use `onSuccess` if you need the updated document immediately after the operation lands. `dispatch` stays the same function while the document's id does, and it always dispatches against the latest document, so it is safe in effect and callback dependencies. --- ## What is Connect? > Source: https://academy.vetra.io/learn/connect/what-is-connect You've got a Reactor running and a document model defined. But where do users actually open a document, browse their files, and click things? That's Connect — the default host application Powerhouse ships, and the place where your code meets real users. ## Connect is a host, not just a UI Back in the architecture overview you saw that a _host application_ embeds a Reactor to get the operation engine, then adds its own UI on top. Connect is the most common host. It runs in a browser (or as a desktop app) and layers three things on top of the bare Reactor: - A **drives sidebar** — a file-browser-style view of all drives the user has access to. - An **editor host** — it looks at the document type you open and mounts whichever editor is registered for it. - A **sync client** — it connects to Switchboard and replicates state in real time. Closing Connect doesn't destroy the Reactor _library_ — that's just code. But the session, the UI, the sync loop, and the drive browser all go away with it. ## How Connect finds your packages When Connect starts, it reads configuration to know which document models and editors to load. The key env variable is `PH_CONNECT_PACKAGES` — a comma-separated list of packages Connect should pull in at runtime. If your todo document model isn't in that list, Connect has no idea it exists. For local development you don't usually set this manually. Running `ph vetra` spins up the full stack — Connect, Switchboard, and MCP — all pointed at your local Reactor Package automatically. ```bash ph vetra # Connect only, pointed at a specific Switchboard ph connect studio ``` On the code side, editors are registered in your package's `editors/editors.ts`: ```ts export const editors: EditorModule[] = [TodoListEditor]; ``` If that array is empty, Connect renders nothing when you open a todo document. ## Studio mode vs. production Connect runs in two modes, and from your editor's point of view they're identical: - **Studio mode** (`PH_CONNECT_STUDIO_MODE=true`) — points at your local Reactor Package. Every save reloads. This is where you build. - **Production mode** — a static bundle deployed to a server, connecting to a production Switchboard. Same editor code, different environment. The built bundle is configured entirely through `PH_CONNECT_*` environment variables — things like which Switchboard drives to default to (`PH_CONNECT_DEFAULT_DRIVES_URL`) or the log level (`PH_CONNECT_LOG_LEVEL`). These are baked in at build time and reach the browser, so never put secrets there. --- ## Drive apps > Source: https://academy.vetra.io/learn/connect/drive-apps Connect's default drive view is a folder tree with documents inside it. That's fine for generic storage — but once you have a real product, you probably want a Kanban board, a project dashboard, or a curated landing page instead. That's exactly what a **drive app** gives you. ## Editor vs. drive app — the sharp line You've already seen editors: a React component that renders and edits _one document_. A drive app is the complementary piece — it renders the _whole drive_. Think of it this way: - **Editor** — the inside of a document. One task, one contract, one budget row. - **Drive app** — the outside. A list of all tasks. A summary dashboard. A workflow screen that composes multiple documents into a single view. Connect calls one or the other depending on what's selected. When a user has a drive open but no document selected, the drive app runs. When they open a specific document, the appropriate editor takes over — and the drive app can still wrap it as a shell. ## What a drive app looks like Codegen scaffolds a drive app with a `DriveExplorer` component. It receives `children` — the active document's editor — and decides where to render it: ```tsx export function DriveExplorer({ children }: EditorProps) { const showDocumentEditor = !!children; return (
{showDocumentEditor ? children : }
); } ``` When a document is open, `children` is truthy and the editor appears. Otherwise, your custom `DriveContents` fills the space — a Kanban board, a document grid, whatever fits your product. Connect also passes the drive app the drive as `document`, with a `dispatch` bound to it, and renders it inside a `DriveProvider`. So `useSelectedDrive`, `useSelectedFolder` and `useSelectedNode` in any of its components read the drive and node the drive app is showing. ## Drive apps ship real products The combination of drive app + editors is how teams turn Powerhouse into a finished product. The drive app owns the navigation, the cross-document dashboard, and any workflow that spans multiple documents. Editors own the detail view for each one. Together they cover the full surface area of a real frontend — without leaving Connect. Hooks like `useDocumentsInSelectedDrive()` let the drive app read every document in the drive, and `showCreateDocumentModal()` wires up the "new document" flow — all from `@powerhousedao/reactor-browser`. --- ## Open .phd files with Connect > Source: https://academy.vetra.io/learn/connect/open-files-with-connect Your users have been saving `.phd` files to their machines. But double-clicking one of them does nothing — no app is registered to open it. Connect's PWA file association fixes that: install Connect as an app, and `.phd` documents open from the file manager and land straight in a drive. ## How it works A Powerhouse document is a zip archive with a vendor MIME type — `application/vnd.powerhouse.document+zip` for `.phd`, `application/vnd.powerhouse.document-model+zip` for `.phdm`. Connect's [web app manifest](https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Manifest) declares that it handles those two extensions, and when a user installs the PWA the browser registers the association with the operating system. When the OS hands a file to Connect, the browser delivers it through the [LaunchQueue API](https://developer.mozilla.org/en-US/docs/Web/API/LaunchQueue). Connect then shows the **Open document** dialog — pick a drive, import. Files opened this way travel the same path as drag-and-drop, so nothing about your document models or editors changes. ## Supported platforms File associations only work in Chromium-based browsers — Chrome, Edge, Brave. Firefox and Safari don't implement the File Handling API, so there's no association flow there at all; on those browsers users open documents by dragging a file into the Connect window. Once Connect is installed as an app, per platform: | Platform | Behavior | | -------- | -------- | | Windows | `.phd`/`.phdm` register with the OS when the PWA is installed. Double-click opens Connect; right-click → **Open with** makes it the default. | | macOS | Installing alone doesn't set the default app. Right-click a file → **Get Info** → **Open with:** → Connect → **Change All…** | | Android | Open a `.phd` in the Files app and the app picker offers Connect. | | Linux / ChromeOS | Chromium ships the feature, but how the association surfaces (open-with menus, `.desktop` entries) varies by distro and desktop environment — check the open-with dialog after installing. | ## Installing Connect as an app 1. Open your Connect URL in Chrome or Edge. 2. Click the install icon in the address bar — on desktop, the browser menu → **Cast, save and share** → **Install page as app** works too. 3. Done. The installed app now handles `.phd` and `.phdm` files. The association belongs to the **installed app**, not to a browser tab: a user who only uses Connect as a web page will never get it. ## What happens when you open a file Double-click a `.phd` (or select several at once): 1. Connect launches — or, because `launch_handler` is `focus-existing`, an already-open window comes to the front and receives the files instead of a new window per file. 2. The **Open document** dialog lists each file and preselects the user's first drive; they can switch it. 3. Click **Import** — or **Rename and import**, when the drive already holds a document with the same name and the copy is automatically renamed to avoid it. If the user has no drives yet, the dialog offers to create one first; the file stays queued and the dialog comes back once the drive exists. ## Caveats - **Existing installs.** The OS snapshots the file association at the moment the PWA is installed. If the association isn't live yet — or a later build adds a new file type — the browser picks it up on its own periodic manifest re-check; reinstalling the app is the sure fix. - **Desktop file-type icons.** Chromium doesn't use per-type document icons on desktop yet (macOS synthesizes one from the app icon, and Windows support isn't implemented), so a `.phd` shows the OS's default icon for now. - **The import dialog, not a direct open.** By design the association lands at the app root and asks for a destination drive — a manifest file handler can't dictate where inside the app a file opens, so the drive picker is as direct as the platform gets. Deploying Connect yourself? The manifest is configurable — a package or the project can declare additional file types of its own. See [Configure the PWA — File handling](/academy/Build/Launch/ConfigurePWA#file-handling). --- ## What is Switchboard? > Source: https://academy.vetra.io/learn/switchboard/what-is-switchboard You've seen Connect give users a browser UI for editing documents. Switchboard is the other side of that coin — it runs the same Reactor engine, but on a Node server, and instead of a UI it hands clients an API. Same documents, same operation log, different surface. ## Just another host Remember the host-application pattern: any runtime that boots a Reactor and loads a package is a host. Connect is one. Switchboard is another. The difference is what each host exposes outward: - **Connect** renders editors — it gives _people_ a UI. - **Switchboard** exposes GraphQL and MCP endpoints — it gives _code_ an API. Because both run the same Reactor library with the same document models installed, they stay in sync. An operation written through Connect arrives in Switchboard; a mutation dispatched via GraphQL shows up immediately in Connect. One Reactor, two surfaces. ## What Switchboard exposes Switchboard serves two protocols at once: - **GraphQL** — composed from _subgraphs_, one per installed package. Web apps, mobile clients, and integrations query documents and dispatch actions here. - **MCP** (Model Context Protocol) — the same Reactor, surfaced as tools an AI agent can call. You'll dig into subgraphs in the next lesson. For now, just think of them as "the GraphQL layer, split by package domain, then stitched back into one unified schema." ## Where processors fit in Processors are event-driven handlers that subscribe to the operation stream and fold it into _read models_ — optimised views for search indexes, dashboards, aggregates, or anything else that's awkward to derive from a single document's state at query time. Processors run inside Switchboard, and subgraphs query the read models they produce. You'll get a full lesson on processors too. The short version: if a query spans many documents or needs pre-computed data, a processor builds it; a subgraph exposes it. ## Running Switchboard locally During development, `ph vetra` spins up a local Switchboard alongside Connect. It hot-reloads your subgraphs as you edit them and attaches MCP so you can test agent workflows without a production deployment. ```bash ph vetra # Subgraphs composed — GraphQL at http://localhost:4001/graphql # MCP at http://localhost:4001/mcp ``` In production, Switchboard deploys as a Docker container — same code, persistent Reactor store, as many instances as you need behind a load balancer. --- ## Subgraphs > Source: https://academy.vetra.io/learn/switchboard/subgraphs You've got documents. You've got operations. Now you need the outside world — a web app, a mobile client, an AI agent — to be able to read and write that data over the network. That's exactly what a subgraph does. ## One endpoint, many packages Switchboard doesn't expose a single monolithic schema. Instead, every Reactor Package ships its _own_ GraphQL schema fragment — its subgraph. When Switchboard boots, it composes all of those fragments into one unified endpoint. The practical payoff: a client can query data from two completely independent packages in a single request. Neither package knows about the other. Switchboard handles the stitching. ```graphql query { todoList(id: "doc-123") { id todos { id title done } } } ``` That query hits one endpoint. Behind the scenes, it's resolved by whatever package owns the `todoList` type. ## Subgraphs are the API face of your document model If a document model defines the _shape_ of your data and the _actions_ allowed on it, the subgraph is how that shape appears to callers on the network. Each subgraph exposes three things: - **Queries** — for reading documents and processor read models. - **Mutations** — for dispatching actions to documents. - **Custom types** — domain-specific GraphQL types your resolvers return. Because each package owns its subgraph, the package is the unit of delivery. Ship a new package and its API comes with it — no central schema registry to update. ## Where mutations go A mutation in a subgraph isn't stored as a database write. It dispatches an **action** to the underlying document via the Reactor. ```ts Mutation: { async addTodo(_parent, { input }, ctx) { return ctx.reactor.dispatch("", [ { type: "ADD_TODO", input } ]); } } ``` The Reactor runs the action through the reducer, stamps it as an operation, appends it to the document history, and syncs it to connected peers. The subgraph layer is thin — it translates network calls into document actions and hands off. --- ## Processors and read models > Source: https://academy.vetra.io/learn/switchboard/processors-and-read-models A document's operation log is authoritative but not always convenient. If you want to ask "which todos did Alice complete last week?" you don't want to replay the entire log every time. That's what **processors** are for: event-driven handlers that subscribe to the operation stream and maintain a **read model** on the side. ## The mental model Think of processors as **materialised views** over the operation log. Each processor: 1. Subscribes to one or more document types (or specific documents). 2. Receives every operation as it's appended. 3. Updates its own storage — a table, an index, a counter. Your reducers stay lean; your queries stay fast; neither has to know about the other. ## The IProcessor interface Every processor implements `IProcessor` from `@powerhousedao/reactor-browser`: ```ts IProcessor, OperationWithContext, } from "@powerhousedao/reactor-browser"; export interface IProcessor { onOperations(operations: OperationWithContext[]): Promise; onDisconnect(): Promise; } ``` - `onOperations` is called each time operations arrive, in order. Batch all your updates inside one call rather than writing one-by-one. - `onDisconnect` is called when the Reactor removes the processor — use it to close connections or flush caches. ## Scaffold a processor Generate the scaffolding with: ```bash ph generate processor --name todo-stats --apps switchboard ``` This creates `processors/todo-stats/` with a class that implements `IProcessor` and a factory that registers it with the Reactor. ## A minimal processor Fill in `onOperations` to build your read model: ```ts title="processors/todo-stats/index.ts" IProcessor, OperationWithContext, } from "@powerhousedao/reactor-browser"; export class TodoStatsProcessor implements IProcessor { private counts = { added: 0, completed: 0, removed: 0 }; async onOperations(operations: OperationWithContext[]): Promise { for (const { operation, context } of operations) { if (context.documentType !== "powerhouse/todo") continue; switch (operation.type) { case "ADD_TODO": this.counts.added++; break; case "COMPLETE_TODO": this.counts.completed++; break; case "REMOVE_TODO": this.counts.removed++; break; } } } async onDisconnect(): Promise { // release any resources here } getStats() { return { ...this.counts }; } } ``` That's it. No polling, no cron, no diffing. Every operation flows through `onOperations` exactly once, in order. ## Replay-safe from day one A processor's job is to reduce the log. So if you change the processor's code — add a new counter, fix a bug, reshape the output — you can drop its state and replay from operation zero. The log never changed; only the lens did. This is how Powerhouse keeps processors "forgiving": you can evolve them without corrupting the source of truth. ## Filtering operations The factory registers a `ProcessorRecord` that tells the Reactor which operations to deliver. A typical filter targets a specific document type and scope: ```ts title="processors/todo-stats/factory.ts" export async function todoStatsProcessorFactory(): Promise { const processor = new TodoStatsProcessor(); return [ { processor, filter: { documentType: ["powerhouse/todo"], scope: ["global"], branch: ["main"], }, }, ]; } ``` When your package registers the factory with the Reactor, only operations matching that filter reach `onOperations`. ## You've seen the whole stack Reducers, editors, sync, and now processors. That's the full Powerhouse shape: ``` dispatch(action) → reducer → operation (in log) │ ├─► synced to peers └─► fanned out to processors → read models → UI / agents ``` Once you have the log, every downstream feature is just another lens on it. --- ## Sync and collaborate > Source: https://academy.vetra.io/learn/sync/sync-and-collaborate So far your Todo document lives in a single Reactor in a single tab. This lesson makes it collaborative: open it in two browsers, take one offline, and see operations reconcile when it comes back. Nothing in your model or editor changes. Sync is infrastructure — it rides on top of the operation log you already have. ## What "syncing" actually means Two Reactors hold the same document if they hold the same ordered log of operations. Sync is just the protocol that gets them there: 1. Each Reactor keeps its local log. 2. When connected, they exchange missing operations. 3. Both sides fold the combined log and arrive at the same state. Because each operation carries its own index and hash, there's no guessing what's new and no ambiguity about order. ## Try it With `ph vetra` running and the Todo model and editor from the previous two lessons in place, open two browser windows pointing at `http://localhost:3000`. In both windows, open the same Todo document from the **`preview-`** drive and start editing. Additions, completions, and deletions flow between them in under a second. Now, in window A: - Open DevTools → Network → toggle "Offline." - Add three new todos. - Complete one of window B's existing todos. Window B keeps working — it doesn't know A is offline. It also keeps editing. Turn A's network back on. Within a moment: - A's three new todos appear in B. - B's changes appear in A. - Both logs have the same operations, in the same order, with the same hashes. That's it. No merge UI, no "whose version do you want to keep?" popup. ## When do conflicts _really_ happen? The one place you still need to think is **semantic conflict**: two operations that are each fine on their own, but contradictory together. For example, if Alice renames todo `t-1` to "Groceries" and Bob renames it to "Errands" while offline, both renames will be in the log after sync, and the later one will win as the current title — but both actors' _intent_ is preserved in the history. If your app needs a different resolution rule (for example, "the first writer wins"), you encode it in the reducer — not in sync. The sync layer never has to make a judgement call, because your reducer already has. ## You've shipped the loop You now have a document model, a reducer, an editor, and a fully-synced, offline-first, multi-device experience — built on the same operation log the rest of Powerhouse uses for undo, audit, and replays. --- ## Reducers in depth > Source: https://academy.vetra.io/learn/deep-dives/reducers-in-depth Reducers are the one part of a Powerhouse app that _must_ be pure. Everything downstream — sync, replay, undo, audit — assumes that running the same reducer against the same inputs produces the same outputs, forever. This lesson covers the patterns that keep reducers tidy and the ones that quietly break them. ## Three rules, always 1. **No I/O.** No `fetch`, no `localStorage`, no `console.log`. 2. **No time or randomness.** No `Date.now()`, no `Math.random()`, no `crypto.randomUUID()` — those values come from action inputs. 3. **Mutate only the provided draft.** Powerhouse wraps reducers in Mutative, so you mutate `state` in place. Never touch module-level variables or anything outside the `state` argument. If you want any of those things, they belong in one of: - **Action inputs** — the caller provides the id, timestamp, or external data. - **Processors** — derived read models that react to operations after they're appended. - **The editor** — for UI-facing side-effects. ## State scopes Real documents have more than one kind of state. Powerhouse splits the state of a document into **scopes**: | Scope | What it holds | Who can write | | -------- | -------------------------------------- | ---------------------------- | | `global` | The canonical shape of the document | Any authenticated actor | | `local` | Per-actor UI state (cursor, selection) | Only the actor it belongs to | Reducers are split along the same lines. When you define an operation in Vetra Studio, you mark which scope it writes to. Codegen emits a typed action creator that passes the scope through to `createAction` automatically: ```ts // generated in document-models/my-doc/gen/creators.ts export const setCursor = (input: SetCursorInput) => createAction( "SET_CURSOR", { ...input }, undefined, SetCursorInputSchema, "local", ); ``` For your model's own operations, never call `createAction` directly. Use the generated creator (`setCursor(...)`) from `document-models//gen/creators.ts`. The built-in `document`-scope actions (`CREATE_DOCUMENT`, `UPGRADE_DOCUMENT`, `DELETE_DOCUMENT`) have no generated creator. `@powerhousedao/reactor` exports `createDocumentAction`, `upgradeDocumentAction` and `deleteDocumentAction` for them. Outside a reactor, call `createAction` yourself with `"document"` as the fifth argument. ## Pattern: invert for undo Because reducers are pure, undo is a pattern, not a feature you have to implement inside the reducer. For each action you support, emit a paired "inverse" action that the app can dispatch to roll it back. ```ts function invert(op: Operation): Action | null { switch (op.type) { case "ADD_TODO": return removeTodo({ id: op.input.id }); case "REMOVE_TODO": return addTodo({ id: op.input.id, title: op.input.restoreTitle }); case "COMPLETE_TODO": return uncompleteTodo({ id: op.input.id }); default: return null; } } ``` Dispatching the inverse produces a _new_ operation in the log — you keep the history, and the effect is undone. Nothing about the log is rewritten. ## Testing reducers Because reducers are pure functions, they test like pure functions. No mocking, no fixtures, no in-memory DB: ```ts test("completing a todo marks it done", () => { let doc = utils.createDocument(); doc = reducer(doc, addTodo({ id: "t-1", title: "Test" })); const afterComplete = reducer(doc, completeTodo({ id: "t-1" })); expect(afterComplete.state.global.items[0].done).toBe(true); expect(doc.state.global.items[0].done).toBe(false); // original snapshot untouched }); ``` Two things you should always cover: - **Determinism** — the same inputs produce the same output on two separate runs. - **Snapshot isolation** — the document passed into `reducer` is unchanged after the call; Mutative ensures that. If either assertion fails, your reducer isn't pure — and sync will eventually betray you. --- ## Identity and signing > Source: https://academy.vetra.io/learn/deep-dives/identity-and-signing You've seen that a document _is_ its operation log, and that the log is what syncs between peers. That raises a question every collaborative system has to answer: **who produced each operation, and can a peer trust it?** This lesson connects the operation model you already know to Powerhouse's identity and signing story. By the end you should be able to explain what a signed operation proves — and what it doesn't. ## Operations carry an actor Every operation already records _who_ dispatched it. In the foundations lesson you saw an operation shaped like this: ```json { "index": 42, "name": "completeTodo", "input": { "id": "t-1" }, "actor": "user:alice", "hash": "…" } ``` That `actor` comes from an **identity**. In Powerhouse, identities are issued through Renown — a decentralized identity layer, so a user's identity isn't owned by any single server. When someone connects in Connect, they authenticate with Renown and every action they dispatch is attributed to that identity. ## A claim is not a proof An `actor` field on its own is just a _claim_. Nothing stops a malicious peer from writing `"actor": "user:alice"` onto an operation Alice never made. In a local-first system where operations flow between machines you don't control, that's a real problem. This is where **signing** comes in. When an operation is signed, the actor's private key produces a signature over the operation's content and position in the log. Any peer can then verify, using the public key, that: - the operation really came from that identity, and - it hasn't been altered since it was signed. Because the signature covers the operation's place in the history, you can't lift a signed operation out of one document and replay it into another — the hash chain won't match. ## Three gates, not one It's worth separating the concerns that all meet at a single operation: | Gate | Question | Where it lives | | ------------------ | ------------------------------------------------------ | ---------------------- | | **Authentication** | Who is this identity? | Renown | | **Signing** | Did this identity really produce this exact operation? | Signature verification | | **Authorization** | Is this identity allowed to perform this action here? | Document permissions | | **Validity** | Is the resulting state legal? | The reducer | A signed operation can be perfectly authentic and still be rejected — because the signer lacks permission on that document, or because the reducer's rules don't allow the change. Identity establishes _trust_; it doesn't replace the other checks. ## Why this falls out of the operation log None of this needs a bolt-on audit system. Because state is already an ordered, hash-linked log of operations, attaching a signature to each entry is enough to make the whole history **tamper-evident**: change any past operation and every signature and hash after it stops verifying. The same structure that gives you deterministic replay and conflict-free sync gives you a trustworthy audit trail for free. --- ## What are workflows? > Source: https://academy.vetra.io/learn/workflows/what-are-workflows You've seen two ways to make a Powerhouse system react to something. An editor reacts to a person. A processor reacts to the operation log — but it's code you write, review, build and deploy, and changing it means another release. A **workflow** is the third way. Something happens, and a chain of steps runs. The difference is that you assemble it from blocks in Connect rather than writing it, so the person who understands the process can change it without waiting on a deploy. ## A workflow is a document A workflow is not a config file or a row in the runtime's database. It's a Powerhouse document of type `powerhouse/workflow`, living in a drive alongside your other documents. So it inherits what every document gets: an operation log of every edit, sync across Reactors, history you can replay, and the same permissions model. Editing a workflow in Connect dispatches operations. Two people can edit one. You can see who changed a step and when. A workflow document holds two copies of the workflow. The **draft** is what you edit. The **published** copy is a snapshot the draft takes when you publish. Triggers and runs use the published copy, so an edit reaches production only when someone publishes it, and a workflow that was never published can't be enabled. ## The parts ``` trigger ──▶ step ──▶ branch ──true──▶ step ──▶ step └───false──▶ step ``` **The trigger** is the one thing that starts a run. A workflow has exactly one — a new row appearing in an external service, a webhook delivery, a schedule, or a document event on the reactor itself. A workflow with no trigger is a draft that can't do anything yet. **Steps** are the work. Each names one action belonging to one piece, which you'll meet in the [next lesson](../docs/docs/02-pieces-and-connections.mdx), plus the configuration that action needs. A step's config can refer to the trigger payload and to earlier steps' output, which is how data moves down the chain. A step marked **skip** is passed over at run time: it records a `null` output and the run continues on its `next` port. **Edges** connect steps, and each leaves its source through a named **port**. A plain step has a `next` port; a branch has `true` and `false`; any step also has an `error` port, so you can route a failure somewhere useful instead of just failing the run. Decisions are made by a branch step rather than by clever expressions on the edges: the expression language is deliberately a lookup, not a small programming language, so `{{steps.check.output.severity}}` resolves a value but `{{a == b}}` does not compare anything. The [authoring reference](/academy/Build/WorkWithData/AuthoringWorkflows) has the syntax and the branch block. **Variables** are named values a workflow carries, read as `{{variables.}}`. A variable can be typed (`TEXT`, `NUMBER`, `BOOLEAN` or `JSON`), and its value is coerced when a run starts. A `SECRET` variable holds a reference to a stored secret and resolves to the secret's value at run time. ## What a run is When a trigger fires, the runtime starts a **run** and walks the published graph. Each run records the version of the published workflow it executed, so a run you inspect next month tells you what the workflow looked like *then*, not now. A run ends `SUCCEEDED` or `FAILED`. Each step runs once, and a step that runs past its `timeoutSeconds` fails. A failed run can be rerun: the steps that succeeded replay from the run journal, and execution restarts at the step that failed. ## Workflow or processor? Both react to things happening, and there's real overlap. The useful split: | | Reach for a **processor** | Reach for a **workflow** | | --- | --- | --- | | Changing it means | a code change and a deploy | editing a document in Connect | | Good at | folding many operations into a read model, computed views, anything needing a real function | calling services, moving data between them, decisions a reader can follow | | Who owns it | a developer | whoever owns the process | A workflow can't call a function you wrote — if a step needs a calculation, that calculation has to live somewhere a workflow can reach: in a reducer, behind a mutation, or inside an action of a piece your package ships. That last option is the subject of the [Building a piece](/academy/Build/WorkWithData/BuildingAPiece) tutorial, and it's what makes workflows extensible rather than limited to what ships in the box. ## Where the parts run Two halves, and they're switched on separately: - **The runtime** lives in the reactor (Switchboard). It watches triggers, starts runs and executes steps. Turn it on with `workflows.enabled` in `powerhouse.config.json`. - **Workflow Studio** lives in Connect — the canvas you author on, plus the list of workflows, connections and runs. Turn it on with `connect.app.workflowsEnabled`. ```json { "workflows": { "enabled": true }, "connect": { "app": { "workflowsEnabled": true } } } ``` Both default to off. They're independent: a headless reactor can run workflows with no Connect at all, and an operator can switch the runtime on with `PH_WORKFLOWS_ENABLED` instead — see [Configure environment](/academy/Build/Launch/ConfigureEnvironment). Next: where the blocks in those steps actually come from. --- ## Pieces and connections > Source: https://academy.vetra.io/learn/workflows/pieces-and-connections A workflow step doesn't contain code. It names one action, belonging to one **piece**, and carries the configuration that action needs. It names it with three fields: ```json { "pieceName": "@acme/piece-crm", "pieceVersion": "1.4.0", "actionName": "get-record" } ``` A trigger is named the same way, with `triggerName` in place of `actionName`: ```json { "pieceName": "@acme/piece-crm", "pieceVersion": "1.4.0", "triggerName": "new-record" } ``` The version is exact, and it is the version of the package that ships the piece, wherever the piece comes from. It records which code a step was built against. It isn't part of the block's identity, though: the same action at `1.5.0` is the same block, so upgrading a package doesn't orphan the workflows that named it. A pinned version doesn't have to be present on the reactor. When no source has the exact version, the reactor runs the closest one it finds and records the match on the step: `compatible` for a higher version of the same major (the same minor for `0.x`), `fallback` for anything else. A mismatch never blocks a run. The block fails only when no source has the piece, or none has an action or trigger of that name. The engine's own blocks are a piece too, `@powerhousedao/piece-core`, which every reactor ships: the actions `branch` and `assert`, and the triggers `schedule`, `webhook` and `manual`. Its version is the version of the workflow runtime, and it always runs the copy the reactor has installed. Don't write these fields from memory. Ask the reactor for them instead, as the [workflow authoring reference](/academy/Build/WorkWithData/AuthoringWorkflows) shows. A piece is a connector: a named bundle of actions (things a step can do) and triggers (things that can start a run), each declaring the properties it takes and the shape of what it returns. Those declarations are what let Workflow Studio draw a form for a step and offer a later step the fields an earlier one produced — before anything has run. ## Where pieces come from Three sources, and a reactor reads all three the same way. **The reactor's own piece**, `@powerhousedao/piece-reactor`, ships with workflows and needs no installing. It's how a workflow reads and writes Powerhouse documents: - the actions `document-find`, `document-get`, `document-create`, `document-dispatch` - `document-schema` and `document-types`, which report a model's shape at run time - the triggers `document-event`, `document-created`, `document-deleted` The document actions (`document-create`, `document-dispatch`, `document-get` and `document-schema`) have an advanced **Parse** option. **Exact**, the default, takes ids and JSON as given. **Extract from AI output** reads them out of a model's prose, such as a fenced JSON block or JSON after reasoning text, and reports what it read them from in `extractedFrom`. Use Extract when the input is an AI step's output. A workflow that only moves data around inside Powerhouse needs nothing else. It's also the piece to copy conventions from, such as kebab-case names. Each of its steps is bound to a reactor connection, which [Reactor access from pieces](../docs/docs/03-reactor-access.mdx) covers. **Pieces your own package ships.** A reactor package already carries document models, editors and processors; it can carry pieces the same way. This is the route for anything specific to your domain — your internal API, your calculation, your service. It's the subject of the [Building a piece](/academy/Build/WorkWithData/BuildingAPiece) tutorial. A piece's action or trigger can also read and write documents itself, by declaring reactor access. **Pieces from a registry.** A reactor reads pieces from the registry it installs packages from, and falls back to the public [Activepieces](https://www.activepieces.com) catalogue for the long tail of common SaaS connectors. Powerhouse pieces use the Activepieces authoring API, so that whole ecosystem is available without a translation layer. However a piece arrives, a step naming one of its actions looks identical. The difference is only in where the block came from. ## Connections Most pieces talk to something outside the reactor, and that something usually wants credentials. Those don't go in the workflow. A **connection** is its own document, of type `powerhouse/connection`: one credential, configured once, referenced by every step that needs it. A workflow step points at a connection by id rather than carrying an API key in its config. A workflow document syncs, has a full operation log, and is readable by anyone who can read the drive. A credential written into a step's config would inherit all of that. Keeping it in a connection means the secret itself is held by the runtime and only ever referred to by reference — what the documents carry is a pointer, not the key. A **reactor connection** is the one kind that holds no credential. It grants a step access to documents on the reactor, optionally narrowed to reads or to certain documents, and the step's run acts as the user who last published the workflow. See [Reactor access from pieces](../docs/docs/03-reactor-access.mdx). Some pieces offer more than one way to sign in, such as Slack's bot token or its OAuth flow. A connection picks one of them, and the piece runs with that method. **Signing in with OAuth.** A piece that signs in with OAuth, such as Google Drive, uses an OAuth app that you register with the service yourself: 1. In the service's developer console, create an OAuth app and add the **Redirect URL** the connection editor shows as an authorized redirect URI. 2. Paste the app's client ID and client secret into the connection. 3. Click **Connect**. A window opens on the service's own sign-in page; approve the access it asks for. The window closes, and the switchboard keeps the token and refreshes it before it expires. The connection holds only a reference to it. Because the app is yours, the service's consent screen names it, and may warn that it is unverified. A Google app left in **Testing** issues refresh tokens that expire after seven days, so publish it before relying on the connection. A piece can also declare a **connection check**, which is what lets Connect show a connection as working or broken, with the account it's actually authenticated as, before a run fails on it. The connection editor's **Test connection** button runs it on demand. ## What a piece is allowed to do Three limits shape what a step can do: **Reaching private addresses.** Piece code runs under an outbound address policy that refuses private and loopback space by default. A piece configuration is a place a user types a URL, which makes it a route into whatever your reactor can reach on its own network — so the default is to refuse. A deployment that genuinely needs a step to reach a service on its own network widens the policy by naming those addresses; it doesn't switch the guard off. This is the one that surprises people running a demo on their laptop: a piece pointed at `http://localhost:9000` will not connect until the deployment says that address is allowed. **Reaching documents.** A step reaches documents only if its block declares reactor access, and then only within its reactor connection and what the run user may do. The host checks every call. **Writing to documents.** The reactor piece's dispatch action takes an allow-list of action types. A step that dispatches can be pinned to exactly the operations it's meant to perform, so a workflow cannot do anything else to a document even if someone edits its step list — which matters when the thing composing the actions is a language model rather than a person. Ready to build one? [Building a piece](/academy/Build/WorkWithData/BuildingAPiece) takes you from `ph generate piece` to a block a workflow can call. --- ## Reactor access from pieces > Source: https://academy.vetra.io/learn/workflows/reactor-access A piece's action or trigger can read and write Powerhouse documents on the reactor running the workflow. Three things decide what it can reach: - **The piece declares it.** An action or trigger asks for `"read"` or `"write"` access, and gets a `ctx.reactor` to match. - **A reactor connection limits it.** The workflow binds the step to a connection, which can narrow access to reads. - **The run acts as a user.** The reactor decides each call for the user who last published the workflow: reads pass that user as the subject, and writes are evaluated as that user. The host makes every reactor call and checks every one. Piece code runs in a worker process and never holds the reactor itself. For now this covers the local reactor only: a step reaches the reactor its workflow runs on. ## Declaring access Declare `requireReactor` on `createAction` or `createTrigger` from `@powerhousedao/pieces-framework`: ```typescript export const archiveInvoice = createAction({ name: "archive-invoice", displayName: "Archive invoice", description: "Sets an invoice's status to ARCHIVED", requireAuth: false, requireReactor: "write", props: { invoiceId: Property.ShortText({ displayName: "Invoice", required: true }), }, async run(ctx) { const doc = await ctx.reactor.get(ctx.propsValue.invoiceId); const invoice = await ctx.reactor.getDocumentModelModuleForDocument(doc); const handle = await ctx.reactor.write.execute(doc.header.id, "main", [ invoice.actions.setStatus({ status: "ARCHIVED" }), ]); const { job } = await handle.readable; if (job.result?.allApplied === false) { throw new Error(`Invoice ${doc.header.id} was not archived`); } const updated = await handle.document(); return documentReference(updated.header); }, }); ``` The invoice model and its `setStatus` operation stand in for your own. The action reads the document, gets its document model module, builds the action with the model's own action creator, and executes it through the write half, `ctx.reactor.write`. It waits for the write to be readable, checks that the action applied, and reads the document back. It outputs a reference to the document: `{ documentId, documentType, branch, revision }`. The declaration sets the type of `ctx.reactor`: | `requireReactor` | `ctx.reactor` | | ----------------- | ---------------------------------------------------------------- | | `"read"` | `ReactorReadClient`, with the read methods only | | `"write"` | `ReactorClient`, with the read methods and the write half | | `false` or absent | none. Using `ctx.reactor` throws `UnsupportedContextMemberError` | A write method on a `"read"` action does not compile. The host also refuses it at run time, in case the declaration and the code disagree. A trigger's hooks get `ctx.reactor` the same way. A declared block's dropdown `options` functions get a read client too, with three differences. They read as the person using the editor, who must be signed in when auth enforcement is on. They never write, whatever the block declares. And a property is created before the block that uses it, so its `ctx.reactor` is typed `ReactorReadClient | undefined`; it is undefined when the block declares nothing: ```typescript const invoice = Property.Dropdown({ auth: undefined, displayName: "Invoice", required: true, refreshers: [], options: async (_values, ctx) => { if (!ctx.reactor) return { options: [], disabled: true }; const page = await ctx.reactor.find({ type: "acme/invoice" }); return { options: page.results.map((doc) => ({ label: doc.header.name, value: doc.header.id, })), }; }, }); ``` `ph build` copies `requireReactor` into each piece's `descriptor.json`, so a reactor knows what a block asks for before it runs it. `ph generate piece-action` and `ph generate piece-trigger` write `requireReactor: false` into the stub. To scaffold a declaration instead, pass `--require-reactor`: ```bash ph generate piece-action archive-invoice --require-reactor write ``` ## What `ctx.reactor` offers `ctx.reactor` is a subset of [`IReactorClient`](/academy/Reference/Reactor/ReactorClient), with the client's own signatures and types. The reference page covers each method. | Access | Methods | | ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Read | `get`, `resolveIdOrSlug`, `find`, `getOperations`, `getOutgoingRelationships`, `getIncomingRelationships`, `getOutgoingRelationshipEdges`, `getIncomingRelationshipEdges`, `getDocumentModelModules`, `getDocumentModelModule`, `getDocumentModelModuleForDocument`, `drives.getNode`, `drives.listNodes` | | Write | `newDocument`, and the write half: `write.create`, `write.createEmpty`, `write.execute`, `write.deleteDocument`, `write.drives.addFile`, `write.drives.addFolder` | | Write, deprecated | `create`, `createEmpty`, `execute`, `executeAsync`, `deleteDocument`, `drives.addFile`, `drives.addFolder` | The deprecated writes are the client's facade, kept so pieces built against them keep working. New pieces write through `write`. ### Write handles A write-half method answers once its jobs are accepted, with a write handle: - **`readable`** resolves when every pre-ready read model holds the write. Its receipt carries the job (`job.status`, `job.result`), a consistency token, `documentId` and `branch`. - **`written`** resolves when the operations are in the log, before the read models. - **`document(view?)`** waits for `readable` and reads the document back as the run user, at the receipt's token. A reducer error does not throw. The job still finishes, and `receipt.job.result.actions` holds one outcome per action: `{ kind: "applied" }`, `{ kind: "reducer-error", message, name?, code? }` or `{ kind: "denied", reason }`. `job.result.allApplied` is false when any action erred or was denied. Check it when the step must fail on a rejected action. The facade's `execute` throws `ReactorActionsFailedError` instead. A handle reads only its own step's jobs, within the step's deadline. A job that fails rejects `readable` and `written` with an error named `JobFailedError`, carrying the job. A job still running at the deadline rejects with `ReactorJobPendingError`. A piece changes a document by building actions with the document model's action creators and passing them to `write.execute`. The creators come from the model's package, from `@powerhousedao/shared/document-model` (`setName`) and `@powerhousedao/shared/document-drive` (`addFile`, `deleteNode`), or from the module `getDocumentModelModuleForDocument` returns. `write.drives.addFile` files a document into a drive of either drive type: it creates the document under the drive, then adds the drive's node for it. Its handle's document is the new file: ```typescript const filed = await ctx.reactor.write.drives.addFile( driveId, invoiceDocument, folderId, ); const file = await filed.document(); ``` `write.drives.addFolder` answers a handle with the new folder's `nodeId`. To read a drive's folders and files, use `drives.listNodes` or `drives.getNode`. Not offered to any piece: | Methods | Why | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- | | `createDocumentInDrive`, `executeBatch`, `rename`, `setPreferredEditor`, `addRelationship`, `updateRelationship`, `removeRelationship`, `moveRelationship`, `upgradeDocument`, `deleteDocuments`, on the client and on `write` | Each would have to be rebuilt on the host to check it as the run user. Build the actions and call `write.execute`. | | `drives.create`, `drives.removeNode`, `drives.renameNode`, `drives.moveNode`, `drives.copyNode`, `drives.setPreferredEditorOnNode`, on the client and on `write` | Each writes more than one document. Build the drive's actions and call `write.execute`. | | `createAsync`, `createEmptyAsync`, `getJobStatus`, `waitForJob`, `read` | The write half covers them: a handle waits for its own step's jobs. | | `subscribe` | Triggers cover document events. | | `loadBatch` | It loads operations other principals signed, which the host can neither sign nor check as the run user. | | `evaluateActions`, `isDocumentIdTaken`, `describe`, `isServed`, `getCreateSignaturePolicy`, `getCreateProtocolVersions` | Host internals. `isDocumentIdTaken` would answer for documents the user cannot read. | Calling one throws `ReactorAccessDeniedError`, naming the method. The host also refuses: - **A chosen subject.** A call that passes `view.subject`, or `execute`'s `subject`, is refused, as is a handle's `document({ subject })`. The host sets the run user as the subject. - **A signed action.** An action carrying `context.signer` is refused. The host signs every action, and a new document gets the host's create signature policy. - **Document lifecycle actions through `execute`**: `CREATE_DOCUMENT`, `DELETE_DOCUMENT`, `UPGRADE_DOCUMENT`, `PURGE_DOCUMENT` and the relationship actions. Create with `write.create` or `write.createEmpty`, and delete with `write.deleteDocument`. - **A cascade delete.** `deleteDocument` with `PropagationMode.Cascade` is refused. ### Denied actions Before submitting a write, the host asks the reactor whether the run user may apply each action. When it may not, the write throws `ReactorAccessDeniedError` and submits nothing. The message names each denied action and ends with their indexes in the write's action list: ```text The run user may not apply SET_STATUS (no grant) to document 3f2a…; denied action indexes: [1] ``` `deniedActionIndexes(error)` from `@powerhousedao/pieces-framework` reads them back, and answers `undefined` for any other error. A drive write's index is that of its drive-side action, `0`. ## At run time ### Waiting for writes A write-half method returns once its jobs are accepted; a handle's `readable` waits for them. A deprecated facade write returns once its jobs are applied and readable, and checks each action's outcome, so a reducer error or a denial fails the call. A wait lasts until the step's deadline, which is the step's `timeoutSeconds` or the runtime's default timeout, and stops a little before it to report why. A document the run creates gets the run user's grant before the create's handle is returned, so `write.create` waits for the create to land when the run has a user. ### Errors Errors cross from the host to the worker by `name` and `message`, so test `error.name`, never `instanceof`. `@powerhousedao/pieces-framework` exports each name as a string constant: | `error.name` | Meaning | | ------------------------------- | ------------------------------------------------------------------------------------- | | `ReactorAccessDeniedError` | The declaration, the reactor connection or the document's auth refused the call. | | `ReactorJobFailedError` | A deprecated write's job failed. | | `ReactorActionsFailedError` | A deprecated write's job finished, but some of its actions failed. | | `JobFailedError` | A write handle's job failed. Not exported: compare the string. | | `ReactorJobPendingError` | A write's job was still running at the step deadline. Its actions may yet be written. | | `ReactorRequestClosedError` | A `ctx.reactor`, or a page's `next`, was used after its step settled. | | `DocumentModelUnavailableError` | The worker has no document model for that type. See below. | ```typescript const handle = await ctx.reactor.write.execute(ctx.propsValue.id, "main", [ setName("Settled"), ]); try { return await handle.readable; } catch (error) { if (error instanceof Error && error.name === ReactorJobPendingError) { return { pending: true }; } throw error; } ``` ### Paging Listings return a page at most 100 long, whatever `paging.limit` asks for. A page's `next()` works within the step that read it: ```typescript let page = await ctx.reactor.find({ type: "acme/invoice" }); for (;;) { ids.push(...page.results.map((doc) => doc.header.id)); if (!page.next) break; page = await page.next(); } ``` A `ctx.reactor`, and every page it returned, closes when its step settles. A later call throws `ReactorRequestClosedError`. To resume a listing in a later step, return `nextCursor` and pass it back as `paging.cursor`: ```typescript const page = await ctx.reactor.find({ type: "acme/invoice" }, undefined, { cursor: ctx.propsValue.cursor ?? "", limit: 50, }); return { results: page.results.map((doc) => ({ header: doc.header })), nextCursor: page.nextCursor, }; ``` Listings read as the run user, so the reactor leaves out documents that user cannot read. ### Document models in the worker The worker holds the reactor's document model modules, so a piece calls their reducers, `utils` and action creators directly. A model is imported the first time a piece asks for its type, through `getDocumentModelModule` or `getDocumentModelModuleForDocument`. `getDocumentModelModules` imports every model the reactor has. A type the worker has no model for throws `DocumentModelUnavailableError`. Documents of that type can still be read and written: only the module is missing. The worker's modules decide nothing, because the reactor applies every action on the host. ## Reactor connections A reactor connection is a `powerhouse/connection` document with `authType: REACTOR`. Every step whose block declares `requireReactor` needs one, including the steps of the reactor's own piece, `@powerhousedao/piece-reactor`. A step without one fails with `ReactorAccessDeniedError`. A reactor connection holds no secrets. Build one with these operations, in order: 1. **`SET_CONNECTION_NAME`**: `{ name: "Invoices" }`. 2. **`SET_CONNECTOR`**: `{ connectorId: "@powerhousedao/reactor#reactor", authType: "REACTOR" }`. The connector id is reserved for reactor connections, and any piece may bind it. This sets the config to `{ endpoint: "local" }`. 3. **`SET_CONFIG`**, to limit it to reads: ```json { "config": { "endpoint": "local", "access": "read" } } ``` The config fields: | Field | Meaning | | ---------- | ---------------------------------------------------------------------------------- | | `endpoint` | `"local"`, the reactor the workflow runs on. It is the only value. | | `access` | `"read"` limits the connection to reads. Absent, it allows what the step declares. | `SET_CONFIG` refuses unknown fields, so a misspelt one fails instead of being ignored. "Check connection" on a reactor connection checks its config; the reactor decides each call. To bind a connection, set `reactorConnectionId` on the step (`ADD_STEP` or `UPDATE_STEP`) or on the trigger (`SET_TRIGGER`), beside `connectionId`. It must be a connection id, not an expression. ## Who a run acts as A run acts as the **run user**: the user who signed the workflow's latest publish. Runs execute the published snapshot, so the publisher vouches for what runs. Who or what fired the run makes no difference. - **Testing a single step** in Workflow Studio acts as you, the tester. - **Dropdown options** read as you, and never write. - **On publish, on enable, when the Switchboard starts and before each run**, the host checks that the reactor serves every reactor connection the snapshot binds to the run user. While it does not, the workflow's trigger is not armed and a run fails. A later publish or enable that passes the check arms it. Every call then passes these checks, in order: 1. **The declaration.** A `"read"` block cannot call a write method. 2. **The connection's `access`.** 3. **The reactor's document auth**, as the run user. Reads pass the run user as the subject. `execute`, `deleteDocument` and the drive writes are evaluated as the run user (`evaluateActions`) before the host signs them. A create needs only checks 1 and 2; the reactor admits it by the Switchboard's signature. The run records each document it reads or writes, and a write's documents when it is submitted, so run visibility and erasure cover a write even when reading it back fails. A document the run creates gets an auth grant for the run user. ### Auth enforcement off and on The reactor's `REACTOR_AUTH_ENFORCEMENT` flag (see [Authorization](/academy/Reference/Reactor/Authorization)) changes two things: | | Enforcement off | Enforcement on | | ----------------------------- | ------------------------------------ | -------------------------------------------------- | | A workflow published unsigned | has reactor access, with no run user | gets no reactor access. Publish it while signed in | | Check 3, the document's auth | skipped. Checks 1 and 2 still apply | applies, and the Switchboard needs grants as well | ### Grants for the Switchboard The reactor admits a write by its signer, and the Switchboard signs every write a run makes. So under enforcement, a write lands only when the run user and the Switchboard may both make it. Every document a step writes needs a grant for the Switchboard as well as one for the run user. Grants are per document: a drive's grants do not cover the documents in it. When `deleteDocument` removes the document's parent relationships, only the Switchboard's grant on each parent is checked. The workflow-runtime subgraph tells you what to grant: ```graphql query { workflowRuntime { authEnforcement reactorIdentity { address key } authConditions } } ``` - `authEnforcement` answers any caller, signed in or not. - `reactorIdentity` is who the Switchboard signs as: its `did:key`, and its address when it has a signed-in identity. Signed-in callers only. - `authConditions` says which grant form to use. Signed-in callers only. With `authConditions` true, grant the Switchboard by key. Otherwise grant it by address: ```typescript // With authConditions true. setGrant({ grant: { id: "workflow-host", description: "The Switchboard that runs workflows", effect: "allow", principal: { match: { eq: [{ attr: "subject.key" }, { lit: key }] } }, capability: { can: "execute" }, }, }); // Otherwise, with principal: { address }. ``` A grant by address also covers the operator whose `ph login` identity the Switchboard signs with. Workflow Studio's reactor connection settings are built on these queries. With enforcement on, they ask you to sign in, and grant the Switchboard `execute` on the documents you pick: global scope, plus the document-scope `DELETE_DOCUMENT`, `ADD_RELATIONSHIP` and `REMOVE_RELATIONSHIP` that creates in a drive and deletes need. For a step that calls `deleteDocument`, grant on the document and on the drives that hold it. ## The reactor piece's outputs The steps of `@powerhousedao/piece-reactor` output: - **`document-create` and `document-dispatch`**: a reference to the document they wrote, `{ documentId, documentType, branch, revision }`. `revision` is the operation count per scope after the write. A later step reads `{{steps.create.output.documentId}}`; to use the document's state, read it with `document-get`. - **`document-get`**: the document as `{ header, state }`, so a later step reads `{{steps.get.output.state.global.}}` or `{{steps.get.output.header.id}}`. - **`document-find`**: `{ results, nextCursor }`. Each result is `{ header }`, or `{ header, state }` with **Include state** on. Pass `nextCursor` to the step's **Cursor** option to resume. With **Parse** set to **Extract**, a step also outputs `extractedFrom`, beside the document or the reference. Outputs leave out `operations` and `clipboard`. ## Documents in the run journal The run journal does not store document state: - **Writes output a reference.** The journal stores it as it is. - **Reads keep state in the run, but journal a reference.** Within the run, later steps read the state the step returned. When the journal records a step's output, it replaces every value shaped like a reactor document, `{ header, state }` at any depth, with `{ "$documentRef": { documentId, documentType, branch, revision } }`. This applies to every piece's output, before redaction and the size cap. - **Reruns re-read.** A rerun runs a step declaring `requireReactor: "read"` again when its journaled output holds a `$documentRef`, so the step reads the document as it is now. Every other succeeded step, including a write whose output holds a document, reuses its journaled output: a rerun never repeats a write. The editor shows a journaled reference with the document's type, id and revision, and an **Open document** action. It opens the document as it is now: reading a document at a past revision is not supported yet ([#3179](https://github.com/powerhouse-inc/powerhouse/issues/3179)). The expression picker offers a referenced document's `state.global` fields from its model's state schema. Testing a step that reads an earlier step's document reads that document as it is now. --- # Reference ## Powerhouse Architecture > Source: https://academy.vetra.io/academy/Reference/Architecture/PowerhouseArchitecture **Vetra is part of the Powerhouse Ecosystem** and acts as the builder platform for creating an independent, open-source and decentralized back-end for any SaaS, ERP, CMS or CRM needs. ## Local First. Built to Scale. Vetra helps you build any type of web application on a **Reactive Document Architecture**. Define workflows once, deploy them globally, and co-own the software you create. The architecture is built on a minimal but powerful tech-stack: **GraphQL**, **TypeScript**, and **React**. ### Reactive Document Architecture The Powerhouse framework is built around structured document models and declarative design: - **Reactive**: Real-time, responsive, and message-driven with an elastic scalable architecture. - **Document-Centric**: Documents as local-first, self-contained data structures and nodes in a decentralized network. - **Git-like UX**: State-of-the-art editing with history branching, merging, and commenting. - **Stateful**: Documents with well-defined operations as state transitions become mini-APIs. - **Scalable**: CQRS and EDA-inspired architecture with read models for data aggregation. ### Specification Driven Design & Development Complementing the Reactive Document Architecture, Powerhouse embraces **Specification Driven Design & Development**. This approach enables you to communicate your solution and intent through structured specification documents designed for AI collaboration. Specifications serve as a shared language—enabling precise, iterative edits that turn messy intent into clean execution. Our documents are machine-readable and executable, laying the groundwork for a "Git for Intent" for your AI agents. Therefore, Vetra is the stack for sovereign infrastructure and AI-run, AI-mediated, AI-executed business, organizations, and networks. ## Target Audiences Vetra supports a variety of organizations with a headless open-source back-end: - Decentralized Autonomous Organizations (DAOs) - Scalable Network Organizations (an evolution of DAOs within the Powerhouse framework) - NGOs and Governmental Organizations - Cooperatives and distributed teams ## Host Applications The Powerhouse ecosystem makes use of 4 core host applications that together form a modular, scalable operating system for any organization or business. Each application serves a distinct role, yet they are interdependent—working as a unified system to streamline operations, enhance collaboration, and drive automation. 1. **Connect** – A collaboration hub and private workspace for independent contributors. 2. **Switchboard** – The data infrastructure and API engine for managing remote instances. 3. **Fusion** – An SDK for visualizing collected data and public-facing collaboration. 4. **Renown** – A decentralized reputation and identity system. ## How It All Connects: Reactors The host applications sync their documents with one another through **Reactors**. A reactor is a node within any Powerhouse network responsible for storing documents, resolving conflicts, and verifying document event histories. Reactors can be configured for: - **Local Storage** – For offline or on-device access. - **Cloud Storage** – For centralized, scalable data management. - **Decentralized Storage** – Such as Ceramic or IPFS for distributed storage. The documents within the network react to one another through the **DocSync** protocol—which sends updates from one reactor to another, ensuring all data stays synchronized across the system. ![Powerhouse Host Apps Interaction](../docs/docs/images/PowerhouseArchitecture.png) ## Powerhouse Platforms With the help of these host applications, Powerhouse is launching 2 platforms that demonstrate the infrastructure that can be built with the Powerhouse tech-stack: - [**Vetra Builder Platform**](https://vetra.io/): Sovereign infrastructure for scalable network organizations—local first, built to scale. - [**Achra Decentralized Operations Platform**](https://achra.com/): Where open organizations can procure and purchase services. --- ## Working with the Reactor > Source: https://academy.vetra.io/academy/Reference/Reactor/WorkingWithTheReactor **TIP:** Document models are the common design pattern that is used for all documents and files. DocSync is a decentralized synchronization protocol that is storage agnostic. **Document Models** are _what_ is synced and **DocSync** is _how_ document models are synced. But who is doing the syncing? We call these participants **Reactors**. ### Powerhouse Reactors **What is a Reactor?** Powerhouse Reactors are the nodes in the network that store documents, resolve conflicts and rerun operations to verify document event histories. Reactors can be configured for local storage, centralized cloud storage or on a decentralized storage network. A Reactor is essentially a storage node used in Powerhouse's framework to handle documents and traditional files. It supports multiple storage solutions, including: - **Local Storage**: For offline or on-device access. - **Cloud Storage**: For centralized, scalable data management. - **Decentralized Storage**: Such as Ceramic or IPFS, enabling distributed and blockchain-based storage options. ### Core Functions of Reactors - **Data Synchronization**: Reactors ensure that all data, whether local or distributed, remains up-to-date and consistent across the system. - **Modular Storage Adapters**: They support integration with different storage backends depending on organizational needs. - **Collaboration Support**: Reactors facilitate document sharing and peer-to-peer collaboration across contributors within the network. **TIP:** The DocSync protocol _sends updates from one reactor to another_ - **smashing document operations into one another** - to ensure all data is synced. A **reactor** is responsible for storing data and resolving merge conflicts. Editing data and submitting new operations must be done through Powerhouse's native applications (Connect, Switchboard, Fusion). Each instance of these applications contains a Reactor that is responsible for storing data and syncing data through DocSync. In other words, Powerhouse applications are how Reactors can be accessed, manipulated, steered, visualized and modified. A local Connect desktop application's reactor can therefore sync with the Reactor of a remote drive (e.g. Switchboard instance). Powerhouse Storage Layer ### Why Are Reactors Important? They are key to ensuring the scalability and resilience of decentralized operations. By acting as the backbone for document models in the Powerhouse framework, they enable seamless version control and event-driven updates. Reactors provide the foundation for advanced features like real-time collaboration, history tracking, and decentralized audits. This modular, flexible infrastructure enables organizations to build efficient and robust decentralized systems, tailored for modern network organizations ## Getting a reactor client Whichever host you run in, the client comes in two halves: reads go through `read` (`IReactorReadClient`) and writes through `write` (`IReactorWriteClient`), whose writes return a handle to wait on. `IReactorClient` is the single object that carries both. How you obtain them depends on where your code runs. **In an app (React).** Call the `useReactorClient()` hook from `@powerhousedao/reactor-browser`. It returns the browser client for the running reactor, with `client.read` and `client.write`. `useReactorHalves()` returns `{ read, write }` alone, and `window.ph.reactor` holds the same pair. The browser halves carry a subset: `read` has `get`, `getMany`, `find`, `getOperations`, `subscribe`, `waitForJob`, and `getJobStatus`; `write` has `execute`, `create`, and `deleteDocument`. ```typescript const client = useReactorClient(); const handle = await client.write.execute(docId, "main", actions); const updated = await handle.document(); ``` The same module exposes sibling hooks: `useSync` (the `ISyncManager`), `useSyncList` (its remotes), `useModelRegistry` (the document-model registry), `useDatabase` (the Kysely database), and `usePGlite` (the PGlite instance). **Standalone (scripts, tests, servers).** Build a client with `ReactorClientBuilder`. Wire a reactor with either `.withReactorBuilder(builder)` or `.withReactor(reactor, eventBus, documentIndexer, documentView)`, add `.withSigner(...)` and `.withDocumentModelLoader(...)` as needed, then call `.build()` (returns the client) or `.buildModule()` (returns the client, its `read` and `write` halves, and the wired module). ```typescript const client = await new ReactorClientBuilder() .withReactorBuilder(reactorBuilder) .withSigner(signer) .build(); ``` `buildModule()` throws if neither `withReactorBuilder` nor `withReactor` was set. For the low-level builder detail, see [Advanced Reactor Usage](/academy/Reference/Reactor/AdvancedReactorUsage). **In a generated subgraph (server-side).** A generated subgraph extends `BaseSubgraph`. Resolvers get the halves on the GraphQL context as `ctx.clients`: `ctx.clients.read` is always there, and `ctx.clients.write` exists only while a Mutation field runs. Reaching for `ctx.clients.write` in a query or subscription throws `WriteClientUnavailableError` naming the field. For setup-time lookups outside a resolver, use `subgraph.readClient` (`this.readClient` inside the subgraph class). ```typescript // resolvers.ts export const getResolvers = (subgraph: BaseSubgraph) => ({ Query: { todoList: (_parent: unknown, args: { id: string }, ctx: Context) => ctx.clients.read.get(args.id), }, Mutation: { renameTodoList: async ( _parent: unknown, args: { id: string; name: string }, ctx: Context, ) => { const handle = await ctx.clients.write.rename(args.id, args.name); return handle.document(); }, }, }); ``` For attachments, a resolver calls `subgraph.attachmentsFor(ctx)`, which returns an `IAttachmentClient` bound to the request's caller. See [Attachment service](/academy/Reference/Reactor/AttachmentService#inside-the-switchboard). **In a generated processor (server-side).** The processor factory builder receives the host module. A processor reads through `module.read` and writes through `module.dispatch` (`execute`, `createInDrive`, `deleteDocument`), so it never needs the write client. `IProcessorHostModule` from `@powerhousedao/reactor-browser` (or `@powerhousedao/reactor-api`) exposes `relationalDb`, `analyticsStore`, `dispatch`, `getReadModel`, `read`, and `attachments`. See [Processors](/academy/Reference/Reactor/Processors) for the full registration flow. ```typescript // factory.ts (generated scaffold) export const factoryBuilder: ProcessorFactoryBuilder = (module) => { const { read, dispatch } = module; return async () => { // use read.get(...), read.drives, dispatch.execute(...) return []; }; }; ``` ## The ReactorClient API The reactor client is the primary interface for interacting with a reactor programmatically. It wraps lower-level APIs to provide a simpler, Promise-based interface for document operations, split into a read half and a write half. How the client and the reactor's components fit together: ![Reactor components: ReactorClient over IReactor, with the write side, event bus, read side, sync and storage](../docs/docs/images/reactor-components-light.png) ```typescript ``` **INFO:** `@powerhousedao/reactor-browser` re-exports a curated subset of reactor types for browser environments (editors, drive-apps, subgraphs) — including `IReactorClient`, `DocumentChangeType`, `ReactorBuilder`, and `ReactorClientBuilder`. Other types used on these pages (`ViewFilter`, `SearchFilter`, `PagingOptions`, `PagedResults`, `PropagationMode`, `JobInfo`, `JobStatus`, `DocumentChangeEvent`, `OperationFilter`, `IReactor`, `ReactorEventTypes`, `SyncEventTypes`, `QueueEventTypes`) are not re-exported; import them from `@powerhousedao/reactor`. Outside the browser, import everything from `@powerhousedao/reactor`. ### Reading documents (`client.read`) Reads never enqueue. `get`, `getMany`, `find`, `resolveIdOrSlug`, and the relationship reads take an optional `consistencyToken` as their last parameter, which makes the read wait until the read models hold a write you made. | Method | Description | | ------------------------------------------------------------------------------ | ------------------------------------------------- | | `get(identifier, view?)` | Retrieve a document by id or slug | | `resolveIdOrSlug(identifier)` | Resolve an id or slug to the canonical document id | | `getOutgoingRelationships(sourceIdentifier, relationshipType, view?, paging?)` | List documents related from a source | | `getIncomingRelationships(targetIdentifier, relationshipType, view?, paging?)` | List documents related into a target | | `find(search, view?, paging?)` | Search documents by type, parentId, ids, or slugs | | `getOperations(documentIdentifier, view?, filter?, paging?)` | Retrieve operations for a document | | `getDocumentModelModules(namespace?, paging?)` | List registered document model modules | | `getDocumentModelModule(documentType)` | Get a specific document model module | The optional `ViewFilter` lets you target a specific branch, set of scopes, or revision. On the client it also names the subject the read is gated for: ```typescript type ViewFilter = { branch?: string; scopes?: string[]; revision?: number; subject?: AuthSubject; // client read gate only, ignored by IReactor }; ``` The `SearchFilter` lets you narrow results: ```typescript type SearchFilter = { type?: string; parentId?: string; ids?: string[]; slugs?: string[]; }; ``` All list methods support pagination via `PagingOptions` (`{ cursor, limit }`) and return `PagedResults` with a `next()` helper for fetching the next page. ### Writing documents (`client.write`) Every write resolves once its job is accepted and returns a `WriteHandle`. Await `handle.written` (operations are in the log), `handle.readable` (read models hold the write), or `handle.document()` (the updated document). Each receipt carries a `token` to pass to a read. | Method | Description | | ------------------------------------------------------------------------------------------- | ------------------------------------------------------------------ | | `create(document, parentIdentifier?)` | Create a document from a full `PHDocument` object | | `createEmpty(documentModelType, options?)` | Create an empty document of a given type | | `execute(documentIdentifier, branch, actions)` | Sign and submit actions on one document | | `executeBatch(request)` | Submit multiple jobs in dependency order; returns a `BatchWriteHandle` | | `loadBatch(request)` | Load batches of pre-existing operations across documents | | `rename(documentIdentifier, name, branch?)` | Rename a document | | `setPreferredEditor(documentIdentifier, preferredEditor, branch?)` | Set the document's preferred editor (`null` clears it) | | `addRelationship(sourceIdentifier, targetIdentifier, relationshipType, metadata?, branch?)` | Add a typed relationship between two documents | | `updateRelationship(sourceIdentifier, targetIdentifier, relationshipType, metadata, branch?)` | Replace an existing relationship's metadata | | `removeRelationship(sourceIdentifier, targetIdentifier, relationshipType, branch?)` | Remove a typed relationship between two documents | | `moveRelationship(sourceParent, targetParent, targetIdentifier, relationshipType, branch?)` | Move a relationship from one source to another | | `deleteDocument(identifier, propagate?)` | Delete a document (`PropagationMode.Cascade` deletes children too) | | `deleteDocuments(identifiers, propagate?)` | Bulk delete | ```typescript const handle = await client.write.execute(docId, "main", actions); const { token } = await handle.written; const doc = await client.read.get(docId, undefined, undefined, token); ``` To create a document inside a drive, use `client.write.drives.addFile`. See the [Drives](/academy/Reference/Reactor/ReactorClient#drives) section on the IReactorClient reference page. ### Subscribing to changes ```typescript const unsubscribe = reactorClient.subscribe( { type: "powerhouse/todo-list" }, // SearchFilter (event) => { // event.type is one of: Created, Deleted, Updated, // ChildAdded, ChildRemoved console.log(event.type, event.documents); }, ); ``` ### Job tracking Every write creates a job. A job tracks the lifecycle of a set of actions as they move through the reactor. A handle's stages wait on the job for you; to get the raw `JobInfo`, pass `handle.jobId` to the read half. ```typescript const handle = await reactorClient.write.execute(docId, "main", actions); const completed = await reactorClient.read.waitForJob(handle.jobId); ``` You can also poll with `read.getJobStatus(jobId)`. For the full API reference, see [IReactorClient API Reference](/academy/Reference/Reactor/ReactorClient). ## Job lifecycle Every mutation in the reactor is processed as a **job**. Jobs move through these statuses: ``` PENDING → RUNNING → WRITE_READY → READ_READY ↘ FAILED ``` | Status | Meaning | | ------------- | --------------------------------------------------------------------- | | `PENDING` | Job is queued but not yet started | | `RUNNING` | Job is currently being executed by the reducer | | `WRITE_READY` | Operations have been written to the operation store | | `READ_READY` | All read models have finished processing — document is fully readable | | `FAILED` | Job encountered an unrecoverable error | Only `READ_READY` and `FAILED` are terminal statuses. `write.execute()` resolves as soon as the job is accepted; `handle.written` resolves at `WRITE_READY` and `handle.readable` at `READ_READY`. Both reject with `JobFailedError` when the job reaches `FAILED`. The path a job takes from the queue to `READ_READY`: ![Reactor write path: queue, job executor (signatures, authorization, reduce, commit), JOB_WRITE_READY, read models, JOB_READ_READY](../docs/docs/images/reactor-write-path-light.png) ## Reactor event system The reactor uses an internal event bus to coordinate between subsystems. The event-type enums you subscribe to most often are: ### Core job events (`ReactorEventTypes`) | Event | Numeric ID | When it fires | | ----------------- | ---------- | --------------------------------------------- | | `JOB_PENDING` | 10001 | Job is registered and waiting to execute | | `JOB_RUNNING` | 10002 | Job starts executing | | `JOB_WRITE_READY` | 10003 | Operations are written to the operation store | | `JOB_READ_READY` | 10004 | All read models have finished processing | | `JOB_FAILED` | 10005 | Job failed with an unrecoverable error | | `READMODEL_BATCH_COMPLETED` | 10006 | The read-model coordinator finishes a projection batch (carries per-stage timings) | | `READMODEL_INDEXED` | 10007 | An individual read model finishes indexing a batch in one stage | | `MODEL_LOADED` | 10008 | The resolver loads a document model module on demand | ### Sync events (`SyncEventTypes`) | Event | Numeric ID | When it fires | | -------------------------- | ---------- | ------------------------------------------------ | | `SYNC_PENDING` | 20001 | Sync operations are queued in outboxes | | `SYNC_SUCCEEDED` | 20002 | All sync operations for a job succeed | | `SYNC_FAILED` | 20003 | At least one sync operation failed | | `DEAD_LETTER_ADDED` | 20004 | A sync operation is moved to dead letter storage | | `CONNECTION_STATE_CHANGED` | 20005 | Remote connection state changes | ### Queue events (`QueueEventTypes`) | Event | Numeric ID | When it fires | | --------------- | ---------- | --------------------------------------- | | `JOB_AVAILABLE` | 10000 | Queue has work available for processing | ### Executor lifecycle events (`JobExecutorEventTypes`) The executor managers emit a lower-level set of events for individual job execution and executor startup/shutdown. Most application code subscribes to `ReactorEventTypes` instead. | Event | Numeric ID | When it fires | | ------------------ | ---------- | -------------------------------------- | | `JOB_STARTED` | 20000 | An executor starts running a job | | `JOB_COMPLETED` | 20001 | An executor finishes a job | | `JOB_FAILED` | 20002 | An executor's job fails | | `EXECUTOR_STARTED` | 20003 | An executor starts | | `EXECUTOR_STOPPED` | 20004 | An executor stops | ## Configuring your reactor In addition to the choice of storage, Reactors also have other configurations. - The **operational data** and **read models** associated with the document models inside a reactor allow to query the gathered data inside a document model or quickly visualize the crucial insights at a glance. - **Processors** are components that receive operations and perform side effects — analytics tracking, relational database indexing, webhooks, and more. You register processor factories with the reactor, and it automatically creates processor instances for each drive. The processor pipeline works as follows: 1. **Operations are written** — a job completes its write phase, persisting operations to storage 2. **Pre-ready read models update** — built-in read models like `DocumentView` and `DocumentIndexer` update their state 3. **`JOB_READ_READY` event fires** — signaling that the document is fully readable 4. **Post-ready read models update** — the `ProcessorManager` routes matching operations to user-defined processors via `onOperations()` For a step-by-step guide to building processors, see [Building a Processor](/academy/Build/WorkWithData/BuildingAProcessor). For the reactor-side registration API, see [Processors](/academy/Reference/Reactor/Processors). ### Ordering guarantees - **Global ordinal**: Every operation gets a monotonically increasing `ordinal` in its `OperationContext`, enabling cross-document ordering - **Within a processor**: Operations arrive sorted by ordinal (chronological order) - **Between processors**: Processors for the same drive execute in parallel — there is no inter-processor ordering guarantee - **Per-document serialization**: The queue serializes execution per document, even across scopes and branches - **Catch-up on restart**: Processors automatically replay missed operations after a restart (each processor's progress is tracked via the `ProcessorCursor` table) If you are working with the Reactor directly or need additional information regarding its architecture you can visit: https://github.com/powerhouse-inc/powerhouse/blob/main/packages/reactor/docs/ARCHITECTURE.md --- ## IReactorClient > Source: https://academy.vetra.io/academy/Reference/Reactor/ReactorClient The reactor client is the primary way to interact with a Powerhouse reactor programmatically. It comes in two halves: - **`client.read`** (`IReactorReadClient`) answers from read models and never enqueues a job. - **`client.write`** (`IReactorWriteClient`) enqueues jobs. Every write resolves once its job is accepted and returns a [`WriteHandle`](#writehandle) you await for the commit or the updated document. `IReactorClient` carries both as `read` and `write`. It also exposes the read methods directly, so `client.get(...)` and `client.read.get(...)` are the same call. Write through `client.write`. ```typescript IReactorClient, IReactorReadClient, IReactorWriteClient, WriteHandle, } from "@powerhousedao/reactor"; ``` **INFO:** `@powerhousedao/reactor-browser` re-exports a curated subset of reactor values and types for browser environments (editors, drive-apps, subgraphs). `IReactorClient`, `IReactorReadClient`, `IReactorWriteClient`, `WriteHandle`, `WriteReceipt`, `DocumentChangeType`, `ReactorBuilder`, and `ReactorClientBuilder` are re-exported there. Most other types on this page are not re-exported and must come from `@powerhousedao/reactor`: `ViewFilter`, `SearchFilter`, `PagingOptions`, `PagedResults`, `PropagationMode`, `JobInfo`, `JobStatus`, `DocumentChangeEvent`, and `OperationFilter`. Outside the browser (a standalone Node.js script, CLI tool, or server-side processor), import everything from `@powerhousedao/reactor`. For an architectural overview of the reactor, see [Working with the Reactor](/academy/Reference/Reactor/WorkingWithTheReactor). For the low-level `IReactor` interface and access to internal components, see [Advanced Reactor Usage](/academy/Reference/Reactor/AdvancedReactorUsage). ## Common parameter types Several types appear across multiple methods. They are described here once. ### `ViewFilter` Targets a specific branch, scopes, or revision when reading documents, and names the subject the read is gated for. ```typescript type ViewFilter = { branch?: string; scopes?: string[]; revision?: number; subject?: AuthSubject; }; ``` | Field | Description | | ---------- | ----------------------------------------------- | | `branch` | The branch to read from (e.g. `"main"`) | | `scopes` | Scopes to include (e.g. `["global"]`) | | `revision` | Read the document at a specific revision number | | `subject` | Who the [read gate](/academy/Reference/Reactor/Authorization) decides for. Defaults to the client's signer. Set it per request when one client serves many principals. `IReactor` ignores it | `AuthSubject` is `{ address?: string; key?: string }` from `document-model`. `address` is the verified signer address and `key` is the signer's `did:key`. Both are optional. An empty subject reads as anonymous. ### `SearchFilter` Narrows which documents a query returns. ```typescript type SearchFilter = { type?: string; parentId?: string; ids?: string[]; slugs?: string[]; }; ``` ### `PagingOptions` Controls pagination for list methods. ```typescript type PagingOptions = { cursor: string; limit: number; }; ``` ### `PagedResults` Returned by all list methods. Includes a `next()` helper for fetching the next page. ```typescript type PagedResults = { results: T[]; options: PagingOptions; next?: () => Promise>; nextCursor?: string; }; ``` ### `PropagationMode` Controls how deletions handle child documents. ```typescript enum PropagationMode { None = "none", // Only delete the specified document Cascade = "cascade", // Also delete all child documents } ``` ### `NewDocumentOptions` Options for `newDocument()` and `createEmpty()`. ```typescript type NewDocumentOptions = { parentIdentifier?: string; // id or slug of parent document documentModelVersion?: number; // defaults to latest protocolVersions?: { [protocol: string]: number }; // merged over the selection signaturePolicy?: SignaturePolicy; // overrides the client's default initialState?: Partial; // passed to the model's createDocument name?: string; id?: string; // kept only under the legacy policy }; ``` ### `JobInfo` Tracks the status and result of a mutation job. ```typescript type JobInfo = { id: string; documentId: string; // empty string when the job is unknown status: JobStatus; createdAtUtcIso: string; completedAtUtcIso?: string; error?: ErrorInfo; errorHistory?: ErrorInfo[]; result?: any; deletedDocument?: boolean; // set once the job has deleted its document consistencyToken: ConsistencyToken; meta: JobMeta; job?: Job; // populated on failure for debugging }; ``` See [Job lifecycle](/academy/Reference/Reactor/WorkingWithTheReactor#job-lifecycle) for details on `JobStatus` values. --- ## Pagination best practices All list methods (`find`, `getOutgoingRelationships`, `getIncomingRelationships`, `getOperations`, `getDocumentModelModules`) accept `PagingOptions` and return `PagedResults`. Here are some guidelines for working with paginated results effectively. **Use `next()` for sequential iteration.** The `next()` helper on `PagedResults` handles cursor management for you: ```typescript let page = await reactorClient.find({ type: "powerhouse/todo-list" }); while (page) { for (const doc of page.results) { console.log(doc.header.id); } page = page.next ? await page.next() : undefined; } ``` **Set a reasonable `limit`.** The default page size varies by method. If you know you only need a few results, set a small limit to reduce response size: ```typescript const topFive = await reactorClient.getOutgoingRelationships( driveId, "child", undefined, { cursor: "0", limit: 5, }, ); ``` **Check `nextCursor` to know if more pages exist.** When `nextCursor` is `undefined`, you have reached the end: ```typescript const page = await reactorClient.getOperations(docId); if (page.nextCursor) { // There are more operations to fetch } ``` **Avoid fetching all pages in tight loops for large datasets.** If you are processing thousands of documents or operations, consider processing each page before fetching the next to keep memory usage predictable. --- ## Cancellation with AbortSignal Most `IReactorClient` methods accept an optional `AbortSignal` parameter. This lets you cancel in-flight requests — useful for cleaning up when a component unmounts, a user navigates away, or a timeout is reached. The two methods that do not take an `AbortSignal` are `getDocumentModelModule` and `subscribe`. **Cancel on component unmount (React):** ```typescript useEffect(() => { const controller = new AbortController(); reactorClient .find( { type: "powerhouse/todo-list" }, undefined, undefined, controller.signal, ) .then(setResults) .catch((err) => { if (!isAbortError(err)) throw err; // isAbortError is exported from @powerhousedao/reactor }); return () => controller.abort(); }, []); ``` **Cancel with a timeout:** ```typescript const result = await reactorClient.get( docId, undefined, AbortSignal.timeout(5000), ); ``` **Bound a write's stage waits.** A write resolves once its job is accepted, but awaiting `handle.readable` or `handle.document()` waits for `READ_READY`. Those waits are bounded by the signal the write was called with: ```typescript const controller = new AbortController(); // Set a 10-second deadline setTimeout(() => controller.abort(), 10_000); const handle = await reactorClient.write.execute( docId, "main", actions, controller.signal, ); const updated = await handle.document(); ``` When a request is aborted, the method throws an `AbortError`. Detect it with `isAbortError(err)` from `@powerhousedao/reactor` rather than matching `err.name`. The underlying reactor job may still complete; aborting only cancels the client-side wait, not the server-side processing. --- ## Read methods (`client.read`) Reads that accept a `consistencyToken` wait until the read models they read from hold every operation the token names. Pass the `token` from a write's [`WriteReceipt`](#writehandle) to read your own write on any instance serving the same store. The token is always the last parameter. ### `get` Retrieve a single document by id or slug. ```typescript get( identifier: string, view?: ViewFilter, signal?: AbortSignal, consistencyToken?: ConsistencyToken, ): Promise ``` **Parameters:** | Name | Type | Required | Description | | ------------------ | ------------------ | -------- | ----------------------------------- | | `identifier` | `string` | Yes | Document id or slug | | `view` | `ViewFilter` | No | Branch, scopes, or revision filter | | `signal` | `AbortSignal` | No | Cancel the request | | `consistencyToken` | `ConsistencyToken` | No | Wait until the read models hold it | `getMany(documentIds, view?, signal?, consistencyToken?)` reads several documents by id in one call. **Example:** ```typescript const doc = await reactorClient.get("my-todo-list"); const atRevision = await reactorClient.get("my-todo-list", { revision: 5 }); ``` --- ### `resolveIdOrSlug` Resolve an id or slug to the canonical document id. Resolution runs against the `main` branch. Throws if the identifier cannot be resolved or is ambiguous. ```typescript resolveIdOrSlug( identifier: string, view?: ViewFilter, signal?: AbortSignal, consistencyToken?: ConsistencyToken, ): Promise ``` **Parameters:** | Name | Type | Required | Description | | ------------------ | ------------------ | -------- | ---------------------------------- | | `identifier` | `string` | Yes | Document id or slug | | `view` | `ViewFilter` | No | Branch/scopes filter | | `signal` | `AbortSignal` | No | Cancel the request | | `consistencyToken` | `ConsistencyToken` | No | Wait until the read models hold it | --- ### `getOutgoingRelationships` List documents that the given source has outgoing relationships to, filtered by relationship type. For example, pass `"child"` to list a parent's children. ```typescript getOutgoingRelationships( sourceIdentifier: string, relationshipType: string, view?: ViewFilter, paging?: PagingOptions, signal?: AbortSignal, consistencyToken?: ConsistencyToken, ): Promise> ``` **Parameters:** | Name | Type | Required | Description | | ------------------ | --------------- | -------- | ----------------------------------------------- | | `sourceIdentifier` | `string` | Yes | Source document id or slug | | `relationshipType` | `string` | Yes | Relationship type to filter by (e.g. `"child"`) | | `view` | `ViewFilter` | No | Branch/scopes filter | | `paging` | `PagingOptions` | No | Pagination cursor and limit | | `signal` | `AbortSignal` | No | Cancel the request | | `consistencyToken` | `ConsistencyToken` | No | Wait until the read models hold it | --- ### `getIncomingRelationships` List documents that have incoming relationships pointing at the given target, filtered by relationship type. For example, pass `"child"` to list a document's parents. ```typescript getIncomingRelationships( targetIdentifier: string, relationshipType: string, view?: ViewFilter, paging?: PagingOptions, signal?: AbortSignal, consistencyToken?: ConsistencyToken, ): Promise> ``` **Parameters:** | Name | Type | Required | Description | | ------------------ | --------------- | -------- | ----------------------------------------------- | | `targetIdentifier` | `string` | Yes | Target document id or slug | | `relationshipType` | `string` | Yes | Relationship type to filter by (e.g. `"child"`) | | `view` | `ViewFilter` | No | Branch/scopes filter | | `paging` | `PagingOptions` | No | Pagination cursor and limit | | `signal` | `AbortSignal` | No | Cancel the request | | `consistencyToken` | `ConsistencyToken` | No | Wait until the read models hold it | --- ### `find` Search for documents matching criteria. ```typescript find( search: SearchFilter, view?: ViewFilter, paging?: PagingOptions, signal?: AbortSignal, consistencyToken?: ConsistencyToken, ): Promise> ``` **Parameters:** | Name | Type | Required | Description | | -------- | --------------- | -------- | --------------------------------------- | | `search` | `SearchFilter` | Yes | Filter by type, parentId, ids, or slugs | | `view` | `ViewFilter` | No | Branch/scopes filter | | `paging` | `PagingOptions` | No | Pagination cursor and limit | | `signal` | `AbortSignal` | No | Cancel the request | | `consistencyToken` | `ConsistencyToken` | No | Wait until the read models hold it | **Example:** ```typescript const todoLists = await reactorClient.find({ type: "powerhouse/todo-list", parentId: driveId, }); for (const doc of todoLists.results) { console.log(doc.header.id, doc.header.name); } ``` --- ### `getOperations` Retrieve the operation history of a document. ```typescript getOperations( documentIdentifier: string, view?: ViewFilter, filter?: OperationFilter, paging?: PagingOptions, signal?: AbortSignal, ): Promise> ``` **Parameters:** | Name | Type | Required | Description | | -------------------- | ----------------- | -------- | ----------------------------------------------- | | `documentIdentifier` | `string` | Yes | Document id or slug | | `view` | `ViewFilter` | No | Branch/scopes filter | | `filter` | `OperationFilter` | No | Filter by action types, timestamps, or revision | | `paging` | `PagingOptions` | No | Pagination cursor and limit | | `signal` | `AbortSignal` | No | Cancel the request | **`OperationFilter`:** ```typescript interface OperationFilter { actionTypes?: string[]; // e.g. ["ADD_TODO_ITEM"] timestampFrom?: string; // ISO string timestampTo?: string; // ISO string sinceRevision?: number; // operations with index >= this value } ``` `getOperations` merges operations across all of the document's scopes and sorts them by operation index, paging through them with a composite cursor that tracks each scope's position. Paging defaults to `{ cursor: "0", limit: 100 }` — the default cursor is `"0"`, not `""`. --- ### `getDocumentModelModules` List registered document model modules. ```typescript getDocumentModelModules( namespace?: string, paging?: PagingOptions, signal?: AbortSignal, ): Promise> ``` **Parameters:** | Name | Type | Required | Description | | ----------- | --------------- | -------- | -------------------------------------------------- | | `namespace` | `string` | No | Filter by namespace (e.g. `"powerhouse"`, `"sky"`) | | `paging` | `PagingOptions` | No | Pagination cursor and limit | | `signal` | `AbortSignal` | No | Cancel the request | --- ### `getDocumentModelModule` Get a specific document model module by document type. ```typescript getDocumentModelModule( documentType: string, ): Promise> ``` **Parameters:** | Name | Type | Required | Description | | -------------- | -------- | -------- | ----------------------------- | | `documentType` | `string` | Yes | e.g. `"powerhouse/todo-list"` | --- ### `newDocument` Head a new, uncreated document of a type with the protocol versions this client selects for it and its parent, and the client's create signature policy. It is the one place a client chooses a new document's versions; pass the result to `client.write.create` or `client.write.drives.addFile`. It builds the header and enqueues nothing, so it is on the read half: `client.read.newDocument(...)`, or `client.newDocument(...)`. ```typescript newDocument( documentType: string, options?: NewDocumentOptions, signal?: AbortSignal, ): Promise ``` --- ### `evaluateActions` On `client.write`: it is a write-side read that decides through the same model that admits a write. Ask what the reactor **would** decide about a set of candidate operations, without submitting any of them. Use it to disable a control rather than offer an action that fails on submit. ```typescript evaluateActions( documentIdentifier: string, branch: string, candidates: ActionCandidate[], subject?: AuthSubject, signal?: AbortSignal, ): Promise ``` **Parameters:** | Name | Type | Required | Description | | -------------------- | ------------------- | -------- | ------------------------------------------------------ | | `documentIdentifier` | `string` | Yes | Document id or slug the candidates target | | `branch` | `string` | Yes | Branch to evaluate against | | `candidates` | `ActionCandidate[]` | Yes | Operations to predict a verdict for | | `subject` | `AuthSubject` | No | Who to decide for; defaults to the client's own signer | | `signal` | `AbortSignal` | No | Cancel the request | **`ActionCandidate` and `ActionEvaluations`:** ```typescript interface ActionCandidate { scope: string; // the scope the operation would execute in type: string; // the action type, e.g. "SUBMIT_EXPENSE" input?: unknown; // the action input, when a grant depends on it } interface ActionEvaluations { evaluations: Evaluation[]; // one per candidate, in the order given allAllowed: boolean; anyAllowed: boolean; allDenied: boolean; anyDenied: boolean; } type Evaluation = | { decision: "allow" } | { decision: "deny"; reason: string }; ``` A denial's `reason` is the same string the reactor would record on the refused operation, so a control can explain itself rather than going grey with no reason. Over an empty candidate list every aggregate is `false`: nothing asked about is nothing allowed *and* nothing denied. ```typescript const answer = await client.write.evaluateActions(documentId, "main", [ { scope: "global", type: "SUBMIT_EXPENSE", input: { amountCents: 900 } }, { scope: "global", type: "APPROVE_EXPENSE", input: { id: "e1" } }, ]); submitButton.disabled = answer.evaluations[0].decision === "deny"; ``` **WARNING:** `evaluateActions` decides through the same model that admits a real write, but it reads no future. A policy change landing between the answer and the submit changes the verdict, so **the submit path stays the only authority** — keep handling the refusal when you write. Treat an allow as "worth offering", never as "will succeed". Three further contract details are worth knowing: - **Supply the input when a grant depends on it.** With `authConditions` enabled, a conditional grant reads `action.input`, so a candidate standing for a half-filled form predicts the verdict a half-filled form earns — not the verdict the completed form will get. - **Document-scope candidates are judged by the document they name.** `DELETE_DOCUMENT` and `UPGRADE_DOCUMENT` name their target in `input.documentId`, the relationship actions in `input.sourceId`, and the policy consulted is that document's rather than the one passed as `documentIdentifier`. `CREATE_DOCUMENT` is exempt, matching the executor: it runs before its document exists, so it always predicts allow. - **The verdict is evaluated at the stream heads.** That is correct for a candidate about to be submitted. A backdated submission is out of contract, because the reactor decides that one by position against the policy as it stood there. **Requires the `authEnforcement` feature flag.** Without it the reactor holds no decision model and there is nothing to answer from, so the call rejects with `AuthEnforcementDisabledError`. Treat that as "cannot know" and leave your controls as they were — reading it as a denial would disable every control on a deployment that never enabled enforcement. Detect it by name, which is all that survives the SharedWorker RPC boundary: ```typescript if (AuthEnforcementDisabledError.isError(error)) { // no verdict available here; render as if unasked } ``` In React, prefer the [`useCanExecute`](/academy/Reference/EditorsUI/ReactHooks) hook, which wraps this call and reports the flags-off case as `status: "unsupported"`. Over GraphQL the same question is the `evaluateActions` query on the reactor subgraph, which decides for the authenticated caller and answers the flags-off case with an `AUTH_EVALUATION_UNSUPPORTED` error code. --- ## Write methods (`client.write`) The write half is `IReactorWriteClient`. Every write resolves once its job is accepted (validated, signed and enqueued) and returns a [`WriteHandle`](#writehandle). Nothing waits for the commit unless you await one of the handle's stages. ```typescript const handle = await client.write.execute(docId, "main", actions); const { token } = await handle.written; // operations are in the log const doc = await client.read.get(docId, undefined, undefined, token); const same = await handle.document(); // readable, then the gated read-back ``` ### `WriteHandle` ```typescript interface WriteHandle { readonly jobId: string; readonly documentId: string; // for a create, the id the new document carries readonly branch: string; readonly written: Promise; // job reached WRITE_READY readonly readable: Promise; // job reached READ_READY document( view?: ViewFilter, signal?: AbortSignal, ): Promise; } type WriteReceipt = { job: JobInfo; token: ConsistencyToken; // pass to a read to bound it documentId: string; branch: string; }; ``` `written` and `readable` reject with `JobFailedError` (exported from `@powerhousedao/reactor`, with the failed `JobInfo` on `job`) only when the job itself failed. A reducer error is not a job failure: the operation is stored with its error, the job reaches `READ_READY`, and `receipt.job.result` says which actions erred. `document()` awaits `readable`, then reads the target with the receipt's token, gated as the writer or as `view.subject` when given. Stage waits are bounded by the signal the write was called with. --- ### `create` Create a document from a full `PHDocument` object. Its header's signature policy, id and protocol versions are taken as given; a header from a model's `utils.createDocument()` is created at base-reducer 2. Build the header with [`newDocument`](#newdocument). With a parent, the handle's stages also wait for the relationship job on the parent. ```typescript create( document: PHDocument, parentIdentifier?: string, signal?: AbortSignal, ): Promise ``` **Parameters:** | Name | Type | Required | Description | | ------------------ | ------------- | -------- | -------------------------------------------------------- | | `document` | `PHDocument` | Yes | Document with optional id, slug, type, and initial state | | `parentIdentifier` | `string` | No | Id or slug of parent document | | `signal` | `AbortSignal` | No | Cancel the request | --- ### `createEmpty` Create the document [`newDocument`](#newdocument) builds. ```typescript createEmpty( documentModelType: string, options?: NewDocumentOptions, signal?: AbortSignal, ): Promise ``` **Parameters:** | Name | Type | Required | Description | | ------------------- | ----------------------- | -------- | -------------------------------------- | | `documentModelType` | `string` | Yes | e.g. `"powerhouse/todo-list"` | | `options` | `NewDocumentOptions` | No | Parent identifier, model version and the rest of `newDocument`'s options | | `signal` | `AbortSignal` | No | Cancel the request | --- ### Creating a document in a drive To create a document inside a drive, use `client.write.drives.addFile`. It submits the document's CREATE_DOCUMENT, UPGRADE_DOCUMENT, and ADD_RELATIONSHIP batch, waits for it to complete, then submits the drive's ADD_FILE. See [Drives](#drives) for the full signature. --- ### `execute` Sign and submit actions on one document. ```typescript execute( documentIdentifier: string, branch: string, actions: Action[], signal?: AbortSignal, ): Promise ``` **Parameters:** | Name | Type | Required | Description | | -------------------- | ------------- | -------- | ------------------------------------------ | | `documentIdentifier` | `string` | Yes | Document id or slug | | `branch` | `string` | Yes | Branch to apply actions to (e.g. `"main"`) | | `actions` | `Action[]` | Yes | List of actions to apply | | `signal` | `AbortSignal` | No | Cancel the request | On a version 3 document the actions run in canonical order, not array order: by timestamp, then by action id. Action creators give ids that sort in creation order within one process, so actions created in sequence keep that order. An action built with another id, such as `crypto.randomUUID()`, can run before an action listed ahead of it in the same millisecond. A submitted `UNDO` or `REDO` on base-reducer 3 is refused with `SubmittedUndoRefusedError`; use [`undo`](#undo) and [`redo`](#redo). **Example:** ```typescript const handle = await client.write.execute(docId, "main", [ actions.addTodoItem({ text: "Buy groceries" }), actions.addTodoItem({ text: "Walk the dog" }), ]); const updated = await handle.document(); ``` The write half has no `executeAsync`: `execute` already returns as soon as the job is accepted. Use `handle.jobId` with `waitForJob()` or `getJobStatus()` when you need the raw `JobInfo`. --- ### `rename` Rename a document. ```typescript rename( documentIdentifier: string, name: string, branch?: string, signal?: AbortSignal, ): Promise ``` **Parameters:** | Name | Type | Required | Description | | -------------------- | ------------- | -------- | -------------------- | | `documentIdentifier` | `string` | Yes | Document id or slug | | `name` | `string` | Yes | New name | | `branch` | `string` | No | Defaults to `"main"` | | `signal` | `AbortSignal` | No | Cancel the request | --- ### `setPreferredEditor` Update the preferred editor recorded in the document header meta. Pass `null` to clear it. ```typescript setPreferredEditor( documentIdentifier: string, preferredEditor: string | null, branch?: string, signal?: AbortSignal, ): Promise ``` **Parameters:** | Name | Type | Required | Description | | -------------------- | ---------------- | -------- | --------------------------------- | | `documentIdentifier` | `string` | Yes | Document id or slug | | `preferredEditor` | `string \| null` | Yes | Editor id, or `null` to clear it | | `branch` | `string` | No | Defaults to `"main"` | | `signal` | `AbortSignal` | No | Cancel the request | --- ### `undo` Submit the undo of a scope's last applied operation. ```typescript undo( documentIdentifier: string, scope?: string, branch?: string, signal?: AbortSignal, ): Promise ``` **Parameters:** | Name | Type | Required | Description | | -------------------- | ------------- | -------- | ---------------------- | | `documentIdentifier` | `string` | Yes | Document id or slug | | `scope` | `string` | No | Defaults to `"global"` | | `branch` | `string` | No | Defaults to `"main"` | | `signal` | `AbortSignal` | No | Cancel the request | On a base-reducer 3 document the client resolves the target with [`undoRedoTargets`](#undoredotargets) and submits a `RETRACT` of it signed by its own signer. With nothing to undo it throws `NoUndoRedoTargetError` before submitting. The `RETRACT` names the row the call saw, so an edit landing in between is not undone. A target retracted or gone by then fails the job with `RetractionRefusedError`, and nothing is resolved again: the handle's stages reject with `JobFailedError`, whose `job.error.name` is `RetractionRefusedError`. On base-reducer 1 and 2 it submits `UNDO`. Calls on one document, scope and branch run one at a time: each resolves once its write is readable, so the next resolves against it. `handle.document()` is the updated document. See [Undo and redo](/academy/Reference/Reactor/UndoRedo). --- ### `redo` Submit the redo of a scope's last undo. Same parameters as [`undo`](#undo). ```typescript redo( documentIdentifier: string, scope?: string, branch?: string, signal?: AbortSignal, ): Promise ``` On base-reducer 3 the client resolves the last live retraction with [`undoRedoTargets`](#undoredotargets) and submits a `RETRACT` of it signed by its own signer, under `undo`'s race rule. Content applied after the undo leaves nothing to redo. On base-reducer 1 and 2 it submits `REDO`. --- ### `undoRedoTargets` The rows a base-reducer 3 `undo` and `redo` of a scope would retract now, resolved by the reactor through the read gate. A scope the subject may not read has none, nor does the `auth` or `document` scope; below base-reducer 3 there are none. It reads the operation log, so it is on the write half, as `evaluateActions` is. ```typescript undoRedoTargets( documentIdentifier: string, scope?: string, // defaults to "global" view?: ViewFilter, // branch and subject signal?: AbortSignal, ): Promise type UndoRedoTargets = { undoTarget?: string; redoTarget?: string }; ``` --- ### `undoRedoAvailability` Report whether `undo` and `redo` would find a target in a scope now. Same parameters as [`undo`](#undo); a write-side read like [`undoRedoTargets`](#undoredotargets). ```typescript undoRedoAvailability( documentIdentifier: string, scope?: string, branch?: string, signal?: AbortSignal, ): Promise type UndoRedoAvailability = { canUndo: boolean; canRedo: boolean }; ``` Exact on base-reducer 3. On base-reducer 1 and 2, `canUndo` is whether the scope has any operation and `canRedo` is always `false`. A scope the caller may not read reports neither. --- ### `addRelationship` Add a typed relationship from a source document to a target document. To add a child, pass `"child"` as the `relationshipType`. The handle's document is the source. ```typescript addRelationship( sourceIdentifier: string, targetIdentifier: string, relationshipType: string, metadata?: Record, branch?: string, signal?: AbortSignal, ): Promise ``` **Parameters:** | Name | Type | Required | Description | | ------------------ | ------------------------- | -------- | ---------------------------------- | | `sourceIdentifier` | `string` | Yes | Source document id or slug | | `targetIdentifier` | `string` | Yes | Target document id or slug | | `relationshipType` | `string` | Yes | Relationship type (e.g. `"child"`) | | `metadata` | `Record` | No | Metadata stored on the edge | | `branch` | `string` | No | Defaults to `"main"` | | `signal` | `AbortSignal` | No | Cancel the request | --- ### `updateRelationship` Replace the metadata on an existing edge. Pass `null` to clear it. Throws `RelationshipNotFoundError` before submitting when the edge does not exist. The handle's document is the source. ```typescript updateRelationship( sourceIdentifier: string, targetIdentifier: string, relationshipType: string, metadata: Record | null, branch?: string, signal?: AbortSignal, ): Promise ``` --- ### `removeRelationship` Remove a typed relationship from a source document to a target document. The handle's document is the source. ```typescript removeRelationship( sourceIdentifier: string, targetIdentifier: string, relationshipType: string, branch?: string, signal?: AbortSignal, ): Promise ``` **Parameters:** | Name | Type | Required | Description | | ------------------ | ------------- | -------- | ---------------------------------- | | `sourceIdentifier` | `string` | Yes | Source document id or slug | | `targetIdentifier` | `string` | Yes | Target document id or slug | | `relationshipType` | `string` | Yes | Relationship type (e.g. `"child"`) | | `branch` | `string` | No | Defaults to `"main"` | | `signal` | `AbortSignal` | No | Cancel the request | --- ### `moveRelationship` Move a typed relationship from one source document to another, keeping the same target. A move is a remove followed by an add. The remove has reached `READ_READY` when the call resolves; the add has only been accepted. ```typescript moveRelationship( sourceParentIdentifier: string, targetParentIdentifier: string, targetIdentifier: string, relationshipType: string, branch?: string, signal?: AbortSignal, ): Promise<{ removed: WriteHandle; added: WriteHandle }> ``` **Parameters:** | Name | Type | Required | Description | | ------------------------ | ------------- | -------- | ---------------------------------- | | `sourceParentIdentifier` | `string` | Yes | Current source document id or slug | | `targetParentIdentifier` | `string` | Yes | New source document id or slug | | `targetIdentifier` | `string` | Yes | The target document id or slug | | `relationshipType` | `string` | Yes | Relationship type (e.g. `"child"`) | | `branch` | `string` | No | Defaults to `"main"` | | `signal` | `AbortSignal` | No | Cancel the request | `removed.document()` reads the old parent; `added.document()` reads the new one. --- ### `deleteDocument` Delete a single document. The delete, its incoming relationship removals and, with `Cascade`, every orphaned descendant go in one batch. The handle's stages wait for the whole batch. ```typescript deleteDocument( identifier: string, propagate?: PropagationMode, signal?: AbortSignal, ): Promise ``` **Parameters:** | Name | Type | Required | Description | | ------------ | ----------------- | -------- | --------------------------------- | | `identifier` | `string` | Yes | Document id or slug | | `propagate` | `PropagationMode` | No | `Cascade` to also delete children | | `signal` | `AbortSignal` | No | Cancel the request | --- ### `deleteDocuments` Bulk delete multiple documents. Returns one handle per identifier. ```typescript deleteDocuments( identifiers: string[], propagate?: PropagationMode, signal?: AbortSignal, ): Promise ``` --- ### `executeBatch` Sign and submit multiple mutation jobs in dependency order. The call resolves once every job is accepted. Each batch stage waits for every job, then rejects with a `BatchJobFailedError` naming the first failed job's plan key (`key`), carrying every job's final `JobInfo` (`jobs`) and the failed job's own error name (`cause.name`). The batch is not atomic: other jobs, including ones that depend on the failed job, may still have committed. See [Error Handling](/academy/Reference/Reactor/ErrorHandling). ```typescript executeBatch( request: BatchExecutionRequest, signal?: AbortSignal, ): Promise ``` Each job carries a `key` (used to express dependencies) and a `dependsOn` list of other job keys. ```typescript type ExecutionJobPlan = { key: string; documentId: string; scope: string; branch: string; actions: Action[]; dependsOn: string[]; }; type BatchExecutionRequest = { jobs: ExecutionJobPlan[]; }; interface BatchWriteHandle { readonly jobs: Record; // the accepted jobs readonly handles: Record; readonly written: Promise>; readonly readable: Promise>; } ``` Every record is keyed by each plan's `key`. --- ### `loadBatch` Submit multiple batches of pre-existing operations across documents, with dependency ordering, and wait for all jobs to complete. Use this to load operations that already exist (for example from sync), as opposed to `executeBatch`, which signs and applies new actions. Unlike the other writes, it keeps its job-map result. ```typescript loadBatch( request: BatchLoadRequest, signal?: AbortSignal, ): Promise ``` Each job carries `operations` instead of actions. `dependsOn` lists intra-batch plan keys; `externalDeps` lists already-resolved job ids from prior batches. ```typescript type LoadJobPlan = { key: string; documentId: string; scope: string; branch: string; operations: Operation[]; dependsOn: string[]; externalDeps: string[]; }; type BatchLoadRequest = { jobs: LoadJobPlan[]; }; type BatchLoadResult = { jobs: Record; }; ``` The result's `jobs` record is keyed by each plan's `key`. If a job fails, `loadBatch` throws with that job's error message. --- ## Drives Drive-aware operations orchestrate the multi-action, multi-document work needed to keep a drive's `state.global.nodes` array consistent with the relationship index and the underlying documents. Reads live on `client.read.drives` (`IDriveReadClient`); writes live on `client.write.drives` (`IDriveWriteClient`). Use the flat methods (`read.get`, `write.execute`, `read.find`) for everything that is not drive-aware. Every method takes a trailing optional `signal?: AbortSignal`. Each write resolves once its last step is accepted. A step a later step depends on (a file's document before the drive names it, a document rename before the node rename) has completed by then; if it failed, the call throws the job's own error, by name. ### `write.drives.addFile` Add a document to a drive. The document's CREATE_DOCUMENT, UPGRADE_DOCUMENT, and ADD_RELATIONSHIP batch completes first, then the drive's ADD_FILE is submitted. This is the way to create a document in a drive. The handle's document is the new file; its stages span both batches. ```typescript addFile( driveIdentifier: string, document: PHDocument, parentFolder?: string, signal?: AbortSignal, ): Promise ``` **Parameters:** | Name | Type | Required | Description | | ----------------- | ------------- | -------- | -------------------------- | | `driveIdentifier` | `string` | Yes | Drive document id or slug | | `document` | `PHDocument` | Yes | The document to create | | `parentFolder` | `string` | No | Folder id within the drive | | `signal` | `AbortSignal` | No | Cancel the request | ### `write.drives.create` Create a new drive document. The handle's document is the drive. ```typescript create( input: DriveInput, signal?: AbortSignal, ): Promise ``` ### `write.drives.addFolder` Add a folder node to a drive. `nodeId` on the returned handle is the new folder's id. ```typescript addFolder( driveIdentifier: string, name: string, parentFolder?: string, signal?: AbortSignal, ): Promise // WriteHandle & { nodeId: string } ``` ### `write.drives.removeNode` Remove a node from a drive. Folder nodes cascade: descendant file documents are deleted first (each completed), then the folder node entry. The handle is the last step; its document is the drive. ```typescript removeNode( driveIdentifier: string, nodeId: string, signal?: AbortSignal, ): Promise ``` ### `write.drives.renameNode` Rename a node. Renames the underlying document (completed and checked), then the drive's node entry. The handle's document is the drive. ```typescript renameNode( driveIdentifier: string, nodeId: string, name: string, signal?: AbortSignal, ): Promise ``` ### `write.drives.setPreferredEditorOnNode` Update the preferred editor recorded in the node's document header meta. Pass `null` to clear it. The handle's document is the node's document. ```typescript setPreferredEditorOnNode( nodeId: string, preferredEditor: string | null, signal?: AbortSignal, ): Promise ``` ### `write.drives.moveNode` Move a node to a different parent folder within the same drive. Pass `undefined` as `targetParentFolderId` to move the node to the drive root. The handle's document is the drive. ```typescript moveNode( driveIdentifier: string, srcNodeId: string, targetParentFolderId: string | undefined, signal?: AbortSignal, ): Promise ``` ### `write.drives.copyNode` Copy a node, and its subtree if it is a folder, within a drive. Each file is duplicated (completed), then the drive's nodes are copied. The handle's document is the drive. A copied file keeps its source's `protocolVersions` unless `options.protocolVersions` overrides them; a reactor-drive refuses the override. See [Undo and redo](/academy/Reference/Reactor/UndoRedo) for copying a document to base-reducer 3. ```typescript copyNode( driveIdentifier: string, srcNodeId: string, targetParentFolderId: string | undefined, signal?: AbortSignal, options?: { protocolVersions?: ProtocolVersions }, ): Promise ``` ### `read.drives.getNode` Return a single node from the drive's `state.global.nodes` array. ```typescript getNode( driveIdentifier: string, nodeId: string, signal?: AbortSignal, ): Promise ``` ### `read.drives.listNodes` Return nodes in the drive, optionally filtered by parent folder. Returns a paged result so you can stream through drives with large node counts. ```typescript listNodes( driveIdentifier: string, parentFolder?: string | null, paging?: PagingOptions, signal?: AbortSignal, ): Promise> ``` The `parentFolder` argument controls scope: | Value | Result | | ----------- | -------------------------------------------- | | `undefined` | Every node in the drive | | `null` | Only root-level nodes | | a folder id | Only the direct children of that folder | --- ## Subscriptions ### `subscribe` Subscribe to document change events matching a filter. ```typescript subscribe( search: SearchFilter, callback: (event: DocumentChangeEvent) => void, view?: ViewFilter, ): () => void ``` **Returns** an unsubscribe function. **`DocumentChangeEvent`:** ```typescript type DocumentChangeEvent = { type: DocumentChangeType; documents: PHDocument[]; context?: { parentId?: string; childId?: string; }; }; ``` **`DocumentChangeType`** values: | Value | Description | | -------------- | -------------------------- | | `Created` | A new document was created | | `Deleted` | A document was deleted | | `Updated` | A document's state changed | | `ChildAdded` | A child relationship was added | | `ChildRemoved` | A child relationship was removed | `subscribe` emits the five values above. `ParentAdded` and `ParentRemoved` exist in the `DocumentChangeType` enum but are not currently emitted by `subscribe`. Which fields populate depends on the change type: - `Created` and `Updated` carry the changed documents in `documents`. - `Deleted` carries `documents: []` and the document id in `context.childId`. - `ChildAdded` and `ChildRemoved` carry `documents: []` and the ids in `context.parentId` and `context.childId`. **Example:** ```typescript // Watch for all todo-list changes const unsubscribe = reactorClient.subscribe( { type: "powerhouse/todo-list" }, (event) => { if (event.type === DocumentChangeType.Updated) { console.log( "Updated:", event.documents.map((d) => d.header.id), ); } }, ); // Later, stop listening unsubscribe(); ``` --- ## Job tracking ### `getJobStatus` Check the current status of a job. ```typescript getJobStatus( jobId: string, signal?: AbortSignal, ): Promise ``` --- ### `waitForJob` Wait for a job to reach a terminal status (`READ_READY` or `FAILED`). ```typescript waitForJob( jobId: string | JobInfo, signal?: AbortSignal, ): Promise ``` **Example:** ```typescript const handle = await reactorClient.write.execute(docId, "main", actions); const completed = await reactorClient.read.waitForJob(handle.jobId); console.log(completed.status); // "READ_READY" or "FAILED" ``` --- ## Advanced reactor usage > Source: https://academy.vetra.io/academy/Reference/Reactor/AdvancedReactorUsage This page covers the low-level `IReactor` interface and the internal components you can access through `ReactorModule`. Most developers should use `IReactorClient` (see [IReactorClient API Reference](/academy/Reference/Reactor/ReactorClient)) — the information here is for advanced scenarios such as: - Building custom tooling or infrastructure on top of the reactor - Working with consistency tokens for read-after-write guarantees - Subscribing directly to internal event bus events - Constructing a reactor with custom storage or executor configurations - Writing integration tests that need access to internals ## IReactor vs IReactorClient `IReactorClient` is a high-level wrapper around `IReactor`. The table below summarizes the key differences: | Aspect | `IReactor` | `IReactorClient` | | ----------------------- | ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | | **Write return values** | Returns `JobInfo` immediately (fire-and-forget) | `client.write` returns a `WriteHandle` once the job is accepted; await `written`, `readable`, or `document()` | | **Signing** | `create`, `deleteDocument`, `addRelationship` and `removeRelationship` take an optional `signer`; `execute` and `load` take none — identity rides on each action's `context.signer` | Client manages signing internally | | **Document lookup** | Separate `get()`, `getBySlug()`, `getByIdOrSlug()` methods | Single `get(identifier)` that accepts either | | **Children/parents** | Returns `string[]` (document IDs only) | Returns `PagedResults` (full documents) | | **Convenience methods** | Basic CRUD | Plus: `createEmpty()`, `rename()`, `moveRelationship()`, `deleteDocuments()`, and `client.write.drives.addFile()` | | **Subscriptions** | Not available (use the event bus directly) | `subscribe(search, callback, view?)` for real-time document changes | | **Consistency tokens** | Explicit — pass tokens to reads after writes | Each `WriteReceipt` carries a `token`; `client.read` methods take it as their last parameter, and `handle.document()` uses it for you | **When to use `IReactor` directly:** - You are building infrastructure that manages its own signing - You need the raw `JobInfo` from every write and drive the job lifecycle yourself ## Building a reactor with ReactorBuilder `ReactorBuilder` uses a fluent API to construct and wire all internal components. ```typescript const reactor = await new ReactorBuilder() .withDocumentModelSources([todoListModule, invoiceModule]) .withLogger(new ConsoleLogger()) .withExecutorConfig({ maxConcurrency: 4, jobTimeoutMs: 30_000 }) .withWriteCacheConfig({ maxDocuments: 100, ringBufferSize: 10 }) .withMigrationStrategy("auto") .withChannelScheme(ChannelScheme.CONNECT) .build(); ``` `build()` returns an `IReactor`. If you need access to internal components, use `buildModule()` instead — it returns an `InProcessReactorModule` containing the reactor plus all its internals (see [ReactorModule](#reactormodule)). ### Builder methods | Method | Description | | --------------------------------- | ------------------------------------------------------------------------ | | `withDocumentModelSources(sources)` | Register document-model sources: live modules, importable `{ filePath }` files, or importable `{ packageName }` packages | | `withUpgradeManifests(manifests)` | Register [upgrade manifests](/academy/Reference/Reactor/DocumentModelRegistry) for document model versioning | | `withLogger(logger)` | Set the logger (defaults to `ConsoleLogger`) | | `withExecutorConfig(config)` | Configure `maxConcurrency`, `jobTimeoutMs`, `signatureVerification` (see [Signing](/academy/Build/BuildingUserExperiences/Authorization/Signing#signature-verification)), and the reactor `featureFlags` (see [Authorization](/academy/Reference/Reactor/Authorization) and [Undo and redo](/academy/Reference/Reactor/UndoRedo)) | | `withWriteCacheConfig(config)` | Configure `maxDocuments` and `ringBufferSize` for the write cache | | `withMigrationStrategy(strategy)` | Set to `"auto"` to run database migrations on build | | `withKysely(kysely)` | Provide a custom Kysely database instance (defaults to in-memory PGlite) | | `withQueue(queue)` | Provide a custom job queue (defaults to `InMemoryQueue`) | | `withEventBus(eventBus)` | Provide a custom event bus | | `withExecutor(executor)` | Provide a custom job executor manager | | `withReadModel(readModel)` | Register an additional read model | | `withReadModelFactory(factory)` | Register a factory that builds a pre-ready read model after the operation index, write cache, and processor-manager tracker exist | | `withReadModelCoordinator(coordinator)` | Provide a custom read model coordinator | | `withSync(syncBuilder)` | Enable [synchronization with remote reactors](/academy/Reference/Reactor/Synchronization) | | `withChannelScheme(scheme)` | Set the sync channel scheme | | `withFeatures(features)` | Set feature flags | | `withJwtHandler(handler)` | Set a JWT handler for authentication | | `withDocumentModelLoader(loader)` | Set a lazy document-model loader: `load(documentType)` returns a `DocumentModelSource`; importable sources are broadcast to executor workers, live modules stay host-only | | `withDriveContainerTypes(types)` | Set the document types treated as drive containers | | `withInstrumentedPool(instrumentation)` | Register an externally-built `pg.Pool` so its metrics surface through `pools` | | `withShutdownHook(hook)` | Register an async cleanup hook to run during graceful shutdown | | `withSignalHandlers()` | Register OS signal handlers for graceful shutdown | | `withTrustPolicy(policy, workerSpec?)` | Decide which keys may sign as which users (see [Trust policy](/academy/Build/BuildingUserExperiences/Authorization/Signing#trust-policy)) | | `withWorkerPool(options)` | Run jobs in N worker threads instead of in-process — calling it enables the pool; `{ numWorkers, db }` or `{ numWorkers, factory }` (see [Storage and scaling](/academy/Reference/Reactor/StorageAndScaling)) | | `withProjectionShards(config)` | Run N sharded projection workers (see [Storage and scaling](/academy/Reference/Reactor/StorageAndScaling)) | | `withProjectionWorkerFactory(factory)` | Inject a custom projection worker factory | Workers open their own Postgres pools: the executor pool takes its connection info in `withWorkerPool({ db })`, and `withProjectionShards` takes its own `db` (falling back to the executor pool's when both are configured). See [Storage and scaling](/academy/Reference/Reactor/StorageAndScaling) for worker pools and projection shards, and [Synchronization and remote drives](/academy/Reference/Reactor/Synchronization) for sync. ### Built-in document models A reactor only knows the document types it was given. Nothing is registered by default. Two built-in modules ship with the platform. Without `driveDocumentModelModule` the reactor cannot create a drive. Without `documentModelDocumentModelModule` the reactor cannot store document-model definitions. ```typescript const reactor = await new ReactorBuilder() .withDocumentModelSources([ documentModelDocumentModelModule, // powerhouse/document-model driveDocumentModelModule, // powerhouse/document-drive todoListModule, ]) .build(); ``` ## IReactor API ### Reading documents ```typescript // By exact ID const doc = await reactor.get(docId); // By slug const doc = await reactor.getBySlug("my-document"); // By either ID or slug (throws if ambiguous) const doc = await reactor.getByIdOrSlug(identifier); // With consistency token for read-after-write const doc = await reactor.get(docId, undefined, token); // Outgoing and incoming relationships (returns string[] of IDs, not full documents) const childIds = await reactor.getOutgoingRelationships(parentId, "child"); const parentIds = await reactor.getIncomingRelationships(childId, "child"); // Search const results = await reactor.find({ type: "powerhouse/todo-list" }); // Operations: keyed by scope, each a PagedResults // (unlike the client's getOperations, which returns a flat PagedResults) const ops = await reactor.getOperations(docId); const globalOps = ops["global"].results; // Document model modules (the IReactor analogue of the client's // getDocumentModelModules) const models = await reactor.getDocumentModels(); // PagedResults ``` `getDocumentModels(namespace?, paging?, signal?)` returns `Promise>`. `getOperations` accepts optional `view`, `OperationFilter`, and paging, and returns `Promise>>` — index it by scope. `resolveUndoRedo(documentId, scope, branch?, signal?)` returns `{ undoTarget?, redoTarget? }`: the action ids a base-reducer 3 undo and redo on that scope would retract. The reactor keeps the targets per scope and advances them over the operations stored since the last call, so a call after an edit reads the new operations, not the whole history; a `RETRACT`, a rewind or a row a retraction names makes it derive the scope again. Like the other `IReactor` reads it is not read-gated. ### Writing documents All write methods return `JobInfo` immediately — they do not wait for the job to complete. ```typescript // Create a document const job = await reactor.create(document, signer); // Execute actions const job = await reactor.execute(docId, "main", actions); // Load pre-existing operations (e.g., from sync) const job = await reactor.load(docId, "main", operations); // Delete const job = await reactor.deleteDocument(docId, signer); // Manage relationships const job = await reactor.addRelationship(parentId, childId1, "child"); const job = await reactor.removeRelationship(parentId, childId1, "child"); ``` `create`, `deleteDocument`, `execute`, `load`, `executeBatch`, and `loadBatch` accept an optional trailing `meta?: Record` that is merged into the job's `JobMeta`. Use it for tracing or correlation. #### Building the document for `create` `create` takes a fully formed `PHDocument`. Build the document with the model's creator, which sets the header, type and initial state. The reactor turns the document into a `CREATE_DOCUMENT` and an `UPGRADE_DOCUMENT` action in one `document`-scope job. The built-in models export their creators directly. A codegen'd module exposes the same function as `utils.createDocument`. ```typescript const drive = driveCreateDocument(); const driveJob = await reactor.create(drive, signer); const modelDefinition = documentModelCreateDocument(); const modelJob = await reactor.create(modelDefinition, signer); const todoList = todoListModule.utils.createDocument(); const todoJob = await reactor.create(todoList, signer); ``` Each creator takes an optional initial state and falls back to the model's defaults. ### Batch operations `executeBatch` lets you submit multiple mutation jobs with dependency ordering. Jobs are executed in the order dictated by their `dependsOn` keys. What it guarantees is ordering, and only ordering. A batch is not atomic and not transactional: each job commits on its own, and there is no batch-level rollback, status, or consistency token. In particular, a `dependsOn` edge waits for the job before it to *finish*, not to *succeed*. A job that fails leaves the queue exactly as a successful one does, so its dependents still run — against whatever state the failure left behind. Jobs that committed stay committed. The returned value is a submission receipt rather than a result: every `JobInfo` comes back `PENDING`, with a placeholder consistency token. To find out what actually happened, poll `getJobStatus` for each job id, or subscribe to the job lifecycle events and correlate through `event.jobMeta.batchId` and `event.jobMeta.batchJobIds`. `ReactorJobFailedEvent` carries no `jobMeta`, and its `job` is optional (`job?: Job`), so guard it before reading `event.job.meta` — or read `JobInfo.meta` from `getJobStatus` instead. There is no idempotency key on a job plan, so re-submitting a batch after a partial failure re-applies the entries that already succeeded. Any compensation is the caller's to write. A job plan carries raw `Action` objects, so a batch that creates a document uses action creators rather than `reactor.create`. `@powerhousedao/reactor` exports `createDocumentAction`, `upgradeDocumentAction` and `deleteDocumentAction` for the `document`-scope lifecycle actions. A model's own actions come from its creators, here `addFile` from the drive model. The `model` field takes a document type string. Each built-in model exports its own as a constant rather than requiring the literal: `driveDocumentType` (`"powerhouse/document-drive"`) from `@powerhousedao/shared/document-drive`, and `documentModelDocumentType` (`"powerhouse/document-model"`) from `@powerhousedao/shared/document-model`. A codegen'd module exports its own the same way, as `DocumentType`. ```typescript createDocumentAction, upgradeDocumentAction, } from "@powerhousedao/reactor"; addFile, driveDocumentType, } from "@powerhousedao/shared/document-drive"; const driveId = generateId(); const fileId = generateId(); const result = await reactor.executeBatch({ jobs: [ { key: "create-drive", documentId: driveId, scope: "document", branch: "main", actions: [ createDocumentAction({ documentId: driveId, model: driveDocumentType, version: 0, }), upgradeDocumentAction({ documentId: driveId, model: driveDocumentType, fromVersion: 0, toVersion: 1, }), ], dependsOn: [], }, { key: "add-document", documentId: driveId, scope: "global", branch: "main", actions: [ addFile({ id: fileId, name: "Budget", documentType: "powerhouse/budget" }), ], dependsOn: ["create-drive"], // Waits for drive creation }, ], }); // result.jobs["create-drive"] and result.jobs["add-document"] are JobInfo objects ``` A job's `scope` must equal the scope of every action it carries. `executeBatch` checks the scopes on submission and throws `Job '' declares scope '' but action has scope ''`. `CREATE_DOCUMENT`, `UPGRADE_DOCUMENT` and `DELETE_DOCUMENT` are `document`-scope actions, so creating a document and writing its `global` state are two jobs, as above. `loadBatch` is the load-side counterpart. It submits batches of pre-existing operations (e.g. from sync) with the same dependency ordering. A `LoadJobPlan` uses `operations` instead of `actions`, and adds `externalDeps: string[]` — pre-resolved job UUIDs from prior batches that are appended to the queue hint without plan-key resolution. ```typescript const result = await reactor.loadBatch({ jobs: [ { key: "load-doc", documentId: docId, scope: "global", branch: "main", operations: incomingOperations, dependsOn: [], externalDeps: [], }, ], }); // result.jobs["load-doc"] is a JobInfo object ``` ### Job tracking and shutdown ```typescript // Check job status const info = await reactor.getJobStatus(jobId); // info.status is PENDING | RUNNING | WRITE_READY | READ_READY | FAILED // Graceful shutdown const shutdown = reactor.kill(); // shutdown.isShutdown is true immediately await shutdown.completed; // Resolves when all in-flight jobs finish ``` `kill()` returns a `ShutdownStatus` synchronously (it is not a Promise): `{ get isShutdown(): boolean; completed: Promise }`. `isShutdown` flips to `true` immediately; `await shutdown.completed` to block until in-flight jobs drain. Do not `await reactor.kill()`. ### Awaiting a job with `JobAwaiter` `IReactorClient` waits for jobs with a `JobAwaiter`. On the `IReactor` path you construct one yourself over the module's event bus instead of polling `getJobStatus`: ```typescript const { reactor, eventBus } = await new ReactorBuilder() .withDocumentModelSources([todoListModule]) .buildModule(); const awaiter = new JobAwaiter(eventBus, (jobId, signal) => reactor.getJobStatus(jobId, signal), ); const job = await reactor.create(document); const info = await awaiter.waitForJob(job.id); // READ_READY or FAILED, so check info.status awaiter.shutdown(); // rejects anything still pending ``` `waitForJob` resolves with the terminal `JobInfo`, for `FAILED` as well as `READ_READY`. It reads the current status first, so a job that finished before the call resolves immediately. Resolving means the job reached a terminal status. It does not mean every other `JOB_READ_READY` subscriber has run: subscribers run sequentially in registration order, and the awaiter registers its own subscriber when it is constructed. If your continuation depends on your own subscriber's work, resolve a promise from inside that subscriber and await that promise too. ## Consistency tokens Every write operation returns a `JobInfo` that includes a `ConsistencyToken`. This token captures the exact operation coordinates that the write produced: ```typescript type ConsistencyToken = { version: 1; createdAtUtcIso: string; coordinates: Array<{ documentId: string; scope: string; branch: string; operationIndex: number; }>; }; ``` Pass this token to subsequent reads to guarantee you see the effects of your write: ```typescript const job = await reactor.execute(docId, "main", actions); const token = job.consistencyToken; // This read is guaranteed to include the operations from the write above const doc = await reactor.get(docId, undefined, token); ``` Without a consistency token, reads may return stale data if the read models have not yet indexed the latest operations. On the client, `handle.document()` waits for `READ_READY` and reads with the receipt's token, and every `client.read` method that takes a `consistencyToken` accepts `receipt.token`. When using `IReactor` directly, you pass the job's token yourself. ## ReactorModule `ReactorBuilder.buildModule()` returns an `InProcessReactorModule` that exposes the full in-process dependency graph: ```typescript const module = await new ReactorBuilder() .withDocumentModelSources([todoListModule]) .buildModule(); const { reactor, eventBus, processorManager, operationStore } = module; ``` The base `ReactorModule` interface has only three fields (`documentModelRegistry`, `syncModule`, `eventBus`) — the subset a client resolves regardless of where the reactor graph lives. `buildModule()` returns the in-process extension `InProcessReactorModule`, which adds the components below. ### Available components `InProcessReactorModule`: | Component | Interface | Purpose | | ---------------------- | ----------------------------- | -------------------------------------------------------- | | `reactor` | `IReactor` | The reactor instance | | `documentModelRegistry`| `IDocumentModelRegistry` | Registered document model modules and version upgrades | | `eventBus` | `IEventBus` | Internal pub/sub for reactor events | | `queue` | `IQueue` | Job queue with per-document ordering | | `jobTracker` | `IJobTracker` | Tracks job lifecycle (PENDING through READ_READY/FAILED) | | `executorManager` | `IJobExecutorManager` | Manages job executor instances | | `operationStore` | `IOperationStore` | Append-only operation log | | `keyframeStore` | `IKeyframeStore` | Document state snapshots | | `writeCache` | `IWriteCache` | Write-path document cache | | `operationIndex` | `IOperationIndex` | Global ordinal assignment | | `documentView` | `IDocumentView` | Maintains document snapshots for reads | | `documentViewConsistencyTracker` | `IConsistencyTracker` | Tracks document-view indexing for read-after-write | | `documentIndexer` | `IDocumentIndexer` | Tracks document relationships (parent/child graph) | | `documentIndexerConsistencyTracker` | `IConsistencyTracker` | Tracks document-indexer indexing for read-after-write | | `readModelCoordinator` | `IReadModelCoordinator` | Dispatches operations to all read models | | `subscriptionManager` | `IReactorSubscriptionManager` | Manages document change subscriptions | | `processorManager` | `IProcessorManager` | Routes operations to user-defined [processors](/academy/Reference/Reactor/Processors) | | `processorManagerConsistencyTracker` | `IConsistencyTracker` | Tracks processor-manager catch-up | | `database` | `Kysely` | The underlying database connection | | `syncModule` | `InProcessSyncModule \| undefined` | Sync infrastructure (if configured) | | `pools` | `PoolInstrumentation[]` | Instrumented `pg.Pool` handles; empty for PGlite | ## Subscribing to the event bus The `IEventBus` lets you listen to internal reactor events. Subscribers are called sequentially in registration order. ```typescript ReactorEventTypes, SyncEventTypes, type JobReadReadyEvent, type SyncFailedEvent, } from "@powerhousedao/reactor"; // Listen for all completed jobs const unsubscribe = module.eventBus.subscribe( ReactorEventTypes.JOB_READ_READY, (type, event: JobReadReadyEvent) => { console.log("Job completed:", event.jobId); console.log("Operations:", event.operations.length); }, ); // Listen for sync failures module.eventBus.subscribe( SyncEventTypes.SYNC_FAILED, (type, event: SyncFailedEvent) => { console.error("Sync failed for job:", event.jobId, event.errors); }, ); ``` `subscribe(type: number, subscriber: (type: number, event: K) => void | Promise)` cannot infer the payload from the event id, so annotate the callback's `event` parameter (or pass `K` explicitly). Left unannotated, `event` is `unknown` and the handler does not compile. Each job lifecycle event has its own payload type, all exported from `@powerhousedao/reactor`: | Event | Payload type | Fields | | ---------------------------------- | ----------------------- | ---------------------------------------------------------- | | `JOB_PENDING` (10001) | `JobPendingEvent` | `jobId`, `jobMeta` | | `JOB_RUNNING` (10002) | `JobRunningEvent` | `jobId`, `jobMeta` | | `JOB_WRITE_READY` (10003) | `JobWriteReadyEvent` | `jobId`, `operations`, `jobMeta`, `collectionMemberships?` | | `JOB_READ_READY` (10004) | `JobReadReadyEvent` | `jobId`, `operations` | | `JOB_FAILED` (10005) | `ReactorJobFailedEvent` | `jobId`, `error: Error`, `job?` | Every payload carries `jobId`, so a subscriber can correlate back to the ids returned in `BatchExecutionResult.jobs`. Where `jobMeta` is present it carries `batchId` and `batchJobIds` plus any `meta` passed at submission; `JobReadReadyEvent` and `ReactorJobFailedEvent` do not carry it. Besides `ReactorEventTypes`, `SyncEventTypes`, and `QueueEventTypes`, the executor managers emit `JobExecutorEventTypes` (`JOB_STARTED: 20000`, `JOB_COMPLETED: 20001`, `JOB_FAILED: 20002`, `EXECUTOR_STARTED: 20003`, `EXECUTOR_STOPPED: 20004`). **WARNING:** Two distinct job-failed payloads exist. The reactor-level `ReactorEventTypes.JOB_FAILED` (10005) is exported as `ReactorJobFailedEvent` and carries `jobId`, `error: Error` and an optional `job`. The executor-level `JobExecutorEventTypes.JOB_FAILED` (20002) is exported as `JobFailedEvent` and carries `job` and `error: string`. Check which enum you subscribed to before reading `error`. See [Reactor event system](/academy/Reference/Reactor/WorkingWithTheReactor#reactor-event-system) for the full list of event types. ## Working with the operation store The `IOperationStore` provides direct access to the append-only operation log. ```typescript const { operationStore } = module; // Get operations since a specific revision const ops = await operationStore.getSince(docId, "global", "main", 5); // Get the latest revision per scope const revisions = await operationStore.getRevisions(docId, "main"); ``` **WARNING:** Writing directly to the operation store bypasses the job queue, reducers, and read models. In almost all cases, use `reactor.execute()` or `reactor.load()` instead. Direct store access is intended for read-only inspection, debugging, and testing. ## Registering custom read models A read model receives operations after each job's write phase completes and builds a derived view of the data. ```typescript class MyCustomReadModel implements IReadModel { // `name` identifies the read model for lookup via getReadModel readonly name = "my-custom-read-model"; async indexOperations(operations: OperationWithContext[]): Promise { for (const { operation, context } of operations) { // Build your derived view } } } const reactor = await new ReactorBuilder() .withDocumentModelSources([todoListModule]) .withReadModel(new MyCustomReadModel()) .build(); ``` Read models registered via `withReadModel()` run in the pre-ready phase — they complete before `JOB_READ_READY` fires. This is in contrast to processors (registered via `ProcessorManager`), which run in the post-ready phase. ## Example: integration test setup A common use case for the low-level API is writing integration tests that need full control over the reactor lifecycle: `ReactorClientBuilder.buildModule()` returns an `InProcessReactorClientModule`. It holds the `client`, its `read` and `write` halves, direct references to `reactor`, `eventBus`, `documentIndexer`, `documentView`, `signer`, `subscriptionManager` and `jobAwaiter`, and the `reactorModule`. `reactorModule` is the same `InProcessReactorModule` you get from `ReactorBuilder.buildModule()`, typed `InProcessReactorModule | undefined` because the `withReactor` path has none. Narrow it before use. Hand the builder a `ReactorBuilder` via `withReactorBuilder` and it builds the reactor for you: ```typescript async function createTestReactor() { const builder = new ReactorBuilder() .withDocumentModelSources([todoListModule]) .withLogger(new ConsoleLogger()); // buildModule returns both the client and the reactor module internals const { client, reactorModule } = await new ReactorClientBuilder() .withReactorBuilder(builder) .buildModule(); if (!reactorModule) { throw new Error("withReactorBuilder always yields a reactorModule"); } return { client, reactorModule }; } // In your test const { client, reactorModule } = await createTestReactor(); // Use client for high-level operations const handle = await client.write.createEmpty("powerhouse/todo-list"); const doc = await handle.document(); // Use the reactor module for low-level inspection const ops = await reactorModule.operationStore.getSince( doc.header.id, "global", "main", 0, ); expect(ops.results.length).toBe(1); // Cleanup reactorModule.reactor.kill(); ``` If you already built a reactor and have its internals, wire them directly instead of passing a builder: ```typescript const client = await new ReactorClientBuilder() .withReactor( module.reactor, module.eventBus, module.documentIndexer, module.documentView, ) .build(); ``` The `withReactor` path receives no feature flags and no registry, so the client cannot derive a read gate from the reactor (see below). ### ReactorClientBuilder methods | Method | Description | | ----------------------------------- | -------------------------------------------------------------------- | | `withLogger(logger)` | Set the logger (defaults to `ConsoleLogger`) | | `withReactorBuilder(builder)` | Build the reactor from a `ReactorBuilder` (mutually exclusive with `withReactor`) | | `withReactor(reactor, eventBus, documentIndexer, documentView)` | Wire an already-built reactor and its internals | | `withSigner(config)` | Set an `ISigner` or `SignerConfig` for signing | | `withSubscriptionManager(manager)` | Provide a custom subscription manager | | `withJobAwaiter(awaiter)` | Provide a custom job awaiter | | `withDocumentModelLoader(loader)` | Set a custom document model loader (forwarded to the reactor builder) | | `withReadGate(gate)` | Override how reads are gated with an `IReadGate` (see below) | | `build()` | Build and return the `IReactorClient` | | `buildModule()` | Build and return the client plus its reactor module internals | You must call exactly one of `withReactorBuilder` or `withReactor` before `build()`/`buildModule()`, or the build throws. The two wiring paths do not gate reads the same way. A client built with `withReactorBuilder` derives its read gate from that reactor's feature flags and model registry, so `{ group }` principals resolve once `authGroups` is on. A client built with `withReactor(...)` evaluates the policy alone. A `{ group }` grant never matches, and a reader who holds a scope only through a roster loses that scope without an error. To run several per-identity clients over one already-built reactor, pass each the same gate through `withReadGate`. `ModelReadGate`, `BareReadGate` and `readDecisionModel` are exported from `@powerhousedao/reactor`. See [Authorization](/academy/Reference/Reactor/Authorization). --- ## Synchronization and remote drives > Source: https://academy.vetra.io/academy/Reference/Reactor/Synchronization Sync makes one reactor mirror operations with a remote drive. The local reactor pushes operations it produces and pulls operations the remote produces, so two reactors converge on the same document history. You need it whenever a reactor must share state with another reactor: a Connect tab syncing to a Switchboard server, or two services replicating a drive. The sync subsystem lives in `@powerhousedao/reactor`. The orchestrator is `ISyncManager`; each remote drive is a `Remote` backed by an `IChannel`. Two transports ship: - GraphQL over HTTP, channel config `type` `"gql"`: `GqlRequestChannel` (the client/poller side, used by Connect) and `GqlResponseChannel` (the server/push side, used by Switchboard). - In-process over a `MessagePort`, channel config `type` `"local"`: `LocalChannel`, built by `LocalChannelFactory`. Both sides push, and there is no server. Connect uses it in multi-reactor mode to sync reactors that run in the same browser. Register it next to a scheme with `withAdditionalChannelFactory("local", factory)`. ![Reactor sync: outbox derived from the operation index, inbox applied through IReactor, mailboxes per remote channel](../docs/docs/images/reactor-sync-light.png) For the reactor itself and the builder, see [Working with the Reactor](/academy/Reference/Reactor/WorkingWithTheReactor) and [Advanced Reactor Usage](/academy/Reference/Reactor/AdvancedReactorUsage). For sync error categories and auth failures, see [Error handling](/academy/Reference/Reactor/ErrorHandling). ## Enabling sync on a reactor Sync is off by default. A reactor has no sync manager unless you call either `withChannelScheme` or `withSync` on the `ReactorBuilder`. Use one or the other, not both. ### The shortcut: `withChannelScheme` `withChannelScheme(scheme)` builds the channel factory and `SyncBuilder` for you, then starts the sync manager during `build()`. Pick the scheme by which side you are: - `ChannelScheme.CONNECT` builds a `GqlRequestChannelFactory` (the polling client side). - `ChannelScheme.SWITCHBOARD` builds a `GqlResponseChannelFactory` (the push server side). ```typescript const module = await new ReactorBuilder() .withDocumentModelSources([/* ... */]) .withChannelScheme(ChannelScheme.CONNECT) .withJwtHandler(async (url) => getTokenForAudience(url)) // optional, CONNECT only .buildModule(); const sync = module.syncModule?.syncManager; // ISyncManager | undefined ``` `withJwtHandler` only flows through the `CONNECT` factory; on the `SWITCHBOARD` path it is ignored. The handler is called per request with the target URL and returns the bearer token or `undefined` when unauthenticated: ```typescript type JwtHandler = (url: string) => Promise; ``` `withChannelScheme` and `withSync` are mutually exclusive: if you set both, `build()` and `buildModule()` throw. Use the scheme, or pass your own `SyncBuilder`, not both. ### Full control: `withSync` Pass a pre-configured `SyncBuilder` when you need a custom channel factory, custom storages, or different limits. The reactor builder still calls `syncManager.startup()` for you on this path. ```typescript ReactorBuilder, SyncBuilder, GqlRequestChannelFactory, } from "@powerhousedao/reactor"; const syncBuilder = new SyncBuilder() .withChannelFactory(new GqlRequestChannelFactory(logger, jwtHandler, queue)) .withMaxInboxBatchSize(32) .withMaxDeadLettersPerRemote(100); const module = await new ReactorBuilder() .withDocumentModelSources([/* ... */]) .withSync(syncBuilder) .buildModule(); ``` If neither method is called, `module.syncModule` is `undefined` and the reactor does not sync. ## `SyncBuilder` reference `SyncBuilder` configures the sync module. Every `withX` method returns `this`. The only required call is `withChannelFactory` — `build`/`buildModule` throws `"Channel factory is required"` without it. | Method | Effect | Required | | --- | --- | --- | | `withChannelFactory(factory: IChannelFactory)` | Sets the transport factory. | Yes | | `withRemoteStorage(storage: ISyncRemoteStorage)` | Overrides where remote records persist. Defaults to a Kysely-backed store on the reactor database. | No | | `withCursorStorage(storage: ISyncCursorStorage)` | Overrides sync-cursor storage. Defaults to a Kysely-backed store. | No | | `withDeadLetterStorage(storage: ISyncDeadLetterStorage)` | Overrides dead-letter storage. Defaults to a Kysely-backed store. | No | | `withMaxDeadLettersPerRemote(limit: number)` | Caps retained dead letters per remote. Default `100`. | No | | `withMaxInboxBatchSize(limit: number)` | Caps how many inbound operations apply per batch. Default `32`. | No | Terminal methods. `build(...)` returns the `ISyncManager`; `buildModule(...)` returns the full `InProcessSyncModule` (`{ remoteStorage, cursorStorage, deadLetterStorage, channelFactory, syncManager }`). Both take, in order: `reactor`, `logger`, `operationIndex`, `eventBus`, `db`, `driveContainerTypes`. ```typescript buildModule( reactor: IReactor, logger: ILogger, operationIndex: IOperationIndex, eventBus: IEventBus, db: Kysely, driveContainerTypes: ReadonlySet, ): InProcessSyncModule ``` Neither `build` nor `buildModule` calls `startup()`. If you construct a `SyncBuilder` directly instead of going through `ReactorBuilder`, you must call `syncManager.startup()` yourself before adding remotes. ## Adding a remote `ISyncManager.add` registers a remote drive, persists it, creates its channel, and calls `channel.init()`. ```typescript add( name: string, collectionId: DriveCollectionId, channelConfig: ChannelConfig, filter?: RemoteFilter, options?: RemoteOptions, id?: string, ): Promise; ``` - `name` is the unique key for the remote across the manager. Reusing a name throws ``Remote with name '${name}' already exists``. - `collectionId` identifies the drive to sync. Build it with `DriveCollectionId.forDrive(driveId, branch?)` (branch defaults to `"main"`). - `channelConfig` wires the transport. - `filter` defaults to `{ documentId: [], scope: [], branch: "" }` (no restriction). - `options` defaults to `{ sinceTimestampUtcMs: "0" }` (sync from the start of history). - `id` defaults to a generated UUID. If `channel.init()` throws, `add` rolls the remote back out of both the map and storage and rethrows the original error. Calling `add` after `shutdown()` throws `"SyncManager is shutdown and cannot add remotes"`. ### `ChannelConfig` ```typescript type ChannelConfig = { type: string; parameters: Record }; ``` For the GraphQL request channel, `type` is `"gql"` and `parameters` must include `url`. The factory validates `parameters` at `add`/startup time and throws if `url` is missing or empty: ```typescript { type: "gql", parameters: { url: "https://switchboard.example/d/my-drive/graphql", // optional, with their defaults: // pollIntervalMs: 2000, // retryBaseDelayMs: 1000, // retryMaxDelayMs: 300000, // maxQueueDepth: 100, // backpressureCheckIntervalMs: 500, // fetchFn: customFetch, }, } ``` Any optional parameter present with the wrong type throws at registration, for example `'"pollIntervalMs" parameter must be a number'`. ### `RemoteFilter` ```typescript type RemoteFilter = { documentId: string[]; scope: string[]; branch: string }; ``` An empty `documentId` or `scope` array means "no restriction" on that dimension. An empty `branch` string means "no branch restriction". Otherwise the filter is a membership or equality test against each operation. ### `RemoteOptions` ```typescript type RemoteOptions = { sinceTimestampUtcMs?: string; // stringified UTC ms; "0"/undefined sync from start pollBehavior?: PollBehavior; // defaults to Auto }; ``` When `sinceTimestampUtcMs` is set and not `"0"`, outbox operations older than that timestamp are excluded. ### `ChannelScheme` ```typescript enum ChannelScheme { CONNECT = "connect", SWITCHBOARD = "switchboard", } ``` `CONNECT` selects `GqlRequestChannelFactory` (polls and pushes). `SWITCHBOARD` selects `GqlResponseChannelFactory` (push-driven, server-side). `GqlRequestChannelFactory` takes `(logger, jwtHandler, queue)` — the queue is needed for poll backpressure. `GqlResponseChannelFactory` takes `(logger)`. ### How Connect adds a drive The app-level helper `addRemoteDrive(url, driveId?, options?)` in `@powerhousedao/reactor-browser` shows the canonical call. It reads `window.ph.reactorClient` and the sync manager (throwing `"ReactorClient not initialized"` or `"Sync not initialized"` if absent), fetches `{ id, graphqlEndpoint }` from `url`, then registers the remote: ```typescript const collectionId = DriveCollectionId.forDrive(resolvedDriveId); await sync.add( crypto.randomUUID(), // name collectionId, { type: "gql", parameters: { url: driveInfo.graphqlEndpoint } }, undefined, // filter -> no restriction options?.pollBehavior ? { pollBehavior: options.pollBehavior } : undefined, ); ``` Before adding, it dedupes against `sync.list().find((r) => r.meta.collectionId.equals(collectionId))`. If `add` throws and `isDriveAuthError(error)` is true, it shows the drive-auth modal and rethrows. To remove a drive, find every remote whose `meta.collectionId.equals(...)` and call `sync.remove(remote.meta.name)`. ## Polling and pulling `PollBehavior` controls a remote's poll schedule: ```typescript enum PollBehavior { Auto = "auto", Manual = "manual", } ``` - `Auto` (default): the channel runs its interval timer in the background. The first tick fires immediately on `init()`, then repeats at `pollIntervalMs` (default 2000 ms). - `Manual`: the channel registers and connects, but the timer never ticks on its own. Pull on demand with `ISyncManager.triggerPull(name)`. ```typescript triggerPull(name: string): void; ``` `triggerPull` runs one pull cycle. For a `Manual` remote it does not start recurring polling — the remote stays Manual and a single fetch runs. `triggerPull` on a missing remote throws ``Remote with name '${name}' does not exist``. On a `GqlResponseChannel` (Switchboard side) `triggerPull` is a no-op, because that channel is push-driven. Polling also applies backpressure: a tick defers when the reactor queue depth exceeds `maxQueueDepth` and rechecks after `backpressureCheckIntervalMs`. Transient poll failures retry with exponential backoff between `retryBaseDelayMs` and `retryMaxDelayMs`. ## Watching sync status There are three views into sync state: per-document status, per-job completion, and per-remote connection state. ### Per-document status ```typescript getSyncStatus(documentId: string): SyncStatus | undefined; onSyncStatusChange(callback: SyncStatusChangeCallback): () => void; ``` `getSyncStatus` returns `undefined` for a document that has never been tracked. `onSyncStatusChange` returns an unsubscribe function. `SyncStatusChangeCallback` is `(documentId: string, status: SyncStatus) => void`. ```typescript enum SyncStatus { Synced = "SYNCED", Outgoing = "OUTGOING", Incoming = "INCOMING", OutgoingAndIncoming = "OUTGOING_AND_INCOMING", Error = "ERROR", } ``` Status derives from mailbox depth: any error gives `Error`; pending in both directions gives `OutgoingAndIncoming`; otherwise `Incoming`, `Outgoing`, or `Synced`. ### Per-job completion ```typescript waitForSync(jobId: string, signal?: AbortSignal): Promise; ``` `waitForSync` resolves when the job's sync operations finish. It resolves on both success and failure — a failed sync resolves with `status: "failed"`, it does not reject. The promise only rejects on abort or manager shutdown. If the job already finished, it returns the cached result immediately. ```typescript type SyncResult = { jobId: string; status: "succeeded" | "failed"; syncOperationCount: number; successCount: number; failureCount: number; errors: Array<{ remoteName: string; documentId: string; error: string }>; }; ``` `SyncResult` is the documented return shape but is not exported as a named type from `@powerhousedao/reactor`; treat it structurally. Note the worker boundary. When the sync manager runs in a SharedWorker and you hold the tab-side RPC proxy, `waitForSync` always rejects with `"waitForSync is not supported over the worker RPC boundary"`. Use `getSyncStatus`/`onSyncStatusChange` or per-remote connection state instead. ### Per-remote connection state Each `Remote` exposes its live channel. Read or subscribe to its connection state: ```typescript getConnectionState(): ConnectionStateSnapshot; onConnectionStateChange(cb: (snapshot: ConnectionStateSnapshot) => void): () => void; ``` ```typescript type ConnectionState = | "connecting" | "connected" | "disconnected" | "reconnecting" | "error"; type ConnectionStateSnapshot = { state: ConnectionState; failureCount: number; lastSuccessUtcMs: number; lastFailureUtcMs: number; pushBlocked: boolean; pushFailureCount: number; receivingPages: boolean; requiresAuth: boolean; // only meaningful while state === "error" }; ``` `requiresAuth` is set when the remote rejected the caller as unauthenticated (HTTP 401/403 or a Forbidden GraphQL error) and is only meaningful while `state` is `"error"`. See [Error handling](/academy/Reference/Reactor/ErrorHandling) for the full error taxonomy and `isDriveAuthError`. ### React hooks `@powerhousedao/reactor-browser` exposes two hooks: ```typescript const sync = useSync(); // ISyncManager | undefined const remotes = useSyncList(); // Remote[] ``` `useSync` returns the sync manager off the reactor client module, or `undefined` when sync is not wired. `useSyncList` returns `sync.list()` (or `[]`). It recomputes on every render and does not subscribe to add/remove, so combine it with a connection-state subscription if you need a re-render when state changes: ```typescript const remotes = useSyncList(); const remote = remotes.find((r) => r.meta.collectionId.equals(DriveCollectionId.forDrive(driveId)), ); const snapshot = remote?.channel.getConnectionState(); // remote.meta is cloneable; remote.channel is the live channel. // To read the GraphQL URL: (remote.channel as GqlRequestChannel).config.url ``` ### Sync events The sync subsystem uses a 20000-range event namespace, distinct from the 10000-range reactor events. Subscribe through the reactor's `IEventBus` (see the event-bus section on [Working with the Reactor](/academy/Reference/Reactor/WorkingWithTheReactor)). ```typescript const SyncEventTypes = { SYNC_PENDING: 20001, SYNC_SUCCEEDED: 20002, SYNC_FAILED: 20003, DEAD_LETTER_ADDED: 20004, CONNECTION_STATE_CHANGED: 20005, } as const; ``` Payload shapes: ```typescript type SyncPendingEvent = { jobId: string; syncOperationCount: number; remoteNames: string[]; }; type SyncSucceededEvent = { jobId: string; syncOperationCount: number }; type SyncFailedEvent = { jobId: string; successCount: number; failureCount: number; errors: Array<{ remoteName: string; documentId: string; error: string }>; }; type ConnectionStateChangedEvent = { remoteName: string; remoteId: string; previous: ConnectionState; current: ConnectionState; snapshot: ConnectionStateSnapshot; }; ``` `CONNECTION_STATE_CHANGED` (20005) fires on every channel transition; subscribe to it for a global view across all remotes. The manager does not track the prior state, so `previous` currently mirrors `current` — read `snapshot.state` rather than relying on `previous`. `SYNC_SUCCEEDED`/`SYNC_FAILED` back `waitForSync`; prefer `waitForSync` over subscribing to them directly. ## Dead letters A dead letter is a sync operation that failed and cannot be retried — a bad signature, a hash mismatch, or an unrecoverable transport error. When a channel routes an operation to its dead-letter mailbox, the sync manager logs it, quarantines the document, persists a record to dead-letter storage, and emits `DEAD_LETTER_ADDED`. ```typescript type DeadLetterAddedEvent = { id: string; jobId: string; remoteName: string; documentId: string; errorSource: ChannelErrorSource; }; enum ChannelErrorSource { None = "none", Channel = "channel", Inbox = "inbox", Outbox = "outbox", } ``` Quarantine is per document. Once a document is quarantined, its inbound operations are dropped and its outbound operations are filtered out, so a single poisoned document does not stall the rest of the drive. Each remote retains at most `maxDeadLettersPerRemote` (default 100) dead letters; the oldest are evicted past that. Subscribe to `DEAD_LETTER_ADDED` to surface failures that need manual intervention. ## Gotchas | Call | Throws | | --- | --- | | `SyncBuilder.build`/`buildModule` without `withChannelFactory` | `"Channel factory is required"` | | `sync.add(name, ...)` with a duplicate name | ``Remote with name '${name}' already exists`` | | `sync.add` after `shutdown()` | `"SyncManager is shutdown and cannot add remotes"` | | `sync.startup()` after `shutdown()` | `"SyncManager is already shutdown and cannot be started"` | | `getByName` / `triggerPull` / `remove` on an unknown remote | ``Remote with name '${name}' does not exist`` | | `getById` on an unknown remote | ``Remote with id '${id}' does not exist`` | | `add` with a `"gql"` config missing/empty `url` | `'GqlRequestChannelFactory requires "url" parameter in config.parameters'` | | `waitForSync` over the worker RPC proxy | `"waitForSync is not supported over the worker RPC boundary"` | ## Not yet stable - The `IChannelFactory.instance` parameters `cursorStorage` and `operationIndex` are marked for removal in source. Do not depend on the factory signature staying fixed. - `GqlResponseChannel`/`GqlResponseChannelFactory` are Switchboard-side primitives, relevant to reactor-api wiring rather than typical app code. - `ConnectionStateChangedEvent.previous` does not currently carry the real prior state. ## Related - [Working with the Reactor](/academy/Reference/Reactor/WorkingWithTheReactor) — the event bus and reactor lifecycle. - [Advanced Reactor Usage](/academy/Reference/Reactor/AdvancedReactorUsage) — `ReactorBuilder` and low-level `IReactor`. - [Error handling](/academy/Reference/Reactor/ErrorHandling) — sync error categories, `isDriveAuthError`, retries. - [Storage and scaling](/academy/Reference/Reactor/StorageAndScaling) — storage backends behind cursors and dead letters. - [Processors](/academy/Reference/Reactor/Processors) and [Building a processor](/academy/Build/WorkWithData/BuildingAProcessor) — reacting to synced operations. --- ## Processors > Source: https://academy.vetra.io/academy/Reference/Reactor/Processors A **processor** receives operations after the write phase and performs side effects: analytics rollups, relational indexing, webhooks, dispatching follow-up actions. Its `onOperations` method runs **post-ready** — after the reactor emits `JOB_READ_READY` for a chain, when the document is already readable and consistent. This is the contrast with **read models**. Read models index **pre-ready**: their `indexOperations` runs before `JOB_READ_READY` fires, so they gate read-after-write consistency. A reader holding a [consistency token](/academy/Reference/Reactor/WorkingWithTheReactor) is unblocked only once the pre-ready models finish. Processors are not on that critical path; they observe operations only after readers can already see them. For the full pre-ready/post-ready ordering see [Working with the Reactor](/academy/Reference/Reactor/WorkingWithTheReactor), and for read models see [Advanced Reactor Usage](/academy/Reference/Reactor/AdvancedReactorUsage). This page is the reactor-side reference: the types you implement, the manager that drives them, and how catch-up works. For the editor-side authoring flow (scaffolding a processor in a package) see the [processor tutorial](/academy/Build/WorkWithData/BuildingAProcessor). **WARNING:** Processors run only on the **in-process reactor path**. On the SharedWorker reactor path, processor registration is **not bridged** — Connect logs a warning and skips every factory (`apps/connect/src/store/reactor.ts`). Worker-hosted reactors do not run processors today. ## Registering a processor Register a factory with the **processor manager**. The manager interface is `IProcessorManager`: ```typescript interface IProcessorManager { registerFactory(identifier: string, factory: ProcessorFactory): Promise; unregisterFactory(identifier: string): Promise; get(processorId: string): TrackedProcessor | undefined; getAll(): TrackedProcessor[]; } ``` `identifier` is the registration key and the `factoryId` prefix of every processor the factory produces. Real callers pass the package name. Registering the same `identifier` twice unregisters the prior factory first, so re-registration is idempotent. The manager is not a method on `IReactor`. It lives on the in-process reactor module as `processorManager`. In Connect you reach it through the browser-path reactor module: ```typescript const reactorModule = reactorClientModule.kind === "browser" ? reactorClientModule.reactorModule : undefined; await reactorModule?.processorManager.registerFactory(id, factory); ``` ### What the factory receives and returns A `ProcessorFactory` is called once per drive. It receives the drive header and returns the processors for that drive: A drive must exist before any processor runs. The manager calls the factories when it sees a `CREATE_DOCUMENT` operation whose document type is a drive container (`powerhouse/document-drive` or `powerhouse/reactor-drive` by default, see `withDriveContainerTypes`). Until a drive exists, a registered factory is never invoked and no operations are routed, even for plain documents that match a filter. In a standalone reactor, create a drive before you expect `onOperations` to fire. ```typescript type ProcessorFactory = ( driveHeader: PHDocumentHeader, processorApp?: ProcessorApp, ) => Promise | ProcessorRecord[]; ``` The header is the drive's header as it was created, from its `CREATE_DOCUMENT` operation. That holds for a drive created while the factory is registered and for one that already existed when `registerFactory` is called (after a restart, say): earlier releases passed such a late registration a minimal header with only `id` and `documentType` set. A factory that selects drives by `slug` or `name` therefore now makes its processors on a late registration too, and they backfill from their persisted cursor, or per `startFrom` if they have none. Only a purged drive's header is still minimal, since its header is erased with it. The manager reads a late registration's headers from the operation index, a few drives at a time. Return `[]` to create no processors for a drive. The factory may be sync or async. The `processorApp?` parameter is part of the type but the manager **never supplies it** — it calls `factory(driveHeader)` with one argument. Read the running app from `module.processorApp` instead (see [the processor host](#the-processor-host)). If the factory throws, the manager catches it, logs `Factory '' failed for drive ''`, and skips that drive. Registration does not crash. Each element of the returned array is a `ProcessorRecord` — the exact shape the factory must produce: ```typescript type ProcessorRecord = { processor: IProcessor; filter: ProcessorFilter; startFrom?: "beginning" | "current"; replayState?: "latest" | "collapse" | "exact" | "operations"; id?: string; // stable cursor key; derived from the processor's namespace or class name when omitted }; ``` `processor` and `filter` are required. `startFrom` is covered under [catch-up and ordering](#catch-up-and-ordering). `replayState` sets what a replayed operation carries. A replay (first install, catch-up after a restart, rerun, retry) reads operations from the index, which keeps no `resultingState`; a live delivery always carries the executor's state after the operation. | `replayState` | A replayed operation carries | | ------------------ | -------------------------------------------------------------------------------------------------------------------------- | | `latest` (default) | Every operation, with the document's latest state. It can be newer than the operation, never older than a state already delivered for that stream. | | `collapse` | Per document stream, one delivery with the latest state; the operations it includes are skipped. Only for processors whose output depends on the latest state alone. | | `exact` | Every operation, with its state at its own revision. Slower. | | `operations` | Every operation, without state. | ### The IProcessor interface ```typescript interface IProcessor { onOperations(operations: OperationWithContext[]): Promise; onDisconnect(): Promise; } ``` `onOperations` is called post-ready with the operations that matched this processor's filter, during both live routing and backfill. Each `OperationWithContext` is `{ operation, context }`; `context` carries `documentId`, `documentType`, `scope`, `branch`, the global `ordinal`, and optionally `resultingState`. On a replay, `resultingState` is what the record's `replayState` says. `onDisconnect` runs when the factory is unregistered or its drive is deleted. The manager wraps this call: a throw is caught and logged, never propagated. It is not a deletion signal; see [Deletion and erasure](#deletion-and-erasure). Most processors subclass `RelationalDbProcessor` rather than implementing `IProcessor` by hand. Its constructor takes `(namespace, filter, relationalDb)`, and you implement `onOperations`, `initAndUpgrade`, and `onDisconnect`. See [Storage and scaling](/academy/Reference/Reactor/StorageAndScaling) for the relational DB surface. ### A complete factory builder Packages export a `ProcessorFactoryBuilder`, not a bare factory. The builder receives the [host module](#the-processor-host) and returns a `ProcessorFactory`: ```typescript type ProcessorFactoryBuilder = ( module: IProcessorHostModule, ) => Promise | ProcessorFactory; ``` This is the shape codegen emits for a relational processor: ```typescript IProcessorHostModule, ProcessorApp, ProcessorFactoryBuilder, ProcessorFilter, ProcessorRecord, } from "@powerhousedao/reactor-browser"; export const todoIndexerFactoryBuilder: ProcessorFactoryBuilder = (module: IProcessorHostModule) => async (driveHeader: PHDocumentHeader, processorApp?: ProcessorApp) => { const namespace = TodoIndexer.getNamespace(driveHeader.id); const store = await module.relationalDb.createNamespace(namespace); const filter: ProcessorFilter = { branch: ["main"], documentId: ["*"], documentType: ["powerhouse/todo"], scope: ["global"], }; const processor = new TodoIndexer(namespace, filter, store, driveHeader.id); return [{ processor, filter }]; }; ``` Wiring at startup is two stages: build the host module, call the builder to get a factory, then register it. ```typescript const factory = await todoIndexerFactoryBuilder(processorHostModule); await reactorModule.processorManager.registerFactory("@my-org/my-package", factory); ``` Processor types are re-exported from `@powerhousedao/reactor-browser` as types only. `RelationalDbProcessor` is re-exported as a value. `IProcessorManager`, `ProcessorStatus`, `TrackedProcessor`, and the concrete `ProcessorManager` class are exported from `@powerhousedao/reactor`, not from reactor-browser. ## ProcessorFilter The filter decides which operations reach a processor: ```typescript type ProcessorFilter = { documentType?: string[]; scope?: string[]; branch?: string[]; documentId?: string[]; }; ``` Every field is an optional array. Matching is per field: a field that is `undefined` or `[]` matches everything; a field with values matches an operation whose corresponding context value is in the array. All present fields must match. ```typescript // Global-scope operations on any todo document, main branch only. const filter: ProcessorFilter = { documentType: ["powerhouse/todo"], scope: ["global"], branch: ["main"], documentId: ["*"], }; ``` The `"*"` wildcard is honored **only in `documentId`**. There it means "any document". In `documentType`, `scope`, and `branch` there is no wildcard special-case: a literal `"*"` matches only an operation whose value is the string `"*"`. To match all scopes, omit `scope` or set it to `[]` — do not write `scope: ["*"]`. **WARNING:** The codegen analytics template emits `scope: ["*"]`. Under the matcher this matches a scope literally named `"*"`, which matches nothing real. Set `scope` to the concrete scopes you want, or omit it. `startFrom` on the `ProcessorRecord` controls the catch-up starting point, not which operations match — see [catch-up and ordering](#catch-up-and-ordering). ## The processor host The host module is the context a processor gets through its factory builder. The host-agnostic core is `IProcessorHostModuleBase`: ```typescript interface IProcessorHostModuleBase { analyticsStore: IAnalyticsStore; relationalDb: IRelationalDb; processorApp: ProcessorApp; dispatch: IProcessorDispatch; getReadModel(name: string): T; config?: Map; } ``` - **`analyticsStore`** — the analytics store for time-series rollups. - **`relationalDb`** — an `IRelationalDb` (a Kysely instance plus `createNamespace` / `queryNamespace`) for relational indexing. See [Storage and scaling](/academy/Reference/Reactor/StorageAndScaling). - **`processorApp`** — `"connect" | "switchboard"`. How a processor learns which app hosts it. Read this rather than the factory's `processorApp?` argument. - **`dispatch`** — how a processor writes. Each member resolves once the write is accepted, answers with the job's `{ id, status }`, and does not wait for the commit: - `execute(docId, branch, actions, signal?, meta?)` submits actions on one document. - `createInDrive(driveId, document, parentFolder?, signal?)` adds a document to a drive and also returns its `documentId`. The document's own creation has completed when it resolves; the drive's file node has been accepted. - `deleteDocument(docId, propagate?, signal?)` deletes a document; pass `"cascade"` to delete its orphaned children too. Do not wait for a write to reach `READ_READY` from inside `onOperations`: the current batch cannot get there while your callback is still running. Use `dispatch` and handle the result in a later `onOperations` call (see [Processor best practices](/academy/Build/WorkWithData/ProcessorBestPractices)). - **`getReadModel(name)`** — looks up a registered read model by its `name`. Reactor-registered names are typed: `getReadModel("document-view")` returns `IDocumentView` and `"document-indexer"` returns `IDocumentIndexer`; other names take an explicit type argument. Connect's implementation throws `Read model "" not found` when there is no match. - **`config?`** — optional `Map` of host config. - **`read`** — the `IReactorReadClient` for reading documents, relationships, operations, and drives (`read.drives`). Reads are gated. See [IReactorClient](/academy/Reference/Reactor/ReactorClient). - **`attachments`** — the `IAttachmentClient` for uploading and downloading attachments. In Switchboard it makes no caller check, because a processor acts for no caller. See the [Attachment service](/academy/Reference/Reactor/AttachmentService). These six core fields are `IProcessorHostModuleBase` in `@powerhousedao/shared`. `@powerhousedao/reactor` adds `read` and the typed `getReadModel` as `IReactorProcessorHostModuleBase`; `@powerhousedao/reactor-browser` and `@powerhousedao/reactor-api` add `attachments` as `IProcessorHostModule`, since neither shared nor reactor can depend on reactor-attachments. Connect sets `processorApp: "connect"`. `IProcessorDispatch` and `ProcessorDispatchResult` are defined in shared but are not part of the public reactor export surface; treat the `dispatch` handle on the module as the supported entry point. ## Catch-up and ordering Each processor has a **cursor**, `lastOrdinal`: the highest operation `ordinal` it has taken, pulled back below any batch it failed to take. The manager tracks this per processor as `TrackedProcessor`: ```typescript type TrackedProcessor = { processorId: string; // `${factoryId}:${driveId}:${index}` factoryId: string; driveId: string; processorIndex: number; record: ProcessorRecord; lastOrdinal: number; status: ProcessorStatus; // "active" | "errored" lastError: string | undefined; lastErrorTimestamp: Date | undefined; retry: () => Promise; }; ``` **Ordering.** Batches reach the manager one document at a time, and different documents are projected in parallel, so a processor is not called in global ordinal order: a call may carry an ordinal lower than one an earlier call carried. Within one document's scope and branch, operations arrive in ordinal order; across documents there is no ordering guarantee. A processor receives one `onOperations` call at a time: each processor has its own delivery queue, and the next call begins after the previous one resolves. Batches that queue up behind a call are delivered together in the next one, in the order they arrived. A slow processor delays only its own queue. On each batch the manager applies the filter and queues the matches, minus any a backfill has already delivered. On success it raises `lastOrdinal` to the batch's maximum ordinal (including unmatched operations) when that is higher. Delivery is at-least-once: a processor may see an operation again after a restart or a retry, or when a backfill reads an operation whose live batch is still on its way to the manager. A crash between two concurrently projected documents, after the higher ordinal's cursor was persisted, can leave the lower ordinals unreplayed. **`startFrom`.** When a processor is first created and no cursor row exists yet: - `"beginning"` (the default) starts the cursor at ordinal 0, so the processor backfills the drive's full history. - `"current"` starts the cursor just below the drive's creation when the processor is created from the drive's creation batch, and at the manager's current ordinal when a late `registerFactory` creates it, so the processor sees only its drive's history or only what comes next. `startFrom` applies **only on first creation**. Once a cursor row exists, it is ignored — the persisted cursor wins. Restarting the reactor never re-runs `startFrom`. **Backfill and replay.** When a processor's `lastOrdinal` is behind the manager, the manager pages through history with `operationIndex.getSinceOrdinal(lastOrdinal)`, filters each page, calls `onOperations`, advances the cursor to the page's max ordinal, persists it, and follows the continuation until exhausted. This runs when a factory registers against a drive that already has history, and after a restart to replay anything missed while the reactor was down. Backfill runs on the processor's queue, so other documents keep indexing and other processors keep receiving; live batches for a processor that is backfilling wait behind it and skip whatever the backfill already delivered. `registerFactory` resolves once the factory has run and its processors are bound, before their backfills finish; to wait for a backfill, watch the processor's `lastOrdinal` through `get()` or `getAll()`. **Persistence.** Cursors are stored in the `ProcessorCursor` table, keyed by `processorId`, and survive restarts. On startup the manager rehydrates every cursor, then catch-up replays the gap. **Failure isolation.** Processors run in parallel, each on its own queue. If `onOperations` throws, or a backfill page cannot be read, that processor goes to `status: "errored"`, its `lastError` and `lastErrorTimestamp` are recorded, and its cursor is set below the lowest ordinal of the batch that failed. Other processors are unaffected. There is no automatic retry: an errored processor stays errored, and later batches that match it are skipped and pull its cursor below them likewise, until you call `tracked.retry()` (which re-activates it and replays from the cursor) or re-register its factory. See [Error handling](/academy/Reference/Reactor/ErrorHandling). A check for purged documents that keeps failing, as during a database failover, does not error the processor. After its retries (about 80 seconds of backoff) the processor parks: it stays `status: "active"` with the failure in `lastError` and `lastErrorTimestamp`, its cursor holds below the batch it could not check, later batches are skipped without holding other processors, and the check is retried every 30 seconds with each failure logged. Once the check answers, `lastError` clears and the processor replays from its cursor. A processor parked when the reactor stops starts active on the next start, and replays from its cursor. To inspect state, use the manager: `get(processorId)` for one processor or `getAll()` for every tracked processor across all drives. ## Deletion and erasure A processor owns the rows it writes, and it must erase a document's rows when the document is deleted. The reactor removes its own records of a deleted or purged document; nothing else removes a processor's. A processor that ignores deletions keeps a purged document's rows. There is no purge callback, and `IProcessor` has no other hook for it. - `DELETE_DOCUMENT` and `PURGE_DOCUMENT` both name the document in `operation.action.input.documentId`. Treat them alike. `PURGE_DOCUMENT` is the marker a purge leaves in the log; a reactor that missed the deletion receives only the marker. - Both are `document`-scope operations. A filter whose `scope` omits `"document"` never receives them. - The marker can arrive without `resultingState`, after the document's `DELETE_DOCUMENT`, and more than once. Handle deletions before parsing `resultingState`, and make the handler idempotent. - Operations of a purged document other than its marker are not delivered, whether routed live or read by a backfill. The check takes no lock, so a delivery racing the purge can still land; the marker that follows erases it. - When a drive is deleted, its processors receive the batch up to and including the drive's `DELETE_DOCUMENT` (or its marker), then `onDisconnect`. The drive's own deletion reaches them whatever their filter, so a processor can drop the storage it keeps for that drive. `RelationalDbProcessor` has `deleteDocumentRows`, `isNamespaceDrive` and `dropNamespace` for this. - A processor that was not running at its drive's deletion (its factory was not registered yet after a restart, or was being reloaded), or whose `onOperations` threw on it, still receives it: the next time its factory is registered, the factory is called for the deleted drive and that processor receives only the drive's `DELETE_DOCUMENT`, or its `PURGE_DOCUMENT` if the drive has been purged since, then `onDisconnect`. Neither `registerFactory` nor a later registration or unregistration under the same identifier waits for that delivery, so a processor that hangs on it holds no reload; a re-registration delivers the deletion again only if the delivery in flight settles with the drive still owed. An errored processor receives its drive's deletion too. A processor whose `onOperations` throws on its drive's deletion stays in `getAll()` as errored, and its `retry()` calls the factory for the drive and delivers the deletion again. The factory receives the drive's header as it was at creation, except for a drive that has been purged: its header is erased with it, so the factory receives a minimal header with only `id` and `documentType` set. A factory that selects drives by `slug` or `name` then makes no processor for the drive, and the deletion stays owed; the manager logs a warning. Only the rows of processors that received the deletion are removed. A factory registered for the first time after a drive's deletion never had processors for that drive and is not called for it. Unregistering a factory keeps its cursor rows as released: a re-registration starts every processor afresh, per `startFrom`, but still owes the deletion of a drive deleted in between. The rows of a factory that is never registered again stay in `ProcessorCursor`. For how processors relate to sync and remote drives, see [Synchronization](/academy/Reference/Reactor/Synchronization). For the document types a filter can target, see the [Document model registry](/academy/Reference/Reactor/DocumentModelRegistry). --- ## Document model registry and version upgrades > Source: https://academy.vetra.io/academy/Reference/Reactor/DocumentModelRegistry The **document model registry** holds document model modules keyed by document type and version. The reactor uses it to resolve the reducer, utils, and specification for a given document when it executes a job. It also holds **upgrade manifests** that describe how to migrate a document from one model version to the next. You register modules and manifests on the [`ReactorBuilder`](/academy/Reference/Reactor/AdvancedReactorUsage). At runtime you read the registry through the reactor module field `documentModelRegistry`, or through the [`useModelRegistry`](#accessing-the-registry) hook in the browser. ```typescript const reactor = await new ReactorBuilder() .withDocumentModelSources([todoModuleV1, todoModuleV2]) .withUpgradeManifests([todoUpgradeManifest]) .build(); ``` The two methods differ in how repeat calls combine. `withDocumentModelSources` **appends** — calling it twice registers both arrays. `withUpgradeManifests` **replaces** — calling it twice keeps only the last array. Order between the two calls does not matter: the builder always registers manifests before modules. Registration at build time is non-fatal. A duplicate or invalid module or manifest is logged through the builder's logger and skipped; the build still completes. Do not wrap `.build()` in a try/catch expecting a registration failure to throw — check the logs instead. ## Accessing the registry The registry instance lives on the reactor module as `documentModelRegistry: IDocumentModelRegistry`. This field is present on both in-process and worker-backed reactors. In the browser, read it with `useModelRegistry`: ```typescript const registry = useModelRegistry(); // IDocumentModelRegistry | undefined ``` `useModelRegistry` returns `undefined` until the reactor client module is set on the PH global. Guard the result before calling registry methods. The core `IReactor` interface does not expose the registry directly. To list model data through the reactor, use `reactor.getDocumentModels(namespace?, paging?, signal?)`, which reads `getAllModules()` internally and filters by document-type prefix. ## `IDocumentModelRegistry` reference `IDocumentModelRegistry` is the contract for both module management and the upgrade API. The default in-memory implementation is `DocumentModelRegistry`. Both are exported from `@powerhousedao/reactor`. The document type used as the lookup key is `module.documentModel.global.id`. A module's version comes from its optional `version` field, which the registry treats as `1` when absent. ### Module management #### `registerModules` ```typescript registerModules( ...modules: DocumentModelModule[] ): RegistrationResult>[]; ``` Registers one or more modules. Modules without a `version` default to version 1. Two different versions of the same document type coexist. A module whose `(documentType, version)` pair is already registered produces a `DuplicateModuleError` in its result. This method **never throws** — it returns one [`RegistrationResult`](#registrationresult) per input module, in input order. Invalid or duplicate modules are skipped without affecting the rest. #### `unregisterModules` ```typescript unregisterModules(...documentTypes: string[]): boolean; ``` Removes all versions of each given document type. Returns `true` only if every requested type was found. Returns `false` if any was missing, while still removing the types that exist. #### `getModule` ```typescript getModule(documentType: string, version?: number): DocumentModelModule; ``` Returns the module for a document type. With `version` omitted, returns the highest registered version. With `version` given, returns that exact version. Throws `ModuleNotFoundError` if the type is unknown, or if the specific version is not registered even when other versions exist. #### `getAllModules` ```typescript getAllModules(): DocumentModelModule[]; ``` Returns a copy of every registered module across all types and versions. #### `getSupportedVersions` ```typescript getSupportedVersions(documentType: string): number[]; ``` Returns the registered version numbers for a type, sorted ascending. Throws `ModuleNotFoundError` if none are registered for the type. #### `getLatestVersion` ```typescript getLatestVersion(documentType: string): number; ``` Returns the highest registered version number for a type. Throws `ModuleNotFoundError` if none are registered. #### `clear` ```typescript clear(): void; ``` Removes all modules and all upgrade manifests. ### `RegistrationResult` `registerModules` and `registerUpgradeManifests` report per-item outcomes instead of throwing: ```typescript export type RegistrationResult = | { status: "success"; item: T } | { status: "error"; item: T; error: Error }; ``` `item` is present on both branches — the registered or offending object is echoed back even on error. Check `result.status === "error"` for each element. ```typescript const results = registry.registerModules(todoModuleV1, todoModuleV2); for (const result of results) { if (result.status === "error") { console.error("skipped", result.item.documentModel.global.id, result.error.message); } } ``` `RegistrationResult` is not exported from `@powerhousedao/reactor`, and the package has no `registry` subpath. Reference it structurally, or read `result.status` (`"success"` or `"error"`) directly as shown above. ### `DocumentModelModule` The shape registered with the registry: ```typescript export type DocumentModelModule = { version?: number; reducer: Reducer; actions: Actions; utils: DocumentModelUtils; documentModel: DocumentModelPHState; // .global.id is the document type key }; ``` `version` is optional. The source marks it "should be made required"; module versioning is not finalized, and the registry defaults a missing `version` to `1` everywhere. Codegen emits every field. If you write a module by hand, build `reducer` with `createReducer(stateReducer)` from `document-model`. `createReducer` wraps a `StateReducer` (`(state: Draft, action, dispatch?) => TState | undefined`) in the base document reducer, so `SET_NAME`, `UNDO` and `REDO` keep working. `utils` is the `DocumentModelUtils` object that a generated `gen/utils.ts` assembles from `baseCreateDocument`, `baseLoadFromInput` and `baseSaveToFileHandle`. Copy that file's shape rather than typing it from memory. ## Version upgrades An **upgrade manifest** declares the supported versions of a document type and the transition that moves a document up each single version step. The registry stores one manifest per document type and exposes methods to look it up and to compute the steps between two versions. ### Upgrade types These types come from `@powerhousedao/shared/document-model` (aliased as `document-model` in generated files). ```typescript export type UpgradeReducer< TFrom extends PHBaseState, TTo extends PHBaseState, > = (document: PHDocument, action: Action) => PHDocument; export type UpgradeTransition = { toVersion: number; upgradeReducer: UpgradeReducer; description?: string; }; export type UpgradeManifest = { documentType: string; latestVersion: TupleMember; // union of versions, e.g. 1 | 2 | 3 supportedVersions: TVersions; // the tuple, e.g. [1, 2, 3] upgrades: { // keys: "v2" | "v3" | ... — no "v1" key (v1 is the base) [V in Exclude, 1> as `v${V}`]: UpgradeTransition; }; }; ``` Pass `TVersions` as a `readonly number[]` tuple (use `as const`). The type forces an `upgrades` key for every supported version except `1`: a manifest for `[1, 2, 3]` requires `v2` and `v3`. There is no `v1` key, because v1 is the base version. Registry methods type the manifest loosely as `UpgradeManifest`. The registry does not cross-check a manifest's versions against registered modules. Registering a manifest whose `supportedVersions` do not match the registered module versions succeeds. ### Authoring a manifest Manifest files are codegen output and carry a `WARNING: DO NOT EDIT` header. You author them by adding versions and upgrade reducers through codegen, not by editing the manifest by hand. The generated shape splits across three files. `versions.ts` declares the tuple: ```typescript export const supportedVersions = [1, 2] as const; export const latestVersion = supportedVersions[1]; ``` A transition file (`v2.ts`) holds the reducer for one step: ```typescript export const v2: UpgradeTransition = { toVersion: 2, upgradeReducer, // (document: PHDocument, action) => PHDocument description: "", }; ``` `upgrade-manifest.ts` assembles them: ```typescript export const todoUpgradeManifest: UpgradeManifest = { documentType: "test/todo", latestVersion, supportedVersions, upgrades: { v2 }, // key "v2", no "v1" }; ``` A manifest with only v1 has an empty `upgrades: {}`. ### Upgrade API #### `registerUpgradeManifests` ```typescript registerUpgradeManifests( ...manifests: UpgradeManifest[] ): RegistrationResult>[]; ``` Registers one or more manifests. A manifest with a falsy `documentType`, or one whose type already has a manifest (`DuplicateManifestError`), produces an error result. Like `registerModules`, this method never throws and returns one [`RegistrationResult`](#registrationresult) per input. Use `withUpgradeManifests` on the builder to register at build time. #### `unregisterUpgradeManifests` ```typescript unregisterUpgradeManifests(...documentTypes: string[]): boolean; ``` Removes the manifest for each given type. Returns `true` only if every type had a manifest; removes those that exist regardless. #### `getUpgradeManifest` ```typescript getUpgradeManifest(documentType: string): UpgradeManifest; ``` Returns the registered manifest. Throws `ManifestNotFoundError` if none exists for the type. #### `computeUpgradePath` ```typescript computeUpgradePath( documentType: string, fromVersion: number, toVersion: number, ): UpgradeTransition[]; ``` Returns the ordered transitions to move from `fromVersion` to `toVersion`. It walks the keys `v(from+1)..v(to)`. Behavior: - `from === to` returns `[]` and skips the manifest lookup, so it does not throw when no manifest is registered. - `to < from` throws `DowngradeNotSupportedError` before the manifest lookup. - No manifest for the type throws `ManifestNotFoundError`. - A missing step in the range throws `MissingUpgradeTransitionError`. #### `getUpgradeReducer` ```typescript getUpgradeReducer( documentType: string, fromVersion: number, toVersion: number, ): UpgradeReducer; ``` Returns the reducer for a **single** step, where `toVersion` must equal `fromVersion + 1`. The single-step check runs before the manifest lookup, so `InvalidUpgradeStepError` fires even with no manifest registered. Throws `ManifestNotFoundError` if no manifest exists, or `MissingUpgradeTransitionError` if that one transition is absent. ### Upgrade path example Given `todoUpgradeManifest` for `test/todo` with `supportedVersions = [1, 2]`: ```typescript const registry = useModelRegistry(); if (!registry) return; // Steps from v1 to v2: [v2 transition] const path = registry.computeUpgradePath("test/todo", 1, 2); // Same version: [] — no manifest needed const none = registry.computeUpgradePath("test/todo", 2, 2); // Single-step reducer for v1 -> v2 const reducer = registry.getUpgradeReducer("test/todo", 1, 2); // Downgrade throws DowngradeNotSupportedError registry.computeUpgradePath("test/todo", 2, 1); // Non-single-step throws InvalidUpgradeStepError (no v3 manifest needed to fail) registry.getUpgradeReducer("test/todo", 1, 3); ``` ### Applying an upgrade without the registry The registry methods need a built reactor. A script or test that upgrades a document in-process can use the same mechanics as plain functions over a manifest, exported from `document-model`. `UPGRADE_DOCUMENT` is a `document`-scope action with no generated creator. `@powerhousedao/reactor` exports `upgradeDocumentAction` for it, or build the action with `createAction` as below. ```typescript applyUpgradeDocumentAction, computeUpgradeTransitions, createAction, type UpgradeDocumentAction, } from "document-model"; const fromVersion = document.state.document.version; const toVersion = todoUpgradeManifest.latestVersion; // The ordered transitions. Throws if the manifest is missing a step const path = computeUpgradeTransitions(todoUpgradeManifest, fromVersion, toVersion); const action = createAction( "UPGRADE_DOCUMENT", { model: todoUpgradeManifest.documentType, fromVersion, toVersion, documentId: document.header.id, }, undefined, undefined, "document", ); // Runs each transition's upgradeReducer in order, then stamps toVersion const upgraded = applyUpgradeDocumentAction(document, action, path); ``` `applyUpgradeDocumentAction` runs each transition in `path` and stamps `state.document.version` on the document it returns. It does not clone: the version is written in place, so the document you passed in is the one that changed. Clone first (`structuredClone(document)`) if the caller needs the original. It rewrites `initialState` only when the action input carries an `initialState` (or `state`) field; the `createAction` call above carries neither, so stamping `initialState.document.version` is the caller's job, and each `upgradeReducer` must migrate `initialState` alongside `state` or a later replay fails its hash check. It appends nothing to the operation log. The reactor records the upgrade as one `UPGRADE_DOCUMENT` operation in the `document` scope. To mirror the reactor, build the operation with `operationFromAction(action, index, skip, context)`. `index` is the next index in `operations.document`, `skip` is `0`, and `context` is an `OperationContext` (`{ documentId, documentType, scope: "document", branch, ordinal }`). Replay never re-runs an upgrade. `replayDocument(initialState, operations, reducer, header, dispatch?, skipHeaderOperations?, options?)` rebuilds a document from its stored `initialState` and operations through the reducer you pass. When replaying a log recorded under an older version through the current reducer, pass `{ checkHashes: false }` in `options`. The per-operation hashes were computed over the old state shape, so only the final hash of each scope is checked. ## Errors All registry errors extend `Error` and set `name`. Each class with a static `isError(error): error is X` guard checks `Error.isError(error) && error.name === "..."`. | Error | `name` | Thrown when | | --- | --- | --- | | `ModuleNotFoundError` | `"ModuleNotFoundError"` | `getModule` for an unknown type or version; `getSupportedVersions` / `getLatestVersion` with no modules for the type. Carries `documentType`, `requestedVersion`. | | `DuplicateModuleError` | `"DuplicateModuleError"` | `registerModules` when the `(documentType, version)` pair is already registered (returned in the result, not thrown). | | `InvalidModuleError` | `"InvalidModuleError"` | Reserved for malformed modules. The in-memory `DocumentModelRegistry` does **not** throw it — duplicate is its only validation. | | `DuplicateManifestError` | `"DuplicateManifestError"` | `registerUpgradeManifests` when the type already has a manifest (returned in the result). | | `ManifestNotFoundError` | `"ManifestNotFoundError"` | `getUpgradeManifest`, and `computeUpgradePath` / `getUpgradeReducer`, when no manifest is registered for the type. | | `DowngradeNotSupportedError` | `"DowngradeNotSupportedError"` | `computeUpgradePath` when `toVersion < fromVersion`. Carries `documentType`, `fromVersion`, `toVersion`. | | `MissingUpgradeTransitionError` | `"MissingUpgradeTransitionError"` | `computeUpgradePath` (a `v{n}` step in range is absent) and `getUpgradeReducer` (the single `v{to}` step is absent). | | `InvalidUpgradeStepError` | `"InvalidUpgradeStepError"` | `getUpgradeReducer` when `toVersion !== fromVersion + 1`. | Of these, `ModuleNotFoundError`, `DuplicateModuleError`, and `DuplicateManifestError` have an `isError` guard. `InvalidModuleError`, `ManifestNotFoundError`, `MissingUpgradeTransitionError`, and `InvalidUpgradeStepError` do not — match on `error.name` instead. Export-surface gap: only `ModuleNotFoundError`, `DuplicateModuleError`, `InvalidModuleError`, and `DuplicateManifestError` are exported from `@powerhousedao/reactor`. `ManifestNotFoundError`, `MissingUpgradeTransitionError`, and `InvalidUpgradeStepError` are not exported, and the package has no `registry` subpath, so match those three on `error.name` even though `getUpgradeManifest`, `computeUpgradePath`, and `getUpgradeReducer` throw them. `DowngradeNotSupportedError` is available from `@powerhousedao/shared/document-model`. For the full reactor error taxonomy across job execution, sync, and storage, see [Error handling](/academy/Reference/Reactor/ErrorHandling). ## Related - [Working with the Reactor](/academy/Reference/Reactor/WorkingWithTheReactor) - [Advanced Reactor Usage](/academy/Reference/Reactor/AdvancedReactorUsage) — `ReactorBuilder` wiring - [IReactorClient](/academy/Reference/Reactor/ReactorClient) - [Processors](/academy/Reference/Reactor/Processors) and [Building a processor](/academy/Build/WorkWithData/BuildingAProcessor) --- ## Storage backends and scaling > Source: https://academy.vetra.io/academy/Reference/Reactor/StorageAndScaling The reactor persists every operation through a Kysely-backed storage layer. You pick the backend and the execution topology on the [`ReactorBuilder`](/academy/Reference/Reactor/WorkingWithTheReactor) before you call `build()`. This page covers the default in-memory backend, switching to Postgres, and the two scaling paths: an executor worker pool and sharded projection workers. Both scaling features are recent and design-doc-tracked. Where a path is not yet stable, this page says so. ## Default: in-memory PGlite Out of the box the builder gives you an in-memory [PGlite](https://pglite.dev/) database. When you build a reactor with only document models registered, the builder calls an internal `createDefaultDatabase()` that constructs `new Kysely({ dialect: new PGliteDialect(new PGlite()) })`. ```typescript const reactor = await new ReactorBuilder() .withDocumentModelSources([myDocumentModel]) .build(); ``` `new PGlite()` with no arguments is **in-memory**. Nothing is written to disk and nothing survives the process. This is the right backend for unit tests and local development, where you want a fresh, isolated database every run with no external service. What you get in this path: - Migrations run automatically. The migration strategy defaults to `"auto"`, so the builder runs `runMigrations(db, REACTOR_SCHEMA)` to create the `reactor` schema and apply the current migration set. - All storage operates under the `"reactor"` Postgres schema (`REACTOR_SCHEMA === "reactor"`). - A single in-process executor runs jobs (`maxConcurrency` defaults to `1`). - An in-process read-model coordinator keeps the document view and indexer up to date. - `module.pools` is `[]` — there is no `pg.Pool` to instrument. State is ephemeral. Move to Postgres when you need data to survive a restart, when more than one process must share storage, or when you scale to a worker pool or projection shards (neither can run on PGlite — see below). ## Choosing a backend The builder picks the base database in this precedence order, inside `buildModule()`: 1. The instance you passed to `withKysely(...)`, if any. 2. A Postgres pool, if the worker pool is enabled and a worker DB config is set. 3. The in-memory PGlite default. ### Bring your own Kysely instance `withKysely` injects a pre-built Kysely instance and overrides default database creation. ```typescript withKysely(kysely: Kysely): this ``` `Database` is the combined schema the reactor operates over: ```typescript type Database = StorageDatabase & DocumentViewDatabase & DocumentIndexerDatabase; ``` `StorageDatabase` holds the operation log and sync tables (`Operation`, `Keyframe`, `document_collections`, `operation_index_operations`, `sync_remotes`, `sync_cursors`, `sync_dead_letters`); the other two cover the document view and the document indexer. `Database`, `StorageDatabase`, `DocumentIndexerDatabase`, and `OperationTable` are exported from the package root. A persistent backend needs a Postgres-compatible dialect and the `reactor` schema. Pass a real Postgres `Kysely` here when you want to control pool construction yourself; otherwise use the worker pool's `db` (next section), which builds the pool for you. If you build the pool yourself, register its instrumentation with `withInstrumentedPool(instrumentation)` so pool stats surface through `module.pools`. ### Postgres via the worker pool's db When the worker pool is configured with a `db` (below), the builder builds the parent reactor's Postgres pool for you from that same `DbConfig`. The internal `createPostgresDatabase` constructs a `pg.Pool` and wraps it in a `PostgresDialect`: ```typescript new Pool({ host: config.host, port: config.port, database: config.database, user: config.user, password: config.password, ssl: config.ssl ? { rejectUnauthorized: false } : undefined, application_name: config.applicationName, max: config.poolSize, connectionTimeoutMillis: config.connectionTimeoutMillis, idleTimeoutMillis: config.idleTimeoutMillis, }); ``` The pool is instrumented with `instrumentPgPool(pool, config.applicationName ?? "reactor-host")` and exposed via `module.pools`. ### Migrations The migration strategy is set with `withMigrationStrategy`: ```typescript withMigrationStrategy(strategy: MigrationStrategy): this // MigrationStrategy = "auto" | "manual" | "none" ``` Default is `"auto"`. Only `"auto"` runs migrations during `buildModule()`. A migration failure throws `` `Database migration failed: ${error.message}` ``. For `"manual"` or `"none"`, run migrations yourself with the exported helpers: ```typescript const result = await runMigrations(db, REACTOR_SCHEMA); if (!result.success && result.error) { throw result.error; } ``` `runMigrations` returns `{ success, migrationsExecuted, error? }` and never throws on a migration error — it reports the error in the result. Use `getMigrationStatus(db, REACTOR_SCHEMA)` to inspect applied versus pending migrations without running them. ### Stored protocol versions After migrations, `buildModule()` reads the protocol versions (`header.protocolVersions`, such as `base-reducer` or `signature`) that stored documents were created with, per document type. If the reactor does not run one of them, for example after a rollback to an older build, or a registered model cannot run its stored version, such as a base-reducer 3 document whose model's reducer is not built by `createReducer`, it throws `UnsupportedStoredProtocolError` before any executor starts. The error's `versions` lists the unsupported `{ protocol, version, documentType? }` entries, with `documentType` set when only that model cannot run the version, and `documents` is how many stored documents need them. A type whose model is loaded after start is checked when it loads: a refusal is logged and emitted as the `STORED_MODEL_REFUSED` event, and its documents stay read-only. ```typescript withUnsupportedStoredDocuments(mode: "refuse" | "read-only"): this ``` `"refuse"` is the default. `"read-only"` builds anyway and logs a warning. Every job into those documents, and every operation received for them, is then refused. The check probes an index once per distinct pair of document type and creation versions rather than reading every document. | Host | Setting | | ----------- | ----------------------------------------------------------------------------- | | Switchboard | `REACTOR_UNSUPPORTED_STORED_DOCUMENTS=refuse` (default) or `read-only` | | Connect | `connect.reactor.unsupportedStoredDocuments` in `powerhouse.config.json` | | Library | `ReactorBuilder.withUnsupportedStoredDocuments()` | A refused switchboard logs the versions, the document count and both ways forward, then exits with code 1. A refused Connect shows the same on its boot screen in place of the app. ## Scaling with a worker pool The executor worker pool moves job execution out of the main thread into N `node:worker_threads` workers. Each worker opens its own Postgres pool and runs document models in isolation. Jobs route to a worker stickily by document id, so all work for one document lands on the same worker. Enable it with `withWorkerPool` — calling it enables the pool; there is no `enabled` flag — and register document models as **importable sources** (`{ filePath }` or `{ packageName }` — workers re-import them; a live module cannot cross the thread boundary): ```typescript const reactor = await new ReactorBuilder() .withDocumentModelSources([ { packageName: "@my-org/account-document-model", subpath: "document-models" }, ]) .withWorkerPool({ numWorkers: 4, db: { host: "localhost", port: 5432, database: "reactor", user: "reactor", password: process.env.PGPASSWORD!, }, }) .build(); ``` ### Config shapes `WorkerPoolOptions`: ```typescript type WorkerPoolOptions = | { numWorkers: number; db: DbConfig; factory?: WorkerFactory } | { numWorkers: number; factory: WorkerFactory; db?: DbConfig }; ``` `numWorkers` is the number of workers the manager spawns at `start()` and the modulus used for sticky routing. Either `db` (the default thread transport; each worker opens its own Postgres pool) or a custom `factory` is required **by construction** — an enabled pool without connection info is unrepresentable rather than a runtime error. `DbConfig`: ```typescript type DbConfig = { host: string; port: number; database: string; user: string; password: string; ssl?: boolean; applicationName?: string; poolSize?: number; connectionTimeoutMillis?: number; // pg defaults to 0 (unlimited wait) if omitted idleTimeoutMillis?: number; // pg defaults to 10000 if omitted }; ``` `DbConfig` is sent across worker IPC, so it must be JSON-clonable. `ssl: true` maps to `{ rejectUnauthorized: false }`; `poolSize` maps to pg's `max`; `applicationName` maps to `application_name`. Workers verify signatures the same way the in-process executor does, with the same `signatureVerification` mode from `withExecutorConfig`. A refusal inside a worker is re-emitted on the host event bus. See [Signature Verification](/academy/Build/BuildingUserExperiences/Authorization/Signing#signature-verification). ### How sources resolve `buildModule()` resolves every registered source in one pass: file and package sources are imported host-side and their exports scanned for `DocumentModelModule` values; the resulting modules are registered on the host registry (identically in both executor modes), and the importable sources form the manifest each worker imports at boot. The host and workers are derived from the same list, so they cannot diverge. ### Build-time constraints that throw When a worker pool is configured, `buildModule()` enforces the model wiring up front. Each of these throws: - No importable sources: `"withWorkerPool requires at least one worker-importable document-model source ({ filePath } or { packageName })."` - A model registered only as a live module: `"withWorkerPool requires worker-importable sources, but these models were registered only as live modules: ..."` — provide a `{ filePath }` or `{ packageName }` source for each (a live module for the same `documentType@version` may coexist; the importable source covers it). Connection info is not a runtime check: the `WorkerPoolOptions` type requires `db` or a custom `factory`, so a misconfigured pool fails at compile time. The worker pool requires a real Postgres server. PGlite cannot be shared across threads, so the parent and the workers must point at the same Postgres instance. When the pool carries a `db`, the builder builds the parent's database from that same config (unless `withKysely` overrides it). ### Custom transports Passing `factory` in the pool options injects a custom `WorkerFactory`, skipping the default thread-transport wiring: ```typescript type WorkerFactory = (index: number) => IExecutorWorker; ``` Use this for tests or an alternative transport (e.g. a child-process adapter); `db` is then optional, since the builder is not constructing the default transport. ## Projection shards Projection shards move read-model indexing out of the main thread. Instead of the in-process read-model coordinator, the builder installs a shard manager that fans `JOB_WRITE_READY` events to N projection workers, sharded by document id. Each worker materializes the built-in read models against its own Postgres pool and re-emits `JOB_READ_READY` and read-model events back to the host bus, so [synchronization](/academy/Reference/Reactor/Synchronization), awaiters, and observers see them exactly once per job. Configure shards with `withProjectionShards`. Projection workers open their own Postgres pools from the config's `db`; when the executor worker pool is also configured with a `db`, it may be omitted here and is reused. ```typescript const reactor = await new ReactorBuilder() .withDocumentModelSources([ { packageName: "@my-org/account-document-model", subpath: "document-models" }, ]) .withProjectionShards({ db: { host: "localhost", port: 5432, database: "reactor", user: "reactor", password: process.env.PGPASSWORD!, }, shardCount: 4, preReadyKinds: ["document-view"], postReadyKinds: ["document-indexer"], }) .build(); ``` ### Config shape `ProjectionShardBuilderConfig`: ```typescript type ProjectionShardBuilderConfig = { shardCount: number; preReadyKinds: BuiltInReadModelKind[]; postReadyKinds: BuiltInReadModelKind[]; poolSize?: number; initTimeoutMs?: number; shutdownGraceMs?: number; drainTimeoutMs?: number; chainDepthReportIntervalMs?: number; }; // BuiltInReadModelKind = "document-view" | "document-indexer" ``` `preReadyKinds` run before a job reaches `READ_READY`; `postReadyKinds` run after. Between them they must name each `BuiltInReadModelKind` exactly once: under sharding the host's own copies of the built-in read models never index an operation, so a kind named in neither list is indexed by nobody — its reads stay stale and any read carrying a consistency token for it waits forever — while a kind named in both is indexed twice per operation. `poolSize` overrides the reused worker DB pool size for the shard pools only; their `application_name` is `db.applicationName` when set, otherwise `"reactor-projection-shard"`. Defaults applied when the optional fields are omitted: `initTimeoutMs` 30000, `shutdownGraceMs` 5000, `drainTimeoutMs` 30000, `chainDepthReportIntervalMs` 250. `initTimeoutMs` bounds only a worker that never answers. A worker whose `init` throws reports the failure instead: `build()` rejects with that error — name, message, and stack intact — and the worker thread is terminated, rather than the build waiting out the timeout and reporting it as the cause. ### Build-time constraints that throw - `withProjectionShards` without a `db` anywhere: `"withProjectionShards requires a db (or an executor worker pool configured with one); projection workers need connection info to open their own pools."` - `shardCount < 1`: `` `ProjectionShardManager: shardCount must be >= 1 (got ${shardCount})` ``. - `preReadyKinds` and `postReadyKinds` that do not name each built-in read model exactly once: `"withProjectionShards requires preReadyKinds and postReadyKinds to name each built-in read model (document-view, document-indexer) exactly once between them; ..."`, with the missing and duplicated kinds listed. - `withReadModel` or `withReadModelFactory` alongside `withProjectionShards`: the workers build their own read models from the shard config, so host-registered instances and factories would be silently omitted. Rejected rather than dropped. - A projection worker `db` that is not the parent reactor's database, when the parent is not `withKysely`-provided: either nothing configures the parent (it would fall back to the default embedded database) or the two name different Postgres hosts, ports, or databases. The parent serves reads from the tables the worker writes, so a split would leave every built-in read model empty with nothing raised. - `withProjectionShards` alongside `withReadModelCoordinatorFactory`: two ways of owning the coordinator. Use one; the factory reaches the projection worker through its `createProjectionShardManager` dependency (see below). - A document model registered only as a live module when a projection worker is configured: the worker rebuilds its registry from the importable manifest, so a live-module-only model would be missing from it. Provide a `{ filePath }` or `{ packageName }` source for each. Shards can run on their own or alongside the worker pool. They replace the read-model coordinator wholesale; the executor side is independent. Because the replacement is wholesale, the reactor's own subscription-notification read model and processor manager — built unconditionally by `buildModule()` — are never fed an operation under bare `withProjectionShards`: GraphQL subscriptions do not fire and package-installed processors see nothing. Hosts that rely on either, or that register their own read models, use the hybrid coordinator instead. As with the worker pool, projection shards require real Postgres — the workers open their own pools and PGlite cannot be shared across threads. ### Hybrid projection worker `withReadModelCoordinatorFactory` hands the coordinator's construction to a factory that runs after the reactor's internal read models exist. `createHybridProjectionCoordinatorFactory` is the shipped factory: one projection worker owns `document-view` and `document-indexer`, and the host keeps everything else, in the same order the in-process coordinator runs today — host pre-ready read models (`withReadModel`, `withReadModelFactory`), then `JOB_READ_READY` on the host bus, then subscription notifications and the processor manager post-ready. Reads carrying a consistency token stay correct because the host's trackers advance from the worker's indexing reports. ```typescript ReactorBuilder, createHybridProjectionCoordinatorFactory, } from "@powerhousedao/reactor"; const reactor = await new ReactorBuilder() .withKysely(postgresKysely) .withDocumentModelSources([ { packageName: "@my-org/account-document-model", subpath: "document-models" }, ]) .withReadModelFactory(async (deps) => buildMyReadModel(deps)) .withReadModelCoordinatorFactory( createHybridProjectionCoordinatorFactory({ db: { host: "localhost", port: 5432, database: "reactor", user: "reactor", password: process.env.PGPASSWORD!, applicationName: "my-host-projection", }, poolSize: 8, onFatal: (shardId, reason) => { logger.error(`projection worker ${shardId} died`, reason); process.kill(process.pid, "SIGTERM"); }, }), ) .build(); ``` ```typescript type HybridProjectionOptions = { shardCount?: number; // default 1 poolSize?: number; db?: DbConfig; // falls back to the worker pool's db onFatal?: (shardId: string, reason: Error) => void; initTimeoutMs?: number; shutdownGraceMs?: number; drainTimeoutMs?: number; chainDepthReportIntervalMs?: number; }; ``` The parent database must be the same Postgres the worker reads, and this is enforced: the builder cannot derive the parent from a `db` that reaches it through the factory, because the parent database is already open by the time the factory runs. Pass the parent connection with `withKysely`, or configure `withWorkerPool` with the same `db`; a `db` with no parent configured at all throws rather than leaving the parent on the default embedded database while the worker projects into Postgres. `onFatal` fires when the worker errors or exits after it was ready, and for every write it then has to drop; nothing respawns the worker, so a host that cares should restart the process. The hook may fire repeatedly, so latch it. `withReadModelCoordinatorFactory` is mutually exclusive with `withReadModelCoordinator` and with `withProjectionShards`. Each job produces two `READMODEL_BATCH_COMPLETED` events under this coordinator, because two chains ran: the worker relays its own, covering the built-in read models, and the host reports the chain it ran afterwards. Stage durations therefore stay attributable to the side that spent them — a merged event could not tell you whether the flag moved work off the host loop — while `READMODEL_INDEXED` continues to name one read model per event on both sides. Switchboard exposes this as `REACTOR_PROJECTION_WORKER=1` with `REACTOR_DB_POOL_SIZE_PROJECTION` (default 8) for the worker's pool. It requires a Postgres reactor database and is rejected in dev mode, like the executor worker pool. `withProjectionWorkerFactory` injects a custom `ProjectionWorkerFactory` and skips the default thread-transport wiring, mirroring the pool options' `factory`: ```typescript type ProjectionWorkerFactory = (shardIndex: number, shardId: string) => IProjectionTransport; ``` **WARNING:** Projection shards and the hybrid projection worker are recent, design-doc-tracked features. The shard-manager class itself is internal; you configure it through `withProjectionShards` or `createHybridProjectionCoordinatorFactory` and the public `ProjectionShardBuilderConfig`, `HybridProjectionOptions`, `BuiltInReadModelKind`, `ProjectionWorkerFactory`, and `IProjectionTransport` types. ## Decision guide - **In-memory PGlite (default).** Dev and tests. Zero setup, ephemeral, single process. Use it unless you need one of the below. - **Postgres (`withKysely` or the worker pool's `db`).** When state must survive a restart or be shared across processes. - **Worker pool (`withWorkerPool`).** When job execution is CPU-bound and one thread is the bottleneck. Requires Postgres and importable document-model sources. - **Hybrid projection worker (`createHybridProjectionCoordinatorFactory`).** When the host event loop is saturated by indexing the built-in read models and you still need subscriptions, processors, or host read models. Requires Postgres. One worker; the host keeps everything else. - **Projection shards (`withProjectionShards`).** When only the built-in read models matter and you want N workers sharded by document id. Requires Postgres. Disables subscriptions and processors. Combine with the worker pool when both write and read paths need to scale. All three scaling paths require real Postgres and trade simplicity for throughput. Start in-memory, move to Postgres for persistence, and reach for the worker pool, the hybrid projection worker, or shards only when a single thread is the proven bottleneck. For the full list of builder methods, see the builder table on [Advanced Reactor Usage](/academy/Reference/Reactor/AdvancedReactorUsage). For the read-model and processor side of indexing, see [Processors](/academy/Reference/Reactor/Processors) and [Document model registry](/academy/Reference/Reactor/DocumentModelRegistry). For the client surface that talks to a built reactor, see [IReactorClient](/academy/Reference/Reactor/ReactorClient). --- ## Error handling > Source: https://academy.vetra.io/academy/Reference/Reactor/ErrorHandling Errors reach your code from the reactor through three channels: - **Failed writes reject with `JobFailedError`.** When a job fails, the `WriteHandle` that a [`client.write`](/academy/Reference/Reactor/ReactorClient) method such as `execute`, `create`, or `deleteDocument` returned rejects its stages with `JobFailedError`, carrying the job's message and its `JobInfo`. The original error class is not preserved at this boundary. - **Aborts throw an `AbortError`.** Any call that takes an `AbortSignal` rejects with an error whose `name` is `"AbortError"` when the signal fires. - **Subsystems export typed errors.** The registry, sync, and attachment layers throw named error classes you can discriminate with `instanceof` or a `name` check. This page is the consolidated error reference for the reactor. For attachment errors see [Attachment service](/academy/Reference/Reactor/AttachmentService); for sync-channel behavior see [Synchronization](/academy/Reference/Reactor/Synchronization). **INFO:** Importable error values come from `@powerhousedao/reactor`: `EventBusAggregateError`, `ModuleNotFoundError`, `DuplicateModuleError`, `InvalidModuleError`, `DuplicateManifestError`, `ChannelError`, `ChannelErrorSource`, `SyncOperationAggregateError`, `PollingChannelError`, `RetractionRefusedError`, `SubmittedUndoRefusedError`, `UndoOperationRefusedError`, `UnsupportedProtocolVersionError`, `ReservedActionError`, `isDriveAuthError`, and `DRIVE_AUTH_ERROR_MESSAGES`. `@powerhousedao/reactor-browser` re-exports only `isDriveAuthError`. Attachment errors come from `@powerhousedao/reactor-attachments`. Several errors are internal and cannot be imported; this page marks each one. ## Job failures A job moves through `PENDING -> RUNNING -> WRITE_READY -> READ_READY`, or transitions to `FAILED`. Only `READ_READY` and `FAILED` are terminal. The failing error rides on the job's `JobInfo`. ### `JobInfo.error` and `errorHistory` ```typescript type ErrorInfo = { name: string; // the failing error's class name message: string; stack: string; }; type JobInfo = { id: string; documentId: string; // empty string when the job is unknown status: JobStatus; createdAtUtcIso: string; completedAtUtcIso?: string; error?: ErrorInfo; // the failing error errorHistory?: ErrorInfo[]; // one entry per attempt, ordered result?: any; deletedDocument?: boolean; // set once the job has deleted its document consistencyToken: ConsistencyToken; meta: JobMeta; job?: Job; // full job object, populated on failure for debugging }; ``` `JobInfo` and `JobStatus` are exported from `@powerhousedao/reactor`. The `ErrorInfo` shape above is not exported under that name. The barrel re-exports a different `ErrorInfo` (the worker protocol's) aliased as `WorkerErrorInfo`. Treat the `{ message, stack }` on `JobInfo.error` as a structural type you match against, not one you import. `errorHistory` holds one `ErrorInfo` per attempt, oldest first. A job that the queue retried carries every failure; `error` is the most recent. On failure the reactor also populates `job` with the full job object for debugging. ### What a write handle rejects with [`client.write`](/academy/Reference/Reactor/ReactorClient#write-methods-clientwrite) methods resolve once the job is accepted and return a `WriteHandle`. When the job reaches `FAILED`, the handle's `written` and `readable` stages, and `document()`, reject with `JobFailedError`, exported from `@powerhousedao/reactor`: - `message` is `job.error?.message`, which can be `undefined` if `error` was never set. Guard for that when you display it. - `job` is the failed `JobInfo`, so `job.error`, `job.errorHistory`, and `job.job` are on hand. A reducer error is not a job failure. The operation is stored with its error, the job reaches `READ_READY`, and `receipt.job.result` says which actions erred. `executeBatch` is the exception, below. ```typescript try { const handle = await client.write.execute(documentId, "main", actions); await handle.readable; } catch (err) { if (err instanceof JobFailedError) { console.error("mutation failed:", err.message, err.job.errorHistory); } else { throw err; } } ``` Several internal write-path errors surface only as the `.message` string and lose their typed identity at this edge: `DocumentDeletedError`, `DocumentNotFoundError`, `CreateDocumentRequiredError`, `InvalidSignatureError`, and `UpgradeManifestNotFoundError`. None are exported. Match on `err.message` if you need to branch on them, and expect that to be brittle. The refusals of undo and protocol versions are terminal, so the job fails without a retry. Each arrives as a `JobFailedError` whose `job.error.name` names the class: - `RetractionRefusedError`: a `RETRACT`'s target was already retracted or is gone. An undo does not resolve again. - `SubmittedUndoRefusedError`: an `UNDO` or `REDO` was submitted to a base-reducer 3 document. Use [`write.undo`](/academy/Reference/Reactor/ReactorClient#undo) and [`write.redo`](/academy/Reference/Reactor/ReactorClient#redo), which submit a signed `RETRACT`. - `UndoOperationRefusedError`: a peer's load carries an `UNDO` operation into a base-reducer 1 or 2 document. Those versions store an undo as a `NOOP`, so only a submitted `UNDO` is valid there. - `UnsupportedProtocolVersionError`: this reactor cannot run the document's protocol versions, for example a base-reducer 3 document whose model's reducer was not built by `createReducer`. - `ReservedActionError`: the action type is reserved for the reactor. `write.undo` and `write.redo` throw `NoUndoRedoTargetError` (from `document-model`) before submitting anything when the scope has nothing to undo or redo. See [Undo and redo](/academy/Reference/Reactor/UndoRedo#errors). #### `executeBatch` rejects with `BatchJobFailedError` `write.executeBatch` resolves once every job is accepted and returns a `BatchWriteHandle`. Its `written` and `readable` stages wait for every job in the batch. If any job is `FAILED`, they reject with a `BatchJobFailedError` (exported from `@powerhousedao/reactor`) for the first failed job in plan order: | Field | Value | | ------------ | ------------------------------------------------------------------------------------------- | | `message` | The failed job's `error.message`. | | `key` | The plan key of the failed job. | | `jobs` | Every job's final `JobInfo`, keyed by plan key, including the jobs that committed. | | `cause.name` | The failed job's own error name, such as `DocumentAlreadyExistsError`. | A batch is not atomic: each job commits on its own, and a failed job still releases the jobs that depend on it. Read `jobs` to see which jobs committed before you retry, since re-submitting re-applies them. ```typescript try { const batch = await client.write.executeBatch({ jobs }); await batch.readable; } catch (err) { if (BatchJobFailedError.isError(err)) { const committed = Object.keys(err.jobs).filter( (key) => err.jobs[key].status === JobStatus.READ_READY, ); console.error(`job ${err.key} failed:`, (err.cause as Error).name); console.error("committed:", committed); } } ``` Across the SharedWorker RPC boundary an error is rebuilt from `name`, `message`, `stack` and `cause` only, so `key` and `jobs` do not survive and `BatchJobFailedError.isError` returns `false` there. Classify by the names in the `cause` chain instead: the failed job's error name is still on `cause.name`. ## Cancellation Every `IReactor` method that accepts an `AbortSignal` rejects when the signal is already aborted or aborts mid-flight. The reactor throws `AbortError`: ```typescript export class AbortError extends Error { constructor(message?: string) { super(message || "Aborted"); this.name = "AbortError"; } } export const isAbortError = (error: unknown): boolean => { return error instanceof AbortError; }; ``` `AbortError` and `isAbortError` live in `core/types.ts` but are **not exported** from the package barrel. You cannot import either one. Because the class is unreachable, `isAbortError` (and any `instanceof AbortError` you write against the imported class) is not available to you. Match on the `name` instead, which the DOM `AbortController` also uses: ```typescript const controller = new AbortController(); try { const doc = await client.get(documentId, undefined, controller.signal); } catch (err) { if (err instanceof Error && err.name === "AbortError") { // the read was cancelled return; } throw err; } ``` `isAbortError` relies on `instanceof`, so even where it is reachable it does not survive a worker or realm boundary. The `name` check survives both and is the reliable test. Note also that `ReactorClient.deleteDocument` throws a plain `new Error("Operation aborted")` (not `AbortError`) when its signal fires during cascade traversal, so prefer `name`-agnostic handling around that call. ## Event bus `IEventBus.emit()` runs subscribers sequentially with `await` over a snapshot, collects any thrown errors, and rejects with one aggregate at the end. ```typescript export class EventBusAggregateError extends Error { public readonly errors: any[]; // message: "EventBus emit failed with N error(s): ; ; ..." } ``` `EventBusAggregateError.errors` holds the individual subscriber errors. Discriminate it, then iterate: ```typescript try { await eventBus.emit(eventType, payload); } catch (err) { if (err instanceof EventBusAggregateError) { for (const subscriberError of err.errors) { console.error(subscriberError); } } } ``` `EventBusAggregateError` is exported from `@powerhousedao/reactor`. It is not re-exported from `@powerhousedao/reactor-browser`. ## Subsystem errors Each subsystem defines its own error classes. Classes with a `static isError` helper are name-based and safe across worker and realm boundaries; the rest must be matched with `instanceof` or a manual `err.name` check. ### Registry and manifest errors Thrown when registering, resolving, or upgrading document model modules. Four are importable from `@powerhousedao/reactor`; the rest are internal. | Error | Thrown when | `static isError`? | Importable | | --- | --- | --- | --- | | `ModuleNotFoundError` | `getModule` / resolve finds no module for a type (and optional version) | Yes | `@powerhousedao/reactor` | | `DuplicateModuleError` | registering a module that already exists | Yes | `@powerhousedao/reactor` | | `InvalidModuleError` | a module is malformed | No | `@powerhousedao/reactor` | | `DuplicateManifestError` | re-registering an upgrade manifest | Yes | `@powerhousedao/reactor` | | `ManifestNotFoundError` | upgrade manifest lookup misses | No | internal | | `MissingUpgradeTransitionError` | a gap in the upgrade chain | No | internal | | `InvalidUpgradeStepError` | `getUpgradeReducer` called with a non-single-step increment | No | internal | | `DowngradeNotSupportedError` | a downgrade is requested | (external) | internal | `ModuleNotFoundError` carries `documentType: string` and `requestedVersion: number | undefined`: ```typescript try { // ...register or resolve a document model module } catch (err) { if (ModuleNotFoundError.isError(err)) { console.error(`no module for ${err.documentType}`, err.requestedVersion); } } ``` `static isError` is the ES2024 pattern `Error.isError(err) && err.name === ""`. Use it for the four classes that have it. For `InvalidModuleError` and the internal upgrade errors, match on `err.name`. The four internal errors are reachable only from `@powerhousedao/reactor/.../src/registry/index.js`, not the package root; treat them as internal. See [Document model registry](/academy/Reference/Reactor/DocumentModelRegistry). ### Sync channel errors Thrown by the sync manager and channels. See [Synchronization](/academy/Reference/Reactor/Synchronization) for how these land on `SyncOperation.error` and dead-letter events. ```typescript enum ChannelErrorSource { None = "none", Channel = "channel", Inbox = "inbox", Outbox = "outbox", } class ChannelError extends Error { source: ChannelErrorSource; error: Error; // the wrapped underlying error // message: "ChannelError[]: " } ``` `ChannelError` wraps an underlying `Error` and tags it with a `source`. Discriminate with `instanceof`, then read `source` and `error`: ```typescript if (err instanceof ChannelError) { console.error(`sync ${err.source} failed:`, err.error.message); } ``` `SyncOperationAggregateError` collects the per-callback errors when a sync operation's status callbacks throw. Its `errors: Error[]` field holds them: ```typescript class SyncOperationAggregateError extends Error { errors: Error[]; // message: "SyncOperation callback failed with N error(s): ..." } ``` `ChannelError`, `ChannelErrorSource`, and `SyncOperationAggregateError` are all exported from `@powerhousedao/reactor`. `PollingChannelError` is exported from `@powerhousedao/reactor` but is never thrown anywhere in source. Treat it as dormant; do not write `catch` logic against it. #### Drive auth: `isDriveAuthError` Use `isDriveAuthError` to detect that a remote rejected the caller as unauthenticated or unauthorized. It returns `true` for an HTTP 401/403 or a Forbidden/Unauthorized GraphQL response, and `false` for everything else. ```typescript // also re-exported from "@powerhousedao/reactor-browser" try { await reactor.addRemoteDrive(url, options); } catch (err) { if (isDriveAuthError(err)) { // prompt the user to sign in / re-authenticate } } ``` `isDriveAuthError` is gated on the internal `GraphQLRequestError` class: it returns `false` for any error that isn't an instance of it. You cannot import or construct `GraphQLRequestError`, so the helper is only meaningful on errors that originated inside the reactor's GraphQL channel and propagated out unchanged. Inside the reactor it drives `ConnectionStateSnapshot.requiresAuth`. The exported message constant `DRIVE_AUTH_ERROR_MESSAGES` (`{ forbidden, authenticationRequired }`) is shared with `reactor-api` so server throw strings and the client check stay in sync. ### Attachment errors `@powerhousedao/reactor-attachments` exports eight error classes: `AttachmentNotFound`, `ReservationNotFound`, `InvalidAttachmentRef`, `UploadTooLarge` (maps to HTTP 413), `AttachmentAlreadyExists`, `HashMismatch`, `SizeMismatch`, and `AttachmentPending`. None have a `static isError`; discriminate with `instanceof` or `err.name`. `AttachmentPending` is deliberately not a subclass of `AttachmentNotFound`, so callers can distinguish "retry later" from "unknown attachment". For the full table of fields and when each is thrown, see [Attachment service](/academy/Reference/Reactor/AttachmentService). ## Detecting errors | Mechanism | Applies to | Cross-boundary safe | | --- | --- | --- | | `err.name === "AbortError"` | aborted reactor calls (DOM-compatible name) | Yes | | `X.isError(err)` (static) | `ModuleNotFoundError`, `DuplicateModuleError`, `DuplicateManifestError`, `RetractionRefusedError`, `ReservedActionError` | Yes (name-based) | | `err instanceof X` | `EventBusAggregateError`, `ChannelError`, `SyncOperationAggregateError`, attachment errors | No | | `err.name === "X"` | any class above (all set `this.name`) | Yes | | `isDriveAuthError(err)` | un-rewrapped GraphQL-channel errors only | only in-channel | Prefer a `name` check over `instanceof` when the error may cross a worker or realm boundary, since `instanceof` fails when the class identity differs between contexts. Every reactor error sets `this.name`, so the `name` check is always available. For building on these APIs, see [Working with the Reactor](/academy/Reference/Reactor/WorkingWithTheReactor), [IReactorClient](/academy/Reference/Reactor/ReactorClient), and [Processors](/academy/Reference/Reactor/Processors). --- ## Attachment service > Source: https://academy.vetra.io/academy/Reference/Reactor/AttachmentService The `@powerhousedao/reactor-attachments` package decouples large binaries (images, files, avatars) from the action and operation pipeline. Instead of carrying base64-encoded `data` strings inside actions, document state holds opaque refs of the form `attachment://v1:`, and the bytes flow through a dedicated `IAttachmentService`. Storage is content-addressed, so identical uploads dedupe to the same ref. ```typescript type IAttachmentService, createRemoteAttachmentService, } from "@powerhousedao/reactor-attachments"; ``` `AttachmentRef` and `AttachmentHash` are defined in `@powerhousedao/reactor`; reactor-attachments re-exports neither. Of the ref-related symbols, only the `InvalidAttachmentRef` error class lives in reactor-attachments. The service and client symbols come from `@powerhousedao/reactor-attachments` and `@powerhousedao/reactor-attachments/client`. The package exposes two layers. `IAttachmentService` is the low-level, transport-facing interface — reserve a slot, stream the bytes, read them back by ref. `IAttachmentClient` wraps a service with convenience helpers; most notably `preprocess()`, which hashes a file up front so its ref is known before the bytes are uploaded. Build a client with `createAttachmentClient(service)`, and import it from the `/client` entrypoint: ```typescript createAttachmentClient, type IAttachmentClient, type PreprocessResult, } from "@powerhousedao/reactor-attachments/client"; ``` The React hooks in `@powerhousedao/reactor-browser` are built on the client. For an architectural overview see [Working with the Reactor](/academy/Reference/Reactor/WorkingWithTheReactor). For the full design — storage layout, eviction, transports, the hash-first/pending protocol, and the planned S3 backend — see the deep-dive docs `packages/reactor/docs/attachments.md` and `packages/reactor/docs/attachments-hash-first.md` in the monorepo. ## When to use it Reach for the attachment service whenever a field would otherwise be a base64 `data` string or a long data URI. Typical cases: - File or image uploads from an editor (chat input bars, document attachments, exports). - Agent or user avatars carried alongside document state. - Any binary that must round-trip through sync or GraphQL, where inlining the bytes would bloat operations and sync channels. If the field is small, fixed-length, and not really binary (a short string, a number, a JSON blob), keep it inline — attachments are designed for arbitrary-sized binary content. ## Getting an `IAttachmentService` instance Code that runs outside the switchboard (a browser app, a script, another service) uses the switchboard-backed remote service. It targets a running switchboard's REST endpoints for reservations, uploads, and downloads: ```typescript const attachments = createRemoteAttachmentService({ remoteUrl: "https://switchboard.example.com", // Optional: provide a JWT handler if the switchboard requires auth. // jwtHandler, // Optional: inject a custom fetch (defaults to globalThis.fetch). // fetchFn, }); ``` `JwtHandler` is `(url: string) => Promise`, defined in `@powerhousedao/reactor`. **TIP:** The switchboard remote service is the supported client implementation for code outside the switchboard. Other transports (S3, peer-to-peer) are designed but not yet stable. The package root also exports the pieces for a local, embedded service: `AttachmentBuilder` (fluent), `KyselyAttachmentStore`, `KyselyReservationStore`, `runAttachmentMigrations`, `ATTACHMENT_SCHEMA`, `DEFAULT_RESERVATION_TTL_MS`, `DirectAttachmentUpload`/`DirectAttachmentUploadFactory`, and `NullAttachmentTransport`. These are on the root entrypoint only, not `/client`. Assemble one with `AttachmentBuilder` if you need an in-process store of your own. ### Inside the switchboard Processors and subgraphs do not build a client. `@powerhousedao/reactor-api` builds one in-process attachment service per host and hands out [attachment clients](#the-attachment-client) over it. **Processors** get a client on the host module as `module.attachments`. It is trusted and makes no caller check, because a processor acts for no caller. **Subgraphs** call `attachmentsFor(ctx)` in a resolver: `subgraph.attachmentsFor(ctx)` in the generated `getResolvers(subgraph)`, `this.attachmentsFor(ctx)` in a class-field resolver. It returns an `IAttachmentClient` bound to the request's caller, `ctx.user`. Calls within one request get the same client. ```typescript export const getResolvers = (subgraph: BaseSubgraph) => ({ Query: { attachmentText: async ( _parent: unknown, args: { documentId: string; ref: AttachmentRef }, ctx: Context, ) => { const attachments = subgraph.attachmentsFor(ctx); const { body } = await attachments.download({ documentId: args.documentId, ref: args.ref, }); return new Response(body).text(); }, }, }); ``` The client is the authorization check, so the resolver needs no `assertCanRead` before it: - **Reads** are decided as the caller, through the document named by `documentId` (an id or a slug). The caller must be able to read that document, and the document's operations must reference the ref. This is the decision the HTTP download route makes. A refusal, an unknown ref, and a missing `documentId` all throw `AttachmentNotFound`, so a refusal does not reveal that a hash exists. There is no supreme-admin bypass: an admin also needs a document that references the ref. - **Anonymous reads** are decided by the document, unless the host sets `REQUIRE_AUTHENTICATED_CALLER`. Then they throw `AuthenticationRequiredError`. - **Uploads** need an authenticated caller when `AUTH_ENABLED` or `REQUIRE_AUTHENTICATED_CALLER` is on. An anonymous upload then throws `AuthenticationRequiredError`. An upload touches no document. Writing the ref into a document is a separate action, authorized when it is dispatched. - Uploads are hash-first only, through `upload`, `uploadMany`, or `preprocess` plus `reserve`. `mimeType`, `fileName`, and `extension` are validated with the rules the HTTP reserve route applies; a value that fails throws `InvalidAttachmentMetadata` from `@powerhousedao/reactor-attachments`, whose `field` names it. - `getShareLink` fails on this client. Only the remote service can mint a download target. Use `download` or `downloadBlob`. Two more errors come from `@powerhousedao/reactor-api`. `AttachmentAccessUnavailable` means the host does not maintain the [reference index](#the-attachment-reference-index), so no read can be decided. `AttachmentAccessFailed` means the access decision itself threw. The cause is logged on the server and kept as the error's `cause`; its message does not reach the GraphQL client. `attachmentsFor` throws when the host gave the subgraph no attachments. The `GraphQLManager` passes them to every subgraph it registers. A host that constructs a subgraph itself passes `api.attachmentClientProvider`, an `IAttachmentClientProvider`, as `attachments` in its `SubgraphArgs`. ## The general flow Every interaction with the package comes down to three operations: reserve a slot, send the bytes, then later read them back by ref. There are two reservation modes. They are equivalent in capability — neither is preferred, so pick whichever fits the caller: - **Upload-first** — reserve with just `{ mimeType, fileName }`. The content hash, and therefore the final ref, is only known _after_ `send()` completes. Simplest when you can stream the bytes straight through and do not need the ref until the upload finishes. - **Hash-first** — hash the content up front and reserve with `{ clientHash, sizeBytes, ... }`. The ref is known at reservation time, so it can be written into document state before (or in parallel with) the upload, and dedup can short-circuit before any bytes are sent. The trade-off is that the whole file must be read to hash it. `IAttachmentClient.preprocess()` does that hashing for you. The walkthrough below uses the low-level service in upload-first mode. The [attachment client](#the-attachment-client) section shows the hash-first flow. ### 1. Reserve an upload slot `reserve()` returns an `IAttachmentUpload` handle. The handle hides the transport — the caller never sees URLs, headers, or auth tokens. ```typescript const upload = await attachments.reserve({ mimeType: "image/png", fileName: "avatar.png", }); ``` The handle is valid until its reservation expires (default 24h). Aborted uploads are cleaned up by a periodic sweep on the server; clients do not need to release reservations manually. ### 2. Send the bytes `upload.send()` takes a `Blob` or a `ReadableStream` and resolves to `{ hash, ref, header }`. Identical bytes always produce the same hash and ref — dedup is automatic. In a browser, pass the `File` itself: it goes out as the request body and the browser streams it from disk, so a file of any size uploads in bounded memory. A `ReadableStream` sent to a remote service is first collected into memory, because browsers do not portably accept a stream as a request body. Browser upload: ```typescript const file = input.files[0]; // a File is a Blob const upload = await attachments.reserve({ mimeType: file.type, fileName: file.name, }); const { ref } = await upload.send(file); ``` Server upload from a data URI or remote URL: ```typescript async function resolveImageSource(image: string) { const dataUriMatch = image.match(/^data:([^;]+);base64,/); if (dataUriMatch) { const mimeType = dataUriMatch[1]; const bytes = Buffer.from(image.slice(dataUriMatch[0].length), "base64"); return { mimeType, stream: new Blob([bytes]).stream() }; } const res = await fetch(image); if (!res.ok || !res.body) throw new Error(`Failed to fetch ${image}`); return { mimeType: res.headers.get("content-type") ?? "application/octet-stream", stream: res.body, }; } const { mimeType, stream } = await resolveImageSource(agent.image); const upload = await attachments.reserve({ mimeType, fileName: `${agent.id}-avatar`, }); const { ref } = await upload.send(stream); ``` ### 3. Use and read the ref Store `result.ref` in document state through a normal action — there are no special "attach" actions. The reducer treats the ref like any other string field. ```typescript await reactor.write.execute(documentId, "main", [ setAgentImage({ attachment: ref }), ]); ``` Later, anywhere the bytes are needed, call `service.get(ref, { documentId })` and stream the body. `documentId` names the document whose operations reference the attachment; a remote service is authorized through it and refuses a read without one. In the browser, drain the stream into a `Blob` and hand it to `URL.createObjectURL` for an `` or ``: ```typescript const response = await attachments.get(ref, { documentId }); const chunks: Uint8Array[] = []; const reader = response.body.getReader(); for (;;) { const { done, value } = await reader.read(); if (done) break; chunks.push(value); } const blob = new Blob(chunks as BlobPart[], { type: response.header.mimeType }); const objectUrl = URL.createObjectURL(blob); // ...later: URL.revokeObjectURL(objectUrl); ``` `get()` succeeds for any ref whose bytes are available. If the bytes were evicted from local storage, the service transparently re-fetches them through the transport. A hash-first ref can also be _pending_ — reserved, with state already referencing it, but the bytes not yet uploaded. During that window `get()` throws `AttachmentPending`, while `stat()` succeeds and returns a header with `status: "pending"`, the declared `sizeBytes`, and `expiresAtUtc` set to the reservation expiry. ## The attachment client `IAttachmentClient` wraps an `IAttachmentService` with the hash-first flow. Build one with `createAttachmentClient(service)`. Processors and subgraphs receive one from the host; see [Inside the switchboard](#inside-the-switchboard). `preprocess(file)` reads the whole file, computes its SHA-256, and returns everything you need to both reference and upload it: ```typescript const attachments = createAttachmentClient(service); const results = await attachments.preprocess(file); // results: { ref, hash, sizeBytes, options, data, stream, blob } ``` `preprocess` is async because it reads the entire file to hash it. In Node it hashes natively in 8 MiB slices at any size. In a browser, a file up to 256 MiB is read whole into one native WebCrypto call; a larger one is read in 8 MiB slices and hashed in JavaScript, which is slower but keeps memory bounded and works past the browser's ~2 GB limit on a single read. No copy is kept: `results.blob` is the file itself. `data` and `stream()` are deprecated; they read `blob` again as a stream. The file must not change before it is uploaded: the server rejects bytes that no longer match the hash with `HashMismatch`. With the ref in hand you can write it into document state through a normal action, then upload the bytes: ```typescript await client.write.execute(documentId, "main", [ attachInvoice({ scan: results.ref, vendorName: "Acme" }), ]); await attachments.reserve(results.options, (handle) => handle.send(results.blob), ); ``` `client.reserve(options, send)` reserves the slot and hands the upload handle to your `send` callback. If an attachment with the same hash already exists it short-circuits and returns the existing ref without uploading. Pass `results.blob`: it can be sent again if the upload is retried, and a browser streams it from disk. A stream sent to a remote service is collected into memory first. `send()` also accepts any `ReadableStream`, so on the server you can stream from the original source. Execute and reserve are awaited separately so each failure is handled on its own — a failed upload does not roll back the action, and a failed action does not strand an upload. If you do not need that separation, run them concurrently: ```typescript await Promise.all([ client.write.execute(documentId, "main", [ attachInvoice({ scan: results.ref, vendorName: "Acme" }), ]), attachments.reserve(results.options, (handle) => handle.send(results.blob), ), ]); ``` ### React `@powerhousedao/reactor-browser` exposes hooks built on the client. `useAttachments()` returns an `IAttachmentClient` for the current service (or `undefined` if none is configured) — one client per service, shared across every consumer. `useAttachmentUpload()` drives the full preprocess + upload lifecycle with byte-level progress: ```tsx const client = useReactorClient(); const { preprocess, upload, cancel, reset, stage, progress, result, error } = useAttachmentUpload(); const submit = useCallback(async () => { // Hashes the file and hands back the ref. Async because it has to read the // whole file to hash it. const results = await preprocess(file); // Execute with the generated ref. `scan` is just a string here; the action // and codegen never see the file or the attachments package. await client.write.execute(documentId, "main", [ attachInvoice({ scan: results.ref, vendorName: "Acme" }), ]); // Upload the bytes. Awaited separately from execute() so each failure is // handled on its own. await upload(results); }, [client, preprocess, upload, documentId, file]); ``` In render, read the lifecycle straight off the hook: - `stage` — `"idle" | "hashing" | "reserving" | "uploading" | "done" | "error"` - `progress` — `{ percent, loaded, total, indeterminate }`. `percent` is 0-100, matching the design-system progress bar; `loaded`/`total` are raw bytes for a "3.2 / 10 MB" label. `indeterminate` is true while a stage is running whose bytes cannot be observed, and `percent` is 0 for the whole of it. - `result` — the `AttachmentUploadResult` once the upload is done - `error` — the upload failure, if any. Progress is left where it stopped rather than reset to zero. - `cancel()` — aborts the transfer in flight; the `upload` promise rejects with an `AbortError` - `reset()` — returns the hook to idle, discarding the previous result and error. A transfer still in flight is aborted, so it cannot keep streaming behind an idle UI `preprocess` and `upload` are stable callbacks. `upload(results)` calls `client.upload({ preprocessed: results })`, so progress, dedup reporting and cancellation are identical to the non-React path. The two-call split is deliberate: the ref must reach the document before the bytes are committed. The ref only exists after `preprocess` resolves, so submission is async — you cannot produce a ref inside the synchronous action input. Progress is byte-level where the transport can report bytes: - **Downloads** always report bytes, against `header.sizeBytes`. - **Uploads** report bytes only through an upload transport that can observe them. `fetch` cannot — it has no upload-side progress event — so the default transport reports the `uploading` stage as indeterminate and stays silent until the request resolves. Pass `uploadTransport: createXhrUploadTransport()` in the `createRemoteAttachmentService` config to get real upload progress in a browser; Connect does this already. - **Hashing** reports no byte progress: the stage stays indeterminate while `preprocess` reads the file. For a large file this is a visibly long phase. - **Deduplication** finishes an upload having sent nothing. The terminal event carries `deduped: true` with `loaded: 0, total: 0`, and the hook reports 100%. - **`upload.progress` measures bytes handed to the socket, not server acknowledgement.** For a small file you get one event at 100% immediately and then a wait for the response, so the stage stays `uploading` until `send()` resolves. `useAttachmentPreview()` reports the same shape for the download side, plus retry visibility — `stage`, `progress`, `attempt`, `maxAttempts`, and `lastError` (the failure of an attempt that is about to be retried, exposed while the hook is still `loading`). It aborts the download on unmount. ## Declaring attachment fields in a document model The integration surface between document models and the attachment system is a single GraphQL scalar: `AttachmentRef`. Declare attachment-bearing fields with it, and codegen will emit a branded TypeScript string (`` `attachment://v${number}:${string}` ``) plus a Zod validator that enforces the ref protocol. ```graphql scalar AttachmentRef input SetAgentImageInput { attachment: AttachmentRef! } type AgentInfo { attachment: AttachmentRef } ``` The reducer stores the value verbatim — no special handling needed: ```typescript setAgentImageOperation(state, action) { state.agent.attachment = action.input.attachment; // AttachmentRef } ``` ## API reference ### `IAttachmentService` | Method | Returns | Description | | ------------------- | ----------------------------- | -------------------------------------------------------------------------------------------------------- | | `reserve(options)` | `Promise` | Reserve a new attachment slot and return an upload handle. | | `stat(ref, { documentId }?)` | `Promise` | Look up metadata for an existing ref. Throws `AttachmentNotFound` if the ref is unknown or, on a remote service, when no `documentId` is given. | | `get(ref, { documentId, signal }?)` | `Promise` | Retrieve the bytes. Re-fetches transparently if the data was evicted. A remote service requires `documentId`. | ### `IAttachmentUpload` | Member | Type | Description | | --------------- | ----------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- | | `reservationId` | `string` | Unique identifier for this reservation. | | `ref` | `AttachmentRef \| null` | The ref this upload will produce. Set immediately in hash-first mode; `null` in upload-first until `send()` resolves. | | `expiresAtUtc` | `string` | ISO 8601 UTC reservation expiry (readonly). Use it to bound retry windows. | | `send(data)` | `(ReadableStream \| Blob) => Promise` | Stream the bytes through the handle. Returns `{ hash, ref, header }`. Dedup against existing hashes is automatic. | ### `IAttachmentClient` | Method | Returns | Description | | ------------------------- | --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `preprocess(file, opts?)` | `Promise` | Read and hash a `Blob` up front. Returns the ref, hash, declared size, hash-first reserve options, and the file itself as `blob`. `opts` may override `{ fileName, mimeType }`. | | `reserve(options, send)` | `Promise` | Reserve a hash-first slot and run `send(handle)`. Short-circuits to the existing ref if the hash is already stored (`AttachmentAlreadyExists` is handled internally). | ### Key types | Type | Shape | | ------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `AttachmentRef` | `` `attachment://v${number}:${string}` `` — opaque ref string used in document state. | | `ReserveAttachmentOptions` | `UploadFirstReserveAttachmentOptions \| HashFirstReserveAttachmentOptions` — discriminated on `clientHash`. | | `UploadFirstReserveAttachmentOptions` | `{ mimeType: string; fileName: string; extension?: string \| null }` — no `clientHash`; ref known only after `send()`. | | `HashFirstReserveAttachmentOptions` | `{ mimeType: string; fileName: string; extension?: string \| null; clientHash: AttachmentHash; sizeBytes: number }` — ref known at reserve time. | | `PreprocessResult` | `{ ref: AttachmentRef; hash: AttachmentHash; sizeBytes: number; options: HashFirstReserveAttachmentOptions; blob: Blob; data: ReadableStream; stream: () => ReadableStream }` — `data` and `stream` are deprecated aliases of `blob.stream()` | | `AttachmentUploadResult` | `{ hash: AttachmentHash; ref: AttachmentRef; header: AttachmentHeader }` | | `AttachmentStatus` | `"available" \| "evicted" \| "pending"` — `pending` is synthesized at query time from a live hash-first reservation; the bytes are not uploaded yet. | | `AttachmentHeader` | `{ hash; mimeType; fileName; sizeBytes; extension; status: AttachmentStatus; source; createdAtUtc; lastAccessedAtUtc; expiresAtUtc: string \| null }` — `expiresAtUtc` is `null` for committed attachments and carries the reservation expiry while `pending`. | | `AttachmentResponse` | `{ header: AttachmentHeader; body: ReadableStream }` | ### Errors | Class | When it is thrown | | ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | | `AttachmentNotFound` | `stat(ref)` or `get(ref)` called with a ref the store does not know, or one the caller may not read through the named document (the server answers both with the same 404). | | `InvalidAttachmentRef` | A string passed where a ref is expected does not match `attachment://v:`. | | `UploadTooLarge` | `upload.send()` exceeded the server's configured byte cap. Maps to HTTP 413. | | `ReservationNotFound` | `upload.send()` after the reservation expired or was deleted. | | `AttachmentAlreadyExists` | Hash-first `reserve()` for content whose hash is already stored. The client's `reserve()` catches this and returns the existing ref instead of uploading, with a header built from the caller's own reserve options. | | `AttachmentPending` | `get(ref)` for a hash that is reserved but whose bytes have not finished uploading. Carries `readonly hash`, `readonly expiresAtUtc: string`, and `readonly metadata?: { mimeType; fileName; sizeBytes }` so the caller can show the declared size and retry timing. Intentionally not a subclass of `AttachmentNotFound` — pending is "retry later", not "unknown". | | `HashMismatch` | Hash-first `send()` whose uploaded bytes do not match the claimed `clientHash`. | | `SizeMismatch` | Hash-first `send()` whose actual byte count differs from the declared `sizeBytes`. | A subgraph's client can also throw `AttachmentAccessUnavailable`, `AttachmentAccessFailed`, and `AuthenticationRequiredError`, all from `@powerhousedao/reactor-api`. See [Inside the switchboard](#inside-the-switchboard). ### Helpers `parseRef(ref)` and `createRef(hash, version?)` are exported from the package, but refs should be treated as opaque outside the service itself. Reach for them only if you genuinely need to inspect or reconstruct a ref (for example, in tests or debugging tooling). ## Storage backends and direct transfer Attachment bytes are stored through a pluggable, provider-neutral backend (`IAttachmentBackend`): the filesystem backend keeps the existing proxy-through-Switchboard behavior, and the S3-compatible backend transfers bytes directly between the client and object storage using short-lived presigned URLs. Switchboard always remains the control plane — authentication, authorization, metadata, reservations, and presigning — while in S3 mode object storage becomes the byte plane. Document models are unaffected either way: they store only opaque `AttachmentRef` values and never see storage configuration, credentials, or transfer mechanics. ### Selecting a backend Filesystem is the default and requires no configuration. S3 is enabled per Switchboard environment exclusively through environment variables: | Variable | Required | Meaning | | --- | --- | --- | | `PH_ATTACHMENT_STORAGE` | — | `s3` enables S3; absent or `filesystem` keeps current behavior. | | `PH_ATTACHMENT_S3_ENDPOINT` | in S3 mode | HTTPS endpoint of the S3-compatible provider. | | `PH_ATTACHMENT_S3_REGION` | in S3 mode | Provider region. | | `PH_ATTACHMENT_S3_BUCKET` | in S3 mode | Private bucket name. | | `PH_ATTACHMENT_S3_ACCESS_KEY_ID` / `PH_ATTACHMENT_S3_SECRET_ACCESS_KEY` | in S3 mode | Permanent credentials; available only to Switchboard, never to clients. | | `S3_ATTACHMENT_PREFIX` | no | Object key prefix, default `attachments`. | | `PH_ATTACHMENT_S3_FORCE_PATH_STYLE` | no | `true` for path-style addressing, default `false`. | | `PH_ATTACHMENT_S3_UPLOAD_TTL_SECONDS` / `PH_ATTACHMENT_S3_DOWNLOAD_TTL_SECONDS` | no | Presigned URL lifetimes, defaults 900 / 300 seconds. | An incomplete or invalid S3 configuration fails startup loudly; S3 mode never falls back to filesystem silently. Object keys are `///` — the same content addressing as the filesystem layout. ### Direct S3 upload The client hashes the file locally (the existing hash-first flow) and reserves through the same `POST /attachments/reservations` route. In S3 mode the reservation response carries a `presigned-put` upload target whose signed headers include the Base64 SHA-256 checksum (`x-amz-checksum-sha256`) and content type; the client PUTs the bytes directly to the provider with exactly those headers — never with a Switchboard JWT — and S3 itself rejects bytes that do not match the signed checksum. There is no finalize endpoint, no post-upload verification request, no temporary object, no CopyObject promotion, and no multipart path. A PUT 2xx (or a confirmed dedup, where the reservation short-circuits to the existing ref) is the signal after which the application may dispatch the `AttachmentRef` into a document operation. Because metadata becomes `available` optimistically at reserve time, an abandoned upload URL can leave metadata pointing at a missing object; a later reservation detects the missing object and issues a fresh target, and a direct GET of a missing object surfaces as `AttachmentNotFound`. ### Document-authorized download Knowing a hash or ref is not download authority. Every read names the document that authorizes it: ``` GET /attachments/:hash/download-target?documentId=[&expiresIn=] GET /attachments/:hash?documentId= HEAD /attachments/:hash?documentId= GET /attachments/:hash?documentId=&expires=&signature= HEAD /attachments/:hash?documentId=&expires=&signature= ``` A `documentId` read is allowed only when all of the following hold, in order, before any metadata or storage access: 1. The verified caller can read the document (`IAuthorizationService.canRead` — the canonical service that already handles admins, owners, unprotected documents, READ/WRITE/ADMIN and inherited grants; attachment code does not reimplement these rules). 2. The reactor's read gate serves the document to the caller. 3. The document's operations reference the attachment, per the reference index below, in a scope the caller may read. The caller identity comes exclusively from verified bearer authentication; identity headers or query parameters are ignored, and a caller with no bearer is decided as the anonymous subject. An unreadable document, an absent relationship, a request that names no document, and an unknown hash all return the same `404 { "error": "Attachment not found" }`, so a refusal never reveals that a hash exists. That includes the uploader: a blob no operation references yet is served to no one. There is no attachment policy package, plugin mechanism, ACL, or owner table — authorization is the document's read decision plus the indexed relationship, nothing else. `download-target` returns a validated, non-cacheable (`Cache-Control: no-store`) download target: a short-lived `presigned-get` for S3, or for filesystem a `switchboard` target whose URL is the byte route with `documentId`, `expires`, and `signature` parameters. The signature is an HMAC-SHA256 over the hash, the canonical document id, and the expiry; the byte route verifies it instead of re-running the decision, so the URL works without a bearer until it expires, like a presigned URL. Both kinds last at most 300 seconds by default, whether or not `expiresIn` asks for a lifetime; `ATTACHMENT_DOWNLOAD_TARGET_MAX_TTL_SECONDS` raises that ceiling, up to 7 days. A tampered, expired, or unverifiable signature answers the same 404. The filesystem signing secret comes from `PH_ATTACHMENT_URL_SIGNING_SECRET` (at least 32 characters). Set it to the same value on every Switchboard instance that serves the same attachment storage. When it is unset and `NODE_ENV` is `production`, filesystem `download-target` answers `503`; any other process signs with a random per-process secret, so its URLs stop verifying on restart and on other instances. `documentId` byte reads never need the secret. With `REQUIRE_AUTHENTICATED_CALLER` on, a signed URL still needs a bearer. ### The attachment reference index `AttachmentReferenceReadModel` is a global read model (standard `BaseReadModel`, registered once through `ReactorBuilder.withReadModelFactory`) that observes successful document operations and maintains an append-only `documentId ↔ AttachmentRef` relationship. Discovery is schema-aware: values are extracted only from fields whose operation schema declares `AttachmentRef` — including nested inputs, lists, and nullable fields — so untyped string lookalikes never create authorization evidence. Extraction plans are compiled lazily per concrete document-model module and action, cached by module identity, and reused on the hot path; operations without attachment fields take a fast path with no database writes. The read model consults the live document-model registry at operation time, so packages installed after Switchboard startup are indexed without a restart (interpretation uses the latest registered schema for the type). Removing a ref from current document state does not erase the historical relationship: a user who can read the document history can still resolve attachments it referenced. The index persists in its own `attachment_reference_read_model` SQL schema with its own migrations and store (built by `AttachmentReferenceIndexBuilder`), sharing the physical database but leaving the original `attachments` schema, its migrations, and `AttachmentBuilder` untouched. Cursor recovery for failed batches is attachment-specific and scoped to this read model; generic `BaseReadModel` behavior is unchanged. In compositions where the projection cannot be hosted (an unusual caller-provided Reactor with a custom coordinator), Switchboard still starts with all existing routes and only the download-target route fails closed with a 503. ### Client and hook additions `IAttachmentService.get` takes the authorization anchor in its options: `get(ref, { documentId, signal })`. Remote downloads negotiate the download target with the Switchboard JWT, then execute it — `switchboard` targets carry their signature and the JWT, `presigned-get` targets receive exactly the returned headers and never the JWT. `stat(ref, { documentId })` sends a `documentId` HEAD. A replica pulling bytes through `SwitchboardAttachmentTransport.fetch(hash, documentId)` names the document whose operation it applied and is authorized as its own JWT subject, which must be able to read a scope referencing the attachment. `IAttachmentClient` adds `upload(input)`, document-aware `download({ documentId, ref })`, and bounded-concurrency `uploadMany` / `downloadMany` batch operations built on a reusable `runWithConcurrency` runner: concurrency bounds hashing and transfer together, results preserve input order, successes survive sibling failures, per-item signals cancel one item, and a batch signal stops unstarted work. Every single-item method takes an options bag as its second argument carrying `onProgress` and `throttleMs`, and reports `AttachmentProgress` — `{ stage, loaded, total, indeterminate }` — where `stage` is one of `hashing`, `reserving`, `uploading`, `requesting-download-target`, `downloading`, `done`, `error`. `loaded`/`total` are per-stage, not per-operation: a per-operation denominator would have to weight hashing against transfer, and a dedup would shrink it mid-flight. Exactly one terminal event is emitted per operation; `done` always carries `loaded === total`. Byte events are throttled to one per 100ms by default. `download()` is the one asymmetric case: it resolves as soon as the stream is available, with `downloading` as the last event and `loaded: 0`. Byte events and the terminal `done` arrive as the caller reads the stream — so `downloadMany`'s `concurrency` bounds target negotiation rather than simultaneous transfer. Abandoning the stream is a failure, not a completion: cancelling it emits the terminal `error`, carrying the bytes that did arrive and the cancel reason (an `AbortError` when none was given), the same way an aborted request reports itself. Batch progress is per item, stamped with `index`, alongside an `onItemSettled` callback carrying `{ settled, completed, failed, total }`. Upload bytes are only observable through an `uploadTransport` that can see them; `fetch` cannot, so the default transport reports `uploading` as indeterminate and stays silent. `createXhrUploadTransport()` is the browser-side opt-in, set on `SwitchboardClientConfig.uploadTransport`. It registers `xhr.upload.onprogress` only when a caller actually passes `onProgress`, because that registration alone forces a CORS preflight on a presigned PUT. An explicit `fetchFn` always wins over it, so adding `uploadTransport` to a config that already pins fetch is a no-op. Remote transfer failures throw `AttachmentTransferError` with a `stage` and no URL, signature, or credential detail. `useAttachments()` in `@powerhousedao/reactor-browser` returns this extended client, `useAttachmentUpload()` exposes upload progress with `cancel`/`reset`/`result`, and `useAttachmentPreview()` exposes download progress with `attempt`/`lastError`. ## Migrating from inline `data` fields If a document model previously inlined binary content, the migration is small and mechanical: - **Schema** — replace `data: String` (or composite fields like `{ image, imageMediaType, imageUrl }`) with a single `attachment: AttachmentRef` field on the input and the state type. - **Write path** — replace the base64-encoding step with a reservation and upload. Either reserve on the service directly (`attachments.reserve({ mimeType, fileName })` then `upload.send(stream)`, dispatching with `result.ref`), or use the client (`attachments.preprocess(file)` then `client.write.execute(...)` with `results.ref` and `attachments.reserve(results.options, ...)`). In the browser, `useAttachmentUpload()` wraps the client flow and reports byte progress. - **Read path** — replace data-URI assembly with `attachments.get(ref, { documentId })`. In a browser, drain the stream into a `Blob` and use `URL.createObjectURL` (remember to `revokeObjectURL` on cleanup). - **Graceful degradation** — code paths that may run without an attachment service (mock environments, tests, off-line previews) should branch on `if (!attachments)`. Writers should surface a clear error to the user; readers should skip the attachment and render a placeholder. ## Deep dive See `packages/reactor/docs/attachments.md` in the monorepo for the full design: storage internals, the LRU eviction policy, the `IAttachmentStore` and `IAttachmentTransport` interfaces, the switchboard upload protocol, and the S3 backend. For the hash-first reservation mode, hash/size verification on ingest, and the pending-upload lifecycle, see `packages/reactor/docs/attachments-hash-first.md`. --- ## Recipes > Source: https://academy.vetra.io/academy/Reference/Reactor/Recipes The [Powerhouse Recipes](https://github.com/powerhouse-inc/recipes) repository is a collection of small, standalone, runnable projects. Each one isolates a single Reactor pattern — a processor, a read model, a sync channel, an auth scheme, a schema migration — so you can read it end to end, run it locally, and lift the parts you need into your own package. This page is a map. For each recipe it gives the core idea, the Reactor primitives it exercises, and links straight to the source on GitHub. Click through to the repo for full setup instructions, demos, and expected output. For the same recipes rendered inline behind expandable toggles, see the [Cookbook](/academy/Lookup/Cookbook#reactor-recipes). **INFO:** Recipes are reference implementations, not published libraries. Clone the repo, read the recipe, and adapt the code — don't depend on the `@powerhousedao/example-*` packages in production. ## Running a recipe Every recipe is a workspace package with a runnable demo. From the repo root: ```sh pnpm install pnpm build # then run a single recipe's demo, e.g. pnpm --filter @powerhousedao/saga start ``` Each recipe's README lists its exact `pnpm --filter … start` command. The in-memory recipes run against PGlite with no external services; the ones that need PostgreSQL document their connection setup in their own README. --- ## Processors & read models The most common Reactor extension point. A [processor](/academy/Reference/Reactor/Processors) reacts to operations **post-ready** (after a chain is readable) to perform side effects; a read model indexes **pre-ready** to build a derived view that readers can trust the instant they see it. These recipes cover both ends. ### [Analytics Processor](https://github.com/powerhouse-inc/recipes/tree/main/analytics-processor) An `IProcessor` that projects expense-report operations into the Powerhouse analytics-engine time-series store, then answers rollups by category, month, and currency. - Maps each `OperationWithContext` to `AnalyticsSeriesInput` rows via `IAnalyticsStore.addSeriesValues`, registered through a `ProcessorFactory` with a filter and `startFrom: "beginning"`. - Models corrections as append-only compensating deltas (`ADD` writes `+amount`; `UPDATE` writes `-old` then `+new`; `DELETE` writes `-old`), so the financial series stays auditable. - Idempotent replay: an `operation.index === 0` for an already-seen document triggers `clearSeriesBySource` and a rebuild, so re-delivery always converges to identical totals. **Source** · [`src/processor.ts`](https://github.com/powerhouse-inc/recipes/blob/main/analytics-processor/src/processor.ts) · [`src/query.ts`](https://github.com/powerhouse-inc/recipes/blob/main/analytics-processor/src/query.ts) **Concepts** · `IProcessor.onOperations` · `ProcessorFactory` · `IAnalyticsStore` · `AnalyticsPath` · `AnalyticsQueryEngine` — see [Processors](/academy/Reference/Reactor/Processors) ### [Audit Trail](https://github.com/powerhouse-inc/recipes/tree/main/audit-trail) A processor that reads `ActionSigner` context off every operation and writes an immutable audit log to PostgreSQL, fronted by a GraphQL subgraph. - Mines signer identity from `operation.action.context.signer` (`user.address`, `networkId`, `chainId`, app credentials) and batch-inserts one Kysely statement per operation batch. - Pairs with `ReactorClientBuilder.withSigner` so client writes automatically populate the signer context the processor reads. - Exposes a graphql-yoga subgraph to query entries by user, document, or time range. **Source** · [`src/processor.ts`](https://github.com/powerhouse-inc/recipes/blob/main/audit-trail/src/processor.ts) · [`src/subgraph.ts`](https://github.com/powerhouse-inc/recipes/blob/main/audit-trail/src/subgraph.ts) **Concepts** · `IProcessor.onOperations` · `ActionSigner` · `ProcessorFactory` · `ReactorClientBuilder.withSigner` — see [Processors](/academy/Reference/Reactor/Processors), [Reactor Client](/academy/Reference/Reactor/ReactorClient) ### [Custom Read Model](https://github.com/powerhouse-inc/recipes/tree/main/custom-read-model) Implements `IReadModel` directly and registers it with `ReactorBuilder.withReadModel()` to maintain a document-count-per-type materialized view. - Implements the `IReadModel` contract directly (just `indexOperations` plus query getters) instead of extending `BaseReadModel` — the right choice when you don't need catch-up/rewind plumbing. - Registered read models index **pre-ready**: their work completes *before* `JOB_READ_READY` fires, so the view is consistent the instant downstream subscribers see the event. This is the key contrast with post-ready processors. **Source** · [`src/document-count-read-model.ts`](https://github.com/powerhouse-inc/recipes/blob/main/custom-read-model/src/document-count-read-model.ts) · [`src/index.ts`](https://github.com/powerhouse-inc/recipes/blob/main/custom-read-model/src/index.ts) **Concepts** · `IReadModel.indexOperations` · `ReactorBuilder.withReadModel` · pre-ready vs post-ready · `JOB_READ_READY` — see [Processors](/academy/Reference/Reactor/Processors), [Working with the Reactor](/academy/Reference/Reactor/WorkingWithTheReactor) ### [Full-Text Search Processor](https://github.com/powerhouse-inc/recipes/tree/main/full-text-search) A processor that flattens each document's resulting state into searchable text and maintains a PostgreSQL `tsvector` index for ranked keyword search. - Indexes from `context.resultingState` rather than individual action inputs, so it stays agnostic to any document model's action set; dedupes to the last operation per `documentId` within a batch. - Recursively flattens arbitrary JSON state into one string and maintains the row with an idempotent `INSERT … ON CONFLICT` upsert; reacts to the `DELETE_DOCUMENT` action by removing the row. - `startFrom: "beginning"` back-indexes existing history; queries use `plainto_tsquery` and `ts_rank` over a GIN index. **Source** · [`processor.ts`](https://github.com/powerhouse-inc/recipes/blob/main/full-text-search/processor.ts) · [`query.ts`](https://github.com/powerhouse-inc/recipes/blob/main/full-text-search/query.ts) **Concepts** · `IProcessor.onOperations` · `context.resultingState` · `processorManager.registerFactory` · `tsvector` — see [Processors](/academy/Reference/Reactor/Processors), [Storage and Scaling](/academy/Reference/Reactor/StorageAndScaling) ### [Relational DB Subgraph](https://github.com/powerhouse-inc/recipes/tree/main/relational-db-subgraph) A document-type-agnostic `RelationalDbProcessor` that projects every document into denormalized, Kysely-managed Postgres tables and serves them through a GraphQL subgraph. - Subclass `RelationalDbProcessor` and maintain the relational read model in `onOperations` — parse `context.resultingState`, handle `DELETE_DOCUMENT` by clearing rows. - Owns its schema through an `initAndUpgrade()` override that runs an idempotent (`ifNotExists`) Kysely migration, so registration is safe to repeat; uses the typed query builder, dropping to raw `sql` only for an `ON CONFLICT` upsert. - A separate query layer feeds a graphql-yoga subgraph that can compose into a supergraph. **Source** · [`src/processor.ts`](https://github.com/powerhouse-inc/recipes/blob/main/relational-db-subgraph/src/processor.ts) · [`src/subgraph.ts`](https://github.com/powerhouse-inc/recipes/blob/main/relational-db-subgraph/src/subgraph.ts) **Concepts** · `RelationalDbProcessor` · `IRelationalDb` · `initAndUpgrade` · GraphQL subgraph — see [Processors](/academy/Reference/Reactor/Processors), [Storage and Scaling](/academy/Reference/Reactor/StorageAndScaling) ### [Semantic Search Processor](https://github.com/powerhouse-inc/recipes/tree/main/semantic-search) A processor read model that embeds document state in-process with Transformers.js (MiniLM) into PGlite + pgvector and answers cosine-similarity queries — no external API. - In-process embeddings (ONNX-quantized `all-MiniLM-L6-v2`, 384-dim) stored and queried in PGlite's pgvector extension — no embedding service, no separate vector database. - Skips the expensive embed step when a SHA-256 `content_hash` matches the stored row; bakes the dimension into the `vector(384)` column and asserts the length before insert so a mismatched model fails loudly. - Injects the embedder as a dependency, so tests run against a deterministic offline fake. **Source** · [`processor.ts`](https://github.com/powerhouse-inc/recipes/blob/main/semantic-search/processor.ts) · [`embedder.ts`](https://github.com/powerhouse-inc/recipes/blob/main/semantic-search/embedder.ts) · [`query.ts`](https://github.com/powerhouse-inc/recipes/blob/main/semantic-search/query.ts) **Concepts** · `IProcessor.onOperations` · `context.resultingState` · pgvector · Transformers.js — see [Processors](/academy/Reference/Reactor/Processors), [Storage and Scaling](/academy/Reference/Reactor/StorageAndScaling) ## Cross-document automation & orchestration Treating the Reactor as an automation engine: one operation triggers work on other documents, or a single call provisions many documents at once. ### [Batch Progress](https://github.com/powerhouse-inc/recipes/tree/main/batch-progress) Creates a dependency-ordered set of documents in a single `IReactor.executeBatch` call and tracks each job's lifecycle live via the EventBus. - One `executeBatch` submits a graph of jobs; each job's `dependsOn` (referencing other jobs by key) lets the Reactor resolve ordering and parallelism across both document and drive scopes. - Subscribe to `JOB_PENDING` / `JOB_RUNNING` / `JOB_WRITE_READY` / `JOB_READ_READY` / `JOB_FAILED` on the EventBus before calling `executeBatch`. Jobs with no dependencies start running while the call is still in flight. Job ids are only known once `result.jobs[key].id` comes back, so buffer events by `jobId` and map them to keys after the call returns. - `JobAwaiter` (`new JobAwaiter(eventBus, (jobId, signal) => reactor.getJobStatus(jobId, signal))`) reads the current status before it waits, so `waitForJob(jobId)` resolves even when the job finished during `executeBatch`. **Source** · [`src/create-project.ts`](https://github.com/powerhouse-inc/recipes/blob/main/batch-progress/src/create-project.ts) · [`src/index.ts`](https://github.com/powerhouse-inc/recipes/blob/main/batch-progress/src/index.ts) **Concepts** · `IReactor.executeBatch` · `dependsOn` · `IEventBus.subscribe` · `JobAwaiter.waitForJob` — see [Advanced Reactor Usage](/academy/Reference/Reactor/AdvancedReactorUsage), [Error Handling](/academy/Reference/Reactor/ErrorHandling) ### [Cross-Document Reactor](https://github.com/powerhouse-inc/recipes/tree/main/cross-document-reactor) Drives event-driven automation by subscribing to all document changes through an `IReactorClient` and dispatching actions on related documents from inside the handler. - `client.subscribe({}, cb)` with an empty filter delivers every `DocumentChangeEvent`; the handler branches on `DocumentChangeType.Updated`. - Resolves relationships at runtime with `client.find({ parentId })` plus a naming convention, then acts via `client.rename`. - A local re-entrancy guard (a `reacting` flag) stops the handler's own write — which fires another change event — from re-triggering the rule. **Source** · [`src/index.ts`](https://github.com/powerhouse-inc/recipes/blob/main/cross-document-reactor/src/index.ts) **Concepts** · `IReactorClient.subscribe` · `DocumentChangeEvent` · `client.find` · re-entrancy guard — see [Reactor Client](/academy/Reference/Reactor/ReactorClient) ### [Saga Coordination Processor](https://github.com/powerhouse-inc/recipes/tree/main/saga) A processor implements the saga pattern: operations on one document dispatch follow-up actions to others, all correlated by a shared `saga_id`. - Declarative step definitions (trigger match, target resolver, action builder) drive which incoming operation fires which follow-up; the processor calls `IReactor.execute` on a *different* document. - A re-entrancy flag prevents the processor from reacting to the operations it itself dispatched, so the saga can't loop forever. - Correlation lives entirely in the processor's own `saga_log` table keyed by `saga_id` — no changes to any document model or interface. **Source** · [`src/processor.ts`](https://github.com/powerhouse-inc/recipes/blob/main/saga/src/processor.ts) · [`src/demo.ts`](https://github.com/powerhouse-inc/recipes/blob/main/saga/src/demo.ts) **Concepts** · `IProcessor.onOperations` · `IReactor.execute` · re-entrancy guard · saga correlation — see [Processors](/academy/Reference/Reactor/Processors) ## External integration & ingestion Bridging the Reactor to the outside world — pulling external events in, and pushing operations out — without losing idempotency or authenticity. ### [Inbound Webhook Bridge](https://github.com/powerhouse-inc/recipes/tree/main/inbound-webhook-bridge) A standalone `node:http` endpoint that HMAC-verifies signed external webhooks against the raw request bytes and dispatches each verified event as a typed document action. - Verifies the HMAC over the **exact raw bytes before `JSON.parse`** — a GraphQL/JSON resolver would parse too early, and a `JSON.parse` → `JSON.stringify` round-trip changes the bytes and breaks the MAC. - Stripe-style scheme: HMAC over the `timestamp` joined to the `rawBody` with `crypto.timingSafeEqual` plus a replay window, so folding the timestamp into the MAC makes the freshness check tamper-proof. - Idempotency survives restarts by storing processed event ids in **document state** (`processedEventIds`), not processor memory; the reducer independently throws `DuplicateEvent` as a second line of defense. **Source** · [`src/webhook-bridge.ts`](https://github.com/powerhouse-inc/recipes/blob/main/inbound-webhook-bridge/src/webhook-bridge.ts) · [`src/signature.ts`](https://github.com/powerhouse-inc/recipes/blob/main/inbound-webhook-bridge/src/signature.ts) **Concepts** · `IReactor.execute` · `JobAwaiter.waitForJob` · action creators · reducer dedup guards — see [Error Handling](/academy/Reference/Reactor/ErrorHandling), [Working with the Reactor](/academy/Reference/Reactor/WorkingWithTheReactor) ### [External Feed Ingest](https://github.com/powerhouse-inc/recipes/tree/main/external-feed-ingest) A long-running polling worker that idempotently ingests an external feed into an event-sourced ledger document, seeding its dedup set and watermark from document state so a restart never double-ingests. - The document *is* the checkpoint store: `seedFromState()` rebuilds the in-memory dedup set and high-watermark from `reactor.get().state`, so a crashed poller resumes with no side database. - Dedupes on the stable `externalId` checked against state, **not** the delivery cursor — at-least-once feeds redeliver the same id past any watermark; the watermark is only a fetch optimization. - Corrections are explicit append-only operations: a restatement marks the old entry `SUPERSEDED` and appends a new `RECORDED` entry — the original payload is never mutated. **Source** · [`src/poller.ts`](https://github.com/powerhouse-inc/recipes/blob/main/external-feed-ingest/src/poller.ts) · [`document-models/feed-ledger/v1/src/reducers/ingest.ts`](https://github.com/powerhouse-inc/recipes/blob/main/external-feed-ingest/document-models/feed-ledger/v1/src/reducers/ingest.ts) **Concepts** · `IReactor.get` / `execute` · `JobAwaiter` · typed reducer errors · append-only corrections — see [Error Handling](/academy/Reference/Reactor/ErrorHandling), [Reactor Client](/academy/Reference/Reactor/ReactorClient) ### [Discord Webhook Processor](https://github.com/powerhouse-inc/recipes/tree/main/discord-webhook-processor) A processor that forwards filtered document operations to a Discord webhook as rich embeds, HMAC-signing every request. - A `ProcessorFilter` on the record (rather than branching inside `onOperations`) scopes which operations reach the processor. - Chunks the operations array to respect a downstream system's batch limit (Discord's 10-embed cap) across multiple `fetch` POSTs. - Signs each outbound request with an HMAC-SHA256 `X-Reactor-Signature` over the exact JSON body. **Source** · [`discord-webhook-processor.ts`](https://github.com/powerhouse-inc/recipes/blob/main/discord-webhook-processor/discord-webhook-processor.ts) · [`demo.ts`](https://github.com/powerhouse-inc/recipes/blob/main/discord-webhook-processor/demo.ts) **Concepts** · `IProcessor.onOperations` · `ProcessorFilter` · `startFrom: "current"` · HMAC signing — see [Processors](/academy/Reference/Reactor/Processors) ## Auth, signing & security Where identity comes from (`action.context.signer`), how it's verified, and how access decisions are made — inside the reducer, at a gate, or after the fact. ### [Role-Based Auth](https://github.com/powerhouse-inc/recipes/tree/main/role-based-auth) A custom document model that embeds RBAC in document state and enforces it inside reducer operations by reading the caller from `action.context.signer.user.address`. - Authorization lives in the reducer, not a server gate — so access control replicates with the document; each operation throws if the caller is missing or lacks the required role. - Roles are *state*, not config: `creator` / `admins` / `members` are fields, and the first `bootstrap` caller auto-promotes to permanent root admin, protected by `CannotRevokeCreator` / `LastAdmin` invariants. - Rejections surface as a typed error taxonomy (`NotAuthorized`, `NotAdmin`, …) captured onto `operation.error` in the document's log. **Source** · [`document-models/role-based-auth/v1/src/reducers/access.ts`](https://github.com/powerhouse-inc/recipes/blob/main/role-based-auth/document-models/role-based-auth/v1/src/reducers/access.ts) · [`src/demo.ts`](https://github.com/powerhouse-inc/recipes/blob/main/role-based-auth/src/demo.ts) **Concepts** · document-model reducer · `action.context.signer` · generated error taxonomy — see [Working with the Reactor](/academy/Reference/Reactor/WorkingWithTheReactor) ### [Rate Limiter](https://github.com/powerhouse-inc/recipes/tree/main/rate-limiter) A read-only processor that counts operations per signer address over a sliding window and trips a shared `AuthService` cooldown, which a request gate consults to throttle abusive users. - Decouples observation from enforcement: the processor only signals (`authService.cooldown`) and never blocks, while the request gate (resolver/middleware) reads `authService.isAllowed` before forwarding mutations. - Derives the per-user key from `signer.user.address`; sliding-window counting in a plain `Map`, reset in `onDisconnect`. **Source** · [`src/rate-limiter-processor.ts`](https://github.com/powerhouse-inc/recipes/blob/main/rate-limiter/src/rate-limiter-processor.ts) · [`src/auth-service.ts`](https://github.com/powerhouse-inc/recipes/blob/main/rate-limiter/src/auth-service.ts) **Concepts** · `IProcessor.onOperations` · `signer.user.address` · `AuthService` gate · `startFrom: "current"` — see [Processors](/academy/Reference/Reactor/Processors) ### [Signed Operations Verifier](https://github.com/powerhouse-inc/recipes/tree/main/signed-operations-verifier) A standalone script that builds cryptographically signed operations with an `ISigner` and verifies each one per-signature, detecting tampered and unsigned operations. - Builds a self-contained ECDSA P-256 `ISigner` with `RenownCryptoBuilder` plus in-memory key storage — no filesystem or network. - `buildSignedAction` advances the document through the reducer between actions, so each signature captures the correct previous-state hash. A reactor checks these tuples itself when it stores a write; see [Signature Verification](/academy/Build/BuildingUserExperiences/Authorization/Signing#signature-verification). - Tamper and unsigned detection: corrupting a signature element or removing `action.context` flips verification to invalid / unsigned respectively. **Source** · [`src/verify-operations.ts`](https://github.com/powerhouse-inc/recipes/blob/main/signed-operations-verifier/src/verify-operations.ts) · [`src/verify-operations.test.ts`](https://github.com/powerhouse-inc/recipes/blob/main/signed-operations-verifier/src/verify-operations.test.ts) **Concepts** · `ISigner` · `RenownCryptoSigner` · `buildSignedAction` · signature tuples — see [Advanced Reactor Usage](/academy/Reference/Reactor/AdvancedReactorUsage) ## Document-model patterns Patterns that live in the document model itself — how schemas evolve over time, and how to model a container without paying for it. ### [Document Versioning](https://github.com/powerhouse-inc/recipes/tree/main/document-versioning) Migrates a document model's schema from v1 to v2 with an `UpgradeManifest` and a pure `upgradeReducer`, so operation logs recorded under the old schema still replay into the new state shape. - Declares every schema version in one codegen spec so `ph generate` emits a versioned `v1` / `v2` / `upgrades` tree plus a wired `UpgradeManifest`. - The upgrade reducer must patch **both** `state` and `initialState`: replay folds domain operations from the stored `initialState` and never re-runs the `UPGRADE_DOCUMENT` op, so a state-only migration passes live but fails replay with `HashMismatchError`. - The latest reducer owns the entire history — retired operations are kept and their reducer maps old fields onto new ones, so cross-version replay converges. **Source** · [`document-models/todo/upgrades/v2.ts`](https://github.com/powerhouse-inc/recipes/blob/main/document-versioning/document-models/todo/upgrades/v2.ts) · [`src/upgrade.ts`](https://github.com/powerhouse-inc/recipes/blob/main/document-versioning/src/upgrade.ts) **Concepts** · `UpgradeManifest` · `upgradeReducer` · `computeUpgradePath` · `replayDocument` · `HashMismatchError` — see [Document Model Registry](/academy/Reference/Reactor/DocumentModelRegistry) ### [Drive Override](https://github.com/powerhouse-inc/recipes/tree/main/drive-override) A custom container document that tracks its children through the reactor's built-in `ADD_RELATIONSHIP` action instead of `document-drive`'s `ADD_FILE`, keeping container state O(1). - The container is an ordinary `DocumentModelModule` holding only metadata (`name` / `description`, one `SET_METADATA` op) — no embedded `nodes[]`, so its state and operation log stay flat as children scale past 10,000. - Children are linked with `addRelationshipAction(...)` dispatched through `reactor.execute`; the indexer writes one row to the `DocumentRelationship` table and the container's reducer never sees them. - Enumerate with `IDocumentIndexer.getOutgoing` / `getIncoming` and cursor paging — DB-native indexed queries, also exposed over GraphQL. **Source** · [`src/index.ts`](https://github.com/powerhouse-inc/recipes/blob/main/drive-override/src/index.ts) · [`document-models/custom-container/v1/src/reducers/metadata.ts`](https://github.com/powerhouse-inc/recipes/blob/main/drive-override/document-models/custom-container/v1/src/reducers/metadata.ts) **Concepts** · `addRelationshipAction` · `IDocumentIndexer` · `ConsistencyToken` · `DocumentModelModule` — see [Working with the Reactor](/academy/Reference/Reactor/WorkingWithTheReactor) ## Tooling & operations Operating a Reactor from the outside: exporting state, watching the live event stream, monitoring sync health, and moving the underlying database. ### [Document Snapshot Exporter](https://github.com/powerhouse-inc/recipes/tree/main/document-snapshot-exporter) A CLI that exports document state and operation history to JSON using `IReactor` consistency tokens for read-after-write, contrasted side-by-side with the auto-managed `IReactorClient`. - Threads the `ConsistencyToken` from a completed write into `reactor.get()` / `getOperations()`, so a read reflects the just-finished write even while background indexing lags. - Side-by-side comparison via a `--mode` flag: low-level `IReactor` (returns `JobInfo`, manual awaiting + tokens) versus `IReactorClient` (awaits and manages consistency internally, returns the document directly). - Handles the differing `getOperations` shapes — a per-scope `Record` for `IReactor` versus a flat `PagedResults` for the client. **Source** · [`src/export-reactor.ts`](https://github.com/powerhouse-inc/recipes/blob/main/document-snapshot-exporter/src/export-reactor.ts) · [`src/export-client.ts`](https://github.com/powerhouse-inc/recipes/blob/main/document-snapshot-exporter/src/export-client.ts) **Concepts** · `ConsistencyToken` · `IReactor` vs `IReactorClient` · `JobAwaiter` · `getOperations` — see [Working with the Reactor](/academy/Reference/Reactor/WorkingWithTheReactor), [Reactor Client](/academy/Reference/Reactor/ReactorClient) ### [Subscription CLI](https://github.com/powerhouse-inc/recipes/tree/main/subscription-cli) A Node CLI that connects to a Reactor's GraphQL subscriptions endpoint over WebSocket and prints `documentChanges` (and optional `jobChanges`) events live. - Consumes real-time subscriptions from *outside* the reactor process via the `graphql-ws` protocol, rather than from an in-process processor. - Server-side filters the change stream with a `SearchFilterInput` (by document type and/or parent drive); passes bearer-token auth through `connectionParams`. - Optionally watches a single job's lifecycle via `jobChanges(jobId)`, and cleans up per-stream subscriptions on `SIGINT` / `SIGTERM`. **Source** · [`src/index.ts`](https://github.com/powerhouse-inc/recipes/blob/main/subscription-cli/src/index.ts) **Concepts** · `documentChanges` · `jobChanges` · `SearchFilterInput` · `graphql-ws` — see [Synchronization](/academy/Reference/Reactor/Synchronization), [Reactor Client](/academy/Reference/Reactor/ReactorClient) ### [Sync Health Monitor](https://github.com/powerhouse-inc/recipes/tree/main/sync-health-monitor) Subscribes to the Reactor EventBus sync lifecycle events to maintain live replication-health counters and exposes them through a GraphQL subgraph. - Folds all five `SyncEventTypes` (pending / succeeded / failed / dead-letter / connection-state) into health counters and derives a healthy / degraded / unhealthy status. - Implements `IChannelFactory` and `IChannel` to bridge two reactors entirely in-process, calling the success/failure lifecycle hooks on each `SyncOperation` item (`transported` / `executed` / `failed`) and routing failed sends to a dead-letter mailbox. - Registers peers via `syncModule.syncManager.add(...)` and serves a `syncHealth` query over a graphql-yoga subgraph. **Source** · [`src/health-monitor.ts`](https://github.com/powerhouse-inc/recipes/blob/main/sync-health-monitor/src/health-monitor.ts) · [`src/internal-channel.ts`](https://github.com/powerhouse-inc/recipes/blob/main/sync-health-monitor/src/internal-channel.ts) **Concepts** · `IEventBus.subscribe` · `SyncEventTypes` · `IChannel` / `IChannelFactory` · `syncManager.add` — see [Synchronization](/academy/Reference/Reactor/Synchronization) ### [DB Migrate](https://github.com/powerhouse-inc/recipes/tree/main/db-migrate) Three Bash scripts that export, import, and migrate a PostgreSQL database by running `pg_dump` / `pg_restore` / `psql` inside the `postgres:17` Docker image — no local Postgres tools needed. - Runs the Postgres client tools from the official Docker image, so Docker is the only prerequisite for backing up or moving a Reactor relational store. - `migrate.sh` streams `pg_dump` straight into `pg_restore` (or `psql`) over a shell pipe, copying one database into another in a single pass with no intermediate dump file. - A code-free operations recipe — no document models, processors, or subgraphs. **Source** · [`migrate.sh`](https://github.com/powerhouse-inc/recipes/blob/main/db-migrate/migrate.sh) · [`export.sh`](https://github.com/powerhouse-inc/recipes/blob/main/db-migrate/export.sh) **Concepts** · `pg_dump` · `pg_restore` · Docker — see [Storage and Scaling](/academy/Reference/Reactor/StorageAndScaling) --- ## Authorization > Source: https://academy.vetra.io/academy/Reference/Reactor/Authorization The reactor decides who may do what from a policy stored on the document itself, in its `auth` scope. The policy is a list of **grants** that the reducer folds like any other state, so it syncs, replays and converges the same way document content does. Reducers contain no authorization code. This is separate from the host-side permission tables described in [Reactor API Authorization](/academy/Build/BuildingUserExperiences/Authorization/Authorization), which decide which addresses a host lets near a drive. The two answer different questions and neither substitutes for the other. **INFO:** Every flag below defaults to off, and enforcement protects nothing that already exists: a document that never emitted `INITIALIZE_AUTH` carries an uninitialized policy, which allows everything. Enforcement bites only on documents that have a policy. ## Feature flags Enforcement is staged. Each flag requires the one above it, and a reactor refuses to start on a set that skips one, because a partially enforcing reactor applies less than the operator asked for. | Flag | Env var | What it turns on | | ------------------- | ---------------------------- | ------------------------------------------------------------------- | | `documentDecisions` | `REACTOR_DOCUMENT_DECISIONS` | Decide writes from the document stream rather than the meta cache | | `authEnforcement` | `REACTOR_AUTH_ENFORCEMENT` | Enforce the policy at admission, replay and read | | `authGroups` | `REACTOR_AUTH_GROUPS` | Let `{ group }` principals match against a roster document | | `authConditions` | `REACTOR_AUTH_CONDITIONS` | Evaluate `where` clauses and `{ match }` principals | ```typescript new ReactorBuilder().withExecutorConfig({ featureFlags: { documentDecisions: true, authEnforcement: true, }, }); ``` The flags govern **enforcement only**. The data model is always live, so a grant written on a reactor with the flags off is stored and syncs; it is not enforced there. **WARNING:** A replay decision is a consensus outcome. Two replicas of the same document running different flags compute different verdicts for the same operation and diverge permanently. Move every node that shares a document together. ## The policy An uninitialized policy leaves the document open. `INITIALIZE_AUTH` installs the first grant list; after that the default is deny and only a grant can permit anything. Four actions write the `auth` scope: | Action | Effect | | ------------------ | ------------------------------------------------- | | `INITIALIZE_AUTH` | Installs the initial policy. Genesis only | | `SET_GRANT` | Adds a grant, or replaces the one with that `id` | | `REMOVE_GRANT` | Removes a grant by `id` | | `MOVE_GRANT` | Reorders a grant, which can change the outcome | `UNDO` and `REDO` are refused on the `auth` scope, and so is a submitted `RETRACT` on a base-reducer 3 document. A policy grants or refuses `RETRACT` like any other operation type. It cannot condition on who wrote the retracted operation. A denied `RETRACT` retracts nothing; see [Undo and redo](/academy/Reference/Reactor/UndoRedo). The creators are exported from `document-model` (and `@powerhousedao/shared/document-model`) alongside `Grant`: | Creator | Input | | ----------------------- | ------------------------------------------------------------- | | `initializeAuth(input)` | `{ version: number; grants: Grant[] }`. `version` is the policy language version, currently `1` | | `setGrant(input)` | `{ grant: Grant }` | | `removeGrant(input)` | `{ id: string }` | | `moveGrant(input)` | `{ id: string; index: number }`. `index` is clamped to the list | ```typescript const admin: Grant = { id: "admin", description: "The owner administers the policy", effect: "allow", principal: { address: ownerAddress }, capability: { can: "execute", scope: "auth" }, }; await client.write.execute(documentId, "main", [initializeAuth({ version: 1, grants: [admin] })]); await client.write.execute(documentId, "main", [setGrant({ grant: readers })]); ``` ### Grants ```typescript type Grant = { id: string; description: string; effect: "allow" | "deny"; principal: | { anyone: true } | { address: string } | { group: string } // a powerhouse/reactor-group document id | { match: Condition }; capability: | { can: "read"; scope?: string } | { can: "execute"; scope?: string; operation?: string[] }; where?: Condition; }; ``` `scope` accepts `"*"` for every scope. Omitting `operation` covers every action in the scope. A `{ group }` principal names a roster document (`powerhouse/reactor-group`) and resolves live, so hiring and offboarding are single membership operations on the roster rather than edits to every policy that trusts it. The roster model ships in `@powerhousedao/reactor-group`. Register its `documentModels` with `withDocumentModelSources`. The `ReactorGroupV1` module is exported from `@powerhousedao/reactor-group/document-models`, and `reactorGroupDocumentType` from `@powerhousedao/reactor-group/document-models/reactor-group`. A `where` clause reads `subject.*`, `doc..*` and `action.input.*`, which is how a grant can depend on the values of the operation it gates. ### How a decision is made Grants are walked in list order and **the last applicable one wins**. A grant applies when its capability covers the request and its principal matches the subject. If none applies, the request is denied. Three rules are easy to miss: - **An allow on `execute` confers `read` of that scope.** A grant of `{ can: "execute", scope: "*" }` therefore publishes every domain scope to whoever it names. Grant administration on `scope: "auth"` rather than `"*"`. - **A grant that cannot apply is skipped, not honoured.** Below `authGroups` a `{ group }` principal never matches; below `authConditions` a `where` clause never holds. Both fail closed, so a policy relying on a conditional *deny* is weaker than it reads on a reactor that cannot evaluate it. - **The creator keeps the auth scope.** The key that signed the document's header may always execute in `scope: "auth"`, so a policy cannot lock its own owner out. On an unsigned document there is no creator, and validation instead refuses any policy that would leave nobody able to administer it. ### Timestamps in the auth scope A reactor refuses an auth operation whose timestamp does not exceed the newest one in the scope. Auth actions made in one process are stamped 1 ms apart when the clock has not moved, so a burst such as `initializeAuth` followed by two `setGrant` calls in one millisecond is admitted. Other actions keep the wall-clock time. A content action made in the same millisecond as such a burst therefore sorts before the burst's later grants, and is denied if it needs them. ## Scopes every holder reads The `auth` and `document` scopes are readable by anyone holding the document, whatever the grants say. A replica that could not read the policy would sync a document, see an uninitialized policy, and allow everything on it. Grants gate the domain scopes. Read filtering happens on the [`IReactorClient`](/academy/Reference/Reactor/ReactorClient), not inside the reactor, so `reactor.get()` and `client.get()` return different documents in the same process. A withheld scope is absent from `state` rather than present and empty. ## Refusal reasons A refused operation is stored with a `deniedReason` drawn from a closed set. Re-evaluation compares these strings, so they are consensus data and carry no grant id, subject or timestamp. | Reason | Meaning | | ----------------------------------- | ---------------------------------------------- | | `no grant permits this operation` | Default deny; nothing applied | | `denied by grant` | The last applicable grant had `effect: "deny"` | | `document deleted` | The document was deleted at this position | | `auth policy version unsupported` | The policy declares a version this build cannot evaluate | ## Limits A version-1 policy holds at most 100 grants. A condition holds at most 100 nodes and nests at most 10 deep. These bound the work a single decision can cost. ## Asking before you write [`evaluateActions`](/academy/Reference/Reactor/ReactorClient) predicts what the policy would decide about candidate operations without submitting them, so a control can be disabled rather than offered and refused. In React, [`useCanExecute`](/academy/Reference/EditorsUI/ReactHooks) wraps it. ## Related - [IReactorClient](/academy/Reference/Reactor/ReactorClient) — `evaluateActions` and the read methods the gate filters. - [React Hooks](/academy/Reference/EditorsUI/ReactHooks) — `useCanExecute`. - [Signing](/academy/Build/BuildingUserExperiences/Authorization/Signing) — where a subject's address and app key come from. - [Cookbook](/academy/Lookup/Cookbook) — runnable recipes: `document-acl`, `scoped-reads`, `group-principals`, `revocation-race`. --- ## Document erasure > Source: https://academy.vetra.io/academy/Reference/Reactor/DocumentErasure A Switchboard with the privacy add-on answers GDPR access and erasure requests. Access lists every document an address or app key appears in. Erasure removes whole documents: the reactor deletes every row it holds about the document and leaves one signed `PURGE_DOCUMENT` operation, the **marker**, in its place. The marker syncs like any other operation, so every peer that held the document deletes its copy too. Erasure works on documents, not on people. An address that signed operations in a document you keep stays in that document. To erase a person, erase the documents that hold them. **INFO:** The add-on runs only when `PH_PRIVACY_ENABLED=true`. With it off, nothing on this page is mounted or scheduled. ## Turning it on ```bash AUTH_ENABLED=true ADMINS="0x123...,0x456..." PH_PRIVACY_ENABLED=true # At least 32 bytes. Keys the hashes in the subject index and the audit log. PH_PRIVACY_DEPLOYMENT_SECRET= ``` Switchboard refuses to boot with the add-on on when: - the authorization policy is `OPEN`. Under `OPEN` every caller, anonymous included, is a supreme admin, so anyone could read disclosures and order erasures. `REQUIRE_AUTHENTICATED_CALLER` does not change this; turn on `AUTH_ENABLED` with `ADMINS`. - the reactor has no signer. Switchboard signs markers with its Renown identity, the keypair `ph login` created (`ph switchboard --use-identity`, or `--keypair-path `); without one, every receiver refuses the markers. - `PH_PRIVACY_DEPLOYMENT_SECRET` is missing or shorter than 32 bytes. `AUTH_ENABLED` with `ADMINS` alone selects the `ADMIN_ONLY` policy, under which only admins read documents: an anonymous Connect gets `404` from the drive URL and never syncs, so it never receives a marker. A Switchboard that serves Connect users needs `DOCUMENT_PERMISSIONS_ENABLED=true` as well, which lets anyone read and write unprotected documents while the `privacy` subgraph stays admin-only. See [Authorization](/academy/Build/BuildingUserExperiences/Authorization/Authorization). Keep the deployment secret. The index and the audit log store `HMAC-SHA256(secret, lower(identifier))`, never the identifier. Rotating the secret means rebuilding the index; losing it means old audit rows can no longer be matched to an address. Optional tuning: | Variable | Default | Meaning | | --- | --- | --- | | `PH_PRIVACY_DEADLINE_DAYS` | `30` | Days from the request after which a purge runs whether or not peers have caught up (GDPR Art. 12(3) allows one month) | | `PH_PRIVACY_MARKER_GRACE_DAYS` | `7` | Days after a purge that remotes and permission rows are kept while peers receive the marker | | `PH_PRIVACY_INTERVAL_MS` | `60000` | How often the scheduler runs. A finished purge also starts a run, so purges run back to back | | `PH_PRIVACY_PURGE_TIMEOUT_MINUTES` | `15` | Minutes a purge may run without committing before it is enqueued again | One Switchboard process per database. The scheduler, the queue and the sync mailboxes are in-process. ## Signer and Renown binding A peer admits a marker through the same signature check as any operation. When the receiver runs with `REACTOR_AUTH_ENFORCEMENT`, its trust policy is Renown: it accepts the marker's key only if a Renown credential binds the signer's user address to that key. A Switchboard whose identity is not bound in Renown produces markers every enforcing peer refuses. Check the binding before the first request. Pooled executor workers (`REACTOR_WORKERS`) sign with the same identity, loaded from the same keypair file. ## The admin API The add-on mounts a `privacy` subgraph on `/graphql`. Every field requires a caller whose address is in `ADMINS`; anyone else gets `FORBIDDEN`. ```graphql query { disclose(identifier: "0xabc...") { subjectHash documents { documentId role firstOrdinal lastOrdinal } boundSyncRemotes { name collectionId channelType } peerManifests { remoteName appKey } permissions { table column documentId detail } notCovered } } ``` `disclose` matches addresses case-insensitively. Roles are `signer`, `app-key`, `creator`, `header-key` and `named` (a grant principal, a condition literal or a group member). `notCovered` lists what the response cannot see, such as personal data inside document-model state. To erase: ```graphql query { erasurePlan(ids: ["drive-id"]) { maxPurgeOperations items { documentId expandedFrom live operationCount groupReferencers } } } mutation { requestErasure(ids: ["drive-id"]) { requestId status deadline items { documentId status } } } query { erasureRequest(requestId: "...") { status items { documentId status markerOrdinal lastError } } } ``` - **`erasurePlan`** is read-only. A drive expands to every document that was ever in it and has no open membership in another drive. For each document it reports the operation count against the cap and any surviving document whose auth history names it as a group. A group named that way cannot be purged; erase the referencing documents too. - **`requestErasure`** records the request and returns at once. It refuses the whole request, with `DocumentNotDeletedError` naming the ids, if any document is still live. Delete the documents first (a drive with cascade), then request again. `deadline` is an ISO 8601 timestamp; it defaults to now plus `PH_PRIVACY_DEADLINE_DAYS`. - **`erasureRequest`** reports progress. ## What the scheduler does Each item moves `waiting` → `purging` → `purged` → `erased`, or to `failed`. At most one item is `purging` at a time across all requests, and a drive waits until its members are purged. When a purge finishes, the scheduler runs again at once and starts the next one, so a large request is not limited to one purge per interval. | State | Moves on when | | --- | --- | | `waiting` | every remote owed the document's `DELETE_DOCUMENT` has acknowledged it, or the deadline has passed (recorded as `deadline-passed`). The purge job is enqueued. | | `purging` | the `document_purges` row for the document exists. A job that fails terminally moves the item to `failed` with the error name. A purge with no row after `PH_PRIVACY_PURGE_TIMEOUT_MINUTES` is enqueued again; a document is never purged twice. | | `purged` | every remote owed the marker has acknowledged it (`marker-converged`), or `markerGrace` has passed (`marker-undelivered`). The scheduler then removes the sync remotes bound to a purged drive and deletes the document's `DocumentPermission`, `OperationUserPermission` and `DocumentProtection` rows. A step that fails is retried on the next run. | A request becomes `complete` when every item is `erased` and `failed` when any item fails. The other items of a failed request still run. A job can fail while its purge still commits, for example when it times out. Each run checks `failed` items against `document_purges`: one whose row has appeared moves to `purged` and continues, and its request is `reopened` once none of its items is `failed`. After a failed or abandoned purge, the next purge waits until that purge's transaction has ended. Remotes and permission rows wait for the marker because removing either stops it: a removed remote never polls again, and an erased permission row can hide the document from a poller. A remote that is still owed the marker at `markerGrace` is removed anyway. The document's memberships stay open, so a peer that reconnects later still receives the marker. A remote is judged from its stored row, not only from the remotes that are running. A remote whose channel failed to start, for example because its host was unreachable at boot, counts as pending with the state `unknown` and never as delivered. If it is bound to a purged drive, it is deleted from storage at `markerGrace`, since nothing is running that could remove it. If this Switchboard has remotes but no sync manager to ask, the check fails closed and the deadline decides. At `markerGrace` the stored remotes bound to a purged drive are deleted the same way. ### The audit log `reactor.erasure_audit` is append-only (an `UPDATE` raises). Each row has the request, the document, an event and a `detail` column. Events: `requested`, `expanded`, `waiting` (the pending remotes, written each time the list changes), `deadline-passed`, `purged` (the marker ordinal and row counts), `marker-converged`, `marker-undelivered`, `remotes-removed`, `permissions-erased`, `failed`, `reopened` and `complete`. `detail` holds remote names, counts and error text. Any address or `did:key` in it, including one embedded in an error message, is replaced by its keyed hash. `requestedBy` is stored hashed. The audit log is not a document and never syncs. Give it a retention period of its own. ## Large documents A purge is one database transaction, and while it runs every read model's cursor, every catch-up sweep and every outbox stops advancing: settlement waits for the oldest open transaction. The executor refuses a document with more than `maxPurgeOperations` operations (default 200,000, about 1.6 s on a local Postgres) with `PurgeTooLargeError`, which fails the item. To erase a larger document, name it in `requestErasure(..., allowLarge: ["id"])` and accept the stall. The purge must also finish inside the executor's `jobTimeoutMs` (default 30 s). Past it the job is marked failed and so is the item; if the purge commits afterwards, the next run moves the item to `purged`. Switchboard does not expose `jobTimeoutMs`; a host that builds its own reactor sets it with `ReactorBuilder.withExecutorConfig({ jobTimeoutMs })`. ## Peers **Peers without erasure.** A peer on a build that does not announce the `document-purge` protocol cannot apply the marker. Its remote holds the marker instead (`waiting` reports it as `held`) and delivers it when the peer upgrades. Until then the peer keeps the document. At `markerGrace` its remote is removed; it receives the marker if it reconnects after upgrading. A relay without erasure blocks the peers behind it. **Refused markers.** A peer whose trust policy rejects this Switchboard's key refuses the marker and keeps the document. The scheduler records that remote as `marker-undelivered` even though its cursor moves past the marker. A refusal counts whichever way the marker travelled: a marker this Switchboard pushed is reported back on the next poll, and a client that polled the marker reports its refusal on its next poll. Each refusal is stored in `reactor.sync_purge_refusals` (remote, document, branch and time, no error text), so one reported while the scheduler is stopped still counts. A client on a build before marker refusal reporting does not report its refusal. Such a remote reads as converged once its cursor passes the marker. A client of this build holds its report for a Switchboard of an older build until that Switchboard announces the `marker-refusal` feature. **Pushed markers.** A marker can also arrive from a client that pushes it. Under `DOCUMENT_PERMISSIONS`, a pushed `PURGE_DOCUMENT` requires a document admin: a supreme admin, the document's owner, or an `ADMIN` grant on that document. `OperationUserPermission` rows cannot widen this. Under `OPEN` every caller passes, so an open Switchboard lets anyone who can write a document purge it, and with `REACTOR_AUTH_ENFORCEMENT` off any key that verifies is trusted. Do not run an open Switchboard that syncs documents you care about. **Groups across peers.** The purging Switchboard checks group references against the documents it holds. A peer holding a document that names the group, which this Switchboard never received, re-judges that document without the group, and its access decisions there can differ from its peers'. ## Read models on another database Models on the reactor's database are fenced: a purge waits for them and they wait for a purge, so no row survives it. Models whose rows live on another database handle (the attachment reference index, third-party relational processors) skip purged ids but can still write a row from a batch fetched just before the purge committed. Repair one by rescanning it from just below the marker, from `packages/reactor` of the monorepo: ```bash pnpm catchup rescan --pg --from --consumer ``` `markerOrdinal` is on the item (`erasureRequest`) and in the `purged` audit row. ## Outside the reactor Erasure does not reach: - database backups, WAL archives and point-in-time recovery. Expire them on a schedule that matches your deadline. - Connect's IndexedDB copies and browser backups. - attachment bytes still referenced by another document. - processors that ignore deletions. --- ## Undo and redo > Source: https://academy.vetra.io/academy/Reference/Reactor/UndoRedo How a document stores an undo depends on its base-reducer version, `header.protocolVersions["base-reducer"]`. The version is fixed by `CREATE_DOCUMENT` and is part of the derived document id, so a document never changes version. | Version | `UNDO` is stored as | `REDO` | | ------- | ----------------------------------------------------- | ------------------------------------------------------------------------------- | | 1, 2 | a `NOOP` whose `skip` removes the operation before it | replays the undone action from the document's clipboard; version 2 refuses it once the stream holds an undo | | 3 | refused; undo is a `RETRACT { target }` the author signs, naming the undone action by id | refused; redo is a `RETRACT` of the retraction | On version 3 a retraction names its target, so where it lands in the stream does not change what it undoes. Replicas that hold the same operations derive the same state, whatever order the operations arrived in. **INFO:** Base-reducer 3 needs the `undoV3` reactor flag, which defaults to on. A reactor with it off refuses to create a version 3 document and never receives one from a peer. A reactor with it on still refuses a version 3 document whose model it cannot run as version 3: the model's reducer is not built by `createReducer`, or the model cannot be loaded. The refusal is an `UnsupportedProtocolVersionError`, on writes, on reads, and on receipt from a peer before any load. A reactor that stores version 3 documents of a model it cannot run refuses to start and names the type (`base-reducer 3 for `). One without `undoV3` refuses to start too, naming the version and how many stored documents need it (`N stored document(s) require base-reducer 3, which this reactor does not run`). `withUnsupportedStoredDocuments("read-only")` starts either with those documents read-only. A model loaded after start is checked when it loads, and a refusal is logged and emitted as `STORED_MODEL_REFUSED`. ## Turning it off | Host | Setting | | ----------- | ------------------------------------------------------------------------- | | Switchboard | `REACTOR_UNDO_V3=false`, or `protocol.featureFlags.undoV3: false` in its config | | Connect | `connect.reactor.featureFlags.undoV3: false` in `powerhouse.config.json` | | Library | `ReactorBuilder.withExecutorConfig({ featureFlags: { undoV3: false } })` | | ph-cli host | `reactor.protocol.featureFlags.undoV3: false` in the host file (projected to `REACTOR_UNDO_V3`) | `undoV3` has no prerequisites. A peer that runs without it is held from version 3 documents until it upgrades. Turning it off only stops new documents taking version 3: a host with it off that stores a version 3 document refuses to start, unless `REACTOR_UNSUPPORTED_STORED_DOCUMENTS=read-only` (or `withUnsupportedStoredDocuments("read-only")`) starts it with those documents read-only. Version 1 and 2 documents behave the same with the flag on or off. ## Versions this build does not know A document whose base-reducer number this build does not know is read, derived and synced as the highest version the reader runs. A reactor, and its in-process client, use the highest version the reactor supports: 3 with `undoV3` on, 2 with it off. The GraphQL light client and a model's reducer use 3. A reactor still refuses a write to such a document unless its protocol support lists the number. ## Which version a document takes A document's version is fixed when it is created, and its creator chooses it. A client chooses in one place, `newDocument` on the read half, which heads an uncreated document with the versions `getCreateProtocolVersions` selects: version 3 when `undoV3` is on and the model's reducer is built by `createReducer`, otherwise version 2. `createEmpty` is `newDocument` then `create`, and `create` takes the header as given. The members of the parent drive do not narrow the base-reducer version: a peer that cannot run it is held from the document until it upgrades. A reactor that does not run the chosen version refuses the document. A call can name a version: ```typescript const document = await client.read.newDocument("powerhouse/todo-list", { parentIdentifier: driveId, protocolVersions: { "base-reducer": 2 }, }); await client.write.drives.addFile(driveId, document); ``` A document headed outside a client, such as one from a model's `utils.createDocument()`, takes version 2, and keeps it when it is created or added to a drive. `IReactor.create` gives a header without protocol versions the versions the reactor selects for its type, as `newDocument` does: 3 for a `createReducer` model with `undoV3` on, otherwise 2. A stored history or an imported `.phd` file keeps the versions it carries, and a `.phd` file without them is replayed at version 1. The GraphQL light client takes the Switchboard's `createDefaults` answer for the document's type and parent as given, so a remote document's version follows the Switchboard's `undoV3`. It caps the answer at the versions its own build runs, so a newer Switchboard's base-reducer 4 becomes 3, and refuses a malformed answer or a version of another known protocol it does not run. It then fits base-reducer down to what its own `documentModels` can run. A Switchboard from before per-type selection answers for every type; against one without the query the client picks version 2, but cannot create there: it also reads the signature policy from that query. An existing document keeps its version. To move one to version 3, copy it through `client.write.drives.copyNode` with the version to take: ```typescript await client.write.drives.copyNode(driveId, nodeId, targetFolderId, undefined, { protocolVersions: { "base-reducer": 3 }, }); ``` The copy starts from the source's current state, so the source's history cannot be undone in it. A copy without `protocolVersions` keeps its source's version. `ReactorDriveClient` replays the source's history into the copy and refuses `protocolVersions`. ## Undoing from a client Call [`undo`, `redo` and `undoRedoAvailability`](/academy/Reference/Reactor/ReactorClient#undo) on the write half, `client.write`. They work on every version; `undo` and `redo` return a `WriteHandle`. On version 3 the client resolves the target with `undoRedoTargets` and submits a `RETRACT` of it signed by the client's own signer. On versions 1 and 2 it submits `UNDO` or `REDO`. The GraphQL light client does the same, resolving the target through the Switchboard's `undoRedoTargets` query; see [the light client's methods](/academy/Reference/ReactorBrowserClient/ApiReference#methods). Either way the author signs, so a policy must grant the author `RETRACT` for undo on version 3. `RemoteDocumentController` from `@powerhousedao/reactor-browser` runs the version the remote document's header names. Its `undo(count = 1, scope = "global")` and `redo(count = 1, scope = "global")` keep their signature. On versions 1 and 2, and on a header without versions, they apply `UNDO` or `REDO` locally, as before. On version 3 each step resolves its target from the controller's own log, including actions not yet pushed, and applies a `RETRACT` that its signer signs on push; every target resolves before anything applies, and a `count` that is not a positive integer throws a `RangeError`. A new document is headed with the host's versions for its type and created from that header on its first push, so undo before the push follows the same version. The `RETRACT` names the row the call saw as the latest, so an edit that lands in between is not undone. If the target is retracted or gone by the time the `RETRACT` runs, the job fails with `RetractionRefusedError`, and the handle's stages reject with a `JobFailedError` naming it; the client does not resolve again or retry. To retract a specific action, submit `retract` yourself. It is exported from `document-model` and `@powerhousedao/shared/document-model`: ```typescript await client.write.execute(documentId, "main", [retract(actionId, "global")]); ``` A submitted `UNDO` or `REDO` on version 3 fails with `SubmittedUndoRefusedError`. The model's reducer refuses one the same way, so `reducer(document, undo())` on a version 3 document throws `SubmittedUndoRefusedError` rather than retracting anything. On versions 1 and 2 the reactor still resolves one and signs the `NOOP` it stores with its own signer. ## Rules on version 3 - An action is live if no live `RETRACT` names it. Liveness depends on which operations are present, not on their order. - Undo targets the last applied content operation in the scope, by any author. - Redo targets the last live retraction of content. Content applied after the undo leaves nothing to redo. - `SET_NAME` and `SET_PREFERRED_EDITOR` cannot be undone. Undo passes over them, and a `RETRACT` naming one is stored but retracts nothing. - A denied `RETRACT` retracts nothing. - An operation that throws applies nothing. Undo passes over it. - `skip` is only the rewind a reshuffle writes. It never undoes anything. - Operations with the same timestamp are ordered by action id. Action ids made in one process sort in creation order, so one client's same-millisecond actions keep their order. The guarantee is per process: actions made in different processes, such as two workflow piece workers or a Connect tab and its shared worker, are ordered by id with no guarantee of creation order. - A job's actions run in this order, not in array order. The reactor sorts every job on a version 3 document the same way its peers sort what they receive. On execute, a `RETRACT` whose target is not in the scope's stream, or is already retracted, is refused. On sync it is admitted: a `RETRACT` that arrives before its target does nothing until the target arrives. The rules do not run in the `auth` and `document` scopes, or in version 1 and 2 documents. On a version 3 document, a `RETRACT` submitted to the `auth` or `document` scope is refused. On version 1 and 2 documents a `RETRACT` is an ordinary action, as before this release: it is stored and changes nothing. A `RETRACT` received from a peer into a scope the rules do not run is stored and does nothing. ## Errors | Error | Fields | Thrown when | | ------------------------ | -------------------------------- | --------------------------------------------------------------------------- | | `RetractionRefusedError` | `documentId`, `target`, `reason` | A submitted `RETRACT` names an absent target (`"target-missing"`) or one already retracted (`"target-retracted"`) | | `ReservedActionError` | `documentId`, `actionType` | A write carries an action type the client may not submit, such as a `RETRACT` to the `auth` or `document` scope of a version 3 document | | `SubmittedUndoRefusedError` | `documentId`, `actionType` | A write, or a peer's load, carries `UNDO` or `REDO` to a version 3 document | | `UndoOperationRefusedError` | `documentId`, `scope` | A peer's load carries an `UNDO` operation to a version 1 or 2 document, which stores an undo as a `NOOP` | All four are exported from `@powerhousedao/reactor` and are terminal: the job fails without a retry. Through the write half they reach you as a `JobFailedError` whose `job.error.name` names the class; see [Error handling](/academy/Reference/Reactor/ErrorHandling). `undo` and `redo` on version 3 throw `NoUndoRedoTargetError` (from `document-model`, message `Cannot undo: nothing to undo in scope history`, or `redo`) before submitting anything when there is no target. --- ## PHDocument Migration Guide > Source: https://academy.vetra.io/academy/Reference/DocumentModels/PHDocumentMigrationGuide **TIP:** This guide covers the **breaking changes** introduced in Powerhouse v4.0.0 related to PHDocument structure changes. If you're upgrading from v3.2.0 or earlier, **this migration is required** and document models must be regenerated. ## Overview Version 4.0.0 introduced a significant refactor of the `PHDocument` structure that consolidates document metadata into a `header` field. This change enables signed and unsigned documents with cryptographic verification capabilities, but requires updating all code that accesses document properties. ## What Changed ### Document Structure Refactor The most significant change is the consolidation of document metadata into a `header` field. Previously, document properties were scattered at the root level of the document object. **Before (v3.2.0 and earlier):** ```javascript const document = { id: "doc-123", created: "2023-01-01T00:00:00.000Z", lastModified: "2023-01-01T12:00:00.000Z", revision: 5, documentType: "powerhouse/todo-list", name: "My Todo List", slug: "my-todo-list", // ... other properties }; ``` **After (v4.0.0):** ```javascript const document = { header: { id: "doc-123", createdAtUtcIso: "2023-01-01T00:00:00.000Z", lastModifiedAtUtcIso: "2023-01-01T12:00:00.000Z", revision: { global: 5, local: 0 }, documentType: "powerhouse/todo-list", name: "My Todo List", slug: "my-todo-list", branch: "main", sig: { nonce: "", publicKey: {} }, meta: {}, }, // ... other properties }; ``` ## Complete Property Migration Map | **Old Property** | **New Property** | **Additional Changes** | | ----------------------- | -------------------------------------- | ------------------------------------------------------------------- | | `document.id` | `document.header.id` | Now an Ed25519 signature for signed documents | | `document.created` | `document.header.createdAtUtcIso` | **Renamed** to include UTC ISO specification | | `document.lastModified` | `document.header.lastModifiedAtUtcIso` | **Renamed** to include UTC ISO specification | | `document.revision` | `document.header.revision` | Now an **object** with scope keys (e.g., `{ global: 5, local: 0 }`) | | `document.documentType` | `document.header.documentType` | No additional changes | | `document.name` | `document.header.name` | No additional changes | | `document.slug` | `document.header.slug` | No additional changes | | `document.branch` | `document.header.branch` | Now explicitly included | | `document.meta` | `document.header.meta` | Now explicitly included | | N/A | `document.header.sig` | **New** - Signature information for document verification | ## Step-by-Step Migration Guide ### Step 1: Update Document Property Access Replace all instances of direct property access with header-based access:
**Common Property Access Patterns** **Document ID Access:** ```javascript // Before const documentId = document.id; // After const documentId = document.header.id; ``` **Document Name Access:** ```javascript // Before const documentName = document.name; // After const documentName = document.header.name; ``` **Document Type Access:** ```javascript // Before const docType = document.documentType; // After const docType = document.header.documentType; ``` **Timestamp Access:** ```javascript // Before const created = document.created; const lastModified = document.lastModified; // After const created = document.header.createdAtUtcIso; const lastModified = document.header.lastModifiedAtUtcIso; ``` **Revision Access:** ```javascript // Before const revision = document.revision; // Was a number // After const globalRevision = document.header.revision.global; // Now an object const localRevision = document.header.revision.local; // Or get all revisions const allRevisions = document.header.revision; // { global: 5, local: 0, ... } ```
### Step 2: Update Component Code **React Components:**
**Example: Document List Component** ```jsx // Before function DocumentList({ documents }) { return (
{documents.map((doc) => (

{doc.name}

Type: {doc.documentType}

Last modified: {new Date(doc.lastModified).toLocaleDateString()}

Revision: {doc.revision}

))}
); } // After function DocumentList({ documents }) { return (
{documents.map((doc) => (

{doc.header.name}

Type: {doc.header.documentType}

Last modified:{" "} {new Date(doc.header.lastModifiedAtUtcIso).toLocaleDateString()}

Global Revision: {doc.header.revision.global}

))}
); } ```
### Step 3: Update Type Definitions If you're using TypeScript, update your type definitions:
**TypeScript Interface Updates** ```typescript // Before interface MyDocument { id: string; name: string; documentType: string; created: string; lastModified: string; revision: number; // ... other properties } // After interface MyDocument { header: { id: string; name: string; documentType: string; createdAtUtcIso: string; lastModifiedAtUtcIso: string; revision: { [scope: string]: number; }; slug: string; branch: string; sig: { nonce: string; publicKey: any; }; meta?: { preferredEditor?: string; }; }; // ... other properties } ```
### Step 4: Database Queries and APIs Compatibility
**GraphQL Query Compatibility** **GraphQL Queries:** ```graphql # Your existing queries continue to work unchanged query GetDocument($id: ID!) { document(id: $id) { id # Still works due to response transformation name # Still works due to response transformation documentType # Still works due to response transformation created # Still works due to response transformation lastModified # Still works due to response transformation revision # Still works due to response transformation } } ``` **TIP:** **GraphQL Backward Compatibility:** The GraphQL API maintains backward compatibility through response transformation. Your existing queries will continue to work without changes. However, when working with the raw document objects in your application code, you'll need to use the new header structure.
## Common Migration Issues and Solutions ### Issue 1: Undefined Property Errors **Problem:** Getting `undefined` when accessing document properties. **Solution:** Update property access to use the header structure: ```javascript // This will be undefined after migration const name = document.name; // Use this instead const name = document.header.name; ``` ### Issue 2: Revision Type Mismatch **Problem:** Code expecting revision to be a number but getting an object. **Solution:** Update revision access to specify the scope: ```javascript // Before - revision was a number if (document.revision > 5) { ... } // After - revision is an object with scope keys if (document.header.revision.global > 5) { ... } ``` ### Issue 3: Date Format Changes **Problem:** Date parsing issues due to property name changes. **Solution:** Update timestamp property names: ```javascript // Before const createdDate = new Date(document.created); const modifiedDate = new Date(document.lastModified); // After const createdDate = new Date(document.header.createdAtUtcIso); const modifiedDate = new Date(document.header.lastModifiedAtUtcIso); ``` ## Testing Your Migration ### Automated Testing Create tests to verify your migration:
**Migration Test Examples** ```javascript // Test document property access describe("Document Migration", () => { it("should access document properties correctly", () => { const mockDocument = { header: { id: "test-id", name: "Test Document", documentType: "powerhouse/test", createdAtUtcIso: "2023-01-01T00:00:00.000Z", lastModifiedAtUtcIso: "2023-01-01T12:00:00.000Z", revision: { global: 5, local: 0 }, // ... other header properties }, // ... other document properties }; // Test property access expect(mockDocument.header.id).toBe("test-id"); expect(mockDocument.header.name).toBe("Test Document"); expect(mockDocument.header.revision.global).toBe(5); }); }); ```
## Related Documentation - [PHDocument Architecture](/academy/Reference/Architecture/PowerhouseArchitecture) - [Document Model Creation](/academy/Build/DocumentModelCreation/WhatIsADocumentModel) - [Document Model Versioning](/academy/Build/DocumentModelCreation/DocumentModelVersioning) - For evolving document schemas over time - [React Hooks](/academy/Reference/EditorsUI/ReactHooks) --- _This migration guide covers the major changes in v4.0.0. For additional technical details, refer to the [RELEASE-NOTES.md](https://github.com/powerhouse-dao/powerhouse/blob/main/RELEASE-NOTES.md) in the main repository._ --- ## Relational Database > Source: https://academy.vetra.io/academy/Reference/GraphQLData/RelationalDatabase This page covers the relational database tools available in Powerhouse applications, providing type-safe database operations with real-time updates through PGlite integration.
Introduction for New Developers ### What is the Relational Database API? This API helps you build applications that need to store, query, and analyze data from Powerhouse documents using traditional database tools. If you're familiar with SQL databases like PostgreSQL or MySQL, this will feel familiar while adding powerful real-time capabilities. ### When Should You Use This API? **Perfect for:** - Building dashboards that show live data - Creating reports and analytics - Integrating with existing business tools - Applications needing complex data queries - Systems requiring audit trails of document changes **Not needed for:** - Simple document viewing or editing - Basic CRUD operations on individual documents - Applications with minimal data analysis needs ### Learning Path **New to relational database processors?** Start with our step-by-step tutorial: [Building a Relational Database Processor](/academy/Build/WorkWithData/RelationalDbProcessor) - it walks you through creating a complete todo-list processor from scratch. **Ready to implement?** Use this API reference for detailed function signatures, parameters, and advanced patterns. ### Quick Overview: How It Works 1. **Define your data structure** - Describe what information you want to track 2. **Set up processing** - Configure automatic data extraction from document changes 3. **Query your data** - Use familiar database operations with real-time updates 4. **Build your UI** - Connect to any frontend framework or reporting tool ### Key Concepts (Simplified) - **Processors**: Background workers that automatically organize document data - **Live Queries**: Database queries that update automatically when data changes - **Type Safety**: Built-in error prevention that catches mistakes before they happen - **Hooks**: Ready-to-use functions for common data access patterns
## Overview The relational database layer gives you powerful tools to work with data in your Powerhouse applications. You get type-safe queries, real-time updates, and a simple API that feels familiar to React developers. **Key Benefits:** - **Type-safe queries** with full TypeScript support - **Live query capabilities** with real-time updates - **Automatic optimization** to prevent infinite re-renders - **Simple API** that abstracts away complexity - **Smart memorization** for parameters and queries ## Quick Start
Setting up your first relational database query ### Step 1: Define your database schema ```typescript type MyDatabase = { users: { id: number; name: string; email: string; }; posts: { id: number; title: string; content: string; author_id: number; }; }; ``` ### Step 2: Create a typed query hook ```typescript // Create a typed query hook for your processor const useTypedQuery = createProcessorQuery(MyProcessor); ``` ### Step 3: Use it in your component ```typescript // Simple query - no parameters needed export function useUserList(driveId: string) { return useTypedQuery(driveId, (db) => { return db.selectFrom("users").selectAll().compile(); }); } // Query with parameters export function useUserById(driveId: string, userId: number) { return useTypedQuery( driveId, (db, params) => { return db .selectFrom("users") .selectAll() .where("id", "=", params.userId) .compile(); }, { userId }, ); } ``` ### Step 4: Use in your React component ```typescript function UserList({ driveId }: { driveId: string }) { const { isLoading, error, result } = useUserList(driveId); if (isLoading) return
Loading...
; if (error) return
Error: {error.message}
; if (!result) return
No data
; return (
    {result.rows.map(user => (
  • {user.name} - {user.email}
  • ))}
); } ```
## Core Hooks ### 1. createProcessorQuery()
`createProcessorQuery(ProcessorClass)`: Creates a typed query hook factory for your processor ### Function Name and Signature ```typescript function createProcessorQuery( ProcessorClass: RelationalDbProcessorClass, ): TypedQueryHook; ``` ### Description Creates a typed query hook factory for a specific processor class. This is the main function you'll use to create hooks for querying your relational database. ### Usage Example ```typescript // Create a typed query hook for your processor const useTypedQuery = createProcessorQuery(MyProcessor); // Use it to create specific query hooks export const useUsers = (driveId: string) => { return useTypedQuery(driveId, (db) => { return db.selectFrom("users").selectAll().compile(); }); }; // With parameters export const useUsersByStatus = (driveId: string, status: string) => { return useTypedQuery( driveId, (db, params) => { return db .selectFrom("users") .selectAll() .where("status", "=", params.status) .compile(); }, { status }, ); }; ``` ### Parameters The returned hook accepts: - `driveId`: The ID of the drive - `queryCallback`: Function that receives the database instance and optional parameters - `parameters`: Optional parameters for the query ### Return Value ```typescript { isLoading: boolean; // True while loading or retrying error: Error | null; // Any error that occurred result: LiveQueryResults | null; // Query results with live updates } ``` ### Notes / Caveats - Create one `useTypedQuery` hook per processor - The hook includes automatic retry logic for common errors - Parameters are automatically memoized - Queries are live and will update automatically when data changes
### 2. useRelationalDb()
`useRelationalDb()`: Access the enhanced database instance directly ### Hook Name and Signature ```typescript function useRelationalDb(): IRelationalDbState; ``` ### Description Provides direct access to the enhanced Kysely database instance with live query capabilities. Use this when you need to perform relational database operations outside of the typical query patterns. ### Usage Example ```typescript function DatabaseOperations() { const { db, isLoading, error } = useRelationalDb(); const createUser = async (name: string, email: string) => { if (!db) return; // Direct database operations await db .insertInto('users') .values({ name, email }) .execute(); }; if (isLoading) return
Database initializing...
; if (error) return
Database error: {error.message}
; return ( ); } ``` ### Parameters - `Schema` - TypeScript type defining your database schema ### Return Value ```typescript { db: RelationalDbWithLive | null; // Enhanced Kysely instance with live capabilities isLoading: boolean; // True while database is initializing error: Error | null; // Any initialization error } ``` ### Notes / Caveats - Always check if `db` is not null before using it - The database instance includes both Kysely methods and live query capabilities - Use this for direct database operations like inserts, updates, and deletes - For queries, prefer `createProcessorQuery()` which provides better optimization ### Related Hooks - [`createProcessorQuery`](#1-createprocessorquery) - For optimized queries - [`useRelationalQuery`](#3-userelationalquery) - For manual query control
### 3. useRelationalQuery()
`useRelationalQuery()`: Lower-level hook for manual query control ### Hook Name and Signature ```typescript function useRelationalQuery( ProcessorClass: RelationalDbProcessorClass, driveId: string, queryCallback: ( db: IRelationalQueryBuilder, parameters?: TParams, ) => QueryCallbackReturnType, parameters?: TParams, options?: useRelationalQueryOptions, ): { isLoading: boolean; error: Error | undefined; result: LiveQueryResults | null; }; ``` ### Description Lower-level hook for creating live queries with manual control over the query callback and parameters. Most developers should use `createProcessorQuery()` instead, but this hook is useful for advanced use cases. ### Usage Example ```typescript function UserCount() { const { result, isLoading, error } = useRelationalQuery( MyProcessorClass, driveId, (db) => { return db .selectFrom('users') .select(db.fn.count('id').as('count')) .compile(); } ); if (isLoading) return
Loading...
; if (error) return
Error: {error.message}
; return
User count: {result?.rows[0]?.count ?? 0}
; } ``` ### Parameters - `ProcessorClass` - The processor class with the relational DB schema - `driveId` - The drive ID to scope the query - `queryCallback` - Function that receives the database instance and optional parameters - `parameters` - Optional parameters for the query - `options` - Optional `useRelationalQueryOptions` (e.g. `hashNamespace`) ### Return Value ```typescript { result: LiveQueryResults | null; // Live query results isLoading: boolean; // Combined loading state error: Error | null; // Any error that occurred } ``` ### Notes / Caveats - This hook doesn't include automatic parameter memoization - Use `createProcessorQuery()` for better developer experience and optimization - Useful for cases where you need manual control over the query lifecycle ### Related Hooks - [`createProcessorQuery`](#1-createprocessorquery) - Recommended higher-level API - [`useRelationalDb`](#2-usedelationaldb) - For direct database access
### 4. useRelationalQueryOptions ```typescript export type useRelationalQueryOptions = { hashNamespace?: boolean }; ``` _Description pending — see source._ ## Advanced Patterns ### Working with Dynamic Parameters
How to handle parameters that change over time ### Problem You need to create queries that update automatically when search terms, filters, or other parameters change. ### Solution The `createProcessorQuery` hook automatically handles parameter changes and memoizes them using deep comparison: ```typescript function useSearchResults(driveId: string) { const [searchTerm, setSearchTerm] = useState(""); const [category, setCategory] = useState("all"); // Query automatically updates when searchTerm or category changes const result = useTypedQuery( driveId, (db, params) => { let query = db.selectFrom("products").selectAll(); if (params.searchTerm) { query = query.where("name", "like", `%${params.searchTerm}%`); } if (params.category !== "all") { query = query.where("category", "=", params.category); } return query.compile(); }, { searchTerm, category }, ); return { result, setSearchTerm, setCategory }; } ``` ### Key Points - Parameters are automatically memoized using deep comparison - No need to wrap parameters in `useMemo` - Query re-runs only when parameter values actually change - Works with complex nested objects
### Custom SQL Queries
Using raw SQL instead of Kysely query builder ### Problem You need to write complex SQL queries that are easier to express in raw SQL than using the Kysely query builder. ### Solution You can return raw SQL queries from your callback: ```typescript function useCustomUserStats(driveId: string) { return useTypedQuery(driveId, () => { return { sql: ` SELECT u.name, COUNT(p.id) as post_count, MAX(p.created_at) as last_post_date FROM users u LEFT JOIN posts p ON u.id = p.author_id GROUP BY u.id, u.name ORDER BY post_count DESC `, }; }); } // With parameters function useUserPostsByDateRange( driveId: string, startDate: string, endDate: string, ) { return useTypedQuery( driveId, (db, params) => { return { sql: ` SELECT p.*, u.name as author_name FROM posts p JOIN users u ON p.author_id = u.id WHERE p.created_at BETWEEN $1 AND $2 ORDER BY p.created_at DESC `, parameters: [params.startDate, params.endDate], }; }, { startDate, endDate }, ); } ``` ### Key Points - Return an object with `sql` and optional `parameters` properties - Use parameterized queries ($1, $2, etc.) for dynamic values - You can mix Kysely and raw SQL approaches in the same application
### Complex Joins and Relationships
Working with related data across multiple tables ### Problem You need to fetch related data from multiple tables with complex relationships. ### Solution Use Kysely's join capabilities within your query callbacks: ```typescript function useUsersWithPosts(driveId: string) { return useTypedQuery(driveId, (db) => { return db .selectFrom("users") .leftJoin("posts", "users.id", "posts.author_id") .select([ "users.id", "users.name", "users.email", "posts.title as post_title", "posts.content as post_content", ]) .compile(); }); } // More complex example with multiple joins and aggregations function useUserDashboardData(driveId: string, userId: number) { return useTypedQuery( driveId, (db, params) => { return db .selectFrom("users") .leftJoin("posts", "users.id", "posts.author_id") .leftJoin("comments", "posts.id", "comments.post_id") .select([ "users.id", "users.name", "users.email", db.fn.count("posts.id").as("post_count"), db.fn.count("comments.id").as("comment_count"), ]) .where("users.id", "=", params.userId) .groupBy(["users.id", "users.name", "users.email"]) .compile(); }, { userId }, ); } ``` ### Key Points - Use Kysely's join methods for related data - Leverage aggregation functions for counts and calculations - Type safety is maintained throughout complex queries
## Best Practices ### 1. Schema Definition
How to properly define your database schema types Always define clear TypeScript interfaces for your database schema: ```typescript // ✅ Good - Clear, typed schema type AppDatabase = { users: { id: number; name: string; email: string; created_at: Date; updated_at: Date; }; posts: { id: number; title: string; content: string; author_id: number; published: boolean; created_at: Date; }; }; // ❌ Avoid - Vague or missing types type BadDatabase = { users: any; posts: Record; }; ```
### 2. Hook Organization
How to organize your database hooks Create focused, reusable hooks for different data access patterns: ```typescript // ✅ Good - Focused, reusable hooks export function useUsers(driveId: string) { return useTypedQuery(driveId, (db) => db.selectFrom("users").selectAll().compile(), ); } export function useUserById(driveId: string, id: number) { return useTypedQuery( driveId, (db, params) => db.selectFrom("users").selectAll().where("id", "=", params.id).compile(), { id }, ); } export function useActiveUsers(driveId: string) { return useTypedQuery(driveId, (db) => db.selectFrom("users").selectAll().where("active", "=", true).compile(), ); } // ❌ Avoid - Too generic or complex export function useEverything(driveId: string) { return useTypedQuery(driveId, (db) => db .selectFrom("users") .leftJoin("posts", "users.id", "posts.author_id") .leftJoin("comments", "posts.id", "comments.post_id") .selectAll() // Too much data .compile(), ); } ```
### 3. Error Handling
How to handle loading states and errors Always handle loading and error states in your components: ```typescript function UserList() { const { isLoading, error, result } = useUsers(); // ✅ Good - Handle all states if (isLoading) return ; if (error) return ; if (!result) return ; return (
    {result.rows.map(user => (
  • {user.name}
  • ))}
); } // ❌ Avoid - Missing error handling function BadUserList() { const { result } = useUsers(); return (
    {result?.rows.map(user => (
  • {user.name}
  • ))}
); } ```
### 4. Performance Optimization
Tips for optimal query performance - **Keep queries focused**: Don't select unnecessary columns or join too many tables - **Use parameters wisely**: The automatic memoization handles most cases, but avoid creating new objects unnecessarily - **Consider query frequency**: For data that changes rarely, consider caching strategies ```typescript // ✅ Good - Focused query function useUserNames(driveId: string) { return useTypedQuery(driveId, (db) => db .selectFrom("users") .select(["id", "name"]) // Only what you need .compile(), ); } // ✅ Good - Stable parameters function useUsersByStatus(driveId: string, status: string) { return useTypedQuery( driveId, (db, params) => db .selectFrom("users") .selectAll() .where("status", "=", params.status) .compile(), { status }, // Simple, stable parameter ); } // ❌ Avoid - Unnecessary data function useEverythingAboutUsers(driveId: string) { return useTypedQuery(driveId, (db) => db .selectFrom("users") .leftJoin("posts", "users.id", "posts.author_id") .selectAll() // Too much data .compile(), ); } ```
## Common Issues and Solutions ### Query Not Updating
My query results aren't updating when I expect them to ### Problem Your query results don't update when you expect them to, even though you've changed parameters. ### Solution Check that your parameters are actually changing in content, not just reference: ```typescript // ✅ Good - Parameters change in content const [userId, setUserId] = useState(1); const result = useUserById(driveId, userId); // Updates when userId changes // ❌ Common mistake - Same content, different objects const result = useTypedQuery( driveId, (db, params) => /* query */, { userId: user.id } // New object every render, but same content ); // ✅ Better - Extract stable values const userId = user.id; const result = useTypedQuery( driveId, (db, params) => /* query */, { userId } // Stable parameter ); ``` ### Debugging Tips - Log your parameters to see if they're actually changing - Check the `isLoading` state to see if queries are re-running - Use React DevTools to inspect hook state changes
### Type Errors
Getting TypeScript errors with my queries ### Problem TypeScript is showing errors about query return types or database schema. ### Solution Make sure your callback returns the correct type: ```typescript // ✅ Good - Returns QueryCallbackReturnType const result = useTypedQuery(driveId, (db) => { return db.selectFrom("users").selectAll().compile(); // Has sql property }); // ❌ Error - Missing .compile() const result = useTypedQuery(driveId, (db) => { return db.selectFrom("users").selectAll(); // No sql property }); // ✅ Good - Raw SQL format const result = useTypedQuery(driveId, () => { return { sql: "SELECT * FROM users", parameters: [], }; }); ```
### Performance Issues
My queries are running too frequently or causing lag ### Problem Your queries are running more often than expected, causing performance issues. ### Solution Check for unstable parameters or overly complex queries: ```typescript // ❌ Problem - New object every render function BadComponent({ driveId, user }) { const result = useTypedQuery( driveId, (db, params) => /* query */, { filter: { status: 'active', dept: user.department } // New object each render } ); } // ✅ Solution - Stable parameters function GoodComponent({ driveId, user }) { const filter = useMemo(() => ({ status: 'active', dept: user.department }), [user.department]); const result = useTypedQuery( driveId, (db, params) => /* query */, { filter } ); } // ✅ Even better - Direct values function BetterComponent({ driveId, user }) { const result = useTypedQuery( driveId, (db, params) => /* query */, { status: 'active', dept: user.department } ); } ```
## Further Reading - [Kysely Documentation](https://kysely.dev/) - Learn more about the query builder - [PGlite Documentation](https://pglite.dev/) - Understanding the underlying database - [React Hooks](/academy/Reference/EditorsUI/ReactHooks) - Other available hooks in Powerhouse - [Component Library](/academy/Reference/EditorsUI/DocumentEngineering) - Building UI components ## Related Hooks - [`useDocuments`](/academy/Reference/EditorsUI/ReactHooks#usedocuments) - Working with Powerhouse documents - [`useDrives`](/academy/Reference/EditorsUI/ReactHooks#usedrives) - Managing document drives - [`useSelectedDocument`](/academy/Reference/EditorsUI/ReactHooks#useselecteddocument) - Document selection state --- ## Subgraph Migration Guide (v6) > Source: https://academy.vetra.io/academy/Reference/GraphQLData/SubgraphMigrationGuide **TIP:** This guide covers the **breaking changes** to the GraphQL subgraph API introduced in the v6 Reactor. If you were querying the old `/graphql/document-drive` or `/graphql/system` endpoints, or building custom subgraphs, **this migration is required**. ## Overview The v6 Reactor replaced the legacy hardcoded subgraphs (`document-drive`, `system`) with a new architecture: - **Reactor subgraph** (`/graphql/r`) — manages drives, documents, sync, and subscriptions - **Document-model subgraphs** (`/graphql/`) — auto-generated per document model, with namespaced queries and mutations - **Custom subgraphs** — user-defined subgraphs now extend `BaseSubgraph` with `reactorClient` ## Endpoint changes | Legacy endpoint | v6 endpoint | Notes | | ------------------------- | ----------------------- | --------------------------------------- | | `/graphql/document-drive` | `/graphql/r` | Drive and document management | | `/graphql/system` | `/graphql/r` | Sync operations moved to reactor | | `/graphql/` | `/graphql/` | Custom subgraphs still user-defined | | N/A | `/graphql/` | New: auto-generated per document model | | `/graphql` | `/graphql` | Supergraph still combines all subgraphs | ## Querying and mutating documents In v6, every operation can be done through the **reactor subgraph** (`/graphql/r`) — which is generic and works with any document type — or through a **document-model subgraph** (`/graphql/`) — which is namespaced and type-specific. Both are shown below for each operation. Document-model subgraphs are auto-generated for each registered document model and provide: - `document(idOrSlug)` — get a single document - `documents(paging)` — list all documents of this type - `findDocuments(search, view, paging)` — search within this type - `documentOutgoingRelationships(sourceIdOrSlug, relationshipType)` — filtered to this type - `documentIncomingRelationships(targetIdOrSlug, relationshipType)` - `createDocument(name, parentIdOrSlug)` mutation - Per-operation mutations (e.g. `addTodoItem(documentIdOrSlug, input)`) - Async variants of each mutation (e.g. `addTodoItemAsync(documentIdOrSlug, input)`) On the reactor subgraph, `document`, `findDocuments`, `documentOutgoingRelationships`, `documentIncomingRelationships`, `documentOutgoingRelationshipEdges`, and `documentIncomingRelationshipEdges` take an optional `consistency: ConsistencyTokenInput`. Pass the `consistencyToken` a job reports (`JobInfo.consistencyToken`) and the read waits until the read models hold every operation it names, so a client can read its own write: ```graphql query ($id: String!, $c: ConsistencyTokenInput!) { document(idOrSlug: $id, consistency: $c) { document { id lastModifiedAtUtcIso } } } ``` ### Getting a drive and its contents **Legacy:** ```graphql # Legacy — /graphql/document-drive query { drive { id name nodes { ... on FileNode { id name documentType } ... on FolderNode { id name } } } } ``` **v6 (reactor subgraph):** ```graphql # v6 — /graphql/r query { document(idOrSlug: "my-drive-slug") { document { id name documentType state } childIds } } ``` **v6 (document-model subgraph):** ```graphql # v6 — /graphql/document-drive (or via supergraph) query { DocumentDrive { document(idOrSlug: "my-drive-slug") { document { id name state { global { nodes { ... } } } } } } } ``` ### Listing children of a drive **Legacy:** Children were returned inline via the `drive.nodes` field (see above). **v6 (reactor subgraph):** ```graphql # v6 — /graphql/r query { documentOutgoingRelationships( sourceIdOrSlug: "my-drive-slug" relationshipType: "child" ) { items { id name documentType state } hasNextPage } } ``` **v6 (document-model subgraph):** To get only children of a specific type (e.g. todo lists): ```graphql # v6 — /graphql/to-do-list (or via supergraph) query { ToDoList { documentOutgoingRelationships( sourceIdOrSlug: "my-drive-slug" relationshipType: "child" ) { items { id name state { global { items { id text checked } } } } hasNextPage cursor } } } ``` ### Finding documents by type **Legacy:** Not directly available — required iterating `drive.nodes` and filtering by `documentType`. **v6 (reactor subgraph):** ```graphql # v6 — /graphql/r query { findDocuments(search: { type: "powerhouse/todo-list" }) { items { id name state } hasNextPage cursor } } ``` **v6 (document-model subgraph):** The type filter is built in: ```graphql # v6 — /graphql/to-do-list (or via supergraph) query { ToDoList { documents { items { id name state { global { items { id text checked } } } } hasNextPage cursor } } } ``` ### Getting a single document **Legacy:** ```graphql # Legacy — /graphql/document-drive query { document(id: "abc123") { id name # ... limited to drive-level fields } } ``` **v6 (reactor subgraph):** ```graphql # v6 — /graphql/r query { document(idOrSlug: "abc123") { document { id name documentType state } childIds } } ``` **v6 (document-model subgraph):** ```graphql # v6 — /graphql/to-do-list (or via supergraph) query { ToDoList { document(idOrSlug: "abc123") { document { id name state { global { items { id text checked } } } } } } } ``` ### Creating a drive **Legacy:** ```graphql # Legacy — /graphql/document-drive mutation { addDrive(name: "tutorial") { id name } } ``` **v6 (reactor subgraph):** ```graphql # v6 — /graphql/r mutation { createDocument( document: { documentType: "powerhouse/document-drive", name: "tutorial" } ) { id name } } ``` **v6 (document-model subgraph):** ```graphql # v6 — /graphql/document-drive (or via supergraph) mutation { DocumentDrive { createDocument(name: "tutorial") { id name } } } ``` ### Creating a document In the legacy system, documents were created indirectly through drive operations or via Connect's internal APIs. In v6, documents are created directly. **v6 (reactor subgraph):** ```graphql # v6 — /graphql/r mutation { createDocument( document: { documentType: "powerhouse/todo-list", name: "My List" } parentIdOrSlug: "my-drive-slug" ) { id name } } ``` **v6 (document-model subgraph):** ```graphql # v6 — /graphql/to-do-list (or via supergraph) mutation { ToDoList { createDocument(name: "My List", parentIdOrSlug: "my-drive-slug") { id name } } } ``` ### Applying operations to a document In the legacy system, document operations were applied through strand-based push/pull via the `system` subgraph. In v6, operations are applied directly. **v6 (reactor subgraph):** ```graphql # v6 — /graphql/r mutation { execute( documentIdOrSlug: "abc123" actions: [ { type: "ADD_TODO_ITEM" input: { text: "Buy milk" } scope: "global" id: "op-1" timestampUtcMs: "1711900000000" } ] ) { id name state } } ``` **v6 (document-model subgraph):** ```graphql # v6 — /graphql/to-do-list (or via supergraph) mutation { ToDoList { addTodoItem(documentIdOrSlug: "abc123", input: { text: "Buy milk" }) { id state { global { items { id text checked } } } } } } ``` ## Sync API changes ### Legacy: system subgraph (strand-based sync) The old `system` subgraph provided strand-based synchronization via pull-responder listeners: ```graphql # Legacy — /graphql/system mutation { registerPullResponderListener(filter: { documentType: ["powerhouse/todo-list"] branch: ["main"] }) { # returned a Listener object } } query { system { sync { strands(listenerId: "listener-123") { driveId documentId scope branch operations { type input index hash timestamp } } } } } ``` ### v6: Channel-based sync The v6 reactor replaces the strand/listener model with channel-based sync and real-time subscriptions: ```graphql # v6 — /graphql/r mutation { touchChannel( input: { id: "channel-1" name: "my-sync" collectionId: "drive-id" filter: { documentId: ["*"], scope: ["global"], branch: "main" } sinceTimestampUtcMs: "0" } ) { success ackOrdinal } } query { pollSyncEnvelopes(channelId: "channel-1", outboxAck: 0, outboxLatest: 100) { envelopes { type operations { operation { index hash action { type input scope } } context { documentId documentType scope branch ordinal } } } ackOrdinal } } ``` Real-time updates are available via GraphQL subscriptions: ```graphql subscription { documentChanges(search: { type: "powerhouse/todo-list" }) { type # CREATED, UPDATED, DELETED, etc. documents { id name state } } } ``` ## Query mapping reference | Legacy query/mutation | v6 equivalent | Subgraph | | ------------------------------- | ------------------------------------------------------------------------------ | ---------------------- | | `drive` | `document(idOrSlug: driveSlug)` | reactor (`/graphql/r`) | | `drives` | `findDocuments(search: { type: "powerhouse/document-drive" })` | reactor | | `document(id)` | `document(idOrSlug)` | reactor | | `documents` | `findDocuments()` | reactor | | `addDrive(name)` | `createDocument(document: { documentType: "powerhouse/document-drive", ... })` | reactor | | `system { sync { strands } }` | `pollSyncEnvelopes(channelId, ...)` | reactor | | `registerPullResponderListener` | `touchChannel(input: {...})` | reactor | | `pushUpdates` | `pushSyncEnvelopes(envelopes: [...])` | reactor | | N/A (new) | ` { document(...) }` | document-model | | N/A (new) | ` { createDocument(...) }` | document-model | | N/A (new) | ` { (documentIdOrSlug, input) }` | document-model | ## Migrating custom subgraphs ### Legacy pattern Custom subgraphs exported a `resolvers` object and a `typeDefs` string, using `ctx.driveServer` for data access: ```typescript // Legacy custom subgraph export const typeDefs = ` type Query { myCustomQuery(driveId: String!): [String!]! } `; export const resolvers: GraphQLResolverMap = { Query: { myCustomQuery: async (_parent, args, ctx: Context) => { const drive = await ctx.driveServer.getDrive(args.driveId); // ... process drive data return results; }, }, }; ``` ### v6 pattern Custom subgraphs now use `getResolvers(subgraph: BaseSubgraph)` and access data through the reactor client halves on the resolver context: `ctx.clients.read` in every resolver, `ctx.clients.write` in Mutation fields only: ```typescript // v6 custom subgraph — subgraphs/my-custom/resolvers.ts export const getResolvers = (subgraph: BaseSubgraph) => { return { Query: { myCustomQuery: async ( _parent: unknown, args: { driveId: string }, ctx: Context, ) => { const children = await ctx.clients.read.getOutgoingRelationships( args.driveId, "child", ); // ... process documents return results; }, }, }; }; ``` ```typescript // v6 custom subgraph — subgraphs/my-custom/schema.ts export const schema: DocumentNode = gql` type Query { myCustomQuery(driveId: String!): [String!]! } `; ``` Generate the scaffolding with: ```bash ph generate subgraph --name my-custom ``` ### Key differences | Aspect | Legacy | v6 | | -------------------- | ------------------------------- | ----------------------------------------------- | | Data access | `ctx.driveServer` | `ctx.clients.read` / `ctx.clients.write` | | Schema format | Template literal string | `gql` tagged template (`DocumentNode`) | | Resolver export | `export const resolvers` | `export const getResolvers = (subgraph) => ...` | | Relational DB access | Not available | `subgraph.relationalDb` | | Attachments | Not available | `subgraph.attachmentsFor(ctx)` | | File structure | `resolvers.ts` + `type-defs.ts` | `resolvers.ts` + `schema.ts` + `index.ts` | | Registration | Manual | Automatic via `ph generate` | ## Migration checklist - [ ] Update all GraphQL client queries that target `/graphql/document-drive` to use `/graphql/r` or the appropriate document-model subgraph - [ ] Replace `drive` queries with `document(idOrSlug)` or `findDocuments` - [ ] Replace strand-based sync (`registerPullResponderListener`, `system.sync.strands`) with `touchChannel` + `pollSyncEnvelopes` - [ ] For real-time updates, use `documentChanges` subscription instead of polling strands - [ ] Migrate custom subgraphs to `getResolvers(subgraph: BaseSubgraph)` pattern - [ ] Replace `ctx.driveServer` calls with `ctx.clients.read` (queries) and `ctx.clients.write` (mutations) methods - [ ] Update schema files from plain strings to `gql` tagged templates - [ ] Regenerate custom subgraph scaffolding with `ph generate subgraph --name ` --- ## Processor Migration Guide (v6) > Source: https://academy.vetra.io/academy/Reference/Processors/ProcessorMigrationGuide **TIP:** This guide covers the **breaking changes** to the processor interface introduced in the v6 Reactor. If you have existing processors built on the legacy strand-based API, **this migration is required**. ## Overview The v6 Reactor replaced the strand-based processor model with a flat operation-based model: - **Old**: Processors received `InternalTransmitterUpdate[]` strands via `onStrands()`, grouped by document - **New**: Processors receive a flat `OperationWithContext[]` list via `onOperations()`, with per-operation context This change simplifies the processor interface, improves cross-document ordering via the `ordinal` field, and unifies all processor types under a single `IProcessor` interface. ## Import path changes | Legacy import | v6 import | | ------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------- | | `import { InternalTransmitterUpdate, IProcessor } from "document-drive"` | `import type { IProcessor, OperationWithContext } from "@powerhousedao/reactor-browser"` | | `import type { ReactorContext } from "document-drive"` | Removed — no longer needed | | `import type { OperationWithContext } from "@powerhousedao/reactor"` | Same (server-side alternative to reactor-browser) | **INFO:** `@powerhousedao/reactor-browser` re-exports all reactor types for convenience in browser environments (editors, drive-apps). If you are working outside the browser (Node.js, CLI tools, server-side code), import directly from `@powerhousedao/reactor` or `@powerhousedao/shared`. ## Interface changes ### Legacy: IProcessor with onStrands ```typescript // Legacy processor interface export class MyProcessor implements IProcessor { // Received grouped strands — one per (driveId, documentId, scope, branch) async onStrands(strands: InternalTransmitterUpdate[]): Promise { for (const strand of strands) { const { driveId, documentId, scope, branch } = strand; for (const operation of strand.operations) { // operation.type, operation.input, operation.index, ... } } } // Also required but often unused onOperations(operations: any[]): Promise { return Promise.resolve(); } onDisconnect(): Promise { return Promise.resolve(); } } ``` ### v6: IProcessor with onOperations ```typescript // v6 processor interface export class MyProcessor implements IProcessor { // Receives a flat list of operations with full context async onOperations(operations: OperationWithContext[]): Promise { for (const { operation, context } of operations) { // context: documentId, documentType, scope, branch, ordinal, resultingState // operation: action (type, input), index, timestampUtcMs, hash } } async onDisconnect(): Promise { // cleanup } } ``` ### Key differences | Aspect | Legacy | v6 | | ----------------------- | ------------------------------------------- | ------------------------------------------------- | | Entry method | `onStrands(strands)` | `onOperations(operations)` | | Data shape | Grouped by document (`strand.operations[]`) | Flat list (`OperationWithContext[]`) | | Document context | `strand.driveId`, `strand.documentId` | `context.documentId`, `context.documentType` | | Operation access | `operation.type`, `operation.input` | `operation.action.type`, `operation.action.input` | | Cross-document ordering | Not available | `context.ordinal` (global monotonic counter) | | Document state | Not provided | `context.resultingState` (JSON string, optional) | | Drive ID | `strand.driveId` | Available via factory's `driveHeader.id` | ## Factory changes ### Legacy factory ```typescript // Legacy processor factory export const myProcessorFactory = (analyticsStore: IAnalyticsStore) => (driveId: string): ProcessorRecord[] => { return [ { processor: new MyProcessor(analyticsStore), filter: { branch: ["main"], documentId: ["*"], scope: ["*"], documentType: ["*"], }, }, ]; }; ``` ### v6 factory ```typescript // v6 processor factory ProcessorRecord, IProcessorHostModule, } from "@powerhousedao/reactor-browser"; export const myProcessorFactory = (module: IProcessorHostModule) => ( driveHeader: PHDocumentHeader, processorApp?: ProcessorApp, ): ProcessorRecord[] => { return [ { processor: new MyProcessor(module.analyticsStore), filter: { branch: ["main"], documentId: ["*"], scope: ["*"], documentType: ["*"], }, }, ]; }; ``` ### Factory differences | Aspect | Legacy | v6 | | ------------------ | ------------------------------------------- | ------------------------------------------------------------------------------------------- | | Outer parameter | Direct dependencies (e.g. `analyticsStore`) | `IProcessorHostModule` (bundles `analyticsStore`, `relationalDb`, `processorApp`, `config`) | | Inner parameter | `driveId: string` | `driveHeader: PHDocumentHeader` (full header with `id`, `name`, `documentType`, etc.) | | Optional parameter | None | `processorApp?: ProcessorApp` | ## Migrating an analytics processor ### Legacy analytics processor ```typescript // Legacy AnalyticsSeriesInput, IAnalyticsStore, } from "@powerhousedao/analytics-engine-core"; export class MyAnalyticsProcessor implements IProcessor { private readonly inputs: AnalyticsSeriesInput[] = []; constructor(private readonly analyticsStore: IAnalyticsStore) {} onOperations(): Promise { return Promise.resolve(); } onDisconnect(): Promise { return Promise.resolve(); } async onStrands(strands: InternalTransmitterUpdate[]): Promise { for (const strand of strands) { if (strand.operations.length === 0) continue; const source = AnalyticsPath.fromString( `/MyAnalytics/${strand.driveId}/${strand.documentId}/${strand.branch}/${strand.scope}`, ); if (strand.operations[0].index === 0) { await this.clearSource(source); } for (const operation of strand.operations) { this.inputs.push({ source, metric: "MyMetric", start: DateTime.fromISO(operation.timestamp), value: 1, dimensions: { /* ... */ }, }); } } if (this.inputs.length > 0) { await this.analyticsStore.addSeriesValues(this.inputs); this.inputs.length = 0; } } private async clearSource(source: AnalyticsPath) { try { await this.analyticsStore.clearSeriesBySource(source, true); } catch (e) { console.error(e); } } } ``` ### v6 analytics processor ```typescript // v6 AnalyticsSeriesInput, IAnalyticsStore, } from "@powerhousedao/analytics-engine-core"; OperationWithContext, IProcessor, } from "@powerhousedao/reactor-browser"; export class MyAnalyticsProcessor implements IProcessor { constructor(private readonly analyticsStore: IAnalyticsStore) {} async onOperations(operations: OperationWithContext[]): Promise { if (operations.length === 0) return; const CHUNK_SIZE = 50; const buffer: AnalyticsSeriesInput[] = []; for (const { operation, context } of operations) { const { documentId, branch, scope } = context; const source = AnalyticsPath.fromString( `ph/my-analytics/${documentId}/${branch}/${scope}`, ); buffer.push({ source, metric: "MyMetric", start: DateTime.fromISO(operation.action.timestampUtcMs), value: 1, dimensions: { /* ... */ }, }); while (buffer.length >= CHUNK_SIZE) { const batch = buffer.splice(0, CHUNK_SIZE); await this.analyticsStore.addSeriesValues(batch); } } if (buffer.length > 0) { await this.analyticsStore.addSeriesValues(buffer); } } async onDisconnect(): Promise {} } ``` ### What changed - `onStrands()` is removed entirely — all logic moves to `onOperations()` - **`clearSource` is no longer needed.** The v6 processor manager guarantees each operation is delivered exactly once — there is no replay. You can remove all `clearSource` / `clearSeriesBySource` logic and the `index === 0` guard. - Strand fields like `strand.driveId`, `strand.documentId` become `context.documentId`, `context.documentType`, etc. - `operation.type` becomes `operation.action.type` - `operation.input` becomes `operation.action.input` - `operation.timestamp` becomes `operation.action.timestampUtcMs` - Chunked batch insert is now the recommended pattern (see example above) ## Migrating a relational database processor ### Legacy relational processor Legacy relational processors (previously called "operational processors") were plain `IProcessor` implementations that managed their own database connection: ```typescript // Legacy export class MyRelationalProcessor implements IProcessor { constructor(private db: any) {} async onStrands(strands: InternalTransmitterUpdate[]): Promise { for (const strand of strands) { for (const operation of strand.operations) { await this.db .insertInto("my_table") .values({ doc_id: strand.documentId, action: operation.type, }) .execute(); } } } onOperations(): Promise { return Promise.resolve(); } onDisconnect(): Promise { return Promise.resolve(); } } ``` ### v6 relational database processor The v6 Reactor provides a `RelationalDbProcessor` base class with built-in database lifecycle management: ```typescript // v6 export class MyRelationalProcessor extends RelationalDbProcessor { // Unique namespace per drive — prevents data collisions static override getNamespace(driveId: string): string { return super.getNamespace(driveId); } // Run migrations on startup override async initAndUpgrade(): Promise { await up(this.relationalDb); } override async onOperations( operations: OperationWithContext[], ): Promise { if (operations.length === 0) return; for (const { operation, context } of operations) { await this.relationalDb .insertInto("my_table") .values({ doc_id: context.documentId, action: operation.action.type, }) .onConflict((oc) => oc.column("doc_id").doNothing()) .execute(); } } async onDisconnect(): Promise {} } ``` ### v6 relational factory ```typescript ProcessorRecord, IProcessorHostModule, ProcessorFilter, } from "@powerhousedao/reactor-browser"; export const myRelationalProcessorFactory = (module: IProcessorHostModule) => async (driveHeader: PHDocumentHeader): Promise => { const namespace = MyRelationalProcessor.getNamespace(driveHeader.id); const store = await module.relationalDb.createNamespace( namespace, ); const filter: ProcessorFilter = { branch: ["main"], documentId: ["*"], documentType: ["powerhouse/my-doc-type"], scope: ["global"], }; const processor = new MyRelationalProcessor( namespace, filter, store, driveHeader.id, ); return [{ processor, filter }]; }; ``` Generate the scaffolding with: ```bash ph generate processor --name my-processor --type relationalDb --document-types powerhouse/my-doc-type ``` ### Key additions in RelationalDbProcessor | Feature | Description | | ------------------------------ | ------------------------------------------------------------------------ | | `RelationalDbProcessor` | Type-safe base class with `this.relationalDb` | | `getNamespace(driveId)` | Static method for per-drive database isolation | | `initAndUpgrade()` | Lifecycle hook for running migrations on startup | | `query(driveId, relationalDb)` | Static method for querying from subgraphs | | Type generation | `ph generate migration-file --path migrations.ts` generates `schema.ts` types | ## Migrating a plain processor ### Legacy ```typescript export class LoggerProcessor implements IProcessor { async onStrands(strands: InternalTransmitterUpdate[]): Promise { for (const strand of strands) { for (const op of strand.operations) { console.log(`[${strand.driveId}] ${strand.documentId}: ${op.type}`); } } } onOperations(): Promise { return Promise.resolve(); } onDisconnect(): Promise { return Promise.resolve(); } } ``` ### v6 ```typescript export class LoggerProcessor implements IProcessor { constructor(private driveId: string) {} async onOperations(operations: OperationWithContext[]): Promise { for (const { operation, context } of operations) { console.log( `[${this.driveId}] ${context.documentId}: ${operation.action.type}`, ); } } async onDisconnect(): Promise {} } ``` ## OperationWithContext reference Each item in the `onOperations` list destructures as `{ operation, context }`: **`context`** — where the operation happened: | Field | Type | Description | | ---------------- | --------- | ------------------------------------------------------------------- | | `documentId` | `string` | The document that was modified | | `documentType` | `string` | e.g. `"powerhouse/todo-list"` | | `scope` | `string` | The scope (e.g. `"global"`, `"local"`) | | `branch` | `string` | The branch (e.g. `"main"`) | | `ordinal` | `number` | Global monotonically increasing ordinal for cross-document ordering | | `resultingState` | `string?` | JSON string of the document state after the operation | **`operation`** — what happened: | Field | Type | Description | | ---------------- | -------- | ---------------------------------------------------------------------------------- | | `action` | `Action` | Contains `type` (e.g. `"ADD_TODO_ITEM"`), `input`, `timestampUtcMs`, `id`, `scope` | | `index` | `number` | Position in the operation history | | `timestampUtcMs` | `string` | When the operation was created | | `hash` | `string` | Hash of the resulting document state | ## Migration checklist - [ ] Replace all `onStrands(strands: InternalTransmitterUpdate[])` with `onOperations(operations: OperationWithContext[])` - [ ] Remove the `onStrands` method entirely - [ ] Update imports from `document-drive` to `@powerhousedao/reactor-browser` (or `@powerhousedao/reactor` for server-side) - [ ] Replace `strand.driveId` / `strand.documentId` with `context.documentId` / `context.documentType` - [ ] Replace `operation.type` with `operation.action.type` - [ ] Replace `operation.input` with `operation.action.input` - [ ] Replace `operation.timestamp` with `operation.action.timestampUtcMs` - [ ] Update factory signature to accept `IProcessorHostModule` and `PHDocumentHeader` - [ ] For relational processors: extend `RelationalDbProcessor` and implement `initAndUpgrade()` - [ ] Regenerate processor scaffolding with `ph generate --processor ` --- ## Document-Engineering > Source: https://academy.vetra.io/academy/Reference/EditorsUI/DocumentEngineering The reusable components in the Document-Engineering system are a set of of front-end components based on graphQL scalars. These components are offered through the **Powerhouse Document-Engineering system** with the help of storybook & the Academy documentation. It provides a collection of pre-built, reusable UI components designed for consistency and efficiency across Powerhouse applications and editors. Think of it as a toolkit of standard UI elements like buttons, inputs, and checkboxes with many of these components based on GraphQL scalars. Document models use built-in scalars from `document-model/scalars` by name, including `OID` and `EthereumAddress`. See the [built-in scalar names](/academy/Build/DocumentModelCreation/CustomScalars#name-the-export-after-the-scalar) for the full list. To define a scalar for your package, see [Custom scalars](/academy/Build/DocumentModelCreation/CustomScalars). **INFO:** A GraphQL scalar is essentially a primitive, indivisible value in the GraphQL type system. Here are the key points to understand: - **Basic Building Blocks:** Scalars are the basic data types—like String, Int, Float, Boolean, and ID—that represent atomic values. - **Leaf Nodes:** Scalars are the "leaves" of a GraphQL query. They can't have any sub-fields, meaning once you hit a scalar in a query, that's the final value. - **Scalars beyond the five GraphQL types:** Use a built-in scalar by name, such as `DateTime`, or define one for your package as described in [Custom scalars](/academy/Build/DocumentModelCreation/CustomScalars). ::: ## What are Components? In the context of Powerhouse Builder platform, components can be thought of as reusable elements, or ready-to-use building blocks that help builders implement **document editors & viewers** with little to no effort. An important utility aspect of a component is that it serves its users as a **data input field**, providing structured ways to enter and manipulate information within your document models. ## Document Editors vs Document Viewers Understanding the relationship between document editors and viewers is crucial for component usage: **Document Editor**: A specific document type that is used by one or more users to make data entries and update its state. Key utility is the ability to enter data in a structured format, making it a great tool for collaboration within a group of authorized users. **Document Viewer**: Does not allow modifications. It's a great way to inform about the state of the document type, making it a great tool for providing a broader group or public with transparent insights. Document viewers do not have to match the view of the editor one-to-one - the data presented could be framed as a specific selection, or filtered to provide desired insights. **TIP:** The same component that will be used in a document viewer will have a **disabled state** (not allowed to edit documents). Document editors precede document viewers - you would start by creating a document editor and then, if needed, decide which viewer format is useful. ## Scalars vs. General UI Components ### Scalar Components A scalar component is a form field for one GraphQL scalar. Write the scalar name in the state schema. In the editor, import the component and place it on that field. These are specialized form components, each corresponding to a GraphQL scalar type, such as `String`, `Boolean`, `Currency`, or `PHID`. They are built on top of react-hook-form, with validation and form state management. Wrap them in a `Form` component from the same library. **Location:** @powerhousedao/document-engineering/scalars https://github.com/powerhouse-inc/document-engineering **Key Feature**: Must be used within a Form component provided by this library. ### General-Purpose UI Components This category includes a broader range of UI elements such as simplified versions of the Scalar components (which don't require a Form wrapper but lack built-in validation), as well as other versatile components like Dropdown, Tooltip, Sidebar, ObjectSetTable and more. These are designed for crafting diverse and complex user interfaces. **Location:** @powerhousedao/document-engineering/ui https://github.com/powerhouse-inc/document-engineering ## Component Types Classification Inspired by atomic design methodology, Powerhouse classifies components into the following categories: ### Fragment The smallest element that combined together makes up a scalar or other simple component. **Examples:** Character counter, Checkbox field, Label ### Scalar (Simple Component) The simplest component that contains the basic input field for one-dimensional data type (single value). **Examples:** Integer, Boolean, String, Powerhouse ID (PHID) ### Complex Component Compound component that has an object/array value. It's made up of multiple scalars combined to serve a specific function. **Examples:** Sidebar (tree structure navigation component with content-style navigation for hierarchical data) ### Layout Component Purpose-specific container for other components like lists of other components, color layouts, sections, etc. **Examples:** Homepage section layout **INFO:** The Powerhouse team is building a Component library with a wide range of components embedding best UX practices & key functionality. This library establishes standards and best practices for building documents while fast-tracking the building process through facilitation of the most basic & useful component types. ## Component Behavior & UX Principles Besides the ability to input data, components have another crucial utility: they describe the mechanism of user interaction through implementing a defined set of behavior rules. **Best Practices for Component Behavior:** - Implementing behaviors at a component level is much more efficient than at the document level - Good component behavior feels natural to the user and is easily understood - Components should be intuitive and not require additional tutorials or explanations - Start with the most simple/basic behaviors first, then layer additional behaviors on top - Keep behaviors as simple as needed - less is more ## Exploring Components with Storybook We use Storybook as an interactive catalog for our design system components. It allows you to visually explore each component, interact with different states, and understand how to integrate them into your projects. [https://storybook.powerhouse.academy](https://storybook.powerhouse.academy) **Understanding the Storybook Interface:** 1. **Visual Demo:** The main panel shows the rendered component (e.g., a `Checkbox`). You can interact with it directly to see different states (checked, unchecked, disabled). 2. **Usage Snippet:** Below the demo, you'll typically find a basic code example demonstrating how to include the component in your code (e.g., ``). This provides a starting point for implementation. 3. **Props Table:** Further down, a table lists the properties (`props`) the component accepts. Props are like settings or configuration options. For the `Checkbox`, this table would show props like `label`, `defaultValue`, `value`, `onChange`, etc., often with descriptions of what they control. ## **Storybook vs. Source Code:** Storybook serves as essential documentation and a usage guide. Our developers write Storybook "stories" to demonstrate components and document their common props. However, the **ultimate source of truth** for a component's capabilities is its actual source code (e.g., the `.tsx` file within the `@powerhousedao/document-engineering/scalars` package). While Storybook aims for accuracy, there might occasionally be discrepancies or undocumented props. ## Implementing a Component Let's walk through the typical workflow for using a component from the document-engineering system, using `BooleanField` for a checkbox in the [TodoList editor](/academy/Build/BuildingUserExperiences/BuildingDocumentEditors). 1. **Identify the Need:** While building your feature (e.g., the TodoList editor), you determine the need for a standard UI element, like a checkbox to mark items as complete. 2. **Consult the Document Engineering Components in Storybook:** - Open the Powerhouse Storybook instance. [https://storybook.powerhouse.academy](https://storybook.powerhouse.academy) - Navigate or search to find the `BooleanField` component (used for checkboxes). - Review the visual examples and interactive demo. - Examine the "Usage" snippet and the **Props table** to understand the basic implementation and available configuration options (`name`, `value`, `onChange`, etc.). 3. **Import the Component:** In your code editor, open the relevant file (e.g., `editors/todo-list-editor/components/Checkbox.tsx`). Add an import statement at the top to bring the component into your file's scope: ```typescript import { Form, BooleanField, } from "@powerhousedao/document-engineering/scalars"; ``` This line instructs the build process to locate the `Form` and `BooleanField` components within the installed `@powerhousedao/document-engineering/scalars` package and make them available for use. :::info[Form Wrapper Required] Scalar components like `BooleanField` must be wrapped in a `Form` component from the same package. This provides built-in validation and form state management. ::: 4. **Use and Configure the Component:** Place the component tag in your JSX where needed. Use the information from Storybook (usage snippet and props table) as a guide, but adapt the props to your specific requirements: **Step 4a: Create a reusable Checkbox component** ```typescript // editors/todo-list-editor/components/Checkbox.tsx import { Form, BooleanField } from "@powerhousedao/document-engineering/scalars"; interface CheckboxProps { value: boolean; onChange: (value: boolean) => void; } export const Checkbox = ({ value, onChange }: CheckboxProps) => { return (
{}}> ); }; ``` **Step 4b: Use it in your Todo component with the document model hook** ```typescript // editors/todo-list-editor/components/Todo.tsx import { useSelectedTodoListDocument, updateTodoItem } from "todo-tutorial/document-models/todo-list"; import type { TodoItem } from "todo-tutorial/document-models/todo-list"; import { Checkbox } from "./Checkbox.js"; type Props = { todo: TodoItem }; export function Todo({ todo }: Props) { // Use the hook to get dispatch function const [todoList, dispatch] = useSelectedTodoListDocument(); if (!todoList) return null; return (
{ dispatch(updateTodoItem({ id: todo.id, checked: !todo.checked, })); }} /> {todo.text}
); } ``` You configure the component's appearance and behavior by passing the appropriate values to its props. Note the use of the `useSelectedTodoListDocument` hook to access the dispatch function. 5. **Test and Refine:** Run your application (e.g., using `ph connect`) to see the component in context. Verify its appearance and functionality. ## Usage The Document Engineering package provides several entry points for different use cases in your powerhouse project: ### Main Package ```typescript ``` ### UI Components ```typescript ``` ### Scalars Form components for scalar fields, such as `Form` and `BooleanField`: ```typescript ``` ### GraphQL The `./graphql` export holds the component library's own GraphQL scalar definitions. Document models and subgraphs don't use them. In their schemas, name a built-in scalar from `document-model/scalars`, or define one as described in [Custom scalars](/academy/Build/DocumentModelCreation/CustomScalars). ```typescript ``` ### Styles To include the package's styles: ```typescript ``` ## Import Maps Within the project, the following import maps are available: - `#assets` - Assets utilities and components - `#scalars` - Form components for scalar fields - `#ui` - UI components - `#graphql` - GraphQL related utilities Please don't hesitate to reach out in our discord channels with any questions. Happy designing! --- ## React Hooks > Source: https://academy.vetra.io/academy/Reference/EditorsUI/ReactHooks This page provides a reference for the hooks available in `@powerhousedao/reactor-browser`. These hooks are intended to be used by editors (including drive editors) which will be rendered inside Powerhouse host-applications such as Connect, Switchboard, Fusion or a Vetra Studio Drive. - Learn more about [Editors](/academy/Build/BuildingUserExperiences/BuildingDocumentEditors) - Learn more about [Drive-apps](/academy/Build/BuildingUserExperiences/BuildingADriveExplorer) The document hooks also run on top of the light GraphQL client, which talks to a Switchboard instead of a full in-browser reactor. The drive, node and job hooks do not: that client implements a six-member subset of `IReactorClient`. Neither do the package and module hooks - `useVetraPackages`, `useDocumentModelModules`, `useEditorModules`, `useEditorModuleById` and the rest read `window.ph.vetraPackageManager`, which only a host application fills. See the [Reactor Browser Client](/academy/Reference/ReactorBrowserClient/OverviewAndQuickstart).
Need a refresher on React Hooks? React Hooks allow you to use various React features directly within your functional components. You can use built-in Hooks or combine them to create your own custom Hooks. **What are Custom Hooks?** A custom hook is a JavaScript function whose name starts with "use" and that calls other Hooks. They are used to: - Reuse stateful logic between components. - Abstract complex logic into a simpler interface. - Isolate side effects, particularly those managed by `useEffect`. **Key Built-in Hooks Examples:** - `useState`: Lets a component "remember" information (state). - `useEffect`: Lets a component perform side effects (e.g., data fetching, subscriptions, manually changing the DOM). - `useContext`: Lets a component receive information from distant parent components without explicitly passing props through every level of the component tree. **Naming Convention:** Hook names must always start with `use` followed by a capital letter (e.g., `useState`, `useOnlineStatus`). **Rules of Hooks:** 1. **Only Call Hooks at the Top Level**: Don't call Hooks inside loops, conditions, or nested functions. 2. **Only Call Hooks from React Functions**: Call Hooks from React functional components or from custom Hooks. It's important to note that a function should only be named and treated as a hook if it actually utilizes one or more built-in React hooks. If a function (even if named `useSomething`) doesn't call any built-in hooks, it behaves like a regular JavaScript function, and making it a "hook" offers no specific React advantages.
## Key Concepts ### Reactor All of the data used by these hooks is ultimately derived from the `Reactor`, which manages the asynchronous eventually consistent state of drives and documents. Learn more about the [Reactor](/academy/Reference/Reactor/WorkingWithTheReactor). ### Editors get their document as props A host such as Connect renders an editor's component with the open `document` and a `dispatch` bound to it as props. `EditorProps` from `document-model` types both for the editor's document model: ```tsx export default function Editor({ document, dispatch, }: EditorProps) { // ... } ``` Bare `EditorProps` leaves `document` and `dispatch` optional. A drive app gets the drive as `document`. ### What the selection hooks read The host also renders the editor inside an [`EditorDocumentProvider`](#editordocumentprovider), and a drive app inside a [`DriveProvider`](#driveprovider). The `useSelected*` hooks read the nearest provider first, and the app-wide selection only without one: - `useSelectedDocument`, `useSelectedDocumentId`, `useSelectedDocumentOfType` and the generated `useSelectedDocument` read the editor's document. - `useSelectedDrive`, `useSelectedDriveId`, `useSelectedNode`, `useSelectedFolder` and the hooks built on them (items in the selected drive or folder, node actions) read the drive app's drive, node and folder. So components inside an editor call the generated hook instead of receiving the document as props. ### Dispatch Function Many hooks return a `dispatch` function for modifying documents. The dispatch function has this signature: ```typescript function dispatch( actionOrActions: Action | Action[] | undefined, onErrors?: (errors: Error[]) => void, onSuccess?: (result: PHDocument) => void, ): void; ``` **Parameters:** - `actionOrActions` — The action or array of actions to dispatch to the document - `onErrors` — Optional callback invoked with any errors that occurred during action execution, or with why nothing was dispatched - `onSuccess` — Optional callback invoked with the updated document `dispatch` stays the same function while the document's id does, and it always dispatches against the latest document, so it is safe in effect and callback dependencies. See [`useDispatch`](#usedispatch). --- ## Quick Reference | Category | Hooks | | --------------------- | ---------------------------------------------------------------------------------------------------------------------------- | | **Editor & Drive** | `EditorDocumentProvider`, `DriveProvider`, `useEditorDocumentContext`, `useDriveContext`, `useEditorModuleFor`, `useNodeTarget` | | **Dispatch** | `useDispatch`, `createDispatch` | | **Selected Document** | `useSelectedDocument`, `useSelectedDocumentSafe`, `useSelectedDocumentId`, `useSelectedDocumentOfType` | | **Document by ID** | `useDocumentById`, `useDocumentsByIds`, `useDocumentOfType` | | **Document Cache** | `useDocumentCache`, `useDocument`, `useDocuments`, `useGetDocument`, `useGetDocuments`, `useGetDocumentAsync` | | **Drives** | `useDrives`, `useSelectedDrive`, `useSelectedDriveSafe`, `useSelectedDriveId`, `useSetSelectedDrive` | | **Nodes & Folders** | `useSelectedNode`, `useSetSelectedNode`, `useCloseNode`, `useSelectedFolder`, `useNodeById`, `useNodePathById` | | **URL & Links** | `useSearchParams`, `useSetSearchParams`, `useLinkProps` | | **Items in Drive** | `useNodesInSelectedDrive`, `useFileNodesInSelectedDrive`, `useFolderNodesInSelectedDrive`, `useDocumentsInSelectedDrive` | | **Items in Folder** | `useNodesInSelectedFolder`, `useFileNodesInSelectedFolder`, `useFolderNodesInSelectedFolder`, `useDocumentsInSelectedFolder` | | **Node Actions** | `useNodeActions` | | **Authorization** | `useCanExecute` | | **Modals** | `usePHModal`, `showPHModal`, `closePHModal`, `showCreateDocumentModal`, `showDeleteNodeModal` | | **Revision History** | `useRevisionHistoryVisible`, `showRevisionHistory`, `hideRevisionHistory` | | **Timeline** | `useSelectedTimelineItem`, `useSelectedTimelineRevision` | | **Config** | `useAllowedDocumentTypes`, `useIsDragAndDropEnabled`, `useIsExternalControlsEnabled` | --- ## Selected Document ### `useSelectedDocumentId` Returns the ID of the selected document: the editor's document inside an `EditorDocumentProvider`, else the drive app's open document inside a `DriveProvider`, else the app's selection. ```typescript check function useSelectedDocumentId(): string | undefined; ``` **Returns:** The selected document's ID, or `undefined` if no file node is selected. --- ### `useSelectedDocument` Returns the selected document along with a dispatch function: the editor's document and dispatch inside an `EditorDocumentProvider`, else the app's selection. Throws error if no document selected. ```typescript function useSelectedDocument(): readonly [ PHDocument, ( actionOrActions: Action | Action[] | undefined, onErrors?: (errors: Error[]) => void, ) => void, ]; ``` **Returns:** A tuple `[document, dispatch]` where: - `document` — The selected document - `dispatch` — A function to dispatch actions to the document **Example:** ```tsx function DocumentViewer() { const [document, dispatch] = useSelectedDocument(); if (!document) { return

No document selected

; } return (

{document.name}

Type: {document.header.documentType}

); } ``` **See also:** [`useSelectedDocumentSafe`](#useselecteddocumentsafe), [`useSelectedDocumentOfType`](#useselecteddocumentoftype), [`useDocumentById`](#usedocumentbyid) --- ### `useSelectedDocumentSafe` Returns the selected document along with a dispatch function or undefined is no document is selected. ```typescript function useSelectedDocumentSafe(): readonly [ PHDocument | undefined, ( actionOrActions: Action | Action[] | undefined, onErrors?: (errors: Error[]) => void, ) => void, ]; ``` **Returns:** A tuple `[document, dispatch]` where: - `document` — The selected document, or `undefined` if none selected - `dispatch` — A function to dispatch actions to the document **Throws:** - `NoSelectedDocumentError` — When no document is selected **Example:** ```tsx function DocumentViewer() { const [document, dispatch] = useSelectedDocument(); if (!document) { return

No document selected

; } return (

{document.name}

Type: {document.header.documentType}

); } ``` **See also:** [`useSelectedDocumentOfType`](#useselecteddocumentoftype), [`useDocumentById`](#usedocumentbyid) --- ### `useSelectedDocumentOfType` Returns the selected document of a specific type along with a dispatch function. Throws an error if the found document has a different type. ```typescript function useSelectedDocumentOfType< TDocument extends PHDocument, TAction extends Action, >(documentType: string): [TDocument, DocumentDispatch]; function useSelectedDocumentOfType(documentType: null | undefined): never[]; ``` **Parameters:** - `documentType` — The expected document type string (e.g., `"powerhouse/budget-statement"`) **Returns:** A tuple `[document, dispatch]` with the document typed as `TDocument`. **Throws:** - `NoSelectedDocumentError` — When no document is selected - `DocumentTypeMismatchError` - When selected document has different document type than the one provided **Example:** ```tsx function BudgetEditor() { const [document, dispatch] = useSelectedDocumentOfType< BudgetStatementDocument, BudgetStatementAction >("powerhouse/budget-statement"); const handleUpdate = () => { dispatch({ type: "UPDATE_BUDGET", input: { amount: 1000 } }, (errors) => console.error("Failed:", errors), ); }; return
{/* editor UI */}
; } ``` --- ## Editor & Drive Contexts Hosts provide these around the editors and drive apps they render. Inside them, the `useSelected*` hooks read the provider first (see [What the selection hooks read](#what-the-selection-hooks-read)). ### `EditorDocumentProvider` Gives the components below an editor's document and dispatch. A host renders the editor inside it with the same pair it passes as props. An editor inside another editor gets its own provider; hooks read the nearest one. ```typescript function EditorDocumentProvider(props: { document: PHDocument; dispatch: DocumentDispatch; children: ReactNode; }): JSX.Element; ``` --- ### `useEditorDocumentContext` Returns the nearest editor's `[document, dispatch]`, or `undefined` outside one. ```typescript function useEditorDocumentContext(): | readonly [PHDocument, DocumentDispatch] | undefined; ``` --- ### `DriveProvider` Gives the components below a drive app's drive. Without `open`, opening a node navigates the app's router in that drive. With it, the host decides, for example by keeping the open node in local state; `preload` warms a node the same way. ```typescript function DriveProvider(props: { value: DriveContextValue; children: ReactNode; }): JSX.Element; type DriveContextValue = { drive: DocumentDriveDocument; dispatch: DocumentDispatch; /** The open folder; undefined at the drive's root or on a document. */ folder?: FolderNode; /** The open node, a folder or a document. */ nodeId?: string; open?: (node: Node | undefined, options?: PHNavigateOptions) => void; preload?: (node: Node | undefined) => void; }; ``` --- ### `useDriveContext` Returns the nearest drive app's `DriveContextValue`, or `undefined` outside one. ```typescript function useDriveContext(): DriveContextValue | undefined; ``` --- ### `useNodeTarget` Returns where a node navigates to: in the drive app's drive inside a `DriveProvider`, else in the open drive. Use it for a link's target, e.g. with [`useLinkProps`](#uselinkprops). ```typescript function useNodeTarget(node: Node | undefined): PHNavigationTarget | undefined; ``` --- ### `useEditorModuleFor` Returns the editor for a document: its `preferredEditor` if that editor edits the document's type, else the first editor installed for the type. ```typescript function useEditorModuleFor( document: PHDocument | null | undefined, ): EditorModule | undefined; ``` --- ### Rendering an editor or drive app yourself A host, or a project route, that renders an editor passes it `document` and `dispatch` and wraps it in `EditorDocumentProvider` with the same pair: ```tsx EditorDocumentProvider, useDocumentById, useEditorModuleFor, } from "@powerhousedao/reactor-browser"; function HostedEditor({ documentId }: { documentId: string }) { const [document, dispatch] = useDocumentById(documentId); const editor = useEditorModuleFor(document); if (!document || !editor) return null; const Editor = editor.Component; return ( ); } ``` A drive app goes inside `DriveProvider` the same way, with the drive as `document`: ```tsx ``` --- ## Dispatch ### `useDispatch` Returns `[document, dispatch]` for a document. `dispatch` is the same function while the document's id stays the same, and it always dispatches against the latest `document` passed in. ```typescript function useDispatch( document: TDocument | undefined, ): readonly [TDocument | undefined, DispatchFn]; ``` --- ### `createDispatch` The dispatch `useDispatch` returns, outside React: it dispatches to the document `getDocument` returns when it runs. ```typescript function createDispatch( getDocument: () => TDocument | undefined, ): DispatchFn; ``` --- ## Document by ID ### `useDocumentById` Returns a document by ID along with a dispatch function. ```typescript function useDocumentById( id: string | null | undefined, ): readonly [ PHDocument | undefined, ( actionOrActions: Action | Action[] | undefined, onErrors?: (errors: Error[]) => void, ) => void, ]; ``` **Parameters:** - `id` — The document ID to retrieve, or `null`/`undefined` to skip retrieval **Returns:** A tuple `[document, dispatch]` where: - `document` — The document if found, or `undefined` - `dispatch` — A function to dispatch actions to the document **Example:** ```tsx function DocumentCard({ documentId }: { documentId: string }) { const [document, dispatch] = useDocumentById(documentId); if (!document) { return

Loading...

; } return
{document.name}
; } ``` --- ### `useDocumentsByIds` Returns multiple documents by their IDs. ```typescript function useDocumentsByIds(ids: string[] | null | undefined): PHDocument[]; ``` **Parameters:** - `ids` — Array of document IDs to retrieve, or `null`/`undefined` to skip **Returns:** An array of documents. Returns an empty array if `ids` is `null`/`undefined`. --- ### `useDocumentOfType` Returns a document of a specific type. Throws an error if the document has a different type. ```typescript function useDocumentOfType< TDocument extends PHDocument, TAction extends Action, >( documentId: string | null | undefined, documentType: string | null | undefined, ): [TDocument, DocumentDispatch] | never[]; ``` **Parameters:** - `documentId` — The document ID to retrieve - `documentType` — The expected document type **Throws:** - `DocumentNotFoundError` — When the document doesn't exist - `DocumentModelNotFoundError` — When the document model isn't registered - `DocumentTypeMismatchError` — When the document type doesn't match --- ## Document Cache ### `useDocumentCache` Returns the document cache containing all documents in the reactor. ```typescript function useDocumentCache(): IDocumentCache | undefined; ``` --- ### `useDocument` Retrieves a document from the reactor and subscribes to changes using React Suspense. This hook will suspend rendering while the document is loading. ```typescript function useDocument(id: string | null | undefined): PHDocument | undefined; ``` **Parameters:** - `id` — The document ID to retrieve, or `null`/`undefined` to skip retrieval **Returns:** The document if found, or `undefined` if `id` is `null`/`undefined`. --- ### `useDocuments` Retrieves multiple documents from the reactor using React Suspense. This hook will suspend rendering while any of the documents are loading. ```typescript function useDocuments(ids: string[] | null | undefined): PHDocument[]; ``` **Parameters:** - `ids` — Array of document IDs to retrieve, or `null`/`undefined` to skip retrieval **Returns:** An array of documents. Returns an empty array if `ids` is `null`/`undefined`. --- ### `useGetDocument` Returns a function to retrieve a document from the cache. The returned function fetches and returns a document by ID. ```typescript function useGetDocument(): (id: string) => Promise; ``` **Returns:** A function that takes a document ID and returns a Promise of the document. **Example:** ```tsx function DocumentFetcher() { const getDocument = useGetDocument(); const handleFetch = async (id: string) => { const document = await getDocument(id); console.log("Fetched document:", document.name); }; return ; } ``` --- ### `useGetDocuments` Returns a function to retrieve multiple documents from the cache. The returned function fetches and returns documents by their IDs. ```typescript function useGetDocuments(): (ids: string[]) => Promise; ``` **Returns:** A function that takes an array of document IDs and returns a Promise of the documents. --- ### `useGetDocumentAsync` Retrieves a document from the reactor without suspending rendering. Returns the current state of the document loading operation. ```typescript function useGetDocumentAsync(id: string | null | undefined): { status: "initial" | "pending" | "success" | "error"; data: PHDocument | undefined; isPending: boolean; error: Error | undefined; reload: (() => Promise) | undefined; }; ``` **Parameters:** - `id` — The document ID to retrieve, or `null`/`undefined` to skip retrieval **Returns:** An object containing: - `status` — `"initial"` | `"pending"` | `"success"` | `"error"` - `data` — The document if successfully loaded - `isPending` — Boolean indicating if the document is currently loading - `error` — Any error that occurred during loading - `reload` — Function to force reload the document from cache **Example:** ```tsx function AsyncDocumentLoader({ id }: { id: string }) { const { status, data, isPending, error, reload } = useGetDocumentAsync(id); if (status === "initial" || isPending) { return

Loading...

; } if (status === "error") { return (

Error: {error?.message}

); } return
{data?.name}
; } ``` --- ## Drives ### `useDrives` Returns all drives in the reactor. ```typescript function useDrives(): DocumentDriveDocument[] | undefined; ``` **Example:** ```tsx function DriveList() { const drives = useDrives(); return (
    {drives?.map((drive) => (
  • {drive.header.slug}
  • ))}
); } ``` --- ### `useSelectedDriveId` Returns the ID of the selected drive: the drive app's drive inside a `DriveProvider`, else the app's selection. ```typescript function useSelectedDriveId(): string | undefined; ``` --- ### `useSelectedDrive` Returns the selected drive along with a dispatch function: the drive app's inside a `DriveProvider`, else the app's selection. **Throws an error if no drive is selected.** ```typescript function useSelectedDrive(): [ DocumentDriveDocument, DocumentDispatch, ]; ``` **Returns:** A tuple `[drive, dispatch]`. **Throws:** `Error` with message `"There is no drive selected. Did you mean to call 'useSelectedDriveSafe'?"` **See also:** [`useSelectedDriveSafe`](#useselecteddrivesafe) --- ### `useSelectedDriveSafe` Returns the selected drive, or `undefined` if no drive is selected. Use this when you need to handle the "no drive selected" case gracefully. ```typescript function useSelectedDriveSafe(): | [DocumentDriveDocument, DocumentDispatch] | readonly [undefined, undefined]; ``` **Returns:** A tuple `[drive, dispatch]` or `[undefined, undefined]` if no drive is selected. **Example:** ```tsx function DriveHeader() { const [drive, dispatch] = useSelectedDriveSafe(); if (!drive) { return

Select a drive to get started

; } return

{drive.header.slug}

; } ``` --- ### `setSelectedDrive` Sets the selected drive and updates the URL. ```typescript function setSelectedDrive( driveOrDriveSlug: string | DocumentDriveDocument | undefined, ): void; ``` **Parameters:** - `driveOrDriveSlug` — The drive object, drive slug string, or `undefined` to deselect `setSelectedDrive` acts on the app's store. --- ### `useSetSelectedDrive` Returns `setSelectedDrive` as a hook. ```typescript function useSetSelectedDrive(): ( driveOrDriveSlug: string | DocumentDriveDocument | undefined, ) => void; ``` --- ## Selected Node & Folder ### `useSelectedNode` Returns the selected node (file or folder): the drive app's open node inside a `DriveProvider`, else the app's selection. ```typescript function useSelectedNode(): Node | undefined; ``` --- ### `setSelectedNode` Sets the selected node and updates the URL. ```typescript function setSelectedNode( node: Node | string | undefined, options?: PHNavigateOptions, ): void; type PHNavigateOptions = { replace?: boolean; search?: Record; }; ``` **Parameters:** - `node` — The node object, node slug string, or `undefined` to deselect - `options.replace` — Replace the current history entry instead of pushing a new one - `options.search` — Query params the node opens with; an `undefined` value removes the param Like `setSelectedDrive`, this acts on the app's store. Inside a drive app, use `useSetSelectedNode`. --- ### `useSetSelectedNode` Returns `setSelectedNode` for the drive app in context: it calls the drive context's `open` when the host gives one, else opens the node in the drive app's drive. ```typescript function useSetSelectedNode(): ( node: Node | string | undefined, options?: PHNavigateOptions, ) => void; ``` --- ### `useCloseNode` Returns a function that leaves a node's view. It goes back to the history entry the node was opened from. If there is none, for example after a deep link, it replaces the current entry with the node's parent folder, or the drive root for a top-level node. ```typescript function useCloseNode(nodeId: string | null | undefined): () => void; ``` --- ### `useSelectedFolder` Returns the selected folder: the drive app's open folder inside a `DriveProvider`, else the app's selection. Returns `undefined` if the selected node is not a folder. ```typescript function useSelectedFolder(): FolderNode | undefined; ``` --- ### `useNodeById` Returns a node in the selected drive by ID. ```typescript function useNodeById(id: string | null | undefined): Node | undefined; ``` **Parameters:** - `id` — The node ID to find --- ### `useNodePathById` Returns the path (array of ancestor nodes) to a node in the selected drive. ```typescript function useNodePathById(id: string | null | undefined): Node[]; ``` **Parameters:** - `id` — The node ID to get the path for **Returns:** An array of nodes from root to the target node. Returns an empty array if the node is not found. --- ### `useSelectedNodePath` Returns the path to the currently selected node. ```typescript function useSelectedNodePath(): Node[]; ``` --- ## URL & Links These hooks read and write the URL through the host's navigation. Without a host navigation they use the browser history. ### `useSearchParams` Returns the current query params. ```typescript function useSearchParams(): Record; ``` --- ### `useSetSearchParams` Returns a setter for the query params. An object merges into the current params, and an `undefined` value removes a param. A function receives the current params and returns the full new set. ```typescript function useSetSearchParams(): ( update: | Record | ((prev: Record) => Record), options?: { replace?: boolean }, ) => void; ``` --- ### `useLinkProps` Returns props for an `
` element that opens `target` in the app on a plain primary click. Clicks with a modifier key or another mouse button stay with the browser, so open-in-new-tab works. The props also preload the target on hover and focus, and set `aria-current="page"` when the target is the current location. ```typescript function useLinkProps( target: PHNavigationTarget | undefined, open?: () => void, ): LinkProps; type PHNavigationTarget = | { driveSlug: string; nodeSlug?: string } | { settings: string | true }; ``` **Parameters:** - `target` — A drive or node, a settings tab by id (`true` opens the first tab), or `undefined` for home - `open` — Optional replacement for the default open action on a plain click --- ## Items in Selected Drive ### `useNodesInSelectedDrive` Returns all nodes (files and folders) in the selected drive. ```typescript function useNodesInSelectedDrive(): Node[] | undefined; ``` --- ### `useFileNodesInSelectedDrive` Returns only the file nodes in the selected drive. ```typescript function useFileNodesInSelectedDrive(): FileNode[] | undefined; ``` --- ### `useFolderNodesInSelectedDrive` Returns only the folder nodes in the selected drive. ```typescript function useFolderNodesInSelectedDrive(): FolderNode[] | undefined; ``` --- ### `useDocumentsInSelectedDrive` Returns all documents in the selected drive. ```typescript function useDocumentsInSelectedDrive(): PHDocument[] | undefined; ``` --- ### `useDocumentTypesInSelectedDrive` Returns the document types supported by the selected drive, as defined by the document model documents present in the drive. ```typescript function useDocumentTypesInSelectedDrive(): string[] | undefined; ``` --- ### `useNodesInSelectedDriveOrFolder` Returns the child nodes for the selected drive or folder. If a folder is selected, returns its children. Otherwise, returns the root-level nodes of the drive. ```typescript function useNodesInSelectedDriveOrFolder(): Node[]; ``` **Returns:** An array of nodes, sorted by name. Returns an empty array if no drive is selected. --- ## Items in Selected Folder ### `useNodesInSelectedFolder` Returns all nodes in the selected folder. ```typescript function useNodesInSelectedFolder(): Node[] | undefined; ``` --- ### `useFileNodesInSelectedFolder` Returns only the file nodes in the selected folder. ```typescript function useFileNodesInSelectedFolder(): FileNode[] | undefined; ``` --- ### `useFolderNodesInSelectedFolder` Returns only the folder nodes in the selected folder. ```typescript function useFolderNodesInSelectedFolder(): FolderNode[] | undefined; ``` --- ### `useDocumentsInSelectedFolder` Returns the documents in the selected folder. ```typescript function useDocumentsInSelectedFolder(): PHDocument[] | undefined; ``` --- ## Node Actions ### `useNodeActions` Returns a set of functions for performing file and folder operations in the selected drive. ```typescript function useNodeActions(): { onAddFile: ( file: File, parent: Node | undefined, ) => Promise; onAddFolder: ( name: string, parent: Node | undefined, ) => Promise; onRenameNode: (newName: string, node: Node) => Promise; onCopyNode: (src: Node, target: Node | undefined) => Promise; onMoveNode: (src: Node, target: Node | undefined) => Promise; onDuplicateNode: (src: Node) => Promise; onAddAndSelectNewFolder: (name: string) => Promise; }; ``` **Returned Functions:** | Function | Description | | ------------------------------- | ---------------------------------------------------------- | | `onAddFile(file, parent)` | Adds a file to the drive under the specified parent folder | | `onAddFolder(name, parent)` | Creates a new folder under the specified parent | | `onRenameNode(newName, node)` | Renames a node | | `onCopyNode(src, target)` | Copies a node to a target folder | | `onMoveNode(src, target)` | Moves a node to a target folder | | `onDuplicateNode(src)` | Duplicates a node in the current folder | | `onAddAndSelectNewFolder(name)` | Creates a new folder and selects it | **Example:** ```tsx useNodeActions, useSelectedFolder, } from "@powerhousedao/reactor-browser"; function FileUploader() { const { onAddFile, onAddFolder } = useNodeActions(); const selectedFolder = useSelectedFolder(); const handleFileUpload = async ( event: React.ChangeEvent, ) => { const file = event.target.files?.[0]; if (file) { await onAddFile(file, selectedFolder); } }; const handleCreateFolder = async () => { await onAddFolder("New Folder", selectedFolder); }; return (
); } ``` --- ## Authorization ### `useCanExecute` Predicts whether the current user may execute each of a set of candidate operations on a document, so you can disable a control instead of rendering one that fails on submit. Wraps [`IReactorClient.evaluateActions`](/academy/Reference/Reactor/ReactorClient). ```typescript function useCanExecute( documentId: string | null | undefined, candidates: ActionCandidate[], branch?: string, subject?: AuthSubject, ): CanExecuteState; ``` **Parameters:** | Name | Type | Required | Description | | ------------ | ------------------- | -------- | ------------------------------------------------------- | | `documentId` | `string \| null` | Yes | Document to ask about; null or undefined stays `"idle"` | | `candidates` | `ActionCandidate[]` | Yes | Operations to predict a verdict for | | `branch` | `string` | No | Defaults to `"main"` | | `subject` | `AuthSubject` | No | Defaults to the signed-in user | **Returns:** ```typescript type CanExecuteState = { status: "idle" | "loading" | "ready" | "unsupported" | "error"; evaluations?: Evaluation[]; allAllowed?: boolean; anyAllowed?: boolean; allDenied?: boolean; anyDenied?: boolean; error?: Error; refetch: () => void; }; ``` `status: "unsupported"` is not a denial. It means this deployment answers no authorization preflight, either because no reactor client module is set or because the reactor runs without the `authEnforcement` feature flag. Render your controls as you would have without asking; treating it as a denial disables every control on a deployment that never enabled enforcement. The hook waits for the signed-in identity to settle before evaluating, so a toolbar does not grey itself out on first paint and then ungrey. It re-evaluates when the document's revision moves. Two gaps are worth knowing: a `{ group }` grant depends on a roster held in a different document, whose membership change fires no event for this one, and a change event carries no scope, so any write to the document re-evaluates. Candidates are read by content rather than identity, so an inline array is fine and does not re-request on every render. **Example:** ```tsx function ApproveButton({ documentId, id }: { documentId: string; id: string }) { const { status, allAllowed, evaluations } = useCanExecute(documentId, [ { scope: "global", type: "APPROVE_EXPENSE", input: { id } }, ]); // "unsupported" means no verdict is available, so leave the control enabled. const disabled = status === "ready" && !allAllowed; const reason = evaluations?.[0].decision === "deny" ? evaluations[0].reason : undefined; return ( ); } ``` **WARNING:** A policy change landing between the answer and the submit changes the verdict. Keep handling the failure from `dispatch`; an allow here means the control is worth offering, not that the write will succeed. --- ## Modals ### `usePHModal` Returns the currently displayed modal. ```typescript function usePHModal(): PHModal | undefined; ``` **Modal Types:** ```typescript type PHModal = | { type: "createDocument"; documentType: string } | { type: "deleteItem"; id: string } | { type: "addDrive" } | { type: "upgradeDrive"; driveId: string } | { type: "deleteDrive"; driveId: string } | { type: "driveSettings"; driveId: string } | { type: "settings" } | { type: "clearStorage" } | { type: "debugSettings" } | { type: "disclaimer" } | { type: "cookiesPolicy" } | { type: "exportDocumentWithErrors"; documentId: string } | { type: "inspector" }; ``` --- ### `showPHModal` Shows a modal. ```typescript function showPHModal(modal: PHModal): void; ``` --- ### `closePHModal` Closes the currently displayed modal. ```typescript function closePHModal(): void; ``` --- ### `showCreateDocumentModal` Shows the create document modal for a specific document type. ```typescript function showCreateDocumentModal(documentType: string): void; ``` **Example:** ```tsx function CreateButton() { return ( ); } ``` --- ### `showDeleteNodeModal` Shows the delete confirmation modal for a node. ```typescript function showDeleteNodeModal(nodeOrId: Node | string): void; ``` **Parameters:** - `nodeOrId` — The node object or node ID to delete --- ## Revision History ### `useRevisionHistoryVisible` Returns whether the revision history panel is visible. ```typescript check function useRevisionHistoryVisible(): boolean | undefined; ``` --- ### `showRevisionHistory` Shows the revision history panel. ```typescript function showRevisionHistory(): void; ``` --- ### `hideRevisionHistory` Hides the revision history panel. ```typescript function hideRevisionHistory(): void; ``` --- ## Timeline ### `useSelectedTimelineItem` Returns the selected timeline item. ```typescript function useSelectedTimelineItem(): TimelineItem | null | undefined; ``` **Timeline Item Types:** ```typescript type TimelineBarItem = { id: string; type: "bar"; addSize?: 0 | 1 | 2 | 3 | 4; delSize?: 0 | 1 | 2 | 3 | 4; timestampUtcMs?: string; additions?: number; deletions?: number; revision?: number; startDate?: Date; endDate?: Date; }; type TimelineDividerItem = { id: string; type: "divider"; timestampUtcMs?: string; title?: string; subtitle?: string; revision?: number; startDate?: Date; endDate?: Date; }; type TimelineItem = TimelineBarItem | TimelineDividerItem; ``` --- ### `setSelectedTimelineItem` Sets the selected timeline item. ```typescript function setSelectedTimelineItem(item: TimelineItem | null | undefined): void; ``` --- ### `useSelectedTimelineRevision` Returns the selected timeline revision. ```typescript function useSelectedTimelineRevision(): string | number | null | undefined; ``` --- ### `setSelectedTimelineRevision` Sets the selected timeline revision. ```typescript function setSelectedTimelineRevision( revision: string | number | null | undefined, ): void; ``` --- ## Document Types ### `useDocumentTypes` Returns the document types a drive editor supports. Uses `allowedDocumentTypes` config if set, otherwise falls back to all supported document types from the reactor. ```typescript function useDocumentTypes(): string[] | undefined; ``` --- ### `useSupportedDocumentTypesInReactor` Returns the supported document types for the reactor, derived from the registered document model modules. ```typescript function useSupportedDocumentTypesInReactor(): string[] | undefined; ``` --- ## Vetra Packages ### `useVetraPackages` Returns all Vetra packages loaded by the Connect instance. ```typescript function useVetraPackages(): VetraPackage[] | undefined; ``` **VetraPackage Type:** ```typescript type VetraPackage = { id: string; name: string; description: string; category: string; author: Author; modules: { documentModelModules?: VetraDocumentModelModule[]; editorModules?: VetraEditorModule[]; subgraphModules?: SubgraphModule[]; importScriptModules?: ImportScriptModule[]; processorModules?: VetraProcessorModule[]; }; }; ``` --- ### `setVetraPackages` Sets the Vetra packages for the Connect instance. ```typescript function setVetraPackages(vetraPackages: VetraPackage[] | undefined): void; ``` --- ## Switchboard Link ### `useGetSwitchboardLink` Hook that returns a function to generate a document's switchboard URL. Only returns a function for documents in remote drives. Returns `null` for local drives or when the document/drive cannot be determined. The returned function generates a fresh bearer token and builds the switchboard URL with authentication when called. ```typescript function useGetSwitchboardLink( document: PHDocument | undefined, ): (() => Promise) | null; ``` **Parameters:** - `document` — The document to create a switchboard URL generator for **Returns:** An async function that returns the switchboard URL, or `null` if not applicable. **Example:** ```tsx useGetSwitchboardLink, useSelectedDocument, } from "@powerhousedao/reactor-browser"; function SwitchboardButton() { const [document] = useSelectedDocument(); const getSwitchboardLink = useGetSwitchboardLink(document); if (!getSwitchboardLink) { return null; // Not available for local drives } const handleClick = async () => { const url = await getSwitchboardLink(); window.open(url, "_blank"); }; return ; } ``` --- ## Editor Configuration ### `useIsExternalControlsEnabled` Gets whether external controls are enabled for a given editor. ```typescript function useIsExternalControlsEnabled(): boolean | undefined; ``` --- ### `setIsExternalControlsEnabled` Sets whether external controls are enabled for a given editor. ```typescript function setIsExternalControlsEnabled(enabled: boolean | undefined): void; ``` --- ### `useIsDragAndDropEnabled` Gets whether drag and drop is enabled for a given drive editor. ```typescript check function useIsDragAndDropEnabled(): boolean | undefined; ``` --- ### `setIsDragAndDropEnabled` Sets whether drag and drop is enabled for a given drive editor. ```typescript function setIsDragAndDropEnabled(enabled: boolean | undefined): void; ``` --- ### `useAllowedDocumentTypes` Defines the document types a drive supports. Defaults to all document types registered in the reactor. ```typescript function useAllowedDocumentTypes(): string[] | undefined; ``` --- ### `setAllowedDocumentTypes` Sets the allowed document types for a given drive editor. ```typescript function setAllowedDocumentTypes(types: string[] | undefined): void; ``` --- ## Config: Set by Object ### `setPHDriveEditorConfig` Sets the global drive editor config. Pass in a partial object of the config to set. ```typescript function setPHDriveEditorConfig(config: Partial): void; ``` **Config Options:** - `allowedDocumentTypes` — Array of allowed document type strings - `isDragAndDropEnabled` — Whether drag and drop is enabled --- ### `setPHDocumentEditorConfig` Sets the global document editor config. Pass in a partial object of the config to set. ```typescript function setPHDocumentEditorConfig( config: Partial, ): void; ``` **Config Options:** - `isExternalControlsEnabled` — Whether external controls are enabled --- ### `useSetPHDriveEditorConfig` Wrapper hook that automatically sets the global drive editor config when the component mounts. ```typescript function useSetPHDriveEditorConfig(config: Partial): void; ``` **Example:** ```tsx function MyDriveEditor() { useSetPHDriveEditorConfig({ isDragAndDropEnabled: true, allowedDocumentTypes: ["powerhouse/budget-statement", "powerhouse/invoice"], }); return
{/* editor content */}
; } ``` --- ### `useSetPHDocumentEditorConfig` Wrapper hook that automatically sets the global document editor config when the component mounts. ```typescript function useSetPHDocumentEditorConfig( config: Partial, ): void; ``` --- ## Config: Get by Key ### `usePHDriveEditorConfigByKey` Gets the value of an item in the global drive config for a given key. Strongly typed, inferred from type definition for the key. ```typescript function usePHDriveEditorConfigByKey( key: TKey, ): PHDriveEditorConfig[TKey]; ``` --- ### `usePHDocumentEditorConfigByKey` Gets the value of an item in the global document config for a given key. Strongly typed, inferred from type definition for the key. ```typescript function usePHDocumentEditorConfigByKey( key: TKey, ): PHDocumentEditorConfig[TKey]; ``` --- ## Renown SDK > Source: https://academy.vetra.io/academy/Reference/Authorization/renown-sdk/Overview A comprehensive SDK for integrating Renown authentication and user profile management into your React applications. ## Features - 🔐 **Authentication** - Complete authentication flow with session management - 👤 **User Profiles** - Fetch and manage user profile data - ⚛️ **React Integration** - Provider and hooks for seamless React integration - 🎨 **UI Component** - Ready-to-use RenownAuthButton component - 🔄 **Session Persistence** - Automatic session restoration across page reloads - 🌐 **Renown Portal** - Easy integration with Renown authentication portal - 📦 **Type-Safe** - Full TypeScript support - 🎯 **Headless** - Customizable UI with optional render props ## Installation ```bash npm install @renown/sdk # or yarn add @renown/sdk # or pnpm add @renown/sdk ``` ## Quick Start ### 1. Wrap Your App with RenownUserProvider The RenownUserProvider automatically initializes the SDK - no manual setup needed! ```typescript // app/layout.tsx or app.tsx export default function RootLayout({ children }) { return ( {children} ) } ``` Optional: Customize with loading/error components: ```typescript

Initializing...

} errorComponent={(error, retry) => (

Failed to initialize

{error.message}

)} > {children}
``` ### 2. Use Authentication in Components ```typescript // components/Header.tsx export function Header() { return (

My App

) } ``` Or use the `useUser` hook for custom implementations: ```typescript // components/CustomAuth.tsx export function CustomAuth() { const { user, loginStatus, openRenown, logout } = useUser() if (user) { return (

Welcome, {user.name || user.did}

) } return } ``` ## Documentation Structure - **[Authentication Guide](../docs/docs/01-Authentication.md)** - Comprehensive guide to implementing authentication - **[API Reference](../docs/docs/02-APIReference.md)** - Complete API documentation ## Key Concepts ### RenownUserProvider The `` component is the central authentication provider that manages auth state across your application. It must wrap your application to provide authentication context. ### useUser Hook The `useUser()` hook provides access to authentication state and methods throughout your application. It can only be used within a ``. The hook returns `connectCrypto` and `renown` instances for advanced use cases. ### Session Management The SDK automatically manages user sessions using sessionStorage, allowing users to stay logged in across page reloads within the same browser session. ### Profile Data User profile data is automatically fetched from the Renown API after successful authentication, enriching the user object with display name, avatar, and other profile information. ### UI Component The SDK provides a ready-to-use component: - **RenownAuthButton** - Smart component that adapts to auth state (shows login button or user info) This component is optional - you can build your own UI using the `useUser` hook. ## Examples ### Next.js App Router ```typescript // app/layout.tsx - Minimal setup export default function RootLayout({ children }) { return ( {children} ) } // components/Navbar.tsx - Using RenownAuthButton component 'use client' export function Navbar() { return ( ) } // app/profile/page.tsx - Using useUser hook 'use client' export default function ProfilePage() { const { user, openRenown, logout } = useUser() if (!user) { return (

Login Required

) } return (

Profile

DID: {user.did}

Name: {user.name}

{user.avatar && Avatar}
) } ``` ### React SPA ```typescript // main.tsx ReactDOM.createRoot(document.getElementById('root')!).render( ) // App.tsx - Using RenownAuthButton function App() { return (

My App

) } // Or custom with useUser hook function CustomApp() { const { user, openRenown } = useUser() return (
{user ? (

Welcome {user.name}

) : ( )}
) } ``` ## Configuration ### RenownUserProvider Props Customize the Renown SDK initialization: ```typescript } // Custom loading screen errorComponent={(error, retry) => } // Custom error screen > ``` All props are optional - RenownUserProvider uses sensible defaults. ### Environment Variables You can use environment variables for configuration: ```typescript ``` ```bash # .env NEXT_PUBLIC_RENOWN_URL=https://www.renown.id ``` ## Troubleshooting ### Context Error **Error:** `useUser must be used within a RenownUserProvider` **Solution:** Ensure your component is wrapped by ``: ```typescript {/* ✅ Can use useUser */} ``` ### Custom Renown URL If you need to use a different Renown instance: ```typescript ``` ## Resources - [GitHub Repository](https://github.com/powerhouse-inc/powerhouse) - [NPM Package](https://www.npmjs.com/package/@renown/sdk) - [Renown Portal](https://www.renown.id) ## License AGPL-3.0-only --- ## Authentication Guide > Source: https://academy.vetra.io/academy/Reference/Authorization/renown-sdk/Authentication Comprehensive guide to implementing authentication with the Renown SDK. ## Table of Contents - [Overview](#overview) - [Authentication Flow](#authentication-flow) - [Setup](#setup) - [Implementation](#implementation) - [Advanced Patterns](#advanced-patterns) - [Security Considerations](#security-considerations) ## Overview The Renown SDK provides a complete authentication system that: 1. **Auto-initializes** the Renown SDK and ConnectCrypto (zero configuration!) 2. **Manages** user sessions across page reloads 3. **Handles** authentication redirects from Renown portal 4. **Fetches** user profile data automatically 5. **Provides** React hooks, components, and context for easy integration ## Authentication Flow ### 1. Initial Load ``` User visits app ↓ RenownUserProvider initializes ↓ Check sessionStorage for existing session ↓ ├─ Session found → Restore user → Authorized └─ No session → Check URL params → Initialize SDK → Not Authorized ``` ### 2. Login Flow ``` User clicks "Login" ↓ openRenown() called ↓ Redirect to Renown Portal ↓ User authenticates ↓ Redirect back to app with DID ↓ handleRenownReturn() processes ↓ login() called ↓ Fetch user profile ↓ Store in sessionStorage ↓ Update auth state → Authorized ``` #### In-page sign-in (wallet adapters) The redirect above is the default. A host app can instead sign in **in-page** by configuring a Switchboard URL and one or more wallet adapters (RainbowKit for external wallets, Privy for social/email). The user picks a method inside the app, signs the credential with the resulting wallet session, and `renown.signIn(session)` writes and logs it in — no redirect. It falls back to the redirect flow when no switchboard/adapter is available. See [Renown authentication flow → In-page sign-in in Connect](../docs/../../03-Build/03-BuildingUserExperiences/07-Authorization/01-RenownAuthenticationFlow.md) and the `@renown/sdk/wallet` section of the SDK README for configuration. ### 3. Session Restoration ``` User refreshes page ↓ RenownUserProvider checks sessionStorage ↓ Valid session found ↓ Fetch latest profile data ↓ Restore auth state → Authorized ``` ## Setup ### Step 1: Wrap Your App with RenownUserProvider The RenownUserProvider automatically initializes the Renown SDK - no manual setup required! ```typescript // app/layout.tsx (Next.js App Router) export default function RootLayout({ children, }: { children: React.ReactNode }) { return ( {children} ) } ``` That's it! The SDK is now initialized and ready to use. ### Optional: Customize Configuration You can customize the Renown URL, network, and chain: ```typescript {children} ``` ### Optional: Add Loading/Error Screens Provide custom UI for initialization states: ```typescript

Initializing authentication...

} errorComponent={(error, retry) => (

Authentication Failed

{error.message}

)} > {children}
``` ## Implementation ### Using the RenownAuthButton Component The simplest way to add authentication is to use the built-in `RenownAuthButton` component: ```typescript 'use client' export function Header() { return (

My App

) } ``` ### Custom Login Component with useUser Hook For full control, build your own component using the `useUser` hook: ```typescript 'use client' export function CustomRenownAuthButton() { const { user, loginStatus, isLoading, openRenown, logout } = useUser() if (isLoading) { return ( ) } if (loginStatus === 'authorized' && user) { return (
{user.avatar && ( {user.name )} {user.name || user.did.slice(0, 15)}...
) } return ( ) } ``` ### Protected Route Component ```typescript 'use client' export function ProtectedRoute({ children }: { children: React.ReactNode }) { const { user, loginStatus, isLoading, openRenown } = useUser() const router = useRouter() useEffect(() => { if (!isLoading && loginStatus !== 'authorized') { // Redirect to login or show login prompt router.push('/login') } }, [isLoading, loginStatus, router]) if (isLoading) { return
Loading...
} if (loginStatus !== 'authorized' || !user) { return (

Authentication Required

Please log in to access this page

) } return <>{children} } ``` ### User Profile Display ```typescript 'use client' export function UserProfile() { const { user, logout } = useUser() if (!user) return null return (
{user.avatar && ( {user.name )}

{user.name || 'Anonymous User'}

DID
{user.did}
{user.ethAddress && ( <>
Ethereum Address
{user.ethAddress}
)} {user.email && ( <>
Email
{user.email}
)}
) } ``` ## Advanced Patterns ### Conditional Navigation Based on Auth ```typescript 'use client' export function Navigation() { const { user, loginStatus } = useUser() const isAuthorized = loginStatus === 'authorized' return ( ) } ``` ### Auth State Listener ```typescript "use client"; export function AuthStateListener() { const { user, loginStatus } = useUser(); const router = useRouter(); useEffect(() => { if (loginStatus === "authorized" && user) { // User just logged in toast.success(`Welcome back, ${user.name || "User"}!`); // Track analytics analytics.identify(user.did, { name: user.name, ethAddress: user.ethAddress, }); // Redirect to dashboard router.push("/dashboard"); } }, [loginStatus, user, router]); useEffect(() => { if (loginStatus === "not-authorized") { // User logged out toast.info("You have been logged out"); // Clear analytics analytics.reset(); // Redirect to home router.push("/"); } }, [loginStatus, router]); return null; // This is a listener component } ``` ### Custom Auth Hook with Additional Logic ```typescript "use client"; interface ExtendedAuthState { user: User | null; isAuthenticated: boolean; isLoading: boolean; hasCompletedProfile: boolean; login: () => void; logout: () => Promise; } export function useUser(): ExtendedAuthState { const auth = useRenownAuth(); const [hasCompletedProfile, setHasCompletedProfile] = useState(false); useEffect(() => { if (auth.user) { // Check if user has completed their profile const isComplete = !!( auth.user.name && auth.user.avatar && auth.user.email ); setHasCompletedProfile(isComplete); } else { setHasCompletedProfile(false); } }, [auth.user]); return { user: auth.user, isAuthenticated: auth.loginStatus === "authorized", isLoading: auth.isLoading, hasCompletedProfile, login: auth.openRenown, logout: auth.logout, }; } ``` ### Role-Based Access Control ```typescript 'use client' interface RBACProps { children: ReactNode allowedRoles?: string[] fallback?: ReactNode } export function RoleBasedAccess({ children, allowedRoles = [], fallback =
Access Denied
}: RBACProps) { const { user, loginStatus } = useUser() if (loginStatus !== 'authorized' || !user) { return fallback } // Check user roles (you'd fetch this from your backend) const userRoles = getUserRoles(user.did) // Implement this const hasAccess = allowedRoles.length === 0 || allowedRoles.some(role => userRoles.includes(role)) return hasAccess ? <>{children} : fallback } // Usage function AdminPanel() { return (

Admin Panel

{/* Admin content */}
) } ``` ## Security Considerations ### 1. Token Storage The SDK stores session data in `sessionStorage` (not `localStorage`) for security: - ✅ Session data clears when tab closes - ✅ Not accessible across tabs - ✅ Not persisted across browser sessions - ✅ Protected from XSS via httpOnly (for API tokens) ### 2. DID Validation Always validate DIDs before processing: ```typescript function isValidDID(did: string): boolean { // Must start with did:pkh: if (!did.startsWith("did:pkh:")) return false; // Must have correct number of parts const parts = did.split(":"); if (parts.length !== 5) return false; // Validate ethereum address format const address = parts[4]; if (!address.match(/^0x[a-fA-F0-9]{40}$/)) return false; return true; } ``` ### 3. Secure Communication Always use HTTPS for Renown URLs: ```typescript const RENOWN_URL = process.env.NEXT_PUBLIC_RENOWN_URL; if (RENOWN_URL && !RENOWN_URL.startsWith("https://")) { console.warn("WARNING: Renown URL should use HTTPS"); } ``` ### 4. Session Timeout Implement session timeout for security: ```typescript const SESSION_TIMEOUT = 24 * 60 * 60 * 1000; // 24 hours function isSessionValid(timestamp: number): boolean { const now = Date.now(); const age = now - timestamp; return age < SESSION_TIMEOUT; } ``` ### 5. Server-Side Verification **Never trust client-side auth alone**. To verify a raw bearer token (e.g. from an `Authorization` header), use `verifyAuthBearerToken` from `@renown/sdk/node`: ```typescript export async function GET(request: Request) { const token = request.headers.get("authorization")?.replace(/^Bearer /, ""); if (!token) return new Response("Unauthorized", { status: 401 }); const verified = await verifyAuthBearerToken(token); if (!verified) return new Response("Invalid credentials", { status: 403 }); const { address } = verified.verifiableCredential.credentialSubject; return Response.json({ address }); } ``` #### SSR session cookie To render authenticated pages on the server (Next.js and similar): 1. On login the client mints a bearer token (`renown.getBearerToken()`) and stores it — with a `RenownSessionProfile` display hint (`name`/`avatar` plus `documentId`/`username`/`userImage`) — in an HttpOnly cookie (name exported as `RENOWN_SESSION_COOKIE`, value built with `serializeRenownSessionCookie`). `RenownProvider`'s session-cookie sync does this automatically when given a `session`. Type your route handler with the exported `RenownSessionCookie` instead of redeclaring the payload shape. 2. A Data Access Layer reads the cookie and calls `verifyRenownSession`, memoized per request: ```typescript export const verifySession = cache(async () => { const cookie = (await cookies()).get(RENOWN_SESSION_COOKIE)?.value; if (!cookie) return null; return (await verifyRenownSession(cookie, { switchboardUrl: SWITCHBOARD_URL })) ?? null; }); ``` `verifyRenownSession` takes the cookie value, verifies the JWT (signature + expiry), and merges the display hint into `user` (unverified — display only), rebuilding `user.ens` and, when `documentId` is present, `user.profile`. Pass `verifyCredential: true` to also re-check the credential against the switchboard so a **revoked/expired** credential is rejected; it defaults to `false` (token-only, no network). `readSessionClaims(cookieValue)` gives a cheap, signature-free decode for optimistic checks only (e.g. an edge redirect) — never trust it for access control. 3. Pass the result to `RenownProvider session={await verifySession()}` so the server render is authenticated (no flash), and consumers read it via `useRenownAuth()`. A `null` session is as useful as a populated one: it tells the first render the visitor is definitively signed out, so `useRenownAuthAsync()` reports `"unauthenticated"` immediately instead of spinning. The cookie seeds the server render and hydration; `localStorage` takes over after mount, since that holds the credential the SDK restores. #### Revalidation and profile refresh on the client On mount the client re-checks the restored credential against the switchboard and logs out if it was revoked/expired (`revalidate` prop on `RenownProvider`, default `"always"`). In the browser this is **non-blocking and fail-open** (a transient outage keeps the session); Node scripts block on it. `renown.revalidate()` runs the same check on demand. Separately, `renown.refreshProfile()` re-reads `username`/`userImage` and updates the store only if they changed. It runs on every browser start **regardless of `revalidate`**, because it can never log anyone out — so apps that set `revalidate: "never"` still pick up a renamed user or a new avatar. ## Best Practices ### 1. Handle Loading States Always handle loading states to provide good UX: ```typescript function MyComponent() { const { user, isLoading, loginStatus } = useUser() if (isLoading) { return // Show skeleton/spinner } // Now safe to use user/loginStatus } ``` ### 2. Graceful Degradation Provide fallbacks for unauthenticated users: ```typescript function FeatureSection() { const { user } = useUser() return (
{user ? ( ) : ( )}
) } ``` ### 3. Cleanup on Unmount Clean up subscriptions and listeners: ```typescript useEffect(() => { const handleAuthChange = () => { // Handle auth changes }; // Subscribe to auth events const unsubscribe = subscribeToAuthEvents(handleAuthChange); return () => { unsubscribe(); // Cleanup }; }, []); ``` ### 4. Error Boundaries Wrap auth components in error boundaries: ```typescript }> ``` ## Troubleshooting ### Issue: Auth state not updating **Cause:** Component not re-rendering when auth changes **Solution:** Ensure you're using `useUser()` hook, not accessing `window.renown` directly ```typescript // ❌ Wrong const user = window.renown?.user; // ✅ Correct const { user } = useUser(); ``` ### Issue: Session not persisting **Cause:** SessionStorage might be disabled or cleared **Solution:** Check browser settings and handle gracefully ```typescript try { SessionStorageManager.setUserData(data); } catch (error) { console.warn("SessionStorage not available:", error); // Fallback to in-memory storage } ``` ### Issue: Multiple login popups **Cause:** `openRenown()` called multiple times **Solution:** Debounce the login button ```typescript const handleLogin = useCallback( debounce(() => { openRenown(); }, 1000), [openRenown], ); ``` ## Next Steps - Read the [API Reference](../docs/docs/02-APIReference.md) for detailed documentation --- ## API Reference > Source: https://academy.vetra.io/academy/Reference/Authorization/renown-sdk/APIReference Complete API reference for the Renown SDK. ## Table of Contents - [Components](#components) - [Hooks](#hooks) - [Functions](#functions) - [Types](#types) - [Classes](#classes) - [Constants](#constants) ## Components ### `RenownUserProvider` Central authentication provider that automatically initializes the Renown SDK. #### Props ```typescript interface RenownUserProviderProps { children: React.ReactNode; renownUrl?: string; networkId?: string; chainId?: string; loadingComponent?: React.ReactNode; errorComponent?: (error: Error, retry: () => void) => React.ReactNode; } ``` | Prop | Type | Default | Description | | ------------------ | ----------------------------- | ------------------------- | --------------------------------------- | | `children` | `React.ReactNode` | - | **Required.** Child components | | `renownUrl` | `string` | `'https://www.renown.id'` | Renown service URL | | `networkId` | `string` | `'eip155'` | Network identifier | | `chainId` | `string` | `'1'` | Chain identifier | | `loadingComponent` | `React.ReactNode` | undefined | Custom loading UI during initialization | | `errorComponent` | `(error, retry) => ReactNode` | undefined | Custom error UI if initialization fails | #### Example **Basic usage (auto-initializes with defaults):** ```typescript ``` **With custom configuration:** ```typescript

Initializing...

} errorComponent={(error, retry) => (

Failed to initialize

{error.message}

)} >
``` #### Behavior - **Automatically initializes** Renown SDK and ConnectCrypto on mount - Creates global `window.renown` and `window.connectCrypto` instances - Checks sessionStorage for existing sessions - Handles Renown authentication redirects - Shows `loadingComponent` during initialization (if provided) - Shows `errorComponent` if initialization fails (if provided) - If no custom components provided, renders children immediately - Provides auth context to all children --- ### `RenownAuthButton` Smart button component that adapts based on authentication state. #### Props ```typescript interface RenownAuthButtonProps { className?: string; profileBaseUrl?: string; renderAuthenticated?: (props: RenownAuthButtonRenderProps) => React.ReactNode; renderUnauthenticated?: (props: { openRenown: () => void; isLoading: boolean; }) => React.ReactNode; renderLoading?: () => React.ReactNode; showUsername?: boolean; showLogoutButton?: boolean; logoutButtonText?: string; } ``` | Prop | Type | Default | Description | | ----------------------- | ---------- | --------------------------------- | ---------------------------- | | `className` | `string` | `""` | Custom CSS class | | `profileBaseUrl` | `string` | `"https://www.renown.id/profile"` | Base URL for profile | | `renderAuthenticated` | `function` | Default renderer | Custom authenticated state | | `renderUnauthenticated` | `function` | Default renderer | Custom unauthenticated state | | `renderLoading` | `function` | Default renderer | Custom loading state | | `showUsername` | `boolean` | `true` | Show username next to avatar | | `showLogoutButton` | `boolean` | `false` | Show logout button | | `logoutButtonText` | `string` | `"Logout"` | Logout button text | #### Example ```typescript // Basic usage // Custom rendering (
{user.name}
)} renderUnauthenticated={({ openRenown }) => ( )} /> ``` --- ### `RenownLoginButton` A login button with Renown branding. By default, clicking the button triggers login directly. Optionally shows a popover with connect option. #### Props ```typescript interface RenownLoginButtonProps { onLogin: (() => void) | undefined; darkMode?: boolean; style?: CSSProperties; className?: string; renderTrigger?: (props: { onMouseEnter: () => void; onMouseLeave: () => void; isLoading: boolean; }) => ReactNode; showPopover?: boolean; } ``` | Prop | Type | Default | Description | | --------------- | ------------------------- | ------- | ----------------------------------------------------------------------- | | `onLogin` | `() => void \| undefined` | - | Callback when login is requested | | `darkMode` | `boolean` | `false` | Enable dark mode styling | | `style` | `CSSProperties` | - | Custom styles for the button | | `className` | `string` | - | Custom class name | | `renderTrigger` | `function` | - | Custom render function for the trigger button | | `showPopover` | `boolean` | `false` | Show a popover with connect option instead of triggering login directly | #### Example ```typescript // Direct login (default) - clicks trigger onLogin immediately // With popover - shows hover popover with "Connect" button // Dark mode // Custom trigger ( )} /> ``` --- ## Hooks ### `useUser()` Access authentication state and methods. #### Returns ```typescript interface RenownUserContextValue { user: User | null; loginStatus: LoginStatus; isLoading: boolean; isInitialized: boolean; login: (userDid?: string) => Promise; logout: () => Promise; openRenown: () => void; connectCrypto: IConnectCrypto | null; renown: IRenown | null; } ``` | Property | Type | Description | | --------------- | ------------------------ | --------------------------------------------------- | | `user` | `User \| null` | Current authenticated user or null | | `loginStatus` | `LoginStatus` | Current authentication status | | `isLoading` | `boolean` | Whether an auth operation is in progress | | `isInitialized` | `boolean` | Whether the auth system has initialized | | `login` | `function` | Authenticate with optional DID | | `logout` | `function` | Log out current user | | `openRenown` | `function` | Open Renown authentication portal | | `connectCrypto` | `IConnectCrypto \| null` | ConnectCrypto instance for cryptographic operations | | `renown` | `IRenown \| null` | Renown SDK instance | #### Example ```typescript function MyComponent() { const { user, loginStatus, isLoading, login, logout, openRenown, connectCrypto, renown } = useUser() if (isLoading) return
Loading...
if (!user) return return } ``` #### Throws Throws an error if used outside of ``: ``` Error: useUser must be used within a RenownUserProvider ``` --- ## Functions ### `initRenown()` Initialize the Renown SDK. #### Signature ```typescript function initRenown( did: string, networkId: string, renownUrl: string, ): Promise; ``` #### Parameters | Parameter | Type | Description | | ----------- | -------- | ----------------------------------- | | `did` | `string` | Decentralized identifier (DID) | | `networkId` | `string` | Network identifier (e.g., 'eip155') | | `renownUrl` | `string` | Renown service URL | #### Returns `Promise` - Initialized Renown instance #### Example ```typescript const renown = await initRenown( "did:pkh:eip155:1:0x123...", "eip155", "https://www.renown.id", ); ``` --- ### `login()` Authenticate a user with Renown. #### Signature ```typescript function login( userDid: string | undefined, renown: IRenown | undefined, connectCrypto: IConnectCrypto | undefined, ): Promise; ``` #### Parameters | Parameter | Type | Description | | --------------- | ----------------------------- | -------------------------- | | `userDid` | `string \| undefined` | User's DID to authenticate | | `renown` | `IRenown \| undefined` | Renown instance | | `connectCrypto` | `IConnectCrypto \| undefined` | ConnectCrypto instance | #### Returns `Promise` - Authenticated user or undefined #### Side Effects - Stores user session in sessionStorage - Fetches user profile data - Updates auth state #### Example ```typescript const user = await login( "did:pkh:eip155:1:0x123...", window.renown, window.connectCrypto, ); ``` --- ### `logout()` Log out the current user. #### Signature ```typescript function logout(): Promise; ``` #### Returns `Promise` #### Side Effects - Clears sessionStorage - Calls renown.logout() - Removes JWT handler #### Example ```typescript await logout(); ``` --- ### `openRenown()` Open the Renown authentication portal. #### Signature ```typescript function openRenown(): void; ``` #### Returns `void` #### Behavior - Constructs authentication URL with current DID - Adds network and chain parameters - Sets return URL to current location - Redirects to Renown portal #### Example ```typescript function MyLoginButton() { return } // Or use the built-in RenownAuthButton component function Header() { return } ``` --- ### `handleRenownReturn()` Process authentication redirect from Renown. #### Signature ```typescript function handleRenownReturn(): Promise; ``` #### Returns `Promise` #### Behavior - Checks URL for authentication parameters - Extracts user DID from query string - Calls login with the DID - Cleans up URL parameters #### Example ```typescript // Called automatically by UserProvider useEffect(() => { handleRenownReturn(); }, []); ``` --- ### `fetchProfileDataForUser()` Fetch user profile data from Renown API. #### Signature ```typescript function fetchProfileDataForUser(user: User): Promise; ``` #### Parameters | Parameter | Type | Description | | --------- | ------ | --------------------------------------- | | `user` | `User` | User object to enrich with profile data | #### Returns `Promise` - User with profile data #### Behavior - Extracts ETH address from user's DID - Calls Renown profile API - Enriches user object with profile data (name, avatar, etc.) - Returns original user if profile not found #### Example ```typescript const userWithProfile = await fetchProfileDataForUser(user); console.log(userWithProfile.name); // Display name console.log(userWithProfile.avatar); // Avatar URL ``` --- ### `reauthenticateFromSession()` Restore authentication from stored session. #### Signature ```typescript function reauthenticateFromSession(): Promise; ``` #### Returns `Promise` - Restored user or null #### Behavior - Checks for stored session in sessionStorage - Calls login with stored DID - Fetches fresh profile data - Returns null if session invalid or expired #### Example ```typescript const user = await reauthenticateFromSession(); if (user) { console.log("Session restored for:", user.did); } ``` --- ### `extractEthAddressFromDid()` Extract Ethereum address from a DID. #### Signature ```typescript function extractEthAddressFromDid(did: string): string | null; ``` #### Parameters | Parameter | Type | Description | | --------- | -------- | ---------------------------------------------- | | `did` | `string` | DID string (e.g., 'did:pkh:eip155:1:0x123...') | #### Returns `string | null` - Ethereum address or null if invalid #### Example ```typescript const address = extractEthAddressFromDid("did:pkh:eip155:1:0x1234..."); console.log(address); // '0x1234...' ``` --- ## Types ### `User` Represents an authenticated user. ```typescript interface User { did: string; // Decentralized identifier address: string; // Ethereum address name?: string; // Display name from profile email?: string; // Email address avatar?: string; // Avatar image URL ethAddress?: string; // Ethereum address (duplicate of address) } ``` --- ### `LoginStatus` Authentication status enumeration. ```typescript type LoginStatus = | "initial" // Not yet checked | "checking" // Currently checking auth | "authorized" // User is authenticated | "not-authorized"; // User is not authenticated ``` --- ### `RenownUserContextValue` Type for the authentication context value. ```typescript interface RenownUserContextValue { user: User | null; loginStatus: LoginStatus; isLoading: boolean; isInitialized: boolean; login: (userDid?: string) => Promise; logout: () => Promise; openRenown: () => void; connectCrypto: IConnectCrypto | null; renown: IRenown | null; } ``` --- ### `IRenown` Interface for the Renown instance. ```typescript interface IRenown { user: User | undefined | (() => Promise); login: (did: string) => Promise; logout: () => Promise; on: (event: string, handler: Function) => Unsubscribe; } ``` --- ### `IConnectCrypto` Interface for the ConnectCrypto instance. ```typescript interface IConnectCrypto { did: () => Promise; // Additional methods... } ``` --- ## Classes ### `ConnectCrypto` Manages cryptographic operations and DID generation. #### Constructor ```typescript constructor(keyStorage: IKeyStorage) ``` #### Parameters | Parameter | Type | Description | | ------------ | ------------- | -------------------------- | | `keyStorage` | `IKeyStorage` | Key storage implementation | #### Methods ##### `did()` Get the DID for the current key. ```typescript async did(): Promise ``` **Returns:** `Promise` - The DID **Example:** ```typescript const connectCrypto = new ConnectCrypto(new BrowserKeyStorage()); const did = await connectCrypto.did(); console.log(did); // 'did:pkh:eip155:1:0x...' ``` --- ### `BrowserKeyStorage` Browser-based key storage using IndexedDB. #### Constructor ```typescript constructor(); ``` #### Usage ```typescript const keyStorage = new BrowserKeyStorage(); const connectCrypto = new ConnectCrypto(keyStorage); ``` --- ### `SessionStorageManager` Manages user session persistence. #### Static Methods ##### `setUserData()` Store user session data. ```typescript static setUserData(data: { user: User userDid: string loginStatus: LoginStatus timestamp: number }): void ``` **Example:** ```typescript SessionStorageManager.setUserData({ user: currentUser, userDid: currentUser.did, loginStatus: "authorized", timestamp: Date.now(), }); ``` ##### `getUserData()` Retrieve stored user session. ```typescript static getUserData(): { user: User userDid: string loginStatus: LoginStatus timestamp: number } | null ``` **Returns:** Session data or null **Example:** ```typescript const session = SessionStorageManager.getUserData(); if (session) { console.log("Found session for:", session.user.did); } ``` ##### `clearUserData()` Clear stored session. ```typescript static clearUserData(): void ``` **Example:** ```typescript SessionStorageManager.clearUserData(); ``` ##### `isUserDataValid()` Check if session data is valid. ```typescript static isUserDataValid(data: { user: User userDid: string loginStatus: LoginStatus timestamp: number }): boolean ``` **Returns:** `boolean` - Whether session is valid **Example:** ```typescript const data = SessionStorageManager.getUserData(); if (data && SessionStorageManager.isUserDataValid(data)) { // Session is valid } ``` ##### `getStoredUserDid()` Get stored user DID. ```typescript static getStoredUserDid(): string | null ``` **Returns:** `string | null` - Stored DID or null --- ## Constants ### `RENOWN_URL` Default Renown service URL. ```typescript const RENOWN_URL: string = "https://www.renown.id"; ``` --- ### `RENOWN_NETWORK_ID` Default network identifier. ```typescript const RENOWN_NETWORK_ID: string = "eip155"; ``` --- ### `RENOWN_CHAIN_ID` Default chain identifier. ```typescript const RENOWN_CHAIN_ID: string = "1"; ``` --- ## Global Window Extensions The SDK extends the global `Window` interface: ```typescript declare global { interface Window { renown?: IRenown; connectCrypto?: IConnectCrypto; reactor?: { setGenerateJwtHandler: ( handler: (driveUrl: string) => Promise, ) => void; removeJwtHandler: () => void; }; } } ``` ### `window.renown` Global Renown instance after initialization. **Usage:** ```typescript if (window.renown) { const user = await window.renown.login("did:pkh:..."); } ``` ### `window.connectCrypto` Global ConnectCrypto instance after initialization. **Usage:** ```typescript if (window.connectCrypto) { const did = await window.connectCrypto.did(); } ``` --- ## Error Handling ### Common Errors #### `useUser must be used within a RenownUserProvider` **Cause:** Using `useUser()` outside of `` **Solution:** Wrap your component tree with `` ```typescript {/* ✅ Can use useUser */} ``` #### `Invalid DID format` **Cause:** DID doesn't match expected format `did:pkh:networkId:chainId:address` **Solution:** Ensure DID is properly formatted ```typescript // ✅ Valid "did:pkh:eip155:1:0x1234567890123456789012345678901234567890"; // ❌ Invalid "did:1234567890123456789012345678901234567890"; ``` #### `Renown or ConnectCrypto not available` **Cause:** SDK initialization failed **Solution:** The RenownUserProvider automatically initializes the SDK. If you see this error: 1. Check that `` is mounted 2. Check browser console for initialization errors 3. Verify network connectivity to Renown service 4. Try providing an `errorComponent` prop to see detailed error messages ```typescript (

Init failed: {error.message}

)} >
``` --- ## TypeScript Support The SDK is fully typed. Import types as needed: ```typescript User, LoginStatus, RenownUserContextValue, IRenown, IConnectCrypto, } from "@renown/sdk"; ``` --- ## Version Compatibility | SDK Version | React Version | TypeScript Version | | ----------- | ------------- | ------------------ | | 5.x | 18.x - 19.x | 4.5+ | | 4.x | 18.x | 4.5+ | --- ## Related Documentation - [Authentication Guide](../docs/docs/01-Authentication.md) - Comprehensive auth implementation guide --- ## CLI Identity & Authentication > Source: https://academy.vetra.io/academy/Reference/Authorization/renown-sdk/CLIIdentity This guide covers how to authenticate the Powerhouse CLI with your Ethereum identity and use that identity in the Switchboard for authenticated operations with remote services. ## Overview The Powerhouse CLI uses **Renown** for identity management. When you run `ph login`, the CLI: 1. Generates a cryptographic keypair (ECDSA P-256) stored locally 2. Creates a DID (Decentralized Identifier) in `did:key:...` format 3. Opens your browser to authorize this DID to act on behalf of your Ethereum address 4. Stores the authorization credentials for future use This enables the CLI and Switchboard to authenticate with remote drives and services using your Ethereum identity. ## Quick Start ```bash # 1. Login with your Ethereum wallet ph login # 2. Start switchboard with your identity ph switchboard --use-identity ``` ## The Login Command ### Basic Usage ```bash # Authenticate with Renown ph login # Check your authentication status ph login --status # Show only your CLI's DID (useful for scripts) ph login --show-did # Logout and clear credentials ph logout ``` ### How It Works ``` ┌──────────────────────────────────────────────────────────────────────┐ │ ph login │ ├──────────────────────────────────────────────────────────────────────┤ │ │ │ 1. CLI generates/loads keypair (.keypair.json) │ │ └─► Creates DID: did:key:zDnae... │ │ │ │ 2. Opens browser to Renown portal │ │ └─► URL includes CLI's DID │ │ │ │ 3. User connects wallet & signs authorization │ │ └─► "I authorize did:key:zDnae... to act on my behalf" │ │ │ │ 4. Renown issues credential │ │ └─► Links CLI DID to user's ETH address │ │ │ │ 5. CLI stores credentials (.auth.json in project dir) │ │ └─► Ready to authenticate with remote services │ │ │ └──────────────────────────────────────────────────────────────────────┘ ``` ### Output Example ``` $ ph login Initializing cryptographic identity... CLI DID: did:key:zDnaej4f3d83mmCodYjZyHzUKDSt2dGVKjzD8dd22AS83GtMo Opening browser for authentication... Session ID: a1b2c3d4... Waiting for authentication in browser (timeout in 300 seconds) Please connect your wallet and authorize this CLI to act on your behalf. Waiting................. Successfully authenticated! ETH Address: 0x1234...abcd User DID: did:pkh:eip155:1:0x1234...abcd CLI DID: did:key:zDnaej4f3d83mmCodYjZyHzUKDSt2dGVKjzD8dd22AS83GtMo The CLI can now act on behalf of your Ethereum identity. ``` ## Storage Locations All identity files are stored **per-project** in the current working directory: ``` your-project/ ├── .keypair.json # CLI's cryptographic keypair (did:key identity) ├── .auth.json # Authentication credentials (ETH address, User DID, etc.) ├── powerhouse.config.json └── ... ``` This means each project can have its own identity and credentials, which is useful for: - Different projects requiring different identities - Team members using the same machine - Separating development and production identities - Isolating credentials between projects ### Environment Variable For CI/CD environments, provide the keypair via environment variable: ```bash # Export keypair as JSON export PH_RENOWN_PRIVATE_KEY='{"publicKey":{"kty":"EC",...},"privateKey":{"kty":"EC",...}}' # Now ph login --show-did will use this keypair ph login --show-did ``` ## Using Identity in Switchboard ### Starting with Identity ```bash # Enable identity using keypair from ph login ph switchboard --use-identity # Output includes identity DID: # ➜ Switchboard: http://localhost:4001 # ➜ Identity: did:key:zDnaej4f3d83mmCodYjZyHzUKDSt2dGVKjzD8dd22AS83GtMo ``` ### Identity Options | Option | Description | | ----------------------- | ------------------------------------- | | `--use-identity` | Enable identity using `.keypair.json` | | `--keypair-path ` | Use a custom keypair file | | `--require-identity` | Fail if no keypair exists | ### Requiring Identity Use `--require-identity` when the switchboard must have a valid identity: ```bash # Fails if no keypair exists (user must run ph login first) ph switchboard --require-identity # Error if not logged in: # Error: Identity required but failed to initialize. Run "ph login" first. ``` ### Custom Keypair Path ```bash # Use a specific keypair file ph switchboard --use-identity --keypair-path /path/to/my-keypair.json ``` ## How the Switchboard Uses Identity When the Switchboard starts with identity enabled, it can: 1. **Authenticate with Remote Drives**: Generate bearer tokens for API requests 2. **Sign Operations**: Cryptographically sign document operations 3. **Identify Itself**: Present its DID to remote services ### Getting Bearer Tokens The Switchboard can generate bearer tokens for authenticated API calls: ```typescript // Get the switchboard's DID const did = await getConnectDid(); console.log("Switchboard DID:", did); // Get a bearer token for a remote drive const token = await getBearerToken("https://remote.drive.example.com"); console.log("Bearer Token:", token); // Use in API requests const response = await fetch("https://remote.drive.example.com/api/documents", { headers: { Authorization: `Bearer ${token}`, }, }); ``` ## Security Considerations ### Identity Files Protection The `.keypair.json` and `.auth.json` files contain sensitive data. Protect them: ```bash # Add to .gitignore echo ".keypair.json" >> .gitignore echo ".auth.json" >> .gitignore # Set restrictive permissions (Unix) chmod 600 .keypair.json .auth.json ``` ### CI/CD Best Practices For automated environments: 1. **Use environment variables** instead of files 2. **Store secrets securely** (GitHub Secrets, AWS Secrets Manager, etc.) 3. **Rotate keys** periodically 4. **Limit scope** - use separate identities for different environments ```yaml # GitHub Actions example jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup identity env: PH_RENOWN_PRIVATE_KEY: ${{ secrets.PH_KEYPAIR }} run: | ph switchboard --use-identity & ``` ### Authorization Scope When you authorize the CLI's DID: - It can act on behalf of your Ethereum address - It can authenticate with services that trust Renown - It **cannot** sign Ethereum transactions or access your wallet ## Troubleshooting ### "No existing keypair found" ```bash $ ph switchboard --require-identity Error: Identity required but failed to initialize. Run "ph login" first. ``` **Solution**: Run `ph login` to create a keypair: ```bash ph login # Then retry ph switchboard --require-identity ``` ### "Authentication timed out" The browser authentication didn't complete in time. **Solutions**: - Increase timeout: `ph login --timeout 600` - Check browser opened correctly - Ensure you completed the wallet connection ### Different DID than expected Each project directory has its own `.keypair.json`. **Check current DID**: ```bash ph login --show-did ``` **Use specific keypair**: ```bash ph switchboard --use-identity --keypair-path ~/.shared-keypair.json ``` ## API Reference ### Login Options | Option | Type | Default | Description | | -------------- | ------- | ------------------------------ | ---------------------- | | `--renown-url` | string | `https://renown.powerhouse.io` | Renown server URL | | `--timeout` | number | `300` | Auth timeout (seconds) | | `--logout` | boolean | `false` | Clear credentials | | `--status` | boolean | `false` | Show auth status | | `--show-did` | boolean | `false` | Print DID only | ### Switchboard Identity Options | Option | Type | Default | Description | | -------------------- | ------- | --------------- | -------------------- | | `--use-identity` | boolean | `false` | Enable identity | | `--keypair-path` | string | `.keypair.json` | Keypair file path | | `--require-identity` | boolean | `false` | Fail without keypair | ### Exported Functions (Switchboard) ```typescript // Get the ConnectCrypto instance function getConnectCrypto(): IConnectCrypto | null; // Get the switchboard's DID async function getConnectDid(): Promise; // Get a bearer token for a remote URL async function getBearerToken( driveUrl: string, address?: string, refresh?: boolean, ): Promise; ``` ## Related Documentation - [Renown SDK Overview](../docs/docs/00-Overview.md) - Introduction to Renown - [Authentication Guide](../docs/docs/01-Authentication.md) - Web app authentication - [API Reference](../docs/docs/02-APIReference.md) - Full SDK reference --- ## Powerhouse CLI > Source: https://academy.vetra.io/academy/Reference/CLITooling/PowerhouseCLI ### Installing the Powerhouse CLI **TIP:** The **Powerhouse CLI tool** is the only essential tool to install on this page. Install it with the command below. You can find all of the commands on this page, similar to what would displayed when using ph --help or ph _command_ --help. Use the table of content or the search function to find what you are looking for. The Powerhouse CLI (`ph-cmd`) is a command-line interface tool that provides essential commands for managing Powerhouse Vetra projects. You can get access to the Powerhouse ecosystem tools by installing them globally. ```bash pnpm install -g ph-cmd ``` {/* AUTO-GENERATED-CLI-COMMANDS-START */} {/* This content is automatically generated. Do not edit directly. */} ## Quick Reference | Command | Description | Example | |---------|-------------|---------| | `ph init` | Initialize a new project | `ph init my-project --pnpm` | | `ph use` | Switch to a release version | `ph use staging` | | `ph update` | Update dependencies to latest | `ph update` | | `ph setup-globals` | Initialize global project | `ph setup-globals my-globals` | | `ph use-local` | Use local monorepo dependencies | `ph use-local ../powerhouse` | --- ### ph-cmd Commands - [Init](#init) - [Use](#use) - [Update](#update) - [Self Update](#self-update) - [Setup Globals](#setup-globals) - [Use Local](#use-local) ## Init Initialize a new project --- ## Parameters ### Arguments **Name** - The name of your project. A new directory will be created in your current directory with this name. - Usage: `[name]` ### Options **Name** - The name of your project. A new directory will be created in your current directory with this name. - Usage: `--name, -n ` **Package Manager** - Package manager that installs the project: `npm`, `pnpm`, `yarn` or `bun`. Defaults to the one that launched ph (npx, pnpm dlx, bunx), else the one that installed ph (unless npm), else bun, then pnpm 10.14 or later if installed, else npm. - Usage: `--package-manager, -p ` **Tag** - Specify the release tag to use for your project. Can be one of: "latest", "staging", "dev", "rc", or "next". - Usage: `--tag, -t ` **Version** - Specify the exact semver release version to use for your project. - Usage: `--version, -v ` **Remote Drive** - Remote drive identifier. - Usage: `--remote-drive, -r ` **Clone** - Path to an existing scaffolded project to clone instead of resolving deps from scratch. Install runs offline from the cloned project's lockfile: pnpm-lock.yaml with pnpm, package-lock.json with npm (other package managers are refused; --version/--tag are ignored). - Usage: `--clone ` ### Flags **Npm** - Use 'npm' as package manager - Usage: `--npm` **Pnpm** - Use 'pnpm' as package manager - Usage: `--pnpm` **Yarn** - Use 'yarn' as package manager - Usage: `--yarn` **Bun** - Use 'bun' as package manager - Usage: `--bun` **Dev** - Use the `dev` release tag. - Usage: `--dev, -d` **Staging** - Use the `staging` release tag. - Usage: `--staging, -s` **Rc** - Use the `rc` release tag. - Usage: `--rc` **Next** - Use the `next` release tag. - Usage: `--next` **Uniform Runners** - Deprecated: no longer needed, `ph init` always scaffolds the hosts/ folder. Accepted with a warning; will be removed in a future release. - Usage: `--uniform-runners` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Use Specify the release version of Powerhouse dependencies to use. --- ## Parameters ### Arguments **Tag** - Specify the release tag to use for your project. Can be one of: "latest", "staging", "dev", "rc", or "next". - Usage: `[tag]` ### Options **Tag** - Specify the release tag to use for your project. Can be one of: "latest", "staging", "dev", "rc", or "next". - Usage: `--tag, -t ` **Version** - Specify the exact semver release version to use for your project. - Usage: `--version, -v ` ### Flags **Skip Install** - Skip running `install` with your package manager - Usage: `--skip-install, -s` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Update Update your Powerhouse dependencies and installed packages to their latest versions ### Flags **Skip Install** - Skip running `install` with your package manager - Usage: `--skip-install, -s` **Update Packages** - Auto-update installed packages (powerhouse.config.json) to their newest same-major version - Usage: `--update-packages` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Self Update Update the globally installed ph to the newest version of its release stream ### Options **Tag** - dist-tag to install (defaults to the running build's stream; e.g. latest, dev, next) - Usage: `--tag, -t ` ### Flags **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Setup Globals Initialize a new global project --- ## Parameters ### Arguments **Name** - The name of your project. A new directory will be created in your current directory with this name. - Usage: `[name]` ### Options **Name** - The name of your project. A new directory will be created in your current directory with this name. - Usage: `--name, -n ` **Package Manager** - Package manager that installs the project: `npm`, `pnpm`, `yarn` or `bun`. Defaults to the one that launched ph (npx, pnpm dlx, bunx), else the one that installed ph (unless npm), else bun, then pnpm 10.14 or later if installed, else npm. - Usage: `--package-manager, -p ` **Tag** - Specify the release tag to use for your project. Can be one of: "latest", "staging", "dev", "rc", or "next". - Usage: `--tag, -t ` **Version** - Specify the exact semver release version to use for your project. - Usage: `--version, -v ` **Remote Drive** - Remote drive identifier. - Usage: `--remote-drive, -r ` **Clone** - Path to an existing scaffolded project to clone instead of resolving deps from scratch. Install runs offline from the cloned project's lockfile: pnpm-lock.yaml with pnpm, package-lock.json with npm (other package managers are refused; --version/--tag are ignored). - Usage: `--clone ` ### Flags **Npm** - Use 'npm' as package manager - Usage: `--npm` **Pnpm** - Use 'pnpm' as package manager - Usage: `--pnpm` **Yarn** - Use 'yarn' as package manager - Usage: `--yarn` **Bun** - Use 'bun' as package manager - Usage: `--bun` **Dev** - Use the `dev` release tag. - Usage: `--dev, -d` **Staging** - Use the `staging` release tag. - Usage: `--staging, -s` **Rc** - Use the `rc` release tag. - Usage: `--rc` **Next** - Use the `next` release tag. - Usage: `--next` **Uniform Runners** - Deprecated: no longer needed, `ph init` always scaffolds the hosts/ folder. Accepted with a warning; will be removed in a future release. - Usage: `--uniform-runners` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Use Local Use your local `powerhouse` monorepo dependencies the current project. --- ## Parameters ### Arguments **Monorepo Path** - Path to your local powerhouse monorepo relative to this project - Usage: `[monorepo path]` ### Options **Path** - Path to your local powerhouse monorepo relative to this project - Usage: `--path, -p ` ### Flags **Skip Install** - Skip running `install` with `pnpm` - Usage: `--skip-install, -s` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ### ph-cli Commands - [Run](#run) - [Host Check](#host-check) - [Host Export](#host-export) - [Generate](#generate) - [All](#all) - [Document Model](#document-model) - [Editor](#editor) - [App](#app) - [Processor](#processor) - [Subgraph](#subgraph) - [Piece](#piece) - [Piece Action](#piece-action) - [Piece Trigger](#piece-trigger) - [Migration File](#migration-file) - [Connect Routes](#connect-routes) - [Hosts](#hosts) - [Vetra](#vetra) - [Build](#build) - [Connect](#connect) - [Connect Studio](#connect-studio) - [Connect Build](#connect-build) - [Connect Preview](#connect-preview) - [Access Token](#access-token) - [Inspect](#inspect) - [Keyframes](#keyframes) - [List](#list) - [Mcp](#mcp) - [Migrate](#migrate) - [Model Check](#model-check) - [Model Inspect](#model-inspect) - [Switchboard](#switchboard) - [Login](#login) - [Install](#install) - [Uninstall](#uninstall) ## Run Run a host from the project's hosts/ folder: `ph run [verb]`. The host's own host.js names the runner module; the verb defaults to `run`. Arguments after the verb go to the runner, which names the ones it accepts. --- ## Parameters ### Arguments **Host *[required]*** - Folder name under hosts/ (e.g. vetra, connect). - Usage: `` **Verb** - Runner verb (default: run; e.g. dev, build, serve). - Usage: `[verb]` **Runner Arguments *[required]*** - Arguments for the host's runner (e.g. --port 4001); they win over the host file. - Usage: `[...runner arguments]` ### Flags **Debug** - Print resolved host/runner/verb before dispatch. - Usage: `--debug` **Stop** - Stop the project's running instance of this host instead of starting one: it shuts down as on Ctrl-C, and the command returns when it has. - Usage: `--stop` **Help** - show help - Usage: `--help, -h` ### Options **Explain** - Explain a resolved aspect instead of running: `--explain drives` prints the merged drive plan with provenance; `--explain outputs` prints what each host produces, the output tokens it reads and the start order they imply. - Usage: `--explain ` ## Host Check Check a host the way `ph run` would, without starting it: every load, validation, resolution and boot issue, grouped by host file. Exits 1 on any issue. `--env ` checks against that file alone. --- ## Parameters ### Arguments **Host *[required]*** - the host to check (a directory under hosts/) - Usage: `` ### Options **Env** - check against this env file alone instead of the project .env and the process env - Usage: `--env ` **Verb** - the verb to check at (default: run) - Usage: `--verb ` ### Flags **Help** - show help - Usage: `--help, -h` ## Host Export Print a host as declared, with every member a compound names, as JSON (tokens unresolved). `--out` writes it to a file instead. --- ## Parameters ### Arguments **Host *[required]*** - the host to export (a directory under hosts/) - Usage: `` ### Options **Out** - write the JSON to this file instead of stdout - Usage: `--out ` ### Flags **Help** - show help - Usage: `--help, -h` ## Generate The generate command creates code for Powerhouse modules. It helps you create new code from scratch, or to re-generate existing code in your project. ## All Re-generate all modules in the current project ### Flags **Extract** - Instead of generating code, write a spec for every module into specs/ (one-shot migration to documents-as-source-of-truth) - Usage: `--extract, -x` **Help** - show help - Usage: `--help, -h` ## Document Model Generate a document model ### Options **Document** - Path to a document model spec (.phd or .json) to generate from - Usage: `--document, -d ` **Dir** - Name of the directory of an existing document model to re-generate (my-document-model or document-models/my-document-model) - Usage: `--dir ` **Name** - Experimental. Create a code-first document model with this name, with its editor - Usage: `--name, -n ` ### Flags **All** - Re-generate all existing document models in the current project - Usage: `--all, -a` **Extract** - Write a powerhouse/document-model spec for each existing document model into specs/document-models/ - Usage: `--extract, -x` **Code First** - Experimental. With --name, create the model code-first. Existing models of both styles are discovered without it; ph migrate --document-model converts a schema-first one - Usage: `--code-first` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Editor Generate a document editor ### Options **Name** - The name of the document editor to generate - Usage: `--name, -n ` **Document Type** - The document type for the new editor - Usage: `--document-type, -t ` **Document** - Path to a powerhouse/document-editor spec file (.phd or .json) to drive codegen - Usage: `--document, -d ` **Dir** - Name of the directory of an existing editor to re-generate (my-editor or editors/my-editor) - Usage: `--dir ` ### Flags **All** - Re-generate all existing editors in the current project - Usage: `--all, -a` **Extract** - Write a powerhouse/document-editor spec for each existing editor into specs/editors/ - Usage: `--extract, -x` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## App Generate a drive app ### Options **Name** - The name of the drive app to generate - Usage: `--name, -n ` **Document Types** - The document types allowed by the new app (repeatable or comma-separated) - Usage: `--document-types , -t=` **Document** - Path to a powerhouse/app spec file (.phd or .json) to drive codegen - Usage: `--document, -d ` **Dir** - Name of the directory of an existing app to re-generate (my-app or editors/my-app) - Usage: `--dir ` ### Flags **Disable Drag And Drop** - Do not allow drag and drop in this drive app. - Usage: `--disable-drag-and-drop` **Default:** `false` **All** - Re-generate all existing apps in the current project - Usage: `--all, -a` **Extract** - Write a powerhouse/app spec for each existing drive app into specs/apps/ - Usage: `--extract, -x` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Processor Generate a processor ### Options **Name** - The name of the processor to generate - Usage: `--name, -n ` **Type** - The type of processor to generate - Usage: `--type ` **Default:** `analytics` **Document Types** - The document types the processor will run on (repeatable or comma-separated) - Usage: `--document-types , -t=` **Default:** `` **Apps** - Whether the processor will run in switchboard (nodejs), connect (browser), or both - Usage: `--apps ` **Default:** `switchboard,connect` **Document** - Path to a powerhouse/processor spec file (.phd or .json) to drive codegen - Usage: `--document, -d ` **Dir** - Name of the directory of an existing processor to re-generate (my-processor or processors/my-processor) - Usage: `--dir ` ### Flags **All** - Re-generate all existing processors in the current project - Usage: `--all, -a` **Extract** - Write a powerhouse/processor spec for each existing processor into specs/processors/ - Usage: `--extract, -x` **Skip Install** - Don't install the dependencies the command adds to package.json - Usage: `--skip-install` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Subgraph Generate a subgraph ### Options **Name** - The name of the subgraph to generate - Usage: `--name, -n ` **Document** - Path to a powerhouse/subgraph spec file (.phd or .json) to drive codegen - Usage: `--document, -d ` **Dir** - Name of the directory of an existing subgraph to re-generate (my-subgraph or subgraphs/my-subgraph) - Usage: `--dir ` ### Flags **All** - Re-generate all existing subgraphs in the current project - Usage: `--all, -a` **Extract** - Write a powerhouse/subgraph spec for each existing subgraph into specs/subgraphs/ - Usage: `--extract, -x` **Code First** - Experimental. With --name or --document, create a new subgraph code-first. Existing subgraphs keep their own style; ph migrate --subgraph converts one - Usage: `--code-first` **Skip Install** - Don't install the dependencies the command adds to package.json - Usage: `--skip-install` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Piece Generate a piece: a connector whose actions and triggers a workflow can call --- ## Parameters ### Arguments **Name** - The name of the piece to generate. Its directory is the kebab-case of this. - Usage: `[name]` ### Options **Name** - The name of the piece to generate - Usage: `--name, -n ` **Id** - The piece id, the pieceName a workflow step holds, e.g. @acme/piece-crm. Defaults to one derived from the package name. - Usage: `--id ` **Auth** - The kind of connection the piece asks for - Usage: `--auth ` **Default:** `custom` **Description** - One line describing what the piece connects to - Usage: `--description ` **Dir** - Name of the directory of an existing piece to re-register (my-piece or pieces/my-piece) - Usage: `--dir ` ### Flags **All** - Re-register every piece in pieces/: refresh the pieces list and the manifest, and prune what is gone - Usage: `--all, -a` **Skip Install** - Don't install the dependencies the command adds to package.json - Usage: `--skip-install` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Piece Action Generate an action inside an existing piece --- ## Parameters ### Arguments **Name** - The name of the action, e.g. get-record - Usage: `[name]` ### Options **Name** - The name of the action to generate - Usage: `--name, -n ` **Piece** - The piece directory under pieces/ to add the action to. Optional when the project ships exactly one piece. - Usage: `--piece, -p ` **Require Reactor** - Declare reactor access for the action: read, or write (which includes read). The action then gets context.reactor - Usage: `--require-reactor ` **Default Timeout Seconds** - Write defaultTimeoutSeconds: the step timeout used when the workflow sets none - Usage: `--default-timeout-seconds ` **Default Max Attempts** - Write defaultRetry: \{ maxAttempts \}: the attempts (1 is no retry) used when the workflow sets no retry - Usage: `--default-max-attempts ` ### Flags **Skip Install** - Don't install the dependencies the command adds to package.json - Usage: `--skip-install` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Piece Trigger Generate a trigger inside an existing piece --- ## Parameters ### Arguments **Name** - The name of the trigger, e.g. new-record - Usage: `[name]` ### Options **Name** - The name of the trigger to generate - Usage: `--name, -n ` **Piece** - The piece directory under pieces/ to add the trigger to. Optional when the project ships exactly one piece. - Usage: `--piece, -p ` **Strategy** - How the trigger fires: polled on a schedule, or delivered to a webhook - Usage: `--strategy ` **Default:** `polling` **Require Reactor** - Declare reactor access for the trigger: read, or write (which includes read). Its hooks then get context.reactor - Usage: `--require-reactor ` ### Flags **Skip Install** - Don't install the dependencies the command adds to package.json - Usage: `--skip-install` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Migration File Generate a migration file ### Options **Path *[required]*** - Path to the migration file - Usage: `--path, -p ` **Schema File** - Path to the output file. Defaults to './schema.ts' - Usage: `--schema-file ` ### Flags **Skip Install** - Don't install the dependencies the command adds to package.json - Usage: `--skip-install` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Connect Routes Generate .ph/connect-routes.gen.ts, the route tree hosts/connect/routes/ type-checks against, and fail if a route the package offers imports @powerhousedao/connect ### Flags **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Hosts Generate hosts/<name>/outputs.js + outputs.d.ts (typed $output token trees) for every host whose runner declares outputs, and refresh hosts/types.d.ts. Rerun after changing a producer's runner or declared drives: a stale outputs.js fails the load. ### Flags **Help** - show help - Usage: `--help, -h` ## Vetra The vetra command sets up a Vetra development environment for working with Vetra projects. It starts a Vetra Switchboard and optionally Connect Studio, enabling document collaboration and real-time processing with a "Vetra" drive or connection to remote drives. **What it does:** - 1. Starts a Vetra Switchboard with a "Vetra" drive for document storage - 2. Optionally connects to remote drives instead of creating a local drive - 3. Starts Connect Studio pointing to the Switchboard for user interaction (unless disabled) - 4. Enables real-time updates, collaboration, and code generation Default drives: Connect's default drives resolve to the Vetra drive (and the preview drive when using --watch), plus any drives configured under connect.drives.defaultDrives in powerhouse.config.json - de-duplicated by URL, so configuring project drives does not cost you the Vetra drive. An explicit --default-drives-url is appended to that list (de-duplicated); it never replaces the Vetra drive. Every drive on the local Switchboard: Studio has no reactor of its own: it reads and writes on the local Switchboard (connect.instance.reactorClient "remote"), so it lists every drive there, such as one an agent adds through MCP, and a drive created in Studio lands there too. Not with --remote-drive, or when the project sets connect.instance.multiReactor or reactorClient itself. Preview drive app: Set vetra.previewDrive.app in powerhouse.config.json to the id of a drive app, and ph vetra --watch opens the Vetra Preview drive in that app: it sets the drive's app when it creates the drive and each time it starts. Without the field the drive keeps the app it has. One Vetra per project: A running Vetra is recorded in .ph/vetra-runtime.json (its pid and the Connect, Switchboard and MCP URLs); a second `ph vetra` prints those and exits. --restart stops the recorded Vetra first, then starts this one; the `ph mcp` launcher reconnects to it on its next tool call. Studio restarts by itself when powerhouse.config.json changes. ### Options **Switchboard Port** - port to use for the Vetra Switchboard - Usage: `--switchboard-port ` **Connect Port** - port to use for the Vetra Connect - Usage: `--connect-port ` **Remote Drive** - URL of remote drive to connect to (skips switchboard initialization) - Usage: `--remote-drive ` **Drives Public Base** - public base URL for the drive URLs advertised to Connect; each drive is exposed as <base>/d/<slug> instead of `http://localhost:/d/`. Use when the switchboard is reachable through a reverse proxy. - Usage: `--drives-public-base ` **Db Path** - Database path or connection string. Use a `postgres://` URL for Postgres; otherwise treated as a PGlite filesystem path. Leave unset for in-memory PGlite. - Usage: `--db-path ` **Renown Namespace** - Renown localStorage namespace; share it across Connects to share login. - Usage: `--renown-namespace ` **Base** - Base path for the app - Usage: `--base ` **Log Level** - Log level for the application - Usage: `--log-level ` **Environment:** `PH_CONNECT_LOG_LEVEL` **Default:** `info` **Packages** - Comma-separated list of package names to load - Usage: `--packages ` **Environment:** `PH_PACKAGES` **Local Package** - Path to local package to load during development - Usage: `--local-package ` **Environment:** `PH_LOCAL_PACKAGE` **Default Drives Url** - Comma-separated drive URLs to use as Connect's default drives. - Usage: `--default-drives-url ` **Drive Preserve Strategy** - The preservation strategy to use on default drives - Usage: `--drive-preserve-strategy ` **Default:** `preserve-by-url-and-detach` **Host** - Expose the server to the network. Pass an IP (e.g. 0.0.0.0) to bind to a specific address. - Usage: `--host ` **Watch Timeout** - Amount of time to wait before a file is considered changed - Usage: `--watch-timeout ` **Default:** `300` **Https Key File** - path to the ssl key file - Usage: `--https-key-file ` **Https Cert File** - path to the ssl cert file - Usage: `--https-cert-file ` **Remote Drives** - Specify remote drive URLs to use - Usage: `--remote-drives ` ### Flags **Watch** - Enable dynamic loading for document-models and editors in connect-studio and switchboard - Usage: `--watch, -w` **Default:** `false` **Logs** - Show additional logs - Usage: `--logs` **Default:** `false` **Restart** - Stop this project's running Vetra first (the pid in .ph/vetra-runtime.json), then start - Usage: `--restart` **Default:** `false` **Disable Connect** - Skip Connect initialization (only start switchboard and reactor) - Usage: `--disable-connect` **Default:** `false` **Interactive** - Enable interactive mode for code generation (requires user confirmation before generating code) - Usage: `--interactive` **Default:** `false` **Ignore Local** - Do not load local packages from this project - Usage: `--ignore-local` **Force** - Force dep pre-optimization regardless of whether deps have changed. - Usage: `--force` **Debug** - Log arguments passed to this command - Usage: `--debug` **Open** - Open browser on startup - Usage: `--open` **Cors** - Enable CORS - Usage: `--cors` **Strict Port** - Exit if specified port is already in use - Usage: `--strictPort` **Print Urls** - Print server urls - Usage: `--print-urls` **Default:** `true` **Bind Cli Shortcuts** - Bind CLI shortcuts - Usage: `--bind-cli-shortcuts` **Default:** `true` **Https** - Use https - Usage: `--https` **Dev** - enable development mode to load local packages - Usage: `--dev` **Help** - show help - Usage: `--help, -h` ## Build Build a Powerhouse package for publishing: a browser bundle and a node bundle of its document models, scalars, editors, subgraphs and processors, type declarations, and its stylesheet. Pieces under pieces/ are built too, each into its own self-contained module under dist/node/pieces/<name>, with a descriptor.json and package.json written beside it and the piece listed in dist/powerhouse.manifest.json. A piece takes the package's version: an entry in pieces/index.ts that declares version fails the build. A package that ships only pieces is an ordinary package: it carries the same boilerplate, and every step above runs for it too. tsc runs first. If it reports type errors the build asks whether to go ahead, and stops where it can't ask. --ignore-type-errors builds without asking; a package built that way can load and still fail at runtime, so don't publish it. ### Options **Out Dir** - Where to output the bundled code - Usage: `--out-dir ` **Default:** `dist` ### Flags **No Shared Deps** - Bundle the shared dependency set instead of externalizing it (default: externalize) - Usage: `--no-shared-deps` **Default:** `false` **Ignore Type Errors** - Unsafe: build even when tsc reports type errors, without asking. The package can load and still fail at runtime - Usage: `--ignore-type-errors` **Default:** `false` **Release** - Experimental: check every document model as for a release and stop the build, previous output intact, when the check finds them invalid or cannot run; then write the release approval ph publish verifies - Usage: `--release` **Default:** `false` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Connect Powerhouse Connect commands. Use with `studio`, `build`, `preview`, or `config`. Defaults to `studio` if not specified. ## Connect Studio The studio command starts the Connect Studio, a development environment for building and testing Powerhouse applications. It provides a visual interface for working with your project. **What it does:** - 1. Starts a local Connect Studio server - 2. Provides a web interface for development - 3. Allows you to interact with your project components - 4. Supports various configuration options for customization ### Options **Port** - Port to run the dev server on. - Usage: `--port ` **Default:** `3000` **Renown Namespace** - Renown localStorage namespace; share it across Connects to share login. - Usage: `--renown-namespace ` **Base** - Base path for the app - Usage: `--base ` **Log Level** - Log level for the application - Usage: `--log-level ` **Environment:** `PH_CONNECT_LOG_LEVEL` **Default:** `info` **Packages** - Comma-separated list of package names to load - Usage: `--packages ` **Environment:** `PH_PACKAGES` **Local Package** - Path to local package to load during development - Usage: `--local-package ` **Environment:** `PH_LOCAL_PACKAGE` **Default Drives Url** - Comma-separated drive URLs to use as Connect's default drives. - Usage: `--default-drives-url ` **Drive Preserve Strategy** - The preservation strategy to use on default drives - Usage: `--drive-preserve-strategy ` **Default:** `preserve-by-url-and-detach` **Host** - Expose the server to the network. Pass an IP (e.g. 0.0.0.0) to bind to a specific address. - Usage: `--host ` **Watch Timeout** - Amount of time to wait before a file is considered changed - Usage: `--watch-timeout ` **Default:** `300` ### Flags **Ignore Local** - Do not load local packages from this project - Usage: `--ignore-local` **Force** - Force dep pre-optimization regardless of whether deps have changed. - Usage: `--force` **Debug** - Log arguments passed to this command - Usage: `--debug` **Open** - Open browser on startup - Usage: `--open` **Cors** - Enable CORS - Usage: `--cors` **Strict Port** - Exit if specified port is already in use - Usage: `--strictPort` **Print Urls** - Print server urls - Usage: `--print-urls` **Default:** `true` **Bind Cli Shortcuts** - Bind CLI shortcuts - Usage: `--bind-cli-shortcuts` **Default:** `true` **Help** - show help - Usage: `--help, -h` ## Connect Build The Connect build command creates a production build with the project's local and external packages included. Runtime-config overrides (all combinable — last wins on collision): ph connect build Build with the current source config. ph connect build <key> <value> Build with a positional override applied (e.g. ph connect build connect.renown.url `https://renown.staging`). ph connect build --<field> <value> Build with a per-field flag override (e.g. --renown-url `https://renown.staging`). ph connect build --json '\{"app":\{"workflowsEnabled":true\}\}' Build with a bulk override. The payload is the connect.* block without the "connect" wrapper; unknown keys are rejected. Build has no read mode; passing only <key> without <value> errors out (use `ph connect config ` to read). ### Options **Out Dir** - Output directory - Usage: `--outDir ` **Default:** `.ph/connect-build/dist/` **Json** - Inline JSON override for the runtime connect.* block, without the "connect" wrapper, e.g. '\{"app":\{"workflowsEnabled":true\},"renown":\{"url":"..."\}\}'. A top-level packageRegistryUrl is also accepted. Validated against the runtime schema (unknown keys fail); deep-merged on top of source powerhouse.config.json. Individual --flag values beat --json on collision. - Usage: `--json ` **Renown Url** - Override connect.renown.url. - Usage: `--renown-url ` **Renown Network Id** - Override connect.renown.networkId. - Usage: `--renown-network-id ` **Renown Chain Id** - Override connect.renown.chainId. - Usage: `--renown-chain-id ` **Renown Namespace** - Renown localStorage namespace; share it across Connects to share login. - Usage: `--renown-namespace ` **Renown Switchboard Url** - Override connect.renown.switchboardUrl. When set, enables in-page Renown sign-in. - Usage: `--renown-switchboard-url ` **Allow Add Drive** - Override connect.drives.allowAddDrive (top-level add-drive toggle). - Usage: `--allow-add-drive ` **External Packages** - Override connect.packages.externalEnabled. - Usage: `--external-packages ` **Workflows** - Override connect.app.workflowsEnabled (load the @powerhousedao/workflow package: workflow + connection documents, their editors and Workflow Studio). - Usage: `--workflows ` **Remote Drives Enabled** - Override connect.drives.sections.remote.enabled (the unified cloud+public section). - Usage: `--remote-drives-enabled ` **Remote Drives Allow Add** - Override connect.drives.sections.remote.allowAdd. - Usage: `--remote-drives-allow-add ` **Remote Drives Allow Delete** - Override connect.drives.sections.remote.allowDelete. - Usage: `--remote-drives-allow-delete ` **Local Drives Enabled** - Override connect.drives.sections.local.enabled. - Usage: `--local-drives-enabled ` **Local Drives Allow Add** - Override connect.drives.sections.local.allowAdd. - Usage: `--local-drives-allow-add ` **Local Drives Allow Delete** - Override connect.drives.sections.local.allowDelete. - Usage: `--local-drives-allow-delete ` **Packages Registry** - Override the top-level packageRegistryUrl. - Usage: `--packages-registry ` **App Name** - Override connect.branding.appName. - Usage: `--app-name ` **Home Background** - Override connect.home.background (the generic plugin's home screen). URL or path to an image, pinned bottom-right; pass an empty string ("") to reset to the bundled gradient. - Usage: `--home-background ` **Sentry Dsn** - Override connect.sentry.dsn (Sentry DSN URL). Pass an empty string ("") to set null and disable Sentry. - Usage: `--sentry-dsn ` **Sentry Env** - Override connect.sentry.env (Sentry environment label). - Usage: `--sentry-env ` **Sentry Tracing Enabled** - Override connect.sentry.tracing (Sentry performance tracing). - Usage: `--sentry-tracing-enabled ` **Favicon** - Path to a favicon file (e.g. .ico) to bundle in place of the default Connect icon. Emitted as icon.ico, or icon.svg/icon.png for those types; resolved relative to the build cwd. - Usage: `--favicon ` **Base** - Base path for the app - Usage: `--base ` **Log Level** - Log level for the application - Usage: `--log-level ` **Environment:** `PH_CONNECT_LOG_LEVEL` **Default:** `info` **Packages** - Comma-separated list of package names to load - Usage: `--packages ` **Environment:** `PH_PACKAGES` **Local Package** - Path to local package to load during development - Usage: `--local-package ` **Environment:** `PH_LOCAL_PACKAGE` **Default Drives Url** - Comma-separated drive URLs to use as Connect's default drives. - Usage: `--default-drives-url ` **Drive Preserve Strategy** - The preservation strategy to use on default drives - Usage: `--drive-preserve-strategy ` **Default:** `preserve-by-url-and-detach` --- ## Parameters ### Arguments **Key** - Dotted path inside the runtime config (e.g. connect.renown.url). Pair with <value> to set; pass alone to `ph connect config` to read. - Usage: `[key]` **Value** - Value to set at <key>. Coerced against the runtime schema (string, bool, number, enum). Arrays and objects require --json instead. - Usage: `[value]` ### Flags **Dynamic Base** - Build one bundle that serves under any subpath; base resolved at serve time from a runtime global. Overrides --base. - Usage: `--dynamic-base` **Ignore Local** - Do not load local packages from this project - Usage: `--ignore-local` **Force** - Force dep pre-optimization regardless of whether deps have changed. - Usage: `--force` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Connect Preview The Connect preview command previews a built Connect project. NOTE: You must run `ph connect build` first ### Options **Port** - Port to run the preview server on. - Usage: `--port ` **Default:** `4173` **Out Dir** - Output directory - Usage: `--outDir ` **Default:** `.ph/connect-build/dist/` **Base** - Base path for the app - Usage: `--base ` **Log Level** - Log level for the application - Usage: `--log-level ` **Environment:** `PH_CONNECT_LOG_LEVEL` **Default:** `info` **Packages** - Comma-separated list of package names to load - Usage: `--packages ` **Environment:** `PH_PACKAGES` **Local Package** - Path to local package to load during development - Usage: `--local-package ` **Environment:** `PH_LOCAL_PACKAGE` **Default Drives Url** - Comma-separated drive URLs to use as Connect's default drives. - Usage: `--default-drives-url ` **Drive Preserve Strategy** - The preservation strategy to use on default drives - Usage: `--drive-preserve-strategy ` **Default:** `preserve-by-url-and-detach` **Host** - Expose the server to the network. Pass an IP (e.g. 0.0.0.0) to bind to a specific address. - Usage: `--host ` **Watch Timeout** - Amount of time to wait before a file is considered changed - Usage: `--watch-timeout ` **Default:** `300` ### Flags **Ignore Local** - Do not load local packages from this project - Usage: `--ignore-local` **Force** - Force dep pre-optimization regardless of whether deps have changed. - Usage: `--force` **Debug** - Log arguments passed to this command - Usage: `--debug` **Open** - Open browser on startup - Usage: `--open` **Cors** - Enable CORS - Usage: `--cors` **Strict Port** - Exit if specified port is already in use - Usage: `--strictPort` **Print Urls** - Print server urls - Usage: `--print-urls` **Default:** `true` **Bind Cli Shortcuts** - Bind CLI shortcuts - Usage: `--bind-cli-shortcuts` **Default:** `true` **Help** - show help - Usage: `--help, -h` ## Access Token The access-token command generates a bearer token for API authentication. This token can be used to authenticate requests to Powerhouse APIs like reactor-api (Switchboard). **What it does:** - 1. Uses your CLI's cryptographic identity (DID) to sign a verifiable credential - 2. Creates a JWT bearer token with configurable expiration - 3. Outputs the token to stdout (info to stderr) for easy piping Prerequisites: You must have a cryptographic identity. Run 'ph login' first to: - Generate a keypair (stored in .ph/.keypair.json) - Optionally link your Ethereum address (stored in .ph/.renown.json) Token Details: The generated token is a JWT (JSON Web Token) containing: - Issuer (iss): Your CLI's DID (did:key:...) - Subject (sub): Your CLI's DID - Credential Subject: Chain ID, network ID, and address (if authenticated) - Expiration (exp): Based on --expiry option - Audience (aud): If --audience is specified Output: - Token information (DID, address, expiry) is printed to stderr - The token itself is printed to stdout for easy piping/copying This allows you to use the command in scripts: TOKEN=$(ph access-token) curl -H "Authorization: Bearer $TOKEN" `http://localhost:4001/graphql` Usage with APIs: Generate token and use with curl TOKEN=$(ph access-token --expiry 1d) curl -X POST `http://localhost:4001/graphql` \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $TOKEN" \ -d '\{"query": "\{ drives \{ id name \} \}"\}' Export as environment variable export PH_ACCESS_TOKEN=$(ph access-token) Notes: - Tokens are self-signed using your CLI's private key - No network request is made; tokens are generated locally - The recipient API must trust your CLI's DID to accept the token - For reactor-api, ensure AUTH_ENABLED=true to require authentication ### Options **Expiry** - Token expiry duration. Supports: "7d" (days), "24h" (hours), "3600" or "3600s" (seconds) - Usage: `--expiry ` **Default:** `7d` **Audience** - Target audience URL for the token - Usage: `--audience ` ### Flags **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Inspect The inspect command examines and provides detailed information about a Powerhouse package. It helps you understand the structure, dependencies, and configuration of packages in your project. **What it does:** - 1. Analyzes the specified package - 2. Retrieves detailed information about its structure and configuration - 3. Displays package metadata, dependencies, and other relevant information - 4. Helps troubleshoot package-related issues --- ## Parameters ### Arguments **Package Name *[required]*** - The name of the package to inspect - Usage: `` ### Flags **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Keyframes The keyframes command reports on and cleans up the keyframes of a running switchboard, through its inspection-admin endpoint. Prerequisites: - The host serves inspection-admin (INSPECTION_ADMIN_ENABLED=true). - The caller is one of the host's auth admins. - The keep rules are in powerhouse.config.json under reactor.keyframes, or in PH_KEYFRAMES. Run preview first: it only reads. clean then removes the keyframes past each type's keep count and all keyframes of deleted documents, in small paced statements beside live traffic. Running it again is safe. A later read that misses a keyframe replays from the operation log, so a clean-up costs time on cold reads and never changes a document. The disk only shrinks after reclaim, which rewrites the Keyframe table once the store is quiet. On PGlite the store answers nothing while it rewrites. On Postgres it says what to run by hand. --- ## Parameters ### Arguments **Action *[required]*** - preview: report what a clean-up would remove, without writing. clean: apply the configured keep and remove the keyframes of deleted documents (needs --yes). status: show the latest run and reclaim. cancel: stop a running clean-up. reclaim: rewrite the Keyframe table to give the space back to the disk. - Usage: `` ### Options **Url** - The switchboard's GraphQL endpoint, e.g. `http://localhost:4001/graphql`. Defaults to reactor.port in powerhouse.config.json. - Usage: `--url ` **Token** - Admin bearer token. Defaults to PH_ACCESS_TOKEN, then a token from your `ph login` identity. Its address must be in the host's auth admins. - Usage: `--token ` ### Flags **Yes** - Confirms a clean or a reclaim; both refuse to run without it. - Usage: `--yes` **Json** - Print the result as JSON. - Usage: `--json` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## List The list command displays information about installed Powerhouse packages in your project. It reads the powerhouse.config.json file and shows the packages that are currently installed. **What it does:** - 1. Examines your project configuration - 2. Lists all installed Powerhouse packages - 3. Provides a clear overview of your project's dependencies - 4. Helps you manage and track your Powerhouse components ### Flags **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Mcp Serve the project's reactor-mcp tools to a coding agent over stdio. Use it as the MCP server command in .mcp.json, .cursor/mcp.json and .gemini/settings.json. Tools are listed at once, without waiting for Vetra. The first tool call connects to the project's running Vetra (found through .ph/vetra-runtime.json), or starts one in the background and waits for it to be ready: `ph run vetra --strictPort` when the project has a hosts/vetra host, else `ph vetra --strictPort --watch`. How the start goes is reported as MCP progress notifications when the client sent a progressToken, else on stderr. Startup failures come back as tool errors carrying Vetra's output, which is also written to .ph/vetra.log. Beside reactor-mcp's tools it serves restartVetra, which stops the project's Vetra, starts it again and returns its URLs. When Vetra restarts some other way (stopped and started again, or `ph vetra --restart` in a project without a hosts/vetra host), the next tool call connects to the new one. ### Options **Project Dir** - Powerhouse project to serve. Defaults to the nearest directory above the working directory that holds powerhouse.config.json. - Usage: `--project-dir ` **Start Timeout** - Seconds to wait for Vetra to answer /ready after starting it on the first tool call - Usage: `--start-timeout ` **Default:** `120` ### Flags **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Migrate Run migrations --- ## Parameters ### Arguments **Version** - The version to migrate to. Accepts a valid semver version or `staging`, `dev`, `next`, `latest`. Defaults to the release channel of the project's installed Powerhouse packages. - Usage: `[version]` ### Options **Version** - The version to migrate to. Accepts a valid semver version or `staging`, `dev`, `next`, `latest`. Defaults to the release channel of the project's installed Powerhouse packages. - Usage: `--version, -v ` **Subgraph** - Convert only this directory of subgraphs/ into a code-first subgraph. Repeat it for more directories. Implies --subgraphs. - Usage: `--subgraph ` **Document Model** - Convert only this directory of the document models directory into a code-first model. Repeat it for more directories. Implies --document-models. - Usage: `--document-model ` ### Flags **Force** - Run migrate from the bundled codegen even if the target version cannot be resolved from the npm registry or differs from the installed ph-cli version. - Usage: `--force, -f` **Subgraphs** - Convert every schema-first subgraph under subgraphs/ into a code-first one, keeping its schema and resolvers. Each resolver becomes a member of the subgraph class that getSchema calls directly, and each separate resolver factory module stays loaded in its original order to preserve initialization and shared bindings. Other files that import the schema-first SDL get it from schema-sdl.ts. Code-first subgraphs are skipped, and a subgraph that cannot keep the same schema is left as it is, with the reason. The project's tsc then checks the converted resolvers against the schema they serve: each call it rejects, because the resolver's own types disagree with the schema, is cast with a ph migrate comment so it runs as before and the project compiles, and is listed to fix. Upgrades no dependencies and regenerates nothing else. - Usage: `--subgraphs` **Strict Resolvers** - With --subgraphs or --subgraph: leave the resolver calls tsc rejects without a cast, so the project does not compile until each resolver's types agree with the schema. They are listed either way. - Usage: `--strict-resolvers` **Document Models** - Convert every schema-first document model into a code-first one, keeping each version's definition and reducers. The model gets the layout ph generate document-model --code-first writes: index.ts, upgrades/index.ts, and per version definition.ts, index.ts and modules/; the custom code of src/ moves into the version's directory. The generated code, src/ and <name>.json are removed. The names the generated code exported stay available from compat/v<N>/index.ts, imports of removed files are rewritten to it, and the document-models barrels are regenerated. A model that cannot keep the same definition is left as it is, with the reason. Upgrades no dependencies. - Usage: `--document-models` **Rename Conflicting Types** - With --document-models or --document-model: rename a model type that has the name of a scalar, such as Address, by prefixing the model's name (InvoiceAddress), instead of refusing the model. The GraphQL type name changes; the TypeScript name the model's compat exports keeps the old name. - Usage: `--rename-conflicting-types` **Dry Run** - Print the diff of every file the migration would add, change or delete, and write nothing. It installs nothing and regenerates no code; it lists those steps instead. - Usage: `--dry-run` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Model Check Check this package's document models through one set of checks: every model directory in documentModelsDir, whether it stores a schema-first <name>.json or exports a code-first model from its index.ts. Writes nothing. `tsc` cannot run these checks: a code-first model compiles when its module is evaluated. --release (experimental) runs the checks `ph build --release` runs: these, the release checks, and host validation, which validates every model and the package's scalars entry as a host that installed the package would. --prepack (experimental) loads nothing. It reads the release approval `ph build --release` wrote in .ph/build/release-approval.json and exits 0 when it matches this package, 1 when it does not (missing, unreadable, stale or refused), naming the reasons, and 2 when it cannot be checked. To stop a plain `npm publish` too, add it to package.json, then run `ph build --release` (package.json is published, so the approval covers it): "scripts": \{ "prepack": "ph-cli model check --prepack" \} With --json it prints \{"decision": ..., "reasons": [...]\}. Exit codes: 0 ok, or skipped. Skipped means the package has no document model, so nothing was checked. Read "status". 1 invalid. The models were read and at least one is wrong. 2 failed. The configuration or a module could not be read, the arguments are wrong, or the command could not run. Output: ph model check a readable report on stdout ph model check --json exactly one JSON report on stdout and nothing else Logs, including what the package's modules print while they load, go to stderr. When the command cannot run at all, it prints only a message on stderr and exits 2. Experimental: the command, its output and the JSON report can change in any release. ### Flags **Release** - Experimental: check as ph build --release does, with the release checks and host validation over every model. Writes nothing - Usage: `--release` **Default:** `false` **Prepack** - Experimental: without loading the package, exit 0 when the release approval ph build --release wrote matches it, 1 when it does not, and 2 when it cannot be checked. For a "prepack" script, so npm publish stops too - Usage: `--prepack` **Default:** `false` **Json** - Print exactly one JSON report on stdout and nothing else - Usage: `--json` **Default:** `false` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Model Inspect Describe this package's document models without checking them: for each model its digest and compatibility, and for each of its versions the source, scopes, scalars and operations. With <documentType>@<version>, describe that version only and add its full definition to the JSON report. Writes nothing. Exit codes: 0 ok, or skipped. Skipped means the package has no document model, so nothing was checked. Read "status". 1 invalid. The models were read and at least one is wrong. 2 failed. The configuration or a module could not be read, the arguments are wrong, or the command could not run. Output: ph model inspect a readable report on stdout ph model inspect --json exactly one JSON report on stdout and nothing else Logs, including what the package's modules print while they load, go to stderr. When the command cannot run at all, it prints only a message on stderr and exits 2. Experimental: the command, its output and the JSON report can change in any release. --- ## Parameters ### Arguments **Document Type Version** - Inspect one model version, such as acme/invoice@1 (default: every model) - Usage: `[documentType@version]` ### Flags **Json** - Print exactly one JSON report on stdout and nothing else - Usage: `--json` **Default:** `false` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Switchboard The switchboard command starts a local Switchboard instance, which acts as the document processing engine for Powerhouse projects. It provides the infrastructure for document models, processors, and real-time updates. **What it does:** - 1. Starts a local switchboard server - 2. Loads document models and processors - 3. Provides an API for document operations - 4. Enables real-time document processing - 5. Can authenticate with remote services using your identity from 'ph login' ### Flags **Https** - Use https - Usage: `--https` **Dev** - enable development mode to load local packages - Usage: `--dev` **Ignore Local** - Do not load local packages from this project - Usage: `--ignore-local` **Debug** - Log arguments passed to this command - Usage: `--debug` **Use Identity** - enable identity using keypair from ph login (uses ~/.ph/keypair.json) - Usage: `--use-identity` **Require Identity** - require existing keypair, fail if not found (implies --use-identity) - Usage: `--require-identity` **Migrate** - Run database migrations and exit - Usage: `--migrate` **Migrate Status** - Show migration status and exit - Usage: `--migrate-status` **Reset** - Wipe the local PGlite switchboard storage after confirmation, then exit - Usage: `--reset` **Yes** - Skip the interactive confirmation prompt for --reset (required for non-interactive use) - Usage: `--yes, -y` **Mcp** - enable Mcp route at /mcp - Usage: `--mcp` **Default:** `true` **Use Vetra Drive** - Use a Vetra drive - Usage: `--use-vetra-drive` **Default:** `false` **Help** - show help - Usage: `--help, -h` ### Options **Https Key File** - path to the ssl key file - Usage: `--https-key-file ` **Https Cert File** - path to the ssl cert file - Usage: `--https-cert-file ` **Remote Drives** - Specify remote drive URLs to use - Usage: `--remote-drives ` **Packages** - Comma-separated list of package names to load - Usage: `--packages ` **Environment:** `PH_PACKAGES` **Port** - Port to host the api - Usage: `--port ` **Default:** `4001` **Base Path** - base path for the API endpoints (overrides BASE_PATH) - Usage: `--base-path ` **Keypair Path** - path to custom keypair file for identity - Usage: `--keypair-path ` **Vetra Drive Id** - Specify a Vetra drive ID - Usage: `--vetra-drive-id ` **Default:** `vetra` **Db Path** - path to the database - Usage: `--db-path ` ## Login The login command authenticates you with Renown using your Ethereum wallet. This enables the CLI to act on behalf of your Ethereum identity for authenticated operations. **What it does:** - 1. Generates or loads a cryptographic identity (DID) for the CLI - 2. Opens your browser to the Renown authentication page - 3. You authorize the CLI's DID to act on behalf of your Ethereum address - 4. Stores the credentials locally in .ph/.renown.json ### Options **Renown Url** - Renown server URL. - Usage: `--renown-url ` **Environment:** `PH_CONNECT_RENOWN_URL` **Default:** `https//www.renown.id` **Timeout** - Authentication timeout in seconds. - Usage: `--timeout ` **Default:** `300` ### Flags **Logout** - Sign out and clear stored credentials - Usage: `--logout` **Status** - Show current authentication status - Usage: `--status` **Show Did** - Show the CLI's DID and exit - Usage: `--show-did` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Install The install command adds Powerhouse dependencies to your project. By default it only registers the package in powerhouse.config.json with provider "registry" — Connect will load it from the registry CDN at runtime. With --local, the package is also installed into node_modules and marked as provider "local" — it will be bundled into ph connect build so the preview works without the registry being reachable. Resolution order for the registry URL: --registry flag > PH_REGISTRY_URL env > powerhouse.config.json > default --- ## Parameters ### Arguments **Dependencies *[required]*** - Names of the dependencies to install - Usage: `[...dependencies]` ### Options **Registry** - Registry URL to install from (overrides config and environment) - Usage: `--registry ` **Allow Build** - A list of package names that are allowed to run postinstall scripts during installation. - Usage: `--allow-build ` **Package Manager** - Specify the package manager to use for your project. Can be one of: `npm`, `pnpm`, `yarn`, or `bun`. Defaults to your environment package manager. - Usage: `--package-manager, -p ` ### Flags **Local** - Also install packages into node_modules (marks them as provider: "local" so they get bundled into ph connect build) - Usage: `--local` **Npm** - Use 'npm' as package manager - Usage: `--npm` **Pnpm** - Use 'pnpm' as package manager - Usage: `--pnpm` **Yarn** - Use 'yarn' as package manager - Usage: `--yarn` **Bun** - Use 'bun' as package manager - Usage: `--bun` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` ## Uninstall The uninstall command removes Powerhouse dependencies from your project. It handles the removal of packages, updates configuration files, and ensures proper cleanup. **What it does:** - 1. Uninstalls specified Powerhouse dependencies using your package manager - 2. Updates powerhouse.config.json to remove the dependencies - 3. Supports various uninstallation options and configurations - 4. Works with npm, yarn, yarn@berry, pnpm, pnpm@6, bun, deno package managers --- ## Parameters ### Arguments **Dependencies *[required]*** - Names of the dependencies to uninstall - Usage: `[...dependencies]` ### Options **Package Manager** - Specify the package manager to use for your project. Can be one of: `npm`, `pnpm`, `yarn`, or `bun`. Defaults to your environment package manager. - Usage: `--package-manager, -p ` ### Flags **Npm** - Use 'npm' as package manager - Usage: `--npm` **Pnpm** - Use 'pnpm' as package manager - Usage: `--pnpm` **Yarn** - Use 'yarn' as package manager - Usage: `--yarn` **Bun** - Use 'bun' as package manager - Usage: `--bun` **Debug** - Log arguments passed to this command - Usage: `--debug` **Help** - show help - Usage: `--help, -h` {/* AUTO-GENERATED-CLI-COMMANDS-END */} --- ## Package manifest: config > Source: https://academy.vetra.io/academy/Reference/CLITooling/PackageManifest `powerhouse.manifest.json` describes a package. Its `config` array lists the environment variables the package needs, so a host (such as the Vetra hosting UI) can ask the operator for them. ```json { "config": [ { "name": "PH_API_URL", "type": "var", "required": true, "default": "https://api.example.com" }, { "name": "PH_API_KEY", "type": "secret", "required": true }, { "name": "AUTH_ENABLED", "type": "boolean", "default": "false", "description": "Require a signed-in caller." } ] } ``` ## Entry fields | Field | Type | Meaning | |---|---|---| | `name` | string | The environment variable. Entries are unique by name; `ph generate` merges by name. | | `type` | `"var"` \| `"secret"` \| `"boolean"` | How the host asks for the value (below). | | `description` | string, optional | Shown to the operator. | | `required` | boolean, optional | The package does not work without a value. | | `default` | string, optional | The value the host falls back to, spelled as the env var would be. | ## Types | Type | Host renders | Value written | `default` | |---|---|---|---| | `var` | free-text field | the text | any string | | `secret` | masked field, never displayed back | the text | ignored | | `boolean` | toggle | `"true"` or `"false"` | `"true"` or `"false"` only | A package reads a boolean the way the Switchboard does: `process.env.AUTH_ENABLED === "true"`. ## Validation `ph generate` parses the manifest and stops on an invalid entry. The error is a JSON array with one item per problem. Each item gives its `path` as an array and its `message`, for example: ```json [ { "code": "invalid_value", "path": ["config", 0, "type"], "message": "type must be one of \"var\", \"secret\", \"boolean\"" } ] ``` A boolean with a bad default gives `"path": ["config", 2, "default"]` and the message `default must be "true" or "false" for a boolean entry`. --- ## Vetra Remote Drive > Source: https://academy.vetra.io/academy/Reference/CLITooling/VetraRemoteDrive These commands enable collaborative development using Vetra remote drives. Instead of working with local drives only, you can connect your Powerhouse project to a remote drive that syncs across team members. ## Commands Overview ### `ph init --remote-drive` **Purpose:** Create a new Powerhouse project and connect it to a remote Vetra drive. **When to use:** You're starting a NEW project that will be shared via a remote drive. **How it works:** 1. Validates the remote drive URL and checks that no GitHub URL is configured yet 2. Creates a standard Powerhouse project from the boilerplate 3. Adds Vetra configuration to `powerhouse.config.json`: ```json { "vetra": { "driveId": "abc123", "driveUrl": "https://vetra.example.com/d/abc123" } } ``` **Usage:** ```bash ph init my-project --remote-drive https://vetra.example.com/d/abc123 ``` **After initialization:** - Create a GitHub repository - Commit and push your code - Run `ph vetra` to configure the GitHub URL in the remote drive --- ### `ph checkout --remote-drive` **Purpose:** Clone an existing Powerhouse project that's already connected to a remote drive. **When to use:** You're joining an EXISTING project that someone else initialized. **How it works:** 1. Queries the remote drive to find the configured GitHub URL 2. Clones the repository from GitHub 3. Installs dependencies 4. The project is already configured to use the remote drive **Usage:** ```bash ph checkout --remote-drive https://vetra.example.com/d/abc123 ``` **Requirements:** - The remote drive must have a GitHub URL configured (done during `ph vetra` after init) --- ### `ph vetra` **Purpose:** Start the Vetra development environment with Switchboard and Connect Studio. **When to use:** After initializing or checking out a project, to start development. **How it works:** 1. Reads the remote drive URL from `powerhouse.config.json` (or `--remote-drive` flag) 2. Starts Switchboard connected to the remote drive (syncs automatically) 3. Prompts to configure GitHub URL if not set (first time after init) 4. Starts Connect Studio pointing to the drive(s) **With `--watch` flag:** - Creates a second "Vetra Preview" drive for testing local changes - Dynamically loads your local document models and editors - Main drive stays stable, preview drive for experimentation **Usage:** ```bash # Basic usage (uses config from powerhouse.config.json) ph vetra # With watch mode for development ph vetra --watch # Override remote drive URL ph vetra --remote-drive https://vetra.example.com/d/abc123 # Disable Connect Studio ph vetra --disable-connect ``` **Key options:** - `--watch` - Enable dynamic loading and create preview drive - `--remote-drive ` - Specify remote drive URL - `--switchboard-port ` - Custom Switchboard port (default: 4001) - `--connect-port ` - Custom Connect Studio port (default: 3000) - `--disable-connect` - Skip Connect Studio - `--interactive` - Enable interactive mode for code generation --- ## Workflows ### Starting a New Project (Owner) ```bash # 1. Initialize with remote drive ph init my-project --remote-drive https://vetra.example.com/d/abc123 # 2. Create GitHub repo and push cd my-project git add . git commit -m "Initial commit" git remote add origin https://github.com/user/my-project.git git push -u origin main # 3. Start Vetra and configure GitHub URL ph vetra # (Select option to use detected GitHub URL when prompted) # 4. Start developing with watch mode ph vetra --watch ``` ### Joining an Existing Project (Collaborator) ```bash # 1. Checkout from remote drive ph checkout --remote-drive https://vetra.example.com/d/abc123 # 2. Navigate to project cd project-name # 3. Start Vetra environment with watch mode ph vetra --watch # You're now synced with the remote drive ``` --- ## Key Concepts **Remote Drive vs Local Drive:** - Without remote drive: `ph vetra` creates a local drive on your machine only - With remote drive: `ph vetra` connects to a shared drive that syncs across team members **When to use each command:** - Use `ph init --remote-drive` when starting a NEW project (no GitHub URL configured in drive yet) - Use `ph checkout --remote-drive` when joining an EXISTING project (GitHub URL already configured) - Use `ph vetra --watch` to start development after either init or checkout **Preview Drive (`--watch` mode):** - Main "Vetra" drive: syncs with remote, contains stable package configuration - "Vetra Preview" drive: created locally with `--watch`, for testing local document models - Without `--watch`: safer, prevents untested code from affecting Connect - With `--watch`: enables rapid development and testing --- ## Overview and quickstart > Source: https://academy.vetra.io/academy/Reference/ReactorBrowserClient/OverviewAndQuickstart `GraphQLReactorClient` is a light client for the `@powerhousedao/reactor-browser` hooks. It talks plain GraphQL to a [Switchboard](/academy/Reference/Reactor/WorkingWithTheReactor) and fills the document slots of `window.ph` the full in-browser reactor fills, so `useDocument`, `useDispatch` and `useReactorClient` work in a plain web app with no reactor, no PGLite and no job queue in the bundle. It fills the document slots and no others. The provider publishes neither `window.ph.vetraPackageManager` nor `window.ph.reactorClientModule`, so the package, document-model and editor-module hooks find nothing below it. An app renders the components it imports itself and builds its own actions. See [Limitations](/academy/Reference/ReactorBrowserClient/ApiReference#limitations). ```tsx GraphQLReactorProvider, useSwitchboardClient, } from "@powerhousedao/reactor-browser/graphql-client"; ``` Two implementations satisfy the same interface. Connect and Vetra Studio run the full reactor client; a web app runs this one. The document hooks below the provider are identical either way, so a component that reads and writes documents moves between them unchanged. ## When to use it Use the light client when the app reads and writes documents that live on a Switchboard, and does not need local-first editing. Everything is remote-first: a dispatch is a round trip to the server, and there is no offline queue. Use the full reactor (Connect, Vetra Studio, or your own `ReactorBuilder` setup) when you need local-first state, drives, jobs, or the document-model modules in the browser. ## What you need - A running Switchboard with the document model your app writes. `ph switchboard` in a Powerhouse project serves one on `http://localhost:4001/graphql`. - React 19 or later, the package's peer requirement. The document hooks read through `use()`. - Any bundler. Import the `/graphql-client` subpath and no build configuration is needed - see [Bundling](#bundling) below. ## Mount the provider One provider is the whole integration. It builds the client, publishes it and a `DocumentCache` over it into `window.ph`, and registers the `window.ph` event handlers the hooks read through. ```tsx "use client"; const SWITCHBOARD_URL = "http://localhost:4001/graphql"; export default function DocumentsPage() { return ( ); } ``` The client is built in the browser only. During server rendering the slots stay empty and the hooks report their loading states, which is why the provider and its children are client components. ## Describe the document without a reducer The app builds actions and documents; the Switchboard owns the model and its reducer. Nothing generated has to ship to the browser. ```ts baseCreateDocument, createAction, createBaseState, generateId, type Action, type PHBaseState, type PHDocument, } from "document-model"; export const TODO_DOCUMENT_TYPE = "test/todo"; const TODO_DOCUMENT_VERSION = 2; export type TodoItem = { id: string; title: string; completed: boolean }; type TodoPHState = PHBaseState & { global: { title: string; todos: TodoItem[] }; local: Record; }; function createTodoState(state?: Partial): TodoPHState { return { ...createBaseState(state?.auth, { version: TODO_DOCUMENT_VERSION, ...state?.document, }), global: { title: "", todos: [], ...state?.global }, local: {}, }; } export function createTodoDocument(): PHDocument { return baseCreateDocument( createTodoState, undefined, TODO_DOCUMENT_TYPE, ); } export function addTodo(title: string): Action { return createAction( "ADD_TODO", { id: generateId(), title, completed: false }, undefined, undefined, "global", ); } /** Reads the todo list off a document whose state shape is not known statically. */ export function readTodos(document: PHDocument | undefined): TodoItem[] { const state = document?.state as Partial | undefined; return state?.global?.todos ?? []; } ``` If the action's input does not match the model, the server says so per action and the dispatch error callback below receives it. ## Create, read, dispatch `useReactorClient()` returns the client for imperative calls such as `create`. `useDocument(id)` reads through the cache and suspends while the document loads. `useDispatch(document)` pushes actions. ```tsx "use client"; useDispatch, useDocument, useReactorClient, } from "@powerhousedao/reactor-browser/graphql-client"; export function TodoDemo() { const client = useReactorClient(); const [documentId, setDocumentId] = useState(); async function create() { if (!client) return; const handle = await client.write.create(createTodoDocument()); setDocumentId(handle.documentId); } return (
{documentId ? ( Loading document…

}>
) : null}
); } function TodoList({ documentId }: { documentId: string }) { const document = useDocument(documentId); const [, dispatch] = useDispatch(document); const [title, setTitle] = useState(""); const [error, setError] = useState(); const todos = readTodos(document); function add() { dispatch(addTodo(title), (errors) => setError(errors.map((e) => e.message).join("; ")), ); setTitle(""); } return (
setTitle(e.target.value)} /> {error ?

{error}

: null}
    {todos.map((todo) => (
  • {todo.title}
  • ))}
); } ``` Read documents by id. The client's change events carry resolved document ids, so a cache entry keyed by slug is never invalidated. Keep the open document's id in the URL if you want a reload to prove the state came back from the Switchboard rather than from memory. ## Realtime The first `subscribe` call opens a websocket to the Switchboard and subscribes to the `documentChanges` firehose. The `DocumentCache` the provider builds subscribes in its constructor, so the socket opens as soon as the provider mounts. A change written by another user, another tab or a server-side processor lands on the page with no reload. The websocket endpoint is derived from `url`: `http` becomes `ws`, `https` becomes `wss`, and `/subscriptions` is appended. For `http://localhost:4001/graphql` that is `ws://localhost:4001/graphql/subscriptions`. Override it with the `subscriptionsUrl` prop, or set `realtime={false}` where there is no websocket to talk to. Realtime is an enhancement, never a dependency. A socket that cannot be opened is logged once and ignored, and the app keeps working on the changes it makes itself. The failed socket is closed rather than kept, so the next component to subscribe tries again - but nothing subscribes on a sign-in of its own accord. An app on a Switchboard that refuses anonymous subscribers, and that authenticates after mount, should remount the provider once the user is in. ## Signing With a logged-in [Renown](/academy/Reference/Authorization/renown-sdk/Authentication) user, a single-action dispatch is stamped with the previous operation's hash and index and signed before it is pushed. Nothing else is required: the client resolves the signer from `window.ph.renown` at push time, so a client built before login signs after login. A batch of more than one action is pushed unsigned with a warning. See [API reference](/academy/Reference/ReactorBrowserClient/ApiReference#signing) for why. ## Bundling Import from `@powerhousedao/reactor-browser/graphql-client`, as every example on this page does. It bundles in Next, Vite or anything else with no alias, stub or other build configuration. Do not import the light client from the package root. The root barrel re-exports the whole package, and part of that value-imports `@powerhousedao/reactor`, whose single entry reaches `pg` (`dns`/`fs`/`net`/`tls`), `@electric-sql/pglite` and `node:worker_threads`. None of it runs on this path, but a browser bundler still has to resolve it, and Turbopack fails on `node:worker_threads` before any app code runs. The `/graphql-client` entry avoids that by importing the reactor for types only. Types are erased at compile time, so a bundler never sees them; the few runtime values the client needs are mirrored inside `reactor-browser` instead, with compile-time assertions that fail if they ever drift from the reactor's own definitions. The built entry imports exactly `react`, `document-model`, `graphql`, `graphql-request`, `graphql-tag`, `graphql-ws/client`, `@powerhousedao/shared/document-model`, `@powerhousedao/shared/document-drive`, `change-case`, `remeda` and `slug` - no database driver, no node built-ins. A test walks the entry's import graph and fails if that regresses. The monorepo's `test/test-fusion/` app is a worked example: it imports the subpath and has no bundler configuration at all. ## Next - [API reference](/academy/Reference/ReactorBrowserClient/ApiReference) - provider props, client methods, and the full limitations list. - [Subgraphs and attachments](/academy/Reference/ReactorBrowserClient/SubgraphsAndAttachments) - typed access to your project's own subgraphs, and the attachment service. - [React Hooks](/academy/Reference/EditorsUI/ReactHooks) - the hooks that run on top of this client. --- ## API reference > Source: https://academy.vetra.io/academy/Reference/ReactorBrowserClient/ApiReference Everything on this page is exported from `@powerhousedao/reactor-browser`. ```typescript GraphQLReactorClient, GraphQLReactorProvider, useSwitchboardClient, type GraphQLReactorClientOptions, type GraphQLReactorProviderProps, type IReactorBrowserClient, } from "@powerhousedao/reactor-browser/graphql-client"; ``` For the quickstart and how the entry stays browser-bundlable, see [Overview and quickstart](/academy/Reference/ReactorBrowserClient/OverviewAndQuickstart). ## `IReactorBrowserClient` The contract the reactor-browser hooks depend on. It is declared as a `Pick` of the reactor's own interfaces, so the member signatures cannot drift from the reactor's: ```typescript export interface IReactorBrowserReadClient extends Pick< IReactorReadClient, | "get" | "getMany" | "find" | "getOperations" | "subscribe" | "waitForJob" | "getJobStatus" > {} export interface IReactorBrowserWriteClient extends Pick< IReactorWriteClient, "execute" | "create" | "deleteDocument" > {} export interface IReactorBrowserClient extends Pick< IReactorClient, | "get" | "find" | "subscribe" | "execute" | "getOperations" | "newDocument" | "create" | "deleteDocument" | "undo" | "redo" | "undoRedoTargets" | "undoRedoAvailability" > { readonly read: IReactorBrowserReadClient; readonly write: IReactorBrowserWriteClient; } ``` `read` and `write` are the client halves the hooks use. Each `write` method resolves once its job is accepted and returns a `WriteHandle`; read the document back with `handle.document()`. Write through `write`; the picked top-level methods remain for callers that have not moved to the halves. Two implementations satisfy it: the full in-browser reactor client, which gets it for free by implementing `IReactorClient`, and `GraphQLReactorClient`. On `GraphQLReactorClient`, `write.execute` submits the job and hands back a handle whose `written` and `readable` resolve from the job's status, woken by the `jobChanges` subscription when realtime is on. Its `document()` runs the `document` query bounded by the receipt's consistency token. `write.create` and `write.deleteDocument` run synchronous mutations and hand back a handle that has already settled. `newDocument`, `undo`, `redo`, `undoRedoTargets` and `undoRedoAvailability` are top-level members only: the browser halves carry no undo. Everything outside these members is deliberately excluded from the interface: `drives`, `resolveIdOrSlug`, relationships, `executeAsync`, `executeBatch`, `createEmpty`, `rename`, `evaluateActions`, and the document-model module getters. For the full-reactor versions of those, see [IReactorClient](/academy/Reference/Reactor/ReactorClient). ### Methods | Method | Behaviour on the light client | | ------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- | | `get(identifier, view?, signal?)` | Reads by id or slug. A `view.revision` throws: the GraphQL read API has no point-in-time reads. | | `getOperations(identifier, view?, filter?, paging?, signal?)` | Paged. `next()` follows the returned cursor; the default page is `{ cursor: "0", limit: 100 }`. | | `newDocument(documentType, options?, signal?)` | Heads a new, uncreated document with the versions the Switchboard selects for the type and parent, fitted to this client's `documentModels`, and its create signature policy. | | `create(document, parentIdentifier?, signal?)` | Creates the document as its header says and emits `Created`. | | `execute(identifier, branch, actions, signal?)` | Reads the document, stamps and signs a single action, pushes it, emits `Updated`. Two round trips. | | `deleteDocument(identifier, propagate?, signal?)` | Deletes and emits `Deleted` carrying the identifier it was called with. | | `subscribe(search, callback, view?)` | Registers a change subscriber and returns its unsubscribe function. `view` is accepted and ignored. | | `undo(identifier, scope?, branch?, signal?)` | Submits `UNDO` signed by this client. On base-reducer 3 it asks the Switchboard for the target and submits a `RETRACT` of it, signed by this client. A stale target is refused, not re-resolved. | | `redo(identifier, scope?, branch?, signal?)` | Submits `REDO`, or on base-reducer 3 a `RETRACT` of the last live retraction, the same way. | | `undoRedoTargets(identifier, scope?, view?, signal?)` | The rows a base-reducer 3 undo and redo would retract, resolved by the Switchboard as the caller. | | `undoRedoAvailability(identifier, scope?, branch?, signal?)` | On base-reducer 3, read from `undoRedoTargets`; below it, from the scope's revision. | `useDispatch` writes through `write.execute` and reads the result back with `handle.document()`. That is how it reports per-action errors: `dispatchActions` scans the read-back document's operations for the action it sent and reads its `error` field. ## `GraphQLReactorClient` ```typescript new GraphQLReactorClient(options: GraphQLReactorClientOptions); ``` Construct it directly only outside React. Under React, mount `GraphQLReactorProvider` and read the client from a hook. ### `GraphQLReactorClientOptions` | Option | Type | Default | Notes | | ------------------ | ------------------------------------ | ---------------------------- | ------------------------------------------------------------------------------------------------------------- | | `url` | `string` | required | The Switchboard's GraphQL endpoint, e.g. `http://localhost:4001/graphql`. | | `tokenProvider` | `() => Promise` | `ambientRenownTokenProvider` | Resolves the bearer token per request. | | `subscriptionsUrl` | `string` | derived from `url` | The websocket endpoint. `http` -> `ws`, `https` -> `wss`, `/subscriptions` appended. | | `realtime` | `boolean` | `true` | `false` opens no websocket; the client then emits only its own changes. | | `graphqlClient` | `ReactorGraphQLClient` | built from `url` | A pre-built SDK, mainly a test seam. It owns its own transport and headers, so it carries no auth middleware. | ### Methods beyond the interface ```typescript subgraph(name: string, getSdk: SubgraphSdkFactory): TSdk; request( document: TypedGraphQLDocument | string, variables?: TVariables, options?: GraphQLRequestOptions, ): Promise; dispose(): void; ``` `subgraph` and `request` are covered in [Subgraphs and attachments](/academy/Reference/ReactorBrowserClient/SubgraphsAndAttachments). `dispose()` closes the realtime socket. The provider calls it on unmount. Registered subscribers are left in place, since they still receive the events the client produces itself. It is not final: a `subscribe` after a dispose opens a new socket. React remounts a tree by running an effect's cleanup and then the effect again on the same client - `StrictMode` does it on every mount - and a terminal dispose would leave those pages with no realtime at all and nothing logged. ## `GraphQLReactorProvider` | Prop | Type | Default | What it does | | ------------------- | ------------------------------------ | ------------------------- | ----------------------------------------------------------------------------------------- | | `url` | `string` | required | The Switchboard's GraphQL endpoint. | | `tokenProvider` | `() => Promise` | the logged-in Renown user | Resolves the bearer token, per request. Return `undefined` to send a request anonymously. | | `subscriptionsUrl` | `string` | derived from `url` | The websocket endpoint. | | `realtime` | `boolean` | `true` | Whether server-pushed changes reach subscribers. | | `attachmentService` | `PHGlobal["attachmentService"]` | none | Published into the slot the editors read. | | `documentModels` | `readonly DocumentModelModule[]` | none | The app's generated modules, published so the document-model hooks work. | | `children` | `ReactNode` | required | | On mount the provider registers the `window.ph` event handlers once per page, publishes the client into `window.ph.reactorClient` and a `DocumentCache` built over it into `window.ph.documentCache`. On unmount it disposes the cache and the client, and leaves the slots populated, matching how Connect never unsets them. The props are read once, in the `useState` initialiser. Changing `url` or `tokenProvider` afterwards does not rebuild the client; remount the provider if the endpoint has to change. `attachmentService` and `documentModels` are the exceptions: each is published from its own effect, so passing them later does not throw away every open document. `documentModels` takes the app's generated modules, imported from the package's `document-models` **subpath** — never from the package root, whose entry also exports `processorFactory` and would drag processor (server-side) code into the browser bundle. They publish through a fixed `StaticPackageManager` into the same `window.ph.vetraPackageManager` slot Connect fills, which makes `useDocumentModelModules` and `useDocumentModelModuleById` work below the provider. `useDocumentModelModuleById(type, version?)` resolves the LATEST version when `version` is omitted — the registry's own semantics — and pins an exact one when given. Passing no modules is equally supported: actions are plain data and the Switchboard runs the reducer either way. Deliberately modules only, no editors: editors read the selected document through Connect's drive/node selection and are not portable to light apps yet — import an editor's React components directly if you need them. ## Hooks | Hook | Returns | Use it for | | ------------------------ | ------------------------------------ | -------------------------------------------------------------------------------- | | `useReactorClient()` | `IReactorBrowserClient \| undefined` | The six shared members. Works on both implementations. | | `useSwitchboardClient()` | `GraphQLReactorClient \| undefined` | `subgraph`, `request` and `dispose`. `undefined` when a full reactor is mounted. | `useSwitchboardClient` is `useReactorClient()` narrowed to the light client, so a component reaches its extras without a cast, and a component running against a full reactor gets `undefined` rather than a runtime failure. The narrowing is by brand rather than by `instanceof`, so a client built by a second copy of the package - a bundled package carrying its own, or a hot-replaced module - is still recognised. **WARNING:** `useGraphQLReactorClient` is a different, older hook. It reads the legacy `window.ph.reactorGraphQLClient` slot and has nothing to do with this client. The document hooks (`useDocument`, `useDocuments`, `useDispatch`, `useDocumentOperations` and the rest) are unchanged. See [React Hooks](/academy/Reference/EditorsUI/ReactHooks). ## Signing The signer is resolved from `window.ph.renown` at push time, so a client built before login signs after login with no rebuild. | Situation | What happens | | -------------------------------------- | -------------------------------------------------------------------- | | Logged-in Renown user, one action | The action is stamped, then signed, then pushed. | | Logged-in Renown user, several actions | The batch is pushed unsigned and a warning is logged. | | No user | Nothing is stamped or signed. The server computes the hashes itself. | Stamping writes two fields onto `action.context`: - `prevOpHash` = `hashDocumentStateForScope(document, scope)`, the hash of the state the action applies to. - `prevOpIndex` = `header.revision[scope] - 1`, so an empty scope stamps `-1`. Order matters: the signer reads `context.prevOpHash` off the action, so the client stamps before it signs. Batch signing is not supported because the second action's baseline hash cannot be computed without running the reducer, and the light client deliberately ships no reducer. Two things are worth knowing when you verify signatures: - The Switchboard signs an unsigned action with its own `did:key`, so a non-empty `signatures` list is not evidence of client-side signing. Check `context.signer.user.address` instead. - The server does not validate `prevOpHash`. A wrong hash is accepted, so conformance has to be asserted in tests rather than inferred from a write succeeding. ## Reactivity Every successful `create`, `execute` and `deleteDocument` emits a change event to matching subscribers. That is what makes a `DocumentCache` built on this client invalidate after a dispatch, with no websocket involved. `SearchFilter` is `{ type?, parentId?, ids?, slugs? }`. Each populated field is an AND condition tested by truthiness, so `ids: []` matches nothing and `{}` matches everything. `parentId` is ignored: this client has no drive concept. | Event | Payload | | --------- | ----------------------------------------------------------- | | `Created` | `{ type, documents: [created] }` | | `Updated` | `{ type, documents: [updated] }` | | `Deleted` | `{ type, documents: [], context: { childId: identifier } }` | Events are emitted after the write resolves and before it returns, so a rejected write emits nothing. ### Realtime The first `subscribe` opens one websocket for all subscribers and subscribes to `documentChanges` with no `search` argument. The server sends every change the connection may read, and this client applies each subscriber's filter on the way out. The endpoint is `` with `http` swapped for `ws` and `/subscriptions` appended - `ws://localhost:4001/graphql/subscriptions` for a Switchboard on `http://localhost:4001/graphql`. There is one socket server for the whole Switchboard, not one per subgraph. Auth on the socket reuses the same `tokenProvider`, sent as the lowercase `authorization` connection param and re-resolved on every reconnect. A Switchboard with auth enabled refuses a connection that arrives without it, so an anonymous subscriber gets no realtime there. A socket that cannot be opened or is refused is logged once and then ignored. ### Id space Change events carry resolved document ids, never the identifier a write was issued with. Read documents by id: a `DocumentCache` entry keyed by slug is never invalidated. The one exception is `deleteDocument`, which announces its identifier verbatim because the mutation returns a boolean and there is no document left to read an id off. ## Auth One `tokenProvider`, resolved per request, applied to the generated SDK, the hand-authored mutation, subgraph SDKs and the websocket connection params alike. The HTTP header is a lowercase `authorization`. ```tsx session?.accessToken} > ``` The default provider returns the logged-in Renown user's bearer token, and `undefined` when nobody is logged in. Returning `undefined` or an empty string sends the request with no authorization header at all. A provider that rejects fails the request rather than silently downgrading to anonymous. ## Limitations Known and deliberate. None of these is a bug report. | Limitation | Detail | | ----------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | No authorization preflight | `useCanExecute` reports `status: "unsupported"` here. The hook reads the reactor client module, which this setup never sets, and `evaluateActions` is not on the light client. Controls render as if unasked; the submit still refuses. | | No offline or local-first writing | No IndexedDB, no outbox, no optimistic state, no reconnect flush. A dispatch that fails is a failed dispatch. | | `execute` costs two round trips | One read for the signing and `sinceRevision` baseline, one mutation. | | No batch signing | More than one action in a batch is pushed unsigned with a warning. | | No point-in-time reads | A `view.revision` throws in `get` and `getOperations`. | | Slug-keyed cache entries are never invalidated | Change events carry resolved ids. Read by id. | | Concurrent dispatches can leave the cache stale | A forced refetch that arrives while another is in flight returns the pending one instead of chaining. The next server event repairs it. | | A mixed-scope batch over-fetches | The returned operations are windowed from the lowest head revision across the scopes the batch targets. | | No drives, no `find`, no jobs | Not on the interface. `create(document, parentIdentifier?)` is the only parenting there is. | | Realtime degrades to local-only | An unreachable or refused socket is logged once and ignored. The failed socket is closed, so the next subscriber retries; nothing subscribes on sign-in of its own accord. | | No document-model or editor modules | The provider populates neither `window.ph.vetraPackageManager` nor `window.ph.reactorClientModule`, so `useVetraPackages`, `useDocumentModelModules`, `useEditorModules` and `useEditorModuleById` return empty or `undefined`, and the exported `loadFile`, `addDocument`, `addFileWithProgress` and `copyNode` helpers throw or no-op. Render editor components the app imports directly. | | The attachment service is a wiring seam | The prop reaches `useAttachmentService` below the provider and nothing more. No attachment has been read over this client end to end. | | Own writes are announced twice | Once locally, once by the server push, so the cache refetches twice. Harmless. | | Import the `/graphql-client` subpath | The package root reaches `@powerhousedao/reactor` and does not bundle for the browser. See [Bundling](/academy/Reference/ReactorBrowserClient/OverviewAndQuickstart#bundling). | --- ## Subgraphs and attachments > Source: https://academy.vetra.io/academy/Reference/ReactorBrowserClient/SubgraphsAndAttachments A Switchboard serves more than the reactor's document API. Your project's own subgraphs sit next to it, and attachments come from a service the app supplies. The light client reaches both through the configuration it already has: one URL, one token provider. ## Typed subgraphs A subgraph is served at `/`. For a Switchboard on `http://localhost:4001/graphql`, the `renown-read-model` subgraph is at `http://localhost:4001/graphql/renown-read-model`. `client.subgraph(name, getSdk)` derives that URL from the client's `url`, keeps one transport per name, and hands the SDK the same auth middleware every reactor call goes through. ```tsx "use client"; const UNKNOWN_ADDRESS = "0x0000000000000000000000000000000000000000"; export function Probe() { const client = useSwitchboardClient(); async function probe() { if (!client) return; const sdk = client.subgraph("renown-read-model", getRenownReadModelSdk); const { renownUsers } = await sdk.RenownUsers([UNKNOWN_ADDRESS]); return renownUsers; } return ( ); } ``` `subgraph` gives you transport, auth and typing. Results are not cached, produce no change events and never reach `useDocument`. Cache them with whatever the app already uses for server state. The registry caches the transport per name, but reuses the SDK only when the same factory function is passed again. Two callers with two different generated SDKs each get their own, rather than the first caller's methods under the second caller's type. **INFO:** The root supergraph federates every subgraph, so a field a subgraph defines is usually reachable at the base URL too. If you need to prove a request went to the subgraph's own endpoint, query `_service { sdl }`: the root has no `_service` field. ### Generate the SDK Point `graphql-codegen` at the subgraph's endpoint and use the same three plugins `@powerhousedao/reactor-browser` generates its own SDK with: ```ts // codegen.ts const config: CodegenConfig = { schema: "http://localhost:4001/graphql/renown-read-model", documents: ["src/**/*.graphql"], generates: { "./src/graphql/renown-read-model.ts": { plugins: [ "typescript", "typescript-operations", "typescript-graphql-request", ], config: { scalars: { JSONObject: "NonNullable", DateTime: "string | Date", }, skipTypename: true, maybeValue: "T | null | undefined", gqlImport: "graphql-tag#gql", }, }, }, }; export default config; ``` The `typescript-graphql-request` plugin emits `getSdk(client: GraphQLClient, withWrapper?: SdkFunctionWrapper)`, which is exactly the `SubgraphSdkFactory` signature `subgraph` expects. Pass the generated function straight in. ```typescript export type SubgraphSdkFactory = ( client: GraphQLClient, withWrapper?: SdkFunctionWrapper, ) => TSdk; ``` `SdkFunctionWrapper` is re-exported from `@powerhousedao/reactor-browser`, so a hand-written SDK can name the type without depending on `graphql-request` directly. An SDK the client builds is always called with the auth wrapper: apply it to every operation, exactly as the generated code does. ### One-off documents For an operation not worth generating an SDK for, use `client.request`. It runs against the Switchboard's own endpoint, through the same transport and auth. ```ts const DOCUMENT_MODEL_IDS = ` query DocumentModelIds { documentModels { items { id } } } `; type DocumentModelIds = { documentModels: { items: { id: string }[] }; }; const { documentModels } = await client.request(DOCUMENT_MODEL_IDS); ``` A document a code generator typed carries its own result type, so the type argument is only needed for hand-written documents and strings. The operation name and type are read off the document; override them through the third argument, which also takes an `AbortSignal`. ## Attachments Attachments are configured at the same point as everything else, but the app constructs the service. Which storage it talks to is the app's business, and `@powerhousedao/reactor-browser` does not depend on the attachment implementation. ```tsx ``` The provider publishes it into the `window.ph.attachmentService` slot Connect publishes it into, so `useAttachmentService` and the editors below find it. Leave the prop out and the slot stays empty, exactly as it is without this provider. This prop is a wiring seam, and that is all it has been proven to be: the service an app passes in reaches `useAttachmentService` below the provider. No attachment has been read over the light client end to end. It is published from its own effect, keyed on the prop, so an app that builds the service asynchronously can pass it in later. Passing a new service does not rebuild the client or the document cache. Build the service with `createRemoteAttachmentService` from `@powerhousedao/reactor-attachments/client`, pointed at the same Switchboard. Import the `/client` subpath, not the package root: the root entry also exports the Kysely, S3 and filesystem backends, so a browser bundler has to resolve `kysely`, `@aws-sdk/client-s3` and `node:fs` and fails before any app code runs - the same failure class the light client's own [Bundling](/academy/Reference/ReactorBrowserClient/OverviewAndQuickstart#bundling) section describes, and it has the same shape of fix: import the browser subpath. See [Attachment service](/academy/Reference/Reactor/AttachmentService) for the service and client APIs, the reserve-send-read flow, and the `attachment://v1:` ref format. --- ## Hosts, Runners & Plugins > Source: https://academy.vetra.io/academy/Reference/HostsAndRunners/HostsRunnersAndPlugins **INFO:** This reference covers the **uniform runners** system. `ph run ` starts any host your project declares, and `ph init` writes the `hosts/` folder. The classic commands (`ph switchboard`, `ph connect`, `ph vetra`) are deprecated: they work in 6.3 with a warning, and 6.4 removes their own boot path. To move an existing project, see [Moving to hosts and `ph run`](/academy/Reference/HostsAndRunners/MigratingToHosts). Since 6.3 the `FEATURE_UNIFORM_RUNNERS_ENABLED` variable has no effect: remove it. Set to `false` it prints a warning, and 6.4 refuses to start. A Powerhouse project describes every way it can run in its `hosts/` folder. Each subfolder holds one **host file** — plain JavaScript, checked in, no build step: ``` hosts/ connect/host.js ph run connect switchboard/host.js ph run switchboard vetra/host.js ph run vetra desktop/host.js ph run desktop ``` `ph run [verb]` loads the host file, validates it against the runner's schema, and calls the runner's verb (`run` is the default; `dev`, `serve`, `build` are common others). ## The host file A host file default-exports an object with a **restricted top level** — five keys, in this order: ```js // @ts-check export default { // 1. Identity of the host (required, unique within the project) name: "my-switchboard", // 2. Presentation: how the host introduces itself (optional) profile: { appId: "com.acme.switchboard", name: "Acme API", description: "Acme's document API", icon: "./icon.svg", }, // 3. WHO runs it and HOW — module, port, and the plugin composition runner: { module: "@powerhousedao/ph-cli/runners/switchboard", port: config.reactor?.port ?? 4001, host: "127.0.0.1", plugins: [ "@powerhousedao/switchboard/plugins/mcp", "@powerhousedao/switchboard/plugins/attachments", "@powerhousedao/switchboard/plugins/workflows", ], }, // 4. Runner-defined settings (a switchboard reads `config.switchboard`) config: {}, // 5. EITHER a reactor block (single-reactor host) ... reactor: { protocol: config.connect?.reactor ?? {}, drives: [ { slug: "orders", name: "Orders", preferredEditor: "orders-app" }, ], }, // ... OR a children block (compound host) — never both. See "Compound hosts". }; ``` Two design rules follow from this shape: - **Reference, don't duplicate.** Values that several hosts must agree on (ports, the replay protocol) live in `powerhouse.config.json` and are *referenced* from host files, so client and server cannot drift. - **Everything is visible.** There are no hidden defaults layered behind the file: the composition you read is the composition that runs. ## Plugin entries: presence is activation The `runner.plugins` array is the host's **composition**. An entry is either a module specifier string or an object: ```ts type PluginEntry = | string | { module: string; // the identity — duplicates fail boot enabled?: boolean; // default true verbs?: "*" | string[]; // default "*": active for every verb config?: object; // validated against the plugin's configSchema }; ``` The rules: - **Presence is activation.** A listed plugin is on; an absent plugin is off. There are no enable/disable flags elsewhere — deleting the line is the off switch. (A curated API host deletes `mcp` and simply has no `/mcp`.) - **Verb scoping** makes one host serve two moods. The canonical example is Vetra Studio, which is listed `verbs: ["dev"]` — `ph run vetra dev` is the studio; `ph run vetra` is the same stack headless. - **Entry config** is data for *that plugin instance*, validated against the schema the plugin declares. A plugin that declares no schema accepts no config — passing one is a boot error, not a silent ignore. ```js plugins: [ "@powerhousedao/switchboard/plugins/attachments", { module: "@powerhousedao/vetra-studio/switchboard", verbs: ["dev"] }, { module: "@acme/audit-plugin/switchboard", config: { sink: "https://audit.acme.com", batchSize: 50 }, }, ], ``` ### Composing across hosts: sibling imports, full inclusion Host files import each other by folder name and build their lists with plain JavaScript — spread, filter, concat. There is no merge DSL and no override grammar; a derived host *fully includes* what it wants and adds the rest: ```js // vetra's switchboard member = the project's switchboard + the studio plugin plugins: [ ...switchboard.runner.plugins, { module: "@powerhousedao/vetra-studio/switchboard", verbs: ["dev"] }, ], ``` Anything expressible over arrays is expressible here — `switchboard.runner.plugins.filter(...)`, reordering, insertion — and the result is always readable in place. Import cycles between host files fail at load with both names. A host file may import exactly three things: the project's `powerhouse.config.json`, a sibling's `..//host.js`, and a sibling's generated `..//outputs.js` (`ph generate hosts`). `process.` is banned — read the environment with `{ $env: "NAME" }` (or `{ $env: { name: "NAME", optional: true } }` to omit the property when unset), resolved when the host starts. ## Core reactor config (not plugins) Some things every reactor has; they are **core config**, not plugins: ```js reactor: { // Fleet-consensus replay/admission settings — validated STRICTLY // (a typo'd key fails boot, it does not silently not-apply): protocol: config.connect?.reactor ?? {}, // Declared drives. "Can't have a reactor without drives." drives: [ { slug: "orders", // the seeding key (idempotent across boots) name: "Orders", preferredEditor: "orders-app", // which app opens documents in it // id: "explicit-id", // or derive it from the project path: // projectHash: true, // same ids classic `ph vetra` produces // remote: { url: "http://127.0.0.1:4001/d/orders" }, // attach, don't own // icon, sharingType: "public" | "private" | "shared", availableOffline }, ], // Storage/read-model backends, renown identity, namespace — see the // generated hosts in a fresh `ph init` project for worked examples. }, ``` Drives declared here are **seeded idempotently** by slug on every boot, and plugins may *contribute* additional drives. To see the merged result with provenance before running anything: ```bash ph run switchboard --explain drives ``` ### Linking to a switchboard: drive addresses and placement A `remote` backend links a host to a switchboard by the address of one of its drives, `/d/`, never by its GraphQL endpoint. The address names the switchboard and the drive, and sync needs both. Read it from the switchboard's outputs: ```js reactor: { backends: { main: { kind: "worker" }, // or { $output: { host: "switchboard", path: ["drives", "b", "url"] } } sb: { kind: "remote", remote: { url: switchboard.drives.b.url } }, }, // More drives on the same switchboard are listed here, not as a second // backend: one switchboard is one remote backend. drives: [{ slug: "c", remote: { url: switchboard.drives.c.url } }], // Where each drive opens, by its declared slug. placement: { drives: { b: "sb", c: "main" } }, }, ``` `placement.drives` maps a drive's declared slug to a backend: - the drive's own remote backend opens it on the switchboard, with no copy in this reactor; - a local backend (`worker`, `in-process`) keeps a synced copy and opens it locally; - a drive that is not listed keeps a synced copy, as before. The map is checked when the host loads. Connect applies it at boot: a drive placed on a switchboard gets no sync remote in the browser (one an earlier boot added is removed), and its reads and writes go to that switchboard. A Connect may link to several switchboards, one remote backend each. ### Where new drives go, and how Connect reaches a switchboard A Connect host sets where new drives are created, how long it waits for each switchboard, and when it stops calling one that doesn't answer: ```js reactor: { backends: { main: { kind: "worker" }, sb: { kind: "remote", remote: { url: switchboard.drives.b.url, // Give up on one request after this long. Default 30 s. requestTimeoutMs: 10_000, // After 3 failed requests in a row, send nothing for 2 minutes. // Default { failures: 3, openMs: 30_000 }. false: keep trying. breaker: { failures: 3, openMs: 120_000 }, // Live updates from this switchboard. Default true. realtime: true, }, }, }, placement: { // New drives are created on sb, with no copy in this browser. newDrives: "sb", }, router: { // Wait this long for a reactor to say whether it holds a document. // Default 3 s; 0 waits for the answer. probeTimeoutMs: 3_000, // Look again after this long for a document found on a switchboard. // Default 30 s. provisionalRouteMs: 30_000, }, }, ``` `placement.newDrives` names the backend new drives are created on. Without it they are created in the browser, or, when the host has no browser reactor, on its only switchboard. A Connect with no browser reactor and several switchboards must set it. A drive created on the `newDrives` switchboard stays there: when the switchboard isn't answering, Connect shows ` isn't answering, so the drive wasn't created.`, and when it refuses the create, Connect names it and gives its reason (or asks you to sign in). The drive is not created in the browser instead: a Connect that has no address for that switchboard, or whose router fails to load, doesn't start. Drives the host declares locally (`reactor.drives` without a `remote`) are created in the browser, with the browser reactor's create defaults. Connect doesn't use `placement.primary`, and refuses a host that sets it: the browser reactor, when there is one, answers what no drive owns. Use `placement.newDrives` to choose where new drives go. A switchboard host keeps `placement.primary` to pick its own store. `requestTimeoutMs`, `breaker` and `realtime` apply only to a `remote` backend. Switchboard hosts ignore `newDrives` and `router`. Validation refuses a remote link that is not a drive address (an `endpoints.graphql` token or a `/graphql` URL fails with an error naming the `drives..url` form), two remote backends on the same switchboard, a placement that names a backend that does not exist, a drive placed on a switchboard it is not on, a slug that names two drives (on two switchboards, or kept here and on a switchboard), and a placement key that names no drive the host links to, declares or attaches through a plugin's `config.drive`. Keys for drives known only at runtime (a `$map` list) are not checked. A `reactor.drives[]` entry's `remote.url` must be a drive address too. ## Switchboard settings (`config.switchboard`) A switchboard host sets the rest of its server config, such as auth, inspection and workers, under `config.switchboard`. It takes the keys of `@powerhousedao/switchboard/config`, except the ones the host file already sets elsewhere: storage, protocol and dynamic packages in `reactor`, and `runner.port`, the `dev` verb, `reactor.drives` and `runner.plugins`: ```js config: { switchboard: { auth: { admins: ["0x…"], defaultProtection: true }, inspection: { enabled: false }, workers: { executor: { count: 2 } }, }, }, ``` Each switchboard gets its own config, so two switchboards in one compound can use different stores, ports and settings. A key set in both `reactor` and `config.switchboard` fails validation. An environment variable the operator sets still wins over both blocks, for example `PH_REACTOR_DATABASE_URL` over the reactor's storage. A value that only the project `.env` sets loses to what `reactor` sets (protocol, storage, dynamic packages, workflows) but, like the shell's, wins over `config.switchboard`. A `DATABASE_URL` does not override `reactor.backends.*.readModel.url`. An environment variable that contradicts `reactor.protocol` fails the boot. A backend with a `readModel.url` and no `storage` still keeps its reactor store on that url when no environment variable names one. This is deprecated, and the switchboard warns at boot: set `storage` explicitly. ## The plugin shape A plugin module default-exports either a `HostPlugin` object or — when it accepts config — a **factory** `(config) => HostPlugin`: ```ts interface HostPlugin { name: string; // stable id: ordering, dependsOn, diagnostics appliesTo?: string; // host-type requirement; default "reactor" enforce?: "pre" | "post"; // coarse ordering bucket (Vite-style) dependsOn?: string[]; // hard deps on OTHER PLUGINS (never the host) configSchema?: z.ZodType; // no schema = no config accepted hooks?: Partial; // the behavior contributions?: { // declarative, mergeable data drives?: DriveDeclaration[]; packages?: string[]; configDeclarations?: { name: string; type?: string; description?: string }[]; }; } ``` **`appliesTo` and the host-type hierarchy.** Compatibility walks a parent chain: `connect`, `switchboard`, and `mastra` all extend `reactor`, while `compound` is its own branch (plugins over a *member set*, like a proxy). A plugin with `appliesTo: "reactor"` composes onto any reactor host; one with `appliesTo: "switchboard"` is rejected elsewhere — at boot, with both names in the error. **Hook kinds.** Host runtimes define a hook *vocabulary*; plugins implement subsets of it. Three kinds exist: | kind | shape | semantics | | --- | --- | --- | | provide | `(ctx) => T` | one winner — two providers of the same hook fail boot | | transform | `(value, ctx) => T` | chained in plugin order, each sees the last's output | | observe | `(ctx) => void` | lifecycle notification; teardown moments run sequentially in order | **Ordering** is `enforce: "pre"` → normal → `"post"`, then `dependsOn` topo-sorted within each bucket. Cycles and missing dependencies fail boot naming both sides. ### Hook vocabularies are owned by the host runtime Each runtime publishes the moments plugins can join. The switchboard's vocabulary (the built-in `mcp` / `attachments` / `privacy` / `workflows` plugins are ordinary consumers of it): ``` transformServerOptions shape the server options before boot onReadModelsRegistering contribute read models onHttpMounting mount routes on the HTTP app onSubgraphsRegistering contribute GraphQL subgraphs onReactorReady the reactor is up onDrivesReady declared drives are seeded onShutdown teardown — sequential, in plugin order ``` Connect's browser-side pipeline currently resolves **built-in** plugins from its own registry (`generic`, `sentry`, `openpanel`, `assistant`, `vetra-studio/connect`); loading arbitrary third-party plugin modules in the browser is on the roadmap. Server-side (switchboard, compound) compositions resolve real modules from your project's `node_modules` today. The generic plugin owns the drive-chrome toggles: `allowAddDrive` and `sections.{remote,local}.{enabled,allowAdd,allowDelete}` (configure them through its entry `config`, not top-level `connect.drives`). When a host declares a plugin list without `@powerhousedao/connect/plugins/generic`, those toggles turn OFF and Connect hides the generic chrome: the sidebar and the drive-list home screen. Drive and document views under `/d/` still render. With no declared plugin list, every plugin reads as present, so classic Connect is unchanged. (`branding` is NOT the generic plugin's concern. It reaches past the chrome, for example into the document title, so it stays a top-level `connect.branding` value that survives the plugin's absence. The switchboard side behaves the same way: removing `mcp` removes `/mcp`.) ### Workflows: one link, two plugins Workflows have a server half and a browser half, and the two are declared so the link between them is visible in `hosts/`: ```js // hosts/switchboard/host.js — serves the workflow runtime AND the drive plugins: [ { module: "@powerhousedao/switchboard/plugins/workflows", // the Workflows drive this plugin seeds; `drive: false` = runtime only config: { drive: { slug: "workflows", name: "Workflows" } }, }, ], // hosts/connect/host.js — loads Workflow Studio, attaches that drive plugins: [ { module: "@powerhousedao/connect/plugins/workflows", config: { drive: "http://localhost:4001/d/workflows" }, }, ], ``` - The **switchboard plugin** contributes its drive to the host's drive plan (`contributions.drives`): `ph run switchboard --explain drives` lists it with the plugin as the source, a host declaration with the same slug wins, and the drive opens in Workflow Studio (`preferredEditor: "workflow-studio"` on the drive document). Defaults: slug `workflows`, name `Workflows`, a project-hashed id (the id classic `ph vetra` gave it). - The **Connect plugin** is presence-is-activation like the other built-ins: listed, Connect loads the workflow package (Workflow Studio exists) and attaches `drive` as a remote default drive; absent from a declared composition, `app.workflowsEnabled` is zeroed. The drive must be the switchboard's: Workflow Studio and the assistant's workflow tools reach the workflow runtime of the switchboard that serves the drive, or the only switchboard Connect reaches that runs workflows. Connect lists the switchboards it reaches (its remote backends by name, and each synced drive's switchboard); with several running workflows, the tools take a `switchboard` name and say which ones there are. Connect warns when a drive names a drive app that no installed package provides, and Workflow Studio says why when no switchboard's runtime can be picked. **Read the link from the switchboard's outputs instead of spelling the URL.** `drive` takes an output token: `switchboard.drives.workflows.url` from the generated `../switchboard/outputs.js` (`ph generate hosts`), or the literal form below: ```js "vetra-studio": { host: "connect", runner: { plugins: [ ...connect.runner.plugins, { module: "@powerhousedao/connect/plugins/workflows", config: { drive: { $output: { host: "switchboard", path: ["drives", "workflows", "url"] } }, }, }, ], }, }, ``` Inside a compound the token reads the switchboard member's live outputs, so the compound starts that member first. Standalone `ph run connect` reads the running switchboard's run record, and fails naming `ph run switchboard` when none is running. The removed `{ from, slug }` form fails validation, naming the token form. (The classic switches, `workflows.enabled` / `connect.app.workflowsEnabled` / the host's `reactor.workflows`, still parse; with the plugins listed they are redundant.) ## Writing your own plugin A complete switchboard plugin with config, in one file: ```ts // src/plugins/audit.ts (built to dist/, exported as ./plugins/audit) const configSchema = z.strictObject({ sink: z.string().url(), batchSize: z.number().int().positive().default(20), }); type AuditConfig = z.infer; export function createAuditPlugin(config: AuditConfig): HostPlugin<{ onReactorReady: (ctx: { read: unknown }) => Promise; onShutdown: () => Promise; }> { let flush: (() => Promise) | undefined; return { name: "acme-audit", appliesTo: "switchboard", configSchema, hooks: { onReactorReady: async (ctx) => { flush = startAuditPipeline(ctx.read, config.sink, config.batchSize); }, onShutdown: async () => { await flush?.(); }, }, contributions: { // declarative data merges with host-declared drives (visible in // `--explain drives` with this plugin named as the source): drives: [{ slug: "audit-log", name: "Audit Log" }], }, }; } export default createAuditPlugin; // factory: entry config is passed here ``` Publish it under a subpath (`"./plugins/audit"` in your package's `exports`), then list it: ```js plugins: [ { module: "@acme/my-package/plugins/audit", config: { sink: "https://audit.acme.com" } }, ], ``` Checklist: 1. **One default export** — the plugin object, or a factory if it takes config. The `module` specifier is the identity; listing it twice fails. 2. **Declare `configSchema`** if you accept config. Unknown keys and missing required keys fail at boot with the entry named. 3. **Pick the narrowest `appliesTo`** that is honest. `"reactor"` composes everywhere; `"switchboard"` only there. 4. **Prefer `contributions` over hooks** for anything declarative (drives, packages) — contributions merge, carry provenance, and show up in `--explain`. 5. **Clean up in `onShutdown`.** Teardown runs sequentially in plugin order. ## Writing your own runner A runner is a module the host file names in `runner.module`. Its contract is a handful of exports: ```ts export const kind = "reactor"; // or "compound" export const hostSchema = reactorHostSchema; // extend with .extend(...) for // runner-specific runner.* keys export async function run(host: HostDefinition, ctx: RunnerContext) { // boot your runtime from the validated host definition } export const serve = run; // additional verbs are just exports export async function dev(host: HostDefinition, ctx: RunnerContext) { /* … */ } export async function build(host: HostDefinition, ctx: RunnerContext) { /* … */ } ``` - `ph run ` calls the export named ``; `run` is the default (a `runner.defaultVerb` in the host file can change it). - `hostSchema` validates the host file before your code sees it. Start from `reactorHostSchema` / `compoundHostSchema` and extend. - A runner that composes plugins resolves them with `resolveHostPlugins` (from `@powerhousedao/host-core`) and defines its own hook vocabulary — the moments above are the switchboard's, not a global list. - Most custom hosts do **not** need a custom runner: the generic compound (`@powerhousedao/host-core/runners/compound`) plus plugins covers the common cases. ## Compound hosts A compound host supervises *members* instead of owning a reactor. `children` replaces `reactor`; each member wraps a sibling host by folder name: ```js /** @satisfies {PHHost} */ export default { name: "desktop", runner: { module: "@powerhousedao/desktop/runner" }, children: { server: { host: "switchboard" }, // the sibling folder, as is app: { host: "connect", runner: { defaultVerb: "serve", port: 3000 }, // Every drive the server member serves (declared and plugin-created, // e.g. Workflows), attached as a remote by the URL it reports. reactor: { ...connect.reactor, drives: { $map: { over: { $output: { host: "switchboard", path: ["drives"] } }, to: { slug: { $key: true }, remote: { url: server.drives.$item.url } }, }, }, }, }, }, }; ``` Wrapper semantics are **full-statement** for `config` and `reactor`: a section present on the wrapper replaces the member's section wholesale (spread the member's own section in, as above, when you mean "theirs plus mine"). `runner` is the exception: it key-merges over the member's own, so `port` or `defaultVerb` alone keeps the member's plugins. **Start order comes from the tokens.** The `app` member reads the `server` member's outputs, so the compound starts `server` first and resolves `app`'s tokens from its live outputs; `config.startOrder` is optional (it must agree with the tokens). `ph run desktop --explain outputs` prints the order and, while a producer runs, each token's value. **`$map`** renders a list (or, with `into: "record"`, a record) from a collection output: `over` is an `$output` (or, nested, an `$item`), `to` is the template rendered once per element. Inside `to`, `{ $key: true }` is the element's key (the index for an array) and `{ $item: ["url"] }` — or the typed `server.drives.$item.url` from `outputs.js` — reads a path of the element. Records are visited in key-sorted order, so the result is deterministic. A nested map names outer bindings with `as` and `{ $item: { as, path } }` / `{ $key: "" }`. Compound-level plugins (`appliesTo: "compound"`) act on the member set — the clint proxy is the reference example: one front port, routes declared per member, resolved from each child's `runner.{host, port}`. ## Debugging | symptom | tool | | --- | --- | | which drives will exist, and why | `ph run --explain drives` | | which outputs a host reads, start order, current values | `ph run --explain outputs` | | plugin rejected on this host | the error names the plugin, its `appliesTo`, and the host type | | two plugins provide one hook | boot error naming both — provide hooks have one winner | | entry config rejected | boot error naming the entry and the schema failure | | duplicate module in a composition | boot error — the specifier is the identity | | sibling import cycle | load error naming the cycle | --- ## Moving to hosts and `ph run` > Source: https://academy.vetra.io/academy/Reference/HostsAndRunners/MigratingToHosts Since 6.3 a project describes every way it can run in a `hosts/` folder, and `ph run ` starts any of them. The classic commands (`ph vetra`, `ph connect`, `ph switchboard`, `ph reactor`) still work in 6.3 and print a warning. Their own boot path is removed in 6.4. This page takes a project from the classic commands to hosts. For what a host file contains, see [Hosts, Runners & Plugins](/academy/Reference/HostsAndRunners/HostsRunnersAndPlugins). ## Who needs this page You need it when one of these is true: - Your project has no `hosts/` folder, or the folder holds no `/host.js`. - A `ph` command prints a line that starts with `Warning:` and ends with a link to this page. - Your scripts, Dockerfile or CI call `ph vetra`, `ph connect`, `ph switchboard` or `ph reactor`. - Your environment sets `FEATURE_UNIFORM_RUNNERS_ENABLED` or `FEATURE_PLUGIN_HOST_ENABLED`. A project created with `ph init` on 6.3 already has `hosts/`. It needs this page only for the warnings it still prints. ## Upgrade in two steps Go to 6.3 first, clear the warnings there, then go to 6.4. 1. Upgrade the project to 6.3 and run `ph migrate`. Everything keeps working; what 6.4 removes prints a warning. 2. Work through the warnings with the list on this page until none are left. 3. Upgrade to 6.4. If you skip 6.3, 6.4 fails where 6.3 warns, and you fix the same things without the warnings to guide you. Run a deployed Switchboard on 6.3 once before you move it to 6.4. From 6.3 a store records the release that last completed its one-time migrations. A later release that has dropped a migration uses that record to refuse a store that still needs it, and names the release to run first. ## Run `ph migrate` `ph migrate` updates the project files and adds the host files. Run it from the project folder, or from any folder below it. ### 1. Commit your work Start from a clean working tree, so the changes `ph migrate` makes are easy to review and to undo. `ph migrate` replaces the agent files of the project (such as `AGENTS.md` and `CLAUDE.md`). It stops before it writes anything when one of them has uncommitted changes. ### 2. Preview the changes ```bash ph migrate 6.3.0 --dry-run ``` The dry run prints a diff for every file it would add, change or delete and writes nothing. It ends with the number of files and the steps it did not preview: installing the dependencies, syncing the agent skills, and re-generating the code of your document models, editors, apps, processors and subgraphs. Leave out the version to stay on the release channel your project already uses. `ph migrate` prints the version it picked. ### 3. Run it ```bash ph migrate 6.3.0 ``` `ph migrate` does this: 1. Updates `package.json`. It sets the Powerhouse dependencies to the target version and adds missing scripts. A script that still has the value an earlier `ph init` wrote is updated, for example `ph-cli vetra` becomes `ph-cli run vetra`. A script you changed yourself is kept, and `ph migrate` prints which ones it kept. 2. Removes the lint and format configuration files of older scaffolds. 3. Adds the root files the project lacks, such as the Dockerfile and the CI workflows. A root file the project already has is kept as it is. When the current template has lines the kept file lacks, `ph migrate` prints one line with a link to the template. The JSON configuration files, such as `tsconfig.json`, gain the keys and entries they lack and keep the values you set. `ph migrate` also removes the compiler options TypeScript 7 dropped. 4. Replaces the agent files. 5. Adds the `hosts/` folder when the project has no `hosts//host.js` yet. Existing host files are never overwritten. 6. Moves the settings the new host files take over out of `powerhouse.config.json`, and prints what it did with each key. See [Settings](#settings). 7. Installs the dependencies and re-generates the code. A project that still has document models on the 5.x layout (no `v1/` folder) is refused with the names of those models. Run the 6.2.2 migration first, then this one again: ```bash npx @powerhousedao/ph-cli@6.2.2 migrate 6.2.2 ``` ### 4. Review the host files `ph migrate` writes these host files: ``` hosts/ README.md connect/host.js ph run connect switchboard/host.js ph run switchboard vetra/host.js ph run vetra desktop/host.js ph run desktop assistant/host.js ph run assistant cli/host.js ph run cli ``` Open `hosts/switchboard/host.js` and check `runner.plugins`. `ph migrate` lists the plugins the classic Switchboard ran: MCP, attachments and privacy. It adds the workflows plugin only when `powerhouse.config.json` sets `workflows.enabled` to `true`. `ph migrate` does not read your environment. If you switched a feature on or off with an environment variable only, add or remove that plugin yourself. ### 5. Move the settings `ph migrate` leaves alone When `ph migrate` adds the host files, it moves `studio.host`, `studio.openBrowser` and `reactor.https` into them and removes the keys from `powerhouse.config.json`. It prints one line for each key, for example: ```text Settings of powerhouse.config.json the host files take: studio.host: moved to runner.host in hosts/connect/host.js reactor.storage: kept, the switchboard does not read it when it starts, so moving it would change where the reactor keeps its data. To use it, set reactor.backends..storage in hosts/switchboard/host.js and remove the key. ``` A moved setting takes effect under `ph run`. The classic commands did not all read these keys, so check that the address and the browser behaviour are what you want. `ph migrate` keeps these keys, and you move them by hand: - `reactor.storage` and `switchboard.database.url`. Moving them would change where the reactor keeps its data. Set the storage in `hosts/switchboard/host.js` when you are ready to move the data, then delete the key. - `studio.https`. The Connect host has no setting that serves https. - Every key in [Settings](#settings), when the project already had host files. `ph migrate` does not edit host files it did not write. ### 6. Check the hosts ```bash ph host check switchboard ph host check connect ph host check vetra ``` `ph host check` loads and validates a host the way `ph run` would, without starting it. It exits with code 1 when it finds a problem. ## Old and new ### Commands | Old | New | | --------------------------------- | ----------------------------------- | | `ph vetra` | `ph run vetra` | | `ph vetra --watch` | `ph run vetra` | | `ph vetra --interactive` | `ph run vetra --interactive` | | `ph connect`, `ph connect studio` | `ph run connect dev` | | `ph connect build` | `ph run connect build` | | `ph connect preview` | `ph run connect serve` | | `ph switchboard` | `ph run switchboard` | | `ph reactor` | `ph run switchboard` | | `ph switchboard --dev` | `ph run switchboard dev` | | `ph switchboard --migrate` | `ph run switchboard migrate` | | `ph switchboard --migrate-status` | `ph run switchboard migrate-status` | | `ph switchboard --reset` | `ph run switchboard reset` | | `ph init --uniform-runners` | `ph init` | `ph connect config` is a tool, not a way to run Connect. It stays as it is. `--watch` is no longer needed. `ph run` accepts it, prints one line and ignores it. Arguments after the host and the verb go to the runner. When you pass one the runner does not accept, `ph run` stops and lists the ones it does accept. ### Settings These `powerhouse.config.json` keys are read by the classic commands only. `ph run` ignores them, and 6.4 refuses them. | Old key in `powerhouse.config.json` | New home | | ----------------------------------- | -------------------------------------------------------------------- | | `studio.host` | `runner.host` in `hosts/connect/host.js` | | `studio.https` | None. The Connect host serves http. | | `studio.openBrowser` | `runner.openBrowser` in `hosts/connect/host.js` | | `reactor.https` | `config.switchboard.server.https` in `hosts/switchboard/host.js` | | `reactor.storage` | `reactor.backends..storage` in `hosts/switchboard/host.js` | | `switchboard.database.url` | `reactor.backends..storage.url` in `hosts/switchboard/host.js` | These keys stay in `powerhouse.config.json`, because the scaffolded host files read them: `studio.port`, `reactor.port`, `vetra.connectPort`, `assistant.port` and the `connect` block. Four Connect keys move into the configuration of the generic Connect plugin. In `hosts/connect/host.js`, list `@powerhousedao/connect/plugins/generic` under `runner.plugins` and set them in its entry `config`: | Old key in `powerhouse.config.json` | Key in the plugin entry `config` | | ----------------------------------- | -------------------------------- | | `connect.drives.allowAddDrive` | `allowAddDrive` | | `connect.drives.sections` | `sections` | | `connect.drives.icons` | `icons` | | `connect.home.background` | `homeBackground` | ### Environment variables | Variable | What to do | | ----------------------------------- | -------------------------------------------------------------------------------------- | | `FEATURE_UNIFORM_RUNNERS_ENABLED` | Remove it. It has no effect since 6.3. | | `FEATURE_PLUGIN_HOST_ENABLED=false` | Remove it. Switchboard then starts the same subsystems through the plugin host. | `MCP_ENABLED` and `PH_WORKFLOWS_ENABLED` stay. ## Warnings and what to do Each warning prints once per command, on stderr. The parts that depend on your project, such as the command you typed, are shown here in angle brackets. ### The project has no host files {/* #no-host-files */} ```text Warning: This project has no hosts//host.js, so `` runs the classic way, which stops working in 6.4. Run `ph migrate` to add the host files, then use ``. ``` Follow [Run `ph migrate`](#run-ph-migrate), then use the replacement command the warning names. A `hosts/` folder without any `/host.js` counts as no hosts. `ph` then also prints that the folder defines no host. ### A classic command, and that host file is missing {/* #missing-host-file */} ```text Warning: `` runs the classic way, which stops working in 6.4. This project has no hosts//host.js: add that host file, then use ``. ``` The project has hosts, but not the one this command needs. `ph migrate` adds host files only to a project that has none, so add this one yourself: write `hosts//host.js`, using the [host file reference](/academy/Reference/HostsAndRunners/HostsRunnersAndPlugins#the-host-file). Then run `ph generate hosts` to update the generated typing files. ### A classic command in a project with hosts {/* #classic-command */} ```text Warning: `` runs the classic way and does not read hosts//host.js. The classic way stops working in 6.4: use `` instead. ``` The host file exists, but `ph connect` and `ph switchboard` do not read it. Change the command in your scripts, Dockerfile and CI to the replacement the warning names. See [Commands](#commands). `ph vetra` is different: in a project with `hosts/vetra/host.js` it already runs `ph run vetra` and prints no warning. ### `ph reactor` {/* #ph-reactor */} ```text Warning: `ph reactor` is a deprecated alias and is removed in 6.4. Use `ph run switchboard` instead. ``` Replace `ph reactor` with `ph run switchboard`. ### A Switchboard flag that became a verb {/* #switchboard-verbs */} ```text Warning: `ph switchboard --migrate` is deprecated and stops working in 6.4. Use `ph run switchboard migrate` instead. ``` `--migrate`, `--migrate-status`, `--reset` and `--dev` are verbs of the switchboard host now. Put the verb after the host name, without the dashes. ### A flag that is deprecated {/* #deprecated-flag */} ```text Warning: ` ` is deprecated and stops working in 6.4: ``` Follow the instruction at the end of the warning. For `--watch` that is removing the flag. ### A flag with no `ph run` argument {/* #flag-without-argument */} ```text Warning: ` ` has no `` argument and stops working in 6.4. Set what it configures in the host files under hosts/ instead. ``` `ph run` has no argument for this flag. Set the value in the host file of that host instead. The [host file reference](/academy/Reference/HostsAndRunners/HostsRunnersAndPlugins) lists the keys. ### A `powerhouse.config.json` key that 6.4 refuses {/* #config-keys */} A classic command prints: ```text Warning: powerhouse.config.json sets : only the classic commands read them, and 6.4 refuses them. Move to . ``` `ph run` prints, for each key: ```text Warning: powerhouse.config.json "" is ignored by `ph run` and is an error in 6.4. Set and remove the key. ``` Move each key to the place the warning names and delete it from `powerhouse.config.json`. The full list is under [Settings](#settings). Under `ph run` the old key has no effect already. If a host started with a different address or database than you expected, this is the reason. ### `FEATURE_PLUGIN_HOST_ENABLED` {/* #feature-plugin-host-enabled */} ```text Warning: FEATURE_PLUGIN_HOST_ENABLED=false stops working in 6.4. It keeps Switchboard on the classic boot path, which 6.4 removes: the plugin host is then the only path. Remove the variable: Switchboard then starts the same subsystems through the plugin host. To choose them yourself, list the plugins under `runner.plugins` in hosts/switchboard/host.js. ``` Remove the variable from your environment, `.env` files and deployment configuration. The values `false`, `0`, `off` and `no` select the classic boot path. Without the variable, Switchboard starts the same subsystems through the plugin host: MCP, attachments, privacy and workflows, each under the switch that selected it before. To choose the plugins yourself, list them under `runner.plugins` in `hosts/switchboard/host.js`. ### `FEATURE_UNIFORM_RUNNERS_ENABLED` {/* #feature-uniform-runners-enabled */} ```text Warning: FEATURE_UNIFORM_RUNNERS_ENABLED=false has no effect since 6.3. Hosts and `ph run` are always on now. Remove the variable: 6.4 will refuse to start with it set to false. ``` Remove the variable. Set to a true value it does nothing and prints nothing, but there is no reason to keep it. ### `reactor.workflows` in a host file {/* #reactor-workflows */} ```text Warning: : reactor.workflows is deprecated and stops working in 6.4. Workflows are a plugin: "" under runner.plugins switches them on, leaving it out switches them off. Remove the key. ``` Remove `reactor.workflows` from the host file. To run workflows, list the plugin the warning names under `runner.plugins`. To run without them, leave the plugin out. ### Connect settings in the host `config` {/* #connect-chrome-settings */} ```text Warning: : config.drives.allowAddDrive is only read by the classic Connect boot, which 6.4 removes. List "@powerhousedao/connect/plugins/generic" under runner.plugins and set allowAddDrive / sections / icons / homeBackground in its entry config. ``` Move the keys into the entry of the generic Connect plugin, as shown under [Settings](#settings). ### `readModel.url` also holds the reactor store {/* #read-model-url */} ```text Warning: : reactor readModel.url () also holds the reactor store, because no storage names one. This is deprecated and stops working in 6.4: set the backend's storage explicitly (the same Postgres url, or a path dataDir). ``` The host file names a database for the read models and none for the reactor store, so both use the same one. Add a `storage` to the backend under `reactor.backends`. Use the same Postgres URL to keep the data where it is. ### `ph init --uniform-runners` {/* #init-uniform-runners */} ```text --uniform-runners is no longer needed: `ph init` always scaffolds hosts/. The option will be removed in a future release. ``` Remove the option from the command. ## Check that you are done Start every host you use and read the first lines of output: ```bash ph run switchboard ph run connect dev ph run vetra ``` You are done when: - No line starts with `Warning:`. - `ph host check ` exits with code 0 for each host. - A search of your scripts, Dockerfile and CI finds no `ph vetra`, `ph connect`, `ph switchboard` or `ph reactor`. `ph connect config` is fine. - Your environment and deployment configuration set neither `FEATURE_UNIFORM_RUNNERS_ENABLED` nor `FEATURE_PLUGIN_HOST_ENABLED`. Run the check with the same environment as your deployment. Some warnings depend on environment variables that your own machine may not set. ## What happens in 6.4 if you do nothing 6.4 removes what 6.3 warns about: - A project without host files does not start. The error names `ph migrate`. - The classic boot path of `ph vetra`, `ph connect` and `ph switchboard` is gone. The three names stay as short forms of `ph run `, and `ph connect studio`, `ph connect build` and `ph connect preview` stay as short forms of the `dev`, `build` and `serve` verbs. A flag that has no `ph run` argument fails. - `ph reactor` is removed. - The `powerhouse.config.json` keys under [Settings](#settings) are errors. - `FEATURE_UNIFORM_RUNNERS_ENABLED=false` stops the start. `FEATURE_PLUGIN_HOST_ENABLED` is removed: the plugin host is the only path. - `reactor.workflows` and the `readModel.url` fallback in host files are removed. --- # Book of Powerhouse ## Overview > Source: https://academy.vetra.io/bookofpowerhouse/Overview Powerhouse is an initiative at the intersection of software development, legal innovation, and new business models for open-source projects. These elements form a common vision for reimagining how software is created, funded, and sustained. While each component has its own purpose, they are deeply interconnected and designed to support one another.
Book of Powerhouse Outline: 1. General Framework and Philosophy 2. Powerhouse Software Architecture 3. Technical Development Approaches 4. SNOs as an extension of DAOs 5. The Powerhouse Operations Platform --- ## **Part 1: Powerhouse General Framework and Open-Source Capitalism** > Source: https://academy.vetra.io/bookofpowerhouse/GeneralFrameworkAndPhilosophy Powerhouse emerged in the wake of Ethereum’s DAO movement, which began gaining momentum after the collapse of _The DAO_ in 2016. This turning point spurred the creation of blockchain-enabled organizations capable of operating without centralized corporate structures. Over the years, governance systems and decentralized operational tools have advanced, paving the way for the next generation of organizations across chains like Solana, Ethereum Layer 2 solutions, and beyond. Powerhouse aims to advance these principles and build large, global organizations around networks. Powerhouse operates at the intersection of open-source innovation and decentralized coordination. This section outlines the philosophical foundation and systemic structure that guides its operations and vision—what we call “Open-Source Capitalism.” ## **Why Open-Source Capitalism?** Capitalism is the most powerful system of economic coordination ever invented. It incentivizes productivity, innovation, and risk-taking. But just as important—open-source has proven to be one of the most powerful engines of innovation within that system. Open-source unlocks coordination across boundaries: between companies, communities, and individuals. It builds shared infrastructure at unprecedented speed, allows bottom-up experimentation, and lowers barriers to participation. This is why Big Tech, for all its closed platforms and walled gardens, has embraced open-source at the core of its own operations—releasing projects like React, Kubernetes, and TensorFlow not out of charity, but because open collaboration is often the best way to build foundational technology. Yet this dynamic is incomplete. While open-source has dominated infrastructure, it has consistently struggled to capture value—particularly in consumer applications and services. The common assumption is that the challenge is distribution: how to equitably allocate funding, resources, and decision-making. But Open-Source Capitalism takes a broader view. Before we can redistribute value, we have to make sure value is being captured in the first place. That’s the central promise of Open-Source Capitalism: not just to build open alternatives, but to make them self-sustaining, investable, and scalable. It’s about combining the pro-market, pro-innovation ethos of open-source with incentive structures that actually work—so we can build networks that don’t just survive, but thrive. ## **Four Principles of Open-Source Capitalism** 1. **Coordination Through Marketplace Platforms** Open-Source Capitalism demands new types of platforms—public marketplaces where contributors, customers, and investors can interact transparently. Like Uber or Airbnb, these marketplaces optimize for liquidity and coordination, but with open governance and ownership. \ 2. **Incentive Alignment via Tokens and Revenue Sharing** Powerhouse uses Proof of Work Tokens (POWTs) to compensate contributors fairly over time, enabling deferred compensation models that align long-term incentives. \ 3. **Reinvestment Through Revenue-Generating Hubs** Monetized OSS must funnel returns back to the contributors. The RGH (Revenue Generating Hub) manages licensing, sales, and commercial transactions in alignment with decentralized values. \ 4. **Make Open Source Investable** By enabling investor flows through the Operational Collateral Fund (OCF), open-source projects gain sustainable funding channels. Returns are tied to the downstream use of open infrastructure. \ ## **Decentralized Operations and Collaboration** Open-Source Capitalism doesn’t work without new organizational structures. DAOs promised a way forward, but fell short: plagued by coordination failures, incentive misalignment, and lack of clarity around roles, responsibilities, and execution. Powerhouse emerged from this gap—building systems that retain the openness of decentralized networks while introducing the operational rigor of traditional institutions. At the core of this approach is a belief in structured decentralization. Powerhouse enables independent teams to coordinate around shared goals, using workflows encoded in software rather than enforced by hierarchy. Contributors take ownership over workstreams, reputations are built over time, and budgets are allocated based on transparent processes. This is not decentralization for its own sake—it is a functional reimagining of how work gets done at scale. Transparency plays a critical role. Rather than relying on opaque decision-making or internal politics, Powerhouse makes contributions, decisions, and payments legible. Governance and compensation are traceable across time, creating a persistent and auditable record of organizational behavior. The result is a system that fosters trust not just between individuals, but across entire networks. This is what makes Powerhouse unique: it’s not just about running decentralized software—it’s about running decentralized organizations. Open-Source Capitalism is the economic philosophy. Scalable Network Organizations are the structure. And these principles come to life through the operational frameworks that guide every action on the network. --- ## Part 2: Powerhouse Software Architecture > Source: https://academy.vetra.io/bookofpowerhouse/PowerhouseSoftwareArchitecture Powerhouse’s software architecture is designed to empower scalable, decentralized organizations with a modular, layered approach. It uses document models as the core unit that used across three layers: - the **Data Infrastructure Layer**, which handles secure storage and synchronization across local, cloud, and decentralized systems; - the **Host Application Layer**, which provides contributor tools like Powerhouse Connect and Fusion; - and the Customization **Layer**, enabling bespoke solutions for specific use cases. ## Software Architecture ### Document Models - A **document model** is a structured representation of data and workflows that captures relationships, states, and operations within a system. By treating documents as dynamic entities that evolve over time, document models allow multiple people to contribute, make changes, and track progress in a flexible and transparent way. Document models are central to Powerhouse’s development approach, forming the backbone of adaptable workflows and versatile data management. In environments where contributors work asynchronously and systems must respond to evolving needs, document models provide the flexibility and responsiveness that traditional, static structures often lack. - Building on this foundation, Powerhouse’s document models are designed to go beyond static data storage, encapsulating the complex relationships, states, and operations that drive decentralized workflows. Each document functions as a living entity, continuously evolving to reflect updates and interactions among contributors. This allows for: - **Asynchronous Collaboration**: Contributors can work on documents without being bound by linear processes. For example, a task document can move through different states (e.g., draft, review, approved) based on specific triggers or events. - **Customizable Processes**: Document models are highly adaptable, supporting unique workflows for different teams or organizations. This flexibility ensures that systems can address specialized requirements while maintaining overall consistency and transparency. - **Event-Driven Updates**: By integrating with Powerhouse’s event-driven architecture, documents can respond to real-time changes, such as new data inputs or status updates, ensuring workflows remain efficient and responsive. ### Three layers of Powerhouse 1. **Data Infrastructure Layer -** This layer is responsible for data storage and synchronization, whether locally, in the cloud, or on decentralized storage networks like Ceramic or IPFS. It includes: - **Reactor**: A storage node for documents and files, supporting multiple storage adapters for various environments. - **Powerhouse Network Components**: services required to build scalable networks of Reactor nodes, such as event buses, queues, caching services, and load balancers. 2. **Host Application Layer - t**his layer provides the tools to build apps and platforms using modular host applications. Each host application is an empty shell that gains functionality through plugins. The key host applications include: - **Powerhouse Connect**: Tools for contributors to perform their roles with tailored plugins for document management. - **Powerhouse Switchboard**: A scalable API service for aggregating data into operational, analytical, or other specialized read models. - **Powerhouse Fusion**: Public-facing collaboration platforms or marketplaces for the SNO’s platform economy. - **Powerhouse Renown**: Decentralized authentication and reputation management for contributors. - **Powerhouse Academy**: Onboarding and training tools for contributors, offering tutorials and reputation badges. 3. **Customization Layer - t**his layer consists of organization-specific plugins and instances of the host applications. It enables customization and deployment of unique platforms for each network organization. Examples include: - **Connect Plugins**: Tailored apps for contributors. - **Switchboard Plugins**: Aggregated data and API services. - **Fusion Plugins**: Custom marketplaces and user-facing platforms. - **Renown and Academy Plugins**: Contributor reputation systems and training resources. --- ## Part 3: Development Approaches > Source: https://academy.vetra.io/bookofpowerhouse/DevelopmentApproaches Powerhouse’s development approaches are designed to enable efficient, scalable, and innovative solutions for decentralized organizations. By drawing from blockchain principles, adopting Model-Driven Development (MDD), leveraging dynamic document models, and employing a Rapid Application Development (RAD) process, Powerhouse provides a comprehensive framework to address the unique challenges of decentralized systems. These methodologies enhance collaboration, streamline workflows, and accelerate iteration cycles, empowering teams to design, implement, and adapt their solutions seamlessly while staying aligned with Powerhouse’s vision for sustainable and decentralized growth. ### Blockchain principles applied to decentralized operations - Powerhouse’s development approach draws inspiration from the principles of blockchain technology while adapting them to meet the specific needs of decentralized organizations. These principles heavily influence its architecture, blending blockchain’s strengths with practical adaptations for scalable operations. - Powerhouse shares several core ideas with blockchain: - **Immutability**: Like a blockchain, Powerhouse emphasizes immutable data records. Through event sourcing, every state change is preserved as an append-only event, ensuring transparent, auditable histories. - **Decentralization**: Both systems operate without relying on a centralized authority. Powerhouse’s Document Synchronization Protocol (DocSync) does not rely on a centralized server but can run P2P. It is also storage agnostic, so there is no lock-in. Users have the option to rely on decentralized, centralized or local storage solutions. - **Transparency**: Inspired by the visibility of blockchain transactions, Powerhouse ensures operational workflows and changes are traceable, which makes governance and accountability easier. - While Powerhouse builds on blockchain principles, it diverges in key ways to address operational challenges: - **Local Scalability**: Blockchain’s global consensus can limit speed and scalability. Powerhouse prioritizes local-first workflows, ensuring contributors can operate efficiently without requiring global synchronization. - **Tailored Operations**: Unlike blockchain’s general-purpose design, Powerhouse’s architecture focuses on organizational needs, supporting custom workflows, dynamic governance, and flexible scaling. ### Model-Driven Development & Rapid Application Development (RAD) - Powerhouse leverages **Model-Driven Development (MDD)** and **Rapid Application Development (RAD)** to streamline the creation of scalable, decentralized systems. - MDD enables cross-functional collaboration by using **abstract models** as blueprints for workflows, data, and system behaviors—similar to how GraphQL schemas unify API design. These models ensure alignment across teams, enable early validation, and support seamless system evolution. **Meta-models** extend this by automating code generation, documentation, and workflow updates, reducing manual effort and ensuring consistency. - RAD accelerates development by eliminating backend complexity and leveraging **automated code generation**. Pre-built frameworks and reusable components let developers focus on UI/UX without managing backend logic. An **event-driven architecture** handles state and data sync, while document models serve as a **source of truth**, ensuring APIs, data structures, and front-end scaffolding stay in sync. Developers can rapidly iterate on tailored user experiences—akin to “swapping skins” in a game—without disrupting core functionality. ### CQRS (Command Query Responsibility Segregation) - CQRS, or Command Query Responsibility Segregation, is a key design principle in Powerhouse’s software architecture. It separates the responsibilities of handling write operations (commands) and read operations (queries) into distinct models, optimizing both for their specific purposes. - In the Powerhouse framework: - **Commands** handle operations that modify the state, such as creating or updating documents. These commands are validated and stored as events, forming the immutable history central to the system’s transparency and auditability. - **Queries** are optimized for retrieving data, leveraging read models designed for performance, scalability, and flexibility. These models aggregate data from events to provide users with tailored insights, whether for analytics, search, or operational tasks. - This separation offers significant advantages: 1. **Scalability**: Write and read models can scale independently, allowing Powerhouse to support large decentralized organizations without performance bottlenecks. 2. **Maintainability**: By isolating business logic (commands) from query logic, developers can iterate on one without affecting the other, ensuring the system evolves efficiently. 3. **Flexibility**: Powerhouse supports multiple types of read models—such as relational databases, full-text search, and analytics—each tailored to specific organizational needs. ### Event-driven Architectures (EDA) - Event-Driven Architecture (EDA) is a foundational element of Powerhouse’s software philosophy, designed to create responsive, scalable systems that support asynchronous workflows. In EDA, events represent meaningful changes or actions, such as “Document Updated” or “Contributor Added,” and these events trigger reactions across the system in real time. - EDA enables Powerhouse to design systems that are inherently responsive and scalable. By decoupling components, EDA allows systems to react to events independently, ensuring high availability and performance even as complexity grows. For example, when a document is updated, different processes like validation, notifications, and analytics generation can run in parallel without blocking each other. This architecture makes Powerhouse systems highly efficient in handling distributed operations, as it allows them to scale dynamically by adding or replicating components where needed. It also improves fault tolerance, as failures in one part of the system do not cascade to others due to the loosely coupled design. - **Asynchronous Workflows -** EDA is particularly effective in supporting asynchronous workflows, which are critical for decentralized organizations. In this model, components communicate through events, eliminating the need for direct dependencies. This loose coupling enables workflows to operate flexibly, allowing different tasks to be executed simultaneously without blocking others. For instance, when a contributor submits work, separate processes—like task approval, logging, and analytics—can run independently, ensuring smoother coordination. Additionally, event histories can be replayed to debug workflows or audit past operations, providing transparency and traceability essential to decentralized systems. --- ## Part 4: Scalable Network Organizations (SNOs) > Source: https://academy.vetra.io/bookofpowerhouse/SNOsandANewModelForOSSandPublicGoods Decentralized Autonomous Organizations (DAOs) once promised to revolutionize global collaboration by enabling decentralized governance, transparent decision-making, and equitable ownership. However, the reality has often fallen short. DAOs have struggled with legal ambiguity, resource allocation inefficiencies, and broken incentive structures. These challenges have stifled their ability to scale and compete with centralized organizations. Scalable Network Organizations (SNOs) emerge as the next evolutionary step. Combining the principles of decentralization with the operational rigor of traditional organizations, SNOs provide a structured framework for decentralized governance, sustainable operations, and global scalability. Powerhouse’s vision for SNOs aims to deliver on the original promise of DAOs, empowering organizations to scale without sacrificing their decentralized principles. ### Core Components of SNOs[​](https://staging.powerhouse.academy/docs/bookofpowerhouse/SNOsandANewModelForOSSandPublicGoods#core-components-of-snos) At the heart of Scalable Network Organizations (SNOs) are five entities, each fulfilling a critical role in scaling decentralized operations. From governance to funding and IP management, these components work together to ensure alignment, collaboration, and financial sustainability. - DAO - The DAO is the governing entity responsible for setting the strategic vision and ensuring alignment across the SNO. Through on-chain governance mechanisms powered by smart contracts, the DAO facilitates transparent decision-making, distributed ownership, and accountability. Members participate in proposing and voting on budgets, initiatives, and key operational decisions, creating a decentralized system where authority is widely distributed rather than concentrated in a single entity. - By maintaining immutable records and eliminating the need for intermediaries, the DAO supports scalable and adaptable governance structures that suit decentralized networks. Its ability to flexibly adapt rules and processes ensures that the SNO remains efficient and innovative as it grows, while retaining the values of transparency and inclusivity. - Operational Hub (OH) - The Operational Hub acts as the administrative core of the SNO, handling contributor relationships, compliance, and payment processing. By leveraging legal structures like Swiss Associations, the OH ensures contributors are protected from liability while enabling the SNO to interact with banks, vendors, and regulatory entities. This hub simplifies complex operations such as tax reporting, cross-border compliance, and contributor agreements, removing these burdens from individual participants. - The Operational Hub acts as the administrative core of the SNO, handling contributor relationships, compliance, and payment processing. By leveraging legal structures like Swiss Associations, the OH ensures contributors are protected from liability while enabling the SNO to interact with banks, vendors, and regulatory entities. This hub simplifies complex operations such as tax reporting, cross-border compliance, and contributor agreements, removing these burdens from individual participants. - Operational Collateral Fund (OCF) - The OCF provides the financial infrastructure to fuel the SNO’s growth. It allocates resources to high-potential initiatives and rewards contributors by issuing Proof of Work Tokens (POWTs). These tokens align incentives by linking compensation to measurable contributions, ensuring that individuals and teams are motivated to create long-term value. POWTs represent a stake in the success of the network, making contributors invested in the outcomes of the projects they support. - Beyond its internal role, the OCF also attracts external investment by offering structured opportunities for funding impactful projects within the network. By maintaining transparency in its allocation processes and linking funding to measurable outputs, the OCF builds trust among both contributors and investors, creating a virtuous cycle where capital supports meaningful innovation. - Revenue Generating Hub (RGH) - The RGH is the SNO’s commercial interface, enabling the network to generate sustainable revenue while maintaining its decentralized principles. It manages licensing agreements, product sales, and customer-facing activities, ensuring that all revenue-generating operations align with the broader goals set by the DAO. By addressing the regulatory complexities associated with handling fiat revenue or contractual obligations, the RGH provides a seamless bridge between decentralized networks and traditional market systems. - The RGH supports multiple revenue streams, including enterprise licensing for proprietary versions of open-source software, subscription models for SaaS offerings, and consulting services tailored to client needs. A portion of the revenue collected flows back into the ecosystem, funding operations, rewarding contributors through the OCF, and sustaining the open-source projects that drive the SNO’s long-term success. - IP-Holding Entity (IPSPV) - The IP-Holding Entity safeguards the SNO’s intellectual property assets, including trademarks, copyrights, and other IP rights. It is responsible for enforcing licensing agreements and ensuring that the use of the network’s IP aligns with governance decisions made by the DAO. By employing a dual licensing model, the IPSPV supports open-source availability through copyleft licenses while generating revenue from enterprise licenses, allowing businesses to use proprietary versions of the SNO’s software. - The IPSPV also plays a critical role in protecting the network’s creative assets from exploitation or infringement. It ensures that the IP is managed as a collective resource, aligned with the community’s mission and values, while providing a steady revenue stream for reinvestment into the ecosystem. By separating IP management from operational activities, the IPSPV ensures that the SNO remains focused on innovation without losing control of its most valuable assets. ### Legal Foundations for SNOs[​](https://staging.powerhouse.academy/docs/bookofpowerhouse/SNOsandANewModelForOSSandPublicGoods#legal-foundations-for-snos) Legal structures are crucial for the success of SNOs, providing clarity, compliance, and protection. - Multisig Participation Agreements (MPAs) \ MPAs are a foundational legal tool within the SNO framework. These agreements define the roles and responsibilities of multisig wallet signers, ensuring accountability and mitigating internal liability risks. By formalizing governance processes, MPAs reduce the chaos and inefficiency often associated with decentralized decision-making. They also protect contributors by clarifying liability boundaries, preventing signers from inadvertently exposing themselves to legal risks. \ MPAs are fully customizable, allowing SNOs to adapt them to their specific operational needs. They integrate seamlessly with Gnosis Safe wallets, creating a transparent and secure environment for managing resources. - Swiss Associations \ Swiss Associations offer a flexible and cost-effective legal wrapper for early-stage SNOs. These entities provide liability protection, separate legal personhood, and the ability to engage in commercial activities that support the SNO’s mission. Notably, Swiss Associations do not require registration, preserving privacy while maintaining compliance. Their reputation as part of Switzerland’s crypto-friendly regulatory environment makes them ideal for DAOs transitioning into SNOs. These are typically used as legal structures for the OCR and IPSPV, while the OH and RGH may be Swiss foundations but their jurisdiction is likely determined by where inbound and outbound payments are taking place. - Open Source Legal Templates \ Powerhouse has developed a library of open-source legal templates to simplify the setup and operation of SNO entities. These templates include Contributor Agreements, MPAs, and licensing contracts, reducing the complexity and cost of navigating legal requirements. By standardizing these processes, Powerhouse enables SNOs to focus on innovation and growth. --- ## **Part 5: Powerhouse Platforms – Decentralized Operations and Builder** > Source: https://academy.vetra.io/bookofpowerhouse/SNOsInActionAndPlatformEconomies ## **Introduction** The Powerhouse architecture is not only organizational but also deeply technological. To enable scalable network organizations (SNOs) to operate effectively, Powerhouse has developed two core platforms that provide the infrastructure for decentralized coordination and execution: the **Decentralized Operations Platform** and the **Builder Platform**. These platforms are complementary: one structures and stabilizes daily operations; the other opens up participation and innovation. Together, they form the digital substrate of the Powerhouse model, encoding its governance logic, collaboration structures, and incentive mechanisms directly into software. --- ## **Decentralized Operations Platform** The Decentralized Operations Platform serves as the operational engine of a SNO. It provides the workflows, rules, and execution logic required for contributors to collaborate without a central management layer. This includes systems for compensation, budgeting, IP management, and contributor reputation. At its core, the platform acts as a programmable coordination system. Contributors are onboarded, assigned tasks, compensated, and recognized through transparent, rule-based processes encoded in smart contracts and synchronized document models. These workflows are not static: they evolve based on activity, inputs, and contributor feedback, adapting to the changing needs of the network. The Decentralized Operations Platform also embeds accountability. Actions taken on the platform generate verifiable records—both financial and reputational—that form a shared source of truth. Disputes can be resolved, work can be audited, and contributors can prove their track record across projects and teams. This persistent memory allows SNOs to grow while retaining coherence and trust. Strategically, the platform supports multiple service categories, including governance operations, legal and financial services, and compliance. Each of these is modular, and the marketplace of service providers enables networks to plug in what they need, when they need it. Revenue is structured around project-based transactions and reinforced by policies that encourage on-platform fulfillment, ensuring alignment across stakeholders. --- ## **Builder Platform** While the Decentralized Operations Platform governs execution, the Builder Platform governs creation. It is designed for extending the Powerhouse architecture—enabling developers and contributors to build new tools, workflows, and modules that others in the ecosystem can use. The Builder Platform represents a shift in how coordination infrastructure is developed. Instead of building monolithic applications, contributors define document types, schemas, automation rules, and interfaces as reusable modules. These are published to a shared registry, where they can be discovered, forked, extended, or monetized. Every time a module is used in another network’s deployment, its original authors receive a share of the value generated. In this way, Powerhouse makes open-source infrastructure economically sustainable and creates a new incentive model for public goods. Technically, the Builder Platform is integrated with the rest of the Powerhouse stack. Its outputs are interoperable with the Operations Platform, the Governance layer, and the contributor onboarding systems. It uses typed schemas, CLI scaffolding, and standardized packaging to ensure modules are composable and production-ready. Strategically, the platform ensures that innovation remains decentralized. No single team or organization controls what can or cannot be built. Any contributor with sufficient context and intent can extend the system—and be rewarded for doing so. This model transforms Powerhouse from a static platform into a living ecosystem. --- ## **A Unified Infrastructure Layer** Together, these platforms operationalize the vision of scalable, decentralized networks. The Operations Platform provides the scaffolding for work: roles, rules, payments, and projects. The Builder Platform enables the ecosystem to evolve: by building, sharing, and monetizing new capabilities. They are not products to be sold—they are foundational infrastructure for a new kind of organization. Powerhouse is not simply offering software; it is building the operating system for a post-corporate world. --- # Release Notes ## Powerhouse v5.3.0 🚀 > Source: https://academy.vetra.io/academy/ReleaseNotes/v5.3.0 ## ✨ Highlights 1. **Authentication & Permissions** - CLI authentication and document-level permissions 2. **Improved Code Generation** - TS Morph and templates for faster, more reliable generation 3. **Runtime Document Model Subgraphs** - No more generated subgraph code to manage This release focuses on authentication, permissions, improved code generation, and runtime document model subgraphs. ## 🔐 CLI Authentication New commands for authentication workflows: | Command | Description | | ----------------- | --------------------------------------------- | | `ph login` | Authenticate with your Powerhouse identity | | `ph access-token` | Generate access tokens for API authentication | The CLI now supports full authentication workflows for secure operations and programmatic API access. ## 🛡️ Document Permission Service Fine-grained access control at the document level for Switchboard. - **Operation Permissions** - Control who can perform specific operations - **Document Group Permissions** - Organize documents with shared access rules - **Feature Flag** - Enable/disable via configuration When enabled, all document operations are validated against permission rules. Enterprise-grade access control that integrates seamlessly with existing Switchboard deployments. ## 📊 Autogenerated Document Model Subgraphs Subgraphs are now **automatically available** on Switchboard at runtime. ### ⚠️ Action Required Previously generated subgraphs should be **deleted** to avoid conflicts. This eliminates manual subgraph generation and maintenance. Clean up old generated files to prevent runtime conflicts. ## ⚛️ New React Hooks `useGetDocument` and `useGetDocuments` ```typescript const getDocument = useGetDocument(); const onDocumentSelected = async (id: string) => { const document = await getDocument(id); // Fetch on demand }; ``` ## ⚛️ Dispatch Callbacks Optional callbacks to handle results of dispatched actions. ```typescript const [document, dispatch] = useDocumentById(documentId); dispatch( myAction, (errors) => { // Handle errors (e.g., show toast notification) alert(errors); }, (document) => { // Handle success console.log("Document updated:", document); }, ); ``` Useful for showing toast notifications or triggering follow-up actions based on dispatch results. ## ⚛️ React Hooks Bug Fixes - Hooks returning multiple documents now correctly update when any document changes - Improved React Suspense integration to avoid unnecessary loading states Thank you **Liberuum**! These improvements make working with documents more reliable and flexible. ## 🎨 Document Model Editor Improvements Enhanced state editing experience in the Document Model Editor. The Document Model Editor now provides a better experience for editing document state schemas with improved validation and feedback. ## 🛠️ Improved Code Generation Faster, more reliable code generation with **TS Morph** and **templates**. - Templates are easier to maintain and customize - More predictable output - Better error messages The new template-based approach makes codegen more maintainable and gives developers clearer error messages when something goes wrong. ## 📝 Updated Editor Boilerplate Better starting point for custom editors with improved default styling. The new boilerplate includes DocumentToolbar integration, proper state management with hooks, and cleaner default styles. ## 🔄 Migration Steps 1. **Run migrations** - `ph migrate` 2. **Update editor styles** - Editors now control their own padding 3. **Delete document model subgraphs** - Remove generated subgraphs to avoid conflicts 4. **Update config files** - Add `vitest.config.ts` to `tsconfig.json` exclude and ESLint's `allowDefaultProject` Follow these steps carefully for a smooth upgrade to v5.3.0. ## 📚 Documentation Coming soon to **https://academy.vetra.io** - Reactor API Authorization - Document Permission System - Inspector Modal guide - Updated hooks documentation - Vetra Studio usage guides --- ## Powerhouse v6.0.0 🚀 > Source: https://academy.vetra.io/academy/ReleaseNotes/v6.0.0 ## ✨ Highlights 1. **Redesigned Reactor with sync reliability** — Complete rewrite of the write model, sync pipeline, and job system with quarantine, dead letters, paging, and FIFO batching 2. **Vetra Package Ecosystem** — Private npm-compatible registry, `ph publish/install/unpublish`, and dynamic package loading at runtime in Connect and Switchboard 3. **Reactor Attachments** — New `reactor-attachments` package for document-linked file storage with reservations, direct upload, and soft-delete 4. **Switchboard Load Balancer** — Built-in `switchboard-lb` for multi-node deployments with least-conn routing and drive-id-based pinning 5. **Full Observability** — OpenTelemetry metrics, Sentry source map uploads, Prometheus exporter, and OTel-to-Sentry span bridging ## ⚙️ Reactor — Rewritten Write Model & Sync Engine The reactor's core write model was fundamentally rearchitected to unify transactions with execution context. The sync pipeline gained per-document quarantine (instead of blocking entire remotes), a persistent dead-letter store, buffered batched mailboxes, abort signals on GQL channels, and cursor-based paging for large sync batches. Key additions: - **DocumentIntegrityService** — validates and rebuilds document keyframes/snapshots - **Sync paging** — `getOperations` now pages across scopes - **Job timeout** via abort signals propagated through the entire job pipeline - **Yield utilities** for cooperative scheduling in the job executor - **Exponential backoff + jitter** on retries - **`test-sync-queue` CLI app** for detecting sync drift on large drives ```bash # New profiling script for direct reactor performance testing npx tsx profiling/reactor-direct.ts --docs 1000 --otel ``` ## 📦 Vetra Package Ecosystem A full end-to-end package ecosystem now powers Powerhouse apps. A private npm-compatible registry (Verdaccio-backed) hosts packages, and the CLI, Connect, and Switchboard all integrate with it. **New CLI commands:** ```bash # Publish a built package to the Powerhouse registry ph publish # Install from the registry (uses Powerhouse registry by default) ph install @your-org/your-package # Unpublish a package and purge the CDN cache ph unpublish @your-org/your-package@1.0.0 ``` **Dynamic loading:** Connect and Switchboard load processors and subgraphs at runtime from the HTTP registry — no rebuild required when new packages are published. **Auto-discovery:** Connect automatically finds and installs the correct package when it encounters an unknown document type. **Version picker:** The Connect UI lets users choose specific dist-tag versions when installing packages. ```json // powerhouse.config.json { "registry": "https://your-registry-url" } ``` ## 📎 Reactor Attachments New `reactor-attachments` package provides document-linked file storage, integrated directly into the reactor and switchboard. Features: - Upload reservations + direct upload to storage - Switchboard transport implementation - `HEAD` support, soft-delete, and case-insensitive hash indexing - `Attachment-Metadata` headers ## ⚖️ Switchboard Load Balancer New `switchboard-lb` package provides HTTP load balancing for multi-node Switchboard deployments: - **M1:** Least-connections proxying of upstream routes - **M2:** Request pinning across restarts - **M3:** Rewrite to drive-id header for deterministic routing ## 📊 OpenTelemetry & Sentry Observability ```bash # Enable OTel metrics export from Switchboard OTEL_EXPORTER_OTLP_ENDPOINT=http://tempo:4318 ph switchboard start # Prometheus metrics (opt-in) PROMETHEUS_METRICS=true ph switchboard start ``` - New `opentelemetry-instrumentation-reactor` package for distributed tracing across reactor internals - Sentry source maps uploaded in CI and releases tagged automatically - OTel spans bridged to Sentry for unified error correlation - Opt-out error reporting for `ph-cli` and `ph-cmd` via config - Lightweight Sentry SDK replaces full SDK in CLI ## 🪝 `useDocumentSafe` Hook ```typescript function MyEditor({ documentId }: { documentId: string }) { const { document, error, isLoading } = useDocumentSafe(documentId); if (isLoading) return ; if (error) return ; return ; } ``` Replaces the pattern of calling `useDocument` and catching thrown errors — now surfaces error and loading state as first-class values. ## 🤖 `ph code` Subcommand — AI Agent Harness ```bash # Launch an AI-assisted code generation session ph code ``` New subcommand that wires Mastra and ph-clint into the CLI for AI-powered code assistance scoped to your Powerhouse project. ## 🛡️ Per-Document Authentication - **Signature verification** on Switchboard: operations must carry a valid app-key signature - **Per-document protection model**: configure which documents require authentication - `ph login` / `ph logout` / `ph access-token` login flow now implemented in the Renown SDK and forwarded by the CLI ## 🔧 Codegen Improvements - **Versioned reducers by default** — reducers are version-stamped at generation time - **`satisfies DocumentModelModule`** instead of type casts - **CI/CD + Docker templates** scaffolded by `ph init` for new projects - **Vitest coverage** scaffolded with reducer threshold checks - **Separate `ph generate` commands** for reducers, editors, and processors - **AGENTS.md template** with editor drag-and-drop guidance and reducer testing playbook - **E2E codegen tests** for processor and subgraph generation ## 🖥️ Connect Improvements - **Processor inspector** panel showing live processor state - **Offline preview** for locally installed packages (bundled into Connect) - **PGlite migration banner** for DB version transitions - **Dump & import** for local PGlite database - **Version picker** UI for dist-tag selection - **Git hash display** in settings/URL for build traceability - **Drive info** shown in settings menu - **JSON viewer** in operations tooltip - **Retry on startup** for default drives with backoff ## 📡 Reactor MCP The `reactor-mcp` package now creates a fresh `McpServer` per `/mcp` request, fixing concurrent transport collision when multiple clients connect simultaneously. ## 🔄 Improvements - **tsdown** replaces the previous build toolchain across all packages: `analytics-engine`, `builder-tools`, `ph-cli`, `ph-cmd`, `registry`, `shared`, `design-system` - **Separate node/browser processor bundles** — processors now publish distinct `node` and `browser` entrypoints - **Tree-shaking** — `sideEffects: false` added to generated package boilerplate; pglite, jszip, and renown crypto are now lazy-loaded - **Document cache** made compatible with GraphQL clients directly - **Remote document controller** in `reactor-browser` for server-driven document state - **Single batch query** in `reactor-browser` for pulling operations on remote controllers - **Switchboard**: dynamic model loading behind `DYNAMIC_MODEL_LOADING` env var; OTel provider registration ordering enforced - **Registry**: SSE + webhook publish notifications; npm uplink for transparent CDN fallback; Renown JWT auth in front of Verdaccio - **`ph-cmd`** now delegates `init` and all forwarded commands to the versioned `ph-cli` binary — version pinning works correctly - **`ph migrate`** now runs against the target codegen version with `--force` flag available - **`ph install`**: `--allow-build` flag supported; prompts for dist-tag on prerelease publish ## 🐞 Bug Fixes - Reactor: orphan reshuffle and cross-batch FIFO bugs fixed; documents with out-of-order `ADD_RELATIONSHIP` now correctly backfilled - Reactor: deleted documents no longer returned by queries; jobs targeting deleted documents are not retried - Reactor: GQL channel now correctly handles abort signals, backpressure, and dead-letter placement - Connect: PGlite idb cleared on storage wipe to avoid flush race; duplicate document models deduplicated by type+version - Switchboard: falls back to a free port on `EADDRINUSE` and propagates to Vetra - Switchboard: `migrate` command now honors correct env vars - Registry: concurrent tarball extraction prevented; correct package version resolved on CDN - ph-cli: lazy-loaded for faster startup; `PH_REGISTRY_URL` env takes precedence over config - Design system: static asset paths fixed post-tsdown migration; icons path corrected ## ⚠️ Breaking Changes **Node.js 24 is now the minimum supported version.** ```bash # Check your Node version node --version # must be >= 24.0.0 # Install Node 24 via nvm nvm install 24 && nvm use 24 ``` **`DriveEditor` renamed** — a bulk rename was applied across all packages. Search your codebase for old `DriveEditor` references and update as directed by TypeScript. **Generated subgraph code should be deleted** — subgraphs are now generated and loaded at runtime. Delete any `src/subgraphs/` directory in your project to avoid conflicts. ```bash rm -rf src/subgraphs/ ``` --- ## Powerhouse v6.3.0 > Source: https://academy.vetra.io/academy/ReleaseNotes/v6.3.0 6.3.0 makes hosts and `ph run` the way to run a project. Nothing is removed in this release. What 6.4 removes prints a warning, and each warning links to the [migration page](/academy/Reference/HostsAndRunners/MigratingToHosts). ## What is new ### Hosts and `ph run` are always on A project describes every way it can run in its `hosts/` folder. Each subfolder holds one host file: ``` hosts/ connect/host.js ph run connect switchboard/host.js ph run switchboard vetra/host.js ph run vetra desktop/host.js ph run desktop assistant/host.js ph run assistant cli/host.js ph run cli ``` ```bash ph run vetra ph run connect dev ph run switchboard ``` - `ph run [verb]` starts a host. The verb defaults to `run`. - `ph init` always writes the `hosts/` folder. The `--uniform-runners` option is no longer needed. - `ph ` is short for `ph run ` when no built-in command has that name. - The `FEATURE_UNIFORM_RUNNERS_ENABLED` variable has no effect any more. See [Hosts, Runners & Plugins](/academy/Reference/HostsAndRunners/HostsRunnersAndPlugins). ### Tools for host files ```bash ph host check switchboard ph host export switchboard --out switchboard.json ph generate hosts ph run switchboard --explain drives ``` - `ph host check ` loads and validates a host the way `ph run` would, without starting it. It exits with code 1 on any issue. - `ph host export ` prints a host as declared, as JSON. `--out` writes it to a file. - `ph generate hosts` writes `outputs.js` for every host whose runner declares outputs, and refreshes `hosts/types.d.ts`. Run it again after you change a host that other hosts read from. - `ph run --explain drives` prints the drive plan of a host instead of starting it. `--explain outputs` prints what each host produces and the start order. ### `ph migrate` adds host files and has a dry run ```bash ph migrate 6.3.0 --dry-run ph migrate 6.3.0 ``` - `ph migrate` adds the `hosts/` folder to a project that has none. The switchboard host lists the plugins the classic Switchboard ran. - `--dry-run` prints a diff of every file the migration would add, change or delete, and writes nothing. - `ph migrate` works from any folder inside the project. - `ph migrate` keeps the `package.json` scripts you changed yourself and updates the ones an earlier `ph init` wrote. - `ph migrate` moves `studio.host`, `studio.openBrowser` and `reactor.https` from `powerhouse.config.json` into the host files it adds, and prints what it did with each key. - `ph migrate` no longer overwrites the root files of the project, such as the Dockerfile and the CI workflows. It adds the ones the project lacks and prints a line for a kept file the current template has moved on from. ### A store records its migration release A store now records the last release that completed its one-time migrations. A later release that drops a migration uses this record to refuse a store that still needs it, and names the release to run first. 6.3.0 refuses no store. ### GraphQL clients send the new argument names The clients in this release send `idOrSlug`, `documentIdOrSlug`, `parentIdOrSlug`, `sourceIdOrSlug` and `targetIdOrSlug`. They need a Switchboard on 6.2.3-dev.35 or later. Upgrade the Switchboard before its clients. ## What warns These keep working in 6.3. Each prints a warning once per command. The [migration page](/academy/Reference/HostsAndRunners/MigratingToHosts) lists every warning and what to do about it. | What | Use instead | | ------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- | | `ph vetra`, `ph connect`, `ph switchboard` in a project without host files | `ph migrate`, then `ph run ` | | `ph connect`, `ph switchboard` in a project with host files | `ph run connect dev`, `ph run switchboard` | | `ph reactor` | `ph run switchboard` | | `ph switchboard --migrate`, `--migrate-status`, `--reset`, `--dev` | `ph run switchboard migrate`, `migrate-status`, `reset`, `dev` | | `FEATURE_PLUGIN_HOST_ENABLED=false` | Remove the variable | | `FEATURE_UNIFORM_RUNNERS_ENABLED=false` | Remove the variable | | `studio.host`, `studio.https`, `studio.openBrowser`, `reactor.https`, `reactor.storage`, `switchboard.database.url` in `powerhouse.config.json` | The host files | | `connect.drives.allowAddDrive`, `connect.drives.sections`, `connect.drives.icons`, `connect.home.background` at the top level | The generic Connect plugin's `config` in `hosts/connect/host.js` | | `reactor.workflows` in a host file | The workflows plugin under `runner.plugins` | | A host file where `readModel.url` also holds the reactor store | An explicit `storage` on the backend | Deprecated in code, without a link to the migration page: - The write methods of the `IReactorClient` facade log one warning per process. Use the write half of the client instead. Set `PH_SILENCE_DEPRECATIONS` to silence these warnings. - `reactorClient` in subgraphs is marked `@deprecated`. Use `ctx.clients.read` and `ctx.clients.write`. - The GraphQL argument names `identifier`, `documentId`, `documentIdentifier`, `parentIdentifier`, `sourceIdentifier` and `targetIdentifier` are marked `@deprecated` in the schema. Use the names listed above. ## What goes in 6.4 6.4 is a breaking release. It removes what 6.3 warns about: - A project without host files does not start. The error names `ph migrate`. - The classic boot path of `ph vetra`, `ph connect` and `ph switchboard`. The three names stay as short forms of `ph run `. - `ph reactor`. - The `powerhouse.config.json` keys in the table above become errors. - `FEATURE_UNIFORM_RUNNERS_ENABLED=false` stops the start. - `FEATURE_PLUGIN_HOST_ENABLED`: the plugin host is the only path. - `reactor.workflows` and the `readModel.url` fallback in host files. These stay in 6.4: `MCP_ENABLED`, `PH_WORKFLOWS_ENABLED`, `DOCUMENT_MODEL_SUBGRAPHS_ENABLED`, `DATABASE_URL`, `PORT` and `PH_SWITCHBOARD_PORT`. ## What goes in 6.5 Not before 6.5: - The `IReactorClient` facade with its deprecated write methods, and `reactorClient` in subgraphs. - The old GraphQL argument names. ## How to upgrade 1. Upgrade the Switchboard before the clients that talk to it. 2. Preview and run the migration in each project: ```bash ph migrate 6.3.0 --dry-run ph migrate 6.3.0 ``` 3. Review `hosts/switchboard/host.js`. Read the lines `ph migrate` printed under "Settings of powerhouse.config.json the host files take", and move the keys it kept into the host files by hand. 4. Replace the classic commands in your scripts, Dockerfile and CI with `ph run `. 5. Start every host and check that no line starts with `Warning:`. Go to 6.4 only after 6.3 runs without warnings. The [migration page](/academy/Reference/HostsAndRunners/MigratingToHosts) has the full steps. A project that still has document models on the 5.x layout runs the 6.2.2 migration first: ```bash npx @powerhousedao/ph-cli@6.2.2 migrate 6.2.2 ``` ## Version numbers From this release a version reads `GENERATION.BREAKING.RELEASE`. The second position may break; the third never does. Pin exact versions or use a tilde range such as `~6.3.0`. A caret range such as `^6.3.0` accepts 6.4.0 and with it the break. --- # Miscellaneous ## Home > Source: https://academy.vetra.io/ --- ## Prerequisites > Source: https://academy.vetra.io/academy/Build/GettingStartedBuilding/Prerequisites **TIP:** This **Build** chapter is the **manual experience** — you build Powerhouse packages by hand on your own machine with the Powerhouse CLI, controlling every step (document models, editors, processors, and publishing). If you'd rather have an AI agent build for you, use the **AI-guided experience** in [**Vetra Studio**](/academy/GetStarted/VetraStudio): you describe what you want in chat and the agent writes the specs and code for you — no local setup required. The two paths produce the same kind of package, so you can mix and match: prototype with the agent, then drop down to the manual workflow for fine-grained control. The rest of this section sets up your machine for the manual path and walks you through the end-to-end package workflow. It covers Node.js 24, VS Code, Git, and the Powerhouse CLI. **INFO:** If you've already set up **Git, Node.js 24, and a package manager (pnpm or npm)**, your most important step is to install the **Powerhouse CLI** with the command: `pnpm install -g ph-cmd` or `npm install -g ph-cmd`. A global install is recommended if you want to use the command from any directory as a power user. The Powerhouse CLI is used to create, build, and run your Document Models and gives you direct access to a series of Powerhouse Builder Tools. Move to the end of this page to [verify your installation.](#verify-installation) --- ## Overview Before building your Document Model, install some software on your machine. You need three tools: - Node.js 24, which runs your code. - Visual Studio Code (VS Code), where you write your code. - Git, which manages your code. Follow the steps below based on your computer's operating system. ### Windows Users: Consider Using WSL If you're on Windows, we recommend using **Windows Subsystem for Linux (WSL)** for the best development experience. WSL lets you run a full Linux environment directly on Windows without the overhead of a virtual machine. This gives you access to Linux command-line tools and utilities, which are often preferred for modern web development workflows. **To install WSL:** 1. Open PowerShell as Administrator (right-click and select "Run as administrator") 2. Run the following command: ```powershell wsl --install ``` 3. Restart your computer when prompted 4. After restart, a terminal will open asking you to create a Linux username and password Once WSL is set up, you can follow the **Linux (Ubuntu/Debian)** instructions below for installing Node.js, Git, and other tools. Your Linux environment will be accessible through the Windows Terminal or by typing `wsl` in PowerShell. **TIP:** Using WSL provides a consistent development experience that matches most production environments and online tutorials. You can still use VS Code on Windows — it integrates with WSL through the [Remote - WSL extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl). For more details, see the official [WSL installation guide](https://learn.microsoft.com/en-us/windows/wsl/install). --- ### Installing Node.js 24 Node.js 24 runs your application. Install it with the steps below. #### For Windows: 1. **Set up PowerShell for running commands:** - Press the Windows key - Type "PowerShell" - Right-click on "Windows PowerShell" and select "Run as administrator" - In the PowerShell window, type this command and press Enter: ```powershell Set-ExecutionPolicy RemoteSigned -Scope CurrentUser ``` - Type 'A' when prompted to confirm - You can now close this window and open PowerShell normally for the remaining steps 2. **Install Node.js 24:** - Visit the [Node.js official website](https://nodejs.org/) - Click the big green button that says "LTS" (this means Long Term Support - it's the most stable version) - Once the installer downloads, double-click it to start installation - Click "Next" through the installation wizard, leaving all settings at their defaults 3. **Install a package manager (pnpm or npm):** - Open PowerShell (no need for admin mode) - For pnpm (recommended), type this command and press Enter: ```powershell npm install -g pnpm ``` - Note: Node.js comes with npm by default, so npm is already available after installing Node.js 4. **Verify Installation:** - Open PowerShell (no need for admin mode) - Type these commands one at a time and press Enter after each: ```powershell node --version pnpm --version # or npm --version ``` - Version numbers should appear after each command (e.g., v24.x.x for Node.js). If they do, Node.js and your package manager are installed. > **Note**: If Node.js commands don't work in VS Code, restart VS Code to refresh environment variables. #### For macOS: 1. **Install Homebrew:** - Open Terminal (press Command + Space and type "Terminal") - Copy and paste this command into Terminal and press Enter: ```bash /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` - Follow any additional instructions that appear 2. **Install Node.js 24:** - In the same Terminal window, type this command and press Enter: ```bash brew install node@24 ``` - Then, optionally install pnpm (npm comes with Node.js): ```bash brew install pnpm ``` 3. **Verify Installation:** - In Terminal, type these commands one at a time and press Enter after each: ```bash node --version pnpm --version # or npm --version ``` - If version numbers appear, Node.js and your package manager are installed. #### For Linux (Ubuntu/Debian): 1. **Open Terminal:** - Press Ctrl + Alt + T on your keyboard, or - Click the Activities button and type "Terminal" 2. **Update Package List:** ```bash sudo apt update ``` 3. **Install Node.js 24 and optionally pnpm:** ```bash sudo apt install nodejs # Optionally install pnpm (npm comes with Node.js) sudo apt install pnpm ``` 4. **Verify Installation:** - Type these commands one at a time and press Enter after each: ```bash node --version pnpm --version # or npm --version ``` - If version numbers appear, your installation is complete. ### Installing Visual Studio Code VS Code is the editor we'll use to write our code. Here's how to install it: #### For Windows: 1. Visit the [Visual Studio Code website](https://code.visualstudio.com/) 2. Click the blue "Download for Windows" button 3. Once the installer downloads, double-click it 4. Accept the license agreement and click "Next" 5. Leave the default installation location and click "Next" 6. In the Select Additional Tasks window, make sure "Add to PATH" is checked 7. Click "Next" and then "Install" 8. When installation is complete, click "Finish" #### For macOS: 1. Visit the [Visual Studio Code website](https://code.visualstudio.com/) 2. Click the blue "Download for Mac" button 3. Once the .zip file downloads, double-click it to extract 4. Drag Visual Studio Code.app to the Applications folder 5. Double-click the app to launch it 6. To make VS Code available in your terminal: - Open VS Code - Press Command + Shift + P - Type "shell command" and select "Shell Command: Install 'code' command in PATH" #### For Linux (Ubuntu/Debian): 1. Open Terminal (Ctrl + Alt + T) 2. First, update the packages list: ```bash sudo apt update ``` 3. Install the dependencies needed to add Microsoft's repository: ```bash sudo apt install software-properties-common apt-transport-https wget ``` 4. Import Microsoft's GPG key: ```bash wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add - ``` 5. Add the VS Code repository: ```bash sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" ``` 6. Install VS Code: ```bash sudo apt install code ``` 7. Once installed, you can launch VS Code by: - Typing `code` in the terminal, or - Finding it in your Applications menu ### Install Git #### For Windows 1. Open PowerShell (press Windows key, type "PowerShell", and press Enter) 2. Visit the [Git website](https://git-scm.com/) 3. Download the latest version for Windows 4. Run the installer and use the recommended settings 5. Verify installation by opening PowerShell: ```powershell git --version ``` #### For macOS 1. Install using Homebrew: ```bash brew install git ``` 2. Verify installation: ```bash git --version ``` #### For Linux (Ubuntu/Debian) 1. Update package list: ```bash sudo apt update ``` 2. Install Git: ```bash sudo apt install git ``` 3. Verify installation: ```bash git --version ``` ### Configure Git (All Systems) After installation, set up your identity: ```bash git config --global user.name "Your Name" git config --global user.email "your.email@example.com" ``` ### Install the Powerhouse CLI The Powerhouse CLI (installed via the `ph-cmd` package) is a command-line interface tool. It provides the `ph` command, which is essential for managing Powerhouse projects. You can get access to the Powerhouse Ecosystem tools by installing them globally using: ```bash pnpm install -g ph-cmd ``` Or if you're using npm: ```bash npm install -g ph-cmd ``` Key commands include: - `ph init` to start a new project and build a document model - `ph run vetra` for starting Vetra Studio with its local Switchboard - `ph run connect dev` for running the Connect application locally - `ph run switchboard` for starting the API service - `ph help` to get an overview of all the available commands `ph run ` starts a host from the `hosts/` folder of your project. The classic commands `ph vetra`, `ph connect`, `ph switchboard` and `ph reactor` are deprecated: see [Moving to hosts and `ph run`](/academy/Reference/HostsAndRunners/MigratingToHosts). Use it to create, build, and run Document Models.
How to use different branches? When installing or using the Powerhouse CLI commands you can use the dev & staging branches. These branches contain more experimental features than the latest stable release the PH CLI uses by default. They can be used to get access to a bug fix or features under development. | Command | Description | | ---------------------------------- | ------------------------------------------------- | | **pnpm install -g ph-cmd** | Install latest stable version | | **pnpm install -g ph-cmd@dev** | Install development version | | **pnpm install -g ph-cmd@staging** | Install staging version | | **ph init** | Use latest stable version of the boilerplate | | **ph init --dev** | Use development version of the boilerplate | | **ph init --staging** | Use staging version of the boilerplate | | **ph use latest** | Switch all dependencies to latest stable versions | | **ph use dev** | Switch all dependencies to development versions | | **ph use staging** | Switch all dependencies to staging versions | Please be aware that these versions can contain bugs and experimental features that aren't fully tested.
Managing ph-cmd Versions and Package Information ### Switching Between Versions To change to a different version of `ph-cmd`, reinstall it globally with your package manager: ```bash # Install latest version with a specific tag npm install -g ph-cmd@staging pnpm install -g ph-cmd@dev # Install a specific version number npm install -g ph-cmd@1.2.3-staging.10 pnpm install -g ph-cmd@6.0.0-dev.33 ``` **Important:** Always use the same package manager you used for the original global install to avoid conflicting installations. ### Checking Your Installation Use the `which` command to see where your global install is located: ```bash which ph # Example output: /Users/username/Library/pnpm/ph ``` This shows which package manager was used (in this case, pnpm). ### Viewing Available Versions Use `npm view` to see all available versions and tags: ```bash npm view ph-cmd ``` **Example dist-tags output:** ``` dist-tags: latest: 5.3.0 dev: 6.0.0-dev.33 staging: 5.3.0-staging.24 test: 2.5.0-test.0 ``` ### Best Practices - Use specific version numbers instead of tags when you need exact version consistency - Check `npm view ph-cmd` before switching to see the latest available versions - Remember that without specifying a version, `@latest` is installed by default
### Verify Installation Open your terminal (command prompt) and run the following commands to verify your setup: ```bash node --version pnpm --version git --version ph --version ``` You should see version numbers displayed for all commands, similar to the example output below (your versions might be higher). The output for `ph --version` includes its version and may also show additional messages if further setup like `ph setup-globals` is needed. You're now ready to start building your first Document Model! ```bash % node --version v24.13.0 % pnpm --version 10.10.0 % git --version git version 2.39.3 % ph --version PH CMD version: 0.43.18 ------------------------------------- PH CLI is not available, please run `ph setup-globals` to generate the default global project ``` ## Up next Your machine is ready. Next, set up your [Vetra Drive](/academy/Build/GettingStartedBuilding/VetraDrive) — the workspace where you'll manage document model specifications, editors, and data integrations. --- ## Vetra Drive > Source: https://academy.vetra.io/academy/Build/GettingStartedBuilding/VetraDrive ## Introducing Vetra Drive for Builders Vetra Studio is the builder environment where you create, manage, and collaborate on Powerhouse packages. It consists of two main components: - **Vetra Studio Drive**: Serves as a hub for developers to access, manage & share specifications through a remote Vetra drive. It functions as the orchestration hub where you as a builder assemble all the necessary specifications for your intended use-case, software solution, or package. Each specification document corresponds to a **module** — a distinct building block of your package (such as a document model, editor, or data integration). - **Vetra Package Library**: Store, publish, and fork git repositories of packages in the Vetra Package Library. Visit the [Vetra Package Library here](https://vetra.io/packages) **INFO:** What is a Specification Document? A **specification document** is a configuration file that defines how a specific module in your package should behave. Think of it as a blueprint: it describes the structure, rules, and relationships that Powerhouse uses to generate the actual code for that module. Specification documents make **Specification-Driven Design & Development** possible: you communicate your solution and intent through a structured framework designed for AI collaboration. Specs are a shared language that enables precise, iterative edits. As Vetra Studio matures, each of these specification documents will offer an interface by which you as a builder get more control over the modules that make up your package. For now, the specification documents offer you a template for code generation.
Modules
The list of available modules color coded according to the 3 categories.
### Module Categories ### 1. Document Models A **document model** is a structured data type that defines what information your application can store and how it can be modified. Unlike traditional databases, document models use **operations** (actions like "add item" or "update title") rather than direct data manipulation, making them ideal for collaborative and auditable applications. - **Document model specification**: Defines the structure and operations of a document model using [GraphQL SDL](https://graphql.org/learn/schema/) (Schema Definition Language), ensuring consistent data management and processing. → [Learn more about Document Models](/academy/Build/DocumentModelCreation/WhatIsADocumentModel) ### 2. User Experiences - **Editor specification**: Outlines the interface and functionalities of a document model editor, allowing users to interact with and modify document data. - **Drive-app specification**: Specifies the UI and interactions for managing documents within a drive, providing tailored views and functionalities. → [Learn more about Building Document Editors](/academy/Build/BuildingUserExperiences/BuildingDocumentEditors) → [Learn more about Building a Drive Explorer](/academy/Build/BuildingUserExperiences/BuildingADriveExplorer) ### 3. Data Integrations - **Subgraph specification**: Details the connections and relationships within a subgraph (a subset of your data exposed via a GraphQL API), facilitating efficient data querying and manipulation. - **Codegen Processor Specification**: Describes the process for automatically generating code from document model specifications, ensuring alignment with intended architecture. - **RelationalDb Processor Specification**: Defines how relational databases are structured and queried, supporting efficient data management and retrieval. → [Learn more about Using Subgraphs](/academy/Build/WorkWithData/UsingSubgraphs) → [Learn more about Relational DB Processor](/academy/Build/WorkWithData/RelationalDbProcessor)
Vetra Studio Drive
The Vetra Studio Drive, a builder app that collects all of the specifications of a package.
### Configure a Vetra Drive in Your Project You can connect to a remote Vetra drive instead of using the local one auto-generated when you run `ph run vetra` (where `ph` is short for "powerhouse", the CLI tool and the Organization behind Vetra). - **Without** the `--remote-drive` option: Vetra will create a local drive for you that lives in your browser's local storage. This is useful for solo development or experimentation. - **With** the `--remote-drive` argument: Vetra will connect to a remote drive instead of creating a local one. The remote drive can be hosted wherever you want (e.g., on your own server or a shared team environment). The Powerhouse config includes a Vetra URL for consistent project configuration across different environments. ```typescript vetra: { driveId: string; driveUrl: string; } ``` Imagine you are a builder and want to work on, or continue with a set of specifications from your teammates. You could then add the specific remote Vetra drive to your Powerhouse configuration in the `powerhouse.config.json` file to get going: ```json "vetra": { "driveId": "bai-specifications", "driveUrl": "https://switchboard.staging.vetra.io/d/bai-specifications" } ``` An example of a builder team building on the Powerhouse Vetra Ecosystem and its complementary Vetra Studio Drive specifications for the different packages can be found [here](https://vetra.io/builders/bai). ### Connect Claude to the Reactor MCP Claude can connect directly to your project's Reactor via the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/), giving it live access to your drives, documents, and document model operations. `ph init` writes the configuration into the project: `.mcp.json` for Claude Code, `.cursor/mcp.json` for Cursor and `.gemini/settings.json` for Gemini CLI. The `.mcp.json` it writes is: ```json { "mcpServers": { "reactor-mcp": { "type": "stdio", "command": "node", "args": ["--run", "mcp"] } } } ``` `ph mcp` serves the reactor-mcp tools over stdio from the project's own `ph-cli`. `node --run mcp` runs the project's `mcp` script (`ph-cli mcp`), which works on every platform. On the first tool call it connects to the project's running Vetra, or starts `ph vetra --watch` in the background and writes its output to `.ph/vetra.log`, so you don't need to start Vetra first. For a client that doesn't start in the project directory, such as Claude Desktop, copy the entry from `.cursor/mcp.json` and replace `${workspaceFolder}` with the project's path. A project created before `ph mcp` points reactor-mcp at `http://localhost:4001/mcp`; run `ph migrate` to switch it to the launcher. → See [Connecting Claude with Reactor MCP](/academy/Lookup/Cookbook#connecting-claude-with-reactor-mcp) for a step-by-step walkthrough.
📦 Vetra Remote Drive Commands Remote drives enable collaborative development by syncing specifications across team members. **Key Commands:** - `ph init --remote-drive ` - Create or connect to a project using a remote drive - `ph run vetra` - Start development with a preview drive for testing local changes **Workflows:** - **Project Owner**: `ph init --remote-drive` → Create GitHub repo → Push → `ph run vetra` to configure - **Collaborator**: `ph init --remote-drive ` → `ph run vetra` to start developing **Preview Drive:** The preview drive allows you to safely test changes before they affect the shared remote drive. - The main **"Vetra" drive** syncs with the remote and contains the stable package configuration. - The **"Vetra Preview" drive** is created locally for testing document models and editors before syncing your changes to the team. - `ph run vetra` loads your local documents and editors. The `--watch` flag of the classic `ph vetra` command is no longer needed. → [Full Vetra Remote Drive Reference](/academy/Reference/CLITooling/VetraRemoteDrive)
## Up next With your drive set up, [create a package with Vetra](/academy/Build/GettingStartedBuilding/CreateAPackageWithVetra) to see the end-to-end flow, then build one by hand in the [Manual Todo tutorial](/academy/Build/ManualTodoTutorial/ExploreDemoPackage). --- ## Create a package with Vetra > Source: https://academy.vetra.io/academy/Build/GettingStartedBuilding/CreateAPackageWithVetra **INFO:** A **Powerhouse Package** is a distributable unit that bundles one or more document models, editors, and optional reactor modules into a single installable artifact. Once published to the Vetra registry, a package can be installed in any Vetra Cloud environment — extending Connect with new document types and editors, and extending Switchboard with the corresponding reactor logic.
Create a Package
The five steps to build and publish a package to the Vetra ecosystem.
On Vetra Cloud you'll find these steps as a quick reference for creating a package. This guide walks you through each one in detail — from installing the CLI and scaffolding your project to building and testing document models and editors. Publishing is covered separately in [Launch](/academy/Build/Launch/PublishYourProject). **WARNING:** **This tutorial is a summary for builders that are already familiar with building document models**. It summarizes the flow from initial setup to a built, tested package. Please start with the [**Manual Todo tutorial**](/academy/Build/ManualTodoTutorial/CreateNewPowerhouseProject) or the [**Document Model Creation**](/academy/Build/DocumentModelCreation/SpecifyTheStateSchema) section if you are unfamiliar with building a document model.
Key commands that you'll use in this flow - `pnpm install -g ph-cmd` or `npm install -g ph-cmd`: Installs the Powerhouse CLI globally. - `ph init`: Initializes a new Powerhouse project or sets up the local environment. - `ph run vetra`: Launches Vetra Studio from the project's `hosts/vetra/host.js`. - `ph run vetra --interactive`: Launches Vetra Studio in interactive mode for package development. - `ph build`: Builds the project for production. - `pnpm run test` or `npm test`: Runs unit tests. - `ph publish`: Publishes your package to the Vetra registry. - `ph install @your-org-ph/your-package-name`: Installs a published package into a local Powerhouse environment.
## Phase 1: Setup and initialization ### 1.1. Install Powerhouse CLI Ensure you have the Powerhouse Command Line Interface (`ph-cmd`) installed. You use it to manage your Powerhouse projects. ```bash pnpm install -g ph-cmd ``` Or if you're using npm: ```bash npm install -g ph-cmd ``` **INFO:** See the [Prerequisites](/academy/Build/GettingStartedBuilding/Prerequisites) guide for detailed installation instructions for Node.js 24, package managers (pnpm or npm), and Git if you haven't set them up yet. ### 1.2. Initialize your project environment Before creating a specific project, or if you're setting up your environment for the first time, initialize the Powerhouse environment. This command prepares your local setup, including a local Reactor configuration. ```bash ph init my-package ``` Replace `my-package` with your desired package name. If you run `ph init` without a name you will be prompted for one. `ph init` also writes a `hosts/` folder. Each subfolder holds one host file, which describes one way to run the project: ``` hosts/ connect/host.js ph run connect switchboard/host.js ph run switchboard vetra/host.js ph run vetra desktop/host.js ph run desktop assistant/host.js ph run assistant cli/host.js ph run cli ``` `ph run ` starts a host. See [Hosts, Runners & Plugins](/academy/Reference/HostsAndRunners/HostsRunnersAndPlugins) for what a host file contains.
How to make use of different branches? When installing or using the Powerhouse CLI commands you are able to make use of the dev & staging branches. These branches contain more experimental features than the latest stable release the PH CLI uses by default. They can be used to get access to a bugfix or features under development. | Command | Description | | ----------------------------------------------------------------------- | ------------------------------------------------- | | **pnpm install -g ph-cmd** or **npm install -g ph-cmd** | Install latest stable version | | **pnpm install -g ph-cmd@dev** or **npm install -g ph-cmd@dev** | Install development version | | **pnpm install -g ph-cmd@staging** or **npm install -g ph-cmd@staging** | Install staging version | | **ph init** | Use latest stable version of the boilerplate | | **ph init --dev** | Use development version of the boilerplate | | **ph init --staging** | Use staging version of the boilerplate | | **ph use latest** | Switch all dependencies to latest stable versions | | **ph use dev** | Switch all dependencies to development versions | | **ph use staging** | Switch all dependencies to staging versions | Please be aware that these versions can contain bugs and experimental features that aren't fully tested.
### 1.3. Launch Vetra Studio You can launch Vetra Studio in two modes: **INFO:** `ph run vetra` starts the `vetra` host from `hosts/vetra/host.js`. The classic `ph vetra` command is deprecated. If your project has no `hosts/` folder, see [Moving to hosts and `ph run`](/academy/Reference/HostsAndRunners/MigratingToHosts). #### Interactive Mode (Recommended for Development) ```bash ph run vetra --interactive ``` In interactive mode: - You'll receive confirmation prompts before any code generation - Changes require explicit confirmation before being processed - Provides better control and visibility over document changes Vetra Studio and its Switchboard reload your document models and editors when their files change. The `--watch` flag of the classic command is no longer needed. **WARNING:** When you are building your document model the code can break the Vetra Studio environment. A full overview of the Vetra Studio commands can be found in the [Powerhouse CLI](/academy/Reference/CLITooling/PowerhouseCLI#vetra) #### Standard Mode ```bash ph run vetra ``` In standard mode: - Changes are processed automatically with 1-second debounce - Multiple changes are batched and processed together - Uses the latest document state for processing
Alternatively: Use Connect Connect is your local development hub. Running it in Studio Mode spins up a local instance with a local Reactor, allowing you to define, build, and test document models. ```bash ph run connect dev ``` This command typically opens Connect in your browser at `http://localhost:3000/`. **INFO:** **Powerhouse Reactors** are essential nodes in the Powerhouse network. They store documents, manage versions, resolve conflicts, and verify document operation histories by rerunning them. Reactors can be configured for local storage (as in Studio Mode), centralized cloud storage, or decentralized storage networks.
### 1.4. Launch Claude with Reactor-MCP Vetra Studio connects to Claude through MCP (Model Context Protocol). This gives you greater control and direction over what the LLM codes for you. **INFO:** Vetra embraces **Specification Driven Design & Development** —an approach where your structured specification documents become the shared language between you and AI agents. You communicate intent through precise specs that are machine-readable and executable.
Explainer: Specification Driven AI In the **Manual Todo tutorial** we've been making use of strict schema definition principles to communicate the intended use case of our document models. The **schema definition language** is not only a shared language that bridges the gap between developer, designer and analyst but also the gap between builder and AI-agent through **specification driven AI control**. - Communicate your solution and intent through a structured specification framework designed for AI collaboration. - Specifications enable precise, iterative edits, since all our specification documents are machine-readable and executable. #### Key Reactor MCP Features **Reactor-mcp** is a Model Context Protocol (MCP) server that bridges AI agents with Powerhouse document operations. - It supports automatic document model creation from natural language descriptions - It implements a smart editor based on the underlying document models - It automatically triggers code generation when documents reach valid state - The MCP server enables the agent to work with both existing and newly created document models - Vetra supports integration with custom remote drives, allowing users to create, share and manage documents within these drives **Document Operations:** - `createDocument` / `getDocument` / `deleteDocument` - Manage documents - `addActions` - Modify document state through operations **Drive Operations:** - `getDrives` / `addDrive` / `getDrive` - Manage document drives - `addRemoteDrive` - Connect to remote drives **Document Model Operations:** - `getDocumentModels` - List available document model types - `getDocumentModelSchema` - Get schema for specific models **Document Model Agent:** A specialized AI agent that guides users through document model creation with requirements gathering, design confirmation, and implementation including state schema definition, operation creation, and code generation.
#### 0. Check the Reactor MCP configuration `ph init` already wrote the Reactor MCP configuration into your project's `.mcp.json`: ```json { "mcpServers": { "reactor-mcp": { "type": "stdio", "command": "node", "args": ["--run", "mcp"] } } } ``` `ph mcp` serves the reactor-mcp tools over stdio from the project's own `ph-cli`. `node --run mcp` runs the project's `mcp` script (`ph-cli mcp`), which works on every platform. On the first tool call it connects to the project's running Vetra, or starts `ph vetra --watch` in the background and writes its output to `.ph/vetra.log`, so you don't need to start Vetra first. A project created before `ph mcp` points reactor-mcp at `http://localhost:4001/mcp`; run `ph migrate` to switch it to the launcher. #### 1. Start the Reactor MCP: Make sure you are in the same directory as your project. Claude will automatically recognize the necessary files and MCP tools. ```bash claude ``` Claude also understands natural language, so similar phrasings work as well. ```bash connect to the reactor mcp ``` #### 2. Verify MCP connection: - Check that the Reactor MCP is available. - Confirm Vetra Studio shows "Connected to Reactor MCP" ```bash Connected to MCP successfully! I can see there's a "vetra-4de7fa45" drive available. The reactor-mcp server is running and ready for document model operations. or Connected to reactor MCP. You have access to 1 drive: vetra+a049e1b1== ``` ## Phase 2: Package Creation ### 2.1. Set Package Description (Required) 1. Provide a name for your package 2. Add a meaningful description 3. Add keywords to add search terms to your package 4. Confirm changes when prompted in interactive mode ### 2.2. Define Document Model (Required) You can create document models in two ways: #### Manual Creation - Define document schema with fields and types as in the **Manual Todo tutorial** - Create the necessary operations - Add the required modules to your package - The document model creation chapter in the Mastery track provides in depth support [here](/academy/Build/DocumentModelCreation/SpecifyTheStateSchema) → [Learn more about Document Models](/academy/Build/DocumentModelCreation/WhatIsADocumentModel) #### Using MCP (AI-Assisted) - Describe your package, it's functionality and your needs in natural language in great detail. - Claude will: - Generate an appropriate schema in the document model - Create the necessary operations - Implement the required reducers - Place the document in the Vetra drive - Claude will also add the necessary interface in the form of a [document editor](/academy/Build/BuildingUserExperiences/BuildingDocumentEditors) and scaffold the [drive-app functionality](/academy/Build/BuildingUserExperiences/BuildingADriveExplorer) when specified. → [Learn more about Building Document Editors](/academy/Build/BuildingUserExperiences/BuildingDocumentEditors) → [Learn more about Building a Drive Explorer](/academy/Build/BuildingUserExperiences/BuildingADriveExplorer)
Alternatively: Use Connect Within Connect Studio Mode, navigate to the Document Model Editor. Here, you'll specify the structure of your document model using GraphQL Schema Definition Language (SDL). - **State Schema:** Describes the data fields and types within your document (e.g., `ToDoItem` with `id`, `text`, `checked` fields). - This schema is the blueprint for your document model's data. In the same editor, specify the operations (state transitions) for your document model. These are also defined using GraphQL, specifically input types. - **Operations Schema:** Specifies the actions that can be performed on the document (e.g., `AddTodoItemInput`, `UpdateTodoItemInput`, `DeleteTodoItemInput`). - Each input type details the parameters required for an operation. - **Best Practices:** - Clearly define operations (often aligning with CRUD principles). - Use GraphQL input types for operation parameters. - Ensure operations reflect user intent for a clean API. Once your schema and operations are defined in Connect, export the specification. This will download a `.phdm.zip` file (e.g., `YourModelName.phdm.zip`). Save this file in the root of your Powerhouse project directory. Use the Powerhouse CLI to process an exported `.phdm.zip` file and generate the necessary boilerplate code for your document model. ```bash ph generate document-model --document YourModelName.phdm.zip ``` This command creates a new directory under `document-models/YourModelName/` containing: - A JSON file with the document model specification. - A GraphQL file with the state and operation schemas. - A `gen/` folder with autogenerated TypeScript types, action creators, and utility functions based on your schema. - A `src/` folder where you'll implement your custom logic (reducers, utils).
### 2.3. Add Document Editor (Required) #### Manual Creation - Select your target document model - Configure the currently limited editor properties - Add the editor specification to Vetra Studio drive - The system will generate scaffolding code #### Using MCP (AI-Assisted) - Request Claude to create an editor for your document. Do this with the help of a detailed description of the user interface, user experience and logic that you wish to generate. Make sure to reference operations from the document model to get the best results - Claude will: - Generate editor components - Implement necessary hooks - Create required UI elements → [Learn more about Building Document Editors](/academy/Build/BuildingUserExperiences/BuildingDocumentEditors)
Alternatively: Generate command A document editor provides the user interface for interacting with your document model. Generate an editor template: ```bash ph generate editor --name YourModelName --document-type powerhouse/YourModelName ``` - The `--name YourModelName` flag specifies the document model this editor is for. - The `--document-type powerhouse/YourModelName` flag links the editor to the specific document type defined in your model specification. This creates a template file, typically at `editors/your-model-name/editor.tsx`. - Customize this React component to build your UI. - You can use standard HTML, Tailwind CSS (available in Connect), or import custom CSS. - Utilize components from `@powerhousedao/document-engineering` for consistency and rapid development. Learn more at [Document-Engineering](/academy/Reference/EditorsUI/DocumentEngineering)
## Phase 3: Implementation and testing ### 3.1. Implement reducer logic Reducers are pure functions that implement the state transition logic for each operation defined in your schema. Navigate to `document-models/YourModelName/src/reducers/to-do-list.ts` (or similar, based on your model name). - Implement the functions for each operation (e.g., `addTodoItemOperation`, `updateTodoItemOperation`). - These functions take the current state and an action (containing input data) and return the new state. - Powerhouse handles immutability behind the scenes. ### 3.2. Write unit tests for reducers Test your reducer logic. Write unit tests in the `document-models/YourModelName/src/reducers/tests/` directory. - Verify that each operation correctly transforms the document state. - Use the auto-generated action creators from the `gen/` folder to create operation actions for your tests. Run tests using: ```bash pnpm run test ``` Or with npm: ```bash npm test ``` ### 3.3. Test the editor Test your editor in Vetra Studio by creating a new document of your defined type. Interact with your editor, test all functionalities, and ensure it correctly dispatches actions to the reducers and reflects state changes.
Alternatively: Use Connect Run Connect locally to see your editor in action: ```bash ph run connect dev ``` Create a new document of your defined type. Interact with your editor, test all functionalities, and ensure it correctly dispatches actions to the reducers and reflects state changes.
**TIP:** **Working with MCP and Claude** 1. Provide clear, specific instructions. 2. Ask for clarifying questions to be answered before code generation. 3. Review generated schemas before confirmation. 4. Work in layers instead of 'one-shotting' your code. 5. Verify implementation details in generated code before continuing. 6. Always double-check proposed next actions.
Complete Guide: Tips for Working with Claude in Vetra Studio ## Before You Start **Setup Requirements:** 1. Run `ph run vetra --interactive` in one terminal first 2. Start Claude in a separate terminal from your project directory 3. Connect with: `claude` or `connect to the reactor mcp` 4. Verify you see the confirmation message with your drive name ## Communication Best Practices ### 1. Always Review Before Implementation **CRITICAL**: Claude will present a proposal before creating anything. You'll see: - Proposed document model structure (state schema, operations, modules) - How data will be organized - What actions users can perform **Always review and confirm** before Claude proceeds. This is your chance to adjust the design. ### 2. Be Specific and Detailed When describing what you need, include: **For Document Models:** - Purpose of the document (what problem does it solve?) - All data fields and their types (strings, numbers, dates, etc.) - What operations users should be able to perform - Any relationships between data - Business rules or constraints **For Document Editors:** - Which document model it's for - UI layout and components you want - User interactions and workflows - Specific operations to use (by name from your document model) - Any styling preferences ### 3. Use Clear Examples Good prompt for a document model: ``` Create a document model for expense tracking with: - Each expense has: amount (number), description (text), category (expense type), date, and receipt URL (optional) - Users can: add expenses, edit expense details, delete expenses, and categorize by type - Track total amount automatically ``` Good prompt for an editor: ``` Create an editor for the expense tracker with: - A form to add new expenses (amount, description, category, date) - A table showing all expenses with sort by date - Each row has edit and delete buttons - Show total at the bottom - Use the addExpense, updateExpense, and deleteExpense operations ``` ### 4. Work in Layers (Don't "One-Shot") Instead of asking for everything at once: - ✅ Start with the core document model - ✅ Test it works - ✅ Then add the editor - ✅ Then add advanced features This approach catches issues early and gives you better results. ### 5. Interactive Mode Benefits Using `ph run vetra --interactive` gives you confirmation prompts: - Schema changes - Operation definitions - Code generation **Review each step** before confirming - it's easier to adjust now than later. ### 6. What to Expect After Implementation Claude will automatically: - Run TypeScript checks (`npm run tsc`) - Run linting (`npm run lint:fix`) - Report any errors found - Fix issues if needed You'll see confirmation when everything compiles successfully. ### 7. Common Issues and How to Avoid Them **Issue**: Generated model doesn't match expectations - **Solution**: Provide more detailed requirements upfront. Ask clarifying questions. **Issue**: Operations don't work as expected - **Solution**: Be explicit about all actions and their parameters. Use real-world examples. **Issue**: Editor UI doesn't look right - **Solution**: Describe the UI in detail (layout, components, interactions). Reference similar interfaces if helpful. ## Key Concepts to Know - **Document Model**: The template/blueprint for your documents (like a database schema) - **Document**: An actual instance with real data (like a database record) - **Operations**: Actions users can perform (like "add expense", "update status") - **Editor**: The user interface to interact with your documents - **Drive**: A collection that holds your documents (like a folder) ## Quick Tips 1. **Be specific**: More detail = better results 2. **Review proposals**: Always check before confirming 3. **Work incrementally**: Build in layers, not all at once 4. **Use operation names**: Reference them when describing editor functionality 5. **Ask questions**: If unsure, ask Claude to clarify or suggest options 6. **Test as you go**: Create model first, test it, then add the editor ## What Claude Can Do For You - Generate complete document models from natural language - Create all necessary operations automatically - Build React editor interfaces with your specifications - Handle all the TypeScript and boilerplate code - Fix type errors and linting issues - Add demo documents to test your models ## What You Should Focus On - Clearly describing your business requirements - Defining what data you need to track - Specifying what actions users should perform - Reviewing and confirming proposals - Testing the generated results **Remember**: Claude works best with clear, detailed requirements. Take time to explain what you want - it's faster than multiple iterations to fix misunderstandings.
## Phase 4: Package and publish With your document model and editor implemented and tested, package and publish them for distribution. Publishing is covered in one place for both the manual and Vetra workflows — it walks through configuring package details, building, versioning, and publishing to NPM or the Vetra registry: → [Publish your package](/academy/Build/Launch/PublishYourProject) ## Up next You've seen the full Vetra flow. Now build a package by hand, step by step, in the [Manual Todo tutorial](/academy/Build/ManualTodoTutorial/ExploreDemoPackage). --- ## Vetra builder tooling > Source: https://academy.vetra.io/academy/Build/GettingStartedBuilding/BuilderTools An overview of the builder tooling in the Powerhouse/Vetra ecosystem, for the manual (CLI) builder workflow. This list grows as the toolkit does. ## Powerhouse CLI The Powerhouse CLI (`ph-cmd`) is the command-line entry point for building by hand: scaffolding projects, generating code, and running Connect and the Reactor locally. - Install it and verify your setup in [Prerequisites](/academy/Build/GettingStartedBuilding/Prerequisites). - For the full command list — `ph init`, `ph use`, `ph update`, `ph generate`, and the rest — see the [Powerhouse CLI reference](/academy/Reference/CLITooling/PowerhouseCLI). - For task-oriented walkthroughs (switching versions, managing dependencies, publishing), see the [Cookbook](/academy/Lookup/Cookbook). ## Boilerplate The Document Model boilerplate is the foundational template used for code generation when scaffolding your editors and models. It ensures compatibility with host applications like Connect and Switchboard for document model and editor integration. After installing `ph-cmd`, run `ph init` to initialize a project directory and structure; this command uses the boilerplate. The boilerplate includes scripts for generating code, linting, formatting, building, and testing. ## Design system The Powerhouse Design System is a collection of reusable front-end components based on GraphQL scalars, including Powerhouse built-in scalars such as `PHID` and `Amount`. It provides: - Consistent UI components across Powerhouse applications - Automatic inclusion as a dependency in new document model projects - Customization options using CSS variables Read more in the [Component Library documentation](/academy/Reference/EditorsUI/DocumentEngineering). ## Reactor libraries Reactors are the nodes in the Powerhouse network that handle document storage, conflict resolution, and operation verification. The Reactor libraries include: - **API** — **Subgraphs** (modular GraphQL services that connect to the Reactor for structured data access) and **Processors** (event-driven components that react to document changes and process data). - **Browser** — handles client-side interactions. - **Local** — manages local storage and offline functionality. - **drive-app** — handles document organization and storage management, and can be customized to offer specific functionality, categorization, or tailored interfaces for your documents. ## Code generators Powerhouse provides several code generation tools: ### Document model scaffolding Generates the basic structure for new document models with `ph init`, based on the boilerplate. ### Editor generator Creates template code for document model editors: `ph generate editor --name --document-type `. ### Subgraph generator Creates GraphQL subgraph templates for data access: `ph generate subgraph --name `. ### Processor generator Generates processor templates for event handling: `ph generate processor --name `. ### Analytics processor generator Creates specialized processors for analytics tracking. ## Analytics engine The Analytics Engine tracks and analyzes operations and state changes on document models. Features include: - Custom dashboard and report generation - Document-model-specific analytics - Metric and dimension tracking - Data combination from multiple document models Generate an analytics processor with: ```bash ph generate processor --type analytics ``` --- ## Explore the demo package > Source: https://academy.vetra.io/academy/Build/ManualTodoTutorial/ExploreDemoPackage Before recreating the package yourself, install the finished version to see what you'll build. The demo is a pre-built package containing a document model, an editor, and a Drive-app — the same "To-do List Demo Package" you'll recreate across this tutorial. ## Step 1: Confirm the CLI is installed You set up `ph-cmd` in [Prerequisites](/academy/Build/GettingStartedBuilding/Prerequisites). Verify it: ```bash ph --version ``` If the command isn't found, complete [Prerequisites](/academy/Build/GettingStartedBuilding/Prerequisites) first. ## Step 2: Initialize a new project Now use the `ph init` command to initialize a new project and install a Powerhouse package inside the project. You'll be asked to name your project. Afterwards, move inside your project with `cd project-name` ## Step 3: Install the to-do list demo package Now, use the `ph install` command to install the demo package inside the project. ```bash # Install the package ph install @powerhousedao/todo-demo ``` This command downloads and sets up the `@powerhousedao/todo-demo`, making its features available in your Powerhouse project.
Expected CLI result ```bash installing dependencies 📦 ...  WARN  19 deprecated subdependencies found: @esbuild-kit/core-utils@3.3.2, @esbuild-kit/esm-loader@2.6.5, @npmcli/move-file@1.1.2, @paulmillr/qr@0.2.1, are-we-there-yet@3.0.1, gauge@4.0.4, glob@7.2.3, graphql-language-service-interface@2.10.2, graphql-language-service-parser@1.10.4, graphql-language-service-types@1.8.7, graphql-language-service-utils@2.7.1, inflight@1.0.6, multibase@4.0.6, multicodec@3.2.1, node-domexception@1.0.0, npmlog@6.0.2, rimraf@2.7.1, rimraf@3.0.2, sudo-prompt@8.2.5 Packages: +1 + Progress: resolved 2277, reused 2106, downloaded 1, added 1, done  WARN  Issues with peer dependencies found . ├─┬ @powerhousedao/reactor-browser 3.1.0 │ └─┬ @powerhousedao/analytics-engine-browser 0.6.0 │ └── ✕ unmet peer @powerhousedao/analytics-engine-knex@0.5.1: found 0.6.0 ├─┬ @types/react-dom 19.1.6 │ └── ✕ unmet peer @types/react@^19.0.0: found 18.3.23 └─┬ react-native 0.80.0 ├── ✕ unmet peer @types/react@^19.1.0: found 18.3.23 ├── ✕ unmet peer react@^19.1.0: found 18.3.1 └─┬ @react-native/virtualized-lists 0.80.0 └── ✕ unmet peer @types/react@^19.0.0: found 18.3.23 dependencies: + @powerhousedao/todo-demo-package 1.1.1 ╭ Warning ───────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ Ignored build scripts: @acaldas/graphql-codegen-typescript-validation-schema, @apollo/protobufjs, │ │ @datadog/pprof, @ipshipyard/node-datachannel, @parcel/watcher, @prisma/client, @prisma/engines, │ │ @tailwindcss/oxide, bufferutil, esbuild, keccak, prisma, sqlite3, utf-8-validate. │ │ Run "pnpm approve-builds" to pick which dependencies should be allowed to run scripts. │ │ │ ╰────────────────────────────────────────────────────────────────────────────────────────────────────────╯ Done in 6s using pnpm v10.9.0 Dependency installed successfully 🎉 ⚙️ Updating powerhouse config file... Config file updated successfully 🎉 ```
You have now successfully installed `ph-cmd`, initalized a product project and added your first package! ## Step 4: Run the Connect host application To run the package locally in Connect, run: ```bash ph run connect dev ``` This starts the `connect` host that `ph init` wrote to `hosts/connect/host.js`. The classic `ph connect` command is deprecated: see [Moving to hosts and `ph run`](/academy/Reference/HostsAndRunners/MigratingToHosts). Click the returned localhost URL and you should see Connect appear in your browser. **INFO:** **Connect** is the Powerhouse host application—a container that runs all your apps, editors, and drives. Think of it as the browser for your Powerhouse ecosystem. **Vetra Studio** (which you'll use for building) runs inside Connect, just like how a web app runs inside a browser.
Connect Home
The Powerhouse Connect interface.
When you click the settings wheel in the bottom right corner, you'll get access to the **Package Manager**. Here, you'll see that you've installed the `@powerhousedao/todo-demo`, which contains not only a **Document Model** and its accompanying editor but also a **Drive-app** specific to the todo-list document model.
Package Manager
The Package Manager showing the installed todo-demo-package.
## Step 5: Create a todo list document **TIP:** A **drive** is a folder to store and organize your documents in. Powerhouse offers the ability to build customized Drive-apps for your documents. Think of a Drive-app as a specialized lens—it offers **different ways to visualize, organize, and interact with** the data stored within a drive, making it more intuitive and efficient for specific use cases. To learn more, visit [Building a Drive-app](/academy/Build/BuildingUserExperiences/BuildingADriveExplorer) **TIP:** An **editor** is a UI component for viewing and modifying a single document. A **Drive-app** is a custom interface for managing multiple documents within a drive—providing aggregated views, progress tracking, and specialized workflows across your document collection. ### 5.1 Create a local todo-list app drive First, let's create a dedicated drive for your to-do lists: - Click the new drive icon in the interface - In the **Drive-app** field, select 'todo-list Drive-app' - This creates a specialized drive that's optimized for to-do list documents ### 5.2 Create a todo-list document Now move into the drive you've just created: - Click the button at the bottom of the page to create a new to-do list document - This opens the to-do list editor where you can start managing your tasks ### 5.3 Add a few todos and inspect the document history - Add a few to-dos that are on your mind - You'll see a statistics widget that counts the open to-dos - After closing the document, look at the todo-list Drive-app interface—you'll see that it tracks your tasks and displays a progress bar This is an example of the **usefulness and impact of Drive-apps**. They offer a customized interface that works well with the different documents inside your drive. Read more about Drive-apps in the Mastery Track: [Drive-apps](/academy/Build/BuildingUserExperiences/BuildingADriveExplorer). A key feature of Connect is the **Operations History**. Every change to a document is stored as an individual operation, creating an immutable and replayable history. This provides complete auditability and transparency, as you can inspect each revision, its details, and any associated signatures. For example, you can see a chronological list of all modifications, along with who made them and when.
Operations History Button
You can find the button to visit the operations history in the document model toolbar
Operations History
Example of the operations history for a document, showing all modifications made to it in a list.{" "}
Learn more about the [Operations History](/academy/Build/BuildingUserExperiences/DocumentTools/OperationHistory) and other document tools you get for free. ## Step 6: Enable operation signing and verification through Renown Renown is Powerhouse's **decentralized identity and reputation system** designed to address the challenge of trust within open organizations, where contributors often operate under pseudonyms. In traditional organizations, personal identity and reputation are key to establishing trust and accountability. Renown replicates this dynamic in the digital space, allowing contributors to earn experience and build reputation without revealing their real-world identities. **TIP:** When signing in with Renown, use an Ethereum or blockchain address that can function as your 'identity', as this address will accrue more experience and history over time. ### 6.1 Click the renown icon and connect your Ethereum identity "**Log in with Renown**" is a decentralized authentication flow that enables you to log into applications by signing a credential with your Ethereum wallet. Upon signing in, a Decentralized Identifier (DID) is created based on your Ethereum key.
Renown Login
The Renown login screen, prompting for a signature from a wallet.
### 6.2 Authorize Connect to sign document edits on your behalf This DID is then associated with a credential that authorizes a specific Connect instance to act on your behalf. That credential is stored securely on Ceramic, a decentralized data network. When you perform actions through the Powerhouse Connect interface, those operations are signed with the DID and transmitted to Switchboard, which serves as the verifier.
Connect Address for DID
A newly generated DID and address shown within the Connect interface.
Renown Login Complete
Confirmation of a successful login with Renown.
### 6.3 Verify the signatures of new operations in the todo list With this system, every operation made to a document is cryptographically signed by the contributor's Renown identity. Each change is verifiable, traceable, and attributable to a specific pseudonymous user, giving every document a complete audit trail. Now, return to your to-do list and make some additional changes. You'll notice that these operations are now signed with your Renown identity, making every action traceable and verifiable in the operations history.
Operation History Signature
Your DID is now signing the operations that are being added to the history.
## Step 7: Export a document Export the document as a `.phd` (Powerhouse Document) file using the export button in the document toolbar at the top. In this toolbar, you will find all available functionality for your documents. The `.phd` file can be sent through any of your preferred channels to other users on your network. ### Up next Now that you have explored a Powerhouse package and discovered its basic functionalities, it is time to start building your own. Our next tutorial focuses on creating a simple to-do list document and will introduce you to the world of **Document Models**—the foundation of **Specification Driven Design & Development**, where structured specs become the shared language between you and AI agents. --- ## Create a new to-do list document > Source: https://academy.vetra.io/academy/Build/ManualTodoTutorial/CreateNewPowerhouseProject **TIP:** 📦 **Reference Code**: [step-1-initialize-with-ph-init](https://github.com/powerhouse-inc/todo-tutorial/tree/step-1-initialize-with-ph-init) This tutorial step has a corresponding branch in the repository. You can: - View the complete code for this step - Clone and checkout the branch to see the result - Compare your implementation using `git diff` ::: ## Overview This tutorial guides you through creating a simplified version of a 'Powerhouse project' for a **todo-list**. A Powerhouse project primarily consists of a document model and its editor. As your projects use-case expands you can add data-integrations or a specific Drive-app as seen in the demo package. For today's purpose, you'll be using **Vetra Studio**, the builder platform through which developers can access and manage specifications of your project. Vetra Studio runs inside **Connect**, the Powerhouse host application that serves as a container for all Powerhouse apps and drives. ## Prerequisites - Powerhouse CLI installed: `pnpm install -g ph-cmd` or `npm install -g ph-cmd` - Node.js 24 and a package manager (pnpm or npm) installed - Visual Studio Code (or your preferred IDE) - Terminal/Command Prompt access If you need help with installing the prerequisites you can visit our page [prerequisites](/academy/Build/GettingStartedBuilding/Prerequisites)
📖 How to use this tutorial This tutorial is designed for you to **build your own project from scratch** while having access to reference code at each step. ### Setup: Create your project and connect to tutorial repo 1. **Create your project** following the tutorial: ```bash mkdir ph-projects cd ph-projects ph init # When prompted, enter project name: todo-tutorial cd todo-tutorial ``` 2. **Add the tutorial repository as a remote** to access reference branches: ```bash git remote add tutorial https://github.com/powerhouse-inc/todo-tutorial.git git fetch tutorial --prune ``` 3. **Create your own branch** to keep your work organized: ```bash git checkout -b my-todo-project ``` Now you have access to all tutorial step branches while working on your own code! ### Compare your work with reference steps At any point, compare what you've built with a tutorial step: ```bash # Compare your current work with step-1 git diff tutorial/step-1-initialize-with-ph-init # See what changed between tutorial steps git diff tutorial/step-1-initialize-with-ph-init..tutorial/step-2-generate-todo-list-document-model # Compare specific files git diff tutorial/step-1-initialize-with-ph-init -- package.json ``` ### Visual diff with GitHub Desktop For a more visual comparison, use GitHub Desktop: 1. **First, make your initial commit** (GitHub Desktop won't show your branch until you have at least one commit): ```bash git add . git commit -m "Initial project setup" ``` 2. **Open GitHub Desktop** and open your repository 3. **Compare branches visually**: - Click on **Branch** menu in the top menu bar - Select **"Compare to Branch..."** - Choose the tutorial branch you want to compare with (e.g., `tutorial/step-1-initialize-with-ph-init`) - GitHub Desktop will show you all file differences in a visual interface 4. **Review the differences**: - Click on any file to see side-by-side or unified diff view - See exactly what's different between your code and the reference **Tip**: You can also use VS Code's Git Graph extension or the command palette → "Git: Compare with Branch" ### If you get stuck Reset your code to match a tutorial step: ```bash # Reset to step-2 (WARNING: loses your changes) git reset --hard tutorial/step-2-generate-todo-list-document-model ```
## Quick start Create a new Powerhouse project with a single command: ```bash ph init ``` ## Before you begin 1. Open your terminal (either your system terminal or IDE's integrated terminal) 2. Optionally, create a folder first to keep your Powerhouse projects: ```bash mkdir ph-projects cd ph-projects ``` 3. Ensure you're in the correct directory before running the `ph init` command. In the terminal, you will be asked to enter the project name. Fill in the project name and press Enter. ````bash you@yourmachine:~/ph-projects % ph init ? What is the project name? ‣ todo-tutorial ``` ```` Once the project is created, you will see output like: ```bash 🚀 Initializing a new project... ▶️ Creating directory for project "todo-tutorial"... ✅ Project directory created ▶️ Initializing git repository... ✅ Git repository initialized ▶️ Creating project boilerplate files... ✅ Project boilerplate files created ▶️ Installing project dependencies with npm... ✅ Project dependencies installed ▶️ Formatting boilerplate project files... ✅ Boilerplate files formatted 🎉 Successfully created project "todo-tutorial" 🎉 ``` Navigate to the newly created project directory: `bash cd todo-tutorial ` ## Develop a single document model in Vetra Studio **Vetra Studio** is the builder's orchestration hub for assembling all specifications needed for your package. It provides a **Vetra Studio Drive** to access, manage, and share document model specifications, editors, and data integrations—all through a visual interface. For deeper coverage, see the [Vetra Studio documentation](/academy/GetStarted/VetraStudio). Once in the project directory, run the `ph run vetra` command to start a Vetra Studio Drive where you'll be defining your specifications. This is the preferred way to launch your development environment. **INFO:** You'll notice "reactor-api" in the terminal output. A **Reactor** is the Powerhouse back-end service that hosts your drives, handles document synchronization, and provides the GraphQL API. When you run `ph run vetra`, a local Reactor starts automatically to power your development environment. `ph run vetra` starts the `vetra` host that `ph init` wrote to `hosts/vetra/host.js`. The classic `ph vetra` command is deprecated: see [Moving to hosts and `ph run`](/academy/Reference/HostsAndRunners/MigratingToHosts). ```bash ph run vetra ``` The host application for Vetra Studio will start and you will see the following output: ```bash ℹ [reactor-api] [package-manager] Loading packages: @powerhousedao/vetra 14:44:19 ℹ [reactor-api] [server] WebSocket server available at /graphql/subscriptions 14:44:22 ℹ [reactor-api] [graphql-manager] Registered /graphql/system subgraph. 14:44:22 ℹ [reactor-api] [graphql-manager] Registered /graphql/analytics subgraph. 14:44:22 ℹ [reactor-api] [graphql-manager] Registered /d/:drive subgraph. 14:44:22 ℹ [reactor-api] [graphql-manager] Registered /graphql supergraph 14:44:23 ℹ [reactor-api] [graphql-manager] Registered /graphql/document-editor subgraph. 14:44:23 ℹ [reactor-api] [graphql-manager] Registered /graphql/vetra-package subgraph. 14:44:23 ℹ [reactor-api] [graphql-manager] Registered /graphql/subgraph-module subgraph. 14:44:23 ℹ [reactor-api] [graphql-manager] Registered /graphql/processor-module subgraph. 14:44:23 ℹ [reactor-api] [graphql-manager] Registered /graphql/app-module subgraph. 14:44:23 ℹ [reactor-api] [graphql-manager] Registered /graphql/vetra-read-model subgraph. 14:44:23 ℹ [reactor-api] [server] MCP server available at http://localhost:4001/mcp 14:44:24 Switchboard initialized 14:44:24 ➜ Drive URL: http://localhost:4001/d/vetra-bac239dd 14:44:24 2:44:24 PM [vite] (client) Re-optimizing dependencies because vite config has changed 14:44:24 Port 3000 is in use, trying another one... 14:44:24 ➜ Local: http://localhost:3000/ 14:44:24 ➜ Network: use --host to expose 14:44:24 ➜ press h + enter to show help ```` A new browser window will open when visiting localhost and you will see the Vetra Studio Drive
Vetra Studio Drive
The Vetra Studio Drive, a builder app that collects all of the specification of a package.
Create a new document model by clicking the Document Models 'Add new specification' button. Name your document TodoList (PascalCase, no spaces or hyphens). If you've followed the steps correctly, you'll have an empty TodoList document where you can define the 'Document Specifications' in the next step.
Alternatively: Develop a single document model in Connect (legacy) **NOTE:** Running Connect on its own is a legacy workflow. We recommend using `ph run vetra` for all new development, as it provides better tooling and automatic code generation. Once in the project directory, run the `ph run connect dev` command to start a local instance of the Connect application. This allows you to start your document model specification document. Run the following command to start the Connect application: ```bash ph run connect dev ``` The Connect application will start and you will see the following output: ```bash ➜ Local: http://localhost:3000/ ➜ Network: http://192.168.5.110:3000/ ➜ press h + enter to show help ``` A new browser window will open and you will see the Connect application. If it doesn't open automatically, you can open it manually by navigating to `http://localhost:3000/` in your browser. You will see your local drive and a button to create a new drive. **TIP:** If you local drive is not present navigate into Settings in the bottom left corner. Settings > Danger Zone > Clear Storage. Clear the storage of your localhost application as it might has an old session cached. 4. Move into your local drive. Create a new document model by clicking the `DocumentModel` button, found in the 'New Document' section at the bottom of the page. Name your document `TodoList` (PascalCase, no spaces or hyphens). If you've followed the steps correctly, you'll have an empty `TodoList` document where you can define the **'Document Specifications'**.
## Verify your setup At this point, your project structure should match the `step-1-initialize-with-ph-init` branch. You should have: - Empty `document-models/`, `editors/`, `processors/`, and `subgraphs/` directories - Configuration files: `powerhouse.config.json`, `powerhouse.manifest.json` - Package management files: `package.json`, `pnpm-lock.yaml` - Build configuration: `tsconfig.json`, `vite.config.ts`, `vitest.config.ts` ### Compare with reference implementation Verify your initial setup matches the tutorial: ```bash # Compare your project structure with step-1 git diff tutorial/step-1-initialize-with-ph-init # List files in the tutorial's step-1 git ls-tree -r --name-only tutorial/step-1-initialize-with-ph-init # View a specific config file from step-1 git show tutorial/step-1-initialize-with-ph-init:package.json ```` ## ph install / ph add ```bash ph install [dependencies...] [--registry ] [--local] [--allow-build ] ``` Aliases: `ph add`, `ph i` The install command adds Powerhouse dependencies to your project. By default it only registers the package in `powerhouse.config.json` with provider `"registry"` — Connect will load it from the registry CDN at runtime. With `--local`, the package is also installed into `node_modules` and marked as provider `"local"` — it will be bundled into `ph connect build` so the preview works without the registry being reachable. Resolution order for the registry URL: `--registry` flag > `PH_REGISTRY_URL` env > `powerhouse.config.json` > default ## Up next In the next tutorials, you will learn how to specify, add code and build an editor for your document model and export it to be used in your Powerhouse package. --- ## Write the document specification > Source: https://academy.vetra.io/academy/Build/ManualTodoTutorial/DefineToDoListDocumentModel **TIP:** 📦 **Reference Code**: [step-2-generate-todo-list-document-model](https://github.com/powerhouse-inc/todo-tutorial/tree/step-2-generate-todo-list-document-model) This tutorial step has a corresponding branch. After completing this step, your project will have a generated document model with: - Document model specification files (`todo-list.json`, `schema.graphql`) - Auto-generated TypeScript types and action creators - Reducer scaffolding ready for implementation :::
📖 How to use this tutorial **Prerequisites**: Complete step 1 and set up the tutorial remote (see previous step). ### Compare your generated code After running `ph generate document-model --document TodoList.phdm.zip`, compare with the reference: ```bash # Compare all generated files with step-2 git diff tutorial/step-2-generate-todo-list-document-model # Compare specific directory git diff tutorial/step-2-generate-todo-list-document-model -- document-models/todo-list/ ``` ### See what was generated View the complete step-2 reference code: ```bash # List files in the tutorial's step-2 git ls-tree -r --name-only tutorial/step-2-generate-todo-list-document-model document-models/ # View a specific file from step-2 git show tutorial/step-2-generate-todo-list-document-model:document-models/todo-list/schema.graphql ``` ### Visual comparison with GitHub Desktop After making a commit, use GitHub Desktop for visual diff: 1. **Branch** menu → **"Compare to Branch..."** 2. Select `tutorial/step-2-generate-todo-list-document-model` 3. Review all file differences in the visual interface See step 1 for detailed GitHub Desktop instructions.
In this tutorial, you will learn how to define the specifications for a **todo-list** document model within Vetra Studio using its GraphQL schema, and then export the resulting document model specification document for your Powerhouse project. If you don't have a document specification file created yet, have a look at the previous step of this tutorial to create a new document specification. ## TodoList document specification We'll continue with this project to teach you how to create a document model specification and later an editor for your document model. We use the **GraphQL Schema Definition Language** (SDL) to define the schema for the document model. Below, you can see the SDL for the `TodoList` document model. **INFO:** This schema defines the **data structure** of the document model and the types involved in its operations, which are detailed further as input types. Documents in Powerhouse use **event sourcing**: every state transition is an operation. GraphQL input types describe those operations and the parameters each one needs, which makes state transitions explicit, validated, and reproducible. This is the essence of **Specification Driven Design & Development**: your schema serves as a machine-readable specification that both humans and AI agents can understand and execute—turning your intent into precise, maintainable functionality.
State schema of our simplified TodoList ```graphql # The state of our TodoList - contains an array of todo items type TodoListState { items: [TodoItem!]! } # A single to-do item with its properties type TodoItem { id: OID! # Unique identifier for each to-do item text: String! # The text description of the to-do item checked: Boolean! # Status of the to-do item (checked/unchecked) } ```
Operations schema of our simplified TodoList ```graphql # Defines a GraphQL input type for adding a new to-do item # Only text is required - ID is generated automatically, checked defaults to false input AddTodoItemInput { text: String! # The text for the new todo item } # Defines a GraphQL input type for updating a to-do item # ID is required to identify which item to update # text and checked are optional - only provided fields will be updated input UpdateTodoItemInput { id: OID! # Required: which item to update text: String # Optional: new text value checked: Boolean # Optional: new checked state } # Defines a GraphQL input type for deleting a to-do item input DeleteTodoItemInput { id: OID! # The ID of the item to delete } ```
## Define the document model specification ### The steps below show you how to do this: 1. In Vetra Studio, click on **'document model'** to open the document model specification editor. 2. Name your document model `TodoList` (PascalCase, no spaces or hyphens) in the Connect application. **Pay close attention to capitalization, as it influences code generation.** 3. You'll be presented with a form to fill in metadata about the document model. Fill in the details in the respective fields. In the **Document Type** field, type `powerhouse/todo-list` (lowercase with hyphen). This defines the new type of document that will be created with this document model specification. ![TodoList Document Model Form Metadata](../docs/docs/images/DocumentModelHeader.png) 4. In the code editor, you can see the SDL for the document model. Replace the existing SDL template with the SDL defined in the [State Schema](#state-schema) section. Only copy and paste the types, leaving the inputs for the next step. You can, however, already press the 'Sync with schema' button to set the initial state of your document model specification based on your Schema Definition Language. 5. Below the editor, find the input field `Add module`. You'll use this to create and name a module for organizing your input operations. In this case, we will name the module `todos`. Press enter. 6. Now there is a new field, called `Add operation`. Here you will have to add each input operation to the module, one by one. 7. Inside the `Add operation` field, type `ADD_TODO_ITEM` and press enter. A small editor will appear underneath it, with an empty input type that you have to fill. Copy the first input type from the [Operations Schema](#operations-schema) section and paste it in the editor. The editor should look like this: ```graphql input AddTodoItemInput { text: String! } ``` 8. Repeat the process from step 7 for the other input operations: `UPDATE_TODO_ITEM` and `DELETE_TODO_ITEM`. You may have noticed that you only need to add the name of the operation (e.g., `UPDATE_TODO_ITEM`, `DELETE_TODO_ITEM`) without the `Input` suffix. It will then be generated once you press enter. 9. In the meantime Vetra has been keeping an eye on your inputs and started code generation in your directory. In your terminal you will also find any validation errors that help you to identify missing specifications. Check below screenshot for the complete implementation: ![ToDoList Document Model](../docs/docs/images/DocumentModelOperations.png) ## Verify your document model generation If you have been watching the terminal in your IDE you will see that Vetra has been tracking your changes and scaffolding your directory. It will mention: ``` ℹ [Vetra] Document model TodoList is valid, proceeding with code generation ``` Your project should have the following structure in `document-models/todo-list/`: ``` document-models/todo-list/ ├── index.ts ├── todo-list.json # Document model specification ├── v1/ │ ├── index.ts │ ├── actions.ts │ ├── hooks.ts │ ├── module.ts │ ├── schema.graphql # GraphQL schema │ ├── utils.ts │ ├── gen/ # Auto-generated code (don't edit) │ │ ├── schema/ │ │ │ └── index.ts │ │ ├── actions.ts │ │ ├── controller.ts │ │ ├── creators.ts │ │ ├── document-model.ts │ │ ├── document-schema.ts │ │ ├── document-type.ts │ │ ├── index.ts │ │ ├── ph-factories.ts │ │ ├── reducer.ts │ │ ├── types.ts │ │ ├── utils.ts │ │ └── todos/ # Per-module generated files │ │ ├── actions.ts │ │ ├── creators.ts │ │ ├── error.ts │ │ └── operations.ts │ ├── src/ # Your custom implementation │ │ ├── index.ts │ │ ├── utils.ts │ │ └── reducers/ │ │ └── todos.ts │ └── tests/ │ ├── document-model.test.ts │ └── todos.test.ts └── upgrades/ ├── index.ts ├── upgrade-manifest.ts ├── v1.ts └── versions.ts ``` **TIP:** To make sure everything works as expected: ```bash # Check types compile correctly pnpm tsc # Check linting passes pnpm lint # Compare your generated files with step-2 git diff tutorial/step-2-generate-todo-list-document-model -- document-models/todo-list/ ``` ### Up next: reducers Up next, you'll learn how to implement the runtime logic and components that will use the `TodoList` document model specification you've just created and exported. --- ## Implement the document model reducers > Source: https://academy.vetra.io/academy/Build/ManualTodoTutorial/ImplementOperationReducers **TIP:** 📦 **Reference Code**: [step-3-implement-reducer-operation-handlers](https://github.com/powerhouse-inc/todo-tutorial/tree/step-3-implement-reducer-operation-handlers) This step focuses on implementing the reducer logic for add, update, and delete operations.
📖 How to use this tutorial ### Compare your reducer implementation After implementing your reducers: ```bash # Compare your reducers with the reference git diff tutorial/step-3-implement-reducer-operation-handlers -- document-models/todo-list/src/reducers/ # View the reference reducer implementation git show tutorial/step-3-implement-reducer-operation-handlers:document-models/todo-list/src/reducers/todos.ts ``` ### Visual comparison with GitHub Desktop After committing your work, compare visually: 1. **Branch** menu → **"Compare to Branch..."** 2. Select `tutorial/step-3-implement-reducer-operation-handlers` 3. Review differences in the visual interface ### If you get stuck View or reset to a specific step: ```bash # View the reducer code git show tutorial/step-3-implement-reducer-operation-handlers:document-models/todo-list/src/reducers/todos.ts # Reset to this step (WARNING: loses your changes) git reset --hard tutorial/step-3-implement-reducer-operation-handlers ```
Now implement the operation reducers for the **todo-list** document model. In the previous step, Vetra imported the document specification and scaffolded the code and directory through live code generation. **INFO:** Reducers are a core concept in Powerhouse document models. They implement the state transition logic for each operation defined in your schema. **Connection to schema definition language (SDL)**: The reducers directly implement the operations you defined in your SDL. Remember how we defined `AddTodoItemInput`, `UpdateTodoItemInput`, and `DeleteTodoItemInput` in our schema? The reducers provide the actual implementation of what happens when those operations are performed. ## Explore the generated reducer file Navigate to `/document-models/todo-list/src/reducers/todos.ts` and open it. You should see scaffolding code that needs to be filled for the three operations you specified: ```typescript export const todoListTodosOperations: TodoListTodosOperations = { addTodoItemOperation(state, action) { // TODO: Implement "addTodoItemOperation" reducer throw new Error('Reducer "addTodoItemOperation" not yet implemented'); }, updateTodoItemOperation(state, action) { // TODO: Implement "updateTodoItemOperation" reducer throw new Error('Reducer "updateTodoItemOperation" not yet implemented'); }, deleteTodoItemOperation(state, action) { // TODO: Implement "deleteTodoItemOperation" reducer throw new Error('Reducer "deleteTodoItemOperation" not yet implemented'); }, }; ``` ## Implement the operation reducers Let's implement each reducer one by one. ### Step 1: Add the import First, add the `generateId` import at the top of the file: ```typescript // added-line ``` ### Step 2: Implement addTodoItemOperation Replace the boilerplate `addTodoItemOperation` with the actual implementation: ```typescript export const todoListTodosOperations: TodoListTodosOperations = { // removed-start addTodoItemOperation(state, action) { // TODO: Implement "addTodoItemOperation" reducer throw new Error('Reducer "addTodoItemOperation" not yet implemented'); }, // removed-end // added-start addTodoItemOperation(state, action) { const id = generateId(); state.items.push({ ...action.input, id, checked: false }); }, // added-end updateTodoItemOperation(state, action) { // ... }, deleteTodoItemOperation(state, action) { // ... }, }; ``` **What's happening here:** - We generate a unique ID using `generateId()` from `document-model/core` - We push a new item to the `items` array with the input text, new ID, and `checked: false` - Under the hood, Powerhouse uses Immer.js, so this "mutation" is actually immutable ### Step 3: Implement updateTodoItemOperation Replace the boilerplate `updateTodoItemOperation`: ```typescript // removed-start updateTodoItemOperation(state, action) { // TODO: Implement "updateTodoItemOperation" reducer throw new Error('Reducer "updateTodoItemOperation" not yet implemented'); }, // removed-end // added-start updateTodoItemOperation(state, action) { const item = state.items.find((item) => item.id === action.input.id); if (!item) return; item.text = action.input.text ?? item.text; item.checked = action.input.checked ?? item.checked; }, // added-end ``` **What's happening here:** - We find the item by its ID - We return early if the item is not found - We use nullish coalescing (`??`) to only update fields that are provided ### Step 4: Implement deleteTodoItemOperation Replace the boilerplate `deleteTodoItemOperation`: ```typescript // removed-start deleteTodoItemOperation(state, action) { // TODO: Implement "deleteTodoItemOperation" reducer throw new Error('Reducer "deleteTodoItemOperation" not yet implemented'); }, // removed-end // added-start deleteTodoItemOperation(state, action) { state.items = state.items.filter((item) => item.id !== action.input.id); }, // added-end ``` **What's happening here:** - We filter out the item with the matching ID - This creates a new array without the deleted item ## Complete reducer file Here's the complete implementation:
Complete todos.ts ```typescript export const todoListTodosOperations: TodoListTodosOperations = { addTodoItemOperation(state, action) { const id = generateId(); state.items.push({ ...action.input, id, checked: false }); }, updateTodoItemOperation(state, action) { const item = state.items.find((item) => item.id === action.input.id); if (!item) return; item.text = action.input.text ?? item.text; item.checked = action.input.checked ?? item.checked; }, deleteTodoItemOperation(state, action) { state.items = state.items.filter((item) => item.id !== action.input.id); }, }; ```
**TIP:** To make sure everything works as expected: ```bash # Check types compile correctly pnpm tsc # Check linting passes pnpm lint # Compare with reference implementation git diff tutorial/step-3-implement-reducer-operation-handlers -- document-models/todo-list/src/reducers/ ``` ## Up next: Writing tests In the next chapter, you'll write tests to verify your reducer implementations work correctly. --- ## Write document model tests > Source: https://academy.vetra.io/academy/Build/ManualTodoTutorial/WriteDocumentModelTests **TIP:** 📦 **Reference Code**: [step-4-implement-tests-for-todos-operations](https://github.com/powerhouse-inc/todo-tutorial/tree/step-4-implement-tests-for-todos-operations) This step covers writing tests for the reducers you implemented in the previous step.
📖 How to use this tutorial ### Compare your tests After writing tests: ```bash # Compare your tests with the reference git diff tutorial/step-4-implement-tests-for-todos-operations -- document-models/todo-list/src/tests/ # View the reference test implementation git show tutorial/step-4-implement-tests-for-todos-operations:document-models/todo-list/src/tests/todos.test.ts ``` ### Visual comparison with GitHub Desktop After committing your work, compare visually: 1. **Branch** menu → **"Compare to Branch..."** 2. Select `tutorial/step-4-implement-tests-for-todos-operations` 3. Review differences in the visual interface
To verify the operation reducers work as expected, write tests for them. Generating your document model code created boilerplate tests; now extend them to check the reducer logic. ## Understanding the generated test file Navigate to `/document-models/todo-list/src/tests/todos.test.ts`. You will see that we have some basic "sanity check" style tests already. These make sure that your operations at least result in a valid document model state. ```typescript reducer, utils, isTodoListDocument, addTodoItem, AddTodoItemInputSchema, updateTodoItem, UpdateTodoItemInputSchema, deleteTodoItem, DeleteTodoItemInputSchema, } from "todo-tutorial/document-models/todo-list"; describe("Todos Operations", () => { it("should handle addTodoItem operation", () => { // The `createDocument` utility function from your document model creates // a new empty document, i.e. one with your default initial state const document = utils.createDocument(); // The generateMock function takes one of your generated input schemas // and creates an object populated with random values for each field const input = generateMock(AddTodoItemInputSchema()); // We call your document model's reducer with the new document we just created // and the action we want to test, `addTodoItem` in this case. // The reducer returns a new object, which is the document with the action applied. // If successful, there will be an operation which corresponds to this action // in the updated document's operations list. const updatedDocument = reducer(document, addTodoItem(input)); // When you generate a document model, we give you validation utilities like // `isTodoListDocument` which confirms the document is of the correct form // in a way that TypeScript recognizes expect(isTodoListDocument(updatedDocument)).toBe(true); // At the start a document will have 0 operations, so after applying this action // there should now be one operation expect(updatedDocument.operations.global).toHaveLength(1); // The operation added to the list should correspond to the correct action type expect(updatedDocument.operations.global[0].action.type).toBe( "ADD_TODO_ITEM", ); }); it("should handle updateTodoItem operation", () => { // ... boilerplate test }); it("should handle deleteTodoItem operation", () => { // ... boilerplate test }); }); ``` ## Enhance the tests The boilerplate tests check that operations are applied, but they don't verify the **actual results**. Extend them to assert on the resulting state. ### Test 1: Update the addTodoItem test The add test is already fairly complete. We just need to add type annotations: ```typescript it("should handle addTodoItem operation", () => { const document = utils.createDocument(); // added-line const input: AddTodoItemInput = generateMock(AddTodoItemInputSchema()); const updatedDocument = reducer(document, addTodoItem(input)); expect(isTodoListDocument(updatedDocument)).toBe(true); expect(updatedDocument.operations.global).toHaveLength(1); expect(updatedDocument.operations.global[0].action.type).toBe( "ADD_TODO_ITEM", ); expect(updatedDocument.operations.global[0].action.input).toStrictEqual( input, ); expect(updatedDocument.operations.global[0].index).toEqual(0); }); ``` ### Test 2: Replace the updateTodoItem test Delete the existing boilerplate and add two separate tests - one for updating text, one for updating the checked state: ```typescript // removed-start it("should handle updateTodoItem operation", () => { const document = utils.createDocument(); const input = generateMock(UpdateTodoItemInputSchema()); const updatedDocument = reducer(document, updateTodoItem(input)); expect(isTodoListDocument(updatedDocument)).toBe(true); // ... }); // removed-end ``` **Add the new test for updating text:** ```typescript it("should handle updateTodoItem operation to update text", () => { // We need there to already be a todo item in the document, // since we want to test updating an existing item const mockItem = generateMock(TodoItemSchema()); // We also need to generate a mock input for the update operation we are testing const input: UpdateTodoItemInput = generateMock(UpdateTodoItemInputSchema()); // Since the mocks are generated with random values, we need to set the `id` // on our mock input to match the `id` of the existing mock item input.id = mockItem.id; // We want to easily check if the item's text was updated to our new value, // so we assign a variable and use that for the mock input's text field const newText = "new text"; input.text = newText; // We are only testing updating the text here, so we want the checked field // on the input to be undefined, i.e. it should not change anything on the existing item input.checked = undefined; // We can pass a different initial state to the `createDocument` utility, // so in this case we pass in an `items` array with our existing item already in it const document = utils.createDocument({ global: { items: [mockItem], }, }); // Create an updated document by applying the reducer with the action and input const updatedDocument = reducer(document, updateTodoItem(input)); // Use our validator to check that the document conforms to the document model schema expect(isTodoListDocument(updatedDocument)).toBe(true); // There should now be one operation in the operations list expect(updatedDocument.operations.global).toHaveLength(1); expect(updatedDocument.operations.global[0].action.type).toBe( "UPDATE_TODO_ITEM", ); // Find the updated item in the items list by its `id` const updatedItem = updatedDocument.state.global.items.find( (item) => item.id === input.id, ); // The item's text should now be updated to be our new text expect(updatedItem?.text).toBe(newText); // The item's `checked` field should be unchanged expect(updatedItem?.checked).toBe(mockItem.checked); }); ``` **Add the new test for updating checked state:** ```typescript it("should handle updateTodoItem operation to update checked", () => { // Generate a mock existing item const mockItem = generateMock(TodoItemSchema()); // Generate a mock input const input: UpdateTodoItemInput = generateMock(UpdateTodoItemInputSchema()); // Set the mock input's `id` to the mock item's `id` input.id = mockItem.id; // We want the new `checked` field value to be the opposite of the // randomly generated value from the mock const newChecked = !mockItem.checked; input.checked = newChecked; // Leave the `text` field unchanged input.text = undefined; // Create a document with the existing item in it const document = utils.createDocument({ global: { items: [mockItem], }, }); // Apply the reducer with the action and the mock input const updatedDocument = reducer(document, updateTodoItem(input)); // Validate your document expect(isTodoListDocument(updatedDocument)).toBe(true); // Get the updated item by its `id` const updatedItem = updatedDocument.state.global.items.find( (item) => item.id === input.id, ); // The item's `text` field should remain unchanged expect(updatedItem?.text).toBe(mockItem.text); // The item's `checked` field should be updated to our new checked value expect(updatedItem?.checked).toBe(newChecked); }); ``` ### Test 3: Update the deleteTodoItem test The boilerplate delete test passes even without an existing item to delete. Let's fix that: ```typescript it("should handle deleteTodoItem operation", () => { // removed-start const document = utils.createDocument(); const input = generateMock(DeleteTodoItemInputSchema()); // removed-end // added-start // Create an existing item to delete const mockItem = generateMock(TodoItemSchema()); const document = utils.createDocument({ global: { items: [mockItem], }, }); const input: DeleteTodoItemInput = generateMock(DeleteTodoItemInputSchema()); input.id = mockItem.id; // added-end const updatedDocument = reducer(document, deleteTodoItem(input)); expect(isTodoListDocument(updatedDocument)).toBe(true); expect(updatedDocument.operations.global).toHaveLength(1); expect(updatedDocument.operations.global[0].action.type).toBe( "DELETE_TODO_ITEM", ); expect(updatedDocument.operations.global[0].action.input).toStrictEqual( input, ); expect(updatedDocument.operations.global[0].index).toEqual(0); // added-start // Verify the item was actually removed const updatedItems = updatedDocument.state.global.items; expect(updatedItems).toHaveLength(0); // added-end }); ``` ## Complete test file Here's the complete test file with all updates. Don't forget to add the missing imports:
Complete todos.test.ts ```typescript AddTodoItemInput, DeleteTodoItemInput, UpdateTodoItemInput, } from "todo-tutorial/document-models/todo-list"; reducer, utils, isTodoListDocument, addTodoItem, AddTodoItemInputSchema, updateTodoItem, UpdateTodoItemInputSchema, deleteTodoItem, DeleteTodoItemInputSchema, TodoItemSchema, } from "todo-tutorial/document-models/todo-list"; describe("Todos Operations", () => { it("should handle addTodoItem operation", () => { const document = utils.createDocument(); const input: AddTodoItemInput = generateMock(AddTodoItemInputSchema()); const updatedDocument = reducer(document, addTodoItem(input)); expect(isTodoListDocument(updatedDocument)).toBe(true); expect(updatedDocument.operations.global).toHaveLength(1); expect(updatedDocument.operations.global[0].action.type).toBe( "ADD_TODO_ITEM", ); expect(updatedDocument.operations.global[0].action.input).toStrictEqual( input, ); expect(updatedDocument.operations.global[0].index).toEqual(0); }); it("should handle updateTodoItem operation to update text", () => { const mockItem = generateMock(TodoItemSchema()); const input: UpdateTodoItemInput = generateMock( UpdateTodoItemInputSchema(), ); input.id = mockItem.id; const newText = "new text"; input.text = newText; input.checked = undefined; const document = utils.createDocument({ global: { items: [mockItem], }, }); const updatedDocument = reducer(document, updateTodoItem(input)); expect(isTodoListDocument(updatedDocument)).toBe(true); expect(updatedDocument.operations.global).toHaveLength(1); expect(updatedDocument.operations.global[0].action.type).toBe( "UPDATE_TODO_ITEM", ); const updatedItem = updatedDocument.state.global.items.find( (item) => item.id === input.id, ); expect(updatedItem?.text).toBe(newText); expect(updatedItem?.checked).toBe(mockItem.checked); }); it("should handle updateTodoItem operation to update checked", () => { const mockItem = generateMock(TodoItemSchema()); const input: UpdateTodoItemInput = generateMock( UpdateTodoItemInputSchema(), ); input.id = mockItem.id; const newChecked = !mockItem.checked; input.checked = newChecked; input.text = undefined; const document = utils.createDocument({ global: { items: [mockItem], }, }); const updatedDocument = reducer(document, updateTodoItem(input)); expect(isTodoListDocument(updatedDocument)).toBe(true); const updatedItem = updatedDocument.state.global.items.find( (item) => item.id === input.id, ); expect(updatedItem?.text).toBe(mockItem.text); expect(updatedItem?.checked).toBe(newChecked); }); it("should handle deleteTodoItem operation", () => { const mockItem = generateMock(TodoItemSchema()); const document = utils.createDocument({ global: { items: [mockItem], }, }); const input: DeleteTodoItemInput = generateMock( DeleteTodoItemInputSchema(), ); input.id = mockItem.id; const updatedDocument = reducer(document, deleteTodoItem(input)); expect(isTodoListDocument(updatedDocument)).toBe(true); expect(updatedDocument.operations.global).toHaveLength(1); expect(updatedDocument.operations.global[0].action.type).toBe( "DELETE_TODO_ITEM", ); const updatedItems = updatedDocument.state.global.items; expect(updatedItems).toHaveLength(0); }); }); ```
**TIP:** To make sure everything works as expected: ```bash # Check types compile correctly pnpm tsc # Check linting passes pnpm lint # Run tests pnpm test # Compare with reference implementation git diff tutorial/step-4-implement-tests-for-todos-operations -- document-models/todo-list/src/tests/ ``` Expected test output: ```bash ✓ document-models/todo-list/src/tests/document-model.test.ts (3 tests) 1ms ✓ document-models/todo-list/src/tests/todos.test.ts (4 tests) 8ms Test Files 2 passed (2) Tests 7 passed (7) ``` ## Up next: Building the editor In the next chapter, you'll learn how to implement a user interface (editor) for your document model so you can interact with it visually. --- ## Build a to-do list editor > Source: https://academy.vetra.io/academy/Build/ManualTodoTutorial/BuildToDoListEditor **TIP:** 📦 **Reference Code**: - **Editor Scaffolding**: [step-5-generate-todo-list-document-editor](https://github.com/powerhouse-inc/todo-tutorial/tree/step-5-generate-todo-list-document-editor) - **Complete Editor UI**: [step-6-add-basic-todo-editor-ui-components](https://github.com/powerhouse-inc/todo-tutorial/tree/step-6-add-basic-todo-editor-ui-components) This tutorial covers two steps: 1. **Step 5**: Generating the editor template with Vetra Studio 2. **Step 6**: Building a complete, interactive UI with components for adding, editing, and deleting todos Compare implementations: `git diff step-5-generate-todo-list-document-editor step-6-add-basic-todo-editor-ui-components`
📖 How to use this tutorial This tutorial shows building from **generated scaffolding** (step-5) to **complete UI** (step-6). ### Compare your generated editor After running `ph generate editor`: ```bash # Compare generated scaffolding with step-5 git diff tutorial/step-5-generate-todo-list-document-editor -- editors/ # View the generated editor template git show tutorial/step-5-generate-todo-list-document-editor:editors/todo-list-editor/editor.tsx ``` ### Compare your custom components After building your UI: ```bash # Compare your complete editor with step-6 git diff tutorial/step-6-add-basic-todo-editor-ui-components -- editors/ # See what was added from scaffolding to complete UI git diff tutorial/step-5-generate-todo-list-document-editor..tutorial/step-6-add-basic-todo-editor-ui-components ``` ### Browse the complete implementation Explore the production-ready component structure: ```bash # List all components in step-6 git ls-tree -r --name-only tutorial/step-6-add-basic-todo-editor-ui-components editors/todo-list-editor/components/ # View a specific component git show tutorial/step-6-add-basic-todo-editor-ui-components:editors/todo-list-editor/components/TodoList.tsx ``` ### Visual comparison with GitHub Desktop After committing your editor code: 1. **Branch** menu → **"Compare to Branch..."** 2. Select `tutorial/step-5-generate-todo-list-document-editor` or `tutorial/step-6-add-basic-todo-editor-ui-components` 3. See all your custom components vs. the reference implementation See step 1 for detailed GitHub Desktop instructions.
Now build the editor: the user interface for the **todo-list** document model. It runs inside Connect to create, update, and delete your todo-list items. ## Add a document editor specification in Vetra Studio. Go back to Vetra Studio and click the 'Add new specification' button in the User Experiences column under 'Editors'. This will create an editor template for your document model. Give the editor the name `todo-list-editor` and select the correct document model. In our case that's the `powerhouse/todo-list` ### Editor implementation options You have several options for styling your editor component: 1. **Default HTML Styling:** Standard HTML tags (`

`, `

`, ` ); } ``` **What's happening here:** - We use a form with `onSubmit` handler for better UX (Enter key support) - We extract the text value from the input field - We dispatch the `addTodoItem` action (generated from our SDL) - We reset the form after submission ### Step 4: Create the Todos list component Create `editors/todo-list-editor/components/Todos.tsx` to render the list of todos: ```tsx type Props = { todos: TodoItem[]; }; /** Shows a list of the todo items in the selected todo list */ export function Todos({ todos }: Props) { const hasTodos = todos.length > 0; if (!hasTodos) { return

Start adding things to your todo list

; } return (
    {todos.map((todo) => (
  • ))}
); } ``` **What's happening here:** - We accept `todos` as a prop (passed from `TodoList` parent) - We show a helpful message if the list is empty - We map over todos and render a `Todo` component for each item ### Step 5: Create the Todo item component Create `editors/todo-list-editor/components/Todo.tsx` for individual todo items with edit and delete functionality: ```tsx useState, type ChangeEventHandler, type FormEventHandler, type MouseEventHandler, } from "react"; deleteTodoItem, updateTodoItem, } from "todo-tutorial/document-models/todo-list"; type Props = { todo: TodoItem; }; /** Displays a single todo item in the selected todo list * * Allows checking/unchecking the todo item. * Allows editing the todo item text. * Allows deleting the todo item. */ export function Todo({ todo }: Props) { const [isEditing, setIsEditing] = useState(false); // Even though this component is for a single todo item and not the whole list, // we can use the exact same hook for dispatching updates to it. // The dispatch function works for any action supported by a TodoList document. const [todoList, dispatch] = useSelectedTodoListDocument(); if (!todoList) return null; const todoId = todo.id; const todoText = todo.text; const todoChecked = todo.checked; const onSubmitUpdateTodoText: FormEventHandler = (event) => { event.preventDefault(); const form = event.currentTarget; const textInput = form.elements.namedItem("todoText") as HTMLInputElement; const text = textInput.value; if (!text) return; // We can use the dispatch function for any of the actions // supported by a TodoList document dispatch(updateTodoItem({ id: todo.id, text })); setIsEditing(false); }; const onChangeTodoChecked: ChangeEventHandler = (event) => { dispatch( updateTodoItem({ id: todo.id, checked: event.target.checked, }), ); }; const onClickDeleteTodo: MouseEventHandler = () => { dispatch(deleteTodoItem({ id: todoId })); }; const onClickEditTodo: MouseEventHandler = () => { setIsEditing(true); }; const onClickCancelEditTodo: MouseEventHandler = () => { setIsEditing(false); }; if (isEditing) return (
); return (
{todoText}
); } ``` **What's happening here:** - We use local state (`isEditing`) to toggle between view and edit modes - We dispatch `updateTodoItem` for both checking and text editing - We dispatch `deleteTodoItem` to remove items - We use TypeScript event handlers for type safety ### Step 6: Create the TodoListName component Finally, create `editors/todo-list-editor/components/TodoListName.tsx` for displaying and editing the document name: ```tsx /** Allows editing the name of the selected todo list */ export function TodoListName() { const [isEditing, setIsEditing] = useState(false); const [selectedTodoList, dispatch] = useSelectedTodoListDocument(); if (!selectedTodoList) return null; const documentName = selectedTodoList.name; const onSubmitEditName: FormEventHandler = (event) => { event.preventDefault(); const form = event.currentTarget; const nameInput = form.elements.namedItem("name") as HTMLInputElement; const name = nameInput.value; if (name) { dispatch(setName(name)); setIsEditing(false); } }; if (isEditing) { return (
); } return (

setIsEditing(true)} > {documentName}

); } ``` **What's happening here:** - We use the `setName` action from `document-model` (a built-in action) - We toggle between viewing and editing the name - Click the name to edit it ## Test your editor Now you can run the Vetra Studio Preview and see the **todo-list** editor in action: ```bash ph vetra --watch ``` In the bottom right corner you'll find a new Document Model that you can create: **todo-list**. Click on it to create a new todo-list document. **INFO:** The editor will update dynamically as you make changes, so you can experiment with styling and functionality while seeing your results appear in Vetra Studio in real-time. **Try it out:** 1. Add some todo items using the input form 2. Click on the document name to edit it 3. Check/uncheck items to mark them as complete 4. Click "Edit" on any item to modify its text 5. Click "Delete" to remove items Congratulations! 🎉 If you managed to follow this tutorial until this point, you have successfully implemented the **todo-list** document model with its reducer operations and editor. ## Compare with the reference implementation The tutorial repository's step-6 branch includes additional enhancements you can explore: **Additional components in step-6:** ``` editors/todo-list-editor/components/ ├── CloseButton.tsx # Editor close functionality ├── UndoRedoButtons.tsx # Operation history navigation └── Stats.tsx # Display metadata (creation/modification times) ``` **View individual components from the reference:** ```bash # See the enhanced TodoList component with all features git show tutorial/step-6-add-basic-todo-editor-ui-components:editors/todo-list-editor/components/TodoList.tsx # Explore the UndoRedoButtons component git show tutorial/step-6-add-basic-todo-editor-ui-components:editors/todo-list-editor/components/UndoRedoButtons.tsx # Compare your implementation with the reference git diff tutorial/step-6-add-basic-todo-editor-ui-components -- editors/todo-list-editor/ ``` **TIP:** To make sure everything works as expected: ```bash # Check types compile correctly pnpm tsc # Check linting passes pnpm lint # Run tests pnpm test # Test in Vetra Studio ph vetra --watch # Compare with reference implementation git diff tutorial/step-6-add-basic-todo-editor-ui-components -- editors/todo-list-editor/ ``` In Connect, you should be able to: - Create a new todo-list document - Add, edit, and delete todo items - Check/uncheck items to mark them complete ## Up next You've built a working package by hand. From here you can go deeper or put it to use: - **Go deeper — [Mastery Track: Document Model Creation](/academy/Build/DocumentModelCreation/WhatIsADocumentModel)** — the theory behind the steps you just followed, plus an advanced version of the todo-list (computed stats), the Document Engineering UI components, and custom Drive Explorers. - **Expose your data — [Work With Data](/academy/Build/WorkWithData/ConfiguringDrives)** — configure drives, query documents over the GraphQL API, and build subgraphs and processors. - **Ship it — [Launch](/academy/Build/Launch/PublishYourProject)** — publish your package and deploy Connect and Switchboard. --- ## What is a document model? > Source: https://academy.vetra.io/academy/Build/DocumentModelCreation/WhatIsADocumentModel **TIP:** This chapter on **Document Model Creation** will help you with an in-depth practical understanding while building an **advanced to-do list** document model. If you have completed the [Manual Todo tutorial](/academy/Build/ManualTodoTutorial/CreateNewPowerhouseProject) (which includes creating a simple to-do list document model), you will revisit familiar topics and can enhance your existing document model with the advanced features covered in this Mastery Track, such as statistics tracking. Although not required, completing the Manual Todo tutorial first is recommended as it provides a hands-on foundation for the concepts explored in depth here. **INFO:** A Document Model is a programmable document structure that defines how data is stored, changed, and interpreted in a decentralized system. It acts like a living blueprint—capturing state, tracking changes, and enabling interaction through defined operations. For instance, an invoice document model might define fields like _issuer_, _lineItems_, and _status_, with operations such as _AddLineItem_ and _MarkAsPaid_. Document models are central to **Specification Driven Design & Development**—an approach where you communicate your solution and intent through structured specification documents. These specs are machine-readable and executable, serving as a shared language between developers, designers, and AI agents. This enables precise, iterative development and lays the groundwork for AI-assisted workflows. A Document Model can be understood as: - A structured software framework that represents and **manages business logic** within a digital environment. - A sophisticated template that **encapsulates the essential aspects of a digital process or a set of data**. - A blueprint that defines how data is **captured, manipulated, and visualised** within a system. - A **standardized way to store, modify, and query data** in scalable, decentralized applications. ### How does a document model function? #### Structure and composition Each document model consists of three key components: 1. **State Schema** – Defines the structure of the document. 2. **Document Operations** – Defines how the document can be modified. 3. **Event History** – Maintains an append-only log of changes. Document models use **event sourcing, CQRS (Command Query Responsibility Segregation), and an append-only architecture** for immutability, auditability, and scalability. --- ## 1. Structure of a document model A document model consists of the following key components: ### 1.1 State schema The **state schema** defines the structure of the document, including its fields and data types. It serves as a blueprint for how data is stored and validated. Example of a **GraphQL-like state schema** for an invoice document: ```graphql type InvoiceState { id: OID! # Unique identifier for the invoice issuer: OID! # Reference to the issuing entity recipient: OID! # Reference to the recipient entity status: String # (value: "DRAFT") # Invoice status dueDate: DateTime # Payment due date lineItems: [LineItem!]! # List of line items totalAmount: Currency # Computed field for total invoice value } type LineItem { id: OID! description: String quantity: Int unitPrice: Currency } ``` ### State schema features: - Uses **GraphQL-like definitions** for a **clear, structured schema**. - Supports **built-in scalars** by name, such as `OID`, `Currency`, and `DateTime`. - Defines **relationships** using object references (`OID!`). The state schema acts as a **template** for document instances. Every new invoice created will follow this structure. --- ### 1.2 Document operations Document models are **append-only**, meaning changes are not made directly to the document state. Instead, **document operations** define valid state transitions. Example operations for modifying an invoice: ```graphql input AddLineItemInput { invoiceId: OID! description: String quantity: Int unitPrice: Currency } input UpdateRecipientInput { invoiceId: OID! newRecipient: OID! } input MarkAsPaidInput { invoiceId: OID! } ``` Each operation **modifies the document state** without altering past data. Instead, a new event is appended to the document history. --- ### 1.3 Event history (append-only log) Every operation applied to a document is **stored as an event** in an append-only log. #### Example event log for an invoice document: ```json [ { "timestamp": 1700000001, "operation": "CREATE_INVOICE", "data": { "id": "inv-001", "issuer": "company-123", "recipient": "client-456" } }, { "timestamp": 1700000100, "operation": "ADD_LINE_ITEM", "data": { "description": "Software Development", "quantity": 10, "unitPrice": 100 } }, { "timestamp": 1700000200, "operation": "MARK_AS_PAID", "data": {} ``` ### Event history benefits: - Provides a **transparent audit trail** of changes. - Enables **time travel debugging** by reconstructing past states. - Supports **event sourcing**, allowing developers to **replay events** to restore state. --- ## 2. How document models work technically Document models in Powerhouse rely on **event-driven architecture, event sourcing, and CQRS principles**. Here's a step-by-step breakdown: ### 2.1 Document creation 1. A user (or system) **submits an operation** to create a new document. 2. The document model **validates** the input data against the state schema. 3. The system **appends the operation** as an event in the document history. 4. The **initial state is computed** by applying the recorded events. **Example:** ```json { "operation": "CREATE_INVOICE", "data": { "id": "inv-001", "issuer": "company-123", "recipient": "client-456" } } ``` --- ### 2.2 Document modification 1. A user submits an **operation** (e.g., `ADD_LINE_ITEM`). 2. The **event is appended** to the document history. 3. The **state transition logic updates the computed state**. 4. The UI re-renders the updated document. **Example:** Adding a line item: ```json { "operation": "ADD_LINE_ITEM", "data": { "description": "Software Development", "quantity": 10, "unitPrice": 100 } } ``` Since changes are **not applied directly**, this model is **highly scalable and auditable**. --- ### 2.3 Querying document models Powerhouse uses **GraphQL queries** to fetch document states efficiently. Because documents store structured data, developers can instantly query: ```graphql query { invoice(id: "inv-001") { issuer recipient status lineItems { description quantity unitPrice } } } ``` This removes the need for **complex database joins** and allows for **fast, structured access to data**. --- ### What document models give you Document models support: - **Automation**: Automate workflows using consistent, structured document logic. - **Auditability**: Maintain a full history of changes for compliance and transparency. - **API Integration**: Connect with Switchboard or external APIs for data exchange. - **Data Analysis**: Get real-time and historical insights through structured read models. - **Version Control**: Track and compare document states over time, similar to Git. - **Collaboration**: Let decentralized teams build, modify, and share documents asynchronously. - **Extensibility**: Add new fields, operations, and integrations over time without rewriting logic. - **Schema Evolution**: Evolve document models with [versioning](/academy/Build/DocumentModelCreation/DocumentModelVersioning): upgrade schemas while keeping backward compatibility with existing documents. ### Up next: How to build a document model Next, you'll build a To-do List document model, with the theory explained as you go. --- ## Specify the state schema > Source: https://academy.vetra.io/academy/Build/DocumentModelCreation/SpecifyTheStateSchema **NOTE:** This is the same step from the [Manual Todo tutorial](/academy/Build/ManualTodoTutorial/DefineToDoListDocumentModel), covered here in depth. The Mastery Track's only addition is the computed `stats` field — if you've done the basic build, skim to the stats parts. The state schema is the backbone of your document model. It defines the structure, data types, and relationships of the information your document will hold. In Powerhouse, you define this schema with the **GraphQL Schema Definition Language (SDL)**. A well-defined state schema keeps data consistent and is what makes the document queryable. **TIP:** Your state schema is more than just a data structure—it's a **specification** that enables **Specification Driven Design & Development**. This schema becomes a machine-readable blueprint that AI agents can interpret and execute, enabling precise collaboration between you and AI throughout the development process. ## Core concepts ### Types At the heart of GraphQL SDL are **types**. Types define the shape of your data. You can define custom object types that represent the entities in your document. For example, in a `TodoList` document, you might have a `TodoListState` type and a `TodoItem` type. - **`TodoListState`**: This could be the root type representing the overall state of the to-do list. It might contain a list of `TodoItem` objects. - **`TodoItem`**: This type would represent an individual to-do item, with properties like an `id`, `text` (the task description), and `checked` (a boolean indicating if the task is completed). ### Fields Each type has **fields**, which represent the properties of that type. Each field has a name and a type. For instance, the `TodoItem` type would have an `id` field of type `OID!`, a `text` field of type `String!`, and a `checked` field of type `Boolean!`. ### Scalars GraphQL has a set of built-in **scalar types**: - `String`: A UTF‐8 character sequence. - `Int`: A signed 32‐bit integer. - `Float`: A signed double-precision floating-point value. - `Boolean`: `true` or `false`. - `ID`: A unique identifier, often used as a key for a field. It is serialized in the same way as a String; however, it is not intended to be human-readable. Powerhouse built-in scalars come from `document-model/scalars` and are available by name in the schema. See the [built-in scalar names](/academy/Build/DocumentModelCreation/CustomScalars#name-the-export-after-the-scalar) for the full list. Editor components for those types are documented in [Document-Engineering](/academy/Reference/EditorsUI/DocumentEngineering). To define a scalar of your own with a zod schema, see [Custom scalars](/academy/Build/DocumentModelCreation/CustomScalars). **TIP:** Powerhouse provides the `OID` (Object ID) scalar for unique identifiers in document models. Use `generateId()` from the document-model core library to generate an id for an `OID` field. ### Lists and non-null You can modify types using lists and non-null indicators: - **Lists**: To indicate that a field will return a list of a certain type, you wrap the type in square brackets, e.g., `[TodoItem!]!`. This means the field `items` in `TodoListState` will be a list of `TodoItem` objects. - **Non-Null**: To indicate that a field cannot be null, you add an exclamation mark `!` after the type name, e.g., `String!`. This means that the `text` field of a `TodoItem` must always have a value. The outer `!` in `[TodoItem!]!` means the list itself cannot be null (it must be at least an empty list), and the inner `!` on `TodoItem!` means that every item within that list must also be non-null. ## Example: TodoList state schema Revisit the `TodoList` example from the "Define the TodoList document specification" step of the Manual Todo tutorial. ### Basic schema (matching the Manual Todo tutorial) This is the same schema you built in the Manual Todo tutorial: ```graphql # The state of our TodoList type TodoListState { items: [TodoItem!]! } # A single to-do item type TodoItem { id: OID! text: String! checked: Boolean! } ``` ### Advanced schema (with statistics tracking) **INFO:** In this Mastery Track, we'll extend the basic schema with a `stats` field to demonstrate how you can add computed statistics to your document model. This is an **optional enhancement** that builds on the foundation from the Manual Todo tutorial. ```graphql # The state of our TodoList (advanced version with stats) type TodoListState { items: [TodoItem!]! stats: TodoListStats! } # A single to-do item type TodoItem { id: OID! text: String! checked: Boolean! } # The statistics on our to-do's (advanced feature) type TodoListStats { total: Int! checked: Int! unchecked: Int! } ``` ### Breakdown: - **`TodoListState` type**: - `items: [TodoItem!]!`: This field defines that our `TodoListState` contains a list called `items`. - `[TodoItem!]`: This signifies that `items` is a list of `TodoItem` objects. - `TodoItem!`: The `!` after `TodoItem` means that no item in the list can be null. Each entry must be a valid `TodoItem`. - The final `!` after `[TodoItem!]` means that the `items` list itself cannot be null. It can be an empty list `[]`, but it cannot be absent. - `stats: TodoListStats!` _(advanced)_: Holds aggregated statistics about the to-do items. - **`TodoItem` type**: - `id: OID!`: Each `TodoItem` has a unique identifier using the built-in `OID` scalar. This is crucial for referencing specific items, for example, when updating or deleting them. - `text: String!`: The textual description of the to-do item. It cannot be null, ensuring every to-do has a description. - `checked: Boolean!`: Indicates whether the to-do item is completed. It defaults to a boolean value (true or false) and cannot be null. - **`TodoListStats` type** _(advanced)_: This type holds the summary statistics for the to-do list. - `total: Int!`: The total count of all to-do items. This field must be an integer and cannot be null. - `checked: Int!`: The number of to-do items that are marked as completed. This must be an integer and cannot be null. - `unchecked: Int!`: The number of to-do items that are still pending. This also must be an integer and cannot be null. ## Best practices for designing your state schema 1. **Start Simple, Iterate**: Begin with the core entities and properties. You can always expand and refine your schema as your understanding of the document's requirements grows. 2. **Clarity and Explicitness**: Name your types and fields clearly and descriptively. This makes the schema easier to understand and maintain. 3. **Use Non-Null Wisely**: Enforce data integrity by using non-null (`!`) for fields that must always have a value. However, be mindful not to over-constrain if a field can genuinely be optional. 4. **Normalize vs. Denormalize**: - **Normalization**: Similar to relational databases, you can normalize your data by having distinct types and linking them via IDs. This can reduce data redundancy. For example, if you had `User` and `TodoItem` and wanted to assign tasks, you might have an `assigneeId` field in `TodoItem` that links to a `User`'s `id`. - **Denormalization**: Sometimes, for performance or simplicity, you might embed data directly. For instance, if user information associated with a to-do item was very simple and only used in that context, you might embed user fields directly in `TodoItem`. - The choice depends on your specific use case, query patterns, and how data is updated. 5. **Consider Future Needs**: While you shouldn't over-engineer, think a little about potential future enhancements. For example, adding a `createdAt: String` or `dueDate: String` field to `TodoItem` might be useful later. 6. **Root State Type**: It's a common pattern to have a single root type for your document state (e.g., `TodoListState`). This provides a clear entry point for accessing all document data. The schema dictates how data is stored, and how it can be queried and mutated through operations. Operations are covered in the next section. ## Practical implementation: defining the state schema in Vetra Studio Now that you understand the concepts behind the state schema, let's put it into practice. This section will guide you through creating a document model specification for the TodoList example discussed above.
Tutorial: The state schema specification ### Prerequisites - You have a Powerhouse project set up. If not, please follow the [Create a new Powerhouse Project](/academy/Build/ManualTodoTutorial/CreateNewPowerhouseProject) tutorial. - Vetra Studio is running. If not, navigate to your project directory in the terminal and run `ph vetra --watch`. ### Steps 1. **Create a New Document Model**: - With Vetra Studio open in your browser, you'll see the Vetra Studio Drive. - Click the **Document Models 'Add new specification'** button to create a new document model specification. 2. **Define Document Metadata**: - **Name**: Give your document model a descriptive name: `TodoList`. **Pay close attention to capitalization, as it influences our code generation.** - **Document Type**: In the 'Document Type' field, enter a unique identifier for this document type: `powerhouse/todo-list`. 3. **Specify the State Schema**: - In the code editor provided, you'll see a template for a GraphQL schema. - Replace the entire content of the editor with the advanced `TodoList` schema we've designed in this chapter: ```graphql # The state of our TodoList (advanced version with stats) type TodoListState { items: [TodoItem!]! stats: TodoListStats! } # A single to-do item type TodoItem { id: OID! text: String! checked: Boolean! } # The statistics on our to-do's (advanced feature) type TodoListStats { total: Int! checked: Int! unchecked: Int! } ``` 4. **Sync Schema and View Initial State**: - After pasting the schema, click the **'Sync with schema'** button. - This action processes your schema and generates an initial JSON state for your document model based on the `TodoListState` type. You can view this initial state, which helps you verify that your schema is structured correctly. For now, you can ignore the "Modules & Operations" section. We will define and implement the operations that modify this state in the upcoming sections of this Mastery Track. By completing these steps, you have successfully specified the data structure for the advanced TodoList document model. The next step is to define the operations that will allow users to interact with and change this state.
Alternatively: Define the state schema in Connect ### Prerequisites - You have a Powerhouse project set up. If not, please follow the [Create a new Powerhouse Project](/academy/Build/ManualTodoTutorial/CreateNewPowerhouseProject) tutorial. - Connect is running. If not, navigate to your project directory in the terminal and run `ph connect`. ### Steps 1. **Create a New Document Model**: - With Connect open in your browser, navigate into your local drive. - At the bottom of the page in the 'New Document' section, click the `DocumentModel` button to create a new document model specification. 2. **Define Document Metadata**: - **Name**: Give your document model a descriptive name: `TodoList`. **Pay close attention to capitalization, as it influences our code generation.** - **Document Type**: In the 'Document Type' field, enter a unique identifier for this document type: `powerhouse/todo-list`. 3. **Specify the State Schema**: - In the code editor provided, you'll see a template for a GraphQL schema. - Replace the entire content of the editor with the advanced `TodoList` schema we've designed in this chapter: ```graphql # The state of our TodoList (advanced version with stats) type TodoListState { items: [TodoItem!]! stats: TodoListStats! } # A single to-do item type TodoItem { id: OID! text: String! checked: Boolean! } # The statistics on our to-do's (advanced feature) type TodoListStats { total: Int! checked: Int! unchecked: Int! } ``` 4. **Sync Schema and View Initial State**: - After pasting the schema, click the **'Sync with schema'** button. - This action processes your schema and generates an initial JSON state for your document model based on the `TodoListState` type. You can view this initial state, which helps you verify that your schema is structured correctly. For now, you can ignore the "Modules & Operations" section. We will define and implement the operations that modify this state in the upcoming sections of this Mastery Track. By completing these steps, you have successfully specified the data structure for the advanced TodoList document model. The next step is to define the operations that will allow users to interact with and change this state.
For a complete, working example, you can always have a look at the [Example TodoList Repository](/academy/Build/DocumentModelCreation/ExampleToDoListRepository) which contains the full implementation of the concepts discussed in this Mastery Track. --- ## Specify document operations > Source: https://academy.vetra.io/academy/Build/DocumentModelCreation/SpecifyDocumentOperations **NOTE:** You defined these operations in the [Manual Todo tutorial](/academy/Build/ManualTodoTutorial/DefineToDoListDocumentModel); this page explains them in depth and adds the operations behind the advanced `stats` feature. In the previous section, we defined the state schema for our document model. Now, we turn our attention to a critical aspect of document model creation: **specifying document operations**. These operations are the heart of your document's behavior, dictating how its state can be modified. ## What are document operations? In Powerhouse, document models adhere to event sourcing principles. This means that every change to a document's state is the result of a sequence of operations (or events). Instead of directly mutating the state, you define specific, named operations that describe the intended change. For example, in our `TodoList` document model, operations might include: - `ADD_TODO_ITEM`: To add a new task. - `UPDATE_TODO_ITEM`: To modify an existing task (e.g., change its text or mark it as completed). - `DELETE_TODO_ITEM`: To remove a task. Each operation acts as a command that, when applied, transitions the document from one state to the next. The complete history of these operations defines the document's journey to its current state. ## Connecting operations to the schema In the "Define TodoList Document Model" step of the Manual Todo tutorial, we used GraphQL `input` types to define the structure of the data required for each operation. Revisit that: ```graphql # Defines a GraphQL input type for adding a new to-do item input AddTodoItemInput { text: String! } # Defines a GraphQL input type for updating a to-do item input UpdateTodoItemInput { id: OID! text: String checked: Boolean } # Defines a GraphQL input type for deleting a to-do item input DeleteTodoItemInput { id: OID! } ``` These `input` types are not just abstract definitions; they are the **specifications for our document operations**. - **`AddTodoItemInput`** specifies that to execute an `ADD_TODO_ITEM` operation, we only need the `text` for the new item. The `id` is automatically generated by the reducer using Powerhouse's `generateId()` function. - **`UpdateTodoItemInput`** specifies that for an `UPDATE_TODO_ITEM` operation, we need the `id` of the item to update, and optionally new `text` or a `checked` status. - **`DeleteTodoItemInput`** specifies that a `DELETE_TODO_ITEM` operation requires the `id` of the item to be removed. **TIP:** Notice that `AddTodoItemInput` only requires `text` — not an `id`. This is because the ID is generated automatically in the reducer using `generateId()` from `document-model/core`. This ensures unique, consistent IDs and follows the pattern used in the [todo-demo repository](https://github.com/powerhouse-inc/todo-demo). Vetra Studio uses these GraphQL input types when you define operations within a module (e.g., the `todos` module with operations `ADD_TODO_ITEM`, `UPDATE_TODO_ITEM`, `DELETE_TODO_ITEM`). ## Designing effective document operations Careful design of your document operations is crucial for a robust and maintainable document model. Here are some key considerations: ### 1. Granularity Operations should be granular enough to represent distinct user intentions or logical changes. - **Too coarse:** An operation like `MODIFY_TODOLIST` that takes a whole new list of items would be too broad. It would be hard to track specific changes and could lead to complex reducer logic. - **Too fine:** While possible, having separate operations like `SET_TODO_ITEM_TEXT` and `SET_TODO_ITEM_CHECKED_STATUS` might be overly verbose if these are often updated together. `UPDATE_TODO_ITEM` with optional fields offers a good balance. - **Just right:** The `ADD_TODO_ITEM`, `UPDATE_TODO_ITEM`, and `DELETE_TODO_ITEM` operations for our `TodoList` are good examples. They represent clear, atomic changes. ### 2. Naming conventions Clear and consistent naming makes your operations understandable. A common convention is `VERB_NOUN` or `VERB_NOUN_SUBJECT`. - Examples: `ADD_ITEM`, `UPDATE_USER_PROFILE`, `ASSIGN_TASK_TO_USER`. - In our case: `ADD_TODO_ITEM`, `UPDATE_TODO_ITEM`, `DELETE_TODO_ITEM`. The name you provide in Vetra Studio (or Connect) (e.g., `ADD_TODO_ITEM`) directly corresponds to the operation type that will be recorded and that your reducers will handle. The base actions reserve seven names: `UNDO`, `REDO`, `SET_NAME`, `SET_PREFERRED_EDITOR`, `NOOP`, `PURGE_DOCUMENT` and `RETRACT`. The check ignores case, so `retract` is reserved too. The document model editor refuses a reserved name, and `ph generate` and Vetra's codegen refuse a specification that contains one. ### 3. Input types (payloads) The input type for an operation (its payload) should contain all the necessary information to perform that operation, and nothing more. - **Completeness:** If an operation needs a user ID to authorize a change, include it in the input. - **Conciseness:** Avoid including data that isn't directly used by the operation. - **Clarity:** Use descriptive field names within your input types. `action.input.text` is clearer than `action.input.t`. The GraphQL `input` types we defined earlier (`AddTodoItemInput`, `UpdateTodoItemInput`, `DeleteTodoItemInput`) serve precisely this purpose. They ensure that whoever triggers an operation provides the correct data in the correct format. ### 4. Immutability and pure functions While not specified in the operation definition itself, remember that the _implementation_ of these operations (the reducers) should treat state as immutable and behave as pure functions. The operation specification (input type) provides the data for these pure functions. ## Role in event sourcing and CQRS - **Events:** Each successfully executed operation is recorded as an event in the document's history. This history provides an audit trail and allows for replaying events to reconstruct state, which is invaluable for debugging and understanding how a document evolved. - **Commands:** Document operations are essentially "commands" in a Command Query Responsibility Segregation (CQRS) pattern. They represent an intent to change the state. The processing of this command (by the reducer) leads to one or more events being stored and the state being updated. ## From specification to implementation Specifying your document operations is the bridge between defining your data structure (the state schema) and implementing the logic that changes that data (the reducers). 1. **You define the state schema** (e.g., `TodoListState`, `TodoItem`). 2. **You specify the operations** that can alter this state, along with their required input data (e.g., `ADD_TODO_ITEM` with `AddTodoItemInput`). 3. **Next, you will implement reducers** for each specified operation. Each reducer will take the current state and an operation's input, and produce a new state. The generated code from `ph generate` (as seen in `03-ImplementOperationReducers.md`) will create a structure for your reducers based on the operations you specified in the Connect application (which, in turn, were based on your GraphQL input types). For example, the `TodoListTodosOperations` type generated by Powerhouse will expect methods corresponding to `addTodoItemOperation`, `updateTodoItemOperation`, and `deleteTodoItemOperation`. ```typescript export const todoListTodosOperations: TodoListTodosOperations = { addTodoItemOperation(state, action) { // Implementation uses action.input which matches AddTodoItemInput }, updateTodoItemOperation(state, action) { // Implementation uses action.input which matches UpdateTodoItemInput }, deleteTodoItemOperation(state, action) { // Implementation uses action.input which matches DeleteTodoItemInput }, }; ``` ## Practical implementation: Defining operations in Vetra Studio Now that you understand the theory, let's walk through the practical steps of defining these operations for our `TodoList` document model within Vetra Studio.
Tutorial: Specifying TodoList operations Assuming you have already defined the state schema for the `TodoList` as covered in the previous section, follow these steps to add the operations: 1. **Create a Module for Operations:** Below the schema editor in Vetra Studio, find the input field labeled `Add module`. Modules help organize your operations. - Type `todos` into the field and press Enter. 2. **Add the `ADD_TODO_ITEM` Operation:** A new field, `Add operation`, will appear under your new module. - Type `ADD_TODO_ITEM` into this field and press Enter. - An editor will appear for the operation's input type. You need to define the data required for this operation. Paste the following GraphQL `input` definition into the editor: ```graphql # Defines a GraphQL input type for adding a new to-do item input AddTodoItemInput { text: String! } ``` :::info Notice we don't include `id` in the input — the reducer will generate it automatically using `generateId()` from `document-model/core`. ::: 3. **Add the `UPDATE_TODO_ITEM` Operation:** - In the `Add operation` field again, type `UPDATE_TODO_ITEM` and press Enter. - Paste the corresponding `input` definition into its editor: ```graphql # Defines a GraphQL input type for updating a to-do item input UpdateTodoItemInput { id: OID! text: String checked: Boolean } ``` 4. **Add the `DELETE_TODO_ITEM` Operation:** - Finally, type `DELETE_TODO_ITEM` in the `Add operation` field and press Enter. - Paste its `input` definition: ```graphql # Defines a GraphQL input type for deleting a to-do item input DeleteTodoItemInput { id: OID! } ``` 5. **Review:** After adding all three operations, your document model specification in Vetra Studio is complete for now. You can see how each operation (`ADD_TODO_ITEM`, etc.) is now explicitly linked to an input type that defines its payload. Vetra Studio automatically tracks your document model specifications. In the next chapter, we will see how the generator is used to create code for our reducers.
Alternatively: Define operations in Connect Assuming you have already defined the state schema for the `TodoList` as covered in the previous section, follow these steps to add the operations in Connect: 1. **Create a Module for Operations:** Below the schema editor in Connect, find the input field labeled `Add module`. Modules help organize your operations. - Type `todos` into the field and press Enter. 2. **Add the `ADD_TODO_ITEM` Operation:** A new field, `Add operation`, will appear under your new module. - Type `ADD_TODO_ITEM` into this field and press Enter. - An editor will appear for the operation's input type. You need to define the data required for this operation. Paste the following GraphQL `input` definition into the editor: ```graphql # Defines a GraphQL input type for adding a new to-do item input AddTodoItemInput { text: String! } ``` :::info Notice we don't include `id` in the input — the reducer will generate it automatically using `generateId()` from `document-model/core`. ::: 3. **Add the `UPDATE_TODO_ITEM` Operation:** - In the `Add operation` field again, type `UPDATE_TODO_ITEM` and press Enter. - Paste the corresponding `input` definition into its editor: ```graphql # Defines a GraphQL input type for updating a to-do item input UpdateTodoItemInput { id: OID! text: String checked: Boolean } ``` 4. **Add the `DELETE_TODO_ITEM` Operation:** - Finally, type `DELETE_TODO_ITEM` in the `Add operation` field and press Enter. - Paste its `input` definition: ```graphql # Defines a GraphQL input type for deleting a to-do item input DeleteTodoItemInput { id: OID! } ``` 5. **Review and Export:** After adding all three operations, your document model specification in Connect is complete for now. You can see how each operation (`ADD_TODO_ITEM`, etc.) is now explicitly linked to an input type that defines its payload. The next step in a real project would be to click the `Export` button to save this specification file. In the next chapter, we will see how this exported file is used to generate code for our reducers.
## Conclusion Specifying document operations is a foundational step in building robust and predictable document models in Powerhouse. By clearly defining the **"what" (the operation and its input)** before implementing the **"how" (the reducer logic)**, you create a clear contract for state transitions. This approach enhances type safety, testability, and the overall maintainability of your document model. In the next section, we will dive deeper into the implementation of the reducer functions for these specified operations. --- ## Use the Document Model Generator > Source: https://academy.vetra.io/academy/Build/DocumentModelCreation/UseTheDocumentModelGenerator When building document models with **Vetra Studio**, code generation happens automatically. As you add and update specification documents in your Vetra Studio Drive, Vetra monitors your changes and generates the necessary scaffolding in real-time. You'll receive updates directly in your terminal as Vetra processes your specifications. This article covers the **manual code generation method** using the `ph generate` command—an alternative approach that remains available when working with **Connect** and exported `.phd` specification files. While Vetra Studio is the recommended workflow for most developers, understanding the generator command provides useful context for how the scaffolding works under the hood. ## When to Use Manual Generation | Workflow | Code Generation | | ---------------- | -------------------------------------------------------------------------- | | **Vetra Studio** | Automatic—Vetra watches your specifications and generates code as you work | | **Connect** | Manual—Export a `.phd` file and run `ph generate` | If you're using Vetra Studio with `ph vetra --interactive`, you don't need to run any generation commands. Vetra handles everything for you, prompting for confirmation before processing changes. ## Prerequisites (Connect Workflow Only) If you're using the Connect workflow and need to manually generate code: 1. **Powerhouse CLI (`ph-cmd`) Installed:** The generator is part of the Powerhouse CLI. If you haven't installed it, see [Prerequisites](/academy/Build/GettingStartedBuilding/Prerequisites). 2. **Exported `.phd` File:** You must have exported your document model specification from Connect as a `.phd` file (e.g., `TodoList.phd`). ## The Generate Command The core command to invoke the Document Model Generator is: ```bash ph generate document-model --document ``` Replace `` with the actual filename of your exported document model specification. For instance, if your exported file is named `TodoList.phd`, the command would be: ```bash ph generate document-model --document TodoList.phd ``` When executed, this command reads and parses the specification file and generates a set of files and directories within your Powerhouse project. ## Understanding the Generated Artifacts Whether generated automatically by Vetra or manually via `ph generate`, the output structure is the same. Understanding these artifacts helps you work effectively with your document model. The generator creates a new directory specific to your document model, located at: `document-models//` For example, using `TodoList.phd` would result in a directory structure under `document-models/todo-list/`. Inside this directory, you will find: ### 1. Specification Files - **`todo-list.json`**: A JSON representation of your document model specification containing the parsed schema, operation definitions, document type, and metadata. - **`schema.graphql`**: The raw GraphQL Schema Definition Language (SDL) for both the state and operations—a human-readable reference of your schema. ### 2. The `gen/` Directory (Auto-Generated Code) This directory houses all code automatically generated from your specification. **Do not manually edit files within the `gen/` directory**—they will be overwritten when the model is regenerated. Key files include: - **`types.ts`**: TypeScript interfaces derived from your GraphQL schema, including types for your document's state (e.g., `TodoListState`), complex types (e.g., `TodoItem`), and operation inputs (e.g., `AddTodoItemInput`). - **`creators.ts`**: Action creator functions for each operation. Instead of manually constructing action objects, you use functions like `addTodoItem({ text: 'Buy groceries' })`. - **`utils.ts`**: Utility functions including helpers to create initial document instances (e.g., `utils.createDocument()`). - **`reducer.ts`**: A TypeScript interface defining the expected shape of your reducer implementation. ### 3. The `src/` Directory (Your Implementation) This is where you write custom logic. Unlike `gen/`, these files are meant for manual editing. - **`reducers/`**: Contains skeleton reducer files (e.g., `todos.ts`) with function stubs for each operation that you implement with state transition logic. - **`tests/`**: Test files for your reducer logic. ## Versioned Document Models When your document model needs to evolve over time—adding new fields, operations, or changing behavior—you can use **versioning**. This allows multiple versions of the same document model to coexist, with automatic upgrade paths between them. ### Enabling Versioning Versioning is enabled by default. Simply run: ```bash ph generate document-model --document TodoList.phd ``` ### Versioned Folder Structure With versioning enabled, the generator creates a different structure: ``` document-models/ └── todo/ ├── v1/ # Version 1 code │ ├── gen/ # Auto-generated v1 types and utilities │ ├── src/reducers/ # V1 reducer implementations │ └── module.ts # Exports DocumentModelModule with version: 1 ├── v2/ # Version 2 code │ ├── gen/ │ ├── src/reducers/ │ └── module.ts # Exports DocumentModelModule with version: 2 ├── upgrades/ # Migration logic │ ├── versions.ts # Supported versions list │ ├── v2.ts # Upgrade reducer: v1 → v2 │ └── upgrade-manifest.ts └── document-models.ts # Exports all versions + manifests ``` ### Key Differences with Versioning | Standard Generation | Versioned Generation | | ------------------------------- | ----------------------------------------- | | Single `gen/` and `src/` folder | Separate `v1/`, `v2/` folders per version | | One reducer implementation | Version-specific reducers | | No upgrade logic | `upgrades/` folder with manifests | | Direct module export | Exports all versions + upgrade manifests | For comprehensive documentation on implementing versioning, including upgrade reducers and integration with Connect and Switchboard, see [Document Model Versioning](/academy/Build/DocumentModelCreation/DocumentModelVersioning). ## Benefits of Generated Scaffolding The generation process—whether automatic via Vetra or manual via `ph generate`—provides: 1. **Reduced Boilerplate:** Automates creation of type definitions, action creators, and utilities. 2. **Type Safety:** TypeScript types from your GraphQL schema catch errors at compile-time. 3. **Consistency:** Standardized project structure across document models. 4. **Accelerated Development:** Focus on business logic instead of foundational plumbing. 5. **Ecosystem Alignment:** Generated code integrates seamlessly with the Powerhouse ecosystem. 6. **Single Source of Truth:** Code stays synchronized with your specification. 7. **Version Support:** Optional versioning enables safe schema evolution over time. ## Practical Examples ### Using Vetra Studio (Recommended) When using Vetra Studio, code generation is automatic: 1. **Start Vetra in Interactive Mode:** ```bash ph vetra --interactive ``` 2. **Create Your Document Model:** Define your `TodoList` document model in the Vetra Studio Drive—either manually or with AI assistance through Claude and the Reactor MCP. 3. **Watch the Terminal:** As you add specifications, Vetra automatically detects changes and generates scaffolding. In interactive mode, you'll be prompted to confirm before generation proceeds. 4. **Explore Generated Files:** Once complete, find your generated files at `document-models/todo-list/`: - `todo-list.json` and `schema.graphql`: Your model definition - `gen/`: Type-safe generated code - `src/reducers/todos.ts`: Skeleton reducer functions ready for implementation ### Using Connect (Alternative Method)
Tutorial: Manual Generation with Connect This approach is useful when working with Connect's Document Model Editor or when you need explicit control over the generation process. #### Prerequisites - **`TodoList.phd` file**: Your document model specification exported from Connect. #### Steps 1. **Place the Specification File in Your Project:** Navigate to your Powerhouse project root and copy your `TodoList.phd` file there. 2. **Run the Generator Command:** ```bash ph generate document-model --document TodoList.phd ``` 3. **Explore the Generated Files:** After the command completes, find the new directory at `document-models/todo-list/`: - `todo-list.json` and `schema.graphql`: The definition of your model - `gen/`: Type-safe generated code including `types.ts`, `creators.ts`, etc. - `src/`: Implementation skeleton, including `src/reducers/todos.ts` with empty functions for `addTodoItemOperation`, `updateTodoItemOperation`, and `deleteTodoItemOperation`
## Next Steps With your document model scaffolded, the next step is implementing the reducer logic in `document-models/todo-list/src/reducers/todos.ts`. Each reducer function takes the current state and action input, returning the new document state. Subsequently, write unit tests for your reducers to ensure they behave correctly. This cycle of defining, generating, implementing, and testing forms the core loop of document model development in Powerhouse. --- ## Implement document reducers > Source: https://academy.vetra.io/academy/Build/DocumentModelCreation/ImplementDocumentReducers **NOTE:** This is the same step from the [Manual Todo tutorial](/academy/Build/ManualTodoTutorial/ImplementOperationReducers), covered here in depth. The Mastery Track's only addition is the reducer logic for the computed `stats` field — if you've done the basic build, skim to the stats parts. ## The heart of document logic In our journey through Powerhouse Document Model creation, we've defined the "what" – the structure of our data ([State Schema](02-SpecifyTheStateSchema.md)) and the ways it can be changed ([Document Operations](03-SpecifyDocumentOperations.md)). We've also seen how the [Document Model Generator](04-UseTheDocumentModelGenerator.md) translates these specifications into a coded scaffold. Now, we arrive at the "how": implementing **Document Reducers**. Reducers are the core logic units of your document model. They are the functions that take the current state of your document and an operation (an "action"), and then determine the _new_ state of the document. They are the embodiment of your business rules and the engine that drives state transitions in a predictable, auditable, and immutable way. ## Recap: The journey to reducer implementation Before diving into the specifics of writing reducers, let's recall the preceding steps: 1. **State Schema Definition**: You designed the GraphQL `type` definitions for your document's data structure (e.g., `TodoListState`, `TodoItem`). 2. **Document Operation Specification**: You defined the GraphQL `input` types that specify the parameters for each allowed modification to your document (e.g., `AddTodoItemInput`, `UpdateTodoItemInput`). These were then associated with named operations (e.g., `ADD_TODO_ITEM`) in the Connect application. 3. **Code Generation**: You used `ph generate document-model --document ` to create the necessary TypeScript types, action creators, and, crucially, the skeleton file for your reducers (typically `document-models//src/reducers/todos.ts`). This generated reducer file is our starting point. It will contain function stubs or an object structure expecting your reducer implementations, all typed according to your schema. ## What is a reducer? ### The core principles In the context of Powerhouse and inspired by patterns like Redux, a reducer is a **pure function** with the following signature (conceptually): `(currentState, action) => newState` Let's break down its components and principles: - **`currentState`**: This is the complete, current state of your document model instance before the operation is applied. It's crucial to treat this as **immutable**. - **`action`**: This is an object describing the operation to be performed. It typically has: - A `type` property: A string identifying the operation (e.g., `'ADD_TODO_ITEM'`). - An `input` property (or similar, like `payload`): An object containing the data necessary for the operation, matching the GraphQL `input` type you defined (e.g., `{ text: 'Buy groceries' }` for `AddTodoItemInput`). - **`newState`**: The reducer must return a _new_ state object representing the state after the operation has been applied. If the operation does not result in a state change, the reducer should return the `currentState` object itself. ### Key principles guiding reducer implementation: 1. **Purity**: - **Deterministic**: Given the same `currentState` and `action`, a reducer must _always_ produce the same `newState`. - **No Side Effects**: Reducers must not perform any side effects. This means no API calls, no direct DOM manipulation, no `Math.random()` (unless seeded deterministically for specific testing scenarios), and no modification of variables outside their own scope. Their sole job is to compute the next state. 2. **Immutability**: - **Never Mutate `currentState`**: You must never directly modify the `currentState` object or any of its nested properties. - **Always Return a New Object for Changes**: If the state changes, you must create and return a brand new object. If the state does not change, you return the original `currentState` object. - This is fundamental to Powerhouse's event sourcing architecture, enabling time travel, efficient change detection, and a clear audit trail. :::tip[Powerhouse uses Immer.js] Powerhouse uses **Immer.js** under the hood, which means you can write code that _looks like_ it's mutating the state directly (e.g., `state.items.push(...)`), but Immer ensures it results in an immutable update. This gives you the best of both worlds: readable code and immutable state. ::: 3. **Single Source of Truth**: The document state managed by reducers is the single source of truth for that document instance. All UI rendering and data queries are derived from this state. 4. **Delegation to specific operation handlers**: While you can write one large reducer that uses a `switch` statement or `if/else if` blocks based on `action.type`, Powerhouse's generated code typically encourages a more modular approach. You'll often implement a separate function for each operation, which are then combined into a main reducer object or map. The `ph generate` command usually sets up this structure for you. For example, in your `document-models/todo-list/src/reducers/todos.ts`, you'll find an object structure like this: ```typescript import type { TodoListTodosOperations } from "todo-tutorial/document-models/todo-list"; export const todoListTodosOperations: TodoListTodosOperations = { addTodoItemOperation(state, action) { // Your logic for ADD_TODO_ITEM }, updateTodoItemOperation(state, action) { // Your logic for UPDATE_TODO_ITEM }, deleteTodoItemOperation(state, action) { // Your logic for DELETE_TODO_ITEM }, }; ``` The `TodoListTodosOperations` type is generated by Powerhouse and ensures your reducer object correctly implements all defined operations. The `state` and `action` parameters within these methods will also be strongly typed based on your schema. ## Implementing reducer logic: A practical guide Let's use our familiar `TodoList` example to illustrate common patterns. ### Basic implementation (matching the Manual Todo tutorial) The basic implementation matches what you built in the Manual Todo tutorial: ```typescript export const todoListTodosOperations: TodoListTodosOperations = { addTodoItemOperation(state, action) { // Generate a unique ID for the new todo item const id = generateId(); // Add the new item to the state (Immer handles immutability) state.items.push({ ...action.input, id, checked: false }); }, updateTodoItemOperation(state, action) { // Find the item to update by its ID const item = state.items.find((item) => item.id === action.input.id); // Return early if item not found if (!item) return; // Update only the fields that are provided (partial update) item.text = action.input.text ?? item.text; item.checked = action.input.checked ?? item.checked; }, deleteTodoItemOperation(state, action) { // Filter out the item with the matching ID state.items = state.items.filter((item) => item.id !== action.input.id); }, }; ``` **INFO:** Notice that `addTodoItemOperation` uses `generateId()` from `document-model/core` to create a unique ID. This is the recommended pattern — the ID is generated in the reducer, not passed from the UI. This ensures consistent, unique IDs across all operations. ### Advanced implementation (with statistics tracking) **INFO:** This section extends the basic reducers with statistics tracking, matching the advanced schema from the previous section. This demonstrates how to update computed/derived state alongside your primary data. For the advanced version with `stats`, we need to update the statistics whenever items are added, updated, or deleted: ```typescript export const todoListTodosOperations: TodoListTodosOperations = { addTodoItemOperation(state, action) { // Generate a unique ID for the new todo item const id = generateId(); // Update statistics state.stats.total += 1; state.stats.unchecked += 1; // Add the new item to the state state.items.push({ id, text: action.input.text, checked: false, // New items always start as unchecked }); }, updateTodoItemOperation(state, action) { // Find the specific item we want to update const item = state.items.find((item) => item.id === action.input.id); if (!item) { throw new Error(`Item with id ${action.input.id} not found`); } // Update text if provided if (action.input.text !== undefined) { item.text = action.input.text; } // Handle checked status changes and update stats if ( action.input.checked !== undefined && action.input.checked !== item.checked ) { if (action.input.checked) { state.stats.unchecked -= 1; state.stats.checked += 1; } else { state.stats.unchecked += 1; state.stats.checked -= 1; } item.checked = action.input.checked; } }, deleteTodoItemOperation(state, action) { // Find the item to determine its checked status for stats const item = state.items.find((item) => item.id === action.input.id); if (item) { // Update statistics state.stats.total -= 1; if (item.checked) { state.stats.checked -= 1; } else { state.stats.unchecked -= 1; } } // Remove the item from the list state.items = state.items.filter((item) => item.id !== action.input.id); }, }; ``` ### Common patterns explained #### 1. Adding an item ```typescript addTodoItemOperation(state, action) { const id = generateId(); // Generate unique ID state.items.push({ ...action.input, id, checked: false }); } ``` - We use `generateId()` to create a unique identifier - We spread `action.input` to get the text, add the generated ID and default `checked: false` - With Immer, this "mutation" is actually immutable #### 2. Updating an item ```typescript updateTodoItemOperation(state, action) { const item = state.items.find((item) => item.id === action.input.id); if (!item) return; item.text = action.input.text ?? item.text; item.checked = action.input.checked ?? item.checked; } ``` - We find the item by ID - We use nullish coalescing (`??`) to only update fields that were provided - This allows partial updates (e.g., just changing `checked` without touching `text`) #### 3. Deleting an item ```typescript deleteTodoItemOperation(state, action) { state.items = state.items.filter((item) => item.id !== action.input.id); } ``` - We use `filter` to create a new array without the deleted item - Immer handles making this immutable ## Leveraging generated types As highlighted in [Using the Document Model Generator](04-UseTheDocumentModelGenerator.md), `ph generate` produces TypeScript types for your state (e.g., `TodoListState`, `TodoItem`) and the inputs for your operations (e.g., `AddTodoItemInput`, `UpdateTodoItemInput`). **Always use these generated types in your reducer implementations!** ```typescript export const todoListTodosOperations: TodoListTodosOperations = { addTodoItemOperation(state, action) { // TypeScript knows action.input has { text: string } const id = generateId(); state.items.push({ id, text: action.input.text, checked: false }); }, // ... other reducers }; ``` Using these types provides: - **Compile-time safety**: Catch errors related to incorrect property names or data types before runtime. - **Autocompletion and IntelliSense**: Improved developer experience in your IDE. - **Clearer code**: Types serve as documentation for the expected data structures. ## Practical implementation: Writing the `TodoList` reducers Now that you understand the principles, let's put them into practice by implementing the reducers for our `TodoList` document model.
Tutorial: Implementing the TodoList reducers This tutorial assumes you have followed the steps in the previous chapters, especially using `ph generate document-model --document TodoList.phd` to scaffold your document model's code. ### Implement the operation reducers Navigate to `document-models/todo-list/src/reducers/todos.ts`. The generator will have created a skeleton file. Replace its contents with the following logic. **Basic version (without stats):** ```typescript export const todoListTodosOperations: TodoListTodosOperations = { addTodoItemOperation(state, action) { const id = generateId(); state.items.push({ ...action.input, id, checked: false }); }, updateTodoItemOperation(state, action) { const item = state.items.find((item) => item.id === action.input.id); if (!item) return; item.text = action.input.text ?? item.text; item.checked = action.input.checked ?? item.checked; }, deleteTodoItemOperation(state, action) { state.items = state.items.filter((item) => item.id !== action.input.id); }, }; ``` **Advanced version (with stats):** ```typescript export const todoListTodosOperations: TodoListTodosOperations = { addTodoItemOperation(state, action) { const id = generateId(); state.stats.total += 1; state.stats.unchecked += 1; state.items.push({ id, text: action.input.text, checked: false, }); }, updateTodoItemOperation(state, action) { const item = state.items.find((item) => item.id === action.input.id); if (!item) { throw new Error(`Item with id ${action.input.id} not found`); } if (action.input.text !== undefined) { item.text = action.input.text; } if ( action.input.checked !== undefined && action.input.checked !== item.checked ) { if (action.input.checked) { state.stats.unchecked -= 1; state.stats.checked += 1; } else { state.stats.unchecked += 1; state.stats.checked -= 1; } item.checked = action.input.checked; } }, deleteTodoItemOperation(state, action) { const item = state.items.find((item) => item.id === action.input.id); if (item) { state.stats.total -= 1; if (item.checked) { state.stats.checked -= 1; } else { state.stats.unchecked -= 1; } } state.items = state.items.filter((item) => item.id !== action.input.id); }, }; ```
## Reducers and the event sourcing model Every time a reducer processes an operation and returns a new state, Powerhouse records the original operation (the "event") in an append-only log associated with the document instance. The current state of the document is effectively a "fold" or "reduction" of all past events, applied sequentially by the reducers. This is why purity and immutability are so critical: - **Purity** ensures that replaying the same sequence of events will always yield the exact same final state. - **Immutability** ensures that each event clearly defines a discrete state transition, making it easy to audit changes and understand the document's history. ## Conclusion Implementing document reducers is where you breathe life into your document model's specification. By adhering to the principles of purity and immutability, and by leveraging the type safety provided by Powerhouse's code generation, you can build predictable, testable, and maintainable business logic. These reducers form the immutable backbone of your document's state management, perfectly aligning with the event sourcing architecture that underpins Powerhouse. With your reducers implemented, your document model is now functionally complete from a data manipulation perspective. The next chapter covers how to write tests for this logic to ensure its correctness and reliability. --- ## Implement document model tests > Source: https://academy.vetra.io/academy/Build/DocumentModelCreation/ImplementDocumentModelTests **NOTE:** This is the same step from the [Manual Todo tutorial](/academy/Build/ManualTodoTutorial/WriteDocumentModelTests), covered here in depth. The Mastery Track's only addition is tests for the computed `stats` field — if you've done the basic build, skim to the stats parts. ## Ensuring robustness and reliability In the previous chapter, we implemented the core reducer logic for our document model. Now, we reach a critical stage that underpins the reliability and correctness of our entire model: **Implementing Document Model Tests**. Testing is not an afterthought; it's an integral part of the development lifecycle, especially in systems like Powerhouse where data integrity and predictable state transitions are paramount. Well-crafted tests serve as a safety net, allowing you to refactor and extend your document model with confidence. This document provides a practical, hands-on tutorial for testing the `TodoList` document model reducers you have just built. ## Practical implementation: Writing and running the TodoList tests This tutorial assumes you have implemented the `TodoList` reducers as described in the previous chapter and that the code generator has created a test file skeleton at `document-models/todo-list/src/tests/todos.test.ts`.
Tutorial: Implementing and running the TodoList reducer tests ### 1. Implement the reducer tests With the reducer logic in place, it's critical to test it. Navigate to the generated test file at `document-models/todo-list/src/tests/todos.test.ts` and replace its contents with comprehensive tests. This suite tests each operation, verifying not only that the `items` array is correct, but also that the operation itself is recorded properly in the document's history. **Basic tests (matching the Manual Todo tutorial):** ```typescript AddTodoItemInput, DeleteTodoItemInput, UpdateTodoItemInput, } from "todo-tutorial/document-models/todo-list"; reducer, utils, isTodoListDocument, addTodoItem, AddTodoItemInputSchema, updateTodoItem, UpdateTodoItemInputSchema, deleteTodoItem, DeleteTodoItemInputSchema, TodoItemSchema, } from "todo-tutorial/document-models/todo-list"; describe("Todos Operations", () => { it("should handle addTodoItem operation", () => { const document = utils.createDocument(); const input: AddTodoItemInput = generateMock(AddTodoItemInputSchema()); const updatedDocument = reducer(document, addTodoItem(input)); expect(isTodoListDocument(updatedDocument)).toBe(true); // Verify the operation was recorded expect(updatedDocument.operations.global).toHaveLength(1); expect(updatedDocument.operations.global[0].action.type).toBe( "ADD_TODO_ITEM", ); expect(updatedDocument.operations.global[0].action.input).toStrictEqual( input, ); expect(updatedDocument.operations.global[0].index).toEqual(0); }); it("should handle updateTodoItem operation to update text", () => { const mockItem = generateMock(TodoItemSchema()); const input: UpdateTodoItemInput = generateMock( UpdateTodoItemInputSchema(), ); input.id = mockItem.id; const newText = "new text"; input.text = newText; input.checked = undefined; const document = utils.createDocument({ global: { items: [mockItem], }, }); const updatedDocument = reducer(document, updateTodoItem(input)); expect(isTodoListDocument(updatedDocument)).toBe(true); // Verify the operation was recorded expect(updatedDocument.operations.global).toHaveLength(1); expect(updatedDocument.operations.global[0].action.type).toBe( "UPDATE_TODO_ITEM", ); // Verify the state was updated correctly const updatedItem = updatedDocument.state.global.items.find( (item) => item.id === input.id, ); expect(updatedItem?.text).toBe(newText); expect(updatedItem?.checked).toBe(mockItem.checked); }); it("should handle updateTodoItem operation to update checked", () => { const mockItem = generateMock(TodoItemSchema()); const input: UpdateTodoItemInput = generateMock( UpdateTodoItemInputSchema(), ); input.id = mockItem.id; const newChecked = !mockItem.checked; input.checked = newChecked; input.text = undefined; const document = utils.createDocument({ global: { items: [mockItem], }, }); const updatedDocument = reducer(document, updateTodoItem(input)); expect(isTodoListDocument(updatedDocument)).toBe(true); const updatedItem = updatedDocument.state.global.items.find( (item) => item.id === input.id, ); expect(updatedItem?.text).toBe(mockItem.text); expect(updatedItem?.checked).toBe(newChecked); }); it("should handle deleteTodoItem operation", () => { const mockItem = generateMock(TodoItemSchema()); const document = utils.createDocument({ global: { items: [mockItem], }, }); const input: DeleteTodoItemInput = generateMock( DeleteTodoItemInputSchema(), ); input.id = mockItem.id; const updatedDocument = reducer(document, deleteTodoItem(input)); expect(isTodoListDocument(updatedDocument)).toBe(true); // Verify the operation was recorded expect(updatedDocument.operations.global).toHaveLength(1); expect(updatedDocument.operations.global[0].action.type).toBe( "DELETE_TODO_ITEM", ); // Verify the item was removed from state const updatedItems = updatedDocument.state.global.items; expect(updatedItems).toHaveLength(0); }); }); ``` **Advanced tests (with stats verification):** **INFO:** If you implemented the advanced version with statistics tracking, add these additional tests to verify the stats are updated correctly. ```typescript describe("Todos Operations with Stats", () => { it("should update stats when adding a todo item", () => { const document = utils.createDocument(); const input = { text: "Buy milk" }; const updatedDocument = reducer(document, addTodoItem(input)); expect(updatedDocument.state.global.items).toHaveLength(1); expect(updatedDocument.state.global.stats.total).toBe(1); expect(updatedDocument.state.global.stats.unchecked).toBe(1); expect(updatedDocument.state.global.stats.checked).toBe(0); }); it("should update stats when checking a todo item", () => { const document = utils.createDocument(); // Add an item first const addedDocument = reducer(document, addTodoItem({ text: "Buy milk" })); const itemId = addedDocument.state.global.items[0].id; // Now check it const updatedDocument = reducer( addedDocument, updateTodoItem({ id: itemId, checked: true }), ); expect(updatedDocument.state.global.stats.total).toBe(1); expect(updatedDocument.state.global.stats.unchecked).toBe(0); expect(updatedDocument.state.global.stats.checked).toBe(1); }); it("should update stats when deleting an unchecked todo item", () => { const document = utils.createDocument(); // Add an item const addedDocument = reducer(document, addTodoItem({ text: "Buy milk" })); const itemId = addedDocument.state.global.items[0].id; // Delete it const updatedDocument = reducer( addedDocument, deleteTodoItem({ id: itemId }), ); expect(updatedDocument.state.global.items).toHaveLength(0); expect(updatedDocument.state.global.stats.total).toBe(0); expect(updatedDocument.state.global.stats.unchecked).toBe(0); expect(updatedDocument.state.global.stats.checked).toBe(0); }); it("should update stats when deleting a checked todo item", () => { const document = utils.createDocument(); // Add and check an item const addedDocument = reducer(document, addTodoItem({ text: "Buy milk" })); const itemId = addedDocument.state.global.items[0].id; const checkedDocument = reducer( addedDocument, updateTodoItem({ id: itemId, checked: true }), ); // Delete it const updatedDocument = reducer( checkedDocument, deleteTodoItem({ id: itemId }), ); expect(updatedDocument.state.global.items).toHaveLength(0); expect(updatedDocument.state.global.stats.total).toBe(0); expect(updatedDocument.state.global.stats.checked).toBe(0); }); }); ``` ### 2. Run the tests Now, run the tests from your project's root directory to verify your implementation. ```bash pnpm run test ``` Or with npm: ```bash npm test ``` If all tests pass, you have successfully verified the core logic of your `TodoList` document model. This ensures that the reducers you wrote behave exactly as expected.
## Best practices for document model tests While the tutorial provides a concrete example, keep these general best practices in mind when writing your tests: - **Isolate Tests**: Each `it` block should ideally test one specific aspect or scenario. `beforeEach` is crucial for resetting state between tests. - **Descriptive Names**: Name your `describe` and `it` blocks clearly so they explain what's being tested. - **AAA Pattern (Arrange, Act, Assert)**: - **Arrange**: Set up the initial state and any required test data (e.g., using `utils.createDocument()` and defining `input` objects). - **Act**: Execute the operation by calling the `reducer` with an action from a `creator`. - **Assert**: Check if the outcome is as expected using `expect()`. - **Test Immutability**: A key assertion is to ensure the state is not mutated directly. You can check that a new array or object was created: `expect(newState.items).not.toBe(oldState.items);`. - **Cover Edge Cases**: Test what happens when an operation receives invalid input (e.g., trying to update an item that doesn't exist). Your test should confirm the reducer either throws an error or returns the state unchanged, depending on your implementation. - **Run Tests Frequently**: Integrate testing into your development workflow. Run tests after making changes to ensure you haven't broken anything. The `pnpm run test` (or `npm test`) command is your friend. ## Conclusion: The payoff of diligent testing Implementing comprehensive tests for your document model reducers is an investment that pays dividends in the long run. It leads to: - **Higher Quality Models**: More reliable and robust document models with fewer bugs. - **Increased Confidence**: Ability to make changes and refactor code without fear of breaking existing functionality. - **Easier Debugging**: When tests fail, they pinpoint the exact operation and scenario that's problematic. - **Better Collaboration**: Tests clarify the intended behavior of the document model for all team members. By following the tutorial and applying these best practices, you can build a strong suite of tests that safeguard the integrity and functionality of your document models. This diligence is a hallmark of a "Mastery Track" developer, ensuring that the solutions you build are not just functional but also stable, maintainable, and trustworthy. ## Up next In the next chapter of the Mastery Track - Building User Experiences you will learn how to implement an [editor](/academy/Build/BuildingUserExperiences/BuildingDocumentEditors) for your document model so you can see a simple user interface for the **TodoList** document model in action. For a complete, working example, you can always have a look at the [Example TodoList Repository](/academy/Build/DocumentModelCreation/ExampleToDoListRepository) which contains the full implementation of the concepts discussed in this Mastery Track. --- ## Example: Todo-demo-package > Source: https://academy.vetra.io/academy/Build/DocumentModelCreation/ExampleToDoListRepository **INFO:** The Todo-demo is maintained by the Powerhouse Team and serves as a reference for testing and introducing new features. It will be continuously updated alongside the accompanying documentation. https://github.com/powerhouse-inc/todo-demo There are several ways to explore this package: ### Option 1: Rebuild the Todo-demo The Todo-demo and repository are your main reference points during the Mastery Track. Follow the steps in the "Mastery Track – Document Model Creation" chapters to build along with the examples. Key patterns used in the repository: - **Naming convention**: `TodoList`, `TodoItem`, `TodoListState` (one word, PascalCase) - **Document type**: `powerhouse/todo-list` - **Module name**: `todos` - **ID generation**: Uses `generateId()` from `document-model/core` in the reducer - **Hooks**: Uses `useSelectedTodoListDocument` for state management in the editor ### Option 2: Clone and run the code locally The package includes: - The Document Model - Reducer Code - Reducer Tests - Editor Code - Drive-app Code You can clone the repository and run Vetra Studio to see all the code in action: ```bash git clone https://github.com/powerhouse-inc/todo-demo cd todo-demo pnpm install ph vetra --watch ```
Alternatively: Run with Connect ```bash git clone https://github.com/powerhouse-inc/todo-demo cd todo-demo pnpm install ph connect ```
### Option 3: Install the todo demo package in a (local) host app Alternatively, you can install this package in a Powerhouse project or in your deployed host apps: ```bash ph install @powerhousedao/todo-demo ``` ## Comparing the Manual Todo tutorial vs Mastery Track | Aspect | Manual Todo tutorial | Mastery Track (Advanced) | | ------------------ | -------------------------- | ------------------------------------ | | Schema | Basic `items` array only | Includes `stats` object for tracking | | Reducer complexity | Simple CRUD operations | Includes statistics updates | | Editor | Component-based with hooks | Same approach + stats display | | Tests | Basic operation tests | Includes stats verification tests | Both approaches use the same naming conventions and patterns — the Mastery Track simply extends the foundation with additional features to demonstrate more advanced concepts. --- ## Document Model Versioning > Source: https://academy.vetra.io/academy/Build/DocumentModelCreation/DocumentModelVersioning **TIP:** This chapter covers **advanced document model versioning**—a system for evolving document schemas and operations while maintaining backward compatibility with existing documents. This is essential when your document models need to change over time in production environments. ## Why Versioning? Document models in Powerhouse are **event-sourced**. Once a document is created with a certain schema (v1), and operations are applied to it, you can't simply change the schema without breaking existing documents. Versioning solves this problem by allowing you to: - **Add new fields** to the state schema - **Add new operations** to the document model - **Modify reducer logic** for new documents - **Automatically upgrade** old documents to new versions when needed **INFO:** Document Model Versioning is a system that allows multiple versions of the same document model to coexist. Each version has its own schema, operations, and reducers. Documents created with older versions continue to work with their original reducers, while new documents use the latest version. Upgrade manifests define how to migrate documents between versions. --- ## How Versioning Works ### The Problem It Solves Consider a simple Todo document model: **Version 1 State:** ```graphql type TodoState { todos: [Todo!]! } ``` Now you want to add a `title` field to track the list's name: **Version 2 State:** ```graphql type TodoState { title: String todos: [Todo!]! } ``` Without versioning, existing v1 documents would break because they don't have a `title` field. With versioning: - V1 documents continue to work with the v1 reducer - New documents are created with v2 - V1 documents can be **upgraded** to v2 when needed ### Key Components Document model versioning consists of four key components: | Component | Purpose | | ----------------------- | ------------------------------------------------------------------ | | **Version Folders** | Separate `v1/`, `v2/` directories containing version-specific code | | **DocumentModelModule** | Each version exports a module with explicit `version` number | | **Upgrade Manifest** | Declares supported versions and upgrade paths | | **Upgrade Reducer** | Transforms document state from one version to another | --- ## Folder Structure When versioning is enabled, the document model generator creates a versioned folder structure: ``` document-models/ └── todo/ ├── v1/ # Version 1 │ ├── gen/ # Auto-generated code (DO NOT EDIT) │ │ ├── reducer.ts │ │ ├── creators.ts │ │ ├── types.ts # V1 TypeScript types │ │ └── ... │ ├── src/ │ │ └── reducers/ # Your v1 reducer implementations │ │ └── todo-operations.ts │ └── module.ts # Exports DocumentModelModule with version: 1 │ ├── v2/ # Version 2 │ ├── gen/ │ │ └── types.ts # V2 TypeScript types (includes 'title') │ ├── src/ │ │ └── reducers/ │ └── module.ts # Exports DocumentModelModule with version: 2 │ ├── upgrades/ # Migration logic │ ├── versions.ts # Supported versions list │ ├── v2.ts # Upgrade reducer: v1 → v2 │ └── upgrade-manifest.ts # Ties everything together │ └── document-models.ts # Exports all versions + manifests ``` --- ## Core Type Definitions Understanding the underlying types helps you implement versioning correctly: ### UpgradeTransition Defines a single version upgrade: ```typescript type UpgradeTransition = { toVersion: number; upgradeReducer: UpgradeReducer; description?: string; }; ``` ### UpgradeManifest Declares all supported versions and their upgrade paths: ```typescript type UpgradeManifest = { documentType: string; latestVersion: number; supportedVersions: TVersions; upgrades: { // Keys are "v2", "v3", etc. (never "v1" - nothing to upgrade from) [V in Exclude, 1> as `v${V}`]: UpgradeTransition; }; }; ``` --- ## Implementation Guide ### Step 1: Enable Versioning in Code Generation Versioning is enabled by default when using the Powerhouse CLI: ```bash ph generate document-model --document TodoList.phd ``` Or when using Vetra Studio, versioning support is configured in your project settings. ### Step 2: Version Configuration Files **versions.ts** - Declare supported versions: ```typescript // upgrades/versions.ts export const supportedVersions = [1, 2] as const; export const latestVersion = supportedVersions[1]; // 2 ``` ### Step 3: Document Model Module Each version exports a `DocumentModelModule` with an explicit version number: ```typescript // v1/module.ts export const Todo: DocumentModelModule = { version: 1, // Explicit version number reducer, actions, utils, documentModel: createState(defaultBaseState(), documentModel), }; ``` ```typescript // v2/module.ts export const Todo: DocumentModelModule = { version: 2, // Different version reducer, actions, utils, documentModel: createState(defaultBaseState(), documentModel), }; ``` ### Step 4: Upgrade Reducer The upgrade reducer transforms a document from one version to the next: ```typescript // upgrades/v2.ts function upgradeReducer( document: PHDocument, action: Action, ): PHDocument { return { ...document, state: { ...document.state, global: { ...document.state.global, title: "", // Initialize the new field }, }, initialState: { ...document.initialState, global: { ...document.initialState.global, title: "", // Also in initial state }, }, }; } export const v2: UpgradeTransition = { toVersion: 2, upgradeReducer, description: "Add title field to global state", }; ``` ### Step 5: Upgrade Manifest Tie everything together in the manifest: ```typescript // upgrades/upgrade-manifest.ts export const upgradeManifest: UpgradeManifest = { documentType: "my-org/todo", latestVersion, supportedVersions, upgrades: { v2 }, }; ``` ### Step 6: Export All Versions ```typescript // document-models.ts export const documentModels: DocumentModelModule[] = [TodoV1, TodoV2]; export const upgradeManifests: UpgradeManifest[] = [ todoUpgradeManifest, ]; ``` --- ## Integration with Connect and Switchboard ### How Connect Loads Versioned Documents Connect automatically loads all document model versions and upgrade manifests from your Vetra packages: ```typescript // Simplified view of Connect's reactor setup const documentModelModules = vetraPackages.flatMap( (pkg) => pkg.modules.documentModelModules, ); const upgradeManifests = vetraPackages.flatMap((pkg) => pkg.upgradeManifests); const reactor = await createBrowserReactor( documentModelModules, upgradeManifests, renown, ); ``` ### Creating Documents at Specific Versions By default, new documents are created at the **latest version**. You can optionally specify a version: ```typescript // Create at latest version (default) const doc = await (await client.write.createEmpty("my-org/todo")).document(); // doc.state.document.version === 2 (latest) // Create at specific version const v1Handle = await client.write.createEmpty("my-org/todo", { documentModelVersion: 1, }); const v1Doc = await v1Handle.document(); // v1Doc.state.document.version === 1 ``` ### Querying Documents Documents can be queried regardless of version: ```typescript // Find all todo documents (both v1 and v2) const result = await client.find({ type: "my-org/todo" }); ``` --- ## Use Cases ### 1. Adding a New Field **Scenario:** Your Todo document needs a `title` field. **Solution:** 1. Create v2 with the new field in the state schema 2. Implement upgrade reducer that sets `title: ""` 3. New documents get v2; existing v1 documents can be upgraded ### 2. Adding New Operations **Scenario:** V1 has `ADD_TODO`, `REMOVE_TODO`. V2 adds `EDIT_TITLE`. **How it works:** - V2 module includes the new operation - V1 documents don't have access to `EDIT_TITLE` until upgraded - The upgrade manifest handles the migration ### 3. Changing Reducer Behavior **Scenario:** V2 items should include an `addedAt` timestamp. ```typescript // V1 reducer - no timestamp function v1StateReducer(state, action) { if (action.type === "ADD_ITEM") { return { ...state, global: { items: [ ...state.global.items, { id: action.input.id, name: action.input.name, }, ], }, }; } } // V2 reducer - adds timestamp field function v2StateReducer(state, action) { if (action.type === "ADD_ITEM") { return { ...state, global: { items: [ ...state.global.items, { id: action.input.id, name: action.input.name, addedAt: action.input.addedAt, // New field from input }, ], }, }; } } ``` --- ## Best Practices ### Upgrade Reducer Guidelines 1. **Always handle both `state` and `initialState`** - Both need to be migrated 2. **Provide sensible defaults** for new fields 3. **Never lose data** - Transform existing data, don't delete it 4. **Keep upgrade reducers pure** - No side effects or async operations ### Version Compatibility - **Don't remove operations** from newer versions unless absolutely necessary - **Don't change existing operation input schemas** - add new operations instead - **Document breaking changes** in the upgrade transition description ### Testing Upgrades Test your upgrade reducers thoroughly: ```typescript it("should upgrade v1 document to v2", () => { const v1Doc = createV1Document(); v1Doc.state.global.todos = [{ id: "1", title: "Test", completed: false }]; const v2Doc = upgradeReducer(v1Doc, {} as Action); expect(v2Doc.state.global.title).toBe(""); // New field initialized expect(v2Doc.state.global.todos).toEqual(v1Doc.state.global.todos); // Data preserved }); ``` --- ## Summary | Concept | Description | | -------------------------- | ----------------------------------------------------------- | | **Version Folders** | `v1/`, `v2/` directories with version-specific code | | **DocumentModelModule** | Exports with explicit `version` field | | **UpgradeTransition** | Defines how to migrate from one version to the next | | **UpgradeManifest** | Declares all versions and upgrade paths for a document type | | **Backward Compatibility** | Old documents work with their original reducers | | **Automatic Upgrades** | Reactor handles version detection and migration | Document model versioning enables your applications to evolve safely while preserving the integrity of existing data. By following these patterns, you can confidently add new features, modify schemas, and improve your document models over time. --- ## Related Documentation - [Use the Document Model Generator](/academy/Build/DocumentModelCreation/UseTheDocumentModelGenerator) - [Implement Document Reducers](/academy/Build/DocumentModelCreation/ImplementDocumentReducers) - [Powerhouse CLI Reference](/academy/Reference/CLITooling/PowerhouseCLI) --- ## Custom scalars > Source: https://academy.vetra.io/academy/Build/DocumentModelCreation/CustomScalars A custom scalar is a GraphQL scalar that your project defines with a zod schema. You declare it in a document model's SDL or use it in a custom subgraph. Codegen generates its TypeScript type and its zod validation from that schema, and the host checks every input value of the scalar against the same schema. Use a custom scalar when a field needs a format that the built-in scalars don't cover. This page uses an `Iban` scalar for international bank account numbers. ## Define a scalar Export each scalar from `scalars/index.ts` at the project root. `ph init` doesn't create this file. Create it when you add your first custom scalar. ```ts title="scalars/index.ts" export const Iban = defineScalar({ name: "Iban", description: "International bank account number", schema: z.string().regex(/^[A-Z]{2}\d{2}[A-Z0-9]{11,30}$/), example: "DE89370400440532013000", }); ``` `defineScalar` accepts these keys and no others: | Key | Required | Purpose | | ---------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `name` | Yes | The GraphQL name of the scalar. | | `schema` | Yes | A zod schema. Its output is the TypeScript type of the scalar and the value that reducers receive. The host validates input with it. | | `description` | No | The scalar's GraphQL description. Introspection shows it. | | `example` | No | A value the schema accepts. Generated reducer tests use its parsed form as the mock value for fields of this scalar. | | `representation` | No | The shape of the scalar's values: `string`, `number`, `boolean`, `json-object` or `json`. `defineScalar` reads it from the schema. Set it only for a schema zod cannot describe, such as `z.custom`. A value the schema contradicts throws. | `representation` is recorded on the definition for tooling. The host and codegen don't read it yet. API clients see the schema's error message when they send an invalid value. To replace zod's default message, pass `error`: ```ts schema: z.string().regex(/^[A-Z]{2}\d{2}[A-Z0-9]{11,30}$/, { error: "Expected an IBAN such as DE89370400440532013000", }), ``` Scalars run in Connect and on every host. Import only packages that run in both the browser and Node. `ph build` fails when `scalars/index.ts` imports a package that needs a native addon or a WebAssembly module. ### Name the export after the scalar The export name must equal `name`. Codegen never runs your project code. It reads `Iban` from the SDL and imports `Iban` from `scalars/index.ts` by that name. The host also refuses a scalar whose export key differs from `name`. `defineScalar` throws for these names: - A name that is not a valid GraphQL name. - A GraphQL built-in scalar: `String`, `Int`, `Float`, `Boolean` or `ID`. - A Powerhouse catalog scalar: `Address`, `Amount`, `Amount_Crypto`, `Amount_Currency`, `Amount_Fiat`, `Amount_Money`, `Amount_Percentage`, `Amount_Tokens`, `AttachmentRef`, `Currency`, `Date`, `DateTime`, `EmailAddress`, `EthereumAddress`, `JSONObject`, `OID`, `OLabel`, `PHID`, `Unknown`, `Upload` or `URL`. It also throws for any key that is not in the table above, and for an `example` that the schema refuses. The message names the scalar and the reason: ```text Cannot define scalar "Int": the name is a GraphQL built-in scalar. Cannot define scalar "Iban": the example fails the schema: ✖ Invalid string: must match pattern /^[A-Z]{2}\d{2}/ ``` Scalar names are global on a host. The scalars of every installed package share one namespace with the catalog. If two packages define the same name with different definitions, the host keeps the one it loaded first and refuses the other. ### Use the value type in TypeScript `ScalarValue` gives the TypeScript type of a scalar's values, which is the output type of its schema. For `Iban`, the type is `string`: ```ts type IbanValue = ScalarValue; // string ``` A schema that ends in `.brand<"Iban">()` gives `string & $brand<"Iban">`, so TypeScript refuses a plain string where an `Iban` is expected. The `example` still takes a plain string, because it has the schema's input type. `defineScalar` parses the example and stores the output. ### Return JSON that the schema accepts again The output of the schema is the canonical value of the scalar. Reducers receive the canonical value, and the document state stores it. `defineScalar` accepts a schema only when its output is JSON and is a valid input of the same schema. The host also checks that the output comes back unchanged when the schema parses it again. A schema can normalize the value. This `Iban` accepts lowercase letters and spaces around the number: ```ts title="scalars/index.ts" export const Iban = defineScalar({ name: "Iban", description: "International bank account number", schema: z .string() .trim() .toUpperCase() .regex(/^[A-Z]{2}\d{2}[A-Z0-9]{11,30}$/), example: "DE89370400440532013000", }); ``` When a client sends `" de89370400440532013000 "`, the reducer receives `"DE89370400440532013000"`, whether the operation comes from Connect or from the host. An operation that Connect stores keeps the input as the user entered it. `defineScalar` accepts these schemas: - Checks such as `.regex()` and `.refine()`. - `.brand()`. - `.trim()`, `.toLowerCase()`, `.toUpperCase()` and `.overwrite()`. - A `.transform()` that keeps the type and returns the same value when it runs again. - `z.enum`, `z.literal` and unions. - Objects, including objects with optional or nullable properties. - Arrays, records and tuples. - `z.coerce.number()`, `z.coerce.string()` and `z.preprocess(String, z.string())`. - `.catch()`. - `z.custom` with a JSON type, such as ``z.custom<`0x${string}`>()``. A refused schema fails to type-check. The error names one of five causes: | Cause in the error | Refused schemas | | ---------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `defineScalar refuses this schema: its output is any` | `z.any()`, `z.custom()` | | `defineScalar refuses this schema: it accepts a missing value; nullability belongs in the SDL` | `.optional()`, `.nullish()`, `.default()` and `.prefault()`, also on `z.coerce` and `z.preprocess` schemas | | `defineScalar refuses this schema: its output must be JSON` | An `undefined` at the top level or inside an array, such as `z.array(z.string().optional())`, a `Date` output, `z.unknown()`, `z.bigint()`, an object with a function field | | `defineScalar refuses this schema: its output can be null; nullability belongs in the SDL` | `.nullable()`, a top-level `z.json()` | | `defineScalar refuses this schema: its output must pass the schema again` | A `.transform()` or `.pipe()` that changes the type, such as `.transform(Number)` or `z.string().pipe(z.coerce.number())` | Make a field nullable in the SDL instead of in the schema. `iban: Iban` is nullable, and `iban: Iban!` is not. For `schema: z.string().transform(Number)`, TypeScript reports: ```text Type 'ZodPipe>' is not assignable to type 'ZodPipe> & OutputFailsSchema'. Property '"defineScalar refuses this schema: its output must pass the schema again"' is missing in type 'ZodPipe>' but required in type 'OutputFailsSchema'. ``` TypeScript doesn't see every output that isn't JSON. It misses an explicit `undefined` in an optional object property, such as a transform that returns `{ value: undefined }` for `{ value?: string }`, and an `any` inside an object, such as `z.object({ value: z.any() })`. The host refuses such a value. Connect doesn't check it and stores it in the state. `defineScalar` also checks for a missing-value schema at runtime. This check catches JavaScript callers, casts, and a union with a coercing default, such as `z.union([z.coerce.number().default(7), z.string()])`, which TypeScript accepts: ```text Cannot define scalar "Count": the schema accepts a missing value; nullability belongs in the SDL. ``` TypeScript can't tell whether a transform returns the same value when it runs again. The host checks that for every value it parses. For details, see [What the host validates](#what-the-host-validates). A schema with `.catch()` replaces a value that fails the schema with the fallback. The fallback must still be JSON that the schema returns unchanged on a second parse. Otherwise the host refuses it. For example, `z.string().regex(/^ok$/).catch((ctx) => String(ctx.input) + "!")` turns `"bad"` into `"bad!"`, and the host refuses the value because `"bad!"` parses to `"bad!!"`. The generated code makes a nullable field `.nullish()`, so a missing nullable field stays missing, even with `.catch()`. A required field can still get a value in Connect when a caller skips the types and leaves it out or sets it to `undefined`. The host never parses an absent value. In the reducer: - `.catch("c")` fills a missing required field with `"c"`. - `z.preprocess((v) => v ?? "x", z.string())` fills it with `"x"`. - `z.coerce.string()` turns a field set to `undefined` into the string `"undefined"`. - `z.preprocess(String, z.string())` turns a missing or `undefined` field into `"undefined"`. Use `z.string()` when a missing value must fail. ### Store free-form JSON in an object A top-level `z.json()` can output `null`, so `defineScalar` refuses it with `its output can be null`. To accept any JSON value, put `z.json()` in an object property: ```ts title="scalars/index.ts" export const Metadata = defineScalar({ name: "Metadata", description: "Free-form JSON under a meta key", schema: z.object({ meta: z.json() }), example: { meta: { tags: ["a", "b"] } }, }); ``` ### Store a big integer as a decimal string A `bigint` isn't JSON, so a scalar can't output one. Define a big integer as a decimal string: ```ts title="scalars/index.ts" export const BigInteger = defineScalar({ name: "BigInteger", description: "A signed integer of any size, as a decimal string", schema: z .string() .regex(/^-?\d+$/) .transform((s) => BigInt(s).toString()) .brand<"BigInteger">(), example: "123", }); ``` The transform writes one form for each number, so `"007"` becomes `"7"` and `"-0"` becomes `"0"`. The `example` is a plain string, because it has the schema's input type. In a reducer, convert the value with `BigInt` for arithmetic, and parse the result to write it back: ```ts addAmountOperation(state, action) { const total = BigInt(state.total) + BigInt(action.input.amount); state.total = BigInteger.schema.parse(total.toString()); }, ``` The parse returns the branded type that the state field expects. ## Use the scalar in a document model Declare `scalar Iban` once, in the state schema. The Vetra document-model editor merges the state and operation schemas, so a second declaration is a duplicate type there. Then use `Iban` like any other type, in state types and in operation inputs: ```graphql title="State schema" scalar Iban type PaymentState { iban: Iban } ``` ```graphql title="Operation schema" input SetIbanInput { iban: Iban! } ``` Regenerate the code: ```bash ph generate all ``` The generated files in `document-models//v/gen/schema/` import the definition from `scalars/index.ts`: - `types.ts` maps the input and output types of `Iban` to `import("document-model/scalars").ScalarValue`. - `zod.ts` imports `{ Iban }` and validates `Iban` fields with `Iban.schema`. The generated reducer parses each operation's input with its zod schema. When the input uses `Iban`, directly or through a nested input type or a list, the reducer passes the parsed input to your operation reducer. Your reducer then receives canonical scalar values, whatever sent the operation: Connect, a GraphQL mutation or sync. For these operations, `z.object` drops keys that the input type doesn't declare, and a default value on an input field in the SDL fills in a missing field. Other operations receive the input as it was stored, as before. A model without custom scalars regenerates the same reducer, so its stored operations replay to the same state. Generated reducer tests mock `Iban` input fields with `Iban.example`, wrapped in an array for a list field. Without `example`, they generate a value from `Iban.schema`. For a `z.custom` schema, the mock generator throws `No generator for schema custom`, so set `example` when you use `z.custom`. ## Use the scalar in a custom subgraph A custom subgraph uses `Iban` in its typeDefs. You can declare `scalar Iban` in the subgraph or leave the declaration out. The host declares every registered scalar in every subgraph and drops the subgraph's own declaration. ```ts title="subgraphs/payments/schema.ts" export const schema: DocumentNode = gql` type Query { ibanCountry(iban: Iban!): String! } `; ``` The host parses `iban` with the schema before the resolver runs, so the resolver receives the canonical value: ```ts title="subgraphs/payments/resolvers.ts" export const getResolvers = ( subgraph: BaseSubgraph, ): Record => ({ Query: { ibanCountry: (_parent: unknown, args: { iban: ScalarValue }) => args.iban.slice(0, 2), }, }); ``` `Iban` has the same name everywhere: in the document-model subgraph at `/graphql/`, in the custom subgraph at `/graphql/payments` and in the supergraph at `/graphql`. The host never prefixes it. For how to create and run a subgraph, see [Using subgraphs](/academy/Build/WorkWithData/UsingSubgraphs). ## Add custom scalars to an existing project 1. In the project root, run: ```bash ph migrate ``` `ph migrate` adds the `./scalars` export to `package.json` and the `scalars` paths to `tsconfig.json`. It doesn't create or change `scalars/index.ts`. As for any upgrade, it also updates the Powerhouse dependencies and regenerates the code. 2. Create `scalars/index.ts` at the project root and define your scalars in it. 3. Declare them in the model SDL. 4. Run `ph generate all`. 5. Before you publish the package, run `ph build`. Hosts load the scalars from the package's `./scalars` export, which points into `dist/`. A project without custom scalars generates the same reducers as before. Only operations whose input uses a custom scalar receive the parsed input, as described in [Use the scalar in a document model](#use-the-scalar-in-a-document-model). ## What the host validates A host, such as Switchboard or `ph vetra`, loads each package's `./scalars` export before the package's document models and subgraphs. Each exported definition registers its scalar by name. For each registered scalar, the host checks input values: - `parseValue` checks values that arrive as variables. - `parseLiteral` checks values written inline in the query. Both run the scalar's `schema` and pass its output to the resolver. When the schema refuses a value, the host refuses the request. The error message holds the GraphQL prefix, the scalar name and the message from `z.prettifyError`. For a variable: ```text Variable "$iban" got invalid value "not-an-iban"; Expected type "Iban". Iban: ✖ Invalid string: must match pattern /^[A-Z]{2}\d{2}[A-Z0-9]{11,30}$/ ``` For an inline literal: ```text Expected value of type "Iban!", found "not-an-iban"; Iban: ✖ Invalid string: must match pattern /^[A-Z]{2}\d{2}[A-Z0-9]{11,30}$/ ``` Both then write the output to JSON and read it back. If the copy differs from the output, the host refuses the request. This check catches schemas that skip the type check, such as one from a JavaScript package. For a schema that outputs a `bigint`, the value `"7"` gives: ```text Iban: the schema output is not JSON: 7n ``` A number scalar refuses `-0` for the same reason, because JSON writes `-0` as `0`. Last, both parse the JSON copy a second time. If the second parse refuses the copy or returns a different value, the host refuses the request. For a scalar `Bang` with the schema `z.string().transform((s) => s + "!")`, the value `"abc"` gives: ```text Bang: the schema must accept its own output unchanged, but "abc!" parses to "abc!!". ``` When the second parse refuses the copy, the message ends with `fails it:` and the zod error instead: ```text Iban: the schema must accept its own output unchanged, but 7 fails it: ✖ Invalid input: expected string, received number ``` Connect doesn't run the JSON check or the second parse. It parses each input once, in the reducer. The subgraphs run these checks. The supergraph at `/graphql` forwards the value to the subgraph. It returns the subgraph's refusal inside a `400: Bad Request` error, with the message above under `extensions.response.body.errors`. `serialize` returns values unchanged. The host doesn't check the values that resolvers return. The host doesn't run the schemas of the catalog scalars, such as `EmailAddress` and `DateTime`. Codegen messages call these the built-in scalars. The host passes their values through as before. The host refuses to register a scalar in these cases: - The definition comes from a version of `document-model` whose definition format the host doesn't support. - The export key differs from `name`. - The name is a GraphQL built-in, a catalog scalar, or a type of the generated document-model schema such as `Query` or `Operation`. `defineScalar` doesn't check schema type names. A package that bundles another copy of `document-model` runs that copy's `defineScalar`, so the host checks all three again. - Another package already registered the same name with a different definition. The first package loaded wins. The host logs one warning per package, with the same header as a failed load, and names the packages involved: ```text Failed to load scalars from @acme/payments: Scalar Iban of package @acme/payments is refused: package @acme/banking already registers a different Iban. ``` A refused scalar is not served. The package's other scalars, document models and subgraphs still load. A scalar can also take a name that a host subgraph defines as a type, such as `PHDocument`. The host registers it but doesn't serve it, and the subgraph keeps the name. The host logs an error instead of the warning above: ```text Refusing scalar PHDocument of package @acme/payments: subgraph r defines a type of that name ``` In `ph vetra`, saving `scalars/index.ts` rebuilds the GraphQL schema and reloads the project's document models, so the reducers also parse with the new definition. You don't need to restart. An object or list default value for an argument or input field of a custom or catalog scalar type breaks GraphQL introspection today. ## Use your own resolver for a scalar A subgraph can put its own `GraphQLScalarType` for a registered scalar in its `resolvers`. The subgraph's resolver wins in that subgraph, and the registered validation doesn't run there. The host logs one warning per subgraph and scalar: ```text Subgraph payments provides its own resolver for registered scalar Iban; the registered validation is not applied. ``` If you replace the resolver, your `parseValue` and `parseLiteral` must do the validation. ## Fix common errors ### Codegen stops with "declares scalar Iban, which is not a built-in scalar" ```text Model PaymentModel declares scalar Iban, which is not a built-in scalar. Create scalars/index.ts and export it from there with defineScalar. ``` The model declares a scalar that isn't in the catalog, and the project has no `scalars/index.ts`. Create `scalars/index.ts` at the project root and export the scalar from it with `defineScalar`. ### TypeScript reports "has no exported member 'Iban'" TypeScript reports these errors in `gen/schema/zod.ts` and `gen/schema/types.ts`: ```text error TS2305: Module '"../../../../../scalars/index.js"' has no exported member 'Iban'. error TS2694: Namespace '"/scalars/index"' has no exported member 'Iban'. ``` The model declares `scalar Iban`, but `scalars/index.ts` doesn't export `Iban`. Export it with `defineScalar`, and check that the export name equals `name`. ### TypeScript reports "defineScalar refuses this schema" ```text Property '"defineScalar refuses this schema: its output must pass the schema again"' is missing in type 'ZodPipe>' but required in type 'OutputFailsSchema'. ``` The text after `refuses this schema:` names the cause: - `its output is any`: replace `z.any()` or `z.custom()` with a schema for the actual type. - `it accepts a missing value; nullability belongs in the SDL`: remove `.optional()`, `.nullish()`, `.default()` or `.prefault()`, and make the field nullable in the SDL. - `its output must be JSON`: remove `.optional()` inside arrays, and return strings instead of a `Date` or a `bigint`. - `its output can be null; nullability belongs in the SDL`: remove `.nullable()`, and make the field nullable in the SDL. For free-form JSON, see [Store free-form JSON in an object](#store-free-form-json-in-an-object). - `its output must pass the schema again`: make the transform return a value of the input type. For the full lists, see [Return JSON that the schema accepts again](#return-json-that-the-schema-accepts-again). ### `defineScalar` throws "Cannot define scalar" ```text Cannot define scalar "Iban": the example fails the schema: ✖ Invalid string: must match pattern /^[A-Z]{2}\d{2}/ Cannot define scalar "Count": the schema accepts a missing value; nullability belongs in the SDL. ``` The first error means `example` fails the schema. Change the example to a value the schema accepts. The second comes from a schema with `.optional()`, `.nullish()`, `.default()` or `.prefault()` that skipped the type check, for example through a cast, or that sits in a union. Remove the modifier and make the field nullable in the SDL. Other messages name an invalid or reserved name, or an unknown input key. See [Name the export after the scalar](#name-the-export-after-the-scalar). ### The host refuses a value with "the schema output is not JSON" ```text Iban: the schema output is not JSON: 7n ``` The schema returned a value that JSON can't hold unchanged, such as a `bigint`, a `Date`, `-0` or an object property set to `undefined`. TypeScript refuses a `bigint` or `Date` output, unless it comes from an `any` inside an object or the schema skipped the type check. Return a JSON value instead. For `-0`, the client must send `0`. ### The host refuses a value with "the schema must accept its own output unchanged" ```text Bang: the schema must accept its own output unchanged, but "abc!" parses to "abc!!". ``` A transform in the schema changes its own output each time it runs. Change the transform so that running it on its output returns the same value. When the message ends with `fails it:`, the schema refuses its own output. Make the output a valid input of the schema. ### A custom subgraph fails with an unknown type ```text Unknown type "Iban". ``` No loaded package registers `Iban`. Check that `scalars/index.ts` exports it and that `package.json` has the `./scalars` export. For an installed package, check that `ph build` ran before you published it. If the host refused the scalar, it logged a `Failed to load scalars from :` warning with the reason. ### `ph build` fails with "Document models and scalars run in Connect and on every host" ```text scalars/index.ts imports , which needs the WebAssembly module . Document models and scalars run in Connect and on every host: use it from a subgraph, processor or piece instead. ``` The scalar imports a package that can't run in Connect. Move the validation that needs the package into a subgraph. --- ## Build document editors > Source: https://academy.vetra.io/academy/Build/BuildingUserExperiences/BuildingDocumentEditors ## Build with React on Powerhouse At Powerhouse, frontend development for document editors follows a simple and familiar flow, leveraging the power and flexibility of React. ### Development environment **Vetra Studio** is your primary tool for builder workflows and editor development. When you run `ph vetra --watch`, it provides a dynamic, local environment where you can define and preview your document models and their editors live. This replaces the need for tools like Storybook for editor development, though Storybook remains invaluable for exploring the [Powerhouse Component Library](#powerhouse-component-library). #### Key aspects of the Powerhouse development environment: - **React Foundation**: Build your editor UIs using React components, just as you would in any standard React project. - **Automatic Build Processes**: Tailwind CSS is installed by default and fully managed by Vetra Studio. There's no need to manually configure or run Tailwind or other build processes during development. Vetra Studio handles CSS generation and other necessary build steps automatically, especially when you publish a package. - **Styling Flexibility**: You are not limited to Tailwind. Regular CSS (`.css` files), inline styles, and any React-compatible styling method work exactly as you would expect. #### Powerhouse aims to keep your developer experience clean, familiar, and focused: - Build React components as you normally would. - Use styling approaches you're comfortable with. - Trust Vetra Studio to handle the setup and build processes for you.
Alternatively: Use Connect for development You can also use **Connect** as your development environment by running `ph connect`. Connect provides a similar dynamic local environment where you can preview your document models and their editors live. The development experience is essentially the same, with Connect also handling Tailwind CSS and build processes automatically.
### Generating your editor template When using **Vetra Studio**, editor generation is automatic and integrated into your workflow. Simply create an **Editor specification document** in your Vetra Studio Drive, and Vetra will automatically generate the editor template code for you. #### With Vetra Studio (Recommended) 1. Open Vetra Studio (`ph vetra --watch`) 2. In your Vetra Studio Drive, click **"Add new specification"** in the Editors section 3. Select your document model (e.g., `TodoList`) to link the editor to 4. Name your editor (e.g., `todo-list-editor`) 5. Vetra automatically generates the `editors/todo-list-editor/editor.tsx` template That's it! No manual commands needed. Vetra watches your specifications and generates code as you work.
Alternatively: Manual generation with ph generate If you're using Connect or prefer manual control, you can use the `ph generate` command to create an editor template: ```bash ph generate editor --name todo-list-editor --document-type powerhouse/todo-list ``` This will create the template in the `editors/todo-list-editor/editor.tsx` folder. If you want a refresher on how to define your document model specification please read the chapter on [specifying the State Schema](/academy/Build/DocumentModelCreation/SpecifyTheStateSchema)
### Styling your editor You have several options for styling your editor components: 1. **Default HTML Styling**: Standard HTML tags (`

`, `

`, `; } ``` **Why hooks below the top:** - ✅ **Self-contained components**: each component reads the editor's document itself - ✅ **Less boilerplate**: no need to pass `document` and `dispatch` through every level - ✅ **Easier refactoring**: move components around without rewiring props **Testing a component:** render it inside `EditorDocumentProvider` from `@powerhousedao/reactor-browser` with a test document and dispatch; its hooks read them. **Which Connect:** an editor that reads its document from props needs a Connect that passes them. Editors that only use the hooks work in every Connect, and the hooks are not deprecated. ### Additional hooks for editors Beyond the document-specific hooks (like `useSelectedTodoListDocument`), the reactor-browser package provides these hooks for your editors: | Hook | Description | | --------------------------- | --------------------------------------------- | | `useSelectedDocument` | Returns the currently selected document | | `useSelectedDocumentId` | Returns just the ID of the selected document | | `useDocumentById` | Returns a document by its ID | | `useSelectedDrive` | Returns the currently selected drive | | `useRevisionHistoryVisible` | Check and control revision history visibility | | `usePHModal` | Manage modals in your editor | **Example: Using `useDocumentById` to reference another document** ```typescript export function RelatedDocument({ documentId }: { documentId: string }) { const relatedDoc = useDocumentById(documentId); if (!relatedDoc) return Loading...; return (

Related: {relatedDoc.name}

{/* Display related document info */}
); } ``` **Example: Showing a modal from your editor** ```typescript export function CreateNewButton({ documentType }: { documentType: string }) { return ( ); } ``` For a complete list of all available hooks, see the [React Hooks API Reference](/academy/Reference/EditorsUI/ReactHooks). ## Local vs. Global State When building editors, you'll work with two types of state: - **Global Document State**: Data that is part of the document itself and should be saved. The editor component gets it as the `document` prop (`document.state.global`), and components inside the editor through hooks (`useSelectedTodoListDocument`). You modify it by dispatching actions. - **Local Component State**: UI-specific state that doesn't need to be saved (e.g., "is the dropdown open?", "what's in the input field before submission?"). Use React's `useState` hook for this. ```typescript export function AddTodo() { // Local state - just for this component's UI const [inputValue, setInputValue] = useState(''); // Global document state - saved in the document const [todoList, dispatch] = useSelectedTodoListDocument(); const handleSubmit = () => { if (inputValue.trim()) { dispatch(addTodoItem({ text: inputValue })); // Updates global state setInputValue(''); // Clears local state } }; return (
setInputValue(e.target.value)} />
); } ``` ## Handling dispatch errors When dispatching actions to a document, you may want to handle errors that occur during action execution. The `dispatch` function accepts an optional `onErrors` callback as its second parameter, which is invoked with any errors thrown by the reducers when processing the actions. ```typescript useSelectedTodoListDocument, addTodoItem, } from "todo-tutorial/document-models/todo-list"; export function AddTodo() { const [todoList, dispatch] = useSelectedTodoListDocument(); const handleAdd = (text: string) => { dispatch(addTodoItem({ text }), (errors) => { // Handle errors - e.g., show a toast notification console.error("Failed to add todo:", errors); alert(`Error: ${errors[0]?.message}`); }); }; // ... rest of component } ``` This pattern is useful when you need to: - Display error messages to users - Log errors for debugging - Trigger recovery actions when an operation fails ## Powerhouse component library The **`@powerhousedao/document-engineering/scalars`** package provides reusable UI components, many of them based on GraphQL scalar types. For more information read our chapter on the [Component Library](/academy/Reference/EditorsUI/DocumentEngineering). ### Exploring components You can explore available components, see usage examples, and understand their properties (props) using our Storybook instance: [https://storybook.powerhouse.academy](https://storybook.powerhouse.academy) Storybook allows you to: - Visually inspect each component. - Interact with different states and variations. - View code snippets for basic implementation. - Consult the props table for detailed configuration options. ### Using components 1. **Import**: Add an import statement at the top of your editor file: ```typescript import { Checkbox, StringField, Form, } from "@powerhousedao/document-engineering/scalars"; ``` 2. **Implement**: Use the component in your JSX, configuring it with props: ```typescript // Example using StringField for an input
{ /* Handle submission */ }}> setTaskText(e.target.value)} /> ```
Tutorial: Implementing the TodoList Editor ## Build a TodoList editor Now build the editor: the user interface for the **TodoList** document model. It runs inside the Connect app to create, update, and delete TodoList items, and (if you followed the advanced version) displays the statistics from your reducers. ## Generate the editor template ### Using Vetra Studio (Recommended) With Vetra Studio running (`ph vetra --watch`), simply create an Editor specification: 1. In your Vetra Studio Drive, click **"Add new specification"** in the Editors section 2. Select the **TodoList** document model to link the editor to 3. Name your editor `todo-list-editor` 4. Vetra automatically generates `editors/todo-list-editor/editor.tsx` Once complete, navigate to the `editors/todo-list-editor/editor.tsx` file and open it in your IDE.
Alternatively: Manual generation with ph generate If you're not using Vetra Studio, run the command below to generate the editor template: ```bash ph generate editor --name todo-list-editor --document-type powerhouse/todo-list ``` This command reads the **TodoList** document model definition from the `document-models` folder and generates the editor template in the `editors/todo-list-editor` folder as `editor.tsx`. Notice the `--editor` flag which specifies the editor name, and the `--document-types` flag defines the document type `powerhouse/todo-list`.
### Editor implementation options You have several options for styling your editor component: 1. **Default HTML Styling:** Standard HTML tags (`

`, `

`, ` ); } ``` ### Todo item component ```typescript // editors/todo-list-editor/components/Todo.tsx type Props = { todo: TodoItem; }; export function Todo({ todo }: Props) { const [isEditing, setIsEditing] = useState(false); const [todoList, dispatch] = useSelectedTodoListDocument(); if (!todoList) return null; const onChangeTodoChecked: ChangeEventHandler = (event) => { dispatch(updateTodoItem({ id: todo.id, checked: event.target.checked })); }; const onClickDeleteTodo: MouseEventHandler = () => { dispatch(deleteTodoItem({ id: todo.id })); }; const onSubmitUpdateTodoText: FormEventHandler = (event) => { event.preventDefault(); const form = event.currentTarget; const textInput = form.elements.namedItem("todoText") as HTMLInputElement; const text = textInput.value; if (!text) return; dispatch(updateTodoItem({ id: todo.id, text })); setIsEditing(false); }; if (isEditing) { return (

); } return (
{todo.text}
); } ``` --- ## Advanced: Adding stats display **INFO:** If you implemented the advanced version with statistics tracking, you can add a stats component to display the todo counts. ```typescript // Add to TodoList.tsx export function TodoList() { const [selectedTodoList] = useSelectedTodoListDocument(); if (!selectedTodoList) return null; const { items, stats } = selectedTodoList.state.global; return (

TodoList

{/* Stats section (only show if there are items) */} {items.length >= 2 && (
Total
{stats.total}
Completed
{stats.checked}
Remaining
{stats.unchecked}
)}
); } ``` --- ## Test your editor Now you can run Vetra Studio and see the **TodoList** editor in action: ```bash ph vetra --watch ``` In Vetra Studio, you'll be able to create and test your **TodoList** documents. Click on the Document Models section and create a new TodoList document. **TIP:** The editor will update dynamically, so you can play around with your editor styling while seeing your results appear in Vetra Studio.
Alternatively: Test with Connect You can also run the Connect app to see the **TodoList** editor in action: ```bash ph connect ``` In Connect, in the bottom right corner you'll find a new Document Model that you can create: **TodoList**. Click on it to create a new TodoList document. The editor will update dynamically, so you can play around with your editor styling while seeing your results appear in Connect.

## Up Next Next, build a [custom Drive-app](/academy/Build/BuildingUserExperiences/BuildingADriveExplorer) for your TodoList document. When you have many TodoLists sitting in a drive, a Drive-app lets you organize and track them at a glance. ### Further Reading - [React Hooks API Reference](/academy/Reference/EditorsUI/ReactHooks) — Complete reference for all available Powerhouse hooks - [Component Library](/academy/Reference/EditorsUI/DocumentEngineering) — Pre-built UI components for your editors --- ## Build a Drive-app > Source: https://academy.vetra.io/academy/Build/BuildingUserExperiences/BuildingADriveExplorer **Drive-apps** enhance how contributors and organizations interact with document models. They create an 'app-like' experience by providing a **custom interface** for exploring and interacting with the contents of a drive. **TIP:** A Drive-app offers a tailored application designed around its document models. Think of a Drive-app as a specialized lens—it offers **different ways to visualize, organize, and interact with** the data stored within a drive, making it more intuitive and efficient for specific use cases. ### Drive-apps are purpose-built Organizations typically build Drive-apps for specific use cases, often packaging them with a corresponding document model. This allows for customized user experiences, streamlined workflows, and maximized efficiency for contributors. Drive-apps **bridge the gap between raw data and usability**, unlocking the full potential of document models within the Powerhouse framework. ### Key features of Drive-apps - **Custom Views & Organization** – Drive-apps can present data in formats like Kanban boards, list views, or other structured layouts to suit different workflows. - **Aggregated Insights** – They can provide high-level summaries of important details across document models, enabling quick decision-making. - **Enhanced Interactivity** – Drive-apps can include widgets, data processors, or read models to process and display document data dynamically. ## Build a Drive-app Drive-apps provide custom interfaces for interacting with the contents of a drive. Let's start with a **quick overview** of the steps for building a Drive-app. We will then apply these steps to create our **todo-list Drive-app**. ### Step 1. Generate the scaffolding code When using **Vetra Studio**, Drive-app generation is automatic. Simply create a **Drive-app specification document** in your Vetra Studio Drive: 1. Open Vetra Studio (`ph vetra --watch`) 2. In your Vetra Studio Drive, click **"Add new specification"** in the Apps section 3. Name your Drive-app (e.g., `todo-list-drive-explorer`) 4. Vetra automatically generates the Drive-app template code
Alternatively: Manual generation with ph generate If you're not using Vetra Studio, use the `generate drive editor` command to create the basic template structure: ```bash ph generate app --name ```
### Step 2. Update the manifest file After creating your Drive-app, you need to update its `powerhouse.manifest.json` file. This file identifies your project and its components within the Powerhouse ecosystem. ### Step 3. Customize the Drive-app Review the generated template and modify it to better suit your document model: 1. Remove unnecessary files and components 2. Add custom views specific to your data model 3. Implement specialized interactions for your use case ### About the Drive-app template The default template provides a solid foundation. It contains: - A tree structure navigation panel - Basic file/folder operations - Standard layout components But the real power comes from tailoring the interface to your specific document models. Now, let's implement a specific example for the to-do list we've been working on throughout this guide. ## Implementation example: todo-list Drive-app This example demonstrates how to create a todo-list Drive-app application using the Powerhouse platform. The application allows users to create and manage to-do lists with a visual progress indicator. **WARNING:** If you've been following the Mastery Track, you can continue with the to-do list document model and Powerhouse project you've created. For more details, you can refer to the [Document Model Creation guide](/academy/Build/DocumentModelCreation/SpecifyTheStateSchema). If not, you can follow the shortened guide below to prepare your project for this tutorial.
Prepare your Powerhouse Project to create a custom drive ### 1. Create a To-do document model: - Initialize a new project with `ph init` and give it a project name. - Start by running Vetra Studio locally with `ph vetra --watch` - Follow the [Manual Todo tutorial](/academy/Build/ManualTodoTutorial/DefineToDoListDocumentModel) to create your TodoList document model specification. - Drop the downloaded file in the Vetra Studio drive. You'll find it under document models. Vetra should now automatically generate the necessary code for your project ### 2. Add the reducer code: - Copy the code from [`todos.ts`](https://github.com/powerhouse-inc/todo-tutorial/blob/step-3-complete-implement-todo-list-document-model-reducer-operation-handlers/document-models/todo-list/src/reducers/todos.ts) - Paste it into `document-models/todo-list/src/reducers/todos.ts` ### 3. Generate a document editor: In Vetra Studio, create an Editor specification: 1. Click **"Add new specification"** in the Editors section 2. Select the **TodoList** document model 3. Name your editor `TodoList` 4. Vetra automatically generates the editor template
Alternatively: Manual generation ```bash ph generate editor --name TodoList --document-type powerhouse/todo-list ```
### 4. Add the editor code: - Follow the [Build TodoList Editor guide](/academy/Build/ManualTodoTutorial/BuildToDoListEditor) to implement your editor components.
Alternatively: Use Connect instead of Vetra Studio You can also start by running Connect locally with `ph connect` instead of Vetra Studio. The workflow is the same, with Connect providing a similar development environment.
## Generate the Drive-app ### 1. Generate a Drive-app: With Vetra Studio running (`ph vetra --watch`), create a Drive-app specification: 1. In your Vetra Studio Drive, click **"Add new specification"** in the Apps section 2. Name your Drive-app `todo-list-drive-explorer` 3. Vetra automatically generates the Drive-app template in `editors/todo-list-drive-explorer/`
Alternatively: Manual generation with ph generate ```bash ph generate app --name todo-list-drive-explorer ```
### 2. Update the `powerhouse.manifest.json` file: - The manifest file contains metadata for your package that is displayed when other users install it. Update the manifest to register your new Drive-app: ```json { "name": "To-do List Package", "description": "A simple todo-list with a dedicated Drive-app", "category": "Productivity", "publisher": { "name": "Powerhouse", "url": "https://www.powerhouse.inc/" }, "documentModels": [ { "id": "todo-list", "name": "TodoList" } ], "editors": [ { "id": "todo-list-editor", "name": "TodoList Editor", "documentTypes": ["todo-list"] } ], "apps": [ { "id": "todo-list-drive-explorer", "name": "TodoList drive-app", "driveEditor": "todo-list-drive-explorer" } ], "subgraphs": [], "importScripts": [] } ``` ### 3. Remove Unnecessary Default Components: - First, let's remove some default template files that we won't need for this specific demo. If you want to see what the default template looks like before removing files, you can run `ph connect` at any time. ```bash rm -rf editors/todo-list-drive-explorer/hooks rm -rf editors/todo-list-drive-explorer/components/FileItemsGrid.tsx rm -rf editors/todo-list-drive-explorer/components/FolderItemsGrid.tsx rm -rf editors/todo-list-drive-explorer/components/FolderTree.tsx ``` ### 4. Create custom components for your Drive-app: - Next, create the following files. These will define the data types for our todo-list items and provide the custom React components for our Drive-app.
Create `editors/todo-list-drive-explorer/types/todo.ts` This file defines the TypeScript type `ToDoState`. It specifies the shape of todo-list document data within the Drive-app, combining the document's revision information with its global state. This ensures that our components work with a predictable and strongly-typed data structure. ```typescript import type { TodoListDocument } from "todo-tutorial/document-models/todo-list"; export type TodoState = { documentType: string; revision: { global: number; local: number; }; global: TodoListDocument["state"]["global"]; }; ```
Create `editors/todo-list-drive-explorer/components/ProgressBar.tsx` This is a simple React component that renders a visual progress bar. It takes a `value` and `max` number to calculate the percentage of completed tasks. It also displays the percentage and has a special state for when there are no tasks. ```tsx import type { FC } from 'react'; interface ProgressBarProps { value: number; max: number; } export const ProgressBar: FC = ({ value, max }) => { if (max === 0) { return (
No tasks
); } const percentage = Math.min(100, (value / max) * 100); return (
{Math.round(percentage)}%
); }; ```
Update `editors/todo-list-drive-explorer/components/DriveExplorer.tsx` This is the main component of our Drive-app. It fetches all `powerhouse/todo-list` documents from the drive, displays them in a table with their progress, and allows a user to click on a document to open it in the `EditorContainer`. It also includes a button to create new documents. ```typescript type TodoState = { documentType: string; revision: { global: number; local: number; }; global: TodoListDocument["state"]["global"]; }; interface DriveExplorerProps { driveId: string; nodes: Node[]; onAddFolder: (name: string, parentFolder?: string) => void; onDeleteNode: (nodeId: string) => void; renameNode: (nodeId: string, name: string) => void; onCopyNode: (nodeId: string, targetName: string, parentId?: string) => void; context: DriveEditorContext; } export function DriveExplorer({ driveId, nodes, context, }: DriveExplorerProps) { const { getDocumentRevision } = context; const [activeDocumentId, setActiveDocumentId] = useState< string | undefined >(); const [openModal, setOpenModal] = useState(false); const selectedDocumentModel = useRef(null); const { addDocument, documentModels, useDriveDocumentStates } = useDriveContext(); const [state, fetchDocuments] = useDriveDocumentStates({ driveId }); useEffect(() => { fetchDocuments(driveId).catch(console.error); }, [activeDocumentId]); const { todoNodes } = useMemo(() => { return Object.keys(state).reduce( (acc, curr) => { const document = state[curr]; if (document.documentType === "powerhouse/todo-list") { acc.todoNodes[curr] = document as TodoState; } return acc; }, { todoNodes: {} as Record, }, ); }, [state]); const handleEditorClose = useCallback(() => { setActiveDocumentId(undefined); }, []); const onCreateDocument = useCallback( async (fileName: string) => { setOpenModal(false); const documentModel = selectedDocumentModel.current; if (!documentModel) return; const node = await addDocument( driveId, fileName, documentModel.documentModel.id, ); selectedDocumentModel.current = null; setActiveDocumentId(node.id); }, [addDocument, driveId], ); const onSelectDocumentModel = useCallback( (documentModel: DocumentModelModule) => { selectedDocumentModel.current = documentModel; setOpenModal(true); }, [], ); const onGetDocumentRevision = useCallback( (options?: GetDocumentOptions) => { if (!activeDocumentId) return; return getDocumentRevision?.(activeDocumentId, options); }, [getDocumentRevision, activeDocumentId], ); const filteredDocumentModels = documentModels; const fileNodes = nodes.filter((node) => node.kind === "file") as FileNode[]; // Get the active document info from nodes const activeDocument = activeDocumentId ? fileNodes.find((file) => file.id === activeDocumentId) : undefined; const documentModelModule = activeDocument ? context.getDocumentModelModule(activeDocument.documentType) : null; const editorModule = activeDocument ? context.getEditor(activeDocument.documentType) : null; return (
{/* Main Content */}
{activeDocument && documentModelModule && editorModule ? ( ) : ( <>

ToDos:

{Object.entries(todoNodes).map(([documentId, todoNode]) => ( ))}
Document ID Document Type Tasks Completed Progress
setActiveDocumentId(documentId)} className="text-blue-600 hover:text-blue-800 cursor-pointer" > {documentId}
{todoNode.documentType} {todoNode.global.stats.total} {todoNode.global.stats.checked}
{/* Create Document Section */} )}
{/* Create Document Modal */} setOpenModal(open)} open={openModal} />
); } ```
Update `editors/todo-list-drive-explorer/components/EditorContainer.tsx` This component acts as a wrapper for the document editor. When a user selects a document in `DriveExplorer.tsx`, this component mounts the document's editor (`todo-list-editor` in this case, picked by `useEditorModuleFor`), passes it the document and a `dispatch` as props, and wraps it in `EditorDocumentProvider` so the hooks inside the editor read the same document. It also renders the `DocumentToolbar` which provides actions like closing, exporting, and viewing revision history. The revision history panel gets its operations from `useDocumentOperations`, which fetches one scope lazily while the panel is open; the panel itself asks for further pages as the viewer scrolls. ```typescript EditorDocumentProvider, useDocumentById, useDocumentOperations, useEditorModuleFor, } from "@powerhousedao/reactor-browser"; export interface EditorContainerProps { documentId: string; onClose: () => void; title: string; } export const EditorContainer: React.FC = (props) => { const { documentId, onClose, title } = props; const [showRevisionHistory, setShowRevisionHistory] = useState(false); const [document, dispatch] = useDocumentById(documentId); // The document's preferred editor if it edits this type, else the first that does. const editorModule = useEditorModuleFor(document); // `scopes` mirrors the document's own header; a document always carries // "global", so it's the fallback while `document` is still loading. const scopes = document ? Object.keys(document.header.revision) : ["global"]; const [scope, setScope] = useState(scopes.includes("global") ? "global" : scopes[0]); // The hook fetches one scope lazily, only while the panel is open; the // panel itself asks for further pages as the viewer scrolls. const { operations, isLoading, hasNextPage, fetchNextPage } = useDocumentOperations( documentId, scope, { enabled: showRevisionHistory }, ); const loadingContent = (
); if (!document || !editorModule) return loadingContent; const EditorComponent = editorModule.Component; return showRevisionHistory ? ( setShowRevisionHistory(false)} key={documentId} /> ) : ( {/* The editor gets the document as props; the hooks inside it read the provider. */} ); }; ```
- In case you are getting stuck and want to verify your progress with the reference repository you can find the example repository of the [Todo Tutorial here](https://github.com/powerhouse-inc/todo-tutorial) ### 3. Run the application: - With the code for our Drive-app in place, it's time to see it in action. If you've been running Vetra Studio with `ph vetra --watch`, your Drive-app is already available. Otherwise, run Connect: ```bash ph connect ``` ## Up Next You've built a custom drive explorer. Next, style it with [CSS customization](/academy/Build/BuildingUserExperiences/CSSCustomization). --- ## CSS Customization for Connect Integration > Source: https://academy.vetra.io/academy/Build/BuildingUserExperiences/CSSCustomization When your editor runs inside Connect, it's rendered within a specific container hierarchy. Understanding this structure allows you to customize your editor's appearance to match your application's design requirements. ## Understanding the Editor Container Hierarchy Connect wraps your editor component in two key containers that you can target for styling: ```
└── └──
└── ``` ### Container Details | Container ID | Default Classes | Data Attributes | Purpose | | ---------------------------- | ----------------- | ----------------------------------- | ----------------------------------------------------------- | | `#document-editor-container` | `flex-1` | `data-document-type` | Outermost wrapper, controls overall editor space allocation | | `#document-editor-context` | `relative h-full` | `data-editor`, `data-document-type` | Inner context, provides positioning context and full height | These containers are defined in Connect's source: - [`document-editor-container.tsx`](https://github.com/powerhouse-inc/ph-monorepo/blob/main/apps/connect/src/components/document-editor-container.tsx) (line 94) - [`editors.tsx`](https://github.com/powerhouse-inc/ph-monorepo/blob/main/apps/connect/src/components/editors.tsx) (line 173) ## Customizing Your Editor's Appearance ### Method 1: Inline Styles in Your Editor Component (Recommended) The simplest and most maintainable approach is to apply styles directly to your editor's root element. This keeps your styling self-contained within your editor. ```tsx export function Editor() { return (
{/* Your editor content */}
); } ``` **TIP:** Using `height: "100%"` ensures your editor fills the available vertical space within Connect's container hierarchy. ### Method 2: CSS File with Container Selectors For more complex customizations or when you need to override Connect's default container styles, you can target the container IDs directly in a CSS file: **DANGER:** Targeting container IDs directly will apply styles to **ALL** editors in your Connect application. For editor-specific styling, use [Method 3: Scoped Styling with Data Attributes](#method-3-scoped-styling-with-data-attributes) instead. ```css /* editors/my-editor/editor.css */ #document-editor-container { /* Customize the outer container */ background-color: #f8fafc; } #document-editor-context { /* Customize the inner context */ max-width: 1200px; margin: 0 auto; } /* Scope styles to your editor within the context */ #document-editor-context .my-editor-root { padding: 2rem; } ``` **WARNING:** Remember to import styles in your `styles.css` file rather than directly in `.tsx` files. Direct imports work in development but won't be included in production builds. ```css /* styles.css */ @import "./editors/my-editor/editor.css"; ``` ### Method 3: Scoped Styling with Data Attributes Connect adds `data-editor` and `data-document-type` attributes to editor containers, allowing you to scope CSS rules to specific editors without affecting others. ```css /* Only applies to a specific editor */ #document-editor-context[data-editor="document-editor-editor"] { background-color: #f0f9ff; } /* Only applies to a specific document type */ #document-editor-context[data-document-type="powerhouse/document-editor"] { max-width: 900px; margin: 0 auto; } /* Combine both for precise targeting */ #document-editor-context[data-editor="document-editor-editor"][data-document-type="powerhouse/document-editor"] { padding: 1rem; } ``` #### Finding Your Editor ID The `data-editor` value comes from the `id` property in your editor module configuration: ```typescript // editors/my-editor/module.ts export const MyEditor: EditorModule = { config: { id: "my-custom-editor", // <-- This becomes the data-editor value documentTypes: ["my-org/my-document-model"], }, Component: MyEditorComponent, }; ``` #### Common Editor IDs | Editor ID | Document Type | Description | | ------------------------ | ---------------------------- | --------------- | | `document-editor-editor` | `powerhouse/document-editor` | Document Editor | | `vetra-drive-app` | `powerhouse/document-drive` | Drive Explorer | | `app-editor` | `powerhouse/app` | App Editor | **TIP:** You can inspect the `data-editor` and `data-document-type` attributes in your browser's developer tools when editing a document to find the exact values for your target editor. ## Reference Implementation: Vetra Drive App The Vetra Drive App provides a real-world example of CSS customization. Here's how it styles its editor wrapper: ```tsx // From: packages/vetra/editors/vetra-drive-app/editor.tsx
``` This example demonstrates: - **`height: "100%"`** - Ensures the editor fills the full container height - **`bg-gray-50`** - Applies a light gray background color - **`p-6`** - Adds consistent padding around the content - **`after:*` pseudo-element** - Creates a visual effect layer for transitions ## Common Use Cases ### Full-Height Editor with Scrollable Content When your editor needs a fixed header/toolbar with scrollable main content: ```tsx export function Editor() { return (

Document Title

{/* Toolbar buttons */}
{/* Scrollable content area */}
); } ``` ### Custom Background and Theming Apply custom backgrounds or gradients to match your application's theme: ```tsx export function Editor() { return (
{/* Themed content */}
); } ``` ### Centered Content with Max Width Constrain content width for better readability: ```tsx export function Editor() { return (
{/* Centered, width-constrained content */}
); } ``` ## Troubleshooting ### Editor Doesn't Fill Available Height **Problem:** Your editor content appears squished or doesn't use the full height. **Solution:** Ensure your root element has `height: "100%"` or uses flex utilities: ```tsx // Option 1: Inline style
// Option 2: Tailwind class
``` ### Styles Not Applied in Production **Problem:** Styles work in development but not in production builds. **Solution:** Move style imports from `.tsx` files to your `styles.css` file: ```css /* styles.css - correct location for imports */ @import "./editors/my-editor/editor.css"; ``` ### Z-Index Conflicts **Problem:** Overlays, modals, or dropdowns appear behind other elements. **Solution:** The `#document-editor-context` has `position: relative`. Use this as your positioning context: ```tsx
{/* Your content */}
{/* Positioned overlay */}
``` ### Content Overflows Container **Problem:** Content extends beyond the editor boundaries. **Solution:** Add overflow handling to your root element: ```tsx
{/* Scrollable when content overflows */}
// Or hide overflow
{/* Content is clipped */}
``` ## Further Reading - [Building Document Editors](/academy/Build/BuildingUserExperiences/BuildingDocumentEditors) - Fundamentals of editor development including basic styling - [Building a Drive Explorer](/academy/Build/BuildingUserExperiences/BuildingADriveExplorer) - Creating custom drive apps with styling --- ## Document Toolbar > Source: https://academy.vetra.io/academy/Build/BuildingUserExperiences/DocumentTools/DocumentToolbar The `DocumentToolbar` renders a default set of document controls for common document actions: undo, redo, download, document name editing, revision history, opening in Switchboard, and closing the current document view. The toolbar can be customized by enabling or disabling built-in controls, replacing individual controls, adding custom controls to toolbar slots, replacing the toolbar containers, applying custom styles, or replacing the entire toolbar contents with `children`.
Document Toolbar
The Document Toolbar can be found at the top of any generic document.
## Basic usage ```tsx export function MyDocumentPage({ document }) { return ; } ``` When no `document` prop is provided, the toolbar falls back to the currently selected document. ```tsx ``` ## Built-in controls The default toolbar includes these controls: ```ts ["undo", "redo", "download", "name", "history", "switchboard", "close"]; ``` The controls are arranged into three slots: ```ts { first: ["undo", "redo", "download"], second: ["name"], third: ["history", "switchboard", "close"], } ``` ## Disabling controls Use `disabledControls` to remove specific built-in controls from the default toolbar. ```tsx ``` ## Enabling only specific controls Use `enabledControls` to render only a subset of the built-in controls. ```tsx ``` ## Combining enabled and disabled controls A control renders only when it is included in `enabledControls` and absent from `disabledControls`. When a control appears in both lists, `disabledControls` takes precedence. ```tsx ``` In this example, only `switchboard` and `close` render. ## Adding a custom control to a slot Use `customControls` to add controls to a specific toolbar slot. Custom controls receive the current document when available. ```tsx ( ), }, }} /> ``` ## Adding a custom control at the end of a slot By default, custom controls render at the start of their slot. Use `position: "end"` to render a custom control after the built-in controls in that slot. ```tsx ( ), }, }} /> ``` ## Adding multiple custom controls to a slot A slot can receive a list of custom controls. Each item needs a `key`. ```tsx ( ), }, { key: "custom-two", position: "end", component: ({ document }) => ( ), }, ], }} /> ``` ## Replacing the toolbar container Use `toolbarContainer` to replace the outer toolbar container. The custom toolbar container receives the toolbar children and the resolved `toolbarClassName`. ```tsx (
{children}
)} /> ``` ## Replacing the controls container Use `controlsContainer` to replace the container used for each toolbar slot. The custom controls container receives the slot children and the resolved `controlsContainerClassName`. ```tsx (
{children}
)} /> ``` ## Replacing a built-in control Use `componentOverrides` to replace individual built-in controls while keeping the default toolbar structure. This is useful when you want to reuse the built-in control behavior or styling but change part of the rendering or click behavior. ```tsx ( alert(props.document?.header.name)} > Download?? ), }} />; ``` The `name` control can also be replaced through `componentOverrides`. ```tsx (