# AI Summary

> A short summary of a long thread or record, with its sources.

Page: https://uicontext.com/docs/components/ai-summary

Group: Agents.

## When to use

A short summary of a long thread or record, with its sources. Use [Markdown](https://uicontext.com/docs/components/markdown.md) instead when the text isn't a summary with sources.

## Usage

```ts
import type { Citation } from "./ui/uicontext/components/Cite.ts"
import { Summary, type SummaryStatus } from "./ui/uicontext/components/Summary.ts"

const Message = UiSchema.TaggedEnum({
	RequestedThreadSummary: {},
	RequestedStopThreadSummary: {},
})

// your app runs the model: pass its status and text as they arrive, then the sources it used
export const ThreadSummary = (
	h: Html.H<typeof Message.Type>,
	run: { readonly status: SummaryStatus; readonly text: string; readonly sources: ReadonlyArray<Citation> },
) =>
	Summary(h, {
		props: { id: "thread-summary", title: "Summary", ...run },
		onEmit: {
			RequestedGenerate: () => Message.RequestedThreadSummary(),
			RequestedRegenerate: () => Message.RequestedThreadSummary(),
			RequestedStop: () => Message.RequestedStopThreadSummary(),
			RequestedOpenSource: () => undefined,
		},
	})
```

## Examples

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

### Generate, stop and regenerate

Words stream in as they're written, with Stop. Numbered markers become citations, and the sources fold under the text.

```ts title="src/demo.ts" source="www/src/examples/ai-summary/demo.ts"
import { Command, UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Effect, Schema } from "effect"
import { Summary, SummaryStatus } from "./ui/uicontext/components/Summary.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { sources, summary } from "./incident.ts"

const styles = stylex.create({
	frame: { inlineSize: "min(100%, 34rem)" },
})

const words = summary.split(/(?<= )/u)

const Message = UiSchema.TaggedEnum({
	RequestedSummary: {},
	StoppedSummary: {},
	ReceivedWords: { run: Schema.Number },
})

// stands in for the model: a first token after 700ms, then a few words every 60ms
const WaitForWords = Command.make("Example.WaitForWords", { args: { run: Schema.Number, first: Schema.Boolean } }, ({ run, first }) =>
	Effect.sleep(first ? "700 millis" : "60 millis").pipe(Effect.as(Message.ReceivedWords({ run }))),
)

export const IncidentSummary = Html.component({
	name: "IncidentSummary",
	model: Schema.Struct({ status: SummaryStatus, shown: Schema.Number, run: Schema.Number, generatedAt: Schema.NullOr(Schema.String) }),
	message: Message,
}).make({
	init: () => ({ status: "idle", shown: 0, run: 0, generatedAt: null }),
	update: {
		// a new run: the old text goes once the first words arrive
		RequestedSummary: ($) =>
			$.evolve({ status: "waiting", shown: 0, run: (n) => n + 1 }).run(WaitForWords.make({ run: $.model.run + 1, first: true })),
		StoppedSummary: ($) => $.evolve({ status: "stopped" }),
		// words from an earlier run, or after Stop, are dropped
		ReceivedWords: ($, { run }) => {
			if (run !== $.model.run || ($.model.status !== "waiting" && $.model.status !== "streaming")) return $.model
			const shown = Math.min(words.length, $.model.shown + 3)
			return shown === words.length
				? $.evolve({ status: "succeeded", shown, generatedAt: "2026-12-01T11:58:00Z" })
				: $.evolve({ status: "streaming", shown }).run(WaitForWords.make({ run, first: false }))
		},
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.frame)).children(
			Summary(h, {
				props: {
					id: "pay-412",
					agentId: "summary-incident",
					title: "Thread summary",
					status: model.status,
					text: words.slice(0, model.shown).join(""),
					sources: model.status === "succeeded" || model.status === "stopped" ? sources : [],
					generatedAt: model.generatedAt ?? undefined,
					now: "2026-12-01T12:00:00Z",
					usage: "Summarizes 38 comments. Uses 1 credit.",
				},
				onEmit: {
					RequestedGenerate: () => Message.RequestedSummary(),
					RequestedRegenerate: () => Message.RequestedSummary(),
					RequestedStop: () => Message.StoppedSummary(),
					RequestedOpenSource: () => undefined,
				},
			}),
		),
})
```

### Out of date, and failed

A summary whose thread moved on says why and offers Regenerate. A failed one says what happened, with Try again.

```ts title="src/out-of-date-and-failed.ts" source="www/src/examples/ai-summary/out-of-date-and-failed.ts"
import { Command, UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Effect, Schema } from "effect"
import { Summary, SummaryStatus } from "./ui/uicontext/components/Summary.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { space } from "./ui/uicontext/components/tokens.stylex.ts"
import { sources, summary } from "./incident.ts"

const styles = stylex.create({
	column: { display: "grid", gap: space.lg, inlineSize: "min(100%, 34rem)" },
})

const Message = UiSchema.TaggedEnum({
	RequestedThreadSummary: {},
	RequestedMeetingSummary: {},
	CompletedThreadRun: {},
	CompletedMeetingRun: { attempt: Schema.Number },
})

// stand-ins for the model: the thread summary takes a second; the meeting summary fails once
const RunThread = Command.make("Example.RunThread", {}, () =>
	Effect.sleep("1 second").pipe(Effect.as(Message.CompletedThreadRun())),
)
const RunMeeting = Command.make("Example.RunMeeting", { args: { attempt: Schema.Number } }, ({ attempt }) =>
	Effect.sleep("1 second").pipe(Effect.as(Message.CompletedMeetingRun({ attempt }))),
)

const meeting =
	"The team agreed to ship the 3-D Secure fix on Thursday behind a flag [1], and to page the payments on-call for any EU decline spike [2]."

export const SummaryRecovery = Html.component({
	name: "SummaryRecovery",
	model: Schema.Struct({ thread: SummaryStatus, stale: Schema.Boolean, meeting: SummaryStatus, attempts: Schema.Number }),
	message: Message,
}).make({
	init: () => ({ thread: "succeeded", stale: true, meeting: "failed", attempts: 1 }),
	update: {
		RequestedThreadSummary: ($) => $.evolve({ thread: "waiting" }).run(RunThread.make()),
		CompletedThreadRun: ($) => $.evolve({ thread: "succeeded", stale: false }),
		RequestedMeetingSummary: ($) =>
			$.evolve({ meeting: "waiting", attempts: (n) => n + 1 }).run(RunMeeting.make({ attempt: $.model.attempts + 1 })),
		CompletedMeetingRun: ($) => $.evolve({ meeting: "succeeded" }),
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.column)).children(
			Summary(h, {
				key: "thread",
				props: {
					id: "pay-412",
					agentId: "summary-stale",
					title: "Thread summary",
					status: model.thread,
					text: summary,
					sources,
					stale: model.stale,
					staleReason: "6 new comments since it was written",
					generatedAt: model.stale ? "2026-12-01T07:10:00Z" : "2026-12-01T12:00:00Z",
					now: "2026-12-01T12:00:00Z",
				},
				onEmit: {
					RequestedGenerate: () => undefined,
					RequestedRegenerate: () => Message.RequestedThreadSummary(),
					RequestedStop: () => undefined,
					RequestedOpenSource: () => undefined,
				},
			}),
			Summary(h, {
				key: "meeting",
				props: {
					id: "standup-1201",
					agentId: "summary-failed",
					title: "Meeting summary",
					status: model.meeting,
					text: model.meeting === "succeeded" ? meeting : "",
					sources:
						model.meeting === "succeeded"
							? [
									{ id: "m1", label: "1", title: "Transcript, 09:14", source: "Payments standup", kind: "file", href: "#t-0914" },
									{ id: "m2", label: "2", title: "Transcript, 09:21", source: "Payments standup", kind: "file", href: "#t-0921" },
								]
							: [],
					notice: "The transcript service didn't answer. Try again in a moment.",
					// no Stop: the transcript service runs to the end
					can: ["regenerate"],
				},
				onEmit: {
					RequestedGenerate: () => undefined,
					RequestedRegenerate: () => Message.RequestedMeetingSummary(),
					RequestedStop: () => undefined,
					RequestedOpenSource: () => undefined,
				},
			}),
		),
})
```

### Refused, empty and read-only

Out of credits, nothing to say, and a viewer who can't start a run: each says why in a sentence.

```ts title="src/unavailable.ts" source="www/src/examples/ai-summary/unavailable.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Summary } from "./ui/uicontext/components/Summary.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	column: { display: "grid", gap: space.lg, inlineSize: "min(100%, 34rem)" },
})

const silent = {
	RequestedGenerate: () => undefined,
	RequestedRegenerate: () => undefined,
	RequestedStop: () => undefined,
	RequestedOpenSource: () => undefined,
}

export const SummaryUnavailable = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.column)).children(
		Summary(h, {
			key: "refused",
			props: {
				id: "ops-weekly",
				agentId: "summary-refused",
				title: "Weekly summary",
				status: "refused",
				text: "",
				sources: [],
				notice: "Your workspace used its AI credits for December. An admin can add more in Billing.",
			},
			onEmit: silent,
		}),
		Summary(h, {
			key: "empty",
			props: {
				id: "ask-pages",
				agentId: "summary-empty",
				title: "Answer",
				status: "empty",
				text: "",
				sources: [],
				notice: "No answer in the pages you can see. Try other words, or ask someone with access.",
			},
			onEmit: silent,
		}),
		Summary(h, {
			key: "viewer",
			props: {
				id: "handbook",
				agentId: "summary-viewer",
				title: "Page summary",
				status: "idle",
				text: "",
				sources: [],
				// a viewer can read summaries others made, but not start one
				can: [],
				usage: "No summary yet. Editors can generate one.",
			},
			onEmit: silent,
		}),
	)
```

### Sample data

The examples above import these files.

```ts title="src/incident.ts" source="www/src/examples/ai-summary/incident.ts"
import type { Citation } from "./ui/uicontext/components/Cite.ts"

// An incident thread: EU card payments fail after Tuesday's deploy, and a fix is in review.

export const sources: ReadonlyArray<Citation> = [
	{
		id: "s1",
		label: "1",
		title: "Mira's comment",
		source: "PAY-412",
		kind: "comment",
		href: "#PAY-412-c3",
		snippet: "Every failure is a card issued in the EU, at the 3-D Secure step.",
	},
	{
		id: "s2",
		label: "2",
		title: "Deploy log, 14:02",
		source: "#deploys",
		kind: "message",
		href: "#deploys-1402",
		snippet: "checkout-web 2026.11.24 rolled out to 100%.",
	},
	{
		id: "s3",
		label: "3",
		title: "Ben's pull request",
		source: "checkout-web#881",
		kind: "page",
		href: "#pr-881",
		snippet: "Keep the session cookie across the 3-D Secure redirect.",
	},
]

export const summary =
	"Card payments fail for customers in the EU since Tuesday's deploy [1][2]. The new 3-D Secure redirect drops the session cookie, so the bank's answer never reaches checkout. Ben has a fix in review and wants a second reviewer before it ships [3]."
```

## API reference

**Props**

| Name | Type | Description |
| --- | --- | --- |
| `agentId` | `string` | The id agents see this by; `summary:<id>` when left out |
| `id` (required) | `string` | Identifies the summary in the events it sends |
| `title` (required) | `string` | The card's heading: "Summary" |
| `status` (required) | `SummaryStatus` | Where it stands: idle, waiting, streaming, succeeded, stopped, empty, failed or refused |
| `text` (required) | `string` | Markdown; `[n]` names the source whose label is n |
| `sources` (required) | `Citation[]` | What it was written from, numbered; `[n]` in the text cites one |
| `stale` | `boolean` | Its inputs changed since it was written |
| `staleReason` | `string` | Why it's out of date, shown with Regenerate: "5 new comments since" |
| `generatedAt` | `string` | When it was written, as an ISO timestamp; shown with `locale` and `timeZone` |
| `now` | `string` | Now, as an ISO timestamp, to say "Generated 2 hours ago"; a date and time when absent |
| `locale` | `string` | Formats `generatedAt`; the reader's own when left out |
| `timeZone` | `string` | The time zone for `generatedAt`; the reader's own when left out |
| `attribution` | `AttributionData` | Who asked for it, or what wrote it |
| `notice` | `string` | For failed and refused: what went wrong and what to do, as a sentence |
| `can` | `Can(["regenerate", "stop"])` | What the viewer may do: "regenerate" shows Generate and Regenerate (not for a viewer, or a plan without it); "stop" shows Stop (not when the run can't be cancelled). Both when left out. |
| `usage` | `string` | What a run costs, said before it runs: "Uses 1 credit" |
| `openSourcesInApp` | `boolean` | The app opens sources itself (`RequestedOpenSource`) instead of following their links |
| `waitingLabel` | `string` | What shows while nothing has arrived: "Searching…" (default "Summarizing…") |
| `collapseAfter` | `number` | Collapse text longer than this many characters behind "Show more" |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedGenerate` | `{ id: string }` | The person asked for a first summary |
| `RequestedRegenerate` | `{ id: string }` | A new run; the old text stays until the new one arrives |
| `RequestedStop` | `{ id: string }` | The person stopped the run |
| `RequestedOpenSource` | `{ id: string; source: string }` | The person opened a source, when `openSourcesInApp` is set |

**Agent actions**

| Action | Description |
| --- | --- |
| `ClickedGenerate` | Generate the summary |
| `ClickedRegenerate` | Write the summary again from its current inputs (a new run) |
| `ClickedStop` | Stop the summary being written |

## Keyboard and accessibility

- The summary is a region named by its title, marked busy while it's written; why it's out of date is said in words.
- Show more says whether it's expanded, and its sources are a disclosure of numbered citations.
- Agents can generate, regenerate or stop it.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/ai-summary
```

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

It writes these files:

- `src/ui/uicontext/components/Attribution.ts`
- `src/ui/uicontext/components/Button.ts`
- `src/ui/uicontext/components/Cite.ts`
- `src/ui/uicontext/components/CopyButton.ts`
- `src/ui/uicontext/components/Glyph.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/Prose.ts`
- `src/ui/uicontext/components/Stream.ts`
- `src/ui/uicontext/components/Summary.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/cite.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/Summary.ts](https://github.com/checkfu/uicontext/blob/main/src/Summary.ts)

## Related

- [Agent Tools](https://uicontext.com/docs/components/agent-tools.md) (`agent-tools`): Lets agents in the browser read the page and act through the same controls people use.
- [Tool Confirmation](https://uicontext.com/docs/components/tool-confirmation.md) (`tool-confirmation`): Asks a person to confirm before an agent's action changes anything.
- [Prompt Template](https://uicontext.com/docs/components/prompt-template.md) (`prompt-template`): Write a prompt with {{variables}} the app fills in: they stand out, unknown ones are flagged, and a preview shows it with samples.
- [Agent Settings](https://uicontext.com/docs/components/agent-settings.md) (`agent-settings`): Edit an agent's name, instructions, tools and model.
- [Computer](https://uicontext.com/docs/components/computer.md) (`computer`): The machine an agent works on: a live view of its screen, full size or as a thumbnail with Take over, and its browser with tabs and an address bar.
- [Persona Avatar](https://uicontext.com/docs/components/persona-avatar.md) (`persona-avatar`): An agent's face: a shape and a color with eyes that move with what it's doing.
