Skip to content

Commit 9c6a10a

Browse files
committed
feat(composer): reorder queued messages on web and mobile
- Add queued-run.reorder with beforeRunId placement helper - Web: up/down controls on queued rows; mobile: Move up/down in long-press menu - Document queue reordering in composer user docs
1 parent 575579d commit 9c6a10a

11 files changed

Lines changed: 326 additions & 8 deletions

File tree

‎apps/mobile/src/features/threads/ThreadQueueControl.tsx‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
queuedRunEditBlockedReason,
66
} from "@t3tools/client-runtime/queued-run-edit";
77
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
8+
import { beforeRunIdForQueuedMove } from "@t3tools/client-runtime/state/thread-workflows";
89
import { resolveUserMessagePresentation } from "@t3tools/client-runtime/user-message";
910
import type { ChatAttachment, EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";
1011
import { Atom } from "effect/reactivity";
@@ -40,6 +41,7 @@ import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
4041
import {
4142
REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL,
4243
buildCancelQueuedRunCommand,
44+
buildReorderQueuedRunCommand,
4345
resolveThreadQueueRowControls,
4446
} from "./threadQueueControlPresentation";
4547

@@ -51,7 +53,7 @@ const EMPTY_QUEUED_COUNT_ATOM = Atom.make(0).pipe(
5153
);
5254

5355
type QueueTarget = { readonly environmentId: EnvironmentId; readonly threadId: ThreadId };
54-
type QueueAction = "steer" | "edit" | "remove";
56+
type QueueAction = "steer" | "edit" | "remove" | "move-up" | "move-down";
5557

5658
function useThreadQueueWorkflow(target: QueueTarget) {
5759
return useAtomValue(environmentThreadDetails.queueWorkflowAtom(target));
@@ -134,6 +136,7 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {
134136
});
135137
const promote = useAtomCommand(threadEnvironment.promoteQueuedRun, "promote queued message");
136138
const cancel = useAtomCommand(threadEnvironment.cancelQueuedRun, "remove queued message");
139+
const reorder = useAtomCommand(threadEnvironment.reorderQueuedRun, "reorder queued message");
137140
const resume = useAtomCommand(threadEnvironment.resumeThreadQueue, "resume queue");
138141
const [resuming, setResuming] = useState(false);
139142
const [busyRunId, setBusyRunId] = useState<RunId | null>(null);
@@ -179,12 +182,34 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {
179182
navigation.goBack();
180183
return;
181184
}
182-
if (action !== "steer" && action !== "remove") return;
185+
if (
186+
action !== "steer" &&
187+
action !== "remove" &&
188+
action !== "move-up" &&
189+
action !== "move-down"
190+
) {
191+
return;
192+
}
183193
busyRef.current = true;
184194
setBusyRunId(runId);
185195
void Haptics.selectionAsync();
186196
if (action === "remove") {
187197
await cancel(buildCancelQueuedRunCommand({ ...target, runId }));
198+
} else if (action === "move-up" || action === "move-down") {
199+
const toIndex = action === "move-up" ? index - 1 : index + 1;
200+
if (toIndex >= 0 && toIndex < queuedRuns.length) {
201+
await reorder(
202+
buildReorderQueuedRunCommand({
203+
...target,
204+
runId,
205+
beforeRunId: beforeRunIdForQueuedMove({
206+
orderedRunIds: queuedRuns.map(({ run }) => run.id),
207+
runId,
208+
toIndex,
209+
}),
210+
}),
211+
);
212+
}
188213
} else if (workflow?.canSendNow) {
189214
await resume({ ...target, input: { threadId: target.threadId, runId } });
190215
} else if (workflow?.activeRun && workflow.canPromoteToSteer) {
@@ -241,8 +266,10 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {
241266
canPromoteToSteer: workflow?.canPromoteToSteer ?? false,
242267
canSendNow: workflow?.canSendNow ?? false,
243268
editBlocked: editBlockedReason !== null,
269+
index,
244270
isEditing: editing?.runId === run.id,
245271
isGuestMessage: context?.guest !== undefined,
272+
queueLength: queuedRuns.length,
246273
text,
247274
});
248275
const title =
@@ -270,6 +297,22 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {
270297
},
271298
]
272299
: []),
300+
...(queuedRuns.length > 1
301+
? [
302+
{
303+
id: "move-up",
304+
title: "Move up",
305+
attributes: { disabled: !controls.canMoveUp },
306+
image: Platform.OS === "ios" ? "arrow.up" : "arrow_upward",
307+
},
308+
{
309+
id: "move-down",
310+
title: "Move down",
311+
attributes: { disabled: !controls.canMoveDown },
312+
image: Platform.OS === "ios" ? "arrow.down" : "arrow_downward",
313+
},
314+
]
315+
: []),
273316
{
274317
id: "edit",
275318
title: "Edit",

‎apps/mobile/src/features/threads/threadQueueControlPresentation.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,50 @@ describe("threadQueueControlPresentation", () => {
7474
expect(guest.canEdit).toBe(false);
7575
expect(guest.canDismiss).toBe(true);
7676
});
77+
78+
it("gates moving within a multi-message queue", () => {
79+
const middle = resolveThreadQueueRowControls({
80+
busy: false,
81+
canPromoteToSteer: false,
82+
canSendNow: false,
83+
index: 1,
84+
queueLength: 3,
85+
text: "Middle",
86+
});
87+
expect(middle.canMoveUp).toBe(true);
88+
expect(middle.canMoveDown).toBe(true);
89+
90+
const first = resolveThreadQueueRowControls({
91+
busy: false,
92+
canPromoteToSteer: false,
93+
canSendNow: false,
94+
index: 0,
95+
queueLength: 2,
96+
text: "First",
97+
});
98+
expect(first.canMoveUp).toBe(false);
99+
expect(first.canMoveDown).toBe(true);
100+
101+
const alone = resolveThreadQueueRowControls({
102+
busy: false,
103+
canPromoteToSteer: false,
104+
canSendNow: false,
105+
index: 0,
106+
queueLength: 1,
107+
text: "Only",
108+
});
109+
expect(alone.canMoveUp).toBe(false);
110+
expect(alone.canMoveDown).toBe(false);
111+
112+
const busy = resolveThreadQueueRowControls({
113+
busy: true,
114+
canPromoteToSteer: false,
115+
canSendNow: false,
116+
index: 1,
117+
queueLength: 3,
118+
text: "Busy",
119+
});
120+
expect(busy.canMoveUp).toBe(false);
121+
expect(busy.canMoveDown).toBe(false);
122+
});
77123
});

