UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

164 lines (163 loc) • 5.88 kB
import { t as commonPrefixLength } from "./string-normalization-DLmfYZXI.mjs"; import { t as levenshteinDistance } from "./levenshtein-distance-DsLDlE7K.mjs"; //#region src/talk/activation-name.ts /** * The matcher accepts short names at the leading or trailing edge of a * transcript, strips the name before agent routing, and keeps fuzzy matching * conservative so ordinary dictation does not trigger Talk turns. */ const REALTIME_VOICE_ACTIVATION_NAME_MAX_WORDS = 2; /** Extract the supported leading activation-name prefix from a longer phrase. */ function normalizeRealtimeVoiceActivationNamePrefix(e, t = REALTIME_VOICE_ACTIVATION_NAME_MAX_WORDS) { const n = Array.from(e.matchAll(/[a-z0-9]+/gi), (e) => e[0]); if (n.length === 0) return; return n.slice(0, t).join(" "); } function isSupportedRealtimeVoiceActivationName(e, t = REALTIME_VOICE_ACTIVATION_NAME_MAX_WORDS) { const n = Array.from(e.matchAll(/[a-z0-9]+/gi)).length; return n >= 1 && n <= t; } function normalizeSupportedRealtimeVoiceActivationName(e, t = REALTIME_VOICE_ACTIVATION_NAME_MAX_WORDS) { if (typeof e !== "string") return; const n = e.toLowerCase().replace(/\s+/g, " ").trim() || void 0; return n && isSupportedRealtimeVoiceActivationName(n, t) ? n : void 0; } /** Prefer longer names first so nested names match the most specific option. */ function sortRealtimeVoiceActivationNames(e) { return e.toSorted((e, t) => t.length - e.length || e.localeCompare(t)); } /** Match and strip a configured activation name from either transcript edge. */ function matchRealtimeVoiceActivationName(e, t, n = REALTIME_VOICE_ACTIVATION_NAME_MAX_WORDS) { const r = []; for (const e of t) { const t = compactActivationName(e.toLowerCase()); if (t) r.push({ activationName: e, compact: t }); } if (r.length === 0) return; const i = [...leadingActivationNameCandidates(e, n), ...trailingActivationNameCandidates(e, n)].map((e) => ({ candidate: e, compact: compactActivationName(e.heardName) })).toSorted((e, t) => t.compact.length - e.compact.length); for (const { candidate: t, compact: n } of i) for (const { activationName: i, compact: a } of r) { const r = n === a; const o = isFuzzyActivationNameMatch(t, n, a); if (r || o) return { allowed: true, text: stripEdgeActivationNameCandidate(e, t), activationName: i, heardName: t.heardName, match: r ? "exact" : "fuzzy", edge: t.edge }; } } function normalizeActivationNameCandidate(e) { return e.toLowerCase().replace(/[^a-z0-9]+/g, " ").trim() || void 0; } function compactActivationName(e) { return e.replace(/[^a-z0-9]+/g, ""); } function leadingActivationNameCandidates(e, t) { const n = /^\s*(?:(?:hey|ok|okay)(?:\s*[-,:;]+\s*|\s+))?/i.exec(e)?.[0].length ?? 0; const r = []; const i = n > 0 ? [0, n] : [0]; for (const n of i) { const i = /[a-z0-9]+/gi; i.lastIndex = n; const a = []; for (let r = 0; r < t; r += 1) { const t = i.exec(e); if (!t) break; const o = r === 0 ? n : a[r - 1]?.endIndex; const s = e.slice(o, t.index); if (r > 0 && !/^[\s'-]+$/.test(s)) break; const c = t.index + t[0].length; const l = normalizeActivationNameCandidate(e.slice(n, c)); if (!l) break; const u = e.slice(c).match(/^\s*([,.:;!?-]|$)/); a.push({ edge: "leading", heardName: l, startIndex: n, endIndex: c, strongBoundary: Boolean(u) }); } r.push(...a); } return r; } function trailingActivationNameCandidates(e, t) { const n = Array.from(e.matchAll(/[a-z0-9]+/gi)); const r = []; const i = Math.min(n.length, t); for (let t = 1; t <= i; t += 1) { const i = n[n.length - t]; const a = n[n.length - 1]; if (!i || !a?.[0]) break; const o = i.index ?? 0; const s = (a.index ?? 0) + a[0].length; if (!/^\s*(?:[,.:;!?-]+\s*)?$/.test(e.slice(s))) break; if (!/(^|[\s,.:;!?-])$/.test(e.slice(0, o))) break; const c = /(^|[,.:;!?-]\s*)$/.test(e.slice(0, o)); const l = /\?\s*$/.test(e); if (t > 1) { const r = n[n.length - t + 1]; const a = r ? e.slice(o + i[0].length, r.index) : ""; if (!/^[\s'-]+$/.test(a)) break; } const u = normalizeActivationNameCandidate(e.slice(o, s)); if (!u) break; r.push({ edge: "trailing", heardName: u, startIndex: o, endIndex: s, strongBoundary: c && l }); } return r; } function hasOnlyPhoneticSubstitutions(e, t) { if (e.length !== t.length) return false; const n = /* @__PURE__ */ new Set([ "a", "e", "i", "o", "u", "y" ]); const r = /* @__PURE__ */ new Set(["l", "r"]); let i = 0; for (let a = 0; a < e.length; a += 1) { const o = e[a]; const s = t[a]; if (o === s) continue; const c = n.has(o ?? "") && n.has(s ?? ""); const l = r.has(o ?? "") && r.has(s ?? ""); if (!c && !l) return false; i += 1; } return i > 0; } function isFuzzyActivationNameMatch(e, t, n) { if (!t || !n || n.length < 5) return false; if (!e.strongBoundary) return false; if (t[0] !== n[0]) return false; if (e.edge === "trailing") return t.length === n.length && hasOnlyPhoneticSubstitutions(t, n); const r = levenshteinDistance(t, n); if (r <= 1) return true; if (r === 2 && t.length >= 4 && n.length >= 5 && (t.length !== n.length || hasOnlyPhoneticSubstitutions(t, n) || commonPrefixLength(t, n) >= 6)) return true; if (r === 3 && t.length >= 7 && n.length >= 7 && t.length !== n.length && commonPrefixLength(t, n) >= 5) return true; return false; } function stripEdgeActivationNameCandidate(e, t) { if (t.edge === "leading") return e.slice(t.endIndex).replace(/^\s*(?:[-,:;.!?]+\s*)?/, "").trim(); return e.slice(0, t.startIndex).replace(/\s*(?:[-,:;.!?]+\s*)?$/, "").trim(); } //#endregion export { sortRealtimeVoiceActivationNames as a, normalizeSupportedRealtimeVoiceActivationName as i, matchRealtimeVoiceActivationName as n, normalizeRealtimeVoiceActivationNamePrefix as r, isSupportedRealtimeVoiceActivationName as t };