# zidane/views

> The views preset and the host side of views. Needs zod.

Import: `import { … } from 'zidane/views'`. Source: [src/views/index.ts](https://github.com/Tahul/zidane/blob/main/src/views/index.ts).

## Functions

### `createViewsPreset`

Function.

`zidane/views` (docs/VIEWS.md): the views preset, its builder, and the host
side of live data and view state.

Its own entry, so `zidane` and `zidane/presets` never load the view
registry: it needs zod, an optional peer dependency. Import this entry only
when you turn views on, and install zod with it.

```ts
function createViewsPreset(base?: ViewRegistry, options?: ViewsPresetOptions): Preset
```

**Parameters**

- `base?` (`ViewRegistry`): The components the model may use. Default: the core components.
- `options?` (`ViewsPresetOptions`, default `{}`): The host's renderer, when it has one.

**Returns** `Preset`

Source: [src/views/preset.ts:224](https://github.com/Tahul/zidane/blob/main/src/views/preset.ts#L224)

### `loadViewHostLive`

Function.

Loads the host side of extension view components (`views.get` migration, `views.resolve`) on first need.

```ts
function loadViewHostLive(): Promise<__module>
```

**Returns** `Promise<__module>`

Source: [src/views/index.ts:19](https://github.com/Tahul/zidane/blob/main/src/views/index.ts#L19)

### `loadViewStateRunner`

Function.

Loads the host side of view state (`views.setState`) on first need.

```ts
function loadViewStateRunner(): Promise<__module>
```

**Returns** `Promise<__module>`

Source: [src/views/index.ts:22](https://github.com/Tahul/zidane/blob/main/src/views/index.ts#L22)

### `viewSession`

Function.

The view methods of `session`, over its store's view port.

```ts
function viewSession(session: ViewSessionSource | undefined): ViewSession | undefined
```

**Parameters**

- `session` (`ViewSessionSource | undefined`): Any session (or `undefined` for a sessionless run).

**Returns** `ViewSession | undefined`

Source: [src/views/session.ts:40](https://github.com/Tahul/zidane/blob/main/src/views/session.ts#L40)

## Constants

### `viewsPreset`

Constant.

Preset that turns views on (docs/VIEWS.md): the prompt block, `describe_view_components`, codemode `view()` and the turn-end fence pass.

```ts
const viewsPreset: Preset
```

Source: [src/views/preset.ts:249](https://github.com/Tahul/zidane/blob/main/src/views/preset.ts#L249)

## Interfaces

### `SavedViewStorePort`

Interface.

What a session store offers for saved views. Every member is optional.

```ts
interface SavedViewStorePort
```

**Properties**

- `deleteSavedView?` (`(scope: string, key: string) => Promise<boolean>`): Deletes a saved view and every version. `false` when there was none.
- `getSavedView?` (`(scope: string, key: string, version?: number) => Promise<SavedViewRecord | null>`): One version of a saved view, migrated; no `version`: the latest. `null` when there is none.
- `getSavedViewMeta?` (`(scope: string, key: string) => Promise<SavedViewMeta | null>`): One saved view's meta, or `null`. Absent: `readSavedViewMeta` finds it in `listSavedViews`.
- `listPanelSavedViews?` (`(scope: string) => Promise<SavedViewMeta[]>`): The panel views of a scope, by key, read without the others. Absent: `readPanelSavedViews` filters `listSavedViews`.
- `listPinnedSavedViews?` (`(scope: string) => Promise<SavedViewMeta[]>`): The pinned views of a scope, in pin order, read without the others. Absent: `readPinnedSavedViews` filters `listSavedViews`.
- `listSavedViews?` (`(filter?: { scope?: string }) => Promise<SavedViewMeta[]>`): The saved views, newest first; with `scope`, of that scope only.
- `pruneSavedViews?` (`(keep: number) => Promise<number>`): Deletes every saved view's versions but its newest `keep`. Resolves to how many versions went.
- `savedViewCreator?` (`(scope: string, key: string) => Promise<string | null | undefined>`): Who saved a saved view first (`created_by`): a user id, `null` when none was recorded, `undefined` when there is no such view.
- `savedViewVersions?` (`(scope: string, key: string) => Promise<SavedViewVersion[]>`): The kept versions of a saved view, oldest first.
- `saveView?` (`(input: SaveViewInput) => Promise<SavedViewRecord>`): Saves a new version (the next number) and keeps the newest `input.keep` (default 20).
- `setSavedViewHeights?` (`(scope: string, key: string, version: number, heights: readonly ViewHeight[]) => Promise<void>`): Replaces the stored heights of one version. Unknown versions do nothing.
- `setSavedViewPinned?` (`(scope: string, key: string, pinned: boolean) => Promise<boolean>`): Pins (last) or unpins a saved view. `false` when there is none.
- `setSavedViewPinOrder?` (`(scope: string, keys: readonly string[]) => Promise<void>`): Orders the pinned views of a scope as `keys` lists them; the others keep their order after them.
- `setSavedViewRecordView?` (`(scope: string, key: string, version: number, view: ViewRecord) => Promise<void>`): Replaces the view of one version in place (a source fetch that changed no data). Unknown versions do nothing.
- `setSavedViewSurface?` (`(scope: string, key: string, into: SavedViewSurface) => Promise<boolean>`): Turns a saved view into a side panel (unpinned) or back into a home block (pinned, last), in one write.
  `false` when there is none.
- `setSavedViewTitle?` (`(scope: string, key: string, title: string) => Promise<SavedViewMeta | null>`): Gives a saved view the person's title (`titleSetByUser`), kept by later
  saves; `""` gives back the latest version's title. No new version.
  `null` when there is none; throws for a title `savedViewTitleOf` refuses.

Source: [src/views/saved.ts:295](https://github.com/Tahul/zidane/blob/main/src/views/saved.ts#L295)

### `ViewHostLiveDeps`

Interface.

What `createViewHostLive` reads.

```ts
interface ViewHostLiveDeps
```

**Properties**

- `broadcast` (`(signal: { components: string[]; kind: 'views:data' }) => void`): Sends `views:data` to every client.
- `liveData` (`() => boolean`): Whether `views` and `viewsLiveData` are on.
- `store` (`Pick<ViewStorePort, 'getViews'>`): The store that holds the views.
- `viewComponents` (`(sessionId: string) => Promise<ViewComponentsSource | undefined>`): The view components of a session's folder.

Source: [src/views/host-live.ts:38](https://github.com/Tahul/zidane/blob/main/src/views/host-live.ts#L38)

### `ViewSession`

Interface.

A session as the views feature sees it.

```ts
interface ViewSession
```

**Properties**

- `addView` (`(view: Omit<StoredView, 'sessionId'>) => Promise<StoredView | undefined>`): Store a view under this session's id. `undefined` when the store keeps no views.
- `canStore` (`boolean`): Whether the session's store keeps views.
- `getViews` (`(ids: readonly string[]) => Promise<StoredView[]>`): The stored views with these ids, in the order asked. Empty when unsupported.
- `id` (`string`): The session's id: every view stored through this object is filed under it.
- `listViewMeta` (`() => Promise<ViewMeta[]>`): Every view of the session as `ViewMeta`, oldest first. Empty when unsupported.
- `nextViewId` (`() => Promise<string | undefined>`): The next free view id (`v1`, `v2`, …), never reused. `undefined` when unsupported.
- `setViewHeights` (`(id: string, heights: readonly ViewHeight[]) => Promise<void>`): Replaces the stored heights of one view. Does nothing when unsupported.

Source: [src/views/session.ts:12](https://github.com/Tahul/zidane/blob/main/src/views/session.ts#L12)

### `ViewsPresetOptions`

Interface.

Options of [`createViewsPreset`](https://zidane.si/docs/reference/views.md#createViewsPreset).

```ts
interface ViewsPresetOptions
```

**Properties**

- `cache?` (`ViewSourceCache`): The host's source cache (`ViewHost.cache`): with URL, MCP or sessions sources on, every source of this agent's views is fetched through it, with the host's fetchers. Absent: each fetch has its own, with the URL fetcher alone.
- `mcpApps?` (`boolean`): MCP Apps (docs/VIEWS.md, MCP Apps): store the app of an MCP tool that declares one. Hosts pass `viewsCustomCode` and `viewsMcpApps`.
- `render?` (`() => ViewRender | undefined`): The host's view renderer. With one, the preset adds `view_preview`
  and the repair pass gets a screenshot of a view with problems.
- `save?` (`{ artifacts: boolean; keep?: number | 'all' }`): Saved views (docs/VIEWS-SAVED.md): the model can save views. `artifacts`: a save records a `view` artifact.
  `keep`: the versions each saved view keeps (`viewsVersionsKept`). Hosts pass it while `viewsSave` is on.
- `sessionActions?` (`boolean`): Session actions (docs/VIEWS-ORCHESTRATION.md): the prompt names them. Hosts pass it while `viewsOrchestration` is `draft`, `approve` or `trusted`, with a registry that shows the session props.
- `sessionsSource?` (`boolean`): The sessions source (docs/VIEWS-ORCHESTRATION.md): hosts pass it while `viewsOrchestration` is not `off`. Absent, the prompt and the catalog are as before.
- `sources?` (`{ mcp?: { allowed: readonly string[]; mode: ViewsMcpMode }; network: CustomNetwork }`): Live sources (docs/VIEWS-SAVED.md, Sources): the `viewsNetwork` they follow,
  and `viewsMcp` with `viewsMcpAllowed` for MCP sources. Absent, `blocked`
  and MCP `off`: every source is off and the prompt is as before.

Source: [src/views/preset.ts:187](https://github.com/Tahul/zidane/blob/main/src/views/preset.ts#L187)

### `ViewStateRunnerDeps`

Interface.

What `createViewStateRunner` reads.

```ts
interface ViewStateRunnerDeps
```

**Properties**

- `now?` (`() => number`): The clock of the rate limit. Default `Date.now`.
- `registry?` (`(sessionId: string, view: StoredView) => ViewRegistry | Promise<ViewRegistry>`): The registry a view runs against. Default: the core components, plus `ViewStateRunnerDeps.viewComponents` for a view that names one.
- `runner?` (`ViewRunner`): Where the view's code runs. Default: the shared worker pool.
- `store` (`Pick<ViewStorePort, 'getViews'>`): Where the stored views are read.
- `viewComponents?` (`(sessionId: string) => Promise<ViewComponentsSource | undefined>`): The view components of a session's folder. A view that names one runs against them too.

Source: [src/views/state-run.ts:148](https://github.com/Tahul/zidane/blob/main/src/views/state-run.ts#L148)

### `ViewStorePort`

Interface.

What a session store offers for views. Every member is optional.

```ts
interface ViewStorePort
```

**Properties**

- `getViews?` (`(sessionId: string, ids: readonly string[]) => Promise<StoredView[]>`): The full records of these ids, in the order asked. Unknown ids are left out.
- `listViewMeta?` (`(sessionId: string) => Promise<ViewMeta[]>`): Every view of the session, as [`ViewMeta`](https://zidane.si/docs/reference/chat-pure.md#ViewMeta), oldest first.
- `nextViewId?` (`(sessionId: string) => Promise<string>`): The next free view id of the session: `v1`, `v2`, … Never reused.
- `setViewHeights?` (`(sessionId: string, id: string, heights: readonly ViewHeight[]) => Promise<void>`): Replaces the stored heights of one view. Unknown ids do nothing.
- `upsertView?` (`(view: StoredView) => Promise<StoredView | null>`): Inserts or replaces the view `(sessionId, id)`. Resolves `null` for an unknown session.

Source: [src/views/store-types.ts:58](https://github.com/Tahul/zidane/blob/main/src/views/store-types.ts#L58)

## Types

### `ViewComponentsSource`

Type.

The view components of a session's folder: the extension runtime's UI registry.

```ts
type ViewComponentsSource = Pick<UiRegistry, 'viewComponents' | 'subscribe'>
```

Source: [src/views/host-live.ts:26](https://github.com/Tahul/zidane/blob/main/src/views/host-live.ts#L26)

### `ViewHostLive`

Type.

One host's live data and migration.

```ts
type ViewHostLive = ReturnType<typeof createViewHostLive>
```

Source: [src/views/host-live.ts:138](https://github.com/Tahul/zidane/blob/main/src/views/host-live.ts#L138)

### `ViewsSessionStore`

Type.

A session store with the ports of the views feature it may implement beside the core: views and saved views.

```ts
type ViewsSessionStore = SessionStore & ViewStorePort & SavedViewStorePort
```

Source: [src/views/store-types.ts:72](https://github.com/Tahul/zidane/blob/main/src/views/store-types.ts#L72)

### `ViewStateRunner`

Type.

One host's `views.setState`.

```ts
type ViewStateRunner = ReturnType<typeof createViewStateRunner>
```

Source: [src/views/state-run.ts:237](https://github.com/Tahul/zidane/blob/main/src/views/state-run.ts#L237)
