WebAssembly Usage

Run Kaya programs inside a web application

Run a browser session

Open the real browser examples and choose Start browser runtime. The basic example needs no model provider. The homepage example uses its original typed input and generation call; configure your own admitted model, answer through the shared presentation, and observe actual model output.

After installing the complete static cohort, a web app can use its typed client without an HTTP daemon:

import { createKayaRuntime } from "./kaya-runtime/index.js";

const runtime = await createKayaRuntime({
  namespace: "my-example",
  files: [{ path: "sources/main.kaya", bytes: new TextEncoder().encode('"Hello, browser!"\n') }],
});
try {
  const session = await runtime.createSession({ program: "sources/main.kaya" });
  const connection = runtime.connectToSession(session.id);
  const done = new Promise<void>((resolve, reject) => {
    connection.on("done", () => resolve());
    connection.onConnectionError(reject);
  });
  // A live failure can arrive before the sequential initialization awaits it.
  void done.catch(() => undefined);
  try {
    await connection.whenOpen();
    const current = await runtime.getSession(session.id);
    if (current.state !== "completed" && current.state !== "failed") await done;
    console.log((await runtime.loadSessionLogSnapshot(session.id)).entries);
  } finally {
    connection.close();
  }
} finally {
  await runtime.close();
}

getSession restores the saved immutable session when needed. Fixed-snapshot history is authoritative; live generation frames are provisional. Reloading a saved run does not authorize reinvoking model work. submitInteraction, sendMessage and sendEvent retain their existing durable IDs and conflict rules. The website reuses RunnableKayaSnippet and its single shared presentation, including input cards, streaming and history recovery.

cancelSession requests cancellation; it is not deletion. The website observes terminal completion, then asks deleteSession to check original cleanup and remove stored data. A custody/storage refusal preserves the stored ID and history and reports its original structured diagnostic. close() awaits the original runtime resources and revokes temporary runtime URLs. Failed close retains the original owner; terminate() is abrupt disposal, not a healthy cleanup receipt.

Read artifacts through readFile using their confined mounted path, preserve their original digest-pinned references, and use VerifiedPresentationArtifactPort for fresh presentation URLs. An old supplemental URL is not durable byte authority. Presentation owners release their URLs and audio/renderer resources.

The full browser runtime is implemented and qualified through actual extracted package execution. Verified public archive availability is shown in WebAssembly Setup; a source build alone does not enable a download. The /try IDE remains a separate website milestone. See Command Line Usage for native execution.

Current component usage

Syntax analysis

From a source workspace, import the syntax wrapper in a Node/Bun TypeScript host. Its CommonJS and Buffer requirements are described in WebAssembly Setup.

import { parseProgram } from "@kaya/syntax-wasm";

const parsed = parseProgram('"Hello from Kaya"\n', "hello.kaya");
if ("error" in parsed) {
  console.error(parsed.error);
} else {
  console.log(parsed.graphs);
}

Parsing returns program graphs or the structured parser error. It does not run the graphs, call a model or start a daemon.

Present a running or saved session

@kaya/presentation accepts generated initialization and operation contracts, and returns transitions and snapshots from the Rust presentation kernel. Release each PresentationKernel or PresentationLiveSessionCore with free() when its owner closes.

@kaya/presentation-web supplies the reference controller, views and browser ports. The application installs its allowed local view modules and supplies session access, clock, durable input, artifact, audio, renderer and diagnostic ports. A saved log can be presented without redispatching model or tool calls; presentation does not grant permission to execute those calls. Follow the package README for initialization and lifecycle ownership.

packages/kaya-core-rs/kaya-host-browser implements host components such as origin-private files and SQLite storage. Component Worker fixtures qualify those components separately; the complete public runtime composes them with the shared executor. A component build alone is not proof of complete runtime execution.