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