# AgentRuntime.instantiate

Instantiate the nanocode WebAssembly module for one session scope.

`AgentRuntime.instantiate` creates a module instance bound to a SQL host and the host's event callbacks. The instance runs reader and log commands at once. Call [`open`](/docs/api/wasm/open) on it to start the SDK session. Compile the module once and instantiate it once per runtime.

```ts title="host.ts"
import { readFileSync } from 'node:fs'
import { AgentRuntime, type SqlHost } from 'nanocode-wasm'

declare const sql: SqlHost

// 1. Compile the module once per process
const module = await WebAssembly.compile(
  readFileSync('node_modules/nanocode-wasm/dist/nanocode_wasm.wasm'),
)

// 2. Instantiate a runtime that logs each committed batch
const runtime = await AgentRuntime.instantiate(module, sql, {
  published: (events) =>
    events.forEach((entry) => console.log(entry.seq, entry.event)),
})
```

## Signature

```ts
// A static method. The constructor is private.
static instantiate(
  module: WebAssembly.Module,
  sql: SqlHost,
  options: HostOptions,
): Promise<AgentRuntime>
```

## Parameters

### module

* **Type:** `WebAssembly.Module`

The compiled `nanocode_wasm.wasm`. Wrangler imports `nanocode-wasm/nanocode_wasm.wasm` as a `WebAssembly.Module`. Other hosts compile the file with `WebAssembly.compile` or `WebAssembly.compileStreaming`.

```ts
// In a browser, with a bundler that resolves the file URL
declare const url: string
const module = await WebAssembly.compileStreaming(fetch(url))
```

### sql

* **Type:** `SqlHost`

Runs the SDK's statements in atomic SQLite transactions. If `sql.value_limit` is set, `instantiate` sends it to the module before any other command. [SqlHost](/docs/api/wasm/SqlHost) documents the contract.

```ts
import type { SqlHost } from 'nanocode-wasm'

// Stores values up to 2 MiB
declare const transaction: SqlHost['transaction']
const sql: SqlHost = { transaction, value_limit: 2 * 1024 * 1024 }
```

### options.published

* **Type:** `(events: LoggedEvent[], bytes: number[]) => void`

Receives each batch of events that the SDK's `SessionEvents` pump logs. The batch is already committed and in sequence order. `bytes[i]` is the encoded JSON size of `events[i]`. [EventHub.publish](/docs/api/wasm/EventHub#publish) takes the same arguments.

```ts
import { AgentRuntime, EventHub, type SqlHost } from 'nanocode-wasm'

declare const module: WebAssembly.Module
declare const sql: SqlHost

// Forwards every batch to a hub that serves readers
const hub = new EventHub(
  () => 128,
  () => 4 * 1024 * 1024,
)
const runtime = await AgentRuntime.instantiate(module, sql, {
  published: (events, bytes) => hub.publish(events, bytes),
})
```

### options.wake

* **Type:** `() => void`

Runs after the SDK's remote Code Mode client receives a batch or finishes a callback. A host uses it to check quiescence again, for example with [`settle`](/docs/api/wasm/AgentRuntime#settle).

```ts
import { AgentRuntime, type SqlHost } from 'nanocode-wasm'

declare const module: WebAssembly.Module
declare const sql: SqlHost
declare function schedule(): void

// Rechecks quiescence when remote work progresses
const runtime = await AgentRuntime.instantiate(module, sql, {
  published: () => {},
  wake: () => schedule(),
})
```

## Returns

* **Type:** `Promise<AgentRuntime>`

A runtime that is not open. `submit`, `turn`, `pending`, `page`, `cancel`, `interrupted`, `retry`, `finish`, `import`, `catalog`, and `admit` work before `open`.

## Errors

The promise rejects if `WebAssembly.instantiate` fails or if the `configure` command for `value_limit` fails.

```ts
import { AgentRuntime, type SqlHost } from 'nanocode-wasm'

declare const module: WebAssembly.Module
declare const sql: SqlHost

// Rejects for a module built from another source
const runtime = await AgentRuntime.instantiate(module, sql, {
  published: () => {},
}).catch((error: unknown) => {
  console.error(error)
  return undefined
})
```
