@mentra/cloud-client 3.4.0-dev.1169 → 3.4.0-dev.1171

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentra/cloud-client",
3
- "version": "3.4.0-dev.1169",
3
+ "version": "3.4.0-dev.1171",
4
4
  "type": "module",
5
5
  "main": "./src/index.ts",
6
6
  "exports": {
@@ -12,7 +12,7 @@
12
12
  "test": "bun test"
13
13
  },
14
14
  "dependencies": {
15
- "@mentra/cloud-protocol": "3.4.0-dev.1169",
15
+ "@mentra/cloud-protocol": "3.4.0-dev.1171",
16
16
  "tweetnacl": "^1.0.3"
17
17
  },
18
18
  "devDependencies": {
package/src/index.ts CHANGED
@@ -58,6 +58,7 @@ export type {
58
58
  RuntimeAudioTransport,
59
59
  RuntimeSnapshot,
60
60
  RuntimeStatus,
61
+ RuntimeTtsFormat,
61
62
  RuntimeTtsSpeakOptions,
62
63
  RuntimeTtsSpeechSource,
63
64
  } from "./modules/runtime/runtime";
@@ -70,7 +70,7 @@ export type {
70
70
  RouteAvoidances,
71
71
  ReverseGeocodeResult,
72
72
  } from "./maps";
73
- export type { RuntimeTtsSpeakOptions, RuntimeTtsSpeechSource } from "./tts";
73
+ export type { RuntimeTtsFormat, RuntimeTtsSpeakOptions, RuntimeTtsSpeechSource } from "./tts";
74
74
  export type { RuntimeAudioTransport } from "./audio-udp";
75
75
  export type { RuntimeStatus, RuntimeSnapshot } from "./status";
76
76
 
@@ -10,7 +10,24 @@ import type { HttpClient } from "../../http";
10
10
 
11
11
  const TTS_PATH = "/api/tts/speak";
12
12
 
13
+ /**
14
+ * Audio encoding of the returned source. `mp3` (default) plays in a media
15
+ * player. `pcm_24000` is raw signed 16-bit little-endian mono PCM at 24 kHz,
16
+ * for hosts that stream it into a PCM player as it downloads.
17
+ */
18
+ export type RuntimeTtsFormat = "mp3" | "pcm_24000";
19
+
20
+ /** Content-type prefix the runtime must return for each format. */
21
+ const FORMAT_CONTENT_TYPE: Record<RuntimeTtsFormat, string> = {
22
+ mp3: "audio/",
23
+ pcm_24000: "audio/pcm",
24
+ };
25
+
13
26
  export interface RuntimeTtsSpeakOptions {
27
+ /** Reuse a successful runtime readiness check for 30 seconds. */
28
+ reusePreflight?: boolean;
29
+ /** Audio encoding to request. Default `mp3`. */
30
+ format?: RuntimeTtsFormat;
14
31
  voiceId?: string;
15
32
  voice_id?: string;
16
33
  modelId?: string;
@@ -31,6 +48,7 @@ export interface TtsDeps {
31
48
 
32
49
  export class Tts {
33
50
  private readonly http: HttpClient;
51
+ private ready?: { endpoint: string; format: RuntimeTtsFormat; contentType: string; expiresAt: number };
34
52
 
35
53
  constructor(deps: TtsDeps) {
36
54
  this.http = deps.http;
@@ -49,11 +67,31 @@ export class Tts {
49
67
  throw new Error("runtime.tts.speak requires text");
50
68
  }
51
69
 
70
+ const format = options.format ?? "mp3";
52
71
  const path = this.buildTtsPath(text, options);
53
- const res = await this.http.head(path, { idempotent: true });
54
- const contentType = res.headers.get("content-type") ?? "";
55
- if (!contentType.toLowerCase().startsWith("audio/")) {
56
- throw new Error(`Runtime TTS returned non-audio content-type: ${contentType || "missing"}`);
72
+ const endpoint = this.http.url(TTS_PATH);
73
+ let contentType: string;
74
+ const ready = this.ready;
75
+ if (
76
+ options.reusePreflight &&
77
+ ready?.endpoint === endpoint &&
78
+ ready.format === format &&
79
+ ready.expiresAt > Date.now()
80
+ ) {
81
+ contentType = ready.contentType;
82
+ } else {
83
+ const res = await this.http.head(path, { idempotent: true });
84
+ contentType = res.headers.get("content-type") ?? "";
85
+ const normalized = contentType.toLowerCase();
86
+ if (!normalized.startsWith("audio/")) {
87
+ throw new Error(`Runtime TTS returned non-audio content-type: ${contentType || "missing"}`);
88
+ }
89
+ // A runtime that predates `format` ignores it and answers MP3. Playing
90
+ // those bytes as PCM would be noise, so refuse before playback.
91
+ if (!normalized.startsWith(FORMAT_CONTENT_TYPE[format])) {
92
+ throw new Error(`Runtime TTS does not support format ${format} (returned ${contentType})`);
93
+ }
94
+ if (options.reusePreflight) this.ready = { endpoint, format, contentType, expiresAt: Date.now() + 30_000 };
57
95
  }
58
96
 
59
97
  return {
@@ -76,6 +114,9 @@ export class Tts {
76
114
  const voiceSettings = options.voiceSettings ?? options.voice_settings;
77
115
  if (voiceSettings) query.set("voice_settings", JSON.stringify(voiceSettings));
78
116
 
117
+ // MP3 is the runtime default; leave its URL unchanged for older runtimes.
118
+ if (options.format && options.format !== "mp3") query.set("format", options.format);
119
+
79
120
  return `${TTS_PATH}?${query.toString()}`;
80
121
  }
81
122
  }