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): stop sending a locked Advanced tier from a stored choice (#15410)
Reinier van der Leer9481d59d6334
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/useConnectionPicker.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/useConnectionPicker.tsindex ee52178f81..80ff52c10a 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/useConnectionPicker.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/useConnectionPicker.ts@@ -12,6 +12,7 @@ import {tiersAreDistinct,visibleOffers,} from "./helpers";+import { useLockedTierFallback } from "./useLockedTierFallback";export function useConnectionPicker() {const query = useGetV2ListChatConnections({@@ -45,6 +46,7 @@ export function useConnectionPicker() {const fallback = choosable.find((offer) => offer.is_default) ?? choosable[0];const active = selected ?? fallback;+ useLockedTierFallback(active);const { connect: connectChatGPT, isPending: isConnecting } = useOAuthConnect({provider: "codex",diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/useLockedTierFallback.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/useLockedTierFallback.tsnew file mode 100644index 0000000000..2f359b74db--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/useLockedTierFallback.ts@@ -0,0 +1,16 @@+import type { AIConnectionOffer } from "@/app/api/__generated__/models/aIConnectionOffer";+import { useEffect } from "react";++import { useCopilotUIStore } from "../../../../store";+import { tierLock } from "./helpers";++// A plan that lapsed with Advanced chosen leaves "advanced" stored, and every+// send is then refused. The server locks Advanced only on a definite "not entitled".+export function useLockedTierFallback(active: AIConnectionOffer | undefined) {+ const { copilotLlmModel, setCopilotLlmModel } = useCopilotUIStore();+ const advancedLocked = Boolean(tierLock(active, "advanced"));+ useEffect(() => {+ if (advancedLocked && copilotLlmModel === "advanced")+ setCopilotLlmModel("standard");+ }, [advancedLocked, copilotLlmModel, setCopilotLlmModel]);+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/LockedTierFallback.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/LockedTierFallback.test.tsxnew file mode 100644index 0000000000..c9f0f2f69f--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/LockedTierFallback.test.tsx@@ -0,0 +1,100 @@+import { getGetV2ListChatConnectionsMockHandler200 } from "@/app/api/__generated__/endpoints/chat/chat.msw";+import type { AIConnectionOffer } from "@/app/api/__generated__/models/aIConnectionOffer";+import { server } from "@/mocks/mock-server";+import { render, screen, waitFor } from "@/tests/integrations/test-utils";+import { beforeEach, describe, expect, it } from "vitest";++import { useCopilotUIStore } from "../../../../store";+import { ConnectionPicker } from "../ConnectionPicker/ConnectionPicker";++const ADVANCED_LOCK = "A Max plan or higher is required for Advanced.";++function platformOffer(advancedLocked: boolean): AIConnectionOffer {+ return {+ offer_id: "platform:deployment",+ auth_provider: "platform",+ provider_family: "autogpt",+ display_name: "AutoGPT Platform",+ auth_method: "deployment",+ credential_id: null,+ backed_by_label: "Your AutoGPT plan",+ description: "New chats are backed by your AutoGPT plan.",+ state: "ready",+ selectable: true,+ is_default: true,+ tiers: [+ {+ tier: "standard",+ label: "Balanced",+ selectable: true,+ display_model: "sonnet-5",+ },+ {+ tier: "advanced",+ label: "Advanced",+ selectable: !advancedLocked,+ display_model: "opus-5",+ lock_reason: advancedLocked ? ADVANCED_LOCK : null,+ },+ ],+ limitations: [],+ } as AIConnectionOffer;+}++beforeEach(() => {+ window.localStorage.clear();+});++describe("a stored tier the server has locked", () => {+ it("falls back to Balanced, in the store and in storage, so sends stop being refused", async () => {+ useCopilotUIStore.getState().setCopilotLlmModel("advanced");+ server.use(+ getGetV2ListChatConnectionsMockHandler200({+ offers: [platformOffer(true)],+ }),+ );++ render(<ConnectionPicker />);++ await waitFor(() =>+ expect(useCopilotUIStore.getState().copilotLlmModel).toBe("standard"),+ );+ expect(window.localStorage.getItem("copilot-model")).toBe("standard");+ expect(+ await screen.findByRole("button", { name: /Balanced/ }),+ ).toBeDefined();+ });++ it("keeps Advanced when the server offers it", async () => {+ useCopilotUIStore.getState().setCopilotLlmModel("advanced");+ server.use(+ getGetV2ListChatConnectionsMockHandler200({+ offers: [platformOffer(false)],+ }),+ );++ render(<ConnectionPicker />);++ expect(+ await screen.findByRole("button", { name: /Advanced/ }),+ ).toBeDefined();+ expect(useCopilotUIStore.getState().copilotLlmModel).toBe("advanced");+ });++ it("leaves a stored Balanced alone while Advanced is locked", async () => {+ useCopilotUIStore.getState().setCopilotLlmModel("standard");+ server.use(+ getGetV2ListChatConnectionsMockHandler200({+ offers: [platformOffer(true)],+ }),+ );++ render(<ConnectionPicker />);++ expect(+ await screen.findByRole("button", { name: /Balanced/ }),+ ).toBeDefined();+ expect(useCopilotUIStore.getState().copilotLlmModel).toBe("standard");+ expect(window.localStorage.getItem("copilot-model")).toBe("standard");+ });+});