@devries-cameron20 / t3code-1 / commits / 2526691ea5
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(mobile): cancelling a model download keeps the current transcription model (#39157)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 19, 2026 at 16:52 UTC 2526691ea515deb19f09efb4cb8e0f1e6a226a5e
12 files changed +322 -94
apps/mobile/src/features/settings/VoiceModelSection.tsx
+2 -6
@@ -4,7 +4,7 @@ import { useAtomRefresh, useAtomValue } from "@effect/atom-react";
4 import { useNavigation } from "@react-navigation/native";
5 import {
6 cancelEnvironmentSpeechModelDownload,
7 - downloadEnvironmentSpeechModel,
7 + installEnvironmentSpeechModel,
8 selectEnvironmentSpeechModel,
9 } from "@t3tools/client-runtime/voice-input";
10 import {
@@ -98,11 +98,7 @@ export function useVoiceModels(environmentId: EnvironmentId, label: string) {
98 onConfirm: () =>
99 void run(
100 gt("Could not download the model"),
101 - async () => {
102 - await runSpeech(downloadEnvironmentSpeechModel(connection, model.id));
103 - if (model.id !== status.modelId)
104 - await runSpeech(selectEnvironmentSpeechModel(connection, model.id));
105 - },
101 + () => runSpeech(installEnvironmentSpeechModel(connection, model.id)),
102 model.id,
103 ),
104 });
apps/server/src/speech/SpeechService.test.ts
+48
@@ -361,6 +361,53 @@ it.effect("keeps a detached download exclusive until the transfer finishes", ()
361 }).pipe(Effect.provide(layer)),
362 );
363
364 +it.effect("reports cancellation even after the speech weights are installed", () =>
365 + Effect.gen(function* () {
366 + const speech = yield* SpeechService.SpeechService;
367 + readyModels.delete("fallback-model");
368 + const weightsInstalled = yield* Deferred.make<void>();
369 + const helperStarted = yield* Deferred.make<void>();
370 + download
371 + .mockImplementationOnce(() =>
372 + Effect.sync(() => {
373 + readyModels.add("fallback-model");
374 + }).pipe(
375 + Effect.andThen(Deferred.succeed(weightsInstalled, undefined)),
376 + Effect.as("test.gguf"),
377 + ),
378 + )
379 + .mockImplementationOnce(() =>
380 + Deferred.succeed(helperStarted, undefined).pipe(Effect.andThen(Effect.never)),
381 + );
382 +
383 + const request = yield* speech.downloadModel("fallback-model").pipe(Effect.forkChild);
384 + yield* Deferred.await(weightsInstalled);
385 + yield* Deferred.await(helperStarted);
386 + yield* speech.cancelDownload("fallback-model");
387 +
388 + expect(yield* Fiber.join(request)).toMatchObject({
389 + cancelled: true,
390 + models: expect.arrayContaining([
391 + expect.objectContaining({ id: "fallback-model", state: "installed", active: false }),
392 + expect.objectContaining({ id: "test-model", active: true }),
393 + ]),
394 + });
395 + }).pipe(Effect.provide(layer)),
396 +);
397 +
398 +it.effect("preserves the current model when another client removes a selection candidate", () =>
399 + Effect.gen(function* () {
400 + const speech = yield* SpeechService.SpeechService;
401 + yield* speech.removeModel("fallback-model");
402 + const selection = yield* Effect.result(speech.selectModel("fallback-model"));
403 + expect(Result.isFailure(selection) && selection.failure).toMatchObject({
404 + _tag: "SpeechModelNotFoundError",
405 + modelId: "fallback-model",
406 + });
407 + expect(yield* speech.status).toMatchObject({ supported: true, modelId: "test-model" });
408 + }).pipe(Effect.provide(layer)),
409 +);
410 +
411 it.effect("bounds missing Silero recovery during recording and continues with raw audio", () =>
412 Effect.gen(function* () {
413 const helperStarted = yield* Deferred.make<void>();
@@ -404,6 +451,7 @@ it.effect("keeps the installed speech model usable when optional Silero acquisit
451 );
452 const speech = yield* SpeechService.SpeechService;
453 const installed = yield* speech.downloadModel("test-model");
454 + expect(installed).toHaveProperty("cancelled", false);
455 expect(installed.models.find((model) => model.id === "test-model")?.state).toBe("installed");
456 expect(yield* speech.transcribe(pcm(), DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS)).toBe("hello");
457 yield* Effect.scoped(
apps/server/src/speech/SpeechService.ts
+43 -49
@@ -1,6 +1,7 @@
1 import {
2 SPEECH_STREAM_MAX_CHUNK_BYTES,
3 type EnvironmentSpeechModel,
4 + type EnvironmentSpeechModelDownload,
5 type EnvironmentSpeechStatus,
6 type SpeechAcceleration,
7 type SpeechModelUnloadTimeout,
@@ -134,7 +135,9 @@ export class SpeechService extends Context.Service<
135 {
136 readonly status: Effect.Effect<EnvironmentSpeechStatus, SpeechError>;
137 readonly models: (includeBenchmarks: boolean) => Effect.Effect<SpeechModels, SpeechError>;
137 - readonly downloadModel: (modelId: string) => Effect.Effect<SpeechModels, SpeechError>;
138 + readonly downloadModel: (
139 + modelId: string,
140 + ) => Effect.Effect<Required<EnvironmentSpeechModelDownload>, SpeechError>;
141 readonly selectModel: (modelId: string) => Effect.Effect<SpeechModels, SpeechError>;
142 readonly cancelDownload: (modelId: string) => Effect.Effect<SpeechModels, SpeechError>;
143 readonly removeModel: (modelId: string) => Effect.Effect<SpeechModels, SpeechError>;
@@ -894,24 +897,25 @@ const make = Effect.gen(function* () {
897 Effect.onError((cause) => Scope.close(session, Exit.failCause(cause))),
898 );
899 }),
897 - downloadModel: Effect.fn("SpeechService.downloadModel")((modelId) =>
898 - requireSupported.pipe(
899 - Effect.andThen(download(modelId)),
900 - Effect.catchTags({ SpeechDownloadCancelledError: () => Effect.void }),
901 - Effect.andThen(modelsEffect),
902 - ),
903 - ),
900 + downloadModel: Effect.fn("SpeechService.downloadModel")(function* (modelId) {
901 + yield* requireSupported;
902 + const cancelled = yield* download(modelId).pipe(
903 + Effect.as(false),
904 + Effect.catchTags({ SpeechDownloadCancelledError: () => Effect.succeed(true) }),
905 + );
906 + return { ...(yield* modelsEffect), cancelled };
907 + }),
908 selectModel: Effect.fn("SpeechService.selectModel")((modelId) =>
909 exclusive(
910 "model selection",
907 - attempt("model selection", async () => {
908 - if (!getSpeechModel(modelId)) throw new SpeechModelNotFoundError({ modelId });
909 - await disposeModel();
911 + Effect.gen(function* () {
912 + const definition = getSpeechModel(modelId);
913 + if (!definition || !(yield* store.isReady(definition)))
914 + return yield* new SpeechModelNotFoundError({ modelId });
915 + yield* attempt("model selection", () => disposeModel());
916 + yield* writeSettings("model selection", { speechModelId: modelId });
917 }),
911 - ).pipe(
912 - Effect.andThen(writeSettings("model selection", { speechModelId: modelId })),
913 - Effect.andThen(modelsEffect),
914 - ),
918 + ).pipe(Effect.andThen(modelsEffect)),
919 ),
920 cancelDownload: Effect.fn("SpeechService.cancelDownload")((modelId) =>
921 Effect.suspend(() =>
@@ -921,40 +925,30 @@ const make = Effect.gen(function* () {
925 ).pipe(Effect.andThen(modelsEffect)),
926 ),
927 removeModel: Effect.fn("SpeechService.removeModel")((modelId) =>
924 - readSettings("model removal").pipe(
925 - Effect.flatMap((settings) =>
926 - exclusive(
927 - "model removal",
928 - Effect.gen(function* () {
929 - const definition = getSpeechModel(modelId);
930 - if (!definition) return yield* new SpeechModelNotFoundError({ modelId });
931 - yield* attempt("model removal", async () => {
932 - await loading?.catch(() => undefined);
933 - if (loadedModelId === modelId) await disposeModel();
934 - });
935 - yield* store
936 - .remove(definition)
937 - .pipe(Effect.mapError((cause) => speechError("model removal", cause)));
938 - if (selectedModel(settings).id !== modelId) return undefined;
939 - const candidates = yield* Effect.forEach(
940 - SPEECH_MODELS.filter((candidate) => candidate.id !== modelId),
941 - (candidate) =>
942 - store.isReady(candidate).pipe(Effect.map((ready) => ({ candidate, ready }))),
943 - { concurrency: "unbounded" },
944 - );
945 - return candidates.find(({ ready }) => ready)?.candidate.id;
946 - }),
947 - ).pipe(
948 - Effect.flatMap((replacementId) =>
949 - replacementId
950 - ? writeSettings("model removal", { speechModelId: replacementId }).pipe(
951 - Effect.andThen(modelsEffect),
952 - )
953 - : modelsEffect,
954 - ),
955 - ),
956 - ),
957 - ),
928 + exclusive(
929 + "model removal",
930 + Effect.gen(function* () {
931 + const settings = yield* readSettings("model removal");
932 + const definition = getSpeechModel(modelId);
933 + if (!definition) return yield* new SpeechModelNotFoundError({ modelId });
934 + yield* attempt("model removal", async () => {
935 + await loading?.catch(() => undefined);
936 + if (loadedModelId === modelId) await disposeModel();
937 + });
938 + yield* store
939 + .remove(definition)
940 + .pipe(Effect.mapError((cause) => speechError("model removal", cause)));
941 + if (selectedModel(settings).id !== modelId) return;
942 + const candidates = yield* Effect.forEach(
943 + SPEECH_MODELS.filter((candidate) => candidate.id !== modelId),
944 + (candidate) =>
945 + store.isReady(candidate).pipe(Effect.map((ready) => ({ candidate, ready }))),
946 + { concurrency: "unbounded" },
947 + );
948 + const replacement = candidates.find(({ ready }) => ready)?.candidate;
949 + if (replacement) yield* writeSettings("model removal", { speechModelId: replacement.id });
950 + }),
951 + ).pipe(Effect.andThen(modelsEffect)),
952 ),
953 linkModels: Effect.fn("SpeechService.linkModels")(function* (source) {
954 yield* requireSupported;
apps/server/src/speech/http.test.ts
+36 -22
@@ -15,6 +15,7 @@ import {
15 DEFAULT_SERVER_SETTINGS,
16 EnvironmentAuthenticatedAuth,
17 EnvironmentAuthenticatedPrincipal,
18 + type EnvironmentSpeechModel,
19 EnvironmentVoiceHttpApi,
20 } from "@t3tools/contracts";
21 import * as Effect from "effect/Effect";
@@ -79,7 +80,7 @@ effectIt.live("rejects oversized chunked audio without invoking transcription",
80 status: Effect.succeed({ supported: false as const, reason: "test" }),
81 prepareModel: Effect.void,
82 models: () => Effect.succeed({ models: [] }),
82 - downloadModel: () => Effect.succeed({ models: [] }),
83 + downloadModel: () => Effect.succeed({ models: [], cancelled: false }),
84 linkModels: () => Effect.succeed({ models: [] }),
85 selectModel: () => Effect.succeed({ models: [] }),
86 cancelDownload: () => Effect.succeed({ models: [] }),
@@ -161,33 +162,30 @@ effectIt.effect(
162 return Effect.fail(new CloudSpeechError({ provider, reason: "rejected-key" }));
163 },
164 });
165 + const linkedModel: EnvironmentSpeechModel = {
166 + id: "fixture/model",
167 + name: "Fixture",
168 + description: "",
169 + size: 1,
170 + languages: ["en"],
171 + accuracy: 0,
172 + speed: 0,
173 + recommended: false,
174 + supportsStreaming: false,
175 + supportsLanguageDetection: false,
176 + supportsTranslation: false,
177 + active: true,
178 + state: "installed",
179 + linkedFrom: "/home/someone/models/fixture.gguf",
180 + };
181 const { handler, dispose } = HttpRouter.toWebHandler(
182 HttpApiBuilder.layer(HttpApi.make("environment").add(EnvironmentVoiceHttpApi)).pipe(
183 Layer.provide(speechHttpApiLayer),
184 Layer.provide(principal(AuthOrchestrationOperateScope, AuthSettingsWriteScope)),
185 Layer.provide(
186 Layer.mock(SpeechService.SpeechService)({
170 - models: () =>
171 - Effect.succeed({
172 - models: [
173 - {
174 - id: "fixture/model",
175 - name: "Fixture",
176 - description: "",
177 - size: 1,
178 - languages: ["en"],
179 - accuracy: 0,
180 - speed: 0,
181 - recommended: false,
182 - supportsStreaming: false,
183 - supportsLanguageDetection: false,
184 - supportsTranslation: false,
185 - active: true,
186 - state: "installed",
187 - linkedFrom: "/home/someone/models/fixture.gguf",
188 - },
189 - ],
190 - }),
187 + models: () => Effect.succeed({ models: [linkedModel] }),
188 + downloadModel: () => Effect.succeed({ models: [linkedModel], cancelled: true }),
189 }),
190 ),
191 Layer.provide(cloud),
@@ -269,6 +267,22 @@ effectIt.effect(
267 handler(new Request("http://localhost/api/voice/models"), context),
268 );
269 expect(yield* Effect.promise(() => listed.text())).not.toContain("/home/someone");
270 + const downloaded = yield* Effect.promise(() =>
271 + handler(
272 + new Request("http://localhost/api/voice/models/download", {
273 + method: "POST",
274 + headers: { "content-type": "application/json" },
275 + body: JSON.stringify({ modelId: linkedModel.id }),
276 + }),
277 + context,
278 + ),
279 + );
280 + expect(downloaded.status).toBe(200);
281 + const { linkedFrom: _, ...publicModel } = linkedModel;
282 + expect(yield* Effect.promise(() => downloaded.json())).toEqual({
283 + models: [publicModel],
284 + cancelled: true,
285 + });
286 }),
287 );
288
apps/server/src/speech/http.ts
+10 -3
@@ -58,10 +58,17 @@ const mapSpeechError = <A, R>(effect: Effect.Effect<A, SpeechService.SpeechError
58
59 // A linked model's origin is a path on this machine; only filesystem readers may see it.
60 // Untraced: it runs inside each handler's own span.
61 -const withoutHostPaths = Effect.fnUntraced(function* (result: EnvironmentSpeechModels) {
61 +const withoutHostPaths = Effect.fnUntraced(function* <Fields extends EnvironmentSpeechModels>({
62 + models,
63 + ...fields
64 +}: Fields) {
65 const session = yield* EnvironmentAuthenticatedPrincipal;
63 - if (session.scopes.has(AuthFilesystemReadScope)) return result;
64 - return { models: result.models.map(({ linkedFrom: _, ...model }) => model) };
66 + return {
67 + ...fields,
68 + models: session.scopes.has(AuthFilesystemReadScope)
69 + ? models
70 + : models.map(({ linkedFrom: _, ...model }) => model),
71 + };
72 });
73
74 export const speechHttpApiLayer = HttpApiBuilder.group(
apps/web/src/speech/useEnvironmentSpeechInput.browser.test.tsx
+40 -8
@@ -47,7 +47,7 @@ const mocks = vi.hoisted(() => ({
47 statusModelId: "test-model",
48 models: [] as EnvironmentSpeechModel[],
49 catalogOffline: false,
50 - download: vi.fn<(modelId: string) => Promise<void>>(),
50 + download: vi.fn<(modelId: string) => Promise<boolean | void>>(),
51 selected: [] as string[],
52 selectionFails: false,
53 platforms: [] as string[],
@@ -142,8 +142,8 @@ vi.mock("@t3tools/client-runtime/voice-input", async (importOriginal) => ({
142 }),
143 downloadEnvironmentSpeechModel: (_prepared: PreparedConnection, modelId: string) =>
144 Effect.promise(async () => {
145 - await mocks.download(modelId);
146 - return { models: mocks.models };
145 + const cancelled = await mocks.download(modelId);
146 + return { models: mocks.models, cancelled: cancelled ?? false };
147 }),
148 selectEnvironmentSpeechModel: (_prepared: PreparedConnection, modelId: string) =>
149 Effect.sync(() => {
@@ -644,30 +644,62 @@ it.each([true, false])(
644 );
645
646 it.each([
647 - ["selects the offered model once its download installed it", "installed", ["german"], null],
647 + [
648 + "selects the offered model once its download installed it",
649 + "installed",
650 + ["german"],
651 + null,
652 + false,
653 + "english",
654 + ],
655 + [
656 + "selects the downloaded model after another client changed the selection",
657 + "installed",
658 + ["german"],
659 + null,
660 + false,
661 + "german",
662 + ],
663 [
664 "does not advance a cancelled download when another model is ready",
665 "downloadable",
666 [],
667 "The model is not ready. Try downloading it again.",
668 + true,
669 + "english",
670 + ],
671 + [
672 + "keeps setup open when an external cancellation leaves the weights installed",
673 + "installed",
674 + [],
675 + "The model is not ready. Try downloading it again.",
676 + true,
677 + "english",
678 ],
679 [
680 "reports a failed selection apart from the download",
681 "installed",
682 ["german"],
683 "The model downloaded, but this environment could not select it. Try again.",
684 + false,
685 + "english",
686 ],
660 -] as const)("%s", async (_, landedState, selected, error) => {
687 +] as const)("%s", async (_, landedState, selected, error, cancelled, initialModelId) => {
688 setup({ destination: "env-a" });
689 using _languages = vi.spyOn(navigator, "languages", "get").mockReturnValue(["de-DE"]);
663 - mocks.selectionFails = error !== null && landedState === "installed";
690 + mocks.selectionFails = error !== null && !cancelled;
691 mocks.modelState = "missing-model";
665 - mocks.statusModelId = "english";
692 + mocks.statusModelId = initialModelId;
693 mocks.models = [catalogModel("english", ["en"], true), catalogModel("german", ["de"], true)];
694 mocks.download.mockImplementation(async () => {
668 - mocks.models = [mocks.models[0]!, { ...mocks.models[1]!, state: landedState }];
695 + mocks.models = [
696 + { ...mocks.models[0]!, state: "installed", active: true },
697 + { ...mocks.models[1]!, state: landedState, active: false },
698 + ];
699 + mocks.statusModelId = "english";
700 mocks.modelState = "ready";
701 mocks.statusTier = "accelerated";
702 + return cancelled;
703 });
704 await using _screen = await render(<Probe />);
705 await act(() => mocks.voice.start());
apps/web/src/speech/useEnvironmentSpeechInput.ts
+8 -5
@@ -3,6 +3,7 @@ import {
3 downloadEnvironmentSpeechModel,
4 getEnvironmentSpeechModels,
5 getEnvironmentSpeechStatus,
6 + getSpeechModelDownloadOutcome,
7 selectEnvironmentSpeechModel,
8 voiceInputBlocksSubmission,
9 type VoiceInputState,
@@ -450,13 +451,12 @@ export function useEnvironmentSpeechInput(input: HookInput) {
451 const request = ++startRequestRef.current;
452 setSetup((current) => ({ ...current, downloading: true, error: null }));
453 try {
453 - const { models } = await runEnvironmentSpeech(
454 + const downloaded = await runEnvironmentSpeech(
455 downloadEnvironmentSpeechModel(prepared, modelId),
456 );
457 if (request !== startRequestRef.current) return;
457 - // A download cancelled from elsewhere still resolves, so select the model only once it landed.
458 - const installed = models.some(({ id, state }) => id === modelId && state === "installed");
459 - if (installed && modelId !== currentStatus.modelId) {
458 + const outcome = getSpeechModelDownloadOutcome(downloaded, modelId);
459 + if (outcome === "needs-selection") {
460 try {
461 await runEnvironmentSpeech(selectEnvironmentSpeechModel(prepared, modelId));
462 } catch {
@@ -476,7 +476,10 @@ export function useEnvironmentSpeechInput(input: HookInput) {
476 if (request !== startRequestRef.current) return;
477 setStatus({ prepared, value: next });
478 const ready =
479 - installed && next.supported && next.modelId === modelId && next.state !== "missing-model";
479 + outcome !== "incomplete" &&
480 + next.supported &&
481 + next.modelId === modelId &&
482 + next.state !== "missing-model";
483 setSetup((current) => ({
484 ...current,
485 downloading: false,
packages/client-runtime/src/voice-input/environment.test.ts new
+104
@@ -0,0 +1,104 @@
1 +import { describe, expect, it } from "@effect/vitest";
2 +import { EnvironmentId, type EnvironmentSpeechModel } from "@t3tools/contracts";
3 +import * as Effect from "effect/Effect";
4 +import { HttpClient, HttpClientResponse } from "effect/http";
5 +
6 +import { BearerConnectionTarget, type PreparedConnection } from "../connection/model.ts";
7 +import { installEnvironmentSpeechModel } from "./environment.ts";
8 +
9 +const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
10 +const PREPARED: PreparedConnection = {
11 + environmentId: ENVIRONMENT_ID,
12 + label: "Remote environment",
13 + httpBaseUrl: "https://remote.example.test",
14 + socketUrl: "wss://remote.example.test/ws",
15 + httpAuthorization: { _tag: "Bearer", token: "token" },
16 + target: new BearerConnectionTarget({
17 + environmentId: ENVIRONMENT_ID,
18 + label: "Remote environment",
19 + connectionId: "connection-1",
20 + }),
21 +};
22 +
23 +const model = (
24 + state: EnvironmentSpeechModel["state"],
25 + active: boolean,
26 +): EnvironmentSpeechModel => ({
27 + id: "voxtral-mini",
28 + name: "Voxtral Mini",
29 + description: "",
30 + size: 1,
31 + languages: ["en"],
32 + accuracy: 1,
33 + speed: 1,
34 + recommended: false,
35 + supportsStreaming: false,
36 + supportsLanguageDetection: false,
37 + supportsTranslation: false,
38 + active,
39 + state,
40 +});
41 +
42 +describe("installEnvironmentSpeechModel", () => {
43 + it.effect.each([
44 + {
45 + name: "keeps the current model after a cancelled download",
46 + downloaded: model("downloadable", false),
47 + selects: false,
48 + },
49 + {
50 + name: "selects the model once its download finishes",
51 + downloaded: model("installed", false),
52 + selects: true,
53 + },
54 + {
55 + name: "leaves a model already in use alone",
56 + downloaded: model("installed", true),
57 + selects: false,
58 + },
59 + ])("$name", ({ downloaded, selects }) =>
60 + Effect.gen(function* () {
61 + const paths: string[] = [];
62 + const client = HttpClient.make((request) => {
63 + const path = new URL(request.url).pathname;
64 + paths.push(path);
65 + const answer = path.endsWith("/select") ? model("installed", true) : downloaded;
66 + return Effect.succeed(
67 + HttpClientResponse.fromWeb(request, Response.json({ models: [answer] })),
68 + );
69 + });
70 +
71 + const { models } = yield* installEnvironmentSpeechModel(PREPARED, downloaded.id).pipe(
72 + Effect.provideService(HttpClient.HttpClient, client),
73 + );
74 +
75 + expect(paths).toEqual([
76 + "/api/voice/models/download",
77 + ...(selects ? ["/api/voice/models/select"] : []),
78 + ]);
79 + expect(models[0]?.active).toBe(selects || downloaded.active);
80 + }),
81 + );
82 +
83 + it.effect("keeps the current model when cancellation leaves the weights installed", () =>
84 + Effect.gen(function* () {
85 + const downloaded = model("installed", false);
86 + const client = HttpClient.make((request) =>
87 + Effect.succeed(
88 + HttpClientResponse.fromWeb(
89 + request,
90 + Response.json(
91 + new URL(request.url).pathname.endsWith("/select")
92 + ? { models: [model("installed", true)] }
93 + : { models: [downloaded], cancelled: true },
94 + ),
95 + ),
96 + ),
97 + );
98 + const { models } = yield* installEnvironmentSpeechModel(PREPARED, downloaded.id).pipe(
99 + Effect.provideService(HttpClient.HttpClient, client),
100 + );
101 + expect(models).toEqual([downloaded]);
102 + }),
103 + );
104 +});
packages/client-runtime/src/voice-input/environment.ts
+20
@@ -6,6 +6,7 @@ import { CloudSpeechError, SPEECH_STREAM_PATH } from "@t3tools/contracts";
6 import type {
7 CloudSpeechModelId,
8 CloudSpeechProvider,
9 + EnvironmentSpeechModelDownload,
10 SettingsSecretValue,
11 SpeechAcceleration,
12 SpeechModelUnloadTimeout,
@@ -213,6 +214,25 @@ export const selectEnvironmentSpeechModel = (prepared: PreparedConnection, model
214 run: ({ client, headers }) => client.selectModel({ headers, payload: { modelId } }),
215 });
216
217 +export function getSpeechModelDownloadOutcome(
218 + download: EnvironmentSpeechModelDownload,
219 + modelId: string,
220 +) {
221 + if (download.cancelled) return "incomplete";
222 + const model = download.models.find(({ id }) => id === modelId);
223 + if (model?.state !== "installed") return "incomplete";
224 + return model.active ? "already-selected" : "needs-selection";
225 +}
226 +
227 +/** Downloads a model and makes it the environment's transcription model on completion. */
228 +export const installEnvironmentSpeechModel = Effect.fn(
229 + "clientRuntime.voiceInput.installEnvironmentSpeechModel",
230 +)(function* (prepared: PreparedConnection, modelId: string) {
231 + const downloaded = yield* downloadEnvironmentSpeechModel(prepared, modelId);
232 + if (getSpeechModelDownloadOutcome(downloaded, modelId) !== "needs-selection") return downloaded;
233 + return yield* selectEnvironmentSpeechModel(prepared, modelId);
234 +});
235 +
236 export const cancelEnvironmentSpeechModelDownload = (
237 prepared: PreparedConnection,
238 modelId: string,
packages/client-runtime/src/voice-input/index.ts
+2
@@ -22,6 +22,8 @@ export {
22 transcribeCloudPcm,
23 getEnvironmentSpeechStatus,
24 getEnvironmentSpeechStreamUrl,
25 + getSpeechModelDownloadOutcome,
26 + installEnvironmentSpeechModel,
27 linkEnvironmentSpeechModels,
28 postProcessEnvironmentTranscript,
29 removeEnvironmentSpeechModel,
packages/contracts/src/environmentHttp.ts
+2 -1
@@ -62,6 +62,7 @@ import {
62 EnvironmentSpeechAccelerationRequest,
63 EnvironmentSpeechModelLinkRequest,
64 EnvironmentSpeechModelRequest,
65 + EnvironmentSpeechModelDownload,
66 EnvironmentSpeechModels,
67 EnvironmentSpeechModelUnloadTimeoutRequest,
68 EnvironmentSpeechPostProcessingRequest,
@@ -911,7 +912,7 @@ export class EnvironmentVoiceHttpApi extends HttpApiGroup.make("voice")
912 HttpApiEndpoint.post("downloadModel", "/api/voice/models/download", {
913 headers: OptionalBearerHeaders,
914 payload: EnvironmentSpeechModelRequest,
914 - success: EnvironmentSpeechModels,
915 + success: EnvironmentSpeechModelDownload,
916 error: [...EnvironmentScopedOperationErrors, EnvironmentRequestInvalidError],
917 }).middleware(EnvironmentAuthenticatedAuth),
918 )
packages/contracts/src/speech.ts
+7
@@ -204,6 +204,13 @@ export const EnvironmentSpeechModels = Schema.Struct({
204 });
205 export type EnvironmentSpeechModels = typeof EnvironmentSpeechModels.Type;
206
207 +export const EnvironmentSpeechModelDownload = Schema.Struct({
208 + ...EnvironmentSpeechModels.fields,
209 + // Older environments return only the catalog.
210 + cancelled: Schema.optionalKey(Schema.Boolean),
211 +});
212 +export type EnvironmentSpeechModelDownload = typeof EnvironmentSpeechModelDownload.Type;
213 +
214 export const EnvironmentSpeechModelRequest = Schema.Struct({ modelId: SpeechModelId });
215 /** A model file, or a folder to search for them, on the environment's own disk. */
216 export const EnvironmentSpeechModelLinkRequest = Schema.Struct({