Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🪃 fix: Remember the Last Code Approval Mode (#15973)
lia-by-librechat[bot]c80f5a709049
diff --git a/client/src/components/Chat/Input/CodeApprovalMenu.tsx b/client/src/components/Chat/Input/CodeApprovalMenu.tsxindex 85446bf57e..13e856a15f 100644--- a/client/src/components/Chat/Input/CodeApprovalMenu.tsx+++ b/client/src/components/Chat/Input/CodeApprovalMenu.tsx@@ -7,6 +7,7 @@ import type { CodeApprovalMode, TConversation } from 'librechat-data-provider';import type { LucideIcon } from 'lucide-react';import type { SetterOrUpdater } from 'recoil';import type { TranslationKeys } from '~/hooks';+import { useCodeApprovalModePreference } from '~/hooks/Agents/codeApprovalPreference';import { useCodeApprovalMode, useLocalize } from '~/hooks';import { cn } from '~/utils';@@ -51,6 +52,7 @@ export default function CodeApprovalMenu({const localize = useLocalize();const queryClient = useQueryClient();const { available, modes, selected } = useCodeApprovalMode(conversation, addedConversation);+ const preference = useCodeApprovalModePreference();const menuStore = Ariakit.useMenuStore({ focusLoop: true, placement: 'top-start' });const isOpen = menuStore.useState('open');@@ -62,11 +64,14 @@ export default function CodeApprovalMenu({* cache, so the pick lands there too, seeding the record from the live* conversation when none exists yet (the resumable transport seeds the same* key optimistically). A chat that has no id yet keeps the pick in- * conversation state alone until the run assigns one. */+ * conversation state alone until the run assigns one. The pick is also this+ * browser's remembered default, so the next chat opens on it rather than back+ * at `ask`; policy is re-checked before it is ever shown or submitted. */const selectMode = (mode: CodeApprovalMode) => {if (!modes.includes(mode)) {return;}+ preference.remember(mode);setConversation((current) =>current == null ? current : { ...current, codeApprovalMode: mode },);diff --git a/client/src/components/Chat/Input/__tests__/CodeApprovalMenu.spec.tsx b/client/src/components/Chat/Input/__tests__/CodeApprovalMenu.spec.tsxindex 4d48c8981e..8a584b561d 100644--- a/client/src/components/Chat/Input/__tests__/CodeApprovalMenu.spec.tsx+++ b/client/src/components/Chat/Input/__tests__/CodeApprovalMenu.spec.tsx@@ -1,7 +1,7 @@import userEvent from '@testing-library/user-event';-import { QueryKeys, Constants } from 'librechat-data-provider';import { render, screen, within } from '@testing-library/react';import { QueryClient, QueryClientProvider } from '@tanstack/react-query';+import { QueryKeys, Constants, LocalStorageKeys } from 'librechat-data-provider';import type { TConversation } from 'librechat-data-provider';import CodeApprovalMenu from '../CodeApprovalMenu';@@ -163,3 +163,28 @@ describe('CodeApprovalMenu', () => {});});});++test("remembers the pick as this browser's default for the next chat", async () => {+ localStorage.clear();+ mockUseCodeApprovalMode.mockReturnValue({+ available: true,+ modes: ['ask', 'acceptEdits'],+ selected: 'ask',+ });+ render(+ <QueryClientProvider client={new QueryClient()}>+ <CodeApprovalMenu+ conversation={conversation}+ setConversation={mockSetConversation}+ disabled={false}+ />+ </QueryClientProvider>,+ );++ await userEvent.click(screen.getByTestId('code-approval-mode'));+ await userEvent.click(await screen.findByText('com_ui_code_approval_accept_edits'));++ expect(localStorage.getItem(LocalStorageKeys.LAST_CODE_APPROVAL_MODE)).toBe(+ JSON.stringify('acceptEdits'),+ );+});diff --git a/client/src/hooks/Agents/__tests__/useCodeApprovalMode.test.ts b/client/src/hooks/Agents/__tests__/useCodeApprovalMode.test.tsindex 35d58e3f7f..688bbf037a 100644--- a/client/src/hooks/Agents/__tests__/useCodeApprovalMode.test.ts+++ b/client/src/hooks/Agents/__tests__/useCodeApprovalMode.test.ts@@ -1,4 +1,6 @@+import { Provider } from 'jotai';import { renderHook } from '@testing-library/react';+import { LocalStorageKeys } from 'librechat-data-provider';import type { TConversation } from 'librechat-data-provider';import useCodeApprovalMode from '../useCodeApprovalMode';@@ -371,3 +373,70 @@ describe('useCodeApprovalMode', () => {expect(result.current.selected).toBe('ask');});});++describe('useCodeApprovalMode remembered pick', () => {+ const withoutMode = { ...conversation, codeApprovalMode: undefined } as TConversation;++ beforeEach(() => {+ jest.clearAllMocks();+ localStorage.clear();+ mockUseAgentsMapContext.mockReturnValue({});+ mockUseAgentToolPermissions.mockReturnValue({+ agent: {+ id: 'agent_1',+ tools: ['execute_code'],+ stateful_code_sessions: true,+ code_environment_id: 'mac',+ },+ });+ mockUseGetAgentsConfig.mockReturnValue({+ agentsConfig: {+ statefulCodeSessions: {+ approvalsEnabled: true,+ approvalModes: ['ask', 'acceptEdits'],+ environments: [+ {+ id: 'mac',+ name: 'Mac',+ type: 'attached',+ configSchema: {+ permissions: {+ fileWrite: { allowed: ['ask', 'allow'], default: 'ask' },+ commandExecution: { allowed: ['ask'], default: 'ask' },+ },+ },+ },+ ],+ },+ },+ });+ });++ test('opens a conversation with no stored mode on the last pick', () => {+ localStorage.setItem(LocalStorageKeys.LAST_CODE_APPROVAL_MODE, JSON.stringify('acceptEdits'));+ const { result } = renderHook(() => useCodeApprovalMode(withoutMode), { wrapper: Provider });+ expect(result.current.selected).toBe('acceptEdits');+ });++ test('a stored conversation mode still wins over the remembered pick', () => {+ localStorage.setItem(LocalStorageKeys.LAST_CODE_APPROVAL_MODE, JSON.stringify('acceptEdits'));+ const { result } = renderHook(+ () => useCodeApprovalMode({ ...conversation, codeApprovalMode: 'ask' } as TConversation),+ { wrapper: Provider },+ );+ expect(result.current.selected).toBe('ask');+ });++ test('falls back to ask when policy no longer allows the remembered pick', () => {+ localStorage.setItem(LocalStorageKeys.LAST_CODE_APPROVAL_MODE, JSON.stringify('fullAccess'));+ const { result } = renderHook(() => useCodeApprovalMode(withoutMode), { wrapper: Provider });+ expect(result.current.modes).not.toContain('fullAccess');+ expect(result.current.selected).toBe('ask');+ });++ test('ignores a corrupted remembered value', () => {+ localStorage.setItem(LocalStorageKeys.LAST_CODE_APPROVAL_MODE, JSON.stringify('nonsense'));+ const { result } = renderHook(() => useCodeApprovalMode(withoutMode), { wrapper: Provider });+ expect(result.current.selected).toBe('ask');+ });+});diff --git a/client/src/hooks/Agents/codeApprovalPreference.ts b/client/src/hooks/Agents/codeApprovalPreference.tsnew file mode 100644index 0000000000..415d9d7936--- /dev/null+++ b/client/src/hooks/Agents/codeApprovalPreference.ts@@ -0,0 +1,33 @@+import { useAtom } from 'jotai';+import { CODE_APPROVAL_MODES, LocalStorageKeys } from 'librechat-data-provider';+import type { CodeApprovalMode } from 'librechat-data-provider';+import { createStorageAtom } from '~/store/jotai-utils';++const preference = createStorageAtom<string | null>(LocalStorageKeys.LAST_CODE_APPROVAL_MODE, null);++function validMode(saved: string | null): CodeApprovalMode | undefined {+ return (CODE_APPROVAL_MODES as readonly string[]).includes(saved ?? '')+ ? (saved as CodeApprovalMode)+ : undefined;+}++/**+ * The mode the reader last picked in this browser, so a new chat opens the way+ * they left the last one instead of falling back to `ask` every time. A+ * disposable hint and never authorization: the caller re-checks it against the+ * modes current policy allows before showing or submitting it, and logout+ * clears it with the rest of this browser's conversation state.+ */+export function useCodeApprovalModePreference() {+ const [saved, setSaved] = useAtom(preference);+ return {+ get: () => validMode(saved),+ remember: (mode: CodeApprovalMode) => {+ try {+ setSaved(mode);+ } catch {+ // Disabled or full browser storage must not prevent an explicit pick.+ }+ },+ };+}diff --git a/client/src/hooks/Agents/useCodeApprovalMode.ts b/client/src/hooks/Agents/useCodeApprovalMode.tsindex 3ee381cd14..1836784291 100644--- a/client/src/hooks/Agents/useCodeApprovalMode.ts+++ b/client/src/hooks/Agents/useCodeApprovalMode.ts@@ -8,6 +8,7 @@ import {} from 'librechat-data-provider';import type { Agent, TAgentsMap, TConfig, TPublicCodeEnvironment } from 'librechat-data-provider';import type { CodeApprovalMode, TConversation } from 'librechat-data-provider';+import { useCodeApprovalModePreference } from './codeApprovalPreference';import useAgentToolPermissions from './useAgentToolPermissions';import useGetAgentsConfig from './useGetAgentsConfig';import { useAgentsMapContext } from '~/Providers';@@ -22,6 +23,7 @@ export default function useCodeApprovalMode(} {const { agentsConfig } = useGetAgentsConfig();const agentsMap = useAgentsMapContext();+ const preference = useCodeApprovalModePreference();const { agent: primaryAgent } = useAgentToolPermissions(conversation?.agent_id);const { agent: addedAgent } = useAgentToolPermissions(addedConversation?.agent_id);const statefulCodeSessions = agentsConfig?.statefulCodeSessions as@@ -81,7 +83,12 @@ export default function useCodeApprovalMode(if (fullAccessAllowed) allowed.add('fullAccess');return CODE_APPROVAL_MODES.filter((mode) => allowed.has(mode));}, [attachedEnvironments, available, codeEnvironments, endpointModes, reachable.complete]);- const requested = conversation?.codeApprovalMode ?? 'ask';+ /** A conversation that carries no mode of its own opens on the reader's last+ * pick in this browser, so choosing `acceptEdits` or `fullAccess` survives a+ * new chat and a reload instead of being re-picked every time. The remembered+ * value is a preference, not a grant: it passes the same policy gate below as+ * a stored one, so a mode current policy no longer allows falls back to `ask`. */+ const requested = conversation?.codeApprovalMode ?? preference.get() ?? 'ask';/*** Fail closed while agent/environment metadata is incomplete. An affirmative* server capability means `ask` is safe to submit even before an attacheddiff --git a/client/src/utils/localStorage.ts b/client/src/utils/localStorage.tsindex 1790068921..1cf590b8ee 100644--- a/client/src/utils/localStorage.ts+++ b/client/src/utils/localStorage.ts@@ -60,7 +60,10 @@ export function clearLocalStorage(skipFirst?: boolean) {key === LocalStorageKeys.LAST_SPEC ||key === LocalStorageKeys.LAST_TOOLS ||key === LocalStorageKeys.LAST_MODEL ||- key === LocalStorageKeys.FILES_TO_DELETE+ key === LocalStorageKeys.FILES_TO_DELETE ||+ /** A permissive code approval default belongs to the account that chose it, not to+ * whoever signs in next on a shared browser. */+ key === LocalStorageKeys.LAST_CODE_APPROVAL_MODE) {localStorage.removeItem(key);}diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.tsindex 73d64858ad..3fc9fecf09 100644--- a/packages/data-provider/src/config.ts+++ b/packages/data-provider/src/config.ts@@ -4270,6 +4270,8 @@ export enum LocalStorageKeys {PIN_WEB_SEARCH_ = 'PIN_WEB_SEARCH_',/** Pin state for Code Interpreter per conversation ID */PIN_CODE_INTERPRETER_ = 'PIN_CODE_INTERPRETER_',+ /** Key for the last selected code approval mode */+ LAST_CODE_APPROVAL_MODE = 'lastCodeApprovalMode',}export enum ForkOptions {