Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

QueuedRunsControl.tsx

97 lines3.9 KB

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 { ArrowDownIcon, ArrowUpIcon, CornerUpRightIcon, ListOrderedIcon } from "lucide-react";
5import { useMemo, useState } from "react";
6​
7import { threadEnvironment } from "../../state/threads";
8import { useThreadProjection } from "../../state/entities";
9import { useAtomCommand } from "../../state/use-atom-command";
10import { Button } from "../ui/button";
11​
12export function QueuedRunsControl(props: {
13 readonly environmentId: EnvironmentId;
14 readonly threadId: ThreadId;
15}) {
16 const projection = useThreadProjection(
17 scopeThreadRef(props.environmentId, props.threadId),
18 )?.projection;
19 const reorder = useAtomCommand(threadEnvironment.reorderQueuedRun);
20 const promote = useAtomCommand(threadEnvironment.promoteQueuedRun);
21 const [busyRunId, setBusyRunId] = useState<RunId | null>(null);
22 const workflow = useMemo(
23 () => (projection ? deriveThreadQueueWorkflowState(projection) : null),
24 [projection],
25 );
26 const queued = workflow?.queuedRuns ?? [];
27 const activeRun = workflow?.activeRun ?? null;
28​
29 if (queued.length === 0) return null;
30​
31 const move = async (runId: RunId, beforeRunId: RunId | null) => {
32 setBusyRunId(runId);
33 await reorder({
34 environmentId: props.environmentId,
35 input: { threadId: props.threadId, runId, beforeRunId },
36 });
37 setBusyRunId(null);
38 };
39​
40 const steer = async (queuedRunId: RunId) => {
41 if (activeRun === null) return;
42 setBusyRunId(queuedRunId);
43 await promote({
44 environmentId: props.environmentId,
45 input: { threadId: props.threadId, queuedRunId, targetRunId: activeRun.id },
46 });
47 setBusyRunId(null);
48 };
49​
50 return (
51 <section className="mx-auto mb-2 max-w-208 rounded-xl border border-border bg-background/96 shadow-xs">
52 <header className="flex items-center gap-2 border-b border-border px-3 py-2 text-xs font-medium">
53 <ListOrderedIcon className="size-3.5 text-muted-foreground" />
54 Queue
55 <span className="ml-auto tabular-nums text-muted-foreground">{queued.length}</span>
56 </header>
57 <ol className="max-h-40 overflow-y-auto p-1.5">
58 {queued.map(({ run, text }, index) => (
59 <li key={run.id} className="flex min-w-0 items-center gap-2 rounded-lg px-2 py-1.5">
60 <span className="w-5 shrink-0 text-right text-[10px] tabular-nums text-muted-foreground">
61 {index + 1}
62 </span>
63 <span className="min-w-0 flex-1 truncate text-xs">{text}</span>
64 <Button
65 size="icon-xs"
66 variant="ghost"
67 aria-label="Move queued message up"
68 disabled={busyRunId !== null || !workflow?.canReorder || index === 0}
69 onClick={() => void move(run.id, queued[index - 1]?.run.id ?? null)}
70 >
71 <ArrowUpIcon className="size-3" />
72 </Button>
73 <Button
74 size="icon-xs"
75 variant="ghost"
76 aria-label="Move queued message down"
77 disabled={busyRunId !== null || !workflow?.canReorder || index === queued.length - 1}
78 onClick={() => void move(run.id, queued[index + 2]?.run.id ?? null)}
79 >
80 <ArrowDownIcon className="size-3" />
81 </Button>
82 <Button
83 size="xs"
84 variant="outline"
85 disabled={busyRunId !== null || !workflow?.canPromoteToSteer}
86 title={activeRun === null ? "There is no active run to steer" : "Promote to steer"}
87 onClick={() => void steer(run.id)}
88 >
89 <CornerUpRightIcon className="size-3" />
90 Steer
91 </Button>
92 </li>
93 ))}
94 </ol>
95 </section>
96 );