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 /** Something else holds the composer, such as an open question. */
23 readonly editBlocked?: boolean;
24 readonly index: number;
25 readonly isEditing?: boolean;
26 /** A guest's message keeps the guest's words; the owner can cancel it, not rewrite it. */
27 readonly isGuestMessage?: boolean;
28 readonly queuedCount: number;
29 readonly text: string;
30}): ThreadQueueRowControls {
31 const mutationEnabled = !input.busy;
32 const isEditing = input.isEditing === true;
33
34 return {
35 canDismiss: !input.busy,
36 // Re-opening the row already in the composer would reload it and throw
37 // away whatever has been typed since.
38 canEdit:
39 mutationEnabled && !isEditing && input.isGuestMessage !== true && input.editBlocked !== true,
40 canMoveDown: mutationEnabled && input.canReorder && input.index < input.queuedCount - 1,
41 canMoveUp: mutationEnabled && input.canReorder && input.index > 0,
42 canSend: mutationEnabled && (input.canPromoteToSteer || input.canSendNow) && !isEditing,
43 dismissAccessibilityLabel: REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL,
44 // Editing and sending keep the original text so quoted citations stay linked.
45 displayText: assistantCitationsToPlainText(input.text),
46 isEditing,
47 };
48}
49
50export function buildCancelQueuedRunCommand(input: {
51 readonly environmentId: EnvironmentId;
52 readonly runId: RunId;
53 readonly threadId: ThreadId;
54}): {
55 readonly environmentId: EnvironmentId;
56 readonly input: {
57 readonly runId: RunId;
58 readonly threadId: ThreadId;
59 };
60} {
61 return {
62 environmentId: input.environmentId,
63 input: {
64 runId: input.runId,
65 threadId: input.threadId,
66 },
67 };
68}
69
70export function resolveQueueDragBeforeRunId(
71 rows: ReadonlyArray<{ id: RunId; y?: number; height?: number }>,
72 runId: RunId,
73 translationY: number,
74): RunId | null | undefined {
75 const sourceIndex = rows.findIndex((row) => row.id === runId);
76 const source = rows[sourceIndex];
77 if (!source || rows.some((row) => row.y === undefined || row.height === undefined)) return;
78 const center = source.y! + source.height! / 2 + translationY;
79 const remaining = rows.filter((row) => row.id !== runId);
80 return remaining.find((row) => center < row.y! + row.height! / 2)?.id ?? null;
81}
82
83export function resolveQueueDropBeforeRunId(
84 rows: ReadonlyArray<{ id: RunId; y?: number; height?: number }>,
85 runId: RunId,
86 translationY: number,
87): RunId | null | undefined {
88 const before = resolveQueueDragBeforeRunId(rows, runId, translationY);
89 if (before === undefined) return;
90 const sourceIndex = rows.findIndex((row) => row.id === runId);
91 return before === (rows[sourceIndex + 1]?.id ?? null) ? undefined : before;
92}