* Run personal commands in embedded QuickJS instead of Bun Hex now runs ~/.config/hex/hex.config.ts in-process: rquickjs on one worker thread, TypeScript erased to same-position JavaScript with oxc's parser, per-call time limits and a runtime memory limit, and no filesystem, network, process, or timer API. Handlers return actions that Hex validates and runs on its native executor. The Bun host, managed SDK provisioning, bun update, the sdk/commands workspace, and their scripts and CI steps are gone; Hex writes hex.d.ts typings instead, and hex commands check replaces bun run check. Plan and measurements: docs/plans/quickjs.md. * Spell out a range check that a new dependency's impls made ambiguous
6.4 KiB
Hex Custom Commands
This folder configures custom voice commands, typed captures, dictation control
phrases, and text transformations for Hex. Edit hex.config.ts (or
hex.config.js; keep only one). hex.d.ts types the @hex/commands module;
Hex owns it, so do not edit it.
Hex runs the config itself in an embedded JavaScript engine (QuickJS) and
reloads it about a third of a second after any .ts, .js, or .json file
here changes. A config that fails to load keeps the previous one active; the
error, with its file, line, and column, shows in the Modes pane's
Transformations section. On macOS, hex commands check loads the config as Hex
does and prints what it registers or why it fails, phrase overlaps included.
No install step and no Node, Bun, or npm are involved.
What a script can do
- Plain modern JavaScript (ES2023 modules). TypeScript works too: Hex erases
type annotations, interfaces, type aliases, generics,
as,satisfies, and!before running, keeping lines and columns. Syntax that would need generated code is rejected: enums, namespaces, constructor parameter properties, andimport x = require(). - Import only
@hex/commandsand other files in this folder by relative path (./lib.ts,./data.json). npm packages are not available. - No filesystem, network, process, environment, or timer APIs (
fetch,setTimeout,require,processdo not exist).console.logwrites to Hex's log. - Each handler or transformation call must finish within one second; a script that runs longer is stopped and fails that call. Scripts share a 64 MiB memory limit.
To check types in an editor or agent, run npx -y -p typescript tsc here.
That checks TypeScript, not phrase overlaps; Hex reports those on load.
Commands
- Import
defineHexConfigfrom@hex/commands. Command keys are stable IDs. - A command has
phrasesand exactly one ofaction(a fixed action) orrun(a handler). - A handler receives
{ hex, context, captures }. It returns an action, a list of actions, or nothing, and may also queue actions withhex.openUrl(...),hex.press(...), and the rest. Hex runs the queued actions, then the returned ones, in order after the handler returns; the first failing action fails the command. - Actions:
openUrl(url)(absolute web URLs and app deep links such asslack://channel?team=T_EXAMPLE&id=C_EXAMPLE; notfile:,javascript:, ordata:),openApplication(nameOrPath),openPath(path),press("k")orpress({ key, modifiers, repeat })(a character, orhome,end,up,down,left,right,enter,escape; modifierscommand,control,option,shift; repeat 1 through 100), andtypeText(text). contexthasapplication,browserHost,browserUrl, andwindowTitlewhen available.whenaccepts exactly one ofapplicationorbrowserHost. Browser-host commands take precedence over application commands, then global commands.- Phrases must not overlap a command whose context can coexist at the same specificity, or any protected native phrase. Hex rejects the whole config when they do.
- Spoken commands and dictation controls require Hex's Commands opt-in, which defaults off. A config does not enable it.
import { defineHexConfig, openApplication, press } from "@hex/commands"
export default defineHexConfig({
commands: {
slack: { phrases: ["open slack"], action: openApplication("Slack") },
"slack-search": {
phrases: ["search slack"],
run: () => [openApplication("Slack"), press({ key: "k", modifiers: ["command"] })],
},
"type-app": {
phrases: ["type app name"],
run: ({ hex, context }) => hex.typeText(context.application ?? "unknown"),
},
},
})
Captures
End a phrase with one {name} placeholder to capture the rest of the spoken
words as captures.name: ASCII letters lowercased, punctuation trimmed from
word boundaries, whitespace collapsed, and spoken digits zero through nine as
0 through 9. Captures need at least one spoken word before the
placeholder and one or more words after it (at most 24 words, 512 bytes).
Capture phrases require run.
"search-amazon": {
phrases: ["search amazon for {query}"],
run: ({ captures }) =>
openUrl(`https://www.amazon.com/s?k=${encodeURIComponent(captures.query ?? "")}`),
},
For typed captures, declare a captures schema; every phrase must bind each
name exactly once:
digit()(ordigit({ min, max })within 0 through 9) arrives as a number and may appear anywhere.letter()accepts letters, spoken names ("bee"), and NATO names ("bravo"), and arrives as the lowercase letter.choice(["left", "right"] as const)returns the spoken value;choice({ left: ["left", "back"], right: ["right", "forward"] } as const)returns the key for any of its one-word aliases.union(letter(), digit(), choice(["home", "end"] as const))composes bounded captures whose spoken words do not overlap.text()captures the trailing words as a string and must be last.
"control-key": {
phrases: ["control {key}"],
captures: { key: union(letter(), digit(), choice(["home", "end"] as const)) },
run: ({ captures }) => press({ key: String(captures.key), modifiers: ["control"] }),
},
Dictation controls
dictation replaces Hex's native voice-dictation phrases (not the capture
lifecycle). start phrases match at the beginning of an utterance while
listening; stop, send, and cancel match at the end during voice
dictation. Every list needs at least one phrase, and they must not collide.
dictation: {
start: ["begin note"],
stop: ["finish note"],
send: ["send note"],
cancel: ["discard note"],
},
Transformations
transformations registers named text functions that appear as optional
final steps in Hex's dictation modes. Selected transformations run in order
after the mode's corrections and optional AI rewrite, for Paste and Send, not
Voice Action or meetings. A transformation receives the text and the
foreground context and returns the new text. If any transformation fails,
Hex keeps the text from before the transformations.
transformations: {
"trim-whitespace": {
name: "Trim whitespace",
description: "Remove leading and trailing whitespace",
transform: (text) => text.trim(),
},
},
Do not reuse lowercase, spongebob-case, or no-trailing-punctuation; those
IDs are Hex's native built-in transformations.