# AI generation

> Describe an extension on code.extension.dev, watch it build and run in the page, then take the source, a share link, or a real project.

Canonical: https://docs.extension.dev/code
This page as Markdown: https://docs.extension.dev/code.md
Section: Code
Siblings: [Terminal](https://docs.extension.dev/code/terminal.md) · [Chat mode](https://docs.extension.dev/code/chat-mode.md) · [Starters and templates](https://docs.extension.dev/code/starters.md) · [Live preview](https://docs.extension.dev/code/preview.md) · [Select to edit](https://docs.extension.dev/code/select-to-edit.md) · [The Code tab](https://docs.extension.dev/code/code-tab.md) · [Build console](https://docs.extension.dev/code/build-console.md) · [Checkpoints](https://docs.extension.dev/code/checkpoints.md) · [Drafts](https://docs.extension.dev/code/drafts.md) · [Firefox engine](https://docs.extension.dev/code/firefox-engine.md) · [Download and run locally](https://docs.extension.dev/code/download-and-run.md) · [Share links](https://docs.extension.dev/code/share.md) · [Publish and import](https://docs.extension.dev/code/publish.md) · [Generation allowance](https://docs.extension.dev/code/allowance.md) · [Settings and sign-in](https://docs.extension.dev/code/settings.md)
Map of every page: https://docs.extension.dev/llms.txt · Whole corpus: https://docs.extension.dev/llms-full.txt

---

# AI generation

[code.extension.dev](https://code.extension.dev) is the workbench where you
describe an extension and get a working one back: a coding agent in the left
column, an emulated browser in the right, and a compiler between them that
never leaves the page. This section covers every part of it, from the
[terminal](/code/terminal) and [chat mode](/code/chat-mode) to
[checkpoints](/code/checkpoints), [drafts](/code/drafts),
[share links](/code/share) and [publish](/code/publish). Reach for the
workbench when you want a prototype running before a repository exists.

## Quick start

```text
Build an extension with a toolbar popup that shows a counter I can increment and reset, and remembers the count.
```

Type that into the terminal. The agent seeds a template, writes the files,
a compiler builds them inside the page, and the popup opens in the preview
next to the terminal. No server compiles your project and nothing installs
in your browser.


## How it works

### Five ways in

| You arrive with | Address | What it does |
| --- | --- | --- |
| A description | `code.extension.dev` | The prompt box starts the workspace, at `/new/chromium` |
| A starter card | `/?prompt=<text>&source=starter` | Routes the sentence to a template and seeds it |
| A template | `/?template=<slug>` | Seeds the workspace from that template |
| A shared artifact | `/?artifact=gen_<id>` | Opens a published artifact's files as your own project |
| A saved draft | `/drafts/<id>` | Reopens a draft you saved before |

`?prompt=<text>` alone only fills the prompt box. It never chooses the seed
and never sends itself, so one link can carry a template and a first
sentence together. When a link carries more than one entry, the draft wins,
then the artifact, then the template. Links of the older shape `/s/<id>`
still open and redirect to `/drafts/<id>`. See
[Starters and templates](/code/starters).

Every page in the [template catalog](/templates/) carries an **Open in
code.extension.dev** button, which is the `?template=` entry. The
[theme builder](/themes/) hands a theme over as an artifact, which is the
`?artifact=` entry.

### What a prompt returns

The first prompt on an empty workspace is routed to a starting template, so
the agent edits a working extension instead of inventing one. In the
terminal the agent then writes files with its own shell and runs `build`.
In chat mode the answer is one structured object:

| Part | What it is |
| --- | --- |
| Operations | `write`, `delete` and `rename` against the files you already have |
| Preview | Which entry the preview opens, and notes about it |
| User steps | What you have to do by hand, such as granting a permission |
| Assumptions | What the assistant decided when the prompt did not say |
| Reasoning | The summary, the steps, and the tradeoffs it took |

Only changed files come back. A correction edits what is there, so the parts
that were already right stay. See [Chat mode](/code/chat-mode).

### The workbench

| Tab | What is on it |
| --- | --- |
| AI | The terminal on the left, the running extension on the right |
| Code | Every file in the project, in an editor |
| Settings | The model, the terminal controls, the allowance, and the browser profile |
| Account | Your session |

Each applied edit saves a checkpoint, compiles, and reloads only what
changed in the preview. The compile runs in a web worker with esbuild built
to WebAssembly, so the edit to preview loop never leaves your machine. The
preview is the same emulator that
[shared build links](/share/unpublished-build-for-review) open. See
[Live preview](/code/preview), [Build console](/code/build-console) and
[The Code tab](/code/code-tab).

The header control that shows your extension's name lists every checkpoint
under the prompt that made it, with **Restore last known good** for the
newest checkpoint whose build passed. See [Checkpoints](/code/checkpoints).

### The terminal

The left column is a terminal running a coding agent. You type what you
want; the agent reads the project, writes files, runs `build`, and reads the
preview back with the `preview-*` commands. The terminal needs WebAssembly
JSPI, which Chrome and Edge 137 and Firefox 153 or newer ship and Safari
does not yet. A browser without it opens chat mode and says so. Chat mode is
also one switch away under **Settings**.

The agent has a fixed toolchain of 23 commands, and 17 of them are yours to
type with no model call: `build`, `changes`, `checkpoint`, `template`,
`usage` and the twelve `preview-*` commands. `publish`, `share`, `download`
and `run-in-chrome` are the agent's: typed bare they print a notice pointing
at the header control, and asked for, the agent runs them. `loop-trace` and
`preview-verify` go to the agent as prompts. See [Terminal](/code/terminal).

What the terminal does not have, on purpose:

- **No npm, node, web-ext, or any package manager.** `build` is the whole
  toolchain, and the compiler handles TypeScript, React, and CSS on its own.
- **No web access.** The agent never fetches a page or an API. Everything it
  reads is in the project or the preview.
- **No images, paste, or clipboard.** `share` prints its link. Nothing is
  copied for you.
- **No undo inside the agent.** Every applied edit records a checkpoint, so
  rolling back is the checkpoint list in the header.

Every model call spends what it cost from your generation allowance, and
**Settings** shows what is left before a refusal does. The conversation is
saved with the draft. Open the same draft on another browser and the terminal
resumes where it stopped. `/new` starts a fresh one.

### Where the work lives

Drafts sync to your account. `code.extension.dev/drafts` lists the cloud
drafts first, the recently deleted ones, and the draft open in this browser.

::: warning
The browser also keeps the open draft in the tab that made it. Each tab holds
its own browser database, and a database whose tab is gone is deleted on a
later sweep. Closing the tab is not a save. The draft on your account is.
See [Drafts](/code/drafts).
:::

### Sign in and the allowance

Everything on code.extension.dev runs on your account. Signed out, the
workbench is a door titled **Sign in to code.extension.dev** with one
button, **Continue with GitHub**, and you come back to the same address. See
[Settings and sign-in](/code/settings).

Free includes an amount of model spend each month, released one cent per
UTC day and shown as about how many generations are left. Every model call
spends what it cost at the model's list price, and **Settings** and `usage`
show what is left before a refusal does. More is released at the next UTC
midnight and the amount resets on the first of the next UTC month. When it
is spent, only generation locks. What you already built
stays on screen, so you can still preview it, edit it, and download it. The
refusal card says when the ceiling resets, links to the pricing page, and
offers **Buy $5 of AI** when the top-up is open for your workspace, or
**Notify me** otherwise. A $5 top-up credits $1.50 of model spend. See
[Generation allowance](/code/allowance).

### Taking the extension out

| Control | What you get |
| --- | --- |
| Download | A dialog with two rows. **Source (.zip)** is `<name>-source.zip`, every file as written. **Built extension (.zip)** is `<name>-chrome.zip`, the compiled build plus a README with the load unpacked steps |
| Run in a real browser | The `npx -y extension@latest preview` command that runs the built extension on your machine |
| Share | A preview link to the latest build, copied to your clipboard |
| Publish | The prototype on your account, with three handoffs below |

::: warning
The two zips are not the same thing. Chrome compiles nothing, so a project
written in TypeScript or React loads from the built zip and not from the
source zip. See [Download and run locally](/code/download-and-run).
:::

Publish answers with three handoffs:

- **An import link.** It opens
  `www.extension.dev/import?source=generation&artifactId=<id>`, which turns
  the prototype into a GitHub repository connected as a project. See
  [Publish and import](/code/publish) and [Import](/guides/import).
- **A live preview.** The same
  [shared preview](/code/share) a share link opens, so a reviewer installs
  nothing and signs in to nothing.
- **A command for your terminal.**

  ::: code-group

  ```bash [npm]
  npx -y extension@latest preview <zipUrl>
  ```

  ```bash [pnpm]
  pnpm dlx extension@latest preview <zipUrl>
  ```

  ```bash [yarn]
  yarn dlx extension@latest preview <zipUrl>
  ```

  ```bash [bun]
  bunx extension@latest preview <zipUrl>
  ```

  :::

  That runs the built extension in a fresh browser profile on your machine
  with Extension.js, the open source framework extension.dev sponsors. It
  needs no account, and it is documented at
  [extension.js.org](https://extension.js.org).

A real Gecko can also run the build beside the emulator. **Switch to
Firefox** in the preview's actions bar adds a `/firefox` suffix to the
address, on desktop browsers with JSPI and for logins the engine admits. See
[Firefox engine](/code/firefox-engine).

## Reference

| Limit | Value |
| --- | --- |
| Included model spend, Free | $0.30 a month, about 5 generations, a cent a day |
| Included model spend, Pro | $3.00 a month, about 56 generations |
| Top-up | $5 buys $1.50 of model spend |
| Drafts per account | 100 |
| Size of one draft | 4 MB |
| Repairs chat mode attempts on a build error | 4 |
| Published source artifact kept | 90 days |
| Shared preview link | 30 days on Free, 365 days on Pro |

The allowance numbers are the plan defaults. The deployment can set other
ceilings.

| Command | What it does |
| --- | --- |
| `build` | Compiles the workspace now and prints the diagnostics |
| `preview-errors` | Reads runtime errors and surface status from the live preview |
| `preview-describe [--host]` | Prints the preview state and an accessibility snapshot of the open surface, or of the served page |
| `preview-open <surface>` | Opens a declared surface: popup, options, side-panel, devtools, newtab, history, or bookmarks |
| `preview-click [--host] <selector>` | Clicks inside the open surface, or in the served page |
| `preview-type [--host] <selector> <text>` | Types inside the open surface, or into the served page |
| `preview-text [--host] [selector]` | Reads text from inside the open surface, or from the served page |
| `preview-network` | Lists the `chrome.*` API calls the extension has made |
| `preview-logs [--realm background] [--tail N]` | Reads the service worker's console and the surfaces' console, in time order |
| `preview-storage [local\|sync\|session] [key]` | Reads `chrome.storage` as the extension sees it |
| `preview-navigate <fixture\|list>` | Moves the served page to one of six fixture pages a content script can be proven on |
| `checkpoint <message>` | Records a restore point under that message |
| `changes` | Prints the diff of what you edited in the Code tab since the agent's last turn |
| `usage` | Prints how much of your generation allowance is left |
| `publish` | The agent puts the extension on your account and prints the import link |
| `share` | The agent mints a preview link from the last build |
| `download [dist\|source]` | The agent saves a zip into `.out/` and hands it to the browser |

## When it fails

- **A template link seeds nothing.** The slug is not in the seedable set.
  The workspace opens on the prompt box with a sentence naming what went
  wrong. Choose the template again from
  [templates.extension.dev](https://templates.extension.dev).
- **The workspace asks you to sign in.** Your session ended. Sign in and send
  the prompt again. The draft is still in this browser.
- **Generation is locked.** The daily or monthly allowance is spent.
  Everything you built stays: preview, editing, and download still work, and
  the refusal links to the pricing page.
- **The preview loads and nothing visible happens.** Read the user steps and
  the assumptions the answer returned. Many extensions need a step the page
  cannot take for you, such as a permission grant or a page to open.
- **The build breaks after a change.** The console under the preview lists
  the problems it read, and **Fix in terminal** hands them to the agent. If
  the fix does not land, restore the last known good checkpoint from the
  header.
- **The generated code is close but wrong.** Correct it in the same
  conversation. Edits land in place, so you do not lose the parts that
  worked.
- **An import link returns nothing.** A published source artifact is kept 90
  days. Publish again from the workspace.
- **A share link stopped opening.** A shared preview lives 30 days on Free
  and 365 days on Pro. Share the same build again to mint a new link.
- **Work disappeared when you reopened the browser.** The copy in this
  browser belongs to one tab and is swept once that tab is gone. Open the
  draft from `code.extension.dev/drafts`, where your account keeps it, or
  download the source before you close the tab.

## Next

- [Terminal](/code/terminal) for all twenty-three commands and the
  verification ladder
- [Chat mode](/code/chat-mode) for the structured answer and its repairs
- [Starters and templates](/code/starters) for the cards and links that seed
  a workspace
- [Publish and import](/code/publish) for the handoff to a repository
