Skip to main content
withAiColumns from tally-ai wraps any data source. Rows come back at once. The model fills the AI columns in batches, and each batch arrives as a live patch, so the grid updates the cells as they resolve.

Usage

mood.ts

Column rules

AI columns are computed on the client, after the source has answered the query. The source cannot filter, sort or group by a value it does not have.
  • Set filterable, sortable and groupable to false. withAiColumns throws invalid_column otherwise, and unknown_column for an id that is not in the schema.
  • Set ai: true. tally-react then draws the header with a sparkle, shows a shimmer while a value is pending and reveals it when it arrives.

Options

Batches and the cache

Every row a query returns is checked against the cache. A cached value is filled in at once. A missing one is queued. Queued rows go to the model in batches of batchSize, with up to concurrency calls at a time per column. The model receives each row as its key and its input columns, and answers with one value per key. The answer is parsed with the column type, so an enum gets one of its options and a number gets a number. A row the model cannot judge gets null. The cache key is the column, the row key and the input values. A value is computed again when one of its inputs changes. Pass the same cache to a new wrapper to keep values when the schema or the source is rebuilt. A failed model call leaves those cells empty. The rows are not queued again until they are loaded again.

Live patches

The wrapped source always reports live: true. Filled values reach the grid as patches with upserts. Patches from the inner source pass through, and their rows are filled too. Mutations go to the inner source unchanged.

Design a column from a prompt

designColumn(prompt, { model, schema, signal }) asks the model for a column definition and the instruction to fill it. The column comes back ready for withAiColumns:
  • a unique snake_case id, a label and a type: text, number, enum, boolean or date
  • tinted options for an enum
  • ai: true, and filterable, sortable, groupable and editable set to false
design.ts
schema.extend(columns, index) returns a new schema with the columns inserted at index.

The AI button

tally-react adds an AI button to the toolbar when the ai prop has an addColumn handler. The button opens a prompt, with suggestions listed under it. The handler receives the prompt and the grid and returns the id of the new column. The grid then animates the column in. A thrown error shows in the popover.
accounts.tsx
The added columns live in state. A new schema and a new source follow from them, and useGrid recreates the grid with the current view. The shared cache keeps values already filled.