The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { PROVIDER_SEND_TURN_MAX_INPUT_CHARS } from "@t3tools/contracts"; |
| 2 | import { expandAssistantCitationsForProvider } from "@t3tools/shared/assistantCitations"; |
| 3 | import { msg } from "gt-react"; |
| 4 | |
| 5 | import { getActiveLocale, m, type MessageResolver } from "../../i18n/messages"; |
| 6 | |
| 7 | type ComposerSubmitEvent = { preventDefault: () => void }; |
| 8 | |
| 9 | type ComposerSubmissionInput = { |
| 10 | prompt: string; |
| 11 | providerInput?: string; |
| 12 | submissionTarget: "provider-turn" | "pending-user-input"; |
| 13 | }; |
| 14 | |
| 15 | export function getComposerPromptLengthValidationMessage( |
| 16 | prompt: string, |
| 17 | resolve: MessageResolver = m, |
| 18 | locale: string = getActiveLocale(), |
| 19 | ): string | null { |
| 20 | const normalizedPrompt = prompt.trim(); |
| 21 | const inputLength = Math.max( |
| 22 | normalizedPrompt.length, |
| 23 | expandAssistantCitationsForProvider(normalizedPrompt).length, |
| 24 | ); |
| 25 | const excessCharacters = inputLength - PROVIDER_SEND_TURN_MAX_INPUT_CHARS; |
| 26 | if (excessCharacters <= 0) return null; |
| 27 | |
| 28 | return resolve( |
| 29 | msg( |
| 30 | "{count, plural, one {Prompt is {excess} character over the {limit}-character limit. Shorten or split it before sending.} other {Prompt is {excess} characters over the {limit}-character limit. Shorten or split it before sending.}}", |
| 31 | { |
| 32 | count: excessCharacters, |
| 33 | excess: excessCharacters.toLocaleString(locale), |
| 34 | limit: PROVIDER_SEND_TURN_MAX_INPUT_CHARS.toLocaleString(locale), |
| 35 | }, |
| 36 | ), |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | export function getComposerSubmissionValidationMessage( |
| 41 | options: ComposerSubmissionInput, |
| 42 | resolve: MessageResolver = m, |
| 43 | ): string | null { |
| 44 | return options.submissionTarget === "provider-turn" |
| 45 | ? getComposerPromptLengthValidationMessage(options.providerInput ?? options.prompt, resolve) |
| 46 | : null; |
| 47 | } |
| 48 | |
| 49 | // Answering a question responds to the open request instead of starting a provider turn, so |
| 50 | // provider and turn-send state must not block it. Its own attachments and connection still do. |
| 51 | export function isComposerSubmissionBlocked(input: { |
| 52 | submissionTarget: ComposerSubmissionInput["submissionTarget"]; |
| 53 | noProviderAvailable: boolean; |
| 54 | sendDisabledReason: string | null; |
| 55 | attachmentBlockReason: string | null; |
| 56 | environmentUnavailable: boolean; |
| 57 | }): boolean { |
| 58 | return input.submissionTarget === "pending-user-input" |
| 59 | ? input.attachmentBlockReason !== null || input.environmentUnavailable |
| 60 | : input.noProviderAvailable || input.sendDisabledReason !== null; |
| 61 | } |
| 62 | |
| 63 | export function submitComposerDraft( |
| 64 | options: ComposerSubmissionInput & { |
| 65 | event: ComposerSubmitEvent | undefined; |
| 66 | onSend: (event?: ComposerSubmitEvent) => boolean | void; |
| 67 | }, |
| 68 | ): { validationMessage: string | null; didDispatch: boolean } { |
| 69 | const validationMessage = getComposerSubmissionValidationMessage(options); |
| 70 | if (validationMessage) { |
| 71 | options.event?.preventDefault(); |
| 72 | return { validationMessage, didDispatch: false }; |
| 73 | } |
| 74 | |
| 75 | if (options.onSend(options.event) === false) { |
| 76 | options.event?.preventDefault(); |
| 77 | return { validationMessage: null, didDispatch: false }; |
| 78 | } |
| 79 | return { validationMessage: null, didDispatch: true }; |
| 80 | } |