Skip to content

🤯 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.

3.4 KB
Raw Blame
import { useModelSupportAudio } from '@/hooks/useModelSupportAudio';
import { useModelSupportToolUse } from '@/hooks/useModelSupportToolUse';
import { useModelSupportVideo } from '@/hooks/useModelSupportVideo';
import { useModelSupportVision } from '@/hooks/useModelSupportVision';
import { useAgentStore } from '@/store/agent';
import { agentByIdSelectors } from '@/store/agent/selectors';
import { aiModelSelectors, useAiInfraStore } from '@/store/aiInfra';
import { serverConfigSelectors, useServerConfigStore } from '@/store/serverConfig';

interface MultimodalUnderstandingMediaAbilityInput {
  enableMultimodalUnderstanding: boolean;
  fallbackConfigured: boolean;
  fallbackModelAbilities?: {
    audio?: boolean;
    video?: boolean;
    vision?: boolean;
  };
  supportToolUse: boolean;
}

export const getMultimodalUnderstandingMediaAbility = ({
  enableMultimodalUnderstanding,
  fallbackConfigured,
  fallbackModelAbilities,
  supportToolUse,
}: MultimodalUnderstandingMediaAbilityInput) => {
  const canUseMultimodalUnderstanding =
    enableMultimodalUnderstanding && fallbackConfigured && supportToolUse;

  return {
    audio: canUseMultimodalUnderstanding && fallbackModelAbilities?.audio !== false,
    video: canUseMultimodalUnderstanding && fallbackModelAbilities?.video !== false,
    vision: canUseMultimodalUnderstanding && fallbackModelAbilities?.vision !== false,
  };
};

export const useMediaUploadAbility = (model: string, provider: string, agentId?: string) => {
  const supportVision = useModelSupportVision(model, provider);
  const supportVideo = useModelSupportVideo(model, provider);
  const supportAudio = useModelSupportAudio(model, provider);
  const supportToolUse = useModelSupportToolUse(model, provider);
  const enableMultimodalUnderstanding = useServerConfigStore(
    serverConfigSelectors.enableMultimodalUnderstanding,
  );
  const multimodalUnderstanding = useServerConfigStore(
    serverConfigSelectors.multimodalUnderstanding,
  );
  const fallbackModel = useAiInfraStore(
    aiModelSelectors.getEnabledModelById(
      multimodalUnderstanding?.model ?? '',
      multimodalUnderstanding?.provider ?? '',
    ),
  );
  const fallbackConfigured = !!(multimodalUnderstanding?.model && multimodalUnderstanding.provider);
  const fallbackAbility = getMultimodalUnderstandingMediaAbility({
    enableMultimodalUnderstanding,
    fallbackConfigured,
    fallbackModelAbilities: fallbackModel?.abilities,
    supportToolUse,
  });

  // In agent mode (tool calls) or heterogeneous agents (Claude Code / Codex, etc.) the agent
  // can parse any file via scripts/terminal, so the upload should not be gated on the model's
  // own multimodal ability. Mirror the store's `enforceFileTypeWhitelist` bypass in
  // `uploadChatFiles` so the input UI doesn't silently drop audio/video/image the agent could
  // still handle (e.g. .m4a on a non-audio model). See lobehub/lobehub#15770.
  const bypassMediaGate = useAgentStore(
    (s) =>
      !!agentId &&
      (agentByIdSelectors.getAgentEnableModeById(agentId)(s) ||
        agentByIdSelectors.isAgentHeterogeneousById(agentId)(s)),
  );

  if (bypassMediaGate) {
    return {
      canUploadAudio: true,
      canUploadImage: true,
      canUploadVideo: true,
    };
  }

  return {
    canUploadAudio: supportAudio || fallbackAbility.audio,
    canUploadImage: supportVision || fallbackAbility.vision,
    canUploadVideo: supportVideo || fallbackAbility.video,
  };
};