Open History to see the last commit
1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { deriveThreadQueueWorkflowState } from "@t3tools/client-runtime/state/thread-workflows";
3import type { EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";
4import {
5 ArrowDownIcon,
6 ArrowUpIcon,
7 CheckIcon,
8 Clock3Icon,
9 CornerUpRightIcon,
10 ListOrderedIcon,
11 PencilIcon,
12 XIcon,
13} from "lucide-react";
14import { useMemo, useState } from "react";
15
16import { threadEnvironment } from "../../state/threads";
17import { useThreadProjection } from "../../state/entities";
18import { useAtomCommand } from "../../state/use-atom-command";
19import type { ChatMessage } from "../../types";
20import { Button } from "../ui/button";
21
22export function QueuedRunsControl(props: {
23 readonly environmentId: EnvironmentId;
24 readonly threadId: ThreadId;
25 readonly optimisticMessages: ReadonlyArray<Pick<ChatMessage, "id" | "inputIntent" | "text">>;
26}) {
27 const projection = useThreadProjection(
28 scopeThreadRef(props.environmentId, props.threadId),
29 )?.projection;
30 const reorder = useAtomCommand(threadEnvironment.reorderQueuedRun);
31 const promote = useAtomCommand(threadEnvironment.promoteQueuedRun);
32 const cancel = useAtomCommand(threadEnvironment.cancelQueuedRun);
33 const edit = useAtomCommand(threadEnvironment.editQueuedRun);
34 const [busyRunId, setBusyRunId] = useState<RunId | null>(null);
35 const [editing, setEditing] = useState<{ runId: RunId; draft: string } | null>(null);
36 const workflow = useMemo(
37 () => (projection ? deriveThreadQueueWorkflowState(projection) : null),
38 [projection],
39 );
40 const queued = workflow?.queuedRuns ?? [];
41 const activeRun = workflow?.activeRun ?? null;
42 const committedQueuedMessageIds = new Set(queued.map(({ run }) => run.userMessageId));
43 const optimisticQueued = props.optimisticMessages.filter(
44 (message) =>
45 message.inputIntent === "queued_turn" && !committedQueuedMessageIds.has(message.id),
46 );
47 const items = [
48 ...queued.map(({ run, text }, serverIndex) => ({
49 key: run.id,
50 runId: run.id,
51 serverIndex,
52 text,
53 pending: false,
54 })),
55 ...optimisticQueued.map((message) => ({
56 key: message.id,
57 runId: null,
58 serverIndex: null,
59 text: message.text,
60 pending: true,
61 })),
62 ];
63
64 if (items.length === 0) return null;
65
66 const move = async (runId: RunId, beforeRunId: RunId | null) => {
67 setBusyRunId(runId);
68 try {
69 await reorder({
70 environmentId: props.environmentId,
71 input: { threadId: props.threadId, runId, beforeRunId },
72 });
73 } finally {
74 setBusyRunId(null);
75 }
76 };
77
78 const steer = async (queuedRunId: RunId) => {
79 if (activeRun === null) return;
80 setBusyRunId(queuedRunId);
81 try {
82 await promote({
83 environmentId: props.environmentId,
84 input: { threadId: props.threadId, queuedRunId, targetRunId: activeRun.id },
85 });
86 } finally {
87 setBusyRunId(null);
88 }
89 };
90
91 const remove = async (runId: RunId) => {
92 setBusyRunId(runId);
93 try {
94 await cancel({
95 environmentId: props.environmentId,
96 input: { threadId: props.threadId, runId },
97 });
98 } finally {
99 setBusyRunId(null);
100 }
101 };
102
103 const saveEdit = async (runId: RunId, originalText: string) => {
104 if (editing === null || editing.runId !== runId) return;
105 const text = editing.draft.trim();
106 if (text.length === 0) return;
107 if (text === originalText) {
108 setEditing(null);
109 return;
110 }
111 setBusyRunId(runId);
112 try {
113 await edit({
114 environmentId: props.environmentId,
115 input: { threadId: props.threadId, runId, text },
116 });
117 setEditing(null);
118 } finally {
119 setBusyRunId(null);
120 }
121 };
122
123 return (
124 <section
125 aria-label={`${items.length} queued message${items.length === 1 ? "" : "s"}`}
126 aria-live="polite"
127 className="chat-composer-queue-strip relative z-0 -mb-4 mx-auto w-[calc(100%-2.75rem)] max-w-[calc(48rem-2.75rem)] px-2 pt-1.5 pb-5"
128 >
129 <header className="flex h-6 items-center gap-1.5 px-1.5 text-[11px] font-medium text-muted-foreground">
130 <ListOrderedIcon className="size-3.5" />
131 <span>Queued</span>
132 <span className="rounded-full bg-muted/70 px-1.5 text-[10px] tabular-nums">
133 {items.length}
134 </span>
135 </header>
136 <ol className="max-h-32 overflow-y-auto px-1">
137 {items.map((item, index) => {
138 const rowEditing =
139 item.runId !== null && editing !== null && editing.runId === item.runId
140 ? editing
141 : null;
142 return (
143 <li
144 key={item.key}
145 className="flex min-w-0 items-center gap-1 border-border/45 border-t py-1 first:border-t-0"
146 >
147 <span className="w-4 shrink-0 text-center text-[10px] tabular-nums text-muted-foreground/65">
148 {index + 1}
149 </span>
150 {item.pending ? (
151 <Clock3Icon
152 aria-label="Saving queued message"
153 className="size-3 shrink-0 text-muted-foreground/60"
154 />
155 ) : null}
156 {rowEditing !== null ? (
157 <>
158 <input
159 aria-label="Edit queued message"
160 autoFocus
161 className="min-w-0 flex-1 rounded-sm border border-border/60 bg-transparent px-1.5 py-0.5 text-xs outline-none focus:border-border"
162 value={rowEditing.draft}
163 onChange={(event) =>
164 setEditing((current) =>
165 current === null ? current : { ...current, draft: event.target.value },
166 )
167 }
168 onKeyDown={(event) => {
169 if (event.key === "Enter") {
170 event.preventDefault();
171 if (item.runId !== null) void saveEdit(item.runId, item.text);
172 }
173 if (event.key === "Escape") {
174 event.preventDefault();
175 setEditing(null);
176 }
177 }}
178 />
179 <Button
180 size="icon-xs"
181 variant="ghost"
182 aria-label="Save queued message"
183 className="size-6 text-muted-foreground"
184 disabled={busyRunId !== null || rowEditing.draft.trim().length === 0}
185 onClick={() => {
186 if (item.runId !== null) void saveEdit(item.runId, item.text);
187 }}
188 >
189 <CheckIcon className="size-3" />
190 </Button>
191 <Button
192 size="icon-xs"
193 variant="ghost"
194 aria-label="Cancel editing queued message"
195 className="size-6 text-muted-foreground"
196 disabled={busyRunId !== null}
197 onClick={() => setEditing(null)}
198 >
199 <XIcon className="size-3" />
200 </Button>
201 </>
202 ) : (
203 <>
204 <span className="min-w-0 flex-1 truncate text-xs" title={item.text}>
205 {item.text}
206 </span>
207 <Button
208 size="icon-xs"
209 variant="ghost"
210 aria-label="Edit queued message"
211 className="size-6 text-muted-foreground"
212 disabled={item.runId === null || busyRunId !== null}
213 onClick={() => {
214 if (item.runId !== null) setEditing({ runId: item.runId, draft: item.text });
215 }}
216 >
217 <PencilIcon className="size-3" />
218 </Button>
219 <Button
220 size="icon-xs"
221 variant="ghost"
222 aria-label="Move queued message up"
223 className="size-6 text-muted-foreground"
224 disabled={
225 item.runId === null ||
226 item.serverIndex === null ||
227 busyRunId !== null ||
228 !workflow?.canReorder ||
229 item.serverIndex === 0
230 }
231 onClick={() => {
232 if (item.runId === null || item.serverIndex === null) return;
233 void move(item.runId, queued[item.serverIndex - 1]?.run.id ?? null);
234 }}
235 >
236 <ArrowUpIcon className="size-3" />
237 </Button>
238 <Button
239 size="icon-xs"
240 variant="ghost"
241 aria-label="Move queued message down"
242 className="size-6 text-muted-foreground"
243 disabled={
244 item.runId === null ||
245 item.serverIndex === null ||
246 busyRunId !== null ||
247 !workflow?.canReorder ||
248 item.serverIndex === queued.length - 1
249 }
250 onClick={() => {
251 if (item.runId === null || item.serverIndex === null) return;
252 void move(item.runId, queued[item.serverIndex + 2]?.run.id ?? null);
253 }}
254 >
255 <ArrowDownIcon className="size-3" />
256 </Button>
257 <Button
258 size="xs"
259 variant="ghost"
260 className="h-6 gap-1 px-1.5 text-[11px] text-muted-foreground"
261 disabled={
262 item.runId === null || busyRunId !== null || !workflow?.canPromoteToSteer
263 }
264 title={
265 activeRun === null
266 ? "There is no active run to steer"
267 : "Send as a steer instead"
268 }
269 onClick={() => {
270 if (item.runId !== null) void steer(item.runId);
271 }}
272 >
273 <CornerUpRightIcon className="size-3" />
274 Steer
275 </Button>
276 <Button
277 size="icon-xs"
278 variant="ghost"
279 aria-label="Remove queued message"
280 className="size-6 text-muted-foreground"
281 disabled={item.runId === null || busyRunId !== null}
282 title="Remove from queue"
283 onClick={() => {
284 if (item.runId !== null) void remove(item.runId);
285 }}
286 >
287 <XIcon className="size-3" />
288 </Button>
289 </>
290 )}
291 </li>
292 );
293 })}
294 </ol>
295 </section>
296 );
297}