Docs menu

Custom Modes

Goal: Host a workbench mode that OpenX does not ship, such as a webhook receiver, next to the built-in MCP, API and Web modes.

A mode is one way a workbench is served: MCP serves tools to AI clients, API serves REST routes, Web serves pages. A mode is a name. Your workbench package declares the modes it has, and a workbench offers exactly those. A custom mode is declared the same way as a built-in one, and there is nothing to register in the workspace.

A mode is a way a workbench is served: what it listens for, and what it does with each call. It is not something the workbench does on a timer. A scheduled job is not a mode: nothing calls it, so it has no address and no callers for a mode to serve. A webhook receiver is one, because it has an address, callers, and a contract for who may call it.

Define one

A mode is a ModeBuilder. DefineModeHolon, from @fathym/fai/workbenches, wraps a factory for one so your package can export it by name. A Webhook mode:

import { DefineModeHolon, ModeBuilder } from '@fathym/fai/workbenches';

export const WebhookModeHolon = DefineModeHolon(
  {
    Kind: 'Webhook',
    ContractVersion: '1.0.0',
    Entrypoint: 'fai run <Entry> --mode Webhook --config /tmp/mode-config.json',
    HealthEndpoint: '/health',
    BuiltIn: false,
    HolonRef: 'https://github.com/fathym/sample-custom-mode-workbench/blob/main/workbenches/webhook/mode-holon.ts',
  },
  () =>
    new ModeBuilder('Webhook', 'Receives signed webhooks at POST /webhooks/<hook> and hands each event to a tool.')
      .Execute(async (_ctx) => ({ Vaults: {}, Close: async () => {} })),
);

WebhookModeHolon.Mode() returns a fresh builder each time it is called. The descriptor in the first argument is what this version of @fathym/fai still asks DefineModeHolon for. OpenX does not read it: the mode is the name your package declares, so there is nothing to register in a workspace before you deploy. The Execute function is where your mode does its work; the sample's one starts a server that checks each call's signature. For a mode that does something real, read the built-in Web mode's source, src/workbenches/modes/WebMode.ts in the @fathym/fai package, which is built the same way with a ModeBuilder.

Write a workbench that uses it

Pass the builder to .Modes() under the mode's name:

export default Workbench('webhook-sample', 'Webhook workbench')
  .Modes({ Webhook: WebhookModeHolon.Mode().Ports(4970) }); // the port the mode serves on

.Ports() is what tells OpenX where to route calls. A mode that listens for callers declares its port; a mode with nothing to listen for declares .Ports(false).

A workbench can declare a custom mode next to built-in ones. The key you give it in .Modes({...}) is the mode's name everywhere else: in the Modes tab, in the container and in the deploy's messages.

Deploy and host it

Add the workbench as in Host Your Own Workbench and save it. Saving reads the modes your package declares. Then in its Modes tab choose Add mode: the list offers the modes the package declares, your custom one among them. Pick it and Deploy. Each enabled mode gets its own container, with FATHYM_OX_WORKBENCH_MODE set to the mode's name.

  • A web address, when the mode declares a port. A mode that declares .Ports(4970) listens on a port OpenX routes to, so /ox-api/w/<workspace id>/workbenches/<apiSlug>/<mode> reaches it, and the Hosting tab shows that address once the deploy is Running. A mode declared with .Ports(false) has no web address, so the same path answers 404 "runs without ingress". Nothing outside the container can wake it, so it keeps at least one replica running.
  • Hosting settings apply. The replica, CPU and memory settings in the Hosting tab apply to a custom mode as they do to a built-in one. The minimum is never below one for a mode with no web address.
  • No config form. The Modes tab shows a config form for MCP only; a custom mode runs with its own defaults.
  • Not declared. A mode enabled on the workbench that its package does not declare is not deployed, and the deploy says "Mode '<mode>' is not declared by this workbench's package (...), so it was not deployed." Remove the mode from the workbench, or declare it in the package and save again. The other modes still deploy.

