Get started
Embedded Studio

Studio handlers

Studio handlers

Everything a studio does is a verb dispatched on its tool bus: a click, your own code and the co-pilot alike. @mountaya/studio declares 253 verbs, and @mountaya/studio/handlers implements every one. Replace a verb's handler to change what it does.

Opt-in subpath

Replace one handler

Hand your handler to the host under the verb's name. The other 252 stand.

import { MountayaStudio, MountayaStudioHost } from "@mountaya/studio-react";

const handlers = { "studio.itinerary.export": exportThroughOurOwnPipeline };

export const Studio = () => (
  <MountayaStudioHost behavior={{ handlers }} client={mountaya}>
    <MountayaStudio />
  </MountayaStudioHost>
);

A handler takes the tool context and the verb's parsed input, and returns a result. A refusal is a result, never a throw.

On the headless path, spread the defaults and pass the map to createStudioToolBus:

import { createStudioHandlers, createStudioToolBus } from "@mountaya/studio/handlers";

const bus = createStudioToolBus({
  handlers: {
    ...createStudioHandlers(),
    "studio.itinerary.export": exportThroughOurOwnPipeline,
  },
});

Wrap a handler

overrideStudioHandler hands you the current handler, so you can log, refuse on your own condition, then delegate.

import { createStudioHandlers, overrideStudioHandler } from "@mountaya/studio/handlers";

const handlers = overrideStudioHandler(
  createStudioHandlers(),
  "studio.itinerary.delete",
  (current) => {
    const wrapped = typeof current === "function" ? current : current?.handler;
    if (wrapped === undefined) {
      throw new Error("no default to wrap");
    }

    return {
      handler: (ctx, input) => {
        recordIntent(input);

        return wrapped(ctx, input);
      },
      requiresConfirmation: true,
    };
  },
);

Ask before destructive verbs

Twelve verbs, such as deleting an itinerary or removing a segment, ask the reader first. The host shows its own confirm dialog; pass behavior.confirm to ask in your own words.

import type { ConfirmationRequest } from "@mountaya/studio";
import { MountayaStudio, MountayaStudioHost } from "@mountaya/studio-react";

const confirm = async (request: ConfirmationRequest) => window.confirm(request.description);

export const Studio = () => (
  <MountayaStudioHost behavior={{ confirm }} client={mountaya}>
    <MountayaStudio />
  </MountayaStudioHost>
);

The same gate asks for the co-pilot's calls, so a model never deletes anything without the reader's yes.

Gate verbs on what the session holds

Every verb needs one capability: embedding to view and move the map, authenticated to read as a member, editor to write. The host grants all three by default; grant fewer for a read-only studio.

import { MountayaStudioHost, MountayaStudioViewer } from "@mountaya/studio-react";
import type { ComponentProps } from "react";

export const PublicMap = (props: ComponentProps<typeof MountayaStudioViewer>) => (
  <MountayaStudioHost client={mountaya} data={{ capabilities: ["share", "embedding"] }}>
    <MountayaStudioViewer {...props} />
  </MountayaStudioHost>
);

A verb outside the grant answers capability-denied, and the studio hides the control rather than offer it.

Classify the errors they raise

A handler that cannot recognize a failure re-throws it, and the bus classifies it. Add recognizers for your own errors.

import { createStudioToolBus } from "@mountaya/studio/handlers";

const strictBus = createStudioToolBus({ recognizers: ourOwnRecognizers });

Next steps

Last updated