@tryprojectblue/dialer 0.1.0
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/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +176 -0
- package/dist/index.cjs +568 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +328 -0
- package/dist/index.d.ts +328 -0
- package/dist/index.js +535 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
type Listener = (...args: never[]) => void;
|
|
2
|
+
/** Minimal typed event emitter; avoids a runtime dependency in the browser bundle. */
|
|
3
|
+
declare class TypedEmitter<Events extends {
|
|
4
|
+
[K in keyof Events]: Listener;
|
|
5
|
+
}> {
|
|
6
|
+
private listeners;
|
|
7
|
+
on<K extends keyof Events>(event: K, listener: Events[K]): () => void;
|
|
8
|
+
once<K extends keyof Events>(event: K, listener: Events[K]): () => void;
|
|
9
|
+
off<K extends keyof Events>(event: K, listener: Events[K]): void;
|
|
10
|
+
protected emit<K extends keyof Events>(event: K, ...args: Parameters<Events[K]>): void;
|
|
11
|
+
protected removeAllListeners(): void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The slice of the voice engine the SDK uses, expressed as plain interfaces so
|
|
16
|
+
* the engine can be swapped in tests and never leaks into the public API.
|
|
17
|
+
*/
|
|
18
|
+
interface EngineCall {
|
|
19
|
+
on(event: string, listener: (...args: never[]) => void): unknown;
|
|
20
|
+
off?(event: string, listener: (...args: never[]) => void): unknown;
|
|
21
|
+
removeListener?(event: string, listener: (...args: never[]) => void): unknown;
|
|
22
|
+
accept(): void;
|
|
23
|
+
reject(): void;
|
|
24
|
+
disconnect(): void;
|
|
25
|
+
mute(shouldMute?: boolean): void;
|
|
26
|
+
isMuted(): boolean;
|
|
27
|
+
sendDigits(digits: string): void;
|
|
28
|
+
status(): string;
|
|
29
|
+
readonly parameters: Record<string, string>;
|
|
30
|
+
readonly customParameters: Map<string, string>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type DialerErrorCode =
|
|
34
|
+
/** The workspace or line has used its daily minute cap (HTTP 402 `dialer_cap_reached`). */
|
|
35
|
+
'cap_reached'
|
|
36
|
+
/** No dialer subscription on the workspace (403). */
|
|
37
|
+
| 'subscription_required'
|
|
38
|
+
/** Account is past due / paywalled (402 `account_paywalled`). */
|
|
39
|
+
| 'account_paywalled'
|
|
40
|
+
/** Your server is minting too fast for this key (429). */
|
|
41
|
+
| 'rate_limited'
|
|
42
|
+
/** The line id is unknown, not yours, inbound-only, or a shared/trial line. */
|
|
43
|
+
| 'line_unavailable'
|
|
44
|
+
/** Dialer not provisioned for this workspace yet (404 `dialer_not_provisioned`). */
|
|
45
|
+
| 'not_provisioned'
|
|
46
|
+
/** The token provider threw, returned garbage, or the mint failed for another reason. */
|
|
47
|
+
| 'token_unavailable'
|
|
48
|
+
/** The session was used before `start()` or after `destroy()`. */
|
|
49
|
+
| 'invalid_state'
|
|
50
|
+
/** The browser cannot provide a microphone. */
|
|
51
|
+
| 'microphone_unavailable'
|
|
52
|
+
/** Connection to the voice service failed. */
|
|
53
|
+
| 'connection_failed'
|
|
54
|
+
/** Anything else from the voice engine; see `cause`. */
|
|
55
|
+
| 'call_failed';
|
|
56
|
+
declare class DialerError extends Error {
|
|
57
|
+
readonly code: DialerErrorCode;
|
|
58
|
+
/** HTTP status of the mint response, when the error came from one. */
|
|
59
|
+
readonly status: number | null;
|
|
60
|
+
readonly cause?: unknown;
|
|
61
|
+
constructor(code: DialerErrorCode, message: string, options?: {
|
|
62
|
+
status?: number;
|
|
63
|
+
cause?: unknown;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
declare class DialerCapReachedError extends DialerError {
|
|
67
|
+
readonly used: number | null;
|
|
68
|
+
readonly included: number | null;
|
|
69
|
+
readonly cap: number | null;
|
|
70
|
+
/** When calling resumes (midnight UTC), if the server said. */
|
|
71
|
+
readonly periodEnd: Date | null;
|
|
72
|
+
constructor(body: Partial<DialerMintErrorBody>, status?: number);
|
|
73
|
+
}
|
|
74
|
+
declare class DialerRateLimitedError extends DialerError {
|
|
75
|
+
readonly retryAfterSeconds: number;
|
|
76
|
+
constructor(body: Partial<DialerMintErrorBody>, status?: number);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Turn a non-2xx mint response into the matching typed error, or null when
|
|
80
|
+
* `status` is a success. Use it in your `tokenProvider`:
|
|
81
|
+
*
|
|
82
|
+
* const res = await fetch('/my-server/dialer-token');
|
|
83
|
+
* const body = await res.json();
|
|
84
|
+
* const error = mintErrorFromResponse(res.status, body);
|
|
85
|
+
* if (error) throw error;
|
|
86
|
+
* return body;
|
|
87
|
+
*/
|
|
88
|
+
declare function mintErrorFromResponse(status: number, body: unknown): DialerError | null;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Public types for @tryprojectblue/dialer. Nothing here names the underlying
|
|
92
|
+
* voice engine — integrators build against Project Blue, not against it.
|
|
93
|
+
*/
|
|
94
|
+
type DialerAllowanceState = 'ok' | 'soft' | 'hard';
|
|
95
|
+
/** Today's minute allowance for the workspace, as returned by the token mint. */
|
|
96
|
+
interface DialerAllowance {
|
|
97
|
+
state: DialerAllowanceState;
|
|
98
|
+
/** Whole minutes used today across the workspace. */
|
|
99
|
+
used: number;
|
|
100
|
+
/** Included minutes today across the workspace. */
|
|
101
|
+
included: number;
|
|
102
|
+
/** Workspace ceiling in minutes; null when the account has no cap. */
|
|
103
|
+
cap: number | null;
|
|
104
|
+
/** used / included, whole percent. */
|
|
105
|
+
pct: number;
|
|
106
|
+
/** ISO timestamp when the current allowance period ends (midnight UTC). */
|
|
107
|
+
periodEnd: string;
|
|
108
|
+
/** The line this token is scoped to, against its own daily cap. */
|
|
109
|
+
line: {
|
|
110
|
+
used: number;
|
|
111
|
+
cap: number | null;
|
|
112
|
+
capped: boolean;
|
|
113
|
+
} | null;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The JSON body of a successful `POST /api/v1/dialer/token`. Your server
|
|
117
|
+
* forwards it to the browser as-is; the SDK reads everything it needs from it.
|
|
118
|
+
*/
|
|
119
|
+
interface DialerTokenResponse {
|
|
120
|
+
token: string;
|
|
121
|
+
/** ISO timestamp. */
|
|
122
|
+
expiresAt?: string;
|
|
123
|
+
ttlSeconds?: number;
|
|
124
|
+
lineId?: string;
|
|
125
|
+
inboundEnabled?: boolean;
|
|
126
|
+
maxCallsPerToken?: number;
|
|
127
|
+
allowance?: DialerAllowance | null;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Called whenever the SDK needs a token: at start, before it expires, and
|
|
131
|
+
* when a token has been used for its maximum number of calls. Must call YOUR
|
|
132
|
+
* server, which holds the workspace API key and calls the mint endpoint.
|
|
133
|
+
*
|
|
134
|
+
* Return the mint response body (recommended) or just the token string. To
|
|
135
|
+
* surface refusals, either throw `mintErrorFromResponse(status, body)` or
|
|
136
|
+
* return the error body itself — the SDK turns both into typed errors.
|
|
137
|
+
*/
|
|
138
|
+
type DialerTokenProvider = () => Promise<DialerTokenResponse | DialerMintErrorBody | string>;
|
|
139
|
+
/**
|
|
140
|
+
* Error body shape of a non-2xx mint response. When your provider RETURNS this
|
|
141
|
+
* body instead of throwing `mintErrorFromResponse(status, body)`, add the HTTP
|
|
142
|
+
* `status` so the SDK can report it; without it the SDK infers 402 for a cap
|
|
143
|
+
* refusal and 400 for everything else.
|
|
144
|
+
*/
|
|
145
|
+
interface DialerMintErrorBody {
|
|
146
|
+
error: string;
|
|
147
|
+
code?: string;
|
|
148
|
+
status?: number;
|
|
149
|
+
used?: number;
|
|
150
|
+
included?: number;
|
|
151
|
+
cap?: number | null;
|
|
152
|
+
periodEnd?: string;
|
|
153
|
+
retryAfterSeconds?: number;
|
|
154
|
+
}
|
|
155
|
+
interface DialerLogger {
|
|
156
|
+
debug(...args: unknown[]): void;
|
|
157
|
+
info(...args: unknown[]): void;
|
|
158
|
+
warn(...args: unknown[]): void;
|
|
159
|
+
error(...args: unknown[]): void;
|
|
160
|
+
}
|
|
161
|
+
interface DialerOptions {
|
|
162
|
+
tokenProvider: DialerTokenProvider;
|
|
163
|
+
/**
|
|
164
|
+
* Refresh the token this many seconds before it expires. Default 60.
|
|
165
|
+
* Tokens live at most 15 minutes, so keep this well under that.
|
|
166
|
+
*/
|
|
167
|
+
refreshLeadSeconds?: number;
|
|
168
|
+
/** Where the SDK logs. `null` silences it. Default: `console` for warn/error only. */
|
|
169
|
+
logger?: DialerLogger | null;
|
|
170
|
+
/**
|
|
171
|
+
* Preferred media region(s), e.g. `'ashburn'` or `['ashburn', 'roaming']`.
|
|
172
|
+
* Leave unset unless you have measured a reason to pin one.
|
|
173
|
+
*/
|
|
174
|
+
edge?: string | string[];
|
|
175
|
+
/** Ring in the browser when an inbound call arrives. Default true. */
|
|
176
|
+
playIncomingRingtone?: boolean;
|
|
177
|
+
}
|
|
178
|
+
type DialerCallDirection = 'outbound' | 'inbound';
|
|
179
|
+
type DialerCallStatus =
|
|
180
|
+
/** Created, no signaling yet. */
|
|
181
|
+
'pending'
|
|
182
|
+
/** Signaling in progress. */
|
|
183
|
+
| 'connecting'
|
|
184
|
+
/** Remote side is being rung. */
|
|
185
|
+
| 'ringing'
|
|
186
|
+
/** Media is flowing. */
|
|
187
|
+
| 'open'
|
|
188
|
+
/** Lost the connection and trying to recover. */
|
|
189
|
+
| 'reconnecting'
|
|
190
|
+
/** Ended. */
|
|
191
|
+
| 'closed';
|
|
192
|
+
interface DialerCallEvents {
|
|
193
|
+
/** Remote side is ringing (outbound). `hasEarlyMedia` = carrier audio already playing. */
|
|
194
|
+
ringing: (info: {
|
|
195
|
+
hasEarlyMedia: boolean;
|
|
196
|
+
}) => void;
|
|
197
|
+
/** Call connected. */
|
|
198
|
+
accepted: () => void;
|
|
199
|
+
/** Call ended, by either side. */
|
|
200
|
+
disconnected: () => void;
|
|
201
|
+
/** Inbound call was cancelled by the caller before you answered. */
|
|
202
|
+
cancelled: () => void;
|
|
203
|
+
/** Rejected locally (inbound) or the remote side rejected. */
|
|
204
|
+
rejected: () => void;
|
|
205
|
+
error: (error: DialerError) => void;
|
|
206
|
+
reconnecting: (error: DialerError) => void;
|
|
207
|
+
reconnected: () => void;
|
|
208
|
+
mute: (muted: boolean) => void;
|
|
209
|
+
/** Non-fatal media quality warning, e.g. `high-jitter`. */
|
|
210
|
+
warning: (name: string) => void;
|
|
211
|
+
/** A previously raised warning cleared. */
|
|
212
|
+
warningCleared: (name: string) => void;
|
|
213
|
+
}
|
|
214
|
+
interface DialerEvents {
|
|
215
|
+
/** Session is ready to place calls. Fires again after every successful token refresh. */
|
|
216
|
+
ready: (info: {
|
|
217
|
+
inboundEnabled: boolean;
|
|
218
|
+
}) => void;
|
|
219
|
+
/** A fresh allowance arrived with a token. Warn your users on `state: 'soft'`. */
|
|
220
|
+
allowance: (allowance: DialerAllowance) => void;
|
|
221
|
+
/** The workspace or line hit its cap: no further tokens until `periodEnd`. */
|
|
222
|
+
capReached: (error: DialerCapReachedError) => void;
|
|
223
|
+
/** Someone is calling the line this session is scoped to. */
|
|
224
|
+
incoming: (call: DialerCall) => void;
|
|
225
|
+
/** Token refresh failed; the session keeps its current token until it expires. */
|
|
226
|
+
tokenRefreshFailed: (error: DialerError) => void;
|
|
227
|
+
/** Session-level error. Calls carry their own `error` events. */
|
|
228
|
+
error: (error: DialerError) => void;
|
|
229
|
+
/** The session was destroyed. */
|
|
230
|
+
destroyed: () => void;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* One call. Outbound calls come from `dialer.call()`; inbound calls arrive via
|
|
235
|
+
* the dialer's `incoming` event and must be `accept()`ed or `reject()`ed.
|
|
236
|
+
*/
|
|
237
|
+
declare class DialerCall extends TypedEmitter<DialerCallEvents> {
|
|
238
|
+
readonly direction: DialerCallDirection;
|
|
239
|
+
/** Dialed number (outbound) or caller number (inbound), E.164 when known. */
|
|
240
|
+
readonly remoteNumber: string | null;
|
|
241
|
+
/** Inbound only: caller display name from your CRM, when the line has one. */
|
|
242
|
+
readonly callerName: string | null;
|
|
243
|
+
/** Inbound only: the name of the line being called. */
|
|
244
|
+
readonly lineName: string | null;
|
|
245
|
+
private engineCall;
|
|
246
|
+
private ended;
|
|
247
|
+
/** @internal */
|
|
248
|
+
constructor(engineCall: EngineCall, direction: DialerCallDirection, remoteNumber: string | null);
|
|
249
|
+
private attach;
|
|
250
|
+
private finish;
|
|
251
|
+
status(): DialerCallStatus;
|
|
252
|
+
isActive(): boolean;
|
|
253
|
+
/** Inbound only. */
|
|
254
|
+
accept(): void;
|
|
255
|
+
/** Inbound only: decline without answering. */
|
|
256
|
+
reject(): void;
|
|
257
|
+
hangup(): void;
|
|
258
|
+
mute(shouldMute?: boolean): void;
|
|
259
|
+
isMuted(): boolean;
|
|
260
|
+
/** DTMF tones, e.g. `'1'` or `'123#'`. */
|
|
261
|
+
sendDigits(digits: string): void;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* A calling session for one Project Blue line.
|
|
266
|
+
*
|
|
267
|
+
* const dialer = new ProjectBlueDialer({ tokenProvider });
|
|
268
|
+
* await dialer.start();
|
|
269
|
+
* const call = await dialer.call('+15551234567');
|
|
270
|
+
* call.on('disconnected', () => ...);
|
|
271
|
+
*
|
|
272
|
+
* The session never sees your workspace API key: `tokenProvider` asks YOUR
|
|
273
|
+
* server, which calls `POST /api/v1/dialer/token` and returns the body.
|
|
274
|
+
*/
|
|
275
|
+
declare class ProjectBlueDialer extends TypedEmitter<DialerEvents> {
|
|
276
|
+
private readonly options;
|
|
277
|
+
private readonly log;
|
|
278
|
+
private readonly now;
|
|
279
|
+
private device;
|
|
280
|
+
private active;
|
|
281
|
+
private latestAllowance;
|
|
282
|
+
private currentCall;
|
|
283
|
+
private refreshTimer;
|
|
284
|
+
private refreshInFlight;
|
|
285
|
+
private refreshFailures;
|
|
286
|
+
private reregisterWhenCallEnds;
|
|
287
|
+
private destroyed;
|
|
288
|
+
private started;
|
|
289
|
+
constructor(options: DialerOptions);
|
|
290
|
+
/** Latest allowance the server reported with a token; null before `start()`. */
|
|
291
|
+
get allowance(): DialerAllowance | null;
|
|
292
|
+
/** Whether the current token lets this session receive calls. */
|
|
293
|
+
get inboundEnabled(): boolean;
|
|
294
|
+
get activeCall(): DialerCall | null;
|
|
295
|
+
isReady(): boolean;
|
|
296
|
+
/** Fetch the first token and connect. Resolves once calls can be placed. */
|
|
297
|
+
start(): Promise<void>;
|
|
298
|
+
/**
|
|
299
|
+
* Place an outbound call from the line this session is scoped to. `to` must
|
|
300
|
+
* be E.164 (`+15551234567`). Rejects with a `DialerError` (`cap_reached`,
|
|
301
|
+
* `token_unavailable`, `microphone_unavailable`, …) when the call cannot start.
|
|
302
|
+
*/
|
|
303
|
+
call(to: string): Promise<DialerCall>;
|
|
304
|
+
/** Force a token refresh now. Usually unnecessary — the session refreshes itself. */
|
|
305
|
+
refreshToken(): Promise<void>;
|
|
306
|
+
/** Hang up any call, stop refreshing, and release the microphone. Idempotent. */
|
|
307
|
+
destroy(): Promise<void>;
|
|
308
|
+
private refreshLeadMs;
|
|
309
|
+
private assertNotDestroyed;
|
|
310
|
+
private assertReady;
|
|
311
|
+
private expiresWithin;
|
|
312
|
+
private fetchToken;
|
|
313
|
+
private applyToken;
|
|
314
|
+
private scheduleRefresh;
|
|
315
|
+
private clearRefreshTimer;
|
|
316
|
+
private runRefresh;
|
|
317
|
+
/**
|
|
318
|
+
* Drop the registration bound to the previous token's identity and register
|
|
319
|
+
* again with the current one. Deferred while a call is up: unregistering
|
|
320
|
+
* mid-call would tear down the signaling the call rides on.
|
|
321
|
+
*/
|
|
322
|
+
private reregisterUnderCurrentIdentity;
|
|
323
|
+
private syncRegistration;
|
|
324
|
+
private attachDeviceListeners;
|
|
325
|
+
private trackCall;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export { type DialerAllowance, type DialerAllowanceState, DialerCall, type DialerCallDirection, type DialerCallEvents, type DialerCallStatus, DialerCapReachedError, DialerError, type DialerErrorCode, type DialerEvents, type DialerLogger, type DialerMintErrorBody, type DialerOptions, DialerRateLimitedError, type DialerTokenProvider, type DialerTokenResponse, ProjectBlueDialer, mintErrorFromResponse };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
type Listener = (...args: never[]) => void;
|
|
2
|
+
/** Minimal typed event emitter; avoids a runtime dependency in the browser bundle. */
|
|
3
|
+
declare class TypedEmitter<Events extends {
|
|
4
|
+
[K in keyof Events]: Listener;
|
|
5
|
+
}> {
|
|
6
|
+
private listeners;
|
|
7
|
+
on<K extends keyof Events>(event: K, listener: Events[K]): () => void;
|
|
8
|
+
once<K extends keyof Events>(event: K, listener: Events[K]): () => void;
|
|
9
|
+
off<K extends keyof Events>(event: K, listener: Events[K]): void;
|
|
10
|
+
protected emit<K extends keyof Events>(event: K, ...args: Parameters<Events[K]>): void;
|
|
11
|
+
protected removeAllListeners(): void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The slice of the voice engine the SDK uses, expressed as plain interfaces so
|
|
16
|
+
* the engine can be swapped in tests and never leaks into the public API.
|
|
17
|
+
*/
|
|
18
|
+
interface EngineCall {
|
|
19
|
+
on(event: string, listener: (...args: never[]) => void): unknown;
|
|
20
|
+
off?(event: string, listener: (...args: never[]) => void): unknown;
|
|
21
|
+
removeListener?(event: string, listener: (...args: never[]) => void): unknown;
|
|
22
|
+
accept(): void;
|
|
23
|
+
reject(): void;
|
|
24
|
+
disconnect(): void;
|
|
25
|
+
mute(shouldMute?: boolean): void;
|
|
26
|
+
isMuted(): boolean;
|
|
27
|
+
sendDigits(digits: string): void;
|
|
28
|
+
status(): string;
|
|
29
|
+
readonly parameters: Record<string, string>;
|
|
30
|
+
readonly customParameters: Map<string, string>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type DialerErrorCode =
|
|
34
|
+
/** The workspace or line has used its daily minute cap (HTTP 402 `dialer_cap_reached`). */
|
|
35
|
+
'cap_reached'
|
|
36
|
+
/** No dialer subscription on the workspace (403). */
|
|
37
|
+
| 'subscription_required'
|
|
38
|
+
/** Account is past due / paywalled (402 `account_paywalled`). */
|
|
39
|
+
| 'account_paywalled'
|
|
40
|
+
/** Your server is minting too fast for this key (429). */
|
|
41
|
+
| 'rate_limited'
|
|
42
|
+
/** The line id is unknown, not yours, inbound-only, or a shared/trial line. */
|
|
43
|
+
| 'line_unavailable'
|
|
44
|
+
/** Dialer not provisioned for this workspace yet (404 `dialer_not_provisioned`). */
|
|
45
|
+
| 'not_provisioned'
|
|
46
|
+
/** The token provider threw, returned garbage, or the mint failed for another reason. */
|
|
47
|
+
| 'token_unavailable'
|
|
48
|
+
/** The session was used before `start()` or after `destroy()`. */
|
|
49
|
+
| 'invalid_state'
|
|
50
|
+
/** The browser cannot provide a microphone. */
|
|
51
|
+
| 'microphone_unavailable'
|
|
52
|
+
/** Connection to the voice service failed. */
|
|
53
|
+
| 'connection_failed'
|
|
54
|
+
/** Anything else from the voice engine; see `cause`. */
|
|
55
|
+
| 'call_failed';
|
|
56
|
+
declare class DialerError extends Error {
|
|
57
|
+
readonly code: DialerErrorCode;
|
|
58
|
+
/** HTTP status of the mint response, when the error came from one. */
|
|
59
|
+
readonly status: number | null;
|
|
60
|
+
readonly cause?: unknown;
|
|
61
|
+
constructor(code: DialerErrorCode, message: string, options?: {
|
|
62
|
+
status?: number;
|
|
63
|
+
cause?: unknown;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
declare class DialerCapReachedError extends DialerError {
|
|
67
|
+
readonly used: number | null;
|
|
68
|
+
readonly included: number | null;
|
|
69
|
+
readonly cap: number | null;
|
|
70
|
+
/** When calling resumes (midnight UTC), if the server said. */
|
|
71
|
+
readonly periodEnd: Date | null;
|
|
72
|
+
constructor(body: Partial<DialerMintErrorBody>, status?: number);
|
|
73
|
+
}
|
|
74
|
+
declare class DialerRateLimitedError extends DialerError {
|
|
75
|
+
readonly retryAfterSeconds: number;
|
|
76
|
+
constructor(body: Partial<DialerMintErrorBody>, status?: number);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Turn a non-2xx mint response into the matching typed error, or null when
|
|
80
|
+
* `status` is a success. Use it in your `tokenProvider`:
|
|
81
|
+
*
|
|
82
|
+
* const res = await fetch('/my-server/dialer-token');
|
|
83
|
+
* const body = await res.json();
|
|
84
|
+
* const error = mintErrorFromResponse(res.status, body);
|
|
85
|
+
* if (error) throw error;
|
|
86
|
+
* return body;
|
|
87
|
+
*/
|
|
88
|
+
declare function mintErrorFromResponse(status: number, body: unknown): DialerError | null;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Public types for @tryprojectblue/dialer. Nothing here names the underlying
|
|
92
|
+
* voice engine — integrators build against Project Blue, not against it.
|
|
93
|
+
*/
|
|
94
|
+
type DialerAllowanceState = 'ok' | 'soft' | 'hard';
|
|
95
|
+
/** Today's minute allowance for the workspace, as returned by the token mint. */
|
|
96
|
+
interface DialerAllowance {
|
|
97
|
+
state: DialerAllowanceState;
|
|
98
|
+
/** Whole minutes used today across the workspace. */
|
|
99
|
+
used: number;
|
|
100
|
+
/** Included minutes today across the workspace. */
|
|
101
|
+
included: number;
|
|
102
|
+
/** Workspace ceiling in minutes; null when the account has no cap. */
|
|
103
|
+
cap: number | null;
|
|
104
|
+
/** used / included, whole percent. */
|
|
105
|
+
pct: number;
|
|
106
|
+
/** ISO timestamp when the current allowance period ends (midnight UTC). */
|
|
107
|
+
periodEnd: string;
|
|
108
|
+
/** The line this token is scoped to, against its own daily cap. */
|
|
109
|
+
line: {
|
|
110
|
+
used: number;
|
|
111
|
+
cap: number | null;
|
|
112
|
+
capped: boolean;
|
|
113
|
+
} | null;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The JSON body of a successful `POST /api/v1/dialer/token`. Your server
|
|
117
|
+
* forwards it to the browser as-is; the SDK reads everything it needs from it.
|
|
118
|
+
*/
|
|
119
|
+
interface DialerTokenResponse {
|
|
120
|
+
token: string;
|
|
121
|
+
/** ISO timestamp. */
|
|
122
|
+
expiresAt?: string;
|
|
123
|
+
ttlSeconds?: number;
|
|
124
|
+
lineId?: string;
|
|
125
|
+
inboundEnabled?: boolean;
|
|
126
|
+
maxCallsPerToken?: number;
|
|
127
|
+
allowance?: DialerAllowance | null;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Called whenever the SDK needs a token: at start, before it expires, and
|
|
131
|
+
* when a token has been used for its maximum number of calls. Must call YOUR
|
|
132
|
+
* server, which holds the workspace API key and calls the mint endpoint.
|
|
133
|
+
*
|
|
134
|
+
* Return the mint response body (recommended) or just the token string. To
|
|
135
|
+
* surface refusals, either throw `mintErrorFromResponse(status, body)` or
|
|
136
|
+
* return the error body itself — the SDK turns both into typed errors.
|
|
137
|
+
*/
|
|
138
|
+
type DialerTokenProvider = () => Promise<DialerTokenResponse | DialerMintErrorBody | string>;
|
|
139
|
+
/**
|
|
140
|
+
* Error body shape of a non-2xx mint response. When your provider RETURNS this
|
|
141
|
+
* body instead of throwing `mintErrorFromResponse(status, body)`, add the HTTP
|
|
142
|
+
* `status` so the SDK can report it; without it the SDK infers 402 for a cap
|
|
143
|
+
* refusal and 400 for everything else.
|
|
144
|
+
*/
|
|
145
|
+
interface DialerMintErrorBody {
|
|
146
|
+
error: string;
|
|
147
|
+
code?: string;
|
|
148
|
+
status?: number;
|
|
149
|
+
used?: number;
|
|
150
|
+
included?: number;
|
|
151
|
+
cap?: number | null;
|
|
152
|
+
periodEnd?: string;
|
|
153
|
+
retryAfterSeconds?: number;
|
|
154
|
+
}
|
|
155
|
+
interface DialerLogger {
|
|
156
|
+
debug(...args: unknown[]): void;
|
|
157
|
+
info(...args: unknown[]): void;
|
|
158
|
+
warn(...args: unknown[]): void;
|
|
159
|
+
error(...args: unknown[]): void;
|
|
160
|
+
}
|
|
161
|
+
interface DialerOptions {
|
|
162
|
+
tokenProvider: DialerTokenProvider;
|
|
163
|
+
/**
|
|
164
|
+
* Refresh the token this many seconds before it expires. Default 60.
|
|
165
|
+
* Tokens live at most 15 minutes, so keep this well under that.
|
|
166
|
+
*/
|
|
167
|
+
refreshLeadSeconds?: number;
|
|
168
|
+
/** Where the SDK logs. `null` silences it. Default: `console` for warn/error only. */
|
|
169
|
+
logger?: DialerLogger | null;
|
|
170
|
+
/**
|
|
171
|
+
* Preferred media region(s), e.g. `'ashburn'` or `['ashburn', 'roaming']`.
|
|
172
|
+
* Leave unset unless you have measured a reason to pin one.
|
|
173
|
+
*/
|
|
174
|
+
edge?: string | string[];
|
|
175
|
+
/** Ring in the browser when an inbound call arrives. Default true. */
|
|
176
|
+
playIncomingRingtone?: boolean;
|
|
177
|
+
}
|
|
178
|
+
type DialerCallDirection = 'outbound' | 'inbound';
|
|
179
|
+
type DialerCallStatus =
|
|
180
|
+
/** Created, no signaling yet. */
|
|
181
|
+
'pending'
|
|
182
|
+
/** Signaling in progress. */
|
|
183
|
+
| 'connecting'
|
|
184
|
+
/** Remote side is being rung. */
|
|
185
|
+
| 'ringing'
|
|
186
|
+
/** Media is flowing. */
|
|
187
|
+
| 'open'
|
|
188
|
+
/** Lost the connection and trying to recover. */
|
|
189
|
+
| 'reconnecting'
|
|
190
|
+
/** Ended. */
|
|
191
|
+
| 'closed';
|
|
192
|
+
interface DialerCallEvents {
|
|
193
|
+
/** Remote side is ringing (outbound). `hasEarlyMedia` = carrier audio already playing. */
|
|
194
|
+
ringing: (info: {
|
|
195
|
+
hasEarlyMedia: boolean;
|
|
196
|
+
}) => void;
|
|
197
|
+
/** Call connected. */
|
|
198
|
+
accepted: () => void;
|
|
199
|
+
/** Call ended, by either side. */
|
|
200
|
+
disconnected: () => void;
|
|
201
|
+
/** Inbound call was cancelled by the caller before you answered. */
|
|
202
|
+
cancelled: () => void;
|
|
203
|
+
/** Rejected locally (inbound) or the remote side rejected. */
|
|
204
|
+
rejected: () => void;
|
|
205
|
+
error: (error: DialerError) => void;
|
|
206
|
+
reconnecting: (error: DialerError) => void;
|
|
207
|
+
reconnected: () => void;
|
|
208
|
+
mute: (muted: boolean) => void;
|
|
209
|
+
/** Non-fatal media quality warning, e.g. `high-jitter`. */
|
|
210
|
+
warning: (name: string) => void;
|
|
211
|
+
/** A previously raised warning cleared. */
|
|
212
|
+
warningCleared: (name: string) => void;
|
|
213
|
+
}
|
|
214
|
+
interface DialerEvents {
|
|
215
|
+
/** Session is ready to place calls. Fires again after every successful token refresh. */
|
|
216
|
+
ready: (info: {
|
|
217
|
+
inboundEnabled: boolean;
|
|
218
|
+
}) => void;
|
|
219
|
+
/** A fresh allowance arrived with a token. Warn your users on `state: 'soft'`. */
|
|
220
|
+
allowance: (allowance: DialerAllowance) => void;
|
|
221
|
+
/** The workspace or line hit its cap: no further tokens until `periodEnd`. */
|
|
222
|
+
capReached: (error: DialerCapReachedError) => void;
|
|
223
|
+
/** Someone is calling the line this session is scoped to. */
|
|
224
|
+
incoming: (call: DialerCall) => void;
|
|
225
|
+
/** Token refresh failed; the session keeps its current token until it expires. */
|
|
226
|
+
tokenRefreshFailed: (error: DialerError) => void;
|
|
227
|
+
/** Session-level error. Calls carry their own `error` events. */
|
|
228
|
+
error: (error: DialerError) => void;
|
|
229
|
+
/** The session was destroyed. */
|
|
230
|
+
destroyed: () => void;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* One call. Outbound calls come from `dialer.call()`; inbound calls arrive via
|
|
235
|
+
* the dialer's `incoming` event and must be `accept()`ed or `reject()`ed.
|
|
236
|
+
*/
|
|
237
|
+
declare class DialerCall extends TypedEmitter<DialerCallEvents> {
|
|
238
|
+
readonly direction: DialerCallDirection;
|
|
239
|
+
/** Dialed number (outbound) or caller number (inbound), E.164 when known. */
|
|
240
|
+
readonly remoteNumber: string | null;
|
|
241
|
+
/** Inbound only: caller display name from your CRM, when the line has one. */
|
|
242
|
+
readonly callerName: string | null;
|
|
243
|
+
/** Inbound only: the name of the line being called. */
|
|
244
|
+
readonly lineName: string | null;
|
|
245
|
+
private engineCall;
|
|
246
|
+
private ended;
|
|
247
|
+
/** @internal */
|
|
248
|
+
constructor(engineCall: EngineCall, direction: DialerCallDirection, remoteNumber: string | null);
|
|
249
|
+
private attach;
|
|
250
|
+
private finish;
|
|
251
|
+
status(): DialerCallStatus;
|
|
252
|
+
isActive(): boolean;
|
|
253
|
+
/** Inbound only. */
|
|
254
|
+
accept(): void;
|
|
255
|
+
/** Inbound only: decline without answering. */
|
|
256
|
+
reject(): void;
|
|
257
|
+
hangup(): void;
|
|
258
|
+
mute(shouldMute?: boolean): void;
|
|
259
|
+
isMuted(): boolean;
|
|
260
|
+
/** DTMF tones, e.g. `'1'` or `'123#'`. */
|
|
261
|
+
sendDigits(digits: string): void;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* A calling session for one Project Blue line.
|
|
266
|
+
*
|
|
267
|
+
* const dialer = new ProjectBlueDialer({ tokenProvider });
|
|
268
|
+
* await dialer.start();
|
|
269
|
+
* const call = await dialer.call('+15551234567');
|
|
270
|
+
* call.on('disconnected', () => ...);
|
|
271
|
+
*
|
|
272
|
+
* The session never sees your workspace API key: `tokenProvider` asks YOUR
|
|
273
|
+
* server, which calls `POST /api/v1/dialer/token` and returns the body.
|
|
274
|
+
*/
|
|
275
|
+
declare class ProjectBlueDialer extends TypedEmitter<DialerEvents> {
|
|
276
|
+
private readonly options;
|
|
277
|
+
private readonly log;
|
|
278
|
+
private readonly now;
|
|
279
|
+
private device;
|
|
280
|
+
private active;
|
|
281
|
+
private latestAllowance;
|
|
282
|
+
private currentCall;
|
|
283
|
+
private refreshTimer;
|
|
284
|
+
private refreshInFlight;
|
|
285
|
+
private refreshFailures;
|
|
286
|
+
private reregisterWhenCallEnds;
|
|
287
|
+
private destroyed;
|
|
288
|
+
private started;
|
|
289
|
+
constructor(options: DialerOptions);
|
|
290
|
+
/** Latest allowance the server reported with a token; null before `start()`. */
|
|
291
|
+
get allowance(): DialerAllowance | null;
|
|
292
|
+
/** Whether the current token lets this session receive calls. */
|
|
293
|
+
get inboundEnabled(): boolean;
|
|
294
|
+
get activeCall(): DialerCall | null;
|
|
295
|
+
isReady(): boolean;
|
|
296
|
+
/** Fetch the first token and connect. Resolves once calls can be placed. */
|
|
297
|
+
start(): Promise<void>;
|
|
298
|
+
/**
|
|
299
|
+
* Place an outbound call from the line this session is scoped to. `to` must
|
|
300
|
+
* be E.164 (`+15551234567`). Rejects with a `DialerError` (`cap_reached`,
|
|
301
|
+
* `token_unavailable`, `microphone_unavailable`, …) when the call cannot start.
|
|
302
|
+
*/
|
|
303
|
+
call(to: string): Promise<DialerCall>;
|
|
304
|
+
/** Force a token refresh now. Usually unnecessary — the session refreshes itself. */
|
|
305
|
+
refreshToken(): Promise<void>;
|
|
306
|
+
/** Hang up any call, stop refreshing, and release the microphone. Idempotent. */
|
|
307
|
+
destroy(): Promise<void>;
|
|
308
|
+
private refreshLeadMs;
|
|
309
|
+
private assertNotDestroyed;
|
|
310
|
+
private assertReady;
|
|
311
|
+
private expiresWithin;
|
|
312
|
+
private fetchToken;
|
|
313
|
+
private applyToken;
|
|
314
|
+
private scheduleRefresh;
|
|
315
|
+
private clearRefreshTimer;
|
|
316
|
+
private runRefresh;
|
|
317
|
+
/**
|
|
318
|
+
* Drop the registration bound to the previous token's identity and register
|
|
319
|
+
* again with the current one. Deferred while a call is up: unregistering
|
|
320
|
+
* mid-call would tear down the signaling the call rides on.
|
|
321
|
+
*/
|
|
322
|
+
private reregisterUnderCurrentIdentity;
|
|
323
|
+
private syncRegistration;
|
|
324
|
+
private attachDeviceListeners;
|
|
325
|
+
private trackCall;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export { type DialerAllowance, type DialerAllowanceState, DialerCall, type DialerCallDirection, type DialerCallEvents, type DialerCallStatus, DialerCapReachedError, DialerError, type DialerErrorCode, type DialerEvents, type DialerLogger, type DialerMintErrorBody, type DialerOptions, DialerRateLimitedError, type DialerTokenProvider, type DialerTokenResponse, ProjectBlueDialer, mintErrorFromResponse };
|