@ciridae/voice-conversations-react 0.0.0-stage → 0.1.0-nightly.0.bootstrap

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.
Files changed (100) hide show
  1. package/README.md +30 -2
  2. package/components/BrowserTalkButton.tsx +96 -0
  3. package/components/CallBanner.tsx +43 -0
  4. package/components/CallCaptions.tsx +38 -0
  5. package/components/CallEntry.tsx +89 -0
  6. package/components/CallTranscript.tsx +58 -0
  7. package/components/RecentCallEntries.tsx +211 -0
  8. package/components/describeBrowserTalkError.ts +39 -0
  9. package/components/formatCallClock.ts +9 -0
  10. package/components/useEndPresentCall.ts +45 -0
  11. package/components/voiceCall.css +225 -0
  12. package/dist/MalformedVoiceResponseError.d.ts +6 -0
  13. package/dist/MalformedVoiceResponseError.js +9 -0
  14. package/dist/NumberToCallQrCode.d.ts +12 -0
  15. package/dist/NumberToCallQrCode.js +29 -0
  16. package/dist/PhoneNumberDialog.d.ts +25 -0
  17. package/dist/PhoneNumberDialog.js +333 -0
  18. package/dist/PhoneNumbersAdminPanel.d.ts +11 -0
  19. package/dist/PhoneNumbersAdminPanel.js +85 -0
  20. package/dist/VoiceApiError.d.ts +18 -0
  21. package/dist/VoiceApiError.js +19 -0
  22. package/dist/VoiceRequestTimedOutError.d.ts +6 -0
  23. package/dist/VoiceRequestTimedOutError.js +9 -0
  24. package/dist/averageSpeechBinsIntoBands.d.ts +6 -0
  25. package/dist/averageSpeechBinsIntoBands.js +20 -0
  26. package/dist/browserApisForBrowserTalk.d.ts +17 -0
  27. package/dist/browserApisForBrowserTalk.js +42 -0
  28. package/dist/browserTalkError.d.ts +36 -0
  29. package/dist/browserTalkError.js +1 -0
  30. package/dist/chooseCallToShowCaptionsOf.d.ts +22 -0
  31. package/dist/chooseCallToShowCaptionsOf.js +26 -0
  32. package/dist/createBrowserTalk.d.ts +56 -0
  33. package/dist/createBrowserTalk.js +414 -0
  34. package/dist/createBrowserTalkErrorFollower.d.ts +17 -0
  35. package/dist/createBrowserTalkErrorFollower.js +76 -0
  36. package/dist/createCallPresenceWatcher.d.ts +25 -0
  37. package/dist/createCallPresenceWatcher.js +66 -0
  38. package/dist/createLiveCallCaptionsPoller.d.ts +24 -0
  39. package/dist/createLiveCallCaptionsPoller.js +58 -0
  40. package/dist/createVisibleTabPoller.d.ts +13 -0
  41. package/dist/createVisibleTabPoller.js +56 -0
  42. package/dist/deriveCallPresence.d.ts +23 -0
  43. package/dist/deriveCallPresence.js +58 -0
  44. package/dist/describePhoneNumberError.d.ts +7 -0
  45. package/dist/describePhoneNumberError.js +29 -0
  46. package/dist/entries/index.d.ts +26 -0
  47. package/dist/entries/index.js +22 -0
  48. package/dist/followBrowserTalkErrorAgainstPresence.d.ts +23 -0
  49. package/dist/followBrowserTalkErrorAgainstPresence.js +52 -0
  50. package/dist/formatPhoneNumberAsTyped.d.ts +31 -0
  51. package/dist/formatPhoneNumberAsTyped.js +85 -0
  52. package/dist/generated/index.d.ts +1 -0
  53. package/dist/generated/index.js +2 -0
  54. package/dist/generated/types.gen.d.ts +795 -0
  55. package/dist/generated/types.gen.js +2 -0
  56. package/dist/getBrowserTalkForThisTab.d.ts +3 -0
  57. package/dist/getBrowserTalkForThisTab.js +13 -0
  58. package/dist/getCallPresenceWatcherForThisTab.d.ts +3 -0
  59. package/dist/getCallPresenceWatcherForThisTab.js +9 -0
  60. package/dist/mapStartBrowserTalkFailureToBrowserTalkError.d.ts +7 -0
  61. package/dist/mapStartBrowserTalkFailureToBrowserTalkError.js +60 -0
  62. package/dist/parseVoiceApiResponses.d.ts +11 -0
  63. package/dist/parseVoiceApiResponses.js +344 -0
  64. package/dist/readCallSummaryUntilItHasDuration.d.ts +12 -0
  65. package/dist/readCallSummaryUntilItHasDuration.js +45 -0
  66. package/dist/readTheBrowsersTimeZone.d.ts +8 -0
  67. package/dist/readTheBrowsersTimeZone.js +17 -0
  68. package/dist/readTimestampMilliseconds.d.ts +8 -0
  69. package/dist/readTimestampMilliseconds.js +10 -0
  70. package/dist/requestEndOfCall.d.ts +7 -0
  71. package/dist/requestEndOfCall.js +11 -0
  72. package/dist/startBrowserTalkErrorFollowerForThisTab.d.ts +6 -0
  73. package/dist/startBrowserTalkErrorFollowerForThisTab.js +12 -0
  74. package/dist/useAdminPhoneNumbers.d.ts +47 -0
  75. package/dist/useAdminPhoneNumbers.js +76 -0
  76. package/dist/useBrowserTalk.d.ts +10 -0
  77. package/dist/useBrowserTalk.js +23 -0
  78. package/dist/useBrowserTalkErrorWhileItApplies.d.ts +10 -0
  79. package/dist/useBrowserTalkErrorWhileItApplies.js +13 -0
  80. package/dist/useCallPresence.d.ts +5 -0
  81. package/dist/useCallPresence.js +34 -0
  82. package/dist/useCallSummaries.d.ts +9 -0
  83. package/dist/useCallSummaries.js +22 -0
  84. package/dist/useCallSummary.d.ts +15 -0
  85. package/dist/useCallSummary.js +21 -0
  86. package/dist/useFinePointerWithHover.d.ts +10 -0
  87. package/dist/useFinePointerWithHover.js +19 -0
  88. package/dist/useLiveCallCaptions.d.ts +8 -0
  89. package/dist/useLiveCallCaptions.js +27 -0
  90. package/dist/useLiveCallCaptionsOfLatestCall.d.ts +13 -0
  91. package/dist/useLiveCallCaptionsOfLatestCall.js +23 -0
  92. package/dist/usePhoneNumber.d.ts +45 -0
  93. package/dist/usePhoneNumber.js +125 -0
  94. package/dist/voiceApiModels.d.ts +85 -0
  95. package/dist/voiceApiModels.js +1 -0
  96. package/dist/voiceConversationsApi.d.ts +44 -0
  97. package/dist/voiceConversationsApi.js +227 -0
  98. package/dist/voiceConversationsSettings.d.ts +20 -0
  99. package/dist/voiceConversationsSettings.js +26 -0
  100. package/package.json +41 -4
