@craftedxp/voice-js 0.4.2 → 0.5.4
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/CONSUMING.md +10 -2
- package/README.md +69 -11
- package/dist/browser.d.mts +178 -1
- package/dist/browser.d.ts +535 -259
- package/dist/browser.js +924 -710
- package/dist/browser.js.map +1 -1
- package/dist/browser.mjs +209 -1
- package/dist/browser.mjs.map +1 -1
- package/dist/embed.iife.js +23373 -42
- package/dist/node.d.mts +177 -1
- package/dist/node.d.ts +516 -256
- package/dist/node.js +471 -432
- package/dist/node.js.map +1 -1
- package/dist/node.mjs +25 -1
- package/dist/node.mjs.map +1 -1
- package/package.json +4 -1
package/dist/browser.d.ts
CHANGED
|
@@ -1,103 +1,291 @@
|
|
|
1
|
+
import { RemoteTrack, LocalVideoTrack } from 'livekit-client'
|
|
2
|
+
|
|
1
3
|
interface ClientTool {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
description: string
|
|
5
|
+
parameters: Record<string, unknown>
|
|
6
|
+
usage?: string
|
|
7
|
+
timeoutMs?: number
|
|
8
|
+
example?: string
|
|
9
|
+
handler: (args: Record<string, unknown>) => Promise<string | object> | string | object
|
|
8
10
|
}
|
|
9
|
-
type ClientToolMap = Record<string, ClientTool
|
|
11
|
+
type ClientToolMap = Record<string, ClientTool>
|
|
10
12
|
interface ClientToolCallFrame {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
toolCallId: string
|
|
14
|
+
name: string
|
|
15
|
+
args: Record<string, unknown>
|
|
14
16
|
}
|
|
15
17
|
|
|
16
|
-
type CallState =
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
18
|
+
type CallState =
|
|
19
|
+
| 'idle'
|
|
20
|
+
| 'connecting'
|
|
21
|
+
| 'listening'
|
|
22
|
+
| 'user_speaking'
|
|
23
|
+
| 'agent_speaking'
|
|
24
|
+
| 'ended'
|
|
25
|
+
| 'error'
|
|
26
|
+
type TranscriptEntry =
|
|
27
|
+
| {
|
|
28
|
+
id: string
|
|
29
|
+
role: 'user'
|
|
30
|
+
text: string
|
|
31
|
+
committed: boolean
|
|
32
|
+
}
|
|
33
|
+
| {
|
|
34
|
+
id: string
|
|
35
|
+
role: 'agent'
|
|
36
|
+
text: string
|
|
37
|
+
interrupted?: boolean
|
|
38
|
+
}
|
|
39
|
+
| {
|
|
40
|
+
id: string
|
|
41
|
+
role: 'tool'
|
|
42
|
+
text: string
|
|
43
|
+
}
|
|
44
|
+
| {
|
|
45
|
+
id: string
|
|
46
|
+
role: 'system'
|
|
47
|
+
text: string
|
|
48
|
+
}
|
|
49
|
+
type CallErrorCode =
|
|
50
|
+
| 'missing_credentials'
|
|
51
|
+
| 'forbidden'
|
|
52
|
+
| 'mic_denied'
|
|
53
|
+
| 'mic_start_failed'
|
|
54
|
+
| 'audio_session_failed'
|
|
55
|
+
| 'token_expired'
|
|
56
|
+
| 'token_invalid'
|
|
57
|
+
| 'unauthorized'
|
|
58
|
+
| 'network_unreachable'
|
|
59
|
+
| 'socket_error'
|
|
60
|
+
| 'payment_required'
|
|
61
|
+
| 'not_found'
|
|
62
|
+
| 'silence_timeout'
|
|
63
|
+
| 'server_error'
|
|
37
64
|
interface CallError {
|
|
38
|
-
|
|
39
|
-
|
|
65
|
+
code: CallErrorCode
|
|
66
|
+
message: string
|
|
40
67
|
}
|
|
41
|
-
type CallEndReason = 'agent_ended' | 'user_hangup' | 'timeout' | 'error'
|
|
68
|
+
type CallEndReason = 'agent_ended' | 'user_hangup' | 'timeout' | 'error'
|
|
42
69
|
interface CallEndEvent {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
70
|
+
reason: CallEndReason
|
|
71
|
+
errorCode?: CallErrorCode
|
|
72
|
+
durationMs: number
|
|
46
73
|
}
|
|
47
74
|
interface VolumeEvent {
|
|
48
|
-
|
|
49
|
-
|
|
75
|
+
input: number
|
|
76
|
+
output: number
|
|
50
77
|
}
|
|
51
78
|
type ServerMessage = Record<string, unknown> & {
|
|
52
|
-
|
|
53
|
-
}
|
|
79
|
+
type?: string
|
|
80
|
+
}
|
|
54
81
|
interface ProtocolState {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
82
|
+
state: CallState
|
|
83
|
+
transcript: TranscriptEntry[]
|
|
84
|
+
agentBubbleId: string | null
|
|
85
|
+
idCounter: number
|
|
86
|
+
endReason: CallEndReason | null
|
|
60
87
|
}
|
|
61
|
-
declare const createProtocolState: () => ProtocolState
|
|
88
|
+
declare const createProtocolState: () => ProtocolState
|
|
62
89
|
interface ProtocolCallbacks {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
90
|
+
onState: (next: CallState) => void
|
|
91
|
+
onTranscript: (entries: TranscriptEntry[]) => void
|
|
92
|
+
onError: (err: CallError) => void
|
|
93
|
+
onInterrupt: () => void
|
|
94
|
+
onAgentTurnStart: (seq?: number) => void
|
|
95
|
+
onAgentTurnEnd: (seq?: number) => void
|
|
96
|
+
onCallEnd: (reason: CallEndReason) => void
|
|
97
|
+
onConnected: () => void
|
|
98
|
+
onClientToolCall: (frame: ClientToolCallFrame) => void
|
|
72
99
|
}
|
|
73
|
-
declare function handleServerMessage(raw: string, state: ProtocolState, cb: ProtocolCallbacks): void
|
|
100
|
+
declare function handleServerMessage(raw: string, state: ProtocolState, cb: ProtocolCallbacks): void
|
|
74
101
|
interface BuildWsUrlArgs {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
102
|
+
apiBase: string
|
|
103
|
+
agentId: string
|
|
104
|
+
token: string
|
|
105
|
+
bargeIn?: boolean
|
|
106
|
+
}
|
|
107
|
+
declare function buildWsUrl(args: BuildWsUrlArgs): string
|
|
108
|
+
|
|
109
|
+
type SystemMessage =
|
|
110
|
+
| {
|
|
111
|
+
kind: 'room.starting'
|
|
112
|
+
at: string
|
|
113
|
+
}
|
|
114
|
+
| {
|
|
115
|
+
kind: 'room.ending.soon'
|
|
116
|
+
minutesRemaining: 5 | 1
|
|
117
|
+
}
|
|
118
|
+
| {
|
|
119
|
+
kind: 'room.ended'
|
|
120
|
+
reason: 'duration_reached' | 'manual' | 'empty'
|
|
121
|
+
}
|
|
122
|
+
| {
|
|
123
|
+
kind: 'role.promoted'
|
|
124
|
+
participantId: string
|
|
125
|
+
name: string
|
|
126
|
+
}
|
|
127
|
+
| {
|
|
128
|
+
kind: 'role.demoted'
|
|
129
|
+
participantId: string
|
|
130
|
+
name: string
|
|
131
|
+
}
|
|
132
|
+
| {
|
|
133
|
+
kind: 'participant.removed'
|
|
134
|
+
participantId: string
|
|
135
|
+
name: string
|
|
136
|
+
byHost?: string
|
|
137
|
+
}
|
|
138
|
+
| {
|
|
139
|
+
kind: 'notetaker.connected'
|
|
140
|
+
}
|
|
141
|
+
| {
|
|
142
|
+
kind: 'notetaker.disconnected'
|
|
143
|
+
}
|
|
144
|
+
| {
|
|
145
|
+
kind: 'notetaker.partial_degraded'
|
|
146
|
+
participantId: string
|
|
147
|
+
}
|
|
148
|
+
type TranscriptMessage = {
|
|
149
|
+
kind: 'partial'
|
|
150
|
+
participantId: string
|
|
151
|
+
speakerName: string
|
|
152
|
+
text: string
|
|
153
|
+
startedAt: string
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
interface JoinRoomOptions {
|
|
157
|
+
/** Full HTTPS URL of the Voissia server. Same shape as VoiceClientConfig.apiBase. */
|
|
158
|
+
apiBase: string
|
|
159
|
+
/** Server-generated room id (`rm_…`). */
|
|
160
|
+
roomId: string
|
|
161
|
+
/** Shared room join token from the invite link. Mints a fresh participant each call. */
|
|
162
|
+
joinCode: string
|
|
163
|
+
/** Display name the joiner registers under for this participant. */
|
|
164
|
+
name: string
|
|
165
|
+
}
|
|
166
|
+
interface RoomParticipantInfo {
|
|
167
|
+
/** Stable participant id (`p_…`); strips any `guest:` LiveKit identity prefix. */
|
|
168
|
+
participantId: string
|
|
169
|
+
/** Display name as the worker registered it; may be empty. */
|
|
170
|
+
name: string
|
|
171
|
+
}
|
|
172
|
+
type RoomTrackKind = 'audio' | 'video'
|
|
173
|
+
/** What a track is — lets consumers tell a camera apart from a screen share
|
|
174
|
+
* (a participant can publish both at once). Mirrors livekit `Track.Source`. */
|
|
175
|
+
type RoomTrackSource = 'camera' | 'microphone' | 'screen_share' | 'screen_share_audio' | 'unknown'
|
|
176
|
+
interface RoomTrackEvent {
|
|
177
|
+
/** Stable participant id (`p_…`); `guest:` prefix stripped. */
|
|
178
|
+
participantId: string
|
|
179
|
+
kind: RoomTrackKind
|
|
180
|
+
/** Distinguishes camera vs screen_share so each can render as its own tile. */
|
|
181
|
+
source: RoomTrackSource
|
|
182
|
+
/** livekit-client track — call `.attach(el)` / `.detach()` to render. */
|
|
183
|
+
track: RemoteTrack
|
|
184
|
+
}
|
|
185
|
+
type RoomEventName =
|
|
186
|
+
| 'participant.joined'
|
|
187
|
+
| 'participant.left'
|
|
188
|
+
| 'transcript.partial'
|
|
189
|
+
| 'transcript.final'
|
|
190
|
+
| 'system.message'
|
|
191
|
+
| 'room.ended'
|
|
192
|
+
| 'track.subscribed'
|
|
193
|
+
| 'track.unsubscribed'
|
|
194
|
+
| 'active.speakers'
|
|
195
|
+
interface RoomEventPayloads {
|
|
196
|
+
'participant.joined': RoomParticipantInfo
|
|
197
|
+
'participant.left': RoomParticipantInfo
|
|
198
|
+
'transcript.partial': TranscriptMessage
|
|
199
|
+
/**
|
|
200
|
+
* Reserved — the worker currently emits only partials over the transcript
|
|
201
|
+
* topic. Final-utterance events will land in Phase 8 once the worker
|
|
202
|
+
* publishes a `final` kind; the SDK keeps the slot reserved so consumers
|
|
203
|
+
* can register handlers today.
|
|
204
|
+
*/
|
|
205
|
+
'transcript.final': {
|
|
206
|
+
participantId: string
|
|
207
|
+
speakerName: string
|
|
208
|
+
text: string
|
|
209
|
+
startedAt: string
|
|
210
|
+
}
|
|
211
|
+
'system.message': SystemMessage
|
|
212
|
+
'room.ended': undefined
|
|
213
|
+
'track.subscribed': RoomTrackEvent
|
|
214
|
+
'track.unsubscribed': RoomTrackEvent
|
|
215
|
+
/** participantIds currently speaking (drives an active-speaker UI). */
|
|
216
|
+
'active.speakers': string[]
|
|
79
217
|
}
|
|
80
|
-
|
|
218
|
+
type Handler<E extends RoomEventName> = (payload: RoomEventPayloads[E]) => void
|
|
219
|
+
interface RoomSession {
|
|
220
|
+
/** This session's own stable participant id (`p_…`). Useful to filter
|
|
221
|
+
* yourself out of `active.speakers`, which includes the local participant. */
|
|
222
|
+
readonly participantId: string
|
|
223
|
+
/** Snapshot of the remote participants currently connected. */
|
|
224
|
+
readonly participants: RoomParticipantInfo[]
|
|
225
|
+
/** Subscribe to a typed event. No unsubscribe surface yet (mirrors `Call.onX`). */
|
|
226
|
+
on<E extends RoomEventName>(event: E, handler: Handler<E>): void
|
|
227
|
+
/** Publish the local mic track. Resolves once the track is live on LiveKit. */
|
|
228
|
+
publishMic(): Promise<void>
|
|
229
|
+
/** Publish the local camera track. */
|
|
230
|
+
publishCamera(): Promise<void>
|
|
231
|
+
/** Mid-call mute/unmute of the local mic. */
|
|
232
|
+
setMicEnabled(on: boolean): Promise<void>
|
|
233
|
+
/** Mid-call camera on/off. */
|
|
234
|
+
setCameraEnabled(on: boolean): Promise<void>
|
|
235
|
+
/** Current local mic state (for toggle UI). */
|
|
236
|
+
isMicEnabled(): boolean
|
|
237
|
+
/** Current local camera state (for toggle UI). */
|
|
238
|
+
isCameraEnabled(): boolean
|
|
239
|
+
/** The local camera track for self-view, or null before publishCamera resolves. */
|
|
240
|
+
getLocalCameraTrack(): LocalVideoTrack | null
|
|
241
|
+
/**
|
|
242
|
+
* Remote tracks already subscribed at this moment. A late joiner misses the
|
|
243
|
+
* live `track.subscribed` events for tracks published before it connected
|
|
244
|
+
* (LiveKit delivers them during `connect`, before consumer listeners attach).
|
|
245
|
+
* Call this right after registering `track.subscribed` to backfill them.
|
|
246
|
+
*/
|
|
247
|
+
getRemoteTracks(): RoomTrackEvent[]
|
|
248
|
+
/**
|
|
249
|
+
* Start/stop sharing the screen (via `getDisplayMedia`). Pass `{ audio: true }`
|
|
250
|
+
* to also capture shared/system audio where the browser allows it (Chrome:
|
|
251
|
+
* tab or system audio; macOS Chrome is tab-audio only; Safari/Firefox don't
|
|
252
|
+
* capture share audio). Publishes a `screen_share` video track (+ optional
|
|
253
|
+
* `screen_share_audio`); remote peers receive them via `track.subscribed`.
|
|
254
|
+
*/
|
|
255
|
+
setScreenShareEnabled(
|
|
256
|
+
on: boolean,
|
|
257
|
+
opts?: {
|
|
258
|
+
audio?: boolean
|
|
259
|
+
},
|
|
260
|
+
): Promise<void>
|
|
261
|
+
/** Current local screen-share state (for toggle UI). */
|
|
262
|
+
isScreenShareEnabled(): boolean
|
|
263
|
+
/** The local screen-share video track for self-preview, or null when off. */
|
|
264
|
+
getLocalScreenTrack(): LocalVideoTrack | null
|
|
265
|
+
/** Disconnect from LiveKit. Idempotent. Triggers `room.ended` via Disconnected. */
|
|
266
|
+
leave(): Promise<void>
|
|
267
|
+
}
|
|
268
|
+
declare const joinRoom: (opts: JoinRoomOptions) => Promise<RoomSession>
|
|
81
269
|
|
|
82
270
|
interface FetchTokenArgs {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
271
|
+
/** The agent the SDK is about to call. */
|
|
272
|
+
agentId: string
|
|
273
|
+
/**
|
|
274
|
+
* Optional consumer-side user identifier. Round-tripped to the server
|
|
275
|
+
* as `contactId` for Phase 11 contact memory. The SDK does not
|
|
276
|
+
* inspect this; your backend uses it to scope the token mint.
|
|
277
|
+
*/
|
|
278
|
+
userId?: string
|
|
279
|
+
/**
|
|
280
|
+
* Per-call structured context lowered into the agent's effective
|
|
281
|
+
* system prompt server-side at session open. Opaque to the SDK.
|
|
282
|
+
*/
|
|
283
|
+
context?: Record<string, unknown>
|
|
284
|
+
/**
|
|
285
|
+
* String key/value pairs round-tripped on the `call.ended` webhook.
|
|
286
|
+
* Capped at 1 KB total server-side. NOT lowered into the system prompt.
|
|
287
|
+
*/
|
|
288
|
+
metadata?: Record<string, string>
|
|
101
289
|
}
|
|
102
290
|
/**
|
|
103
291
|
* What `fetchToken` may return. The rich object form lets the server
|
|
@@ -105,207 +293,245 @@ interface FetchTokenArgs {
|
|
|
105
293
|
* compatible — the SDK treats it as `{ token, transport: 'ws' }`.
|
|
106
294
|
*/
|
|
107
295
|
interface FetchTokenResult {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
296
|
+
/** Raw `ct_` to feed into the WS open / WebRTC offer. */
|
|
297
|
+
token: string
|
|
298
|
+
/** Server-selected transport. Default `'ws'` if absent. */
|
|
299
|
+
transport?: 'ws' | 'webrtc'
|
|
300
|
+
/** Required when `transport === 'webrtc'` AND the server uses a
|
|
301
|
+
* separate signaling gateway. When omitted on a webrtc result, the
|
|
302
|
+
* SDK falls back to the API base's Phase-1 routes (local dev). */
|
|
303
|
+
webrtcGatewayBase?: string
|
|
116
304
|
}
|
|
117
|
-
type FetchToken = (args: FetchTokenArgs) => Promise<string | FetchTokenResult
|
|
305
|
+
type FetchToken = (args: FetchTokenArgs) => Promise<string | FetchTokenResult>
|
|
118
306
|
interface VoiceClientConfig {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
307
|
+
/**
|
|
308
|
+
* Full HTTPS URL of the Voissia server. The WebSocket scheme is
|
|
309
|
+
* derived: `https` → `wss`, `http` → `ws`. No trailing slash needed.
|
|
310
|
+
*/
|
|
311
|
+
apiBase: string
|
|
312
|
+
/**
|
|
313
|
+
* Called by the SDK whenever it needs a fresh `ct_` token (initial
|
|
314
|
+
* connect; mid-call refresh on `token_expired`). Your implementation
|
|
315
|
+
* should hit YOUR backend, which holds the `sk_` API key and mints
|
|
316
|
+
* via `POST /v1/call-tokens` (or `client.callTokens.mint` from
|
|
317
|
+
* @craftedxp/sdk-node). Never embed `sk_` in JS code that ships to a
|
|
318
|
+
* client.
|
|
319
|
+
*/
|
|
320
|
+
fetchToken: FetchToken
|
|
321
|
+
/**
|
|
322
|
+
* Optional metadata applied to EVERY startCall. Per-call `metadata`
|
|
323
|
+
* in `startCall` is merged on top (per-call wins on key conflicts).
|
|
324
|
+
* Useful for dashboard-wide tags like `{ surface: 'web', appVersion }`.
|
|
325
|
+
*/
|
|
326
|
+
defaultMetadata?: Record<string, string>
|
|
327
|
+
/**
|
|
328
|
+
* Optional context applied to EVERY startCall. Per-call `context` in
|
|
329
|
+
* `startCall` is merged on top. Useful for cross-call invariants like
|
|
330
|
+
* the signed-in user's locale.
|
|
331
|
+
*/
|
|
332
|
+
defaultContext?: Record<string, unknown>
|
|
145
333
|
}
|
|
146
334
|
interface StartCallOptions {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
335
|
+
/** The agent to call. */
|
|
336
|
+
agentId: string
|
|
337
|
+
/** Per-call user identifier. Round-tripped to fetchToken as `userId`. */
|
|
338
|
+
userId?: string
|
|
339
|
+
/**
|
|
340
|
+
* Per-call structured context. Merged on top of `defaultContext`
|
|
341
|
+
* configured at factory time.
|
|
342
|
+
*/
|
|
343
|
+
context?: Record<string, unknown>
|
|
344
|
+
/**
|
|
345
|
+
* Per-call metadata. Merged on top of `defaultMetadata` configured
|
|
346
|
+
* at factory time.
|
|
347
|
+
*/
|
|
348
|
+
metadata?: Record<string, string>
|
|
349
|
+
/**
|
|
350
|
+
* When false, the SDK + server stay full-duplex but barge-in is
|
|
351
|
+
* suppressed. Useful for alarm-style flows where the user shouldn't
|
|
352
|
+
* accidentally interrupt the script. Default true.
|
|
353
|
+
*/
|
|
354
|
+
bargeIn?: boolean
|
|
355
|
+
/**
|
|
356
|
+
* Client-side tools the agent's LLM can call mid-conversation. Each
|
|
357
|
+
* tool's handler runs on the consumer's side; result is fed back to
|
|
358
|
+
* the LLM through the existing call WebSocket. Schema and handler
|
|
359
|
+
* colocate. Validated synchronously at startCall — bad input throws.
|
|
360
|
+
*
|
|
361
|
+
* See docs/integration-echocheck.md for the wire protocol and the
|
|
362
|
+
* server-side guarantees.
|
|
363
|
+
*/
|
|
364
|
+
clientTools?: ClientToolMap
|
|
365
|
+
/**
|
|
366
|
+
* Test-only escape hatch — pass a pre-minted `ct_` directly and skip
|
|
367
|
+
* the `fetchToken` call. Don't use this in production code: tokens
|
|
368
|
+
* expire and the SDK can't re-mint without the callback.
|
|
369
|
+
*/
|
|
370
|
+
token?: string
|
|
371
|
+
onStateChange?: (state: CallState) => void
|
|
372
|
+
onTranscript?: (entries: TranscriptEntry[]) => void
|
|
373
|
+
onError?: (err: CallError) => void
|
|
374
|
+
onEnd?: (end: CallEndEvent) => void
|
|
375
|
+
/** Volume-meter event for VU UIs. ~10 Hz cadence (browser bundle only). */
|
|
376
|
+
onVolume?: (vol: VolumeEvent) => void
|
|
377
|
+
/**
|
|
378
|
+
* Fires when the server signals barge-in (the user started talking
|
|
379
|
+
* mid-agent-turn). The browser bundle automatically flushes its
|
|
380
|
+
* built-in audio playback before this callback runs; the callback is
|
|
381
|
+
* fired regardless. Node / Electron consumers with custom playback
|
|
382
|
+
* should drain their audio queue here so the agent goes silent
|
|
383
|
+
* immediately.
|
|
384
|
+
*/
|
|
385
|
+
onInterrupt?: () => void
|
|
386
|
+
/**
|
|
387
|
+
* Fires on `agent_turn_start` — the server has begun a new agent
|
|
388
|
+
* turn. The state-machine transition to `agent_speaking` happens at
|
|
389
|
+
* the same moment via `onStateChange`; use this when you want a
|
|
390
|
+
* precise turn anchor (e.g. "agent has been speaking for N ms" UIs)
|
|
391
|
+
* without diffing state.
|
|
392
|
+
*/
|
|
393
|
+
onAgentTurnStart?: () => void
|
|
206
394
|
}
|
|
207
395
|
interface Call {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
396
|
+
/** Current state. Snapshot — subscribe via onStateChange for live updates. */
|
|
397
|
+
readonly state: CallState
|
|
398
|
+
/** Full transcript so far. Snapshot — subscribe via onTranscript for live updates. */
|
|
399
|
+
readonly transcript: TranscriptEntry[]
|
|
400
|
+
/** True after `mute()` and before `unmute()`. */
|
|
401
|
+
readonly isMuted: boolean
|
|
402
|
+
/** End the call locally. Closes the WS, stops the mic, fires onEnd. Idempotent. */
|
|
403
|
+
end: () => void
|
|
404
|
+
/** Mute mic frames. Wire stays active so server endpointing doesn't false-positive. Idempotent. */
|
|
405
|
+
mute: () => void
|
|
406
|
+
/** Unmute mic frames. Idempotent. */
|
|
407
|
+
unmute: () => void
|
|
220
408
|
}
|
|
221
409
|
interface VoiceClientFactory {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
410
|
+
/** Read back the resolved config (post trailing-slash normalisation). */
|
|
411
|
+
readonly config: VoiceClientConfig
|
|
412
|
+
/**
|
|
413
|
+
* Open a fresh call. Returns when the WS is open; rejects on
|
|
414
|
+
* pre-flight failure (missing config, fetchToken throw, etc). Mid-
|
|
415
|
+
* call failures arrive via the per-call `onError` callback — they
|
|
416
|
+
* don't reject this promise.
|
|
417
|
+
*/
|
|
418
|
+
startCall: (options: StartCallOptions) => Promise<Call>
|
|
419
|
+
/**
|
|
420
|
+
* Phase 7 (multi-party rooms). Browser only. Exchange a single-use
|
|
421
|
+
* joinCode for a LiveKit JWT and connect to the room. The returned
|
|
422
|
+
* `RoomSession` exposes a typed event surface
|
|
423
|
+
* (participant.joined / participant.left / transcript.partial /
|
|
424
|
+
* transcript.final / system.message / room.ended) plus
|
|
425
|
+
* publishMic / publishCamera / leave. The Node bundle does NOT
|
|
426
|
+
* implement this — livekit-client is a browser-only WebRTC client.
|
|
427
|
+
*/
|
|
428
|
+
joinRoom?: (options: Omit<JoinRoomOptions, 'apiBase'>) => Promise<RoomSession>
|
|
231
429
|
}
|
|
232
430
|
|
|
233
|
-
type OnChunk = (pcm: ArrayBuffer) => void
|
|
234
|
-
type OnVolume$1 = (rms01: number) => void
|
|
235
|
-
type OnError = (err: Error) => void
|
|
431
|
+
type OnChunk = (pcm: ArrayBuffer) => void
|
|
432
|
+
type OnVolume$1 = (rms01: number) => void
|
|
433
|
+
type OnError = (err: Error) => void
|
|
236
434
|
interface CaptureOptions {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
435
|
+
onChunk: OnChunk
|
|
436
|
+
onVolume?: OnVolume$1
|
|
437
|
+
onError?: OnError
|
|
240
438
|
}
|
|
241
439
|
interface CaptureController {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
440
|
+
start: () => Promise<void>
|
|
441
|
+
stop: () => void
|
|
442
|
+
mute: (muted: boolean) => void
|
|
443
|
+
isCapturing: () => boolean
|
|
246
444
|
}
|
|
247
|
-
declare const createAudioCapture: (options: CaptureOptions) => CaptureController
|
|
445
|
+
declare const createAudioCapture: (options: CaptureOptions) => CaptureController
|
|
248
446
|
|
|
249
|
-
type OnVolume = (rms01: number) => void
|
|
250
|
-
type OnAgentSpeakingChange = (speaking: boolean) => void
|
|
447
|
+
type OnVolume = (rms01: number) => void
|
|
448
|
+
type OnAgentSpeakingChange = (speaking: boolean) => void
|
|
251
449
|
interface PlaybackOptions {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
450
|
+
sampleRate?: number
|
|
451
|
+
onVolume?: OnVolume
|
|
452
|
+
onSpeakingChange?: OnAgentSpeakingChange
|
|
255
453
|
}
|
|
256
454
|
interface PlaybackController {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
455
|
+
enqueue: (pcm: ArrayBuffer) => void
|
|
456
|
+
flush: () => void
|
|
457
|
+
close: () => void
|
|
458
|
+
resume: () => Promise<void>
|
|
261
459
|
}
|
|
262
|
-
declare const createAudioPlayback: (options?: PlaybackOptions) => PlaybackController
|
|
460
|
+
declare const createAudioPlayback: (options?: PlaybackOptions) => PlaybackController
|
|
263
461
|
|
|
264
|
-
type RWSEvent =
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
462
|
+
type RWSEvent =
|
|
463
|
+
| {
|
|
464
|
+
type: 'open'
|
|
465
|
+
}
|
|
466
|
+
| {
|
|
467
|
+
type: 'reconnected'
|
|
468
|
+
}
|
|
469
|
+
| {
|
|
470
|
+
type: 'message'
|
|
471
|
+
data: string | ArrayBuffer
|
|
472
|
+
}
|
|
473
|
+
| {
|
|
474
|
+
type: 'close'
|
|
475
|
+
code: number
|
|
476
|
+
reason: string
|
|
477
|
+
permanent: boolean
|
|
478
|
+
}
|
|
479
|
+
| {
|
|
480
|
+
type: 'error'
|
|
481
|
+
error: Error
|
|
482
|
+
}
|
|
280
483
|
interface WebSocketLike {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
code: number;
|
|
290
|
-
reason: string;
|
|
291
|
-
}) => void) | null;
|
|
292
|
-
send: (data: string | ArrayBuffer | ArrayBufferView) => void;
|
|
293
|
-
close: (code?: number, reason?: string) => void;
|
|
484
|
+
binaryType: string
|
|
485
|
+
readyState: number
|
|
486
|
+
onopen: ((ev: unknown) => void) | null
|
|
487
|
+
onmessage: ((ev: { data: string | ArrayBuffer }) => void) | null
|
|
488
|
+
onerror: ((ev: unknown) => void) | null
|
|
489
|
+
onclose: ((ev: { code: number; reason: string }) => void) | null
|
|
490
|
+
send: (data: string | ArrayBuffer | ArrayBufferView) => void
|
|
491
|
+
close: (code?: number, reason?: string) => void
|
|
294
492
|
}
|
|
295
|
-
type WebSocketFactory = (url: string) => WebSocketLike
|
|
493
|
+
type WebSocketFactory = (url: string) => WebSocketLike
|
|
296
494
|
interface RWSOptions {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
495
|
+
url: string
|
|
496
|
+
wsFactory: WebSocketFactory
|
|
497
|
+
maxRetries?: number
|
|
498
|
+
initialBackoffMs?: number
|
|
499
|
+
maxBackoffMs?: number
|
|
500
|
+
}
|
|
501
|
+
declare const createReconnectingWebSocket: (
|
|
502
|
+
options: RWSOptions,
|
|
503
|
+
onEvent: (ev: RWSEvent) => void,
|
|
504
|
+
) => {
|
|
505
|
+
send: (data: string | ArrayBuffer | ArrayBufferView) => void
|
|
506
|
+
close: (code?: number, reason?: string) => void
|
|
507
|
+
readyState: () => number
|
|
302
508
|
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
509
|
+
type ReconnectingWebSocket = ReturnType<typeof createReconnectingWebSocket>
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Canonical payload a tenant places in their VoIP/FCM push so an
|
|
513
|
+
* agent-initiated call can connect. It is the `callTokens.mint` result
|
|
514
|
+
* (token + transport) plus two optional display fields for the native
|
|
515
|
+
* incoming-call UI. The host receives the push, runs `parseIncomingCall`,
|
|
516
|
+
* and on accept passes `token` (+ transport / webrtcGatewayBase) into the
|
|
517
|
+
* voice client. Web background-wake is best-effort (Web Push); native is
|
|
518
|
+
* the real target. See docs/sdks.md "Agent-initiated calls".
|
|
519
|
+
*/
|
|
520
|
+
interface IncomingCallPayload {
|
|
521
|
+
token: string
|
|
522
|
+
agentId: string
|
|
523
|
+
transport: 'ws' | 'webrtc'
|
|
524
|
+
webrtcGatewayBase?: string
|
|
525
|
+
expiresAt?: number
|
|
526
|
+
agentName?: string
|
|
527
|
+
agentAvatarUrl?: string
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Validate + normalise a raw push payload into an IncomingCallPayload.
|
|
531
|
+
* Throws synchronously on malformed input. Unknown transports fall back to
|
|
532
|
+
* 'ws'; webrtcGatewayBase is ignored unless transport === 'webrtc'.
|
|
533
|
+
*/
|
|
534
|
+
declare const parseIncomingCall: (raw: unknown) => IncomingCallPayload
|
|
309
535
|
|
|
310
536
|
/**
|
|
311
537
|
* One-time SDK setup. Returns a factory you call `startCall` on for
|
|
@@ -332,6 +558,56 @@ type ReconnectingWebSocket = ReturnType<typeof createReconnectingWebSocket>;
|
|
|
332
558
|
* call.mute()
|
|
333
559
|
* call.end()
|
|
334
560
|
*/
|
|
335
|
-
declare function configureVoiceClient(config: VoiceClientConfig): VoiceClientFactory
|
|
561
|
+
declare function configureVoiceClient(config: VoiceClientConfig): VoiceClientFactory
|
|
336
562
|
|
|
337
|
-
export {
|
|
563
|
+
export {
|
|
564
|
+
type Call,
|
|
565
|
+
type CallEndEvent,
|
|
566
|
+
type CallEndReason,
|
|
567
|
+
type CallError,
|
|
568
|
+
type CallErrorCode,
|
|
569
|
+
type CallState,
|
|
570
|
+
type CaptureController,
|
|
571
|
+
type CaptureOptions,
|
|
572
|
+
type ClientTool,
|
|
573
|
+
type ClientToolMap,
|
|
574
|
+
type FetchToken,
|
|
575
|
+
type FetchTokenArgs,
|
|
576
|
+
type FetchTokenResult,
|
|
577
|
+
type IncomingCallPayload,
|
|
578
|
+
type JoinRoomOptions,
|
|
579
|
+
type OnAgentSpeakingChange,
|
|
580
|
+
type OnChunk,
|
|
581
|
+
type OnError,
|
|
582
|
+
type OnVolume$1 as OnVolume,
|
|
583
|
+
type PlaybackController,
|
|
584
|
+
type PlaybackOptions,
|
|
585
|
+
type ProtocolCallbacks,
|
|
586
|
+
type ProtocolState,
|
|
587
|
+
type RWSEvent,
|
|
588
|
+
type RWSOptions,
|
|
589
|
+
type ReconnectingWebSocket,
|
|
590
|
+
type RoomEventName,
|
|
591
|
+
type RoomEventPayloads,
|
|
592
|
+
type RoomParticipantInfo,
|
|
593
|
+
type RoomSession,
|
|
594
|
+
type ServerMessage,
|
|
595
|
+
type StartCallOptions,
|
|
596
|
+
type SystemMessage,
|
|
597
|
+
type TranscriptEntry,
|
|
598
|
+
type TranscriptMessage,
|
|
599
|
+
type VoiceClientConfig,
|
|
600
|
+
type VoiceClientFactory,
|
|
601
|
+
type VolumeEvent,
|
|
602
|
+
type WebSocketFactory,
|
|
603
|
+
type WebSocketLike,
|
|
604
|
+
buildWsUrl,
|
|
605
|
+
configureVoiceClient,
|
|
606
|
+
createAudioCapture,
|
|
607
|
+
createAudioPlayback,
|
|
608
|
+
createProtocolState,
|
|
609
|
+
createReconnectingWebSocket,
|
|
610
|
+
handleServerMessage,
|
|
611
|
+
joinRoom,
|
|
612
|
+
parseIncomingCall,
|
|
613
|
+
}
|