Skip to content

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

3.0 KB
Raw Blame
// Pure orchestration for the cleanup-then-translate chain. No imports: callers
// inject the actual cleanup/translate calls and logging so this stays node-testable.

function normalizeComparableText(value) {
  if (typeof value !== "string") return value;
  return value.trim().replace(/\s+/g, " ");
}

// Shared guard for reasoning/cleanup output (#1616): a whitespace-only reply counts
// as empty and must never replace existing text. Also used by the agent/cleanup
// call sites in audioManager and the history-retry path in ControlPanel.
export function hasTextContent(value) {
  return typeof value === "string" && value.trim().length > 0;
}

// Whether the translate step should run: skip only when an explicit source language
// equals the target. "auto" (or empty, treated as auto) always translates.
export function shouldRunTranslateStep(sourceLanguage, targetLanguage) {
  return (sourceLanguage || "auto") === "auto" || sourceLanguage !== targetLanguage;
}

// Step 1 (optional cleanup) soft-fails to the input text; Step 2 translates unless
// shouldTranslate is false. usedCloudReasoning tracks whether a cloud step actually ran;
// translated tracks whether the output really reached the target language.
export async function executeTranslationChain({
  text,
  cleanupReachable,
  cleanupIsCloud = false,
  runCleanup,
  runTranslate,
  shouldTranslate,
  translateIsCloud = false,
  onCleanupError,
  onEmptyTranslate,
  onUnchangedTranslate,
}) {
  let out = text;
  let usedCloudReasoning = false;
  let translated = false;

  if (cleanupReachable) {
    try {
      const cleaned = await runCleanup(out);
      if (hasTextContent(cleaned)) out = cleaned;
      // Cloud cleanup counts as cloud reasoning once its call succeeds, even if it
      // returned empty text.
      if (cleanupIsCloud) usedCloudReasoning = true;
    } catch (cleanupError) {
      // Rejected output still came from a successful cloud call.
      if (cleanupIsCloud && cleanupError?.code === "CLEANUP_OUTPUT_INVALID") {
        usedCloudReasoning = true;
      }
      if (onCleanupError) onCleanupError(cleanupError);
    }
  }

  if (shouldTranslate) {
    const translateResult = await runTranslate(out);
    const normalizedResult = normalizeComparableText(translateResult);
    const translationChanged = normalizedResult !== normalizeComparableText(out);
    if (normalizedResult && translationChanged) {
      out = translateResult;
      translated = true;
    } else if (normalizedResult) {
      if (onUnchangedTranslate) onUnchangedTranslate();
    } else if (onEmptyTranslate) {
      onEmptyTranslate();
    }
    if (translateIsCloud) usedCloudReasoning = true;
  }

  return { text: out, usedCloudReasoning, translated };
}

// Keep the chain result only when it produced text; an empty/missing result
// preserves the input so a dictation is never overwritten with nothing.
export function resolveTranslatedText(previousText, chainResult) {
  return hasTextContent(chainResult?.text) ? chainResult.text : previousText;
}