Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix(dictation): strip a dictionary list recited after the speech (#2581) (#2586)
Mitchell Etzela7c64aa6d889
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.jsindex d517b4f08..94e58712d 100644--- a/src/helpers/audioManager.js+++ b/src/helpers/audioManager.js@@ -128,6 +128,7 @@ import {dictionaryEchoError,matchesDictionaryPrompt,payloadSendsDictionaryBias,+ stripTrailingDictionaryEcho,} from "../utils/dictionaryEchoFilter.js";import { dictionaryPromptLimit, trimDictionaryPrompt } from "../utils/dictionaryPromptCap.js";import {@@ -857,6 +858,21 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor););}+ // Drops a hint list the model recited after the speech (#2581). Pass the+ // prompt the request actually carried: a capped prompt can only echo itself.+ // Snippet triggers beside the dictionary never start or count toward a recital.+ stripTrailingDictionaryEcho(text, sentPrompt) {+ const stripped = stripTrailingDictionaryEcho(text, sentPrompt, this.getCustomDictionaryArray());+ if (stripped !== text) {+ logger.debug(+ "Stripped trailing dictionary echo",+ { originalLength: text.length, strippedLength: stripped.length },+ "transcription"+ );+ }+ return stripped;+ }+setCallbacks({onStateChange,onError,@@ -3573,11 +3589,11 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);});timings.transcriptionProcessingDurationMs = Math.round(performance.now() - transcriptionStart);- const rawText = result.text;- if (this.isDictionaryEcho(rawText)) {+ if (this.isDictionaryEcho(result.text)) {throw dictionaryEchoError();}- let processedText = result.text;+ const rawText = this.stripTrailingDictionaryEcho(result.text, dictionaryPrompt);+ let processedText = rawText;if (processedText) {const reasoningStart = performance.now();const agentName = getAgentName();@@ -3789,6 +3805,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);throw new Error(`${proxySpec.displayName} transcription is unavailable in this window`);}const apiCallStart = performance.now();+ const proxyDictionaryPrompt = this.getWhisperPrompt(apiSettings);const proxyPayload = proxySpec.buildPayload({audioBuffer: await optimizedAudio.arrayBuffer(),model,@@ -3796,7 +3813,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);apiSettings,managedResolution,mimeType: optimizedAudio.type || "audio/webm",- dictionaryPrompt: this.getWhisperPrompt(apiSettings),+ dictionaryPrompt: proxyDictionaryPrompt,keyterms: this.getKeyterms().map((t) => t.trim().slice(0, 50)).filter(Boolean)@@ -3818,11 +3835,15 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);// message (net::ERR_*), so classify the rebuilt error here.throw classifyProxyFailure(errorFromIpcResult(result));}- const proxyText = result?.text;+ const sendsDictionaryBias = payloadSendsDictionaryBias(proxyPayload);+ const proxyText = this.stripTrailingDictionaryEcho(+ result?.text,+ sendsDictionaryBias ? proxyDictionaryPrompt : null+ );if (!proxyText?.trim()) {throw new Error(`No text transcribed - ${proxySpec.displayName} response was empty`);}- if (payloadSendsDictionaryBias(proxyPayload) && this.isDictionaryEcho(proxyText)) {+ if (sendsDictionaryBias && this.isDictionaryEcho(result.text)) {throw dictionaryEchoError();}timings.transcriptionProcessingDurationMs = Math.round(performance.now() - apiCallStart);@@ -3936,6 +3957,14 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);formData.append("keywords[]", keyword);}}+ // The terms the model could recite. gpt-transcribe got them as keywords[]+ // plus the prompt's overflow; when the overflow was capped, count only the+ // keywords.+ const sentDictionaryPrompt = usesKeywords+ ? trimmedPrompt.truncated+ ? dictionaryKeywords(dictionary).join(", ")+ : dictionary+ : dictionaryPrompt;const shouldStream = this.shouldStreamTranscription(model, provider);if (shouldStream) {@@ -4091,10 +4120,13 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);throw dictionaryEchoError();}timings.transcriptionProcessingDurationMs = Math.round(performance.now() - apiCallStart);- const rawText = result.text;+ const rawText = this.stripTrailingDictionaryEcho(+ result.text,+ model === "orukeet-v0.1.0" ? null : sentDictionaryPrompt+ );const reasoningStart = performance.now();- const text = await this.processTranscription(result.text, "openai", wasCancelled);+ const text = await this.processTranscription(rawText, "openai", wasCancelled);timings.reasoningProcessingDurationMs = Math.round(performance.now() - reasoningStart);const source = (await this.isReasoningAvailable()) ? "openai-reasoned" : "openai";diff --git a/src/utils/dictionaryEchoFilter.js b/src/utils/dictionaryEchoFilter.jsindex bb20e2c8f..5b25fefb2 100644--- a/src/utils/dictionaryEchoFilter.js+++ b/src/utils/dictionaryEchoFilter.js@@ -1,3 +1,5 @@+import { getChineseScriptPromptBias } from "./chineseScript.js";+const normalize = (s) =>s.toLowerCase()@@ -26,6 +28,10 @@ const MIN_CONSECUTIVE_TERM_RUN = 3;const PROMPT_DELIMITER_RE = /[,、,]/;const TRAILING_DELIMITER_RE = /[,、,]\s*$/;+// The hint list's entries, normalized, in the order the prompt carries them.+const promptTerms = (dictionaryPrompt) =>+ dictionaryPrompt.split(PROMPT_DELIMITER_RE).map(normalize).filter(Boolean);+const hasLoopedWord = (textWords) => {const counts = new Map();for (const word of textWords) {@@ -40,15 +46,11 @@ const hasLoopedWord = (textWords) => {// the prompt's own order — a literal continuation of the hint list, however long.const matchesConsecutiveTermRun = (textWords, dictionaryPrompt) => {const promptSequence = [];- let termIndex = 0;- for (const term of dictionaryPrompt.split(PROMPT_DELIMITER_RE)) {- const normalizedTerm = normalize(term);- if (!normalizedTerm) continue;- for (const word of normalizedTerm.split(" ")) {+ promptTerms(dictionaryPrompt).forEach((term, termIndex) => {+ for (const word of term.split(" ")) {promptSequence.push({ word, termIndex });}- termIndex++;- }+ });for (let start = 0; start + textWords.length <= promptSequence.length; start++) {let offset = 0;while (offset < textWords.length && promptSequence[start + offset].word === textWords[offset]) {@@ -126,6 +128,78 @@ export function isLikelyDictionaryPromptFragment(text, dictionaryPrompt) {return analyzeDictionaryPromptFragment(text, dictionaryPrompt).isPromptFragment;}+// Prompt-conditioned models can recite the hint list after the speech ends+// ("... Thanks, you. OpenWhispr, n8n."). matchesDictionaryPrompt cannot see+// that: the real speech dilutes its word ratios (#2581). The recited tail is a+// sentence of its own that copies the prompt in its own consecutive order,+// starting on a custom-dictionary entry, with at least two of them. It must+// open the prompt, which is where observed recitals start, or, when it is+// MIN_PROMPT_END_ECHO_TERMS+ long, close it: a Whisper decoder keeps only the+// tail of a long prompt (dictionaryPromptCap). A shorter run from mid-list is+// kept, since the dictionary stores entries oldest first and auto-learn and+// imports add names in the order people say them. Snippet triggers ride in+// the same prompt after the dictionary (getDictionaryHintWords), so a recital+// may run on into them, but they are phrases people say and never start or+// count toward one (#1889). A whole-text echo is left to matchesDictionaryPrompt.+const MIN_TRAILING_ECHO_TERMS = 2;+const MIN_PROMPT_END_ECHO_TERMS = 5;+// ASCII terminators need trailing whitespace so "Node.js" stays one term.+const SENTENCE_BREAK_RE = /[.!?]\s+|[。!?]\s*/g;+// An abbreviation ("etc.", "Prof.", "incl.", "z.B.", "т.е."), an initial, a list+// marker ("1.") or an ellipsis ends in a period without ending the sentence, so+// the text after it is not a tail.+const NON_TERMINAL_PERIOD_RE =+ /(?:(?:^|[\s(])(?:etc|vs|mrs?|ms|dr|st|jr|sra?|prof|incl|esp|approx|ca|cf|al|no|nr|ex|ej|vgl|bzw|usw|\p{L}|\d{1,2})|\p{L}\.\p{L})$|\.$/iu;+// mergeWhisperPrompt sends `${bias} ${dictionary}`; the bias is not a term list.+const SCRIPT_PROMPT_BIASES = ["simplified", "traditional"].map(getChineseScriptPromptBias);++const withoutScriptBias = (prompt) => {+ const bias = SCRIPT_PROMPT_BIASES.find((candidate) => prompt.startsWith(candidate));+ return bias ? prompt.slice(bias.length) : prompt;+};++export function stripTrailingDictionaryEcho(text, sentPrompt, dictionary) {+ if (typeof text !== "string" || typeof sentPrompt !== "string" || !Array.isArray(dictionary)) {+ return text;+ }++ const dictionaryTerms = new Set(+ dictionary.filter((term) => typeof term === "string").flatMap(promptTerms)+ );+ const sentTerms = promptTerms(withoutScriptBias(sentPrompt));+ const countDictionaryTerms = (terms) => terms.filter((term) => dictionaryTerms.has(term)).length;+ if (countDictionaryTerms(sentTerms) < MIN_TRAILING_ECHO_TERMS) return text;+ const termIndexes = new Map();+ sentTerms.forEach((term, index) => {+ if (!termIndexes.has(term)) termIndexes.set(term, index);+ });++ const body = text.trimEnd();+ let tailStart = -1;+ for (const match of body.matchAll(SENTENCE_BREAK_RE)) {+ const end = match.index + match[0].length;+ if (end >= body.length) continue;+ if (match[0][0] === "." && NON_TERMINAL_PERIOD_RE.test(body.slice(0, match.index))) continue;+ tailStart = end;+ }+ if (tailStart <= 0) return text;++ const tail = promptTerms(body.slice(tailStart));+ if (!dictionaryTerms.has(tail[0]) || countDictionaryTerms(tail) < MIN_TRAILING_ECHO_TERMS) {+ return text;+ }+ const tailIndexes = tail.map((term) => termIndexes.get(term));+ for (let i = 1; i < tailIndexes.length; i++) {+ if (tailIndexes[i] !== tailIndexes[i - 1] + 1) return text;+ }+ const opensPrompt = tailIndexes[0] === 0;+ const closesPrompt =+ tailIndexes.length >= MIN_PROMPT_END_ECHO_TERMS && tailIndexes.at(-1) === sentTerms.length - 1;+ if (!opensPrompt && !closesPrompt) return text;++ return body.slice(0, tailStart).trimEnd();+}+// A provider that never received the dictionary can't echo it back: the echo// check only applies when the outgoing payload actually carried dictionary// bias (Tinfoil's `prompt`, Mistral's `contextBias`, xAI/Gemini's `keyterms`).diff --git a/test/helpers/audioManagerTrailingDictionaryEcho.test.js b/test/helpers/audioManagerTrailingDictionaryEcho.test.jsnew file mode 100644index 000000000..5a79c68e9--- /dev/null+++ b/test/helpers/audioManagerTrailingDictionaryEcho.test.js@@ -0,0 +1,223 @@+const test = require("node:test");+const assert = require("node:assert/strict");+const { loadAudioManager } = require("./harness/audioManager");++// #2581 through the OpenAI-compatible batch path: the server recites the+// dictionary prompt after the real speech, and the recited tail must not reach+// the saved transcript.++function respondWith(t, text) {+ const originalFetch = globalThis.fetch;+ const prompts = [];+ globalThis.fetch = async (endpoint, init) => {+ prompts.push(init.body.get("prompt"));+ return {+ ok: true,+ status: 200,+ headers: { get: () => "application/json" },+ text: async () => JSON.stringify({ text }),+ };+ };+ t.after(() => {+ globalThis.fetch = originalFetch;+ });+ return prompts;+}++test("OpenAI-compatible transcription drops a dictionary list recited after the speech", async (t) => {+ const { setSettings, createManager } = await loadAudioManager(t, {+ cachePrefix: "openwhispr-trailing-dictionary-echo-test-",+ settingsKey: "__trailingDictionaryEchoSettings",+ });+ // The prompt is built by the real getCustomDictionaryPrompt/getWhisperPrompt+ // from these settings, snippet triggers included.+ const useDictionary = (customDictionary, snippets = []) =>+ setSettings({+ useLocalWhisper: false,+ allowLocalFallback: false,+ cloudTranscriptionProvider: "openai",+ customDictionary,+ snippets,+ });+ const audioBlob = new Blob([new ArrayBuffer(8)], { type: "audio/webm" });+ const makeManager = (dictionary, model = "whisper-1", snippets = []) => {+ useDictionary(dictionary, snippets);+ return createManager({+ getEffectiveSttLanguage: () => "auto",+ getTranscriptionModel: () => model,+ getTranscriptionEndpoint: () => "https://stt.example.test/v1/audio/transcriptions",+ getAPIKey: async () => "test-key",+ getKeyterms: () => [],+ shouldStreamTranscription: () => false,+ processTranscription: async (text) => text,+ isReasoningAvailable: async () => false,+ });+ };++ await t.test("the recited tail is removed from text and rawText", async () => {+ respondWith(t, "Appreciate the time. Thanks, all. Thanks, you. OpenWhispr, n8n.");++ const result = await makeManager(["OpenWhispr", "n8n"]).processWithOpenAIAPI(audioBlob, {});++ assert.equal(result.success, true);+ assert.equal(result.rawText, "Appreciate the time. Thanks, all. Thanks, you.");+ assert.equal(result.text, "Appreciate the time. Thanks, all. Thanks, you.");+ });++ await t.test("a whole-response echo is still discarded as before", async () => {+ respondWith(t, "OpenWhispr, n8n.");++ await assert.rejects(makeManager(["OpenWhispr", "n8n"]).processWithOpenAIAPI(audioBlob, {}), {+ code: "DICTIONARY_ECHO",+ });+ });++ await t.test("only terms the capped prompt carried count as an echo", async (t) => {+ const terms = Array.from(+ { length: 100 },+ (_, i) => `SpecializedTerm${String(i).padStart(3, "0")}`+ );+ // Recited from the first entry on, past where the cap cut the prompt: against+ // the full list this would be stripped.+ const recital = `We shipped it. ${terms.slice(0, 60).join(", ")}.`;+ const prompts = respondWith(t, recital);++ const result = await makeManager(terms).processWithOpenAIAPI(audioBlob, {});++ assert.ok(prompts[0].startsWith("SpecializedTerm000"), "the prompt opens the list");+ assert.ok(!prompts[0].includes("SpecializedTerm059"), "fixture must cap the prompt");+ assert.equal(result.rawText, recital);+ });++ await t.test("a snippet trigger said as its own sentence is kept", async () => {+ respondWith(t, "Text him. On my way, see you.");+ const manager = makeManager(["OpenWhispr", "n8n"], "whisper-1", [+ { trigger: "on my way, see you", replacement: "On my way, see you soon!" },+ ]);++ const result = await manager.processWithOpenAIAPI(audioBlob, {});++ assert.equal(result.rawText, "Text him. On my way, see you.");+ });++ await t.test("gpt-transcribe compares against the terms it sent as keywords[]", async (t) => {+ const prompts = respondWith(t, "Appreciate the time. Thanks, you. OpenWhispr, n8n.");++ const result = await makeManager(["OpenWhispr", "n8n"], "gpt-transcribe").processWithOpenAIAPI(+ audioBlob,+ {}+ );++ assert.equal(prompts[0], null, "the dictionary must ride keywords[], not the prompt");+ assert.equal(result.rawText, "Appreciate the time. Thanks, you.");+ });++ await t.test("a model that is sent no prompt keeps its whole transcript", async () => {+ const prompts = respondWith(t, "Thanks, you. OpenWhispr, n8n.");++ const result = await makeManager(["OpenWhispr", "n8n"], "orukeet-v0.1.0").processWithOpenAIAPI(+ audioBlob,+ {}+ );++ assert.equal(prompts[0], null);+ assert.equal(result.rawText, "Thanks, you. OpenWhispr, n8n.");+ });+});++test("OpenWhispr Cloud transcription drops a dictionary list recited after the speech", async (t) => {+ const { window, setSettings, createManager } = await loadAudioManager(t, {+ cachePrefix: "openwhispr-trailing-dictionary-echo-cloud-test-",+ settingsKey: "__trailingDictionaryEchoCloudSettings",+ });+ const originalNavigator = globalThis.navigator;+ Object.defineProperty(globalThis, "navigator", {+ value: { ...originalNavigator, onLine: true },+ configurable: true,+ });+ t.after(() => {+ Object.defineProperty(globalThis, "navigator", {+ value: originalNavigator,+ configurable: true,+ });+ });+ setSettings({+ preferredLanguage: "auto",+ useCleanupModel: false,+ customDictionary: ["OpenWhispr", "n8n", "Traefik", "Tailscale", "Claude Code", "kubectl"],+ snippets: [],+ });+ const audioBlob = { type: "audio/webm", size: 8, arrayBuffer: async () => new ArrayBuffer(8) };+ const respondWith = (text) => {+ const prompts = [];+ window.electronAPI.cloudTranscribe = async (_audio, options) => {+ prompts.push(options.prompt);+ return { success: true, text };+ };+ return prompts;+ };+ const manager = createManager({+ getEffectiveSttLanguage: () => "auto",+ finalizeChineseScript: async (text) => text,+ });++ await t.test("the recited tail is removed from text and rawText", async () => {+ const prompts = respondWith("Appreciate the time. Thanks, you. OpenWhispr, n8n, Traefik.");++ const result = await manager.processWithOpenWhisprCloud(audioBlob);++ assert.equal(prompts[0], "OpenWhispr, n8n, Traefik, Tailscale, Claude Code, kubectl");+ assert.equal(result.rawText, "Appreciate the time. Thanks, you.");+ assert.equal(result.text, "Appreciate the time. Thanks, you.");+ });++ await t.test("a whole-response echo is judged before its tail is stripped", async () => {+ // Stripped first, this would leave "OpenWhispr." and pass as speech.+ respondWith("OpenWhispr. n8n, Traefik, Tailscale, Claude Code, kubectl.");++ await assert.rejects(manager.processWithOpenWhisprCloud(audioBlob), {+ code: "DICTIONARY_ECHO",+ });+ });+});++test("proxied transcription strips a recital only when the payload carried the dictionary", async (t) => {+ const { window, setSettings, createManager } = await loadAudioManager(t, {+ cachePrefix: "openwhispr-trailing-dictionary-echo-proxy-test-",+ settingsKey: "__trailingDictionaryEchoProxySettings",+ });+ const recital = "Appreciate the time. Thanks, you. OpenWhispr, n8n.";+ window.electronAPI.proxyTinfoilTranscription = async () => ({ text: recital });+ window.electronAPI.proxyCortiTranscription = async () => ({ text: recital });+ const audioBlob = new Blob([new Uint8Array([1, 2, 3])], { type: "audio/webm" });+ const transcribeWith = (provider) => {+ setSettings({+ allowLocalFallback: false,+ cloudTranscriptionProvider: provider,+ transcriptionMode: "providers",+ useLocalWhisper: false,+ customDictionary: ["OpenWhispr", "n8n"],+ snippets: [],+ });+ return createManager({+ getEffectiveSttLanguage: () => "auto",+ getTranscriptionModel: () => "whisper-large-v3-turbo",+ getAPIKey: async () => "key",+ processTranscription: async (text) => text,+ isReasoningAvailable: async () => false,+ }).processWithOpenAIAPI(audioBlob);+ };++ await t.test("Tinfoil, sent the dictionary as its prompt, loses the recited tail", async () => {+ const result = await transcribeWith("tinfoil");++ assert.equal(result.rawText, "Appreciate the time. Thanks, you.");+ assert.equal(result.text, "Appreciate the time. Thanks, you.");+ });++ await t.test("Corti, sent no dictionary, keeps its whole transcript", async () => {+ const result = await transcribeWith("corti");++ assert.equal(result.rawText, recital);+ });+});diff --git a/test/helpers/dictionaryEchoFilter.test.js b/test/helpers/dictionaryEchoFilter.test.jsindex 45f81effb..32361684a 100644--- a/test/helpers/dictionaryEchoFilter.test.js+++ b/test/helpers/dictionaryEchoFilter.test.js@@ -352,3 +352,225 @@ test("payloadSendsDictionaryBias: nullish or malformed payloads never enable theassert.equal(payloadSendsDictionaryBias(undefined), false);assert.equal(payloadSendsDictionaryBias("prompt"), false);});++// #2581: a prompt-conditioned model recited the hint list after the speech.+// The real speech dilutes matchesDictionaryPrompt's ratios, so only the+// trailing list is removed and everything spoken is kept. The prompt is the+// hint list as sent; the third argument is the user's own dictionary.+const entries = (prompt) => prompt.split(", ");++test("stripTrailingDictionaryEcho removes a two-term echo appended to speech", async () => {+ const { matchesDictionaryPrompt, stripTrailingDictionaryEcho } =+ await import("../../src/utils/dictionaryEchoFilter.js");+ const text = "Appreciate the time. Thanks, all. Thanks, you. OpenWhispr, n8n.";+ const prompt = "OpenWhispr, n8n";++ assert.equal(matchesDictionaryPrompt(text, prompt), false);+ assert.equal(+ stripTrailingDictionaryEcho(text, prompt, entries(prompt)),+ "Appreciate the time. Thanks, all. Thanks, you."+ );+});++test("stripTrailingDictionaryEcho removes a whole recited list, case and punctuation aside", async () => {+ const { stripTrailingDictionaryEcho } = await import("../../src/utils/dictionaryEchoFilter.js");+ const prompt = "OpenWhispr, n8n, Traefik, Tailscale, Claude Code, kubectl";+ const dictionary = entries(prompt);++ assert.equal(+ stripTrailingDictionaryEcho(+ "All right. Appreciate the time. Thanks, you. OpenWhispr, n8n, Traefik, Tailscale, Claude Code, kubectl",+ prompt,+ dictionary+ ),+ "All right. Appreciate the time. Thanks, you."+ );+ assert.equal(+ stripTrailingDictionaryEcho("Ship it today! openwhispr, n8n, traefik,", prompt, dictionary),+ "Ship it today!"+ );+ assert.equal(+ stripTrailingDictionaryEcho(+ "下周发布。OpenWhispr、n8n。",+ `以下是简体中文。语言、学习、软件、网络。 ${prompt}`,+ dictionary+ ),+ "下周发布。"+ );+});++test("stripTrailingDictionaryEcho strips a mid-list run only when it closes a long prompt", async () => {+ const { stripTrailingDictionaryEcho } = await import("../../src/utils/dictionaryEchoFilter.js");+ const prompt = "OpenWhispr, n8n, Traefik, Tailscale, Claude Code, kubectl, Qwen, Ollama";+ const dictionary = entries(prompt);++ // A Whisper decoder keeps only the tail of a long prompt and recites it to the end.+ assert.equal(+ stripTrailingDictionaryEcho(+ "We shipped it. Tailscale, Claude Code, kubectl, Qwen, Ollama.",+ prompt,+ dictionary+ ),+ "We shipped it."+ );+ for (const said of [+ // A short list the user said, even when it reaches the end of the prompt.+ "Here are the tools I use. Traefik, Tailscale.",+ "We shipped it. kubectl, Qwen, Ollama.",+ // A long run that stops before the end is not a continuation of the prompt.+ "We shipped it. n8n, Traefik, Tailscale, Claude Code, kubectl.",+ // Out of the prompt's order, or with an entry skipped.+ "Which ones did you pick? Tailscale, Traefik, n8n.",+ "Tools. OpenWhispr, Traefik, Tailscale.",+ ]) {+ assert.equal(stripTrailingDictionaryEcho(said, prompt, dictionary), said);+ }++ // The dictionary stores entries oldest first, and auto-learn appends names in+ // the order they were corrected, so saying them back is dictation.+ const names = "OpenWhispr, Kubernetes, Jonathan, Sarah, Michael";+ const reviewers = "Who reviewed it? Jonathan, Sarah, Michael.";+ assert.equal(stripTrailingDictionaryEcho(reviewers, names, entries(names)), reviewers);+});++test("stripTrailingDictionaryEcho keeps a sentence that ends on one dictionary word", async () => {+ const { stripTrailingDictionaryEcho } = await import("../../src/utils/dictionaryEchoFilter.js");+ const prompt = "OpenWhispr, n8n, Traefik, Tailscale";++ for (const text of [+ "The VPN was flaky, so we moved it to Tailscale.",+ "That was flaky. Tailscale.",+ "That was flaky. Tailscale, Tailscale.",+ ]) {+ assert.equal(stripTrailingDictionaryEcho(text, prompt, entries(prompt)), text);+ }+});++test("stripTrailingDictionaryEcho keeps dictionary terms used inside speech", async () => {+ const { stripTrailingDictionaryEcho } = await import("../../src/utils/dictionaryEchoFilter.js");+ const prompt = "OpenWhispr, n8n, Traefik, Tailscale, Claude Code";++ for (const text of [+ // Leading list, then real speech: only a tail can be an appended echo.+ "OpenWhispr, n8n. Those are the two tools I set up today.",+ // Terms mid-sentence, even consecutive ones in prompt order.+ "Today I wired OpenWhispr, n8n and Traefik together.",+ "We route it through Traefik, Tailscale.",+ // Its own sentence, but out of the prompt's order: dictation, not recital.+ "Which ones did you pick? Tailscale, Traefik.",+ // One term is a prefix of a spoken word, not the term itself.+ "It is ready. Open, n8n.",+ // A spoken word between the terms.+ "It is ready. OpenWhispr, then n8n.",+ ]) {+ assert.equal(stripTrailingDictionaryEcho(text, prompt, entries(prompt)), text);+ }+});++test("stripTrailingDictionaryEcho does not treat an abbreviation as a sentence break", async () => {+ const { stripTrailingDictionaryEcho } = await import("../../src/utils/dictionaryEchoFilter.js");+ const prompt = "OpenWhispr, n8n, Traefik";++ for (const text of [+ "Use the tools e.g. OpenWhispr, n8n",+ "Use the tools, i.e. OpenWhispr, n8n.",+ "Ask Mr. OpenWhispr, n8n",+ "Step 1. OpenWhispr, n8n",+ "So... OpenWhispr, n8n",+ "Signed, A. OpenWhispr, n8n.",+ "Wir nutzen Tools, z.B. OpenWhispr, n8n.",+ "Das heißt, d.h. OpenWhispr, n8n.",+ "Our stack, a.k.a. OpenWhispr, n8n.",+ "We use tools incl. OpenWhispr, n8n.",+ "Ask Prof. OpenWhispr, n8n.",+ "Des outils, p. ex. OpenWhispr, n8n.",+ ]) {+ assert.equal(stripTrailingDictionaryEcho(text, prompt, entries(prompt)), text);+ }+ // A year is not a list marker: the sentence before it still ends there.+ assert.equal(+ stripTrailingDictionaryEcho("We shipped in 2026. OpenWhispr, n8n.", prompt, entries(prompt)),+ "We shipped in 2026."+ );+});++test("stripTrailingDictionaryEcho keeps an entry containing a period whole", async () => {+ const { stripTrailingDictionaryEcho } = await import("../../src/utils/dictionaryEchoFilter.js");+ const prompt = "St. Louis, Chicago, J. R. R. Tolkien";++ assert.equal(+ stripTrailingDictionaryEcho("Done. St. Louis, Chicago", prompt, entries(prompt)),+ "Done."+ );+ assert.equal(+ stripTrailingDictionaryEcho("I read it. Louis, Chicago", prompt, entries(prompt)),+ "I read it. Louis, Chicago"+ );+});++test("stripTrailingDictionaryEcho never strips a snippet trigger or the script bias", async () => {+ const { stripTrailingDictionaryEcho } = await import("../../src/utils/dictionaryEchoFilter.js");+ const dictionary = ["OpenWhispr", "n8n"];++ // getDictionaryHintWords appends snippet triggers after the dictionary.+ const withTrigger = "OpenWhispr, n8n, on my way, see you";+ for (const text of ["Text him. On my way, see you.", "Text him. n8n, on my way, see you."]) {+ assert.equal(stripTrailingDictionaryEcho(text, withTrigger, dictionary), text);+ }+ // A recital of the whole prompt runs on into the triggers after the dictionary.+ assert.equal(+ stripTrailingDictionaryEcho(+ "Thanks, you. OpenWhispr, n8n, on my way, see you.",+ withTrigger,+ dictionary+ ),+ "Thanks, you."+ );++ // The Chinese script bias's generic words are not dictionary entries.+ const biased = "以下是简体中文。语言、学习、软件、网络。 OpenWhispr, n8n";+ assert.equal(+ stripTrailingDictionaryEcho("我们看看。语言、学习。", biased, dictionary),+ "我们看看。语言、学习。"+ );+ assert.equal(+ stripTrailingDictionaryEcho("我们看看。软件、网络、OpenWhispr。", biased, dictionary),+ "我们看看。软件、网络、OpenWhispr。"+ );+});++test("stripTrailingDictionaryEcho leaves a whole-response echo to matchesDictionaryPrompt", async () => {+ const { matchesDictionaryPrompt, stripTrailingDictionaryEcho } =+ await import("../../src/utils/dictionaryEchoFilter.js");++ assert.equal(+ stripTrailingDictionaryEcho("OpenWhispr, n8n.", "OpenWhispr, n8n", ["OpenWhispr", "n8n"]),+ "OpenWhispr, n8n."+ );+ assert.equal(matchesDictionaryPrompt("OpenWhispr, n8n.", "OpenWhispr, n8n"), true);+});++test("stripTrailingDictionaryEcho only compares against the prompt it was given", async () => {+ const { stripTrailingDictionaryEcho } = await import("../../src/utils/dictionaryEchoFilter.js");+ const text = "Thanks, you. OpenWhispr, n8n.";+ const dictionary = ["OpenWhispr", "n8n", "Traefik", "Tailscale"];++ assert.equal(stripTrailingDictionaryEcho(text, null, dictionary), text);+ assert.equal(stripTrailingDictionaryEcho(text, "", dictionary), text);+ // Terms the capped prompt never carried cannot have been recited from it.+ assert.equal(stripTrailingDictionaryEcho(text, "Traefik, Tailscale", dictionary), text);+});++test("stripTrailingDictionaryEcho returns non-string input untouched", async () => {+ const { stripTrailingDictionaryEcho } = await import("../../src/utils/dictionaryEchoFilter.js");+ const prompt = "OpenWhispr, n8n";++ assert.equal(stripTrailingDictionaryEcho(null, prompt, entries(prompt)), null);+ assert.equal(stripTrailingDictionaryEcho(undefined, prompt, entries(prompt)), undefined);+ assert.deepEqual(stripTrailingDictionaryEcho({ text: "x" }, prompt, entries(prompt)), {+ text: "x",+ });+ const text = "Thanks, you. OpenWhispr, n8n.";+ assert.equal(stripTrailingDictionaryEcho(text, ["OpenWhispr", "n8n"], entries(prompt)), text);+ assert.equal(stripTrailingDictionaryEcho(text, prompt, undefined), text);+});