Files
coder/docs/ai-coder
Thomas Kosiewski 8ff3175299 feat: prefill the Coder Agents composer from a prompt link (#29943)
Integrations and docs can now send users to a new Coder Agents chat with
a message already written: `/agents?prompt=<url-encoded text>`.

- The text fills the new-chat composer. Nothing is sent until the user
selects **Send**.
- The page moves `prompt` out of the address bar into the router's
history state, in the same navigation that already handles
`?debug_workspace_build=` (#29811). Other query parameters stay, and a
reload of that entry shows the prompt again.
- A warning inside the composer, with a warning ring, asks the user to
take care before running a prompt that came from a link, since any
website can build one. It clears once the user edits the prompt.
- The saved draft is untouched. New chat afterwards shows the draft,
including when New chat is clicked directly on the prefilled page. The
layout now reads the deep link state (shared `utils/deepLinkState.ts`)
so it no longer clears the draft in that case. Debug links keep the
existing behavior, because they can fall back to the draft-backed
composer.
- The debug link wins when both parameters are present. A blank `prompt`
is ignored.
- No experiment flag, because integrations need a stable URL. Documented
in `docs/ai-coder/agents/getting-started.md`.

Tests: Vitest coverage for prefill-then-send, URL cleanup with history
state, debug link precedence, and New chat keeping the draft after a
prompt link and clearing it after a debug link. The `PromptLink` and
`PromptLinkEdited` stories cover the warning visually.

Remote dogfood UAT through Coder Agents passed on the implementation
head (sign-in redirect, reload, draft handling, edge cases such as `+`,
malformed encoding and HTML-looking text).

Known pre-existing behavior on `main`, unchanged here: New chat on a
plain `/agents` page clears the saved draft but leaves its text on
screen, so a reload shows an empty composer.

---

<details>
<summary>📋 Implementation Plan</summary>

# Plan: `?prompt=` link for the Coder Agents new-chat page

**Goal.** Opening `/agents?prompt=<url-encoded text>` shows the new-chat
page with that text already in the message box (the composer). The user
reviews it and clicks Send. Nothing is sent automatically.

**Base.** Branch `agents-prompt-deeplink`, created from `origin/main` at
`702b13112d`. The change reuses the link code that #29811 added for
`?debug_workspace_build=<build-id>`.

## Decisions

1. **Name the parameter `prompt`, not `q`.** Elsewhere in Coder, `q`
means a search filter.
2. **Fill in only; never send.** There is no `submit` option.
3. **Reuse the #29811 flow.** It works in five steps:
   1. `AgentCreatePage.tsx` reads the parameter.
2. It removes the parameter from the address bar with a `replace`
navigation.
3. It carries the value in `location.state`, the router's per-entry
history state.
   4. It builds an `AgentCreatePrefill` from the value.
5. `useEmptyStateDraft(prefill.message)` puts the text in the editor. It
doesn't touch the user's saved draft.
4. **Show a one-line notice** above the composer: "This prompt came from
a link. Review it before you send it." Any website can put text in this
link, so the user should read it before sending. The debug link carries
only a build ID, so it has no notice.
5. **Set no length limit in the browser.** See the rationale below.
6. **Don't put it behind an experiment flag, and document it.**
Integrations need a stable URL. `/agents` itself has no flag today
(`site/src/router.tsx:828`).
7. **The debug link wins.** `prompt` applies only when no debug link is
active. Both parameters are removed from the URL in one navigation.
8. **Ignore an empty or whitespace-only `prompt`.** The page shows the
normal composer.

Out of scope: `workspace=` or `model=` parameters, the embed page, and
existing chat pages (`/agents/:id`).

## Steps

### 1. Tests first (they should fail)

Add two tests to `site/src/pages/AgentsPage/AgentCreatePage.test.tsx`.
Copy the setup of the #29811 tests: `renderWithAuth(<AgentCreatePage />,
{ path: "/agents", route, extraRoutes })` with
`vi.spyOn(API.experimental, ...)`.

- **"prefills the prompt from a prompt link and sends it only on
Send."** Use the route `/agents?prompt=${encodeURIComponent("Fix the
flaky test\nin a & b = c")}`.
  1. Check that the textbox named "Chat message" has that text.
2. Check that `createChat` has not been called and no file was uploaded.
  3. Click Send.
  4. Check that `createChat` was called once with that text.
- **"moves the prompt out of the URL so New chat gets a plain
composer."** Copy the debug-link test with the same name. Use the route
`/agents?archived=archived&prompt=hi`.
1. Check `router.state.location` against `{ search:
"?archived=archived", state: { prompt: "hi" } }`.
2. Navigate to `/agents`. Check that the composer has no prefilled text.

→ verify: both tests fail on the current code because the textbox is
empty and `prompt` is still in the URL.

### 2. Implement (the tests should pass)

In `site/src/pages/AgentsPage/AgentCreatePage.tsx`:

1. Add `promptSearchParam = "prompt"`.
2. Replace `DebugLinkState` and `readDebugLinkState` with `DeepLinkState
= { debugWorkspaceBuildId?: string; prompt?: string }` and one reader
function.
3. Set `linkPrompt` from the URL parameter, or from the value in
`location.state` after the parameter is removed. Keep it only if it
isn't blank and no debug link is active.
4. **Extend the existing effect** at lines 102 to 116 so one `navigate`
removes both parameters and saves both values in state. Don't add a
second effect: each effect would rebuild the URL from the same snapshot
and put the other parameter back. The frontend pattern FE8 also says to
avoid effects where possible.
5. Build the prefill as `debugPrefill ?? (linkPrompt ? { message:
linkPrompt } : undefined)`.
6. Give the form a `key`:
   - the build ID for a debug link;
   - `` `prompt:${linkPrompt}` `` for a prompt link;
   - `"draft"` otherwise.

   A new link then resets the form.
7. Show the notice when a prompt link is active. Reuse the `Alert`
component that `DebugWorkspaceBuildAlert` uses, and place it next to
that alert (line 221).

In `site/src/pages/AgentsPage/components/AgentCreateForm.tsx`:

1. Make `AgentCreatePrefill.attachment` optional (line 74).
2. Handle a missing attachment in the `prefillFile` initializer (lines
581 to 587).

Don't add `useMemo` or `useCallback`: the React Compiler handles that in
`AgentsPage`.

→ verify: `cd site && pnpm test
src/pages/AgentsPage/AgentCreatePage.test.tsx
src/pages/AgentsPage/components/AgentCreateForm.test.tsx` passes,
including the tests #29811 added.

### 3. Story for the visual check

Add a `PromptLink` story to
`site/src/pages/AgentsPage/AgentsPageLayout.stories.tsx`, next to
`DebugWorkspaceBuildLoading` (line 1100). It shows the notice and the
filled composer. Its `play` function has no assertions (frontend pattern
FE1).

→ verify: `pnpm test:storybook
src/pages/AgentsPage/AgentsPageLayout.stories.tsx` passes. Take a
screenshot of the story from `pnpm storybook --no-open`.

### 4. Docs

In `docs/ai-coder/agents/getting-started.md`, add a section "### Link to
a new chat with a prompt" after "Use the API for programmatic
automation" (line 204). Cover:

- the URL format, built with `encodeURIComponent`, and one example;
- that the link only fills in the message, and the user still clicks
Send;
- that the link survives sign-in;
- that links should not contain secrets, because URLs end up in logs and
browser history.

`docs/manifest.json` doesn't change.

→ verify: `pnpm run format-docs && pnpm run lint-docs` finish clean.
Check the section against `docs/.style/style-guide/README.md`.

### 5. Gates before commit

1. Run `cd site && pnpm check && pnpm lint && pnpm format`, plus the
tests above.
2. Review the diff with the `frontend-review` skill (FE1 to FE10).
3. Commit with the hooks on. `make pre-commit` runs from the hook.

## Dogfooding

1. **Start the server.** Run `CODER_DEV_PORT_OFFSET=true
./scripts/develop.sh` detached and save its PID.
   - Use the web UI (Vite) URL printed in the startup banner.
- Don't use `.coderv2/url`: it can serve an older build of the frontend.
2. **Set up the instance.**
   1. Log in as `admin@coder.com` / `SomeSecurePassword!`.
2. Add a provider at `/ai/settings/providers` and a model at
`/ai/settings/models`. Without a model, the composer is disabled.
3. On `/agents`, type "my draft" and leave the page, so a saved draft
exists.
3. **Check with agent-browser.**
1. Open `<vite>/agents?prompt=<encoded "Fix the flaky test\nline 2 &
more = ok 🚀">`. The snapshot should show:
      - the decoded text in the composer;
      - no `prompt` in the address bar;
      - the notice;
      - no new chat in the sidebar.

      Take a screenshot.
2. Reload the page. The prompt should appear again, because it is kept
in history state (the debug link behaves the same way).
3. Edit the text and click Send. The page should move to `/agents/<id>`,
and the first message should be the edited text.
   4. Click New chat. The composer should show "my draft".
5. Log out. In a fresh session, open the link, then log in. The composer
should show the prompt.
4. **Save the evidence.**
   - Screenshots of checks 1, 4 and 5, attached with `attach_file`.
- One short WebM video of checks 1 to 3, recorded in a fresh session.
agent-browser recordings freeze after about two navigations, so keep it
short.
5. **Clean up.** Stop `develop.sh` by its PID and confirm its ports are
free.

## Acceptance criteria

1. `/agents?prompt=<encoded>` shows the decoded text in the composer,
with newlines, `&`, `=` and emoji intact.
2. No chat is created until the user clicks Send.
3. `prompt` is removed from the address bar and other parameters stay.
New chat shows the normal composer, and the saved draft is unchanged.
4. A reload shows the link's prompt again, the same way the debug link
does.
5. The notice appears only for prompt links.
6. The link works after the sign-in redirect, for both password and
OAuth login.
7. An empty `prompt` shows the normal composer. The debug link works as
before and wins when both parameters are present.
8. The unit tests, story, docs checks and pre-commit all pass.

If a PR is requested, use the title `feat: prefill the Coder Agents
composer from a prompt link`. It has no scope because the change touches
both `site/` and `docs/`.

<details>
<summary>Why no length limit, and what the sign-in check
covers</summary>

- **No length limit.** A limit doesn't stop hidden text: even a short
prompt can push lines below the visible part of the box. The real
protections are that nothing sends without a click and that the notice
asks the user to read the text. Browsers and reverse proxies already
limit URL length. The server caps the create-chat request body at 256
KiB (`coderd/exp_chats.go:79`), so a very long prompt fails when sent.
- **Sign-in.** `RequireAuth` sends the user to `/login?redirect=<encoded
path and query>`. Password login returns through `sanitizeRedirect`, and
OAuth login returns through `SafeRedirectPath`. On both paths, the query
is decoded exactly once, so `%26` stays inside the prompt. Existing
tests cover query preservation (`site/src/utils/redirect.test.ts`,
`LoginPage.test.tsx`), so no new test is needed. Dogfooding check 5
confirms it end to end.

</details>


</details>

---
_Generated with [`xum`](https://github.com/coder/xum) • Model:
`anthropic:claude-opus-5-5` • Thinking: `high`_

---------

Signed-off-by: Thomas Kosiewski <tk@coder.com>
2026-09-28 12:51:15 +02:00
..