@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.
- package/README.md +30 -2
- package/components/BrowserTalkButton.tsx +96 -0
- package/components/CallBanner.tsx +43 -0
- package/components/CallCaptions.tsx +38 -0
- package/components/CallEntry.tsx +89 -0
- package/components/CallTranscript.tsx +58 -0
- package/components/RecentCallEntries.tsx +211 -0
- package/components/describeBrowserTalkError.ts +39 -0
- package/components/formatCallClock.ts +9 -0
- package/components/useEndPresentCall.ts +45 -0
- package/components/voiceCall.css +225 -0
- package/dist/MalformedVoiceResponseError.d.ts +6 -0
- package/dist/MalformedVoiceResponseError.js +9 -0
- package/dist/NumberToCallQrCode.d.ts +12 -0
- package/dist/NumberToCallQrCode.js +29 -0
- package/dist/PhoneNumberDialog.d.ts +25 -0
- package/dist/PhoneNumberDialog.js +333 -0
- package/dist/PhoneNumbersAdminPanel.d.ts +11 -0
- package/dist/PhoneNumbersAdminPanel.js +85 -0
- package/dist/VoiceApiError.d.ts +18 -0
- package/dist/VoiceApiError.js +19 -0
- package/dist/VoiceRequestTimedOutError.d.ts +6 -0
- package/dist/VoiceRequestTimedOutError.js +9 -0
- package/dist/averageSpeechBinsIntoBands.d.ts +6 -0
- package/dist/averageSpeechBinsIntoBands.js +20 -0
- package/dist/browserApisForBrowserTalk.d.ts +17 -0
- package/dist/browserApisForBrowserTalk.js +42 -0
- package/dist/browserTalkError.d.ts +36 -0
- package/dist/browserTalkError.js +1 -0
- package/dist/chooseCallToShowCaptionsOf.d.ts +22 -0
- package/dist/chooseCallToShowCaptionsOf.js +26 -0
- package/dist/createBrowserTalk.d.ts +56 -0
- package/dist/createBrowserTalk.js +414 -0
- package/dist/createBrowserTalkErrorFollower.d.ts +17 -0
- package/dist/createBrowserTalkErrorFollower.js +76 -0
- package/dist/createCallPresenceWatcher.d.ts +25 -0
- package/dist/createCallPresenceWatcher.js +66 -0
- package/dist/createLiveCallCaptionsPoller.d.ts +24 -0
- package/dist/createLiveCallCaptionsPoller.js +58 -0
- package/dist/createVisibleTabPoller.d.ts +13 -0
- package/dist/createVisibleTabPoller.js +56 -0
- package/dist/deriveCallPresence.d.ts +23 -0
- package/dist/deriveCallPresence.js +58 -0
- package/dist/describePhoneNumberError.d.ts +7 -0
- package/dist/describePhoneNumberError.js +29 -0
- package/dist/entries/index.d.ts +26 -0
- package/dist/entries/index.js +22 -0
- package/dist/followBrowserTalkErrorAgainstPresence.d.ts +23 -0
- package/dist/followBrowserTalkErrorAgainstPresence.js +52 -0
- package/dist/formatPhoneNumberAsTyped.d.ts +31 -0
- package/dist/formatPhoneNumberAsTyped.js +85 -0
- package/dist/generated/index.d.ts +1 -0
- package/dist/generated/index.js +2 -0
- package/dist/generated/types.gen.d.ts +795 -0
- package/dist/generated/types.gen.js +2 -0
- package/dist/getBrowserTalkForThisTab.d.ts +3 -0
- package/dist/getBrowserTalkForThisTab.js +13 -0
- package/dist/getCallPresenceWatcherForThisTab.d.ts +3 -0
- package/dist/getCallPresenceWatcherForThisTab.js +9 -0
- package/dist/mapStartBrowserTalkFailureToBrowserTalkError.d.ts +7 -0
- package/dist/mapStartBrowserTalkFailureToBrowserTalkError.js +60 -0
- package/dist/parseVoiceApiResponses.d.ts +11 -0
- package/dist/parseVoiceApiResponses.js +344 -0
- package/dist/readCallSummaryUntilItHasDuration.d.ts +12 -0
- package/dist/readCallSummaryUntilItHasDuration.js +45 -0
- package/dist/readTheBrowsersTimeZone.d.ts +8 -0
- package/dist/readTheBrowsersTimeZone.js +17 -0
- package/dist/readTimestampMilliseconds.d.ts +8 -0
- package/dist/readTimestampMilliseconds.js +10 -0
- package/dist/requestEndOfCall.d.ts +7 -0
- package/dist/requestEndOfCall.js +11 -0
- package/dist/startBrowserTalkErrorFollowerForThisTab.d.ts +6 -0
- package/dist/startBrowserTalkErrorFollowerForThisTab.js +12 -0
- package/dist/useAdminPhoneNumbers.d.ts +47 -0
- package/dist/useAdminPhoneNumbers.js +76 -0
- package/dist/useBrowserTalk.d.ts +10 -0
- package/dist/useBrowserTalk.js +23 -0
- package/dist/useBrowserTalkErrorWhileItApplies.d.ts +10 -0
- package/dist/useBrowserTalkErrorWhileItApplies.js +13 -0
- package/dist/useCallPresence.d.ts +5 -0
- package/dist/useCallPresence.js +34 -0
- package/dist/useCallSummaries.d.ts +9 -0
- package/dist/useCallSummaries.js +22 -0
- package/dist/useCallSummary.d.ts +15 -0
- package/dist/useCallSummary.js +21 -0
- package/dist/useFinePointerWithHover.d.ts +10 -0
- package/dist/useFinePointerWithHover.js +19 -0
- package/dist/useLiveCallCaptions.d.ts +8 -0
- package/dist/useLiveCallCaptions.js +27 -0
- package/dist/useLiveCallCaptionsOfLatestCall.d.ts +13 -0
- package/dist/useLiveCallCaptionsOfLatestCall.js +23 -0
- package/dist/usePhoneNumber.d.ts +45 -0
- package/dist/usePhoneNumber.js +125 -0
- package/dist/voiceApiModels.d.ts +85 -0
- package/dist/voiceApiModels.js +1 -0
- package/dist/voiceConversationsApi.d.ts +44 -0
- package/dist/voiceConversationsApi.js +227 -0
- package/dist/voiceConversationsSettings.d.ts +20 -0
- package/dist/voiceConversationsSettings.js +26 -0
- package/package.json +41 -4
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createBrowserTalk } from "./createBrowserTalk.js";
|
|
2
|
+
import { realBrowserApisForBrowserTalk } from "./browserApisForBrowserTalk.js";
|
|
3
|
+
import { getCallCaptions, postEndCall, postStartBrowserTalk } from "./voiceConversationsApi.js";
|
|
4
|
+
let browserTalkForThisTab = null;
|
|
5
|
+
/** The tab's one browser talk, created on first use so it outlives route changes and closed drawers. */
|
|
6
|
+
export function getBrowserTalkForThisTab() {
|
|
7
|
+
browserTalkForThisTab ?? (browserTalkForThisTab = createBrowserTalk(realBrowserApisForBrowserTalk, {
|
|
8
|
+
postStartBrowserTalk,
|
|
9
|
+
postEndCall,
|
|
10
|
+
getCallCaptions,
|
|
11
|
+
}));
|
|
12
|
+
return browserTalkForThisTab;
|
|
13
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { createCallPresenceWatcher } from "./createCallPresenceWatcher.js";
|
|
2
|
+
import { getBrowserTalkForThisTab } from "./getBrowserTalkForThisTab.js";
|
|
3
|
+
import { getCurrentCall } from "./voiceConversationsApi.js";
|
|
4
|
+
let callPresenceWatcherForThisTab = null;
|
|
5
|
+
/** One presence poll per tab, however many components show presence. */
|
|
6
|
+
export function getCallPresenceWatcherForThisTab() {
|
|
7
|
+
callPresenceWatcherForThisTab ?? (callPresenceWatcherForThisTab = createCallPresenceWatcher(getBrowserTalkForThisTab(), getCurrentCall));
|
|
8
|
+
return callPresenceWatcherForThisTab;
|
|
9
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { BrowserTalkError } from "./browserTalkError.js";
|
|
2
|
+
/**
|
|
3
|
+
* The member-facing cause of a failed `POST /browser-talks`. A `fetch` that never reached the server
|
|
4
|
+
* (offline, CORS, DNS) is `did_not_connect`; a malformed 201 body is `voice_unavailable`, the same
|
|
5
|
+
* as a failed call, and so is a start that did not answer in time. Unexpected errors are rethrown rather than shown as a known cause.
|
|
6
|
+
*/
|
|
7
|
+
export declare function mapStartBrowserTalkFailureToBrowserTalkError(failure: unknown): BrowserTalkError;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { MalformedVoiceResponseError } from "./MalformedVoiceResponseError.js";
|
|
2
|
+
import { VoiceApiError } from "./VoiceApiError.js";
|
|
3
|
+
const HTTP_SERVICE_UNAVAILABLE = 503;
|
|
4
|
+
/**
|
|
5
|
+
* The package's codes a `POST /browser-talks` refusal is mapped from, each one checked by `tsc` against the
|
|
6
|
+
* generated contract, so a code the backend renames or drops fails to compile here.
|
|
7
|
+
*/
|
|
8
|
+
const BROWSER_TALK_START_REFUSAL_CODES = [
|
|
9
|
+
"on_a_phone_call",
|
|
10
|
+
"already_on_a_browser_talk",
|
|
11
|
+
"not_a_current_member",
|
|
12
|
+
"voice_not_ready",
|
|
13
|
+
"voice_unavailable",
|
|
14
|
+
"auth_service_unavailable",
|
|
15
|
+
];
|
|
16
|
+
/**
|
|
17
|
+
* The member-facing cause of a failed `POST /browser-talks`. A `fetch` that never reached the server
|
|
18
|
+
* (offline, CORS, DNS) is `did_not_connect`; a malformed 201 body is `voice_unavailable`, the same
|
|
19
|
+
* as a failed call, and so is a start that did not answer in time. Unexpected errors are rethrown rather than shown as a known cause.
|
|
20
|
+
*/
|
|
21
|
+
export function mapStartBrowserTalkFailureToBrowserTalkError(failure) {
|
|
22
|
+
if (failure instanceof MalformedVoiceResponseError)
|
|
23
|
+
return { kind: "voice_unavailable" };
|
|
24
|
+
if (failure instanceof TypeError)
|
|
25
|
+
return { kind: "did_not_connect" };
|
|
26
|
+
if (!(failure instanceof VoiceApiError))
|
|
27
|
+
throw failure;
|
|
28
|
+
// Ciridae Auth refusing the app's key (passed through as itself) is the app's setup, not the
|
|
29
|
+
// member's access or the voice service: shown like the voice being unavailable.
|
|
30
|
+
if (failure.code === "auth_service_refused")
|
|
31
|
+
return { kind: "voice_unavailable" };
|
|
32
|
+
if (failure.source === "provider")
|
|
33
|
+
return { kind: "provider_rejected", status: failure.status };
|
|
34
|
+
if (failure.source === "host") {
|
|
35
|
+
if (failure.status === HTTP_SERVICE_UNAVAILABLE)
|
|
36
|
+
return { kind: "voice_unavailable" };
|
|
37
|
+
return { kind: "host_refused", status: failure.status };
|
|
38
|
+
}
|
|
39
|
+
const code = failure.code;
|
|
40
|
+
if (!isBrowserTalkStartRefusalCode(code)) {
|
|
41
|
+
console.warn(`Voice conversations POST /browser-talks refused with unknown code ${code ?? "(none)"}; showing voice_unavailable`);
|
|
42
|
+
return { kind: "voice_unavailable" };
|
|
43
|
+
}
|
|
44
|
+
switch (code) {
|
|
45
|
+
case "on_a_phone_call":
|
|
46
|
+
return { kind: "on_a_phone_call" };
|
|
47
|
+
case "already_on_a_browser_talk":
|
|
48
|
+
return { kind: "already_on_a_browser_talk", callId: failure.callId };
|
|
49
|
+
case "not_a_current_member":
|
|
50
|
+
return { kind: "not_a_current_member" };
|
|
51
|
+
case "voice_not_ready":
|
|
52
|
+
return { kind: "voice_not_ready" };
|
|
53
|
+
case "voice_unavailable":
|
|
54
|
+
case "auth_service_unavailable":
|
|
55
|
+
return { kind: "voice_unavailable" };
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function isBrowserTalkStartRefusalCode(code) {
|
|
59
|
+
return BROWSER_TALK_START_REFUSAL_CODES.some((known) => known === code);
|
|
60
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AdminAddedPhoneNumber, AdminPhoneNumbersPage, CallCaptionsSoFar, CallSummary, CurrentCall, MemberPhoneNumber, PhoneNumberUploadReport, BrowserTalkStarted, VoiceErrorBody } from "./voiceApiModels.js";
|
|
2
|
+
export declare function parseBrowserTalkStarted(body: unknown): BrowserTalkStarted;
|
|
3
|
+
export declare function parseCurrentCall(body: unknown): CurrentCall;
|
|
4
|
+
export declare function parseCallCaptions(body: unknown): CallCaptionsSoFar;
|
|
5
|
+
export declare function parseCallSummary(body: unknown): CallSummary;
|
|
6
|
+
export declare function parseMemberPhoneNumber(route: string, body: unknown): MemberPhoneNumber;
|
|
7
|
+
export declare function parseAdminAddedPhoneNumber(body: unknown): AdminAddedPhoneNumber;
|
|
8
|
+
export declare function parseAdminPhoneNumbersPage(body: unknown): AdminPhoneNumbersPage;
|
|
9
|
+
export declare function parsePhoneNumberUploadReport(body: unknown): PhoneNumberUploadReport;
|
|
10
|
+
/** The package's error shape, or null when the body is someone else's (the host's 401, a proxy page). */
|
|
11
|
+
export declare function parseVoiceErrorBody(body: unknown): VoiceErrorBody | null;
|
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
import { MalformedVoiceResponseError } from "./MalformedVoiceResponseError.js";
|
|
2
|
+
import { readTimestampMilliseconds } from "./readTimestampMilliseconds.js";
|
|
3
|
+
class BodyReader {
|
|
4
|
+
constructor(route, value, path) {
|
|
5
|
+
this.route = route;
|
|
6
|
+
this.value = value;
|
|
7
|
+
this.path = path;
|
|
8
|
+
}
|
|
9
|
+
object() {
|
|
10
|
+
if (typeof this.value !== "object" || this.value === null || Array.isArray(this.value))
|
|
11
|
+
throw new MalformedVoiceResponseError(this.route, this.path);
|
|
12
|
+
return new ObjectReader(this.route, this.value, this.path);
|
|
13
|
+
}
|
|
14
|
+
array() {
|
|
15
|
+
if (!Array.isArray(this.value))
|
|
16
|
+
throw new MalformedVoiceResponseError(this.route, this.path);
|
|
17
|
+
return this.value.map((item, index) => new BodyReader(this.route, item, `${this.path}[${index}]`));
|
|
18
|
+
}
|
|
19
|
+
isNull() {
|
|
20
|
+
return this.value === null;
|
|
21
|
+
}
|
|
22
|
+
string() {
|
|
23
|
+
if (typeof this.value !== "string")
|
|
24
|
+
throw new MalformedVoiceResponseError(this.route, this.path);
|
|
25
|
+
return this.value;
|
|
26
|
+
}
|
|
27
|
+
nonNegativeInteger() {
|
|
28
|
+
if (typeof this.value !== "number" || !Number.isInteger(this.value) || this.value < 0)
|
|
29
|
+
throw new MalformedVoiceResponseError(this.route, this.path);
|
|
30
|
+
return this.value;
|
|
31
|
+
}
|
|
32
|
+
timestamp() {
|
|
33
|
+
const text = this.string();
|
|
34
|
+
if (Number.isNaN(readTimestampMilliseconds(text)))
|
|
35
|
+
throw new MalformedVoiceResponseError(this.route, this.path);
|
|
36
|
+
return text;
|
|
37
|
+
}
|
|
38
|
+
oneOf(allowed) {
|
|
39
|
+
const text = this.string();
|
|
40
|
+
const match = allowed.find((candidate) => candidate === text);
|
|
41
|
+
if (match === undefined)
|
|
42
|
+
throw new MalformedVoiceResponseError(this.route, this.path);
|
|
43
|
+
return match;
|
|
44
|
+
}
|
|
45
|
+
callId() {
|
|
46
|
+
const text = this.string();
|
|
47
|
+
if (text.length === 0)
|
|
48
|
+
throw new MalformedVoiceResponseError(this.route, this.path);
|
|
49
|
+
return text;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
class ObjectReader {
|
|
53
|
+
constructor(route, value, path) {
|
|
54
|
+
this.route = route;
|
|
55
|
+
this.value = value;
|
|
56
|
+
this.path = path;
|
|
57
|
+
}
|
|
58
|
+
field(name) {
|
|
59
|
+
return new BodyReader(this.route, this.value[name], `${this.path}.${name}`);
|
|
60
|
+
}
|
|
61
|
+
has(name) {
|
|
62
|
+
return this.value[name] !== undefined;
|
|
63
|
+
}
|
|
64
|
+
/** Throws for the first field not in `names`; returns the reader for chaining. */
|
|
65
|
+
onlyFields(names) {
|
|
66
|
+
const unknown = Object.keys(this.value).find((name) => !names.includes(name));
|
|
67
|
+
if (unknown !== undefined)
|
|
68
|
+
throw new MalformedVoiceResponseError(this.route, `${this.path}.${unknown}`);
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A parser's closed list of one generated union's values, checked both ways by `tsc`: a value the
|
|
74
|
+
* backend added that the list lacks fails to compile, and so does a list value the backend dropped.
|
|
75
|
+
* At runtime the list is returned as it is.
|
|
76
|
+
*/
|
|
77
|
+
function listEveryValueOf() {
|
|
78
|
+
return (values) => values;
|
|
79
|
+
}
|
|
80
|
+
const CALL_STARTED_THROUGH_VALUES = listEveryValueOf()([
|
|
81
|
+
"phone_call",
|
|
82
|
+
"browser_talk",
|
|
83
|
+
]);
|
|
84
|
+
const CALL_STATUSES = listEveryValueOf()(["connecting", "live", "ending"]);
|
|
85
|
+
const CAPTIONED_CALL_STATUSES = listEveryValueOf()([...CALL_STATUSES, "ended"]);
|
|
86
|
+
const SPEAKERS = listEveryValueOf()(["caller", "voice"]);
|
|
87
|
+
const CALL_END_REASONS = listEveryValueOf()([
|
|
88
|
+
"ended_by_member",
|
|
89
|
+
"phone_took_over",
|
|
90
|
+
"time_limit",
|
|
91
|
+
"caller_hung_up",
|
|
92
|
+
"connection_lost",
|
|
93
|
+
"caller_silent",
|
|
94
|
+
]);
|
|
95
|
+
export function parseBrowserTalkStarted(body) {
|
|
96
|
+
const startedBody = new BodyReader("POST /browser-talks", body, "body")
|
|
97
|
+
.object()
|
|
98
|
+
.onlyFields(["call_id", "sdp", "started_at", "ends_at", "server_now"]);
|
|
99
|
+
return {
|
|
100
|
+
call_id: startedBody.field("call_id").callId(),
|
|
101
|
+
sdp: startedBody.field("sdp").string(),
|
|
102
|
+
started_at: startedBody.field("started_at").timestamp(),
|
|
103
|
+
ends_at: startedBody.field("ends_at").timestamp(),
|
|
104
|
+
server_now: startedBody.field("server_now").timestamp(),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
export function parseCurrentCall(body) {
|
|
108
|
+
const current = new BodyReader("GET /calls/current", body, "body").object().onlyFields(["call"]);
|
|
109
|
+
const callField = current.field("call");
|
|
110
|
+
if (callField.isNull())
|
|
111
|
+
return { call: null };
|
|
112
|
+
const call = callField
|
|
113
|
+
.object()
|
|
114
|
+
.onlyFields(["call_id", "started_through", "status", "started_at", "ends_at", "server_now"]);
|
|
115
|
+
const details = {
|
|
116
|
+
call_id: call.field("call_id").callId(),
|
|
117
|
+
started_through: call.field("started_through").oneOf(CALL_STARTED_THROUGH_VALUES),
|
|
118
|
+
status: call.field("status").oneOf(CALL_STATUSES),
|
|
119
|
+
started_at: call.field("started_at").timestamp(),
|
|
120
|
+
ends_at: call.field("ends_at").timestamp(),
|
|
121
|
+
server_now: call.field("server_now").timestamp(),
|
|
122
|
+
};
|
|
123
|
+
return { call: details };
|
|
124
|
+
}
|
|
125
|
+
export function parseCallCaptions(body) {
|
|
126
|
+
const captionsBody = new BodyReader("GET /calls/{call_id}/captions", body, "body")
|
|
127
|
+
.object()
|
|
128
|
+
.onlyFields(["call_status", "end_reason", "captions"]);
|
|
129
|
+
const endReason = captionsBody.field("end_reason");
|
|
130
|
+
return {
|
|
131
|
+
call_status: captionsBody.field("call_status").oneOf(CAPTIONED_CALL_STATUSES),
|
|
132
|
+
end_reason: endReason.isNull() ? null : endReason.oneOf(CALL_END_REASONS),
|
|
133
|
+
captions: captionsBody
|
|
134
|
+
.field("captions")
|
|
135
|
+
.array()
|
|
136
|
+
.map((caption) => parseCallCaption(caption.object())),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
export function parseCallSummary(body) {
|
|
140
|
+
const summary = new BodyReader("GET /calls/{call_id}", body, "body")
|
|
141
|
+
.object()
|
|
142
|
+
.onlyFields([
|
|
143
|
+
"call_id",
|
|
144
|
+
"started_through",
|
|
145
|
+
"member_display_name",
|
|
146
|
+
"caller_last_four_digits",
|
|
147
|
+
"started_at",
|
|
148
|
+
"duration_seconds",
|
|
149
|
+
"delegation_count",
|
|
150
|
+
"unanswered_delegation_count",
|
|
151
|
+
]);
|
|
152
|
+
const memberDisplayName = summary.field("member_display_name");
|
|
153
|
+
const callerLastFourDigits = summary.field("caller_last_four_digits");
|
|
154
|
+
const startedAt = summary.field("started_at");
|
|
155
|
+
const durationSeconds = summary.field("duration_seconds");
|
|
156
|
+
return {
|
|
157
|
+
call_id: summary.field("call_id").callId(),
|
|
158
|
+
started_through: summary.field("started_through").oneOf(CALL_STARTED_THROUGH_VALUES),
|
|
159
|
+
member_display_name: memberDisplayName.isNull() ? null : memberDisplayName.string(),
|
|
160
|
+
caller_last_four_digits: callerLastFourDigits.isNull() ? null : callerLastFourDigits.string(),
|
|
161
|
+
started_at: startedAt.isNull() ? null : startedAt.timestamp(),
|
|
162
|
+
duration_seconds: durationSeconds.isNull() ? null : durationSeconds.nonNegativeInteger(),
|
|
163
|
+
delegation_count: summary.field("delegation_count").nonNegativeInteger(),
|
|
164
|
+
unanswered_delegation_count: summary.field("unanswered_delegation_count").nonNegativeInteger(),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
export function parseMemberPhoneNumber(route, body) {
|
|
168
|
+
const saved = new BodyReader(route, body, "body")
|
|
169
|
+
.object()
|
|
170
|
+
.onlyFields([
|
|
171
|
+
"phone_number",
|
|
172
|
+
"phone_number_display",
|
|
173
|
+
"phone_number_to_call",
|
|
174
|
+
"phone_number_to_call_display",
|
|
175
|
+
]);
|
|
176
|
+
const readStringOrNull = (name) => {
|
|
177
|
+
const field = saved.field(name);
|
|
178
|
+
return field.isNull() ? null : field.string();
|
|
179
|
+
};
|
|
180
|
+
return {
|
|
181
|
+
phone_number: readStringOrNull("phone_number"),
|
|
182
|
+
phone_number_display: readStringOrNull("phone_number_display"),
|
|
183
|
+
phone_number_to_call: readStringOrNull("phone_number_to_call"),
|
|
184
|
+
phone_number_to_call_display: readStringOrNull("phone_number_to_call_display"),
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
const COUNTED_INVITE_STATUSES = listEveryValueOf()(["accepted", "pending"]);
|
|
188
|
+
const UPLOADED_ROW_REFUSAL_REASONS = listEveryValueOf()([
|
|
189
|
+
"missing_value",
|
|
190
|
+
"member_not_found",
|
|
191
|
+
"invite_expired",
|
|
192
|
+
"phone_number_not_e164",
|
|
193
|
+
"phone_number_held_by_another_member",
|
|
194
|
+
]);
|
|
195
|
+
const PHONE_NUMBER_SAVERS = listEveryValueOf()([
|
|
196
|
+
"admin",
|
|
197
|
+
"member",
|
|
198
|
+
]);
|
|
199
|
+
const UPLOADED_ROW_OUTCOMES = listEveryValueOf()([
|
|
200
|
+
"added",
|
|
201
|
+
"refused",
|
|
202
|
+
]);
|
|
203
|
+
const ERROR_DETAIL_FIELDS = listEveryValueOf()([
|
|
204
|
+
"code",
|
|
205
|
+
"source",
|
|
206
|
+
"message",
|
|
207
|
+
"call_id",
|
|
208
|
+
]);
|
|
209
|
+
const ERROR_SOURCES = listEveryValueOf()([
|
|
210
|
+
"voice_conversations",
|
|
211
|
+
"provider",
|
|
212
|
+
]);
|
|
213
|
+
export function parseAdminAddedPhoneNumber(body) {
|
|
214
|
+
const added = new BodyReader("POST /admin/phone-numbers", body, "body")
|
|
215
|
+
.object()
|
|
216
|
+
.onlyFields([
|
|
217
|
+
"member_user_id",
|
|
218
|
+
"member_email",
|
|
219
|
+
"phone_number",
|
|
220
|
+
"phone_number_display",
|
|
221
|
+
"invite_status",
|
|
222
|
+
]);
|
|
223
|
+
return {
|
|
224
|
+
member_user_id: added.field("member_user_id").string(),
|
|
225
|
+
member_email: added.field("member_email").string(),
|
|
226
|
+
phone_number: added.field("phone_number").string(),
|
|
227
|
+
phone_number_display: added.field("phone_number_display").string(),
|
|
228
|
+
invite_status: added.field("invite_status").oneOf(COUNTED_INVITE_STATUSES),
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
export function parseAdminPhoneNumbersPage(body) {
|
|
232
|
+
const page = new BodyReader("GET /admin/phone-numbers", body, "body")
|
|
233
|
+
.object()
|
|
234
|
+
.onlyFields(["phone_numbers", "next_after"]);
|
|
235
|
+
const nextAfter = page.field("next_after");
|
|
236
|
+
return {
|
|
237
|
+
phone_numbers: page
|
|
238
|
+
.field("phone_numbers")
|
|
239
|
+
.array()
|
|
240
|
+
.map((listed) => parseAdminListedPhoneNumber(listed.object())),
|
|
241
|
+
next_after: nextAfter.isNull() ? null : nextAfter.string(),
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
function parseAdminListedPhoneNumber(listed) {
|
|
245
|
+
listed.onlyFields([
|
|
246
|
+
"member_user_id",
|
|
247
|
+
"member_email",
|
|
248
|
+
"phone_number",
|
|
249
|
+
"phone_number_display",
|
|
250
|
+
"added_by",
|
|
251
|
+
]);
|
|
252
|
+
return {
|
|
253
|
+
member_user_id: listed.field("member_user_id").string(),
|
|
254
|
+
member_email: listed.field("member_email").string(),
|
|
255
|
+
phone_number: listed.field("phone_number").string(),
|
|
256
|
+
phone_number_display: listed.field("phone_number_display").string(),
|
|
257
|
+
added_by: listed.field("added_by").oneOf(PHONE_NUMBER_SAVERS),
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
export function parsePhoneNumberUploadReport(body) {
|
|
261
|
+
const route = "POST /admin/phone-numbers/csv";
|
|
262
|
+
const report = new BodyReader(route, body, "body")
|
|
263
|
+
.object()
|
|
264
|
+
.onlyFields(["added_count", "refused_count", "rows"]);
|
|
265
|
+
return {
|
|
266
|
+
added_count: report.field("added_count").nonNegativeInteger(),
|
|
267
|
+
refused_count: report.field("refused_count").nonNegativeInteger(),
|
|
268
|
+
rows: report
|
|
269
|
+
.field("rows")
|
|
270
|
+
.array()
|
|
271
|
+
.map((row) => parseUploadedPhoneNumberRow(row.object())),
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
function parseUploadedPhoneNumberRow(row) {
|
|
275
|
+
const outcome = row.field("outcome").oneOf(UPLOADED_ROW_OUTCOMES);
|
|
276
|
+
if (outcome === "added") {
|
|
277
|
+
row.onlyFields([
|
|
278
|
+
"outcome",
|
|
279
|
+
"row_number",
|
|
280
|
+
"member",
|
|
281
|
+
"member_user_id",
|
|
282
|
+
"phone_number",
|
|
283
|
+
"phone_number_display",
|
|
284
|
+
"invite_status",
|
|
285
|
+
]);
|
|
286
|
+
return {
|
|
287
|
+
outcome,
|
|
288
|
+
row_number: row.field("row_number").nonNegativeInteger(),
|
|
289
|
+
member: row.field("member").string(),
|
|
290
|
+
member_user_id: row.field("member_user_id").string(),
|
|
291
|
+
phone_number: row.field("phone_number").string(),
|
|
292
|
+
phone_number_display: row.field("phone_number_display").string(),
|
|
293
|
+
invite_status: row.field("invite_status").oneOf(COUNTED_INVITE_STATUSES),
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
row.onlyFields(["outcome", "row_number", "member", "reason"]);
|
|
297
|
+
return {
|
|
298
|
+
outcome,
|
|
299
|
+
row_number: row.field("row_number").nonNegativeInteger(),
|
|
300
|
+
member: row.field("member").string(),
|
|
301
|
+
reason: row.field("reason").oneOf(UPLOADED_ROW_REFUSAL_REASONS),
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
/** The package's error shape, or null when the body is someone else's (the host's 401, a proxy page). */
|
|
305
|
+
export function parseVoiceErrorBody(body) {
|
|
306
|
+
try {
|
|
307
|
+
const error = new BodyReader("error", body, "body")
|
|
308
|
+
.object()
|
|
309
|
+
.onlyFields(["error"])
|
|
310
|
+
.field("error")
|
|
311
|
+
.object()
|
|
312
|
+
.onlyFields(ERROR_DETAIL_FIELDS);
|
|
313
|
+
return {
|
|
314
|
+
error: {
|
|
315
|
+
code: error.field("code").string(),
|
|
316
|
+
source: error.field("source").oneOf(ERROR_SOURCES),
|
|
317
|
+
message: error.field("message").string(),
|
|
318
|
+
...(error.has("call_id") ? { call_id: error.field("call_id").callId() } : {}),
|
|
319
|
+
},
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
catch (error) {
|
|
323
|
+
if (error instanceof MalformedVoiceResponseError)
|
|
324
|
+
return null;
|
|
325
|
+
throw error;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const CALL_CAPTION_FIELDS = [
|
|
329
|
+
"speaker",
|
|
330
|
+
"text",
|
|
331
|
+
"started_ms",
|
|
332
|
+
"ended_ms",
|
|
333
|
+
"first_fragment_id",
|
|
334
|
+
];
|
|
335
|
+
function parseCallCaption(caption) {
|
|
336
|
+
caption.onlyFields(CALL_CAPTION_FIELDS);
|
|
337
|
+
return {
|
|
338
|
+
speaker: caption.field("speaker").oneOf(SPEAKERS),
|
|
339
|
+
text: caption.field("text").string(),
|
|
340
|
+
started_ms: caption.field("started_ms").nonNegativeInteger(),
|
|
341
|
+
ended_ms: caption.field("ended_ms").nonNegativeInteger(),
|
|
342
|
+
first_fragment_id: caption.field("first_fragment_id").nonNegativeInteger(),
|
|
343
|
+
};
|
|
344
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { CallId, CallSummary } from "./voiceApiModels.js";
|
|
2
|
+
export interface CallSummaryRead {
|
|
3
|
+
summary: CallSummary | null;
|
|
4
|
+
readError: unknown;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Reads the summary of `callId` (`GET calls/{id}`) and hands each answer to `onRead`. A call that
|
|
8
|
+
* ended a moment ago may not have its end recorded yet, so a summary without a duration is read
|
|
9
|
+
* again every 2 s, three reads in all; a failed re-read keeps the summary already handed over.
|
|
10
|
+
* Returns a function that stops reading and handing over answers.
|
|
11
|
+
*/
|
|
12
|
+
export declare function readCallSummaryUntilItHasDuration(callId: CallId, onRead: (read: CallSummaryRead) => void): () => void;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { getCallSummary } from "./voiceConversationsApi.js";
|
|
2
|
+
/** How long after a summary without a duration it is read again. */
|
|
3
|
+
const SUMMARY_REREAD_WITHOUT_DURATION_MS = 2000;
|
|
4
|
+
/** Reads of one call's summary at most, while it comes back without a duration. */
|
|
5
|
+
const MAXIMUM_SUMMARY_READS = 3;
|
|
6
|
+
/**
|
|
7
|
+
* Reads the summary of `callId` (`GET calls/{id}`) and hands each answer to `onRead`. A call that
|
|
8
|
+
* ended a moment ago may not have its end recorded yet, so a summary without a duration is read
|
|
9
|
+
* again every 2 s, three reads in all; a failed re-read keeps the summary already handed over.
|
|
10
|
+
* Returns a function that stops reading and handing over answers.
|
|
11
|
+
*/
|
|
12
|
+
export function readCallSummaryUntilItHasDuration(callId, onRead) {
|
|
13
|
+
let current = true;
|
|
14
|
+
let rereadTimer;
|
|
15
|
+
function readSummary(readNumber) {
|
|
16
|
+
getCallSummary(callId).then((summary) => {
|
|
17
|
+
if (!current)
|
|
18
|
+
return;
|
|
19
|
+
onRead({ summary, readError: null });
|
|
20
|
+
if (summary.duration_seconds !== null)
|
|
21
|
+
return;
|
|
22
|
+
if (readNumber >= MAXIMUM_SUMMARY_READS) {
|
|
23
|
+
console.warn(`The summary of call_id=${callId} still had no duration after ${readNumber} reads; showing it without one`);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
rereadTimer = setTimeout(() => {
|
|
27
|
+
readSummary(readNumber + 1);
|
|
28
|
+
}, SUMMARY_REREAD_WITHOUT_DURATION_MS);
|
|
29
|
+
}, (failure) => {
|
|
30
|
+
if (readNumber > 1) {
|
|
31
|
+
// A re-read for the duration failed: the summary already read stays, without one.
|
|
32
|
+
console.warn(`Re-reading the summary of call_id=${callId} for its duration failed; showing it without one`, failure);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
console.warn(`Reading the summary of call_id=${callId} failed`, failure);
|
|
36
|
+
if (current)
|
|
37
|
+
onRead({ summary: null, readError: failure });
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
readSummary(1);
|
|
41
|
+
return () => {
|
|
42
|
+
current = false;
|
|
43
|
+
clearTimeout(rereadTimer);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { BrowserReportedTimeZone } from "./voiceApiModels.js";
|
|
2
|
+
/**
|
|
3
|
+
* The zone the browser is in, for the caller's sense of "tomorrow at 9", as the app's chat sends it
|
|
4
|
+
* with a message. A zone the server cannot load is still sent: the server stores it and uses the
|
|
5
|
+
* app's default zone. A browser that reports nothing, or whose lookup throws, sends a zone not
|
|
6
|
+
* reported, so the browser talk still starts.
|
|
7
|
+
*/
|
|
8
|
+
export declare function readTheBrowsersTimeZone(): BrowserReportedTimeZone;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The zone the browser is in, for the caller's sense of "tomorrow at 9", as the app's chat sends it
|
|
3
|
+
* with a message. A zone the server cannot load is still sent: the server stores it and uses the
|
|
4
|
+
* app's default zone. A browser that reports nothing, or whose lookup throws, sends a zone not
|
|
5
|
+
* reported, so the browser talk still starts.
|
|
6
|
+
*/
|
|
7
|
+
export function readTheBrowsersTimeZone() {
|
|
8
|
+
let name;
|
|
9
|
+
try {
|
|
10
|
+
name = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
11
|
+
}
|
|
12
|
+
catch (failure) {
|
|
13
|
+
console.warn("The browser's time zone could not be read; the browser talk reports none", failure);
|
|
14
|
+
return { type: "time_zone_not_reported" };
|
|
15
|
+
}
|
|
16
|
+
return name ? { type: "iana_time_zone", name } : { type: "time_zone_not_reported" };
|
|
17
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Milliseconds since the epoch of a backend timestamp, or NaN when it is not one.
|
|
3
|
+
*
|
|
4
|
+
* The backend sends Python ISO datetimes with up to six fractional digits;
|
|
5
|
+
* some browser engines parse at most three, so the fraction is cut to
|
|
6
|
+
* milliseconds before parsing.
|
|
7
|
+
*/
|
|
8
|
+
export declare function readTimestampMilliseconds(timestamp: string): number;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Milliseconds since the epoch of a backend timestamp, or NaN when it is not one.
|
|
3
|
+
*
|
|
4
|
+
* The backend sends Python ISO datetimes with up to six fractional digits;
|
|
5
|
+
* some browser engines parse at most three, so the fraction is cut to
|
|
6
|
+
* milliseconds before parsing.
|
|
7
|
+
*/
|
|
8
|
+
export function readTimestampMilliseconds(timestamp) {
|
|
9
|
+
return Date.parse(timestamp.replace(/(\.\d{3})\d+/, "$1"));
|
|
10
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { CallId } from "./voiceApiModels.js";
|
|
2
|
+
import { type EndCallOutcome } from "./voiceConversationsApi.js";
|
|
3
|
+
/**
|
|
4
|
+
* Asks the backend to end the member's phone call or another tab's browser talk, then rereads presence.
|
|
5
|
+
* For this tab's own browser talk use `useBrowserTalk().end()`, which also releases the microphone.
|
|
6
|
+
*/
|
|
7
|
+
export declare function requestEndOfCall(callId: CallId): Promise<EndCallOutcome>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { getCallPresenceWatcherForThisTab } from "./getCallPresenceWatcherForThisTab.js";
|
|
2
|
+
import { postEndCall } from "./voiceConversationsApi.js";
|
|
3
|
+
/**
|
|
4
|
+
* Asks the backend to end the member's phone call or another tab's browser talk, then rereads presence.
|
|
5
|
+
* For this tab's own browser talk use `useBrowserTalk().end()`, which also releases the microphone.
|
|
6
|
+
*/
|
|
7
|
+
export async function requestEndOfCall(callId) {
|
|
8
|
+
const outcome = await postEndCall(callId, "normal");
|
|
9
|
+
getCallPresenceWatcherForThisTab().refetch();
|
|
10
|
+
return outcome;
|
|
11
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Starts the tab's one browser talk error follower (`createBrowserTalkErrorFollower`) unless it already runs, so
|
|
3
|
+
* an error about another call ("Your phone call took over") clears once that call ends. It runs
|
|
4
|
+
* for the life of the tab and polls presence only while such an error stands.
|
|
5
|
+
*/
|
|
6
|
+
export declare function startBrowserTalkErrorFollowerForThisTab(): void;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createBrowserTalkErrorFollower, } from "./createBrowserTalkErrorFollower.js";
|
|
2
|
+
import { getCallPresenceWatcherForThisTab } from "./getCallPresenceWatcherForThisTab.js";
|
|
3
|
+
import { getBrowserTalkForThisTab } from "./getBrowserTalkForThisTab.js";
|
|
4
|
+
let browserTalkErrorFollowerForThisTab = null;
|
|
5
|
+
/**
|
|
6
|
+
* Starts the tab's one browser talk error follower (`createBrowserTalkErrorFollower`) unless it already runs, so
|
|
7
|
+
* an error about another call ("Your phone call took over") clears once that call ends. It runs
|
|
8
|
+
* for the life of the tab and polls presence only while such an error stands.
|
|
9
|
+
*/
|
|
10
|
+
export function startBrowserTalkErrorFollowerForThisTab() {
|
|
11
|
+
browserTalkErrorFollowerForThisTab ?? (browserTalkErrorFollowerForThisTab = createBrowserTalkErrorFollower(getBrowserTalkForThisTab(), getCallPresenceWatcherForThisTab()));
|
|
12
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { AdminAddedPhoneNumber, AdminListedPhoneNumber, PhoneNumberUploadReport } from "./voiceApiModels.js";
|
|
2
|
+
/** The admin's last change: under way, done with its result, or failed with a sentence to show. */
|
|
3
|
+
export type AdminPhoneNumbersState = {
|
|
4
|
+
status: "idle";
|
|
5
|
+
} | {
|
|
6
|
+
status: "sending";
|
|
7
|
+
} | {
|
|
8
|
+
status: "added";
|
|
9
|
+
added: AdminAddedPhoneNumber;
|
|
10
|
+
} | {
|
|
11
|
+
status: "uploaded";
|
|
12
|
+
report: PhoneNumberUploadReport;
|
|
13
|
+
} | {
|
|
14
|
+
status: "removed";
|
|
15
|
+
memberUserId: string;
|
|
16
|
+
} | {
|
|
17
|
+
status: "failed";
|
|
18
|
+
error: string;
|
|
19
|
+
};
|
|
20
|
+
/** `loading` while a read of the list is under way; `load_failed` keeps the last list read, if any. */
|
|
21
|
+
export type AdminPhoneNumberListStatus = "loading" | "ready" | "load_failed";
|
|
22
|
+
export interface AdminPhoneNumbersControls {
|
|
23
|
+
state: AdminPhoneNumbersState;
|
|
24
|
+
/** Every saved number from the last complete read; `undefined` until one answers. */
|
|
25
|
+
phoneNumbers: AdminListedPhoneNumber[] | undefined;
|
|
26
|
+
listStatus: AdminPhoneNumberListStatus;
|
|
27
|
+
/** Why the last read of the list failed, as a sentence to show; `null` after a success. */
|
|
28
|
+
listError: string | null;
|
|
29
|
+
/** Adds or replaces one person's number; `member` is their email or Ciridae Auth user id. Resolves `true` once saved. */
|
|
30
|
+
addOne: (member: string, typedPhoneNumber: string) => Promise<boolean>;
|
|
31
|
+
/** Uploads a CSV with `email` and `phone_number` columns; the report lists every row. Resolves `true` once read. */
|
|
32
|
+
uploadCsv: (csv: Blob | string) => Promise<boolean>;
|
|
33
|
+
/** Removes one person's number by their Ciridae Auth user id. Resolves `true` once it is gone. */
|
|
34
|
+
removeOne: (memberUserId: string) => Promise<boolean>;
|
|
35
|
+
/** Reads the whole list again, every page. */
|
|
36
|
+
reloadList: () => void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The admin phone-number routes (`GET`, `POST` and `DELETE {prefix}/admin/phone-numbers`, and the
|
|
40
|
+
* CSV upload), served only when the backend install was given `require_admin`. The app's own admin
|
|
41
|
+
* check answers anyone else, which reads here as a failed request.
|
|
42
|
+
*
|
|
43
|
+
* The list is read on mount, every page, and again after each change that succeeds. A read that
|
|
44
|
+
* fails keeps the last complete list and reports the failure; a partial list is never shown. A
|
|
45
|
+
* read or change that answers after a newer one, or after unmount, is dropped.
|
|
46
|
+
*/
|
|
47
|
+
export declare function useAdminPhoneNumbers(): AdminPhoneNumbersControls;
|