# 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.

Page: https://uicontext.com/docs/components/prompt-template

Group: Agents.

## When to use

Write a prompt with {{variables}} the app fills in: they stand out, unknown ones are flagged, and a preview shows it with samples. Use [Composer](https://uicontext.com/docs/components/composer.md) instead when the text is a message to send now, with no {{variables}} for the app to fill in.

## Usage

```ts
import { PromptTemplate } from "./ui/uicontext/components/PromptTemplate.ts"

export const Instructions = <M>(h: Html.H<M>, prompt: string, onChange: (text: string) => M) =>
	PromptTemplate(h, {
		props: {
			label: "Instructions",
			value: prompt,
			variables: [{ name: "customer.name", description: "The customer's first name", sample: "Ada" }],
		},
		onEmit: { RequestedChange: ({ value }) => onChange(value) },
	})
```

## Examples

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

### A support reply drafter

Fix {{agent.nmae}} and the warning clears. The Insert buttons put a variable where the cursor is.

```ts title="src/demo.ts" source="www/src/examples/prompt-template/demo.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { PromptTemplate, type TemplateVariable } from "./ui/uicontext/components/PromptTemplate.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"

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

const variables: ReadonlyArray<TemplateVariable> = [
	{ name: "customer.name", description: "The customer's first name", sample: "Ada" },
	{ name: "customer.plan", description: "Their plan: Free, Team or Business", sample: "Team" },
	{ name: "ticket.subject", description: "The ticket's subject line", sample: "Can't export invoices to CSV" },
	{ name: "ticket.body", description: "What they wrote, as plain text", sample: "Every export since Monday comes back empty. We close the month on Friday." },
	{ name: "agent.name", description: "The person the reply is signed by", sample: "Tomás" },
	{ name: "today", description: "Today's date in the customer's language" },
]

// a support team's reply drafter: one variable is misspelled, so it's flagged before it ships
const value = `You draft replies for the support team. Be brief and warm, and never promise dates.

The customer is {{customer.name}}, on the {{customer.plan}} plan.
Subject: {{ticket.subject}}
They wrote:
{{ticket.body}}

Answer what they asked, in their language. If it's a bug, say we've passed it to the team.
Sign off as {{agent.nmae}}.`

export const ReplyInstructions = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.wrap)).children(
		PromptTemplate(h, { key: "reply", props: { label: "Reply instructions", value, variables, rows: 8 } }),
	)
} and the warning clears. The Insert buttons put a variable where the cursor is." }
```

### Variables the writer makes up

Without `variables`, any name goes: each is listed so it can be tried with a sample.

```ts title="src/free.ts" source="www/src/examples/prompt-template/free.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { PromptTemplate } from "./ui/uicontext/components/PromptTemplate.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"

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

// a team's own saved prompt: no list from the app, so every name it uses is listed with a sample field
const value = "Summarize {{doc}} for {{audience}} in {{length}} bullet points. Keep the numbers exact and say what's still undecided."

export const SavedPrompt = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.wrap)).children(
		PromptTemplate(h, { key: "saved", props: { id: "saved", label: "Saved prompt", value, rows: 3, placeholder: "Write a prompt. Put {{name}} where a value goes." } }),
	)
```

## API reference

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `label` (required) | `string` | — | The editor's name: "Instructions". |
| `value` (required) | `string` | — | The template to start with. |
| `variables` | `TemplateVariable[]` | — | The variables the app fills in. Without it, any name goes and nothing is flagged. |
| `rows` | `number` | `6` | The editor's height in lines before it grows; 6 by default. |
| `id` | `string` | `"prompt"` | Starts the ids inside it, unique on the page; "prompt" by default. |
| `placeholder` | `string` | — | — |
| `locale` | `string` | — | For the counts; the browser's by default. |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedChange` | `{ value: string; unknown: string[] }` | The text changed: the template, and the variables in it the app doesn't offer. |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/prompt-template
```

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

It writes these files:

- `src/ui/uicontext/components/FormControls.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/PromptTemplate.ts`
- `src/ui/uicontext/components/ShowText.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

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

## Source

- [src/PromptTemplate.ts](https://github.com/checkfu/uicontext/blob/main/src/PromptTemplate.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.
- [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.
- [Agent List](https://uicontext.com/docs/components/agent-list.md) (`agent-list`): Agents listed like conversations, each with its face, last message time and status.
