File size: 1,310 Bytes
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 * Messages exchanged with `speechToTextWorker`, shared by both sides.
 *
 * Each dictation session gets its own worker; the worker loads the model
 * (browser-cached after the first press), runs the transcriber, and reports
 * the growing transcript back.
 */

export type WorkerRequest =
  | {
      type: "load";
      /** Canonical model filename -> URL to fetch it from. */
      modelFiles: Record<string, string>;
    }
  | {
      type: "audio";
      /** Detached Float32 PCM buffer, mono. */
      buffer: ArrayBuffer;
      sampleRate: number;
    }
  | { type: "stop" };

export type WorkerResponse =
  | { type: "loaded" }
  | {
      type: "progress";
      /** Cumulative bytes fetched across the whole model. */
      loaded: number;
      /** Undefined when the sizes are unknown; render as indeterminate. */
      total?: number;
    }
  | {
      type: "transcript";
      /** Everything dictated so far, completed lines and the in-progress one. */
      text: string;
    }
  | { type: "error"; message: string }
  /**
   * Sent after `transcriber.stop()` returns. The transcriber runs
   * synchronously inside `onmessage`, so a backlog of queued audio can leave
   * the worker seconds behind; terminating before this arrives drops the last
   * words.
   */
  | { type: "stopped" };