Walk through it: the Webhook sample

The custom mode sample, published as @fathym/sample-custom-mode-workbench, is a Webhook mode. GitHub, or anything that signs the same way, POSTs an event, the mode checks the signature, and a workbench tool handles the event.

RouteWhat it does
POST /webhooks/<hook>Checks the signature, then calls the hook's tool and answers 200.
GET /health200 {"status":"ok"} for OpenX's readiness check.
  1. Drop a SurfaceWorkbench on a surface. Set its Source to Repo https://github.com/fathym/sample-custom-mode-workbench, Ref main, Entry workbenches/webhook/local.ts. In Hosting set the APISlug to webhook-sample. In Environment add GITHUB_WEBHOOK_SECRET with a secret of your choosing. Save.

  2. Modes -> Add mode -> Webhook, and Deploy. There is nothing to register first.

  3. When the workbench's status in the Hosting tab reads Running, the tab shows the mode's address:

    Webhook: <workspace origin>/ox-api/w/<workspace id>/workbenches/webhook-sample/Webhook
    

    The container's logs show [webhook] POST /webhooks/github -> GitHubEvents.Receive (signed, secret in GITHUB_WEBHOOK_SECRET) and [webhook] listening on port 4970.

  4. Point the sender at that address followed by /webhooks/github. In GitHub, that is the repository's Settings -> Webhooks -> Add webhook: Payload URL as above, Content type application/json, and Secret the same value as GITHUB_WEBHOOK_SECRET.

Send a test webhook

A webhook is signed the way GitHub signs it. The X-Hub-Signature-256 header carries sha256= followed by the HMAC-SHA256 of the raw request body, keyed with the shared secret. Try it on your own machine first, with fai run jsr:@fathym/sample-custom-mode-workbench --mode Webhook (or deno task webhook from a clone), which listens on http://localhost:4970:

export GITHUB_WEBHOOK_SECRET=It-is-a-secret-to-everybody
BODY='{"repository":{"full_name":"fathym/sample-custom-mode-workbench"},"sender":{"login":"you"}}'
SIG="sha256=$(printf '%s' "$BODY" | openssl dgst -sha256 -hmac "$GITHUB_WEBHOOK_SECRET" | sed 's/^.* //')"

curl -i -X POST http://localhost:4970/webhooks/github \
  -H 'Content-Type: application/json' \
  -H 'X-GitHub-Event: push' \
  -H 'X-GitHub-Delivery: test-1' \
  -H "X-Hub-Signature-256: $SIG" \
  -d "$BODY"

The answer is 200 {"received":true,"result":{"Seen":"push on fathym/sample-custom-mode-workbench by you"}}. The mode refuses what it cannot trust, with a status that says why: 401 for a missing or wrong signature, 404 for a hook it does not have, 405 for anything but POST, 400 for a body that is not JSON, and 500 when the hook's secret variable is not set (it fails closed).

Who can reach a deployed endpoint

The workspace's door signs callers in with OpenX before it forwards them, and a webhook sender has no OpenX sign-in. So GitHub reaches the deployed mode only once the workspace owner detaches this endpoint from the workspace's sign-in: in the workspace API settings, the endpoint identity for this workbench, mode Webhook, is set to detached. A detached endpoint signs its own callers in, which is what the signature check does. While the endpoint is still linked, you can send the same test webhook to the deployed address with your own OpenX bearer token added: the door checks the token and removes it, and the mode still checks the signature.

The sample is kept small on purpose. A production receiver would also de-duplicate redeliveries by X-GitHub-Delivery, answer fast and queue slow work, and rotate secrets without a restart.

Versioning

A workbench picks up a new version of the mode's code from its own source and ref on its next deploy. If a new version drops a mode, the Modes tab says a deploy refuses it, and you remove it from the workbench.

Earlier, a custom mode was registered under Workspace Settings -> Mode types first. That step is gone. A workspace that still holds a registration keeps it, unused.

Next steps

On fathym.com: Workbenches →

On this page