Skip to content

Writing a command

A command is one declaration. The platform parses the line, resolves paths to Live objects, asks when a name is ambiguous and generates --help. Design background: 0005. How the layers fit together is on the Architecture page.

A module with one command

A module is a folder under src/modules/<name>/ with an index.ts that exports activate(api). Add it to MODULES in src/modules/index.ts; nothing else in the platform changes.

import type { ModuleApi } from "../types.js";
import type { ObjectNode } from "../../ableton/objectTree.js";
import { ok } from "../../core/types.js";

export function activate(api: ModuleApi): void {
  api.commands.add({
    name: "flip",
    summary: "toggle mute on tracks",
    description: "Switches mute on for each track that is not muted and off for each that is.",
    examples: [{ line: "flip /Kick, /Snare", note: "two tracks, separated by a comma" }],
    args: [{ name: "track", kind: "path", kinds: ["track"], variadic: true, summary: "Tracks to flip" }],
    run: (invocation) => {
      const nodes = (invocation.args[0] ?? []) as ObjectNode[];
      api.tracks.transaction(() => {
        for (const node of nodes) {
          const track = api.tracks.track(node);
          if (track) track.mute = !track.mute;
        }
      });
      return ok();
    },
  });
}

run gets the parsed invocation. invocation.args[0] holds the Live objects of the first argument, each already resolved to exactly one object. A successful write returns ok() with no message, and the palette stays silent.

Rules

  • A command does one thing. Words are commands and subcommands; --flags are options; an action is never a flag.
  • The handler never parses raw input, never resolves paths, never reaches the SDK or the socket directly and never shows a dialog for an error. It returns a CommandResult: ok(), or fail(ExitCode.…, message, details).
  • Nothing throws at a user action. An uncaught error stops the whole Extension Host. The platform turns a throw from a handler into exit code 1, but catch what you can and return a result.
  • Write several targets in one api.tracks.transaction. Its callback must be synchronous: await before it, not inside.
  • Prefer setting over toggling where a re-run from history should mean the same thing (mute --set 1).
  • Declare kinds on path arguments unless the command really means every kind.

Options and arguments

An ArgDef has a kind (path, enum, text or number), and required, variadic (takes all remaining tokens), rest (takes the rest of the line as one value) and kinds (which objects a path matches). An OptionDef has a name, a short form and a summary. For an option with a value:

Field What it does
takesValue The option takes a value: --set 1 or --set=1.
choices The allowed values. The parser rejects anything else with a usage error that lists them.
number { min, max, integer }: the value must be a number in that range. The handler gets the text.
values + valueLabels The option takes N values, as in --rename <old> <new>. The handler reads them from invocation.values[name]; invocation.options[name] is true.
valueDefault The value when the option is given without one: tag -e acts like tag -e #.

--help and --pick are reserved option names, and -h is the reserved short form.

Two more fields on a command help it behave like the others:

  • requires: ["bridge"] says the command cannot work without the Remote Script. The platform checks it before it resolves anything or asks the user to pick, and answers exit code 5 when the script is missing.
  • pickState makes the pick list show a state button on each row: read(nodes, invocation) gives the state per row, and change(invocation) says what Enter would do ("toggle", "on" or "off").

What a command shows

A result goes to one of three places:

  • A dialog for a failure and for output the user asked for. fail(…) shows its message; ok("text") shows the text.
  • Silence for a successful write: ok() with no message.
  • The log for everything else. CommandResult.log holds lines that the platform writes to the log at info under the command's name. The presenter never shows them. A successful write that has something to report returns only log. Put what changed there, and the commands that undo it if Cmd+Z cannot.

A write that changes nothing should say so with a message ("/Bass already has #bass"), because a silent result would look like success. A failure that touches several targets returns one details entry per target.

Reaching Live: the ports

A module never touches the SDK or the socket. It uses the ports on api, so a command's run can be tested with fakes (createFakeApi in src/modules/shared/testPorts.ts, which fakes set.id and files too).

