mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
feat(app): queue a follow-up first with Alt+Enter (#63109)
This commit is contained in:
committed by
GitHub
Unverified
parent
c554c31cbe
commit
4b762db5f0
@@ -236,6 +236,34 @@ test("follow-up preference controls Enter while Mod+Enter uses the alternate del
|
||||
await expect(view.input).toHaveText("")
|
||||
})
|
||||
|
||||
test("Alt+Enter queues a follow-up first without steering, regardless of the follow-up preference", async ({
|
||||
page,
|
||||
}) => {
|
||||
const mock = createQueueMock(["Follow-up A", "Follow-up B"])
|
||||
|
||||
const view = await openQueue(page, mock, "steer")
|
||||
|
||||
await view.input.fill("Follow-up C")
|
||||
await view.input.press("Alt+Enter")
|
||||
await expect(view.rows.locator('[data-action="session-queue-edit"]')).toHaveText([
|
||||
"Follow-up C",
|
||||
"Follow-up A",
|
||||
"Follow-up B",
|
||||
])
|
||||
await expect
|
||||
.poll(() => mock.rows.map((row) => [row.payload.text, row.delivery]))
|
||||
.toEqual([
|
||||
["Follow-up C", "queue"],
|
||||
["Follow-up A", "queue"],
|
||||
["Follow-up B", "queue"],
|
||||
])
|
||||
await expect(view.input).toHaveText("")
|
||||
expect(mock.changes.every((change) => change.action === "cancel")).toBe(true)
|
||||
await view.input.fill("Another follow-up")
|
||||
await view.composer.locator('[data-action="composer-alternate-delivery"]').hover()
|
||||
await expect(page.getByRole("tooltip")).toContainText("Queue first")
|
||||
})
|
||||
|
||||
test("a blank Enter leaves the running session alone while Stop still interrupts it, as in the TUI", async ({
|
||||
page,
|
||||
}) => {
|
||||
|
||||
@@ -51,6 +51,8 @@ export type ComposerQueue = {
|
||||
cancelEdit: () => void
|
||||
// Loads the first queued prompt into the composer. Returns false when the queue is empty.
|
||||
editFirst: () => boolean
|
||||
// Moves an admitted follow-up ahead of the queued prompts, using the queue's serialized reorder.
|
||||
first: (inboxID: string) => void
|
||||
}
|
||||
|
||||
export type ComposerSession = {
|
||||
|
||||
@@ -123,6 +123,7 @@ function TouchComposer(props: { state: State }) {
|
||||
confirmEdit: (delivery: "steer" | "queue") => record(`Edited queued prompt, ${delivery}`),
|
||||
cancelEdit: () => record("Cancelled the edit"),
|
||||
editFirst: () => false,
|
||||
first() {},
|
||||
}
|
||||
|
||||
const editor = createComposerEditor({
|
||||
|
||||
@@ -241,6 +241,7 @@ function ComposerStory(props: {
|
||||
undoing: () => false,
|
||||
cancelEdit() {},
|
||||
editFirst: () => false,
|
||||
first() {},
|
||||
}
|
||||
: undefined,
|
||||
onSubmit: () => {
|
||||
|
||||
@@ -67,6 +67,8 @@ export function Composer(props: {
|
||||
attachKeybind={command.keybindParts("file.attach")}
|
||||
attachShortcut={command.keybind("file.attach")}
|
||||
alternateKeybind={[formatKeybind("mod", language.t), "↵"]}
|
||||
queueFirstKeybind={[formatKeybind("alt", language.t), "↵"]}
|
||||
queueFirstLabel={language.t("session.queue.first")}
|
||||
exitShellKeybind={[formatKeybind("esc", language.t)]}
|
||||
suggestionBoundary={props.suggestionBoundary}
|
||||
modelControl={
|
||||
|
||||
@@ -93,6 +93,8 @@ export type ComposerEditorProps = {
|
||||
attachKeybind?: string[]
|
||||
attachShortcut?: string
|
||||
alternateKeybind?: string[]
|
||||
queueFirstKeybind?: string[]
|
||||
queueFirstLabel?: string
|
||||
exitShellKeybind?: string[]
|
||||
suggestionBoundary?: () => HTMLElement | undefined
|
||||
}
|
||||
@@ -381,7 +383,13 @@ export function ComposerEditor(props: ComposerEditorProps) {
|
||||
|
||||
if (event.repeat) return
|
||||
// On touch, Cmd+Return is the touch bar's primary action, since plain Return cannot send.
|
||||
props.controller.submit(mod && !touch() ? { alternate: true } : undefined)
|
||||
props.controller.submit(
|
||||
!touch() && event.altKey && !mod
|
||||
? { first: true }
|
||||
: mod && !touch()
|
||||
? { alternate: true }
|
||||
: undefined,
|
||||
)
|
||||
}
|
||||
}}
|
||||
// The editor holds one flat run of text, mentions and <br>s. A touch keyboard's Return arrives as a
|
||||
@@ -506,6 +514,8 @@ export function ComposerEditor(props: ComposerEditorProps) {
|
||||
<ComposerEditorAlternateDelivery
|
||||
controller={props.controller}
|
||||
keybind={props.alternateKeybind ?? ["Mod", "Enter"]}
|
||||
firstKeybind={props.queueFirstKeybind ?? ["Alt", "Enter"]}
|
||||
firstLabel={props.queueFirstLabel}
|
||||
/>
|
||||
</Show>
|
||||
<Show when={mode() === "shell"}>
|
||||
@@ -1438,7 +1448,12 @@ function composerSuggestionMaxHeight(boundaryHeight: number, search: boolean, hi
|
||||
// delivery opposite to what plain Enter does. Visible only while the queue
|
||||
// exposes an alternate (turn running and composer holding a value), so it
|
||||
// disappears on its own when the current turn ends.
|
||||
function ComposerEditorAlternateDelivery(props: { controller: ComposerEditorModel; keybind: string[] }) {
|
||||
function ComposerEditorAlternateDelivery(props: {
|
||||
controller: ComposerEditorModel
|
||||
keybind: string[]
|
||||
firstKeybind: string[]
|
||||
firstLabel?: string
|
||||
}) {
|
||||
const i18n = useI18n()
|
||||
const view = props.controller.view
|
||||
|
||||
@@ -1456,7 +1471,27 @@ function ComposerEditorAlternateDelivery(props: { controller: ComposerEditorMode
|
||||
return (
|
||||
<Show when={action()} keyed>
|
||||
{(delivery) => (
|
||||
<Tooltip placement="top" inactive={delivery !== "steer"} value={i18n.t("ui.promptInput.steerHint")}>
|
||||
<Tooltip
|
||||
placement="top"
|
||||
inactive={delivery !== "steer" && !view.submit.queue?.count()}
|
||||
value={
|
||||
<Show
|
||||
when={view.submit.queue?.count() && !view.submit.queue?.editing() && props.firstLabel}
|
||||
fallback={i18n.t("ui.promptInput.steerHint")}
|
||||
>
|
||||
<div class="flex flex-col gap-1">
|
||||
<div class="flex items-center justify-between gap-4">
|
||||
<span>{delivery === "steer" ? i18n.t("ui.promptInput.steer") : i18n.t("ui.promptInput.queue")}</span>
|
||||
<Keybind keys={props.keybind} variant="neutral" />
|
||||
</div>
|
||||
<div class="flex items-center justify-between gap-4">
|
||||
<span>{props.firstLabel}</span>
|
||||
<Keybind keys={props.firstKeybind} variant="neutral" />
|
||||
</div>
|
||||
</div>
|
||||
</Show>
|
||||
}
|
||||
>
|
||||
<Button
|
||||
data-action="composer-alternate-delivery"
|
||||
type="button"
|
||||
|
||||
@@ -75,7 +75,7 @@ export type ComposerEditorView = {
|
||||
delivery: Accessor<ComposerDelivery | undefined>
|
||||
onSubmit: (key: string, comment: string) => Promise<boolean>
|
||||
}
|
||||
onSubmit: (options?: { alternate?: boolean }) => void
|
||||
onSubmit: (options?: { alternate?: boolean; first?: boolean }) => void
|
||||
onStop: () => void
|
||||
}
|
||||
shell?: {
|
||||
@@ -748,7 +748,7 @@ export function createComposerEditor(input: {
|
||||
openShell() {
|
||||
dispatch({ type: "mode.shell" })
|
||||
},
|
||||
submit(options?: { alternate?: boolean }) {
|
||||
submit(options?: { alternate?: boolean; first?: boolean }) {
|
||||
if (input.view.submit.available?.() === false) return
|
||||
|
||||
if (input.view.draftOnly) return
|
||||
|
||||
@@ -17,6 +17,7 @@ export type ComposerSubmission = {
|
||||
images: ImageAttachmentPart[]
|
||||
selection: ComposerSelection
|
||||
delivery: ComposerDelivery
|
||||
first?: boolean
|
||||
}
|
||||
|
||||
type QuoteItem = Extract<ComposerSubmission["context"][number], { type: "quote" }>
|
||||
@@ -88,6 +89,7 @@ export type SubmitSnapshot = {
|
||||
text: string
|
||||
mode: "normal" | "shell"
|
||||
alternate: boolean
|
||||
first?: boolean
|
||||
delivery: ComposerDelivery
|
||||
selection: ComposerSelection | undefined
|
||||
loading: boolean
|
||||
@@ -142,7 +144,7 @@ export type ComposerSubmitEffect =
|
||||
| { type: "notify.missing-selection" }
|
||||
| { type: "notify.unqueueable" }
|
||||
| { type: "interrupt" }
|
||||
| { type: "submit.retry"; alternate: boolean; done: SubmitDone }
|
||||
| { type: "submit.retry"; alternate: boolean; first?: boolean; done: SubmitDone }
|
||||
/** Shows a held prompt in its Session's timeline and clears the composer it came from. */
|
||||
| { type: "hold.show"; attempt: number; session: ComposerSession; snapshot: SubmitSnapshot }
|
||||
/** Removes a cancelled held prompt from the timeline and returns it to the composer it left. */
|
||||
@@ -224,7 +226,14 @@ export function transitionComposerSubmit(
|
||||
if (!state.shown)
|
||||
return {
|
||||
state: { type: "idle", attempt: state.attempt },
|
||||
effects: [{ type: "submit.retry", alternate: state.snapshot.alternate, done: state.done }],
|
||||
effects: [
|
||||
{
|
||||
type: "submit.retry",
|
||||
alternate: state.snapshot.alternate,
|
||||
first: state.snapshot.first,
|
||||
done: state.done,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
// The composer already cleared, so the send goes on from what was captured at Enter.
|
||||
@@ -372,6 +381,7 @@ function prepare(
|
||||
images: snapshot.draft.prompt.filter((part): part is ImageAttachmentPart => part.type === "image"),
|
||||
selection,
|
||||
delivery: snapshot.delivery,
|
||||
first: snapshot.first,
|
||||
}
|
||||
|
||||
// Only a prompt is admitted under its ID; shell commands and server commands always start fresh.
|
||||
|
||||
@@ -309,6 +309,7 @@ export function createComposerModel(adapter: ComposerAdapter, options?: { queue?
|
||||
|
||||
return (alternate ? queue.alternate() : queue.delivery()) ?? "steer"
|
||||
},
|
||||
queueFirst: (inboxID) => options?.queue?.first(inboxID),
|
||||
notify: {
|
||||
missingSelection: () =>
|
||||
showToast({
|
||||
|
||||
@@ -55,6 +55,7 @@ type ComposerSubmitInput = {
|
||||
resetHistory: () => void
|
||||
closePopover: () => void
|
||||
delivery?: (alternate: boolean) => ComposerDelivery
|
||||
queueFirst?: (inboxID: string) => void
|
||||
clientCommand?: (text: string) => (() => void | Promise<void>) | undefined
|
||||
notify: {
|
||||
missingSelection: () => void
|
||||
@@ -93,7 +94,7 @@ export function createComposerSubmit(input: ComposerSubmitInput) {
|
||||
}
|
||||
|
||||
if (effect.type === "submit.retry")
|
||||
return dispatch({ type: "submit", snapshot: snapshot(input, effect.alternate), done: effect.done })
|
||||
return dispatch({ type: "submit", snapshot: snapshot(input, effect.alternate, effect.first), done: effect.done })
|
||||
|
||||
if (effect.type === "hold.show") {
|
||||
input.adapter.beforeSubmit?.()
|
||||
@@ -257,11 +258,15 @@ export function createComposerSubmit(input: ComposerSubmitInput) {
|
||||
|
||||
return {
|
||||
state,
|
||||
submit(event: globalThis.Event, options?: { alternate?: boolean }) {
|
||||
submit(event: globalThis.Event, options?: { alternate?: boolean; first?: boolean }) {
|
||||
event.preventDefault()
|
||||
|
||||
return new Promise<void>((resolve) =>
|
||||
dispatch({ type: "submit", snapshot: snapshot(input, options?.alternate ?? false), done: () => resolve() }),
|
||||
dispatch({
|
||||
type: "submit",
|
||||
snapshot: snapshot(input, options?.alternate ?? false, options?.first),
|
||||
done: () => resolve(),
|
||||
}),
|
||||
)
|
||||
},
|
||||
submitQuote(key: string, comment: string) {
|
||||
@@ -294,12 +299,14 @@ export function createComposerSubmit(input: ComposerSubmitInput) {
|
||||
}
|
||||
}
|
||||
|
||||
function snapshot(input: ComposerSubmitInput, alternate: boolean): SubmitSnapshot {
|
||||
function snapshot(input: ComposerSubmitInput, alternate: boolean, first = false): SubmitSnapshot {
|
||||
const target = input.adapter.state
|
||||
const prompt = clonePrompt(target.current())
|
||||
const text = submissionText(prompt)
|
||||
const mode = input.mode()
|
||||
|
||||
const queueFirst = first && mode === "normal" && input.adapter.kind === "active-session" && input.adapter.working()
|
||||
|
||||
const context = target.context.items().map((item) => {
|
||||
if (item.type === "note") return { ...item, live: item.live ? { ...item.live } : undefined }
|
||||
|
||||
@@ -325,7 +332,8 @@ function snapshot(input: ComposerSubmitInput, alternate: boolean): SubmitSnapsho
|
||||
text,
|
||||
mode,
|
||||
alternate,
|
||||
delivery: input.delivery?.(alternate) ?? "steer",
|
||||
delivery: queueFirst ? "queue" : (input.delivery?.(alternate) ?? "steer"),
|
||||
first: queueFirst,
|
||||
selection: currentSelection(input),
|
||||
loading: input.adapter.controls().model.loading,
|
||||
retry: target.retry.current(),
|
||||
@@ -568,6 +576,8 @@ function dispatchPrompt(input: ComposerSubmitInput, session: ComposerSession, va
|
||||
|
||||
if (!admitted) return false
|
||||
|
||||
if (value.first) input.queueFirst?.(value.id)
|
||||
|
||||
// Move blocking work only once the steer is admitted, so the next step can read it from the inbox.
|
||||
void background?.move()
|
||||
|
||||
|
||||
@@ -645,6 +645,7 @@ export const dict = {
|
||||
"session.queue.count.one": "{{count}} queued",
|
||||
"session.queue.count.other": "{{count}} queued",
|
||||
"session.queue.steer": "Steer",
|
||||
"session.queue.first": "Queue first",
|
||||
"session.queue.send": "Send",
|
||||
"session.queue.steerTooltip": "Send without interrupting",
|
||||
"session.queue.remove": "Remove",
|
||||
|
||||
@@ -56,6 +56,56 @@ const rowIDs = (state: QueueState) => selectQueueRows(state).map((row) => row.id
|
||||
|
||||
const [one, two, three] = ["inb_1", "inb_2", "inb_3"].map((id) => item(id))
|
||||
|
||||
describe("queue first shares the serialized reorder", () => {
|
||||
test("a waiting request follows rewritten inbox IDs instead of resurrecting an original", () => {
|
||||
const result = replay(createQueueState([one, two, three]), [
|
||||
{ type: "reorder.requested", inboxIDs: [two.id, one.id, three.id], reverted: false },
|
||||
{ type: "first.requested", id: three.id, reverted: false },
|
||||
{ type: "mutation.moved", attempt: 1, moved: { [one.id]: "new_1", [two.id]: "new_2", [three.id]: "new_3" } },
|
||||
{ type: "inbox.synced", items: [item("new_2"), item("new_1"), item("new_3")] },
|
||||
{ type: "mutation.succeeded", attempt: 1 },
|
||||
])
|
||||
|
||||
expect(runs(result.effects)).toMatchObject([
|
||||
{ type: "reorder", inboxIDs: [two.id, one.id, three.id] },
|
||||
{ type: "reorder", inboxIDs: ["new_3", "new_2", "new_1"] },
|
||||
])
|
||||
expect(rowIDs(result.state)).toEqual(["new_3", "new_2", "new_1"])
|
||||
|
||||
const stale = transitionQueue(result.state, { type: "mutation.failed", attempt: 1, hidden: [] })
|
||||
|
||||
expect(stale.state).toBe(result.state)
|
||||
expect(stale.effects).toEqual([])
|
||||
})
|
||||
|
||||
test.each([one.id, "delivered"])("%s needs no reorder", (id) => {
|
||||
const state = createQueueState([one, two])
|
||||
|
||||
expect(transitionQueue(state, { type: "first.requested", id, reverted: false })).toEqual({ state, effects: [] })
|
||||
})
|
||||
|
||||
test.each(["mutation.succeeded", "mutation.failed", "mutation.timed-out", "dispose"] as const)(
|
||||
"%s settles a queue-first reorder through the existing lifecycle",
|
||||
(type) => {
|
||||
const started = transitionQueue(createQueueState([one, two]), {
|
||||
type: "first.requested",
|
||||
id: two.id,
|
||||
reverted: false,
|
||||
})
|
||||
|
||||
const result = transitionQueue(
|
||||
started.state,
|
||||
type === "dispose" ? { type } : { type, attempt: 1, hidden: [], stage: "running" },
|
||||
)
|
||||
|
||||
expect(result.state.status).toBe(type === "dispose" ? "disposed" : "idle")
|
||||
expect(
|
||||
result.effects.some((effect) => effect.type === "mutation.timer.clear" || effect.type === "timer.clearAll"),
|
||||
).toBe(true)
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
describe("an edit always returns the draft it replaced", () => {
|
||||
test("when the server delivers the prompt being edited", () => {
|
||||
const result = replay(createQueueState([one, two]), [
|
||||
|
||||
@@ -50,6 +50,7 @@ export type QueuedOperation =
|
||||
| EditOperation
|
||||
| { readonly type: "steer"; readonly id: string }
|
||||
| { readonly type: "remove"; readonly id: string }
|
||||
| { readonly type: "first"; readonly id: string }
|
||||
|
||||
// Operations that leave the composer free, so a row can be edited while one runs. `cancelling` means the server
|
||||
// admitted every replacement, so a later failure must not hand the edit back to the composer.
|
||||
@@ -140,6 +141,7 @@ export type QueueEvent =
|
||||
readonly ids: readonly string[]
|
||||
}
|
||||
| { readonly type: "reorder.requested"; readonly inboxIDs: readonly string[]; readonly reverted: boolean }
|
||||
| { readonly type: "first.requested"; readonly id: string; readonly reverted: boolean }
|
||||
| { readonly type: "steer.requested"; readonly id: string }
|
||||
| { readonly type: "remove.requested"; readonly id: string }
|
||||
| { readonly type: "undo.requested"; readonly id: string; readonly mode: "normal" | "shell" }
|
||||
@@ -232,6 +234,16 @@ export function transitionQueue(state: QueueState, event: QueueEvent): QueueTran
|
||||
|
||||
if (event.type === "reorder.requested") return reorder(state, event)
|
||||
|
||||
if (event.type === "first.requested") {
|
||||
if (event.reverted) return changed(state, [{ type: "toast", key: "session.queue.reverted" }])
|
||||
|
||||
const rows = selectQueueRows(state)
|
||||
|
||||
if (!rows.some((row) => row.id === event.id) || rows[0]?.id === event.id) return unchanged(state)
|
||||
|
||||
return enqueue(state, { type: "first", id: event.id })
|
||||
}
|
||||
|
||||
if (event.type === "steer.requested") return request(state, { type: "steer", id: event.id })
|
||||
|
||||
if (event.type === "remove.requested") return request(state, { type: "remove", id: event.id })
|
||||
@@ -683,14 +695,20 @@ function advance(state: Mutating, moved: Readonly<Record<string, string>>): Queu
|
||||
: { ...base, status: "idle" },
|
||||
clear,
|
||||
)
|
||||
const active = activate(head, state.seq + 1)
|
||||
const active = activate(head, state.seq + 1, state)
|
||||
const next = { ...base, seq: active.attempt, active, pending: rest }
|
||||
|
||||
return changed(
|
||||
state.status === "editing-while-mutating"
|
||||
? { ...next, status: "editing-while-mutating", edit: state.edit }
|
||||
state.status === "editing-while-mutating" && isConcurrent(active)
|
||||
? { ...next, status: "editing-while-mutating", edit: state.edit, active }
|
||||
: { ...next, status: "mutating" },
|
||||
[...clear, ...start(active)],
|
||||
[
|
||||
...clear,
|
||||
...(state.status === "editing-while-mutating" && !isConcurrent(active)
|
||||
? [restore(state.edit, true, state.edit.item)]
|
||||
: []),
|
||||
...start(active),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
@@ -707,16 +725,37 @@ function bounded(operation: ActiveOperation) {
|
||||
function enqueue(state: LiveQueueState, operation: QueuedOperation, effects: QueueEffect[] = []): QueueTransition {
|
||||
if (state.status === "mutating" || state.status === "editing-while-mutating")
|
||||
return changed({ ...state, pending: [...state.pending, operation] }, effects)
|
||||
const active = activate(operation, state.seq + 1)
|
||||
const active = activate(operation, state.seq + 1, state)
|
||||
const base = { seq: active.attempt, items: state.items, gone: state.gone, moved: state.moved, active, pending: [] }
|
||||
|
||||
if (state.status === "editing")
|
||||
return changed({ ...base, status: "editing-while-mutating", edit: state.edit }, [...effects, ...start(active)])
|
||||
if (state.status === "editing" && isConcurrent(active))
|
||||
return changed({ ...base, status: "editing-while-mutating", edit: state.edit, active }, [
|
||||
...effects,
|
||||
...start(active),
|
||||
])
|
||||
|
||||
return changed({ ...base, status: "mutating" }, [...effects, ...start(active)])
|
||||
return changed({ ...base, status: "mutating" }, [
|
||||
...effects,
|
||||
...(state.status === "editing" ? [restore(state.edit, true, state.edit.item)] : []),
|
||||
...start(active),
|
||||
])
|
||||
}
|
||||
|
||||
function activate(operation: QueuedOperation, attempt: number): ConcurrentOperation {
|
||||
function activate(operation: QueuedOperation, attempt: number, state: LiveQueueState): ActiveOperation {
|
||||
if (operation.type === "first") {
|
||||
const current = queuedPromptRows(
|
||||
state.items,
|
||||
[],
|
||||
state.gone.map((entry) => entry.id),
|
||||
)
|
||||
|
||||
const first = current.find((row) => row.id === operation.id)
|
||||
|
||||
const rows = first ? [first, ...current.filter((row) => row !== first)] : current
|
||||
|
||||
return { type: "reorder", attempt, inboxIDs: rows.map((row) => row.id), rows, moved: {} }
|
||||
}
|
||||
|
||||
if (operation.type === "edit") return { ...operation, attempt, phase: "admitting" }
|
||||
|
||||
return { ...operation, attempt }
|
||||
|
||||
@@ -476,6 +476,7 @@ export function createSessionQueue(input: {
|
||||
undo: (id: string) => dispatch({ type: "undo.requested", id, mode: input.draft.mode.current() }),
|
||||
edit,
|
||||
reorder: (inboxIDs: string[]) => dispatch({ type: "reorder.requested", inboxIDs, reverted: reverted() }),
|
||||
first: (id: string) => dispatch({ type: "first.requested", id, reverted: reverted() }),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user