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

threadQueueControlPresentation.ts

70 lines2.3 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";
8​
9export const REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL = msg("Remove queued message");
10​
11export interface ThreadQueueRowControls {
12 readonly canDismiss: boolean;
13 readonly canEdit: boolean;
14 readonly canSend: boolean;
15 readonly dismissAccessibilityLabel: string;
16 readonly displayText: string;
17 readonly isEditing: boolean;
19​
20export function resolveThreadQueueRowControls(input: {
21 readonly busy: boolean;
22 readonly canPromoteToSteer: boolean;
23 readonly canSendNow: boolean;
24 /** Something else holds the composer, such as an open question. */
25 readonly editBlocked?: boolean;
26 readonly isEditing?: boolean;
27 /** A guest's message keeps the guest's words; the owner can cancel it, not rewrite it. */
28 readonly isGuestMessage?: boolean;
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 canSend: mutationEnabled && (input.canPromoteToSteer || input.canSendNow) && !isEditing,
41 dismissAccessibilityLabel: REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL,
42 // Editing and sending keep the original text so quoted citations stay linked.
43 displayText: isAttachmentOnlyBootstrapPrompt(input.text)
44 ? ""
45 : assistantCitationsToPlainText(
46 replaceComposerContextReferences(input.text, (reference) => reference.label),
47 ).trim(),
48 isEditing,
49 };
51​
52export function buildCancelQueuedRunCommand(input: {
53 readonly environmentId: EnvironmentId;
54 readonly runId: RunId;
55 readonly threadId: ThreadId;
56}): {
57 readonly environmentId: EnvironmentId;
58 readonly input: {
59 readonly runId: RunId;
60 readonly threadId: ThreadId;
61 };
62} {
63 return {
64 environmentId: input.environmentId,
65 input: {
66 runId: input.runId,
67 threadId: input.threadId,
68 },
69 };