# composer-kit

A message composer for agent products, and the conversation above it: mentions, skills, attachments, voice, a model picker, queued follow-ups, questions from the agent, connectors, a context row, and a thread with streamed replies, tool calls, diffs, questions and retries. One JS file, one CSS file, no dependencies. MIT.

Configure it and copy the code in the playground (`index.html` in the repo).

## Use it

```html
<link rel="stylesheet" href="composer-kit.css">
<script src="composer-kit.js"></script>

<div id="composer"></div>

<script>
  const composer = ComposerKit.create('#composer', {
    mentions: { files: [{ id: 'src/Field.tsx', label: 'Field.tsx', desc: 'src' }] },
    skills: [{ id: 'review', desc: 'Review the diff against the house rules' }],
  });
  composer.on('submit', payload => fetch('/api/agent', { method: 'POST', body: JSON.stringify(payload) }));
</script>
```

In React, create it in an effect and destroy it on unmount. The playground's React tab has the whole component.

## What it does

- `@` opens files (and agents or people, if you pass them). `/` opens skills. Both characters can be changed or turned off.
- A pick turns the typed text into one token that deletes as a unit.
- Paste a screenshot and it becomes an image attachment. Paste 30 or more lines, or a stack trace, and it becomes a log attachment instead of a wall of text. Files can be dropped or added from the `+` menu.
- If you pass agents and the message starts with one, it goes to them; turn on `ping` to offer queue, steer (Alt+Enter) or notify while that agent runs.
- The `+` menu groups Add, Context (connectors and skills, with drill-down) and Recent. Connectors carry their real logos (GitHub, Linear, Slack, Sentry, Figma, Notion, Vercel and more) and show their status: a switch when connected, Connect when they need sign-in, and the reason with an action when they fail (`Retry` by default, or your own, such as `Sign in` for an expired token). Connected ones can also be mentioned with `@`.
- The model picker groups models by provider, each with the company's real logo (Claude, OpenAI, Gemini, xAI, Mistral, DeepSeek; other providers get a pixel mark), a context and price hint, a search field, reasoning effort, and an optional custom entry (name, endpoint, key; the key stays out of the payload).
- The mic: tap to record, hold to talk and release to finish. Tapping gives the transcript its own line as you speak, with the level, the time, Cancel, and Insert where send sits. Without permission or a microphone it says so and how to fix it.
- While a turn runs, Enter queues the message. Queued rows can be edited (double-click or Enter), reordered (drag the grip, or Alt+Up and Down), sent now (it steers the live turn) or removed. They send in order when the turn ends.
- `composer.ask(questions)` docks a question card above the input: the question and a tight list of numbered options (press the digit), or a typed answer. While a turn runs, "Question 1 of 3" and Skip sit in the status line; Esc skips and the arrows page.
- Edge cases each have a state: offline (messages wait), rate limited (a countdown), stopped, disabled, a failed upload with Retry that holds send until it is fixed, a file over the size limit, a full attachment row that ends on a whole tile with a count of the rest, long text that fades at the scroll edge, a mention with no match, and IME composition that never sends early.
- The context row holds any slots you define (repo and branch by default). A slot's options can depend on another slot.
- Light, dark or auto. The accent colour, send shape, density, placeholder and width are all settings.
- Every list is a listbox with arrow keys, Enter, Tab and Escape, and changes are announced to screen readers.

## The thread

`ComposerKit.thread(host, opts)` draws the conversation above the composer, in the same material. Bind it to a composer and each send becomes a turn; your `reply` runs the agent:

```js
const thread = ComposerKit.thread('#thread', { theme: 'auto' });
thread.bind(composer, async (payload) => {
  const turn = thread.agent({ label: 'Opus 5.5', model: 'Opus 5.5' });
  await turn.think(1200);
  await turn.tool({ kind: 'read', target: 'src/Field.tsx', body: { start: 112, lines: source } });
  await turn.say('Found it. `removeToken` detaches the node the caret was in.');
  await turn.diff({ file: 'src/Field.tsx', start: 114, lines: [['-', 'removeToken(sel.anchorNode);'], ['+', 'const after = removeToken(sel.anchorNode);']] });
  const { option } = await turn.ask({ text: 'Which branch?', options: ['fix/paste-keys', 'main'] });
  turn.done();
  composer.finish();
});
```

