Open History to see the last commit
1import type { EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";
2import { assistantCitationsToPlainText } from "@t3tools/shared/assistantCitations";
3
4export const REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL = "Remove queued message";
5
6export interface ThreadQueueRowControls {
7 readonly canDismiss: boolean;
8 readonly canEdit: boolean;
9 readonly canMoveDown: boolean;
10 readonly canMoveUp: boolean;
11 readonly canSend: boolean;
12 readonly dismissAccessibilityLabel: string;
13 readonly displayText: string;
14 readonly isEditing: boolean;
15}
16
17export function resolveThreadQueueRowControls(input: {
18 readonly busy: boolean;
19 readonly canPromoteToSteer: boolean;
20 readonly canSendNow: boolean;
21 readonly canReorder: boolean;
22 readonly index: number;
23 readonly isEditing?: boolean;
24 /** A guest's message keeps the guest's words; the owner can cancel it, not rewrite it. */
25 readonly isGuestMessage?: boolean;
26 readonly queuedCount: number;
27 readonly text: string;
28}): ThreadQueueRowControls {
29 const mutationEnabled = !input.busy;
30 const isEditing = input.isEditing === true;
31
32 return {
33 canDismiss: !input.busy,
34 // Re-opening the row already in the composer would reload it and throw
35 // away whatever has been typed since.
36 canEdit: mutationEnabled && !isEditing && input.isGuestMessage !== true,
37 canMoveDown: mutationEnabled && input.canReorder && input.index < input.queuedCount - 1,
38 canMoveUp: mutationEnabled && input.canReorder && input.index > 0,
39 canSend: mutationEnabled && (input.canPromoteToSteer || input.canSendNow) && !isEditing,
40 dismissAccessibilityLabel: REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL,
41 // Editing and sending keep the original text so quoted citations stay linked.
42 displayText: assistantCitationsToPlainText(input.text),
43 isEditing,
44 };
45}
46
47export function buildCancelQueuedRunCommand(input: {
48 readonly environmentId: EnvironmentId;
49 readonly runId: RunId;
50 readonly threadId: ThreadId;
51}): {
52 readonly environmentId: EnvironmentId;
53 readonly input: {
54 readonly runId: RunId;
55 readonly threadId: ThreadId;
56 };
57} {
58 return {
59 environmentId: input.environmentId,
60 input: {
61 runId: input.runId,
62 threadId: input.threadId,
63 },
64 };
65}
66
67export function resolveQueueDragBeforeRunId(
68 rows: ReadonlyArray<{ id: RunId; y?: number; height?: number }>,
69 runId: RunId,
70 translationY: number,
71): RunId | null | undefined {
72 const sourceIndex = rows.findIndex((row) => row.id === runId);
73 const source = rows[sourceIndex];
74 if (!source || rows.some((row) => row.y === undefined || row.height === undefined)) return;
75 const center = source.y! + source.height! / 2 + translationY;
76 const remaining = rows.filter((row) => row.id !== runId);
77 return remaining.find((row) => center < row.y! + row.height! / 2)?.id ?? null;
78}
79
80export function resolveQueueDropBeforeRunId(
81 rows: ReadonlyArray<{ id: RunId; y?: number; height?: number }>,
82 runId: RunId,
83 translationY: number,
84): RunId | null | undefined {
85 const before = resolveQueueDragBeforeRunId(rows, runId, translationY);
86 if (before === undefined) return;
87 const sourceIndex = rows.findIndex((row) => row.id === runId);
88 return before === (rows[sourceIndex + 1]?.id ?? null) ? undefined : before;
89}