‎apps/mobile/src/features/threads/threadQueueControlPresentation.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export const REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL = "Remove queued message"
77
export interface ThreadQueueRowControls {
88
readonly canDismiss: boolean;
99
readonly canEdit: boolean;
10+
readonly canMoveDown: boolean;
11+
readonly canMoveUp: boolean;
1012
readonly canSend: boolean;
1113
readonly dismissAccessibilityLabel: string;
1214
readonly displayText: string;
@@ -19,20 +21,27 @@ export function resolveThreadQueueRowControls(input: {
1921
readonly canSendNow: boolean;
2022
/** Something else holds the composer, such as an open question. */
2123
readonly editBlocked?: boolean;
24+
readonly index?: number;
2225
readonly isEditing?: boolean;
2326
/** A guest's message keeps the guest's words; the owner can cancel it, not rewrite it. */
2427
readonly isGuestMessage?: boolean;
28+
readonly queueLength?: number;
2529
readonly text: string;
2630
}): ThreadQueueRowControls {
2731
const mutationEnabled = !input.busy;
2832
const isEditing = input.isEditing === true;
33+
const index = input.index ?? 0;
34+
const queueLength = input.queueLength ?? 0;
35+
const canReorder = mutationEnabled && !isEditing && queueLength > 1;
2936

3037
return {
3138
canDismiss: !input.busy,
3239
// Re-opening the row already in the composer would reload it and throw
3340
// away whatever has been typed since.
3441
canEdit:
3542
mutationEnabled && !isEditing && input.isGuestMessage !== true && input.editBlocked !== true,
43+
canMoveUp: canReorder && index > 0,
44+
canMoveDown: canReorder && index < queueLength - 1,
3645
canSend: mutationEnabled && (input.canPromoteToSteer || input.canSendNow) && !isEditing,
3746
dismissAccessibilityLabel: REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL,
3847
// Editing and sending keep the original text so quoted citations stay linked.
@@ -62,3 +71,26 @@ export function buildCancelQueuedRunCommand(input: {
6271
},
6372
};
6473
}
74+
75+
export function buildReorderQueuedRunCommand(input: {
76+
readonly beforeRunId: RunId | null;
77+
readonly environmentId: EnvironmentId;
78+
readonly runId: RunId;
79+
readonly threadId: ThreadId;
80+
}): {
81+
readonly environmentId: EnvironmentId;
82+
readonly input: {
83+
readonly beforeRunId: RunId | null;
84+
readonly runId: RunId;
85+
readonly threadId: ThreadId;
86+
};
87+
} {
88+
return {
89+
environmentId: input.environmentId,
90+
input: {
91+
beforeRunId: input.beforeRunId,
92+
runId: input.runId,
93+
threadId: input.threadId,
94+
},
95+
};
96+
}

‎apps/web/src/components/chat/QueuedRunsControl.browser.test.tsx‎

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,21 @@ vi.mock(import("../../state/query"), async (importOriginal) => ({
2929
})) as never,
3030
}));
3131

