Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
refactor: hoist voice-echo pure helpers to server/lib/voiceEcho.js (#6818)
[._.]/ Adam Eivye932f90c4aed
diff --git a/client/src/services/voiceClient.js b/client/src/services/voiceClient.jsindex 22044cebc3..5a9d11920e 100644--- a/client/src/services/voiceClient.js+++ b/client/src/services/voiceClient.js@@ -9,6 +9,7 @@ import socket from './socket';import { subscribeVisibility } from '../hooks/useVisibilityEvent.js';import { sleep } from '../utils/sleep.js';import { resumeAudioContext, acquireAudioSession } from '../lib/audioContext.js';+import { ECHO_WINDOW_MS, MIN_TOKENS_FOR_ECHO_CHECK, MIN_SHARED_TRIGRAMS, tokenize, trigramsOf } from '../../../server/lib/voiceEcho.js';// iOS audio-session claims held while a mic stream is open — one per capture// mode, because push-to-talk and hands-free listening can overlap. `getUserMedia`@@ -190,39 +191,20 @@ const isInTtsEchoWindow = () => isTtsActive() || performance.now() < ttsCooldown// This client gate runs only on the Web Speech path where transcripts are// produced in-browser and sent as voice:text. Whisper-based continuous mode// sends audio to the server, so its echo gate lives in-// server/services/voice/echo.js — KEEP THE TWO IN SYNC. Both implement the-// same algorithm so tuning one (threshold, window, tokenizer) requires the-// other to match.-const TTS_ECHO_MEMORY_MS = 8000;-const MIN_TOKENS_FOR_ECHO_CHECK = 4;-const MIN_SHARED_TRIGRAMS = 2;+// server/services/voice/echo.js. Pure helpers are shared via+// server/lib/voiceEcho.js.const recentTtsSentences = [];-const tokenizeForEcho = (s) => (s || '')- .toLowerCase()- .replace(/[^\p{L}\p{N}\s]/gu, ' ')- .split(/\s+/)- .filter(Boolean);--const buildTrigrams = (tokens) => {- if (tokens.length < 3) return [];- const out = [];- for (let i = 0; i + 3 <= tokens.length; i++) {- out.push(`${tokens[i]} ${tokens[i + 1]} ${tokens[i + 2]}`);- }- return out;-};-const rememberTtsSentence = (sentence) => {- const tokens = tokenizeForEcho(sentence);+ const tokens = tokenize(sentence);if (!tokens.length) return;const now = performance.now();- while (recentTtsSentences.length && now - recentTtsSentences[0].t > TTS_ECHO_MEMORY_MS) {+ while (recentTtsSentences.length && now - recentTtsSentences[0].t > ECHO_WINDOW_MS) {recentTtsSentences.shift();}recentTtsSentences.push({text: tokens.join(' '),- trigrams: new Set(buildTrigrams(tokens)),+ trigrams: new Set(trigramsOf(tokens)),t: now,});};@@ -233,14 +215,14 @@ const looksLikeTtsEcho = (text) => {// content gate entirely so we never misclassify the user's actual speech.if (audioRoute.likelyHeadset) return false;- const tokens = tokenizeForEcho(text);+ const tokens = tokenize(text);if (tokens.length < MIN_TOKENS_FOR_ECHO_CHECK) return false;const heardText = tokens.join(' ');- const heardTrigrams = buildTrigrams(tokens);+ const heardTrigrams = trigramsOf(tokens);if (!heardTrigrams.length) return false;const now = performance.now();for (const entry of recentTtsSentences) {- if (now - entry.t > TTS_ECHO_MEMORY_MS) continue;+ if (now - entry.t > ECHO_WINDOW_MS) continue;if (entry.text.includes(heardText)) return true;let shared = 0;for (const tg of heardTrigrams) {diff --git a/server/lib/voiceEcho.js b/server/lib/voiceEcho.jsnew file mode 100644index 0000000000..fe93a3cbac--- /dev/null+++ b/server/lib/voiceEcho.js@@ -0,0 +1,33 @@+/**+ * Pure voice-echo detection helpers — tokenization, trigram building, and+ * thresholds shared between server echo filtering (`server/services/voice/echo.js`)+ * and client STT echo cancellation (`client/src/services/voiceClient.js`).+ *+ * Both implementations apply the same algorithm:+ * 1. Length gate: utterances shorter than MIN_TOKENS_FOR_ECHO_CHECK words+ * are NEVER classified as echo (preserves user interrupts).+ * 2. Trigram match: for longer utterances, count shared 3-word windows+ * against recently-spoken TTS sentences. Two+ shared trigrams is strong+ * evidence of echo.+ *+ * Time-windowed: TTS sentences older than ECHO_WINDOW_MS are ignored.+ */++export const ECHO_WINDOW_MS = 8000;+export const MIN_TOKENS_FOR_ECHO_CHECK = 4;+export const MIN_SHARED_TRIGRAMS = 2;++export const tokenize = (s) => (s || '')+ .toLowerCase()+ .replace(/[^\p{L}\p{N}\s]/gu, ' ')+ .split(/\s+/)+ .filter(Boolean);++export const trigramsOf = (tokens) => {+ if (!Array.isArray(tokens) || tokens.length < 3) return [];+ const out = [];+ for (let i = 0; i + 3 <= tokens.length; i++) {+ out.push(`${tokens[i]} ${tokens[i + 1]} ${tokens[i + 2]}`);+ }+ return out;+};diff --git a/server/services/voice/echo.js b/server/services/voice/echo.jsindex 9c0a663c3d..e88d8bc5ba 100644--- a/server/services/voice/echo.js+++ b/server/services/voice/echo.js@@ -6,7 +6,7 @@// A mirror of this algorithm runs client-side in// client/src/services/voiceClient.js for the Web Speech STT path (which// produces transcripts in-browser and never sends audio to the server).-// KEEP THE TWO IN SYNC — same tokenizer, same thresholds, same window.+// Pure helpers are shared via server/lib/voiceEcho.js.//// Strategy — two stacked filters:// 1. Length gate: utterances shorter than MIN_TOKENS_FOR_ECHO_CHECK words@@ -22,23 +22,20 @@// Time-windowed: TTS sentences older than `windowMs` are ignored so an echo// from 30 seconds ago can't suppress a legitimate later utterance.-export const ECHO_WINDOW_MS = 8000;-export const MIN_TOKENS_FOR_ECHO_CHECK = 4;-export const MIN_SHARED_TRIGRAMS = 2;+import {+ ECHO_WINDOW_MS,+ MIN_TOKENS_FOR_ECHO_CHECK,+ MIN_SHARED_TRIGRAMS,+ tokenize,+ trigramsOf,+} from '../../lib/voiceEcho.js';-export const tokenize = (s) => (s || '')- .toLowerCase()- .replace(/[^\p{L}\p{N}\s]/gu, ' ')- .split(/\s+/)- .filter(Boolean);--export const trigramsOf = (tokens) => {- if (!Array.isArray(tokens) || tokens.length < 3) return [];- const out = [];- for (let i = 0; i + 3 <= tokens.length; i++) {- out.push(`${tokens[i]} ${tokens[i + 1]} ${tokens[i + 2]}`);- }- return out;+export {+ ECHO_WINDOW_MS,+ MIN_TOKENS_FOR_ECHO_CHECK,+ MIN_SHARED_TRIGRAMS,+ tokenize,+ trigramsOf,};// Push a freshly-spoken TTS sentence into the per-socket echo memory and