Examples
Examples
Section titled “Examples”Show a command as unavailable
Section titled “Show a command as unavailable”import { createWhistle } from "@fungi.computer/whistle";
let signedIn = false;const whistle = createWhistle();whistle.contribute({ sourceId: "account", namespace: "account", commands: [ { id: "sync", title: "Sync now", category: "Account", projections: { palette: {}, agent: {} }, availability: () => signedIn ? { status: "available" } : { status: "unavailable", reason: "Sign in first." }, handler: () => undefined, }, ],});
console.log(whistle.snapshot().palette[0]?.availability);// { status: "unavailable", reason: "Sign in first." }console.log(await whistle.execute("account:sync", { origin: "agent" }));// { status: "unavailable", reason: "Sign in first." }signedIn = true;Fail with a code
Section titled “Fail with a code”import { WhistleActionError, createWhistle } from "@fungi.computer/whistle";
const whistle = createWhistle();whistle.contribute({ sourceId: "files", namespace: "files", commands: [ { id: "open", title: "Open file", category: "File", handler: () => { throw new WhistleActionError("not_found", "No such file", { path: "a.txt", }); }, }, ],});
const outcome = await whistle.execute("files:open", { origin: "terminal" });// { status: "failed", error: { code: "not_found", message: "No such file", details: { path: "a.txt" } } }Load a backend catalog
Section titled “Load a backend catalog”import { createWhistle } from "@fungi.computer/whistle";import { catalogCommands, parseCatalog } from "@fungi.computer/whistle/catalog";
const catalog = parseCatalog(await (await fetch("/commands")).json());const whistle = createWhistle();whistle.contribute({ sourceId: "backend", namespace: "backend", commands: catalogCommands(catalog, async (commandId, args, signal) => { const response = await fetch(`/commands/${commandId}`, { method: "POST", body: JSON.stringify(args), signal, }); return response.json(); }),});Checks
Section titled “Checks”From a checkout, the unit tests cover the runtime, wire schemas, interaction host and catalog:
pnpm --filter @fungi.computer/whistle test