# Suggestions

> Proposed edits you can review, accept or reject.

Page: https://uicontext.com/docs/components/suggestions

Group: Chat.

## When to use

Proposed edits you can review, accept or reject. Use [Diff](https://uicontext.com/docs/components/diff.md) instead when people only read the change.

## Usage

```ts
import { Suggest, type SuggestStatus } from "./ui/uicontext/components/Suggest.ts"

const Message = UiSchema.TaggedEnum({
	RequestedAssignee: { value: Schema.String },
	DismissedAssignee: {},
})

export const AssigneeSuggestion = (h: Html.H<typeof Message.Type>, status: SuggestStatus) =>
	Suggest(h, {
		props: {
			id: "pay-415-assignee",
			field: "Assignee",
			value: { value: "mira", label: "Mira Patel" },
			status,
			reason: "Mira changed the invoice template on Tuesday.",
		},
		onEmit: {
			// set the assignee, then pass `status: "accepted"`
			RequestedAccept: ({ value }) => Message.RequestedAssignee({ value }),
			RequestedDismiss: () => Message.DismissedAssignee(),
			RequestedUndo: () => undefined,
			RequestedOpenWhy: () => undefined,
			RequestedAction: () => undefined,
		},
	})
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### Beside an issue's properties

The example the page opens with: Suggestions in “Beside an issue's properties”.

```ts title="src/demo.ts" source="www/src/examples/suggestions/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { Suggest, SuggestStatus } from "./ui/uicontext/components/Suggest.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	// an issue's properties, label on the left and value on the right, as in a side panel
	properties: {
		display: "grid",
		gridTemplateColumns: "6.5rem minmax(0, 1fr)",
		alignItems: "center",
		rowGap: space.md,
		columnGap: space.lg,
		inlineSize: "min(100%, 26rem)",
		margin: 0,
	},
	label: { fontSize: font.sm, color: color.muted },
	value: { margin: 0, minWidth: 0 },
})

const Message = UiSchema.TaggedEnum({
	RequestedAssignee: { value: Schema.String },
	DismissedAssignee: {},
	UndidProject: {},
})

const silent = { RequestedUndo: () => undefined, RequestedOpenWhy: () => undefined, RequestedAction: () => undefined }

