🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(file): only enforce chat upload file-type whitelist in chat mode (#15884)
Arvin Xu984815cfc63b
diff --git a/src/components/DragUploadZone/useUploadFiles.ts b/src/components/DragUploadZone/useUploadFiles.tsindex fd2f1368fae..e98a9e8ce40 100644--- a/src/components/DragUploadZone/useUploadFiles.ts+++ b/src/components/DragUploadZone/useUploadFiles.ts@@ -5,6 +5,8 @@ import { useVisualMediaUploadAbility } from '@/hooks/useVisualMediaUploadAbilityimport { useFileStore } from '@/store/file';interface UseUploadFilesOptions {+ /** The conversation's agent id. Decides whether the chat-only file-type whitelist applies. */+ agentId: string;model?: string;provider?: string;}@@ -13,11 +15,11 @@ interface UseUploadFilesOptions {* Hook to handle file uploads with visual media support filtering.* Filters out image/video files if the model cannot receive them directly or via fallback.*- * @param options - The model and provider to check for vision support+ * @param options - The agent id (for upload validation scope) plus model/provider for vision support* @returns handleUploadFiles - Callback to handle file uploads*/-export const useUploadFiles = (options: UseUploadFilesOptions = {}) => {- const { model = '', provider = '' } = options;+export const useUploadFiles = (options: UseUploadFilesOptions) => {+ const { agentId, model = '', provider = '' } = options;const { canUploadImage, canUploadVideo } = useVisualMediaUploadAbility(model, provider);const uploadFiles = useFileStore((s) => s.uploadChatFiles);@@ -35,10 +37,10 @@ export const useUploadFiles = (options: UseUploadFilesOptions = {}) => {});if (filteredFiles.length > 0) {- uploadFiles(filteredFiles);+ uploadFiles(filteredFiles, agentId);}},- [canUpload, canUploadImage, canUploadVideo, uploadFiles],+ [agentId, canUpload, canUploadImage, canUploadVideo, uploadFiles],);return { canUploadImage, canUploadVideo, handleUploadFiles };diff --git a/src/features/AgentBuilder/AgentBuilderConversation.tsx b/src/features/AgentBuilder/AgentBuilderConversation.tsxindex ba4b785307f..0aa03f63941 100644--- a/src/features/AgentBuilder/AgentBuilderConversation.tsx+++ b/src/features/AgentBuilder/AgentBuilderConversation.tsx@@ -25,7 +25,7 @@ const AgentBuilderConversation = memo<AgentBuilderConversationProps>(({ agentId// Get agent's model info for vision support checkconst model = useAgentStore((s) => agentByIdSelectors.getAgentModelById(agentId)(s));const provider = useAgentStore((s) => agentByIdSelectors.getAgentModelProviderById(agentId)(s));- const { handleUploadFiles } = useUploadFiles({ model, provider });+ const { handleUploadFiles } = useUploadFiles({ agentId, model, provider });const { allowed: canCreate } = usePermission('create_content');return (diff --git a/src/features/AgentTaskManager/Conversation.tsx b/src/features/AgentTaskManager/Conversation.tsxindex 148392af152..873e3a697f6 100644--- a/src/features/AgentTaskManager/Conversation.tsx+++ b/src/features/AgentTaskManager/Conversation.tsx@@ -55,7 +55,7 @@ const Conversation = memo(() => {const provider = useAgentStore((s) =>agentByIdSelectors.getAgentModelProviderById(currentAgentId)(s),);- const { handleUploadFiles } = useUploadFiles({ model, provider });+ const { handleUploadFiles } = useUploadFiles({ agentId: currentAgentId, model, provider });const handleAgentChange = useCallback((id: string) => {diff --git a/src/features/ChatInput/ActionBar/Plus/index.tsx b/src/features/ChatInput/ActionBar/Plus/index.tsxindex 28c7016e6eb..86d27826559 100644--- a/src/features/ChatInput/ActionBar/Plus/index.tsx+++ b/src/features/ChatInput/ActionBar/Plus/index.tsx@@ -441,7 +441,7 @@ const PlusAction = memo(() => {}setDropdownOpen(false);editor?.focus();- await upload([file]);+ await upload([file], agentId);return false;}}>diff --git a/src/features/ChatInput/ActionBar/Upload/index.tsx b/src/features/ChatInput/ActionBar/Upload/index.tsxindex dfdde978285..604463aaf3a 100644--- a/src/features/ChatInput/ActionBar/Upload/index.tsx+++ b/src/features/ChatInput/ActionBar/Upload/index.tsx@@ -106,7 +106,7 @@ const FileUpload = memo(() => {beforeUpload={async (file) => {setDropdownOpen(false);editor?.focus();- await upload([file]);+ await upload([file], agentId);return false;}}@@ -148,7 +148,7 @@ const FileUpload = memo(() => {setDropdownOpen(false);editor?.focus();- await upload([file]);+ await upload([file], agentId);return false;}}@@ -187,7 +187,7 @@ const FileUpload = memo(() => {setDropdownOpen(false);editor?.focus();- await upload([file]);+ await upload([file], agentId);return false;}}diff --git a/src/features/ChatInput/InputEditor/index.tsx b/src/features/ChatInput/InputEditor/index.tsxindex ed9c1df806b..2a76a015cc6 100644--- a/src/features/ChatInput/InputEditor/index.tsx+++ b/src/features/ChatInput/InputEditor/index.tsx@@ -156,7 +156,7 @@ const InputEditor = memo<{isMentionEnabled &&!heterogeneousName &&categories.some((category) => category.id === 'agent');- const { handleUploadFiles } = useUploadFiles({ model, provider });+ const { handleUploadFiles } = useUploadFiles({ agentId, model, provider });// Listen to editor's paste event for file uploadsusePasteFile(editor, handleUploadFiles);diff --git a/src/features/PageEditor/Copilot/Conversation.tsx b/src/features/PageEditor/Copilot/Conversation.tsxindex bb709527eee..309a15898b3 100644--- a/src/features/PageEditor/Copilot/Conversation.tsx+++ b/src/features/PageEditor/Copilot/Conversation.tsx@@ -41,7 +41,7 @@ const Conversation = memo(() => {const provider = useAgentStore((s) =>agentByIdSelectors.getAgentModelProviderById(currentAgentId)(s),);- const { handleUploadFiles } = useUploadFiles({ model, provider });+ const { handleUploadFiles } = useUploadFiles({ agentId: currentAgentId, model, provider });const handleAgentChange = useCallback((id: string) => {diff --git a/src/features/ResourceManager/components/Editor/FileCopilot.tsx b/src/features/ResourceManager/components/Editor/FileCopilot.tsxindex e219c32e1ea..c33ad23986a 100644--- a/src/features/ResourceManager/components/Editor/FileCopilot.tsx+++ b/src/features/ResourceManager/components/Editor/FileCopilot.tsx@@ -32,7 +32,7 @@ const FileCopilot = memo(() => {const provider = useAgentStore((s) =>agentByIdSelectors.getAgentModelProviderById(currentAgentId)(s),);- const { handleUploadFiles } = useUploadFiles({ model, provider });+ const { handleUploadFiles } = useUploadFiles({ agentId: currentAgentId, model, provider });return (<RightPanel>diff --git a/src/routes/(main)/agent/features/Conversation/index.tsx b/src/routes/(main)/agent/features/Conversation/index.tsxindex af401f6174c..bbb1c5d34e1 100644--- a/src/routes/(main)/agent/features/Conversation/index.tsx+++ b/src/routes/(main)/agent/features/Conversation/index.tsx@@ -6,7 +6,7 @@ import DragUploadZone, { type DroppedFolder, useUploadFiles } from '@/componentsimport Loading from '@/components/Loading/BrandTextLoading';import { insertLocalFolderMentions } from '@/features/ChatInput/InputEditor/insertLocalFolderMentions';import { useAgentStore } from '@/store/agent';-import { agentByIdSelectors, chatConfigByIdSelectors } from '@/store/agent/selectors';+import { agentChatConfigSelectors, agentSelectors } from '@/store/agent/selectors';import { useChatStore } from '@/store/chat';import ConversationArea from './ConversationArea';@@ -19,18 +19,13 @@ const wrapperStyle: React.CSSProperties = {};const ChatConversation = memo(() => {- // ChatConversation sits above the ConversationProvider, so read the routed- // agent from the chat store (set by AgentIdSync, not the hijack-prone agent- // store) and feed it to the scoped `*ById` selectors.- const agentId = useChatStore((s) => s.activeAgentId) || '';- const model = useAgentStore(agentByIdSelectors.getAgentModelById(agentId));- const provider = useAgentStore(agentByIdSelectors.getAgentModelProviderById(agentId));- const isHeterogeneous = useAgentStore(agentByIdSelectors.isAgentHeterogeneousById(agentId));- const isLocalSystemEnabled = useAgentStore(- chatConfigByIdSelectors.isLocalSystemEnabledById(agentId),- );+ const agentId = useAgentStore((s) => s.activeAgentId || '');+ const model = useAgentStore(agentSelectors.currentAgentModel);+ const provider = useAgentStore(agentSelectors.currentAgentModelProvider);+ const isHeterogeneous = useAgentStore(agentSelectors.isCurrentAgentHeterogeneous);+ const isLocalSystemEnabled = useAgentStore(agentChatConfigSelectors.isLocalSystemEnabled);- const { handleUploadFiles } = useUploadFiles({ model, provider });+ const { handleUploadFiles } = useUploadFiles({ agentId, model, provider });const enableLocalFolderMention = isDesktop && (isHeterogeneous || isLocalSystemEnabled);diff --git a/src/routes/(main)/group/features/Conversation/index.tsx b/src/routes/(main)/group/features/Conversation/index.tsxindex f70efd5c7c2..55a2526d78c 100644--- a/src/routes/(main)/group/features/Conversation/index.tsx+++ b/src/routes/(main)/group/features/Conversation/index.tsx@@ -14,9 +14,10 @@ const ChatConversation = memo(() => {const showHeader = useGlobalStore(systemStatusSelectors.showChatHeader);// Get current agent's model info for vision support check+ const agentId = useAgentStore((s) => s.activeAgentId || '');const model = useAgentStore(agentSelectors.currentAgentModel);const provider = useAgentStore(agentSelectors.currentAgentModelProvider);- const { handleUploadFiles } = useUploadFiles({ model, provider });+ const { handleUploadFiles } = useUploadFiles({ agentId, model, provider });return (<DragUploadZone style={{ height: '100%', width: '100%' }} onUploadFiles={handleUploadFiles}>diff --git a/src/routes/(main)/home/features/InputArea/index.tsx b/src/routes/(main)/home/features/InputArea/index.tsxindex 382e3a43a8c..f84275c3463 100644--- a/src/routes/(main)/home/features/InputArea/index.tsx+++ b/src/routes/(main)/home/features/InputArea/index.tsx@@ -100,7 +100,7 @@ const InputArea = () => {const provider = useAgentStore((s) =>agentByIdSelectors.getAgentModelProviderById(resolvedAgentId)(s),);- const { handleUploadFiles } = useUploadFiles({ model, provider });+ const { handleUploadFiles } = useUploadFiles({ agentId: resolvedAgentId, model, provider });// A slot to insert content above the chat input// Override some default behavior of the chat inputdiff --git a/src/store/file/slices/chat/action.test.ts b/src/store/file/slices/chat/action.test.tsindex 0d03fa7bfa7..d000498099b 100644--- a/src/store/file/slices/chat/action.test.ts+++ b/src/store/file/slices/chat/action.test.ts@@ -3,9 +3,24 @@ import { act, renderHook } from '@testing-library/react';import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';import { notification } from '@/components/AntdStaticMethods';+import { agentByIdSelectors } from '@/store/agent/selectors';import { useFileStore as useStore } from '../../store';+const AGENT_ID = 'agent-1';++/** Force the conversation agent into chat / agent / heterogeneous mode for the by-id selectors. */+const mockAgentMode = ({+ enableAgentMode,+ heterogeneous,+}: {+ enableAgentMode: boolean;+ heterogeneous: boolean;+}) => {+ vi.spyOn(agentByIdSelectors, 'getAgentEnableModeById').mockReturnValue(() => enableAgentMode);+ vi.spyOn(agentByIdSelectors, 'isAgentHeterogeneousById').mockReturnValue(() => heterogeneous);+};+vi.mock('zustand/traditional');vi.mock('@lobehub/ui/base-ui', () => ({@@ -21,6 +36,12 @@ vi.mock('@/components/AntdStaticMethods', () => ({},}));+vi.mock('@/services/rag', () => ({+ ragService: {+ parseFileContent: vi.fn().mockResolvedValue(undefined),+ },+}));+vi.mock('i18next', () => ({t: (key: string, options?: { reason?: string }) => {if (key === 'upload.permissionDenied') {@@ -71,7 +92,10 @@ describe('useFileStore:chat', () => {expect(result.current.chatUploadFileList).toEqual([]);});- it('uploadChatFiles should reject unsupported files before upload', async () => {+ it('uploadChatFiles should reject unsupported files before upload in chat mode', async () => {+ // chat mode: agent mode disabled and not a heterogeneous agent+ mockAgentMode({ enableAgentMode: false, heterogeneous: false });+const { result } = renderHook(() => useStore());const uploadWithProgress = vi.fn();@@ -83,10 +107,13 @@ describe('useFileStore:chat', () => {});await act(async () => {- await result.current.uploadChatFiles([- new File(['<svg />'], 'icon.svg', { type: 'image/svg+xml' }),- new File(['zip'], 'archive.zip', { type: 'application/zip' }),- ]);+ await result.current.uploadChatFiles(+ [+ new File(['<svg />'], 'icon.svg', { type: 'image/svg+xml' }),+ new File(['zip'], 'archive.zip', { type: 'application/zip' }),+ ],+ AGENT_ID,+ );});expect(uploadWithProgress).not.toHaveBeenCalled();@@ -94,7 +121,57 @@ describe('useFileStore:chat', () => {expect(toast.error).toHaveBeenCalledWith(expect.any(String));});+ it('uploadChatFiles should allow any file type in agent mode', async () => {+ mockAgentMode({ enableAgentMode: true, heterogeneous: false });++ const { result } = renderHook(() => useStore());+ const uploadWithProgress = vi.fn().mockResolvedValue({ id: 'file-1', url: 'http://x/1' });++ act(() => {+ useStore.setState({+ chatUploadFileList: [],+ uploadWithProgress: uploadWithProgress as any,+ });+ });++ await act(async () => {+ await result.current.uploadChatFiles(+ [new File(['zip'], 'archive.zip', { type: 'application/zip' })],+ AGENT_ID,+ );+ });++ expect(toast.error).not.toHaveBeenCalled();+ expect(uploadWithProgress).toHaveBeenCalledTimes(1);+ });++ it('uploadChatFiles should allow any file type for heterogeneous agents', async () => {+ mockAgentMode({ enableAgentMode: false, heterogeneous: true });++ const { result } = renderHook(() => useStore());+ const uploadWithProgress = vi.fn().mockResolvedValue({ id: 'file-2', url: 'http://x/2' });++ act(() => {+ useStore.setState({+ chatUploadFileList: [],+ uploadWithProgress: uploadWithProgress as any,+ });+ });++ await act(async () => {+ await result.current.uploadChatFiles(+ [new File(['profile'], 'Communication_Notifications.provisionprofile')],+ AGENT_ID,+ );+ });++ expect(toast.error).not.toHaveBeenCalled();+ expect(uploadWithProgress).toHaveBeenCalledTimes(1);+ });+it('shows a permission denied description when upload is rejected by RBAC', async () => {+ mockAgentMode({ enableAgentMode: false, heterogeneous: false });+const { result } = renderHook(() => useStore());const file = new File(['test'], 'test.txt', { type: 'text/plain' });@@ -104,7 +181,7 @@ describe('useFileStore:chat', () => {});await act(async () => {- await result.current.uploadChatFiles([file]);+ await result.current.uploadChatFiles([file], AGENT_ID);});expect(notification.error).toHaveBeenCalledWith({diff --git a/src/store/file/slices/chat/action.ts b/src/store/file/slices/chat/action.tsindex 022b18709bb..3774a9a24ab 100644--- a/src/store/file/slices/chat/action.ts+++ b/src/store/file/slices/chat/action.ts@@ -9,6 +9,8 @@ import { FILE_UPLOAD_BLACKLIST } from '@/const/file';import { fileService } from '@/services/file';import { ragService } from '@/services/rag';import { UPLOAD_NETWORK_ERROR } from '@/services/upload';+import { getAgentStoreState } from '@/store/agent';+import { agentByIdSelectors } from '@/store/agent/selectors';import { type UploadFileListDispatch } from '@/store/file/reducers/uploadFileList';import { uploadFileListReducer } from '@/store/file/reducers/uploadFileList';import { type StoreSetter } from '@/store/types';@@ -140,11 +142,25 @@ export class FileActionImpl {}};- uploadChatFiles = async (rawFiles: File[]): Promise<void> => {+ uploadChatFiles = async (rawFiles: File[], agentId: string): Promise<void> => {const { dispatchChatUploadFileList } = this.#get();// 0. skip file in blacklistconst filteredFiles = rawFiles.filter((file) => !FILE_UPLOAD_BLACKLIST.includes(file.name));- const { supportedFiles, unsupportedFiles } = filterSupportedChatUploadFiles(filteredFiles);++ // The file-type whitelist only makes sense in plain chat mode, where files are fed+ // directly to the model. In agent mode (tool calls) or heterogeneous agents (Claude+ // Code / Codex, etc.) the agent can parse any file via scripts/terminal, so the+ // whitelist must not apply there. We key off the conversation's own agent id rather+ // than the global current agent, because the chat input can be scoped to a different+ // agent than activeAgentId (e.g. another desktop tab). See lobehub/lobehub#15770.+ const agentState = getAgentStoreState();+ const enforceFileTypeWhitelist =+ !agentByIdSelectors.getAgentEnableModeById(agentId)(agentState) &&+ !agentByIdSelectors.isAgentHeterogeneousById(agentId)(agentState);++ const { supportedFiles, unsupportedFiles } = enforceFileTypeWhitelist+ ? filterSupportedChatUploadFiles(filteredFiles)+ : { supportedFiles: filteredFiles, unsupportedFiles: [] as File[] };if (unsupportedFiles.length > 0) {toast.error(diff --git a/src/store/file/slices/chat/uploadGuard.test.ts b/src/store/file/slices/chat/uploadGuard.test.tsindex 92cb7b462ee..f4a2e96fc2a 100644--- a/src/store/file/slices/chat/uploadGuard.test.ts+++ b/src/store/file/slices/chat/uploadGuard.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from 'vitest';-import { isSupportedChatUploadFile } from './uploadGuard';+import { filterSupportedChatUploadFiles, isSupportedChatUploadFile } from './uploadGuard';describe('isSupportedChatUploadFile', () => {it('accepts supported chat image formats', () => {@@ -38,3 +38,15 @@ describe('isSupportedChatUploadFile', () => {).toBe(false);});});++describe('filterSupportedChatUploadFiles', () => {+ it('splits supported and unsupported files by default', () => {+ const png = new File(['image'], 'image.png', { type: 'image/png' });+ const zip = new File(['zip'], 'archive.zip', { type: 'application/zip' });++ const { supportedFiles, unsupportedFiles } = filterSupportedChatUploadFiles([png, zip]);++ expect(supportedFiles).toEqual([png]);+ expect(unsupportedFiles).toEqual([zip]);+ });+});