@@ -0,0 +1,414 @@
1
+ import { mapStartBrowserTalkFailureToBrowserTalkError } from "./mapStartBrowserTalkFailureToBrowserTalkError.js";
2
+ import { readTimestampMilliseconds } from "./readTimestampMilliseconds.js";
3
+ const ICE_GATHERING_TIMEOUT_MS = 10000;
4
+ const WAIT_FOR_SESSION_CLOSED_MS = 15000;
5
+ // A peer that stays disconnected this long has dropped; a brief blip recovers on its own.
6
+ const DISCONNECTED_DROP_MS = 5000;
7
+ const VOICE_NOT_READY_RETRY_MS = 1000;
8
+ export function createBrowserTalk(browser, backend) {
9
+ let state = { status: "idle", error: null };
10
+ let held = null;
11
+ let closeTimer;
12
+ let dropTimer;
13
+ // The start response's timing, held until `session.started` makes the browser talk live.
14
+ let timingAwaitingSessionStart = null;
15
+ const listeners = new Set();
16
+ // The presence read lags a browser talk's end by a poll, so these never read as "elsewhere".
17
+ const callIdsOfBrowserTalksStartedHere = new Set();
18
+ /** The status now; another press may have changed it while this one awaited. */
19
+ function readStatusAfterAwait() {
20
+ return state.status;
21
+ }
22
+ function setState(next) {
23
+ state = next;
24
+ for (const listener of listeners)
25
+ listener();
26
+ }
27
+ /** Let go of the microphone and the peer; with "tell_server", ask the backend to end it too. */
28
+ function releaseBrowserTalk(error, serverNotice) {
29
+ clearTimeout(closeTimer);
30
+ clearTimeout(dropTimer);
31
+ if (held) {
32
+ const { callId } = held;
33
+ if (serverNotice === "tell_server" && callId)
34
+ sendEndWithoutWaiting(callId);
35
+ for (const track of held.microphone.getTracks())
36
+ track.stop();
37
+ held.dataChannel.close();
38
+ held.peer.close();
39
+ held.audio.close();
40
+ held = null;
41
+ }
42
+ setState({ status: "idle", error });
43
+ }
44
+ function sendEndWithoutWaiting(callId) {
45
+ backend.postEndCall(callId, "keepalive").catch((failure) => {
46
+ // Best effort: GPT-Live's connection_lost close ends the row if this never lands.
47
+ console.warn(`Ending browser talk call_id=${callId} from this tab failed`, failure);
48
+ });
49
+ }
50
+ async function start() {
51
+ if (state.status !== "idle")
52
+ return;
53
+ setState({ status: "requesting_microphone" });
54
+ let microphone;
55
+ try {
56
+ microphone = await browser.requestMicrophone();
57
+ }
58
+ catch (failure) {
59
+ // A denial after a cancel is the cancel: the session is already idle with no error.
60
+ if (readStatusAfterAwait() !== "requesting_microphone")
61
+ return;
62
+ setState({ status: "idle", error: mapMicrophoneFailure(failure) });
63
+ return;
64
+ }
65
+ if (readStatusAfterAwait() !== "requesting_microphone") {
66
+ // Cancelled while the browser asked for the microphone.
67
+ for (const track of microphone.getTracks())
68
+ track.stop();
69
+ return;
70
+ }
71
+ setState({ status: "connecting", callId: null });
72
+ let peer = null;
73
+ let audio;
74
+ try {
75
+ peer = browser.createPeerConnection();
76
+ audio = browser.createBrowserTalkAudio(microphone);
77
+ }
78
+ catch (failure) {
79
+ console.warn("A browser talk connection could not be set up; no server call was made", failure);
80
+ peer?.close();
81
+ for (const track of microphone.getTracks())
82
+ track.stop();
83
+ setState({ status: "idle", error: { kind: "did_not_connect" } });
84
+ return;
85
+ }
86
+ peer.addEventListener("track", (event) => {
87
+ // A track queued before the browser talk was released would play into a closed AudioContext.
88
+ if (held === mine)
89
+ audio.playReply(new MediaStream([event.track]));
90
+ });
91
+ for (const track of microphone.getAudioTracks())
92
+ peer.addTrack(track, microphone);
93
+ // The event channel before the offer, as OpenAI's WebRTC guide orders it.
94
+ const dataChannel = peer.createDataChannel("oai-events");
95
+ const mine = { peer, dataChannel, microphone, audio, callId: null };
96
+ held = mine;
97
+ dataChannel.addEventListener("message", (event) => {
98
+ if (held === mine)
99
+ readDataChannelEvent(mine, event.data);
100
+ });
101
+ dataChannel.addEventListener("close", () => {
102
+ if (held !== mine)
103
+ return;
104
+ if (state.status === "ending")
105
+ releaseBrowserTalk(null, "quiet");
106
+ else
107
+ releaseBrowserTalk(readPeerLossError(), "tell_server");
108
+ });
109
+ peer.addEventListener("connectionstatechange", () => {
110
+ watchForDroppedPeer(mine);
111
+ });
112
+ let offerSdp;
113
+ try {
114
+ await peer.setLocalDescription(await peer.createOffer());
115
+ await waitForIceGathering(peer);
116
+ offerSdp = peer.localDescription?.sdp ?? "";
117
+ if (offerSdp === "")
118
+ throw new Error("The peer produced no local offer");
119
+ }
120
+ catch (failure) {
121
+ console.warn("A browser talk offer could not be prepared; no server call was made", failure);
122
+ if (held === mine)
123
+ releaseBrowserTalk({ kind: "did_not_connect" }, "quiet");
124
+ return;
125
+ }
126
+ // Cancelled while ICE gathered: no call was made, so there is nothing to end.
127
+ if (held !== mine)
128
+ return;
129
+ let started;
130
+ try {
131
+ started = await postStartBrowserTalkRetryingOnceWhenNotReady(offerSdp, mine);
132
+ }
133
+ catch (failure) {
134
+ let browserTalkError;
135
+ try {
136
+ browserTalkError = mapStartBrowserTalkFailureToBrowserTalkError(failure);
137
+ }
138
+ catch (unexpected) {
139
+ // An unexpected failure (a rejected auth callback) still releases the microphone; it
140
+ // reaches the caller as itself, not as a browser talk error.
141
+ if (held === mine)
142
+ releaseBrowserTalk(null, "quiet");
143
+ throw unexpected;
144
+ }
145
+ if (held === mine)
146
+ releaseBrowserTalk(browserTalkError, "quiet");
147
+ return;
148
+ }
149
+ if (started === "cancelled")
150
+ return;
151
+ mine.callId = started.call_id;
152
+ callIdsOfBrowserTalksStartedHere.add(started.call_id);
153
+ if (held !== mine) {
154
+ // Cancelled while the server created it: end what was made.
155
+ sendEndWithoutWaiting(started.call_id);
156
+ return;
157
+ }
158
+ const timing = {
159
+ callId: started.call_id,
160
+ startedAt: started.started_at,
161
+ endsAt: started.ends_at,
162
+ serverOffsetMs: readTimestampMilliseconds(started.server_now) - Date.now(),
163
+ };
164
+ timingAwaitingSessionStart = timing;
165
+ setState({ status: "connecting", callId: started.call_id });
166
+ try {
167
+ await peer.setRemoteDescription({ type: "answer", sdp: started.sdp });
168
+ }
169
+ catch (failure) {
170
+ console.warn(`The browser talk answer for call_id=${started.call_id} was rejected by the peer`, failure);
171
+ if (held === mine)
172
+ releaseBrowserTalk({ kind: "did_not_connect" }, "tell_server");
173
+ }
174
+ }
175
+ /** The started call, or "cancelled" when the member cancelled during the not-ready wait. */
176
+ async function postStartBrowserTalkRetryingOnceWhenNotReady(sdp, mine) {
177
+ const request = { sdp, time_zone: browser.readTimeZone() };
178
+ try {
179
+ return await backend.postStartBrowserTalk(request);
180
+ }
181
+ catch (failure) {
182
+ if (mapStartBrowserTalkFailureToBrowserTalkError(failure).kind !== "voice_not_ready")
183
+ throw failure;
184
+ console.warn("POST /browser-talks answered voice_not_ready; retrying once after 1 s");
185
+ await new Promise((resolve) => setTimeout(resolve, VOICE_NOT_READY_RETRY_MS));
186
+ if (held !== mine)
187
+ return "cancelled";
188
+ return backend.postStartBrowserTalk(request);
189
+ }
190
+ }
191
+ function readDataChannelEvent(mine, data) {
192
+ const event = parseDataChannelEvent(data);
193
+ if (event === null)
194
+ return;
195
+ switch (event.type) {
196
+ case "session.started":
197
+ if (state.status === "connecting" &&
198
+ mine.callId !== null &&
199
+ timingAwaitingSessionStart?.callId === mine.callId)
200
+ setState({
201
+ ...timingAwaitingSessionStart,
202
+ status: "live",
203
+ muted: false,
204
+ });
205
+ return;
206
+ case "session.closed":
207
+ onSessionClosed(event.reason);
208
+ return;
209
+ }
210
+ }
211
+ function onSessionClosed(reason) {
212
+ if (state.status === "ending") {
213
+ releaseBrowserTalk(null, "quiet");
214
+ return;
215
+ }
216
+ const closedCallId = held?.callId ?? null;
217
+ if (reason === "expired")
218
+ releaseBrowserTalk({ kind: "time_limit_reached" }, "quiet");
219
+ else if (reason === "connection_lost")
220
+ releaseBrowserTalk({ kind: "connection_dropped" }, "quiet");
221
+ else if (reason === "content")
222
+ releaseBrowserTalk({ kind: "stopped_by_content_filter" }, "quiet");
223
+ else {
224
+ releaseBrowserTalk(null, "quiet");
225
+ if (closedCallId !== null)
226
+ void showWhyTheServerEndedTheBrowserTalk(closedCallId, state);
227
+ }
228
+ }
229
+ /**
230
+ * A close this tab did not ask for: the server says why (another tab's End, phone-wins, the
231
+ * hour cap). It records the reason before it closes the session, so one read is enough. The
232
+ * reason is shown only while the state is still the idle one this close left: a browser talk started
233
+ * (and cancelled or finished) during the read is not given the earlier call's reason.
234
+ */
235
+ async function showWhyTheServerEndedTheBrowserTalk(callId, stateAfterClose) {
236
+ let endReason;
237
+ try {
238
+ endReason = (await backend.getCallCaptions(callId)).end_reason;
239
+ }
240
+ catch (failure) {
241
+ console.warn(`Reading why browser talk call_id=${callId} ended failed; showing it as ended with no reason`, failure);
242
+ return;
243
+ }
244
+ if (endReason === null)
245
+ console.warn(`Browser talk call_id=${callId} closed but the server gave no end reason; showing it as ended with no reason`);
246
+ const error = mapCallEndReasonToBrowserTalkError(endReason);
247
+ if (error !== null && state === stateAfterClose)
248
+ setState({ status: "idle", error });
249
+ }
250
+ function watchForDroppedPeer(mine) {
251
+ if (held !== mine)
252
+ return;
253
+ clearTimeout(dropTimer);
254
+ const peerState = mine.peer.connectionState;
255
+ if (state.status === "ending" && (peerState === "failed" || peerState === "disconnected")) {
256
+ // The member pressed End: the peer tearing down is that hang-up, not a dropped call.
257
+ releaseBrowserTalk(null, "quiet");
258
+ return;
259
+ }
260
+ if (peerState === "failed")
261
+ releaseBrowserTalk(readPeerLossError(), "tell_server");
262
+ else if (peerState === "disconnected")
263
+ dropTimer = setTimeout(() => {
264
+ if (held === mine)
265
+ releaseBrowserTalk(readPeerLossError(), "tell_server");
266
+ }, DISCONNECTED_DROP_MS);
267
+ }
268
+ /** A peer or data channel lost before `session.started` never connected; one lost after it dropped. */
269
+ function readPeerLossError() {
270
+ return { kind: state.status === "connecting" ? "did_not_connect" : "connection_dropped" };
271
+ }
272
+ async function end() {
273
+ if (state.status !== "live")
274
+ return;
275
+ const ending = {
276
+ status: "ending",
277
+ callId: state.callId,
278
+ startedAt: state.startedAt,
279
+ endsAt: state.endsAt,
280
+ serverOffsetMs: state.serverOffsetMs,
281
+ };
282
+ setState(ending);
283
+ // A disconnect timer still running is not a drop any more: the member hung up.
284
+ clearTimeout(dropTimer);
285
+ const holdBrowserTalk = held;
286
+ closeTimer = setTimeout(() => {
287
+ if (held === holdBrowserTalk)
288
+ releaseBrowserTalk(null, "quiet");
289
+ }, WAIT_FOR_SESSION_CLOSED_MS);
290
+ try {
291
+ const outcome = await backend.postEndCall(ending.callId, "normal");
292
+ if (outcome === "already_ended" && held === holdBrowserTalk)
293
+ releaseBrowserTalk(null, "quiet");
294
+ }
295
+ catch (failure) {
296
+ console.warn(`Ending browser talk call_id=${ending.callId} failed; releasing the microphone`, failure);
297
+ if (held === holdBrowserTalk)
298
+ releaseBrowserTalk(null, "quiet");
299
+ }
300
+ }
301
+ function cancel() {
302
+ if (state.status === "requesting_microphone")
303
+ setState({ status: "idle", error: null });
304
+ else if (state.status === "connecting")
305
+ releaseBrowserTalk(null, "tell_server");
306
+ }
307
+ function dismissError() {
308
+ if (state.status === "idle" && state.error !== null)
309
+ setState({ status: "idle", error: null });
310
+ }
311
+ function toggleMute() {
312
+ if (state.status !== "live" || !held)
313
+ return;
314
+ const muted = !state.muted;
315
+ for (const track of held.microphone.getAudioTracks())
316
+ track.enabled = !muted;
317
+ setState({ ...state, muted });
318
+ }
319
+ // A tab closing or reloading in the middle of a browser talk ends its session at once, not when GPT-Live notices.
320
+ browser.listenForPageHide(() => {
321
+ if (held)
322
+ releaseBrowserTalk(null, "tell_server");
323
+ });
324
+ return {
325
+ subscribe: (listener) => {
326
+ listeners.add(listener);
327
+ return () => {
328
+ listeners.delete(listener);
329
+ };
330
+ },
331
+ getState: () => state,
332
+ start,
333
+ cancel,
334
+ end,
335
+ toggleMute,
336
+ dismissError,
337
+ readVoiceLevels: (bands) => held && state.status === "live" ? held.audio.readLevels(bands) : [],
338
+ browserTalkStartedInThisTab: (callId) => callIdsOfBrowserTalksStartedHere.has(callId),
339
+ };
340
+ }
341
+ /** What the member is told about a browser talk the server ended; their own End elsewhere needs no note. */
342
+ function mapCallEndReasonToBrowserTalkError(endReason) {
343
+ switch (endReason) {
344
+ case "phone_took_over":
345
+ return { kind: "phone_took_over" };
346
+ case "time_limit":
347
+ return { kind: "time_limit_reached" };
348
+ case "connection_lost":
349
+ return { kind: "connection_dropped" };
350
+ case "ended_by_member":
351
+ case "caller_hung_up":
352
+ case "caller_silent":
353
+ case null:
354
+ return null;
355
+ }
356
+ }
357
+ function mapMicrophoneFailure(failure) {
358
+ if (!(failure instanceof DOMException))
359
+ return { kind: "microphone_blocked" };
360
+ switch (failure.name) {
361
+ case "NotFoundError":
362
+ case "OverconstrainedError":
363
+ return { kind: "no_microphone" };
364
+ // Permission was granted but the device could not be opened, as when another app holds it.
365
+ case "NotReadableError":
366
+ return { kind: "microphone_unavailable" };
367
+ default:
368
+ return { kind: "microphone_blocked" };
369
+ }
370
+ }
371
+ /** Resolves once ICE gathering is complete; the offer is sent whole, with no trickle ICE. */
372
+ function waitForIceGathering(peer) {
373
+ if (peer.iceGatheringState === "complete")
374
+ return Promise.resolve();
375
+ return new Promise((resolve, reject) => {
376
+ const timeout = setTimeout(() => {
377
+ peer.removeEventListener("icegatheringstatechange", onGatheringStateChange);
378
+ reject(new Error("ICE gathering timed out"));
379
+ }, ICE_GATHERING_TIMEOUT_MS);
380
+ function onGatheringStateChange() {
381
+ if (peer.iceGatheringState !== "complete")
382
+ return;
383
+ clearTimeout(timeout);
384
+ peer.removeEventListener("icegatheringstatechange", onGatheringStateChange);
385
+ resolve();
386
+ }
387
+ peer.addEventListener("icegatheringstatechange", onGatheringStateChange);
388
+ });
389
+ }
390
+ /** The two lifecycle events a browser talk acts on; anything else (the transcript deltas included) is ignored. */
391
+ function parseDataChannelEvent(data) {
392
+ let event;
393
+ try {
394
+ event = JSON.parse(String(data));
395
+ }
396
+ catch {
397
+ console.warn("Ignored a browser talk data-channel message that was not JSON");
398
+ return null;
399
+ }
400
+ if (typeof event !== "object" || event === null)
401
+ return null;
402
+ const fields = event;
403
+ switch (fields.type) {
404
+ case "session.started":
405
+ return { type: "session.started" };
406
+ case "session.closed":
407
+ return {
408
+ type: "session.closed",
409
+ reason: typeof fields.reason === "string" ? fields.reason : null,
410
+ };
411
+ default:
412
+ return null;
413
+ }
414
+ }
@@ -0,0 +1,17 @@
1
+ import type { CallPresenceWatcher } from "./createCallPresenceWatcher.js";
2
+ import type { BrowserTalk } from "./createBrowserTalk.js";
3
+ export interface BrowserTalkErrorFollower {
4
+ stop: () => void;
5
+ }
6
+ /**
7
+ * Clears the tab's browser talk error once the call it is about has ended, whether or not any component
8
+ * shows it meanwhile. When such an error appears on an idle browser talk it watches presence (which keeps
9
+ * the presence poll running), starts a read at once, and dismisses the error once presence has
10
+ * shown that call and shows it no longer (`followBrowserTalkErrorAgainstPresence`). The read at once
11
+ * matters: the browser talk went idle before the error arrived, so nothing else rereads, and a short phone
12
+ * call could otherwise end before the next poll and never be seen. Only successful reads that
13
+ * started after the error count: the watcher's last state, a read already in flight, or a failed
14
+ * read (which keeps the last good call) may show an earlier call, and it must not count as the
15
+ * error's call. It stops watching presence, and so stops polling for it, when the error goes.
16
+ */
17
+ export declare function createBrowserTalkErrorFollower(browserTalk: BrowserTalk, presenceWatcher: CallPresenceWatcher): BrowserTalkErrorFollower;
@@ -0,0 +1,76 @@
1
+ import { deriveCallPresence } from "./deriveCallPresence.js";
2
+ import { followBrowserTalkErrorAgainstPresence, NO_FOLLOWED_BROWSER_TALK_ERROR, } from "./followBrowserTalkErrorAgainstPresence.js";
3
+ /** No call seen: until a presence read arrives, the error's call has not been seen. */
4
+ const PRESENCE_NOT_READ_SINCE_ERROR = {
5
+ presence: "none",
6
+ call: null,
7
+ };
8
+ /**
9
+ * Clears the tab's browser talk error once the call it is about has ended, whether or not any component
10
+ * shows it meanwhile. When such an error appears on an idle browser talk it watches presence (which keeps
11
+ * the presence poll running), starts a read at once, and dismisses the error once presence has
12
+ * shown that call and shows it no longer (`followBrowserTalkErrorAgainstPresence`). The read at once
13
+ * matters: the browser talk went idle before the error arrived, so nothing else rereads, and a short phone
14
+ * call could otherwise end before the next poll and never be seen. Only successful reads that
15
+ * started after the error count: the watcher's last state, a read already in flight, or a failed
16
+ * read (which keeps the last good call) may show an earlier call, and it must not count as the
17
+ * error's call. It stops watching presence, and so stops polling for it, when the error goes.
18
+ */
19
+ export function createBrowserTalkErrorFollower(browserTalk, presenceWatcher) {
20
+ let followed = NO_FOLLOWED_BROWSER_TALK_ERROR;
21
+ let stopWatchingPresence = null;
22
+ // Reads numbered up to this one started before the followed error appeared.
23
+ let readsStartedBeforeError = 0;
24
+ function readBrowserTalkError() {
25
+ const browserTalkState = browserTalk.getState();
26
+ return browserTalkState.status === "idle" ? browserTalkState.error : null;
27
+ }
28
+ /** A presence read finished while the follower watches: follow the error against its answer. */
29
+ function followPresenceRead() {
30
+ const server = presenceWatcher.getState();
31
+ if (server.readError !== null || server.answeredByReadNumber <= readsStartedBeforeError)
32
+ return;
33
+ follow(deriveCallPresence(server, browserTalk.getState(), browserTalk.browserTalkStartedInThisTab, Date.now()));
34
+ }
35
+ function follow(presence) {
36
+ const now = followBrowserTalkErrorAgainstPresence(followed, readBrowserTalkError(), presence);
37
+ followed = now.followed;
38
+ if (now.callEndedSinceError === "ended")
39
+ browserTalk.dismissError();
40
+ }
41
+ function followBrowserTalk() {
42
+ const error = readBrowserTalkError();
43
+ const errorIsAboutACall = error !== null &&
44
+ (error.kind === "phone_took_over" ||
45
+ error.kind === "on_a_phone_call" ||
46
+ error.kind === "already_on_a_browser_talk");
47
+ if (!errorIsAboutACall && stopWatchingPresence !== null) {
48
+ stopWatchingPresence();
49
+ stopWatchingPresence = null;
50
+ }
51
+ if (followed.error === error)
52
+ return;
53
+ // A new error (or none) starts unseen; the watcher's last state may predate it, so it does not count.
54
+ follow(PRESENCE_NOT_READ_SINCE_ERROR);
55
+ if (errorIsAboutACall)
56
+ watchPresenceFromNow();
57
+ }
58
+ /** Counts only reads that start from now, and makes sure one does. */
59
+ function watchPresenceFromNow() {
60
+ readsStartedBeforeError = presenceWatcher.countReadsStarted();
61
+ if (stopWatchingPresence === null)
62
+ stopWatchingPresence = presenceWatcher.subscribe(followPresenceRead);
63
+ // Subscribing reads at once when nothing else watched; otherwise ask for a read.
64
+ if (presenceWatcher.countReadsStarted() === readsStartedBeforeError)
65
+ presenceWatcher.refetch();
66
+ }
67
+ const stopFollowingBrowserTalk = browserTalk.subscribe(followBrowserTalk);
68
+ followBrowserTalk();
69
+ return {
70
+ stop: () => {
71
+ stopFollowingBrowserTalk();
72
+ stopWatchingPresence?.();
73
+ stopWatchingPresence = null;
74
+ },
75
+ };
76
+ }
@@ -0,0 +1,25 @@
1
+ import type { BrowserTalk } from "./createBrowserTalk.js";
2
+ import type { CurrentCall, CurrentCallDetails } from "./voiceApiModels.js";
3
+ /**
4
+ * Reads `GET calls/current` while any component watches presence: every 5 s with no call, every
5
+ * 2 s while one is live (to see its end and phone-wins promptly), paused while the tab is hidden,
6
+ * and at once on focus, on becoming visible and whenever this tab's browser talk changes status.
7
+ */
8
+ export interface ServerCallPresence {
9
+ call: CurrentCallDetails | null;
10
+ /** Server clock minus this browser's clock at the last read. */
11
+ serverOffsetMs: number;
12
+ /** Which read gave `call`, numbering reads from 1 in the order they started; 0 before any read succeeded. */
13
+ answeredByReadNumber: number;
14
+ readError: unknown;
15
+ }
16
+ export interface CallPresenceWatcher {
17
+ subscribe: (listener: () => void) => () => void;
18
+ getState: () => ServerCallPresence;
19
+ refetch: () => void;
20
+ /** How many reads have started; a later answer whose `answeredByReadNumber` exceeds it started after this moment. */
21
+ countReadsStarted: () => number;
22
+ }
23
+ export declare const PRESENCE_POLL_WITHOUT_CALL_MS = 5000;
24
+ export declare const PRESENCE_POLL_DURING_CALL_MS = 2000;
25
+ export declare function createCallPresenceWatcher(browserTalk: BrowserTalk, getCurrentCall: () => Promise<CurrentCall>): CallPresenceWatcher;
@@ -0,0 +1,66 @@
1
+ import { createVisibleTabPoller } from "./createVisibleTabPoller.js";
2
+ import { readTimestampMilliseconds } from "./readTimestampMilliseconds.js";
3
+ export const PRESENCE_POLL_WITHOUT_CALL_MS = 5000;
4
+ export const PRESENCE_POLL_DURING_CALL_MS = 2000;
5
+ export function createCallPresenceWatcher(browserTalk, getCurrentCall) {
6
+ let state = {
7
+ call: null,
8
+ serverOffsetMs: 0,
9
+ answeredByReadNumber: 0,
10
+ readError: null,
11
+ };
12
+ let readsStarted = 0;
13
+ const listeners = new Set();
14
+ let stopFollowingBrowserTalk = null;
15
+ let lastBrowserTalkStatus = browserTalk.getState().status;
16
+ async function readCurrentCall() {
17
+ readsStarted += 1;
18
+ const readNumber = readsStarted;
19
+ try {
20
+ const { call } = await getCurrentCall();
21
+ state = {
22
+ call,
23
+ serverOffsetMs: call
24
+ ? readTimestampMilliseconds(call.server_now) - Date.now()
25
+ : state.serverOffsetMs,
26
+ answeredByReadNumber: readNumber,
27
+ readError: null,
28
+ };
29
+ }
30
+ catch (failure) {
31
+ state = { ...state, readError: failure };
32
+ }
33
+ for (const listener of listeners)
34
+ listener();
35
+ }
36
+ const poller = createVisibleTabPoller(readCurrentCall, () => state.call !== null || browserTalk.getState().status !== "idle"
37
+ ? PRESENCE_POLL_DURING_CALL_MS
38
+ : PRESENCE_POLL_WITHOUT_CALL_MS);
39
+ function followBrowserTalkStatus() {
40
+ const status = browserTalk.getState().status;
41
+ if (status === lastBrowserTalkStatus)
42
+ return;
43
+ lastBrowserTalkStatus = status;
44
+ poller.readNow();
45
+ }
46
+ return {
47
+ subscribe: (listener) => {
48
+ listeners.add(listener);
49
+ if (listeners.size === 1) {
50
+ stopFollowingBrowserTalk = browserTalk.subscribe(followBrowserTalkStatus);
51
+ poller.start();
52
+ }
53
+ return () => {
54
+ listeners.delete(listener);
55
+ if (listeners.size > 0)
56
+ return;
57
+ poller.stop();
58
+ stopFollowingBrowserTalk?.();
59
+ stopFollowingBrowserTalk = null;
60
+ };
61
+ },
62
+ getState: () => state,
63
+ refetch: poller.readNow,
64
+ countReadsStarted: () => readsStarted,
65
+ };
66
+ }
@@ -0,0 +1,24 @@
1
+ import type { CallCaption, CallEndReason, CallId, CallCaptionsSoFar, CaptionedCallStatus } from "./voiceApiModels.js";
2
+ /**
3
+ * Follows one call's captions: `GET calls/{id}/captions` every second while the call is not over,
4
+ * paused while the tab is hidden, plus one last read after the call ends so words that arrive at
5
+ * the end still show. Each read carries every caption of the call, built by the backend's one
6
+ * caption rule, and replaces the previous one: the last caption grows in place.
7
+ */
8
+ export interface LiveCallCaptionsState {
9
+ captions: readonly CallCaption[];
10
+ callStatus: CaptionedCallStatus | null;
11
+ /** Why the call ended or is ending, once the server says. */
12
+ endReason: CallEndReason | null;
13
+ /** The last failed read; the captions above stay as the last good read left them. */
14
+ readError: unknown;
15
+ }
16
+ export interface LiveCallCaptionsPoller {
17
+ subscribe: (listener: () => void) => () => void;
18
+ getState: () => LiveCallCaptionsState;
19
+ start: () => void;
20
+ stop: () => void;
21
+ }
22
+ export declare const LIVE_CALL_CAPTIONS_POLL_MS = 1000;
23
+ export declare const NO_LIVE_CALL_CAPTIONS: LiveCallCaptionsState;
24
+ export declare function createLiveCallCaptionsPoller(callId: CallId, getCallCaptions: (callId: CallId) => Promise<CallCaptionsSoFar>): LiveCallCaptionsPoller;
@@ -0,0 +1,58 @@
1
+ import { createVisibleTabPoller } from "./createVisibleTabPoller.js";
2
+ import { VoiceApiError } from "./VoiceApiError.js";
3
+ export const LIVE_CALL_CAPTIONS_POLL_MS = 1000;
4
+ const HTTP_NOT_FOUND = 404;
5
+ export const NO_LIVE_CALL_CAPTIONS = {
6
+ captions: [],
7
+ callStatus: null,
8
+ endReason: null,
9
+ readError: null,
10
+ };
11
+ export function createLiveCallCaptionsPoller(callId, getCallCaptions) {
12
+ let state = NO_LIVE_CALL_CAPTIONS;
13
+ let finalReadDone = false;
14
+ let callNotFound = false;
15
+ const listeners = new Set();
16
+ async function readCaptions() {
17
+ const wasEnded = state.callStatus === "ended";
18
+ try {
19
+ const captionsSoFar = await getCallCaptions(callId);
20
+ state = {
21
+ captions: captionsSoFar.captions,
22
+ callStatus: captionsSoFar.call_status,
23
+ endReason: captionsSoFar.end_reason,
24
+ readError: null,
25
+ };
26
+ if (wasEnded)
27
+ finalReadDone = true;
28
+ }
29
+ catch (failure) {
30
+ // Only the package's own call_not_found proves the call is not this member's; a host or
31
+ // proxy 404 keeps the last good captions and the poll.
32
+ if (failure instanceof VoiceApiError &&
33
+ failure.status === HTTP_NOT_FOUND &&
34
+ failure.source === "voice_conversations" &&
35
+ failure.code === "call_not_found") {
36
+ // Not this member's call (or a member who signed in since): drop what was shown.
37
+ callNotFound = true;
38
+ state = { ...NO_LIVE_CALL_CAPTIONS, readError: failure };
39
+ }
40
+ else
41
+ state = { ...state, readError: failure };
42
+ }
43
+ for (const listener of listeners)
44
+ listener();
45
+ }
46
+ const poller = createVisibleTabPoller(readCaptions, () => finalReadDone || callNotFound ? null : LIVE_CALL_CAPTIONS_POLL_MS);
47
+ return {
48
+ subscribe: (listener) => {
49
+ listeners.add(listener);
50
+ return () => {
51
+ listeners.delete(listener);
52
+ };
53
+ },
54
+ getState: () => state,
55
+ start: poller.start,
56
+ stop: poller.stop,
57
+ };
58
+ }