Skip to content

Commit 93d3340

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-31831
2 parents 0aff918 + 5c004fd commit 93d3340

29 files changed

Lines changed: 706 additions & 72 deletions

‎apps/mobile/src/features/settings/ThreadSettingsSections.tsx‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ function BetaSettingsSection() {
268268
</SettingsSection>
269269
<Text className="px-2 text-sm text-foreground-muted">
270270
Let agents on a machine work on your other machines, and theirs on it. Turn it on for each
271-
machine. While it is off, nothing new starts over a link; tasks already running finish.
271+
machine. While off, nothing new starts over a link; running tasks finish.
272272
</Text>
273273
</View>
274274
);
@@ -306,9 +306,8 @@ function ToolCallVisibilitySection() {
306306
))}
307307
</SettingsSection>
308308
<Text className="px-2 text-sm text-foreground-muted">
309-
Hide the commands, edits, and searches agents run, so a thread reads like a conversation.
310-
Hide successful keeps calls that fail. Zen also hides the agent&apos;s in-between messages
311-
and shows only final answers. Approvals, questions, and errors always show.
309+
Hide the commands, edits, and searches agents run. Hide successful keeps failures. Zen shows
310+
only final answers. Approvals, questions, and errors always show.
312311
</Text>
313312
</View>
314313
);

