hex/src/personal_commands/host.js
Dax 6b988c493c
Run personal commands in embedded QuickJS instead of Bun (#300)
* 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
2026-10-28 15:06:45 -04:00

110 lines
4.6 KiB
JavaScript

// Hex's side of a loaded config: the registration Rust compiles, and the
// handler and transformation calls it makes. Values cross as JSON strings.
import { openApplication, openPath, openUrl, press, typeText } from "@hex/commands"
const ACTIONS = new Set(["openUrl", "openApplication", "openPath", "press", "typeText"])
const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value)
const isAction = (value) => isRecord(value) && ACTIONS.has(value.type)
const parse = (json) => Object.freeze(JSON.parse(json))
function registerTransformations(config, transforms) {
if (config.transformations === undefined) return []
if (!isRecord(config.transformations)) throw new Error("transformations must be an object")
return Object.entries(config.transformations).map(([id, definition]) => {
if (!isRecord(definition)) throw new Error(`transformations.${id} must be an object`)
if (typeof definition.name !== "string") throw new Error(`transformations.${id}.name must be a string`)
if (typeof definition.transform !== "function") {
throw new Error(`transformations.${id}.transform must be a function`)
}
transforms.set(id, definition.transform)
return { id, name: definition.name, description: definition.description }
})
}
function registerCommands(config, handlers) {
return Object.entries(config.commands).map(([id, definition]) => {
if (!isRecord(definition)) throw new Error(`commands.${id} must be an object`)
if ((definition.action === undefined) === (definition.run === undefined)) {
throw new Error(`commands.${id} must contain exactly one of action or run`)
}
if (!Array.isArray(definition.phrases)) throw new Error(`commands.${id}.phrases must be a list of phrases`)
const field = definition.action === undefined ? "run" : "action"
const value = definition[field]
let execution
if (typeof value === "function") {
handlers.set(id, value)
execution = { type: "handler" }
} else if (isAction(value)) {
execution = { type: "native", action: value }
} else {
throw new Error(`commands.${id}.${field} must be a function or an action such as openUrl(...)`)
}
const when = definition.when
if (when !== undefined
&& (!isRecord(when) || (when.application === undefined) === (when.browserHost === undefined))) {
throw new Error(`commands.${id}.when must contain exactly one of application or browserHost`)
}
return {
id,
phrases: definition.phrases,
group: definition.group,
description: definition.description,
when,
captures: definition.captures,
execution,
}
})
}
export function install(config) {
if (!isRecord(config) || !isRecord(config.commands)) {
throw new Error("The config's default export must be a Hex config with a commands object")
}
const transforms = new Map()
const handlers = new Map()
const registration = JSON.stringify({
dictation: config.dictation,
transformations: registerTransformations(config, transforms),
commands: registerCommands(config, handlers),
})
return {
registration,
async invoke(id, contextJson, capturesJson) {
const handler = handlers.get(id)
if (handler === undefined) throw new Error(`Command ${id} has no handler`)
const actions = []
const queue = (action) => {
actions.push(action)
return Promise.resolve()
}
const hex = Object.freeze({
openUrl: (url) => queue(openUrl(url)),
openApplication: (application) => queue(openApplication(application)),
openPath: (path) => queue(openPath(path)),
press: (input) => queue(press(input)),
typeText: (text) => queue(typeText(text)),
})
const returned = await handler({ hex, context: parse(contextJson), captures: parse(capturesJson) })
for (const action of returned === undefined ? [] : Array.isArray(returned) ? returned : [returned]) {
if (!isAction(action)) {
throw new Error(`Command ${id} must return an action, a list of actions, or nothing`)
}
actions.push(action)
}
return JSON.stringify(actions)
},
async transform(idsJson, text, contextJson) {
const context = parse(contextJson)
let output = text
for (const id of JSON.parse(idsJson)) {
const transform = transforms.get(id)
if (transform === undefined) throw new Error(`Unknown transformation: ${id}`)
output = await transform(output, context)
if (typeof output !== "string") throw new Error(`Transformation ${id} did not return a string`)
}
return output
},
}
}