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