Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

8.6 KB
Raw Blame
export function resolveModeReachability({ mode, provider, model, isCloud, isSelfHosted }) {
  if (mode === "openwhispr") return isCloud;
  if (mode === "self-hosted") return isSelfHosted;

  const hasModel = (model?.trim()?.length ?? 0) > 0;
  if (mode === "local") return hasModel;
  if (mode === "providers" || mode === "enterprise") {
    return !!provider?.trim() && hasModel;
  }
  return false;
}

export function resolveDictationAgentReachability({
  useDictationAgent,
  dictationAgentMode,
  dictationAgentProvider,
  dictationAgentModel,
  isCloudAgent,
  isSelfHostedAgent,
}) {
  if (!useDictationAgent) return false;
  return resolveModeReachability({
    mode: dictationAgentMode,
    provider: dictationAgentProvider,
    model: dictationAgentModel,
    isCloud: isCloudAgent,
    isSelfHosted: isSelfHostedAgent,
  });
}

// Picks which model receives a captured screenshot, or drops it. An
// explicitly configured vision override is trusted without a capability check
// (custom and OpenRouter model ids aren't in the registry); an override that
// is toggled on but never configured inherits the agent's own config, so it
// falls through to the base rules rather than forcing an image onto a
// possibly text-only model. Dropping the image always beats failing the
// dictation.
export function resolveAgentImageTarget({
  hasScreenContext,
  visionOverrideActive,
  visionProviderImageWired,
  baseProviderImageWired,
  isCloudAgent,
  baseModelSupportsVision,
}) {
  if (!hasScreenContext) {
    return { attach: false, useVisionOverride: false };
  }
  if (visionOverrideActive) {
    // Configured but unable to send images: drop rather than quietly
    // redirecting the screenshot to a model the user didn't choose.
    return visionProviderImageWired
      ? { attach: true, useVisionOverride: true }
      : { attach: false, useVisionOverride: false };
  }
  // Cloud defers the vision-model choice to the server's vision chain.
  if (baseProviderImageWired && (isCloudAgent || baseModelSupportsVision)) {
    return { attach: true, useVisionOverride: false };
  }
  return { attach: false, useVisionOverride: false };
}

// Decides which reasoning path ("agent" | "cleanup" | "skip") a finished
// dictation takes. A recording started via the voice agent hotkey always takes
// the agent path — no wake word needed — and never falls back to cleanup.
export function resolveDictationTranslationReachability({
  useDictationTranslation,
  translationTargetLanguage,
  translationMode,
  translationProvider,
  translationModel,
  isCloudTranslation,
  isSelfHostedTranslation,
}) {
  if (!useDictationTranslation) return false;
  if (!translationTargetLanguage?.trim()) return false;
  return resolveModeReachability({
    mode: translationMode,
    provider: translationProvider,
    model: translationModel,
    isCloud: isCloudTranslation,
    isSelfHosted: isSelfHostedTranslation,
  });
}

export function resolveModeProvider({ isCloud, mode, provider }) {
  switch (mode) {
    case "openwhispr":
      return isCloud ? "openwhispr" : undefined;
    case "local":
      return "local";
    case "self-hosted":
      return undefined;
    case "providers":
    case "enterprise":
      return provider?.trim() || undefined;
    default:
      return undefined;
  }
}

export function resolveDictationAgentProvider({
  isCloudAgent,
  dictationAgentMode,
  dictationAgentProvider,
}) {
  return resolveModeProvider({
    isCloud: isCloudAgent,
    mode: dictationAgentMode,
    provider: dictationAgentProvider,
  });
}

function resolveModeDisplayProvider(mode, provider) {
  if (mode === "openwhispr") return "openwhispr";
  if (mode === "local") return "local";
  if (mode === "self-hosted") return "self-hosted";
  return provider?.trim() || "none";
}

export function resolveDictationAgentDisplayProvider({
  dictationAgentMode,
  dictationAgentProvider,
}) {
  return resolveModeDisplayProvider(dictationAgentMode, dictationAgentProvider);
}

export function resolveTranslationProviderId({
  isCloudTranslation,
  translationMode,
  translationProvider,
}) {
  return resolveModeProvider({
    isCloud: isCloudTranslation,
    mode: translationMode,
    provider: translationProvider,
  });
}