- Your turns sit in a sunken well. Messages that waited in the queue say "From the queue", and messages sent mid-turn say "Sent while it works".
- `think`, `say`, `tool`, `diff`, `ask` and `fail` each return a promise, so a turn reads in order. Consecutive tool calls share one inset band. Each row runs, then says what it did, and opens on its detail: code for a read, a diff for an edit, output for a run.
- `fail(text)` shows what went wrong, that nothing changed, and Retry. It resolves when the user retries.
- While you read further up, the thread stays where you are and a black circle takes you back to the latest message.
- Set `runFor: 0` on the composer when a thread drives the turns, and call `composer.finish()` at the end of each one so the queue drains.

## The Captain

`captain: { size, sizes, autonomy, budget, budgets, types }` adds a Captain control by the model picker. Its panel sets who leads (the Captain or one agent), the swarm size, autonomy (`ask`, `risk`, `full`), the budget cap and the agent types it may use. `payload.captain` carries them; the composer emits `captain` when they change and `budget` when spend reaches the cap. `meter: {}` adds tokens and spend; feed it with `composer.spend({ usd, tokens })`. While the Captain runs, a message you send goes to it at once (`mode: 'steer'`) instead of queueing.

On a turn: `state(name)`, `reason(text)`, `plan(tasks, { density })`, `swarm()`, `merge({ text, conflict })` and `route(text, to)`. On the thread: `pair(a, b)` and `handoff(from, to, text)`. Agent types (`review`, `tests`, `build`, `research`, `security`, `docs`, `design`, `ship`) each have a mark and a colour, in `ComposerKit.types`.

Also new in 0.5: send turns into a stop square while a turn runs; ↑ edits your last message; Esc stops; pasted links become cards (`unfurl`); text files show their first line; skills can show a `shortcut`; `composer.draft(id)` keeps a draft per thread.

## Phones

At 560px and under, every picker except the inline mention list opens as a bottom sheet over a scrim. Controls, rows and the question options are all at least 44px tall.

## The payload

```js
{
  version: 1,
  text: '/review @Field.tsx drops a key after a paste',
  tokens: [{ kind: 'skill', id: 'review', range: [0, 7] }, { kind: 'file', id: 'src/Field.tsx', range: [8, 18] }],
  attachments: [{ kind: 'log', name: 'Pasted log', size: '214 lines' }],
  to: [],
  mode: 'queue',
  skill: { id: 'review' },
  model: { id: 'opus-5-5', effort: 'medium' },
  context: { repo: 'composer-kit', branch: 'main' },
  connectors: ['github', 'linear'],
}
```

## API

`ComposerKit.create(host, config)` returns the composer. Besides `payload()`, `focus()`, `clear()`, `look()`, `update()` and `destroy()` it has:

- `ask(questions, { from })`, which resolves with the answers
- `setBusy(on, who, ms)`, `finish()`, `stop()` for your app's own turns, and `queue` / `clearQueue()`
- `setOffline(on)`, `limit(seconds)`, `setDisabled(on, reason)`, `note(kind, text, opts)`
- `attach(meta)`, `insertText(text)`, `pasteFile(file)`, `pasteText(text)`, `voice(on)`

`ComposerKit.brand(name)` returns the real logo for a provider or app name as an SVG string (sources in `BRANDS.md`).

Events: `change`, `submit`, `done`, `stop`, `queue`, `answer`, `answers`, `attach`, `uploaded`, `connect`, `custommodel`. Types are in `composer-kit.d.ts`.

Set `runFor: 0` when your backend decides when a turn ends, and call `setBusy(false)` then. Pass `attachments.upload(file)` to upload for real, `voice.transcribe(blob)` to use your own speech-to-text, and `onConnect(connector)` to run your sign-in.

Any key you leave out keeps the kit's default. Passing `mentions` or `plus` replaces the default lists whole. Set `ping`, `model` or `plus` to `false` to remove that control.