export const IssueSuggestions = Html.component({
	name: "IssueSuggestions",
	model: Schema.Struct({ assignee: SuggestStatus, picked: Schema.String, project: SuggestStatus }),
	message: Message,
}).make({
	init: () => ({ assignee: "pending", picked: "mira", project: "auto-applied" }),
	update: {
		// accepting is a request: here the app takes it at once
		RequestedAssignee: ($, { value }) => $.evolve({ assignee: "accepted", picked: value }),
		DismissedAssignee: ($) => $.evolve({ assignee: "dismissed" }),
		UndidProject: ($) => $.evolve({ project: "undone" }),
	},
	view: (h, { assignee, picked, project }) =>
		h.dl.attrs(sx(styles.properties)).children(
			h.dt.attrs(sx(styles.label)).children("Assignee"),
			h.dd.attrs(sx(styles.value)).children(Suggest(h, {
				key: "assignee",
				props: {
					agentId: "suggest-demo:assignee",
					id: "pay-415-assignee",
					field: "Assignee",
					value: picked === "omar"
						? { value: "omar", label: "Omar Haddad", glyph: { _tag: "Person", id: "omar", name: "Omar Haddad", agent: false } }
						: { value: "mira", label: "Mira Patel", glyph: { _tag: "Person", id: "mira", name: "Mira Patel", agent: false } },
					alternatives: [{ value: "omar", label: "Omar Haddad" }],
					status: assignee,
					reason: "Mira changed the invoice template on Tuesday, and VAT shows twice since then.",
					source: "Triage assistant",
				},
				onEmit: { ...silent, RequestedAccept: ({ value }) => Message.RequestedAssignee({ value }), RequestedDismiss: () => Message.DismissedAssignee() },
			})),
			h.dt.attrs(sx(styles.label)).children("Project"),
			h.dd.attrs(sx(styles.value)).children(Suggest(h, {
				key: "project",
				props: {
					agentId: "suggest-demo:project",
					id: "pay-415-project",
					field: "Project",
					value: { value: "eu-invoices", label: "EU invoicing" },
					status: project,
					reason: "Four of the last five VAT bugs were filed under EU invoicing.",
					source: "Triage assistant",
				},
				onEmit: { ...silent, RequestedAccept: () => undefined, RequestedDismiss: () => undefined, RequestedUndo: () => Message.UndidProject() },
			})),
			h.dt.attrs(sx(styles.label)).children("Labels"),
			h.dd.attrs(sx(styles.value)).children(Suggest(h, {
				key: "labels",
				props: { agentId: "suggest-demo:labels", id: "pay-415-labels", field: "Labels", value: { value: "", label: "" }, status: "processing" },
				onEmit: { ...silent, RequestedAccept: () => undefined, RequestedDismiss: () => undefined },
			})),
		),
})
```

### An offer, with the change

As a card, a suggestion holds a bigger proposal and shows the change in its `detail` slot.

```ts title="src/offer.ts" source="www/src/examples/suggestions/offer.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { Suggest, SuggestStatus } from "./ui/uicontext/components/Suggest.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	frame: { inlineSize: "min(100%, 30rem)" },
	diff: {
		margin: 0,
		padding: space.md,
		borderRadius: corner.control,
		backgroundColor: color.raised,
		fontFamily: font.mono,
		fontSize: font.xs,
		lineHeight: 1.6,
		overflowX: "auto",
		whiteSpace: "pre",
	},
	removed: { color: color.danger },
	added: { color: color.success },
})

const Message = UiSchema.TaggedEnum({ AnsweredOffer: { status: SuggestStatus } })

export const ScheduleOffer = Html.component({
	name: "ScheduleOffer",
	model: Schema.Struct({ status: SuggestStatus }),
	message: Message,
}).make({
	init: () => ({ status: "pending" }),
	update: { AnsweredOffer: ($, { status }) => $.evolve({ status }) },
	view: (h, { status }) =>
		h.div.attrs(sx(styles.frame)).children(Suggest(h, {
			props: {
				agentId: "suggest-offer",
				id: "standup-move",
				layout: "card",
				title: "Move Thursday's standup so Lena can join?",
				field: "Calendar",
				value: { value: "move", label: "Move it" },
				status,
				reason: "Lena has a customer call 9:30–10:15 every Thursday this month.",
				source: "Juno",
				acceptLabel: "Move it",
				dismissLabel: "Keep 9:45",
				actions: [{ id: "mute", label: "Don't suggest calendar changes" }],
			},
			onEmit: {
				RequestedAccept: () => Message.AnsweredOffer({ status: "accepted" }),
				RequestedDismiss: () => Message.AnsweredOffer({ status: "dismissed" }),
				RequestedUndo: () => undefined,
				RequestedOpenWhy: () => undefined,
				RequestedAction: () => undefined,
			},
			// the change itself, for the person to check before accepting
			slots: {
				detail: h.pre.attrs(sx(styles.diff)).children(
					h.span.attrs(sx(styles.removed)).children("− Thu 9:45–10:00  Standup\n"),
					h.span.attrs(sx(styles.added)).children("+ Thu 10:30–10:45 Standup"),
				),
			},
		})),
})
```

## API reference

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `agentId` | `string` | — | This suggestion's id for agents, unique on the page; `suggestion:<id>` when left out |
| `id` (required) | `string` | — | Identifies the suggestion in the events it sends |
| `field` (required) | `string` | — | The property it would set: "Assignee" |
| `value` (required) | `SuggestValue` | — | The value it would set, with its label and glyph |
| `alternatives` | `SuggestValue[]` | — | Other values the model considered, offered after the main one |
| `status` (required) | `SuggestStatus` | — | Where it stands: processing, pending, accepted, auto-applied, dismissed, undone, stale or unavailable |
| `reason` | `string` | — | The model's reason; leave out until loaded (the chip reports `RequestedOpenWhy`) |
| `reasonLoading` | `boolean` | — | The reason is being fetched: Why shows a placeholder |
| `source` | `string` | — | What made it: "Triage Intelligence" |
| `can` | `Can(["accept"])` | — | "accept" shows Accept; leave it out of the list for a viewer, or the agent that made it. Allowed when left out. |
| `acceptLabel` | `string` | `"Accept"` | The accept button's label; "Accept" by default |
| `dismissLabel` | `string` | `"Dismiss"` | The dismiss button's label; "Dismiss" by default |
| `reapply` | `string` | — | An undone suggestion may be applied again: the button's label ("Apply again"); it asks with `RequestedAccept` |
| `title` | `string` | — | A card's heading: "Want me to look into this?" |
| `layout` | `"chip" \| "card"` | `"chip"` | A compact chip beside the property, or a card with a title and actions; "chip" by default |
| `actions` | `{ id, label }[]` | — | More choices on a card: "Don't suggest in this channel" |
| `notice` | `string` | — | Why the last request was refused, as a sentence |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedAccept` | `{ id: string; value: string }` | The person accepted the suggested value or an alternative |
| `RequestedDismiss` | `{ id: string }` | The person dismissed the suggestion |
| `RequestedUndo` | `{ id: string }` | Undo an auto-applied value: the app clears or restores the property |
| `RequestedOpenWhy` | `{ id: string }` | The reason was asked for; load it if `reason` is missing |
| `RequestedAction` | `{ id: string; action: string }` | The person picked one of a card's `actions` |