export function resolveTranslationDisplayProvider({ translationMode, translationProvider }) {
  return resolveModeDisplayProvider(translationMode, translationProvider);
}

const ARABIC_SCRIPT_RE = /\p{Script=Arabic}/u;

// Wake-word cues gate on the explicit dictation language, then the language
// detected by STT. Some local and streaming providers do not report their
// auto-detected language, so Arabic script is a deterministic hint before the
// UI language fallback. Explicit and provider-reported languages stay
// authoritative, preventing foreign-language cues from broadening those modes.
export function resolveWakeWordLanguage(
  { preferredLanguage, uiLanguage },
  detectedLanguage,
  transcript
) {
  const language = typeof preferredLanguage === "string" ? preferredLanguage.trim() : "";
  if (language && language.toLowerCase() !== "auto") return language;
  const detected = typeof detectedLanguage === "string" ? detectedLanguage.trim() : "";
  if (detected && detected.toLowerCase() !== "auto") return detected;
  if (typeof transcript === "string" && ARABIC_SCRIPT_RE.test(transcript)) return "ar";
  return typeof uiLanguage === "string" ? uiLanguage : undefined;
}

// The renderer-side source of truth for what the main process gates the Agent
// companion pill on. Assistant wins over translation: a voice-agent request is
// explicit user intent even if a stale translation flag survived.
export function resolveLifecycleInputKind({ voiceAgentRequested, translationRequested }) {
  if (voiceAgentRequested) return "assistant";
  if (translationRequested) return "translation";
  return "dictation";
}

// Decides which reasoning path ("translation" | "agent" | "cleanup" | "skip")
// a finished dictation takes. A recording started via the voice assistant
// hotkey always takes the agent path — no wake word needed, and it wins over
// translation, as in resolveLifecycleInputKind. Standalone commands stream into
// the assistant panel (which resolves the Voice Assistant scope itself and
// reports configuration problems in-conversation), so the dictation
// agent's reachability only gates selection edits — that check happens at the
// selection disposition, not here. A translation recording degrades to
// cleanup instead: the transcript is still a useful dictation without the
// translation step.
export function resolveDictationRouteKind({
  cleanupReachable,
  agentReachable,
  agentInvoked,
  voiceAgentRequested,
  translationRequested,
  translationReachable,
}) {
  if (voiceAgentRequested) {
    return "agent";
  }
  if (translationRequested) {
    if (translationReachable) return "translation";
    return cleanupReachable ? "cleanup" : "skip";
  }
  if (agentReachable && agentInvoked) {
    return "agent";
  }
  if (cleanupReachable) {
    return "cleanup";
  }
  return "skip";
}

// A dictation skipped while cleanup is switched on goes out raw, which looks like
// a cleanup that changed nothing, so it carries a notice. `shownKey` names the
// broken setup the user was last warned about: the same one warns once, while a
// different mode or provider, or signing out, warns again. Once cleanup is off or
// reachable the warning is forgotten (`forget`), so the next broken setup warns.
// Signed out of OpenWhispr Cloud gets its own message, since the fix is signing in.
// The "llm" surface gives it the same friendly cleanup toast as a provider failure.
export function cleanupUnconfiguredNotice(shownKey, { skipped, cleanupReachable, settings }) {
  if (!settings.useCleanupModel || cleanupReachable) return { forget: true, notice: null };
  if (!skipped) return { forget: false, notice: null };
  const signedOut =
    settings.cleanupMode === "openwhispr" &&
    settings.cleanupCloudMode === "openwhispr" &&
    !settings.isSignedIn;
  const setupKey = [
    signedOut ? "signedOut" : "unconfigured",
    settings.cleanupMode || "",
    settings.cleanupProvider || "",
  ].join("\0");
  if (setupKey === shownKey) return { forget: false, notice: null };
  return {
    forget: false,
    notice: signedOut
      ? {
          message: "AI cleanup uses OpenWhispr Cloud, but you're signed out.",
          messageKey: "hooks.audioRecording.cleanupSignedOut",
          surface: "llm",
          settingsTarget: "llms",
          setupKey,
        }
      : {
          message: "AI cleanup is on, but no cleanup model is set up.",
          messageKey: "hooks.audioRecording.cleanupUnconfigured",
          surface: "llm",
          settingsTarget: "llms",
          setupKey,
        },
  };
}