# page

Read logged events after a cursor.

`runtime.page(scope, after, limit, request_id)` reads events from the session log in sequence order. Without a request ID, it reads the whole scope. With one, it reads only that turn's events. It works before `open`, so readers can catch up from storage while another runtime drives turns.

```ts title="host.ts"
import type { AgentRuntime } from 'nanocode-wasm'

declare const runtime: AgentRuntime

// Reads the whole log, 100 events at a time
let cursor = 0
for (;;) {
  const page = await runtime.page('owner', cursor, 100)
  for (const entry of page.events) console.log(entry.seq, entry.event)
  cursor = page.next_cursor
  if (!page.has_more) break
}
```

## Signature

```ts
// Events with seq greater than after
page(
  scope: string,
  after: number,
  limit: number,
  request_id?: string,
): Promise<LogPage>
```

## Parameters

### scope

* **Type:** `string`

The session log scope, the same value as `config.scope`.

```ts
// Reads the first page of the scope
await runtime.page('owner', 0, 100)
```

### after

* **Type:** `number`

The cursor. The page holds events whose `seq` is greater than this value. Start at `0` and pass the previous page's `next_cursor`.

```ts
declare const lastSeen: number
// Resumes after the last event the client received
await runtime.page('owner', lastSeen, 100)
```

### limit

* **Type:** `number`

The maximum number of events in the page.

```ts
// Reads at most 10 events
await runtime.page('owner', 0, 10)
```

### request_id

* **Type:** `string`

Reads only the events of this turn, from its `first_seq` to its `last_seq`. A cursor before the turn starts at the turn's first event.

```ts
// Replays one turn's events
await runtime.page('owner', 0, 100, 'turn-1')
```

## Returns

* **Type:** `Promise<LogPage>`

```ts
type LogPage = {
  // Events in sequence order
  events: LoggedEvent[]
  // The cursor for the next page
  next_cursor: number
  // The page is full, so more events can follow
  has_more: boolean
}

type LoggedEvent = {
  seq: number
  request_id: string | null
  created_at: number
  event: unknown
}
```

### events

* **Type:** `LoggedEvent[]`

The events, each with its sequence number, its turn's request ID or `null`, its creation time in milliseconds since the epoch, and the event JSON.

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

declare const page: LogPage

// Lists the event types
for (const entry of page.events)
  console.log((entry.event as { type?: string }).type)
```

### next_cursor

* **Type:** `number`

The `seq` of the last event in the page, or the cursor that the page started from when it is empty.

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

declare const page: LogPage

// The cursor for the next call
const after = page.next_cursor
```

### has_more

* **Type:** `boolean`

`true` when the page holds `limit` events, so the next page can hold more.

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

declare const page: LogPage

// Stops paging at the end of the log
if (!page.has_more) console.log('caught up')
```

## Errors

The promise rejects if the SQL host throws or the runtime is closed.

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

declare const runtime: AgentRuntime

// Rejects with the transaction error
await runtime.page('owner', 0, 100).catch(console.error)
```
