import { validateUnion } from "./captures.js" export const PROTOCOL_VERSION = 2 as const export type Modifier = "command" | "control" | "option" | "shift" export type NativeAction = | { readonly type: "openUrl"; readonly url: string } | { readonly type: "openApplication"; readonly application: string } | { readonly type: "openPath"; readonly path: string } | { readonly type: "press" readonly key: string readonly modifiers?: readonly Modifier[] readonly repeat?: number } | { readonly type: "typeText"; readonly text: string } export interface PressOptions { readonly key: string readonly modifiers?: readonly Modifier[] readonly repeat?: number } /** Open an absolute web URL or app deep link with its macOS handler. */ export const openUrl = (url: string): NativeAction => Object.freeze({ type: "openUrl", url }) /** Launch an application by name or path. Use openUrl for app deep links. */ export const openApplication = (application: string): NativeAction => Object.freeze({ type: "openApplication", application }) export const openPath = (path: string): NativeAction => Object.freeze({ type: "openPath", path }) export function press(input: string | PressOptions): NativeAction { return typeof input === "string" ? Object.freeze({ type: "press", key: input }) : Object.freeze({ type: "press", key: input.key, ...(input.modifiers === undefined ? {} : { modifiers: Object.freeze([...input.modifiers]) }), ...(input.repeat === undefined ? {} : { repeat: input.repeat }), }) } export const typeText = (text: string): NativeAction => Object.freeze({ type: "typeText", text }) export interface HexCapabilities { /** Open an absolute web URL or app deep link, such as slack://channel?team=...&id=... . */ readonly openUrl: (url: string) => Result /** Launch an application by name or path. Use openUrl for app deep links. */ readonly openApplication: (application: string) => Result readonly openPath: (path: string) => Result readonly press: (input: string | PressOptions) => Result readonly typeText: (text: string) => Result } export interface PromiseHex extends HexCapabilities> {} export interface DigitCapture { readonly type: "digit" readonly min: number readonly max: number } export interface TextCapture { readonly type: "text" } export type Letter = | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" export interface LetterCapture { readonly type: "letter" } export interface ChoiceCapture { readonly type: "choice" readonly choices: Readonly> } export type AtomicCaptureDescriptor = DigitCapture | LetterCapture | ChoiceCapture export interface UnionCapture { readonly type: "union" readonly members: Members } export type BoundedCaptureDescriptor = AtomicCaptureDescriptor | UnionCapture export type CaptureDescriptor = BoundedCaptureDescriptor | TextCapture export type CaptureSchema = Readonly> type CaptureValue = Descriptor extends DigitCapture ? number : Descriptor extends LetterCapture ? Letter : Descriptor extends ChoiceCapture ? Value : Descriptor extends UnionCapture ? CaptureValue : string export type CaptureValues = Readonly<{ [Name in keyof Schema]: CaptureValue }> export type UntypedCaptures = Readonly> export const digit = (options: { readonly min: number; readonly max: number } = { min: 0, max: 9 }): DigitCapture => Object.freeze({ type: "digit", min: options.min, max: options.max }) export const text = (): TextCapture => Object.freeze({ type: "text" }) export const letter = (): LetterCapture => Object.freeze({ type: "letter" }) export function choice( values: Values, ): ChoiceCapture export function choice>>( choices: Choices, ): ChoiceCapture> export function choice( input: readonly string[] | Readonly>, ): ChoiceCapture { const entries: readonly (readonly [string, readonly string[]])[] = Array.isArray(input) ? input.map((value) => [value, [value]] as const) : Object.entries(input) const grouped = new Map() for (const [value, aliases] of entries) { grouped.set(value, [...(grouped.get(value) ?? []), ...aliases]) } const choices = Object.fromEntries( [...grouped].map(([value, aliases]) => [value, Object.freeze(aliases)]), ) return Object.freeze({ type: "choice", choices: Object.freeze(choices) }) } type FlattenedUnionMember = Member extends UnionCapture ? Nested[number] : Member export function union< const Members extends readonly [BoundedCaptureDescriptor, BoundedCaptureDescriptor, ...BoundedCaptureDescriptor[]], >(...members: Members): UnionCapture, AtomicCaptureDescriptor>[]> export function union(...members: readonly CaptureDescriptor[]): UnionCapture { return validateUnion(members, "union()") } export interface HandlerArguments>> { readonly hex: PromiseHex readonly context: HandlerContext /** * Free text matched by `{name}` placeholders in the spoken phrase, keyed by * placeholder name. A placeholder captures the normalized trailing words, * so "search amazon for {query}" invoked as "search amazon for wool socks" * yields `{ query: "wool socks" }`. Empty for plain phrases. */ readonly captures: Captures } export interface HandlerContext { readonly application?: string readonly browserHost?: string readonly browserUrl?: string readonly windowTitle?: string } export type Handler>> = (arguments_: HandlerArguments) => void | Promise export type Transformation = (text: string, context: HandlerContext) => string | Promise export interface TransformationDefinition { readonly name: string readonly description?: string readonly transform: Transformation } export interface CommandMetadata { readonly phrases: readonly [string, ...string[]] readonly group?: string readonly description?: string readonly when?: | { readonly application: string; readonly browserHost?: never } | { readonly browserHost: string; readonly application?: never } } export interface DictationProtocolConfig { readonly start: readonly [string, ...string[]] readonly stop: readonly [string, ...string[]] readonly send: readonly [string, ...string[]] readonly cancel: readonly [string, ...string[]] } export type CommandDefinitionFor = CommandMetadata & ( | { readonly captures?: never; readonly action: NativeAction; readonly run?: never } | { readonly captures?: never; readonly action?: never; readonly run: NativeAction | Run } ) export type CapturesFor = [Schema] extends [never] ? Readonly> : [Schema] extends [CaptureSchema] ? CaptureValues, CaptureSchema>> : Readonly> type PromiseCommandFor = CommandMetadata & { readonly captures?: Schema & CaptureSchema } & ( | { readonly action: [Schema] extends [never] ? NativeAction : [Schema] extends [CaptureSchema] ? never : NativeAction readonly run?: never } | { readonly action?: never readonly run: [Schema] extends [CaptureSchema] ? Handler> : NativeAction | Handler> } ) export type TypedCommandDefinition = PromiseCommandFor export interface HexConfigFor { readonly dictation?: DictationProtocolConfig readonly transformations?: Readonly> readonly commands: Readonly> } export type CommandDefinition = CommandDefinitionFor export interface HexConfig extends HexConfigFor {} export const defineHexConfig = < const Captures extends Readonly>, const Extra extends Omit, >(config: Extra & { readonly commands: { readonly [Id in keyof Captures]: PromiseCommandFor } }): typeof config => config