Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend/onboarding): cap the typed brain dump at the backend's 20,000-character limit (#15960)

Reinier van der Leerd7afa9bd66fa
diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/brain-dump.test.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/brain-dump.test.tsx
index f57a848cdb..0f3904f0fc 100644
--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/brain-dump.test.tsx
+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/brain-dump.test.tsx
@@ -4,6 +4,7 @@ import {
getGetBrainDumpStatusMockHandler200,
getUploadBrainDumpPartMockHandler200,
} from "@/app/api/__generated__/endpoints/brain-dump/brain-dump.msw";
+import openapi from "@/app/api/openapi.json";
import { server } from "@/mocks/mock-server";
import {
cleanup,
@@ -502,6 +503,83 @@ describe("onboarding brain dump — typed fallback", () => {
).toBeDefined();
expect(getUserMedia).not.toHaveBeenCalled();
});
+
+ it("caps a long paste at the backend's limit and says how much is left", async () => {
+ const backendLimit = finalizeTextMaxLength();
+ landOnPainPointsStep();
+
+ render(<OnboardingPage />);
+ await screen.findByText(DUMP_HEADLINE);
+ await userEvent.click(screen.getByRole("button", { name: "Write" }));
+ const box = await screen.findByPlaceholderText<HTMLTextAreaElement>(
+ "What repeats every week? What would you hand off first?",
+ );
+
+ await userEvent.click(box);
+ await userEvent.paste("a".repeat(backendLimit + 50));
+
+ expect(box.value).toHaveLength(backendLimit);
+ expect(screen.getByText("0 characters left")).toBeDefined();
+ });
+
+ it("returns to the text with the limit spelled out when finalize refuses it as too long", async () => {
+ server.use(
+ http.post(
+ "http://localhost:3000/api/proxy/api/onboarding/brain-dump/finalize",
+ () =>
+ HttpResponse.json(
+ {
+ detail: [
+ {
+ type: "string_too_long",
+ loc: ["body", "text"],
+ msg: "String should have at most 20000 characters",
+ ctx: { max_length: 20000 },
+ },
+ ],
+ },
+ { status: 422 },
+ ),
+ ),
+ );
+ landOnPainPointsStep();
+
+ render(<OnboardingPage />);
+ await screen.findByText(DUMP_HEADLINE);
+ await userEvent.click(screen.getByRole("button", { name: "Write" }));
+ await userEvent.type(
+ await screen.findByPlaceholderText(
+ "What repeats every week? What would you hand off first?",
+ ),
+ "Invoices every Monday",
+ );
+ await userEvent.click(screen.getByRole("button", { name: "I'm done" }));
+
+ expect(
+ await screen.findByText(
+ "That's over the 20,000-character limit; trim it and try again.",
+ ),
+ ).toBeDefined();
+ expect(
+ screen.getByPlaceholderText<HTMLTextAreaElement>(
+ "What repeats every week? What would you hand off first?",
+ ).value,
+ ).toBe("Invoices every Monday");
+ expect(screen.queryByText("Your recording is safe. Try again.")).toBeNull();
+
+ await userEvent.type(
+ screen.getByPlaceholderText(
+ "What repeats every week? What would you hand off first?",
+ ),
+ "{Backspace}",
+ );
+
+ expect(
+ screen.queryByText(
+ "That's over the 20,000-character limit; trim it and try again.",
+ ),
+ ).toBeNull();
+ });
});
describe("onboarding brain dump — skip", () => {
@@ -1005,3 +1083,12 @@ describe("onboarding step map integrity", () => {
});
});
});
+
+function finalizeTextMaxLength() {
+ const limit =
+ openapi.components.schemas.FinalizeRequest.properties.text.anyOf.find(
+ (branch) => branch.type === "string",
+ )?.maxLength;
+ expect(limit).toBeTypeOf("number");
+ return limit!;
+}
diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/BrainDumpStep.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/BrainDumpStep.tsx
index c02409adfa..2a536a8d0c 100644
--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/BrainDumpStep.tsx
+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/BrainDumpStep.tsx
@@ -117,6 +117,7 @@ export function BrainDumpStep() {
<TypedFallback
value={dump.typedText}
onChange={dump.setTypedText}
+ isTooLong={dump.isTypedTooLong}
onSubmit={dump.handleSubmitTyped}
/>
) : isRecording ? (
diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/TypedFallback.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/TypedFallback.tsx
index af5c402c31..41a7d7b3c9 100644
--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/TypedFallback.tsx
+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/TypedFallback.tsx
@@ -3,29 +3,65 @@
import { Button } from "@/components/atoms/Button/Button";
import { FadeIn } from "@/components/atoms/FadeIn/FadeIn";
import { Input } from "@/components/atoms/Input/Input";
+import { Text } from "@/components/atoms/Text/Text";
+import {
+ MAX_TYPED_CHARS,
+ TYPED_TOO_LONG_COPY,
+ typedCharsLeftHint,
+} from "../helpers";
interface Props {
value: string;
onChange: (value: string) => void;
onSubmit: () => void;
+ isTooLong: boolean;
}
-export function TypedFallback({ value, onChange, onSubmit }: Props) {
+const LIMIT_HINT_ID = "brain-dump-text-limit";
+
+export function TypedFallback({ value, onChange, onSubmit, isTooLong }: Props) {
return (
<FadeIn className="w-full">
<div className="flex w-full flex-col items-center gap-3">
- <Input
- id="brain-dump-text"
- type="textarea"
- label="Your brain dump"
- hideLabel
- rows={6}
- className="rounded-xl border-zinc-200"
- placeholder="What repeats every week? What would you hand off first?"
- value={value}
- onChange={(e) => onChange(e.target.value)}
- autoFocus
- />
+ <div className="flex w-full flex-col gap-1">
+ <Input
+ id="brain-dump-text"
+ type="textarea"
+ label="Your brain dump"
+ hideLabel
+ rows={6}
+ className="rounded-xl border-zinc-200"
+ placeholder="What repeats every week? What would you hand off first?"
+ value={value}
+ onChange={(e) => onChange(e.target.value)}
+ maxLength={MAX_TYPED_CHARS}
+ aria-describedby={LIMIT_HINT_ID}
+ wrapperClassName="mb-0"
+ autoFocus
+ />
+ {isTooLong ? (
+ <Text
+ variant="small"
+ tone="danger"
+ role="alert"
+ id={LIMIT_HINT_ID}
+ className="text-left"
+ unmask
+ >
+ {TYPED_TOO_LONG_COPY}
+ </Text>
+ ) : (
+ <Text
+ variant="small"
+ tone="muted"
+ id={LIMIT_HINT_ID}
+ className="min-h-[1.125rem] text-right"
+ unmask
+ >
+ {typedCharsLeftHint(value.length)}
+ </Text>
+ )}
+ </div>
<Button
size="small"
onClick={onSubmit}
diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/helpers.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/helpers.ts
index 7d7d2d1a32..0008bd8a69 100644
--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/helpers.ts
@@ -1,3 +1,6 @@
+import type { HTTPValidationError } from "@/app/api/__generated__/models/hTTPValidationError";
+import { ApiError } from "@/lib/autogpt-server-api/helpers";
+
// Pure helpers for the brain dump. Timings and copy live here so the hook
// stays about behaviour and the component stays about pixels.
@@ -8,6 +11,12 @@
// even then everything captured is kept.
export const HARD_STOP_SECONDS = 1800;
+// The backend refuses longer typed text with a 422 (`MAX_TYPED_CHARS` in
+// `onboarding_dump/models.py`); a test pins the two together.
+export const MAX_TYPED_CHARS = 20_000;
+const TYPED_CHARS_LEFT_FROM = 2_000;
+export const TYPED_TOO_LONG_COPY = `That's over the ${formatCount(MAX_TYPED_CHARS)}-character limit; trim it and try again.`;
+
// Every 3s the recorder hands us a chunk to persist. Small enough that a
// crash costs almost nothing, large enough not to thrash IndexedDB.
export const TIMESLICE_MS = 3000;
@@ -111,3 +120,24 @@ export function isInsufficientDump(errorCode: unknown) {
INSUFFICIENT_DUMP_ERROR_CODES.some((code) => code === errorCode)
);
}
+
+export function typedCharsLeftHint(length: number) {
+ const left = Math.max(0, MAX_TYPED_CHARS - length);
+ if (left > TYPED_CHARS_LEFT_FROM) return null;
+ return `${formatCount(left)} ${left === 1 ? "character" : "characters"} left`;
+}
+
+export function isTypedTextTooLong(error: unknown) {
+ if (!(error instanceof ApiError) || error.status !== 422) return false;
+ const detail: HTTPValidationError["detail"] = error.response?.detail;
+ return (
+ Array.isArray(detail) &&
+ detail.some(
+ (issue) => issue.type === "string_too_long" && issue.loc.includes("text"),
+ )
+ );
+}
+
+function formatCount(count: number) {
+ return count.toLocaleString("en-US");
+}
diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useBrainDumpStep.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useBrainDumpStep.ts
index 5cd8661b29..4d09d0e61b 100644
--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useBrainDumpStep.ts
+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useBrainDumpStep.ts
@@ -10,6 +10,7 @@ import { trackBrainDump } from "@/services/onboarding/brain-dump-analytics";
import {
BRAIN_DUMP_HEADLINE,
isInsufficientDump,
+ isTypedTextTooLong,
SILENCE_NUDGE_SECONDS,
} from "./helpers";
import {
@@ -63,6 +64,7 @@ export function useBrainDumpStep() {
mimeType: string;
durationSecs: number;
} | null>(null);
+ const [isTypedTooLong, setIsTypedTooLong] = useState(false);
const retryCountRef = useRef(0);
// A submit in flight owns the screen: "Skip for now" must not race
// `completeAndAdvance` to `nextStep()`, or the wizard advances twice and
@@ -299,10 +301,16 @@ export function useBrainDumpStep() {
setScreen("rest");
}
+ function handleTypedTextChange(text: string) {
+ setTypedText(text);
+ setIsTypedTooLong(false);
+ }
+
async function handleSubmitTyped() {
const text = typedText.trim();
if (!text) return;
isSubmittingRef.current = true;
+ setIsTypedTooLong(false);
try {
await submitTyped(text);
} finally {
@@ -333,8 +341,12 @@ export function useBrainDumpStep() {
setScreen(rejected ? "insufficient" : "failed");
return;
}
- } catch {
- setScreen("failed");
+ } catch (error) {
+ // Retrying the same text would fail the same way, so the user goes
+ // back to it with the limit spelled out.
+ const tooLong = isTypedTextTooLong(error);
+ setIsTypedTooLong(tooLong);
+ setScreen(tooLong ? "typing" : "failed");
return;
}
trackBrainDump("brain_dump_completed", {
@@ -485,7 +497,7 @@ export function useBrainDumpStep() {
insufficientMode,
submittedMode,
typedText,
- setTypedText,
+ setTypedText: handleTypedTextChange,
elapsedSeconds: recorder.elapsedSeconds,
isOffline: recorder.isOffline,
isSavedLocally: recorder.isSavedLocally,
@@ -500,6 +512,7 @@ export function useBrainDumpStep() {
handleRetry,
handleSkip,
handleSubmitTyped,
+ isTypedTooLong,
handleResumeRecovered,
handleDiscardRecovered,
handleTypeInsteadOfRecovered,