# New Agent

> Make an agent: pick its face, name it, or start from a suggested job.

Page: https://uicontext.com/docs/components/new-agent

Group: Agents.

## When to use

Make an agent: pick its face, name it, or start from a suggested job. Use [Agent Settings](https://uicontext.com/docs/components/agent-settings.md) instead when the agent already exists.

## Usage

```ts
import { type Outcome } from "./ui/uicontext/components/domain/Outcome.ts"
import { AgentCreate } from "./ui/uicontext/components/AgentCreate.ts"
import { Persona } from "./ui/uicontext/components/Persona.ts"

const Message = UiSchema.TaggedEnum({ SubmittedAgent: { name: Schema.String, persona: Persona, job: Schema.optional(Schema.String) } })

// the pane keeps its draft; you make the agent, then pass back how it went as `outcome`
export const NewAgent = (h: Html.H<typeof Message.Type>, outcome: Outcome | null) =>
	AgentCreate(h, {
		props: {
			suggestions: [{ id: "night-shift", name: "Night Shift", job: "Works overnight and preps your morning digest.", persona: { shape: "hexagon", color: "orange" } }],
			outcome,
		},
		onEmit: { RequestedSubmit: ({ name, persona, job }) => Message.SubmittedAgent({ name, persona, ...(job === undefined ? {} : { job }) }) },
	})
```

## Examples

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

### Make an agent

Pick a color and a shape and the face changes with you; a suggestion fills in the face, the name and its first job. Try the name Inbox, which is taken.

```ts title="src/demo.ts" source="www/src/examples/new-agent/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 { accepted, Outcome, pending, refused } from "./ui/uicontext/components/domain/Outcome.ts"
import { AgentCreate } from "./ui/uicontext/components/AgentCreate.ts"
import { Persona } from "./ui/uicontext/components/Persona.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font } from "./ui/uicontext/components/tokens.stylex.ts"

const Message = UiSchema.TaggedEnum({
	SubmittedAgent: { name: Schema.String, persona: Persona, job: Schema.optional(Schema.String) },
	CompletedCreate: { name: Schema.String, taken: Schema.Boolean },
})

// Stands in for the server making the agent: a moment, then yes, or no when the name is taken.
const Create = Command.make("CreateSampleAgent", { args: { name: Schema.String } }, ({ name }) =>
	Effect.sleep("600 millis").pipe(Effect.as(Message.CompletedCreate({ name, taken: name.toLowerCase() === "inbox" }))))

const styles = stylex.create({ made: { margin: 0, color: color.muted, fontSize: font.sm, textAlign: "center" } })

export const MakeAnAgent = Html.component({
	name: "MakeAnAgent",
	model: Schema.Struct({ outcome: Schema.NullOr(Outcome), made: Schema.NullOr(Schema.String) }),
	message: Message,
}).make({
	init: () => ({ outcome: null, made: null }),
	update: {
		SubmittedAgent: ($, { name }) => $.evolve({ outcome: pending("create") }).run(Create.make({ name })),
		CompletedCreate: ($, { name, taken }) => taken
			? $.evolve({ outcome: refused("create", `You already have an agent called ${name}.`) })
			: $.evolve({ outcome: accepted("create"), made: name }),
	},
	view: (h, model) => h.div.children(
		model.made !== null && h.p.attrs({ role: "status", ...sx(styles.made) }).children(`${model.made} is ready. It opens in its conversation.`),
		AgentCreate(h, {
			props: {
				agentId: "agent-create-demo",
				outcome: model.outcome,
				suggestions: [
					{ id: "night-shift", name: "Night Shift", job: "Works overnight and preps your morning digest.", persona: { shape: "hexagon", color: "orange" } },
					{ id: "inbox-triage", name: "Inbox Triage", job: "Sorts your email and drafts replies in your voice.", persona: { shape: "cloud", color: "pink" } },
				],
			},
			onEmit: { RequestedSubmit: ({ name, persona, job }) => Message.SubmittedAgent({ name, persona, ...(job === undefined ? {} : { job }) }) },
		}),
	),
})
```

## API reference

**Props**

| Name | Type | Description |
| --- | --- | --- |
| `suggestions` (required) | `AgentSuggestion[]` | — |
| `persona` | `Persona` | The face it starts with |
| `outcome` | `Outcome \| null` | — |
| `agentId` | `string` | — |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedSubmit` | `{ name: string; persona: Persona; job?: string }` | — |

**Agent actions**

| Action | Description |
| --- | --- |
| `RequestedCreate` (asks first) | Create an agent with a name, optionally a color and shape, and its job as its first message |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/new-agent
```

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

It writes these files:

- `src/ui/uicontext/components/AgentCreate.ts`
- `src/ui/uicontext/components/Button.ts`
- `src/ui/uicontext/components/FormControls.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/InlineNotice.ts`
- `src/ui/uicontext/components/Persona.ts`
- `src/ui/uicontext/components/ShowText.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/Outcome.ts`
- `src/ui/uicontext/components/persona.stylex.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/touch.stylex.ts`
- `src/ui/uicontext/components/variants.ts`

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

## Source

- [src/AgentCreate.ts](https://github.com/checkfu/uicontext/blob/main/src/AgentCreate.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.
