fix(web): reject oversized prompts before provider turn start (#6602)
This commit is contained in:
1 parent
c0f9d917c1
commit
7c55e86320
7 files changed
+362
-25
No files matched your search
@@ -4994,6 +4994,16 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
draftText: trimmed,
|
||||
planMarkdown: activeProposedPlan.planMarkdown,
|
||||
});
|
||||
const outgoingFollowUpText = formatOutgoingPrompt({
|
||||
provider: ctxSelectedProvider,
|
||||
model: ctxSelectedModel,
|
||||
models: ctxSelectedProviderModels,
|
||||
effort: ctxSelectedPromptEffort,
|
||||
text: followUp.text.trim(),
|
||||
});
|
||||
if (composerRef.current?.validateProviderInput(outgoingFollowUpText) === false) {
|
||||
return;
|
||||
}
|
||||
promptRef.current = "";
|
||||
clearComposerDraftContent(composerDraftTarget);
|
||||
composerRef.current?.resetCursorState();
|
||||
@@ -5063,6 +5073,34 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
return;
|
||||
}
|
||||
|
||||
const composerImagesSnapshot = [...composerImages];
|
||||
const composerTerminalContextsSnapshot = [...sendableComposerTerminalContexts];
|
||||
const composerElementContextsSnapshot = [...composerElementContexts];
|
||||
const composerPreviewAnnotationsSnapshot = [...composerPreviewAnnotations];
|
||||
const composerReviewCommentsSnapshot: ReviewCommentContext[] = [...composerReviewComments];
|
||||
const messageTextWithContexts = appendElementContextsToPrompt(
|
||||
appendTerminalContextsToPrompt(promptForSend, composerTerminalContextsSnapshot),
|
||||
composerElementContextsSnapshot,
|
||||
);
|
||||
const messageTextWithPreviewAnnotations = composerPreviewAnnotationsSnapshot.reduce(
|
||||
(text, annotation) => appendPreviewAnnotationPrompt(text, annotation),
|
||||
messageTextWithContexts,
|
||||
);
|
||||
const messageTextForSend = appendReviewCommentsToPrompt(
|
||||
messageTextWithPreviewAnnotations,
|
||||
composerReviewCommentsSnapshot,
|
||||
);
|
||||
const outgoingMessageText = formatOutgoingPrompt({
|
||||
provider: ctxSelectedProvider,
|
||||
model: ctxSelectedModel,
|
||||
models: ctxSelectedProviderModels,
|
||||
effort: ctxSelectedPromptEffort,
|
||||
text: messageTextForSend || IMAGE_ONLY_BOOTSTRAP_PROMPT,
|
||||
});
|
||||
if (composerRef.current?.validateProviderInput(outgoingMessageText) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
sendInFlightRef.current = true;
|
||||
if (isDraftHeroState && activeThreadKey) {
|
||||
let resolveDockStarted: (() => void) | undefined;
|
||||
@@ -5081,32 +5119,8 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
}
|
||||
beginLocalDispatch({ preparingWorktree: Boolean(baseBranchForWorktree) });
|
||||
|
||||
const composerImagesSnapshot = [...composerImages];
|
||||
const composerTerminalContextsSnapshot = [...sendableComposerTerminalContexts];
|
||||
const composerElementContextsSnapshot = [...composerElementContexts];
|
||||
const composerPreviewAnnotationsSnapshot = [...composerPreviewAnnotations];
|
||||
const composerReviewCommentsSnapshot: ReviewCommentContext[] = [...composerReviewComments];
|
||||
const messageTextWithContexts = appendElementContextsToPrompt(
|
||||
appendTerminalContextsToPrompt(promptForSend, composerTerminalContextsSnapshot),
|
||||
composerElementContextsSnapshot,
|
||||
);
|
||||
const messageTextWithPreviewAnnotations = composerPreviewAnnotationsSnapshot.reduce(
|
||||
(text, annotation) => appendPreviewAnnotationPrompt(text, annotation),
|
||||
messageTextWithContexts,
|
||||
);
|
||||
const messageTextForSend = appendReviewCommentsToPrompt(
|
||||
messageTextWithPreviewAnnotations,
|
||||
composerReviewCommentsSnapshot,
|
||||
);
|
||||
const messageIdForSend = newMessageId();
|
||||
const messageCreatedAt = new Date().toISOString();
|
||||
const outgoingMessageText = formatOutgoingPrompt({
|
||||
provider: ctxSelectedProvider,
|
||||
model: ctxSelectedModel,
|
||||
models: ctxSelectedProviderModels,
|
||||
effort: ctxSelectedPromptEffort,
|
||||
text: messageTextForSend || IMAGE_ONLY_BOOTSTRAP_PROMPT,
|
||||
});
|
||||
const turnAttachmentsPromise = Promise.all(
|
||||
composerImagesSnapshot.map(async (image) => ({
|
||||
type: "image" as const,
|
||||
@@ -5723,6 +5737,9 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
effort: ctxSelectedPromptEffort,
|
||||
text: implementationPrompt,
|
||||
});
|
||||
if (composerRef.current?.validateProviderInput(outgoingImplementationPrompt) === false) {
|
||||
return;
|
||||
}
|
||||
const nextThreadTitle = truncate(buildPlanImplementationThreadTitle(planMarkdown));
|
||||
const nextThreadModelSelection: ModelSelection = ctxSelectedModelSelection;
|
||||
|
||||
|
||||
@@ -107,6 +107,12 @@ import { buildExpandedImagePreview, type ExpandedImagePreview } from "./Expanded
|
||||
import { basenameOfPath } from "../../pierre-icons";
|
||||
import { cn, randomUUID } from "~/lib/utils";
|
||||
import { Separator } from "../ui/separator";
|
||||
import {
|
||||
getComposerPromptLengthValidationMessage,
|
||||
getComposerSubmissionValidationMessage,
|
||||
submitComposerDraft,
|
||||
} from "./composerSubmission";
|
||||
import { ComposerPromptLengthValidation } from "./ComposerPromptLengthValidation";
|
||||
|
||||
type ComposerCommandMenuPosition = {
|
||||
bottom: number;
|
||||
@@ -488,6 +494,8 @@ export interface ChatComposerHandle {
|
||||
selectedModel: string;
|
||||
selectedProviderModels: ReadonlyArray<ServerProvider["models"][number]>;
|
||||
};
|
||||
/** Validate the fully composed text immediately before a provider turn starts. */
|
||||
validateProviderInput: (providerInput: string) => boolean;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
@@ -951,6 +959,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
const [isComposerPrimaryActionsCompact, setIsComposerPrimaryActionsCompact] = useState(false);
|
||||
const [isComposerModelPickerOpen, setIsComposerModelPickerOpen] = useState(false);
|
||||
const [isComposerFocused, setIsComposerFocused] = useState(false);
|
||||
const [composerSubmissionError, setComposerSubmissionError] = useState<string | null>(null);
|
||||
const [providerInputSubmissionError, setProviderInputSubmissionError] = useState<string | null>(
|
||||
null,
|
||||
);
|
||||
const [composerMenuAnchor, setComposerMenuAnchor] = useState<HTMLDivElement | null>(null);
|
||||
const [isStashMenuOpen, setIsStashMenuOpen] = useState(false);
|
||||
const [stashPulse, setStashPulse] = useState<{ key: number; active: boolean }>({
|
||||
@@ -967,6 +979,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
const composerEditorRef = useRef<ComposerPromptEditorHandle>(null);
|
||||
const composerFormRef = useRef<HTMLFormElement>(null);
|
||||
const composerSurfaceRef = useRef<HTMLDivElement>(null);
|
||||
const providerInputRejectedRef = useRef(false);
|
||||
const composerSelectLockRef = useRef(false);
|
||||
const composerMenuOpenRef = useRef(false);
|
||||
const composerMenuItemsRef = useRef<ComposerCommandItem[]>([]);
|
||||
@@ -1309,6 +1322,27 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
setComposerCursor((existing) => clampCollapsedComposerCursor(prompt, existing));
|
||||
}, [prompt, promptRef]);
|
||||
|
||||
useEffect(() => {
|
||||
if (composerSubmissionError === null) return;
|
||||
const nextError = getComposerPromptLengthValidationMessage(prompt);
|
||||
if (nextError !== composerSubmissionError) {
|
||||
setComposerSubmissionError(nextError);
|
||||
}
|
||||
}, [composerSubmissionError, prompt]);
|
||||
|
||||
useEffect(() => {
|
||||
setProviderInputSubmissionError(null);
|
||||
}, [
|
||||
composerElementContexts,
|
||||
composerPreviewAnnotations,
|
||||
composerReviewComments,
|
||||
composerTerminalContexts,
|
||||
prompt,
|
||||
selectedModel,
|
||||
selectedPromptEffort,
|
||||
selectedProvider,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
composerImagesRef.current = composerImages;
|
||||
}, [composerImages, composerImagesRef]);
|
||||
@@ -1400,6 +1434,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
// ------------------------------------------------------------------
|
||||
useEffect(() => {
|
||||
setComposerHighlightedItemId(null);
|
||||
setComposerSubmissionError(null);
|
||||
setProviderInputSubmissionError(null);
|
||||
setComposerCursor(collapseExpandedComposerCursor(promptRef.current, promptRef.current.length));
|
||||
setComposerTrigger(detectComposerTrigger(promptRef.current, promptRef.current.length));
|
||||
setIsDragOverComposer(false);
|
||||
@@ -1826,17 +1862,32 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
});
|
||||
return;
|
||||
}
|
||||
onSend(event);
|
||||
const submission = submitComposerDraft({
|
||||
prompt: promptRef.current,
|
||||
submissionTarget: activePendingProgress ? "pending-user-input" : "provider-turn",
|
||||
event,
|
||||
onSend: (sendEvent) => {
|
||||
// ChatView reports its final composed-input preflight through the
|
||||
// composer handle before its first asynchronous send step.
|
||||
providerInputRejectedRef.current = false;
|
||||
onSend(sendEvent);
|
||||
return !providerInputRejectedRef.current;
|
||||
},
|
||||
});
|
||||
setComposerSubmissionError(submission.validationMessage);
|
||||
if (!submission.didDispatch) return;
|
||||
if (shouldBlurMobileComposerOnSubmit()) {
|
||||
blurMobileComposerAfterSend();
|
||||
}
|
||||
},
|
||||
[
|
||||
activeThreadId,
|
||||
activePendingProgress,
|
||||
blurMobileComposerAfterSend,
|
||||
isSendDisabled,
|
||||
noProviderAvailable,
|
||||
onSend,
|
||||
promptRef,
|
||||
shouldBlurMobileComposerOnSubmit,
|
||||
],
|
||||
);
|
||||
@@ -2590,6 +2641,16 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
selectedModel,
|
||||
selectedProviderModels,
|
||||
}),
|
||||
validateProviderInput: (providerInput: string) => {
|
||||
const validationMessage = getComposerSubmissionValidationMessage({
|
||||
prompt: promptRef.current,
|
||||
providerInput,
|
||||
submissionTarget: "provider-turn",
|
||||
});
|
||||
providerInputRejectedRef.current = validationMessage !== null;
|
||||
setProviderInputSubmissionError(validationMessage);
|
||||
return validationMessage === null;
|
||||
},
|
||||
}),
|
||||
[
|
||||
activeThread,
|
||||
@@ -3058,6 +3119,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<ComposerPromptLengthValidation
|
||||
message={providerInputSubmissionError ?? composerSubmissionError}
|
||||
/>
|
||||
|
||||
{/* Bottom toolbar */}
|
||||
{isComposerCollapsedMobile ? null : activePendingApproval ? (
|
||||
<div className="flex items-center justify-end gap-2 px-3 pb-3 sm:px-4 sm:pb-4">
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { PROVIDER_SEND_TURN_MAX_INPUT_CHARS } from "@t3tools/contracts";
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { getComposerPromptLengthValidationMessage } from "./composerSubmission";
|
||||
import { ComposerPromptLengthValidation } from "./ComposerPromptLengthValidation";
|
||||
|
||||
describe("ComposerPromptLengthValidation", () => {
|
||||
it("renders oversized prompt feedback as an actionable composer alert", () => {
|
||||
const message = getComposerPromptLengthValidationMessage(
|
||||
"x".repeat(PROVIDER_SEND_TURN_MAX_INPUT_CHARS + 1),
|
||||
);
|
||||
|
||||
const markup = renderToStaticMarkup(<ComposerPromptLengthValidation message={message} />);
|
||||
|
||||
expect(markup).toContain('role="alert"');
|
||||
expect(markup).toContain('data-chat-composer-validation="prompt-length"');
|
||||
expect(markup).toContain(
|
||||
"Prompt is 1 character over the 120,000-character limit. Shorten or split it before sending.",
|
||||
);
|
||||
expect(markup).not.toContain("ProviderValidationError");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
export function ComposerPromptLengthValidation({ message }: { message: string | null }) {
|
||||
if (!message) return null;
|
||||
|
||||
return (
|
||||
<p
|
||||
role="alert"
|
||||
className="px-3 pb-2 text-xs text-destructive sm:px-4"
|
||||
data-chat-composer-validation="prompt-length"
|
||||
>
|
||||
{message}
|
||||
</p>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
import { PROVIDER_SEND_TURN_MAX_INPUT_CHARS } from "@t3tools/contracts";
|
||||
import { describe, expect, it, vi } from "vite-plus/test";
|
||||
|
||||
import { submitComposerDraft } from "./composerSubmission";
|
||||
|
||||
describe("submitComposerDraft", () => {
|
||||
it("keeps an oversized draft editable and sends a corrected follow-up", () => {
|
||||
let draft = "x".repeat(PROVIDER_SEND_TURN_MAX_INPUT_CHARS + 1);
|
||||
let validationMessage: string | null = null;
|
||||
const dispatchedDrafts: string[] = [];
|
||||
const preventDefault = vi.fn();
|
||||
|
||||
const submit = () => {
|
||||
const result = submitComposerDraft({
|
||||
prompt: draft,
|
||||
submissionTarget: "provider-turn",
|
||||
event: { preventDefault },
|
||||
onSend: () => {
|
||||
dispatchedDrafts.push(draft);
|
||||
},
|
||||
});
|
||||
validationMessage = result.validationMessage;
|
||||
};
|
||||
|
||||
submit();
|
||||
|
||||
expect(dispatchedDrafts).toEqual([]);
|
||||
expect(draft).toHaveLength(PROVIDER_SEND_TURN_MAX_INPUT_CHARS + 1);
|
||||
expect(validationMessage).toBe(
|
||||
"Prompt is 1 character over the 120,000-character limit. Shorten or split it before sending.",
|
||||
);
|
||||
expect(preventDefault).toHaveBeenCalledOnce();
|
||||
|
||||
draft = "Corrected prompt";
|
||||
submit();
|
||||
|
||||
expect(dispatchedDrafts).toEqual(["Corrected prompt"]);
|
||||
expect(validationMessage).toBeNull();
|
||||
});
|
||||
|
||||
it("allows a draft at the shared character limit through the normal send path", () => {
|
||||
const draft = "x".repeat(PROVIDER_SEND_TURN_MAX_INPUT_CHARS);
|
||||
const onSend = vi.fn();
|
||||
const preventDefault = vi.fn();
|
||||
|
||||
const result = submitComposerDraft({
|
||||
prompt: draft,
|
||||
submissionTarget: "provider-turn",
|
||||
event: { preventDefault },
|
||||
onSend,
|
||||
});
|
||||
|
||||
expect(result).toEqual({ validationMessage: null, didDispatch: true });
|
||||
expect(onSend).toHaveBeenCalledOnce();
|
||||
expect(preventDefault).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("blocks when appended context pushes the provider input over the shared limit", () => {
|
||||
const draft = "x".repeat(PROVIDER_SEND_TURN_MAX_INPUT_CHARS);
|
||||
const onSend = vi.fn();
|
||||
|
||||
const result = submitComposerDraft({
|
||||
prompt: draft,
|
||||
providerInput: `${draft}\n\nTerminal context`,
|
||||
submissionTarget: "provider-turn",
|
||||
event: undefined,
|
||||
onSend,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
validationMessage:
|
||||
"Prompt is 18 characters over the 120,000-character limit. Shorten or split it before sending.",
|
||||
didDispatch: false,
|
||||
});
|
||||
expect(draft).toHaveLength(PROVIDER_SEND_TURN_MAX_INPUT_CHARS);
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
|
||||
const correctedResult = submitComposerDraft({
|
||||
prompt: "Corrected prompt",
|
||||
providerInput: "Corrected prompt\n\nShort terminal context",
|
||||
submissionTarget: "provider-turn",
|
||||
event: undefined,
|
||||
onSend,
|
||||
});
|
||||
|
||||
expect(correctedResult).toEqual({ validationMessage: null, didDispatch: true });
|
||||
expect(onSend).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("does not finish submission when the send boundary rejects composed provider input", () => {
|
||||
const preventDefault = vi.fn();
|
||||
|
||||
const result = submitComposerDraft({
|
||||
prompt: "Sendable raw draft",
|
||||
submissionTarget: "provider-turn",
|
||||
event: { preventDefault },
|
||||
onSend: () => false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({ validationMessage: null, didDispatch: false });
|
||||
expect(preventDefault).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("allows fully composed provider input at the shared character limit", () => {
|
||||
const onSend = vi.fn();
|
||||
|
||||
const result = submitComposerDraft({
|
||||
prompt: "Short draft",
|
||||
providerInput: "x".repeat(PROVIDER_SEND_TURN_MAX_INPUT_CHARS),
|
||||
submissionTarget: "provider-turn",
|
||||
event: undefined,
|
||||
onSend,
|
||||
});
|
||||
|
||||
expect(result).toEqual({ validationMessage: null, didDispatch: true });
|
||||
expect(onSend).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("blocks a generated plan follow-up that exceeds the shared limit", () => {
|
||||
const onSend = vi.fn();
|
||||
|
||||
const result = submitComposerDraft({
|
||||
prompt: "",
|
||||
providerInput: `PLEASE IMPLEMENT THIS PLAN:\n${"x".repeat(
|
||||
PROVIDER_SEND_TURN_MAX_INPUT_CHARS,
|
||||
)}`,
|
||||
submissionTarget: "provider-turn",
|
||||
event: undefined,
|
||||
onSend,
|
||||
});
|
||||
|
||||
expect(result.didDispatch).toBe(false);
|
||||
expect(result.validationMessage).toContain("over the 120,000-character limit");
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("allows surrounding whitespace that the provider turn contract trims", () => {
|
||||
const draft = ` ${"x".repeat(PROVIDER_SEND_TURN_MAX_INPUT_CHARS)} `;
|
||||
const onSend = vi.fn();
|
||||
const preventDefault = vi.fn();
|
||||
|
||||
const result = submitComposerDraft({
|
||||
prompt: draft,
|
||||
submissionTarget: "provider-turn",
|
||||
event: { preventDefault },
|
||||
onSend,
|
||||
});
|
||||
|
||||
expect(result).toEqual({ validationMessage: null, didDispatch: true });
|
||||
expect(onSend).toHaveBeenCalledOnce();
|
||||
expect(preventDefault).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("dispatches pending user input answers on their separate response path", () => {
|
||||
const answer = "x".repeat(PROVIDER_SEND_TURN_MAX_INPUT_CHARS + 1);
|
||||
const onSend = vi.fn();
|
||||
const preventDefault = vi.fn();
|
||||
|
||||
const result = submitComposerDraft({
|
||||
prompt: answer,
|
||||
submissionTarget: "pending-user-input",
|
||||
event: { preventDefault },
|
||||
onSend,
|
||||
});
|
||||
|
||||
expect(result).toEqual({ validationMessage: null, didDispatch: true });
|
||||
expect(onSend).toHaveBeenCalledOnce();
|
||||
expect(preventDefault).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
import { PROVIDER_SEND_TURN_MAX_INPUT_CHARS } from "@t3tools/contracts";
|
||||
|
||||
type ComposerSubmitEvent = { preventDefault: () => void };
|
||||
|
||||
type ComposerSubmissionInput = {
|
||||
prompt: string;
|
||||
providerInput?: string;
|
||||
submissionTarget: "provider-turn" | "pending-user-input";
|
||||
};
|
||||
|
||||
export function getComposerPromptLengthValidationMessage(prompt: string): string | null {
|
||||
const excessCharacters = prompt.trim().length - PROVIDER_SEND_TURN_MAX_INPUT_CHARS;
|
||||
if (excessCharacters <= 0) return null;
|
||||
|
||||
const characterLabel = excessCharacters === 1 ? "character" : "characters";
|
||||
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.`;
|
||||
}
|
||||
|
||||
export function getComposerSubmissionValidationMessage(
|
||||
options: ComposerSubmissionInput,
|
||||
): string | null {
|
||||
return options.submissionTarget === "provider-turn"
|
||||
? getComposerPromptLengthValidationMessage(options.providerInput ?? options.prompt)
|
||||
: null;
|
||||
}
|
||||
|
||||
export function submitComposerDraft(
|
||||
options: ComposerSubmissionInput & {
|
||||
event: ComposerSubmitEvent | undefined;
|
||||
onSend: (event?: ComposerSubmitEvent) => boolean | void;
|
||||
},
|
||||
): { validationMessage: string | null; didDispatch: boolean } {
|
||||
const validationMessage = getComposerSubmissionValidationMessage(options);
|
||||
if (validationMessage) {
|
||||
options.event?.preventDefault();
|
||||
return { validationMessage, didDispatch: false };
|
||||
}
|
||||
|
||||
if (options.onSend(options.event) === false) {
|
||||
options.event?.preventDefault();
|
||||
return { validationMessage: null, didDispatch: false };
|
||||
}
|
||||
return { validationMessage: null, didDispatch: true };
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# Message composer
|
||||
|
||||
Messages can contain up to 120,000 characters. If a draft is longer, T3 Code keeps it in the
|
||||
composer and shows how many characters need to be removed. Shorten the draft or split it into
|
||||
multiple messages, then send again in the same thread.
|
||||
Reference in new issue
Block a user