0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00
mastra/integrations/json-render
dane-ai-mastra[bot] a5859ee2b9
chore: version packages (alpha) (#30430)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
History 2026-11-16 14:09:31 +01:00
..
src feat(json-render): add @mastra/json-render so agents can compose json-render interfaces (#29914) 2026-11-16 09:04:48 +01:00
CHANGELOG.md chore: version packages (alpha) (#30430) 2026-11-16 14:09:31 +01:00
lint-staged.config.js feat(json-render): add @mastra/json-render so agents can compose json-render interfaces (#29914) 2026-11-16 09:04:48 +01:00
package.json chore: version packages (alpha) (#30430) 2026-11-16 14:09:31 +01:00
README.md feat(json-render): add @mastra/json-render so agents can compose json-render interfaces (#29914) 2026-11-16 09:04:48 +01:00
tsconfig.build.json feat(json-render): add @mastra/json-render so agents can compose json-render interfaces (#29914) 2026-11-16 09:04:48 +01:00
tsconfig.json feat(json-render): add @mastra/json-render so agents can compose json-render interfaces (#29914) 2026-11-16 09:04:48 +01:00
tsdown.config.ts feat(json-render): add @mastra/json-render so agents can compose json-render interfaces (#29914) 2026-11-16 09:04:48 +01:00
turbo.json feat(json-render): add @mastra/json-render so agents can compose json-render interfaces (#29914) 2026-11-16 09:04:48 +01:00
vitest.config.ts feat(json-render): add @mastra/json-render so agents can compose json-render interfaces (#29914) 2026-11-16 09:04:48 +01:00

@mastra/json-render

Let Mastra agents compose json-render interfaces from your component catalog. The interfaces stream to the client, are saved with the message, and can be edited in later turns.

Installation

npm install @mastra/json-render

It needs @json-render/core on the server and @json-render/react to render on the client:

npm install @json-render/core @json-render/react

Usage

Register the processor as both an input and an output processor, and build the instructions from your catalog:

import { Agent } from '@mastra/core/agent';
import { JsonRenderProcessor } from '@mastra/json-render';
import { Memory } from '@mastra/memory';
import { catalog } from './catalog';

const ui = new JsonRenderProcessor({ catalog });

export const uiAgent = new Agent({
  id: 'ui-agent',
  name: 'UI Agent',
  model: 'openai/gpt-6.1-sol',
  instructions: catalog.prompt({
    mode: 'inline',
    editModes: ['patch'],
    system: 'You are a travel assistant. Prefer showing an interface over describing it.',
  }),
  inputProcessors: [ui],
  outputProcessors: [ui],
  maxProcessorRetries: 2,
  memory: new Memory(),
});

On the client, render each message's data-spec parts with useJsonRenderMessage() from @json-render/react. Send the interface's state under the JSON_RENDER_STATE request context key so the model sees what the user set.

json-render 0.22's buildSpecFromParts() writes patch values into the spec by reference, so building the same parts again (on every streamed chunk) repeats list items. Pass useJsonRenderMessage() a copy of the parts:

import { useJsonRenderMessage } from '@json-render/react';
import { useMemo } from 'react';

function Reply({ message }) {
  const parts = useMemo(() => structuredClone(message.content.parts), [message.content.parts]);
  const { spec } = useJsonRenderMessage(parts);
  // Render `spec` with your registry.
}

Documentation

Changelog

See the package changelog for version history and release notes.

Support

We have an open community Discord. Come and say hello and let us know if you have any questions or need any help getting things running.