Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadQueueControlPresentation.ts

73 lines2.5 KB

1import type { EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";
2import { msg } from "gt-react-native";
3import { assistantCitationsToPlainText } from "@t3tools/shared/assistantCitations";
4import {
5 isAttachmentOnlyBootstrapPrompt,
6 replaceComposerContextReferences,
7} from "@t3tools/shared/composerContextReferences";
8import { composerFileLinksToPlainText } from "@t3tools/shared/composerInlineTokens";
9​
10export const REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL = msg("Remove queued message");
11​
12export interface ThreadQueueRowControls {
13 readonly canDismiss: boolean;
14 readonly canEdit: boolean;
15 readonly canSend: boolean;
16 readonly dismissAccessibilityLabel: string;
17 readonly displayText: string;
18 readonly isEditing: boolean;
20​
21export function resolveThreadQueueRowControls(input: {
22 readonly busy: boolean;
23 readonly canPromoteToSteer: boolean;
24 readonly canSendNow: boolean;
25 /** Something else holds the composer, such as an open question. */
26 readonly editBlocked?: boolean;
27 readonly isEditing?: boolean;
28 /** A guest's message keeps the guest's words; the owner can cancel it, not rewrite it. */
29 readonly isGuestMessage?: boolean;
30 readonly text: string;
31}): ThreadQueueRowControls {
32 const mutationEnabled = !input.busy;
33 const isEditing = input.isEditing === true;
34​
35 return {
36 canDismiss: !input.busy,
37 // Re-opening the row already in the composer would reload it and throw
38 // away whatever has been typed since.
39 canEdit:
40 mutationEnabled && !isEditing && input.isGuestMessage !== true && input.editBlocked !== true,
41 canSend: mutationEnabled && (input.canPromoteToSteer || input.canSendNow) && !isEditing,
42 dismissAccessibilityLabel: REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL,
43 // Editing and sending keep the original text so quoted citations stay linked.
44 displayText: isAttachmentOnlyBootstrapPrompt(input.text)
45 ? ""
46 : assistantCitationsToPlainText(
47 composerFileLinksToPlainText(
48 replaceComposerContextReferences(input.text, (reference) => reference.label),
49 ),
50 ).trim(),
51 isEditing,
52 };
54​
55export function buildCancelQueuedRunCommand(input: {
56 readonly environmentId: EnvironmentId;
57 readonly runId: RunId;
58 readonly threadId: ThreadId;
59}): {
60 readonly environmentId: EnvironmentId;
61 readonly input: {
62 readonly runId: RunId;
63 readonly threadId: ThreadId;
64 };
65} {
66 return {
67 environmentId: input.environmentId,
68 input: {
69 runId: input.runId,
70 threadId: input.threadId,
71 },
72 };