32-
vi.mock(import("../../state/use-atom-command"), async (importOriginal) => ({
33-
...(await importOriginal()),
34-
useAtomCommand: (() => async () => undefined) as never,
32+
const commandMocks = vi.hoisted(() => ({
33+
reorderQueuedRun: vi.fn(async () => undefined),
3534
}));
3635

36+
vi.mock(import("../../state/use-atom-command"), async (importOriginal) => {
37+
const { threadEnvironment } = await import("../../state/threads");
38+
return {
39+
...(await importOriginal()),
40+
useAtomCommand: ((command: unknown) =>
41+
command === threadEnvironment.reorderQueuedRun
42+
? commandMocks.reorderQueuedRun
43+
: async () => undefined) as never,
44+
};
45+
});
46+
3747
vi.mock(import("../../assets/assetUrls"), async (importOriginal) => ({
3848
...(await importOriginal()),
3949
useAssetUrls: ((_environmentId: never, resources: ReadonlyArray<{ attachmentId: string }>) =>
@@ -174,6 +184,39 @@ it("shows who sent a queued message and folds automation and agent prompts", asy
174184
await expect.element(page.getByText("Continue where you left off.")).toBeVisible();
175185
});
176186

187+
it("moves a queued message later with beforeRunId null at the end", async () => {
188+
setup();
189+
commandMocks.reorderQueuedRun.mockClear();
190+
state.projection = { projection: { messages: [] } };
191+
state.workflow = {
192+
activeRun: { id: "run:active" },
193+
canPromoteToSteer: true,
194+
queuedRuns: [
195+
{
196+
run: { id: "run:first", userMessageId: "message:first" },
197+
text: "First",
198+
attachments: [],
199+
},
200+
{
201+
run: { id: "run:second", userMessageId: "message:second" },
202+
text: "Second",
203+
attachments: [],
204+
},
205+
],
206+
};
207+
await using _screen = await renderQueue();
208+
209+
await page.getByRole("button", { name: "Move queued message down" }).first().click();
210+
expect(commandMocks.reorderQueuedRun).toHaveBeenCalledExactlyOnceWith({
211+
environmentId: baseProps.environmentId,
212+
input: {
213+
threadId: baseProps.threadId,
214+
runId: "run:first",
215+
beforeRunId: null,
216+
},
217+
});
218+
});
219+
177220
it("stays inside the timeline scroller instead of growing the chat column", async () => {
178221
setup();
179222
state.projection = { projection: { messages: [] } };

‎apps/web/src/components/chat/QueuedRunsControl.tsx‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2-
import { deriveThreadQueueWorkflowState } from "@t3tools/client-runtime/state/thread-workflows";
2+
import {
3+
beforeRunIdForQueuedMove,
4+
deriveThreadQueueWorkflowState,
5+
} from "@t3tools/client-runtime/state/thread-workflows";
36
import { assistantCitationsToPlainText } from "@t3tools/shared/assistantCitations";
47
import {
58
isAttachmentOnlyBootstrapPrompt,
@@ -18,6 +21,7 @@ import {
1821
ArrowUpIcon,
1922
CalendarClockIcon,
2023
ChevronDownIcon,
24+
ChevronUpIcon,
2125
Clock3Icon,
2226
MessagesSquareIcon,
2327
PencilIcon,
@@ -99,6 +103,7 @@ export function QueuedRunsControl({
99103
const promote = useAtomCommand(threadEnvironment.promoteQueuedRun);
100104
const resume = useAtomCommand(threadEnvironment.resumeThreadQueue);
101105
const cancel = useAtomCommand(threadEnvironment.cancelQueuedRun);
106+
const reorder = useAtomCommand(threadEnvironment.reorderQueuedRun);
102107
const [showAll, setShowAll] = useState(false);
103108
const queueListId = useId();
104109
const [busyRunId, setBusyRunId] = useState<RunId | null>(null);
@@ -225,8 +230,23 @@ export function QueuedRunsControl({
225230
setBusyRunId(null);
226231
};
227232

233+
const move = async (runId: RunId, toIndex: number) => {
234+
const orderedRunIds = queued.map(({ run }) => run.id);
235+
setBusyRunId(runId);
236+
await reorder({
237+
environmentId: props.environmentId,
238+
input: {
239+
threadId: props.threadId,
240+
runId,
241+
beforeRunId: beforeRunIdForQueuedMove({ orderedRunIds, runId, toIndex }),
242+
},
243+
});
244+
setBusyRunId(null);
245+
};
246+
228247
const collapsible = items.length > QUEUE_VISIBLE_ROWS;
229248
const visibleItems = collapsible && !showAll ? items.slice(0, QUEUE_VISIBLE_ROWS - 1) : items;
249+
const canReorder = queued.length > 1;
230250

231251
return (
232252
<section
@@ -310,6 +330,46 @@ export function QueuedRunsControl({
310330
</>
311331
) : (
312332
<>
333+
{canReorder && item.serverIndex !== null ? (
334+
<>
335+
<Tooltip>
336+
<TooltipTrigger render={<span className="flex shrink-0" />}>
337+
<Button
338+
size="icon-xs"
339+
variant="ghost-muted"
340+
aria-label="Move queued message up"
341+
disabled={item.serverIndex === 0 || busyRunId !== null}
342+
onClick={() => {
343+
if (item.runId !== null && item.serverIndex !== null) {
344+
void move(item.runId, item.serverIndex - 1);
345+
}
346+
}}
347+
>
348+
<ChevronUpIcon />
349+
</Button>
350+
</TooltipTrigger>
351+
<TooltipPopup>Move earlier in the queue</TooltipPopup>
352+
</Tooltip>
353+
<Tooltip>
354+
<TooltipTrigger render={<span className="flex shrink-0" />}>
355+
<Button
356+
size="icon-xs"
357+
variant="ghost-muted"
358+
aria-label="Move queued message down"
359+
disabled={item.serverIndex >= queued.length - 1 || busyRunId !== null}
360+
onClick={() => {
361+
if (item.runId !== null && item.serverIndex !== null) {
362+
void move(item.runId, item.serverIndex + 1);
363+
}
364+
}}
365+
>
366+
<ChevronDownIcon />
367+
</Button>
368+
</TooltipTrigger>
369+
<TooltipPopup>Move later in the queue</TooltipPopup>
370+
</Tooltip>
371+
</>
372+
) : null}
313373
<Tooltip>
314374
{/* The span keeps the tooltip, and its reason, on a disabled button. */}
315375
<TooltipTrigger render={<span className="flex shrink-0" />}>

‎docs/user/composer.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ shortcut described above.
208208
On web and desktop, queued messages appear at the end of the thread as dashed messages that have
209209
not been sent yet. On mobile they appear above the composer. Each shows its text and any attached
210210
images. **Send now** sends a message right away, into the running turn if there is one. You can
211-
also edit or remove it.
211+
also edit or remove it. With more than one queued message, move one earlier or later with the
212+
up/down controls on web and desktop, or **Move up** / **Move down** in the long-press menu on
213+
mobile.
212214

213215
If you stop the agent or the server restarts, queued messages keep their order and are held.
214216
Press **Resume** in an empty composer on web and desktop, or **Resume queue** on mobile, to

0 commit comments

Comments
 (0)