TypeScript
fix(voice): insert Unicode word boundaries for dictation (#31799)
Julius Marmingebc1eb5d32afb
diff --git a/packages/client-runtime/src/voice-input/controller.test.ts b/packages/client-runtime/src/voice-input/controller.test.tsindex b28c885ce0d..265a284e20f 100644--- a/packages/client-runtime/src/voice-input/controller.test.ts+++ b/packages/client-runtime/src/voice-input/controller.test.ts@@ -234,6 +234,7 @@ describe("resolveTranscriptCommit", () => {it.each([["Hello", "world", "Hello world"],["Hello", "世界", "Hello世界"],+ ["Hello", "𝟚", "Hello 𝟚"],["你好", "世界", "你好世界"],])("preserves script boundaries with an autodetected language: %s + %s",@@ -246,6 +247,24 @@ describe("resolveTranscriptCommit", () => {},);+ it.each([+ ["Korean", "코드를", 3, "검토해 주세요", "ko_KR", "코드를 검토해 주세요"],+ ["a supplementary letter", "𞤀", 2, "𞤦", "ff-Adlm", "𞤀 𞤦"],+ ["a trailing combining mark", "नमस्ते", 6, "दुनिया", "hi-IN", "नमस्ते दुनिया"],+ ["a non-ASCII word after the caret", "Änderungen", 0, "Weitere", "de_DE", "Weitere Änderungen"],+ ["spaceless Japanese", "修正キャッシュ", 7, "テストも", "ja-JP", "修正キャッシュテストも"],+ ["spaceless Thai", "โลก", 0, "สวัสดี", "th-TH", "สวัสดีโลก"],+ ])(+ "inserts at a caret with the recorded language's word spacing: %s",+ (_case, text, caret, spoken, locale, expected) => {+ const captured = draft({ text, selection: { start: caret, end: caret } });+ expect(resolveTranscriptCommit(captured, captured, spoken, locale)).toMatchObject({+ kind: "commit",+ text: expected,+ });+ },+ );+it("replaces the recorded UTF-16 selection around emoji and composer tokens", () => {const text = "Fix 🧪 then $review please";const tokenStart = text.indexOf("$review");diff --git a/packages/client-runtime/src/voice-input/controller.ts b/packages/client-runtime/src/voice-input/controller.tsindex 92d743b5e88..c67287dd74d 100644--- a/packages/client-runtime/src/voice-input/controller.ts+++ b/packages/client-runtime/src/voice-input/controller.ts@@ -107,6 +107,8 @@ type TranscriptCommitResult =| { readonly kind: "stale" }| { readonly kind: "empty" };+const WORD_SPACELESS_LANGUAGES = new Set(["ja", "zh", "yue", "th", "lo", "km", "my"]);+export function resolveTranscriptCommit(captured: VoiceDraftSnapshot,current: VoiceDraftSnapshot | null,@@ -128,23 +130,23 @@ export function resolveTranscriptCommit(}const isEmptySelection = captured.selection.start === captured.selection.end;- const normalizedLocale = locale.replaceAll("_", "-").toLowerCase();- const usesEnglishSpacing = normalizedLocale === "en" || normalizedLocale.startsWith("en-");+ const language = locale.split(/[-_]/)[0]!.toLowerCase();let insertion = replacement;- const autoSpacing = normalizedLocale === "auto";- if (isEmptySelection && (usesEnglishSpacing || autoSpacing)) {- const left = captured.text[captured.selection.start - 1];- const right = captured.text[captured.selection.start];+ if (isEmptySelection && !WORD_SPACELESS_LANGUAGES.has(language)) {+ const auto = language === "auto";+ const caret = captured.selection.start;+ const left = captured.text.slice(Math.max(0, caret - 2), caret);+ const right = captured.text.slice(caret, caret + 2);+ const leftEdge = auto ? /[A-Za-z0-9.!?,:;)\]}'"]$/ : /(?:\p{L}|\p{M}|\p{N}|[.!?,:;)\]}'"])$/u;+ const rightEdge = auto ? /^[A-Za-z0-9([{'"]/ : /^[\p{L}\p{M}\p{N}([{'"]/u;const leftNeedsBoundary =- (!autoSpacing || /^[\p{Script=Latin}\p{N}]/u.test(replacement)) &&- left !== undefined &&- /[A-Za-z0-9.!?,:;)\]}'"]/.test(left) &&- (right === undefined || /\s/.test(right));+ (!auto || /^[\p{Script=Latin}\p{N}]/u.test(replacement)) &&+ leftEdge.test(left) &&+ (!right || /^\s/.test(right));const rightNeedsBoundary =- (!autoSpacing || /[\p{Script=Latin}\p{N}]$/u.test(replacement)) &&- right !== undefined &&- /[A-Za-z0-9([{'"]/.test(right) &&- (left === undefined || /\s/.test(left));+ (!auto || /(?:\p{Script=Latin}|\p{N})$/u.test(replacement)) &&+ rightEdge.test(right) &&+ (!left || /\s$/.test(left));insertion = `${leftNeedsBoundary ? " " : ""}${replacement}${rightNeedsBoundary ? " " : ""}`;}