Skip to content

Commit 3449855

Browse files
authored
feat(app): add searchable prompt history picker (#57716)
1 parent 46dd771 commit 3449855

12 files changed

Lines changed: 1074 additions & 304 deletions

File tree

‎packages/app/component-tests/composer.spec.ts‎

Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,310 @@
11
import { expect, story } from "../../storybook/playwright/story"
22

3+
story("cycles full prompts with plain arrows without opening the picker", async ({ mount, page }) => {
4+
const component = await mount("opencode-composer-flow--prompt-history")
5+
const input = component.getByRole("textbox", { name: "Prompt", exact: true })
6+
const options = component.getByRole("option")
7+
const composer = component.locator('[data-component="composer"]')
8+
const content = component.locator('[data-slot="composer-content"]')
9+
await page.evaluate(() => document.fonts.ready.then(() => undefined))
10+
const height = await composer.evaluate((element) => element.getBoundingClientRect().height)
11+
12+
await input.press("ArrowUp")
13+
await expect(input).toHaveText("Explain the latest changes")
14+
await expect(input).toBeFocused()
15+
await expect(options).toHaveCount(0)
16+
await expect(content).toHaveCSS("mask-image", "none")
17+
await input.press("ArrowUp")
18+
await expect(input).toHaveText("Review the composer layout\nKeep keyboard navigation accessible")
19+
await expect(component.getByRole("img", { name: "layout.png", exact: true })).toBeVisible()
20+
await expect(component.getByText("Keep the search separate from the draft", { exact: true })).toBeVisible()
21+
await expect
22+
.poll(() => composer.evaluate((element) => element.getBoundingClientRect().height))
23+
.toBeGreaterThan(height)
24+
await expect(options).toHaveCount(0)
25+
await input.press("ArrowUp")
26+
await expect(input).toHaveText("Run the focused tests")
27+
await input.press("ArrowUp")
28+
await expect(input).toHaveText("Run the focused tests")
29+
await input.press("ArrowDown")
30+
await expect(input).toHaveText("Review the composer layout\nKeep keyboard navigation accessible")
31+
await input.press("ArrowDown")
32+
await expect(input).toHaveText("Explain the latest changes")
33+
await input.press("ArrowDown")
34+
await expect(input).toBeEmpty()
35+
await expect(composer).toHaveCSS("height", `${height}px`)
36+
37+
// A new cycle saves and restores an empty-text draft's attachments and comments.
38+
await input.press("ArrowUp")
39+
await input.press("ArrowUp")
40+
await expect(input).toHaveText("Review the composer layout\nKeep keyboard navigation accessible")
41+
await input.fill("")
42+
await input.press("ArrowUp")
43+
await expect(input).toHaveText("Explain the latest changes")
44+
await expect(component.getByRole("img", { name: "layout.png", exact: true })).toHaveCount(0)
45+
await input.press("ArrowDown")
46+
await expect(input).toBeEmpty()
47+
await expect(component.getByRole("img", { name: "layout.png", exact: true })).toBeVisible()
48+
await expect(component.getByText("Keep the search separate from the draft", { exact: true })).toBeVisible()
49+
50+
await input.press("ArrowUp")
51+
await input.press("ArrowUp")
52+
await expect(input).toHaveText("Review the composer layout\nKeep keyboard navigation accessible")
53+
await input.press("ArrowRight")
54+
await input.press("Shift+ArrowUp")
55+
await expect(options).toHaveCount(0)
56+
await expect.poll(() => input.evaluate(() => window.getSelection()?.toString())).toBe("R")
57+
await input.press("ArrowRight")
58+
await input.pressSequentially("!")
59+
await expect(input).toHaveText("R!eview the composer layout\nKeep keyboard navigation accessible")
60+
await input.press("ArrowUp")
61+
await expect(input).toHaveText("R!eview the composer layout\nKeep keyboard navigation accessible")
62+
63+
await input.fill("")
64+
await input.press("ArrowUp")
65+
await expect(input).toHaveText("Explain the latest changes")
66+
await input.press("Enter")
67+
await expect(input).toBeEmpty()
68+
await expect(component.getByRole("status")).toHaveText("Submitted: Explain the latest changes")
69+
await input.press("ArrowUp")
70+
await expect(input).toHaveText("Explain the latest changes")
71+
await expect(options).toHaveCount(0)
72+
73+
await input.fill("!")
74+
await expect(input).toBeEmpty()
75+
await input.press("ArrowUp")
76+
await expect(input).toHaveText("bun typecheck")
77+
await expect(options).toHaveCount(0)
78+
await input.press("ArrowDown")
79+
await expect(input).toBeEmpty()
80+
await input.press("ArrowUp")
81+
await input.press("Enter")
82+
await expect(component.getByRole("status")).toHaveText("Submitted: bun typecheck")
83+
})
84+
85+
story(
86+
"previews history in the composer, accepts without sending, and cancels back to the original draft",
87+
async ({ mount, page }) => {
88+
const component = await mount("opencode-composer-flow--prompt-history")
89+
const input = component.getByRole("textbox", { name: "Prompt", exact: true })
90+
const search = component.getByRole("combobox", { name: "Prompt history", exact: true })
91+
const options = component.getByRole("option")
92+
const composer = component.locator('[data-component="composer"]')
93+
const content = component.locator('[data-slot="composer-content"]')
94+
const viewport = component.locator('[data-component="composer-scroll"] > .scroll-view__viewport')
95+
await page.evaluate(() => document.fonts.ready.then(() => undefined))
96+
const height = await composer.evaluate((element) => element.getBoundingClientRect().height)
97+
98+
await input.press("Shift+ArrowUp")
99+
await expect(input).toBeFocused()
100+
await expect(search).toHaveCount(0)
101+
await expect(input).toHaveText("Explain the latest changes")
102+
await expect(composer).toHaveCSS("height", `${height}px`)
103+
const menu = await component.locator('[data-component="composer-suggestions"]').boundingBox()
104+
105+
if (!menu) throw new Error("Missing history menu")
106+
await expect(options).toHaveText([
107+
"Run the focused tests",
108+
"Review the composer layout Keep keyboard navigation accessible",
109+
"Explain the latest changes",
110+
])
111+
await expect(component.getByRole("option", { selected: true })).toHaveText("Explain the latest changes")
112+
await input.press("ArrowUp")
113+
await expect(component.getByRole("option", { selected: true })).toHaveText(
114+
"Review the composer layout Keep keyboard navigation accessible",
115+
)
116+
await expect(input).toHaveText("Review the composer layout\nKeep keyboard navigation accessible")
117+
await expect(composer).toHaveCSS("height", `${height}px`)
118+
await expect
119+
.poll(() => component.locator('[data-slot="composer-content"]').evaluate((element) => element.scrollTop))
120+
.toBeGreaterThan(0)
121+
await expect(input).toBeInViewport()
122+
await expect
123+
.poll(() =>
124+
input.evaluate(
125+
(element) =>
126+
element.getBoundingClientRect().top -
127+
element.closest('[data-slot="composer-content"]')!.getBoundingClientRect().top,
128+
),
129+
)
130+
.toBe(0)
131+
await expect(content).not.toHaveCSS("mask-image", "none")
132+
await expect(viewport).toHaveCSS("mask-image", "none")
133+
await expect(content).toHaveCSS("--composer-fade-bottom", "16px")
134+
await content.evaluate((element) => {
135+
element.scrollTop = element.scrollHeight
136+
})
137+
await expect(content).toHaveCSS("--composer-fade-bottom", "0px")
138+
await expect
139+
.poll(async () => (await component.locator('[data-component="composer-suggestions"]').boundingBox())?.y)
140+
.toBe(menu.y)
141+
await expect(component.getByRole("img", { name: "layout.png", exact: true })).toBeVisible()
142+
await expect(component.getByText("Keep the search separate from the draft", { exact: true })).toBeVisible()
143+
await input.press("ArrowUp")
144+
await expect(input).toHaveText("Run the focused tests")
145+
await expect(composer).toHaveCSS("height", `${height}px`)
146+
await input.press("ArrowUp")
147+
await expect(input).toHaveText("Run the focused tests")
148+
await input.press("ArrowDown")
149+
await expect(input).toHaveText("Review the composer layout\nKeep keyboard navigation accessible")
150+
await input.press("ControlOrMeta+f")
151+
await expect(search).toBeFocused()
152+
await search.fill("ACCESSIBLE")
153+
await expect(composer).toHaveCSS("height", `${height}px`)
154+
await search.press("Escape")
155+
await expect(input).toBeFocused()
156+
await expect(composer).toHaveCSS("height", `${height}px`)
157+
await expect
158+
.poll(() =>
159+
component
160+
.locator('[data-slot="composer-content"]')
161+
.evaluate((element) => element.scrollHeight > element.clientHeight),
162+
)
163+
.toBe(true)
164+
await input.press("Enter")
165+
await expect(options).toHaveCount(0)
166+
await expect(content).toHaveCSS("mask-image", "none")
167+
await expect(viewport).not.toHaveCSS("mask-image", "none")
168+
await expect(content).toHaveCSS("--composer-fade-bottom", "0px")
169+
await expect(input).toBeFocused()
170+
await expect(input).toHaveText("Review the composer layout\nKeep keyboard navigation accessible")
171+
await expect(component.getByRole("status")).toHaveText("Ready")
172+
await expect
173+
.poll(() => composer.evaluate((element) => element.getBoundingClientRect().height))
174+
.toBeGreaterThan(height)
175+
176+
// History must not intercept navigation in a populated multiline editor.
177+
await input.press("ControlOrMeta+Home")
178+
await input.press("ArrowUp")
179+
await expect(search).toHaveCount(0)
180+
await expect(input).toHaveText("Review the composer layout\nKeep keyboard navigation accessible")
181+
182+
// An empty-text draft can still have attachments and comments. Previewing must not lose them on cancel.
183+
await input.fill("")
184+
const restoredHeight = await composer.evaluate((element) => element.getBoundingClientRect().height)
185+
await input.press("Shift+ArrowUp")
186+
await expect(input).toHaveText("Explain the latest changes")
187+
await expect(component.getByRole("img", { name: "layout.png", exact: true })).toHaveCount(0)
188+
await expect(composer).toHaveCSS("height", `${restoredHeight}px`)
189+
await input.press("Escape")
190+
await expect(options).toHaveCount(0)
191+
await expect(input).toBeFocused()
192+
await expect(input).toBeEmpty()
193+
await expect(composer).toHaveCSS("height", `${restoredHeight}px`)
194+
await expect(component.getByRole("img", { name: "layout.png", exact: true })).toBeVisible()
195+
await expect(component.getByText("Keep the search separate from the draft", { exact: true })).toBeVisible()
196+
},
197+
)
198+
199+
story("searches history on demand, returns to browsing, and keeps shell history separate", async ({ mount, page }) => {
200+
const component = await mount("opencode-composer-flow--prompt-history")
201+
const input = component.getByRole("textbox", { name: "Prompt", exact: true })
202+
const search = component.getByRole("combobox", { name: "Prompt history", exact: true })
203+
204+
await input.press("Shift+ArrowUp")
205+
await input.press("ControlOrMeta+f")
206+
await expect(search).toBeFocused()
207+
await search.fill("ACCESSIBLE")
208+
await expect(component.getByRole("option")).toHaveCount(1)
209+
await expect(input).toHaveText("Review the composer layout\nKeep keyboard navigation accessible")
210+
await expect(search).toBeFocused()
211+
await search.press("Escape")
212+
await expect(search).toHaveCount(0)
213+
await expect(input).toBeFocused()
214+
await expect(component.getByRole("option")).toHaveCount(3)
215+
await input.press("Escape")
216+
await expect(component.getByRole("option")).toHaveCount(0)
217+
await expect(input).toBeEmpty()
218+
219+
await input.press("Shift+ArrowUp")
220+
await component.getByRole("button", { name: /^Search/ }).click()
221+
await expect(search).toBeFocused()
222+
await search.fill("no matching prompt")
223+
await expect(component.getByRole("option")).toHaveCount(0)
224+
await expect(component.getByText("No matching items", { exact: true })).toBeVisible()
225+
await search.press("ArrowUp")
226+
await search.press("Enter")
227+
await expect(search).toHaveValue("no matching prompt")
228+
await expect(input).toHaveText("Explain the latest changes")
229+
await search.fill("tests")
230+
await expect(input).toHaveText("Run the focused tests")
231+
await expect(search).toBeFocused()
232+
await search.press("Enter")
233+
await expect(search).toHaveCount(0)
234+
await expect(input).toBeFocused()
235+
await expect(component.getByRole("status")).toHaveText("Ready")
236+
237+
await input.fill("")
238+
await input.press("Shift+ArrowUp")
239+
await component.getByRole("option", { name: "Run the focused tests", exact: true }).click()
240+
await expect(input).toHaveText("Run the focused tests")
241+
await expect(input).toBeFocused()
242+
await expect(component.getByRole("status")).toHaveText("Ready")
243+
244+
await page.mouse.move(0, 0)
245+
await input.fill("")
246+
await input.press("Shift+ArrowUp")
247+
await expect(input).toHaveText("Explain the latest changes")
248+
await expect(component.getByRole("option")).toHaveCount(3)
249+
await expect(input).toBeFocused()
250+
await input.press("Shift+Digit1")
251+
await expect(component.getByRole("option")).toHaveCount(0)
252+
await expect(input).toHaveText("Explain the latest changes!")
253+
254+
await input.fill("!")
255+
await expect(input).toBeEmpty()
256+
await input.press("Shift+ArrowUp")
257+
await expect(input).toHaveText("bun typecheck")
258+
await expect(component.getByRole("option")).toHaveText(["bun typecheck"])
259+
await input.press("Escape")
260+
await expect(input).toBeFocused()
261+
await expect(input).toBeEmpty()
262+
await input.press("Shift+ArrowUp")
263+
await input.press("Enter")
264+
await expect(input).toHaveText("bun typecheck")
265+
await expect(component.getByRole("status")).toHaveText("Ready")
266+
})
267+
268+
story("pins history search above the scrolling prompts", async ({ mount }) => {
269+
const component = await mount("opencode-composer-flow--prompt-history", { args: { many: true } })
270+
const input = component.getByRole("textbox", { name: "Prompt", exact: true })
271+
await input.press("Shift+ArrowUp")
272+
const menu = component.locator('[data-component="composer-suggestions"]')
273+
const search = menu.getByRole("button", { name: /^Search/ })
274+
const viewport = menu.getByRole("region", { name: "scrollable content" })
275+
await expect.poll(() => viewport.evaluate((element) => element.scrollHeight > element.clientHeight)).toBe(true)
276+
277+
const clippedTop = () =>
278+
viewport.evaluate((element) => {
279+
const top = element.getBoundingClientRect().top
280+
281+
return Array.from(element.querySelectorAll('[role="option"]')).some((row) => {
282+
const rect = row.getBoundingClientRect()
283+
284+
return rect.top < top && rect.bottom > top
285+
})
286+
})
287+
288+
await expect.poll(clippedTop).toBe(false)
289+
290+
for (let index = 0; index < 12; index++) await input.press("ArrowUp")
291+
await expect(menu.getByRole("option", { selected: true })).toBeInViewport({ ratio: 1 })
292+
await expect.poll(clippedTop).toBe(false)
293+
await expect.poll(async () => (await search.boundingBox())!.y - (await viewport.boundingBox())!.y).toBeLessThan(0)
294+
const top = (await search.boundingBox())!.y
295+
296+
await viewport.evaluate((element) => {
297+
element.scrollTop = 0
298+
})
299+
await expect(search).toBeInViewport()
300+
expect((await search.boundingBox())!.y).toBe(top)
301+
await viewport.evaluate((element) => {
302+
element.scrollTop = element.scrollHeight
303+
})
304+
await expect(search).toBeInViewport()
305+
expect((await search.boundingBox())!.y).toBe(top)
306+
})
307+
3308
for (const direction of ["ltr", "rtl"]) {
4309
for (const alternate of ["none", "queue", "steer"]) {
5310
story(`scrolls overflowing controls beside fixed ${alternate} actions in ${direction}`, async ({ mount, page }) => {

‎packages/app/src/composer/composer.stories.tsx‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ function ComposerStory(props: {
6060
longLabels?: boolean
6161
alternate?: "queue" | "steer"
6262
manySuggestions?: boolean
63+
history?: boolean
64+
manyHistory?: boolean
6365
suggestionBoundary?: () => HTMLElement | undefined
6466
}) {
6567
const [draft, setDraft] = createStore<ComposerPersistedState>({
@@ -149,6 +151,51 @@ function ComposerStory(props: {
149151
commands: () => commands,
150152
context: () => context,
151153
searchContextFiles: () => [],
154+
history: props.history
155+
? {
156+
entries: (mode) =>
157+
mode === "shell"
158+
? [{ prompt: text("bun typecheck") }]
159+
: [
160+
{ prompt: text("Explain the latest changes") },
161+
{
162+
prompt: [
163+
...text("Review the composer layout\nKeep keyboard navigation accessible"),
164+
{
165+
type: "path",
166+
id: "history-layout",
167+
filename: "layout.png",
168+
mime: "image/png",
169+
path: "C:/shots/layout.png",
170+
},
171+
],
172+
metadata: "review",
173+
},
174+
{ prompt: text("Run the focused tests") },
175+
...(props.manyHistory
176+
? Array.from({ length: 16 }, (_, index) => ({ prompt: text(`Earlier prompt ${index + 1}`) }))
177+
: []),
178+
],
179+
add() {},
180+
capture: () => (draft.context.items.some((item) => item.key === "history-comment") ? "review" : undefined),
181+
restore: (metadata) =>
182+
setDraft(
183+
"context",
184+
"items",
185+
metadata === "review"
186+
? [
187+
{
188+
type: "file",
189+
key: "history-comment",
190+
path: "src/composer.tsx",
191+
selection: { startLine: 12, startChar: 0, endLine: 14, endChar: 0 },
192+
comment: "Keep the search separate from the draft",
193+
},
194+
]
195+
: [],
196+
),
197+
}
198+
: undefined,
152199
readImage: async () => pixel,
153200
view: {
154201
placeholder: () => "Ask anything, / for commands, @ for context...",
@@ -231,7 +278,10 @@ function ComposerStory(props: {
231278
})
232279

233280
return (
234-
<div class="mx-auto flex min-h-80 w-full max-w-200 flex-col justify-end gap-3 rounded-xl bg-v2-background-bg-deep p-6">
281+
<div
282+
class="mx-auto flex min-h-80 w-full max-w-200 flex-col justify-end gap-3 rounded-xl bg-v2-background-bg-deep p-6"
283+
style={{ "min-height": props.manyHistory ? "512px" : undefined }}
284+
>
235285
<output class="text-12-regular text-text-weak" aria-live="polite">
236286
{story.activity}
237287
</output>
@@ -252,6 +302,15 @@ export default {
252302

253303
export const EmptyDraft = { render: () => <ComposerStory /> }
254304

305+
export const PromptHistory = {
306+
args: { many: false },
307+
render: (args: { many: boolean }) => (
308+
<div class="w-[min(640px,calc(100vw-32px))]">
309+
<ComposerStory history manyHistory={args.many} />
310+
</div>
311+
),
312+
}
313+
255314
export const TextDraft = { render: () => <ComposerStory prompt={text("Explain this change")} /> }
256315

257316
export const MultilineDraft = {

0 commit comments

Comments
 (0)