@render-policy/react
v0.1.8
Published
React adapter for @render-policy/core: render agent Markdown/HTML into a ref without dangerouslySetInnerHTML, with a streaming mode; A2UI Text and Image components under @render-policy/react/a2ui.
Maintainers
Readme
@render-policy/react
React adapter for @render-policy/core: render agent Markdown or HTML without
dangerouslySetInnerHTML, with a streaming mode.
import { RenderPolicyProvider, RpMarkdown, RpHtml, useRenderPolicy } from '@render-policy/react';
const config = { mode: 'balanced', policy: { images: { hosts: ['cdn.example.com'] } } };
function App() {
return (
<RenderPolicyProvider config={config}>
<RpMarkdown content={message.content} streaming={message.pending} className="message" />
</RenderPolicyProvider>
);
}RenderPolicyProviderbuilds one renderer for the tree fromconfig, or takes a prebuiltrenderer. Keepconfigreferentially stable (module scope oruseMemo).RpMarkdown/RpHtmlrender into an element (as, defaultdiv) that React owns but never fills. Whilestreamingis true the growing text is rendered at most once per animation frame, with incomplete URLs withheld, unfinished code fences closed and settled blocks kept in place; when it turns false the final text is rendered once.useRenderPolicy(content, { mode, streaming, scheduler, onDecisions })returns a ref for an element of your own. The element must have no React-managed children.onDecisionsreceives the journal of every one-shot render and of the final render of a stream.useRenderer()returns the renderer from the nearest provider, or a default balanced one.- Without a provider the components still work with the balanced defaults.
- On the server the components render an empty container; content appears on the client.
A2UI Text and Image
@render-policy/react/a2ui has the basic catalog's Text and Image for an A2UI v0.9 surface of your
own. They take the values your renderer resolved (after data binding and formatString) and run
them through @render-policy/a2ui, an optional peer dependency you install for this entry.
import { A2uiGuardProvider, RpA2uiImage, RpA2uiText } from '@render-policy/react/a2ui';
const guard = { policy: { images: { hosts: ['cdn.example.com'] } }, onDecision: log };
<A2uiGuardProvider config={guard}>
<RpA2uiText text={resolve(props.text)} variant={props.variant} componentId={id} />
<RpA2uiImage url={resolve(props.url)} description={resolve(props.description)} fit={props.fit} variant={props.variant} fallback={<span>image blocked</span>} />
</A2uiGuardProvider>RpA2uiTextholdsTextto the catalog's contract (Markdown without HTML, images or links) and inserts a sanitized fragment;h1–h5render as headings and, likecaption, without block wrappers. A bound value swapped later is rendered again through the same check.RpA2uiImagechecks the URL before an<img>exists: a blocked URL never reaches the DOM, so the browser makes no request, andfallbackrenders instead. Thesrcis the value the guard returns (for example with the query string stripped).variantandfitcome from the agent: values outside the catalog's enums count as the defaults, so they never become a tag name or a class of the host page.- Classes follow the A2UI renderers' (
a2ui-text h2,a2ui-image avatar), so their styles apply. onDecisionsreceives the journal of each check;useA2uiGuard()returns the guard in use.- On the server
RpA2uiTextrenders an empty element andRpA2uiImageonly itsfallback.
Requires React 18 or later.
