Skip to documentation content

Guardrail Notice

Show what a guardrail masked, blocked, or flagged in a conversation, the rule that fired, and why.

guardrail-notice-demo

Overview

When a guardrail changes a conversation, the person should be able to see that it did, and why. Guardrail Notice has two parts for that. RedactedText shows masked spans inside a message as chips; each chip opens what was found, the rule that fired, and the reason. GuardrailNotice is a line under the message that sums it up, such as "1 request blocked · 3 items masked", and opens every event with its severity.

Take a billing assistant whose reply contained a customer's email, a phone number, card digits, and a live API key. The email and phone are masked but can be revealed by staff. The card digits are masked for everyone. The key was blocked from the reply. The notice says all of that in one line.

The originals never need to reach the browser. The text you pass holds placeholders where the masked spans are. When a person with access presses Reveal, onReveal asks your server for the original, so your server decides who may see it and records that they did.

Guardrail Notice detects nothing. It shows the events your guardrail reported.

Anatomy

RedactedText
  • TextThe message, with each masked span replaced by a chip
  • ChipA hatched label, such as email, that opens the event's details
  • DetailsThe category, the action, the rule, the reason, and Reveal when allowed
GuardrailNotice
  • SummaryA count for each action, most serious first
  • EventsEach event with its category, severity, action, rule, reason, and source

Installing guardrail-notice brings Button and Popover along as registry dependencies.

Behavior

Actions

An event has one of four actions. masked hid part of the text, blocked stopped a request or reply, flagged let it through and marked it for review, and allowed let it through with a warning. A block is shown in the danger color and a flag or warning in the attention color; a mask stays neutral. Each action also has its own icon and word, so none depends on color.

Masked spans

ranges places each chip by start and end in the text you pass. toGuardrailSegments turns the text and ranges into plain runs and masked spans; ranges are sorted and clamped, and a range that overlaps an earlier one is dropped. The chip shows the event's placeholder, then its categoryLabel, then its category.

Reveal

Reveal appears only when the event is revealable and you handle onReveal. While the promise is pending the button reads "Revealing…". When it resolves, the chip shows the original with a dashed outline and the details say the reveal is recorded. Hide puts the chip back. When it rejects, the details say the original could not be shown.

The notice

The notice starts open when anything was blocked, so a block's reason is never hidden behind a click. Otherwise it starts closed. Pass open and onOpenChange to control it. With no events it renders nothing.

Accessibility

Each chip is a button. Its name starts with what was masked, such as "Masked: Email address", so a screen reader never reads the placeholder characters. Its details open in a popover named by the category, which takes focus and closes on Escape.

The notice's summary is a button with aria-expanded that controls the list. Severity is three bars and a word. When an original is revealed, a polite live region says the reveal is recorded.

Every string comes from labels.

KeyAction
Enter or Space on a chipOpen its details
Enter or Space on the summaryShow or hide the events
EscapeClose a chip's details

Installation

npx honestui@latest add guardrail-notice

Usage

import { GuardrailNotice, RedactedText } from "@/components/ai/guardrail-notice"

<p>
  <RedactedText
    text={message.text}
    ranges={message.redactions}
    events={message.guardrailEvents}
    onReveal={(event) => revealOriginal(message.id, event.id)}
  />
</p>
<GuardrailNotice events={message.guardrailEvents} />

Examples

A blocked request

The notice opens on its own when a request was blocked.

guardrail-notice-blocked

API reference

RedactedText

Accepts native span props beyond those listed.

PropTypeDefaultDescription
textstringRequiredThe text as the person may see it, with placeholders where spans are masked.
rangesGuardrailRange[]RequiredWhere each masked span is, and which event it belongs to.
eventsGuardrailEvent[]RequiredThe events the ranges point to.
onReveal(event, range) => string | Promise<string>NoneFetches the original of a revealable span.
labelsPartial<GuardrailNoticeLabels>EnglishEvery string.

GuardrailNotice

Accepts native div props beyond those listed.

PropTypeDefaultDescription
eventsGuardrailEvent[]RequiredWhat the guardrails did.
defaultOpenbooleanOpen when anything was blockedWhether the events start shown.
openbooleanNoneControls whether the events are shown.
onOpenChange(open: boolean) => voidNoneCalled when the summary is pressed.
labelsPartial<GuardrailNoticeLabels>EnglishEvery string.

Helpers

ExportDescription
toGuardrailSegments(text, ranges)Splits text into plain runs and masked spans.
summarizeGuardrailEvents(events)Counts the events for each action.

Types

ExportShape
GuardrailEventid, action, category, rule, and optional categoryLabel, reason, severity, source, placeholder, revealable
GuardrailAction"masked", "blocked", "flagged", or "allowed"
GuardrailSeverity"low", "medium", or "high"
GuardrailRangestart, end, and eventId
GuardrailSegmentA "text" run or a "redaction" span