Port Use it for
api.bridge Calls to the Remote Script (select, trackFacts, chainSet, …) through the typed functions in src/ableton/bridgeOps.ts. It never throws; a failure is a result with the bridge's exit code and text, which you pass on unchanged.
api.liveSet.nodes() The Set's objects (tracks, devices, chains and pads), flattened, for find-like commands and the palette rows.
api.tracks transaction(fn) (one undo step; fn is synchronous, but it may return a promise to group async writes into the step), track(node) → { mute, solo, arm } or null, volume(node) → Promise<number \| null> and setVolume(node, v) → Promise<boolean>. volume is 0..1, where 0.85 is 0 dB, for regular, return, group and main tracks; null when the node is no track or the read fails. setVolume clamps to 0..1 and resolves false for no track or a failed write. The dB ↔ value conversion (Live's measured curve) is in src/modules/shared/mixValue.ts. SDK-backed, so it works without the Remote Script.
api.set.id(create?) Promise<string \| null>: the Set's own id, kept in the Set. With create it is made if missing. Null when the Set has no id yet (without create) or the id could not be read (no or old Remote Script, no token, timeout), so never treat null as "no id". The op needs the bridge token, also for a read.
api.files.read(name) and api.files.write(name, text) Promise<string \| null> and Promise<boolean>: text files in a folder of your module in cmdAbl's data folder. Names are plain: no /, \ or .., no leading dot, at most 100 characters. UTF-8, written atomically, at most 1 MB per write. A missing or unreadable file reads null. Nothing throws.
api.tags Track tags: refresh() reads them from the script, of(node) returns a track's tags, write(node, tags) replaces them. Names must be normalised by the caller.
api.pick(title, candidates, multi?, options?) The pick list, when a command has to ask. options.state gives each row a state button: { label, values, change? } with values[i] true, false or null.
api.pickRows(title, rows, options?) A single-select list of plain rows. rows are { label, detail?, disabled? }; a disabled row is shown dimmed and cannot be picked. options.cursor is the index the cursor starts on. It resolves with the chosen row's index, or null.
api.pickTags(title, rows, options?) A list of tags. rows are { name, count?, on? }. options.multi: false makes it single-select, options.allowNew adds a row that creates a new name, options.change previews what Enter does. It resolves with the chosen names, or null.
api.settings declare({ heading, items }) once at activation, after the first commands.add (or once Module.name is set), and get(name) → Promise<value>, the full key (tag.editView) or the part after the module's name (editView); the declared default when the file has no or a wrong value, undefined for a key nobody declared. Every key must be <module>.<name>, else it is dropped with a warning. Kinds: bool, enum (choices) and int (min…max). Modules don't write settings: only the Settings page does, and get reads the file again so a new value applies at the next run. The platform adds <module> config (e.g. tag config) to every module that declares settings (from 1b on, every module with a command), opening just that group. config is therefore a reserved word at the first position: tag config opens the settings, and tag "config" addresses a track called config.
api.confirm(message, confirmLabel) A yes/no question. Cancel is the first row, so a reflex Enter never acts.
api.dialog(html, width, height, name?, { gear? }) A page of the module's own, through the dialog queue; resolves with the string the page returns, or null when dismissed. With gear: true the page shows a gear and answers {"settings":true}: core then opens <module> config and runs the line again, so write only after the page closes. Where the gear can't work (a map binding, a page run, a ; chain, a headless run, a command without settings) you get the raw string {"settings":true}: treat it as closed. Pass api.settings.gearAvailable() as gear (and into the page data) so the page hides the gear where it can't work.
api.ui.livePage(name, html, width, height, routes, options?) A page served from http://localhost:27184 with a one-time token per dialog. While it is open it may read from your module through routes, and it may run the command lines you allow in options.run. It resolves like dialog. See "Live pages and page runs" below.
api.commands.run(line, options?) Run a command line through the normal command path from your handler and get its CommandResult back (see "Running command lines" below). Only paths in your command's runs.
api.log(line) One log line at info: cmdAbl [<module>] info: …. The platform adds the prefix; write the text only.
api.logger.{debug,info,warn,error}(text) The same, at the level you name. debug lines show only after cmdabl log --debug.
api.commands add, plus roots(), help(path) and usage(path) for commands such as man, and defaults(): the default commands (bare command word → the line it runs, read-only, fresh in any handler; may be empty at activation).
api.registerProvider, api.onPick, api.onCommandRun, api.showFeedback Palette rows, a listener for picks, a listener for finished commands, and a small message.

A module that has its own page puts the HTML in ui/modules/<module>/. A module may import a page only from its own folder.

Live pages and page runs

A page that opens with api.dialog gets JSON in and returns one string when it closes; everything that changes Live happens afterwards, in a command. A live page (api.ui.livePage) is the one exception. It is served from http://localhost:27184 and may read from your module while it is open, through the routes you pass. It still works when the server cannot be reached; then the page's LIVE value is null, and the page should fall back to what a plain dialog does.

A live page may also run command lines while it is open, but only those your module allows when it opens the page:

api.ui.livePage("note", html, width, height, routes, { run: ["loop here", "loop back"], onClose: "loop back" });
  • onClose (optional) is one command line the platform runs headless after the page has closed, but only if the page ran at least one line meanwhile (a safety net, here loop back). It must pass the same allowlist check as run.
  • Each entry is a command path: the command and its subcommands, as typed. The page may run a line only if its parsed command path is in the list. Options and arguments are free. A line outside the list is refused (exit code 2 to the page, a warning in the log).
  • The page sends POST <base>/run with { "line": "…" }, using the same token and origin check as the read routes. The answer is { "code", "text" }: the command's result.
  • The line goes through the normal command path: it is parsed and resolved, it makes one undo step, it is logged and its errors are caught. It runs without dialogs, so a part that would ask ends with exit code 4. It is not added to the history.
  • The line runs nested, while the command that opened the page still waits for its dialog. So an allowed command must not wait for that dialog or for the opening command. livePage resolves after the runs in flight have finished; a run that arrives after the page closed gets 404.

Nothing else writes. The page still answers with one string when it closes.

Running command lines

A handler may run another command line with api.commands.run(line, { allow? }), for example tag -l runs the line the setting tag.listEnter names (default find #fx) when you press Enter on a tag. The rules (plan 0028):

  • Declare runs on your command (a leaf): the command paths it may run, as plain paths, e.g. runs: ["find", "tag", "mute", "solo"]. No options in an entry. A line whose command path is not listed is refused with exit 2 and a warning in the log; so is a ; chain with an unlisted part. allow on the call can only narrow the list.
  • outside: true marks a command that acts outside the Live Set (it installs or removes things, edits the setup): no allowlist may contain it, so no module or page can run it. cmdabl setup and pakabl install, uninstall, upgrade have it.
  • Depth 1. A line run this way cannot run further lines.
  • The result comes back to you: run resolves with the other command's CommandResult; return it from your handler (return api.commands.run(line)) so the user sees the same result. A dialog the line opens goes through the queue; Esc in it returns ok() quietly. Inside a headless run (a map binding, a chain, a page run) the line runs headless too.
  • The line goes through parsing, resolution and the log like a typed one (run: find #x -> 0 (from tag -l)). api.settings.gearAvailable() is false inside an inner run, so a page opened that way shows no gear.
  • A live page's run list is checked against the same runs of the command that opened it, so declare the paths there too (the note commands declare runs: ["loop here", "loop back", "jump"]). man <command> shows what a command may run.

Completing arguments

#tag targets and Live paths are built in, so a target needs no completer. For other values, such as an extension id or a tag name, give the argument or option a completer:

  1. In activate, register a source: api.commands.completer(key, source).
  2. Name the key in ArgDef.complete or OptionDef.complete.

An option with complete must take a value, and cannot also have choices or number. For an enum whose values change at run time, use kind: "enum" without choices, and name the completer in complete.

source(ctx) returns a list of string | { label, insert?, description? }, directly or as a promise. insert is the raw value; the platform quotes it when needed.

  • The platform calls source once each time the palette opens, with no tokens. It filters the rows while the user types and shows at most about 100 rows per position.
  • A source is read-only: no writes, no dialogs, no bridge calls. Read from caches (the platform refreshes tags and chains when the palette opens).
  • Stay within about 100 ms. If source throws or times out, the user sees no rows and a debug log line is written. Work that is not async cannot be interrupted, so keep it cheap.
  • A key that is not registered gives a warning at activation. Module tests assert that fake.unknownCompleters() is empty.

Logging

Never call console. in a module; the architecture test fails on it. Log through api.log or api.logger. Never put a token, a secret or an environment value in a log line.

Test it

Build the fake with createFakeApi({ nodes, bridge }), activate the module on fake.api, and call the declared command's run with an invocation. Add a docs.test.ts that runs assertDocumented, so every example line must parse:

import { test } from "node:test";
import { assertDocumented, declaredBy } from "../docs.testkit.js";
import { activate } from "./index.js";

test("flip: the command is documented and its examples parse", () => {
  assertDocumented(declaredBy((api) => activate(api)), ["flip"]);
});

Documentation fields

These texts are the single source for <command> --help, for man <command> and for the generated command reference (npm run docs:reference). Write them for someone using Live, not for a developer.

Field On What goes in
summary command, option One line: what it does.
description command, group A sentence or two beyond the summary: what it needs (the Remote Script, macOS), what it changes, what it refuses. Plain text; no markdown backticks, they show literally in --help.
examples runnable command At least one { line, note? }: the line exactly as typed, and a few words of note. Add one per form that differs (several targets, a quoted path, a flag).
summary each argument What to type there.
topic top-level command The heading help and the command reference list it under: "Find and select", "Mix", "Tags", "Look things up", "Recent commands" or "Extensions" (core sets the topic of cmdabl; the man module that of help). The topics appear in that fixed order, and the commands of a topic in the order their modules are listed in src/modules/index.ts. A missing or unknown topic puts the command under "More commands".