Esc
Documentation
On this page

An action is a command. It can change the service, start work, or write state. An action defines behavior only, so labels and layout belong to the components that run it. Reads belong in resources.

Define an action without input#

ts
import { defineAction } from "@northgraindata/dsui-adapter-sdk";
import type { MyContext } from "./context.js";

export const flush = defineAction({
  id: "flush",
  run: async (_input: undefined, ctx: MyContext) => {
    await ctx.client.clearCache();
    return { flushed: true };
  },
});

The id must be unique within the adapter. Call the action to create a binding: flush(). Creating a binding doesn’t run the action. It makes a serializable description that DSUI runs when someone clicks a button or submits a form.

Define an action with input#

Add a Zod input schema. DSUI validates the input before run starts, so run always receives parsed values:

ts
import { defineAction, z } from "@northgraindata/dsui-adapter-sdk";

export const greet = defineAction({
  id: "greet",
  input: z.object({ name: z.string().min(1) }),
  run: async ({ name }, ctx: MyContext) => ({ message: `Hello, ${name}` }),
});

Bind it with its input: greet({ name: "Ada" }). An input that doesn’t match the schema is rejected with an error that lists the Zod issues.

Run an action from a page#

Pass a binding to a Button. Add a confirmation for a destructive action:

ts
Button({
  label: "Suspend",
  action: suspendWarehouse({ warehouse: row.name }),
  variant: "danger",
  confirmation: {
    title: "Suspend warehouse?",
    description: "Running queries will be cancelled.",
    confirmLabel: "Suspend",
  },
});

The runtime context#

run receives your adapter context plus helpers from DSUI. Add ActionRuntimeContext to the type to see them:

HelperMeaning
signalAn AbortSignal that the caller owns. Pass it to cancellable work and check it during long work
storesAccess to the adapter’s stores
blobsBinary storage private to the service
emit(signal, payload, options?)Publishes a declared signal from this action
invalidate(resource?, input?)Makes DSUI read watched resources again
ts
import type { ActionRuntimeContext } from "@northgraindata/dsui-adapter-sdk";

run: async ({ warehouse }, ctx: MyContext & ActionRuntimeContext) => {
  await ctx.client.suspend(warehouse, { signal: ctx.signal });
  ctx.invalidate(warehouses);
  return { warehouse, status: "SUSPENDED" };
},

Refresh data after a change#

invalidate makes DSUI run watched resources again, and every component that watches them updates:

  • ctx.invalidate() refreshes every resource
  • ctx.invalidate(resource) refreshes every binding of that resource
  • ctx.invalidate(resource, input) refreshes only the binding with that input

Publish a signal#

emit publishes one of the signals that the adapter declares in signals. Pass an idempotencyKey so a retry doesn’t create a second event. DSUI ignores a repeat with the same key and the same data, and it reports an error when the key is reused with different data. See signals.

Results and errors#

An action returns data, and DSUI wraps it in a result:

ResultShape
Success{ "status": "success", "data": … }, with what run returned
Error{ "status": "error", "message": … }, when run throws

An action never throws to the caller. A thrown error becomes an error result with its message.

Describe an action#

An optional description says what the action does. DSUI publishes it with the input schema, so tools that list the adapter’s actions can tell them apart. The AI assistant can run an action that an adapter declares, when the person who asks has the execute permission and the assistant’s allowActionExecution setting isn’t false. See AI assistant.

ts
defineAction({
  id: "suspend-warehouse",
  description: "Suspend a compute warehouse. Running queries are cancelled.",
  input: z.object({ warehouse: z.string() }),
  run: async ({ warehouse }, ctx: MyContext) => { /* … */ },
});

Write a description that states the effect of the action.