API Reference

@json-render/chat

Chat SDK renderer. Turn JSON specs into Chat SDK cards, which each adapter posts in its native format: Block Kit on Slack, Adaptive Cards on Teams, and the card format of the other platforms.

Install

npm install @json-render/core @json-render/chat chat zod

See the Chat SDK example for a playground that previews generated messages as Slack renders them.

schema

The chat element schema for specs. Use with defineCatalog from core.

import { defineCatalog } from "@json-render/core";
import { schema, standardComponentDefinitions } from "@json-render/chat";

const catalog = defineCatalog(schema, {
  components: standardComponentDefinitions,
});

The root element is a Card. Every component maps to a Chat SDK card element.

ComponentChat SDK element
CardThe card itself. An inner Card renders as a titled section, since cards cannot nest.
Sectionsection
Texttext, with markdown content
Fields and Fieldfields and field
Tabletable
Chartchart, as a pie, bar, line, or area chart
Dividerdivider
Imageimage
Actions, Button, and LinkButtonactions, button, and link-button

streamCard

Stream a model's spec into a chat message. streamCard posts the card once its root element exists, edits the message as elements arrive, and finishes with a validated card.

import { streamCard } from "@json-render/chat";
import { streamText } from "ai";

chat.onNewMention(async (thread, message) => {
  const result = streamText({
    model: "anthropic/claude-haiku-4.5",
    system: catalog.prompt(),
    prompt: message.text,
  });
  await streamCard(thread, result.textStream);
});

The target is any object with Chat SDK's post(card) method that returns a message with edit(card), such as a Thread or a Channel.

OptionDescription
updateIntervalMsMinimum time between message edits while the spec streams. Defaults to 500, the interval Chat SDK uses for streaming with post and edit.
limitsPlatform limits to enforce on the final card. See validation and limits.
maxElementsMaximum number of elements and table rows the spec may expand to. See state and repeat.
componentsComponent definitions. Defaults to the standard components.

If the final spec is invalid, streamCard throws CardSpecError after the partial message is visible, so you can retry or edit the message.

toCard

Convert a complete spec to a CardElement. Post the result with thread.post(card).

import { toCard } from "@json-render/chat";

await thread.post(toCard(spec));

toPartialCard(spec) returns a best-effort card for a spec that is still streaming. It keeps the elements whose props validate and returns null until the root Card exists.

State and repeat

Specs can read from spec.state. toCard resolves $state, $item, $index, and $template expressions, drops elements whose visible condition is false, and renders the children of an element with repeat once per item. A Table with repeat uses its rows as a template, with one copy per item. A Table whose rows resolve to objects matches each header to the key with the same name.

A Fields, Actions, or Section left without children, for example after a repeat over an empty array, is omitted, since platforms reject empty groups. A posted message has no inputs, so $bindState and $bindItem have no effect.

Expansion stops at maxElements, which defaults to 1000 elements and table rows, so nested repeats over state cannot grow without bound. toCard throws CardSpecError when a spec goes past it, and toPartialCard keeps what was expanded so far.

Validation and limits

toCard validates the props of every element against its component schema and checks the structure of the spec, for example that a component without slots has no children and that the root Card has a title, an image, or a child. It throws CardSpecError, whose issues array has one readable entry per problem. Send the issues back to the model to get a corrected spec.

import { CardSpecError, toCard } from "@json-render/chat";

try {
  await thread.post(toCard(spec, { limits: { tables: 1, charts: 2 } }));
} catch (error) {
  if (error instanceof CardSpecError) console.log(error.issues);
}

Platforms cap what a message can hold, and Chat SDK adapters degrade content past those caps. For example, the Slack adapter turns a second table into a code block. Pass limits to reject the spec instead.

LimitDescription
tablesTables per message
chartsCharts per message
fieldsFields per Fields element
tableRowsTable rows, including the header
tableColumnsTable columns
chartTitleCharacters in a chart title
chartLabelCharacters in a chart category, series, or segment label
chartSeriesSeries in a chart, or segments in a pie chart
chartPointsCategories in a bar, line, or area chart