‎apps/mobile/src/features/settings/components/DatabaseStorageNotice.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function DatabaseStorageNotice({
3131
return (
3232
<SettingsSection
3333
title="Reclaim database space"
34-
footer="Old history is cleaned up automatically, but these databases still hold the freed space. Run the command on each machine to return it to disk; it pauses and restarts the background service. Quit the desktop app first if it hosts the environment."
34+
footer="These databases still hold space freed by history cleanup. Run the command on each machine to reclaim it. It restarts the background service. Quit the desktop app first if it hosts the environment."
3535
>
3636
{visible.map(({ environmentId, status }) => (
3737
<SettingsActionRow

‎apps/mobile/src/native/voiceTranscription.android.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ it.each([
7878
selection: { start: 16, end: 16 },
7979
revision: 1,
8080
};
81+
const prepareToRecordAsync = vi.fn(async () => {});
8182
const controller = new VoiceInputController({
8283
recorder: {
8384
uri: "file:///acceptance.m4a",
84-
prepareToRecordAsync: async () => {},
85+
prepareToRecordAsync,
8586
record: () => {},
8687
stop: async () => {},
8788
},
@@ -101,4 +102,5 @@ it.each([
101102
expect(commit).not.toHaveBeenCalled();
102103
expect(draft.text).toBe("Preserved draft.");
103104
expect(controller.currentState.error).toMatch(expected);
105+
expect(prepareToRecordAsync).toHaveBeenCalledTimes(phase === "preparation" ? 0 : 1);
104106
});

‎apps/mobile/src/native/voiceTranscription.ios.test.ts‎

Lines changed: 192 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import type { TranscriptionResult } from "@react-native-ai/apple/src/NativeAppleTranscription";
22
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
33

4-
import { VoiceTranscriptionError } from "@t3tools/client-runtime/voice-input";
4+
import { VoiceInputController, VoiceTranscriptionError } from "@t3tools/client-runtime/voice-input";
5+
import { resetVoiceInputGlobalsForTests } from "../../../../packages/client-runtime/src/voice-input/controller";
56

67
const mocks = vi.hoisted(() => ({
78
isAvailable: vi.fn<(locale: string) => boolean>(),
89
prepare: vi.fn<(locale: string) => Promise<string>>(),
910
transcribe: vi.fn<(audio: ArrayBufferLike, locale: string) => Promise<TranscriptionResult>>(),
1011
readAudio: vi.fn<() => Promise<ArrayBuffer>>(),
12+
preferredLanguages: [] as unknown,
13+
}));
14+
15+
// iOS exposes the user's ordered Language & Region list through NSUserDefaults.
16+
vi.mock("react-native", () => ({
17+
Settings: { get: (key: string) => (key === "AppleLanguages" ? mocks.preferredLanguages : null) },
1118
}));
1219

1320
vi.mock("@react-native-ai/apple/src/NativeAppleTranscription", () => ({
@@ -35,16 +42,68 @@ const nativeTranscript: TranscriptionResult = {
3542
],
3643
};
3744

38-
function deferred<T>() {
39-
let resolve!: (value: T) => void;
40-
const promise = new Promise<T>((next) => {
41-
resolve = next;
45+
const unsupportedLocale = () =>
46+
Object.assign(new Error("unsupported"), { code: "AppleTranscriptionUnsupportedLocale" });
47+
48+
function useLanguages(preferred: ReadonlyArray<string>, appLocale: string) {
49+
mocks.preferredLanguages = preferred;
50+
const resolvedOptions = Intl.DateTimeFormat().resolvedOptions();
51+
vi.spyOn(Intl.DateTimeFormat.prototype, "resolvedOptions").mockReturnValue({
52+
...resolvedOptions,
53+
locale: appLocale,
54+
});
55+
}
56+
57+
/** Prepares and transcribes one recording, resolving with the error when either step fails. */
58+
async function transcribeOnce(signal = new AbortController().signal) {
59+
const transcriber = (await getLocalVoiceTranscriber())!;
60+
return transcriber
61+
.prepare({ signal })
62+
.then(async (prepared) => {
63+
const text = await prepared.transcribe("file:///voice.m4a", { signal });
64+
return { text, locale: prepared.locale };
65+
})
66+
.catch((error: unknown) => error);
67+
}
68+
69+
/** Drives one on-device dictation through the controller the composer uses. */
70+
function createDictation() {
71+
const recorder = {
72+
uri: "file:///voice.m4a",
73+
prepareToRecordAsync: async () => {},
74+
record: vi.fn(),
75+
stop: vi.fn(async () => {}),
76+
};
77+
const commits: string[] = [];
78+
const deleted: string[] = [];
79+
const ended = Promise.withResolvers<void>();
80+
const controller = new VoiceInputController({
81+
recorder,
82+
getTranscriber: () => ({
83+
prepare: async (options) => (await getLocalVoiceTranscriber())!.prepare(options),
84+
}),
85+
requestPermission: async () => ({ granted: true, canAskAgain: true }),
86+
configureRecording: async () => {},
87+
releaseRecording: async () => {},
88+
deleteRecording: (uri) => deleted.push(uri),
89+
readDraft: () => ({
90+
ownerKey: "environment:thread",
91+
text: "",
92+
selection: { start: 0, end: 0 },
93+
revision: 1,
94+
}),
95+
commitDraft: (text) => commits.push(text),
96+
onStateChange: (state) => {
97+
if (state.phase === "idle" || state.phase === "error") ended.resolve();
98+
},
4299
});
43-
return { promise, resolve };
100+
return { controller, recorder, commits, deleted, ended: ended.promise };
44101
}
45102

46103
beforeEach(() => {
47104
vi.resetAllMocks();
105+
resetVoiceInputGlobalsForTests();
106+
useLanguages([], "sv-SE");
48107
mocks.isAvailable.mockReturnValue(true);
49108
mocks.prepare.mockResolvedValue("sv-SE");
50109
mocks.readAudio.mockResolvedValue(audio);
@@ -56,9 +115,134 @@ afterEach(() => {
56115
});
57116

58117
describe("getLocalVoiceTranscriber", () => {
118+
it.each([
119+
{
120+
case: "a spoken language ahead of the English app locale",
121+
preferred: ["ko-KR", "en-US"],
122+
supported: ["ko-KR", "en-US"],
123+
tried: ["ko-KR"],
124+
},
125+
{
126+
case: "the next preferred language after an unsupported one",
127+
preferred: ["gsw-CH", "de-CH", "en-US"],
128+
supported: ["de-CH", "en-US"],
129+
tried: ["gsw-CH", "de-CH"],
130+
},
131+
{
132+
case: "the app locale when no preferred language is supported",
133+
preferred: ["gsw-CH", "yi"],
134+
supported: ["en-US"],
135+
tried: ["gsw-CH", "yi", "en-US"],
136+
},
137+
])("prepares $case", async ({ preferred, supported, tried }) => {
138+
useLanguages(preferred, "en-US");
139+
mocks.prepare.mockImplementation(async (candidate) => {
140+
if (!supported.includes(candidate)) throw unsupportedLocale();
141+
return candidate;
142+
});
143+
144+
const locale = tried.at(-1)!;
145+
expect(await transcribeOnce()).toEqual({ text: "Hej världen.", locale });
146+
expect(mocks.prepare.mock.calls.map(([candidate]) => candidate)).toEqual(tried);
147+
expect(mocks.transcribe).toHaveBeenCalledWith(audio, locale);
148+
});
149+
150+
it("reports an unsupported language when neither preferred nor app locales are supported", async () => {
151+
useLanguages(["gsw-CH"], "yi");
152+
mocks.prepare.mockRejectedValue(unsupportedLocale());
153+
154+
expect(await transcribeOnce()).toMatchObject({ code: "unsupported-locale" });
155+
expect(mocks.prepare.mock.calls.map(([candidate]) => candidate)).toEqual(["gsw-CH", "yi"]);
156+
});
157+
158+
it.each(["cancellation", "a model failure"] as const)(
159+
"does not fall back to another language after %s",
160+
async (failure) => {
161+
useLanguages(["ko-KR", "ja-JP"], "en-US");
162+
const entered = Promise.withResolvers<void>();
163+
const nativePrepare = Promise.withResolvers<string>();
164+
mocks.prepare.mockImplementation(() => {
165+
entered.resolve();
166+
return nativePrepare.promise;
167+
});
168+
const controller = new AbortController();
169+
const result = transcribeOnce(controller.signal);
170+
171+
await entered.promise;
172+
if (failure === "cancellation") {
173+
controller.abort();
174+
nativePrepare.reject(unsupportedLocale());
175+
} else {
176+
nativePrepare.reject(Object.assign(new Error("download failed"), { code: "Network" }));
177+
}
178+
179+
expect(await result).toMatchObject({
180+
code: failure === "cancellation" ? "cancelled" : "preparation-failed",
181+
});
182+
expect(mocks.prepare.mock.calls.map(([candidate]) => candidate)).toEqual(["ko-KR"]);
183+
},
184+
);
185+
186+
it("records while Apple prepares its speech model and transcribes once it is ready", async () => {
187+
const nativePrepare = Promise.withResolvers<string>();
188+
mocks.prepare.mockReturnValue(nativePrepare.promise);
189+
const dictation = createDictation();
190+
191+
await dictation.controller.start();
192+
expect(dictation.recorder.record).toHaveBeenCalledOnce();
193+
expect(dictation.controller.currentState.phase).toBe("recording");
194+
195+
const stopping = dictation.controller.stop();
196+
nativePrepare.resolve("sv-SE");
197+
await stopping;
198+
199+
expect(mocks.transcribe).toHaveBeenCalledOnce();
200+
expect(dictation.commits).toEqual(["Hej världen."]);
201+
expect(dictation.deleted).toEqual(["file:///voice.m4a"]);
202+
});
203+
204+
it.each(["cancel", "cancel after stop", "model failure", "model failure after stop"] as const)(
205+
"discards early audio without a late commit after %s",
206+
async (ending) => {
207+
const nativePrepare = Promise.withResolvers<string>();
208+
mocks.prepare.mockReturnValue(nativePrepare.promise);
209+
const dictation = createDictation();
210+
await dictation.controller.start();
211+
212+
let finishing: Promise<void> | undefined;
213+
const cancelled = ending === "cancel" || ending === "cancel after stop";
214+
if (ending.endsWith("after stop")) finishing = dictation.controller.stop();
215+
if (cancelled) {
216+
dictation.controller.cancel();
217+
} else {
218+
nativePrepare.reject(Object.assign(new Error("download failed"), { code: "Network" }));
219+
}
220+
await finishing;
221+
await dictation.ended;
222+
223+
const next = createDictation();
224+
mocks.prepare.mockResolvedValue("sv-SE");
225+
await next.controller.start();
226+
expect(next.controller.currentState.phase).toBe("recording");
227+
expect(dictation.deleted).toEqual(["file:///voice.m4a"]);
228+
expect(mocks.transcribe).not.toHaveBeenCalled();
229+
await next.controller.stop();
230+
expect(next.commits).toEqual(["Hej världen."]);
231+
nativePrepare.resolve("sv-SE");
232+
233+
expect(dictation.commits).toEqual([]);
234+
expect(dictation.recorder.stop).toHaveBeenCalled();
235+
expect(dictation.controller.currentState).toMatchObject(
236+
cancelled
237+
? { phase: "idle" }
238+
: { phase: "error", error: "Voice transcription could not prepare this language." },
239+
);
240+
},
241+
);
242+
59243
it("does not start native transcription after cancellation during a file read", async () => {
60-
const enteredRead = deferred<void>();
61-
const readResult = deferred<ArrayBuffer>();
244+
const enteredRead = Promise.withResolvers<void>();
245+
const readResult = Promise.withResolvers<ArrayBuffer>();
62246
mocks.readAudio.mockImplementation(() => {
63247
enteredRead.resolve();
64248
return readResult.promise;

0 commit comments

Comments
 (0)