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,sortableandgroupabletofalse.withAiColumnsthrowsinvalid_columnotherwise, andunknown_columnfor an id that is not in the schema. - Set
ai: true.tally-reactthen 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 ofbatchSize, 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 reportslive: 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,booleanordate - tinted options for an enum
ai: true, andfilterable,sortable,groupableandeditableset tofalse
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
useGrid recreates the grid with the current view. The shared cache keeps values already filled.