@mentra/cloud-client 3.2.1-dev.311 → 3.2.1

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.2.1-dev.311",
3
+ "version": "3.2.1",
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.2.1-dev.311",
15
+ "@mentra/cloud-protocol": "3.2.1",
16
16
  "tweetnacl": "^1.0.3"
17
17
  },
18
18
  "devDependencies": {
package/src/client.ts CHANGED
@@ -33,6 +33,7 @@ import {RuntimeEmitter} from "./modules/runtime/emitter"
33
33
  import {Subscriptions} from "./modules/runtime/subscriptions"
34
34
  import {Camera} from "./modules/runtime/camera"
35
35
  import {Maps} from "./modules/runtime/maps"
36
+ import {Meetings} from "./modules/runtime/meetings"
36
37
  import {Tts} from "./modules/runtime/tts"
37
38
  import {UdpAudio} from "./modules/runtime/audio-udp"
38
39
  import {Core} from "./modules/core/core"
@@ -247,6 +248,7 @@ export class CloudClient {
247
248
  camera,
248
249
  tts,
249
250
  maps,
251
+ meetings: new Meetings(runtimeHttp),
250
252
  audio,
251
253
  timers,
252
254
  logger,
package/src/index.ts CHANGED
@@ -16,6 +16,7 @@
16
16
 
17
17
  // The top-level object. Implemented in ./client by the client agent; re-exported
18
18
  // here so the public import is `@mentra/cloud-client`, not a deep path.
19
+ export type { AcsMeetingCredential, CreatedTeamsMeeting } from "./modules/runtime/meetings";
19
20
  export { CloudClient } from "./client";
20
21
  export { DEFAULT_REFRESH_TOKEN_KEY } from "./modules/auth/token-store";
21
22
 
@@ -78,6 +78,13 @@ export class UdpAudio {
78
78
  return this.session?.sessionTag ?? null;
79
79
  }
80
80
 
81
+ /** Refresh a dead UDP route without changing the session key, tag or sequence. */
82
+ resetSocket(): void {
83
+ if (!this.session) return;
84
+ this.session.socket.close();
85
+ this.session.socket = this.udpFactory();
86
+ }
87
+
81
88
  /**
82
89
  * Open the UDP socket and load the session's routing + encryption material
83
90
  * from `connection.ack.audio`.
@@ -0,0 +1,76 @@
1
+ import type {HttpClient} from "../../http"
2
+
3
+ /** Host-only credentials. Never forward this object to a miniapp. */
4
+ export interface AcsMeetingCredential {
5
+ token: string
6
+ expiresOn: string
7
+ identityMode: "guest" | "teams-user"
8
+ acsUserId?: string
9
+ guestReason?: "teams-license-unavailable"
10
+ }
11
+
12
+ export interface CreatedTeamsMeeting {
13
+ provider: "acs-teams"
14
+ joinUrl: string
15
+ meetingRef: string
16
+ identityMode: "guest" | "teams-user"
17
+ guestReason?: "no-entra-identity" | "teams-license-unavailable"
18
+ }
19
+
20
+ export class Meetings {
21
+ constructor(private readonly http: HttpClient) {}
22
+
23
+ async createTeamsMeeting(
24
+ options: {subject?: string; durationMinutes?: number},
25
+ teamsUserAadToken?: string,
26
+ ): Promise<CreatedTeamsMeeting> {
27
+ const value = await this.http.post<CreatedTeamsMeeting>("/api/meetings/teams/create", {
28
+ ...options,
29
+ ...(teamsUserAadToken ? {teamsUserAadToken} : {}),
30
+ })
31
+ if (
32
+ !value ||
33
+ value.provider !== "acs-teams" ||
34
+ typeof value.joinUrl !== "string" ||
35
+ !/^https:\/\/(teams\.microsoft\.com|teams\.cloud\.microsoft)\//.test(value.joinUrl) ||
36
+ typeof value.meetingRef !== "string" ||
37
+ !value.meetingRef ||
38
+ !["guest", "teams-user"].includes(value.identityMode) ||
39
+ (teamsUserAadToken && value.identityMode === "guest" && value.guestReason !== "teams-license-unavailable") ||
40
+ (!teamsUserAadToken && (value.identityMode !== "guest" || value.guestReason !== "no-entra-identity"))
41
+ ) {
42
+ throw new Error("Runtime returned an invalid created meeting")
43
+ }
44
+ return {
45
+ provider: value.provider,
46
+ joinUrl: value.joinUrl,
47
+ meetingRef: value.meetingRef,
48
+ identityMode: value.identityMode,
49
+ ...(value.identityMode === "guest" ? {guestReason: value.guestReason} : {}),
50
+ }
51
+ }
52
+
53
+ async retireTeamsMeeting(meetingRef: string): Promise<void> {
54
+ await this.http.post("/api/meetings/teams/retire", {meetingRef})
55
+ }
56
+
57
+ async getAcsCredential(teamsUserAadToken?: string): Promise<AcsMeetingCredential> {
58
+ const value = await this.http.post<AcsMeetingCredential>(
59
+ "/api/meetings/acs/token",
60
+ teamsUserAadToken ? {teamsUserAadToken} : {},
61
+ )
62
+ if (
63
+ !value ||
64
+ typeof value.token !== "string" ||
65
+ !value.token ||
66
+ !Number.isFinite(Date.parse(value.expiresOn)) ||
67
+ Date.parse(value.expiresOn) <= Date.now() ||
68
+ (value.identityMode !== "guest" && value.identityMode !== "teams-user") ||
69
+ (value.identityMode === "guest" && (typeof value.acsUserId !== "string" || !value.acsUserId)) ||
70
+ (teamsUserAadToken && value.identityMode === "guest" && value.guestReason !== "teams-license-unavailable") ||
71
+ (!teamsUserAadToken && value.identityMode !== "guest")
72
+ )
73
+ throw new Error("Runtime returned an invalid meeting credential")
74
+ return value
75
+ }
76
+ }
@@ -30,6 +30,7 @@ import { HandshakeRejectedError } from "./connection";
30
30
  import type { RuntimeEmitter, RuntimeEvents } from "./emitter";
31
31
  import type { Subscriptions } from "./subscriptions";
32
32
  import type { Camera, StreamOptions, ManagedStream, StreamStatusResult } from "./camera";
33
+ import type { Meetings } from "./meetings";
33
34
  import type { Maps, DirectionsRequest, DirectionsResult, LatLng, ReverseGeocodeResult } from "./maps";
34
35
  import type { Tts, RuntimeTtsSpeakOptions, RuntimeTtsSpeechSource } from "./tts";
35
36
  import type { UdpAudio } from "./audio-udp";
@@ -68,6 +69,7 @@ export type { RuntimeStatus, RuntimeSnapshot } from "./status";
68
69
  * unsubscribe function.
69
70
  */
70
71
  export interface RuntimeModule {
72
+ readonly meetings: Meetings;
71
73
  connect(): Promise<void>;
72
74
  close(): void;
73
75
 
@@ -111,6 +113,7 @@ export interface RuntimeModule {
111
113
  }
112
114
 
113
115
  export interface RuntimeDeps {
116
+ meetings?: Meetings;
114
117
  connection: Connection;
115
118
  emitter: RuntimeEmitter;
116
119
  subscriptions: Subscriptions;
@@ -136,6 +139,11 @@ export interface RuntimeDeps {
136
139
  const AUTH_EXPIRED_CODE = "AUTH_EXPIRED";
137
140
 
138
141
  export class Runtime implements RuntimeModule {
142
+ private readonly meetingService?: Meetings;
143
+ get meetings(): Meetings {
144
+ if (!this.meetingService) throw new Error("Meeting credentials are unavailable");
145
+ return this.meetingService;
146
+ }
139
147
  private readonly connection: Connection;
140
148
  private readonly emitter: RuntimeEmitter;
141
149
  private readonly subscriptions: Subscriptions;
@@ -154,6 +162,7 @@ export class Runtime implements RuntimeModule {
154
162
  private udpProbeTimer: unknown | null = null;
155
163
  private udpProbeStartedAt = 0;
156
164
  private lastUdpAckAt = 0;
165
+ private readonly pendingUdpProbes = new Map<string, number>();
157
166
 
158
167
  /**
159
168
  * Whether the inbound-message routing has been wired to the connection yet.
@@ -175,6 +184,7 @@ export class Runtime implements RuntimeModule {
175
184
  private opened = false;
176
185
 
177
186
  constructor(deps: RuntimeDeps) {
187
+ this.meetingService = deps.meetings;
178
188
  this.connection = deps.connection;
179
189
  this.emitter = deps.emitter;
180
190
  this.subscriptions = deps.subscriptions;
@@ -384,7 +394,9 @@ export class Runtime implements RuntimeModule {
384
394
  private configureAudio(ack: ConnectionAck): void {
385
395
  if (ack.audio) {
386
396
  this.audio.configure(ack.audio);
387
- this.updateStatus({ audioTransport: "udp" });
397
+ // Configured credentials do not prove DNS/connect or UDP delivery works.
398
+ // Use the already-open WS while probes establish the new UDP route.
399
+ this.updateStatus({ audioTransport: this.connection.isOpen ? "ws" : "none" });
388
400
  this.startUdpLiveness();
389
401
  return;
390
402
  }
@@ -527,8 +539,8 @@ export class Runtime implements RuntimeModule {
527
539
  this.lastUdpAckAt = 0;
528
540
  this.sendUdpProbe();
529
541
  this.udpProbeTimer = this.timers.setInterval(() => {
530
- this.sendUdpProbe();
531
542
  this.checkUdpLiveness();
543
+ this.sendUdpProbe();
532
544
  }, UDP_PROBE_INTERVAL_MS);
533
545
  }
534
546
 
@@ -539,12 +551,18 @@ export class Runtime implements RuntimeModule {
539
551
  }
540
552
  this.udpProbeStartedAt = 0;
541
553
  this.lastUdpAckAt = 0;
554
+ this.pendingUdpProbes.clear();
542
555
  }
543
556
 
544
557
  private sendUdpProbe(): void {
545
558
  const tag = this.audio.sessionTag;
546
559
  if (tag === null) return;
547
- const probeId = `${tag}-${Date.now()}-${Math.floor(Math.random() * 1_000_000)}`;
560
+ const now = Date.now();
561
+ for (const [id, sentAt] of this.pendingUdpProbes) {
562
+ if (now - sentAt >= UDP_LIVENESS_TIMEOUT_MS) this.pendingUdpProbes.delete(id);
563
+ }
564
+ const probeId = `${tag}-${now}-${Math.floor(Math.random() * 1_000_000)}`;
565
+ this.pendingUdpProbes.set(probeId, now);
548
566
  this.audio.sendProbe(probeId);
549
567
  }
550
568
 
@@ -553,6 +571,14 @@ export class Runtime implements RuntimeModule {
553
571
  const since = this.lastUdpAckAt || this.udpProbeStartedAt;
554
572
  if (since === 0 || Date.now() - since < UDP_LIVENESS_TIMEOUT_MS) return;
555
573
  this.updateStatus({ audioTransport: this.connection.isOpen ? "ws" : "none" });
574
+ // Connected UDP sockets retain their resolved peer and route. Recreate one
575
+ // after a missed liveness window so DNS/network changes can recover even
576
+ // when the WebSocket stayed open. Only this replacement's probes may restore
577
+ // UDP: an old socket's delayed ack does not establish the new route works.
578
+ this.pendingUdpProbes.clear();
579
+ this.audio.resetSocket();
580
+ this.lastUdpAckAt = 0;
581
+ this.udpProbeStartedAt = Date.now();
556
582
  }
557
583
 
558
584
  private handleUdpLivenessAck(payload: {
@@ -562,6 +588,9 @@ export class Runtime implements RuntimeModule {
562
588
  receivedAt: number;
563
589
  }): void {
564
590
  if (payload.sessionTag !== this.audio.sessionTag) return;
591
+ const sentAt = this.pendingUdpProbes.get(payload.probeId);
592
+ this.pendingUdpProbes.delete(payload.probeId);
593
+ if (sentAt === undefined || Date.now() - sentAt >= UDP_LIVENESS_TIMEOUT_MS) return;
565
594
  this.lastUdpAckAt = Date.now();
566
595
  if (this.status.status === "connected") {
567
596
  this.updateStatus({ audioTransport: "udp" });