@fabric-harness/channels 0.23.0 → 0.25.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/NOTICE +1 -1
- package/README.md +1 -1
- package/dist/buzz-attestation.d.ts +2 -2
- package/dist/buzz-attestation.js +2 -2
- package/dist/buzz-decisions.d.ts +4 -4
- package/dist/buzz-decisions.d.ts.map +1 -1
- package/dist/buzz-decisions.js +3 -3
- package/dist/buzz-decisions.js.map +1 -1
- package/dist/buzz-tail.d.ts +2 -2
- package/dist/buzz-tail.d.ts.map +1 -1
- package/dist/buzz-tail.js +1 -1
- package/dist/buzz-tail.js.map +1 -1
- package/dist/buzz.d.ts +3 -3
- package/dist/buzz.d.ts.map +1 -1
- package/dist/buzz.js +2 -2
- package/dist/github.d.ts +2 -2
- package/dist/github.js +2 -2
- package/dist/slack.d.ts +2 -2
- package/dist/slack.js +2 -2
- package/dist/twilio-voice.d.ts +407 -0
- package/dist/twilio-voice.d.ts.map +1 -0
- package/dist/twilio-voice.js +1005 -0
- package/dist/twilio-voice.js.map +1 -0
- package/package.json +19 -12
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import type { GovernedVoiceSession } from '@fabric-harness/sdk';
|
|
2
|
+
/** `<Parameter>` name that carries the stream token by default. */
|
|
3
|
+
export declare const TWILIO_STREAM_TOKEN_PARAMETER = "fabricStreamToken";
|
|
4
|
+
/**
|
|
5
|
+
* Longest stream token {@link createTwilioStreamToken} mints and
|
|
6
|
+
* {@link verifyTwilioStreamToken} accepts. Twilio requires each `<Parameter>`
|
|
7
|
+
* name plus value to be under 500 characters, so this bound leaves room for a
|
|
8
|
+
* parameter name of up to 39 characters, including the default
|
|
9
|
+
* {@link TWILIO_STREAM_TOKEN_PARAMETER}.
|
|
10
|
+
*/
|
|
11
|
+
export declare const TWILIO_STREAM_TOKEN_MAX_LENGTH = 460;
|
|
12
|
+
export interface TwilioConnectStreamTwimlOptions {
|
|
13
|
+
/** Public `wss://` Media Streams URL. Twilio does not support query strings here; route by path. */
|
|
14
|
+
streamUrl: string;
|
|
15
|
+
/** `<Parameter>` values delivered in the stream's `start.customParameters`. Each name+value must stay under 500 characters. */
|
|
16
|
+
parameters?: Record<string, string>;
|
|
17
|
+
/** Optional HTTPS stream status callback. */
|
|
18
|
+
statusCallback?: string;
|
|
19
|
+
statusCallbackMethod?: 'GET' | 'POST';
|
|
20
|
+
/** Optional `<Say>` played before the stream connects, e.g. an AI or recording disclosure. */
|
|
21
|
+
say?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Build a `<Response><Connect><Stream>` TwiML document. All attribute and text
|
|
25
|
+
* values are XML-escaped; invalid URLs or oversize parameters throw.
|
|
26
|
+
*/
|
|
27
|
+
export declare function twilioConnectStreamTwiml(options: TwilioConnectStreamTwimlOptions): string;
|
|
28
|
+
/** A TwiML response with the XML content type Twilio expects. */
|
|
29
|
+
export declare function twimlResponse(twiml: string, init?: ResponseInit): Response;
|
|
30
|
+
/** HMAC key material: at least 32 bytes. Resolve it from a secret store, never from request data. */
|
|
31
|
+
export type TwilioStreamTokenSecret = string | Uint8Array;
|
|
32
|
+
/** Server-owned call binding carried from the TwiML webhook to the stream. */
|
|
33
|
+
export interface TwilioStreamBinding {
|
|
34
|
+
callSid: string;
|
|
35
|
+
tenantId: string;
|
|
36
|
+
resourceId: string;
|
|
37
|
+
sessionId?: string;
|
|
38
|
+
actorId?: string;
|
|
39
|
+
}
|
|
40
|
+
export interface TwilioStreamTokenClaims extends TwilioStreamBinding {
|
|
41
|
+
/** Expiry, epoch milliseconds (second precision). */
|
|
42
|
+
expiresAt: number;
|
|
43
|
+
/** Random per-token nonce; `claimStream` records it to make the token single use. */
|
|
44
|
+
nonce: string;
|
|
45
|
+
}
|
|
46
|
+
export interface CreateTwilioStreamTokenOptions extends TwilioStreamBinding {
|
|
47
|
+
secret: TwilioStreamTokenSecret;
|
|
48
|
+
/** Expiry, epoch milliseconds. Keep it short: the stream starts seconds after TwiML is served. */
|
|
49
|
+
expiresAt: number;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Mint an HMAC-SHA256 token binding a Twilio `CallSid` to a tenant, resource
|
|
53
|
+
* and optional session/actor. The token is compact enough for a `<Parameter>`:
|
|
54
|
+
* a binding whose token would exceed {@link TWILIO_STREAM_TOKEN_MAX_LENGTH}
|
|
55
|
+
* characters throws instead of minting a token that could not verify.
|
|
56
|
+
*/
|
|
57
|
+
export declare function createTwilioStreamToken(options: CreateTwilioStreamTokenOptions): Promise<string>;
|
|
58
|
+
export interface VerifyTwilioStreamTokenOptions {
|
|
59
|
+
secret: TwilioStreamTokenSecret;
|
|
60
|
+
/** The `CallSid` the token must be bound to (from the verified `start` message). */
|
|
61
|
+
callSid: string;
|
|
62
|
+
/** Current time, epoch milliseconds. Defaults to `Date.now()`. */
|
|
63
|
+
now?: number;
|
|
64
|
+
/** Reject tokens whose remaining lifetime exceeds this bound. Default 15 minutes. */
|
|
65
|
+
maxLifetimeMs?: number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Verify a stream token. Returns the claims, or `undefined` for any malformed,
|
|
69
|
+
* tampered, expired, over-long-lived or wrong-call token. Never throws on
|
|
70
|
+
* attacker-controlled input and never reports why verification failed.
|
|
71
|
+
*/
|
|
72
|
+
export declare function verifyTwilioStreamToken(token: unknown, options: VerifyTwilioStreamTokenOptions): Promise<TwilioStreamTokenClaims | undefined>;
|
|
73
|
+
export interface VerifyTwilioVoiceWebhookOptions {
|
|
74
|
+
authToken: string;
|
|
75
|
+
/** Public webhook URL configured in Twilio. Required when a proxy rewrites the request URL. */
|
|
76
|
+
url?: string;
|
|
77
|
+
/** Expected account. Requests for any other account are rejected. */
|
|
78
|
+
accountSid?: string;
|
|
79
|
+
/** Form body bound. Default 64 KiB. */
|
|
80
|
+
maxBodyBytes?: number;
|
|
81
|
+
}
|
|
82
|
+
export interface TwilioVoiceWebhook {
|
|
83
|
+
callSid: string;
|
|
84
|
+
accountSid: string;
|
|
85
|
+
from?: string;
|
|
86
|
+
to?: string;
|
|
87
|
+
direction?: string;
|
|
88
|
+
/** All signed form parameters. */
|
|
89
|
+
params: URLSearchParams;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Authenticate a Twilio Voice webhook (`application/x-www-form-urlencoded`
|
|
93
|
+
* POST signed with `X-Twilio-Signature`). Returns `undefined` when the
|
|
94
|
+
* signature, method, size, account or `CallSid` is invalid — answer 403.
|
|
95
|
+
*/
|
|
96
|
+
export declare function verifyTwilioVoiceWebhook(request: Request, options: VerifyTwilioVoiceWebhookOptions): Promise<TwilioVoiceWebhook | undefined>;
|
|
97
|
+
export interface VerifyTwilioMediaStreamUpgradeOptions {
|
|
98
|
+
authToken: string;
|
|
99
|
+
/** Exact public `wss://` stream URL from the TwiML. Defaults to the request URL with its scheme mapped to ws/wss. */
|
|
100
|
+
url?: string;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Verify the `X-Twilio-Signature` Twilio sends on the Media Streams WebSocket
|
|
104
|
+
* handshake: HMAC-SHA1 over the exact `wss://` URL with no parameters. Twilio
|
|
105
|
+
* documents that the handshake is signed but not the exact URL canonicalisation,
|
|
106
|
+
* so both the URL and its trailing-slash variant are accepted. Treat this as
|
|
107
|
+
* defence in depth; the stream token remains the mandatory call binding.
|
|
108
|
+
*/
|
|
109
|
+
export declare function verifyTwilioMediaStreamUpgrade(request: Request, options: VerifyTwilioMediaStreamUpgradeOptions): Promise<boolean>;
|
|
110
|
+
/** Extract a routable `CallSid` path segment (for per-call Durable Object routing), or `undefined`. */
|
|
111
|
+
export declare function twilioCallSidFromPath(pathname: string, prefix: string): string | undefined;
|
|
112
|
+
/**
|
|
113
|
+
* Minimal server-side WebSocket surface. Node `ws` sockets and accepted
|
|
114
|
+
* Cloudflare Workers sockets satisfy it directly.
|
|
115
|
+
*/
|
|
116
|
+
export interface TwilioMediaStreamSocket {
|
|
117
|
+
send(data: string): void;
|
|
118
|
+
close(code?: number, reason?: string): void;
|
|
119
|
+
addEventListener(type: 'message', listener: (event: {
|
|
120
|
+
data: unknown;
|
|
121
|
+
}) => void): void;
|
|
122
|
+
addEventListener(type: 'close', listener: (event: unknown) => void): void;
|
|
123
|
+
addEventListener(type: 'error', listener: (event: unknown) => void): void;
|
|
124
|
+
/** Bytes queued but not yet transmitted, when the runtime exposes it. */
|
|
125
|
+
readonly bufferedAmount?: number | undefined;
|
|
126
|
+
}
|
|
127
|
+
export interface TwilioMediaFormat {
|
|
128
|
+
encoding: string;
|
|
129
|
+
sampleRate: number;
|
|
130
|
+
channels: number;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Validated `start` message. `customParameters` never includes the stream
|
|
134
|
+
* token. Parameters are not authenticated: use the verified binding for
|
|
135
|
+
* identity and never read instructions, tools or tenant data from them.
|
|
136
|
+
*/
|
|
137
|
+
export interface TwilioMediaStreamStart {
|
|
138
|
+
streamSid: string;
|
|
139
|
+
callSid: string;
|
|
140
|
+
accountSid: string;
|
|
141
|
+
tracks: string[];
|
|
142
|
+
mediaFormat: TwilioMediaFormat;
|
|
143
|
+
customParameters: Record<string, string>;
|
|
144
|
+
}
|
|
145
|
+
export interface TwilioMediaStreamContext {
|
|
146
|
+
callSid: string;
|
|
147
|
+
streamSid: string;
|
|
148
|
+
binding: TwilioStreamTokenClaims;
|
|
149
|
+
}
|
|
150
|
+
export type TwilioVoiceMetric =
|
|
151
|
+
/** Stream `start` accepted → first assistant audio sent to Twilio (greeting latency). */
|
|
152
|
+
{
|
|
153
|
+
name: 'call_first_audio_ms';
|
|
154
|
+
valueMs: number;
|
|
155
|
+
callSid: string;
|
|
156
|
+
}
|
|
157
|
+
/** Provider end-of-speech (`speech_stopped`) → first assistant audio of the reply. */
|
|
158
|
+
| {
|
|
159
|
+
name: 'response_first_audio_ms';
|
|
160
|
+
valueMs: number;
|
|
161
|
+
callSid: string;
|
|
162
|
+
}
|
|
163
|
+
/** Provider `speech_started` → Twilio `clear` sent (playback stop). */
|
|
164
|
+
| {
|
|
165
|
+
name: 'barge_in_clear_ms';
|
|
166
|
+
valueMs: number;
|
|
167
|
+
callSid: string;
|
|
168
|
+
}
|
|
169
|
+
/** Provider `speech_started` → governed `cancelResponse()` settled. */
|
|
170
|
+
| {
|
|
171
|
+
name: 'barge_in_cancel_ms';
|
|
172
|
+
valueMs: number;
|
|
173
|
+
callSid: string;
|
|
174
|
+
};
|
|
175
|
+
export type TwilioMediaStreamCloseReason = 'stopped' | 'remote_closed' | 'aborted' | 'closed_by_host' | 'voice_ended' | 'start_timeout' | 'idle_timeout' | 'duration_limit' | 'protocol_error' | 'unsupported_media_format' | 'unauthorized' | 'resolve_failed' | 'voice_error' | 'backpressure' | 'message_too_large' | 'callback_error' | 'socket_error';
|
|
176
|
+
export interface TwilioMediaStreamCloseSummary {
|
|
177
|
+
reason: TwilioMediaStreamCloseReason;
|
|
178
|
+
callSid?: string;
|
|
179
|
+
streamSid?: string;
|
|
180
|
+
durationMs: number;
|
|
181
|
+
inboundFrames: number;
|
|
182
|
+
outboundFrames: number;
|
|
183
|
+
}
|
|
184
|
+
export interface TwilioMediaStreamLimits {
|
|
185
|
+
/** Socket open → valid `start`. Default 10 s. */
|
|
186
|
+
startTimeoutMs?: number;
|
|
187
|
+
/** Bound on token verification, `claimStream` and `resolveCall`. Default 10 s. */
|
|
188
|
+
resolveTimeoutMs?: number;
|
|
189
|
+
/** Close when no inbound media arrives for this long. Default 15 s. */
|
|
190
|
+
idleTimeoutMs?: number;
|
|
191
|
+
/** Bridge-level call ceiling, independent of governed voice limits. Default 1 h. */
|
|
192
|
+
maxCallDurationMs?: number;
|
|
193
|
+
/** Largest Twilio text message accepted. Default 64 KiB. */
|
|
194
|
+
maxMessageBytes?: number;
|
|
195
|
+
/** Largest decoded inbound media payload. Default 1600 bytes (200 ms of μ-law). */
|
|
196
|
+
maxInboundFrameBytes?: number;
|
|
197
|
+
/** Inbound frames queued behind governed `sendAudio`; overflow closes the call. Default 250 (~5 s). */
|
|
198
|
+
maxInboundQueueFrames?: number;
|
|
199
|
+
/** Frames buffered while the call is resolving; the oldest are dropped. Default 50 (~1 s). */
|
|
200
|
+
maxPrerollFrames?: number;
|
|
201
|
+
/** Close when the socket reports more buffered outbound bytes than this. Default 2 MiB. */
|
|
202
|
+
maxOutboundBufferedBytes?: number;
|
|
203
|
+
/**
|
|
204
|
+
* Close when assistant audio sent to Twilio but not yet acknowledged as
|
|
205
|
+
* played (by an echoed playback `mark`) exceeds this many milliseconds.
|
|
206
|
+
* Bounds a stalled Twilio consumer on runtimes whose sockets report no
|
|
207
|
+
* `bufferedAmount`, such as Cloudflare Workers. Default 180 s (about
|
|
208
|
+
* 1.4 MiB of μ-law, 2 MiB on the wire).
|
|
209
|
+
*/
|
|
210
|
+
maxUnplayedAudioMs?: number;
|
|
211
|
+
/** Outbound μ-law frame size. Default 160 bytes (20 ms). */
|
|
212
|
+
outboundFrameBytes?: number;
|
|
213
|
+
/**
|
|
214
|
+
* Outstanding playback marks tracked. Default 256. Audio sent while the
|
|
215
|
+
* budget is exhausted is covered by one mark sent at the next echo, so it is
|
|
216
|
+
* still bounded by `maxUnplayedAudioMs` and cleared on barge-in.
|
|
217
|
+
*/
|
|
218
|
+
maxPendingMarks?: number;
|
|
219
|
+
/**
|
|
220
|
+
* After a barge-in `clear`, the bridge sends a fence `mark`; Twilio echoes it
|
|
221
|
+
* once its buffer is emptied, which is the only evidence that the cleared
|
|
222
|
+
* audio left Twilio. Until the echo arrives the cleared audio still counts
|
|
223
|
+
* against `maxUnplayedAudioMs`, and the call closes with `backpressure` when
|
|
224
|
+
* no echo arrives within this bound. Default 5 s.
|
|
225
|
+
*/
|
|
226
|
+
clearAckTimeoutMs?: number;
|
|
227
|
+
/** Caller keypad digits queued behind a running `onDtmf` callback; overflow closes the call with `backpressure`. Default 32. */
|
|
228
|
+
maxPendingDtmf?: number;
|
|
229
|
+
/** Bound on one `onDtmf` callback; a callback that does not settle in time closes the call with `callback_error`. Default 5 s. */
|
|
230
|
+
dtmfTimeoutMs?: number;
|
|
231
|
+
/**
|
|
232
|
+
* Transcripts queued behind a running `onTranscript` callback. Overflow
|
|
233
|
+
* closes the call with `backpressure` (fail closed, like DTMF): a transcript
|
|
234
|
+
* consumer that cannot keep up never silently loses turns. Default 32.
|
|
235
|
+
*/
|
|
236
|
+
maxPendingTranscripts?: number;
|
|
237
|
+
/** Bound on one `onTranscript` callback; a callback that does not settle in time closes the call with `callback_error`. Default 5 s. */
|
|
238
|
+
transcriptTimeoutMs?: number;
|
|
239
|
+
/** Bound on closing the voice session. Default 2 s. */
|
|
240
|
+
closeTimeoutMs?: number;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Voice options the bridge requires of the governed session. Spread them into
|
|
244
|
+
* `session.voice({ ...voiceOptions, ... })` from `resolveCall`: μ-law 8 kHz
|
|
245
|
+
* end to end, plus `speech_stopped` for reply-latency metrics.
|
|
246
|
+
*/
|
|
247
|
+
export interface TwilioBridgeVoiceOptions {
|
|
248
|
+
readonly audioFormat: 'g711_ulaw';
|
|
249
|
+
readonly sampleRate: 8000;
|
|
250
|
+
readonly emitSpeechStopped: true;
|
|
251
|
+
}
|
|
252
|
+
export interface TwilioMediaStreamOptions {
|
|
253
|
+
/** Secret that signed the stream token. */
|
|
254
|
+
tokenSecret: TwilioStreamTokenSecret;
|
|
255
|
+
/** `<Parameter>` name carrying the token. Default {@link TWILIO_STREAM_TOKEN_PARAMETER}. */
|
|
256
|
+
tokenParameter?: string;
|
|
257
|
+
/** `CallSid` the connection was routed for (for example from the URL path). Mismatches fail closed. */
|
|
258
|
+
expectedCallSid?: string;
|
|
259
|
+
/** Expected Twilio account. Mismatches fail closed. */
|
|
260
|
+
accountSid?: string;
|
|
261
|
+
/** Maximum remaining token lifetime accepted. Default 15 minutes. */
|
|
262
|
+
maxTokenLifetimeMs?: number;
|
|
263
|
+
/**
|
|
264
|
+
* Required single-use guard, called once per verified token before
|
|
265
|
+
* `resolveCall`. It must atomically record `binding.nonce` (for example an
|
|
266
|
+
* `INSERT` on a primary key in the per-call Durable Object or database) and
|
|
267
|
+
* return `true` only for the first claim. Anything but `true`, a throw or a
|
|
268
|
+
* timeout rejects the stream, so a replayed token never opens a second
|
|
269
|
+
* stream. The bridge refuses to attach without it.
|
|
270
|
+
*/
|
|
271
|
+
claimStream: (binding: TwilioStreamTokenClaims, start: TwilioMediaStreamStart, signal: AbortSignal) => boolean | Promise<boolean>;
|
|
272
|
+
/**
|
|
273
|
+
* Open the governed voice session for a verified call: load the tenant/case
|
|
274
|
+
* from `binding`, then return `session.voice({ ...voiceOptions, ... })` with
|
|
275
|
+
* server-owned instructions and tools. Throw to reject the call; no audio is
|
|
276
|
+
* forwarded before this resolves.
|
|
277
|
+
*/
|
|
278
|
+
resolveCall: (input: {
|
|
279
|
+
start: TwilioMediaStreamStart;
|
|
280
|
+
binding: TwilioStreamTokenClaims;
|
|
281
|
+
signal: AbortSignal;
|
|
282
|
+
voiceOptions: TwilioBridgeVoiceOptions;
|
|
283
|
+
}) => Promise<GovernedVoiceSession>;
|
|
284
|
+
/**
|
|
285
|
+
* Caller keypad input. Never forwarded to the model by the bridge. Digits
|
|
286
|
+
* are delivered one at a time in arrival order; at most `maxPendingDtmf`
|
|
287
|
+
* wait behind a running callback, each callback is bounded by
|
|
288
|
+
* `dtmfTimeoutMs`, and `signal` aborts when the bridge closes. Digits still
|
|
289
|
+
* queued at close are dropped.
|
|
290
|
+
*/
|
|
291
|
+
onDtmf?: (digit: string, context: TwilioMediaStreamContext, signal: AbortSignal) => void | Promise<void>;
|
|
292
|
+
/**
|
|
293
|
+
* Final transcripts, already redacted by governed voice. Delivered outside
|
|
294
|
+
* the voice event loop, so a slow callback never delays barge-in (`clear`
|
|
295
|
+
* and cancellation) or assistant audio. Transcripts are delivered one at a
|
|
296
|
+
* time in arrival order; at most `maxPendingTranscripts` wait behind a
|
|
297
|
+
* running callback (overflow closes with `backpressure`), each callback is
|
|
298
|
+
* bounded by `transcriptTimeoutMs` (then `callback_error`), and `signal`
|
|
299
|
+
* aborts when the bridge closes. Transcripts still queued at close are dropped.
|
|
300
|
+
*/
|
|
301
|
+
onTranscript?: (event: {
|
|
302
|
+
role: 'assistant' | 'user';
|
|
303
|
+
text: string;
|
|
304
|
+
}, context: TwilioMediaStreamContext, signal: AbortSignal) => void | Promise<void>;
|
|
305
|
+
/** Latency metrics. Must not throw. */
|
|
306
|
+
onMetric?: (metric: TwilioVoiceMetric) => void;
|
|
307
|
+
/** Called once when the bridge closes, with a sanitized summary. */
|
|
308
|
+
onClose?: (summary: TwilioMediaStreamCloseSummary) => void;
|
|
309
|
+
signal?: AbortSignal;
|
|
310
|
+
limits?: TwilioMediaStreamLimits;
|
|
311
|
+
/** Clock for latency metrics and timers. Defaults to `Date.now`. */
|
|
312
|
+
now?: () => number;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Bridges one Twilio Media Streams WebSocket to one governed voice session.
|
|
316
|
+
* Create it with {@link attachTwilioMediaStream}.
|
|
317
|
+
*/
|
|
318
|
+
export declare class TwilioMediaStreamBridge {
|
|
319
|
+
private readonly socket;
|
|
320
|
+
private readonly options;
|
|
321
|
+
/** Resolves once with the close summary. */
|
|
322
|
+
readonly closed: Promise<TwilioMediaStreamCloseSummary>;
|
|
323
|
+
private resolveClosed;
|
|
324
|
+
private readonly limits;
|
|
325
|
+
private readonly now;
|
|
326
|
+
private readonly abort;
|
|
327
|
+
private readonly startedAt;
|
|
328
|
+
private state;
|
|
329
|
+
private closing;
|
|
330
|
+
private voice;
|
|
331
|
+
private context;
|
|
332
|
+
private callSid;
|
|
333
|
+
private streamSid;
|
|
334
|
+
private startTimer;
|
|
335
|
+
private idleTimer;
|
|
336
|
+
private durationTimer;
|
|
337
|
+
private readonly inbound;
|
|
338
|
+
private pumping;
|
|
339
|
+
private inboundFrames;
|
|
340
|
+
private outboundFrames;
|
|
341
|
+
private streamAcceptedAt;
|
|
342
|
+
private sentAnyAudio;
|
|
343
|
+
private responseSeq;
|
|
344
|
+
private responseActive;
|
|
345
|
+
private responseSentBytes;
|
|
346
|
+
private responsePlayedMs;
|
|
347
|
+
private dropAssistantAudio;
|
|
348
|
+
private speechStoppedAt;
|
|
349
|
+
/**
|
|
350
|
+
* Outstanding playback marks → the cumulative assistant audio bytes and the
|
|
351
|
+
* response-relative milliseconds each acknowledges. Mark names carry a
|
|
352
|
+
* per-call monotonic counter so no two outstanding marks can share a name.
|
|
353
|
+
*/
|
|
354
|
+
private readonly pendingMarks;
|
|
355
|
+
private markCounter;
|
|
356
|
+
/** Assistant audio bytes sent to Twilio, and those Twilio acknowledged as played. */
|
|
357
|
+
private outboundAudioBytes;
|
|
358
|
+
private playedAudioBytes;
|
|
359
|
+
/**
|
|
360
|
+
* Assistant audio bytes covered by the latest mark sent. When the mark budget
|
|
361
|
+
* is exhausted, audio past this point waits for a slot: the next echo sends
|
|
362
|
+
* one mark covering the whole untracked tail.
|
|
363
|
+
*/
|
|
364
|
+
private markedAudioBytes;
|
|
365
|
+
/**
|
|
366
|
+
* Assistant audio bytes a `clear` discarded from Twilio's buffer. They no
|
|
367
|
+
* longer count as playing, but stay unacknowledged (against the unplayed
|
|
368
|
+
* budget) until the fence mark sent after the clear is echoed.
|
|
369
|
+
*/
|
|
370
|
+
private clearedAudioBytes;
|
|
371
|
+
/** Acknowledgement the latest fence mark stands for, and its deadline. */
|
|
372
|
+
private clearFenceBytes;
|
|
373
|
+
private clearAckTimer;
|
|
374
|
+
/** Caller DTMF digits, delivered to one `onDtmf` callback at a time. */
|
|
375
|
+
private readonly dtmf;
|
|
376
|
+
/** Final transcripts, delivered to one `onTranscript` callback at a time, off the voice event loop. */
|
|
377
|
+
private readonly transcripts;
|
|
378
|
+
private readonly onAbort;
|
|
379
|
+
constructor(socket: TwilioMediaStreamSocket, options: TwilioMediaStreamOptions);
|
|
380
|
+
/** Close the call's stream and voice session. Idempotent. */
|
|
381
|
+
close(reason?: TwilioMediaStreamCloseReason): Promise<TwilioMediaStreamCloseSummary>;
|
|
382
|
+
private onMessage;
|
|
383
|
+
private handleStart;
|
|
384
|
+
private handleMedia;
|
|
385
|
+
private pumpInbound;
|
|
386
|
+
private handleMark;
|
|
387
|
+
private handleDtmf;
|
|
388
|
+
private pumpVoice;
|
|
389
|
+
private handleVoiceEvent;
|
|
390
|
+
private sendAssistantAudio;
|
|
391
|
+
/**
|
|
392
|
+
* Send a playback mark covering all assistant audio sent so far, when some
|
|
393
|
+
* is not yet covered and the mark budget allows. Twilio echoes a mark once
|
|
394
|
+
* playback reaches it, so a mark sent now covers every earlier byte.
|
|
395
|
+
*/
|
|
396
|
+
private markPlayback;
|
|
397
|
+
private bargeIn;
|
|
398
|
+
private send;
|
|
399
|
+
private resetIdle;
|
|
400
|
+
private metric;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Attach a governed voice bridge to an accepted Twilio Media Streams socket.
|
|
404
|
+
* Works with Node `ws` sockets and Cloudflare Workers sockets alike.
|
|
405
|
+
*/
|
|
406
|
+
export declare function attachTwilioMediaStream(socket: TwilioMediaStreamSocket, options: TwilioMediaStreamOptions): TwilioMediaStreamBridge;
|
|
407
|
+
//# sourceMappingURL=twilio-voice.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"twilio-voice.d.ts","sourceRoot":"","sources":["../src/twilio-voice.ts"],"names":[],"mappings":"AAwBA,OAAO,KAAK,EAAE,oBAAoB,EAAiC,MAAM,qBAAqB,CAAC;AAG/F,mEAAmE;AACnE,eAAO,MAAM,6BAA6B,sBAAsB,CAAC;AAEjE;;;;;;GAMG;AACH,eAAO,MAAM,8BAA8B,MAAM,CAAC;AAclD,MAAM,WAAW,+BAA+B;IAC9C,oGAAoG;IACpG,SAAS,EAAE,MAAM,CAAC;IAClB,+HAA+H;IAC/H,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpC,6CAA6C;IAC7C,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