Optimize prompt button above the prompt box: a model (Haiku by default) rewrites your draft in place

A Claude Code mod: an Optimize prompt button above the prompt box. A model (Haiku by default) rewrites your draft in place.
model: haiku (default), sonnet or opus. Set it at install, or later in /config.
At the prompt of a Claude Code terminal session:
/plugin install optimize-prompt --marketplace kelokchan/optimize-prompt
Answer y to add the marketplace, then pick a scope (user scope is first; press Enter).
From a shell:
claude plugin install optimize-prompt --marketplace kelokchan/optimize-prompt
In the desktop app, where /plugin does not run: click + next to the prompt box, then Plugins, Add marketplace with kelokchan/optimize-prompt, then Add plugin.
hooks/register.tsx 116 lines1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register, ToolUseSummary } from 'claude-code'
3
4const isBusy = atom({ plugin: 'optimize-prompt', key: 'isBusy' } as const, false)
5const frame = atom({ plugin: 'optimize-prompt', key: 'frame' } as const, 0)
6
7// Braille dots: one column wide in every terminal font. No Spinner element on
8// the terminal, so a $.clock.every tick advances `frame` and the band redraws.
9const SPINNER = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
10const SPINNER_MS = 100
11const spinnerGlyph = (n: number) => SPINNER[n % SPINNER.length] ?? ''
12
13const SYSTEM = `You rewrite a draft prompt the user is about to send to an AI coding assistant, in the middle of an ongoing session.
14<conversation> holds the latest messages of that session and the assistant's tool calls between them (lines starting "tool", showing files read or edited, commands run and their results). Use it only to understand what the draft refers to: resolve vague references ("it", "the button", "that error") to the concrete thing, file or feature being discussed. Name a file or command from it only where the draft refers to one.
15Rules:
16- Keep the draft's scope. Never add requirements, deliverables, sections or questions the author did not ask for.
17- Never comment on what the draft leaves unspecified. If the conversation does not resolve something, leave it as written.
18- Stay proportionate: a one-line draft becomes one to three sentences, not a spec with headings.
19- Keep the author's voice, language and every concrete detail (file paths, names, code, numbers).
20- Do not answer or carry out the prompt.
21Reply with the rewritten prompt only, no preamble or quotes.`
22
23// Recent turns ground the rewrite: without them Haiku sees a bare draft and
24// generalises "the spinner" into "the application's UI".
25// Tool calls show which files and commands "it" means. The window is counted in
26// text messages so a tool-heavy turn can't push the person's own words out.
27// ponytail: plain char cuts, not token counts; CONTEXT_TOTAL is the ceiling.
28const CONTEXT_MESSAGES = 8 // text messages; tool calls between them ride along
29const CONTEXT_CHARS = 1200 // per message text, its tail kept
30const TOOL_CHARS = 200 // per tool input and per tool result, the head kept
31const CONTEXT_TOTAL = 12000 // whole block, the newest kept
32
33const head = (s: string, n: number) => (s.length > n ? `${s.slice(0, n)}…` : s)
34const tail = (s: string, n: number) => (s.length > n ? `…${s.slice(-n)}` : s)
35
36const toolLine = (u: ToolUseSummary) =>
37 `tool ${u.tool}(${head(JSON.stringify(u.input), TOOL_CHARS)})` +
38 (u.text === undefined ? '' : ` → ${u.isError ? 'error: ' : ''}${head(u.text, TOOL_CHARS)}`)
39
40const recentContext = async ($: EngineInterface) => {
41 const messages = (await $.session.messages()).filter(m => m.text.trim() || m.toolUses.length)
42 const textAt = messages.flatMap((m, i) => (m.text.trim() ? [i] : []))
43 const rendered = messages
44 .slice(textAt.at(-CONTEXT_MESSAGES) ?? 0)
45 .map(m =>
46 [...(m.text.trim() ? [`${m.role}: ${tail(m.text, CONTEXT_CHARS)}`] : []), ...m.toolUses.map(toolLine)].join('\n'),
47 )
48 .join('\n\n')
49 return tail(rendered, CONTEXT_TOTAL)
50}
51
52export const register: Register = (on, options) => {
53 // userConfig `model` in plugin.json; the engine fills its default (haiku).
54 const model = String(options.model)
55
56 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
57 if (e.props.hasSurvey) {
58 return next(e)
59 }
60
61 const busy = await read($, isBusy)
62 const { Box, Button, Text } = $.ui.resolve(e)
63
64 const optimize = async () => {
65 if (await read($, isBusy)) return
66
67 const draft = (await $.prompt.read()).text
68 if (!draft.trim()) {
69 $.ui.toast('Type a prompt first')
70 return
71 }
72
73 await update($, isBusy, () => true)
74 const tick = $.clock.every(SPINNER_MS, () => void update($, frame, n => n + 1))
75 try {
76 const context = await recentContext($)
77 const r = await $.model.complete({
78 model,
79 system: SYSTEM,
80 prompt: `${context && `<conversation>\n${context}\n</conversation>\n\n`}<draft>\n${draft}\n</draft>`,
81 effort: 'low',
82 maxTokens: 2048,
83 timeoutMs: 30000,
84 })
85 if (!r.isAnswered) {
86 $.ui.toast(`Optimize failed: ${r.reason}`)
87 return
88 }
89 // Don't clobber anything typed while the model was working.
90 if ((await $.prompt.read()).text !== draft) {
91 $.ui.toast('Draft changed while optimizing; left it as is')
92 return
93 }
94 await $.prompt.fill({ text: r.text.trim(), mode: 'replace' })
95 } finally {
96 tick.cancel()
97 await update($, isBusy, () => false)
98 }
99 }
100
101 return (
102 <Box>
103 {busy ? (
104 <Text color="suggestion">
105 {spinnerGlyph(await read($, frame))} Optimizing prompt
106 </Text>
107 ) : (
108 <Button key="optimize" variant="primary" onPress={optimize}>
109 ✦ Optimize prompt
110 </Button>
111 )}
112 </Box>
113 )
114 })
115}
116types/index.d.ts 8 lines1export type Busy = boolean
2
3declare module 'claude-code' {
4 interface PluginState {
5 'optimize-prompt': { isBusy: Busy; frame: number }
6 }
7}
8