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

composerSubmission.ts

64 lines2.6 KB

1import { PROVIDER_SEND_TURN_MAX_INPUT_CHARS } from "@t3tools/contracts";
2import { expandAssistantCitationsForProvider } from "@t3tools/shared/assistantCitations";
3​
4type ComposerSubmitEvent = { preventDefault: () => void };
5​
6type ComposerSubmissionInput = {
7 prompt: string;
8 providerInput?: string;
9 submissionTarget: "provider-turn" | "pending-user-input";
10};
11​
12export function getComposerPromptLengthValidationMessage(prompt: string): string | null {
13 const normalizedPrompt = prompt.trim();
14 const inputLength = Math.max(
15 normalizedPrompt.length,
16 expandAssistantCitationsForProvider(normalizedPrompt).length,
17 );
18 const excessCharacters = inputLength - PROVIDER_SEND_TURN_MAX_INPUT_CHARS;
19 if (excessCharacters <= 0) return null;
20​
21 const characterLabel = excessCharacters === 1 ? "character" : "characters";
22 return `Prompt is ${excessCharacters.toLocaleString("en-US")} ${characterLabel} over the ${PROVIDER_SEND_TURN_MAX_INPUT_CHARS.toLocaleString("en-US")}-character limit. Shorten or split it before sending.`;
24​
25export function getComposerSubmissionValidationMessage(
26 options: ComposerSubmissionInput,
27): string | null {
28 return options.submissionTarget === "provider-turn"
29 ? getComposerPromptLengthValidationMessage(options.providerInput ?? options.prompt)
30 : null;
32​
33// Answering a question responds to the open request instead of starting a provider turn, so
34// provider and turn-send state must not block it. Its own attachments and connection still do.
35export function isComposerSubmissionBlocked(input: {
36 submissionTarget: ComposerSubmissionInput["submissionTarget"];
37 noProviderAvailable: boolean;
38 sendDisabledReason: string | null;
39 attachmentBlockReason: string | null;
40 environmentUnavailable: boolean;
41}): boolean {
42 return input.submissionTarget === "pending-user-input"
43 ? input.attachmentBlockReason !== null || input.environmentUnavailable
44 : input.noProviderAvailable || input.sendDisabledReason !== null;
46​
47export function submitComposerDraft(
48 options: ComposerSubmissionInput & {
49 event: ComposerSubmitEvent | undefined;
50 onSend: (event?: ComposerSubmitEvent) => boolean | void;
51 },
52): { validationMessage: string | null; didDispatch: boolean } {
53 const validationMessage = getComposerSubmissionValidationMessage(options);
54 if (validationMessage) {
55 options.event?.preventDefault();
56 return { validationMessage, didDispatch: false };
57 }
58​
59 if (options.onSend(options.event) === false) {
60 options.event?.preventDefault();
61 return { validationMessage: null, didDispatch: false };
62 }
63 return { validationMessage: null, didDispatch: true };