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/copilot): Use onboarding "call you" name in copilot greeting (#13520)

Reinier van der Leer7142e1182d34
diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/page.test.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/page.test.tsx
index 9c74b6e46d..4b3667bd29 100644
--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/page.test.tsx
+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/page.test.tsx
@@ -1,5 +1,12 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import { cleanup, render, screen } from "@/tests/integrations/test-utils";
+import {
+ cleanup,
+ render,
+ screen,
+ waitFor,
+} from "@/tests/integrations/test-utils";
+import { server } from "@/mocks/mock-server";
+import { http, HttpResponse } from "msw";
import OnboardingPage from "../page";
import { useOnboardingWizardStore } from "../store";
@@ -34,10 +41,28 @@ vi.mock("next/navigation", () => ({
}));
let mockSupabaseState = { isLoggedIn: true, isUserLoading: false };
+const mockRefreshSession = vi.fn(() => Promise.resolve({ user: null }));
vi.mock("@/lib/supabase/hooks/useSupabase", () => ({
- useSupabase: () => ({ ...mockSupabaseState, user: null }),
+ useSupabase: () => ({
+ ...mockSupabaseState,
+ user: null,
+ refreshSession: mockRefreshSession,
+ }),
}));
+// The onboarding page writes preferred_name through the server route (the
+// browser Supabase client has no session — persistSession: false — so
+// client-side auth.updateUser silently fails; regression caught in manual QA).
+const authUserPutBodies: unknown[] = [];
+function mockAuthUserRoute() {
+ server.use(
+ http.put("/api/auth/user", async ({ request }) => {
+ authUserPutBodies.push(await request.json());
+ return HttpResponse.json({ user: { id: "u1" } });
+ }),
+ );
+}
+
vi.mock("@/app/api/__generated__/endpoints/onboarding/onboarding", () => ({
getV1OnboardingState: () =>
Promise.resolve({ status: 200, data: { completedSteps: [] } }),
@@ -84,6 +109,9 @@ beforeEach(() => {
mockFlagValue = false;
mockSubscriptionTier = "NO_TIER";
mockSupabaseState = { isLoggedIn: true, isUserLoading: false };
+ authUserPutBodies.length = 0;
+ mockAuthUserRoute();
+ mockRefreshSession.mockClear();
routerReplace.mockClear();
useOnboardingWizardStore.getState().reset();
window.sessionStorage.removeItem(STEP_STORAGE_KEY);
@@ -260,6 +288,37 @@ describe("OnboardingPage — flag-gated SubscriptionStep", () => {
expect(useOnboardingWizardStore.getState().currentStep).toBe(1);
});
+ it("saves the onboarding name to auth metadata on Preparing-step submit", async () => {
+ // Regression for OPEN-3200: the "what should I call you?" answer was
+ // only sent to the business-understanding store, so the copilot
+ // greeting (which reads auth user_metadata) never used it.
+ mockFlagValue = false;
+ window.sessionStorage.setItem(STEP_STORAGE_KEY, "4");
+ useOnboardingWizardStore.setState({
+ name: " Reinier ",
+ role: "Engineering",
+ painPoints: [],
+ });
+ currentSearchParams = new URLSearchParams("step=4");
+ render(<OnboardingPage />);
+ expect(await screen.findByTestId("step-preparing")).toBeDefined();
+ await waitFor(() => {
+ expect(authUserPutBodies).toEqual([{ preferred_name: "Reinier" }]);
+ });
+ await waitFor(() => {
+ expect(mockRefreshSession).toHaveBeenCalled();
+ });
+ });
+
+ it("does not touch auth metadata when the wizard has no name", async () => {
+ mockFlagValue = false;
+ window.sessionStorage.setItem(STEP_STORAGE_KEY, "4");
+ currentSearchParams = new URLSearchParams("step=4");
+ render(<OnboardingPage />);
+ expect(await screen.findByTestId("step-preparing")).toBeDefined();
+ expect(authUserPutBodies).toEqual([]);
+ });
+
it("preserves form data on mount (zustand persist; no reset-on-init)", async () => {
// Regression test for the 422 caused by init's old `reset()` wiping
// name/role on every mount. With zustand persist, refreshing mid-wizard
diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/useOnboardingPage.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/useOnboardingPage.ts
index 6c0d20b6cc..758c163986 100644
--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/useOnboardingPage.ts
+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/useOnboardingPage.ts
@@ -54,7 +54,7 @@ function clearHighestStep() {
export function useOnboardingPage() {
const router = useRouter();
const searchParams = useSearchParams();
- const { isLoggedIn, isUserLoading } = useSupabase();
+ const { isLoggedIn, isUserLoading, refreshSession } = useSupabase();
const currentStep = useOnboardingWizardStore((s) => s.currentStep);
const goToStep = useOnboardingWizardStore((s) => s.goToStep);
@@ -200,16 +200,32 @@ export function useOnboardingPage() {
// the profile is only ever submitted here, once, on reaching Preparing.
// Guard against an empty name so a stray Preparing visit can't blank a
// previously-saved profile.
- if (!name.trim()) return;
+ const trimmedName = name.trim();
+ if (!trimmedName) return;
postV1SubmitOnboardingProfile({
- user_name: name,
+ user_name: trimmedName,
user_role: role,
pain_points: painPoints,
}).catch(() => {
// Best effort — profile data is non-critical for accessing copilot
});
- }, [currentStep, preparingStep]);
+
+ // Also store the chosen name in auth user_metadata so the copilot
+ // greeting (getGreetingName) uses it; refresh the cached session user
+ // so the new name shows up right after onboarding without a reload.
+ // Goes through the server route because the browser Supabase client has
+ // no session (persistSession: false) and can't call auth.updateUser.
+ fetch("/api/auth/user", {
+ method: "PUT",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ preferred_name: trimmedName }),
+ })
+ .then((res) => (res.ok ? refreshSession() : undefined))
+ .catch(() => {
+ // Best effort — the greeting falls back to existing metadata
+ });
+ }, [currentStep, preparingStep, refreshSession]);
async function handlePreparingComplete() {
for (let attempt = 0; attempt < 3; attempt++) {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/__tests__/helpers.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/__tests__/helpers.test.ts
new file mode 100644
index 0000000000..f5037282d3
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/__tests__/helpers.test.ts
@@ -0,0 +1,42 @@
+import type { User } from "@supabase/supabase-js";
+import { describe, expect, it } from "vitest";
+import { getGreetingName } from "../helpers";
+
+function makeUser(metadata: Record<string, unknown>, email?: string): User {
+ return { user_metadata: metadata, email } as unknown as User;
+}
+
+describe("getGreetingName", () => {
+ it("prefers the name chosen during onboarding (preferred_name)", () => {
+ const user = makeUser(
+ { preferred_name: "Reinier", full_name: "R. van der Leer" },
+ );
+ expect(getGreetingName(user)).toBe("Reinier");
+ });
+
+ it("ignores a blank preferred_name", () => {
+ const user = makeUser({ preferred_name: " ", full_name: "Jane Doe" });
+ expect(getGreetingName(user)).toBe("Jane");
+ });
+
+ it("uses the first name from full_name when no preferred_name is set", () => {
+ const user = makeUser({ full_name: "Jane Doe" });
+ expect(getGreetingName(user)).toBe("Jane");
+ });
+
+ it("falls back to the name metadata field", () => {
+ const user = makeUser({ name: "John Smith" });
+ expect(getGreetingName(user)).toBe("John");
+ });
+
+ it("falls back to the email prefix when metadata has no name", () => {
+ const user = makeUser({}, "[email protected]");
+ expect(getGreetingName(user)).toBe("jane.doe");
+ });
+
+ it('falls back to "there" without a user', () => {
+ expect(getGreetingName(null)).toBe("there");
+ expect(getGreetingName(undefined)).toBe("there");
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/components/EditNameDialog/EditNameDialog.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/components/EditNameDialog/EditNameDialog.tsx
index 70d9670f5c..8ad6328f51 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/components/EditNameDialog/EditNameDialog.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/components/EditNameDialog/EditNameDialog.tsx
@@ -30,10 +30,11 @@ export function EditNameDialog({ currentName }: Props) {
setIsSaving(true);
try {
+ // preferred_name is also written by onboarding's "What should I call you?"
const res = await fetch("/api/auth/user", {
method: "PUT",
headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ full_name: trimmed }),
+ body: JSON.stringify({ preferred_name: trimmed }),
});
if (!res.ok) {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/components/EditNameDialog/__tests__/EditNameDialog.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/components/EditNameDialog/__tests__/EditNameDialog.test.tsx
index 89029f211e..e2dd2bb069 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/components/EditNameDialog/__tests__/EditNameDialog.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/components/EditNameDialog/__tests__/EditNameDialog.test.tsx
@@ -22,9 +22,10 @@ vi.mock("@/lib/supabase/hooks/useSupabase", () => ({
}),
}));
-function mockUpdateNameSuccess() {
+function mockUpdateNameSuccess(onBody?: (body: unknown) => void) {
server.use(
- http.put("/api/auth/user", () => {
+ http.put("/api/auth/user", async ({ request }) => {
+ onBody?.(await request.json());
return HttpResponse.json({ user: { id: "u1" } });
}),
);
@@ -67,8 +68,12 @@ describe("EditNameDialog", () => {
expect(input.value).toBe("Alice");
});
- test("saves name via API route and closes dialog", async () => {
- mockUpdateNameSuccess();
+ test("saves the name as preferred_name via API route and closes dialog", async () => {
+ // preferred_name is also written by onboarding's "What should I call you?"
+ let requestBody: unknown;
+ mockUpdateNameSuccess((body) => {
+ requestBody = body;
+ });
render(<EditNameDialog currentName="Alice" />);
const input = await openDialogAndGetInput();
@@ -78,6 +83,7 @@ describe("EditNameDialog", () => {
await waitFor(() => {
expect(mockRefreshSession).toHaveBeenCalled();
});
+ expect(requestBody).toEqual({ preferred_name: "Bob" });
expect(mockToast).toHaveBeenCalledWith({ title: "Name updated" });
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/helpers.ts
index fa4baa4838..7a8ac246b3 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/helpers.ts
@@ -98,8 +98,14 @@ export function getSuggestionThemes(
export function getGreetingName(user?: User | null) {
if (!user) return "there";
const metadata = user.user_metadata as Record<string, unknown> | undefined;
+ // preferred_name is what the user answered to onboarding's "What should I
+ // call you?" — it wins over provider-supplied names, and is used verbatim.
+ const preferredName = metadata?.preferred_name;
const fullName = metadata?.full_name;
const name = metadata?.name;
+ if (typeof preferredName === "string" && preferredName.trim()) {
+ return preferredName.trim();
+ }
if (typeof fullName === "string" && fullName.trim()) {
return fullName.split(" ")[0];
}
diff --git a/autogpt_platform/frontend/src/app/api/auth/user/__tests__/route.test.ts b/autogpt_platform/frontend/src/app/api/auth/user/__tests__/route.test.ts
new file mode 100644
index 0000000000..9f1be00f12
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/api/auth/user/__tests__/route.test.ts
@@ -0,0 +1,54 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mockUpdateUser = vi.hoisted(() =>
+ vi.fn(() => Promise.resolve({ data: { user: { id: "u1" } }, error: null })),
+);
+
+vi.mock("@/lib/supabase/server/getServerSupabase", () => ({
+ getServerSupabase: () =>
+ Promise.resolve({ auth: { updateUser: mockUpdateUser } }),
+}));
+
+import { PUT } from "../route";
+
+function makeRequest(body: unknown) {
+ return new Request("http://localhost/api/auth/user", {
+ method: "PUT",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify(body),
+ });
+}
+
+describe("PUT /api/auth/user", () => {
+ beforeEach(() => {
+ mockUpdateUser.mockClear();
+ });
+
+ it("updates preferred_name metadata", async () => {
+ const res = await PUT(makeRequest({ preferred_name: " Rein " }));
+ expect(res.status).toBe(200);
+ expect(mockUpdateUser).toHaveBeenCalledWith({
+ data: { preferred_name: "Rein" },
+ });
+ });
+
+ it("still updates full_name metadata", async () => {
+ const res = await PUT(makeRequest({ full_name: "Jane Doe" }));
+ expect(res.status).toBe(200);
+ expect(mockUpdateUser).toHaveBeenCalledWith({
+ data: { full_name: "Jane Doe" },
+ });
+ });
+
+ it("still updates email", async () => {
+ const res = await PUT(makeRequest({ email: "[email protected]" }));
+ expect(res.status).toBe(200);
+ expect(mockUpdateUser).toHaveBeenCalledWith({ email: "[email protected]" });
+ });
+
+ it("rejects a body without any supported field", async () => {
+ const res = await PUT(makeRequest({ something: "else" }));
+ expect(res.status).toBe(400);
+ expect(mockUpdateUser).not.toHaveBeenCalled();
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/api/auth/user/route.ts b/autogpt_platform/frontend/src/app/api/auth/user/route.ts
index 63cef27fc5..a78f61cfd5 100644
--- a/autogpt_platform/frontend/src/app/api/auth/user/route.ts
+++ b/autogpt_platform/frontend/src/app/api/auth/user/route.ts
@@ -23,25 +23,39 @@ export async function PUT(request: Request) {
return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
}
- const { email: rawEmail, full_name: rawFullName } = body as {
+ const {
+ email: rawEmail,
+ full_name: rawFullName,
+ preferred_name: rawPreferredName,
+ } = body as {
email?: unknown;
full_name?: unknown;
+ preferred_name?: unknown;
};
const email = typeof rawEmail === "string" ? rawEmail.trim() : undefined;
const fullName =
typeof rawFullName === "string" ? rawFullName.trim() : undefined;
+ const preferredName =
+ typeof rawPreferredName === "string"
+ ? rawPreferredName.trim()
+ : undefined;
- if (!email && !fullName) {
+ if (!email && !fullName && !preferredName) {
return NextResponse.json(
- { error: "Email or full_name is required" },
+ { error: "Email, full_name or preferred_name is required" },
{ status: 400 },
);
}
const updatePayload: Parameters<typeof supabase.auth.updateUser>[0] = {};
if (email) updatePayload.email = email;
- if (fullName) updatePayload.data = { full_name: fullName };
+ if (fullName || preferredName) {
+ updatePayload.data = {
+ ...(fullName && { full_name: fullName }),
+ ...(preferredName && { preferred_name: preferredName }),
+ };
+ }
const { data, error } = await supabase.auth.updateUser(updatePayload);