Skip to content
Examples

Examples

Semantic command graph, projections and wire parsing

TS
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(
{ namespace: "account", id: "sync" },
{ origin: "agent" },
),
);
// { status: "unavailable", reason: "Sign in first." }
signedIn = true;
TS
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(
{ namespace: "files", id: "open" },
{ origin: "terminal" },
);
// { status: "failed", error: { code: "not_found", message: "No such file", details: { path: "a.txt" } } }
TS
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();
}),
});

From a checkout, the unit tests cover the runtime, wire schemas, interaction host and catalog:

TERMINAL
pnpm --filter @fungi.computer/whistle test