+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
497 lines
20 KiB
TypeScript
497 lines
20 KiB
TypeScript
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
|
|
import { deriveThreadQueueWorkflowState } from "@t3tools/client-runtime/state/thread-workflows";
|
|
import { replaceComposerContextReferences } from "@t3tools/shared/composerContextReferences";
|
|
import type {
|
|
ChatAttachment as ContractChatAttachment,
|
|
EnvironmentId,
|
|
MessageId,
|
|
RunId,
|
|
ThreadId,
|
|
} from "@t3tools/contracts";
|
|
import {
|
|
Clock3Icon,
|
|
CornerUpRightIcon,
|
|
GripVerticalIcon,
|
|
ListOrderedIcon,
|
|
PencilIcon,
|
|
} from "lucide-react";
|
|
import { useId, useImperativeHandle, useMemo, useRef, useState, type Ref } from "react";
|
|
|
|
import { useAssetUrls } from "../../assets/assetUrls";
|
|
import { threadEnvironment } from "../../state/threads";
|
|
import { useThreadProjection } from "../../state/entities";
|
|
import { useAtomCommand } from "../../state/use-atom-command";
|
|
import { isImageAttachment, type ChatMessage } from "../../types";
|
|
import { cn } from "~/lib/utils";
|
|
import { ComposerBanner } from "./ComposerBanner";
|
|
import { Button } from "../ui/button";
|
|
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
|
|
|
|
export interface EditQueuedRunRequest {
|
|
readonly runId: RunId;
|
|
readonly messageId: MessageId;
|
|
readonly text: string;
|
|
readonly attachments: ReadonlyArray<ContractChatAttachment>;
|
|
}
|
|
|
|
interface QueuedRowThumbnail {
|
|
readonly key: string;
|
|
readonly name: string;
|
|
readonly url: string | null;
|
|
}
|
|
|
|
const QUEUED_RUN_DRAG_TYPE = "application/x-t3code-queued-run";
|
|
|
|
export interface QueuedRunsControlHandle {
|
|
steerNext: (repeat: boolean) => boolean;
|
|
editLatest: (repeat: boolean) => boolean;
|
|
}
|
|
|
|
export function QueuedRunsControl({
|
|
ref,
|
|
...props
|
|
}: {
|
|
readonly ref?: Ref<QueuedRunsControlHandle>;
|
|
readonly steerShortcutLabel?: string | null;
|
|
readonly editShortcutLabel?: string | null;
|
|
readonly environmentId: EnvironmentId;
|
|
readonly threadId: ThreadId;
|
|
readonly optimisticMessages: ReadonlyArray<
|
|
Pick<ChatMessage, "id" | "inputIntent" | "text" | "attachments">
|
|
>;
|
|
/** The saved queue entry stays visible while its draft is edited in the composer. */
|
|
readonly editingRunId: RunId | null;
|
|
readonly onEditQueuedRun: (request: EditQueuedRunRequest) => void;
|
|
readonly onCancelEdit: () => void;
|
|
}) {
|
|
const projection = useThreadProjection(
|
|
scopeThreadRef(props.environmentId, props.threadId),
|
|
)?.projection;
|
|
const reorder = useAtomCommand(threadEnvironment.reorderQueuedRun);
|
|
const promote = useAtomCommand(threadEnvironment.promoteQueuedRun);
|
|
const cancel = useAtomCommand(threadEnvironment.cancelQueuedRun);
|
|
const [expanded, setExpanded] = useState(true);
|
|
const queueListId = useId();
|
|
const [busyRunId, setBusyRunId] = useState<RunId | null>(null);
|
|
// Live drag-reorder state: the dragged run and the queue index the row
|
|
// would be inserted at (0..queued.length) as of the latest dragover.
|
|
const [dragState, setDragState] = useState<{
|
|
readonly runId: RunId;
|
|
readonly insertIndex: number | null;
|
|
} | null>(null);
|
|
// Drags must start from the grip, not from an accidental text-drag on the
|
|
// row. The grip arms its run id on pointerdown; dragstart verifies it.
|
|
const dragArmedRunIdRef = useRef<RunId | null>(null);
|
|
const workflow = useMemo(
|
|
() => (projection ? deriveThreadQueueWorkflowState(projection) : null),
|
|
[projection],
|
|
);
|
|
const queued = workflow?.queuedRuns ?? [];
|
|
const activeRun = workflow?.activeRun ?? null;
|
|
const canReorder = workflow?.canReorder === true;
|
|
const queuedImageAttachmentIds = useMemo(() => {
|
|
const ids: string[] = [];
|
|
for (const { attachments } of workflow?.queuedRuns ?? []) {
|
|
for (const attachment of attachments) {
|
|
if (attachment.type === "image") ids.push(attachment.id);
|
|
}
|
|
}
|
|
return ids;
|
|
}, [workflow]);
|
|
const queuedImageAttachmentResources = useMemo(
|
|
() =>
|
|
queuedImageAttachmentIds.map((attachmentId) => ({
|
|
_tag: "attachment" as const,
|
|
attachmentId,
|
|
})),
|
|
[queuedImageAttachmentIds],
|
|
);
|
|
const queuedImageAttachmentUrls = useAssetUrls(
|
|
props.environmentId,
|
|
queuedImageAttachmentResources,
|
|
);
|
|
const queuedImageUrlById = useMemo(
|
|
() =>
|
|
new Map(
|
|
queuedImageAttachmentIds.flatMap((attachmentId, index) => {
|
|
const url = queuedImageAttachmentUrls[index];
|
|
return url ? [[attachmentId, url] as const] : [];
|
|
}),
|
|
),
|
|
[queuedImageAttachmentIds, queuedImageAttachmentUrls],
|
|
);
|
|
// Once the projection holds the message the optimistic copy is stale no
|
|
// matter what happened to its run — keying on still-queued runs alone would
|
|
// resurrect a phantom "pending" row after the run is cancelled or started.
|
|
const acknowledgedMessageIds = useMemo(
|
|
() => new Set((projection?.messages ?? []).map((message) => message.id)),
|
|
[projection],
|
|
);
|
|
const optimisticQueued = props.optimisticMessages.filter(
|
|
(message) => message.inputIntent === "queued_turn" && !acknowledgedMessageIds.has(message.id),
|
|
);
|
|
const items = [
|
|
...queued.map(({ run, text, attachments }, serverIndex) => ({
|
|
key: run.id,
|
|
runId: run.id,
|
|
messageId: run.userMessageId,
|
|
serverIndex,
|
|
text,
|
|
attachments,
|
|
thumbnails: attachments
|
|
.filter((attachment) => attachment.type === "image")
|
|
.map<QueuedRowThumbnail>((attachment) => ({
|
|
key: attachment.id,
|
|
name: attachment.name,
|
|
url: queuedImageUrlById.get(attachment.id) ?? null,
|
|
})),
|
|
pending: false,
|
|
})),
|
|
...optimisticQueued.map((message) => ({
|
|
key: message.id,
|
|
runId: null,
|
|
messageId: null,
|
|
serverIndex: null,
|
|
text: message.text,
|
|
attachments: [] as ReadonlyArray<ContractChatAttachment>,
|
|
thumbnails: (message.attachments ?? [])
|
|
.filter(isImageAttachment)
|
|
.map<QueuedRowThumbnail>((attachment) => ({
|
|
key: attachment.id,
|
|
name: attachment.name,
|
|
url: attachment.previewUrl ?? null,
|
|
})),
|
|
pending: true,
|
|
})),
|
|
];
|
|
|
|
const move = async (runId: RunId, beforeRunId: RunId | null) => {
|
|
setBusyRunId(runId);
|
|
try {
|
|
await reorder({
|
|
environmentId: props.environmentId,
|
|
input: { threadId: props.threadId, runId, beforeRunId },
|
|
});
|
|
} finally {
|
|
setBusyRunId(null);
|
|
}
|
|
};
|
|
|
|
const completeDrag = (runId: RunId, insertIndex: number | null) => {
|
|
setDragState(null);
|
|
dragArmedRunIdRef.current = null;
|
|
if (insertIndex === null || busyRunId !== null) return;
|
|
const draggedIndex = queued.findIndex(({ run }) => run.id === runId);
|
|
if (draggedIndex === -1) return;
|
|
// Inserting immediately before or after itself is a no-op.
|
|
if (insertIndex === draggedIndex || insertIndex === draggedIndex + 1) return;
|
|
void move(runId, queued[insertIndex]?.run.id ?? null);
|
|
};
|
|
|
|
const steerInFlightRef = useRef(false);
|
|
const steer = async (queuedRunId: RunId) => {
|
|
if (activeRun === null || !workflow?.canPromoteToSteer || steerInFlightRef.current) return;
|
|
steerInFlightRef.current = true;
|
|
setBusyRunId(queuedRunId);
|
|
try {
|
|
await promote({
|
|
environmentId: props.environmentId,
|
|
input: { threadId: props.threadId, queuedRunId, targetRunId: activeRun.id },
|
|
});
|
|
} finally {
|
|
steerInFlightRef.current = false;
|
|
setBusyRunId(null);
|
|
}
|
|
};
|
|
|
|
useImperativeHandle(ref, () => ({
|
|
steerNext(repeat) {
|
|
const next = queued[0];
|
|
if (!next || !workflow?.canPromoteToSteer) return false;
|
|
if (!repeat && busyRunId === null) void steer(next.run.id);
|
|
return true;
|
|
},
|
|
// Declines while a queued message is already being edited so the key keeps
|
|
// moving the caret inside that draft.
|
|
editLatest(repeat) {
|
|
const latest = queued.at(-1);
|
|
if (!latest || props.editingRunId !== null || busyRunId !== null) return false;
|
|
if (!repeat) {
|
|
setExpanded(true);
|
|
props.onEditQueuedRun({
|
|
runId: latest.run.id,
|
|
messageId: latest.run.userMessageId,
|
|
text: latest.text,
|
|
attachments: latest.attachments,
|
|
});
|
|
}
|
|
return true;
|
|
},
|
|
}));
|
|
|
|
if (items.length === 0) return null;
|
|
|
|
const remove = async (runId: RunId) => {
|
|
setBusyRunId(runId);
|
|
try {
|
|
await cancel({
|
|
environmentId: props.environmentId,
|
|
input: { threadId: props.threadId, runId },
|
|
});
|
|
} finally {
|
|
setBusyRunId(null);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<ComposerBanner.Attachment>
|
|
<ComposerBanner.Root
|
|
role="region"
|
|
aria-label={`${items.length} queued message${items.length === 1 ? "" : "s"}`}
|
|
aria-live="polite"
|
|
data-chat-composer-collapsed-controls="true"
|
|
className="relative z-0"
|
|
>
|
|
<ComposerBanner.Row
|
|
render={<button type="button" />}
|
|
aria-label={expanded ? "Collapse queued messages" : "Expand queued messages"}
|
|
aria-expanded={expanded}
|
|
aria-controls={queueListId}
|
|
onPointerDown={(event) => event.preventDefault()}
|
|
onClick={() => setExpanded((value) => !value)}
|
|
>
|
|
<ComposerBanner.Icon>
|
|
<ListOrderedIcon />
|
|
</ComposerBanner.Icon>
|
|
<ComposerBanner.Content className="text-muted-foreground">Queued</ComposerBanner.Content>
|
|
<ComposerBanner.Actions>
|
|
<ComposerBanner.Count>{items.length}</ComposerBanner.Count>
|
|
<ComposerBanner.ToggleIcon expanded={expanded} />
|
|
</ComposerBanner.Actions>
|
|
</ComposerBanner.Row>
|
|
<ComposerBanner.Scroll className={cn("max-h-32", !expanded && "hidden")}>
|
|
<ComposerBanner.Children render={<ol />} id={queueListId}>
|
|
{items.map((item) => {
|
|
const previewText = replaceComposerContextReferences(item.text, (reference) =>
|
|
reference.kind === "image" && item.thumbnails.length > 0 ? "" : reference.label,
|
|
).trim();
|
|
const rowRunId = item.runId;
|
|
const rowServerIndex = item.serverIndex;
|
|
const isEditing = rowRunId !== null && rowRunId === props.editingRunId;
|
|
const rowDraggable =
|
|
rowRunId !== null && rowServerIndex !== null && canReorder && busyRunId === null;
|
|
return (
|
|
<ComposerBanner.Row
|
|
render={<li />}
|
|
key={item.key}
|
|
aria-current={isEditing ? "true" : undefined}
|
|
className={cn(
|
|
"relative rounded-sm",
|
|
isEditing && "bg-accent text-accent-foreground",
|
|
dragState !== null && dragState.runId === item.runId && "opacity-50",
|
|
)}
|
|
draggable={rowDraggable}
|
|
onDragStart={(event) => {
|
|
if (item.runId === null || dragArmedRunIdRef.current !== item.runId) {
|
|
event.preventDefault();
|
|
return;
|
|
}
|
|
event.dataTransfer.setData(QUEUED_RUN_DRAG_TYPE, item.runId);
|
|
event.dataTransfer.effectAllowed = "move";
|
|
setDragState({ runId: item.runId, insertIndex: null });
|
|
}}
|
|
onDragEnd={() => {
|
|
dragArmedRunIdRef.current = null;
|
|
setDragState(null);
|
|
}}
|
|
onDragOver={(event) => {
|
|
if (dragState === null || item.serverIndex === null) return;
|
|
event.preventDefault();
|
|
event.dataTransfer.dropEffect = "move";
|
|
const rect = event.currentTarget.getBoundingClientRect();
|
|
const insertIndex =
|
|
event.clientY < rect.top + rect.height / 2
|
|
? item.serverIndex
|
|
: item.serverIndex + 1;
|
|
if (dragState.insertIndex !== insertIndex) {
|
|
setDragState({ runId: dragState.runId, insertIndex });
|
|
}
|
|
}}
|
|
onDrop={(event) => {
|
|
if (dragState === null) return;
|
|
event.preventDefault();
|
|
completeDrag(dragState.runId, dragState.insertIndex);
|
|
}}
|
|
>
|
|
{item.serverIndex !== null && dragState?.insertIndex === item.serverIndex ? (
|
|
<span
|
|
aria-hidden
|
|
className="pointer-events-none absolute inset-x-0 top-0 h-0.5 rounded bg-primary/70"
|
|
/>
|
|
) : null}
|
|
{item.serverIndex === queued.length - 1 &&
|
|
dragState?.insertIndex === queued.length ? (
|
|
<span
|
|
aria-hidden
|
|
className="pointer-events-none absolute inset-x-0 bottom-0 h-0.5 rounded bg-primary/70"
|
|
/>
|
|
) : null}
|
|
<ComposerBanner.Icon aria-hidden={false}>
|
|
{canReorder && rowRunId !== null && rowServerIndex !== null ? (
|
|
<Button
|
|
size="icon-xs"
|
|
variant="ghost-muted"
|
|
aria-label="Reorder queued message (drag, or press the arrow keys)"
|
|
className="cursor-grab active:cursor-grabbing disabled:cursor-default"
|
|
disabled={busyRunId !== null}
|
|
onPointerDown={() => {
|
|
dragArmedRunIdRef.current = rowRunId;
|
|
}}
|
|
onKeyDown={(event) => {
|
|
if (busyRunId !== null) return;
|
|
if (event.key === "ArrowUp" && rowServerIndex > 0) {
|
|
event.preventDefault();
|
|
void move(rowRunId, queued[rowServerIndex - 1]?.run.id ?? null);
|
|
}
|
|
if (event.key === "ArrowDown" && rowServerIndex < queued.length - 1) {
|
|
event.preventDefault();
|
|
void move(rowRunId, queued[rowServerIndex + 2]?.run.id ?? null);
|
|
}
|
|
}}
|
|
>
|
|
<GripVerticalIcon />
|
|
</Button>
|
|
) : null}
|
|
</ComposerBanner.Icon>
|
|
<ComposerBanner.Content className="text-foreground/80">
|
|
{isEditing ? <span className="sr-only">Editing queued message: </span> : null}
|
|
{item.pending ? (
|
|
<Clock3Icon
|
|
aria-label="Saving queued message"
|
|
className="size-3 shrink-0 text-muted-foreground/60"
|
|
/>
|
|
) : null}
|
|
{item.thumbnails.length > 0 ? (
|
|
<span className="flex shrink-0 items-center gap-0.5">
|
|
{item.thumbnails.map((thumbnail) => (
|
|
<span
|
|
key={thumbnail.key}
|
|
className="size-4 overflow-hidden rounded border border-border/70 bg-background"
|
|
>
|
|
{thumbnail.url ? (
|
|
<img
|
|
src={thumbnail.url}
|
|
alt={thumbnail.name}
|
|
className="size-full object-cover"
|
|
/>
|
|
) : (
|
|
<span
|
|
aria-label={thumbnail.name}
|
|
className="block size-full bg-muted/60"
|
|
/>
|
|
)}
|
|
</span>
|
|
))}
|
|
</span>
|
|
) : null}
|
|
<Tooltip>
|
|
<TooltipTrigger render={<span className="min-w-0 flex-1 truncate" />}>
|
|
{previewText}
|
|
</TooltipTrigger>
|
|
<TooltipPopup side="top" className="max-w-96 break-words">
|
|
{previewText}
|
|
</TooltipPopup>
|
|
</Tooltip>
|
|
</ComposerBanner.Content>
|
|
<ComposerBanner.Actions>
|
|
{isEditing ? (
|
|
<Button
|
|
size="xs"
|
|
variant="ghost"
|
|
aria-label="Cancel editing queued message"
|
|
onClick={props.onCancelEdit}
|
|
>
|
|
Cancel
|
|
</Button>
|
|
) : (
|
|
<>
|
|
<Tooltip>
|
|
<TooltipTrigger
|
|
render={
|
|
<Button
|
|
size="icon-xs"
|
|
variant="ghost-muted"
|
|
aria-label="Edit queued message"
|
|
disabled={item.runId === null || busyRunId !== null}
|
|
onClick={() => {
|
|
if (item.runId !== null && item.messageId !== null) {
|
|
props.onEditQueuedRun({
|
|
runId: item.runId,
|
|
messageId: item.messageId,
|
|
text: item.text,
|
|
attachments: item.attachments,
|
|
});
|
|
}
|
|
}}
|
|
/>
|
|
}
|
|
>
|
|
<PencilIcon />
|
|
</TooltipTrigger>
|
|
<TooltipPopup>
|
|
{`Edit in the composer${item.serverIndex === queued.length - 1 && props.editShortcutLabel ? ` (${props.editShortcutLabel})` : ""}`}
|
|
</TooltipPopup>
|
|
</Tooltip>
|
|
<Tooltip>
|
|
<TooltipTrigger render={<span className="flex shrink-0" />}>
|
|
<Button
|
|
size="xs"
|
|
variant="ghost-muted"
|
|
disabled={
|
|
item.runId === null ||
|
|
busyRunId !== null ||
|
|
!workflow?.canPromoteToSteer
|
|
}
|
|
onClick={() => {
|
|
if (item.runId !== null) {
|
|
void steer(item.runId);
|
|
}
|
|
}}
|
|
>
|
|
<CornerUpRightIcon />
|
|
Steer
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipPopup>
|
|
{activeRun === null
|
|
? "There is no active run to steer"
|
|
: `Send as a steer instead${item.serverIndex === 0 && props.steerShortcutLabel ? ` (${props.steerShortcutLabel})` : ""}`}
|
|
</TooltipPopup>
|
|
</Tooltip>
|
|
<Tooltip>
|
|
<TooltipTrigger
|
|
render={
|
|
<ComposerBanner.Dismiss
|
|
aria-label="Remove queued message"
|
|
disabled={item.runId === null || busyRunId !== null}
|
|
onClick={() => {
|
|
if (item.runId !== null) void remove(item.runId);
|
|
}}
|
|
/>
|
|
}
|
|
/>
|
|
<TooltipPopup>Remove from queue</TooltipPopup>
|
|
</Tooltip>
|
|
</>
|
|
)}
|
|
</ComposerBanner.Actions>
|
|
</ComposerBanner.Row>
|
|
);
|
|
})}
|
|
</ComposerBanner.Children>
|
|
</ComposerBanner.Scroll>
|
|
</ComposerBanner.Root>
|
|
</ComposerBanner.Attachment>
|
|
);
|
|
}
|