**Slots**: `detail`

## Keyboard and accessibility

- Accept and Dismiss are buttons, and Why opens a popover with the reason that closes with Escape.
- Accepting, undoing and a refusal are announced as they happen.
- Agents see the field, the value and why it's suggested; accepting or dismissing must name the suggestion they read.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/suggestions
```

With npm: `npx shadcn@latest add checkfu/uicontext/suggestions`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/suggestions`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/suggestions`.

It writes these files:

- `src/ui/uicontext/components/Attribution.ts`
- `src/ui/uicontext/components/Button.ts`
- `src/ui/uicontext/components/Glyph.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/Popover.ts`
- `src/ui/uicontext/components/Suggest.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/agent/Agent.ts`
- `src/ui/uicontext/components/button.stylex.ts`
- `src/ui/uicontext/components/domain/Actor.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/domain/Glyph.ts`
- `src/ui/uicontext/components/domain/Permission.ts`
- `src/ui/uicontext/components/domain/Time.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/variants.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/Suggest.ts](https://github.com/checkfu/uicontext/blob/main/src/Suggest.ts)

## Used in

- [Helpdesk](https://uicontext.com/blocks/helpdesk.md)
- [Assistant panel](https://uicontext.com/blocks/assistant-panel.md)

## Related

- [Conversation](https://uicontext.com/docs/components/conversation.md) (`conversation`): What a person and an assistant say to each other, with actions, sources and tool calls on each message.
- [Composer](https://uicontext.com/docs/components/composer.md) (`composer`): Write and send a message, with attachments and modes.
- [Conversation Viewport](https://uicontext.com/docs/components/conversation-viewport.md) (`conversation-viewport`): Keeps the newest message in view, and lets you scroll back without losing your place.
- [Streaming Text](https://uicontext.com/docs/components/streaming-text.md) (`streaming-text`): A reply that appears as it's written, with a stop button.
- [Agent Plan](https://uicontext.com/docs/components/agent-plan.md) (`agent-plan`): What an agent did and plans to do, step by step, with each step's input and result.
- [Tool Call](https://uicontext.com/docs/components/tool-call.md) (`tool-call`): What an assistant's tools did, one past-tense row each, such as "Searched the web · 8 sources". Each opens to what it read.
