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.

2.0 KB
Raw Blame
import isEqual from 'fast-deep-equal';
import { useEffect } from 'react';
import { useTranslation } from 'react-i18next';

import {
  type EffortKey,
  resolveReasoningEffortValue,
} from '@/features/ChatInput/hooks/useReasoningEffortControl';
import { aiModelSelectors, useAiInfraStore } from '@/store/aiInfra';

/**
 * The reasoning effort a model would run at if picked now: the user's saved
 * per-model default, else the model's fallback level. Returns `undefined` for
 * models without an effort param and while the saved value is still loading,
 * so a row never flashes the fallback before the real value arrives.
 *
 * Values come from the runtime-state response, which seeds every enabled
 * model's entry in one read. `ensureModelReasoningConfig` is only a fallback
 * for models the snapshot missed; it is a no-op once a key is cached
 * (including "nothing saved").
 */
export const useModelEffortLabel = (model: string, provider: string): string | undefined => {
  const { t } = useTranslation('chat');
  const reasoningParams = useAiInfraStore(
    aiModelSelectors.modelReasoningExtendParams(model, provider),
    isEqual,
  );
  // modelReasoningExtendParams only returns reasoning-family params, so every
  // entry other than reasoningMode is an effort key
  const effortKey = reasoningParams.find((param) => param !== 'reasoningMode') as
    EffortKey | undefined;

  const ensureModelReasoningConfig = useAiInfraStore((s) => s.ensureModelReasoningConfig);
  useEffect(() => {
    if (effortKey) void ensureModelReasoningConfig(model, provider);
  }, [effortKey, ensureModelReasoningConfig, model, provider]);

  const loaded = useAiInfraStore(aiModelSelectors.isModelReasoningConfigLoaded(model, provider));
  const config = useAiInfraStore(aiModelSelectors.modelReasoningConfig(model, provider), isEqual);

  if (!effortKey || !loaded) return undefined;

  const value = resolveReasoningEffortValue(model, effortKey, config);

  return value ? t(`reasoningEffort.levels.${value}`) : undefined;
};