Skip to content

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

Merge pull request #852 from xAlcahest/fix/dictionary-prompt-echo

Alcahestd3da31d2a5fe
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.js
index 4c88b514e..dc5c62e02 100644
--- a/src/helpers/audioManager.js
+++ b/src/helpers/audioManager.js
@@ -16,6 +16,7 @@ import { shouldSkipTranscriptionApiKey } from "./transcriptionAuth";
import { detectAgentName } from "../config/agentDetection";
import { resolvePrompt } from "../config/prompts";
import { syncService } from "../services/SyncService.js";
+import { matchesDictionaryPrompt } from "../utils/dictionaryEchoFilter.js";
const REASONING_CACHE_TTL = 30000; // 30 seconds
const REALTIME_MODELS = new Set(["gpt-4o-mini-transcribe", "gpt-4o-transcribe"]);
@@ -219,6 +220,10 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
return words.length > 0 ? words.join(", ") : null;
}
+ isDictionaryEcho(text) {
+ return matchesDictionaryPrompt(text, this.getCustomDictionaryPrompt());
+ }
+
setCallbacks({
onStateChange,
onError,
@@ -731,6 +736,9 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
);
if (result.success && result.text) {
+ if (this.isDictionaryEcho(result.text)) {
+ throw new Error("No audio detected");
+ }
const rawText = result.text;
const reasoningStart = performance.now();
const text = await this.processTranscription(result.text, "local");
@@ -1339,6 +1347,9 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
timings.transcriptionProcessingDurationMs = Math.round(performance.now() - transcriptionStart);
const rawText = result.text;
+ if (this.isDictionaryEcho(rawText)) {
+ throw new Error("No audio detected");
+ }
let processedText = result.text;
if (processedText && !this.skipReasoning) {
const reasoningStart = performance.now();
@@ -1542,6 +1553,9 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
const proxyText = result?.text;
if (proxyText && proxyText.trim().length > 0) {
+ if (this.isDictionaryEcho(proxyText)) {
+ throw new Error("No audio detected");
+ }
timings.transcriptionProcessingDurationMs = Math.round(performance.now() - apiCallStart);
const rawText = proxyText;
const reasoningStart = performance.now();
@@ -1682,6 +1696,9 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
// Check for text - handle both empty string and missing field
if (result.text && result.text.trim().length > 0) {
+ if (this.isDictionaryEcho(result.text)) {
+ throw new Error("No audio detected");
+ }
timings.transcriptionProcessingDurationMs = Math.round(performance.now() - apiCallStart);
const rawText = result.text;
@@ -1732,6 +1749,10 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
);
}
} catch (error) {
+ if (error.message === "No audio detected") {
+ throw error;
+ }
+
const isOpenAIMode = !getSettings().useLocalWhisper;
if (allowLocalFallback && isOpenAIMode) {
diff --git a/src/utils/dictionaryEchoFilter.js b/src/utils/dictionaryEchoFilter.js
new file mode 100644
index 000000000..d0847a3ee
--- /dev/null
+++ b/src/utils/dictionaryEchoFilter.js
@@ -0,0 +1,28 @@
+const normalize = (s) =>
+ s
+ .toLowerCase()
+ .replace(/[^\p{L}\p{N}\s]/gu, "")
+ .replace(/\s+/g, " ")
+ .trim();
+
+export function matchesDictionaryPrompt(text, dictionaryPrompt) {
+ if (!text || !dictionaryPrompt) return false;
+
+ const normalizedText = normalize(text);
+ const normalizedPrompt = normalize(dictionaryPrompt);
+
+ if (normalizedText === normalizedPrompt) return true;
+
+ const dictWords = new Set(normalizedPrompt.split(" "));
+ const uniqueTextWords = new Set(normalizedText.split(" "));
+
+ let matchCount = 0;
+ for (const word of uniqueTextWords) {
+ if (dictWords.has(word)) matchCount++;
+ }
+
+ const textComposition = matchCount / uniqueTextWords.size;
+ const dictionaryUsage = matchCount / dictWords.size;
+
+ return textComposition >= 0.9 && dictionaryUsage >= 0.7;
+}
diff --git a/test/helpers/dictionaryEchoFilter.test.js b/test/helpers/dictionaryEchoFilter.test.js
new file mode 100644
index 000000000..192ee5e18
--- /dev/null
+++ b/test/helpers/dictionaryEchoFilter.test.js
@@ -0,0 +1,159 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+
+test("detects verbatim echo of dictionary prompt", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ assert.equal(
+ matchesDictionaryPrompt("OpenWhispr, Parakeet, Alcahest", "OpenWhispr, Parakeet, Alcahest"),
+ true
+ );
+});
+
+test("detects echo when Whisper adds trailing period", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ assert.equal(
+ matchesDictionaryPrompt("OpenWhispr, Parakeet, Alcahest.", "OpenWhispr, Parakeet, Alcahest"),
+ true
+ );
+});
+
+test("detects echo with different capitalization", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ assert.equal(
+ matchesDictionaryPrompt("openwhispr, parakeet, alcahest", "OpenWhispr, Parakeet, Alcahest"),
+ true
+ );
+});
+
+test("detects echo when Whisper strips commas", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ assert.equal(
+ matchesDictionaryPrompt("OpenWhispr Parakeet Alcahest", "OpenWhispr, Parakeet, Alcahest"),
+ true
+ );
+});
+
+test("detects echo with extra whitespace", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ assert.equal(
+ matchesDictionaryPrompt("OpenWhispr, Parakeet, Alcahest", "OpenWhispr, Parakeet, Alcahest"),
+ true
+ );
+});
+
+test("does not flag legitimate speech containing dictionary words", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ assert.equal(
+ matchesDictionaryPrompt(
+ "I just installed OpenWhispr and it works great",
+ "OpenWhispr, Parakeet, Alcahest"
+ ),
+ false
+ );
+});
+
+test("does not flag speech that partially overlaps with dictionary", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ assert.equal(
+ matchesDictionaryPrompt("OpenWhispr, Parakeet", "OpenWhispr, Parakeet, Alcahest"),
+ false
+ );
+});
+
+test("returns false when dictionary prompt is null", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ assert.equal(matchesDictionaryPrompt("some text", null), false);
+});
+
+test("returns false when text is null", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ assert.equal(matchesDictionaryPrompt(null, "OpenWhispr"), false);
+});
+
+test("returns false when both inputs are empty strings", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ assert.equal(matchesDictionaryPrompt("", ""), false);
+});
+
+test("handles single-word dictionary", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ assert.equal(matchesDictionaryPrompt("OpenWhispr", "OpenWhispr"), true);
+ assert.equal(matchesDictionaryPrompt("OpenWhispr is great", "OpenWhispr"), false);
+});
+
+test("handles unicode dictionary words with accents", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ assert.equal(
+ matchesDictionaryPrompt("Müller, François, José", "Müller, François, José"),
+ true
+ );
+ assert.equal(
+ matchesDictionaryPrompt("muller francois jose", "Müller, François, José"),
+ false
+ );
+});
+
+test("handles CJK dictionary words", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ assert.equal(
+ matchesDictionaryPrompt("東京, 大阪", "東京, 大阪"),
+ true
+ );
+});
+
+test("detects repeated echo where Whisper loops the dictionary", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ const dict = "OpenWhispr, Parakeet, Alcahest";
+ const repeated = "OpenWhispr, Parakeet, Alcahest, OpenWhispr, Parakeet, Alcahest";
+ assert.equal(matchesDictionaryPrompt(repeated, dict), true);
+});
+
+test("detects echo with minor Whisper additions among dictionary words", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ const dict = "Alpha, Bravo, Charlie, Delta, Echo, Foxtrot, Golf, Hotel, India, Juliet";
+ const echoWithFiller = "Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliet the";
+ assert.equal(matchesDictionaryPrompt(echoWithFiller, dict), true);
+});
+
+test("does not flag completely unrelated text", async () => {
+ const { matchesDictionaryPrompt } = await import(
+ "../../src/utils/dictionaryEchoFilter.js"
+ );
+ assert.equal(
+ matchesDictionaryPrompt(
+ "The quick brown fox jumps over the lazy dog",
+ "OpenWhispr, Parakeet, Alcahest"
+ ),
+ false
+ );
+});